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