2 * Atheros CARL9170 driver
4 * mac80211 interaction code
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, see
21 * http://www.gnu.org/licenses/.
23 * This file incorporates work covered by the following copyright and
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
27 * Permission to use, copy, modify, and/or distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
31 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/module.h>
43 #include <linux/etherdevice.h>
44 #include <linux/random.h>
45 #include <net/mac80211.h>
46 #include <net/cfg80211.h>
51 static bool modparam_nohwcrypt;
52 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
53 MODULE_PARM_DESC(nohwcrypt, "Disable hardware crypto offload.");
56 module_param_named(noht, modparam_noht, int, S_IRUGO);
57 MODULE_PARM_DESC(noht, "Disable MPDU aggregation.");
59 #define RATE(_bitrate, _hw_rate, _txpidx, _flags) { \
60 .bitrate = (_bitrate), \
62 .hw_value = (_hw_rate) | (_txpidx) << 4, \
65 struct ieee80211_rate __carl9170_ratetable[] = {
67 RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE),
68 RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE),
69 RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE),
81 #define carl9170_g_ratetable (__carl9170_ratetable + 0)
82 #define carl9170_g_ratetable_size 12
83 #define carl9170_a_ratetable (__carl9170_ratetable + 4)
84 #define carl9170_a_ratetable_size 8
87 * NB: The hw_value is used as an index into the carl9170_phy_freq_params
88 * array in phy.c so that we don't have to do frequency lookups!
90 #define CHAN(_freq, _idx) { \
91 .center_freq = (_freq), \
93 .max_power = 18, /* XXX */ \
96 static struct ieee80211_channel carl9170_2ghz_chantable[] = {
113 static struct ieee80211_channel carl9170_5ghz_chantable[] = {
152 #define CARL9170_HT_CAP \
154 .ht_supported = true, \
155 .cap = IEEE80211_HT_CAP_MAX_AMSDU | \
156 IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
157 IEEE80211_HT_CAP_SGI_40 | \
158 IEEE80211_HT_CAP_DSSSCCK40 | \
159 IEEE80211_HT_CAP_SM_PS, \
160 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, \
161 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, \
163 .rx_mask = { 0xff, 0xff, 0, 0, 0x1, 0, 0, 0, 0, 0, }, \
164 .rx_highest = cpu_to_le16(300), \
165 .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
169 static struct ieee80211_supported_band carl9170_band_2GHz = {
170 .channels = carl9170_2ghz_chantable,
171 .n_channels = ARRAY_SIZE(carl9170_2ghz_chantable),
172 .bitrates = carl9170_g_ratetable,
173 .n_bitrates = carl9170_g_ratetable_size,
174 .ht_cap = CARL9170_HT_CAP,
177 static struct ieee80211_supported_band carl9170_band_5GHz = {
178 .channels = carl9170_5ghz_chantable,
179 .n_channels = ARRAY_SIZE(carl9170_5ghz_chantable),
180 .bitrates = carl9170_a_ratetable,
181 .n_bitrates = carl9170_a_ratetable_size,
182 .ht_cap = CARL9170_HT_CAP,
185 static void carl9170_ampdu_gc(struct ar9170 *ar)
187 struct carl9170_sta_tid *tid_info;
191 list_for_each_entry_rcu(tid_info, &ar->tx_ampdu_list, list) {
192 spin_lock_bh(&ar->tx_ampdu_list_lock);
193 if (tid_info->state == CARL9170_TID_STATE_SHUTDOWN) {
194 tid_info->state = CARL9170_TID_STATE_KILLED;
195 list_del_rcu(&tid_info->list);
196 ar->tx_ampdu_list_len--;
197 list_add_tail(&tid_info->tmp_list, &tid_gc);
199 spin_unlock_bh(&ar->tx_ampdu_list_lock);
202 rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
207 while (!list_empty(&tid_gc)) {
209 tid_info = list_first_entry(&tid_gc, struct carl9170_sta_tid,
212 while ((skb = __skb_dequeue(&tid_info->queue)))
213 carl9170_tx_status(ar, skb, false);
215 list_del_init(&tid_info->tmp_list);
220 static void carl9170_flush(struct ar9170 *ar, bool drop_queued)
226 * We can only drop frames which have not been uploaded
230 for (i = 0; i < ar->hw->queues; i++) {
233 while ((skb = skb_dequeue(&ar->tx_pending[i]))) {
234 struct ieee80211_tx_info *info;
236 info = IEEE80211_SKB_CB(skb);
237 if (info->flags & IEEE80211_TX_CTL_AMPDU)
238 atomic_dec(&ar->tx_ampdu_upload);
240 carl9170_tx_status(ar, skb, false);
245 /* Wait for all other outstanding frames to timeout. */
246 if (atomic_read(&ar->tx_total_queued))
247 WARN_ON(wait_for_completion_timeout(&ar->tx_flush, HZ) == 0);
250 static void carl9170_flush_ba(struct ar9170 *ar)
252 struct sk_buff_head free;
253 struct carl9170_sta_tid *tid_info;
256 __skb_queue_head_init(&free);
259 spin_lock_bh(&ar->tx_ampdu_list_lock);
260 list_for_each_entry_rcu(tid_info, &ar->tx_ampdu_list, list) {
261 if (tid_info->state > CARL9170_TID_STATE_SUSPEND) {
262 tid_info->state = CARL9170_TID_STATE_SUSPEND;
264 spin_lock(&tid_info->lock);
265 while ((skb = __skb_dequeue(&tid_info->queue)))
266 __skb_queue_tail(&free, skb);
267 spin_unlock(&tid_info->lock);
270 spin_unlock_bh(&ar->tx_ampdu_list_lock);
273 while ((skb = __skb_dequeue(&free)))
274 carl9170_tx_status(ar, skb, false);
277 static void carl9170_zap_queues(struct ar9170 *ar)
279 struct carl9170_vif_info *cvif;
282 carl9170_ampdu_gc(ar);
284 carl9170_flush_ba(ar);
285 carl9170_flush(ar, true);
287 for (i = 0; i < ar->hw->queues; i++) {
288 spin_lock_bh(&ar->tx_status[i].lock);
289 while (!skb_queue_empty(&ar->tx_status[i])) {
292 skb = skb_peek(&ar->tx_status[i]);
293 carl9170_tx_get_skb(skb);
294 spin_unlock_bh(&ar->tx_status[i].lock);
295 carl9170_tx_drop(ar, skb);
296 spin_lock_bh(&ar->tx_status[i].lock);
297 carl9170_tx_put_skb(skb);
299 spin_unlock_bh(&ar->tx_status[i].lock);
302 BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_SOFT < 1);
303 BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_HARD < CARL9170_NUM_TX_LIMIT_SOFT);
304 BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_HARD >= CARL9170_BAW_BITS);
306 /* reinitialize queues statistics */
307 memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
308 for (i = 0; i < ar->hw->queues; i++)
309 ar->tx_stats[i].limit = CARL9170_NUM_TX_LIMIT_HARD;
311 for (i = 0; i < DIV_ROUND_UP(ar->fw.mem_blocks, BITS_PER_LONG); i++)
312 ar->mem_bitmap[i] = 0;
315 list_for_each_entry_rcu(cvif, &ar->vif_list, list) {
316 spin_lock_bh(&ar->beacon_lock);
317 dev_kfree_skb_any(cvif->beacon);
319 spin_unlock_bh(&ar->beacon_lock);
323 atomic_set(&ar->tx_ampdu_upload, 0);
324 atomic_set(&ar->tx_ampdu_scheduler, 0);
325 atomic_set(&ar->tx_total_pending, 0);
326 atomic_set(&ar->tx_total_queued, 0);
327 atomic_set(&ar->mem_free_blocks, ar->fw.mem_blocks);
330 #define CARL9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
332 queue.aifs = ai_fs; \
333 queue.cw_min = cwmin; \
334 queue.cw_max = cwmax; \
335 queue.txop = _txop; \
338 static int carl9170_op_start(struct ieee80211_hw *hw)
340 struct ar9170 *ar = hw->priv;
343 mutex_lock(&ar->mutex);
345 carl9170_zap_queues(ar);
347 /* reset QoS defaults */
348 CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_VO], 2, 3, 7, 47);
349 CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_VI], 2, 7, 15, 94);
350 CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_BE], 3, 15, 1023, 0);
351 CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_BK], 7, 15, 1023, 0);
352 CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_SPECIAL], 2, 3, 7, 0);
354 ar->current_factor = ar->current_density = -1;
355 /* "The first key is unique." */
357 ar->filter_state = 0;
358 ar->ps.last_action = jiffies;
359 ar->ps.last_slept = jiffies;
360 ar->erp_mode = CARL9170_ERP_AUTO;
361 ar->rx_software_decryption = false;
362 ar->disable_offload = false;
364 for (i = 0; i < ar->hw->queues; i++) {
365 ar->queue_stop_timeout[i] = jiffies;
366 ar->max_queue_stop_timeout[i] = 0;
369 atomic_set(&ar->mem_allocs, 0);
371 err = carl9170_usb_open(ar);
375 err = carl9170_init_mac(ar);
379 err = carl9170_set_qos(ar);
383 if (ar->fw.rx_filter) {
384 err = carl9170_rx_filter(ar, CARL9170_RX_FILTER_OTHER_RA |
385 CARL9170_RX_FILTER_CTL_OTHER | CARL9170_RX_FILTER_BAD);
390 err = carl9170_write_reg(ar, AR9170_MAC_REG_DMA_TRIGGER,
391 AR9170_DMA_TRIGGER_RXQ);
395 /* Clear key-cache */
396 for (i = 0; i < AR9170_CAM_MAX_USER + 4; i++) {
397 err = carl9170_upload_key(ar, i, NULL, AR9170_ENC_ALG_NONE,
402 err = carl9170_upload_key(ar, i, NULL, AR9170_ENC_ALG_NONE,
407 if (i < AR9170_CAM_MAX_USER) {
408 err = carl9170_disable_key(ar, i);
414 carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STARTED);
416 ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
417 round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
419 ieee80211_wake_queues(ar->hw);
423 mutex_unlock(&ar->mutex);
427 static void carl9170_cancel_worker(struct ar9170 *ar)
429 cancel_delayed_work_sync(&ar->stat_work);
430 cancel_delayed_work_sync(&ar->tx_janitor);
431 #ifdef CONFIG_CARL9170_LEDS
432 cancel_delayed_work_sync(&ar->led_work);
433 #endif /* CONFIG_CARL9170_LEDS */
434 cancel_work_sync(&ar->ps_work);
435 cancel_work_sync(&ar->ping_work);
436 cancel_work_sync(&ar->ampdu_work);
439 static void carl9170_op_stop(struct ieee80211_hw *hw)
441 struct ar9170 *ar = hw->priv;
443 carl9170_set_state_when(ar, CARL9170_STARTED, CARL9170_IDLE);
445 ieee80211_stop_queues(ar->hw);
447 mutex_lock(&ar->mutex);
448 if (IS_ACCEPTING_CMD(ar)) {
449 RCU_INIT_POINTER(ar->beacon_iter, NULL);
451 carl9170_led_set_state(ar, 0);
454 carl9170_write_reg(ar, AR9170_MAC_REG_DMA_TRIGGER, 0);
455 carl9170_usb_stop(ar);
458 carl9170_zap_queues(ar);
459 mutex_unlock(&ar->mutex);
461 carl9170_cancel_worker(ar);
464 static void carl9170_restart_work(struct work_struct *work)
466 struct ar9170 *ar = container_of(work, struct ar9170,
471 ar->filter_state = 0;
472 carl9170_cancel_worker(ar);
474 mutex_lock(&ar->mutex);
475 err = carl9170_usb_restart(ar);
476 if (net_ratelimit()) {
478 dev_err(&ar->udev->dev, "Failed to restart device "
481 dev_info(&ar->udev->dev, "device restarted "
486 carl9170_zap_queues(ar);
487 mutex_unlock(&ar->mutex);
489 ar->restart_counter++;
490 atomic_set(&ar->pending_restarts, 0);
492 ieee80211_restart_hw(ar->hw);
495 * The reset was unsuccessful and the device seems to
496 * be dead. But there's still one option: a low-level
497 * usb subsystem reset...
500 carl9170_usb_reset(ar);
504 void carl9170_restart(struct ar9170 *ar, const enum carl9170_restart_reasons r)
506 carl9170_set_state_when(ar, CARL9170_STARTED, CARL9170_IDLE);
509 * Sometimes, an error can trigger several different reset events.
510 * By ignoring these *surplus* reset events, the device won't be
511 * killed again, right after it has recovered.
513 if (atomic_inc_return(&ar->pending_restarts) > 1) {
514 dev_dbg(&ar->udev->dev, "ignoring restart (%d)\n", r);
518 ieee80211_stop_queues(ar->hw);
520 dev_err(&ar->udev->dev, "restart device (%d)\n", r);
522 if (!WARN_ON(r == CARL9170_RR_NO_REASON) ||
523 !WARN_ON(r >= __CARL9170_RR_LAST))
529 if (IS_ACCEPTING_CMD(ar) && !ar->needs_full_reset)
530 ieee80211_queue_work(ar->hw, &ar->restart_work);
532 carl9170_usb_reset(ar);
535 * At this point, the device instance might have vanished/disabled.
536 * So, don't put any code which access the ar9170 struct
537 * without proper protection.
541 static void carl9170_ping_work(struct work_struct *work)
543 struct ar9170 *ar = container_of(work, struct ar9170, ping_work);
549 mutex_lock(&ar->mutex);
550 err = carl9170_echo_test(ar, 0xdeadbeef);
552 carl9170_restart(ar, CARL9170_RR_UNRESPONSIVE_DEVICE);
553 mutex_unlock(&ar->mutex);
556 static int carl9170_init_interface(struct ar9170 *ar,
557 struct ieee80211_vif *vif)
559 struct ath_common *common = &ar->common;
563 WARN_ON_ONCE(IS_STARTED(ar));
567 memcpy(common->macaddr, vif->addr, ETH_ALEN);
569 if (modparam_nohwcrypt ||
570 ((vif->type != NL80211_IFTYPE_STATION) &&
571 (vif->type != NL80211_IFTYPE_AP))) {
572 ar->rx_software_decryption = true;
573 ar->disable_offload = true;
576 err = carl9170_set_operating_mode(ar);
580 static int carl9170_op_add_interface(struct ieee80211_hw *hw,
581 struct ieee80211_vif *vif)
583 struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
584 struct ieee80211_vif *main_vif;
585 struct ar9170 *ar = hw->priv;
586 int vif_id = -1, err = 0;
588 mutex_lock(&ar->mutex);
590 if (vif_priv->active) {
592 * Skip the interface structure initialization,
593 * if the vif survived the _restart call.
595 vif_id = vif_priv->id;
596 vif_priv->enable_beacon = false;
598 spin_lock_bh(&ar->beacon_lock);
599 dev_kfree_skb_any(vif_priv->beacon);
600 vif_priv->beacon = NULL;
601 spin_unlock_bh(&ar->beacon_lock);
606 main_vif = carl9170_get_main_vif(ar);
609 switch (main_vif->type) {
610 case NL80211_IFTYPE_STATION:
611 if (vif->type == NL80211_IFTYPE_STATION)
619 case NL80211_IFTYPE_AP:
620 if ((vif->type == NL80211_IFTYPE_STATION) ||
621 (vif->type == NL80211_IFTYPE_WDS) ||
622 (vif->type == NL80211_IFTYPE_AP))
635 vif_id = bitmap_find_free_region(&ar->vif_bitmap, ar->fw.vif_num, 0);
644 BUG_ON(ar->vif_priv[vif_id].id != vif_id);
646 vif_priv->active = true;
647 vif_priv->id = vif_id;
648 vif_priv->enable_beacon = false;
650 list_add_tail_rcu(&vif_priv->list, &ar->vif_list);
651 rcu_assign_pointer(ar->vif_priv[vif_id].vif, vif);
654 if (carl9170_get_main_vif(ar) == vif) {
655 rcu_assign_pointer(ar->beacon_iter, vif_priv);
658 err = carl9170_init_interface(ar, vif);
663 err = carl9170_mod_virtual_mac(ar, vif_id, vif->addr);
669 if (ar->fw.tx_seq_table) {
670 err = carl9170_write_reg(ar, ar->fw.tx_seq_table + vif_id * 4,
677 if (err && (vif_id >= 0)) {
678 vif_priv->active = false;
679 bitmap_release_region(&ar->vif_bitmap, vif_id, 0);
681 RCU_INIT_POINTER(ar->vif_priv[vif_id].vif, NULL);
682 list_del_rcu(&vif_priv->list);
683 mutex_unlock(&ar->mutex);
687 ar->ps.off_override |= PS_OFF_VIF;
689 mutex_unlock(&ar->mutex);
695 static void carl9170_op_remove_interface(struct ieee80211_hw *hw,
696 struct ieee80211_vif *vif)
698 struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
699 struct ieee80211_vif *main_vif;
700 struct ar9170 *ar = hw->priv;
703 mutex_lock(&ar->mutex);
705 if (WARN_ON_ONCE(!vif_priv->active))
711 main_vif = carl9170_get_main_vif(ar);
715 vif_priv->active = false;
716 WARN_ON(vif_priv->enable_beacon);
717 vif_priv->enable_beacon = false;
718 list_del_rcu(&vif_priv->list);
719 RCU_INIT_POINTER(ar->vif_priv[id].vif, NULL);
721 if (vif == main_vif) {
725 WARN_ON(carl9170_init_interface(ar,
726 carl9170_get_main_vif(ar)));
728 carl9170_set_operating_mode(ar);
733 WARN_ON(carl9170_mod_virtual_mac(ar, id, NULL));
736 carl9170_update_beacon(ar, false);
737 carl9170_flush_cab(ar, id);
739 spin_lock_bh(&ar->beacon_lock);
740 dev_kfree_skb_any(vif_priv->beacon);
741 vif_priv->beacon = NULL;
742 spin_unlock_bh(&ar->beacon_lock);
744 bitmap_release_region(&ar->vif_bitmap, id, 0);
746 carl9170_set_beacon_timers(ar);
749 ar->ps.off_override &= ~PS_OFF_VIF;
752 mutex_unlock(&ar->mutex);
757 void carl9170_ps_check(struct ar9170 *ar)
759 ieee80211_queue_work(ar->hw, &ar->ps_work);
762 /* caller must hold ar->mutex */
763 static int carl9170_ps_update(struct ar9170 *ar)
768 if (!ar->ps.off_override)
769 ps = (ar->hw->conf.flags & IEEE80211_CONF_PS);
771 if (ps != ar->ps.state) {
772 err = carl9170_powersave(ar, ps);
776 if (ar->ps.state && !ps) {
777 ar->ps.sleep_ms = jiffies_to_msecs(jiffies -
782 ar->ps.last_slept = jiffies;
784 ar->ps.last_action = jiffies;
791 static void carl9170_ps_work(struct work_struct *work)
793 struct ar9170 *ar = container_of(work, struct ar9170,
795 mutex_lock(&ar->mutex);
797 WARN_ON_ONCE(carl9170_ps_update(ar) != 0);
798 mutex_unlock(&ar->mutex);
801 static int carl9170_update_survey(struct ar9170 *ar, bool flush, bool noise)
806 err = carl9170_get_noisefloor(ar);
811 if (ar->fw.hw_counters) {
812 err = carl9170_collect_tally(ar);
818 memset(&ar->tally, 0, sizeof(ar->tally));
823 static void carl9170_stat_work(struct work_struct *work)
825 struct ar9170 *ar = container_of(work, struct ar9170, stat_work.work);
828 mutex_lock(&ar->mutex);
829 err = carl9170_update_survey(ar, false, true);
830 mutex_unlock(&ar->mutex);
835 ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
836 round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
839 static int carl9170_op_config(struct ieee80211_hw *hw, u32 changed)
841 struct ar9170 *ar = hw->priv;
844 mutex_lock(&ar->mutex);
845 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
850 if (changed & IEEE80211_CONF_CHANGE_PS) {
851 err = carl9170_ps_update(ar);
856 if (changed & IEEE80211_CONF_CHANGE_SMPS) {
861 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
862 /* adjust slot time for 5 GHz */
863 err = carl9170_set_slot_time(ar);
867 err = carl9170_update_survey(ar, true, false);
871 err = carl9170_set_channel(ar, hw->conf.channel,
872 hw->conf.channel_type, CARL9170_RFI_NONE);
876 err = carl9170_update_survey(ar, false, true);
880 err = carl9170_set_dyn_sifs_ack(ar);
884 err = carl9170_set_rts_cts_rate(ar);
889 if (changed & IEEE80211_CONF_CHANGE_POWER) {
890 err = carl9170_set_mac_tpc(ar, ar->hw->conf.channel);
896 mutex_unlock(&ar->mutex);
900 static u64 carl9170_op_prepare_multicast(struct ieee80211_hw *hw,
901 struct netdev_hw_addr_list *mc_list)
903 struct netdev_hw_addr *ha;
906 /* always get broadcast frames */
907 mchash = 1ULL << (0xff >> 2);
909 netdev_hw_addr_list_for_each(ha, mc_list)
910 mchash |= 1ULL << (ha->addr[5] >> 2);
915 static void carl9170_op_configure_filter(struct ieee80211_hw *hw,
916 unsigned int changed_flags,
917 unsigned int *new_flags,
920 struct ar9170 *ar = hw->priv;
922 /* mask supported flags */
923 *new_flags &= FIF_ALLMULTI | ar->rx_filter_caps;
925 if (!IS_ACCEPTING_CMD(ar))
928 mutex_lock(&ar->mutex);
930 ar->filter_state = *new_flags;
932 * We can support more by setting the sniffer bit and
933 * then checking the error flags, later.
936 if (*new_flags & FIF_ALLMULTI)
939 if (multicast != ar->cur_mc_hash)
940 WARN_ON(carl9170_update_multicast(ar, multicast));
942 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
943 ar->sniffer_enabled = !!(*new_flags &
944 (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS));
946 WARN_ON(carl9170_set_operating_mode(ar));
949 if (ar->fw.rx_filter && changed_flags & ar->rx_filter_caps) {
952 if (!ar->fw.ba_filter)
953 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
955 if (!(*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL)))
956 rx_filter |= CARL9170_RX_FILTER_BAD;
958 if (!(*new_flags & FIF_CONTROL))
959 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
961 if (!(*new_flags & FIF_PSPOLL))
962 rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
964 if (!(*new_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))) {
965 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
966 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
969 WARN_ON(carl9170_rx_filter(ar, rx_filter));
972 mutex_unlock(&ar->mutex);
976 static void carl9170_op_bss_info_changed(struct ieee80211_hw *hw,
977 struct ieee80211_vif *vif,
978 struct ieee80211_bss_conf *bss_conf,
981 struct ar9170 *ar = hw->priv;
982 struct ath_common *common = &ar->common;
984 struct carl9170_vif_info *vif_priv;
985 struct ieee80211_vif *main_vif;
987 mutex_lock(&ar->mutex);
988 vif_priv = (void *) vif->drv_priv;
989 main_vif = carl9170_get_main_vif(ar);
990 if (WARN_ON(!main_vif))
993 if (changed & BSS_CHANGED_BEACON_ENABLED) {
994 struct carl9170_vif_info *iter;
997 vif_priv->enable_beacon = bss_conf->enable_beacon;
999 list_for_each_entry_rcu(iter, &ar->vif_list, list) {
1000 if (iter->active && iter->enable_beacon)
1006 ar->beacon_enabled = i;
1009 if (changed & BSS_CHANGED_BEACON) {
1010 err = carl9170_update_beacon(ar, false);
1015 if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
1016 BSS_CHANGED_BEACON_INT)) {
1018 if (main_vif != vif) {
1019 bss_conf->beacon_int = main_vif->bss_conf.beacon_int;
1020 bss_conf->dtim_period = main_vif->bss_conf.dtim_period;
1024 * Therefore a hard limit for the broadcast traffic should
1025 * prevent false alarms.
1027 if (vif->type != NL80211_IFTYPE_STATION &&
1028 (bss_conf->beacon_int * bss_conf->dtim_period >=
1029 (CARL9170_QUEUE_STUCK_TIMEOUT / 2))) {
1034 err = carl9170_set_beacon_timers(ar);
1039 if (changed & BSS_CHANGED_HT) {
1046 if (main_vif != vif)
1050 * The following settings can only be changed by the
1054 if (changed & BSS_CHANGED_BSSID) {
1055 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1056 err = carl9170_set_operating_mode(ar);
1061 if (changed & BSS_CHANGED_ASSOC) {
1062 ar->common.curaid = bss_conf->aid;
1063 err = carl9170_set_beacon_timers(ar);
1068 if (changed & BSS_CHANGED_ERP_SLOT) {
1069 err = carl9170_set_slot_time(ar);
1074 if (changed & BSS_CHANGED_BASIC_RATES) {
1075 err = carl9170_set_mac_rates(ar);
1081 WARN_ON_ONCE(err && IS_STARTED(ar));
1082 mutex_unlock(&ar->mutex);
1085 static u64 carl9170_op_get_tsf(struct ieee80211_hw *hw,
1086 struct ieee80211_vif *vif)
1088 struct ar9170 *ar = hw->priv;
1089 struct carl9170_tsf_rsp tsf;
1092 mutex_lock(&ar->mutex);
1093 err = carl9170_exec_cmd(ar, CARL9170_CMD_READ_TSF,
1094 0, NULL, sizeof(tsf), &tsf);
1095 mutex_unlock(&ar->mutex);
1099 return le64_to_cpu(tsf.tsf_64);
1102 static int carl9170_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1103 struct ieee80211_vif *vif,
1104 struct ieee80211_sta *sta,
1105 struct ieee80211_key_conf *key)
1107 struct ar9170 *ar = hw->priv;
1111 if (ar->disable_offload || !vif)
1115 * We have to fall back to software encryption, whenever
1116 * the user choose to participates in an IBSS or is connected
1117 * to more than one network.
1119 * This is very unfortunate, because some machines cannot handle
1120 * the high througput speed in 802.11n networks.
1123 if (!is_main_vif(ar, vif)) {
1124 mutex_lock(&ar->mutex);
1129 * While the hardware supports *catch-all* key, for offloading
1130 * group-key en-/de-cryption. The way of how the hardware
1131 * decides which keyId maps to which key, remains a mystery...
1133 if ((vif->type != NL80211_IFTYPE_STATION &&
1134 vif->type != NL80211_IFTYPE_ADHOC) &&
1135 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1138 switch (key->cipher) {
1139 case WLAN_CIPHER_SUITE_WEP40:
1140 ktype = AR9170_ENC_ALG_WEP64;
1142 case WLAN_CIPHER_SUITE_WEP104:
1143 ktype = AR9170_ENC_ALG_WEP128;
1145 case WLAN_CIPHER_SUITE_TKIP:
1146 ktype = AR9170_ENC_ALG_TKIP;
1148 case WLAN_CIPHER_SUITE_CCMP:
1149 ktype = AR9170_ENC_ALG_AESCCMP;
1155 mutex_lock(&ar->mutex);
1156 if (cmd == SET_KEY) {
1157 if (!IS_STARTED(ar)) {
1162 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1165 i = 64 + key->keyidx;
1167 for (i = 0; i < 64; i++)
1168 if (!(ar->usedkeys & BIT(i)))
1174 key->hw_key_idx = i;
1176 err = carl9170_upload_key(ar, i, sta ? sta->addr : NULL,
1178 min_t(u8, 16, key->keylen));
1182 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1183 err = carl9170_upload_key(ar, i, sta ? sta->addr :
1190 * hardware is not capable generating MMIC
1191 * of fragmented frames!
1193 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1197 ar->usedkeys |= BIT(i);
1199 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1201 if (!IS_STARTED(ar)) {
1202 /* The device is gone... together with the key ;-) */
1207 if (key->hw_key_idx < 64) {
1208 ar->usedkeys &= ~BIT(key->hw_key_idx);
1210 err = carl9170_upload_key(ar, key->hw_key_idx, NULL,
1211 AR9170_ENC_ALG_NONE, 0,
1216 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1217 err = carl9170_upload_key(ar, key->hw_key_idx,
1219 AR9170_ENC_ALG_NONE,
1227 err = carl9170_disable_key(ar, key->hw_key_idx);
1233 mutex_unlock(&ar->mutex);
1237 if (!ar->rx_software_decryption) {
1238 ar->rx_software_decryption = true;
1239 carl9170_set_operating_mode(ar);
1241 mutex_unlock(&ar->mutex);
1245 static int carl9170_op_sta_add(struct ieee80211_hw *hw,
1246 struct ieee80211_vif *vif,
1247 struct ieee80211_sta *sta)
1249 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1252 atomic_set(&sta_info->pending_frames, 0);
1254 if (sta->ht_cap.ht_supported) {
1255 if (sta->ht_cap.ampdu_density > 6) {
1257 * HW does support 16us AMPDU density.
1258 * No HT-Xmit for station.
1264 for (i = 0; i < CARL9170_NUM_TID; i++)
1265 RCU_INIT_POINTER(sta_info->agg[i], NULL);
1267 sta_info->ampdu_max_len = 1 << (3 + sta->ht_cap.ampdu_factor);
1268 sta_info->ht_sta = true;
1274 static int carl9170_op_sta_remove(struct ieee80211_hw *hw,
1275 struct ieee80211_vif *vif,
1276 struct ieee80211_sta *sta)
1278 struct ar9170 *ar = hw->priv;
1279 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1281 bool cleanup = false;
1283 if (sta->ht_cap.ht_supported) {
1285 sta_info->ht_sta = false;
1288 for (i = 0; i < CARL9170_NUM_TID; i++) {
1289 struct carl9170_sta_tid *tid_info;
1291 tid_info = rcu_dereference(sta_info->agg[i]);
1292 RCU_INIT_POINTER(sta_info->agg[i], NULL);
1297 spin_lock_bh(&ar->tx_ampdu_list_lock);
1298 if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1299 tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1300 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1306 carl9170_ampdu_gc(ar);
1312 static int carl9170_op_conf_tx(struct ieee80211_hw *hw,
1313 struct ieee80211_vif *vif, u16 queue,
1314 const struct ieee80211_tx_queue_params *param)
1316 struct ar9170 *ar = hw->priv;
1319 mutex_lock(&ar->mutex);
1320 if (queue < ar->hw->queues) {
1321 memcpy(&ar->edcf[ar9170_qmap[queue]], param, sizeof(*param));
1322 ret = carl9170_set_qos(ar);
1327 mutex_unlock(&ar->mutex);
1331 static void carl9170_ampdu_work(struct work_struct *work)
1333 struct ar9170 *ar = container_of(work, struct ar9170,
1336 if (!IS_STARTED(ar))
1339 mutex_lock(&ar->mutex);
1340 carl9170_ampdu_gc(ar);
1341 mutex_unlock(&ar->mutex);
1344 static int carl9170_op_ampdu_action(struct ieee80211_hw *hw,
1345 struct ieee80211_vif *vif,
1346 enum ieee80211_ampdu_mlme_action action,
1347 struct ieee80211_sta *sta,
1348 u16 tid, u16 *ssn, u8 buf_size)
1350 struct ar9170 *ar = hw->priv;
1351 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1352 struct carl9170_sta_tid *tid_info;
1358 case IEEE80211_AMPDU_TX_START:
1359 if (!sta_info->ht_sta)
1363 if (rcu_dereference(sta_info->agg[tid])) {
1368 tid_info = kzalloc(sizeof(struct carl9170_sta_tid),
1375 tid_info->hsn = tid_info->bsn = tid_info->snx = (*ssn);
1376 tid_info->state = CARL9170_TID_STATE_PROGRESS;
1377 tid_info->tid = tid;
1378 tid_info->max = sta_info->ampdu_max_len;
1380 INIT_LIST_HEAD(&tid_info->list);
1381 INIT_LIST_HEAD(&tid_info->tmp_list);
1382 skb_queue_head_init(&tid_info->queue);
1383 spin_lock_init(&tid_info->lock);
1385 spin_lock_bh(&ar->tx_ampdu_list_lock);
1386 ar->tx_ampdu_list_len++;
1387 list_add_tail_rcu(&tid_info->list, &ar->tx_ampdu_list);
1388 rcu_assign_pointer(sta_info->agg[tid], tid_info);
1389 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1392 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1395 case IEEE80211_AMPDU_TX_STOP:
1397 tid_info = rcu_dereference(sta_info->agg[tid]);
1399 spin_lock_bh(&ar->tx_ampdu_list_lock);
1400 if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1401 tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1402 spin_unlock_bh(&ar->tx_ampdu_list_lock);
1405 RCU_INIT_POINTER(sta_info->agg[tid], NULL);
1408 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1409 ieee80211_queue_work(ar->hw, &ar->ampdu_work);
1412 case IEEE80211_AMPDU_TX_OPERATIONAL:
1414 tid_info = rcu_dereference(sta_info->agg[tid]);
1416 sta_info->stats[tid].clear = true;
1417 sta_info->stats[tid].req = false;
1420 bitmap_zero(tid_info->bitmap, CARL9170_BAW_SIZE);
1421 tid_info->state = CARL9170_TID_STATE_IDLE;
1425 if (WARN_ON_ONCE(!tid_info))
1430 case IEEE80211_AMPDU_RX_START:
1431 case IEEE80211_AMPDU_RX_STOP:
1432 /* Handled by hardware */
1442 #ifdef CONFIG_CARL9170_WPC
1443 static int carl9170_register_wps_button(struct ar9170 *ar)
1445 struct input_dev *input;
1448 if (!(ar->features & CARL9170_WPS_BUTTON))
1451 input = input_allocate_device();
1455 snprintf(ar->wps.name, sizeof(ar->wps.name), "%s WPS Button",
1456 wiphy_name(ar->hw->wiphy));
1458 snprintf(ar->wps.phys, sizeof(ar->wps.phys),
1459 "ieee80211/%s/input0", wiphy_name(ar->hw->wiphy));
1461 input->name = ar->wps.name;
1462 input->phys = ar->wps.phys;
1463 input->id.bustype = BUS_USB;
1464 input->dev.parent = &ar->hw->wiphy->dev;
1466 input_set_capability(input, EV_KEY, KEY_WPS_BUTTON);
1468 err = input_register_device(input);
1470 input_free_device(input);
1474 ar->wps.pbc = input;
1477 #endif /* CONFIG_CARL9170_WPC */
1479 #ifdef CONFIG_CARL9170_HWRNG
1480 static int carl9170_rng_get(struct ar9170 *ar)
1483 #define RW (CARL9170_MAX_CMD_PAYLOAD_LEN / sizeof(u32))
1484 #define RB (CARL9170_MAX_CMD_PAYLOAD_LEN)
1486 static const __le32 rng_load[RW] = {
1487 [0 ... (RW - 1)] = cpu_to_le32(AR9170_RAND_REG_NUM)};
1491 unsigned int i, off = 0, transfer, count;
1494 BUILD_BUG_ON(RB > CARL9170_MAX_CMD_PAYLOAD_LEN);
1496 if (!IS_ACCEPTING_CMD(ar) || !ar->rng.initialized)
1499 count = ARRAY_SIZE(ar->rng.cache);
1501 err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1502 RB, (u8 *) rng_load,
1507 transfer = min_t(unsigned int, count, RW);
1508 for (i = 0; i < transfer; i++)
1509 ar->rng.cache[off + i] = buf[i];
1515 ar->rng.cache_idx = 0;
1522 static int carl9170_rng_read(struct hwrng *rng, u32 *data)
1524 struct ar9170 *ar = (struct ar9170 *)rng->priv;
1527 mutex_lock(&ar->mutex);
1528 if (ar->rng.cache_idx >= ARRAY_SIZE(ar->rng.cache)) {
1529 ret = carl9170_rng_get(ar);
1531 mutex_unlock(&ar->mutex);
1536 *data = ar->rng.cache[ar->rng.cache_idx++];
1537 mutex_unlock(&ar->mutex);
1542 static void carl9170_unregister_hwrng(struct ar9170 *ar)
1544 if (ar->rng.initialized) {
1545 hwrng_unregister(&ar->rng.rng);
1546 ar->rng.initialized = false;
1550 static int carl9170_register_hwrng(struct ar9170 *ar)
1554 snprintf(ar->rng.name, ARRAY_SIZE(ar->rng.name),
1555 "%s_%s", KBUILD_MODNAME, wiphy_name(ar->hw->wiphy));
1556 ar->rng.rng.name = ar->rng.name;
1557 ar->rng.rng.data_read = carl9170_rng_read;
1558 ar->rng.rng.priv = (unsigned long)ar;
1560 if (WARN_ON(ar->rng.initialized))
1563 err = hwrng_register(&ar->rng.rng);
1565 dev_err(&ar->udev->dev, "Failed to register the random "
1566 "number generator (%d)\n", err);
1570 ar->rng.initialized = true;
1572 err = carl9170_rng_get(ar);
1574 carl9170_unregister_hwrng(ar);
1580 #endif /* CONFIG_CARL9170_HWRNG */
1582 static int carl9170_op_get_survey(struct ieee80211_hw *hw, int idx,
1583 struct survey_info *survey)
1585 struct ar9170 *ar = hw->priv;
1586 struct ieee80211_channel *chan;
1587 struct ieee80211_supported_band *band;
1594 if (idx == chan->hw_value) {
1595 mutex_lock(&ar->mutex);
1596 err = carl9170_update_survey(ar, false, true);
1597 mutex_unlock(&ar->mutex);
1602 for (b = 0; b < IEEE80211_NUM_BANDS; b++) {
1603 band = ar->hw->wiphy->bands[b];
1608 for (i = 0; i < band->n_channels; i++) {
1609 if (band->channels[i].hw_value == idx) {
1610 chan = &band->channels[i];
1618 memcpy(survey, &ar->survey[idx], sizeof(*survey));
1620 survey->channel = chan;
1621 survey->filled = SURVEY_INFO_NOISE_DBM;
1623 if (ar->channel == chan)
1624 survey->filled |= SURVEY_INFO_IN_USE;
1626 if (ar->fw.hw_counters) {
1627 survey->filled |= SURVEY_INFO_CHANNEL_TIME |
1628 SURVEY_INFO_CHANNEL_TIME_BUSY |
1629 SURVEY_INFO_CHANNEL_TIME_TX;
1635 static void carl9170_op_flush(struct ieee80211_hw *hw, bool drop)
1637 struct ar9170 *ar = hw->priv;
1640 mutex_lock(&ar->mutex);
1641 for_each_set_bit(vid, &ar->vif_bitmap, ar->fw.vif_num)
1642 carl9170_flush_cab(ar, vid);
1644 carl9170_flush(ar, drop);
1645 mutex_unlock(&ar->mutex);
1648 static int carl9170_op_get_stats(struct ieee80211_hw *hw,
1649 struct ieee80211_low_level_stats *stats)
1651 struct ar9170 *ar = hw->priv;
1653 memset(stats, 0, sizeof(*stats));
1654 stats->dot11ACKFailureCount = ar->tx_ack_failures;
1655 stats->dot11FCSErrorCount = ar->tx_fcs_errors;
1659 static void carl9170_op_sta_notify(struct ieee80211_hw *hw,
1660 struct ieee80211_vif *vif,
1661 enum sta_notify_cmd cmd,
1662 struct ieee80211_sta *sta)
1664 struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1667 case STA_NOTIFY_SLEEP:
1668 sta_info->sleeping = true;
1669 if (atomic_read(&sta_info->pending_frames))
1670 ieee80211_sta_block_awake(hw, sta, true);
1673 case STA_NOTIFY_AWAKE:
1674 sta_info->sleeping = false;
1679 static bool carl9170_tx_frames_pending(struct ieee80211_hw *hw)
1681 struct ar9170 *ar = hw->priv;
1683 return !!atomic_read(&ar->tx_total_queued);
1686 static const struct ieee80211_ops carl9170_ops = {
1687 .start = carl9170_op_start,
1688 .stop = carl9170_op_stop,
1689 .tx = carl9170_op_tx,
1690 .flush = carl9170_op_flush,
1691 .add_interface = carl9170_op_add_interface,
1692 .remove_interface = carl9170_op_remove_interface,
1693 .config = carl9170_op_config,
1694 .prepare_multicast = carl9170_op_prepare_multicast,
1695 .configure_filter = carl9170_op_configure_filter,
1696 .conf_tx = carl9170_op_conf_tx,
1697 .bss_info_changed = carl9170_op_bss_info_changed,
1698 .get_tsf = carl9170_op_get_tsf,
1699 .set_key = carl9170_op_set_key,
1700 .sta_add = carl9170_op_sta_add,
1701 .sta_remove = carl9170_op_sta_remove,
1702 .sta_notify = carl9170_op_sta_notify,
1703 .get_survey = carl9170_op_get_survey,
1704 .get_stats = carl9170_op_get_stats,
1705 .ampdu_action = carl9170_op_ampdu_action,
1706 .tx_frames_pending = carl9170_tx_frames_pending,
1709 void *carl9170_alloc(size_t priv_size)
1711 struct ieee80211_hw *hw;
1713 struct sk_buff *skb;
1717 * this buffer is used for rx stream reconstruction.
1718 * Under heavy load this device (or the transport layer?)
1719 * tends to split the streams into separate rx descriptors.
1722 skb = __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE, GFP_KERNEL);
1726 hw = ieee80211_alloc_hw(priv_size, &carl9170_ops);
1732 ar->rx_failover = skb;
1734 memset(&ar->rx_plcp, 0, sizeof(struct ar9170_rx_head));
1735 ar->rx_has_plcp = false;
1738 * Here's a hidden pitfall!
1740 * All 4 AC queues work perfectly well under _legacy_ operation.
1741 * However as soon as aggregation is enabled, the traffic flow
1742 * gets very bumpy. Therefore we have to _switch_ to a
1743 * software AC with a single HW queue.
1745 hw->queues = __AR9170_NUM_TXQ;
1747 mutex_init(&ar->mutex);
1748 spin_lock_init(&ar->beacon_lock);
1749 spin_lock_init(&ar->cmd_lock);
1750 spin_lock_init(&ar->tx_stats_lock);
1751 spin_lock_init(&ar->tx_ampdu_list_lock);
1752 spin_lock_init(&ar->mem_lock);
1753 spin_lock_init(&ar->state_lock);
1754 atomic_set(&ar->pending_restarts, 0);
1756 for (i = 0; i < ar->hw->queues; i++) {
1757 skb_queue_head_init(&ar->tx_status[i]);
1758 skb_queue_head_init(&ar->tx_pending[i]);
1760 INIT_LIST_HEAD(&ar->bar_list[i]);
1761 spin_lock_init(&ar->bar_list_lock[i]);
1763 INIT_WORK(&ar->ps_work, carl9170_ps_work);
1764 INIT_WORK(&ar->ping_work, carl9170_ping_work);
1765 INIT_WORK(&ar->restart_work, carl9170_restart_work);
1766 INIT_WORK(&ar->ampdu_work, carl9170_ampdu_work);
1767 INIT_DELAYED_WORK(&ar->stat_work, carl9170_stat_work);
1768 INIT_DELAYED_WORK(&ar->tx_janitor, carl9170_tx_janitor);
1769 INIT_LIST_HEAD(&ar->tx_ampdu_list);
1770 rcu_assign_pointer(ar->tx_ampdu_iter,
1771 (struct carl9170_sta_tid *) &ar->tx_ampdu_list);
1773 bitmap_zero(&ar->vif_bitmap, ar->fw.vif_num);
1774 INIT_LIST_HEAD(&ar->vif_list);
1775 init_completion(&ar->tx_flush);
1777 /* firmware decides which modes we support */
1778 hw->wiphy->interface_modes = 0;
1780 hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
1781 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
1782 IEEE80211_HW_SUPPORTS_PS |
1783 IEEE80211_HW_PS_NULLFUNC_STACK |
1784 IEEE80211_HW_NEED_DTIM_PERIOD |
1785 IEEE80211_HW_SIGNAL_DBM;
1787 if (!modparam_noht) {
1789 * see the comment above, why we allow the user
1790 * to disable HT by a module parameter.
1792 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
1795 hw->extra_tx_headroom = sizeof(struct _carl9170_tx_superframe);
1796 hw->sta_data_size = sizeof(struct carl9170_sta_info);
1797 hw->vif_data_size = sizeof(struct carl9170_vif_info);
1799 hw->max_rates = CARL9170_TX_MAX_RATES;
1800 hw->max_rate_tries = CARL9170_TX_USER_RATE_TRIES;
1802 for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
1803 ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
1805 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
1807 /* As IBSS Encryption is software-based, IBSS RSN is supported. */
1808 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
1813 return ERR_PTR(-ENOMEM);
1816 static int carl9170_read_eeprom(struct ar9170 *ar)
1818 #define RW 8 /* number of words to read at once */
1819 #define RB (sizeof(u32) * RW)
1820 u8 *eeprom = (void *)&ar->eeprom;
1824 BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
1826 BUILD_BUG_ON(RB > CARL9170_MAX_CMD_LEN - 4);
1828 /* don't want to handle trailing remains */
1829 BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
1832 for (i = 0; i < sizeof(ar->eeprom) / RB; i++) {
1833 for (j = 0; j < RW; j++)
1834 offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
1837 err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1838 RB, (u8 *) &offsets,
1839 RB, eeprom + RB * i);
1849 static int carl9170_parse_eeprom(struct ar9170 *ar)
1851 struct ath_regulatory *regulatory = &ar->common.regulatory;
1852 unsigned int rx_streams, tx_streams, tx_params = 0;
1856 if (ar->eeprom.length == cpu_to_le16(0xffff))
1859 rx_streams = hweight8(ar->eeprom.rx_mask);
1860 tx_streams = hweight8(ar->eeprom.tx_mask);
1862 if (rx_streams != tx_streams) {
1863 tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
1865 WARN_ON(!(tx_streams >= 1 && tx_streams <=
1866 IEEE80211_HT_MCS_TX_MAX_STREAMS));
1868 tx_params = (tx_streams - 1) <<
1869 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1871 carl9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
1872 carl9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
1875 if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
1876 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
1877 &carl9170_band_2GHz;
1878 chans += carl9170_band_2GHz.n_channels;
1881 if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
1882 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
1883 &carl9170_band_5GHz;
1884 chans += carl9170_band_5GHz.n_channels;
1891 ar->survey = kzalloc(sizeof(struct survey_info) * chans, GFP_KERNEL);
1894 ar->num_channels = chans;
1897 * I measured this, a bandswitch takes roughly
1898 * 135 ms and a frequency switch about 80.
1900 * FIXME: measure these values again once EEPROM settings
1901 * are used, that will influence them!
1904 ar->hw->channel_change_time = 135 * 1000;
1906 ar->hw->channel_change_time = 80 * 1000;
1908 regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
1910 /* second part of wiphy init */
1911 SET_IEEE80211_PERM_ADDR(ar->hw, ar->eeprom.mac_address);
1916 static int carl9170_reg_notifier(struct wiphy *wiphy,
1917 struct regulatory_request *request)
1919 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1920 struct ar9170 *ar = hw->priv;
1922 return ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
1925 int carl9170_register(struct ar9170 *ar)
1927 struct ath_regulatory *regulatory = &ar->common.regulatory;
1930 if (WARN_ON(ar->mem_bitmap))
1933 ar->mem_bitmap = kzalloc(roundup(ar->fw.mem_blocks, BITS_PER_LONG) *
1934 sizeof(unsigned long), GFP_KERNEL);
1936 if (!ar->mem_bitmap)
1939 /* try to read EEPROM, init MAC addr */
1940 err = carl9170_read_eeprom(ar);
1944 err = carl9170_parse_eeprom(ar);
1948 err = ath_regd_init(regulatory, ar->hw->wiphy,
1949 carl9170_reg_notifier);
1953 if (modparam_noht) {
1954 carl9170_band_2GHz.ht_cap.ht_supported = false;
1955 carl9170_band_5GHz.ht_cap.ht_supported = false;
1958 for (i = 0; i < ar->fw.vif_num; i++) {
1959 ar->vif_priv[i].id = i;
1960 ar->vif_priv[i].vif = NULL;
1963 err = ieee80211_register_hw(ar->hw);
1967 /* mac80211 interface is now registered */
1968 ar->registered = true;
1970 if (!ath_is_world_regd(regulatory))
1971 regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
1973 #ifdef CONFIG_CARL9170_DEBUGFS
1974 carl9170_debugfs_register(ar);
1975 #endif /* CONFIG_CARL9170_DEBUGFS */
1977 err = carl9170_led_init(ar);
1981 #ifdef CONFIG_CARL9170_LEDS
1982 err = carl9170_led_register(ar);
1985 #endif /* CONFIG_CARL9170_LEDS */
1987 #ifdef CONFIG_CARL9170_WPC
1988 err = carl9170_register_wps_button(ar);
1991 #endif /* CONFIG_CARL9170_WPC */
1993 #ifdef CONFIG_CARL9170_HWRNG
1994 err = carl9170_register_hwrng(ar);
1997 #endif /* CONFIG_CARL9170_HWRNG */
1999 dev_info(&ar->udev->dev, "Atheros AR9170 is registered as '%s'\n",
2000 wiphy_name(ar->hw->wiphy));
2005 carl9170_unregister(ar);
2009 void carl9170_unregister(struct ar9170 *ar)
2011 if (!ar->registered)
2014 ar->registered = false;
2016 #ifdef CONFIG_CARL9170_LEDS
2017 carl9170_led_unregister(ar);
2018 #endif /* CONFIG_CARL9170_LEDS */
2020 #ifdef CONFIG_CARL9170_DEBUGFS
2021 carl9170_debugfs_unregister(ar);
2022 #endif /* CONFIG_CARL9170_DEBUGFS */
2024 #ifdef CONFIG_CARL9170_WPC
2026 input_unregister_device(ar->wps.pbc);
2029 #endif /* CONFIG_CARL9170_WPC */
2031 #ifdef CONFIG_CARL9170_HWRNG
2032 carl9170_unregister_hwrng(ar);
2033 #endif /* CONFIG_CARL9170_HWRNG */
2035 carl9170_cancel_worker(ar);
2036 cancel_work_sync(&ar->restart_work);
2038 ieee80211_unregister_hw(ar->hw);
2041 void carl9170_free(struct ar9170 *ar)
2043 WARN_ON(ar->registered);
2044 WARN_ON(IS_INITIALIZED(ar));
2046 kfree_skb(ar->rx_failover);
2047 ar->rx_failover = NULL;
2049 kfree(ar->mem_bitmap);
2050 ar->mem_bitmap = NULL;
2055 mutex_destroy(&ar->mutex);
2057 ieee80211_free_hw(ar->hw);