2 * Copyright (c) 2008 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #define BITS_PER_BYTE 8
20 #define OFDM_PLCP_BITS 22
21 #define HT_RC_2_MCS(_rc) ((_rc) & 0x0f)
22 #define HT_RC_2_STREAMS(_rc) ((((_rc) & 0x78) >> 3) + 1)
28 #define HT_LTF(_ns) (4 * (_ns))
29 #define SYMBOL_TIME(_ns) ((_ns) << 2) /* ns * 4 us */
30 #define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5) /* ns * 3.6 us */
31 #define NUM_SYMBOLS_PER_USEC(_usec) (_usec >> 2)
32 #define NUM_SYMBOLS_PER_USEC_HALFGI(_usec) (((_usec*5)-4)/18)
34 #define OFDM_SIFS_TIME 16
36 static u32 bits_per_symbol[][2] = {
38 { 26, 54 }, /* 0: BPSK */
39 { 52, 108 }, /* 1: QPSK 1/2 */
40 { 78, 162 }, /* 2: QPSK 3/4 */
41 { 104, 216 }, /* 3: 16-QAM 1/2 */
42 { 156, 324 }, /* 4: 16-QAM 3/4 */
43 { 208, 432 }, /* 5: 64-QAM 2/3 */
44 { 234, 486 }, /* 6: 64-QAM 3/4 */
45 { 260, 540 }, /* 7: 64-QAM 5/6 */
46 { 52, 108 }, /* 8: BPSK */
47 { 104, 216 }, /* 9: QPSK 1/2 */
48 { 156, 324 }, /* 10: QPSK 3/4 */
49 { 208, 432 }, /* 11: 16-QAM 1/2 */
50 { 312, 648 }, /* 12: 16-QAM 3/4 */
51 { 416, 864 }, /* 13: 64-QAM 2/3 */
52 { 468, 972 }, /* 14: 64-QAM 3/4 */
53 { 520, 1080 }, /* 15: 64-QAM 5/6 */
56 #define IS_HT_RATE(_rate) ((_rate) & 0x80)
59 * Insert a chain of ath_buf (descriptors) on a txq and
60 * assume the descriptors are already chained together by caller.
61 * NB: must be called with txq lock held
64 static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
65 struct list_head *head)
67 struct ath_hal *ah = sc->sc_ah;
71 * Insert the frame on the outbound list and
72 * pass it on to the hardware.
78 bf = list_first_entry(head, struct ath_buf, list);
80 list_splice_tail_init(head, &txq->axq_q);
82 txq->axq_totalqueued++;
83 txq->axq_linkbuf = list_entry(txq->axq_q.prev, struct ath_buf, list);
85 DPRINTF(sc, ATH_DBG_QUEUE,
86 "qnum: %d, txq depth: %d\n", txq->axq_qnum, txq->axq_depth);
88 if (txq->axq_link == NULL) {
89 ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
90 DPRINTF(sc, ATH_DBG_XMIT,
91 "TXDP[%u] = %llx (%p)\n",
92 txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
94 *txq->axq_link = bf->bf_daddr;
95 DPRINTF(sc, ATH_DBG_XMIT, "link[%u] (%p)=%llx (%p)\n",
96 txq->axq_qnum, txq->axq_link,
97 ito64(bf->bf_daddr), bf->bf_desc);
99 txq->axq_link = &(bf->bf_lastbf->bf_desc->ds_link);
100 ath9k_hw_txstart(ah, txq->axq_qnum);
103 static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
104 struct ath_xmit_status *tx_status)
106 struct ieee80211_hw *hw = sc->hw;
107 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
108 struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
110 DPRINTF(sc, ATH_DBG_XMIT, "TX complete: skb: %p\n", skb);
112 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
113 tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
115 tx_info->rate_driver_data[0] = NULL;
118 if (tx_status->flags & ATH_TX_BAR) {
119 tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
120 tx_status->flags &= ~ATH_TX_BAR;
123 if (!(tx_status->flags & (ATH_TX_ERROR | ATH_TX_XRETRY))) {
124 /* Frame was ACKed */
125 tx_info->flags |= IEEE80211_TX_STAT_ACK;
128 tx_info->status.rates[0].count = tx_status->retries + 1;
130 ieee80211_tx_status(hw, skb);
133 /* Check if it's okay to send out aggregates */
135 static int ath_aggr_query(struct ath_softc *sc, struct ath_node *an, u8 tidno)
137 struct ath_atx_tid *tid;
138 tid = ATH_AN_2_TID(an, tidno);
140 if (tid->state & AGGR_ADDBA_COMPLETE ||
141 tid->state & AGGR_ADDBA_PROGRESS)
147 static void ath_get_beaconconfig(struct ath_softc *sc, int if_id,
148 struct ath_beacon_config *conf)
150 struct ieee80211_hw *hw = sc->hw;
152 /* fill in beacon config data */
154 conf->beacon_interval = hw->conf.beacon_int;
155 conf->listen_interval = 100;
156 conf->dtim_count = 1;
157 conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
160 /* Calculate Atheros packet type from IEEE80211 packet header */
162 static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
164 struct ieee80211_hdr *hdr;
165 enum ath9k_pkt_type htype;
168 hdr = (struct ieee80211_hdr *)skb->data;
169 fc = hdr->frame_control;
171 if (ieee80211_is_beacon(fc))
172 htype = ATH9K_PKT_TYPE_BEACON;
173 else if (ieee80211_is_probe_resp(fc))
174 htype = ATH9K_PKT_TYPE_PROBE_RESP;
175 else if (ieee80211_is_atim(fc))
176 htype = ATH9K_PKT_TYPE_ATIM;
177 else if (ieee80211_is_pspoll(fc))
178 htype = ATH9K_PKT_TYPE_PSPOLL;
180 htype = ATH9K_PKT_TYPE_NORMAL;
185 static bool is_pae(struct sk_buff *skb)
187 struct ieee80211_hdr *hdr;
190 hdr = (struct ieee80211_hdr *)skb->data;
191 fc = hdr->frame_control;
193 if (ieee80211_is_data(fc)) {
194 if (ieee80211_is_nullfunc(fc) ||
195 /* Port Access Entity (IEEE 802.1X) */
196 (skb->protocol == cpu_to_be16(ETH_P_PAE))) {
204 static int get_hw_crypto_keytype(struct sk_buff *skb)
206 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
208 if (tx_info->control.hw_key) {
209 if (tx_info->control.hw_key->alg == ALG_WEP)
210 return ATH9K_KEY_TYPE_WEP;
211 else if (tx_info->control.hw_key->alg == ALG_TKIP)
212 return ATH9K_KEY_TYPE_TKIP;
213 else if (tx_info->control.hw_key->alg == ALG_CCMP)
214 return ATH9K_KEY_TYPE_AES;
217 return ATH9K_KEY_TYPE_CLEAR;
220 /* Called only when tx aggregation is enabled and HT is supported */
222 static void assign_aggr_tid_seqno(struct sk_buff *skb,
225 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
226 struct ieee80211_hdr *hdr;
228 struct ath_atx_tid *tid;
232 if (!tx_info->control.sta)
235 an = (struct ath_node *)tx_info->control.sta->drv_priv;
236 hdr = (struct ieee80211_hdr *)skb->data;
237 fc = hdr->frame_control;
241 if (ieee80211_is_data_qos(fc)) {
242 qc = ieee80211_get_qos_ctl(hdr);
243 bf->bf_tidno = qc[0] & 0xf;
248 if (ieee80211_is_data(fc) && !is_pae(skb)) {
249 /* For HT capable stations, we save tidno for later use.
250 * We also override seqno set by upper layer with the one
251 * in tx aggregation state.
253 * If fragmentation is on, the sequence number is
254 * not overridden, since it has been
255 * incremented by the fragmentation routine.
257 * FIXME: check if the fragmentation threshold exceeds
260 tid = ATH_AN_2_TID(an, bf->bf_tidno);
261 hdr->seq_ctrl = cpu_to_le16(tid->seq_next <<
262 IEEE80211_SEQ_SEQ_SHIFT);
263 bf->bf_seqno = tid->seq_next;
264 INCR(tid->seq_next, IEEE80211_SEQ_MAX);
268 static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
271 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
274 flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
275 flags |= ATH9K_TXDESC_INTREQ;
277 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
278 flags |= ATH9K_TXDESC_NOACK;
279 if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
280 flags |= ATH9K_TXDESC_RTSENA;
285 static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
287 struct ath_buf *bf = NULL;
289 spin_lock_bh(&sc->tx.txbuflock);
291 if (unlikely(list_empty(&sc->tx.txbuf))) {
292 spin_unlock_bh(&sc->tx.txbuflock);
296 bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
299 spin_unlock_bh(&sc->tx.txbuflock);
304 /* To complete a chain of buffers associated a frame */
306 static void ath_tx_complete_buf(struct ath_softc *sc,
308 struct list_head *bf_q,
309 int txok, int sendbar)
311 struct sk_buff *skb = bf->bf_mpdu;
312 struct ath_xmit_status tx_status;
316 * Set retry information.
317 * NB: Don't use the information in the descriptor, because the frame
318 * could be software retried.
320 tx_status.retries = bf->bf_retries;
324 tx_status.flags = ATH_TX_BAR;
327 tx_status.flags |= ATH_TX_ERROR;
329 if (bf_isxretried(bf))
330 tx_status.flags |= ATH_TX_XRETRY;
333 /* Unmap this frame */
334 pci_unmap_single(sc->pdev,
338 /* complete this frame */
339 ath_tx_complete(sc, skb, &tx_status);
342 * Return the list of ath_buf of this mpdu to free queue
344 spin_lock_irqsave(&sc->tx.txbuflock, flags);
345 list_splice_tail_init(bf_q, &sc->tx.txbuf);
346 spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
350 * queue up a dest/ac pair for tx scheduling
351 * NB: must be called with txq lock held
354 static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
356 struct ath_atx_ac *ac = tid->ac;
359 * if tid is paused, hold off
365 * add tid to ac atmost once
371 list_add_tail(&tid->list, &ac->tid_q);
374 * add node ac to txq atmost once
380 list_add_tail(&ac->list, &txq->axq_acq);
385 static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
387 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
389 spin_lock_bh(&txq->axq_lock);
393 spin_unlock_bh(&txq->axq_lock);
396 /* resume a tid and schedule aggregate */
398 void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
400 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
402 ASSERT(tid->paused > 0);
403 spin_lock_bh(&txq->axq_lock);
410 if (list_empty(&tid->buf_q))
414 * Add this TID to scheduler and try to send out aggregates
416 ath_tx_queue_tid(txq, tid);
417 ath_txq_schedule(sc, txq);
419 spin_unlock_bh(&txq->axq_lock);
422 /* Compute the number of bad frames */
424 static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
427 struct ath_buf *bf_last = bf->bf_lastbf;
428 struct ath_desc *ds = bf_last->bf_desc;
430 u32 ba[WME_BA_BMP_SIZE >> 5];
435 if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
438 isaggr = bf_isaggr(bf);
440 seq_st = ATH_DS_BA_SEQ(ds);
441 memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
445 ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
446 if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
455 static void ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
458 struct ieee80211_hdr *hdr;
460 bf->bf_state.bf_type |= BUF_RETRY;
464 hdr = (struct ieee80211_hdr *)skb->data;
465 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
468 /* Update block ack window */
470 static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
475 index = ATH_BA_INDEX(tid->seq_start, seqno);
476 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
478 tid->tx_buf[cindex] = NULL;
480 while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
481 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
482 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
487 * ath_pkt_dur - compute packet duration (NB: not NAV)
490 * pktlen - total bytes (delims + data + fcs + pads + pad delims)
491 * width - 0 for 20 MHz, 1 for 40 MHz
492 * half_gi - to use 4us v/s 3.6 us for symbol time
494 static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
495 int width, int half_gi, bool shortPreamble)
497 struct ath_rate_table *rate_table = sc->cur_rate_table;
498 u32 nbits, nsymbits, duration, nsymbols;
502 pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
503 rc = rate_table->info[rix].ratecode;
505 /* for legacy rates, use old function to compute packet duration */
507 return ath9k_hw_computetxtime(sc->sc_ah, rate_table, pktlen,
510 /* find number of symbols: PLCP + data */
511 nbits = (pktlen << 3) + OFDM_PLCP_BITS;
512 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
513 nsymbols = (nbits + nsymbits - 1) / nsymbits;
516 duration = SYMBOL_TIME(nsymbols);
518 duration = SYMBOL_TIME_HALFGI(nsymbols);
520 /* addup duration for legacy/ht training and signal fields */
521 streams = HT_RC_2_STREAMS(rc);
522 duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
527 /* Rate module function to set rate related fields in tx descriptor */
529 static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
531 struct ath_hal *ah = sc->sc_ah;
532 struct ath_rate_table *rt;
533 struct ath_desc *ds = bf->bf_desc;
534 struct ath_desc *lastds = bf->bf_lastbf->bf_desc;
535 struct ath9k_11n_rate_series series[4];
537 struct ieee80211_tx_info *tx_info;
538 struct ieee80211_tx_rate *rates;
539 struct ieee80211_hdr *hdr;
540 int i, flags, rtsctsena = 0;
542 u8 rix = 0, cix, ctsrate = 0;
545 memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
547 skb = (struct sk_buff *)bf->bf_mpdu;
548 hdr = (struct ieee80211_hdr *)skb->data;
549 fc = hdr->frame_control;
550 tx_info = IEEE80211_SKB_CB(skb);
551 rates = tx_info->control.rates;
553 if (ieee80211_has_morefrags(fc) ||
554 (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)) {
555 rates[1].count = rates[2].count = rates[3].count = 0;
556 rates[1].idx = rates[2].idx = rates[3].idx = 0;
557 rates[0].count = ATH_TXMAXTRY;
560 /* get the cix for the lowest valid rix */
561 rt = sc->cur_rate_table;
562 for (i = 3; i >= 0; i--) {
563 if (rates[i].count && (rates[i].idx >= 0)) {
569 flags = (bf->bf_flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA));
570 cix = rt->info[rix].ctrl_rate;
573 * If 802.11g protection is enabled, determine whether to use RTS/CTS or
574 * just CTS. Note that this is only done for OFDM/HT unicast frames.
576 if (sc->sc_protmode != PROT_M_NONE && !(bf->bf_flags & ATH9K_TXDESC_NOACK)
577 && (rt->info[rix].phy == WLAN_RC_PHY_OFDM ||
578 WLAN_RC_PHY_HT(rt->info[rix].phy))) {
579 if (sc->sc_protmode == PROT_M_RTSCTS)
580 flags = ATH9K_TXDESC_RTSENA;
581 else if (sc->sc_protmode == PROT_M_CTSONLY)
582 flags = ATH9K_TXDESC_CTSENA;
584 cix = rt->info[sc->sc_protrix].ctrl_rate;
588 /* For 11n, the default behavior is to enable RTS for hw retried frames.
589 * We enable the global flag here and let rate series flags determine
590 * which rates will actually use RTS.
592 if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) && bf_isdata(bf)) {
593 /* 802.11g protection not needed, use our default behavior */
595 flags = ATH9K_TXDESC_RTSENA;
598 /* Set protection if aggregate protection on */
599 if (sc->sc_config.ath_aggr_prot &&
600 (!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
601 flags = ATH9K_TXDESC_RTSENA;
602 cix = rt->info[sc->sc_protrix].ctrl_rate;
606 /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
607 if (bf_isaggr(bf) && (bf->bf_al > ah->ah_caps.rts_aggr_limit))
608 flags &= ~(ATH9K_TXDESC_RTSENA);
611 * CTS transmit rate is derived from the transmit rate by looking in the
612 * h/w rate table. We must also factor in whether or not a short
613 * preamble is to be used. NB: cix is set above where RTS/CTS is enabled
615 ctsrate = rt->info[cix].ratecode |
616 (bf_isshpreamble(bf) ? rt->info[cix].short_preamble : 0);
618 for (i = 0; i < 4; i++) {
619 if (!rates[i].count || (rates[i].idx < 0))
624 series[i].Rate = rt->info[rix].ratecode |
625 (bf_isshpreamble(bf) ? rt->info[rix].short_preamble : 0);
627 series[i].Tries = rates[i].count;
629 series[i].RateFlags = (
630 (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) ?
631 ATH9K_RATESERIES_RTS_CTS : 0) |
632 ((rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ?
633 ATH9K_RATESERIES_2040 : 0) |
634 ((rates[i].flags & IEEE80211_TX_RC_SHORT_GI) ?
635 ATH9K_RATESERIES_HALFGI : 0);
637 series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
638 (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) != 0,
639 (rates[i].flags & IEEE80211_TX_RC_SHORT_GI),
640 bf_isshpreamble(bf));
642 series[i].ChSel = sc->sc_tx_chainmask;
645 series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
648 /* set dur_update_en for l-sig computation except for PS-Poll frames */
649 ath9k_hw_set11n_ratescenario(ah, ds, lastds, !bf_ispspoll(bf),
650 ctsrate, ctsduration,
653 if (sc->sc_config.ath_aggr_prot && flags)
654 ath9k_hw_set11n_burstduration(ah, ds, 8192);
658 * Function to send a normal HT (non-AMPDU) frame
659 * NB: must be called with txq lock held
661 static int ath_tx_send_normal(struct ath_softc *sc,
663 struct ath_atx_tid *tid,
664 struct list_head *bf_head)
668 BUG_ON(list_empty(bf_head));
670 bf = list_first_entry(bf_head, struct ath_buf, list);
671 bf->bf_state.bf_type &= ~BUF_AMPDU; /* regular HT frame */
673 /* update starting sequence number for subsequent ADDBA request */
674 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
676 /* Queue to h/w without aggregation */
678 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
679 ath_buf_set_rate(sc, bf);
680 ath_tx_txqaddbuf(sc, txq, bf_head);
685 /* flush tid's software queue and send frames as non-ampdu's */
687 static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
689 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
691 struct list_head bf_head;
692 INIT_LIST_HEAD(&bf_head);
694 ASSERT(tid->paused > 0);
695 spin_lock_bh(&txq->axq_lock);
699 if (tid->paused > 0) {
700 spin_unlock_bh(&txq->axq_lock);
704 while (!list_empty(&tid->buf_q)) {
705 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
706 ASSERT(!bf_isretried(bf));
707 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
708 ath_tx_send_normal(sc, txq, tid, &bf_head);
711 spin_unlock_bh(&txq->axq_lock);
714 /* Completion routine of an aggregate */
716 static void ath_tx_complete_aggr_rifs(struct ath_softc *sc,
719 struct list_head *bf_q,
722 struct ath_node *an = NULL;
724 struct ieee80211_tx_info *tx_info;
725 struct ath_atx_tid *tid = NULL;
726 struct ath_buf *bf_last = bf->bf_lastbf;
727 struct ath_desc *ds = bf_last->bf_desc;
728 struct ath_buf *bf_next, *bf_lastq = NULL;
729 struct list_head bf_head, bf_pending;
731 u32 ba[WME_BA_BMP_SIZE >> 5];
732 int isaggr, txfail, txpending, sendbar = 0, needreset = 0;
734 skb = (struct sk_buff *)bf->bf_mpdu;
735 tx_info = IEEE80211_SKB_CB(skb);
737 if (tx_info->control.sta) {
738 an = (struct ath_node *)tx_info->control.sta->drv_priv;
739 tid = ATH_AN_2_TID(an, bf->bf_tidno);
742 isaggr = bf_isaggr(bf);
745 if (ATH_DS_TX_BA(ds)) {
747 * extract starting sequence and
750 seq_st = ATH_DS_BA_SEQ(ds);
752 ATH_DS_BA_BITMAP(ds),
753 WME_BA_BMP_SIZE >> 3);
755 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
758 * AR5416 can become deaf/mute when BA
759 * issue happens. Chip needs to be reset.
760 * But AP code may have sychronization issues
761 * when perform internal reset in this routine.
762 * Only enable reset in STA mode for now.
764 if (sc->sc_ah->ah_opmode ==
765 NL80211_IFTYPE_STATION)
769 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
773 INIT_LIST_HEAD(&bf_pending);
774 INIT_LIST_HEAD(&bf_head);
777 txfail = txpending = 0;
778 bf_next = bf->bf_next;
780 if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
781 /* transmit completion, subframe is
782 * acked by block ack */
783 } else if (!isaggr && txok) {
784 /* transmit completion */
787 if (!(tid->state & AGGR_CLEANUP) &&
788 ds->ds_txstat.ts_flags != ATH9K_TX_SW_ABORTED) {
789 if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
790 ath_tx_set_retry(sc, bf);
793 bf->bf_state.bf_type |= BUF_XRETRY;
799 * cleanup in progress, just fail
800 * the un-acked sub-frames
806 * Remove ath_buf's of this sub-frame from aggregate queue.
808 if (bf_next == NULL) { /* last subframe in the aggregate */
809 ASSERT(bf->bf_lastfrm == bf_last);
812 * The last descriptor of the last sub frame could be
813 * a holding descriptor for h/w. If that's the case,
814 * bf->bf_lastfrm won't be in the bf_q.
815 * Make sure we handle bf_q properly here.
818 if (!list_empty(bf_q)) {
819 bf_lastq = list_entry(bf_q->prev,
820 struct ath_buf, list);
821 list_cut_position(&bf_head,
822 bf_q, &bf_lastq->list);
825 * XXX: if the last subframe only has one
826 * descriptor which is also being used as
827 * a holding descriptor. Then the ath_buf
828 * is not in the bf_q at all.
830 INIT_LIST_HEAD(&bf_head);
833 ASSERT(!list_empty(bf_q));
834 list_cut_position(&bf_head,
835 bf_q, &bf->bf_lastfrm->list);
840 * complete the acked-ones/xretried ones; update
843 spin_lock_bh(&txq->axq_lock);
844 ath_tx_update_baw(sc, tid, bf->bf_seqno);
845 spin_unlock_bh(&txq->axq_lock);
847 /* complete this sub-frame */
848 ath_tx_complete_buf(sc, bf, &bf_head, !txfail, sendbar);
851 * retry the un-acked ones
854 * XXX: if the last descriptor is holding descriptor,
855 * in order to requeue the frame to software queue, we
856 * need to allocate a new descriptor and
857 * copy the content of holding descriptor to it.
859 if (bf->bf_next == NULL &&
860 bf_last->bf_status & ATH_BUFSTATUS_STALE) {
863 /* allocate new descriptor */
864 spin_lock_bh(&sc->tx.txbuflock);
865 ASSERT(!list_empty((&sc->tx.txbuf)));
866 tbf = list_first_entry(&sc->tx.txbuf,
867 struct ath_buf, list);
868 list_del(&tbf->list);
869 spin_unlock_bh(&sc->tx.txbuflock);
871 ATH_TXBUF_RESET(tbf);
873 /* copy descriptor content */
874 tbf->bf_mpdu = bf_last->bf_mpdu;
875 tbf->bf_buf_addr = bf_last->bf_buf_addr;
876 *(tbf->bf_desc) = *(bf_last->bf_desc);
878 /* link it to the frame */
880 bf_lastq->bf_desc->ds_link =
882 bf->bf_lastfrm = tbf;
883 ath9k_hw_cleartxdesc(sc->sc_ah,
884 bf->bf_lastfrm->bf_desc);
886 tbf->bf_state = bf_last->bf_state;
887 tbf->bf_lastfrm = tbf;
888 ath9k_hw_cleartxdesc(sc->sc_ah,
889 tbf->bf_lastfrm->bf_desc);
891 /* copy the DMA context */
893 bf_last->bf_dmacontext;
895 list_add_tail(&tbf->list, &bf_head);
898 * Clear descriptor status words for
901 ath9k_hw_cleartxdesc(sc->sc_ah,
902 bf->bf_lastfrm->bf_desc);
906 * Put this buffer to the temporary pending
907 * queue to retain ordering
909 list_splice_tail_init(&bf_head, &bf_pending);
915 if (tid->state & AGGR_CLEANUP) {
916 /* check to see if we're done with cleaning the h/w queue */
917 spin_lock_bh(&txq->axq_lock);
919 if (tid->baw_head == tid->baw_tail) {
920 tid->state &= ~AGGR_ADDBA_COMPLETE;
921 tid->addba_exchangeattempts = 0;
922 spin_unlock_bh(&txq->axq_lock);
924 tid->state &= ~AGGR_CLEANUP;
926 /* send buffered frames as singles */
927 ath_tx_flush_tid(sc, tid);
929 spin_unlock_bh(&txq->axq_lock);
935 * prepend un-acked frames to the beginning of the pending frame queue
937 if (!list_empty(&bf_pending)) {
938 spin_lock_bh(&txq->axq_lock);
939 /* Note: we _prepend_, we _do_not_ at to
940 * the end of the queue ! */
941 list_splice(&bf_pending, &tid->buf_q);
942 ath_tx_queue_tid(txq, tid);
943 spin_unlock_bh(&txq->axq_lock);
947 ath_reset(sc, false);
952 static void ath_tx_rc_status(struct ath_buf *bf, struct ath_desc *ds, int nbad)
954 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
955 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
956 struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
958 tx_info_priv->update_rc = false;
959 if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
960 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
962 if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
963 (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
965 memcpy(&tx_info_priv->tx, &ds->ds_txstat,
966 sizeof(tx_info_priv->tx));
967 tx_info_priv->n_frames = bf->bf_nframes;
968 tx_info_priv->n_bad_frames = nbad;
969 tx_info_priv->update_rc = true;
974 /* Process completed xmit descriptors from the specified queue */
976 static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
978 struct ath_hal *ah = sc->sc_ah;
979 struct ath_buf *bf, *lastbf, *bf_held = NULL;
980 struct list_head bf_head;
985 DPRINTF(sc, ATH_DBG_QUEUE, "tx queue %d (%x), link %p\n",
986 txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
990 spin_lock_bh(&txq->axq_lock);
991 if (list_empty(&txq->axq_q)) {
992 txq->axq_link = NULL;
993 txq->axq_linkbuf = NULL;
994 spin_unlock_bh(&txq->axq_lock);
997 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
1000 * There is a race condition that a BH gets scheduled
1001 * after sw writes TxE and before hw re-load the last
1002 * descriptor to get the newly chained one.
1003 * Software must keep the last DONE descriptor as a
1004 * holding descriptor - software does so by marking
1005 * it with the STALE flag.
1008 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
1010 if (list_is_last(&bf_held->list, &txq->axq_q)) {
1012 * The holding descriptor is the last
1013 * descriptor in queue. It's safe to remove
1014 * the last holding descriptor in BH context.
1016 spin_unlock_bh(&txq->axq_lock);
1019 /* Lets work with the next buffer now */
1020 bf = list_entry(bf_held->list.next,
1021 struct ath_buf, list);
1025 lastbf = bf->bf_lastbf;
1026 ds = lastbf->bf_desc; /* NB: last decriptor */
1028 status = ath9k_hw_txprocdesc(ah, ds);
1029 if (status == -EINPROGRESS) {
1030 spin_unlock_bh(&txq->axq_lock);
1033 if (bf->bf_desc == txq->axq_lastdsWithCTS)
1034 txq->axq_lastdsWithCTS = NULL;
1035 if (ds == txq->axq_gatingds)
1036 txq->axq_gatingds = NULL;
1039 * Remove ath_buf's of the same transmit unit from txq,
1040 * however leave the last descriptor back as the holding
1041 * descriptor for hw.
1043 lastbf->bf_status |= ATH_BUFSTATUS_STALE;
1044 INIT_LIST_HEAD(&bf_head);
1046 if (!list_is_singular(&lastbf->list))
1047 list_cut_position(&bf_head,
1048 &txq->axq_q, lastbf->list.prev);
1053 txq->axq_aggr_depth--;
1055 txok = (ds->ds_txstat.ts_status == 0);
1057 spin_unlock_bh(&txq->axq_lock);
1060 list_del(&bf_held->list);
1061 spin_lock_bh(&sc->tx.txbuflock);
1062 list_add_tail(&bf_held->list, &sc->tx.txbuf);
1063 spin_unlock_bh(&sc->tx.txbuflock);
1066 if (!bf_isampdu(bf)) {
1068 * This frame is sent out as a single frame.
1069 * Use hardware retry status for this frame.
1071 bf->bf_retries = ds->ds_txstat.ts_longretry;
1072 if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
1073 bf->bf_state.bf_type |= BUF_XRETRY;
1076 nbad = ath_tx_num_badfrms(sc, bf, txok);
1079 ath_tx_rc_status(bf, ds, nbad);
1082 * Complete this transmit unit
1085 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, txok);
1087 ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
1089 /* Wake up mac80211 queue */
1091 spin_lock_bh(&txq->axq_lock);
1092 if (txq->stopped && ath_txq_depth(sc, txq->axq_qnum) <=
1095 qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
1097 ieee80211_wake_queue(sc->hw, qnum);
1104 * schedule any pending packets if aggregation is enabled
1106 if (sc->sc_flags & SC_OP_TXAGGR)
1107 ath_txq_schedule(sc, txq);
1108 spin_unlock_bh(&txq->axq_lock);
1112 static void ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
1114 struct ath_hal *ah = sc->sc_ah;
1116 (void) ath9k_hw_stoptxdma(ah, txq->axq_qnum);
1117 DPRINTF(sc, ATH_DBG_XMIT, "tx queue [%u] %x, link %p\n",
1118 txq->axq_qnum, ath9k_hw_gettxbuf(ah, txq->axq_qnum),
1122 /* Drain only the data queues */
1124 static void ath_drain_txdataq(struct ath_softc *sc, bool retry_tx)
1126 struct ath_hal *ah = sc->sc_ah;
1127 int i, status, npend = 0;
1129 if (!(sc->sc_flags & SC_OP_INVALID)) {
1130 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1131 if (ATH_TXQ_SETUP(sc, i)) {
1132 ath_tx_stopdma(sc, &sc->tx.txq[i]);
1133 /* The TxDMA may not really be stopped.
1134 * Double check the hal tx pending count */
1135 npend += ath9k_hw_numtxpending(ah,
1136 sc->tx.txq[i].axq_qnum);
1142 /* TxDMA not stopped, reset the hal */
1143 DPRINTF(sc, ATH_DBG_XMIT, "Unable to stop TxDMA. Reset HAL!\n");
1145 spin_lock_bh(&sc->sc_resetlock);
1146 if (!ath9k_hw_reset(ah,
1147 sc->sc_ah->ah_curchan,
1149 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
1150 sc->sc_ht_extprotspacing, true, &status)) {
1152 DPRINTF(sc, ATH_DBG_FATAL,
1153 "Unable to reset hardware; hal status %u\n",
1156 spin_unlock_bh(&sc->sc_resetlock);
1159 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1160 if (ATH_TXQ_SETUP(sc, i))
1161 ath_tx_draintxq(sc, &sc->tx.txq[i], retry_tx);
1165 /* Add a sub-frame to block ack window */
1167 static void ath_tx_addto_baw(struct ath_softc *sc,
1168 struct ath_atx_tid *tid,
1173 if (bf_isretried(bf))
1176 index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
1177 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
1179 ASSERT(tid->tx_buf[cindex] == NULL);
1180 tid->tx_buf[cindex] = bf;
1182 if (index >= ((tid->baw_tail - tid->baw_head) &
1183 (ATH_TID_MAX_BUFS - 1))) {
1184 tid->baw_tail = cindex;
1185 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
1190 * Function to send an A-MPDU
1191 * NB: must be called with txq lock held
1193 static int ath_tx_send_ampdu(struct ath_softc *sc,
1194 struct ath_atx_tid *tid,
1195 struct list_head *bf_head,
1196 struct ath_tx_control *txctl)
1200 BUG_ON(list_empty(bf_head));
1202 bf = list_first_entry(bf_head, struct ath_buf, list);
1203 bf->bf_state.bf_type |= BUF_AMPDU;
1206 * Do not queue to h/w when any of the following conditions is true:
1207 * - there are pending frames in software queue
1208 * - the TID is currently paused for ADDBA/BAR request
1209 * - seqno is not within block-ack window
1210 * - h/w queue depth exceeds low water mark
1212 if (!list_empty(&tid->buf_q) || tid->paused ||
1213 !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
1214 txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
1216 * Add this frame to software queue for scheduling later
1219 list_splice_tail_init(bf_head, &tid->buf_q);
1220 ath_tx_queue_tid(txctl->txq, tid);
1224 /* Add sub-frame to BAW */
1225 ath_tx_addto_baw(sc, tid, bf);
1227 /* Queue to h/w without aggregation */
1229 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
1230 ath_buf_set_rate(sc, bf);
1231 ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
1238 * returns aggr limit based on lowest of the rates
1240 static u32 ath_lookup_rate(struct ath_softc *sc,
1242 struct ath_atx_tid *tid)
1244 struct ath_rate_table *rate_table = sc->cur_rate_table;
1245 struct sk_buff *skb;
1246 struct ieee80211_tx_info *tx_info;
1247 struct ieee80211_tx_rate *rates;
1248 struct ath_tx_info_priv *tx_info_priv;
1249 u32 max_4ms_framelen, frame_length;
1250 u16 aggr_limit, legacy = 0, maxampdu;
1253 skb = (struct sk_buff *)bf->bf_mpdu;
1254 tx_info = IEEE80211_SKB_CB(skb);
1255 rates = tx_info->control.rates;
1257 (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
1260 * Find the lowest frame length among the rate series that will have a
1261 * 4ms transmit duration.
1262 * TODO - TXOP limit needs to be considered.
1264 max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
1266 for (i = 0; i < 4; i++) {
1267 if (rates[i].count) {
1268 if (!WLAN_RC_PHY_HT(rate_table->info[rates[i].idx].phy)) {
1274 rate_table->info[rates[i].idx].max_4ms_framelen;
1275 max_4ms_framelen = min(max_4ms_framelen, frame_length);
1280 * limit aggregate size by the minimum rate if rate selected is
1281 * not a probe rate, if rate selected is a probe rate then
1282 * avoid aggregation of this packet.
1284 if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
1287 aggr_limit = min(max_4ms_framelen,
1288 (u32)ATH_AMPDU_LIMIT_DEFAULT);
1291 * h/w can accept aggregates upto 16 bit lengths (65535).
1292 * The IE, however can hold upto 65536, which shows up here
1293 * as zero. Ignore 65536 since we are constrained by hw.
1295 maxampdu = tid->an->maxampdu;
1297 aggr_limit = min(aggr_limit, maxampdu);
1303 * returns the number of delimiters to be added to
1304 * meet the minimum required mpdudensity.
1305 * caller should make sure that the rate is HT rate .
1307 static int ath_compute_num_delims(struct ath_softc *sc,
1308 struct ath_atx_tid *tid,
1312 struct ath_rate_table *rt = sc->cur_rate_table;
1313 struct sk_buff *skb = bf->bf_mpdu;
1314 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1315 u32 nsymbits, nsymbols, mpdudensity;
1318 int width, half_gi, ndelim, mindelim;
1320 /* Select standard number of delimiters based on frame length alone */
1321 ndelim = ATH_AGGR_GET_NDELIM(frmlen);
1324 * If encryption enabled, hardware requires some more padding between
1326 * TODO - this could be improved to be dependent on the rate.
1327 * The hardware can keep up at lower rates, but not higher rates
1329 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
1330 ndelim += ATH_AGGR_ENCRYPTDELIM;
1333 * Convert desired mpdu density from microeconds to bytes based
1334 * on highest rate in rate series (i.e. first rate) to determine
1335 * required minimum length for subframe. Take into account
1336 * whether high rate is 20 or 40Mhz and half or full GI.
1338 mpdudensity = tid->an->mpdudensity;
1341 * If there is no mpdu density restriction, no further calculation
1344 if (mpdudensity == 0)
1347 rix = tx_info->control.rates[0].idx;
1348 flags = tx_info->control.rates[0].flags;
1349 rc = rt->info[rix].ratecode;
1350 width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
1351 half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
1354 nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
1356 nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
1361 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
1362 minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
1364 /* Is frame shorter than required minimum length? */
1365 if (frmlen < minlen) {
1366 /* Get the minimum number of delimiters required. */
1367 mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
1368 ndelim = max(mindelim, ndelim);
1375 * For aggregation from software buffer queue.
1376 * NB: must be called with txq lock held
1378 static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
1379 struct ath_atx_tid *tid,
1380 struct list_head *bf_q,
1381 struct ath_buf **bf_last,
1382 struct aggr_rifs_param *param,
1385 #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
1386 struct ath_buf *bf, *tbf, *bf_first, *bf_prev = NULL;
1387 struct list_head bf_head;
1388 int rl = 0, nframes = 0, ndelim;
1389 u16 aggr_limit = 0, al = 0, bpad = 0,
1390 al_delta, h_baw = tid->baw_size / 2;
1391 enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
1393 INIT_LIST_HEAD(&bf_head);
1395 BUG_ON(list_empty(&tid->buf_q));
1397 bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
1400 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1403 * do not step over block-ack window
1405 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
1406 status = ATH_AGGR_BAW_CLOSED;
1411 aggr_limit = ath_lookup_rate(sc, bf, tid);
1416 * do not exceed aggregation limit
1418 al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
1420 if (nframes && (aggr_limit <
1421 (al + bpad + al_delta + prev_al))) {
1422 status = ATH_AGGR_LIMITED;
1427 * do not exceed subframe limit
1429 if ((nframes + *prev_frames) >=
1430 min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
1431 status = ATH_AGGR_LIMITED;
1436 * add padding for previous frame to aggregation length
1438 al += bpad + al_delta;
1441 * Get the delimiters needed to meet the MPDU
1442 * density for this node.
1444 ndelim = ath_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
1446 bpad = PADBYTES(al_delta) + (ndelim << 2);
1449 bf->bf_lastfrm->bf_desc->ds_link = 0;
1452 * this packet is part of an aggregate
1453 * - remove all descriptors belonging to this frame from
1455 * - add it to block ack window
1456 * - set up descriptors for aggregation
1458 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1459 ath_tx_addto_baw(sc, tid, bf);
1461 list_for_each_entry(tbf, &bf_head, list) {
1462 ath9k_hw_set11n_aggr_middle(sc->sc_ah,
1463 tbf->bf_desc, ndelim);
1467 * link buffers of this frame to the aggregate
1469 list_splice_tail_init(&bf_head, bf_q);
1473 bf_prev->bf_next = bf;
1474 bf_prev->bf_lastfrm->bf_desc->ds_link = bf->bf_daddr;
1480 * terminate aggregation on a small packet boundary
1482 if (bf->bf_frmlen < ATH_AGGR_MINPLEN) {
1483 status = ATH_AGGR_SHORTPKT;
1487 } while (!list_empty(&tid->buf_q));
1489 bf_first->bf_al = al;
1490 bf_first->bf_nframes = nframes;
1497 * process pending frames possibly doing a-mpdu aggregation
1498 * NB: must be called with txq lock held
1500 static void ath_tx_sched_aggr(struct ath_softc *sc,
1501 struct ath_txq *txq, struct ath_atx_tid *tid)
1503 struct ath_buf *bf, *tbf, *bf_last, *bf_lastaggr = NULL;
1504 enum ATH_AGGR_STATUS status;
1505 struct list_head bf_q;
1506 struct aggr_rifs_param param = {0, 0, 0, 0, NULL};
1507 int prev_frames = 0;
1510 if (list_empty(&tid->buf_q))
1513 INIT_LIST_HEAD(&bf_q);
1515 status = ath_tx_form_aggr(sc, tid, &bf_q, &bf_lastaggr, ¶m,
1519 * no frames picked up to be aggregated; block-ack
1520 * window is not open
1522 if (list_empty(&bf_q))
1525 bf = list_first_entry(&bf_q, struct ath_buf, list);
1526 bf_last = list_entry(bf_q.prev, struct ath_buf, list);
1527 bf->bf_lastbf = bf_last;
1530 * if only one frame, send as non-aggregate
1532 if (bf->bf_nframes == 1) {
1533 ASSERT(bf->bf_lastfrm == bf_last);
1535 bf->bf_state.bf_type &= ~BUF_AGGR;
1537 * clear aggr bits for every descriptor
1538 * XXX TODO: is there a way to optimize it?
1540 list_for_each_entry(tbf, &bf_q, list) {
1541 ath9k_hw_clr11n_aggr(sc->sc_ah, tbf->bf_desc);
1544 ath_buf_set_rate(sc, bf);
1545 ath_tx_txqaddbuf(sc, txq, &bf_q);
1550 * setup first desc with rate and aggr info
1552 bf->bf_state.bf_type |= BUF_AGGR;
1553 ath_buf_set_rate(sc, bf);
1554 ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
1557 * anchor last frame of aggregate correctly
1559 ASSERT(bf_lastaggr);
1560 ASSERT(bf_lastaggr->bf_lastfrm == bf_last);
1562 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1564 /* XXX: We don't enter into this loop, consider removing this */
1565 while (!list_empty(&bf_q) && !list_is_last(&tbf->list, &bf_q)) {
1566 tbf = list_entry(tbf->list.next, struct ath_buf, list);
1567 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1570 txq->axq_aggr_depth++;
1573 * Normal aggregate, queue to hardware
1575 ath_tx_txqaddbuf(sc, txq, &bf_q);
1577 } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
1578 status != ATH_AGGR_BAW_CLOSED);
1581 /* Called with txq lock held */
1583 static void ath_tid_drain(struct ath_softc *sc,
1584 struct ath_txq *txq,
1585 struct ath_atx_tid *tid)
1589 struct list_head bf_head;
1590 INIT_LIST_HEAD(&bf_head);
1593 if (list_empty(&tid->buf_q))
1595 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1597 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1599 /* update baw for software retried frame */
1600 if (bf_isretried(bf))
1601 ath_tx_update_baw(sc, tid, bf->bf_seqno);
1604 * do not indicate packets while holding txq spinlock.
1605 * unlock is intentional here
1607 spin_unlock(&txq->axq_lock);
1609 /* complete this sub-frame */
1610 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
1612 spin_lock(&txq->axq_lock);
1616 * TODO: For frame(s) that are in the retry state, we will reuse the
1617 * sequence number(s) without setting the retry bit. The
1618 * alternative is to give up on these and BAR the receiver's window
1621 tid->seq_next = tid->seq_start;
1622 tid->baw_tail = tid->baw_head;
1626 * Drain all pending buffers
1627 * NB: must be called with txq lock held
1629 static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
1630 struct ath_txq *txq)
1632 struct ath_atx_ac *ac, *ac_tmp;
1633 struct ath_atx_tid *tid, *tid_tmp;
1635 list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
1636 list_del(&ac->list);
1638 list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
1639 list_del(&tid->list);
1641 ath_tid_drain(sc, txq, tid);
1646 static int ath_tx_setup_buffer(struct ath_softc *sc, struct ath_buf *bf,
1647 struct sk_buff *skb,
1648 struct ath_tx_control *txctl)
1650 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1651 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1652 struct ath_tx_info_priv *tx_info_priv;
1656 tx_info_priv = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
1657 if (unlikely(!tx_info_priv))
1659 tx_info->rate_driver_data[0] = tx_info_priv;
1660 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1661 fc = hdr->frame_control;
1663 ATH_TXBUF_RESET(bf);
1667 bf->bf_frmlen = skb->len + FCS_LEN - (hdrlen & 3);
1669 ieee80211_is_data(fc) ?
1670 (bf->bf_state.bf_type |= BUF_DATA) :
1671 (bf->bf_state.bf_type &= ~BUF_DATA);
1672 ieee80211_is_back_req(fc) ?
1673 (bf->bf_state.bf_type |= BUF_BAR) :
1674 (bf->bf_state.bf_type &= ~BUF_BAR);
1675 ieee80211_is_pspoll(fc) ?
1676 (bf->bf_state.bf_type |= BUF_PSPOLL) :
1677 (bf->bf_state.bf_type &= ~BUF_PSPOLL);
1678 (sc->sc_flags & SC_OP_PREAMBLE_SHORT) ?
1679 (bf->bf_state.bf_type |= BUF_SHORT_PREAMBLE) :
1680 (bf->bf_state.bf_type &= ~BUF_SHORT_PREAMBLE);
1681 (sc->hw->conf.ht.enabled && !is_pae(skb) &&
1682 (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) ?
1683 (bf->bf_state.bf_type |= BUF_HT) :
1684 (bf->bf_state.bf_type &= ~BUF_HT);
1686 bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
1690 bf->bf_keytype = get_hw_crypto_keytype(skb);
1692 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR) {
1693 bf->bf_frmlen += tx_info->control.hw_key->icv_len;
1694 bf->bf_keyix = tx_info->control.hw_key->hw_key_idx;
1696 bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
1699 /* Assign seqno, tidno */
1701 if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR))
1702 assign_aggr_tid_seqno(skb, bf);
1708 bf->bf_dmacontext = pci_map_single(sc->pdev, skb->data,
1709 skb->len, PCI_DMA_TODEVICE);
1710 if (unlikely(pci_dma_mapping_error(sc->pdev, bf->bf_dmacontext))) {
1712 DPRINTF(sc, ATH_DBG_CONFIG,
1713 "pci_dma_mapping_error() on TX\n");
1717 bf->bf_buf_addr = bf->bf_dmacontext;
1721 /* FIXME: tx power */
1722 static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
1723 struct ath_tx_control *txctl)
1725 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
1726 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1727 struct ath_node *an = NULL;
1728 struct list_head bf_head;
1729 struct ath_desc *ds;
1730 struct ath_atx_tid *tid;
1731 struct ath_hal *ah = sc->sc_ah;
1734 frm_type = get_hw_packet_type(skb);
1736 INIT_LIST_HEAD(&bf_head);
1737 list_add_tail(&bf->list, &bf_head);
1739 /* setup descriptor */
1743 ds->ds_data = bf->bf_buf_addr;
1745 /* Formulate first tx descriptor with tx controls */
1747 ath9k_hw_set11n_txdesc(ah, ds, bf->bf_frmlen, frm_type, MAX_RATE_POWER,
1748 bf->bf_keyix, bf->bf_keytype, bf->bf_flags);
1750 ath9k_hw_filltxdesc(ah, ds,
1751 skb->len, /* segment length */
1752 true, /* first segment */
1753 true, /* last segment */
1754 ds); /* first descriptor */
1756 bf->bf_lastfrm = bf;
1758 spin_lock_bh(&txctl->txq->axq_lock);
1760 if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR) &&
1761 tx_info->control.sta) {
1762 an = (struct ath_node *)tx_info->control.sta->drv_priv;
1763 tid = ATH_AN_2_TID(an, bf->bf_tidno);
1765 if (ath_aggr_query(sc, an, bf->bf_tidno)) {
1767 * Try aggregation if it's a unicast data frame
1768 * and the destination is HT capable.
1770 ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
1773 * Send this frame as regular when ADDBA
1774 * exchange is neither complete nor pending.
1776 ath_tx_send_normal(sc, txctl->txq,
1783 ath_buf_set_rate(sc, bf);
1784 ath_tx_txqaddbuf(sc, txctl->txq, &bf_head);
1787 spin_unlock_bh(&txctl->txq->axq_lock);
1790 /* Upon failure caller should free skb */
1791 int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb,
1792 struct ath_tx_control *txctl)
1797 /* Check if a tx buffer is available */
1799 bf = ath_tx_get_buffer(sc);
1801 DPRINTF(sc, ATH_DBG_XMIT, "TX buffers are full\n");
1805 r = ath_tx_setup_buffer(sc, bf, skb, txctl);
1807 struct ath_txq *txq = txctl->txq;
1809 DPRINTF(sc, ATH_DBG_FATAL, "TX mem alloc failure\n");
1811 /* upon ath_tx_processq() this TX queue will be resumed, we
1812 * guarantee this will happen by knowing beforehand that
1813 * we will at least have to run TX completionon one buffer
1815 spin_lock_bh(&txq->axq_lock);
1816 if (ath_txq_depth(sc, txq->axq_qnum) > 1) {
1817 ieee80211_stop_queue(sc->hw,
1818 skb_get_queue_mapping(skb));
1821 spin_unlock_bh(&txq->axq_lock);
1823 spin_lock_bh(&sc->tx.txbuflock);
1824 list_add_tail(&bf->list, &sc->tx.txbuf);
1825 spin_unlock_bh(&sc->tx.txbuflock);
1830 ath_tx_start_dma(sc, bf, txctl);
1835 /* Initialize TX queue and h/w */
1837 int ath_tx_init(struct ath_softc *sc, int nbufs)
1842 spin_lock_init(&sc->tx.txbuflock);
1844 /* Setup tx descriptors */
1845 error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
1848 DPRINTF(sc, ATH_DBG_FATAL,
1849 "Failed to allocate tx descriptors: %d\n",
1854 /* XXX allocate beacon state together with vap */
1855 error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
1856 "beacon", ATH_BCBUF, 1);
1858 DPRINTF(sc, ATH_DBG_FATAL,
1859 "Failed to allocate beacon descriptors: %d\n",
1872 /* Reclaim all tx queue resources */
1874 int ath_tx_cleanup(struct ath_softc *sc)
1876 /* cleanup beacon descriptors */
1877 if (sc->beacon.bdma.dd_desc_len != 0)
1878 ath_descdma_cleanup(sc, &sc->beacon.bdma, &sc->beacon.bbuf);
1880 /* cleanup tx descriptors */
1881 if (sc->tx.txdma.dd_desc_len != 0)
1882 ath_descdma_cleanup(sc, &sc->tx.txdma, &sc->tx.txbuf);
1887 /* Setup a h/w transmit queue */
1889 struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
1891 struct ath_hal *ah = sc->sc_ah;
1892 struct ath9k_tx_queue_info qi;
1895 memset(&qi, 0, sizeof(qi));
1896 qi.tqi_subtype = subtype;
1897 qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
1898 qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
1899 qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
1900 qi.tqi_physCompBuf = 0;
1903 * Enable interrupts only for EOL and DESC conditions.
1904 * We mark tx descriptors to receive a DESC interrupt
1905 * when a tx queue gets deep; otherwise waiting for the
1906 * EOL to reap descriptors. Note that this is done to
1907 * reduce interrupt load and this only defers reaping
1908 * descriptors, never transmitting frames. Aside from
1909 * reducing interrupts this also permits more concurrency.
1910 * The only potential downside is if the tx queue backs
1911 * up in which case the top half of the kernel may backup
1912 * due to a lack of tx descriptors.
1914 * The UAPSD queue is an exception, since we take a desc-
1915 * based intr on the EOSP frames.
1917 if (qtype == ATH9K_TX_QUEUE_UAPSD)
1918 qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1920 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
1921 TXQ_FLAG_TXDESCINT_ENABLE;
1922 qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
1925 * NB: don't print a message, this happens
1926 * normally on parts with too few tx queues
1930 if (qnum >= ARRAY_SIZE(sc->tx.txq)) {
1931 DPRINTF(sc, ATH_DBG_FATAL,
1932 "qnum %u out of range, max %u!\n",
1933 qnum, (unsigned int)ARRAY_SIZE(sc->tx.txq));
1934 ath9k_hw_releasetxqueue(ah, qnum);
1937 if (!ATH_TXQ_SETUP(sc, qnum)) {
1938 struct ath_txq *txq = &sc->tx.txq[qnum];
1940 txq->axq_qnum = qnum;
1941 txq->axq_link = NULL;
1942 INIT_LIST_HEAD(&txq->axq_q);
1943 INIT_LIST_HEAD(&txq->axq_acq);
1944 spin_lock_init(&txq->axq_lock);
1946 txq->axq_aggr_depth = 0;
1947 txq->axq_totalqueued = 0;
1948 txq->axq_linkbuf = NULL;
1949 sc->tx.txqsetup |= 1<<qnum;
1951 return &sc->tx.txq[qnum];
1954 /* Reclaim resources for a setup queue */
1956 void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1958 ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
1959 sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
1963 * Setup a hardware data transmit queue for the specified
1964 * access control. The hal may not support all requested
1965 * queues in which case it will return a reference to a
1966 * previously setup queue. We record the mapping from ac's
1967 * to h/w queues for use by ath_tx_start and also track
1968 * the set of h/w queues being used to optimize work in the
1969 * transmit interrupt handler and related routines.
1972 int ath_tx_setup(struct ath_softc *sc, int haltype)
1974 struct ath_txq *txq;
1976 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
1977 DPRINTF(sc, ATH_DBG_FATAL,
1978 "HAL AC %u out of range, max %zu!\n",
1979 haltype, ARRAY_SIZE(sc->tx.hwq_map));
1982 txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
1984 sc->tx.hwq_map[haltype] = txq->axq_qnum;
1990 int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
1995 case ATH9K_TX_QUEUE_DATA:
1996 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
1997 DPRINTF(sc, ATH_DBG_FATAL,
1998 "HAL AC %u out of range, max %zu!\n",
1999 haltype, ARRAY_SIZE(sc->tx.hwq_map));
2002 qnum = sc->tx.hwq_map[haltype];
2004 case ATH9K_TX_QUEUE_BEACON:
2005 qnum = sc->beacon.beaconq;
2007 case ATH9K_TX_QUEUE_CAB:
2008 qnum = sc->beacon.cabq->axq_qnum;
2016 /* Get a transmit queue, if available */
2018 struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
2020 struct ath_txq *txq = NULL;
2023 qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
2024 txq = &sc->tx.txq[qnum];
2026 spin_lock_bh(&txq->axq_lock);
2028 /* Try to avoid running out of descriptors */
2029 if (txq->axq_depth >= (ATH_TXBUF - 20)) {
2030 DPRINTF(sc, ATH_DBG_FATAL,
2031 "TX queue: %d is full, depth: %d\n",
2032 qnum, txq->axq_depth);
2033 ieee80211_stop_queue(sc->hw, skb_get_queue_mapping(skb));
2035 spin_unlock_bh(&txq->axq_lock);
2039 spin_unlock_bh(&txq->axq_lock);
2044 /* Update parameters for a transmit queue */
2046 int ath_txq_update(struct ath_softc *sc, int qnum,
2047 struct ath9k_tx_queue_info *qinfo)
2049 struct ath_hal *ah = sc->sc_ah;
2051 struct ath9k_tx_queue_info qi;
2053 if (qnum == sc->beacon.beaconq) {
2055 * XXX: for beacon queue, we just save the parameter.
2056 * It will be picked up by ath_beaconq_config when
2059 sc->beacon.beacon_qi = *qinfo;
2063 ASSERT(sc->tx.txq[qnum].axq_qnum == qnum);
2065 ath9k_hw_get_txq_props(ah, qnum, &qi);
2066 qi.tqi_aifs = qinfo->tqi_aifs;
2067 qi.tqi_cwmin = qinfo->tqi_cwmin;
2068 qi.tqi_cwmax = qinfo->tqi_cwmax;
2069 qi.tqi_burstTime = qinfo->tqi_burstTime;
2070 qi.tqi_readyTime = qinfo->tqi_readyTime;
2072 if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
2073 DPRINTF(sc, ATH_DBG_FATAL,
2074 "Unable to update hardware queue %u!\n", qnum);
2077 ath9k_hw_resettxqueue(ah, qnum); /* push to h/w */
2083 int ath_cabq_update(struct ath_softc *sc)
2085 struct ath9k_tx_queue_info qi;
2086 int qnum = sc->beacon.cabq->axq_qnum;
2087 struct ath_beacon_config conf;
2089 ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
2091 * Ensure the readytime % is within the bounds.
2093 if (sc->sc_config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
2094 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
2095 else if (sc->sc_config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
2096 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
2098 ath_get_beaconconfig(sc, ATH_IF_ID_ANY, &conf);
2100 (conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
2101 ath_txq_update(sc, qnum, &qi);
2106 /* Deferred processing of transmit interrupt */
2108 void ath_tx_tasklet(struct ath_softc *sc)
2111 u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
2113 ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
2116 * Process each active queue.
2118 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2119 if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
2120 ath_tx_processq(sc, &sc->tx.txq[i]);
2124 void ath_tx_draintxq(struct ath_softc *sc,
2125 struct ath_txq *txq, bool retry_tx)
2127 struct ath_buf *bf, *lastbf;
2128 struct list_head bf_head;
2130 INIT_LIST_HEAD(&bf_head);
2133 * NB: this assumes output has been stopped and
2134 * we do not need to block ath_tx_tasklet
2137 spin_lock_bh(&txq->axq_lock);
2139 if (list_empty(&txq->axq_q)) {
2140 txq->axq_link = NULL;
2141 txq->axq_linkbuf = NULL;
2142 spin_unlock_bh(&txq->axq_lock);
2146 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2148 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
2149 list_del(&bf->list);
2150 spin_unlock_bh(&txq->axq_lock);
2152 spin_lock_bh(&sc->tx.txbuflock);
2153 list_add_tail(&bf->list, &sc->tx.txbuf);
2154 spin_unlock_bh(&sc->tx.txbuflock);
2158 lastbf = bf->bf_lastbf;
2160 lastbf->bf_desc->ds_txstat.ts_flags =
2161 ATH9K_TX_SW_ABORTED;
2163 /* remove ath_buf's of the same mpdu from txq */
2164 list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
2167 spin_unlock_bh(&txq->axq_lock);
2170 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, 0);
2172 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2175 /* flush any pending frames if aggregation is enabled */
2176 if (sc->sc_flags & SC_OP_TXAGGR) {
2178 spin_lock_bh(&txq->axq_lock);
2179 ath_txq_drain_pending_buffers(sc, txq);
2180 spin_unlock_bh(&txq->axq_lock);
2185 /* Drain the transmit queues and reclaim resources */
2187 void ath_draintxq(struct ath_softc *sc, bool retry_tx)
2189 /* stop beacon queue. The beacon will be freed when
2190 * we go to INIT state */
2191 if (!(sc->sc_flags & SC_OP_INVALID)) {
2192 (void) ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
2193 DPRINTF(sc, ATH_DBG_XMIT, "beacon queue %x\n",
2194 ath9k_hw_gettxbuf(sc->sc_ah, sc->beacon.beaconq));
2197 ath_drain_txdataq(sc, retry_tx);
2200 u32 ath_txq_depth(struct ath_softc *sc, int qnum)
2202 return sc->tx.txq[qnum].axq_depth;
2205 u32 ath_txq_aggr_depth(struct ath_softc *sc, int qnum)
2207 return sc->tx.txq[qnum].axq_aggr_depth;
2210 bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an, u8 tidno)
2212 struct ath_atx_tid *txtid;
2214 if (!(sc->sc_flags & SC_OP_TXAGGR))
2217 txtid = ATH_AN_2_TID(an, tidno);
2219 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
2220 if (!(txtid->state & AGGR_ADDBA_PROGRESS) &&
2221 (txtid->addba_exchangeattempts < ADDBA_EXCHANGE_ATTEMPTS)) {
2222 txtid->addba_exchangeattempts++;
2230 /* Start TX aggregation */
2232 int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
2235 struct ath_atx_tid *txtid;
2236 struct ath_node *an;
2238 an = (struct ath_node *)sta->drv_priv;
2240 if (sc->sc_flags & SC_OP_TXAGGR) {
2241 txtid = ATH_AN_2_TID(an, tid);
2242 txtid->state |= AGGR_ADDBA_PROGRESS;
2243 ath_tx_pause_tid(sc, txtid);
2249 /* Stop tx aggregation */
2251 int ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
2253 struct ath_node *an = (struct ath_node *)sta->drv_priv;
2255 ath_tx_aggr_teardown(sc, an, tid);
2259 /* Resume tx aggregation */
2261 void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
2263 struct ath_atx_tid *txtid;
2264 struct ath_node *an;
2266 an = (struct ath_node *)sta->drv_priv;
2268 if (sc->sc_flags & SC_OP_TXAGGR) {
2269 txtid = ATH_AN_2_TID(an, tid);
2271 IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
2272 txtid->state |= AGGR_ADDBA_COMPLETE;
2273 txtid->state &= ~AGGR_ADDBA_PROGRESS;
2274 ath_tx_resume_tid(sc, txtid);
2279 * Performs transmit side cleanup when TID changes from aggregated to
2281 * - Pause the TID and mark cleanup in progress
2282 * - Discard all retry frames from the s/w queue.
2285 void ath_tx_aggr_teardown(struct ath_softc *sc, struct ath_node *an, u8 tid)
2287 struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
2288 struct ath_txq *txq = &sc->tx.txq[txtid->ac->qnum];
2290 struct list_head bf_head;
2291 INIT_LIST_HEAD(&bf_head);
2293 if (txtid->state & AGGR_CLEANUP) /* cleanup is in progress */
2296 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
2297 txtid->addba_exchangeattempts = 0;
2301 /* TID must be paused first */
2302 ath_tx_pause_tid(sc, txtid);
2304 /* drop all software retried frames and mark this TID */
2305 spin_lock_bh(&txq->axq_lock);
2306 while (!list_empty(&txtid->buf_q)) {
2307 bf = list_first_entry(&txtid->buf_q, struct ath_buf, list);
2308 if (!bf_isretried(bf)) {
2310 * NB: it's based on the assumption that
2311 * software retried frame will always stay
2312 * at the head of software queue.
2316 list_cut_position(&bf_head,
2317 &txtid->buf_q, &bf->bf_lastfrm->list);
2318 ath_tx_update_baw(sc, txtid, bf->bf_seqno);
2320 /* complete this sub-frame */
2321 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2324 if (txtid->baw_head != txtid->baw_tail) {
2325 spin_unlock_bh(&txq->axq_lock);
2326 txtid->state |= AGGR_CLEANUP;
2328 txtid->state &= ~AGGR_ADDBA_COMPLETE;
2329 txtid->addba_exchangeattempts = 0;
2330 spin_unlock_bh(&txq->axq_lock);
2331 ath_tx_flush_tid(sc, txtid);
2336 * Tx scheduling logic
2337 * NB: must be called with txq lock held
2340 void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
2342 struct ath_atx_ac *ac;
2343 struct ath_atx_tid *tid;
2345 /* nothing to schedule */
2346 if (list_empty(&txq->axq_acq))
2349 * get the first node/ac pair on the queue
2351 ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
2352 list_del(&ac->list);
2356 * process a single tid per destination
2359 /* nothing to schedule */
2360 if (list_empty(&ac->tid_q))
2363 tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
2364 list_del(&tid->list);
2367 if (tid->paused) /* check next tid to keep h/w busy */
2370 if ((txq->axq_depth % 2) == 0)
2371 ath_tx_sched_aggr(sc, txq, tid);
2374 * add tid to round-robin queue if more frames
2375 * are pending for the tid
2377 if (!list_empty(&tid->buf_q))
2378 ath_tx_queue_tid(txq, tid);
2380 /* only schedule one TID at a time */
2382 } while (!list_empty(&ac->tid_q));
2385 * schedule AC if more TIDs need processing
2387 if (!list_empty(&ac->tid_q)) {
2389 * add dest ac to txq if not already added
2393 list_add_tail(&ac->list, &txq->axq_acq);
2398 /* Initialize per-node transmit state */
2400 void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2402 struct ath_atx_tid *tid;
2403 struct ath_atx_ac *ac;
2407 * Init per tid tx state
2409 for (tidno = 0, tid = &an->tid[tidno];
2410 tidno < WME_NUM_TID;
2414 tid->seq_start = tid->seq_next = 0;
2415 tid->baw_size = WME_MAX_BA;
2416 tid->baw_head = tid->baw_tail = 0;
2418 tid->paused = false;
2419 tid->state &= ~AGGR_CLEANUP;
2420 INIT_LIST_HEAD(&tid->buf_q);
2422 acno = TID_TO_WME_AC(tidno);
2423 tid->ac = &an->ac[acno];
2426 tid->state &= ~AGGR_ADDBA_COMPLETE;
2427 tid->state &= ~AGGR_ADDBA_PROGRESS;
2428 tid->addba_exchangeattempts = 0;
2432 * Init per ac tx state
2434 for (acno = 0, ac = &an->ac[acno];
2435 acno < WME_NUM_AC; acno++, ac++) {
2437 INIT_LIST_HEAD(&ac->tid_q);
2441 ac->qnum = ath_tx_get_qnum(sc,
2442 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
2445 ac->qnum = ath_tx_get_qnum(sc,
2446 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
2449 ac->qnum = ath_tx_get_qnum(sc,
2450 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
2453 ac->qnum = ath_tx_get_qnum(sc,
2454 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
2460 /* Cleanupthe pending buffers for the node. */
2462 void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
2465 struct ath_atx_ac *ac, *ac_tmp;
2466 struct ath_atx_tid *tid, *tid_tmp;
2467 struct ath_txq *txq;
2468 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2469 if (ATH_TXQ_SETUP(sc, i)) {
2470 txq = &sc->tx.txq[i];
2472 spin_lock(&txq->axq_lock);
2474 list_for_each_entry_safe(ac,
2475 ac_tmp, &txq->axq_acq, list) {
2476 tid = list_first_entry(&ac->tid_q,
2477 struct ath_atx_tid, list);
2478 if (tid && tid->an != an)
2480 list_del(&ac->list);
2483 list_for_each_entry_safe(tid,
2484 tid_tmp, &ac->tid_q, list) {
2485 list_del(&tid->list);
2487 ath_tid_drain(sc, txq, tid);
2488 tid->state &= ~AGGR_ADDBA_COMPLETE;
2489 tid->addba_exchangeattempts = 0;
2490 tid->state &= ~AGGR_CLEANUP;
2494 spin_unlock(&txq->axq_lock);
2499 void ath_tx_cabq(struct ath_softc *sc, struct sk_buff *skb)
2501 int hdrlen, padsize;
2502 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2503 struct ath_tx_control txctl;
2505 memset(&txctl, 0, sizeof(struct ath_tx_control));
2508 * As a temporary workaround, assign seq# here; this will likely need
2509 * to be cleaned up to work better with Beacon transmission and virtual
2512 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
2513 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2514 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
2515 sc->tx.seq_no += 0x10;
2516 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
2517 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
2520 /* Add the padding after the header if this is not already done */
2521 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
2523 padsize = hdrlen % 4;
2524 if (skb_headroom(skb) < padsize) {
2525 DPRINTF(sc, ATH_DBG_XMIT, "TX CABQ padding failed\n");
2526 dev_kfree_skb_any(skb);
2529 skb_push(skb, padsize);
2530 memmove(skb->data, skb->data + padsize, hdrlen);
2533 txctl.txq = sc->beacon.cabq;
2535 DPRINTF(sc, ATH_DBG_XMIT, "transmitting CABQ packet, skb: %p\n", skb);
2537 if (ath_tx_start(sc, skb, &txctl) != 0) {
2538 DPRINTF(sc, ATH_DBG_XMIT, "CABQ TX failed\n");
2544 dev_kfree_skb_any(skb);