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