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