blob: 1ea9428c0cd21bf22c9433cef4e7287017e8f67c [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
2 * Copyright (c) 2008 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070017#include "core.h"
18
19#define BITS_PER_BYTE 8
20#define OFDM_PLCP_BITS 22
21#define HT_RC_2_MCS(_rc) ((_rc) & 0x0f)
22#define HT_RC_2_STREAMS(_rc) ((((_rc) & 0x78) >> 3) + 1)
23#define L_STF 8
24#define L_LTF 8
25#define L_SIG 4
26#define HT_SIG 8
27#define HT_STF 4
28#define HT_LTF(_ns) (4 * (_ns))
29#define SYMBOL_TIME(_ns) ((_ns) << 2) /* ns * 4 us */
30#define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5) /* ns * 3.6 us */
31#define NUM_SYMBOLS_PER_USEC(_usec) (_usec >> 2)
32#define NUM_SYMBOLS_PER_USEC_HALFGI(_usec) (((_usec*5)-4)/18)
33
34#define OFDM_SIFS_TIME 16
35
36static u32 bits_per_symbol[][2] = {
37 /* 20MHz 40MHz */
38 { 26, 54 }, /* 0: BPSK */
39 { 52, 108 }, /* 1: QPSK 1/2 */
40 { 78, 162 }, /* 2: QPSK 3/4 */
41 { 104, 216 }, /* 3: 16-QAM 1/2 */
42 { 156, 324 }, /* 4: 16-QAM 3/4 */
43 { 208, 432 }, /* 5: 64-QAM 2/3 */
44 { 234, 486 }, /* 6: 64-QAM 3/4 */
45 { 260, 540 }, /* 7: 64-QAM 5/6 */
46 { 52, 108 }, /* 8: BPSK */
47 { 104, 216 }, /* 9: QPSK 1/2 */
48 { 156, 324 }, /* 10: QPSK 3/4 */
49 { 208, 432 }, /* 11: 16-QAM 1/2 */
50 { 312, 648 }, /* 12: 16-QAM 3/4 */
51 { 416, 864 }, /* 13: 64-QAM 2/3 */
52 { 468, 972 }, /* 14: 64-QAM 3/4 */
53 { 520, 1080 }, /* 15: 64-QAM 5/6 */
54};
55
56#define IS_HT_RATE(_rate) ((_rate) & 0x80)
57
58/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070059 * Insert a chain of ath_buf (descriptors) on a txq and
60 * assume the descriptors are already chained together by caller.
61 * NB: must be called with txq lock held
62 */
63
Sujith102e0572008-10-29 10:15:16 +053064static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
65 struct list_head *head)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070066{
67 struct ath_hal *ah = sc->sc_ah;
68 struct ath_buf *bf;
Sujith102e0572008-10-29 10:15:16 +053069
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070070 /*
71 * Insert the frame on the outbound list and
72 * pass it on to the hardware.
73 */
74
75 if (list_empty(head))
76 return;
77
78 bf = list_first_entry(head, struct ath_buf, list);
79
80 list_splice_tail_init(head, &txq->axq_q);
81 txq->axq_depth++;
82 txq->axq_totalqueued++;
83 txq->axq_linkbuf = list_entry(txq->axq_q.prev, struct ath_buf, list);
84
85 DPRINTF(sc, ATH_DBG_QUEUE,
Sujith04bd46382008-11-28 22:18:05 +053086 "qnum: %d, txq depth: %d\n", txq->axq_qnum, txq->axq_depth);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070087
88 if (txq->axq_link == NULL) {
89 ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
90 DPRINTF(sc, ATH_DBG_XMIT,
Sujith04bd46382008-11-28 22:18:05 +053091 "TXDP[%u] = %llx (%p)\n",
92 txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070093 } else {
94 *txq->axq_link = bf->bf_daddr;
Sujith04bd46382008-11-28 22:18:05 +053095 DPRINTF(sc, ATH_DBG_XMIT, "link[%u] (%p)=%llx (%p)\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070096 txq->axq_qnum, txq->axq_link,
97 ito64(bf->bf_daddr), bf->bf_desc);
98 }
99 txq->axq_link = &(bf->bf_lastbf->bf_desc->ds_link);
100 ath9k_hw_txstart(ah, txq->axq_qnum);
101}
102
Sujithc4288392008-11-18 09:09:30 +0530103static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
104 struct ath_xmit_status *tx_status)
105{
106 struct ieee80211_hw *hw = sc->hw;
107 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
108 struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
Jouni Malinenf7a276a2008-12-15 16:02:04 +0200109 int hdrlen, padsize;
Sujithc4288392008-11-18 09:09:30 +0530110
Sujith04bd46382008-11-28 22:18:05 +0530111 DPRINTF(sc, ATH_DBG_XMIT, "TX complete: skb: %p\n", skb);
Sujithc4288392008-11-18 09:09:30 +0530112
113 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
114 tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
115 kfree(tx_info_priv);
116 tx_info->rate_driver_data[0] = NULL;
117 }
118
119 if (tx_status->flags & ATH_TX_BAR) {
120 tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
121 tx_status->flags &= ~ATH_TX_BAR;
122 }
123
124 if (!(tx_status->flags & (ATH_TX_ERROR | ATH_TX_XRETRY))) {
125 /* Frame was ACKed */
126 tx_info->flags |= IEEE80211_TX_STAT_ACK;
127 }
128
Jouni Malinenda027ca2008-12-15 15:44:53 +0200129 tx_info->status.rates[0].count = tx_status->retries;
130 if (tx_info->status.rates[0].flags & IEEE80211_TX_RC_MCS) {
131 /* Change idx from internal table index to MCS index */
132 int idx = tx_info->status.rates[0].idx;
133 struct ath_rate_table *rate_table = sc->cur_rate_table;
134 if (idx >= 0 && idx < rate_table->rate_cnt)
135 tx_info->status.rates[0].idx =
136 rate_table->info[idx].ratecode & 0x7f;
137 }
Sujithc4288392008-11-18 09:09:30 +0530138
Jouni Malinenf7a276a2008-12-15 16:02:04 +0200139 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
140 padsize = hdrlen & 3;
141 if (padsize && hdrlen >= 24) {
142 /*
143 * Remove MAC header padding before giving the frame back to
144 * mac80211.
145 */
146 memmove(skb->data + padsize, skb->data, hdrlen);
147 skb_pull(skb, padsize);
148 }
149
Sujithc4288392008-11-18 09:09:30 +0530150 ieee80211_tx_status(hw, skb);
151}
152
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700153/* Check if it's okay to send out aggregates */
154
Sujitha37c2c72008-10-29 10:15:40 +0530155static int ath_aggr_query(struct ath_softc *sc, struct ath_node *an, u8 tidno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700156{
157 struct ath_atx_tid *tid;
158 tid = ATH_AN_2_TID(an, tidno);
159
Sujitha37c2c72008-10-29 10:15:40 +0530160 if (tid->state & AGGR_ADDBA_COMPLETE ||
161 tid->state & AGGR_ADDBA_PROGRESS)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700162 return 1;
163 else
164 return 0;
165}
166
Sujithff37e332008-11-24 12:07:55 +0530167static void ath_get_beaconconfig(struct ath_softc *sc, int if_id,
168 struct ath_beacon_config *conf)
169{
170 struct ieee80211_hw *hw = sc->hw;
171
172 /* fill in beacon config data */
173
174 conf->beacon_interval = hw->conf.beacon_int;
175 conf->listen_interval = 100;
176 conf->dtim_count = 1;
177 conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
178}
179
Sujith528f0c62008-10-29 10:14:26 +0530180/* Calculate Atheros packet type from IEEE80211 packet header */
181
182static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700183{
Sujith528f0c62008-10-29 10:14:26 +0530184 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700185 enum ath9k_pkt_type htype;
186 __le16 fc;
187
Sujith528f0c62008-10-29 10:14:26 +0530188 hdr = (struct ieee80211_hdr *)skb->data;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700189 fc = hdr->frame_control;
190
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700191 if (ieee80211_is_beacon(fc))
192 htype = ATH9K_PKT_TYPE_BEACON;
193 else if (ieee80211_is_probe_resp(fc))
194 htype = ATH9K_PKT_TYPE_PROBE_RESP;
195 else if (ieee80211_is_atim(fc))
196 htype = ATH9K_PKT_TYPE_ATIM;
197 else if (ieee80211_is_pspoll(fc))
198 htype = ATH9K_PKT_TYPE_PSPOLL;
199 else
200 htype = ATH9K_PKT_TYPE_NORMAL;
201
202 return htype;
203}
204
Sujitha8efee42008-11-18 09:07:30 +0530205static bool is_pae(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700206{
207 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700208 __le16 fc;
209
210 hdr = (struct ieee80211_hdr *)skb->data;
211 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +0200212
Sujitha8efee42008-11-18 09:07:30 +0530213 if (ieee80211_is_data(fc)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700214 if (ieee80211_is_nullfunc(fc) ||
Sujith528f0c62008-10-29 10:14:26 +0530215 /* Port Access Entity (IEEE 802.1X) */
216 (skb->protocol == cpu_to_be16(ETH_P_PAE))) {
Sujitha8efee42008-11-18 09:07:30 +0530217 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700218 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700219 }
220
Sujitha8efee42008-11-18 09:07:30 +0530221 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700222}
223
Sujith528f0c62008-10-29 10:14:26 +0530224static int get_hw_crypto_keytype(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700225{
Sujith528f0c62008-10-29 10:14:26 +0530226 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
227
228 if (tx_info->control.hw_key) {
229 if (tx_info->control.hw_key->alg == ALG_WEP)
230 return ATH9K_KEY_TYPE_WEP;
231 else if (tx_info->control.hw_key->alg == ALG_TKIP)
232 return ATH9K_KEY_TYPE_TKIP;
233 else if (tx_info->control.hw_key->alg == ALG_CCMP)
234 return ATH9K_KEY_TYPE_AES;
235 }
236
237 return ATH9K_KEY_TYPE_CLEAR;
238}
239
Sujith528f0c62008-10-29 10:14:26 +0530240/* Called only when tx aggregation is enabled and HT is supported */
241
242static void assign_aggr_tid_seqno(struct sk_buff *skb,
243 struct ath_buf *bf)
244{
245 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
246 struct ieee80211_hdr *hdr;
247 struct ath_node *an;
248 struct ath_atx_tid *tid;
249 __le16 fc;
250 u8 *qc;
251
252 if (!tx_info->control.sta)
253 return;
254
255 an = (struct ath_node *)tx_info->control.sta->drv_priv;
256 hdr = (struct ieee80211_hdr *)skb->data;
257 fc = hdr->frame_control;
258
259 /* Get tidno */
260
261 if (ieee80211_is_data_qos(fc)) {
262 qc = ieee80211_get_qos_ctl(hdr);
263 bf->bf_tidno = qc[0] & 0xf;
Sujith98deeea2008-08-11 14:05:46 +0530264 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700265
Sujith528f0c62008-10-29 10:14:26 +0530266 /* Get seqno */
Senthil Balasubramaniand3a1db12008-12-22 16:31:58 +0530267 /* For HT capable stations, we save tidno for later use.
268 * We also override seqno set by upper layer with the one
269 * in tx aggregation state.
270 *
271 * If fragmentation is on, the sequence number is
272 * not overridden, since it has been
273 * incremented by the fragmentation routine.
274 *
275 * FIXME: check if the fragmentation threshold exceeds
276 * IEEE80211 max.
277 */
278 tid = ATH_AN_2_TID(an, bf->bf_tidno);
279 hdr->seq_ctrl = cpu_to_le16(tid->seq_next <<
280 IEEE80211_SEQ_SEQ_SHIFT);
281 bf->bf_seqno = tid->seq_next;
282 INCR(tid->seq_next, IEEE80211_SEQ_MAX);
Sujith528f0c62008-10-29 10:14:26 +0530283}
284
285static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
286 struct ath_txq *txq)
287{
288 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
289 int flags = 0;
290
291 flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
292 flags |= ATH9K_TXDESC_INTREQ;
293
294 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
295 flags |= ATH9K_TXDESC_NOACK;
296 if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
297 flags |= ATH9K_TXDESC_RTSENA;
298
299 return flags;
300}
301
302static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
303{
304 struct ath_buf *bf = NULL;
305
Sujithb77f4832008-12-07 21:44:03 +0530306 spin_lock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530307
Sujithb77f4832008-12-07 21:44:03 +0530308 if (unlikely(list_empty(&sc->tx.txbuf))) {
309 spin_unlock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530310 return NULL;
311 }
312
Sujithb77f4832008-12-07 21:44:03 +0530313 bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
Sujith528f0c62008-10-29 10:14:26 +0530314 list_del(&bf->list);
315
Sujithb77f4832008-12-07 21:44:03 +0530316 spin_unlock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530317
318 return bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700319}
320
321/* To complete a chain of buffers associated a frame */
322
323static void ath_tx_complete_buf(struct ath_softc *sc,
324 struct ath_buf *bf,
325 struct list_head *bf_q,
326 int txok, int sendbar)
327{
328 struct sk_buff *skb = bf->bf_mpdu;
329 struct ath_xmit_status tx_status;
Senthil Balasubramaniana07d3612008-12-09 17:23:33 +0530330 unsigned long flags;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700331
332 /*
333 * Set retry information.
334 * NB: Don't use the information in the descriptor, because the frame
335 * could be software retried.
336 */
337 tx_status.retries = bf->bf_retries;
338 tx_status.flags = 0;
339
340 if (sendbar)
341 tx_status.flags = ATH_TX_BAR;
342
343 if (!txok) {
344 tx_status.flags |= ATH_TX_ERROR;
345
Sujithcd3d39a2008-08-11 14:03:34 +0530346 if (bf_isxretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700347 tx_status.flags |= ATH_TX_XRETRY;
348 }
Sujith102e0572008-10-29 10:15:16 +0530349
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700350 /* Unmap this frame */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700351 pci_unmap_single(sc->pdev,
Sujithff9b6622008-08-14 13:27:16 +0530352 bf->bf_dmacontext,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700353 skb->len,
354 PCI_DMA_TODEVICE);
355 /* complete this frame */
Sujith528f0c62008-10-29 10:14:26 +0530356 ath_tx_complete(sc, skb, &tx_status);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700357
358 /*
359 * Return the list of ath_buf of this mpdu to free queue
360 */
Sujithb77f4832008-12-07 21:44:03 +0530361 spin_lock_irqsave(&sc->tx.txbuflock, flags);
362 list_splice_tail_init(bf_q, &sc->tx.txbuf);
363 spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700364}
365
366/*
367 * queue up a dest/ac pair for tx scheduling
368 * NB: must be called with txq lock held
369 */
370
371static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
372{
373 struct ath_atx_ac *ac = tid->ac;
374
375 /*
376 * if tid is paused, hold off
377 */
378 if (tid->paused)
379 return;
380
381 /*
382 * add tid to ac atmost once
383 */
384 if (tid->sched)
385 return;
386
387 tid->sched = true;
388 list_add_tail(&tid->list, &ac->tid_q);
389
390 /*
391 * add node ac to txq atmost once
392 */
393 if (ac->sched)
394 return;
395
396 ac->sched = true;
397 list_add_tail(&ac->list, &txq->axq_acq);
398}
399
400/* pause a tid */
401
402static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
403{
Sujithb77f4832008-12-07 21:44:03 +0530404 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700405
406 spin_lock_bh(&txq->axq_lock);
407
408 tid->paused++;
409
410 spin_unlock_bh(&txq->axq_lock);
411}
412
413/* resume a tid and schedule aggregate */
414
415void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
416{
Sujithb77f4832008-12-07 21:44:03 +0530417 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700418
419 ASSERT(tid->paused > 0);
420 spin_lock_bh(&txq->axq_lock);
421
422 tid->paused--;
423
424 if (tid->paused > 0)
425 goto unlock;
426
427 if (list_empty(&tid->buf_q))
428 goto unlock;
429
430 /*
431 * Add this TID to scheduler and try to send out aggregates
432 */
433 ath_tx_queue_tid(txq, tid);
434 ath_txq_schedule(sc, txq);
435unlock:
436 spin_unlock_bh(&txq->axq_lock);
437}
438
439/* Compute the number of bad frames */
440
Sujithb5aa9bf2008-10-29 10:13:31 +0530441static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
442 int txok)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700443{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700444 struct ath_buf *bf_last = bf->bf_lastbf;
445 struct ath_desc *ds = bf_last->bf_desc;
446 u16 seq_st = 0;
447 u32 ba[WME_BA_BMP_SIZE >> 5];
448 int ba_index;
449 int nbad = 0;
450 int isaggr = 0;
451
Sujithb5aa9bf2008-10-29 10:13:31 +0530452 if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700453 return 0;
454
Sujithcd3d39a2008-08-11 14:03:34 +0530455 isaggr = bf_isaggr(bf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700456 if (isaggr) {
457 seq_st = ATH_DS_BA_SEQ(ds);
458 memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
459 }
460
461 while (bf) {
462 ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
463 if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
464 nbad++;
465
466 bf = bf->bf_next;
467 }
468
469 return nbad;
470}
471
472static void ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
473{
474 struct sk_buff *skb;
475 struct ieee80211_hdr *hdr;
476
Sujithcd3d39a2008-08-11 14:03:34 +0530477 bf->bf_state.bf_type |= BUF_RETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700478 bf->bf_retries++;
479
480 skb = bf->bf_mpdu;
481 hdr = (struct ieee80211_hdr *)skb->data;
482 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
483}
484
485/* Update block ack window */
486
Sujith102e0572008-10-29 10:15:16 +0530487static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
488 int seqno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700489{
490 int index, cindex;
491
492 index = ATH_BA_INDEX(tid->seq_start, seqno);
493 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
494
495 tid->tx_buf[cindex] = NULL;
496
497 while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
498 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
499 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
500 }
501}
502
503/*
504 * ath_pkt_dur - compute packet duration (NB: not NAV)
505 *
506 * rix - rate index
507 * pktlen - total bytes (delims + data + fcs + pads + pad delims)
508 * width - 0 for 20 MHz, 1 for 40 MHz
509 * half_gi - to use 4us v/s 3.6 us for symbol time
510 */
Sujith102e0572008-10-29 10:15:16 +0530511static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
512 int width, int half_gi, bool shortPreamble)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700513{
Sujith3706de62008-12-07 21:42:10 +0530514 struct ath_rate_table *rate_table = sc->cur_rate_table;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700515 u32 nbits, nsymbits, duration, nsymbols;
516 u8 rc;
517 int streams, pktlen;
518
Sujithcd3d39a2008-08-11 14:03:34 +0530519 pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
Sujithe63835b2008-11-18 09:07:53 +0530520 rc = rate_table->info[rix].ratecode;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700521
Sujithe63835b2008-11-18 09:07:53 +0530522 /* for legacy rates, use old function to compute packet duration */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700523 if (!IS_HT_RATE(rc))
Sujithe63835b2008-11-18 09:07:53 +0530524 return ath9k_hw_computetxtime(sc->sc_ah, rate_table, pktlen,
525 rix, shortPreamble);
526
527 /* find number of symbols: PLCP + data */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700528 nbits = (pktlen << 3) + OFDM_PLCP_BITS;
529 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
530 nsymbols = (nbits + nsymbits - 1) / nsymbits;
531
532 if (!half_gi)
533 duration = SYMBOL_TIME(nsymbols);
534 else
535 duration = SYMBOL_TIME_HALFGI(nsymbols);
536
Sujithe63835b2008-11-18 09:07:53 +0530537 /* addup duration for legacy/ht training and signal fields */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700538 streams = HT_RC_2_STREAMS(rc);
539 duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
Sujith102e0572008-10-29 10:15:16 +0530540
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700541 return duration;
542}
543
544/* Rate module function to set rate related fields in tx descriptor */
545
546static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
547{
548 struct ath_hal *ah = sc->sc_ah;
Sujithe63835b2008-11-18 09:07:53 +0530549 struct ath_rate_table *rt;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700550 struct ath_desc *ds = bf->bf_desc;
551 struct ath_desc *lastds = bf->bf_lastbf->bf_desc;
552 struct ath9k_11n_rate_series series[4];
Sujith528f0c62008-10-29 10:14:26 +0530553 struct sk_buff *skb;
554 struct ieee80211_tx_info *tx_info;
Sujitha8efee42008-11-18 09:07:30 +0530555 struct ieee80211_tx_rate *rates;
Sujithe63835b2008-11-18 09:07:53 +0530556 struct ieee80211_hdr *hdr;
557 int i, flags, rtsctsena = 0;
558 u32 ctsduration = 0;
559 u8 rix = 0, cix, ctsrate = 0;
560 __le16 fc;
561
562 memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
Sujith528f0c62008-10-29 10:14:26 +0530563
564 skb = (struct sk_buff *)bf->bf_mpdu;
Sujithe63835b2008-11-18 09:07:53 +0530565 hdr = (struct ieee80211_hdr *)skb->data;
566 fc = hdr->frame_control;
Sujith528f0c62008-10-29 10:14:26 +0530567 tx_info = IEEE80211_SKB_CB(skb);
Sujithe63835b2008-11-18 09:07:53 +0530568 rates = tx_info->control.rates;
Sujith528f0c62008-10-29 10:14:26 +0530569
Sujithe63835b2008-11-18 09:07:53 +0530570 if (ieee80211_has_morefrags(fc) ||
571 (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)) {
572 rates[1].count = rates[2].count = rates[3].count = 0;
573 rates[1].idx = rates[2].idx = rates[3].idx = 0;
574 rates[0].count = ATH_TXMAXTRY;
575 }
576
577 /* get the cix for the lowest valid rix */
Sujith3706de62008-12-07 21:42:10 +0530578 rt = sc->cur_rate_table;
Sujitha8efee42008-11-18 09:07:30 +0530579 for (i = 3; i >= 0; i--) {
Sujithe63835b2008-11-18 09:07:53 +0530580 if (rates[i].count && (rates[i].idx >= 0)) {
Sujitha8efee42008-11-18 09:07:30 +0530581 rix = rates[i].idx;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700582 break;
583 }
584 }
Sujithe63835b2008-11-18 09:07:53 +0530585
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700586 flags = (bf->bf_flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA));
Sujithe63835b2008-11-18 09:07:53 +0530587 cix = rt->info[rix].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700588
589 /*
Sujithe63835b2008-11-18 09:07:53 +0530590 * If 802.11g protection is enabled, determine whether to use RTS/CTS or
591 * just CTS. Note that this is only done for OFDM/HT unicast frames.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700592 */
Sujithe63835b2008-11-18 09:07:53 +0530593 if (sc->sc_protmode != PROT_M_NONE && !(bf->bf_flags & ATH9K_TXDESC_NOACK)
Sujith46d14a52008-11-18 09:08:13 +0530594 && (rt->info[rix].phy == WLAN_RC_PHY_OFDM ||
Sujithe63835b2008-11-18 09:07:53 +0530595 WLAN_RC_PHY_HT(rt->info[rix].phy))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700596 if (sc->sc_protmode == PROT_M_RTSCTS)
597 flags = ATH9K_TXDESC_RTSENA;
598 else if (sc->sc_protmode == PROT_M_CTSONLY)
599 flags = ATH9K_TXDESC_CTSENA;
600
Sujithe63835b2008-11-18 09:07:53 +0530601 cix = rt->info[sc->sc_protrix].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700602 rtsctsena = 1;
603 }
604
Sujithe63835b2008-11-18 09:07:53 +0530605 /* For 11n, the default behavior is to enable RTS for hw retried frames.
606 * We enable the global flag here and let rate series flags determine
607 * which rates will actually use RTS.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700608 */
Sujithcd3d39a2008-08-11 14:03:34 +0530609 if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) && bf_isdata(bf)) {
Sujithe63835b2008-11-18 09:07:53 +0530610 /* 802.11g protection not needed, use our default behavior */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700611 if (!rtsctsena)
612 flags = ATH9K_TXDESC_RTSENA;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700613 }
614
Sujithe63835b2008-11-18 09:07:53 +0530615 /* Set protection if aggregate protection on */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700616 if (sc->sc_config.ath_aggr_prot &&
Sujithcd3d39a2008-08-11 14:03:34 +0530617 (!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700618 flags = ATH9K_TXDESC_RTSENA;
Sujithe63835b2008-11-18 09:07:53 +0530619 cix = rt->info[sc->sc_protrix].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700620 rtsctsena = 1;
621 }
622
Sujithe63835b2008-11-18 09:07:53 +0530623 /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
624 if (bf_isaggr(bf) && (bf->bf_al > ah->ah_caps.rts_aggr_limit))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700625 flags &= ~(ATH9K_TXDESC_RTSENA);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700626
627 /*
Sujithe63835b2008-11-18 09:07:53 +0530628 * CTS transmit rate is derived from the transmit rate by looking in the
629 * h/w rate table. We must also factor in whether or not a short
630 * preamble is to be used. NB: cix is set above where RTS/CTS is enabled
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700631 */
Sujithe63835b2008-11-18 09:07:53 +0530632 ctsrate = rt->info[cix].ratecode |
633 (bf_isshpreamble(bf) ? rt->info[cix].short_preamble : 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700634
635 for (i = 0; i < 4; i++) {
Sujithe63835b2008-11-18 09:07:53 +0530636 if (!rates[i].count || (rates[i].idx < 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700637 continue;
638
Sujitha8efee42008-11-18 09:07:30 +0530639 rix = rates[i].idx;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700640
Sujithe63835b2008-11-18 09:07:53 +0530641 series[i].Rate = rt->info[rix].ratecode |
642 (bf_isshpreamble(bf) ? rt->info[rix].short_preamble : 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700643
Sujitha8efee42008-11-18 09:07:30 +0530644 series[i].Tries = rates[i].count;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700645
646 series[i].RateFlags = (
Sujitha8efee42008-11-18 09:07:30 +0530647 (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700648 ATH9K_RATESERIES_RTS_CTS : 0) |
Sujitha8efee42008-11-18 09:07:30 +0530649 ((rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700650 ATH9K_RATESERIES_2040 : 0) |
Sujitha8efee42008-11-18 09:07:30 +0530651 ((rates[i].flags & IEEE80211_TX_RC_SHORT_GI) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700652 ATH9K_RATESERIES_HALFGI : 0);
653
Sujith102e0572008-10-29 10:15:16 +0530654 series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
Sujitha8efee42008-11-18 09:07:30 +0530655 (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) != 0,
656 (rates[i].flags & IEEE80211_TX_RC_SHORT_GI),
Sujith102e0572008-10-29 10:15:16 +0530657 bf_isshpreamble(bf));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700658
Sujithff37e332008-11-24 12:07:55 +0530659 series[i].ChSel = sc->sc_tx_chainmask;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700660
661 if (rtsctsena)
662 series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700663 }
664
Sujithe63835b2008-11-18 09:07:53 +0530665 /* set dur_update_en for l-sig computation except for PS-Poll frames */
666 ath9k_hw_set11n_ratescenario(ah, ds, lastds, !bf_ispspoll(bf),
667 ctsrate, ctsduration,
Sujithcd3d39a2008-08-11 14:03:34 +0530668 series, 4, flags);
Sujith102e0572008-10-29 10:15:16 +0530669
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700670 if (sc->sc_config.ath_aggr_prot && flags)
671 ath9k_hw_set11n_burstduration(ah, ds, 8192);
672}
673
674/*
675 * Function to send a normal HT (non-AMPDU) frame
676 * NB: must be called with txq lock held
677 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700678static int ath_tx_send_normal(struct ath_softc *sc,
679 struct ath_txq *txq,
680 struct ath_atx_tid *tid,
681 struct list_head *bf_head)
682{
683 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700684
685 BUG_ON(list_empty(bf_head));
686
687 bf = list_first_entry(bf_head, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +0530688 bf->bf_state.bf_type &= ~BUF_AMPDU; /* regular HT frame */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700689
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700690 /* update starting sequence number for subsequent ADDBA request */
691 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
692
693 /* Queue to h/w without aggregation */
694 bf->bf_nframes = 1;
695 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
696 ath_buf_set_rate(sc, bf);
697 ath_tx_txqaddbuf(sc, txq, bf_head);
698
699 return 0;
700}
701
702/* flush tid's software queue and send frames as non-ampdu's */
703
704static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
705{
Sujithb77f4832008-12-07 21:44:03 +0530706 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700707 struct ath_buf *bf;
708 struct list_head bf_head;
709 INIT_LIST_HEAD(&bf_head);
710
711 ASSERT(tid->paused > 0);
712 spin_lock_bh(&txq->axq_lock);
713
714 tid->paused--;
715
716 if (tid->paused > 0) {
717 spin_unlock_bh(&txq->axq_lock);
718 return;
719 }
720
721 while (!list_empty(&tid->buf_q)) {
722 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +0530723 ASSERT(!bf_isretried(bf));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700724 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
725 ath_tx_send_normal(sc, txq, tid, &bf_head);
726 }
727
728 spin_unlock_bh(&txq->axq_lock);
729}
730
731/* Completion routine of an aggregate */
732
733static void ath_tx_complete_aggr_rifs(struct ath_softc *sc,
734 struct ath_txq *txq,
735 struct ath_buf *bf,
736 struct list_head *bf_q,
737 int txok)
738{
Sujith528f0c62008-10-29 10:14:26 +0530739 struct ath_node *an = NULL;
740 struct sk_buff *skb;
741 struct ieee80211_tx_info *tx_info;
742 struct ath_atx_tid *tid = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700743 struct ath_buf *bf_last = bf->bf_lastbf;
744 struct ath_desc *ds = bf_last->bf_desc;
745 struct ath_buf *bf_next, *bf_lastq = NULL;
746 struct list_head bf_head, bf_pending;
747 u16 seq_st = 0;
748 u32 ba[WME_BA_BMP_SIZE >> 5];
749 int isaggr, txfail, txpending, sendbar = 0, needreset = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700750
Sujith528f0c62008-10-29 10:14:26 +0530751 skb = (struct sk_buff *)bf->bf_mpdu;
752 tx_info = IEEE80211_SKB_CB(skb);
753
754 if (tx_info->control.sta) {
755 an = (struct ath_node *)tx_info->control.sta->drv_priv;
756 tid = ATH_AN_2_TID(an, bf->bf_tidno);
757 }
758
Sujithcd3d39a2008-08-11 14:03:34 +0530759 isaggr = bf_isaggr(bf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700760 if (isaggr) {
761 if (txok) {
762 if (ATH_DS_TX_BA(ds)) {
763 /*
764 * extract starting sequence and
765 * block-ack bitmap
766 */
767 seq_st = ATH_DS_BA_SEQ(ds);
768 memcpy(ba,
769 ATH_DS_BA_BITMAP(ds),
770 WME_BA_BMP_SIZE >> 3);
771 } else {
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700772 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700773
774 /*
775 * AR5416 can become deaf/mute when BA
776 * issue happens. Chip needs to be reset.
777 * But AP code may have sychronization issues
778 * when perform internal reset in this routine.
779 * Only enable reset in STA mode for now.
780 */
Colin McCabed97809d2008-12-01 13:38:55 -0800781 if (sc->sc_ah->ah_opmode ==
782 NL80211_IFTYPE_STATION)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700783 needreset = 1;
784 }
785 } else {
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700786 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700787 }
788 }
789
790 INIT_LIST_HEAD(&bf_pending);
791 INIT_LIST_HEAD(&bf_head);
792
793 while (bf) {
794 txfail = txpending = 0;
795 bf_next = bf->bf_next;
796
797 if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
798 /* transmit completion, subframe is
799 * acked by block ack */
800 } else if (!isaggr && txok) {
801 /* transmit completion */
802 } else {
803
Sujitha37c2c72008-10-29 10:15:40 +0530804 if (!(tid->state & AGGR_CLEANUP) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700805 ds->ds_txstat.ts_flags != ATH9K_TX_SW_ABORTED) {
806 if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
807 ath_tx_set_retry(sc, bf);
808 txpending = 1;
809 } else {
Sujithcd3d39a2008-08-11 14:03:34 +0530810 bf->bf_state.bf_type |= BUF_XRETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700811 txfail = 1;
812 sendbar = 1;
813 }
814 } else {
815 /*
816 * cleanup in progress, just fail
817 * the un-acked sub-frames
818 */
819 txfail = 1;
820 }
821 }
822 /*
823 * Remove ath_buf's of this sub-frame from aggregate queue.
824 */
825 if (bf_next == NULL) { /* last subframe in the aggregate */
826 ASSERT(bf->bf_lastfrm == bf_last);
827
828 /*
829 * The last descriptor of the last sub frame could be
830 * a holding descriptor for h/w. If that's the case,
831 * bf->bf_lastfrm won't be in the bf_q.
832 * Make sure we handle bf_q properly here.
833 */
834
835 if (!list_empty(bf_q)) {
836 bf_lastq = list_entry(bf_q->prev,
837 struct ath_buf, list);
838 list_cut_position(&bf_head,
839 bf_q, &bf_lastq->list);
840 } else {
841 /*
842 * XXX: if the last subframe only has one
843 * descriptor which is also being used as
844 * a holding descriptor. Then the ath_buf
845 * is not in the bf_q at all.
846 */
847 INIT_LIST_HEAD(&bf_head);
848 }
849 } else {
850 ASSERT(!list_empty(bf_q));
851 list_cut_position(&bf_head,
852 bf_q, &bf->bf_lastfrm->list);
853 }
854
855 if (!txpending) {
856 /*
857 * complete the acked-ones/xretried ones; update
858 * block-ack window
859 */
860 spin_lock_bh(&txq->axq_lock);
861 ath_tx_update_baw(sc, tid, bf->bf_seqno);
862 spin_unlock_bh(&txq->axq_lock);
863
864 /* complete this sub-frame */
865 ath_tx_complete_buf(sc, bf, &bf_head, !txfail, sendbar);
866 } else {
867 /*
868 * retry the un-acked ones
869 */
870 /*
871 * XXX: if the last descriptor is holding descriptor,
872 * in order to requeue the frame to software queue, we
873 * need to allocate a new descriptor and
874 * copy the content of holding descriptor to it.
875 */
876 if (bf->bf_next == NULL &&
877 bf_last->bf_status & ATH_BUFSTATUS_STALE) {
878 struct ath_buf *tbf;
879
880 /* allocate new descriptor */
Sujithb77f4832008-12-07 21:44:03 +0530881 spin_lock_bh(&sc->tx.txbuflock);
882 ASSERT(!list_empty((&sc->tx.txbuf)));
883 tbf = list_first_entry(&sc->tx.txbuf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700884 struct ath_buf, list);
885 list_del(&tbf->list);
Sujithb77f4832008-12-07 21:44:03 +0530886 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700887
888 ATH_TXBUF_RESET(tbf);
889
890 /* copy descriptor content */
891 tbf->bf_mpdu = bf_last->bf_mpdu;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700892 tbf->bf_buf_addr = bf_last->bf_buf_addr;
893 *(tbf->bf_desc) = *(bf_last->bf_desc);
894
895 /* link it to the frame */
896 if (bf_lastq) {
897 bf_lastq->bf_desc->ds_link =
898 tbf->bf_daddr;
899 bf->bf_lastfrm = tbf;
900 ath9k_hw_cleartxdesc(sc->sc_ah,
901 bf->bf_lastfrm->bf_desc);
902 } else {
903 tbf->bf_state = bf_last->bf_state;
904 tbf->bf_lastfrm = tbf;
905 ath9k_hw_cleartxdesc(sc->sc_ah,
906 tbf->bf_lastfrm->bf_desc);
907
908 /* copy the DMA context */
Sujithff9b6622008-08-14 13:27:16 +0530909 tbf->bf_dmacontext =
910 bf_last->bf_dmacontext;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700911 }
912 list_add_tail(&tbf->list, &bf_head);
913 } else {
914 /*
915 * Clear descriptor status words for
916 * software retry
917 */
918 ath9k_hw_cleartxdesc(sc->sc_ah,
Sujithff9b6622008-08-14 13:27:16 +0530919 bf->bf_lastfrm->bf_desc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700920 }
921
922 /*
923 * Put this buffer to the temporary pending
924 * queue to retain ordering
925 */
926 list_splice_tail_init(&bf_head, &bf_pending);
927 }
928
929 bf = bf_next;
930 }
931
Sujitha37c2c72008-10-29 10:15:40 +0530932 if (tid->state & AGGR_CLEANUP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700933 /* check to see if we're done with cleaning the h/w queue */
934 spin_lock_bh(&txq->axq_lock);
935
936 if (tid->baw_head == tid->baw_tail) {
Sujitha37c2c72008-10-29 10:15:40 +0530937 tid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700938 tid->addba_exchangeattempts = 0;
939 spin_unlock_bh(&txq->axq_lock);
940
Sujitha37c2c72008-10-29 10:15:40 +0530941 tid->state &= ~AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700942
943 /* send buffered frames as singles */
944 ath_tx_flush_tid(sc, tid);
945 } else
946 spin_unlock_bh(&txq->axq_lock);
947
948 return;
949 }
950
951 /*
952 * prepend un-acked frames to the beginning of the pending frame queue
953 */
954 if (!list_empty(&bf_pending)) {
955 spin_lock_bh(&txq->axq_lock);
956 /* Note: we _prepend_, we _do_not_ at to
957 * the end of the queue ! */
958 list_splice(&bf_pending, &tid->buf_q);
959 ath_tx_queue_tid(txq, tid);
960 spin_unlock_bh(&txq->axq_lock);
961 }
962
963 if (needreset)
Sujithf45144e2008-08-11 14:02:53 +0530964 ath_reset(sc, false);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700965
966 return;
967}
968
Sujithc4288392008-11-18 09:09:30 +0530969static void ath_tx_rc_status(struct ath_buf *bf, struct ath_desc *ds, int nbad)
970{
971 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
972 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
973 struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
974
Vasanthakumar Thiagarajan7ac47012008-11-20 11:51:18 +0530975 tx_info_priv->update_rc = false;
Sujithc4288392008-11-18 09:09:30 +0530976 if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
977 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
978
979 if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
980 (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
981 if (bf_isdata(bf)) {
982 memcpy(&tx_info_priv->tx, &ds->ds_txstat,
983 sizeof(tx_info_priv->tx));
984 tx_info_priv->n_frames = bf->bf_nframes;
985 tx_info_priv->n_bad_frames = nbad;
Vasanthakumar Thiagarajan7ac47012008-11-20 11:51:18 +0530986 tx_info_priv->update_rc = true;
Sujithc4288392008-11-18 09:09:30 +0530987 }
988 }
989}
990
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700991/* Process completed xmit descriptors from the specified queue */
992
Sujithc4288392008-11-18 09:09:30 +0530993static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700994{
995 struct ath_hal *ah = sc->sc_ah;
996 struct ath_buf *bf, *lastbf, *bf_held = NULL;
997 struct list_head bf_head;
Sujithc4288392008-11-18 09:09:30 +0530998 struct ath_desc *ds;
999 int txok, nbad = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001000 int status;
1001
Sujith04bd46382008-11-28 22:18:05 +05301002 DPRINTF(sc, ATH_DBG_QUEUE, "tx queue %d (%x), link %p\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001003 txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
1004 txq->axq_link);
1005
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001006 for (;;) {
1007 spin_lock_bh(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001008 if (list_empty(&txq->axq_q)) {
1009 txq->axq_link = NULL;
1010 txq->axq_linkbuf = NULL;
1011 spin_unlock_bh(&txq->axq_lock);
1012 break;
1013 }
1014 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
1015
1016 /*
1017 * There is a race condition that a BH gets scheduled
1018 * after sw writes TxE and before hw re-load the last
1019 * descriptor to get the newly chained one.
1020 * Software must keep the last DONE descriptor as a
1021 * holding descriptor - software does so by marking
1022 * it with the STALE flag.
1023 */
1024 bf_held = NULL;
1025 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
1026 bf_held = bf;
1027 if (list_is_last(&bf_held->list, &txq->axq_q)) {
1028 /* FIXME:
1029 * The holding descriptor is the last
1030 * descriptor in queue. It's safe to remove
1031 * the last holding descriptor in BH context.
1032 */
1033 spin_unlock_bh(&txq->axq_lock);
1034 break;
1035 } else {
1036 /* Lets work with the next buffer now */
1037 bf = list_entry(bf_held->list.next,
1038 struct ath_buf, list);
1039 }
1040 }
1041
1042 lastbf = bf->bf_lastbf;
1043 ds = lastbf->bf_desc; /* NB: last decriptor */
1044
1045 status = ath9k_hw_txprocdesc(ah, ds);
1046 if (status == -EINPROGRESS) {
1047 spin_unlock_bh(&txq->axq_lock);
1048 break;
1049 }
1050 if (bf->bf_desc == txq->axq_lastdsWithCTS)
1051 txq->axq_lastdsWithCTS = NULL;
1052 if (ds == txq->axq_gatingds)
1053 txq->axq_gatingds = NULL;
1054
1055 /*
1056 * Remove ath_buf's of the same transmit unit from txq,
1057 * however leave the last descriptor back as the holding
1058 * descriptor for hw.
1059 */
1060 lastbf->bf_status |= ATH_BUFSTATUS_STALE;
1061 INIT_LIST_HEAD(&bf_head);
1062
1063 if (!list_is_singular(&lastbf->list))
1064 list_cut_position(&bf_head,
1065 &txq->axq_q, lastbf->list.prev);
1066
1067 txq->axq_depth--;
1068
Sujithcd3d39a2008-08-11 14:03:34 +05301069 if (bf_isaggr(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001070 txq->axq_aggr_depth--;
1071
1072 txok = (ds->ds_txstat.ts_status == 0);
1073
1074 spin_unlock_bh(&txq->axq_lock);
1075
1076 if (bf_held) {
1077 list_del(&bf_held->list);
Sujithb77f4832008-12-07 21:44:03 +05301078 spin_lock_bh(&sc->tx.txbuflock);
1079 list_add_tail(&bf_held->list, &sc->tx.txbuf);
1080 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001081 }
1082
Sujithcd3d39a2008-08-11 14:03:34 +05301083 if (!bf_isampdu(bf)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001084 /*
1085 * This frame is sent out as a single frame.
1086 * Use hardware retry status for this frame.
1087 */
1088 bf->bf_retries = ds->ds_txstat.ts_longretry;
1089 if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
Sujithcd3d39a2008-08-11 14:03:34 +05301090 bf->bf_state.bf_type |= BUF_XRETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001091 nbad = 0;
1092 } else {
1093 nbad = ath_tx_num_badfrms(sc, bf, txok);
1094 }
Johannes Berge6a98542008-10-21 12:40:02 +02001095
Sujithc4288392008-11-18 09:09:30 +05301096 ath_tx_rc_status(bf, ds, nbad);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001097
1098 /*
1099 * Complete this transmit unit
1100 */
Sujithcd3d39a2008-08-11 14:03:34 +05301101 if (bf_isampdu(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001102 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, txok);
1103 else
1104 ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
1105
1106 /* Wake up mac80211 queue */
1107
1108 spin_lock_bh(&txq->axq_lock);
1109 if (txq->stopped && ath_txq_depth(sc, txq->axq_qnum) <=
1110 (ATH_TXBUF - 20)) {
1111 int qnum;
1112 qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
1113 if (qnum != -1) {
1114 ieee80211_wake_queue(sc->hw, qnum);
1115 txq->stopped = 0;
1116 }
1117
1118 }
1119
1120 /*
1121 * schedule any pending packets if aggregation is enabled
1122 */
Sujith672840a2008-08-11 14:05:08 +05301123 if (sc->sc_flags & SC_OP_TXAGGR)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001124 ath_txq_schedule(sc, txq);
1125 spin_unlock_bh(&txq->axq_lock);
1126 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001127}
1128
1129static void ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
1130{
1131 struct ath_hal *ah = sc->sc_ah;
1132
1133 (void) ath9k_hw_stoptxdma(ah, txq->axq_qnum);
Sujith04bd46382008-11-28 22:18:05 +05301134 DPRINTF(sc, ATH_DBG_XMIT, "tx queue [%u] %x, link %p\n",
1135 txq->axq_qnum, ath9k_hw_gettxbuf(ah, txq->axq_qnum),
1136 txq->axq_link);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001137}
1138
1139/* Drain only the data queues */
1140
1141static void ath_drain_txdataq(struct ath_softc *sc, bool retry_tx)
1142{
1143 struct ath_hal *ah = sc->sc_ah;
Sujith102e0572008-10-29 10:15:16 +05301144 int i, status, npend = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001145
Sujith672840a2008-08-11 14:05:08 +05301146 if (!(sc->sc_flags & SC_OP_INVALID)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001147 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1148 if (ATH_TXQ_SETUP(sc, i)) {
Sujithb77f4832008-12-07 21:44:03 +05301149 ath_tx_stopdma(sc, &sc->tx.txq[i]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001150 /* The TxDMA may not really be stopped.
1151 * Double check the hal tx pending count */
1152 npend += ath9k_hw_numtxpending(ah,
Sujithb77f4832008-12-07 21:44:03 +05301153 sc->tx.txq[i].axq_qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001154 }
1155 }
1156 }
1157
1158 if (npend) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001159 /* TxDMA not stopped, reset the hal */
Sujith04bd46382008-11-28 22:18:05 +05301160 DPRINTF(sc, ATH_DBG_XMIT, "Unable to stop TxDMA. Reset HAL!\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001161
1162 spin_lock_bh(&sc->sc_resetlock);
Sujithb4696c8b2008-08-11 14:04:52 +05301163 if (!ath9k_hw_reset(ah,
Sujith927e70e2008-08-14 13:26:34 +05301164 sc->sc_ah->ah_curchan,
Sujith99405f92008-11-24 12:08:35 +05301165 sc->tx_chan_width,
Sujith927e70e2008-08-14 13:26:34 +05301166 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
1167 sc->sc_ht_extprotspacing, true, &status)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001168
1169 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301170 "Unable to reset hardware; hal status %u\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001171 status);
1172 }
1173 spin_unlock_bh(&sc->sc_resetlock);
1174 }
1175
1176 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1177 if (ATH_TXQ_SETUP(sc, i))
Sujithb77f4832008-12-07 21:44:03 +05301178 ath_tx_draintxq(sc, &sc->tx.txq[i], retry_tx);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001179 }
1180}
1181
1182/* Add a sub-frame to block ack window */
1183
1184static void ath_tx_addto_baw(struct ath_softc *sc,
1185 struct ath_atx_tid *tid,
1186 struct ath_buf *bf)
1187{
1188 int index, cindex;
1189
Sujithcd3d39a2008-08-11 14:03:34 +05301190 if (bf_isretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001191 return;
1192
1193 index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
1194 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
1195
1196 ASSERT(tid->tx_buf[cindex] == NULL);
1197 tid->tx_buf[cindex] = bf;
1198
1199 if (index >= ((tid->baw_tail - tid->baw_head) &
1200 (ATH_TID_MAX_BUFS - 1))) {
1201 tid->baw_tail = cindex;
1202 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
1203 }
1204}
1205
1206/*
1207 * Function to send an A-MPDU
1208 * NB: must be called with txq lock held
1209 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001210static int ath_tx_send_ampdu(struct ath_softc *sc,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001211 struct ath_atx_tid *tid,
1212 struct list_head *bf_head,
1213 struct ath_tx_control *txctl)
1214{
1215 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001216
1217 BUG_ON(list_empty(bf_head));
1218
1219 bf = list_first_entry(bf_head, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +05301220 bf->bf_state.bf_type |= BUF_AMPDU;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001221
1222 /*
1223 * Do not queue to h/w when any of the following conditions is true:
1224 * - there are pending frames in software queue
1225 * - the TID is currently paused for ADDBA/BAR request
1226 * - seqno is not within block-ack window
1227 * - h/w queue depth exceeds low water mark
1228 */
1229 if (!list_empty(&tid->buf_q) || tid->paused ||
1230 !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
Sujith528f0c62008-10-29 10:14:26 +05301231 txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001232 /*
1233 * Add this frame to software queue for scheduling later
1234 * for aggregation.
1235 */
1236 list_splice_tail_init(bf_head, &tid->buf_q);
Sujith528f0c62008-10-29 10:14:26 +05301237 ath_tx_queue_tid(txctl->txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001238 return 0;
1239 }
1240
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001241 /* Add sub-frame to BAW */
1242 ath_tx_addto_baw(sc, tid, bf);
1243
1244 /* Queue to h/w without aggregation */
1245 bf->bf_nframes = 1;
1246 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
1247 ath_buf_set_rate(sc, bf);
Sujith528f0c62008-10-29 10:14:26 +05301248 ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
Sujith102e0572008-10-29 10:15:16 +05301249
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001250 return 0;
1251}
1252
1253/*
1254 * looks up the rate
1255 * returns aggr limit based on lowest of the rates
1256 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001257static u32 ath_lookup_rate(struct ath_softc *sc,
Johannes Bergae5eb022008-10-14 16:58:37 +02001258 struct ath_buf *bf,
1259 struct ath_atx_tid *tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001260{
Sujith3706de62008-12-07 21:42:10 +05301261 struct ath_rate_table *rate_table = sc->cur_rate_table;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001262 struct sk_buff *skb;
1263 struct ieee80211_tx_info *tx_info;
Sujitha8efee42008-11-18 09:07:30 +05301264 struct ieee80211_tx_rate *rates;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001265 struct ath_tx_info_priv *tx_info_priv;
1266 u32 max_4ms_framelen, frame_length;
1267 u16 aggr_limit, legacy = 0, maxampdu;
1268 int i;
1269
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001270 skb = (struct sk_buff *)bf->bf_mpdu;
1271 tx_info = IEEE80211_SKB_CB(skb);
Sujitha8efee42008-11-18 09:07:30 +05301272 rates = tx_info->control.rates;
1273 tx_info_priv =
1274 (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001275
1276 /*
1277 * Find the lowest frame length among the rate series that will have a
1278 * 4ms transmit duration.
1279 * TODO - TXOP limit needs to be considered.
1280 */
1281 max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
1282
1283 for (i = 0; i < 4; i++) {
Sujitha8efee42008-11-18 09:07:30 +05301284 if (rates[i].count) {
Sujithe63835b2008-11-18 09:07:53 +05301285 if (!WLAN_RC_PHY_HT(rate_table->info[rates[i].idx].phy)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001286 legacy = 1;
1287 break;
1288 }
1289
Sujitha8efee42008-11-18 09:07:30 +05301290 frame_length =
1291 rate_table->info[rates[i].idx].max_4ms_framelen;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001292 max_4ms_framelen = min(max_4ms_framelen, frame_length);
1293 }
1294 }
1295
1296 /*
1297 * limit aggregate size by the minimum rate if rate selected is
1298 * not a probe rate, if rate selected is a probe rate then
1299 * avoid aggregation of this packet.
1300 */
1301 if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
1302 return 0;
1303
1304 aggr_limit = min(max_4ms_framelen,
1305 (u32)ATH_AMPDU_LIMIT_DEFAULT);
1306
1307 /*
1308 * h/w can accept aggregates upto 16 bit lengths (65535).
1309 * The IE, however can hold upto 65536, which shows up here
1310 * as zero. Ignore 65536 since we are constrained by hw.
1311 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001312 maxampdu = tid->an->maxampdu;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001313 if (maxampdu)
1314 aggr_limit = min(aggr_limit, maxampdu);
1315
1316 return aggr_limit;
1317}
1318
1319/*
1320 * returns the number of delimiters to be added to
1321 * meet the minimum required mpdudensity.
1322 * caller should make sure that the rate is HT rate .
1323 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001324static int ath_compute_num_delims(struct ath_softc *sc,
Johannes Bergae5eb022008-10-14 16:58:37 +02001325 struct ath_atx_tid *tid,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001326 struct ath_buf *bf,
1327 u16 frmlen)
1328{
Sujith3706de62008-12-07 21:42:10 +05301329 struct ath_rate_table *rt = sc->cur_rate_table;
Sujitha8efee42008-11-18 09:07:30 +05301330 struct sk_buff *skb = bf->bf_mpdu;
1331 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001332 u32 nsymbits, nsymbols, mpdudensity;
1333 u16 minlen;
1334 u8 rc, flags, rix;
1335 int width, half_gi, ndelim, mindelim;
1336
1337 /* Select standard number of delimiters based on frame length alone */
1338 ndelim = ATH_AGGR_GET_NDELIM(frmlen);
1339
1340 /*
1341 * If encryption enabled, hardware requires some more padding between
1342 * subframes.
1343 * TODO - this could be improved to be dependent on the rate.
1344 * The hardware can keep up at lower rates, but not higher rates
1345 */
1346 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
1347 ndelim += ATH_AGGR_ENCRYPTDELIM;
1348
1349 /*
1350 * Convert desired mpdu density from microeconds to bytes based
1351 * on highest rate in rate series (i.e. first rate) to determine
1352 * required minimum length for subframe. Take into account
1353 * whether high rate is 20 or 40Mhz and half or full GI.
1354 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001355 mpdudensity = tid->an->mpdudensity;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001356
1357 /*
1358 * If there is no mpdu density restriction, no further calculation
1359 * is needed.
1360 */
1361 if (mpdudensity == 0)
1362 return ndelim;
1363
Sujitha8efee42008-11-18 09:07:30 +05301364 rix = tx_info->control.rates[0].idx;
1365 flags = tx_info->control.rates[0].flags;
Sujithe63835b2008-11-18 09:07:53 +05301366 rc = rt->info[rix].ratecode;
Sujitha8efee42008-11-18 09:07:30 +05301367 width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
1368 half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001369
1370 if (half_gi)
1371 nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
1372 else
1373 nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
1374
1375 if (nsymbols == 0)
1376 nsymbols = 1;
1377
1378 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
1379 minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
1380
1381 /* Is frame shorter than required minimum length? */
1382 if (frmlen < minlen) {
1383 /* Get the minimum number of delimiters required. */
1384 mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
1385 ndelim = max(mindelim, ndelim);
1386 }
1387
1388 return ndelim;
1389}
1390
1391/*
1392 * For aggregation from software buffer queue.
1393 * NB: must be called with txq lock held
1394 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001395static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
1396 struct ath_atx_tid *tid,
1397 struct list_head *bf_q,
1398 struct ath_buf **bf_last,
1399 struct aggr_rifs_param *param,
1400 int *prev_frames)
1401{
1402#define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
1403 struct ath_buf *bf, *tbf, *bf_first, *bf_prev = NULL;
1404 struct list_head bf_head;
1405 int rl = 0, nframes = 0, ndelim;
1406 u16 aggr_limit = 0, al = 0, bpad = 0,
1407 al_delta, h_baw = tid->baw_size / 2;
1408 enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
Sujitha8efee42008-11-18 09:07:30 +05301409 int prev_al = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001410 INIT_LIST_HEAD(&bf_head);
1411
1412 BUG_ON(list_empty(&tid->buf_q));
1413
1414 bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
1415
1416 do {
1417 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1418
1419 /*
1420 * do not step over block-ack window
1421 */
1422 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
1423 status = ATH_AGGR_BAW_CLOSED;
1424 break;
1425 }
1426
1427 if (!rl) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001428 aggr_limit = ath_lookup_rate(sc, bf, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001429 rl = 1;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001430 }
1431
1432 /*
1433 * do not exceed aggregation limit
1434 */
1435 al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
1436
1437 if (nframes && (aggr_limit <
1438 (al + bpad + al_delta + prev_al))) {
1439 status = ATH_AGGR_LIMITED;
1440 break;
1441 }
1442
1443 /*
1444 * do not exceed subframe limit
1445 */
1446 if ((nframes + *prev_frames) >=
1447 min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
1448 status = ATH_AGGR_LIMITED;
1449 break;
1450 }
1451
1452 /*
1453 * add padding for previous frame to aggregation length
1454 */
1455 al += bpad + al_delta;
1456
1457 /*
1458 * Get the delimiters needed to meet the MPDU
1459 * density for this node.
1460 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001461 ndelim = ath_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001462
1463 bpad = PADBYTES(al_delta) + (ndelim << 2);
1464
1465 bf->bf_next = NULL;
1466 bf->bf_lastfrm->bf_desc->ds_link = 0;
1467
1468 /*
1469 * this packet is part of an aggregate
1470 * - remove all descriptors belonging to this frame from
1471 * software queue
1472 * - add it to block ack window
1473 * - set up descriptors for aggregation
1474 */
1475 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1476 ath_tx_addto_baw(sc, tid, bf);
1477
1478 list_for_each_entry(tbf, &bf_head, list) {
1479 ath9k_hw_set11n_aggr_middle(sc->sc_ah,
1480 tbf->bf_desc, ndelim);
1481 }
1482
1483 /*
1484 * link buffers of this frame to the aggregate
1485 */
1486 list_splice_tail_init(&bf_head, bf_q);
1487 nframes++;
1488
1489 if (bf_prev) {
1490 bf_prev->bf_next = bf;
1491 bf_prev->bf_lastfrm->bf_desc->ds_link = bf->bf_daddr;
1492 }
1493 bf_prev = bf;
1494
1495#ifdef AGGR_NOSHORT
1496 /*
1497 * terminate aggregation on a small packet boundary
1498 */
1499 if (bf->bf_frmlen < ATH_AGGR_MINPLEN) {
1500 status = ATH_AGGR_SHORTPKT;
1501 break;
1502 }
1503#endif
1504 } while (!list_empty(&tid->buf_q));
1505
1506 bf_first->bf_al = al;
1507 bf_first->bf_nframes = nframes;
1508 *bf_last = bf_prev;
1509 return status;
1510#undef PADBYTES
1511}
1512
1513/*
1514 * process pending frames possibly doing a-mpdu aggregation
1515 * NB: must be called with txq lock held
1516 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001517static void ath_tx_sched_aggr(struct ath_softc *sc,
1518 struct ath_txq *txq, struct ath_atx_tid *tid)
1519{
1520 struct ath_buf *bf, *tbf, *bf_last, *bf_lastaggr = NULL;
1521 enum ATH_AGGR_STATUS status;
1522 struct list_head bf_q;
1523 struct aggr_rifs_param param = {0, 0, 0, 0, NULL};
1524 int prev_frames = 0;
1525
1526 do {
1527 if (list_empty(&tid->buf_q))
1528 return;
1529
1530 INIT_LIST_HEAD(&bf_q);
1531
1532 status = ath_tx_form_aggr(sc, tid, &bf_q, &bf_lastaggr, &param,
1533 &prev_frames);
1534
1535 /*
1536 * no frames picked up to be aggregated; block-ack
1537 * window is not open
1538 */
1539 if (list_empty(&bf_q))
1540 break;
1541
1542 bf = list_first_entry(&bf_q, struct ath_buf, list);
1543 bf_last = list_entry(bf_q.prev, struct ath_buf, list);
1544 bf->bf_lastbf = bf_last;
1545
1546 /*
1547 * if only one frame, send as non-aggregate
1548 */
1549 if (bf->bf_nframes == 1) {
1550 ASSERT(bf->bf_lastfrm == bf_last);
1551
Sujithcd3d39a2008-08-11 14:03:34 +05301552 bf->bf_state.bf_type &= ~BUF_AGGR;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001553 /*
1554 * clear aggr bits for every descriptor
1555 * XXX TODO: is there a way to optimize it?
1556 */
1557 list_for_each_entry(tbf, &bf_q, list) {
1558 ath9k_hw_clr11n_aggr(sc->sc_ah, tbf->bf_desc);
1559 }
1560
1561 ath_buf_set_rate(sc, bf);
1562 ath_tx_txqaddbuf(sc, txq, &bf_q);
1563 continue;
1564 }
1565
1566 /*
1567 * setup first desc with rate and aggr info
1568 */
Sujithcd3d39a2008-08-11 14:03:34 +05301569 bf->bf_state.bf_type |= BUF_AGGR;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001570 ath_buf_set_rate(sc, bf);
1571 ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
1572
1573 /*
1574 * anchor last frame of aggregate correctly
1575 */
1576 ASSERT(bf_lastaggr);
1577 ASSERT(bf_lastaggr->bf_lastfrm == bf_last);
1578 tbf = bf_lastaggr;
1579 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1580
1581 /* XXX: We don't enter into this loop, consider removing this */
1582 while (!list_empty(&bf_q) && !list_is_last(&tbf->list, &bf_q)) {
1583 tbf = list_entry(tbf->list.next, struct ath_buf, list);
1584 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1585 }
1586
1587 txq->axq_aggr_depth++;
1588
1589 /*
1590 * Normal aggregate, queue to hardware
1591 */
1592 ath_tx_txqaddbuf(sc, txq, &bf_q);
1593
1594 } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
1595 status != ATH_AGGR_BAW_CLOSED);
1596}
1597
1598/* Called with txq lock held */
1599
1600static void ath_tid_drain(struct ath_softc *sc,
1601 struct ath_txq *txq,
Sujithb5aa9bf2008-10-29 10:13:31 +05301602 struct ath_atx_tid *tid)
1603
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001604{
1605 struct ath_buf *bf;
1606 struct list_head bf_head;
1607 INIT_LIST_HEAD(&bf_head);
1608
1609 for (;;) {
1610 if (list_empty(&tid->buf_q))
1611 break;
1612 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1613
1614 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1615
1616 /* update baw for software retried frame */
Sujithcd3d39a2008-08-11 14:03:34 +05301617 if (bf_isretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001618 ath_tx_update_baw(sc, tid, bf->bf_seqno);
1619
1620 /*
1621 * do not indicate packets while holding txq spinlock.
1622 * unlock is intentional here
1623 */
Sujithb5aa9bf2008-10-29 10:13:31 +05301624 spin_unlock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001625
1626 /* complete this sub-frame */
1627 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
1628
Sujithb5aa9bf2008-10-29 10:13:31 +05301629 spin_lock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001630 }
1631
1632 /*
1633 * TODO: For frame(s) that are in the retry state, we will reuse the
1634 * sequence number(s) without setting the retry bit. The
1635 * alternative is to give up on these and BAR the receiver's window
1636 * forward.
1637 */
1638 tid->seq_next = tid->seq_start;
1639 tid->baw_tail = tid->baw_head;
1640}
1641
1642/*
1643 * Drain all pending buffers
1644 * NB: must be called with txq lock held
1645 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001646static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
Sujithb5aa9bf2008-10-29 10:13:31 +05301647 struct ath_txq *txq)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001648{
1649 struct ath_atx_ac *ac, *ac_tmp;
1650 struct ath_atx_tid *tid, *tid_tmp;
1651
1652 list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
1653 list_del(&ac->list);
1654 ac->sched = false;
1655 list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
1656 list_del(&tid->list);
1657 tid->sched = false;
Sujithb5aa9bf2008-10-29 10:13:31 +05301658 ath_tid_drain(sc, txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001659 }
1660 }
1661}
1662
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001663static int ath_tx_setup_buffer(struct ath_softc *sc, struct ath_buf *bf,
Sujith8f93b8b2008-11-18 09:10:42 +05301664 struct sk_buff *skb,
Sujith528f0c62008-10-29 10:14:26 +05301665 struct ath_tx_control *txctl)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001666{
Sujith528f0c62008-10-29 10:14:26 +05301667 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1668 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001669 struct ath_tx_info_priv *tx_info_priv;
Sujith528f0c62008-10-29 10:14:26 +05301670 int hdrlen;
1671 __le16 fc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001672
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001673 tx_info_priv = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
1674 if (unlikely(!tx_info_priv))
1675 return -ENOMEM;
Sujitha8efee42008-11-18 09:07:30 +05301676 tx_info->rate_driver_data[0] = tx_info_priv;
Sujith528f0c62008-10-29 10:14:26 +05301677 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1678 fc = hdr->frame_control;
Jouni Malinene022edb2008-08-22 17:31:33 +03001679
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001680 ATH_TXBUF_RESET(bf);
Sujith528f0c62008-10-29 10:14:26 +05301681
1682 /* Frame type */
1683
1684 bf->bf_frmlen = skb->len + FCS_LEN - (hdrlen & 3);
Sujithcd3d39a2008-08-11 14:03:34 +05301685
1686 ieee80211_is_data(fc) ?
1687 (bf->bf_state.bf_type |= BUF_DATA) :
1688 (bf->bf_state.bf_type &= ~BUF_DATA);
1689 ieee80211_is_back_req(fc) ?
1690 (bf->bf_state.bf_type |= BUF_BAR) :
1691 (bf->bf_state.bf_type &= ~BUF_BAR);
1692 ieee80211_is_pspoll(fc) ?
1693 (bf->bf_state.bf_type |= BUF_PSPOLL) :
1694 (bf->bf_state.bf_type &= ~BUF_PSPOLL);
Sujith672840a2008-08-11 14:05:08 +05301695 (sc->sc_flags & SC_OP_PREAMBLE_SHORT) ?
Sujithcd3d39a2008-08-11 14:03:34 +05301696 (bf->bf_state.bf_type |= BUF_SHORT_PREAMBLE) :
1697 (bf->bf_state.bf_type &= ~BUF_SHORT_PREAMBLE);
Sujitha8efee42008-11-18 09:07:30 +05301698 (sc->hw->conf.ht.enabled && !is_pae(skb) &&
Sujith528f0c62008-10-29 10:14:26 +05301699 (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) ?
1700 (bf->bf_state.bf_type |= BUF_HT) :
1701 (bf->bf_state.bf_type &= ~BUF_HT);
Sujithcd3d39a2008-08-11 14:03:34 +05301702
Sujith528f0c62008-10-29 10:14:26 +05301703 bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
1704
1705 /* Crypto */
1706
1707 bf->bf_keytype = get_hw_crypto_keytype(skb);
1708
1709 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR) {
1710 bf->bf_frmlen += tx_info->control.hw_key->icv_len;
1711 bf->bf_keyix = tx_info->control.hw_key->hw_key_idx;
1712 } else {
1713 bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
1714 }
1715
Sujith528f0c62008-10-29 10:14:26 +05301716 /* Assign seqno, tidno */
1717
Senthil Balasubramaniand3a1db12008-12-22 16:31:58 +05301718 if (ieee80211_is_data_qos(fc) && (sc->sc_flags & SC_OP_TXAGGR))
Sujith528f0c62008-10-29 10:14:26 +05301719 assign_aggr_tid_seqno(skb, bf);
1720
1721 /* DMA setup */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001722 bf->bf_mpdu = skb;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001723
Sujith528f0c62008-10-29 10:14:26 +05301724 bf->bf_dmacontext = pci_map_single(sc->pdev, skb->data,
1725 skb->len, PCI_DMA_TODEVICE);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001726 if (unlikely(pci_dma_mapping_error(sc->pdev, bf->bf_dmacontext))) {
1727 bf->bf_mpdu = NULL;
1728 DPRINTF(sc, ATH_DBG_CONFIG,
1729 "pci_dma_mapping_error() on TX\n");
1730 return -ENOMEM;
1731 }
1732
Sujith528f0c62008-10-29 10:14:26 +05301733 bf->bf_buf_addr = bf->bf_dmacontext;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001734 return 0;
Sujith528f0c62008-10-29 10:14:26 +05301735}
1736
1737/* FIXME: tx power */
1738static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
Sujith528f0c62008-10-29 10:14:26 +05301739 struct ath_tx_control *txctl)
1740{
1741 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
1742 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1743 struct ath_node *an = NULL;
1744 struct list_head bf_head;
1745 struct ath_desc *ds;
1746 struct ath_atx_tid *tid;
1747 struct ath_hal *ah = sc->sc_ah;
1748 int frm_type;
1749
Sujith528f0c62008-10-29 10:14:26 +05301750 frm_type = get_hw_packet_type(skb);
1751
1752 INIT_LIST_HEAD(&bf_head);
1753 list_add_tail(&bf->list, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001754
1755 /* setup descriptor */
Sujith528f0c62008-10-29 10:14:26 +05301756
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001757 ds = bf->bf_desc;
1758 ds->ds_link = 0;
1759 ds->ds_data = bf->bf_buf_addr;
1760
Sujith528f0c62008-10-29 10:14:26 +05301761 /* Formulate first tx descriptor with tx controls */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001762
Sujith528f0c62008-10-29 10:14:26 +05301763 ath9k_hw_set11n_txdesc(ah, ds, bf->bf_frmlen, frm_type, MAX_RATE_POWER,
1764 bf->bf_keyix, bf->bf_keytype, bf->bf_flags);
1765
1766 ath9k_hw_filltxdesc(ah, ds,
Sujith8f93b8b2008-11-18 09:10:42 +05301767 skb->len, /* segment length */
1768 true, /* first segment */
1769 true, /* last segment */
1770 ds); /* first descriptor */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001771
1772 bf->bf_lastfrm = bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001773
Sujith528f0c62008-10-29 10:14:26 +05301774 spin_lock_bh(&txctl->txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001775
John W. Linvillef1617962008-10-31 16:45:15 -04001776 if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR) &&
1777 tx_info->control.sta) {
1778 an = (struct ath_node *)tx_info->control.sta->drv_priv;
1779 tid = ATH_AN_2_TID(an, bf->bf_tidno);
1780
Sujith528f0c62008-10-29 10:14:26 +05301781 if (ath_aggr_query(sc, an, bf->bf_tidno)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001782 /*
1783 * Try aggregation if it's a unicast data frame
1784 * and the destination is HT capable.
1785 */
Sujith528f0c62008-10-29 10:14:26 +05301786 ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001787 } else {
1788 /*
Sujith528f0c62008-10-29 10:14:26 +05301789 * Send this frame as regular when ADDBA
1790 * exchange is neither complete nor pending.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001791 */
Sujith528f0c62008-10-29 10:14:26 +05301792 ath_tx_send_normal(sc, txctl->txq,
1793 tid, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001794 }
1795 } else {
1796 bf->bf_lastbf = bf;
1797 bf->bf_nframes = 1;
Sujith528f0c62008-10-29 10:14:26 +05301798
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001799 ath_buf_set_rate(sc, bf);
Sujith528f0c62008-10-29 10:14:26 +05301800 ath_tx_txqaddbuf(sc, txctl->txq, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001801 }
Sujith528f0c62008-10-29 10:14:26 +05301802
1803 spin_unlock_bh(&txctl->txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001804}
1805
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001806/* Upon failure caller should free skb */
Sujith528f0c62008-10-29 10:14:26 +05301807int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb,
1808 struct ath_tx_control *txctl)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001809{
Sujith528f0c62008-10-29 10:14:26 +05301810 struct ath_buf *bf;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001811 int r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001812
Sujith528f0c62008-10-29 10:14:26 +05301813 /* Check if a tx buffer is available */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001814
Sujith528f0c62008-10-29 10:14:26 +05301815 bf = ath_tx_get_buffer(sc);
1816 if (!bf) {
Sujith04bd46382008-11-28 22:18:05 +05301817 DPRINTF(sc, ATH_DBG_XMIT, "TX buffers are full\n");
Sujith528f0c62008-10-29 10:14:26 +05301818 return -1;
1819 }
1820
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001821 r = ath_tx_setup_buffer(sc, bf, skb, txctl);
1822 if (unlikely(r)) {
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001823 struct ath_txq *txq = txctl->txq;
1824
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001825 DPRINTF(sc, ATH_DBG_FATAL, "TX mem alloc failure\n");
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001826
1827 /* upon ath_tx_processq() this TX queue will be resumed, we
1828 * guarantee this will happen by knowing beforehand that
1829 * we will at least have to run TX completionon one buffer
1830 * on the queue */
1831 spin_lock_bh(&txq->axq_lock);
1832 if (ath_txq_depth(sc, txq->axq_qnum) > 1) {
1833 ieee80211_stop_queue(sc->hw,
1834 skb_get_queue_mapping(skb));
1835 txq->stopped = 1;
1836 }
1837 spin_unlock_bh(&txq->axq_lock);
1838
Sujithb77f4832008-12-07 21:44:03 +05301839 spin_lock_bh(&sc->tx.txbuflock);
1840 list_add_tail(&bf->list, &sc->tx.txbuf);
1841 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001842
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001843 return r;
1844 }
1845
Sujith8f93b8b2008-11-18 09:10:42 +05301846 ath_tx_start_dma(sc, bf, txctl);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001847
Sujith528f0c62008-10-29 10:14:26 +05301848 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001849}
1850
1851/* Initialize TX queue and h/w */
1852
1853int ath_tx_init(struct ath_softc *sc, int nbufs)
1854{
1855 int error = 0;
1856
1857 do {
Sujithb77f4832008-12-07 21:44:03 +05301858 spin_lock_init(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001859
1860 /* Setup tx descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301861 error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
Sujith556bb8f2008-08-11 14:03:53 +05301862 "tx", nbufs, 1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001863 if (error != 0) {
1864 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301865 "Failed to allocate tx descriptors: %d\n",
1866 error);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001867 break;
1868 }
1869
1870 /* XXX allocate beacon state together with vap */
Sujithb77f4832008-12-07 21:44:03 +05301871 error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001872 "beacon", ATH_BCBUF, 1);
1873 if (error != 0) {
1874 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301875 "Failed to allocate beacon descriptors: %d\n",
1876 error);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001877 break;
1878 }
1879
1880 } while (0);
1881
1882 if (error != 0)
1883 ath_tx_cleanup(sc);
1884
1885 return error;
1886}
1887
1888/* Reclaim all tx queue resources */
1889
1890int ath_tx_cleanup(struct ath_softc *sc)
1891{
1892 /* cleanup beacon descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301893 if (sc->beacon.bdma.dd_desc_len != 0)
1894 ath_descdma_cleanup(sc, &sc->beacon.bdma, &sc->beacon.bbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001895
1896 /* cleanup tx descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301897 if (sc->tx.txdma.dd_desc_len != 0)
1898 ath_descdma_cleanup(sc, &sc->tx.txdma, &sc->tx.txbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001899
1900 return 0;
1901}
1902
1903/* Setup a h/w transmit queue */
1904
1905struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
1906{
1907 struct ath_hal *ah = sc->sc_ah;
Sujithea9880f2008-08-07 10:53:10 +05301908 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001909 int qnum;
1910
Luis R. Rodriguez0345f372008-10-03 15:45:25 -07001911 memset(&qi, 0, sizeof(qi));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001912 qi.tqi_subtype = subtype;
1913 qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
1914 qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
1915 qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
Sujithea9880f2008-08-07 10:53:10 +05301916 qi.tqi_physCompBuf = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001917
1918 /*
1919 * Enable interrupts only for EOL and DESC conditions.
1920 * We mark tx descriptors to receive a DESC interrupt
1921 * when a tx queue gets deep; otherwise waiting for the
1922 * EOL to reap descriptors. Note that this is done to
1923 * reduce interrupt load and this only defers reaping
1924 * descriptors, never transmitting frames. Aside from
1925 * reducing interrupts this also permits more concurrency.
1926 * The only potential downside is if the tx queue backs
1927 * up in which case the top half of the kernel may backup
1928 * due to a lack of tx descriptors.
1929 *
1930 * The UAPSD queue is an exception, since we take a desc-
1931 * based intr on the EOSP frames.
1932 */
1933 if (qtype == ATH9K_TX_QUEUE_UAPSD)
1934 qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1935 else
1936 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
1937 TXQ_FLAG_TXDESCINT_ENABLE;
1938 qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
1939 if (qnum == -1) {
1940 /*
1941 * NB: don't print a message, this happens
1942 * normally on parts with too few tx queues
1943 */
1944 return NULL;
1945 }
Sujithb77f4832008-12-07 21:44:03 +05301946 if (qnum >= ARRAY_SIZE(sc->tx.txq)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001947 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301948 "qnum %u out of range, max %u!\n",
Sujithb77f4832008-12-07 21:44:03 +05301949 qnum, (unsigned int)ARRAY_SIZE(sc->tx.txq));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001950 ath9k_hw_releasetxqueue(ah, qnum);
1951 return NULL;
1952 }
1953 if (!ATH_TXQ_SETUP(sc, qnum)) {
Sujithb77f4832008-12-07 21:44:03 +05301954 struct ath_txq *txq = &sc->tx.txq[qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001955
1956 txq->axq_qnum = qnum;
1957 txq->axq_link = NULL;
1958 INIT_LIST_HEAD(&txq->axq_q);
1959 INIT_LIST_HEAD(&txq->axq_acq);
1960 spin_lock_init(&txq->axq_lock);
1961 txq->axq_depth = 0;
1962 txq->axq_aggr_depth = 0;
1963 txq->axq_totalqueued = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001964 txq->axq_linkbuf = NULL;
Sujithb77f4832008-12-07 21:44:03 +05301965 sc->tx.txqsetup |= 1<<qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001966 }
Sujithb77f4832008-12-07 21:44:03 +05301967 return &sc->tx.txq[qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001968}
1969
1970/* Reclaim resources for a setup queue */
1971
1972void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1973{
1974 ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
Sujithb77f4832008-12-07 21:44:03 +05301975 sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001976}
1977
1978/*
1979 * Setup a hardware data transmit queue for the specified
1980 * access control. The hal may not support all requested
1981 * queues in which case it will return a reference to a
1982 * previously setup queue. We record the mapping from ac's
1983 * to h/w queues for use by ath_tx_start and also track
1984 * the set of h/w queues being used to optimize work in the
1985 * transmit interrupt handler and related routines.
1986 */
1987
1988int ath_tx_setup(struct ath_softc *sc, int haltype)
1989{
1990 struct ath_txq *txq;
1991
Sujithb77f4832008-12-07 21:44:03 +05301992 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001993 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301994 "HAL AC %u out of range, max %zu!\n",
Sujithb77f4832008-12-07 21:44:03 +05301995 haltype, ARRAY_SIZE(sc->tx.hwq_map));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001996 return 0;
1997 }
1998 txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
1999 if (txq != NULL) {
Sujithb77f4832008-12-07 21:44:03 +05302000 sc->tx.hwq_map[haltype] = txq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002001 return 1;
2002 } else
2003 return 0;
2004}
2005
2006int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
2007{
2008 int qnum;
2009
2010 switch (qtype) {
2011 case ATH9K_TX_QUEUE_DATA:
Sujithb77f4832008-12-07 21:44:03 +05302012 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002013 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05302014 "HAL AC %u out of range, max %zu!\n",
Sujithb77f4832008-12-07 21:44:03 +05302015 haltype, ARRAY_SIZE(sc->tx.hwq_map));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002016 return -1;
2017 }
Sujithb77f4832008-12-07 21:44:03 +05302018 qnum = sc->tx.hwq_map[haltype];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002019 break;
2020 case ATH9K_TX_QUEUE_BEACON:
Sujithb77f4832008-12-07 21:44:03 +05302021 qnum = sc->beacon.beaconq;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002022 break;
2023 case ATH9K_TX_QUEUE_CAB:
Sujithb77f4832008-12-07 21:44:03 +05302024 qnum = sc->beacon.cabq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002025 break;
2026 default:
2027 qnum = -1;
2028 }
2029 return qnum;
2030}
2031
Sujith528f0c62008-10-29 10:14:26 +05302032/* Get a transmit queue, if available */
2033
2034struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
2035{
2036 struct ath_txq *txq = NULL;
2037 int qnum;
2038
2039 qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
Sujithb77f4832008-12-07 21:44:03 +05302040 txq = &sc->tx.txq[qnum];
Sujith528f0c62008-10-29 10:14:26 +05302041
2042 spin_lock_bh(&txq->axq_lock);
2043
2044 /* Try to avoid running out of descriptors */
2045 if (txq->axq_depth >= (ATH_TXBUF - 20)) {
2046 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05302047 "TX queue: %d is full, depth: %d\n",
2048 qnum, txq->axq_depth);
Sujith528f0c62008-10-29 10:14:26 +05302049 ieee80211_stop_queue(sc->hw, skb_get_queue_mapping(skb));
2050 txq->stopped = 1;
2051 spin_unlock_bh(&txq->axq_lock);
2052 return NULL;
2053 }
2054
2055 spin_unlock_bh(&txq->axq_lock);
2056
2057 return txq;
2058}
2059
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002060/* Update parameters for a transmit queue */
2061
Sujithea9880f2008-08-07 10:53:10 +05302062int ath_txq_update(struct ath_softc *sc, int qnum,
2063 struct ath9k_tx_queue_info *qinfo)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002064{
2065 struct ath_hal *ah = sc->sc_ah;
2066 int error = 0;
Sujithea9880f2008-08-07 10:53:10 +05302067 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002068
Sujithb77f4832008-12-07 21:44:03 +05302069 if (qnum == sc->beacon.beaconq) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002070 /*
2071 * XXX: for beacon queue, we just save the parameter.
2072 * It will be picked up by ath_beaconq_config when
2073 * it's necessary.
2074 */
Sujithb77f4832008-12-07 21:44:03 +05302075 sc->beacon.beacon_qi = *qinfo;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002076 return 0;
2077 }
2078
Sujithb77f4832008-12-07 21:44:03 +05302079 ASSERT(sc->tx.txq[qnum].axq_qnum == qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002080
Sujithea9880f2008-08-07 10:53:10 +05302081 ath9k_hw_get_txq_props(ah, qnum, &qi);
2082 qi.tqi_aifs = qinfo->tqi_aifs;
2083 qi.tqi_cwmin = qinfo->tqi_cwmin;
2084 qi.tqi_cwmax = qinfo->tqi_cwmax;
2085 qi.tqi_burstTime = qinfo->tqi_burstTime;
2086 qi.tqi_readyTime = qinfo->tqi_readyTime;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002087
Sujithea9880f2008-08-07 10:53:10 +05302088 if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002089 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05302090 "Unable to update hardware queue %u!\n", qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002091 error = -EIO;
2092 } else {
2093 ath9k_hw_resettxqueue(ah, qnum); /* push to h/w */
2094 }
2095
2096 return error;
2097}
2098
2099int ath_cabq_update(struct ath_softc *sc)
2100{
Sujithea9880f2008-08-07 10:53:10 +05302101 struct ath9k_tx_queue_info qi;
Sujithb77f4832008-12-07 21:44:03 +05302102 int qnum = sc->beacon.cabq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002103 struct ath_beacon_config conf;
2104
Sujithea9880f2008-08-07 10:53:10 +05302105 ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002106 /*
2107 * Ensure the readytime % is within the bounds.
2108 */
2109 if (sc->sc_config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
2110 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
2111 else if (sc->sc_config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
2112 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
2113
2114 ath_get_beaconconfig(sc, ATH_IF_ID_ANY, &conf);
2115 qi.tqi_readyTime =
2116 (conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
2117 ath_txq_update(sc, qnum, &qi);
2118
2119 return 0;
2120}
2121
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002122/* Deferred processing of transmit interrupt */
2123
2124void ath_tx_tasklet(struct ath_softc *sc)
2125{
Sujith1fe11322008-08-26 08:11:06 +05302126 int i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002127 u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
2128
2129 ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
2130
2131 /*
2132 * Process each active queue.
2133 */
2134 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2135 if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
Sujithb77f4832008-12-07 21:44:03 +05302136 ath_tx_processq(sc, &sc->tx.txq[i]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002137 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002138}
2139
2140void ath_tx_draintxq(struct ath_softc *sc,
2141 struct ath_txq *txq, bool retry_tx)
2142{
2143 struct ath_buf *bf, *lastbf;
2144 struct list_head bf_head;
2145
2146 INIT_LIST_HEAD(&bf_head);
2147
2148 /*
2149 * NB: this assumes output has been stopped and
2150 * we do not need to block ath_tx_tasklet
2151 */
2152 for (;;) {
2153 spin_lock_bh(&txq->axq_lock);
2154
2155 if (list_empty(&txq->axq_q)) {
2156 txq->axq_link = NULL;
2157 txq->axq_linkbuf = NULL;
2158 spin_unlock_bh(&txq->axq_lock);
2159 break;
2160 }
2161
2162 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2163
2164 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
2165 list_del(&bf->list);
2166 spin_unlock_bh(&txq->axq_lock);
2167
Sujithb77f4832008-12-07 21:44:03 +05302168 spin_lock_bh(&sc->tx.txbuflock);
2169 list_add_tail(&bf->list, &sc->tx.txbuf);
2170 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002171 continue;
2172 }
2173
2174 lastbf = bf->bf_lastbf;
2175 if (!retry_tx)
2176 lastbf->bf_desc->ds_txstat.ts_flags =
2177 ATH9K_TX_SW_ABORTED;
2178
2179 /* remove ath_buf's of the same mpdu from txq */
2180 list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
2181 txq->axq_depth--;
2182
2183 spin_unlock_bh(&txq->axq_lock);
2184
Sujithcd3d39a2008-08-11 14:03:34 +05302185 if (bf_isampdu(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002186 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, 0);
2187 else
2188 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2189 }
2190
2191 /* flush any pending frames if aggregation is enabled */
Sujith672840a2008-08-11 14:05:08 +05302192 if (sc->sc_flags & SC_OP_TXAGGR) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002193 if (!retry_tx) {
2194 spin_lock_bh(&txq->axq_lock);
Sujithb5aa9bf2008-10-29 10:13:31 +05302195 ath_txq_drain_pending_buffers(sc, txq);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002196 spin_unlock_bh(&txq->axq_lock);
2197 }
2198 }
2199}
2200
2201/* Drain the transmit queues and reclaim resources */
2202
2203void ath_draintxq(struct ath_softc *sc, bool retry_tx)
2204{
2205 /* stop beacon queue. The beacon will be freed when
2206 * we go to INIT state */
Sujith672840a2008-08-11 14:05:08 +05302207 if (!(sc->sc_flags & SC_OP_INVALID)) {
Sujithb77f4832008-12-07 21:44:03 +05302208 (void) ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
Sujith04bd46382008-11-28 22:18:05 +05302209 DPRINTF(sc, ATH_DBG_XMIT, "beacon queue %x\n",
Sujithb77f4832008-12-07 21:44:03 +05302210 ath9k_hw_gettxbuf(sc->sc_ah, sc->beacon.beaconq));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002211 }
2212
2213 ath_drain_txdataq(sc, retry_tx);
2214}
2215
2216u32 ath_txq_depth(struct ath_softc *sc, int qnum)
2217{
Sujithb77f4832008-12-07 21:44:03 +05302218 return sc->tx.txq[qnum].axq_depth;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002219}
2220
2221u32 ath_txq_aggr_depth(struct ath_softc *sc, int qnum)
2222{
Sujithb77f4832008-12-07 21:44:03 +05302223 return sc->tx.txq[qnum].axq_aggr_depth;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002224}
2225
Sujithccc75c52008-10-29 10:18:14 +05302226bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an, u8 tidno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002227{
2228 struct ath_atx_tid *txtid;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002229
Sujith672840a2008-08-11 14:05:08 +05302230 if (!(sc->sc_flags & SC_OP_TXAGGR))
Sujithccc75c52008-10-29 10:18:14 +05302231 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002232
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002233 txtid = ATH_AN_2_TID(an, tidno);
2234
Sujitha37c2c72008-10-29 10:15:40 +05302235 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
2236 if (!(txtid->state & AGGR_ADDBA_PROGRESS) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002237 (txtid->addba_exchangeattempts < ADDBA_EXCHANGE_ATTEMPTS)) {
2238 txtid->addba_exchangeattempts++;
Sujithccc75c52008-10-29 10:18:14 +05302239 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002240 }
2241 }
2242
Sujithccc75c52008-10-29 10:18:14 +05302243 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002244}
2245
2246/* Start TX aggregation */
2247
Sujithb5aa9bf2008-10-29 10:13:31 +05302248int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
2249 u16 tid, u16 *ssn)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002250{
2251 struct ath_atx_tid *txtid;
2252 struct ath_node *an;
2253
Sujithb5aa9bf2008-10-29 10:13:31 +05302254 an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002255
Sujith672840a2008-08-11 14:05:08 +05302256 if (sc->sc_flags & SC_OP_TXAGGR) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002257 txtid = ATH_AN_2_TID(an, tid);
Sujitha37c2c72008-10-29 10:15:40 +05302258 txtid->state |= AGGR_ADDBA_PROGRESS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002259 ath_tx_pause_tid(sc, txtid);
2260 }
2261
2262 return 0;
2263}
2264
2265/* Stop tx aggregation */
2266
Sujithb5aa9bf2008-10-29 10:13:31 +05302267int ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002268{
Sujithb5aa9bf2008-10-29 10:13:31 +05302269 struct ath_node *an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002270
2271 ath_tx_aggr_teardown(sc, an, tid);
2272 return 0;
2273}
2274
Sujith8469cde2008-10-29 10:19:28 +05302275/* Resume tx aggregation */
2276
2277void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
2278{
2279 struct ath_atx_tid *txtid;
2280 struct ath_node *an;
2281
2282 an = (struct ath_node *)sta->drv_priv;
2283
2284 if (sc->sc_flags & SC_OP_TXAGGR) {
2285 txtid = ATH_AN_2_TID(an, tid);
2286 txtid->baw_size =
2287 IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
2288 txtid->state |= AGGR_ADDBA_COMPLETE;
2289 txtid->state &= ~AGGR_ADDBA_PROGRESS;
2290 ath_tx_resume_tid(sc, txtid);
2291 }
2292}
2293
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002294/*
2295 * Performs transmit side cleanup when TID changes from aggregated to
2296 * unaggregated.
2297 * - Pause the TID and mark cleanup in progress
2298 * - Discard all retry frames from the s/w queue.
2299 */
2300
Sujithb5aa9bf2008-10-29 10:13:31 +05302301void ath_tx_aggr_teardown(struct ath_softc *sc, struct ath_node *an, u8 tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002302{
2303 struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
Sujithb77f4832008-12-07 21:44:03 +05302304 struct ath_txq *txq = &sc->tx.txq[txtid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002305 struct ath_buf *bf;
2306 struct list_head bf_head;
2307 INIT_LIST_HEAD(&bf_head);
2308
Sujitha37c2c72008-10-29 10:15:40 +05302309 if (txtid->state & AGGR_CLEANUP) /* cleanup is in progress */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002310 return;
2311
Sujitha37c2c72008-10-29 10:15:40 +05302312 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002313 txtid->addba_exchangeattempts = 0;
2314 return;
2315 }
2316
2317 /* TID must be paused first */
2318 ath_tx_pause_tid(sc, txtid);
2319
2320 /* drop all software retried frames and mark this TID */
2321 spin_lock_bh(&txq->axq_lock);
2322 while (!list_empty(&txtid->buf_q)) {
2323 bf = list_first_entry(&txtid->buf_q, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +05302324 if (!bf_isretried(bf)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002325 /*
2326 * NB: it's based on the assumption that
2327 * software retried frame will always stay
2328 * at the head of software queue.
2329 */
2330 break;
2331 }
2332 list_cut_position(&bf_head,
2333 &txtid->buf_q, &bf->bf_lastfrm->list);
2334 ath_tx_update_baw(sc, txtid, bf->bf_seqno);
2335
2336 /* complete this sub-frame */
2337 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2338 }
2339
2340 if (txtid->baw_head != txtid->baw_tail) {
2341 spin_unlock_bh(&txq->axq_lock);
Sujitha37c2c72008-10-29 10:15:40 +05302342 txtid->state |= AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002343 } else {
Sujitha37c2c72008-10-29 10:15:40 +05302344 txtid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002345 txtid->addba_exchangeattempts = 0;
2346 spin_unlock_bh(&txq->axq_lock);
2347 ath_tx_flush_tid(sc, txtid);
2348 }
2349}
2350
2351/*
2352 * Tx scheduling logic
2353 * NB: must be called with txq lock held
2354 */
2355
2356void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
2357{
2358 struct ath_atx_ac *ac;
2359 struct ath_atx_tid *tid;
2360
2361 /* nothing to schedule */
2362 if (list_empty(&txq->axq_acq))
2363 return;
2364 /*
2365 * get the first node/ac pair on the queue
2366 */
2367 ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
2368 list_del(&ac->list);
2369 ac->sched = false;
2370
2371 /*
2372 * process a single tid per destination
2373 */
2374 do {
2375 /* nothing to schedule */
2376 if (list_empty(&ac->tid_q))
2377 return;
2378
2379 tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
2380 list_del(&tid->list);
2381 tid->sched = false;
2382
2383 if (tid->paused) /* check next tid to keep h/w busy */
2384 continue;
2385
Sujith43453b32008-10-29 10:14:52 +05302386 if ((txq->axq_depth % 2) == 0)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002387 ath_tx_sched_aggr(sc, txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002388
2389 /*
2390 * add tid to round-robin queue if more frames
2391 * are pending for the tid
2392 */
2393 if (!list_empty(&tid->buf_q))
2394 ath_tx_queue_tid(txq, tid);
2395
2396 /* only schedule one TID at a time */
2397 break;
2398 } while (!list_empty(&ac->tid_q));
2399
2400 /*
2401 * schedule AC if more TIDs need processing
2402 */
2403 if (!list_empty(&ac->tid_q)) {
2404 /*
2405 * add dest ac to txq if not already added
2406 */
2407 if (!ac->sched) {
2408 ac->sched = true;
2409 list_add_tail(&ac->list, &txq->axq_acq);
2410 }
2411 }
2412}
2413
2414/* Initialize per-node transmit state */
2415
2416void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2417{
Sujithc5170162008-10-29 10:13:59 +05302418 struct ath_atx_tid *tid;
2419 struct ath_atx_ac *ac;
2420 int tidno, acno;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002421
Sujithc5170162008-10-29 10:13:59 +05302422 /*
2423 * Init per tid tx state
2424 */
Sujith8ee5afb2008-12-07 21:43:36 +05302425 for (tidno = 0, tid = &an->tid[tidno];
Sujithc5170162008-10-29 10:13:59 +05302426 tidno < WME_NUM_TID;
2427 tidno++, tid++) {
2428 tid->an = an;
2429 tid->tidno = tidno;
2430 tid->seq_start = tid->seq_next = 0;
2431 tid->baw_size = WME_MAX_BA;
2432 tid->baw_head = tid->baw_tail = 0;
2433 tid->sched = false;
2434 tid->paused = false;
Sujitha37c2c72008-10-29 10:15:40 +05302435 tid->state &= ~AGGR_CLEANUP;
Sujithc5170162008-10-29 10:13:59 +05302436 INIT_LIST_HEAD(&tid->buf_q);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002437
Sujithc5170162008-10-29 10:13:59 +05302438 acno = TID_TO_WME_AC(tidno);
Sujith8ee5afb2008-12-07 21:43:36 +05302439 tid->ac = &an->ac[acno];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002440
Sujithc5170162008-10-29 10:13:59 +05302441 /* ADDBA state */
Sujitha37c2c72008-10-29 10:15:40 +05302442 tid->state &= ~AGGR_ADDBA_COMPLETE;
2443 tid->state &= ~AGGR_ADDBA_PROGRESS;
2444 tid->addba_exchangeattempts = 0;
Sujithc5170162008-10-29 10:13:59 +05302445 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002446
Sujithc5170162008-10-29 10:13:59 +05302447 /*
2448 * Init per ac tx state
2449 */
Sujith8ee5afb2008-12-07 21:43:36 +05302450 for (acno = 0, ac = &an->ac[acno];
Sujithc5170162008-10-29 10:13:59 +05302451 acno < WME_NUM_AC; acno++, ac++) {
2452 ac->sched = false;
2453 INIT_LIST_HEAD(&ac->tid_q);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002454
Sujithc5170162008-10-29 10:13:59 +05302455 switch (acno) {
2456 case WME_AC_BE:
2457 ac->qnum = ath_tx_get_qnum(sc,
2458 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
2459 break;
2460 case WME_AC_BK:
2461 ac->qnum = ath_tx_get_qnum(sc,
2462 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
2463 break;
2464 case WME_AC_VI:
2465 ac->qnum = ath_tx_get_qnum(sc,
2466 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
2467 break;
2468 case WME_AC_VO:
2469 ac->qnum = ath_tx_get_qnum(sc,
2470 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
2471 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002472 }
2473 }
2474}
2475
2476/* Cleanupthe pending buffers for the node. */
2477
Sujithb5aa9bf2008-10-29 10:13:31 +05302478void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002479{
2480 int i;
2481 struct ath_atx_ac *ac, *ac_tmp;
2482 struct ath_atx_tid *tid, *tid_tmp;
2483 struct ath_txq *txq;
2484 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2485 if (ATH_TXQ_SETUP(sc, i)) {
Sujithb77f4832008-12-07 21:44:03 +05302486 txq = &sc->tx.txq[i];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002487
Sujithb5aa9bf2008-10-29 10:13:31 +05302488 spin_lock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002489
2490 list_for_each_entry_safe(ac,
2491 ac_tmp, &txq->axq_acq, list) {
2492 tid = list_first_entry(&ac->tid_q,
2493 struct ath_atx_tid, list);
2494 if (tid && tid->an != an)
2495 continue;
2496 list_del(&ac->list);
2497 ac->sched = false;
2498
2499 list_for_each_entry_safe(tid,
2500 tid_tmp, &ac->tid_q, list) {
2501 list_del(&tid->list);
2502 tid->sched = false;
Sujithb5aa9bf2008-10-29 10:13:31 +05302503 ath_tid_drain(sc, txq, tid);
Sujitha37c2c72008-10-29 10:15:40 +05302504 tid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002505 tid->addba_exchangeattempts = 0;
Sujitha37c2c72008-10-29 10:15:40 +05302506 tid->state &= ~AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002507 }
2508 }
2509
Sujithb5aa9bf2008-10-29 10:13:31 +05302510 spin_unlock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002511 }
2512 }
2513}
2514
Jouni Malinene022edb2008-08-22 17:31:33 +03002515void ath_tx_cabq(struct ath_softc *sc, struct sk_buff *skb)
2516{
2517 int hdrlen, padsize;
2518 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2519 struct ath_tx_control txctl;
2520
Sujith528f0c62008-10-29 10:14:26 +05302521 memset(&txctl, 0, sizeof(struct ath_tx_control));
2522
Jouni Malinene022edb2008-08-22 17:31:33 +03002523 /*
2524 * As a temporary workaround, assign seq# here; this will likely need
2525 * to be cleaned up to work better with Beacon transmission and virtual
2526 * BSSes.
2527 */
2528 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
2529 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2530 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
Sujithb77f4832008-12-07 21:44:03 +05302531 sc->tx.seq_no += 0x10;
Jouni Malinene022edb2008-08-22 17:31:33 +03002532 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Sujithb77f4832008-12-07 21:44:03 +05302533 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
Jouni Malinene022edb2008-08-22 17:31:33 +03002534 }
2535
2536 /* Add the padding after the header if this is not already done */
2537 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
2538 if (hdrlen & 3) {
2539 padsize = hdrlen % 4;
2540 if (skb_headroom(skb) < padsize) {
Sujith04bd46382008-11-28 22:18:05 +05302541 DPRINTF(sc, ATH_DBG_XMIT, "TX CABQ padding failed\n");
Jouni Malinene022edb2008-08-22 17:31:33 +03002542 dev_kfree_skb_any(skb);
2543 return;
2544 }
2545 skb_push(skb, padsize);
2546 memmove(skb->data, skb->data + padsize, hdrlen);
2547 }
2548
Sujithb77f4832008-12-07 21:44:03 +05302549 txctl.txq = sc->beacon.cabq;
Sujith528f0c62008-10-29 10:14:26 +05302550
Sujith04bd46382008-11-28 22:18:05 +05302551 DPRINTF(sc, ATH_DBG_XMIT, "transmitting CABQ packet, skb: %p\n", skb);
Jouni Malinene022edb2008-08-22 17:31:33 +03002552
Sujith528f0c62008-10-29 10:14:26 +05302553 if (ath_tx_start(sc, skb, &txctl) != 0) {
Sujith04bd46382008-11-28 22:18:05 +05302554 DPRINTF(sc, ATH_DBG_XMIT, "CABQ TX failed\n");
Sujith528f0c62008-10-29 10:14:26 +05302555 goto exit;
Jouni Malinene022edb2008-08-22 17:31:33 +03002556 }
Jouni Malinene022edb2008-08-22 17:31:33 +03002557
Sujith528f0c62008-10-29 10:14:26 +05302558 return;
2559exit:
2560 dev_kfree_skb_any(skb);
2561}