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