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