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