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