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