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/slab.h>
42 #include <linux/module.h>
43 #include <linux/etherdevice.h>
44 #include <net/mac80211.h>
49 static int modparam_nohwcrypt
;
50 module_param_named(nohwcrypt
, modparam_nohwcrypt
, bool, S_IRUGO
);
51 MODULE_PARM_DESC(nohwcrypt
, "Disable hardware encryption.");
53 #define RATE(_bitrate, _hw_rate, _txpidx, _flags) { \
54 .bitrate = (_bitrate), \
56 .hw_value = (_hw_rate) | (_txpidx) << 4, \
59 static struct ieee80211_rate __ar9170_ratetable
[] = {
61 RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE
),
62 RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE
),
63 RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE
),
75 #define ar9170_g_ratetable (__ar9170_ratetable + 0)
76 #define ar9170_g_ratetable_size 12
77 #define ar9170_a_ratetable (__ar9170_ratetable + 4)
78 #define ar9170_a_ratetable_size 8
81 * NB: The hw_value is used as an index into the ar9170_phy_freq_params
82 * array in phy.c so that we don't have to do frequency lookups!
84 #define CHAN(_freq, _idx) { \
85 .center_freq = (_freq), \
87 .max_power = 18, /* XXX */ \
90 static struct ieee80211_channel ar9170_2ghz_chantable
[] = {
107 static struct ieee80211_channel ar9170_5ghz_chantable
[] = {
146 #define AR9170_HT_CAP \
148 .ht_supported = true, \
149 .cap = IEEE80211_HT_CAP_MAX_AMSDU | \
150 IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
151 IEEE80211_HT_CAP_SGI_40 | \
152 IEEE80211_HT_CAP_GRN_FLD | \
153 IEEE80211_HT_CAP_DSSSCCK40 | \
154 IEEE80211_HT_CAP_SM_PS, \
156 .ampdu_density = 6, \
158 .rx_mask = { 0xff, 0xff, 0, 0, 0x1, 0, 0, 0, 0, 0, }, \
159 .rx_highest = cpu_to_le16(300), \
160 .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
164 static struct ieee80211_supported_band ar9170_band_2GHz
= {
165 .channels
= ar9170_2ghz_chantable
,
166 .n_channels
= ARRAY_SIZE(ar9170_2ghz_chantable
),
167 .bitrates
= ar9170_g_ratetable
,
168 .n_bitrates
= ar9170_g_ratetable_size
,
169 .ht_cap
= AR9170_HT_CAP
,
172 static struct ieee80211_supported_band ar9170_band_5GHz
= {
173 .channels
= ar9170_5ghz_chantable
,
174 .n_channels
= ARRAY_SIZE(ar9170_5ghz_chantable
),
175 .bitrates
= ar9170_a_ratetable
,
176 .n_bitrates
= ar9170_a_ratetable_size
,
177 .ht_cap
= AR9170_HT_CAP
,
180 static void ar9170_tx(struct ar9170
*ar
);
182 static inline u16
ar9170_get_seq_h(struct ieee80211_hdr
*hdr
)
184 return le16_to_cpu(hdr
->seq_ctrl
) >> 4;
187 static inline u16
ar9170_get_seq(struct sk_buff
*skb
)
189 struct ar9170_tx_control
*txc
= (void *) skb
->data
;
190 return ar9170_get_seq_h((void *) txc
->frame_data
);
193 #ifdef AR9170_QUEUE_DEBUG
194 static void ar9170_print_txheader(struct ar9170
*ar
, struct sk_buff
*skb
)
196 struct ar9170_tx_control
*txc
= (void *) skb
->data
;
197 struct ieee80211_tx_info
*txinfo
= IEEE80211_SKB_CB(skb
);
198 struct ar9170_tx_info
*arinfo
= (void *) txinfo
->rate_driver_data
;
199 struct ieee80211_hdr
*hdr
= (void *) txc
->frame_data
;
201 wiphy_debug(ar
->hw
->wiphy
,
202 "=> FRAME [skb:%p, q:%d, DA:[%pM] s:%d "
203 "mac_ctrl:%04x, phy_ctrl:%08x, timeout:[%d ms]]\n",
204 skb
, skb_get_queue_mapping(skb
),
205 ieee80211_get_DA(hdr
), ar9170_get_seq_h(hdr
),
206 le16_to_cpu(txc
->mac_control
), le32_to_cpu(txc
->phy_control
),
207 jiffies_to_msecs(arinfo
->timeout
- jiffies
));
210 static void __ar9170_dump_txqueue(struct ar9170
*ar
,
211 struct sk_buff_head
*queue
)
216 printk(KERN_DEBUG
"---[ cut here ]---\n");
217 wiphy_debug(ar
->hw
->wiphy
, "%d entries in queue.\n",
218 skb_queue_len(queue
));
220 skb_queue_walk(queue
, skb
) {
221 printk(KERN_DEBUG
"index:%d =>\n", i
++);
222 ar9170_print_txheader(ar
, skb
);
224 if (i
!= skb_queue_len(queue
))
225 printk(KERN_DEBUG
"WARNING: queue frame counter "
226 "mismatch %d != %d\n", skb_queue_len(queue
), i
);
227 printk(KERN_DEBUG
"---[ end ]---\n");
229 #endif /* AR9170_QUEUE_DEBUG */
231 #ifdef AR9170_QUEUE_DEBUG
232 static void ar9170_dump_txqueue(struct ar9170
*ar
,
233 struct sk_buff_head
*queue
)
237 spin_lock_irqsave(&queue
->lock
, flags
);
238 __ar9170_dump_txqueue(ar
, queue
);
239 spin_unlock_irqrestore(&queue
->lock
, flags
);
241 #endif /* AR9170_QUEUE_DEBUG */
243 #ifdef AR9170_QUEUE_STOP_DEBUG
244 static void __ar9170_dump_txstats(struct ar9170
*ar
)
248 wiphy_debug(ar
->hw
->wiphy
, "qos queue stats\n");
250 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++)
251 wiphy_debug(ar
->hw
->wiphy
,
252 "queue:%d limit:%d len:%d waitack:%d stopped:%d\n",
253 i
, ar
->tx_stats
[i
].limit
, ar
->tx_stats
[i
].len
,
254 skb_queue_len(&ar
->tx_status
[i
]),
255 ieee80211_queue_stopped(ar
->hw
, i
));
257 #endif /* AR9170_QUEUE_STOP_DEBUG */
259 /* caller must guarantee exclusive access for _bin_ queue. */
260 static void ar9170_recycle_expired(struct ar9170
*ar
,
261 struct sk_buff_head
*queue
,
262 struct sk_buff_head
*bin
)
264 struct sk_buff
*skb
, *old
= NULL
;
267 spin_lock_irqsave(&queue
->lock
, flags
);
268 while ((skb
= skb_peek(queue
))) {
269 struct ieee80211_tx_info
*txinfo
;
270 struct ar9170_tx_info
*arinfo
;
272 txinfo
= IEEE80211_SKB_CB(skb
);
273 arinfo
= (void *) txinfo
->rate_driver_data
;
275 if (time_is_before_jiffies(arinfo
->timeout
)) {
276 #ifdef AR9170_QUEUE_DEBUG
277 wiphy_debug(ar
->hw
->wiphy
,
278 "[%ld > %ld] frame expired => recycle\n",
279 jiffies
, arinfo
->timeout
);
280 ar9170_print_txheader(ar
, skb
);
281 #endif /* AR9170_QUEUE_DEBUG */
282 __skb_unlink(skb
, queue
);
283 __skb_queue_tail(bin
, skb
);
288 if (unlikely(old
== skb
)) {
289 /* bail out - queue is shot. */
296 spin_unlock_irqrestore(&queue
->lock
, flags
);
299 static void ar9170_tx_status(struct ar9170
*ar
, struct sk_buff
*skb
,
302 struct ieee80211_tx_info
*txinfo
;
303 unsigned int retries
= 0;
305 txinfo
= IEEE80211_SKB_CB(skb
);
306 ieee80211_tx_info_clear_status(txinfo
);
309 case AR9170_TX_STATUS_RETRY
:
311 case AR9170_TX_STATUS_COMPLETE
:
312 txinfo
->flags
|= IEEE80211_TX_STAT_ACK
;
315 case AR9170_TX_STATUS_FAILED
:
316 retries
= ar
->hw
->conf
.long_frame_max_tx_count
;
320 wiphy_err(ar
->hw
->wiphy
,
321 "invalid tx_status response (%x)\n", tx_status
);
325 txinfo
->status
.rates
[0].count
= retries
+ 1;
326 skb_pull(skb
, sizeof(struct ar9170_tx_control
));
327 ieee80211_tx_status_irqsafe(ar
->hw
, skb
);
330 void ar9170_tx_callback(struct ar9170
*ar
, struct sk_buff
*skb
)
332 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
333 struct ar9170_tx_info
*arinfo
= (void *) info
->rate_driver_data
;
334 unsigned int queue
= skb_get_queue_mapping(skb
);
337 spin_lock_irqsave(&ar
->tx_stats_lock
, flags
);
338 ar
->tx_stats
[queue
].len
--;
340 if (ar
->tx_stats
[queue
].len
< AR9170_NUM_TX_LIMIT_SOFT
) {
341 #ifdef AR9170_QUEUE_STOP_DEBUG
342 wiphy_debug(ar
->hw
->wiphy
, "wake queue %d\n", queue
);
343 __ar9170_dump_txstats(ar
);
344 #endif /* AR9170_QUEUE_STOP_DEBUG */
345 ieee80211_wake_queue(ar
->hw
, queue
);
347 spin_unlock_irqrestore(&ar
->tx_stats_lock
, flags
);
349 if (info
->flags
& IEEE80211_TX_CTL_NO_ACK
) {
350 ar9170_tx_status(ar
, skb
, AR9170_TX_STATUS_FAILED
);
352 arinfo
->timeout
= jiffies
+
353 msecs_to_jiffies(AR9170_TX_TIMEOUT
);
355 skb_queue_tail(&ar
->tx_status
[queue
], skb
);
358 if (!ar
->tx_stats
[queue
].len
&&
359 !skb_queue_empty(&ar
->tx_pending
[queue
])) {
364 static struct sk_buff
*ar9170_get_queued_skb(struct ar9170
*ar
,
366 struct sk_buff_head
*queue
,
373 * Unfortunately, the firmware does not tell to which (queued) frame
374 * this transmission status report belongs to.
376 * So we have to make risky guesses - with the scarce information
377 * the firmware provided (-> destination MAC, and phy_control) -
378 * and hope that we picked the right one...
381 spin_lock_irqsave(&queue
->lock
, flags
);
382 skb_queue_walk(queue
, skb
) {
383 struct ar9170_tx_control
*txc
= (void *) skb
->data
;
384 struct ieee80211_hdr
*hdr
= (void *) txc
->frame_data
;
387 if (mac
&& compare_ether_addr(ieee80211_get_DA(hdr
), mac
)) {
388 #ifdef AR9170_QUEUE_DEBUG
389 wiphy_debug(ar
->hw
->wiphy
,
390 "skip frame => da %pm != %pm\n",
391 mac
, ieee80211_get_DA(hdr
));
392 ar9170_print_txheader(ar
, skb
);
393 #endif /* AR9170_QUEUE_DEBUG */
397 r
= (le32_to_cpu(txc
->phy_control
) & AR9170_TX_PHY_MCS_MASK
) >>
398 AR9170_TX_PHY_MCS_SHIFT
;
400 if ((rate
!= AR9170_TX_INVALID_RATE
) && (r
!= rate
)) {
401 #ifdef AR9170_QUEUE_DEBUG
402 wiphy_debug(ar
->hw
->wiphy
,
403 "skip frame => rate %d != %d\n", rate
, r
);
404 ar9170_print_txheader(ar
, skb
);
405 #endif /* AR9170_QUEUE_DEBUG */
409 __skb_unlink(skb
, queue
);
410 spin_unlock_irqrestore(&queue
->lock
, flags
);
414 #ifdef AR9170_QUEUE_DEBUG
415 wiphy_err(ar
->hw
->wiphy
,
416 "ESS:[%pM] does not have any outstanding frames in queue.\n",
418 __ar9170_dump_txqueue(ar
, queue
);
419 #endif /* AR9170_QUEUE_DEBUG */
420 spin_unlock_irqrestore(&queue
->lock
, flags
);
426 * This worker tries to keeps an maintain tx_status queues.
427 * So we can guarantee that incoming tx_status reports are
428 * actually for a pending frame.
431 static void ar9170_tx_janitor(struct work_struct
*work
)
433 struct ar9170
*ar
= container_of(work
, struct ar9170
,
435 struct sk_buff_head waste
;
437 bool resched
= false;
439 if (unlikely(!IS_STARTED(ar
)))
442 skb_queue_head_init(&waste
);
444 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++) {
445 #ifdef AR9170_QUEUE_DEBUG
446 wiphy_debug(ar
->hw
->wiphy
, "garbage collector scans queue:%d\n",
448 ar9170_dump_txqueue(ar
, &ar
->tx_pending
[i
]);
449 ar9170_dump_txqueue(ar
, &ar
->tx_status
[i
]);
450 #endif /* AR9170_QUEUE_DEBUG */
452 ar9170_recycle_expired(ar
, &ar
->tx_status
[i
], &waste
);
453 ar9170_recycle_expired(ar
, &ar
->tx_pending
[i
], &waste
);
454 skb_queue_purge(&waste
);
456 if (!skb_queue_empty(&ar
->tx_status
[i
]) ||
457 !skb_queue_empty(&ar
->tx_pending
[i
]))
464 ieee80211_queue_delayed_work(ar
->hw
,
466 msecs_to_jiffies(AR9170_JANITOR_DELAY
));
469 void ar9170_handle_command_response(struct ar9170
*ar
, void *buf
, u32 len
)
471 struct ar9170_cmd_response
*cmd
= (void *) buf
;
473 if ((cmd
->type
& 0xc0) != 0xc0) {
474 ar
->callback_cmd(ar
, len
, buf
);
478 /* hardware event handlers */
482 * TX status notification:
483 * bytes: 0c c1 XX YY M1 M2 M3 M4 M5 M6 R4 R3 R2 R1 S2 S1
487 * M1-M6 is the MAC address
488 * R1-R4 is the transmit rate
489 * S1-S2 is the transmit status
493 u32 phy
= le32_to_cpu(cmd
->tx_status
.rate
);
494 u32 q
= (phy
& AR9170_TX_PHY_QOS_MASK
) >>
495 AR9170_TX_PHY_QOS_SHIFT
;
496 #ifdef AR9170_QUEUE_DEBUG
497 wiphy_debug(ar
->hw
->wiphy
,
498 "recv tx_status for %pm, p:%08x, q:%d\n",
499 cmd
->tx_status
.dst
, phy
, q
);
500 #endif /* AR9170_QUEUE_DEBUG */
502 skb
= ar9170_get_queued_skb(ar
, cmd
->tx_status
.dst
,
504 AR9170_TX_INVALID_RATE
);
508 ar9170_tx_status(ar
, skb
, le16_to_cpu(cmd
->tx_status
.status
));
516 if (ar
->vif
&& ar
->vif
->type
== NL80211_IFTYPE_AP
)
517 ieee80211_queue_work(ar
->hw
, &ar
->beacon_work
);
522 * (IBSS) beacon send notification
523 * bytes: 04 c2 XX YY B4 B3 B2 B1
527 * B1-B4 "should" be the number of send out beacons.
532 /* End of Atim Window */
536 /* BlockACK bitmap */
540 /* BlockACK events */
544 /* Watchdog Interrupt */
548 /* retransmission issue / SIFS/EIFS collision ?! */
553 printk(KERN_DEBUG
"ar9170 FW: %.*s\n", len
- 4,
561 printk(KERN_DEBUG
"ar9170 FW: u8: %#.2x\n",
565 printk(KERN_DEBUG
"ar9170 FW: u8: %#.4x\n",
566 le16_to_cpup((__le16
*)((char *)buf
+ 4)));
569 printk(KERN_DEBUG
"ar9170 FW: u8: %#.8x\n",
570 le32_to_cpup((__le32
*)((char *)buf
+ 4)));
573 printk(KERN_DEBUG
"ar9170 FW: u8: %#.16lx\n",
574 (unsigned long)le64_to_cpup(
575 (__le64
*)((char *)buf
+ 4)));
580 print_hex_dump_bytes("ar9170 FW:", DUMP_PREFIX_NONE
,
581 (char *)buf
+ 4, len
- 4);
585 pr_info("received unhandled event %x\n", cmd
->type
);
586 print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE
, buf
, len
);
591 static void ar9170_rx_reset_rx_mpdu(struct ar9170
*ar
)
593 memset(&ar
->rx_mpdu
.plcp
, 0, sizeof(struct ar9170_rx_head
));
594 ar
->rx_mpdu
.has_plcp
= false;
597 int ar9170_nag_limiter(struct ar9170
*ar
)
602 * we expect all sorts of errors in promiscuous mode.
603 * don't bother with it, it's OK!
605 if (ar
->sniffer_enabled
)
609 * only go for frequent errors! The hardware tends to
610 * do some stupid thing once in a while under load, in
611 * noisy environments or just for fun!
613 if (time_before(jiffies
, ar
->bad_hw_nagger
) && net_ratelimit())
614 print_message
= true;
616 print_message
= false;
618 /* reset threshold for "once in a while" */
619 ar
->bad_hw_nagger
= jiffies
+ HZ
/ 4;
620 return print_message
;
623 static int ar9170_rx_mac_status(struct ar9170
*ar
,
624 struct ar9170_rx_head
*head
,
625 struct ar9170_rx_macstatus
*mac
,
626 struct ieee80211_rx_status
*status
)
630 BUILD_BUG_ON(sizeof(struct ar9170_rx_head
) != 12);
631 BUILD_BUG_ON(sizeof(struct ar9170_rx_macstatus
) != 4);
634 if (error
& AR9170_RX_ERROR_MMIC
) {
635 status
->flag
|= RX_FLAG_MMIC_ERROR
;
636 error
&= ~AR9170_RX_ERROR_MMIC
;
639 if (error
& AR9170_RX_ERROR_PLCP
) {
640 status
->flag
|= RX_FLAG_FAILED_PLCP_CRC
;
641 error
&= ~AR9170_RX_ERROR_PLCP
;
643 if (!(ar
->filter_state
& FIF_PLCPFAIL
))
647 if (error
& AR9170_RX_ERROR_FCS
) {
648 status
->flag
|= RX_FLAG_FAILED_FCS_CRC
;
649 error
&= ~AR9170_RX_ERROR_FCS
;
651 if (!(ar
->filter_state
& FIF_FCSFAIL
))
655 decrypt
= ar9170_get_decrypt_type(mac
);
656 if (!(decrypt
& AR9170_RX_ENC_SOFTWARE
) &&
657 decrypt
!= AR9170_ENC_ALG_NONE
)
658 status
->flag
|= RX_FLAG_DECRYPTED
;
660 /* ignore wrong RA errors */
661 error
&= ~AR9170_RX_ERROR_WRONG_RA
;
663 if (error
& AR9170_RX_ERROR_DECRYPT
) {
664 error
&= ~AR9170_RX_ERROR_DECRYPT
;
666 * Rx decryption is done in place,
667 * the original data is lost anyway.
673 /* drop any other error frames */
674 if (unlikely(error
)) {
675 /* TODO: update netdevice's RX dropped/errors statistics */
677 if (ar9170_nag_limiter(ar
))
678 wiphy_debug(ar
->hw
->wiphy
,
679 "received frame with suspicious error code (%#x).\n",
685 status
->band
= ar
->channel
->band
;
686 status
->freq
= ar
->channel
->center_freq
;
688 switch (mac
->status
& AR9170_RX_STATUS_MODULATION_MASK
) {
689 case AR9170_RX_STATUS_MODULATION_CCK
:
690 if (mac
->status
& AR9170_RX_STATUS_SHORT_PREAMBLE
)
691 status
->flag
|= RX_FLAG_SHORTPRE
;
692 switch (head
->plcp
[0]) {
694 status
->rate_idx
= 0;
697 status
->rate_idx
= 1;
700 status
->rate_idx
= 2;
703 status
->rate_idx
= 3;
706 if (ar9170_nag_limiter(ar
))
707 wiphy_err(ar
->hw
->wiphy
,
708 "invalid plcp cck rate (%x).\n",
714 case AR9170_RX_STATUS_MODULATION_DUPOFDM
:
715 case AR9170_RX_STATUS_MODULATION_OFDM
:
716 switch (head
->plcp
[0] & 0xf) {
718 status
->rate_idx
= 0;
721 status
->rate_idx
= 1;
724 status
->rate_idx
= 2;
727 status
->rate_idx
= 3;
730 status
->rate_idx
= 4;
733 status
->rate_idx
= 5;
736 status
->rate_idx
= 6;
739 status
->rate_idx
= 7;
742 if (ar9170_nag_limiter(ar
))
743 wiphy_err(ar
->hw
->wiphy
,
744 "invalid plcp ofdm rate (%x).\n",
748 if (status
->band
== IEEE80211_BAND_2GHZ
)
749 status
->rate_idx
+= 4;
752 case AR9170_RX_STATUS_MODULATION_HT
:
753 if (head
->plcp
[3] & 0x80)
754 status
->flag
|= RX_FLAG_40MHZ
;
755 if (head
->plcp
[6] & 0x80)
756 status
->flag
|= RX_FLAG_SHORT_GI
;
758 status
->rate_idx
= clamp(0, 75, head
->plcp
[6] & 0x7f);
759 status
->flag
|= RX_FLAG_HT
;
763 if (ar9170_nag_limiter(ar
))
764 wiphy_err(ar
->hw
->wiphy
, "invalid modulation\n");
771 static void ar9170_rx_phy_status(struct ar9170
*ar
,
772 struct ar9170_rx_phystatus
*phy
,
773 struct ieee80211_rx_status
*status
)
777 BUILD_BUG_ON(sizeof(struct ar9170_rx_phystatus
) != 20);
779 for (i
= 0; i
< 3; i
++)
780 if (phy
->rssi
[i
] != 0x80)
781 status
->antenna
|= BIT(i
);
783 /* post-process RSSI */
784 for (i
= 0; i
< 7; i
++)
785 if (phy
->rssi
[i
] & 0x80)
786 phy
->rssi
[i
] = ((phy
->rssi
[i
] & 0x7f) + 1) & 0x7f;
788 /* TODO: we could do something with phy_errors */
789 status
->signal
= ar
->noise
[0] + phy
->rssi_combined
;
792 static struct sk_buff
*ar9170_rx_copy_data(u8
*buf
, int len
)
796 struct ieee80211_hdr
*hdr
= (void *) buf
;
798 if (ieee80211_is_data_qos(hdr
->frame_control
)) {
799 u8
*qc
= ieee80211_get_qos_ctl(hdr
);
800 reserved
+= NET_IP_ALIGN
;
802 if (*qc
& IEEE80211_QOS_CONTROL_A_MSDU_PRESENT
)
803 reserved
+= NET_IP_ALIGN
;
806 if (ieee80211_has_a4(hdr
->frame_control
))
807 reserved
+= NET_IP_ALIGN
;
809 reserved
= 32 + (reserved
& NET_IP_ALIGN
);
811 skb
= dev_alloc_skb(len
+ reserved
);
813 skb_reserve(skb
, reserved
);
814 memcpy(skb_put(skb
, len
), buf
, len
);
821 * If the frame alignment is right (or the kernel has
822 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
823 * is only a single MPDU in the USB frame, then we could
824 * submit to mac80211 the SKB directly. However, since
825 * there may be multiple packets in one SKB in stream
826 * mode, and we need to observe the proper ordering,
827 * this is non-trivial.
830 static void ar9170_handle_mpdu(struct ar9170
*ar
, u8
*buf
, int len
)
832 struct ar9170_rx_head
*head
;
833 struct ar9170_rx_macstatus
*mac
;
834 struct ar9170_rx_phystatus
*phy
= NULL
;
835 struct ieee80211_rx_status status
;
839 if (unlikely(!IS_STARTED(ar
) || len
< (sizeof(*mac
))))
843 mpdu_len
= len
- sizeof(*mac
);
845 mac
= (void *)(buf
+ mpdu_len
);
846 if (unlikely(mac
->error
& AR9170_RX_ERROR_FATAL
)) {
847 /* this frame is too damaged and can't be used - drop it */
852 switch (mac
->status
& AR9170_RX_STATUS_MPDU_MASK
) {
853 case AR9170_RX_STATUS_MPDU_FIRST
:
854 /* first mpdu packet has the plcp header */
855 if (likely(mpdu_len
>= sizeof(struct ar9170_rx_head
))) {
857 memcpy(&ar
->rx_mpdu
.plcp
, (void *) buf
,
858 sizeof(struct ar9170_rx_head
));
860 mpdu_len
-= sizeof(struct ar9170_rx_head
);
861 buf
+= sizeof(struct ar9170_rx_head
);
862 ar
->rx_mpdu
.has_plcp
= true;
864 if (ar9170_nag_limiter(ar
))
865 wiphy_err(ar
->hw
->wiphy
,
866 "plcp info is clipped.\n");
871 case AR9170_RX_STATUS_MPDU_LAST
:
872 /* last mpdu has a extra tail with phy status information */
874 if (likely(mpdu_len
>= sizeof(struct ar9170_rx_phystatus
))) {
875 mpdu_len
-= sizeof(struct ar9170_rx_phystatus
);
876 phy
= (void *)(buf
+ mpdu_len
);
878 if (ar9170_nag_limiter(ar
))
879 wiphy_err(ar
->hw
->wiphy
,
880 "frame tail is clipped.\n");
884 case AR9170_RX_STATUS_MPDU_MIDDLE
:
885 /* middle mpdus are just data */
886 if (unlikely(!ar
->rx_mpdu
.has_plcp
)) {
887 if (!ar9170_nag_limiter(ar
))
890 wiphy_err(ar
->hw
->wiphy
,
891 "rx stream did not start with a first_mpdu frame tag.\n");
896 head
= &ar
->rx_mpdu
.plcp
;
899 case AR9170_RX_STATUS_MPDU_SINGLE
:
900 /* single mpdu - has plcp (head) and phy status (tail) */
903 mpdu_len
-= sizeof(struct ar9170_rx_head
);
904 mpdu_len
-= sizeof(struct ar9170_rx_phystatus
);
906 buf
+= sizeof(struct ar9170_rx_head
);
907 phy
= (void *)(buf
+ mpdu_len
);
915 if (unlikely(mpdu_len
< FCS_LEN
))
918 memset(&status
, 0, sizeof(status
));
919 if (unlikely(ar9170_rx_mac_status(ar
, head
, mac
, &status
)))
923 ar9170_rx_phy_status(ar
, phy
, &status
);
925 skb
= ar9170_rx_copy_data(buf
, mpdu_len
);
927 memcpy(IEEE80211_SKB_RXCB(skb
), &status
, sizeof(status
));
928 ieee80211_rx_irqsafe(ar
->hw
, skb
);
932 void ar9170_rx(struct ar9170
*ar
, struct sk_buff
*skb
)
934 unsigned int i
, tlen
, resplen
, wlen
= 0, clen
= 0;
941 clen
= tbuf
[1] << 8 | tbuf
[0];
942 wlen
= ALIGN(clen
, 4);
944 /* check if this is stream has a valid tag.*/
945 if (tbuf
[2] != 0 || tbuf
[3] != 0x4e) {
947 * TODO: handle the highly unlikely event that the
948 * corrupted stream has the TAG at the right position.
951 /* check if the frame can be repaired. */
952 if (!ar
->rx_failover_missing
) {
953 /* this is no "short read". */
954 if (ar9170_nag_limiter(ar
)) {
955 wiphy_err(ar
->hw
->wiphy
,
962 if (ar
->rx_failover_missing
> tlen
) {
963 if (ar9170_nag_limiter(ar
)) {
964 wiphy_err(ar
->hw
->wiphy
,
965 "possible multi stream corruption!\n");
971 memcpy(skb_put(ar
->rx_failover
, tlen
), tbuf
, tlen
);
972 ar
->rx_failover_missing
-= tlen
;
974 if (ar
->rx_failover_missing
<= 0) {
976 * nested ar9170_rx call!
977 * termination is guranteed, even when the
978 * combined frame also have a element with
982 ar
->rx_failover_missing
= 0;
983 ar9170_rx(ar
, ar
->rx_failover
);
985 skb_reset_tail_pointer(ar
->rx_failover
);
986 skb_trim(ar
->rx_failover
, 0);
992 /* check if stream is clipped */
993 if (wlen
> tlen
- 4) {
994 if (ar
->rx_failover_missing
) {
995 /* TODO: handle double stream corruption. */
996 if (ar9170_nag_limiter(ar
)) {
997 wiphy_err(ar
->hw
->wiphy
,
998 "double rx stream corruption!\n");
1005 * save incomplete data set.
1006 * the firmware will resend the missing bits when
1007 * the rx - descriptor comes round again.
1010 memcpy(skb_put(ar
->rx_failover
, tlen
), tbuf
, tlen
);
1011 ar
->rx_failover_missing
= clen
- tlen
;
1021 /* weird thing, but this is the same in the original driver */
1022 while (resplen
> 2 && i
< 12 &&
1023 respbuf
[0] == 0xff && respbuf
[1] == 0xff) {
1032 /* found the 6 * 0xffff marker? */
1034 ar9170_handle_command_response(ar
, respbuf
, resplen
);
1036 ar9170_handle_mpdu(ar
, respbuf
, clen
);
1040 if (net_ratelimit())
1041 wiphy_err(ar
->hw
->wiphy
,
1042 "%d bytes of unprocessed data left in rx stream!\n",
1051 wiphy_err(ar
->hw
->wiphy
,
1052 "damaged RX stream data [want:%d, data:%d, rx:%d, pending:%d ]\n",
1053 clen
, wlen
, tlen
, ar
->rx_failover_missing
);
1055 if (ar
->rx_failover_missing
)
1056 print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET
,
1057 ar
->rx_failover
->data
,
1058 ar
->rx_failover
->len
);
1060 print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET
,
1061 skb
->data
, skb
->len
);
1063 wiphy_err(ar
->hw
->wiphy
,
1064 "If you see this message frequently, please check your hardware and cables.\n");
1067 if (ar
->rx_failover_missing
) {
1068 skb_reset_tail_pointer(ar
->rx_failover
);
1069 skb_trim(ar
->rx_failover
, 0);
1070 ar
->rx_failover_missing
= 0;
1074 #define AR9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
1076 queue.aifs = ai_fs; \
1077 queue.cw_min = cwmin; \
1078 queue.cw_max = cwmax; \
1079 queue.txop = _txop; \
1082 static int ar9170_op_start(struct ieee80211_hw
*hw
)
1084 struct ar9170
*ar
= hw
->priv
;
1087 mutex_lock(&ar
->mutex
);
1089 /* reinitialize queues statistics */
1090 memset(&ar
->tx_stats
, 0, sizeof(ar
->tx_stats
));
1091 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++)
1092 ar
->tx_stats
[i
].limit
= AR9170_TXQ_DEPTH
;
1094 /* reset QoS defaults */
1095 AR9170_FILL_QUEUE(ar
->edcf
[0], 3, 15, 1023, 0); /* BEST EFFORT*/
1096 AR9170_FILL_QUEUE(ar
->edcf
[1], 7, 15, 1023, 0); /* BACKGROUND */
1097 AR9170_FILL_QUEUE(ar
->edcf
[2], 2, 7, 15, 94); /* VIDEO */
1098 AR9170_FILL_QUEUE(ar
->edcf
[3], 2, 3, 7, 47); /* VOICE */
1099 AR9170_FILL_QUEUE(ar
->edcf
[4], 2, 3, 7, 0); /* SPECIAL */
1101 /* set sane AMPDU defaults */
1102 ar
->global_ampdu_density
= 6;
1103 ar
->global_ampdu_factor
= 3;
1105 ar
->bad_hw_nagger
= jiffies
;
1111 err
= ar9170_init_mac(ar
);
1115 err
= ar9170_set_qos(ar
);
1119 err
= ar9170_init_phy(ar
, IEEE80211_BAND_2GHZ
);
1123 err
= ar9170_init_rf(ar
);
1128 err
= ar9170_write_reg(ar
, 0x1c3d30, 0x100);
1132 ar
->state
= AR9170_STARTED
;
1135 mutex_unlock(&ar
->mutex
);
1139 static void ar9170_op_stop(struct ieee80211_hw
*hw
)
1141 struct ar9170
*ar
= hw
->priv
;
1145 ar
->state
= AR9170_IDLE
;
1147 cancel_delayed_work_sync(&ar
->tx_janitor
);
1148 #ifdef CONFIG_AR9170_LEDS
1149 cancel_delayed_work_sync(&ar
->led_work
);
1151 cancel_work_sync(&ar
->beacon_work
);
1153 mutex_lock(&ar
->mutex
);
1155 if (IS_ACCEPTING_CMD(ar
)) {
1156 ar9170_set_leds_state(ar
, 0);
1159 ar9170_write_reg(ar
, 0x1c3d30, 0);
1163 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++) {
1164 skb_queue_purge(&ar
->tx_pending
[i
]);
1165 skb_queue_purge(&ar
->tx_status
[i
]);
1168 mutex_unlock(&ar
->mutex
);
1171 static int ar9170_tx_prepare(struct ar9170
*ar
, struct sk_buff
*skb
)
1173 struct ieee80211_hdr
*hdr
;
1174 struct ar9170_tx_control
*txc
;
1175 struct ieee80211_tx_info
*info
;
1176 struct ieee80211_tx_rate
*txrate
;
1177 struct ar9170_tx_info
*arinfo
;
1178 unsigned int queue
= skb_get_queue_mapping(skb
);
1182 BUILD_BUG_ON(sizeof(*arinfo
) > sizeof(info
->rate_driver_data
));
1184 hdr
= (void *)skb
->data
;
1185 info
= IEEE80211_SKB_CB(skb
);
1188 txc
= (void *)skb_push(skb
, sizeof(*txc
));
1190 if (info
->control
.hw_key
) {
1191 icv
= info
->control
.hw_key
->icv_len
;
1193 switch (info
->control
.hw_key
->cipher
) {
1194 case WLAN_CIPHER_SUITE_WEP40
:
1195 case WLAN_CIPHER_SUITE_WEP104
:
1196 case WLAN_CIPHER_SUITE_TKIP
:
1197 keytype
= AR9170_TX_MAC_ENCR_RC4
;
1199 case WLAN_CIPHER_SUITE_CCMP
:
1200 keytype
= AR9170_TX_MAC_ENCR_AES
;
1209 txc
->length
= cpu_to_le16(len
+ icv
+ 4);
1211 txc
->mac_control
= cpu_to_le16(AR9170_TX_MAC_HW_DURATION
|
1212 AR9170_TX_MAC_BACKOFF
);
1213 txc
->mac_control
|= cpu_to_le16(ar9170_qos_hwmap
[queue
] <<
1214 AR9170_TX_MAC_QOS_SHIFT
);
1215 txc
->mac_control
|= cpu_to_le16(keytype
);
1216 txc
->phy_control
= cpu_to_le32(0);
1218 if (info
->flags
& IEEE80211_TX_CTL_NO_ACK
)
1219 txc
->mac_control
|= cpu_to_le16(AR9170_TX_MAC_NO_ACK
);
1221 txrate
= &info
->control
.rates
[0];
1222 if (txrate
->flags
& IEEE80211_TX_RC_USE_CTS_PROTECT
)
1223 txc
->mac_control
|= cpu_to_le16(AR9170_TX_MAC_PROT_CTS
);
1224 else if (txrate
->flags
& IEEE80211_TX_RC_USE_RTS_CTS
)
1225 txc
->mac_control
|= cpu_to_le16(AR9170_TX_MAC_PROT_RTS
);
1227 arinfo
= (void *)info
->rate_driver_data
;
1228 arinfo
->timeout
= jiffies
+ msecs_to_jiffies(AR9170_QUEUE_TIMEOUT
);
1230 if (!(info
->flags
& IEEE80211_TX_CTL_NO_ACK
) &&
1231 (is_valid_ether_addr(ieee80211_get_DA(hdr
)))) {
1234 * Putting the QoS queue bits into an unexplored territory is
1235 * certainly not elegant.
1237 * In my defense: This idea provides a reasonable way to
1238 * smuggle valuable information to the tx_status callback.
1239 * Also, the idea behind this bit-abuse came straight from
1240 * the original driver code.
1244 cpu_to_le32(queue
<< AR9170_TX_PHY_QOS_SHIFT
);
1246 txc
->mac_control
|= cpu_to_le16(AR9170_TX_MAC_RATE_PROBE
);
1252 skb_pull(skb
, sizeof(*txc
));
1256 static void ar9170_tx_prepare_phy(struct ar9170
*ar
, struct sk_buff
*skb
)
1258 struct ar9170_tx_control
*txc
;
1259 struct ieee80211_tx_info
*info
;
1260 struct ieee80211_rate
*rate
= NULL
;
1261 struct ieee80211_tx_rate
*txrate
;
1264 txc
= (void *) skb
->data
;
1265 info
= IEEE80211_SKB_CB(skb
);
1266 txrate
= &info
->control
.rates
[0];
1268 if (txrate
->flags
& IEEE80211_TX_RC_GREEN_FIELD
)
1269 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_GREENFIELD
);
1271 if (txrate
->flags
& IEEE80211_TX_RC_USE_SHORT_PREAMBLE
)
1272 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE
);
1274 if (txrate
->flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
)
1275 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ
);
1276 /* this works because 40 MHz is 2 and dup is 3 */
1277 if (txrate
->flags
& IEEE80211_TX_RC_DUP_DATA
)
1278 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP
);
1280 if (txrate
->flags
& IEEE80211_TX_RC_SHORT_GI
)
1281 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_SHORT_GI
);
1283 if (txrate
->flags
& IEEE80211_TX_RC_MCS
) {
1284 u32 r
= txrate
->idx
;
1287 /* heavy clip control */
1288 txc
->phy_control
|= cpu_to_le32((r
& 0x7) << 7);
1290 r
<<= AR9170_TX_PHY_MCS_SHIFT
;
1291 BUG_ON(r
& ~AR9170_TX_PHY_MCS_MASK
);
1293 txc
->phy_control
|= cpu_to_le32(r
& AR9170_TX_PHY_MCS_MASK
);
1294 txc
->phy_control
|= cpu_to_le32(AR9170_TX_PHY_MOD_HT
);
1296 if (txrate
->flags
& IEEE80211_TX_RC_40_MHZ_WIDTH
) {
1297 if (info
->band
== IEEE80211_BAND_5GHZ
)
1298 txpower
= ar
->power_5G_ht40
;
1300 txpower
= ar
->power_2G_ht40
;
1302 if (info
->band
== IEEE80211_BAND_5GHZ
)
1303 txpower
= ar
->power_5G_ht20
;
1305 txpower
= ar
->power_2G_ht20
;
1308 power
= txpower
[(txrate
->idx
) & 7];
1313 u8 idx
= txrate
->idx
;
1315 if (info
->band
!= IEEE80211_BAND_2GHZ
) {
1317 txpower
= ar
->power_5G_leg
;
1318 mod
= AR9170_TX_PHY_MOD_OFDM
;
1321 txpower
= ar
->power_2G_cck
;
1322 mod
= AR9170_TX_PHY_MOD_CCK
;
1324 mod
= AR9170_TX_PHY_MOD_OFDM
;
1325 txpower
= ar
->power_2G_ofdm
;
1329 rate
= &__ar9170_ratetable
[idx
];
1331 phyrate
= rate
->hw_value
& 0xF;
1332 power
= txpower
[(rate
->hw_value
& 0x30) >> 4];
1333 phyrate
<<= AR9170_TX_PHY_MCS_SHIFT
;
1335 txc
->phy_control
|= cpu_to_le32(mod
);
1336 txc
->phy_control
|= cpu_to_le32(phyrate
);
1339 power
<<= AR9170_TX_PHY_TX_PWR_SHIFT
;
1340 power
&= AR9170_TX_PHY_TX_PWR_MASK
;
1341 txc
->phy_control
|= cpu_to_le32(power
);
1344 if (ar
->eeprom
.tx_mask
== 1) {
1345 chains
= AR9170_TX_PHY_TXCHAIN_1
;
1347 chains
= AR9170_TX_PHY_TXCHAIN_2
;
1349 /* >= 36M legacy OFDM - use only one chain */
1350 if (rate
&& rate
->bitrate
>= 360)
1351 chains
= AR9170_TX_PHY_TXCHAIN_1
;
1353 txc
->phy_control
|= cpu_to_le32(chains
<< AR9170_TX_PHY_TXCHAIN_SHIFT
);
1356 static void ar9170_tx(struct ar9170
*ar
)
1358 struct sk_buff
*skb
;
1359 unsigned long flags
;
1360 struct ieee80211_tx_info
*info
;
1361 struct ar9170_tx_info
*arinfo
;
1362 unsigned int i
, frames
, frames_failed
, remaining_space
;
1364 bool schedule_garbagecollector
= false;
1366 BUILD_BUG_ON(sizeof(*arinfo
) > sizeof(info
->rate_driver_data
));
1368 if (unlikely(!IS_STARTED(ar
)))
1371 remaining_space
= AR9170_TX_MAX_PENDING
;
1373 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++) {
1374 spin_lock_irqsave(&ar
->tx_stats_lock
, flags
);
1375 frames
= min(ar
->tx_stats
[i
].limit
- ar
->tx_stats
[i
].len
,
1376 skb_queue_len(&ar
->tx_pending
[i
]));
1378 if (remaining_space
< frames
) {
1379 #ifdef AR9170_QUEUE_DEBUG
1380 wiphy_debug(ar
->hw
->wiphy
,
1381 "tx quota reached queue:%d, "
1382 "remaining slots:%d, needed:%d\n",
1383 i
, remaining_space
, frames
);
1384 #endif /* AR9170_QUEUE_DEBUG */
1385 frames
= remaining_space
;
1388 ar
->tx_stats
[i
].len
+= frames
;
1389 ar
->tx_stats
[i
].count
+= frames
;
1390 if (ar
->tx_stats
[i
].len
>= ar
->tx_stats
[i
].limit
) {
1391 #ifdef AR9170_QUEUE_DEBUG
1392 wiphy_debug(ar
->hw
->wiphy
, "queue %d full\n", i
);
1393 wiphy_debug(ar
->hw
->wiphy
, "stuck frames: ===>\n");
1394 ar9170_dump_txqueue(ar
, &ar
->tx_pending
[i
]);
1395 ar9170_dump_txqueue(ar
, &ar
->tx_status
[i
]);
1396 #endif /* AR9170_QUEUE_DEBUG */
1398 #ifdef AR9170_QUEUE_STOP_DEBUG
1399 wiphy_debug(ar
->hw
->wiphy
, "stop queue %d\n", i
);
1400 __ar9170_dump_txstats(ar
);
1401 #endif /* AR9170_QUEUE_STOP_DEBUG */
1402 ieee80211_stop_queue(ar
->hw
, i
);
1405 spin_unlock_irqrestore(&ar
->tx_stats_lock
, flags
);
1412 skb
= skb_dequeue(&ar
->tx_pending
[i
]);
1413 if (unlikely(!skb
)) {
1414 frames_failed
+= frames
;
1419 info
= IEEE80211_SKB_CB(skb
);
1420 arinfo
= (void *) info
->rate_driver_data
;
1422 /* TODO: cancel stuck frames */
1423 arinfo
->timeout
= jiffies
+
1424 msecs_to_jiffies(AR9170_TX_TIMEOUT
);
1426 #ifdef AR9170_QUEUE_DEBUG
1427 wiphy_debug(ar
->hw
->wiphy
, "send frame q:%d =>\n", i
);
1428 ar9170_print_txheader(ar
, skb
);
1429 #endif /* AR9170_QUEUE_DEBUG */
1431 err
= ar
->tx(ar
, skb
);
1432 if (unlikely(err
)) {
1434 dev_kfree_skb_any(skb
);
1437 schedule_garbagecollector
= true;
1443 #ifdef AR9170_QUEUE_DEBUG
1444 wiphy_debug(ar
->hw
->wiphy
,
1445 "ar9170_tx report for queue %d\n", i
);
1447 wiphy_debug(ar
->hw
->wiphy
,
1448 "unprocessed pending frames left:\n");
1449 ar9170_dump_txqueue(ar
, &ar
->tx_pending
[i
]);
1450 #endif /* AR9170_QUEUE_DEBUG */
1452 if (unlikely(frames_failed
)) {
1453 #ifdef AR9170_QUEUE_DEBUG
1454 wiphy_debug(ar
->hw
->wiphy
,
1455 "frames failed %d =>\n", frames_failed
);
1456 #endif /* AR9170_QUEUE_DEBUG */
1458 spin_lock_irqsave(&ar
->tx_stats_lock
, flags
);
1459 ar
->tx_stats
[i
].len
-= frames_failed
;
1460 ar
->tx_stats
[i
].count
-= frames_failed
;
1461 #ifdef AR9170_QUEUE_STOP_DEBUG
1462 wiphy_debug(ar
->hw
->wiphy
, "wake queue %d\n", i
);
1463 __ar9170_dump_txstats(ar
);
1464 #endif /* AR9170_QUEUE_STOP_DEBUG */
1465 ieee80211_wake_queue(ar
->hw
, i
);
1466 spin_unlock_irqrestore(&ar
->tx_stats_lock
, flags
);
1470 if (!schedule_garbagecollector
)
1473 ieee80211_queue_delayed_work(ar
->hw
,
1475 msecs_to_jiffies(AR9170_JANITOR_DELAY
));
1478 int ar9170_op_tx(struct ieee80211_hw
*hw
, struct sk_buff
*skb
)
1480 struct ar9170
*ar
= hw
->priv
;
1481 struct ieee80211_tx_info
*info
;
1484 if (unlikely(!IS_STARTED(ar
)))
1487 if (unlikely(ar9170_tx_prepare(ar
, skb
)))
1490 queue
= skb_get_queue_mapping(skb
);
1491 info
= IEEE80211_SKB_CB(skb
);
1492 ar9170_tx_prepare_phy(ar
, skb
);
1493 skb_queue_tail(&ar
->tx_pending
[queue
], skb
);
1496 return NETDEV_TX_OK
;
1499 dev_kfree_skb_any(skb
);
1500 return NETDEV_TX_OK
;
1503 static int ar9170_op_add_interface(struct ieee80211_hw
*hw
,
1504 struct ieee80211_vif
*vif
)
1506 struct ar9170
*ar
= hw
->priv
;
1507 struct ath_common
*common
= &ar
->common
;
1510 mutex_lock(&ar
->mutex
);
1518 memcpy(common
->macaddr
, vif
->addr
, ETH_ALEN
);
1520 if (modparam_nohwcrypt
|| (ar
->vif
->type
!= NL80211_IFTYPE_STATION
)) {
1521 ar
->rx_software_decryption
= true;
1522 ar
->disable_offload
= true;
1526 err
= ar9170_update_frame_filter(ar
, AR9170_MAC_REG_FTF_DEFAULTS
);
1530 err
= ar9170_set_operating_mode(ar
);
1533 mutex_unlock(&ar
->mutex
);
1537 static void ar9170_op_remove_interface(struct ieee80211_hw
*hw
,
1538 struct ieee80211_vif
*vif
)
1540 struct ar9170
*ar
= hw
->priv
;
1542 mutex_lock(&ar
->mutex
);
1544 ar9170_update_frame_filter(ar
, 0);
1545 ar9170_set_beacon_timers(ar
);
1546 dev_kfree_skb(ar
->beacon
);
1548 ar
->sniffer_enabled
= false;
1549 ar
->rx_software_decryption
= false;
1550 ar9170_set_operating_mode(ar
);
1551 mutex_unlock(&ar
->mutex
);
1554 static int ar9170_op_config(struct ieee80211_hw
*hw
, u32 changed
)
1556 struct ar9170
*ar
= hw
->priv
;
1559 mutex_lock(&ar
->mutex
);
1561 if (changed
& IEEE80211_CONF_CHANGE_LISTEN_INTERVAL
) {
1566 if (changed
& IEEE80211_CONF_CHANGE_PS
) {
1571 if (changed
& IEEE80211_CONF_CHANGE_POWER
) {
1576 if (changed
& IEEE80211_CONF_CHANGE_RETRY_LIMITS
) {
1578 * is it long_frame_max_tx_count or short_frame_max_tx_count?
1581 err
= ar9170_set_hwretry_limit(ar
,
1582 ar
->hw
->conf
.long_frame_max_tx_count
);
1587 if (changed
& IEEE80211_CONF_CHANGE_CHANNEL
) {
1589 /* adjust slot time for 5 GHz */
1590 err
= ar9170_set_slot_time(ar
);
1594 err
= ar9170_set_dyn_sifs_ack(ar
);
1598 err
= ar9170_set_channel(ar
, hw
->conf
.channel
,
1600 nl80211_to_ar9170(hw
->conf
.channel_type
));
1606 mutex_unlock(&ar
->mutex
);
1610 static u64
ar9170_op_prepare_multicast(struct ieee80211_hw
*hw
,
1611 struct netdev_hw_addr_list
*mc_list
)
1614 struct netdev_hw_addr
*ha
;
1616 /* always get broadcast frames */
1617 mchash
= 1ULL << (0xff >> 2);
1619 netdev_hw_addr_list_for_each(ha
, mc_list
)
1620 mchash
|= 1ULL << (ha
->addr
[5] >> 2);
1625 static void ar9170_op_configure_filter(struct ieee80211_hw
*hw
,
1626 unsigned int changed_flags
,
1627 unsigned int *new_flags
,
1630 struct ar9170
*ar
= hw
->priv
;
1632 if (unlikely(!IS_ACCEPTING_CMD(ar
)))
1635 mutex_lock(&ar
->mutex
);
1637 /* mask supported flags */
1638 *new_flags
&= FIF_ALLMULTI
| FIF_CONTROL
| FIF_BCN_PRBRESP_PROMISC
|
1639 FIF_PROMISC_IN_BSS
| FIF_FCSFAIL
| FIF_PLCPFAIL
;
1640 ar
->filter_state
= *new_flags
;
1642 * We can support more by setting the sniffer bit and
1643 * then checking the error flags, later.
1646 if (changed_flags
& FIF_ALLMULTI
&& *new_flags
& FIF_ALLMULTI
)
1649 if (multicast
!= ar
->cur_mc_hash
)
1650 ar9170_update_multicast(ar
, multicast
);
1652 if (changed_flags
& FIF_CONTROL
) {
1653 u32 filter
= AR9170_MAC_REG_FTF_PSPOLL
|
1654 AR9170_MAC_REG_FTF_RTS
|
1655 AR9170_MAC_REG_FTF_CTS
|
1656 AR9170_MAC_REG_FTF_ACK
|
1657 AR9170_MAC_REG_FTF_CFE
|
1658 AR9170_MAC_REG_FTF_CFE_ACK
;
1660 if (*new_flags
& FIF_CONTROL
)
1661 filter
|= ar
->cur_filter
;
1663 filter
&= (~ar
->cur_filter
);
1665 ar9170_update_frame_filter(ar
, filter
);
1668 if (changed_flags
& FIF_PROMISC_IN_BSS
) {
1669 ar
->sniffer_enabled
= ((*new_flags
) & FIF_PROMISC_IN_BSS
) != 0;
1670 ar9170_set_operating_mode(ar
);
1673 mutex_unlock(&ar
->mutex
);
1677 static void ar9170_op_bss_info_changed(struct ieee80211_hw
*hw
,
1678 struct ieee80211_vif
*vif
,
1679 struct ieee80211_bss_conf
*bss_conf
,
1682 struct ar9170
*ar
= hw
->priv
;
1683 struct ath_common
*common
= &ar
->common
;
1686 mutex_lock(&ar
->mutex
);
1688 if (changed
& BSS_CHANGED_BSSID
) {
1689 memcpy(common
->curbssid
, bss_conf
->bssid
, ETH_ALEN
);
1690 err
= ar9170_set_operating_mode(ar
);
1695 if (changed
& BSS_CHANGED_BEACON_ENABLED
)
1696 ar
->enable_beacon
= bss_conf
->enable_beacon
;
1698 if (changed
& BSS_CHANGED_BEACON
) {
1699 err
= ar9170_update_beacon(ar
);
1704 if (changed
& (BSS_CHANGED_BEACON_ENABLED
| BSS_CHANGED_BEACON
|
1705 BSS_CHANGED_BEACON_INT
)) {
1706 err
= ar9170_set_beacon_timers(ar
);
1711 if (changed
& BSS_CHANGED_ASSOC
) {
1712 #ifndef CONFIG_AR9170_LEDS
1713 /* enable assoc LED. */
1714 err
= ar9170_set_leds_state(ar
, bss_conf
->assoc
? 2 : 0);
1715 #endif /* CONFIG_AR9170_LEDS */
1718 if (changed
& BSS_CHANGED_HT
) {
1723 if (changed
& BSS_CHANGED_ERP_SLOT
) {
1724 err
= ar9170_set_slot_time(ar
);
1729 if (changed
& BSS_CHANGED_BASIC_RATES
) {
1730 err
= ar9170_set_basic_rates(ar
);
1736 mutex_unlock(&ar
->mutex
);
1739 static u64
ar9170_op_get_tsf(struct ieee80211_hw
*hw
)
1741 struct ar9170
*ar
= hw
->priv
;
1745 static const u32 addr
[NR
] = { AR9170_MAC_REG_TSF_H
,
1746 AR9170_MAC_REG_TSF_L
,
1747 AR9170_MAC_REG_TSF_H
};
1751 mutex_lock(&ar
->mutex
);
1753 while (loops
++ < 10) {
1754 err
= ar9170_read_mreg(ar
, NR
, addr
, val
);
1755 if (err
|| val
[0] == val
[2])
1759 mutex_unlock(&ar
->mutex
);
1764 tsf
= (tsf
<< 32) | val
[1];
1769 static int ar9170_set_key(struct ieee80211_hw
*hw
, enum set_key_cmd cmd
,
1770 struct ieee80211_vif
*vif
, struct ieee80211_sta
*sta
,
1771 struct ieee80211_key_conf
*key
)
1773 struct ar9170
*ar
= hw
->priv
;
1777 if ((!ar
->vif
) || (ar
->disable_offload
))
1780 switch (key
->cipher
) {
1781 case WLAN_CIPHER_SUITE_WEP40
:
1782 ktype
= AR9170_ENC_ALG_WEP64
;
1784 case WLAN_CIPHER_SUITE_WEP104
:
1785 ktype
= AR9170_ENC_ALG_WEP128
;
1787 case WLAN_CIPHER_SUITE_TKIP
:
1788 ktype
= AR9170_ENC_ALG_TKIP
;
1790 case WLAN_CIPHER_SUITE_CCMP
:
1791 ktype
= AR9170_ENC_ALG_AESCCMP
;
1797 mutex_lock(&ar
->mutex
);
1798 if (cmd
== SET_KEY
) {
1799 if (unlikely(!IS_STARTED(ar
))) {
1804 /* group keys need all-zeroes address */
1805 if (!(key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
))
1808 if (key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
) {
1809 for (i
= 0; i
< 64; i
++)
1810 if (!(ar
->usedkeys
& BIT(i
)))
1813 ar
->rx_software_decryption
= true;
1814 ar9170_set_operating_mode(ar
);
1819 i
= 64 + key
->keyidx
;
1822 key
->hw_key_idx
= i
;
1824 err
= ar9170_upload_key(ar
, i
, sta
? sta
->addr
: NULL
, ktype
, 0,
1825 key
->key
, min_t(u8
, 16, key
->keylen
));
1829 if (key
->cipher
== WLAN_CIPHER_SUITE_TKIP
) {
1830 err
= ar9170_upload_key(ar
, i
, sta
? sta
->addr
: NULL
,
1831 ktype
, 1, key
->key
+ 16, 16);
1836 * hardware is not capable generating the MMIC
1837 * for fragmented frames!
1839 key
->flags
|= IEEE80211_KEY_FLAG_GENERATE_MMIC
;
1843 ar
->usedkeys
|= BIT(i
);
1845 key
->flags
|= IEEE80211_KEY_FLAG_GENERATE_IV
;
1847 if (unlikely(!IS_STARTED(ar
))) {
1848 /* The device is gone... together with the key ;-) */
1853 err
= ar9170_disable_key(ar
, key
->hw_key_idx
);
1857 if (key
->hw_key_idx
< 64) {
1858 ar
->usedkeys
&= ~BIT(key
->hw_key_idx
);
1860 err
= ar9170_upload_key(ar
, key
->hw_key_idx
, NULL
,
1861 AR9170_ENC_ALG_NONE
, 0,
1866 if (key
->cipher
== WLAN_CIPHER_SUITE_TKIP
) {
1867 err
= ar9170_upload_key(ar
, key
->hw_key_idx
,
1869 AR9170_ENC_ALG_NONE
, 1,
1878 ar9170_regwrite_begin(ar
);
1879 ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_L
, ar
->usedkeys
);
1880 ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_H
, ar
->usedkeys
>> 32);
1881 ar9170_regwrite_finish();
1882 err
= ar9170_regwrite_result();
1885 mutex_unlock(&ar
->mutex
);
1890 static int ar9170_get_stats(struct ieee80211_hw
*hw
,
1891 struct ieee80211_low_level_stats
*stats
)
1893 struct ar9170
*ar
= hw
->priv
;
1897 mutex_lock(&ar
->mutex
);
1898 err
= ar9170_read_reg(ar
, AR9170_MAC_REG_TX_RETRY
, &val
);
1899 ar
->stats
.dot11ACKFailureCount
+= val
;
1901 memcpy(stats
, &ar
->stats
, sizeof(*stats
));
1902 mutex_unlock(&ar
->mutex
);
1907 static int ar9170_get_survey(struct ieee80211_hw
*hw
, int idx
,
1908 struct survey_info
*survey
)
1910 struct ar9170
*ar
= hw
->priv
;
1911 struct ieee80211_conf
*conf
= &hw
->conf
;
1916 /* TODO: update noise value, e.g. call ar9170_set_channel */
1918 survey
->channel
= conf
->channel
;
1919 survey
->filled
= SURVEY_INFO_NOISE_DBM
;
1920 survey
->noise
= ar
->noise
[0];
1925 static int ar9170_conf_tx(struct ieee80211_hw
*hw
, u16 queue
,
1926 const struct ieee80211_tx_queue_params
*param
)
1928 struct ar9170
*ar
= hw
->priv
;
1931 mutex_lock(&ar
->mutex
);
1932 if (queue
< __AR9170_NUM_TXQ
) {
1933 memcpy(&ar
->edcf
[ar9170_qos_hwmap
[queue
]],
1934 param
, sizeof(*param
));
1936 ret
= ar9170_set_qos(ar
);
1941 mutex_unlock(&ar
->mutex
);
1945 static int ar9170_ampdu_action(struct ieee80211_hw
*hw
,
1946 struct ieee80211_vif
*vif
,
1947 enum ieee80211_ampdu_mlme_action action
,
1948 struct ieee80211_sta
*sta
, u16 tid
, u16
*ssn
)
1951 case IEEE80211_AMPDU_RX_START
:
1952 case IEEE80211_AMPDU_RX_STOP
:
1953 /* Handled by firmware */
1963 static const struct ieee80211_ops ar9170_ops
= {
1964 .start
= ar9170_op_start
,
1965 .stop
= ar9170_op_stop
,
1967 .add_interface
= ar9170_op_add_interface
,
1968 .remove_interface
= ar9170_op_remove_interface
,
1969 .config
= ar9170_op_config
,
1970 .prepare_multicast
= ar9170_op_prepare_multicast
,
1971 .configure_filter
= ar9170_op_configure_filter
,
1972 .conf_tx
= ar9170_conf_tx
,
1973 .bss_info_changed
= ar9170_op_bss_info_changed
,
1974 .get_tsf
= ar9170_op_get_tsf
,
1975 .set_key
= ar9170_set_key
,
1976 .get_stats
= ar9170_get_stats
,
1977 .get_survey
= ar9170_get_survey
,
1978 .ampdu_action
= ar9170_ampdu_action
,
1981 void *ar9170_alloc(size_t priv_size
)
1983 struct ieee80211_hw
*hw
;
1985 struct sk_buff
*skb
;
1989 * this buffer is used for rx stream reconstruction.
1990 * Under heavy load this device (or the transport layer?)
1991 * tends to split the streams into separate rx descriptors.
1994 skb
= __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE
, GFP_KERNEL
);
1998 hw
= ieee80211_alloc_hw(priv_size
, &ar9170_ops
);
2004 ar
->rx_failover
= skb
;
2006 mutex_init(&ar
->mutex
);
2007 spin_lock_init(&ar
->cmdlock
);
2008 spin_lock_init(&ar
->tx_stats_lock
);
2009 for (i
= 0; i
< __AR9170_NUM_TXQ
; i
++) {
2010 skb_queue_head_init(&ar
->tx_status
[i
]);
2011 skb_queue_head_init(&ar
->tx_pending
[i
]);
2013 ar9170_rx_reset_rx_mpdu(ar
);
2014 INIT_WORK(&ar
->beacon_work
, ar9170_new_beacon
);
2015 INIT_DELAYED_WORK(&ar
->tx_janitor
, ar9170_tx_janitor
);
2017 /* all hw supports 2.4 GHz, so set channel to 1 by default */
2018 ar
->channel
= &ar9170_2ghz_chantable
[0];
2020 /* first part of wiphy init */
2021 ar
->hw
->wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
) |
2022 BIT(NL80211_IFTYPE_WDS
) |
2023 BIT(NL80211_IFTYPE_ADHOC
);
2024 ar
->hw
->flags
|= IEEE80211_HW_RX_INCLUDES_FCS
|
2025 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING
|
2026 IEEE80211_HW_SIGNAL_DBM
;
2028 ar
->hw
->queues
= __AR9170_NUM_TXQ
;
2029 ar
->hw
->extra_tx_headroom
= 8;
2031 ar
->hw
->max_rates
= 1;
2032 ar
->hw
->max_rate_tries
= 3;
2034 for (i
= 0; i
< ARRAY_SIZE(ar
->noise
); i
++)
2035 ar
->noise
[i
] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
2041 return ERR_PTR(-ENOMEM
);
2044 static int ar9170_read_eeprom(struct ar9170
*ar
)
2046 #define RW 8 /* number of words to read at once */
2047 #define RB (sizeof(u32) * RW)
2048 struct ath_regulatory
*regulatory
= &ar
->common
.regulatory
;
2049 u8
*eeprom
= (void *)&ar
->eeprom
;
2050 u8
*addr
= ar
->eeprom
.mac_address
;
2052 unsigned int rx_streams
, tx_streams
, tx_params
= 0;
2053 int i
, j
, err
, bands
= 0;
2055 BUILD_BUG_ON(sizeof(ar
->eeprom
) & 3);
2057 BUILD_BUG_ON(RB
> AR9170_MAX_CMD_LEN
- 4);
2059 /* don't want to handle trailing remains */
2060 BUILD_BUG_ON(sizeof(ar
->eeprom
) % RB
);
2063 for (i
= 0; i
< sizeof(ar
->eeprom
)/RB
; i
++) {
2064 for (j
= 0; j
< RW
; j
++)
2065 offsets
[j
] = cpu_to_le32(AR9170_EEPROM_START
+
2068 err
= ar
->exec_cmd(ar
, AR9170_CMD_RREG
,
2069 RB
, (u8
*) &offsets
,
2070 RB
, eeprom
+ RB
* i
);
2078 if (ar
->eeprom
.length
== cpu_to_le16(0xFFFF))
2081 if (ar
->eeprom
.operating_flags
& AR9170_OPFLAG_2GHZ
) {
2082 ar
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] = &ar9170_band_2GHz
;
2085 if (ar
->eeprom
.operating_flags
& AR9170_OPFLAG_5GHZ
) {
2086 ar
->hw
->wiphy
->bands
[IEEE80211_BAND_5GHZ
] = &ar9170_band_5GHz
;
2090 rx_streams
= hweight8(ar
->eeprom
.rx_mask
);
2091 tx_streams
= hweight8(ar
->eeprom
.tx_mask
);
2093 if (rx_streams
!= tx_streams
)
2094 tx_params
= IEEE80211_HT_MCS_TX_RX_DIFF
;
2096 if (tx_streams
>= 1 && tx_streams
<= IEEE80211_HT_MCS_TX_MAX_STREAMS
)
2097 tx_params
= (tx_streams
- 1) <<
2098 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT
;
2100 ar9170_band_2GHz
.ht_cap
.mcs
.tx_params
|= tx_params
;
2101 ar9170_band_5GHz
.ht_cap
.mcs
.tx_params
|= tx_params
;
2104 * I measured this, a bandswitch takes roughly
2105 * 135 ms and a frequency switch about 80.
2107 * FIXME: measure these values again once EEPROM settings
2108 * are used, that will influence them!
2111 ar
->hw
->channel_change_time
= 135 * 1000;
2113 ar
->hw
->channel_change_time
= 80 * 1000;
2115 regulatory
->current_rd
= le16_to_cpu(ar
->eeprom
.reg_domain
[0]);
2116 regulatory
->current_rd_ext
= le16_to_cpu(ar
->eeprom
.reg_domain
[1]);
2118 /* second part of wiphy init */
2119 SET_IEEE80211_PERM_ADDR(ar
->hw
, addr
);
2121 return bands
? 0 : -EINVAL
;
2124 static int ar9170_reg_notifier(struct wiphy
*wiphy
,
2125 struct regulatory_request
*request
)
2127 struct ieee80211_hw
*hw
= wiphy_to_ieee80211_hw(wiphy
);
2128 struct ar9170
*ar
= hw
->priv
;
2130 return ath_reg_notifier_apply(wiphy
, request
, &ar
->common
.regulatory
);
2133 int ar9170_register(struct ar9170
*ar
, struct device
*pdev
)
2135 struct ath_regulatory
*regulatory
= &ar
->common
.regulatory
;
2138 /* try to read EEPROM, init MAC addr */
2139 err
= ar9170_read_eeprom(ar
);
2143 err
= ath_regd_init(regulatory
, ar
->hw
->wiphy
,
2144 ar9170_reg_notifier
);
2148 err
= ieee80211_register_hw(ar
->hw
);
2152 if (!ath_is_world_regd(regulatory
))
2153 regulatory_hint(ar
->hw
->wiphy
, regulatory
->alpha2
);
2155 err
= ar9170_init_leds(ar
);
2159 #ifdef CONFIG_AR9170_LEDS
2160 err
= ar9170_register_leds(ar
);
2163 #endif /* CONFIG_AR9170_LEDS */
2165 dev_info(pdev
, "Atheros AR9170 is registered as '%s'\n",
2166 wiphy_name(ar
->hw
->wiphy
));
2168 ar
->registered
= true;
2172 ieee80211_unregister_hw(ar
->hw
);
2178 void ar9170_unregister(struct ar9170
*ar
)
2180 if (ar
->registered
) {
2181 #ifdef CONFIG_AR9170_LEDS
2182 ar9170_unregister_leds(ar
);
2183 #endif /* CONFIG_AR9170_LEDS */
2185 ieee80211_unregister_hw(ar
->hw
);
2188 kfree_skb(ar
->rx_failover
);
2189 mutex_destroy(&ar
->mutex
);