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 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
922 ieee80211_rx_irqsafe(ar->hw, skb);
926 void ar9170_rx(struct ar9170 *ar, struct sk_buff *skb)
928 unsigned int i, tlen, resplen, wlen = 0, clen = 0;
935 clen = tbuf[1] << 8 | tbuf[0];
936 wlen = ALIGN(clen, 4);
938 /* check if this is stream has a valid tag.*/
939 if (tbuf[2] != 0 || tbuf[3] != 0x4e) {
941 * TODO: handle the highly unlikely event that the
942 * corrupted stream has the TAG at the right position.
945 /* check if the frame can be repaired. */
946 if (!ar->rx_failover_missing) {
947 /* this is no "short read". */
948 if (ar9170_nag_limiter(ar)) {
949 printk(KERN_ERR "%s: missing tag!\n",
950 wiphy_name(ar->hw->wiphy));
956 if (ar->rx_failover_missing > tlen) {
957 if (ar9170_nag_limiter(ar)) {
958 printk(KERN_ERR "%s: possible multi "
959 "stream corruption!\n",
960 wiphy_name(ar->hw->wiphy));
966 memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
967 ar->rx_failover_missing -= tlen;
969 if (ar->rx_failover_missing <= 0) {
971 * nested ar9170_rx call!
972 * termination is guranteed, even when the
973 * combined frame also have a element with
977 ar->rx_failover_missing = 0;
978 ar9170_rx(ar, ar->rx_failover);
980 skb_reset_tail_pointer(ar->rx_failover);
981 skb_trim(ar->rx_failover, 0);
987 /* check if stream is clipped */
988 if (wlen > tlen - 4) {
989 if (ar->rx_failover_missing) {
990 /* TODO: handle double stream corruption. */
991 if (ar9170_nag_limiter(ar)) {
992 printk(KERN_ERR "%s: double rx stream "
994 wiphy_name(ar->hw->wiphy));
1001 * save incomplete data set.
1002 * the firmware will resend the missing bits when
1003 * the rx - descriptor comes round again.
1006 memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
1007 ar->rx_failover_missing = clen - tlen;
1017 /* weird thing, but this is the same in the original driver */
1018 while (resplen > 2 && i < 12 &&
1019 respbuf[0] == 0xff && respbuf[1] == 0xff) {
1028 /* found the 6 * 0xffff marker? */
1030 ar9170_handle_command_response(ar, respbuf, resplen);
1032 ar9170_handle_mpdu(ar, respbuf, clen);
1036 if (net_ratelimit())
1037 printk(KERN_ERR "%s: %d bytes of unprocessed "
1038 "data left in rx stream!\n",
1039 wiphy_name(ar->hw->wiphy), tlen);
1047 printk(KERN_ERR "%s: damaged RX stream data [want:%d, "
1048 "data:%d, rx:%d, pending:%d ]\n",
1049 wiphy_name(ar->hw->wiphy), clen, wlen, tlen,
1050 ar->rx_failover_missing);
1052 if (ar->rx_failover_missing)
1053 print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET,
1054 ar->rx_failover->data,
1055 ar->rx_failover->len);
1057 print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET,
1058 skb->data, skb->len);
1060 printk(KERN_ERR "%s: please check your hardware and cables, if "
1061 "you see this message frequently.\n",
1062 wiphy_name(ar->hw->wiphy));
1065 if (ar->rx_failover_missing) {
1066 skb_reset_tail_pointer(ar->rx_failover);
1067 skb_trim(ar->rx_failover, 0);
1068 ar->rx_failover_missing = 0;
1072 #define AR9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
1074 queue.aifs = ai_fs; \
1075 queue.cw_min = cwmin; \
1076 queue.cw_max = cwmax; \
1077 queue.txop = _txop; \
1080 static int ar9170_op_start(struct ieee80211_hw *hw)
1082 struct ar9170 *ar = hw->priv;
1085 mutex_lock(&ar->mutex);
1087 ar->filter_changed = 0;
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 ar->bad_hw_nagger = jiffies;
1107 err = ar9170_init_mac(ar);
1111 err = ar9170_set_qos(ar);
1115 err = ar9170_init_phy(ar, IEEE80211_BAND_2GHZ);
1119 err = ar9170_init_rf(ar);
1124 err = ar9170_write_reg(ar, 0x1c3d30, 0x100);
1128 ar->state = AR9170_STARTED;
1131 mutex_unlock(&ar->mutex);
1135 static void ar9170_op_stop(struct ieee80211_hw *hw)
1137 struct ar9170 *ar = hw->priv;
1141 ar->state = AR9170_IDLE;
1143 flush_workqueue(ar->hw->workqueue);
1145 cancel_delayed_work_sync(&ar->tx_janitor);
1146 cancel_work_sync(&ar->filter_config_work);
1147 cancel_work_sync(&ar->beacon_work);
1148 mutex_lock(&ar->mutex);
1150 if (IS_ACCEPTING_CMD(ar)) {
1151 ar9170_set_leds_state(ar, 0);
1154 ar9170_write_reg(ar, 0x1c3d30, 0);
1158 for (i = 0; i < __AR9170_NUM_TXQ; i++) {
1159 skb_queue_purge(&ar->tx_pending[i]);
1160 skb_queue_purge(&ar->tx_status[i]);
1162 mutex_unlock(&ar->mutex);
1165 static int ar9170_tx_prepare(struct ar9170 *ar, struct sk_buff *skb)
1167 struct ieee80211_hdr *hdr;
1168 struct ar9170_tx_control *txc;
1169 struct ieee80211_tx_info *info;
1170 struct ieee80211_tx_rate *txrate;
1171 struct ar9170_tx_info *arinfo;
1172 unsigned int queue = skb_get_queue_mapping(skb);
1176 BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
1178 hdr = (void *)skb->data;
1179 info = IEEE80211_SKB_CB(skb);
1182 txc = (void *)skb_push(skb, sizeof(*txc));
1184 if (info->control.hw_key) {
1185 icv = info->control.hw_key->icv_len;
1187 switch (info->control.hw_key->alg) {
1189 keytype = AR9170_TX_MAC_ENCR_RC4;
1192 keytype = AR9170_TX_MAC_ENCR_RC4;
1195 keytype = AR9170_TX_MAC_ENCR_AES;
1204 txc->length = cpu_to_le16(len + icv + 4);
1206 txc->mac_control = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
1207 AR9170_TX_MAC_BACKOFF);
1208 txc->mac_control |= cpu_to_le16(ar9170_qos_hwmap[queue] <<
1209 AR9170_TX_MAC_QOS_SHIFT);
1210 txc->mac_control |= cpu_to_le16(keytype);
1211 txc->phy_control = cpu_to_le32(0);
1213 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
1214 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
1216 txrate = &info->control.rates[0];
1217 if (txrate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
1218 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
1219 else if (txrate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
1220 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
1222 arinfo = (void *)info->rate_driver_data;
1223 arinfo->timeout = jiffies + msecs_to_jiffies(AR9170_QUEUE_TIMEOUT);
1225 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
1226 (is_valid_ether_addr(ieee80211_get_DA(hdr)))) {
1227 if (info->flags & IEEE80211_TX_CTL_AMPDU) {
1228 if (unlikely(!info->control.sta))
1231 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_AGGR);
1232 arinfo->flags = AR9170_TX_FLAG_BLOCK_ACK;
1236 txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_RATE_PROBE);
1239 * Putting the QoS queue bits into an unexplored territory is
1240 * certainly not elegant.
1242 * In my defense: This idea provides a reasonable way to
1243 * smuggle valuable information to the tx_status callback.
1244 * Also, the idea behind this bit-abuse came straight from
1245 * the original driver code.
1249 cpu_to_le32(queue << AR9170_TX_PHY_QOS_SHIFT);
1250 arinfo->flags = AR9170_TX_FLAG_WAIT_FOR_ACK;
1252 arinfo->flags = AR9170_TX_FLAG_NO_ACK;
1259 skb_pull(skb, sizeof(*txc));
1263 static void ar9170_tx_prepare_phy(struct ar9170 *ar, struct sk_buff *skb)
1265 struct ar9170_tx_control *txc;
1266 struct ieee80211_tx_info *info;
1267 struct ieee80211_rate *rate = NULL;
1268 struct ieee80211_tx_rate *txrate;
1271 txc = (void *) skb->data;
1272 info = IEEE80211_SKB_CB(skb);
1273 txrate = &info->control.rates[0];
1275 if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
1276 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
1278 if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1279 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
1281 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1282 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ);
1283 /* this works because 40 MHz is 2 and dup is 3 */
1284 if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
1285 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP);
1287 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
1288 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
1290 if (txrate->flags & IEEE80211_TX_RC_MCS) {
1291 u32 r = txrate->idx;
1294 /* heavy clip control */
1295 txc->phy_control |= cpu_to_le32((r & 0x7) << 7);
1297 r <<= AR9170_TX_PHY_MCS_SHIFT;
1298 BUG_ON(r & ~AR9170_TX_PHY_MCS_MASK);
1300 txc->phy_control |= cpu_to_le32(r & AR9170_TX_PHY_MCS_MASK);
1301 txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
1303 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
1304 if (info->band == IEEE80211_BAND_5GHZ)
1305 txpower = ar->power_5G_ht40;
1307 txpower = ar->power_2G_ht40;
1309 if (info->band == IEEE80211_BAND_5GHZ)
1310 txpower = ar->power_5G_ht20;
1312 txpower = ar->power_2G_ht20;
1315 power = txpower[(txrate->idx) & 7];
1320 u8 idx = txrate->idx;
1322 if (info->band != IEEE80211_BAND_2GHZ) {
1324 txpower = ar->power_5G_leg;
1325 mod = AR9170_TX_PHY_MOD_OFDM;
1328 txpower = ar->power_2G_cck;
1329 mod = AR9170_TX_PHY_MOD_CCK;
1331 mod = AR9170_TX_PHY_MOD_OFDM;
1332 txpower = ar->power_2G_ofdm;
1336 rate = &__ar9170_ratetable[idx];
1338 phyrate = rate->hw_value & 0xF;
1339 power = txpower[(rate->hw_value & 0x30) >> 4];
1340 phyrate <<= AR9170_TX_PHY_MCS_SHIFT;
1342 txc->phy_control |= cpu_to_le32(mod);
1343 txc->phy_control |= cpu_to_le32(phyrate);
1346 power <<= AR9170_TX_PHY_TX_PWR_SHIFT;
1347 power &= AR9170_TX_PHY_TX_PWR_MASK;
1348 txc->phy_control |= cpu_to_le32(power);
1351 if (ar->eeprom.tx_mask == 1) {
1352 chains = AR9170_TX_PHY_TXCHAIN_1;
1354 chains = AR9170_TX_PHY_TXCHAIN_2;
1356 /* >= 36M legacy OFDM - use only one chain */
1357 if (rate && rate->bitrate >= 360)
1358 chains = AR9170_TX_PHY_TXCHAIN_1;
1360 txc->phy_control |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_SHIFT);
1363 static void ar9170_tx(struct ar9170 *ar)
1365 struct sk_buff *skb;
1366 unsigned long flags;
1367 struct ieee80211_tx_info *info;
1368 struct ar9170_tx_info *arinfo;
1369 unsigned int i, frames, frames_failed, remaining_space;
1371 bool schedule_garbagecollector = false;
1373 BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
1375 if (unlikely(!IS_STARTED(ar)))
1378 remaining_space = AR9170_TX_MAX_PENDING;
1380 for (i = 0; i < __AR9170_NUM_TXQ; i++) {
1381 spin_lock_irqsave(&ar->tx_stats_lock, flags);
1382 if (ar->tx_stats[i].len >= ar->tx_stats[i].limit) {
1383 #ifdef AR9170_QUEUE_DEBUG
1384 printk(KERN_DEBUG "%s: queue %d full\n",
1385 wiphy_name(ar->hw->wiphy), i);
1387 __ar9170_dump_txstats(ar);
1388 printk(KERN_DEBUG "stuck frames: ===> \n");
1389 ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
1390 ar9170_dump_txqueue(ar, &ar->tx_status[i]);
1391 #endif /* AR9170_QUEUE_DEBUG */
1392 ieee80211_stop_queue(ar->hw, i);
1393 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
1397 frames = min(ar->tx_stats[i].limit - ar->tx_stats[i].len,
1398 skb_queue_len(&ar->tx_pending[i]));
1400 if (remaining_space < frames) {
1401 #ifdef AR9170_QUEUE_DEBUG
1402 printk(KERN_DEBUG "%s: tx quota reached queue:%d, "
1403 "remaining slots:%d, needed:%d\n",
1404 wiphy_name(ar->hw->wiphy), i, remaining_space,
1407 ar9170_dump_txstats(ar);
1408 #endif /* AR9170_QUEUE_DEBUG */
1409 frames = remaining_space;
1412 ar->tx_stats[i].len += frames;
1413 ar->tx_stats[i].count += frames;
1414 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
1421 skb = skb_dequeue(&ar->tx_pending[i]);
1422 if (unlikely(!skb)) {
1423 frames_failed += frames;
1428 info = IEEE80211_SKB_CB(skb);
1429 arinfo = (void *) info->rate_driver_data;
1431 /* TODO: cancel stuck frames */
1432 arinfo->timeout = jiffies +
1433 msecs_to_jiffies(AR9170_TX_TIMEOUT);
1435 #ifdef AR9170_QUEUE_DEBUG
1436 printk(KERN_DEBUG "%s: send frame q:%d =>\n",
1437 wiphy_name(ar->hw->wiphy), i);
1438 ar9170_print_txheader(ar, skb);
1439 #endif /* AR9170_QUEUE_DEBUG */
1441 err = ar->tx(ar, skb);
1442 if (unlikely(err)) {
1444 dev_kfree_skb_any(skb);
1447 schedule_garbagecollector = true;
1453 #ifdef AR9170_QUEUE_DEBUG
1454 printk(KERN_DEBUG "%s: ar9170_tx report for queue %d\n",
1455 wiphy_name(ar->hw->wiphy), i);
1457 printk(KERN_DEBUG "%s: unprocessed pending frames left:\n",
1458 wiphy_name(ar->hw->wiphy));
1459 ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
1460 #endif /* AR9170_QUEUE_DEBUG */
1462 if (unlikely(frames_failed)) {
1463 #ifdef AR9170_QUEUE_DEBUG
1464 printk(KERN_DEBUG "%s: frames failed =>\n",
1465 wiphy_name(ar->hw->wiphy), frames_failed);
1466 #endif /* AR9170_QUEUE_DEBUG */
1468 spin_lock_irqsave(&ar->tx_stats_lock, flags);
1469 ar->tx_stats[i].len -= frames_failed;
1470 ar->tx_stats[i].count -= frames_failed;
1471 ieee80211_wake_queue(ar->hw, i);
1472 spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
1476 if (schedule_garbagecollector)
1477 queue_delayed_work(ar->hw->workqueue,
1479 msecs_to_jiffies(AR9170_JANITOR_DELAY));
1482 int ar9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1484 struct ar9170 *ar = hw->priv;
1485 struct ieee80211_tx_info *info;
1487 if (unlikely(!IS_STARTED(ar)))
1490 if (unlikely(ar9170_tx_prepare(ar, skb)))
1493 info = IEEE80211_SKB_CB(skb);
1494 if (info->flags & IEEE80211_TX_CTL_AMPDU) {
1495 /* drop frame, we do not allow TX A-MPDU aggregation yet. */
1498 unsigned int queue = skb_get_queue_mapping(skb);
1500 ar9170_tx_prepare_phy(ar, skb);
1501 skb_queue_tail(&ar->tx_pending[queue], skb);
1505 return NETDEV_TX_OK;
1508 dev_kfree_skb_any(skb);
1509 return NETDEV_TX_OK;
1512 static int ar9170_op_add_interface(struct ieee80211_hw *hw,
1513 struct ieee80211_if_init_conf *conf)
1515 struct ar9170 *ar = hw->priv;
1518 mutex_lock(&ar->mutex);
1525 ar->vif = conf->vif;
1526 memcpy(ar->mac_addr, conf->mac_addr, ETH_ALEN);
1528 if (modparam_nohwcrypt || (ar->vif->type != NL80211_IFTYPE_STATION)) {
1529 ar->rx_software_decryption = true;
1530 ar->disable_offload = true;
1534 ar->want_filter = AR9170_MAC_REG_FTF_DEFAULTS;
1535 err = ar9170_update_frame_filter(ar);
1539 err = ar9170_set_operating_mode(ar);
1542 mutex_unlock(&ar->mutex);
1546 static void ar9170_op_remove_interface(struct ieee80211_hw *hw,
1547 struct ieee80211_if_init_conf *conf)
1549 struct ar9170 *ar = hw->priv;
1551 mutex_lock(&ar->mutex);
1553 ar->want_filter = 0;
1554 ar9170_update_frame_filter(ar);
1555 ar9170_set_beacon_timers(ar);
1556 dev_kfree_skb(ar->beacon);
1558 ar->sniffer_enabled = false;
1559 ar->rx_software_decryption = false;
1560 ar9170_set_operating_mode(ar);
1561 mutex_unlock(&ar->mutex);
1564 static int ar9170_op_config(struct ieee80211_hw *hw, u32 changed)
1566 struct ar9170 *ar = hw->priv;
1569 mutex_lock(&ar->mutex);
1571 if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
1576 if (changed & IEEE80211_CONF_CHANGE_PS) {
1581 if (changed & IEEE80211_CONF_CHANGE_POWER) {
1586 if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
1588 * is it long_frame_max_tx_count or short_frame_max_tx_count?
1591 err = ar9170_set_hwretry_limit(ar,
1592 ar->hw->conf.long_frame_max_tx_count);
1597 if (changed & BSS_CHANGED_BEACON_INT) {
1598 err = ar9170_set_beacon_timers(ar);
1603 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1605 /* adjust slot time for 5 GHz */
1606 err = ar9170_set_slot_time(ar);
1610 err = ar9170_set_dyn_sifs_ack(ar);
1614 err = ar9170_set_channel(ar, hw->conf.channel,
1616 nl80211_to_ar9170(hw->conf.channel_type));
1622 mutex_unlock(&ar->mutex);
1626 static void ar9170_set_filters(struct work_struct *work)
1628 struct ar9170 *ar = container_of(work, struct ar9170,
1629 filter_config_work);
1632 if (unlikely(!IS_STARTED(ar)))
1635 mutex_lock(&ar->mutex);
1636 if (test_and_clear_bit(AR9170_FILTER_CHANGED_MODE,
1637 &ar->filter_changed)) {
1638 err = ar9170_set_operating_mode(ar);
1643 if (test_and_clear_bit(AR9170_FILTER_CHANGED_MULTICAST,
1644 &ar->filter_changed)) {
1645 err = ar9170_update_multicast(ar);
1650 if (test_and_clear_bit(AR9170_FILTER_CHANGED_FRAMEFILTER,
1651 &ar->filter_changed)) {
1652 err = ar9170_update_frame_filter(ar);
1658 mutex_unlock(&ar->mutex);
1661 static void ar9170_op_configure_filter(struct ieee80211_hw *hw,
1662 unsigned int changed_flags,
1663 unsigned int *new_flags,
1664 int mc_count, struct dev_mc_list *mclist)
1666 struct ar9170 *ar = hw->priv;
1668 /* mask supported flags */
1669 *new_flags &= FIF_ALLMULTI | FIF_CONTROL | FIF_BCN_PRBRESP_PROMISC |
1670 FIF_PROMISC_IN_BSS | FIF_FCSFAIL | FIF_PLCPFAIL;
1671 ar->filter_state = *new_flags;
1673 * We can support more by setting the sniffer bit and
1674 * then checking the error flags, later.
1677 if (changed_flags & FIF_ALLMULTI) {
1678 if (*new_flags & FIF_ALLMULTI) {
1679 ar->want_mc_hash = ~0ULL;
1684 /* always get broadcast frames */
1685 mchash = 1ULL << (0xff >> 2);
1687 for (i = 0; i < mc_count; i++) {
1688 if (WARN_ON(!mclist))
1690 mchash |= 1ULL << (mclist->dmi_addr[5] >> 2);
1691 mclist = mclist->next;
1693 ar->want_mc_hash = mchash;
1695 set_bit(AR9170_FILTER_CHANGED_MULTICAST, &ar->filter_changed);
1698 if (changed_flags & FIF_CONTROL) {
1699 u32 filter = AR9170_MAC_REG_FTF_PSPOLL |
1700 AR9170_MAC_REG_FTF_RTS |
1701 AR9170_MAC_REG_FTF_CTS |
1702 AR9170_MAC_REG_FTF_ACK |
1703 AR9170_MAC_REG_FTF_CFE |
1704 AR9170_MAC_REG_FTF_CFE_ACK;
1706 if (*new_flags & FIF_CONTROL)
1707 ar->want_filter = ar->cur_filter | filter;
1709 ar->want_filter = ar->cur_filter & ~filter;
1711 set_bit(AR9170_FILTER_CHANGED_FRAMEFILTER,
1712 &ar->filter_changed);
1715 if (changed_flags & FIF_PROMISC_IN_BSS) {
1716 ar->sniffer_enabled = ((*new_flags) & FIF_PROMISC_IN_BSS) != 0;
1717 set_bit(AR9170_FILTER_CHANGED_MODE,
1718 &ar->filter_changed);
1721 if (likely(IS_STARTED(ar)))
1722 queue_work(ar->hw->workqueue, &ar->filter_config_work);
1725 static void ar9170_op_bss_info_changed(struct ieee80211_hw *hw,
1726 struct ieee80211_vif *vif,
1727 struct ieee80211_bss_conf *bss_conf,
1730 struct ar9170 *ar = hw->priv;
1733 mutex_lock(&ar->mutex);
1735 if (changed & BSS_CHANGED_BSSID) {
1736 memcpy(ar->bssid, bss_conf->bssid, ETH_ALEN);
1737 err = ar9170_set_operating_mode(ar);
1742 if (changed & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED)) {
1743 err = ar9170_update_beacon(ar);
1747 err = ar9170_set_beacon_timers(ar);
1752 if (changed & BSS_CHANGED_ASSOC) {
1753 #ifndef CONFIG_AR9170_LEDS
1754 /* enable assoc LED. */
1755 err = ar9170_set_leds_state(ar, bss_conf->assoc ? 2 : 0);
1756 #endif /* CONFIG_AR9170_LEDS */
1759 if (changed & BSS_CHANGED_BEACON_INT) {
1760 err = ar9170_set_beacon_timers(ar);
1765 if (changed & BSS_CHANGED_HT) {
1770 if (changed & BSS_CHANGED_ERP_SLOT) {
1771 err = ar9170_set_slot_time(ar);
1776 if (changed & BSS_CHANGED_BASIC_RATES) {
1777 err = ar9170_set_basic_rates(ar);
1783 mutex_unlock(&ar->mutex);
1786 static u64 ar9170_op_get_tsf(struct ieee80211_hw *hw)
1788 struct ar9170 *ar = hw->priv;
1794 mutex_lock(&ar->mutex);
1795 err = ar9170_read_reg(ar, AR9170_MAC_REG_TSF_L, &tsf_low);
1797 err = ar9170_read_reg(ar, AR9170_MAC_REG_TSF_H, &tsf_high);
1798 mutex_unlock(&ar->mutex);
1804 tsf = (tsf << 32) | tsf_low;
1808 static int ar9170_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1809 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1810 struct ieee80211_key_conf *key)
1812 struct ar9170 *ar = hw->priv;
1816 if ((!ar->vif) || (ar->disable_offload))
1821 if (key->keylen == WLAN_KEY_LEN_WEP40)
1822 ktype = AR9170_ENC_ALG_WEP64;
1824 ktype = AR9170_ENC_ALG_WEP128;
1827 ktype = AR9170_ENC_ALG_TKIP;
1830 ktype = AR9170_ENC_ALG_AESCCMP;
1836 mutex_lock(&ar->mutex);
1837 if (cmd == SET_KEY) {
1838 if (unlikely(!IS_STARTED(ar))) {
1843 /* group keys need all-zeroes address */
1844 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1847 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
1848 for (i = 0; i < 64; i++)
1849 if (!(ar->usedkeys & BIT(i)))
1852 ar->rx_software_decryption = true;
1853 ar9170_set_operating_mode(ar);
1858 i = 64 + key->keyidx;
1861 key->hw_key_idx = i;
1863 err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL, ktype, 0,
1864 key->key, min_t(u8, 16, key->keylen));
1868 if (key->alg == ALG_TKIP) {
1869 err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL,
1870 ktype, 1, key->key + 16, 16);
1875 * hardware is not capable generating the MMIC
1876 * for fragmented frames!
1878 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1882 ar->usedkeys |= BIT(i);
1884 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1886 if (unlikely(!IS_STARTED(ar))) {
1887 /* The device is gone... together with the key ;-) */
1892 err = ar9170_disable_key(ar, key->hw_key_idx);
1896 if (key->hw_key_idx < 64) {
1897 ar->usedkeys &= ~BIT(key->hw_key_idx);
1899 err = ar9170_upload_key(ar, key->hw_key_idx, NULL,
1900 AR9170_ENC_ALG_NONE, 0,
1905 if (key->alg == ALG_TKIP) {
1906 err = ar9170_upload_key(ar, key->hw_key_idx,
1908 AR9170_ENC_ALG_NONE, 1,
1917 ar9170_regwrite_begin(ar);
1918 ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_L, ar->usedkeys);
1919 ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_H, ar->usedkeys >> 32);
1920 ar9170_regwrite_finish();
1921 err = ar9170_regwrite_result();
1924 mutex_unlock(&ar->mutex);
1929 static void ar9170_sta_notify(struct ieee80211_hw *hw,
1930 struct ieee80211_vif *vif,
1931 enum sta_notify_cmd cmd,
1932 struct ieee80211_sta *sta)
1936 static int ar9170_get_stats(struct ieee80211_hw *hw,
1937 struct ieee80211_low_level_stats *stats)
1939 struct ar9170 *ar = hw->priv;
1943 mutex_lock(&ar->mutex);
1944 err = ar9170_read_reg(ar, AR9170_MAC_REG_TX_RETRY, &val);
1945 ar->stats.dot11ACKFailureCount += val;
1947 memcpy(stats, &ar->stats, sizeof(*stats));
1948 mutex_unlock(&ar->mutex);
1953 static int ar9170_get_tx_stats(struct ieee80211_hw *hw,
1954 struct ieee80211_tx_queue_stats *tx_stats)
1956 struct ar9170 *ar = hw->priv;
1958 spin_lock_bh(&ar->tx_stats_lock);
1959 memcpy(tx_stats, ar->tx_stats, sizeof(tx_stats[0]) * hw->queues);
1960 spin_unlock_bh(&ar->tx_stats_lock);
1965 static int ar9170_conf_tx(struct ieee80211_hw *hw, u16 queue,
1966 const struct ieee80211_tx_queue_params *param)
1968 struct ar9170 *ar = hw->priv;
1971 mutex_lock(&ar->mutex);
1972 if ((param) && !(queue > __AR9170_NUM_TXQ)) {
1973 memcpy(&ar->edcf[ar9170_qos_hwmap[queue]],
1974 param, sizeof(*param));
1976 ret = ar9170_set_qos(ar);
1980 mutex_unlock(&ar->mutex);
1984 static int ar9170_ampdu_action(struct ieee80211_hw *hw,
1985 enum ieee80211_ampdu_mlme_action action,
1986 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1989 case IEEE80211_AMPDU_RX_START:
1990 case IEEE80211_AMPDU_RX_STOP:
1992 * Something goes wrong -- RX locks up
1993 * after a while of receiving aggregated
1994 * frames -- not enabling for now.
2002 static const struct ieee80211_ops ar9170_ops = {
2003 .start = ar9170_op_start,
2004 .stop = ar9170_op_stop,
2006 .add_interface = ar9170_op_add_interface,
2007 .remove_interface = ar9170_op_remove_interface,
2008 .config = ar9170_op_config,
2009 .configure_filter = ar9170_op_configure_filter,
2010 .conf_tx = ar9170_conf_tx,
2011 .bss_info_changed = ar9170_op_bss_info_changed,
2012 .get_tsf = ar9170_op_get_tsf,
2013 .set_key = ar9170_set_key,
2014 .sta_notify = ar9170_sta_notify,
2015 .get_stats = ar9170_get_stats,
2016 .get_tx_stats = ar9170_get_tx_stats,
2017 .ampdu_action = ar9170_ampdu_action,
2020 void *ar9170_alloc(size_t priv_size)
2022 struct ieee80211_hw *hw;
2024 struct sk_buff *skb;
2028 * this buffer is used for rx stream reconstruction.
2029 * Under heavy load this device (or the transport layer?)
2030 * tends to split the streams into seperate rx descriptors.
2033 skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE, GFP_KERNEL);
2037 hw = ieee80211_alloc_hw(priv_size, &ar9170_ops);
2043 ar->rx_failover = skb;
2045 mutex_init(&ar->mutex);
2046 spin_lock_init(&ar->cmdlock);
2047 spin_lock_init(&ar->tx_stats_lock);
2048 for (i = 0; i < __AR9170_NUM_TXQ; i++) {
2049 skb_queue_head_init(&ar->tx_status[i]);
2050 skb_queue_head_init(&ar->tx_pending[i]);
2052 ar9170_rx_reset_rx_mpdu(ar);
2053 INIT_WORK(&ar->filter_config_work, ar9170_set_filters);
2054 INIT_WORK(&ar->beacon_work, ar9170_new_beacon);
2055 INIT_DELAYED_WORK(&ar->tx_janitor, ar9170_tx_janitor);
2057 /* all hw supports 2.4 GHz, so set channel to 1 by default */
2058 ar->channel = &ar9170_2ghz_chantable[0];
2060 /* first part of wiphy init */
2061 ar->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2062 BIT(NL80211_IFTYPE_WDS) |
2063 BIT(NL80211_IFTYPE_ADHOC);
2064 ar->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
2065 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2066 IEEE80211_HW_SIGNAL_DBM |
2067 IEEE80211_HW_NOISE_DBM;
2069 ar->hw->queues = __AR9170_NUM_TXQ;
2070 ar->hw->extra_tx_headroom = 8;
2071 ar->hw->sta_data_size = sizeof(struct ar9170_sta_info);
2073 ar->hw->max_rates = 1;
2074 ar->hw->max_rate_tries = 3;
2076 for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
2077 ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
2083 return ERR_PTR(-ENOMEM);
2086 static int ar9170_read_eeprom(struct ar9170 *ar)
2088 #define RW 8 /* number of words to read at once */
2089 #define RB (sizeof(u32) * RW)
2090 DECLARE_MAC_BUF(mbuf);
2091 u8 *eeprom = (void *)&ar->eeprom;
2092 u8 *addr = ar->eeprom.mac_address;
2094 int i, j, err, bands = 0;
2096 BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
2098 BUILD_BUG_ON(RB > AR9170_MAX_CMD_LEN - 4);
2100 /* don't want to handle trailing remains */
2101 BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
2104 for (i = 0; i < sizeof(ar->eeprom)/RB; i++) {
2105 for (j = 0; j < RW; j++)
2106 offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
2109 err = ar->exec_cmd(ar, AR9170_CMD_RREG,
2110 RB, (u8 *) &offsets,
2111 RB, eeprom + RB * i);
2119 if (ar->eeprom.length == cpu_to_le16(0xFFFF))
2122 if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
2123 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &ar9170_band_2GHz;
2126 if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
2127 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &ar9170_band_5GHz;
2131 * I measured this, a bandswitch takes roughly
2132 * 135 ms and a frequency switch about 80.
2134 * FIXME: measure these values again once EEPROM settings
2135 * are used, that will influence them!
2138 ar->hw->channel_change_time = 135 * 1000;
2140 ar->hw->channel_change_time = 80 * 1000;
2142 ar->regulatory.current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
2143 ar->regulatory.current_rd_ext = le16_to_cpu(ar->eeprom.reg_domain[1]);
2145 /* second part of wiphy init */
2146 SET_IEEE80211_PERM_ADDR(ar->hw, addr);
2148 return bands ? 0 : -EINVAL;
2151 static int ar9170_reg_notifier(struct wiphy *wiphy,
2152 struct regulatory_request *request)
2154 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2155 struct ar9170 *ar = hw->priv;
2157 return ath_reg_notifier_apply(wiphy, request, &ar->regulatory);
2160 int ar9170_register(struct ar9170 *ar, struct device *pdev)
2164 /* try to read EEPROM, init MAC addr */
2165 err = ar9170_read_eeprom(ar);
2169 err = ath_regd_init(&ar->regulatory, ar->hw->wiphy,
2170 ar9170_reg_notifier);
2174 err = ieee80211_register_hw(ar->hw);
2178 if (!ath_is_world_regd(&ar->regulatory))
2179 regulatory_hint(ar->hw->wiphy, ar->regulatory.alpha2);
2181 err = ar9170_init_leds(ar);
2185 #ifdef CONFIG_AR9170_LEDS
2186 err = ar9170_register_leds(ar);
2189 #endif /* CONFIG_AR9170_LEDS */
2191 dev_info(pdev, "Atheros AR9170 is registered as '%s'\n",
2192 wiphy_name(ar->hw->wiphy));
2197 ieee80211_unregister_hw(ar->hw);
2203 void ar9170_unregister(struct ar9170 *ar)
2205 #ifdef CONFIG_AR9170_LEDS
2206 ar9170_unregister_leds(ar);
2207 #endif /* CONFIG_AR9170_LEDS */
2209 kfree_skb(ar->rx_failover);
2210 ieee80211_unregister_hw(ar->hw);
2211 mutex_destroy(&ar->mutex);