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