• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2008-2011 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 
17 #include <linux/dma-mapping.h>
18 #include "ath9k.h"
19 #include "ar9003_mac.h"
20 
21 #define BITS_PER_BYTE           8
22 #define OFDM_PLCP_BITS          22
23 #define HT_RC_2_STREAMS(_rc)    ((((_rc) & 0x78) >> 3) + 1)
24 #define L_STF                   8
25 #define L_LTF                   8
26 #define L_SIG                   4
27 #define HT_SIG                  8
28 #define HT_STF                  4
29 #define HT_LTF(_ns)             (4 * (_ns))
30 #define SYMBOL_TIME(_ns)        ((_ns) << 2) /* ns * 4 us */
31 #define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5)  /* ns * 3.6 us */
32 #define TIME_SYMBOLS(t)         ((t) >> 2)
33 #define TIME_SYMBOLS_HALFGI(t)  (((t) * 5 - 4) / 18)
34 #define NUM_SYMBOLS_PER_USEC(_usec) (_usec >> 2)
35 #define NUM_SYMBOLS_PER_USEC_HALFGI(_usec) (((_usec*5)-4)/18)
36 
37 
38 static u16 bits_per_symbol[][2] = {
39 	/* 20MHz 40MHz */
40 	{    26,   54 },     /*  0: BPSK */
41 	{    52,  108 },     /*  1: QPSK 1/2 */
42 	{    78,  162 },     /*  2: QPSK 3/4 */
43 	{   104,  216 },     /*  3: 16-QAM 1/2 */
44 	{   156,  324 },     /*  4: 16-QAM 3/4 */
45 	{   208,  432 },     /*  5: 64-QAM 2/3 */
46 	{   234,  486 },     /*  6: 64-QAM 3/4 */
47 	{   260,  540 },     /*  7: 64-QAM 5/6 */
48 };
49 
50 static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
51 			       struct ath_atx_tid *tid, struct sk_buff *skb);
52 static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
53 			    int tx_flags, struct ath_txq *txq,
54 			    struct ieee80211_sta *sta);
55 static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
56 				struct ath_txq *txq, struct list_head *bf_q,
57 				struct ieee80211_sta *sta,
58 				struct ath_tx_status *ts, int txok);
59 static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
60 			     struct list_head *head, bool internal);
61 static void ath_tx_rc_status(struct ath_softc *sc, struct ath_buf *bf,
62 			     struct ath_tx_status *ts, int nframes, int nbad,
63 			     int txok);
64 static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
65 			      int seqno);
66 static struct ath_buf *ath_tx_setup_buffer(struct ath_softc *sc,
67 					   struct ath_txq *txq,
68 					   struct ath_atx_tid *tid,
69 					   struct sk_buff *skb);
70 static int ath_tx_prepare(struct ieee80211_hw *hw, struct sk_buff *skb,
71 			  struct ath_tx_control *txctl);
72 
73 enum {
74 	MCS_HT20,
75 	MCS_HT20_SGI,
76 	MCS_HT40,
77 	MCS_HT40_SGI,
78 };
79 
80 /*********************/
81 /* Aggregation logic */
82 /*********************/
83 
ath_tx_status(struct ieee80211_hw * hw,struct sk_buff * skb)84 static void ath_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
85 {
86 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
87 	struct ieee80211_sta *sta = info->status.status_driver_data[0];
88 
89 	if (info->flags & (IEEE80211_TX_CTL_REQ_TX_STATUS |
90 			   IEEE80211_TX_STATUS_EOSP)) {
91 		ieee80211_tx_status(hw, skb);
92 		return;
93 	}
94 
95 	if (sta)
96 		ieee80211_tx_status_noskb(hw, sta, info);
97 
98 	dev_kfree_skb(skb);
99 }
100 
ath_txq_unlock_complete(struct ath_softc * sc,struct ath_txq * txq)101 void ath_txq_unlock_complete(struct ath_softc *sc, struct ath_txq *txq)
102 	__releases(&txq->axq_lock)
103 {
104 	struct ieee80211_hw *hw = sc->hw;
105 	struct sk_buff_head q;
106 	struct sk_buff *skb;
107 
108 	__skb_queue_head_init(&q);
109 	skb_queue_splice_init(&txq->complete_q, &q);
110 	spin_unlock_bh(&txq->axq_lock);
111 
112 	while ((skb = __skb_dequeue(&q)))
113 		ath_tx_status(hw, skb);
114 }
115 
__ath_tx_queue_tid(struct ath_softc * sc,struct ath_atx_tid * tid)116 void __ath_tx_queue_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
117 {
118 	struct ath_vif *avp = (struct ath_vif *) tid->an->vif->drv_priv;
119 	struct ath_chanctx *ctx = avp->chanctx;
120 	struct ath_acq *acq;
121 	struct list_head *tid_list;
122 	u8 acno = TID_TO_WME_AC(tid->tidno);
123 
124 	if (!ctx || !list_empty(&tid->list))
125 		return;
126 
127 
128 	acq = &ctx->acq[acno];
129 	if ((sc->airtime_flags & AIRTIME_USE_NEW_QUEUES) &&
130 	    tid->an->airtime_deficit[acno] > 0)
131 		tid_list = &acq->acq_new;
132 	else
133 		tid_list = &acq->acq_old;
134 
135 	list_add_tail(&tid->list, tid_list);
136 }
137 
ath_tx_queue_tid(struct ath_softc * sc,struct ath_atx_tid * tid)138 void ath_tx_queue_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
139 {
140 	struct ath_vif *avp = (struct ath_vif *) tid->an->vif->drv_priv;
141 	struct ath_chanctx *ctx = avp->chanctx;
142 	struct ath_acq *acq;
143 
144 	if (!ctx || !list_empty(&tid->list))
145 		return;
146 
147 	acq = &ctx->acq[TID_TO_WME_AC(tid->tidno)];
148 	spin_lock_bh(&acq->lock);
149 	__ath_tx_queue_tid(sc, tid);
150 	spin_unlock_bh(&acq->lock);
151 }
152 
153 
ath9k_wake_tx_queue(struct ieee80211_hw * hw,struct ieee80211_txq * queue)154 void ath9k_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *queue)
155 {
156 	struct ath_softc *sc = hw->priv;
157 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
158 	struct ath_atx_tid *tid = (struct ath_atx_tid *) queue->drv_priv;
159 	struct ath_txq *txq = tid->txq;
160 
161 	ath_dbg(common, QUEUE, "Waking TX queue: %pM (%d)\n",
162 		queue->sta ? queue->sta->addr : queue->vif->addr,
163 		tid->tidno);
164 
165 	ath_txq_lock(sc, txq);
166 
167 	tid->has_queued = true;
168 	ath_tx_queue_tid(sc, tid);
169 	ath_txq_schedule(sc, txq);
170 
171 	ath_txq_unlock(sc, txq);
172 }
173 
get_frame_info(struct sk_buff * skb)174 static struct ath_frame_info *get_frame_info(struct sk_buff *skb)
175 {
176 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
177 	BUILD_BUG_ON(sizeof(struct ath_frame_info) >
178 		     sizeof(tx_info->rate_driver_data));
179 	return (struct ath_frame_info *) &tx_info->rate_driver_data[0];
180 }
181 
ath_send_bar(struct ath_atx_tid * tid,u16 seqno)182 static void ath_send_bar(struct ath_atx_tid *tid, u16 seqno)
183 {
184 	if (!tid->an->sta)
185 		return;
186 
187 	ieee80211_send_bar(tid->an->vif, tid->an->sta->addr, tid->tidno,
188 			   seqno << IEEE80211_SEQ_SEQ_SHIFT);
189 }
190 
ath_set_rates(struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ath_buf * bf)191 static void ath_set_rates(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
192 			  struct ath_buf *bf)
193 {
194 	ieee80211_get_tx_rates(vif, sta, bf->bf_mpdu, bf->rates,
195 			       ARRAY_SIZE(bf->rates));
196 }
197 
ath_txq_skb_done(struct ath_softc * sc,struct ath_txq * txq,struct sk_buff * skb)198 static void ath_txq_skb_done(struct ath_softc *sc, struct ath_txq *txq,
199 			     struct sk_buff *skb)
200 {
201 	struct ath_frame_info *fi = get_frame_info(skb);
202 	int q = fi->txq;
203 
204 	if (q < 0)
205 		return;
206 
207 	txq = sc->tx.txq_map[q];
208 	if (WARN_ON(--txq->pending_frames < 0))
209 		txq->pending_frames = 0;
210 
211 }
212 
213 static struct ath_atx_tid *
ath_get_skb_tid(struct ath_softc * sc,struct ath_node * an,struct sk_buff * skb)214 ath_get_skb_tid(struct ath_softc *sc, struct ath_node *an, struct sk_buff *skb)
215 {
216 	u8 tidno = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
217 	return ATH_AN_2_TID(an, tidno);
218 }
219 
220 static struct sk_buff *
ath_tid_pull(struct ath_atx_tid * tid)221 ath_tid_pull(struct ath_atx_tid *tid)
222 {
223 	struct ieee80211_txq *txq = container_of((void*)tid, struct ieee80211_txq, drv_priv);
224 	struct ath_softc *sc = tid->an->sc;
225 	struct ieee80211_hw *hw = sc->hw;
226 	struct ath_tx_control txctl = {
227 		.txq = tid->txq,
228 		.sta = tid->an->sta,
229 	};
230 	struct sk_buff *skb;
231 	struct ath_frame_info *fi;
232 	int q;
233 
234 	if (!tid->has_queued)
235 		return NULL;
236 
237 	skb = ieee80211_tx_dequeue(hw, txq);
238 	if (!skb) {
239 		tid->has_queued = false;
240 		return NULL;
241 	}
242 
243 	if (ath_tx_prepare(hw, skb, &txctl)) {
244 		ieee80211_free_txskb(hw, skb);
245 		return NULL;
246 	}
247 
248 	q = skb_get_queue_mapping(skb);
249 	if (tid->txq == sc->tx.txq_map[q]) {
250 		fi = get_frame_info(skb);
251 		fi->txq = q;
252 		++tid->txq->pending_frames;
253 	}
254 
255 	return skb;
256  }
257 
258 
ath_tid_has_buffered(struct ath_atx_tid * tid)259 static bool ath_tid_has_buffered(struct ath_atx_tid *tid)
260 {
261 	return !skb_queue_empty(&tid->retry_q) || tid->has_queued;
262 }
263 
ath_tid_dequeue(struct ath_atx_tid * tid)264 static struct sk_buff *ath_tid_dequeue(struct ath_atx_tid *tid)
265 {
266 	struct sk_buff *skb;
267 
268 	skb = __skb_dequeue(&tid->retry_q);
269 	if (!skb)
270 		skb = ath_tid_pull(tid);
271 
272 	return skb;
273 }
274 
ath_tx_flush_tid(struct ath_softc * sc,struct ath_atx_tid * tid)275 static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
276 {
277 	struct ath_txq *txq = tid->txq;
278 	struct sk_buff *skb;
279 	struct ath_buf *bf;
280 	struct list_head bf_head;
281 	struct ath_tx_status ts;
282 	struct ath_frame_info *fi;
283 	bool sendbar = false;
284 
285 	INIT_LIST_HEAD(&bf_head);
286 
287 	memset(&ts, 0, sizeof(ts));
288 
289 	while ((skb = __skb_dequeue(&tid->retry_q))) {
290 		fi = get_frame_info(skb);
291 		bf = fi->bf;
292 		if (!bf) {
293 			ath_txq_skb_done(sc, txq, skb);
294 			ieee80211_free_txskb(sc->hw, skb);
295 			continue;
296 		}
297 
298 		if (fi->baw_tracked) {
299 			ath_tx_update_baw(sc, tid, bf->bf_state.seqno);
300 			sendbar = true;
301 		}
302 
303 		list_add_tail(&bf->list, &bf_head);
304 		ath_tx_complete_buf(sc, bf, txq, &bf_head, NULL, &ts, 0);
305 	}
306 
307 	if (sendbar) {
308 		ath_txq_unlock(sc, txq);
309 		ath_send_bar(tid, tid->seq_start);
310 		ath_txq_lock(sc, txq);
311 	}
312 }
313 
ath_tx_update_baw(struct ath_softc * sc,struct ath_atx_tid * tid,int seqno)314 static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
315 			      int seqno)
316 {
317 	int index, cindex;
318 
319 	index  = ATH_BA_INDEX(tid->seq_start, seqno);
320 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
321 
322 	__clear_bit(cindex, tid->tx_buf);
323 
324 	while (tid->baw_head != tid->baw_tail && !test_bit(tid->baw_head, tid->tx_buf)) {
325 		INCR(tid->seq_start, IEEE80211_SEQ_MAX);
326 		INCR(tid->baw_head, ATH_TID_MAX_BUFS);
327 		if (tid->bar_index >= 0)
328 			tid->bar_index--;
329 	}
330 }
331 
ath_tx_addto_baw(struct ath_softc * sc,struct ath_atx_tid * tid,struct ath_buf * bf)332 static void ath_tx_addto_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
333 			     struct ath_buf *bf)
334 {
335 	struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
336 	u16 seqno = bf->bf_state.seqno;
337 	int index, cindex;
338 
339 	index  = ATH_BA_INDEX(tid->seq_start, seqno);
340 	cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
341 	__set_bit(cindex, tid->tx_buf);
342 	fi->baw_tracked = 1;
343 
344 	if (index >= ((tid->baw_tail - tid->baw_head) &
345 		(ATH_TID_MAX_BUFS - 1))) {
346 		tid->baw_tail = cindex;
347 		INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
348 	}
349 }
350 
ath_tid_drain(struct ath_softc * sc,struct ath_txq * txq,struct ath_atx_tid * tid)351 static void ath_tid_drain(struct ath_softc *sc, struct ath_txq *txq,
352 			  struct ath_atx_tid *tid)
353 
354 {
355 	struct sk_buff *skb;
356 	struct ath_buf *bf;
357 	struct list_head bf_head;
358 	struct ath_tx_status ts;
359 	struct ath_frame_info *fi;
360 
361 	memset(&ts, 0, sizeof(ts));
362 	INIT_LIST_HEAD(&bf_head);
363 
364 	while ((skb = ath_tid_dequeue(tid))) {
365 		fi = get_frame_info(skb);
366 		bf = fi->bf;
367 
368 		if (!bf) {
369 			ath_tx_complete(sc, skb, ATH_TX_ERROR, txq, NULL);
370 			continue;
371 		}
372 
373 		list_add_tail(&bf->list, &bf_head);
374 		ath_tx_complete_buf(sc, bf, txq, &bf_head, NULL, &ts, 0);
375 	}
376 }
377 
ath_tx_set_retry(struct ath_softc * sc,struct ath_txq * txq,struct sk_buff * skb,int count)378 static void ath_tx_set_retry(struct ath_softc *sc, struct ath_txq *txq,
379 			     struct sk_buff *skb, int count)
380 {
381 	struct ath_frame_info *fi = get_frame_info(skb);
382 	struct ath_buf *bf = fi->bf;
383 	struct ieee80211_hdr *hdr;
384 	int prev = fi->retries;
385 
386 	TX_STAT_INC(txq->axq_qnum, a_retries);
387 	fi->retries += count;
388 
389 	if (prev > 0)
390 		return;
391 
392 	hdr = (struct ieee80211_hdr *)skb->data;
393 	hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
394 	dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
395 		sizeof(*hdr), DMA_TO_DEVICE);
396 }
397 
ath_tx_get_buffer(struct ath_softc * sc)398 static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
399 {
400 	struct ath_buf *bf = NULL;
401 
402 	spin_lock_bh(&sc->tx.txbuflock);
403 
404 	if (unlikely(list_empty(&sc->tx.txbuf))) {
405 		spin_unlock_bh(&sc->tx.txbuflock);
406 		return NULL;
407 	}
408 
409 	bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
410 	list_del(&bf->list);
411 
412 	spin_unlock_bh(&sc->tx.txbuflock);
413 
414 	return bf;
415 }
416 
ath_tx_return_buffer(struct ath_softc * sc,struct ath_buf * bf)417 static void ath_tx_return_buffer(struct ath_softc *sc, struct ath_buf *bf)
418 {
419 	spin_lock_bh(&sc->tx.txbuflock);
420 	list_add_tail(&bf->list, &sc->tx.txbuf);
421 	spin_unlock_bh(&sc->tx.txbuflock);
422 }
423 
ath_clone_txbuf(struct ath_softc * sc,struct ath_buf * bf)424 static struct ath_buf* ath_clone_txbuf(struct ath_softc *sc, struct ath_buf *bf)
425 {
426 	struct ath_buf *tbf;
427 
428 	tbf = ath_tx_get_buffer(sc);
429 	if (WARN_ON(!tbf))
430 		return NULL;
431 
432 	ATH_TXBUF_RESET(tbf);
433 
434 	tbf->bf_mpdu = bf->bf_mpdu;
435 	tbf->bf_buf_addr = bf->bf_buf_addr;
436 	memcpy(tbf->bf_desc, bf->bf_desc, sc->sc_ah->caps.tx_desc_len);
437 	tbf->bf_state = bf->bf_state;
438 	tbf->bf_state.stale = false;
439 
440 	return tbf;
441 }
442 
ath_tx_count_frames(struct ath_softc * sc,struct ath_buf * bf,struct ath_tx_status * ts,int txok,int * nframes,int * nbad)443 static void ath_tx_count_frames(struct ath_softc *sc, struct ath_buf *bf,
444 			        struct ath_tx_status *ts, int txok,
445 			        int *nframes, int *nbad)
446 {
447 	struct ath_frame_info *fi;
448 	u16 seq_st = 0;
449 	u32 ba[WME_BA_BMP_SIZE >> 5];
450 	int ba_index;
451 	int isaggr = 0;
452 
453 	*nbad = 0;
454 	*nframes = 0;
455 
456 	isaggr = bf_isaggr(bf);
457 	if (isaggr) {
458 		seq_st = ts->ts_seqnum;
459 		memcpy(ba, &ts->ba_low, WME_BA_BMP_SIZE >> 3);
460 	}
461 
462 	while (bf) {
463 		fi = get_frame_info(bf->bf_mpdu);
464 		ba_index = ATH_BA_INDEX(seq_st, bf->bf_state.seqno);
465 
466 		(*nframes)++;
467 		if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
468 			(*nbad)++;
469 
470 		bf = bf->bf_next;
471 	}
472 }
473 
474 
ath_tx_complete_aggr(struct ath_softc * sc,struct ath_txq * txq,struct ath_buf * bf,struct list_head * bf_q,struct ieee80211_sta * sta,struct ath_atx_tid * tid,struct ath_tx_status * ts,int txok)475 static void ath_tx_complete_aggr(struct ath_softc *sc, struct ath_txq *txq,
476 				 struct ath_buf *bf, struct list_head *bf_q,
477 				 struct ieee80211_sta *sta,
478 				 struct ath_atx_tid *tid,
479 				 struct ath_tx_status *ts, int txok)
480 {
481 	struct ath_node *an = NULL;
482 	struct sk_buff *skb;
483 	struct ieee80211_hdr *hdr;
484 	struct ieee80211_tx_info *tx_info;
485 	struct ath_buf *bf_next, *bf_last = bf->bf_lastbf;
486 	struct list_head bf_head;
487 	struct sk_buff_head bf_pending;
488 	u16 seq_st = 0, acked_cnt = 0, txfail_cnt = 0, seq_first;
489 	u32 ba[WME_BA_BMP_SIZE >> 5];
490 	int isaggr, txfail, txpending, sendbar = 0, needreset = 0, nbad = 0;
491 	bool rc_update = true, isba;
492 	struct ieee80211_tx_rate rates[4];
493 	struct ath_frame_info *fi;
494 	int nframes;
495 	bool flush = !!(ts->ts_status & ATH9K_TX_FLUSH);
496 	int i, retries;
497 	int bar_index = -1;
498 
499 	skb = bf->bf_mpdu;
500 	hdr = (struct ieee80211_hdr *)skb->data;
501 
502 	tx_info = IEEE80211_SKB_CB(skb);
503 
504 	memcpy(rates, bf->rates, sizeof(rates));
505 
506 	retries = ts->ts_longretry + 1;
507 	for (i = 0; i < ts->ts_rateindex; i++)
508 		retries += rates[i].count;
509 
510 	if (!sta) {
511 		INIT_LIST_HEAD(&bf_head);
512 		while (bf) {
513 			bf_next = bf->bf_next;
514 
515 			if (!bf->bf_state.stale || bf_next != NULL)
516 				list_move_tail(&bf->list, &bf_head);
517 
518 			ath_tx_complete_buf(sc, bf, txq, &bf_head, NULL, ts, 0);
519 
520 			bf = bf_next;
521 		}
522 		return;
523 	}
524 
525 	an = (struct ath_node *)sta->drv_priv;
526 	seq_first = tid->seq_start;
527 	isba = ts->ts_flags & ATH9K_TX_BA;
528 
529 	/*
530 	 * The hardware occasionally sends a tx status for the wrong TID.
531 	 * In this case, the BA status cannot be considered valid and all
532 	 * subframes need to be retransmitted
533 	 *
534 	 * Only BlockAcks have a TID and therefore normal Acks cannot be
535 	 * checked
536 	 */
537 	if (isba && tid->tidno != ts->tid)
538 		txok = false;
539 
540 	isaggr = bf_isaggr(bf);
541 	memset(ba, 0, WME_BA_BMP_SIZE >> 3);
542 
543 	if (isaggr && txok) {
544 		if (ts->ts_flags & ATH9K_TX_BA) {
545 			seq_st = ts->ts_seqnum;
546 			memcpy(ba, &ts->ba_low, WME_BA_BMP_SIZE >> 3);
547 		} else {
548 			/*
549 			 * AR5416 can become deaf/mute when BA
550 			 * issue happens. Chip needs to be reset.
551 			 * But AP code may have sychronization issues
552 			 * when perform internal reset in this routine.
553 			 * Only enable reset in STA mode for now.
554 			 */
555 			if (sc->sc_ah->opmode == NL80211_IFTYPE_STATION)
556 				needreset = 1;
557 		}
558 	}
559 
560 	__skb_queue_head_init(&bf_pending);
561 
562 	ath_tx_count_frames(sc, bf, ts, txok, &nframes, &nbad);
563 	while (bf) {
564 		u16 seqno = bf->bf_state.seqno;
565 
566 		txfail = txpending = sendbar = 0;
567 		bf_next = bf->bf_next;
568 
569 		skb = bf->bf_mpdu;
570 		tx_info = IEEE80211_SKB_CB(skb);
571 		fi = get_frame_info(skb);
572 
573 		if (!BAW_WITHIN(tid->seq_start, tid->baw_size, seqno) ||
574 		    !tid->active) {
575 			/*
576 			 * Outside of the current BlockAck window,
577 			 * maybe part of a previous session
578 			 */
579 			txfail = 1;
580 		} else if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, seqno))) {
581 			/* transmit completion, subframe is
582 			 * acked by block ack */
583 			acked_cnt++;
584 		} else if (!isaggr && txok) {
585 			/* transmit completion */
586 			acked_cnt++;
587 		} else if (flush) {
588 			txpending = 1;
589 		} else if (fi->retries < ATH_MAX_SW_RETRIES) {
590 			if (txok || !an->sleeping)
591 				ath_tx_set_retry(sc, txq, bf->bf_mpdu,
592 						 retries);
593 
594 			txpending = 1;
595 		} else {
596 			txfail = 1;
597 			txfail_cnt++;
598 			bar_index = max_t(int, bar_index,
599 				ATH_BA_INDEX(seq_first, seqno));
600 		}
601 
602 		/*
603 		 * Make sure the last desc is reclaimed if it
604 		 * not a holding desc.
605 		 */
606 		INIT_LIST_HEAD(&bf_head);
607 		if (bf_next != NULL || !bf_last->bf_state.stale)
608 			list_move_tail(&bf->list, &bf_head);
609 
610 		if (!txpending) {
611 			/*
612 			 * complete the acked-ones/xretried ones; update
613 			 * block-ack window
614 			 */
615 			ath_tx_update_baw(sc, tid, seqno);
616 
617 			if (rc_update && (acked_cnt == 1 || txfail_cnt == 1)) {
618 				memcpy(tx_info->control.rates, rates, sizeof(rates));
619 				ath_tx_rc_status(sc, bf, ts, nframes, nbad, txok);
620 				rc_update = false;
621 				if (bf == bf->bf_lastbf)
622 					ath_dynack_sample_tx_ts(sc->sc_ah,
623 								bf->bf_mpdu,
624 								ts, sta);
625 			}
626 
627 			ath_tx_complete_buf(sc, bf, txq, &bf_head, sta, ts,
628 				!txfail);
629 		} else {
630 			if (tx_info->flags & IEEE80211_TX_STATUS_EOSP) {
631 				tx_info->flags &= ~IEEE80211_TX_STATUS_EOSP;
632 				ieee80211_sta_eosp(sta);
633 			}
634 			/* retry the un-acked ones */
635 			if (bf->bf_next == NULL && bf_last->bf_state.stale) {
636 				struct ath_buf *tbf;
637 
638 				tbf = ath_clone_txbuf(sc, bf_last);
639 				/*
640 				 * Update tx baw and complete the
641 				 * frame with failed status if we
642 				 * run out of tx buf.
643 				 */
644 				if (!tbf) {
645 					ath_tx_update_baw(sc, tid, seqno);
646 
647 					ath_tx_complete_buf(sc, bf, txq,
648 							    &bf_head, NULL, ts,
649 							    0);
650 					bar_index = max_t(int, bar_index,
651 						ATH_BA_INDEX(seq_first, seqno));
652 					break;
653 				}
654 
655 				fi->bf = tbf;
656 			}
657 
658 			/*
659 			 * Put this buffer to the temporary pending
660 			 * queue to retain ordering
661 			 */
662 			__skb_queue_tail(&bf_pending, skb);
663 		}
664 
665 		bf = bf_next;
666 	}
667 
668 	/* prepend un-acked frames to the beginning of the pending frame queue */
669 	if (!skb_queue_empty(&bf_pending)) {
670 		if (an->sleeping)
671 			ieee80211_sta_set_buffered(sta, tid->tidno, true);
672 
673 		skb_queue_splice_tail(&bf_pending, &tid->retry_q);
674 		if (!an->sleeping) {
675 			ath_tx_queue_tid(sc, tid);
676 
677 			if (ts->ts_status & (ATH9K_TXERR_FILT | ATH9K_TXERR_XRETRY))
678 				tid->clear_ps_filter = true;
679 		}
680 	}
681 
682 	if (bar_index >= 0) {
683 		u16 bar_seq = ATH_BA_INDEX2SEQ(seq_first, bar_index);
684 
685 		if (BAW_WITHIN(tid->seq_start, tid->baw_size, bar_seq))
686 			tid->bar_index = ATH_BA_INDEX(tid->seq_start, bar_seq);
687 
688 		ath_txq_unlock(sc, txq);
689 		ath_send_bar(tid, ATH_BA_INDEX2SEQ(seq_first, bar_index + 1));
690 		ath_txq_lock(sc, txq);
691 	}
692 
693 	if (needreset)
694 		ath9k_queue_reset(sc, RESET_TYPE_TX_ERROR);
695 }
696 
bf_is_ampdu_not_probing(struct ath_buf * bf)697 static bool bf_is_ampdu_not_probing(struct ath_buf *bf)
698 {
699     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(bf->bf_mpdu);
700     return bf_isampdu(bf) && !(info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE);
701 }
702 
ath_tx_count_airtime(struct ath_softc * sc,struct ath_node * an,struct ath_atx_tid * tid,struct ath_buf * bf,struct ath_tx_status * ts)703 static void ath_tx_count_airtime(struct ath_softc *sc, struct ath_node *an,
704 				 struct ath_atx_tid *tid, struct ath_buf *bf,
705 				 struct ath_tx_status *ts)
706 {
707 	struct ath_txq *txq = tid->txq;
708 	u32 airtime = 0;
709 	int i;
710 
711 	airtime += ts->duration * (ts->ts_longretry + 1);
712 	for(i = 0; i < ts->ts_rateindex; i++) {
713 		int rate_dur = ath9k_hw_get_duration(sc->sc_ah, bf->bf_desc, i);
714 		airtime += rate_dur * bf->rates[i].count;
715 	}
716 
717 	if (sc->airtime_flags & AIRTIME_USE_TX) {
718 		int q = txq->mac80211_qnum;
719 		struct ath_acq *acq = &sc->cur_chan->acq[q];
720 
721 		spin_lock_bh(&acq->lock);
722 		an->airtime_deficit[q] -= airtime;
723 		if (an->airtime_deficit[q] <= 0)
724 			__ath_tx_queue_tid(sc, tid);
725 		spin_unlock_bh(&acq->lock);
726 	}
727 	ath_debug_airtime(sc, an, 0, airtime);
728 }
729 
ath_tx_process_buffer(struct ath_softc * sc,struct ath_txq * txq,struct ath_tx_status * ts,struct ath_buf * bf,struct list_head * bf_head)730 static void ath_tx_process_buffer(struct ath_softc *sc, struct ath_txq *txq,
731 				  struct ath_tx_status *ts, struct ath_buf *bf,
732 				  struct list_head *bf_head)
733 {
734 	struct ieee80211_hw *hw = sc->hw;
735 	struct ieee80211_tx_info *info;
736 	struct ieee80211_sta *sta;
737 	struct ieee80211_hdr *hdr;
738 	struct ath_atx_tid *tid = NULL;
739 	bool txok, flush;
740 
741 	txok = !(ts->ts_status & ATH9K_TXERR_MASK);
742 	flush = !!(ts->ts_status & ATH9K_TX_FLUSH);
743 	txq->axq_tx_inprogress = false;
744 
745 	txq->axq_depth--;
746 	if (bf_is_ampdu_not_probing(bf))
747 		txq->axq_ampdu_depth--;
748 
749 	ts->duration = ath9k_hw_get_duration(sc->sc_ah, bf->bf_desc,
750 					     ts->ts_rateindex);
751 
752 	hdr = (struct ieee80211_hdr *) bf->bf_mpdu->data;
753 	sta = ieee80211_find_sta_by_ifaddr(hw, hdr->addr1, hdr->addr2);
754 	if (sta) {
755 		struct ath_node *an = (struct ath_node *)sta->drv_priv;
756 		tid = ath_get_skb_tid(sc, an, bf->bf_mpdu);
757 		ath_tx_count_airtime(sc, an, tid, bf, ts);
758 		if (ts->ts_status & (ATH9K_TXERR_FILT | ATH9K_TXERR_XRETRY))
759 			tid->clear_ps_filter = true;
760 	}
761 
762 	if (!bf_isampdu(bf)) {
763 		if (!flush) {
764 			info = IEEE80211_SKB_CB(bf->bf_mpdu);
765 			memcpy(info->control.rates, bf->rates,
766 			       sizeof(info->control.rates));
767 			ath_tx_rc_status(sc, bf, ts, 1, txok ? 0 : 1, txok);
768 			ath_dynack_sample_tx_ts(sc->sc_ah, bf->bf_mpdu, ts,
769 						sta);
770 		}
771 		ath_tx_complete_buf(sc, bf, txq, bf_head, sta, ts, txok);
772 	} else
773 		ath_tx_complete_aggr(sc, txq, bf, bf_head, sta, tid, ts, txok);
774 
775 	if (!flush)
776 		ath_txq_schedule(sc, txq);
777 }
778 
ath_lookup_legacy(struct ath_buf * bf)779 static bool ath_lookup_legacy(struct ath_buf *bf)
780 {
781 	struct sk_buff *skb;
782 	struct ieee80211_tx_info *tx_info;
783 	struct ieee80211_tx_rate *rates;
784 	int i;
785 
786 	skb = bf->bf_mpdu;
787 	tx_info = IEEE80211_SKB_CB(skb);
788 	rates = tx_info->control.rates;
789 
790 	for (i = 0; i < 4; i++) {
791 		if (!rates[i].count || rates[i].idx < 0)
792 			break;
793 
794 		if (!(rates[i].flags & IEEE80211_TX_RC_MCS))
795 			return true;
796 	}
797 
798 	return false;
799 }
800 
ath_lookup_rate(struct ath_softc * sc,struct ath_buf * bf,struct ath_atx_tid * tid)801 static u32 ath_lookup_rate(struct ath_softc *sc, struct ath_buf *bf,
802 			   struct ath_atx_tid *tid)
803 {
804 	struct sk_buff *skb;
805 	struct ieee80211_tx_info *tx_info;
806 	struct ieee80211_tx_rate *rates;
807 	u32 max_4ms_framelen, frmlen;
808 	u16 aggr_limit, bt_aggr_limit, legacy = 0;
809 	int q = tid->txq->mac80211_qnum;
810 	int i;
811 
812 	skb = bf->bf_mpdu;
813 	tx_info = IEEE80211_SKB_CB(skb);
814 	rates = bf->rates;
815 
816 	/*
817 	 * Find the lowest frame length among the rate series that will have a
818 	 * 4ms (or TXOP limited) transmit duration.
819 	 */
820 	max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
821 
822 	for (i = 0; i < 4; i++) {
823 		int modeidx;
824 
825 		if (!rates[i].count)
826 			continue;
827 
828 		if (!(rates[i].flags & IEEE80211_TX_RC_MCS)) {
829 			legacy = 1;
830 			break;
831 		}
832 
833 		if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
834 			modeidx = MCS_HT40;
835 		else
836 			modeidx = MCS_HT20;
837 
838 		if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
839 			modeidx++;
840 
841 		frmlen = sc->tx.max_aggr_framelen[q][modeidx][rates[i].idx];
842 		max_4ms_framelen = min(max_4ms_framelen, frmlen);
843 	}
844 
845 	/*
846 	 * limit aggregate size by the minimum rate if rate selected is
847 	 * not a probe rate, if rate selected is a probe rate then
848 	 * avoid aggregation of this packet.
849 	 */
850 	if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
851 		return 0;
852 
853 	aggr_limit = min(max_4ms_framelen, (u32)ATH_AMPDU_LIMIT_MAX);
854 
855 	/*
856 	 * Override the default aggregation limit for BTCOEX.
857 	 */
858 	bt_aggr_limit = ath9k_btcoex_aggr_limit(sc, max_4ms_framelen);
859 	if (bt_aggr_limit)
860 		aggr_limit = bt_aggr_limit;
861 
862 	if (tid->an->maxampdu)
863 		aggr_limit = min(aggr_limit, tid->an->maxampdu);
864 
865 	return aggr_limit;
866 }
867 
868 /*
869  * Returns the number of delimiters to be added to
870  * meet the minimum required mpdudensity.
871  */
ath_compute_num_delims(struct ath_softc * sc,struct ath_atx_tid * tid,struct ath_buf * bf,u16 frmlen,bool first_subfrm)872 static int ath_compute_num_delims(struct ath_softc *sc, struct ath_atx_tid *tid,
873 				  struct ath_buf *bf, u16 frmlen,
874 				  bool first_subfrm)
875 {
876 #define FIRST_DESC_NDELIMS 60
877 	u32 nsymbits, nsymbols;
878 	u16 minlen;
879 	u8 flags, rix;
880 	int width, streams, half_gi, ndelim, mindelim;
881 	struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
882 
883 	/* Select standard number of delimiters based on frame length alone */
884 	ndelim = ATH_AGGR_GET_NDELIM(frmlen);
885 
886 	/*
887 	 * If encryption enabled, hardware requires some more padding between
888 	 * subframes.
889 	 * TODO - this could be improved to be dependent on the rate.
890 	 *      The hardware can keep up at lower rates, but not higher rates
891 	 */
892 	if ((fi->keyix != ATH9K_TXKEYIX_INVALID) &&
893 	    !(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA))
894 		ndelim += ATH_AGGR_ENCRYPTDELIM;
895 
896 	/*
897 	 * Add delimiter when using RTS/CTS with aggregation
898 	 * and non enterprise AR9003 card
899 	 */
900 	if (first_subfrm && !AR_SREV_9580_10_OR_LATER(sc->sc_ah) &&
901 	    (sc->sc_ah->ent_mode & AR_ENT_OTP_MIN_PKT_SIZE_DISABLE))
902 		ndelim = max(ndelim, FIRST_DESC_NDELIMS);
903 
904 	/*
905 	 * Convert desired mpdu density from microeconds to bytes based
906 	 * on highest rate in rate series (i.e. first rate) to determine
907 	 * required minimum length for subframe. Take into account
908 	 * whether high rate is 20 or 40Mhz and half or full GI.
909 	 *
910 	 * If there is no mpdu density restriction, no further calculation
911 	 * is needed.
912 	 */
913 
914 	if (tid->an->mpdudensity == 0)
915 		return ndelim;
916 
917 	rix = bf->rates[0].idx;
918 	flags = bf->rates[0].flags;
919 	width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
920 	half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
921 
922 	if (half_gi)
923 		nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(tid->an->mpdudensity);
924 	else
925 		nsymbols = NUM_SYMBOLS_PER_USEC(tid->an->mpdudensity);
926 
927 	if (nsymbols == 0)
928 		nsymbols = 1;
929 
930 	streams = HT_RC_2_STREAMS(rix);
931 	nsymbits = bits_per_symbol[rix % 8][width] * streams;
932 	minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
933 
934 	if (frmlen < minlen) {
935 		mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
936 		ndelim = max(mindelim, ndelim);
937 	}
938 
939 	return ndelim;
940 }
941 
942 static struct ath_buf *
ath_tx_get_tid_subframe(struct ath_softc * sc,struct ath_txq * txq,struct ath_atx_tid * tid)943 ath_tx_get_tid_subframe(struct ath_softc *sc, struct ath_txq *txq,
944 			struct ath_atx_tid *tid)
945 {
946 	struct ieee80211_tx_info *tx_info;
947 	struct ath_frame_info *fi;
948 	struct sk_buff *skb, *first_skb = NULL;
949 	struct ath_buf *bf;
950 	u16 seqno;
951 
952 	while (1) {
953 		skb = ath_tid_dequeue(tid);
954 		if (!skb)
955 			break;
956 
957 		fi = get_frame_info(skb);
958 		bf = fi->bf;
959 		if (!fi->bf)
960 			bf = ath_tx_setup_buffer(sc, txq, tid, skb);
961 		else
962 			bf->bf_state.stale = false;
963 
964 		if (!bf) {
965 			ath_txq_skb_done(sc, txq, skb);
966 			ieee80211_free_txskb(sc->hw, skb);
967 			continue;
968 		}
969 
970 		bf->bf_next = NULL;
971 		bf->bf_lastbf = bf;
972 
973 		tx_info = IEEE80211_SKB_CB(skb);
974 		tx_info->flags &= ~IEEE80211_TX_CTL_CLEAR_PS_FILT;
975 
976 		/*
977 		 * No aggregation session is running, but there may be frames
978 		 * from a previous session or a failed attempt in the queue.
979 		 * Send them out as normal data frames
980 		 */
981 		if (!tid->active)
982 			tx_info->flags &= ~IEEE80211_TX_CTL_AMPDU;
983 
984 		if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU)) {
985 			bf->bf_state.bf_type = 0;
986 			return bf;
987 		}
988 
989 		bf->bf_state.bf_type = BUF_AMPDU | BUF_AGGR;
990 		seqno = bf->bf_state.seqno;
991 
992 		/* do not step over block-ack window */
993 		if (!BAW_WITHIN(tid->seq_start, tid->baw_size, seqno)) {
994 			__skb_queue_tail(&tid->retry_q, skb);
995 
996 			/* If there are other skbs in the retry q, they are
997 			 * probably within the BAW, so loop immediately to get
998 			 * one of them. Otherwise the queue can get stuck. */
999 			if (!skb_queue_is_first(&tid->retry_q, skb) &&
1000 			    !WARN_ON(skb == first_skb)) {
1001 				if(!first_skb) /* infinite loop prevention */
1002 					first_skb = skb;
1003 				continue;
1004 			}
1005 			break;
1006 		}
1007 
1008 		if (tid->bar_index > ATH_BA_INDEX(tid->seq_start, seqno)) {
1009 			struct ath_tx_status ts = {};
1010 			struct list_head bf_head;
1011 
1012 			INIT_LIST_HEAD(&bf_head);
1013 			list_add(&bf->list, &bf_head);
1014 			ath_tx_update_baw(sc, tid, seqno);
1015 			ath_tx_complete_buf(sc, bf, txq, &bf_head, NULL, &ts, 0);
1016 			continue;
1017 		}
1018 
1019 		return bf;
1020 	}
1021 
1022 	return NULL;
1023 }
1024 
1025 static int
ath_tx_form_aggr(struct ath_softc * sc,struct ath_txq * txq,struct ath_atx_tid * tid,struct list_head * bf_q,struct ath_buf * bf_first)1026 ath_tx_form_aggr(struct ath_softc *sc, struct ath_txq *txq,
1027 		 struct ath_atx_tid *tid, struct list_head *bf_q,
1028 		 struct ath_buf *bf_first)
1029 {
1030 #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
1031 	struct ath_buf *bf = bf_first, *bf_prev = NULL;
1032 	int nframes = 0, ndelim;
1033 	u16 aggr_limit = 0, al = 0, bpad = 0,
1034 	    al_delta, h_baw = tid->baw_size / 2;
1035 	struct ieee80211_tx_info *tx_info;
1036 	struct ath_frame_info *fi;
1037 	struct sk_buff *skb;
1038 
1039 
1040 	bf = bf_first;
1041 	aggr_limit = ath_lookup_rate(sc, bf, tid);
1042 
1043 	while (bf)
1044 	{
1045 		skb = bf->bf_mpdu;
1046 		fi = get_frame_info(skb);
1047 
1048 		/* do not exceed aggregation limit */
1049 		al_delta = ATH_AGGR_DELIM_SZ + fi->framelen;
1050 		if (nframes) {
1051 			if (aggr_limit < al + bpad + al_delta ||
1052 			    ath_lookup_legacy(bf) || nframes >= h_baw)
1053 				goto stop;
1054 
1055 			tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
1056 			if ((tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) ||
1057 			    !(tx_info->flags & IEEE80211_TX_CTL_AMPDU))
1058 				goto stop;
1059 		}
1060 
1061 		/* add padding for previous frame to aggregation length */
1062 		al += bpad + al_delta;
1063 
1064 		/*
1065 		 * Get the delimiters needed to meet the MPDU
1066 		 * density for this node.
1067 		 */
1068 		ndelim = ath_compute_num_delims(sc, tid, bf_first, fi->framelen,
1069 						!nframes);
1070 		bpad = PADBYTES(al_delta) + (ndelim << 2);
1071 
1072 		nframes++;
1073 		bf->bf_next = NULL;
1074 
1075 		/* link buffers of this frame to the aggregate */
1076 		if (!fi->baw_tracked)
1077 			ath_tx_addto_baw(sc, tid, bf);
1078 		bf->bf_state.ndelim = ndelim;
1079 
1080 		list_add_tail(&bf->list, bf_q);
1081 		if (bf_prev)
1082 			bf_prev->bf_next = bf;
1083 
1084 		bf_prev = bf;
1085 
1086 		bf = ath_tx_get_tid_subframe(sc, txq, tid);
1087 	}
1088 	goto finish;
1089 stop:
1090 	__skb_queue_tail(&tid->retry_q, bf->bf_mpdu);
1091 finish:
1092 	bf = bf_first;
1093 	bf->bf_lastbf = bf_prev;
1094 
1095 	if (bf == bf_prev) {
1096 		al = get_frame_info(bf->bf_mpdu)->framelen;
1097 		bf->bf_state.bf_type = BUF_AMPDU;
1098 	} else {
1099 		TX_STAT_INC(txq->axq_qnum, a_aggr);
1100 	}
1101 
1102 	return al;
1103 #undef PADBYTES
1104 }
1105 
1106 /*
1107  * rix - rate index
1108  * pktlen - total bytes (delims + data + fcs + pads + pad delims)
1109  * width  - 0 for 20 MHz, 1 for 40 MHz
1110  * half_gi - to use 4us v/s 3.6 us for symbol time
1111  */
ath_pkt_duration(struct ath_softc * sc,u8 rix,int pktlen,int width,int half_gi,bool shortPreamble)1112 u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, int pktlen,
1113 		     int width, int half_gi, bool shortPreamble)
1114 {
1115 	u32 nbits, nsymbits, duration, nsymbols;
1116 	int streams;
1117 
1118 	/* find number of symbols: PLCP + data */
1119 	streams = HT_RC_2_STREAMS(rix);
1120 	nbits = (pktlen << 3) + OFDM_PLCP_BITS;
1121 	nsymbits = bits_per_symbol[rix % 8][width] * streams;
1122 	nsymbols = (nbits + nsymbits - 1) / nsymbits;
1123 
1124 	if (!half_gi)
1125 		duration = SYMBOL_TIME(nsymbols);
1126 	else
1127 		duration = SYMBOL_TIME_HALFGI(nsymbols);
1128 
1129 	/* addup duration for legacy/ht training and signal fields */
1130 	duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
1131 
1132 	return duration;
1133 }
1134 
ath_max_framelen(int usec,int mcs,bool ht40,bool sgi)1135 static int ath_max_framelen(int usec, int mcs, bool ht40, bool sgi)
1136 {
1137 	int streams = HT_RC_2_STREAMS(mcs);
1138 	int symbols, bits;
1139 	int bytes = 0;
1140 
1141 	usec -= L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
1142 	symbols = sgi ? TIME_SYMBOLS_HALFGI(usec) : TIME_SYMBOLS(usec);
1143 	bits = symbols * bits_per_symbol[mcs % 8][ht40] * streams;
1144 	bits -= OFDM_PLCP_BITS;
1145 	bytes = bits / 8;
1146 	if (bytes > 65532)
1147 		bytes = 65532;
1148 
1149 	return bytes;
1150 }
1151 
ath_update_max_aggr_framelen(struct ath_softc * sc,int queue,int txop)1152 void ath_update_max_aggr_framelen(struct ath_softc *sc, int queue, int txop)
1153 {
1154 	u16 *cur_ht20, *cur_ht20_sgi, *cur_ht40, *cur_ht40_sgi;
1155 	int mcs;
1156 
1157 	/* 4ms is the default (and maximum) duration */
1158 	if (!txop || txop > 4096)
1159 		txop = 4096;
1160 
1161 	cur_ht20 = sc->tx.max_aggr_framelen[queue][MCS_HT20];
1162 	cur_ht20_sgi = sc->tx.max_aggr_framelen[queue][MCS_HT20_SGI];
1163 	cur_ht40 = sc->tx.max_aggr_framelen[queue][MCS_HT40];
1164 	cur_ht40_sgi = sc->tx.max_aggr_framelen[queue][MCS_HT40_SGI];
1165 	for (mcs = 0; mcs < 32; mcs++) {
1166 		cur_ht20[mcs] = ath_max_framelen(txop, mcs, false, false);
1167 		cur_ht20_sgi[mcs] = ath_max_framelen(txop, mcs, false, true);
1168 		cur_ht40[mcs] = ath_max_framelen(txop, mcs, true, false);
1169 		cur_ht40_sgi[mcs] = ath_max_framelen(txop, mcs, true, true);
1170 	}
1171 }
1172 
ath_get_rate_txpower(struct ath_softc * sc,struct ath_buf * bf,u8 rateidx,bool is_40,bool is_cck)1173 static u8 ath_get_rate_txpower(struct ath_softc *sc, struct ath_buf *bf,
1174 			       u8 rateidx, bool is_40, bool is_cck)
1175 {
1176 	u8 max_power;
1177 	struct sk_buff *skb;
1178 	struct ath_frame_info *fi;
1179 	struct ieee80211_tx_info *info;
1180 	struct ath_hw *ah = sc->sc_ah;
1181 
1182 	if (sc->tx99_state || !ah->tpc_enabled)
1183 		return MAX_RATE_POWER;
1184 
1185 	skb = bf->bf_mpdu;
1186 	fi = get_frame_info(skb);
1187 	info = IEEE80211_SKB_CB(skb);
1188 
1189 	if (!AR_SREV_9300_20_OR_LATER(ah)) {
1190 		int txpower = fi->tx_power;
1191 
1192 		if (is_40) {
1193 			u8 power_ht40delta;
1194 			struct ar5416_eeprom_def *eep = &ah->eeprom.def;
1195 			u16 eeprom_rev = ah->eep_ops->get_eeprom_rev(ah);
1196 
1197 			if (eeprom_rev >= AR5416_EEP_MINOR_VER_2) {
1198 				bool is_2ghz;
1199 				struct modal_eep_header *pmodal;
1200 
1201 				is_2ghz = info->band == NL80211_BAND_2GHZ;
1202 				pmodal = &eep->modalHeader[is_2ghz];
1203 				power_ht40delta = pmodal->ht40PowerIncForPdadc;
1204 			} else {
1205 				power_ht40delta = 2;
1206 			}
1207 			txpower += power_ht40delta;
1208 		}
1209 
1210 		if (AR_SREV_9287(ah) || AR_SREV_9285(ah) ||
1211 		    AR_SREV_9271(ah)) {
1212 			txpower -= 2 * AR9287_PWR_TABLE_OFFSET_DB;
1213 		} else if (AR_SREV_9280_20_OR_LATER(ah)) {
1214 			s8 power_offset;
1215 
1216 			power_offset = ah->eep_ops->get_eeprom(ah,
1217 							EEP_PWR_TABLE_OFFSET);
1218 			txpower -= 2 * power_offset;
1219 		}
1220 
1221 		if (OLC_FOR_AR9280_20_LATER && is_cck)
1222 			txpower -= 2;
1223 
1224 		txpower = max(txpower, 0);
1225 		max_power = min_t(u8, ah->tx_power[rateidx], txpower);
1226 
1227 		/* XXX: clamp minimum TX power at 1 for AR9160 since if
1228 		 * max_power is set to 0, frames are transmitted at max
1229 		 * TX power
1230 		 */
1231 		if (!max_power && !AR_SREV_9280_20_OR_LATER(ah))
1232 			max_power = 1;
1233 	} else if (!bf->bf_state.bfs_paprd) {
1234 		if (rateidx < 8 && (info->flags & IEEE80211_TX_CTL_STBC))
1235 			max_power = min_t(u8, ah->tx_power_stbc[rateidx],
1236 					  fi->tx_power);
1237 		else
1238 			max_power = min_t(u8, ah->tx_power[rateidx],
1239 					  fi->tx_power);
1240 	} else {
1241 		max_power = ah->paprd_training_power;
1242 	}
1243 
1244 	return max_power;
1245 }
1246 
ath_buf_set_rate(struct ath_softc * sc,struct ath_buf * bf,struct ath_tx_info * info,int len,bool rts)1247 static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf,
1248 			     struct ath_tx_info *info, int len, bool rts)
1249 {
1250 	struct ath_hw *ah = sc->sc_ah;
1251 	struct ath_common *common = ath9k_hw_common(ah);
1252 	struct sk_buff *skb;
1253 	struct ieee80211_tx_info *tx_info;
1254 	struct ieee80211_tx_rate *rates;
1255 	const struct ieee80211_rate *rate;
1256 	struct ieee80211_hdr *hdr;
1257 	struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
1258 	u32 rts_thresh = sc->hw->wiphy->rts_threshold;
1259 	int i;
1260 	u8 rix = 0;
1261 
1262 	skb = bf->bf_mpdu;
1263 	tx_info = IEEE80211_SKB_CB(skb);
1264 	rates = bf->rates;
1265 	hdr = (struct ieee80211_hdr *)skb->data;
1266 
1267 	/* set dur_update_en for l-sig computation except for PS-Poll frames */
1268 	info->dur_update = !ieee80211_is_pspoll(hdr->frame_control);
1269 	info->rtscts_rate = fi->rtscts_rate;
1270 
1271 	for (i = 0; i < ARRAY_SIZE(bf->rates); i++) {
1272 		bool is_40, is_sgi, is_sp, is_cck;
1273 		int phy;
1274 
1275 		if (!rates[i].count || (rates[i].idx < 0))
1276 			continue;
1277 
1278 		rix = rates[i].idx;
1279 		info->rates[i].Tries = rates[i].count;
1280 
1281 		/*
1282 		 * Handle RTS threshold for unaggregated HT frames.
1283 		 */
1284 		if (bf_isampdu(bf) && !bf_isaggr(bf) &&
1285 		    (rates[i].flags & IEEE80211_TX_RC_MCS) &&
1286 		    unlikely(rts_thresh != (u32) -1)) {
1287 			if (!rts_thresh || (len > rts_thresh))
1288 				rts = true;
1289 		}
1290 
1291 		if (rts || rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
1292 			info->rates[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
1293 			info->flags |= ATH9K_TXDESC_RTSENA;
1294 		} else if (rates[i].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
1295 			info->rates[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
1296 			info->flags |= ATH9K_TXDESC_CTSENA;
1297 		}
1298 
1299 		if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
1300 			info->rates[i].RateFlags |= ATH9K_RATESERIES_2040;
1301 		if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
1302 			info->rates[i].RateFlags |= ATH9K_RATESERIES_HALFGI;
1303 
1304 		is_sgi = !!(rates[i].flags & IEEE80211_TX_RC_SHORT_GI);
1305 		is_40 = !!(rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH);
1306 		is_sp = !!(rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
1307 
1308 		if (rates[i].flags & IEEE80211_TX_RC_MCS) {
1309 			/* MCS rates */
1310 			info->rates[i].Rate = rix | 0x80;
1311 			info->rates[i].ChSel = ath_txchainmask_reduction(sc,
1312 					ah->txchainmask, info->rates[i].Rate);
1313 			info->rates[i].PktDuration = ath_pkt_duration(sc, rix, len,
1314 				 is_40, is_sgi, is_sp);
1315 			if (rix < 8 && (tx_info->flags & IEEE80211_TX_CTL_STBC))
1316 				info->rates[i].RateFlags |= ATH9K_RATESERIES_STBC;
1317 
1318 			info->txpower[i] = ath_get_rate_txpower(sc, bf, rix,
1319 								is_40, false);
1320 			continue;
1321 		}
1322 
1323 		/* legacy rates */
1324 		rate = &common->sbands[tx_info->band].bitrates[rates[i].idx];
1325 		if ((tx_info->band == NL80211_BAND_2GHZ) &&
1326 		    !(rate->flags & IEEE80211_RATE_ERP_G))
1327 			phy = WLAN_RC_PHY_CCK;
1328 		else
1329 			phy = WLAN_RC_PHY_OFDM;
1330 
1331 		info->rates[i].Rate = rate->hw_value;
1332 		if (rate->hw_value_short) {
1333 			if (rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
1334 				info->rates[i].Rate |= rate->hw_value_short;
1335 		} else {
1336 			is_sp = false;
1337 		}
1338 
1339 		if (bf->bf_state.bfs_paprd)
1340 			info->rates[i].ChSel = ah->txchainmask;
1341 		else
1342 			info->rates[i].ChSel = ath_txchainmask_reduction(sc,
1343 					ah->txchainmask, info->rates[i].Rate);
1344 
1345 		info->rates[i].PktDuration = ath9k_hw_computetxtime(sc->sc_ah,
1346 			phy, rate->bitrate * 100, len, rix, is_sp);
1347 
1348 		is_cck = IS_CCK_RATE(info->rates[i].Rate);
1349 		info->txpower[i] = ath_get_rate_txpower(sc, bf, rix, false,
1350 							is_cck);
1351 	}
1352 
1353 	/* For AR5416 - RTS cannot be followed by a frame larger than 8K */
1354 	if (bf_isaggr(bf) && (len > sc->sc_ah->caps.rts_aggr_limit))
1355 		info->flags &= ~ATH9K_TXDESC_RTSENA;
1356 
1357 	/* ATH9K_TXDESC_RTSENA and ATH9K_TXDESC_CTSENA are mutually exclusive. */
1358 	if (info->flags & ATH9K_TXDESC_RTSENA)
1359 		info->flags &= ~ATH9K_TXDESC_CTSENA;
1360 }
1361 
get_hw_packet_type(struct sk_buff * skb)1362 static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
1363 {
1364 	struct ieee80211_hdr *hdr;
1365 	enum ath9k_pkt_type htype;
1366 	__le16 fc;
1367 
1368 	hdr = (struct ieee80211_hdr *)skb->data;
1369 	fc = hdr->frame_control;
1370 
1371 	if (ieee80211_is_beacon(fc))
1372 		htype = ATH9K_PKT_TYPE_BEACON;
1373 	else if (ieee80211_is_probe_resp(fc))
1374 		htype = ATH9K_PKT_TYPE_PROBE_RESP;
1375 	else if (ieee80211_is_atim(fc))
1376 		htype = ATH9K_PKT_TYPE_ATIM;
1377 	else if (ieee80211_is_pspoll(fc))
1378 		htype = ATH9K_PKT_TYPE_PSPOLL;
1379 	else
1380 		htype = ATH9K_PKT_TYPE_NORMAL;
1381 
1382 	return htype;
1383 }
1384 
ath_tx_fill_desc(struct ath_softc * sc,struct ath_buf * bf,struct ath_txq * txq,int len)1385 static void ath_tx_fill_desc(struct ath_softc *sc, struct ath_buf *bf,
1386 			     struct ath_txq *txq, int len)
1387 {
1388 	struct ath_hw *ah = sc->sc_ah;
1389 	struct ath_buf *bf_first = NULL;
1390 	struct ath_tx_info info;
1391 	u32 rts_thresh = sc->hw->wiphy->rts_threshold;
1392 	bool rts = false;
1393 
1394 	memset(&info, 0, sizeof(info));
1395 	info.is_first = true;
1396 	info.is_last = true;
1397 	info.qcu = txq->axq_qnum;
1398 
1399 	while (bf) {
1400 		struct sk_buff *skb = bf->bf_mpdu;
1401 		struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1402 		struct ath_frame_info *fi = get_frame_info(skb);
1403 		bool aggr = !!(bf->bf_state.bf_type & BUF_AGGR);
1404 
1405 		info.type = get_hw_packet_type(skb);
1406 		if (bf->bf_next)
1407 			info.link = bf->bf_next->bf_daddr;
1408 		else
1409 			info.link = (sc->tx99_state) ? bf->bf_daddr : 0;
1410 
1411 		if (!bf_first) {
1412 			bf_first = bf;
1413 
1414 			if (!sc->tx99_state)
1415 				info.flags = ATH9K_TXDESC_INTREQ;
1416 			if ((tx_info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT) ||
1417 			    txq == sc->tx.uapsdq)
1418 				info.flags |= ATH9K_TXDESC_CLRDMASK;
1419 
1420 			if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
1421 				info.flags |= ATH9K_TXDESC_NOACK;
1422 			if (tx_info->flags & IEEE80211_TX_CTL_LDPC)
1423 				info.flags |= ATH9K_TXDESC_LDPC;
1424 
1425 			if (bf->bf_state.bfs_paprd)
1426 				info.flags |= (u32) bf->bf_state.bfs_paprd <<
1427 					      ATH9K_TXDESC_PAPRD_S;
1428 
1429 			/*
1430 			 * mac80211 doesn't handle RTS threshold for HT because
1431 			 * the decision has to be taken based on AMPDU length
1432 			 * and aggregation is done entirely inside ath9k.
1433 			 * Set the RTS/CTS flag for the first subframe based
1434 			 * on the threshold.
1435 			 */
1436 			if (aggr && (bf == bf_first) &&
1437 			    unlikely(rts_thresh != (u32) -1)) {
1438 				/*
1439 				 * "len" is the size of the entire AMPDU.
1440 				 */
1441 				if (!rts_thresh || (len > rts_thresh))
1442 					rts = true;
1443 			}
1444 
1445 			if (!aggr)
1446 				len = fi->framelen;
1447 
1448 			ath_buf_set_rate(sc, bf, &info, len, rts);
1449 		}
1450 
1451 		info.buf_addr[0] = bf->bf_buf_addr;
1452 		info.buf_len[0] = skb->len;
1453 		info.pkt_len = fi->framelen;
1454 		info.keyix = fi->keyix;
1455 		info.keytype = fi->keytype;
1456 
1457 		if (aggr) {
1458 			if (bf == bf_first)
1459 				info.aggr = AGGR_BUF_FIRST;
1460 			else if (bf == bf_first->bf_lastbf)
1461 				info.aggr = AGGR_BUF_LAST;
1462 			else
1463 				info.aggr = AGGR_BUF_MIDDLE;
1464 
1465 			info.ndelim = bf->bf_state.ndelim;
1466 			info.aggr_len = len;
1467 		}
1468 
1469 		if (bf == bf_first->bf_lastbf)
1470 			bf_first = NULL;
1471 
1472 		ath9k_hw_set_txdesc(ah, bf->bf_desc, &info);
1473 		bf = bf->bf_next;
1474 	}
1475 }
1476 
1477 static void
ath_tx_form_burst(struct ath_softc * sc,struct ath_txq * txq,struct ath_atx_tid * tid,struct list_head * bf_q,struct ath_buf * bf_first)1478 ath_tx_form_burst(struct ath_softc *sc, struct ath_txq *txq,
1479 		  struct ath_atx_tid *tid, struct list_head *bf_q,
1480 		  struct ath_buf *bf_first)
1481 {
1482 	struct ath_buf *bf = bf_first, *bf_prev = NULL;
1483 	int nframes = 0;
1484 
1485 	do {
1486 		struct ieee80211_tx_info *tx_info;
1487 
1488 		nframes++;
1489 		list_add_tail(&bf->list, bf_q);
1490 		if (bf_prev)
1491 			bf_prev->bf_next = bf;
1492 		bf_prev = bf;
1493 
1494 		if (nframes >= 2)
1495 			break;
1496 
1497 		bf = ath_tx_get_tid_subframe(sc, txq, tid);
1498 		if (!bf)
1499 			break;
1500 
1501 		tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
1502 		if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
1503 			__skb_queue_tail(&tid->retry_q, bf->bf_mpdu);
1504 			break;
1505 		}
1506 
1507 		ath_set_rates(tid->an->vif, tid->an->sta, bf);
1508 	} while (1);
1509 }
1510 
ath_tx_sched_aggr(struct ath_softc * sc,struct ath_txq * txq,struct ath_atx_tid * tid)1511 static bool ath_tx_sched_aggr(struct ath_softc *sc, struct ath_txq *txq,
1512 			      struct ath_atx_tid *tid)
1513 {
1514 	struct ath_buf *bf;
1515 	struct ieee80211_tx_info *tx_info;
1516 	struct list_head bf_q;
1517 	int aggr_len = 0;
1518 	bool aggr;
1519 
1520 	if (!ath_tid_has_buffered(tid))
1521 		return false;
1522 
1523 	INIT_LIST_HEAD(&bf_q);
1524 
1525 	bf = ath_tx_get_tid_subframe(sc, txq, tid);
1526 	if (!bf)
1527 		return false;
1528 
1529 	tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
1530 	aggr = !!(tx_info->flags & IEEE80211_TX_CTL_AMPDU);
1531 	if ((aggr && txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH) ||
1532 	    (!aggr && txq->axq_depth >= ATH_NON_AGGR_MIN_QDEPTH)) {
1533 		__skb_queue_tail(&tid->retry_q, bf->bf_mpdu);
1534 		return false;
1535 	}
1536 
1537 	ath_set_rates(tid->an->vif, tid->an->sta, bf);
1538 	if (aggr)
1539 		aggr_len = ath_tx_form_aggr(sc, txq, tid, &bf_q, bf);
1540 	else
1541 		ath_tx_form_burst(sc, txq, tid, &bf_q, bf);
1542 
1543 	if (list_empty(&bf_q))
1544 		return false;
1545 
1546 	if (tid->clear_ps_filter || tid->an->no_ps_filter) {
1547 		tid->clear_ps_filter = false;
1548 		tx_info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1549 	}
1550 
1551 	ath_tx_fill_desc(sc, bf, txq, aggr_len);
1552 	ath_tx_txqaddbuf(sc, txq, &bf_q, false);
1553 	return true;
1554 }
1555 
ath_tx_aggr_start(struct ath_softc * sc,struct ieee80211_sta * sta,u16 tid,u16 * ssn)1556 int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
1557 		      u16 tid, u16 *ssn)
1558 {
1559 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1560 	struct ath_atx_tid *txtid;
1561 	struct ath_txq *txq;
1562 	struct ath_node *an;
1563 	u8 density;
1564 
1565 	ath_dbg(common, XMIT, "%s called\n", __func__);
1566 
1567 	an = (struct ath_node *)sta->drv_priv;
1568 	txtid = ATH_AN_2_TID(an, tid);
1569 	txq = txtid->txq;
1570 
1571 	ath_txq_lock(sc, txq);
1572 
1573 	/* update ampdu factor/density, they may have changed. This may happen
1574 	 * in HT IBSS when a beacon with HT-info is received after the station
1575 	 * has already been added.
1576 	 */
1577 	if (sta->ht_cap.ht_supported) {
1578 		an->maxampdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1579 				      sta->ht_cap.ampdu_factor)) - 1;
1580 		density = ath9k_parse_mpdudensity(sta->ht_cap.ampdu_density);
1581 		an->mpdudensity = density;
1582 	}
1583 
1584 	txtid->active = true;
1585 	*ssn = txtid->seq_start = txtid->seq_next;
1586 	txtid->bar_index = -1;
1587 
1588 	memset(txtid->tx_buf, 0, sizeof(txtid->tx_buf));
1589 	txtid->baw_head = txtid->baw_tail = 0;
1590 
1591 	ath_txq_unlock_complete(sc, txq);
1592 
1593 	return 0;
1594 }
1595 
ath_tx_aggr_stop(struct ath_softc * sc,struct ieee80211_sta * sta,u16 tid)1596 void ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
1597 {
1598 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1599 	struct ath_node *an = (struct ath_node *)sta->drv_priv;
1600 	struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
1601 	struct ath_txq *txq = txtid->txq;
1602 
1603 	ath_dbg(common, XMIT, "%s called\n", __func__);
1604 
1605 	ath_txq_lock(sc, txq);
1606 	txtid->active = false;
1607 	ath_tx_flush_tid(sc, txtid);
1608 	ath_txq_unlock_complete(sc, txq);
1609 }
1610 
ath_tx_aggr_sleep(struct ieee80211_sta * sta,struct ath_softc * sc,struct ath_node * an)1611 void ath_tx_aggr_sleep(struct ieee80211_sta *sta, struct ath_softc *sc,
1612 		       struct ath_node *an)
1613 {
1614 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1615 	struct ath_atx_tid *tid;
1616 	struct ath_txq *txq;
1617 	int tidno;
1618 
1619 	ath_dbg(common, XMIT, "%s called\n", __func__);
1620 
1621 	for (tidno = 0; tidno < IEEE80211_NUM_TIDS; tidno++) {
1622 		tid = ath_node_to_tid(an, tidno);
1623 		txq = tid->txq;
1624 
1625 		ath_txq_lock(sc, txq);
1626 
1627 		if (list_empty(&tid->list)) {
1628 			ath_txq_unlock(sc, txq);
1629 			continue;
1630 		}
1631 
1632 		if (!skb_queue_empty(&tid->retry_q))
1633 			ieee80211_sta_set_buffered(sta, tid->tidno, true);
1634 
1635 		list_del_init(&tid->list);
1636 
1637 		ath_txq_unlock(sc, txq);
1638 	}
1639 }
1640 
ath_tx_aggr_wakeup(struct ath_softc * sc,struct ath_node * an)1641 void ath_tx_aggr_wakeup(struct ath_softc *sc, struct ath_node *an)
1642 {
1643 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1644 	struct ath_atx_tid *tid;
1645 	struct ath_txq *txq;
1646 	int tidno;
1647 
1648 	ath_dbg(common, XMIT, "%s called\n", __func__);
1649 
1650 	for (tidno = 0; tidno < IEEE80211_NUM_TIDS; tidno++) {
1651 		tid = ath_node_to_tid(an, tidno);
1652 		txq = tid->txq;
1653 
1654 		ath_txq_lock(sc, txq);
1655 		tid->clear_ps_filter = true;
1656 		if (ath_tid_has_buffered(tid)) {
1657 			ath_tx_queue_tid(sc, tid);
1658 			ath_txq_schedule(sc, txq);
1659 		}
1660 		ath_txq_unlock_complete(sc, txq);
1661 	}
1662 }
1663 
ath9k_release_buffered_frames(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tids,int nframes,enum ieee80211_frame_release_type reason,bool more_data)1664 void ath9k_release_buffered_frames(struct ieee80211_hw *hw,
1665 				   struct ieee80211_sta *sta,
1666 				   u16 tids, int nframes,
1667 				   enum ieee80211_frame_release_type reason,
1668 				   bool more_data)
1669 {
1670 	struct ath_softc *sc = hw->priv;
1671 	struct ath_node *an = (struct ath_node *)sta->drv_priv;
1672 	struct ath_txq *txq = sc->tx.uapsdq;
1673 	struct ieee80211_tx_info *info;
1674 	struct list_head bf_q;
1675 	struct ath_buf *bf_tail = NULL, *bf;
1676 	int sent = 0;
1677 	int i;
1678 
1679 	INIT_LIST_HEAD(&bf_q);
1680 	for (i = 0; tids && nframes; i++, tids >>= 1) {
1681 		struct ath_atx_tid *tid;
1682 
1683 		if (!(tids & 1))
1684 			continue;
1685 
1686 		tid = ATH_AN_2_TID(an, i);
1687 
1688 		ath_txq_lock(sc, tid->txq);
1689 		while (nframes > 0) {
1690 			bf = ath_tx_get_tid_subframe(sc, sc->tx.uapsdq, tid);
1691 			if (!bf)
1692 				break;
1693 
1694 			list_add_tail(&bf->list, &bf_q);
1695 			ath_set_rates(tid->an->vif, tid->an->sta, bf);
1696 			if (bf_isampdu(bf)) {
1697 				ath_tx_addto_baw(sc, tid, bf);
1698 				bf->bf_state.bf_type &= ~BUF_AGGR;
1699 			}
1700 			if (bf_tail)
1701 				bf_tail->bf_next = bf;
1702 
1703 			bf_tail = bf;
1704 			nframes--;
1705 			sent++;
1706 			TX_STAT_INC(txq->axq_qnum, a_queued_hw);
1707 
1708 			if (an->sta && skb_queue_empty(&tid->retry_q))
1709 				ieee80211_sta_set_buffered(an->sta, i, false);
1710 		}
1711 		ath_txq_unlock_complete(sc, tid->txq);
1712 	}
1713 
1714 	if (list_empty(&bf_q))
1715 		return;
1716 
1717 	info = IEEE80211_SKB_CB(bf_tail->bf_mpdu);
1718 	info->flags |= IEEE80211_TX_STATUS_EOSP;
1719 
1720 	bf = list_first_entry(&bf_q, struct ath_buf, list);
1721 	ath_txq_lock(sc, txq);
1722 	ath_tx_fill_desc(sc, bf, txq, 0);
1723 	ath_tx_txqaddbuf(sc, txq, &bf_q, false);
1724 	ath_txq_unlock(sc, txq);
1725 }
1726 
1727 /********************/
1728 /* Queue Management */
1729 /********************/
1730 
ath_txq_setup(struct ath_softc * sc,int qtype,int subtype)1731 struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
1732 {
1733 	struct ath_hw *ah = sc->sc_ah;
1734 	struct ath9k_tx_queue_info qi;
1735 	static const int subtype_txq_to_hwq[] = {
1736 		[IEEE80211_AC_BE] = ATH_TXQ_AC_BE,
1737 		[IEEE80211_AC_BK] = ATH_TXQ_AC_BK,
1738 		[IEEE80211_AC_VI] = ATH_TXQ_AC_VI,
1739 		[IEEE80211_AC_VO] = ATH_TXQ_AC_VO,
1740 	};
1741 	int axq_qnum, i;
1742 
1743 	memset(&qi, 0, sizeof(qi));
1744 	qi.tqi_subtype = subtype_txq_to_hwq[subtype];
1745 	qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
1746 	qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
1747 	qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
1748 	qi.tqi_physCompBuf = 0;
1749 
1750 	/*
1751 	 * Enable interrupts only for EOL and DESC conditions.
1752 	 * We mark tx descriptors to receive a DESC interrupt
1753 	 * when a tx queue gets deep; otherwise waiting for the
1754 	 * EOL to reap descriptors.  Note that this is done to
1755 	 * reduce interrupt load and this only defers reaping
1756 	 * descriptors, never transmitting frames.  Aside from
1757 	 * reducing interrupts this also permits more concurrency.
1758 	 * The only potential downside is if the tx queue backs
1759 	 * up in which case the top half of the kernel may backup
1760 	 * due to a lack of tx descriptors.
1761 	 *
1762 	 * The UAPSD queue is an exception, since we take a desc-
1763 	 * based intr on the EOSP frames.
1764 	 */
1765 	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
1766 		qi.tqi_qflags = TXQ_FLAG_TXINT_ENABLE;
1767 	} else {
1768 		if (qtype == ATH9K_TX_QUEUE_UAPSD)
1769 			qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1770 		else
1771 			qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
1772 					TXQ_FLAG_TXDESCINT_ENABLE;
1773 	}
1774 	axq_qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
1775 	if (axq_qnum == -1) {
1776 		/*
1777 		 * NB: don't print a message, this happens
1778 		 * normally on parts with too few tx queues
1779 		 */
1780 		return NULL;
1781 	}
1782 	if (!ATH_TXQ_SETUP(sc, axq_qnum)) {
1783 		struct ath_txq *txq = &sc->tx.txq[axq_qnum];
1784 
1785 		txq->axq_qnum = axq_qnum;
1786 		txq->mac80211_qnum = -1;
1787 		txq->axq_link = NULL;
1788 		__skb_queue_head_init(&txq->complete_q);
1789 		INIT_LIST_HEAD(&txq->axq_q);
1790 		spin_lock_init(&txq->axq_lock);
1791 		txq->axq_depth = 0;
1792 		txq->axq_ampdu_depth = 0;
1793 		txq->axq_tx_inprogress = false;
1794 		sc->tx.txqsetup |= 1<<axq_qnum;
1795 
1796 		txq->txq_headidx = txq->txq_tailidx = 0;
1797 		for (i = 0; i < ATH_TXFIFO_DEPTH; i++)
1798 			INIT_LIST_HEAD(&txq->txq_fifo[i]);
1799 	}
1800 	return &sc->tx.txq[axq_qnum];
1801 }
1802 
ath_txq_update(struct ath_softc * sc,int qnum,struct ath9k_tx_queue_info * qinfo)1803 int ath_txq_update(struct ath_softc *sc, int qnum,
1804 		   struct ath9k_tx_queue_info *qinfo)
1805 {
1806 	struct ath_hw *ah = sc->sc_ah;
1807 	int error = 0;
1808 	struct ath9k_tx_queue_info qi;
1809 
1810 	BUG_ON(sc->tx.txq[qnum].axq_qnum != qnum);
1811 
1812 	ath9k_hw_get_txq_props(ah, qnum, &qi);
1813 	qi.tqi_aifs = qinfo->tqi_aifs;
1814 	qi.tqi_cwmin = qinfo->tqi_cwmin;
1815 	qi.tqi_cwmax = qinfo->tqi_cwmax;
1816 	qi.tqi_burstTime = qinfo->tqi_burstTime;
1817 	qi.tqi_readyTime = qinfo->tqi_readyTime;
1818 
1819 	if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
1820 		ath_err(ath9k_hw_common(sc->sc_ah),
1821 			"Unable to update hardware queue %u!\n", qnum);
1822 		error = -EIO;
1823 	} else {
1824 		ath9k_hw_resettxqueue(ah, qnum);
1825 	}
1826 
1827 	return error;
1828 }
1829 
ath_cabq_update(struct ath_softc * sc)1830 int ath_cabq_update(struct ath_softc *sc)
1831 {
1832 	struct ath9k_tx_queue_info qi;
1833 	struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1834 	int qnum = sc->beacon.cabq->axq_qnum;
1835 
1836 	ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
1837 
1838 	qi.tqi_readyTime = (TU_TO_USEC(cur_conf->beacon_interval) *
1839 			    ATH_CABQ_READY_TIME) / 100;
1840 	ath_txq_update(sc, qnum, &qi);
1841 
1842 	return 0;
1843 }
1844 
ath_drain_txq_list(struct ath_softc * sc,struct ath_txq * txq,struct list_head * list)1845 static void ath_drain_txq_list(struct ath_softc *sc, struct ath_txq *txq,
1846 			       struct list_head *list)
1847 {
1848 	struct ath_buf *bf, *lastbf;
1849 	struct list_head bf_head;
1850 	struct ath_tx_status ts;
1851 
1852 	memset(&ts, 0, sizeof(ts));
1853 	ts.ts_status = ATH9K_TX_FLUSH;
1854 	INIT_LIST_HEAD(&bf_head);
1855 
1856 	while (!list_empty(list)) {
1857 		bf = list_first_entry(list, struct ath_buf, list);
1858 
1859 		if (bf->bf_state.stale) {
1860 			list_del(&bf->list);
1861 
1862 			ath_tx_return_buffer(sc, bf);
1863 			continue;
1864 		}
1865 
1866 		lastbf = bf->bf_lastbf;
1867 		list_cut_position(&bf_head, list, &lastbf->list);
1868 		ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
1869 	}
1870 }
1871 
1872 /*
1873  * Drain a given TX queue (could be Beacon or Data)
1874  *
1875  * This assumes output has been stopped and
1876  * we do not need to block ath_tx_tasklet.
1877  */
ath_draintxq(struct ath_softc * sc,struct ath_txq * txq)1878 void ath_draintxq(struct ath_softc *sc, struct ath_txq *txq)
1879 {
1880 	rcu_read_lock();
1881 	ath_txq_lock(sc, txq);
1882 
1883 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
1884 		int idx = txq->txq_tailidx;
1885 
1886 		while (!list_empty(&txq->txq_fifo[idx])) {
1887 			ath_drain_txq_list(sc, txq, &txq->txq_fifo[idx]);
1888 
1889 			INCR(idx, ATH_TXFIFO_DEPTH);
1890 		}
1891 		txq->txq_tailidx = idx;
1892 	}
1893 
1894 	txq->axq_link = NULL;
1895 	txq->axq_tx_inprogress = false;
1896 	ath_drain_txq_list(sc, txq, &txq->axq_q);
1897 
1898 	ath_txq_unlock_complete(sc, txq);
1899 	rcu_read_unlock();
1900 }
1901 
ath_drain_all_txq(struct ath_softc * sc)1902 bool ath_drain_all_txq(struct ath_softc *sc)
1903 {
1904 	struct ath_hw *ah = sc->sc_ah;
1905 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1906 	struct ath_txq *txq;
1907 	int i;
1908 	u32 npend = 0;
1909 
1910 	if (test_bit(ATH_OP_INVALID, &common->op_flags))
1911 		return true;
1912 
1913 	ath9k_hw_abort_tx_dma(ah);
1914 
1915 	/* Check if any queue remains active */
1916 	for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1917 		if (!ATH_TXQ_SETUP(sc, i))
1918 			continue;
1919 
1920 		if (!sc->tx.txq[i].axq_depth)
1921 			continue;
1922 
1923 		if (ath9k_hw_numtxpending(ah, sc->tx.txq[i].axq_qnum))
1924 			npend |= BIT(i);
1925 	}
1926 
1927 	if (npend) {
1928 		RESET_STAT_INC(sc, RESET_TX_DMA_ERROR);
1929 		ath_dbg(common, RESET,
1930 			"Failed to stop TX DMA, queues=0x%03x!\n", npend);
1931 	}
1932 
1933 	for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1934 		if (!ATH_TXQ_SETUP(sc, i))
1935 			continue;
1936 
1937 		txq = &sc->tx.txq[i];
1938 		ath_draintxq(sc, txq);
1939 	}
1940 
1941 	return !npend;
1942 }
1943 
ath_tx_cleanupq(struct ath_softc * sc,struct ath_txq * txq)1944 void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
1945 {
1946 	ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
1947 	sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
1948 }
1949 
1950 /* For each acq entry, for each tid, try to schedule packets
1951  * for transmit until ampdu_depth has reached min Q depth.
1952  */
ath_txq_schedule(struct ath_softc * sc,struct ath_txq * txq)1953 void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
1954 {
1955 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1956 	struct ath_atx_tid *tid;
1957 	struct list_head *tid_list;
1958 	struct ath_acq *acq;
1959 	bool active = AIRTIME_ACTIVE(sc->airtime_flags);
1960 
1961 	if (txq->mac80211_qnum < 0)
1962 		return;
1963 
1964 	if (test_bit(ATH_OP_HW_RESET, &common->op_flags))
1965 		return;
1966 
1967 	spin_lock_bh(&sc->chan_lock);
1968 	rcu_read_lock();
1969 	acq = &sc->cur_chan->acq[txq->mac80211_qnum];
1970 
1971 	if (sc->cur_chan->stopped)
1972 		goto out;
1973 
1974 begin:
1975 	tid_list = &acq->acq_new;
1976 	if (list_empty(tid_list)) {
1977 		tid_list = &acq->acq_old;
1978 		if (list_empty(tid_list))
1979 			goto out;
1980 	}
1981 	tid = list_first_entry(tid_list, struct ath_atx_tid, list);
1982 
1983 	if (active && tid->an->airtime_deficit[txq->mac80211_qnum] <= 0) {
1984 		spin_lock_bh(&acq->lock);
1985 		tid->an->airtime_deficit[txq->mac80211_qnum] += ATH_AIRTIME_QUANTUM;
1986 		list_move_tail(&tid->list, &acq->acq_old);
1987 		spin_unlock_bh(&acq->lock);
1988 		goto begin;
1989 	}
1990 
1991 	if (!ath_tid_has_buffered(tid)) {
1992 		spin_lock_bh(&acq->lock);
1993 		if ((tid_list == &acq->acq_new) && !list_empty(&acq->acq_old))
1994 			list_move_tail(&tid->list, &acq->acq_old);
1995 		else {
1996 			list_del_init(&tid->list);
1997 		}
1998 		spin_unlock_bh(&acq->lock);
1999 		goto begin;
2000 	}
2001 
2002 
2003 	/*
2004 	 * If we succeed in scheduling something, immediately restart to make
2005 	 * sure we keep the HW busy.
2006 	 */
2007 	if(ath_tx_sched_aggr(sc, txq, tid)) {
2008 		if (!active) {
2009 			spin_lock_bh(&acq->lock);
2010 			list_move_tail(&tid->list, &acq->acq_old);
2011 			spin_unlock_bh(&acq->lock);
2012 		}
2013 		goto begin;
2014 	}
2015 
2016 out:
2017 	rcu_read_unlock();
2018 	spin_unlock_bh(&sc->chan_lock);
2019 }
2020 
ath_txq_schedule_all(struct ath_softc * sc)2021 void ath_txq_schedule_all(struct ath_softc *sc)
2022 {
2023 	struct ath_txq *txq;
2024 	int i;
2025 
2026 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2027 		txq = sc->tx.txq_map[i];
2028 
2029 		spin_lock_bh(&txq->axq_lock);
2030 		ath_txq_schedule(sc, txq);
2031 		spin_unlock_bh(&txq->axq_lock);
2032 	}
2033 }
2034 
2035 /***********/
2036 /* TX, DMA */
2037 /***********/
2038 
2039 /*
2040  * Insert a chain of ath_buf (descriptors) on a txq and
2041  * assume the descriptors are already chained together by caller.
2042  */
ath_tx_txqaddbuf(struct ath_softc * sc,struct ath_txq * txq,struct list_head * head,bool internal)2043 static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
2044 			     struct list_head *head, bool internal)
2045 {
2046 	struct ath_hw *ah = sc->sc_ah;
2047 	struct ath_common *common = ath9k_hw_common(ah);
2048 	struct ath_buf *bf, *bf_last;
2049 	bool puttxbuf = false;
2050 	bool edma;
2051 
2052 	/*
2053 	 * Insert the frame on the outbound list and
2054 	 * pass it on to the hardware.
2055 	 */
2056 
2057 	if (list_empty(head))
2058 		return;
2059 
2060 	edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
2061 	bf = list_first_entry(head, struct ath_buf, list);
2062 	bf_last = list_entry(head->prev, struct ath_buf, list);
2063 
2064 	ath_dbg(common, QUEUE, "qnum: %d, txq depth: %d\n",
2065 		txq->axq_qnum, txq->axq_depth);
2066 
2067 	if (edma && list_empty(&txq->txq_fifo[txq->txq_headidx])) {
2068 		list_splice_tail_init(head, &txq->txq_fifo[txq->txq_headidx]);
2069 		INCR(txq->txq_headidx, ATH_TXFIFO_DEPTH);
2070 		puttxbuf = true;
2071 	} else {
2072 		list_splice_tail_init(head, &txq->axq_q);
2073 
2074 		if (txq->axq_link) {
2075 			ath9k_hw_set_desc_link(ah, txq->axq_link, bf->bf_daddr);
2076 			ath_dbg(common, XMIT, "link[%u] (%p)=%llx (%p)\n",
2077 				txq->axq_qnum, txq->axq_link,
2078 				ito64(bf->bf_daddr), bf->bf_desc);
2079 		} else if (!edma)
2080 			puttxbuf = true;
2081 
2082 		txq->axq_link = bf_last->bf_desc;
2083 	}
2084 
2085 	if (puttxbuf) {
2086 		TX_STAT_INC(txq->axq_qnum, puttxbuf);
2087 		ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
2088 		ath_dbg(common, XMIT, "TXDP[%u] = %llx (%p)\n",
2089 			txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
2090 	}
2091 
2092 	if (!edma || sc->tx99_state) {
2093 		TX_STAT_INC(txq->axq_qnum, txstart);
2094 		ath9k_hw_txstart(ah, txq->axq_qnum);
2095 	}
2096 
2097 	if (!internal) {
2098 		while (bf) {
2099 			txq->axq_depth++;
2100 			if (bf_is_ampdu_not_probing(bf))
2101 				txq->axq_ampdu_depth++;
2102 
2103 			bf_last = bf->bf_lastbf;
2104 			bf = bf_last->bf_next;
2105 			bf_last->bf_next = NULL;
2106 		}
2107 	}
2108 }
2109 
ath_tx_send_normal(struct ath_softc * sc,struct ath_txq * txq,struct ath_atx_tid * tid,struct sk_buff * skb)2110 static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
2111 			       struct ath_atx_tid *tid, struct sk_buff *skb)
2112 {
2113 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2114 	struct ath_frame_info *fi = get_frame_info(skb);
2115 	struct list_head bf_head;
2116 	struct ath_buf *bf = fi->bf;
2117 
2118 	INIT_LIST_HEAD(&bf_head);
2119 	list_add_tail(&bf->list, &bf_head);
2120 	bf->bf_state.bf_type = 0;
2121 	if (tid && (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) {
2122 		bf->bf_state.bf_type = BUF_AMPDU;
2123 		ath_tx_addto_baw(sc, tid, bf);
2124 	}
2125 
2126 	bf->bf_next = NULL;
2127 	bf->bf_lastbf = bf;
2128 	ath_tx_fill_desc(sc, bf, txq, fi->framelen);
2129 	ath_tx_txqaddbuf(sc, txq, &bf_head, false);
2130 	TX_STAT_INC(txq->axq_qnum, queued);
2131 }
2132 
setup_frame_info(struct ieee80211_hw * hw,struct ieee80211_sta * sta,struct sk_buff * skb,int framelen)2133 static void setup_frame_info(struct ieee80211_hw *hw,
2134 			     struct ieee80211_sta *sta,
2135 			     struct sk_buff *skb,
2136 			     int framelen)
2137 {
2138 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2139 	struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
2140 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2141 	const struct ieee80211_rate *rate;
2142 	struct ath_frame_info *fi = get_frame_info(skb);
2143 	struct ath_node *an = NULL;
2144 	enum ath9k_key_type keytype;
2145 	bool short_preamble = false;
2146 	u8 txpower;
2147 
2148 	/*
2149 	 * We check if Short Preamble is needed for the CTS rate by
2150 	 * checking the BSS's global flag.
2151 	 * But for the rate series, IEEE80211_TX_RC_USE_SHORT_PREAMBLE is used.
2152 	 */
2153 	if (tx_info->control.vif &&
2154 	    tx_info->control.vif->bss_conf.use_short_preamble)
2155 		short_preamble = true;
2156 
2157 	rate = ieee80211_get_rts_cts_rate(hw, tx_info);
2158 	keytype = ath9k_cmn_get_hw_crypto_keytype(skb);
2159 
2160 	if (sta)
2161 		an = (struct ath_node *) sta->drv_priv;
2162 
2163 	if (tx_info->control.vif) {
2164 		struct ieee80211_vif *vif = tx_info->control.vif;
2165 
2166 		txpower = 2 * vif->bss_conf.txpower;
2167 	} else {
2168 		struct ath_softc *sc = hw->priv;
2169 
2170 		txpower = sc->cur_chan->cur_txpower;
2171 	}
2172 
2173 	memset(fi, 0, sizeof(*fi));
2174 	fi->txq = -1;
2175 	if (hw_key)
2176 		fi->keyix = hw_key->hw_key_idx;
2177 	else if (an && ieee80211_is_data(hdr->frame_control) && an->ps_key > 0)
2178 		fi->keyix = an->ps_key;
2179 	else
2180 		fi->keyix = ATH9K_TXKEYIX_INVALID;
2181 	fi->keytype = keytype;
2182 	fi->framelen = framelen;
2183 	fi->tx_power = txpower;
2184 
2185 	if (!rate)
2186 		return;
2187 	fi->rtscts_rate = rate->hw_value;
2188 	if (short_preamble)
2189 		fi->rtscts_rate |= rate->hw_value_short;
2190 }
2191 
ath_txchainmask_reduction(struct ath_softc * sc,u8 chainmask,u32 rate)2192 u8 ath_txchainmask_reduction(struct ath_softc *sc, u8 chainmask, u32 rate)
2193 {
2194 	struct ath_hw *ah = sc->sc_ah;
2195 	struct ath9k_channel *curchan = ah->curchan;
2196 
2197 	if ((ah->caps.hw_caps & ATH9K_HW_CAP_APM) && IS_CHAN_5GHZ(curchan) &&
2198 	    (chainmask == 0x7) && (rate < 0x90))
2199 		return 0x3;
2200 	else if (AR_SREV_9462(ah) && ath9k_hw_btcoex_is_enabled(ah) &&
2201 		 IS_CCK_RATE(rate))
2202 		return 0x2;
2203 	else
2204 		return chainmask;
2205 }
2206 
2207 /*
2208  * Assign a descriptor (and sequence number if necessary,
2209  * and map buffer for DMA. Frees skb on error
2210  */
ath_tx_setup_buffer(struct ath_softc * sc,struct ath_txq * txq,struct ath_atx_tid * tid,struct sk_buff * skb)2211 static struct ath_buf *ath_tx_setup_buffer(struct ath_softc *sc,
2212 					   struct ath_txq *txq,
2213 					   struct ath_atx_tid *tid,
2214 					   struct sk_buff *skb)
2215 {
2216 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2217 	struct ath_frame_info *fi = get_frame_info(skb);
2218 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2219 	struct ath_buf *bf;
2220 	int fragno;
2221 	u16 seqno;
2222 
2223 	bf = ath_tx_get_buffer(sc);
2224 	if (!bf) {
2225 		ath_dbg(common, XMIT, "TX buffers are full\n");
2226 		return NULL;
2227 	}
2228 
2229 	ATH_TXBUF_RESET(bf);
2230 
2231 	if (tid && ieee80211_is_data_present(hdr->frame_control)) {
2232 		fragno = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
2233 		seqno = tid->seq_next;
2234 		hdr->seq_ctrl = cpu_to_le16(tid->seq_next << IEEE80211_SEQ_SEQ_SHIFT);
2235 
2236 		if (fragno)
2237 			hdr->seq_ctrl |= cpu_to_le16(fragno);
2238 
2239 		if (!ieee80211_has_morefrags(hdr->frame_control))
2240 			INCR(tid->seq_next, IEEE80211_SEQ_MAX);
2241 
2242 		bf->bf_state.seqno = seqno;
2243 	}
2244 
2245 	bf->bf_mpdu = skb;
2246 
2247 	bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
2248 					 skb->len, DMA_TO_DEVICE);
2249 	if (unlikely(dma_mapping_error(sc->dev, bf->bf_buf_addr))) {
2250 		bf->bf_mpdu = NULL;
2251 		bf->bf_buf_addr = 0;
2252 		ath_err(ath9k_hw_common(sc->sc_ah),
2253 			"dma_mapping_error() on TX\n");
2254 		ath_tx_return_buffer(sc, bf);
2255 		return NULL;
2256 	}
2257 
2258 	fi->bf = bf;
2259 
2260 	return bf;
2261 }
2262 
ath_assign_seq(struct ath_common * common,struct sk_buff * skb)2263 void ath_assign_seq(struct ath_common *common, struct sk_buff *skb)
2264 {
2265 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2266 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2267 	struct ieee80211_vif *vif = info->control.vif;
2268 	struct ath_vif *avp;
2269 
2270 	if (!(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
2271 		return;
2272 
2273 	if (!vif)
2274 		return;
2275 
2276 	avp = (struct ath_vif *)vif->drv_priv;
2277 
2278 	if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
2279 		avp->seq_no += 0x10;
2280 
2281 	hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
2282 	hdr->seq_ctrl |= cpu_to_le16(avp->seq_no);
2283 }
2284 
ath_tx_prepare(struct ieee80211_hw * hw,struct sk_buff * skb,struct ath_tx_control * txctl)2285 static int ath_tx_prepare(struct ieee80211_hw *hw, struct sk_buff *skb,
2286 			  struct ath_tx_control *txctl)
2287 {
2288 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2289 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2290 	struct ieee80211_sta *sta = txctl->sta;
2291 	struct ieee80211_vif *vif = info->control.vif;
2292 	struct ath_vif *avp;
2293 	struct ath_softc *sc = hw->priv;
2294 	int frmlen = skb->len + FCS_LEN;
2295 	int padpos, padsize;
2296 
2297 	/* NOTE:  sta can be NULL according to net/mac80211.h */
2298 	if (sta)
2299 		txctl->an = (struct ath_node *)sta->drv_priv;
2300 	else if (vif && ieee80211_is_data(hdr->frame_control)) {
2301 		avp = (void *)vif->drv_priv;
2302 		txctl->an = &avp->mcast_node;
2303 	}
2304 
2305 	if (info->control.hw_key)
2306 		frmlen += info->control.hw_key->icv_len;
2307 
2308 	ath_assign_seq(ath9k_hw_common(sc->sc_ah), skb);
2309 
2310 	if ((vif && vif->type != NL80211_IFTYPE_AP &&
2311 	            vif->type != NL80211_IFTYPE_AP_VLAN) ||
2312 	    !ieee80211_is_data(hdr->frame_control))
2313 		info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
2314 
2315 	/* Add the padding after the header if this is not already done */
2316 	padpos = ieee80211_hdrlen(hdr->frame_control);
2317 	padsize = padpos & 3;
2318 	if (padsize && skb->len > padpos) {
2319 		if (skb_headroom(skb) < padsize)
2320 			return -ENOMEM;
2321 
2322 		skb_push(skb, padsize);
2323 		memmove(skb->data, skb->data + padsize, padpos);
2324 	}
2325 
2326 	setup_frame_info(hw, sta, skb, frmlen);
2327 	return 0;
2328 }
2329 
2330 
2331 /* Upon failure caller should free skb */
ath_tx_start(struct ieee80211_hw * hw,struct sk_buff * skb,struct ath_tx_control * txctl)2332 int ath_tx_start(struct ieee80211_hw *hw, struct sk_buff *skb,
2333 		 struct ath_tx_control *txctl)
2334 {
2335 	struct ieee80211_hdr *hdr;
2336 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2337 	struct ieee80211_sta *sta = txctl->sta;
2338 	struct ieee80211_vif *vif = info->control.vif;
2339 	struct ath_frame_info *fi = get_frame_info(skb);
2340 	struct ath_vif *avp = NULL;
2341 	struct ath_softc *sc = hw->priv;
2342 	struct ath_txq *txq = txctl->txq;
2343 	struct ath_atx_tid *tid = NULL;
2344 	struct ath_node *an = NULL;
2345 	struct ath_buf *bf;
2346 	bool ps_resp;
2347 	int q, ret;
2348 
2349 	if (vif)
2350 		avp = (void *)vif->drv_priv;
2351 
2352 	ps_resp = !!(info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE);
2353 
2354 	ret = ath_tx_prepare(hw, skb, txctl);
2355 	if (ret)
2356 	    return ret;
2357 
2358 	hdr = (struct ieee80211_hdr *) skb->data;
2359 	/*
2360 	 * At this point, the vif, hw_key and sta pointers in the tx control
2361 	 * info are no longer valid (overwritten by the ath_frame_info data.
2362 	 */
2363 
2364 	q = skb_get_queue_mapping(skb);
2365 
2366 	if (ps_resp)
2367 		txq = sc->tx.uapsdq;
2368 
2369 	if (txctl->sta) {
2370 		an = (struct ath_node *) sta->drv_priv;
2371 		tid = ath_get_skb_tid(sc, an, skb);
2372 	}
2373 
2374 	ath_txq_lock(sc, txq);
2375 	if (txq == sc->tx.txq_map[q]) {
2376 		fi->txq = q;
2377 		++txq->pending_frames;
2378 	}
2379 
2380 	bf = ath_tx_setup_buffer(sc, txq, tid, skb);
2381 	if (!bf) {
2382 		ath_txq_skb_done(sc, txq, skb);
2383 		if (txctl->paprd)
2384 			dev_kfree_skb_any(skb);
2385 		else
2386 			ieee80211_free_txskb(sc->hw, skb);
2387 		goto out;
2388 	}
2389 
2390 	bf->bf_state.bfs_paprd = txctl->paprd;
2391 
2392 	if (txctl->paprd)
2393 		bf->bf_state.bfs_paprd_timestamp = jiffies;
2394 
2395 	ath_set_rates(vif, sta, bf);
2396 	ath_tx_send_normal(sc, txq, tid, skb);
2397 
2398 out:
2399 	ath_txq_unlock(sc, txq);
2400 
2401 	return 0;
2402 }
2403 
ath_tx_cabq(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct sk_buff * skb)2404 void ath_tx_cabq(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2405 		 struct sk_buff *skb)
2406 {
2407 	struct ath_softc *sc = hw->priv;
2408 	struct ath_tx_control txctl = {
2409 		.txq = sc->beacon.cabq
2410 	};
2411 	struct ath_tx_info info = {};
2412 	struct ieee80211_hdr *hdr;
2413 	struct ath_buf *bf_tail = NULL;
2414 	struct ath_buf *bf;
2415 	LIST_HEAD(bf_q);
2416 	int duration = 0;
2417 	int max_duration;
2418 
2419 	max_duration =
2420 		sc->cur_chan->beacon.beacon_interval * 1000 *
2421 		sc->cur_chan->beacon.dtim_period / ATH_BCBUF;
2422 
2423 	do {
2424 		struct ath_frame_info *fi = get_frame_info(skb);
2425 
2426 		if (ath_tx_prepare(hw, skb, &txctl))
2427 			break;
2428 
2429 		bf = ath_tx_setup_buffer(sc, txctl.txq, NULL, skb);
2430 		if (!bf)
2431 			break;
2432 
2433 		bf->bf_lastbf = bf;
2434 		ath_set_rates(vif, NULL, bf);
2435 		ath_buf_set_rate(sc, bf, &info, fi->framelen, false);
2436 		duration += info.rates[0].PktDuration;
2437 		if (bf_tail)
2438 			bf_tail->bf_next = bf;
2439 
2440 		list_add_tail(&bf->list, &bf_q);
2441 		bf_tail = bf;
2442 		skb = NULL;
2443 
2444 		if (duration > max_duration)
2445 			break;
2446 
2447 		skb = ieee80211_get_buffered_bc(hw, vif);
2448 	} while(skb);
2449 
2450 	if (skb)
2451 		ieee80211_free_txskb(hw, skb);
2452 
2453 	if (list_empty(&bf_q))
2454 		return;
2455 
2456 	bf = list_first_entry(&bf_q, struct ath_buf, list);
2457 	hdr = (struct ieee80211_hdr *) bf->bf_mpdu->data;
2458 
2459 	if (hdr->frame_control & cpu_to_le16(IEEE80211_FCTL_MOREDATA)) {
2460 		hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2461 		dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
2462 			sizeof(*hdr), DMA_TO_DEVICE);
2463 	}
2464 
2465 	ath_txq_lock(sc, txctl.txq);
2466 	ath_tx_fill_desc(sc, bf, txctl.txq, 0);
2467 	ath_tx_txqaddbuf(sc, txctl.txq, &bf_q, false);
2468 	TX_STAT_INC(txctl.txq->axq_qnum, queued);
2469 	ath_txq_unlock(sc, txctl.txq);
2470 }
2471 
2472 /*****************/
2473 /* TX Completion */
2474 /*****************/
2475 
ath_tx_complete(struct ath_softc * sc,struct sk_buff * skb,int tx_flags,struct ath_txq * txq,struct ieee80211_sta * sta)2476 static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
2477 			    int tx_flags, struct ath_txq *txq,
2478 			    struct ieee80211_sta *sta)
2479 {
2480 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2481 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2482 	struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
2483 	int padpos, padsize;
2484 	unsigned long flags;
2485 
2486 	ath_dbg(common, XMIT, "TX complete: skb: %p\n", skb);
2487 
2488 	if (sc->sc_ah->caldata)
2489 		set_bit(PAPRD_PACKET_SENT, &sc->sc_ah->caldata->cal_flags);
2490 
2491 	if (!(tx_flags & ATH_TX_ERROR)) {
2492 		if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
2493 			tx_info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
2494 		else
2495 			tx_info->flags |= IEEE80211_TX_STAT_ACK;
2496 	}
2497 
2498 	if (tx_info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) {
2499 		padpos = ieee80211_hdrlen(hdr->frame_control);
2500 		padsize = padpos & 3;
2501 		if (padsize && skb->len>padpos+padsize) {
2502 			/*
2503 			 * Remove MAC header padding before giving the frame back to
2504 			 * mac80211.
2505 			 */
2506 			memmove(skb->data + padsize, skb->data, padpos);
2507 			skb_pull(skb, padsize);
2508 		}
2509 	}
2510 
2511 	spin_lock_irqsave(&sc->sc_pm_lock, flags);
2512 	if ((sc->ps_flags & PS_WAIT_FOR_TX_ACK) && !txq->axq_depth) {
2513 		sc->ps_flags &= ~PS_WAIT_FOR_TX_ACK;
2514 		ath_dbg(common, PS,
2515 			"Going back to sleep after having received TX status (0x%lx)\n",
2516 			sc->ps_flags & (PS_WAIT_FOR_BEACON |
2517 					PS_WAIT_FOR_CAB |
2518 					PS_WAIT_FOR_PSPOLL_DATA |
2519 					PS_WAIT_FOR_TX_ACK));
2520 	}
2521 	spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
2522 
2523 	ath_txq_skb_done(sc, txq, skb);
2524 	tx_info->status.status_driver_data[0] = sta;
2525 	__skb_queue_tail(&txq->complete_q, skb);
2526 }
2527 
ath_tx_complete_buf(struct ath_softc * sc,struct ath_buf * bf,struct ath_txq * txq,struct list_head * bf_q,struct ieee80211_sta * sta,struct ath_tx_status * ts,int txok)2528 static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
2529 				struct ath_txq *txq, struct list_head *bf_q,
2530 				struct ieee80211_sta *sta,
2531 				struct ath_tx_status *ts, int txok)
2532 {
2533 	struct sk_buff *skb = bf->bf_mpdu;
2534 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2535 	unsigned long flags;
2536 	int tx_flags = 0;
2537 
2538 	if (!txok)
2539 		tx_flags |= ATH_TX_ERROR;
2540 
2541 	if (ts->ts_status & ATH9K_TXERR_FILT)
2542 		tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
2543 
2544 	dma_unmap_single(sc->dev, bf->bf_buf_addr, skb->len, DMA_TO_DEVICE);
2545 	bf->bf_buf_addr = 0;
2546 	if (sc->tx99_state)
2547 		goto skip_tx_complete;
2548 
2549 	if (bf->bf_state.bfs_paprd) {
2550 		if (time_after(jiffies,
2551 				bf->bf_state.bfs_paprd_timestamp +
2552 				msecs_to_jiffies(ATH_PAPRD_TIMEOUT)))
2553 			dev_kfree_skb_any(skb);
2554 		else
2555 			complete(&sc->paprd_complete);
2556 	} else {
2557 		ath_debug_stat_tx(sc, bf, ts, txq, tx_flags);
2558 		ath_tx_complete(sc, skb, tx_flags, txq, sta);
2559 	}
2560 skip_tx_complete:
2561 	/* At this point, skb (bf->bf_mpdu) is consumed...make sure we don't
2562 	 * accidentally reference it later.
2563 	 */
2564 	bf->bf_mpdu = NULL;
2565 
2566 	/*
2567 	 * Return the list of ath_buf of this mpdu to free queue
2568 	 */
2569 	spin_lock_irqsave(&sc->tx.txbuflock, flags);
2570 	list_splice_tail_init(bf_q, &sc->tx.txbuf);
2571 	spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
2572 }
2573 
ath_tx_rc_status(struct ath_softc * sc,struct ath_buf * bf,struct ath_tx_status * ts,int nframes,int nbad,int txok)2574 static void ath_tx_rc_status(struct ath_softc *sc, struct ath_buf *bf,
2575 			     struct ath_tx_status *ts, int nframes, int nbad,
2576 			     int txok)
2577 {
2578 	struct sk_buff *skb = bf->bf_mpdu;
2579 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2580 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2581 	struct ieee80211_hw *hw = sc->hw;
2582 	struct ath_hw *ah = sc->sc_ah;
2583 	u8 i, tx_rateindex;
2584 
2585 	if (txok)
2586 		tx_info->status.ack_signal = ts->ts_rssi;
2587 
2588 	tx_rateindex = ts->ts_rateindex;
2589 	WARN_ON(tx_rateindex >= hw->max_rates);
2590 
2591 	if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
2592 		tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
2593 
2594 		BUG_ON(nbad > nframes);
2595 	}
2596 	tx_info->status.ampdu_len = nframes;
2597 	tx_info->status.ampdu_ack_len = nframes - nbad;
2598 
2599 	if ((ts->ts_status & ATH9K_TXERR_FILT) == 0 &&
2600 	    (tx_info->flags & IEEE80211_TX_CTL_NO_ACK) == 0) {
2601 		/*
2602 		 * If an underrun error is seen assume it as an excessive
2603 		 * retry only if max frame trigger level has been reached
2604 		 * (2 KB for single stream, and 4 KB for dual stream).
2605 		 * Adjust the long retry as if the frame was tried
2606 		 * hw->max_rate_tries times to affect how rate control updates
2607 		 * PER for the failed rate.
2608 		 * In case of congestion on the bus penalizing this type of
2609 		 * underruns should help hardware actually transmit new frames
2610 		 * successfully by eventually preferring slower rates.
2611 		 * This itself should also alleviate congestion on the bus.
2612 		 */
2613 		if (unlikely(ts->ts_flags & (ATH9K_TX_DATA_UNDERRUN |
2614 		                             ATH9K_TX_DELIM_UNDERRUN)) &&
2615 		    ieee80211_is_data(hdr->frame_control) &&
2616 		    ah->tx_trig_level >= sc->sc_ah->config.max_txtrig_level)
2617 			tx_info->status.rates[tx_rateindex].count =
2618 				hw->max_rate_tries;
2619 	}
2620 
2621 	for (i = tx_rateindex + 1; i < hw->max_rates; i++) {
2622 		tx_info->status.rates[i].count = 0;
2623 		tx_info->status.rates[i].idx = -1;
2624 	}
2625 
2626 	tx_info->status.rates[tx_rateindex].count = ts->ts_longretry + 1;
2627 }
2628 
ath_tx_processq(struct ath_softc * sc,struct ath_txq * txq)2629 static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
2630 {
2631 	struct ath_hw *ah = sc->sc_ah;
2632 	struct ath_common *common = ath9k_hw_common(ah);
2633 	struct ath_buf *bf, *lastbf, *bf_held = NULL;
2634 	struct list_head bf_head;
2635 	struct ath_desc *ds;
2636 	struct ath_tx_status ts;
2637 	int status;
2638 
2639 	ath_dbg(common, QUEUE, "tx queue %d (%x), link %p\n",
2640 		txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
2641 		txq->axq_link);
2642 
2643 	ath_txq_lock(sc, txq);
2644 	for (;;) {
2645 		if (test_bit(ATH_OP_HW_RESET, &common->op_flags))
2646 			break;
2647 
2648 		if (list_empty(&txq->axq_q)) {
2649 			txq->axq_link = NULL;
2650 			ath_txq_schedule(sc, txq);
2651 			break;
2652 		}
2653 		bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
2654 
2655 		/*
2656 		 * There is a race condition that a BH gets scheduled
2657 		 * after sw writes TxE and before hw re-load the last
2658 		 * descriptor to get the newly chained one.
2659 		 * Software must keep the last DONE descriptor as a
2660 		 * holding descriptor - software does so by marking
2661 		 * it with the STALE flag.
2662 		 */
2663 		bf_held = NULL;
2664 		if (bf->bf_state.stale) {
2665 			bf_held = bf;
2666 			if (list_is_last(&bf_held->list, &txq->axq_q))
2667 				break;
2668 
2669 			bf = list_entry(bf_held->list.next, struct ath_buf,
2670 					list);
2671 		}
2672 
2673 		lastbf = bf->bf_lastbf;
2674 		ds = lastbf->bf_desc;
2675 
2676 		memset(&ts, 0, sizeof(ts));
2677 		status = ath9k_hw_txprocdesc(ah, ds, &ts);
2678 		if (status == -EINPROGRESS)
2679 			break;
2680 
2681 		TX_STAT_INC(txq->axq_qnum, txprocdesc);
2682 
2683 		/*
2684 		 * Remove ath_buf's of the same transmit unit from txq,
2685 		 * however leave the last descriptor back as the holding
2686 		 * descriptor for hw.
2687 		 */
2688 		lastbf->bf_state.stale = true;
2689 		INIT_LIST_HEAD(&bf_head);
2690 		if (!list_is_singular(&lastbf->list))
2691 			list_cut_position(&bf_head,
2692 				&txq->axq_q, lastbf->list.prev);
2693 
2694 		if (bf_held) {
2695 			list_del(&bf_held->list);
2696 			ath_tx_return_buffer(sc, bf_held);
2697 		}
2698 
2699 		ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
2700 	}
2701 	ath_txq_unlock_complete(sc, txq);
2702 }
2703 
ath_tx_tasklet(struct ath_softc * sc)2704 void ath_tx_tasklet(struct ath_softc *sc)
2705 {
2706 	struct ath_hw *ah = sc->sc_ah;
2707 	u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1) & ah->intr_txqs;
2708 	int i;
2709 
2710 	rcu_read_lock();
2711 	for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2712 		if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
2713 			ath_tx_processq(sc, &sc->tx.txq[i]);
2714 	}
2715 	rcu_read_unlock();
2716 }
2717 
ath_tx_edma_tasklet(struct ath_softc * sc)2718 void ath_tx_edma_tasklet(struct ath_softc *sc)
2719 {
2720 	struct ath_tx_status ts;
2721 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2722 	struct ath_hw *ah = sc->sc_ah;
2723 	struct ath_txq *txq;
2724 	struct ath_buf *bf, *lastbf;
2725 	struct list_head bf_head;
2726 	struct list_head *fifo_list;
2727 	int status;
2728 
2729 	rcu_read_lock();
2730 	for (;;) {
2731 		if (test_bit(ATH_OP_HW_RESET, &common->op_flags))
2732 			break;
2733 
2734 		status = ath9k_hw_txprocdesc(ah, NULL, (void *)&ts);
2735 		if (status == -EINPROGRESS)
2736 			break;
2737 		if (status == -EIO) {
2738 			ath_dbg(common, XMIT, "Error processing tx status\n");
2739 			break;
2740 		}
2741 
2742 		/* Process beacon completions separately */
2743 		if (ts.qid == sc->beacon.beaconq) {
2744 			sc->beacon.tx_processed = true;
2745 			sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
2746 
2747 			if (ath9k_is_chanctx_enabled()) {
2748 				ath_chanctx_event(sc, NULL,
2749 						  ATH_CHANCTX_EVENT_BEACON_SENT);
2750 			}
2751 
2752 			ath9k_csa_update(sc);
2753 			continue;
2754 		}
2755 
2756 		txq = &sc->tx.txq[ts.qid];
2757 
2758 		ath_txq_lock(sc, txq);
2759 
2760 		TX_STAT_INC(txq->axq_qnum, txprocdesc);
2761 
2762 		fifo_list = &txq->txq_fifo[txq->txq_tailidx];
2763 		if (list_empty(fifo_list)) {
2764 			ath_txq_unlock(sc, txq);
2765 			break;
2766 		}
2767 
2768 		bf = list_first_entry(fifo_list, struct ath_buf, list);
2769 		if (bf->bf_state.stale) {
2770 			list_del(&bf->list);
2771 			ath_tx_return_buffer(sc, bf);
2772 			bf = list_first_entry(fifo_list, struct ath_buf, list);
2773 		}
2774 
2775 		lastbf = bf->bf_lastbf;
2776 
2777 		INIT_LIST_HEAD(&bf_head);
2778 		if (list_is_last(&lastbf->list, fifo_list)) {
2779 			list_splice_tail_init(fifo_list, &bf_head);
2780 			INCR(txq->txq_tailidx, ATH_TXFIFO_DEPTH);
2781 
2782 			if (!list_empty(&txq->axq_q)) {
2783 				struct list_head bf_q;
2784 
2785 				INIT_LIST_HEAD(&bf_q);
2786 				txq->axq_link = NULL;
2787 				list_splice_tail_init(&txq->axq_q, &bf_q);
2788 				ath_tx_txqaddbuf(sc, txq, &bf_q, true);
2789 			}
2790 		} else {
2791 			lastbf->bf_state.stale = true;
2792 			if (bf != lastbf)
2793 				list_cut_position(&bf_head, fifo_list,
2794 						  lastbf->list.prev);
2795 		}
2796 
2797 		ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
2798 		ath_txq_unlock_complete(sc, txq);
2799 	}
2800 	rcu_read_unlock();
2801 }
2802 
2803 /*****************/
2804 /* Init, Cleanup */
2805 /*****************/
2806 
ath_txstatus_setup(struct ath_softc * sc,int size)2807 static int ath_txstatus_setup(struct ath_softc *sc, int size)
2808 {
2809 	struct ath_descdma *dd = &sc->txsdma;
2810 	u8 txs_len = sc->sc_ah->caps.txs_len;
2811 
2812 	dd->dd_desc_len = size * txs_len;
2813 	dd->dd_desc = dmam_alloc_coherent(sc->dev, dd->dd_desc_len,
2814 					  &dd->dd_desc_paddr, GFP_KERNEL);
2815 	if (!dd->dd_desc)
2816 		return -ENOMEM;
2817 
2818 	return 0;
2819 }
2820 
ath_tx_edma_init(struct ath_softc * sc)2821 static int ath_tx_edma_init(struct ath_softc *sc)
2822 {
2823 	int err;
2824 
2825 	err = ath_txstatus_setup(sc, ATH_TXSTATUS_RING_SIZE);
2826 	if (!err)
2827 		ath9k_hw_setup_statusring(sc->sc_ah, sc->txsdma.dd_desc,
2828 					  sc->txsdma.dd_desc_paddr,
2829 					  ATH_TXSTATUS_RING_SIZE);
2830 
2831 	return err;
2832 }
2833 
ath_tx_init(struct ath_softc * sc,int nbufs)2834 int ath_tx_init(struct ath_softc *sc, int nbufs)
2835 {
2836 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2837 	int error = 0;
2838 
2839 	spin_lock_init(&sc->tx.txbuflock);
2840 
2841 	error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
2842 				  "tx", nbufs, 1, 1);
2843 	if (error != 0) {
2844 		ath_err(common,
2845 			"Failed to allocate tx descriptors: %d\n", error);
2846 		return error;
2847 	}
2848 
2849 	error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
2850 				  "beacon", ATH_BCBUF, 1, 1);
2851 	if (error != 0) {
2852 		ath_err(common,
2853 			"Failed to allocate beacon descriptors: %d\n", error);
2854 		return error;
2855 	}
2856 
2857 	if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
2858 		error = ath_tx_edma_init(sc);
2859 
2860 	return error;
2861 }
2862 
ath_tx_node_init(struct ath_softc * sc,struct ath_node * an)2863 void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
2864 {
2865 	struct ath_atx_tid *tid;
2866 	int tidno, acno;
2867 
2868 	for (acno = 0; acno < IEEE80211_NUM_ACS; acno++)
2869 		an->airtime_deficit[acno] = ATH_AIRTIME_QUANTUM;
2870 
2871 	for (tidno = 0; tidno < IEEE80211_NUM_TIDS; tidno++) {
2872 		tid = ath_node_to_tid(an, tidno);
2873 		tid->an        = an;
2874 		tid->tidno     = tidno;
2875 		tid->seq_start = tid->seq_next = 0;
2876 		tid->baw_size  = WME_MAX_BA;
2877 		tid->baw_head  = tid->baw_tail = 0;
2878 		tid->active	   = false;
2879 		tid->clear_ps_filter = true;
2880 		tid->has_queued  = false;
2881 		__skb_queue_head_init(&tid->retry_q);
2882 		INIT_LIST_HEAD(&tid->list);
2883 		acno = TID_TO_WME_AC(tidno);
2884 		tid->txq = sc->tx.txq_map[acno];
2885 
2886 		if (!an->sta)
2887 			break; /* just one multicast ath_atx_tid */
2888 	}
2889 }
2890 
ath_tx_node_cleanup(struct ath_softc * sc,struct ath_node * an)2891 void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
2892 {
2893 	struct ath_atx_tid *tid;
2894 	struct ath_txq *txq;
2895 	int tidno;
2896 
2897 	rcu_read_lock();
2898 
2899 	for (tidno = 0; tidno < IEEE80211_NUM_TIDS; tidno++) {
2900 		tid = ath_node_to_tid(an, tidno);
2901 		txq = tid->txq;
2902 
2903 		ath_txq_lock(sc, txq);
2904 
2905 		if (!list_empty(&tid->list))
2906 			list_del_init(&tid->list);
2907 
2908 		ath_tid_drain(sc, txq, tid);
2909 		tid->active = false;
2910 
2911 		ath_txq_unlock(sc, txq);
2912 
2913 		if (!an->sta)
2914 			break; /* just one multicast ath_atx_tid */
2915 	}
2916 
2917 	rcu_read_unlock();
2918 }
2919 
2920 #ifdef CONFIG_ATH9K_TX99
2921 
ath9k_tx99_send(struct ath_softc * sc,struct sk_buff * skb,struct ath_tx_control * txctl)2922 int ath9k_tx99_send(struct ath_softc *sc, struct sk_buff *skb,
2923 		    struct ath_tx_control *txctl)
2924 {
2925 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2926 	struct ath_frame_info *fi = get_frame_info(skb);
2927 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2928 	struct ath_buf *bf;
2929 	int padpos, padsize;
2930 
2931 	padpos = ieee80211_hdrlen(hdr->frame_control);
2932 	padsize = padpos & 3;
2933 
2934 	if (padsize && skb->len > padpos) {
2935 		if (skb_headroom(skb) < padsize) {
2936 			ath_dbg(common, XMIT,
2937 				"tx99 padding failed\n");
2938 			return -EINVAL;
2939 		}
2940 
2941 		skb_push(skb, padsize);
2942 		memmove(skb->data, skb->data + padsize, padpos);
2943 	}
2944 
2945 	fi->keyix = ATH9K_TXKEYIX_INVALID;
2946 	fi->framelen = skb->len + FCS_LEN;
2947 	fi->keytype = ATH9K_KEY_TYPE_CLEAR;
2948 
2949 	bf = ath_tx_setup_buffer(sc, txctl->txq, NULL, skb);
2950 	if (!bf) {
2951 		ath_dbg(common, XMIT, "tx99 buffer setup failed\n");
2952 		return -EINVAL;
2953 	}
2954 
2955 	ath_set_rates(sc->tx99_vif, NULL, bf);
2956 
2957 	ath9k_hw_set_desc_link(sc->sc_ah, bf->bf_desc, bf->bf_daddr);
2958 	ath9k_hw_tx99_start(sc->sc_ah, txctl->txq->axq_qnum);
2959 
2960 	ath_tx_send_normal(sc, txctl->txq, NULL, skb);
2961 
2962 	return 0;
2963 }
2964 
2965 #endif /* CONFIG_ATH9K_TX99 */
2966