blob: 27cb9d523f5ffb410f457f08322f0688e9744b85 [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,
Sujith04bd4632008-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,
Sujith04bd4632008-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;
Sujith04bd4632008-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
Sujith04bd4632008-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 Malinen157ec872008-12-22 16:45:54 +0200129 tx_info->status.rates[0].count = tx_status->retries + 1;
Sujithc4288392008-11-18 09:09:30 +0530130
Jouni Malinenf7a276a2008-12-15 16:02:04 +0200131 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
132 padsize = hdrlen & 3;
133 if (padsize && hdrlen >= 24) {
134 /*
135 * Remove MAC header padding before giving the frame back to
136 * mac80211.
137 */
138 memmove(skb->data + padsize, skb->data, hdrlen);
139 skb_pull(skb, padsize);
140 }
141
Sujithc4288392008-11-18 09:09:30 +0530142 ieee80211_tx_status(hw, skb);
143}
144
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700145/* Check if it's okay to send out aggregates */
146
Sujitha37c2c72008-10-29 10:15:40 +0530147static int ath_aggr_query(struct ath_softc *sc, struct ath_node *an, u8 tidno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700148{
149 struct ath_atx_tid *tid;
150 tid = ATH_AN_2_TID(an, tidno);
151
Sujitha37c2c72008-10-29 10:15:40 +0530152 if (tid->state & AGGR_ADDBA_COMPLETE ||
153 tid->state & AGGR_ADDBA_PROGRESS)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700154 return 1;
155 else
156 return 0;
157}
158
Sujithff37e332008-11-24 12:07:55 +0530159static void ath_get_beaconconfig(struct ath_softc *sc, int if_id,
160 struct ath_beacon_config *conf)
161{
162 struct ieee80211_hw *hw = sc->hw;
163
164 /* fill in beacon config data */
165
166 conf->beacon_interval = hw->conf.beacon_int;
167 conf->listen_interval = 100;
168 conf->dtim_count = 1;
169 conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
170}
171
Sujith528f0c62008-10-29 10:14:26 +0530172/* Calculate Atheros packet type from IEEE80211 packet header */
173
174static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700175{
Sujith528f0c62008-10-29 10:14:26 +0530176 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700177 enum ath9k_pkt_type htype;
178 __le16 fc;
179
Sujith528f0c62008-10-29 10:14:26 +0530180 hdr = (struct ieee80211_hdr *)skb->data;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700181 fc = hdr->frame_control;
182
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700183 if (ieee80211_is_beacon(fc))
184 htype = ATH9K_PKT_TYPE_BEACON;
185 else if (ieee80211_is_probe_resp(fc))
186 htype = ATH9K_PKT_TYPE_PROBE_RESP;
187 else if (ieee80211_is_atim(fc))
188 htype = ATH9K_PKT_TYPE_ATIM;
189 else if (ieee80211_is_pspoll(fc))
190 htype = ATH9K_PKT_TYPE_PSPOLL;
191 else
192 htype = ATH9K_PKT_TYPE_NORMAL;
193
194 return htype;
195}
196
Sujitha8efee42008-11-18 09:07:30 +0530197static bool is_pae(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700198{
199 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700200 __le16 fc;
201
202 hdr = (struct ieee80211_hdr *)skb->data;
203 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +0200204
Sujitha8efee42008-11-18 09:07:30 +0530205 if (ieee80211_is_data(fc)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700206 if (ieee80211_is_nullfunc(fc) ||
Sujith528f0c62008-10-29 10:14:26 +0530207 /* Port Access Entity (IEEE 802.1X) */
208 (skb->protocol == cpu_to_be16(ETH_P_PAE))) {
Sujitha8efee42008-11-18 09:07:30 +0530209 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700210 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700211 }
212
Sujitha8efee42008-11-18 09:07:30 +0530213 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700214}
215
Sujith528f0c62008-10-29 10:14:26 +0530216static int get_hw_crypto_keytype(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700217{
Sujith528f0c62008-10-29 10:14:26 +0530218 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
219
220 if (tx_info->control.hw_key) {
221 if (tx_info->control.hw_key->alg == ALG_WEP)
222 return ATH9K_KEY_TYPE_WEP;
223 else if (tx_info->control.hw_key->alg == ALG_TKIP)
224 return ATH9K_KEY_TYPE_TKIP;
225 else if (tx_info->control.hw_key->alg == ALG_CCMP)
226 return ATH9K_KEY_TYPE_AES;
227 }
228
229 return ATH9K_KEY_TYPE_CLEAR;
230}
231
Sujith528f0c62008-10-29 10:14:26 +0530232/* Called only when tx aggregation is enabled and HT is supported */
233
234static void assign_aggr_tid_seqno(struct sk_buff *skb,
235 struct ath_buf *bf)
236{
237 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
238 struct ieee80211_hdr *hdr;
239 struct ath_node *an;
240 struct ath_atx_tid *tid;
241 __le16 fc;
242 u8 *qc;
243
244 if (!tx_info->control.sta)
245 return;
246
247 an = (struct ath_node *)tx_info->control.sta->drv_priv;
248 hdr = (struct ieee80211_hdr *)skb->data;
249 fc = hdr->frame_control;
250
251 /* Get tidno */
252
253 if (ieee80211_is_data_qos(fc)) {
254 qc = ieee80211_get_qos_ctl(hdr);
255 bf->bf_tidno = qc[0] & 0xf;
Sujith98deeea2008-08-11 14:05:46 +0530256 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700257
Sujith528f0c62008-10-29 10:14:26 +0530258 /* Get seqno */
Senthil Balasubramaniand3a1db12008-12-22 16:31:58 +0530259 /* For HT capable stations, we save tidno for later use.
260 * We also override seqno set by upper layer with the one
261 * in tx aggregation state.
262 *
263 * If fragmentation is on, the sequence number is
264 * not overridden, since it has been
265 * incremented by the fragmentation routine.
266 *
267 * FIXME: check if the fragmentation threshold exceeds
268 * IEEE80211 max.
269 */
270 tid = ATH_AN_2_TID(an, bf->bf_tidno);
271 hdr->seq_ctrl = cpu_to_le16(tid->seq_next <<
272 IEEE80211_SEQ_SEQ_SHIFT);
273 bf->bf_seqno = tid->seq_next;
274 INCR(tid->seq_next, IEEE80211_SEQ_MAX);
Sujith528f0c62008-10-29 10:14:26 +0530275}
276
277static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
278 struct ath_txq *txq)
279{
280 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
281 int flags = 0;
282
283 flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
284 flags |= ATH9K_TXDESC_INTREQ;
285
286 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
287 flags |= ATH9K_TXDESC_NOACK;
288 if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
289 flags |= ATH9K_TXDESC_RTSENA;
290
291 return flags;
292}
293
294static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
295{
296 struct ath_buf *bf = NULL;
297
Sujithb77f4832008-12-07 21:44:03 +0530298 spin_lock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530299
Sujithb77f4832008-12-07 21:44:03 +0530300 if (unlikely(list_empty(&sc->tx.txbuf))) {
301 spin_unlock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530302 return NULL;
303 }
304
Sujithb77f4832008-12-07 21:44:03 +0530305 bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
Sujith528f0c62008-10-29 10:14:26 +0530306 list_del(&bf->list);
307
Sujithb77f4832008-12-07 21:44:03 +0530308 spin_unlock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530309
310 return bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700311}
312
313/* To complete a chain of buffers associated a frame */
314
315static void ath_tx_complete_buf(struct ath_softc *sc,
316 struct ath_buf *bf,
317 struct list_head *bf_q,
318 int txok, int sendbar)
319{
320 struct sk_buff *skb = bf->bf_mpdu;
321 struct ath_xmit_status tx_status;
Senthil Balasubramaniana07d3612008-12-09 17:23:33 +0530322 unsigned long flags;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700323
324 /*
325 * Set retry information.
326 * NB: Don't use the information in the descriptor, because the frame
327 * could be software retried.
328 */
329 tx_status.retries = bf->bf_retries;
330 tx_status.flags = 0;
331
332 if (sendbar)
333 tx_status.flags = ATH_TX_BAR;
334
335 if (!txok) {
336 tx_status.flags |= ATH_TX_ERROR;
337
Sujithcd3d39a2008-08-11 14:03:34 +0530338 if (bf_isxretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700339 tx_status.flags |= ATH_TX_XRETRY;
340 }
Sujith102e0572008-10-29 10:15:16 +0530341
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700342 /* Unmap this frame */
Gabor Juhosf5870ac2009-01-14 20:17:02 +0100343 pci_unmap_single(to_pci_dev(sc->dev),
Sujithff9b6622008-08-14 13:27:16 +0530344 bf->bf_dmacontext,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700345 skb->len,
346 PCI_DMA_TODEVICE);
347 /* complete this frame */
Sujith528f0c62008-10-29 10:14:26 +0530348 ath_tx_complete(sc, skb, &tx_status);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700349
350 /*
351 * Return the list of ath_buf of this mpdu to free queue
352 */
Sujithb77f4832008-12-07 21:44:03 +0530353 spin_lock_irqsave(&sc->tx.txbuflock, flags);
354 list_splice_tail_init(bf_q, &sc->tx.txbuf);
355 spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700356}
357
358/*
359 * queue up a dest/ac pair for tx scheduling
360 * NB: must be called with txq lock held
361 */
362
363static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
364{
365 struct ath_atx_ac *ac = tid->ac;
366
367 /*
368 * if tid is paused, hold off
369 */
370 if (tid->paused)
371 return;
372
373 /*
374 * add tid to ac atmost once
375 */
376 if (tid->sched)
377 return;
378
379 tid->sched = true;
380 list_add_tail(&tid->list, &ac->tid_q);
381
382 /*
383 * add node ac to txq atmost once
384 */
385 if (ac->sched)
386 return;
387
388 ac->sched = true;
389 list_add_tail(&ac->list, &txq->axq_acq);
390}
391
392/* pause a tid */
393
394static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
395{
Sujithb77f4832008-12-07 21:44:03 +0530396 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700397
398 spin_lock_bh(&txq->axq_lock);
399
400 tid->paused++;
401
402 spin_unlock_bh(&txq->axq_lock);
403}
404
405/* resume a tid and schedule aggregate */
406
407void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
408{
Sujithb77f4832008-12-07 21:44:03 +0530409 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700410
411 ASSERT(tid->paused > 0);
412 spin_lock_bh(&txq->axq_lock);
413
414 tid->paused--;
415
416 if (tid->paused > 0)
417 goto unlock;
418
419 if (list_empty(&tid->buf_q))
420 goto unlock;
421
422 /*
423 * Add this TID to scheduler and try to send out aggregates
424 */
425 ath_tx_queue_tid(txq, tid);
426 ath_txq_schedule(sc, txq);
427unlock:
428 spin_unlock_bh(&txq->axq_lock);
429}
430
431/* Compute the number of bad frames */
432
Sujithb5aa9bf2008-10-29 10:13:31 +0530433static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
434 int txok)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700435{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700436 struct ath_buf *bf_last = bf->bf_lastbf;
437 struct ath_desc *ds = bf_last->bf_desc;
438 u16 seq_st = 0;
439 u32 ba[WME_BA_BMP_SIZE >> 5];
440 int ba_index;
441 int nbad = 0;
442 int isaggr = 0;
443
Sujithb5aa9bf2008-10-29 10:13:31 +0530444 if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700445 return 0;
446
Sujithcd3d39a2008-08-11 14:03:34 +0530447 isaggr = bf_isaggr(bf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700448 if (isaggr) {
449 seq_st = ATH_DS_BA_SEQ(ds);
450 memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
451 }
452
453 while (bf) {
454 ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
455 if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
456 nbad++;
457
458 bf = bf->bf_next;
459 }
460
461 return nbad;
462}
463
464static void ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
465{
466 struct sk_buff *skb;
467 struct ieee80211_hdr *hdr;
468
Sujithcd3d39a2008-08-11 14:03:34 +0530469 bf->bf_state.bf_type |= BUF_RETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700470 bf->bf_retries++;
471
472 skb = bf->bf_mpdu;
473 hdr = (struct ieee80211_hdr *)skb->data;
474 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
475}
476
477/* Update block ack window */
478
Sujith102e0572008-10-29 10:15:16 +0530479static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
480 int seqno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700481{
482 int index, cindex;
483
484 index = ATH_BA_INDEX(tid->seq_start, seqno);
485 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
486
487 tid->tx_buf[cindex] = NULL;
488
489 while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
490 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
491 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
492 }
493}
494
495/*
496 * ath_pkt_dur - compute packet duration (NB: not NAV)
497 *
498 * rix - rate index
499 * pktlen - total bytes (delims + data + fcs + pads + pad delims)
500 * width - 0 for 20 MHz, 1 for 40 MHz
501 * half_gi - to use 4us v/s 3.6 us for symbol time
502 */
Sujith102e0572008-10-29 10:15:16 +0530503static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
504 int width, int half_gi, bool shortPreamble)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700505{
Sujith3706de62008-12-07 21:42:10 +0530506 struct ath_rate_table *rate_table = sc->cur_rate_table;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700507 u32 nbits, nsymbits, duration, nsymbols;
508 u8 rc;
509 int streams, pktlen;
510
Sujithcd3d39a2008-08-11 14:03:34 +0530511 pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
Sujithe63835b2008-11-18 09:07:53 +0530512 rc = rate_table->info[rix].ratecode;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700513
Sujithe63835b2008-11-18 09:07:53 +0530514 /* for legacy rates, use old function to compute packet duration */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700515 if (!IS_HT_RATE(rc))
Sujithe63835b2008-11-18 09:07:53 +0530516 return ath9k_hw_computetxtime(sc->sc_ah, rate_table, pktlen,
517 rix, shortPreamble);
518
519 /* find number of symbols: PLCP + data */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700520 nbits = (pktlen << 3) + OFDM_PLCP_BITS;
521 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
522 nsymbols = (nbits + nsymbits - 1) / nsymbits;
523
524 if (!half_gi)
525 duration = SYMBOL_TIME(nsymbols);
526 else
527 duration = SYMBOL_TIME_HALFGI(nsymbols);
528
Sujithe63835b2008-11-18 09:07:53 +0530529 /* addup duration for legacy/ht training and signal fields */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700530 streams = HT_RC_2_STREAMS(rc);
531 duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
Sujith102e0572008-10-29 10:15:16 +0530532
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700533 return duration;
534}
535
536/* Rate module function to set rate related fields in tx descriptor */
537
538static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
539{
540 struct ath_hal *ah = sc->sc_ah;
Sujithe63835b2008-11-18 09:07:53 +0530541 struct ath_rate_table *rt;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700542 struct ath_desc *ds = bf->bf_desc;
543 struct ath_desc *lastds = bf->bf_lastbf->bf_desc;
544 struct ath9k_11n_rate_series series[4];
Sujith528f0c62008-10-29 10:14:26 +0530545 struct sk_buff *skb;
546 struct ieee80211_tx_info *tx_info;
Sujitha8efee42008-11-18 09:07:30 +0530547 struct ieee80211_tx_rate *rates;
Sujithe63835b2008-11-18 09:07:53 +0530548 struct ieee80211_hdr *hdr;
Luis R. Rodriguez96742252008-12-23 15:58:38 -0800549 struct ieee80211_hw *hw = sc->hw;
550 int i, flags, rtsctsena = 0, enable_g_protection = 0;
Sujithe63835b2008-11-18 09:07:53 +0530551 u32 ctsduration = 0;
552 u8 rix = 0, cix, ctsrate = 0;
553 __le16 fc;
554
555 memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
Sujith528f0c62008-10-29 10:14:26 +0530556
557 skb = (struct sk_buff *)bf->bf_mpdu;
Sujithe63835b2008-11-18 09:07:53 +0530558 hdr = (struct ieee80211_hdr *)skb->data;
559 fc = hdr->frame_control;
Sujith528f0c62008-10-29 10:14:26 +0530560 tx_info = IEEE80211_SKB_CB(skb);
Sujithe63835b2008-11-18 09:07:53 +0530561 rates = tx_info->control.rates;
Sujith528f0c62008-10-29 10:14:26 +0530562
Sujithe63835b2008-11-18 09:07:53 +0530563 if (ieee80211_has_morefrags(fc) ||
564 (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)) {
565 rates[1].count = rates[2].count = rates[3].count = 0;
566 rates[1].idx = rates[2].idx = rates[3].idx = 0;
567 rates[0].count = ATH_TXMAXTRY;
568 }
569
570 /* get the cix for the lowest valid rix */
Sujith3706de62008-12-07 21:42:10 +0530571 rt = sc->cur_rate_table;
Sujitha8efee42008-11-18 09:07:30 +0530572 for (i = 3; i >= 0; i--) {
Sujithe63835b2008-11-18 09:07:53 +0530573 if (rates[i].count && (rates[i].idx >= 0)) {
Sujitha8efee42008-11-18 09:07:30 +0530574 rix = rates[i].idx;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700575 break;
576 }
577 }
Sujithe63835b2008-11-18 09:07:53 +0530578
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700579 flags = (bf->bf_flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA));
Sujithe63835b2008-11-18 09:07:53 +0530580 cix = rt->info[rix].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700581
Luis R. Rodriguez96742252008-12-23 15:58:38 -0800582 /* All protection frames are transmited at 2Mb/s for 802.11g,
583 * otherwise we transmit them at 1Mb/s */
584 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ &&
585 !conf_is_ht(&hw->conf))
586 enable_g_protection = 1;
587
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700588 /*
Sujithe63835b2008-11-18 09:07:53 +0530589 * If 802.11g protection is enabled, determine whether to use RTS/CTS or
590 * just CTS. Note that this is only done for OFDM/HT unicast frames.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700591 */
Sujithe63835b2008-11-18 09:07:53 +0530592 if (sc->sc_protmode != PROT_M_NONE && !(bf->bf_flags & ATH9K_TXDESC_NOACK)
Sujith46d14a52008-11-18 09:08:13 +0530593 && (rt->info[rix].phy == WLAN_RC_PHY_OFDM ||
Sujithe63835b2008-11-18 09:07:53 +0530594 WLAN_RC_PHY_HT(rt->info[rix].phy))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700595 if (sc->sc_protmode == PROT_M_RTSCTS)
596 flags = ATH9K_TXDESC_RTSENA;
597 else if (sc->sc_protmode == PROT_M_CTSONLY)
598 flags = ATH9K_TXDESC_CTSENA;
599
Luis R. Rodriguez96742252008-12-23 15:58:38 -0800600 cix = rt->info[enable_g_protection].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700601 rtsctsena = 1;
602 }
603
Sujithe63835b2008-11-18 09:07:53 +0530604 /* For 11n, the default behavior is to enable RTS for hw retried frames.
605 * We enable the global flag here and let rate series flags determine
606 * which rates will actually use RTS.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700607 */
Sujithcd3d39a2008-08-11 14:03:34 +0530608 if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) && bf_isdata(bf)) {
Sujithe63835b2008-11-18 09:07:53 +0530609 /* 802.11g protection not needed, use our default behavior */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700610 if (!rtsctsena)
611 flags = ATH9K_TXDESC_RTSENA;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700612 }
613
Sujithe63835b2008-11-18 09:07:53 +0530614 /* Set protection if aggregate protection on */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700615 if (sc->sc_config.ath_aggr_prot &&
Sujithcd3d39a2008-08-11 14:03:34 +0530616 (!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700617 flags = ATH9K_TXDESC_RTSENA;
Luis R. Rodriguez96742252008-12-23 15:58:38 -0800618 cix = rt->info[enable_g_protection].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700619 rtsctsena = 1;
620 }
621
Sujithe63835b2008-11-18 09:07:53 +0530622 /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
623 if (bf_isaggr(bf) && (bf->bf_al > ah->ah_caps.rts_aggr_limit))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700624 flags &= ~(ATH9K_TXDESC_RTSENA);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700625
626 /*
Sujithe63835b2008-11-18 09:07:53 +0530627 * CTS transmit rate is derived from the transmit rate by looking in the
628 * h/w rate table. We must also factor in whether or not a short
629 * preamble is to be used. NB: cix is set above where RTS/CTS is enabled
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700630 */
Sujithe63835b2008-11-18 09:07:53 +0530631 ctsrate = rt->info[cix].ratecode |
632 (bf_isshpreamble(bf) ? rt->info[cix].short_preamble : 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700633
634 for (i = 0; i < 4; i++) {
Sujithe63835b2008-11-18 09:07:53 +0530635 if (!rates[i].count || (rates[i].idx < 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700636 continue;
637
Sujitha8efee42008-11-18 09:07:30 +0530638 rix = rates[i].idx;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700639
Sujithe63835b2008-11-18 09:07:53 +0530640 series[i].Rate = rt->info[rix].ratecode |
641 (bf_isshpreamble(bf) ? rt->info[rix].short_preamble : 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700642
Sujitha8efee42008-11-18 09:07:30 +0530643 series[i].Tries = rates[i].count;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700644
645 series[i].RateFlags = (
Sujitha8efee42008-11-18 09:07:30 +0530646 (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700647 ATH9K_RATESERIES_RTS_CTS : 0) |
Sujitha8efee42008-11-18 09:07:30 +0530648 ((rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700649 ATH9K_RATESERIES_2040 : 0) |
Sujitha8efee42008-11-18 09:07:30 +0530650 ((rates[i].flags & IEEE80211_TX_RC_SHORT_GI) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700651 ATH9K_RATESERIES_HALFGI : 0);
652
Sujith102e0572008-10-29 10:15:16 +0530653 series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
Sujitha8efee42008-11-18 09:07:30 +0530654 (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) != 0,
655 (rates[i].flags & IEEE80211_TX_RC_SHORT_GI),
Sujith102e0572008-10-29 10:15:16 +0530656 bf_isshpreamble(bf));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700657
Sujithff37e332008-11-24 12:07:55 +0530658 series[i].ChSel = sc->sc_tx_chainmask;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700659
660 if (rtsctsena)
661 series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700662 }
663
Sujithe63835b2008-11-18 09:07:53 +0530664 /* set dur_update_en for l-sig computation except for PS-Poll frames */
665 ath9k_hw_set11n_ratescenario(ah, ds, lastds, !bf_ispspoll(bf),
666 ctsrate, ctsduration,
Sujithcd3d39a2008-08-11 14:03:34 +0530667 series, 4, flags);
Sujith102e0572008-10-29 10:15:16 +0530668
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700669 if (sc->sc_config.ath_aggr_prot && flags)
670 ath9k_hw_set11n_burstduration(ah, ds, 8192);
671}
672
673/*
674 * Function to send a normal HT (non-AMPDU) frame
675 * NB: must be called with txq lock held
676 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700677static int ath_tx_send_normal(struct ath_softc *sc,
678 struct ath_txq *txq,
679 struct ath_atx_tid *tid,
680 struct list_head *bf_head)
681{
682 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700683
684 BUG_ON(list_empty(bf_head));
685
686 bf = list_first_entry(bf_head, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +0530687 bf->bf_state.bf_type &= ~BUF_AMPDU; /* regular HT frame */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700688
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700689 /* update starting sequence number for subsequent ADDBA request */
690 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
691
692 /* Queue to h/w without aggregation */
693 bf->bf_nframes = 1;
694 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
695 ath_buf_set_rate(sc, bf);
696 ath_tx_txqaddbuf(sc, txq, bf_head);
697
698 return 0;
699}
700
701/* flush tid's software queue and send frames as non-ampdu's */
702
703static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
704{
Sujithb77f4832008-12-07 21:44:03 +0530705 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700706 struct ath_buf *bf;
707 struct list_head bf_head;
708 INIT_LIST_HEAD(&bf_head);
709
710 ASSERT(tid->paused > 0);
711 spin_lock_bh(&txq->axq_lock);
712
713 tid->paused--;
714
715 if (tid->paused > 0) {
716 spin_unlock_bh(&txq->axq_lock);
717 return;
718 }
719
720 while (!list_empty(&tid->buf_q)) {
721 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +0530722 ASSERT(!bf_isretried(bf));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700723 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
724 ath_tx_send_normal(sc, txq, tid, &bf_head);
725 }
726
727 spin_unlock_bh(&txq->axq_lock);
728}
729
730/* Completion routine of an aggregate */
731
732static void ath_tx_complete_aggr_rifs(struct ath_softc *sc,
733 struct ath_txq *txq,
734 struct ath_buf *bf,
735 struct list_head *bf_q,
736 int txok)
737{
Sujith528f0c62008-10-29 10:14:26 +0530738 struct ath_node *an = NULL;
739 struct sk_buff *skb;
740 struct ieee80211_tx_info *tx_info;
741 struct ath_atx_tid *tid = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700742 struct ath_buf *bf_last = bf->bf_lastbf;
743 struct ath_desc *ds = bf_last->bf_desc;
744 struct ath_buf *bf_next, *bf_lastq = NULL;
745 struct list_head bf_head, bf_pending;
746 u16 seq_st = 0;
747 u32 ba[WME_BA_BMP_SIZE >> 5];
748 int isaggr, txfail, txpending, sendbar = 0, needreset = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700749
Sujith528f0c62008-10-29 10:14:26 +0530750 skb = (struct sk_buff *)bf->bf_mpdu;
751 tx_info = IEEE80211_SKB_CB(skb);
752
753 if (tx_info->control.sta) {
754 an = (struct ath_node *)tx_info->control.sta->drv_priv;
755 tid = ATH_AN_2_TID(an, bf->bf_tidno);
756 }
757
Sujithcd3d39a2008-08-11 14:03:34 +0530758 isaggr = bf_isaggr(bf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700759 if (isaggr) {
760 if (txok) {
761 if (ATH_DS_TX_BA(ds)) {
762 /*
763 * extract starting sequence and
764 * block-ack bitmap
765 */
766 seq_st = ATH_DS_BA_SEQ(ds);
767 memcpy(ba,
768 ATH_DS_BA_BITMAP(ds),
769 WME_BA_BMP_SIZE >> 3);
770 } else {
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700771 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700772
773 /*
774 * AR5416 can become deaf/mute when BA
775 * issue happens. Chip needs to be reset.
776 * But AP code may have sychronization issues
777 * when perform internal reset in this routine.
778 * Only enable reset in STA mode for now.
779 */
Colin McCabed97809d2008-12-01 13:38:55 -0800780 if (sc->sc_ah->ah_opmode ==
781 NL80211_IFTYPE_STATION)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700782 needreset = 1;
783 }
784 } else {
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700785 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700786 }
787 }
788
789 INIT_LIST_HEAD(&bf_pending);
790 INIT_LIST_HEAD(&bf_head);
791
792 while (bf) {
793 txfail = txpending = 0;
794 bf_next = bf->bf_next;
795
796 if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
797 /* transmit completion, subframe is
798 * acked by block ack */
799 } else if (!isaggr && txok) {
800 /* transmit completion */
801 } else {
802
Sujitha37c2c72008-10-29 10:15:40 +0530803 if (!(tid->state & AGGR_CLEANUP) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700804 ds->ds_txstat.ts_flags != ATH9K_TX_SW_ABORTED) {
805 if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
806 ath_tx_set_retry(sc, bf);
807 txpending = 1;
808 } else {
Sujithcd3d39a2008-08-11 14:03:34 +0530809 bf->bf_state.bf_type |= BUF_XRETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700810 txfail = 1;
811 sendbar = 1;
812 }
813 } else {
814 /*
815 * cleanup in progress, just fail
816 * the un-acked sub-frames
817 */
818 txfail = 1;
819 }
820 }
821 /*
822 * Remove ath_buf's of this sub-frame from aggregate queue.
823 */
824 if (bf_next == NULL) { /* last subframe in the aggregate */
825 ASSERT(bf->bf_lastfrm == bf_last);
826
827 /*
828 * The last descriptor of the last sub frame could be
829 * a holding descriptor for h/w. If that's the case,
830 * bf->bf_lastfrm won't be in the bf_q.
831 * Make sure we handle bf_q properly here.
832 */
833
834 if (!list_empty(bf_q)) {
835 bf_lastq = list_entry(bf_q->prev,
836 struct ath_buf, list);
837 list_cut_position(&bf_head,
838 bf_q, &bf_lastq->list);
839 } else {
840 /*
841 * XXX: if the last subframe only has one
842 * descriptor which is also being used as
843 * a holding descriptor. Then the ath_buf
844 * is not in the bf_q at all.
845 */
846 INIT_LIST_HEAD(&bf_head);
847 }
848 } else {
849 ASSERT(!list_empty(bf_q));
850 list_cut_position(&bf_head,
851 bf_q, &bf->bf_lastfrm->list);
852 }
853
854 if (!txpending) {
855 /*
856 * complete the acked-ones/xretried ones; update
857 * block-ack window
858 */
859 spin_lock_bh(&txq->axq_lock);
860 ath_tx_update_baw(sc, tid, bf->bf_seqno);
861 spin_unlock_bh(&txq->axq_lock);
862
863 /* complete this sub-frame */
864 ath_tx_complete_buf(sc, bf, &bf_head, !txfail, sendbar);
865 } else {
866 /*
867 * retry the un-acked ones
868 */
869 /*
870 * XXX: if the last descriptor is holding descriptor,
871 * in order to requeue the frame to software queue, we
872 * need to allocate a new descriptor and
873 * copy the content of holding descriptor to it.
874 */
875 if (bf->bf_next == NULL &&
876 bf_last->bf_status & ATH_BUFSTATUS_STALE) {
877 struct ath_buf *tbf;
878
879 /* allocate new descriptor */
Sujithb77f4832008-12-07 21:44:03 +0530880 spin_lock_bh(&sc->tx.txbuflock);
881 ASSERT(!list_empty((&sc->tx.txbuf)));
882 tbf = list_first_entry(&sc->tx.txbuf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700883 struct ath_buf, list);
884 list_del(&tbf->list);
Sujithb77f4832008-12-07 21:44:03 +0530885 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700886
887 ATH_TXBUF_RESET(tbf);
888
889 /* copy descriptor content */
890 tbf->bf_mpdu = bf_last->bf_mpdu;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700891 tbf->bf_buf_addr = bf_last->bf_buf_addr;
892 *(tbf->bf_desc) = *(bf_last->bf_desc);
893
894 /* link it to the frame */
895 if (bf_lastq) {
896 bf_lastq->bf_desc->ds_link =
897 tbf->bf_daddr;
898 bf->bf_lastfrm = tbf;
899 ath9k_hw_cleartxdesc(sc->sc_ah,
900 bf->bf_lastfrm->bf_desc);
901 } else {
902 tbf->bf_state = bf_last->bf_state;
903 tbf->bf_lastfrm = tbf;
904 ath9k_hw_cleartxdesc(sc->sc_ah,
905 tbf->bf_lastfrm->bf_desc);
906
907 /* copy the DMA context */
Sujithff9b6622008-08-14 13:27:16 +0530908 tbf->bf_dmacontext =
909 bf_last->bf_dmacontext;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700910 }
911 list_add_tail(&tbf->list, &bf_head);
912 } else {
913 /*
914 * Clear descriptor status words for
915 * software retry
916 */
917 ath9k_hw_cleartxdesc(sc->sc_ah,
Sujithff9b6622008-08-14 13:27:16 +0530918 bf->bf_lastfrm->bf_desc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700919 }
920
921 /*
922 * Put this buffer to the temporary pending
923 * queue to retain ordering
924 */
925 list_splice_tail_init(&bf_head, &bf_pending);
926 }
927
928 bf = bf_next;
929 }
930
Sujitha37c2c72008-10-29 10:15:40 +0530931 if (tid->state & AGGR_CLEANUP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700932 /* check to see if we're done with cleaning the h/w queue */
933 spin_lock_bh(&txq->axq_lock);
934
935 if (tid->baw_head == tid->baw_tail) {
Sujitha37c2c72008-10-29 10:15:40 +0530936 tid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700937 tid->addba_exchangeattempts = 0;
938 spin_unlock_bh(&txq->axq_lock);
939
Sujitha37c2c72008-10-29 10:15:40 +0530940 tid->state &= ~AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700941
942 /* send buffered frames as singles */
943 ath_tx_flush_tid(sc, tid);
944 } else
945 spin_unlock_bh(&txq->axq_lock);
946
947 return;
948 }
949
950 /*
951 * prepend un-acked frames to the beginning of the pending frame queue
952 */
953 if (!list_empty(&bf_pending)) {
954 spin_lock_bh(&txq->axq_lock);
955 /* Note: we _prepend_, we _do_not_ at to
956 * the end of the queue ! */
957 list_splice(&bf_pending, &tid->buf_q);
958 ath_tx_queue_tid(txq, tid);
959 spin_unlock_bh(&txq->axq_lock);
960 }
961
962 if (needreset)
Sujithf45144e2008-08-11 14:02:53 +0530963 ath_reset(sc, false);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700964
965 return;
966}
967
Sujithc4288392008-11-18 09:09:30 +0530968static void ath_tx_rc_status(struct ath_buf *bf, struct ath_desc *ds, int nbad)
969{
970 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
971 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
972 struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
973
Vasanthakumar Thiagarajan7ac47012008-11-20 11:51:18 +0530974 tx_info_priv->update_rc = false;
Sujithc4288392008-11-18 09:09:30 +0530975 if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
976 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
977
978 if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
979 (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
980 if (bf_isdata(bf)) {
981 memcpy(&tx_info_priv->tx, &ds->ds_txstat,
982 sizeof(tx_info_priv->tx));
983 tx_info_priv->n_frames = bf->bf_nframes;
984 tx_info_priv->n_bad_frames = nbad;
Vasanthakumar Thiagarajan7ac47012008-11-20 11:51:18 +0530985 tx_info_priv->update_rc = true;
Sujithc4288392008-11-18 09:09:30 +0530986 }
987 }
988}
989
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700990/* Process completed xmit descriptors from the specified queue */
991
Sujithc4288392008-11-18 09:09:30 +0530992static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700993{
994 struct ath_hal *ah = sc->sc_ah;
995 struct ath_buf *bf, *lastbf, *bf_held = NULL;
996 struct list_head bf_head;
Sujithc4288392008-11-18 09:09:30 +0530997 struct ath_desc *ds;
998 int txok, nbad = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700999 int status;
1000
Sujith04bd4632008-11-28 22:18:05 +05301001 DPRINTF(sc, ATH_DBG_QUEUE, "tx queue %d (%x), link %p\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001002 txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
1003 txq->axq_link);
1004
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001005 for (;;) {
1006 spin_lock_bh(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001007 if (list_empty(&txq->axq_q)) {
1008 txq->axq_link = NULL;
1009 txq->axq_linkbuf = NULL;
1010 spin_unlock_bh(&txq->axq_lock);
1011 break;
1012 }
1013 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
1014
1015 /*
1016 * There is a race condition that a BH gets scheduled
1017 * after sw writes TxE and before hw re-load the last
1018 * descriptor to get the newly chained one.
1019 * Software must keep the last DONE descriptor as a
1020 * holding descriptor - software does so by marking
1021 * it with the STALE flag.
1022 */
1023 bf_held = NULL;
1024 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
1025 bf_held = bf;
1026 if (list_is_last(&bf_held->list, &txq->axq_q)) {
1027 /* FIXME:
1028 * The holding descriptor is the last
1029 * descriptor in queue. It's safe to remove
1030 * the last holding descriptor in BH context.
1031 */
1032 spin_unlock_bh(&txq->axq_lock);
1033 break;
1034 } else {
1035 /* Lets work with the next buffer now */
1036 bf = list_entry(bf_held->list.next,
1037 struct ath_buf, list);
1038 }
1039 }
1040
1041 lastbf = bf->bf_lastbf;
1042 ds = lastbf->bf_desc; /* NB: last decriptor */
1043
1044 status = ath9k_hw_txprocdesc(ah, ds);
1045 if (status == -EINPROGRESS) {
1046 spin_unlock_bh(&txq->axq_lock);
1047 break;
1048 }
1049 if (bf->bf_desc == txq->axq_lastdsWithCTS)
1050 txq->axq_lastdsWithCTS = NULL;
1051 if (ds == txq->axq_gatingds)
1052 txq->axq_gatingds = NULL;
1053
1054 /*
1055 * Remove ath_buf's of the same transmit unit from txq,
1056 * however leave the last descriptor back as the holding
1057 * descriptor for hw.
1058 */
1059 lastbf->bf_status |= ATH_BUFSTATUS_STALE;
1060 INIT_LIST_HEAD(&bf_head);
1061
1062 if (!list_is_singular(&lastbf->list))
1063 list_cut_position(&bf_head,
1064 &txq->axq_q, lastbf->list.prev);
1065
1066 txq->axq_depth--;
1067
Sujithcd3d39a2008-08-11 14:03:34 +05301068 if (bf_isaggr(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001069 txq->axq_aggr_depth--;
1070
1071 txok = (ds->ds_txstat.ts_status == 0);
1072
1073 spin_unlock_bh(&txq->axq_lock);
1074
1075 if (bf_held) {
1076 list_del(&bf_held->list);
Sujithb77f4832008-12-07 21:44:03 +05301077 spin_lock_bh(&sc->tx.txbuflock);
1078 list_add_tail(&bf_held->list, &sc->tx.txbuf);
1079 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001080 }
1081
Sujithcd3d39a2008-08-11 14:03:34 +05301082 if (!bf_isampdu(bf)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001083 /*
1084 * This frame is sent out as a single frame.
1085 * Use hardware retry status for this frame.
1086 */
1087 bf->bf_retries = ds->ds_txstat.ts_longretry;
1088 if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
Sujithcd3d39a2008-08-11 14:03:34 +05301089 bf->bf_state.bf_type |= BUF_XRETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001090 nbad = 0;
1091 } else {
1092 nbad = ath_tx_num_badfrms(sc, bf, txok);
1093 }
Johannes Berge6a98542008-10-21 12:40:02 +02001094
Sujithc4288392008-11-18 09:09:30 +05301095 ath_tx_rc_status(bf, ds, nbad);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001096
1097 /*
1098 * Complete this transmit unit
1099 */
Sujithcd3d39a2008-08-11 14:03:34 +05301100 if (bf_isampdu(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001101 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, txok);
1102 else
1103 ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
1104
1105 /* Wake up mac80211 queue */
1106
1107 spin_lock_bh(&txq->axq_lock);
1108 if (txq->stopped && ath_txq_depth(sc, txq->axq_qnum) <=
1109 (ATH_TXBUF - 20)) {
1110 int qnum;
1111 qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
1112 if (qnum != -1) {
1113 ieee80211_wake_queue(sc->hw, qnum);
1114 txq->stopped = 0;
1115 }
1116
1117 }
1118
1119 /*
1120 * schedule any pending packets if aggregation is enabled
1121 */
Sujith672840a2008-08-11 14:05:08 +05301122 if (sc->sc_flags & SC_OP_TXAGGR)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001123 ath_txq_schedule(sc, txq);
1124 spin_unlock_bh(&txq->axq_lock);
1125 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001126}
1127
1128static void ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
1129{
1130 struct ath_hal *ah = sc->sc_ah;
1131
1132 (void) ath9k_hw_stoptxdma(ah, txq->axq_qnum);
Sujith04bd4632008-11-28 22:18:05 +05301133 DPRINTF(sc, ATH_DBG_XMIT, "tx queue [%u] %x, link %p\n",
1134 txq->axq_qnum, ath9k_hw_gettxbuf(ah, txq->axq_qnum),
1135 txq->axq_link);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001136}
1137
1138/* Drain only the data queues */
1139
1140static void ath_drain_txdataq(struct ath_softc *sc, bool retry_tx)
1141{
1142 struct ath_hal *ah = sc->sc_ah;
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001143 int i, npend = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001144
Sujith672840a2008-08-11 14:05:08 +05301145 if (!(sc->sc_flags & SC_OP_INVALID)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001146 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1147 if (ATH_TXQ_SETUP(sc, i)) {
Sujithb77f4832008-12-07 21:44:03 +05301148 ath_tx_stopdma(sc, &sc->tx.txq[i]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001149 /* The TxDMA may not really be stopped.
1150 * Double check the hal tx pending count */
1151 npend += ath9k_hw_numtxpending(ah,
Sujithb77f4832008-12-07 21:44:03 +05301152 sc->tx.txq[i].axq_qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001153 }
1154 }
1155 }
1156
1157 if (npend) {
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001158 int r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001159 /* TxDMA not stopped, reset the hal */
Sujith04bd4632008-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);
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001163 r = ath9k_hw_reset(ah, sc->sc_ah->ah_curchan, true);
1164 if (r)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001165 DPRINTF(sc, ATH_DBG_FATAL,
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -08001166 "Unable to reset hardware; reset status %u\n",
1167 r);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001168 spin_unlock_bh(&sc->sc_resetlock);
1169 }
1170
1171 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1172 if (ATH_TXQ_SETUP(sc, i))
Sujithb77f4832008-12-07 21:44:03 +05301173 ath_tx_draintxq(sc, &sc->tx.txq[i], retry_tx);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001174 }
1175}
1176
1177/* Add a sub-frame to block ack window */
1178
1179static void ath_tx_addto_baw(struct ath_softc *sc,
1180 struct ath_atx_tid *tid,
1181 struct ath_buf *bf)
1182{
1183 int index, cindex;
1184
Sujithcd3d39a2008-08-11 14:03:34 +05301185 if (bf_isretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001186 return;
1187
1188 index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
1189 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
1190
1191 ASSERT(tid->tx_buf[cindex] == NULL);
1192 tid->tx_buf[cindex] = bf;
1193
1194 if (index >= ((tid->baw_tail - tid->baw_head) &
1195 (ATH_TID_MAX_BUFS - 1))) {
1196 tid->baw_tail = cindex;
1197 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
1198 }
1199}
1200
1201/*
1202 * Function to send an A-MPDU
1203 * NB: must be called with txq lock held
1204 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001205static int ath_tx_send_ampdu(struct ath_softc *sc,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001206 struct ath_atx_tid *tid,
1207 struct list_head *bf_head,
1208 struct ath_tx_control *txctl)
1209{
1210 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001211
1212 BUG_ON(list_empty(bf_head));
1213
1214 bf = list_first_entry(bf_head, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +05301215 bf->bf_state.bf_type |= BUF_AMPDU;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001216
1217 /*
1218 * Do not queue to h/w when any of the following conditions is true:
1219 * - there are pending frames in software queue
1220 * - the TID is currently paused for ADDBA/BAR request
1221 * - seqno is not within block-ack window
1222 * - h/w queue depth exceeds low water mark
1223 */
1224 if (!list_empty(&tid->buf_q) || tid->paused ||
1225 !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
Sujith528f0c62008-10-29 10:14:26 +05301226 txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001227 /*
1228 * Add this frame to software queue for scheduling later
1229 * for aggregation.
1230 */
1231 list_splice_tail_init(bf_head, &tid->buf_q);
Sujith528f0c62008-10-29 10:14:26 +05301232 ath_tx_queue_tid(txctl->txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001233 return 0;
1234 }
1235
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001236 /* Add sub-frame to BAW */
1237 ath_tx_addto_baw(sc, tid, bf);
1238
1239 /* Queue to h/w without aggregation */
1240 bf->bf_nframes = 1;
1241 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
1242 ath_buf_set_rate(sc, bf);
Sujith528f0c62008-10-29 10:14:26 +05301243 ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
Sujith102e0572008-10-29 10:15:16 +05301244
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001245 return 0;
1246}
1247
1248/*
1249 * looks up the rate
1250 * returns aggr limit based on lowest of the rates
1251 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001252static u32 ath_lookup_rate(struct ath_softc *sc,
Johannes Bergae5eb022008-10-14 16:58:37 +02001253 struct ath_buf *bf,
1254 struct ath_atx_tid *tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001255{
Sujith3706de62008-12-07 21:42:10 +05301256 struct ath_rate_table *rate_table = sc->cur_rate_table;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001257 struct sk_buff *skb;
1258 struct ieee80211_tx_info *tx_info;
Sujitha8efee42008-11-18 09:07:30 +05301259 struct ieee80211_tx_rate *rates;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001260 struct ath_tx_info_priv *tx_info_priv;
1261 u32 max_4ms_framelen, frame_length;
1262 u16 aggr_limit, legacy = 0, maxampdu;
1263 int i;
1264
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001265 skb = (struct sk_buff *)bf->bf_mpdu;
1266 tx_info = IEEE80211_SKB_CB(skb);
Sujitha8efee42008-11-18 09:07:30 +05301267 rates = tx_info->control.rates;
1268 tx_info_priv =
1269 (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001270
1271 /*
1272 * Find the lowest frame length among the rate series that will have a
1273 * 4ms transmit duration.
1274 * TODO - TXOP limit needs to be considered.
1275 */
1276 max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
1277
1278 for (i = 0; i < 4; i++) {
Sujitha8efee42008-11-18 09:07:30 +05301279 if (rates[i].count) {
Sujithe63835b2008-11-18 09:07:53 +05301280 if (!WLAN_RC_PHY_HT(rate_table->info[rates[i].idx].phy)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001281 legacy = 1;
1282 break;
1283 }
1284
Sujitha8efee42008-11-18 09:07:30 +05301285 frame_length =
1286 rate_table->info[rates[i].idx].max_4ms_framelen;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001287 max_4ms_framelen = min(max_4ms_framelen, frame_length);
1288 }
1289 }
1290
1291 /*
1292 * limit aggregate size by the minimum rate if rate selected is
1293 * not a probe rate, if rate selected is a probe rate then
1294 * avoid aggregation of this packet.
1295 */
1296 if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
1297 return 0;
1298
1299 aggr_limit = min(max_4ms_framelen,
1300 (u32)ATH_AMPDU_LIMIT_DEFAULT);
1301
1302 /*
1303 * h/w can accept aggregates upto 16 bit lengths (65535).
1304 * The IE, however can hold upto 65536, which shows up here
1305 * as zero. Ignore 65536 since we are constrained by hw.
1306 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001307 maxampdu = tid->an->maxampdu;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001308 if (maxampdu)
1309 aggr_limit = min(aggr_limit, maxampdu);
1310
1311 return aggr_limit;
1312}
1313
1314/*
1315 * returns the number of delimiters to be added to
1316 * meet the minimum required mpdudensity.
1317 * caller should make sure that the rate is HT rate .
1318 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001319static int ath_compute_num_delims(struct ath_softc *sc,
Johannes Bergae5eb022008-10-14 16:58:37 +02001320 struct ath_atx_tid *tid,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001321 struct ath_buf *bf,
1322 u16 frmlen)
1323{
Sujith3706de62008-12-07 21:42:10 +05301324 struct ath_rate_table *rt = sc->cur_rate_table;
Sujitha8efee42008-11-18 09:07:30 +05301325 struct sk_buff *skb = bf->bf_mpdu;
1326 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001327 u32 nsymbits, nsymbols, mpdudensity;
1328 u16 minlen;
1329 u8 rc, flags, rix;
1330 int width, half_gi, ndelim, mindelim;
1331
1332 /* Select standard number of delimiters based on frame length alone */
1333 ndelim = ATH_AGGR_GET_NDELIM(frmlen);
1334
1335 /*
1336 * If encryption enabled, hardware requires some more padding between
1337 * subframes.
1338 * TODO - this could be improved to be dependent on the rate.
1339 * The hardware can keep up at lower rates, but not higher rates
1340 */
1341 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
1342 ndelim += ATH_AGGR_ENCRYPTDELIM;
1343
1344 /*
1345 * Convert desired mpdu density from microeconds to bytes based
1346 * on highest rate in rate series (i.e. first rate) to determine
1347 * required minimum length for subframe. Take into account
1348 * whether high rate is 20 or 40Mhz and half or full GI.
1349 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001350 mpdudensity = tid->an->mpdudensity;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001351
1352 /*
1353 * If there is no mpdu density restriction, no further calculation
1354 * is needed.
1355 */
1356 if (mpdudensity == 0)
1357 return ndelim;
1358
Sujitha8efee42008-11-18 09:07:30 +05301359 rix = tx_info->control.rates[0].idx;
1360 flags = tx_info->control.rates[0].flags;
Sujithe63835b2008-11-18 09:07:53 +05301361 rc = rt->info[rix].ratecode;
Sujitha8efee42008-11-18 09:07:30 +05301362 width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
1363 half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001364
1365 if (half_gi)
1366 nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
1367 else
1368 nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
1369
1370 if (nsymbols == 0)
1371 nsymbols = 1;
1372
1373 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
1374 minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
1375
1376 /* Is frame shorter than required minimum length? */
1377 if (frmlen < minlen) {
1378 /* Get the minimum number of delimiters required. */
1379 mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
1380 ndelim = max(mindelim, ndelim);
1381 }
1382
1383 return ndelim;
1384}
1385
1386/*
1387 * For aggregation from software buffer queue.
1388 * NB: must be called with txq lock held
1389 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001390static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
1391 struct ath_atx_tid *tid,
1392 struct list_head *bf_q,
1393 struct ath_buf **bf_last,
1394 struct aggr_rifs_param *param,
1395 int *prev_frames)
1396{
1397#define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
1398 struct ath_buf *bf, *tbf, *bf_first, *bf_prev = NULL;
1399 struct list_head bf_head;
1400 int rl = 0, nframes = 0, ndelim;
1401 u16 aggr_limit = 0, al = 0, bpad = 0,
1402 al_delta, h_baw = tid->baw_size / 2;
1403 enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
Sujitha8efee42008-11-18 09:07:30 +05301404 int prev_al = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001405 INIT_LIST_HEAD(&bf_head);
1406
1407 BUG_ON(list_empty(&tid->buf_q));
1408
1409 bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
1410
1411 do {
1412 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1413
1414 /*
1415 * do not step over block-ack window
1416 */
1417 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
1418 status = ATH_AGGR_BAW_CLOSED;
1419 break;
1420 }
1421
1422 if (!rl) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001423 aggr_limit = ath_lookup_rate(sc, bf, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001424 rl = 1;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001425 }
1426
1427 /*
1428 * do not exceed aggregation limit
1429 */
1430 al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
1431
1432 if (nframes && (aggr_limit <
1433 (al + bpad + al_delta + prev_al))) {
1434 status = ATH_AGGR_LIMITED;
1435 break;
1436 }
1437
1438 /*
1439 * do not exceed subframe limit
1440 */
1441 if ((nframes + *prev_frames) >=
1442 min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
1443 status = ATH_AGGR_LIMITED;
1444 break;
1445 }
1446
1447 /*
1448 * add padding for previous frame to aggregation length
1449 */
1450 al += bpad + al_delta;
1451
1452 /*
1453 * Get the delimiters needed to meet the MPDU
1454 * density for this node.
1455 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001456 ndelim = ath_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001457
1458 bpad = PADBYTES(al_delta) + (ndelim << 2);
1459
1460 bf->bf_next = NULL;
1461 bf->bf_lastfrm->bf_desc->ds_link = 0;
1462
1463 /*
1464 * this packet is part of an aggregate
1465 * - remove all descriptors belonging to this frame from
1466 * software queue
1467 * - add it to block ack window
1468 * - set up descriptors for aggregation
1469 */
1470 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1471 ath_tx_addto_baw(sc, tid, bf);
1472
1473 list_for_each_entry(tbf, &bf_head, list) {
1474 ath9k_hw_set11n_aggr_middle(sc->sc_ah,
1475 tbf->bf_desc, ndelim);
1476 }
1477
1478 /*
1479 * link buffers of this frame to the aggregate
1480 */
1481 list_splice_tail_init(&bf_head, bf_q);
1482 nframes++;
1483
1484 if (bf_prev) {
1485 bf_prev->bf_next = bf;
1486 bf_prev->bf_lastfrm->bf_desc->ds_link = bf->bf_daddr;
1487 }
1488 bf_prev = bf;
1489
1490#ifdef AGGR_NOSHORT
1491 /*
1492 * terminate aggregation on a small packet boundary
1493 */
1494 if (bf->bf_frmlen < ATH_AGGR_MINPLEN) {
1495 status = ATH_AGGR_SHORTPKT;
1496 break;
1497 }
1498#endif
1499 } while (!list_empty(&tid->buf_q));
1500
1501 bf_first->bf_al = al;
1502 bf_first->bf_nframes = nframes;
1503 *bf_last = bf_prev;
1504 return status;
1505#undef PADBYTES
1506}
1507
1508/*
1509 * process pending frames possibly doing a-mpdu aggregation
1510 * NB: must be called with txq lock held
1511 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001512static void ath_tx_sched_aggr(struct ath_softc *sc,
1513 struct ath_txq *txq, struct ath_atx_tid *tid)
1514{
1515 struct ath_buf *bf, *tbf, *bf_last, *bf_lastaggr = NULL;
1516 enum ATH_AGGR_STATUS status;
1517 struct list_head bf_q;
1518 struct aggr_rifs_param param = {0, 0, 0, 0, NULL};
1519 int prev_frames = 0;
1520
1521 do {
1522 if (list_empty(&tid->buf_q))
1523 return;
1524
1525 INIT_LIST_HEAD(&bf_q);
1526
1527 status = ath_tx_form_aggr(sc, tid, &bf_q, &bf_lastaggr, &param,
1528 &prev_frames);
1529
1530 /*
1531 * no frames picked up to be aggregated; block-ack
1532 * window is not open
1533 */
1534 if (list_empty(&bf_q))
1535 break;
1536
1537 bf = list_first_entry(&bf_q, struct ath_buf, list);
1538 bf_last = list_entry(bf_q.prev, struct ath_buf, list);
1539 bf->bf_lastbf = bf_last;
1540
1541 /*
1542 * if only one frame, send as non-aggregate
1543 */
1544 if (bf->bf_nframes == 1) {
1545 ASSERT(bf->bf_lastfrm == bf_last);
1546
Sujithcd3d39a2008-08-11 14:03:34 +05301547 bf->bf_state.bf_type &= ~BUF_AGGR;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001548 /*
1549 * clear aggr bits for every descriptor
1550 * XXX TODO: is there a way to optimize it?
1551 */
1552 list_for_each_entry(tbf, &bf_q, list) {
1553 ath9k_hw_clr11n_aggr(sc->sc_ah, tbf->bf_desc);
1554 }
1555
1556 ath_buf_set_rate(sc, bf);
1557 ath_tx_txqaddbuf(sc, txq, &bf_q);
1558 continue;
1559 }
1560
1561 /*
1562 * setup first desc with rate and aggr info
1563 */
Sujithcd3d39a2008-08-11 14:03:34 +05301564 bf->bf_state.bf_type |= BUF_AGGR;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001565 ath_buf_set_rate(sc, bf);
1566 ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
1567
1568 /*
1569 * anchor last frame of aggregate correctly
1570 */
1571 ASSERT(bf_lastaggr);
1572 ASSERT(bf_lastaggr->bf_lastfrm == bf_last);
1573 tbf = bf_lastaggr;
1574 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1575
1576 /* XXX: We don't enter into this loop, consider removing this */
1577 while (!list_empty(&bf_q) && !list_is_last(&tbf->list, &bf_q)) {
1578 tbf = list_entry(tbf->list.next, struct ath_buf, list);
1579 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1580 }
1581
1582 txq->axq_aggr_depth++;
1583
1584 /*
1585 * Normal aggregate, queue to hardware
1586 */
1587 ath_tx_txqaddbuf(sc, txq, &bf_q);
1588
1589 } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
1590 status != ATH_AGGR_BAW_CLOSED);
1591}
1592
1593/* Called with txq lock held */
1594
1595static void ath_tid_drain(struct ath_softc *sc,
1596 struct ath_txq *txq,
Sujithb5aa9bf2008-10-29 10:13:31 +05301597 struct ath_atx_tid *tid)
1598
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001599{
1600 struct ath_buf *bf;
1601 struct list_head bf_head;
1602 INIT_LIST_HEAD(&bf_head);
1603
1604 for (;;) {
1605 if (list_empty(&tid->buf_q))
1606 break;
1607 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1608
1609 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1610
1611 /* update baw for software retried frame */
Sujithcd3d39a2008-08-11 14:03:34 +05301612 if (bf_isretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001613 ath_tx_update_baw(sc, tid, bf->bf_seqno);
1614
1615 /*
1616 * do not indicate packets while holding txq spinlock.
1617 * unlock is intentional here
1618 */
Sujithb5aa9bf2008-10-29 10:13:31 +05301619 spin_unlock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001620
1621 /* complete this sub-frame */
1622 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
1623
Sujithb5aa9bf2008-10-29 10:13:31 +05301624 spin_lock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001625 }
1626
1627 /*
1628 * TODO: For frame(s) that are in the retry state, we will reuse the
1629 * sequence number(s) without setting the retry bit. The
1630 * alternative is to give up on these and BAR the receiver's window
1631 * forward.
1632 */
1633 tid->seq_next = tid->seq_start;
1634 tid->baw_tail = tid->baw_head;
1635}
1636
1637/*
1638 * Drain all pending buffers
1639 * NB: must be called with txq lock held
1640 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001641static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
Sujithb5aa9bf2008-10-29 10:13:31 +05301642 struct ath_txq *txq)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001643{
1644 struct ath_atx_ac *ac, *ac_tmp;
1645 struct ath_atx_tid *tid, *tid_tmp;
1646
1647 list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
1648 list_del(&ac->list);
1649 ac->sched = false;
1650 list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
1651 list_del(&tid->list);
1652 tid->sched = false;
Sujithb5aa9bf2008-10-29 10:13:31 +05301653 ath_tid_drain(sc, txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001654 }
1655 }
1656}
1657
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001658static int ath_tx_setup_buffer(struct ath_softc *sc, struct ath_buf *bf,
Sujith8f93b8b2008-11-18 09:10:42 +05301659 struct sk_buff *skb,
Sujith528f0c62008-10-29 10:14:26 +05301660 struct ath_tx_control *txctl)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001661{
Sujith528f0c62008-10-29 10:14:26 +05301662 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1663 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001664 struct ath_tx_info_priv *tx_info_priv;
Sujith528f0c62008-10-29 10:14:26 +05301665 int hdrlen;
1666 __le16 fc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001667
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001668 tx_info_priv = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
1669 if (unlikely(!tx_info_priv))
1670 return -ENOMEM;
Sujitha8efee42008-11-18 09:07:30 +05301671 tx_info->rate_driver_data[0] = tx_info_priv;
Sujith528f0c62008-10-29 10:14:26 +05301672 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1673 fc = hdr->frame_control;
Jouni Malinene022edb2008-08-22 17:31:33 +03001674
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001675 ATH_TXBUF_RESET(bf);
Sujith528f0c62008-10-29 10:14:26 +05301676
1677 /* Frame type */
1678
1679 bf->bf_frmlen = skb->len + FCS_LEN - (hdrlen & 3);
Sujithcd3d39a2008-08-11 14:03:34 +05301680
1681 ieee80211_is_data(fc) ?
1682 (bf->bf_state.bf_type |= BUF_DATA) :
1683 (bf->bf_state.bf_type &= ~BUF_DATA);
1684 ieee80211_is_back_req(fc) ?
1685 (bf->bf_state.bf_type |= BUF_BAR) :
1686 (bf->bf_state.bf_type &= ~BUF_BAR);
1687 ieee80211_is_pspoll(fc) ?
1688 (bf->bf_state.bf_type |= BUF_PSPOLL) :
1689 (bf->bf_state.bf_type &= ~BUF_PSPOLL);
Sujith672840a2008-08-11 14:05:08 +05301690 (sc->sc_flags & SC_OP_PREAMBLE_SHORT) ?
Sujithcd3d39a2008-08-11 14:03:34 +05301691 (bf->bf_state.bf_type |= BUF_SHORT_PREAMBLE) :
1692 (bf->bf_state.bf_type &= ~BUF_SHORT_PREAMBLE);
Luis R. Rodriguezecf70442008-12-23 15:58:43 -08001693 (conf_is_ht(&sc->hw->conf) && !is_pae(skb) &&
Sujith528f0c62008-10-29 10:14:26 +05301694 (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) ?
1695 (bf->bf_state.bf_type |= BUF_HT) :
1696 (bf->bf_state.bf_type &= ~BUF_HT);
Sujithcd3d39a2008-08-11 14:03:34 +05301697
Sujith528f0c62008-10-29 10:14:26 +05301698 bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
1699
1700 /* Crypto */
1701
1702 bf->bf_keytype = get_hw_crypto_keytype(skb);
1703
1704 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR) {
1705 bf->bf_frmlen += tx_info->control.hw_key->icv_len;
1706 bf->bf_keyix = tx_info->control.hw_key->hw_key_idx;
1707 } else {
1708 bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
1709 }
1710
Sujith528f0c62008-10-29 10:14:26 +05301711 /* Assign seqno, tidno */
1712
Senthil Balasubramaniand3a1db12008-12-22 16:31:58 +05301713 if (ieee80211_is_data_qos(fc) && (sc->sc_flags & SC_OP_TXAGGR))
Sujith528f0c62008-10-29 10:14:26 +05301714 assign_aggr_tid_seqno(skb, bf);
1715
1716 /* DMA setup */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001717 bf->bf_mpdu = skb;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001718
Gabor Juhosf5870ac2009-01-14 20:17:02 +01001719 bf->bf_dmacontext = pci_map_single(to_pci_dev(sc->dev), skb->data,
Sujith528f0c62008-10-29 10:14:26 +05301720 skb->len, PCI_DMA_TODEVICE);
Gabor Juhosf5870ac2009-01-14 20:17:02 +01001721 if (unlikely(pci_dma_mapping_error(to_pci_dev(sc->dev),
1722 bf->bf_dmacontext))) {
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001723 bf->bf_mpdu = NULL;
1724 DPRINTF(sc, ATH_DBG_CONFIG,
1725 "pci_dma_mapping_error() on TX\n");
1726 return -ENOMEM;
1727 }
1728
Sujith528f0c62008-10-29 10:14:26 +05301729 bf->bf_buf_addr = bf->bf_dmacontext;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001730 return 0;
Sujith528f0c62008-10-29 10:14:26 +05301731}
1732
1733/* FIXME: tx power */
1734static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
Sujith528f0c62008-10-29 10:14:26 +05301735 struct ath_tx_control *txctl)
1736{
1737 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
1738 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1739 struct ath_node *an = NULL;
1740 struct list_head bf_head;
1741 struct ath_desc *ds;
1742 struct ath_atx_tid *tid;
1743 struct ath_hal *ah = sc->sc_ah;
1744 int frm_type;
1745
Sujith528f0c62008-10-29 10:14:26 +05301746 frm_type = get_hw_packet_type(skb);
1747
1748 INIT_LIST_HEAD(&bf_head);
1749 list_add_tail(&bf->list, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001750
1751 /* setup descriptor */
Sujith528f0c62008-10-29 10:14:26 +05301752
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001753 ds = bf->bf_desc;
1754 ds->ds_link = 0;
1755 ds->ds_data = bf->bf_buf_addr;
1756
Sujith528f0c62008-10-29 10:14:26 +05301757 /* Formulate first tx descriptor with tx controls */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001758
Sujith528f0c62008-10-29 10:14:26 +05301759 ath9k_hw_set11n_txdesc(ah, ds, bf->bf_frmlen, frm_type, MAX_RATE_POWER,
1760 bf->bf_keyix, bf->bf_keytype, bf->bf_flags);
1761
1762 ath9k_hw_filltxdesc(ah, ds,
Sujith8f93b8b2008-11-18 09:10:42 +05301763 skb->len, /* segment length */
1764 true, /* first segment */
1765 true, /* last segment */
1766 ds); /* first descriptor */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001767
1768 bf->bf_lastfrm = bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001769
Sujith528f0c62008-10-29 10:14:26 +05301770 spin_lock_bh(&txctl->txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001771
John W. Linvillef1617962008-10-31 16:45:15 -04001772 if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR) &&
1773 tx_info->control.sta) {
1774 an = (struct ath_node *)tx_info->control.sta->drv_priv;
1775 tid = ATH_AN_2_TID(an, bf->bf_tidno);
1776
Sujith528f0c62008-10-29 10:14:26 +05301777 if (ath_aggr_query(sc, an, bf->bf_tidno)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001778 /*
1779 * Try aggregation if it's a unicast data frame
1780 * and the destination is HT capable.
1781 */
Sujith528f0c62008-10-29 10:14:26 +05301782 ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001783 } else {
1784 /*
Sujith528f0c62008-10-29 10:14:26 +05301785 * Send this frame as regular when ADDBA
1786 * exchange is neither complete nor pending.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001787 */
Sujith528f0c62008-10-29 10:14:26 +05301788 ath_tx_send_normal(sc, txctl->txq,
1789 tid, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001790 }
1791 } else {
1792 bf->bf_lastbf = bf;
1793 bf->bf_nframes = 1;
Sujith528f0c62008-10-29 10:14:26 +05301794
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001795 ath_buf_set_rate(sc, bf);
Sujith528f0c62008-10-29 10:14:26 +05301796 ath_tx_txqaddbuf(sc, txctl->txq, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001797 }
Sujith528f0c62008-10-29 10:14:26 +05301798
1799 spin_unlock_bh(&txctl->txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001800}
1801
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001802/* Upon failure caller should free skb */
Sujith528f0c62008-10-29 10:14:26 +05301803int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb,
1804 struct ath_tx_control *txctl)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001805{
Sujith528f0c62008-10-29 10:14:26 +05301806 struct ath_buf *bf;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001807 int r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001808
Sujith528f0c62008-10-29 10:14:26 +05301809 /* Check if a tx buffer is available */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001810
Sujith528f0c62008-10-29 10:14:26 +05301811 bf = ath_tx_get_buffer(sc);
1812 if (!bf) {
Sujith04bd4632008-11-28 22:18:05 +05301813 DPRINTF(sc, ATH_DBG_XMIT, "TX buffers are full\n");
Sujith528f0c62008-10-29 10:14:26 +05301814 return -1;
1815 }
1816
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001817 r = ath_tx_setup_buffer(sc, bf, skb, txctl);
1818 if (unlikely(r)) {
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001819 struct ath_txq *txq = txctl->txq;
1820
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001821 DPRINTF(sc, ATH_DBG_FATAL, "TX mem alloc failure\n");
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001822
1823 /* upon ath_tx_processq() this TX queue will be resumed, we
1824 * guarantee this will happen by knowing beforehand that
1825 * we will at least have to run TX completionon one buffer
1826 * on the queue */
1827 spin_lock_bh(&txq->axq_lock);
1828 if (ath_txq_depth(sc, txq->axq_qnum) > 1) {
1829 ieee80211_stop_queue(sc->hw,
1830 skb_get_queue_mapping(skb));
1831 txq->stopped = 1;
1832 }
1833 spin_unlock_bh(&txq->axq_lock);
1834
Sujithb77f4832008-12-07 21:44:03 +05301835 spin_lock_bh(&sc->tx.txbuflock);
1836 list_add_tail(&bf->list, &sc->tx.txbuf);
1837 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001838
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001839 return r;
1840 }
1841
Sujith8f93b8b2008-11-18 09:10:42 +05301842 ath_tx_start_dma(sc, bf, txctl);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001843
Sujith528f0c62008-10-29 10:14:26 +05301844 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001845}
1846
1847/* Initialize TX queue and h/w */
1848
1849int ath_tx_init(struct ath_softc *sc, int nbufs)
1850{
1851 int error = 0;
1852
1853 do {
Sujithb77f4832008-12-07 21:44:03 +05301854 spin_lock_init(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001855
1856 /* Setup tx descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301857 error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
Sujith556bb8f2008-08-11 14:03:53 +05301858 "tx", nbufs, 1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001859 if (error != 0) {
1860 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301861 "Failed to allocate tx descriptors: %d\n",
1862 error);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001863 break;
1864 }
1865
1866 /* XXX allocate beacon state together with vap */
Sujithb77f4832008-12-07 21:44:03 +05301867 error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001868 "beacon", ATH_BCBUF, 1);
1869 if (error != 0) {
1870 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301871 "Failed to allocate beacon descriptors: %d\n",
1872 error);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001873 break;
1874 }
1875
1876 } while (0);
1877
1878 if (error != 0)
1879 ath_tx_cleanup(sc);
1880
1881 return error;
1882}
1883
1884/* Reclaim all tx queue resources */
1885
1886int ath_tx_cleanup(struct ath_softc *sc)
1887{
1888 /* cleanup beacon descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301889 if (sc->beacon.bdma.dd_desc_len != 0)
1890 ath_descdma_cleanup(sc, &sc->beacon.bdma, &sc->beacon.bbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001891
1892 /* cleanup tx descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301893 if (sc->tx.txdma.dd_desc_len != 0)
1894 ath_descdma_cleanup(sc, &sc->tx.txdma, &sc->tx.txbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001895
1896 return 0;
1897}
1898
1899/* Setup a h/w transmit queue */
1900
1901struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
1902{
1903 struct ath_hal *ah = sc->sc_ah;
Sujithea9880f2008-08-07 10:53:10 +05301904 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001905 int qnum;
1906
Luis R. Rodriguez0345f372008-10-03 15:45:25 -07001907 memset(&qi, 0, sizeof(qi));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001908 qi.tqi_subtype = subtype;
1909 qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
1910 qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
1911 qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
Sujithea9880f2008-08-07 10:53:10 +05301912 qi.tqi_physCompBuf = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001913
1914 /*
1915 * Enable interrupts only for EOL and DESC conditions.
1916 * We mark tx descriptors to receive a DESC interrupt
1917 * when a tx queue gets deep; otherwise waiting for the
1918 * EOL to reap descriptors. Note that this is done to
1919 * reduce interrupt load and this only defers reaping
1920 * descriptors, never transmitting frames. Aside from
1921 * reducing interrupts this also permits more concurrency.
1922 * The only potential downside is if the tx queue backs
1923 * up in which case the top half of the kernel may backup
1924 * due to a lack of tx descriptors.
1925 *
1926 * The UAPSD queue is an exception, since we take a desc-
1927 * based intr on the EOSP frames.
1928 */
1929 if (qtype == ATH9K_TX_QUEUE_UAPSD)
1930 qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1931 else
1932 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
1933 TXQ_FLAG_TXDESCINT_ENABLE;
1934 qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
1935 if (qnum == -1) {
1936 /*
1937 * NB: don't print a message, this happens
1938 * normally on parts with too few tx queues
1939 */
1940 return NULL;
1941 }
Sujithb77f4832008-12-07 21:44:03 +05301942 if (qnum >= ARRAY_SIZE(sc->tx.txq)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001943 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301944 "qnum %u out of range, max %u!\n",
Sujithb77f4832008-12-07 21:44:03 +05301945 qnum, (unsigned int)ARRAY_SIZE(sc->tx.txq));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001946 ath9k_hw_releasetxqueue(ah, qnum);
1947 return NULL;
1948 }
1949 if (!ATH_TXQ_SETUP(sc, qnum)) {
Sujithb77f4832008-12-07 21:44:03 +05301950 struct ath_txq *txq = &sc->tx.txq[qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001951
1952 txq->axq_qnum = qnum;
1953 txq->axq_link = NULL;
1954 INIT_LIST_HEAD(&txq->axq_q);
1955 INIT_LIST_HEAD(&txq->axq_acq);
1956 spin_lock_init(&txq->axq_lock);
1957 txq->axq_depth = 0;
1958 txq->axq_aggr_depth = 0;
1959 txq->axq_totalqueued = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001960 txq->axq_linkbuf = NULL;
Sujithb77f4832008-12-07 21:44:03 +05301961 sc->tx.txqsetup |= 1<<qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001962 }
Sujithb77f4832008-12-07 21:44:03 +05301963 return &sc->tx.txq[qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001964}
1965
1966/* Reclaim resources for a setup queue */
1967
1968void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1969{
1970 ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
Sujithb77f4832008-12-07 21:44:03 +05301971 sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001972}
1973
1974/*
1975 * Setup a hardware data transmit queue for the specified
1976 * access control. The hal may not support all requested
1977 * queues in which case it will return a reference to a
1978 * previously setup queue. We record the mapping from ac's
1979 * to h/w queues for use by ath_tx_start and also track
1980 * the set of h/w queues being used to optimize work in the
1981 * transmit interrupt handler and related routines.
1982 */
1983
1984int ath_tx_setup(struct ath_softc *sc, int haltype)
1985{
1986 struct ath_txq *txq;
1987
Sujithb77f4832008-12-07 21:44:03 +05301988 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001989 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301990 "HAL AC %u out of range, max %zu!\n",
Sujithb77f4832008-12-07 21:44:03 +05301991 haltype, ARRAY_SIZE(sc->tx.hwq_map));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001992 return 0;
1993 }
1994 txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
1995 if (txq != NULL) {
Sujithb77f4832008-12-07 21:44:03 +05301996 sc->tx.hwq_map[haltype] = txq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001997 return 1;
1998 } else
1999 return 0;
2000}
2001
2002int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
2003{
2004 int qnum;
2005
2006 switch (qtype) {
2007 case ATH9K_TX_QUEUE_DATA:
Sujithb77f4832008-12-07 21:44:03 +05302008 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002009 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05302010 "HAL AC %u out of range, max %zu!\n",
Sujithb77f4832008-12-07 21:44:03 +05302011 haltype, ARRAY_SIZE(sc->tx.hwq_map));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002012 return -1;
2013 }
Sujithb77f4832008-12-07 21:44:03 +05302014 qnum = sc->tx.hwq_map[haltype];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002015 break;
2016 case ATH9K_TX_QUEUE_BEACON:
Sujithb77f4832008-12-07 21:44:03 +05302017 qnum = sc->beacon.beaconq;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002018 break;
2019 case ATH9K_TX_QUEUE_CAB:
Sujithb77f4832008-12-07 21:44:03 +05302020 qnum = sc->beacon.cabq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002021 break;
2022 default:
2023 qnum = -1;
2024 }
2025 return qnum;
2026}
2027
Sujith528f0c62008-10-29 10:14:26 +05302028/* Get a transmit queue, if available */
2029
2030struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
2031{
2032 struct ath_txq *txq = NULL;
2033 int qnum;
2034
2035 qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
Sujithb77f4832008-12-07 21:44:03 +05302036 txq = &sc->tx.txq[qnum];
Sujith528f0c62008-10-29 10:14:26 +05302037
2038 spin_lock_bh(&txq->axq_lock);
2039
2040 /* Try to avoid running out of descriptors */
2041 if (txq->axq_depth >= (ATH_TXBUF - 20)) {
2042 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05302043 "TX queue: %d is full, depth: %d\n",
2044 qnum, txq->axq_depth);
Sujith528f0c62008-10-29 10:14:26 +05302045 ieee80211_stop_queue(sc->hw, skb_get_queue_mapping(skb));
2046 txq->stopped = 1;
2047 spin_unlock_bh(&txq->axq_lock);
2048 return NULL;
2049 }
2050
2051 spin_unlock_bh(&txq->axq_lock);
2052
2053 return txq;
2054}
2055
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002056/* Update parameters for a transmit queue */
2057
Sujithea9880f2008-08-07 10:53:10 +05302058int ath_txq_update(struct ath_softc *sc, int qnum,
2059 struct ath9k_tx_queue_info *qinfo)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002060{
2061 struct ath_hal *ah = sc->sc_ah;
2062 int error = 0;
Sujithea9880f2008-08-07 10:53:10 +05302063 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002064
Sujithb77f4832008-12-07 21:44:03 +05302065 if (qnum == sc->beacon.beaconq) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002066 /*
2067 * XXX: for beacon queue, we just save the parameter.
2068 * It will be picked up by ath_beaconq_config when
2069 * it's necessary.
2070 */
Sujithb77f4832008-12-07 21:44:03 +05302071 sc->beacon.beacon_qi = *qinfo;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002072 return 0;
2073 }
2074
Sujithb77f4832008-12-07 21:44:03 +05302075 ASSERT(sc->tx.txq[qnum].axq_qnum == qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002076
Sujithea9880f2008-08-07 10:53:10 +05302077 ath9k_hw_get_txq_props(ah, qnum, &qi);
2078 qi.tqi_aifs = qinfo->tqi_aifs;
2079 qi.tqi_cwmin = qinfo->tqi_cwmin;
2080 qi.tqi_cwmax = qinfo->tqi_cwmax;
2081 qi.tqi_burstTime = qinfo->tqi_burstTime;
2082 qi.tqi_readyTime = qinfo->tqi_readyTime;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002083
Sujithea9880f2008-08-07 10:53:10 +05302084 if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002085 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05302086 "Unable to update hardware queue %u!\n", qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002087 error = -EIO;
2088 } else {
2089 ath9k_hw_resettxqueue(ah, qnum); /* push to h/w */
2090 }
2091
2092 return error;
2093}
2094
2095int ath_cabq_update(struct ath_softc *sc)
2096{
Sujithea9880f2008-08-07 10:53:10 +05302097 struct ath9k_tx_queue_info qi;
Sujithb77f4832008-12-07 21:44:03 +05302098 int qnum = sc->beacon.cabq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002099 struct ath_beacon_config conf;
2100
Sujithea9880f2008-08-07 10:53:10 +05302101 ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002102 /*
2103 * Ensure the readytime % is within the bounds.
2104 */
2105 if (sc->sc_config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
2106 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
2107 else if (sc->sc_config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
2108 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
2109
2110 ath_get_beaconconfig(sc, ATH_IF_ID_ANY, &conf);
2111 qi.tqi_readyTime =
2112 (conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
2113 ath_txq_update(sc, qnum, &qi);
2114
2115 return 0;
2116}
2117
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002118/* Deferred processing of transmit interrupt */
2119
2120void ath_tx_tasklet(struct ath_softc *sc)
2121{
Sujith1fe11322008-08-26 08:11:06 +05302122 int i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002123 u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
2124
2125 ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
2126
2127 /*
2128 * Process each active queue.
2129 */
2130 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2131 if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
Sujithb77f4832008-12-07 21:44:03 +05302132 ath_tx_processq(sc, &sc->tx.txq[i]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002133 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002134}
2135
2136void ath_tx_draintxq(struct ath_softc *sc,
2137 struct ath_txq *txq, bool retry_tx)
2138{
2139 struct ath_buf *bf, *lastbf;
2140 struct list_head bf_head;
2141
2142 INIT_LIST_HEAD(&bf_head);
2143
2144 /*
2145 * NB: this assumes output has been stopped and
2146 * we do not need to block ath_tx_tasklet
2147 */
2148 for (;;) {
2149 spin_lock_bh(&txq->axq_lock);
2150
2151 if (list_empty(&txq->axq_q)) {
2152 txq->axq_link = NULL;
2153 txq->axq_linkbuf = NULL;
2154 spin_unlock_bh(&txq->axq_lock);
2155 break;
2156 }
2157
2158 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2159
2160 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
2161 list_del(&bf->list);
2162 spin_unlock_bh(&txq->axq_lock);
2163
Sujithb77f4832008-12-07 21:44:03 +05302164 spin_lock_bh(&sc->tx.txbuflock);
2165 list_add_tail(&bf->list, &sc->tx.txbuf);
2166 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002167 continue;
2168 }
2169
2170 lastbf = bf->bf_lastbf;
2171 if (!retry_tx)
2172 lastbf->bf_desc->ds_txstat.ts_flags =
2173 ATH9K_TX_SW_ABORTED;
2174
2175 /* remove ath_buf's of the same mpdu from txq */
2176 list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
2177 txq->axq_depth--;
2178
2179 spin_unlock_bh(&txq->axq_lock);
2180
Sujithcd3d39a2008-08-11 14:03:34 +05302181 if (bf_isampdu(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002182 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, 0);
2183 else
2184 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2185 }
2186
2187 /* flush any pending frames if aggregation is enabled */
Sujith672840a2008-08-11 14:05:08 +05302188 if (sc->sc_flags & SC_OP_TXAGGR) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002189 if (!retry_tx) {
2190 spin_lock_bh(&txq->axq_lock);
Sujithb5aa9bf2008-10-29 10:13:31 +05302191 ath_txq_drain_pending_buffers(sc, txq);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002192 spin_unlock_bh(&txq->axq_lock);
2193 }
2194 }
2195}
2196
2197/* Drain the transmit queues and reclaim resources */
2198
2199void ath_draintxq(struct ath_softc *sc, bool retry_tx)
2200{
2201 /* stop beacon queue. The beacon will be freed when
2202 * we go to INIT state */
Sujith672840a2008-08-11 14:05:08 +05302203 if (!(sc->sc_flags & SC_OP_INVALID)) {
Sujithb77f4832008-12-07 21:44:03 +05302204 (void) ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
Sujith04bd4632008-11-28 22:18:05 +05302205 DPRINTF(sc, ATH_DBG_XMIT, "beacon queue %x\n",
Sujithb77f4832008-12-07 21:44:03 +05302206 ath9k_hw_gettxbuf(sc->sc_ah, sc->beacon.beaconq));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002207 }
2208
2209 ath_drain_txdataq(sc, retry_tx);
2210}
2211
2212u32 ath_txq_depth(struct ath_softc *sc, int qnum)
2213{
Sujithb77f4832008-12-07 21:44:03 +05302214 return sc->tx.txq[qnum].axq_depth;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002215}
2216
2217u32 ath_txq_aggr_depth(struct ath_softc *sc, int qnum)
2218{
Sujithb77f4832008-12-07 21:44:03 +05302219 return sc->tx.txq[qnum].axq_aggr_depth;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002220}
2221
Sujithccc75c52008-10-29 10:18:14 +05302222bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an, u8 tidno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002223{
2224 struct ath_atx_tid *txtid;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002225
Sujith672840a2008-08-11 14:05:08 +05302226 if (!(sc->sc_flags & SC_OP_TXAGGR))
Sujithccc75c52008-10-29 10:18:14 +05302227 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002228
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002229 txtid = ATH_AN_2_TID(an, tidno);
2230
Sujitha37c2c72008-10-29 10:15:40 +05302231 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
2232 if (!(txtid->state & AGGR_ADDBA_PROGRESS) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002233 (txtid->addba_exchangeattempts < ADDBA_EXCHANGE_ATTEMPTS)) {
2234 txtid->addba_exchangeattempts++;
Sujithccc75c52008-10-29 10:18:14 +05302235 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002236 }
2237 }
2238
Sujithccc75c52008-10-29 10:18:14 +05302239 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002240}
2241
2242/* Start TX aggregation */
2243
Sujithb5aa9bf2008-10-29 10:13:31 +05302244int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
2245 u16 tid, u16 *ssn)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002246{
2247 struct ath_atx_tid *txtid;
2248 struct ath_node *an;
2249
Sujithb5aa9bf2008-10-29 10:13:31 +05302250 an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002251
Sujith672840a2008-08-11 14:05:08 +05302252 if (sc->sc_flags & SC_OP_TXAGGR) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002253 txtid = ATH_AN_2_TID(an, tid);
Sujitha37c2c72008-10-29 10:15:40 +05302254 txtid->state |= AGGR_ADDBA_PROGRESS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002255 ath_tx_pause_tid(sc, txtid);
2256 }
2257
2258 return 0;
2259}
2260
2261/* Stop tx aggregation */
2262
Sujithb5aa9bf2008-10-29 10:13:31 +05302263int ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002264{
Sujithb5aa9bf2008-10-29 10:13:31 +05302265 struct ath_node *an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002266
2267 ath_tx_aggr_teardown(sc, an, tid);
2268 return 0;
2269}
2270
Sujith8469cde2008-10-29 10:19:28 +05302271/* Resume tx aggregation */
2272
2273void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
2274{
2275 struct ath_atx_tid *txtid;
2276 struct ath_node *an;
2277
2278 an = (struct ath_node *)sta->drv_priv;
2279
2280 if (sc->sc_flags & SC_OP_TXAGGR) {
2281 txtid = ATH_AN_2_TID(an, tid);
2282 txtid->baw_size =
2283 IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
2284 txtid->state |= AGGR_ADDBA_COMPLETE;
2285 txtid->state &= ~AGGR_ADDBA_PROGRESS;
2286 ath_tx_resume_tid(sc, txtid);
2287 }
2288}
2289
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002290/*
2291 * Performs transmit side cleanup when TID changes from aggregated to
2292 * unaggregated.
2293 * - Pause the TID and mark cleanup in progress
2294 * - Discard all retry frames from the s/w queue.
2295 */
2296
Sujithb5aa9bf2008-10-29 10:13:31 +05302297void ath_tx_aggr_teardown(struct ath_softc *sc, struct ath_node *an, u8 tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002298{
2299 struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
Sujithb77f4832008-12-07 21:44:03 +05302300 struct ath_txq *txq = &sc->tx.txq[txtid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002301 struct ath_buf *bf;
2302 struct list_head bf_head;
2303 INIT_LIST_HEAD(&bf_head);
2304
Sujitha37c2c72008-10-29 10:15:40 +05302305 if (txtid->state & AGGR_CLEANUP) /* cleanup is in progress */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002306 return;
2307
Sujitha37c2c72008-10-29 10:15:40 +05302308 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002309 txtid->addba_exchangeattempts = 0;
2310 return;
2311 }
2312
2313 /* TID must be paused first */
2314 ath_tx_pause_tid(sc, txtid);
2315
2316 /* drop all software retried frames and mark this TID */
2317 spin_lock_bh(&txq->axq_lock);
2318 while (!list_empty(&txtid->buf_q)) {
2319 bf = list_first_entry(&txtid->buf_q, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +05302320 if (!bf_isretried(bf)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002321 /*
2322 * NB: it's based on the assumption that
2323 * software retried frame will always stay
2324 * at the head of software queue.
2325 */
2326 break;
2327 }
2328 list_cut_position(&bf_head,
2329 &txtid->buf_q, &bf->bf_lastfrm->list);
2330 ath_tx_update_baw(sc, txtid, bf->bf_seqno);
2331
2332 /* complete this sub-frame */
2333 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2334 }
2335
2336 if (txtid->baw_head != txtid->baw_tail) {
2337 spin_unlock_bh(&txq->axq_lock);
Sujitha37c2c72008-10-29 10:15:40 +05302338 txtid->state |= AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002339 } else {
Sujitha37c2c72008-10-29 10:15:40 +05302340 txtid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002341 txtid->addba_exchangeattempts = 0;
2342 spin_unlock_bh(&txq->axq_lock);
2343 ath_tx_flush_tid(sc, txtid);
2344 }
2345}
2346
2347/*
2348 * Tx scheduling logic
2349 * NB: must be called with txq lock held
2350 */
2351
2352void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
2353{
2354 struct ath_atx_ac *ac;
2355 struct ath_atx_tid *tid;
2356
2357 /* nothing to schedule */
2358 if (list_empty(&txq->axq_acq))
2359 return;
2360 /*
2361 * get the first node/ac pair on the queue
2362 */
2363 ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
2364 list_del(&ac->list);
2365 ac->sched = false;
2366
2367 /*
2368 * process a single tid per destination
2369 */
2370 do {
2371 /* nothing to schedule */
2372 if (list_empty(&ac->tid_q))
2373 return;
2374
2375 tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
2376 list_del(&tid->list);
2377 tid->sched = false;
2378
2379 if (tid->paused) /* check next tid to keep h/w busy */
2380 continue;
2381
Sujith43453b32008-10-29 10:14:52 +05302382 if ((txq->axq_depth % 2) == 0)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002383 ath_tx_sched_aggr(sc, txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002384
2385 /*
2386 * add tid to round-robin queue if more frames
2387 * are pending for the tid
2388 */
2389 if (!list_empty(&tid->buf_q))
2390 ath_tx_queue_tid(txq, tid);
2391
2392 /* only schedule one TID at a time */
2393 break;
2394 } while (!list_empty(&ac->tid_q));
2395
2396 /*
2397 * schedule AC if more TIDs need processing
2398 */
2399 if (!list_empty(&ac->tid_q)) {
2400 /*
2401 * add dest ac to txq if not already added
2402 */
2403 if (!ac->sched) {
2404 ac->sched = true;
2405 list_add_tail(&ac->list, &txq->axq_acq);
2406 }
2407 }
2408}
2409
2410/* Initialize per-node transmit state */
2411
2412void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2413{
Sujithc5170162008-10-29 10:13:59 +05302414 struct ath_atx_tid *tid;
2415 struct ath_atx_ac *ac;
2416 int tidno, acno;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002417
Sujithc5170162008-10-29 10:13:59 +05302418 /*
2419 * Init per tid tx state
2420 */
Sujith8ee5afb2008-12-07 21:43:36 +05302421 for (tidno = 0, tid = &an->tid[tidno];
Sujithc5170162008-10-29 10:13:59 +05302422 tidno < WME_NUM_TID;
2423 tidno++, tid++) {
2424 tid->an = an;
2425 tid->tidno = tidno;
2426 tid->seq_start = tid->seq_next = 0;
2427 tid->baw_size = WME_MAX_BA;
2428 tid->baw_head = tid->baw_tail = 0;
2429 tid->sched = false;
2430 tid->paused = false;
Sujitha37c2c72008-10-29 10:15:40 +05302431 tid->state &= ~AGGR_CLEANUP;
Sujithc5170162008-10-29 10:13:59 +05302432 INIT_LIST_HEAD(&tid->buf_q);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002433
Sujithc5170162008-10-29 10:13:59 +05302434 acno = TID_TO_WME_AC(tidno);
Sujith8ee5afb2008-12-07 21:43:36 +05302435 tid->ac = &an->ac[acno];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002436
Sujithc5170162008-10-29 10:13:59 +05302437 /* ADDBA state */
Sujitha37c2c72008-10-29 10:15:40 +05302438 tid->state &= ~AGGR_ADDBA_COMPLETE;
2439 tid->state &= ~AGGR_ADDBA_PROGRESS;
2440 tid->addba_exchangeattempts = 0;
Sujithc5170162008-10-29 10:13:59 +05302441 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002442
Sujithc5170162008-10-29 10:13:59 +05302443 /*
2444 * Init per ac tx state
2445 */
Sujith8ee5afb2008-12-07 21:43:36 +05302446 for (acno = 0, ac = &an->ac[acno];
Sujithc5170162008-10-29 10:13:59 +05302447 acno < WME_NUM_AC; acno++, ac++) {
2448 ac->sched = false;
2449 INIT_LIST_HEAD(&ac->tid_q);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002450
Sujithc5170162008-10-29 10:13:59 +05302451 switch (acno) {
2452 case WME_AC_BE:
2453 ac->qnum = ath_tx_get_qnum(sc,
2454 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
2455 break;
2456 case WME_AC_BK:
2457 ac->qnum = ath_tx_get_qnum(sc,
2458 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
2459 break;
2460 case WME_AC_VI:
2461 ac->qnum = ath_tx_get_qnum(sc,
2462 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
2463 break;
2464 case WME_AC_VO:
2465 ac->qnum = ath_tx_get_qnum(sc,
2466 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
2467 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002468 }
2469 }
2470}
2471
2472/* Cleanupthe pending buffers for the node. */
2473
Sujithb5aa9bf2008-10-29 10:13:31 +05302474void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002475{
2476 int i;
2477 struct ath_atx_ac *ac, *ac_tmp;
2478 struct ath_atx_tid *tid, *tid_tmp;
2479 struct ath_txq *txq;
2480 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2481 if (ATH_TXQ_SETUP(sc, i)) {
Sujithb77f4832008-12-07 21:44:03 +05302482 txq = &sc->tx.txq[i];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002483
Sujithb5aa9bf2008-10-29 10:13:31 +05302484 spin_lock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002485
2486 list_for_each_entry_safe(ac,
2487 ac_tmp, &txq->axq_acq, list) {
2488 tid = list_first_entry(&ac->tid_q,
2489 struct ath_atx_tid, list);
2490 if (tid && tid->an != an)
2491 continue;
2492 list_del(&ac->list);
2493 ac->sched = false;
2494
2495 list_for_each_entry_safe(tid,
2496 tid_tmp, &ac->tid_q, list) {
2497 list_del(&tid->list);
2498 tid->sched = false;
Sujithb5aa9bf2008-10-29 10:13:31 +05302499 ath_tid_drain(sc, txq, tid);
Sujitha37c2c72008-10-29 10:15:40 +05302500 tid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002501 tid->addba_exchangeattempts = 0;
Sujitha37c2c72008-10-29 10:15:40 +05302502 tid->state &= ~AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002503 }
2504 }
2505
Sujithb5aa9bf2008-10-29 10:13:31 +05302506 spin_unlock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002507 }
2508 }
2509}
2510
Jouni Malinene022edb2008-08-22 17:31:33 +03002511void ath_tx_cabq(struct ath_softc *sc, struct sk_buff *skb)
2512{
2513 int hdrlen, padsize;
2514 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2515 struct ath_tx_control txctl;
2516
Sujith528f0c62008-10-29 10:14:26 +05302517 memset(&txctl, 0, sizeof(struct ath_tx_control));
2518
Jouni Malinene022edb2008-08-22 17:31:33 +03002519 /*
2520 * As a temporary workaround, assign seq# here; this will likely need
2521 * to be cleaned up to work better with Beacon transmission and virtual
2522 * BSSes.
2523 */
2524 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
2525 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2526 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
Sujithb77f4832008-12-07 21:44:03 +05302527 sc->tx.seq_no += 0x10;
Jouni Malinene022edb2008-08-22 17:31:33 +03002528 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Sujithb77f4832008-12-07 21:44:03 +05302529 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
Jouni Malinene022edb2008-08-22 17:31:33 +03002530 }
2531
2532 /* Add the padding after the header if this is not already done */
2533 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
2534 if (hdrlen & 3) {
2535 padsize = hdrlen % 4;
2536 if (skb_headroom(skb) < padsize) {
Sujith04bd4632008-11-28 22:18:05 +05302537 DPRINTF(sc, ATH_DBG_XMIT, "TX CABQ padding failed\n");
Jouni Malinene022edb2008-08-22 17:31:33 +03002538 dev_kfree_skb_any(skb);
2539 return;
2540 }
2541 skb_push(skb, padsize);
2542 memmove(skb->data, skb->data + padsize, hdrlen);
2543 }
2544
Sujithb77f4832008-12-07 21:44:03 +05302545 txctl.txq = sc->beacon.cabq;
Sujith528f0c62008-10-29 10:14:26 +05302546
Sujith04bd4632008-11-28 22:18:05 +05302547 DPRINTF(sc, ATH_DBG_XMIT, "transmitting CABQ packet, skb: %p\n", skb);
Jouni Malinene022edb2008-08-22 17:31:33 +03002548
Sujith528f0c62008-10-29 10:14:26 +05302549 if (ath_tx_start(sc, skb, &txctl) != 0) {
Sujith04bd4632008-11-28 22:18:05 +05302550 DPRINTF(sc, ATH_DBG_XMIT, "CABQ TX failed\n");
Sujith528f0c62008-10-29 10:14:26 +05302551 goto exit;
Jouni Malinene022edb2008-08-22 17:31:33 +03002552 }
Jouni Malinene022edb2008-08-22 17:31:33 +03002553
Sujith528f0c62008-10-29 10:14:26 +05302554 return;
2555exit:
2556 dev_kfree_skb_any(skb);
2557}