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