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