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