blob: f9c309ed3a2d22e170738d0e2156598fdaf5eee2 [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
2 * Copyright (c) 2008 Atheros Communications Inc.
3 *
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.
7 *
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.
15 */
16
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070017#include "core.h"
18
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)
23#define L_STF 8
24#define L_LTF 8
25#define L_SIG 4
26#define HT_SIG 8
27#define HT_STF 4
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)
33
34#define OFDM_SIFS_TIME 16
35
36static u32 bits_per_symbol[][2] = {
37 /* 20MHz 40MHz */
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 */
54};
55
56#define IS_HT_RATE(_rate) ((_rate) & 0x80)
57
58/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070059 * 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
62 */
63
Sujith102e0572008-10-29 10:15:16 +053064static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
65 struct list_head *head)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070066{
67 struct ath_hal *ah = sc->sc_ah;
68 struct ath_buf *bf;
Sujith102e0572008-10-29 10:15:16 +053069
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070070 /*
71 * Insert the frame on the outbound list and
72 * pass it on to the hardware.
73 */
74
75 if (list_empty(head))
76 return;
77
78 bf = list_first_entry(head, struct ath_buf, list);
79
80 list_splice_tail_init(head, &txq->axq_q);
81 txq->axq_depth++;
82 txq->axq_totalqueued++;
83 txq->axq_linkbuf = list_entry(txq->axq_q.prev, struct ath_buf, list);
84
85 DPRINTF(sc, ATH_DBG_QUEUE,
Sujith04bd46382008-11-28 22:18:05 +053086 "qnum: %d, txq depth: %d\n", txq->axq_qnum, txq->axq_depth);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070087
88 if (txq->axq_link == NULL) {
89 ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
90 DPRINTF(sc, ATH_DBG_XMIT,
Sujith04bd46382008-11-28 22:18:05 +053091 "TXDP[%u] = %llx (%p)\n",
92 txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070093 } else {
94 *txq->axq_link = bf->bf_daddr;
Sujith04bd46382008-11-28 22:18:05 +053095 DPRINTF(sc, ATH_DBG_XMIT, "link[%u] (%p)=%llx (%p)\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070096 txq->axq_qnum, txq->axq_link,
97 ito64(bf->bf_daddr), bf->bf_desc);
98 }
99 txq->axq_link = &(bf->bf_lastbf->bf_desc->ds_link);
100 ath9k_hw_txstart(ah, txq->axq_qnum);
101}
102
Sujithc4288392008-11-18 09:09:30 +0530103static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
104 struct ath_xmit_status *tx_status)
105{
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);
109
Sujith04bd46382008-11-28 22:18:05 +0530110 DPRINTF(sc, ATH_DBG_XMIT, "TX complete: skb: %p\n", skb);
Sujithc4288392008-11-18 09:09:30 +0530111
112 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
113 tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
114 kfree(tx_info_priv);
115 tx_info->rate_driver_data[0] = NULL;
116 }
117
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;
121 }
122
123 if (!(tx_status->flags & (ATH_TX_ERROR | ATH_TX_XRETRY))) {
124 /* Frame was ACKed */
125 tx_info->flags |= IEEE80211_TX_STAT_ACK;
126 }
127
128 tx_info->status.rates[0].count = tx_status->retries + 1;
129
130 ieee80211_tx_status(hw, skb);
131}
132
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700133/* Check if it's okay to send out aggregates */
134
Sujitha37c2c72008-10-29 10:15:40 +0530135static int ath_aggr_query(struct ath_softc *sc, struct ath_node *an, u8 tidno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700136{
137 struct ath_atx_tid *tid;
138 tid = ATH_AN_2_TID(an, tidno);
139
Sujitha37c2c72008-10-29 10:15:40 +0530140 if (tid->state & AGGR_ADDBA_COMPLETE ||
141 tid->state & AGGR_ADDBA_PROGRESS)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700142 return 1;
143 else
144 return 0;
145}
146
Sujithff37e332008-11-24 12:07:55 +0530147static void ath_get_beaconconfig(struct ath_softc *sc, int if_id,
148 struct ath_beacon_config *conf)
149{
150 struct ieee80211_hw *hw = sc->hw;
151
152 /* fill in beacon config data */
153
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;
158}
159
Sujith528f0c62008-10-29 10:14:26 +0530160/* Calculate Atheros packet type from IEEE80211 packet header */
161
162static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700163{
Sujith528f0c62008-10-29 10:14:26 +0530164 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700165 enum ath9k_pkt_type htype;
166 __le16 fc;
167
Sujith528f0c62008-10-29 10:14:26 +0530168 hdr = (struct ieee80211_hdr *)skb->data;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700169 fc = hdr->frame_control;
170
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700171 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;
179 else
180 htype = ATH9K_PKT_TYPE_NORMAL;
181
182 return htype;
183}
184
Sujitha8efee42008-11-18 09:07:30 +0530185static bool is_pae(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700186{
187 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700188 __le16 fc;
189
190 hdr = (struct ieee80211_hdr *)skb->data;
191 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +0200192
Sujitha8efee42008-11-18 09:07:30 +0530193 if (ieee80211_is_data(fc)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700194 if (ieee80211_is_nullfunc(fc) ||
Sujith528f0c62008-10-29 10:14:26 +0530195 /* Port Access Entity (IEEE 802.1X) */
196 (skb->protocol == cpu_to_be16(ETH_P_PAE))) {
Sujitha8efee42008-11-18 09:07:30 +0530197 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700198 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700199 }
200
Sujitha8efee42008-11-18 09:07:30 +0530201 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700202}
203
Sujith528f0c62008-10-29 10:14:26 +0530204static int get_hw_crypto_keytype(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700205{
Sujith528f0c62008-10-29 10:14:26 +0530206 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
207
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;
215 }
216
217 return ATH9K_KEY_TYPE_CLEAR;
218}
219
Sujith528f0c62008-10-29 10:14:26 +0530220/* Called only when tx aggregation is enabled and HT is supported */
221
222static void assign_aggr_tid_seqno(struct sk_buff *skb,
223 struct ath_buf *bf)
224{
225 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
226 struct ieee80211_hdr *hdr;
227 struct ath_node *an;
228 struct ath_atx_tid *tid;
229 __le16 fc;
230 u8 *qc;
231
232 if (!tx_info->control.sta)
233 return;
234
235 an = (struct ath_node *)tx_info->control.sta->drv_priv;
236 hdr = (struct ieee80211_hdr *)skb->data;
237 fc = hdr->frame_control;
238
239 /* Get tidno */
240
241 if (ieee80211_is_data_qos(fc)) {
242 qc = ieee80211_get_qos_ctl(hdr);
243 bf->bf_tidno = qc[0] & 0xf;
Sujith98deeea2008-08-11 14:05:46 +0530244 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700245
Sujith528f0c62008-10-29 10:14:26 +0530246 /* Get seqno */
247
Sujitha8efee42008-11-18 09:07:30 +0530248 if (ieee80211_is_data(fc) && !is_pae(skb)) {
Sujith528f0c62008-10-29 10:14:26 +0530249 /* 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.
252 *
253 * If fragmentation is on, the sequence number is
254 * not overridden, since it has been
255 * incremented by the fragmentation routine.
256 *
257 * FIXME: check if the fragmentation threshold exceeds
258 * IEEE80211 max.
259 */
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);
265 }
266}
267
268static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
269 struct ath_txq *txq)
270{
271 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
272 int flags = 0;
273
274 flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
275 flags |= ATH9K_TXDESC_INTREQ;
276
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;
281
282 return flags;
283}
284
285static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
286{
287 struct ath_buf *bf = NULL;
288
Sujithb77f4832008-12-07 21:44:03 +0530289 spin_lock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530290
Sujithb77f4832008-12-07 21:44:03 +0530291 if (unlikely(list_empty(&sc->tx.txbuf))) {
292 spin_unlock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530293 return NULL;
294 }
295
Sujithb77f4832008-12-07 21:44:03 +0530296 bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
Sujith528f0c62008-10-29 10:14:26 +0530297 list_del(&bf->list);
298
Sujithb77f4832008-12-07 21:44:03 +0530299 spin_unlock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530300
301 return bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700302}
303
304/* To complete a chain of buffers associated a frame */
305
306static void ath_tx_complete_buf(struct ath_softc *sc,
307 struct ath_buf *bf,
308 struct list_head *bf_q,
309 int txok, int sendbar)
310{
311 struct sk_buff *skb = bf->bf_mpdu;
312 struct ath_xmit_status tx_status;
Senthil Balasubramaniana07d3612008-12-09 17:23:33 +0530313 unsigned long flags;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700314
315 /*
316 * Set retry information.
317 * NB: Don't use the information in the descriptor, because the frame
318 * could be software retried.
319 */
320 tx_status.retries = bf->bf_retries;
321 tx_status.flags = 0;
322
323 if (sendbar)
324 tx_status.flags = ATH_TX_BAR;
325
326 if (!txok) {
327 tx_status.flags |= ATH_TX_ERROR;
328
Sujithcd3d39a2008-08-11 14:03:34 +0530329 if (bf_isxretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700330 tx_status.flags |= ATH_TX_XRETRY;
331 }
Sujith102e0572008-10-29 10:15:16 +0530332
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700333 /* Unmap this frame */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700334 pci_unmap_single(sc->pdev,
Sujithff9b6622008-08-14 13:27:16 +0530335 bf->bf_dmacontext,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700336 skb->len,
337 PCI_DMA_TODEVICE);
338 /* complete this frame */
Sujith528f0c62008-10-29 10:14:26 +0530339 ath_tx_complete(sc, skb, &tx_status);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700340
341 /*
342 * Return the list of ath_buf of this mpdu to free queue
343 */
Sujithb77f4832008-12-07 21:44:03 +0530344 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);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700347}
348
349/*
350 * queue up a dest/ac pair for tx scheduling
351 * NB: must be called with txq lock held
352 */
353
354static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
355{
356 struct ath_atx_ac *ac = tid->ac;
357
358 /*
359 * if tid is paused, hold off
360 */
361 if (tid->paused)
362 return;
363
364 /*
365 * add tid to ac atmost once
366 */
367 if (tid->sched)
368 return;
369
370 tid->sched = true;
371 list_add_tail(&tid->list, &ac->tid_q);
372
373 /*
374 * add node ac to txq atmost once
375 */
376 if (ac->sched)
377 return;
378
379 ac->sched = true;
380 list_add_tail(&ac->list, &txq->axq_acq);
381}
382
383/* pause a tid */
384
385static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
386{
Sujithb77f4832008-12-07 21:44:03 +0530387 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700388
389 spin_lock_bh(&txq->axq_lock);
390
391 tid->paused++;
392
393 spin_unlock_bh(&txq->axq_lock);
394}
395
396/* resume a tid and schedule aggregate */
397
398void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
399{
Sujithb77f4832008-12-07 21:44:03 +0530400 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700401
402 ASSERT(tid->paused > 0);
403 spin_lock_bh(&txq->axq_lock);
404
405 tid->paused--;
406
407 if (tid->paused > 0)
408 goto unlock;
409
410 if (list_empty(&tid->buf_q))
411 goto unlock;
412
413 /*
414 * Add this TID to scheduler and try to send out aggregates
415 */
416 ath_tx_queue_tid(txq, tid);
417 ath_txq_schedule(sc, txq);
418unlock:
419 spin_unlock_bh(&txq->axq_lock);
420}
421
422/* Compute the number of bad frames */
423
Sujithb5aa9bf2008-10-29 10:13:31 +0530424static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
425 int txok)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700426{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700427 struct ath_buf *bf_last = bf->bf_lastbf;
428 struct ath_desc *ds = bf_last->bf_desc;
429 u16 seq_st = 0;
430 u32 ba[WME_BA_BMP_SIZE >> 5];
431 int ba_index;
432 int nbad = 0;
433 int isaggr = 0;
434
Sujithb5aa9bf2008-10-29 10:13:31 +0530435 if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700436 return 0;
437
Sujithcd3d39a2008-08-11 14:03:34 +0530438 isaggr = bf_isaggr(bf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700439 if (isaggr) {
440 seq_st = ATH_DS_BA_SEQ(ds);
441 memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
442 }
443
444 while (bf) {
445 ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
446 if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
447 nbad++;
448
449 bf = bf->bf_next;
450 }
451
452 return nbad;
453}
454
455static void ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
456{
457 struct sk_buff *skb;
458 struct ieee80211_hdr *hdr;
459
Sujithcd3d39a2008-08-11 14:03:34 +0530460 bf->bf_state.bf_type |= BUF_RETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700461 bf->bf_retries++;
462
463 skb = bf->bf_mpdu;
464 hdr = (struct ieee80211_hdr *)skb->data;
465 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
466}
467
468/* Update block ack window */
469
Sujith102e0572008-10-29 10:15:16 +0530470static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
471 int seqno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700472{
473 int index, cindex;
474
475 index = ATH_BA_INDEX(tid->seq_start, seqno);
476 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
477
478 tid->tx_buf[cindex] = NULL;
479
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);
483 }
484}
485
486/*
487 * ath_pkt_dur - compute packet duration (NB: not NAV)
488 *
489 * rix - rate index
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
493 */
Sujith102e0572008-10-29 10:15:16 +0530494static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
495 int width, int half_gi, bool shortPreamble)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700496{
Sujith3706de62008-12-07 21:42:10 +0530497 struct ath_rate_table *rate_table = sc->cur_rate_table;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700498 u32 nbits, nsymbits, duration, nsymbols;
499 u8 rc;
500 int streams, pktlen;
501
Sujithcd3d39a2008-08-11 14:03:34 +0530502 pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
Sujithe63835b2008-11-18 09:07:53 +0530503 rc = rate_table->info[rix].ratecode;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700504
Sujithe63835b2008-11-18 09:07:53 +0530505 /* for legacy rates, use old function to compute packet duration */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700506 if (!IS_HT_RATE(rc))
Sujithe63835b2008-11-18 09:07:53 +0530507 return ath9k_hw_computetxtime(sc->sc_ah, rate_table, pktlen,
508 rix, shortPreamble);
509
510 /* find number of symbols: PLCP + data */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700511 nbits = (pktlen << 3) + OFDM_PLCP_BITS;
512 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
513 nsymbols = (nbits + nsymbits - 1) / nsymbits;
514
515 if (!half_gi)
516 duration = SYMBOL_TIME(nsymbols);
517 else
518 duration = SYMBOL_TIME_HALFGI(nsymbols);
519
Sujithe63835b2008-11-18 09:07:53 +0530520 /* addup duration for legacy/ht training and signal fields */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700521 streams = HT_RC_2_STREAMS(rc);
522 duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
Sujith102e0572008-10-29 10:15:16 +0530523
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700524 return duration;
525}
526
527/* Rate module function to set rate related fields in tx descriptor */
528
529static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
530{
531 struct ath_hal *ah = sc->sc_ah;
Sujithe63835b2008-11-18 09:07:53 +0530532 struct ath_rate_table *rt;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700533 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];
Sujith528f0c62008-10-29 10:14:26 +0530536 struct sk_buff *skb;
537 struct ieee80211_tx_info *tx_info;
Sujitha8efee42008-11-18 09:07:30 +0530538 struct ieee80211_tx_rate *rates;
Sujithe63835b2008-11-18 09:07:53 +0530539 struct ieee80211_hdr *hdr;
540 int i, flags, rtsctsena = 0;
541 u32 ctsduration = 0;
542 u8 rix = 0, cix, ctsrate = 0;
543 __le16 fc;
544
545 memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
Sujith528f0c62008-10-29 10:14:26 +0530546
547 skb = (struct sk_buff *)bf->bf_mpdu;
Sujithe63835b2008-11-18 09:07:53 +0530548 hdr = (struct ieee80211_hdr *)skb->data;
549 fc = hdr->frame_control;
Sujith528f0c62008-10-29 10:14:26 +0530550 tx_info = IEEE80211_SKB_CB(skb);
Sujithe63835b2008-11-18 09:07:53 +0530551 rates = tx_info->control.rates;
Sujith528f0c62008-10-29 10:14:26 +0530552
Sujithe63835b2008-11-18 09:07:53 +0530553 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;
558 }
559
560 /* get the cix for the lowest valid rix */
Sujith3706de62008-12-07 21:42:10 +0530561 rt = sc->cur_rate_table;
Sujitha8efee42008-11-18 09:07:30 +0530562 for (i = 3; i >= 0; i--) {
Sujithe63835b2008-11-18 09:07:53 +0530563 if (rates[i].count && (rates[i].idx >= 0)) {
Sujitha8efee42008-11-18 09:07:30 +0530564 rix = rates[i].idx;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700565 break;
566 }
567 }
Sujithe63835b2008-11-18 09:07:53 +0530568
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700569 flags = (bf->bf_flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA));
Sujithe63835b2008-11-18 09:07:53 +0530570 cix = rt->info[rix].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700571
572 /*
Sujithe63835b2008-11-18 09:07:53 +0530573 * 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.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700575 */
Sujithe63835b2008-11-18 09:07:53 +0530576 if (sc->sc_protmode != PROT_M_NONE && !(bf->bf_flags & ATH9K_TXDESC_NOACK)
Sujith46d14a52008-11-18 09:08:13 +0530577 && (rt->info[rix].phy == WLAN_RC_PHY_OFDM ||
Sujithe63835b2008-11-18 09:07:53 +0530578 WLAN_RC_PHY_HT(rt->info[rix].phy))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700579 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;
583
Sujithe63835b2008-11-18 09:07:53 +0530584 cix = rt->info[sc->sc_protrix].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700585 rtsctsena = 1;
586 }
587
Sujithe63835b2008-11-18 09:07:53 +0530588 /* 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.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700591 */
Sujithcd3d39a2008-08-11 14:03:34 +0530592 if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) && bf_isdata(bf)) {
Sujithe63835b2008-11-18 09:07:53 +0530593 /* 802.11g protection not needed, use our default behavior */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700594 if (!rtsctsena)
595 flags = ATH9K_TXDESC_RTSENA;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700596 }
597
Sujithe63835b2008-11-18 09:07:53 +0530598 /* Set protection if aggregate protection on */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700599 if (sc->sc_config.ath_aggr_prot &&
Sujithcd3d39a2008-08-11 14:03:34 +0530600 (!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700601 flags = ATH9K_TXDESC_RTSENA;
Sujithe63835b2008-11-18 09:07:53 +0530602 cix = rt->info[sc->sc_protrix].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700603 rtsctsena = 1;
604 }
605
Sujithe63835b2008-11-18 09:07:53 +0530606 /* 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))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700608 flags &= ~(ATH9K_TXDESC_RTSENA);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700609
610 /*
Sujithe63835b2008-11-18 09:07:53 +0530611 * 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
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700614 */
Sujithe63835b2008-11-18 09:07:53 +0530615 ctsrate = rt->info[cix].ratecode |
616 (bf_isshpreamble(bf) ? rt->info[cix].short_preamble : 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700617
618 for (i = 0; i < 4; i++) {
Sujithe63835b2008-11-18 09:07:53 +0530619 if (!rates[i].count || (rates[i].idx < 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700620 continue;
621
Sujitha8efee42008-11-18 09:07:30 +0530622 rix = rates[i].idx;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700623
Sujithe63835b2008-11-18 09:07:53 +0530624 series[i].Rate = rt->info[rix].ratecode |
625 (bf_isshpreamble(bf) ? rt->info[rix].short_preamble : 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700626
Sujitha8efee42008-11-18 09:07:30 +0530627 series[i].Tries = rates[i].count;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700628
629 series[i].RateFlags = (
Sujitha8efee42008-11-18 09:07:30 +0530630 (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700631 ATH9K_RATESERIES_RTS_CTS : 0) |
Sujitha8efee42008-11-18 09:07:30 +0530632 ((rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700633 ATH9K_RATESERIES_2040 : 0) |
Sujitha8efee42008-11-18 09:07:30 +0530634 ((rates[i].flags & IEEE80211_TX_RC_SHORT_GI) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700635 ATH9K_RATESERIES_HALFGI : 0);
636
Sujith102e0572008-10-29 10:15:16 +0530637 series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
Sujitha8efee42008-11-18 09:07:30 +0530638 (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) != 0,
639 (rates[i].flags & IEEE80211_TX_RC_SHORT_GI),
Sujith102e0572008-10-29 10:15:16 +0530640 bf_isshpreamble(bf));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700641
Sujithff37e332008-11-24 12:07:55 +0530642 series[i].ChSel = sc->sc_tx_chainmask;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700643
644 if (rtsctsena)
645 series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700646 }
647
Sujithe63835b2008-11-18 09:07:53 +0530648 /* 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,
Sujithcd3d39a2008-08-11 14:03:34 +0530651 series, 4, flags);
Sujith102e0572008-10-29 10:15:16 +0530652
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700653 if (sc->sc_config.ath_aggr_prot && flags)
654 ath9k_hw_set11n_burstduration(ah, ds, 8192);
655}
656
657/*
658 * Function to send a normal HT (non-AMPDU) frame
659 * NB: must be called with txq lock held
660 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700661static int ath_tx_send_normal(struct ath_softc *sc,
662 struct ath_txq *txq,
663 struct ath_atx_tid *tid,
664 struct list_head *bf_head)
665{
666 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700667
668 BUG_ON(list_empty(bf_head));
669
670 bf = list_first_entry(bf_head, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +0530671 bf->bf_state.bf_type &= ~BUF_AMPDU; /* regular HT frame */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700672
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700673 /* update starting sequence number for subsequent ADDBA request */
674 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
675
676 /* Queue to h/w without aggregation */
677 bf->bf_nframes = 1;
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);
681
682 return 0;
683}
684
685/* flush tid's software queue and send frames as non-ampdu's */
686
687static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
688{
Sujithb77f4832008-12-07 21:44:03 +0530689 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700690 struct ath_buf *bf;
691 struct list_head bf_head;
692 INIT_LIST_HEAD(&bf_head);
693
694 ASSERT(tid->paused > 0);
695 spin_lock_bh(&txq->axq_lock);
696
697 tid->paused--;
698
699 if (tid->paused > 0) {
700 spin_unlock_bh(&txq->axq_lock);
701 return;
702 }
703
704 while (!list_empty(&tid->buf_q)) {
705 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +0530706 ASSERT(!bf_isretried(bf));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700707 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
708 ath_tx_send_normal(sc, txq, tid, &bf_head);
709 }
710
711 spin_unlock_bh(&txq->axq_lock);
712}
713
714/* Completion routine of an aggregate */
715
716static void ath_tx_complete_aggr_rifs(struct ath_softc *sc,
717 struct ath_txq *txq,
718 struct ath_buf *bf,
719 struct list_head *bf_q,
720 int txok)
721{
Sujith528f0c62008-10-29 10:14:26 +0530722 struct ath_node *an = NULL;
723 struct sk_buff *skb;
724 struct ieee80211_tx_info *tx_info;
725 struct ath_atx_tid *tid = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700726 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;
730 u16 seq_st = 0;
731 u32 ba[WME_BA_BMP_SIZE >> 5];
732 int isaggr, txfail, txpending, sendbar = 0, needreset = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700733
Sujith528f0c62008-10-29 10:14:26 +0530734 skb = (struct sk_buff *)bf->bf_mpdu;
735 tx_info = IEEE80211_SKB_CB(skb);
736
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);
740 }
741
Sujithcd3d39a2008-08-11 14:03:34 +0530742 isaggr = bf_isaggr(bf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700743 if (isaggr) {
744 if (txok) {
745 if (ATH_DS_TX_BA(ds)) {
746 /*
747 * extract starting sequence and
748 * block-ack bitmap
749 */
750 seq_st = ATH_DS_BA_SEQ(ds);
751 memcpy(ba,
752 ATH_DS_BA_BITMAP(ds),
753 WME_BA_BMP_SIZE >> 3);
754 } else {
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700755 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700756
757 /*
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.
763 */
Colin McCabed97809d2008-12-01 13:38:55 -0800764 if (sc->sc_ah->ah_opmode ==
765 NL80211_IFTYPE_STATION)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700766 needreset = 1;
767 }
768 } else {
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700769 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700770 }
771 }
772
773 INIT_LIST_HEAD(&bf_pending);
774 INIT_LIST_HEAD(&bf_head);
775
776 while (bf) {
777 txfail = txpending = 0;
778 bf_next = bf->bf_next;
779
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 */
785 } else {
786
Sujitha37c2c72008-10-29 10:15:40 +0530787 if (!(tid->state & AGGR_CLEANUP) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700788 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);
791 txpending = 1;
792 } else {
Sujithcd3d39a2008-08-11 14:03:34 +0530793 bf->bf_state.bf_type |= BUF_XRETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700794 txfail = 1;
795 sendbar = 1;
796 }
797 } else {
798 /*
799 * cleanup in progress, just fail
800 * the un-acked sub-frames
801 */
802 txfail = 1;
803 }
804 }
805 /*
806 * Remove ath_buf's of this sub-frame from aggregate queue.
807 */
808 if (bf_next == NULL) { /* last subframe in the aggregate */
809 ASSERT(bf->bf_lastfrm == bf_last);
810
811 /*
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.
816 */
817
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);
823 } else {
824 /*
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.
829 */
830 INIT_LIST_HEAD(&bf_head);
831 }
832 } else {
833 ASSERT(!list_empty(bf_q));
834 list_cut_position(&bf_head,
835 bf_q, &bf->bf_lastfrm->list);
836 }
837
838 if (!txpending) {
839 /*
840 * complete the acked-ones/xretried ones; update
841 * block-ack window
842 */
843 spin_lock_bh(&txq->axq_lock);
844 ath_tx_update_baw(sc, tid, bf->bf_seqno);
845 spin_unlock_bh(&txq->axq_lock);
846
847 /* complete this sub-frame */
848 ath_tx_complete_buf(sc, bf, &bf_head, !txfail, sendbar);
849 } else {
850 /*
851 * retry the un-acked ones
852 */
853 /*
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.
858 */
859 if (bf->bf_next == NULL &&
860 bf_last->bf_status & ATH_BUFSTATUS_STALE) {
861 struct ath_buf *tbf;
862
863 /* allocate new descriptor */
Sujithb77f4832008-12-07 21:44:03 +0530864 spin_lock_bh(&sc->tx.txbuflock);
865 ASSERT(!list_empty((&sc->tx.txbuf)));
866 tbf = list_first_entry(&sc->tx.txbuf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700867 struct ath_buf, list);
868 list_del(&tbf->list);
Sujithb77f4832008-12-07 21:44:03 +0530869 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700870
871 ATH_TXBUF_RESET(tbf);
872
873 /* copy descriptor content */
874 tbf->bf_mpdu = bf_last->bf_mpdu;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700875 tbf->bf_buf_addr = bf_last->bf_buf_addr;
876 *(tbf->bf_desc) = *(bf_last->bf_desc);
877
878 /* link it to the frame */
879 if (bf_lastq) {
880 bf_lastq->bf_desc->ds_link =
881 tbf->bf_daddr;
882 bf->bf_lastfrm = tbf;
883 ath9k_hw_cleartxdesc(sc->sc_ah,
884 bf->bf_lastfrm->bf_desc);
885 } else {
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);
890
891 /* copy the DMA context */
Sujithff9b6622008-08-14 13:27:16 +0530892 tbf->bf_dmacontext =
893 bf_last->bf_dmacontext;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700894 }
895 list_add_tail(&tbf->list, &bf_head);
896 } else {
897 /*
898 * Clear descriptor status words for
899 * software retry
900 */
901 ath9k_hw_cleartxdesc(sc->sc_ah,
Sujithff9b6622008-08-14 13:27:16 +0530902 bf->bf_lastfrm->bf_desc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700903 }
904
905 /*
906 * Put this buffer to the temporary pending
907 * queue to retain ordering
908 */
909 list_splice_tail_init(&bf_head, &bf_pending);
910 }
911
912 bf = bf_next;
913 }
914
Sujitha37c2c72008-10-29 10:15:40 +0530915 if (tid->state & AGGR_CLEANUP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700916 /* check to see if we're done with cleaning the h/w queue */
917 spin_lock_bh(&txq->axq_lock);
918
919 if (tid->baw_head == tid->baw_tail) {
Sujitha37c2c72008-10-29 10:15:40 +0530920 tid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700921 tid->addba_exchangeattempts = 0;
922 spin_unlock_bh(&txq->axq_lock);
923
Sujitha37c2c72008-10-29 10:15:40 +0530924 tid->state &= ~AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700925
926 /* send buffered frames as singles */
927 ath_tx_flush_tid(sc, tid);
928 } else
929 spin_unlock_bh(&txq->axq_lock);
930
931 return;
932 }
933
934 /*
935 * prepend un-acked frames to the beginning of the pending frame queue
936 */
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);
944 }
945
946 if (needreset)
Sujithf45144e2008-08-11 14:02:53 +0530947 ath_reset(sc, false);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700948
949 return;
950}
951
Sujithc4288392008-11-18 09:09:30 +0530952static void ath_tx_rc_status(struct ath_buf *bf, struct ath_desc *ds, int nbad)
953{
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);
957
Vasanthakumar Thiagarajan7ac47012008-11-20 11:51:18 +0530958 tx_info_priv->update_rc = false;
Sujithc4288392008-11-18 09:09:30 +0530959 if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
960 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
961
962 if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
963 (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
964 if (bf_isdata(bf)) {
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;
Vasanthakumar Thiagarajan7ac47012008-11-20 11:51:18 +0530969 tx_info_priv->update_rc = true;
Sujithc4288392008-11-18 09:09:30 +0530970 }
971 }
972}
973
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700974/* Process completed xmit descriptors from the specified queue */
975
Sujithc4288392008-11-18 09:09:30 +0530976static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700977{
978 struct ath_hal *ah = sc->sc_ah;
979 struct ath_buf *bf, *lastbf, *bf_held = NULL;
980 struct list_head bf_head;
Sujithc4288392008-11-18 09:09:30 +0530981 struct ath_desc *ds;
982 int txok, nbad = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700983 int status;
984
Sujith04bd46382008-11-28 22:18:05 +0530985 DPRINTF(sc, ATH_DBG_QUEUE, "tx queue %d (%x), link %p\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700986 txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
987 txq->axq_link);
988
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700989 for (;;) {
990 spin_lock_bh(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700991 if (list_empty(&txq->axq_q)) {
992 txq->axq_link = NULL;
993 txq->axq_linkbuf = NULL;
994 spin_unlock_bh(&txq->axq_lock);
995 break;
996 }
997 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
998
999 /*
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.
1006 */
1007 bf_held = NULL;
1008 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
1009 bf_held = bf;
1010 if (list_is_last(&bf_held->list, &txq->axq_q)) {
1011 /* FIXME:
1012 * The holding descriptor is the last
1013 * descriptor in queue. It's safe to remove
1014 * the last holding descriptor in BH context.
1015 */
1016 spin_unlock_bh(&txq->axq_lock);
1017 break;
1018 } else {
1019 /* Lets work with the next buffer now */
1020 bf = list_entry(bf_held->list.next,
1021 struct ath_buf, list);
1022 }
1023 }
1024
1025 lastbf = bf->bf_lastbf;
1026 ds = lastbf->bf_desc; /* NB: last decriptor */
1027
1028 status = ath9k_hw_txprocdesc(ah, ds);
1029 if (status == -EINPROGRESS) {
1030 spin_unlock_bh(&txq->axq_lock);
1031 break;
1032 }
1033 if (bf->bf_desc == txq->axq_lastdsWithCTS)
1034 txq->axq_lastdsWithCTS = NULL;
1035 if (ds == txq->axq_gatingds)
1036 txq->axq_gatingds = NULL;
1037
1038 /*
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.
1042 */
1043 lastbf->bf_status |= ATH_BUFSTATUS_STALE;
1044 INIT_LIST_HEAD(&bf_head);
1045
1046 if (!list_is_singular(&lastbf->list))
1047 list_cut_position(&bf_head,
1048 &txq->axq_q, lastbf->list.prev);
1049
1050 txq->axq_depth--;
1051
Sujithcd3d39a2008-08-11 14:03:34 +05301052 if (bf_isaggr(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001053 txq->axq_aggr_depth--;
1054
1055 txok = (ds->ds_txstat.ts_status == 0);
1056
1057 spin_unlock_bh(&txq->axq_lock);
1058
1059 if (bf_held) {
1060 list_del(&bf_held->list);
Sujithb77f4832008-12-07 21:44:03 +05301061 spin_lock_bh(&sc->tx.txbuflock);
1062 list_add_tail(&bf_held->list, &sc->tx.txbuf);
1063 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001064 }
1065
Sujithcd3d39a2008-08-11 14:03:34 +05301066 if (!bf_isampdu(bf)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001067 /*
1068 * This frame is sent out as a single frame.
1069 * Use hardware retry status for this frame.
1070 */
1071 bf->bf_retries = ds->ds_txstat.ts_longretry;
1072 if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
Sujithcd3d39a2008-08-11 14:03:34 +05301073 bf->bf_state.bf_type |= BUF_XRETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001074 nbad = 0;
1075 } else {
1076 nbad = ath_tx_num_badfrms(sc, bf, txok);
1077 }
Johannes Berge6a98542008-10-21 12:40:02 +02001078
Sujithc4288392008-11-18 09:09:30 +05301079 ath_tx_rc_status(bf, ds, nbad);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001080
1081 /*
1082 * Complete this transmit unit
1083 */
Sujithcd3d39a2008-08-11 14:03:34 +05301084 if (bf_isampdu(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001085 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, txok);
1086 else
1087 ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
1088
1089 /* Wake up mac80211 queue */
1090
1091 spin_lock_bh(&txq->axq_lock);
1092 if (txq->stopped && ath_txq_depth(sc, txq->axq_qnum) <=
1093 (ATH_TXBUF - 20)) {
1094 int qnum;
1095 qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
1096 if (qnum != -1) {
1097 ieee80211_wake_queue(sc->hw, qnum);
1098 txq->stopped = 0;
1099 }
1100
1101 }
1102
1103 /*
1104 * schedule any pending packets if aggregation is enabled
1105 */
Sujith672840a2008-08-11 14:05:08 +05301106 if (sc->sc_flags & SC_OP_TXAGGR)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001107 ath_txq_schedule(sc, txq);
1108 spin_unlock_bh(&txq->axq_lock);
1109 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001110}
1111
1112static void ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
1113{
1114 struct ath_hal *ah = sc->sc_ah;
1115
1116 (void) ath9k_hw_stoptxdma(ah, txq->axq_qnum);
Sujith04bd46382008-11-28 22:18:05 +05301117 DPRINTF(sc, ATH_DBG_XMIT, "tx queue [%u] %x, link %p\n",
1118 txq->axq_qnum, ath9k_hw_gettxbuf(ah, txq->axq_qnum),
1119 txq->axq_link);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001120}
1121
1122/* Drain only the data queues */
1123
1124static void ath_drain_txdataq(struct ath_softc *sc, bool retry_tx)
1125{
1126 struct ath_hal *ah = sc->sc_ah;
Sujith102e0572008-10-29 10:15:16 +05301127 int i, status, npend = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001128
Sujith672840a2008-08-11 14:05:08 +05301129 if (!(sc->sc_flags & SC_OP_INVALID)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001130 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1131 if (ATH_TXQ_SETUP(sc, i)) {
Sujithb77f4832008-12-07 21:44:03 +05301132 ath_tx_stopdma(sc, &sc->tx.txq[i]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001133 /* The TxDMA may not really be stopped.
1134 * Double check the hal tx pending count */
1135 npend += ath9k_hw_numtxpending(ah,
Sujithb77f4832008-12-07 21:44:03 +05301136 sc->tx.txq[i].axq_qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001137 }
1138 }
1139 }
1140
1141 if (npend) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001142 /* TxDMA not stopped, reset the hal */
Sujith04bd46382008-11-28 22:18:05 +05301143 DPRINTF(sc, ATH_DBG_XMIT, "Unable to stop TxDMA. Reset HAL!\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001144
1145 spin_lock_bh(&sc->sc_resetlock);
Sujithb4696c8b2008-08-11 14:04:52 +05301146 if (!ath9k_hw_reset(ah,
Sujith927e70e2008-08-14 13:26:34 +05301147 sc->sc_ah->ah_curchan,
Sujith99405f92008-11-24 12:08:35 +05301148 sc->tx_chan_width,
Sujith927e70e2008-08-14 13:26:34 +05301149 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
1150 sc->sc_ht_extprotspacing, true, &status)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001151
1152 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301153 "Unable to reset hardware; hal status %u\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001154 status);
1155 }
1156 spin_unlock_bh(&sc->sc_resetlock);
1157 }
1158
1159 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1160 if (ATH_TXQ_SETUP(sc, i))
Sujithb77f4832008-12-07 21:44:03 +05301161 ath_tx_draintxq(sc, &sc->tx.txq[i], retry_tx);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001162 }
1163}
1164
1165/* Add a sub-frame to block ack window */
1166
1167static void ath_tx_addto_baw(struct ath_softc *sc,
1168 struct ath_atx_tid *tid,
1169 struct ath_buf *bf)
1170{
1171 int index, cindex;
1172
Sujithcd3d39a2008-08-11 14:03:34 +05301173 if (bf_isretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001174 return;
1175
1176 index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
1177 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
1178
1179 ASSERT(tid->tx_buf[cindex] == NULL);
1180 tid->tx_buf[cindex] = bf;
1181
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);
1186 }
1187}
1188
1189/*
1190 * Function to send an A-MPDU
1191 * NB: must be called with txq lock held
1192 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001193static int ath_tx_send_ampdu(struct ath_softc *sc,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001194 struct ath_atx_tid *tid,
1195 struct list_head *bf_head,
1196 struct ath_tx_control *txctl)
1197{
1198 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001199
1200 BUG_ON(list_empty(bf_head));
1201
1202 bf = list_first_entry(bf_head, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +05301203 bf->bf_state.bf_type |= BUF_AMPDU;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001204
1205 /*
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
1211 */
1212 if (!list_empty(&tid->buf_q) || tid->paused ||
1213 !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
Sujith528f0c62008-10-29 10:14:26 +05301214 txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001215 /*
1216 * Add this frame to software queue for scheduling later
1217 * for aggregation.
1218 */
1219 list_splice_tail_init(bf_head, &tid->buf_q);
Sujith528f0c62008-10-29 10:14:26 +05301220 ath_tx_queue_tid(txctl->txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001221 return 0;
1222 }
1223
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001224 /* Add sub-frame to BAW */
1225 ath_tx_addto_baw(sc, tid, bf);
1226
1227 /* Queue to h/w without aggregation */
1228 bf->bf_nframes = 1;
1229 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
1230 ath_buf_set_rate(sc, bf);
Sujith528f0c62008-10-29 10:14:26 +05301231 ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
Sujith102e0572008-10-29 10:15:16 +05301232
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001233 return 0;
1234}
1235
1236/*
1237 * looks up the rate
1238 * returns aggr limit based on lowest of the rates
1239 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001240static u32 ath_lookup_rate(struct ath_softc *sc,
Johannes Bergae5eb022008-10-14 16:58:37 +02001241 struct ath_buf *bf,
1242 struct ath_atx_tid *tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001243{
Sujith3706de62008-12-07 21:42:10 +05301244 struct ath_rate_table *rate_table = sc->cur_rate_table;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001245 struct sk_buff *skb;
1246 struct ieee80211_tx_info *tx_info;
Sujitha8efee42008-11-18 09:07:30 +05301247 struct ieee80211_tx_rate *rates;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001248 struct ath_tx_info_priv *tx_info_priv;
1249 u32 max_4ms_framelen, frame_length;
1250 u16 aggr_limit, legacy = 0, maxampdu;
1251 int i;
1252
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001253 skb = (struct sk_buff *)bf->bf_mpdu;
1254 tx_info = IEEE80211_SKB_CB(skb);
Sujitha8efee42008-11-18 09:07:30 +05301255 rates = tx_info->control.rates;
1256 tx_info_priv =
1257 (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001258
1259 /*
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.
1263 */
1264 max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
1265
1266 for (i = 0; i < 4; i++) {
Sujitha8efee42008-11-18 09:07:30 +05301267 if (rates[i].count) {
Sujithe63835b2008-11-18 09:07:53 +05301268 if (!WLAN_RC_PHY_HT(rate_table->info[rates[i].idx].phy)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001269 legacy = 1;
1270 break;
1271 }
1272
Sujitha8efee42008-11-18 09:07:30 +05301273 frame_length =
1274 rate_table->info[rates[i].idx].max_4ms_framelen;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001275 max_4ms_framelen = min(max_4ms_framelen, frame_length);
1276 }
1277 }
1278
1279 /*
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.
1283 */
1284 if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
1285 return 0;
1286
1287 aggr_limit = min(max_4ms_framelen,
1288 (u32)ATH_AMPDU_LIMIT_DEFAULT);
1289
1290 /*
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.
1294 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001295 maxampdu = tid->an->maxampdu;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001296 if (maxampdu)
1297 aggr_limit = min(aggr_limit, maxampdu);
1298
1299 return aggr_limit;
1300}
1301
1302/*
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 .
1306 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001307static int ath_compute_num_delims(struct ath_softc *sc,
Johannes Bergae5eb022008-10-14 16:58:37 +02001308 struct ath_atx_tid *tid,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001309 struct ath_buf *bf,
1310 u16 frmlen)
1311{
Sujith3706de62008-12-07 21:42:10 +05301312 struct ath_rate_table *rt = sc->cur_rate_table;
Sujitha8efee42008-11-18 09:07:30 +05301313 struct sk_buff *skb = bf->bf_mpdu;
1314 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001315 u32 nsymbits, nsymbols, mpdudensity;
1316 u16 minlen;
1317 u8 rc, flags, rix;
1318 int width, half_gi, ndelim, mindelim;
1319
1320 /* Select standard number of delimiters based on frame length alone */
1321 ndelim = ATH_AGGR_GET_NDELIM(frmlen);
1322
1323 /*
1324 * If encryption enabled, hardware requires some more padding between
1325 * subframes.
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
1328 */
1329 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
1330 ndelim += ATH_AGGR_ENCRYPTDELIM;
1331
1332 /*
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.
1337 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001338 mpdudensity = tid->an->mpdudensity;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001339
1340 /*
1341 * If there is no mpdu density restriction, no further calculation
1342 * is needed.
1343 */
1344 if (mpdudensity == 0)
1345 return ndelim;
1346
Sujitha8efee42008-11-18 09:07:30 +05301347 rix = tx_info->control.rates[0].idx;
1348 flags = tx_info->control.rates[0].flags;
Sujithe63835b2008-11-18 09:07:53 +05301349 rc = rt->info[rix].ratecode;
Sujitha8efee42008-11-18 09:07:30 +05301350 width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
1351 half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001352
1353 if (half_gi)
1354 nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
1355 else
1356 nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
1357
1358 if (nsymbols == 0)
1359 nsymbols = 1;
1360
1361 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
1362 minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
1363
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);
1369 }
1370
1371 return ndelim;
1372}
1373
1374/*
1375 * For aggregation from software buffer queue.
1376 * NB: must be called with txq lock held
1377 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001378static 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,
1383 int *prev_frames)
1384{
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;
Sujitha8efee42008-11-18 09:07:30 +05301392 int prev_al = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001393 INIT_LIST_HEAD(&bf_head);
1394
1395 BUG_ON(list_empty(&tid->buf_q));
1396
1397 bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
1398
1399 do {
1400 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1401
1402 /*
1403 * do not step over block-ack window
1404 */
1405 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
1406 status = ATH_AGGR_BAW_CLOSED;
1407 break;
1408 }
1409
1410 if (!rl) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001411 aggr_limit = ath_lookup_rate(sc, bf, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001412 rl = 1;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001413 }
1414
1415 /*
1416 * do not exceed aggregation limit
1417 */
1418 al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
1419
1420 if (nframes && (aggr_limit <
1421 (al + bpad + al_delta + prev_al))) {
1422 status = ATH_AGGR_LIMITED;
1423 break;
1424 }
1425
1426 /*
1427 * do not exceed subframe limit
1428 */
1429 if ((nframes + *prev_frames) >=
1430 min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
1431 status = ATH_AGGR_LIMITED;
1432 break;
1433 }
1434
1435 /*
1436 * add padding for previous frame to aggregation length
1437 */
1438 al += bpad + al_delta;
1439
1440 /*
1441 * Get the delimiters needed to meet the MPDU
1442 * density for this node.
1443 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001444 ndelim = ath_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001445
1446 bpad = PADBYTES(al_delta) + (ndelim << 2);
1447
1448 bf->bf_next = NULL;
1449 bf->bf_lastfrm->bf_desc->ds_link = 0;
1450
1451 /*
1452 * this packet is part of an aggregate
1453 * - remove all descriptors belonging to this frame from
1454 * software queue
1455 * - add it to block ack window
1456 * - set up descriptors for aggregation
1457 */
1458 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1459 ath_tx_addto_baw(sc, tid, bf);
1460
1461 list_for_each_entry(tbf, &bf_head, list) {
1462 ath9k_hw_set11n_aggr_middle(sc->sc_ah,
1463 tbf->bf_desc, ndelim);
1464 }
1465
1466 /*
1467 * link buffers of this frame to the aggregate
1468 */
1469 list_splice_tail_init(&bf_head, bf_q);
1470 nframes++;
1471
1472 if (bf_prev) {
1473 bf_prev->bf_next = bf;
1474 bf_prev->bf_lastfrm->bf_desc->ds_link = bf->bf_daddr;
1475 }
1476 bf_prev = bf;
1477
1478#ifdef AGGR_NOSHORT
1479 /*
1480 * terminate aggregation on a small packet boundary
1481 */
1482 if (bf->bf_frmlen < ATH_AGGR_MINPLEN) {
1483 status = ATH_AGGR_SHORTPKT;
1484 break;
1485 }
1486#endif
1487 } while (!list_empty(&tid->buf_q));
1488
1489 bf_first->bf_al = al;
1490 bf_first->bf_nframes = nframes;
1491 *bf_last = bf_prev;
1492 return status;
1493#undef PADBYTES
1494}
1495
1496/*
1497 * process pending frames possibly doing a-mpdu aggregation
1498 * NB: must be called with txq lock held
1499 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001500static void ath_tx_sched_aggr(struct ath_softc *sc,
1501 struct ath_txq *txq, struct ath_atx_tid *tid)
1502{
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;
1508
1509 do {
1510 if (list_empty(&tid->buf_q))
1511 return;
1512
1513 INIT_LIST_HEAD(&bf_q);
1514
1515 status = ath_tx_form_aggr(sc, tid, &bf_q, &bf_lastaggr, &param,
1516 &prev_frames);
1517
1518 /*
1519 * no frames picked up to be aggregated; block-ack
1520 * window is not open
1521 */
1522 if (list_empty(&bf_q))
1523 break;
1524
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;
1528
1529 /*
1530 * if only one frame, send as non-aggregate
1531 */
1532 if (bf->bf_nframes == 1) {
1533 ASSERT(bf->bf_lastfrm == bf_last);
1534
Sujithcd3d39a2008-08-11 14:03:34 +05301535 bf->bf_state.bf_type &= ~BUF_AGGR;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001536 /*
1537 * clear aggr bits for every descriptor
1538 * XXX TODO: is there a way to optimize it?
1539 */
1540 list_for_each_entry(tbf, &bf_q, list) {
1541 ath9k_hw_clr11n_aggr(sc->sc_ah, tbf->bf_desc);
1542 }
1543
1544 ath_buf_set_rate(sc, bf);
1545 ath_tx_txqaddbuf(sc, txq, &bf_q);
1546 continue;
1547 }
1548
1549 /*
1550 * setup first desc with rate and aggr info
1551 */
Sujithcd3d39a2008-08-11 14:03:34 +05301552 bf->bf_state.bf_type |= BUF_AGGR;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001553 ath_buf_set_rate(sc, bf);
1554 ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
1555
1556 /*
1557 * anchor last frame of aggregate correctly
1558 */
1559 ASSERT(bf_lastaggr);
1560 ASSERT(bf_lastaggr->bf_lastfrm == bf_last);
1561 tbf = bf_lastaggr;
1562 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1563
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);
1568 }
1569
1570 txq->axq_aggr_depth++;
1571
1572 /*
1573 * Normal aggregate, queue to hardware
1574 */
1575 ath_tx_txqaddbuf(sc, txq, &bf_q);
1576
1577 } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
1578 status != ATH_AGGR_BAW_CLOSED);
1579}
1580
1581/* Called with txq lock held */
1582
1583static void ath_tid_drain(struct ath_softc *sc,
1584 struct ath_txq *txq,
Sujithb5aa9bf2008-10-29 10:13:31 +05301585 struct ath_atx_tid *tid)
1586
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001587{
1588 struct ath_buf *bf;
1589 struct list_head bf_head;
1590 INIT_LIST_HEAD(&bf_head);
1591
1592 for (;;) {
1593 if (list_empty(&tid->buf_q))
1594 break;
1595 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1596
1597 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1598
1599 /* update baw for software retried frame */
Sujithcd3d39a2008-08-11 14:03:34 +05301600 if (bf_isretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001601 ath_tx_update_baw(sc, tid, bf->bf_seqno);
1602
1603 /*
1604 * do not indicate packets while holding txq spinlock.
1605 * unlock is intentional here
1606 */
Sujithb5aa9bf2008-10-29 10:13:31 +05301607 spin_unlock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001608
1609 /* complete this sub-frame */
1610 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
1611
Sujithb5aa9bf2008-10-29 10:13:31 +05301612 spin_lock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001613 }
1614
1615 /*
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
1619 * forward.
1620 */
1621 tid->seq_next = tid->seq_start;
1622 tid->baw_tail = tid->baw_head;
1623}
1624
1625/*
1626 * Drain all pending buffers
1627 * NB: must be called with txq lock held
1628 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001629static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
Sujithb5aa9bf2008-10-29 10:13:31 +05301630 struct ath_txq *txq)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001631{
1632 struct ath_atx_ac *ac, *ac_tmp;
1633 struct ath_atx_tid *tid, *tid_tmp;
1634
1635 list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
1636 list_del(&ac->list);
1637 ac->sched = false;
1638 list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
1639 list_del(&tid->list);
1640 tid->sched = false;
Sujithb5aa9bf2008-10-29 10:13:31 +05301641 ath_tid_drain(sc, txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001642 }
1643 }
1644}
1645
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001646static int ath_tx_setup_buffer(struct ath_softc *sc, struct ath_buf *bf,
Sujith8f93b8b2008-11-18 09:10:42 +05301647 struct sk_buff *skb,
Sujith528f0c62008-10-29 10:14:26 +05301648 struct ath_tx_control *txctl)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001649{
Sujith528f0c62008-10-29 10:14:26 +05301650 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1651 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001652 struct ath_tx_info_priv *tx_info_priv;
Sujith528f0c62008-10-29 10:14:26 +05301653 int hdrlen;
1654 __le16 fc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001655
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001656 tx_info_priv = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
1657 if (unlikely(!tx_info_priv))
1658 return -ENOMEM;
Sujitha8efee42008-11-18 09:07:30 +05301659 tx_info->rate_driver_data[0] = tx_info_priv;
Sujith528f0c62008-10-29 10:14:26 +05301660 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1661 fc = hdr->frame_control;
Jouni Malinene022edb2008-08-22 17:31:33 +03001662
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001663 ATH_TXBUF_RESET(bf);
Sujith528f0c62008-10-29 10:14:26 +05301664
1665 /* Frame type */
1666
1667 bf->bf_frmlen = skb->len + FCS_LEN - (hdrlen & 3);
Sujithcd3d39a2008-08-11 14:03:34 +05301668
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);
Sujith672840a2008-08-11 14:05:08 +05301678 (sc->sc_flags & SC_OP_PREAMBLE_SHORT) ?
Sujithcd3d39a2008-08-11 14:03:34 +05301679 (bf->bf_state.bf_type |= BUF_SHORT_PREAMBLE) :
1680 (bf->bf_state.bf_type &= ~BUF_SHORT_PREAMBLE);
Sujitha8efee42008-11-18 09:07:30 +05301681 (sc->hw->conf.ht.enabled && !is_pae(skb) &&
Sujith528f0c62008-10-29 10:14:26 +05301682 (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) ?
1683 (bf->bf_state.bf_type |= BUF_HT) :
1684 (bf->bf_state.bf_type &= ~BUF_HT);
Sujithcd3d39a2008-08-11 14:03:34 +05301685
Sujith528f0c62008-10-29 10:14:26 +05301686 bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
1687
1688 /* Crypto */
1689
1690 bf->bf_keytype = get_hw_crypto_keytype(skb);
1691
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;
1695 } else {
1696 bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
1697 }
1698
Sujith528f0c62008-10-29 10:14:26 +05301699 /* Assign seqno, tidno */
1700
1701 if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR))
1702 assign_aggr_tid_seqno(skb, bf);
1703
1704 /* DMA setup */
1705
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001706 bf->bf_mpdu = skb;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001707
Sujith528f0c62008-10-29 10:14:26 +05301708 bf->bf_dmacontext = pci_map_single(sc->pdev, skb->data,
1709 skb->len, PCI_DMA_TODEVICE);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001710 if (unlikely(pci_dma_mapping_error(sc->pdev, bf->bf_dmacontext))) {
1711 bf->bf_mpdu = NULL;
1712 DPRINTF(sc, ATH_DBG_CONFIG,
1713 "pci_dma_mapping_error() on TX\n");
1714 return -ENOMEM;
1715 }
1716
Sujith528f0c62008-10-29 10:14:26 +05301717 bf->bf_buf_addr = bf->bf_dmacontext;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001718 return 0;
Sujith528f0c62008-10-29 10:14:26 +05301719}
1720
1721/* FIXME: tx power */
1722static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
Sujith528f0c62008-10-29 10:14:26 +05301723 struct ath_tx_control *txctl)
1724{
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;
1732 int frm_type;
1733
Sujith528f0c62008-10-29 10:14:26 +05301734 frm_type = get_hw_packet_type(skb);
1735
1736 INIT_LIST_HEAD(&bf_head);
1737 list_add_tail(&bf->list, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001738
1739 /* setup descriptor */
Sujith528f0c62008-10-29 10:14:26 +05301740
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001741 ds = bf->bf_desc;
1742 ds->ds_link = 0;
1743 ds->ds_data = bf->bf_buf_addr;
1744
Sujith528f0c62008-10-29 10:14:26 +05301745 /* Formulate first tx descriptor with tx controls */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001746
Sujith528f0c62008-10-29 10:14:26 +05301747 ath9k_hw_set11n_txdesc(ah, ds, bf->bf_frmlen, frm_type, MAX_RATE_POWER,
1748 bf->bf_keyix, bf->bf_keytype, bf->bf_flags);
1749
1750 ath9k_hw_filltxdesc(ah, ds,
Sujith8f93b8b2008-11-18 09:10:42 +05301751 skb->len, /* segment length */
1752 true, /* first segment */
1753 true, /* last segment */
1754 ds); /* first descriptor */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001755
1756 bf->bf_lastfrm = bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001757
Sujith528f0c62008-10-29 10:14:26 +05301758 spin_lock_bh(&txctl->txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001759
John W. Linvillef1617962008-10-31 16:45:15 -04001760 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);
1764
Sujith528f0c62008-10-29 10:14:26 +05301765 if (ath_aggr_query(sc, an, bf->bf_tidno)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001766 /*
1767 * Try aggregation if it's a unicast data frame
1768 * and the destination is HT capable.
1769 */
Sujith528f0c62008-10-29 10:14:26 +05301770 ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001771 } else {
1772 /*
Sujith528f0c62008-10-29 10:14:26 +05301773 * Send this frame as regular when ADDBA
1774 * exchange is neither complete nor pending.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001775 */
Sujith528f0c62008-10-29 10:14:26 +05301776 ath_tx_send_normal(sc, txctl->txq,
1777 tid, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001778 }
1779 } else {
1780 bf->bf_lastbf = bf;
1781 bf->bf_nframes = 1;
Sujith528f0c62008-10-29 10:14:26 +05301782
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001783 ath_buf_set_rate(sc, bf);
Sujith528f0c62008-10-29 10:14:26 +05301784 ath_tx_txqaddbuf(sc, txctl->txq, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001785 }
Sujith528f0c62008-10-29 10:14:26 +05301786
1787 spin_unlock_bh(&txctl->txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001788}
1789
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001790/* Upon failure caller should free skb */
Sujith528f0c62008-10-29 10:14:26 +05301791int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb,
1792 struct ath_tx_control *txctl)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001793{
Sujith528f0c62008-10-29 10:14:26 +05301794 struct ath_buf *bf;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001795 int r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001796
Sujith528f0c62008-10-29 10:14:26 +05301797 /* Check if a tx buffer is available */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001798
Sujith528f0c62008-10-29 10:14:26 +05301799 bf = ath_tx_get_buffer(sc);
1800 if (!bf) {
Sujith04bd46382008-11-28 22:18:05 +05301801 DPRINTF(sc, ATH_DBG_XMIT, "TX buffers are full\n");
Sujith528f0c62008-10-29 10:14:26 +05301802 return -1;
1803 }
1804
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001805 r = ath_tx_setup_buffer(sc, bf, skb, txctl);
1806 if (unlikely(r)) {
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001807 struct ath_txq *txq = txctl->txq;
1808
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001809 DPRINTF(sc, ATH_DBG_FATAL, "TX mem alloc failure\n");
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001810
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
1814 * on the queue */
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));
1819 txq->stopped = 1;
1820 }
1821 spin_unlock_bh(&txq->axq_lock);
1822
Sujithb77f4832008-12-07 21:44:03 +05301823 spin_lock_bh(&sc->tx.txbuflock);
1824 list_add_tail(&bf->list, &sc->tx.txbuf);
1825 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001826
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001827 return r;
1828 }
1829
Sujith8f93b8b2008-11-18 09:10:42 +05301830 ath_tx_start_dma(sc, bf, txctl);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001831
Sujith528f0c62008-10-29 10:14:26 +05301832 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001833}
1834
1835/* Initialize TX queue and h/w */
1836
1837int ath_tx_init(struct ath_softc *sc, int nbufs)
1838{
1839 int error = 0;
1840
1841 do {
Sujithb77f4832008-12-07 21:44:03 +05301842 spin_lock_init(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001843
1844 /* Setup tx descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301845 error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
Sujith556bb8f2008-08-11 14:03:53 +05301846 "tx", nbufs, 1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001847 if (error != 0) {
1848 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301849 "Failed to allocate tx descriptors: %d\n",
1850 error);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001851 break;
1852 }
1853
1854 /* XXX allocate beacon state together with vap */
Sujithb77f4832008-12-07 21:44:03 +05301855 error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001856 "beacon", ATH_BCBUF, 1);
1857 if (error != 0) {
1858 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301859 "Failed to allocate beacon descriptors: %d\n",
1860 error);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001861 break;
1862 }
1863
1864 } while (0);
1865
1866 if (error != 0)
1867 ath_tx_cleanup(sc);
1868
1869 return error;
1870}
1871
1872/* Reclaim all tx queue resources */
1873
1874int ath_tx_cleanup(struct ath_softc *sc)
1875{
1876 /* cleanup beacon descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301877 if (sc->beacon.bdma.dd_desc_len != 0)
1878 ath_descdma_cleanup(sc, &sc->beacon.bdma, &sc->beacon.bbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001879
1880 /* cleanup tx descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301881 if (sc->tx.txdma.dd_desc_len != 0)
1882 ath_descdma_cleanup(sc, &sc->tx.txdma, &sc->tx.txbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001883
1884 return 0;
1885}
1886
1887/* Setup a h/w transmit queue */
1888
1889struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
1890{
1891 struct ath_hal *ah = sc->sc_ah;
Sujithea9880f2008-08-07 10:53:10 +05301892 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001893 int qnum;
1894
Luis R. Rodriguez0345f372008-10-03 15:45:25 -07001895 memset(&qi, 0, sizeof(qi));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001896 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;
Sujithea9880f2008-08-07 10:53:10 +05301900 qi.tqi_physCompBuf = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001901
1902 /*
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.
1913 *
1914 * The UAPSD queue is an exception, since we take a desc-
1915 * based intr on the EOSP frames.
1916 */
1917 if (qtype == ATH9K_TX_QUEUE_UAPSD)
1918 qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1919 else
1920 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
1921 TXQ_FLAG_TXDESCINT_ENABLE;
1922 qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
1923 if (qnum == -1) {
1924 /*
1925 * NB: don't print a message, this happens
1926 * normally on parts with too few tx queues
1927 */
1928 return NULL;
1929 }
Sujithb77f4832008-12-07 21:44:03 +05301930 if (qnum >= ARRAY_SIZE(sc->tx.txq)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001931 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301932 "qnum %u out of range, max %u!\n",
Sujithb77f4832008-12-07 21:44:03 +05301933 qnum, (unsigned int)ARRAY_SIZE(sc->tx.txq));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001934 ath9k_hw_releasetxqueue(ah, qnum);
1935 return NULL;
1936 }
1937 if (!ATH_TXQ_SETUP(sc, qnum)) {
Sujithb77f4832008-12-07 21:44:03 +05301938 struct ath_txq *txq = &sc->tx.txq[qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001939
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);
1945 txq->axq_depth = 0;
1946 txq->axq_aggr_depth = 0;
1947 txq->axq_totalqueued = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001948 txq->axq_linkbuf = NULL;
Sujithb77f4832008-12-07 21:44:03 +05301949 sc->tx.txqsetup |= 1<<qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001950 }
Sujithb77f4832008-12-07 21:44:03 +05301951 return &sc->tx.txq[qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001952}
1953
1954/* Reclaim resources for a setup queue */
1955
1956void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1957{
1958 ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
Sujithb77f4832008-12-07 21:44:03 +05301959 sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001960}
1961
1962/*
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.
1970 */
1971
1972int ath_tx_setup(struct ath_softc *sc, int haltype)
1973{
1974 struct ath_txq *txq;
1975
Sujithb77f4832008-12-07 21:44:03 +05301976 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001977 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301978 "HAL AC %u out of range, max %zu!\n",
Sujithb77f4832008-12-07 21:44:03 +05301979 haltype, ARRAY_SIZE(sc->tx.hwq_map));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001980 return 0;
1981 }
1982 txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
1983 if (txq != NULL) {
Sujithb77f4832008-12-07 21:44:03 +05301984 sc->tx.hwq_map[haltype] = txq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001985 return 1;
1986 } else
1987 return 0;
1988}
1989
1990int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
1991{
1992 int qnum;
1993
1994 switch (qtype) {
1995 case ATH9K_TX_QUEUE_DATA:
Sujithb77f4832008-12-07 21:44:03 +05301996 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001997 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301998 "HAL AC %u out of range, max %zu!\n",
Sujithb77f4832008-12-07 21:44:03 +05301999 haltype, ARRAY_SIZE(sc->tx.hwq_map));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002000 return -1;
2001 }
Sujithb77f4832008-12-07 21:44:03 +05302002 qnum = sc->tx.hwq_map[haltype];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002003 break;
2004 case ATH9K_TX_QUEUE_BEACON:
Sujithb77f4832008-12-07 21:44:03 +05302005 qnum = sc->beacon.beaconq;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002006 break;
2007 case ATH9K_TX_QUEUE_CAB:
Sujithb77f4832008-12-07 21:44:03 +05302008 qnum = sc->beacon.cabq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002009 break;
2010 default:
2011 qnum = -1;
2012 }
2013 return qnum;
2014}
2015
Sujith528f0c62008-10-29 10:14:26 +05302016/* Get a transmit queue, if available */
2017
2018struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
2019{
2020 struct ath_txq *txq = NULL;
2021 int qnum;
2022
2023 qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
Sujithb77f4832008-12-07 21:44:03 +05302024 txq = &sc->tx.txq[qnum];
Sujith528f0c62008-10-29 10:14:26 +05302025
2026 spin_lock_bh(&txq->axq_lock);
2027
2028 /* Try to avoid running out of descriptors */
2029 if (txq->axq_depth >= (ATH_TXBUF - 20)) {
2030 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05302031 "TX queue: %d is full, depth: %d\n",
2032 qnum, txq->axq_depth);
Sujith528f0c62008-10-29 10:14:26 +05302033 ieee80211_stop_queue(sc->hw, skb_get_queue_mapping(skb));
2034 txq->stopped = 1;
2035 spin_unlock_bh(&txq->axq_lock);
2036 return NULL;
2037 }
2038
2039 spin_unlock_bh(&txq->axq_lock);
2040
2041 return txq;
2042}
2043
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002044/* Update parameters for a transmit queue */
2045
Sujithea9880f2008-08-07 10:53:10 +05302046int ath_txq_update(struct ath_softc *sc, int qnum,
2047 struct ath9k_tx_queue_info *qinfo)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002048{
2049 struct ath_hal *ah = sc->sc_ah;
2050 int error = 0;
Sujithea9880f2008-08-07 10:53:10 +05302051 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002052
Sujithb77f4832008-12-07 21:44:03 +05302053 if (qnum == sc->beacon.beaconq) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002054 /*
2055 * XXX: for beacon queue, we just save the parameter.
2056 * It will be picked up by ath_beaconq_config when
2057 * it's necessary.
2058 */
Sujithb77f4832008-12-07 21:44:03 +05302059 sc->beacon.beacon_qi = *qinfo;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002060 return 0;
2061 }
2062
Sujithb77f4832008-12-07 21:44:03 +05302063 ASSERT(sc->tx.txq[qnum].axq_qnum == qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002064
Sujithea9880f2008-08-07 10:53:10 +05302065 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;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002071
Sujithea9880f2008-08-07 10:53:10 +05302072 if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002073 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05302074 "Unable to update hardware queue %u!\n", qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002075 error = -EIO;
2076 } else {
2077 ath9k_hw_resettxqueue(ah, qnum); /* push to h/w */
2078 }
2079
2080 return error;
2081}
2082
2083int ath_cabq_update(struct ath_softc *sc)
2084{
Sujithea9880f2008-08-07 10:53:10 +05302085 struct ath9k_tx_queue_info qi;
Sujithb77f4832008-12-07 21:44:03 +05302086 int qnum = sc->beacon.cabq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002087 struct ath_beacon_config conf;
2088
Sujithea9880f2008-08-07 10:53:10 +05302089 ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002090 /*
2091 * Ensure the readytime % is within the bounds.
2092 */
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;
2097
2098 ath_get_beaconconfig(sc, ATH_IF_ID_ANY, &conf);
2099 qi.tqi_readyTime =
2100 (conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
2101 ath_txq_update(sc, qnum, &qi);
2102
2103 return 0;
2104}
2105
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002106/* Deferred processing of transmit interrupt */
2107
2108void ath_tx_tasklet(struct ath_softc *sc)
2109{
Sujith1fe11322008-08-26 08:11:06 +05302110 int i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002111 u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
2112
2113 ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
2114
2115 /*
2116 * Process each active queue.
2117 */
2118 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2119 if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
Sujithb77f4832008-12-07 21:44:03 +05302120 ath_tx_processq(sc, &sc->tx.txq[i]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002121 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002122}
2123
2124void ath_tx_draintxq(struct ath_softc *sc,
2125 struct ath_txq *txq, bool retry_tx)
2126{
2127 struct ath_buf *bf, *lastbf;
2128 struct list_head bf_head;
2129
2130 INIT_LIST_HEAD(&bf_head);
2131
2132 /*
2133 * NB: this assumes output has been stopped and
2134 * we do not need to block ath_tx_tasklet
2135 */
2136 for (;;) {
2137 spin_lock_bh(&txq->axq_lock);
2138
2139 if (list_empty(&txq->axq_q)) {
2140 txq->axq_link = NULL;
2141 txq->axq_linkbuf = NULL;
2142 spin_unlock_bh(&txq->axq_lock);
2143 break;
2144 }
2145
2146 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2147
2148 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
2149 list_del(&bf->list);
2150 spin_unlock_bh(&txq->axq_lock);
2151
Sujithb77f4832008-12-07 21:44:03 +05302152 spin_lock_bh(&sc->tx.txbuflock);
2153 list_add_tail(&bf->list, &sc->tx.txbuf);
2154 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002155 continue;
2156 }
2157
2158 lastbf = bf->bf_lastbf;
2159 if (!retry_tx)
2160 lastbf->bf_desc->ds_txstat.ts_flags =
2161 ATH9K_TX_SW_ABORTED;
2162
2163 /* remove ath_buf's of the same mpdu from txq */
2164 list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
2165 txq->axq_depth--;
2166
2167 spin_unlock_bh(&txq->axq_lock);
2168
Sujithcd3d39a2008-08-11 14:03:34 +05302169 if (bf_isampdu(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002170 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, 0);
2171 else
2172 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2173 }
2174
2175 /* flush any pending frames if aggregation is enabled */
Sujith672840a2008-08-11 14:05:08 +05302176 if (sc->sc_flags & SC_OP_TXAGGR) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002177 if (!retry_tx) {
2178 spin_lock_bh(&txq->axq_lock);
Sujithb5aa9bf2008-10-29 10:13:31 +05302179 ath_txq_drain_pending_buffers(sc, txq);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002180 spin_unlock_bh(&txq->axq_lock);
2181 }
2182 }
2183}
2184
2185/* Drain the transmit queues and reclaim resources */
2186
2187void ath_draintxq(struct ath_softc *sc, bool retry_tx)
2188{
2189 /* stop beacon queue. The beacon will be freed when
2190 * we go to INIT state */
Sujith672840a2008-08-11 14:05:08 +05302191 if (!(sc->sc_flags & SC_OP_INVALID)) {
Sujithb77f4832008-12-07 21:44:03 +05302192 (void) ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
Sujith04bd46382008-11-28 22:18:05 +05302193 DPRINTF(sc, ATH_DBG_XMIT, "beacon queue %x\n",
Sujithb77f4832008-12-07 21:44:03 +05302194 ath9k_hw_gettxbuf(sc->sc_ah, sc->beacon.beaconq));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002195 }
2196
2197 ath_drain_txdataq(sc, retry_tx);
2198}
2199
2200u32 ath_txq_depth(struct ath_softc *sc, int qnum)
2201{
Sujithb77f4832008-12-07 21:44:03 +05302202 return sc->tx.txq[qnum].axq_depth;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002203}
2204
2205u32 ath_txq_aggr_depth(struct ath_softc *sc, int qnum)
2206{
Sujithb77f4832008-12-07 21:44:03 +05302207 return sc->tx.txq[qnum].axq_aggr_depth;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002208}
2209
Sujithccc75c52008-10-29 10:18:14 +05302210bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an, u8 tidno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002211{
2212 struct ath_atx_tid *txtid;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002213
Sujith672840a2008-08-11 14:05:08 +05302214 if (!(sc->sc_flags & SC_OP_TXAGGR))
Sujithccc75c52008-10-29 10:18:14 +05302215 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002216
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002217 txtid = ATH_AN_2_TID(an, tidno);
2218
Sujitha37c2c72008-10-29 10:15:40 +05302219 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
2220 if (!(txtid->state & AGGR_ADDBA_PROGRESS) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002221 (txtid->addba_exchangeattempts < ADDBA_EXCHANGE_ATTEMPTS)) {
2222 txtid->addba_exchangeattempts++;
Sujithccc75c52008-10-29 10:18:14 +05302223 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002224 }
2225 }
2226
Sujithccc75c52008-10-29 10:18:14 +05302227 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002228}
2229
2230/* Start TX aggregation */
2231
Sujithb5aa9bf2008-10-29 10:13:31 +05302232int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
2233 u16 tid, u16 *ssn)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002234{
2235 struct ath_atx_tid *txtid;
2236 struct ath_node *an;
2237
Sujithb5aa9bf2008-10-29 10:13:31 +05302238 an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002239
Sujith672840a2008-08-11 14:05:08 +05302240 if (sc->sc_flags & SC_OP_TXAGGR) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002241 txtid = ATH_AN_2_TID(an, tid);
Sujitha37c2c72008-10-29 10:15:40 +05302242 txtid->state |= AGGR_ADDBA_PROGRESS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002243 ath_tx_pause_tid(sc, txtid);
2244 }
2245
2246 return 0;
2247}
2248
2249/* Stop tx aggregation */
2250
Sujithb5aa9bf2008-10-29 10:13:31 +05302251int ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002252{
Sujithb5aa9bf2008-10-29 10:13:31 +05302253 struct ath_node *an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002254
2255 ath_tx_aggr_teardown(sc, an, tid);
2256 return 0;
2257}
2258
Sujith8469cde2008-10-29 10:19:28 +05302259/* Resume tx aggregation */
2260
2261void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
2262{
2263 struct ath_atx_tid *txtid;
2264 struct ath_node *an;
2265
2266 an = (struct ath_node *)sta->drv_priv;
2267
2268 if (sc->sc_flags & SC_OP_TXAGGR) {
2269 txtid = ATH_AN_2_TID(an, tid);
2270 txtid->baw_size =
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);
2275 }
2276}
2277
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002278/*
2279 * Performs transmit side cleanup when TID changes from aggregated to
2280 * unaggregated.
2281 * - Pause the TID and mark cleanup in progress
2282 * - Discard all retry frames from the s/w queue.
2283 */
2284
Sujithb5aa9bf2008-10-29 10:13:31 +05302285void ath_tx_aggr_teardown(struct ath_softc *sc, struct ath_node *an, u8 tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002286{
2287 struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
Sujithb77f4832008-12-07 21:44:03 +05302288 struct ath_txq *txq = &sc->tx.txq[txtid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002289 struct ath_buf *bf;
2290 struct list_head bf_head;
2291 INIT_LIST_HEAD(&bf_head);
2292
Sujitha37c2c72008-10-29 10:15:40 +05302293 if (txtid->state & AGGR_CLEANUP) /* cleanup is in progress */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002294 return;
2295
Sujitha37c2c72008-10-29 10:15:40 +05302296 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002297 txtid->addba_exchangeattempts = 0;
2298 return;
2299 }
2300
2301 /* TID must be paused first */
2302 ath_tx_pause_tid(sc, txtid);
2303
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);
Sujithcd3d39a2008-08-11 14:03:34 +05302308 if (!bf_isretried(bf)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002309 /*
2310 * NB: it's based on the assumption that
2311 * software retried frame will always stay
2312 * at the head of software queue.
2313 */
2314 break;
2315 }
2316 list_cut_position(&bf_head,
2317 &txtid->buf_q, &bf->bf_lastfrm->list);
2318 ath_tx_update_baw(sc, txtid, bf->bf_seqno);
2319
2320 /* complete this sub-frame */
2321 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2322 }
2323
2324 if (txtid->baw_head != txtid->baw_tail) {
2325 spin_unlock_bh(&txq->axq_lock);
Sujitha37c2c72008-10-29 10:15:40 +05302326 txtid->state |= AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002327 } else {
Sujitha37c2c72008-10-29 10:15:40 +05302328 txtid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002329 txtid->addba_exchangeattempts = 0;
2330 spin_unlock_bh(&txq->axq_lock);
2331 ath_tx_flush_tid(sc, txtid);
2332 }
2333}
2334
2335/*
2336 * Tx scheduling logic
2337 * NB: must be called with txq lock held
2338 */
2339
2340void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
2341{
2342 struct ath_atx_ac *ac;
2343 struct ath_atx_tid *tid;
2344
2345 /* nothing to schedule */
2346 if (list_empty(&txq->axq_acq))
2347 return;
2348 /*
2349 * get the first node/ac pair on the queue
2350 */
2351 ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
2352 list_del(&ac->list);
2353 ac->sched = false;
2354
2355 /*
2356 * process a single tid per destination
2357 */
2358 do {
2359 /* nothing to schedule */
2360 if (list_empty(&ac->tid_q))
2361 return;
2362
2363 tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
2364 list_del(&tid->list);
2365 tid->sched = false;
2366
2367 if (tid->paused) /* check next tid to keep h/w busy */
2368 continue;
2369
Sujith43453b32008-10-29 10:14:52 +05302370 if ((txq->axq_depth % 2) == 0)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002371 ath_tx_sched_aggr(sc, txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002372
2373 /*
2374 * add tid to round-robin queue if more frames
2375 * are pending for the tid
2376 */
2377 if (!list_empty(&tid->buf_q))
2378 ath_tx_queue_tid(txq, tid);
2379
2380 /* only schedule one TID at a time */
2381 break;
2382 } while (!list_empty(&ac->tid_q));
2383
2384 /*
2385 * schedule AC if more TIDs need processing
2386 */
2387 if (!list_empty(&ac->tid_q)) {
2388 /*
2389 * add dest ac to txq if not already added
2390 */
2391 if (!ac->sched) {
2392 ac->sched = true;
2393 list_add_tail(&ac->list, &txq->axq_acq);
2394 }
2395 }
2396}
2397
2398/* Initialize per-node transmit state */
2399
2400void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2401{
Sujithc5170162008-10-29 10:13:59 +05302402 struct ath_atx_tid *tid;
2403 struct ath_atx_ac *ac;
2404 int tidno, acno;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002405
Sujithc5170162008-10-29 10:13:59 +05302406 /*
2407 * Init per tid tx state
2408 */
Sujith8ee5afb2008-12-07 21:43:36 +05302409 for (tidno = 0, tid = &an->tid[tidno];
Sujithc5170162008-10-29 10:13:59 +05302410 tidno < WME_NUM_TID;
2411 tidno++, tid++) {
2412 tid->an = an;
2413 tid->tidno = tidno;
2414 tid->seq_start = tid->seq_next = 0;
2415 tid->baw_size = WME_MAX_BA;
2416 tid->baw_head = tid->baw_tail = 0;
2417 tid->sched = false;
2418 tid->paused = false;
Sujitha37c2c72008-10-29 10:15:40 +05302419 tid->state &= ~AGGR_CLEANUP;
Sujithc5170162008-10-29 10:13:59 +05302420 INIT_LIST_HEAD(&tid->buf_q);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002421
Sujithc5170162008-10-29 10:13:59 +05302422 acno = TID_TO_WME_AC(tidno);
Sujith8ee5afb2008-12-07 21:43:36 +05302423 tid->ac = &an->ac[acno];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002424
Sujithc5170162008-10-29 10:13:59 +05302425 /* ADDBA state */
Sujitha37c2c72008-10-29 10:15:40 +05302426 tid->state &= ~AGGR_ADDBA_COMPLETE;
2427 tid->state &= ~AGGR_ADDBA_PROGRESS;
2428 tid->addba_exchangeattempts = 0;
Sujithc5170162008-10-29 10:13:59 +05302429 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002430
Sujithc5170162008-10-29 10:13:59 +05302431 /*
2432 * Init per ac tx state
2433 */
Sujith8ee5afb2008-12-07 21:43:36 +05302434 for (acno = 0, ac = &an->ac[acno];
Sujithc5170162008-10-29 10:13:59 +05302435 acno < WME_NUM_AC; acno++, ac++) {
2436 ac->sched = false;
2437 INIT_LIST_HEAD(&ac->tid_q);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002438
Sujithc5170162008-10-29 10:13:59 +05302439 switch (acno) {
2440 case WME_AC_BE:
2441 ac->qnum = ath_tx_get_qnum(sc,
2442 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
2443 break;
2444 case WME_AC_BK:
2445 ac->qnum = ath_tx_get_qnum(sc,
2446 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
2447 break;
2448 case WME_AC_VI:
2449 ac->qnum = ath_tx_get_qnum(sc,
2450 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
2451 break;
2452 case WME_AC_VO:
2453 ac->qnum = ath_tx_get_qnum(sc,
2454 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
2455 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002456 }
2457 }
2458}
2459
2460/* Cleanupthe pending buffers for the node. */
2461
Sujithb5aa9bf2008-10-29 10:13:31 +05302462void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002463{
2464 int i;
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)) {
Sujithb77f4832008-12-07 21:44:03 +05302470 txq = &sc->tx.txq[i];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002471
Sujithb5aa9bf2008-10-29 10:13:31 +05302472 spin_lock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002473
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)
2479 continue;
2480 list_del(&ac->list);
2481 ac->sched = false;
2482
2483 list_for_each_entry_safe(tid,
2484 tid_tmp, &ac->tid_q, list) {
2485 list_del(&tid->list);
2486 tid->sched = false;
Sujithb5aa9bf2008-10-29 10:13:31 +05302487 ath_tid_drain(sc, txq, tid);
Sujitha37c2c72008-10-29 10:15:40 +05302488 tid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002489 tid->addba_exchangeattempts = 0;
Sujitha37c2c72008-10-29 10:15:40 +05302490 tid->state &= ~AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002491 }
2492 }
2493
Sujithb5aa9bf2008-10-29 10:13:31 +05302494 spin_unlock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002495 }
2496 }
2497}
2498
Jouni Malinene022edb2008-08-22 17:31:33 +03002499void ath_tx_cabq(struct ath_softc *sc, struct sk_buff *skb)
2500{
2501 int hdrlen, padsize;
2502 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2503 struct ath_tx_control txctl;
2504
Sujith528f0c62008-10-29 10:14:26 +05302505 memset(&txctl, 0, sizeof(struct ath_tx_control));
2506
Jouni Malinene022edb2008-08-22 17:31:33 +03002507 /*
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
2510 * BSSes.
2511 */
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)
Sujithb77f4832008-12-07 21:44:03 +05302515 sc->tx.seq_no += 0x10;
Jouni Malinene022edb2008-08-22 17:31:33 +03002516 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Sujithb77f4832008-12-07 21:44:03 +05302517 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
Jouni Malinene022edb2008-08-22 17:31:33 +03002518 }
2519
2520 /* Add the padding after the header if this is not already done */
2521 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
2522 if (hdrlen & 3) {
2523 padsize = hdrlen % 4;
2524 if (skb_headroom(skb) < padsize) {
Sujith04bd46382008-11-28 22:18:05 +05302525 DPRINTF(sc, ATH_DBG_XMIT, "TX CABQ padding failed\n");
Jouni Malinene022edb2008-08-22 17:31:33 +03002526 dev_kfree_skb_any(skb);
2527 return;
2528 }
2529 skb_push(skb, padsize);
2530 memmove(skb->data, skb->data + padsize, hdrlen);
2531 }
2532
Sujithb77f4832008-12-07 21:44:03 +05302533 txctl.txq = sc->beacon.cabq;
Sujith528f0c62008-10-29 10:14:26 +05302534
Sujith04bd46382008-11-28 22:18:05 +05302535 DPRINTF(sc, ATH_DBG_XMIT, "transmitting CABQ packet, skb: %p\n", skb);
Jouni Malinene022edb2008-08-22 17:31:33 +03002536
Sujith528f0c62008-10-29 10:14:26 +05302537 if (ath_tx_start(sc, skb, &txctl) != 0) {
Sujith04bd46382008-11-28 22:18:05 +05302538 DPRINTF(sc, ATH_DBG_XMIT, "CABQ TX failed\n");
Sujith528f0c62008-10-29 10:14:26 +05302539 goto exit;
Jouni Malinene022edb2008-08-22 17:31:33 +03002540 }
Jouni Malinene022edb2008-08-22 17:31:33 +03002541
Sujith528f0c62008-10-29 10:14:26 +05302542 return;
2543exit:
2544 dev_kfree_skb_any(skb);
2545}