3 * This file is part of wl1271
5 * Copyright (C) 2008-2010 Nokia Corporation
7 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 #include <linux/module.h>
26 #include <linux/firmware.h>
27 #include <linux/delay.h>
28 #include <linux/spi/spi.h>
29 #include <linux/crc32.h>
30 #include <linux/etherdevice.h>
31 #include <linux/vmalloc.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <linux/wl12xx.h>
35 #include <linux/sched.h>
36 #include <linux/interrupt.h>
40 #include "wl12xx_80211.h"
54 #define WL1271_BOOT_RETRIES 3
56 #define WL1271_BOOT_RETRIES 3
58 static char *fwlog_param;
59 static bool bug_on_recovery;
60 static bool no_recovery;
62 static void __wl1271_op_remove_interface(struct wl1271 *wl,
63 struct ieee80211_vif *vif,
64 bool reset_tx_queues);
65 static void wl1271_op_stop(struct ieee80211_hw *hw);
66 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif);
68 static int wl12xx_set_authorized(struct wl1271 *wl,
69 struct wl12xx_vif *wlvif)
73 if (WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS))
76 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
79 if (test_and_set_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags))
82 ret = wl12xx_cmd_set_peer_state(wl, wlvif->sta.hlid);
86 wl12xx_croc(wl, wlvif->role_id);
88 wl1271_info("Association completed.");
92 static int wl1271_reg_notify(struct wiphy *wiphy,
93 struct regulatory_request *request)
95 struct ieee80211_supported_band *band;
96 struct ieee80211_channel *ch;
99 band = wiphy->bands[IEEE80211_BAND_5GHZ];
100 for (i = 0; i < band->n_channels; i++) {
101 ch = &band->channels[i];
102 if (ch->flags & IEEE80211_CHAN_DISABLED)
105 if (ch->flags & IEEE80211_CHAN_RADAR)
106 ch->flags |= IEEE80211_CHAN_NO_IBSS |
107 IEEE80211_CHAN_PASSIVE_SCAN;
114 static int wl1271_set_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif,
119 /* we should hold wl->mutex */
120 ret = wl1271_acx_ps_rx_streaming(wl, wlvif, enable);
125 set_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
127 clear_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
133 * this function is being called when the rx_streaming interval
134 * has beed changed or rx_streaming should be disabled
136 int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif)
139 int period = wl->conf.rx_streaming.interval;
141 /* don't reconfigure if rx_streaming is disabled */
142 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
145 /* reconfigure/disable according to new streaming_period */
147 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
148 (wl->conf.rx_streaming.always ||
149 test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
150 ret = wl1271_set_rx_streaming(wl, wlvif, true);
152 ret = wl1271_set_rx_streaming(wl, wlvif, false);
153 /* don't cancel_work_sync since we might deadlock */
154 del_timer_sync(&wlvif->rx_streaming_timer);
160 static void wl1271_rx_streaming_enable_work(struct work_struct *work)
163 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
164 rx_streaming_enable_work);
165 struct wl1271 *wl = wlvif->wl;
167 mutex_lock(&wl->mutex);
169 if (test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags) ||
170 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
171 (!wl->conf.rx_streaming.always &&
172 !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
175 if (!wl->conf.rx_streaming.interval)
178 ret = wl1271_ps_elp_wakeup(wl);
182 ret = wl1271_set_rx_streaming(wl, wlvif, true);
186 /* stop it after some time of inactivity */
187 mod_timer(&wlvif->rx_streaming_timer,
188 jiffies + msecs_to_jiffies(wl->conf.rx_streaming.duration));
191 wl1271_ps_elp_sleep(wl);
193 mutex_unlock(&wl->mutex);
196 static void wl1271_rx_streaming_disable_work(struct work_struct *work)
199 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
200 rx_streaming_disable_work);
201 struct wl1271 *wl = wlvif->wl;
203 mutex_lock(&wl->mutex);
205 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
208 ret = wl1271_ps_elp_wakeup(wl);
212 ret = wl1271_set_rx_streaming(wl, wlvif, false);
217 wl1271_ps_elp_sleep(wl);
219 mutex_unlock(&wl->mutex);
222 static void wl1271_rx_streaming_timer(unsigned long data)
224 struct wl12xx_vif *wlvif = (struct wl12xx_vif *)data;
225 struct wl1271 *wl = wlvif->wl;
226 ieee80211_queue_work(wl->hw, &wlvif->rx_streaming_disable_work);
229 /* wl->mutex must be taken */
230 void wl12xx_rearm_tx_watchdog_locked(struct wl1271 *wl)
232 /* if the watchdog is not armed, don't do anything */
233 if (wl->tx_allocated_blocks == 0)
236 cancel_delayed_work(&wl->tx_watchdog_work);
237 ieee80211_queue_delayed_work(wl->hw, &wl->tx_watchdog_work,
238 msecs_to_jiffies(wl->conf.tx.tx_watchdog_timeout));
241 static void wl12xx_tx_watchdog_work(struct work_struct *work)
243 struct delayed_work *dwork;
246 dwork = container_of(work, struct delayed_work, work);
247 wl = container_of(dwork, struct wl1271, tx_watchdog_work);
249 mutex_lock(&wl->mutex);
251 if (unlikely(wl->state == WL1271_STATE_OFF))
254 /* Tx went out in the meantime - everything is ok */
255 if (unlikely(wl->tx_allocated_blocks == 0))
259 * if a ROC is in progress, we might not have any Tx for a long
260 * time (e.g. pending Tx on the non-ROC channels)
262 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
263 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to ROC",
264 wl->conf.tx.tx_watchdog_timeout);
265 wl12xx_rearm_tx_watchdog_locked(wl);
270 * if a scan is in progress, we might not have any Tx for a long
273 if (wl->scan.state != WL1271_SCAN_STATE_IDLE) {
274 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to scan",
275 wl->conf.tx.tx_watchdog_timeout);
276 wl12xx_rearm_tx_watchdog_locked(wl);
281 * AP might cache a frame for a long time for a sleeping station,
282 * so rearm the timer if there's an AP interface with stations. If
283 * Tx is genuinely stuck we will most hopefully discover it when all
284 * stations are removed due to inactivity.
286 if (wl->active_sta_count) {
287 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms. AP has "
289 wl->conf.tx.tx_watchdog_timeout,
290 wl->active_sta_count);
291 wl12xx_rearm_tx_watchdog_locked(wl);
295 wl1271_error("Tx stuck (in FW) for %d ms. Starting recovery",
296 wl->conf.tx.tx_watchdog_timeout);
297 wl12xx_queue_recovery_work(wl);
300 mutex_unlock(&wl->mutex);
303 static void wlcore_adjust_conf(struct wl1271 *wl)
305 /* Adjust settings according to optional module parameters */
307 if (!strcmp(fwlog_param, "continuous")) {
308 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
309 } else if (!strcmp(fwlog_param, "ondemand")) {
310 wl->conf.fwlog.mode = WL12XX_FWLOG_ON_DEMAND;
311 } else if (!strcmp(fwlog_param, "dbgpins")) {
312 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
313 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_DBG_PINS;
314 } else if (!strcmp(fwlog_param, "disable")) {
315 wl->conf.fwlog.mem_blocks = 0;
316 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_NONE;
318 wl1271_error("Unknown fwlog parameter %s", fwlog_param);
323 static void wl12xx_irq_ps_regulate_link(struct wl1271 *wl,
324 struct wl12xx_vif *wlvif,
327 bool fw_ps, single_sta;
329 fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
330 single_sta = (wl->active_sta_count == 1);
333 * Wake up from high level PS if the STA is asleep with too little
334 * packets in FW or if the STA is awake.
336 if (!fw_ps || tx_pkts < WL1271_PS_STA_MAX_PACKETS)
337 wl12xx_ps_link_end(wl, wlvif, hlid);
340 * Start high-level PS if the STA is asleep with enough blocks in FW.
341 * Make an exception if this is the only connected station. In this
342 * case FW-memory congestion is not a problem.
344 else if (!single_sta && fw_ps && tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
345 wl12xx_ps_link_start(wl, wlvif, hlid, true);
348 static void wl12xx_irq_update_links_status(struct wl1271 *wl,
349 struct wl12xx_vif *wlvif,
350 struct wl_fw_status_2 *status)
352 struct wl1271_link *lnk;
356 /* TODO: also use link_fast_bitmap here */
358 cur_fw_ps_map = le32_to_cpu(status->link_ps_bitmap);
359 if (wl->ap_fw_ps_map != cur_fw_ps_map) {
360 wl1271_debug(DEBUG_PSM,
361 "link ps prev 0x%x cur 0x%x changed 0x%x",
362 wl->ap_fw_ps_map, cur_fw_ps_map,
363 wl->ap_fw_ps_map ^ cur_fw_ps_map);
365 wl->ap_fw_ps_map = cur_fw_ps_map;
368 for_each_set_bit(hlid, wlvif->ap.sta_hlid_map, WL12XX_MAX_LINKS) {
369 lnk = &wl->links[hlid];
370 cnt = status->counters.tx_lnk_free_pkts[hlid] -
371 lnk->prev_freed_pkts;
373 lnk->prev_freed_pkts = status->counters.tx_lnk_free_pkts[hlid];
374 lnk->allocated_pkts -= cnt;
376 wl12xx_irq_ps_regulate_link(wl, wlvif, hlid,
377 lnk->allocated_pkts);
381 static void wl12xx_fw_status(struct wl1271 *wl,
382 struct wl_fw_status_1 *status_1,
383 struct wl_fw_status_2 *status_2)
385 struct wl12xx_vif *wlvif;
387 u32 old_tx_blk_count = wl->tx_blocks_available;
388 int avail, freed_blocks;
392 status_len = WLCORE_FW_STATUS_1_LEN(wl->num_rx_desc) +
393 sizeof(*status_2) + wl->fw_status_priv_len;
395 wlcore_raw_read_data(wl, REG_RAW_FW_STATUS_ADDR, status_1,
398 wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
399 "drv_rx_counter = %d, tx_results_counter = %d)",
401 status_1->fw_rx_counter,
402 status_1->drv_rx_counter,
403 status_1->tx_results_counter);
405 for (i = 0; i < NUM_TX_QUEUES; i++) {
406 /* prevent wrap-around in freed-packets counter */
407 wl->tx_allocated_pkts[i] -=
408 (status_2->counters.tx_released_pkts[i] -
409 wl->tx_pkts_freed[i]) & 0xff;
411 wl->tx_pkts_freed[i] = status_2->counters.tx_released_pkts[i];
414 /* prevent wrap-around in total blocks counter */
415 if (likely(wl->tx_blocks_freed <=
416 le32_to_cpu(status_2->total_released_blks)))
417 freed_blocks = le32_to_cpu(status_2->total_released_blks) -
420 freed_blocks = 0x100000000LL - wl->tx_blocks_freed +
421 le32_to_cpu(status_2->total_released_blks);
423 wl->tx_blocks_freed = le32_to_cpu(status_2->total_released_blks);
425 wl->tx_allocated_blocks -= freed_blocks;
428 * If the FW freed some blocks:
429 * If we still have allocated blocks - re-arm the timer, Tx is
430 * not stuck. Otherwise, cancel the timer (no Tx currently).
433 if (wl->tx_allocated_blocks)
434 wl12xx_rearm_tx_watchdog_locked(wl);
436 cancel_delayed_work(&wl->tx_watchdog_work);
439 avail = le32_to_cpu(status_2->tx_total) - wl->tx_allocated_blocks;
442 * The FW might change the total number of TX memblocks before
443 * we get a notification about blocks being released. Thus, the
444 * available blocks calculation might yield a temporary result
445 * which is lower than the actual available blocks. Keeping in
446 * mind that only blocks that were allocated can be moved from
447 * TX to RX, tx_blocks_available should never decrease here.
449 wl->tx_blocks_available = max((int)wl->tx_blocks_available,
452 /* if more blocks are available now, tx work can be scheduled */
453 if (wl->tx_blocks_available > old_tx_blk_count)
454 clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
456 /* for AP update num of allocated TX blocks per link and ps status */
457 wl12xx_for_each_wlvif_ap(wl, wlvif) {
458 wl12xx_irq_update_links_status(wl, wlvif, status_2);
461 /* update the host-chipset time offset */
463 wl->time_offset = (timespec_to_ns(&ts) >> 10) -
464 (s64)le32_to_cpu(status_2->fw_localtime);
467 static void wl1271_flush_deferred_work(struct wl1271 *wl)
471 /* Pass all received frames to the network stack */
472 while ((skb = skb_dequeue(&wl->deferred_rx_queue)))
473 ieee80211_rx_ni(wl->hw, skb);
475 /* Return sent skbs to the network stack */
476 while ((skb = skb_dequeue(&wl->deferred_tx_queue)))
477 ieee80211_tx_status_ni(wl->hw, skb);
480 static void wl1271_netstack_work(struct work_struct *work)
483 container_of(work, struct wl1271, netstack_work);
486 wl1271_flush_deferred_work(wl);
487 } while (skb_queue_len(&wl->deferred_rx_queue));
490 #define WL1271_IRQ_MAX_LOOPS 256
492 static irqreturn_t wl1271_irq(int irq, void *cookie)
496 int loopcount = WL1271_IRQ_MAX_LOOPS;
497 struct wl1271 *wl = (struct wl1271 *)cookie;
499 unsigned int defer_count;
502 /* TX might be handled here, avoid redundant work */
503 set_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
504 cancel_work_sync(&wl->tx_work);
507 * In case edge triggered interrupt must be used, we cannot iterate
508 * more than once without introducing race conditions with the hardirq.
510 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
513 mutex_lock(&wl->mutex);
515 wl1271_debug(DEBUG_IRQ, "IRQ work");
517 if (unlikely(wl->state == WL1271_STATE_OFF))
520 ret = wl1271_ps_elp_wakeup(wl);
524 while (!done && loopcount--) {
526 * In order to avoid a race with the hardirq, clear the flag
527 * before acknowledging the chip. Since the mutex is held,
528 * wl1271_ps_elp_wakeup cannot be called concurrently.
530 clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
531 smp_mb__after_clear_bit();
533 wl12xx_fw_status(wl, wl->fw_status_1, wl->fw_status_2);
535 wlcore_hw_tx_immediate_compl(wl);
537 intr = le32_to_cpu(wl->fw_status_1->intr);
538 intr &= WLCORE_ALL_INTR_MASK;
544 if (unlikely(intr & WL1271_ACX_INTR_WATCHDOG)) {
545 wl1271_error("HW watchdog interrupt received! starting recovery.");
546 wl->watchdog_recovery = true;
547 wl12xx_queue_recovery_work(wl);
549 /* restarting the chip. ignore any other interrupt. */
553 if (unlikely(intr & WL1271_ACX_SW_INTR_WATCHDOG)) {
554 wl1271_error("SW watchdog interrupt received! "
555 "starting recovery.");
556 wl->watchdog_recovery = true;
557 wl12xx_queue_recovery_work(wl);
559 /* restarting the chip. ignore any other interrupt. */
563 if (likely(intr & WL1271_ACX_INTR_DATA)) {
564 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
566 wl12xx_rx(wl, wl->fw_status_1);
568 /* Check if any tx blocks were freed */
569 spin_lock_irqsave(&wl->wl_lock, flags);
570 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
571 wl1271_tx_total_queue_count(wl) > 0) {
572 spin_unlock_irqrestore(&wl->wl_lock, flags);
574 * In order to avoid starvation of the TX path,
575 * call the work function directly.
577 wl1271_tx_work_locked(wl);
579 spin_unlock_irqrestore(&wl->wl_lock, flags);
582 /* check for tx results */
583 wlcore_hw_tx_delayed_compl(wl);
585 /* Make sure the deferred queues don't get too long */
586 defer_count = skb_queue_len(&wl->deferred_tx_queue) +
587 skb_queue_len(&wl->deferred_rx_queue);
588 if (defer_count > WL1271_DEFERRED_QUEUE_LIMIT)
589 wl1271_flush_deferred_work(wl);
592 if (intr & WL1271_ACX_INTR_EVENT_A) {
593 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
594 wl1271_event_handle(wl, 0);
597 if (intr & WL1271_ACX_INTR_EVENT_B) {
598 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
599 wl1271_event_handle(wl, 1);
602 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
603 wl1271_debug(DEBUG_IRQ,
604 "WL1271_ACX_INTR_INIT_COMPLETE");
606 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
607 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
610 wl1271_ps_elp_sleep(wl);
613 spin_lock_irqsave(&wl->wl_lock, flags);
614 /* In case TX was not handled here, queue TX work */
615 clear_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
616 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
617 wl1271_tx_total_queue_count(wl) > 0)
618 ieee80211_queue_work(wl->hw, &wl->tx_work);
619 spin_unlock_irqrestore(&wl->wl_lock, flags);
621 mutex_unlock(&wl->mutex);
626 struct vif_counter_data {
629 struct ieee80211_vif *cur_vif;
630 bool cur_vif_running;
633 static void wl12xx_vif_count_iter(void *data, u8 *mac,
634 struct ieee80211_vif *vif)
636 struct vif_counter_data *counter = data;
639 if (counter->cur_vif == vif)
640 counter->cur_vif_running = true;
643 /* caller must not hold wl->mutex, as it might deadlock */
644 static void wl12xx_get_vif_count(struct ieee80211_hw *hw,
645 struct ieee80211_vif *cur_vif,
646 struct vif_counter_data *data)
648 memset(data, 0, sizeof(*data));
649 data->cur_vif = cur_vif;
651 ieee80211_iterate_active_interfaces(hw,
652 wl12xx_vif_count_iter, data);
655 static int wl12xx_fetch_firmware(struct wl1271 *wl, bool plt)
657 const struct firmware *fw;
659 enum wl12xx_fw_type fw_type;
663 fw_type = WL12XX_FW_TYPE_PLT;
664 fw_name = wl->plt_fw_name;
667 * we can't call wl12xx_get_vif_count() here because
668 * wl->mutex is taken, so use the cached last_vif_count value
670 if (wl->last_vif_count > 1) {
671 fw_type = WL12XX_FW_TYPE_MULTI;
672 fw_name = wl->mr_fw_name;
674 fw_type = WL12XX_FW_TYPE_NORMAL;
675 fw_name = wl->sr_fw_name;
679 if (wl->fw_type == fw_type)
682 wl1271_debug(DEBUG_BOOT, "booting firmware %s", fw_name);
684 ret = request_firmware(&fw, fw_name, wl->dev);
687 wl1271_error("could not get firmware %s: %d", fw_name, ret);
692 wl1271_error("firmware size is not multiple of 32 bits: %zu",
699 wl->fw_type = WL12XX_FW_TYPE_NONE;
700 wl->fw_len = fw->size;
701 wl->fw = vmalloc(wl->fw_len);
704 wl1271_error("could not allocate memory for the firmware");
709 memcpy(wl->fw, fw->data, wl->fw_len);
711 wl->fw_type = fw_type;
713 release_firmware(fw);
718 static void wl1271_fetch_nvs(struct wl1271 *wl)
720 const struct firmware *fw;
723 ret = request_firmware(&fw, WL12XX_NVS_NAME, wl->dev);
726 wl1271_debug(DEBUG_BOOT, "could not get nvs file %s: %d",
727 WL12XX_NVS_NAME, ret);
731 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
734 wl1271_error("could not allocate memory for the nvs file");
738 wl->nvs_len = fw->size;
741 release_firmware(fw);
744 void wl12xx_queue_recovery_work(struct wl1271 *wl)
746 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
747 ieee80211_queue_work(wl->hw, &wl->recovery_work);
750 size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen)
754 /* The FW log is a length-value list, find where the log end */
755 while (len < maxlen) {
756 if (memblock[len] == 0)
758 if (len + memblock[len] + 1 > maxlen)
760 len += memblock[len] + 1;
763 /* Make sure we have enough room */
764 len = min(len, (size_t)(PAGE_SIZE - wl->fwlog_size));
766 /* Fill the FW log file, consumed by the sysfs fwlog entry */
767 memcpy(wl->fwlog + wl->fwlog_size, memblock, len);
768 wl->fwlog_size += len;
773 static void wl12xx_read_fwlog_panic(struct wl1271 *wl)
779 if ((wl->quirks & WLCORE_QUIRK_FWLOG_NOT_IMPLEMENTED) ||
780 (wl->conf.fwlog.mode != WL12XX_FWLOG_ON_DEMAND) ||
781 (wl->conf.fwlog.mem_blocks == 0))
784 wl1271_info("Reading FW panic log");
786 block = kmalloc(WL12XX_HW_BLOCK_SIZE, GFP_KERNEL);
791 * Make sure the chip is awake and the logger isn't active.
792 * Do not send a stop fwlog command if the fw is hanged.
794 if (!wl1271_ps_elp_wakeup(wl) && !wl->watchdog_recovery)
795 wl12xx_cmd_stop_fwlog(wl);
799 /* Read the first memory block address */
800 wl12xx_fw_status(wl, wl->fw_status_1, wl->fw_status_2);
801 first_addr = le32_to_cpu(wl->fw_status_2->log_start_addr);
805 /* Traverse the memory blocks linked list */
808 memset(block, 0, WL12XX_HW_BLOCK_SIZE);
809 wl1271_read_hwaddr(wl, addr, block, WL12XX_HW_BLOCK_SIZE,
813 * Memory blocks are linked to one another. The first 4 bytes
814 * of each memory block hold the hardware address of the next
815 * one. The last memory block points to the first one.
817 addr = le32_to_cpup((__le32 *)block);
818 if (!wl12xx_copy_fwlog(wl, block + sizeof(addr),
819 WL12XX_HW_BLOCK_SIZE - sizeof(addr)))
821 } while (addr && (addr != first_addr));
823 wake_up_interruptible(&wl->fwlog_waitq);
829 static void wl1271_recovery_work(struct work_struct *work)
832 container_of(work, struct wl1271, recovery_work);
833 struct wl12xx_vif *wlvif;
834 struct ieee80211_vif *vif;
836 mutex_lock(&wl->mutex);
838 if (wl->state != WL1271_STATE_ON || wl->plt)
841 /* Avoid a recursive recovery */
842 set_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
844 wl12xx_read_fwlog_panic(wl);
846 /* change partitions momentarily so we can read the FW pc */
847 wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
848 wl1271_info("Hardware recovery in progress. FW ver: %s pc: 0x%x "
851 wlcore_read_reg(wl, REG_PC_ON_RECOVERY),
852 wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR));
853 wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
855 BUG_ON(bug_on_recovery &&
856 !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags));
859 wl1271_info("No recovery (chosen on module load). Fw will remain stuck.");
860 clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
865 * Advance security sequence number to overcome potential progress
866 * in the firmware during recovery. This doens't hurt if the network is
869 wl12xx_for_each_wlvif(wl, wlvif) {
870 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
871 test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
872 wlvif->tx_security_seq +=
873 WL1271_TX_SQN_POST_RECOVERY_PADDING;
876 /* Prevent spurious TX during FW restart */
877 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_FW_RESTART);
879 if (wl->sched_scanning) {
880 ieee80211_sched_scan_stopped(wl->hw);
881 wl->sched_scanning = false;
884 /* reboot the chipset */
885 while (!list_empty(&wl->wlvif_list)) {
886 wlvif = list_first_entry(&wl->wlvif_list,
887 struct wl12xx_vif, list);
888 vif = wl12xx_wlvif_to_vif(wlvif);
889 __wl1271_op_remove_interface(wl, vif, false);
891 wl->watchdog_recovery = false;
892 mutex_unlock(&wl->mutex);
893 wl1271_op_stop(wl->hw);
895 clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
897 ieee80211_restart_hw(wl->hw);
900 * Its safe to enable TX now - the queues are stopped after a request
903 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_FW_RESTART);
906 wl->watchdog_recovery = false;
907 mutex_unlock(&wl->mutex);
910 static void wl1271_fw_wakeup(struct wl1271 *wl)
912 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
915 static int wl1271_setup(struct wl1271 *wl)
917 wl->fw_status_1 = kmalloc(WLCORE_FW_STATUS_1_LEN(wl->num_rx_desc) +
918 sizeof(*wl->fw_status_2) +
919 wl->fw_status_priv_len, GFP_KERNEL);
920 if (!wl->fw_status_1)
923 wl->fw_status_2 = (struct wl_fw_status_2 *)
924 (((u8 *) wl->fw_status_1) +
925 WLCORE_FW_STATUS_1_LEN(wl->num_rx_desc));
927 wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
928 if (!wl->tx_res_if) {
929 kfree(wl->fw_status_1);
936 static int wl12xx_set_power_on(struct wl1271 *wl)
940 msleep(WL1271_PRE_POWER_ON_SLEEP);
941 ret = wl1271_power_on(wl);
944 msleep(WL1271_POWER_ON_SLEEP);
948 wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
950 /* ELP module wake up */
951 wl1271_fw_wakeup(wl);
957 static int wl12xx_chip_wakeup(struct wl1271 *wl, bool plt)
961 ret = wl12xx_set_power_on(wl);
966 * For wl127x based devices we could use the default block
967 * size (512 bytes), but due to a bug in the sdio driver, we
968 * need to set it explicitly after the chip is powered on. To
969 * simplify the code and since the performance impact is
970 * negligible, we use the same block size for all different
973 * Check if the bus supports blocksize alignment and, if it
974 * doesn't, make sure we don't have the quirk.
976 if (!wl1271_set_block_size(wl))
977 wl->quirks &= ~WLCORE_QUIRK_TX_BLOCKSIZE_ALIGN;
979 /* TODO: make sure the lower driver has set things up correctly */
981 ret = wl1271_setup(wl);
985 ret = wl12xx_fetch_firmware(wl, plt);
993 int wl1271_plt_start(struct wl1271 *wl)
995 int retries = WL1271_BOOT_RETRIES;
996 struct wiphy *wiphy = wl->hw->wiphy;
999 mutex_lock(&wl->mutex);
1001 wl1271_notice("power up");
1003 if (wl->state != WL1271_STATE_OFF) {
1004 wl1271_error("cannot go into PLT state because not "
1005 "in off state: %d", wl->state);
1012 ret = wl12xx_chip_wakeup(wl, true);
1016 ret = wl->ops->plt_init(wl);
1021 wl->state = WL1271_STATE_ON;
1022 wl1271_notice("firmware booted in PLT mode (%s)",
1023 wl->chip.fw_ver_str);
1025 /* update hw/fw version info in wiphy struct */
1026 wiphy->hw_version = wl->chip.id;
1027 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1028 sizeof(wiphy->fw_version));
1033 wl1271_power_off(wl);
1036 wl1271_error("firmware boot in PLT mode failed despite %d retries",
1037 WL1271_BOOT_RETRIES);
1039 mutex_unlock(&wl->mutex);
1044 int wl1271_plt_stop(struct wl1271 *wl)
1048 wl1271_notice("power down");
1051 * Interrupts must be disabled before setting the state to OFF.
1052 * Otherwise, the interrupt handler might be called and exit without
1053 * reading the interrupt status.
1055 wlcore_disable_interrupts(wl);
1056 mutex_lock(&wl->mutex);
1058 mutex_unlock(&wl->mutex);
1061 * This will not necessarily enable interrupts as interrupts
1062 * may have been disabled when op_stop was called. It will,
1063 * however, balance the above call to disable_interrupts().
1065 wlcore_enable_interrupts(wl);
1067 wl1271_error("cannot power down because not in PLT "
1068 "state: %d", wl->state);
1073 mutex_unlock(&wl->mutex);
1075 wl1271_flush_deferred_work(wl);
1076 cancel_work_sync(&wl->netstack_work);
1077 cancel_work_sync(&wl->recovery_work);
1078 cancel_delayed_work_sync(&wl->elp_work);
1079 cancel_delayed_work_sync(&wl->tx_watchdog_work);
1080 cancel_delayed_work_sync(&wl->connection_loss_work);
1082 mutex_lock(&wl->mutex);
1083 wl1271_power_off(wl);
1085 wl->state = WL1271_STATE_OFF;
1088 mutex_unlock(&wl->mutex);
1094 static void wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1096 struct wl1271 *wl = hw->priv;
1097 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1098 struct ieee80211_vif *vif = info->control.vif;
1099 struct wl12xx_vif *wlvif = NULL;
1100 unsigned long flags;
1105 wlvif = wl12xx_vif_to_data(vif);
1107 mapping = skb_get_queue_mapping(skb);
1108 q = wl1271_tx_get_queue(mapping);
1110 hlid = wl12xx_tx_get_hlid(wl, wlvif, skb);
1112 spin_lock_irqsave(&wl->wl_lock, flags);
1115 * drop the packet if the link is invalid or the queue is stopped
1116 * for any reason but watermark. Watermark is a "soft"-stop so we
1117 * allow these packets through.
1119 if (hlid == WL12XX_INVALID_LINK_ID ||
1120 (wlvif && !test_bit(hlid, wlvif->links_map)) ||
1121 (wlcore_is_queue_stopped(wl, q) &&
1122 !wlcore_is_queue_stopped_by_reason(wl, q,
1123 WLCORE_QUEUE_STOP_REASON_WATERMARK))) {
1124 wl1271_debug(DEBUG_TX, "DROP skb hlid %d q %d", hlid, q);
1125 ieee80211_free_txskb(hw, skb);
1129 wl1271_debug(DEBUG_TX, "queue skb hlid %d q %d len %d",
1131 skb_queue_tail(&wl->links[hlid].tx_queue[q], skb);
1133 wl->tx_queue_count[q]++;
1136 * The workqueue is slow to process the tx_queue and we need stop
1137 * the queue here, otherwise the queue will get too long.
1139 if (wl->tx_queue_count[q] >= WL1271_TX_QUEUE_HIGH_WATERMARK) {
1140 wl1271_debug(DEBUG_TX, "op_tx: stopping queues for q %d", q);
1141 wlcore_stop_queue_locked(wl, q,
1142 WLCORE_QUEUE_STOP_REASON_WATERMARK);
1146 * The chip specific setup must run before the first TX packet -
1147 * before that, the tx_work will not be initialized!
1150 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
1151 !test_bit(WL1271_FLAG_TX_PENDING, &wl->flags))
1152 ieee80211_queue_work(wl->hw, &wl->tx_work);
1155 spin_unlock_irqrestore(&wl->wl_lock, flags);
1158 int wl1271_tx_dummy_packet(struct wl1271 *wl)
1160 unsigned long flags;
1163 /* no need to queue a new dummy packet if one is already pending */
1164 if (test_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags))
1167 q = wl1271_tx_get_queue(skb_get_queue_mapping(wl->dummy_packet));
1169 spin_lock_irqsave(&wl->wl_lock, flags);
1170 set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
1171 wl->tx_queue_count[q]++;
1172 spin_unlock_irqrestore(&wl->wl_lock, flags);
1174 /* The FW is low on RX memory blocks, so send the dummy packet asap */
1175 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags))
1176 wl1271_tx_work_locked(wl);
1179 * If the FW TX is busy, TX work will be scheduled by the threaded
1180 * interrupt handler function
1186 * The size of the dummy packet should be at least 1400 bytes. However, in
1187 * order to minimize the number of bus transactions, aligning it to 512 bytes
1188 * boundaries could be beneficial, performance wise
1190 #define TOTAL_TX_DUMMY_PACKET_SIZE (ALIGN(1400, 512))
1192 static struct sk_buff *wl12xx_alloc_dummy_packet(struct wl1271 *wl)
1194 struct sk_buff *skb;
1195 struct ieee80211_hdr_3addr *hdr;
1196 unsigned int dummy_packet_size;
1198 dummy_packet_size = TOTAL_TX_DUMMY_PACKET_SIZE -
1199 sizeof(struct wl1271_tx_hw_descr) - sizeof(*hdr);
1201 skb = dev_alloc_skb(TOTAL_TX_DUMMY_PACKET_SIZE);
1203 wl1271_warning("Failed to allocate a dummy packet skb");
1207 skb_reserve(skb, sizeof(struct wl1271_tx_hw_descr));
1209 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
1210 memset(hdr, 0, sizeof(*hdr));
1211 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1212 IEEE80211_STYPE_NULLFUNC |
1213 IEEE80211_FCTL_TODS);
1215 memset(skb_put(skb, dummy_packet_size), 0, dummy_packet_size);
1217 /* Dummy packets require the TID to be management */
1218 skb->priority = WL1271_TID_MGMT;
1220 /* Initialize all fields that might be used */
1221 skb_set_queue_mapping(skb, 0);
1222 memset(IEEE80211_SKB_CB(skb), 0, sizeof(struct ieee80211_tx_info));
1230 wl1271_validate_wowlan_pattern(struct cfg80211_wowlan_trig_pkt_pattern *p)
1232 int num_fields = 0, in_field = 0, fields_size = 0;
1233 int i, pattern_len = 0;
1236 wl1271_warning("No mask in WoWLAN pattern");
1241 * The pattern is broken up into segments of bytes at different offsets
1242 * that need to be checked by the FW filter. Each segment is called
1243 * a field in the FW API. We verify that the total number of fields
1244 * required for this pattern won't exceed FW limits (8)
1245 * as well as the total fields buffer won't exceed the FW limit.
1246 * Note that if there's a pattern which crosses Ethernet/IP header
1247 * boundary a new field is required.
1249 for (i = 0; i < p->pattern_len; i++) {
1250 if (test_bit(i, (unsigned long *)p->mask)) {
1255 if (i == WL1271_RX_FILTER_ETH_HEADER_SIZE) {
1257 fields_size += pattern_len +
1258 RX_FILTER_FIELD_OVERHEAD;
1266 fields_size += pattern_len +
1267 RX_FILTER_FIELD_OVERHEAD;
1274 fields_size += pattern_len + RX_FILTER_FIELD_OVERHEAD;
1278 if (num_fields > WL1271_RX_FILTER_MAX_FIELDS) {
1279 wl1271_warning("RX Filter too complex. Too many segments");
1283 if (fields_size > WL1271_RX_FILTER_MAX_FIELDS_SIZE) {
1284 wl1271_warning("RX filter pattern is too big");
1291 struct wl12xx_rx_filter *wl1271_rx_filter_alloc(void)
1293 return kzalloc(sizeof(struct wl12xx_rx_filter), GFP_KERNEL);
1296 void wl1271_rx_filter_free(struct wl12xx_rx_filter *filter)
1303 for (i = 0; i < filter->num_fields; i++)
1304 kfree(filter->fields[i].pattern);
1309 int wl1271_rx_filter_alloc_field(struct wl12xx_rx_filter *filter,
1310 u16 offset, u8 flags,
1311 u8 *pattern, u8 len)
1313 struct wl12xx_rx_filter_field *field;
1315 if (filter->num_fields == WL1271_RX_FILTER_MAX_FIELDS) {
1316 wl1271_warning("Max fields per RX filter. can't alloc another");
1320 field = &filter->fields[filter->num_fields];
1322 field->pattern = kzalloc(len, GFP_KERNEL);
1323 if (!field->pattern) {
1324 wl1271_warning("Failed to allocate RX filter pattern");
1328 filter->num_fields++;
1330 field->offset = cpu_to_le16(offset);
1331 field->flags = flags;
1333 memcpy(field->pattern, pattern, len);
1338 int wl1271_rx_filter_get_fields_size(struct wl12xx_rx_filter *filter)
1340 int i, fields_size = 0;
1342 for (i = 0; i < filter->num_fields; i++)
1343 fields_size += filter->fields[i].len +
1344 sizeof(struct wl12xx_rx_filter_field) -
1350 void wl1271_rx_filter_flatten_fields(struct wl12xx_rx_filter *filter,
1354 struct wl12xx_rx_filter_field *field;
1356 for (i = 0; i < filter->num_fields; i++) {
1357 field = (struct wl12xx_rx_filter_field *)buf;
1359 field->offset = filter->fields[i].offset;
1360 field->flags = filter->fields[i].flags;
1361 field->len = filter->fields[i].len;
1363 memcpy(&field->pattern, filter->fields[i].pattern, field->len);
1364 buf += sizeof(struct wl12xx_rx_filter_field) -
1365 sizeof(u8 *) + field->len;
1370 * Allocates an RX filter returned through f
1371 * which needs to be freed using rx_filter_free()
1373 static int wl1271_convert_wowlan_pattern_to_rx_filter(
1374 struct cfg80211_wowlan_trig_pkt_pattern *p,
1375 struct wl12xx_rx_filter **f)
1378 struct wl12xx_rx_filter *filter;
1382 filter = wl1271_rx_filter_alloc();
1384 wl1271_warning("Failed to alloc rx filter");
1390 while (i < p->pattern_len) {
1391 if (!test_bit(i, (unsigned long *)p->mask)) {
1396 for (j = i; j < p->pattern_len; j++) {
1397 if (!test_bit(j, (unsigned long *)p->mask))
1400 if (i < WL1271_RX_FILTER_ETH_HEADER_SIZE &&
1401 j >= WL1271_RX_FILTER_ETH_HEADER_SIZE)
1405 if (i < WL1271_RX_FILTER_ETH_HEADER_SIZE) {
1407 flags = WL1271_RX_FILTER_FLAG_ETHERNET_HEADER;
1409 offset = i - WL1271_RX_FILTER_ETH_HEADER_SIZE;
1410 flags = WL1271_RX_FILTER_FLAG_IP_HEADER;
1415 ret = wl1271_rx_filter_alloc_field(filter,
1418 &p->pattern[i], len);
1425 filter->action = FILTER_SIGNAL;
1431 wl1271_rx_filter_free(filter);
1437 static int wl1271_configure_wowlan(struct wl1271 *wl,
1438 struct cfg80211_wowlan *wow)
1442 if (!wow || wow->any || !wow->n_patterns) {
1443 wl1271_acx_default_rx_filter_enable(wl, 0, FILTER_SIGNAL);
1444 wl1271_rx_filter_clear_all(wl);
1448 if (WARN_ON(wow->n_patterns > WL1271_MAX_RX_FILTERS))
1451 /* Validate all incoming patterns before clearing current FW state */
1452 for (i = 0; i < wow->n_patterns; i++) {
1453 ret = wl1271_validate_wowlan_pattern(&wow->patterns[i]);
1455 wl1271_warning("Bad wowlan pattern %d", i);
1460 wl1271_acx_default_rx_filter_enable(wl, 0, FILTER_SIGNAL);
1461 wl1271_rx_filter_clear_all(wl);
1463 /* Translate WoWLAN patterns into filters */
1464 for (i = 0; i < wow->n_patterns; i++) {
1465 struct cfg80211_wowlan_trig_pkt_pattern *p;
1466 struct wl12xx_rx_filter *filter = NULL;
1468 p = &wow->patterns[i];
1470 ret = wl1271_convert_wowlan_pattern_to_rx_filter(p, &filter);
1472 wl1271_warning("Failed to create an RX filter from "
1473 "wowlan pattern %d", i);
1477 ret = wl1271_rx_filter_enable(wl, i, 1, filter);
1479 wl1271_rx_filter_free(filter);
1484 ret = wl1271_acx_default_rx_filter_enable(wl, 1, FILTER_DROP);
1490 static int wl1271_configure_suspend_sta(struct wl1271 *wl,
1491 struct wl12xx_vif *wlvif,
1492 struct cfg80211_wowlan *wow)
1496 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
1499 ret = wl1271_ps_elp_wakeup(wl);
1503 wl1271_configure_wowlan(wl, wow);
1504 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1505 wl->conf.conn.suspend_wake_up_event,
1506 wl->conf.conn.suspend_listen_interval);
1509 wl1271_error("suspend: set wake up conditions failed: %d", ret);
1511 wl1271_ps_elp_sleep(wl);
1518 static int wl1271_configure_suspend_ap(struct wl1271 *wl,
1519 struct wl12xx_vif *wlvif)
1523 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
1526 ret = wl1271_ps_elp_wakeup(wl);
1530 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
1532 wl1271_ps_elp_sleep(wl);
1538 static int wl1271_configure_suspend(struct wl1271 *wl,
1539 struct wl12xx_vif *wlvif,
1540 struct cfg80211_wowlan *wow)
1542 if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1543 return wl1271_configure_suspend_sta(wl, wlvif, wow);
1544 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
1545 return wl1271_configure_suspend_ap(wl, wlvif);
1549 static void wl1271_configure_resume(struct wl1271 *wl,
1550 struct wl12xx_vif *wlvif)
1553 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
1554 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
1556 if ((!is_ap) && (!is_sta))
1559 ret = wl1271_ps_elp_wakeup(wl);
1564 wl1271_configure_wowlan(wl, NULL);
1566 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1567 wl->conf.conn.wake_up_event,
1568 wl->conf.conn.listen_interval);
1571 wl1271_error("resume: wake up conditions failed: %d",
1575 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
1578 wl1271_ps_elp_sleep(wl);
1581 static int wl1271_op_suspend(struct ieee80211_hw *hw,
1582 struct cfg80211_wowlan *wow)
1584 struct wl1271 *wl = hw->priv;
1585 struct wl12xx_vif *wlvif;
1588 wl1271_debug(DEBUG_MAC80211, "mac80211 suspend wow=%d", !!wow);
1591 wl1271_tx_flush(wl);
1593 mutex_lock(&wl->mutex);
1594 wl->wow_enabled = true;
1595 wl12xx_for_each_wlvif(wl, wlvif) {
1596 ret = wl1271_configure_suspend(wl, wlvif, wow);
1598 mutex_unlock(&wl->mutex);
1599 wl1271_warning("couldn't prepare device to suspend");
1603 mutex_unlock(&wl->mutex);
1604 /* flush any remaining work */
1605 wl1271_debug(DEBUG_MAC80211, "flushing remaining works");
1608 * disable and re-enable interrupts in order to flush
1611 wlcore_disable_interrupts(wl);
1614 * set suspended flag to avoid triggering a new threaded_irq
1615 * work. no need for spinlock as interrupts are disabled.
1617 set_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1619 wlcore_enable_interrupts(wl);
1620 flush_work(&wl->tx_work);
1621 flush_delayed_work(&wl->elp_work);
1626 static int wl1271_op_resume(struct ieee80211_hw *hw)
1628 struct wl1271 *wl = hw->priv;
1629 struct wl12xx_vif *wlvif;
1630 unsigned long flags;
1631 bool run_irq_work = false;
1633 wl1271_debug(DEBUG_MAC80211, "mac80211 resume wow=%d",
1635 WARN_ON(!wl->wow_enabled);
1638 * re-enable irq_work enqueuing, and call irq_work directly if
1639 * there is a pending work.
1641 spin_lock_irqsave(&wl->wl_lock, flags);
1642 clear_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1643 if (test_and_clear_bit(WL1271_FLAG_PENDING_WORK, &wl->flags))
1644 run_irq_work = true;
1645 spin_unlock_irqrestore(&wl->wl_lock, flags);
1648 wl1271_debug(DEBUG_MAC80211,
1649 "run postponed irq_work directly");
1651 wlcore_enable_interrupts(wl);
1654 mutex_lock(&wl->mutex);
1655 wl12xx_for_each_wlvif(wl, wlvif) {
1656 wl1271_configure_resume(wl, wlvif);
1658 wl->wow_enabled = false;
1659 mutex_unlock(&wl->mutex);
1665 static int wl1271_op_start(struct ieee80211_hw *hw)
1667 wl1271_debug(DEBUG_MAC80211, "mac80211 start");
1670 * We have to delay the booting of the hardware because
1671 * we need to know the local MAC address before downloading and
1672 * initializing the firmware. The MAC address cannot be changed
1673 * after boot, and without the proper MAC address, the firmware
1674 * will not function properly.
1676 * The MAC address is first known when the corresponding interface
1677 * is added. That is where we will initialize the hardware.
1683 static void wl1271_op_stop(struct ieee80211_hw *hw)
1685 struct wl1271 *wl = hw->priv;
1688 wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
1691 * Interrupts must be disabled before setting the state to OFF.
1692 * Otherwise, the interrupt handler might be called and exit without
1693 * reading the interrupt status.
1695 wlcore_disable_interrupts(wl);
1696 mutex_lock(&wl->mutex);
1697 if (wl->state == WL1271_STATE_OFF) {
1698 mutex_unlock(&wl->mutex);
1701 * This will not necessarily enable interrupts as interrupts
1702 * may have been disabled when op_stop was called. It will,
1703 * however, balance the above call to disable_interrupts().
1705 wlcore_enable_interrupts(wl);
1710 * this must be before the cancel_work calls below, so that the work
1711 * functions don't perform further work.
1713 wl->state = WL1271_STATE_OFF;
1714 mutex_unlock(&wl->mutex);
1716 wl1271_flush_deferred_work(wl);
1717 cancel_delayed_work_sync(&wl->scan_complete_work);
1718 cancel_work_sync(&wl->netstack_work);
1719 cancel_work_sync(&wl->tx_work);
1720 cancel_delayed_work_sync(&wl->elp_work);
1721 cancel_delayed_work_sync(&wl->tx_watchdog_work);
1722 cancel_delayed_work_sync(&wl->connection_loss_work);
1724 /* let's notify MAC80211 about the remaining pending TX frames */
1725 wl12xx_tx_reset(wl);
1726 mutex_lock(&wl->mutex);
1728 wl1271_power_off(wl);
1730 wl->band = IEEE80211_BAND_2GHZ;
1733 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1734 wl->channel_type = NL80211_CHAN_NO_HT;
1735 wl->tx_blocks_available = 0;
1736 wl->tx_allocated_blocks = 0;
1737 wl->tx_results_count = 0;
1738 wl->tx_packets_count = 0;
1739 wl->time_offset = 0;
1740 wl->ap_fw_ps_map = 0;
1742 wl->sched_scanning = false;
1743 memset(wl->roles_map, 0, sizeof(wl->roles_map));
1744 memset(wl->links_map, 0, sizeof(wl->links_map));
1745 memset(wl->roc_map, 0, sizeof(wl->roc_map));
1746 wl->active_sta_count = 0;
1748 /* The system link is always allocated */
1749 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
1752 * this is performed after the cancel_work calls and the associated
1753 * mutex_lock, so that wl1271_op_add_interface does not accidentally
1754 * get executed before all these vars have been reset.
1758 wl->tx_blocks_freed = 0;
1760 for (i = 0; i < NUM_TX_QUEUES; i++) {
1761 wl->tx_pkts_freed[i] = 0;
1762 wl->tx_allocated_pkts[i] = 0;
1765 wl1271_debugfs_reset(wl);
1767 kfree(wl->fw_status_1);
1768 wl->fw_status_1 = NULL;
1769 wl->fw_status_2 = NULL;
1770 kfree(wl->tx_res_if);
1771 wl->tx_res_if = NULL;
1772 kfree(wl->target_mem_map);
1773 wl->target_mem_map = NULL;
1775 mutex_unlock(&wl->mutex);
1778 static int wl12xx_allocate_rate_policy(struct wl1271 *wl, u8 *idx)
1780 u8 policy = find_first_zero_bit(wl->rate_policies_map,
1781 WL12XX_MAX_RATE_POLICIES);
1782 if (policy >= WL12XX_MAX_RATE_POLICIES)
1785 __set_bit(policy, wl->rate_policies_map);
1790 static void wl12xx_free_rate_policy(struct wl1271 *wl, u8 *idx)
1792 if (WARN_ON(*idx >= WL12XX_MAX_RATE_POLICIES))
1795 __clear_bit(*idx, wl->rate_policies_map);
1796 *idx = WL12XX_MAX_RATE_POLICIES;
1799 static u8 wl12xx_get_role_type(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1801 switch (wlvif->bss_type) {
1802 case BSS_TYPE_AP_BSS:
1804 return WL1271_ROLE_P2P_GO;
1806 return WL1271_ROLE_AP;
1808 case BSS_TYPE_STA_BSS:
1810 return WL1271_ROLE_P2P_CL;
1812 return WL1271_ROLE_STA;
1815 return WL1271_ROLE_IBSS;
1818 wl1271_error("invalid bss_type: %d", wlvif->bss_type);
1820 return WL12XX_INVALID_ROLE_TYPE;
1823 static int wl12xx_init_vif_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1825 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1828 /* clear everything but the persistent data */
1829 memset(wlvif, 0, offsetof(struct wl12xx_vif, persistent));
1831 switch (ieee80211_vif_type_p2p(vif)) {
1832 case NL80211_IFTYPE_P2P_CLIENT:
1835 case NL80211_IFTYPE_STATION:
1836 wlvif->bss_type = BSS_TYPE_STA_BSS;
1838 case NL80211_IFTYPE_ADHOC:
1839 wlvif->bss_type = BSS_TYPE_IBSS;
1841 case NL80211_IFTYPE_P2P_GO:
1844 case NL80211_IFTYPE_AP:
1845 wlvif->bss_type = BSS_TYPE_AP_BSS;
1848 wlvif->bss_type = MAX_BSS_TYPE;
1852 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
1853 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
1854 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
1856 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
1857 wlvif->bss_type == BSS_TYPE_IBSS) {
1858 /* init sta/ibss data */
1859 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
1860 wl12xx_allocate_rate_policy(wl, &wlvif->sta.basic_rate_idx);
1861 wl12xx_allocate_rate_policy(wl, &wlvif->sta.ap_rate_idx);
1862 wl12xx_allocate_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
1863 wlvif->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
1864 wlvif->basic_rate = CONF_TX_RATE_MASK_BASIC;
1865 wlvif->rate_set = CONF_TX_RATE_MASK_BASIC;
1868 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
1869 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
1870 wl12xx_allocate_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
1871 wl12xx_allocate_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
1872 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
1873 wl12xx_allocate_rate_policy(wl,
1874 &wlvif->ap.ucast_rate_idx[i]);
1875 wlvif->basic_rate_set = CONF_TX_AP_ENABLED_RATES;
1877 * TODO: check if basic_rate shouldn't be
1878 * wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
1879 * instead (the same thing for STA above).
1881 wlvif->basic_rate = CONF_TX_AP_ENABLED_RATES;
1882 /* TODO: this seems to be used only for STA, check it */
1883 wlvif->rate_set = CONF_TX_AP_ENABLED_RATES;
1886 wlvif->bitrate_masks[IEEE80211_BAND_2GHZ] = wl->conf.tx.basic_rate;
1887 wlvif->bitrate_masks[IEEE80211_BAND_5GHZ] = wl->conf.tx.basic_rate_5;
1888 wlvif->beacon_int = WL1271_DEFAULT_BEACON_INT;
1891 * mac80211 configures some values globally, while we treat them
1892 * per-interface. thus, on init, we have to copy them from wl
1894 wlvif->band = wl->band;
1895 wlvif->channel = wl->channel;
1896 wlvif->power_level = wl->power_level;
1897 wlvif->channel_type = wl->channel_type;
1899 INIT_WORK(&wlvif->rx_streaming_enable_work,
1900 wl1271_rx_streaming_enable_work);
1901 INIT_WORK(&wlvif->rx_streaming_disable_work,
1902 wl1271_rx_streaming_disable_work);
1903 INIT_LIST_HEAD(&wlvif->list);
1905 setup_timer(&wlvif->rx_streaming_timer, wl1271_rx_streaming_timer,
1906 (unsigned long) wlvif);
1910 static bool wl12xx_init_fw(struct wl1271 *wl)
1912 int retries = WL1271_BOOT_RETRIES;
1913 bool booted = false;
1914 struct wiphy *wiphy = wl->hw->wiphy;
1919 ret = wl12xx_chip_wakeup(wl, false);
1923 ret = wl->ops->boot(wl);
1927 ret = wl1271_hw_init(wl);
1935 mutex_unlock(&wl->mutex);
1936 /* Unlocking the mutex in the middle of handling is
1937 inherently unsafe. In this case we deem it safe to do,
1938 because we need to let any possibly pending IRQ out of
1939 the system (and while we are WL1271_STATE_OFF the IRQ
1940 work function will not do anything.) Also, any other
1941 possible concurrent operations will fail due to the
1942 current state, hence the wl1271 struct should be safe. */
1943 wlcore_disable_interrupts(wl);
1944 wl1271_flush_deferred_work(wl);
1945 cancel_work_sync(&wl->netstack_work);
1946 mutex_lock(&wl->mutex);
1948 wl1271_power_off(wl);
1952 wl1271_error("firmware boot failed despite %d retries",
1953 WL1271_BOOT_RETRIES);
1957 wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
1959 /* update hw/fw version info in wiphy struct */
1960 wiphy->hw_version = wl->chip.id;
1961 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1962 sizeof(wiphy->fw_version));
1965 * Now we know if 11a is supported (info from the NVS), so disable
1966 * 11a channels if not supported
1968 if (!wl->enable_11a)
1969 wiphy->bands[IEEE80211_BAND_5GHZ]->n_channels = 0;
1971 wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
1972 wl->enable_11a ? "" : "not ");
1974 wl->state = WL1271_STATE_ON;
1979 static bool wl12xx_dev_role_started(struct wl12xx_vif *wlvif)
1981 return wlvif->dev_hlid != WL12XX_INVALID_LINK_ID;
1985 * Check whether a fw switch (i.e. moving from one loaded
1986 * fw to another) is needed. This function is also responsible
1987 * for updating wl->last_vif_count, so it must be called before
1988 * loading a non-plt fw (so the correct fw (single-role/multi-role)
1991 static bool wl12xx_need_fw_change(struct wl1271 *wl,
1992 struct vif_counter_data vif_counter_data,
1995 enum wl12xx_fw_type current_fw = wl->fw_type;
1996 u8 vif_count = vif_counter_data.counter;
1998 if (test_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags))
2001 /* increase the vif count if this is a new vif */
2002 if (add && !vif_counter_data.cur_vif_running)
2005 wl->last_vif_count = vif_count;
2007 /* no need for fw change if the device is OFF */
2008 if (wl->state == WL1271_STATE_OFF)
2011 if (vif_count > 1 && current_fw == WL12XX_FW_TYPE_NORMAL)
2013 if (vif_count <= 1 && current_fw == WL12XX_FW_TYPE_MULTI)
2020 * Enter "forced psm". Make sure the sta is in psm against the ap,
2021 * to make the fw switch a bit more disconnection-persistent.
2023 static void wl12xx_force_active_psm(struct wl1271 *wl)
2025 struct wl12xx_vif *wlvif;
2027 wl12xx_for_each_wlvif_sta(wl, wlvif) {
2028 wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
2032 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
2033 struct ieee80211_vif *vif)
2035 struct wl1271 *wl = hw->priv;
2036 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2037 struct vif_counter_data vif_count;
2040 bool booted = false;
2042 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
2043 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
2045 wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
2046 ieee80211_vif_type_p2p(vif), vif->addr);
2048 wl12xx_get_vif_count(hw, vif, &vif_count);
2050 mutex_lock(&wl->mutex);
2051 ret = wl1271_ps_elp_wakeup(wl);
2056 * in some very corner case HW recovery scenarios its possible to
2057 * get here before __wl1271_op_remove_interface is complete, so
2058 * opt out if that is the case.
2060 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) ||
2061 test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)) {
2067 ret = wl12xx_init_vif_data(wl, vif);
2072 role_type = wl12xx_get_role_type(wl, wlvif);
2073 if (role_type == WL12XX_INVALID_ROLE_TYPE) {
2078 if (wl12xx_need_fw_change(wl, vif_count, true)) {
2079 wl12xx_force_active_psm(wl);
2080 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2081 mutex_unlock(&wl->mutex);
2082 wl1271_recovery_work(&wl->recovery_work);
2087 * TODO: after the nvs issue will be solved, move this block
2088 * to start(), and make sure here the driver is ON.
2090 if (wl->state == WL1271_STATE_OFF) {
2092 * we still need this in order to configure the fw
2093 * while uploading the nvs
2095 memcpy(wl->addresses[0].addr, vif->addr, ETH_ALEN);
2097 booted = wl12xx_init_fw(wl);
2104 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2105 wlvif->bss_type == BSS_TYPE_IBSS) {
2107 * The device role is a special role used for
2108 * rx and tx frames prior to association (as
2109 * the STA role can get packets only from
2110 * its associated bssid)
2112 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2114 &wlvif->dev_role_id);
2119 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2120 role_type, &wlvif->role_id);
2124 ret = wl1271_init_vif_specific(wl, vif);
2128 list_add(&wlvif->list, &wl->wlvif_list);
2129 set_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags);
2131 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2136 wl1271_ps_elp_sleep(wl);
2138 mutex_unlock(&wl->mutex);
2143 static void __wl1271_op_remove_interface(struct wl1271 *wl,
2144 struct ieee80211_vif *vif,
2145 bool reset_tx_queues)
2147 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2150 wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
2152 if (!test_and_clear_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2155 /* because of hardware recovery, we may get here twice */
2156 if (wl->state != WL1271_STATE_ON)
2159 wl1271_info("down");
2161 if (wl->scan.state != WL1271_SCAN_STATE_IDLE &&
2162 wl->scan_vif == vif) {
2164 * Rearm the tx watchdog just before idling scan. This
2165 * prevents just-finished scans from triggering the watchdog
2167 wl12xx_rearm_tx_watchdog_locked(wl);
2169 wl->scan.state = WL1271_SCAN_STATE_IDLE;
2170 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
2171 wl->scan_vif = NULL;
2172 wl->scan.req = NULL;
2173 ieee80211_scan_completed(wl->hw, true);
2176 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2177 /* disable active roles */
2178 ret = wl1271_ps_elp_wakeup(wl);
2182 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2183 wlvif->bss_type == BSS_TYPE_IBSS) {
2184 if (wl12xx_dev_role_started(wlvif))
2185 wl12xx_stop_dev(wl, wlvif);
2187 ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2192 ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
2196 wl1271_ps_elp_sleep(wl);
2199 /* clear all hlids (except system_hlid) */
2200 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2202 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2203 wlvif->bss_type == BSS_TYPE_IBSS) {
2204 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2205 wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2206 wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2207 wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2209 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2210 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2211 wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2212 wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2213 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2214 wl12xx_free_rate_policy(wl,
2215 &wlvif->ap.ucast_rate_idx[i]);
2216 wl1271_free_ap_keys(wl, wlvif);
2219 dev_kfree_skb(wlvif->probereq);
2220 wlvif->probereq = NULL;
2221 wl12xx_tx_reset_wlvif(wl, wlvif);
2222 if (wl->last_wlvif == wlvif)
2223 wl->last_wlvif = NULL;
2224 list_del(&wlvif->list);
2225 memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
2226 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2227 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2229 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2234 mutex_unlock(&wl->mutex);
2236 del_timer_sync(&wlvif->rx_streaming_timer);
2237 cancel_work_sync(&wlvif->rx_streaming_enable_work);
2238 cancel_work_sync(&wlvif->rx_streaming_disable_work);
2240 mutex_lock(&wl->mutex);
2243 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2244 struct ieee80211_vif *vif)
2246 struct wl1271 *wl = hw->priv;
2247 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2248 struct wl12xx_vif *iter;
2249 struct vif_counter_data vif_count;
2250 bool cancel_recovery = true;
2252 wl12xx_get_vif_count(hw, vif, &vif_count);
2253 mutex_lock(&wl->mutex);
2255 if (wl->state == WL1271_STATE_OFF ||
2256 !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2260 * wl->vif can be null here if someone shuts down the interface
2261 * just when hardware recovery has been started.
2263 wl12xx_for_each_wlvif(wl, iter) {
2267 __wl1271_op_remove_interface(wl, vif, true);
2270 WARN_ON(iter != wlvif);
2271 if (wl12xx_need_fw_change(wl, vif_count, false)) {
2272 wl12xx_force_active_psm(wl);
2273 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2274 wl12xx_queue_recovery_work(wl);
2275 cancel_recovery = false;
2278 mutex_unlock(&wl->mutex);
2279 if (cancel_recovery)
2280 cancel_work_sync(&wl->recovery_work);
2283 static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2284 struct ieee80211_vif *vif,
2285 enum nl80211_iftype new_type, bool p2p)
2287 struct wl1271 *wl = hw->priv;
2290 set_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2291 wl1271_op_remove_interface(hw, vif);
2293 vif->type = new_type;
2295 ret = wl1271_op_add_interface(hw, vif);
2297 clear_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2301 static int wl1271_join(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2305 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
2308 * One of the side effects of the JOIN command is that is clears
2309 * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2310 * to a WPA/WPA2 access point will therefore kill the data-path.
2311 * Currently the only valid scenario for JOIN during association
2312 * is on roaming, in which case we will also be given new keys.
2313 * Keep the below message for now, unless it starts bothering
2314 * users who really like to roam a lot :)
2316 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2317 wl1271_info("JOIN while associated.");
2319 /* clear encryption type */
2320 wlvif->encryption_type = KEY_NONE;
2323 set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2326 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
2328 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
2332 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2336 * The join command disable the keep-alive mode, shut down its process,
2337 * and also clear the template config, so we need to reset it all after
2338 * the join. The acx_aid starts the keep-alive process, and the order
2339 * of the commands below is relevant.
2341 ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
2345 ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
2349 ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
2353 ret = wl1271_acx_keep_alive_config(wl, wlvif,
2354 CMD_TEMPL_KLV_IDX_NULL_DATA,
2355 ACX_KEEP_ALIVE_TPL_VALID);
2363 static int wl1271_unjoin(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2367 if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
2368 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2370 wl12xx_cmd_stop_channel_switch(wl);
2371 ieee80211_chswitch_done(vif, false);
2374 /* to stop listening to a channel, we disconnect */
2375 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
2379 /* reset TX security counters on a clean disconnect */
2380 wlvif->tx_security_last_seq_lsb = 0;
2381 wlvif->tx_security_seq = 0;
2387 static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2389 wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
2390 wlvif->rate_set = wlvif->basic_rate_set;
2393 static int wl1271_sta_handle_idle(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2397 bool cur_idle = !test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2399 if (idle == cur_idle)
2403 /* no need to croc if we weren't busy (e.g. during boot) */
2404 if (wl12xx_dev_role_started(wlvif)) {
2405 ret = wl12xx_stop_dev(wl, wlvif);
2410 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
2411 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2414 ret = wl1271_acx_keep_alive_config(
2415 wl, wlvif, CMD_TEMPL_KLV_IDX_NULL_DATA,
2416 ACX_KEEP_ALIVE_TPL_INVALID);
2419 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2421 /* The current firmware only supports sched_scan in idle */
2422 if (wl->sched_scanning) {
2423 wl1271_scan_sched_scan_stop(wl, wlvif);
2424 ieee80211_sched_scan_stopped(wl->hw);
2427 ret = wl12xx_start_dev(wl, wlvif);
2430 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2437 static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2438 struct ieee80211_conf *conf, u32 changed)
2440 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2443 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2445 /* if the channel changes while joined, join again */
2446 if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
2447 ((wlvif->band != conf->channel->band) ||
2448 (wlvif->channel != channel) ||
2449 (wlvif->channel_type != conf->channel_type))) {
2450 /* send all pending packets */
2451 wl1271_tx_work_locked(wl);
2452 wlvif->band = conf->channel->band;
2453 wlvif->channel = channel;
2454 wlvif->channel_type = conf->channel_type;
2457 ret = wl1271_init_ap_rates(wl, wlvif);
2459 wl1271_error("AP rate policy change failed %d",
2463 * FIXME: the mac80211 should really provide a fixed
2464 * rate to use here. for now, just use the smallest
2465 * possible rate for the band as a fixed rate for
2466 * association frames and other control messages.
2468 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2469 wl1271_set_band_rate(wl, wlvif);
2472 wl1271_tx_min_rate_get(wl,
2473 wlvif->basic_rate_set);
2474 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2476 wl1271_warning("rate policy for channel "
2480 * change the ROC channel. do it only if we are
2481 * not idle. otherwise, CROC will be called
2484 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED,
2486 wl12xx_dev_role_started(wlvif) &&
2487 !(conf->flags & IEEE80211_CONF_IDLE)) {
2488 ret = wl12xx_stop_dev(wl, wlvif);
2492 ret = wl12xx_start_dev(wl, wlvif);
2499 if ((changed & IEEE80211_CONF_CHANGE_PS) && !is_ap) {
2501 if ((conf->flags & IEEE80211_CONF_PS) &&
2502 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
2503 !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
2508 if (wl->conf.conn.forced_ps) {
2509 ps_mode = STATION_POWER_SAVE_MODE;
2510 ps_mode_str = "forced";
2512 ps_mode = STATION_AUTO_PS_MODE;
2513 ps_mode_str = "auto";
2516 wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
2518 ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
2521 wl1271_warning("enter %s ps failed %d",
2524 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
2525 test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
2527 wl1271_debug(DEBUG_PSM, "auto ps disabled");
2529 ret = wl1271_ps_set_mode(wl, wlvif,
2530 STATION_ACTIVE_MODE);
2532 wl1271_warning("exit auto ps failed %d", ret);
2536 if (conf->power_level != wlvif->power_level) {
2537 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
2541 wlvif->power_level = conf->power_level;
2547 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
2549 struct wl1271 *wl = hw->priv;
2550 struct wl12xx_vif *wlvif;
2551 struct ieee80211_conf *conf = &hw->conf;
2552 int channel, ret = 0;
2554 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2556 wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d %s"
2559 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
2561 conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
2565 * mac80211 will go to idle nearly immediately after transmitting some
2566 * frames, such as the deauth. To make sure those frames reach the air,
2567 * wait here until the TX queue is fully flushed.
2569 if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) ||
2570 ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
2571 (conf->flags & IEEE80211_CONF_IDLE)))
2572 wl1271_tx_flush(wl);
2574 mutex_lock(&wl->mutex);
2576 /* we support configuring the channel and band even while off */
2577 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2578 wl->band = conf->channel->band;
2579 wl->channel = channel;
2580 wl->channel_type = conf->channel_type;
2583 if (changed & IEEE80211_CONF_CHANGE_POWER)
2584 wl->power_level = conf->power_level;
2586 if (unlikely(wl->state == WL1271_STATE_OFF))
2589 ret = wl1271_ps_elp_wakeup(wl);
2593 /* configure each interface */
2594 wl12xx_for_each_wlvif(wl, wlvif) {
2595 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
2601 wl1271_ps_elp_sleep(wl);
2604 mutex_unlock(&wl->mutex);
2609 struct wl1271_filter_params {
2612 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
2615 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
2616 struct netdev_hw_addr_list *mc_list)
2618 struct wl1271_filter_params *fp;
2619 struct netdev_hw_addr *ha;
2620 struct wl1271 *wl = hw->priv;
2622 if (unlikely(wl->state == WL1271_STATE_OFF))
2625 fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
2627 wl1271_error("Out of memory setting filters.");
2631 /* update multicast filtering parameters */
2632 fp->mc_list_length = 0;
2633 if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
2634 fp->enabled = false;
2637 netdev_hw_addr_list_for_each(ha, mc_list) {
2638 memcpy(fp->mc_list[fp->mc_list_length],
2639 ha->addr, ETH_ALEN);
2640 fp->mc_list_length++;
2644 return (u64)(unsigned long)fp;
2647 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
2650 FIF_BCN_PRBRESP_PROMISC | \
2654 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
2655 unsigned int changed,
2656 unsigned int *total, u64 multicast)
2658 struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
2659 struct wl1271 *wl = hw->priv;
2660 struct wl12xx_vif *wlvif;
2664 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
2665 " total %x", changed, *total);
2667 mutex_lock(&wl->mutex);
2669 *total &= WL1271_SUPPORTED_FILTERS;
2670 changed &= WL1271_SUPPORTED_FILTERS;
2672 if (unlikely(wl->state == WL1271_STATE_OFF))
2675 ret = wl1271_ps_elp_wakeup(wl);
2679 wl12xx_for_each_wlvif(wl, wlvif) {
2680 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
2681 if (*total & FIF_ALLMULTI)
2682 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2686 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2689 fp->mc_list_length);
2696 * the fw doesn't provide an api to configure the filters. instead,
2697 * the filters configuration is based on the active roles / ROC
2702 wl1271_ps_elp_sleep(wl);
2705 mutex_unlock(&wl->mutex);
2709 static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2710 u8 id, u8 key_type, u8 key_size,
2711 const u8 *key, u8 hlid, u32 tx_seq_32,
2714 struct wl1271_ap_key *ap_key;
2717 wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
2719 if (key_size > MAX_KEY_SIZE)
2723 * Find next free entry in ap_keys. Also check we are not replacing
2726 for (i = 0; i < MAX_NUM_KEYS; i++) {
2727 if (wlvif->ap.recorded_keys[i] == NULL)
2730 if (wlvif->ap.recorded_keys[i]->id == id) {
2731 wl1271_warning("trying to record key replacement");
2736 if (i == MAX_NUM_KEYS)
2739 ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
2744 ap_key->key_type = key_type;
2745 ap_key->key_size = key_size;
2746 memcpy(ap_key->key, key, key_size);
2747 ap_key->hlid = hlid;
2748 ap_key->tx_seq_32 = tx_seq_32;
2749 ap_key->tx_seq_16 = tx_seq_16;
2751 wlvif->ap.recorded_keys[i] = ap_key;
2755 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2759 for (i = 0; i < MAX_NUM_KEYS; i++) {
2760 kfree(wlvif->ap.recorded_keys[i]);
2761 wlvif->ap.recorded_keys[i] = NULL;
2765 static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2768 struct wl1271_ap_key *key;
2769 bool wep_key_added = false;
2771 for (i = 0; i < MAX_NUM_KEYS; i++) {
2773 if (wlvif->ap.recorded_keys[i] == NULL)
2776 key = wlvif->ap.recorded_keys[i];
2778 if (hlid == WL12XX_INVALID_LINK_ID)
2779 hlid = wlvif->ap.bcast_hlid;
2781 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
2782 key->id, key->key_type,
2783 key->key_size, key->key,
2784 hlid, key->tx_seq_32,
2789 if (key->key_type == KEY_WEP)
2790 wep_key_added = true;
2793 if (wep_key_added) {
2794 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
2795 wlvif->ap.bcast_hlid);
2801 wl1271_free_ap_keys(wl, wlvif);
2805 static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2806 u16 action, u8 id, u8 key_type,
2807 u8 key_size, const u8 *key, u32 tx_seq_32,
2808 u16 tx_seq_16, struct ieee80211_sta *sta)
2811 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2814 struct wl1271_station *wl_sta;
2818 wl_sta = (struct wl1271_station *)sta->drv_priv;
2819 hlid = wl_sta->hlid;
2821 hlid = wlvif->ap.bcast_hlid;
2824 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
2826 * We do not support removing keys after AP shutdown.
2827 * Pretend we do to make mac80211 happy.
2829 if (action != KEY_ADD_OR_REPLACE)
2832 ret = wl1271_record_ap_key(wl, wlvif, id,
2834 key, hlid, tx_seq_32,
2837 ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
2838 id, key_type, key_size,
2839 key, hlid, tx_seq_32,
2847 static const u8 bcast_addr[ETH_ALEN] = {
2848 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2851 addr = sta ? sta->addr : bcast_addr;
2853 if (is_zero_ether_addr(addr)) {
2854 /* We dont support TX only encryption */
2858 /* The wl1271 does not allow to remove unicast keys - they
2859 will be cleared automatically on next CMD_JOIN. Ignore the
2860 request silently, as we dont want the mac80211 to emit
2861 an error message. */
2862 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
2865 /* don't remove key if hlid was already deleted */
2866 if (action == KEY_REMOVE &&
2867 wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
2870 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
2871 id, key_type, key_size,
2872 key, addr, tx_seq_32,
2877 /* the default WEP key needs to be configured at least once */
2878 if (key_type == KEY_WEP) {
2879 ret = wl12xx_cmd_set_default_wep_key(wl,
2890 static int wlcore_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2891 struct ieee80211_vif *vif,
2892 struct ieee80211_sta *sta,
2893 struct ieee80211_key_conf *key_conf)
2895 struct wl1271 *wl = hw->priv;
2897 return wlcore_hw_set_key(wl, cmd, vif, sta, key_conf);
2900 int wlcore_set_key(struct wl1271 *wl, enum set_key_cmd cmd,
2901 struct ieee80211_vif *vif,
2902 struct ieee80211_sta *sta,
2903 struct ieee80211_key_conf *key_conf)
2905 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2911 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
2913 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
2914 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
2915 key_conf->cipher, key_conf->keyidx,
2916 key_conf->keylen, key_conf->flags);
2917 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
2919 mutex_lock(&wl->mutex);
2921 if (unlikely(wl->state == WL1271_STATE_OFF)) {
2926 ret = wl1271_ps_elp_wakeup(wl);
2930 switch (key_conf->cipher) {
2931 case WLAN_CIPHER_SUITE_WEP40:
2932 case WLAN_CIPHER_SUITE_WEP104:
2935 key_conf->hw_key_idx = key_conf->keyidx;
2937 case WLAN_CIPHER_SUITE_TKIP:
2938 key_type = KEY_TKIP;
2940 key_conf->hw_key_idx = key_conf->keyidx;
2941 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
2942 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
2944 case WLAN_CIPHER_SUITE_CCMP:
2947 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2948 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
2949 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
2951 case WL1271_CIPHER_SUITE_GEM:
2953 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
2954 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
2957 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
2965 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
2966 key_conf->keyidx, key_type,
2967 key_conf->keylen, key_conf->key,
2968 tx_seq_32, tx_seq_16, sta);
2970 wl1271_error("Could not add or replace key");
2975 * reconfiguring arp response if the unicast (or common)
2976 * encryption key type was changed
2978 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
2979 (sta || key_type == KEY_WEP) &&
2980 wlvif->encryption_type != key_type) {
2981 wlvif->encryption_type = key_type;
2982 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
2984 wl1271_warning("build arp rsp failed: %d", ret);
2991 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
2992 key_conf->keyidx, key_type,
2993 key_conf->keylen, key_conf->key,
2996 wl1271_error("Could not remove key");
3002 wl1271_error("Unsupported key cmd 0x%x", cmd);
3008 wl1271_ps_elp_sleep(wl);
3011 mutex_unlock(&wl->mutex);
3015 EXPORT_SYMBOL_GPL(wlcore_set_key);
3017 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
3018 struct ieee80211_vif *vif,
3019 struct cfg80211_scan_request *req)
3021 struct wl1271 *wl = hw->priv;
3026 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3029 ssid = req->ssids[0].ssid;
3030 len = req->ssids[0].ssid_len;
3033 mutex_lock(&wl->mutex);
3035 if (wl->state == WL1271_STATE_OFF) {
3037 * We cannot return -EBUSY here because cfg80211 will expect
3038 * a call to ieee80211_scan_completed if we do - in this case
3039 * there won't be any call.
3045 ret = wl1271_ps_elp_wakeup(wl);
3049 /* fail if there is any role in ROC */
3050 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
3051 /* don't allow scanning right now */
3056 ret = wl1271_scan(hw->priv, vif, ssid, len, req);
3058 wl1271_ps_elp_sleep(wl);
3060 mutex_unlock(&wl->mutex);
3065 static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3066 struct ieee80211_vif *vif)
3068 struct wl1271 *wl = hw->priv;
3071 wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3073 mutex_lock(&wl->mutex);
3075 if (wl->state == WL1271_STATE_OFF)
3078 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3081 ret = wl1271_ps_elp_wakeup(wl);
3085 if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
3086 ret = wl1271_scan_stop(wl);
3092 * Rearm the tx watchdog just before idling scan. This
3093 * prevents just-finished scans from triggering the watchdog
3095 wl12xx_rearm_tx_watchdog_locked(wl);
3097 wl->scan.state = WL1271_SCAN_STATE_IDLE;
3098 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
3099 wl->scan_vif = NULL;
3100 wl->scan.req = NULL;
3101 ieee80211_scan_completed(wl->hw, true);
3104 wl1271_ps_elp_sleep(wl);
3106 mutex_unlock(&wl->mutex);
3108 cancel_delayed_work_sync(&wl->scan_complete_work);
3111 static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3112 struct ieee80211_vif *vif,
3113 struct cfg80211_sched_scan_request *req,
3114 struct ieee80211_sched_scan_ies *ies)
3116 struct wl1271 *wl = hw->priv;
3117 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3120 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3122 mutex_lock(&wl->mutex);
3124 if (wl->state == WL1271_STATE_OFF) {
3129 ret = wl1271_ps_elp_wakeup(wl);
3133 ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
3137 ret = wl1271_scan_sched_scan_start(wl, wlvif);
3141 wl->sched_scanning = true;
3144 wl1271_ps_elp_sleep(wl);
3146 mutex_unlock(&wl->mutex);
3150 static void wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3151 struct ieee80211_vif *vif)
3153 struct wl1271 *wl = hw->priv;
3154 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3157 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3159 mutex_lock(&wl->mutex);
3161 if (wl->state == WL1271_STATE_OFF)
3164 ret = wl1271_ps_elp_wakeup(wl);
3168 wl1271_scan_sched_scan_stop(wl, wlvif);
3170 wl1271_ps_elp_sleep(wl);
3172 mutex_unlock(&wl->mutex);
3175 static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3177 struct wl1271 *wl = hw->priv;
3180 mutex_lock(&wl->mutex);
3182 if (unlikely(wl->state == WL1271_STATE_OFF)) {
3187 ret = wl1271_ps_elp_wakeup(wl);
3191 ret = wl1271_acx_frag_threshold(wl, value);
3193 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3195 wl1271_ps_elp_sleep(wl);
3198 mutex_unlock(&wl->mutex);
3203 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3205 struct wl1271 *wl = hw->priv;
3206 struct wl12xx_vif *wlvif;
3209 mutex_lock(&wl->mutex);
3211 if (unlikely(wl->state == WL1271_STATE_OFF)) {
3216 ret = wl1271_ps_elp_wakeup(wl);
3220 wl12xx_for_each_wlvif(wl, wlvif) {
3221 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3223 wl1271_warning("set rts threshold failed: %d", ret);
3225 wl1271_ps_elp_sleep(wl);
3228 mutex_unlock(&wl->mutex);
3233 static int wl1271_ssid_set(struct ieee80211_vif *vif, struct sk_buff *skb,
3236 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3238 const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
3242 wl1271_error("No SSID in IEs!");
3247 if (ssid_len > IEEE80211_MAX_SSID_LEN) {
3248 wl1271_error("SSID is too long!");
3252 wlvif->ssid_len = ssid_len;
3253 memcpy(wlvif->ssid, ptr+2, ssid_len);
3257 static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3260 const u8 *next, *end = skb->data + skb->len;
3261 u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3262 skb->len - ieoffset);
3267 memmove(ie, next, end - next);
3268 skb_trim(skb, skb->len - len);
3271 static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3272 unsigned int oui, u8 oui_type,
3276 const u8 *next, *end = skb->data + skb->len;
3277 u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3278 skb->data + ieoffset,
3279 skb->len - ieoffset);
3284 memmove(ie, next, end - next);
3285 skb_trim(skb, skb->len - len);
3288 static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3289 struct ieee80211_vif *vif)
3291 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3292 struct sk_buff *skb;
3295 skb = ieee80211_proberesp_get(wl->hw, vif);
3299 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3300 CMD_TEMPL_AP_PROBE_RESPONSE,
3309 wl1271_debug(DEBUG_AP, "probe response updated");
3310 set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3316 static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3317 struct ieee80211_vif *vif,
3319 size_t probe_rsp_len,
3322 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3323 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
3324 u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3325 int ssid_ie_offset, ie_offset, templ_len;
3328 /* no need to change probe response if the SSID is set correctly */
3329 if (wlvif->ssid_len > 0)
3330 return wl1271_cmd_template_set(wl, wlvif->role_id,
3331 CMD_TEMPL_AP_PROBE_RESPONSE,
3336 if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3337 wl1271_error("probe_rsp template too big");
3341 /* start searching from IE offset */
3342 ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3344 ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3345 probe_rsp_len - ie_offset);
3347 wl1271_error("No SSID in beacon!");
3351 ssid_ie_offset = ptr - probe_rsp_data;
3352 ptr += (ptr[1] + 2);
3354 memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3356 /* insert SSID from bss_conf */
3357 probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3358 probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3359 memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3360 bss_conf->ssid, bss_conf->ssid_len);
3361 templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3363 memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3364 ptr, probe_rsp_len - (ptr - probe_rsp_data));
3365 templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3367 return wl1271_cmd_template_set(wl, wlvif->role_id,
3368 CMD_TEMPL_AP_PROBE_RESPONSE,
3374 static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
3375 struct ieee80211_vif *vif,
3376 struct ieee80211_bss_conf *bss_conf,
3379 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3382 if (changed & BSS_CHANGED_ERP_SLOT) {
3383 if (bss_conf->use_short_slot)
3384 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
3386 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
3388 wl1271_warning("Set slot time failed %d", ret);
3393 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3394 if (bss_conf->use_short_preamble)
3395 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
3397 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
3400 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3401 if (bss_conf->use_cts_prot)
3402 ret = wl1271_acx_cts_protect(wl, wlvif,
3405 ret = wl1271_acx_cts_protect(wl, wlvif,
3406 CTSPROTECT_DISABLE);
3408 wl1271_warning("Set ctsprotect failed %d", ret);
3417 static int wlcore_set_beacon_template(struct wl1271 *wl,
3418 struct ieee80211_vif *vif,
3421 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3422 struct ieee80211_hdr *hdr;
3425 int ieoffset = offsetof(struct ieee80211_mgmt,
3427 struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
3435 wl1271_debug(DEBUG_MASTER, "beacon updated");
3437 ret = wl1271_ssid_set(vif, beacon, ieoffset);
3439 dev_kfree_skb(beacon);
3442 min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3443 tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
3445 ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
3450 dev_kfree_skb(beacon);
3455 * In case we already have a probe-resp beacon set explicitly
3456 * by usermode, don't use the beacon data.
3458 if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
3461 /* remove TIM ie from probe response */
3462 wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
3465 * remove p2p ie from probe response.
3466 * the fw reponds to probe requests that don't include
3467 * the p2p ie. probe requests with p2p ie will be passed,
3468 * and will be responded by the supplicant (the spec
3469 * forbids including the p2p ie when responding to probe
3470 * requests that didn't include it).
3472 wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
3473 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
3475 hdr = (struct ieee80211_hdr *) beacon->data;
3476 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3477 IEEE80211_STYPE_PROBE_RESP);
3479 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
3484 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3485 CMD_TEMPL_PROBE_RESPONSE,
3490 dev_kfree_skb(beacon);
3498 static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
3499 struct ieee80211_vif *vif,
3500 struct ieee80211_bss_conf *bss_conf,
3503 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3504 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
3507 if ((changed & BSS_CHANGED_BEACON_INT)) {
3508 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
3509 bss_conf->beacon_int);
3511 wlvif->beacon_int = bss_conf->beacon_int;
3514 if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
3515 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3517 wl1271_ap_set_probe_resp_tmpl(wl, rate, vif);
3520 if ((changed & BSS_CHANGED_BEACON)) {
3521 ret = wlcore_set_beacon_template(wl, vif, is_ap);
3528 wl1271_error("beacon info change failed: %d", ret);
3532 /* AP mode changes */
3533 static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
3534 struct ieee80211_vif *vif,
3535 struct ieee80211_bss_conf *bss_conf,
3538 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3541 if ((changed & BSS_CHANGED_BASIC_RATES)) {
3542 u32 rates = bss_conf->basic_rates;
3544 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
3546 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
3547 wlvif->basic_rate_set);
3549 ret = wl1271_init_ap_rates(wl, wlvif);
3551 wl1271_error("AP rate policy change failed %d", ret);
3555 ret = wl1271_ap_init_templates(wl, vif);
3559 ret = wl1271_ap_set_probe_resp_tmpl(wl, wlvif->basic_rate, vif);
3563 ret = wlcore_set_beacon_template(wl, vif, true);
3568 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
3572 if ((changed & BSS_CHANGED_BEACON_ENABLED)) {
3573 if (bss_conf->enable_beacon) {
3574 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3575 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
3579 ret = wl1271_ap_init_hwenc(wl, wlvif);
3583 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
3584 wl1271_debug(DEBUG_AP, "started AP");
3587 if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3588 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
3592 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
3593 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
3595 wl1271_debug(DEBUG_AP, "stopped AP");
3600 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
3604 /* Handle HT information change */
3605 if ((changed & BSS_CHANGED_HT) &&
3606 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3607 ret = wl1271_acx_set_ht_information(wl, wlvif,
3608 bss_conf->ht_operation_mode);
3610 wl1271_warning("Set ht information failed %d", ret);
3619 /* STA/IBSS mode changes */
3620 static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
3621 struct ieee80211_vif *vif,
3622 struct ieee80211_bss_conf *bss_conf,
3625 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3626 bool do_join = false, set_assoc = false;
3627 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
3628 bool ibss_joined = false;
3629 u32 sta_rate_set = 0;
3631 struct ieee80211_sta *sta;
3632 bool sta_exists = false;
3633 struct ieee80211_sta_ht_cap sta_ht_cap;
3636 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
3642 if (changed & BSS_CHANGED_IBSS) {
3643 if (bss_conf->ibss_joined) {
3644 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
3647 if (test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED,
3649 wl1271_unjoin(wl, wlvif);
3653 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
3656 /* Need to update the SSID (for filtering etc) */
3657 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
3660 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
3661 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
3662 bss_conf->enable_beacon ? "enabled" : "disabled");
3667 if (changed & BSS_CHANGED_IDLE && !is_ibss) {
3668 ret = wl1271_sta_handle_idle(wl, wlvif, bss_conf->idle);
3670 wl1271_warning("idle mode change failed %d", ret);
3673 if ((changed & BSS_CHANGED_CQM)) {
3674 bool enable = false;
3675 if (bss_conf->cqm_rssi_thold)
3677 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
3678 bss_conf->cqm_rssi_thold,
3679 bss_conf->cqm_rssi_hyst);
3682 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
3685 if (changed & BSS_CHANGED_BSSID)
3686 if (!is_zero_ether_addr(bss_conf->bssid)) {
3687 ret = wl12xx_cmd_build_null_data(wl, wlvif);
3691 ret = wl1271_build_qos_null_data(wl, vif);
3696 if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_HT)) {
3698 sta = ieee80211_find_sta(vif, bss_conf->bssid);
3702 /* save the supp_rates of the ap */
3703 sta_rate_set = sta->supp_rates[wl->hw->conf.channel->band];
3704 if (sta->ht_cap.ht_supported)
3706 (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
3707 (sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
3708 sta_ht_cap = sta->ht_cap;
3715 if ((changed & BSS_CHANGED_ASSOC)) {
3716 if (bss_conf->assoc) {
3719 wlvif->aid = bss_conf->aid;
3720 wlvif->channel_type = bss_conf->channel_type;
3721 wlvif->beacon_int = bss_conf->beacon_int;
3726 * use basic rates from AP, and determine lowest rate
3727 * to use with control frames.
3729 rates = bss_conf->basic_rates;
3730 wlvif->basic_rate_set =
3731 wl1271_tx_enabled_rates_get(wl, rates,
3734 wl1271_tx_min_rate_get(wl,
3735 wlvif->basic_rate_set);
3738 wl1271_tx_enabled_rates_get(wl,
3741 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3746 * with wl1271, we don't need to update the
3747 * beacon_int and dtim_period, because the firmware
3748 * updates it by itself when the first beacon is
3749 * received after a join.
3751 ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
3756 * Get a template for hardware connection maintenance
3758 dev_kfree_skb(wlvif->probereq);
3759 wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
3762 ieoffset = offsetof(struct ieee80211_mgmt,
3763 u.probe_req.variable);
3764 wl1271_ssid_set(vif, wlvif->probereq, ieoffset);
3766 /* enable the connection monitoring feature */
3767 ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
3771 /* use defaults when not associated */
3773 !!test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED,
3776 !!test_and_clear_bit(WLVIF_FLAG_STA_STATE_SENT,
3780 /* free probe-request template */
3781 dev_kfree_skb(wlvif->probereq);
3782 wlvif->probereq = NULL;
3784 /* revert back to minimum rates for the current band */
3785 wl1271_set_band_rate(wl, wlvif);
3787 wl1271_tx_min_rate_get(wl,
3788 wlvif->basic_rate_set);
3789 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3793 /* disable connection monitor features */
3794 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
3796 /* Disable the keep-alive feature */
3797 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
3801 /* restore the bssid filter and go to dummy bssid */
3804 * we might have to disable roc, if there was
3805 * no IF_OPER_UP notification.
3808 ret = wl12xx_croc(wl, wlvif->role_id);
3813 * (we also need to disable roc in case of
3814 * roaming on the same channel. until we will
3815 * have a better flow...)
3817 if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
3818 ret = wl12xx_croc(wl,
3819 wlvif->dev_role_id);
3824 wl1271_unjoin(wl, wlvif);
3825 if (!bss_conf->idle)
3826 wl12xx_start_dev(wl, wlvif);
3831 if (changed & BSS_CHANGED_IBSS) {
3832 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
3833 bss_conf->ibss_joined);
3835 if (bss_conf->ibss_joined) {
3836 u32 rates = bss_conf->basic_rates;
3837 wlvif->basic_rate_set =
3838 wl1271_tx_enabled_rates_get(wl, rates,
3841 wl1271_tx_min_rate_get(wl,
3842 wlvif->basic_rate_set);
3844 /* by default, use 11b + OFDM rates */
3845 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
3846 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3852 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
3857 ret = wl1271_join(wl, wlvif, set_assoc);
3859 wl1271_warning("cmd join failed %d", ret);
3863 /* ROC until connected (after EAPOL exchange) */
3865 ret = wl12xx_roc(wl, wlvif, wlvif->role_id);
3869 if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
3870 wl12xx_set_authorized(wl, wlvif);
3873 * stop device role if started (we might already be in
3876 if (wl12xx_dev_role_started(wlvif)) {
3877 ret = wl12xx_stop_dev(wl, wlvif);
3883 /* Handle new association with HT. Do this after join. */
3885 if ((changed & BSS_CHANGED_HT) &&
3886 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3887 ret = wl1271_acx_set_ht_capabilities(wl,
3892 wl1271_warning("Set ht cap true failed %d",
3897 /* handle new association without HT and disassociation */
3898 else if (changed & BSS_CHANGED_ASSOC) {
3899 ret = wl1271_acx_set_ht_capabilities(wl,
3904 wl1271_warning("Set ht cap false failed %d",
3911 /* Handle HT information change. Done after join. */
3912 if ((changed & BSS_CHANGED_HT) &&
3913 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3914 ret = wl1271_acx_set_ht_information(wl, wlvif,
3915 bss_conf->ht_operation_mode);
3917 wl1271_warning("Set ht information failed %d", ret);
3922 /* Handle arp filtering. Done after join. */
3923 if ((changed & BSS_CHANGED_ARP_FILTER) ||
3924 (!is_ibss && (changed & BSS_CHANGED_QOS))) {
3925 __be32 addr = bss_conf->arp_addr_list[0];
3926 wlvif->sta.qos = bss_conf->qos;
3927 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
3929 if (bss_conf->arp_addr_cnt == 1 &&
3930 bss_conf->arp_filter_enabled) {
3931 wlvif->ip_addr = addr;
3933 * The template should have been configured only upon
3934 * association. however, it seems that the correct ip
3935 * isn't being set (when sending), so we have to
3936 * reconfigure the template upon every ip change.
3938 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3940 wl1271_warning("build arp rsp failed: %d", ret);
3944 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
3945 (ACX_ARP_FILTER_ARP_FILTERING |
3946 ACX_ARP_FILTER_AUTO_ARP),
3950 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
3961 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
3962 struct ieee80211_vif *vif,
3963 struct ieee80211_bss_conf *bss_conf,
3966 struct wl1271 *wl = hw->priv;
3967 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3968 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
3971 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed 0x%x",
3975 * make sure to cancel pending disconnections if our association
3978 if (!is_ap && (changed & BSS_CHANGED_ASSOC))
3979 cancel_delayed_work_sync(&wl->connection_loss_work);
3981 if (is_ap && (changed & BSS_CHANGED_BEACON_ENABLED) &&
3982 !bss_conf->enable_beacon)
3983 wl1271_tx_flush(wl);
3985 mutex_lock(&wl->mutex);
3987 if (unlikely(wl->state == WL1271_STATE_OFF))
3990 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
3993 ret = wl1271_ps_elp_wakeup(wl);
3998 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4000 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4002 wl1271_ps_elp_sleep(wl);
4005 mutex_unlock(&wl->mutex);
4008 static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4009 struct ieee80211_vif *vif, u16 queue,
4010 const struct ieee80211_tx_queue_params *params)
4012 struct wl1271 *wl = hw->priv;
4013 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4017 mutex_lock(&wl->mutex);
4019 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4022 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4024 ps_scheme = CONF_PS_SCHEME_LEGACY;
4026 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4029 ret = wl1271_ps_elp_wakeup(wl);
4034 * the txop is confed in units of 32us by the mac80211,
4037 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4038 params->cw_min, params->cw_max,
4039 params->aifs, params->txop << 5);
4043 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4044 CONF_CHANNEL_TYPE_EDCF,
4045 wl1271_tx_get_queue(queue),
4046 ps_scheme, CONF_ACK_POLICY_LEGACY,
4050 wl1271_ps_elp_sleep(wl);
4053 mutex_unlock(&wl->mutex);
4058 static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4059 struct ieee80211_vif *vif)
4062 struct wl1271 *wl = hw->priv;
4063 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4064 u64 mactime = ULLONG_MAX;
4067 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4069 mutex_lock(&wl->mutex);
4071 if (unlikely(wl->state == WL1271_STATE_OFF))
4074 ret = wl1271_ps_elp_wakeup(wl);
4078 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
4083 wl1271_ps_elp_sleep(wl);
4086 mutex_unlock(&wl->mutex);
4090 static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4091 struct survey_info *survey)
4093 struct wl1271 *wl = hw->priv;
4094 struct ieee80211_conf *conf = &hw->conf;
4099 survey->channel = conf->channel;
4100 survey->filled = SURVEY_INFO_NOISE_DBM;
4101 survey->noise = wl->noise;
4106 static int wl1271_allocate_sta(struct wl1271 *wl,
4107 struct wl12xx_vif *wlvif,
4108 struct ieee80211_sta *sta)
4110 struct wl1271_station *wl_sta;
4114 if (wl->active_sta_count >= AP_MAX_STATIONS) {
4115 wl1271_warning("could not allocate HLID - too much stations");
4119 wl_sta = (struct wl1271_station *)sta->drv_priv;
4120 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4122 wl1271_warning("could not allocate HLID - too many links");
4126 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
4127 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
4128 wl->active_sta_count++;
4132 void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
4134 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
4137 clear_bit(hlid, wlvif->ap.sta_hlid_map);
4138 memset(wl->links[hlid].addr, 0, ETH_ALEN);
4139 wl->links[hlid].ba_bitmap = 0;
4140 __clear_bit(hlid, &wl->ap_ps_map);
4141 __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
4142 wl12xx_free_link(wl, wlvif, &hlid);
4143 wl->active_sta_count--;
4146 * rearm the tx watchdog when the last STA is freed - give the FW a
4147 * chance to return STA-buffered packets before complaining.
4149 if (wl->active_sta_count == 0)
4150 wl12xx_rearm_tx_watchdog_locked(wl);
4153 static int wl12xx_sta_add(struct wl1271 *wl,
4154 struct wl12xx_vif *wlvif,
4155 struct ieee80211_sta *sta)
4157 struct wl1271_station *wl_sta;
4161 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4163 ret = wl1271_allocate_sta(wl, wlvif, sta);
4167 wl_sta = (struct wl1271_station *)sta->drv_priv;
4168 hlid = wl_sta->hlid;
4170 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
4172 wl1271_free_sta(wl, wlvif, hlid);
4177 static int wl12xx_sta_remove(struct wl1271 *wl,
4178 struct wl12xx_vif *wlvif,
4179 struct ieee80211_sta *sta)
4181 struct wl1271_station *wl_sta;
4184 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4186 wl_sta = (struct wl1271_station *)sta->drv_priv;
4188 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
4191 ret = wl12xx_cmd_remove_peer(wl, wl_sta->hlid);
4195 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
4199 static int wl12xx_update_sta_state(struct wl1271 *wl,
4200 struct wl12xx_vif *wlvif,
4201 struct ieee80211_sta *sta,
4202 enum ieee80211_sta_state old_state,
4203 enum ieee80211_sta_state new_state)
4205 struct wl1271_station *wl_sta;
4207 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
4208 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
4211 wl_sta = (struct wl1271_station *)sta->drv_priv;
4212 hlid = wl_sta->hlid;
4214 /* Add station (AP mode) */
4216 old_state == IEEE80211_STA_NOTEXIST &&
4217 new_state == IEEE80211_STA_NONE)
4218 return wl12xx_sta_add(wl, wlvif, sta);
4220 /* Remove station (AP mode) */
4222 old_state == IEEE80211_STA_NONE &&
4223 new_state == IEEE80211_STA_NOTEXIST) {
4225 wl12xx_sta_remove(wl, wlvif, sta);
4229 /* Authorize station (AP mode) */
4231 new_state == IEEE80211_STA_AUTHORIZED) {
4232 ret = wl12xx_cmd_set_peer_state(wl, hlid);
4236 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
4241 /* Authorize station */
4243 new_state == IEEE80211_STA_AUTHORIZED) {
4244 set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
4245 return wl12xx_set_authorized(wl, wlvif);
4249 old_state == IEEE80211_STA_AUTHORIZED &&
4250 new_state == IEEE80211_STA_ASSOC) {
4251 clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
4258 static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
4259 struct ieee80211_vif *vif,
4260 struct ieee80211_sta *sta,
4261 enum ieee80211_sta_state old_state,
4262 enum ieee80211_sta_state new_state)
4264 struct wl1271 *wl = hw->priv;
4265 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4268 wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
4269 sta->aid, old_state, new_state);
4271 mutex_lock(&wl->mutex);
4273 if (unlikely(wl->state == WL1271_STATE_OFF)) {
4278 ret = wl1271_ps_elp_wakeup(wl);
4282 ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
4284 wl1271_ps_elp_sleep(wl);
4286 mutex_unlock(&wl->mutex);
4287 if (new_state < old_state)
4292 static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
4293 struct ieee80211_vif *vif,
4294 enum ieee80211_ampdu_mlme_action action,
4295 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4298 struct wl1271 *wl = hw->priv;
4299 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4301 u8 hlid, *ba_bitmap;
4303 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
4306 /* sanity check - the fields in FW are only 8bits wide */
4307 if (WARN_ON(tid > 0xFF))
4310 mutex_lock(&wl->mutex);
4312 if (unlikely(wl->state == WL1271_STATE_OFF)) {
4317 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
4318 hlid = wlvif->sta.hlid;
4319 ba_bitmap = &wlvif->sta.ba_rx_bitmap;
4320 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
4321 struct wl1271_station *wl_sta;
4323 wl_sta = (struct wl1271_station *)sta->drv_priv;
4324 hlid = wl_sta->hlid;
4325 ba_bitmap = &wl->links[hlid].ba_bitmap;
4331 ret = wl1271_ps_elp_wakeup(wl);
4335 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
4339 case IEEE80211_AMPDU_RX_START:
4340 if (!wlvif->ba_support || !wlvif->ba_allowed) {
4345 if (wl->ba_rx_session_count >= RX_BA_MAX_SESSIONS) {
4347 wl1271_error("exceeded max RX BA sessions");
4351 if (*ba_bitmap & BIT(tid)) {
4353 wl1271_error("cannot enable RX BA session on active "
4358 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
4361 *ba_bitmap |= BIT(tid);
4362 wl->ba_rx_session_count++;
4366 case IEEE80211_AMPDU_RX_STOP:
4367 if (!(*ba_bitmap & BIT(tid))) {
4369 wl1271_error("no active RX BA session on tid: %d",
4374 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
4377 *ba_bitmap &= ~BIT(tid);
4378 wl->ba_rx_session_count--;
4383 * The BA initiator session management in FW independently.
4384 * Falling break here on purpose for all TX APDU commands.
4386 case IEEE80211_AMPDU_TX_START:
4387 case IEEE80211_AMPDU_TX_STOP:
4388 case IEEE80211_AMPDU_TX_OPERATIONAL:
4393 wl1271_error("Incorrect ampdu action id=%x\n", action);
4397 wl1271_ps_elp_sleep(wl);
4400 mutex_unlock(&wl->mutex);
4405 static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
4406 struct ieee80211_vif *vif,
4407 const struct cfg80211_bitrate_mask *mask)
4409 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4410 struct wl1271 *wl = hw->priv;
4413 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
4414 mask->control[NL80211_BAND_2GHZ].legacy,
4415 mask->control[NL80211_BAND_5GHZ].legacy);
4417 mutex_lock(&wl->mutex);
4419 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
4420 wlvif->bitrate_masks[i] =
4421 wl1271_tx_enabled_rates_get(wl,
4422 mask->control[i].legacy,
4425 if (unlikely(wl->state == WL1271_STATE_OFF))
4428 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4429 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
4431 ret = wl1271_ps_elp_wakeup(wl);
4435 wl1271_set_band_rate(wl, wlvif);
4437 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4438 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4440 wl1271_ps_elp_sleep(wl);
4443 mutex_unlock(&wl->mutex);
4448 static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
4449 struct ieee80211_channel_switch *ch_switch)
4451 struct wl1271 *wl = hw->priv;
4452 struct wl12xx_vif *wlvif;
4455 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
4457 wl1271_tx_flush(wl);
4459 mutex_lock(&wl->mutex);
4461 if (unlikely(wl->state == WL1271_STATE_OFF)) {
4462 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4463 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4464 ieee80211_chswitch_done(vif, false);
4469 ret = wl1271_ps_elp_wakeup(wl);
4473 /* TODO: change mac80211 to pass vif as param */
4474 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4475 ret = wl12xx_cmd_channel_switch(wl, wlvif, ch_switch);
4478 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
4481 wl1271_ps_elp_sleep(wl);
4484 mutex_unlock(&wl->mutex);
4487 static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
4489 struct wl1271 *wl = hw->priv;
4492 mutex_lock(&wl->mutex);
4494 if (unlikely(wl->state == WL1271_STATE_OFF))
4497 /* packets are considered pending if in the TX queue or the FW */
4498 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
4500 mutex_unlock(&wl->mutex);
4505 /* can't be const, mac80211 writes to this */
4506 static struct ieee80211_rate wl1271_rates[] = {
4508 .hw_value = CONF_HW_BIT_RATE_1MBPS,
4509 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
4511 .hw_value = CONF_HW_BIT_RATE_2MBPS,
4512 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
4513 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4515 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
4516 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
4517 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4519 .hw_value = CONF_HW_BIT_RATE_11MBPS,
4520 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
4521 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4523 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4524 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4526 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4527 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4529 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4530 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4532 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4533 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4535 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4536 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4538 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4539 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4541 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4542 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4544 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4545 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4548 /* can't be const, mac80211 writes to this */
4549 static struct ieee80211_channel wl1271_channels[] = {
4550 { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
4551 { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
4552 { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
4553 { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
4554 { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
4555 { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
4556 { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
4557 { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
4558 { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
4559 { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
4560 { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
4561 { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
4562 { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
4563 { .hw_value = 14, .center_freq = 2484, .max_power = 25 },
4566 /* can't be const, mac80211 writes to this */
4567 static struct ieee80211_supported_band wl1271_band_2ghz = {
4568 .channels = wl1271_channels,
4569 .n_channels = ARRAY_SIZE(wl1271_channels),
4570 .bitrates = wl1271_rates,
4571 .n_bitrates = ARRAY_SIZE(wl1271_rates),
4574 /* 5 GHz data rates for WL1273 */
4575 static struct ieee80211_rate wl1271_rates_5ghz[] = {
4577 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4578 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4580 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4581 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4583 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4584 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4586 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4587 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4589 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4590 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4592 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4593 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4595 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4596 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4598 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4599 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4602 /* 5 GHz band channels for WL1273 */
4603 static struct ieee80211_channel wl1271_channels_5ghz[] = {
4604 { .hw_value = 7, .center_freq = 5035, .max_power = 25 },
4605 { .hw_value = 8, .center_freq = 5040, .max_power = 25 },
4606 { .hw_value = 9, .center_freq = 5045, .max_power = 25 },
4607 { .hw_value = 11, .center_freq = 5055, .max_power = 25 },
4608 { .hw_value = 12, .center_freq = 5060, .max_power = 25 },
4609 { .hw_value = 16, .center_freq = 5080, .max_power = 25 },
4610 { .hw_value = 34, .center_freq = 5170, .max_power = 25 },
4611 { .hw_value = 36, .center_freq = 5180, .max_power = 25 },
4612 { .hw_value = 38, .center_freq = 5190, .max_power = 25 },
4613 { .hw_value = 40, .center_freq = 5200, .max_power = 25 },
4614 { .hw_value = 42, .center_freq = 5210, .max_power = 25 },
4615 { .hw_value = 44, .center_freq = 5220, .max_power = 25 },
4616 { .hw_value = 46, .center_freq = 5230, .max_power = 25 },
4617 { .hw_value = 48, .center_freq = 5240, .max_power = 25 },
4618 { .hw_value = 52, .center_freq = 5260, .max_power = 25 },
4619 { .hw_value = 56, .center_freq = 5280, .max_power = 25 },
4620 { .hw_value = 60, .center_freq = 5300, .max_power = 25 },
4621 { .hw_value = 64, .center_freq = 5320, .max_power = 25 },
4622 { .hw_value = 100, .center_freq = 5500, .max_power = 25 },
4623 { .hw_value = 104, .center_freq = 5520, .max_power = 25 },
4624 { .hw_value = 108, .center_freq = 5540, .max_power = 25 },
4625 { .hw_value = 112, .center_freq = 5560, .max_power = 25 },
4626 { .hw_value = 116, .center_freq = 5580, .max_power = 25 },
4627 { .hw_value = 120, .center_freq = 5600, .max_power = 25 },
4628 { .hw_value = 124, .center_freq = 5620, .max_power = 25 },
4629 { .hw_value = 128, .center_freq = 5640, .max_power = 25 },
4630 { .hw_value = 132, .center_freq = 5660, .max_power = 25 },
4631 { .hw_value = 136, .center_freq = 5680, .max_power = 25 },
4632 { .hw_value = 140, .center_freq = 5700, .max_power = 25 },
4633 { .hw_value = 149, .center_freq = 5745, .max_power = 25 },
4634 { .hw_value = 153, .center_freq = 5765, .max_power = 25 },
4635 { .hw_value = 157, .center_freq = 5785, .max_power = 25 },
4636 { .hw_value = 161, .center_freq = 5805, .max_power = 25 },
4637 { .hw_value = 165, .center_freq = 5825, .max_power = 25 },
4640 static struct ieee80211_supported_band wl1271_band_5ghz = {
4641 .channels = wl1271_channels_5ghz,
4642 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
4643 .bitrates = wl1271_rates_5ghz,
4644 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
4647 static const struct ieee80211_ops wl1271_ops = {
4648 .start = wl1271_op_start,
4649 .stop = wl1271_op_stop,
4650 .add_interface = wl1271_op_add_interface,
4651 .remove_interface = wl1271_op_remove_interface,
4652 .change_interface = wl12xx_op_change_interface,
4654 .suspend = wl1271_op_suspend,
4655 .resume = wl1271_op_resume,
4657 .config = wl1271_op_config,
4658 .prepare_multicast = wl1271_op_prepare_multicast,
4659 .configure_filter = wl1271_op_configure_filter,
4661 .set_key = wlcore_op_set_key,
4662 .hw_scan = wl1271_op_hw_scan,
4663 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
4664 .sched_scan_start = wl1271_op_sched_scan_start,
4665 .sched_scan_stop = wl1271_op_sched_scan_stop,
4666 .bss_info_changed = wl1271_op_bss_info_changed,
4667 .set_frag_threshold = wl1271_op_set_frag_threshold,
4668 .set_rts_threshold = wl1271_op_set_rts_threshold,
4669 .conf_tx = wl1271_op_conf_tx,
4670 .get_tsf = wl1271_op_get_tsf,
4671 .get_survey = wl1271_op_get_survey,
4672 .sta_state = wl12xx_op_sta_state,
4673 .ampdu_action = wl1271_op_ampdu_action,
4674 .tx_frames_pending = wl1271_tx_frames_pending,
4675 .set_bitrate_mask = wl12xx_set_bitrate_mask,
4676 .channel_switch = wl12xx_op_channel_switch,
4677 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
4681 u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum ieee80211_band band)
4687 if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
4688 wl1271_error("Illegal RX rate from HW: %d", rate);
4692 idx = wl->band_rate_to_idx[band][rate];
4693 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
4694 wl1271_error("Unsupported RX rate from HW: %d", rate);
4701 static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
4702 struct device_attribute *attr,
4705 struct wl1271 *wl = dev_get_drvdata(dev);
4710 mutex_lock(&wl->mutex);
4711 len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
4713 mutex_unlock(&wl->mutex);
4719 static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
4720 struct device_attribute *attr,
4721 const char *buf, size_t count)
4723 struct wl1271 *wl = dev_get_drvdata(dev);
4727 ret = kstrtoul(buf, 10, &res);
4729 wl1271_warning("incorrect value written to bt_coex_mode");
4733 mutex_lock(&wl->mutex);
4737 if (res == wl->sg_enabled)
4740 wl->sg_enabled = res;
4742 if (wl->state == WL1271_STATE_OFF)
4745 ret = wl1271_ps_elp_wakeup(wl);
4749 wl1271_acx_sg_enable(wl, wl->sg_enabled);
4750 wl1271_ps_elp_sleep(wl);
4753 mutex_unlock(&wl->mutex);
4757 static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
4758 wl1271_sysfs_show_bt_coex_state,
4759 wl1271_sysfs_store_bt_coex_state);
4761 static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
4762 struct device_attribute *attr,
4765 struct wl1271 *wl = dev_get_drvdata(dev);
4770 mutex_lock(&wl->mutex);
4771 if (wl->hw_pg_ver >= 0)
4772 len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
4774 len = snprintf(buf, len, "n/a\n");
4775 mutex_unlock(&wl->mutex);
4780 static DEVICE_ATTR(hw_pg_ver, S_IRUGO,
4781 wl1271_sysfs_show_hw_pg_ver, NULL);
4783 static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
4784 struct bin_attribute *bin_attr,
4785 char *buffer, loff_t pos, size_t count)
4787 struct device *dev = container_of(kobj, struct device, kobj);
4788 struct wl1271 *wl = dev_get_drvdata(dev);
4792 ret = mutex_lock_interruptible(&wl->mutex);
4794 return -ERESTARTSYS;
4796 /* Let only one thread read the log at a time, blocking others */
4797 while (wl->fwlog_size == 0) {
4800 prepare_to_wait_exclusive(&wl->fwlog_waitq,
4802 TASK_INTERRUPTIBLE);
4804 if (wl->fwlog_size != 0) {
4805 finish_wait(&wl->fwlog_waitq, &wait);
4809 mutex_unlock(&wl->mutex);
4812 finish_wait(&wl->fwlog_waitq, &wait);
4814 if (signal_pending(current))
4815 return -ERESTARTSYS;
4817 ret = mutex_lock_interruptible(&wl->mutex);
4819 return -ERESTARTSYS;
4822 /* Check if the fwlog is still valid */
4823 if (wl->fwlog_size < 0) {
4824 mutex_unlock(&wl->mutex);
4828 /* Seeking is not supported - old logs are not kept. Disregard pos. */
4829 len = min(count, (size_t)wl->fwlog_size);
4830 wl->fwlog_size -= len;
4831 memcpy(buffer, wl->fwlog, len);
4833 /* Make room for new messages */
4834 memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
4836 mutex_unlock(&wl->mutex);
4841 static struct bin_attribute fwlog_attr = {
4842 .attr = {.name = "fwlog", .mode = S_IRUSR},
4843 .read = wl1271_sysfs_read_fwlog,
4846 static void wl1271_connection_loss_work(struct work_struct *work)
4848 struct delayed_work *dwork;
4850 struct ieee80211_vif *vif;
4851 struct wl12xx_vif *wlvif;
4853 dwork = container_of(work, struct delayed_work, work);
4854 wl = container_of(dwork, struct wl1271, connection_loss_work);
4856 wl1271_info("Connection loss work.");
4858 mutex_lock(&wl->mutex);
4860 if (unlikely(wl->state == WL1271_STATE_OFF))
4863 /* Call mac80211 connection loss */
4864 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4865 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
4867 vif = wl12xx_wlvif_to_vif(wlvif);
4868 ieee80211_connection_loss(vif);
4871 mutex_unlock(&wl->mutex);
4874 static void wl12xx_derive_mac_addresses(struct wl1271 *wl,
4875 u32 oui, u32 nic, int n)
4879 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x, n %d",
4882 if (nic + n - 1 > 0xffffff)
4883 wl1271_warning("NIC part of the MAC address wraps around!");
4885 for (i = 0; i < n; i++) {
4886 wl->addresses[i].addr[0] = (u8)(oui >> 16);
4887 wl->addresses[i].addr[1] = (u8)(oui >> 8);
4888 wl->addresses[i].addr[2] = (u8) oui;
4889 wl->addresses[i].addr[3] = (u8)(nic >> 16);
4890 wl->addresses[i].addr[4] = (u8)(nic >> 8);
4891 wl->addresses[i].addr[5] = (u8) nic;
4895 wl->hw->wiphy->n_addresses = n;
4896 wl->hw->wiphy->addresses = wl->addresses;
4899 static int wl12xx_get_hw_info(struct wl1271 *wl)
4903 ret = wl12xx_set_power_on(wl);
4907 wl->chip.id = wlcore_read_reg(wl, REG_CHIP_ID_B);
4909 wl->fuse_oui_addr = 0;
4910 wl->fuse_nic_addr = 0;
4912 wl->hw_pg_ver = wl->ops->get_pg_ver(wl);
4914 if (wl->ops->get_mac)
4915 wl->ops->get_mac(wl);
4917 wl1271_power_off(wl);
4922 static int wl1271_register_hw(struct wl1271 *wl)
4925 u32 oui_addr = 0, nic_addr = 0;
4927 if (wl->mac80211_registered)
4930 wl1271_fetch_nvs(wl);
4931 if (wl->nvs != NULL) {
4932 /* NOTE: The wl->nvs->nvs element must be first, in
4933 * order to simplify the casting, we assume it is at
4934 * the beginning of the wl->nvs structure.
4936 u8 *nvs_ptr = (u8 *)wl->nvs;
4939 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
4941 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
4944 /* if the MAC address is zeroed in the NVS derive from fuse */
4945 if (oui_addr == 0 && nic_addr == 0) {
4946 oui_addr = wl->fuse_oui_addr;
4947 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
4948 nic_addr = wl->fuse_nic_addr + 1;
4951 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr, 2);
4953 ret = ieee80211_register_hw(wl->hw);
4955 wl1271_error("unable to register mac80211 hw: %d", ret);
4959 wl->mac80211_registered = true;
4961 wl1271_debugfs_init(wl);
4963 wl1271_notice("loaded");
4969 static void wl1271_unregister_hw(struct wl1271 *wl)
4972 wl1271_plt_stop(wl);
4974 ieee80211_unregister_hw(wl->hw);
4975 wl->mac80211_registered = false;
4979 static int wl1271_init_ieee80211(struct wl1271 *wl)
4981 static const u32 cipher_suites[] = {
4982 WLAN_CIPHER_SUITE_WEP40,
4983 WLAN_CIPHER_SUITE_WEP104,
4984 WLAN_CIPHER_SUITE_TKIP,
4985 WLAN_CIPHER_SUITE_CCMP,
4986 WL1271_CIPHER_SUITE_GEM,
4989 /* The tx descriptor buffer */
4990 wl->hw->extra_tx_headroom = sizeof(struct wl1271_tx_hw_descr);
4992 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
4993 wl->hw->extra_tx_headroom += WL1271_EXTRA_SPACE_TKIP;
4996 /* FIXME: find a proper value */
4997 wl->hw->channel_change_time = 10000;
4998 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
5000 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5001 IEEE80211_HW_SUPPORTS_PS |
5002 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5003 IEEE80211_HW_SUPPORTS_UAPSD |
5004 IEEE80211_HW_HAS_RATE_CONTROL |
5005 IEEE80211_HW_CONNECTION_MONITOR |
5006 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5007 IEEE80211_HW_SPECTRUM_MGMT |
5008 IEEE80211_HW_AP_LINK_PS |
5009 IEEE80211_HW_AMPDU_AGGREGATION |
5010 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
5011 IEEE80211_HW_SCAN_WHILE_IDLE;
5013 wl->hw->wiphy->cipher_suites = cipher_suites;
5014 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5016 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
5017 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
5018 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
5019 wl->hw->wiphy->max_scan_ssids = 1;
5020 wl->hw->wiphy->max_sched_scan_ssids = 16;
5021 wl->hw->wiphy->max_match_sets = 16;
5023 * Maximum length of elements in scanning probe request templates
5024 * should be the maximum length possible for a template, without
5025 * the IEEE80211 header of the template
5027 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
5028 sizeof(struct ieee80211_header);
5030 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
5031 sizeof(struct ieee80211_header);
5033 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD |
5034 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
5036 /* make sure all our channels fit in the scanned_ch bitmask */
5037 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5038 ARRAY_SIZE(wl1271_channels_5ghz) >
5039 WL1271_MAX_CHANNELS);
5041 * We keep local copies of the band structs because we need to
5042 * modify them on a per-device basis.
5044 memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5045 sizeof(wl1271_band_2ghz));
5046 memcpy(&wl->bands[IEEE80211_BAND_2GHZ].ht_cap,
5047 &wl->ht_cap[IEEE80211_BAND_2GHZ],
5048 sizeof(*wl->ht_cap));
5049 memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5050 sizeof(wl1271_band_5ghz));
5051 memcpy(&wl->bands[IEEE80211_BAND_5GHZ].ht_cap,
5052 &wl->ht_cap[IEEE80211_BAND_5GHZ],
5053 sizeof(*wl->ht_cap));
5055 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5056 &wl->bands[IEEE80211_BAND_2GHZ];
5057 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5058 &wl->bands[IEEE80211_BAND_5GHZ];
5061 wl->hw->max_rates = 1;
5063 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5065 /* the FW answers probe-requests in AP-mode */
5066 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5067 wl->hw->wiphy->probe_resp_offload =
5068 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5069 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5070 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5072 SET_IEEE80211_DEV(wl->hw, wl->dev);
5074 wl->hw->sta_data_size = sizeof(struct wl1271_station);
5075 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
5077 wl->hw->max_rx_aggregation_subframes = wl->conf.ht.rx_ba_win_size;
5082 #define WL1271_DEFAULT_CHANNEL 0
5084 struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size)
5086 struct ieee80211_hw *hw;
5091 BUILD_BUG_ON(AP_MAX_STATIONS > WL12XX_MAX_LINKS);
5093 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5095 wl1271_error("could not alloc ieee80211_hw");
5101 memset(wl, 0, sizeof(*wl));
5103 wl->priv = kzalloc(priv_size, GFP_KERNEL);
5105 wl1271_error("could not alloc wl priv");
5107 goto err_priv_alloc;
5110 INIT_LIST_HEAD(&wl->wlvif_list);
5114 for (i = 0; i < NUM_TX_QUEUES; i++)
5115 for (j = 0; j < WL12XX_MAX_LINKS; j++)
5116 skb_queue_head_init(&wl->links[j].tx_queue[i]);
5118 skb_queue_head_init(&wl->deferred_rx_queue);
5119 skb_queue_head_init(&wl->deferred_tx_queue);
5121 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
5122 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
5123 INIT_WORK(&wl->tx_work, wl1271_tx_work);
5124 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5125 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
5126 INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
5127 INIT_DELAYED_WORK(&wl->connection_loss_work,
5128 wl1271_connection_loss_work);
5130 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5131 if (!wl->freezable_wq) {
5136 wl->channel = WL1271_DEFAULT_CHANNEL;
5138 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
5139 wl->band = IEEE80211_BAND_2GHZ;
5140 wl->channel_type = NL80211_CHAN_NO_HT;
5142 wl->sg_enabled = true;
5145 wl->ap_fw_ps_map = 0;
5147 wl->platform_quirks = 0;
5148 wl->sched_scanning = false;
5149 wl->system_hlid = WL12XX_SYSTEM_HLID;
5150 wl->active_sta_count = 0;
5152 init_waitqueue_head(&wl->fwlog_waitq);
5154 /* The system link is always allocated */
5155 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5157 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
5158 for (i = 0; i < wl->num_tx_desc; i++)
5159 wl->tx_frames[i] = NULL;
5161 spin_lock_init(&wl->wl_lock);
5163 wl->state = WL1271_STATE_OFF;
5164 wl->fw_type = WL12XX_FW_TYPE_NONE;
5165 mutex_init(&wl->mutex);
5166 mutex_init(&wl->flush_mutex);
5168 order = get_order(WL1271_AGGR_BUFFER_SIZE);
5169 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5170 if (!wl->aggr_buf) {
5175 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5176 if (!wl->dummy_packet) {
5181 /* Allocate one page for the FW log */
5182 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5185 goto err_dummy_packet;
5188 wl->mbox = kmalloc(sizeof(*wl->mbox), GFP_KERNEL | GFP_DMA);
5197 free_page((unsigned long)wl->fwlog);
5200 dev_kfree_skb(wl->dummy_packet);
5203 free_pages((unsigned long)wl->aggr_buf, order);
5206 destroy_workqueue(wl->freezable_wq);
5209 wl1271_debugfs_exit(wl);
5213 ieee80211_free_hw(hw);
5217 return ERR_PTR(ret);
5219 EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
5221 int wlcore_free_hw(struct wl1271 *wl)
5223 /* Unblock any fwlog readers */
5224 mutex_lock(&wl->mutex);
5225 wl->fwlog_size = -1;
5226 wake_up_interruptible_all(&wl->fwlog_waitq);
5227 mutex_unlock(&wl->mutex);
5229 device_remove_bin_file(wl->dev, &fwlog_attr);
5231 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5233 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5234 free_page((unsigned long)wl->fwlog);
5235 dev_kfree_skb(wl->dummy_packet);
5236 free_pages((unsigned long)wl->aggr_buf,
5237 get_order(WL1271_AGGR_BUFFER_SIZE));
5239 wl1271_debugfs_exit(wl);
5243 wl->fw_type = WL12XX_FW_TYPE_NONE;
5247 kfree(wl->fw_status_1);
5248 kfree(wl->tx_res_if);
5249 destroy_workqueue(wl->freezable_wq);
5252 ieee80211_free_hw(wl->hw);
5256 EXPORT_SYMBOL_GPL(wlcore_free_hw);
5258 static irqreturn_t wl12xx_hardirq(int irq, void *cookie)
5260 struct wl1271 *wl = cookie;
5261 unsigned long flags;
5263 wl1271_debug(DEBUG_IRQ, "IRQ");
5265 /* complete the ELP completion */
5266 spin_lock_irqsave(&wl->wl_lock, flags);
5267 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
5268 if (wl->elp_compl) {
5269 complete(wl->elp_compl);
5270 wl->elp_compl = NULL;
5273 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
5274 /* don't enqueue a work right now. mark it as pending */
5275 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
5276 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
5277 disable_irq_nosync(wl->irq);
5278 pm_wakeup_event(wl->dev, 0);
5279 spin_unlock_irqrestore(&wl->wl_lock, flags);
5282 spin_unlock_irqrestore(&wl->wl_lock, flags);
5284 return IRQ_WAKE_THREAD;
5287 int __devinit wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
5289 struct wl12xx_platform_data *pdata = pdev->dev.platform_data;
5290 unsigned long irqflags;
5293 if (!wl->ops || !wl->ptable) {
5298 BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
5300 /* adjust some runtime configuration parameters */
5301 wlcore_adjust_conf(wl);
5303 wl->irq = platform_get_irq(pdev, 0);
5304 wl->platform_quirks = pdata->platform_quirks;
5305 wl->set_power = pdata->set_power;
5306 wl->dev = &pdev->dev;
5307 wl->if_ops = pdata->ops;
5309 platform_set_drvdata(pdev, wl);
5311 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
5312 irqflags = IRQF_TRIGGER_RISING;
5314 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
5316 ret = request_threaded_irq(wl->irq, wl12xx_hardirq, wl1271_irq,
5320 wl1271_error("request_irq() failed: %d", ret);
5325 ret = enable_irq_wake(wl->irq);
5327 wl->irq_wake_enabled = true;
5328 device_init_wakeup(wl->dev, 1);
5329 if (pdata->pwr_in_suspend) {
5330 wl->hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
5331 wl->hw->wiphy->wowlan.n_patterns =
5332 WL1271_MAX_RX_FILTERS;
5333 wl->hw->wiphy->wowlan.pattern_min_len = 1;
5334 wl->hw->wiphy->wowlan.pattern_max_len =
5335 WL1271_RX_FILTER_MAX_PATTERN_SIZE;
5339 disable_irq(wl->irq);
5341 ret = wl12xx_get_hw_info(wl);
5343 wl1271_error("couldn't get hw info");
5347 ret = wl->ops->identify_chip(wl);
5351 ret = wl1271_init_ieee80211(wl);
5355 ret = wl1271_register_hw(wl);
5359 /* Create sysfs file to control bt coex state */
5360 ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
5362 wl1271_error("failed to create sysfs file bt_coex_state");
5366 /* Create sysfs file to get HW PG version */
5367 ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
5369 wl1271_error("failed to create sysfs file hw_pg_ver");
5370 goto out_bt_coex_state;
5373 /* Create sysfs file for the FW log */
5374 ret = device_create_bin_file(wl->dev, &fwlog_attr);
5376 wl1271_error("failed to create sysfs file fwlog");
5383 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5386 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5389 free_irq(wl->irq, wl);
5397 EXPORT_SYMBOL_GPL(wlcore_probe);
5399 int __devexit wlcore_remove(struct platform_device *pdev)
5401 struct wl1271 *wl = platform_get_drvdata(pdev);
5403 if (wl->irq_wake_enabled) {
5404 device_init_wakeup(wl->dev, 0);
5405 disable_irq_wake(wl->irq);
5407 wl1271_unregister_hw(wl);
5408 free_irq(wl->irq, wl);
5413 EXPORT_SYMBOL_GPL(wlcore_remove);
5415 u32 wl12xx_debug_level = DEBUG_NONE;
5416 EXPORT_SYMBOL_GPL(wl12xx_debug_level);
5417 module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
5418 MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
5420 module_param_named(fwlog, fwlog_param, charp, 0);
5421 MODULE_PARM_DESC(fwlog,
5422 "FW logger options: continuous, ondemand, dbgpins or disable");
5424 module_param(bug_on_recovery, bool, S_IRUSR | S_IWUSR);
5425 MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
5427 module_param(no_recovery, bool, S_IRUSR | S_IWUSR);
5428 MODULE_PARM_DESC(no_recovery, "Prevent HW recovery. FW will remain stuck.");
5430 MODULE_LICENSE("GPL");
5431 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
5432 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");