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