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