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