2 * Atheros CARL9170 driver
4 * 802.11 xmit & status routines
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, see
21 * http://www.gnu.org/licenses/.
23 * This file incorporates work covered by the following copyright and
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
27 * Permission to use, copy, modify, and/or distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
31 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/module.h>
43 #include <linux/etherdevice.h>
44 #include <net/mac80211.h>
49 static inline unsigned int __carl9170_get_queue(struct ar9170 *ar,
52 if (unlikely(modparam_noht)) {
56 * This is just another workaround, until
57 * someone figures out how to get QoS and
58 * AMPDU to play nicely together.
65 static inline unsigned int carl9170_get_queue(struct ar9170 *ar,
68 return __carl9170_get_queue(ar, skb_get_queue_mapping(skb));
71 static bool is_mem_full(struct ar9170 *ar)
73 return (DIV_ROUND_UP(IEEE80211_MAX_FRAME_LEN, ar->fw.mem_block_size) >
74 atomic_read(&ar->mem_free_blocks));
77 static void carl9170_tx_accounting(struct ar9170 *ar, struct sk_buff *skb)
82 atomic_inc(&ar->tx_total_queued);
84 queue = skb_get_queue_mapping(skb);
85 spin_lock_bh(&ar->tx_stats_lock);
88 * The driver has to accept the frame, regardless if the queue is
89 * full to the brim, or not. We have to do the queuing internally,
90 * since mac80211 assumes that a driver which can operate with
91 * aggregated frames does not reject frames for this reason.
93 ar->tx_stats[queue].len++;
94 ar->tx_stats[queue].count++;
96 mem_full = is_mem_full(ar);
97 for (i = 0; i < ar->hw->queues; i++) {
98 if (mem_full || ar->tx_stats[i].len >= ar->tx_stats[i].limit) {
99 ieee80211_stop_queue(ar->hw, i);
100 ar->queue_stop_timeout[i] = jiffies;
104 spin_unlock_bh(&ar->tx_stats_lock);
107 static void carl9170_tx_accounting_free(struct ar9170 *ar, struct sk_buff *skb)
109 struct ieee80211_tx_info *txinfo;
112 txinfo = IEEE80211_SKB_CB(skb);
113 queue = skb_get_queue_mapping(skb);
115 spin_lock_bh(&ar->tx_stats_lock);
117 ar->tx_stats[queue].len--;
119 if (!is_mem_full(ar)) {
121 for (i = 0; i < ar->hw->queues; i++) {
122 if (ar->tx_stats[i].len >= CARL9170_NUM_TX_LIMIT_SOFT)
125 if (ieee80211_queue_stopped(ar->hw, i)) {
128 tmp = jiffies - ar->queue_stop_timeout[i];
129 if (tmp > ar->max_queue_stop_timeout[i])
130 ar->max_queue_stop_timeout[i] = tmp;
133 ieee80211_wake_queue(ar->hw, i);
137 spin_unlock_bh(&ar->tx_stats_lock);
138 if (atomic_dec_and_test(&ar->tx_total_queued))
139 complete(&ar->tx_flush);
142 static int carl9170_alloc_dev_space(struct ar9170 *ar, struct sk_buff *skb)
144 struct _carl9170_tx_superframe *super = (void *) skb->data;
148 atomic_inc(&ar->mem_allocs);
150 chunks = DIV_ROUND_UP(skb->len, ar->fw.mem_block_size);
151 if (unlikely(atomic_sub_return(chunks, &ar->mem_free_blocks) < 0)) {
152 atomic_add(chunks, &ar->mem_free_blocks);
156 spin_lock_bh(&ar->mem_lock);
157 cookie = bitmap_find_free_region(ar->mem_bitmap, ar->fw.mem_blocks, 0);
158 spin_unlock_bh(&ar->mem_lock);
160 if (unlikely(cookie < 0)) {
161 atomic_add(chunks, &ar->mem_free_blocks);
165 super = (void *) skb->data;
168 * Cookie #0 serves two special purposes:
169 * 1. The firmware might use it generate BlockACK frames
170 * in responds of an incoming BlockAckReqs.
172 * 2. Prevent double-free bugs.
174 super->s.cookie = (u8) cookie + 1;
178 static void carl9170_release_dev_space(struct ar9170 *ar, struct sk_buff *skb)
180 struct _carl9170_tx_superframe *super = (void *) skb->data;
183 /* make a local copy of the cookie */
184 cookie = super->s.cookie;
185 /* invalidate cookie */
189 * Do a out-of-bounds check on the cookie:
191 * * cookie "0" is reserved and won't be assigned to any
192 * out-going frame. Internally however, it is used to
193 * mark no longer/un-accounted frames and serves as a
194 * cheap way of preventing frames from being freed
195 * twice by _accident_. NB: There is a tiny race...
197 * * obviously, cookie number is limited by the amount
198 * of available memory blocks, so the number can
199 * never execeed the mem_blocks count.
201 if (unlikely(WARN_ON_ONCE(cookie == 0) ||
202 WARN_ON_ONCE(cookie > ar->fw.mem_blocks)))
205 atomic_add(DIV_ROUND_UP(skb->len, ar->fw.mem_block_size),
206 &ar->mem_free_blocks);
208 spin_lock_bh(&ar->mem_lock);
209 bitmap_release_region(ar->mem_bitmap, cookie - 1, 0);
210 spin_unlock_bh(&ar->mem_lock);
213 /* Called from any context */
214 static void carl9170_tx_release(struct kref *ref)
217 struct carl9170_tx_info *arinfo;
218 struct ieee80211_tx_info *txinfo;
221 arinfo = container_of(ref, struct carl9170_tx_info, ref);
222 txinfo = container_of((void *) arinfo, struct ieee80211_tx_info,
224 skb = container_of((void *) txinfo, struct sk_buff, cb);
227 if (WARN_ON_ONCE(!ar))
231 offsetof(struct ieee80211_tx_info, status.ampdu_ack_len) != 23);
233 memset(&txinfo->status.ampdu_ack_len, 0,
234 sizeof(struct ieee80211_tx_info) -
235 offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
237 if (atomic_read(&ar->tx_total_queued))
238 ar->tx_schedule = true;
240 if (txinfo->flags & IEEE80211_TX_CTL_AMPDU) {
241 if (!atomic_read(&ar->tx_ampdu_upload))
242 ar->tx_ampdu_schedule = true;
244 if (txinfo->flags & IEEE80211_TX_STAT_AMPDU) {
245 struct _carl9170_tx_superframe *super;
247 super = (void *)skb->data;
248 txinfo->status.ampdu_len = super->s.rix;
249 txinfo->status.ampdu_ack_len = super->s.cnt;
250 } else if (txinfo->flags & IEEE80211_TX_STAT_ACK) {
252 * drop redundant tx_status reports:
254 * 1. ampdu_ack_len of the final tx_status does
255 * include the feedback of this particular frame.
257 * 2. tx_status_irqsafe only queues up to 128
258 * tx feedback reports and discards the rest.
260 * 3. minstrel_ht is picky, it only accepts
261 * reports of frames with the TX_STATUS_AMPDU flag.
264 dev_kfree_skb_any(skb);
268 * Frame has failed, but we want to keep it in
269 * case it was lost due to a power-state
275 skb_pull(skb, sizeof(struct _carl9170_tx_superframe));
276 ieee80211_tx_status_irqsafe(ar->hw, skb);
279 void carl9170_tx_get_skb(struct sk_buff *skb)
281 struct carl9170_tx_info *arinfo = (void *)
282 (IEEE80211_SKB_CB(skb))->rate_driver_data;
283 kref_get(&arinfo->ref);
286 int carl9170_tx_put_skb(struct sk_buff *skb)
288 struct carl9170_tx_info *arinfo = (void *)
289 (IEEE80211_SKB_CB(skb))->rate_driver_data;
291 return kref_put(&arinfo->ref, carl9170_tx_release);
294 /* Caller must hold the tid_info->lock & rcu_read_lock */
295 static void carl9170_tx_shift_bm(struct ar9170 *ar,
296 struct carl9170_sta_tid *tid_info, u16 seq)
300 off = SEQ_DIFF(seq, tid_info->bsn);
302 if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
306 * Sanity check. For each MPDU we set the bit in bitmap and
307 * clear it once we received the tx_status.
308 * But if the bit is already cleared then we've been bitten
311 WARN_ON_ONCE(!test_and_clear_bit(off, tid_info->bitmap));
313 off = SEQ_DIFF(tid_info->snx, tid_info->bsn);
314 if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
317 if (!bitmap_empty(tid_info->bitmap, off))
318 off = find_first_bit(tid_info->bitmap, off);
320 tid_info->bsn += off;
321 tid_info->bsn &= 0x0fff;
323 bitmap_shift_right(tid_info->bitmap, tid_info->bitmap,
324 off, CARL9170_BAW_BITS);
327 static void carl9170_tx_status_process_ampdu(struct ar9170 *ar,
328 struct sk_buff *skb, struct ieee80211_tx_info *txinfo)
330 struct _carl9170_tx_superframe *super = (void *) skb->data;
331 struct ieee80211_hdr *hdr = (void *) super->frame_data;
332 struct ieee80211_tx_info *tx_info;
333 struct carl9170_tx_info *ar_info;
334 struct carl9170_sta_info *sta_info;
335 struct ieee80211_sta *sta;
336 struct carl9170_sta_tid *tid_info;
337 struct ieee80211_vif *vif;
341 if (!(txinfo->flags & IEEE80211_TX_CTL_AMPDU) ||
342 txinfo->flags & IEEE80211_TX_CTL_INJECTED ||
343 (!(super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_AGGR))))
346 tx_info = IEEE80211_SKB_CB(skb);
347 ar_info = (void *) tx_info->rate_driver_data;
349 vif_id = (super->s.misc & CARL9170_TX_SUPER_MISC_VIF_ID) >>
350 CARL9170_TX_SUPER_MISC_VIF_ID_S;
352 if (WARN_ON_ONCE(vif_id >= AR9170_MAX_VIRTUAL_MAC))
356 vif = rcu_dereference(ar->vif_priv[vif_id].vif);
361 * Normally we should use wrappers like ieee80211_get_DA to get
362 * the correct peer ieee80211_sta.
364 * But there is a problem with indirect traffic (broadcasts, or
365 * data which is designated for other stations) in station mode.
366 * The frame will be directed to the AP for distribution and not
367 * to the actual destination.
369 sta = ieee80211_find_sta(vif, hdr->addr1);
373 tid = get_tid_h(hdr);
375 sta_info = (void *) sta->drv_priv;
376 tid_info = rcu_dereference(sta_info->agg[tid]);
380 spin_lock_bh(&tid_info->lock);
381 if (likely(tid_info->state >= CARL9170_TID_STATE_IDLE))
382 carl9170_tx_shift_bm(ar, tid_info, get_seq_h(hdr));
384 if (sta_info->stats[tid].clear) {
385 sta_info->stats[tid].clear = false;
386 sta_info->stats[tid].req = false;
387 sta_info->stats[tid].ampdu_len = 0;
388 sta_info->stats[tid].ampdu_ack_len = 0;
391 sta_info->stats[tid].ampdu_len++;
392 if (txinfo->status.rates[0].count == 1)
393 sta_info->stats[tid].ampdu_ack_len++;
395 if (!(txinfo->flags & IEEE80211_TX_STAT_ACK))
396 sta_info->stats[tid].req = true;
398 if (super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_IMM_BA)) {
399 super->s.rix = sta_info->stats[tid].ampdu_len;
400 super->s.cnt = sta_info->stats[tid].ampdu_ack_len;
401 txinfo->flags |= IEEE80211_TX_STAT_AMPDU;
402 if (sta_info->stats[tid].req)
403 txinfo->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
405 sta_info->stats[tid].clear = true;
407 spin_unlock_bh(&tid_info->lock);
413 void carl9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
416 struct ieee80211_tx_info *txinfo;
418 carl9170_tx_accounting_free(ar, skb);
420 txinfo = IEEE80211_SKB_CB(skb);
423 txinfo->flags |= IEEE80211_TX_STAT_ACK;
425 ar->tx_ack_failures++;
427 if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
428 carl9170_tx_status_process_ampdu(ar, skb, txinfo);
430 carl9170_tx_put_skb(skb);
433 /* This function may be called form any context */
434 void carl9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb)
436 struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
438 atomic_dec(&ar->tx_total_pending);
440 if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
441 atomic_dec(&ar->tx_ampdu_upload);
443 if (carl9170_tx_put_skb(skb))
444 tasklet_hi_schedule(&ar->usb_tasklet);
447 static struct sk_buff *carl9170_get_queued_skb(struct ar9170 *ar, u8 cookie,
448 struct sk_buff_head *queue)
452 spin_lock_bh(&queue->lock);
453 skb_queue_walk(queue, skb) {
454 struct _carl9170_tx_superframe *txc = (void *) skb->data;
456 if (txc->s.cookie != cookie)
459 __skb_unlink(skb, queue);
460 spin_unlock_bh(&queue->lock);
462 carl9170_release_dev_space(ar, skb);
465 spin_unlock_bh(&queue->lock);
470 static void carl9170_tx_fill_rateinfo(struct ar9170 *ar, unsigned int rix,
471 unsigned int tries, struct ieee80211_tx_info *txinfo)
475 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
476 if (txinfo->status.rates[i].idx < 0)
480 txinfo->status.rates[i].count = tries;
486 for (; i < IEEE80211_TX_MAX_RATES; i++) {
487 txinfo->status.rates[i].idx = -1;
488 txinfo->status.rates[i].count = 0;
492 static void carl9170_check_queue_stop_timeout(struct ar9170 *ar)
496 struct ieee80211_tx_info *txinfo;
497 struct carl9170_tx_info *arinfo;
498 bool restart = false;
500 for (i = 0; i < ar->hw->queues; i++) {
501 spin_lock_bh(&ar->tx_status[i].lock);
503 skb = skb_peek(&ar->tx_status[i]);
508 txinfo = IEEE80211_SKB_CB(skb);
509 arinfo = (void *) txinfo->rate_driver_data;
511 if (time_is_before_jiffies(arinfo->timeout +
512 msecs_to_jiffies(CARL9170_QUEUE_STUCK_TIMEOUT)) == true)
516 spin_unlock_bh(&ar->tx_status[i].lock);
521 * At least one queue has been stuck for long enough.
522 * Give the device a kick and hope it gets back to
525 * possible reasons may include:
526 * - frames got lost/corrupted (bad connection to the device)
527 * - stalled rx processing/usb controller hiccups
528 * - firmware errors/bugs
529 * - every bug you can think of.
530 * - all bugs you can't...
533 carl9170_restart(ar, CARL9170_RR_STUCK_TX);
537 static void carl9170_tx_ampdu_timeout(struct ar9170 *ar)
539 struct carl9170_sta_tid *iter;
541 struct ieee80211_tx_info *txinfo;
542 struct carl9170_tx_info *arinfo;
543 struct _carl9170_tx_superframe *super;
544 struct ieee80211_sta *sta;
545 struct ieee80211_vif *vif;
546 struct ieee80211_hdr *hdr;
550 list_for_each_entry_rcu(iter, &ar->tx_ampdu_list, list) {
551 if (iter->state < CARL9170_TID_STATE_IDLE)
554 spin_lock_bh(&iter->lock);
555 skb = skb_peek(&iter->queue);
559 txinfo = IEEE80211_SKB_CB(skb);
560 arinfo = (void *)txinfo->rate_driver_data;
561 if (time_is_after_jiffies(arinfo->timeout +
562 msecs_to_jiffies(CARL9170_QUEUE_TIMEOUT)))
565 super = (void *) skb->data;
566 hdr = (void *) super->frame_data;
568 vif_id = (super->s.misc & CARL9170_TX_SUPER_MISC_VIF_ID) >>
569 CARL9170_TX_SUPER_MISC_VIF_ID_S;
571 if (WARN_ON(vif_id >= AR9170_MAX_VIRTUAL_MAC))
574 vif = rcu_dereference(ar->vif_priv[vif_id].vif);
578 sta = ieee80211_find_sta(vif, hdr->addr1);
582 ieee80211_stop_tx_ba_session(sta, iter->tid);
584 spin_unlock_bh(&iter->lock);
590 void carl9170_tx_janitor(struct work_struct *work)
592 struct ar9170 *ar = container_of(work, struct ar9170,
597 ar->tx_janitor_last_run = jiffies;
599 carl9170_check_queue_stop_timeout(ar);
600 carl9170_tx_ampdu_timeout(ar);
602 if (!atomic_read(&ar->tx_total_queued))
605 ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
606 msecs_to_jiffies(CARL9170_TX_TIMEOUT));
609 static void __carl9170_tx_process_status(struct ar9170 *ar,
610 const uint8_t cookie, const uint8_t info)
613 struct ieee80211_tx_info *txinfo;
614 struct carl9170_tx_info *arinfo;
615 unsigned int r, t, q;
618 q = ar9170_qmap[info & CARL9170_TX_STATUS_QUEUE];
620 skb = carl9170_get_queued_skb(ar, cookie, &ar->tx_status[q]);
623 * We have lost the race to another thread.
629 txinfo = IEEE80211_SKB_CB(skb);
630 arinfo = (void *) txinfo->rate_driver_data;
632 if (!(info & CARL9170_TX_STATUS_SUCCESS))
635 r = (info & CARL9170_TX_STATUS_RIX) >> CARL9170_TX_STATUS_RIX_S;
636 t = (info & CARL9170_TX_STATUS_TRIES) >> CARL9170_TX_STATUS_TRIES_S;
638 carl9170_tx_fill_rateinfo(ar, r, t, txinfo);
639 carl9170_tx_status(ar, skb, success);
642 void carl9170_tx_process_status(struct ar9170 *ar,
643 const struct carl9170_rsp *cmd)
647 for (i = 0; i < cmd->hdr.ext; i++) {
648 if (WARN_ON(i > ((cmd->hdr.len / 2) + 1))) {
649 print_hex_dump_bytes("UU:", DUMP_PREFIX_NONE,
650 (void *) cmd, cmd->hdr.len + 4);
654 __carl9170_tx_process_status(ar, cmd->_tx_status[i].cookie,
655 cmd->_tx_status[i].info);
659 static __le32 carl9170_tx_physet(struct ar9170 *ar,
660 struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate)
662 struct ieee80211_rate *rate = NULL;
666 tmp = cpu_to_le32(0);
668 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
669 tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ <<
671 /* this works because 40 MHz is 2 and dup is 3 */
672 if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
673 tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP <<
676 if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
677 tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
679 if (txrate->flags & IEEE80211_TX_RC_MCS) {
683 /* heavy clip control */
684 tmp |= cpu_to_le32((r & 0x7) <<
685 AR9170_TX_PHY_TX_HEAVY_CLIP_S);
687 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
688 if (info->band == IEEE80211_BAND_5GHZ)
689 txpower = ar->power_5G_ht40;
691 txpower = ar->power_2G_ht40;
693 if (info->band == IEEE80211_BAND_5GHZ)
694 txpower = ar->power_5G_ht20;
696 txpower = ar->power_2G_ht20;
699 power = txpower[r & 7];
701 /* +1 dBm for HT40 */
702 if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
705 r <<= AR9170_TX_PHY_MCS_S;
706 BUG_ON(r & ~AR9170_TX_PHY_MCS);
708 tmp |= cpu_to_le32(r & AR9170_TX_PHY_MCS);
709 tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
712 * green field preamble does not work.
714 * if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
715 * tmp |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
721 u8 idx = txrate->idx;
723 if (info->band != IEEE80211_BAND_2GHZ) {
725 txpower = ar->power_5G_leg;
726 mod = AR9170_TX_PHY_MOD_OFDM;
729 txpower = ar->power_2G_cck;
730 mod = AR9170_TX_PHY_MOD_CCK;
732 mod = AR9170_TX_PHY_MOD_OFDM;
733 txpower = ar->power_2G_ofdm;
737 rate = &__carl9170_ratetable[idx];
739 phyrate = rate->hw_value & 0xF;
740 power = txpower[(rate->hw_value & 0x30) >> 4];
741 phyrate <<= AR9170_TX_PHY_MCS_S;
743 tmp |= cpu_to_le32(mod);
744 tmp |= cpu_to_le32(phyrate);
747 * short preamble seems to be broken too.
749 * if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
750 * tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
753 power <<= AR9170_TX_PHY_TX_PWR_S;
754 power &= AR9170_TX_PHY_TX_PWR;
755 tmp |= cpu_to_le32(power);
758 if (ar->eeprom.tx_mask == 1) {
759 chains = AR9170_TX_PHY_TXCHAIN_1;
761 chains = AR9170_TX_PHY_TXCHAIN_2;
763 /* >= 36M legacy OFDM - use only one chain */
764 if (rate && rate->bitrate >= 360 &&
765 !(txrate->flags & IEEE80211_TX_RC_MCS))
766 chains = AR9170_TX_PHY_TXCHAIN_1;
768 tmp |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_S);
773 static bool carl9170_tx_rts_check(struct ar9170 *ar,
774 struct ieee80211_tx_rate *rate,
775 bool ampdu, bool multi)
777 switch (ar->erp_mode) {
778 case CARL9170_ERP_AUTO:
782 case CARL9170_ERP_MAC80211:
783 if (!(rate->flags & IEEE80211_TX_RC_USE_RTS_CTS))
786 case CARL9170_ERP_RTS:
797 static bool carl9170_tx_cts_check(struct ar9170 *ar,
798 struct ieee80211_tx_rate *rate)
800 switch (ar->erp_mode) {
801 case CARL9170_ERP_AUTO:
802 case CARL9170_ERP_MAC80211:
803 if (!(rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
806 case CARL9170_ERP_CTS:
816 static int carl9170_tx_prepare(struct ar9170 *ar, struct sk_buff *skb)
818 struct ieee80211_hdr *hdr;
819 struct _carl9170_tx_superframe *txc;
820 struct carl9170_vif_info *cvif;
821 struct ieee80211_tx_info *info;
822 struct ieee80211_tx_rate *txrate;
823 struct ieee80211_sta *sta;
824 struct carl9170_tx_info *arinfo;
825 unsigned int hw_queue;
831 BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
832 BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc) !=
833 CARL9170_TX_SUPERDESC_LEN);
835 BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc) !=
836 AR9170_TX_HWDESC_LEN);
838 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES < CARL9170_TX_MAX_RATES);
840 BUILD_BUG_ON(AR9170_MAX_VIRTUAL_MAC >
841 ((CARL9170_TX_SUPER_MISC_VIF_ID >>
842 CARL9170_TX_SUPER_MISC_VIF_ID_S) + 1));
844 hw_queue = ar9170_qmap[carl9170_get_queue(ar, skb)];
846 hdr = (void *)skb->data;
847 info = IEEE80211_SKB_CB(skb);
851 * Note: If the frame was sent through a monitor interface,
852 * the ieee80211_vif pointer can be NULL.
854 if (likely(info->control.vif))
855 cvif = (void *) info->control.vif->drv_priv;
859 sta = info->control.sta;
861 txc = (void *)skb_push(skb, sizeof(*txc));
862 memset(txc, 0, sizeof(*txc));
864 SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, txc->s.misc, hw_queue);
867 SET_VAL(CARL9170_TX_SUPER_MISC_VIF_ID, txc->s.misc, cvif->id);
869 if (unlikely(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM))
870 txc->s.misc |= CARL9170_TX_SUPER_MISC_CAB;
872 if (unlikely(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
873 txc->s.misc |= CARL9170_TX_SUPER_MISC_ASSIGN_SEQ;
875 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control)))
876 txc->s.misc |= CARL9170_TX_SUPER_MISC_FILL_IN_TSF;
878 mac_tmp = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
879 AR9170_TX_MAC_BACKOFF);
880 mac_tmp |= cpu_to_le16((hw_queue << AR9170_TX_MAC_QOS_S) &
883 no_ack = !!(info->flags & IEEE80211_TX_CTL_NO_ACK);
884 if (unlikely(no_ack))
885 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
887 if (info->control.hw_key) {
888 len += info->control.hw_key->icv_len;
890 switch (info->control.hw_key->cipher) {
891 case WLAN_CIPHER_SUITE_WEP40:
892 case WLAN_CIPHER_SUITE_WEP104:
893 case WLAN_CIPHER_SUITE_TKIP:
894 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_RC4);
896 case WLAN_CIPHER_SUITE_CCMP:
897 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_AES);
905 ampdu = !!(info->flags & IEEE80211_TX_CTL_AMPDU);
907 unsigned int density, factor;
909 if (unlikely(!sta || !cvif))
912 factor = min_t(unsigned int, 1u, sta->ht_cap.ampdu_factor);
913 density = sta->ht_cap.ampdu_density;
919 * Otus uses slightly different density values than
920 * those from the 802.11n spec.
923 density = max_t(unsigned int, density + 1, 7u);
926 SET_VAL(CARL9170_TX_SUPER_AMPDU_DENSITY,
927 txc->s.ampdu_settings, density);
929 SET_VAL(CARL9170_TX_SUPER_AMPDU_FACTOR,
930 txc->s.ampdu_settings, factor);
932 for (i = 0; i < CARL9170_TX_MAX_RATES; i++) {
933 txrate = &info->control.rates[i];
934 if (txrate->idx >= 0) {
936 CARL9170_TX_SUPER_RI_AMPDU;
938 if (WARN_ON(!(txrate->flags &
939 IEEE80211_TX_RC_MCS))) {
941 * Not sure if it's even possible
942 * to aggregate non-ht rates with
951 txrate->count = ar->hw->max_rate_tries;
954 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_AGGR);
958 * NOTE: For the first rate, the ERP & AMPDU flags are directly
959 * taken from mac_control. For all fallback rate, the firmware
960 * updates the mac_control flags from the rate info field.
962 for (i = 1; i < CARL9170_TX_MAX_RATES; i++) {
963 txrate = &info->control.rates[i];
967 SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[i],
970 if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
971 txc->s.ri[i] |= (AR9170_TX_MAC_PROT_RTS <<
972 CARL9170_TX_SUPER_RI_ERP_PROT_S);
973 else if (carl9170_tx_cts_check(ar, txrate))
974 txc->s.ri[i] |= (AR9170_TX_MAC_PROT_CTS <<
975 CARL9170_TX_SUPER_RI_ERP_PROT_S);
977 txc->s.rr[i - 1] = carl9170_tx_physet(ar, info, txrate);
980 txrate = &info->control.rates[0];
981 SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[0], txrate->count);
983 if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
984 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
985 else if (carl9170_tx_cts_check(ar, txrate))
986 mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
988 txc->s.len = cpu_to_le16(skb->len);
989 txc->f.length = cpu_to_le16(len + FCS_LEN);
990 txc->f.mac_control = mac_tmp;
991 txc->f.phy_control = carl9170_tx_physet(ar, info, txrate);
993 arinfo = (void *)info->rate_driver_data;
994 arinfo->timeout = jiffies;
996 kref_init(&arinfo->ref);
1000 skb_pull(skb, sizeof(*txc));
1004 static void carl9170_set_immba(struct ar9170 *ar, struct sk_buff *skb)
1006 struct _carl9170_tx_superframe *super;
1008 super = (void *) skb->data;
1009 super->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_BA);
1012 static void carl9170_set_ampdu_params(struct ar9170 *ar, struct sk_buff *skb)
1014 struct _carl9170_tx_superframe *super;
1017 super = (void *) skb->data;
1019 tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_DENSITY) <<
1020 CARL9170_TX_SUPER_AMPDU_DENSITY_S;
1023 * If you haven't noticed carl9170_tx_prepare has already filled
1024 * in all ampdu spacing & factor parameters.
1025 * Now it's the time to check whenever the settings have to be
1026 * updated by the firmware, or if everything is still the same.
1028 * There's no sane way to handle different density values with
1029 * this hardware, so we may as well just do the compare in the
1033 if (tmp != ar->current_density) {
1034 ar->current_density = tmp;
1035 super->s.ampdu_settings |=
1036 CARL9170_TX_SUPER_AMPDU_COMMIT_DENSITY;
1039 tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_FACTOR) <<
1040 CARL9170_TX_SUPER_AMPDU_FACTOR_S;
1042 if (tmp != ar->current_factor) {
1043 ar->current_factor = tmp;
1044 super->s.ampdu_settings |=
1045 CARL9170_TX_SUPER_AMPDU_COMMIT_FACTOR;
1049 static bool carl9170_tx_rate_check(struct ar9170 *ar, struct sk_buff *_dest,
1050 struct sk_buff *_src)
1052 struct _carl9170_tx_superframe *dest, *src;
1054 dest = (void *) _dest->data;
1055 src = (void *) _src->data;
1058 * The mac80211 rate control algorithm expects that all MPDUs in
1059 * an AMPDU share the same tx vectors.
1060 * This is not really obvious right now, because the hardware
1061 * does the AMPDU setup according to its own rulebook.
1062 * Our nicely assembled, strictly monotonic increasing mpdu
1063 * chains will be broken up, mashed back together...
1066 return (dest->f.phy_control == src->f.phy_control);
1069 static void carl9170_tx_ampdu(struct ar9170 *ar)
1071 struct sk_buff_head agg;
1072 struct carl9170_sta_tid *tid_info;
1073 struct sk_buff *skb, *first;
1074 unsigned int i = 0, done_ampdus = 0;
1075 u16 seq, queue, tmpssn;
1077 atomic_inc(&ar->tx_ampdu_scheduler);
1078 ar->tx_ampdu_schedule = false;
1080 if (atomic_read(&ar->tx_ampdu_upload))
1083 if (!ar->tx_ampdu_list_len)
1086 __skb_queue_head_init(&agg);
1089 tid_info = rcu_dereference(ar->tx_ampdu_iter);
1090 if (WARN_ON_ONCE(!tid_info)) {
1096 list_for_each_entry_continue_rcu(tid_info, &ar->tx_ampdu_list, list) {
1099 if (tid_info->state < CARL9170_TID_STATE_PROGRESS)
1102 queue = TID_TO_WME_AC(tid_info->tid);
1104 spin_lock_bh(&tid_info->lock);
1105 if (tid_info->state != CARL9170_TID_STATE_XMIT)
1108 tid_info->counter++;
1109 first = skb_peek(&tid_info->queue);
1110 tmpssn = carl9170_get_seq(first);
1111 seq = tid_info->snx;
1113 if (unlikely(tmpssn != seq)) {
1114 tid_info->state = CARL9170_TID_STATE_IDLE;
1119 while ((skb = skb_peek(&tid_info->queue))) {
1120 /* strict 0, 1, ..., n - 1, n frame sequence order */
1121 if (unlikely(carl9170_get_seq(skb) != seq))
1124 /* don't upload more than AMPDU FACTOR allows. */
1125 if (unlikely(SEQ_DIFF(tid_info->snx, tid_info->bsn) >=
1126 (tid_info->max - 1)))
1129 if (!carl9170_tx_rate_check(ar, skb, first))
1132 atomic_inc(&ar->tx_ampdu_upload);
1133 tid_info->snx = seq = SEQ_NEXT(seq);
1134 __skb_unlink(skb, &tid_info->queue);
1136 __skb_queue_tail(&agg, skb);
1138 if (skb_queue_len(&agg) >= CARL9170_NUM_TX_AGG_MAX)
1142 if (skb_queue_empty(&tid_info->queue) ||
1143 carl9170_get_seq(skb_peek(&tid_info->queue)) !=
1146 * stop TID, if A-MPDU frames are still missing,
1147 * or whenever the queue is empty.
1150 tid_info->state = CARL9170_TID_STATE_IDLE;
1155 spin_unlock_bh(&tid_info->lock);
1157 if (skb_queue_empty(&agg))
1160 /* apply ampdu spacing & factor settings */
1161 carl9170_set_ampdu_params(ar, skb_peek(&agg));
1163 /* set aggregation push bit */
1164 carl9170_set_immba(ar, skb_peek_tail(&agg));
1166 spin_lock_bh(&ar->tx_pending[queue].lock);
1167 skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
1168 spin_unlock_bh(&ar->tx_pending[queue].lock);
1169 ar->tx_schedule = true;
1171 if ((done_ampdus++ == 0) && (i++ == 0))
1174 rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
1178 static struct sk_buff *carl9170_tx_pick_skb(struct ar9170 *ar,
1179 struct sk_buff_head *queue)
1181 struct sk_buff *skb;
1182 struct ieee80211_tx_info *info;
1183 struct carl9170_tx_info *arinfo;
1185 BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
1187 spin_lock_bh(&queue->lock);
1188 skb = skb_peek(queue);
1192 if (carl9170_alloc_dev_space(ar, skb))
1195 __skb_unlink(skb, queue);
1196 spin_unlock_bh(&queue->lock);
1198 info = IEEE80211_SKB_CB(skb);
1199 arinfo = (void *) info->rate_driver_data;
1201 arinfo->timeout = jiffies;
1204 * increase ref count to "2".
1205 * Ref counting is the easiest way to solve the race between
1206 * the the urb's completion routine: carl9170_tx_callback and
1207 * wlan tx status functions: carl9170_tx_status/janitor.
1209 carl9170_tx_get_skb(skb);
1214 spin_unlock_bh(&queue->lock);
1218 void carl9170_tx_drop(struct ar9170 *ar, struct sk_buff *skb)
1220 struct _carl9170_tx_superframe *super;
1225 super = (void *)skb->data;
1226 SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, q,
1227 ar9170_qmap[carl9170_get_queue(ar, skb)]);
1228 __carl9170_tx_process_status(ar, super->s.cookie, q);
1231 static void carl9170_tx(struct ar9170 *ar)
1233 struct sk_buff *skb;
1235 bool schedule_garbagecollector = false;
1237 ar->tx_schedule = false;
1239 if (unlikely(!IS_STARTED(ar)))
1242 carl9170_usb_handle_tx_err(ar);
1244 for (i = 0; i < ar->hw->queues; i++) {
1245 while (!skb_queue_empty(&ar->tx_pending[i])) {
1246 skb = carl9170_tx_pick_skb(ar, &ar->tx_pending[i]);
1250 atomic_inc(&ar->tx_total_pending);
1252 q = __carl9170_get_queue(ar, i);
1254 * NB: tx_status[i] vs. tx_status[q],
1255 * TODO: Move into pick_skb or alloc_dev_space.
1257 skb_queue_tail(&ar->tx_status[q], skb);
1259 carl9170_usb_tx(ar, skb);
1260 schedule_garbagecollector = true;
1264 if (!schedule_garbagecollector)
1267 ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
1268 msecs_to_jiffies(CARL9170_TX_TIMEOUT));
1271 static bool carl9170_tx_ampdu_queue(struct ar9170 *ar,
1272 struct ieee80211_sta *sta, struct sk_buff *skb)
1274 struct _carl9170_tx_superframe *super = (void *) skb->data;
1275 struct carl9170_sta_info *sta_info;
1276 struct carl9170_sta_tid *agg;
1277 struct sk_buff *iter;
1279 u16 tid, seq, qseq, off;
1282 tid = carl9170_get_tid(skb);
1283 seq = carl9170_get_seq(skb);
1284 sta_info = (void *) sta->drv_priv;
1287 agg = rcu_dereference(sta_info->agg[tid]);
1288 max = sta_info->ampdu_max_len;
1291 goto err_unlock_rcu;
1293 spin_lock_bh(&agg->lock);
1294 if (unlikely(agg->state < CARL9170_TID_STATE_IDLE))
1297 /* check if sequence is within the BA window */
1298 if (unlikely(!BAW_WITHIN(agg->bsn, CARL9170_BAW_BITS, seq)))
1301 if (WARN_ON_ONCE(!BAW_WITHIN(agg->snx, CARL9170_BAW_BITS, seq)))
1304 off = SEQ_DIFF(seq, agg->bsn);
1305 if (WARN_ON_ONCE(test_and_set_bit(off, agg->bitmap)))
1308 if (likely(BAW_WITHIN(agg->hsn, CARL9170_BAW_BITS, seq))) {
1309 __skb_queue_tail(&agg->queue, skb);
1314 skb_queue_reverse_walk(&agg->queue, iter) {
1315 qseq = carl9170_get_seq(iter);
1317 if (BAW_WITHIN(qseq, CARL9170_BAW_BITS, seq)) {
1318 __skb_queue_after(&agg->queue, iter, skb);
1323 __skb_queue_head(&agg->queue, skb);
1326 if (unlikely(agg->state != CARL9170_TID_STATE_XMIT)) {
1327 if (agg->snx == carl9170_get_seq(skb_peek(&agg->queue))) {
1328 agg->state = CARL9170_TID_STATE_XMIT;
1333 spin_unlock_bh(&agg->lock);
1339 spin_unlock_bh(&agg->lock);
1343 super->f.mac_control &= ~cpu_to_le16(AR9170_TX_MAC_AGGR);
1344 carl9170_tx_status(ar, skb, false);
1349 void carl9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1351 struct ar9170 *ar = hw->priv;
1352 struct ieee80211_tx_info *info;
1353 struct ieee80211_sta *sta;
1356 if (unlikely(!IS_STARTED(ar)))
1359 info = IEEE80211_SKB_CB(skb);
1360 sta = info->control.sta;
1362 if (unlikely(carl9170_tx_prepare(ar, skb)))
1365 carl9170_tx_accounting(ar, skb);
1367 * from now on, one has to use carl9170_tx_status to free
1368 * all ressouces which are associated with the frame.
1371 if (info->flags & IEEE80211_TX_CTL_AMPDU) {
1372 run = carl9170_tx_ampdu_queue(ar, sta, skb);
1374 carl9170_tx_ampdu(ar);
1377 unsigned int queue = skb_get_queue_mapping(skb);
1379 skb_queue_tail(&ar->tx_pending[queue], skb);
1387 dev_kfree_skb_any(skb);
1390 void carl9170_tx_scheduler(struct ar9170 *ar)
1393 if (ar->tx_ampdu_schedule)
1394 carl9170_tx_ampdu(ar);
1396 if (ar->tx_schedule)