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