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