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