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