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