2 * Atheros AR9170 driver
4 * mac80211 interaction code
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, Christian Lamparter <chunkeey@web.de>
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/module.h>
42 #include <linux/etherdevice.h>
43 #include <net/mac80211.h>
48 static int modparam_nohwcrypt
;
49 module_param_named(nohwcrypt
, modparam_nohwcrypt
, bool, S_IRUGO
);
50 MODULE_PARM_DESC(nohwcrypt
, "Disable hardware encryption.");
52 #define RATE(_bitrate, _hw_rate, _txpidx, _flags) { \
53 .bitrate = (_bitrate), \
55 .hw_value = (_hw_rate) | (_txpidx) << 4, \
58 static struct ieee80211_rate __ar9170_ratetable
[] = {
60 RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE
),
61 RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE
),
62 RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE
),
74 #define ar9170_g_ratetable (__ar9170_ratetable + 0)
75 #define ar9170_g_ratetable_size 12
76 #define ar9170_a_ratetable (__ar9170_ratetable + 4)
77 #define ar9170_a_ratetable_size 8
80 * NB: The hw_value is used as an index into the ar9170_phy_freq_params
81 * array in phy.c so that we don't have to do frequency lookups!
83 #define CHAN(_freq, _idx) { \
84 .center_freq = (_freq), \
86 .max_power = 18, /* XXX */ \
89 static struct ieee80211_channel ar9170_2ghz_chantable
[] = {
106 static struct ieee80211_channel ar9170_5ghz_chantable
[] = {
145 #define AR9170_HT_CAP \
147 .ht_supported = true, \
148 .cap = IEEE80211_HT_CAP_MAX_AMSDU | \
149 IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
150 IEEE80211_HT_CAP_SGI_40 | \
151 IEEE80211_HT_CAP_DSSSCCK40 | \
152 IEEE80211_HT_CAP_SM_PS, \
154 .ampdu_density = 6, \
156 .rx_mask = { 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, }, \
160 static struct ieee80211_supported_band ar9170_band_2GHz
= {
161 .channels
= ar9170_2ghz_chantable
,
162 .n_channels
= ARRAY_SIZE(ar9170_2ghz_chantable
),
163 .bitrates
= ar9170_g_ratetable
,
164 .n_bitrates
= ar9170_g_ratetable_size
,
165 .ht_cap
= AR9170_HT_CAP
,
168 static struct ieee80211_supported_band ar9170_band_5GHz
= {
169 .channels
= ar9170_5ghz_chantable
,
170 .n_channels
= ARRAY_SIZE(ar9170_5ghz_chantable
),
171 .bitrates
= ar9170_a_ratetable
,
172 .n_bitrates
= ar9170_a_ratetable_size
,
173 .ht_cap
= AR9170_HT_CAP
,
176 static void ar9170_tx(struct ar9170
*ar
);
178 #ifdef AR9170_QUEUE_DEBUG
179 static void ar9170_print_txheader(struct ar9170
*ar
, struct sk_buff
*skb
)
181 struct ar9170_tx_control
*txc
= (void *) skb
->data
;
182 struct ieee80211_tx_info
*txinfo
= IEEE80211_SKB_CB(skb
);
183 struct ar9170_tx_info
*arinfo
= (void *) txinfo
->rate_driver_data
;
184 struct ieee80211_hdr
*hdr
= (void *) txc
->frame_data
;
186 printk(KERN_DEBUG
"%s: => FRAME [skb:%p, q:%d, DA:[%pM] flags:%x "
187 "mac_ctrl:%04x, phy_ctrl:%08x, timeout:[%d ms]]\n",
188 wiphy_name(ar
->hw
->wiphy
), skb
, skb_get_queue_mapping(skb
),
189 ieee80211_get_DA(hdr
), arinfo
->flags
,
190 le16_to_cpu(txc
->mac_control
), le32_to_cpu(txc
->phy_control
),
191 jiffies_to_msecs(arinfo
->timeout
- jiffies
));
194 static void __ar9170_dump_txqueue(struct ar9170
*ar
,
195 struct sk_buff_head
*queue
)
200 printk(KERN_DEBUG
"---[ cut here ]---\n");
201 printk(KERN_DEBUG
"%s: %d entries in queue.\n",
202 wiphy_name(ar
->hw
->wiphy
), skb_queue_len(queue
));
204 skb_queue_walk(queue
, skb
) {
205 printk(KERN_DEBUG
"index:%d => \n", i
++);
206 ar9170_print_txheader(ar
, skb
);
208 if (i
!= skb_queue_len(queue
))
209 printk(KERN_DEBUG
"WARNING: queue frame counter "
210 "mismatch %d != %d\n", skb_queue_len(queue
), i
);
211 printk(KERN_DEBUG
"---[ end ]---\n");
214 static void ar9170_dump_txqueue(struct ar9170
*ar
,
215 struct sk_buff_head
*queue
)
219 spin_lock_irqsave(&queue
->lock
, flags
);
220 __ar9170_dump_txqueue(ar
, queue
);
221 spin_unlock_irqrestore(&queue
->lock
, flags
);
224 static void __ar9170_dump_txstats(struct ar9170
*ar
)
228 printk(KERN_DEBUG
"%s: QoS queue stats\n",
229 wiphy_name(ar
->hw
->wiphy
));
231 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++)
232 printk(KERN_DEBUG
"%s: queue:%d limit:%d len:%d waitack:%d\n",
233 wiphy_name(ar
->hw
->wiphy
), i
, ar
->tx_stats
[i
].limit
,
234 ar
->tx_stats
[i
].len
, skb_queue_len(&ar
->tx_status
[i
]));
237 static void ar9170_dump_txstats(struct ar9170
*ar
)
241 spin_lock_irqsave(&ar
->tx_stats_lock
, flags
);
242 __ar9170_dump_txstats(ar
);
243 spin_unlock_irqrestore(&ar
->tx_stats_lock
, flags
);
245 #endif /* AR9170_QUEUE_DEBUG */
247 /* caller must guarantee exclusive access for _bin_ queue. */
248 static void ar9170_recycle_expired(struct ar9170
*ar
,
249 struct sk_buff_head
*queue
,
250 struct sk_buff_head
*bin
)
252 struct sk_buff
*skb
, *old
= NULL
;
255 spin_lock_irqsave(&queue
->lock
, flags
);
256 while ((skb
= skb_peek(queue
))) {
257 struct ieee80211_tx_info
*txinfo
;
258 struct ar9170_tx_info
*arinfo
;
260 txinfo
= IEEE80211_SKB_CB(skb
);
261 arinfo
= (void *) txinfo
->rate_driver_data
;
263 if (time_is_before_jiffies(arinfo
->timeout
)) {
264 #ifdef AR9170_QUEUE_DEBUG
265 printk(KERN_DEBUG
"%s: [%ld > %ld] frame expired => "
266 "recycle \n", wiphy_name(ar
->hw
->wiphy
),
267 jiffies
, arinfo
->timeout
);
268 ar9170_print_txheader(ar
, skb
);
269 #endif /* AR9170_QUEUE_DEBUG */
270 __skb_unlink(skb
, queue
);
271 __skb_queue_tail(bin
, skb
);
276 if (unlikely(old
== skb
)) {
277 /* bail out - queue is shot. */
284 spin_unlock_irqrestore(&queue
->lock
, flags
);
287 static void ar9170_tx_status(struct ar9170
*ar
, struct sk_buff
*skb
,
290 struct ieee80211_tx_info
*txinfo
;
291 unsigned int retries
= 0;
293 txinfo
= IEEE80211_SKB_CB(skb
);
294 ieee80211_tx_info_clear_status(txinfo
);
297 case AR9170_TX_STATUS_RETRY
:
299 case AR9170_TX_STATUS_COMPLETE
:
300 txinfo
->flags
|= IEEE80211_TX_STAT_ACK
;
303 case AR9170_TX_STATUS_FAILED
:
304 retries
= ar
->hw
->conf
.long_frame_max_tx_count
;
308 printk(KERN_ERR
"%s: invalid tx_status response (%x).\n",
309 wiphy_name(ar
->hw
->wiphy
), tx_status
);
313 txinfo
->status
.rates
[0].count
= retries
+ 1;
314 skb_pull(skb
, sizeof(struct ar9170_tx_control
));
315 ieee80211_tx_status_irqsafe(ar
->hw
, skb
);
318 void ar9170_tx_callback(struct ar9170
*ar
, struct sk_buff
*skb
)
320 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
321 struct ar9170_tx_info
*arinfo
= (void *) info
->rate_driver_data
;
322 unsigned int queue
= skb_get_queue_mapping(skb
);
325 spin_lock_irqsave(&ar
->tx_stats_lock
, flags
);
326 ar
->tx_stats
[queue
].len
--;
328 if (skb_queue_empty(&ar
->tx_pending
[queue
])) {
329 #ifdef AR9170_QUEUE_STOP_DEBUG
330 printk(KERN_DEBUG
"%s: wake queue %d\n",
331 wiphy_name(ar
->hw
->wiphy
), queue
);
332 __ar9170_dump_txstats(ar
);
333 #endif /* AR9170_QUEUE_STOP_DEBUG */
334 ieee80211_wake_queue(ar
->hw
, queue
);
336 spin_unlock_irqrestore(&ar
->tx_stats_lock
, flags
);
338 if (arinfo
->flags
& AR9170_TX_FLAG_BLOCK_ACK
) {
339 dev_kfree_skb_any(skb
);
340 } else if (arinfo
->flags
& AR9170_TX_FLAG_WAIT_FOR_ACK
) {
341 arinfo
->timeout
= jiffies
+
342 msecs_to_jiffies(AR9170_TX_TIMEOUT
);
344 skb_queue_tail(&ar
->tx_status
[queue
], skb
);
345 } else if (arinfo
->flags
& AR9170_TX_FLAG_NO_ACK
) {
346 ar9170_tx_status(ar
, skb
, AR9170_TX_STATUS_FAILED
);
348 #ifdef AR9170_QUEUE_DEBUG
349 printk(KERN_DEBUG
"%s: unsupported frame flags!\n",
350 wiphy_name(ar
->hw
->wiphy
));
351 ar9170_print_txheader(ar
, skb
);
352 #endif /* AR9170_QUEUE_DEBUG */
353 dev_kfree_skb_any(skb
);
356 if (!ar
->tx_stats
[queue
].len
&&
357 !skb_queue_empty(&ar
->tx_pending
[queue
])) {
362 static struct sk_buff
*ar9170_get_queued_skb(struct ar9170
*ar
,
364 struct sk_buff_head
*queue
,
371 * Unfortunately, the firmware does not tell to which (queued) frame
372 * this transmission status report belongs to.
374 * So we have to make risky guesses - with the scarce information
375 * the firmware provided (-> destination MAC, and phy_control) -
376 * and hope that we picked the right one...
379 spin_lock_irqsave(&queue
->lock
, flags
);
380 skb_queue_walk(queue
, skb
) {
381 struct ar9170_tx_control
*txc
= (void *) skb
->data
;
382 struct ieee80211_hdr
*hdr
= (void *) txc
->frame_data
;
385 if (mac
&& compare_ether_addr(ieee80211_get_DA(hdr
), mac
)) {
386 #ifdef AR9170_QUEUE_DEBUG
387 printk(KERN_DEBUG
"%s: skip frame => DA %pM != %pM\n",
388 wiphy_name(ar
->hw
->wiphy
), mac
,
389 ieee80211_get_DA(hdr
));
390 ar9170_print_txheader(ar
, skb
);
391 #endif /* AR9170_QUEUE_DEBUG */
395 r
= (le32_to_cpu(txc
->phy_control
) & AR9170_TX_PHY_MCS_MASK
) >>
396 AR9170_TX_PHY_MCS_SHIFT
;
398 if ((rate
!= AR9170_TX_INVALID_RATE
) && (r
!= rate
)) {
399 #ifdef AR9170_QUEUE_DEBUG
400 printk(KERN_DEBUG
"%s: skip frame => rate %d != %d\n",
401 wiphy_name(ar
->hw
->wiphy
), rate
, r
);
402 ar9170_print_txheader(ar
, skb
);
403 #endif /* AR9170_QUEUE_DEBUG */
407 __skb_unlink(skb
, queue
);
408 spin_unlock_irqrestore(&queue
->lock
, flags
);
412 #ifdef AR9170_QUEUE_DEBUG
413 printk(KERN_ERR
"%s: ESS:[%pM] does not have any "
414 "outstanding frames in queue.\n",
415 wiphy_name(ar
->hw
->wiphy
), mac
);
416 __ar9170_dump_txqueue(ar
, queue
);
417 #endif /* AR9170_QUEUE_DEBUG */
418 spin_unlock_irqrestore(&queue
->lock
, flags
);
424 * This worker tries to keeps an maintain tx_status queues.
425 * So we can guarantee that incoming tx_status reports are
426 * actually for a pending frame.
429 static void ar9170_tx_janitor(struct work_struct
*work
)
431 struct ar9170
*ar
= container_of(work
, struct ar9170
,
433 struct sk_buff_head waste
;
435 bool resched
= false;
437 if (unlikely(!IS_STARTED(ar
)))
440 skb_queue_head_init(&waste
);
442 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++) {
443 #ifdef AR9170_QUEUE_DEBUG
444 printk(KERN_DEBUG
"%s: garbage collector scans queue:%d\n",
445 wiphy_name(ar
->hw
->wiphy
), i
);
446 ar9170_dump_txqueue(ar
, &ar
->tx_pending
[i
]);
447 ar9170_dump_txqueue(ar
, &ar
->tx_status
[i
]);
448 #endif /* AR9170_QUEUE_DEBUG */
450 ar9170_recycle_expired(ar
, &ar
->tx_status
[i
], &waste
);
451 ar9170_recycle_expired(ar
, &ar
->tx_pending
[i
], &waste
);
452 skb_queue_purge(&waste
);
454 if (!skb_queue_empty(&ar
->tx_status
[i
]) ||
455 !skb_queue_empty(&ar
->tx_pending
[i
]))
460 queue_delayed_work(ar
->hw
->workqueue
,
462 msecs_to_jiffies(AR9170_JANITOR_DELAY
));
465 void ar9170_handle_command_response(struct ar9170
*ar
, void *buf
, u32 len
)
467 struct ar9170_cmd_response
*cmd
= (void *) buf
;
469 if ((cmd
->type
& 0xc0) != 0xc0) {
470 ar
->callback_cmd(ar
, len
, buf
);
474 /* hardware event handlers */
478 * TX status notification:
479 * bytes: 0c c1 XX YY M1 M2 M3 M4 M5 M6 R4 R3 R2 R1 S2 S1
483 * M1-M6 is the MAC address
484 * R1-R4 is the transmit rate
485 * S1-S2 is the transmit status
489 u32 phy
= le32_to_cpu(cmd
->tx_status
.rate
);
490 u32 q
= (phy
& AR9170_TX_PHY_QOS_MASK
) >>
491 AR9170_TX_PHY_QOS_SHIFT
;
492 #ifdef AR9170_QUEUE_DEBUG
493 printk(KERN_DEBUG
"%s: recv tx_status for %pM, p:%08x, q:%d\n",
494 wiphy_name(ar
->hw
->wiphy
), cmd
->tx_status
.dst
, phy
, q
);
495 #endif /* AR9170_QUEUE_DEBUG */
497 skb
= ar9170_get_queued_skb(ar
, cmd
->tx_status
.dst
,
499 AR9170_TX_INVALID_RATE
);
503 ar9170_tx_status(ar
, skb
, le16_to_cpu(cmd
->tx_status
.status
));
511 if (ar
->vif
&& ar
->vif
->type
== NL80211_IFTYPE_AP
)
512 queue_work(ar
->hw
->workqueue
, &ar
->beacon_work
);
517 * (IBSS) beacon send notification
518 * bytes: 04 c2 XX YY B4 B3 B2 B1
522 * B1-B4 "should" be the number of send out beacons.
527 /* End of Atim Window */
532 /* BlockACK events */
536 /* Watchdog Interrupt */
540 /* retransmission issue / SIFS/EIFS collision ?! */
545 printk(KERN_DEBUG
"ar9170 FW: %.*s\n", len
- 4, (char *)buf
+ 4);
552 printk(KERN_DEBUG
"ar9170 FW: u8: %#.2x\n",
556 printk(KERN_DEBUG
"ar9170 FW: u8: %#.4x\n",
557 le16_to_cpup((__le16
*)((char *)buf
+ 4)));
560 printk(KERN_DEBUG
"ar9170 FW: u8: %#.8x\n",
561 le32_to_cpup((__le32
*)((char *)buf
+ 4)));
564 printk(KERN_DEBUG
"ar9170 FW: u8: %#.16lx\n",
565 (unsigned long)le64_to_cpup(
566 (__le64
*)((char *)buf
+ 4)));
571 print_hex_dump_bytes("ar9170 FW:", DUMP_PREFIX_NONE
,
572 (char *)buf
+ 4, len
- 4);
576 printk(KERN_INFO
"received unhandled event %x\n", cmd
->type
);
577 print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE
, buf
, len
);
582 static void ar9170_rx_reset_rx_mpdu(struct ar9170
*ar
)
584 memset(&ar
->rx_mpdu
.plcp
, 0, sizeof(struct ar9170_rx_head
));
585 ar
->rx_mpdu
.has_plcp
= false;
588 int ar9170_nag_limiter(struct ar9170
*ar
)
593 * we expect all sorts of errors in promiscuous mode.
594 * don't bother with it, it's OK!
596 if (ar
->sniffer_enabled
)
600 * only go for frequent errors! The hardware tends to
601 * do some stupid thing once in a while under load, in
602 * noisy environments or just for fun!
604 if (time_before(jiffies
, ar
->bad_hw_nagger
) && net_ratelimit())
605 print_message
= true;
607 print_message
= false;
609 /* reset threshold for "once in a while" */
610 ar
->bad_hw_nagger
= jiffies
+ HZ
/ 4;
611 return print_message
;
614 static int ar9170_rx_mac_status(struct ar9170
*ar
,
615 struct ar9170_rx_head
*head
,
616 struct ar9170_rx_macstatus
*mac
,
617 struct ieee80211_rx_status
*status
)
621 BUILD_BUG_ON(sizeof(struct ar9170_rx_head
) != 12);
622 BUILD_BUG_ON(sizeof(struct ar9170_rx_macstatus
) != 4);
625 if (error
& AR9170_RX_ERROR_MMIC
) {
626 status
->flag
|= RX_FLAG_MMIC_ERROR
;
627 error
&= ~AR9170_RX_ERROR_MMIC
;
630 if (error
& AR9170_RX_ERROR_PLCP
) {
631 status
->flag
|= RX_FLAG_FAILED_PLCP_CRC
;
632 error
&= ~AR9170_RX_ERROR_PLCP
;
634 if (!(ar
->filter_state
& FIF_PLCPFAIL
))
638 if (error
& AR9170_RX_ERROR_FCS
) {
639 status
->flag
|= RX_FLAG_FAILED_FCS_CRC
;
640 error
&= ~AR9170_RX_ERROR_FCS
;
642 if (!(ar
->filter_state
& FIF_FCSFAIL
))
646 decrypt
= ar9170_get_decrypt_type(mac
);
647 if (!(decrypt
& AR9170_RX_ENC_SOFTWARE
) &&
648 decrypt
!= AR9170_ENC_ALG_NONE
)
649 status
->flag
|= RX_FLAG_DECRYPTED
;
651 /* ignore wrong RA errors */
652 error
&= ~AR9170_RX_ERROR_WRONG_RA
;
654 if (error
& AR9170_RX_ERROR_DECRYPT
) {
655 error
&= ~AR9170_RX_ERROR_DECRYPT
;
657 * Rx decryption is done in place,
658 * the original data is lost anyway.
664 /* drop any other error frames */
665 if (unlikely(error
)) {
666 /* TODO: update netdevice's RX dropped/errors statistics */
668 if (ar9170_nag_limiter(ar
))
669 printk(KERN_DEBUG
"%s: received frame with "
670 "suspicious error code (%#x).\n",
671 wiphy_name(ar
->hw
->wiphy
), error
);
676 status
->band
= ar
->channel
->band
;
677 status
->freq
= ar
->channel
->center_freq
;
679 switch (mac
->status
& AR9170_RX_STATUS_MODULATION_MASK
) {
680 case AR9170_RX_STATUS_MODULATION_CCK
:
681 if (mac
->status
& AR9170_RX_STATUS_SHORT_PREAMBLE
)
682 status
->flag
|= RX_FLAG_SHORTPRE
;
683 switch (head
->plcp
[0]) {
685 status
->rate_idx
= 0;
688 status
->rate_idx
= 1;
691 status
->rate_idx
= 2;
694 status
->rate_idx
= 3;
697 if (ar9170_nag_limiter(ar
))
698 printk(KERN_ERR
"%s: invalid plcp cck rate "
699 "(%x).\n", wiphy_name(ar
->hw
->wiphy
),
705 case AR9170_RX_STATUS_MODULATION_OFDM
:
706 switch (head
->plcp
[0] & 0xf) {
708 status
->rate_idx
= 0;
711 status
->rate_idx
= 1;
714 status
->rate_idx
= 2;
717 status
->rate_idx
= 3;
720 status
->rate_idx
= 4;
723 status
->rate_idx
= 5;
726 status
->rate_idx
= 6;
729 status
->rate_idx
= 7;
732 if (ar9170_nag_limiter(ar
))
733 printk(KERN_ERR
"%s: invalid plcp ofdm rate "
734 "(%x).\n", wiphy_name(ar
->hw
->wiphy
),
738 if (status
->band
== IEEE80211_BAND_2GHZ
)
739 status
->rate_idx
+= 4;
742 case AR9170_RX_STATUS_MODULATION_HT
:
743 if (head
->plcp
[3] & 0x80)
744 status
->flag
|= RX_FLAG_40MHZ
;
745 if (head
->plcp
[6] & 0x80)
746 status
->flag
|= RX_FLAG_SHORT_GI
;
748 status
->rate_idx
= clamp(0, 75, head
->plcp
[6] & 0x7f);
749 status
->flag
|= RX_FLAG_HT
;
752 case AR9170_RX_STATUS_MODULATION_DUPOFDM
:
754 if (ar9170_nag_limiter(ar
))
755 printk(KERN_ERR
"%s: invalid modulation\n",
756 wiphy_name(ar
->hw
->wiphy
));
763 static void ar9170_rx_phy_status(struct ar9170
*ar
,
764 struct ar9170_rx_phystatus
*phy
,
765 struct ieee80211_rx_status
*status
)
769 BUILD_BUG_ON(sizeof(struct ar9170_rx_phystatus
) != 20);
771 for (i
= 0; i
< 3; i
++)
772 if (phy
->rssi
[i
] != 0x80)
773 status
->antenna
|= BIT(i
);
775 /* post-process RSSI */
776 for (i
= 0; i
< 7; i
++)
777 if (phy
->rssi
[i
] & 0x80)
778 phy
->rssi
[i
] = ((phy
->rssi
[i
] & 0x7f) + 1) & 0x7f;
780 /* TODO: we could do something with phy_errors */
781 status
->signal
= ar
->noise
[0] + phy
->rssi_combined
;
782 status
->noise
= ar
->noise
[0];
785 static struct sk_buff
*ar9170_rx_copy_data(u8
*buf
, int len
)
789 struct ieee80211_hdr
*hdr
= (void *) buf
;
791 if (ieee80211_is_data_qos(hdr
->frame_control
)) {
792 u8
*qc
= ieee80211_get_qos_ctl(hdr
);
793 reserved
+= NET_IP_ALIGN
;
795 if (*qc
& IEEE80211_QOS_CONTROL_A_MSDU_PRESENT
)
796 reserved
+= NET_IP_ALIGN
;
799 if (ieee80211_has_a4(hdr
->frame_control
))
800 reserved
+= NET_IP_ALIGN
;
802 reserved
= 32 + (reserved
& NET_IP_ALIGN
);
804 skb
= dev_alloc_skb(len
+ reserved
);
806 skb_reserve(skb
, reserved
);
807 memcpy(skb_put(skb
, len
), buf
, len
);
814 * If the frame alignment is right (or the kernel has
815 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
816 * is only a single MPDU in the USB frame, then we could
817 * submit to mac80211 the SKB directly. However, since
818 * there may be multiple packets in one SKB in stream
819 * mode, and we need to observe the proper ordering,
820 * this is non-trivial.
823 static void ar9170_handle_mpdu(struct ar9170
*ar
, u8
*buf
, int len
)
825 struct ar9170_rx_head
*head
;
826 struct ar9170_rx_macstatus
*mac
;
827 struct ar9170_rx_phystatus
*phy
= NULL
;
828 struct ieee80211_rx_status status
;
832 if (unlikely(!IS_STARTED(ar
) || len
< (sizeof(*mac
))))
836 mpdu_len
= len
- sizeof(*mac
);
838 mac
= (void *)(buf
+ mpdu_len
);
839 if (unlikely(mac
->error
& AR9170_RX_ERROR_FATAL
)) {
840 /* this frame is too damaged and can't be used - drop it */
845 switch (mac
->status
& AR9170_RX_STATUS_MPDU_MASK
) {
846 case AR9170_RX_STATUS_MPDU_FIRST
:
847 /* first mpdu packet has the plcp header */
848 if (likely(mpdu_len
>= sizeof(struct ar9170_rx_head
))) {
850 memcpy(&ar
->rx_mpdu
.plcp
, (void *) buf
,
851 sizeof(struct ar9170_rx_head
));
853 mpdu_len
-= sizeof(struct ar9170_rx_head
);
854 buf
+= sizeof(struct ar9170_rx_head
);
855 ar
->rx_mpdu
.has_plcp
= true;
857 if (ar9170_nag_limiter(ar
))
858 printk(KERN_ERR
"%s: plcp info is clipped.\n",
859 wiphy_name(ar
->hw
->wiphy
));
864 case AR9170_RX_STATUS_MPDU_LAST
:
865 /* last mpdu has a extra tail with phy status information */
867 if (likely(mpdu_len
>= sizeof(struct ar9170_rx_phystatus
))) {
868 mpdu_len
-= sizeof(struct ar9170_rx_phystatus
);
869 phy
= (void *)(buf
+ mpdu_len
);
871 if (ar9170_nag_limiter(ar
))
872 printk(KERN_ERR
"%s: frame tail is clipped.\n",
873 wiphy_name(ar
->hw
->wiphy
));
877 case AR9170_RX_STATUS_MPDU_MIDDLE
:
878 /* middle mpdus are just data */
879 if (unlikely(!ar
->rx_mpdu
.has_plcp
)) {
880 if (!ar9170_nag_limiter(ar
))
883 printk(KERN_ERR
"%s: rx stream did not start "
884 "with a first_mpdu frame tag.\n",
885 wiphy_name(ar
->hw
->wiphy
));
890 head
= &ar
->rx_mpdu
.plcp
;
893 case AR9170_RX_STATUS_MPDU_SINGLE
:
894 /* single mpdu - has plcp (head) and phy status (tail) */
897 mpdu_len
-= sizeof(struct ar9170_rx_head
);
898 mpdu_len
-= sizeof(struct ar9170_rx_phystatus
);
900 buf
+= sizeof(struct ar9170_rx_head
);
901 phy
= (void *)(buf
+ mpdu_len
);
909 if (unlikely(mpdu_len
< FCS_LEN
))
912 memset(&status
, 0, sizeof(status
));
913 if (unlikely(ar9170_rx_mac_status(ar
, head
, mac
, &status
)))
917 ar9170_rx_phy_status(ar
, phy
, &status
);
919 skb
= ar9170_rx_copy_data(buf
, mpdu_len
);
921 ieee80211_rx_irqsafe(ar
->hw
, skb
, &status
);
924 void ar9170_rx(struct ar9170
*ar
, struct sk_buff
*skb
)
926 unsigned int i
, tlen
, resplen
, wlen
= 0, clen
= 0;
933 clen
= tbuf
[1] << 8 | tbuf
[0];
934 wlen
= ALIGN(clen
, 4);
936 /* check if this is stream has a valid tag.*/
937 if (tbuf
[2] != 0 || tbuf
[3] != 0x4e) {
939 * TODO: handle the highly unlikely event that the
940 * corrupted stream has the TAG at the right position.
943 /* check if the frame can be repaired. */
944 if (!ar
->rx_failover_missing
) {
945 /* this is no "short read". */
946 if (ar9170_nag_limiter(ar
)) {
947 printk(KERN_ERR
"%s: missing tag!\n",
948 wiphy_name(ar
->hw
->wiphy
));
954 if (ar
->rx_failover_missing
> tlen
) {
955 if (ar9170_nag_limiter(ar
)) {
956 printk(KERN_ERR
"%s: possible multi "
957 "stream corruption!\n",
958 wiphy_name(ar
->hw
->wiphy
));
964 memcpy(skb_put(ar
->rx_failover
, tlen
), tbuf
, tlen
);
965 ar
->rx_failover_missing
-= tlen
;
967 if (ar
->rx_failover_missing
<= 0) {
969 * nested ar9170_rx call!
970 * termination is guranteed, even when the
971 * combined frame also have a element with
975 ar
->rx_failover_missing
= 0;
976 ar9170_rx(ar
, ar
->rx_failover
);
978 skb_reset_tail_pointer(ar
->rx_failover
);
979 skb_trim(ar
->rx_failover
, 0);
985 /* check if stream is clipped */
986 if (wlen
> tlen
- 4) {
987 if (ar
->rx_failover_missing
) {
988 /* TODO: handle double stream corruption. */
989 if (ar9170_nag_limiter(ar
)) {
990 printk(KERN_ERR
"%s: double rx stream "
992 wiphy_name(ar
->hw
->wiphy
));
999 * save incomplete data set.
1000 * the firmware will resend the missing bits when
1001 * the rx - descriptor comes round again.
1004 memcpy(skb_put(ar
->rx_failover
, tlen
), tbuf
, tlen
);
1005 ar
->rx_failover_missing
= clen
- tlen
;
1015 /* weird thing, but this is the same in the original driver */
1016 while (resplen
> 2 && i
< 12 &&
1017 respbuf
[0] == 0xff && respbuf
[1] == 0xff) {
1026 /* found the 6 * 0xffff marker? */
1028 ar9170_handle_command_response(ar
, respbuf
, resplen
);
1030 ar9170_handle_mpdu(ar
, respbuf
, clen
);
1034 if (net_ratelimit())
1035 printk(KERN_ERR
"%s: %d bytes of unprocessed "
1036 "data left in rx stream!\n",
1037 wiphy_name(ar
->hw
->wiphy
), tlen
);
1045 printk(KERN_ERR
"%s: damaged RX stream data [want:%d, "
1046 "data:%d, rx:%d, pending:%d ]\n",
1047 wiphy_name(ar
->hw
->wiphy
), clen
, wlen
, tlen
,
1048 ar
->rx_failover_missing
);
1050 if (ar
->rx_failover_missing
)
1051 print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET
,
1052 ar
->rx_failover
->data
,
1053 ar
->rx_failover
->len
);
1055 print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET
,
1056 skb
->data
, skb
->len
);
1058 printk(KERN_ERR
"%s: please check your hardware and cables, if "
1059 "you see this message frequently.\n",
1060 wiphy_name(ar
->hw
->wiphy
));
1063 if (ar
->rx_failover_missing
) {
1064 skb_reset_tail_pointer(ar
->rx_failover
);
1065 skb_trim(ar
->rx_failover
, 0);
1066 ar
->rx_failover_missing
= 0;
1070 #define AR9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
1072 queue.aifs = ai_fs; \
1073 queue.cw_min = cwmin; \
1074 queue.cw_max = cwmax; \
1075 queue.txop = _txop; \
1078 static int ar9170_op_start(struct ieee80211_hw
*hw
)
1080 struct ar9170
*ar
= hw
->priv
;
1083 mutex_lock(&ar
->mutex
);
1085 ar
->filter_changed
= 0;
1087 /* reinitialize queues statistics */
1088 memset(&ar
->tx_stats
, 0, sizeof(ar
->tx_stats
));
1089 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++)
1090 ar
->tx_stats
[i
].limit
= AR9170_TXQ_DEPTH
;
1092 /* reset QoS defaults */
1093 AR9170_FILL_QUEUE(ar
->edcf
[0], 3, 15, 1023, 0); /* BEST EFFORT*/
1094 AR9170_FILL_QUEUE(ar
->edcf
[1], 7, 15, 1023, 0); /* BACKGROUND */
1095 AR9170_FILL_QUEUE(ar
->edcf
[2], 2, 7, 15, 94); /* VIDEO */
1096 AR9170_FILL_QUEUE(ar
->edcf
[3], 2, 3, 7, 47); /* VOICE */
1097 AR9170_FILL_QUEUE(ar
->edcf
[4], 2, 3, 7, 0); /* SPECIAL */
1099 ar
->bad_hw_nagger
= jiffies
;
1105 err
= ar9170_init_mac(ar
);
1109 err
= ar9170_set_qos(ar
);
1113 err
= ar9170_init_phy(ar
, IEEE80211_BAND_2GHZ
);
1117 err
= ar9170_init_rf(ar
);
1122 err
= ar9170_write_reg(ar
, 0x1c3d30, 0x100);
1126 ar
->state
= AR9170_STARTED
;
1129 mutex_unlock(&ar
->mutex
);
1133 static void ar9170_op_stop(struct ieee80211_hw
*hw
)
1135 struct ar9170
*ar
= hw
->priv
;
1139 ar
->state
= AR9170_IDLE
;
1141 flush_workqueue(ar
->hw
->workqueue
);
1143 cancel_delayed_work_sync(&ar
->tx_janitor
);
1144 cancel_work_sync(&ar
->filter_config_work
);
1145 cancel_work_sync(&ar
->beacon_work
);
1146 mutex_lock(&ar
->mutex
);
1148 if (IS_ACCEPTING_CMD(ar
)) {
1149 ar9170_set_leds_state(ar
, 0);
1152 ar9170_write_reg(ar
, 0x1c3d30, 0);
1156 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++) {
1157 skb_queue_purge(&ar
->tx_pending
[i
]);
1158 skb_queue_purge(&ar
->tx_status
[i
]);
1160 mutex_unlock(&ar
->mutex
);
1163 static int ar9170_tx_prepare(struct ar9170
*ar
, struct sk_buff
*skb
)
1165 struct ieee80211_hdr
*hdr
;
1166 struct ar9170_tx_control
*txc
;
1167 struct ieee80211_tx_info
*info
;
1168 struct ieee80211_tx_rate
*txrate
;
1169 struct ar9170_tx_info
*arinfo
;
1170 unsigned int queue
= skb_get_queue_mapping(skb
);
1174 BUILD_BUG_ON(sizeof(*arinfo
) > sizeof(info
->rate_driver_data
));
1176 hdr
= (void *)skb
->data
;
1177 info
= IEEE80211_SKB_CB(skb
);
1180 txc
= (void *)skb_push(skb
, sizeof(*txc
));
1182 if (info
->control
.hw_key
) {
1183 icv
= info
->control
.hw_key
->icv_len
;
1185 switch (info
->control
.hw_key
->alg
) {
1187 keytype
= AR9170_TX_MAC_ENCR_RC4
;
1190 keytype
= AR9170_TX_MAC_ENCR_RC4
;
1193 keytype
= AR9170_TX_MAC_ENCR_AES
;
1202 txc
->length
= cpu_to_le16(len
+ icv
+ 4);
1204 txc
->mac_control
= cpu_to_le16(AR9170_TX_MAC_HW_DURATION
|
1205 AR9170_TX_MAC_BACKOFF
);
1206 txc
->mac_control
|= cpu_to_le16(ar9170_qos_hwmap
[queue
] <<
1207 AR9170_TX_MAC_QOS_SHIFT
);
1208 txc
->mac_control
|= cpu_to_le16(keytype
);
1209 txc
->phy_control
= cpu_to_le32(0);
1211 if (info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
1212 txc
->mac_control
|= cpu_to_le16(AR9170_TX_MAC_NO_ACK
);
1214 txrate
= &info
->control
.rates
[0];
1215 if (txrate
->flags
& IEEE80211_TX_RC_USE_CTS_PROTECT
)
1216 txc
->mac_control
|= cpu_to_le16(AR9170_TX_MAC_PROT_CTS
);
1217 else if (txrate
->flags
& IEEE80211_TX_RC_USE_RTS_CTS
)
1218 txc
->mac_control
|= cpu_to_le16(AR9170_TX_MAC_PROT_RTS
);
1220 arinfo
= (void *)info
->rate_driver_data
;
1221 arinfo
->timeout
= jiffies
+ msecs_to_jiffies(AR9170_QUEUE_TIMEOUT
);
1223 if (!(info
->flags
& IEEE80211_TX_CTL_NO_ACK
) &&
1224 (is_valid_ether_addr(ieee80211_get_DA(hdr
)))) {
1225 if (info
->flags
& IEEE80211_TX_CTL_AMPDU
) {
1226 if (unlikely(!info
->control
.sta
))
1229 txc
->mac_control
|= cpu_to_le16(AR9170_TX_MAC_AGGR
);
1230 arinfo
->flags
= AR9170_TX_FLAG_BLOCK_ACK
;
1234 txc
->mac_control
|= cpu_to_le16(AR9170_TX_MAC_RATE_PROBE
);
1237 * Putting the QoS queue bits into an unexplored territory is
1238 * certainly not elegant.
1240 * In my defense: This idea provides a reasonable way to
1241 * smuggle valuable information to the tx_status callback.
1242 * Also, the idea behind this bit-abuse came straight from
1243 * the original driver code.
1247 cpu_to_le32(queue
<< AR9170_TX_PHY_QOS_SHIFT
);
1248 arinfo
->flags
= AR9170_TX_FLAG_WAIT_FOR_ACK
;
1250 arinfo
->flags
= AR9170_TX_FLAG_NO_ACK
;
1257 skb_pull(skb
, sizeof(*txc
));
1261 static void ar9170_tx_prepare_phy(struct ar9170
*ar
, struct sk_buff
*skb
)
1263 struct ar9170_tx_control
*txc
;
1264 struct ieee80211_tx_info
*info
;
1265 struct ieee80211_rate
*rate
= NULL
;
1266 struct ieee80211_tx_rate
*txrate
;
1269 txc
= (void *) skb
->data
;
1270 info
= IEEE80211_SKB_CB(skb
);
1271 txrate
= &info
->control
.rates
[0];
1273 if (txrate
->flags
& IEEE80211_TX_RC_GREEN_FIELD
)
1274 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_GREENFIELD
);
1276 if (txrate
->flags
& IEEE80211_TX_RC_USE_SHORT_PREAMBLE
)
1277 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE
);
1279 if (txrate
->flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)
1280 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ
);
1281 /* this works because 40 MHz is 2 and dup is 3 */
1282 if (txrate
->flags
& IEEE80211_TX_RC_DUP_DATA
)
1283 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP
);
1285 if (txrate
->flags
& IEEE80211_TX_RC_SHORT_GI
)
1286 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_SHORT_GI
);
1288 if (txrate
->flags
& IEEE80211_TX_RC_MCS
) {
1289 u32 r
= txrate
->idx
;
1292 /* heavy clip control */
1293 txc
->phy_control
|= cpu_to_le32((r
& 0x7) << 7);
1295 r
<<= AR9170_TX_PHY_MCS_SHIFT
;
1296 BUG_ON(r
& ~AR9170_TX_PHY_MCS_MASK
);
1298 txc
->phy_control
|= cpu_to_le32(r
& AR9170_TX_PHY_MCS_MASK
);
1299 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_MOD_HT
);
1301 if (txrate
->flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
) {
1302 if (info
->band
== IEEE80211_BAND_5GHZ
)
1303 txpower
= ar
->power_5G_ht40
;
1305 txpower
= ar
->power_2G_ht40
;
1307 if (info
->band
== IEEE80211_BAND_5GHZ
)
1308 txpower
= ar
->power_5G_ht20
;
1310 txpower
= ar
->power_2G_ht20
;
1313 power
= txpower
[(txrate
->idx
) & 7];
1318 u8 idx
= txrate
->idx
;
1320 if (info
->band
!= IEEE80211_BAND_2GHZ
) {
1322 txpower
= ar
->power_5G_leg
;
1323 mod
= AR9170_TX_PHY_MOD_OFDM
;
1326 txpower
= ar
->power_2G_cck
;
1327 mod
= AR9170_TX_PHY_MOD_CCK
;
1329 mod
= AR9170_TX_PHY_MOD_OFDM
;
1330 txpower
= ar
->power_2G_ofdm
;
1334 rate
= &__ar9170_ratetable
[idx
];
1336 phyrate
= rate
->hw_value
& 0xF;
1337 power
= txpower
[(rate
->hw_value
& 0x30) >> 4];
1338 phyrate
<<= AR9170_TX_PHY_MCS_SHIFT
;
1340 txc
->phy_control
|= cpu_to_le32(mod
);
1341 txc
->phy_control
|= cpu_to_le32(phyrate
);
1344 power
<<= AR9170_TX_PHY_TX_PWR_SHIFT
;
1345 power
&= AR9170_TX_PHY_TX_PWR_MASK
;
1346 txc
->phy_control
|= cpu_to_le32(power
);
1349 if (ar
->eeprom
.tx_mask
== 1) {
1350 chains
= AR9170_TX_PHY_TXCHAIN_1
;
1352 chains
= AR9170_TX_PHY_TXCHAIN_2
;
1354 /* >= 36M legacy OFDM - use only one chain */
1355 if (rate
&& rate
->bitrate
>= 360)
1356 chains
= AR9170_TX_PHY_TXCHAIN_1
;
1358 txc
->phy_control
|= cpu_to_le32(chains
<< AR9170_TX_PHY_TXCHAIN_SHIFT
);
1361 static void ar9170_tx(struct ar9170
*ar
)
1363 struct sk_buff
*skb
;
1364 unsigned long flags
;
1365 struct ieee80211_tx_info
*info
;
1366 struct ar9170_tx_info
*arinfo
;
1367 unsigned int i
, frames
, frames_failed
, remaining_space
;
1369 bool schedule_garbagecollector
= false;
1371 BUILD_BUG_ON(sizeof(*arinfo
) > sizeof(info
->rate_driver_data
));
1373 if (unlikely(!IS_STARTED(ar
)))
1376 remaining_space
= AR9170_TX_MAX_PENDING
;
1378 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++) {
1379 spin_lock_irqsave(&ar
->tx_stats_lock
, flags
);
1380 if (ar
->tx_stats
[i
].len
>= ar
->tx_stats
[i
].limit
) {
1381 #ifdef AR9170_QUEUE_DEBUG
1382 printk(KERN_DEBUG
"%s: queue %d full\n",
1383 wiphy_name(ar
->hw
->wiphy
), i
);
1385 __ar9170_dump_txstats(ar
);
1386 printk(KERN_DEBUG
"stuck frames: ===> \n");
1387 ar9170_dump_txqueue(ar
, &ar
->tx_pending
[i
]);
1388 ar9170_dump_txqueue(ar
, &ar
->tx_status
[i
]);
1389 #endif /* AR9170_QUEUE_DEBUG */
1390 ieee80211_stop_queue(ar
->hw
, i
);
1391 spin_unlock_irqrestore(&ar
->tx_stats_lock
, flags
);
1395 frames
= min(ar
->tx_stats
[i
].limit
- ar
->tx_stats
[i
].len
,
1396 skb_queue_len(&ar
->tx_pending
[i
]));
1398 if (remaining_space
< frames
) {
1399 #ifdef AR9170_QUEUE_DEBUG
1400 printk(KERN_DEBUG
"%s: tx quota reached queue:%d, "
1401 "remaining slots:%d, needed:%d\n",
1402 wiphy_name(ar
->hw
->wiphy
), i
, remaining_space
,
1405 ar9170_dump_txstats(ar
);
1406 #endif /* AR9170_QUEUE_DEBUG */
1407 frames
= remaining_space
;
1410 ar
->tx_stats
[i
].len
+= frames
;
1411 ar
->tx_stats
[i
].count
+= frames
;
1412 spin_unlock_irqrestore(&ar
->tx_stats_lock
, flags
);
1419 skb
= skb_dequeue(&ar
->tx_pending
[i
]);
1420 if (unlikely(!skb
)) {
1421 frames_failed
+= frames
;
1426 info
= IEEE80211_SKB_CB(skb
);
1427 arinfo
= (void *) info
->rate_driver_data
;
1429 /* TODO: cancel stuck frames */
1430 arinfo
->timeout
= jiffies
+
1431 msecs_to_jiffies(AR9170_TX_TIMEOUT
);
1433 #ifdef AR9170_QUEUE_DEBUG
1434 printk(KERN_DEBUG
"%s: send frame q:%d =>\n",
1435 wiphy_name(ar
->hw
->wiphy
), i
);
1436 ar9170_print_txheader(ar
, skb
);
1437 #endif /* AR9170_QUEUE_DEBUG */
1439 err
= ar
->tx(ar
, skb
);
1440 if (unlikely(err
)) {
1442 dev_kfree_skb_any(skb
);
1445 schedule_garbagecollector
= true;
1451 #ifdef AR9170_QUEUE_DEBUG
1452 printk(KERN_DEBUG
"%s: ar9170_tx report for queue %d\n",
1453 wiphy_name(ar
->hw
->wiphy
), i
);
1455 printk(KERN_DEBUG
"%s: unprocessed pending frames left:\n",
1456 wiphy_name(ar
->hw
->wiphy
));
1457 ar9170_dump_txqueue(ar
, &ar
->tx_pending
[i
]);
1458 #endif /* AR9170_QUEUE_DEBUG */
1460 if (unlikely(frames_failed
)) {
1461 #ifdef AR9170_QUEUE_DEBUG
1462 printk(KERN_DEBUG
"%s: frames failed =>\n",
1463 wiphy_name(ar
->hw
->wiphy
), frames_failed
);
1464 #endif /* AR9170_QUEUE_DEBUG */
1466 spin_lock_irqsave(&ar
->tx_stats_lock
, flags
);
1467 ar
->tx_stats
[i
].len
-= frames_failed
;
1468 ar
->tx_stats
[i
].count
-= frames_failed
;
1469 ieee80211_wake_queue(ar
->hw
, i
);
1470 spin_unlock_irqrestore(&ar
->tx_stats_lock
, flags
);
1474 if (schedule_garbagecollector
)
1475 queue_delayed_work(ar
->hw
->workqueue
,
1477 msecs_to_jiffies(AR9170_JANITOR_DELAY
));
1480 int ar9170_op_tx(struct ieee80211_hw
*hw
, struct sk_buff
*skb
)
1482 struct ar9170
*ar
= hw
->priv
;
1483 struct ieee80211_tx_info
*info
;
1485 if (unlikely(!IS_STARTED(ar
)))
1488 if (unlikely(ar9170_tx_prepare(ar
, skb
)))
1491 info
= IEEE80211_SKB_CB(skb
);
1492 if (info
->flags
& IEEE80211_TX_CTL_AMPDU
) {
1493 /* drop frame, we do not allow TX A-MPDU aggregation yet. */
1496 unsigned int queue
= skb_get_queue_mapping(skb
);
1498 ar9170_tx_prepare_phy(ar
, skb
);
1499 skb_queue_tail(&ar
->tx_pending
[queue
], skb
);
1503 return NETDEV_TX_OK
;
1506 dev_kfree_skb_any(skb
);
1507 return NETDEV_TX_OK
;
1510 static int ar9170_op_add_interface(struct ieee80211_hw
*hw
,
1511 struct ieee80211_if_init_conf
*conf
)
1513 struct ar9170
*ar
= hw
->priv
;
1516 mutex_lock(&ar
->mutex
);
1523 ar
->vif
= conf
->vif
;
1524 memcpy(ar
->mac_addr
, conf
->mac_addr
, ETH_ALEN
);
1526 if (modparam_nohwcrypt
|| (ar
->vif
->type
!= NL80211_IFTYPE_STATION
)) {
1527 ar
->rx_software_decryption
= true;
1528 ar
->disable_offload
= true;
1532 ar
->want_filter
= AR9170_MAC_REG_FTF_DEFAULTS
;
1533 err
= ar9170_update_frame_filter(ar
);
1537 err
= ar9170_set_operating_mode(ar
);
1540 mutex_unlock(&ar
->mutex
);
1544 static void ar9170_op_remove_interface(struct ieee80211_hw
*hw
,
1545 struct ieee80211_if_init_conf
*conf
)
1547 struct ar9170
*ar
= hw
->priv
;
1549 mutex_lock(&ar
->mutex
);
1551 ar
->want_filter
= 0;
1552 ar9170_update_frame_filter(ar
);
1553 ar9170_set_beacon_timers(ar
);
1554 dev_kfree_skb(ar
->beacon
);
1556 ar
->sniffer_enabled
= false;
1557 ar
->rx_software_decryption
= false;
1558 ar9170_set_operating_mode(ar
);
1559 mutex_unlock(&ar
->mutex
);
1562 static int ar9170_op_config(struct ieee80211_hw
*hw
, u32 changed
)
1564 struct ar9170
*ar
= hw
->priv
;
1567 mutex_lock(&ar
->mutex
);
1569 if (changed
& IEEE80211_CONF_CHANGE_LISTEN_INTERVAL
) {
1574 if (changed
& IEEE80211_CONF_CHANGE_PS
) {
1579 if (changed
& IEEE80211_CONF_CHANGE_POWER
) {
1584 if (changed
& IEEE80211_CONF_CHANGE_RETRY_LIMITS
) {
1586 * is it long_frame_max_tx_count or short_frame_max_tx_count?
1589 err
= ar9170_set_hwretry_limit(ar
,
1590 ar
->hw
->conf
.long_frame_max_tx_count
);
1595 if (changed
& BSS_CHANGED_BEACON_INT
) {
1596 err
= ar9170_set_beacon_timers(ar
);
1601 if (changed
& IEEE80211_CONF_CHANGE_CHANNEL
) {
1603 /* adjust slot time for 5 GHz */
1604 err
= ar9170_set_slot_time(ar
);
1608 err
= ar9170_set_dyn_sifs_ack(ar
);
1612 err
= ar9170_set_channel(ar
, hw
->conf
.channel
,
1614 nl80211_to_ar9170(hw
->conf
.channel_type
));
1620 mutex_unlock(&ar
->mutex
);
1624 static void ar9170_set_filters(struct work_struct
*work
)
1626 struct ar9170
*ar
= container_of(work
, struct ar9170
,
1627 filter_config_work
);
1630 if (unlikely(!IS_STARTED(ar
)))
1633 mutex_lock(&ar
->mutex
);
1634 if (test_and_clear_bit(AR9170_FILTER_CHANGED_MODE
,
1635 &ar
->filter_changed
)) {
1636 err
= ar9170_set_operating_mode(ar
);
1641 if (test_and_clear_bit(AR9170_FILTER_CHANGED_MULTICAST
,
1642 &ar
->filter_changed
)) {
1643 err
= ar9170_update_multicast(ar
);
1648 if (test_and_clear_bit(AR9170_FILTER_CHANGED_FRAMEFILTER
,
1649 &ar
->filter_changed
)) {
1650 err
= ar9170_update_frame_filter(ar
);
1656 mutex_unlock(&ar
->mutex
);
1659 static void ar9170_op_configure_filter(struct ieee80211_hw
*hw
,
1660 unsigned int changed_flags
,
1661 unsigned int *new_flags
,
1662 int mc_count
, struct dev_mc_list
*mclist
)
1664 struct ar9170
*ar
= hw
->priv
;
1666 /* mask supported flags */
1667 *new_flags
&= FIF_ALLMULTI
| FIF_CONTROL
| FIF_BCN_PRBRESP_PROMISC
|
1668 FIF_PROMISC_IN_BSS
| FIF_FCSFAIL
| FIF_PLCPFAIL
;
1669 ar
->filter_state
= *new_flags
;
1671 * We can support more by setting the sniffer bit and
1672 * then checking the error flags, later.
1675 if (changed_flags
& FIF_ALLMULTI
) {
1676 if (*new_flags
& FIF_ALLMULTI
) {
1677 ar
->want_mc_hash
= ~0ULL;
1682 /* always get broadcast frames */
1683 mchash
= 1ULL << (0xff >> 2);
1685 for (i
= 0; i
< mc_count
; i
++) {
1686 if (WARN_ON(!mclist
))
1688 mchash
|= 1ULL << (mclist
->dmi_addr
[5] >> 2);
1689 mclist
= mclist
->next
;
1691 ar
->want_mc_hash
= mchash
;
1693 set_bit(AR9170_FILTER_CHANGED_MULTICAST
, &ar
->filter_changed
);
1696 if (changed_flags
& FIF_CONTROL
) {
1697 u32 filter
= AR9170_MAC_REG_FTF_PSPOLL
|
1698 AR9170_MAC_REG_FTF_RTS
|
1699 AR9170_MAC_REG_FTF_CTS
|
1700 AR9170_MAC_REG_FTF_ACK
|
1701 AR9170_MAC_REG_FTF_CFE
|
1702 AR9170_MAC_REG_FTF_CFE_ACK
;
1704 if (*new_flags
& FIF_CONTROL
)
1705 ar
->want_filter
= ar
->cur_filter
| filter
;
1707 ar
->want_filter
= ar
->cur_filter
& ~filter
;
1709 set_bit(AR9170_FILTER_CHANGED_FRAMEFILTER
,
1710 &ar
->filter_changed
);
1713 if (changed_flags
& FIF_PROMISC_IN_BSS
) {
1714 ar
->sniffer_enabled
= ((*new_flags
) & FIF_PROMISC_IN_BSS
) != 0;
1715 set_bit(AR9170_FILTER_CHANGED_MODE
,
1716 &ar
->filter_changed
);
1719 if (likely(IS_STARTED(ar
)))
1720 queue_work(ar
->hw
->workqueue
, &ar
->filter_config_work
);
1723 static void ar9170_op_bss_info_changed(struct ieee80211_hw
*hw
,
1724 struct ieee80211_vif
*vif
,
1725 struct ieee80211_bss_conf
*bss_conf
,
1728 struct ar9170
*ar
= hw
->priv
;
1731 mutex_lock(&ar
->mutex
);
1733 if (changed
& BSS_CHANGED_BSSID
) {
1734 memcpy(ar
->bssid
, bss_conf
->bssid
, ETH_ALEN
);
1735 err
= ar9170_set_operating_mode(ar
);
1740 if (changed
& (BSS_CHANGED_BEACON
| BSS_CHANGED_BEACON_ENABLED
)) {
1741 err
= ar9170_update_beacon(ar
);
1745 err
= ar9170_set_beacon_timers(ar
);
1750 if (changed
& BSS_CHANGED_ASSOC
) {
1751 #ifndef CONFIG_AR9170_LEDS
1752 /* enable assoc LED. */
1753 err
= ar9170_set_leds_state(ar
, bss_conf
->assoc
? 2 : 0);
1754 #endif /* CONFIG_AR9170_LEDS */
1757 if (changed
& BSS_CHANGED_BEACON_INT
) {
1758 err
= ar9170_set_beacon_timers(ar
);
1763 if (changed
& BSS_CHANGED_HT
) {
1768 if (changed
& BSS_CHANGED_ERP_SLOT
) {
1769 err
= ar9170_set_slot_time(ar
);
1774 if (changed
& BSS_CHANGED_BASIC_RATES
) {
1775 err
= ar9170_set_basic_rates(ar
);
1781 mutex_unlock(&ar
->mutex
);
1784 static u64
ar9170_op_get_tsf(struct ieee80211_hw
*hw
)
1786 struct ar9170
*ar
= hw
->priv
;
1792 mutex_lock(&ar
->mutex
);
1793 err
= ar9170_read_reg(ar
, AR9170_MAC_REG_TSF_L
, &tsf_low
);
1795 err
= ar9170_read_reg(ar
, AR9170_MAC_REG_TSF_H
, &tsf_high
);
1796 mutex_unlock(&ar
->mutex
);
1802 tsf
= (tsf
<< 32) | tsf_low
;
1806 static int ar9170_set_key(struct ieee80211_hw
*hw
, enum set_key_cmd cmd
,
1807 struct ieee80211_vif
*vif
, struct ieee80211_sta
*sta
,
1808 struct ieee80211_key_conf
*key
)
1810 struct ar9170
*ar
= hw
->priv
;
1814 if ((!ar
->vif
) || (ar
->disable_offload
))
1819 if (key
->keylen
== WLAN_KEY_LEN_WEP40
)
1820 ktype
= AR9170_ENC_ALG_WEP64
;
1822 ktype
= AR9170_ENC_ALG_WEP128
;
1825 ktype
= AR9170_ENC_ALG_TKIP
;
1828 ktype
= AR9170_ENC_ALG_AESCCMP
;
1834 mutex_lock(&ar
->mutex
);
1835 if (cmd
== SET_KEY
) {
1836 if (unlikely(!IS_STARTED(ar
))) {
1841 /* group keys need all-zeroes address */
1842 if (!(key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
1845 if (key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
) {
1846 for (i
= 0; i
< 64; i
++)
1847 if (!(ar
->usedkeys
& BIT(i
)))
1850 ar
->rx_software_decryption
= true;
1851 ar9170_set_operating_mode(ar
);
1856 i
= 64 + key
->keyidx
;
1859 key
->hw_key_idx
= i
;
1861 err
= ar9170_upload_key(ar
, i
, sta
? sta
->addr
: NULL
, ktype
, 0,
1862 key
->key
, min_t(u8
, 16, key
->keylen
));
1866 if (key
->alg
== ALG_TKIP
) {
1867 err
= ar9170_upload_key(ar
, i
, sta
? sta
->addr
: NULL
,
1868 ktype
, 1, key
->key
+ 16, 16);
1873 * hardware is not capable generating the MMIC
1874 * for fragmented frames!
1876 key
->flags
|= IEEE80211_KEY_FLAG_GENERATE_MMIC
;
1880 ar
->usedkeys
|= BIT(i
);
1882 key
->flags
|= IEEE80211_KEY_FLAG_GENERATE_IV
;
1884 if (unlikely(!IS_STARTED(ar
))) {
1885 /* The device is gone... together with the key ;-) */
1890 err
= ar9170_disable_key(ar
, key
->hw_key_idx
);
1894 if (key
->hw_key_idx
< 64) {
1895 ar
->usedkeys
&= ~BIT(key
->hw_key_idx
);
1897 err
= ar9170_upload_key(ar
, key
->hw_key_idx
, NULL
,
1898 AR9170_ENC_ALG_NONE
, 0,
1903 if (key
->alg
== ALG_TKIP
) {
1904 err
= ar9170_upload_key(ar
, key
->hw_key_idx
,
1906 AR9170_ENC_ALG_NONE
, 1,
1915 ar9170_regwrite_begin(ar
);
1916 ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_L
, ar
->usedkeys
);
1917 ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_H
, ar
->usedkeys
>> 32);
1918 ar9170_regwrite_finish();
1919 err
= ar9170_regwrite_result();
1922 mutex_unlock(&ar
->mutex
);
1927 static void ar9170_sta_notify(struct ieee80211_hw
*hw
,
1928 struct ieee80211_vif
*vif
,
1929 enum sta_notify_cmd cmd
,
1930 struct ieee80211_sta
*sta
)
1934 static int ar9170_get_stats(struct ieee80211_hw
*hw
,
1935 struct ieee80211_low_level_stats
*stats
)
1937 struct ar9170
*ar
= hw
->priv
;
1941 mutex_lock(&ar
->mutex
);
1942 err
= ar9170_read_reg(ar
, AR9170_MAC_REG_TX_RETRY
, &val
);
1943 ar
->stats
.dot11ACKFailureCount
+= val
;
1945 memcpy(stats
, &ar
->stats
, sizeof(*stats
));
1946 mutex_unlock(&ar
->mutex
);
1951 static int ar9170_get_tx_stats(struct ieee80211_hw
*hw
,
1952 struct ieee80211_tx_queue_stats
*tx_stats
)
1954 struct ar9170
*ar
= hw
->priv
;
1956 spin_lock_bh(&ar
->tx_stats_lock
);
1957 memcpy(tx_stats
, ar
->tx_stats
, sizeof(tx_stats
[0]) * hw
->queues
);
1958 spin_unlock_bh(&ar
->tx_stats_lock
);
1963 static int ar9170_conf_tx(struct ieee80211_hw
*hw
, u16 queue
,
1964 const struct ieee80211_tx_queue_params
*param
)
1966 struct ar9170
*ar
= hw
->priv
;
1969 mutex_lock(&ar
->mutex
);
1970 if (queue
< __AR9170_NUM_TXQ
) {
1971 memcpy(&ar
->edcf
[ar9170_qos_hwmap
[queue
]],
1972 param
, sizeof(*param
));
1974 ret
= ar9170_set_qos(ar
);
1979 mutex_unlock(&ar
->mutex
);
1983 static int ar9170_ampdu_action(struct ieee80211_hw
*hw
,
1984 enum ieee80211_ampdu_mlme_action action
,
1985 struct ieee80211_sta
*sta
, u16 tid
, u16
*ssn
)
1988 case IEEE80211_AMPDU_RX_START
:
1989 case IEEE80211_AMPDU_RX_STOP
:
1991 * Something goes wrong -- RX locks up
1992 * after a while of receiving aggregated
1993 * frames -- not enabling for now.
2001 static const struct ieee80211_ops ar9170_ops
= {
2002 .start
= ar9170_op_start
,
2003 .stop
= ar9170_op_stop
,
2005 .add_interface
= ar9170_op_add_interface
,
2006 .remove_interface
= ar9170_op_remove_interface
,
2007 .config
= ar9170_op_config
,
2008 .configure_filter
= ar9170_op_configure_filter
,
2009 .conf_tx
= ar9170_conf_tx
,
2010 .bss_info_changed
= ar9170_op_bss_info_changed
,
2011 .get_tsf
= ar9170_op_get_tsf
,
2012 .set_key
= ar9170_set_key
,
2013 .sta_notify
= ar9170_sta_notify
,
2014 .get_stats
= ar9170_get_stats
,
2015 .get_tx_stats
= ar9170_get_tx_stats
,
2016 .ampdu_action
= ar9170_ampdu_action
,
2019 void *ar9170_alloc(size_t priv_size
)
2021 struct ieee80211_hw
*hw
;
2023 struct sk_buff
*skb
;
2027 * this buffer is used for rx stream reconstruction.
2028 * Under heavy load this device (or the transport layer?)
2029 * tends to split the streams into seperate rx descriptors.
2032 skb
= __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE
, GFP_KERNEL
);
2036 hw
= ieee80211_alloc_hw(priv_size
, &ar9170_ops
);
2042 ar
->rx_failover
= skb
;
2044 mutex_init(&ar
->mutex
);
2045 spin_lock_init(&ar
->cmdlock
);
2046 spin_lock_init(&ar
->tx_stats_lock
);
2047 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++) {
2048 skb_queue_head_init(&ar
->tx_status
[i
]);
2049 skb_queue_head_init(&ar
->tx_pending
[i
]);
2051 ar9170_rx_reset_rx_mpdu(ar
);
2052 INIT_WORK(&ar
->filter_config_work
, ar9170_set_filters
);
2053 INIT_WORK(&ar
->beacon_work
, ar9170_new_beacon
);
2054 INIT_DELAYED_WORK(&ar
->tx_janitor
, ar9170_tx_janitor
);
2056 /* all hw supports 2.4 GHz, so set channel to 1 by default */
2057 ar
->channel
= &ar9170_2ghz_chantable
[0];
2059 /* first part of wiphy init */
2060 ar
->hw
->wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
) |
2061 BIT(NL80211_IFTYPE_WDS
) |
2062 BIT(NL80211_IFTYPE_ADHOC
);
2063 ar
->hw
->flags
|= IEEE80211_HW_RX_INCLUDES_FCS
|
2064 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING
|
2065 IEEE80211_HW_SIGNAL_DBM
|
2066 IEEE80211_HW_NOISE_DBM
;
2068 ar
->hw
->queues
= __AR9170_NUM_TXQ
;
2069 ar
->hw
->extra_tx_headroom
= 8;
2070 ar
->hw
->sta_data_size
= sizeof(struct ar9170_sta_info
);
2072 ar
->hw
->max_rates
= 1;
2073 ar
->hw
->max_rate_tries
= 3;
2075 for (i
= 0; i
< ARRAY_SIZE(ar
->noise
); i
++)
2076 ar
->noise
[i
] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
2082 return ERR_PTR(-ENOMEM
);
2085 static int ar9170_read_eeprom(struct ar9170
*ar
)
2087 #define RW 8 /* number of words to read at once */
2088 #define RB (sizeof(u32) * RW)
2089 DECLARE_MAC_BUF(mbuf
);
2090 u8
*eeprom
= (void *)&ar
->eeprom
;
2091 u8
*addr
= ar
->eeprom
.mac_address
;
2093 int i
, j
, err
, bands
= 0;
2095 BUILD_BUG_ON(sizeof(ar
->eeprom
) & 3);
2097 BUILD_BUG_ON(RB
> AR9170_MAX_CMD_LEN
- 4);
2099 /* don't want to handle trailing remains */
2100 BUILD_BUG_ON(sizeof(ar
->eeprom
) % RB
);
2103 for (i
= 0; i
< sizeof(ar
->eeprom
)/RB
; i
++) {
2104 for (j
= 0; j
< RW
; j
++)
2105 offsets
[j
] = cpu_to_le32(AR9170_EEPROM_START
+
2108 err
= ar
->exec_cmd(ar
, AR9170_CMD_RREG
,
2109 RB
, (u8
*) &offsets
,
2110 RB
, eeprom
+ RB
* i
);
2118 if (ar
->eeprom
.length
== cpu_to_le16(0xFFFF))
2121 if (ar
->eeprom
.operating_flags
& AR9170_OPFLAG_2GHZ
) {
2122 ar
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] = &ar9170_band_2GHz
;
2125 if (ar
->eeprom
.operating_flags
& AR9170_OPFLAG_5GHZ
) {
2126 ar
->hw
->wiphy
->bands
[IEEE80211_BAND_5GHZ
] = &ar9170_band_5GHz
;
2130 * I measured this, a bandswitch takes roughly
2131 * 135 ms and a frequency switch about 80.
2133 * FIXME: measure these values again once EEPROM settings
2134 * are used, that will influence them!
2137 ar
->hw
->channel_change_time
= 135 * 1000;
2139 ar
->hw
->channel_change_time
= 80 * 1000;
2141 ar
->regulatory
.current_rd
= le16_to_cpu(ar
->eeprom
.reg_domain
[0]);
2142 ar
->regulatory
.current_rd_ext
= le16_to_cpu(ar
->eeprom
.reg_domain
[1]);
2144 /* second part of wiphy init */
2145 SET_IEEE80211_PERM_ADDR(ar
->hw
, addr
);
2147 return bands
? 0 : -EINVAL
;
2150 static int ar9170_reg_notifier(struct wiphy
*wiphy
,
2151 struct regulatory_request
*request
)
2153 struct ieee80211_hw
*hw
= wiphy_to_ieee80211_hw(wiphy
);
2154 struct ar9170
*ar
= hw
->priv
;
2156 return ath_reg_notifier_apply(wiphy
, request
, &ar
->regulatory
);
2159 int ar9170_register(struct ar9170
*ar
, struct device
*pdev
)
2163 /* try to read EEPROM, init MAC addr */
2164 err
= ar9170_read_eeprom(ar
);
2168 err
= ath_regd_init(&ar
->regulatory
, ar
->hw
->wiphy
,
2169 ar9170_reg_notifier
);
2173 err
= ieee80211_register_hw(ar
->hw
);
2177 if (!ath_is_world_regd(&ar
->regulatory
))
2178 regulatory_hint(ar
->hw
->wiphy
, ar
->regulatory
.alpha2
);
2180 err
= ar9170_init_leds(ar
);
2184 #ifdef CONFIG_AR9170_LEDS
2185 err
= ar9170_register_leds(ar
);
2188 #endif /* CONFIG_AR9170_LEDS */
2190 dev_info(pdev
, "Atheros AR9170 is registered as '%s'\n",
2191 wiphy_name(ar
->hw
->wiphy
));
2196 ieee80211_unregister_hw(ar
->hw
);
2202 void ar9170_unregister(struct ar9170
*ar
)
2204 #ifdef CONFIG_AR9170_LEDS
2205 ar9170_unregister_leds(ar
);
2206 #endif /* CONFIG_AR9170_LEDS */
2208 kfree_skb(ar
->rx_failover
);
2209 ieee80211_unregister_hw(ar
->hw
);
2210 mutex_destroy(&ar
->mutex
);