mm/zsmalloc: allocate exactly size of struct zs_pool
[linux/fpc-iii.git] / drivers / net / wireless / ti / wlcore / main.c
blob6ad3fcedab9b29fa92c9fb37eb65037a5b6d6f0f
2 /*
3 * This file is part of wlcore
5 * Copyright (C) 2008-2010 Nokia Corporation
6 * Copyright (C) 2011-2013 Texas Instruments Inc.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
24 #include <linux/module.h>
25 #include <linux/firmware.h>
26 #include <linux/etherdevice.h>
27 #include <linux/vmalloc.h>
28 #include <linux/wl12xx.h>
29 #include <linux/interrupt.h>
31 #include "wlcore.h"
32 #include "debug.h"
33 #include "wl12xx_80211.h"
34 #include "io.h"
35 #include "tx.h"
36 #include "ps.h"
37 #include "init.h"
38 #include "debugfs.h"
39 #include "testmode.h"
40 #include "vendor_cmd.h"
41 #include "scan.h"
42 #include "hw_ops.h"
43 #include "sysfs.h"
45 #define WL1271_BOOT_RETRIES 3
47 static char *fwlog_param;
48 static int fwlog_mem_blocks = -1;
49 static int bug_on_recovery = -1;
50 static int no_recovery = -1;
52 static void __wl1271_op_remove_interface(struct wl1271 *wl,
53 struct ieee80211_vif *vif,
54 bool reset_tx_queues);
55 static void wlcore_op_stop_locked(struct wl1271 *wl);
56 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif);
58 static int wl12xx_set_authorized(struct wl1271 *wl, struct wl12xx_vif *wlvif)
60 int ret;
62 if (WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS))
63 return -EINVAL;
65 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
66 return 0;
68 if (test_and_set_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags))
69 return 0;
71 ret = wl12xx_cmd_set_peer_state(wl, wlvif, wlvif->sta.hlid);
72 if (ret < 0)
73 return ret;
75 wl1271_info("Association completed.");
76 return 0;
79 static void wl1271_reg_notify(struct wiphy *wiphy,
80 struct regulatory_request *request)
82 struct ieee80211_supported_band *band;
83 struct ieee80211_channel *ch;
84 int i;
85 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
86 struct wl1271 *wl = hw->priv;
88 band = wiphy->bands[IEEE80211_BAND_5GHZ];
89 for (i = 0; i < band->n_channels; i++) {
90 ch = &band->channels[i];
91 if (ch->flags & IEEE80211_CHAN_DISABLED)
92 continue;
94 if (ch->flags & IEEE80211_CHAN_RADAR)
95 ch->flags |= IEEE80211_CHAN_NO_IR;
99 wlcore_regdomain_config(wl);
102 static int wl1271_set_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif,
103 bool enable)
105 int ret = 0;
107 /* we should hold wl->mutex */
108 ret = wl1271_acx_ps_rx_streaming(wl, wlvif, enable);
109 if (ret < 0)
110 goto out;
112 if (enable)
113 set_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
114 else
115 clear_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
116 out:
117 return ret;
121 * this function is being called when the rx_streaming interval
122 * has beed changed or rx_streaming should be disabled
124 int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif)
126 int ret = 0;
127 int period = wl->conf.rx_streaming.interval;
129 /* don't reconfigure if rx_streaming is disabled */
130 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
131 goto out;
133 /* reconfigure/disable according to new streaming_period */
134 if (period &&
135 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
136 (wl->conf.rx_streaming.always ||
137 test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
138 ret = wl1271_set_rx_streaming(wl, wlvif, true);
139 else {
140 ret = wl1271_set_rx_streaming(wl, wlvif, false);
141 /* don't cancel_work_sync since we might deadlock */
142 del_timer_sync(&wlvif->rx_streaming_timer);
144 out:
145 return ret;
148 static void wl1271_rx_streaming_enable_work(struct work_struct *work)
150 int ret;
151 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
152 rx_streaming_enable_work);
153 struct wl1271 *wl = wlvif->wl;
155 mutex_lock(&wl->mutex);
157 if (test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags) ||
158 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
159 (!wl->conf.rx_streaming.always &&
160 !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
161 goto out;
163 if (!wl->conf.rx_streaming.interval)
164 goto out;
166 ret = wl1271_ps_elp_wakeup(wl);
167 if (ret < 0)
168 goto out;
170 ret = wl1271_set_rx_streaming(wl, wlvif, true);
171 if (ret < 0)
172 goto out_sleep;
174 /* stop it after some time of inactivity */
175 mod_timer(&wlvif->rx_streaming_timer,
176 jiffies + msecs_to_jiffies(wl->conf.rx_streaming.duration));
178 out_sleep:
179 wl1271_ps_elp_sleep(wl);
180 out:
181 mutex_unlock(&wl->mutex);
184 static void wl1271_rx_streaming_disable_work(struct work_struct *work)
186 int ret;
187 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
188 rx_streaming_disable_work);
189 struct wl1271 *wl = wlvif->wl;
191 mutex_lock(&wl->mutex);
193 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
194 goto out;
196 ret = wl1271_ps_elp_wakeup(wl);
197 if (ret < 0)
198 goto out;
200 ret = wl1271_set_rx_streaming(wl, wlvif, false);
201 if (ret)
202 goto out_sleep;
204 out_sleep:
205 wl1271_ps_elp_sleep(wl);
206 out:
207 mutex_unlock(&wl->mutex);
210 static void wl1271_rx_streaming_timer(unsigned long data)
212 struct wl12xx_vif *wlvif = (struct wl12xx_vif *)data;
213 struct wl1271 *wl = wlvif->wl;
214 ieee80211_queue_work(wl->hw, &wlvif->rx_streaming_disable_work);
217 /* wl->mutex must be taken */
218 void wl12xx_rearm_tx_watchdog_locked(struct wl1271 *wl)
220 /* if the watchdog is not armed, don't do anything */
221 if (wl->tx_allocated_blocks == 0)
222 return;
224 cancel_delayed_work(&wl->tx_watchdog_work);
225 ieee80211_queue_delayed_work(wl->hw, &wl->tx_watchdog_work,
226 msecs_to_jiffies(wl->conf.tx.tx_watchdog_timeout));
229 static void wl12xx_tx_watchdog_work(struct work_struct *work)
231 struct delayed_work *dwork;
232 struct wl1271 *wl;
234 dwork = container_of(work, struct delayed_work, work);
235 wl = container_of(dwork, struct wl1271, tx_watchdog_work);
237 mutex_lock(&wl->mutex);
239 if (unlikely(wl->state != WLCORE_STATE_ON))
240 goto out;
242 /* Tx went out in the meantime - everything is ok */
243 if (unlikely(wl->tx_allocated_blocks == 0))
244 goto out;
247 * if a ROC is in progress, we might not have any Tx for a long
248 * time (e.g. pending Tx on the non-ROC channels)
250 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
251 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to ROC",
252 wl->conf.tx.tx_watchdog_timeout);
253 wl12xx_rearm_tx_watchdog_locked(wl);
254 goto out;
258 * if a scan is in progress, we might not have any Tx for a long
259 * time
261 if (wl->scan.state != WL1271_SCAN_STATE_IDLE) {
262 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to scan",
263 wl->conf.tx.tx_watchdog_timeout);
264 wl12xx_rearm_tx_watchdog_locked(wl);
265 goto out;
269 * AP might cache a frame for a long time for a sleeping station,
270 * so rearm the timer if there's an AP interface with stations. If
271 * Tx is genuinely stuck we will most hopefully discover it when all
272 * stations are removed due to inactivity.
274 if (wl->active_sta_count) {
275 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms. AP has "
276 " %d stations",
277 wl->conf.tx.tx_watchdog_timeout,
278 wl->active_sta_count);
279 wl12xx_rearm_tx_watchdog_locked(wl);
280 goto out;
283 wl1271_error("Tx stuck (in FW) for %d ms. Starting recovery",
284 wl->conf.tx.tx_watchdog_timeout);
285 wl12xx_queue_recovery_work(wl);
287 out:
288 mutex_unlock(&wl->mutex);
291 static void wlcore_adjust_conf(struct wl1271 *wl)
293 /* Adjust settings according to optional module parameters */
295 /* Firmware Logger params */
296 if (fwlog_mem_blocks != -1) {
297 if (fwlog_mem_blocks >= CONF_FWLOG_MIN_MEM_BLOCKS &&
298 fwlog_mem_blocks <= CONF_FWLOG_MAX_MEM_BLOCKS) {
299 wl->conf.fwlog.mem_blocks = fwlog_mem_blocks;
300 } else {
301 wl1271_error(
302 "Illegal fwlog_mem_blocks=%d using default %d",
303 fwlog_mem_blocks, wl->conf.fwlog.mem_blocks);
307 if (fwlog_param) {
308 if (!strcmp(fwlog_param, "continuous")) {
309 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
310 } else if (!strcmp(fwlog_param, "ondemand")) {
311 wl->conf.fwlog.mode = WL12XX_FWLOG_ON_DEMAND;
312 } else if (!strcmp(fwlog_param, "dbgpins")) {
313 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
314 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_DBG_PINS;
315 } else if (!strcmp(fwlog_param, "disable")) {
316 wl->conf.fwlog.mem_blocks = 0;
317 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_NONE;
318 } else {
319 wl1271_error("Unknown fwlog parameter %s", fwlog_param);
323 if (bug_on_recovery != -1)
324 wl->conf.recovery.bug_on_recovery = (u8) bug_on_recovery;
326 if (no_recovery != -1)
327 wl->conf.recovery.no_recovery = (u8) no_recovery;
330 static void wl12xx_irq_ps_regulate_link(struct wl1271 *wl,
331 struct wl12xx_vif *wlvif,
332 u8 hlid, u8 tx_pkts)
334 bool fw_ps;
336 fw_ps = test_bit(hlid, &wl->ap_fw_ps_map);
339 * Wake up from high level PS if the STA is asleep with too little
340 * packets in FW or if the STA is awake.
342 if (!fw_ps || tx_pkts < WL1271_PS_STA_MAX_PACKETS)
343 wl12xx_ps_link_end(wl, wlvif, hlid);
346 * Start high-level PS if the STA is asleep with enough blocks in FW.
347 * Make an exception if this is the only connected link. In this
348 * case FW-memory congestion is less of a problem.
349 * Note that a single connected STA means 2*ap_count + 1 active links,
350 * since we must account for the global and broadcast AP links
351 * for each AP. The "fw_ps" check assures us the other link is a STA
352 * connected to the AP. Otherwise the FW would not set the PSM bit.
354 else if (wl->active_link_count > (wl->ap_count*2 + 1) && fw_ps &&
355 tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
356 wl12xx_ps_link_start(wl, wlvif, hlid, true);
359 static void wl12xx_irq_update_links_status(struct wl1271 *wl,
360 struct wl12xx_vif *wlvif,
361 struct wl_fw_status *status)
363 unsigned long cur_fw_ps_map;
364 u8 hlid;
366 cur_fw_ps_map = status->link_ps_bitmap;
367 if (wl->ap_fw_ps_map != cur_fw_ps_map) {
368 wl1271_debug(DEBUG_PSM,
369 "link ps prev 0x%lx cur 0x%lx changed 0x%lx",
370 wl->ap_fw_ps_map, cur_fw_ps_map,
371 wl->ap_fw_ps_map ^ cur_fw_ps_map);
373 wl->ap_fw_ps_map = cur_fw_ps_map;
376 for_each_set_bit(hlid, wlvif->ap.sta_hlid_map, wl->num_links)
377 wl12xx_irq_ps_regulate_link(wl, wlvif, hlid,
378 wl->links[hlid].allocated_pkts);
381 static int wlcore_fw_status(struct wl1271 *wl, struct wl_fw_status *status)
383 struct wl12xx_vif *wlvif;
384 struct timespec ts;
385 u32 old_tx_blk_count = wl->tx_blocks_available;
386 int avail, freed_blocks;
387 int i;
388 int ret;
389 struct wl1271_link *lnk;
391 ret = wlcore_raw_read_data(wl, REG_RAW_FW_STATUS_ADDR,
392 wl->raw_fw_status,
393 wl->fw_status_len, false);
394 if (ret < 0)
395 return ret;
397 wlcore_hw_convert_fw_status(wl, wl->raw_fw_status, wl->fw_status);
399 wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
400 "drv_rx_counter = %d, tx_results_counter = %d)",
401 status->intr,
402 status->fw_rx_counter,
403 status->drv_rx_counter,
404 status->tx_results_counter);
406 for (i = 0; i < NUM_TX_QUEUES; i++) {
407 /* prevent wrap-around in freed-packets counter */
408 wl->tx_allocated_pkts[i] -=
409 (status->counters.tx_released_pkts[i] -
410 wl->tx_pkts_freed[i]) & 0xff;
412 wl->tx_pkts_freed[i] = status->counters.tx_released_pkts[i];
416 for_each_set_bit(i, wl->links_map, wl->num_links) {
417 u8 diff;
418 lnk = &wl->links[i];
420 /* prevent wrap-around in freed-packets counter */
421 diff = (status->counters.tx_lnk_free_pkts[i] -
422 lnk->prev_freed_pkts) & 0xff;
424 if (diff == 0)
425 continue;
427 lnk->allocated_pkts -= diff;
428 lnk->prev_freed_pkts = status->counters.tx_lnk_free_pkts[i];
430 /* accumulate the prev_freed_pkts counter */
431 lnk->total_freed_pkts += diff;
434 /* prevent wrap-around in total blocks counter */
435 if (likely(wl->tx_blocks_freed <= status->total_released_blks))
436 freed_blocks = status->total_released_blks -
437 wl->tx_blocks_freed;
438 else
439 freed_blocks = 0x100000000LL - wl->tx_blocks_freed +
440 status->total_released_blks;
442 wl->tx_blocks_freed = status->total_released_blks;
444 wl->tx_allocated_blocks -= freed_blocks;
447 * If the FW freed some blocks:
448 * If we still have allocated blocks - re-arm the timer, Tx is
449 * not stuck. Otherwise, cancel the timer (no Tx currently).
451 if (freed_blocks) {
452 if (wl->tx_allocated_blocks)
453 wl12xx_rearm_tx_watchdog_locked(wl);
454 else
455 cancel_delayed_work(&wl->tx_watchdog_work);
458 avail = status->tx_total - wl->tx_allocated_blocks;
461 * The FW might change the total number of TX memblocks before
462 * we get a notification about blocks being released. Thus, the
463 * available blocks calculation might yield a temporary result
464 * which is lower than the actual available blocks. Keeping in
465 * mind that only blocks that were allocated can be moved from
466 * TX to RX, tx_blocks_available should never decrease here.
468 wl->tx_blocks_available = max((int)wl->tx_blocks_available,
469 avail);
471 /* if more blocks are available now, tx work can be scheduled */
472 if (wl->tx_blocks_available > old_tx_blk_count)
473 clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
475 /* for AP update num of allocated TX blocks per link and ps status */
476 wl12xx_for_each_wlvif_ap(wl, wlvif) {
477 wl12xx_irq_update_links_status(wl, wlvif, status);
480 /* update the host-chipset time offset */
481 getnstimeofday(&ts);
482 wl->time_offset = (timespec_to_ns(&ts) >> 10) -
483 (s64)(status->fw_localtime);
485 wl->fw_fast_lnk_map = status->link_fast_bitmap;
487 return 0;
490 static void wl1271_flush_deferred_work(struct wl1271 *wl)
492 struct sk_buff *skb;
494 /* Pass all received frames to the network stack */
495 while ((skb = skb_dequeue(&wl->deferred_rx_queue)))
496 ieee80211_rx_ni(wl->hw, skb);
498 /* Return sent skbs to the network stack */
499 while ((skb = skb_dequeue(&wl->deferred_tx_queue)))
500 ieee80211_tx_status_ni(wl->hw, skb);
503 static void wl1271_netstack_work(struct work_struct *work)
505 struct wl1271 *wl =
506 container_of(work, struct wl1271, netstack_work);
508 do {
509 wl1271_flush_deferred_work(wl);
510 } while (skb_queue_len(&wl->deferred_rx_queue));
513 #define WL1271_IRQ_MAX_LOOPS 256
515 static int wlcore_irq_locked(struct wl1271 *wl)
517 int ret = 0;
518 u32 intr;
519 int loopcount = WL1271_IRQ_MAX_LOOPS;
520 bool done = false;
521 unsigned int defer_count;
522 unsigned long flags;
525 * In case edge triggered interrupt must be used, we cannot iterate
526 * more than once without introducing race conditions with the hardirq.
528 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
529 loopcount = 1;
531 wl1271_debug(DEBUG_IRQ, "IRQ work");
533 if (unlikely(wl->state != WLCORE_STATE_ON))
534 goto out;
536 ret = wl1271_ps_elp_wakeup(wl);
537 if (ret < 0)
538 goto out;
540 while (!done && loopcount--) {
542 * In order to avoid a race with the hardirq, clear the flag
543 * before acknowledging the chip. Since the mutex is held,
544 * wl1271_ps_elp_wakeup cannot be called concurrently.
546 clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
547 smp_mb__after_atomic();
549 ret = wlcore_fw_status(wl, wl->fw_status);
550 if (ret < 0)
551 goto out;
553 wlcore_hw_tx_immediate_compl(wl);
555 intr = wl->fw_status->intr;
556 intr &= WLCORE_ALL_INTR_MASK;
557 if (!intr) {
558 done = true;
559 continue;
562 if (unlikely(intr & WL1271_ACX_INTR_WATCHDOG)) {
563 wl1271_error("HW watchdog interrupt received! starting recovery.");
564 wl->watchdog_recovery = true;
565 ret = -EIO;
567 /* restarting the chip. ignore any other interrupt. */
568 goto out;
571 if (unlikely(intr & WL1271_ACX_SW_INTR_WATCHDOG)) {
572 wl1271_error("SW watchdog interrupt received! "
573 "starting recovery.");
574 wl->watchdog_recovery = true;
575 ret = -EIO;
577 /* restarting the chip. ignore any other interrupt. */
578 goto out;
581 if (likely(intr & WL1271_ACX_INTR_DATA)) {
582 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
584 ret = wlcore_rx(wl, wl->fw_status);
585 if (ret < 0)
586 goto out;
588 /* Check if any tx blocks were freed */
589 spin_lock_irqsave(&wl->wl_lock, flags);
590 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
591 wl1271_tx_total_queue_count(wl) > 0) {
592 spin_unlock_irqrestore(&wl->wl_lock, flags);
594 * In order to avoid starvation of the TX path,
595 * call the work function directly.
597 ret = wlcore_tx_work_locked(wl);
598 if (ret < 0)
599 goto out;
600 } else {
601 spin_unlock_irqrestore(&wl->wl_lock, flags);
604 /* check for tx results */
605 ret = wlcore_hw_tx_delayed_compl(wl);
606 if (ret < 0)
607 goto out;
609 /* Make sure the deferred queues don't get too long */
610 defer_count = skb_queue_len(&wl->deferred_tx_queue) +
611 skb_queue_len(&wl->deferred_rx_queue);
612 if (defer_count > WL1271_DEFERRED_QUEUE_LIMIT)
613 wl1271_flush_deferred_work(wl);
616 if (intr & WL1271_ACX_INTR_EVENT_A) {
617 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
618 ret = wl1271_event_handle(wl, 0);
619 if (ret < 0)
620 goto out;
623 if (intr & WL1271_ACX_INTR_EVENT_B) {
624 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
625 ret = wl1271_event_handle(wl, 1);
626 if (ret < 0)
627 goto out;
630 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
631 wl1271_debug(DEBUG_IRQ,
632 "WL1271_ACX_INTR_INIT_COMPLETE");
634 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
635 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
638 wl1271_ps_elp_sleep(wl);
640 out:
641 return ret;
644 static irqreturn_t wlcore_irq(int irq, void *cookie)
646 int ret;
647 unsigned long flags;
648 struct wl1271 *wl = cookie;
650 /* complete the ELP completion */
651 spin_lock_irqsave(&wl->wl_lock, flags);
652 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
653 if (wl->elp_compl) {
654 complete(wl->elp_compl);
655 wl->elp_compl = NULL;
658 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
659 /* don't enqueue a work right now. mark it as pending */
660 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
661 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
662 disable_irq_nosync(wl->irq);
663 pm_wakeup_event(wl->dev, 0);
664 spin_unlock_irqrestore(&wl->wl_lock, flags);
665 return IRQ_HANDLED;
667 spin_unlock_irqrestore(&wl->wl_lock, flags);
669 /* TX might be handled here, avoid redundant work */
670 set_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
671 cancel_work_sync(&wl->tx_work);
673 mutex_lock(&wl->mutex);
675 ret = wlcore_irq_locked(wl);
676 if (ret)
677 wl12xx_queue_recovery_work(wl);
679 spin_lock_irqsave(&wl->wl_lock, flags);
680 /* In case TX was not handled here, queue TX work */
681 clear_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
682 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
683 wl1271_tx_total_queue_count(wl) > 0)
684 ieee80211_queue_work(wl->hw, &wl->tx_work);
685 spin_unlock_irqrestore(&wl->wl_lock, flags);
687 mutex_unlock(&wl->mutex);
689 return IRQ_HANDLED;
692 struct vif_counter_data {
693 u8 counter;
695 struct ieee80211_vif *cur_vif;
696 bool cur_vif_running;
699 static void wl12xx_vif_count_iter(void *data, u8 *mac,
700 struct ieee80211_vif *vif)
702 struct vif_counter_data *counter = data;
704 counter->counter++;
705 if (counter->cur_vif == vif)
706 counter->cur_vif_running = true;
709 /* caller must not hold wl->mutex, as it might deadlock */
710 static void wl12xx_get_vif_count(struct ieee80211_hw *hw,
711 struct ieee80211_vif *cur_vif,
712 struct vif_counter_data *data)
714 memset(data, 0, sizeof(*data));
715 data->cur_vif = cur_vif;
717 ieee80211_iterate_active_interfaces(hw, IEEE80211_IFACE_ITER_RESUME_ALL,
718 wl12xx_vif_count_iter, data);
721 static int wl12xx_fetch_firmware(struct wl1271 *wl, bool plt)
723 const struct firmware *fw;
724 const char *fw_name;
725 enum wl12xx_fw_type fw_type;
726 int ret;
728 if (plt) {
729 fw_type = WL12XX_FW_TYPE_PLT;
730 fw_name = wl->plt_fw_name;
731 } else {
733 * we can't call wl12xx_get_vif_count() here because
734 * wl->mutex is taken, so use the cached last_vif_count value
736 if (wl->last_vif_count > 1 && wl->mr_fw_name) {
737 fw_type = WL12XX_FW_TYPE_MULTI;
738 fw_name = wl->mr_fw_name;
739 } else {
740 fw_type = WL12XX_FW_TYPE_NORMAL;
741 fw_name = wl->sr_fw_name;
745 if (wl->fw_type == fw_type)
746 return 0;
748 wl1271_debug(DEBUG_BOOT, "booting firmware %s", fw_name);
750 ret = request_firmware(&fw, fw_name, wl->dev);
752 if (ret < 0) {
753 wl1271_error("could not get firmware %s: %d", fw_name, ret);
754 return ret;
757 if (fw->size % 4) {
758 wl1271_error("firmware size is not multiple of 32 bits: %zu",
759 fw->size);
760 ret = -EILSEQ;
761 goto out;
764 vfree(wl->fw);
765 wl->fw_type = WL12XX_FW_TYPE_NONE;
766 wl->fw_len = fw->size;
767 wl->fw = vmalloc(wl->fw_len);
769 if (!wl->fw) {
770 wl1271_error("could not allocate memory for the firmware");
771 ret = -ENOMEM;
772 goto out;
775 memcpy(wl->fw, fw->data, wl->fw_len);
776 ret = 0;
777 wl->fw_type = fw_type;
778 out:
779 release_firmware(fw);
781 return ret;
784 void wl12xx_queue_recovery_work(struct wl1271 *wl)
786 /* Avoid a recursive recovery */
787 if (wl->state == WLCORE_STATE_ON) {
788 WARN_ON(!test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY,
789 &wl->flags));
791 wl->state = WLCORE_STATE_RESTARTING;
792 set_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
793 wl1271_ps_elp_wakeup(wl);
794 wlcore_disable_interrupts_nosync(wl);
795 ieee80211_queue_work(wl->hw, &wl->recovery_work);
799 size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen)
801 size_t len;
803 /* Make sure we have enough room */
804 len = min_t(size_t, maxlen, PAGE_SIZE - wl->fwlog_size);
806 /* Fill the FW log file, consumed by the sysfs fwlog entry */
807 memcpy(wl->fwlog + wl->fwlog_size, memblock, len);
808 wl->fwlog_size += len;
810 return len;
813 static void wl12xx_read_fwlog_panic(struct wl1271 *wl)
815 struct wlcore_partition_set part, old_part;
816 u32 addr;
817 u32 offset;
818 u32 end_of_log;
819 u8 *block;
820 int ret;
822 if ((wl->quirks & WLCORE_QUIRK_FWLOG_NOT_IMPLEMENTED) ||
823 (wl->conf.fwlog.mem_blocks == 0))
824 return;
826 wl1271_info("Reading FW panic log");
828 block = kmalloc(wl->fw_mem_block_size, GFP_KERNEL);
829 if (!block)
830 return;
833 * Make sure the chip is awake and the logger isn't active.
834 * Do not send a stop fwlog command if the fw is hanged or if
835 * dbgpins are used (due to some fw bug).
837 if (wl1271_ps_elp_wakeup(wl))
838 goto out;
839 if (!wl->watchdog_recovery &&
840 wl->conf.fwlog.output != WL12XX_FWLOG_OUTPUT_DBG_PINS)
841 wl12xx_cmd_stop_fwlog(wl);
843 /* Read the first memory block address */
844 ret = wlcore_fw_status(wl, wl->fw_status);
845 if (ret < 0)
846 goto out;
848 addr = wl->fw_status->log_start_addr;
849 if (!addr)
850 goto out;
852 if (wl->conf.fwlog.mode == WL12XX_FWLOG_CONTINUOUS) {
853 offset = sizeof(addr) + sizeof(struct wl1271_rx_descriptor);
854 end_of_log = wl->fwlog_end;
855 } else {
856 offset = sizeof(addr);
857 end_of_log = addr;
860 old_part = wl->curr_part;
861 memset(&part, 0, sizeof(part));
863 /* Traverse the memory blocks linked list */
864 do {
865 part.mem.start = wlcore_hw_convert_hwaddr(wl, addr);
866 part.mem.size = PAGE_SIZE;
868 ret = wlcore_set_partition(wl, &part);
869 if (ret < 0) {
870 wl1271_error("%s: set_partition start=0x%X size=%d",
871 __func__, part.mem.start, part.mem.size);
872 goto out;
875 memset(block, 0, wl->fw_mem_block_size);
876 ret = wlcore_read_hwaddr(wl, addr, block,
877 wl->fw_mem_block_size, false);
879 if (ret < 0)
880 goto out;
883 * Memory blocks are linked to one another. The first 4 bytes
884 * of each memory block hold the hardware address of the next
885 * one. The last memory block points to the first one in
886 * on demand mode and is equal to 0x2000000 in continuous mode.
888 addr = le32_to_cpup((__le32 *)block);
890 if (!wl12xx_copy_fwlog(wl, block + offset,
891 wl->fw_mem_block_size - offset))
892 break;
893 } while (addr && (addr != end_of_log));
895 wake_up_interruptible(&wl->fwlog_waitq);
897 out:
898 kfree(block);
899 wlcore_set_partition(wl, &old_part);
902 static void wlcore_save_freed_pkts(struct wl1271 *wl, struct wl12xx_vif *wlvif,
903 u8 hlid, struct ieee80211_sta *sta)
905 struct wl1271_station *wl_sta;
906 u32 sqn_recovery_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING;
908 wl_sta = (void *)sta->drv_priv;
909 wl_sta->total_freed_pkts = wl->links[hlid].total_freed_pkts;
912 * increment the initial seq number on recovery to account for
913 * transmitted packets that we haven't yet got in the FW status
915 if (wlvif->encryption_type == KEY_GEM)
916 sqn_recovery_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING_GEM;
918 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
919 wl_sta->total_freed_pkts += sqn_recovery_padding;
922 static void wlcore_save_freed_pkts_addr(struct wl1271 *wl,
923 struct wl12xx_vif *wlvif,
924 u8 hlid, const u8 *addr)
926 struct ieee80211_sta *sta;
927 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
929 if (WARN_ON(hlid == WL12XX_INVALID_LINK_ID ||
930 is_zero_ether_addr(addr)))
931 return;
933 rcu_read_lock();
934 sta = ieee80211_find_sta(vif, addr);
935 if (sta)
936 wlcore_save_freed_pkts(wl, wlvif, hlid, sta);
937 rcu_read_unlock();
940 static void wlcore_print_recovery(struct wl1271 *wl)
942 u32 pc = 0;
943 u32 hint_sts = 0;
944 int ret;
946 wl1271_info("Hardware recovery in progress. FW ver: %s",
947 wl->chip.fw_ver_str);
949 /* change partitions momentarily so we can read the FW pc */
950 ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
951 if (ret < 0)
952 return;
954 ret = wlcore_read_reg(wl, REG_PC_ON_RECOVERY, &pc);
955 if (ret < 0)
956 return;
958 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &hint_sts);
959 if (ret < 0)
960 return;
962 wl1271_info("pc: 0x%x, hint_sts: 0x%08x count: %d",
963 pc, hint_sts, ++wl->recovery_count);
965 wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
969 static void wl1271_recovery_work(struct work_struct *work)
971 struct wl1271 *wl =
972 container_of(work, struct wl1271, recovery_work);
973 struct wl12xx_vif *wlvif;
974 struct ieee80211_vif *vif;
976 mutex_lock(&wl->mutex);
978 if (wl->state == WLCORE_STATE_OFF || wl->plt)
979 goto out_unlock;
981 if (!test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags)) {
982 if (wl->conf.fwlog.output == WL12XX_FWLOG_OUTPUT_HOST)
983 wl12xx_read_fwlog_panic(wl);
984 wlcore_print_recovery(wl);
987 BUG_ON(wl->conf.recovery.bug_on_recovery &&
988 !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags));
990 if (wl->conf.recovery.no_recovery) {
991 wl1271_info("No recovery (chosen on module load). Fw will remain stuck.");
992 goto out_unlock;
995 /* Prevent spurious TX during FW restart */
996 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_FW_RESTART);
998 /* reboot the chipset */
999 while (!list_empty(&wl->wlvif_list)) {
1000 wlvif = list_first_entry(&wl->wlvif_list,
1001 struct wl12xx_vif, list);
1002 vif = wl12xx_wlvif_to_vif(wlvif);
1004 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
1005 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
1006 wlcore_save_freed_pkts_addr(wl, wlvif, wlvif->sta.hlid,
1007 vif->bss_conf.bssid);
1010 __wl1271_op_remove_interface(wl, vif, false);
1013 wlcore_op_stop_locked(wl);
1015 ieee80211_restart_hw(wl->hw);
1018 * Its safe to enable TX now - the queues are stopped after a request
1019 * to restart the HW.
1021 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_FW_RESTART);
1023 out_unlock:
1024 wl->watchdog_recovery = false;
1025 clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
1026 mutex_unlock(&wl->mutex);
1029 static int wlcore_fw_wakeup(struct wl1271 *wl)
1031 return wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
1034 static int wl1271_setup(struct wl1271 *wl)
1036 wl->raw_fw_status = kzalloc(wl->fw_status_len, GFP_KERNEL);
1037 if (!wl->raw_fw_status)
1038 goto err;
1040 wl->fw_status = kzalloc(sizeof(*wl->fw_status), GFP_KERNEL);
1041 if (!wl->fw_status)
1042 goto err;
1044 wl->tx_res_if = kzalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
1045 if (!wl->tx_res_if)
1046 goto err;
1048 return 0;
1049 err:
1050 kfree(wl->fw_status);
1051 kfree(wl->raw_fw_status);
1052 return -ENOMEM;
1055 static int wl12xx_set_power_on(struct wl1271 *wl)
1057 int ret;
1059 msleep(WL1271_PRE_POWER_ON_SLEEP);
1060 ret = wl1271_power_on(wl);
1061 if (ret < 0)
1062 goto out;
1063 msleep(WL1271_POWER_ON_SLEEP);
1064 wl1271_io_reset(wl);
1065 wl1271_io_init(wl);
1067 ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
1068 if (ret < 0)
1069 goto fail;
1071 /* ELP module wake up */
1072 ret = wlcore_fw_wakeup(wl);
1073 if (ret < 0)
1074 goto fail;
1076 out:
1077 return ret;
1079 fail:
1080 wl1271_power_off(wl);
1081 return ret;
1084 static int wl12xx_chip_wakeup(struct wl1271 *wl, bool plt)
1086 int ret = 0;
1088 ret = wl12xx_set_power_on(wl);
1089 if (ret < 0)
1090 goto out;
1093 * For wl127x based devices we could use the default block
1094 * size (512 bytes), but due to a bug in the sdio driver, we
1095 * need to set it explicitly after the chip is powered on. To
1096 * simplify the code and since the performance impact is
1097 * negligible, we use the same block size for all different
1098 * chip types.
1100 * Check if the bus supports blocksize alignment and, if it
1101 * doesn't, make sure we don't have the quirk.
1103 if (!wl1271_set_block_size(wl))
1104 wl->quirks &= ~WLCORE_QUIRK_TX_BLOCKSIZE_ALIGN;
1106 /* TODO: make sure the lower driver has set things up correctly */
1108 ret = wl1271_setup(wl);
1109 if (ret < 0)
1110 goto out;
1112 ret = wl12xx_fetch_firmware(wl, plt);
1113 if (ret < 0)
1114 goto out;
1116 out:
1117 return ret;
1120 int wl1271_plt_start(struct wl1271 *wl, const enum plt_mode plt_mode)
1122 int retries = WL1271_BOOT_RETRIES;
1123 struct wiphy *wiphy = wl->hw->wiphy;
1125 static const char* const PLT_MODE[] = {
1126 "PLT_OFF",
1127 "PLT_ON",
1128 "PLT_FEM_DETECT",
1129 "PLT_CHIP_AWAKE"
1132 int ret;
1134 mutex_lock(&wl->mutex);
1136 wl1271_notice("power up");
1138 if (wl->state != WLCORE_STATE_OFF) {
1139 wl1271_error("cannot go into PLT state because not "
1140 "in off state: %d", wl->state);
1141 ret = -EBUSY;
1142 goto out;
1145 /* Indicate to lower levels that we are now in PLT mode */
1146 wl->plt = true;
1147 wl->plt_mode = plt_mode;
1149 while (retries) {
1150 retries--;
1151 ret = wl12xx_chip_wakeup(wl, true);
1152 if (ret < 0)
1153 goto power_off;
1155 if (plt_mode != PLT_CHIP_AWAKE) {
1156 ret = wl->ops->plt_init(wl);
1157 if (ret < 0)
1158 goto power_off;
1161 wl->state = WLCORE_STATE_ON;
1162 wl1271_notice("firmware booted in PLT mode %s (%s)",
1163 PLT_MODE[plt_mode],
1164 wl->chip.fw_ver_str);
1166 /* update hw/fw version info in wiphy struct */
1167 wiphy->hw_version = wl->chip.id;
1168 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1169 sizeof(wiphy->fw_version));
1171 goto out;
1173 power_off:
1174 wl1271_power_off(wl);
1177 wl->plt = false;
1178 wl->plt_mode = PLT_OFF;
1180 wl1271_error("firmware boot in PLT mode failed despite %d retries",
1181 WL1271_BOOT_RETRIES);
1182 out:
1183 mutex_unlock(&wl->mutex);
1185 return ret;
1188 int wl1271_plt_stop(struct wl1271 *wl)
1190 int ret = 0;
1192 wl1271_notice("power down");
1195 * Interrupts must be disabled before setting the state to OFF.
1196 * Otherwise, the interrupt handler might be called and exit without
1197 * reading the interrupt status.
1199 wlcore_disable_interrupts(wl);
1200 mutex_lock(&wl->mutex);
1201 if (!wl->plt) {
1202 mutex_unlock(&wl->mutex);
1205 * This will not necessarily enable interrupts as interrupts
1206 * may have been disabled when op_stop was called. It will,
1207 * however, balance the above call to disable_interrupts().
1209 wlcore_enable_interrupts(wl);
1211 wl1271_error("cannot power down because not in PLT "
1212 "state: %d", wl->state);
1213 ret = -EBUSY;
1214 goto out;
1217 mutex_unlock(&wl->mutex);
1219 wl1271_flush_deferred_work(wl);
1220 cancel_work_sync(&wl->netstack_work);
1221 cancel_work_sync(&wl->recovery_work);
1222 cancel_delayed_work_sync(&wl->elp_work);
1223 cancel_delayed_work_sync(&wl->tx_watchdog_work);
1225 mutex_lock(&wl->mutex);
1226 wl1271_power_off(wl);
1227 wl->flags = 0;
1228 wl->sleep_auth = WL1271_PSM_ILLEGAL;
1229 wl->state = WLCORE_STATE_OFF;
1230 wl->plt = false;
1231 wl->plt_mode = PLT_OFF;
1232 wl->rx_counter = 0;
1233 mutex_unlock(&wl->mutex);
1235 out:
1236 return ret;
1239 static void wl1271_op_tx(struct ieee80211_hw *hw,
1240 struct ieee80211_tx_control *control,
1241 struct sk_buff *skb)
1243 struct wl1271 *wl = hw->priv;
1244 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1245 struct ieee80211_vif *vif = info->control.vif;
1246 struct wl12xx_vif *wlvif = NULL;
1247 unsigned long flags;
1248 int q, mapping;
1249 u8 hlid;
1251 if (!vif) {
1252 wl1271_debug(DEBUG_TX, "DROP skb with no vif");
1253 ieee80211_free_txskb(hw, skb);
1254 return;
1257 wlvif = wl12xx_vif_to_data(vif);
1258 mapping = skb_get_queue_mapping(skb);
1259 q = wl1271_tx_get_queue(mapping);
1261 hlid = wl12xx_tx_get_hlid(wl, wlvif, skb, control->sta);
1263 spin_lock_irqsave(&wl->wl_lock, flags);
1266 * drop the packet if the link is invalid or the queue is stopped
1267 * for any reason but watermark. Watermark is a "soft"-stop so we
1268 * allow these packets through.
1270 if (hlid == WL12XX_INVALID_LINK_ID ||
1271 (!test_bit(hlid, wlvif->links_map)) ||
1272 (wlcore_is_queue_stopped_locked(wl, wlvif, q) &&
1273 !wlcore_is_queue_stopped_by_reason_locked(wl, wlvif, q,
1274 WLCORE_QUEUE_STOP_REASON_WATERMARK))) {
1275 wl1271_debug(DEBUG_TX, "DROP skb hlid %d q %d", hlid, q);
1276 ieee80211_free_txskb(hw, skb);
1277 goto out;
1280 wl1271_debug(DEBUG_TX, "queue skb hlid %d q %d len %d",
1281 hlid, q, skb->len);
1282 skb_queue_tail(&wl->links[hlid].tx_queue[q], skb);
1284 wl->tx_queue_count[q]++;
1285 wlvif->tx_queue_count[q]++;
1288 * The workqueue is slow to process the tx_queue and we need stop
1289 * the queue here, otherwise the queue will get too long.
1291 if (wlvif->tx_queue_count[q] >= WL1271_TX_QUEUE_HIGH_WATERMARK &&
1292 !wlcore_is_queue_stopped_by_reason_locked(wl, wlvif, q,
1293 WLCORE_QUEUE_STOP_REASON_WATERMARK)) {
1294 wl1271_debug(DEBUG_TX, "op_tx: stopping queues for q %d", q);
1295 wlcore_stop_queue_locked(wl, wlvif, q,
1296 WLCORE_QUEUE_STOP_REASON_WATERMARK);
1300 * The chip specific setup must run before the first TX packet -
1301 * before that, the tx_work will not be initialized!
1304 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
1305 !test_bit(WL1271_FLAG_TX_PENDING, &wl->flags))
1306 ieee80211_queue_work(wl->hw, &wl->tx_work);
1308 out:
1309 spin_unlock_irqrestore(&wl->wl_lock, flags);
1312 int wl1271_tx_dummy_packet(struct wl1271 *wl)
1314 unsigned long flags;
1315 int q;
1317 /* no need to queue a new dummy packet if one is already pending */
1318 if (test_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags))
1319 return 0;
1321 q = wl1271_tx_get_queue(skb_get_queue_mapping(wl->dummy_packet));
1323 spin_lock_irqsave(&wl->wl_lock, flags);
1324 set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
1325 wl->tx_queue_count[q]++;
1326 spin_unlock_irqrestore(&wl->wl_lock, flags);
1328 /* The FW is low on RX memory blocks, so send the dummy packet asap */
1329 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags))
1330 return wlcore_tx_work_locked(wl);
1333 * If the FW TX is busy, TX work will be scheduled by the threaded
1334 * interrupt handler function
1336 return 0;
1340 * The size of the dummy packet should be at least 1400 bytes. However, in
1341 * order to minimize the number of bus transactions, aligning it to 512 bytes
1342 * boundaries could be beneficial, performance wise
1344 #define TOTAL_TX_DUMMY_PACKET_SIZE (ALIGN(1400, 512))
1346 static struct sk_buff *wl12xx_alloc_dummy_packet(struct wl1271 *wl)
1348 struct sk_buff *skb;
1349 struct ieee80211_hdr_3addr *hdr;
1350 unsigned int dummy_packet_size;
1352 dummy_packet_size = TOTAL_TX_DUMMY_PACKET_SIZE -
1353 sizeof(struct wl1271_tx_hw_descr) - sizeof(*hdr);
1355 skb = dev_alloc_skb(TOTAL_TX_DUMMY_PACKET_SIZE);
1356 if (!skb) {
1357 wl1271_warning("Failed to allocate a dummy packet skb");
1358 return NULL;
1361 skb_reserve(skb, sizeof(struct wl1271_tx_hw_descr));
1363 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
1364 memset(hdr, 0, sizeof(*hdr));
1365 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1366 IEEE80211_STYPE_NULLFUNC |
1367 IEEE80211_FCTL_TODS);
1369 memset(skb_put(skb, dummy_packet_size), 0, dummy_packet_size);
1371 /* Dummy packets require the TID to be management */
1372 skb->priority = WL1271_TID_MGMT;
1374 /* Initialize all fields that might be used */
1375 skb_set_queue_mapping(skb, 0);
1376 memset(IEEE80211_SKB_CB(skb), 0, sizeof(struct ieee80211_tx_info));
1378 return skb;
1382 #ifdef CONFIG_PM
1383 static int
1384 wl1271_validate_wowlan_pattern(struct cfg80211_pkt_pattern *p)
1386 int num_fields = 0, in_field = 0, fields_size = 0;
1387 int i, pattern_len = 0;
1389 if (!p->mask) {
1390 wl1271_warning("No mask in WoWLAN pattern");
1391 return -EINVAL;
1395 * The pattern is broken up into segments of bytes at different offsets
1396 * that need to be checked by the FW filter. Each segment is called
1397 * a field in the FW API. We verify that the total number of fields
1398 * required for this pattern won't exceed FW limits (8)
1399 * as well as the total fields buffer won't exceed the FW limit.
1400 * Note that if there's a pattern which crosses Ethernet/IP header
1401 * boundary a new field is required.
1403 for (i = 0; i < p->pattern_len; i++) {
1404 if (test_bit(i, (unsigned long *)p->mask)) {
1405 if (!in_field) {
1406 in_field = 1;
1407 pattern_len = 1;
1408 } else {
1409 if (i == WL1271_RX_FILTER_ETH_HEADER_SIZE) {
1410 num_fields++;
1411 fields_size += pattern_len +
1412 RX_FILTER_FIELD_OVERHEAD;
1413 pattern_len = 1;
1414 } else
1415 pattern_len++;
1417 } else {
1418 if (in_field) {
1419 in_field = 0;
1420 fields_size += pattern_len +
1421 RX_FILTER_FIELD_OVERHEAD;
1422 num_fields++;
1427 if (in_field) {
1428 fields_size += pattern_len + RX_FILTER_FIELD_OVERHEAD;
1429 num_fields++;
1432 if (num_fields > WL1271_RX_FILTER_MAX_FIELDS) {
1433 wl1271_warning("RX Filter too complex. Too many segments");
1434 return -EINVAL;
1437 if (fields_size > WL1271_RX_FILTER_MAX_FIELDS_SIZE) {
1438 wl1271_warning("RX filter pattern is too big");
1439 return -E2BIG;
1442 return 0;
1445 struct wl12xx_rx_filter *wl1271_rx_filter_alloc(void)
1447 return kzalloc(sizeof(struct wl12xx_rx_filter), GFP_KERNEL);
1450 void wl1271_rx_filter_free(struct wl12xx_rx_filter *filter)
1452 int i;
1454 if (filter == NULL)
1455 return;
1457 for (i = 0; i < filter->num_fields; i++)
1458 kfree(filter->fields[i].pattern);
1460 kfree(filter);
1463 int wl1271_rx_filter_alloc_field(struct wl12xx_rx_filter *filter,
1464 u16 offset, u8 flags,
1465 const u8 *pattern, u8 len)
1467 struct wl12xx_rx_filter_field *field;
1469 if (filter->num_fields == WL1271_RX_FILTER_MAX_FIELDS) {
1470 wl1271_warning("Max fields per RX filter. can't alloc another");
1471 return -EINVAL;
1474 field = &filter->fields[filter->num_fields];
1476 field->pattern = kzalloc(len, GFP_KERNEL);
1477 if (!field->pattern) {
1478 wl1271_warning("Failed to allocate RX filter pattern");
1479 return -ENOMEM;
1482 filter->num_fields++;
1484 field->offset = cpu_to_le16(offset);
1485 field->flags = flags;
1486 field->len = len;
1487 memcpy(field->pattern, pattern, len);
1489 return 0;
1492 int wl1271_rx_filter_get_fields_size(struct wl12xx_rx_filter *filter)
1494 int i, fields_size = 0;
1496 for (i = 0; i < filter->num_fields; i++)
1497 fields_size += filter->fields[i].len +
1498 sizeof(struct wl12xx_rx_filter_field) -
1499 sizeof(u8 *);
1501 return fields_size;
1504 void wl1271_rx_filter_flatten_fields(struct wl12xx_rx_filter *filter,
1505 u8 *buf)
1507 int i;
1508 struct wl12xx_rx_filter_field *field;
1510 for (i = 0; i < filter->num_fields; i++) {
1511 field = (struct wl12xx_rx_filter_field *)buf;
1513 field->offset = filter->fields[i].offset;
1514 field->flags = filter->fields[i].flags;
1515 field->len = filter->fields[i].len;
1517 memcpy(&field->pattern, filter->fields[i].pattern, field->len);
1518 buf += sizeof(struct wl12xx_rx_filter_field) -
1519 sizeof(u8 *) + field->len;
1524 * Allocates an RX filter returned through f
1525 * which needs to be freed using rx_filter_free()
1527 static int
1528 wl1271_convert_wowlan_pattern_to_rx_filter(struct cfg80211_pkt_pattern *p,
1529 struct wl12xx_rx_filter **f)
1531 int i, j, ret = 0;
1532 struct wl12xx_rx_filter *filter;
1533 u16 offset;
1534 u8 flags, len;
1536 filter = wl1271_rx_filter_alloc();
1537 if (!filter) {
1538 wl1271_warning("Failed to alloc rx filter");
1539 ret = -ENOMEM;
1540 goto err;
1543 i = 0;
1544 while (i < p->pattern_len) {
1545 if (!test_bit(i, (unsigned long *)p->mask)) {
1546 i++;
1547 continue;
1550 for (j = i; j < p->pattern_len; j++) {
1551 if (!test_bit(j, (unsigned long *)p->mask))
1552 break;
1554 if (i < WL1271_RX_FILTER_ETH_HEADER_SIZE &&
1555 j >= WL1271_RX_FILTER_ETH_HEADER_SIZE)
1556 break;
1559 if (i < WL1271_RX_FILTER_ETH_HEADER_SIZE) {
1560 offset = i;
1561 flags = WL1271_RX_FILTER_FLAG_ETHERNET_HEADER;
1562 } else {
1563 offset = i - WL1271_RX_FILTER_ETH_HEADER_SIZE;
1564 flags = WL1271_RX_FILTER_FLAG_IP_HEADER;
1567 len = j - i;
1569 ret = wl1271_rx_filter_alloc_field(filter,
1570 offset,
1571 flags,
1572 &p->pattern[i], len);
1573 if (ret)
1574 goto err;
1576 i = j;
1579 filter->action = FILTER_SIGNAL;
1581 *f = filter;
1582 return 0;
1584 err:
1585 wl1271_rx_filter_free(filter);
1586 *f = NULL;
1588 return ret;
1591 static int wl1271_configure_wowlan(struct wl1271 *wl,
1592 struct cfg80211_wowlan *wow)
1594 int i, ret;
1596 if (!wow || wow->any || !wow->n_patterns) {
1597 ret = wl1271_acx_default_rx_filter_enable(wl, 0,
1598 FILTER_SIGNAL);
1599 if (ret)
1600 goto out;
1602 ret = wl1271_rx_filter_clear_all(wl);
1603 if (ret)
1604 goto out;
1606 return 0;
1609 if (WARN_ON(wow->n_patterns > WL1271_MAX_RX_FILTERS))
1610 return -EINVAL;
1612 /* Validate all incoming patterns before clearing current FW state */
1613 for (i = 0; i < wow->n_patterns; i++) {
1614 ret = wl1271_validate_wowlan_pattern(&wow->patterns[i]);
1615 if (ret) {
1616 wl1271_warning("Bad wowlan pattern %d", i);
1617 return ret;
1621 ret = wl1271_acx_default_rx_filter_enable(wl, 0, FILTER_SIGNAL);
1622 if (ret)
1623 goto out;
1625 ret = wl1271_rx_filter_clear_all(wl);
1626 if (ret)
1627 goto out;
1629 /* Translate WoWLAN patterns into filters */
1630 for (i = 0; i < wow->n_patterns; i++) {
1631 struct cfg80211_pkt_pattern *p;
1632 struct wl12xx_rx_filter *filter = NULL;
1634 p = &wow->patterns[i];
1636 ret = wl1271_convert_wowlan_pattern_to_rx_filter(p, &filter);
1637 if (ret) {
1638 wl1271_warning("Failed to create an RX filter from "
1639 "wowlan pattern %d", i);
1640 goto out;
1643 ret = wl1271_rx_filter_enable(wl, i, 1, filter);
1645 wl1271_rx_filter_free(filter);
1646 if (ret)
1647 goto out;
1650 ret = wl1271_acx_default_rx_filter_enable(wl, 1, FILTER_DROP);
1652 out:
1653 return ret;
1656 static int wl1271_configure_suspend_sta(struct wl1271 *wl,
1657 struct wl12xx_vif *wlvif,
1658 struct cfg80211_wowlan *wow)
1660 int ret = 0;
1662 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
1663 goto out;
1665 ret = wl1271_ps_elp_wakeup(wl);
1666 if (ret < 0)
1667 goto out;
1669 ret = wl1271_configure_wowlan(wl, wow);
1670 if (ret < 0)
1671 goto out_sleep;
1673 if ((wl->conf.conn.suspend_wake_up_event ==
1674 wl->conf.conn.wake_up_event) &&
1675 (wl->conf.conn.suspend_listen_interval ==
1676 wl->conf.conn.listen_interval))
1677 goto out_sleep;
1679 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1680 wl->conf.conn.suspend_wake_up_event,
1681 wl->conf.conn.suspend_listen_interval);
1683 if (ret < 0)
1684 wl1271_error("suspend: set wake up conditions failed: %d", ret);
1686 out_sleep:
1687 wl1271_ps_elp_sleep(wl);
1688 out:
1689 return ret;
1693 static int wl1271_configure_suspend_ap(struct wl1271 *wl,
1694 struct wl12xx_vif *wlvif)
1696 int ret = 0;
1698 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
1699 goto out;
1701 ret = wl1271_ps_elp_wakeup(wl);
1702 if (ret < 0)
1703 goto out;
1705 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
1707 wl1271_ps_elp_sleep(wl);
1708 out:
1709 return ret;
1713 static int wl1271_configure_suspend(struct wl1271 *wl,
1714 struct wl12xx_vif *wlvif,
1715 struct cfg80211_wowlan *wow)
1717 if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1718 return wl1271_configure_suspend_sta(wl, wlvif, wow);
1719 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
1720 return wl1271_configure_suspend_ap(wl, wlvif);
1721 return 0;
1724 static void wl1271_configure_resume(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1726 int ret = 0;
1727 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
1728 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
1730 if ((!is_ap) && (!is_sta))
1731 return;
1733 if (is_sta && !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
1734 return;
1736 ret = wl1271_ps_elp_wakeup(wl);
1737 if (ret < 0)
1738 return;
1740 if (is_sta) {
1741 wl1271_configure_wowlan(wl, NULL);
1743 if ((wl->conf.conn.suspend_wake_up_event ==
1744 wl->conf.conn.wake_up_event) &&
1745 (wl->conf.conn.suspend_listen_interval ==
1746 wl->conf.conn.listen_interval))
1747 goto out_sleep;
1749 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1750 wl->conf.conn.wake_up_event,
1751 wl->conf.conn.listen_interval);
1753 if (ret < 0)
1754 wl1271_error("resume: wake up conditions failed: %d",
1755 ret);
1757 } else if (is_ap) {
1758 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
1761 out_sleep:
1762 wl1271_ps_elp_sleep(wl);
1765 static int wl1271_op_suspend(struct ieee80211_hw *hw,
1766 struct cfg80211_wowlan *wow)
1768 struct wl1271 *wl = hw->priv;
1769 struct wl12xx_vif *wlvif;
1770 int ret;
1772 wl1271_debug(DEBUG_MAC80211, "mac80211 suspend wow=%d", !!wow);
1773 WARN_ON(!wow);
1775 /* we want to perform the recovery before suspending */
1776 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
1777 wl1271_warning("postponing suspend to perform recovery");
1778 return -EBUSY;
1781 wl1271_tx_flush(wl);
1783 mutex_lock(&wl->mutex);
1784 wl->wow_enabled = true;
1785 wl12xx_for_each_wlvif(wl, wlvif) {
1786 ret = wl1271_configure_suspend(wl, wlvif, wow);
1787 if (ret < 0) {
1788 mutex_unlock(&wl->mutex);
1789 wl1271_warning("couldn't prepare device to suspend");
1790 return ret;
1793 mutex_unlock(&wl->mutex);
1794 /* flush any remaining work */
1795 wl1271_debug(DEBUG_MAC80211, "flushing remaining works");
1798 * disable and re-enable interrupts in order to flush
1799 * the threaded_irq
1801 wlcore_disable_interrupts(wl);
1804 * set suspended flag to avoid triggering a new threaded_irq
1805 * work. no need for spinlock as interrupts are disabled.
1807 set_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1809 wlcore_enable_interrupts(wl);
1810 flush_work(&wl->tx_work);
1811 flush_delayed_work(&wl->elp_work);
1814 * Cancel the watchdog even if above tx_flush failed. We will detect
1815 * it on resume anyway.
1817 cancel_delayed_work(&wl->tx_watchdog_work);
1819 return 0;
1822 static int wl1271_op_resume(struct ieee80211_hw *hw)
1824 struct wl1271 *wl = hw->priv;
1825 struct wl12xx_vif *wlvif;
1826 unsigned long flags;
1827 bool run_irq_work = false, pending_recovery;
1828 int ret;
1830 wl1271_debug(DEBUG_MAC80211, "mac80211 resume wow=%d",
1831 wl->wow_enabled);
1832 WARN_ON(!wl->wow_enabled);
1835 * re-enable irq_work enqueuing, and call irq_work directly if
1836 * there is a pending work.
1838 spin_lock_irqsave(&wl->wl_lock, flags);
1839 clear_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1840 if (test_and_clear_bit(WL1271_FLAG_PENDING_WORK, &wl->flags))
1841 run_irq_work = true;
1842 spin_unlock_irqrestore(&wl->wl_lock, flags);
1844 mutex_lock(&wl->mutex);
1846 /* test the recovery flag before calling any SDIO functions */
1847 pending_recovery = test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS,
1848 &wl->flags);
1850 if (run_irq_work) {
1851 wl1271_debug(DEBUG_MAC80211,
1852 "run postponed irq_work directly");
1854 /* don't talk to the HW if recovery is pending */
1855 if (!pending_recovery) {
1856 ret = wlcore_irq_locked(wl);
1857 if (ret)
1858 wl12xx_queue_recovery_work(wl);
1861 wlcore_enable_interrupts(wl);
1864 if (pending_recovery) {
1865 wl1271_warning("queuing forgotten recovery on resume");
1866 ieee80211_queue_work(wl->hw, &wl->recovery_work);
1867 goto out;
1870 wl12xx_for_each_wlvif(wl, wlvif) {
1871 wl1271_configure_resume(wl, wlvif);
1874 out:
1875 wl->wow_enabled = false;
1878 * Set a flag to re-init the watchdog on the first Tx after resume.
1879 * That way we avoid possible conditions where Tx-complete interrupts
1880 * fail to arrive and we perform a spurious recovery.
1882 set_bit(WL1271_FLAG_REINIT_TX_WDOG, &wl->flags);
1883 mutex_unlock(&wl->mutex);
1885 return 0;
1887 #endif
1889 static int wl1271_op_start(struct ieee80211_hw *hw)
1891 wl1271_debug(DEBUG_MAC80211, "mac80211 start");
1894 * We have to delay the booting of the hardware because
1895 * we need to know the local MAC address before downloading and
1896 * initializing the firmware. The MAC address cannot be changed
1897 * after boot, and without the proper MAC address, the firmware
1898 * will not function properly.
1900 * The MAC address is first known when the corresponding interface
1901 * is added. That is where we will initialize the hardware.
1904 return 0;
1907 static void wlcore_op_stop_locked(struct wl1271 *wl)
1909 int i;
1911 if (wl->state == WLCORE_STATE_OFF) {
1912 if (test_and_clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS,
1913 &wl->flags))
1914 wlcore_enable_interrupts(wl);
1916 return;
1920 * this must be before the cancel_work calls below, so that the work
1921 * functions don't perform further work.
1923 wl->state = WLCORE_STATE_OFF;
1926 * Use the nosync variant to disable interrupts, so the mutex could be
1927 * held while doing so without deadlocking.
1929 wlcore_disable_interrupts_nosync(wl);
1931 mutex_unlock(&wl->mutex);
1933 wlcore_synchronize_interrupts(wl);
1934 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1935 cancel_work_sync(&wl->recovery_work);
1936 wl1271_flush_deferred_work(wl);
1937 cancel_delayed_work_sync(&wl->scan_complete_work);
1938 cancel_work_sync(&wl->netstack_work);
1939 cancel_work_sync(&wl->tx_work);
1940 cancel_delayed_work_sync(&wl->elp_work);
1941 cancel_delayed_work_sync(&wl->tx_watchdog_work);
1943 /* let's notify MAC80211 about the remaining pending TX frames */
1944 mutex_lock(&wl->mutex);
1945 wl12xx_tx_reset(wl);
1947 wl1271_power_off(wl);
1949 * In case a recovery was scheduled, interrupts were disabled to avoid
1950 * an interrupt storm. Now that the power is down, it is safe to
1951 * re-enable interrupts to balance the disable depth
1953 if (test_and_clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1954 wlcore_enable_interrupts(wl);
1956 wl->band = IEEE80211_BAND_2GHZ;
1958 wl->rx_counter = 0;
1959 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1960 wl->channel_type = NL80211_CHAN_NO_HT;
1961 wl->tx_blocks_available = 0;
1962 wl->tx_allocated_blocks = 0;
1963 wl->tx_results_count = 0;
1964 wl->tx_packets_count = 0;
1965 wl->time_offset = 0;
1966 wl->ap_fw_ps_map = 0;
1967 wl->ap_ps_map = 0;
1968 wl->sleep_auth = WL1271_PSM_ILLEGAL;
1969 memset(wl->roles_map, 0, sizeof(wl->roles_map));
1970 memset(wl->links_map, 0, sizeof(wl->links_map));
1971 memset(wl->roc_map, 0, sizeof(wl->roc_map));
1972 memset(wl->session_ids, 0, sizeof(wl->session_ids));
1973 memset(wl->rx_filter_enabled, 0, sizeof(wl->rx_filter_enabled));
1974 wl->active_sta_count = 0;
1975 wl->active_link_count = 0;
1977 /* The system link is always allocated */
1978 wl->links[WL12XX_SYSTEM_HLID].allocated_pkts = 0;
1979 wl->links[WL12XX_SYSTEM_HLID].prev_freed_pkts = 0;
1980 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
1983 * this is performed after the cancel_work calls and the associated
1984 * mutex_lock, so that wl1271_op_add_interface does not accidentally
1985 * get executed before all these vars have been reset.
1987 wl->flags = 0;
1989 wl->tx_blocks_freed = 0;
1991 for (i = 0; i < NUM_TX_QUEUES; i++) {
1992 wl->tx_pkts_freed[i] = 0;
1993 wl->tx_allocated_pkts[i] = 0;
1996 wl1271_debugfs_reset(wl);
1998 kfree(wl->raw_fw_status);
1999 wl->raw_fw_status = NULL;
2000 kfree(wl->fw_status);
2001 wl->fw_status = NULL;
2002 kfree(wl->tx_res_if);
2003 wl->tx_res_if = NULL;
2004 kfree(wl->target_mem_map);
2005 wl->target_mem_map = NULL;
2008 * FW channels must be re-calibrated after recovery,
2009 * save current Reg-Domain channel configuration and clear it.
2011 memcpy(wl->reg_ch_conf_pending, wl->reg_ch_conf_last,
2012 sizeof(wl->reg_ch_conf_pending));
2013 memset(wl->reg_ch_conf_last, 0, sizeof(wl->reg_ch_conf_last));
2016 static void wlcore_op_stop(struct ieee80211_hw *hw)
2018 struct wl1271 *wl = hw->priv;
2020 wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
2022 mutex_lock(&wl->mutex);
2024 wlcore_op_stop_locked(wl);
2026 mutex_unlock(&wl->mutex);
2029 static void wlcore_channel_switch_work(struct work_struct *work)
2031 struct delayed_work *dwork;
2032 struct wl1271 *wl;
2033 struct ieee80211_vif *vif;
2034 struct wl12xx_vif *wlvif;
2035 int ret;
2037 dwork = container_of(work, struct delayed_work, work);
2038 wlvif = container_of(dwork, struct wl12xx_vif, channel_switch_work);
2039 wl = wlvif->wl;
2041 wl1271_info("channel switch failed (role_id: %d).", wlvif->role_id);
2043 mutex_lock(&wl->mutex);
2045 if (unlikely(wl->state != WLCORE_STATE_ON))
2046 goto out;
2048 /* check the channel switch is still ongoing */
2049 if (!test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags))
2050 goto out;
2052 vif = wl12xx_wlvif_to_vif(wlvif);
2053 ieee80211_chswitch_done(vif, false);
2055 ret = wl1271_ps_elp_wakeup(wl);
2056 if (ret < 0)
2057 goto out;
2059 wl12xx_cmd_stop_channel_switch(wl, wlvif);
2061 wl1271_ps_elp_sleep(wl);
2062 out:
2063 mutex_unlock(&wl->mutex);
2066 static void wlcore_connection_loss_work(struct work_struct *work)
2068 struct delayed_work *dwork;
2069 struct wl1271 *wl;
2070 struct ieee80211_vif *vif;
2071 struct wl12xx_vif *wlvif;
2073 dwork = container_of(work, struct delayed_work, work);
2074 wlvif = container_of(dwork, struct wl12xx_vif, connection_loss_work);
2075 wl = wlvif->wl;
2077 wl1271_info("Connection loss work (role_id: %d).", wlvif->role_id);
2079 mutex_lock(&wl->mutex);
2081 if (unlikely(wl->state != WLCORE_STATE_ON))
2082 goto out;
2084 /* Call mac80211 connection loss */
2085 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2086 goto out;
2088 vif = wl12xx_wlvif_to_vif(wlvif);
2089 ieee80211_connection_loss(vif);
2090 out:
2091 mutex_unlock(&wl->mutex);
2094 static void wlcore_pending_auth_complete_work(struct work_struct *work)
2096 struct delayed_work *dwork;
2097 struct wl1271 *wl;
2098 struct wl12xx_vif *wlvif;
2099 unsigned long time_spare;
2100 int ret;
2102 dwork = container_of(work, struct delayed_work, work);
2103 wlvif = container_of(dwork, struct wl12xx_vif,
2104 pending_auth_complete_work);
2105 wl = wlvif->wl;
2107 mutex_lock(&wl->mutex);
2109 if (unlikely(wl->state != WLCORE_STATE_ON))
2110 goto out;
2113 * Make sure a second really passed since the last auth reply. Maybe
2114 * a second auth reply arrived while we were stuck on the mutex.
2115 * Check for a little less than the timeout to protect from scheduler
2116 * irregularities.
2118 time_spare = jiffies +
2119 msecs_to_jiffies(WLCORE_PEND_AUTH_ROC_TIMEOUT - 50);
2120 if (!time_after(time_spare, wlvif->pending_auth_reply_time))
2121 goto out;
2123 ret = wl1271_ps_elp_wakeup(wl);
2124 if (ret < 0)
2125 goto out;
2127 /* cancel the ROC if active */
2128 wlcore_update_inconn_sta(wl, wlvif, NULL, false);
2130 wl1271_ps_elp_sleep(wl);
2131 out:
2132 mutex_unlock(&wl->mutex);
2135 static int wl12xx_allocate_rate_policy(struct wl1271 *wl, u8 *idx)
2137 u8 policy = find_first_zero_bit(wl->rate_policies_map,
2138 WL12XX_MAX_RATE_POLICIES);
2139 if (policy >= WL12XX_MAX_RATE_POLICIES)
2140 return -EBUSY;
2142 __set_bit(policy, wl->rate_policies_map);
2143 *idx = policy;
2144 return 0;
2147 static void wl12xx_free_rate_policy(struct wl1271 *wl, u8 *idx)
2149 if (WARN_ON(*idx >= WL12XX_MAX_RATE_POLICIES))
2150 return;
2152 __clear_bit(*idx, wl->rate_policies_map);
2153 *idx = WL12XX_MAX_RATE_POLICIES;
2156 static int wlcore_allocate_klv_template(struct wl1271 *wl, u8 *idx)
2158 u8 policy = find_first_zero_bit(wl->klv_templates_map,
2159 WLCORE_MAX_KLV_TEMPLATES);
2160 if (policy >= WLCORE_MAX_KLV_TEMPLATES)
2161 return -EBUSY;
2163 __set_bit(policy, wl->klv_templates_map);
2164 *idx = policy;
2165 return 0;
2168 static void wlcore_free_klv_template(struct wl1271 *wl, u8 *idx)
2170 if (WARN_ON(*idx >= WLCORE_MAX_KLV_TEMPLATES))
2171 return;
2173 __clear_bit(*idx, wl->klv_templates_map);
2174 *idx = WLCORE_MAX_KLV_TEMPLATES;
2177 static u8 wl12xx_get_role_type(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2179 switch (wlvif->bss_type) {
2180 case BSS_TYPE_AP_BSS:
2181 if (wlvif->p2p)
2182 return WL1271_ROLE_P2P_GO;
2183 else
2184 return WL1271_ROLE_AP;
2186 case BSS_TYPE_STA_BSS:
2187 if (wlvif->p2p)
2188 return WL1271_ROLE_P2P_CL;
2189 else
2190 return WL1271_ROLE_STA;
2192 case BSS_TYPE_IBSS:
2193 return WL1271_ROLE_IBSS;
2195 default:
2196 wl1271_error("invalid bss_type: %d", wlvif->bss_type);
2198 return WL12XX_INVALID_ROLE_TYPE;
2201 static int wl12xx_init_vif_data(struct wl1271 *wl, struct ieee80211_vif *vif)
2203 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2204 int i;
2206 /* clear everything but the persistent data */
2207 memset(wlvif, 0, offsetof(struct wl12xx_vif, persistent));
2209 switch (ieee80211_vif_type_p2p(vif)) {
2210 case NL80211_IFTYPE_P2P_CLIENT:
2211 wlvif->p2p = 1;
2212 /* fall-through */
2213 case NL80211_IFTYPE_STATION:
2214 wlvif->bss_type = BSS_TYPE_STA_BSS;
2215 break;
2216 case NL80211_IFTYPE_ADHOC:
2217 wlvif->bss_type = BSS_TYPE_IBSS;
2218 break;
2219 case NL80211_IFTYPE_P2P_GO:
2220 wlvif->p2p = 1;
2221 /* fall-through */
2222 case NL80211_IFTYPE_AP:
2223 wlvif->bss_type = BSS_TYPE_AP_BSS;
2224 break;
2225 default:
2226 wlvif->bss_type = MAX_BSS_TYPE;
2227 return -EOPNOTSUPP;
2230 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2231 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2232 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2234 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2235 wlvif->bss_type == BSS_TYPE_IBSS) {
2236 /* init sta/ibss data */
2237 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2238 wl12xx_allocate_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2239 wl12xx_allocate_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2240 wl12xx_allocate_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2241 wlcore_allocate_klv_template(wl, &wlvif->sta.klv_template_id);
2242 wlvif->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
2243 wlvif->basic_rate = CONF_TX_RATE_MASK_BASIC;
2244 wlvif->rate_set = CONF_TX_RATE_MASK_BASIC;
2245 } else {
2246 /* init ap data */
2247 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2248 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2249 wl12xx_allocate_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2250 wl12xx_allocate_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2251 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2252 wl12xx_allocate_rate_policy(wl,
2253 &wlvif->ap.ucast_rate_idx[i]);
2254 wlvif->basic_rate_set = CONF_TX_ENABLED_RATES;
2256 * TODO: check if basic_rate shouldn't be
2257 * wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
2258 * instead (the same thing for STA above).
2260 wlvif->basic_rate = CONF_TX_ENABLED_RATES;
2261 /* TODO: this seems to be used only for STA, check it */
2262 wlvif->rate_set = CONF_TX_ENABLED_RATES;
2265 wlvif->bitrate_masks[IEEE80211_BAND_2GHZ] = wl->conf.tx.basic_rate;
2266 wlvif->bitrate_masks[IEEE80211_BAND_5GHZ] = wl->conf.tx.basic_rate_5;
2267 wlvif->beacon_int = WL1271_DEFAULT_BEACON_INT;
2270 * mac80211 configures some values globally, while we treat them
2271 * per-interface. thus, on init, we have to copy them from wl
2273 wlvif->band = wl->band;
2274 wlvif->channel = wl->channel;
2275 wlvif->power_level = wl->power_level;
2276 wlvif->channel_type = wl->channel_type;
2278 INIT_WORK(&wlvif->rx_streaming_enable_work,
2279 wl1271_rx_streaming_enable_work);
2280 INIT_WORK(&wlvif->rx_streaming_disable_work,
2281 wl1271_rx_streaming_disable_work);
2282 INIT_DELAYED_WORK(&wlvif->channel_switch_work,
2283 wlcore_channel_switch_work);
2284 INIT_DELAYED_WORK(&wlvif->connection_loss_work,
2285 wlcore_connection_loss_work);
2286 INIT_DELAYED_WORK(&wlvif->pending_auth_complete_work,
2287 wlcore_pending_auth_complete_work);
2288 INIT_LIST_HEAD(&wlvif->list);
2290 setup_timer(&wlvif->rx_streaming_timer, wl1271_rx_streaming_timer,
2291 (unsigned long) wlvif);
2292 return 0;
2295 static int wl12xx_init_fw(struct wl1271 *wl)
2297 int retries = WL1271_BOOT_RETRIES;
2298 bool booted = false;
2299 struct wiphy *wiphy = wl->hw->wiphy;
2300 int ret;
2302 while (retries) {
2303 retries--;
2304 ret = wl12xx_chip_wakeup(wl, false);
2305 if (ret < 0)
2306 goto power_off;
2308 ret = wl->ops->boot(wl);
2309 if (ret < 0)
2310 goto power_off;
2312 ret = wl1271_hw_init(wl);
2313 if (ret < 0)
2314 goto irq_disable;
2316 booted = true;
2317 break;
2319 irq_disable:
2320 mutex_unlock(&wl->mutex);
2321 /* Unlocking the mutex in the middle of handling is
2322 inherently unsafe. In this case we deem it safe to do,
2323 because we need to let any possibly pending IRQ out of
2324 the system (and while we are WLCORE_STATE_OFF the IRQ
2325 work function will not do anything.) Also, any other
2326 possible concurrent operations will fail due to the
2327 current state, hence the wl1271 struct should be safe. */
2328 wlcore_disable_interrupts(wl);
2329 wl1271_flush_deferred_work(wl);
2330 cancel_work_sync(&wl->netstack_work);
2331 mutex_lock(&wl->mutex);
2332 power_off:
2333 wl1271_power_off(wl);
2336 if (!booted) {
2337 wl1271_error("firmware boot failed despite %d retries",
2338 WL1271_BOOT_RETRIES);
2339 goto out;
2342 wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
2344 /* update hw/fw version info in wiphy struct */
2345 wiphy->hw_version = wl->chip.id;
2346 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
2347 sizeof(wiphy->fw_version));
2350 * Now we know if 11a is supported (info from the NVS), so disable
2351 * 11a channels if not supported
2353 if (!wl->enable_11a)
2354 wiphy->bands[IEEE80211_BAND_5GHZ]->n_channels = 0;
2356 wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
2357 wl->enable_11a ? "" : "not ");
2359 wl->state = WLCORE_STATE_ON;
2360 out:
2361 return ret;
2364 static bool wl12xx_dev_role_started(struct wl12xx_vif *wlvif)
2366 return wlvif->dev_hlid != WL12XX_INVALID_LINK_ID;
2370 * Check whether a fw switch (i.e. moving from one loaded
2371 * fw to another) is needed. This function is also responsible
2372 * for updating wl->last_vif_count, so it must be called before
2373 * loading a non-plt fw (so the correct fw (single-role/multi-role)
2374 * will be used).
2376 static bool wl12xx_need_fw_change(struct wl1271 *wl,
2377 struct vif_counter_data vif_counter_data,
2378 bool add)
2380 enum wl12xx_fw_type current_fw = wl->fw_type;
2381 u8 vif_count = vif_counter_data.counter;
2383 if (test_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags))
2384 return false;
2386 /* increase the vif count if this is a new vif */
2387 if (add && !vif_counter_data.cur_vif_running)
2388 vif_count++;
2390 wl->last_vif_count = vif_count;
2392 /* no need for fw change if the device is OFF */
2393 if (wl->state == WLCORE_STATE_OFF)
2394 return false;
2396 /* no need for fw change if a single fw is used */
2397 if (!wl->mr_fw_name)
2398 return false;
2400 if (vif_count > 1 && current_fw == WL12XX_FW_TYPE_NORMAL)
2401 return true;
2402 if (vif_count <= 1 && current_fw == WL12XX_FW_TYPE_MULTI)
2403 return true;
2405 return false;
2409 * Enter "forced psm". Make sure the sta is in psm against the ap,
2410 * to make the fw switch a bit more disconnection-persistent.
2412 static void wl12xx_force_active_psm(struct wl1271 *wl)
2414 struct wl12xx_vif *wlvif;
2416 wl12xx_for_each_wlvif_sta(wl, wlvif) {
2417 wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
2421 struct wlcore_hw_queue_iter_data {
2422 unsigned long hw_queue_map[BITS_TO_LONGS(WLCORE_NUM_MAC_ADDRESSES)];
2423 /* current vif */
2424 struct ieee80211_vif *vif;
2425 /* is the current vif among those iterated */
2426 bool cur_running;
2429 static void wlcore_hw_queue_iter(void *data, u8 *mac,
2430 struct ieee80211_vif *vif)
2432 struct wlcore_hw_queue_iter_data *iter_data = data;
2434 if (WARN_ON_ONCE(vif->hw_queue[0] == IEEE80211_INVAL_HW_QUEUE))
2435 return;
2437 if (iter_data->cur_running || vif == iter_data->vif) {
2438 iter_data->cur_running = true;
2439 return;
2442 __set_bit(vif->hw_queue[0] / NUM_TX_QUEUES, iter_data->hw_queue_map);
2445 static int wlcore_allocate_hw_queue_base(struct wl1271 *wl,
2446 struct wl12xx_vif *wlvif)
2448 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2449 struct wlcore_hw_queue_iter_data iter_data = {};
2450 int i, q_base;
2452 iter_data.vif = vif;
2454 /* mark all bits taken by active interfaces */
2455 ieee80211_iterate_active_interfaces_atomic(wl->hw,
2456 IEEE80211_IFACE_ITER_RESUME_ALL,
2457 wlcore_hw_queue_iter, &iter_data);
2459 /* the current vif is already running in mac80211 (resume/recovery) */
2460 if (iter_data.cur_running) {
2461 wlvif->hw_queue_base = vif->hw_queue[0];
2462 wl1271_debug(DEBUG_MAC80211,
2463 "using pre-allocated hw queue base %d",
2464 wlvif->hw_queue_base);
2466 /* interface type might have changed type */
2467 goto adjust_cab_queue;
2470 q_base = find_first_zero_bit(iter_data.hw_queue_map,
2471 WLCORE_NUM_MAC_ADDRESSES);
2472 if (q_base >= WLCORE_NUM_MAC_ADDRESSES)
2473 return -EBUSY;
2475 wlvif->hw_queue_base = q_base * NUM_TX_QUEUES;
2476 wl1271_debug(DEBUG_MAC80211, "allocating hw queue base: %d",
2477 wlvif->hw_queue_base);
2479 for (i = 0; i < NUM_TX_QUEUES; i++) {
2480 wl->queue_stop_reasons[wlvif->hw_queue_base + i] = 0;
2481 /* register hw queues in mac80211 */
2482 vif->hw_queue[i] = wlvif->hw_queue_base + i;
2485 adjust_cab_queue:
2486 /* the last places are reserved for cab queues per interface */
2487 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2488 vif->cab_queue = NUM_TX_QUEUES * WLCORE_NUM_MAC_ADDRESSES +
2489 wlvif->hw_queue_base / NUM_TX_QUEUES;
2490 else
2491 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2493 return 0;
2496 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
2497 struct ieee80211_vif *vif)
2499 struct wl1271 *wl = hw->priv;
2500 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2501 struct vif_counter_data vif_count;
2502 int ret = 0;
2503 u8 role_type;
2505 if (wl->plt) {
2506 wl1271_error("Adding Interface not allowed while in PLT mode");
2507 return -EBUSY;
2510 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
2511 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
2513 wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
2514 ieee80211_vif_type_p2p(vif), vif->addr);
2516 wl12xx_get_vif_count(hw, vif, &vif_count);
2518 mutex_lock(&wl->mutex);
2519 ret = wl1271_ps_elp_wakeup(wl);
2520 if (ret < 0)
2521 goto out_unlock;
2524 * in some very corner case HW recovery scenarios its possible to
2525 * get here before __wl1271_op_remove_interface is complete, so
2526 * opt out if that is the case.
2528 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) ||
2529 test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)) {
2530 ret = -EBUSY;
2531 goto out;
2535 ret = wl12xx_init_vif_data(wl, vif);
2536 if (ret < 0)
2537 goto out;
2539 wlvif->wl = wl;
2540 role_type = wl12xx_get_role_type(wl, wlvif);
2541 if (role_type == WL12XX_INVALID_ROLE_TYPE) {
2542 ret = -EINVAL;
2543 goto out;
2546 ret = wlcore_allocate_hw_queue_base(wl, wlvif);
2547 if (ret < 0)
2548 goto out;
2550 if (wl12xx_need_fw_change(wl, vif_count, true)) {
2551 wl12xx_force_active_psm(wl);
2552 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2553 mutex_unlock(&wl->mutex);
2554 wl1271_recovery_work(&wl->recovery_work);
2555 return 0;
2559 * TODO: after the nvs issue will be solved, move this block
2560 * to start(), and make sure here the driver is ON.
2562 if (wl->state == WLCORE_STATE_OFF) {
2564 * we still need this in order to configure the fw
2565 * while uploading the nvs
2567 memcpy(wl->addresses[0].addr, vif->addr, ETH_ALEN);
2569 ret = wl12xx_init_fw(wl);
2570 if (ret < 0)
2571 goto out;
2574 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2575 role_type, &wlvif->role_id);
2576 if (ret < 0)
2577 goto out;
2579 ret = wl1271_init_vif_specific(wl, vif);
2580 if (ret < 0)
2581 goto out;
2583 list_add(&wlvif->list, &wl->wlvif_list);
2584 set_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags);
2586 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2587 wl->ap_count++;
2588 else
2589 wl->sta_count++;
2590 out:
2591 wl1271_ps_elp_sleep(wl);
2592 out_unlock:
2593 mutex_unlock(&wl->mutex);
2595 return ret;
2598 static void __wl1271_op_remove_interface(struct wl1271 *wl,
2599 struct ieee80211_vif *vif,
2600 bool reset_tx_queues)
2602 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2603 int i, ret;
2604 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2606 wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
2608 if (!test_and_clear_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2609 return;
2611 /* because of hardware recovery, we may get here twice */
2612 if (wl->state == WLCORE_STATE_OFF)
2613 return;
2615 wl1271_info("down");
2617 if (wl->scan.state != WL1271_SCAN_STATE_IDLE &&
2618 wl->scan_wlvif == wlvif) {
2620 * Rearm the tx watchdog just before idling scan. This
2621 * prevents just-finished scans from triggering the watchdog
2623 wl12xx_rearm_tx_watchdog_locked(wl);
2625 wl->scan.state = WL1271_SCAN_STATE_IDLE;
2626 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
2627 wl->scan_wlvif = NULL;
2628 wl->scan.req = NULL;
2629 ieee80211_scan_completed(wl->hw, true);
2632 if (wl->sched_vif == wlvif)
2633 wl->sched_vif = NULL;
2635 if (wl->roc_vif == vif) {
2636 wl->roc_vif = NULL;
2637 ieee80211_remain_on_channel_expired(wl->hw);
2640 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2641 /* disable active roles */
2642 ret = wl1271_ps_elp_wakeup(wl);
2643 if (ret < 0)
2644 goto deinit;
2646 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2647 wlvif->bss_type == BSS_TYPE_IBSS) {
2648 if (wl12xx_dev_role_started(wlvif))
2649 wl12xx_stop_dev(wl, wlvif);
2652 ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
2653 if (ret < 0)
2654 goto deinit;
2656 wl1271_ps_elp_sleep(wl);
2658 deinit:
2659 wl12xx_tx_reset_wlvif(wl, wlvif);
2661 /* clear all hlids (except system_hlid) */
2662 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2664 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2665 wlvif->bss_type == BSS_TYPE_IBSS) {
2666 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2667 wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2668 wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2669 wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2670 wlcore_free_klv_template(wl, &wlvif->sta.klv_template_id);
2671 } else {
2672 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2673 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2674 wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2675 wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2676 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2677 wl12xx_free_rate_policy(wl,
2678 &wlvif->ap.ucast_rate_idx[i]);
2679 wl1271_free_ap_keys(wl, wlvif);
2682 dev_kfree_skb(wlvif->probereq);
2683 wlvif->probereq = NULL;
2684 if (wl->last_wlvif == wlvif)
2685 wl->last_wlvif = NULL;
2686 list_del(&wlvif->list);
2687 memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
2688 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2689 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2691 if (is_ap)
2692 wl->ap_count--;
2693 else
2694 wl->sta_count--;
2697 * Last AP, have more stations. Configure sleep auth according to STA.
2698 * Don't do thin on unintended recovery.
2700 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) &&
2701 !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags))
2702 goto unlock;
2704 if (wl->ap_count == 0 && is_ap) {
2705 /* mask ap events */
2706 wl->event_mask &= ~wl->ap_event_mask;
2707 wl1271_event_unmask(wl);
2710 if (wl->ap_count == 0 && is_ap && wl->sta_count) {
2711 u8 sta_auth = wl->conf.conn.sta_sleep_auth;
2712 /* Configure for power according to debugfs */
2713 if (sta_auth != WL1271_PSM_ILLEGAL)
2714 wl1271_acx_sleep_auth(wl, sta_auth);
2715 /* Configure for ELP power saving */
2716 else
2717 wl1271_acx_sleep_auth(wl, WL1271_PSM_ELP);
2720 unlock:
2721 mutex_unlock(&wl->mutex);
2723 del_timer_sync(&wlvif->rx_streaming_timer);
2724 cancel_work_sync(&wlvif->rx_streaming_enable_work);
2725 cancel_work_sync(&wlvif->rx_streaming_disable_work);
2726 cancel_delayed_work_sync(&wlvif->connection_loss_work);
2727 cancel_delayed_work_sync(&wlvif->channel_switch_work);
2728 cancel_delayed_work_sync(&wlvif->pending_auth_complete_work);
2730 mutex_lock(&wl->mutex);
2733 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2734 struct ieee80211_vif *vif)
2736 struct wl1271 *wl = hw->priv;
2737 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2738 struct wl12xx_vif *iter;
2739 struct vif_counter_data vif_count;
2741 wl12xx_get_vif_count(hw, vif, &vif_count);
2742 mutex_lock(&wl->mutex);
2744 if (wl->state == WLCORE_STATE_OFF ||
2745 !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2746 goto out;
2749 * wl->vif can be null here if someone shuts down the interface
2750 * just when hardware recovery has been started.
2752 wl12xx_for_each_wlvif(wl, iter) {
2753 if (iter != wlvif)
2754 continue;
2756 __wl1271_op_remove_interface(wl, vif, true);
2757 break;
2759 WARN_ON(iter != wlvif);
2760 if (wl12xx_need_fw_change(wl, vif_count, false)) {
2761 wl12xx_force_active_psm(wl);
2762 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2763 wl12xx_queue_recovery_work(wl);
2765 out:
2766 mutex_unlock(&wl->mutex);
2769 static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2770 struct ieee80211_vif *vif,
2771 enum nl80211_iftype new_type, bool p2p)
2773 struct wl1271 *wl = hw->priv;
2774 int ret;
2776 set_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2777 wl1271_op_remove_interface(hw, vif);
2779 vif->type = new_type;
2780 vif->p2p = p2p;
2781 ret = wl1271_op_add_interface(hw, vif);
2783 clear_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2784 return ret;
2787 static int wlcore_join(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2789 int ret;
2790 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
2793 * One of the side effects of the JOIN command is that is clears
2794 * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2795 * to a WPA/WPA2 access point will therefore kill the data-path.
2796 * Currently the only valid scenario for JOIN during association
2797 * is on roaming, in which case we will also be given new keys.
2798 * Keep the below message for now, unless it starts bothering
2799 * users who really like to roam a lot :)
2801 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2802 wl1271_info("JOIN while associated.");
2804 /* clear encryption type */
2805 wlvif->encryption_type = KEY_NONE;
2807 if (is_ibss)
2808 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
2809 else {
2810 if (wl->quirks & WLCORE_QUIRK_START_STA_FAILS) {
2812 * TODO: this is an ugly workaround for wl12xx fw
2813 * bug - we are not able to tx/rx after the first
2814 * start_sta, so make dummy start+stop calls,
2815 * and then call start_sta again.
2816 * this should be fixed in the fw.
2818 wl12xx_cmd_role_start_sta(wl, wlvif);
2819 wl12xx_cmd_role_stop_sta(wl, wlvif);
2822 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
2825 return ret;
2828 static int wl1271_ssid_set(struct wl12xx_vif *wlvif, struct sk_buff *skb,
2829 int offset)
2831 u8 ssid_len;
2832 const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
2833 skb->len - offset);
2835 if (!ptr) {
2836 wl1271_error("No SSID in IEs!");
2837 return -ENOENT;
2840 ssid_len = ptr[1];
2841 if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2842 wl1271_error("SSID is too long!");
2843 return -EINVAL;
2846 wlvif->ssid_len = ssid_len;
2847 memcpy(wlvif->ssid, ptr+2, ssid_len);
2848 return 0;
2851 static int wlcore_set_ssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2853 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2854 struct sk_buff *skb;
2855 int ieoffset;
2857 /* we currently only support setting the ssid from the ap probe req */
2858 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
2859 return -EINVAL;
2861 skb = ieee80211_ap_probereq_get(wl->hw, vif);
2862 if (!skb)
2863 return -EINVAL;
2865 ieoffset = offsetof(struct ieee80211_mgmt,
2866 u.probe_req.variable);
2867 wl1271_ssid_set(wlvif, skb, ieoffset);
2868 dev_kfree_skb(skb);
2870 return 0;
2873 static int wlcore_set_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2874 struct ieee80211_bss_conf *bss_conf,
2875 u32 sta_rate_set)
2877 int ieoffset;
2878 int ret;
2880 wlvif->aid = bss_conf->aid;
2881 wlvif->channel_type = cfg80211_get_chandef_type(&bss_conf->chandef);
2882 wlvif->beacon_int = bss_conf->beacon_int;
2883 wlvif->wmm_enabled = bss_conf->qos;
2885 set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2888 * with wl1271, we don't need to update the
2889 * beacon_int and dtim_period, because the firmware
2890 * updates it by itself when the first beacon is
2891 * received after a join.
2893 ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
2894 if (ret < 0)
2895 return ret;
2898 * Get a template for hardware connection maintenance
2900 dev_kfree_skb(wlvif->probereq);
2901 wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
2902 wlvif,
2903 NULL);
2904 ieoffset = offsetof(struct ieee80211_mgmt,
2905 u.probe_req.variable);
2906 wl1271_ssid_set(wlvif, wlvif->probereq, ieoffset);
2908 /* enable the connection monitoring feature */
2909 ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
2910 if (ret < 0)
2911 return ret;
2914 * The join command disable the keep-alive mode, shut down its process,
2915 * and also clear the template config, so we need to reset it all after
2916 * the join. The acx_aid starts the keep-alive process, and the order
2917 * of the commands below is relevant.
2919 ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
2920 if (ret < 0)
2921 return ret;
2923 ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
2924 if (ret < 0)
2925 return ret;
2927 ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
2928 if (ret < 0)
2929 return ret;
2931 ret = wl1271_acx_keep_alive_config(wl, wlvif,
2932 wlvif->sta.klv_template_id,
2933 ACX_KEEP_ALIVE_TPL_VALID);
2934 if (ret < 0)
2935 return ret;
2938 * The default fw psm configuration is AUTO, while mac80211 default
2939 * setting is off (ACTIVE), so sync the fw with the correct value.
2941 ret = wl1271_ps_set_mode(wl, wlvif, STATION_ACTIVE_MODE);
2942 if (ret < 0)
2943 return ret;
2945 if (sta_rate_set) {
2946 wlvif->rate_set =
2947 wl1271_tx_enabled_rates_get(wl,
2948 sta_rate_set,
2949 wlvif->band);
2950 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2951 if (ret < 0)
2952 return ret;
2955 return ret;
2958 static int wlcore_unset_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2960 int ret;
2961 bool sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
2963 /* make sure we are connected (sta) joined */
2964 if (sta &&
2965 !test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2966 return false;
2968 /* make sure we are joined (ibss) */
2969 if (!sta &&
2970 test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags))
2971 return false;
2973 if (sta) {
2974 /* use defaults when not associated */
2975 wlvif->aid = 0;
2977 /* free probe-request template */
2978 dev_kfree_skb(wlvif->probereq);
2979 wlvif->probereq = NULL;
2981 /* disable connection monitor features */
2982 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
2983 if (ret < 0)
2984 return ret;
2986 /* Disable the keep-alive feature */
2987 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
2988 if (ret < 0)
2989 return ret;
2991 /* disable beacon filtering */
2992 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
2993 if (ret < 0)
2994 return ret;
2997 if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
2998 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
3000 wl12xx_cmd_stop_channel_switch(wl, wlvif);
3001 ieee80211_chswitch_done(vif, false);
3002 cancel_delayed_work(&wlvif->channel_switch_work);
3005 /* invalidate keep-alive template */
3006 wl1271_acx_keep_alive_config(wl, wlvif,
3007 wlvif->sta.klv_template_id,
3008 ACX_KEEP_ALIVE_TPL_INVALID);
3010 return 0;
3013 static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3015 wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
3016 wlvif->rate_set = wlvif->basic_rate_set;
3019 static void wl1271_sta_handle_idle(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3020 bool idle)
3022 bool cur_idle = !test_bit(WLVIF_FLAG_ACTIVE, &wlvif->flags);
3024 if (idle == cur_idle)
3025 return;
3027 if (idle) {
3028 clear_bit(WLVIF_FLAG_ACTIVE, &wlvif->flags);
3029 } else {
3030 /* The current firmware only supports sched_scan in idle */
3031 if (wl->sched_vif == wlvif)
3032 wl->ops->sched_scan_stop(wl, wlvif);
3034 set_bit(WLVIF_FLAG_ACTIVE, &wlvif->flags);
3038 static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3039 struct ieee80211_conf *conf, u32 changed)
3041 int ret;
3043 if (conf->power_level != wlvif->power_level) {
3044 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
3045 if (ret < 0)
3046 return ret;
3048 wlvif->power_level = conf->power_level;
3051 return 0;
3054 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
3056 struct wl1271 *wl = hw->priv;
3057 struct wl12xx_vif *wlvif;
3058 struct ieee80211_conf *conf = &hw->conf;
3059 int ret = 0;
3061 wl1271_debug(DEBUG_MAC80211, "mac80211 config psm %s power %d %s"
3062 " changed 0x%x",
3063 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
3064 conf->power_level,
3065 conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
3066 changed);
3068 mutex_lock(&wl->mutex);
3070 if (changed & IEEE80211_CONF_CHANGE_POWER)
3071 wl->power_level = conf->power_level;
3073 if (unlikely(wl->state != WLCORE_STATE_ON))
3074 goto out;
3076 ret = wl1271_ps_elp_wakeup(wl);
3077 if (ret < 0)
3078 goto out;
3080 /* configure each interface */
3081 wl12xx_for_each_wlvif(wl, wlvif) {
3082 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
3083 if (ret < 0)
3084 goto out_sleep;
3087 out_sleep:
3088 wl1271_ps_elp_sleep(wl);
3090 out:
3091 mutex_unlock(&wl->mutex);
3093 return ret;
3096 struct wl1271_filter_params {
3097 bool enabled;
3098 int mc_list_length;
3099 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
3102 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
3103 struct netdev_hw_addr_list *mc_list)
3105 struct wl1271_filter_params *fp;
3106 struct netdev_hw_addr *ha;
3108 fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
3109 if (!fp) {
3110 wl1271_error("Out of memory setting filters.");
3111 return 0;
3114 /* update multicast filtering parameters */
3115 fp->mc_list_length = 0;
3116 if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
3117 fp->enabled = false;
3118 } else {
3119 fp->enabled = true;
3120 netdev_hw_addr_list_for_each(ha, mc_list) {
3121 memcpy(fp->mc_list[fp->mc_list_length],
3122 ha->addr, ETH_ALEN);
3123 fp->mc_list_length++;
3127 return (u64)(unsigned long)fp;
3130 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
3131 FIF_ALLMULTI | \
3132 FIF_FCSFAIL | \
3133 FIF_BCN_PRBRESP_PROMISC | \
3134 FIF_CONTROL | \
3135 FIF_OTHER_BSS)
3137 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
3138 unsigned int changed,
3139 unsigned int *total, u64 multicast)
3141 struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
3142 struct wl1271 *wl = hw->priv;
3143 struct wl12xx_vif *wlvif;
3145 int ret;
3147 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
3148 " total %x", changed, *total);
3150 mutex_lock(&wl->mutex);
3152 *total &= WL1271_SUPPORTED_FILTERS;
3153 changed &= WL1271_SUPPORTED_FILTERS;
3155 if (unlikely(wl->state != WLCORE_STATE_ON))
3156 goto out;
3158 ret = wl1271_ps_elp_wakeup(wl);
3159 if (ret < 0)
3160 goto out;
3162 wl12xx_for_each_wlvif(wl, wlvif) {
3163 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
3164 if (*total & FIF_ALLMULTI)
3165 ret = wl1271_acx_group_address_tbl(wl, wlvif,
3166 false,
3167 NULL, 0);
3168 else if (fp)
3169 ret = wl1271_acx_group_address_tbl(wl, wlvif,
3170 fp->enabled,
3171 fp->mc_list,
3172 fp->mc_list_length);
3173 if (ret < 0)
3174 goto out_sleep;
3179 * the fw doesn't provide an api to configure the filters. instead,
3180 * the filters configuration is based on the active roles / ROC
3181 * state.
3184 out_sleep:
3185 wl1271_ps_elp_sleep(wl);
3187 out:
3188 mutex_unlock(&wl->mutex);
3189 kfree(fp);
3192 static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3193 u8 id, u8 key_type, u8 key_size,
3194 const u8 *key, u8 hlid, u32 tx_seq_32,
3195 u16 tx_seq_16)
3197 struct wl1271_ap_key *ap_key;
3198 int i;
3200 wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
3202 if (key_size > MAX_KEY_SIZE)
3203 return -EINVAL;
3206 * Find next free entry in ap_keys. Also check we are not replacing
3207 * an existing key.
3209 for (i = 0; i < MAX_NUM_KEYS; i++) {
3210 if (wlvif->ap.recorded_keys[i] == NULL)
3211 break;
3213 if (wlvif->ap.recorded_keys[i]->id == id) {
3214 wl1271_warning("trying to record key replacement");
3215 return -EINVAL;
3219 if (i == MAX_NUM_KEYS)
3220 return -EBUSY;
3222 ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
3223 if (!ap_key)
3224 return -ENOMEM;
3226 ap_key->id = id;
3227 ap_key->key_type = key_type;
3228 ap_key->key_size = key_size;
3229 memcpy(ap_key->key, key, key_size);
3230 ap_key->hlid = hlid;
3231 ap_key->tx_seq_32 = tx_seq_32;
3232 ap_key->tx_seq_16 = tx_seq_16;
3234 wlvif->ap.recorded_keys[i] = ap_key;
3235 return 0;
3238 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3240 int i;
3242 for (i = 0; i < MAX_NUM_KEYS; i++) {
3243 kfree(wlvif->ap.recorded_keys[i]);
3244 wlvif->ap.recorded_keys[i] = NULL;
3248 static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3250 int i, ret = 0;
3251 struct wl1271_ap_key *key;
3252 bool wep_key_added = false;
3254 for (i = 0; i < MAX_NUM_KEYS; i++) {
3255 u8 hlid;
3256 if (wlvif->ap.recorded_keys[i] == NULL)
3257 break;
3259 key = wlvif->ap.recorded_keys[i];
3260 hlid = key->hlid;
3261 if (hlid == WL12XX_INVALID_LINK_ID)
3262 hlid = wlvif->ap.bcast_hlid;
3264 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
3265 key->id, key->key_type,
3266 key->key_size, key->key,
3267 hlid, key->tx_seq_32,
3268 key->tx_seq_16);
3269 if (ret < 0)
3270 goto out;
3272 if (key->key_type == KEY_WEP)
3273 wep_key_added = true;
3276 if (wep_key_added) {
3277 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
3278 wlvif->ap.bcast_hlid);
3279 if (ret < 0)
3280 goto out;
3283 out:
3284 wl1271_free_ap_keys(wl, wlvif);
3285 return ret;
3288 static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3289 u16 action, u8 id, u8 key_type,
3290 u8 key_size, const u8 *key, u32 tx_seq_32,
3291 u16 tx_seq_16, struct ieee80211_sta *sta)
3293 int ret;
3294 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
3296 if (is_ap) {
3297 struct wl1271_station *wl_sta;
3298 u8 hlid;
3300 if (sta) {
3301 wl_sta = (struct wl1271_station *)sta->drv_priv;
3302 hlid = wl_sta->hlid;
3303 } else {
3304 hlid = wlvif->ap.bcast_hlid;
3307 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3309 * We do not support removing keys after AP shutdown.
3310 * Pretend we do to make mac80211 happy.
3312 if (action != KEY_ADD_OR_REPLACE)
3313 return 0;
3315 ret = wl1271_record_ap_key(wl, wlvif, id,
3316 key_type, key_size,
3317 key, hlid, tx_seq_32,
3318 tx_seq_16);
3319 } else {
3320 ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
3321 id, key_type, key_size,
3322 key, hlid, tx_seq_32,
3323 tx_seq_16);
3326 if (ret < 0)
3327 return ret;
3328 } else {
3329 const u8 *addr;
3330 static const u8 bcast_addr[ETH_ALEN] = {
3331 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
3334 addr = sta ? sta->addr : bcast_addr;
3336 if (is_zero_ether_addr(addr)) {
3337 /* We dont support TX only encryption */
3338 return -EOPNOTSUPP;
3341 /* The wl1271 does not allow to remove unicast keys - they
3342 will be cleared automatically on next CMD_JOIN. Ignore the
3343 request silently, as we dont want the mac80211 to emit
3344 an error message. */
3345 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
3346 return 0;
3348 /* don't remove key if hlid was already deleted */
3349 if (action == KEY_REMOVE &&
3350 wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
3351 return 0;
3353 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
3354 id, key_type, key_size,
3355 key, addr, tx_seq_32,
3356 tx_seq_16);
3357 if (ret < 0)
3358 return ret;
3362 return 0;
3365 static int wlcore_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3366 struct ieee80211_vif *vif,
3367 struct ieee80211_sta *sta,
3368 struct ieee80211_key_conf *key_conf)
3370 struct wl1271 *wl = hw->priv;
3371 int ret;
3372 bool might_change_spare =
3373 key_conf->cipher == WL1271_CIPHER_SUITE_GEM ||
3374 key_conf->cipher == WLAN_CIPHER_SUITE_TKIP;
3376 if (might_change_spare) {
3378 * stop the queues and flush to ensure the next packets are
3379 * in sync with FW spare block accounting
3381 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3382 wl1271_tx_flush(wl);
3385 mutex_lock(&wl->mutex);
3387 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3388 ret = -EAGAIN;
3389 goto out_wake_queues;
3392 ret = wl1271_ps_elp_wakeup(wl);
3393 if (ret < 0)
3394 goto out_wake_queues;
3396 ret = wlcore_hw_set_key(wl, cmd, vif, sta, key_conf);
3398 wl1271_ps_elp_sleep(wl);
3400 out_wake_queues:
3401 if (might_change_spare)
3402 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3404 mutex_unlock(&wl->mutex);
3406 return ret;
3409 int wlcore_set_key(struct wl1271 *wl, enum set_key_cmd cmd,
3410 struct ieee80211_vif *vif,
3411 struct ieee80211_sta *sta,
3412 struct ieee80211_key_conf *key_conf)
3414 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3415 int ret;
3416 u32 tx_seq_32 = 0;
3417 u16 tx_seq_16 = 0;
3418 u8 key_type;
3419 u8 hlid;
3421 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
3423 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
3424 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
3425 key_conf->cipher, key_conf->keyidx,
3426 key_conf->keylen, key_conf->flags);
3427 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
3429 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
3430 if (sta) {
3431 struct wl1271_station *wl_sta = (void *)sta->drv_priv;
3432 hlid = wl_sta->hlid;
3433 } else {
3434 hlid = wlvif->ap.bcast_hlid;
3436 else
3437 hlid = wlvif->sta.hlid;
3439 if (hlid != WL12XX_INVALID_LINK_ID) {
3440 u64 tx_seq = wl->links[hlid].total_freed_pkts;
3441 tx_seq_32 = WL1271_TX_SECURITY_HI32(tx_seq);
3442 tx_seq_16 = WL1271_TX_SECURITY_LO16(tx_seq);
3445 switch (key_conf->cipher) {
3446 case WLAN_CIPHER_SUITE_WEP40:
3447 case WLAN_CIPHER_SUITE_WEP104:
3448 key_type = KEY_WEP;
3450 key_conf->hw_key_idx = key_conf->keyidx;
3451 break;
3452 case WLAN_CIPHER_SUITE_TKIP:
3453 key_type = KEY_TKIP;
3454 key_conf->hw_key_idx = key_conf->keyidx;
3455 break;
3456 case WLAN_CIPHER_SUITE_CCMP:
3457 key_type = KEY_AES;
3458 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3459 break;
3460 case WL1271_CIPHER_SUITE_GEM:
3461 key_type = KEY_GEM;
3462 break;
3463 default:
3464 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
3466 return -EOPNOTSUPP;
3469 switch (cmd) {
3470 case SET_KEY:
3471 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
3472 key_conf->keyidx, key_type,
3473 key_conf->keylen, key_conf->key,
3474 tx_seq_32, tx_seq_16, sta);
3475 if (ret < 0) {
3476 wl1271_error("Could not add or replace key");
3477 return ret;
3481 * reconfiguring arp response if the unicast (or common)
3482 * encryption key type was changed
3484 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3485 (sta || key_type == KEY_WEP) &&
3486 wlvif->encryption_type != key_type) {
3487 wlvif->encryption_type = key_type;
3488 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3489 if (ret < 0) {
3490 wl1271_warning("build arp rsp failed: %d", ret);
3491 return ret;
3494 break;
3496 case DISABLE_KEY:
3497 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
3498 key_conf->keyidx, key_type,
3499 key_conf->keylen, key_conf->key,
3500 0, 0, sta);
3501 if (ret < 0) {
3502 wl1271_error("Could not remove key");
3503 return ret;
3505 break;
3507 default:
3508 wl1271_error("Unsupported key cmd 0x%x", cmd);
3509 return -EOPNOTSUPP;
3512 return ret;
3514 EXPORT_SYMBOL_GPL(wlcore_set_key);
3516 static void wl1271_op_set_default_key_idx(struct ieee80211_hw *hw,
3517 struct ieee80211_vif *vif,
3518 int key_idx)
3520 struct wl1271 *wl = hw->priv;
3521 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3522 int ret;
3524 wl1271_debug(DEBUG_MAC80211, "mac80211 set default key idx %d",
3525 key_idx);
3527 /* we don't handle unsetting of default key */
3528 if (key_idx == -1)
3529 return;
3531 mutex_lock(&wl->mutex);
3533 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3534 ret = -EAGAIN;
3535 goto out_unlock;
3538 ret = wl1271_ps_elp_wakeup(wl);
3539 if (ret < 0)
3540 goto out_unlock;
3542 wlvif->default_key = key_idx;
3544 /* the default WEP key needs to be configured at least once */
3545 if (wlvif->encryption_type == KEY_WEP) {
3546 ret = wl12xx_cmd_set_default_wep_key(wl,
3547 key_idx,
3548 wlvif->sta.hlid);
3549 if (ret < 0)
3550 goto out_sleep;
3553 out_sleep:
3554 wl1271_ps_elp_sleep(wl);
3556 out_unlock:
3557 mutex_unlock(&wl->mutex);
3560 void wlcore_regdomain_config(struct wl1271 *wl)
3562 int ret;
3564 if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
3565 return;
3567 mutex_lock(&wl->mutex);
3569 if (unlikely(wl->state != WLCORE_STATE_ON))
3570 goto out;
3572 ret = wl1271_ps_elp_wakeup(wl);
3573 if (ret < 0)
3574 goto out;
3576 ret = wlcore_cmd_regdomain_config_locked(wl);
3577 if (ret < 0) {
3578 wl12xx_queue_recovery_work(wl);
3579 goto out;
3582 wl1271_ps_elp_sleep(wl);
3583 out:
3584 mutex_unlock(&wl->mutex);
3587 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
3588 struct ieee80211_vif *vif,
3589 struct ieee80211_scan_request *hw_req)
3591 struct cfg80211_scan_request *req = &hw_req->req;
3592 struct wl1271 *wl = hw->priv;
3593 int ret;
3594 u8 *ssid = NULL;
3595 size_t len = 0;
3597 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3599 if (req->n_ssids) {
3600 ssid = req->ssids[0].ssid;
3601 len = req->ssids[0].ssid_len;
3604 mutex_lock(&wl->mutex);
3606 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3608 * We cannot return -EBUSY here because cfg80211 will expect
3609 * a call to ieee80211_scan_completed if we do - in this case
3610 * there won't be any call.
3612 ret = -EAGAIN;
3613 goto out;
3616 ret = wl1271_ps_elp_wakeup(wl);
3617 if (ret < 0)
3618 goto out;
3620 /* fail if there is any role in ROC */
3621 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
3622 /* don't allow scanning right now */
3623 ret = -EBUSY;
3624 goto out_sleep;
3627 ret = wlcore_scan(hw->priv, vif, ssid, len, req);
3628 out_sleep:
3629 wl1271_ps_elp_sleep(wl);
3630 out:
3631 mutex_unlock(&wl->mutex);
3633 return ret;
3636 static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3637 struct ieee80211_vif *vif)
3639 struct wl1271 *wl = hw->priv;
3640 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3641 int ret;
3643 wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3645 mutex_lock(&wl->mutex);
3647 if (unlikely(wl->state != WLCORE_STATE_ON))
3648 goto out;
3650 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3651 goto out;
3653 ret = wl1271_ps_elp_wakeup(wl);
3654 if (ret < 0)
3655 goto out;
3657 if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
3658 ret = wl->ops->scan_stop(wl, wlvif);
3659 if (ret < 0)
3660 goto out_sleep;
3664 * Rearm the tx watchdog just before idling scan. This
3665 * prevents just-finished scans from triggering the watchdog
3667 wl12xx_rearm_tx_watchdog_locked(wl);
3669 wl->scan.state = WL1271_SCAN_STATE_IDLE;
3670 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
3671 wl->scan_wlvif = NULL;
3672 wl->scan.req = NULL;
3673 ieee80211_scan_completed(wl->hw, true);
3675 out_sleep:
3676 wl1271_ps_elp_sleep(wl);
3677 out:
3678 mutex_unlock(&wl->mutex);
3680 cancel_delayed_work_sync(&wl->scan_complete_work);
3683 static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3684 struct ieee80211_vif *vif,
3685 struct cfg80211_sched_scan_request *req,
3686 struct ieee80211_scan_ies *ies)
3688 struct wl1271 *wl = hw->priv;
3689 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3690 int ret;
3692 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3694 mutex_lock(&wl->mutex);
3696 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3697 ret = -EAGAIN;
3698 goto out;
3701 ret = wl1271_ps_elp_wakeup(wl);
3702 if (ret < 0)
3703 goto out;
3705 ret = wl->ops->sched_scan_start(wl, wlvif, req, ies);
3706 if (ret < 0)
3707 goto out_sleep;
3709 wl->sched_vif = wlvif;
3711 out_sleep:
3712 wl1271_ps_elp_sleep(wl);
3713 out:
3714 mutex_unlock(&wl->mutex);
3715 return ret;
3718 static int wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3719 struct ieee80211_vif *vif)
3721 struct wl1271 *wl = hw->priv;
3722 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3723 int ret;
3725 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3727 mutex_lock(&wl->mutex);
3729 if (unlikely(wl->state != WLCORE_STATE_ON))
3730 goto out;
3732 ret = wl1271_ps_elp_wakeup(wl);
3733 if (ret < 0)
3734 goto out;
3736 wl->ops->sched_scan_stop(wl, wlvif);
3738 wl1271_ps_elp_sleep(wl);
3739 out:
3740 mutex_unlock(&wl->mutex);
3742 return 0;
3745 static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3747 struct wl1271 *wl = hw->priv;
3748 int ret = 0;
3750 mutex_lock(&wl->mutex);
3752 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3753 ret = -EAGAIN;
3754 goto out;
3757 ret = wl1271_ps_elp_wakeup(wl);
3758 if (ret < 0)
3759 goto out;
3761 ret = wl1271_acx_frag_threshold(wl, value);
3762 if (ret < 0)
3763 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3765 wl1271_ps_elp_sleep(wl);
3767 out:
3768 mutex_unlock(&wl->mutex);
3770 return ret;
3773 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3775 struct wl1271 *wl = hw->priv;
3776 struct wl12xx_vif *wlvif;
3777 int ret = 0;
3779 mutex_lock(&wl->mutex);
3781 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3782 ret = -EAGAIN;
3783 goto out;
3786 ret = wl1271_ps_elp_wakeup(wl);
3787 if (ret < 0)
3788 goto out;
3790 wl12xx_for_each_wlvif(wl, wlvif) {
3791 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3792 if (ret < 0)
3793 wl1271_warning("set rts threshold failed: %d", ret);
3795 wl1271_ps_elp_sleep(wl);
3797 out:
3798 mutex_unlock(&wl->mutex);
3800 return ret;
3803 static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3805 int len;
3806 const u8 *next, *end = skb->data + skb->len;
3807 u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3808 skb->len - ieoffset);
3809 if (!ie)
3810 return;
3811 len = ie[1] + 2;
3812 next = ie + len;
3813 memmove(ie, next, end - next);
3814 skb_trim(skb, skb->len - len);
3817 static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3818 unsigned int oui, u8 oui_type,
3819 int ieoffset)
3821 int len;
3822 const u8 *next, *end = skb->data + skb->len;
3823 u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3824 skb->data + ieoffset,
3825 skb->len - ieoffset);
3826 if (!ie)
3827 return;
3828 len = ie[1] + 2;
3829 next = ie + len;
3830 memmove(ie, next, end - next);
3831 skb_trim(skb, skb->len - len);
3834 static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3835 struct ieee80211_vif *vif)
3837 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3838 struct sk_buff *skb;
3839 int ret;
3841 skb = ieee80211_proberesp_get(wl->hw, vif);
3842 if (!skb)
3843 return -EOPNOTSUPP;
3845 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3846 CMD_TEMPL_AP_PROBE_RESPONSE,
3847 skb->data,
3848 skb->len, 0,
3849 rates);
3850 dev_kfree_skb(skb);
3852 if (ret < 0)
3853 goto out;
3855 wl1271_debug(DEBUG_AP, "probe response updated");
3856 set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3858 out:
3859 return ret;
3862 static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3863 struct ieee80211_vif *vif,
3864 u8 *probe_rsp_data,
3865 size_t probe_rsp_len,
3866 u32 rates)
3868 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3869 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
3870 u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3871 int ssid_ie_offset, ie_offset, templ_len;
3872 const u8 *ptr;
3874 /* no need to change probe response if the SSID is set correctly */
3875 if (wlvif->ssid_len > 0)
3876 return wl1271_cmd_template_set(wl, wlvif->role_id,
3877 CMD_TEMPL_AP_PROBE_RESPONSE,
3878 probe_rsp_data,
3879 probe_rsp_len, 0,
3880 rates);
3882 if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3883 wl1271_error("probe_rsp template too big");
3884 return -EINVAL;
3887 /* start searching from IE offset */
3888 ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3890 ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3891 probe_rsp_len - ie_offset);
3892 if (!ptr) {
3893 wl1271_error("No SSID in beacon!");
3894 return -EINVAL;
3897 ssid_ie_offset = ptr - probe_rsp_data;
3898 ptr += (ptr[1] + 2);
3900 memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3902 /* insert SSID from bss_conf */
3903 probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3904 probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3905 memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3906 bss_conf->ssid, bss_conf->ssid_len);
3907 templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3909 memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3910 ptr, probe_rsp_len - (ptr - probe_rsp_data));
3911 templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3913 return wl1271_cmd_template_set(wl, wlvif->role_id,
3914 CMD_TEMPL_AP_PROBE_RESPONSE,
3915 probe_rsp_templ,
3916 templ_len, 0,
3917 rates);
3920 static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
3921 struct ieee80211_vif *vif,
3922 struct ieee80211_bss_conf *bss_conf,
3923 u32 changed)
3925 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3926 int ret = 0;
3928 if (changed & BSS_CHANGED_ERP_SLOT) {
3929 if (bss_conf->use_short_slot)
3930 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
3931 else
3932 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
3933 if (ret < 0) {
3934 wl1271_warning("Set slot time failed %d", ret);
3935 goto out;
3939 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3940 if (bss_conf->use_short_preamble)
3941 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
3942 else
3943 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
3946 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3947 if (bss_conf->use_cts_prot)
3948 ret = wl1271_acx_cts_protect(wl, wlvif,
3949 CTSPROTECT_ENABLE);
3950 else
3951 ret = wl1271_acx_cts_protect(wl, wlvif,
3952 CTSPROTECT_DISABLE);
3953 if (ret < 0) {
3954 wl1271_warning("Set ctsprotect failed %d", ret);
3955 goto out;
3959 out:
3960 return ret;
3963 static int wlcore_set_beacon_template(struct wl1271 *wl,
3964 struct ieee80211_vif *vif,
3965 bool is_ap)
3967 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3968 struct ieee80211_hdr *hdr;
3969 u32 min_rate;
3970 int ret;
3971 int ieoffset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
3972 struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
3973 u16 tmpl_id;
3975 if (!beacon) {
3976 ret = -EINVAL;
3977 goto out;
3980 wl1271_debug(DEBUG_MASTER, "beacon updated");
3982 ret = wl1271_ssid_set(wlvif, beacon, ieoffset);
3983 if (ret < 0) {
3984 dev_kfree_skb(beacon);
3985 goto out;
3987 min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3988 tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
3989 CMD_TEMPL_BEACON;
3990 ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
3991 beacon->data,
3992 beacon->len, 0,
3993 min_rate);
3994 if (ret < 0) {
3995 dev_kfree_skb(beacon);
3996 goto out;
3999 wlvif->wmm_enabled =
4000 cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
4001 WLAN_OUI_TYPE_MICROSOFT_WMM,
4002 beacon->data + ieoffset,
4003 beacon->len - ieoffset);
4006 * In case we already have a probe-resp beacon set explicitly
4007 * by usermode, don't use the beacon data.
4009 if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
4010 goto end_bcn;
4012 /* remove TIM ie from probe response */
4013 wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
4016 * remove p2p ie from probe response.
4017 * the fw reponds to probe requests that don't include
4018 * the p2p ie. probe requests with p2p ie will be passed,
4019 * and will be responded by the supplicant (the spec
4020 * forbids including the p2p ie when responding to probe
4021 * requests that didn't include it).
4023 wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
4024 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
4026 hdr = (struct ieee80211_hdr *) beacon->data;
4027 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4028 IEEE80211_STYPE_PROBE_RESP);
4029 if (is_ap)
4030 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
4031 beacon->data,
4032 beacon->len,
4033 min_rate);
4034 else
4035 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
4036 CMD_TEMPL_PROBE_RESPONSE,
4037 beacon->data,
4038 beacon->len, 0,
4039 min_rate);
4040 end_bcn:
4041 dev_kfree_skb(beacon);
4042 if (ret < 0)
4043 goto out;
4045 out:
4046 return ret;
4049 static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
4050 struct ieee80211_vif *vif,
4051 struct ieee80211_bss_conf *bss_conf,
4052 u32 changed)
4054 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4055 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
4056 int ret = 0;
4058 if (changed & BSS_CHANGED_BEACON_INT) {
4059 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
4060 bss_conf->beacon_int);
4062 wlvif->beacon_int = bss_conf->beacon_int;
4065 if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
4066 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4068 wl1271_ap_set_probe_resp_tmpl(wl, rate, vif);
4071 if (changed & BSS_CHANGED_BEACON) {
4072 ret = wlcore_set_beacon_template(wl, vif, is_ap);
4073 if (ret < 0)
4074 goto out;
4077 out:
4078 if (ret != 0)
4079 wl1271_error("beacon info change failed: %d", ret);
4080 return ret;
4083 /* AP mode changes */
4084 static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
4085 struct ieee80211_vif *vif,
4086 struct ieee80211_bss_conf *bss_conf,
4087 u32 changed)
4089 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4090 int ret = 0;
4092 if (changed & BSS_CHANGED_BASIC_RATES) {
4093 u32 rates = bss_conf->basic_rates;
4095 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
4096 wlvif->band);
4097 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
4098 wlvif->basic_rate_set);
4100 ret = wl1271_init_ap_rates(wl, wlvif);
4101 if (ret < 0) {
4102 wl1271_error("AP rate policy change failed %d", ret);
4103 goto out;
4106 ret = wl1271_ap_init_templates(wl, vif);
4107 if (ret < 0)
4108 goto out;
4110 ret = wl1271_ap_set_probe_resp_tmpl(wl, wlvif->basic_rate, vif);
4111 if (ret < 0)
4112 goto out;
4114 ret = wlcore_set_beacon_template(wl, vif, true);
4115 if (ret < 0)
4116 goto out;
4119 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
4120 if (ret < 0)
4121 goto out;
4123 if (changed & BSS_CHANGED_BEACON_ENABLED) {
4124 if (bss_conf->enable_beacon) {
4125 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
4126 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
4127 if (ret < 0)
4128 goto out;
4130 ret = wl1271_ap_init_hwenc(wl, wlvif);
4131 if (ret < 0)
4132 goto out;
4134 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
4135 wl1271_debug(DEBUG_AP, "started AP");
4137 } else {
4138 if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
4140 * AP might be in ROC in case we have just
4141 * sent auth reply. handle it.
4143 if (test_bit(wlvif->role_id, wl->roc_map))
4144 wl12xx_croc(wl, wlvif->role_id);
4146 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
4147 if (ret < 0)
4148 goto out;
4150 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
4151 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
4152 &wlvif->flags);
4153 wl1271_debug(DEBUG_AP, "stopped AP");
4158 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
4159 if (ret < 0)
4160 goto out;
4162 /* Handle HT information change */
4163 if ((changed & BSS_CHANGED_HT) &&
4164 (bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)) {
4165 ret = wl1271_acx_set_ht_information(wl, wlvif,
4166 bss_conf->ht_operation_mode);
4167 if (ret < 0) {
4168 wl1271_warning("Set ht information failed %d", ret);
4169 goto out;
4173 out:
4174 return;
4177 static int wlcore_set_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif,
4178 struct ieee80211_bss_conf *bss_conf,
4179 u32 sta_rate_set)
4181 u32 rates;
4182 int ret;
4184 wl1271_debug(DEBUG_MAC80211,
4185 "changed_bssid: %pM, aid: %d, bcn_int: %d, brates: 0x%x sta_rate_set: 0x%x",
4186 bss_conf->bssid, bss_conf->aid,
4187 bss_conf->beacon_int,
4188 bss_conf->basic_rates, sta_rate_set);
4190 wlvif->beacon_int = bss_conf->beacon_int;
4191 rates = bss_conf->basic_rates;
4192 wlvif->basic_rate_set =
4193 wl1271_tx_enabled_rates_get(wl, rates,
4194 wlvif->band);
4195 wlvif->basic_rate =
4196 wl1271_tx_min_rate_get(wl,
4197 wlvif->basic_rate_set);
4199 if (sta_rate_set)
4200 wlvif->rate_set =
4201 wl1271_tx_enabled_rates_get(wl,
4202 sta_rate_set,
4203 wlvif->band);
4205 /* we only support sched_scan while not connected */
4206 if (wl->sched_vif == wlvif)
4207 wl->ops->sched_scan_stop(wl, wlvif);
4209 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4210 if (ret < 0)
4211 return ret;
4213 ret = wl12xx_cmd_build_null_data(wl, wlvif);
4214 if (ret < 0)
4215 return ret;
4217 ret = wl1271_build_qos_null_data(wl, wl12xx_wlvif_to_vif(wlvif));
4218 if (ret < 0)
4219 return ret;
4221 wlcore_set_ssid(wl, wlvif);
4223 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
4225 return 0;
4228 static int wlcore_clear_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
4230 int ret;
4232 /* revert back to minimum rates for the current band */
4233 wl1271_set_band_rate(wl, wlvif);
4234 wlvif->basic_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4236 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4237 if (ret < 0)
4238 return ret;
4240 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4241 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) {
4242 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
4243 if (ret < 0)
4244 return ret;
4247 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
4248 return 0;
4250 /* STA/IBSS mode changes */
4251 static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
4252 struct ieee80211_vif *vif,
4253 struct ieee80211_bss_conf *bss_conf,
4254 u32 changed)
4256 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4257 bool do_join = false;
4258 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
4259 bool ibss_joined = false;
4260 u32 sta_rate_set = 0;
4261 int ret;
4262 struct ieee80211_sta *sta;
4263 bool sta_exists = false;
4264 struct ieee80211_sta_ht_cap sta_ht_cap;
4266 if (is_ibss) {
4267 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
4268 changed);
4269 if (ret < 0)
4270 goto out;
4273 if (changed & BSS_CHANGED_IBSS) {
4274 if (bss_conf->ibss_joined) {
4275 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
4276 ibss_joined = true;
4277 } else {
4278 wlcore_unset_assoc(wl, wlvif);
4279 wl12xx_cmd_role_stop_sta(wl, wlvif);
4283 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
4284 do_join = true;
4286 /* Need to update the SSID (for filtering etc) */
4287 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
4288 do_join = true;
4290 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
4291 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
4292 bss_conf->enable_beacon ? "enabled" : "disabled");
4294 do_join = true;
4297 if (changed & BSS_CHANGED_IDLE && !is_ibss)
4298 wl1271_sta_handle_idle(wl, wlvif, bss_conf->idle);
4300 if (changed & BSS_CHANGED_CQM) {
4301 bool enable = false;
4302 if (bss_conf->cqm_rssi_thold)
4303 enable = true;
4304 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
4305 bss_conf->cqm_rssi_thold,
4306 bss_conf->cqm_rssi_hyst);
4307 if (ret < 0)
4308 goto out;
4309 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
4312 if (changed & (BSS_CHANGED_BSSID | BSS_CHANGED_HT |
4313 BSS_CHANGED_ASSOC)) {
4314 rcu_read_lock();
4315 sta = ieee80211_find_sta(vif, bss_conf->bssid);
4316 if (sta) {
4317 u8 *rx_mask = sta->ht_cap.mcs.rx_mask;
4319 /* save the supp_rates of the ap */
4320 sta_rate_set = sta->supp_rates[wlvif->band];
4321 if (sta->ht_cap.ht_supported)
4322 sta_rate_set |=
4323 (rx_mask[0] << HW_HT_RATES_OFFSET) |
4324 (rx_mask[1] << HW_MIMO_RATES_OFFSET);
4325 sta_ht_cap = sta->ht_cap;
4326 sta_exists = true;
4329 rcu_read_unlock();
4332 if (changed & BSS_CHANGED_BSSID) {
4333 if (!is_zero_ether_addr(bss_conf->bssid)) {
4334 ret = wlcore_set_bssid(wl, wlvif, bss_conf,
4335 sta_rate_set);
4336 if (ret < 0)
4337 goto out;
4339 /* Need to update the BSSID (for filtering etc) */
4340 do_join = true;
4341 } else {
4342 ret = wlcore_clear_bssid(wl, wlvif);
4343 if (ret < 0)
4344 goto out;
4348 if (changed & BSS_CHANGED_IBSS) {
4349 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
4350 bss_conf->ibss_joined);
4352 if (bss_conf->ibss_joined) {
4353 u32 rates = bss_conf->basic_rates;
4354 wlvif->basic_rate_set =
4355 wl1271_tx_enabled_rates_get(wl, rates,
4356 wlvif->band);
4357 wlvif->basic_rate =
4358 wl1271_tx_min_rate_get(wl,
4359 wlvif->basic_rate_set);
4361 /* by default, use 11b + OFDM rates */
4362 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
4363 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4364 if (ret < 0)
4365 goto out;
4369 if ((changed & BSS_CHANGED_BEACON_INFO) && bss_conf->dtim_period) {
4370 /* enable beacon filtering */
4371 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
4372 if (ret < 0)
4373 goto out;
4376 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
4377 if (ret < 0)
4378 goto out;
4380 if (do_join) {
4381 ret = wlcore_join(wl, wlvif);
4382 if (ret < 0) {
4383 wl1271_warning("cmd join failed %d", ret);
4384 goto out;
4388 if (changed & BSS_CHANGED_ASSOC) {
4389 if (bss_conf->assoc) {
4390 ret = wlcore_set_assoc(wl, wlvif, bss_conf,
4391 sta_rate_set);
4392 if (ret < 0)
4393 goto out;
4395 if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
4396 wl12xx_set_authorized(wl, wlvif);
4397 } else {
4398 wlcore_unset_assoc(wl, wlvif);
4402 if (changed & BSS_CHANGED_PS) {
4403 if ((bss_conf->ps) &&
4404 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
4405 !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4406 int ps_mode;
4407 char *ps_mode_str;
4409 if (wl->conf.conn.forced_ps) {
4410 ps_mode = STATION_POWER_SAVE_MODE;
4411 ps_mode_str = "forced";
4412 } else {
4413 ps_mode = STATION_AUTO_PS_MODE;
4414 ps_mode_str = "auto";
4417 wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
4419 ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
4420 if (ret < 0)
4421 wl1271_warning("enter %s ps failed %d",
4422 ps_mode_str, ret);
4423 } else if (!bss_conf->ps &&
4424 test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4425 wl1271_debug(DEBUG_PSM, "auto ps disabled");
4427 ret = wl1271_ps_set_mode(wl, wlvif,
4428 STATION_ACTIVE_MODE);
4429 if (ret < 0)
4430 wl1271_warning("exit auto ps failed %d", ret);
4434 /* Handle new association with HT. Do this after join. */
4435 if (sta_exists) {
4436 bool enabled =
4437 bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT;
4439 ret = wlcore_hw_set_peer_cap(wl,
4440 &sta_ht_cap,
4441 enabled,
4442 wlvif->rate_set,
4443 wlvif->sta.hlid);
4444 if (ret < 0) {
4445 wl1271_warning("Set ht cap failed %d", ret);
4446 goto out;
4450 if (enabled) {
4451 ret = wl1271_acx_set_ht_information(wl, wlvif,
4452 bss_conf->ht_operation_mode);
4453 if (ret < 0) {
4454 wl1271_warning("Set ht information failed %d",
4455 ret);
4456 goto out;
4461 /* Handle arp filtering. Done after join. */
4462 if ((changed & BSS_CHANGED_ARP_FILTER) ||
4463 (!is_ibss && (changed & BSS_CHANGED_QOS))) {
4464 __be32 addr = bss_conf->arp_addr_list[0];
4465 wlvif->sta.qos = bss_conf->qos;
4466 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
4468 if (bss_conf->arp_addr_cnt == 1 && bss_conf->assoc) {
4469 wlvif->ip_addr = addr;
4471 * The template should have been configured only upon
4472 * association. however, it seems that the correct ip
4473 * isn't being set (when sending), so we have to
4474 * reconfigure the template upon every ip change.
4476 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
4477 if (ret < 0) {
4478 wl1271_warning("build arp rsp failed: %d", ret);
4479 goto out;
4482 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
4483 (ACX_ARP_FILTER_ARP_FILTERING |
4484 ACX_ARP_FILTER_AUTO_ARP),
4485 addr);
4486 } else {
4487 wlvif->ip_addr = 0;
4488 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4491 if (ret < 0)
4492 goto out;
4495 out:
4496 return;
4499 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4500 struct ieee80211_vif *vif,
4501 struct ieee80211_bss_conf *bss_conf,
4502 u32 changed)
4504 struct wl1271 *wl = hw->priv;
4505 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4506 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
4507 int ret;
4509 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info role %d changed 0x%x",
4510 wlvif->role_id, (int)changed);
4513 * make sure to cancel pending disconnections if our association
4514 * state changed
4516 if (!is_ap && (changed & BSS_CHANGED_ASSOC))
4517 cancel_delayed_work_sync(&wlvif->connection_loss_work);
4519 if (is_ap && (changed & BSS_CHANGED_BEACON_ENABLED) &&
4520 !bss_conf->enable_beacon)
4521 wl1271_tx_flush(wl);
4523 mutex_lock(&wl->mutex);
4525 if (unlikely(wl->state != WLCORE_STATE_ON))
4526 goto out;
4528 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4529 goto out;
4531 ret = wl1271_ps_elp_wakeup(wl);
4532 if (ret < 0)
4533 goto out;
4535 if ((changed & BSS_CHANGED_TXPOWER) &&
4536 bss_conf->txpower != wlvif->power_level) {
4538 ret = wl1271_acx_tx_power(wl, wlvif, bss_conf->txpower);
4539 if (ret < 0)
4540 goto out;
4542 wlvif->power_level = bss_conf->txpower;
4545 if (is_ap)
4546 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4547 else
4548 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4550 wl1271_ps_elp_sleep(wl);
4552 out:
4553 mutex_unlock(&wl->mutex);
4556 static int wlcore_op_add_chanctx(struct ieee80211_hw *hw,
4557 struct ieee80211_chanctx_conf *ctx)
4559 wl1271_debug(DEBUG_MAC80211, "mac80211 add chanctx %d (type %d)",
4560 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4561 cfg80211_get_chandef_type(&ctx->def));
4562 return 0;
4565 static void wlcore_op_remove_chanctx(struct ieee80211_hw *hw,
4566 struct ieee80211_chanctx_conf *ctx)
4568 wl1271_debug(DEBUG_MAC80211, "mac80211 remove chanctx %d (type %d)",
4569 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4570 cfg80211_get_chandef_type(&ctx->def));
4573 static void wlcore_op_change_chanctx(struct ieee80211_hw *hw,
4574 struct ieee80211_chanctx_conf *ctx,
4575 u32 changed)
4577 wl1271_debug(DEBUG_MAC80211,
4578 "mac80211 change chanctx %d (type %d) changed 0x%x",
4579 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4580 cfg80211_get_chandef_type(&ctx->def), changed);
4583 static int wlcore_op_assign_vif_chanctx(struct ieee80211_hw *hw,
4584 struct ieee80211_vif *vif,
4585 struct ieee80211_chanctx_conf *ctx)
4587 struct wl1271 *wl = hw->priv;
4588 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4589 int channel = ieee80211_frequency_to_channel(
4590 ctx->def.chan->center_freq);
4592 wl1271_debug(DEBUG_MAC80211,
4593 "mac80211 assign chanctx (role %d) %d (type %d)",
4594 wlvif->role_id, channel, cfg80211_get_chandef_type(&ctx->def));
4596 mutex_lock(&wl->mutex);
4598 wlvif->band = ctx->def.chan->band;
4599 wlvif->channel = channel;
4600 wlvif->channel_type = cfg80211_get_chandef_type(&ctx->def);
4602 /* update default rates according to the band */
4603 wl1271_set_band_rate(wl, wlvif);
4605 mutex_unlock(&wl->mutex);
4607 return 0;
4610 static void wlcore_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
4611 struct ieee80211_vif *vif,
4612 struct ieee80211_chanctx_conf *ctx)
4614 struct wl1271 *wl = hw->priv;
4615 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4617 wl1271_debug(DEBUG_MAC80211,
4618 "mac80211 unassign chanctx (role %d) %d (type %d)",
4619 wlvif->role_id,
4620 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4621 cfg80211_get_chandef_type(&ctx->def));
4623 wl1271_tx_flush(wl);
4626 static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4627 struct ieee80211_vif *vif, u16 queue,
4628 const struct ieee80211_tx_queue_params *params)
4630 struct wl1271 *wl = hw->priv;
4631 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4632 u8 ps_scheme;
4633 int ret = 0;
4635 mutex_lock(&wl->mutex);
4637 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4639 if (params->uapsd)
4640 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4641 else
4642 ps_scheme = CONF_PS_SCHEME_LEGACY;
4644 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4645 goto out;
4647 ret = wl1271_ps_elp_wakeup(wl);
4648 if (ret < 0)
4649 goto out;
4652 * the txop is confed in units of 32us by the mac80211,
4653 * we need us
4655 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4656 params->cw_min, params->cw_max,
4657 params->aifs, params->txop << 5);
4658 if (ret < 0)
4659 goto out_sleep;
4661 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4662 CONF_CHANNEL_TYPE_EDCF,
4663 wl1271_tx_get_queue(queue),
4664 ps_scheme, CONF_ACK_POLICY_LEGACY,
4665 0, 0);
4667 out_sleep:
4668 wl1271_ps_elp_sleep(wl);
4670 out:
4671 mutex_unlock(&wl->mutex);
4673 return ret;
4676 static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4677 struct ieee80211_vif *vif)
4680 struct wl1271 *wl = hw->priv;
4681 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4682 u64 mactime = ULLONG_MAX;
4683 int ret;
4685 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4687 mutex_lock(&wl->mutex);
4689 if (unlikely(wl->state != WLCORE_STATE_ON))
4690 goto out;
4692 ret = wl1271_ps_elp_wakeup(wl);
4693 if (ret < 0)
4694 goto out;
4696 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
4697 if (ret < 0)
4698 goto out_sleep;
4700 out_sleep:
4701 wl1271_ps_elp_sleep(wl);
4703 out:
4704 mutex_unlock(&wl->mutex);
4705 return mactime;
4708 static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4709 struct survey_info *survey)
4711 struct ieee80211_conf *conf = &hw->conf;
4713 if (idx != 0)
4714 return -ENOENT;
4716 survey->channel = conf->chandef.chan;
4717 survey->filled = 0;
4718 return 0;
4721 static int wl1271_allocate_sta(struct wl1271 *wl,
4722 struct wl12xx_vif *wlvif,
4723 struct ieee80211_sta *sta)
4725 struct wl1271_station *wl_sta;
4726 int ret;
4729 if (wl->active_sta_count >= wl->max_ap_stations) {
4730 wl1271_warning("could not allocate HLID - too much stations");
4731 return -EBUSY;
4734 wl_sta = (struct wl1271_station *)sta->drv_priv;
4735 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4736 if (ret < 0) {
4737 wl1271_warning("could not allocate HLID - too many links");
4738 return -EBUSY;
4741 /* use the previous security seq, if this is a recovery/resume */
4742 wl->links[wl_sta->hlid].total_freed_pkts = wl_sta->total_freed_pkts;
4744 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
4745 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
4746 wl->active_sta_count++;
4747 return 0;
4750 void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
4752 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
4753 return;
4755 clear_bit(hlid, wlvif->ap.sta_hlid_map);
4756 __clear_bit(hlid, &wl->ap_ps_map);
4757 __clear_bit(hlid, &wl->ap_fw_ps_map);
4760 * save the last used PN in the private part of iee80211_sta,
4761 * in case of recovery/suspend
4763 wlcore_save_freed_pkts_addr(wl, wlvif, hlid, wl->links[hlid].addr);
4765 wl12xx_free_link(wl, wlvif, &hlid);
4766 wl->active_sta_count--;
4769 * rearm the tx watchdog when the last STA is freed - give the FW a
4770 * chance to return STA-buffered packets before complaining.
4772 if (wl->active_sta_count == 0)
4773 wl12xx_rearm_tx_watchdog_locked(wl);
4776 static int wl12xx_sta_add(struct wl1271 *wl,
4777 struct wl12xx_vif *wlvif,
4778 struct ieee80211_sta *sta)
4780 struct wl1271_station *wl_sta;
4781 int ret = 0;
4782 u8 hlid;
4784 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4786 ret = wl1271_allocate_sta(wl, wlvif, sta);
4787 if (ret < 0)
4788 return ret;
4790 wl_sta = (struct wl1271_station *)sta->drv_priv;
4791 hlid = wl_sta->hlid;
4793 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
4794 if (ret < 0)
4795 wl1271_free_sta(wl, wlvif, hlid);
4797 return ret;
4800 static int wl12xx_sta_remove(struct wl1271 *wl,
4801 struct wl12xx_vif *wlvif,
4802 struct ieee80211_sta *sta)
4804 struct wl1271_station *wl_sta;
4805 int ret = 0, id;
4807 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4809 wl_sta = (struct wl1271_station *)sta->drv_priv;
4810 id = wl_sta->hlid;
4811 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
4812 return -EINVAL;
4814 ret = wl12xx_cmd_remove_peer(wl, wlvif, wl_sta->hlid);
4815 if (ret < 0)
4816 return ret;
4818 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
4819 return ret;
4822 static void wlcore_roc_if_possible(struct wl1271 *wl,
4823 struct wl12xx_vif *wlvif)
4825 if (find_first_bit(wl->roc_map,
4826 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)
4827 return;
4829 if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID))
4830 return;
4832 wl12xx_roc(wl, wlvif, wlvif->role_id, wlvif->band, wlvif->channel);
4836 * when wl_sta is NULL, we treat this call as if coming from a
4837 * pending auth reply.
4838 * wl->mutex must be taken and the FW must be awake when the call
4839 * takes place.
4841 void wlcore_update_inconn_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif,
4842 struct wl1271_station *wl_sta, bool in_conn)
4844 if (in_conn) {
4845 if (WARN_ON(wl_sta && wl_sta->in_connection))
4846 return;
4848 if (!wlvif->ap_pending_auth_reply &&
4849 !wlvif->inconn_count)
4850 wlcore_roc_if_possible(wl, wlvif);
4852 if (wl_sta) {
4853 wl_sta->in_connection = true;
4854 wlvif->inconn_count++;
4855 } else {
4856 wlvif->ap_pending_auth_reply = true;
4858 } else {
4859 if (wl_sta && !wl_sta->in_connection)
4860 return;
4862 if (WARN_ON(!wl_sta && !wlvif->ap_pending_auth_reply))
4863 return;
4865 if (WARN_ON(wl_sta && !wlvif->inconn_count))
4866 return;
4868 if (wl_sta) {
4869 wl_sta->in_connection = false;
4870 wlvif->inconn_count--;
4871 } else {
4872 wlvif->ap_pending_auth_reply = false;
4875 if (!wlvif->inconn_count && !wlvif->ap_pending_auth_reply &&
4876 test_bit(wlvif->role_id, wl->roc_map))
4877 wl12xx_croc(wl, wlvif->role_id);
4881 static int wl12xx_update_sta_state(struct wl1271 *wl,
4882 struct wl12xx_vif *wlvif,
4883 struct ieee80211_sta *sta,
4884 enum ieee80211_sta_state old_state,
4885 enum ieee80211_sta_state new_state)
4887 struct wl1271_station *wl_sta;
4888 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
4889 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
4890 int ret;
4892 wl_sta = (struct wl1271_station *)sta->drv_priv;
4894 /* Add station (AP mode) */
4895 if (is_ap &&
4896 old_state == IEEE80211_STA_NOTEXIST &&
4897 new_state == IEEE80211_STA_NONE) {
4898 ret = wl12xx_sta_add(wl, wlvif, sta);
4899 if (ret)
4900 return ret;
4902 wlcore_update_inconn_sta(wl, wlvif, wl_sta, true);
4905 /* Remove station (AP mode) */
4906 if (is_ap &&
4907 old_state == IEEE80211_STA_NONE &&
4908 new_state == IEEE80211_STA_NOTEXIST) {
4909 /* must not fail */
4910 wl12xx_sta_remove(wl, wlvif, sta);
4912 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
4915 /* Authorize station (AP mode) */
4916 if (is_ap &&
4917 new_state == IEEE80211_STA_AUTHORIZED) {
4918 ret = wl12xx_cmd_set_peer_state(wl, wlvif, wl_sta->hlid);
4919 if (ret < 0)
4920 return ret;
4922 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
4923 wl_sta->hlid);
4924 if (ret)
4925 return ret;
4927 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
4930 /* Authorize station */
4931 if (is_sta &&
4932 new_state == IEEE80211_STA_AUTHORIZED) {
4933 set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
4934 ret = wl12xx_set_authorized(wl, wlvif);
4935 if (ret)
4936 return ret;
4939 if (is_sta &&
4940 old_state == IEEE80211_STA_AUTHORIZED &&
4941 new_state == IEEE80211_STA_ASSOC) {
4942 clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
4943 clear_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags);
4946 /* save seq number on disassoc (suspend) */
4947 if (is_sta &&
4948 old_state == IEEE80211_STA_ASSOC &&
4949 new_state == IEEE80211_STA_AUTH) {
4950 wlcore_save_freed_pkts(wl, wlvif, wlvif->sta.hlid, sta);
4951 wlvif->total_freed_pkts = 0;
4954 /* restore seq number on assoc (resume) */
4955 if (is_sta &&
4956 old_state == IEEE80211_STA_AUTH &&
4957 new_state == IEEE80211_STA_ASSOC) {
4958 wlvif->total_freed_pkts = wl_sta->total_freed_pkts;
4961 /* clear ROCs on failure or authorization */
4962 if (is_sta &&
4963 (new_state == IEEE80211_STA_AUTHORIZED ||
4964 new_state == IEEE80211_STA_NOTEXIST)) {
4965 if (test_bit(wlvif->role_id, wl->roc_map))
4966 wl12xx_croc(wl, wlvif->role_id);
4969 if (is_sta &&
4970 old_state == IEEE80211_STA_NOTEXIST &&
4971 new_state == IEEE80211_STA_NONE) {
4972 if (find_first_bit(wl->roc_map,
4973 WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES) {
4974 WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID);
4975 wl12xx_roc(wl, wlvif, wlvif->role_id,
4976 wlvif->band, wlvif->channel);
4979 return 0;
4982 static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
4983 struct ieee80211_vif *vif,
4984 struct ieee80211_sta *sta,
4985 enum ieee80211_sta_state old_state,
4986 enum ieee80211_sta_state new_state)
4988 struct wl1271 *wl = hw->priv;
4989 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4990 int ret;
4992 wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
4993 sta->aid, old_state, new_state);
4995 mutex_lock(&wl->mutex);
4997 if (unlikely(wl->state != WLCORE_STATE_ON)) {
4998 ret = -EBUSY;
4999 goto out;
5002 ret = wl1271_ps_elp_wakeup(wl);
5003 if (ret < 0)
5004 goto out;
5006 ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
5008 wl1271_ps_elp_sleep(wl);
5009 out:
5010 mutex_unlock(&wl->mutex);
5011 if (new_state < old_state)
5012 return 0;
5013 return ret;
5016 static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
5017 struct ieee80211_vif *vif,
5018 enum ieee80211_ampdu_mlme_action action,
5019 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5020 u8 buf_size)
5022 struct wl1271 *wl = hw->priv;
5023 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5024 int ret;
5025 u8 hlid, *ba_bitmap;
5027 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
5028 tid);
5030 /* sanity check - the fields in FW are only 8bits wide */
5031 if (WARN_ON(tid > 0xFF))
5032 return -ENOTSUPP;
5034 mutex_lock(&wl->mutex);
5036 if (unlikely(wl->state != WLCORE_STATE_ON)) {
5037 ret = -EAGAIN;
5038 goto out;
5041 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
5042 hlid = wlvif->sta.hlid;
5043 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
5044 struct wl1271_station *wl_sta;
5046 wl_sta = (struct wl1271_station *)sta->drv_priv;
5047 hlid = wl_sta->hlid;
5048 } else {
5049 ret = -EINVAL;
5050 goto out;
5053 ba_bitmap = &wl->links[hlid].ba_bitmap;
5055 ret = wl1271_ps_elp_wakeup(wl);
5056 if (ret < 0)
5057 goto out;
5059 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
5060 tid, action);
5062 switch (action) {
5063 case IEEE80211_AMPDU_RX_START:
5064 if (!wlvif->ba_support || !wlvif->ba_allowed) {
5065 ret = -ENOTSUPP;
5066 break;
5069 if (wl->ba_rx_session_count >= wl->ba_rx_session_count_max) {
5070 ret = -EBUSY;
5071 wl1271_error("exceeded max RX BA sessions");
5072 break;
5075 if (*ba_bitmap & BIT(tid)) {
5076 ret = -EINVAL;
5077 wl1271_error("cannot enable RX BA session on active "
5078 "tid: %d", tid);
5079 break;
5082 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
5083 hlid);
5084 if (!ret) {
5085 *ba_bitmap |= BIT(tid);
5086 wl->ba_rx_session_count++;
5088 break;
5090 case IEEE80211_AMPDU_RX_STOP:
5091 if (!(*ba_bitmap & BIT(tid))) {
5093 * this happens on reconfig - so only output a debug
5094 * message for now, and don't fail the function.
5096 wl1271_debug(DEBUG_MAC80211,
5097 "no active RX BA session on tid: %d",
5098 tid);
5099 ret = 0;
5100 break;
5103 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
5104 hlid);
5105 if (!ret) {
5106 *ba_bitmap &= ~BIT(tid);
5107 wl->ba_rx_session_count--;
5109 break;
5112 * The BA initiator session management in FW independently.
5113 * Falling break here on purpose for all TX APDU commands.
5115 case IEEE80211_AMPDU_TX_START:
5116 case IEEE80211_AMPDU_TX_STOP_CONT:
5117 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5118 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5119 case IEEE80211_AMPDU_TX_OPERATIONAL:
5120 ret = -EINVAL;
5121 break;
5123 default:
5124 wl1271_error("Incorrect ampdu action id=%x\n", action);
5125 ret = -EINVAL;
5128 wl1271_ps_elp_sleep(wl);
5130 out:
5131 mutex_unlock(&wl->mutex);
5133 return ret;
5136 static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
5137 struct ieee80211_vif *vif,
5138 const struct cfg80211_bitrate_mask *mask)
5140 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5141 struct wl1271 *wl = hw->priv;
5142 int i, ret = 0;
5144 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
5145 mask->control[NL80211_BAND_2GHZ].legacy,
5146 mask->control[NL80211_BAND_5GHZ].legacy);
5148 mutex_lock(&wl->mutex);
5150 for (i = 0; i < WLCORE_NUM_BANDS; i++)
5151 wlvif->bitrate_masks[i] =
5152 wl1271_tx_enabled_rates_get(wl,
5153 mask->control[i].legacy,
5156 if (unlikely(wl->state != WLCORE_STATE_ON))
5157 goto out;
5159 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
5160 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
5162 ret = wl1271_ps_elp_wakeup(wl);
5163 if (ret < 0)
5164 goto out;
5166 wl1271_set_band_rate(wl, wlvif);
5167 wlvif->basic_rate =
5168 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
5169 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
5171 wl1271_ps_elp_sleep(wl);
5173 out:
5174 mutex_unlock(&wl->mutex);
5176 return ret;
5179 static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
5180 struct ieee80211_vif *vif,
5181 struct ieee80211_channel_switch *ch_switch)
5183 struct wl1271 *wl = hw->priv;
5184 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5185 int ret;
5187 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
5189 wl1271_tx_flush(wl);
5191 mutex_lock(&wl->mutex);
5193 if (unlikely(wl->state == WLCORE_STATE_OFF)) {
5194 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
5195 ieee80211_chswitch_done(vif, false);
5196 goto out;
5197 } else if (unlikely(wl->state != WLCORE_STATE_ON)) {
5198 goto out;
5201 ret = wl1271_ps_elp_wakeup(wl);
5202 if (ret < 0)
5203 goto out;
5205 /* TODO: change mac80211 to pass vif as param */
5207 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
5208 unsigned long delay_usec;
5210 ret = wl->ops->channel_switch(wl, wlvif, ch_switch);
5211 if (ret)
5212 goto out_sleep;
5214 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
5216 /* indicate failure 5 seconds after channel switch time */
5217 delay_usec = ieee80211_tu_to_usec(wlvif->beacon_int) *
5218 ch_switch->count;
5219 ieee80211_queue_delayed_work(hw, &wlvif->channel_switch_work,
5220 usecs_to_jiffies(delay_usec) +
5221 msecs_to_jiffies(5000));
5224 out_sleep:
5225 wl1271_ps_elp_sleep(wl);
5227 out:
5228 mutex_unlock(&wl->mutex);
5231 static void wlcore_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5232 u32 queues, bool drop)
5234 struct wl1271 *wl = hw->priv;
5236 wl1271_tx_flush(wl);
5239 static int wlcore_op_remain_on_channel(struct ieee80211_hw *hw,
5240 struct ieee80211_vif *vif,
5241 struct ieee80211_channel *chan,
5242 int duration,
5243 enum ieee80211_roc_type type)
5245 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5246 struct wl1271 *wl = hw->priv;
5247 int channel, ret = 0;
5249 channel = ieee80211_frequency_to_channel(chan->center_freq);
5251 wl1271_debug(DEBUG_MAC80211, "mac80211 roc %d (%d)",
5252 channel, wlvif->role_id);
5254 mutex_lock(&wl->mutex);
5256 if (unlikely(wl->state != WLCORE_STATE_ON))
5257 goto out;
5259 /* return EBUSY if we can't ROC right now */
5260 if (WARN_ON(wl->roc_vif ||
5261 find_first_bit(wl->roc_map,
5262 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)) {
5263 ret = -EBUSY;
5264 goto out;
5267 ret = wl1271_ps_elp_wakeup(wl);
5268 if (ret < 0)
5269 goto out;
5271 ret = wl12xx_start_dev(wl, wlvif, chan->band, channel);
5272 if (ret < 0)
5273 goto out_sleep;
5275 wl->roc_vif = vif;
5276 ieee80211_queue_delayed_work(hw, &wl->roc_complete_work,
5277 msecs_to_jiffies(duration));
5278 out_sleep:
5279 wl1271_ps_elp_sleep(wl);
5280 out:
5281 mutex_unlock(&wl->mutex);
5282 return ret;
5285 static int __wlcore_roc_completed(struct wl1271 *wl)
5287 struct wl12xx_vif *wlvif;
5288 int ret;
5290 /* already completed */
5291 if (unlikely(!wl->roc_vif))
5292 return 0;
5294 wlvif = wl12xx_vif_to_data(wl->roc_vif);
5296 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
5297 return -EBUSY;
5299 ret = wl12xx_stop_dev(wl, wlvif);
5300 if (ret < 0)
5301 return ret;
5303 wl->roc_vif = NULL;
5305 return 0;
5308 static int wlcore_roc_completed(struct wl1271 *wl)
5310 int ret;
5312 wl1271_debug(DEBUG_MAC80211, "roc complete");
5314 mutex_lock(&wl->mutex);
5316 if (unlikely(wl->state != WLCORE_STATE_ON)) {
5317 ret = -EBUSY;
5318 goto out;
5321 ret = wl1271_ps_elp_wakeup(wl);
5322 if (ret < 0)
5323 goto out;
5325 ret = __wlcore_roc_completed(wl);
5327 wl1271_ps_elp_sleep(wl);
5328 out:
5329 mutex_unlock(&wl->mutex);
5331 return ret;
5334 static void wlcore_roc_complete_work(struct work_struct *work)
5336 struct delayed_work *dwork;
5337 struct wl1271 *wl;
5338 int ret;
5340 dwork = container_of(work, struct delayed_work, work);
5341 wl = container_of(dwork, struct wl1271, roc_complete_work);
5343 ret = wlcore_roc_completed(wl);
5344 if (!ret)
5345 ieee80211_remain_on_channel_expired(wl->hw);
5348 static int wlcore_op_cancel_remain_on_channel(struct ieee80211_hw *hw)
5350 struct wl1271 *wl = hw->priv;
5352 wl1271_debug(DEBUG_MAC80211, "mac80211 croc");
5354 /* TODO: per-vif */
5355 wl1271_tx_flush(wl);
5358 * we can't just flush_work here, because it might deadlock
5359 * (as we might get called from the same workqueue)
5361 cancel_delayed_work_sync(&wl->roc_complete_work);
5362 wlcore_roc_completed(wl);
5364 return 0;
5367 static void wlcore_op_sta_rc_update(struct ieee80211_hw *hw,
5368 struct ieee80211_vif *vif,
5369 struct ieee80211_sta *sta,
5370 u32 changed)
5372 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5373 struct wl1271 *wl = hw->priv;
5375 wlcore_hw_sta_rc_update(wl, wlvif, sta, changed);
5378 static int wlcore_op_get_rssi(struct ieee80211_hw *hw,
5379 struct ieee80211_vif *vif,
5380 struct ieee80211_sta *sta,
5381 s8 *rssi_dbm)
5383 struct wl1271 *wl = hw->priv;
5384 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5385 int ret = 0;
5387 wl1271_debug(DEBUG_MAC80211, "mac80211 get_rssi");
5389 mutex_lock(&wl->mutex);
5391 if (unlikely(wl->state != WLCORE_STATE_ON))
5392 goto out;
5394 ret = wl1271_ps_elp_wakeup(wl);
5395 if (ret < 0)
5396 goto out_sleep;
5398 ret = wlcore_acx_average_rssi(wl, wlvif, rssi_dbm);
5399 if (ret < 0)
5400 goto out_sleep;
5402 out_sleep:
5403 wl1271_ps_elp_sleep(wl);
5405 out:
5406 mutex_unlock(&wl->mutex);
5408 return ret;
5411 static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
5413 struct wl1271 *wl = hw->priv;
5414 bool ret = false;
5416 mutex_lock(&wl->mutex);
5418 if (unlikely(wl->state != WLCORE_STATE_ON))
5419 goto out;
5421 /* packets are considered pending if in the TX queue or the FW */
5422 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
5423 out:
5424 mutex_unlock(&wl->mutex);
5426 return ret;
5429 /* can't be const, mac80211 writes to this */
5430 static struct ieee80211_rate wl1271_rates[] = {
5431 { .bitrate = 10,
5432 .hw_value = CONF_HW_BIT_RATE_1MBPS,
5433 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
5434 { .bitrate = 20,
5435 .hw_value = CONF_HW_BIT_RATE_2MBPS,
5436 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
5437 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5438 { .bitrate = 55,
5439 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
5440 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
5441 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5442 { .bitrate = 110,
5443 .hw_value = CONF_HW_BIT_RATE_11MBPS,
5444 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
5445 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5446 { .bitrate = 60,
5447 .hw_value = CONF_HW_BIT_RATE_6MBPS,
5448 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5449 { .bitrate = 90,
5450 .hw_value = CONF_HW_BIT_RATE_9MBPS,
5451 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5452 { .bitrate = 120,
5453 .hw_value = CONF_HW_BIT_RATE_12MBPS,
5454 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5455 { .bitrate = 180,
5456 .hw_value = CONF_HW_BIT_RATE_18MBPS,
5457 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5458 { .bitrate = 240,
5459 .hw_value = CONF_HW_BIT_RATE_24MBPS,
5460 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5461 { .bitrate = 360,
5462 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5463 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5464 { .bitrate = 480,
5465 .hw_value = CONF_HW_BIT_RATE_48MBPS,
5466 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5467 { .bitrate = 540,
5468 .hw_value = CONF_HW_BIT_RATE_54MBPS,
5469 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5472 /* can't be const, mac80211 writes to this */
5473 static struct ieee80211_channel wl1271_channels[] = {
5474 { .hw_value = 1, .center_freq = 2412, .max_power = WLCORE_MAX_TXPWR },
5475 { .hw_value = 2, .center_freq = 2417, .max_power = WLCORE_MAX_TXPWR },
5476 { .hw_value = 3, .center_freq = 2422, .max_power = WLCORE_MAX_TXPWR },
5477 { .hw_value = 4, .center_freq = 2427, .max_power = WLCORE_MAX_TXPWR },
5478 { .hw_value = 5, .center_freq = 2432, .max_power = WLCORE_MAX_TXPWR },
5479 { .hw_value = 6, .center_freq = 2437, .max_power = WLCORE_MAX_TXPWR },
5480 { .hw_value = 7, .center_freq = 2442, .max_power = WLCORE_MAX_TXPWR },
5481 { .hw_value = 8, .center_freq = 2447, .max_power = WLCORE_MAX_TXPWR },
5482 { .hw_value = 9, .center_freq = 2452, .max_power = WLCORE_MAX_TXPWR },
5483 { .hw_value = 10, .center_freq = 2457, .max_power = WLCORE_MAX_TXPWR },
5484 { .hw_value = 11, .center_freq = 2462, .max_power = WLCORE_MAX_TXPWR },
5485 { .hw_value = 12, .center_freq = 2467, .max_power = WLCORE_MAX_TXPWR },
5486 { .hw_value = 13, .center_freq = 2472, .max_power = WLCORE_MAX_TXPWR },
5487 { .hw_value = 14, .center_freq = 2484, .max_power = WLCORE_MAX_TXPWR },
5490 /* can't be const, mac80211 writes to this */
5491 static struct ieee80211_supported_band wl1271_band_2ghz = {
5492 .channels = wl1271_channels,
5493 .n_channels = ARRAY_SIZE(wl1271_channels),
5494 .bitrates = wl1271_rates,
5495 .n_bitrates = ARRAY_SIZE(wl1271_rates),
5498 /* 5 GHz data rates for WL1273 */
5499 static struct ieee80211_rate wl1271_rates_5ghz[] = {
5500 { .bitrate = 60,
5501 .hw_value = CONF_HW_BIT_RATE_6MBPS,
5502 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5503 { .bitrate = 90,
5504 .hw_value = CONF_HW_BIT_RATE_9MBPS,
5505 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5506 { .bitrate = 120,
5507 .hw_value = CONF_HW_BIT_RATE_12MBPS,
5508 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5509 { .bitrate = 180,
5510 .hw_value = CONF_HW_BIT_RATE_18MBPS,
5511 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5512 { .bitrate = 240,
5513 .hw_value = CONF_HW_BIT_RATE_24MBPS,
5514 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5515 { .bitrate = 360,
5516 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5517 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5518 { .bitrate = 480,
5519 .hw_value = CONF_HW_BIT_RATE_48MBPS,
5520 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5521 { .bitrate = 540,
5522 .hw_value = CONF_HW_BIT_RATE_54MBPS,
5523 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5526 /* 5 GHz band channels for WL1273 */
5527 static struct ieee80211_channel wl1271_channels_5ghz[] = {
5528 { .hw_value = 8, .center_freq = 5040, .max_power = WLCORE_MAX_TXPWR },
5529 { .hw_value = 12, .center_freq = 5060, .max_power = WLCORE_MAX_TXPWR },
5530 { .hw_value = 16, .center_freq = 5080, .max_power = WLCORE_MAX_TXPWR },
5531 { .hw_value = 34, .center_freq = 5170, .max_power = WLCORE_MAX_TXPWR },
5532 { .hw_value = 36, .center_freq = 5180, .max_power = WLCORE_MAX_TXPWR },
5533 { .hw_value = 38, .center_freq = 5190, .max_power = WLCORE_MAX_TXPWR },
5534 { .hw_value = 40, .center_freq = 5200, .max_power = WLCORE_MAX_TXPWR },
5535 { .hw_value = 42, .center_freq = 5210, .max_power = WLCORE_MAX_TXPWR },
5536 { .hw_value = 44, .center_freq = 5220, .max_power = WLCORE_MAX_TXPWR },
5537 { .hw_value = 46, .center_freq = 5230, .max_power = WLCORE_MAX_TXPWR },
5538 { .hw_value = 48, .center_freq = 5240, .max_power = WLCORE_MAX_TXPWR },
5539 { .hw_value = 52, .center_freq = 5260, .max_power = WLCORE_MAX_TXPWR },
5540 { .hw_value = 56, .center_freq = 5280, .max_power = WLCORE_MAX_TXPWR },
5541 { .hw_value = 60, .center_freq = 5300, .max_power = WLCORE_MAX_TXPWR },
5542 { .hw_value = 64, .center_freq = 5320, .max_power = WLCORE_MAX_TXPWR },
5543 { .hw_value = 100, .center_freq = 5500, .max_power = WLCORE_MAX_TXPWR },
5544 { .hw_value = 104, .center_freq = 5520, .max_power = WLCORE_MAX_TXPWR },
5545 { .hw_value = 108, .center_freq = 5540, .max_power = WLCORE_MAX_TXPWR },
5546 { .hw_value = 112, .center_freq = 5560, .max_power = WLCORE_MAX_TXPWR },
5547 { .hw_value = 116, .center_freq = 5580, .max_power = WLCORE_MAX_TXPWR },
5548 { .hw_value = 120, .center_freq = 5600, .max_power = WLCORE_MAX_TXPWR },
5549 { .hw_value = 124, .center_freq = 5620, .max_power = WLCORE_MAX_TXPWR },
5550 { .hw_value = 128, .center_freq = 5640, .max_power = WLCORE_MAX_TXPWR },
5551 { .hw_value = 132, .center_freq = 5660, .max_power = WLCORE_MAX_TXPWR },
5552 { .hw_value = 136, .center_freq = 5680, .max_power = WLCORE_MAX_TXPWR },
5553 { .hw_value = 140, .center_freq = 5700, .max_power = WLCORE_MAX_TXPWR },
5554 { .hw_value = 149, .center_freq = 5745, .max_power = WLCORE_MAX_TXPWR },
5555 { .hw_value = 153, .center_freq = 5765, .max_power = WLCORE_MAX_TXPWR },
5556 { .hw_value = 157, .center_freq = 5785, .max_power = WLCORE_MAX_TXPWR },
5557 { .hw_value = 161, .center_freq = 5805, .max_power = WLCORE_MAX_TXPWR },
5558 { .hw_value = 165, .center_freq = 5825, .max_power = WLCORE_MAX_TXPWR },
5561 static struct ieee80211_supported_band wl1271_band_5ghz = {
5562 .channels = wl1271_channels_5ghz,
5563 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
5564 .bitrates = wl1271_rates_5ghz,
5565 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
5568 static const struct ieee80211_ops wl1271_ops = {
5569 .start = wl1271_op_start,
5570 .stop = wlcore_op_stop,
5571 .add_interface = wl1271_op_add_interface,
5572 .remove_interface = wl1271_op_remove_interface,
5573 .change_interface = wl12xx_op_change_interface,
5574 #ifdef CONFIG_PM
5575 .suspend = wl1271_op_suspend,
5576 .resume = wl1271_op_resume,
5577 #endif
5578 .config = wl1271_op_config,
5579 .prepare_multicast = wl1271_op_prepare_multicast,
5580 .configure_filter = wl1271_op_configure_filter,
5581 .tx = wl1271_op_tx,
5582 .set_key = wlcore_op_set_key,
5583 .hw_scan = wl1271_op_hw_scan,
5584 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
5585 .sched_scan_start = wl1271_op_sched_scan_start,
5586 .sched_scan_stop = wl1271_op_sched_scan_stop,
5587 .bss_info_changed = wl1271_op_bss_info_changed,
5588 .set_frag_threshold = wl1271_op_set_frag_threshold,
5589 .set_rts_threshold = wl1271_op_set_rts_threshold,
5590 .conf_tx = wl1271_op_conf_tx,
5591 .get_tsf = wl1271_op_get_tsf,
5592 .get_survey = wl1271_op_get_survey,
5593 .sta_state = wl12xx_op_sta_state,
5594 .ampdu_action = wl1271_op_ampdu_action,
5595 .tx_frames_pending = wl1271_tx_frames_pending,
5596 .set_bitrate_mask = wl12xx_set_bitrate_mask,
5597 .set_default_unicast_key = wl1271_op_set_default_key_idx,
5598 .channel_switch = wl12xx_op_channel_switch,
5599 .flush = wlcore_op_flush,
5600 .remain_on_channel = wlcore_op_remain_on_channel,
5601 .cancel_remain_on_channel = wlcore_op_cancel_remain_on_channel,
5602 .add_chanctx = wlcore_op_add_chanctx,
5603 .remove_chanctx = wlcore_op_remove_chanctx,
5604 .change_chanctx = wlcore_op_change_chanctx,
5605 .assign_vif_chanctx = wlcore_op_assign_vif_chanctx,
5606 .unassign_vif_chanctx = wlcore_op_unassign_vif_chanctx,
5607 .sta_rc_update = wlcore_op_sta_rc_update,
5608 .get_rssi = wlcore_op_get_rssi,
5609 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
5613 u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum ieee80211_band band)
5615 u8 idx;
5617 BUG_ON(band >= 2);
5619 if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
5620 wl1271_error("Illegal RX rate from HW: %d", rate);
5621 return 0;
5624 idx = wl->band_rate_to_idx[band][rate];
5625 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
5626 wl1271_error("Unsupported RX rate from HW: %d", rate);
5627 return 0;
5630 return idx;
5633 static void wl12xx_derive_mac_addresses(struct wl1271 *wl, u32 oui, u32 nic)
5635 int i;
5637 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x",
5638 oui, nic);
5640 if (nic + WLCORE_NUM_MAC_ADDRESSES - wl->num_mac_addr > 0xffffff)
5641 wl1271_warning("NIC part of the MAC address wraps around!");
5643 for (i = 0; i < wl->num_mac_addr; i++) {
5644 wl->addresses[i].addr[0] = (u8)(oui >> 16);
5645 wl->addresses[i].addr[1] = (u8)(oui >> 8);
5646 wl->addresses[i].addr[2] = (u8) oui;
5647 wl->addresses[i].addr[3] = (u8)(nic >> 16);
5648 wl->addresses[i].addr[4] = (u8)(nic >> 8);
5649 wl->addresses[i].addr[5] = (u8) nic;
5650 nic++;
5653 /* we may be one address short at the most */
5654 WARN_ON(wl->num_mac_addr + 1 < WLCORE_NUM_MAC_ADDRESSES);
5657 * turn on the LAA bit in the first address and use it as
5658 * the last address.
5660 if (wl->num_mac_addr < WLCORE_NUM_MAC_ADDRESSES) {
5661 int idx = WLCORE_NUM_MAC_ADDRESSES - 1;
5662 memcpy(&wl->addresses[idx], &wl->addresses[0],
5663 sizeof(wl->addresses[0]));
5664 /* LAA bit */
5665 wl->addresses[idx].addr[0] |= BIT(1);
5668 wl->hw->wiphy->n_addresses = WLCORE_NUM_MAC_ADDRESSES;
5669 wl->hw->wiphy->addresses = wl->addresses;
5672 static int wl12xx_get_hw_info(struct wl1271 *wl)
5674 int ret;
5676 ret = wl12xx_set_power_on(wl);
5677 if (ret < 0)
5678 return ret;
5680 ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &wl->chip.id);
5681 if (ret < 0)
5682 goto out;
5684 wl->fuse_oui_addr = 0;
5685 wl->fuse_nic_addr = 0;
5687 ret = wl->ops->get_pg_ver(wl, &wl->hw_pg_ver);
5688 if (ret < 0)
5689 goto out;
5691 if (wl->ops->get_mac)
5692 ret = wl->ops->get_mac(wl);
5694 out:
5695 wl1271_power_off(wl);
5696 return ret;
5699 static int wl1271_register_hw(struct wl1271 *wl)
5701 int ret;
5702 u32 oui_addr = 0, nic_addr = 0;
5704 if (wl->mac80211_registered)
5705 return 0;
5707 if (wl->nvs_len >= 12) {
5708 /* NOTE: The wl->nvs->nvs element must be first, in
5709 * order to simplify the casting, we assume it is at
5710 * the beginning of the wl->nvs structure.
5712 u8 *nvs_ptr = (u8 *)wl->nvs;
5714 oui_addr =
5715 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
5716 nic_addr =
5717 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
5720 /* if the MAC address is zeroed in the NVS derive from fuse */
5721 if (oui_addr == 0 && nic_addr == 0) {
5722 oui_addr = wl->fuse_oui_addr;
5723 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
5724 nic_addr = wl->fuse_nic_addr + 1;
5727 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr);
5729 ret = ieee80211_register_hw(wl->hw);
5730 if (ret < 0) {
5731 wl1271_error("unable to register mac80211 hw: %d", ret);
5732 goto out;
5735 wl->mac80211_registered = true;
5737 wl1271_debugfs_init(wl);
5739 wl1271_notice("loaded");
5741 out:
5742 return ret;
5745 static void wl1271_unregister_hw(struct wl1271 *wl)
5747 if (wl->plt)
5748 wl1271_plt_stop(wl);
5750 ieee80211_unregister_hw(wl->hw);
5751 wl->mac80211_registered = false;
5755 static int wl1271_init_ieee80211(struct wl1271 *wl)
5757 int i;
5758 static const u32 cipher_suites[] = {
5759 WLAN_CIPHER_SUITE_WEP40,
5760 WLAN_CIPHER_SUITE_WEP104,
5761 WLAN_CIPHER_SUITE_TKIP,
5762 WLAN_CIPHER_SUITE_CCMP,
5763 WL1271_CIPHER_SUITE_GEM,
5766 /* The tx descriptor buffer */
5767 wl->hw->extra_tx_headroom = sizeof(struct wl1271_tx_hw_descr);
5769 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
5770 wl->hw->extra_tx_headroom += WL1271_EXTRA_SPACE_TKIP;
5772 /* unit us */
5773 /* FIXME: find a proper value */
5774 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
5776 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5777 IEEE80211_HW_SUPPORTS_PS |
5778 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5779 IEEE80211_HW_SUPPORTS_UAPSD |
5780 IEEE80211_HW_HAS_RATE_CONTROL |
5781 IEEE80211_HW_CONNECTION_MONITOR |
5782 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5783 IEEE80211_HW_SPECTRUM_MGMT |
5784 IEEE80211_HW_AP_LINK_PS |
5785 IEEE80211_HW_AMPDU_AGGREGATION |
5786 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
5787 IEEE80211_HW_QUEUE_CONTROL |
5788 IEEE80211_HW_CHANCTX_STA_CSA;
5790 wl->hw->wiphy->cipher_suites = cipher_suites;
5791 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5793 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
5794 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
5795 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
5796 wl->hw->wiphy->max_scan_ssids = 1;
5797 wl->hw->wiphy->max_sched_scan_ssids = 16;
5798 wl->hw->wiphy->max_match_sets = 16;
5800 * Maximum length of elements in scanning probe request templates
5801 * should be the maximum length possible for a template, without
5802 * the IEEE80211 header of the template
5804 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
5805 sizeof(struct ieee80211_header);
5807 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
5808 sizeof(struct ieee80211_header);
5810 wl->hw->wiphy->max_remain_on_channel_duration = 30000;
5812 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD |
5813 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
5814 WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
5816 /* make sure all our channels fit in the scanned_ch bitmask */
5817 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5818 ARRAY_SIZE(wl1271_channels_5ghz) >
5819 WL1271_MAX_CHANNELS);
5821 * clear channel flags from the previous usage
5822 * and restore max_power & max_antenna_gain values.
5824 for (i = 0; i < ARRAY_SIZE(wl1271_channels); i++) {
5825 wl1271_band_2ghz.channels[i].flags = 0;
5826 wl1271_band_2ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
5827 wl1271_band_2ghz.channels[i].max_antenna_gain = 0;
5830 for (i = 0; i < ARRAY_SIZE(wl1271_channels_5ghz); i++) {
5831 wl1271_band_5ghz.channels[i].flags = 0;
5832 wl1271_band_5ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
5833 wl1271_band_5ghz.channels[i].max_antenna_gain = 0;
5837 * We keep local copies of the band structs because we need to
5838 * modify them on a per-device basis.
5840 memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5841 sizeof(wl1271_band_2ghz));
5842 memcpy(&wl->bands[IEEE80211_BAND_2GHZ].ht_cap,
5843 &wl->ht_cap[IEEE80211_BAND_2GHZ],
5844 sizeof(*wl->ht_cap));
5845 memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5846 sizeof(wl1271_band_5ghz));
5847 memcpy(&wl->bands[IEEE80211_BAND_5GHZ].ht_cap,
5848 &wl->ht_cap[IEEE80211_BAND_5GHZ],
5849 sizeof(*wl->ht_cap));
5851 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5852 &wl->bands[IEEE80211_BAND_2GHZ];
5853 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5854 &wl->bands[IEEE80211_BAND_5GHZ];
5857 * allow 4 queues per mac address we support +
5858 * 1 cab queue per mac + one global offchannel Tx queue
5860 wl->hw->queues = (NUM_TX_QUEUES + 1) * WLCORE_NUM_MAC_ADDRESSES + 1;
5862 /* the last queue is the offchannel queue */
5863 wl->hw->offchannel_tx_hw_queue = wl->hw->queues - 1;
5864 wl->hw->max_rates = 1;
5866 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5868 /* the FW answers probe-requests in AP-mode */
5869 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5870 wl->hw->wiphy->probe_resp_offload =
5871 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5872 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5873 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5875 /* allowed interface combinations */
5876 wl->hw->wiphy->iface_combinations = wl->iface_combinations;
5877 wl->hw->wiphy->n_iface_combinations = wl->n_iface_combinations;
5879 /* register vendor commands */
5880 wlcore_set_vendor_commands(wl->hw->wiphy);
5882 SET_IEEE80211_DEV(wl->hw, wl->dev);
5884 wl->hw->sta_data_size = sizeof(struct wl1271_station);
5885 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
5887 wl->hw->max_rx_aggregation_subframes = wl->conf.ht.rx_ba_win_size;
5889 return 0;
5892 struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size, u32 aggr_buf_size,
5893 u32 mbox_size)
5895 struct ieee80211_hw *hw;
5896 struct wl1271 *wl;
5897 int i, j, ret;
5898 unsigned int order;
5900 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5901 if (!hw) {
5902 wl1271_error("could not alloc ieee80211_hw");
5903 ret = -ENOMEM;
5904 goto err_hw_alloc;
5907 wl = hw->priv;
5908 memset(wl, 0, sizeof(*wl));
5910 wl->priv = kzalloc(priv_size, GFP_KERNEL);
5911 if (!wl->priv) {
5912 wl1271_error("could not alloc wl priv");
5913 ret = -ENOMEM;
5914 goto err_priv_alloc;
5917 INIT_LIST_HEAD(&wl->wlvif_list);
5919 wl->hw = hw;
5922 * wl->num_links is not configured yet, so just use WLCORE_MAX_LINKS.
5923 * we don't allocate any additional resource here, so that's fine.
5925 for (i = 0; i < NUM_TX_QUEUES; i++)
5926 for (j = 0; j < WLCORE_MAX_LINKS; j++)
5927 skb_queue_head_init(&wl->links[j].tx_queue[i]);
5929 skb_queue_head_init(&wl->deferred_rx_queue);
5930 skb_queue_head_init(&wl->deferred_tx_queue);
5932 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
5933 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
5934 INIT_WORK(&wl->tx_work, wl1271_tx_work);
5935 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5936 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
5937 INIT_DELAYED_WORK(&wl->roc_complete_work, wlcore_roc_complete_work);
5938 INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
5940 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5941 if (!wl->freezable_wq) {
5942 ret = -ENOMEM;
5943 goto err_hw;
5946 wl->channel = 0;
5947 wl->rx_counter = 0;
5948 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
5949 wl->band = IEEE80211_BAND_2GHZ;
5950 wl->channel_type = NL80211_CHAN_NO_HT;
5951 wl->flags = 0;
5952 wl->sg_enabled = true;
5953 wl->sleep_auth = WL1271_PSM_ILLEGAL;
5954 wl->recovery_count = 0;
5955 wl->hw_pg_ver = -1;
5956 wl->ap_ps_map = 0;
5957 wl->ap_fw_ps_map = 0;
5958 wl->quirks = 0;
5959 wl->platform_quirks = 0;
5960 wl->system_hlid = WL12XX_SYSTEM_HLID;
5961 wl->active_sta_count = 0;
5962 wl->active_link_count = 0;
5963 wl->fwlog_size = 0;
5964 init_waitqueue_head(&wl->fwlog_waitq);
5966 /* The system link is always allocated */
5967 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5969 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
5970 for (i = 0; i < wl->num_tx_desc; i++)
5971 wl->tx_frames[i] = NULL;
5973 spin_lock_init(&wl->wl_lock);
5975 wl->state = WLCORE_STATE_OFF;
5976 wl->fw_type = WL12XX_FW_TYPE_NONE;
5977 mutex_init(&wl->mutex);
5978 mutex_init(&wl->flush_mutex);
5979 init_completion(&wl->nvs_loading_complete);
5981 order = get_order(aggr_buf_size);
5982 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5983 if (!wl->aggr_buf) {
5984 ret = -ENOMEM;
5985 goto err_wq;
5987 wl->aggr_buf_size = aggr_buf_size;
5989 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5990 if (!wl->dummy_packet) {
5991 ret = -ENOMEM;
5992 goto err_aggr;
5995 /* Allocate one page for the FW log */
5996 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5997 if (!wl->fwlog) {
5998 ret = -ENOMEM;
5999 goto err_dummy_packet;
6002 wl->mbox_size = mbox_size;
6003 wl->mbox = kmalloc(wl->mbox_size, GFP_KERNEL | GFP_DMA);
6004 if (!wl->mbox) {
6005 ret = -ENOMEM;
6006 goto err_fwlog;
6009 wl->buffer_32 = kmalloc(sizeof(*wl->buffer_32), GFP_KERNEL);
6010 if (!wl->buffer_32) {
6011 ret = -ENOMEM;
6012 goto err_mbox;
6015 return hw;
6017 err_mbox:
6018 kfree(wl->mbox);
6020 err_fwlog:
6021 free_page((unsigned long)wl->fwlog);
6023 err_dummy_packet:
6024 dev_kfree_skb(wl->dummy_packet);
6026 err_aggr:
6027 free_pages((unsigned long)wl->aggr_buf, order);
6029 err_wq:
6030 destroy_workqueue(wl->freezable_wq);
6032 err_hw:
6033 wl1271_debugfs_exit(wl);
6034 kfree(wl->priv);
6036 err_priv_alloc:
6037 ieee80211_free_hw(hw);
6039 err_hw_alloc:
6041 return ERR_PTR(ret);
6043 EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
6045 int wlcore_free_hw(struct wl1271 *wl)
6047 /* Unblock any fwlog readers */
6048 mutex_lock(&wl->mutex);
6049 wl->fwlog_size = -1;
6050 wake_up_interruptible_all(&wl->fwlog_waitq);
6051 mutex_unlock(&wl->mutex);
6053 wlcore_sysfs_free(wl);
6055 kfree(wl->buffer_32);
6056 kfree(wl->mbox);
6057 free_page((unsigned long)wl->fwlog);
6058 dev_kfree_skb(wl->dummy_packet);
6059 free_pages((unsigned long)wl->aggr_buf, get_order(wl->aggr_buf_size));
6061 wl1271_debugfs_exit(wl);
6063 vfree(wl->fw);
6064 wl->fw = NULL;
6065 wl->fw_type = WL12XX_FW_TYPE_NONE;
6066 kfree(wl->nvs);
6067 wl->nvs = NULL;
6069 kfree(wl->raw_fw_status);
6070 kfree(wl->fw_status);
6071 kfree(wl->tx_res_if);
6072 destroy_workqueue(wl->freezable_wq);
6074 kfree(wl->priv);
6075 ieee80211_free_hw(wl->hw);
6077 return 0;
6079 EXPORT_SYMBOL_GPL(wlcore_free_hw);
6081 #ifdef CONFIG_PM
6082 static const struct wiphy_wowlan_support wlcore_wowlan_support = {
6083 .flags = WIPHY_WOWLAN_ANY,
6084 .n_patterns = WL1271_MAX_RX_FILTERS,
6085 .pattern_min_len = 1,
6086 .pattern_max_len = WL1271_RX_FILTER_MAX_PATTERN_SIZE,
6088 #endif
6090 static irqreturn_t wlcore_hardirq(int irq, void *cookie)
6092 return IRQ_WAKE_THREAD;
6095 static void wlcore_nvs_cb(const struct firmware *fw, void *context)
6097 struct wl1271 *wl = context;
6098 struct platform_device *pdev = wl->pdev;
6099 struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
6100 struct wl12xx_platform_data *pdata = pdev_data->pdata;
6101 unsigned long irqflags;
6102 int ret;
6103 irq_handler_t hardirq_fn = NULL;
6105 if (fw) {
6106 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
6107 if (!wl->nvs) {
6108 wl1271_error("Could not allocate nvs data");
6109 goto out;
6111 wl->nvs_len = fw->size;
6112 } else {
6113 wl1271_debug(DEBUG_BOOT, "Could not get nvs file %s",
6114 WL12XX_NVS_NAME);
6115 wl->nvs = NULL;
6116 wl->nvs_len = 0;
6119 ret = wl->ops->setup(wl);
6120 if (ret < 0)
6121 goto out_free_nvs;
6123 BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
6125 /* adjust some runtime configuration parameters */
6126 wlcore_adjust_conf(wl);
6128 wl->irq = platform_get_irq(pdev, 0);
6129 wl->platform_quirks = pdata->platform_quirks;
6130 wl->if_ops = pdev_data->if_ops;
6132 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ) {
6133 irqflags = IRQF_TRIGGER_RISING;
6134 hardirq_fn = wlcore_hardirq;
6135 } else {
6136 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
6139 ret = request_threaded_irq(wl->irq, hardirq_fn, wlcore_irq,
6140 irqflags, pdev->name, wl);
6141 if (ret < 0) {
6142 wl1271_error("request_irq() failed: %d", ret);
6143 goto out_free_nvs;
6146 #ifdef CONFIG_PM
6147 ret = enable_irq_wake(wl->irq);
6148 if (!ret) {
6149 wl->irq_wake_enabled = true;
6150 device_init_wakeup(wl->dev, 1);
6151 if (pdata->pwr_in_suspend)
6152 wl->hw->wiphy->wowlan = &wlcore_wowlan_support;
6154 #endif
6155 disable_irq(wl->irq);
6157 ret = wl12xx_get_hw_info(wl);
6158 if (ret < 0) {
6159 wl1271_error("couldn't get hw info");
6160 goto out_irq;
6163 ret = wl->ops->identify_chip(wl);
6164 if (ret < 0)
6165 goto out_irq;
6167 ret = wl1271_init_ieee80211(wl);
6168 if (ret)
6169 goto out_irq;
6171 ret = wl1271_register_hw(wl);
6172 if (ret)
6173 goto out_irq;
6175 ret = wlcore_sysfs_init(wl);
6176 if (ret)
6177 goto out_unreg;
6179 wl->initialized = true;
6180 goto out;
6182 out_unreg:
6183 wl1271_unregister_hw(wl);
6185 out_irq:
6186 free_irq(wl->irq, wl);
6188 out_free_nvs:
6189 kfree(wl->nvs);
6191 out:
6192 release_firmware(fw);
6193 complete_all(&wl->nvs_loading_complete);
6196 int wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
6198 int ret;
6200 if (!wl->ops || !wl->ptable)
6201 return -EINVAL;
6203 wl->dev = &pdev->dev;
6204 wl->pdev = pdev;
6205 platform_set_drvdata(pdev, wl);
6207 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
6208 WL12XX_NVS_NAME, &pdev->dev, GFP_KERNEL,
6209 wl, wlcore_nvs_cb);
6210 if (ret < 0) {
6211 wl1271_error("request_firmware_nowait failed: %d", ret);
6212 complete_all(&wl->nvs_loading_complete);
6215 return ret;
6217 EXPORT_SYMBOL_GPL(wlcore_probe);
6219 int wlcore_remove(struct platform_device *pdev)
6221 struct wl1271 *wl = platform_get_drvdata(pdev);
6223 wait_for_completion(&wl->nvs_loading_complete);
6224 if (!wl->initialized)
6225 return 0;
6227 if (wl->irq_wake_enabled) {
6228 device_init_wakeup(wl->dev, 0);
6229 disable_irq_wake(wl->irq);
6231 wl1271_unregister_hw(wl);
6232 free_irq(wl->irq, wl);
6233 wlcore_free_hw(wl);
6235 return 0;
6237 EXPORT_SYMBOL_GPL(wlcore_remove);
6239 u32 wl12xx_debug_level = DEBUG_NONE;
6240 EXPORT_SYMBOL_GPL(wl12xx_debug_level);
6241 module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
6242 MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
6244 module_param_named(fwlog, fwlog_param, charp, 0);
6245 MODULE_PARM_DESC(fwlog,
6246 "FW logger options: continuous, ondemand, dbgpins or disable");
6248 module_param(fwlog_mem_blocks, int, S_IRUSR | S_IWUSR);
6249 MODULE_PARM_DESC(fwlog_mem_blocks, "fwlog mem_blocks");
6251 module_param(bug_on_recovery, int, S_IRUSR | S_IWUSR);
6252 MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
6254 module_param(no_recovery, int, S_IRUSR | S_IWUSR);
6255 MODULE_PARM_DESC(no_recovery, "Prevent HW recovery. FW will remain stuck.");
6257 MODULE_LICENSE("GPL");
6258 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
6259 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
6260 MODULE_FIRMWARE(WL12XX_NVS_NAME);