blob: d6d402c18934497a7e70644b05108de60e670bd5 [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
2 * Copyright (c) 2008 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070017#include "core.h"
18
19#define BITS_PER_BYTE 8
20#define OFDM_PLCP_BITS 22
21#define HT_RC_2_MCS(_rc) ((_rc) & 0x0f)
22#define HT_RC_2_STREAMS(_rc) ((((_rc) & 0x78) >> 3) + 1)
23#define L_STF 8
24#define L_LTF 8
25#define L_SIG 4
26#define HT_SIG 8
27#define HT_STF 4
28#define HT_LTF(_ns) (4 * (_ns))
29#define SYMBOL_TIME(_ns) ((_ns) << 2) /* ns * 4 us */
30#define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5) /* ns * 3.6 us */
31#define NUM_SYMBOLS_PER_USEC(_usec) (_usec >> 2)
32#define NUM_SYMBOLS_PER_USEC_HALFGI(_usec) (((_usec*5)-4)/18)
33
34#define OFDM_SIFS_TIME 16
35
36static u32 bits_per_symbol[][2] = {
37 /* 20MHz 40MHz */
38 { 26, 54 }, /* 0: BPSK */
39 { 52, 108 }, /* 1: QPSK 1/2 */
40 { 78, 162 }, /* 2: QPSK 3/4 */
41 { 104, 216 }, /* 3: 16-QAM 1/2 */
42 { 156, 324 }, /* 4: 16-QAM 3/4 */
43 { 208, 432 }, /* 5: 64-QAM 2/3 */
44 { 234, 486 }, /* 6: 64-QAM 3/4 */
45 { 260, 540 }, /* 7: 64-QAM 5/6 */
46 { 52, 108 }, /* 8: BPSK */
47 { 104, 216 }, /* 9: QPSK 1/2 */
48 { 156, 324 }, /* 10: QPSK 3/4 */
49 { 208, 432 }, /* 11: 16-QAM 1/2 */
50 { 312, 648 }, /* 12: 16-QAM 3/4 */
51 { 416, 864 }, /* 13: 64-QAM 2/3 */
52 { 468, 972 }, /* 14: 64-QAM 3/4 */
53 { 520, 1080 }, /* 15: 64-QAM 5/6 */
54};
55
56#define IS_HT_RATE(_rate) ((_rate) & 0x80)
57
58/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070059 * Insert a chain of ath_buf (descriptors) on a txq and
60 * assume the descriptors are already chained together by caller.
61 * NB: must be called with txq lock held
62 */
63
Sujith102e0572008-10-29 10:15:16 +053064static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
65 struct list_head *head)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070066{
67 struct ath_hal *ah = sc->sc_ah;
68 struct ath_buf *bf;
Sujith102e0572008-10-29 10:15:16 +053069
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070070 /*
71 * Insert the frame on the outbound list and
72 * pass it on to the hardware.
73 */
74
75 if (list_empty(head))
76 return;
77
78 bf = list_first_entry(head, struct ath_buf, list);
79
80 list_splice_tail_init(head, &txq->axq_q);
81 txq->axq_depth++;
82 txq->axq_totalqueued++;
83 txq->axq_linkbuf = list_entry(txq->axq_q.prev, struct ath_buf, list);
84
85 DPRINTF(sc, ATH_DBG_QUEUE,
Sujith04bd46382008-11-28 22:18:05 +053086 "qnum: %d, txq depth: %d\n", txq->axq_qnum, txq->axq_depth);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070087
88 if (txq->axq_link == NULL) {
89 ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
90 DPRINTF(sc, ATH_DBG_XMIT,
Sujith04bd46382008-11-28 22:18:05 +053091 "TXDP[%u] = %llx (%p)\n",
92 txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070093 } else {
94 *txq->axq_link = bf->bf_daddr;
Sujith04bd46382008-11-28 22:18:05 +053095 DPRINTF(sc, ATH_DBG_XMIT, "link[%u] (%p)=%llx (%p)\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070096 txq->axq_qnum, txq->axq_link,
97 ito64(bf->bf_daddr), bf->bf_desc);
98 }
99 txq->axq_link = &(bf->bf_lastbf->bf_desc->ds_link);
100 ath9k_hw_txstart(ah, txq->axq_qnum);
101}
102
Sujithc4288392008-11-18 09:09:30 +0530103static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
104 struct ath_xmit_status *tx_status)
105{
106 struct ieee80211_hw *hw = sc->hw;
107 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
108 struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
109
Sujith04bd46382008-11-28 22:18:05 +0530110 DPRINTF(sc, ATH_DBG_XMIT, "TX complete: skb: %p\n", skb);
Sujithc4288392008-11-18 09:09:30 +0530111
112 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
113 tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
114 kfree(tx_info_priv);
115 tx_info->rate_driver_data[0] = NULL;
116 }
117
118 if (tx_status->flags & ATH_TX_BAR) {
119 tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
120 tx_status->flags &= ~ATH_TX_BAR;
121 }
122
123 if (!(tx_status->flags & (ATH_TX_ERROR | ATH_TX_XRETRY))) {
124 /* Frame was ACKed */
125 tx_info->flags |= IEEE80211_TX_STAT_ACK;
126 }
127
Jouni Malinenda027ca2008-12-15 15:44:53 +0200128 tx_info->status.rates[0].count = tx_status->retries;
129 if (tx_info->status.rates[0].flags & IEEE80211_TX_RC_MCS) {
130 /* Change idx from internal table index to MCS index */
131 int idx = tx_info->status.rates[0].idx;
132 struct ath_rate_table *rate_table = sc->cur_rate_table;
133 if (idx >= 0 && idx < rate_table->rate_cnt)
134 tx_info->status.rates[0].idx =
135 rate_table->info[idx].ratecode & 0x7f;
136 }
Sujithc4288392008-11-18 09:09:30 +0530137
138 ieee80211_tx_status(hw, skb);
139}
140
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700141/* Check if it's okay to send out aggregates */
142
Sujitha37c2c72008-10-29 10:15:40 +0530143static int ath_aggr_query(struct ath_softc *sc, struct ath_node *an, u8 tidno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700144{
145 struct ath_atx_tid *tid;
146 tid = ATH_AN_2_TID(an, tidno);
147
Sujitha37c2c72008-10-29 10:15:40 +0530148 if (tid->state & AGGR_ADDBA_COMPLETE ||
149 tid->state & AGGR_ADDBA_PROGRESS)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700150 return 1;
151 else
152 return 0;
153}
154
Sujithff37e332008-11-24 12:07:55 +0530155static void ath_get_beaconconfig(struct ath_softc *sc, int if_id,
156 struct ath_beacon_config *conf)
157{
158 struct ieee80211_hw *hw = sc->hw;
159
160 /* fill in beacon config data */
161
162 conf->beacon_interval = hw->conf.beacon_int;
163 conf->listen_interval = 100;
164 conf->dtim_count = 1;
165 conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
166}
167
Sujith528f0c62008-10-29 10:14:26 +0530168/* Calculate Atheros packet type from IEEE80211 packet header */
169
170static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700171{
Sujith528f0c62008-10-29 10:14:26 +0530172 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700173 enum ath9k_pkt_type htype;
174 __le16 fc;
175
Sujith528f0c62008-10-29 10:14:26 +0530176 hdr = (struct ieee80211_hdr *)skb->data;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700177 fc = hdr->frame_control;
178
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700179 if (ieee80211_is_beacon(fc))
180 htype = ATH9K_PKT_TYPE_BEACON;
181 else if (ieee80211_is_probe_resp(fc))
182 htype = ATH9K_PKT_TYPE_PROBE_RESP;
183 else if (ieee80211_is_atim(fc))
184 htype = ATH9K_PKT_TYPE_ATIM;
185 else if (ieee80211_is_pspoll(fc))
186 htype = ATH9K_PKT_TYPE_PSPOLL;
187 else
188 htype = ATH9K_PKT_TYPE_NORMAL;
189
190 return htype;
191}
192
Sujitha8efee42008-11-18 09:07:30 +0530193static bool is_pae(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700194{
195 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700196 __le16 fc;
197
198 hdr = (struct ieee80211_hdr *)skb->data;
199 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +0200200
Sujitha8efee42008-11-18 09:07:30 +0530201 if (ieee80211_is_data(fc)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700202 if (ieee80211_is_nullfunc(fc) ||
Sujith528f0c62008-10-29 10:14:26 +0530203 /* Port Access Entity (IEEE 802.1X) */
204 (skb->protocol == cpu_to_be16(ETH_P_PAE))) {
Sujitha8efee42008-11-18 09:07:30 +0530205 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700206 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700207 }
208
Sujitha8efee42008-11-18 09:07:30 +0530209 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700210}
211
Sujith528f0c62008-10-29 10:14:26 +0530212static int get_hw_crypto_keytype(struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700213{
Sujith528f0c62008-10-29 10:14:26 +0530214 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
215
216 if (tx_info->control.hw_key) {
217 if (tx_info->control.hw_key->alg == ALG_WEP)
218 return ATH9K_KEY_TYPE_WEP;
219 else if (tx_info->control.hw_key->alg == ALG_TKIP)
220 return ATH9K_KEY_TYPE_TKIP;
221 else if (tx_info->control.hw_key->alg == ALG_CCMP)
222 return ATH9K_KEY_TYPE_AES;
223 }
224
225 return ATH9K_KEY_TYPE_CLEAR;
226}
227
Sujith528f0c62008-10-29 10:14:26 +0530228/* Called only when tx aggregation is enabled and HT is supported */
229
230static void assign_aggr_tid_seqno(struct sk_buff *skb,
231 struct ath_buf *bf)
232{
233 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
234 struct ieee80211_hdr *hdr;
235 struct ath_node *an;
236 struct ath_atx_tid *tid;
237 __le16 fc;
238 u8 *qc;
239
240 if (!tx_info->control.sta)
241 return;
242
243 an = (struct ath_node *)tx_info->control.sta->drv_priv;
244 hdr = (struct ieee80211_hdr *)skb->data;
245 fc = hdr->frame_control;
246
247 /* Get tidno */
248
249 if (ieee80211_is_data_qos(fc)) {
250 qc = ieee80211_get_qos_ctl(hdr);
251 bf->bf_tidno = qc[0] & 0xf;
Sujith98deeea2008-08-11 14:05:46 +0530252 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700253
Sujith528f0c62008-10-29 10:14:26 +0530254 /* Get seqno */
255
Sujitha8efee42008-11-18 09:07:30 +0530256 if (ieee80211_is_data(fc) && !is_pae(skb)) {
Sujith528f0c62008-10-29 10:14:26 +0530257 /* For HT capable stations, we save tidno for later use.
258 * We also override seqno set by upper layer with the one
259 * in tx aggregation state.
260 *
261 * If fragmentation is on, the sequence number is
262 * not overridden, since it has been
263 * incremented by the fragmentation routine.
264 *
265 * FIXME: check if the fragmentation threshold exceeds
266 * IEEE80211 max.
267 */
268 tid = ATH_AN_2_TID(an, bf->bf_tidno);
269 hdr->seq_ctrl = cpu_to_le16(tid->seq_next <<
270 IEEE80211_SEQ_SEQ_SHIFT);
271 bf->bf_seqno = tid->seq_next;
272 INCR(tid->seq_next, IEEE80211_SEQ_MAX);
273 }
274}
275
276static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
277 struct ath_txq *txq)
278{
279 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
280 int flags = 0;
281
282 flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
283 flags |= ATH9K_TXDESC_INTREQ;
284
285 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
286 flags |= ATH9K_TXDESC_NOACK;
287 if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
288 flags |= ATH9K_TXDESC_RTSENA;
289
290 return flags;
291}
292
293static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
294{
295 struct ath_buf *bf = NULL;
296
Sujithb77f4832008-12-07 21:44:03 +0530297 spin_lock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530298
Sujithb77f4832008-12-07 21:44:03 +0530299 if (unlikely(list_empty(&sc->tx.txbuf))) {
300 spin_unlock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530301 return NULL;
302 }
303
Sujithb77f4832008-12-07 21:44:03 +0530304 bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
Sujith528f0c62008-10-29 10:14:26 +0530305 list_del(&bf->list);
306
Sujithb77f4832008-12-07 21:44:03 +0530307 spin_unlock_bh(&sc->tx.txbuflock);
Sujith528f0c62008-10-29 10:14:26 +0530308
309 return bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700310}
311
312/* To complete a chain of buffers associated a frame */
313
314static void ath_tx_complete_buf(struct ath_softc *sc,
315 struct ath_buf *bf,
316 struct list_head *bf_q,
317 int txok, int sendbar)
318{
319 struct sk_buff *skb = bf->bf_mpdu;
320 struct ath_xmit_status tx_status;
Senthil Balasubramaniana07d3612008-12-09 17:23:33 +0530321 unsigned long flags;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700322
323 /*
324 * Set retry information.
325 * NB: Don't use the information in the descriptor, because the frame
326 * could be software retried.
327 */
328 tx_status.retries = bf->bf_retries;
329 tx_status.flags = 0;
330
331 if (sendbar)
332 tx_status.flags = ATH_TX_BAR;
333
334 if (!txok) {
335 tx_status.flags |= ATH_TX_ERROR;
336
Sujithcd3d39a2008-08-11 14:03:34 +0530337 if (bf_isxretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700338 tx_status.flags |= ATH_TX_XRETRY;
339 }
Sujith102e0572008-10-29 10:15:16 +0530340
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700341 /* Unmap this frame */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700342 pci_unmap_single(sc->pdev,
Sujithff9b6622008-08-14 13:27:16 +0530343 bf->bf_dmacontext,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700344 skb->len,
345 PCI_DMA_TODEVICE);
346 /* complete this frame */
Sujith528f0c62008-10-29 10:14:26 +0530347 ath_tx_complete(sc, skb, &tx_status);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700348
349 /*
350 * Return the list of ath_buf of this mpdu to free queue
351 */
Sujithb77f4832008-12-07 21:44:03 +0530352 spin_lock_irqsave(&sc->tx.txbuflock, flags);
353 list_splice_tail_init(bf_q, &sc->tx.txbuf);
354 spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700355}
356
357/*
358 * queue up a dest/ac pair for tx scheduling
359 * NB: must be called with txq lock held
360 */
361
362static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
363{
364 struct ath_atx_ac *ac = tid->ac;
365
366 /*
367 * if tid is paused, hold off
368 */
369 if (tid->paused)
370 return;
371
372 /*
373 * add tid to ac atmost once
374 */
375 if (tid->sched)
376 return;
377
378 tid->sched = true;
379 list_add_tail(&tid->list, &ac->tid_q);
380
381 /*
382 * add node ac to txq atmost once
383 */
384 if (ac->sched)
385 return;
386
387 ac->sched = true;
388 list_add_tail(&ac->list, &txq->axq_acq);
389}
390
391/* pause a tid */
392
393static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
394{
Sujithb77f4832008-12-07 21:44:03 +0530395 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700396
397 spin_lock_bh(&txq->axq_lock);
398
399 tid->paused++;
400
401 spin_unlock_bh(&txq->axq_lock);
402}
403
404/* resume a tid and schedule aggregate */
405
406void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
407{
Sujithb77f4832008-12-07 21:44:03 +0530408 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700409
410 ASSERT(tid->paused > 0);
411 spin_lock_bh(&txq->axq_lock);
412
413 tid->paused--;
414
415 if (tid->paused > 0)
416 goto unlock;
417
418 if (list_empty(&tid->buf_q))
419 goto unlock;
420
421 /*
422 * Add this TID to scheduler and try to send out aggregates
423 */
424 ath_tx_queue_tid(txq, tid);
425 ath_txq_schedule(sc, txq);
426unlock:
427 spin_unlock_bh(&txq->axq_lock);
428}
429
430/* Compute the number of bad frames */
431
Sujithb5aa9bf2008-10-29 10:13:31 +0530432static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
433 int txok)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700434{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700435 struct ath_buf *bf_last = bf->bf_lastbf;
436 struct ath_desc *ds = bf_last->bf_desc;
437 u16 seq_st = 0;
438 u32 ba[WME_BA_BMP_SIZE >> 5];
439 int ba_index;
440 int nbad = 0;
441 int isaggr = 0;
442
Sujithb5aa9bf2008-10-29 10:13:31 +0530443 if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700444 return 0;
445
Sujithcd3d39a2008-08-11 14:03:34 +0530446 isaggr = bf_isaggr(bf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700447 if (isaggr) {
448 seq_st = ATH_DS_BA_SEQ(ds);
449 memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
450 }
451
452 while (bf) {
453 ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
454 if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
455 nbad++;
456
457 bf = bf->bf_next;
458 }
459
460 return nbad;
461}
462
463static void ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
464{
465 struct sk_buff *skb;
466 struct ieee80211_hdr *hdr;
467
Sujithcd3d39a2008-08-11 14:03:34 +0530468 bf->bf_state.bf_type |= BUF_RETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700469 bf->bf_retries++;
470
471 skb = bf->bf_mpdu;
472 hdr = (struct ieee80211_hdr *)skb->data;
473 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
474}
475
476/* Update block ack window */
477
Sujith102e0572008-10-29 10:15:16 +0530478static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
479 int seqno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700480{
481 int index, cindex;
482
483 index = ATH_BA_INDEX(tid->seq_start, seqno);
484 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
485
486 tid->tx_buf[cindex] = NULL;
487
488 while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
489 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
490 INCR(tid->baw_head, ATH_TID_MAX_BUFS);
491 }
492}
493
494/*
495 * ath_pkt_dur - compute packet duration (NB: not NAV)
496 *
497 * rix - rate index
498 * pktlen - total bytes (delims + data + fcs + pads + pad delims)
499 * width - 0 for 20 MHz, 1 for 40 MHz
500 * half_gi - to use 4us v/s 3.6 us for symbol time
501 */
Sujith102e0572008-10-29 10:15:16 +0530502static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
503 int width, int half_gi, bool shortPreamble)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700504{
Sujith3706de62008-12-07 21:42:10 +0530505 struct ath_rate_table *rate_table = sc->cur_rate_table;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700506 u32 nbits, nsymbits, duration, nsymbols;
507 u8 rc;
508 int streams, pktlen;
509
Sujithcd3d39a2008-08-11 14:03:34 +0530510 pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
Sujithe63835b2008-11-18 09:07:53 +0530511 rc = rate_table->info[rix].ratecode;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700512
Sujithe63835b2008-11-18 09:07:53 +0530513 /* for legacy rates, use old function to compute packet duration */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700514 if (!IS_HT_RATE(rc))
Sujithe63835b2008-11-18 09:07:53 +0530515 return ath9k_hw_computetxtime(sc->sc_ah, rate_table, pktlen,
516 rix, shortPreamble);
517
518 /* find number of symbols: PLCP + data */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700519 nbits = (pktlen << 3) + OFDM_PLCP_BITS;
520 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
521 nsymbols = (nbits + nsymbits - 1) / nsymbits;
522
523 if (!half_gi)
524 duration = SYMBOL_TIME(nsymbols);
525 else
526 duration = SYMBOL_TIME_HALFGI(nsymbols);
527
Sujithe63835b2008-11-18 09:07:53 +0530528 /* addup duration for legacy/ht training and signal fields */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700529 streams = HT_RC_2_STREAMS(rc);
530 duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
Sujith102e0572008-10-29 10:15:16 +0530531
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700532 return duration;
533}
534
535/* Rate module function to set rate related fields in tx descriptor */
536
537static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
538{
539 struct ath_hal *ah = sc->sc_ah;
Sujithe63835b2008-11-18 09:07:53 +0530540 struct ath_rate_table *rt;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700541 struct ath_desc *ds = bf->bf_desc;
542 struct ath_desc *lastds = bf->bf_lastbf->bf_desc;
543 struct ath9k_11n_rate_series series[4];
Sujith528f0c62008-10-29 10:14:26 +0530544 struct sk_buff *skb;
545 struct ieee80211_tx_info *tx_info;
Sujitha8efee42008-11-18 09:07:30 +0530546 struct ieee80211_tx_rate *rates;
Sujithe63835b2008-11-18 09:07:53 +0530547 struct ieee80211_hdr *hdr;
548 int i, flags, rtsctsena = 0;
549 u32 ctsduration = 0;
550 u8 rix = 0, cix, ctsrate = 0;
551 __le16 fc;
552
553 memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
Sujith528f0c62008-10-29 10:14:26 +0530554
555 skb = (struct sk_buff *)bf->bf_mpdu;
Sujithe63835b2008-11-18 09:07:53 +0530556 hdr = (struct ieee80211_hdr *)skb->data;
557 fc = hdr->frame_control;
Sujith528f0c62008-10-29 10:14:26 +0530558 tx_info = IEEE80211_SKB_CB(skb);
Sujithe63835b2008-11-18 09:07:53 +0530559 rates = tx_info->control.rates;
Sujith528f0c62008-10-29 10:14:26 +0530560
Sujithe63835b2008-11-18 09:07:53 +0530561 if (ieee80211_has_morefrags(fc) ||
562 (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)) {
563 rates[1].count = rates[2].count = rates[3].count = 0;
564 rates[1].idx = rates[2].idx = rates[3].idx = 0;
565 rates[0].count = ATH_TXMAXTRY;
566 }
567
568 /* get the cix for the lowest valid rix */
Sujith3706de62008-12-07 21:42:10 +0530569 rt = sc->cur_rate_table;
Sujitha8efee42008-11-18 09:07:30 +0530570 for (i = 3; i >= 0; i--) {
Sujithe63835b2008-11-18 09:07:53 +0530571 if (rates[i].count && (rates[i].idx >= 0)) {
Sujitha8efee42008-11-18 09:07:30 +0530572 rix = rates[i].idx;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700573 break;
574 }
575 }
Sujithe63835b2008-11-18 09:07:53 +0530576
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700577 flags = (bf->bf_flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA));
Sujithe63835b2008-11-18 09:07:53 +0530578 cix = rt->info[rix].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700579
580 /*
Sujithe63835b2008-11-18 09:07:53 +0530581 * If 802.11g protection is enabled, determine whether to use RTS/CTS or
582 * just CTS. Note that this is only done for OFDM/HT unicast frames.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700583 */
Sujithe63835b2008-11-18 09:07:53 +0530584 if (sc->sc_protmode != PROT_M_NONE && !(bf->bf_flags & ATH9K_TXDESC_NOACK)
Sujith46d14a52008-11-18 09:08:13 +0530585 && (rt->info[rix].phy == WLAN_RC_PHY_OFDM ||
Sujithe63835b2008-11-18 09:07:53 +0530586 WLAN_RC_PHY_HT(rt->info[rix].phy))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700587 if (sc->sc_protmode == PROT_M_RTSCTS)
588 flags = ATH9K_TXDESC_RTSENA;
589 else if (sc->sc_protmode == PROT_M_CTSONLY)
590 flags = ATH9K_TXDESC_CTSENA;
591
Sujithe63835b2008-11-18 09:07:53 +0530592 cix = rt->info[sc->sc_protrix].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700593 rtsctsena = 1;
594 }
595
Sujithe63835b2008-11-18 09:07:53 +0530596 /* For 11n, the default behavior is to enable RTS for hw retried frames.
597 * We enable the global flag here and let rate series flags determine
598 * which rates will actually use RTS.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700599 */
Sujithcd3d39a2008-08-11 14:03:34 +0530600 if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) && bf_isdata(bf)) {
Sujithe63835b2008-11-18 09:07:53 +0530601 /* 802.11g protection not needed, use our default behavior */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700602 if (!rtsctsena)
603 flags = ATH9K_TXDESC_RTSENA;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700604 }
605
Sujithe63835b2008-11-18 09:07:53 +0530606 /* Set protection if aggregate protection on */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700607 if (sc->sc_config.ath_aggr_prot &&
Sujithcd3d39a2008-08-11 14:03:34 +0530608 (!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700609 flags = ATH9K_TXDESC_RTSENA;
Sujithe63835b2008-11-18 09:07:53 +0530610 cix = rt->info[sc->sc_protrix].ctrl_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700611 rtsctsena = 1;
612 }
613
Sujithe63835b2008-11-18 09:07:53 +0530614 /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
615 if (bf_isaggr(bf) && (bf->bf_al > ah->ah_caps.rts_aggr_limit))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700616 flags &= ~(ATH9K_TXDESC_RTSENA);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700617
618 /*
Sujithe63835b2008-11-18 09:07:53 +0530619 * CTS transmit rate is derived from the transmit rate by looking in the
620 * h/w rate table. We must also factor in whether or not a short
621 * preamble is to be used. NB: cix is set above where RTS/CTS is enabled
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700622 */
Sujithe63835b2008-11-18 09:07:53 +0530623 ctsrate = rt->info[cix].ratecode |
624 (bf_isshpreamble(bf) ? rt->info[cix].short_preamble : 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700625
626 for (i = 0; i < 4; i++) {
Sujithe63835b2008-11-18 09:07:53 +0530627 if (!rates[i].count || (rates[i].idx < 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700628 continue;
629
Sujitha8efee42008-11-18 09:07:30 +0530630 rix = rates[i].idx;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700631
Sujithe63835b2008-11-18 09:07:53 +0530632 series[i].Rate = rt->info[rix].ratecode |
633 (bf_isshpreamble(bf) ? rt->info[rix].short_preamble : 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700634
Sujitha8efee42008-11-18 09:07:30 +0530635 series[i].Tries = rates[i].count;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700636
637 series[i].RateFlags = (
Sujitha8efee42008-11-18 09:07:30 +0530638 (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700639 ATH9K_RATESERIES_RTS_CTS : 0) |
Sujitha8efee42008-11-18 09:07:30 +0530640 ((rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700641 ATH9K_RATESERIES_2040 : 0) |
Sujitha8efee42008-11-18 09:07:30 +0530642 ((rates[i].flags & IEEE80211_TX_RC_SHORT_GI) ?
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700643 ATH9K_RATESERIES_HALFGI : 0);
644
Sujith102e0572008-10-29 10:15:16 +0530645 series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
Sujitha8efee42008-11-18 09:07:30 +0530646 (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) != 0,
647 (rates[i].flags & IEEE80211_TX_RC_SHORT_GI),
Sujith102e0572008-10-29 10:15:16 +0530648 bf_isshpreamble(bf));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700649
Sujithff37e332008-11-24 12:07:55 +0530650 series[i].ChSel = sc->sc_tx_chainmask;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700651
652 if (rtsctsena)
653 series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700654 }
655
Sujithe63835b2008-11-18 09:07:53 +0530656 /* set dur_update_en for l-sig computation except for PS-Poll frames */
657 ath9k_hw_set11n_ratescenario(ah, ds, lastds, !bf_ispspoll(bf),
658 ctsrate, ctsduration,
Sujithcd3d39a2008-08-11 14:03:34 +0530659 series, 4, flags);
Sujith102e0572008-10-29 10:15:16 +0530660
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700661 if (sc->sc_config.ath_aggr_prot && flags)
662 ath9k_hw_set11n_burstduration(ah, ds, 8192);
663}
664
665/*
666 * Function to send a normal HT (non-AMPDU) frame
667 * NB: must be called with txq lock held
668 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700669static int ath_tx_send_normal(struct ath_softc *sc,
670 struct ath_txq *txq,
671 struct ath_atx_tid *tid,
672 struct list_head *bf_head)
673{
674 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700675
676 BUG_ON(list_empty(bf_head));
677
678 bf = list_first_entry(bf_head, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +0530679 bf->bf_state.bf_type &= ~BUF_AMPDU; /* regular HT frame */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700680
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700681 /* update starting sequence number for subsequent ADDBA request */
682 INCR(tid->seq_start, IEEE80211_SEQ_MAX);
683
684 /* Queue to h/w without aggregation */
685 bf->bf_nframes = 1;
686 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
687 ath_buf_set_rate(sc, bf);
688 ath_tx_txqaddbuf(sc, txq, bf_head);
689
690 return 0;
691}
692
693/* flush tid's software queue and send frames as non-ampdu's */
694
695static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
696{
Sujithb77f4832008-12-07 21:44:03 +0530697 struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700698 struct ath_buf *bf;
699 struct list_head bf_head;
700 INIT_LIST_HEAD(&bf_head);
701
702 ASSERT(tid->paused > 0);
703 spin_lock_bh(&txq->axq_lock);
704
705 tid->paused--;
706
707 if (tid->paused > 0) {
708 spin_unlock_bh(&txq->axq_lock);
709 return;
710 }
711
712 while (!list_empty(&tid->buf_q)) {
713 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +0530714 ASSERT(!bf_isretried(bf));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700715 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
716 ath_tx_send_normal(sc, txq, tid, &bf_head);
717 }
718
719 spin_unlock_bh(&txq->axq_lock);
720}
721
722/* Completion routine of an aggregate */
723
724static void ath_tx_complete_aggr_rifs(struct ath_softc *sc,
725 struct ath_txq *txq,
726 struct ath_buf *bf,
727 struct list_head *bf_q,
728 int txok)
729{
Sujith528f0c62008-10-29 10:14:26 +0530730 struct ath_node *an = NULL;
731 struct sk_buff *skb;
732 struct ieee80211_tx_info *tx_info;
733 struct ath_atx_tid *tid = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700734 struct ath_buf *bf_last = bf->bf_lastbf;
735 struct ath_desc *ds = bf_last->bf_desc;
736 struct ath_buf *bf_next, *bf_lastq = NULL;
737 struct list_head bf_head, bf_pending;
738 u16 seq_st = 0;
739 u32 ba[WME_BA_BMP_SIZE >> 5];
740 int isaggr, txfail, txpending, sendbar = 0, needreset = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700741
Sujith528f0c62008-10-29 10:14:26 +0530742 skb = (struct sk_buff *)bf->bf_mpdu;
743 tx_info = IEEE80211_SKB_CB(skb);
744
745 if (tx_info->control.sta) {
746 an = (struct ath_node *)tx_info->control.sta->drv_priv;
747 tid = ATH_AN_2_TID(an, bf->bf_tidno);
748 }
749
Sujithcd3d39a2008-08-11 14:03:34 +0530750 isaggr = bf_isaggr(bf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700751 if (isaggr) {
752 if (txok) {
753 if (ATH_DS_TX_BA(ds)) {
754 /*
755 * extract starting sequence and
756 * block-ack bitmap
757 */
758 seq_st = ATH_DS_BA_SEQ(ds);
759 memcpy(ba,
760 ATH_DS_BA_BITMAP(ds),
761 WME_BA_BMP_SIZE >> 3);
762 } else {
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700763 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700764
765 /*
766 * AR5416 can become deaf/mute when BA
767 * issue happens. Chip needs to be reset.
768 * But AP code may have sychronization issues
769 * when perform internal reset in this routine.
770 * Only enable reset in STA mode for now.
771 */
Colin McCabed97809d2008-12-01 13:38:55 -0800772 if (sc->sc_ah->ah_opmode ==
773 NL80211_IFTYPE_STATION)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700774 needreset = 1;
775 }
776 } else {
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700777 memset(ba, 0, WME_BA_BMP_SIZE >> 3);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700778 }
779 }
780
781 INIT_LIST_HEAD(&bf_pending);
782 INIT_LIST_HEAD(&bf_head);
783
784 while (bf) {
785 txfail = txpending = 0;
786 bf_next = bf->bf_next;
787
788 if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
789 /* transmit completion, subframe is
790 * acked by block ack */
791 } else if (!isaggr && txok) {
792 /* transmit completion */
793 } else {
794
Sujitha37c2c72008-10-29 10:15:40 +0530795 if (!(tid->state & AGGR_CLEANUP) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700796 ds->ds_txstat.ts_flags != ATH9K_TX_SW_ABORTED) {
797 if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
798 ath_tx_set_retry(sc, bf);
799 txpending = 1;
800 } else {
Sujithcd3d39a2008-08-11 14:03:34 +0530801 bf->bf_state.bf_type |= BUF_XRETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700802 txfail = 1;
803 sendbar = 1;
804 }
805 } else {
806 /*
807 * cleanup in progress, just fail
808 * the un-acked sub-frames
809 */
810 txfail = 1;
811 }
812 }
813 /*
814 * Remove ath_buf's of this sub-frame from aggregate queue.
815 */
816 if (bf_next == NULL) { /* last subframe in the aggregate */
817 ASSERT(bf->bf_lastfrm == bf_last);
818
819 /*
820 * The last descriptor of the last sub frame could be
821 * a holding descriptor for h/w. If that's the case,
822 * bf->bf_lastfrm won't be in the bf_q.
823 * Make sure we handle bf_q properly here.
824 */
825
826 if (!list_empty(bf_q)) {
827 bf_lastq = list_entry(bf_q->prev,
828 struct ath_buf, list);
829 list_cut_position(&bf_head,
830 bf_q, &bf_lastq->list);
831 } else {
832 /*
833 * XXX: if the last subframe only has one
834 * descriptor which is also being used as
835 * a holding descriptor. Then the ath_buf
836 * is not in the bf_q at all.
837 */
838 INIT_LIST_HEAD(&bf_head);
839 }
840 } else {
841 ASSERT(!list_empty(bf_q));
842 list_cut_position(&bf_head,
843 bf_q, &bf->bf_lastfrm->list);
844 }
845
846 if (!txpending) {
847 /*
848 * complete the acked-ones/xretried ones; update
849 * block-ack window
850 */
851 spin_lock_bh(&txq->axq_lock);
852 ath_tx_update_baw(sc, tid, bf->bf_seqno);
853 spin_unlock_bh(&txq->axq_lock);
854
855 /* complete this sub-frame */
856 ath_tx_complete_buf(sc, bf, &bf_head, !txfail, sendbar);
857 } else {
858 /*
859 * retry the un-acked ones
860 */
861 /*
862 * XXX: if the last descriptor is holding descriptor,
863 * in order to requeue the frame to software queue, we
864 * need to allocate a new descriptor and
865 * copy the content of holding descriptor to it.
866 */
867 if (bf->bf_next == NULL &&
868 bf_last->bf_status & ATH_BUFSTATUS_STALE) {
869 struct ath_buf *tbf;
870
871 /* allocate new descriptor */
Sujithb77f4832008-12-07 21:44:03 +0530872 spin_lock_bh(&sc->tx.txbuflock);
873 ASSERT(!list_empty((&sc->tx.txbuf)));
874 tbf = list_first_entry(&sc->tx.txbuf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700875 struct ath_buf, list);
876 list_del(&tbf->list);
Sujithb77f4832008-12-07 21:44:03 +0530877 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700878
879 ATH_TXBUF_RESET(tbf);
880
881 /* copy descriptor content */
882 tbf->bf_mpdu = bf_last->bf_mpdu;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700883 tbf->bf_buf_addr = bf_last->bf_buf_addr;
884 *(tbf->bf_desc) = *(bf_last->bf_desc);
885
886 /* link it to the frame */
887 if (bf_lastq) {
888 bf_lastq->bf_desc->ds_link =
889 tbf->bf_daddr;
890 bf->bf_lastfrm = tbf;
891 ath9k_hw_cleartxdesc(sc->sc_ah,
892 bf->bf_lastfrm->bf_desc);
893 } else {
894 tbf->bf_state = bf_last->bf_state;
895 tbf->bf_lastfrm = tbf;
896 ath9k_hw_cleartxdesc(sc->sc_ah,
897 tbf->bf_lastfrm->bf_desc);
898
899 /* copy the DMA context */
Sujithff9b6622008-08-14 13:27:16 +0530900 tbf->bf_dmacontext =
901 bf_last->bf_dmacontext;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700902 }
903 list_add_tail(&tbf->list, &bf_head);
904 } else {
905 /*
906 * Clear descriptor status words for
907 * software retry
908 */
909 ath9k_hw_cleartxdesc(sc->sc_ah,
Sujithff9b6622008-08-14 13:27:16 +0530910 bf->bf_lastfrm->bf_desc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700911 }
912
913 /*
914 * Put this buffer to the temporary pending
915 * queue to retain ordering
916 */
917 list_splice_tail_init(&bf_head, &bf_pending);
918 }
919
920 bf = bf_next;
921 }
922
Sujitha37c2c72008-10-29 10:15:40 +0530923 if (tid->state & AGGR_CLEANUP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700924 /* check to see if we're done with cleaning the h/w queue */
925 spin_lock_bh(&txq->axq_lock);
926
927 if (tid->baw_head == tid->baw_tail) {
Sujitha37c2c72008-10-29 10:15:40 +0530928 tid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700929 tid->addba_exchangeattempts = 0;
930 spin_unlock_bh(&txq->axq_lock);
931
Sujitha37c2c72008-10-29 10:15:40 +0530932 tid->state &= ~AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700933
934 /* send buffered frames as singles */
935 ath_tx_flush_tid(sc, tid);
936 } else
937 spin_unlock_bh(&txq->axq_lock);
938
939 return;
940 }
941
942 /*
943 * prepend un-acked frames to the beginning of the pending frame queue
944 */
945 if (!list_empty(&bf_pending)) {
946 spin_lock_bh(&txq->axq_lock);
947 /* Note: we _prepend_, we _do_not_ at to
948 * the end of the queue ! */
949 list_splice(&bf_pending, &tid->buf_q);
950 ath_tx_queue_tid(txq, tid);
951 spin_unlock_bh(&txq->axq_lock);
952 }
953
954 if (needreset)
Sujithf45144e2008-08-11 14:02:53 +0530955 ath_reset(sc, false);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700956
957 return;
958}
959
Sujithc4288392008-11-18 09:09:30 +0530960static void ath_tx_rc_status(struct ath_buf *bf, struct ath_desc *ds, int nbad)
961{
962 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
963 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
964 struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
965
Vasanthakumar Thiagarajan7ac47012008-11-20 11:51:18 +0530966 tx_info_priv->update_rc = false;
Sujithc4288392008-11-18 09:09:30 +0530967 if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
968 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
969
970 if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
971 (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
972 if (bf_isdata(bf)) {
973 memcpy(&tx_info_priv->tx, &ds->ds_txstat,
974 sizeof(tx_info_priv->tx));
975 tx_info_priv->n_frames = bf->bf_nframes;
976 tx_info_priv->n_bad_frames = nbad;
Vasanthakumar Thiagarajan7ac47012008-11-20 11:51:18 +0530977 tx_info_priv->update_rc = true;
Sujithc4288392008-11-18 09:09:30 +0530978 }
979 }
980}
981
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700982/* Process completed xmit descriptors from the specified queue */
983
Sujithc4288392008-11-18 09:09:30 +0530984static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700985{
986 struct ath_hal *ah = sc->sc_ah;
987 struct ath_buf *bf, *lastbf, *bf_held = NULL;
988 struct list_head bf_head;
Sujithc4288392008-11-18 09:09:30 +0530989 struct ath_desc *ds;
990 int txok, nbad = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700991 int status;
992
Sujith04bd46382008-11-28 22:18:05 +0530993 DPRINTF(sc, ATH_DBG_QUEUE, "tx queue %d (%x), link %p\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700994 txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
995 txq->axq_link);
996
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700997 for (;;) {
998 spin_lock_bh(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700999 if (list_empty(&txq->axq_q)) {
1000 txq->axq_link = NULL;
1001 txq->axq_linkbuf = NULL;
1002 spin_unlock_bh(&txq->axq_lock);
1003 break;
1004 }
1005 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
1006
1007 /*
1008 * There is a race condition that a BH gets scheduled
1009 * after sw writes TxE and before hw re-load the last
1010 * descriptor to get the newly chained one.
1011 * Software must keep the last DONE descriptor as a
1012 * holding descriptor - software does so by marking
1013 * it with the STALE flag.
1014 */
1015 bf_held = NULL;
1016 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
1017 bf_held = bf;
1018 if (list_is_last(&bf_held->list, &txq->axq_q)) {
1019 /* FIXME:
1020 * The holding descriptor is the last
1021 * descriptor in queue. It's safe to remove
1022 * the last holding descriptor in BH context.
1023 */
1024 spin_unlock_bh(&txq->axq_lock);
1025 break;
1026 } else {
1027 /* Lets work with the next buffer now */
1028 bf = list_entry(bf_held->list.next,
1029 struct ath_buf, list);
1030 }
1031 }
1032
1033 lastbf = bf->bf_lastbf;
1034 ds = lastbf->bf_desc; /* NB: last decriptor */
1035
1036 status = ath9k_hw_txprocdesc(ah, ds);
1037 if (status == -EINPROGRESS) {
1038 spin_unlock_bh(&txq->axq_lock);
1039 break;
1040 }
1041 if (bf->bf_desc == txq->axq_lastdsWithCTS)
1042 txq->axq_lastdsWithCTS = NULL;
1043 if (ds == txq->axq_gatingds)
1044 txq->axq_gatingds = NULL;
1045
1046 /*
1047 * Remove ath_buf's of the same transmit unit from txq,
1048 * however leave the last descriptor back as the holding
1049 * descriptor for hw.
1050 */
1051 lastbf->bf_status |= ATH_BUFSTATUS_STALE;
1052 INIT_LIST_HEAD(&bf_head);
1053
1054 if (!list_is_singular(&lastbf->list))
1055 list_cut_position(&bf_head,
1056 &txq->axq_q, lastbf->list.prev);
1057
1058 txq->axq_depth--;
1059
Sujithcd3d39a2008-08-11 14:03:34 +05301060 if (bf_isaggr(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001061 txq->axq_aggr_depth--;
1062
1063 txok = (ds->ds_txstat.ts_status == 0);
1064
1065 spin_unlock_bh(&txq->axq_lock);
1066
1067 if (bf_held) {
1068 list_del(&bf_held->list);
Sujithb77f4832008-12-07 21:44:03 +05301069 spin_lock_bh(&sc->tx.txbuflock);
1070 list_add_tail(&bf_held->list, &sc->tx.txbuf);
1071 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001072 }
1073
Sujithcd3d39a2008-08-11 14:03:34 +05301074 if (!bf_isampdu(bf)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001075 /*
1076 * This frame is sent out as a single frame.
1077 * Use hardware retry status for this frame.
1078 */
1079 bf->bf_retries = ds->ds_txstat.ts_longretry;
1080 if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
Sujithcd3d39a2008-08-11 14:03:34 +05301081 bf->bf_state.bf_type |= BUF_XRETRY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001082 nbad = 0;
1083 } else {
1084 nbad = ath_tx_num_badfrms(sc, bf, txok);
1085 }
Johannes Berge6a98542008-10-21 12:40:02 +02001086
Sujithc4288392008-11-18 09:09:30 +05301087 ath_tx_rc_status(bf, ds, nbad);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001088
1089 /*
1090 * Complete this transmit unit
1091 */
Sujithcd3d39a2008-08-11 14:03:34 +05301092 if (bf_isampdu(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001093 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, txok);
1094 else
1095 ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
1096
1097 /* Wake up mac80211 queue */
1098
1099 spin_lock_bh(&txq->axq_lock);
1100 if (txq->stopped && ath_txq_depth(sc, txq->axq_qnum) <=
1101 (ATH_TXBUF - 20)) {
1102 int qnum;
1103 qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
1104 if (qnum != -1) {
1105 ieee80211_wake_queue(sc->hw, qnum);
1106 txq->stopped = 0;
1107 }
1108
1109 }
1110
1111 /*
1112 * schedule any pending packets if aggregation is enabled
1113 */
Sujith672840a2008-08-11 14:05:08 +05301114 if (sc->sc_flags & SC_OP_TXAGGR)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001115 ath_txq_schedule(sc, txq);
1116 spin_unlock_bh(&txq->axq_lock);
1117 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001118}
1119
1120static void ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
1121{
1122 struct ath_hal *ah = sc->sc_ah;
1123
1124 (void) ath9k_hw_stoptxdma(ah, txq->axq_qnum);
Sujith04bd46382008-11-28 22:18:05 +05301125 DPRINTF(sc, ATH_DBG_XMIT, "tx queue [%u] %x, link %p\n",
1126 txq->axq_qnum, ath9k_hw_gettxbuf(ah, txq->axq_qnum),
1127 txq->axq_link);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001128}
1129
1130/* Drain only the data queues */
1131
1132static void ath_drain_txdataq(struct ath_softc *sc, bool retry_tx)
1133{
1134 struct ath_hal *ah = sc->sc_ah;
Sujith102e0572008-10-29 10:15:16 +05301135 int i, status, npend = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001136
Sujith672840a2008-08-11 14:05:08 +05301137 if (!(sc->sc_flags & SC_OP_INVALID)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001138 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1139 if (ATH_TXQ_SETUP(sc, i)) {
Sujithb77f4832008-12-07 21:44:03 +05301140 ath_tx_stopdma(sc, &sc->tx.txq[i]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001141 /* The TxDMA may not really be stopped.
1142 * Double check the hal tx pending count */
1143 npend += ath9k_hw_numtxpending(ah,
Sujithb77f4832008-12-07 21:44:03 +05301144 sc->tx.txq[i].axq_qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001145 }
1146 }
1147 }
1148
1149 if (npend) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001150 /* TxDMA not stopped, reset the hal */
Sujith04bd46382008-11-28 22:18:05 +05301151 DPRINTF(sc, ATH_DBG_XMIT, "Unable to stop TxDMA. Reset HAL!\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001152
1153 spin_lock_bh(&sc->sc_resetlock);
Sujithb4696c8b2008-08-11 14:04:52 +05301154 if (!ath9k_hw_reset(ah,
Sujith927e70e2008-08-14 13:26:34 +05301155 sc->sc_ah->ah_curchan,
Sujith99405f92008-11-24 12:08:35 +05301156 sc->tx_chan_width,
Sujith927e70e2008-08-14 13:26:34 +05301157 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
1158 sc->sc_ht_extprotspacing, true, &status)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001159
1160 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301161 "Unable to reset hardware; hal status %u\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001162 status);
1163 }
1164 spin_unlock_bh(&sc->sc_resetlock);
1165 }
1166
1167 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1168 if (ATH_TXQ_SETUP(sc, i))
Sujithb77f4832008-12-07 21:44:03 +05301169 ath_tx_draintxq(sc, &sc->tx.txq[i], retry_tx);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001170 }
1171}
1172
1173/* Add a sub-frame to block ack window */
1174
1175static void ath_tx_addto_baw(struct ath_softc *sc,
1176 struct ath_atx_tid *tid,
1177 struct ath_buf *bf)
1178{
1179 int index, cindex;
1180
Sujithcd3d39a2008-08-11 14:03:34 +05301181 if (bf_isretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001182 return;
1183
1184 index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
1185 cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
1186
1187 ASSERT(tid->tx_buf[cindex] == NULL);
1188 tid->tx_buf[cindex] = bf;
1189
1190 if (index >= ((tid->baw_tail - tid->baw_head) &
1191 (ATH_TID_MAX_BUFS - 1))) {
1192 tid->baw_tail = cindex;
1193 INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
1194 }
1195}
1196
1197/*
1198 * Function to send an A-MPDU
1199 * NB: must be called with txq lock held
1200 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001201static int ath_tx_send_ampdu(struct ath_softc *sc,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001202 struct ath_atx_tid *tid,
1203 struct list_head *bf_head,
1204 struct ath_tx_control *txctl)
1205{
1206 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001207
1208 BUG_ON(list_empty(bf_head));
1209
1210 bf = list_first_entry(bf_head, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +05301211 bf->bf_state.bf_type |= BUF_AMPDU;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001212
1213 /*
1214 * Do not queue to h/w when any of the following conditions is true:
1215 * - there are pending frames in software queue
1216 * - the TID is currently paused for ADDBA/BAR request
1217 * - seqno is not within block-ack window
1218 * - h/w queue depth exceeds low water mark
1219 */
1220 if (!list_empty(&tid->buf_q) || tid->paused ||
1221 !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
Sujith528f0c62008-10-29 10:14:26 +05301222 txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001223 /*
1224 * Add this frame to software queue for scheduling later
1225 * for aggregation.
1226 */
1227 list_splice_tail_init(bf_head, &tid->buf_q);
Sujith528f0c62008-10-29 10:14:26 +05301228 ath_tx_queue_tid(txctl->txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001229 return 0;
1230 }
1231
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001232 /* Add sub-frame to BAW */
1233 ath_tx_addto_baw(sc, tid, bf);
1234
1235 /* Queue to h/w without aggregation */
1236 bf->bf_nframes = 1;
1237 bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
1238 ath_buf_set_rate(sc, bf);
Sujith528f0c62008-10-29 10:14:26 +05301239 ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
Sujith102e0572008-10-29 10:15:16 +05301240
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001241 return 0;
1242}
1243
1244/*
1245 * looks up the rate
1246 * returns aggr limit based on lowest of the rates
1247 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001248static u32 ath_lookup_rate(struct ath_softc *sc,
Johannes Bergae5eb022008-10-14 16:58:37 +02001249 struct ath_buf *bf,
1250 struct ath_atx_tid *tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001251{
Sujith3706de62008-12-07 21:42:10 +05301252 struct ath_rate_table *rate_table = sc->cur_rate_table;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001253 struct sk_buff *skb;
1254 struct ieee80211_tx_info *tx_info;
Sujitha8efee42008-11-18 09:07:30 +05301255 struct ieee80211_tx_rate *rates;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001256 struct ath_tx_info_priv *tx_info_priv;
1257 u32 max_4ms_framelen, frame_length;
1258 u16 aggr_limit, legacy = 0, maxampdu;
1259 int i;
1260
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001261 skb = (struct sk_buff *)bf->bf_mpdu;
1262 tx_info = IEEE80211_SKB_CB(skb);
Sujitha8efee42008-11-18 09:07:30 +05301263 rates = tx_info->control.rates;
1264 tx_info_priv =
1265 (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001266
1267 /*
1268 * Find the lowest frame length among the rate series that will have a
1269 * 4ms transmit duration.
1270 * TODO - TXOP limit needs to be considered.
1271 */
1272 max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
1273
1274 for (i = 0; i < 4; i++) {
Sujitha8efee42008-11-18 09:07:30 +05301275 if (rates[i].count) {
Sujithe63835b2008-11-18 09:07:53 +05301276 if (!WLAN_RC_PHY_HT(rate_table->info[rates[i].idx].phy)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001277 legacy = 1;
1278 break;
1279 }
1280
Sujitha8efee42008-11-18 09:07:30 +05301281 frame_length =
1282 rate_table->info[rates[i].idx].max_4ms_framelen;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001283 max_4ms_framelen = min(max_4ms_framelen, frame_length);
1284 }
1285 }
1286
1287 /*
1288 * limit aggregate size by the minimum rate if rate selected is
1289 * not a probe rate, if rate selected is a probe rate then
1290 * avoid aggregation of this packet.
1291 */
1292 if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
1293 return 0;
1294
1295 aggr_limit = min(max_4ms_framelen,
1296 (u32)ATH_AMPDU_LIMIT_DEFAULT);
1297
1298 /*
1299 * h/w can accept aggregates upto 16 bit lengths (65535).
1300 * The IE, however can hold upto 65536, which shows up here
1301 * as zero. Ignore 65536 since we are constrained by hw.
1302 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001303 maxampdu = tid->an->maxampdu;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001304 if (maxampdu)
1305 aggr_limit = min(aggr_limit, maxampdu);
1306
1307 return aggr_limit;
1308}
1309
1310/*
1311 * returns the number of delimiters to be added to
1312 * meet the minimum required mpdudensity.
1313 * caller should make sure that the rate is HT rate .
1314 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001315static int ath_compute_num_delims(struct ath_softc *sc,
Johannes Bergae5eb022008-10-14 16:58:37 +02001316 struct ath_atx_tid *tid,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001317 struct ath_buf *bf,
1318 u16 frmlen)
1319{
Sujith3706de62008-12-07 21:42:10 +05301320 struct ath_rate_table *rt = sc->cur_rate_table;
Sujitha8efee42008-11-18 09:07:30 +05301321 struct sk_buff *skb = bf->bf_mpdu;
1322 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001323 u32 nsymbits, nsymbols, mpdudensity;
1324 u16 minlen;
1325 u8 rc, flags, rix;
1326 int width, half_gi, ndelim, mindelim;
1327
1328 /* Select standard number of delimiters based on frame length alone */
1329 ndelim = ATH_AGGR_GET_NDELIM(frmlen);
1330
1331 /*
1332 * If encryption enabled, hardware requires some more padding between
1333 * subframes.
1334 * TODO - this could be improved to be dependent on the rate.
1335 * The hardware can keep up at lower rates, but not higher rates
1336 */
1337 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
1338 ndelim += ATH_AGGR_ENCRYPTDELIM;
1339
1340 /*
1341 * Convert desired mpdu density from microeconds to bytes based
1342 * on highest rate in rate series (i.e. first rate) to determine
1343 * required minimum length for subframe. Take into account
1344 * whether high rate is 20 or 40Mhz and half or full GI.
1345 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001346 mpdudensity = tid->an->mpdudensity;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001347
1348 /*
1349 * If there is no mpdu density restriction, no further calculation
1350 * is needed.
1351 */
1352 if (mpdudensity == 0)
1353 return ndelim;
1354
Sujitha8efee42008-11-18 09:07:30 +05301355 rix = tx_info->control.rates[0].idx;
1356 flags = tx_info->control.rates[0].flags;
Sujithe63835b2008-11-18 09:07:53 +05301357 rc = rt->info[rix].ratecode;
Sujitha8efee42008-11-18 09:07:30 +05301358 width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
1359 half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001360
1361 if (half_gi)
1362 nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
1363 else
1364 nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
1365
1366 if (nsymbols == 0)
1367 nsymbols = 1;
1368
1369 nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
1370 minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
1371
1372 /* Is frame shorter than required minimum length? */
1373 if (frmlen < minlen) {
1374 /* Get the minimum number of delimiters required. */
1375 mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
1376 ndelim = max(mindelim, ndelim);
1377 }
1378
1379 return ndelim;
1380}
1381
1382/*
1383 * For aggregation from software buffer queue.
1384 * NB: must be called with txq lock held
1385 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001386static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
1387 struct ath_atx_tid *tid,
1388 struct list_head *bf_q,
1389 struct ath_buf **bf_last,
1390 struct aggr_rifs_param *param,
1391 int *prev_frames)
1392{
1393#define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
1394 struct ath_buf *bf, *tbf, *bf_first, *bf_prev = NULL;
1395 struct list_head bf_head;
1396 int rl = 0, nframes = 0, ndelim;
1397 u16 aggr_limit = 0, al = 0, bpad = 0,
1398 al_delta, h_baw = tid->baw_size / 2;
1399 enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
Sujitha8efee42008-11-18 09:07:30 +05301400 int prev_al = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001401 INIT_LIST_HEAD(&bf_head);
1402
1403 BUG_ON(list_empty(&tid->buf_q));
1404
1405 bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
1406
1407 do {
1408 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1409
1410 /*
1411 * do not step over block-ack window
1412 */
1413 if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
1414 status = ATH_AGGR_BAW_CLOSED;
1415 break;
1416 }
1417
1418 if (!rl) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001419 aggr_limit = ath_lookup_rate(sc, bf, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001420 rl = 1;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001421 }
1422
1423 /*
1424 * do not exceed aggregation limit
1425 */
1426 al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
1427
1428 if (nframes && (aggr_limit <
1429 (al + bpad + al_delta + prev_al))) {
1430 status = ATH_AGGR_LIMITED;
1431 break;
1432 }
1433
1434 /*
1435 * do not exceed subframe limit
1436 */
1437 if ((nframes + *prev_frames) >=
1438 min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
1439 status = ATH_AGGR_LIMITED;
1440 break;
1441 }
1442
1443 /*
1444 * add padding for previous frame to aggregation length
1445 */
1446 al += bpad + al_delta;
1447
1448 /*
1449 * Get the delimiters needed to meet the MPDU
1450 * density for this node.
1451 */
Johannes Bergae5eb022008-10-14 16:58:37 +02001452 ndelim = ath_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001453
1454 bpad = PADBYTES(al_delta) + (ndelim << 2);
1455
1456 bf->bf_next = NULL;
1457 bf->bf_lastfrm->bf_desc->ds_link = 0;
1458
1459 /*
1460 * this packet is part of an aggregate
1461 * - remove all descriptors belonging to this frame from
1462 * software queue
1463 * - add it to block ack window
1464 * - set up descriptors for aggregation
1465 */
1466 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1467 ath_tx_addto_baw(sc, tid, bf);
1468
1469 list_for_each_entry(tbf, &bf_head, list) {
1470 ath9k_hw_set11n_aggr_middle(sc->sc_ah,
1471 tbf->bf_desc, ndelim);
1472 }
1473
1474 /*
1475 * link buffers of this frame to the aggregate
1476 */
1477 list_splice_tail_init(&bf_head, bf_q);
1478 nframes++;
1479
1480 if (bf_prev) {
1481 bf_prev->bf_next = bf;
1482 bf_prev->bf_lastfrm->bf_desc->ds_link = bf->bf_daddr;
1483 }
1484 bf_prev = bf;
1485
1486#ifdef AGGR_NOSHORT
1487 /*
1488 * terminate aggregation on a small packet boundary
1489 */
1490 if (bf->bf_frmlen < ATH_AGGR_MINPLEN) {
1491 status = ATH_AGGR_SHORTPKT;
1492 break;
1493 }
1494#endif
1495 } while (!list_empty(&tid->buf_q));
1496
1497 bf_first->bf_al = al;
1498 bf_first->bf_nframes = nframes;
1499 *bf_last = bf_prev;
1500 return status;
1501#undef PADBYTES
1502}
1503
1504/*
1505 * process pending frames possibly doing a-mpdu aggregation
1506 * NB: must be called with txq lock held
1507 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001508static void ath_tx_sched_aggr(struct ath_softc *sc,
1509 struct ath_txq *txq, struct ath_atx_tid *tid)
1510{
1511 struct ath_buf *bf, *tbf, *bf_last, *bf_lastaggr = NULL;
1512 enum ATH_AGGR_STATUS status;
1513 struct list_head bf_q;
1514 struct aggr_rifs_param param = {0, 0, 0, 0, NULL};
1515 int prev_frames = 0;
1516
1517 do {
1518 if (list_empty(&tid->buf_q))
1519 return;
1520
1521 INIT_LIST_HEAD(&bf_q);
1522
1523 status = ath_tx_form_aggr(sc, tid, &bf_q, &bf_lastaggr, &param,
1524 &prev_frames);
1525
1526 /*
1527 * no frames picked up to be aggregated; block-ack
1528 * window is not open
1529 */
1530 if (list_empty(&bf_q))
1531 break;
1532
1533 bf = list_first_entry(&bf_q, struct ath_buf, list);
1534 bf_last = list_entry(bf_q.prev, struct ath_buf, list);
1535 bf->bf_lastbf = bf_last;
1536
1537 /*
1538 * if only one frame, send as non-aggregate
1539 */
1540 if (bf->bf_nframes == 1) {
1541 ASSERT(bf->bf_lastfrm == bf_last);
1542
Sujithcd3d39a2008-08-11 14:03:34 +05301543 bf->bf_state.bf_type &= ~BUF_AGGR;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001544 /*
1545 * clear aggr bits for every descriptor
1546 * XXX TODO: is there a way to optimize it?
1547 */
1548 list_for_each_entry(tbf, &bf_q, list) {
1549 ath9k_hw_clr11n_aggr(sc->sc_ah, tbf->bf_desc);
1550 }
1551
1552 ath_buf_set_rate(sc, bf);
1553 ath_tx_txqaddbuf(sc, txq, &bf_q);
1554 continue;
1555 }
1556
1557 /*
1558 * setup first desc with rate and aggr info
1559 */
Sujithcd3d39a2008-08-11 14:03:34 +05301560 bf->bf_state.bf_type |= BUF_AGGR;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001561 ath_buf_set_rate(sc, bf);
1562 ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
1563
1564 /*
1565 * anchor last frame of aggregate correctly
1566 */
1567 ASSERT(bf_lastaggr);
1568 ASSERT(bf_lastaggr->bf_lastfrm == bf_last);
1569 tbf = bf_lastaggr;
1570 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1571
1572 /* XXX: We don't enter into this loop, consider removing this */
1573 while (!list_empty(&bf_q) && !list_is_last(&tbf->list, &bf_q)) {
1574 tbf = list_entry(tbf->list.next, struct ath_buf, list);
1575 ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
1576 }
1577
1578 txq->axq_aggr_depth++;
1579
1580 /*
1581 * Normal aggregate, queue to hardware
1582 */
1583 ath_tx_txqaddbuf(sc, txq, &bf_q);
1584
1585 } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
1586 status != ATH_AGGR_BAW_CLOSED);
1587}
1588
1589/* Called with txq lock held */
1590
1591static void ath_tid_drain(struct ath_softc *sc,
1592 struct ath_txq *txq,
Sujithb5aa9bf2008-10-29 10:13:31 +05301593 struct ath_atx_tid *tid)
1594
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001595{
1596 struct ath_buf *bf;
1597 struct list_head bf_head;
1598 INIT_LIST_HEAD(&bf_head);
1599
1600 for (;;) {
1601 if (list_empty(&tid->buf_q))
1602 break;
1603 bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
1604
1605 list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
1606
1607 /* update baw for software retried frame */
Sujithcd3d39a2008-08-11 14:03:34 +05301608 if (bf_isretried(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001609 ath_tx_update_baw(sc, tid, bf->bf_seqno);
1610
1611 /*
1612 * do not indicate packets while holding txq spinlock.
1613 * unlock is intentional here
1614 */
Sujithb5aa9bf2008-10-29 10:13:31 +05301615 spin_unlock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001616
1617 /* complete this sub-frame */
1618 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
1619
Sujithb5aa9bf2008-10-29 10:13:31 +05301620 spin_lock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001621 }
1622
1623 /*
1624 * TODO: For frame(s) that are in the retry state, we will reuse the
1625 * sequence number(s) without setting the retry bit. The
1626 * alternative is to give up on these and BAR the receiver's window
1627 * forward.
1628 */
1629 tid->seq_next = tid->seq_start;
1630 tid->baw_tail = tid->baw_head;
1631}
1632
1633/*
1634 * Drain all pending buffers
1635 * NB: must be called with txq lock held
1636 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001637static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
Sujithb5aa9bf2008-10-29 10:13:31 +05301638 struct ath_txq *txq)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001639{
1640 struct ath_atx_ac *ac, *ac_tmp;
1641 struct ath_atx_tid *tid, *tid_tmp;
1642
1643 list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
1644 list_del(&ac->list);
1645 ac->sched = false;
1646 list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
1647 list_del(&tid->list);
1648 tid->sched = false;
Sujithb5aa9bf2008-10-29 10:13:31 +05301649 ath_tid_drain(sc, txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001650 }
1651 }
1652}
1653
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001654static int ath_tx_setup_buffer(struct ath_softc *sc, struct ath_buf *bf,
Sujith8f93b8b2008-11-18 09:10:42 +05301655 struct sk_buff *skb,
Sujith528f0c62008-10-29 10:14:26 +05301656 struct ath_tx_control *txctl)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001657{
Sujith528f0c62008-10-29 10:14:26 +05301658 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1659 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001660 struct ath_tx_info_priv *tx_info_priv;
Sujith528f0c62008-10-29 10:14:26 +05301661 int hdrlen;
1662 __le16 fc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001663
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001664 tx_info_priv = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
1665 if (unlikely(!tx_info_priv))
1666 return -ENOMEM;
Sujitha8efee42008-11-18 09:07:30 +05301667 tx_info->rate_driver_data[0] = tx_info_priv;
Sujith528f0c62008-10-29 10:14:26 +05301668 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1669 fc = hdr->frame_control;
Jouni Malinene022edb2008-08-22 17:31:33 +03001670
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001671 ATH_TXBUF_RESET(bf);
Sujith528f0c62008-10-29 10:14:26 +05301672
1673 /* Frame type */
1674
1675 bf->bf_frmlen = skb->len + FCS_LEN - (hdrlen & 3);
Sujithcd3d39a2008-08-11 14:03:34 +05301676
1677 ieee80211_is_data(fc) ?
1678 (bf->bf_state.bf_type |= BUF_DATA) :
1679 (bf->bf_state.bf_type &= ~BUF_DATA);
1680 ieee80211_is_back_req(fc) ?
1681 (bf->bf_state.bf_type |= BUF_BAR) :
1682 (bf->bf_state.bf_type &= ~BUF_BAR);
1683 ieee80211_is_pspoll(fc) ?
1684 (bf->bf_state.bf_type |= BUF_PSPOLL) :
1685 (bf->bf_state.bf_type &= ~BUF_PSPOLL);
Sujith672840a2008-08-11 14:05:08 +05301686 (sc->sc_flags & SC_OP_PREAMBLE_SHORT) ?
Sujithcd3d39a2008-08-11 14:03:34 +05301687 (bf->bf_state.bf_type |= BUF_SHORT_PREAMBLE) :
1688 (bf->bf_state.bf_type &= ~BUF_SHORT_PREAMBLE);
Sujitha8efee42008-11-18 09:07:30 +05301689 (sc->hw->conf.ht.enabled && !is_pae(skb) &&
Sujith528f0c62008-10-29 10:14:26 +05301690 (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) ?
1691 (bf->bf_state.bf_type |= BUF_HT) :
1692 (bf->bf_state.bf_type &= ~BUF_HT);
Sujithcd3d39a2008-08-11 14:03:34 +05301693
Sujith528f0c62008-10-29 10:14:26 +05301694 bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
1695
1696 /* Crypto */
1697
1698 bf->bf_keytype = get_hw_crypto_keytype(skb);
1699
1700 if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR) {
1701 bf->bf_frmlen += tx_info->control.hw_key->icv_len;
1702 bf->bf_keyix = tx_info->control.hw_key->hw_key_idx;
1703 } else {
1704 bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
1705 }
1706
Sujith528f0c62008-10-29 10:14:26 +05301707 /* Assign seqno, tidno */
1708
1709 if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR))
1710 assign_aggr_tid_seqno(skb, bf);
1711
1712 /* DMA setup */
1713
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001714 bf->bf_mpdu = skb;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001715
Sujith528f0c62008-10-29 10:14:26 +05301716 bf->bf_dmacontext = pci_map_single(sc->pdev, skb->data,
1717 skb->len, PCI_DMA_TODEVICE);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001718 if (unlikely(pci_dma_mapping_error(sc->pdev, bf->bf_dmacontext))) {
1719 bf->bf_mpdu = NULL;
1720 DPRINTF(sc, ATH_DBG_CONFIG,
1721 "pci_dma_mapping_error() on TX\n");
1722 return -ENOMEM;
1723 }
1724
Sujith528f0c62008-10-29 10:14:26 +05301725 bf->bf_buf_addr = bf->bf_dmacontext;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001726 return 0;
Sujith528f0c62008-10-29 10:14:26 +05301727}
1728
1729/* FIXME: tx power */
1730static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
Sujith528f0c62008-10-29 10:14:26 +05301731 struct ath_tx_control *txctl)
1732{
1733 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
1734 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1735 struct ath_node *an = NULL;
1736 struct list_head bf_head;
1737 struct ath_desc *ds;
1738 struct ath_atx_tid *tid;
1739 struct ath_hal *ah = sc->sc_ah;
1740 int frm_type;
1741
Sujith528f0c62008-10-29 10:14:26 +05301742 frm_type = get_hw_packet_type(skb);
1743
1744 INIT_LIST_HEAD(&bf_head);
1745 list_add_tail(&bf->list, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001746
1747 /* setup descriptor */
Sujith528f0c62008-10-29 10:14:26 +05301748
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001749 ds = bf->bf_desc;
1750 ds->ds_link = 0;
1751 ds->ds_data = bf->bf_buf_addr;
1752
Sujith528f0c62008-10-29 10:14:26 +05301753 /* Formulate first tx descriptor with tx controls */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001754
Sujith528f0c62008-10-29 10:14:26 +05301755 ath9k_hw_set11n_txdesc(ah, ds, bf->bf_frmlen, frm_type, MAX_RATE_POWER,
1756 bf->bf_keyix, bf->bf_keytype, bf->bf_flags);
1757
1758 ath9k_hw_filltxdesc(ah, ds,
Sujith8f93b8b2008-11-18 09:10:42 +05301759 skb->len, /* segment length */
1760 true, /* first segment */
1761 true, /* last segment */
1762 ds); /* first descriptor */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001763
1764 bf->bf_lastfrm = bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001765
Sujith528f0c62008-10-29 10:14:26 +05301766 spin_lock_bh(&txctl->txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001767
John W. Linvillef1617962008-10-31 16:45:15 -04001768 if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR) &&
1769 tx_info->control.sta) {
1770 an = (struct ath_node *)tx_info->control.sta->drv_priv;
1771 tid = ATH_AN_2_TID(an, bf->bf_tidno);
1772
Sujith528f0c62008-10-29 10:14:26 +05301773 if (ath_aggr_query(sc, an, bf->bf_tidno)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001774 /*
1775 * Try aggregation if it's a unicast data frame
1776 * and the destination is HT capable.
1777 */
Sujith528f0c62008-10-29 10:14:26 +05301778 ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001779 } else {
1780 /*
Sujith528f0c62008-10-29 10:14:26 +05301781 * Send this frame as regular when ADDBA
1782 * exchange is neither complete nor pending.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001783 */
Sujith528f0c62008-10-29 10:14:26 +05301784 ath_tx_send_normal(sc, txctl->txq,
1785 tid, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001786 }
1787 } else {
1788 bf->bf_lastbf = bf;
1789 bf->bf_nframes = 1;
Sujith528f0c62008-10-29 10:14:26 +05301790
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001791 ath_buf_set_rate(sc, bf);
Sujith528f0c62008-10-29 10:14:26 +05301792 ath_tx_txqaddbuf(sc, txctl->txq, &bf_head);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001793 }
Sujith528f0c62008-10-29 10:14:26 +05301794
1795 spin_unlock_bh(&txctl->txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001796}
1797
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001798/* Upon failure caller should free skb */
Sujith528f0c62008-10-29 10:14:26 +05301799int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb,
1800 struct ath_tx_control *txctl)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001801{
Sujith528f0c62008-10-29 10:14:26 +05301802 struct ath_buf *bf;
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001803 int r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001804
Sujith528f0c62008-10-29 10:14:26 +05301805 /* Check if a tx buffer is available */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001806
Sujith528f0c62008-10-29 10:14:26 +05301807 bf = ath_tx_get_buffer(sc);
1808 if (!bf) {
Sujith04bd46382008-11-28 22:18:05 +05301809 DPRINTF(sc, ATH_DBG_XMIT, "TX buffers are full\n");
Sujith528f0c62008-10-29 10:14:26 +05301810 return -1;
1811 }
1812
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001813 r = ath_tx_setup_buffer(sc, bf, skb, txctl);
1814 if (unlikely(r)) {
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001815 struct ath_txq *txq = txctl->txq;
1816
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001817 DPRINTF(sc, ATH_DBG_FATAL, "TX mem alloc failure\n");
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001818
1819 /* upon ath_tx_processq() this TX queue will be resumed, we
1820 * guarantee this will happen by knowing beforehand that
1821 * we will at least have to run TX completionon one buffer
1822 * on the queue */
1823 spin_lock_bh(&txq->axq_lock);
1824 if (ath_txq_depth(sc, txq->axq_qnum) > 1) {
1825 ieee80211_stop_queue(sc->hw,
1826 skb_get_queue_mapping(skb));
1827 txq->stopped = 1;
1828 }
1829 spin_unlock_bh(&txq->axq_lock);
1830
Sujithb77f4832008-12-07 21:44:03 +05301831 spin_lock_bh(&sc->tx.txbuflock);
1832 list_add_tail(&bf->list, &sc->tx.txbuf);
1833 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezc112d0c2008-12-03 03:35:30 -08001834
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001835 return r;
1836 }
1837
Sujith8f93b8b2008-11-18 09:10:42 +05301838 ath_tx_start_dma(sc, bf, txctl);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001839
Sujith528f0c62008-10-29 10:14:26 +05301840 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001841}
1842
1843/* Initialize TX queue and h/w */
1844
1845int ath_tx_init(struct ath_softc *sc, int nbufs)
1846{
1847 int error = 0;
1848
1849 do {
Sujithb77f4832008-12-07 21:44:03 +05301850 spin_lock_init(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001851
1852 /* Setup tx descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301853 error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
Sujith556bb8f2008-08-11 14:03:53 +05301854 "tx", nbufs, 1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001855 if (error != 0) {
1856 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301857 "Failed to allocate tx descriptors: %d\n",
1858 error);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001859 break;
1860 }
1861
1862 /* XXX allocate beacon state together with vap */
Sujithb77f4832008-12-07 21:44:03 +05301863 error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001864 "beacon", ATH_BCBUF, 1);
1865 if (error != 0) {
1866 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301867 "Failed to allocate beacon descriptors: %d\n",
1868 error);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001869 break;
1870 }
1871
1872 } while (0);
1873
1874 if (error != 0)
1875 ath_tx_cleanup(sc);
1876
1877 return error;
1878}
1879
1880/* Reclaim all tx queue resources */
1881
1882int ath_tx_cleanup(struct ath_softc *sc)
1883{
1884 /* cleanup beacon descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301885 if (sc->beacon.bdma.dd_desc_len != 0)
1886 ath_descdma_cleanup(sc, &sc->beacon.bdma, &sc->beacon.bbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001887
1888 /* cleanup tx descriptors */
Sujithb77f4832008-12-07 21:44:03 +05301889 if (sc->tx.txdma.dd_desc_len != 0)
1890 ath_descdma_cleanup(sc, &sc->tx.txdma, &sc->tx.txbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001891
1892 return 0;
1893}
1894
1895/* Setup a h/w transmit queue */
1896
1897struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
1898{
1899 struct ath_hal *ah = sc->sc_ah;
Sujithea9880f2008-08-07 10:53:10 +05301900 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001901 int qnum;
1902
Luis R. Rodriguez0345f372008-10-03 15:45:25 -07001903 memset(&qi, 0, sizeof(qi));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001904 qi.tqi_subtype = subtype;
1905 qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
1906 qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
1907 qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
Sujithea9880f2008-08-07 10:53:10 +05301908 qi.tqi_physCompBuf = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001909
1910 /*
1911 * Enable interrupts only for EOL and DESC conditions.
1912 * We mark tx descriptors to receive a DESC interrupt
1913 * when a tx queue gets deep; otherwise waiting for the
1914 * EOL to reap descriptors. Note that this is done to
1915 * reduce interrupt load and this only defers reaping
1916 * descriptors, never transmitting frames. Aside from
1917 * reducing interrupts this also permits more concurrency.
1918 * The only potential downside is if the tx queue backs
1919 * up in which case the top half of the kernel may backup
1920 * due to a lack of tx descriptors.
1921 *
1922 * The UAPSD queue is an exception, since we take a desc-
1923 * based intr on the EOSP frames.
1924 */
1925 if (qtype == ATH9K_TX_QUEUE_UAPSD)
1926 qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1927 else
1928 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
1929 TXQ_FLAG_TXDESCINT_ENABLE;
1930 qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
1931 if (qnum == -1) {
1932 /*
1933 * NB: don't print a message, this happens
1934 * normally on parts with too few tx queues
1935 */
1936 return NULL;
1937 }
Sujithb77f4832008-12-07 21:44:03 +05301938 if (qnum >= ARRAY_SIZE(sc->tx.txq)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001939 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301940 "qnum %u out of range, max %u!\n",
Sujithb77f4832008-12-07 21:44:03 +05301941 qnum, (unsigned int)ARRAY_SIZE(sc->tx.txq));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001942 ath9k_hw_releasetxqueue(ah, qnum);
1943 return NULL;
1944 }
1945 if (!ATH_TXQ_SETUP(sc, qnum)) {
Sujithb77f4832008-12-07 21:44:03 +05301946 struct ath_txq *txq = &sc->tx.txq[qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001947
1948 txq->axq_qnum = qnum;
1949 txq->axq_link = NULL;
1950 INIT_LIST_HEAD(&txq->axq_q);
1951 INIT_LIST_HEAD(&txq->axq_acq);
1952 spin_lock_init(&txq->axq_lock);
1953 txq->axq_depth = 0;
1954 txq->axq_aggr_depth = 0;
1955 txq->axq_totalqueued = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001956 txq->axq_linkbuf = NULL;
Sujithb77f4832008-12-07 21:44:03 +05301957 sc->tx.txqsetup |= 1<<qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001958 }
Sujithb77f4832008-12-07 21:44:03 +05301959 return &sc->tx.txq[qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001960}
1961
1962/* Reclaim resources for a setup queue */
1963
1964void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1965{
1966 ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
Sujithb77f4832008-12-07 21:44:03 +05301967 sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001968}
1969
1970/*
1971 * Setup a hardware data transmit queue for the specified
1972 * access control. The hal may not support all requested
1973 * queues in which case it will return a reference to a
1974 * previously setup queue. We record the mapping from ac's
1975 * to h/w queues for use by ath_tx_start and also track
1976 * the set of h/w queues being used to optimize work in the
1977 * transmit interrupt handler and related routines.
1978 */
1979
1980int ath_tx_setup(struct ath_softc *sc, int haltype)
1981{
1982 struct ath_txq *txq;
1983
Sujithb77f4832008-12-07 21:44:03 +05301984 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001985 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05301986 "HAL AC %u out of range, max %zu!\n",
Sujithb77f4832008-12-07 21:44:03 +05301987 haltype, ARRAY_SIZE(sc->tx.hwq_map));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001988 return 0;
1989 }
1990 txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
1991 if (txq != NULL) {
Sujithb77f4832008-12-07 21:44:03 +05301992 sc->tx.hwq_map[haltype] = txq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001993 return 1;
1994 } else
1995 return 0;
1996}
1997
1998int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
1999{
2000 int qnum;
2001
2002 switch (qtype) {
2003 case ATH9K_TX_QUEUE_DATA:
Sujithb77f4832008-12-07 21:44:03 +05302004 if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002005 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05302006 "HAL AC %u out of range, max %zu!\n",
Sujithb77f4832008-12-07 21:44:03 +05302007 haltype, ARRAY_SIZE(sc->tx.hwq_map));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002008 return -1;
2009 }
Sujithb77f4832008-12-07 21:44:03 +05302010 qnum = sc->tx.hwq_map[haltype];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002011 break;
2012 case ATH9K_TX_QUEUE_BEACON:
Sujithb77f4832008-12-07 21:44:03 +05302013 qnum = sc->beacon.beaconq;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002014 break;
2015 case ATH9K_TX_QUEUE_CAB:
Sujithb77f4832008-12-07 21:44:03 +05302016 qnum = sc->beacon.cabq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002017 break;
2018 default:
2019 qnum = -1;
2020 }
2021 return qnum;
2022}
2023
Sujith528f0c62008-10-29 10:14:26 +05302024/* Get a transmit queue, if available */
2025
2026struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
2027{
2028 struct ath_txq *txq = NULL;
2029 int qnum;
2030
2031 qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
Sujithb77f4832008-12-07 21:44:03 +05302032 txq = &sc->tx.txq[qnum];
Sujith528f0c62008-10-29 10:14:26 +05302033
2034 spin_lock_bh(&txq->axq_lock);
2035
2036 /* Try to avoid running out of descriptors */
2037 if (txq->axq_depth >= (ATH_TXBUF - 20)) {
2038 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05302039 "TX queue: %d is full, depth: %d\n",
2040 qnum, txq->axq_depth);
Sujith528f0c62008-10-29 10:14:26 +05302041 ieee80211_stop_queue(sc->hw, skb_get_queue_mapping(skb));
2042 txq->stopped = 1;
2043 spin_unlock_bh(&txq->axq_lock);
2044 return NULL;
2045 }
2046
2047 spin_unlock_bh(&txq->axq_lock);
2048
2049 return txq;
2050}
2051
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002052/* Update parameters for a transmit queue */
2053
Sujithea9880f2008-08-07 10:53:10 +05302054int ath_txq_update(struct ath_softc *sc, int qnum,
2055 struct ath9k_tx_queue_info *qinfo)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002056{
2057 struct ath_hal *ah = sc->sc_ah;
2058 int error = 0;
Sujithea9880f2008-08-07 10:53:10 +05302059 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002060
Sujithb77f4832008-12-07 21:44:03 +05302061 if (qnum == sc->beacon.beaconq) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002062 /*
2063 * XXX: for beacon queue, we just save the parameter.
2064 * It will be picked up by ath_beaconq_config when
2065 * it's necessary.
2066 */
Sujithb77f4832008-12-07 21:44:03 +05302067 sc->beacon.beacon_qi = *qinfo;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002068 return 0;
2069 }
2070
Sujithb77f4832008-12-07 21:44:03 +05302071 ASSERT(sc->tx.txq[qnum].axq_qnum == qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002072
Sujithea9880f2008-08-07 10:53:10 +05302073 ath9k_hw_get_txq_props(ah, qnum, &qi);
2074 qi.tqi_aifs = qinfo->tqi_aifs;
2075 qi.tqi_cwmin = qinfo->tqi_cwmin;
2076 qi.tqi_cwmax = qinfo->tqi_cwmax;
2077 qi.tqi_burstTime = qinfo->tqi_burstTime;
2078 qi.tqi_readyTime = qinfo->tqi_readyTime;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002079
Sujithea9880f2008-08-07 10:53:10 +05302080 if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002081 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +05302082 "Unable to update hardware queue %u!\n", qnum);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002083 error = -EIO;
2084 } else {
2085 ath9k_hw_resettxqueue(ah, qnum); /* push to h/w */
2086 }
2087
2088 return error;
2089}
2090
2091int ath_cabq_update(struct ath_softc *sc)
2092{
Sujithea9880f2008-08-07 10:53:10 +05302093 struct ath9k_tx_queue_info qi;
Sujithb77f4832008-12-07 21:44:03 +05302094 int qnum = sc->beacon.cabq->axq_qnum;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002095 struct ath_beacon_config conf;
2096
Sujithea9880f2008-08-07 10:53:10 +05302097 ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002098 /*
2099 * Ensure the readytime % is within the bounds.
2100 */
2101 if (sc->sc_config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
2102 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
2103 else if (sc->sc_config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
2104 sc->sc_config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
2105
2106 ath_get_beaconconfig(sc, ATH_IF_ID_ANY, &conf);
2107 qi.tqi_readyTime =
2108 (conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
2109 ath_txq_update(sc, qnum, &qi);
2110
2111 return 0;
2112}
2113
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002114/* Deferred processing of transmit interrupt */
2115
2116void ath_tx_tasklet(struct ath_softc *sc)
2117{
Sujith1fe11322008-08-26 08:11:06 +05302118 int i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002119 u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
2120
2121 ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
2122
2123 /*
2124 * Process each active queue.
2125 */
2126 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2127 if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
Sujithb77f4832008-12-07 21:44:03 +05302128 ath_tx_processq(sc, &sc->tx.txq[i]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002129 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002130}
2131
2132void ath_tx_draintxq(struct ath_softc *sc,
2133 struct ath_txq *txq, bool retry_tx)
2134{
2135 struct ath_buf *bf, *lastbf;
2136 struct list_head bf_head;
2137
2138 INIT_LIST_HEAD(&bf_head);
2139
2140 /*
2141 * NB: this assumes output has been stopped and
2142 * we do not need to block ath_tx_tasklet
2143 */
2144 for (;;) {
2145 spin_lock_bh(&txq->axq_lock);
2146
2147 if (list_empty(&txq->axq_q)) {
2148 txq->axq_link = NULL;
2149 txq->axq_linkbuf = NULL;
2150 spin_unlock_bh(&txq->axq_lock);
2151 break;
2152 }
2153
2154 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2155
2156 if (bf->bf_status & ATH_BUFSTATUS_STALE) {
2157 list_del(&bf->list);
2158 spin_unlock_bh(&txq->axq_lock);
2159
Sujithb77f4832008-12-07 21:44:03 +05302160 spin_lock_bh(&sc->tx.txbuflock);
2161 list_add_tail(&bf->list, &sc->tx.txbuf);
2162 spin_unlock_bh(&sc->tx.txbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002163 continue;
2164 }
2165
2166 lastbf = bf->bf_lastbf;
2167 if (!retry_tx)
2168 lastbf->bf_desc->ds_txstat.ts_flags =
2169 ATH9K_TX_SW_ABORTED;
2170
2171 /* remove ath_buf's of the same mpdu from txq */
2172 list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
2173 txq->axq_depth--;
2174
2175 spin_unlock_bh(&txq->axq_lock);
2176
Sujithcd3d39a2008-08-11 14:03:34 +05302177 if (bf_isampdu(bf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002178 ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, 0);
2179 else
2180 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2181 }
2182
2183 /* flush any pending frames if aggregation is enabled */
Sujith672840a2008-08-11 14:05:08 +05302184 if (sc->sc_flags & SC_OP_TXAGGR) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002185 if (!retry_tx) {
2186 spin_lock_bh(&txq->axq_lock);
Sujithb5aa9bf2008-10-29 10:13:31 +05302187 ath_txq_drain_pending_buffers(sc, txq);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002188 spin_unlock_bh(&txq->axq_lock);
2189 }
2190 }
2191}
2192
2193/* Drain the transmit queues and reclaim resources */
2194
2195void ath_draintxq(struct ath_softc *sc, bool retry_tx)
2196{
2197 /* stop beacon queue. The beacon will be freed when
2198 * we go to INIT state */
Sujith672840a2008-08-11 14:05:08 +05302199 if (!(sc->sc_flags & SC_OP_INVALID)) {
Sujithb77f4832008-12-07 21:44:03 +05302200 (void) ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
Sujith04bd46382008-11-28 22:18:05 +05302201 DPRINTF(sc, ATH_DBG_XMIT, "beacon queue %x\n",
Sujithb77f4832008-12-07 21:44:03 +05302202 ath9k_hw_gettxbuf(sc->sc_ah, sc->beacon.beaconq));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002203 }
2204
2205 ath_drain_txdataq(sc, retry_tx);
2206}
2207
2208u32 ath_txq_depth(struct ath_softc *sc, int qnum)
2209{
Sujithb77f4832008-12-07 21:44:03 +05302210 return sc->tx.txq[qnum].axq_depth;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002211}
2212
2213u32 ath_txq_aggr_depth(struct ath_softc *sc, int qnum)
2214{
Sujithb77f4832008-12-07 21:44:03 +05302215 return sc->tx.txq[qnum].axq_aggr_depth;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002216}
2217
Sujithccc75c52008-10-29 10:18:14 +05302218bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an, u8 tidno)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002219{
2220 struct ath_atx_tid *txtid;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002221
Sujith672840a2008-08-11 14:05:08 +05302222 if (!(sc->sc_flags & SC_OP_TXAGGR))
Sujithccc75c52008-10-29 10:18:14 +05302223 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002224
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002225 txtid = ATH_AN_2_TID(an, tidno);
2226
Sujitha37c2c72008-10-29 10:15:40 +05302227 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
2228 if (!(txtid->state & AGGR_ADDBA_PROGRESS) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002229 (txtid->addba_exchangeattempts < ADDBA_EXCHANGE_ATTEMPTS)) {
2230 txtid->addba_exchangeattempts++;
Sujithccc75c52008-10-29 10:18:14 +05302231 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002232 }
2233 }
2234
Sujithccc75c52008-10-29 10:18:14 +05302235 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002236}
2237
2238/* Start TX aggregation */
2239
Sujithb5aa9bf2008-10-29 10:13:31 +05302240int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
2241 u16 tid, u16 *ssn)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002242{
2243 struct ath_atx_tid *txtid;
2244 struct ath_node *an;
2245
Sujithb5aa9bf2008-10-29 10:13:31 +05302246 an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002247
Sujith672840a2008-08-11 14:05:08 +05302248 if (sc->sc_flags & SC_OP_TXAGGR) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002249 txtid = ATH_AN_2_TID(an, tid);
Sujitha37c2c72008-10-29 10:15:40 +05302250 txtid->state |= AGGR_ADDBA_PROGRESS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002251 ath_tx_pause_tid(sc, txtid);
2252 }
2253
2254 return 0;
2255}
2256
2257/* Stop tx aggregation */
2258
Sujithb5aa9bf2008-10-29 10:13:31 +05302259int ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002260{
Sujithb5aa9bf2008-10-29 10:13:31 +05302261 struct ath_node *an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002262
2263 ath_tx_aggr_teardown(sc, an, tid);
2264 return 0;
2265}
2266
Sujith8469cde2008-10-29 10:19:28 +05302267/* Resume tx aggregation */
2268
2269void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
2270{
2271 struct ath_atx_tid *txtid;
2272 struct ath_node *an;
2273
2274 an = (struct ath_node *)sta->drv_priv;
2275
2276 if (sc->sc_flags & SC_OP_TXAGGR) {
2277 txtid = ATH_AN_2_TID(an, tid);
2278 txtid->baw_size =
2279 IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
2280 txtid->state |= AGGR_ADDBA_COMPLETE;
2281 txtid->state &= ~AGGR_ADDBA_PROGRESS;
2282 ath_tx_resume_tid(sc, txtid);
2283 }
2284}
2285
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002286/*
2287 * Performs transmit side cleanup when TID changes from aggregated to
2288 * unaggregated.
2289 * - Pause the TID and mark cleanup in progress
2290 * - Discard all retry frames from the s/w queue.
2291 */
2292
Sujithb5aa9bf2008-10-29 10:13:31 +05302293void ath_tx_aggr_teardown(struct ath_softc *sc, struct ath_node *an, u8 tid)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002294{
2295 struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
Sujithb77f4832008-12-07 21:44:03 +05302296 struct ath_txq *txq = &sc->tx.txq[txtid->ac->qnum];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002297 struct ath_buf *bf;
2298 struct list_head bf_head;
2299 INIT_LIST_HEAD(&bf_head);
2300
Sujitha37c2c72008-10-29 10:15:40 +05302301 if (txtid->state & AGGR_CLEANUP) /* cleanup is in progress */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002302 return;
2303
Sujitha37c2c72008-10-29 10:15:40 +05302304 if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002305 txtid->addba_exchangeattempts = 0;
2306 return;
2307 }
2308
2309 /* TID must be paused first */
2310 ath_tx_pause_tid(sc, txtid);
2311
2312 /* drop all software retried frames and mark this TID */
2313 spin_lock_bh(&txq->axq_lock);
2314 while (!list_empty(&txtid->buf_q)) {
2315 bf = list_first_entry(&txtid->buf_q, struct ath_buf, list);
Sujithcd3d39a2008-08-11 14:03:34 +05302316 if (!bf_isretried(bf)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002317 /*
2318 * NB: it's based on the assumption that
2319 * software retried frame will always stay
2320 * at the head of software queue.
2321 */
2322 break;
2323 }
2324 list_cut_position(&bf_head,
2325 &txtid->buf_q, &bf->bf_lastfrm->list);
2326 ath_tx_update_baw(sc, txtid, bf->bf_seqno);
2327
2328 /* complete this sub-frame */
2329 ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
2330 }
2331
2332 if (txtid->baw_head != txtid->baw_tail) {
2333 spin_unlock_bh(&txq->axq_lock);
Sujitha37c2c72008-10-29 10:15:40 +05302334 txtid->state |= AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002335 } else {
Sujitha37c2c72008-10-29 10:15:40 +05302336 txtid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002337 txtid->addba_exchangeattempts = 0;
2338 spin_unlock_bh(&txq->axq_lock);
2339 ath_tx_flush_tid(sc, txtid);
2340 }
2341}
2342
2343/*
2344 * Tx scheduling logic
2345 * NB: must be called with txq lock held
2346 */
2347
2348void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
2349{
2350 struct ath_atx_ac *ac;
2351 struct ath_atx_tid *tid;
2352
2353 /* nothing to schedule */
2354 if (list_empty(&txq->axq_acq))
2355 return;
2356 /*
2357 * get the first node/ac pair on the queue
2358 */
2359 ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
2360 list_del(&ac->list);
2361 ac->sched = false;
2362
2363 /*
2364 * process a single tid per destination
2365 */
2366 do {
2367 /* nothing to schedule */
2368 if (list_empty(&ac->tid_q))
2369 return;
2370
2371 tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
2372 list_del(&tid->list);
2373 tid->sched = false;
2374
2375 if (tid->paused) /* check next tid to keep h/w busy */
2376 continue;
2377
Sujith43453b32008-10-29 10:14:52 +05302378 if ((txq->axq_depth % 2) == 0)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002379 ath_tx_sched_aggr(sc, txq, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002380
2381 /*
2382 * add tid to round-robin queue if more frames
2383 * are pending for the tid
2384 */
2385 if (!list_empty(&tid->buf_q))
2386 ath_tx_queue_tid(txq, tid);
2387
2388 /* only schedule one TID at a time */
2389 break;
2390 } while (!list_empty(&ac->tid_q));
2391
2392 /*
2393 * schedule AC if more TIDs need processing
2394 */
2395 if (!list_empty(&ac->tid_q)) {
2396 /*
2397 * add dest ac to txq if not already added
2398 */
2399 if (!ac->sched) {
2400 ac->sched = true;
2401 list_add_tail(&ac->list, &txq->axq_acq);
2402 }
2403 }
2404}
2405
2406/* Initialize per-node transmit state */
2407
2408void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2409{
Sujithc5170162008-10-29 10:13:59 +05302410 struct ath_atx_tid *tid;
2411 struct ath_atx_ac *ac;
2412 int tidno, acno;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002413
Sujithc5170162008-10-29 10:13:59 +05302414 /*
2415 * Init per tid tx state
2416 */
Sujith8ee5afb2008-12-07 21:43:36 +05302417 for (tidno = 0, tid = &an->tid[tidno];
Sujithc5170162008-10-29 10:13:59 +05302418 tidno < WME_NUM_TID;
2419 tidno++, tid++) {
2420 tid->an = an;
2421 tid->tidno = tidno;
2422 tid->seq_start = tid->seq_next = 0;
2423 tid->baw_size = WME_MAX_BA;
2424 tid->baw_head = tid->baw_tail = 0;
2425 tid->sched = false;
2426 tid->paused = false;
Sujitha37c2c72008-10-29 10:15:40 +05302427 tid->state &= ~AGGR_CLEANUP;
Sujithc5170162008-10-29 10:13:59 +05302428 INIT_LIST_HEAD(&tid->buf_q);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002429
Sujithc5170162008-10-29 10:13:59 +05302430 acno = TID_TO_WME_AC(tidno);
Sujith8ee5afb2008-12-07 21:43:36 +05302431 tid->ac = &an->ac[acno];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002432
Sujithc5170162008-10-29 10:13:59 +05302433 /* ADDBA state */
Sujitha37c2c72008-10-29 10:15:40 +05302434 tid->state &= ~AGGR_ADDBA_COMPLETE;
2435 tid->state &= ~AGGR_ADDBA_PROGRESS;
2436 tid->addba_exchangeattempts = 0;
Sujithc5170162008-10-29 10:13:59 +05302437 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002438
Sujithc5170162008-10-29 10:13:59 +05302439 /*
2440 * Init per ac tx state
2441 */
Sujith8ee5afb2008-12-07 21:43:36 +05302442 for (acno = 0, ac = &an->ac[acno];
Sujithc5170162008-10-29 10:13:59 +05302443 acno < WME_NUM_AC; acno++, ac++) {
2444 ac->sched = false;
2445 INIT_LIST_HEAD(&ac->tid_q);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002446
Sujithc5170162008-10-29 10:13:59 +05302447 switch (acno) {
2448 case WME_AC_BE:
2449 ac->qnum = ath_tx_get_qnum(sc,
2450 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
2451 break;
2452 case WME_AC_BK:
2453 ac->qnum = ath_tx_get_qnum(sc,
2454 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
2455 break;
2456 case WME_AC_VI:
2457 ac->qnum = ath_tx_get_qnum(sc,
2458 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
2459 break;
2460 case WME_AC_VO:
2461 ac->qnum = ath_tx_get_qnum(sc,
2462 ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
2463 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002464 }
2465 }
2466}
2467
2468/* Cleanupthe pending buffers for the node. */
2469
Sujithb5aa9bf2008-10-29 10:13:31 +05302470void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002471{
2472 int i;
2473 struct ath_atx_ac *ac, *ac_tmp;
2474 struct ath_atx_tid *tid, *tid_tmp;
2475 struct ath_txq *txq;
2476 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2477 if (ATH_TXQ_SETUP(sc, i)) {
Sujithb77f4832008-12-07 21:44:03 +05302478 txq = &sc->tx.txq[i];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002479
Sujithb5aa9bf2008-10-29 10:13:31 +05302480 spin_lock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002481
2482 list_for_each_entry_safe(ac,
2483 ac_tmp, &txq->axq_acq, list) {
2484 tid = list_first_entry(&ac->tid_q,
2485 struct ath_atx_tid, list);
2486 if (tid && tid->an != an)
2487 continue;
2488 list_del(&ac->list);
2489 ac->sched = false;
2490
2491 list_for_each_entry_safe(tid,
2492 tid_tmp, &ac->tid_q, list) {
2493 list_del(&tid->list);
2494 tid->sched = false;
Sujithb5aa9bf2008-10-29 10:13:31 +05302495 ath_tid_drain(sc, txq, tid);
Sujitha37c2c72008-10-29 10:15:40 +05302496 tid->state &= ~AGGR_ADDBA_COMPLETE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002497 tid->addba_exchangeattempts = 0;
Sujitha37c2c72008-10-29 10:15:40 +05302498 tid->state &= ~AGGR_CLEANUP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002499 }
2500 }
2501
Sujithb5aa9bf2008-10-29 10:13:31 +05302502 spin_unlock(&txq->axq_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002503 }
2504 }
2505}
2506
Jouni Malinene022edb2008-08-22 17:31:33 +03002507void ath_tx_cabq(struct ath_softc *sc, struct sk_buff *skb)
2508{
2509 int hdrlen, padsize;
2510 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2511 struct ath_tx_control txctl;
2512
Sujith528f0c62008-10-29 10:14:26 +05302513 memset(&txctl, 0, sizeof(struct ath_tx_control));
2514
Jouni Malinene022edb2008-08-22 17:31:33 +03002515 /*
2516 * As a temporary workaround, assign seq# here; this will likely need
2517 * to be cleaned up to work better with Beacon transmission and virtual
2518 * BSSes.
2519 */
2520 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
2521 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2522 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
Sujithb77f4832008-12-07 21:44:03 +05302523 sc->tx.seq_no += 0x10;
Jouni Malinene022edb2008-08-22 17:31:33 +03002524 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Sujithb77f4832008-12-07 21:44:03 +05302525 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
Jouni Malinene022edb2008-08-22 17:31:33 +03002526 }
2527
2528 /* Add the padding after the header if this is not already done */
2529 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
2530 if (hdrlen & 3) {
2531 padsize = hdrlen % 4;
2532 if (skb_headroom(skb) < padsize) {
Sujith04bd46382008-11-28 22:18:05 +05302533 DPRINTF(sc, ATH_DBG_XMIT, "TX CABQ padding failed\n");
Jouni Malinene022edb2008-08-22 17:31:33 +03002534 dev_kfree_skb_any(skb);
2535 return;
2536 }
2537 skb_push(skb, padsize);
2538 memmove(skb->data, skb->data + padsize, hdrlen);
2539 }
2540
Sujithb77f4832008-12-07 21:44:03 +05302541 txctl.txq = sc->beacon.cabq;
Sujith528f0c62008-10-29 10:14:26 +05302542
Sujith04bd46382008-11-28 22:18:05 +05302543 DPRINTF(sc, ATH_DBG_XMIT, "transmitting CABQ packet, skb: %p\n", skb);
Jouni Malinene022edb2008-08-22 17:31:33 +03002544
Sujith528f0c62008-10-29 10:14:26 +05302545 if (ath_tx_start(sc, skb, &txctl) != 0) {
Sujith04bd46382008-11-28 22:18:05 +05302546 DPRINTF(sc, ATH_DBG_XMIT, "CABQ TX failed\n");
Sujith528f0c62008-10-29 10:14:26 +05302547 goto exit;
Jouni Malinene022edb2008-08-22 17:31:33 +03002548 }
Jouni Malinene022edb2008-08-22 17:31:33 +03002549
Sujith528f0c62008-10-29 10:14:26 +05302550 return;
2551exit:
2552 dev_kfree_skb_any(skb);
2553}