wlcore/wl12xx: add hw_init operation
[deliverable/linux.git] / drivers / net / wireless / ti / wlcore / main.c
1
2 /*
3 * This file is part of wl1271
4 *
5 * Copyright (C) 2008-2010 Nokia Corporation
6 *
7 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25 #include <linux/module.h>
26 #include <linux/firmware.h>
27 #include <linux/delay.h>
28 #include <linux/spi/spi.h>
29 #include <linux/crc32.h>
30 #include <linux/etherdevice.h>
31 #include <linux/vmalloc.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <linux/wl12xx.h>
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
37
38 #include "wlcore.h"
39 #include "debug.h"
40 #include "wl12xx_80211.h"
41 #include "io.h"
42 #include "event.h"
43 #include "tx.h"
44 #include "rx.h"
45 #include "ps.h"
46 #include "init.h"
47 #include "debugfs.h"
48 #include "cmd.h"
49 #include "boot.h"
50 #include "testmode.h"
51 #include "scan.h"
52 #include "hw_ops.h"
53
54 #define WL1271_BOOT_RETRIES 3
55
56 static struct conf_drv_settings default_conf = {
57 .sg = {
58 .params = {
59 [CONF_SG_ACL_BT_MASTER_MIN_BR] = 10,
60 [CONF_SG_ACL_BT_MASTER_MAX_BR] = 180,
61 [CONF_SG_ACL_BT_SLAVE_MIN_BR] = 10,
62 [CONF_SG_ACL_BT_SLAVE_MAX_BR] = 180,
63 [CONF_SG_ACL_BT_MASTER_MIN_EDR] = 10,
64 [CONF_SG_ACL_BT_MASTER_MAX_EDR] = 80,
65 [CONF_SG_ACL_BT_SLAVE_MIN_EDR] = 10,
66 [CONF_SG_ACL_BT_SLAVE_MAX_EDR] = 80,
67 [CONF_SG_ACL_WLAN_PS_MASTER_BR] = 8,
68 [CONF_SG_ACL_WLAN_PS_SLAVE_BR] = 8,
69 [CONF_SG_ACL_WLAN_PS_MASTER_EDR] = 20,
70 [CONF_SG_ACL_WLAN_PS_SLAVE_EDR] = 20,
71 [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MIN_BR] = 20,
72 [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MAX_BR] = 35,
73 [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MIN_BR] = 16,
74 [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MAX_BR] = 35,
75 [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MIN_EDR] = 32,
76 [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MAX_EDR] = 50,
77 [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MIN_EDR] = 28,
78 [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MAX_EDR] = 50,
79 [CONF_SG_ACL_ACTIVE_SCAN_WLAN_BR] = 10,
80 [CONF_SG_ACL_ACTIVE_SCAN_WLAN_EDR] = 20,
81 [CONF_SG_ACL_PASSIVE_SCAN_BT_BR] = 75,
82 [CONF_SG_ACL_PASSIVE_SCAN_WLAN_BR] = 15,
83 [CONF_SG_ACL_PASSIVE_SCAN_BT_EDR] = 27,
84 [CONF_SG_ACL_PASSIVE_SCAN_WLAN_EDR] = 17,
85 /* active scan params */
86 [CONF_SG_AUTO_SCAN_PROBE_REQ] = 170,
87 [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_HV3] = 50,
88 [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_A2DP] = 100,
89 /* passive scan params */
90 [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_A2DP_BR] = 800,
91 [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_A2DP_EDR] = 200,
92 [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_HV3] = 200,
93 /* passive scan in dual antenna params */
94 [CONF_SG_CONSECUTIVE_HV3_IN_PASSIVE_SCAN] = 0,
95 [CONF_SG_BCN_HV3_COLLISION_THRESH_IN_PASSIVE_SCAN] = 0,
96 [CONF_SG_TX_RX_PROTECTION_BWIDTH_IN_PASSIVE_SCAN] = 0,
97 /* general params */
98 [CONF_SG_STA_FORCE_PS_IN_BT_SCO] = 1,
99 [CONF_SG_ANTENNA_CONFIGURATION] = 0,
100 [CONF_SG_BEACON_MISS_PERCENT] = 60,
101 [CONF_SG_DHCP_TIME] = 5000,
102 [CONF_SG_RXT] = 1200,
103 [CONF_SG_TXT] = 1000,
104 [CONF_SG_ADAPTIVE_RXT_TXT] = 1,
105 [CONF_SG_GENERAL_USAGE_BIT_MAP] = 3,
106 [CONF_SG_HV3_MAX_SERVED] = 6,
107 [CONF_SG_PS_POLL_TIMEOUT] = 10,
108 [CONF_SG_UPSD_TIMEOUT] = 10,
109 [CONF_SG_CONSECUTIVE_CTS_THRESHOLD] = 2,
110 [CONF_SG_STA_RX_WINDOW_AFTER_DTIM] = 5,
111 [CONF_SG_STA_CONNECTION_PROTECTION_TIME] = 30,
112 /* AP params */
113 [CONF_AP_BEACON_MISS_TX] = 3,
114 [CONF_AP_RX_WINDOW_AFTER_BEACON] = 10,
115 [CONF_AP_BEACON_WINDOW_INTERVAL] = 2,
116 [CONF_AP_CONNECTION_PROTECTION_TIME] = 0,
117 [CONF_AP_BT_ACL_VAL_BT_SERVE_TIME] = 25,
118 [CONF_AP_BT_ACL_VAL_WL_SERVE_TIME] = 25,
119 /* CTS Diluting params */
120 [CONF_SG_CTS_DILUTED_BAD_RX_PACKETS_TH] = 0,
121 [CONF_SG_CTS_CHOP_IN_DUAL_ANT_SCO_MASTER] = 0,
122 },
123 .state = CONF_SG_PROTECTIVE,
124 },
125 .rx = {
126 .rx_msdu_life_time = 512000,
127 .packet_detection_threshold = 0,
128 .ps_poll_timeout = 15,
129 .upsd_timeout = 15,
130 .rts_threshold = IEEE80211_MAX_RTS_THRESHOLD,
131 .rx_cca_threshold = 0,
132 .irq_blk_threshold = 0xFFFF,
133 .irq_pkt_threshold = 0,
134 .irq_timeout = 600,
135 .queue_type = CONF_RX_QUEUE_TYPE_LOW_PRIORITY,
136 },
137 .tx = {
138 .tx_energy_detection = 0,
139 .sta_rc_conf = {
140 .enabled_rates = 0,
141 .short_retry_limit = 10,
142 .long_retry_limit = 10,
143 .aflags = 0,
144 },
145 .ac_conf_count = 4,
146 .ac_conf = {
147 [CONF_TX_AC_BE] = {
148 .ac = CONF_TX_AC_BE,
149 .cw_min = 15,
150 .cw_max = 63,
151 .aifsn = 3,
152 .tx_op_limit = 0,
153 },
154 [CONF_TX_AC_BK] = {
155 .ac = CONF_TX_AC_BK,
156 .cw_min = 15,
157 .cw_max = 63,
158 .aifsn = 7,
159 .tx_op_limit = 0,
160 },
161 [CONF_TX_AC_VI] = {
162 .ac = CONF_TX_AC_VI,
163 .cw_min = 15,
164 .cw_max = 63,
165 .aifsn = CONF_TX_AIFS_PIFS,
166 .tx_op_limit = 3008,
167 },
168 [CONF_TX_AC_VO] = {
169 .ac = CONF_TX_AC_VO,
170 .cw_min = 15,
171 .cw_max = 63,
172 .aifsn = CONF_TX_AIFS_PIFS,
173 .tx_op_limit = 1504,
174 },
175 },
176 .max_tx_retries = 100,
177 .ap_aging_period = 300,
178 .tid_conf_count = 4,
179 .tid_conf = {
180 [CONF_TX_AC_BE] = {
181 .queue_id = CONF_TX_AC_BE,
182 .channel_type = CONF_CHANNEL_TYPE_EDCF,
183 .tsid = CONF_TX_AC_BE,
184 .ps_scheme = CONF_PS_SCHEME_LEGACY,
185 .ack_policy = CONF_ACK_POLICY_LEGACY,
186 .apsd_conf = {0, 0},
187 },
188 [CONF_TX_AC_BK] = {
189 .queue_id = CONF_TX_AC_BK,
190 .channel_type = CONF_CHANNEL_TYPE_EDCF,
191 .tsid = CONF_TX_AC_BK,
192 .ps_scheme = CONF_PS_SCHEME_LEGACY,
193 .ack_policy = CONF_ACK_POLICY_LEGACY,
194 .apsd_conf = {0, 0},
195 },
196 [CONF_TX_AC_VI] = {
197 .queue_id = CONF_TX_AC_VI,
198 .channel_type = CONF_CHANNEL_TYPE_EDCF,
199 .tsid = CONF_TX_AC_VI,
200 .ps_scheme = CONF_PS_SCHEME_LEGACY,
201 .ack_policy = CONF_ACK_POLICY_LEGACY,
202 .apsd_conf = {0, 0},
203 },
204 [CONF_TX_AC_VO] = {
205 .queue_id = CONF_TX_AC_VO,
206 .channel_type = CONF_CHANNEL_TYPE_EDCF,
207 .tsid = CONF_TX_AC_VO,
208 .ps_scheme = CONF_PS_SCHEME_LEGACY,
209 .ack_policy = CONF_ACK_POLICY_LEGACY,
210 .apsd_conf = {0, 0},
211 },
212 },
213 .frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD,
214 .tx_compl_timeout = 700,
215 .tx_compl_threshold = 4,
216 .basic_rate = CONF_HW_BIT_RATE_1MBPS,
217 .basic_rate_5 = CONF_HW_BIT_RATE_6MBPS,
218 .tmpl_short_retry_limit = 10,
219 .tmpl_long_retry_limit = 10,
220 .tx_watchdog_timeout = 5000,
221 },
222 .conn = {
223 .wake_up_event = CONF_WAKE_UP_EVENT_DTIM,
224 .listen_interval = 1,
225 .suspend_wake_up_event = CONF_WAKE_UP_EVENT_N_DTIM,
226 .suspend_listen_interval = 3,
227 .bcn_filt_mode = CONF_BCN_FILT_MODE_ENABLED,
228 .bcn_filt_ie_count = 2,
229 .bcn_filt_ie = {
230 [0] = {
231 .ie = WLAN_EID_CHANNEL_SWITCH,
232 .rule = CONF_BCN_RULE_PASS_ON_APPEARANCE,
233 },
234 [1] = {
235 .ie = WLAN_EID_HT_OPERATION,
236 .rule = CONF_BCN_RULE_PASS_ON_CHANGE,
237 },
238 },
239 .synch_fail_thold = 10,
240 .bss_lose_timeout = 100,
241 .beacon_rx_timeout = 10000,
242 .broadcast_timeout = 20000,
243 .rx_broadcast_in_ps = 1,
244 .ps_poll_threshold = 10,
245 .bet_enable = CONF_BET_MODE_ENABLE,
246 .bet_max_consecutive = 50,
247 .psm_entry_retries = 8,
248 .psm_exit_retries = 16,
249 .psm_entry_nullfunc_retries = 3,
250 .dynamic_ps_timeout = 200,
251 .forced_ps = false,
252 .keep_alive_interval = 55000,
253 .max_listen_interval = 20,
254 },
255 .itrim = {
256 .enable = false,
257 .timeout = 50000,
258 },
259 .pm_config = {
260 .host_clk_settling_time = 5000,
261 .host_fast_wakeup_support = false
262 },
263 .roam_trigger = {
264 .trigger_pacing = 1,
265 .avg_weight_rssi_beacon = 20,
266 .avg_weight_rssi_data = 10,
267 .avg_weight_snr_beacon = 20,
268 .avg_weight_snr_data = 10,
269 },
270 .scan = {
271 .min_dwell_time_active = 7500,
272 .max_dwell_time_active = 30000,
273 .min_dwell_time_passive = 100000,
274 .max_dwell_time_passive = 100000,
275 .num_probe_reqs = 2,
276 .split_scan_timeout = 50000,
277 },
278 .sched_scan = {
279 /*
280 * Values are in TU/1000 but since sched scan FW command
281 * params are in TUs rounding up may occur.
282 */
283 .base_dwell_time = 7500,
284 .max_dwell_time_delta = 22500,
285 /* based on 250bits per probe @1Mbps */
286 .dwell_time_delta_per_probe = 2000,
287 /* based on 250bits per probe @6Mbps (plus a bit more) */
288 .dwell_time_delta_per_probe_5 = 350,
289 .dwell_time_passive = 100000,
290 .dwell_time_dfs = 150000,
291 .num_probe_reqs = 2,
292 .rssi_threshold = -90,
293 .snr_threshold = 0,
294 },
295 .rf = {
296 .tx_per_channel_power_compensation_2 = {
297 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
298 },
299 .tx_per_channel_power_compensation_5 = {
300 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
302 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
303 },
304 },
305 .ht = {
306 .rx_ba_win_size = 8,
307 .tx_ba_win_size = 64,
308 .inactivity_timeout = 10000,
309 .tx_ba_tid_bitmap = CONF_TX_BA_ENABLED_TID_BITMAP,
310 },
311 .mem_wl127x = {
312 .num_stations = 1,
313 .ssid_profiles = 1,
314 .rx_block_num = 70,
315 .tx_min_block_num = 40,
316 .dynamic_memory = 1,
317 .min_req_tx_blocks = 100,
318 .min_req_rx_blocks = 22,
319 .tx_min = 27,
320 },
321 .mem_wl128x = {
322 .num_stations = 1,
323 .ssid_profiles = 1,
324 .rx_block_num = 40,
325 .tx_min_block_num = 40,
326 .dynamic_memory = 1,
327 .min_req_tx_blocks = 45,
328 .min_req_rx_blocks = 22,
329 .tx_min = 27,
330 },
331 .fm_coex = {
332 .enable = true,
333 .swallow_period = 5,
334 .n_divider_fref_set_1 = 0xff, /* default */
335 .n_divider_fref_set_2 = 12,
336 .m_divider_fref_set_1 = 148,
337 .m_divider_fref_set_2 = 0xffff, /* default */
338 .coex_pll_stabilization_time = 0xffffffff, /* default */
339 .ldo_stabilization_time = 0xffff, /* default */
340 .fm_disturbed_band_margin = 0xff, /* default */
341 .swallow_clk_diff = 0xff, /* default */
342 },
343 .rx_streaming = {
344 .duration = 150,
345 .queues = 0x1,
346 .interval = 20,
347 .always = 0,
348 },
349 .fwlog = {
350 .mode = WL12XX_FWLOG_ON_DEMAND,
351 .mem_blocks = 2,
352 .severity = 0,
353 .timestamp = WL12XX_FWLOG_TIMESTAMP_DISABLED,
354 .output = WL12XX_FWLOG_OUTPUT_HOST,
355 .threshold = 0,
356 },
357 .rate = {
358 .rate_retry_score = 32000,
359 .per_add = 8192,
360 .per_th1 = 2048,
361 .per_th2 = 4096,
362 .max_per = 8100,
363 .inverse_curiosity_factor = 5,
364 .tx_fail_low_th = 4,
365 .tx_fail_high_th = 10,
366 .per_alpha_shift = 4,
367 .per_add_shift = 13,
368 .per_beta1_shift = 10,
369 .per_beta2_shift = 8,
370 .rate_check_up = 2,
371 .rate_check_down = 12,
372 .rate_retry_policy = {
373 0x00, 0x00, 0x00, 0x00, 0x00,
374 0x00, 0x00, 0x00, 0x00, 0x00,
375 0x00, 0x00, 0x00,
376 },
377 },
378 .hangover = {
379 .recover_time = 0,
380 .hangover_period = 20,
381 .dynamic_mode = 1,
382 .early_termination_mode = 1,
383 .max_period = 20,
384 .min_period = 1,
385 .increase_delta = 1,
386 .decrease_delta = 2,
387 .quiet_time = 4,
388 .increase_time = 1,
389 .window_size = 16,
390 },
391 };
392
393 static char *fwlog_param;
394 static bool bug_on_recovery;
395
396 static void __wl1271_op_remove_interface(struct wl1271 *wl,
397 struct ieee80211_vif *vif,
398 bool reset_tx_queues);
399 static void wl1271_op_stop(struct ieee80211_hw *hw);
400 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif);
401
402 static int wl12xx_set_authorized(struct wl1271 *wl,
403 struct wl12xx_vif *wlvif)
404 {
405 int ret;
406
407 if (WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS))
408 return -EINVAL;
409
410 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
411 return 0;
412
413 if (test_and_set_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags))
414 return 0;
415
416 ret = wl12xx_cmd_set_peer_state(wl, wlvif->sta.hlid);
417 if (ret < 0)
418 return ret;
419
420 wl12xx_croc(wl, wlvif->role_id);
421
422 wl1271_info("Association completed.");
423 return 0;
424 }
425
426 static int wl1271_reg_notify(struct wiphy *wiphy,
427 struct regulatory_request *request)
428 {
429 struct ieee80211_supported_band *band;
430 struct ieee80211_channel *ch;
431 int i;
432
433 band = wiphy->bands[IEEE80211_BAND_5GHZ];
434 for (i = 0; i < band->n_channels; i++) {
435 ch = &band->channels[i];
436 if (ch->flags & IEEE80211_CHAN_DISABLED)
437 continue;
438
439 if (ch->flags & IEEE80211_CHAN_RADAR)
440 ch->flags |= IEEE80211_CHAN_NO_IBSS |
441 IEEE80211_CHAN_PASSIVE_SCAN;
442
443 }
444
445 return 0;
446 }
447
448 static int wl1271_set_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif,
449 bool enable)
450 {
451 int ret = 0;
452
453 /* we should hold wl->mutex */
454 ret = wl1271_acx_ps_rx_streaming(wl, wlvif, enable);
455 if (ret < 0)
456 goto out;
457
458 if (enable)
459 set_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
460 else
461 clear_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
462 out:
463 return ret;
464 }
465
466 /*
467 * this function is being called when the rx_streaming interval
468 * has beed changed or rx_streaming should be disabled
469 */
470 int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif)
471 {
472 int ret = 0;
473 int period = wl->conf.rx_streaming.interval;
474
475 /* don't reconfigure if rx_streaming is disabled */
476 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
477 goto out;
478
479 /* reconfigure/disable according to new streaming_period */
480 if (period &&
481 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
482 (wl->conf.rx_streaming.always ||
483 test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
484 ret = wl1271_set_rx_streaming(wl, wlvif, true);
485 else {
486 ret = wl1271_set_rx_streaming(wl, wlvif, false);
487 /* don't cancel_work_sync since we might deadlock */
488 del_timer_sync(&wlvif->rx_streaming_timer);
489 }
490 out:
491 return ret;
492 }
493
494 static void wl1271_rx_streaming_enable_work(struct work_struct *work)
495 {
496 int ret;
497 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
498 rx_streaming_enable_work);
499 struct wl1271 *wl = wlvif->wl;
500
501 mutex_lock(&wl->mutex);
502
503 if (test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags) ||
504 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
505 (!wl->conf.rx_streaming.always &&
506 !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
507 goto out;
508
509 if (!wl->conf.rx_streaming.interval)
510 goto out;
511
512 ret = wl1271_ps_elp_wakeup(wl);
513 if (ret < 0)
514 goto out;
515
516 ret = wl1271_set_rx_streaming(wl, wlvif, true);
517 if (ret < 0)
518 goto out_sleep;
519
520 /* stop it after some time of inactivity */
521 mod_timer(&wlvif->rx_streaming_timer,
522 jiffies + msecs_to_jiffies(wl->conf.rx_streaming.duration));
523
524 out_sleep:
525 wl1271_ps_elp_sleep(wl);
526 out:
527 mutex_unlock(&wl->mutex);
528 }
529
530 static void wl1271_rx_streaming_disable_work(struct work_struct *work)
531 {
532 int ret;
533 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
534 rx_streaming_disable_work);
535 struct wl1271 *wl = wlvif->wl;
536
537 mutex_lock(&wl->mutex);
538
539 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
540 goto out;
541
542 ret = wl1271_ps_elp_wakeup(wl);
543 if (ret < 0)
544 goto out;
545
546 ret = wl1271_set_rx_streaming(wl, wlvif, false);
547 if (ret)
548 goto out_sleep;
549
550 out_sleep:
551 wl1271_ps_elp_sleep(wl);
552 out:
553 mutex_unlock(&wl->mutex);
554 }
555
556 static void wl1271_rx_streaming_timer(unsigned long data)
557 {
558 struct wl12xx_vif *wlvif = (struct wl12xx_vif *)data;
559 struct wl1271 *wl = wlvif->wl;
560 ieee80211_queue_work(wl->hw, &wlvif->rx_streaming_disable_work);
561 }
562
563 /* wl->mutex must be taken */
564 void wl12xx_rearm_tx_watchdog_locked(struct wl1271 *wl)
565 {
566 /* if the watchdog is not armed, don't do anything */
567 if (wl->tx_allocated_blocks == 0)
568 return;
569
570 cancel_delayed_work(&wl->tx_watchdog_work);
571 ieee80211_queue_delayed_work(wl->hw, &wl->tx_watchdog_work,
572 msecs_to_jiffies(wl->conf.tx.tx_watchdog_timeout));
573 }
574
575 static void wl12xx_tx_watchdog_work(struct work_struct *work)
576 {
577 struct delayed_work *dwork;
578 struct wl1271 *wl;
579
580 dwork = container_of(work, struct delayed_work, work);
581 wl = container_of(dwork, struct wl1271, tx_watchdog_work);
582
583 mutex_lock(&wl->mutex);
584
585 if (unlikely(wl->state == WL1271_STATE_OFF))
586 goto out;
587
588 /* Tx went out in the meantime - everything is ok */
589 if (unlikely(wl->tx_allocated_blocks == 0))
590 goto out;
591
592 /*
593 * if a ROC is in progress, we might not have any Tx for a long
594 * time (e.g. pending Tx on the non-ROC channels)
595 */
596 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
597 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to ROC",
598 wl->conf.tx.tx_watchdog_timeout);
599 wl12xx_rearm_tx_watchdog_locked(wl);
600 goto out;
601 }
602
603 /*
604 * if a scan is in progress, we might not have any Tx for a long
605 * time
606 */
607 if (wl->scan.state != WL1271_SCAN_STATE_IDLE) {
608 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to scan",
609 wl->conf.tx.tx_watchdog_timeout);
610 wl12xx_rearm_tx_watchdog_locked(wl);
611 goto out;
612 }
613
614 /*
615 * AP might cache a frame for a long time for a sleeping station,
616 * so rearm the timer if there's an AP interface with stations. If
617 * Tx is genuinely stuck we will most hopefully discover it when all
618 * stations are removed due to inactivity.
619 */
620 if (wl->active_sta_count) {
621 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms. AP has "
622 " %d stations",
623 wl->conf.tx.tx_watchdog_timeout,
624 wl->active_sta_count);
625 wl12xx_rearm_tx_watchdog_locked(wl);
626 goto out;
627 }
628
629 wl1271_error("Tx stuck (in FW) for %d ms. Starting recovery",
630 wl->conf.tx.tx_watchdog_timeout);
631 wl12xx_queue_recovery_work(wl);
632
633 out:
634 mutex_unlock(&wl->mutex);
635 }
636
637 static void wl1271_conf_init(struct wl1271 *wl)
638 {
639
640 /*
641 * This function applies the default configuration to the driver. This
642 * function is invoked upon driver load (spi probe.)
643 *
644 * The configuration is stored in a run-time structure in order to
645 * facilitate for run-time adjustment of any of the parameters. Making
646 * changes to the configuration structure will apply the new values on
647 * the next interface up (wl1271_op_start.)
648 */
649
650 /* apply driver default configuration */
651 memcpy(&wl->conf, &default_conf, sizeof(default_conf));
652
653 /* Adjust settings according to optional module parameters */
654 if (fwlog_param) {
655 if (!strcmp(fwlog_param, "continuous")) {
656 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
657 } else if (!strcmp(fwlog_param, "ondemand")) {
658 wl->conf.fwlog.mode = WL12XX_FWLOG_ON_DEMAND;
659 } else if (!strcmp(fwlog_param, "dbgpins")) {
660 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
661 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_DBG_PINS;
662 } else if (!strcmp(fwlog_param, "disable")) {
663 wl->conf.fwlog.mem_blocks = 0;
664 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_NONE;
665 } else {
666 wl1271_error("Unknown fwlog parameter %s", fwlog_param);
667 }
668 }
669 }
670
671 static int wl1271_plt_init(struct wl1271 *wl)
672 {
673 int ret;
674
675 ret = wl->ops->hw_init(wl);
676 if (ret < 0)
677 return ret;
678
679 ret = wl1271_acx_init_mem_config(wl);
680 if (ret < 0)
681 return ret;
682
683 ret = wl12xx_acx_mem_cfg(wl);
684 if (ret < 0)
685 goto out_free_memmap;
686
687 /* Enable data path */
688 ret = wl1271_cmd_data_path(wl, 1);
689 if (ret < 0)
690 goto out_free_memmap;
691
692 /* Configure for CAM power saving (ie. always active) */
693 ret = wl1271_acx_sleep_auth(wl, WL1271_PSM_CAM);
694 if (ret < 0)
695 goto out_free_memmap;
696
697 /* configure PM */
698 ret = wl1271_acx_pm_config(wl);
699 if (ret < 0)
700 goto out_free_memmap;
701
702 return 0;
703
704 out_free_memmap:
705 kfree(wl->target_mem_map);
706 wl->target_mem_map = NULL;
707
708 return ret;
709 }
710
711 static void wl12xx_irq_ps_regulate_link(struct wl1271 *wl,
712 struct wl12xx_vif *wlvif,
713 u8 hlid, u8 tx_pkts)
714 {
715 bool fw_ps, single_sta;
716
717 fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
718 single_sta = (wl->active_sta_count == 1);
719
720 /*
721 * Wake up from high level PS if the STA is asleep with too little
722 * packets in FW or if the STA is awake.
723 */
724 if (!fw_ps || tx_pkts < WL1271_PS_STA_MAX_PACKETS)
725 wl12xx_ps_link_end(wl, wlvif, hlid);
726
727 /*
728 * Start high-level PS if the STA is asleep with enough blocks in FW.
729 * Make an exception if this is the only connected station. In this
730 * case FW-memory congestion is not a problem.
731 */
732 else if (!single_sta && fw_ps && tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
733 wl12xx_ps_link_start(wl, wlvif, hlid, true);
734 }
735
736 static void wl12xx_irq_update_links_status(struct wl1271 *wl,
737 struct wl12xx_vif *wlvif,
738 struct wl12xx_fw_status *status)
739 {
740 struct wl1271_link *lnk;
741 u32 cur_fw_ps_map;
742 u8 hlid, cnt;
743
744 /* TODO: also use link_fast_bitmap here */
745
746 cur_fw_ps_map = le32_to_cpu(status->link_ps_bitmap);
747 if (wl->ap_fw_ps_map != cur_fw_ps_map) {
748 wl1271_debug(DEBUG_PSM,
749 "link ps prev 0x%x cur 0x%x changed 0x%x",
750 wl->ap_fw_ps_map, cur_fw_ps_map,
751 wl->ap_fw_ps_map ^ cur_fw_ps_map);
752
753 wl->ap_fw_ps_map = cur_fw_ps_map;
754 }
755
756 for_each_set_bit(hlid, wlvif->ap.sta_hlid_map, WL12XX_MAX_LINKS) {
757 lnk = &wl->links[hlid];
758 cnt = status->tx_lnk_free_pkts[hlid] - lnk->prev_freed_pkts;
759
760 lnk->prev_freed_pkts = status->tx_lnk_free_pkts[hlid];
761 lnk->allocated_pkts -= cnt;
762
763 wl12xx_irq_ps_regulate_link(wl, wlvif, hlid,
764 lnk->allocated_pkts);
765 }
766 }
767
768 static void wl12xx_fw_status(struct wl1271 *wl,
769 struct wl12xx_fw_status *status)
770 {
771 struct wl12xx_vif *wlvif;
772 struct timespec ts;
773 u32 old_tx_blk_count = wl->tx_blocks_available;
774 int avail, freed_blocks;
775 int i;
776
777 wlcore_raw_read_data(wl, REG_RAW_FW_STATUS_ADDR, status,
778 sizeof(*status), false);
779
780 wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
781 "drv_rx_counter = %d, tx_results_counter = %d)",
782 status->intr,
783 status->fw_rx_counter,
784 status->drv_rx_counter,
785 status->tx_results_counter);
786
787 for (i = 0; i < NUM_TX_QUEUES; i++) {
788 /* prevent wrap-around in freed-packets counter */
789 wl->tx_allocated_pkts[i] -=
790 (status->tx_released_pkts[i] -
791 wl->tx_pkts_freed[i]) & 0xff;
792
793 wl->tx_pkts_freed[i] = status->tx_released_pkts[i];
794 }
795
796 /* prevent wrap-around in total blocks counter */
797 if (likely(wl->tx_blocks_freed <=
798 le32_to_cpu(status->total_released_blks)))
799 freed_blocks = le32_to_cpu(status->total_released_blks) -
800 wl->tx_blocks_freed;
801 else
802 freed_blocks = 0x100000000LL - wl->tx_blocks_freed +
803 le32_to_cpu(status->total_released_blks);
804
805 wl->tx_blocks_freed = le32_to_cpu(status->total_released_blks);
806
807 wl->tx_allocated_blocks -= freed_blocks;
808
809 /*
810 * If the FW freed some blocks:
811 * If we still have allocated blocks - re-arm the timer, Tx is
812 * not stuck. Otherwise, cancel the timer (no Tx currently).
813 */
814 if (freed_blocks) {
815 if (wl->tx_allocated_blocks)
816 wl12xx_rearm_tx_watchdog_locked(wl);
817 else
818 cancel_delayed_work(&wl->tx_watchdog_work);
819 }
820
821 avail = le32_to_cpu(status->tx_total) - wl->tx_allocated_blocks;
822
823 /*
824 * The FW might change the total number of TX memblocks before
825 * we get a notification about blocks being released. Thus, the
826 * available blocks calculation might yield a temporary result
827 * which is lower than the actual available blocks. Keeping in
828 * mind that only blocks that were allocated can be moved from
829 * TX to RX, tx_blocks_available should never decrease here.
830 */
831 wl->tx_blocks_available = max((int)wl->tx_blocks_available,
832 avail);
833
834 /* if more blocks are available now, tx work can be scheduled */
835 if (wl->tx_blocks_available > old_tx_blk_count)
836 clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
837
838 /* for AP update num of allocated TX blocks per link and ps status */
839 wl12xx_for_each_wlvif_ap(wl, wlvif) {
840 wl12xx_irq_update_links_status(wl, wlvif, status);
841 }
842
843 /* update the host-chipset time offset */
844 getnstimeofday(&ts);
845 wl->time_offset = (timespec_to_ns(&ts) >> 10) -
846 (s64)le32_to_cpu(status->fw_localtime);
847 }
848
849 static void wl1271_flush_deferred_work(struct wl1271 *wl)
850 {
851 struct sk_buff *skb;
852
853 /* Pass all received frames to the network stack */
854 while ((skb = skb_dequeue(&wl->deferred_rx_queue)))
855 ieee80211_rx_ni(wl->hw, skb);
856
857 /* Return sent skbs to the network stack */
858 while ((skb = skb_dequeue(&wl->deferred_tx_queue)))
859 ieee80211_tx_status_ni(wl->hw, skb);
860 }
861
862 static void wl1271_netstack_work(struct work_struct *work)
863 {
864 struct wl1271 *wl =
865 container_of(work, struct wl1271, netstack_work);
866
867 do {
868 wl1271_flush_deferred_work(wl);
869 } while (skb_queue_len(&wl->deferred_rx_queue));
870 }
871
872 #define WL1271_IRQ_MAX_LOOPS 256
873
874 static irqreturn_t wl1271_irq(int irq, void *cookie)
875 {
876 int ret;
877 u32 intr;
878 int loopcount = WL1271_IRQ_MAX_LOOPS;
879 struct wl1271 *wl = (struct wl1271 *)cookie;
880 bool done = false;
881 unsigned int defer_count;
882 unsigned long flags;
883
884 /* TX might be handled here, avoid redundant work */
885 set_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
886 cancel_work_sync(&wl->tx_work);
887
888 /*
889 * In case edge triggered interrupt must be used, we cannot iterate
890 * more than once without introducing race conditions with the hardirq.
891 */
892 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
893 loopcount = 1;
894
895 mutex_lock(&wl->mutex);
896
897 wl1271_debug(DEBUG_IRQ, "IRQ work");
898
899 if (unlikely(wl->state == WL1271_STATE_OFF))
900 goto out;
901
902 ret = wl1271_ps_elp_wakeup(wl);
903 if (ret < 0)
904 goto out;
905
906 while (!done && loopcount--) {
907 /*
908 * In order to avoid a race with the hardirq, clear the flag
909 * before acknowledging the chip. Since the mutex is held,
910 * wl1271_ps_elp_wakeup cannot be called concurrently.
911 */
912 clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
913 smp_mb__after_clear_bit();
914
915 wl12xx_fw_status(wl, wl->fw_status);
916
917 wlcore_hw_tx_immediate_compl(wl);
918
919 intr = le32_to_cpu(wl->fw_status->intr);
920 intr &= WL1271_INTR_MASK;
921 if (!intr) {
922 done = true;
923 continue;
924 }
925
926 if (unlikely(intr & WL1271_ACX_INTR_WATCHDOG)) {
927 wl1271_error("watchdog interrupt received! "
928 "starting recovery.");
929 wl12xx_queue_recovery_work(wl);
930
931 /* restarting the chip. ignore any other interrupt. */
932 goto out;
933 }
934
935 if (likely(intr & WL1271_ACX_INTR_DATA)) {
936 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
937
938 wl12xx_rx(wl, wl->fw_status);
939
940 /* Check if any tx blocks were freed */
941 spin_lock_irqsave(&wl->wl_lock, flags);
942 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
943 wl1271_tx_total_queue_count(wl) > 0) {
944 spin_unlock_irqrestore(&wl->wl_lock, flags);
945 /*
946 * In order to avoid starvation of the TX path,
947 * call the work function directly.
948 */
949 wl1271_tx_work_locked(wl);
950 } else {
951 spin_unlock_irqrestore(&wl->wl_lock, flags);
952 }
953
954 /* check for tx results */
955 wlcore_hw_tx_delayed_compl(wl);
956
957 /* Make sure the deferred queues don't get too long */
958 defer_count = skb_queue_len(&wl->deferred_tx_queue) +
959 skb_queue_len(&wl->deferred_rx_queue);
960 if (defer_count > WL1271_DEFERRED_QUEUE_LIMIT)
961 wl1271_flush_deferred_work(wl);
962 }
963
964 if (intr & WL1271_ACX_INTR_EVENT_A) {
965 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
966 wl1271_event_handle(wl, 0);
967 }
968
969 if (intr & WL1271_ACX_INTR_EVENT_B) {
970 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
971 wl1271_event_handle(wl, 1);
972 }
973
974 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
975 wl1271_debug(DEBUG_IRQ,
976 "WL1271_ACX_INTR_INIT_COMPLETE");
977
978 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
979 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
980 }
981
982 wl1271_ps_elp_sleep(wl);
983
984 out:
985 spin_lock_irqsave(&wl->wl_lock, flags);
986 /* In case TX was not handled here, queue TX work */
987 clear_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
988 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
989 wl1271_tx_total_queue_count(wl) > 0)
990 ieee80211_queue_work(wl->hw, &wl->tx_work);
991 spin_unlock_irqrestore(&wl->wl_lock, flags);
992
993 mutex_unlock(&wl->mutex);
994
995 return IRQ_HANDLED;
996 }
997
998 struct vif_counter_data {
999 u8 counter;
1000
1001 struct ieee80211_vif *cur_vif;
1002 bool cur_vif_running;
1003 };
1004
1005 static void wl12xx_vif_count_iter(void *data, u8 *mac,
1006 struct ieee80211_vif *vif)
1007 {
1008 struct vif_counter_data *counter = data;
1009
1010 counter->counter++;
1011 if (counter->cur_vif == vif)
1012 counter->cur_vif_running = true;
1013 }
1014
1015 /* caller must not hold wl->mutex, as it might deadlock */
1016 static void wl12xx_get_vif_count(struct ieee80211_hw *hw,
1017 struct ieee80211_vif *cur_vif,
1018 struct vif_counter_data *data)
1019 {
1020 memset(data, 0, sizeof(*data));
1021 data->cur_vif = cur_vif;
1022
1023 ieee80211_iterate_active_interfaces(hw,
1024 wl12xx_vif_count_iter, data);
1025 }
1026
1027 static int wl12xx_fetch_firmware(struct wl1271 *wl, bool plt)
1028 {
1029 const struct firmware *fw;
1030 const char *fw_name;
1031 enum wl12xx_fw_type fw_type;
1032 int ret;
1033
1034 if (plt) {
1035 fw_type = WL12XX_FW_TYPE_PLT;
1036 fw_name = wl->plt_fw_name;
1037 } else {
1038 /*
1039 * we can't call wl12xx_get_vif_count() here because
1040 * wl->mutex is taken, so use the cached last_vif_count value
1041 */
1042 if (wl->last_vif_count > 1) {
1043 fw_type = WL12XX_FW_TYPE_MULTI;
1044 fw_name = wl->mr_fw_name;
1045 } else {
1046 fw_type = WL12XX_FW_TYPE_NORMAL;
1047 fw_name = wl->sr_fw_name;
1048 }
1049 }
1050
1051 if (wl->fw_type == fw_type)
1052 return 0;
1053
1054 wl1271_debug(DEBUG_BOOT, "booting firmware %s", fw_name);
1055
1056 ret = request_firmware(&fw, fw_name, wl->dev);
1057
1058 if (ret < 0) {
1059 wl1271_error("could not get firmware %s: %d", fw_name, ret);
1060 return ret;
1061 }
1062
1063 if (fw->size % 4) {
1064 wl1271_error("firmware size is not multiple of 32 bits: %zu",
1065 fw->size);
1066 ret = -EILSEQ;
1067 goto out;
1068 }
1069
1070 vfree(wl->fw);
1071 wl->fw_type = WL12XX_FW_TYPE_NONE;
1072 wl->fw_len = fw->size;
1073 wl->fw = vmalloc(wl->fw_len);
1074
1075 if (!wl->fw) {
1076 wl1271_error("could not allocate memory for the firmware");
1077 ret = -ENOMEM;
1078 goto out;
1079 }
1080
1081 memcpy(wl->fw, fw->data, wl->fw_len);
1082 ret = 0;
1083 wl->fw_type = fw_type;
1084 out:
1085 release_firmware(fw);
1086
1087 return ret;
1088 }
1089
1090 static int wl1271_fetch_nvs(struct wl1271 *wl)
1091 {
1092 const struct firmware *fw;
1093 int ret;
1094
1095 ret = request_firmware(&fw, WL12XX_NVS_NAME, wl->dev);
1096
1097 if (ret < 0) {
1098 wl1271_error("could not get nvs file %s: %d", WL12XX_NVS_NAME,
1099 ret);
1100 return ret;
1101 }
1102
1103 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
1104
1105 if (!wl->nvs) {
1106 wl1271_error("could not allocate memory for the nvs file");
1107 ret = -ENOMEM;
1108 goto out;
1109 }
1110
1111 wl->nvs_len = fw->size;
1112
1113 out:
1114 release_firmware(fw);
1115
1116 return ret;
1117 }
1118
1119 void wl12xx_queue_recovery_work(struct wl1271 *wl)
1120 {
1121 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1122 ieee80211_queue_work(wl->hw, &wl->recovery_work);
1123 }
1124
1125 size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen)
1126 {
1127 size_t len = 0;
1128
1129 /* The FW log is a length-value list, find where the log end */
1130 while (len < maxlen) {
1131 if (memblock[len] == 0)
1132 break;
1133 if (len + memblock[len] + 1 > maxlen)
1134 break;
1135 len += memblock[len] + 1;
1136 }
1137
1138 /* Make sure we have enough room */
1139 len = min(len, (size_t)(PAGE_SIZE - wl->fwlog_size));
1140
1141 /* Fill the FW log file, consumed by the sysfs fwlog entry */
1142 memcpy(wl->fwlog + wl->fwlog_size, memblock, len);
1143 wl->fwlog_size += len;
1144
1145 return len;
1146 }
1147
1148 static void wl12xx_read_fwlog_panic(struct wl1271 *wl)
1149 {
1150 u32 addr;
1151 u32 first_addr;
1152 u8 *block;
1153
1154 if ((wl->quirks & WLCORE_QUIRK_FWLOG_NOT_IMPLEMENTED) ||
1155 (wl->conf.fwlog.mode != WL12XX_FWLOG_ON_DEMAND) ||
1156 (wl->conf.fwlog.mem_blocks == 0))
1157 return;
1158
1159 wl1271_info("Reading FW panic log");
1160
1161 block = kmalloc(WL12XX_HW_BLOCK_SIZE, GFP_KERNEL);
1162 if (!block)
1163 return;
1164
1165 /*
1166 * Make sure the chip is awake and the logger isn't active.
1167 * This might fail if the firmware hanged.
1168 */
1169 if (!wl1271_ps_elp_wakeup(wl))
1170 wl12xx_cmd_stop_fwlog(wl);
1171
1172 /* Read the first memory block address */
1173 wl12xx_fw_status(wl, wl->fw_status);
1174 first_addr = le32_to_cpu(wl->fw_status->log_start_addr);
1175 if (!first_addr)
1176 goto out;
1177
1178 /* Traverse the memory blocks linked list */
1179 addr = first_addr;
1180 do {
1181 memset(block, 0, WL12XX_HW_BLOCK_SIZE);
1182 wl1271_read_hwaddr(wl, addr, block, WL12XX_HW_BLOCK_SIZE,
1183 false);
1184
1185 /*
1186 * Memory blocks are linked to one another. The first 4 bytes
1187 * of each memory block hold the hardware address of the next
1188 * one. The last memory block points to the first one.
1189 */
1190 addr = le32_to_cpup((__le32 *)block);
1191 if (!wl12xx_copy_fwlog(wl, block + sizeof(addr),
1192 WL12XX_HW_BLOCK_SIZE - sizeof(addr)))
1193 break;
1194 } while (addr && (addr != first_addr));
1195
1196 wake_up_interruptible(&wl->fwlog_waitq);
1197
1198 out:
1199 kfree(block);
1200 }
1201
1202 static void wl1271_recovery_work(struct work_struct *work)
1203 {
1204 struct wl1271 *wl =
1205 container_of(work, struct wl1271, recovery_work);
1206 struct wl12xx_vif *wlvif;
1207 struct ieee80211_vif *vif;
1208
1209 mutex_lock(&wl->mutex);
1210
1211 if (wl->state != WL1271_STATE_ON || wl->plt)
1212 goto out_unlock;
1213
1214 /* Avoid a recursive recovery */
1215 set_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
1216
1217 wl12xx_read_fwlog_panic(wl);
1218
1219 wl1271_info("Hardware recovery in progress. FW ver: %s pc: 0x%x",
1220 wl->chip.fw_ver_str,
1221 wlcore_read_reg(wl, REG_PC_ON_RECOVERY));
1222
1223 BUG_ON(bug_on_recovery &&
1224 !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags));
1225
1226 /*
1227 * Advance security sequence number to overcome potential progress
1228 * in the firmware during recovery. This doens't hurt if the network is
1229 * not encrypted.
1230 */
1231 wl12xx_for_each_wlvif(wl, wlvif) {
1232 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
1233 test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
1234 wlvif->tx_security_seq +=
1235 WL1271_TX_SQN_POST_RECOVERY_PADDING;
1236 }
1237
1238 /* Prevent spurious TX during FW restart */
1239 ieee80211_stop_queues(wl->hw);
1240
1241 if (wl->sched_scanning) {
1242 ieee80211_sched_scan_stopped(wl->hw);
1243 wl->sched_scanning = false;
1244 }
1245
1246 /* reboot the chipset */
1247 while (!list_empty(&wl->wlvif_list)) {
1248 wlvif = list_first_entry(&wl->wlvif_list,
1249 struct wl12xx_vif, list);
1250 vif = wl12xx_wlvif_to_vif(wlvif);
1251 __wl1271_op_remove_interface(wl, vif, false);
1252 }
1253 mutex_unlock(&wl->mutex);
1254 wl1271_op_stop(wl->hw);
1255
1256 clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
1257
1258 ieee80211_restart_hw(wl->hw);
1259
1260 /*
1261 * Its safe to enable TX now - the queues are stopped after a request
1262 * to restart the HW.
1263 */
1264 ieee80211_wake_queues(wl->hw);
1265 return;
1266 out_unlock:
1267 mutex_unlock(&wl->mutex);
1268 }
1269
1270 static void wl1271_fw_wakeup(struct wl1271 *wl)
1271 {
1272 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
1273 }
1274
1275 static int wl1271_setup(struct wl1271 *wl)
1276 {
1277 wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
1278 if (!wl->fw_status)
1279 return -ENOMEM;
1280
1281 wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
1282 if (!wl->tx_res_if) {
1283 kfree(wl->fw_status);
1284 return -ENOMEM;
1285 }
1286
1287 return 0;
1288 }
1289
1290 static int wl12xx_set_power_on(struct wl1271 *wl)
1291 {
1292 int ret;
1293
1294 msleep(WL1271_PRE_POWER_ON_SLEEP);
1295 ret = wl1271_power_on(wl);
1296 if (ret < 0)
1297 goto out;
1298 msleep(WL1271_POWER_ON_SLEEP);
1299 wl1271_io_reset(wl);
1300 wl1271_io_init(wl);
1301
1302 wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
1303
1304 /* ELP module wake up */
1305 wl1271_fw_wakeup(wl);
1306
1307 out:
1308 return ret;
1309 }
1310
1311 static int wl12xx_chip_wakeup(struct wl1271 *wl, bool plt)
1312 {
1313 int ret = 0;
1314
1315 ret = wl12xx_set_power_on(wl);
1316 if (ret < 0)
1317 goto out;
1318
1319 /*
1320 * For wl127x based devices we could use the default block
1321 * size (512 bytes), but due to a bug in the sdio driver, we
1322 * need to set it explicitly after the chip is powered on. To
1323 * simplify the code and since the performance impact is
1324 * negligible, we use the same block size for all different
1325 * chip types.
1326 */
1327 if (wl1271_set_block_size(wl))
1328 wl->quirks |= WLCORE_QUIRK_TX_BLOCKSIZE_ALIGN;
1329
1330 ret = wl->ops->identify_chip(wl);
1331 if (ret < 0)
1332 goto out;
1333
1334 /* TODO: make sure the lower driver has set things up correctly */
1335
1336 ret = wl1271_setup(wl);
1337 if (ret < 0)
1338 goto out;
1339
1340 ret = wl12xx_fetch_firmware(wl, plt);
1341 if (ret < 0)
1342 goto out;
1343
1344 /* No NVS from netlink, try to get it from the filesystem */
1345 if (wl->nvs == NULL) {
1346 ret = wl1271_fetch_nvs(wl);
1347 if (ret < 0)
1348 goto out;
1349 }
1350
1351 out:
1352 return ret;
1353 }
1354
1355 int wl1271_plt_start(struct wl1271 *wl)
1356 {
1357 int retries = WL1271_BOOT_RETRIES;
1358 struct wiphy *wiphy = wl->hw->wiphy;
1359 int ret;
1360
1361 mutex_lock(&wl->mutex);
1362
1363 wl1271_notice("power up");
1364
1365 if (wl->state != WL1271_STATE_OFF) {
1366 wl1271_error("cannot go into PLT state because not "
1367 "in off state: %d", wl->state);
1368 ret = -EBUSY;
1369 goto out;
1370 }
1371
1372 while (retries) {
1373 retries--;
1374 ret = wl12xx_chip_wakeup(wl, true);
1375 if (ret < 0)
1376 goto power_off;
1377
1378 ret = wl->ops->boot(wl);
1379 if (ret < 0)
1380 goto power_off;
1381
1382 ret = wl1271_plt_init(wl);
1383 if (ret < 0)
1384 goto irq_disable;
1385
1386 wl->plt = true;
1387 wl->state = WL1271_STATE_ON;
1388 wl1271_notice("firmware booted in PLT mode (%s)",
1389 wl->chip.fw_ver_str);
1390
1391 /* update hw/fw version info in wiphy struct */
1392 wiphy->hw_version = wl->chip.id;
1393 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1394 sizeof(wiphy->fw_version));
1395
1396 goto out;
1397
1398 irq_disable:
1399 mutex_unlock(&wl->mutex);
1400 /* Unlocking the mutex in the middle of handling is
1401 inherently unsafe. In this case we deem it safe to do,
1402 because we need to let any possibly pending IRQ out of
1403 the system (and while we are WL1271_STATE_OFF the IRQ
1404 work function will not do anything.) Also, any other
1405 possible concurrent operations will fail due to the
1406 current state, hence the wl1271 struct should be safe. */
1407 wlcore_disable_interrupts(wl);
1408 wl1271_flush_deferred_work(wl);
1409 cancel_work_sync(&wl->netstack_work);
1410 mutex_lock(&wl->mutex);
1411 power_off:
1412 wl1271_power_off(wl);
1413 }
1414
1415 wl1271_error("firmware boot in PLT mode failed despite %d retries",
1416 WL1271_BOOT_RETRIES);
1417 out:
1418 mutex_unlock(&wl->mutex);
1419
1420 return ret;
1421 }
1422
1423 int wl1271_plt_stop(struct wl1271 *wl)
1424 {
1425 int ret = 0;
1426
1427 wl1271_notice("power down");
1428
1429 /*
1430 * Interrupts must be disabled before setting the state to OFF.
1431 * Otherwise, the interrupt handler might be called and exit without
1432 * reading the interrupt status.
1433 */
1434 wlcore_disable_interrupts(wl);
1435 mutex_lock(&wl->mutex);
1436 if (!wl->plt) {
1437 mutex_unlock(&wl->mutex);
1438
1439 /*
1440 * This will not necessarily enable interrupts as interrupts
1441 * may have been disabled when op_stop was called. It will,
1442 * however, balance the above call to disable_interrupts().
1443 */
1444 wlcore_enable_interrupts(wl);
1445
1446 wl1271_error("cannot power down because not in PLT "
1447 "state: %d", wl->state);
1448 ret = -EBUSY;
1449 goto out;
1450 }
1451
1452 mutex_unlock(&wl->mutex);
1453
1454 wl1271_flush_deferred_work(wl);
1455 cancel_work_sync(&wl->netstack_work);
1456 cancel_work_sync(&wl->recovery_work);
1457 cancel_delayed_work_sync(&wl->elp_work);
1458 cancel_delayed_work_sync(&wl->tx_watchdog_work);
1459
1460 mutex_lock(&wl->mutex);
1461 wl1271_power_off(wl);
1462 wl->flags = 0;
1463 wl->state = WL1271_STATE_OFF;
1464 wl->plt = false;
1465 wl->rx_counter = 0;
1466 mutex_unlock(&wl->mutex);
1467
1468 out:
1469 return ret;
1470 }
1471
1472 static void wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1473 {
1474 struct wl1271 *wl = hw->priv;
1475 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1476 struct ieee80211_vif *vif = info->control.vif;
1477 struct wl12xx_vif *wlvif = NULL;
1478 unsigned long flags;
1479 int q, mapping;
1480 u8 hlid;
1481
1482 if (vif)
1483 wlvif = wl12xx_vif_to_data(vif);
1484
1485 mapping = skb_get_queue_mapping(skb);
1486 q = wl1271_tx_get_queue(mapping);
1487
1488 hlid = wl12xx_tx_get_hlid(wl, wlvif, skb);
1489
1490 spin_lock_irqsave(&wl->wl_lock, flags);
1491
1492 /* queue the packet */
1493 if (hlid == WL12XX_INVALID_LINK_ID ||
1494 (wlvif && !test_bit(hlid, wlvif->links_map))) {
1495 wl1271_debug(DEBUG_TX, "DROP skb hlid %d q %d", hlid, q);
1496 ieee80211_free_txskb(hw, skb);
1497 goto out;
1498 }
1499
1500 wl1271_debug(DEBUG_TX, "queue skb hlid %d q %d len %d",
1501 hlid, q, skb->len);
1502 skb_queue_tail(&wl->links[hlid].tx_queue[q], skb);
1503
1504 wl->tx_queue_count[q]++;
1505
1506 /*
1507 * The workqueue is slow to process the tx_queue and we need stop
1508 * the queue here, otherwise the queue will get too long.
1509 */
1510 if (wl->tx_queue_count[q] >= WL1271_TX_QUEUE_HIGH_WATERMARK) {
1511 wl1271_debug(DEBUG_TX, "op_tx: stopping queues for q %d", q);
1512 ieee80211_stop_queue(wl->hw, mapping);
1513 set_bit(q, &wl->stopped_queues_map);
1514 }
1515
1516 /*
1517 * The chip specific setup must run before the first TX packet -
1518 * before that, the tx_work will not be initialized!
1519 */
1520
1521 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
1522 !test_bit(WL1271_FLAG_TX_PENDING, &wl->flags))
1523 ieee80211_queue_work(wl->hw, &wl->tx_work);
1524
1525 out:
1526 spin_unlock_irqrestore(&wl->wl_lock, flags);
1527 }
1528
1529 int wl1271_tx_dummy_packet(struct wl1271 *wl)
1530 {
1531 unsigned long flags;
1532 int q;
1533
1534 /* no need to queue a new dummy packet if one is already pending */
1535 if (test_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags))
1536 return 0;
1537
1538 q = wl1271_tx_get_queue(skb_get_queue_mapping(wl->dummy_packet));
1539
1540 spin_lock_irqsave(&wl->wl_lock, flags);
1541 set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
1542 wl->tx_queue_count[q]++;
1543 spin_unlock_irqrestore(&wl->wl_lock, flags);
1544
1545 /* The FW is low on RX memory blocks, so send the dummy packet asap */
1546 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags))
1547 wl1271_tx_work_locked(wl);
1548
1549 /*
1550 * If the FW TX is busy, TX work will be scheduled by the threaded
1551 * interrupt handler function
1552 */
1553 return 0;
1554 }
1555
1556 /*
1557 * The size of the dummy packet should be at least 1400 bytes. However, in
1558 * order to minimize the number of bus transactions, aligning it to 512 bytes
1559 * boundaries could be beneficial, performance wise
1560 */
1561 #define TOTAL_TX_DUMMY_PACKET_SIZE (ALIGN(1400, 512))
1562
1563 static struct sk_buff *wl12xx_alloc_dummy_packet(struct wl1271 *wl)
1564 {
1565 struct sk_buff *skb;
1566 struct ieee80211_hdr_3addr *hdr;
1567 unsigned int dummy_packet_size;
1568
1569 dummy_packet_size = TOTAL_TX_DUMMY_PACKET_SIZE -
1570 sizeof(struct wl1271_tx_hw_descr) - sizeof(*hdr);
1571
1572 skb = dev_alloc_skb(TOTAL_TX_DUMMY_PACKET_SIZE);
1573 if (!skb) {
1574 wl1271_warning("Failed to allocate a dummy packet skb");
1575 return NULL;
1576 }
1577
1578 skb_reserve(skb, sizeof(struct wl1271_tx_hw_descr));
1579
1580 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
1581 memset(hdr, 0, sizeof(*hdr));
1582 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1583 IEEE80211_STYPE_NULLFUNC |
1584 IEEE80211_FCTL_TODS);
1585
1586 memset(skb_put(skb, dummy_packet_size), 0, dummy_packet_size);
1587
1588 /* Dummy packets require the TID to be management */
1589 skb->priority = WL1271_TID_MGMT;
1590
1591 /* Initialize all fields that might be used */
1592 skb_set_queue_mapping(skb, 0);
1593 memset(IEEE80211_SKB_CB(skb), 0, sizeof(struct ieee80211_tx_info));
1594
1595 return skb;
1596 }
1597
1598
1599 #ifdef CONFIG_PM
1600 static int wl1271_configure_suspend_sta(struct wl1271 *wl,
1601 struct wl12xx_vif *wlvif)
1602 {
1603 int ret = 0;
1604
1605 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
1606 goto out;
1607
1608 ret = wl1271_ps_elp_wakeup(wl);
1609 if (ret < 0)
1610 goto out;
1611
1612 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1613 wl->conf.conn.suspend_wake_up_event,
1614 wl->conf.conn.suspend_listen_interval);
1615
1616 if (ret < 0)
1617 wl1271_error("suspend: set wake up conditions failed: %d", ret);
1618
1619 wl1271_ps_elp_sleep(wl);
1620
1621 out:
1622 return ret;
1623
1624 }
1625
1626 static int wl1271_configure_suspend_ap(struct wl1271 *wl,
1627 struct wl12xx_vif *wlvif)
1628 {
1629 int ret = 0;
1630
1631 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
1632 goto out;
1633
1634 ret = wl1271_ps_elp_wakeup(wl);
1635 if (ret < 0)
1636 goto out;
1637
1638 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
1639
1640 wl1271_ps_elp_sleep(wl);
1641 out:
1642 return ret;
1643
1644 }
1645
1646 static int wl1271_configure_suspend(struct wl1271 *wl,
1647 struct wl12xx_vif *wlvif)
1648 {
1649 if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1650 return wl1271_configure_suspend_sta(wl, wlvif);
1651 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
1652 return wl1271_configure_suspend_ap(wl, wlvif);
1653 return 0;
1654 }
1655
1656 static void wl1271_configure_resume(struct wl1271 *wl,
1657 struct wl12xx_vif *wlvif)
1658 {
1659 int ret = 0;
1660 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
1661 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
1662
1663 if ((!is_ap) && (!is_sta))
1664 return;
1665
1666 ret = wl1271_ps_elp_wakeup(wl);
1667 if (ret < 0)
1668 return;
1669
1670 if (is_sta) {
1671 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1672 wl->conf.conn.wake_up_event,
1673 wl->conf.conn.listen_interval);
1674
1675 if (ret < 0)
1676 wl1271_error("resume: wake up conditions failed: %d",
1677 ret);
1678
1679 } else if (is_ap) {
1680 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
1681 }
1682
1683 wl1271_ps_elp_sleep(wl);
1684 }
1685
1686 static int wl1271_op_suspend(struct ieee80211_hw *hw,
1687 struct cfg80211_wowlan *wow)
1688 {
1689 struct wl1271 *wl = hw->priv;
1690 struct wl12xx_vif *wlvif;
1691 int ret;
1692
1693 wl1271_debug(DEBUG_MAC80211, "mac80211 suspend wow=%d", !!wow);
1694 WARN_ON(!wow || !wow->any);
1695
1696 wl1271_tx_flush(wl);
1697
1698 mutex_lock(&wl->mutex);
1699 wl->wow_enabled = true;
1700 wl12xx_for_each_wlvif(wl, wlvif) {
1701 ret = wl1271_configure_suspend(wl, wlvif);
1702 if (ret < 0) {
1703 wl1271_warning("couldn't prepare device to suspend");
1704 return ret;
1705 }
1706 }
1707 mutex_unlock(&wl->mutex);
1708 /* flush any remaining work */
1709 wl1271_debug(DEBUG_MAC80211, "flushing remaining works");
1710
1711 /*
1712 * disable and re-enable interrupts in order to flush
1713 * the threaded_irq
1714 */
1715 wlcore_disable_interrupts(wl);
1716
1717 /*
1718 * set suspended flag to avoid triggering a new threaded_irq
1719 * work. no need for spinlock as interrupts are disabled.
1720 */
1721 set_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1722
1723 wlcore_enable_interrupts(wl);
1724 flush_work(&wl->tx_work);
1725 flush_delayed_work(&wl->elp_work);
1726
1727 return 0;
1728 }
1729
1730 static int wl1271_op_resume(struct ieee80211_hw *hw)
1731 {
1732 struct wl1271 *wl = hw->priv;
1733 struct wl12xx_vif *wlvif;
1734 unsigned long flags;
1735 bool run_irq_work = false;
1736
1737 wl1271_debug(DEBUG_MAC80211, "mac80211 resume wow=%d",
1738 wl->wow_enabled);
1739 WARN_ON(!wl->wow_enabled);
1740
1741 /*
1742 * re-enable irq_work enqueuing, and call irq_work directly if
1743 * there is a pending work.
1744 */
1745 spin_lock_irqsave(&wl->wl_lock, flags);
1746 clear_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1747 if (test_and_clear_bit(WL1271_FLAG_PENDING_WORK, &wl->flags))
1748 run_irq_work = true;
1749 spin_unlock_irqrestore(&wl->wl_lock, flags);
1750
1751 if (run_irq_work) {
1752 wl1271_debug(DEBUG_MAC80211,
1753 "run postponed irq_work directly");
1754 wl1271_irq(0, wl);
1755 wlcore_enable_interrupts(wl);
1756 }
1757
1758 mutex_lock(&wl->mutex);
1759 wl12xx_for_each_wlvif(wl, wlvif) {
1760 wl1271_configure_resume(wl, wlvif);
1761 }
1762 wl->wow_enabled = false;
1763 mutex_unlock(&wl->mutex);
1764
1765 return 0;
1766 }
1767 #endif
1768
1769 static int wl1271_op_start(struct ieee80211_hw *hw)
1770 {
1771 wl1271_debug(DEBUG_MAC80211, "mac80211 start");
1772
1773 /*
1774 * We have to delay the booting of the hardware because
1775 * we need to know the local MAC address before downloading and
1776 * initializing the firmware. The MAC address cannot be changed
1777 * after boot, and without the proper MAC address, the firmware
1778 * will not function properly.
1779 *
1780 * The MAC address is first known when the corresponding interface
1781 * is added. That is where we will initialize the hardware.
1782 */
1783
1784 return 0;
1785 }
1786
1787 static void wl1271_op_stop(struct ieee80211_hw *hw)
1788 {
1789 struct wl1271 *wl = hw->priv;
1790 int i;
1791
1792 wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
1793
1794 /*
1795 * Interrupts must be disabled before setting the state to OFF.
1796 * Otherwise, the interrupt handler might be called and exit without
1797 * reading the interrupt status.
1798 */
1799 wlcore_disable_interrupts(wl);
1800 mutex_lock(&wl->mutex);
1801 if (wl->state == WL1271_STATE_OFF) {
1802 mutex_unlock(&wl->mutex);
1803
1804 /*
1805 * This will not necessarily enable interrupts as interrupts
1806 * may have been disabled when op_stop was called. It will,
1807 * however, balance the above call to disable_interrupts().
1808 */
1809 wlcore_enable_interrupts(wl);
1810 return;
1811 }
1812
1813 /*
1814 * this must be before the cancel_work calls below, so that the work
1815 * functions don't perform further work.
1816 */
1817 wl->state = WL1271_STATE_OFF;
1818 mutex_unlock(&wl->mutex);
1819
1820 wl1271_flush_deferred_work(wl);
1821 cancel_delayed_work_sync(&wl->scan_complete_work);
1822 cancel_work_sync(&wl->netstack_work);
1823 cancel_work_sync(&wl->tx_work);
1824 cancel_delayed_work_sync(&wl->elp_work);
1825 cancel_delayed_work_sync(&wl->tx_watchdog_work);
1826
1827 /* let's notify MAC80211 about the remaining pending TX frames */
1828 wl12xx_tx_reset(wl, true);
1829 mutex_lock(&wl->mutex);
1830
1831 wl1271_power_off(wl);
1832
1833 wl->band = IEEE80211_BAND_2GHZ;
1834
1835 wl->rx_counter = 0;
1836 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1837 wl->tx_blocks_available = 0;
1838 wl->tx_allocated_blocks = 0;
1839 wl->tx_results_count = 0;
1840 wl->tx_packets_count = 0;
1841 wl->time_offset = 0;
1842 wl->ap_fw_ps_map = 0;
1843 wl->ap_ps_map = 0;
1844 wl->sched_scanning = false;
1845 memset(wl->roles_map, 0, sizeof(wl->roles_map));
1846 memset(wl->links_map, 0, sizeof(wl->links_map));
1847 memset(wl->roc_map, 0, sizeof(wl->roc_map));
1848 wl->active_sta_count = 0;
1849
1850 /* The system link is always allocated */
1851 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
1852
1853 /*
1854 * this is performed after the cancel_work calls and the associated
1855 * mutex_lock, so that wl1271_op_add_interface does not accidentally
1856 * get executed before all these vars have been reset.
1857 */
1858 wl->flags = 0;
1859
1860 wl->tx_blocks_freed = 0;
1861
1862 for (i = 0; i < NUM_TX_QUEUES; i++) {
1863 wl->tx_pkts_freed[i] = 0;
1864 wl->tx_allocated_pkts[i] = 0;
1865 }
1866
1867 wl1271_debugfs_reset(wl);
1868
1869 kfree(wl->fw_status);
1870 wl->fw_status = NULL;
1871 kfree(wl->tx_res_if);
1872 wl->tx_res_if = NULL;
1873 kfree(wl->target_mem_map);
1874 wl->target_mem_map = NULL;
1875
1876 mutex_unlock(&wl->mutex);
1877 }
1878
1879 static int wl12xx_allocate_rate_policy(struct wl1271 *wl, u8 *idx)
1880 {
1881 u8 policy = find_first_zero_bit(wl->rate_policies_map,
1882 WL12XX_MAX_RATE_POLICIES);
1883 if (policy >= WL12XX_MAX_RATE_POLICIES)
1884 return -EBUSY;
1885
1886 __set_bit(policy, wl->rate_policies_map);
1887 *idx = policy;
1888 return 0;
1889 }
1890
1891 static void wl12xx_free_rate_policy(struct wl1271 *wl, u8 *idx)
1892 {
1893 if (WARN_ON(*idx >= WL12XX_MAX_RATE_POLICIES))
1894 return;
1895
1896 __clear_bit(*idx, wl->rate_policies_map);
1897 *idx = WL12XX_MAX_RATE_POLICIES;
1898 }
1899
1900 static u8 wl12xx_get_role_type(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1901 {
1902 switch (wlvif->bss_type) {
1903 case BSS_TYPE_AP_BSS:
1904 if (wlvif->p2p)
1905 return WL1271_ROLE_P2P_GO;
1906 else
1907 return WL1271_ROLE_AP;
1908
1909 case BSS_TYPE_STA_BSS:
1910 if (wlvif->p2p)
1911 return WL1271_ROLE_P2P_CL;
1912 else
1913 return WL1271_ROLE_STA;
1914
1915 case BSS_TYPE_IBSS:
1916 return WL1271_ROLE_IBSS;
1917
1918 default:
1919 wl1271_error("invalid bss_type: %d", wlvif->bss_type);
1920 }
1921 return WL12XX_INVALID_ROLE_TYPE;
1922 }
1923
1924 static int wl12xx_init_vif_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1925 {
1926 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1927 int i;
1928
1929 /* clear everything but the persistent data */
1930 memset(wlvif, 0, offsetof(struct wl12xx_vif, persistent));
1931
1932 switch (ieee80211_vif_type_p2p(vif)) {
1933 case NL80211_IFTYPE_P2P_CLIENT:
1934 wlvif->p2p = 1;
1935 /* fall-through */
1936 case NL80211_IFTYPE_STATION:
1937 wlvif->bss_type = BSS_TYPE_STA_BSS;
1938 break;
1939 case NL80211_IFTYPE_ADHOC:
1940 wlvif->bss_type = BSS_TYPE_IBSS;
1941 break;
1942 case NL80211_IFTYPE_P2P_GO:
1943 wlvif->p2p = 1;
1944 /* fall-through */
1945 case NL80211_IFTYPE_AP:
1946 wlvif->bss_type = BSS_TYPE_AP_BSS;
1947 break;
1948 default:
1949 wlvif->bss_type = MAX_BSS_TYPE;
1950 return -EOPNOTSUPP;
1951 }
1952
1953 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
1954 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
1955 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
1956
1957 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
1958 wlvif->bss_type == BSS_TYPE_IBSS) {
1959 /* init sta/ibss data */
1960 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
1961 wl12xx_allocate_rate_policy(wl, &wlvif->sta.basic_rate_idx);
1962 wl12xx_allocate_rate_policy(wl, &wlvif->sta.ap_rate_idx);
1963 wl12xx_allocate_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
1964 } else {
1965 /* init ap data */
1966 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
1967 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
1968 wl12xx_allocate_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
1969 wl12xx_allocate_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
1970 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
1971 wl12xx_allocate_rate_policy(wl,
1972 &wlvif->ap.ucast_rate_idx[i]);
1973 }
1974
1975 wlvif->bitrate_masks[IEEE80211_BAND_2GHZ] = wl->conf.tx.basic_rate;
1976 wlvif->bitrate_masks[IEEE80211_BAND_5GHZ] = wl->conf.tx.basic_rate_5;
1977 wlvif->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
1978 wlvif->basic_rate = CONF_TX_RATE_MASK_BASIC;
1979 wlvif->rate_set = CONF_TX_RATE_MASK_BASIC;
1980 wlvif->beacon_int = WL1271_DEFAULT_BEACON_INT;
1981
1982 /*
1983 * mac80211 configures some values globally, while we treat them
1984 * per-interface. thus, on init, we have to copy them from wl
1985 */
1986 wlvif->band = wl->band;
1987 wlvif->channel = wl->channel;
1988 wlvif->power_level = wl->power_level;
1989
1990 INIT_WORK(&wlvif->rx_streaming_enable_work,
1991 wl1271_rx_streaming_enable_work);
1992 INIT_WORK(&wlvif->rx_streaming_disable_work,
1993 wl1271_rx_streaming_disable_work);
1994 INIT_LIST_HEAD(&wlvif->list);
1995
1996 setup_timer(&wlvif->rx_streaming_timer, wl1271_rx_streaming_timer,
1997 (unsigned long) wlvif);
1998 return 0;
1999 }
2000
2001 static bool wl12xx_init_fw(struct wl1271 *wl)
2002 {
2003 int retries = WL1271_BOOT_RETRIES;
2004 bool booted = false;
2005 struct wiphy *wiphy = wl->hw->wiphy;
2006 int ret;
2007
2008 while (retries) {
2009 retries--;
2010 ret = wl12xx_chip_wakeup(wl, false);
2011 if (ret < 0)
2012 goto power_off;
2013
2014 ret = wl->ops->boot(wl);
2015 if (ret < 0)
2016 goto power_off;
2017
2018 ret = wl1271_hw_init(wl);
2019 if (ret < 0)
2020 goto irq_disable;
2021
2022 booted = true;
2023 break;
2024
2025 irq_disable:
2026 mutex_unlock(&wl->mutex);
2027 /* Unlocking the mutex in the middle of handling is
2028 inherently unsafe. In this case we deem it safe to do,
2029 because we need to let any possibly pending IRQ out of
2030 the system (and while we are WL1271_STATE_OFF the IRQ
2031 work function will not do anything.) Also, any other
2032 possible concurrent operations will fail due to the
2033 current state, hence the wl1271 struct should be safe. */
2034 wlcore_disable_interrupts(wl);
2035 wl1271_flush_deferred_work(wl);
2036 cancel_work_sync(&wl->netstack_work);
2037 mutex_lock(&wl->mutex);
2038 power_off:
2039 wl1271_power_off(wl);
2040 }
2041
2042 if (!booted) {
2043 wl1271_error("firmware boot failed despite %d retries",
2044 WL1271_BOOT_RETRIES);
2045 goto out;
2046 }
2047
2048 wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
2049
2050 /* update hw/fw version info in wiphy struct */
2051 wiphy->hw_version = wl->chip.id;
2052 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
2053 sizeof(wiphy->fw_version));
2054
2055 /*
2056 * Now we know if 11a is supported (info from the NVS), so disable
2057 * 11a channels if not supported
2058 */
2059 if (!wl->enable_11a)
2060 wiphy->bands[IEEE80211_BAND_5GHZ]->n_channels = 0;
2061
2062 wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
2063 wl->enable_11a ? "" : "not ");
2064
2065 wl->state = WL1271_STATE_ON;
2066 out:
2067 return booted;
2068 }
2069
2070 static bool wl12xx_dev_role_started(struct wl12xx_vif *wlvif)
2071 {
2072 return wlvif->dev_hlid != WL12XX_INVALID_LINK_ID;
2073 }
2074
2075 /*
2076 * Check whether a fw switch (i.e. moving from one loaded
2077 * fw to another) is needed. This function is also responsible
2078 * for updating wl->last_vif_count, so it must be called before
2079 * loading a non-plt fw (so the correct fw (single-role/multi-role)
2080 * will be used).
2081 */
2082 static bool wl12xx_need_fw_change(struct wl1271 *wl,
2083 struct vif_counter_data vif_counter_data,
2084 bool add)
2085 {
2086 enum wl12xx_fw_type current_fw = wl->fw_type;
2087 u8 vif_count = vif_counter_data.counter;
2088
2089 if (test_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags))
2090 return false;
2091
2092 /* increase the vif count if this is a new vif */
2093 if (add && !vif_counter_data.cur_vif_running)
2094 vif_count++;
2095
2096 wl->last_vif_count = vif_count;
2097
2098 /* no need for fw change if the device is OFF */
2099 if (wl->state == WL1271_STATE_OFF)
2100 return false;
2101
2102 if (vif_count > 1 && current_fw == WL12XX_FW_TYPE_NORMAL)
2103 return true;
2104 if (vif_count <= 1 && current_fw == WL12XX_FW_TYPE_MULTI)
2105 return true;
2106
2107 return false;
2108 }
2109
2110 /*
2111 * Enter "forced psm". Make sure the sta is in psm against the ap,
2112 * to make the fw switch a bit more disconnection-persistent.
2113 */
2114 static void wl12xx_force_active_psm(struct wl1271 *wl)
2115 {
2116 struct wl12xx_vif *wlvif;
2117
2118 wl12xx_for_each_wlvif_sta(wl, wlvif) {
2119 wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
2120 }
2121 }
2122
2123 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
2124 struct ieee80211_vif *vif)
2125 {
2126 struct wl1271 *wl = hw->priv;
2127 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2128 struct vif_counter_data vif_count;
2129 int ret = 0;
2130 u8 role_type;
2131 bool booted = false;
2132
2133 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
2134 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
2135
2136 wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
2137 ieee80211_vif_type_p2p(vif), vif->addr);
2138
2139 wl12xx_get_vif_count(hw, vif, &vif_count);
2140
2141 mutex_lock(&wl->mutex);
2142 ret = wl1271_ps_elp_wakeup(wl);
2143 if (ret < 0)
2144 goto out_unlock;
2145
2146 /*
2147 * in some very corner case HW recovery scenarios its possible to
2148 * get here before __wl1271_op_remove_interface is complete, so
2149 * opt out if that is the case.
2150 */
2151 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) ||
2152 test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)) {
2153 ret = -EBUSY;
2154 goto out;
2155 }
2156
2157
2158 ret = wl12xx_init_vif_data(wl, vif);
2159 if (ret < 0)
2160 goto out;
2161
2162 wlvif->wl = wl;
2163 role_type = wl12xx_get_role_type(wl, wlvif);
2164 if (role_type == WL12XX_INVALID_ROLE_TYPE) {
2165 ret = -EINVAL;
2166 goto out;
2167 }
2168
2169 if (wl12xx_need_fw_change(wl, vif_count, true)) {
2170 wl12xx_force_active_psm(wl);
2171 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2172 mutex_unlock(&wl->mutex);
2173 wl1271_recovery_work(&wl->recovery_work);
2174 return 0;
2175 }
2176
2177 /*
2178 * TODO: after the nvs issue will be solved, move this block
2179 * to start(), and make sure here the driver is ON.
2180 */
2181 if (wl->state == WL1271_STATE_OFF) {
2182 /*
2183 * we still need this in order to configure the fw
2184 * while uploading the nvs
2185 */
2186 memcpy(wl->addresses[0].addr, vif->addr, ETH_ALEN);
2187
2188 booted = wl12xx_init_fw(wl);
2189 if (!booted) {
2190 ret = -EINVAL;
2191 goto out;
2192 }
2193 }
2194
2195 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2196 wlvif->bss_type == BSS_TYPE_IBSS) {
2197 /*
2198 * The device role is a special role used for
2199 * rx and tx frames prior to association (as
2200 * the STA role can get packets only from
2201 * its associated bssid)
2202 */
2203 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2204 WL1271_ROLE_DEVICE,
2205 &wlvif->dev_role_id);
2206 if (ret < 0)
2207 goto out;
2208 }
2209
2210 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2211 role_type, &wlvif->role_id);
2212 if (ret < 0)
2213 goto out;
2214
2215 ret = wl1271_init_vif_specific(wl, vif);
2216 if (ret < 0)
2217 goto out;
2218
2219 list_add(&wlvif->list, &wl->wlvif_list);
2220 set_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags);
2221
2222 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2223 wl->ap_count++;
2224 else
2225 wl->sta_count++;
2226 out:
2227 wl1271_ps_elp_sleep(wl);
2228 out_unlock:
2229 mutex_unlock(&wl->mutex);
2230
2231 return ret;
2232 }
2233
2234 static void __wl1271_op_remove_interface(struct wl1271 *wl,
2235 struct ieee80211_vif *vif,
2236 bool reset_tx_queues)
2237 {
2238 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2239 int i, ret;
2240
2241 wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
2242
2243 if (!test_and_clear_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2244 return;
2245
2246 /* because of hardware recovery, we may get here twice */
2247 if (wl->state != WL1271_STATE_ON)
2248 return;
2249
2250 wl1271_info("down");
2251
2252 if (wl->scan.state != WL1271_SCAN_STATE_IDLE &&
2253 wl->scan_vif == vif) {
2254 /*
2255 * Rearm the tx watchdog just before idling scan. This
2256 * prevents just-finished scans from triggering the watchdog
2257 */
2258 wl12xx_rearm_tx_watchdog_locked(wl);
2259
2260 wl->scan.state = WL1271_SCAN_STATE_IDLE;
2261 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
2262 wl->scan_vif = NULL;
2263 wl->scan.req = NULL;
2264 ieee80211_scan_completed(wl->hw, true);
2265 }
2266
2267 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2268 /* disable active roles */
2269 ret = wl1271_ps_elp_wakeup(wl);
2270 if (ret < 0)
2271 goto deinit;
2272
2273 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2274 wlvif->bss_type == BSS_TYPE_IBSS) {
2275 if (wl12xx_dev_role_started(wlvif))
2276 wl12xx_stop_dev(wl, wlvif);
2277
2278 ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2279 if (ret < 0)
2280 goto deinit;
2281 }
2282
2283 ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
2284 if (ret < 0)
2285 goto deinit;
2286
2287 wl1271_ps_elp_sleep(wl);
2288 }
2289 deinit:
2290 /* clear all hlids (except system_hlid) */
2291 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2292
2293 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2294 wlvif->bss_type == BSS_TYPE_IBSS) {
2295 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2296 wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2297 wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2298 wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2299 } else {
2300 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2301 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2302 wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2303 wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2304 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2305 wl12xx_free_rate_policy(wl,
2306 &wlvif->ap.ucast_rate_idx[i]);
2307 wl1271_free_ap_keys(wl, wlvif);
2308 }
2309
2310 dev_kfree_skb(wlvif->probereq);
2311 wlvif->probereq = NULL;
2312 wl12xx_tx_reset_wlvif(wl, wlvif);
2313 if (wl->last_wlvif == wlvif)
2314 wl->last_wlvif = NULL;
2315 list_del(&wlvif->list);
2316 memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
2317 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2318 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2319
2320 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2321 wl->ap_count--;
2322 else
2323 wl->sta_count--;
2324
2325 mutex_unlock(&wl->mutex);
2326
2327 del_timer_sync(&wlvif->rx_streaming_timer);
2328 cancel_work_sync(&wlvif->rx_streaming_enable_work);
2329 cancel_work_sync(&wlvif->rx_streaming_disable_work);
2330
2331 mutex_lock(&wl->mutex);
2332 }
2333
2334 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2335 struct ieee80211_vif *vif)
2336 {
2337 struct wl1271 *wl = hw->priv;
2338 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2339 struct wl12xx_vif *iter;
2340 struct vif_counter_data vif_count;
2341 bool cancel_recovery = true;
2342
2343 wl12xx_get_vif_count(hw, vif, &vif_count);
2344 mutex_lock(&wl->mutex);
2345
2346 if (wl->state == WL1271_STATE_OFF ||
2347 !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2348 goto out;
2349
2350 /*
2351 * wl->vif can be null here if someone shuts down the interface
2352 * just when hardware recovery has been started.
2353 */
2354 wl12xx_for_each_wlvif(wl, iter) {
2355 if (iter != wlvif)
2356 continue;
2357
2358 __wl1271_op_remove_interface(wl, vif, true);
2359 break;
2360 }
2361 WARN_ON(iter != wlvif);
2362 if (wl12xx_need_fw_change(wl, vif_count, false)) {
2363 wl12xx_force_active_psm(wl);
2364 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2365 wl12xx_queue_recovery_work(wl);
2366 cancel_recovery = false;
2367 }
2368 out:
2369 mutex_unlock(&wl->mutex);
2370 if (cancel_recovery)
2371 cancel_work_sync(&wl->recovery_work);
2372 }
2373
2374 static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2375 struct ieee80211_vif *vif,
2376 enum nl80211_iftype new_type, bool p2p)
2377 {
2378 struct wl1271 *wl = hw->priv;
2379 int ret;
2380
2381 set_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2382 wl1271_op_remove_interface(hw, vif);
2383
2384 vif->type = new_type;
2385 vif->p2p = p2p;
2386 ret = wl1271_op_add_interface(hw, vif);
2387
2388 clear_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2389 return ret;
2390 }
2391
2392 static int wl1271_join(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2393 bool set_assoc)
2394 {
2395 int ret;
2396 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
2397
2398 /*
2399 * One of the side effects of the JOIN command is that is clears
2400 * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2401 * to a WPA/WPA2 access point will therefore kill the data-path.
2402 * Currently the only valid scenario for JOIN during association
2403 * is on roaming, in which case we will also be given new keys.
2404 * Keep the below message for now, unless it starts bothering
2405 * users who really like to roam a lot :)
2406 */
2407 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2408 wl1271_info("JOIN while associated.");
2409
2410 /* clear encryption type */
2411 wlvif->encryption_type = KEY_NONE;
2412
2413 if (set_assoc)
2414 set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2415
2416 if (is_ibss)
2417 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
2418 else
2419 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
2420 if (ret < 0)
2421 goto out;
2422
2423 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2424 goto out;
2425
2426 /*
2427 * The join command disable the keep-alive mode, shut down its process,
2428 * and also clear the template config, so we need to reset it all after
2429 * the join. The acx_aid starts the keep-alive process, and the order
2430 * of the commands below is relevant.
2431 */
2432 ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
2433 if (ret < 0)
2434 goto out;
2435
2436 ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
2437 if (ret < 0)
2438 goto out;
2439
2440 ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
2441 if (ret < 0)
2442 goto out;
2443
2444 ret = wl1271_acx_keep_alive_config(wl, wlvif,
2445 CMD_TEMPL_KLV_IDX_NULL_DATA,
2446 ACX_KEEP_ALIVE_TPL_VALID);
2447 if (ret < 0)
2448 goto out;
2449
2450 out:
2451 return ret;
2452 }
2453
2454 static int wl1271_unjoin(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2455 {
2456 int ret;
2457
2458 if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
2459 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2460
2461 wl12xx_cmd_stop_channel_switch(wl);
2462 ieee80211_chswitch_done(vif, false);
2463 }
2464
2465 /* to stop listening to a channel, we disconnect */
2466 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
2467 if (ret < 0)
2468 goto out;
2469
2470 /* reset TX security counters on a clean disconnect */
2471 wlvif->tx_security_last_seq_lsb = 0;
2472 wlvif->tx_security_seq = 0;
2473
2474 out:
2475 return ret;
2476 }
2477
2478 static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2479 {
2480 wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
2481 wlvif->rate_set = wlvif->basic_rate_set;
2482 }
2483
2484 static int wl1271_sta_handle_idle(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2485 bool idle)
2486 {
2487 int ret;
2488 bool cur_idle = !test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2489
2490 if (idle == cur_idle)
2491 return 0;
2492
2493 if (idle) {
2494 /* no need to croc if we weren't busy (e.g. during boot) */
2495 if (wl12xx_dev_role_started(wlvif)) {
2496 ret = wl12xx_stop_dev(wl, wlvif);
2497 if (ret < 0)
2498 goto out;
2499 }
2500 wlvif->rate_set =
2501 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
2502 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2503 if (ret < 0)
2504 goto out;
2505 ret = wl1271_acx_keep_alive_config(
2506 wl, wlvif, CMD_TEMPL_KLV_IDX_NULL_DATA,
2507 ACX_KEEP_ALIVE_TPL_INVALID);
2508 if (ret < 0)
2509 goto out;
2510 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2511 } else {
2512 /* The current firmware only supports sched_scan in idle */
2513 if (wl->sched_scanning) {
2514 wl1271_scan_sched_scan_stop(wl);
2515 ieee80211_sched_scan_stopped(wl->hw);
2516 }
2517
2518 ret = wl12xx_start_dev(wl, wlvif);
2519 if (ret < 0)
2520 goto out;
2521 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2522 }
2523
2524 out:
2525 return ret;
2526 }
2527
2528 static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2529 struct ieee80211_conf *conf, u32 changed)
2530 {
2531 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2532 int channel, ret;
2533
2534 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2535
2536 /* if the channel changes while joined, join again */
2537 if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
2538 ((wlvif->band != conf->channel->band) ||
2539 (wlvif->channel != channel))) {
2540 /* send all pending packets */
2541 wl1271_tx_work_locked(wl);
2542 wlvif->band = conf->channel->band;
2543 wlvif->channel = channel;
2544
2545 if (!is_ap) {
2546 /*
2547 * FIXME: the mac80211 should really provide a fixed
2548 * rate to use here. for now, just use the smallest
2549 * possible rate for the band as a fixed rate for
2550 * association frames and other control messages.
2551 */
2552 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2553 wl1271_set_band_rate(wl, wlvif);
2554
2555 wlvif->basic_rate =
2556 wl1271_tx_min_rate_get(wl,
2557 wlvif->basic_rate_set);
2558 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2559 if (ret < 0)
2560 wl1271_warning("rate policy for channel "
2561 "failed %d", ret);
2562
2563 /*
2564 * change the ROC channel. do it only if we are
2565 * not idle. otherwise, CROC will be called
2566 * anyway.
2567 */
2568 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED,
2569 &wlvif->flags) &&
2570 wl12xx_dev_role_started(wlvif) &&
2571 !(conf->flags & IEEE80211_CONF_IDLE)) {
2572 ret = wl12xx_stop_dev(wl, wlvif);
2573 if (ret < 0)
2574 return ret;
2575
2576 ret = wl12xx_start_dev(wl, wlvif);
2577 if (ret < 0)
2578 return ret;
2579 }
2580 }
2581 }
2582
2583 if ((changed & IEEE80211_CONF_CHANGE_PS) && !is_ap) {
2584
2585 if ((conf->flags & IEEE80211_CONF_PS) &&
2586 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
2587 !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
2588
2589 int ps_mode;
2590 char *ps_mode_str;
2591
2592 if (wl->conf.conn.forced_ps) {
2593 ps_mode = STATION_POWER_SAVE_MODE;
2594 ps_mode_str = "forced";
2595 } else {
2596 ps_mode = STATION_AUTO_PS_MODE;
2597 ps_mode_str = "auto";
2598 }
2599
2600 wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
2601
2602 ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
2603
2604 if (ret < 0)
2605 wl1271_warning("enter %s ps failed %d",
2606 ps_mode_str, ret);
2607
2608 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
2609 test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
2610
2611 wl1271_debug(DEBUG_PSM, "auto ps disabled");
2612
2613 ret = wl1271_ps_set_mode(wl, wlvif,
2614 STATION_ACTIVE_MODE);
2615 if (ret < 0)
2616 wl1271_warning("exit auto ps failed %d", ret);
2617 }
2618 }
2619
2620 if (conf->power_level != wlvif->power_level) {
2621 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
2622 if (ret < 0)
2623 return ret;
2624
2625 wlvif->power_level = conf->power_level;
2626 }
2627
2628 return 0;
2629 }
2630
2631 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
2632 {
2633 struct wl1271 *wl = hw->priv;
2634 struct wl12xx_vif *wlvif;
2635 struct ieee80211_conf *conf = &hw->conf;
2636 int channel, ret = 0;
2637
2638 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2639
2640 wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d %s"
2641 " changed 0x%x",
2642 channel,
2643 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
2644 conf->power_level,
2645 conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
2646 changed);
2647
2648 /*
2649 * mac80211 will go to idle nearly immediately after transmitting some
2650 * frames, such as the deauth. To make sure those frames reach the air,
2651 * wait here until the TX queue is fully flushed.
2652 */
2653 if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
2654 (conf->flags & IEEE80211_CONF_IDLE))
2655 wl1271_tx_flush(wl);
2656
2657 mutex_lock(&wl->mutex);
2658
2659 /* we support configuring the channel and band even while off */
2660 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2661 wl->band = conf->channel->band;
2662 wl->channel = channel;
2663 }
2664
2665 if (changed & IEEE80211_CONF_CHANGE_POWER)
2666 wl->power_level = conf->power_level;
2667
2668 if (unlikely(wl->state == WL1271_STATE_OFF))
2669 goto out;
2670
2671 ret = wl1271_ps_elp_wakeup(wl);
2672 if (ret < 0)
2673 goto out;
2674
2675 /* configure each interface */
2676 wl12xx_for_each_wlvif(wl, wlvif) {
2677 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
2678 if (ret < 0)
2679 goto out_sleep;
2680 }
2681
2682 out_sleep:
2683 wl1271_ps_elp_sleep(wl);
2684
2685 out:
2686 mutex_unlock(&wl->mutex);
2687
2688 return ret;
2689 }
2690
2691 struct wl1271_filter_params {
2692 bool enabled;
2693 int mc_list_length;
2694 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
2695 };
2696
2697 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
2698 struct netdev_hw_addr_list *mc_list)
2699 {
2700 struct wl1271_filter_params *fp;
2701 struct netdev_hw_addr *ha;
2702 struct wl1271 *wl = hw->priv;
2703
2704 if (unlikely(wl->state == WL1271_STATE_OFF))
2705 return 0;
2706
2707 fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
2708 if (!fp) {
2709 wl1271_error("Out of memory setting filters.");
2710 return 0;
2711 }
2712
2713 /* update multicast filtering parameters */
2714 fp->mc_list_length = 0;
2715 if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
2716 fp->enabled = false;
2717 } else {
2718 fp->enabled = true;
2719 netdev_hw_addr_list_for_each(ha, mc_list) {
2720 memcpy(fp->mc_list[fp->mc_list_length],
2721 ha->addr, ETH_ALEN);
2722 fp->mc_list_length++;
2723 }
2724 }
2725
2726 return (u64)(unsigned long)fp;
2727 }
2728
2729 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
2730 FIF_ALLMULTI | \
2731 FIF_FCSFAIL | \
2732 FIF_BCN_PRBRESP_PROMISC | \
2733 FIF_CONTROL | \
2734 FIF_OTHER_BSS)
2735
2736 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
2737 unsigned int changed,
2738 unsigned int *total, u64 multicast)
2739 {
2740 struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
2741 struct wl1271 *wl = hw->priv;
2742 struct wl12xx_vif *wlvif;
2743
2744 int ret;
2745
2746 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
2747 " total %x", changed, *total);
2748
2749 mutex_lock(&wl->mutex);
2750
2751 *total &= WL1271_SUPPORTED_FILTERS;
2752 changed &= WL1271_SUPPORTED_FILTERS;
2753
2754 if (unlikely(wl->state == WL1271_STATE_OFF))
2755 goto out;
2756
2757 ret = wl1271_ps_elp_wakeup(wl);
2758 if (ret < 0)
2759 goto out;
2760
2761 wl12xx_for_each_wlvif(wl, wlvif) {
2762 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
2763 if (*total & FIF_ALLMULTI)
2764 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2765 false,
2766 NULL, 0);
2767 else if (fp)
2768 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2769 fp->enabled,
2770 fp->mc_list,
2771 fp->mc_list_length);
2772 if (ret < 0)
2773 goto out_sleep;
2774 }
2775 }
2776
2777 /*
2778 * the fw doesn't provide an api to configure the filters. instead,
2779 * the filters configuration is based on the active roles / ROC
2780 * state.
2781 */
2782
2783 out_sleep:
2784 wl1271_ps_elp_sleep(wl);
2785
2786 out:
2787 mutex_unlock(&wl->mutex);
2788 kfree(fp);
2789 }
2790
2791 static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2792 u8 id, u8 key_type, u8 key_size,
2793 const u8 *key, u8 hlid, u32 tx_seq_32,
2794 u16 tx_seq_16)
2795 {
2796 struct wl1271_ap_key *ap_key;
2797 int i;
2798
2799 wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
2800
2801 if (key_size > MAX_KEY_SIZE)
2802 return -EINVAL;
2803
2804 /*
2805 * Find next free entry in ap_keys. Also check we are not replacing
2806 * an existing key.
2807 */
2808 for (i = 0; i < MAX_NUM_KEYS; i++) {
2809 if (wlvif->ap.recorded_keys[i] == NULL)
2810 break;
2811
2812 if (wlvif->ap.recorded_keys[i]->id == id) {
2813 wl1271_warning("trying to record key replacement");
2814 return -EINVAL;
2815 }
2816 }
2817
2818 if (i == MAX_NUM_KEYS)
2819 return -EBUSY;
2820
2821 ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
2822 if (!ap_key)
2823 return -ENOMEM;
2824
2825 ap_key->id = id;
2826 ap_key->key_type = key_type;
2827 ap_key->key_size = key_size;
2828 memcpy(ap_key->key, key, key_size);
2829 ap_key->hlid = hlid;
2830 ap_key->tx_seq_32 = tx_seq_32;
2831 ap_key->tx_seq_16 = tx_seq_16;
2832
2833 wlvif->ap.recorded_keys[i] = ap_key;
2834 return 0;
2835 }
2836
2837 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2838 {
2839 int i;
2840
2841 for (i = 0; i < MAX_NUM_KEYS; i++) {
2842 kfree(wlvif->ap.recorded_keys[i]);
2843 wlvif->ap.recorded_keys[i] = NULL;
2844 }
2845 }
2846
2847 static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2848 {
2849 int i, ret = 0;
2850 struct wl1271_ap_key *key;
2851 bool wep_key_added = false;
2852
2853 for (i = 0; i < MAX_NUM_KEYS; i++) {
2854 u8 hlid;
2855 if (wlvif->ap.recorded_keys[i] == NULL)
2856 break;
2857
2858 key = wlvif->ap.recorded_keys[i];
2859 hlid = key->hlid;
2860 if (hlid == WL12XX_INVALID_LINK_ID)
2861 hlid = wlvif->ap.bcast_hlid;
2862
2863 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
2864 key->id, key->key_type,
2865 key->key_size, key->key,
2866 hlid, key->tx_seq_32,
2867 key->tx_seq_16);
2868 if (ret < 0)
2869 goto out;
2870
2871 if (key->key_type == KEY_WEP)
2872 wep_key_added = true;
2873 }
2874
2875 if (wep_key_added) {
2876 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
2877 wlvif->ap.bcast_hlid);
2878 if (ret < 0)
2879 goto out;
2880 }
2881
2882 out:
2883 wl1271_free_ap_keys(wl, wlvif);
2884 return ret;
2885 }
2886
2887 static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2888 u16 action, u8 id, u8 key_type,
2889 u8 key_size, const u8 *key, u32 tx_seq_32,
2890 u16 tx_seq_16, struct ieee80211_sta *sta)
2891 {
2892 int ret;
2893 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2894
2895 /*
2896 * A role set to GEM cipher requires different Tx settings (namely
2897 * spare blocks). Note when we are in this mode so the HW can adjust.
2898 */
2899 if (key_type == KEY_GEM) {
2900 if (action == KEY_ADD_OR_REPLACE)
2901 wlvif->is_gem = true;
2902 else if (action == KEY_REMOVE)
2903 wlvif->is_gem = false;
2904 }
2905
2906 if (is_ap) {
2907 struct wl1271_station *wl_sta;
2908 u8 hlid;
2909
2910 if (sta) {
2911 wl_sta = (struct wl1271_station *)sta->drv_priv;
2912 hlid = wl_sta->hlid;
2913 } else {
2914 hlid = wlvif->ap.bcast_hlid;
2915 }
2916
2917 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
2918 /*
2919 * We do not support removing keys after AP shutdown.
2920 * Pretend we do to make mac80211 happy.
2921 */
2922 if (action != KEY_ADD_OR_REPLACE)
2923 return 0;
2924
2925 ret = wl1271_record_ap_key(wl, wlvif, id,
2926 key_type, key_size,
2927 key, hlid, tx_seq_32,
2928 tx_seq_16);
2929 } else {
2930 ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
2931 id, key_type, key_size,
2932 key, hlid, tx_seq_32,
2933 tx_seq_16);
2934 }
2935
2936 if (ret < 0)
2937 return ret;
2938 } else {
2939 const u8 *addr;
2940 static const u8 bcast_addr[ETH_ALEN] = {
2941 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2942 };
2943
2944 addr = sta ? sta->addr : bcast_addr;
2945
2946 if (is_zero_ether_addr(addr)) {
2947 /* We dont support TX only encryption */
2948 return -EOPNOTSUPP;
2949 }
2950
2951 /* The wl1271 does not allow to remove unicast keys - they
2952 will be cleared automatically on next CMD_JOIN. Ignore the
2953 request silently, as we dont want the mac80211 to emit
2954 an error message. */
2955 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
2956 return 0;
2957
2958 /* don't remove key if hlid was already deleted */
2959 if (action == KEY_REMOVE &&
2960 wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
2961 return 0;
2962
2963 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
2964 id, key_type, key_size,
2965 key, addr, tx_seq_32,
2966 tx_seq_16);
2967 if (ret < 0)
2968 return ret;
2969
2970 /* the default WEP key needs to be configured at least once */
2971 if (key_type == KEY_WEP) {
2972 ret = wl12xx_cmd_set_default_wep_key(wl,
2973 wlvif->default_key,
2974 wlvif->sta.hlid);
2975 if (ret < 0)
2976 return ret;
2977 }
2978 }
2979
2980 return 0;
2981 }
2982
2983 static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2984 struct ieee80211_vif *vif,
2985 struct ieee80211_sta *sta,
2986 struct ieee80211_key_conf *key_conf)
2987 {
2988 struct wl1271 *wl = hw->priv;
2989 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2990 int ret;
2991 u32 tx_seq_32 = 0;
2992 u16 tx_seq_16 = 0;
2993 u8 key_type;
2994
2995 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
2996
2997 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
2998 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
2999 key_conf->cipher, key_conf->keyidx,
3000 key_conf->keylen, key_conf->flags);
3001 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
3002
3003 mutex_lock(&wl->mutex);
3004
3005 if (unlikely(wl->state == WL1271_STATE_OFF)) {
3006 ret = -EAGAIN;
3007 goto out_unlock;
3008 }
3009
3010 ret = wl1271_ps_elp_wakeup(wl);
3011 if (ret < 0)
3012 goto out_unlock;
3013
3014 switch (key_conf->cipher) {
3015 case WLAN_CIPHER_SUITE_WEP40:
3016 case WLAN_CIPHER_SUITE_WEP104:
3017 key_type = KEY_WEP;
3018
3019 key_conf->hw_key_idx = key_conf->keyidx;
3020 break;
3021 case WLAN_CIPHER_SUITE_TKIP:
3022 key_type = KEY_TKIP;
3023
3024 key_conf->hw_key_idx = key_conf->keyidx;
3025 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3026 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
3027 break;
3028 case WLAN_CIPHER_SUITE_CCMP:
3029 key_type = KEY_AES;
3030
3031 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3032 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3033 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
3034 break;
3035 case WL1271_CIPHER_SUITE_GEM:
3036 key_type = KEY_GEM;
3037 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3038 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
3039 break;
3040 default:
3041 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
3042
3043 ret = -EOPNOTSUPP;
3044 goto out_sleep;
3045 }
3046
3047 switch (cmd) {
3048 case SET_KEY:
3049 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
3050 key_conf->keyidx, key_type,
3051 key_conf->keylen, key_conf->key,
3052 tx_seq_32, tx_seq_16, sta);
3053 if (ret < 0) {
3054 wl1271_error("Could not add or replace key");
3055 goto out_sleep;
3056 }
3057
3058 /*
3059 * reconfiguring arp response if the unicast (or common)
3060 * encryption key type was changed
3061 */
3062 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3063 (sta || key_type == KEY_WEP) &&
3064 wlvif->encryption_type != key_type) {
3065 wlvif->encryption_type = key_type;
3066 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3067 if (ret < 0) {
3068 wl1271_warning("build arp rsp failed: %d", ret);
3069 goto out_sleep;
3070 }
3071 }
3072 break;
3073
3074 case DISABLE_KEY:
3075 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
3076 key_conf->keyidx, key_type,
3077 key_conf->keylen, key_conf->key,
3078 0, 0, sta);
3079 if (ret < 0) {
3080 wl1271_error("Could not remove key");
3081 goto out_sleep;
3082 }
3083 break;
3084
3085 default:
3086 wl1271_error("Unsupported key cmd 0x%x", cmd);
3087 ret = -EOPNOTSUPP;
3088 break;
3089 }
3090
3091 out_sleep:
3092 wl1271_ps_elp_sleep(wl);
3093
3094 out_unlock:
3095 mutex_unlock(&wl->mutex);
3096
3097 return ret;
3098 }
3099
3100 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
3101 struct ieee80211_vif *vif,
3102 struct cfg80211_scan_request *req)
3103 {
3104 struct wl1271 *wl = hw->priv;
3105 int ret;
3106 u8 *ssid = NULL;
3107 size_t len = 0;
3108
3109 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3110
3111 if (req->n_ssids) {
3112 ssid = req->ssids[0].ssid;
3113 len = req->ssids[0].ssid_len;
3114 }
3115
3116 mutex_lock(&wl->mutex);
3117
3118 if (wl->state == WL1271_STATE_OFF) {
3119 /*
3120 * We cannot return -EBUSY here because cfg80211 will expect
3121 * a call to ieee80211_scan_completed if we do - in this case
3122 * there won't be any call.
3123 */
3124 ret = -EAGAIN;
3125 goto out;
3126 }
3127
3128 ret = wl1271_ps_elp_wakeup(wl);
3129 if (ret < 0)
3130 goto out;
3131
3132 /* fail if there is any role in ROC */
3133 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
3134 /* don't allow scanning right now */
3135 ret = -EBUSY;
3136 goto out_sleep;
3137 }
3138
3139 ret = wl1271_scan(hw->priv, vif, ssid, len, req);
3140 out_sleep:
3141 wl1271_ps_elp_sleep(wl);
3142 out:
3143 mutex_unlock(&wl->mutex);
3144
3145 return ret;
3146 }
3147
3148 static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3149 struct ieee80211_vif *vif)
3150 {
3151 struct wl1271 *wl = hw->priv;
3152 int ret;
3153
3154 wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3155
3156 mutex_lock(&wl->mutex);
3157
3158 if (wl->state == WL1271_STATE_OFF)
3159 goto out;
3160
3161 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3162 goto out;
3163
3164 ret = wl1271_ps_elp_wakeup(wl);
3165 if (ret < 0)
3166 goto out;
3167
3168 if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
3169 ret = wl1271_scan_stop(wl);
3170 if (ret < 0)
3171 goto out_sleep;
3172 }
3173
3174 /*
3175 * Rearm the tx watchdog just before idling scan. This
3176 * prevents just-finished scans from triggering the watchdog
3177 */
3178 wl12xx_rearm_tx_watchdog_locked(wl);
3179
3180 wl->scan.state = WL1271_SCAN_STATE_IDLE;
3181 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
3182 wl->scan_vif = NULL;
3183 wl->scan.req = NULL;
3184 ieee80211_scan_completed(wl->hw, true);
3185
3186 out_sleep:
3187 wl1271_ps_elp_sleep(wl);
3188 out:
3189 mutex_unlock(&wl->mutex);
3190
3191 cancel_delayed_work_sync(&wl->scan_complete_work);
3192 }
3193
3194 static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3195 struct ieee80211_vif *vif,
3196 struct cfg80211_sched_scan_request *req,
3197 struct ieee80211_sched_scan_ies *ies)
3198 {
3199 struct wl1271 *wl = hw->priv;
3200 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3201 int ret;
3202
3203 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3204
3205 mutex_lock(&wl->mutex);
3206
3207 if (wl->state == WL1271_STATE_OFF) {
3208 ret = -EAGAIN;
3209 goto out;
3210 }
3211
3212 ret = wl1271_ps_elp_wakeup(wl);
3213 if (ret < 0)
3214 goto out;
3215
3216 ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
3217 if (ret < 0)
3218 goto out_sleep;
3219
3220 ret = wl1271_scan_sched_scan_start(wl, wlvif);
3221 if (ret < 0)
3222 goto out_sleep;
3223
3224 wl->sched_scanning = true;
3225
3226 out_sleep:
3227 wl1271_ps_elp_sleep(wl);
3228 out:
3229 mutex_unlock(&wl->mutex);
3230 return ret;
3231 }
3232
3233 static void wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3234 struct ieee80211_vif *vif)
3235 {
3236 struct wl1271 *wl = hw->priv;
3237 int ret;
3238
3239 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3240
3241 mutex_lock(&wl->mutex);
3242
3243 if (wl->state == WL1271_STATE_OFF)
3244 goto out;
3245
3246 ret = wl1271_ps_elp_wakeup(wl);
3247 if (ret < 0)
3248 goto out;
3249
3250 wl1271_scan_sched_scan_stop(wl);
3251
3252 wl1271_ps_elp_sleep(wl);
3253 out:
3254 mutex_unlock(&wl->mutex);
3255 }
3256
3257 static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3258 {
3259 struct wl1271 *wl = hw->priv;
3260 int ret = 0;
3261
3262 mutex_lock(&wl->mutex);
3263
3264 if (unlikely(wl->state == WL1271_STATE_OFF)) {
3265 ret = -EAGAIN;
3266 goto out;
3267 }
3268
3269 ret = wl1271_ps_elp_wakeup(wl);
3270 if (ret < 0)
3271 goto out;
3272
3273 ret = wl1271_acx_frag_threshold(wl, value);
3274 if (ret < 0)
3275 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3276
3277 wl1271_ps_elp_sleep(wl);
3278
3279 out:
3280 mutex_unlock(&wl->mutex);
3281
3282 return ret;
3283 }
3284
3285 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3286 {
3287 struct wl1271 *wl = hw->priv;
3288 struct wl12xx_vif *wlvif;
3289 int ret = 0;
3290
3291 mutex_lock(&wl->mutex);
3292
3293 if (unlikely(wl->state == WL1271_STATE_OFF)) {
3294 ret = -EAGAIN;
3295 goto out;
3296 }
3297
3298 ret = wl1271_ps_elp_wakeup(wl);
3299 if (ret < 0)
3300 goto out;
3301
3302 wl12xx_for_each_wlvif(wl, wlvif) {
3303 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3304 if (ret < 0)
3305 wl1271_warning("set rts threshold failed: %d", ret);
3306 }
3307 wl1271_ps_elp_sleep(wl);
3308
3309 out:
3310 mutex_unlock(&wl->mutex);
3311
3312 return ret;
3313 }
3314
3315 static int wl1271_ssid_set(struct ieee80211_vif *vif, struct sk_buff *skb,
3316 int offset)
3317 {
3318 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3319 u8 ssid_len;
3320 const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
3321 skb->len - offset);
3322
3323 if (!ptr) {
3324 wl1271_error("No SSID in IEs!");
3325 return -ENOENT;
3326 }
3327
3328 ssid_len = ptr[1];
3329 if (ssid_len > IEEE80211_MAX_SSID_LEN) {
3330 wl1271_error("SSID is too long!");
3331 return -EINVAL;
3332 }
3333
3334 wlvif->ssid_len = ssid_len;
3335 memcpy(wlvif->ssid, ptr+2, ssid_len);
3336 return 0;
3337 }
3338
3339 static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3340 {
3341 int len;
3342 const u8 *next, *end = skb->data + skb->len;
3343 u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3344 skb->len - ieoffset);
3345 if (!ie)
3346 return;
3347 len = ie[1] + 2;
3348 next = ie + len;
3349 memmove(ie, next, end - next);
3350 skb_trim(skb, skb->len - len);
3351 }
3352
3353 static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3354 unsigned int oui, u8 oui_type,
3355 int ieoffset)
3356 {
3357 int len;
3358 const u8 *next, *end = skb->data + skb->len;
3359 u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3360 skb->data + ieoffset,
3361 skb->len - ieoffset);
3362 if (!ie)
3363 return;
3364 len = ie[1] + 2;
3365 next = ie + len;
3366 memmove(ie, next, end - next);
3367 skb_trim(skb, skb->len - len);
3368 }
3369
3370 static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3371 struct ieee80211_vif *vif)
3372 {
3373 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3374 struct sk_buff *skb;
3375 int ret;
3376
3377 skb = ieee80211_proberesp_get(wl->hw, vif);
3378 if (!skb)
3379 return -EOPNOTSUPP;
3380
3381 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3382 CMD_TEMPL_AP_PROBE_RESPONSE,
3383 skb->data,
3384 skb->len, 0,
3385 rates);
3386
3387 dev_kfree_skb(skb);
3388 return ret;
3389 }
3390
3391 static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3392 struct ieee80211_vif *vif,
3393 u8 *probe_rsp_data,
3394 size_t probe_rsp_len,
3395 u32 rates)
3396 {
3397 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3398 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
3399 u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3400 int ssid_ie_offset, ie_offset, templ_len;
3401 const u8 *ptr;
3402
3403 /* no need to change probe response if the SSID is set correctly */
3404 if (wlvif->ssid_len > 0)
3405 return wl1271_cmd_template_set(wl, wlvif->role_id,
3406 CMD_TEMPL_AP_PROBE_RESPONSE,
3407 probe_rsp_data,
3408 probe_rsp_len, 0,
3409 rates);
3410
3411 if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3412 wl1271_error("probe_rsp template too big");
3413 return -EINVAL;
3414 }
3415
3416 /* start searching from IE offset */
3417 ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3418
3419 ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3420 probe_rsp_len - ie_offset);
3421 if (!ptr) {
3422 wl1271_error("No SSID in beacon!");
3423 return -EINVAL;
3424 }
3425
3426 ssid_ie_offset = ptr - probe_rsp_data;
3427 ptr += (ptr[1] + 2);
3428
3429 memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3430
3431 /* insert SSID from bss_conf */
3432 probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3433 probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3434 memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3435 bss_conf->ssid, bss_conf->ssid_len);
3436 templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3437
3438 memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3439 ptr, probe_rsp_len - (ptr - probe_rsp_data));
3440 templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3441
3442 return wl1271_cmd_template_set(wl, wlvif->role_id,
3443 CMD_TEMPL_AP_PROBE_RESPONSE,
3444 probe_rsp_templ,
3445 templ_len, 0,
3446 rates);
3447 }
3448
3449 static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
3450 struct ieee80211_vif *vif,
3451 struct ieee80211_bss_conf *bss_conf,
3452 u32 changed)
3453 {
3454 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3455 int ret = 0;
3456
3457 if (changed & BSS_CHANGED_ERP_SLOT) {
3458 if (bss_conf->use_short_slot)
3459 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
3460 else
3461 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
3462 if (ret < 0) {
3463 wl1271_warning("Set slot time failed %d", ret);
3464 goto out;
3465 }
3466 }
3467
3468 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3469 if (bss_conf->use_short_preamble)
3470 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
3471 else
3472 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
3473 }
3474
3475 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3476 if (bss_conf->use_cts_prot)
3477 ret = wl1271_acx_cts_protect(wl, wlvif,
3478 CTSPROTECT_ENABLE);
3479 else
3480 ret = wl1271_acx_cts_protect(wl, wlvif,
3481 CTSPROTECT_DISABLE);
3482 if (ret < 0) {
3483 wl1271_warning("Set ctsprotect failed %d", ret);
3484 goto out;
3485 }
3486 }
3487
3488 out:
3489 return ret;
3490 }
3491
3492 static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
3493 struct ieee80211_vif *vif,
3494 struct ieee80211_bss_conf *bss_conf,
3495 u32 changed)
3496 {
3497 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3498 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
3499 int ret = 0;
3500
3501 if ((changed & BSS_CHANGED_BEACON_INT)) {
3502 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
3503 bss_conf->beacon_int);
3504
3505 wlvif->beacon_int = bss_conf->beacon_int;
3506 }
3507
3508 if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
3509 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3510 if (!wl1271_ap_set_probe_resp_tmpl(wl, rate, vif)) {
3511 wl1271_debug(DEBUG_AP, "probe response updated");
3512 set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3513 }
3514 }
3515
3516 if ((changed & BSS_CHANGED_BEACON)) {
3517 struct ieee80211_hdr *hdr;
3518 u32 min_rate;
3519 int ieoffset = offsetof(struct ieee80211_mgmt,
3520 u.beacon.variable);
3521 struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
3522 u16 tmpl_id;
3523
3524 if (!beacon) {
3525 ret = -EINVAL;
3526 goto out;
3527 }
3528
3529 wl1271_debug(DEBUG_MASTER, "beacon updated");
3530
3531 ret = wl1271_ssid_set(vif, beacon, ieoffset);
3532 if (ret < 0) {
3533 dev_kfree_skb(beacon);
3534 goto out;
3535 }
3536 min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3537 tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
3538 CMD_TEMPL_BEACON;
3539 ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
3540 beacon->data,
3541 beacon->len, 0,
3542 min_rate);
3543 if (ret < 0) {
3544 dev_kfree_skb(beacon);
3545 goto out;
3546 }
3547
3548 /*
3549 * In case we already have a probe-resp beacon set explicitly
3550 * by usermode, don't use the beacon data.
3551 */
3552 if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
3553 goto end_bcn;
3554
3555 /* remove TIM ie from probe response */
3556 wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
3557
3558 /*
3559 * remove p2p ie from probe response.
3560 * the fw reponds to probe requests that don't include
3561 * the p2p ie. probe requests with p2p ie will be passed,
3562 * and will be responded by the supplicant (the spec
3563 * forbids including the p2p ie when responding to probe
3564 * requests that didn't include it).
3565 */
3566 wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
3567 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
3568
3569 hdr = (struct ieee80211_hdr *) beacon->data;
3570 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3571 IEEE80211_STYPE_PROBE_RESP);
3572 if (is_ap)
3573 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
3574 beacon->data,
3575 beacon->len,
3576 min_rate);
3577 else
3578 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3579 CMD_TEMPL_PROBE_RESPONSE,
3580 beacon->data,
3581 beacon->len, 0,
3582 min_rate);
3583 end_bcn:
3584 dev_kfree_skb(beacon);
3585 if (ret < 0)
3586 goto out;
3587 }
3588
3589 out:
3590 if (ret != 0)
3591 wl1271_error("beacon info change failed: %d", ret);
3592 return ret;
3593 }
3594
3595 /* AP mode changes */
3596 static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
3597 struct ieee80211_vif *vif,
3598 struct ieee80211_bss_conf *bss_conf,
3599 u32 changed)
3600 {
3601 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3602 int ret = 0;
3603
3604 if ((changed & BSS_CHANGED_BASIC_RATES)) {
3605 u32 rates = bss_conf->basic_rates;
3606
3607 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
3608 wlvif->band);
3609 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
3610 wlvif->basic_rate_set);
3611
3612 ret = wl1271_init_ap_rates(wl, wlvif);
3613 if (ret < 0) {
3614 wl1271_error("AP rate policy change failed %d", ret);
3615 goto out;
3616 }
3617
3618 ret = wl1271_ap_init_templates(wl, vif);
3619 if (ret < 0)
3620 goto out;
3621 }
3622
3623 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
3624 if (ret < 0)
3625 goto out;
3626
3627 if ((changed & BSS_CHANGED_BEACON_ENABLED)) {
3628 if (bss_conf->enable_beacon) {
3629 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3630 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
3631 if (ret < 0)
3632 goto out;
3633
3634 ret = wl1271_ap_init_hwenc(wl, wlvif);
3635 if (ret < 0)
3636 goto out;
3637
3638 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
3639 wl1271_debug(DEBUG_AP, "started AP");
3640 }
3641 } else {
3642 if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3643 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
3644 if (ret < 0)
3645 goto out;
3646
3647 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
3648 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
3649 &wlvif->flags);
3650 wl1271_debug(DEBUG_AP, "stopped AP");
3651 }
3652 }
3653 }
3654
3655 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
3656 if (ret < 0)
3657 goto out;
3658
3659 /* Handle HT information change */
3660 if ((changed & BSS_CHANGED_HT) &&
3661 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3662 ret = wl1271_acx_set_ht_information(wl, wlvif,
3663 bss_conf->ht_operation_mode);
3664 if (ret < 0) {
3665 wl1271_warning("Set ht information failed %d", ret);
3666 goto out;
3667 }
3668 }
3669
3670 out:
3671 return;
3672 }
3673
3674 /* STA/IBSS mode changes */
3675 static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
3676 struct ieee80211_vif *vif,
3677 struct ieee80211_bss_conf *bss_conf,
3678 u32 changed)
3679 {
3680 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3681 bool do_join = false, set_assoc = false;
3682 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
3683 bool ibss_joined = false;
3684 u32 sta_rate_set = 0;
3685 int ret;
3686 struct ieee80211_sta *sta;
3687 bool sta_exists = false;
3688 struct ieee80211_sta_ht_cap sta_ht_cap;
3689
3690 if (is_ibss) {
3691 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
3692 changed);
3693 if (ret < 0)
3694 goto out;
3695 }
3696
3697 if (changed & BSS_CHANGED_IBSS) {
3698 if (bss_conf->ibss_joined) {
3699 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
3700 ibss_joined = true;
3701 } else {
3702 if (test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED,
3703 &wlvif->flags))
3704 wl1271_unjoin(wl, wlvif);
3705 }
3706 }
3707
3708 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
3709 do_join = true;
3710
3711 /* Need to update the SSID (for filtering etc) */
3712 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
3713 do_join = true;
3714
3715 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
3716 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
3717 bss_conf->enable_beacon ? "enabled" : "disabled");
3718
3719 do_join = true;
3720 }
3721
3722 if (changed & BSS_CHANGED_IDLE && !is_ibss) {
3723 ret = wl1271_sta_handle_idle(wl, wlvif, bss_conf->idle);
3724 if (ret < 0)
3725 wl1271_warning("idle mode change failed %d", ret);
3726 }
3727
3728 if ((changed & BSS_CHANGED_CQM)) {
3729 bool enable = false;
3730 if (bss_conf->cqm_rssi_thold)
3731 enable = true;
3732 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
3733 bss_conf->cqm_rssi_thold,
3734 bss_conf->cqm_rssi_hyst);
3735 if (ret < 0)
3736 goto out;
3737 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
3738 }
3739
3740 if (changed & BSS_CHANGED_BSSID)
3741 if (!is_zero_ether_addr(bss_conf->bssid)) {
3742 ret = wl12xx_cmd_build_null_data(wl, wlvif);
3743 if (ret < 0)
3744 goto out;
3745
3746 ret = wl1271_build_qos_null_data(wl, vif);
3747 if (ret < 0)
3748 goto out;
3749 }
3750
3751 if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_HT)) {
3752 rcu_read_lock();
3753 sta = ieee80211_find_sta(vif, bss_conf->bssid);
3754 if (!sta)
3755 goto sta_not_found;
3756
3757 /* save the supp_rates of the ap */
3758 sta_rate_set = sta->supp_rates[wl->hw->conf.channel->band];
3759 if (sta->ht_cap.ht_supported)
3760 sta_rate_set |=
3761 (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET);
3762 sta_ht_cap = sta->ht_cap;
3763 sta_exists = true;
3764
3765 sta_not_found:
3766 rcu_read_unlock();
3767 }
3768
3769 if ((changed & BSS_CHANGED_ASSOC)) {
3770 if (bss_conf->assoc) {
3771 u32 rates;
3772 int ieoffset;
3773 wlvif->aid = bss_conf->aid;
3774 wlvif->beacon_int = bss_conf->beacon_int;
3775 do_join = true;
3776 set_assoc = true;
3777
3778 /*
3779 * use basic rates from AP, and determine lowest rate
3780 * to use with control frames.
3781 */
3782 rates = bss_conf->basic_rates;
3783 wlvif->basic_rate_set =
3784 wl1271_tx_enabled_rates_get(wl, rates,
3785 wlvif->band);
3786 wlvif->basic_rate =
3787 wl1271_tx_min_rate_get(wl,
3788 wlvif->basic_rate_set);
3789 if (sta_rate_set)
3790 wlvif->rate_set =
3791 wl1271_tx_enabled_rates_get(wl,
3792 sta_rate_set,
3793 wlvif->band);
3794 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3795 if (ret < 0)
3796 goto out;
3797
3798 /*
3799 * with wl1271, we don't need to update the
3800 * beacon_int and dtim_period, because the firmware
3801 * updates it by itself when the first beacon is
3802 * received after a join.
3803 */
3804 ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
3805 if (ret < 0)
3806 goto out;
3807
3808 /*
3809 * Get a template for hardware connection maintenance
3810 */
3811 dev_kfree_skb(wlvif->probereq);
3812 wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
3813 wlvif,
3814 NULL);
3815 ieoffset = offsetof(struct ieee80211_mgmt,
3816 u.probe_req.variable);
3817 wl1271_ssid_set(vif, wlvif->probereq, ieoffset);
3818
3819 /* enable the connection monitoring feature */
3820 ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
3821 if (ret < 0)
3822 goto out;
3823 } else {
3824 /* use defaults when not associated */
3825 bool was_assoc =
3826 !!test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED,
3827 &wlvif->flags);
3828 bool was_ifup =
3829 !!test_and_clear_bit(WLVIF_FLAG_STA_STATE_SENT,
3830 &wlvif->flags);
3831 wlvif->aid = 0;
3832
3833 /* free probe-request template */
3834 dev_kfree_skb(wlvif->probereq);
3835 wlvif->probereq = NULL;
3836
3837 /* revert back to minimum rates for the current band */
3838 wl1271_set_band_rate(wl, wlvif);
3839 wlvif->basic_rate =
3840 wl1271_tx_min_rate_get(wl,
3841 wlvif->basic_rate_set);
3842 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3843 if (ret < 0)
3844 goto out;
3845
3846 /* disable connection monitor features */
3847 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
3848
3849 /* Disable the keep-alive feature */
3850 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
3851 if (ret < 0)
3852 goto out;
3853
3854 /* restore the bssid filter and go to dummy bssid */
3855 if (was_assoc) {
3856 /*
3857 * we might have to disable roc, if there was
3858 * no IF_OPER_UP notification.
3859 */
3860 if (!was_ifup) {
3861 ret = wl12xx_croc(wl, wlvif->role_id);
3862 if (ret < 0)
3863 goto out;
3864 }
3865 /*
3866 * (we also need to disable roc in case of
3867 * roaming on the same channel. until we will
3868 * have a better flow...)
3869 */
3870 if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
3871 ret = wl12xx_croc(wl,
3872 wlvif->dev_role_id);
3873 if (ret < 0)
3874 goto out;
3875 }
3876
3877 wl1271_unjoin(wl, wlvif);
3878 if (!bss_conf->idle)
3879 wl12xx_start_dev(wl, wlvif);
3880 }
3881 }
3882 }
3883
3884 if (changed & BSS_CHANGED_IBSS) {
3885 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
3886 bss_conf->ibss_joined);
3887
3888 if (bss_conf->ibss_joined) {
3889 u32 rates = bss_conf->basic_rates;
3890 wlvif->basic_rate_set =
3891 wl1271_tx_enabled_rates_get(wl, rates,
3892 wlvif->band);
3893 wlvif->basic_rate =
3894 wl1271_tx_min_rate_get(wl,
3895 wlvif->basic_rate_set);
3896
3897 /* by default, use 11b + OFDM rates */
3898 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
3899 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3900 if (ret < 0)
3901 goto out;
3902 }
3903 }
3904
3905 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
3906 if (ret < 0)
3907 goto out;
3908
3909 if (do_join) {
3910 ret = wl1271_join(wl, wlvif, set_assoc);
3911 if (ret < 0) {
3912 wl1271_warning("cmd join failed %d", ret);
3913 goto out;
3914 }
3915
3916 /* ROC until connected (after EAPOL exchange) */
3917 if (!is_ibss) {
3918 ret = wl12xx_roc(wl, wlvif, wlvif->role_id);
3919 if (ret < 0)
3920 goto out;
3921
3922 if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
3923 wl12xx_set_authorized(wl, wlvif);
3924 }
3925 /*
3926 * stop device role if started (we might already be in
3927 * STA/IBSS role).
3928 */
3929 if (wl12xx_dev_role_started(wlvif)) {
3930 ret = wl12xx_stop_dev(wl, wlvif);
3931 if (ret < 0)
3932 goto out;
3933 }
3934 }
3935
3936 /* Handle new association with HT. Do this after join. */
3937 if (sta_exists) {
3938 if ((changed & BSS_CHANGED_HT) &&
3939 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3940 ret = wl1271_acx_set_ht_capabilities(wl,
3941 &sta_ht_cap,
3942 true,
3943 wlvif->sta.hlid);
3944 if (ret < 0) {
3945 wl1271_warning("Set ht cap true failed %d",
3946 ret);
3947 goto out;
3948 }
3949 }
3950 /* handle new association without HT and disassociation */
3951 else if (changed & BSS_CHANGED_ASSOC) {
3952 ret = wl1271_acx_set_ht_capabilities(wl,
3953 &sta_ht_cap,
3954 false,
3955 wlvif->sta.hlid);
3956 if (ret < 0) {
3957 wl1271_warning("Set ht cap false failed %d",
3958 ret);
3959 goto out;
3960 }
3961 }
3962 }
3963
3964 /* Handle HT information change. Done after join. */
3965 if ((changed & BSS_CHANGED_HT) &&
3966 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3967 ret = wl1271_acx_set_ht_information(wl, wlvif,
3968 bss_conf->ht_operation_mode);
3969 if (ret < 0) {
3970 wl1271_warning("Set ht information failed %d", ret);
3971 goto out;
3972 }
3973 }
3974
3975 /* Handle arp filtering. Done after join. */
3976 if ((changed & BSS_CHANGED_ARP_FILTER) ||
3977 (!is_ibss && (changed & BSS_CHANGED_QOS))) {
3978 __be32 addr = bss_conf->arp_addr_list[0];
3979 wlvif->sta.qos = bss_conf->qos;
3980 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
3981
3982 if (bss_conf->arp_addr_cnt == 1 &&
3983 bss_conf->arp_filter_enabled) {
3984 wlvif->ip_addr = addr;
3985 /*
3986 * The template should have been configured only upon
3987 * association. however, it seems that the correct ip
3988 * isn't being set (when sending), so we have to
3989 * reconfigure the template upon every ip change.
3990 */
3991 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3992 if (ret < 0) {
3993 wl1271_warning("build arp rsp failed: %d", ret);
3994 goto out;
3995 }
3996
3997 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
3998 (ACX_ARP_FILTER_ARP_FILTERING |
3999 ACX_ARP_FILTER_AUTO_ARP),
4000 addr);
4001 } else {
4002 wlvif->ip_addr = 0;
4003 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4004 }
4005
4006 if (ret < 0)
4007 goto out;
4008 }
4009
4010 out:
4011 return;
4012 }
4013
4014 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4015 struct ieee80211_vif *vif,
4016 struct ieee80211_bss_conf *bss_conf,
4017 u32 changed)
4018 {
4019 struct wl1271 *wl = hw->priv;
4020 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4021 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
4022 int ret;
4023
4024 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed 0x%x",
4025 (int)changed);
4026
4027 mutex_lock(&wl->mutex);
4028
4029 if (unlikely(wl->state == WL1271_STATE_OFF))
4030 goto out;
4031
4032 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4033 goto out;
4034
4035 ret = wl1271_ps_elp_wakeup(wl);
4036 if (ret < 0)
4037 goto out;
4038
4039 if (is_ap)
4040 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4041 else
4042 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4043
4044 wl1271_ps_elp_sleep(wl);
4045
4046 out:
4047 mutex_unlock(&wl->mutex);
4048 }
4049
4050 static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4051 struct ieee80211_vif *vif, u16 queue,
4052 const struct ieee80211_tx_queue_params *params)
4053 {
4054 struct wl1271 *wl = hw->priv;
4055 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4056 u8 ps_scheme;
4057 int ret = 0;
4058
4059 mutex_lock(&wl->mutex);
4060
4061 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4062
4063 if (params->uapsd)
4064 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4065 else
4066 ps_scheme = CONF_PS_SCHEME_LEGACY;
4067
4068 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4069 goto out;
4070
4071 ret = wl1271_ps_elp_wakeup(wl);
4072 if (ret < 0)
4073 goto out;
4074
4075 /*
4076 * the txop is confed in units of 32us by the mac80211,
4077 * we need us
4078 */
4079 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4080 params->cw_min, params->cw_max,
4081 params->aifs, params->txop << 5);
4082 if (ret < 0)
4083 goto out_sleep;
4084
4085 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4086 CONF_CHANNEL_TYPE_EDCF,
4087 wl1271_tx_get_queue(queue),
4088 ps_scheme, CONF_ACK_POLICY_LEGACY,
4089 0, 0);
4090
4091 out_sleep:
4092 wl1271_ps_elp_sleep(wl);
4093
4094 out:
4095 mutex_unlock(&wl->mutex);
4096
4097 return ret;
4098 }
4099
4100 static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4101 struct ieee80211_vif *vif)
4102 {
4103
4104 struct wl1271 *wl = hw->priv;
4105 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4106 u64 mactime = ULLONG_MAX;
4107 int ret;
4108
4109 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4110
4111 mutex_lock(&wl->mutex);
4112
4113 if (unlikely(wl->state == WL1271_STATE_OFF))
4114 goto out;
4115
4116 ret = wl1271_ps_elp_wakeup(wl);
4117 if (ret < 0)
4118 goto out;
4119
4120 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
4121 if (ret < 0)
4122 goto out_sleep;
4123
4124 out_sleep:
4125 wl1271_ps_elp_sleep(wl);
4126
4127 out:
4128 mutex_unlock(&wl->mutex);
4129 return mactime;
4130 }
4131
4132 static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4133 struct survey_info *survey)
4134 {
4135 struct wl1271 *wl = hw->priv;
4136 struct ieee80211_conf *conf = &hw->conf;
4137
4138 if (idx != 0)
4139 return -ENOENT;
4140
4141 survey->channel = conf->channel;
4142 survey->filled = SURVEY_INFO_NOISE_DBM;
4143 survey->noise = wl->noise;
4144
4145 return 0;
4146 }
4147
4148 static int wl1271_allocate_sta(struct wl1271 *wl,
4149 struct wl12xx_vif *wlvif,
4150 struct ieee80211_sta *sta)
4151 {
4152 struct wl1271_station *wl_sta;
4153 int ret;
4154
4155
4156 if (wl->active_sta_count >= AP_MAX_STATIONS) {
4157 wl1271_warning("could not allocate HLID - too much stations");
4158 return -EBUSY;
4159 }
4160
4161 wl_sta = (struct wl1271_station *)sta->drv_priv;
4162 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4163 if (ret < 0) {
4164 wl1271_warning("could not allocate HLID - too many links");
4165 return -EBUSY;
4166 }
4167
4168 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
4169 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
4170 wl->active_sta_count++;
4171 return 0;
4172 }
4173
4174 void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
4175 {
4176 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
4177 return;
4178
4179 clear_bit(hlid, wlvif->ap.sta_hlid_map);
4180 memset(wl->links[hlid].addr, 0, ETH_ALEN);
4181 wl->links[hlid].ba_bitmap = 0;
4182 __clear_bit(hlid, &wl->ap_ps_map);
4183 __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
4184 wl12xx_free_link(wl, wlvif, &hlid);
4185 wl->active_sta_count--;
4186
4187 /*
4188 * rearm the tx watchdog when the last STA is freed - give the FW a
4189 * chance to return STA-buffered packets before complaining.
4190 */
4191 if (wl->active_sta_count == 0)
4192 wl12xx_rearm_tx_watchdog_locked(wl);
4193 }
4194
4195 static int wl12xx_sta_add(struct wl1271 *wl,
4196 struct wl12xx_vif *wlvif,
4197 struct ieee80211_sta *sta)
4198 {
4199 struct wl1271_station *wl_sta;
4200 int ret = 0;
4201 u8 hlid;
4202
4203 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4204
4205 ret = wl1271_allocate_sta(wl, wlvif, sta);
4206 if (ret < 0)
4207 return ret;
4208
4209 wl_sta = (struct wl1271_station *)sta->drv_priv;
4210 hlid = wl_sta->hlid;
4211
4212 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
4213 if (ret < 0)
4214 wl1271_free_sta(wl, wlvif, hlid);
4215
4216 return ret;
4217 }
4218
4219 static int wl12xx_sta_remove(struct wl1271 *wl,
4220 struct wl12xx_vif *wlvif,
4221 struct ieee80211_sta *sta)
4222 {
4223 struct wl1271_station *wl_sta;
4224 int ret = 0, id;
4225
4226 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4227
4228 wl_sta = (struct wl1271_station *)sta->drv_priv;
4229 id = wl_sta->hlid;
4230 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
4231 return -EINVAL;
4232
4233 ret = wl12xx_cmd_remove_peer(wl, wl_sta->hlid);
4234 if (ret < 0)
4235 return ret;
4236
4237 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
4238 return ret;
4239 }
4240
4241 static int wl12xx_update_sta_state(struct wl1271 *wl,
4242 struct wl12xx_vif *wlvif,
4243 struct ieee80211_sta *sta,
4244 enum ieee80211_sta_state old_state,
4245 enum ieee80211_sta_state new_state)
4246 {
4247 struct wl1271_station *wl_sta;
4248 u8 hlid;
4249 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
4250 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
4251 int ret;
4252
4253 wl_sta = (struct wl1271_station *)sta->drv_priv;
4254 hlid = wl_sta->hlid;
4255
4256 /* Add station (AP mode) */
4257 if (is_ap &&
4258 old_state == IEEE80211_STA_NOTEXIST &&
4259 new_state == IEEE80211_STA_NONE)
4260 return wl12xx_sta_add(wl, wlvif, sta);
4261
4262 /* Remove station (AP mode) */
4263 if (is_ap &&
4264 old_state == IEEE80211_STA_NONE &&
4265 new_state == IEEE80211_STA_NOTEXIST) {
4266 /* must not fail */
4267 wl12xx_sta_remove(wl, wlvif, sta);
4268 return 0;
4269 }
4270
4271 /* Authorize station (AP mode) */
4272 if (is_ap &&
4273 new_state == IEEE80211_STA_AUTHORIZED) {
4274 ret = wl12xx_cmd_set_peer_state(wl, hlid);
4275 if (ret < 0)
4276 return ret;
4277
4278 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
4279 hlid);
4280 return ret;
4281 }
4282
4283 /* Authorize station */
4284 if (is_sta &&
4285 new_state == IEEE80211_STA_AUTHORIZED) {
4286 set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
4287 return wl12xx_set_authorized(wl, wlvif);
4288 }
4289
4290 if (is_sta &&
4291 old_state == IEEE80211_STA_AUTHORIZED &&
4292 new_state == IEEE80211_STA_ASSOC) {
4293 clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
4294 return 0;
4295 }
4296
4297 return 0;
4298 }
4299
4300 static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
4301 struct ieee80211_vif *vif,
4302 struct ieee80211_sta *sta,
4303 enum ieee80211_sta_state old_state,
4304 enum ieee80211_sta_state new_state)
4305 {
4306 struct wl1271 *wl = hw->priv;
4307 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4308 int ret;
4309
4310 wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
4311 sta->aid, old_state, new_state);
4312
4313 mutex_lock(&wl->mutex);
4314
4315 if (unlikely(wl->state == WL1271_STATE_OFF)) {
4316 ret = -EBUSY;
4317 goto out;
4318 }
4319
4320 ret = wl1271_ps_elp_wakeup(wl);
4321 if (ret < 0)
4322 goto out;
4323
4324 ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
4325
4326 wl1271_ps_elp_sleep(wl);
4327 out:
4328 mutex_unlock(&wl->mutex);
4329 if (new_state < old_state)
4330 return 0;
4331 return ret;
4332 }
4333
4334 static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
4335 struct ieee80211_vif *vif,
4336 enum ieee80211_ampdu_mlme_action action,
4337 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4338 u8 buf_size)
4339 {
4340 struct wl1271 *wl = hw->priv;
4341 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4342 int ret;
4343 u8 hlid, *ba_bitmap;
4344
4345 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
4346 tid);
4347
4348 /* sanity check - the fields in FW are only 8bits wide */
4349 if (WARN_ON(tid > 0xFF))
4350 return -ENOTSUPP;
4351
4352 mutex_lock(&wl->mutex);
4353
4354 if (unlikely(wl->state == WL1271_STATE_OFF)) {
4355 ret = -EAGAIN;
4356 goto out;
4357 }
4358
4359 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
4360 hlid = wlvif->sta.hlid;
4361 ba_bitmap = &wlvif->sta.ba_rx_bitmap;
4362 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
4363 struct wl1271_station *wl_sta;
4364
4365 wl_sta = (struct wl1271_station *)sta->drv_priv;
4366 hlid = wl_sta->hlid;
4367 ba_bitmap = &wl->links[hlid].ba_bitmap;
4368 } else {
4369 ret = -EINVAL;
4370 goto out;
4371 }
4372
4373 ret = wl1271_ps_elp_wakeup(wl);
4374 if (ret < 0)
4375 goto out;
4376
4377 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
4378 tid, action);
4379
4380 switch (action) {
4381 case IEEE80211_AMPDU_RX_START:
4382 if (!wlvif->ba_support || !wlvif->ba_allowed) {
4383 ret = -ENOTSUPP;
4384 break;
4385 }
4386
4387 if (wl->ba_rx_session_count >= RX_BA_MAX_SESSIONS) {
4388 ret = -EBUSY;
4389 wl1271_error("exceeded max RX BA sessions");
4390 break;
4391 }
4392
4393 if (*ba_bitmap & BIT(tid)) {
4394 ret = -EINVAL;
4395 wl1271_error("cannot enable RX BA session on active "
4396 "tid: %d", tid);
4397 break;
4398 }
4399
4400 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
4401 hlid);
4402 if (!ret) {
4403 *ba_bitmap |= BIT(tid);
4404 wl->ba_rx_session_count++;
4405 }
4406 break;
4407
4408 case IEEE80211_AMPDU_RX_STOP:
4409 if (!(*ba_bitmap & BIT(tid))) {
4410 ret = -EINVAL;
4411 wl1271_error("no active RX BA session on tid: %d",
4412 tid);
4413 break;
4414 }
4415
4416 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
4417 hlid);
4418 if (!ret) {
4419 *ba_bitmap &= ~BIT(tid);
4420 wl->ba_rx_session_count--;
4421 }
4422 break;
4423
4424 /*
4425 * The BA initiator session management in FW independently.
4426 * Falling break here on purpose for all TX APDU commands.
4427 */
4428 case IEEE80211_AMPDU_TX_START:
4429 case IEEE80211_AMPDU_TX_STOP:
4430 case IEEE80211_AMPDU_TX_OPERATIONAL:
4431 ret = -EINVAL;
4432 break;
4433
4434 default:
4435 wl1271_error("Incorrect ampdu action id=%x\n", action);
4436 ret = -EINVAL;
4437 }
4438
4439 wl1271_ps_elp_sleep(wl);
4440
4441 out:
4442 mutex_unlock(&wl->mutex);
4443
4444 return ret;
4445 }
4446
4447 static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
4448 struct ieee80211_vif *vif,
4449 const struct cfg80211_bitrate_mask *mask)
4450 {
4451 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4452 struct wl1271 *wl = hw->priv;
4453 int i, ret = 0;
4454
4455 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
4456 mask->control[NL80211_BAND_2GHZ].legacy,
4457 mask->control[NL80211_BAND_5GHZ].legacy);
4458
4459 mutex_lock(&wl->mutex);
4460
4461 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
4462 wlvif->bitrate_masks[i] =
4463 wl1271_tx_enabled_rates_get(wl,
4464 mask->control[i].legacy,
4465 i);
4466
4467 if (unlikely(wl->state == WL1271_STATE_OFF))
4468 goto out;
4469
4470 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4471 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
4472
4473 ret = wl1271_ps_elp_wakeup(wl);
4474 if (ret < 0)
4475 goto out;
4476
4477 wl1271_set_band_rate(wl, wlvif);
4478 wlvif->basic_rate =
4479 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4480 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4481
4482 wl1271_ps_elp_sleep(wl);
4483 }
4484 out:
4485 mutex_unlock(&wl->mutex);
4486
4487 return ret;
4488 }
4489
4490 static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
4491 struct ieee80211_channel_switch *ch_switch)
4492 {
4493 struct wl1271 *wl = hw->priv;
4494 struct wl12xx_vif *wlvif;
4495 int ret;
4496
4497 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
4498
4499 wl1271_tx_flush(wl);
4500
4501 mutex_lock(&wl->mutex);
4502
4503 if (unlikely(wl->state == WL1271_STATE_OFF)) {
4504 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4505 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4506 ieee80211_chswitch_done(vif, false);
4507 }
4508 goto out;
4509 }
4510
4511 ret = wl1271_ps_elp_wakeup(wl);
4512 if (ret < 0)
4513 goto out;
4514
4515 /* TODO: change mac80211 to pass vif as param */
4516 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4517 ret = wl12xx_cmd_channel_switch(wl, wlvif, ch_switch);
4518
4519 if (!ret)
4520 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
4521 }
4522
4523 wl1271_ps_elp_sleep(wl);
4524
4525 out:
4526 mutex_unlock(&wl->mutex);
4527 }
4528
4529 static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
4530 {
4531 struct wl1271 *wl = hw->priv;
4532 bool ret = false;
4533
4534 mutex_lock(&wl->mutex);
4535
4536 if (unlikely(wl->state == WL1271_STATE_OFF))
4537 goto out;
4538
4539 /* packets are considered pending if in the TX queue or the FW */
4540 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
4541 out:
4542 mutex_unlock(&wl->mutex);
4543
4544 return ret;
4545 }
4546
4547 /* can't be const, mac80211 writes to this */
4548 static struct ieee80211_rate wl1271_rates[] = {
4549 { .bitrate = 10,
4550 .hw_value = CONF_HW_BIT_RATE_1MBPS,
4551 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
4552 { .bitrate = 20,
4553 .hw_value = CONF_HW_BIT_RATE_2MBPS,
4554 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
4555 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4556 { .bitrate = 55,
4557 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
4558 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
4559 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4560 { .bitrate = 110,
4561 .hw_value = CONF_HW_BIT_RATE_11MBPS,
4562 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
4563 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4564 { .bitrate = 60,
4565 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4566 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4567 { .bitrate = 90,
4568 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4569 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4570 { .bitrate = 120,
4571 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4572 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4573 { .bitrate = 180,
4574 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4575 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4576 { .bitrate = 240,
4577 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4578 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4579 { .bitrate = 360,
4580 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4581 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4582 { .bitrate = 480,
4583 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4584 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4585 { .bitrate = 540,
4586 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4587 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4588 };
4589
4590 /* can't be const, mac80211 writes to this */
4591 static struct ieee80211_channel wl1271_channels[] = {
4592 { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
4593 { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
4594 { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
4595 { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
4596 { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
4597 { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
4598 { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
4599 { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
4600 { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
4601 { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
4602 { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
4603 { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
4604 { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
4605 { .hw_value = 14, .center_freq = 2484, .max_power = 25 },
4606 };
4607
4608 /* 11n STA capabilities */
4609 #define HW_RX_HIGHEST_RATE 72
4610
4611 #define WL12XX_HT_CAP { \
4612 .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 | \
4613 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT), \
4614 .ht_supported = true, \
4615 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_8K, \
4616 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, \
4617 .mcs = { \
4618 .rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, \
4619 .rx_highest = cpu_to_le16(HW_RX_HIGHEST_RATE), \
4620 .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
4621 }, \
4622 }
4623
4624 /* can't be const, mac80211 writes to this */
4625 static struct ieee80211_supported_band wl1271_band_2ghz = {
4626 .channels = wl1271_channels,
4627 .n_channels = ARRAY_SIZE(wl1271_channels),
4628 .bitrates = wl1271_rates,
4629 .n_bitrates = ARRAY_SIZE(wl1271_rates),
4630 .ht_cap = WL12XX_HT_CAP,
4631 };
4632
4633 /* 5 GHz data rates for WL1273 */
4634 static struct ieee80211_rate wl1271_rates_5ghz[] = {
4635 { .bitrate = 60,
4636 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4637 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4638 { .bitrate = 90,
4639 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4640 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4641 { .bitrate = 120,
4642 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4643 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4644 { .bitrate = 180,
4645 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4646 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4647 { .bitrate = 240,
4648 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4649 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4650 { .bitrate = 360,
4651 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4652 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4653 { .bitrate = 480,
4654 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4655 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4656 { .bitrate = 540,
4657 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4658 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4659 };
4660
4661 /* 5 GHz band channels for WL1273 */
4662 static struct ieee80211_channel wl1271_channels_5ghz[] = {
4663 { .hw_value = 7, .center_freq = 5035, .max_power = 25 },
4664 { .hw_value = 8, .center_freq = 5040, .max_power = 25 },
4665 { .hw_value = 9, .center_freq = 5045, .max_power = 25 },
4666 { .hw_value = 11, .center_freq = 5055, .max_power = 25 },
4667 { .hw_value = 12, .center_freq = 5060, .max_power = 25 },
4668 { .hw_value = 16, .center_freq = 5080, .max_power = 25 },
4669 { .hw_value = 34, .center_freq = 5170, .max_power = 25 },
4670 { .hw_value = 36, .center_freq = 5180, .max_power = 25 },
4671 { .hw_value = 38, .center_freq = 5190, .max_power = 25 },
4672 { .hw_value = 40, .center_freq = 5200, .max_power = 25 },
4673 { .hw_value = 42, .center_freq = 5210, .max_power = 25 },
4674 { .hw_value = 44, .center_freq = 5220, .max_power = 25 },
4675 { .hw_value = 46, .center_freq = 5230, .max_power = 25 },
4676 { .hw_value = 48, .center_freq = 5240, .max_power = 25 },
4677 { .hw_value = 52, .center_freq = 5260, .max_power = 25 },
4678 { .hw_value = 56, .center_freq = 5280, .max_power = 25 },
4679 { .hw_value = 60, .center_freq = 5300, .max_power = 25 },
4680 { .hw_value = 64, .center_freq = 5320, .max_power = 25 },
4681 { .hw_value = 100, .center_freq = 5500, .max_power = 25 },
4682 { .hw_value = 104, .center_freq = 5520, .max_power = 25 },
4683 { .hw_value = 108, .center_freq = 5540, .max_power = 25 },
4684 { .hw_value = 112, .center_freq = 5560, .max_power = 25 },
4685 { .hw_value = 116, .center_freq = 5580, .max_power = 25 },
4686 { .hw_value = 120, .center_freq = 5600, .max_power = 25 },
4687 { .hw_value = 124, .center_freq = 5620, .max_power = 25 },
4688 { .hw_value = 128, .center_freq = 5640, .max_power = 25 },
4689 { .hw_value = 132, .center_freq = 5660, .max_power = 25 },
4690 { .hw_value = 136, .center_freq = 5680, .max_power = 25 },
4691 { .hw_value = 140, .center_freq = 5700, .max_power = 25 },
4692 { .hw_value = 149, .center_freq = 5745, .max_power = 25 },
4693 { .hw_value = 153, .center_freq = 5765, .max_power = 25 },
4694 { .hw_value = 157, .center_freq = 5785, .max_power = 25 },
4695 { .hw_value = 161, .center_freq = 5805, .max_power = 25 },
4696 { .hw_value = 165, .center_freq = 5825, .max_power = 25 },
4697 };
4698
4699 static struct ieee80211_supported_band wl1271_band_5ghz = {
4700 .channels = wl1271_channels_5ghz,
4701 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
4702 .bitrates = wl1271_rates_5ghz,
4703 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
4704 .ht_cap = WL12XX_HT_CAP,
4705 };
4706
4707 static const struct ieee80211_ops wl1271_ops = {
4708 .start = wl1271_op_start,
4709 .stop = wl1271_op_stop,
4710 .add_interface = wl1271_op_add_interface,
4711 .remove_interface = wl1271_op_remove_interface,
4712 .change_interface = wl12xx_op_change_interface,
4713 #ifdef CONFIG_PM
4714 .suspend = wl1271_op_suspend,
4715 .resume = wl1271_op_resume,
4716 #endif
4717 .config = wl1271_op_config,
4718 .prepare_multicast = wl1271_op_prepare_multicast,
4719 .configure_filter = wl1271_op_configure_filter,
4720 .tx = wl1271_op_tx,
4721 .set_key = wl1271_op_set_key,
4722 .hw_scan = wl1271_op_hw_scan,
4723 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
4724 .sched_scan_start = wl1271_op_sched_scan_start,
4725 .sched_scan_stop = wl1271_op_sched_scan_stop,
4726 .bss_info_changed = wl1271_op_bss_info_changed,
4727 .set_frag_threshold = wl1271_op_set_frag_threshold,
4728 .set_rts_threshold = wl1271_op_set_rts_threshold,
4729 .conf_tx = wl1271_op_conf_tx,
4730 .get_tsf = wl1271_op_get_tsf,
4731 .get_survey = wl1271_op_get_survey,
4732 .sta_state = wl12xx_op_sta_state,
4733 .ampdu_action = wl1271_op_ampdu_action,
4734 .tx_frames_pending = wl1271_tx_frames_pending,
4735 .set_bitrate_mask = wl12xx_set_bitrate_mask,
4736 .channel_switch = wl12xx_op_channel_switch,
4737 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
4738 };
4739
4740
4741 u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum ieee80211_band band)
4742 {
4743 u8 idx;
4744
4745 BUG_ON(band >= 2);
4746
4747 if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
4748 wl1271_error("Illegal RX rate from HW: %d", rate);
4749 return 0;
4750 }
4751
4752 idx = wl->band_rate_to_idx[band][rate];
4753 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
4754 wl1271_error("Unsupported RX rate from HW: %d", rate);
4755 return 0;
4756 }
4757
4758 return idx;
4759 }
4760
4761 static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
4762 struct device_attribute *attr,
4763 char *buf)
4764 {
4765 struct wl1271 *wl = dev_get_drvdata(dev);
4766 ssize_t len;
4767
4768 len = PAGE_SIZE;
4769
4770 mutex_lock(&wl->mutex);
4771 len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
4772 wl->sg_enabled);
4773 mutex_unlock(&wl->mutex);
4774
4775 return len;
4776
4777 }
4778
4779 static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
4780 struct device_attribute *attr,
4781 const char *buf, size_t count)
4782 {
4783 struct wl1271 *wl = dev_get_drvdata(dev);
4784 unsigned long res;
4785 int ret;
4786
4787 ret = kstrtoul(buf, 10, &res);
4788 if (ret < 0) {
4789 wl1271_warning("incorrect value written to bt_coex_mode");
4790 return count;
4791 }
4792
4793 mutex_lock(&wl->mutex);
4794
4795 res = !!res;
4796
4797 if (res == wl->sg_enabled)
4798 goto out;
4799
4800 wl->sg_enabled = res;
4801
4802 if (wl->state == WL1271_STATE_OFF)
4803 goto out;
4804
4805 ret = wl1271_ps_elp_wakeup(wl);
4806 if (ret < 0)
4807 goto out;
4808
4809 wl1271_acx_sg_enable(wl, wl->sg_enabled);
4810 wl1271_ps_elp_sleep(wl);
4811
4812 out:
4813 mutex_unlock(&wl->mutex);
4814 return count;
4815 }
4816
4817 static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
4818 wl1271_sysfs_show_bt_coex_state,
4819 wl1271_sysfs_store_bt_coex_state);
4820
4821 static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
4822 struct device_attribute *attr,
4823 char *buf)
4824 {
4825 struct wl1271 *wl = dev_get_drvdata(dev);
4826 ssize_t len;
4827
4828 len = PAGE_SIZE;
4829
4830 mutex_lock(&wl->mutex);
4831 if (wl->hw_pg_ver >= 0)
4832 len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
4833 else
4834 len = snprintf(buf, len, "n/a\n");
4835 mutex_unlock(&wl->mutex);
4836
4837 return len;
4838 }
4839
4840 static DEVICE_ATTR(hw_pg_ver, S_IRUGO,
4841 wl1271_sysfs_show_hw_pg_ver, NULL);
4842
4843 static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
4844 struct bin_attribute *bin_attr,
4845 char *buffer, loff_t pos, size_t count)
4846 {
4847 struct device *dev = container_of(kobj, struct device, kobj);
4848 struct wl1271 *wl = dev_get_drvdata(dev);
4849 ssize_t len;
4850 int ret;
4851
4852 ret = mutex_lock_interruptible(&wl->mutex);
4853 if (ret < 0)
4854 return -ERESTARTSYS;
4855
4856 /* Let only one thread read the log at a time, blocking others */
4857 while (wl->fwlog_size == 0) {
4858 DEFINE_WAIT(wait);
4859
4860 prepare_to_wait_exclusive(&wl->fwlog_waitq,
4861 &wait,
4862 TASK_INTERRUPTIBLE);
4863
4864 if (wl->fwlog_size != 0) {
4865 finish_wait(&wl->fwlog_waitq, &wait);
4866 break;
4867 }
4868
4869 mutex_unlock(&wl->mutex);
4870
4871 schedule();
4872 finish_wait(&wl->fwlog_waitq, &wait);
4873
4874 if (signal_pending(current))
4875 return -ERESTARTSYS;
4876
4877 ret = mutex_lock_interruptible(&wl->mutex);
4878 if (ret < 0)
4879 return -ERESTARTSYS;
4880 }
4881
4882 /* Check if the fwlog is still valid */
4883 if (wl->fwlog_size < 0) {
4884 mutex_unlock(&wl->mutex);
4885 return 0;
4886 }
4887
4888 /* Seeking is not supported - old logs are not kept. Disregard pos. */
4889 len = min(count, (size_t)wl->fwlog_size);
4890 wl->fwlog_size -= len;
4891 memcpy(buffer, wl->fwlog, len);
4892
4893 /* Make room for new messages */
4894 memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
4895
4896 mutex_unlock(&wl->mutex);
4897
4898 return len;
4899 }
4900
4901 static struct bin_attribute fwlog_attr = {
4902 .attr = {.name = "fwlog", .mode = S_IRUSR},
4903 .read = wl1271_sysfs_read_fwlog,
4904 };
4905
4906 static void wl12xx_derive_mac_addresses(struct wl1271 *wl,
4907 u32 oui, u32 nic, int n)
4908 {
4909 int i;
4910
4911 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x, n %d",
4912 oui, nic, n);
4913
4914 if (nic + n - 1 > 0xffffff)
4915 wl1271_warning("NIC part of the MAC address wraps around!");
4916
4917 for (i = 0; i < n; i++) {
4918 wl->addresses[i].addr[0] = (u8)(oui >> 16);
4919 wl->addresses[i].addr[1] = (u8)(oui >> 8);
4920 wl->addresses[i].addr[2] = (u8) oui;
4921 wl->addresses[i].addr[3] = (u8)(nic >> 16);
4922 wl->addresses[i].addr[4] = (u8)(nic >> 8);
4923 wl->addresses[i].addr[5] = (u8) nic;
4924 nic++;
4925 }
4926
4927 wl->hw->wiphy->n_addresses = n;
4928 wl->hw->wiphy->addresses = wl->addresses;
4929 }
4930
4931 static int wl12xx_get_hw_info(struct wl1271 *wl)
4932 {
4933 int ret;
4934
4935 ret = wl12xx_set_power_on(wl);
4936 if (ret < 0)
4937 goto out;
4938
4939 wl->chip.id = wlcore_read_reg(wl, REG_CHIP_ID_B);
4940
4941 wl->fuse_oui_addr = 0;
4942 wl->fuse_nic_addr = 0;
4943
4944 wl->hw_pg_ver = wl->ops->get_pg_ver(wl);
4945
4946 if (wl->ops->get_mac)
4947 wl->ops->get_mac(wl);
4948
4949 wl1271_power_off(wl);
4950 out:
4951 return ret;
4952 }
4953
4954 static int wl1271_register_hw(struct wl1271 *wl)
4955 {
4956 int ret;
4957 u32 oui_addr = 0, nic_addr = 0;
4958
4959 if (wl->mac80211_registered)
4960 return 0;
4961
4962 ret = wl12xx_get_hw_info(wl);
4963 if (ret < 0) {
4964 wl1271_error("couldn't get hw info");
4965 goto out;
4966 }
4967
4968 ret = wl1271_fetch_nvs(wl);
4969 if (ret == 0) {
4970 /* NOTE: The wl->nvs->nvs element must be first, in
4971 * order to simplify the casting, we assume it is at
4972 * the beginning of the wl->nvs structure.
4973 */
4974 u8 *nvs_ptr = (u8 *)wl->nvs;
4975
4976 oui_addr =
4977 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
4978 nic_addr =
4979 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
4980 }
4981
4982 /* if the MAC address is zeroed in the NVS derive from fuse */
4983 if (oui_addr == 0 && nic_addr == 0) {
4984 oui_addr = wl->fuse_oui_addr;
4985 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
4986 nic_addr = wl->fuse_nic_addr + 1;
4987 }
4988
4989 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr, 2);
4990
4991 ret = ieee80211_register_hw(wl->hw);
4992 if (ret < 0) {
4993 wl1271_error("unable to register mac80211 hw: %d", ret);
4994 goto out;
4995 }
4996
4997 wl->mac80211_registered = true;
4998
4999 wl1271_debugfs_init(wl);
5000
5001 wl1271_notice("loaded");
5002
5003 out:
5004 return ret;
5005 }
5006
5007 static void wl1271_unregister_hw(struct wl1271 *wl)
5008 {
5009 if (wl->plt)
5010 wl1271_plt_stop(wl);
5011
5012 ieee80211_unregister_hw(wl->hw);
5013 wl->mac80211_registered = false;
5014
5015 }
5016
5017 static int wl1271_init_ieee80211(struct wl1271 *wl)
5018 {
5019 static const u32 cipher_suites[] = {
5020 WLAN_CIPHER_SUITE_WEP40,
5021 WLAN_CIPHER_SUITE_WEP104,
5022 WLAN_CIPHER_SUITE_TKIP,
5023 WLAN_CIPHER_SUITE_CCMP,
5024 WL1271_CIPHER_SUITE_GEM,
5025 };
5026
5027 /* The tx descriptor buffer and the TKIP space. */
5028 wl->hw->extra_tx_headroom = WL1271_EXTRA_SPACE_TKIP +
5029 sizeof(struct wl1271_tx_hw_descr);
5030
5031 /* unit us */
5032 /* FIXME: find a proper value */
5033 wl->hw->channel_change_time = 10000;
5034 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
5035
5036 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5037 IEEE80211_HW_SUPPORTS_PS |
5038 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5039 IEEE80211_HW_SUPPORTS_UAPSD |
5040 IEEE80211_HW_HAS_RATE_CONTROL |
5041 IEEE80211_HW_CONNECTION_MONITOR |
5042 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5043 IEEE80211_HW_SPECTRUM_MGMT |
5044 IEEE80211_HW_AP_LINK_PS |
5045 IEEE80211_HW_AMPDU_AGGREGATION |
5046 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
5047 IEEE80211_HW_SCAN_WHILE_IDLE;
5048
5049 wl->hw->wiphy->cipher_suites = cipher_suites;
5050 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5051
5052 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
5053 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
5054 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
5055 wl->hw->wiphy->max_scan_ssids = 1;
5056 wl->hw->wiphy->max_sched_scan_ssids = 16;
5057 wl->hw->wiphy->max_match_sets = 16;
5058 /*
5059 * Maximum length of elements in scanning probe request templates
5060 * should be the maximum length possible for a template, without
5061 * the IEEE80211 header of the template
5062 */
5063 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
5064 sizeof(struct ieee80211_header);
5065
5066 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
5067 sizeof(struct ieee80211_header);
5068
5069 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
5070
5071 /* make sure all our channels fit in the scanned_ch bitmask */
5072 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5073 ARRAY_SIZE(wl1271_channels_5ghz) >
5074 WL1271_MAX_CHANNELS);
5075 /*
5076 * We keep local copies of the band structs because we need to
5077 * modify them on a per-device basis.
5078 */
5079 memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5080 sizeof(wl1271_band_2ghz));
5081 memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5082 sizeof(wl1271_band_5ghz));
5083
5084 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5085 &wl->bands[IEEE80211_BAND_2GHZ];
5086 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5087 &wl->bands[IEEE80211_BAND_5GHZ];
5088
5089 wl->hw->queues = 4;
5090 wl->hw->max_rates = 1;
5091
5092 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5093
5094 /* the FW answers probe-requests in AP-mode */
5095 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5096 wl->hw->wiphy->probe_resp_offload =
5097 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5098 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5099 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5100
5101 SET_IEEE80211_DEV(wl->hw, wl->dev);
5102
5103 wl->hw->sta_data_size = sizeof(struct wl1271_station);
5104 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
5105
5106 wl->hw->max_rx_aggregation_subframes = 8;
5107
5108 return 0;
5109 }
5110
5111 #define WL1271_DEFAULT_CHANNEL 0
5112
5113 struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size)
5114 {
5115 struct ieee80211_hw *hw;
5116 struct wl1271 *wl;
5117 int i, j, ret;
5118 unsigned int order;
5119
5120 BUILD_BUG_ON(AP_MAX_STATIONS > WL12XX_MAX_LINKS);
5121
5122 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5123 if (!hw) {
5124 wl1271_error("could not alloc ieee80211_hw");
5125 ret = -ENOMEM;
5126 goto err_hw_alloc;
5127 }
5128
5129 wl = hw->priv;
5130 memset(wl, 0, sizeof(*wl));
5131
5132 wl->priv = kzalloc(priv_size, GFP_KERNEL);
5133 if (!wl->priv) {
5134 wl1271_error("could not alloc wl priv");
5135 ret = -ENOMEM;
5136 goto err_priv_alloc;
5137 }
5138
5139 INIT_LIST_HEAD(&wl->wlvif_list);
5140
5141 wl->hw = hw;
5142
5143 for (i = 0; i < NUM_TX_QUEUES; i++)
5144 for (j = 0; j < WL12XX_MAX_LINKS; j++)
5145 skb_queue_head_init(&wl->links[j].tx_queue[i]);
5146
5147 skb_queue_head_init(&wl->deferred_rx_queue);
5148 skb_queue_head_init(&wl->deferred_tx_queue);
5149
5150 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
5151 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
5152 INIT_WORK(&wl->tx_work, wl1271_tx_work);
5153 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5154 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
5155 INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
5156
5157 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5158 if (!wl->freezable_wq) {
5159 ret = -ENOMEM;
5160 goto err_hw;
5161 }
5162
5163 wl->channel = WL1271_DEFAULT_CHANNEL;
5164 wl->rx_counter = 0;
5165 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
5166 wl->band = IEEE80211_BAND_2GHZ;
5167 wl->flags = 0;
5168 wl->sg_enabled = true;
5169 wl->hw_pg_ver = -1;
5170 wl->ap_ps_map = 0;
5171 wl->ap_fw_ps_map = 0;
5172 wl->quirks = 0;
5173 wl->platform_quirks = 0;
5174 wl->sched_scanning = false;
5175 wl->system_hlid = WL12XX_SYSTEM_HLID;
5176 wl->active_sta_count = 0;
5177 wl->fwlog_size = 0;
5178 init_waitqueue_head(&wl->fwlog_waitq);
5179
5180 /* The system link is always allocated */
5181 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5182
5183 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
5184 for (i = 0; i < wl->num_tx_desc; i++)
5185 wl->tx_frames[i] = NULL;
5186
5187 spin_lock_init(&wl->wl_lock);
5188
5189 wl->state = WL1271_STATE_OFF;
5190 wl->fw_type = WL12XX_FW_TYPE_NONE;
5191 mutex_init(&wl->mutex);
5192
5193 /* Apply default driver configuration. */
5194 wl1271_conf_init(wl);
5195
5196 order = get_order(WL1271_AGGR_BUFFER_SIZE);
5197 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5198 if (!wl->aggr_buf) {
5199 ret = -ENOMEM;
5200 goto err_wq;
5201 }
5202
5203 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5204 if (!wl->dummy_packet) {
5205 ret = -ENOMEM;
5206 goto err_aggr;
5207 }
5208
5209 /* Allocate one page for the FW log */
5210 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5211 if (!wl->fwlog) {
5212 ret = -ENOMEM;
5213 goto err_dummy_packet;
5214 }
5215
5216 wl->mbox = kmalloc(sizeof(*wl->mbox), GFP_DMA);
5217 if (!wl->mbox) {
5218 ret = -ENOMEM;
5219 goto err_fwlog;
5220 }
5221
5222 return hw;
5223
5224 err_fwlog:
5225 free_page((unsigned long)wl->fwlog);
5226
5227 err_dummy_packet:
5228 dev_kfree_skb(wl->dummy_packet);
5229
5230 err_aggr:
5231 free_pages((unsigned long)wl->aggr_buf, order);
5232
5233 err_wq:
5234 destroy_workqueue(wl->freezable_wq);
5235
5236 err_hw:
5237 wl1271_debugfs_exit(wl);
5238 kfree(wl->priv);
5239
5240 err_priv_alloc:
5241 ieee80211_free_hw(hw);
5242
5243 err_hw_alloc:
5244
5245 return ERR_PTR(ret);
5246 }
5247 EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
5248
5249 int wlcore_free_hw(struct wl1271 *wl)
5250 {
5251 /* Unblock any fwlog readers */
5252 mutex_lock(&wl->mutex);
5253 wl->fwlog_size = -1;
5254 wake_up_interruptible_all(&wl->fwlog_waitq);
5255 mutex_unlock(&wl->mutex);
5256
5257 device_remove_bin_file(wl->dev, &fwlog_attr);
5258
5259 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5260
5261 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5262 free_page((unsigned long)wl->fwlog);
5263 dev_kfree_skb(wl->dummy_packet);
5264 free_pages((unsigned long)wl->aggr_buf,
5265 get_order(WL1271_AGGR_BUFFER_SIZE));
5266
5267 wl1271_debugfs_exit(wl);
5268
5269 vfree(wl->fw);
5270 wl->fw = NULL;
5271 wl->fw_type = WL12XX_FW_TYPE_NONE;
5272 kfree(wl->nvs);
5273 wl->nvs = NULL;
5274
5275 kfree(wl->fw_status);
5276 kfree(wl->tx_res_if);
5277 destroy_workqueue(wl->freezable_wq);
5278
5279 kfree(wl->priv);
5280 ieee80211_free_hw(wl->hw);
5281
5282 return 0;
5283 }
5284 EXPORT_SYMBOL_GPL(wlcore_free_hw);
5285
5286 static irqreturn_t wl12xx_hardirq(int irq, void *cookie)
5287 {
5288 struct wl1271 *wl = cookie;
5289 unsigned long flags;
5290
5291 wl1271_debug(DEBUG_IRQ, "IRQ");
5292
5293 /* complete the ELP completion */
5294 spin_lock_irqsave(&wl->wl_lock, flags);
5295 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
5296 if (wl->elp_compl) {
5297 complete(wl->elp_compl);
5298 wl->elp_compl = NULL;
5299 }
5300
5301 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
5302 /* don't enqueue a work right now. mark it as pending */
5303 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
5304 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
5305 disable_irq_nosync(wl->irq);
5306 pm_wakeup_event(wl->dev, 0);
5307 spin_unlock_irqrestore(&wl->wl_lock, flags);
5308 return IRQ_HANDLED;
5309 }
5310 spin_unlock_irqrestore(&wl->wl_lock, flags);
5311
5312 return IRQ_WAKE_THREAD;
5313 }
5314
5315 int __devinit wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
5316 {
5317 struct wl12xx_platform_data *pdata = pdev->dev.platform_data;
5318 unsigned long irqflags;
5319 int ret;
5320
5321 if (!wl->ops || !wl->ptable) {
5322 ret = -EINVAL;
5323 goto out_free_hw;
5324 }
5325
5326 BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
5327
5328 wl->irq = platform_get_irq(pdev, 0);
5329 wl->ref_clock = pdata->board_ref_clock;
5330 wl->tcxo_clock = pdata->board_tcxo_clock;
5331 wl->platform_quirks = pdata->platform_quirks;
5332 wl->set_power = pdata->set_power;
5333 wl->dev = &pdev->dev;
5334 wl->if_ops = pdata->ops;
5335
5336 platform_set_drvdata(pdev, wl);
5337
5338 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
5339 irqflags = IRQF_TRIGGER_RISING;
5340 else
5341 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
5342
5343 ret = request_threaded_irq(wl->irq, wl12xx_hardirq, wl1271_irq,
5344 irqflags,
5345 pdev->name, wl);
5346 if (ret < 0) {
5347 wl1271_error("request_irq() failed: %d", ret);
5348 goto out_free_hw;
5349 }
5350
5351 ret = enable_irq_wake(wl->irq);
5352 if (!ret) {
5353 wl->irq_wake_enabled = true;
5354 device_init_wakeup(wl->dev, 1);
5355 if (pdata->pwr_in_suspend)
5356 wl->hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
5357
5358 }
5359 disable_irq(wl->irq);
5360
5361 ret = wl1271_init_ieee80211(wl);
5362 if (ret)
5363 goto out_irq;
5364
5365 ret = wl1271_register_hw(wl);
5366 if (ret)
5367 goto out_irq;
5368
5369 /* Create sysfs file to control bt coex state */
5370 ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
5371 if (ret < 0) {
5372 wl1271_error("failed to create sysfs file bt_coex_state");
5373 goto out_irq;
5374 }
5375
5376 /* Create sysfs file to get HW PG version */
5377 ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
5378 if (ret < 0) {
5379 wl1271_error("failed to create sysfs file hw_pg_ver");
5380 goto out_bt_coex_state;
5381 }
5382
5383 /* Create sysfs file for the FW log */
5384 ret = device_create_bin_file(wl->dev, &fwlog_attr);
5385 if (ret < 0) {
5386 wl1271_error("failed to create sysfs file fwlog");
5387 goto out_hw_pg_ver;
5388 }
5389
5390 goto out;
5391
5392 out_hw_pg_ver:
5393 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5394
5395 out_bt_coex_state:
5396 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5397
5398 out_irq:
5399 free_irq(wl->irq, wl);
5400
5401 out_free_hw:
5402 wlcore_free_hw(wl);
5403
5404 out:
5405 return ret;
5406 }
5407 EXPORT_SYMBOL_GPL(wlcore_probe);
5408
5409 int __devexit wlcore_remove(struct platform_device *pdev)
5410 {
5411 struct wl1271 *wl = platform_get_drvdata(pdev);
5412
5413 if (wl->irq_wake_enabled) {
5414 device_init_wakeup(wl->dev, 0);
5415 disable_irq_wake(wl->irq);
5416 }
5417 wl1271_unregister_hw(wl);
5418 free_irq(wl->irq, wl);
5419 wlcore_free_hw(wl);
5420
5421 return 0;
5422 }
5423 EXPORT_SYMBOL_GPL(wlcore_remove);
5424
5425 u32 wl12xx_debug_level = DEBUG_NONE;
5426 EXPORT_SYMBOL_GPL(wl12xx_debug_level);
5427 module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
5428 MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
5429
5430 module_param_named(fwlog, fwlog_param, charp, 0);
5431 MODULE_PARM_DESC(fwlog,
5432 "FW logger options: continuous, ondemand, dbgpins or disable");
5433
5434 module_param(bug_on_recovery, bool, S_IRUSR | S_IWUSR);
5435 MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
5436
5437 MODULE_LICENSE("GPL");
5438 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
5439 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
This page took 0.143259 seconds and 5 git commands to generate.