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