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/slab.h>
41 #include <linux/module.h>
42 #include <linux/etherdevice.h>
43 #include <linux/random.h>
44 #include <net/mac80211.h>
45 #include <net/cfg80211.h>
50 static bool modparam_nohwcrypt
;
51 module_param_named(nohwcrypt
, modparam_nohwcrypt
, bool, 0444);
52 MODULE_PARM_DESC(nohwcrypt
, "Disable hardware crypto offload.");
55 module_param_named(noht
, modparam_noht
, int, 0444);
56 MODULE_PARM_DESC(noht
, "Disable MPDU aggregation.");
58 #define RATE(_bitrate, _hw_rate, _txpidx, _flags) { \
59 .bitrate = (_bitrate), \
61 .hw_value = (_hw_rate) | (_txpidx) << 4, \
64 struct ieee80211_rate __carl9170_ratetable
[] = {
66 RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE
),
67 RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE
),
68 RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE
),
80 #define carl9170_g_ratetable (__carl9170_ratetable + 0)
81 #define carl9170_g_ratetable_size 12
82 #define carl9170_a_ratetable (__carl9170_ratetable + 4)
83 #define carl9170_a_ratetable_size 8
86 * NB: The hw_value is used as an index into the carl9170_phy_freq_params
87 * array in phy.c so that we don't have to do frequency lookups!
89 #define CHAN(_freq, _idx) { \
90 .center_freq = (_freq), \
92 .max_power = 18, /* XXX */ \
95 static struct ieee80211_channel carl9170_2ghz_chantable
[] = {
112 static struct ieee80211_channel carl9170_5ghz_chantable
[] = {
151 #define CARL9170_HT_CAP \
153 .ht_supported = true, \
154 .cap = IEEE80211_HT_CAP_MAX_AMSDU | \
155 IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
156 IEEE80211_HT_CAP_SGI_40 | \
157 IEEE80211_HT_CAP_DSSSCCK40 | \
158 IEEE80211_HT_CAP_SM_PS, \
159 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, \
160 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, \
162 .rx_mask = { 0xff, 0xff, 0, 0, 0x1, 0, 0, 0, 0, 0, }, \
163 .rx_highest = cpu_to_le16(300), \
164 .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
168 static struct ieee80211_supported_band carl9170_band_2GHz
= {
169 .channels
= carl9170_2ghz_chantable
,
170 .n_channels
= ARRAY_SIZE(carl9170_2ghz_chantable
),
171 .bitrates
= carl9170_g_ratetable
,
172 .n_bitrates
= carl9170_g_ratetable_size
,
173 .ht_cap
= CARL9170_HT_CAP
,
176 static struct ieee80211_supported_band carl9170_band_5GHz
= {
177 .channels
= carl9170_5ghz_chantable
,
178 .n_channels
= ARRAY_SIZE(carl9170_5ghz_chantable
),
179 .bitrates
= carl9170_a_ratetable
,
180 .n_bitrates
= carl9170_a_ratetable_size
,
181 .ht_cap
= CARL9170_HT_CAP
,
184 static void carl9170_ampdu_gc(struct ar9170
*ar
)
186 struct carl9170_sta_tid
*tid_info
;
190 list_for_each_entry_rcu(tid_info
, &ar
->tx_ampdu_list
, list
) {
191 spin_lock_bh(&ar
->tx_ampdu_list_lock
);
192 if (tid_info
->state
== CARL9170_TID_STATE_SHUTDOWN
) {
193 tid_info
->state
= CARL9170_TID_STATE_KILLED
;
194 list_del_rcu(&tid_info
->list
);
195 ar
->tx_ampdu_list_len
--;
196 list_add_tail(&tid_info
->tmp_list
, &tid_gc
);
198 spin_unlock_bh(&ar
->tx_ampdu_list_lock
);
201 rcu_assign_pointer(ar
->tx_ampdu_iter
, tid_info
);
206 while (!list_empty(&tid_gc
)) {
208 tid_info
= list_first_entry(&tid_gc
, struct carl9170_sta_tid
,
211 while ((skb
= __skb_dequeue(&tid_info
->queue
)))
212 carl9170_tx_status(ar
, skb
, false);
214 list_del_init(&tid_info
->tmp_list
);
219 static void carl9170_flush(struct ar9170
*ar
, bool drop_queued
)
225 * We can only drop frames which have not been uploaded
229 for (i
= 0; i
< ar
->hw
->queues
; i
++) {
232 while ((skb
= skb_dequeue(&ar
->tx_pending
[i
]))) {
233 struct ieee80211_tx_info
*info
;
235 info
= IEEE80211_SKB_CB(skb
);
236 if (info
->flags
& IEEE80211_TX_CTL_AMPDU
)
237 atomic_dec(&ar
->tx_ampdu_upload
);
239 carl9170_tx_status(ar
, skb
, false);
244 /* Wait for all other outstanding frames to timeout. */
245 if (atomic_read(&ar
->tx_total_queued
))
246 WARN_ON(wait_for_completion_timeout(&ar
->tx_flush
, HZ
) == 0);
249 static void carl9170_flush_ba(struct ar9170
*ar
)
251 struct sk_buff_head free
;
252 struct carl9170_sta_tid
*tid_info
;
255 __skb_queue_head_init(&free
);
258 spin_lock_bh(&ar
->tx_ampdu_list_lock
);
259 list_for_each_entry_rcu(tid_info
, &ar
->tx_ampdu_list
, list
) {
260 if (tid_info
->state
> CARL9170_TID_STATE_SUSPEND
) {
261 tid_info
->state
= CARL9170_TID_STATE_SUSPEND
;
263 spin_lock(&tid_info
->lock
);
264 while ((skb
= __skb_dequeue(&tid_info
->queue
)))
265 __skb_queue_tail(&free
, skb
);
266 spin_unlock(&tid_info
->lock
);
269 spin_unlock_bh(&ar
->tx_ampdu_list_lock
);
272 while ((skb
= __skb_dequeue(&free
)))
273 carl9170_tx_status(ar
, skb
, false);
276 static void carl9170_zap_queues(struct ar9170
*ar
)
278 struct carl9170_vif_info
*cvif
;
281 carl9170_ampdu_gc(ar
);
283 carl9170_flush_ba(ar
);
284 carl9170_flush(ar
, true);
286 for (i
= 0; i
< ar
->hw
->queues
; i
++) {
287 spin_lock_bh(&ar
->tx_status
[i
].lock
);
288 while (!skb_queue_empty(&ar
->tx_status
[i
])) {
291 skb
= skb_peek(&ar
->tx_status
[i
]);
292 carl9170_tx_get_skb(skb
);
293 spin_unlock_bh(&ar
->tx_status
[i
].lock
);
294 carl9170_tx_drop(ar
, skb
);
295 spin_lock_bh(&ar
->tx_status
[i
].lock
);
296 carl9170_tx_put_skb(skb
);
298 spin_unlock_bh(&ar
->tx_status
[i
].lock
);
301 BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_SOFT
< 1);
302 BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_HARD
< CARL9170_NUM_TX_LIMIT_SOFT
);
303 BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_HARD
>= CARL9170_BAW_BITS
);
305 /* reinitialize queues statistics */
306 memset(&ar
->tx_stats
, 0, sizeof(ar
->tx_stats
));
307 for (i
= 0; i
< ar
->hw
->queues
; i
++)
308 ar
->tx_stats
[i
].limit
= CARL9170_NUM_TX_LIMIT_HARD
;
310 bitmap_zero(ar
->mem_bitmap
, ar
->fw
.mem_blocks
);
313 list_for_each_entry_rcu(cvif
, &ar
->vif_list
, list
) {
314 spin_lock_bh(&ar
->beacon_lock
);
315 dev_kfree_skb_any(cvif
->beacon
);
317 spin_unlock_bh(&ar
->beacon_lock
);
321 atomic_set(&ar
->tx_ampdu_upload
, 0);
322 atomic_set(&ar
->tx_ampdu_scheduler
, 0);
323 atomic_set(&ar
->tx_total_pending
, 0);
324 atomic_set(&ar
->tx_total_queued
, 0);
325 atomic_set(&ar
->mem_free_blocks
, ar
->fw
.mem_blocks
);
328 #define CARL9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
330 queue.aifs = ai_fs; \
331 queue.cw_min = cwmin; \
332 queue.cw_max = cwmax; \
333 queue.txop = _txop; \
336 static int carl9170_op_start(struct ieee80211_hw
*hw
)
338 struct ar9170
*ar
= hw
->priv
;
341 mutex_lock(&ar
->mutex
);
343 carl9170_zap_queues(ar
);
345 /* reset QoS defaults */
346 CARL9170_FILL_QUEUE(ar
->edcf
[AR9170_TXQ_VO
], 2, 3, 7, 47);
347 CARL9170_FILL_QUEUE(ar
->edcf
[AR9170_TXQ_VI
], 2, 7, 15, 94);
348 CARL9170_FILL_QUEUE(ar
->edcf
[AR9170_TXQ_BE
], 3, 15, 1023, 0);
349 CARL9170_FILL_QUEUE(ar
->edcf
[AR9170_TXQ_BK
], 7, 15, 1023, 0);
350 CARL9170_FILL_QUEUE(ar
->edcf
[AR9170_TXQ_SPECIAL
], 2, 3, 7, 0);
352 ar
->current_factor
= ar
->current_density
= -1;
353 /* "The first key is unique." */
355 ar
->filter_state
= 0;
356 ar
->ps
.last_action
= jiffies
;
357 ar
->ps
.last_slept
= jiffies
;
358 ar
->erp_mode
= CARL9170_ERP_AUTO
;
360 /* Set "disable hw crypto offload" whenever the module parameter
361 * nohwcrypt is true or if the firmware does not support it.
363 ar
->disable_offload
= modparam_nohwcrypt
|
364 ar
->fw
.disable_offload_fw
;
365 ar
->rx_software_decryption
= ar
->disable_offload
;
367 for (i
= 0; i
< ar
->hw
->queues
; i
++) {
368 ar
->queue_stop_timeout
[i
] = jiffies
;
369 ar
->max_queue_stop_timeout
[i
] = 0;
372 atomic_set(&ar
->mem_allocs
, 0);
374 err
= carl9170_usb_open(ar
);
378 err
= carl9170_init_mac(ar
);
382 err
= carl9170_set_qos(ar
);
386 if (ar
->fw
.rx_filter
) {
387 err
= carl9170_rx_filter(ar
, CARL9170_RX_FILTER_OTHER_RA
|
388 CARL9170_RX_FILTER_CTL_OTHER
| CARL9170_RX_FILTER_BAD
);
393 err
= carl9170_write_reg(ar
, AR9170_MAC_REG_DMA_TRIGGER
,
394 AR9170_DMA_TRIGGER_RXQ
);
398 /* Clear key-cache */
399 for (i
= 0; i
< AR9170_CAM_MAX_USER
+ 4; i
++) {
400 err
= carl9170_upload_key(ar
, i
, NULL
, AR9170_ENC_ALG_NONE
,
405 err
= carl9170_upload_key(ar
, i
, NULL
, AR9170_ENC_ALG_NONE
,
410 if (i
< AR9170_CAM_MAX_USER
) {
411 err
= carl9170_disable_key(ar
, i
);
417 carl9170_set_state_when(ar
, CARL9170_IDLE
, CARL9170_STARTED
);
419 ieee80211_queue_delayed_work(ar
->hw
, &ar
->stat_work
,
420 round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK
)));
422 ieee80211_wake_queues(ar
->hw
);
426 mutex_unlock(&ar
->mutex
);
430 static void carl9170_cancel_worker(struct ar9170
*ar
)
432 cancel_delayed_work_sync(&ar
->stat_work
);
433 cancel_delayed_work_sync(&ar
->tx_janitor
);
434 #ifdef CONFIG_CARL9170_LEDS
435 cancel_delayed_work_sync(&ar
->led_work
);
436 #endif /* CONFIG_CARL9170_LEDS */
437 cancel_work_sync(&ar
->ps_work
);
438 cancel_work_sync(&ar
->ping_work
);
439 cancel_work_sync(&ar
->ampdu_work
);
442 static void carl9170_op_stop(struct ieee80211_hw
*hw
, bool suspend
)
444 struct ar9170
*ar
= hw
->priv
;
446 carl9170_set_state_when(ar
, CARL9170_STARTED
, CARL9170_IDLE
);
448 ieee80211_stop_queues(ar
->hw
);
450 mutex_lock(&ar
->mutex
);
451 if (IS_ACCEPTING_CMD(ar
)) {
452 RCU_INIT_POINTER(ar
->beacon_iter
, NULL
);
454 carl9170_led_set_state(ar
, 0);
457 carl9170_write_reg(ar
, AR9170_MAC_REG_DMA_TRIGGER
, 0);
458 carl9170_usb_stop(ar
);
461 carl9170_zap_queues(ar
);
462 mutex_unlock(&ar
->mutex
);
464 carl9170_cancel_worker(ar
);
467 static void carl9170_restart_work(struct work_struct
*work
)
469 struct ar9170
*ar
= container_of(work
, struct ar9170
,
474 ar
->filter_state
= 0;
475 carl9170_cancel_worker(ar
);
477 mutex_lock(&ar
->mutex
);
478 if (!ar
->force_usb_reset
) {
479 err
= carl9170_usb_restart(ar
);
480 if (net_ratelimit()) {
482 dev_err(&ar
->udev
->dev
, "Failed to restart device (%d).\n", err
);
484 dev_info(&ar
->udev
->dev
, "device restarted successfully.\n");
487 carl9170_zap_queues(ar
);
488 mutex_unlock(&ar
->mutex
);
490 if (!err
&& !ar
->force_usb_reset
) {
491 ar
->restart_counter
++;
492 atomic_set(&ar
->pending_restarts
, 0);
494 ieee80211_restart_hw(ar
->hw
);
497 * The reset was unsuccessful and the device seems to
498 * be dead. But there's still one option: a low-level
499 * usb subsystem reset...
502 carl9170_usb_reset(ar
);
506 void carl9170_restart(struct ar9170
*ar
, const enum carl9170_restart_reasons r
)
508 carl9170_set_state_when(ar
, CARL9170_STARTED
, CARL9170_IDLE
);
511 * Sometimes, an error can trigger several different reset events.
512 * By ignoring these *surplus* reset events, the device won't be
513 * killed again, right after it has recovered.
515 if (atomic_inc_return(&ar
->pending_restarts
) > 1) {
516 dev_dbg(&ar
->udev
->dev
, "ignoring restart (%d)\n", r
);
520 ieee80211_stop_queues(ar
->hw
);
522 dev_err(&ar
->udev
->dev
, "restart device (%d)\n", r
);
524 if (!WARN_ON(r
== CARL9170_RR_NO_REASON
) ||
525 !WARN_ON(r
>= __CARL9170_RR_LAST
))
531 if (!IS_ACCEPTING_CMD(ar
) || ar
->needs_full_reset
)
532 ar
->force_usb_reset
= true;
534 ieee80211_queue_work(ar
->hw
, &ar
->restart_work
);
537 * At this point, the device instance might have vanished/disabled.
538 * So, don't put any code which access the ar9170 struct
539 * without proper protection.
543 static void carl9170_ping_work(struct work_struct
*work
)
545 struct ar9170
*ar
= container_of(work
, struct ar9170
, ping_work
);
551 mutex_lock(&ar
->mutex
);
552 err
= carl9170_echo_test(ar
, 0xdeadbeef);
554 carl9170_restart(ar
, CARL9170_RR_UNRESPONSIVE_DEVICE
);
555 mutex_unlock(&ar
->mutex
);
558 static int carl9170_init_interface(struct ar9170
*ar
,
559 struct ieee80211_vif
*vif
)
561 struct ath_common
*common
= &ar
->common
;
565 WARN_ON_ONCE(IS_STARTED(ar
));
569 memcpy(common
->macaddr
, vif
->addr
, ETH_ALEN
);
571 /* We have to fall back to software crypto, whenever
572 * the user choose to participates in an IBSS. HW
573 * offload for IBSS RSN is not supported by this driver.
575 * NOTE: If the previous main interface has already
576 * disabled hw crypto offload, we have to keep this
577 * previous disable_offload setting as it was.
578 * Altough ideally, we should notify mac80211 and tell
579 * it to forget about any HW crypto offload for now.
581 ar
->disable_offload
|= ((vif
->type
!= NL80211_IFTYPE_STATION
) &&
582 (vif
->type
!= NL80211_IFTYPE_AP
));
584 /* The driver used to have P2P GO+CLIENT support,
585 * but since this was dropped and we don't know if
586 * there are any gremlins lurking in the shadows,
587 * so best we keep HW offload disabled for P2P.
589 ar
->disable_offload
|= vif
->p2p
;
591 ar
->rx_software_decryption
= ar
->disable_offload
;
593 err
= carl9170_set_operating_mode(ar
);
597 static int carl9170_op_add_interface(struct ieee80211_hw
*hw
,
598 struct ieee80211_vif
*vif
)
600 struct carl9170_vif_info
*vif_priv
= (void *) vif
->drv_priv
;
601 struct ieee80211_vif
*main_vif
, *old_main
= NULL
;
602 struct ar9170
*ar
= hw
->priv
;
603 int vif_id
= -1, err
= 0;
605 mutex_lock(&ar
->mutex
);
607 if (vif_priv
->active
) {
609 * Skip the interface structure initialization,
610 * if the vif survived the _restart call.
612 vif_id
= vif_priv
->id
;
613 vif_priv
->enable_beacon
= false;
615 spin_lock_bh(&ar
->beacon_lock
);
616 dev_kfree_skb_any(vif_priv
->beacon
);
617 vif_priv
->beacon
= NULL
;
618 spin_unlock_bh(&ar
->beacon_lock
);
623 /* Because the AR9170 HW's MAC doesn't provide full support for
624 * multiple, independent interfaces [of different operation modes].
625 * We have to select ONE main interface [main mode of HW], but we
626 * can have multiple slaves [AKA: entry in the ACK-table].
628 * The first (from HEAD/TOP) interface in the ar->vif_list is
629 * always the main intf. All following intfs in this list
630 * are considered to be slave intfs.
632 main_vif
= carl9170_get_main_vif(ar
);
635 switch (main_vif
->type
) {
636 case NL80211_IFTYPE_STATION
:
637 if (vif
->type
== NL80211_IFTYPE_STATION
)
645 case NL80211_IFTYPE_MESH_POINT
:
646 case NL80211_IFTYPE_AP
:
647 if ((vif
->type
== NL80211_IFTYPE_STATION
) ||
648 (vif
->type
== NL80211_IFTYPE_AP
) ||
649 (vif
->type
== NL80211_IFTYPE_MESH_POINT
))
662 vif_id
= bitmap_find_free_region(&ar
->vif_bitmap
, ar
->fw
.vif_num
, 0);
671 BUG_ON(ar
->vif_priv
[vif_id
].id
!= vif_id
);
673 vif_priv
->active
= true;
674 vif_priv
->id
= vif_id
;
675 vif_priv
->enable_beacon
= false;
678 /* We end up in here, if the main interface is being replaced.
679 * Put the new main interface at the HEAD of the list and the
680 * previous inteface will automatically become second in line.
682 list_add_rcu(&vif_priv
->list
, &ar
->vif_list
);
684 /* Add new inteface. If the list is empty, it will become the
685 * main inteface, otherwise it will be slave.
687 list_add_tail_rcu(&vif_priv
->list
, &ar
->vif_list
);
689 rcu_assign_pointer(ar
->vif_priv
[vif_id
].vif
, vif
);
692 main_vif
= carl9170_get_main_vif(ar
);
694 if (main_vif
== vif
) {
695 rcu_assign_pointer(ar
->beacon_iter
, vif_priv
);
699 struct carl9170_vif_info
*old_main_priv
=
700 (void *) old_main
->drv_priv
;
701 /* downgrade old main intf to slave intf.
702 * NOTE: We are no longer under rcu_read_lock.
703 * But we are still holding ar->mutex, so the
704 * vif data [id, addr] is safe.
706 err
= carl9170_mod_virtual_mac(ar
, old_main_priv
->id
,
712 err
= carl9170_init_interface(ar
, vif
);
717 err
= carl9170_mod_virtual_mac(ar
, vif_id
, vif
->addr
);
723 if (ar
->fw
.tx_seq_table
) {
724 err
= carl9170_write_reg(ar
, ar
->fw
.tx_seq_table
+ vif_id
* 4,
731 if (err
&& (vif_id
>= 0)) {
732 vif_priv
->active
= false;
733 bitmap_release_region(&ar
->vif_bitmap
, vif_id
, 0);
735 RCU_INIT_POINTER(ar
->vif_priv
[vif_id
].vif
, NULL
);
736 list_del_rcu(&vif_priv
->list
);
737 mutex_unlock(&ar
->mutex
);
741 ar
->ps
.off_override
|= PS_OFF_VIF
;
743 mutex_unlock(&ar
->mutex
);
749 static void carl9170_op_remove_interface(struct ieee80211_hw
*hw
,
750 struct ieee80211_vif
*vif
)
752 struct carl9170_vif_info
*vif_priv
= (void *) vif
->drv_priv
;
753 struct ieee80211_vif
*main_vif
;
754 struct ar9170
*ar
= hw
->priv
;
757 mutex_lock(&ar
->mutex
);
759 if (WARN_ON_ONCE(!vif_priv
->active
))
765 main_vif
= carl9170_get_main_vif(ar
);
769 vif_priv
->active
= false;
770 WARN_ON(vif_priv
->enable_beacon
);
771 vif_priv
->enable_beacon
= false;
772 list_del_rcu(&vif_priv
->list
);
773 RCU_INIT_POINTER(ar
->vif_priv
[id
].vif
, NULL
);
775 if (vif
== main_vif
) {
779 WARN_ON(carl9170_init_interface(ar
,
780 carl9170_get_main_vif(ar
)));
782 carl9170_set_operating_mode(ar
);
787 WARN_ON(carl9170_mod_virtual_mac(ar
, id
, NULL
));
790 carl9170_update_beacon(ar
, false);
791 carl9170_flush_cab(ar
, id
);
793 spin_lock_bh(&ar
->beacon_lock
);
794 dev_kfree_skb_any(vif_priv
->beacon
);
795 vif_priv
->beacon
= NULL
;
796 spin_unlock_bh(&ar
->beacon_lock
);
798 bitmap_release_region(&ar
->vif_bitmap
, id
, 0);
800 carl9170_set_beacon_timers(ar
);
803 ar
->ps
.off_override
&= ~PS_OFF_VIF
;
806 mutex_unlock(&ar
->mutex
);
811 void carl9170_ps_check(struct ar9170
*ar
)
813 ieee80211_queue_work(ar
->hw
, &ar
->ps_work
);
816 /* caller must hold ar->mutex */
817 static int carl9170_ps_update(struct ar9170
*ar
)
822 if (!ar
->ps
.off_override
)
823 ps
= (ar
->hw
->conf
.flags
& IEEE80211_CONF_PS
);
825 if (ps
!= ar
->ps
.state
) {
826 err
= carl9170_powersave(ar
, ps
);
830 if (ar
->ps
.state
&& !ps
) {
831 ar
->ps
.sleep_ms
= jiffies_to_msecs(jiffies
-
836 ar
->ps
.last_slept
= jiffies
;
838 ar
->ps
.last_action
= jiffies
;
845 static void carl9170_ps_work(struct work_struct
*work
)
847 struct ar9170
*ar
= container_of(work
, struct ar9170
,
849 mutex_lock(&ar
->mutex
);
851 WARN_ON_ONCE(carl9170_ps_update(ar
) != 0);
852 mutex_unlock(&ar
->mutex
);
855 static int carl9170_update_survey(struct ar9170
*ar
, bool flush
, bool noise
)
860 err
= carl9170_get_noisefloor(ar
);
865 if (ar
->fw
.hw_counters
) {
866 err
= carl9170_collect_tally(ar
);
872 memset(&ar
->tally
, 0, sizeof(ar
->tally
));
877 static void carl9170_stat_work(struct work_struct
*work
)
879 struct ar9170
*ar
= container_of(work
, struct ar9170
, stat_work
.work
);
882 mutex_lock(&ar
->mutex
);
883 err
= carl9170_update_survey(ar
, false, true);
884 mutex_unlock(&ar
->mutex
);
889 ieee80211_queue_delayed_work(ar
->hw
, &ar
->stat_work
,
890 round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK
)));
893 static int carl9170_op_config(struct ieee80211_hw
*hw
, u32 changed
)
895 struct ar9170
*ar
= hw
->priv
;
898 mutex_lock(&ar
->mutex
);
899 if (changed
& IEEE80211_CONF_CHANGE_LISTEN_INTERVAL
) {
904 if (changed
& IEEE80211_CONF_CHANGE_PS
) {
905 err
= carl9170_ps_update(ar
);
910 if (changed
& IEEE80211_CONF_CHANGE_SMPS
) {
915 if (changed
& IEEE80211_CONF_CHANGE_CHANNEL
) {
916 enum nl80211_channel_type channel_type
=
917 cfg80211_get_chandef_type(&hw
->conf
.chandef
);
919 /* adjust slot time for 5 GHz */
920 err
= carl9170_set_slot_time(ar
);
924 err
= carl9170_update_survey(ar
, true, false);
928 err
= carl9170_set_channel(ar
, hw
->conf
.chandef
.chan
,
933 err
= carl9170_update_survey(ar
, false, true);
937 err
= carl9170_set_dyn_sifs_ack(ar
);
941 err
= carl9170_set_rts_cts_rate(ar
);
946 if (changed
& IEEE80211_CONF_CHANGE_POWER
) {
947 err
= carl9170_set_mac_tpc(ar
, ar
->hw
->conf
.chandef
.chan
);
953 mutex_unlock(&ar
->mutex
);
957 static u64
carl9170_op_prepare_multicast(struct ieee80211_hw
*hw
,
958 struct netdev_hw_addr_list
*mc_list
)
960 struct netdev_hw_addr
*ha
;
963 /* always get broadcast frames */
964 mchash
= 1ULL << (0xff >> 2);
966 netdev_hw_addr_list_for_each(ha
, mc_list
)
967 mchash
|= 1ULL << (ha
->addr
[5] >> 2);
972 static void carl9170_op_configure_filter(struct ieee80211_hw
*hw
,
973 unsigned int changed_flags
,
974 unsigned int *new_flags
,
977 struct ar9170
*ar
= hw
->priv
;
979 /* mask supported flags */
980 *new_flags
&= FIF_ALLMULTI
| ar
->rx_filter_caps
;
982 if (!IS_ACCEPTING_CMD(ar
))
985 mutex_lock(&ar
->mutex
);
987 ar
->filter_state
= *new_flags
;
989 * We can support more by setting the sniffer bit and
990 * then checking the error flags, later.
993 if (*new_flags
& FIF_ALLMULTI
)
996 if (multicast
!= ar
->cur_mc_hash
)
997 WARN_ON(carl9170_update_multicast(ar
, multicast
));
999 if (changed_flags
& FIF_OTHER_BSS
) {
1000 ar
->sniffer_enabled
= !!(*new_flags
& FIF_OTHER_BSS
);
1002 WARN_ON(carl9170_set_operating_mode(ar
));
1005 if (ar
->fw
.rx_filter
&& changed_flags
& ar
->rx_filter_caps
) {
1008 if (!ar
->fw
.ba_filter
)
1009 rx_filter
|= CARL9170_RX_FILTER_CTL_OTHER
;
1011 if (!(*new_flags
& (FIF_FCSFAIL
| FIF_PLCPFAIL
)))
1012 rx_filter
|= CARL9170_RX_FILTER_BAD
;
1014 if (!(*new_flags
& FIF_CONTROL
))
1015 rx_filter
|= CARL9170_RX_FILTER_CTL_OTHER
;
1017 if (!(*new_flags
& FIF_PSPOLL
))
1018 rx_filter
|= CARL9170_RX_FILTER_CTL_PSPOLL
;
1020 if (!(*new_flags
& FIF_OTHER_BSS
)) {
1021 rx_filter
|= CARL9170_RX_FILTER_OTHER_RA
;
1022 rx_filter
|= CARL9170_RX_FILTER_DECRY_FAIL
;
1025 WARN_ON(carl9170_rx_filter(ar
, rx_filter
));
1028 mutex_unlock(&ar
->mutex
);
1032 static void carl9170_op_bss_info_changed(struct ieee80211_hw
*hw
,
1033 struct ieee80211_vif
*vif
,
1034 struct ieee80211_bss_conf
*bss_conf
,
1037 struct ar9170
*ar
= hw
->priv
;
1038 struct ath_common
*common
= &ar
->common
;
1040 struct carl9170_vif_info
*vif_priv
;
1041 struct ieee80211_vif
*main_vif
;
1043 mutex_lock(&ar
->mutex
);
1044 vif_priv
= (void *) vif
->drv_priv
;
1045 main_vif
= carl9170_get_main_vif(ar
);
1046 if (WARN_ON(!main_vif
))
1049 if (changed
& BSS_CHANGED_BEACON_ENABLED
) {
1050 struct carl9170_vif_info
*iter
;
1053 vif_priv
->enable_beacon
= bss_conf
->enable_beacon
;
1055 list_for_each_entry_rcu(iter
, &ar
->vif_list
, list
) {
1056 if (iter
->active
&& iter
->enable_beacon
)
1062 ar
->beacon_enabled
= i
;
1065 if (changed
& BSS_CHANGED_BEACON
) {
1066 err
= carl9170_update_beacon(ar
, false);
1071 if (changed
& (BSS_CHANGED_BEACON_ENABLED
| BSS_CHANGED_BEACON
|
1072 BSS_CHANGED_BEACON_INT
)) {
1074 if (main_vif
!= vif
) {
1075 bss_conf
->beacon_int
= main_vif
->bss_conf
.beacon_int
;
1076 bss_conf
->dtim_period
= main_vif
->bss_conf
.dtim_period
;
1080 * Therefore a hard limit for the broadcast traffic should
1081 * prevent false alarms.
1083 if (vif
->type
!= NL80211_IFTYPE_STATION
&&
1084 (bss_conf
->beacon_int
* bss_conf
->dtim_period
>=
1085 (CARL9170_QUEUE_STUCK_TIMEOUT
/ 2))) {
1090 err
= carl9170_set_beacon_timers(ar
);
1095 if (changed
& BSS_CHANGED_HT
) {
1102 if (main_vif
!= vif
)
1106 * The following settings can only be changed by the
1110 if (changed
& BSS_CHANGED_BSSID
) {
1111 memcpy(common
->curbssid
, bss_conf
->bssid
, ETH_ALEN
);
1112 err
= carl9170_set_operating_mode(ar
);
1117 if (changed
& BSS_CHANGED_ASSOC
) {
1118 ar
->common
.curaid
= vif
->cfg
.aid
;
1119 err
= carl9170_set_beacon_timers(ar
);
1124 if (changed
& BSS_CHANGED_ERP_SLOT
) {
1125 err
= carl9170_set_slot_time(ar
);
1130 if (changed
& BSS_CHANGED_BASIC_RATES
) {
1131 err
= carl9170_set_mac_rates(ar
);
1137 WARN_ON_ONCE(err
&& IS_STARTED(ar
));
1138 mutex_unlock(&ar
->mutex
);
1141 static u64
carl9170_op_get_tsf(struct ieee80211_hw
*hw
,
1142 struct ieee80211_vif
*vif
)
1144 struct ar9170
*ar
= hw
->priv
;
1145 struct carl9170_tsf_rsp tsf
;
1148 mutex_lock(&ar
->mutex
);
1149 err
= carl9170_exec_cmd(ar
, CARL9170_CMD_READ_TSF
,
1150 0, NULL
, sizeof(tsf
), &tsf
);
1151 mutex_unlock(&ar
->mutex
);
1155 return le64_to_cpu(tsf
.tsf_64
);
1158 static int carl9170_op_set_key(struct ieee80211_hw
*hw
, enum set_key_cmd cmd
,
1159 struct ieee80211_vif
*vif
,
1160 struct ieee80211_sta
*sta
,
1161 struct ieee80211_key_conf
*key
)
1163 struct ar9170
*ar
= hw
->priv
;
1167 if (ar
->disable_offload
|| !vif
)
1170 /* Fall back to software encryption whenever the driver is connected
1171 * to more than one network.
1173 * This is very unfortunate, because some machines cannot handle
1174 * the high througput speed in 802.11n networks.
1177 if (!is_main_vif(ar
, vif
)) {
1178 mutex_lock(&ar
->mutex
);
1183 * While the hardware supports *catch-all* key, for offloading
1184 * group-key en-/de-cryption. The way of how the hardware
1185 * decides which keyId maps to which key, remains a mystery...
1187 if ((vif
->type
!= NL80211_IFTYPE_STATION
&&
1188 vif
->type
!= NL80211_IFTYPE_ADHOC
) &&
1189 !(key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
1192 switch (key
->cipher
) {
1193 case WLAN_CIPHER_SUITE_WEP40
:
1194 ktype
= AR9170_ENC_ALG_WEP64
;
1196 case WLAN_CIPHER_SUITE_WEP104
:
1197 ktype
= AR9170_ENC_ALG_WEP128
;
1199 case WLAN_CIPHER_SUITE_TKIP
:
1200 ktype
= AR9170_ENC_ALG_TKIP
;
1202 case WLAN_CIPHER_SUITE_CCMP
:
1203 ktype
= AR9170_ENC_ALG_AESCCMP
;
1204 key
->flags
|= IEEE80211_KEY_FLAG_SW_MGMT_TX
;
1210 mutex_lock(&ar
->mutex
);
1211 if (cmd
== SET_KEY
) {
1212 if (!IS_STARTED(ar
)) {
1217 if (!(key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
)) {
1220 i
= 64 + key
->keyidx
;
1222 for (i
= 0; i
< 64; i
++)
1223 if (!(ar
->usedkeys
& BIT(i
)))
1229 key
->hw_key_idx
= i
;
1231 err
= carl9170_upload_key(ar
, i
, sta
? sta
->addr
: NULL
,
1233 min_t(u8
, 16, key
->keylen
));
1237 if (key
->cipher
== WLAN_CIPHER_SUITE_TKIP
) {
1238 err
= carl9170_upload_key(ar
, i
, sta
? sta
->addr
:
1245 * hardware is not capable generating MMIC
1246 * of fragmented frames!
1248 key
->flags
|= IEEE80211_KEY_FLAG_GENERATE_MMIC
;
1252 ar
->usedkeys
|= BIT(i
);
1254 key
->flags
|= IEEE80211_KEY_FLAG_GENERATE_IV
;
1256 if (!IS_STARTED(ar
)) {
1257 /* The device is gone... together with the key ;-) */
1262 if (key
->hw_key_idx
< 64) {
1263 ar
->usedkeys
&= ~BIT(key
->hw_key_idx
);
1265 err
= carl9170_upload_key(ar
, key
->hw_key_idx
, NULL
,
1266 AR9170_ENC_ALG_NONE
, 0,
1271 if (key
->cipher
== WLAN_CIPHER_SUITE_TKIP
) {
1272 err
= carl9170_upload_key(ar
, key
->hw_key_idx
,
1274 AR9170_ENC_ALG_NONE
,
1282 err
= carl9170_disable_key(ar
, key
->hw_key_idx
);
1288 mutex_unlock(&ar
->mutex
);
1292 if (!ar
->rx_software_decryption
) {
1293 ar
->rx_software_decryption
= true;
1294 carl9170_set_operating_mode(ar
);
1296 mutex_unlock(&ar
->mutex
);
1300 static int carl9170_op_sta_add(struct ieee80211_hw
*hw
,
1301 struct ieee80211_vif
*vif
,
1302 struct ieee80211_sta
*sta
)
1304 struct carl9170_sta_info
*sta_info
= (void *) sta
->drv_priv
;
1307 atomic_set(&sta_info
->pending_frames
, 0);
1309 if (sta
->deflink
.ht_cap
.ht_supported
) {
1310 if (sta
->deflink
.ht_cap
.ampdu_density
> 6) {
1312 * HW does support 16us AMPDU density.
1313 * No HT-Xmit for station.
1319 for (i
= 0; i
< ARRAY_SIZE(sta_info
->agg
); i
++)
1320 RCU_INIT_POINTER(sta_info
->agg
[i
], NULL
);
1322 sta_info
->ampdu_max_len
= 1 << (3 + sta
->deflink
.ht_cap
.ampdu_factor
);
1323 sta_info
->ht_sta
= true;
1329 static int carl9170_op_sta_remove(struct ieee80211_hw
*hw
,
1330 struct ieee80211_vif
*vif
,
1331 struct ieee80211_sta
*sta
)
1333 struct ar9170
*ar
= hw
->priv
;
1334 struct carl9170_sta_info
*sta_info
= (void *) sta
->drv_priv
;
1336 bool cleanup
= false;
1338 if (sta
->deflink
.ht_cap
.ht_supported
) {
1340 sta_info
->ht_sta
= false;
1343 for (i
= 0; i
< ARRAY_SIZE(sta_info
->agg
); i
++) {
1344 struct carl9170_sta_tid
*tid_info
;
1346 tid_info
= rcu_dereference(sta_info
->agg
[i
]);
1347 RCU_INIT_POINTER(sta_info
->agg
[i
], NULL
);
1352 spin_lock_bh(&ar
->tx_ampdu_list_lock
);
1353 if (tid_info
->state
> CARL9170_TID_STATE_SHUTDOWN
)
1354 tid_info
->state
= CARL9170_TID_STATE_SHUTDOWN
;
1355 spin_unlock_bh(&ar
->tx_ampdu_list_lock
);
1361 carl9170_ampdu_gc(ar
);
1367 static int carl9170_op_conf_tx(struct ieee80211_hw
*hw
,
1368 struct ieee80211_vif
*vif
,
1369 unsigned int link_id
, u16 queue
,
1370 const struct ieee80211_tx_queue_params
*param
)
1372 struct ar9170
*ar
= hw
->priv
;
1375 mutex_lock(&ar
->mutex
);
1376 memcpy(&ar
->edcf
[ar9170_qmap(queue
)], param
, sizeof(*param
));
1377 ret
= carl9170_set_qos(ar
);
1378 mutex_unlock(&ar
->mutex
);
1382 static void carl9170_ampdu_work(struct work_struct
*work
)
1384 struct ar9170
*ar
= container_of(work
, struct ar9170
,
1387 if (!IS_STARTED(ar
))
1390 mutex_lock(&ar
->mutex
);
1391 carl9170_ampdu_gc(ar
);
1392 mutex_unlock(&ar
->mutex
);
1395 static int carl9170_op_ampdu_action(struct ieee80211_hw
*hw
,
1396 struct ieee80211_vif
*vif
,
1397 struct ieee80211_ampdu_params
*params
)
1399 struct ieee80211_sta
*sta
= params
->sta
;
1400 enum ieee80211_ampdu_mlme_action action
= params
->action
;
1401 u16 tid
= params
->tid
;
1402 u16
*ssn
= ¶ms
->ssn
;
1403 struct ar9170
*ar
= hw
->priv
;
1404 struct carl9170_sta_info
*sta_info
= (void *) sta
->drv_priv
;
1405 struct carl9170_sta_tid
*tid_info
;
1411 case IEEE80211_AMPDU_TX_START
:
1412 if (!sta_info
->ht_sta
)
1415 tid_info
= kzalloc(sizeof(struct carl9170_sta_tid
),
1420 tid_info
->hsn
= tid_info
->bsn
= tid_info
->snx
= (*ssn
);
1421 tid_info
->state
= CARL9170_TID_STATE_PROGRESS
;
1422 tid_info
->tid
= tid
;
1423 tid_info
->max
= sta_info
->ampdu_max_len
;
1424 tid_info
->sta
= sta
;
1425 tid_info
->vif
= vif
;
1427 INIT_LIST_HEAD(&tid_info
->list
);
1428 INIT_LIST_HEAD(&tid_info
->tmp_list
);
1429 skb_queue_head_init(&tid_info
->queue
);
1430 spin_lock_init(&tid_info
->lock
);
1432 spin_lock_bh(&ar
->tx_ampdu_list_lock
);
1433 ar
->tx_ampdu_list_len
++;
1434 list_add_tail_rcu(&tid_info
->list
, &ar
->tx_ampdu_list
);
1435 rcu_assign_pointer(sta_info
->agg
[tid
], tid_info
);
1436 spin_unlock_bh(&ar
->tx_ampdu_list_lock
);
1438 return IEEE80211_AMPDU_TX_START_IMMEDIATE
;
1440 case IEEE80211_AMPDU_TX_STOP_CONT
:
1441 case IEEE80211_AMPDU_TX_STOP_FLUSH
:
1442 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT
:
1444 tid_info
= rcu_dereference(sta_info
->agg
[tid
]);
1446 spin_lock_bh(&ar
->tx_ampdu_list_lock
);
1447 if (tid_info
->state
> CARL9170_TID_STATE_SHUTDOWN
)
1448 tid_info
->state
= CARL9170_TID_STATE_SHUTDOWN
;
1449 spin_unlock_bh(&ar
->tx_ampdu_list_lock
);
1452 RCU_INIT_POINTER(sta_info
->agg
[tid
], NULL
);
1455 ieee80211_stop_tx_ba_cb_irqsafe(vif
, sta
->addr
, tid
);
1456 ieee80211_queue_work(ar
->hw
, &ar
->ampdu_work
);
1459 case IEEE80211_AMPDU_TX_OPERATIONAL
:
1461 tid_info
= rcu_dereference(sta_info
->agg
[tid
]);
1463 sta_info
->stats
[tid
].clear
= true;
1464 sta_info
->stats
[tid
].req
= false;
1467 bitmap_zero(tid_info
->bitmap
, CARL9170_BAW_SIZE
);
1468 tid_info
->state
= CARL9170_TID_STATE_IDLE
;
1472 if (WARN_ON_ONCE(!tid_info
))
1477 case IEEE80211_AMPDU_RX_START
:
1478 case IEEE80211_AMPDU_RX_STOP
:
1479 /* Handled by hardware */
1489 #ifdef CONFIG_CARL9170_WPC
1490 static int carl9170_register_wps_button(struct ar9170
*ar
)
1492 struct input_dev
*input
;
1495 if (!(ar
->features
& CARL9170_WPS_BUTTON
))
1498 input
= devm_input_allocate_device(&ar
->udev
->dev
);
1502 snprintf(ar
->wps
.name
, sizeof(ar
->wps
.name
), "%s WPS Button",
1503 wiphy_name(ar
->hw
->wiphy
));
1505 snprintf(ar
->wps
.phys
, sizeof(ar
->wps
.phys
),
1506 "ieee80211/%s/input0", wiphy_name(ar
->hw
->wiphy
));
1508 input
->name
= ar
->wps
.name
;
1509 input
->phys
= ar
->wps
.phys
;
1510 input
->id
.bustype
= BUS_USB
;
1511 input
->dev
.parent
= &ar
->hw
->wiphy
->dev
;
1513 input_set_capability(input
, EV_KEY
, KEY_WPS_BUTTON
);
1515 err
= input_register_device(input
);
1519 ar
->wps
.pbc
= input
;
1522 #endif /* CONFIG_CARL9170_WPC */
1524 #ifdef CONFIG_CARL9170_HWRNG
1525 static int carl9170_rng_get(struct ar9170
*ar
)
1528 #define RW (CARL9170_MAX_CMD_PAYLOAD_LEN / sizeof(u32))
1529 #define RB (CARL9170_MAX_CMD_PAYLOAD_LEN)
1531 static const __le32 rng_load
[RW
] = {
1532 [0 ... (RW
- 1)] = cpu_to_le32(AR9170_RAND_REG_NUM
)};
1536 unsigned int i
, off
= 0, transfer
, count
;
1539 BUILD_BUG_ON(RB
> CARL9170_MAX_CMD_PAYLOAD_LEN
);
1541 if (!IS_ACCEPTING_CMD(ar
))
1544 count
= ARRAY_SIZE(ar
->rng
.cache
);
1546 err
= carl9170_exec_cmd(ar
, CARL9170_CMD_RREG
,
1547 RB
, (u8
*) rng_load
,
1552 transfer
= min_t(unsigned int, count
, RW
);
1553 for (i
= 0; i
< transfer
; i
++)
1554 ar
->rng
.cache
[off
+ i
] = buf
[i
];
1560 ar
->rng
.cache_idx
= 0;
1567 static int carl9170_rng_read(struct hwrng
*rng
, u32
*data
)
1569 struct ar9170
*ar
= (struct ar9170
*)rng
->priv
;
1572 mutex_lock(&ar
->mutex
);
1573 if (ar
->rng
.cache_idx
>= ARRAY_SIZE(ar
->rng
.cache
)) {
1574 ret
= carl9170_rng_get(ar
);
1576 mutex_unlock(&ar
->mutex
);
1581 *data
= ar
->rng
.cache
[ar
->rng
.cache_idx
++];
1582 mutex_unlock(&ar
->mutex
);
1587 static int carl9170_register_hwrng(struct ar9170
*ar
)
1591 snprintf(ar
->rng
.name
, ARRAY_SIZE(ar
->rng
.name
),
1592 "%s_%s", KBUILD_MODNAME
, wiphy_name(ar
->hw
->wiphy
));
1593 ar
->rng
.rng
.name
= ar
->rng
.name
;
1594 ar
->rng
.rng
.data_read
= carl9170_rng_read
;
1595 ar
->rng
.rng
.priv
= (unsigned long)ar
;
1597 err
= devm_hwrng_register(&ar
->udev
->dev
, &ar
->rng
.rng
);
1599 dev_err(&ar
->udev
->dev
, "Failed to register the random "
1600 "number generator (%d)\n", err
);
1604 return carl9170_rng_get(ar
);
1606 #endif /* CONFIG_CARL9170_HWRNG */
1608 static int carl9170_op_get_survey(struct ieee80211_hw
*hw
, int idx
,
1609 struct survey_info
*survey
)
1611 struct ar9170
*ar
= hw
->priv
;
1612 struct ieee80211_channel
*chan
;
1613 struct ieee80211_supported_band
*band
;
1620 if (idx
== chan
->hw_value
) {
1621 mutex_lock(&ar
->mutex
);
1622 err
= carl9170_update_survey(ar
, false, true);
1623 mutex_unlock(&ar
->mutex
);
1628 for (b
= 0; b
< NUM_NL80211_BANDS
; b
++) {
1629 band
= ar
->hw
->wiphy
->bands
[b
];
1634 for (i
= 0; i
< band
->n_channels
; i
++) {
1635 if (band
->channels
[i
].hw_value
== idx
) {
1636 chan
= &band
->channels
[i
];
1644 memcpy(survey
, &ar
->survey
[idx
], sizeof(*survey
));
1646 survey
->channel
= chan
;
1647 survey
->filled
= SURVEY_INFO_NOISE_DBM
;
1649 if (ar
->channel
== chan
)
1650 survey
->filled
|= SURVEY_INFO_IN_USE
;
1652 if (ar
->fw
.hw_counters
) {
1653 survey
->filled
|= SURVEY_INFO_TIME
|
1654 SURVEY_INFO_TIME_BUSY
|
1655 SURVEY_INFO_TIME_TX
;
1661 static void carl9170_op_flush(struct ieee80211_hw
*hw
,
1662 struct ieee80211_vif
*vif
,
1663 u32 queues
, bool drop
)
1665 struct ar9170
*ar
= hw
->priv
;
1668 mutex_lock(&ar
->mutex
);
1669 for_each_set_bit(vid
, &ar
->vif_bitmap
, ar
->fw
.vif_num
)
1670 carl9170_flush_cab(ar
, vid
);
1672 carl9170_flush(ar
, drop
);
1673 mutex_unlock(&ar
->mutex
);
1676 static int carl9170_op_get_stats(struct ieee80211_hw
*hw
,
1677 struct ieee80211_low_level_stats
*stats
)
1679 struct ar9170
*ar
= hw
->priv
;
1681 memset(stats
, 0, sizeof(*stats
));
1682 stats
->dot11ACKFailureCount
= ar
->tx_ack_failures
;
1683 stats
->dot11FCSErrorCount
= ar
->tx_fcs_errors
;
1687 static void carl9170_op_sta_notify(struct ieee80211_hw
*hw
,
1688 struct ieee80211_vif
*vif
,
1689 enum sta_notify_cmd cmd
,
1690 struct ieee80211_sta
*sta
)
1692 struct carl9170_sta_info
*sta_info
= (void *) sta
->drv_priv
;
1695 case STA_NOTIFY_SLEEP
:
1696 sta_info
->sleeping
= true;
1697 if (atomic_read(&sta_info
->pending_frames
))
1698 ieee80211_sta_block_awake(hw
, sta
, true);
1701 case STA_NOTIFY_AWAKE
:
1702 sta_info
->sleeping
= false;
1707 static bool carl9170_tx_frames_pending(struct ieee80211_hw
*hw
)
1709 struct ar9170
*ar
= hw
->priv
;
1711 return !!atomic_read(&ar
->tx_total_queued
);
1714 static const struct ieee80211_ops carl9170_ops
= {
1715 .add_chanctx
= ieee80211_emulate_add_chanctx
,
1716 .remove_chanctx
= ieee80211_emulate_remove_chanctx
,
1717 .change_chanctx
= ieee80211_emulate_change_chanctx
,
1718 .switch_vif_chanctx
= ieee80211_emulate_switch_vif_chanctx
,
1719 .start
= carl9170_op_start
,
1720 .stop
= carl9170_op_stop
,
1721 .tx
= carl9170_op_tx
,
1722 .wake_tx_queue
= ieee80211_handle_wake_tx_queue
,
1723 .flush
= carl9170_op_flush
,
1724 .add_interface
= carl9170_op_add_interface
,
1725 .remove_interface
= carl9170_op_remove_interface
,
1726 .config
= carl9170_op_config
,
1727 .prepare_multicast
= carl9170_op_prepare_multicast
,
1728 .configure_filter
= carl9170_op_configure_filter
,
1729 .conf_tx
= carl9170_op_conf_tx
,
1730 .bss_info_changed
= carl9170_op_bss_info_changed
,
1731 .get_tsf
= carl9170_op_get_tsf
,
1732 .set_key
= carl9170_op_set_key
,
1733 .sta_add
= carl9170_op_sta_add
,
1734 .sta_remove
= carl9170_op_sta_remove
,
1735 .sta_notify
= carl9170_op_sta_notify
,
1736 .get_survey
= carl9170_op_get_survey
,
1737 .get_stats
= carl9170_op_get_stats
,
1738 .ampdu_action
= carl9170_op_ampdu_action
,
1739 .tx_frames_pending
= carl9170_tx_frames_pending
,
1742 void *carl9170_alloc(size_t priv_size
)
1744 struct ieee80211_hw
*hw
;
1746 struct sk_buff
*skb
;
1750 * this buffer is used for rx stream reconstruction.
1751 * Under heavy load this device (or the transport layer?)
1752 * tends to split the streams into separate rx descriptors.
1755 skb
= __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE
, GFP_KERNEL
);
1759 hw
= ieee80211_alloc_hw(priv_size
, &carl9170_ops
);
1765 ar
->rx_failover
= skb
;
1767 memset(&ar
->rx_plcp
, 0, sizeof(struct ar9170_rx_head
));
1768 ar
->rx_has_plcp
= false;
1771 * Here's a hidden pitfall!
1773 * All 4 AC queues work perfectly well under _legacy_ operation.
1774 * However as soon as aggregation is enabled, the traffic flow
1775 * gets very bumpy. Therefore we have to _switch_ to a
1776 * software AC with a single HW queue.
1778 hw
->queues
= __AR9170_NUM_TXQ
;
1780 mutex_init(&ar
->mutex
);
1781 spin_lock_init(&ar
->beacon_lock
);
1782 spin_lock_init(&ar
->cmd_lock
);
1783 spin_lock_init(&ar
->tx_stats_lock
);
1784 spin_lock_init(&ar
->tx_ampdu_list_lock
);
1785 spin_lock_init(&ar
->mem_lock
);
1786 spin_lock_init(&ar
->state_lock
);
1787 atomic_set(&ar
->pending_restarts
, 0);
1789 for (i
= 0; i
< ar
->hw
->queues
; i
++) {
1790 skb_queue_head_init(&ar
->tx_status
[i
]);
1791 skb_queue_head_init(&ar
->tx_pending
[i
]);
1793 INIT_LIST_HEAD(&ar
->bar_list
[i
]);
1794 spin_lock_init(&ar
->bar_list_lock
[i
]);
1796 INIT_WORK(&ar
->ps_work
, carl9170_ps_work
);
1797 INIT_WORK(&ar
->ping_work
, carl9170_ping_work
);
1798 INIT_WORK(&ar
->restart_work
, carl9170_restart_work
);
1799 INIT_WORK(&ar
->ampdu_work
, carl9170_ampdu_work
);
1800 INIT_DELAYED_WORK(&ar
->stat_work
, carl9170_stat_work
);
1801 INIT_DELAYED_WORK(&ar
->tx_janitor
, carl9170_tx_janitor
);
1802 INIT_LIST_HEAD(&ar
->tx_ampdu_list
);
1803 rcu_assign_pointer(ar
->tx_ampdu_iter
,
1804 (struct carl9170_sta_tid
*) &ar
->tx_ampdu_list
);
1806 bitmap_zero(&ar
->vif_bitmap
, ar
->fw
.vif_num
);
1807 INIT_LIST_HEAD(&ar
->vif_list
);
1808 init_completion(&ar
->tx_flush
);
1810 /* firmware decides which modes we support */
1811 hw
->wiphy
->interface_modes
= 0;
1813 ieee80211_hw_set(hw
, RX_INCLUDES_FCS
);
1814 ieee80211_hw_set(hw
, MFP_CAPABLE
);
1815 ieee80211_hw_set(hw
, REPORTS_TX_ACK_STATUS
);
1816 ieee80211_hw_set(hw
, SUPPORTS_PS
);
1817 ieee80211_hw_set(hw
, PS_NULLFUNC_STACK
);
1818 ieee80211_hw_set(hw
, NEED_DTIM_BEFORE_ASSOC
);
1819 ieee80211_hw_set(hw
, SUPPORTS_RC_TABLE
);
1820 ieee80211_hw_set(hw
, SIGNAL_DBM
);
1821 ieee80211_hw_set(hw
, SUPPORTS_HT_CCK_RATES
);
1823 if (!modparam_noht
) {
1825 * see the comment above, why we allow the user
1826 * to disable HT by a module parameter.
1828 ieee80211_hw_set(hw
, AMPDU_AGGREGATION
);
1831 hw
->extra_tx_headroom
= sizeof(struct _carl9170_tx_superframe
);
1832 hw
->sta_data_size
= sizeof(struct carl9170_sta_info
);
1833 hw
->vif_data_size
= sizeof(struct carl9170_vif_info
);
1835 hw
->max_rates
= CARL9170_TX_MAX_RATES
;
1836 hw
->max_rate_tries
= CARL9170_TX_USER_RATE_TRIES
;
1838 for (i
= 0; i
< ARRAY_SIZE(ar
->noise
); i
++)
1839 ar
->noise
[i
] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
1841 wiphy_ext_feature_set(hw
->wiphy
, NL80211_EXT_FEATURE_CQM_RSSI_LIST
);
1847 return ERR_PTR(-ENOMEM
);
1850 static int carl9170_read_eeprom(struct ar9170
*ar
)
1852 #define RW 8 /* number of words to read at once */
1853 #define RB (sizeof(u32) * RW)
1854 u8
*eeprom
= (void *)&ar
->eeprom
;
1858 BUILD_BUG_ON(sizeof(ar
->eeprom
) & 3);
1860 BUILD_BUG_ON(RB
> CARL9170_MAX_CMD_LEN
- 4);
1862 /* don't want to handle trailing remains */
1863 BUILD_BUG_ON(sizeof(ar
->eeprom
) % RB
);
1866 for (i
= 0; i
< sizeof(ar
->eeprom
) / RB
; i
++) {
1867 for (j
= 0; j
< RW
; j
++)
1868 offsets
[j
] = cpu_to_le32(AR9170_EEPROM_START
+
1871 err
= carl9170_exec_cmd(ar
, CARL9170_CMD_RREG
,
1872 RB
, (u8
*) &offsets
,
1873 RB
, eeprom
+ RB
* i
);
1883 static int carl9170_parse_eeprom(struct ar9170
*ar
)
1885 struct ath_regulatory
*regulatory
= &ar
->common
.regulatory
;
1886 unsigned int rx_streams
, tx_streams
, tx_params
= 0;
1890 if (ar
->eeprom
.length
== cpu_to_le16(0xffff))
1893 rx_streams
= hweight8(ar
->eeprom
.rx_mask
);
1894 tx_streams
= hweight8(ar
->eeprom
.tx_mask
);
1896 if (rx_streams
!= tx_streams
) {
1897 tx_params
= IEEE80211_HT_MCS_TX_RX_DIFF
;
1899 WARN_ON(!(tx_streams
>= 1 && tx_streams
<=
1900 IEEE80211_HT_MCS_TX_MAX_STREAMS
));
1902 tx_params
|= (tx_streams
- 1) <<
1903 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT
;
1905 carl9170_band_2GHz
.ht_cap
.mcs
.tx_params
|= tx_params
;
1906 carl9170_band_5GHz
.ht_cap
.mcs
.tx_params
|= tx_params
;
1909 if (ar
->eeprom
.operating_flags
& AR9170_OPFLAG_2GHZ
) {
1910 ar
->hw
->wiphy
->bands
[NL80211_BAND_2GHZ
] =
1911 &carl9170_band_2GHz
;
1912 chans
+= carl9170_band_2GHz
.n_channels
;
1915 if (ar
->eeprom
.operating_flags
& AR9170_OPFLAG_5GHZ
) {
1916 ar
->hw
->wiphy
->bands
[NL80211_BAND_5GHZ
] =
1917 &carl9170_band_5GHz
;
1918 chans
+= carl9170_band_5GHz
.n_channels
;
1925 ar
->survey
= devm_kcalloc(&ar
->udev
->dev
, chans
,
1926 sizeof(struct survey_info
), GFP_KERNEL
);
1929 ar
->num_channels
= chans
;
1931 regulatory
->current_rd
= le16_to_cpu(ar
->eeprom
.reg_domain
[0]);
1933 /* second part of wiphy init */
1934 SET_IEEE80211_PERM_ADDR(ar
->hw
, ar
->eeprom
.mac_address
);
1939 static void carl9170_reg_notifier(struct wiphy
*wiphy
,
1940 struct regulatory_request
*request
)
1942 struct ieee80211_hw
*hw
= wiphy_to_ieee80211_hw(wiphy
);
1943 struct ar9170
*ar
= hw
->priv
;
1945 ath_reg_notifier_apply(wiphy
, request
, &ar
->common
.regulatory
);
1948 int carl9170_register(struct ar9170
*ar
)
1950 struct ath_regulatory
*regulatory
= &ar
->common
.regulatory
;
1953 ar
->mem_bitmap
= devm_bitmap_zalloc(&ar
->udev
->dev
, ar
->fw
.mem_blocks
, GFP_KERNEL
);
1954 if (!ar
->mem_bitmap
)
1957 /* try to read EEPROM, init MAC addr */
1958 err
= carl9170_read_eeprom(ar
);
1962 err
= carl9170_parse_eeprom(ar
);
1966 err
= ath_regd_init(regulatory
, ar
->hw
->wiphy
,
1967 carl9170_reg_notifier
);
1971 if (modparam_noht
) {
1972 carl9170_band_2GHz
.ht_cap
.ht_supported
= false;
1973 carl9170_band_5GHz
.ht_cap
.ht_supported
= false;
1976 for (i
= 0; i
< ar
->fw
.vif_num
; i
++) {
1977 ar
->vif_priv
[i
].id
= i
;
1978 ar
->vif_priv
[i
].vif
= NULL
;
1981 err
= ieee80211_register_hw(ar
->hw
);
1985 /* mac80211 interface is now registered */
1986 ar
->registered
= true;
1988 if (!ath_is_world_regd(regulatory
))
1989 regulatory_hint(ar
->hw
->wiphy
, regulatory
->alpha2
);
1991 #ifdef CONFIG_CARL9170_DEBUGFS
1992 carl9170_debugfs_register(ar
);
1993 #endif /* CONFIG_CARL9170_DEBUGFS */
1995 err
= carl9170_led_init(ar
);
1999 #ifdef CONFIG_CARL9170_LEDS
2000 err
= carl9170_led_register(ar
);
2003 #endif /* CONFIG_CARL9170_LEDS */
2005 #ifdef CONFIG_CARL9170_WPC
2006 err
= carl9170_register_wps_button(ar
);
2009 #endif /* CONFIG_CARL9170_WPC */
2011 #ifdef CONFIG_CARL9170_HWRNG
2012 err
= carl9170_register_hwrng(ar
);
2015 #endif /* CONFIG_CARL9170_HWRNG */
2017 dev_info(&ar
->udev
->dev
, "Atheros AR9170 is registered as '%s'\n",
2018 wiphy_name(ar
->hw
->wiphy
));
2023 carl9170_unregister(ar
);
2027 void carl9170_unregister(struct ar9170
*ar
)
2029 if (!ar
->registered
)
2032 ar
->registered
= false;
2034 #ifdef CONFIG_CARL9170_LEDS
2035 carl9170_led_unregister(ar
);
2036 #endif /* CONFIG_CARL9170_LEDS */
2038 #ifdef CONFIG_CARL9170_DEBUGFS
2039 carl9170_debugfs_unregister(ar
);
2040 #endif /* CONFIG_CARL9170_DEBUGFS */
2042 carl9170_cancel_worker(ar
);
2043 cancel_work_sync(&ar
->restart_work
);
2045 ieee80211_unregister_hw(ar
->hw
);
2048 void carl9170_free(struct ar9170
*ar
)
2050 WARN_ON(ar
->registered
);
2051 WARN_ON(IS_INITIALIZED(ar
));
2053 kfree_skb(ar
->rx_failover
);
2054 ar
->rx_failover
= NULL
;
2056 mutex_destroy(&ar
->mutex
);
2058 ieee80211_free_hw(ar
->hw
);