]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/ti/wlcore/main.c
wlcore: use separate HW queue for each AC in each vif
[mirror_ubuntu-artful-kernel.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{
9a100968 337 bool fw_ps, single_link;
b622d992 338
b622d992 339 fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
9a100968 340 single_link = (wl->active_link_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.
9a100968
AN
351 * Make an exception if this is the only connected link. In this
352 * case FW-memory congestion is less of a problem.
da03209e 353 */
9a100968 354 else if (!single_link && 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)) ||
1c33db78
AN
1203 (wlcore_is_queue_stopped(wl, wlvif, q) &&
1204 !wlcore_is_queue_stopped_by_reason(wl, wlvif, q,
66396114 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 */
1c33db78
AN
1222 if (wlvif->tx_queue_count[q] >= WL1271_TX_QUEUE_HIGH_WATERMARK &&
1223 !wlcore_is_queue_stopped_by_reason(wl, wlvif, q,
8cdc44aa 1224 WLCORE_QUEUE_STOP_REASON_WATERMARK)) {
04b4d69c 1225 wl1271_debug(DEBUG_TX, "op_tx: stopping queues for q %d", q);
1c33db78 1226 wlcore_stop_queue_locked(wl, wlvif, q,
66396114 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 1892 wl->active_sta_count = 0;
9a100968 1893 wl->active_link_count = 0;
baf6277a
EP
1894
1895 /* The system link is always allocated */
9ebcb232
AN
1896 wl->links[WL12XX_SYSTEM_HLID].allocated_pkts = 0;
1897 wl->links[WL12XX_SYSTEM_HLID].prev_freed_pkts = 0;
baf6277a
EP
1898 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
1899
1900 /*
1901 * this is performed after the cancel_work calls and the associated
1902 * mutex_lock, so that wl1271_op_add_interface does not accidentally
1903 * get executed before all these vars have been reset.
1904 */
1905 wl->flags = 0;
1906
1907 wl->tx_blocks_freed = 0;
1908
1909 for (i = 0; i < NUM_TX_QUEUES; i++) {
1910 wl->tx_pkts_freed[i] = 0;
1911 wl->tx_allocated_pkts[i] = 0;
1912 }
1913
1914 wl1271_debugfs_reset(wl);
1915
0afd04e5
AN
1916 kfree(wl->fw_status_1);
1917 wl->fw_status_1 = NULL;
1918 wl->fw_status_2 = NULL;
baf6277a
EP
1919 kfree(wl->tx_res_if);
1920 wl->tx_res_if = NULL;
1921 kfree(wl->target_mem_map);
1922 wl->target_mem_map = NULL;
6b70e7eb
VG
1923
1924 /*
1925 * FW channels must be re-calibrated after recovery,
1926 * clear the last Reg-Domain channel configuration.
1927 */
1928 memset(wl->reg_ch_conf_last, 0, sizeof(wl->reg_ch_conf_last));
c24ec83b
IY
1929}
1930
1931static void wlcore_op_stop(struct ieee80211_hw *hw)
1932{
1933 struct wl1271 *wl = hw->priv;
1934
1935 wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
1936
1937 mutex_lock(&wl->mutex);
1938
1939 wlcore_op_stop_locked(wl);
baf6277a
EP
1940
1941 mutex_unlock(&wl->mutex);
1b72aecd
JO
1942}
1943
c50a2825
EP
1944static void wlcore_channel_switch_work(struct work_struct *work)
1945{
1946 struct delayed_work *dwork;
1947 struct wl1271 *wl;
1948 struct ieee80211_vif *vif;
1949 struct wl12xx_vif *wlvif;
1950 int ret;
1951
1952 dwork = container_of(work, struct delayed_work, work);
1953 wlvif = container_of(dwork, struct wl12xx_vif, channel_switch_work);
1954 wl = wlvif->wl;
1955
1956 wl1271_info("channel switch failed (role_id: %d).", wlvif->role_id);
1957
1958 mutex_lock(&wl->mutex);
1959
1960 if (unlikely(wl->state != WLCORE_STATE_ON))
1961 goto out;
1962
1963 /* check the channel switch is still ongoing */
1964 if (!test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags))
1965 goto out;
1966
1967 vif = wl12xx_wlvif_to_vif(wlvif);
1968 ieee80211_chswitch_done(vif, false);
1969
1970 ret = wl1271_ps_elp_wakeup(wl);
1971 if (ret < 0)
1972 goto out;
1973
1974 wl12xx_cmd_stop_channel_switch(wl, wlvif);
1975
1976 wl1271_ps_elp_sleep(wl);
1977out:
1978 mutex_unlock(&wl->mutex);
1979}
1980
1981static void wlcore_connection_loss_work(struct work_struct *work)
1982{
1983 struct delayed_work *dwork;
1984 struct wl1271 *wl;
1985 struct ieee80211_vif *vif;
1986 struct wl12xx_vif *wlvif;
1987
1988 dwork = container_of(work, struct delayed_work, work);
1989 wlvif = container_of(dwork, struct wl12xx_vif, connection_loss_work);
1990 wl = wlvif->wl;
1991
1992 wl1271_info("Connection loss work (role_id: %d).", wlvif->role_id);
1993
1994 mutex_lock(&wl->mutex);
1995
1996 if (unlikely(wl->state != WLCORE_STATE_ON))
1997 goto out;
1998
1999 /* Call mac80211 connection loss */
2000 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2001 goto out;
2002
2003 vif = wl12xx_wlvif_to_vif(wlvif);
2004 ieee80211_connection_loss(vif);
2005out:
2006 mutex_unlock(&wl->mutex);
2007}
2008
e5a359f8
EP
2009static int wl12xx_allocate_rate_policy(struct wl1271 *wl, u8 *idx)
2010{
2011 u8 policy = find_first_zero_bit(wl->rate_policies_map,
2012 WL12XX_MAX_RATE_POLICIES);
2013 if (policy >= WL12XX_MAX_RATE_POLICIES)
2014 return -EBUSY;
2015
2016 __set_bit(policy, wl->rate_policies_map);
2017 *idx = policy;
2018 return 0;
2019}
2020
2021static void wl12xx_free_rate_policy(struct wl1271 *wl, u8 *idx)
2022{
2023 if (WARN_ON(*idx >= WL12XX_MAX_RATE_POLICIES))
2024 return;
2025
2026 __clear_bit(*idx, wl->rate_policies_map);
2027 *idx = WL12XX_MAX_RATE_POLICIES;
2028}
2029
001e39a8
EP
2030static int wlcore_allocate_klv_template(struct wl1271 *wl, u8 *idx)
2031{
2032 u8 policy = find_first_zero_bit(wl->klv_templates_map,
2033 WLCORE_MAX_KLV_TEMPLATES);
2034 if (policy >= WLCORE_MAX_KLV_TEMPLATES)
2035 return -EBUSY;
2036
2037 __set_bit(policy, wl->klv_templates_map);
2038 *idx = policy;
2039 return 0;
2040}
2041
2042static void wlcore_free_klv_template(struct wl1271 *wl, u8 *idx)
2043{
2044 if (WARN_ON(*idx >= WLCORE_MAX_KLV_TEMPLATES))
2045 return;
2046
2047 __clear_bit(*idx, wl->klv_templates_map);
2048 *idx = WLCORE_MAX_KLV_TEMPLATES;
2049}
2050
536129c8 2051static u8 wl12xx_get_role_type(struct wl1271 *wl, struct wl12xx_vif *wlvif)
b78b47eb 2052{
536129c8 2053 switch (wlvif->bss_type) {
b78b47eb 2054 case BSS_TYPE_AP_BSS:
fb0e707c 2055 if (wlvif->p2p)
045c745f
EP
2056 return WL1271_ROLE_P2P_GO;
2057 else
2058 return WL1271_ROLE_AP;
b78b47eb
EP
2059
2060 case BSS_TYPE_STA_BSS:
fb0e707c 2061 if (wlvif->p2p)
045c745f
EP
2062 return WL1271_ROLE_P2P_CL;
2063 else
2064 return WL1271_ROLE_STA;
b78b47eb 2065
227e81e1
EP
2066 case BSS_TYPE_IBSS:
2067 return WL1271_ROLE_IBSS;
2068
b78b47eb 2069 default:
536129c8 2070 wl1271_error("invalid bss_type: %d", wlvif->bss_type);
b78b47eb
EP
2071 }
2072 return WL12XX_INVALID_ROLE_TYPE;
2073}
2074
83587505 2075static int wl12xx_init_vif_data(struct wl1271 *wl, struct ieee80211_vif *vif)
87fbcb0f 2076{
e936bbe0 2077 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e5a359f8 2078 int i;
e936bbe0 2079
48e93e40
EP
2080 /* clear everything but the persistent data */
2081 memset(wlvif, 0, offsetof(struct wl12xx_vif, persistent));
e936bbe0
EP
2082
2083 switch (ieee80211_vif_type_p2p(vif)) {
2084 case NL80211_IFTYPE_P2P_CLIENT:
2085 wlvif->p2p = 1;
2086 /* fall-through */
2087 case NL80211_IFTYPE_STATION:
2088 wlvif->bss_type = BSS_TYPE_STA_BSS;
2089 break;
2090 case NL80211_IFTYPE_ADHOC:
2091 wlvif->bss_type = BSS_TYPE_IBSS;
2092 break;
2093 case NL80211_IFTYPE_P2P_GO:
2094 wlvif->p2p = 1;
2095 /* fall-through */
2096 case NL80211_IFTYPE_AP:
2097 wlvif->bss_type = BSS_TYPE_AP_BSS;
2098 break;
2099 default:
2100 wlvif->bss_type = MAX_BSS_TYPE;
2101 return -EOPNOTSUPP;
2102 }
2103
0603d891 2104 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
7edebf56 2105 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
afaf8bdb 2106 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
a8ab39a4 2107
e936bbe0
EP
2108 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2109 wlvif->bss_type == BSS_TYPE_IBSS) {
2110 /* init sta/ibss data */
2111 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
e5a359f8
EP
2112 wl12xx_allocate_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2113 wl12xx_allocate_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2114 wl12xx_allocate_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
001e39a8 2115 wlcore_allocate_klv_template(wl, &wlvif->sta.klv_template_id);
15e05bc0
LC
2116 wlvif->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
2117 wlvif->basic_rate = CONF_TX_RATE_MASK_BASIC;
2118 wlvif->rate_set = CONF_TX_RATE_MASK_BASIC;
e936bbe0
EP
2119 } else {
2120 /* init ap data */
2121 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2122 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
e5a359f8
EP
2123 wl12xx_allocate_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2124 wl12xx_allocate_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2125 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2126 wl12xx_allocate_rate_policy(wl,
2127 &wlvif->ap.ucast_rate_idx[i]);
42ec1f82 2128 wlvif->basic_rate_set = CONF_TX_ENABLED_RATES;
15e05bc0
LC
2129 /*
2130 * TODO: check if basic_rate shouldn't be
2131 * wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
2132 * instead (the same thing for STA above).
2133 */
42ec1f82 2134 wlvif->basic_rate = CONF_TX_ENABLED_RATES;
15e05bc0 2135 /* TODO: this seems to be used only for STA, check it */
42ec1f82 2136 wlvif->rate_set = CONF_TX_ENABLED_RATES;
e936bbe0 2137 }
a8ab39a4 2138
83587505
EP
2139 wlvif->bitrate_masks[IEEE80211_BAND_2GHZ] = wl->conf.tx.basic_rate;
2140 wlvif->bitrate_masks[IEEE80211_BAND_5GHZ] = wl->conf.tx.basic_rate_5;
6a899796
EP
2141 wlvif->beacon_int = WL1271_DEFAULT_BEACON_INT;
2142
1b92f15e
EP
2143 /*
2144 * mac80211 configures some values globally, while we treat them
2145 * per-interface. thus, on init, we have to copy them from wl
2146 */
2147 wlvif->band = wl->band;
61f845f4 2148 wlvif->channel = wl->channel;
6bd65029 2149 wlvif->power_level = wl->power_level;
83d08d3f 2150 wlvif->channel_type = wl->channel_type;
1b92f15e 2151
9eb599e9
EP
2152 INIT_WORK(&wlvif->rx_streaming_enable_work,
2153 wl1271_rx_streaming_enable_work);
2154 INIT_WORK(&wlvif->rx_streaming_disable_work,
2155 wl1271_rx_streaming_disable_work);
c50a2825
EP
2156 INIT_DELAYED_WORK(&wlvif->channel_switch_work,
2157 wlcore_channel_switch_work);
2158 INIT_DELAYED_WORK(&wlvif->connection_loss_work,
2159 wlcore_connection_loss_work);
87627214 2160 INIT_LIST_HEAD(&wlvif->list);
252efa4f 2161
9eb599e9
EP
2162 setup_timer(&wlvif->rx_streaming_timer, wl1271_rx_streaming_timer,
2163 (unsigned long) wlvif);
e936bbe0 2164 return 0;
87fbcb0f
EP
2165}
2166
1d095475 2167static bool wl12xx_init_fw(struct wl1271 *wl)
f5fc0f86 2168{
9ccd9217 2169 int retries = WL1271_BOOT_RETRIES;
71125abd 2170 bool booted = false;
1d095475
EP
2171 struct wiphy *wiphy = wl->hw->wiphy;
2172 int ret;
f5fc0f86 2173
9ccd9217
JO
2174 while (retries) {
2175 retries--;
3fcdab70 2176 ret = wl12xx_chip_wakeup(wl, false);
9ccd9217
JO
2177 if (ret < 0)
2178 goto power_off;
f5fc0f86 2179
dd5512eb 2180 ret = wl->ops->boot(wl);
9ccd9217
JO
2181 if (ret < 0)
2182 goto power_off;
f5fc0f86 2183
92c77c73
EP
2184 ret = wl1271_hw_init(wl);
2185 if (ret < 0)
2186 goto irq_disable;
2187
71125abd
EP
2188 booted = true;
2189 break;
eb5b28d0 2190
9ccd9217 2191irq_disable:
9ccd9217
JO
2192 mutex_unlock(&wl->mutex);
2193 /* Unlocking the mutex in the middle of handling is
2194 inherently unsafe. In this case we deem it safe to do,
2195 because we need to let any possibly pending IRQ out of
4cc53383 2196 the system (and while we are WLCORE_STATE_OFF the IRQ
9ccd9217
JO
2197 work function will not do anything.) Also, any other
2198 possible concurrent operations will fail due to the
2199 current state, hence the wl1271 struct should be safe. */
dd5512eb 2200 wlcore_disable_interrupts(wl);
a620865e
IY
2201 wl1271_flush_deferred_work(wl);
2202 cancel_work_sync(&wl->netstack_work);
9ccd9217
JO
2203 mutex_lock(&wl->mutex);
2204power_off:
2205 wl1271_power_off(wl);
2206 }
eb5b28d0 2207
71125abd
EP
2208 if (!booted) {
2209 wl1271_error("firmware boot failed despite %d retries",
2210 WL1271_BOOT_RETRIES);
2211 goto out;
2212 }
2213
4b7fac77 2214 wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
71125abd
EP
2215
2216 /* update hw/fw version info in wiphy struct */
2217 wiphy->hw_version = wl->chip.id;
4b7fac77 2218 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
71125abd
EP
2219 sizeof(wiphy->fw_version));
2220
fb6a6819
LC
2221 /*
2222 * Now we know if 11a is supported (info from the NVS), so disable
2223 * 11a channels if not supported
2224 */
2225 if (!wl->enable_11a)
2226 wiphy->bands[IEEE80211_BAND_5GHZ]->n_channels = 0;
2227
2228 wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
2229 wl->enable_11a ? "" : "not ");
2230
4cc53383 2231 wl->state = WLCORE_STATE_ON;
1d095475
EP
2232out:
2233 return booted;
2234}
2235
92e712da
EP
2236static bool wl12xx_dev_role_started(struct wl12xx_vif *wlvif)
2237{
2238 return wlvif->dev_hlid != WL12XX_INVALID_LINK_ID;
2239}
2240
4549d09c
EP
2241/*
2242 * Check whether a fw switch (i.e. moving from one loaded
2243 * fw to another) is needed. This function is also responsible
2244 * for updating wl->last_vif_count, so it must be called before
2245 * loading a non-plt fw (so the correct fw (single-role/multi-role)
2246 * will be used).
2247 */
2248static bool wl12xx_need_fw_change(struct wl1271 *wl,
2249 struct vif_counter_data vif_counter_data,
2250 bool add)
2251{
2252 enum wl12xx_fw_type current_fw = wl->fw_type;
2253 u8 vif_count = vif_counter_data.counter;
2254
2255 if (test_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags))
2256 return false;
2257
2258 /* increase the vif count if this is a new vif */
2259 if (add && !vif_counter_data.cur_vif_running)
2260 vif_count++;
2261
2262 wl->last_vif_count = vif_count;
2263
2264 /* no need for fw change if the device is OFF */
4cc53383 2265 if (wl->state == WLCORE_STATE_OFF)
4549d09c
EP
2266 return false;
2267
9b1a0a77
EP
2268 /* no need for fw change if a single fw is used */
2269 if (!wl->mr_fw_name)
2270 return false;
2271
4549d09c
EP
2272 if (vif_count > 1 && current_fw == WL12XX_FW_TYPE_NORMAL)
2273 return true;
2274 if (vif_count <= 1 && current_fw == WL12XX_FW_TYPE_MULTI)
2275 return true;
2276
2277 return false;
2278}
2279
3dee4393
EP
2280/*
2281 * Enter "forced psm". Make sure the sta is in psm against the ap,
2282 * to make the fw switch a bit more disconnection-persistent.
2283 */
2284static void wl12xx_force_active_psm(struct wl1271 *wl)
2285{
2286 struct wl12xx_vif *wlvif;
2287
2288 wl12xx_for_each_wlvif_sta(wl, wlvif) {
2289 wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
2290 }
2291}
2292
1c33db78
AN
2293struct wlcore_hw_queue_iter_data {
2294 unsigned long hw_queue_map[BITS_TO_LONGS(WLCORE_NUM_MAC_ADDRESSES)];
2295 /* current vif */
2296 struct ieee80211_vif *vif;
2297 /* is the current vif among those iterated */
2298 bool cur_running;
2299};
2300
2301static void wlcore_hw_queue_iter(void *data, u8 *mac,
2302 struct ieee80211_vif *vif)
2303{
2304 struct wlcore_hw_queue_iter_data *iter_data = data;
2305
2306 if (WARN_ON_ONCE(vif->hw_queue[0] == IEEE80211_INVAL_HW_QUEUE))
2307 return;
2308
2309 if (iter_data->cur_running || vif == iter_data->vif) {
2310 iter_data->cur_running = true;
2311 return;
2312 }
2313
2314 __set_bit(vif->hw_queue[0] / NUM_TX_QUEUES, iter_data->hw_queue_map);
2315}
2316
2317static int wlcore_allocate_hw_queue_base(struct wl1271 *wl,
2318 struct wl12xx_vif *wlvif)
2319{
2320 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2321 struct wlcore_hw_queue_iter_data iter_data = {};
2322 int i, q_base;
2323
2324 iter_data.vif = vif;
2325
2326 /* mark all bits taken by active interfaces */
2327 ieee80211_iterate_active_interfaces_atomic(wl->hw,
2328 IEEE80211_IFACE_ITER_RESUME_ALL,
2329 wlcore_hw_queue_iter, &iter_data);
2330
2331 /* the current vif is already running in mac80211 (resume/recovery) */
2332 if (iter_data.cur_running) {
2333 wlvif->hw_queue_base = vif->hw_queue[0];
2334 wl1271_debug(DEBUG_MAC80211,
2335 "using pre-allocated hw queue base %d",
2336 wlvif->hw_queue_base);
2337
2338 /* interface type might have changed type */
2339 goto adjust_cab_queue;
2340 }
2341
2342 q_base = find_first_zero_bit(iter_data.hw_queue_map,
2343 WLCORE_NUM_MAC_ADDRESSES);
2344 if (q_base >= WLCORE_NUM_MAC_ADDRESSES)
2345 return -EBUSY;
2346
2347 wlvif->hw_queue_base = q_base * NUM_TX_QUEUES;
2348 wl1271_debug(DEBUG_MAC80211, "allocating hw queue base: %d",
2349 wlvif->hw_queue_base);
2350
2351 for (i = 0; i < NUM_TX_QUEUES; i++) {
2352 wl->queue_stop_reasons[wlvif->hw_queue_base + i] = 0;
2353 /* register hw queues in mac80211 */
2354 vif->hw_queue[i] = wlvif->hw_queue_base + i;
2355 }
2356
2357adjust_cab_queue:
2358 /* the last places are reserved for cab queues per interface */
2359 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2360 vif->cab_queue = NUM_TX_QUEUES * WLCORE_NUM_MAC_ADDRESSES +
2361 wlvif->hw_queue_base / NUM_TX_QUEUES;
2362 else
2363 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2364
2365 return 0;
2366}
2367
1d095475
EP
2368static int wl1271_op_add_interface(struct ieee80211_hw *hw,
2369 struct ieee80211_vif *vif)
2370{
2371 struct wl1271 *wl = hw->priv;
2372 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4549d09c 2373 struct vif_counter_data vif_count;
1d095475
EP
2374 int ret = 0;
2375 u8 role_type;
2376 bool booted = false;
2377
ea086359
JB
2378 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
2379 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
c1288b12 2380
1d095475
EP
2381 wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
2382 ieee80211_vif_type_p2p(vif), vif->addr);
2383
4549d09c
EP
2384 wl12xx_get_vif_count(hw, vif, &vif_count);
2385
1d095475 2386 mutex_lock(&wl->mutex);
f750c820
EP
2387 ret = wl1271_ps_elp_wakeup(wl);
2388 if (ret < 0)
2389 goto out_unlock;
2390
1d095475
EP
2391 /*
2392 * in some very corner case HW recovery scenarios its possible to
2393 * get here before __wl1271_op_remove_interface is complete, so
2394 * opt out if that is the case.
2395 */
10c8cd01
EP
2396 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) ||
2397 test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)) {
1d095475
EP
2398 ret = -EBUSY;
2399 goto out;
2400 }
2401
3fcdab70 2402
83587505 2403 ret = wl12xx_init_vif_data(wl, vif);
1d095475
EP
2404 if (ret < 0)
2405 goto out;
2406
2407 wlvif->wl = wl;
2408 role_type = wl12xx_get_role_type(wl, wlvif);
2409 if (role_type == WL12XX_INVALID_ROLE_TYPE) {
2410 ret = -EINVAL;
2411 goto out;
2412 }
2413
1c33db78
AN
2414 ret = wlcore_allocate_hw_queue_base(wl, wlvif);
2415 if (ret < 0)
2416 goto out;
2417
4549d09c 2418 if (wl12xx_need_fw_change(wl, vif_count, true)) {
3dee4393 2419 wl12xx_force_active_psm(wl);
e9ba7152 2420 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
4549d09c
EP
2421 mutex_unlock(&wl->mutex);
2422 wl1271_recovery_work(&wl->recovery_work);
2423 return 0;
2424 }
2425
1d095475
EP
2426 /*
2427 * TODO: after the nvs issue will be solved, move this block
2428 * to start(), and make sure here the driver is ON.
2429 */
4cc53383 2430 if (wl->state == WLCORE_STATE_OFF) {
1d095475
EP
2431 /*
2432 * we still need this in order to configure the fw
2433 * while uploading the nvs
2434 */
5e037e74 2435 memcpy(wl->addresses[0].addr, vif->addr, ETH_ALEN);
1d095475
EP
2436
2437 booted = wl12xx_init_fw(wl);
2438 if (!booted) {
2439 ret = -EINVAL;
2440 goto out;
2441 }
2442 }
2443
1d095475
EP
2444 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2445 role_type, &wlvif->role_id);
2446 if (ret < 0)
2447 goto out;
2448
2449 ret = wl1271_init_vif_specific(wl, vif);
2450 if (ret < 0)
2451 goto out;
2452
87627214 2453 list_add(&wlvif->list, &wl->wlvif_list);
10c8cd01 2454 set_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags);
a4e4130d
EP
2455
2456 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2457 wl->ap_count++;
2458 else
2459 wl->sta_count++;
eb5b28d0 2460out:
f750c820
EP
2461 wl1271_ps_elp_sleep(wl);
2462out_unlock:
f5fc0f86
LC
2463 mutex_unlock(&wl->mutex);
2464
2465 return ret;
2466}
2467
7dece1c8 2468static void __wl1271_op_remove_interface(struct wl1271 *wl,
536129c8 2469 struct ieee80211_vif *vif,
7dece1c8 2470 bool reset_tx_queues)
f5fc0f86 2471{
536129c8 2472 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e5a359f8 2473 int i, ret;
2f18cf7c 2474 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
f5fc0f86 2475
1b72aecd 2476 wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
f5fc0f86 2477
10c8cd01
EP
2478 if (!test_and_clear_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2479 return;
2480
13026dec 2481 /* because of hardware recovery, we may get here twice */
4cc53383 2482 if (wl->state == WLCORE_STATE_OFF)
13026dec
JO
2483 return;
2484
1b72aecd 2485 wl1271_info("down");
f5fc0f86 2486
baf6277a 2487 if (wl->scan.state != WL1271_SCAN_STATE_IDLE &&
c50a2825 2488 wl->scan_wlvif == wlvif) {
55df5afb
AN
2489 /*
2490 * Rearm the tx watchdog just before idling scan. This
2491 * prevents just-finished scans from triggering the watchdog
2492 */
2493 wl12xx_rearm_tx_watchdog_locked(wl);
2494
08688d6b 2495 wl->scan.state = WL1271_SCAN_STATE_IDLE;
4a31c11c 2496 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
c50a2825 2497 wl->scan_wlvif = NULL;
b739a42c 2498 wl->scan.req = NULL;
76a029fb 2499 ieee80211_scan_completed(wl->hw, true);
f5fc0f86
LC
2500 }
2501
10199756
EP
2502 if (wl->sched_vif == wlvif) {
2503 ieee80211_sched_scan_stopped(wl->hw);
2504 wl->sched_vif = NULL;
2505 }
2506
5d979f35
AN
2507 if (wl->roc_vif == vif) {
2508 wl->roc_vif = NULL;
2509 ieee80211_remain_on_channel_expired(wl->hw);
2510 }
2511
b78b47eb
EP
2512 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2513 /* disable active roles */
2514 ret = wl1271_ps_elp_wakeup(wl);
2515 if (ret < 0)
2516 goto deinit;
2517
b890f4c3
EP
2518 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2519 wlvif->bss_type == BSS_TYPE_IBSS) {
2520 if (wl12xx_dev_role_started(wlvif))
2521 wl12xx_stop_dev(wl, wlvif);
04e8079c
EP
2522 }
2523
0603d891 2524 ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
b78b47eb
EP
2525 if (ret < 0)
2526 goto deinit;
2527
2528 wl1271_ps_elp_sleep(wl);
2529 }
2530deinit:
e51ae9be 2531 /* clear all hlids (except system_hlid) */
afaf8bdb 2532 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
e5a359f8
EP
2533
2534 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2535 wlvif->bss_type == BSS_TYPE_IBSS) {
2536 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2537 wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2538 wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2539 wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
001e39a8 2540 wlcore_free_klv_template(wl, &wlvif->sta.klv_template_id);
e5a359f8
EP
2541 } else {
2542 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2543 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2544 wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2545 wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2546 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2547 wl12xx_free_rate_policy(wl,
2548 &wlvif->ap.ucast_rate_idx[i]);
830be7e0 2549 wl1271_free_ap_keys(wl, wlvif);
e5a359f8 2550 }
b78b47eb 2551
3eba4a0e
ES
2552 dev_kfree_skb(wlvif->probereq);
2553 wlvif->probereq = NULL;
d6a3cc2e 2554 wl12xx_tx_reset_wlvif(wl, wlvif);
e4120df9
EP
2555 if (wl->last_wlvif == wlvif)
2556 wl->last_wlvif = NULL;
87627214 2557 list_del(&wlvif->list);
c7ffb902 2558 memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
0603d891 2559 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
7edebf56 2560 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
d6e19d13 2561
2f18cf7c 2562 if (is_ap)
a4e4130d
EP
2563 wl->ap_count--;
2564 else
2565 wl->sta_count--;
2566
42066f9a
AN
2567 /*
2568 * Last AP, have more stations. Configure sleep auth according to STA.
2569 * Don't do thin on unintended recovery.
2570 */
2571 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) &&
2572 !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags))
2573 goto unlock;
2574
2f18cf7c
AN
2575 if (wl->ap_count == 0 && is_ap && wl->sta_count) {
2576 u8 sta_auth = wl->conf.conn.sta_sleep_auth;
2577 /* Configure for power according to debugfs */
2578 if (sta_auth != WL1271_PSM_ILLEGAL)
2579 wl1271_acx_sleep_auth(wl, sta_auth);
2f18cf7c
AN
2580 /* Configure for ELP power saving */
2581 else
2582 wl1271_acx_sleep_auth(wl, WL1271_PSM_ELP);
2583 }
2584
42066f9a 2585unlock:
baf6277a 2586 mutex_unlock(&wl->mutex);
d6bf9ada 2587
9eb599e9
EP
2588 del_timer_sync(&wlvif->rx_streaming_timer);
2589 cancel_work_sync(&wlvif->rx_streaming_enable_work);
2590 cancel_work_sync(&wlvif->rx_streaming_disable_work);
c50a2825 2591 cancel_delayed_work_sync(&wlvif->connection_loss_work);
bd9dc49c 2592
baf6277a 2593 mutex_lock(&wl->mutex);
52a2a375 2594}
bd9dc49c 2595
52a2a375
JO
2596static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2597 struct ieee80211_vif *vif)
2598{
2599 struct wl1271 *wl = hw->priv;
10c8cd01 2600 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
6e8cd331 2601 struct wl12xx_vif *iter;
4549d09c 2602 struct vif_counter_data vif_count;
52a2a375 2603
4549d09c 2604 wl12xx_get_vif_count(hw, vif, &vif_count);
52a2a375 2605 mutex_lock(&wl->mutex);
10c8cd01 2606
4cc53383 2607 if (wl->state == WLCORE_STATE_OFF ||
10c8cd01
EP
2608 !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2609 goto out;
2610
67353299
JO
2611 /*
2612 * wl->vif can be null here if someone shuts down the interface
2613 * just when hardware recovery has been started.
2614 */
6e8cd331
EP
2615 wl12xx_for_each_wlvif(wl, iter) {
2616 if (iter != wlvif)
2617 continue;
2618
536129c8 2619 __wl1271_op_remove_interface(wl, vif, true);
6e8cd331 2620 break;
67353299 2621 }
6e8cd331 2622 WARN_ON(iter != wlvif);
4549d09c 2623 if (wl12xx_need_fw_change(wl, vif_count, false)) {
3dee4393 2624 wl12xx_force_active_psm(wl);
e9ba7152 2625 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
4549d09c 2626 wl12xx_queue_recovery_work(wl);
4549d09c 2627 }
10c8cd01 2628out:
67353299 2629 mutex_unlock(&wl->mutex);
f5fc0f86
LC
2630}
2631
c0fad1b7
EP
2632static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2633 struct ieee80211_vif *vif,
2634 enum nl80211_iftype new_type, bool p2p)
2635{
4549d09c
EP
2636 struct wl1271 *wl = hw->priv;
2637 int ret;
2638
2639 set_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
c0fad1b7
EP
2640 wl1271_op_remove_interface(hw, vif);
2641
249e9698 2642 vif->type = new_type;
c0fad1b7 2643 vif->p2p = p2p;
4549d09c
EP
2644 ret = wl1271_op_add_interface(hw, vif);
2645
2646 clear_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2647 return ret;
c0fad1b7
EP
2648}
2649
3230f35e 2650static int wlcore_join(struct wl1271 *wl, struct wl12xx_vif *wlvif)
82429d32
JO
2651{
2652 int ret;
536129c8 2653 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
82429d32 2654
69e5434c
JO
2655 /*
2656 * One of the side effects of the JOIN command is that is clears
2657 * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2658 * to a WPA/WPA2 access point will therefore kill the data-path.
8bf69aae
OBC
2659 * Currently the only valid scenario for JOIN during association
2660 * is on roaming, in which case we will also be given new keys.
2661 * Keep the below message for now, unless it starts bothering
2662 * users who really like to roam a lot :)
69e5434c 2663 */
ba8447f6 2664 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
69e5434c
JO
2665 wl1271_info("JOIN while associated.");
2666
5ec8a448
EP
2667 /* clear encryption type */
2668 wlvif->encryption_type = KEY_NONE;
2669
227e81e1 2670 if (is_ibss)
87fbcb0f 2671 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
18eab430
EP
2672 else {
2673 if (wl->quirks & WLCORE_QUIRK_START_STA_FAILS) {
2674 /*
2675 * TODO: this is an ugly workaround for wl12xx fw
2676 * bug - we are not able to tx/rx after the first
2677 * start_sta, so make dummy start+stop calls,
2678 * and then call start_sta again.
2679 * this should be fixed in the fw.
2680 */
2681 wl12xx_cmd_role_start_sta(wl, wlvif);
2682 wl12xx_cmd_role_stop_sta(wl, wlvif);
2683 }
2684
87fbcb0f 2685 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
18eab430 2686 }
3230f35e
EP
2687
2688 return ret;
2689}
2690
2691static int wl1271_ssid_set(struct wl12xx_vif *wlvif, struct sk_buff *skb,
2692 int offset)
2693{
2694 u8 ssid_len;
2695 const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
2696 skb->len - offset);
2697
2698 if (!ptr) {
2699 wl1271_error("No SSID in IEs!");
2700 return -ENOENT;
2701 }
2702
2703 ssid_len = ptr[1];
2704 if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2705 wl1271_error("SSID is too long!");
2706 return -EINVAL;
2707 }
2708
2709 wlvif->ssid_len = ssid_len;
2710 memcpy(wlvif->ssid, ptr+2, ssid_len);
2711 return 0;
2712}
2713
2714static int wlcore_set_ssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2715{
2716 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2717 struct sk_buff *skb;
2718 int ieoffset;
2719
2720 /* we currently only support setting the ssid from the ap probe req */
2721 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
2722 return -EINVAL;
2723
2724 skb = ieee80211_ap_probereq_get(wl->hw, vif);
2725 if (!skb)
2726 return -EINVAL;
2727
2728 ieoffset = offsetof(struct ieee80211_mgmt,
2729 u.probe_req.variable);
2730 wl1271_ssid_set(wlvif, skb, ieoffset);
2731 dev_kfree_skb(skb);
2732
2733 return 0;
2734}
2735
2736static int wlcore_set_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
ec87011a
EP
2737 struct ieee80211_bss_conf *bss_conf,
2738 u32 sta_rate_set)
3230f35e
EP
2739{
2740 int ieoffset;
2741 int ret;
2742
2743 wlvif->aid = bss_conf->aid;
aaabee8b 2744 wlvif->channel_type = cfg80211_get_chandef_type(&bss_conf->chandef);
3230f35e 2745 wlvif->beacon_int = bss_conf->beacon_int;
d50529c0 2746 wlvif->wmm_enabled = bss_conf->qos;
3230f35e
EP
2747
2748 set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2749
2750 /*
2751 * with wl1271, we don't need to update the
2752 * beacon_int and dtim_period, because the firmware
2753 * updates it by itself when the first beacon is
2754 * received after a join.
2755 */
2756 ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
82429d32 2757 if (ret < 0)
3230f35e 2758 return ret;
82429d32 2759
3230f35e
EP
2760 /*
2761 * Get a template for hardware connection maintenance
2762 */
2763 dev_kfree_skb(wlvif->probereq);
2764 wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
2765 wlvif,
2766 NULL);
2767 ieoffset = offsetof(struct ieee80211_mgmt,
2768 u.probe_req.variable);
2769 wl1271_ssid_set(wlvif, wlvif->probereq, ieoffset);
2770
2771 /* enable the connection monitoring feature */
2772 ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
2773 if (ret < 0)
2774 return ret;
82429d32
JO
2775
2776 /*
2777 * The join command disable the keep-alive mode, shut down its process,
2778 * and also clear the template config, so we need to reset it all after
2779 * the join. The acx_aid starts the keep-alive process, and the order
2780 * of the commands below is relevant.
2781 */
0603d891 2782 ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
82429d32 2783 if (ret < 0)
3230f35e 2784 return ret;
82429d32 2785
0603d891 2786 ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
82429d32 2787 if (ret < 0)
3230f35e 2788 return ret;
82429d32 2789
d2d66c56 2790 ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
82429d32 2791 if (ret < 0)
3230f35e 2792 return ret;
82429d32 2793
0603d891 2794 ret = wl1271_acx_keep_alive_config(wl, wlvif,
001e39a8 2795 wlvif->sta.klv_template_id,
82429d32
JO
2796 ACX_KEEP_ALIVE_TPL_VALID);
2797 if (ret < 0)
3230f35e 2798 return ret;
82429d32 2799
6c7b5194
EP
2800 /*
2801 * The default fw psm configuration is AUTO, while mac80211 default
2802 * setting is off (ACTIVE), so sync the fw with the correct value.
2803 */
2804 ret = wl1271_ps_set_mode(wl, wlvif, STATION_ACTIVE_MODE);
ec87011a
EP
2805 if (ret < 0)
2806 return ret;
2807
2808 if (sta_rate_set) {
2809 wlvif->rate_set =
2810 wl1271_tx_enabled_rates_get(wl,
2811 sta_rate_set,
2812 wlvif->band);
2813 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2814 if (ret < 0)
2815 return ret;
2816 }
82429d32 2817
82429d32
JO
2818 return ret;
2819}
2820
3230f35e 2821static int wlcore_unset_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
c7f43e45
LC
2822{
2823 int ret;
3230f35e
EP
2824 bool sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
2825
2826 /* make sure we are connected (sta) joined */
2827 if (sta &&
2828 !test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2829 return false;
2830
2831 /* make sure we are joined (ibss) */
2832 if (!sta &&
2833 test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags))
2834 return false;
2835
2836 if (sta) {
2837 /* use defaults when not associated */
2838 wlvif->aid = 0;
2839
2840 /* free probe-request template */
2841 dev_kfree_skb(wlvif->probereq);
2842 wlvif->probereq = NULL;
2843
2844 /* disable connection monitor features */
2845 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
2846 if (ret < 0)
2847 return ret;
2848
2849 /* Disable the keep-alive feature */
2850 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
2851 if (ret < 0)
2852 return ret;
2853 }
c7f43e45 2854
52630c5d 2855 if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
6e8cd331
EP
2856 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2857
fcab1890 2858 wl12xx_cmd_stop_channel_switch(wl, wlvif);
6e8cd331 2859 ieee80211_chswitch_done(vif, false);
c50a2825 2860 cancel_delayed_work(&wlvif->channel_switch_work);
6d158ff3
SL
2861 }
2862
4137c17c
EP
2863 /* invalidate keep-alive template */
2864 wl1271_acx_keep_alive_config(wl, wlvif,
001e39a8 2865 wlvif->sta.klv_template_id,
4137c17c
EP
2866 ACX_KEEP_ALIVE_TPL_INVALID);
2867
b992c682 2868 /* reset TX security counters on a clean disconnect */
48e93e40
EP
2869 wlvif->tx_security_last_seq_lsb = 0;
2870 wlvif->tx_security_seq = 0;
b992c682 2871
3230f35e 2872 return 0;
c7f43e45
LC
2873}
2874
87fbcb0f 2875static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
ebba60c6 2876{
1b92f15e 2877 wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
30d0c8fd 2878 wlvif->rate_set = wlvif->basic_rate_set;
ebba60c6
JO
2879}
2880
9f259c4e
EP
2881static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2882 struct ieee80211_conf *conf, u32 changed)
f5fc0f86 2883{
b6970ee5 2884 int ret;
f5fc0f86 2885
6bd65029 2886 if (conf->power_level != wlvif->power_level) {
0603d891 2887 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
f5fc0f86 2888 if (ret < 0)
9f259c4e 2889 return ret;
f5fc0f86 2890
6bd65029 2891 wlvif->power_level = conf->power_level;
f5fc0f86
LC
2892 }
2893
9f259c4e
EP
2894 return 0;
2895}
2896
2897static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
2898{
2899 struct wl1271 *wl = hw->priv;
2900 struct wl12xx_vif *wlvif;
2901 struct ieee80211_conf *conf = &hw->conf;
b6970ee5 2902 int ret = 0;
9f259c4e 2903
b6970ee5 2904 wl1271_debug(DEBUG_MAC80211, "mac80211 config psm %s power %d %s"
9f259c4e 2905 " changed 0x%x",
9f259c4e
EP
2906 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
2907 conf->power_level,
2908 conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
2909 changed);
2910
9f259c4e
EP
2911 mutex_lock(&wl->mutex);
2912
9f259c4e
EP
2913 if (changed & IEEE80211_CONF_CHANGE_POWER)
2914 wl->power_level = conf->power_level;
2915
4cc53383 2916 if (unlikely(wl->state != WLCORE_STATE_ON))
9f259c4e
EP
2917 goto out;
2918
2919 ret = wl1271_ps_elp_wakeup(wl);
2920 if (ret < 0)
2921 goto out;
2922
2923 /* configure each interface */
2924 wl12xx_for_each_wlvif(wl, wlvif) {
2925 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
2926 if (ret < 0)
2927 goto out_sleep;
2928 }
2929
f5fc0f86
LC
2930out_sleep:
2931 wl1271_ps_elp_sleep(wl);
2932
2933out:
2934 mutex_unlock(&wl->mutex);
2935
2936 return ret;
2937}
2938
b54853f1
JO
2939struct wl1271_filter_params {
2940 bool enabled;
2941 int mc_list_length;
2942 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
2943};
2944
22bedad3
JP
2945static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
2946 struct netdev_hw_addr_list *mc_list)
c87dec9f 2947{
c87dec9f 2948 struct wl1271_filter_params *fp;
22bedad3 2949 struct netdev_hw_addr *ha;
c87dec9f 2950
74441130 2951 fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
c87dec9f
JO
2952 if (!fp) {
2953 wl1271_error("Out of memory setting filters.");
2954 return 0;
2955 }
2956
2957 /* update multicast filtering parameters */
c87dec9f 2958 fp->mc_list_length = 0;
22bedad3
JP
2959 if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
2960 fp->enabled = false;
2961 } else {
2962 fp->enabled = true;
2963 netdev_hw_addr_list_for_each(ha, mc_list) {
c87dec9f 2964 memcpy(fp->mc_list[fp->mc_list_length],
22bedad3 2965 ha->addr, ETH_ALEN);
c87dec9f 2966 fp->mc_list_length++;
22bedad3 2967 }
c87dec9f
JO
2968 }
2969
b54853f1 2970 return (u64)(unsigned long)fp;
c87dec9f 2971}
f5fc0f86 2972
b54853f1
JO
2973#define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
2974 FIF_ALLMULTI | \
2975 FIF_FCSFAIL | \
2976 FIF_BCN_PRBRESP_PROMISC | \
2977 FIF_CONTROL | \
2978 FIF_OTHER_BSS)
2979
f5fc0f86
LC
2980static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
2981 unsigned int changed,
c87dec9f 2982 unsigned int *total, u64 multicast)
f5fc0f86 2983{
b54853f1 2984 struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
f5fc0f86 2985 struct wl1271 *wl = hw->priv;
6e8cd331 2986 struct wl12xx_vif *wlvif;
536129c8 2987
b54853f1 2988 int ret;
f5fc0f86 2989
7d057869
AN
2990 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
2991 " total %x", changed, *total);
f5fc0f86 2992
b54853f1
JO
2993 mutex_lock(&wl->mutex);
2994
2c10bb9c
SD
2995 *total &= WL1271_SUPPORTED_FILTERS;
2996 changed &= WL1271_SUPPORTED_FILTERS;
2997
4cc53383 2998 if (unlikely(wl->state != WLCORE_STATE_ON))
b54853f1
JO
2999 goto out;
3000
a620865e 3001 ret = wl1271_ps_elp_wakeup(wl);
b54853f1
JO
3002 if (ret < 0)
3003 goto out;
3004
6e8cd331
EP
3005 wl12xx_for_each_wlvif(wl, wlvif) {
3006 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
3007 if (*total & FIF_ALLMULTI)
3008 ret = wl1271_acx_group_address_tbl(wl, wlvif,
3009 false,
3010 NULL, 0);
3011 else if (fp)
3012 ret = wl1271_acx_group_address_tbl(wl, wlvif,
3013 fp->enabled,
3014 fp->mc_list,
3015 fp->mc_list_length);
3016 if (ret < 0)
3017 goto out_sleep;
3018 }
7d057869 3019 }
f5fc0f86 3020
08c1d1c7
EP
3021 /*
3022 * the fw doesn't provide an api to configure the filters. instead,
3023 * the filters configuration is based on the active roles / ROC
3024 * state.
3025 */
b54853f1
JO
3026
3027out_sleep:
3028 wl1271_ps_elp_sleep(wl);
3029
3030out:
3031 mutex_unlock(&wl->mutex);
14b228a0 3032 kfree(fp);
f5fc0f86
LC
3033}
3034
170d0e67
EP
3035static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3036 u8 id, u8 key_type, u8 key_size,
3037 const u8 *key, u8 hlid, u32 tx_seq_32,
3038 u16 tx_seq_16)
7f179b46
AN
3039{
3040 struct wl1271_ap_key *ap_key;
3041 int i;
3042
3043 wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
3044
3045 if (key_size > MAX_KEY_SIZE)
3046 return -EINVAL;
3047
3048 /*
3049 * Find next free entry in ap_keys. Also check we are not replacing
3050 * an existing key.
3051 */
3052 for (i = 0; i < MAX_NUM_KEYS; i++) {
170d0e67 3053 if (wlvif->ap.recorded_keys[i] == NULL)
7f179b46
AN
3054 break;
3055
170d0e67 3056 if (wlvif->ap.recorded_keys[i]->id == id) {
7f179b46
AN
3057 wl1271_warning("trying to record key replacement");
3058 return -EINVAL;
3059 }
3060 }
3061
3062 if (i == MAX_NUM_KEYS)
3063 return -EBUSY;
3064
3065 ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
3066 if (!ap_key)
3067 return -ENOMEM;
3068
3069 ap_key->id = id;
3070 ap_key->key_type = key_type;
3071 ap_key->key_size = key_size;
3072 memcpy(ap_key->key, key, key_size);
3073 ap_key->hlid = hlid;
3074 ap_key->tx_seq_32 = tx_seq_32;
3075 ap_key->tx_seq_16 = tx_seq_16;
3076
170d0e67 3077 wlvif->ap.recorded_keys[i] = ap_key;
7f179b46
AN
3078 return 0;
3079}
3080
170d0e67 3081static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
7f179b46
AN
3082{
3083 int i;
3084
3085 for (i = 0; i < MAX_NUM_KEYS; i++) {
170d0e67
EP
3086 kfree(wlvif->ap.recorded_keys[i]);
3087 wlvif->ap.recorded_keys[i] = NULL;
7f179b46
AN
3088 }
3089}
3090
a8ab39a4 3091static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
7f179b46
AN
3092{
3093 int i, ret = 0;
3094 struct wl1271_ap_key *key;
3095 bool wep_key_added = false;
3096
3097 for (i = 0; i < MAX_NUM_KEYS; i++) {
7f97b487 3098 u8 hlid;
170d0e67 3099 if (wlvif->ap.recorded_keys[i] == NULL)
7f179b46
AN
3100 break;
3101
170d0e67 3102 key = wlvif->ap.recorded_keys[i];
7f97b487
EP
3103 hlid = key->hlid;
3104 if (hlid == WL12XX_INVALID_LINK_ID)
a8ab39a4 3105 hlid = wlvif->ap.bcast_hlid;
7f97b487 3106
a8ab39a4 3107 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
7f179b46
AN
3108 key->id, key->key_type,
3109 key->key_size, key->key,
7f97b487 3110 hlid, key->tx_seq_32,
7f179b46
AN
3111 key->tx_seq_16);
3112 if (ret < 0)
3113 goto out;
3114
3115 if (key->key_type == KEY_WEP)
3116 wep_key_added = true;
3117 }
3118
3119 if (wep_key_added) {
f75c753f 3120 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
a8ab39a4 3121 wlvif->ap.bcast_hlid);
7f179b46
AN
3122 if (ret < 0)
3123 goto out;
3124 }
3125
3126out:
170d0e67 3127 wl1271_free_ap_keys(wl, wlvif);
7f179b46
AN
3128 return ret;
3129}
3130
536129c8
EP
3131static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3132 u16 action, u8 id, u8 key_type,
7f179b46
AN
3133 u8 key_size, const u8 *key, u32 tx_seq_32,
3134 u16 tx_seq_16, struct ieee80211_sta *sta)
3135{
3136 int ret;
536129c8 3137 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
7f179b46
AN
3138
3139 if (is_ap) {
3140 struct wl1271_station *wl_sta;
3141 u8 hlid;
3142
3143 if (sta) {
3144 wl_sta = (struct wl1271_station *)sta->drv_priv;
3145 hlid = wl_sta->hlid;
3146 } else {
a8ab39a4 3147 hlid = wlvif->ap.bcast_hlid;
7f179b46
AN
3148 }
3149
53d40d0b 3150 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
7f179b46
AN
3151 /*
3152 * We do not support removing keys after AP shutdown.
3153 * Pretend we do to make mac80211 happy.
3154 */
3155 if (action != KEY_ADD_OR_REPLACE)
3156 return 0;
3157
170d0e67 3158 ret = wl1271_record_ap_key(wl, wlvif, id,
7f179b46
AN
3159 key_type, key_size,
3160 key, hlid, tx_seq_32,
3161 tx_seq_16);
3162 } else {
a8ab39a4 3163 ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
7f179b46
AN
3164 id, key_type, key_size,
3165 key, hlid, tx_seq_32,
3166 tx_seq_16);
3167 }
3168
3169 if (ret < 0)
3170 return ret;
3171 } else {
3172 const u8 *addr;
3173 static const u8 bcast_addr[ETH_ALEN] = {
3174 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
3175 };
3176
3177 addr = sta ? sta->addr : bcast_addr;
3178
3179 if (is_zero_ether_addr(addr)) {
3180 /* We dont support TX only encryption */
3181 return -EOPNOTSUPP;
3182 }
3183
3184 /* The wl1271 does not allow to remove unicast keys - they
3185 will be cleared automatically on next CMD_JOIN. Ignore the
3186 request silently, as we dont want the mac80211 to emit
3187 an error message. */
3188 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
3189 return 0;
3190
010d3d30
EP
3191 /* don't remove key if hlid was already deleted */
3192 if (action == KEY_REMOVE &&
154da67c 3193 wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
010d3d30
EP
3194 return 0;
3195
a8ab39a4 3196 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
7f179b46
AN
3197 id, key_type, key_size,
3198 key, addr, tx_seq_32,
3199 tx_seq_16);
3200 if (ret < 0)
3201 return ret;
3202
3203 /* the default WEP key needs to be configured at least once */
3204 if (key_type == KEY_WEP) {
c690ec81 3205 ret = wl12xx_cmd_set_default_wep_key(wl,
f75c753f
EP
3206 wlvif->default_key,
3207 wlvif->sta.hlid);
7f179b46
AN
3208 if (ret < 0)
3209 return ret;
3210 }
3211 }
3212
3213 return 0;
3214}
3215
a1c597f2 3216static int wlcore_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
f5fc0f86
LC
3217 struct ieee80211_vif *vif,
3218 struct ieee80211_sta *sta,
3219 struct ieee80211_key_conf *key_conf)
3220{
3221 struct wl1271 *wl = hw->priv;
af390f4d
EP
3222 int ret;
3223 bool might_change_spare =
3224 key_conf->cipher == WL1271_CIPHER_SUITE_GEM ||
3225 key_conf->cipher == WLAN_CIPHER_SUITE_TKIP;
3226
3227 if (might_change_spare) {
3228 /*
3229 * stop the queues and flush to ensure the next packets are
3230 * in sync with FW spare block accounting
3231 */
3232 mutex_lock(&wl->mutex);
3233 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3234 mutex_unlock(&wl->mutex);
3235
3236 wl1271_tx_flush(wl);
3237 }
3238
3239 mutex_lock(&wl->mutex);
3240
3241 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3242 ret = -EAGAIN;
3243 goto out_wake_queues;
3244 }
a1c597f2 3245
af390f4d
EP
3246 ret = wl1271_ps_elp_wakeup(wl);
3247 if (ret < 0)
3248 goto out_wake_queues;
3249
3250 ret = wlcore_hw_set_key(wl, cmd, vif, sta, key_conf);
3251
3252 wl1271_ps_elp_sleep(wl);
3253
3254out_wake_queues:
3255 if (might_change_spare)
3256 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3257
3258 mutex_unlock(&wl->mutex);
3259
3260 return ret;
a1c597f2
AN
3261}
3262
3263int wlcore_set_key(struct wl1271 *wl, enum set_key_cmd cmd,
3264 struct ieee80211_vif *vif,
3265 struct ieee80211_sta *sta,
3266 struct ieee80211_key_conf *key_conf)
3267{
536129c8 3268 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
f5fc0f86 3269 int ret;
ac4e4ce5
JO
3270 u32 tx_seq_32 = 0;
3271 u16 tx_seq_16 = 0;
f5fc0f86
LC
3272 u8 key_type;
3273
f5fc0f86
LC
3274 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
3275
7f179b46 3276 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
f5fc0f86 3277 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
97359d12 3278 key_conf->cipher, key_conf->keyidx,
f5fc0f86
LC
3279 key_conf->keylen, key_conf->flags);
3280 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
3281
97359d12
JB
3282 switch (key_conf->cipher) {
3283 case WLAN_CIPHER_SUITE_WEP40:
3284 case WLAN_CIPHER_SUITE_WEP104:
f5fc0f86
LC
3285 key_type = KEY_WEP;
3286
3287 key_conf->hw_key_idx = key_conf->keyidx;
3288 break;
97359d12 3289 case WLAN_CIPHER_SUITE_TKIP:
f5fc0f86
LC
3290 key_type = KEY_TKIP;
3291
3292 key_conf->hw_key_idx = key_conf->keyidx;
48e93e40
EP
3293 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3294 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
f5fc0f86 3295 break;
97359d12 3296 case WLAN_CIPHER_SUITE_CCMP:
f5fc0f86
LC
3297 key_type = KEY_AES;
3298
12d4b975 3299 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
48e93e40
EP
3300 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3301 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
f5fc0f86 3302 break;
7a55724e
JO
3303 case WL1271_CIPHER_SUITE_GEM:
3304 key_type = KEY_GEM;
48e93e40
EP
3305 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3306 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
7a55724e 3307 break;
f5fc0f86 3308 default:
97359d12 3309 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
f5fc0f86 3310
af390f4d 3311 return -EOPNOTSUPP;
f5fc0f86
LC
3312 }
3313
3314 switch (cmd) {
3315 case SET_KEY:
536129c8 3316 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
7f179b46
AN
3317 key_conf->keyidx, key_type,
3318 key_conf->keylen, key_conf->key,
3319 tx_seq_32, tx_seq_16, sta);
f5fc0f86
LC
3320 if (ret < 0) {
3321 wl1271_error("Could not add or replace key");
af390f4d 3322 return ret;
f5fc0f86 3323 }
5ec8a448
EP
3324
3325 /*
3326 * reconfiguring arp response if the unicast (or common)
3327 * encryption key type was changed
3328 */
3329 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3330 (sta || key_type == KEY_WEP) &&
3331 wlvif->encryption_type != key_type) {
3332 wlvif->encryption_type = key_type;
3333 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3334 if (ret < 0) {
3335 wl1271_warning("build arp rsp failed: %d", ret);
af390f4d 3336 return ret;
5ec8a448
EP
3337 }
3338 }
f5fc0f86
LC
3339 break;
3340
3341 case DISABLE_KEY:
536129c8 3342 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
7f179b46
AN
3343 key_conf->keyidx, key_type,
3344 key_conf->keylen, key_conf->key,
3345 0, 0, sta);
f5fc0f86
LC
3346 if (ret < 0) {
3347 wl1271_error("Could not remove key");
af390f4d 3348 return ret;
f5fc0f86
LC
3349 }
3350 break;
3351
3352 default:
3353 wl1271_error("Unsupported key cmd 0x%x", cmd);
af390f4d 3354 return -EOPNOTSUPP;
f5fc0f86
LC
3355 }
3356
f5fc0f86
LC
3357 return ret;
3358}
a1c597f2 3359EXPORT_SYMBOL_GPL(wlcore_set_key);
f5fc0f86 3360
6b70e7eb
VG
3361void wlcore_regdomain_config(struct wl1271 *wl)
3362{
3363 int ret;
3364
3365 if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
3366 return;
3367
3368 mutex_lock(&wl->mutex);
3369 ret = wl1271_ps_elp_wakeup(wl);
3370 if (ret < 0)
3371 goto out;
3372
3373 ret = wlcore_cmd_regdomain_config_locked(wl);
3374 if (ret < 0) {
3375 wl12xx_queue_recovery_work(wl);
3376 goto out;
3377 }
3378
3379 wl1271_ps_elp_sleep(wl);
3380out:
3381 mutex_unlock(&wl->mutex);
3382}
3383
f5fc0f86 3384static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
a060bbfe 3385 struct ieee80211_vif *vif,
f5fc0f86
LC
3386 struct cfg80211_scan_request *req)
3387{
3388 struct wl1271 *wl = hw->priv;
3389 int ret;
3390 u8 *ssid = NULL;
abb0b3bf 3391 size_t len = 0;
f5fc0f86
LC
3392
3393 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3394
3395 if (req->n_ssids) {
3396 ssid = req->ssids[0].ssid;
abb0b3bf 3397 len = req->ssids[0].ssid_len;
f5fc0f86
LC
3398 }
3399
3400 mutex_lock(&wl->mutex);
3401
4cc53383 3402 if (unlikely(wl->state != WLCORE_STATE_ON)) {
b739a42c
JO
3403 /*
3404 * We cannot return -EBUSY here because cfg80211 will expect
3405 * a call to ieee80211_scan_completed if we do - in this case
3406 * there won't be any call.
3407 */
3408 ret = -EAGAIN;
3409 goto out;
3410 }
3411
a620865e 3412 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
3413 if (ret < 0)
3414 goto out;
3415
97fd311a
EP
3416 /* fail if there is any role in ROC */
3417 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
92e712da
EP
3418 /* don't allow scanning right now */
3419 ret = -EBUSY;
3420 goto out_sleep;
3421 }
3422
78e28062 3423 ret = wlcore_scan(hw->priv, vif, ssid, len, req);
251c177f 3424out_sleep:
f5fc0f86 3425 wl1271_ps_elp_sleep(wl);
f5fc0f86
LC
3426out:
3427 mutex_unlock(&wl->mutex);
3428
3429 return ret;
3430}
3431
73ecce31
EP
3432static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3433 struct ieee80211_vif *vif)
3434{
3435 struct wl1271 *wl = hw->priv;
78e28062 3436 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
73ecce31
EP
3437 int ret;
3438
3439 wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3440
3441 mutex_lock(&wl->mutex);
3442
4cc53383 3443 if (unlikely(wl->state != WLCORE_STATE_ON))
73ecce31
EP
3444 goto out;
3445
3446 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3447 goto out;
3448
3449 ret = wl1271_ps_elp_wakeup(wl);
3450 if (ret < 0)
3451 goto out;
3452
3453 if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
78e28062 3454 ret = wl->ops->scan_stop(wl, wlvif);
73ecce31
EP
3455 if (ret < 0)
3456 goto out_sleep;
3457 }
55df5afb
AN
3458
3459 /*
3460 * Rearm the tx watchdog just before idling scan. This
3461 * prevents just-finished scans from triggering the watchdog
3462 */
3463 wl12xx_rearm_tx_watchdog_locked(wl);
3464
73ecce31
EP
3465 wl->scan.state = WL1271_SCAN_STATE_IDLE;
3466 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
c50a2825 3467 wl->scan_wlvif = NULL;
73ecce31
EP
3468 wl->scan.req = NULL;
3469 ieee80211_scan_completed(wl->hw, true);
3470
3471out_sleep:
3472 wl1271_ps_elp_sleep(wl);
3473out:
3474 mutex_unlock(&wl->mutex);
3475
3476 cancel_delayed_work_sync(&wl->scan_complete_work);
3477}
3478
33c2c06c
LC
3479static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3480 struct ieee80211_vif *vif,
3481 struct cfg80211_sched_scan_request *req,
3482 struct ieee80211_sched_scan_ies *ies)
3483{
3484 struct wl1271 *wl = hw->priv;
536129c8 3485 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
33c2c06c
LC
3486 int ret;
3487
3488 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3489
3490 mutex_lock(&wl->mutex);
3491
4cc53383 3492 if (unlikely(wl->state != WLCORE_STATE_ON)) {
9e0dc890
PF
3493 ret = -EAGAIN;
3494 goto out;
3495 }
3496
33c2c06c
LC
3497 ret = wl1271_ps_elp_wakeup(wl);
3498 if (ret < 0)
3499 goto out;
3500
78e28062 3501 ret = wl->ops->sched_scan_start(wl, wlvif, req, ies);
33c2c06c
LC
3502 if (ret < 0)
3503 goto out_sleep;
3504
10199756 3505 wl->sched_vif = wlvif;
33c2c06c
LC
3506
3507out_sleep:
3508 wl1271_ps_elp_sleep(wl);
3509out:
3510 mutex_unlock(&wl->mutex);
3511 return ret;
3512}
3513
3514static void wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3515 struct ieee80211_vif *vif)
3516{
3517 struct wl1271 *wl = hw->priv;
78f85f50 3518 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
33c2c06c
LC
3519 int ret;
3520
3521 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3522
3523 mutex_lock(&wl->mutex);
3524
4cc53383 3525 if (unlikely(wl->state != WLCORE_STATE_ON))
9e0dc890
PF
3526 goto out;
3527
33c2c06c
LC
3528 ret = wl1271_ps_elp_wakeup(wl);
3529 if (ret < 0)
3530 goto out;
3531
78e28062 3532 wl->ops->sched_scan_stop(wl, wlvif);
33c2c06c
LC
3533
3534 wl1271_ps_elp_sleep(wl);
3535out:
3536 mutex_unlock(&wl->mutex);
3537}
3538
68d069c4
AN
3539static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3540{
3541 struct wl1271 *wl = hw->priv;
3542 int ret = 0;
3543
3544 mutex_lock(&wl->mutex);
3545
4cc53383 3546 if (unlikely(wl->state != WLCORE_STATE_ON)) {
68d069c4
AN
3547 ret = -EAGAIN;
3548 goto out;
3549 }
3550
a620865e 3551 ret = wl1271_ps_elp_wakeup(wl);
68d069c4
AN
3552 if (ret < 0)
3553 goto out;
3554
5f704d18 3555 ret = wl1271_acx_frag_threshold(wl, value);
68d069c4
AN
3556 if (ret < 0)
3557 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3558
3559 wl1271_ps_elp_sleep(wl);
3560
3561out:
3562 mutex_unlock(&wl->mutex);
3563
3564 return ret;
3565}
3566
f5fc0f86
LC
3567static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3568{
3569 struct wl1271 *wl = hw->priv;
6e8cd331 3570 struct wl12xx_vif *wlvif;
aecb0565 3571 int ret = 0;
f5fc0f86
LC
3572
3573 mutex_lock(&wl->mutex);
3574
4cc53383 3575 if (unlikely(wl->state != WLCORE_STATE_ON)) {
f8d9802f 3576 ret = -EAGAIN;
aecb0565 3577 goto out;
f8d9802f 3578 }
aecb0565 3579
a620865e 3580 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
3581 if (ret < 0)
3582 goto out;
3583
6e8cd331
EP
3584 wl12xx_for_each_wlvif(wl, wlvif) {
3585 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3586 if (ret < 0)
3587 wl1271_warning("set rts threshold failed: %d", ret);
3588 }
f5fc0f86
LC
3589 wl1271_ps_elp_sleep(wl);
3590
3591out:
3592 mutex_unlock(&wl->mutex);
3593
3594 return ret;
3595}
3596
d48055d9
EP
3597static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3598{
3599 int len;
3600 const u8 *next, *end = skb->data + skb->len;
3601 u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3602 skb->len - ieoffset);
3603 if (!ie)
3604 return;
3605 len = ie[1] + 2;
3606 next = ie + len;
3607 memmove(ie, next, end - next);
3608 skb_trim(skb, skb->len - len);
3609}
3610
26b4bf2e
EP
3611static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3612 unsigned int oui, u8 oui_type,
3613 int ieoffset)
3614{
3615 int len;
3616 const u8 *next, *end = skb->data + skb->len;
3617 u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3618 skb->data + ieoffset,
3619 skb->len - ieoffset);
3620 if (!ie)
3621 return;
3622 len = ie[1] + 2;
3623 next = ie + len;
3624 memmove(ie, next, end - next);
3625 skb_trim(skb, skb->len - len);
3626}
3627
341f2c11
AN
3628static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3629 struct ieee80211_vif *vif)
560f0024 3630{
cdaac628 3631 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
560f0024
AN
3632 struct sk_buff *skb;
3633 int ret;
3634
341f2c11 3635 skb = ieee80211_proberesp_get(wl->hw, vif);
560f0024 3636 if (!skb)
341f2c11 3637 return -EOPNOTSUPP;
560f0024 3638
cdaac628 3639 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
560f0024
AN
3640 CMD_TEMPL_AP_PROBE_RESPONSE,
3641 skb->data,
3642 skb->len, 0,
3643 rates);
560f0024 3644 dev_kfree_skb(skb);
62c2e579
LC
3645
3646 if (ret < 0)
3647 goto out;
3648
3649 wl1271_debug(DEBUG_AP, "probe response updated");
3650 set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3651
3652out:
560f0024
AN
3653 return ret;
3654}
3655
3656static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3657 struct ieee80211_vif *vif,
3658 u8 *probe_rsp_data,
3659 size_t probe_rsp_len,
3660 u32 rates)
68eaaf6e 3661{
1fe9f161
EP
3662 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3663 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
68eaaf6e
AN
3664 u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3665 int ssid_ie_offset, ie_offset, templ_len;
3666 const u8 *ptr;
3667
3668 /* no need to change probe response if the SSID is set correctly */
1fe9f161 3669 if (wlvif->ssid_len > 0)
cdaac628 3670 return wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3671 CMD_TEMPL_AP_PROBE_RESPONSE,
3672 probe_rsp_data,
3673 probe_rsp_len, 0,
3674 rates);
3675
3676 if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3677 wl1271_error("probe_rsp template too big");
3678 return -EINVAL;
3679 }
3680
3681 /* start searching from IE offset */
3682 ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3683
3684 ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3685 probe_rsp_len - ie_offset);
3686 if (!ptr) {
3687 wl1271_error("No SSID in beacon!");
3688 return -EINVAL;
3689 }
3690
3691 ssid_ie_offset = ptr - probe_rsp_data;
3692 ptr += (ptr[1] + 2);
3693
3694 memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3695
3696 /* insert SSID from bss_conf */
3697 probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3698 probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3699 memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3700 bss_conf->ssid, bss_conf->ssid_len);
3701 templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3702
3703 memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3704 ptr, probe_rsp_len - (ptr - probe_rsp_data));
3705 templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3706
cdaac628 3707 return wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3708 CMD_TEMPL_AP_PROBE_RESPONSE,
3709 probe_rsp_templ,
3710 templ_len, 0,
3711 rates);
3712}
3713
e78a287a 3714static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
0603d891 3715 struct ieee80211_vif *vif,
f5fc0f86
LC
3716 struct ieee80211_bss_conf *bss_conf,
3717 u32 changed)
3718{
0603d891 3719 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 3720 int ret = 0;
f5fc0f86 3721
e78a287a
AN
3722 if (changed & BSS_CHANGED_ERP_SLOT) {
3723 if (bss_conf->use_short_slot)
0603d891 3724 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
e78a287a 3725 else
0603d891 3726 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
e78a287a
AN
3727 if (ret < 0) {
3728 wl1271_warning("Set slot time failed %d", ret);
3729 goto out;
3730 }
3731 }
f5fc0f86 3732
e78a287a
AN
3733 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3734 if (bss_conf->use_short_preamble)
0603d891 3735 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
e78a287a 3736 else
0603d891 3737 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
e78a287a 3738 }
f5fc0f86 3739
e78a287a
AN
3740 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3741 if (bss_conf->use_cts_prot)
0603d891
EP
3742 ret = wl1271_acx_cts_protect(wl, wlvif,
3743 CTSPROTECT_ENABLE);
e78a287a 3744 else
0603d891
EP
3745 ret = wl1271_acx_cts_protect(wl, wlvif,
3746 CTSPROTECT_DISABLE);
e78a287a
AN
3747 if (ret < 0) {
3748 wl1271_warning("Set ctsprotect failed %d", ret);
3749 goto out;
3750 }
3751 }
f8d9802f 3752
e78a287a
AN
3753out:
3754 return ret;
3755}
f5fc0f86 3756
62c2e579
LC
3757static int wlcore_set_beacon_template(struct wl1271 *wl,
3758 struct ieee80211_vif *vif,
3759 bool is_ap)
3760{
3761 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3762 struct ieee80211_hdr *hdr;
3763 u32 min_rate;
3764 int ret;
3765 int ieoffset = offsetof(struct ieee80211_mgmt,
3766 u.beacon.variable);
3767 struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
3768 u16 tmpl_id;
3769
3770 if (!beacon) {
3771 ret = -EINVAL;
3772 goto out;
3773 }
3774
3775 wl1271_debug(DEBUG_MASTER, "beacon updated");
3776
3230f35e 3777 ret = wl1271_ssid_set(wlvif, beacon, ieoffset);
62c2e579
LC
3778 if (ret < 0) {
3779 dev_kfree_skb(beacon);
3780 goto out;
3781 }
3782 min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3783 tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
3784 CMD_TEMPL_BEACON;
3785 ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
3786 beacon->data,
3787 beacon->len, 0,
3788 min_rate);
3789 if (ret < 0) {
3790 dev_kfree_skb(beacon);
3791 goto out;
3792 }
3793
d50529c0
EP
3794 wlvif->wmm_enabled =
3795 cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
3796 WLAN_OUI_TYPE_MICROSOFT_WMM,
3797 beacon->data + ieoffset,
3798 beacon->len - ieoffset);
3799
62c2e579
LC
3800 /*
3801 * In case we already have a probe-resp beacon set explicitly
3802 * by usermode, don't use the beacon data.
3803 */
3804 if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
3805 goto end_bcn;
3806
3807 /* remove TIM ie from probe response */
3808 wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
3809
3810 /*
3811 * remove p2p ie from probe response.
3812 * the fw reponds to probe requests that don't include
3813 * the p2p ie. probe requests with p2p ie will be passed,
3814 * and will be responded by the supplicant (the spec
3815 * forbids including the p2p ie when responding to probe
3816 * requests that didn't include it).
3817 */
3818 wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
3819 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
3820
3821 hdr = (struct ieee80211_hdr *) beacon->data;
3822 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3823 IEEE80211_STYPE_PROBE_RESP);
3824 if (is_ap)
3825 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
3826 beacon->data,
3827 beacon->len,
3828 min_rate);
3829 else
3830 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3831 CMD_TEMPL_PROBE_RESPONSE,
3832 beacon->data,
3833 beacon->len, 0,
3834 min_rate);
3835end_bcn:
3836 dev_kfree_skb(beacon);
3837 if (ret < 0)
3838 goto out;
3839
3840out:
3841 return ret;
3842}
3843
e78a287a
AN
3844static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
3845 struct ieee80211_vif *vif,
3846 struct ieee80211_bss_conf *bss_conf,
3847 u32 changed)
3848{
87fbcb0f 3849 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
536129c8 3850 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
3851 int ret = 0;
3852
48af2eb0 3853 if (changed & BSS_CHANGED_BEACON_INT) {
e78a287a 3854 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
60e84c2e
JO
3855 bss_conf->beacon_int);
3856
6a899796 3857 wlvif->beacon_int = bss_conf->beacon_int;
60e84c2e
JO
3858 }
3859
560f0024
AN
3860 if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
3861 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
62c2e579
LC
3862
3863 wl1271_ap_set_probe_resp_tmpl(wl, rate, vif);
560f0024
AN
3864 }
3865
48af2eb0 3866 if (changed & BSS_CHANGED_BEACON) {
62c2e579 3867 ret = wlcore_set_beacon_template(wl, vif, is_ap);
e78a287a
AN
3868 if (ret < 0)
3869 goto out;
3870 }
3871
3872out:
560f0024
AN
3873 if (ret != 0)
3874 wl1271_error("beacon info change failed: %d", ret);
e78a287a
AN
3875 return ret;
3876}
3877
3878/* AP mode changes */
3879static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
3880 struct ieee80211_vif *vif,
3881 struct ieee80211_bss_conf *bss_conf,
3882 u32 changed)
3883{
87fbcb0f 3884 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 3885 int ret = 0;
e0d8bbf0 3886
b6970ee5 3887 if (changed & BSS_CHANGED_BASIC_RATES) {
e78a287a 3888 u32 rates = bss_conf->basic_rates;
5da11dcd 3889
87fbcb0f 3890 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 3891 wlvif->band);
d2d66c56 3892 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
87fbcb0f 3893 wlvif->basic_rate_set);
70f47424 3894
87fbcb0f 3895 ret = wl1271_init_ap_rates(wl, wlvif);
e78a287a 3896 if (ret < 0) {
70f47424 3897 wl1271_error("AP rate policy change failed %d", ret);
e78a287a
AN
3898 goto out;
3899 }
c45a85b5 3900
784f694d 3901 ret = wl1271_ap_init_templates(wl, vif);
c45a85b5
AN
3902 if (ret < 0)
3903 goto out;
62c2e579
LC
3904
3905 ret = wl1271_ap_set_probe_resp_tmpl(wl, wlvif->basic_rate, vif);
3906 if (ret < 0)
3907 goto out;
3908
3909 ret = wlcore_set_beacon_template(wl, vif, true);
3910 if (ret < 0)
3911 goto out;
e78a287a 3912 }
2f6724b2 3913
e78a287a
AN
3914 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
3915 if (ret < 0)
3916 goto out;
30240fc7 3917
48af2eb0 3918 if (changed & BSS_CHANGED_BEACON_ENABLED) {
e78a287a 3919 if (bss_conf->enable_beacon) {
53d40d0b 3920 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
87fbcb0f 3921 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
e78a287a
AN
3922 if (ret < 0)
3923 goto out;
e0d8bbf0 3924
a8ab39a4 3925 ret = wl1271_ap_init_hwenc(wl, wlvif);
7f179b46
AN
3926 if (ret < 0)
3927 goto out;
cf42039f 3928
53d40d0b 3929 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
cf42039f 3930 wl1271_debug(DEBUG_AP, "started AP");
e0d8bbf0 3931 }
e78a287a 3932 } else {
53d40d0b 3933 if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
0603d891 3934 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
e78a287a
AN
3935 if (ret < 0)
3936 goto out;
e0d8bbf0 3937
53d40d0b 3938 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
560f0024
AN
3939 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
3940 &wlvif->flags);
e78a287a
AN
3941 wl1271_debug(DEBUG_AP, "stopped AP");
3942 }
3943 }
3944 }
e0d8bbf0 3945
0603d891 3946 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
3947 if (ret < 0)
3948 goto out;
0b932ab9
AN
3949
3950 /* Handle HT information change */
3951 if ((changed & BSS_CHANGED_HT) &&
4bf88530 3952 (bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)) {
0603d891 3953 ret = wl1271_acx_set_ht_information(wl, wlvif,
0b932ab9
AN
3954 bss_conf->ht_operation_mode);
3955 if (ret < 0) {
3956 wl1271_warning("Set ht information failed %d", ret);
3957 goto out;
3958 }
3959 }
3960
e78a287a
AN
3961out:
3962 return;
3963}
8bf29b0e 3964
3230f35e
EP
3965static int wlcore_set_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3966 struct ieee80211_bss_conf *bss_conf,
3967 u32 sta_rate_set)
3968{
3969 u32 rates;
3970 int ret;
3971
3972 wl1271_debug(DEBUG_MAC80211,
3973 "changed_bssid: %pM, aid: %d, bcn_int: %d, brates: 0x%x sta_rate_set: 0x%x",
3974 bss_conf->bssid, bss_conf->aid,
3975 bss_conf->beacon_int,
3976 bss_conf->basic_rates, sta_rate_set);
3977
3978 wlvif->beacon_int = bss_conf->beacon_int;
3979 rates = bss_conf->basic_rates;
3980 wlvif->basic_rate_set =
3981 wl1271_tx_enabled_rates_get(wl, rates,
3982 wlvif->band);
3983 wlvif->basic_rate =
3984 wl1271_tx_min_rate_get(wl,
3985 wlvif->basic_rate_set);
3986
3987 if (sta_rate_set)
3988 wlvif->rate_set =
3989 wl1271_tx_enabled_rates_get(wl,
3990 sta_rate_set,
3991 wlvif->band);
3992
3993 /* we only support sched_scan while not connected */
10199756 3994 if (wl->sched_vif == wlvif)
78e28062 3995 wl->ops->sched_scan_stop(wl, wlvif);
3230f35e
EP
3996
3997 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3998 if (ret < 0)
3999 return ret;
4000
4001 ret = wl12xx_cmd_build_null_data(wl, wlvif);
4002 if (ret < 0)
4003 return ret;
4004
4005 ret = wl1271_build_qos_null_data(wl, wl12xx_wlvif_to_vif(wlvif));
4006 if (ret < 0)
4007 return ret;
4008
4009 wlcore_set_ssid(wl, wlvif);
4010
4011 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
4012
4013 return 0;
4014}
4015
4016static int wlcore_clear_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
4017{
4018 int ret;
4019
4020 /* revert back to minimum rates for the current band */
4021 wl1271_set_band_rate(wl, wlvif);
4022 wlvif->basic_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4023
4024 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4025 if (ret < 0)
4026 return ret;
4027
4028 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4029 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) {
4030 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
4031 if (ret < 0)
4032 return ret;
4033 }
4034
4035 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
4036 return 0;
4037}
e78a287a
AN
4038/* STA/IBSS mode changes */
4039static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
4040 struct ieee80211_vif *vif,
4041 struct ieee80211_bss_conf *bss_conf,
4042 u32 changed)
4043{
87fbcb0f 4044 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3230f35e 4045 bool do_join = false;
536129c8 4046 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
227e81e1 4047 bool ibss_joined = false;
72c2d9e5 4048 u32 sta_rate_set = 0;
e78a287a 4049 int ret;
2d6e4e76 4050 struct ieee80211_sta *sta;
a100885d
AN
4051 bool sta_exists = false;
4052 struct ieee80211_sta_ht_cap sta_ht_cap;
e78a287a
AN
4053
4054 if (is_ibss) {
4055 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
4056 changed);
4057 if (ret < 0)
4058 goto out;
e0d8bbf0
JO
4059 }
4060
227e81e1
EP
4061 if (changed & BSS_CHANGED_IBSS) {
4062 if (bss_conf->ibss_joined) {
eee514e3 4063 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
227e81e1
EP
4064 ibss_joined = true;
4065 } else {
3230f35e
EP
4066 wlcore_unset_assoc(wl, wlvif);
4067 wl12xx_cmd_role_stop_sta(wl, wlvif);
227e81e1
EP
4068 }
4069 }
4070
4071 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
e78a287a
AN
4072 do_join = true;
4073
4074 /* Need to update the SSID (for filtering etc) */
227e81e1 4075 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
e78a287a
AN
4076 do_join = true;
4077
227e81e1 4078 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
5da11dcd
JO
4079 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
4080 bss_conf->enable_beacon ? "enabled" : "disabled");
4081
5da11dcd
JO
4082 do_join = true;
4083 }
4084
48af2eb0 4085 if (changed & BSS_CHANGED_CQM) {
00236aed
JO
4086 bool enable = false;
4087 if (bss_conf->cqm_rssi_thold)
4088 enable = true;
0603d891 4089 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
00236aed
JO
4090 bss_conf->cqm_rssi_thold,
4091 bss_conf->cqm_rssi_hyst);
4092 if (ret < 0)
4093 goto out;
04324d99 4094 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
00236aed
JO
4095 }
4096
ec87011a
EP
4097 if (changed & (BSS_CHANGED_BSSID | BSS_CHANGED_HT |
4098 BSS_CHANGED_ASSOC)) {
0f9c8250
AN
4099 rcu_read_lock();
4100 sta = ieee80211_find_sta(vif, bss_conf->bssid);
ef08d028
LC
4101 if (sta) {
4102 u8 *rx_mask = sta->ht_cap.mcs.rx_mask;
4103
4104 /* save the supp_rates of the ap */
4105 sta_rate_set = sta->supp_rates[wlvif->band];
4106 if (sta->ht_cap.ht_supported)
4107 sta_rate_set |=
4108 (rx_mask[0] << HW_HT_RATES_OFFSET) |
4109 (rx_mask[1] << HW_MIMO_RATES_OFFSET);
4110 sta_ht_cap = sta->ht_cap;
4111 sta_exists = true;
4112 }
4113
0f9c8250 4114 rcu_read_unlock();
72c2d9e5 4115 }
72c2d9e5 4116
3230f35e
EP
4117 if (changed & BSS_CHANGED_BSSID) {
4118 if (!is_zero_ether_addr(bss_conf->bssid)) {
4119 ret = wlcore_set_bssid(wl, wlvif, bss_conf,
4120 sta_rate_set);
f5fc0f86 4121 if (ret < 0)
e78a287a 4122 goto out;
f5fc0f86 4123
3230f35e
EP
4124 /* Need to update the BSSID (for filtering etc) */
4125 do_join = true;
d94cd297 4126 } else {
3230f35e 4127 ret = wlcore_clear_bssid(wl, wlvif);
6ccbb92e 4128 if (ret < 0)
e78a287a 4129 goto out;
f5fc0f86 4130 }
f5fc0f86
LC
4131 }
4132
d192d268
EP
4133 if (changed & BSS_CHANGED_IBSS) {
4134 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
4135 bss_conf->ibss_joined);
4136
4137 if (bss_conf->ibss_joined) {
4138 u32 rates = bss_conf->basic_rates;
87fbcb0f 4139 wlvif->basic_rate_set =
af7fbb28 4140 wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 4141 wlvif->band);
d2d66c56 4142 wlvif->basic_rate =
87fbcb0f
EP
4143 wl1271_tx_min_rate_get(wl,
4144 wlvif->basic_rate_set);
d192d268 4145
06b660e1 4146 /* by default, use 11b + OFDM rates */
30d0c8fd
EP
4147 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
4148 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
d192d268
EP
4149 if (ret < 0)
4150 goto out;
4151 }
4152 }
4153
0603d891 4154 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
4155 if (ret < 0)
4156 goto out;
f5fc0f86 4157
8bf29b0e 4158 if (do_join) {
3230f35e 4159 ret = wlcore_join(wl, wlvif);
8bf29b0e
JO
4160 if (ret < 0) {
4161 wl1271_warning("cmd join failed %d", ret);
e78a287a 4162 goto out;
8bf29b0e 4163 }
3230f35e 4164 }
251c177f 4165
3230f35e
EP
4166 if (changed & BSS_CHANGED_ASSOC) {
4167 if (bss_conf->assoc) {
ec87011a
EP
4168 ret = wlcore_set_assoc(wl, wlvif, bss_conf,
4169 sta_rate_set);
251c177f
EP
4170 if (ret < 0)
4171 goto out;
4172
9fd6f21b
EP
4173 if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
4174 wl12xx_set_authorized(wl, wlvif);
3230f35e
EP
4175 } else {
4176 wlcore_unset_assoc(wl, wlvif);
251c177f 4177 }
c1899554
JO
4178 }
4179
518b680a
EP
4180 if (changed & BSS_CHANGED_PS) {
4181 if ((bss_conf->ps) &&
4182 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
4183 !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4184 int ps_mode;
4185 char *ps_mode_str;
4186
4187 if (wl->conf.conn.forced_ps) {
4188 ps_mode = STATION_POWER_SAVE_MODE;
4189 ps_mode_str = "forced";
4190 } else {
4191 ps_mode = STATION_AUTO_PS_MODE;
4192 ps_mode_str = "auto";
4193 }
4194
4195 wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
4196
4197 ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
251c177f 4198 if (ret < 0)
518b680a
EP
4199 wl1271_warning("enter %s ps failed %d",
4200 ps_mode_str, ret);
4201 } else if (!bss_conf->ps &&
4202 test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4203 wl1271_debug(DEBUG_PSM, "auto ps disabled");
4204
4205 ret = wl1271_ps_set_mode(wl, wlvif,
4206 STATION_ACTIVE_MODE);
4207 if (ret < 0)
4208 wl1271_warning("exit auto ps failed %d", ret);
251c177f 4209 }
c1899554
JO
4210 }
4211
0b932ab9 4212 /* Handle new association with HT. Do this after join. */
58321b29
EP
4213 if (sta_exists &&
4214 (changed & BSS_CHANGED_HT)) {
4215 bool enabled =
aaabee8b 4216 bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT;
58321b29 4217
530abe19
EP
4218 ret = wlcore_hw_set_peer_cap(wl,
4219 &sta_ht_cap,
4220 enabled,
4221 wlvif->rate_set,
4222 wlvif->sta.hlid);
58321b29
EP
4223 if (ret < 0) {
4224 wl1271_warning("Set ht cap failed %d", ret);
4225 goto out;
4226
0f9c8250 4227 }
58321b29
EP
4228
4229 if (enabled) {
4230 ret = wl1271_acx_set_ht_information(wl, wlvif,
4231 bss_conf->ht_operation_mode);
0f9c8250 4232 if (ret < 0) {
58321b29 4233 wl1271_warning("Set ht information failed %d",
0f9c8250
AN
4234 ret);
4235 goto out;
4236 }
4237 }
4238 }
4239
76a74c8a
EP
4240 /* Handle arp filtering. Done after join. */
4241 if ((changed & BSS_CHANGED_ARP_FILTER) ||
4242 (!is_ibss && (changed & BSS_CHANGED_QOS))) {
4243 __be32 addr = bss_conf->arp_addr_list[0];
4244 wlvif->sta.qos = bss_conf->qos;
4245 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
4246
4247 if (bss_conf->arp_addr_cnt == 1 &&
4248 bss_conf->arp_filter_enabled) {
4249 wlvif->ip_addr = addr;
4250 /*
4251 * The template should have been configured only upon
4252 * association. however, it seems that the correct ip
4253 * isn't being set (when sending), so we have to
4254 * reconfigure the template upon every ip change.
4255 */
4256 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
4257 if (ret < 0) {
4258 wl1271_warning("build arp rsp failed: %d", ret);
4259 goto out;
4260 }
4261
4262 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
4263 (ACX_ARP_FILTER_ARP_FILTERING |
4264 ACX_ARP_FILTER_AUTO_ARP),
4265 addr);
4266 } else {
4267 wlvif->ip_addr = 0;
4268 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4269 }
4270
4271 if (ret < 0)
4272 goto out;
4273 }
4274
e78a287a
AN
4275out:
4276 return;
4277}
4278
4279static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4280 struct ieee80211_vif *vif,
4281 struct ieee80211_bss_conf *bss_conf,
4282 u32 changed)
4283{
4284 struct wl1271 *wl = hw->priv;
536129c8
EP
4285 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4286 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
4287 int ret;
4288
d3f5a1b5
EP
4289 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info role %d changed 0x%x",
4290 wlvif->role_id, (int)changed);
e78a287a 4291
6b8bf5bc
AN
4292 /*
4293 * make sure to cancel pending disconnections if our association
4294 * state changed
4295 */
4296 if (!is_ap && (changed & BSS_CHANGED_ASSOC))
c50a2825 4297 cancel_delayed_work_sync(&wlvif->connection_loss_work);
6b8bf5bc 4298
b515d83a
EP
4299 if (is_ap && (changed & BSS_CHANGED_BEACON_ENABLED) &&
4300 !bss_conf->enable_beacon)
4301 wl1271_tx_flush(wl);
4302
e78a287a
AN
4303 mutex_lock(&wl->mutex);
4304
4cc53383 4305 if (unlikely(wl->state != WLCORE_STATE_ON))
e78a287a
AN
4306 goto out;
4307
10c8cd01
EP
4308 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4309 goto out;
4310
a620865e 4311 ret = wl1271_ps_elp_wakeup(wl);
e78a287a
AN
4312 if (ret < 0)
4313 goto out;
4314
4315 if (is_ap)
4316 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4317 else
4318 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4319
f5fc0f86
LC
4320 wl1271_ps_elp_sleep(wl);
4321
4322out:
4323 mutex_unlock(&wl->mutex);
4324}
4325
b6970ee5
EP
4326static int wlcore_op_add_chanctx(struct ieee80211_hw *hw,
4327 struct ieee80211_chanctx_conf *ctx)
4328{
4329 wl1271_debug(DEBUG_MAC80211, "mac80211 add chanctx %d (type %d)",
aaabee8b
LC
4330 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4331 cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4332 return 0;
4333}
4334
4335static void wlcore_op_remove_chanctx(struct ieee80211_hw *hw,
4336 struct ieee80211_chanctx_conf *ctx)
4337{
4338 wl1271_debug(DEBUG_MAC80211, "mac80211 remove chanctx %d (type %d)",
aaabee8b
LC
4339 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4340 cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4341}
4342
4343static void wlcore_op_change_chanctx(struct ieee80211_hw *hw,
4344 struct ieee80211_chanctx_conf *ctx,
4345 u32 changed)
4346{
4347 wl1271_debug(DEBUG_MAC80211,
4348 "mac80211 change chanctx %d (type %d) changed 0x%x",
aaabee8b
LC
4349 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4350 cfg80211_get_chandef_type(&ctx->def), changed);
b6970ee5
EP
4351}
4352
4353static int wlcore_op_assign_vif_chanctx(struct ieee80211_hw *hw,
4354 struct ieee80211_vif *vif,
4355 struct ieee80211_chanctx_conf *ctx)
4356{
4357 struct wl1271 *wl = hw->priv;
4358 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4359 int channel = ieee80211_frequency_to_channel(
aaabee8b 4360 ctx->def.chan->center_freq);
b6970ee5
EP
4361
4362 wl1271_debug(DEBUG_MAC80211,
4363 "mac80211 assign chanctx (role %d) %d (type %d)",
aaabee8b 4364 wlvif->role_id, channel, cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4365
4366 mutex_lock(&wl->mutex);
4367
aaabee8b 4368 wlvif->band = ctx->def.chan->band;
b6970ee5 4369 wlvif->channel = channel;
aaabee8b 4370 wlvif->channel_type = cfg80211_get_chandef_type(&ctx->def);
b6970ee5
EP
4371
4372 /* update default rates according to the band */
4373 wl1271_set_band_rate(wl, wlvif);
4374
4375 mutex_unlock(&wl->mutex);
4376
4377 return 0;
4378}
4379
4380static void wlcore_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
4381 struct ieee80211_vif *vif,
4382 struct ieee80211_chanctx_conf *ctx)
4383{
4384 struct wl1271 *wl = hw->priv;
4385 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4386
4387 wl1271_debug(DEBUG_MAC80211,
4388 "mac80211 unassign chanctx (role %d) %d (type %d)",
4389 wlvif->role_id,
aaabee8b
LC
4390 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4391 cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4392
4393 wl1271_tx_flush(wl);
4394}
4395
8a3a3c85
EP
4396static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4397 struct ieee80211_vif *vif, u16 queue,
c6999d83
KV
4398 const struct ieee80211_tx_queue_params *params)
4399{
4400 struct wl1271 *wl = hw->priv;
0603d891 4401 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4695dc91 4402 u8 ps_scheme;
488fc540 4403 int ret = 0;
c6999d83
KV
4404
4405 mutex_lock(&wl->mutex);
4406
4407 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4408
4695dc91
KV
4409 if (params->uapsd)
4410 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4411 else
4412 ps_scheme = CONF_PS_SCHEME_LEGACY;
4413
5b37ddfe 4414 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
c1b193eb 4415 goto out;
488fc540 4416
c1b193eb
EP
4417 ret = wl1271_ps_elp_wakeup(wl);
4418 if (ret < 0)
4419 goto out;
488fc540 4420
c1b193eb
EP
4421 /*
4422 * the txop is confed in units of 32us by the mac80211,
4423 * we need us
4424 */
0603d891 4425 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4426 params->cw_min, params->cw_max,
4427 params->aifs, params->txop << 5);
4428 if (ret < 0)
4429 goto out_sleep;
4430
0603d891 4431 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4432 CONF_CHANNEL_TYPE_EDCF,
4433 wl1271_tx_get_queue(queue),
4434 ps_scheme, CONF_ACK_POLICY_LEGACY,
4435 0, 0);
c82c1dde
KV
4436
4437out_sleep:
c1b193eb 4438 wl1271_ps_elp_sleep(wl);
c6999d83
KV
4439
4440out:
4441 mutex_unlock(&wl->mutex);
4442
4443 return ret;
4444}
4445
37a41b4a
EP
4446static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4447 struct ieee80211_vif *vif)
bbbb538e
JO
4448{
4449
4450 struct wl1271 *wl = hw->priv;
9c531149 4451 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbbb538e
JO
4452 u64 mactime = ULLONG_MAX;
4453 int ret;
4454
4455 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4456
4457 mutex_lock(&wl->mutex);
4458
4cc53383 4459 if (unlikely(wl->state != WLCORE_STATE_ON))
f8d9802f
JO
4460 goto out;
4461
a620865e 4462 ret = wl1271_ps_elp_wakeup(wl);
bbbb538e
JO
4463 if (ret < 0)
4464 goto out;
4465
9c531149 4466 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
bbbb538e
JO
4467 if (ret < 0)
4468 goto out_sleep;
4469
4470out_sleep:
4471 wl1271_ps_elp_sleep(wl);
4472
4473out:
4474 mutex_unlock(&wl->mutex);
4475 return mactime;
4476}
f5fc0f86 4477
ece550d0
JL
4478static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4479 struct survey_info *survey)
4480{
ece550d0 4481 struct ieee80211_conf *conf = &hw->conf;
b739a42c 4482
ece550d0
JL
4483 if (idx != 0)
4484 return -ENOENT;
b739a42c 4485
ece550d0 4486 survey->channel = conf->channel;
add779a0 4487 survey->filled = 0;
ece550d0
JL
4488 return 0;
4489}
4490
409622ec 4491static int wl1271_allocate_sta(struct wl1271 *wl,
c7ffb902
EP
4492 struct wl12xx_vif *wlvif,
4493 struct ieee80211_sta *sta)
f84f7d78
AN
4494{
4495 struct wl1271_station *wl_sta;
c7ffb902 4496 int ret;
f84f7d78 4497
c7ffb902
EP
4498
4499 if (wl->active_sta_count >= AP_MAX_STATIONS) {
f84f7d78
AN
4500 wl1271_warning("could not allocate HLID - too much stations");
4501 return -EBUSY;
4502 }
4503
4504 wl_sta = (struct wl1271_station *)sta->drv_priv;
c7ffb902
EP
4505 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4506 if (ret < 0) {
4507 wl1271_warning("could not allocate HLID - too many links");
4508 return -EBUSY;
4509 }
4510
4511 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
b622d992 4512 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
da03209e 4513 wl->active_sta_count++;
f84f7d78
AN
4514 return 0;
4515}
4516
c7ffb902 4517void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
f84f7d78 4518{
c7ffb902 4519 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
f1acea9a
AN
4520 return;
4521
c7ffb902 4522 clear_bit(hlid, wlvif->ap.sta_hlid_map);
b622d992
AN
4523 __clear_bit(hlid, &wl->ap_ps_map);
4524 __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
c7ffb902 4525 wl12xx_free_link(wl, wlvif, &hlid);
da03209e 4526 wl->active_sta_count--;
55df5afb
AN
4527
4528 /*
4529 * rearm the tx watchdog when the last STA is freed - give the FW a
4530 * chance to return STA-buffered packets before complaining.
4531 */
4532 if (wl->active_sta_count == 0)
4533 wl12xx_rearm_tx_watchdog_locked(wl);
f84f7d78
AN
4534}
4535
2d6cf2b5
EP
4536static int wl12xx_sta_add(struct wl1271 *wl,
4537 struct wl12xx_vif *wlvif,
4538 struct ieee80211_sta *sta)
f84f7d78 4539{
c7ffb902 4540 struct wl1271_station *wl_sta;
f84f7d78
AN
4541 int ret = 0;
4542 u8 hlid;
4543
f84f7d78
AN
4544 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4545
c7ffb902 4546 ret = wl1271_allocate_sta(wl, wlvif, sta);
f84f7d78 4547 if (ret < 0)
2d6cf2b5 4548 return ret;
f84f7d78 4549
c7ffb902
EP
4550 wl_sta = (struct wl1271_station *)sta->drv_priv;
4551 hlid = wl_sta->hlid;
4552
1b92f15e 4553 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
f84f7d78 4554 if (ret < 0)
2d6cf2b5 4555 wl1271_free_sta(wl, wlvif, hlid);
f84f7d78 4556
2d6cf2b5
EP
4557 return ret;
4558}
b67476ef 4559
2d6cf2b5
EP
4560static int wl12xx_sta_remove(struct wl1271 *wl,
4561 struct wl12xx_vif *wlvif,
4562 struct ieee80211_sta *sta)
4563{
4564 struct wl1271_station *wl_sta;
4565 int ret = 0, id;
0b932ab9 4566
2d6cf2b5
EP
4567 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4568
4569 wl_sta = (struct wl1271_station *)sta->drv_priv;
4570 id = wl_sta->hlid;
4571 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
4572 return -EINVAL;
f84f7d78 4573
2d6cf2b5 4574 ret = wl12xx_cmd_remove_peer(wl, wl_sta->hlid);
409622ec 4575 if (ret < 0)
2d6cf2b5 4576 return ret;
409622ec 4577
2d6cf2b5 4578 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
f84f7d78
AN
4579 return ret;
4580}
4581
426001a6
EP
4582static void wlcore_roc_if_possible(struct wl1271 *wl,
4583 struct wl12xx_vif *wlvif)
4584{
4585 if (find_first_bit(wl->roc_map,
4586 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)
4587 return;
4588
4589 if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID))
4590 return;
4591
4592 wl12xx_roc(wl, wlvif, wlvif->role_id, wlvif->band, wlvif->channel);
4593}
4594
4595static void wlcore_update_inconn_sta(struct wl1271 *wl,
4596 struct wl12xx_vif *wlvif,
4597 struct wl1271_station *wl_sta,
4598 bool in_connection)
4599{
4600 if (in_connection) {
4601 if (WARN_ON(wl_sta->in_connection))
4602 return;
4603 wl_sta->in_connection = true;
4604 if (!wlvif->inconn_count++)
4605 wlcore_roc_if_possible(wl, wlvif);
4606 } else {
4607 if (!wl_sta->in_connection)
4608 return;
4609
4610 wl_sta->in_connection = false;
4611 wlvif->inconn_count--;
4612 if (WARN_ON(wlvif->inconn_count < 0))
4613 return;
4614
4615 if (!wlvif->inconn_count)
4616 if (test_bit(wlvif->role_id, wl->roc_map))
4617 wl12xx_croc(wl, wlvif->role_id);
4618 }
4619}
4620
2d6cf2b5
EP
4621static int wl12xx_update_sta_state(struct wl1271 *wl,
4622 struct wl12xx_vif *wlvif,
4623 struct ieee80211_sta *sta,
4624 enum ieee80211_sta_state old_state,
4625 enum ieee80211_sta_state new_state)
f84f7d78 4626{
f84f7d78 4627 struct wl1271_station *wl_sta;
2d6cf2b5
EP
4628 u8 hlid;
4629 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
4630 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
4631 int ret;
f84f7d78 4632
2d6cf2b5
EP
4633 wl_sta = (struct wl1271_station *)sta->drv_priv;
4634 hlid = wl_sta->hlid;
f84f7d78 4635
2d6cf2b5
EP
4636 /* Add station (AP mode) */
4637 if (is_ap &&
4638 old_state == IEEE80211_STA_NOTEXIST &&
29936266
EP
4639 new_state == IEEE80211_STA_NONE) {
4640 ret = wl12xx_sta_add(wl, wlvif, sta);
4641 if (ret)
4642 return ret;
426001a6
EP
4643
4644 wlcore_update_inconn_sta(wl, wlvif, wl_sta, true);
29936266 4645 }
2d6cf2b5
EP
4646
4647 /* Remove station (AP mode) */
4648 if (is_ap &&
4649 old_state == IEEE80211_STA_NONE &&
4650 new_state == IEEE80211_STA_NOTEXIST) {
4651 /* must not fail */
4652 wl12xx_sta_remove(wl, wlvif, sta);
426001a6
EP
4653
4654 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
2d6cf2b5 4655 }
f84f7d78 4656
2d6cf2b5
EP
4657 /* Authorize station (AP mode) */
4658 if (is_ap &&
4659 new_state == IEEE80211_STA_AUTHORIZED) {
d50529c0 4660 ret = wl12xx_cmd_set_peer_state(wl, wlvif, hlid);
2d6cf2b5
EP
4661 if (ret < 0)
4662 return ret;
f84f7d78 4663
2d6cf2b5
EP
4664 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
4665 hlid);
29936266
EP
4666 if (ret)
4667 return ret;
426001a6
EP
4668
4669 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
2d6cf2b5 4670 }
f84f7d78 4671
9fd6f21b
EP
4672 /* Authorize station */
4673 if (is_sta &&
4674 new_state == IEEE80211_STA_AUTHORIZED) {
4675 set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
29936266
EP
4676 ret = wl12xx_set_authorized(wl, wlvif);
4677 if (ret)
4678 return ret;
9fd6f21b
EP
4679 }
4680
4681 if (is_sta &&
4682 old_state == IEEE80211_STA_AUTHORIZED &&
4683 new_state == IEEE80211_STA_ASSOC) {
4684 clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
3230f35e 4685 clear_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags);
9fd6f21b
EP
4686 }
4687
29936266
EP
4688 /* clear ROCs on failure or authorization */
4689 if (is_sta &&
4690 (new_state == IEEE80211_STA_AUTHORIZED ||
4691 new_state == IEEE80211_STA_NOTEXIST)) {
4692 if (test_bit(wlvif->role_id, wl->roc_map))
4693 wl12xx_croc(wl, wlvif->role_id);
9fd6f21b
EP
4694 }
4695
29936266
EP
4696 if (is_sta &&
4697 old_state == IEEE80211_STA_NOTEXIST &&
4698 new_state == IEEE80211_STA_NONE) {
4699 if (find_first_bit(wl->roc_map,
4700 WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES) {
4701 WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID);
4702 wl12xx_roc(wl, wlvif, wlvif->role_id,
4703 wlvif->band, wlvif->channel);
4704 }
4705 }
2d6cf2b5
EP
4706 return 0;
4707}
4708
4709static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
4710 struct ieee80211_vif *vif,
4711 struct ieee80211_sta *sta,
4712 enum ieee80211_sta_state old_state,
4713 enum ieee80211_sta_state new_state)
4714{
4715 struct wl1271 *wl = hw->priv;
4716 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4717 int ret;
4718
4719 wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
4720 sta->aid, old_state, new_state);
4721
4722 mutex_lock(&wl->mutex);
4723
4cc53383 4724 if (unlikely(wl->state != WLCORE_STATE_ON)) {
2d6cf2b5 4725 ret = -EBUSY;
f84f7d78 4726 goto out;
2d6cf2b5 4727 }
f84f7d78 4728
a620865e 4729 ret = wl1271_ps_elp_wakeup(wl);
f84f7d78
AN
4730 if (ret < 0)
4731 goto out;
4732
2d6cf2b5 4733 ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
f84f7d78 4734
f84f7d78 4735 wl1271_ps_elp_sleep(wl);
f84f7d78
AN
4736out:
4737 mutex_unlock(&wl->mutex);
2d6cf2b5
EP
4738 if (new_state < old_state)
4739 return 0;
f84f7d78
AN
4740 return ret;
4741}
4742
4623ec7d
LC
4743static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
4744 struct ieee80211_vif *vif,
4745 enum ieee80211_ampdu_mlme_action action,
4746 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4747 u8 buf_size)
bbba3e68
LS
4748{
4749 struct wl1271 *wl = hw->priv;
536129c8 4750 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbba3e68 4751 int ret;
0f9c8250
AN
4752 u8 hlid, *ba_bitmap;
4753
4754 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
4755 tid);
4756
4757 /* sanity check - the fields in FW are only 8bits wide */
4758 if (WARN_ON(tid > 0xFF))
4759 return -ENOTSUPP;
bbba3e68
LS
4760
4761 mutex_lock(&wl->mutex);
4762
4cc53383 4763 if (unlikely(wl->state != WLCORE_STATE_ON)) {
bbba3e68
LS
4764 ret = -EAGAIN;
4765 goto out;
4766 }
4767
536129c8 4768 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
154da67c 4769 hlid = wlvif->sta.hlid;
d0802abd 4770 ba_bitmap = &wlvif->sta.ba_rx_bitmap;
536129c8 4771 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
0f9c8250
AN
4772 struct wl1271_station *wl_sta;
4773
4774 wl_sta = (struct wl1271_station *)sta->drv_priv;
4775 hlid = wl_sta->hlid;
4776 ba_bitmap = &wl->links[hlid].ba_bitmap;
4777 } else {
4778 ret = -EINVAL;
4779 goto out;
4780 }
4781
a620865e 4782 ret = wl1271_ps_elp_wakeup(wl);
bbba3e68
LS
4783 if (ret < 0)
4784 goto out;
4785
70559a06
SL
4786 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
4787 tid, action);
4788
bbba3e68
LS
4789 switch (action) {
4790 case IEEE80211_AMPDU_RX_START:
d0802abd 4791 if (!wlvif->ba_support || !wlvif->ba_allowed) {
bbba3e68 4792 ret = -ENOTSUPP;
0f9c8250
AN
4793 break;
4794 }
4795
4796 if (wl->ba_rx_session_count >= RX_BA_MAX_SESSIONS) {
4797 ret = -EBUSY;
4798 wl1271_error("exceeded max RX BA sessions");
4799 break;
4800 }
4801
4802 if (*ba_bitmap & BIT(tid)) {
4803 ret = -EINVAL;
4804 wl1271_error("cannot enable RX BA session on active "
4805 "tid: %d", tid);
4806 break;
4807 }
4808
4809 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
4810 hlid);
4811 if (!ret) {
4812 *ba_bitmap |= BIT(tid);
4813 wl->ba_rx_session_count++;
bbba3e68
LS
4814 }
4815 break;
4816
4817 case IEEE80211_AMPDU_RX_STOP:
0f9c8250 4818 if (!(*ba_bitmap & BIT(tid))) {
c954910b
AN
4819 /*
4820 * this happens on reconfig - so only output a debug
4821 * message for now, and don't fail the function.
4822 */
4823 wl1271_debug(DEBUG_MAC80211,
4824 "no active RX BA session on tid: %d",
0f9c8250 4825 tid);
c954910b 4826 ret = 0;
0f9c8250
AN
4827 break;
4828 }
4829
4830 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
4831 hlid);
4832 if (!ret) {
4833 *ba_bitmap &= ~BIT(tid);
4834 wl->ba_rx_session_count--;
4835 }
bbba3e68
LS
4836 break;
4837
4838 /*
4839 * The BA initiator session management in FW independently.
4840 * Falling break here on purpose for all TX APDU commands.
4841 */
4842 case IEEE80211_AMPDU_TX_START:
4843 case IEEE80211_AMPDU_TX_STOP:
4844 case IEEE80211_AMPDU_TX_OPERATIONAL:
4845 ret = -EINVAL;
4846 break;
4847
4848 default:
4849 wl1271_error("Incorrect ampdu action id=%x\n", action);
4850 ret = -EINVAL;
4851 }
4852
4853 wl1271_ps_elp_sleep(wl);
4854
4855out:
4856 mutex_unlock(&wl->mutex);
4857
4858 return ret;
4859}
4860
af7fbb28
EP
4861static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
4862 struct ieee80211_vif *vif,
4863 const struct cfg80211_bitrate_mask *mask)
4864{
83587505 4865 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
af7fbb28 4866 struct wl1271 *wl = hw->priv;
d6fa37c9 4867 int i, ret = 0;
af7fbb28
EP
4868
4869 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
4870 mask->control[NL80211_BAND_2GHZ].legacy,
4871 mask->control[NL80211_BAND_5GHZ].legacy);
4872
4873 mutex_lock(&wl->mutex);
4874
091185d6 4875 for (i = 0; i < WLCORE_NUM_BANDS; i++)
83587505 4876 wlvif->bitrate_masks[i] =
af7fbb28
EP
4877 wl1271_tx_enabled_rates_get(wl,
4878 mask->control[i].legacy,
4879 i);
d6fa37c9 4880
4cc53383 4881 if (unlikely(wl->state != WLCORE_STATE_ON))
d6fa37c9
EP
4882 goto out;
4883
4884 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4885 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
4886
4887 ret = wl1271_ps_elp_wakeup(wl);
4888 if (ret < 0)
4889 goto out;
4890
4891 wl1271_set_band_rate(wl, wlvif);
4892 wlvif->basic_rate =
4893 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4894 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4895
4896 wl1271_ps_elp_sleep(wl);
4897 }
4898out:
af7fbb28
EP
4899 mutex_unlock(&wl->mutex);
4900
d6fa37c9 4901 return ret;
af7fbb28
EP
4902}
4903
6d158ff3
SL
4904static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
4905 struct ieee80211_channel_switch *ch_switch)
4906{
4907 struct wl1271 *wl = hw->priv;
52630c5d 4908 struct wl12xx_vif *wlvif;
6d158ff3
SL
4909 int ret;
4910
4911 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
4912
b9239b66
AN
4913 wl1271_tx_flush(wl);
4914
6d158ff3
SL
4915 mutex_lock(&wl->mutex);
4916
4cc53383 4917 if (unlikely(wl->state == WLCORE_STATE_OFF)) {
6e8cd331
EP
4918 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4919 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4920 ieee80211_chswitch_done(vif, false);
4921 }
4922 goto out;
4cc53383
IY
4923 } else if (unlikely(wl->state != WLCORE_STATE_ON)) {
4924 goto out;
6d158ff3
SL
4925 }
4926
4927 ret = wl1271_ps_elp_wakeup(wl);
4928 if (ret < 0)
4929 goto out;
4930
52630c5d
EP
4931 /* TODO: change mac80211 to pass vif as param */
4932 wl12xx_for_each_wlvif_sta(wl, wlvif) {
c50a2825
EP
4933 unsigned long delay_usec;
4934
fcab1890 4935 ret = wl->ops->channel_switch(wl, wlvif, ch_switch);
c50a2825
EP
4936 if (ret)
4937 goto out_sleep;
6d158ff3 4938
c50a2825
EP
4939 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
4940
4941 /* indicate failure 5 seconds after channel switch time */
4942 delay_usec = ieee80211_tu_to_usec(wlvif->beacon_int) *
4943 ch_switch->count;
4944 ieee80211_queue_delayed_work(hw, &wlvif->channel_switch_work,
4945 usecs_to_jiffies(delay_usec) +
4946 msecs_to_jiffies(5000));
52630c5d 4947 }
6d158ff3 4948
c50a2825 4949out_sleep:
6d158ff3
SL
4950 wl1271_ps_elp_sleep(wl);
4951
4952out:
4953 mutex_unlock(&wl->mutex);
4954}
4955
d8ae5a25
EP
4956static void wlcore_op_flush(struct ieee80211_hw *hw, bool drop)
4957{
4958 struct wl1271 *wl = hw->priv;
4959
4960 wl1271_tx_flush(wl);
4961}
4962
dabf37db
EP
4963static int wlcore_op_remain_on_channel(struct ieee80211_hw *hw,
4964 struct ieee80211_vif *vif,
4965 struct ieee80211_channel *chan,
dabf37db
EP
4966 int duration)
4967{
4968 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4969 struct wl1271 *wl = hw->priv;
4970 int channel, ret = 0;
4971
4972 channel = ieee80211_frequency_to_channel(chan->center_freq);
4973
4974 wl1271_debug(DEBUG_MAC80211, "mac80211 roc %d (%d)",
4975 channel, wlvif->role_id);
4976
4977 mutex_lock(&wl->mutex);
4978
4979 if (unlikely(wl->state != WLCORE_STATE_ON))
4980 goto out;
4981
4982 /* return EBUSY if we can't ROC right now */
4983 if (WARN_ON(wl->roc_vif ||
4984 find_first_bit(wl->roc_map,
4985 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)) {
4986 ret = -EBUSY;
4987 goto out;
4988 }
4989
4990 ret = wl1271_ps_elp_wakeup(wl);
4991 if (ret < 0)
4992 goto out;
4993
4994 ret = wl12xx_start_dev(wl, wlvif, chan->band, channel);
4995 if (ret < 0)
4996 goto out_sleep;
4997
4998 wl->roc_vif = vif;
4999 ieee80211_queue_delayed_work(hw, &wl->roc_complete_work,
5000 msecs_to_jiffies(duration));
5001out_sleep:
5002 wl1271_ps_elp_sleep(wl);
5003out:
5004 mutex_unlock(&wl->mutex);
5005 return ret;
5006}
5007
5008static int __wlcore_roc_completed(struct wl1271 *wl)
5009{
5010 struct wl12xx_vif *wlvif;
5011 int ret;
5012
5013 /* already completed */
5014 if (unlikely(!wl->roc_vif))
5015 return 0;
5016
5017 wlvif = wl12xx_vif_to_data(wl->roc_vif);
5018
5019 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
5020 return -EBUSY;
5021
5022 ret = wl12xx_stop_dev(wl, wlvif);
5023 if (ret < 0)
5024 return ret;
5025
5026 wl->roc_vif = NULL;
5027
5028 return 0;
5029}
5030
5031static int wlcore_roc_completed(struct wl1271 *wl)
5032{
5033 int ret;
5034
5035 wl1271_debug(DEBUG_MAC80211, "roc complete");
5036
5037 mutex_lock(&wl->mutex);
5038
5039 if (unlikely(wl->state != WLCORE_STATE_ON)) {
5040 ret = -EBUSY;
5041 goto out;
5042 }
5043
5044 ret = wl1271_ps_elp_wakeup(wl);
5045 if (ret < 0)
5046 goto out;
5047
5048 ret = __wlcore_roc_completed(wl);
5049
5050 wl1271_ps_elp_sleep(wl);
5051out:
5052 mutex_unlock(&wl->mutex);
5053
5054 return ret;
5055}
5056
5057static void wlcore_roc_complete_work(struct work_struct *work)
5058{
5059 struct delayed_work *dwork;
5060 struct wl1271 *wl;
5061 int ret;
5062
5063 dwork = container_of(work, struct delayed_work, work);
5064 wl = container_of(dwork, struct wl1271, roc_complete_work);
5065
5066 ret = wlcore_roc_completed(wl);
5067 if (!ret)
5068 ieee80211_remain_on_channel_expired(wl->hw);
5069}
5070
5071static int wlcore_op_cancel_remain_on_channel(struct ieee80211_hw *hw)
5072{
5073 struct wl1271 *wl = hw->priv;
5074
5075 wl1271_debug(DEBUG_MAC80211, "mac80211 croc");
5076
5077 /* TODO: per-vif */
5078 wl1271_tx_flush(wl);
5079
5080 /*
5081 * we can't just flush_work here, because it might deadlock
5082 * (as we might get called from the same workqueue)
5083 */
5084 cancel_delayed_work_sync(&wl->roc_complete_work);
5085 wlcore_roc_completed(wl);
5086
5087 return 0;
5088}
5089
5f9b6777
AN
5090static void wlcore_op_sta_rc_update(struct ieee80211_hw *hw,
5091 struct ieee80211_vif *vif,
5092 struct ieee80211_sta *sta,
5093 u32 changed)
5094{
5095 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5096 struct wl1271 *wl = hw->priv;
5097
5098 wlcore_hw_sta_rc_update(wl, wlvif, sta, changed);
5099}
5100
33437893
AN
5101static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
5102{
5103 struct wl1271 *wl = hw->priv;
5104 bool ret = false;
5105
5106 mutex_lock(&wl->mutex);
5107
4cc53383 5108 if (unlikely(wl->state != WLCORE_STATE_ON))
33437893
AN
5109 goto out;
5110
5111 /* packets are considered pending if in the TX queue or the FW */
f1a46384 5112 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
33437893
AN
5113out:
5114 mutex_unlock(&wl->mutex);
5115
5116 return ret;
5117}
5118
f5fc0f86
LC
5119/* can't be const, mac80211 writes to this */
5120static struct ieee80211_rate wl1271_rates[] = {
5121 { .bitrate = 10,
2b60100b
JO
5122 .hw_value = CONF_HW_BIT_RATE_1MBPS,
5123 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
f5fc0f86 5124 { .bitrate = 20,
2b60100b
JO
5125 .hw_value = CONF_HW_BIT_RATE_2MBPS,
5126 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
f5fc0f86
LC
5127 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5128 { .bitrate = 55,
2b60100b
JO
5129 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
5130 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
f5fc0f86
LC
5131 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5132 { .bitrate = 110,
2b60100b
JO
5133 .hw_value = CONF_HW_BIT_RATE_11MBPS,
5134 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
f5fc0f86
LC
5135 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5136 { .bitrate = 60,
2b60100b
JO
5137 .hw_value = CONF_HW_BIT_RATE_6MBPS,
5138 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
f5fc0f86 5139 { .bitrate = 90,
2b60100b
JO
5140 .hw_value = CONF_HW_BIT_RATE_9MBPS,
5141 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
f5fc0f86 5142 { .bitrate = 120,
2b60100b
JO
5143 .hw_value = CONF_HW_BIT_RATE_12MBPS,
5144 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
f5fc0f86 5145 { .bitrate = 180,
2b60100b
JO
5146 .hw_value = CONF_HW_BIT_RATE_18MBPS,
5147 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
f5fc0f86 5148 { .bitrate = 240,
2b60100b
JO
5149 .hw_value = CONF_HW_BIT_RATE_24MBPS,
5150 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
f5fc0f86 5151 { .bitrate = 360,
2b60100b
JO
5152 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5153 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
f5fc0f86 5154 { .bitrate = 480,
2b60100b
JO
5155 .hw_value = CONF_HW_BIT_RATE_48MBPS,
5156 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
f5fc0f86 5157 { .bitrate = 540,
2b60100b
JO
5158 .hw_value = CONF_HW_BIT_RATE_54MBPS,
5159 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
f5fc0f86
LC
5160};
5161
fa97f46b 5162/* can't be const, mac80211 writes to this */
f5fc0f86 5163static struct ieee80211_channel wl1271_channels[] = {
583f8164
VG
5164 { .hw_value = 1, .center_freq = 2412, .max_power = WLCORE_MAX_TXPWR },
5165 { .hw_value = 2, .center_freq = 2417, .max_power = WLCORE_MAX_TXPWR },
5166 { .hw_value = 3, .center_freq = 2422, .max_power = WLCORE_MAX_TXPWR },
5167 { .hw_value = 4, .center_freq = 2427, .max_power = WLCORE_MAX_TXPWR },
5168 { .hw_value = 5, .center_freq = 2432, .max_power = WLCORE_MAX_TXPWR },
5169 { .hw_value = 6, .center_freq = 2437, .max_power = WLCORE_MAX_TXPWR },
5170 { .hw_value = 7, .center_freq = 2442, .max_power = WLCORE_MAX_TXPWR },
5171 { .hw_value = 8, .center_freq = 2447, .max_power = WLCORE_MAX_TXPWR },
5172 { .hw_value = 9, .center_freq = 2452, .max_power = WLCORE_MAX_TXPWR },
5173 { .hw_value = 10, .center_freq = 2457, .max_power = WLCORE_MAX_TXPWR },
5174 { .hw_value = 11, .center_freq = 2462, .max_power = WLCORE_MAX_TXPWR },
5175 { .hw_value = 12, .center_freq = 2467, .max_power = WLCORE_MAX_TXPWR },
5176 { .hw_value = 13, .center_freq = 2472, .max_power = WLCORE_MAX_TXPWR },
5177 { .hw_value = 14, .center_freq = 2484, .max_power = WLCORE_MAX_TXPWR },
f5fc0f86
LC
5178};
5179
5180/* can't be const, mac80211 writes to this */
5181static struct ieee80211_supported_band wl1271_band_2ghz = {
5182 .channels = wl1271_channels,
5183 .n_channels = ARRAY_SIZE(wl1271_channels),
5184 .bitrates = wl1271_rates,
5185 .n_bitrates = ARRAY_SIZE(wl1271_rates),
5186};
5187
1ebec3d7
TP
5188/* 5 GHz data rates for WL1273 */
5189static struct ieee80211_rate wl1271_rates_5ghz[] = {
5190 { .bitrate = 60,
5191 .hw_value = CONF_HW_BIT_RATE_6MBPS,
5192 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5193 { .bitrate = 90,
5194 .hw_value = CONF_HW_BIT_RATE_9MBPS,
5195 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5196 { .bitrate = 120,
5197 .hw_value = CONF_HW_BIT_RATE_12MBPS,
5198 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5199 { .bitrate = 180,
5200 .hw_value = CONF_HW_BIT_RATE_18MBPS,
5201 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5202 { .bitrate = 240,
5203 .hw_value = CONF_HW_BIT_RATE_24MBPS,
5204 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5205 { .bitrate = 360,
5206 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5207 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5208 { .bitrate = 480,
5209 .hw_value = CONF_HW_BIT_RATE_48MBPS,
5210 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5211 { .bitrate = 540,
5212 .hw_value = CONF_HW_BIT_RATE_54MBPS,
5213 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5214};
5215
fa97f46b 5216/* 5 GHz band channels for WL1273 */
1ebec3d7 5217static struct ieee80211_channel wl1271_channels_5ghz[] = {
583f8164
VG
5218 { .hw_value = 7, .center_freq = 5035, .max_power = WLCORE_MAX_TXPWR },
5219 { .hw_value = 8, .center_freq = 5040, .max_power = WLCORE_MAX_TXPWR },
5220 { .hw_value = 9, .center_freq = 5045, .max_power = WLCORE_MAX_TXPWR },
5221 { .hw_value = 11, .center_freq = 5055, .max_power = WLCORE_MAX_TXPWR },
5222 { .hw_value = 12, .center_freq = 5060, .max_power = WLCORE_MAX_TXPWR },
5223 { .hw_value = 16, .center_freq = 5080, .max_power = WLCORE_MAX_TXPWR },
5224 { .hw_value = 34, .center_freq = 5170, .max_power = WLCORE_MAX_TXPWR },
5225 { .hw_value = 36, .center_freq = 5180, .max_power = WLCORE_MAX_TXPWR },
5226 { .hw_value = 38, .center_freq = 5190, .max_power = WLCORE_MAX_TXPWR },
5227 { .hw_value = 40, .center_freq = 5200, .max_power = WLCORE_MAX_TXPWR },
5228 { .hw_value = 42, .center_freq = 5210, .max_power = WLCORE_MAX_TXPWR },
5229 { .hw_value = 44, .center_freq = 5220, .max_power = WLCORE_MAX_TXPWR },
5230 { .hw_value = 46, .center_freq = 5230, .max_power = WLCORE_MAX_TXPWR },
5231 { .hw_value = 48, .center_freq = 5240, .max_power = WLCORE_MAX_TXPWR },
5232 { .hw_value = 52, .center_freq = 5260, .max_power = WLCORE_MAX_TXPWR },
5233 { .hw_value = 56, .center_freq = 5280, .max_power = WLCORE_MAX_TXPWR },
5234 { .hw_value = 60, .center_freq = 5300, .max_power = WLCORE_MAX_TXPWR },
5235 { .hw_value = 64, .center_freq = 5320, .max_power = WLCORE_MAX_TXPWR },
5236 { .hw_value = 100, .center_freq = 5500, .max_power = WLCORE_MAX_TXPWR },
5237 { .hw_value = 104, .center_freq = 5520, .max_power = WLCORE_MAX_TXPWR },
5238 { .hw_value = 108, .center_freq = 5540, .max_power = WLCORE_MAX_TXPWR },
5239 { .hw_value = 112, .center_freq = 5560, .max_power = WLCORE_MAX_TXPWR },
5240 { .hw_value = 116, .center_freq = 5580, .max_power = WLCORE_MAX_TXPWR },
5241 { .hw_value = 120, .center_freq = 5600, .max_power = WLCORE_MAX_TXPWR },
5242 { .hw_value = 124, .center_freq = 5620, .max_power = WLCORE_MAX_TXPWR },
5243 { .hw_value = 128, .center_freq = 5640, .max_power = WLCORE_MAX_TXPWR },
5244 { .hw_value = 132, .center_freq = 5660, .max_power = WLCORE_MAX_TXPWR },
5245 { .hw_value = 136, .center_freq = 5680, .max_power = WLCORE_MAX_TXPWR },
5246 { .hw_value = 140, .center_freq = 5700, .max_power = WLCORE_MAX_TXPWR },
5247 { .hw_value = 149, .center_freq = 5745, .max_power = WLCORE_MAX_TXPWR },
5248 { .hw_value = 153, .center_freq = 5765, .max_power = WLCORE_MAX_TXPWR },
5249 { .hw_value = 157, .center_freq = 5785, .max_power = WLCORE_MAX_TXPWR },
5250 { .hw_value = 161, .center_freq = 5805, .max_power = WLCORE_MAX_TXPWR },
5251 { .hw_value = 165, .center_freq = 5825, .max_power = WLCORE_MAX_TXPWR },
1ebec3d7
TP
5252};
5253
1ebec3d7
TP
5254static struct ieee80211_supported_band wl1271_band_5ghz = {
5255 .channels = wl1271_channels_5ghz,
5256 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
5257 .bitrates = wl1271_rates_5ghz,
5258 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
f876bb9a
JO
5259};
5260
f5fc0f86
LC
5261static const struct ieee80211_ops wl1271_ops = {
5262 .start = wl1271_op_start,
c24ec83b 5263 .stop = wlcore_op_stop,
f5fc0f86
LC
5264 .add_interface = wl1271_op_add_interface,
5265 .remove_interface = wl1271_op_remove_interface,
c0fad1b7 5266 .change_interface = wl12xx_op_change_interface,
f634a4e7 5267#ifdef CONFIG_PM
402e4861
EP
5268 .suspend = wl1271_op_suspend,
5269 .resume = wl1271_op_resume,
f634a4e7 5270#endif
f5fc0f86 5271 .config = wl1271_op_config,
c87dec9f 5272 .prepare_multicast = wl1271_op_prepare_multicast,
f5fc0f86
LC
5273 .configure_filter = wl1271_op_configure_filter,
5274 .tx = wl1271_op_tx,
a1c597f2 5275 .set_key = wlcore_op_set_key,
f5fc0f86 5276 .hw_scan = wl1271_op_hw_scan,
73ecce31 5277 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
33c2c06c
LC
5278 .sched_scan_start = wl1271_op_sched_scan_start,
5279 .sched_scan_stop = wl1271_op_sched_scan_stop,
f5fc0f86 5280 .bss_info_changed = wl1271_op_bss_info_changed,
68d069c4 5281 .set_frag_threshold = wl1271_op_set_frag_threshold,
f5fc0f86 5282 .set_rts_threshold = wl1271_op_set_rts_threshold,
c6999d83 5283 .conf_tx = wl1271_op_conf_tx,
bbbb538e 5284 .get_tsf = wl1271_op_get_tsf,
ece550d0 5285 .get_survey = wl1271_op_get_survey,
2d6cf2b5 5286 .sta_state = wl12xx_op_sta_state,
bbba3e68 5287 .ampdu_action = wl1271_op_ampdu_action,
33437893 5288 .tx_frames_pending = wl1271_tx_frames_pending,
af7fbb28 5289 .set_bitrate_mask = wl12xx_set_bitrate_mask,
6d158ff3 5290 .channel_switch = wl12xx_op_channel_switch,
d8ae5a25 5291 .flush = wlcore_op_flush,
dabf37db
EP
5292 .remain_on_channel = wlcore_op_remain_on_channel,
5293 .cancel_remain_on_channel = wlcore_op_cancel_remain_on_channel,
b6970ee5
EP
5294 .add_chanctx = wlcore_op_add_chanctx,
5295 .remove_chanctx = wlcore_op_remove_chanctx,
5296 .change_chanctx = wlcore_op_change_chanctx,
5297 .assign_vif_chanctx = wlcore_op_assign_vif_chanctx,
5298 .unassign_vif_chanctx = wlcore_op_unassign_vif_chanctx,
5f9b6777 5299 .sta_rc_update = wlcore_op_sta_rc_update,
c8c90873 5300 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
f5fc0f86
LC
5301};
5302
f876bb9a 5303
43a8bc5a 5304u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum ieee80211_band band)
f876bb9a
JO
5305{
5306 u8 idx;
5307
43a8bc5a 5308 BUG_ON(band >= 2);
f876bb9a 5309
43a8bc5a 5310 if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
f876bb9a
JO
5311 wl1271_error("Illegal RX rate from HW: %d", rate);
5312 return 0;
5313 }
5314
43a8bc5a 5315 idx = wl->band_rate_to_idx[band][rate];
f876bb9a
JO
5316 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
5317 wl1271_error("Unsupported RX rate from HW: %d", rate);
5318 return 0;
5319 }
5320
5321 return idx;
5322}
5323
7fc3a864
JO
5324static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
5325 struct device_attribute *attr,
5326 char *buf)
5327{
5328 struct wl1271 *wl = dev_get_drvdata(dev);
5329 ssize_t len;
5330
2f63b011 5331 len = PAGE_SIZE;
7fc3a864
JO
5332
5333 mutex_lock(&wl->mutex);
5334 len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
5335 wl->sg_enabled);
5336 mutex_unlock(&wl->mutex);
5337
5338 return len;
5339
5340}
5341
5342static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
5343 struct device_attribute *attr,
5344 const char *buf, size_t count)
5345{
5346 struct wl1271 *wl = dev_get_drvdata(dev);
5347 unsigned long res;
5348 int ret;
5349
6277ed65 5350 ret = kstrtoul(buf, 10, &res);
7fc3a864
JO
5351 if (ret < 0) {
5352 wl1271_warning("incorrect value written to bt_coex_mode");
5353 return count;
5354 }
5355
5356 mutex_lock(&wl->mutex);
5357
5358 res = !!res;
5359
5360 if (res == wl->sg_enabled)
5361 goto out;
5362
5363 wl->sg_enabled = res;
5364
4cc53383 5365 if (unlikely(wl->state != WLCORE_STATE_ON))
7fc3a864
JO
5366 goto out;
5367
a620865e 5368 ret = wl1271_ps_elp_wakeup(wl);
7fc3a864
JO
5369 if (ret < 0)
5370 goto out;
5371
5372 wl1271_acx_sg_enable(wl, wl->sg_enabled);
5373 wl1271_ps_elp_sleep(wl);
5374
5375 out:
5376 mutex_unlock(&wl->mutex);
5377 return count;
5378}
5379
5380static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
5381 wl1271_sysfs_show_bt_coex_state,
5382 wl1271_sysfs_store_bt_coex_state);
5383
d717fd61
JO
5384static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
5385 struct device_attribute *attr,
5386 char *buf)
5387{
5388 struct wl1271 *wl = dev_get_drvdata(dev);
5389 ssize_t len;
5390
2f63b011 5391 len = PAGE_SIZE;
d717fd61
JO
5392
5393 mutex_lock(&wl->mutex);
5394 if (wl->hw_pg_ver >= 0)
5395 len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
5396 else
5397 len = snprintf(buf, len, "n/a\n");
5398 mutex_unlock(&wl->mutex);
5399
5400 return len;
5401}
5402
6f07b72a 5403static DEVICE_ATTR(hw_pg_ver, S_IRUGO,
d717fd61
JO
5404 wl1271_sysfs_show_hw_pg_ver, NULL);
5405
95dac04f
IY
5406static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
5407 struct bin_attribute *bin_attr,
5408 char *buffer, loff_t pos, size_t count)
5409{
5410 struct device *dev = container_of(kobj, struct device, kobj);
5411 struct wl1271 *wl = dev_get_drvdata(dev);
5412 ssize_t len;
5413 int ret;
5414
5415 ret = mutex_lock_interruptible(&wl->mutex);
5416 if (ret < 0)
5417 return -ERESTARTSYS;
5418
5419 /* Let only one thread read the log at a time, blocking others */
5420 while (wl->fwlog_size == 0) {
5421 DEFINE_WAIT(wait);
5422
5423 prepare_to_wait_exclusive(&wl->fwlog_waitq,
5424 &wait,
5425 TASK_INTERRUPTIBLE);
5426
5427 if (wl->fwlog_size != 0) {
5428 finish_wait(&wl->fwlog_waitq, &wait);
5429 break;
5430 }
5431
5432 mutex_unlock(&wl->mutex);
5433
5434 schedule();
5435 finish_wait(&wl->fwlog_waitq, &wait);
5436
5437 if (signal_pending(current))
5438 return -ERESTARTSYS;
5439
5440 ret = mutex_lock_interruptible(&wl->mutex);
5441 if (ret < 0)
5442 return -ERESTARTSYS;
5443 }
5444
5445 /* Check if the fwlog is still valid */
5446 if (wl->fwlog_size < 0) {
5447 mutex_unlock(&wl->mutex);
5448 return 0;
5449 }
5450
5451 /* Seeking is not supported - old logs are not kept. Disregard pos. */
5452 len = min(count, (size_t)wl->fwlog_size);
5453 wl->fwlog_size -= len;
5454 memcpy(buffer, wl->fwlog, len);
5455
5456 /* Make room for new messages */
5457 memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
5458
5459 mutex_unlock(&wl->mutex);
5460
5461 return len;
5462}
5463
5464static struct bin_attribute fwlog_attr = {
5465 .attr = {.name = "fwlog", .mode = S_IRUSR},
5466 .read = wl1271_sysfs_read_fwlog,
5467};
5468
f4afbed9 5469static void wl12xx_derive_mac_addresses(struct wl1271 *wl, u32 oui, u32 nic)
5e037e74
LC
5470{
5471 int i;
5472
f4afbed9
AN
5473 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x",
5474 oui, nic);
5e037e74 5475
f4afbed9 5476 if (nic + WLCORE_NUM_MAC_ADDRESSES - wl->num_mac_addr > 0xffffff)
5e037e74
LC
5477 wl1271_warning("NIC part of the MAC address wraps around!");
5478
f4afbed9 5479 for (i = 0; i < wl->num_mac_addr; i++) {
5e037e74
LC
5480 wl->addresses[i].addr[0] = (u8)(oui >> 16);
5481 wl->addresses[i].addr[1] = (u8)(oui >> 8);
5482 wl->addresses[i].addr[2] = (u8) oui;
5483 wl->addresses[i].addr[3] = (u8)(nic >> 16);
5484 wl->addresses[i].addr[4] = (u8)(nic >> 8);
5485 wl->addresses[i].addr[5] = (u8) nic;
5486 nic++;
5487 }
5488
f4afbed9
AN
5489 /* we may be one address short at the most */
5490 WARN_ON(wl->num_mac_addr + 1 < WLCORE_NUM_MAC_ADDRESSES);
5491
5492 /*
5493 * turn on the LAA bit in the first address and use it as
5494 * the last address.
5495 */
5496 if (wl->num_mac_addr < WLCORE_NUM_MAC_ADDRESSES) {
5497 int idx = WLCORE_NUM_MAC_ADDRESSES - 1;
5498 memcpy(&wl->addresses[idx], &wl->addresses[0],
5499 sizeof(wl->addresses[0]));
5500 /* LAA bit */
5501 wl->addresses[idx].addr[2] |= BIT(1);
5502 }
5503
5504 wl->hw->wiphy->n_addresses = WLCORE_NUM_MAC_ADDRESSES;
5e037e74
LC
5505 wl->hw->wiphy->addresses = wl->addresses;
5506}
5507
30c5dbd1
LC
5508static int wl12xx_get_hw_info(struct wl1271 *wl)
5509{
5510 int ret;
30c5dbd1
LC
5511
5512 ret = wl12xx_set_power_on(wl);
5513 if (ret < 0)
4fb4e0be 5514 return ret;
30c5dbd1 5515
6134323f
IY
5516 ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &wl->chip.id);
5517 if (ret < 0)
5518 goto out;
30c5dbd1 5519
00782136
LC
5520 wl->fuse_oui_addr = 0;
5521 wl->fuse_nic_addr = 0;
30c5dbd1 5522
6134323f
IY
5523 ret = wl->ops->get_pg_ver(wl, &wl->hw_pg_ver);
5524 if (ret < 0)
5525 goto out;
30c5dbd1 5526
30d9b4a5 5527 if (wl->ops->get_mac)
6134323f 5528 ret = wl->ops->get_mac(wl);
5e037e74 5529
30c5dbd1 5530out:
6134323f 5531 wl1271_power_off(wl);
30c5dbd1
LC
5532 return ret;
5533}
5534
4b32a2c9 5535static int wl1271_register_hw(struct wl1271 *wl)
f5fc0f86
LC
5536{
5537 int ret;
5e037e74 5538 u32 oui_addr = 0, nic_addr = 0;
f5fc0f86
LC
5539
5540 if (wl->mac80211_registered)
5541 return 0;
5542
6f8d6b20 5543 if (wl->nvs_len >= 12) {
bc765bf3
SL
5544 /* NOTE: The wl->nvs->nvs element must be first, in
5545 * order to simplify the casting, we assume it is at
5546 * the beginning of the wl->nvs structure.
5547 */
5548 u8 *nvs_ptr = (u8 *)wl->nvs;
31d26ec6 5549
5e037e74
LC
5550 oui_addr =
5551 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
5552 nic_addr =
5553 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
5554 }
5555
5556 /* if the MAC address is zeroed in the NVS derive from fuse */
5557 if (oui_addr == 0 && nic_addr == 0) {
5558 oui_addr = wl->fuse_oui_addr;
5559 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
5560 nic_addr = wl->fuse_nic_addr + 1;
31d26ec6
AN
5561 }
5562
f4afbed9 5563 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr);
f5fc0f86
LC
5564
5565 ret = ieee80211_register_hw(wl->hw);
5566 if (ret < 0) {
5567 wl1271_error("unable to register mac80211 hw: %d", ret);
30c5dbd1 5568 goto out;
f5fc0f86
LC
5569 }
5570
5571 wl->mac80211_registered = true;
5572
d60080ae
EP
5573 wl1271_debugfs_init(wl);
5574
f5fc0f86
LC
5575 wl1271_notice("loaded");
5576
30c5dbd1
LC
5577out:
5578 return ret;
f5fc0f86
LC
5579}
5580
4b32a2c9 5581static void wl1271_unregister_hw(struct wl1271 *wl)
3b56dd6a 5582{
3fcdab70 5583 if (wl->plt)
f3df1331 5584 wl1271_plt_stop(wl);
4ae3fa87 5585
3b56dd6a
TP
5586 ieee80211_unregister_hw(wl->hw);
5587 wl->mac80211_registered = false;
5588
5589}
3b56dd6a 5590
bcab320b
EP
5591static const struct ieee80211_iface_limit wlcore_iface_limits[] = {
5592 {
e7a6ba29 5593 .max = 3,
bcab320b
EP
5594 .types = BIT(NL80211_IFTYPE_STATION),
5595 },
5596 {
5597 .max = 1,
5598 .types = BIT(NL80211_IFTYPE_AP) |
5599 BIT(NL80211_IFTYPE_P2P_GO) |
5600 BIT(NL80211_IFTYPE_P2P_CLIENT),
5601 },
5602};
5603
de40750f 5604static struct ieee80211_iface_combination
bcab320b
EP
5605wlcore_iface_combinations[] = {
5606 {
e7a6ba29 5607 .max_interfaces = 3,
bcab320b
EP
5608 .limits = wlcore_iface_limits,
5609 .n_limits = ARRAY_SIZE(wlcore_iface_limits),
5610 },
5611};
5612
4b32a2c9 5613static int wl1271_init_ieee80211(struct wl1271 *wl)
f5fc0f86 5614{
583f8164 5615 int i;
7a55724e
JO
5616 static const u32 cipher_suites[] = {
5617 WLAN_CIPHER_SUITE_WEP40,
5618 WLAN_CIPHER_SUITE_WEP104,
5619 WLAN_CIPHER_SUITE_TKIP,
5620 WLAN_CIPHER_SUITE_CCMP,
5621 WL1271_CIPHER_SUITE_GEM,
5622 };
5623
2c0133a4
AN
5624 /* The tx descriptor buffer */
5625 wl->hw->extra_tx_headroom = sizeof(struct wl1271_tx_hw_descr);
5626
5627 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
5628 wl->hw->extra_tx_headroom += WL1271_EXTRA_SPACE_TKIP;
f5fc0f86
LC
5629
5630 /* unit us */
5631 /* FIXME: find a proper value */
5632 wl->hw->channel_change_time = 10000;
50c500ad 5633 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
f5fc0f86
LC
5634
5635 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
0a34332f 5636 IEEE80211_HW_SUPPORTS_PS |
f1d63a59 5637 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
4695dc91 5638 IEEE80211_HW_SUPPORTS_UAPSD |
a9af092b 5639 IEEE80211_HW_HAS_RATE_CONTROL |
00236aed 5640 IEEE80211_HW_CONNECTION_MONITOR |
25eaea30 5641 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
fcd23b63 5642 IEEE80211_HW_SPECTRUM_MGMT |
93f8c8e0
AN
5643 IEEE80211_HW_AP_LINK_PS |
5644 IEEE80211_HW_AMPDU_AGGREGATION |
79aba1ba 5645 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
1c33db78
AN
5646 IEEE80211_HW_SCAN_WHILE_IDLE |
5647 IEEE80211_HW_QUEUE_CONTROL;
f5fc0f86 5648
7a55724e
JO
5649 wl->hw->wiphy->cipher_suites = cipher_suites;
5650 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5651
e0d8bbf0 5652 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
045c745f
EP
5653 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
5654 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
f5fc0f86 5655 wl->hw->wiphy->max_scan_ssids = 1;
221737d2
LC
5656 wl->hw->wiphy->max_sched_scan_ssids = 16;
5657 wl->hw->wiphy->max_match_sets = 16;
ea559b46
GE
5658 /*
5659 * Maximum length of elements in scanning probe request templates
5660 * should be the maximum length possible for a template, without
5661 * the IEEE80211 header of the template
5662 */
c08e371a 5663 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
ea559b46 5664 sizeof(struct ieee80211_header);
a8aaaf53 5665
c08e371a 5666 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
c9e79a47
LC
5667 sizeof(struct ieee80211_header);
5668
dabf37db
EP
5669 wl->hw->wiphy->max_remain_on_channel_duration = 5000;
5670
81ddbb5c
JB
5671 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD |
5672 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
1ec23f7f 5673
4a31c11c
LC
5674 /* make sure all our channels fit in the scanned_ch bitmask */
5675 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5676 ARRAY_SIZE(wl1271_channels_5ghz) >
5677 WL1271_MAX_CHANNELS);
583f8164
VG
5678 /*
5679 * clear channel flags from the previous usage
5680 * and restore max_power & max_antenna_gain values.
5681 */
5682 for (i = 0; i < ARRAY_SIZE(wl1271_channels); i++) {
5683 wl1271_band_2ghz.channels[i].flags = 0;
5684 wl1271_band_2ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
5685 wl1271_band_2ghz.channels[i].max_antenna_gain = 0;
5686 }
5687
5688 for (i = 0; i < ARRAY_SIZE(wl1271_channels_5ghz); i++) {
5689 wl1271_band_5ghz.channels[i].flags = 0;
5690 wl1271_band_5ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
5691 wl1271_band_5ghz.channels[i].max_antenna_gain = 0;
5692 }
5693
a8aaaf53
LC
5694 /*
5695 * We keep local copies of the band structs because we need to
5696 * modify them on a per-device basis.
5697 */
5698 memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5699 sizeof(wl1271_band_2ghz));
bfb92ca1
EP
5700 memcpy(&wl->bands[IEEE80211_BAND_2GHZ].ht_cap,
5701 &wl->ht_cap[IEEE80211_BAND_2GHZ],
5702 sizeof(*wl->ht_cap));
a8aaaf53
LC
5703 memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5704 sizeof(wl1271_band_5ghz));
bfb92ca1
EP
5705 memcpy(&wl->bands[IEEE80211_BAND_5GHZ].ht_cap,
5706 &wl->ht_cap[IEEE80211_BAND_5GHZ],
5707 sizeof(*wl->ht_cap));
a8aaaf53
LC
5708
5709 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5710 &wl->bands[IEEE80211_BAND_2GHZ];
5711 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5712 &wl->bands[IEEE80211_BAND_5GHZ];
1ebec3d7 5713
1c33db78
AN
5714 /*
5715 * allow 4 queues per mac address we support +
5716 * 1 cab queue per mac + one global offchannel Tx queue
5717 */
5718 wl->hw->queues = (NUM_TX_QUEUES + 1) * WLCORE_NUM_MAC_ADDRESSES + 1;
5719
5720 /* the last queue is the offchannel queue */
5721 wl->hw->offchannel_tx_hw_queue = wl->hw->queues - 1;
31627dc5 5722 wl->hw->max_rates = 1;
12bd8949 5723
b7417d93
JO
5724 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5725
9c1b190b
AN
5726 /* the FW answers probe-requests in AP-mode */
5727 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5728 wl->hw->wiphy->probe_resp_offload =
5729 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5730 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5731 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5732
bcab320b 5733 /* allowed interface combinations */
de40750f 5734 wlcore_iface_combinations[0].num_different_channels = wl->num_channels;
bcab320b
EP
5735 wl->hw->wiphy->iface_combinations = wlcore_iface_combinations;
5736 wl->hw->wiphy->n_iface_combinations =
5737 ARRAY_SIZE(wlcore_iface_combinations);
5738
a390e85c 5739 SET_IEEE80211_DEV(wl->hw, wl->dev);
f5fc0f86 5740
f84f7d78 5741 wl->hw->sta_data_size = sizeof(struct wl1271_station);
87fbcb0f 5742 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
f84f7d78 5743
ba421f8f 5744 wl->hw->max_rx_aggregation_subframes = wl->conf.ht.rx_ba_win_size;
4c9cfa78 5745
f5fc0f86
LC
5746 return 0;
5747}
5748
f5fc0f86 5749#define WL1271_DEFAULT_CHANNEL 0
c332a4b8 5750
c50a2825
EP
5751struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size, u32 aggr_buf_size,
5752 u32 mbox_size)
f5fc0f86 5753{
f5fc0f86
LC
5754 struct ieee80211_hw *hw;
5755 struct wl1271 *wl;
a8c0ddb5 5756 int i, j, ret;
1f37cbc9 5757 unsigned int order;
f5fc0f86 5758
c7ffb902 5759 BUILD_BUG_ON(AP_MAX_STATIONS > WL12XX_MAX_LINKS);
f80c2d12 5760
f5fc0f86
LC
5761 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5762 if (!hw) {
5763 wl1271_error("could not alloc ieee80211_hw");
a1dd8187 5764 ret = -ENOMEM;
3b56dd6a
TP
5765 goto err_hw_alloc;
5766 }
5767
f5fc0f86
LC
5768 wl = hw->priv;
5769 memset(wl, 0, sizeof(*wl));
5770
96e0c683
AN
5771 wl->priv = kzalloc(priv_size, GFP_KERNEL);
5772 if (!wl->priv) {
5773 wl1271_error("could not alloc wl priv");
5774 ret = -ENOMEM;
5775 goto err_priv_alloc;
5776 }
5777
87627214 5778 INIT_LIST_HEAD(&wl->wlvif_list);
01c09162 5779
f5fc0f86 5780 wl->hw = hw;
f5fc0f86 5781
a8c0ddb5 5782 for (i = 0; i < NUM_TX_QUEUES; i++)
c7ffb902 5783 for (j = 0; j < WL12XX_MAX_LINKS; j++)
a8c0ddb5
AN
5784 skb_queue_head_init(&wl->links[j].tx_queue[i]);
5785
a620865e
IY
5786 skb_queue_head_init(&wl->deferred_rx_queue);
5787 skb_queue_head_init(&wl->deferred_tx_queue);
5788
37b70a81 5789 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
a620865e 5790 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
117b38d0
JO
5791 INIT_WORK(&wl->tx_work, wl1271_tx_work);
5792 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5793 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
dabf37db 5794 INIT_DELAYED_WORK(&wl->roc_complete_work, wlcore_roc_complete_work);
55df5afb 5795 INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
77ddaa10 5796
92ef8960
EP
5797 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5798 if (!wl->freezable_wq) {
5799 ret = -ENOMEM;
5800 goto err_hw;
5801 }
5802
f5fc0f86 5803 wl->channel = WL1271_DEFAULT_CHANNEL;
f5fc0f86 5804 wl->rx_counter = 0;
f5fc0f86 5805 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
8a5a37a6 5806 wl->band = IEEE80211_BAND_2GHZ;
83d08d3f 5807 wl->channel_type = NL80211_CHAN_NO_HT;
830fb67b 5808 wl->flags = 0;
7fc3a864 5809 wl->sg_enabled = true;
66340e5b 5810 wl->sleep_auth = WL1271_PSM_ILLEGAL;
c108c905 5811 wl->recovery_count = 0;
d717fd61 5812 wl->hw_pg_ver = -1;
b622d992
AN
5813 wl->ap_ps_map = 0;
5814 wl->ap_fw_ps_map = 0;
606ea9fa 5815 wl->quirks = 0;
341b7cde 5816 wl->platform_quirks = 0;
f4df1bd5 5817 wl->system_hlid = WL12XX_SYSTEM_HLID;
da03209e 5818 wl->active_sta_count = 0;
9a100968 5819 wl->active_link_count = 0;
95dac04f
IY
5820 wl->fwlog_size = 0;
5821 init_waitqueue_head(&wl->fwlog_waitq);
f5fc0f86 5822
f4df1bd5
EP
5823 /* The system link is always allocated */
5824 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5825
25eeb9e3 5826 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
72b0624f 5827 for (i = 0; i < wl->num_tx_desc; i++)
f5fc0f86
LC
5828 wl->tx_frames[i] = NULL;
5829
5830 spin_lock_init(&wl->wl_lock);
5831
4cc53383 5832 wl->state = WLCORE_STATE_OFF;
3fcdab70 5833 wl->fw_type = WL12XX_FW_TYPE_NONE;
f5fc0f86 5834 mutex_init(&wl->mutex);
2c38849f 5835 mutex_init(&wl->flush_mutex);
6f8d6b20 5836 init_completion(&wl->nvs_loading_complete);
f5fc0f86 5837
26a309c7 5838 order = get_order(aggr_buf_size);
1f37cbc9
IY
5839 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5840 if (!wl->aggr_buf) {
5841 ret = -ENOMEM;
92ef8960 5842 goto err_wq;
1f37cbc9 5843 }
26a309c7 5844 wl->aggr_buf_size = aggr_buf_size;
1f37cbc9 5845
990f5de7
IY
5846 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5847 if (!wl->dummy_packet) {
5848 ret = -ENOMEM;
5849 goto err_aggr;
5850 }
5851
95dac04f
IY
5852 /* Allocate one page for the FW log */
5853 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5854 if (!wl->fwlog) {
5855 ret = -ENOMEM;
5856 goto err_dummy_packet;
5857 }
5858
c50a2825
EP
5859 wl->mbox_size = mbox_size;
5860 wl->mbox = kmalloc(wl->mbox_size, GFP_KERNEL | GFP_DMA);
690142e9
MG
5861 if (!wl->mbox) {
5862 ret = -ENOMEM;
5863 goto err_fwlog;
5864 }
5865
c332a4b8 5866 return hw;
a1dd8187 5867
690142e9
MG
5868err_fwlog:
5869 free_page((unsigned long)wl->fwlog);
5870
990f5de7
IY
5871err_dummy_packet:
5872 dev_kfree_skb(wl->dummy_packet);
5873
1f37cbc9
IY
5874err_aggr:
5875 free_pages((unsigned long)wl->aggr_buf, order);
5876
92ef8960
EP
5877err_wq:
5878 destroy_workqueue(wl->freezable_wq);
5879
a1dd8187 5880err_hw:
3b56dd6a 5881 wl1271_debugfs_exit(wl);
96e0c683
AN
5882 kfree(wl->priv);
5883
5884err_priv_alloc:
3b56dd6a
TP
5885 ieee80211_free_hw(hw);
5886
5887err_hw_alloc:
a1dd8187 5888
a1dd8187 5889 return ERR_PTR(ret);
c332a4b8 5890}
ffeb501c 5891EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
c332a4b8 5892
ffeb501c 5893int wlcore_free_hw(struct wl1271 *wl)
c332a4b8 5894{
95dac04f
IY
5895 /* Unblock any fwlog readers */
5896 mutex_lock(&wl->mutex);
5897 wl->fwlog_size = -1;
5898 wake_up_interruptible_all(&wl->fwlog_waitq);
5899 mutex_unlock(&wl->mutex);
5900
f79f890c 5901 device_remove_bin_file(wl->dev, &fwlog_attr);
6f07b72a 5902
f79f890c 5903 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
6f07b72a 5904
f79f890c 5905 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
a8e27820 5906 kfree(wl->mbox);
95dac04f 5907 free_page((unsigned long)wl->fwlog);
990f5de7 5908 dev_kfree_skb(wl->dummy_packet);
26a309c7 5909 free_pages((unsigned long)wl->aggr_buf, get_order(wl->aggr_buf_size));
c332a4b8
TP
5910
5911 wl1271_debugfs_exit(wl);
5912
c332a4b8
TP
5913 vfree(wl->fw);
5914 wl->fw = NULL;
3fcdab70 5915 wl->fw_type = WL12XX_FW_TYPE_NONE;
c332a4b8
TP
5916 kfree(wl->nvs);
5917 wl->nvs = NULL;
5918
0afd04e5 5919 kfree(wl->fw_status_1);
c332a4b8 5920 kfree(wl->tx_res_if);
92ef8960 5921 destroy_workqueue(wl->freezable_wq);
c332a4b8 5922
96e0c683 5923 kfree(wl->priv);
c332a4b8
TP
5924 ieee80211_free_hw(wl->hw);
5925
5926 return 0;
5927}
ffeb501c 5928EXPORT_SYMBOL_GPL(wlcore_free_hw);
50b3eb4b 5929
a390e85c
FB
5930static irqreturn_t wl12xx_hardirq(int irq, void *cookie)
5931{
5932 struct wl1271 *wl = cookie;
5933 unsigned long flags;
5934
5935 wl1271_debug(DEBUG_IRQ, "IRQ");
5936
5937 /* complete the ELP completion */
5938 spin_lock_irqsave(&wl->wl_lock, flags);
5939 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
5940 if (wl->elp_compl) {
5941 complete(wl->elp_compl);
5942 wl->elp_compl = NULL;
5943 }
5944
5945 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
5946 /* don't enqueue a work right now. mark it as pending */
5947 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
5948 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
5949 disable_irq_nosync(wl->irq);
5950 pm_wakeup_event(wl->dev, 0);
5951 spin_unlock_irqrestore(&wl->wl_lock, flags);
5952 return IRQ_HANDLED;
5953 }
5954 spin_unlock_irqrestore(&wl->wl_lock, flags);
5955
5956 return IRQ_WAKE_THREAD;
5957}
5958
6f8d6b20 5959static void wlcore_nvs_cb(const struct firmware *fw, void *context)
ce2a217c 5960{
6f8d6b20
IY
5961 struct wl1271 *wl = context;
5962 struct platform_device *pdev = wl->pdev;
a390e85c 5963 struct wl12xx_platform_data *pdata = pdev->dev.platform_data;
a390e85c 5964 unsigned long irqflags;
ffeb501c 5965 int ret;
a390e85c 5966
6f8d6b20
IY
5967 if (fw) {
5968 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
5969 if (!wl->nvs) {
5970 wl1271_error("Could not allocate nvs data");
5971 goto out;
5972 }
5973 wl->nvs_len = fw->size;
5974 } else {
5975 wl1271_debug(DEBUG_BOOT, "Could not get nvs file %s",
5976 WL12XX_NVS_NAME);
5977 wl->nvs = NULL;
5978 wl->nvs_len = 0;
a390e85c
FB
5979 }
5980
3992eb2b
IY
5981 ret = wl->ops->setup(wl);
5982 if (ret < 0)
6f8d6b20 5983 goto out_free_nvs;
3992eb2b 5984
72b0624f
AN
5985 BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
5986
e87288f0
LC
5987 /* adjust some runtime configuration parameters */
5988 wlcore_adjust_conf(wl);
5989
a390e85c 5990 wl->irq = platform_get_irq(pdev, 0);
a390e85c
FB
5991 wl->platform_quirks = pdata->platform_quirks;
5992 wl->set_power = pdata->set_power;
a390e85c
FB
5993 wl->if_ops = pdata->ops;
5994
a390e85c
FB
5995 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
5996 irqflags = IRQF_TRIGGER_RISING;
5997 else
5998 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
5999
b5b45b3c 6000 ret = request_threaded_irq(wl->irq, wl12xx_hardirq, wlcore_irq,
a390e85c
FB
6001 irqflags,
6002 pdev->name, wl);
6003 if (ret < 0) {
6004 wl1271_error("request_irq() failed: %d", ret);
6f8d6b20 6005 goto out_free_nvs;
a390e85c
FB
6006 }
6007
dfb89c56 6008#ifdef CONFIG_PM
a390e85c
FB
6009 ret = enable_irq_wake(wl->irq);
6010 if (!ret) {
6011 wl->irq_wake_enabled = true;
6012 device_init_wakeup(wl->dev, 1);
b95d7cef 6013 if (pdata->pwr_in_suspend) {
ffeb501c 6014 wl->hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
b95d7cef
ES
6015 wl->hw->wiphy->wowlan.n_patterns =
6016 WL1271_MAX_RX_FILTERS;
6017 wl->hw->wiphy->wowlan.pattern_min_len = 1;
6018 wl->hw->wiphy->wowlan.pattern_max_len =
6019 WL1271_RX_FILTER_MAX_PATTERN_SIZE;
6020 }
a390e85c 6021 }
dfb89c56 6022#endif
a390e85c
FB
6023 disable_irq(wl->irq);
6024
4afc37a0
LC
6025 ret = wl12xx_get_hw_info(wl);
6026 if (ret < 0) {
6027 wl1271_error("couldn't get hw info");
8b425e62 6028 goto out_irq;
4afc37a0
LC
6029 }
6030
6031 ret = wl->ops->identify_chip(wl);
6032 if (ret < 0)
8b425e62 6033 goto out_irq;
4afc37a0 6034
a390e85c
FB
6035 ret = wl1271_init_ieee80211(wl);
6036 if (ret)
6037 goto out_irq;
6038
6039 ret = wl1271_register_hw(wl);
6040 if (ret)
6041 goto out_irq;
6042
f79f890c
FB
6043 /* Create sysfs file to control bt coex state */
6044 ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
6045 if (ret < 0) {
6046 wl1271_error("failed to create sysfs file bt_coex_state");
8b425e62 6047 goto out_unreg;
f79f890c
FB
6048 }
6049
6050 /* Create sysfs file to get HW PG version */
6051 ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
6052 if (ret < 0) {
6053 wl1271_error("failed to create sysfs file hw_pg_ver");
6054 goto out_bt_coex_state;
6055 }
6056
6057 /* Create sysfs file for the FW log */
6058 ret = device_create_bin_file(wl->dev, &fwlog_attr);
6059 if (ret < 0) {
6060 wl1271_error("failed to create sysfs file fwlog");
6061 goto out_hw_pg_ver;
6062 }
6063
6f8d6b20 6064 wl->initialized = true;
ffeb501c 6065 goto out;
a390e85c 6066
f79f890c
FB
6067out_hw_pg_ver:
6068 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
6069
6070out_bt_coex_state:
6071 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
6072
8b425e62
LC
6073out_unreg:
6074 wl1271_unregister_hw(wl);
6075
a390e85c
FB
6076out_irq:
6077 free_irq(wl->irq, wl);
6078
6f8d6b20
IY
6079out_free_nvs:
6080 kfree(wl->nvs);
6081
a390e85c 6082out:
6f8d6b20
IY
6083 release_firmware(fw);
6084 complete_all(&wl->nvs_loading_complete);
6085}
6086
6087int __devinit wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
6088{
6089 int ret;
6090
6091 if (!wl->ops || !wl->ptable)
6092 return -EINVAL;
6093
6094 wl->dev = &pdev->dev;
6095 wl->pdev = pdev;
6096 platform_set_drvdata(pdev, wl);
6097
6098 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
6099 WL12XX_NVS_NAME, &pdev->dev, GFP_KERNEL,
6100 wl, wlcore_nvs_cb);
6101 if (ret < 0) {
6102 wl1271_error("request_firmware_nowait failed: %d", ret);
6103 complete_all(&wl->nvs_loading_complete);
6104 }
6105
a390e85c 6106 return ret;
ce2a217c 6107}
b2ba99ff 6108EXPORT_SYMBOL_GPL(wlcore_probe);
ce2a217c 6109
b2ba99ff 6110int __devexit wlcore_remove(struct platform_device *pdev)
ce2a217c 6111{
a390e85c
FB
6112 struct wl1271 *wl = platform_get_drvdata(pdev);
6113
6f8d6b20
IY
6114 wait_for_completion(&wl->nvs_loading_complete);
6115 if (!wl->initialized)
6116 return 0;
6117
a390e85c
FB
6118 if (wl->irq_wake_enabled) {
6119 device_init_wakeup(wl->dev, 0);
6120 disable_irq_wake(wl->irq);
6121 }
6122 wl1271_unregister_hw(wl);
6123 free_irq(wl->irq, wl);
ffeb501c 6124 wlcore_free_hw(wl);
a390e85c 6125
ce2a217c
FB
6126 return 0;
6127}
b2ba99ff 6128EXPORT_SYMBOL_GPL(wlcore_remove);
ce2a217c 6129
491bbd6b 6130u32 wl12xx_debug_level = DEBUG_NONE;
17c1755c 6131EXPORT_SYMBOL_GPL(wl12xx_debug_level);
491bbd6b 6132module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
17c1755c
EP
6133MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
6134
95dac04f 6135module_param_named(fwlog, fwlog_param, charp, 0);
2c882fa4 6136MODULE_PARM_DESC(fwlog,
95dac04f
IY
6137 "FW logger options: continuous, ondemand, dbgpins or disable");
6138
7230341f 6139module_param(bug_on_recovery, int, S_IRUSR | S_IWUSR);
2a5bff09
EP
6140MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
6141
7230341f 6142module_param(no_recovery, int, S_IRUSR | S_IWUSR);
34785be5
AN
6143MODULE_PARM_DESC(no_recovery, "Prevent HW recovery. FW will remain stuck.");
6144
50b3eb4b 6145MODULE_LICENSE("GPL");
b1a48cab 6146MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
50b3eb4b 6147MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
0635ad45 6148MODULE_FIRMWARE(WL12XX_NVS_NAME);