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 "%s: txq depth = %d\n", __func__, 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 "%s: TXDP[%u] = %llx (%p)\n",
92 __func__, txq->axq_qnum,
93 ito64(bf->bf_daddr), bf->bf_desc);
95 *txq->axq_link = bf->bf_daddr;
96 DPRINTF(sc, ATH_DBG_XMIT, "%s: link[%u] (%p)=%llx (%p)\n",
98 txq->axq_qnum, txq->axq_link,
99 ito64(bf->bf_daddr), bf->bf_desc);
101 txq->axq_link = &(bf->bf_lastbf->bf_desc->ds_link);
102 ath9k_hw_txstart(ah, txq->axq_qnum);
105 /* Check if it's okay to send out aggregates */
107 static int ath_aggr_query(struct ath_softc *sc, struct ath_node *an, u8 tidno)
109 struct ath_atx_tid *tid;
110 tid = ATH_AN_2_TID(an, tidno);
112 if (tid->state & AGGR_ADDBA_COMPLETE ||
113 tid->state & AGGR_ADDBA_PROGRESS)
119 /* Calculate Atheros packet type from IEEE80211 packet header */
121 static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
123 struct ieee80211_hdr *hdr;
124 enum ath9k_pkt_type htype;
127 hdr = (struct ieee80211_hdr *)skb->data;
128 fc = hdr->frame_control;
130 if (ieee80211_is_beacon(fc))
131 htype = ATH9K_PKT_TYPE_BEACON;
132 else if (ieee80211_is_probe_resp(fc))
133 htype = ATH9K_PKT_TYPE_PROBE_RESP;
134 else if (ieee80211_is_atim(fc))
135 htype = ATH9K_PKT_TYPE_ATIM;
136 else if (ieee80211_is_pspoll(fc))
137 htype = ATH9K_PKT_TYPE_PSPOLL;
139 htype = ATH9K_PKT_TYPE_NORMAL;
144 static bool is_pae(struct sk_buff *skb)
146 struct ieee80211_hdr *hdr;
149 hdr = (struct ieee80211_hdr *)skb->data;
150 fc = hdr->frame_control;
152 if (ieee80211_is_data(fc)) {
153 if (ieee80211_is_nullfunc(fc) ||
154 /* Port Access Entity (IEEE 802.1X) */
155 (skb->protocol == cpu_to_be16(ETH_P_PAE))) {
163 static int get_hw_crypto_keytype(struct sk_buff *skb)
165 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
167 if (tx_info->control.hw_key) {
168 if (tx_info->control.hw_key->alg == ALG_WEP)
169 return ATH9K_KEY_TYPE_WEP;
170 else if (tx_info->control.hw_key->alg == ALG_TKIP)
171 return ATH9K_KEY_TYPE_TKIP;
172 else if (tx_info->control.hw_key->alg == ALG_CCMP)
173 return ATH9K_KEY_TYPE_AES;
176 return ATH9K_KEY_TYPE_CLEAR;
179 /* Called only when tx aggregation is enabled and HT is supported */
181 static void assign_aggr_tid_seqno(struct sk_buff *skb,
184 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
185 struct ieee80211_hdr *hdr;
187 struct ath_atx_tid *tid;
191 if (!tx_info->control.sta)
194 an = (struct ath_node *)tx_info->control.sta->drv_priv;
195 hdr = (struct ieee80211_hdr *)skb->data;
196 fc = hdr->frame_control;
200 if (ieee80211_is_data_qos(fc)) {
201 qc = ieee80211_get_qos_ctl(hdr);
202 bf->bf_tidno = qc[0] & 0xf;
207 if (ieee80211_is_data(fc) && !is_pae(skb)) {
208 /* For HT capable stations, we save tidno for later use.
209 * We also override seqno set by upper layer with the one
210 * in tx aggregation state.
212 * If fragmentation is on, the sequence number is
213 * not overridden, since it has been
214 * incremented by the fragmentation routine.
216 * FIXME: check if the fragmentation threshold exceeds
219 tid = ATH_AN_2_TID(an, bf->bf_tidno);
220 hdr->seq_ctrl = cpu_to_le16(tid->seq_next <<
221 IEEE80211_SEQ_SEQ_SHIFT);
222 bf->bf_seqno = tid->seq_next;
223 INCR(tid->seq_next, IEEE80211_SEQ_MAX);
227 static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
230 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
233 flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
234 flags |= ATH9K_TXDESC_INTREQ;
236 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
237 flags |= ATH9K_TXDESC_NOACK;
238 if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
239 flags |= ATH9K_TXDESC_RTSENA;
244 static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
246 struct ath_buf *bf = NULL;
248 spin_lock_bh(&sc->sc_txbuflock);
250 if (unlikely(list_empty(&sc->sc_txbuf))) {
251 spin_unlock_bh(&sc->sc_txbuflock);
255 bf = list_first_entry(&sc->sc_txbuf, struct ath_buf, list);
258 spin_unlock_bh(&sc->sc_txbuflock);
263 /* To complete a chain of buffers associated a frame */
265 static void ath_tx_complete_buf(struct ath_softc *sc,
267 struct list_head *bf_q,
268 int txok, int sendbar)
270 struct sk_buff *skb = bf->bf_mpdu;
271 struct ath_xmit_status tx_status;
274 * Set retry information.
275 * NB: Don't use the information in the descriptor, because the frame
276 * could be software retried.
278 tx_status.retries = bf->bf_retries;
282 tx_status.flags = ATH_TX_BAR;
285 tx_status.flags |= ATH_TX_ERROR;
287 if (bf_isxretried(bf))
288 tx_status.flags |= ATH_TX_XRETRY;
291 /* Unmap this frame */
292 pci_unmap_single(sc->pdev,
296 /* complete this frame */
297 ath_tx_complete(sc, skb, &tx_status);
300 * Return the list of ath_buf of this mpdu to free queue
302 spin_lock_bh(&sc->sc_txbuflock);
303 list_splice_tail_init(bf_q, &sc->sc_txbuf);
304 spin_unlock_bh(&sc->sc_txbuflock);
308 * queue up a dest/ac pair for tx scheduling
309 * NB: must be called with txq lock held
312 static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
314 struct ath_atx_ac *ac = tid->ac;
317 * if tid is paused, hold off
323 * add tid to ac atmost once
329 list_add_tail(&tid->list, &ac->tid_q);
332 * add node ac to txq atmost once
338 list_add_tail(&ac->list, &txq->axq_acq);
343 static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
345 struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
347 spin_lock_bh(&txq->axq_lock);
351 spin_unlock_bh(&txq->axq_lock);
354 /* resume a tid and schedule aggregate */
356 void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
358 struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
360 ASSERT(tid->paused > 0);
361 spin_lock_bh(&txq->axq_lock);
368 if (list_empty(&tid->buf_q))
372 * Add this TID to scheduler and try to send out aggregates
374 ath_tx_queue_tid(txq, tid);
375 ath_txq_schedule(sc, txq);
377 spin_unlock_bh(&txq->axq_lock);
380 /* Compute the number of bad frames */
382 static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
385 struct ath_buf *bf_last = bf->bf_lastbf;
386 struct ath_desc *ds = bf_last->bf_desc;
388 u32 ba[WME_BA_BMP_SIZE >> 5];
393 if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
396 isaggr = bf_isaggr(bf);
398 seq_st = ATH_DS_BA_SEQ(ds);
399 memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
403 ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
404 if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
413 static void ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
416 struct ieee80211_hdr *hdr;
418 bf->bf_state.bf_type |= BUF_RETRY;
422 hdr = (struct ieee80211_hdr *)skb->data;
423 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
426 /* Update block ack window */
428 static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
433 index = ATH_BA_INDEX(tid->seq_start, seqno);
434 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
436 tid->tx_buf[cindex] = NULL;
438 while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
439 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
440 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
445 * ath_pkt_dur - compute packet duration (NB: not NAV)
448 * pktlen - total bytes (delims + data + fcs + pads + pad delims)
449 * width - 0 for 20 MHz, 1 for 40 MHz
450 * half_gi - to use 4us v/s 3.6 us for symbol time
452 static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
453 int width, int half_gi, bool shortPreamble)
455 struct ath_rate_table *rate_table = sc->hw_rate_table[sc->sc_curmode];
456 u32 nbits, nsymbits, duration, nsymbols;
460 pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
461 rc = rate_table->info[rix].ratecode;
463 /* for legacy rates, use old function to compute packet duration */
465 return ath9k_hw_computetxtime(sc->sc_ah, rate_table, pktlen,
468 /* find number of symbols: PLCP + data */
469 nbits = (pktlen << 3) + OFDM_PLCP_BITS;
470 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
471 nsymbols = (nbits + nsymbits - 1) / nsymbits;
474 duration = SYMBOL_TIME(nsymbols);
476 duration = SYMBOL_TIME_HALFGI(nsymbols);
478 /* addup duration for legacy/ht training and signal fields */
479 streams = HT_RC_2_STREAMS(rc);
480 duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
485 /* Rate module function to set rate related fields in tx descriptor */
487 static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
489 struct ath_hal *ah = sc->sc_ah;
490 struct ath_rate_table *rt;
491 struct ath_desc *ds = bf->bf_desc;
492 struct ath_desc *lastds = bf->bf_lastbf->bf_desc;
493 struct ath9k_11n_rate_series series[4];
494 struct ath_node *an = NULL;
496 struct ieee80211_tx_info *tx_info;
497 struct ieee80211_tx_rate *rates;
498 struct ieee80211_hdr *hdr;
499 int i, flags, rtsctsena = 0;
501 u8 rix = 0, cix, ctsrate = 0;
504 memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
506 skb = (struct sk_buff *)bf->bf_mpdu;
507 hdr = (struct ieee80211_hdr *)skb->data;
508 fc = hdr->frame_control;
509 tx_info = IEEE80211_SKB_CB(skb);
510 rates = tx_info->control.rates;
512 if (tx_info->control.sta)
513 an = (struct ath_node *)tx_info->control.sta->drv_priv;
515 if (ieee80211_has_morefrags(fc) ||
516 (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)) {
517 rates[1].count = rates[2].count = rates[3].count = 0;
518 rates[1].idx = rates[2].idx = rates[3].idx = 0;
519 rates[0].count = ATH_TXMAXTRY;
522 /* get the cix for the lowest valid rix */
523 rt = sc->hw_rate_table[sc->sc_curmode];
524 for (i = 3; i >= 0; i--) {
525 if (rates[i].count && (rates[i].idx >= 0)) {
531 flags = (bf->bf_flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA));
532 cix = rt->info[rix].ctrl_rate;
535 * If 802.11g protection is enabled, determine whether to use RTS/CTS or
536 * just CTS. Note that this is only done for OFDM/HT unicast frames.
538 if (sc->sc_protmode != PROT_M_NONE && !(bf->bf_flags & ATH9K_TXDESC_NOACK)
539 && (rt->info[rix].phy == WLAN_RC_PHY_OFDM ||
540 WLAN_RC_PHY_HT(rt->info[rix].phy))) {
541 if (sc->sc_protmode == PROT_M_RTSCTS)
542 flags = ATH9K_TXDESC_RTSENA;
543 else if (sc->sc_protmode == PROT_M_CTSONLY)
544 flags = ATH9K_TXDESC_CTSENA;
546 cix = rt->info[sc->sc_protrix].ctrl_rate;
550 /* For 11n, the default behavior is to enable RTS for hw retried frames.
551 * We enable the global flag here and let rate series flags determine
552 * which rates will actually use RTS.
554 if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) && bf_isdata(bf)) {
555 /* 802.11g protection not needed, use our default behavior */
557 flags = ATH9K_TXDESC_RTSENA;
560 /* Set protection if aggregate protection on */
561 if (sc->sc_config.ath_aggr_prot &&
562 (!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
563 flags = ATH9K_TXDESC_RTSENA;
564 cix = rt->info[sc->sc_protrix].ctrl_rate;
568 /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
569 if (bf_isaggr(bf) && (bf->bf_al > ah->ah_caps.rts_aggr_limit))
570 flags &= ~(ATH9K_TXDESC_RTSENA);
573 * CTS transmit rate is derived from the transmit rate by looking in the
574 * h/w rate table. We must also factor in whether or not a short
575 * preamble is to be used. NB: cix is set above where RTS/CTS is enabled
577 ctsrate = rt->info[cix].ratecode |
578 (bf_isshpreamble(bf) ? rt->info[cix].short_preamble : 0);
580 for (i = 0; i < 4; i++) {
581 if (!rates[i].count || (rates[i].idx < 0))
586 series[i].Rate = rt->info[rix].ratecode |
587 (bf_isshpreamble(bf) ? rt->info[rix].short_preamble : 0);
589 series[i].Tries = rates[i].count;
591 series[i].RateFlags = (
592 (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) ?
593 ATH9K_RATESERIES_RTS_CTS : 0) |
594 ((rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ?
595 ATH9K_RATESERIES_2040 : 0) |
596 ((rates[i].flags & IEEE80211_TX_RC_SHORT_GI) ?
597 ATH9K_RATESERIES_HALFGI : 0);
599 series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
600 (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) != 0,
601 (rates[i].flags & IEEE80211_TX_RC_SHORT_GI),
602 bf_isshpreamble(bf));
604 if (bf_isht(bf) && an)
605 series[i].ChSel = ath_chainmask_sel_logic(sc, an);
607 series[i].ChSel = sc->sc_tx_chainmask;
610 series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
613 /* set dur_update_en for l-sig computation except for PS-Poll frames */
614 ath9k_hw_set11n_ratescenario(ah, ds, lastds, !bf_ispspoll(bf),
615 ctsrate, ctsduration,
618 if (sc->sc_config.ath_aggr_prot && flags)
619 ath9k_hw_set11n_burstduration(ah, ds, 8192);
623 * Function to send a normal HT (non-AMPDU) frame
624 * NB: must be called with txq lock held
626 static int ath_tx_send_normal(struct ath_softc *sc,
628 struct ath_atx_tid *tid,
629 struct list_head *bf_head)
633 BUG_ON(list_empty(bf_head));
635 bf = list_first_entry(bf_head, struct ath_buf, list);
636 bf->bf_state.bf_type &= ~BUF_AMPDU; /* regular HT frame */
638 /* update starting sequence number for subsequent ADDBA request */
639 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
641 /* Queue to h/w without aggregation */
643 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
644 ath_buf_set_rate(sc, bf);
645 ath_tx_txqaddbuf(sc, txq, bf_head);
650 /* flush tid's software queue and send frames as non-ampdu's */
652 static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
654 struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
656 struct list_head bf_head;
657 INIT_LIST_HEAD(&bf_head);
659 ASSERT(tid->paused > 0);
660 spin_lock_bh(&txq->axq_lock);
664 if (tid->paused > 0) {
665 spin_unlock_bh(&txq->axq_lock);
669 while (!list_empty(&tid->buf_q)) {
670 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
671 ASSERT(!bf_isretried(bf));
672 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
673 ath_tx_send_normal(sc, txq, tid, &bf_head);
676 spin_unlock_bh(&txq->axq_lock);
679 /* Completion routine of an aggregate */
681 static void ath_tx_complete_aggr_rifs(struct ath_softc *sc,
684 struct list_head *bf_q,
687 struct ath_node *an = NULL;
689 struct ieee80211_tx_info *tx_info;
690 struct ath_atx_tid *tid = NULL;
691 struct ath_buf *bf_last = bf->bf_lastbf;
692 struct ath_desc *ds = bf_last->bf_desc;
693 struct ath_buf *bf_next, *bf_lastq = NULL;
694 struct list_head bf_head, bf_pending;
696 u32 ba[WME_BA_BMP_SIZE >> 5];
697 int isaggr, txfail, txpending, sendbar = 0, needreset = 0;
699 skb = (struct sk_buff *)bf->bf_mpdu;
700 tx_info = IEEE80211_SKB_CB(skb);
702 if (tx_info->control.sta) {
703 an = (struct ath_node *)tx_info->control.sta->drv_priv;
704 tid = ATH_AN_2_TID(an, bf->bf_tidno);
707 isaggr = bf_isaggr(bf);
710 if (ATH_DS_TX_BA(ds)) {
712 * extract starting sequence and
715 seq_st = ATH_DS_BA_SEQ(ds);
717 ATH_DS_BA_BITMAP(ds),
718 WME_BA_BMP_SIZE >> 3);
720 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
723 * AR5416 can become deaf/mute when BA
724 * issue happens. Chip needs to be reset.
725 * But AP code may have sychronization issues
726 * when perform internal reset in this routine.
727 * Only enable reset in STA mode for now.
729 if (sc->sc_ah->ah_opmode == ATH9K_M_STA)
733 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
737 INIT_LIST_HEAD(&bf_pending);
738 INIT_LIST_HEAD(&bf_head);
741 txfail = txpending = 0;
742 bf_next = bf->bf_next;
744 if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
745 /* transmit completion, subframe is
746 * acked by block ack */
747 } else if (!isaggr && txok) {
748 /* transmit completion */
751 if (!(tid->state & AGGR_CLEANUP) &&
752 ds->ds_txstat.ts_flags != ATH9K_TX_SW_ABORTED) {
753 if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
754 ath_tx_set_retry(sc, bf);
757 bf->bf_state.bf_type |= BUF_XRETRY;
763 * cleanup in progress, just fail
764 * the un-acked sub-frames
770 * Remove ath_buf's of this sub-frame from aggregate queue.
772 if (bf_next == NULL) { /* last subframe in the aggregate */
773 ASSERT(bf->bf_lastfrm == bf_last);
776 * The last descriptor of the last sub frame could be
777 * a holding descriptor for h/w. If that's the case,
778 * bf->bf_lastfrm won't be in the bf_q.
779 * Make sure we handle bf_q properly here.
782 if (!list_empty(bf_q)) {
783 bf_lastq = list_entry(bf_q->prev,
784 struct ath_buf, list);
785 list_cut_position(&bf_head,
786 bf_q, &bf_lastq->list);
789 * XXX: if the last subframe only has one
790 * descriptor which is also being used as
791 * a holding descriptor. Then the ath_buf
792 * is not in the bf_q at all.
794 INIT_LIST_HEAD(&bf_head);
797 ASSERT(!list_empty(bf_q));
798 list_cut_position(&bf_head,
799 bf_q, &bf->bf_lastfrm->list);
804 * complete the acked-ones/xretried ones; update
807 spin_lock_bh(&txq->axq_lock);
808 ath_tx_update_baw(sc, tid, bf->bf_seqno);
809 spin_unlock_bh(&txq->axq_lock);
811 /* complete this sub-frame */
812 ath_tx_complete_buf(sc, bf, &bf_head, !txfail, sendbar);
815 * retry the un-acked ones
818 * XXX: if the last descriptor is holding descriptor,
819 * in order to requeue the frame to software queue, we
820 * need to allocate a new descriptor and
821 * copy the content of holding descriptor to it.
823 if (bf->bf_next == NULL &&
824 bf_last->bf_status & ATH_BUFSTATUS_STALE) {
827 /* allocate new descriptor */
828 spin_lock_bh(&sc->sc_txbuflock);
829 ASSERT(!list_empty((&sc->sc_txbuf)));
830 tbf = list_first_entry(&sc->sc_txbuf,
831 struct ath_buf, list);
832 list_del(&tbf->list);
833 spin_unlock_bh(&sc->sc_txbuflock);
835 ATH_TXBUF_RESET(tbf);
837 /* copy descriptor content */
838 tbf->bf_mpdu = bf_last->bf_mpdu;
839 tbf->bf_buf_addr = bf_last->bf_buf_addr;
840 *(tbf->bf_desc) = *(bf_last->bf_desc);
842 /* link it to the frame */
844 bf_lastq->bf_desc->ds_link =
846 bf->bf_lastfrm = tbf;
847 ath9k_hw_cleartxdesc(sc->sc_ah,
848 bf->bf_lastfrm->bf_desc);
850 tbf->bf_state = bf_last->bf_state;
851 tbf->bf_lastfrm = tbf;
852 ath9k_hw_cleartxdesc(sc->sc_ah,
853 tbf->bf_lastfrm->bf_desc);
855 /* copy the DMA context */
857 bf_last->bf_dmacontext;
859 list_add_tail(&tbf->list, &bf_head);
862 * Clear descriptor status words for
865 ath9k_hw_cleartxdesc(sc->sc_ah,
866 bf->bf_lastfrm->bf_desc);
870 * Put this buffer to the temporary pending
871 * queue to retain ordering
873 list_splice_tail_init(&bf_head, &bf_pending);
879 if (tid->state & AGGR_CLEANUP) {
880 /* check to see if we're done with cleaning the h/w queue */
881 spin_lock_bh(&txq->axq_lock);
883 if (tid->baw_head == tid->baw_tail) {
884 tid->state &= ~AGGR_ADDBA_COMPLETE;
885 tid->addba_exchangeattempts = 0;
886 spin_unlock_bh(&txq->axq_lock);
888 tid->state &= ~AGGR_CLEANUP;
890 /* send buffered frames as singles */
891 ath_tx_flush_tid(sc, tid);
893 spin_unlock_bh(&txq->axq_lock);
899 * prepend un-acked frames to the beginning of the pending frame queue
901 if (!list_empty(&bf_pending)) {
902 spin_lock_bh(&txq->axq_lock);
903 /* Note: we _prepend_, we _do_not_ at to
904 * the end of the queue ! */
905 list_splice(&bf_pending, &tid->buf_q);
906 ath_tx_queue_tid(txq, tid);
907 spin_unlock_bh(&txq->axq_lock);
911 ath_reset(sc, false);
916 /* Process completed xmit descriptors from the specified queue */
918 static int ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
920 struct ath_hal *ah = sc->sc_ah;
921 struct ath_buf *bf, *lastbf, *bf_held = NULL;
922 struct list_head bf_head;
923 struct ath_desc *ds, *tmp_ds;
925 struct ieee80211_tx_info *tx_info;
926 struct ath_tx_info_priv *tx_info_priv;
927 int nacked, txok, nbad = 0, isrifs = 0;
930 DPRINTF(sc, ATH_DBG_QUEUE,
931 "%s: tx queue %d (%x), link %p\n", __func__,
932 txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
937 spin_lock_bh(&txq->axq_lock);
938 if (list_empty(&txq->axq_q)) {
939 txq->axq_link = NULL;
940 txq->axq_linkbuf = NULL;
941 spin_unlock_bh(&txq->axq_lock);
944 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
947 * There is a race condition that a BH gets scheduled
948 * after sw writes TxE and before hw re-load the last
949 * descriptor to get the newly chained one.
950 * Software must keep the last DONE descriptor as a
951 * holding descriptor - software does so by marking
952 * it with the STALE flag.
955 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
957 if (list_is_last(&bf_held->list, &txq->axq_q)) {
959 * The holding descriptor is the last
960 * descriptor in queue. It's safe to remove
961 * the last holding descriptor in BH context.
963 spin_unlock_bh(&txq->axq_lock);
966 /* Lets work with the next buffer now */
967 bf = list_entry(bf_held->list.next,
968 struct ath_buf, list);
972 lastbf = bf->bf_lastbf;
973 ds = lastbf->bf_desc; /* NB: last decriptor */
975 status = ath9k_hw_txprocdesc(ah, ds);
976 if (status == -EINPROGRESS) {
977 spin_unlock_bh(&txq->axq_lock);
980 if (bf->bf_desc == txq->axq_lastdsWithCTS)
981 txq->axq_lastdsWithCTS = NULL;
982 if (ds == txq->axq_gatingds)
983 txq->axq_gatingds = NULL;
986 * Remove ath_buf's of the same transmit unit from txq,
987 * however leave the last descriptor back as the holding
990 lastbf->bf_status |= ATH_BUFSTATUS_STALE;
991 INIT_LIST_HEAD(&bf_head);
993 if (!list_is_singular(&lastbf->list))
994 list_cut_position(&bf_head,
995 &txq->axq_q, lastbf->list.prev);
1000 txq->axq_aggr_depth--;
1002 txok = (ds->ds_txstat.ts_status == 0);
1004 spin_unlock_bh(&txq->axq_lock);
1007 list_del(&bf_held->list);
1008 spin_lock_bh(&sc->sc_txbuflock);
1009 list_add_tail(&bf_held->list, &sc->sc_txbuf);
1010 spin_unlock_bh(&sc->sc_txbuflock);
1013 if (!bf_isampdu(bf)) {
1015 * This frame is sent out as a single frame.
1016 * Use hardware retry status for this frame.
1018 bf->bf_retries = ds->ds_txstat.ts_longretry;
1019 if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
1020 bf->bf_state.bf_type |= BUF_XRETRY;
1023 nbad = ath_tx_num_badfrms(sc, bf, txok);
1026 tx_info = IEEE80211_SKB_CB(skb);
1029 (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
1030 if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
1031 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1032 if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
1033 (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
1034 if (ds->ds_txstat.ts_status == 0)
1037 if (bf_isdata(bf)) {
1039 tmp_ds = bf->bf_rifslast->bf_desc;
1042 memcpy(&tx_info_priv->tx,
1044 sizeof(tx_info_priv->tx));
1045 tx_info_priv->n_frames = bf->bf_nframes;
1046 tx_info_priv->n_bad_frames = nbad;
1051 * Complete this transmit unit
1054 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, txok);
1056 ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
1058 /* Wake up mac80211 queue */
1060 spin_lock_bh(&txq->axq_lock);
1061 if (txq->stopped && ath_txq_depth(sc, txq->axq_qnum) <=
1064 qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
1066 ieee80211_wake_queue(sc->hw, qnum);
1073 * schedule any pending packets if aggregation is enabled
1075 if (sc->sc_flags & SC_OP_TXAGGR)
1076 ath_txq_schedule(sc, txq);
1077 spin_unlock_bh(&txq->axq_lock);
1082 static void ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
1084 struct ath_hal *ah = sc->sc_ah;
1086 (void) ath9k_hw_stoptxdma(ah, txq->axq_qnum);
1087 DPRINTF(sc, ATH_DBG_XMIT, "%s: tx queue [%u] %x, link %p\n",
1088 __func__, txq->axq_qnum,
1089 ath9k_hw_gettxbuf(ah, txq->axq_qnum), txq->axq_link);
1092 /* Drain only the data queues */
1094 static void ath_drain_txdataq(struct ath_softc *sc, bool retry_tx)
1096 struct ath_hal *ah = sc->sc_ah;
1097 int i, status, npend = 0;
1099 if (!(sc->sc_flags & SC_OP_INVALID)) {
1100 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1101 if (ATH_TXQ_SETUP(sc, i)) {
1102 ath_tx_stopdma(sc, &sc->sc_txq[i]);
1103 /* The TxDMA may not really be stopped.
1104 * Double check the hal tx pending count */
1105 npend += ath9k_hw_numtxpending(ah,
1106 sc->sc_txq[i].axq_qnum);
1112 /* TxDMA not stopped, reset the hal */
1113 DPRINTF(sc, ATH_DBG_XMIT,
1114 "%s: Unable to stop TxDMA. Reset HAL!\n", __func__);
1116 spin_lock_bh(&sc->sc_resetlock);
1117 if (!ath9k_hw_reset(ah,
1118 sc->sc_ah->ah_curchan,
1119 sc->sc_ht_info.tx_chan_width,
1120 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
1121 sc->sc_ht_extprotspacing, true, &status)) {
1123 DPRINTF(sc, ATH_DBG_FATAL,
1124 "%s: unable to reset hardware; hal status %u\n",
1128 spin_unlock_bh(&sc->sc_resetlock);
1131 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1132 if (ATH_TXQ_SETUP(sc, i))
1133 ath_tx_draintxq(sc, &sc->sc_txq[i], retry_tx);
1137 /* Add a sub-frame to block ack window */
1139 static void ath_tx_addto_baw(struct ath_softc *sc,
1140 struct ath_atx_tid *tid,
1145 if (bf_isretried(bf))
1148 index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
1149 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
1151 ASSERT(tid->tx_buf[cindex] == NULL);
1152 tid->tx_buf[cindex] = bf;
1154 if (index >= ((tid->baw_tail - tid->baw_head) &
1155 (ATH_TID_MAX_BUFS - 1))) {
1156 tid->baw_tail = cindex;
1157 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
1162 * Function to send an A-MPDU
1163 * NB: must be called with txq lock held
1166 static int ath_tx_send_ampdu(struct ath_softc *sc,
1167 struct ath_atx_tid *tid,
1168 struct list_head *bf_head,
1169 struct ath_tx_control *txctl)
1173 BUG_ON(list_empty(bf_head));
1175 bf = list_first_entry(bf_head, struct ath_buf, list);
1176 bf->bf_state.bf_type |= BUF_AMPDU;
1179 * Do not queue to h/w when any of the following conditions is true:
1180 * - there are pending frames in software queue
1181 * - the TID is currently paused for ADDBA/BAR request
1182 * - seqno is not within block-ack window
1183 * - h/w queue depth exceeds low water mark
1185 if (!list_empty(&tid->buf_q) || tid->paused ||
1186 !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
1187 txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
1189 * Add this frame to software queue for scheduling later
1192 list_splice_tail_init(bf_head, &tid->buf_q);
1193 ath_tx_queue_tid(txctl->txq, tid);
1197 /* Add sub-frame to BAW */
1198 ath_tx_addto_baw(sc, tid, bf);
1200 /* Queue to h/w without aggregation */
1202 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
1203 ath_buf_set_rate(sc, bf);
1204 ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
1211 * returns aggr limit based on lowest of the rates
1214 static u32 ath_lookup_rate(struct ath_softc *sc,
1216 struct ath_atx_tid *tid)
1218 struct ath_rate_table *rate_table = sc->hw_rate_table[sc->sc_curmode];
1219 struct sk_buff *skb;
1220 struct ieee80211_tx_info *tx_info;
1221 struct ieee80211_tx_rate *rates;
1222 struct ath_tx_info_priv *tx_info_priv;
1223 u32 max_4ms_framelen, frame_length;
1224 u16 aggr_limit, legacy = 0, maxampdu;
1227 skb = (struct sk_buff *)bf->bf_mpdu;
1228 tx_info = IEEE80211_SKB_CB(skb);
1229 rates = tx_info->control.rates;
1231 (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
1234 * Find the lowest frame length among the rate series that will have a
1235 * 4ms transmit duration.
1236 * TODO - TXOP limit needs to be considered.
1238 max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
1240 for (i = 0; i < 4; i++) {
1241 if (rates[i].count) {
1242 if (!WLAN_RC_PHY_HT(rate_table->info[rates[i].idx].phy)) {
1248 rate_table->info[rates[i].idx].max_4ms_framelen;
1249 max_4ms_framelen = min(max_4ms_framelen, frame_length);
1254 * limit aggregate size by the minimum rate if rate selected is
1255 * not a probe rate, if rate selected is a probe rate then
1256 * avoid aggregation of this packet.
1258 if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
1261 aggr_limit = min(max_4ms_framelen,
1262 (u32)ATH_AMPDU_LIMIT_DEFAULT);
1265 * h/w can accept aggregates upto 16 bit lengths (65535).
1266 * The IE, however can hold upto 65536, which shows up here
1267 * as zero. Ignore 65536 since we are constrained by hw.
1269 maxampdu = tid->an->maxampdu;
1271 aggr_limit = min(aggr_limit, maxampdu);
1277 * returns the number of delimiters to be added to
1278 * meet the minimum required mpdudensity.
1279 * caller should make sure that the rate is HT rate .
1282 static int ath_compute_num_delims(struct ath_softc *sc,
1283 struct ath_atx_tid *tid,
1287 struct ath_rate_table *rt = sc->hw_rate_table[sc->sc_curmode];
1288 struct sk_buff *skb = bf->bf_mpdu;
1289 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1290 u32 nsymbits, nsymbols, mpdudensity;
1293 int width, half_gi, ndelim, mindelim;
1295 /* Select standard number of delimiters based on frame length alone */
1296 ndelim = ATH_AGGR_GET_NDELIM(frmlen);
1299 * If encryption enabled, hardware requires some more padding between
1301 * TODO - this could be improved to be dependent on the rate.
1302 * The hardware can keep up at lower rates, but not higher rates
1304 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
1305 ndelim += ATH_AGGR_ENCRYPTDELIM;
1308 * Convert desired mpdu density from microeconds to bytes based
1309 * on highest rate in rate series (i.e. first rate) to determine
1310 * required minimum length for subframe. Take into account
1311 * whether high rate is 20 or 40Mhz and half or full GI.
1313 mpdudensity = tid->an->mpdudensity;
1316 * If there is no mpdu density restriction, no further calculation
1319 if (mpdudensity == 0)
1322 rix = tx_info->control.rates[0].idx;
1323 flags = tx_info->control.rates[0].flags;
1324 rc = rt->info[rix].ratecode;
1325 width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
1326 half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
1329 nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
1331 nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
1336 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
1337 minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
1339 /* Is frame shorter than required minimum length? */
1340 if (frmlen < minlen) {
1341 /* Get the minimum number of delimiters required. */
1342 mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
1343 ndelim = max(mindelim, ndelim);
1350 * For aggregation from software buffer queue.
1351 * NB: must be called with txq lock held
1354 static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
1355 struct ath_atx_tid *tid,
1356 struct list_head *bf_q,
1357 struct ath_buf **bf_last,
1358 struct aggr_rifs_param *param,
1361 #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
1362 struct ath_buf *bf, *tbf, *bf_first, *bf_prev = NULL;
1363 struct list_head bf_head;
1364 int rl = 0, nframes = 0, ndelim;
1365 u16 aggr_limit = 0, al = 0, bpad = 0,
1366 al_delta, h_baw = tid->baw_size / 2;
1367 enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
1369 INIT_LIST_HEAD(&bf_head);
1371 BUG_ON(list_empty(&tid->buf_q));
1373 bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
1376 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1379 * do not step over block-ack window
1381 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
1382 status = ATH_AGGR_BAW_CLOSED;
1387 aggr_limit = ath_lookup_rate(sc, bf, tid);
1392 * do not exceed aggregation limit
1394 al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
1396 if (nframes && (aggr_limit <
1397 (al + bpad + al_delta + prev_al))) {
1398 status = ATH_AGGR_LIMITED;
1403 * do not exceed subframe limit
1405 if ((nframes + *prev_frames) >=
1406 min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
1407 status = ATH_AGGR_LIMITED;
1412 * add padding for previous frame to aggregation length
1414 al += bpad + al_delta;
1417 * Get the delimiters needed to meet the MPDU
1418 * density for this node.
1420 ndelim = ath_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
1422 bpad = PADBYTES(al_delta) + (ndelim << 2);
1425 bf->bf_lastfrm->bf_desc->ds_link = 0;
1428 * this packet is part of an aggregate
1429 * - remove all descriptors belonging to this frame from
1431 * - add it to block ack window
1432 * - set up descriptors for aggregation
1434 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1435 ath_tx_addto_baw(sc, tid, bf);
1437 list_for_each_entry(tbf, &bf_head, list) {
1438 ath9k_hw_set11n_aggr_middle(sc->sc_ah,
1439 tbf->bf_desc, ndelim);
1443 * link buffers of this frame to the aggregate
1445 list_splice_tail_init(&bf_head, bf_q);
1449 bf_prev->bf_next = bf;
1450 bf_prev->bf_lastfrm->bf_desc->ds_link = bf->bf_daddr;
1456 * terminate aggregation on a small packet boundary
1458 if (bf->bf_frmlen < ATH_AGGR_MINPLEN) {
1459 status = ATH_AGGR_SHORTPKT;
1463 } while (!list_empty(&tid->buf_q));
1465 bf_first->bf_al = al;
1466 bf_first->bf_nframes = nframes;
1473 * process pending frames possibly doing a-mpdu aggregation
1474 * NB: must be called with txq lock held
1477 static void ath_tx_sched_aggr(struct ath_softc *sc,
1478 struct ath_txq *txq, struct ath_atx_tid *tid)
1480 struct ath_buf *bf, *tbf, *bf_last, *bf_lastaggr = NULL;
1481 enum ATH_AGGR_STATUS status;
1482 struct list_head bf_q;
1483 struct aggr_rifs_param param = {0, 0, 0, 0, NULL};
1484 int prev_frames = 0;
1487 if (list_empty(&tid->buf_q))
1490 INIT_LIST_HEAD(&bf_q);
1492 status = ath_tx_form_aggr(sc, tid, &bf_q, &bf_lastaggr, ¶m,
1496 * no frames picked up to be aggregated; block-ack
1497 * window is not open
1499 if (list_empty(&bf_q))
1502 bf = list_first_entry(&bf_q, struct ath_buf, list);
1503 bf_last = list_entry(bf_q.prev, struct ath_buf, list);
1504 bf->bf_lastbf = bf_last;
1507 * if only one frame, send as non-aggregate
1509 if (bf->bf_nframes == 1) {
1510 ASSERT(bf->bf_lastfrm == bf_last);
1512 bf->bf_state.bf_type &= ~BUF_AGGR;
1514 * clear aggr bits for every descriptor
1515 * XXX TODO: is there a way to optimize it?
1517 list_for_each_entry(tbf, &bf_q, list) {
1518 ath9k_hw_clr11n_aggr(sc->sc_ah, tbf->bf_desc);
1521 ath_buf_set_rate(sc, bf);
1522 ath_tx_txqaddbuf(sc, txq, &bf_q);
1527 * setup first desc with rate and aggr info
1529 bf->bf_state.bf_type |= BUF_AGGR;
1530 ath_buf_set_rate(sc, bf);
1531 ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
1534 * anchor last frame of aggregate correctly
1536 ASSERT(bf_lastaggr);
1537 ASSERT(bf_lastaggr->bf_lastfrm == bf_last);
1539 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1541 /* XXX: We don't enter into this loop, consider removing this */
1542 while (!list_empty(&bf_q) && !list_is_last(&tbf->list, &bf_q)) {
1543 tbf = list_entry(tbf->list.next, struct ath_buf, list);
1544 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1547 txq->axq_aggr_depth++;
1550 * Normal aggregate, queue to hardware
1552 ath_tx_txqaddbuf(sc, txq, &bf_q);
1554 } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
1555 status != ATH_AGGR_BAW_CLOSED);
1558 /* Called with txq lock held */
1560 static void ath_tid_drain(struct ath_softc *sc,
1561 struct ath_txq *txq,
1562 struct ath_atx_tid *tid)
1566 struct list_head bf_head;
1567 INIT_LIST_HEAD(&bf_head);
1570 if (list_empty(&tid->buf_q))
1572 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1574 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1576 /* update baw for software retried frame */
1577 if (bf_isretried(bf))
1578 ath_tx_update_baw(sc, tid, bf->bf_seqno);
1581 * do not indicate packets while holding txq spinlock.
1582 * unlock is intentional here
1584 spin_unlock(&txq->axq_lock);
1586 /* complete this sub-frame */
1587 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
1589 spin_lock(&txq->axq_lock);
1593 * TODO: For frame(s) that are in the retry state, we will reuse the
1594 * sequence number(s) without setting the retry bit. The
1595 * alternative is to give up on these and BAR the receiver's window
1598 tid->seq_next = tid->seq_start;
1599 tid->baw_tail = tid->baw_head;
1603 * Drain all pending buffers
1604 * NB: must be called with txq lock held
1607 static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
1608 struct ath_txq *txq)
1610 struct ath_atx_ac *ac, *ac_tmp;
1611 struct ath_atx_tid *tid, *tid_tmp;
1613 list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
1614 list_del(&ac->list);
1616 list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
1617 list_del(&tid->list);
1619 ath_tid_drain(sc, txq, tid);
1624 static void ath_tx_setup_buffer(struct ath_softc *sc, struct ath_buf *bf,
1625 struct sk_buff *skb, struct scatterlist *sg,
1626 struct ath_tx_control *txctl)
1628 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1629 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1630 struct ath_tx_info_priv *tx_info_priv;
1634 tx_info_priv = kzalloc(sizeof(*tx_info_priv), GFP_KERNEL);
1635 tx_info->rate_driver_data[0] = tx_info_priv;
1636 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1637 fc = hdr->frame_control;
1639 ATH_TXBUF_RESET(bf);
1643 bf->bf_frmlen = skb->len + FCS_LEN - (hdrlen & 3);
1645 ieee80211_is_data(fc) ?
1646 (bf->bf_state.bf_type |= BUF_DATA) :
1647 (bf->bf_state.bf_type &= ~BUF_DATA);
1648 ieee80211_is_back_req(fc) ?
1649 (bf->bf_state.bf_type |= BUF_BAR) :
1650 (bf->bf_state.bf_type &= ~BUF_BAR);
1651 ieee80211_is_pspoll(fc) ?
1652 (bf->bf_state.bf_type |= BUF_PSPOLL) :
1653 (bf->bf_state.bf_type &= ~BUF_PSPOLL);
1654 (sc->sc_flags & SC_OP_PREAMBLE_SHORT) ?
1655 (bf->bf_state.bf_type |= BUF_SHORT_PREAMBLE) :
1656 (bf->bf_state.bf_type &= ~BUF_SHORT_PREAMBLE);
1657 (sc->hw->conf.ht.enabled && !is_pae(skb) &&
1658 (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) ?
1659 (bf->bf_state.bf_type |= BUF_HT) :
1660 (bf->bf_state.bf_type &= ~BUF_HT);
1662 bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
1666 bf->bf_keytype = get_hw_crypto_keytype(skb);
1668 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR) {
1669 bf->bf_frmlen += tx_info->control.hw_key->icv_len;
1670 bf->bf_keyix = tx_info->control.hw_key->hw_key_idx;
1672 bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
1675 /* Assign seqno, tidno */
1677 if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR))
1678 assign_aggr_tid_seqno(skb, bf);
1683 bf->bf_dmacontext = pci_map_single(sc->pdev, skb->data,
1684 skb->len, PCI_DMA_TODEVICE);
1685 bf->bf_buf_addr = bf->bf_dmacontext;
1688 /* FIXME: tx power */
1689 static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
1690 struct scatterlist *sg, u32 n_sg,
1691 struct ath_tx_control *txctl)
1693 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
1694 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1695 struct ath_node *an = NULL;
1696 struct list_head bf_head;
1697 struct ath_desc *ds;
1698 struct ath_atx_tid *tid;
1699 struct ath_hal *ah = sc->sc_ah;
1702 frm_type = get_hw_packet_type(skb);
1704 INIT_LIST_HEAD(&bf_head);
1705 list_add_tail(&bf->list, &bf_head);
1707 /* setup descriptor */
1711 ds->ds_data = bf->bf_buf_addr;
1713 /* Formulate first tx descriptor with tx controls */
1715 ath9k_hw_set11n_txdesc(ah, ds, bf->bf_frmlen, frm_type, MAX_RATE_POWER,
1716 bf->bf_keyix, bf->bf_keytype, bf->bf_flags);
1718 ath9k_hw_filltxdesc(ah, ds,
1719 sg_dma_len(sg), /* segment length */
1720 true, /* first segment */
1721 (n_sg == 1) ? true : false, /* last segment */
1722 ds); /* first descriptor */
1724 bf->bf_lastfrm = bf;
1726 spin_lock_bh(&txctl->txq->axq_lock);
1728 if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR) &&
1729 tx_info->control.sta) {
1730 an = (struct ath_node *)tx_info->control.sta->drv_priv;
1731 tid = ATH_AN_2_TID(an, bf->bf_tidno);
1733 if (ath_aggr_query(sc, an, bf->bf_tidno)) {
1735 * Try aggregation if it's a unicast data frame
1736 * and the destination is HT capable.
1738 ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
1741 * Send this frame as regular when ADDBA
1742 * exchange is neither complete nor pending.
1744 ath_tx_send_normal(sc, txctl->txq,
1751 ath_buf_set_rate(sc, bf);
1752 ath_tx_txqaddbuf(sc, txctl->txq, &bf_head);
1755 spin_unlock_bh(&txctl->txq->axq_lock);
1758 int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb,
1759 struct ath_tx_control *txctl)
1762 struct scatterlist sg;
1764 /* Check if a tx buffer is available */
1766 bf = ath_tx_get_buffer(sc);
1768 DPRINTF(sc, ATH_DBG_XMIT, "%s: TX buffers are full\n",
1773 ath_tx_setup_buffer(sc, bf, skb, &sg, txctl);
1777 memset(&sg, 0, sizeof(struct scatterlist));
1778 sg_dma_address(&sg) = bf->bf_dmacontext;
1779 sg_dma_len(&sg) = skb->len;
1781 ath_tx_start_dma(sc, bf, &sg, 1, txctl);
1786 /* Initialize TX queue and h/w */
1788 int ath_tx_init(struct ath_softc *sc, int nbufs)
1793 spin_lock_init(&sc->sc_txbuflock);
1795 /* Setup tx descriptors */
1796 error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
1799 DPRINTF(sc, ATH_DBG_FATAL,
1800 "%s: failed to allocate tx descriptors: %d\n",
1805 /* XXX allocate beacon state together with vap */
1806 error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
1807 "beacon", ATH_BCBUF, 1);
1809 DPRINTF(sc, ATH_DBG_FATAL,
1810 "%s: failed to allocate "
1811 "beacon descripotrs: %d\n",
1824 /* Reclaim all tx queue resources */
1826 int ath_tx_cleanup(struct ath_softc *sc)
1828 /* cleanup beacon descriptors */
1829 if (sc->sc_bdma.dd_desc_len != 0)
1830 ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
1832 /* cleanup tx descriptors */
1833 if (sc->sc_txdma.dd_desc_len != 0)
1834 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
1839 /* Setup a h/w transmit queue */
1841 struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
1843 struct ath_hal *ah = sc->sc_ah;
1844 struct ath9k_tx_queue_info qi;
1847 memset(&qi, 0, sizeof(qi));
1848 qi.tqi_subtype = subtype;
1849 qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
1850 qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
1851 qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
1852 qi.tqi_physCompBuf = 0;
1855 * Enable interrupts only for EOL and DESC conditions.
1856 * We mark tx descriptors to receive a DESC interrupt
1857 * when a tx queue gets deep; otherwise waiting for the
1858 * EOL to reap descriptors. Note that this is done to
1859 * reduce interrupt load and this only defers reaping
1860 * descriptors, never transmitting frames. Aside from
1861 * reducing interrupts this also permits more concurrency.
1862 * The only potential downside is if the tx queue backs
1863 * up in which case the top half of the kernel may backup
1864 * due to a lack of tx descriptors.
1866 * The UAPSD queue is an exception, since we take a desc-
1867 * based intr on the EOSP frames.
1869 if (qtype == ATH9K_TX_QUEUE_UAPSD)
1870 qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1872 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
1873 TXQ_FLAG_TXDESCINT_ENABLE;
1874 qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
1877 * NB: don't print a message, this happens
1878 * normally on parts with too few tx queues
1882 if (qnum >= ARRAY_SIZE(sc->sc_txq)) {
1883 DPRINTF(sc, ATH_DBG_FATAL,
1884 "%s: hal qnum %u out of range, max %u!\n",
1885 __func__, qnum, (unsigned int)ARRAY_SIZE(sc->sc_txq));
1886 ath9k_hw_releasetxqueue(ah, qnum);
1889 if (!ATH_TXQ_SETUP(sc, qnum)) {
1890 struct ath_txq *txq = &sc->sc_txq[qnum];
1892 txq->axq_qnum = qnum;
1893 txq->axq_link = NULL;
1894 INIT_LIST_HEAD(&txq->axq_q);
1895 INIT_LIST_HEAD(&txq->axq_acq);
1896 spin_lock_init(&txq->axq_lock);
1898 txq->axq_aggr_depth = 0;
1899 txq->axq_totalqueued = 0;
1900 txq->axq_linkbuf = NULL;
1901 sc->sc_txqsetup |= 1<<qnum;
1903 return &sc->sc_txq[qnum];
1906 /* Reclaim resources for a setup queue */
1908 void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1910 ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
1911 sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
1915 * Setup a hardware data transmit queue for the specified
1916 * access control. The hal may not support all requested
1917 * queues in which case it will return a reference to a
1918 * previously setup queue. We record the mapping from ac's
1919 * to h/w queues for use by ath_tx_start and also track
1920 * the set of h/w queues being used to optimize work in the
1921 * transmit interrupt handler and related routines.
1924 int ath_tx_setup(struct ath_softc *sc, int haltype)
1926 struct ath_txq *txq;
1928 if (haltype >= ARRAY_SIZE(sc->sc_haltype2q)) {
1929 DPRINTF(sc, ATH_DBG_FATAL,
1930 "%s: HAL AC %u out of range, max %zu!\n",
1931 __func__, haltype, ARRAY_SIZE(sc->sc_haltype2q));
1934 txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
1936 sc->sc_haltype2q[haltype] = txq->axq_qnum;
1942 int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
1947 case ATH9K_TX_QUEUE_DATA:
1948 if (haltype >= ARRAY_SIZE(sc->sc_haltype2q)) {
1949 DPRINTF(sc, ATH_DBG_FATAL,
1950 "%s: HAL AC %u out of range, max %zu!\n",
1952 haltype, ARRAY_SIZE(sc->sc_haltype2q));
1955 qnum = sc->sc_haltype2q[haltype];
1957 case ATH9K_TX_QUEUE_BEACON:
1958 qnum = sc->sc_bhalq;
1960 case ATH9K_TX_QUEUE_CAB:
1961 qnum = sc->sc_cabq->axq_qnum;
1969 /* Get a transmit queue, if available */
1971 struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
1973 struct ath_txq *txq = NULL;
1976 qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
1977 txq = &sc->sc_txq[qnum];
1979 spin_lock_bh(&txq->axq_lock);
1981 /* Try to avoid running out of descriptors */
1982 if (txq->axq_depth >= (ATH_TXBUF - 20)) {
1983 DPRINTF(sc, ATH_DBG_FATAL,
1984 "%s: TX queue: %d is full, depth: %d\n",
1985 __func__, qnum, txq->axq_depth);
1986 ieee80211_stop_queue(sc->hw, skb_get_queue_mapping(skb));
1988 spin_unlock_bh(&txq->axq_lock);
1992 spin_unlock_bh(&txq->axq_lock);
1997 /* Update parameters for a transmit queue */
1999 int ath_txq_update(struct ath_softc *sc, int qnum,
2000 struct ath9k_tx_queue_info *qinfo)
2002 struct ath_hal *ah = sc->sc_ah;
2004 struct ath9k_tx_queue_info qi;
2006 if (qnum == sc->sc_bhalq) {
2008 * XXX: for beacon queue, we just save the parameter.
2009 * It will be picked up by ath_beaconq_config when
2012 sc->sc_beacon_qi = *qinfo;
2016 ASSERT(sc->sc_txq[qnum].axq_qnum == qnum);
2018 ath9k_hw_get_txq_props(ah, qnum, &qi);
2019 qi.tqi_aifs = qinfo->tqi_aifs;
2020 qi.tqi_cwmin = qinfo->tqi_cwmin;
2021 qi.tqi_cwmax = qinfo->tqi_cwmax;
2022 qi.tqi_burstTime = qinfo->tqi_burstTime;
2023 qi.tqi_readyTime = qinfo->tqi_readyTime;
2025 if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
2026 DPRINTF(sc, ATH_DBG_FATAL,
2027 "%s: unable to update hardware queue %u!\n",
2031 ath9k_hw_resettxqueue(ah, qnum); /* push to h/w */
2037 int ath_cabq_update(struct ath_softc *sc)
2039 struct ath9k_tx_queue_info qi;
2040 int qnum = sc->sc_cabq->axq_qnum;
2041 struct ath_beacon_config conf;
2043 ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
2045 * Ensure the readytime % is within the bounds.
2047 if (sc->sc_config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
2048 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
2049 else if (sc->sc_config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
2050 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
2052 ath_get_beaconconfig(sc, ATH_IF_ID_ANY, &conf);
2054 (conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
2055 ath_txq_update(sc, qnum, &qi);
2060 /* Deferred processing of transmit interrupt */
2062 void ath_tx_tasklet(struct ath_softc *sc)
2065 u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
2067 ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
2070 * Process each active queue.
2072 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2073 if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
2074 ath_tx_processq(sc, &sc->sc_txq[i]);
2078 void ath_tx_draintxq(struct ath_softc *sc,
2079 struct ath_txq *txq, bool retry_tx)
2081 struct ath_buf *bf, *lastbf;
2082 struct list_head bf_head;
2084 INIT_LIST_HEAD(&bf_head);
2087 * NB: this assumes output has been stopped and
2088 * we do not need to block ath_tx_tasklet
2091 spin_lock_bh(&txq->axq_lock);
2093 if (list_empty(&txq->axq_q)) {
2094 txq->axq_link = NULL;
2095 txq->axq_linkbuf = NULL;
2096 spin_unlock_bh(&txq->axq_lock);
2100 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2102 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
2103 list_del(&bf->list);
2104 spin_unlock_bh(&txq->axq_lock);
2106 spin_lock_bh(&sc->sc_txbuflock);
2107 list_add_tail(&bf->list, &sc->sc_txbuf);
2108 spin_unlock_bh(&sc->sc_txbuflock);
2112 lastbf = bf->bf_lastbf;
2114 lastbf->bf_desc->ds_txstat.ts_flags =
2115 ATH9K_TX_SW_ABORTED;
2117 /* remove ath_buf's of the same mpdu from txq */
2118 list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
2121 spin_unlock_bh(&txq->axq_lock);
2124 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, 0);
2126 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2129 /* flush any pending frames if aggregation is enabled */
2130 if (sc->sc_flags & SC_OP_TXAGGR) {
2132 spin_lock_bh(&txq->axq_lock);
2133 ath_txq_drain_pending_buffers(sc, txq);
2134 spin_unlock_bh(&txq->axq_lock);
2139 /* Drain the transmit queues and reclaim resources */
2141 void ath_draintxq(struct ath_softc *sc, bool retry_tx)
2143 /* stop beacon queue. The beacon will be freed when
2144 * we go to INIT state */
2145 if (!(sc->sc_flags & SC_OP_INVALID)) {
2146 (void) ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
2147 DPRINTF(sc, ATH_DBG_XMIT, "%s: beacon queue %x\n", __func__,
2148 ath9k_hw_gettxbuf(sc->sc_ah, sc->sc_bhalq));
2151 ath_drain_txdataq(sc, retry_tx);
2154 u32 ath_txq_depth(struct ath_softc *sc, int qnum)
2156 return sc->sc_txq[qnum].axq_depth;
2159 u32 ath_txq_aggr_depth(struct ath_softc *sc, int qnum)
2161 return sc->sc_txq[qnum].axq_aggr_depth;
2164 bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an, u8 tidno)
2166 struct ath_atx_tid *txtid;
2168 if (!(sc->sc_flags & SC_OP_TXAGGR))
2171 txtid = ATH_AN_2_TID(an, tidno);
2173 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
2174 if (!(txtid->state & AGGR_ADDBA_PROGRESS) &&
2175 (txtid->addba_exchangeattempts < ADDBA_EXCHANGE_ATTEMPTS)) {
2176 txtid->addba_exchangeattempts++;
2184 /* Start TX aggregation */
2186 int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
2189 struct ath_atx_tid *txtid;
2190 struct ath_node *an;
2192 an = (struct ath_node *)sta->drv_priv;
2194 if (sc->sc_flags & SC_OP_TXAGGR) {
2195 txtid = ATH_AN_2_TID(an, tid);
2196 txtid->state |= AGGR_ADDBA_PROGRESS;
2197 ath_tx_pause_tid(sc, txtid);
2203 /* Stop tx aggregation */
2205 int ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
2207 struct ath_node *an = (struct ath_node *)sta->drv_priv;
2209 ath_tx_aggr_teardown(sc, an, tid);
2213 /* Resume tx aggregation */
2215 void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
2217 struct ath_atx_tid *txtid;
2218 struct ath_node *an;
2220 an = (struct ath_node *)sta->drv_priv;
2222 if (sc->sc_flags & SC_OP_TXAGGR) {
2223 txtid = ATH_AN_2_TID(an, tid);
2225 IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
2226 txtid->state |= AGGR_ADDBA_COMPLETE;
2227 txtid->state &= ~AGGR_ADDBA_PROGRESS;
2228 ath_tx_resume_tid(sc, txtid);
2233 * Performs transmit side cleanup when TID changes from aggregated to
2235 * - Pause the TID and mark cleanup in progress
2236 * - Discard all retry frames from the s/w queue.
2239 void ath_tx_aggr_teardown(struct ath_softc *sc, struct ath_node *an, u8 tid)
2241 struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
2242 struct ath_txq *txq = &sc->sc_txq[txtid->ac->qnum];
2244 struct list_head bf_head;
2245 INIT_LIST_HEAD(&bf_head);
2247 DPRINTF(sc, ATH_DBG_AGGR, "%s: teardown TX aggregation\n", __func__);
2249 if (txtid->state & AGGR_CLEANUP) /* cleanup is in progress */
2252 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
2253 txtid->addba_exchangeattempts = 0;
2257 /* TID must be paused first */
2258 ath_tx_pause_tid(sc, txtid);
2260 /* drop all software retried frames and mark this TID */
2261 spin_lock_bh(&txq->axq_lock);
2262 while (!list_empty(&txtid->buf_q)) {
2263 bf = list_first_entry(&txtid->buf_q, struct ath_buf, list);
2264 if (!bf_isretried(bf)) {
2266 * NB: it's based on the assumption that
2267 * software retried frame will always stay
2268 * at the head of software queue.
2272 list_cut_position(&bf_head,
2273 &txtid->buf_q, &bf->bf_lastfrm->list);
2274 ath_tx_update_baw(sc, txtid, bf->bf_seqno);
2276 /* complete this sub-frame */
2277 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2280 if (txtid->baw_head != txtid->baw_tail) {
2281 spin_unlock_bh(&txq->axq_lock);
2282 txtid->state |= AGGR_CLEANUP;
2284 txtid->state &= ~AGGR_ADDBA_COMPLETE;
2285 txtid->addba_exchangeattempts = 0;
2286 spin_unlock_bh(&txq->axq_lock);
2287 ath_tx_flush_tid(sc, txtid);
2292 * Tx scheduling logic
2293 * NB: must be called with txq lock held
2296 void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
2298 struct ath_atx_ac *ac;
2299 struct ath_atx_tid *tid;
2301 /* nothing to schedule */
2302 if (list_empty(&txq->axq_acq))
2305 * get the first node/ac pair on the queue
2307 ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
2308 list_del(&ac->list);
2312 * process a single tid per destination
2315 /* nothing to schedule */
2316 if (list_empty(&ac->tid_q))
2319 tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
2320 list_del(&tid->list);
2323 if (tid->paused) /* check next tid to keep h/w busy */
2326 if ((txq->axq_depth % 2) == 0)
2327 ath_tx_sched_aggr(sc, txq, tid);
2330 * add tid to round-robin queue if more frames
2331 * are pending for the tid
2333 if (!list_empty(&tid->buf_q))
2334 ath_tx_queue_tid(txq, tid);
2336 /* only schedule one TID at a time */
2338 } while (!list_empty(&ac->tid_q));
2341 * schedule AC if more TIDs need processing
2343 if (!list_empty(&ac->tid_q)) {
2345 * add dest ac to txq if not already added
2349 list_add_tail(&ac->list, &txq->axq_acq);
2354 /* Initialize per-node transmit state */
2356 void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2358 struct ath_atx_tid *tid;
2359 struct ath_atx_ac *ac;
2363 * Init per tid tx state
2365 for (tidno = 0, tid = &an->an_aggr.tx.tid[tidno];
2366 tidno < WME_NUM_TID;
2370 tid->seq_start = tid->seq_next = 0;
2371 tid->baw_size = WME_MAX_BA;
2372 tid->baw_head = tid->baw_tail = 0;
2374 tid->paused = false;
2375 tid->state &= ~AGGR_CLEANUP;
2376 INIT_LIST_HEAD(&tid->buf_q);
2378 acno = TID_TO_WME_AC(tidno);
2379 tid->ac = &an->an_aggr.tx.ac[acno];
2382 tid->state &= ~AGGR_ADDBA_COMPLETE;
2383 tid->state &= ~AGGR_ADDBA_PROGRESS;
2384 tid->addba_exchangeattempts = 0;
2388 * Init per ac tx state
2390 for (acno = 0, ac = &an->an_aggr.tx.ac[acno];
2391 acno < WME_NUM_AC; acno++, ac++) {
2393 INIT_LIST_HEAD(&ac->tid_q);
2397 ac->qnum = ath_tx_get_qnum(sc,
2398 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
2401 ac->qnum = ath_tx_get_qnum(sc,
2402 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
2405 ac->qnum = ath_tx_get_qnum(sc,
2406 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
2409 ac->qnum = ath_tx_get_qnum(sc,
2410 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
2416 /* Cleanupthe pending buffers for the node. */
2418 void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
2421 struct ath_atx_ac *ac, *ac_tmp;
2422 struct ath_atx_tid *tid, *tid_tmp;
2423 struct ath_txq *txq;
2424 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2425 if (ATH_TXQ_SETUP(sc, i)) {
2426 txq = &sc->sc_txq[i];
2428 spin_lock(&txq->axq_lock);
2430 list_for_each_entry_safe(ac,
2431 ac_tmp, &txq->axq_acq, list) {
2432 tid = list_first_entry(&ac->tid_q,
2433 struct ath_atx_tid, list);
2434 if (tid && tid->an != an)
2436 list_del(&ac->list);
2439 list_for_each_entry_safe(tid,
2440 tid_tmp, &ac->tid_q, list) {
2441 list_del(&tid->list);
2443 ath_tid_drain(sc, txq, tid);
2444 tid->state &= ~AGGR_ADDBA_COMPLETE;
2445 tid->addba_exchangeattempts = 0;
2446 tid->state &= ~AGGR_CLEANUP;
2450 spin_unlock(&txq->axq_lock);
2455 void ath_tx_cabq(struct ath_softc *sc, struct sk_buff *skb)
2457 int hdrlen, padsize;
2458 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2459 struct ath_tx_control txctl;
2461 memset(&txctl, 0, sizeof(struct ath_tx_control));
2464 * As a temporary workaround, assign seq# here; this will likely need
2465 * to be cleaned up to work better with Beacon transmission and virtual
2468 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
2469 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2470 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
2472 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
2473 hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
2476 /* Add the padding after the header if this is not already done */
2477 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
2479 padsize = hdrlen % 4;
2480 if (skb_headroom(skb) < padsize) {
2481 DPRINTF(sc, ATH_DBG_XMIT, "%s: TX CABQ padding "
2482 "failed\n", __func__);
2483 dev_kfree_skb_any(skb);
2486 skb_push(skb, padsize);
2487 memmove(skb->data, skb->data + padsize, hdrlen);
2490 txctl.txq = sc->sc_cabq;
2492 DPRINTF(sc, ATH_DBG_XMIT, "%s: transmitting CABQ packet, skb: %p\n",
2496 if (ath_tx_start(sc, skb, &txctl) != 0) {
2497 DPRINTF(sc, ATH_DBG_XMIT, "%s: TX failed\n", __func__);
2503 dev_kfree_skb_any(skb);