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