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