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