blob: c32da053c6ed28210d2174a4ad3e86ba911a8a31 [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
Sujithcee075a2009-03-13 09:07:23 +05302 * Copyright (c) 2008-2009 Atheros Communications Inc.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07003 *
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
Sujith394cf0a2009-02-09 13:26:54 +053017#include "ath9k.h"
Luis R. Rodriguezb622a722010-04-15 17:39:28 -040018#include "ar9003_mac.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070019
20#define BITS_PER_BYTE 8
21#define OFDM_PLCP_BITS 22
22#define HT_RC_2_MCS(_rc) ((_rc) & 0x0f)
23#define HT_RC_2_STREAMS(_rc) ((((_rc) & 0x78) >> 3) + 1)
24#define L_STF 8
25#define L_LTF 8
26#define L_SIG 4
27#define HT_SIG 8
28#define HT_STF 4
29#define HT_LTF(_ns) (4 * (_ns))
30#define SYMBOL_TIME(_ns) ((_ns) << 2) /* ns * 4 us */
31#define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5) /* ns * 3.6 us */
32#define NUM_SYMBOLS_PER_USEC(_usec) (_usec >> 2)
33#define NUM_SYMBOLS_PER_USEC_HALFGI(_usec) (((_usec*5)-4)/18)
34
35#define OFDM_SIFS_TIME 16
36
37static u32 bits_per_symbol[][2] = {
38 /* 20MHz 40MHz */
39 { 26, 54 }, /* 0: BPSK */
40 { 52, 108 }, /* 1: QPSK 1/2 */
41 { 78, 162 }, /* 2: QPSK 3/4 */
42 { 104, 216 }, /* 3: 16-QAM 1/2 */
43 { 156, 324 }, /* 4: 16-QAM 3/4 */
44 { 208, 432 }, /* 5: 64-QAM 2/3 */
45 { 234, 486 }, /* 6: 64-QAM 3/4 */
46 { 260, 540 }, /* 7: 64-QAM 5/6 */
47 { 52, 108 }, /* 8: BPSK */
48 { 104, 216 }, /* 9: QPSK 1/2 */
49 { 156, 324 }, /* 10: QPSK 3/4 */
50 { 208, 432 }, /* 11: 16-QAM 1/2 */
51 { 312, 648 }, /* 12: 16-QAM 3/4 */
52 { 416, 864 }, /* 13: 64-QAM 2/3 */
53 { 468, 972 }, /* 14: 64-QAM 3/4 */
54 { 520, 1080 }, /* 15: 64-QAM 5/6 */
55};
56
57#define IS_HT_RATE(_rate) ((_rate) & 0x80)
58
Sujithc37452b2009-03-09 09:31:57 +053059static void ath_tx_send_ht_normal(struct ath_softc *sc, struct ath_txq *txq,
60 struct ath_atx_tid *tid,
61 struct list_head *bf_head);
Sujithe8324352009-01-16 21:38:42 +053062static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
Felix Fietkaudb1a0522010-03-29 20:07:11 -070063 struct ath_txq *txq, struct list_head *bf_q,
64 struct ath_tx_status *ts, int txok, int sendbar);
Sujithe8324352009-01-16 21:38:42 +053065static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
66 struct list_head *head);
67static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf);
Vasanthakumar Thiagarajan0934af22009-03-18 20:22:00 +053068static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
Felix Fietkaudb1a0522010-03-29 20:07:11 -070069 struct ath_tx_status *ts, int txok);
70static void ath_tx_rc_status(struct ath_buf *bf, struct ath_tx_status *ts,
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +053071 int nbad, int txok, bool update_rc);
Sujithe8324352009-01-16 21:38:42 +053072
Felix Fietkau545750d2009-11-23 22:21:01 +010073enum {
74 MCS_DEFAULT,
75 MCS_HT40,
76 MCS_HT40_SGI,
77};
78
79static int ath_max_4ms_framelen[3][16] = {
80 [MCS_DEFAULT] = {
81 3216, 6434, 9650, 12868, 19304, 25740, 28956, 32180,
82 6430, 12860, 19300, 25736, 38600, 51472, 57890, 64320,
83 },
84 [MCS_HT40] = {
85 6684, 13368, 20052, 26738, 40104, 53476, 60156, 66840,
86 13360, 26720, 40080, 53440, 80160, 106880, 120240, 133600,
87 },
88 [MCS_HT40_SGI] = {
89 /* TODO: Only MCS 7 and 15 updated, recalculate the rest */
90 6684, 13368, 20052, 26738, 40104, 53476, 60156, 74200,
91 13360, 26720, 40080, 53440, 80160, 106880, 120240, 148400,
92 }
93};
94
95
Sujithe8324352009-01-16 21:38:42 +053096/*********************/
97/* Aggregation logic */
98/*********************/
99
Sujithe8324352009-01-16 21:38:42 +0530100static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
101{
102 struct ath_atx_ac *ac = tid->ac;
103
104 if (tid->paused)
105 return;
106
107 if (tid->sched)
108 return;
109
110 tid->sched = true;
111 list_add_tail(&tid->list, &ac->tid_q);
112
113 if (ac->sched)
114 return;
115
116 ac->sched = true;
117 list_add_tail(&ac->list, &txq->axq_acq);
118}
119
120static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
121{
122 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
123
124 spin_lock_bh(&txq->axq_lock);
125 tid->paused++;
126 spin_unlock_bh(&txq->axq_lock);
127}
128
129static void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
130{
131 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
132
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -0700133 BUG_ON(tid->paused <= 0);
Sujithe8324352009-01-16 21:38:42 +0530134 spin_lock_bh(&txq->axq_lock);
135
136 tid->paused--;
137
138 if (tid->paused > 0)
139 goto unlock;
140
141 if (list_empty(&tid->buf_q))
142 goto unlock;
143
144 ath_tx_queue_tid(txq, tid);
145 ath_txq_schedule(sc, txq);
146unlock:
147 spin_unlock_bh(&txq->axq_lock);
148}
149
150static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
151{
152 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
153 struct ath_buf *bf;
154 struct list_head bf_head;
155 INIT_LIST_HEAD(&bf_head);
156
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -0700157 BUG_ON(tid->paused <= 0);
Sujithe8324352009-01-16 21:38:42 +0530158 spin_lock_bh(&txq->axq_lock);
159
160 tid->paused--;
161
162 if (tid->paused > 0) {
163 spin_unlock_bh(&txq->axq_lock);
164 return;
165 }
166
167 while (!list_empty(&tid->buf_q)) {
168 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -0700169 BUG_ON(bf_isretried(bf));
Sujithd43f30152009-01-16 21:38:53 +0530170 list_move_tail(&bf->list, &bf_head);
Sujithc37452b2009-03-09 09:31:57 +0530171 ath_tx_send_ht_normal(sc, txq, tid, &bf_head);
Sujithe8324352009-01-16 21:38:42 +0530172 }
173
174 spin_unlock_bh(&txq->axq_lock);
175}
176
177static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
178 int seqno)
179{
180 int index, cindex;
181
182 index = ATH_BA_INDEX(tid->seq_start, seqno);
183 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
184
185 tid->tx_buf[cindex] = NULL;
186
187 while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
188 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
189 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
190 }
191}
192
193static void ath_tx_addto_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
194 struct ath_buf *bf)
195{
196 int index, cindex;
197
198 if (bf_isretried(bf))
199 return;
200
201 index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
202 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
203
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -0700204 BUG_ON(tid->tx_buf[cindex] != NULL);
Sujithe8324352009-01-16 21:38:42 +0530205 tid->tx_buf[cindex] = bf;
206
207 if (index >= ((tid->baw_tail - tid->baw_head) &
208 (ATH_TID_MAX_BUFS - 1))) {
209 tid->baw_tail = cindex;
210 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
211 }
212}
213
214/*
215 * TODO: For frame(s) that are in the retry state, we will reuse the
216 * sequence number(s) without setting the retry bit. The
217 * alternative is to give up on these and BAR the receiver's window
218 * forward.
219 */
220static void ath_tid_drain(struct ath_softc *sc, struct ath_txq *txq,
221 struct ath_atx_tid *tid)
222
223{
224 struct ath_buf *bf;
225 struct list_head bf_head;
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700226 struct ath_tx_status ts;
227
228 memset(&ts, 0, sizeof(ts));
Sujithe8324352009-01-16 21:38:42 +0530229 INIT_LIST_HEAD(&bf_head);
230
231 for (;;) {
232 if (list_empty(&tid->buf_q))
233 break;
Sujithe8324352009-01-16 21:38:42 +0530234
Sujithd43f30152009-01-16 21:38:53 +0530235 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
236 list_move_tail(&bf->list, &bf_head);
Sujithe8324352009-01-16 21:38:42 +0530237
238 if (bf_isretried(bf))
239 ath_tx_update_baw(sc, tid, bf->bf_seqno);
240
241 spin_unlock(&txq->axq_lock);
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700242 ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0, 0);
Sujithe8324352009-01-16 21:38:42 +0530243 spin_lock(&txq->axq_lock);
244 }
245
246 tid->seq_next = tid->seq_start;
247 tid->baw_tail = tid->baw_head;
248}
249
Sujithfec247c2009-07-27 12:08:16 +0530250static void ath_tx_set_retry(struct ath_softc *sc, struct ath_txq *txq,
251 struct ath_buf *bf)
Sujithe8324352009-01-16 21:38:42 +0530252{
253 struct sk_buff *skb;
254 struct ieee80211_hdr *hdr;
255
256 bf->bf_state.bf_type |= BUF_RETRY;
257 bf->bf_retries++;
Sujithfec247c2009-07-27 12:08:16 +0530258 TX_STAT_INC(txq->axq_qnum, a_retries);
Sujithe8324352009-01-16 21:38:42 +0530259
260 skb = bf->bf_mpdu;
261 hdr = (struct ieee80211_hdr *)skb->data;
262 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
263}
264
Sujithd43f30152009-01-16 21:38:53 +0530265static struct ath_buf* ath_clone_txbuf(struct ath_softc *sc, struct ath_buf *bf)
266{
267 struct ath_buf *tbf;
268
269 spin_lock_bh(&sc->tx.txbuflock);
Vasanthakumar Thiagarajan8a460972009-06-10 17:50:09 +0530270 if (WARN_ON(list_empty(&sc->tx.txbuf))) {
271 spin_unlock_bh(&sc->tx.txbuflock);
272 return NULL;
273 }
Sujithd43f30152009-01-16 21:38:53 +0530274 tbf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
275 list_del(&tbf->list);
276 spin_unlock_bh(&sc->tx.txbuflock);
277
278 ATH_TXBUF_RESET(tbf);
279
Felix Fietkau827e69b2009-11-15 23:09:25 +0100280 tbf->aphy = bf->aphy;
Sujithd43f30152009-01-16 21:38:53 +0530281 tbf->bf_mpdu = bf->bf_mpdu;
282 tbf->bf_buf_addr = bf->bf_buf_addr;
Vasanthakumar Thiagarajand826c832010-04-15 17:38:45 -0400283 memcpy(tbf->bf_desc, bf->bf_desc, sc->sc_ah->caps.tx_desc_len);
Sujithd43f30152009-01-16 21:38:53 +0530284 tbf->bf_state = bf->bf_state;
285 tbf->bf_dmacontext = bf->bf_dmacontext;
286
287 return tbf;
288}
289
290static void ath_tx_complete_aggr(struct ath_softc *sc, struct ath_txq *txq,
291 struct ath_buf *bf, struct list_head *bf_q,
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700292 struct ath_tx_status *ts, int txok)
Sujithe8324352009-01-16 21:38:42 +0530293{
294 struct ath_node *an = NULL;
295 struct sk_buff *skb;
Sujith1286ec62009-01-27 13:30:37 +0530296 struct ieee80211_sta *sta;
Luis R. Rodriguez76d5a9e2009-11-02 16:08:34 -0800297 struct ieee80211_hw *hw;
Sujith1286ec62009-01-27 13:30:37 +0530298 struct ieee80211_hdr *hdr;
Luis R. Rodriguez76d5a9e2009-11-02 16:08:34 -0800299 struct ieee80211_tx_info *tx_info;
Sujithe8324352009-01-16 21:38:42 +0530300 struct ath_atx_tid *tid = NULL;
Sujithd43f30152009-01-16 21:38:53 +0530301 struct ath_buf *bf_next, *bf_last = bf->bf_lastbf;
Sujithe8324352009-01-16 21:38:42 +0530302 struct list_head bf_head, bf_pending;
Vasanthakumar Thiagarajan0934af22009-03-18 20:22:00 +0530303 u16 seq_st = 0, acked_cnt = 0, txfail_cnt = 0;
Sujithe8324352009-01-16 21:38:42 +0530304 u32 ba[WME_BA_BMP_SIZE >> 5];
Vasanthakumar Thiagarajan0934af22009-03-18 20:22:00 +0530305 int isaggr, txfail, txpending, sendbar = 0, needreset = 0, nbad = 0;
306 bool rc_update = true;
Sujithe8324352009-01-16 21:38:42 +0530307
Sujitha22be222009-03-30 15:28:36 +0530308 skb = bf->bf_mpdu;
Sujith1286ec62009-01-27 13:30:37 +0530309 hdr = (struct ieee80211_hdr *)skb->data;
Sujithe8324352009-01-16 21:38:42 +0530310
Luis R. Rodriguez76d5a9e2009-11-02 16:08:34 -0800311 tx_info = IEEE80211_SKB_CB(skb);
Felix Fietkau827e69b2009-11-15 23:09:25 +0100312 hw = bf->aphy->hw;
Luis R. Rodriguez76d5a9e2009-11-02 16:08:34 -0800313
Sujith1286ec62009-01-27 13:30:37 +0530314 rcu_read_lock();
315
Johannes Berg5ed176e2009-11-04 14:42:28 +0100316 /* XXX: use ieee80211_find_sta! */
Luis R. Rodriguez76d5a9e2009-11-02 16:08:34 -0800317 sta = ieee80211_find_sta_by_hw(hw, hdr->addr1);
Sujith1286ec62009-01-27 13:30:37 +0530318 if (!sta) {
319 rcu_read_unlock();
320 return;
Sujithe8324352009-01-16 21:38:42 +0530321 }
322
Sujith1286ec62009-01-27 13:30:37 +0530323 an = (struct ath_node *)sta->drv_priv;
324 tid = ATH_AN_2_TID(an, bf->bf_tidno);
325
Sujithe8324352009-01-16 21:38:42 +0530326 isaggr = bf_isaggr(bf);
Sujithd43f30152009-01-16 21:38:53 +0530327 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
Sujithe8324352009-01-16 21:38:42 +0530328
Sujithd43f30152009-01-16 21:38:53 +0530329 if (isaggr && txok) {
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700330 if (ts->ts_flags & ATH9K_TX_BA) {
331 seq_st = ts->ts_seqnum;
332 memcpy(ba, &ts->ba_low, WME_BA_BMP_SIZE >> 3);
Sujithe8324352009-01-16 21:38:42 +0530333 } else {
Sujithd43f30152009-01-16 21:38:53 +0530334 /*
335 * AR5416 can become deaf/mute when BA
336 * issue happens. Chip needs to be reset.
337 * But AP code may have sychronization issues
338 * when perform internal reset in this routine.
339 * Only enable reset in STA mode for now.
340 */
Sujith2660b812009-02-09 13:27:26 +0530341 if (sc->sc_ah->opmode == NL80211_IFTYPE_STATION)
Sujithd43f30152009-01-16 21:38:53 +0530342 needreset = 1;
Sujithe8324352009-01-16 21:38:42 +0530343 }
344 }
345
346 INIT_LIST_HEAD(&bf_pending);
347 INIT_LIST_HEAD(&bf_head);
348
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700349 nbad = ath_tx_num_badfrms(sc, bf, ts, txok);
Sujithe8324352009-01-16 21:38:42 +0530350 while (bf) {
351 txfail = txpending = 0;
352 bf_next = bf->bf_next;
353
354 if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
355 /* transmit completion, subframe is
356 * acked by block ack */
Vasanthakumar Thiagarajan0934af22009-03-18 20:22:00 +0530357 acked_cnt++;
Sujithe8324352009-01-16 21:38:42 +0530358 } else if (!isaggr && txok) {
359 /* transmit completion */
Vasanthakumar Thiagarajan0934af22009-03-18 20:22:00 +0530360 acked_cnt++;
Sujithe8324352009-01-16 21:38:42 +0530361 } else {
Sujithe8324352009-01-16 21:38:42 +0530362 if (!(tid->state & AGGR_CLEANUP) &&
Vasanthakumar Thiagarajan6d913f72010-04-15 17:38:46 -0400363 !bf_last->bf_tx_aborted) {
Sujithe8324352009-01-16 21:38:42 +0530364 if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
Sujithfec247c2009-07-27 12:08:16 +0530365 ath_tx_set_retry(sc, txq, bf);
Sujithe8324352009-01-16 21:38:42 +0530366 txpending = 1;
367 } else {
368 bf->bf_state.bf_type |= BUF_XRETRY;
369 txfail = 1;
370 sendbar = 1;
Vasanthakumar Thiagarajan0934af22009-03-18 20:22:00 +0530371 txfail_cnt++;
Sujithe8324352009-01-16 21:38:42 +0530372 }
373 } else {
374 /*
375 * cleanup in progress, just fail
376 * the un-acked sub-frames
377 */
378 txfail = 1;
379 }
380 }
381
382 if (bf_next == NULL) {
Vasanthakumar Thiagarajancbfe89c2009-06-24 18:58:47 +0530383 /*
384 * Make sure the last desc is reclaimed if it
385 * not a holding desc.
386 */
387 if (!bf_last->bf_stale)
388 list_move_tail(&bf->list, &bf_head);
389 else
390 INIT_LIST_HEAD(&bf_head);
Sujithe8324352009-01-16 21:38:42 +0530391 } else {
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -0700392 BUG_ON(list_empty(bf_q));
Sujithd43f30152009-01-16 21:38:53 +0530393 list_move_tail(&bf->list, &bf_head);
Sujithe8324352009-01-16 21:38:42 +0530394 }
395
396 if (!txpending) {
397 /*
398 * complete the acked-ones/xretried ones; update
399 * block-ack window
400 */
401 spin_lock_bh(&txq->axq_lock);
402 ath_tx_update_baw(sc, tid, bf->bf_seqno);
403 spin_unlock_bh(&txq->axq_lock);
404
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +0530405 if (rc_update && (acked_cnt == 1 || txfail_cnt == 1)) {
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700406 ath_tx_rc_status(bf, ts, nbad, txok, true);
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +0530407 rc_update = false;
408 } else {
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700409 ath_tx_rc_status(bf, ts, nbad, txok, false);
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +0530410 }
411
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700412 ath_tx_complete_buf(sc, bf, txq, &bf_head, ts,
413 !txfail, sendbar);
Sujithe8324352009-01-16 21:38:42 +0530414 } else {
Sujithd43f30152009-01-16 21:38:53 +0530415 /* retry the un-acked ones */
Sujitha119cc42009-03-30 15:28:38 +0530416 if (bf->bf_next == NULL && bf_last->bf_stale) {
Sujithe8324352009-01-16 21:38:42 +0530417 struct ath_buf *tbf;
418
Sujithd43f30152009-01-16 21:38:53 +0530419 tbf = ath_clone_txbuf(sc, bf_last);
Vasanthakumar Thiagarajanc41d92d2009-07-14 20:17:11 -0400420 /*
421 * Update tx baw and complete the frame with
422 * failed status if we run out of tx buf
423 */
424 if (!tbf) {
425 spin_lock_bh(&txq->axq_lock);
426 ath_tx_update_baw(sc, tid,
427 bf->bf_seqno);
428 spin_unlock_bh(&txq->axq_lock);
429
430 bf->bf_state.bf_type |= BUF_XRETRY;
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700431 ath_tx_rc_status(bf, ts, nbad,
Vasanthakumar Thiagarajanc41d92d2009-07-14 20:17:11 -0400432 0, false);
Sujithfec247c2009-07-27 12:08:16 +0530433 ath_tx_complete_buf(sc, bf, txq,
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700434 &bf_head, ts, 0, 0);
Vasanthakumar Thiagarajan8a460972009-06-10 17:50:09 +0530435 break;
Vasanthakumar Thiagarajanc41d92d2009-07-14 20:17:11 -0400436 }
437
Sujithd43f30152009-01-16 21:38:53 +0530438 ath9k_hw_cleartxdesc(sc->sc_ah, tbf->bf_desc);
Sujithe8324352009-01-16 21:38:42 +0530439 list_add_tail(&tbf->list, &bf_head);
440 } else {
441 /*
442 * Clear descriptor status words for
443 * software retry
444 */
Sujithd43f30152009-01-16 21:38:53 +0530445 ath9k_hw_cleartxdesc(sc->sc_ah, bf->bf_desc);
Sujithe8324352009-01-16 21:38:42 +0530446 }
447
448 /*
449 * Put this buffer to the temporary pending
450 * queue to retain ordering
451 */
452 list_splice_tail_init(&bf_head, &bf_pending);
453 }
454
455 bf = bf_next;
456 }
457
458 if (tid->state & AGGR_CLEANUP) {
Sujithe8324352009-01-16 21:38:42 +0530459 if (tid->baw_head == tid->baw_tail) {
460 tid->state &= ~AGGR_ADDBA_COMPLETE;
Sujithe8324352009-01-16 21:38:42 +0530461 tid->state &= ~AGGR_CLEANUP;
462
463 /* send buffered frames as singles */
464 ath_tx_flush_tid(sc, tid);
Sujithd43f30152009-01-16 21:38:53 +0530465 }
Sujith1286ec62009-01-27 13:30:37 +0530466 rcu_read_unlock();
Sujithe8324352009-01-16 21:38:42 +0530467 return;
468 }
469
Sujithd43f30152009-01-16 21:38:53 +0530470 /* prepend un-acked frames to the beginning of the pending frame queue */
Sujithe8324352009-01-16 21:38:42 +0530471 if (!list_empty(&bf_pending)) {
472 spin_lock_bh(&txq->axq_lock);
473 list_splice(&bf_pending, &tid->buf_q);
474 ath_tx_queue_tid(txq, tid);
475 spin_unlock_bh(&txq->axq_lock);
476 }
477
Sujith1286ec62009-01-27 13:30:37 +0530478 rcu_read_unlock();
479
Sujithe8324352009-01-16 21:38:42 +0530480 if (needreset)
481 ath_reset(sc, false);
Sujithe8324352009-01-16 21:38:42 +0530482}
483
484static u32 ath_lookup_rate(struct ath_softc *sc, struct ath_buf *bf,
485 struct ath_atx_tid *tid)
486{
Sujithe8324352009-01-16 21:38:42 +0530487 struct sk_buff *skb;
488 struct ieee80211_tx_info *tx_info;
489 struct ieee80211_tx_rate *rates;
Sujithd43f30152009-01-16 21:38:53 +0530490 u32 max_4ms_framelen, frmlen;
Sujith4ef70842009-07-23 15:32:41 +0530491 u16 aggr_limit, legacy = 0;
Sujithe8324352009-01-16 21:38:42 +0530492 int i;
493
Sujitha22be222009-03-30 15:28:36 +0530494 skb = bf->bf_mpdu;
Sujithe8324352009-01-16 21:38:42 +0530495 tx_info = IEEE80211_SKB_CB(skb);
496 rates = tx_info->control.rates;
Sujithe8324352009-01-16 21:38:42 +0530497
498 /*
499 * Find the lowest frame length among the rate series that will have a
500 * 4ms transmit duration.
501 * TODO - TXOP limit needs to be considered.
502 */
503 max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
504
505 for (i = 0; i < 4; i++) {
506 if (rates[i].count) {
Felix Fietkau545750d2009-11-23 22:21:01 +0100507 int modeidx;
508 if (!(rates[i].flags & IEEE80211_TX_RC_MCS)) {
Sujithe8324352009-01-16 21:38:42 +0530509 legacy = 1;
510 break;
511 }
512
Felix Fietkau545750d2009-11-23 22:21:01 +0100513 if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
514 modeidx = MCS_HT40_SGI;
515 else if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
516 modeidx = MCS_HT40;
517 else
518 modeidx = MCS_DEFAULT;
519
520 frmlen = ath_max_4ms_framelen[modeidx][rates[i].idx];
Sujithd43f30152009-01-16 21:38:53 +0530521 max_4ms_framelen = min(max_4ms_framelen, frmlen);
Sujithe8324352009-01-16 21:38:42 +0530522 }
523 }
524
525 /*
526 * limit aggregate size by the minimum rate if rate selected is
527 * not a probe rate, if rate selected is a probe rate then
528 * avoid aggregation of this packet.
529 */
530 if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
531 return 0;
532
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530533 if (sc->sc_flags & SC_OP_BT_PRIORITY_DETECTED)
534 aggr_limit = min((max_4ms_framelen * 3) / 8,
535 (u32)ATH_AMPDU_LIMIT_MAX);
536 else
537 aggr_limit = min(max_4ms_framelen,
538 (u32)ATH_AMPDU_LIMIT_MAX);
Sujithe8324352009-01-16 21:38:42 +0530539
540 /*
541 * h/w can accept aggregates upto 16 bit lengths (65535).
542 * The IE, however can hold upto 65536, which shows up here
543 * as zero. Ignore 65536 since we are constrained by hw.
544 */
Sujith4ef70842009-07-23 15:32:41 +0530545 if (tid->an->maxampdu)
546 aggr_limit = min(aggr_limit, tid->an->maxampdu);
Sujithe8324352009-01-16 21:38:42 +0530547
548 return aggr_limit;
549}
550
551/*
Sujithd43f30152009-01-16 21:38:53 +0530552 * Returns the number of delimiters to be added to
Sujithe8324352009-01-16 21:38:42 +0530553 * meet the minimum required mpdudensity.
Sujithe8324352009-01-16 21:38:42 +0530554 */
555static int ath_compute_num_delims(struct ath_softc *sc, struct ath_atx_tid *tid,
556 struct ath_buf *bf, u16 frmlen)
557{
Sujithe8324352009-01-16 21:38:42 +0530558 struct sk_buff *skb = bf->bf_mpdu;
559 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Sujith4ef70842009-07-23 15:32:41 +0530560 u32 nsymbits, nsymbols;
Sujithe8324352009-01-16 21:38:42 +0530561 u16 minlen;
Felix Fietkau545750d2009-11-23 22:21:01 +0100562 u8 flags, rix;
Sujithe8324352009-01-16 21:38:42 +0530563 int width, half_gi, ndelim, mindelim;
564
565 /* Select standard number of delimiters based on frame length alone */
566 ndelim = ATH_AGGR_GET_NDELIM(frmlen);
567
568 /*
569 * If encryption enabled, hardware requires some more padding between
570 * subframes.
571 * TODO - this could be improved to be dependent on the rate.
572 * The hardware can keep up at lower rates, but not higher rates
573 */
574 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
575 ndelim += ATH_AGGR_ENCRYPTDELIM;
576
577 /*
578 * Convert desired mpdu density from microeconds to bytes based
579 * on highest rate in rate series (i.e. first rate) to determine
580 * required minimum length for subframe. Take into account
581 * whether high rate is 20 or 40Mhz and half or full GI.
Sujith4ef70842009-07-23 15:32:41 +0530582 *
Sujithe8324352009-01-16 21:38:42 +0530583 * If there is no mpdu density restriction, no further calculation
584 * is needed.
585 */
Sujith4ef70842009-07-23 15:32:41 +0530586
587 if (tid->an->mpdudensity == 0)
Sujithe8324352009-01-16 21:38:42 +0530588 return ndelim;
589
590 rix = tx_info->control.rates[0].idx;
591 flags = tx_info->control.rates[0].flags;
Sujithe8324352009-01-16 21:38:42 +0530592 width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
593 half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
594
595 if (half_gi)
Sujith4ef70842009-07-23 15:32:41 +0530596 nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(tid->an->mpdudensity);
Sujithe8324352009-01-16 21:38:42 +0530597 else
Sujith4ef70842009-07-23 15:32:41 +0530598 nsymbols = NUM_SYMBOLS_PER_USEC(tid->an->mpdudensity);
Sujithe8324352009-01-16 21:38:42 +0530599
600 if (nsymbols == 0)
601 nsymbols = 1;
602
Felix Fietkau545750d2009-11-23 22:21:01 +0100603 nsymbits = bits_per_symbol[rix][width];
Sujithe8324352009-01-16 21:38:42 +0530604 minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
605
Sujithe8324352009-01-16 21:38:42 +0530606 if (frmlen < minlen) {
Sujithe8324352009-01-16 21:38:42 +0530607 mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
608 ndelim = max(mindelim, ndelim);
609 }
610
611 return ndelim;
612}
613
614static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
Sujithfec247c2009-07-27 12:08:16 +0530615 struct ath_txq *txq,
Sujithd43f30152009-01-16 21:38:53 +0530616 struct ath_atx_tid *tid,
617 struct list_head *bf_q)
Sujithe8324352009-01-16 21:38:42 +0530618{
619#define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
Sujithd43f30152009-01-16 21:38:53 +0530620 struct ath_buf *bf, *bf_first, *bf_prev = NULL;
621 int rl = 0, nframes = 0, ndelim, prev_al = 0;
Sujithe8324352009-01-16 21:38:42 +0530622 u16 aggr_limit = 0, al = 0, bpad = 0,
623 al_delta, h_baw = tid->baw_size / 2;
624 enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
Sujithe8324352009-01-16 21:38:42 +0530625
626 bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
627
628 do {
629 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
630
Sujithd43f30152009-01-16 21:38:53 +0530631 /* do not step over block-ack window */
Sujithe8324352009-01-16 21:38:42 +0530632 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
633 status = ATH_AGGR_BAW_CLOSED;
634 break;
635 }
636
637 if (!rl) {
638 aggr_limit = ath_lookup_rate(sc, bf, tid);
639 rl = 1;
640 }
641
Sujithd43f30152009-01-16 21:38:53 +0530642 /* do not exceed aggregation limit */
Sujithe8324352009-01-16 21:38:42 +0530643 al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
644
Sujithd43f30152009-01-16 21:38:53 +0530645 if (nframes &&
646 (aggr_limit < (al + bpad + al_delta + prev_al))) {
Sujithe8324352009-01-16 21:38:42 +0530647 status = ATH_AGGR_LIMITED;
648 break;
649 }
650
Sujithd43f30152009-01-16 21:38:53 +0530651 /* do not exceed subframe limit */
652 if (nframes >= min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
Sujithe8324352009-01-16 21:38:42 +0530653 status = ATH_AGGR_LIMITED;
654 break;
655 }
Sujithd43f30152009-01-16 21:38:53 +0530656 nframes++;
Sujithe8324352009-01-16 21:38:42 +0530657
Sujithd43f30152009-01-16 21:38:53 +0530658 /* add padding for previous frame to aggregation length */
Sujithe8324352009-01-16 21:38:42 +0530659 al += bpad + al_delta;
660
661 /*
662 * Get the delimiters needed to meet the MPDU
663 * density for this node.
664 */
665 ndelim = ath_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
Sujithe8324352009-01-16 21:38:42 +0530666 bpad = PADBYTES(al_delta) + (ndelim << 2);
667
668 bf->bf_next = NULL;
Vasanthakumar Thiagarajan87d5efb2010-04-15 17:38:43 -0400669 ath9k_hw_set_desc_link(sc->sc_ah, bf->bf_desc, 0);
Sujithe8324352009-01-16 21:38:42 +0530670
Sujithd43f30152009-01-16 21:38:53 +0530671 /* link buffers of this frame to the aggregate */
Sujithe8324352009-01-16 21:38:42 +0530672 ath_tx_addto_baw(sc, tid, bf);
Sujithd43f30152009-01-16 21:38:53 +0530673 ath9k_hw_set11n_aggr_middle(sc->sc_ah, bf->bf_desc, ndelim);
674 list_move_tail(&bf->list, bf_q);
Sujithe8324352009-01-16 21:38:42 +0530675 if (bf_prev) {
676 bf_prev->bf_next = bf;
Vasanthakumar Thiagarajan87d5efb2010-04-15 17:38:43 -0400677 ath9k_hw_set_desc_link(sc->sc_ah, bf_prev->bf_desc,
678 bf->bf_daddr);
Sujithe8324352009-01-16 21:38:42 +0530679 }
680 bf_prev = bf;
Sujithfec247c2009-07-27 12:08:16 +0530681
Sujithe8324352009-01-16 21:38:42 +0530682 } while (!list_empty(&tid->buf_q));
683
684 bf_first->bf_al = al;
685 bf_first->bf_nframes = nframes;
Sujithd43f30152009-01-16 21:38:53 +0530686
Sujithe8324352009-01-16 21:38:42 +0530687 return status;
688#undef PADBYTES
689}
690
691static void ath_tx_sched_aggr(struct ath_softc *sc, struct ath_txq *txq,
692 struct ath_atx_tid *tid)
693{
Sujithd43f30152009-01-16 21:38:53 +0530694 struct ath_buf *bf;
Sujithe8324352009-01-16 21:38:42 +0530695 enum ATH_AGGR_STATUS status;
696 struct list_head bf_q;
Sujithe8324352009-01-16 21:38:42 +0530697
698 do {
699 if (list_empty(&tid->buf_q))
700 return;
701
702 INIT_LIST_HEAD(&bf_q);
703
Sujithfec247c2009-07-27 12:08:16 +0530704 status = ath_tx_form_aggr(sc, txq, tid, &bf_q);
Sujithe8324352009-01-16 21:38:42 +0530705
706 /*
Sujithd43f30152009-01-16 21:38:53 +0530707 * no frames picked up to be aggregated;
708 * block-ack window is not open.
Sujithe8324352009-01-16 21:38:42 +0530709 */
710 if (list_empty(&bf_q))
711 break;
712
713 bf = list_first_entry(&bf_q, struct ath_buf, list);
Sujithd43f30152009-01-16 21:38:53 +0530714 bf->bf_lastbf = list_entry(bf_q.prev, struct ath_buf, list);
Sujithe8324352009-01-16 21:38:42 +0530715
Sujithd43f30152009-01-16 21:38:53 +0530716 /* if only one frame, send as non-aggregate */
Sujithe8324352009-01-16 21:38:42 +0530717 if (bf->bf_nframes == 1) {
Sujithe8324352009-01-16 21:38:42 +0530718 bf->bf_state.bf_type &= ~BUF_AGGR;
Sujithd43f30152009-01-16 21:38:53 +0530719 ath9k_hw_clr11n_aggr(sc->sc_ah, bf->bf_desc);
Sujithe8324352009-01-16 21:38:42 +0530720 ath_buf_set_rate(sc, bf);
721 ath_tx_txqaddbuf(sc, txq, &bf_q);
722 continue;
723 }
724
Sujithd43f30152009-01-16 21:38:53 +0530725 /* setup first desc of aggregate */
Sujithe8324352009-01-16 21:38:42 +0530726 bf->bf_state.bf_type |= BUF_AGGR;
727 ath_buf_set_rate(sc, bf);
728 ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
729
Sujithd43f30152009-01-16 21:38:53 +0530730 /* anchor last desc of aggregate */
731 ath9k_hw_set11n_aggr_last(sc->sc_ah, bf->bf_lastbf->bf_desc);
Sujithe8324352009-01-16 21:38:42 +0530732
Sujithe8324352009-01-16 21:38:42 +0530733 ath_tx_txqaddbuf(sc, txq, &bf_q);
Sujithfec247c2009-07-27 12:08:16 +0530734 TX_STAT_INC(txq->axq_qnum, a_aggr);
Sujithe8324352009-01-16 21:38:42 +0530735
736 } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
737 status != ATH_AGGR_BAW_CLOSED);
738}
739
Sujithf83da962009-07-23 15:32:37 +0530740void ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
741 u16 tid, u16 *ssn)
Sujithe8324352009-01-16 21:38:42 +0530742{
743 struct ath_atx_tid *txtid;
744 struct ath_node *an;
745
746 an = (struct ath_node *)sta->drv_priv;
Sujithf83da962009-07-23 15:32:37 +0530747 txtid = ATH_AN_2_TID(an, tid);
748 txtid->state |= AGGR_ADDBA_PROGRESS;
749 ath_tx_pause_tid(sc, txtid);
750 *ssn = txtid->seq_start;
Sujithe8324352009-01-16 21:38:42 +0530751}
752
Sujithf83da962009-07-23 15:32:37 +0530753void ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
Sujithe8324352009-01-16 21:38:42 +0530754{
755 struct ath_node *an = (struct ath_node *)sta->drv_priv;
756 struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
757 struct ath_txq *txq = &sc->tx.txq[txtid->ac->qnum];
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700758 struct ath_tx_status ts;
Sujithe8324352009-01-16 21:38:42 +0530759 struct ath_buf *bf;
760 struct list_head bf_head;
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700761
762 memset(&ts, 0, sizeof(ts));
Sujithe8324352009-01-16 21:38:42 +0530763 INIT_LIST_HEAD(&bf_head);
764
765 if (txtid->state & AGGR_CLEANUP)
Sujithf83da962009-07-23 15:32:37 +0530766 return;
Sujithe8324352009-01-16 21:38:42 +0530767
768 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
Vasanthakumar Thiagarajan5eae6592009-06-09 15:28:21 +0530769 txtid->state &= ~AGGR_ADDBA_PROGRESS;
Sujithf83da962009-07-23 15:32:37 +0530770 return;
Sujithe8324352009-01-16 21:38:42 +0530771 }
772
773 ath_tx_pause_tid(sc, txtid);
774
775 /* drop all software retried frames and mark this TID */
776 spin_lock_bh(&txq->axq_lock);
777 while (!list_empty(&txtid->buf_q)) {
778 bf = list_first_entry(&txtid->buf_q, struct ath_buf, list);
779 if (!bf_isretried(bf)) {
780 /*
781 * NB: it's based on the assumption that
782 * software retried frame will always stay
783 * at the head of software queue.
784 */
785 break;
786 }
Sujithd43f30152009-01-16 21:38:53 +0530787 list_move_tail(&bf->list, &bf_head);
Sujithe8324352009-01-16 21:38:42 +0530788 ath_tx_update_baw(sc, txtid, bf->bf_seqno);
Felix Fietkaudb1a0522010-03-29 20:07:11 -0700789 ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0, 0);
Sujithe8324352009-01-16 21:38:42 +0530790 }
Sujithd43f30152009-01-16 21:38:53 +0530791 spin_unlock_bh(&txq->axq_lock);
Sujithe8324352009-01-16 21:38:42 +0530792
793 if (txtid->baw_head != txtid->baw_tail) {
Sujithe8324352009-01-16 21:38:42 +0530794 txtid->state |= AGGR_CLEANUP;
795 } else {
796 txtid->state &= ~AGGR_ADDBA_COMPLETE;
Sujithe8324352009-01-16 21:38:42 +0530797 ath_tx_flush_tid(sc, txtid);
798 }
Sujithe8324352009-01-16 21:38:42 +0530799}
800
801void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
802{
803 struct ath_atx_tid *txtid;
804 struct ath_node *an;
805
806 an = (struct ath_node *)sta->drv_priv;
807
808 if (sc->sc_flags & SC_OP_TXAGGR) {
809 txtid = ATH_AN_2_TID(an, tid);
810 txtid->baw_size =
811 IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
812 txtid->state |= AGGR_ADDBA_COMPLETE;
813 txtid->state &= ~AGGR_ADDBA_PROGRESS;
814 ath_tx_resume_tid(sc, txtid);
815 }
816}
817
818bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an, u8 tidno)
819{
820 struct ath_atx_tid *txtid;
821
822 if (!(sc->sc_flags & SC_OP_TXAGGR))
823 return false;
824
825 txtid = ATH_AN_2_TID(an, tidno);
826
Vasanthakumar Thiagarajanc3d8f022009-06-10 17:50:08 +0530827 if (!(txtid->state & (AGGR_ADDBA_COMPLETE | AGGR_ADDBA_PROGRESS)))
Sujithe8324352009-01-16 21:38:42 +0530828 return true;
Sujithe8324352009-01-16 21:38:42 +0530829 return false;
830}
831
832/********************/
833/* Queue Management */
834/********************/
835
Sujithe8324352009-01-16 21:38:42 +0530836static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
837 struct ath_txq *txq)
838{
839 struct ath_atx_ac *ac, *ac_tmp;
840 struct ath_atx_tid *tid, *tid_tmp;
841
842 list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
843 list_del(&ac->list);
844 ac->sched = false;
845 list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
846 list_del(&tid->list);
847 tid->sched = false;
848 ath_tid_drain(sc, txq, tid);
849 }
850 }
851}
852
853struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
854{
Sujithcbe61d82009-02-09 13:27:12 +0530855 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700856 struct ath_common *common = ath9k_hw_common(ah);
Sujithe8324352009-01-16 21:38:42 +0530857 struct ath9k_tx_queue_info qi;
858 int qnum;
859
860 memset(&qi, 0, sizeof(qi));
861 qi.tqi_subtype = subtype;
862 qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
863 qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
864 qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
865 qi.tqi_physCompBuf = 0;
866
867 /*
868 * Enable interrupts only for EOL and DESC conditions.
869 * We mark tx descriptors to receive a DESC interrupt
870 * when a tx queue gets deep; otherwise waiting for the
871 * EOL to reap descriptors. Note that this is done to
872 * reduce interrupt load and this only defers reaping
873 * descriptors, never transmitting frames. Aside from
874 * reducing interrupts this also permits more concurrency.
875 * The only potential downside is if the tx queue backs
876 * up in which case the top half of the kernel may backup
877 * due to a lack of tx descriptors.
878 *
879 * The UAPSD queue is an exception, since we take a desc-
880 * based intr on the EOSP frames.
881 */
882 if (qtype == ATH9K_TX_QUEUE_UAPSD)
883 qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
884 else
885 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
886 TXQ_FLAG_TXDESCINT_ENABLE;
887 qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
888 if (qnum == -1) {
889 /*
890 * NB: don't print a message, this happens
891 * normally on parts with too few tx queues
892 */
893 return NULL;
894 }
895 if (qnum >= ARRAY_SIZE(sc->tx.txq)) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700896 ath_print(common, ATH_DBG_FATAL,
897 "qnum %u out of range, max %u!\n",
898 qnum, (unsigned int)ARRAY_SIZE(sc->tx.txq));
Sujithe8324352009-01-16 21:38:42 +0530899 ath9k_hw_releasetxqueue(ah, qnum);
900 return NULL;
901 }
902 if (!ATH_TXQ_SETUP(sc, qnum)) {
903 struct ath_txq *txq = &sc->tx.txq[qnum];
904
905 txq->axq_qnum = qnum;
906 txq->axq_link = NULL;
907 INIT_LIST_HEAD(&txq->axq_q);
908 INIT_LIST_HEAD(&txq->axq_acq);
909 spin_lock_init(&txq->axq_lock);
910 txq->axq_depth = 0;
Senthil Balasubramanian164ace32009-07-14 20:17:09 -0400911 txq->axq_tx_inprogress = false;
Sujithe8324352009-01-16 21:38:42 +0530912 sc->tx.txqsetup |= 1<<qnum;
913 }
914 return &sc->tx.txq[qnum];
915}
916
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530917int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
Sujithe8324352009-01-16 21:38:42 +0530918{
919 int qnum;
920
921 switch (qtype) {
922 case ATH9K_TX_QUEUE_DATA:
923 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700924 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
925 "HAL AC %u out of range, max %zu!\n",
926 haltype, ARRAY_SIZE(sc->tx.hwq_map));
Sujithe8324352009-01-16 21:38:42 +0530927 return -1;
928 }
929 qnum = sc->tx.hwq_map[haltype];
930 break;
931 case ATH9K_TX_QUEUE_BEACON:
932 qnum = sc->beacon.beaconq;
933 break;
934 case ATH9K_TX_QUEUE_CAB:
935 qnum = sc->beacon.cabq->axq_qnum;
936 break;
937 default:
938 qnum = -1;
939 }
940 return qnum;
941}
942
943struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
944{
945 struct ath_txq *txq = NULL;
Luis R. Rodriguezf52de032009-11-02 17:09:12 -0800946 u16 skb_queue = skb_get_queue_mapping(skb);
Sujithe8324352009-01-16 21:38:42 +0530947 int qnum;
948
Luis R. Rodriguezf52de032009-11-02 17:09:12 -0800949 qnum = ath_get_hal_qnum(skb_queue, sc);
Sujithe8324352009-01-16 21:38:42 +0530950 txq = &sc->tx.txq[qnum];
951
952 spin_lock_bh(&txq->axq_lock);
953
954 if (txq->axq_depth >= (ATH_TXBUF - 20)) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700955 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_XMIT,
956 "TX queue: %d is full, depth: %d\n",
957 qnum, txq->axq_depth);
Luis R. Rodriguezf52de032009-11-02 17:09:12 -0800958 ath_mac80211_stop_queue(sc, skb_queue);
Sujithe8324352009-01-16 21:38:42 +0530959 txq->stopped = 1;
960 spin_unlock_bh(&txq->axq_lock);
961 return NULL;
962 }
963
964 spin_unlock_bh(&txq->axq_lock);
965
966 return txq;
967}
968
969int ath_txq_update(struct ath_softc *sc, int qnum,
970 struct ath9k_tx_queue_info *qinfo)
971{
Sujithcbe61d82009-02-09 13:27:12 +0530972 struct ath_hw *ah = sc->sc_ah;
Sujithe8324352009-01-16 21:38:42 +0530973 int error = 0;
974 struct ath9k_tx_queue_info qi;
975
976 if (qnum == sc->beacon.beaconq) {
977 /*
978 * XXX: for beacon queue, we just save the parameter.
979 * It will be picked up by ath_beaconq_config when
980 * it's necessary.
981 */
982 sc->beacon.beacon_qi = *qinfo;
983 return 0;
984 }
985
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -0700986 BUG_ON(sc->tx.txq[qnum].axq_qnum != qnum);
Sujithe8324352009-01-16 21:38:42 +0530987
988 ath9k_hw_get_txq_props(ah, qnum, &qi);
989 qi.tqi_aifs = qinfo->tqi_aifs;
990 qi.tqi_cwmin = qinfo->tqi_cwmin;
991 qi.tqi_cwmax = qinfo->tqi_cwmax;
992 qi.tqi_burstTime = qinfo->tqi_burstTime;
993 qi.tqi_readyTime = qinfo->tqi_readyTime;
994
995 if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700996 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
997 "Unable to update hardware queue %u!\n", qnum);
Sujithe8324352009-01-16 21:38:42 +0530998 error = -EIO;
999 } else {
1000 ath9k_hw_resettxqueue(ah, qnum);
1001 }
1002
1003 return error;
1004}
1005
1006int ath_cabq_update(struct ath_softc *sc)
1007{
1008 struct ath9k_tx_queue_info qi;
1009 int qnum = sc->beacon.cabq->axq_qnum;
Sujithe8324352009-01-16 21:38:42 +05301010
1011 ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
1012 /*
1013 * Ensure the readytime % is within the bounds.
1014 */
Sujith17d79042009-02-09 13:27:03 +05301015 if (sc->config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
1016 sc->config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
1017 else if (sc->config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
1018 sc->config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
Sujithe8324352009-01-16 21:38:42 +05301019
Johannes Berg57c4d7b2009-04-23 16:10:04 +02001020 qi.tqi_readyTime = (sc->beacon_interval *
Sujithfdbf7332009-02-17 15:36:35 +05301021 sc->config.cabqReadytime) / 100;
Sujithe8324352009-01-16 21:38:42 +05301022 ath_txq_update(sc, qnum, &qi);
1023
1024 return 0;
1025}
1026
Sujith043a0402009-01-16 21:38:47 +05301027/*
1028 * Drain a given TX queue (could be Beacon or Data)
1029 *
1030 * This assumes output has been stopped and
1031 * we do not need to block ath_tx_tasklet.
1032 */
1033void ath_draintxq(struct ath_softc *sc, struct ath_txq *txq, bool retry_tx)
Sujithe8324352009-01-16 21:38:42 +05301034{
1035 struct ath_buf *bf, *lastbf;
1036 struct list_head bf_head;
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001037 struct ath_tx_status ts;
1038
1039 memset(&ts, 0, sizeof(ts));
Sujithe8324352009-01-16 21:38:42 +05301040 INIT_LIST_HEAD(&bf_head);
1041
Sujithe8324352009-01-16 21:38:42 +05301042 for (;;) {
1043 spin_lock_bh(&txq->axq_lock);
1044
1045 if (list_empty(&txq->axq_q)) {
1046 txq->axq_link = NULL;
Sujithe8324352009-01-16 21:38:42 +05301047 spin_unlock_bh(&txq->axq_lock);
1048 break;
1049 }
1050
1051 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
1052
Sujitha119cc42009-03-30 15:28:38 +05301053 if (bf->bf_stale) {
Sujithe8324352009-01-16 21:38:42 +05301054 list_del(&bf->list);
1055 spin_unlock_bh(&txq->axq_lock);
1056
1057 spin_lock_bh(&sc->tx.txbuflock);
1058 list_add_tail(&bf->list, &sc->tx.txbuf);
1059 spin_unlock_bh(&sc->tx.txbuflock);
1060 continue;
1061 }
1062
1063 lastbf = bf->bf_lastbf;
Vasanthakumar Thiagarajan6d913f72010-04-15 17:38:46 -04001064 if (!retry_tx)
1065 lastbf->bf_tx_aborted = true;
Sujithe8324352009-01-16 21:38:42 +05301066
1067 /* remove ath_buf's of the same mpdu from txq */
1068 list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
1069 txq->axq_depth--;
1070
1071 spin_unlock_bh(&txq->axq_lock);
1072
1073 if (bf_isampdu(bf))
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001074 ath_tx_complete_aggr(sc, txq, bf, &bf_head, &ts, 0);
Sujithe8324352009-01-16 21:38:42 +05301075 else
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001076 ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0, 0);
Sujithe8324352009-01-16 21:38:42 +05301077 }
1078
Senthil Balasubramanian164ace32009-07-14 20:17:09 -04001079 spin_lock_bh(&txq->axq_lock);
1080 txq->axq_tx_inprogress = false;
1081 spin_unlock_bh(&txq->axq_lock);
1082
Sujithe8324352009-01-16 21:38:42 +05301083 /* flush any pending frames if aggregation is enabled */
1084 if (sc->sc_flags & SC_OP_TXAGGR) {
1085 if (!retry_tx) {
1086 spin_lock_bh(&txq->axq_lock);
1087 ath_txq_drain_pending_buffers(sc, txq);
1088 spin_unlock_bh(&txq->axq_lock);
1089 }
1090 }
1091}
1092
Sujith043a0402009-01-16 21:38:47 +05301093void ath_drain_all_txq(struct ath_softc *sc, bool retry_tx)
1094{
Sujithcbe61d82009-02-09 13:27:12 +05301095 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001096 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith043a0402009-01-16 21:38:47 +05301097 struct ath_txq *txq;
1098 int i, npend = 0;
1099
1100 if (sc->sc_flags & SC_OP_INVALID)
1101 return;
1102
1103 /* Stop beacon queue */
1104 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
1105
1106 /* Stop data queues */
1107 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1108 if (ATH_TXQ_SETUP(sc, i)) {
1109 txq = &sc->tx.txq[i];
1110 ath9k_hw_stoptxdma(ah, txq->axq_qnum);
1111 npend += ath9k_hw_numtxpending(ah, txq->axq_qnum);
1112 }
1113 }
1114
1115 if (npend) {
1116 int r;
1117
Sujithe8009e92009-12-14 14:57:08 +05301118 ath_print(common, ATH_DBG_FATAL,
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001119 "Unable to stop TxDMA. Reset HAL!\n");
Sujith043a0402009-01-16 21:38:47 +05301120
1121 spin_lock_bh(&sc->sc_resetlock);
Sujithe8009e92009-12-14 14:57:08 +05301122 r = ath9k_hw_reset(ah, sc->sc_ah->curchan, false);
Sujith043a0402009-01-16 21:38:47 +05301123 if (r)
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001124 ath_print(common, ATH_DBG_FATAL,
1125 "Unable to reset hardware; reset status %d\n",
1126 r);
Sujith043a0402009-01-16 21:38:47 +05301127 spin_unlock_bh(&sc->sc_resetlock);
1128 }
1129
1130 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1131 if (ATH_TXQ_SETUP(sc, i))
1132 ath_draintxq(sc, &sc->tx.txq[i], retry_tx);
1133 }
1134}
1135
Sujithe8324352009-01-16 21:38:42 +05301136void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1137{
1138 ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
1139 sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
1140}
1141
Sujithe8324352009-01-16 21:38:42 +05301142void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
1143{
1144 struct ath_atx_ac *ac;
1145 struct ath_atx_tid *tid;
1146
1147 if (list_empty(&txq->axq_acq))
1148 return;
1149
1150 ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
1151 list_del(&ac->list);
1152 ac->sched = false;
1153
1154 do {
1155 if (list_empty(&ac->tid_q))
1156 return;
1157
1158 tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
1159 list_del(&tid->list);
1160 tid->sched = false;
1161
1162 if (tid->paused)
1163 continue;
1164
Senthil Balasubramanian164ace32009-07-14 20:17:09 -04001165 ath_tx_sched_aggr(sc, txq, tid);
Sujithe8324352009-01-16 21:38:42 +05301166
1167 /*
1168 * add tid to round-robin queue if more frames
1169 * are pending for the tid
1170 */
1171 if (!list_empty(&tid->buf_q))
1172 ath_tx_queue_tid(txq, tid);
1173
1174 break;
1175 } while (!list_empty(&ac->tid_q));
1176
1177 if (!list_empty(&ac->tid_q)) {
1178 if (!ac->sched) {
1179 ac->sched = true;
1180 list_add_tail(&ac->list, &txq->axq_acq);
1181 }
1182 }
1183}
1184
1185int ath_tx_setup(struct ath_softc *sc, int haltype)
1186{
1187 struct ath_txq *txq;
1188
1189 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001190 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
1191 "HAL AC %u out of range, max %zu!\n",
Sujithe8324352009-01-16 21:38:42 +05301192 haltype, ARRAY_SIZE(sc->tx.hwq_map));
1193 return 0;
1194 }
1195 txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
1196 if (txq != NULL) {
1197 sc->tx.hwq_map[haltype] = txq->axq_qnum;
1198 return 1;
1199 } else
1200 return 0;
1201}
1202
1203/***********/
1204/* TX, DMA */
1205/***********/
1206
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001207/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001208 * Insert a chain of ath_buf (descriptors) on a txq and
1209 * assume the descriptors are already chained together by caller.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001210 */
Sujith102e0572008-10-29 10:15:16 +05301211static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
1212 struct list_head *head)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001213{
Sujithcbe61d82009-02-09 13:27:12 +05301214 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001215 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001216 struct ath_buf *bf;
Sujith102e0572008-10-29 10:15:16 +05301217
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001218 /*
1219 * Insert the frame on the outbound list and
1220 * pass it on to the hardware.
1221 */
1222
1223 if (list_empty(head))
1224 return;
1225
1226 bf = list_first_entry(head, struct ath_buf, list);
1227
1228 list_splice_tail_init(head, &txq->axq_q);
1229 txq->axq_depth++;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001230
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001231 ath_print(common, ATH_DBG_QUEUE,
1232 "qnum: %d, txq depth: %d\n", txq->axq_qnum, txq->axq_depth);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001233
1234 if (txq->axq_link == NULL) {
1235 ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001236 ath_print(common, ATH_DBG_XMIT,
1237 "TXDP[%u] = %llx (%p)\n",
1238 txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001239 } else {
1240 *txq->axq_link = bf->bf_daddr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001241 ath_print(common, ATH_DBG_XMIT, "link[%u] (%p)=%llx (%p)\n",
1242 txq->axq_qnum, txq->axq_link,
1243 ito64(bf->bf_daddr), bf->bf_desc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001244 }
Vasanthakumar Thiagarajan5c3a3382010-04-15 17:38:44 -04001245 ath9k_hw_get_desc_link(ah, bf->bf_lastbf->bf_desc, &txq->axq_link);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001246 ath9k_hw_txstart(ah, txq->axq_qnum);
1247}
1248
Sujithe8324352009-01-16 21:38:42 +05301249static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
Sujithc4288392008-11-18 09:09:30 +05301250{
Sujithe8324352009-01-16 21:38:42 +05301251 struct ath_buf *bf = NULL;
Sujithc4288392008-11-18 09:09:30 +05301252
Sujithe8324352009-01-16 21:38:42 +05301253 spin_lock_bh(&sc->tx.txbuflock);
Sujithc4288392008-11-18 09:09:30 +05301254
Sujithe8324352009-01-16 21:38:42 +05301255 if (unlikely(list_empty(&sc->tx.txbuf))) {
1256 spin_unlock_bh(&sc->tx.txbuflock);
1257 return NULL;
Sujithc4288392008-11-18 09:09:30 +05301258 }
1259
Sujithe8324352009-01-16 21:38:42 +05301260 bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
1261 list_del(&bf->list);
Sujithc4288392008-11-18 09:09:30 +05301262
Sujithe8324352009-01-16 21:38:42 +05301263 spin_unlock_bh(&sc->tx.txbuflock);
Sujithc4288392008-11-18 09:09:30 +05301264
Sujithe8324352009-01-16 21:38:42 +05301265 return bf;
1266}
Sujithc4288392008-11-18 09:09:30 +05301267
Sujithe8324352009-01-16 21:38:42 +05301268static void ath_tx_send_ampdu(struct ath_softc *sc, struct ath_atx_tid *tid,
1269 struct list_head *bf_head,
1270 struct ath_tx_control *txctl)
1271{
1272 struct ath_buf *bf;
1273
Sujithe8324352009-01-16 21:38:42 +05301274 bf = list_first_entry(bf_head, struct ath_buf, list);
1275 bf->bf_state.bf_type |= BUF_AMPDU;
Sujithfec247c2009-07-27 12:08:16 +05301276 TX_STAT_INC(txctl->txq->axq_qnum, a_queued);
Sujithe8324352009-01-16 21:38:42 +05301277
1278 /*
1279 * Do not queue to h/w when any of the following conditions is true:
1280 * - there are pending frames in software queue
1281 * - the TID is currently paused for ADDBA/BAR request
1282 * - seqno is not within block-ack window
1283 * - h/w queue depth exceeds low water mark
1284 */
1285 if (!list_empty(&tid->buf_q) || tid->paused ||
1286 !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
1287 txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
Jouni Malinenf7a276a2008-12-15 16:02:04 +02001288 /*
Sujithe8324352009-01-16 21:38:42 +05301289 * Add this frame to software queue for scheduling later
1290 * for aggregation.
Jouni Malinenf7a276a2008-12-15 16:02:04 +02001291 */
Sujithd43f30152009-01-16 21:38:53 +05301292 list_move_tail(&bf->list, &tid->buf_q);
Sujithe8324352009-01-16 21:38:42 +05301293 ath_tx_queue_tid(txctl->txq, tid);
1294 return;
Jouni Malinenf7a276a2008-12-15 16:02:04 +02001295 }
1296
Sujithe8324352009-01-16 21:38:42 +05301297 /* Add sub-frame to BAW */
1298 ath_tx_addto_baw(sc, tid, bf);
1299
1300 /* Queue to h/w without aggregation */
1301 bf->bf_nframes = 1;
Sujithd43f30152009-01-16 21:38:53 +05301302 bf->bf_lastbf = bf;
Sujithe8324352009-01-16 21:38:42 +05301303 ath_buf_set_rate(sc, bf);
1304 ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
Sujithc4288392008-11-18 09:09:30 +05301305}
1306
Sujithc37452b2009-03-09 09:31:57 +05301307static void ath_tx_send_ht_normal(struct ath_softc *sc, struct ath_txq *txq,
1308 struct ath_atx_tid *tid,
1309 struct list_head *bf_head)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001310{
Sujithe8324352009-01-16 21:38:42 +05301311 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001312
Sujithe8324352009-01-16 21:38:42 +05301313 bf = list_first_entry(bf_head, struct ath_buf, list);
1314 bf->bf_state.bf_type &= ~BUF_AMPDU;
1315
1316 /* update starting sequence number for subsequent ADDBA request */
1317 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
1318
1319 bf->bf_nframes = 1;
Sujithd43f30152009-01-16 21:38:53 +05301320 bf->bf_lastbf = bf;
Sujithe8324352009-01-16 21:38:42 +05301321 ath_buf_set_rate(sc, bf);
1322 ath_tx_txqaddbuf(sc, txq, bf_head);
Sujithfec247c2009-07-27 12:08:16 +05301323 TX_STAT_INC(txq->axq_qnum, queued);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001324}
1325
Sujithc37452b2009-03-09 09:31:57 +05301326static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
1327 struct list_head *bf_head)
1328{
1329 struct ath_buf *bf;
1330
1331 bf = list_first_entry(bf_head, struct ath_buf, list);
1332
1333 bf->bf_lastbf = bf;
1334 bf->bf_nframes = 1;
1335 ath_buf_set_rate(sc, bf);
1336 ath_tx_txqaddbuf(sc, txq, bf_head);
Sujithfec247c2009-07-27 12:08:16 +05301337 TX_STAT_INC(txq->axq_qnum, queued);
Sujithc37452b2009-03-09 09:31:57 +05301338}
1339
Sujith528f0c62008-10-29 10:14:26 +05301340static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001341{
Sujith528f0c62008-10-29 10:14:26 +05301342 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001343 enum ath9k_pkt_type htype;
1344 __le16 fc;
1345
Sujith528f0c62008-10-29 10:14:26 +05301346 hdr = (struct ieee80211_hdr *)skb->data;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001347 fc = hdr->frame_control;
1348
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001349 if (ieee80211_is_beacon(fc))
1350 htype = ATH9K_PKT_TYPE_BEACON;
1351 else if (ieee80211_is_probe_resp(fc))
1352 htype = ATH9K_PKT_TYPE_PROBE_RESP;
1353 else if (ieee80211_is_atim(fc))
1354 htype = ATH9K_PKT_TYPE_ATIM;
1355 else if (ieee80211_is_pspoll(fc))
1356 htype = ATH9K_PKT_TYPE_PSPOLL;
1357 else
1358 htype = ATH9K_PKT_TYPE_NORMAL;
1359
1360 return htype;
1361}
1362
Sujith528f0c62008-10-29 10:14:26 +05301363static int get_hw_crypto_keytype(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001364{
Sujith528f0c62008-10-29 10:14:26 +05301365 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1366
1367 if (tx_info->control.hw_key) {
1368 if (tx_info->control.hw_key->alg == ALG_WEP)
1369 return ATH9K_KEY_TYPE_WEP;
1370 else if (tx_info->control.hw_key->alg == ALG_TKIP)
1371 return ATH9K_KEY_TYPE_TKIP;
1372 else if (tx_info->control.hw_key->alg == ALG_CCMP)
1373 return ATH9K_KEY_TYPE_AES;
1374 }
1375
1376 return ATH9K_KEY_TYPE_CLEAR;
1377}
1378
Sujith528f0c62008-10-29 10:14:26 +05301379static void assign_aggr_tid_seqno(struct sk_buff *skb,
1380 struct ath_buf *bf)
1381{
1382 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1383 struct ieee80211_hdr *hdr;
1384 struct ath_node *an;
1385 struct ath_atx_tid *tid;
1386 __le16 fc;
1387 u8 *qc;
1388
1389 if (!tx_info->control.sta)
1390 return;
1391
1392 an = (struct ath_node *)tx_info->control.sta->drv_priv;
1393 hdr = (struct ieee80211_hdr *)skb->data;
1394 fc = hdr->frame_control;
1395
Sujith528f0c62008-10-29 10:14:26 +05301396 if (ieee80211_is_data_qos(fc)) {
1397 qc = ieee80211_get_qos_ctl(hdr);
1398 bf->bf_tidno = qc[0] & 0xf;
Sujith98deeea2008-08-11 14:05:46 +05301399 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001400
Sujithe8324352009-01-16 21:38:42 +05301401 /*
1402 * For HT capable stations, we save tidno for later use.
Senthil Balasubramaniand3a1db12008-12-22 16:31:58 +05301403 * We also override seqno set by upper layer with the one
1404 * in tx aggregation state.
Senthil Balasubramaniand3a1db12008-12-22 16:31:58 +05301405 */
1406 tid = ATH_AN_2_TID(an, bf->bf_tidno);
Sujith17b182e2009-12-14 14:56:56 +05301407 hdr->seq_ctrl = cpu_to_le16(tid->seq_next << IEEE80211_SEQ_SEQ_SHIFT);
Senthil Balasubramaniand3a1db12008-12-22 16:31:58 +05301408 bf->bf_seqno = tid->seq_next;
1409 INCR(tid->seq_next, IEEE80211_SEQ_MAX);
Sujith528f0c62008-10-29 10:14:26 +05301410}
1411
1412static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
1413 struct ath_txq *txq)
1414{
1415 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1416 int flags = 0;
1417
1418 flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
1419 flags |= ATH9K_TXDESC_INTREQ;
1420
1421 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
1422 flags |= ATH9K_TXDESC_NOACK;
Sujith528f0c62008-10-29 10:14:26 +05301423
1424 return flags;
1425}
1426
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001427/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001428 * rix - rate index
1429 * pktlen - total bytes (delims + data + fcs + pads + pad delims)
1430 * width - 0 for 20 MHz, 1 for 40 MHz
1431 * half_gi - to use 4us v/s 3.6 us for symbol time
1432 */
Sujith102e0572008-10-29 10:15:16 +05301433static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
1434 int width, int half_gi, bool shortPreamble)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001435{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001436 u32 nbits, nsymbits, duration, nsymbols;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001437 int streams, pktlen;
1438
Sujithcd3d39a2008-08-11 14:03:34 +05301439 pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
Sujithe63835b2008-11-18 09:07:53 +05301440
1441 /* find number of symbols: PLCP + data */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001442 nbits = (pktlen << 3) + OFDM_PLCP_BITS;
Felix Fietkau545750d2009-11-23 22:21:01 +01001443 nsymbits = bits_per_symbol[rix][width];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001444 nsymbols = (nbits + nsymbits - 1) / nsymbits;
1445
1446 if (!half_gi)
1447 duration = SYMBOL_TIME(nsymbols);
1448 else
1449 duration = SYMBOL_TIME_HALFGI(nsymbols);
1450
Sujithe63835b2008-11-18 09:07:53 +05301451 /* addup duration for legacy/ht training and signal fields */
Felix Fietkau545750d2009-11-23 22:21:01 +01001452 streams = HT_RC_2_STREAMS(rix);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001453 duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
Sujith102e0572008-10-29 10:15:16 +05301454
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001455 return duration;
1456}
1457
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001458static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
1459{
Luis R. Rodriguez43c27612009-09-13 21:07:07 -07001460 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001461 struct ath9k_11n_rate_series series[4];
Sujith528f0c62008-10-29 10:14:26 +05301462 struct sk_buff *skb;
1463 struct ieee80211_tx_info *tx_info;
Sujitha8efee42008-11-18 09:07:30 +05301464 struct ieee80211_tx_rate *rates;
Felix Fietkau545750d2009-11-23 22:21:01 +01001465 const struct ieee80211_rate *rate;
Sujith254ad0f2009-02-04 08:10:19 +05301466 struct ieee80211_hdr *hdr;
Sujithc89424d2009-01-30 14:29:28 +05301467 int i, flags = 0;
1468 u8 rix = 0, ctsrate = 0;
Sujith254ad0f2009-02-04 08:10:19 +05301469 bool is_pspoll;
Sujithe63835b2008-11-18 09:07:53 +05301470
1471 memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
Sujith528f0c62008-10-29 10:14:26 +05301472
Sujitha22be222009-03-30 15:28:36 +05301473 skb = bf->bf_mpdu;
Sujith528f0c62008-10-29 10:14:26 +05301474 tx_info = IEEE80211_SKB_CB(skb);
Sujithe63835b2008-11-18 09:07:53 +05301475 rates = tx_info->control.rates;
Sujith254ad0f2009-02-04 08:10:19 +05301476 hdr = (struct ieee80211_hdr *)skb->data;
1477 is_pspoll = ieee80211_is_pspoll(hdr->frame_control);
Sujith528f0c62008-10-29 10:14:26 +05301478
Sujithc89424d2009-01-30 14:29:28 +05301479 /*
1480 * We check if Short Preamble is needed for the CTS rate by
1481 * checking the BSS's global flag.
1482 * But for the rate series, IEEE80211_TX_RC_USE_SHORT_PREAMBLE is used.
1483 */
Felix Fietkau545750d2009-11-23 22:21:01 +01001484 rate = ieee80211_get_rts_cts_rate(sc->hw, tx_info);
1485 ctsrate = rate->hw_value;
Sujithc89424d2009-01-30 14:29:28 +05301486 if (sc->sc_flags & SC_OP_PREAMBLE_SHORT)
Felix Fietkau545750d2009-11-23 22:21:01 +01001487 ctsrate |= rate->hw_value_short;
Luis R. Rodriguez96742252008-12-23 15:58:38 -08001488
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001489 for (i = 0; i < 4; i++) {
Felix Fietkau545750d2009-11-23 22:21:01 +01001490 bool is_40, is_sgi, is_sp;
1491 int phy;
1492
Sujithe63835b2008-11-18 09:07:53 +05301493 if (!rates[i].count || (rates[i].idx < 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001494 continue;
1495
Sujitha8efee42008-11-18 09:07:30 +05301496 rix = rates[i].idx;
Sujitha8efee42008-11-18 09:07:30 +05301497 series[i].Tries = rates[i].count;
Luis R. Rodriguez43c27612009-09-13 21:07:07 -07001498 series[i].ChSel = common->tx_chainmask;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001499
Felix Fietkau27032052010-01-17 21:08:50 +01001500 if ((sc->config.ath_aggr_prot && bf_isaggr(bf)) ||
1501 (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS)) {
Sujithc89424d2009-01-30 14:29:28 +05301502 series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
Felix Fietkau27032052010-01-17 21:08:50 +01001503 flags |= ATH9K_TXDESC_RTSENA;
1504 } else if (rates[i].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
1505 series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
1506 flags |= ATH9K_TXDESC_CTSENA;
1507 }
1508
Sujithc89424d2009-01-30 14:29:28 +05301509 if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1510 series[i].RateFlags |= ATH9K_RATESERIES_2040;
1511 if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
1512 series[i].RateFlags |= ATH9K_RATESERIES_HALFGI;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001513
Felix Fietkau545750d2009-11-23 22:21:01 +01001514 is_sgi = !!(rates[i].flags & IEEE80211_TX_RC_SHORT_GI);
1515 is_40 = !!(rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH);
1516 is_sp = !!(rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
1517
1518 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
1519 /* MCS rates */
1520 series[i].Rate = rix | 0x80;
1521 series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
1522 is_40, is_sgi, is_sp);
1523 continue;
1524 }
1525
1526 /* legcay rates */
1527 if ((tx_info->band == IEEE80211_BAND_2GHZ) &&
1528 !(rate->flags & IEEE80211_RATE_ERP_G))
1529 phy = WLAN_RC_PHY_CCK;
1530 else
1531 phy = WLAN_RC_PHY_OFDM;
1532
1533 rate = &sc->sbands[tx_info->band].bitrates[rates[i].idx];
1534 series[i].Rate = rate->hw_value;
1535 if (rate->hw_value_short) {
1536 if (rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1537 series[i].Rate |= rate->hw_value_short;
1538 } else {
1539 is_sp = false;
1540 }
1541
1542 series[i].PktDuration = ath9k_hw_computetxtime(sc->sc_ah,
1543 phy, rate->bitrate * 100, bf->bf_frmlen, rix, is_sp);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001544 }
1545
Felix Fietkau27032052010-01-17 21:08:50 +01001546 /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
1547 if (bf_isaggr(bf) && (bf->bf_al > sc->sc_ah->caps.rts_aggr_limit))
1548 flags &= ~ATH9K_TXDESC_RTSENA;
1549
1550 /* ATH9K_TXDESC_RTSENA and ATH9K_TXDESC_CTSENA are mutually exclusive. */
1551 if (flags & ATH9K_TXDESC_RTSENA)
1552 flags &= ~ATH9K_TXDESC_CTSENA;
1553
Sujithe63835b2008-11-18 09:07:53 +05301554 /* set dur_update_en for l-sig computation except for PS-Poll frames */
Sujithc89424d2009-01-30 14:29:28 +05301555 ath9k_hw_set11n_ratescenario(sc->sc_ah, bf->bf_desc,
1556 bf->bf_lastbf->bf_desc,
Sujith254ad0f2009-02-04 08:10:19 +05301557 !is_pspoll, ctsrate,
Sujithc89424d2009-01-30 14:29:28 +05301558 0, series, 4, flags);
Sujith102e0572008-10-29 10:15:16 +05301559
Sujith17d79042009-02-09 13:27:03 +05301560 if (sc->config.ath_aggr_prot && flags)
Sujithc89424d2009-01-30 14:29:28 +05301561 ath9k_hw_set11n_burstduration(sc->sc_ah, bf->bf_desc, 8192);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001562}
1563
Jouni Malinenc52f33d2009-03-03 19:23:29 +02001564static int ath_tx_setup_buffer(struct ieee80211_hw *hw, struct ath_buf *bf,
Sujithe8324352009-01-16 21:38:42 +05301565 struct sk_buff *skb,
1566 struct ath_tx_control *txctl)
1567{
Jouni Malinenc52f33d2009-03-03 19:23:29 +02001568 struct ath_wiphy *aphy = hw->priv;
1569 struct ath_softc *sc = aphy->sc;
Sujithe8324352009-01-16 21:38:42 +05301570 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1571 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Sujithe8324352009-01-16 21:38:42 +05301572 int hdrlen;
1573 __le16 fc;
Benoit Papillault1bc14882009-11-24 15:49:18 +01001574 int padpos, padsize;
Sujithe8324352009-01-16 21:38:42 +05301575
Felix Fietkau827e69b2009-11-15 23:09:25 +01001576 tx_info->pad[0] = 0;
1577 switch (txctl->frame_type) {
Pavel Roskinc81494d2010-03-31 18:05:25 -04001578 case ATH9K_IFT_NOT_INTERNAL:
Felix Fietkau827e69b2009-11-15 23:09:25 +01001579 break;
Pavel Roskinc81494d2010-03-31 18:05:25 -04001580 case ATH9K_IFT_PAUSE:
Felix Fietkau827e69b2009-11-15 23:09:25 +01001581 tx_info->pad[0] |= ATH_TX_INFO_FRAME_TYPE_PAUSE;
1582 /* fall through */
Pavel Roskinc81494d2010-03-31 18:05:25 -04001583 case ATH9K_IFT_UNPAUSE:
Felix Fietkau827e69b2009-11-15 23:09:25 +01001584 tx_info->pad[0] |= ATH_TX_INFO_FRAME_TYPE_INTERNAL;
1585 break;
1586 }
Sujithe8324352009-01-16 21:38:42 +05301587 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1588 fc = hdr->frame_control;
1589
1590 ATH_TXBUF_RESET(bf);
1591
Felix Fietkau827e69b2009-11-15 23:09:25 +01001592 bf->aphy = aphy;
Benoit Papillault1bc14882009-11-24 15:49:18 +01001593 bf->bf_frmlen = skb->len + FCS_LEN;
1594 /* Remove the padding size from bf_frmlen, if any */
1595 padpos = ath9k_cmn_padpos(hdr->frame_control);
1596 padsize = padpos & 3;
1597 if (padsize && skb->len>padpos+padsize) {
1598 bf->bf_frmlen -= padsize;
1599 }
Sujithe8324352009-01-16 21:38:42 +05301600
Sujith6c8afef2010-02-09 10:07:00 +05301601 if (conf_is_ht(&hw->conf))
Sujithc656bbb2009-01-16 21:38:56 +05301602 bf->bf_state.bf_type |= BUF_HT;
Sujithe8324352009-01-16 21:38:42 +05301603
1604 bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
1605
1606 bf->bf_keytype = get_hw_crypto_keytype(skb);
Sujithe8324352009-01-16 21:38:42 +05301607 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR) {
1608 bf->bf_frmlen += tx_info->control.hw_key->icv_len;
1609 bf->bf_keyix = tx_info->control.hw_key->hw_key_idx;
1610 } else {
1611 bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
1612 }
1613
Sujith17b182e2009-12-14 14:56:56 +05301614 if (ieee80211_is_data_qos(fc) && bf_isht(bf) &&
1615 (sc->sc_flags & SC_OP_TXAGGR))
Sujithe8324352009-01-16 21:38:42 +05301616 assign_aggr_tid_seqno(skb, bf);
1617
1618 bf->bf_mpdu = skb;
1619
1620 bf->bf_dmacontext = dma_map_single(sc->dev, skb->data,
1621 skb->len, DMA_TO_DEVICE);
1622 if (unlikely(dma_mapping_error(sc->dev, bf->bf_dmacontext))) {
1623 bf->bf_mpdu = NULL;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001624 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
1625 "dma_mapping_error() on TX\n");
Sujithe8324352009-01-16 21:38:42 +05301626 return -ENOMEM;
1627 }
1628
1629 bf->bf_buf_addr = bf->bf_dmacontext;
Luis R. Rodrigueze7824a52009-11-24 02:53:25 -05001630
1631 /* tag if this is a nullfunc frame to enable PS when AP acks it */
1632 if (ieee80211_is_nullfunc(fc) && ieee80211_has_pm(fc)) {
1633 bf->bf_isnullfunc = true;
Sujith1b04b932010-01-08 10:36:05 +05301634 sc->ps_flags &= ~PS_NULLFUNC_COMPLETED;
Luis R. Rodrigueze7824a52009-11-24 02:53:25 -05001635 } else
1636 bf->bf_isnullfunc = false;
1637
Sujithe8324352009-01-16 21:38:42 +05301638 return 0;
1639}
1640
1641/* FIXME: tx power */
1642static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
1643 struct ath_tx_control *txctl)
1644{
Sujitha22be222009-03-30 15:28:36 +05301645 struct sk_buff *skb = bf->bf_mpdu;
Sujithe8324352009-01-16 21:38:42 +05301646 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Sujithc37452b2009-03-09 09:31:57 +05301647 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Sujithe8324352009-01-16 21:38:42 +05301648 struct ath_node *an = NULL;
1649 struct list_head bf_head;
1650 struct ath_desc *ds;
1651 struct ath_atx_tid *tid;
Sujithcbe61d82009-02-09 13:27:12 +05301652 struct ath_hw *ah = sc->sc_ah;
Sujithe8324352009-01-16 21:38:42 +05301653 int frm_type;
Sujithc37452b2009-03-09 09:31:57 +05301654 __le16 fc;
Sujithe8324352009-01-16 21:38:42 +05301655
1656 frm_type = get_hw_packet_type(skb);
Sujithc37452b2009-03-09 09:31:57 +05301657 fc = hdr->frame_control;
Sujithe8324352009-01-16 21:38:42 +05301658
1659 INIT_LIST_HEAD(&bf_head);
1660 list_add_tail(&bf->list, &bf_head);
1661
1662 ds = bf->bf_desc;
Vasanthakumar Thiagarajan87d5efb2010-04-15 17:38:43 -04001663 ath9k_hw_set_desc_link(ah, ds, 0);
Sujithe8324352009-01-16 21:38:42 +05301664
1665 ath9k_hw_set11n_txdesc(ah, ds, bf->bf_frmlen, frm_type, MAX_RATE_POWER,
1666 bf->bf_keyix, bf->bf_keytype, bf->bf_flags);
1667
1668 ath9k_hw_filltxdesc(ah, ds,
1669 skb->len, /* segment length */
1670 true, /* first segment */
1671 true, /* last segment */
Vasanthakumar Thiagarajan3f3a1c82010-04-15 17:38:42 -04001672 ds, /* first descriptor */
Vasanthakumar Thiagarajancc610ac02010-04-15 17:39:26 -04001673 bf->bf_buf_addr,
1674 txctl->txq->axq_qnum);
Sujithe8324352009-01-16 21:38:42 +05301675
Sujithe8324352009-01-16 21:38:42 +05301676 spin_lock_bh(&txctl->txq->axq_lock);
1677
1678 if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR) &&
1679 tx_info->control.sta) {
1680 an = (struct ath_node *)tx_info->control.sta->drv_priv;
1681 tid = ATH_AN_2_TID(an, bf->bf_tidno);
1682
Sujithc37452b2009-03-09 09:31:57 +05301683 if (!ieee80211_is_data_qos(fc)) {
1684 ath_tx_send_normal(sc, txctl->txq, &bf_head);
1685 goto tx_done;
1686 }
1687
Felix Fietkau4fdec032010-03-12 04:02:43 +01001688 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
Sujithe8324352009-01-16 21:38:42 +05301689 /*
1690 * Try aggregation if it's a unicast data frame
1691 * and the destination is HT capable.
1692 */
1693 ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
1694 } else {
1695 /*
1696 * Send this frame as regular when ADDBA
1697 * exchange is neither complete nor pending.
1698 */
Sujithc37452b2009-03-09 09:31:57 +05301699 ath_tx_send_ht_normal(sc, txctl->txq,
1700 tid, &bf_head);
Sujithe8324352009-01-16 21:38:42 +05301701 }
1702 } else {
Sujithc37452b2009-03-09 09:31:57 +05301703 ath_tx_send_normal(sc, txctl->txq, &bf_head);
Sujithe8324352009-01-16 21:38:42 +05301704 }
1705
Sujithc37452b2009-03-09 09:31:57 +05301706tx_done:
Sujithe8324352009-01-16 21:38:42 +05301707 spin_unlock_bh(&txctl->txq->axq_lock);
1708}
1709
1710/* Upon failure caller should free skb */
Jouni Malinenc52f33d2009-03-03 19:23:29 +02001711int ath_tx_start(struct ieee80211_hw *hw, struct sk_buff *skb,
Sujithe8324352009-01-16 21:38:42 +05301712 struct ath_tx_control *txctl)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001713{
Jouni Malinenc52f33d2009-03-03 19:23:29 +02001714 struct ath_wiphy *aphy = hw->priv;
1715 struct ath_softc *sc = aphy->sc;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001716 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001717 struct ath_buf *bf;
Sujithe8324352009-01-16 21:38:42 +05301718 int r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001719
Sujithe8324352009-01-16 21:38:42 +05301720 bf = ath_tx_get_buffer(sc);
1721 if (!bf) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001722 ath_print(common, ATH_DBG_XMIT, "TX buffers are full\n");
Sujithe8324352009-01-16 21:38:42 +05301723 return -1;
1724 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001725
Jouni Malinenc52f33d2009-03-03 19:23:29 +02001726 r = ath_tx_setup_buffer(hw, bf, skb, txctl);
Sujithe8324352009-01-16 21:38:42 +05301727 if (unlikely(r)) {
1728 struct ath_txq *txq = txctl->txq;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001729
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001730 ath_print(common, ATH_DBG_FATAL, "TX mem alloc failure\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001731
Sujithe8324352009-01-16 21:38:42 +05301732 /* upon ath_tx_processq() this TX queue will be resumed, we
1733 * guarantee this will happen by knowing beforehand that
1734 * we will at least have to run TX completionon one buffer
1735 * on the queue */
1736 spin_lock_bh(&txq->axq_lock);
Sujithf7a99e42009-02-17 15:36:33 +05301737 if (sc->tx.txq[txq->axq_qnum].axq_depth > 1) {
Luis R. Rodriguezf52de032009-11-02 17:09:12 -08001738 ath_mac80211_stop_queue(sc, skb_get_queue_mapping(skb));
Sujithe8324352009-01-16 21:38:42 +05301739 txq->stopped = 1;
1740 }
1741 spin_unlock_bh(&txq->axq_lock);
1742
1743 spin_lock_bh(&sc->tx.txbuflock);
1744 list_add_tail(&bf->list, &sc->tx.txbuf);
1745 spin_unlock_bh(&sc->tx.txbuflock);
1746
1747 return r;
1748 }
1749
1750 ath_tx_start_dma(sc, bf, txctl);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001751
1752 return 0;
1753}
1754
Jouni Malinenc52f33d2009-03-03 19:23:29 +02001755void ath_tx_cabq(struct ieee80211_hw *hw, struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001756{
Jouni Malinenc52f33d2009-03-03 19:23:29 +02001757 struct ath_wiphy *aphy = hw->priv;
1758 struct ath_softc *sc = aphy->sc;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001759 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Benoit Papillault4d91f9f2009-12-12 00:22:35 +01001760 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1761 int padpos, padsize;
Sujithe8324352009-01-16 21:38:42 +05301762 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1763 struct ath_tx_control txctl;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001764
Sujithe8324352009-01-16 21:38:42 +05301765 memset(&txctl, 0, sizeof(struct ath_tx_control));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001766
Sujithe8324352009-01-16 21:38:42 +05301767 /*
1768 * As a temporary workaround, assign seq# here; this will likely need
1769 * to be cleaned up to work better with Beacon transmission and virtual
1770 * BSSes.
1771 */
1772 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Sujithe8324352009-01-16 21:38:42 +05301773 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
1774 sc->tx.seq_no += 0x10;
1775 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1776 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001777 }
1778
Sujithe8324352009-01-16 21:38:42 +05301779 /* Add the padding after the header if this is not already done */
Benoit Papillault4d91f9f2009-12-12 00:22:35 +01001780 padpos = ath9k_cmn_padpos(hdr->frame_control);
1781 padsize = padpos & 3;
1782 if (padsize && skb->len>padpos) {
Sujithe8324352009-01-16 21:38:42 +05301783 if (skb_headroom(skb) < padsize) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001784 ath_print(common, ATH_DBG_XMIT,
1785 "TX CABQ padding failed\n");
Sujithe8324352009-01-16 21:38:42 +05301786 dev_kfree_skb_any(skb);
1787 return;
1788 }
1789 skb_push(skb, padsize);
Benoit Papillault4d91f9f2009-12-12 00:22:35 +01001790 memmove(skb->data, skb->data + padsize, padpos);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001791 }
1792
Sujithe8324352009-01-16 21:38:42 +05301793 txctl.txq = sc->beacon.cabq;
1794
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001795 ath_print(common, ATH_DBG_XMIT,
1796 "transmitting CABQ packet, skb: %p\n", skb);
Sujithe8324352009-01-16 21:38:42 +05301797
Jouni Malinenc52f33d2009-03-03 19:23:29 +02001798 if (ath_tx_start(hw, skb, &txctl) != 0) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001799 ath_print(common, ATH_DBG_XMIT, "CABQ TX failed\n");
Sujithe8324352009-01-16 21:38:42 +05301800 goto exit;
1801 }
1802
1803 return;
1804exit:
1805 dev_kfree_skb_any(skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001806}
1807
Sujithe8324352009-01-16 21:38:42 +05301808/*****************/
1809/* TX Completion */
1810/*****************/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001811
Sujithe8324352009-01-16 21:38:42 +05301812static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
Felix Fietkau827e69b2009-11-15 23:09:25 +01001813 struct ath_wiphy *aphy, int tx_flags)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001814{
Sujithe8324352009-01-16 21:38:42 +05301815 struct ieee80211_hw *hw = sc->hw;
1816 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001817 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Benoit Papillault4d91f9f2009-12-12 00:22:35 +01001818 struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1819 int padpos, padsize;
Sujithe8324352009-01-16 21:38:42 +05301820
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001821 ath_print(common, ATH_DBG_XMIT, "TX complete: skb: %p\n", skb);
Sujithe8324352009-01-16 21:38:42 +05301822
Felix Fietkau827e69b2009-11-15 23:09:25 +01001823 if (aphy)
1824 hw = aphy->hw;
Sujithe8324352009-01-16 21:38:42 +05301825
Vasanthakumar Thiagarajan6b2c4032009-03-20 15:27:50 +05301826 if (tx_flags & ATH_TX_BAR)
Sujithe8324352009-01-16 21:38:42 +05301827 tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
Sujithe8324352009-01-16 21:38:42 +05301828
Vasanthakumar Thiagarajan6b2c4032009-03-20 15:27:50 +05301829 if (!(tx_flags & (ATH_TX_ERROR | ATH_TX_XRETRY))) {
Sujithe8324352009-01-16 21:38:42 +05301830 /* Frame was ACKed */
1831 tx_info->flags |= IEEE80211_TX_STAT_ACK;
1832 }
1833
Benoit Papillault4d91f9f2009-12-12 00:22:35 +01001834 padpos = ath9k_cmn_padpos(hdr->frame_control);
1835 padsize = padpos & 3;
1836 if (padsize && skb->len>padpos+padsize) {
Sujithe8324352009-01-16 21:38:42 +05301837 /*
1838 * Remove MAC header padding before giving the frame back to
1839 * mac80211.
1840 */
Benoit Papillault4d91f9f2009-12-12 00:22:35 +01001841 memmove(skb->data + padsize, skb->data, padpos);
Sujithe8324352009-01-16 21:38:42 +05301842 skb_pull(skb, padsize);
1843 }
1844
Sujith1b04b932010-01-08 10:36:05 +05301845 if (sc->ps_flags & PS_WAIT_FOR_TX_ACK) {
1846 sc->ps_flags &= ~PS_WAIT_FOR_TX_ACK;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001847 ath_print(common, ATH_DBG_PS,
1848 "Going back to sleep after having "
Pavel Roskinf643e512010-01-29 17:22:12 -05001849 "received TX status (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +05301850 sc->ps_flags & (PS_WAIT_FOR_BEACON |
1851 PS_WAIT_FOR_CAB |
1852 PS_WAIT_FOR_PSPOLL_DATA |
1853 PS_WAIT_FOR_TX_ACK));
Jouni Malinen9a23f9c2009-05-19 17:01:38 +03001854 }
1855
Felix Fietkau827e69b2009-11-15 23:09:25 +01001856 if (unlikely(tx_info->pad[0] & ATH_TX_INFO_FRAME_TYPE_INTERNAL))
Jouni Malinenf0ed85c2009-03-03 19:23:31 +02001857 ath9k_tx_status(hw, skb);
Felix Fietkau827e69b2009-11-15 23:09:25 +01001858 else
1859 ieee80211_tx_status(hw, skb);
Sujithe8324352009-01-16 21:38:42 +05301860}
1861
1862static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001863 struct ath_txq *txq, struct list_head *bf_q,
1864 struct ath_tx_status *ts, int txok, int sendbar)
Sujithe8324352009-01-16 21:38:42 +05301865{
1866 struct sk_buff *skb = bf->bf_mpdu;
Sujithe8324352009-01-16 21:38:42 +05301867 unsigned long flags;
Vasanthakumar Thiagarajan6b2c4032009-03-20 15:27:50 +05301868 int tx_flags = 0;
Sujithe8324352009-01-16 21:38:42 +05301869
Sujithe8324352009-01-16 21:38:42 +05301870 if (sendbar)
Vasanthakumar Thiagarajan6b2c4032009-03-20 15:27:50 +05301871 tx_flags = ATH_TX_BAR;
Sujithe8324352009-01-16 21:38:42 +05301872
1873 if (!txok) {
Vasanthakumar Thiagarajan6b2c4032009-03-20 15:27:50 +05301874 tx_flags |= ATH_TX_ERROR;
Sujithe8324352009-01-16 21:38:42 +05301875
1876 if (bf_isxretried(bf))
Vasanthakumar Thiagarajan6b2c4032009-03-20 15:27:50 +05301877 tx_flags |= ATH_TX_XRETRY;
Sujithe8324352009-01-16 21:38:42 +05301878 }
1879
1880 dma_unmap_single(sc->dev, bf->bf_dmacontext, skb->len, DMA_TO_DEVICE);
Felix Fietkau827e69b2009-11-15 23:09:25 +01001881 ath_tx_complete(sc, skb, bf->aphy, tx_flags);
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001882 ath_debug_stat_tx(sc, txq, bf, ts);
Sujithe8324352009-01-16 21:38:42 +05301883
1884 /*
1885 * Return the list of ath_buf of this mpdu to free queue
1886 */
1887 spin_lock_irqsave(&sc->tx.txbuflock, flags);
1888 list_splice_tail_init(bf_q, &sc->tx.txbuf);
1889 spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
1890}
1891
1892static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001893 struct ath_tx_status *ts, int txok)
Sujithe8324352009-01-16 21:38:42 +05301894{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001895 u16 seq_st = 0;
1896 u32 ba[WME_BA_BMP_SIZE >> 5];
Sujithe8324352009-01-16 21:38:42 +05301897 int ba_index;
1898 int nbad = 0;
1899 int isaggr = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001900
Vasanthakumar Thiagarajan6d913f72010-04-15 17:38:46 -04001901 if (bf->bf_tx_aborted)
Sujithe8324352009-01-16 21:38:42 +05301902 return 0;
Sujith528f0c62008-10-29 10:14:26 +05301903
Sujithcd3d39a2008-08-11 14:03:34 +05301904 isaggr = bf_isaggr(bf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001905 if (isaggr) {
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001906 seq_st = ts->ts_seqnum;
1907 memcpy(ba, &ts->ba_low, WME_BA_BMP_SIZE >> 3);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001908 }
1909
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001910 while (bf) {
Sujithe8324352009-01-16 21:38:42 +05301911 ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
1912 if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
1913 nbad++;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001914
Sujithe8324352009-01-16 21:38:42 +05301915 bf = bf->bf_next;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001916 }
1917
Sujithe8324352009-01-16 21:38:42 +05301918 return nbad;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001919}
1920
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001921static void ath_tx_rc_status(struct ath_buf *bf, struct ath_tx_status *ts,
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +05301922 int nbad, int txok, bool update_rc)
Sujithc4288392008-11-18 09:09:30 +05301923{
Sujitha22be222009-03-30 15:28:36 +05301924 struct sk_buff *skb = bf->bf_mpdu;
Sujith254ad0f2009-02-04 08:10:19 +05301925 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Sujithc4288392008-11-18 09:09:30 +05301926 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Felix Fietkau827e69b2009-11-15 23:09:25 +01001927 struct ieee80211_hw *hw = bf->aphy->hw;
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +05301928 u8 i, tx_rateindex;
Sujithc4288392008-11-18 09:09:30 +05301929
Sujith95e4acb2009-03-13 08:56:09 +05301930 if (txok)
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001931 tx_info->status.ack_signal = ts->ts_rssi;
Sujith95e4acb2009-03-13 08:56:09 +05301932
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001933 tx_rateindex = ts->ts_rateindex;
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +05301934 WARN_ON(tx_rateindex >= hw->max_rates);
1935
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001936 if (ts->ts_status & ATH9K_TXERR_FILT)
Sujithc4288392008-11-18 09:09:30 +05301937 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
Felix Fietkaud9698472010-03-01 13:32:11 +01001938 if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && update_rc)
1939 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
Sujithc4288392008-11-18 09:09:30 +05301940
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001941 if ((ts->ts_status & ATH9K_TXERR_FILT) == 0 &&
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +05301942 (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0 && update_rc) {
Sujith254ad0f2009-02-04 08:10:19 +05301943 if (ieee80211_is_data(hdr->frame_control)) {
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001944 if (ts->ts_flags &
Felix Fietkau827e69b2009-11-15 23:09:25 +01001945 (ATH9K_TX_DATA_UNDERRUN | ATH9K_TX_DELIM_UNDERRUN))
1946 tx_info->pad[0] |= ATH_TX_INFO_UNDERRUN;
Felix Fietkaudb1a0522010-03-29 20:07:11 -07001947 if ((ts->ts_status & ATH9K_TXERR_XRETRY) ||
1948 (ts->ts_status & ATH9K_TXERR_FIFO))
Felix Fietkau827e69b2009-11-15 23:09:25 +01001949 tx_info->pad[0] |= ATH_TX_INFO_XRETRY;
1950 tx_info->status.ampdu_len = bf->bf_nframes;
1951 tx_info->status.ampdu_ack_len = bf->bf_nframes - nbad;
Sujithc4288392008-11-18 09:09:30 +05301952 }
1953 }
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +05301954
Felix Fietkau545750d2009-11-23 22:21:01 +01001955 for (i = tx_rateindex + 1; i < hw->max_rates; i++) {
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +05301956 tx_info->status.rates[i].count = 0;
Felix Fietkau545750d2009-11-23 22:21:01 +01001957 tx_info->status.rates[i].idx = -1;
1958 }
Vasanthakumar Thiagarajan8a92e2e2009-03-20 15:27:49 +05301959
1960 tx_info->status.rates[tx_rateindex].count = bf->bf_retries + 1;
Sujithc4288392008-11-18 09:09:30 +05301961}
1962
Sujith059d8062009-01-16 21:38:49 +05301963static void ath_wake_mac80211_queue(struct ath_softc *sc, struct ath_txq *txq)
1964{
1965 int qnum;
1966
1967 spin_lock_bh(&txq->axq_lock);
1968 if (txq->stopped &&
Sujithf7a99e42009-02-17 15:36:33 +05301969 sc->tx.txq[txq->axq_qnum].axq_depth <= (ATH_TXBUF - 20)) {
Sujith059d8062009-01-16 21:38:49 +05301970 qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
1971 if (qnum != -1) {
Luis R. Rodriguezf52de032009-11-02 17:09:12 -08001972 ath_mac80211_start_queue(sc, qnum);
Sujith059d8062009-01-16 21:38:49 +05301973 txq->stopped = 0;
1974 }
1975 }
1976 spin_unlock_bh(&txq->axq_lock);
1977}
1978
Sujithc4288392008-11-18 09:09:30 +05301979static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001980{
Sujithcbe61d82009-02-09 13:27:12 +05301981 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001982 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001983 struct ath_buf *bf, *lastbf, *bf_held = NULL;
1984 struct list_head bf_head;
Sujithc4288392008-11-18 09:09:30 +05301985 struct ath_desc *ds;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001986 struct ath_tx_status ts;
Vasanthakumar Thiagarajan0934af22009-03-18 20:22:00 +05301987 int txok;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001988 int status;
1989
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001990 ath_print(common, ATH_DBG_QUEUE, "tx queue %d (%x), link %p\n",
1991 txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
1992 txq->axq_link);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001993
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001994 for (;;) {
1995 spin_lock_bh(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001996 if (list_empty(&txq->axq_q)) {
1997 txq->axq_link = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001998 spin_unlock_bh(&txq->axq_lock);
1999 break;
2000 }
2001 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2002
2003 /*
2004 * There is a race condition that a BH gets scheduled
2005 * after sw writes TxE and before hw re-load the last
2006 * descriptor to get the newly chained one.
2007 * Software must keep the last DONE descriptor as a
2008 * holding descriptor - software does so by marking
2009 * it with the STALE flag.
2010 */
2011 bf_held = NULL;
Sujitha119cc42009-03-30 15:28:38 +05302012 if (bf->bf_stale) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002013 bf_held = bf;
2014 if (list_is_last(&bf_held->list, &txq->axq_q)) {
Sujith6ef9b132009-01-16 21:38:51 +05302015 spin_unlock_bh(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002016 break;
2017 } else {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002018 bf = list_entry(bf_held->list.next,
Sujith6ef9b132009-01-16 21:38:51 +05302019 struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002020 }
2021 }
2022
2023 lastbf = bf->bf_lastbf;
Sujithe8324352009-01-16 21:38:42 +05302024 ds = lastbf->bf_desc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002025
Felix Fietkau29bffa92010-03-29 20:14:23 -07002026 memset(&ts, 0, sizeof(ts));
2027 status = ath9k_hw_txprocdesc(ah, ds, &ts);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002028 if (status == -EINPROGRESS) {
2029 spin_unlock_bh(&txq->axq_lock);
2030 break;
2031 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002032
2033 /*
Luis R. Rodrigueze7824a52009-11-24 02:53:25 -05002034 * We now know the nullfunc frame has been ACKed so we
2035 * can disable RX.
2036 */
2037 if (bf->bf_isnullfunc &&
Felix Fietkau29bffa92010-03-29 20:14:23 -07002038 (ts.ts_status & ATH9K_TX_ACKED)) {
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05302039 if ((sc->ps_flags & PS_ENABLED))
2040 ath9k_enable_ps(sc);
2041 else
Sujith1b04b932010-01-08 10:36:05 +05302042 sc->ps_flags |= PS_NULLFUNC_COMPLETED;
Luis R. Rodrigueze7824a52009-11-24 02:53:25 -05002043 }
2044
2045 /*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002046 * Remove ath_buf's of the same transmit unit from txq,
2047 * however leave the last descriptor back as the holding
2048 * descriptor for hw.
2049 */
Sujitha119cc42009-03-30 15:28:38 +05302050 lastbf->bf_stale = true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002051 INIT_LIST_HEAD(&bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002052 if (!list_is_singular(&lastbf->list))
2053 list_cut_position(&bf_head,
2054 &txq->axq_q, lastbf->list.prev);
2055
2056 txq->axq_depth--;
Felix Fietkau29bffa92010-03-29 20:14:23 -07002057 txok = !(ts.ts_status & ATH9K_TXERR_MASK);
Senthil Balasubramanian164ace32009-07-14 20:17:09 -04002058 txq->axq_tx_inprogress = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002059 spin_unlock_bh(&txq->axq_lock);
2060
2061 if (bf_held) {
Sujithb77f4832008-12-07 21:44:03 +05302062 spin_lock_bh(&sc->tx.txbuflock);
Sujith6ef9b132009-01-16 21:38:51 +05302063 list_move_tail(&bf_held->list, &sc->tx.txbuf);
Sujithb77f4832008-12-07 21:44:03 +05302064 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002065 }
2066
Sujithcd3d39a2008-08-11 14:03:34 +05302067 if (!bf_isampdu(bf)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002068 /*
2069 * This frame is sent out as a single frame.
2070 * Use hardware retry status for this frame.
2071 */
Felix Fietkau29bffa92010-03-29 20:14:23 -07002072 bf->bf_retries = ts.ts_longretry;
2073 if (ts.ts_status & ATH9K_TXERR_XRETRY)
Sujithcd3d39a2008-08-11 14:03:34 +05302074 bf->bf_state.bf_type |= BUF_XRETRY;
Felix Fietkau29bffa92010-03-29 20:14:23 -07002075 ath_tx_rc_status(bf, &ts, 0, txok, true);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002076 }
Johannes Berge6a98542008-10-21 12:40:02 +02002077
Sujithcd3d39a2008-08-11 14:03:34 +05302078 if (bf_isampdu(bf))
Felix Fietkau29bffa92010-03-29 20:14:23 -07002079 ath_tx_complete_aggr(sc, txq, bf, &bf_head, &ts, txok);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002080 else
Felix Fietkau29bffa92010-03-29 20:14:23 -07002081 ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, txok, 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002082
Sujith059d8062009-01-16 21:38:49 +05302083 ath_wake_mac80211_queue(sc, txq);
2084
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002085 spin_lock_bh(&txq->axq_lock);
Sujith672840a2008-08-11 14:05:08 +05302086 if (sc->sc_flags & SC_OP_TXAGGR)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002087 ath_txq_schedule(sc, txq);
2088 spin_unlock_bh(&txq->axq_lock);
2089 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002090}
2091
Sujith305fe472009-07-23 15:32:29 +05302092static void ath_tx_complete_poll_work(struct work_struct *work)
Senthil Balasubramanian164ace32009-07-14 20:17:09 -04002093{
2094 struct ath_softc *sc = container_of(work, struct ath_softc,
2095 tx_complete_work.work);
2096 struct ath_txq *txq;
2097 int i;
2098 bool needreset = false;
2099
2100 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
2101 if (ATH_TXQ_SETUP(sc, i)) {
2102 txq = &sc->tx.txq[i];
2103 spin_lock_bh(&txq->axq_lock);
2104 if (txq->axq_depth) {
2105 if (txq->axq_tx_inprogress) {
2106 needreset = true;
2107 spin_unlock_bh(&txq->axq_lock);
2108 break;
2109 } else {
2110 txq->axq_tx_inprogress = true;
2111 }
2112 }
2113 spin_unlock_bh(&txq->axq_lock);
2114 }
2115
2116 if (needreset) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07002117 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_RESET,
2118 "tx hung, resetting the chip\n");
Sujith332c5562009-10-09 09:51:28 +05302119 ath9k_ps_wakeup(sc);
Senthil Balasubramanian164ace32009-07-14 20:17:09 -04002120 ath_reset(sc, false);
Sujith332c5562009-10-09 09:51:28 +05302121 ath9k_ps_restore(sc);
Senthil Balasubramanian164ace32009-07-14 20:17:09 -04002122 }
2123
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04002124 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
Senthil Balasubramanian164ace32009-07-14 20:17:09 -04002125 msecs_to_jiffies(ATH_TX_COMPLETE_POLL_INT));
2126}
2127
2128
Sujithe8324352009-01-16 21:38:42 +05302129
2130void ath_tx_tasklet(struct ath_softc *sc)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002131{
Sujithe8324352009-01-16 21:38:42 +05302132 int i;
2133 u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002134
Sujithe8324352009-01-16 21:38:42 +05302135 ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002136
2137 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
Sujithe8324352009-01-16 21:38:42 +05302138 if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
2139 ath_tx_processq(sc, &sc->tx.txq[i]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002140 }
2141}
2142
Sujithe8324352009-01-16 21:38:42 +05302143/*****************/
2144/* Init, Cleanup */
2145/*****************/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002146
2147int ath_tx_init(struct ath_softc *sc, int nbufs)
2148{
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07002149 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002150 int error = 0;
2151
Sujith797fe5cb2009-03-30 15:28:45 +05302152 spin_lock_init(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002153
Sujith797fe5cb2009-03-30 15:28:45 +05302154 error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
Vasanthakumar Thiagarajan4adfcde2010-04-15 17:39:33 -04002155 "tx", nbufs, 1, 1);
Sujith797fe5cb2009-03-30 15:28:45 +05302156 if (error != 0) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07002157 ath_print(common, ATH_DBG_FATAL,
2158 "Failed to allocate tx descriptors: %d\n", error);
Sujith797fe5cb2009-03-30 15:28:45 +05302159 goto err;
2160 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002161
Sujith797fe5cb2009-03-30 15:28:45 +05302162 error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
Vasanthakumar Thiagarajan4adfcde2010-04-15 17:39:33 -04002163 "beacon", ATH_BCBUF, 1, 0);
Sujith797fe5cb2009-03-30 15:28:45 +05302164 if (error != 0) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07002165 ath_print(common, ATH_DBG_FATAL,
2166 "Failed to allocate beacon descriptors: %d\n", error);
Sujith797fe5cb2009-03-30 15:28:45 +05302167 goto err;
2168 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002169
Senthil Balasubramanian164ace32009-07-14 20:17:09 -04002170 INIT_DELAYED_WORK(&sc->tx_complete_work, ath_tx_complete_poll_work);
2171
Sujith797fe5cb2009-03-30 15:28:45 +05302172err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002173 if (error != 0)
2174 ath_tx_cleanup(sc);
2175
2176 return error;
2177}
2178
Sujith797fe5cb2009-03-30 15:28:45 +05302179void ath_tx_cleanup(struct ath_softc *sc)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002180{
Sujithb77f4832008-12-07 21:44:03 +05302181 if (sc->beacon.bdma.dd_desc_len != 0)
2182 ath_descdma_cleanup(sc, &sc->beacon.bdma, &sc->beacon.bbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002183
Sujithb77f4832008-12-07 21:44:03 +05302184 if (sc->tx.txdma.dd_desc_len != 0)
2185 ath_descdma_cleanup(sc, &sc->tx.txdma, &sc->tx.txbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002186}
2187
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002188void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2189{
Sujithc5170162008-10-29 10:13:59 +05302190 struct ath_atx_tid *tid;
2191 struct ath_atx_ac *ac;
2192 int tidno, acno;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002193
Sujith8ee5afb2008-12-07 21:43:36 +05302194 for (tidno = 0, tid = &an->tid[tidno];
Sujithc5170162008-10-29 10:13:59 +05302195 tidno < WME_NUM_TID;
2196 tidno++, tid++) {
2197 tid->an = an;
2198 tid->tidno = tidno;
2199 tid->seq_start = tid->seq_next = 0;
2200 tid->baw_size = WME_MAX_BA;
2201 tid->baw_head = tid->baw_tail = 0;
2202 tid->sched = false;
Sujithe8324352009-01-16 21:38:42 +05302203 tid->paused = false;
Sujitha37c2c72008-10-29 10:15:40 +05302204 tid->state &= ~AGGR_CLEANUP;
Sujithc5170162008-10-29 10:13:59 +05302205 INIT_LIST_HEAD(&tid->buf_q);
Sujithc5170162008-10-29 10:13:59 +05302206 acno = TID_TO_WME_AC(tidno);
Sujith8ee5afb2008-12-07 21:43:36 +05302207 tid->ac = &an->ac[acno];
Sujitha37c2c72008-10-29 10:15:40 +05302208 tid->state &= ~AGGR_ADDBA_COMPLETE;
2209 tid->state &= ~AGGR_ADDBA_PROGRESS;
Sujithc5170162008-10-29 10:13:59 +05302210 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002211
Sujith8ee5afb2008-12-07 21:43:36 +05302212 for (acno = 0, ac = &an->ac[acno];
Sujithc5170162008-10-29 10:13:59 +05302213 acno < WME_NUM_AC; acno++, ac++) {
2214 ac->sched = false;
2215 INIT_LIST_HEAD(&ac->tid_q);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002216
Sujithc5170162008-10-29 10:13:59 +05302217 switch (acno) {
2218 case WME_AC_BE:
2219 ac->qnum = ath_tx_get_qnum(sc,
2220 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
2221 break;
2222 case WME_AC_BK:
2223 ac->qnum = ath_tx_get_qnum(sc,
2224 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
2225 break;
2226 case WME_AC_VI:
2227 ac->qnum = ath_tx_get_qnum(sc,
2228 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
2229 break;
2230 case WME_AC_VO:
2231 ac->qnum = ath_tx_get_qnum(sc,
2232 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
2233 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002234 }
2235 }
2236}
2237
Sujithb5aa9bf2008-10-29 10:13:31 +05302238void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002239{
2240 int i;
2241 struct ath_atx_ac *ac, *ac_tmp;
2242 struct ath_atx_tid *tid, *tid_tmp;
2243 struct ath_txq *txq;
Sujithe8324352009-01-16 21:38:42 +05302244
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002245 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2246 if (ATH_TXQ_SETUP(sc, i)) {
Sujithb77f4832008-12-07 21:44:03 +05302247 txq = &sc->tx.txq[i];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002248
Ming Leia9f042c2010-02-28 00:56:24 +08002249 spin_lock_bh(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002250
2251 list_for_each_entry_safe(ac,
2252 ac_tmp, &txq->axq_acq, list) {
2253 tid = list_first_entry(&ac->tid_q,
2254 struct ath_atx_tid, list);
2255 if (tid && tid->an != an)
2256 continue;
2257 list_del(&ac->list);
2258 ac->sched = false;
2259
2260 list_for_each_entry_safe(tid,
2261 tid_tmp, &ac->tid_q, list) {
2262 list_del(&tid->list);
2263 tid->sched = false;
Sujithb5aa9bf2008-10-29 10:13:31 +05302264 ath_tid_drain(sc, txq, tid);
Sujitha37c2c72008-10-29 10:15:40 +05302265 tid->state &= ~AGGR_ADDBA_COMPLETE;
Sujitha37c2c72008-10-29 10:15:40 +05302266 tid->state &= ~AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002267 }
2268 }
2269
Ming Leia9f042c2010-02-28 00:56:24 +08002270 spin_unlock_bh(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002271 }
2272 }
2273}