xmit.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183
  1. /*
  2. * Copyright (c) 2008-2009 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. #include "ath9k.h"
  17. #define BITS_PER_BYTE 8
  18. #define OFDM_PLCP_BITS 22
  19. #define HT_RC_2_MCS(_rc) ((_rc) & 0x0f)
  20. #define HT_RC_2_STREAMS(_rc) ((((_rc) & 0x78) >> 3) + 1)
  21. #define L_STF 8
  22. #define L_LTF 8
  23. #define L_SIG 4
  24. #define HT_SIG 8
  25. #define HT_STF 4
  26. #define HT_LTF(_ns) (4 * (_ns))
  27. #define SYMBOL_TIME(_ns) ((_ns) << 2) /* ns * 4 us */
  28. #define SYMBOL_TIME_HALFGI(_ns) (((_ns) * 18 + 4) / 5) /* ns * 3.6 us */
  29. #define NUM_SYMBOLS_PER_USEC(_usec) (_usec >> 2)
  30. #define NUM_SYMBOLS_PER_USEC_HALFGI(_usec) (((_usec*5)-4)/18)
  31. #define OFDM_SIFS_TIME 16
  32. static u32 bits_per_symbol[][2] = {
  33. /* 20MHz 40MHz */
  34. { 26, 54 }, /* 0: BPSK */
  35. { 52, 108 }, /* 1: QPSK 1/2 */
  36. { 78, 162 }, /* 2: QPSK 3/4 */
  37. { 104, 216 }, /* 3: 16-QAM 1/2 */
  38. { 156, 324 }, /* 4: 16-QAM 3/4 */
  39. { 208, 432 }, /* 5: 64-QAM 2/3 */
  40. { 234, 486 }, /* 6: 64-QAM 3/4 */
  41. { 260, 540 }, /* 7: 64-QAM 5/6 */
  42. { 52, 108 }, /* 8: BPSK */
  43. { 104, 216 }, /* 9: QPSK 1/2 */
  44. { 156, 324 }, /* 10: QPSK 3/4 */
  45. { 208, 432 }, /* 11: 16-QAM 1/2 */
  46. { 312, 648 }, /* 12: 16-QAM 3/4 */
  47. { 416, 864 }, /* 13: 64-QAM 2/3 */
  48. { 468, 972 }, /* 14: 64-QAM 3/4 */
  49. { 520, 1080 }, /* 15: 64-QAM 5/6 */
  50. };
  51. #define IS_HT_RATE(_rate) ((_rate) & 0x80)
  52. static void ath_tx_send_ht_normal(struct ath_softc *sc, struct ath_txq *txq,
  53. struct ath_atx_tid *tid,
  54. struct list_head *bf_head);
  55. static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
  56. struct list_head *bf_q,
  57. int txok, int sendbar);
  58. static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
  59. struct list_head *head);
  60. static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf);
  61. static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
  62. int txok);
  63. static void ath_tx_rc_status(struct ath_buf *bf, struct ath_desc *ds,
  64. int nbad, int txok, bool update_rc);
  65. /*********************/
  66. /* Aggregation logic */
  67. /*********************/
  68. static int ath_aggr_query(struct ath_softc *sc, struct ath_node *an, u8 tidno)
  69. {
  70. struct ath_atx_tid *tid;
  71. tid = ATH_AN_2_TID(an, tidno);
  72. if (tid->state & AGGR_ADDBA_COMPLETE ||
  73. tid->state & AGGR_ADDBA_PROGRESS)
  74. return 1;
  75. else
  76. return 0;
  77. }
  78. static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
  79. {
  80. struct ath_atx_ac *ac = tid->ac;
  81. if (tid->paused)
  82. return;
  83. if (tid->sched)
  84. return;
  85. tid->sched = true;
  86. list_add_tail(&tid->list, &ac->tid_q);
  87. if (ac->sched)
  88. return;
  89. ac->sched = true;
  90. list_add_tail(&ac->list, &txq->axq_acq);
  91. }
  92. static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
  93. {
  94. struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
  95. spin_lock_bh(&txq->axq_lock);
  96. tid->paused++;
  97. spin_unlock_bh(&txq->axq_lock);
  98. }
  99. static void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
  100. {
  101. struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
  102. ASSERT(tid->paused > 0);
  103. spin_lock_bh(&txq->axq_lock);
  104. tid->paused--;
  105. if (tid->paused > 0)
  106. goto unlock;
  107. if (list_empty(&tid->buf_q))
  108. goto unlock;
  109. ath_tx_queue_tid(txq, tid);
  110. ath_txq_schedule(sc, txq);
  111. unlock:
  112. spin_unlock_bh(&txq->axq_lock);
  113. }
  114. static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
  115. {
  116. struct ath_txq *txq = &sc->tx.txq[tid->ac->qnum];
  117. struct ath_buf *bf;
  118. struct list_head bf_head;
  119. INIT_LIST_HEAD(&bf_head);
  120. ASSERT(tid->paused > 0);
  121. spin_lock_bh(&txq->axq_lock);
  122. tid->paused--;
  123. if (tid->paused > 0) {
  124. spin_unlock_bh(&txq->axq_lock);
  125. return;
  126. }
  127. while (!list_empty(&tid->buf_q)) {
  128. bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
  129. ASSERT(!bf_isretried(bf));
  130. list_move_tail(&bf->list, &bf_head);
  131. ath_tx_send_ht_normal(sc, txq, tid, &bf_head);
  132. }
  133. spin_unlock_bh(&txq->axq_lock);
  134. }
  135. static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
  136. int seqno)
  137. {
  138. int index, cindex;
  139. index = ATH_BA_INDEX(tid->seq_start, seqno);
  140. cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
  141. tid->tx_buf[cindex] = NULL;
  142. while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
  143. INCR(tid->seq_start, IEEE80211_SEQ_MAX);
  144. INCR(tid->baw_head, ATH_TID_MAX_BUFS);
  145. }
  146. }
  147. static void ath_tx_addto_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
  148. struct ath_buf *bf)
  149. {
  150. int index, cindex;
  151. if (bf_isretried(bf))
  152. return;
  153. index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
  154. cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
  155. ASSERT(tid->tx_buf[cindex] == NULL);
  156. tid->tx_buf[cindex] = bf;
  157. if (index >= ((tid->baw_tail - tid->baw_head) &
  158. (ATH_TID_MAX_BUFS - 1))) {
  159. tid->baw_tail = cindex;
  160. INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
  161. }
  162. }
  163. /*
  164. * TODO: For frame(s) that are in the retry state, we will reuse the
  165. * sequence number(s) without setting the retry bit. The
  166. * alternative is to give up on these and BAR the receiver's window
  167. * forward.
  168. */
  169. static void ath_tid_drain(struct ath_softc *sc, struct ath_txq *txq,
  170. struct ath_atx_tid *tid)
  171. {
  172. struct ath_buf *bf;
  173. struct list_head bf_head;
  174. INIT_LIST_HEAD(&bf_head);
  175. for (;;) {
  176. if (list_empty(&tid->buf_q))
  177. break;
  178. bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
  179. list_move_tail(&bf->list, &bf_head);
  180. if (bf_isretried(bf))
  181. ath_tx_update_baw(sc, tid, bf->bf_seqno);
  182. spin_unlock(&txq->axq_lock);
  183. ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
  184. spin_lock(&txq->axq_lock);
  185. }
  186. tid->seq_next = tid->seq_start;
  187. tid->baw_tail = tid->baw_head;
  188. }
  189. static void ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
  190. {
  191. struct sk_buff *skb;
  192. struct ieee80211_hdr *hdr;
  193. bf->bf_state.bf_type |= BUF_RETRY;
  194. bf->bf_retries++;
  195. skb = bf->bf_mpdu;
  196. hdr = (struct ieee80211_hdr *)skb->data;
  197. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
  198. }
  199. static struct ath_buf* ath_clone_txbuf(struct ath_softc *sc, struct ath_buf *bf)
  200. {
  201. struct ath_buf *tbf;
  202. spin_lock_bh(&sc->tx.txbuflock);
  203. ASSERT(!list_empty((&sc->tx.txbuf)));
  204. tbf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
  205. list_del(&tbf->list);
  206. spin_unlock_bh(&sc->tx.txbuflock);
  207. ATH_TXBUF_RESET(tbf);
  208. tbf->bf_mpdu = bf->bf_mpdu;
  209. tbf->bf_buf_addr = bf->bf_buf_addr;
  210. *(tbf->bf_desc) = *(bf->bf_desc);
  211. tbf->bf_state = bf->bf_state;
  212. tbf->bf_dmacontext = bf->bf_dmacontext;
  213. return tbf;
  214. }
  215. static void ath_tx_complete_aggr(struct ath_softc *sc, struct ath_txq *txq,
  216. struct ath_buf *bf, struct list_head *bf_q,
  217. int txok)
  218. {
  219. struct ath_node *an = NULL;
  220. struct sk_buff *skb;
  221. struct ieee80211_sta *sta;
  222. struct ieee80211_hdr *hdr;
  223. struct ath_atx_tid *tid = NULL;
  224. struct ath_buf *bf_next, *bf_last = bf->bf_lastbf;
  225. struct ath_desc *ds = bf_last->bf_desc;
  226. struct list_head bf_head, bf_pending;
  227. u16 seq_st = 0, acked_cnt = 0, txfail_cnt = 0;
  228. u32 ba[WME_BA_BMP_SIZE >> 5];
  229. int isaggr, txfail, txpending, sendbar = 0, needreset = 0, nbad = 0;
  230. bool rc_update = true;
  231. skb = bf->bf_mpdu;
  232. hdr = (struct ieee80211_hdr *)skb->data;
  233. rcu_read_lock();
  234. sta = ieee80211_find_sta(sc->hw, hdr->addr1);
  235. if (!sta) {
  236. rcu_read_unlock();
  237. return;
  238. }
  239. an = (struct ath_node *)sta->drv_priv;
  240. tid = ATH_AN_2_TID(an, bf->bf_tidno);
  241. isaggr = bf_isaggr(bf);
  242. memset(ba, 0, WME_BA_BMP_SIZE >> 3);
  243. if (isaggr && txok) {
  244. if (ATH_DS_TX_BA(ds)) {
  245. seq_st = ATH_DS_BA_SEQ(ds);
  246. memcpy(ba, ATH_DS_BA_BITMAP(ds),
  247. WME_BA_BMP_SIZE >> 3);
  248. } else {
  249. /*
  250. * AR5416 can become deaf/mute when BA
  251. * issue happens. Chip needs to be reset.
  252. * But AP code may have sychronization issues
  253. * when perform internal reset in this routine.
  254. * Only enable reset in STA mode for now.
  255. */
  256. if (sc->sc_ah->opmode == NL80211_IFTYPE_STATION)
  257. needreset = 1;
  258. }
  259. }
  260. INIT_LIST_HEAD(&bf_pending);
  261. INIT_LIST_HEAD(&bf_head);
  262. nbad = ath_tx_num_badfrms(sc, bf, txok);
  263. while (bf) {
  264. txfail = txpending = 0;
  265. bf_next = bf->bf_next;
  266. if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
  267. /* transmit completion, subframe is
  268. * acked by block ack */
  269. acked_cnt++;
  270. } else if (!isaggr && txok) {
  271. /* transmit completion */
  272. acked_cnt++;
  273. } else {
  274. if (!(tid->state & AGGR_CLEANUP) &&
  275. ds->ds_txstat.ts_flags != ATH9K_TX_SW_ABORTED) {
  276. if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
  277. ath_tx_set_retry(sc, bf);
  278. txpending = 1;
  279. } else {
  280. bf->bf_state.bf_type |= BUF_XRETRY;
  281. txfail = 1;
  282. sendbar = 1;
  283. txfail_cnt++;
  284. }
  285. } else {
  286. /*
  287. * cleanup in progress, just fail
  288. * the un-acked sub-frames
  289. */
  290. txfail = 1;
  291. }
  292. }
  293. if (bf_next == NULL) {
  294. INIT_LIST_HEAD(&bf_head);
  295. } else {
  296. ASSERT(!list_empty(bf_q));
  297. list_move_tail(&bf->list, &bf_head);
  298. }
  299. if (!txpending) {
  300. /*
  301. * complete the acked-ones/xretried ones; update
  302. * block-ack window
  303. */
  304. spin_lock_bh(&txq->axq_lock);
  305. ath_tx_update_baw(sc, tid, bf->bf_seqno);
  306. spin_unlock_bh(&txq->axq_lock);
  307. if (rc_update && (acked_cnt == 1 || txfail_cnt == 1)) {
  308. ath_tx_rc_status(bf, ds, nbad, txok, true);
  309. rc_update = false;
  310. } else {
  311. ath_tx_rc_status(bf, ds, nbad, txok, false);
  312. }
  313. ath_tx_complete_buf(sc, bf, &bf_head, !txfail, sendbar);
  314. } else {
  315. /* retry the un-acked ones */
  316. if (bf->bf_next == NULL && bf_last->bf_stale) {
  317. struct ath_buf *tbf;
  318. tbf = ath_clone_txbuf(sc, bf_last);
  319. ath9k_hw_cleartxdesc(sc->sc_ah, tbf->bf_desc);
  320. list_add_tail(&tbf->list, &bf_head);
  321. } else {
  322. /*
  323. * Clear descriptor status words for
  324. * software retry
  325. */
  326. ath9k_hw_cleartxdesc(sc->sc_ah, bf->bf_desc);
  327. }
  328. /*
  329. * Put this buffer to the temporary pending
  330. * queue to retain ordering
  331. */
  332. list_splice_tail_init(&bf_head, &bf_pending);
  333. }
  334. bf = bf_next;
  335. }
  336. if (tid->state & AGGR_CLEANUP) {
  337. if (tid->baw_head == tid->baw_tail) {
  338. tid->state &= ~AGGR_ADDBA_COMPLETE;
  339. tid->addba_exchangeattempts = 0;
  340. tid->state &= ~AGGR_CLEANUP;
  341. /* send buffered frames as singles */
  342. ath_tx_flush_tid(sc, tid);
  343. }
  344. rcu_read_unlock();
  345. return;
  346. }
  347. /* prepend un-acked frames to the beginning of the pending frame queue */
  348. if (!list_empty(&bf_pending)) {
  349. spin_lock_bh(&txq->axq_lock);
  350. list_splice(&bf_pending, &tid->buf_q);
  351. ath_tx_queue_tid(txq, tid);
  352. spin_unlock_bh(&txq->axq_lock);
  353. }
  354. rcu_read_unlock();
  355. if (needreset)
  356. ath_reset(sc, false);
  357. }
  358. static u32 ath_lookup_rate(struct ath_softc *sc, struct ath_buf *bf,
  359. struct ath_atx_tid *tid)
  360. {
  361. const struct ath_rate_table *rate_table = sc->cur_rate_table;
  362. struct sk_buff *skb;
  363. struct ieee80211_tx_info *tx_info;
  364. struct ieee80211_tx_rate *rates;
  365. struct ath_tx_info_priv *tx_info_priv;
  366. u32 max_4ms_framelen, frmlen;
  367. u16 aggr_limit, legacy = 0, maxampdu;
  368. int i;
  369. skb = bf->bf_mpdu;
  370. tx_info = IEEE80211_SKB_CB(skb);
  371. rates = tx_info->control.rates;
  372. tx_info_priv = (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
  373. /*
  374. * Find the lowest frame length among the rate series that will have a
  375. * 4ms transmit duration.
  376. * TODO - TXOP limit needs to be considered.
  377. */
  378. max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
  379. for (i = 0; i < 4; i++) {
  380. if (rates[i].count) {
  381. if (!WLAN_RC_PHY_HT(rate_table->info[rates[i].idx].phy)) {
  382. legacy = 1;
  383. break;
  384. }
  385. frmlen = rate_table->info[rates[i].idx].max_4ms_framelen;
  386. max_4ms_framelen = min(max_4ms_framelen, frmlen);
  387. }
  388. }
  389. /*
  390. * limit aggregate size by the minimum rate if rate selected is
  391. * not a probe rate, if rate selected is a probe rate then
  392. * avoid aggregation of this packet.
  393. */
  394. if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
  395. return 0;
  396. aggr_limit = min(max_4ms_framelen, (u32)ATH_AMPDU_LIMIT_DEFAULT);
  397. /*
  398. * h/w can accept aggregates upto 16 bit lengths (65535).
  399. * The IE, however can hold upto 65536, which shows up here
  400. * as zero. Ignore 65536 since we are constrained by hw.
  401. */
  402. maxampdu = tid->an->maxampdu;
  403. if (maxampdu)
  404. aggr_limit = min(aggr_limit, maxampdu);
  405. return aggr_limit;
  406. }
  407. /*
  408. * Returns the number of delimiters to be added to
  409. * meet the minimum required mpdudensity.
  410. * caller should make sure that the rate is HT rate .
  411. */
  412. static int ath_compute_num_delims(struct ath_softc *sc, struct ath_atx_tid *tid,
  413. struct ath_buf *bf, u16 frmlen)
  414. {
  415. const struct ath_rate_table *rt = sc->cur_rate_table;
  416. struct sk_buff *skb = bf->bf_mpdu;
  417. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  418. u32 nsymbits, nsymbols, mpdudensity;
  419. u16 minlen;
  420. u8 rc, flags, rix;
  421. int width, half_gi, ndelim, mindelim;
  422. /* Select standard number of delimiters based on frame length alone */
  423. ndelim = ATH_AGGR_GET_NDELIM(frmlen);
  424. /*
  425. * If encryption enabled, hardware requires some more padding between
  426. * subframes.
  427. * TODO - this could be improved to be dependent on the rate.
  428. * The hardware can keep up at lower rates, but not higher rates
  429. */
  430. if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
  431. ndelim += ATH_AGGR_ENCRYPTDELIM;
  432. /*
  433. * Convert desired mpdu density from microeconds to bytes based
  434. * on highest rate in rate series (i.e. first rate) to determine
  435. * required minimum length for subframe. Take into account
  436. * whether high rate is 20 or 40Mhz and half or full GI.
  437. */
  438. mpdudensity = tid->an->mpdudensity;
  439. /*
  440. * If there is no mpdu density restriction, no further calculation
  441. * is needed.
  442. */
  443. if (mpdudensity == 0)
  444. return ndelim;
  445. rix = tx_info->control.rates[0].idx;
  446. flags = tx_info->control.rates[0].flags;
  447. rc = rt->info[rix].ratecode;
  448. width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
  449. half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
  450. if (half_gi)
  451. nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
  452. else
  453. nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
  454. if (nsymbols == 0)
  455. nsymbols = 1;
  456. nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
  457. minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
  458. if (frmlen < minlen) {
  459. mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
  460. ndelim = max(mindelim, ndelim);
  461. }
  462. return ndelim;
  463. }
  464. static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
  465. struct ath_atx_tid *tid,
  466. struct list_head *bf_q)
  467. {
  468. #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
  469. struct ath_buf *bf, *bf_first, *bf_prev = NULL;
  470. int rl = 0, nframes = 0, ndelim, prev_al = 0;
  471. u16 aggr_limit = 0, al = 0, bpad = 0,
  472. al_delta, h_baw = tid->baw_size / 2;
  473. enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
  474. bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
  475. do {
  476. bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
  477. /* do not step over block-ack window */
  478. if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
  479. status = ATH_AGGR_BAW_CLOSED;
  480. break;
  481. }
  482. if (!rl) {
  483. aggr_limit = ath_lookup_rate(sc, bf, tid);
  484. rl = 1;
  485. }
  486. /* do not exceed aggregation limit */
  487. al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
  488. if (nframes &&
  489. (aggr_limit < (al + bpad + al_delta + prev_al))) {
  490. status = ATH_AGGR_LIMITED;
  491. break;
  492. }
  493. /* do not exceed subframe limit */
  494. if (nframes >= min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
  495. status = ATH_AGGR_LIMITED;
  496. break;
  497. }
  498. nframes++;
  499. /* add padding for previous frame to aggregation length */
  500. al += bpad + al_delta;
  501. /*
  502. * Get the delimiters needed to meet the MPDU
  503. * density for this node.
  504. */
  505. ndelim = ath_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
  506. bpad = PADBYTES(al_delta) + (ndelim << 2);
  507. bf->bf_next = NULL;
  508. bf->bf_desc->ds_link = 0;
  509. /* link buffers of this frame to the aggregate */
  510. ath_tx_addto_baw(sc, tid, bf);
  511. ath9k_hw_set11n_aggr_middle(sc->sc_ah, bf->bf_desc, ndelim);
  512. list_move_tail(&bf->list, bf_q);
  513. if (bf_prev) {
  514. bf_prev->bf_next = bf;
  515. bf_prev->bf_desc->ds_link = bf->bf_daddr;
  516. }
  517. bf_prev = bf;
  518. } while (!list_empty(&tid->buf_q));
  519. bf_first->bf_al = al;
  520. bf_first->bf_nframes = nframes;
  521. return status;
  522. #undef PADBYTES
  523. }
  524. static void ath_tx_sched_aggr(struct ath_softc *sc, struct ath_txq *txq,
  525. struct ath_atx_tid *tid)
  526. {
  527. struct ath_buf *bf;
  528. enum ATH_AGGR_STATUS status;
  529. struct list_head bf_q;
  530. do {
  531. if (list_empty(&tid->buf_q))
  532. return;
  533. INIT_LIST_HEAD(&bf_q);
  534. status = ath_tx_form_aggr(sc, tid, &bf_q);
  535. /*
  536. * no frames picked up to be aggregated;
  537. * block-ack window is not open.
  538. */
  539. if (list_empty(&bf_q))
  540. break;
  541. bf = list_first_entry(&bf_q, struct ath_buf, list);
  542. bf->bf_lastbf = list_entry(bf_q.prev, struct ath_buf, list);
  543. /* if only one frame, send as non-aggregate */
  544. if (bf->bf_nframes == 1) {
  545. bf->bf_state.bf_type &= ~BUF_AGGR;
  546. ath9k_hw_clr11n_aggr(sc->sc_ah, bf->bf_desc);
  547. ath_buf_set_rate(sc, bf);
  548. ath_tx_txqaddbuf(sc, txq, &bf_q);
  549. continue;
  550. }
  551. /* setup first desc of aggregate */
  552. bf->bf_state.bf_type |= BUF_AGGR;
  553. ath_buf_set_rate(sc, bf);
  554. ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
  555. /* anchor last desc of aggregate */
  556. ath9k_hw_set11n_aggr_last(sc->sc_ah, bf->bf_lastbf->bf_desc);
  557. txq->axq_aggr_depth++;
  558. ath_tx_txqaddbuf(sc, txq, &bf_q);
  559. } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
  560. status != ATH_AGGR_BAW_CLOSED);
  561. }
  562. int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
  563. u16 tid, u16 *ssn)
  564. {
  565. struct ath_atx_tid *txtid;
  566. struct ath_node *an;
  567. an = (struct ath_node *)sta->drv_priv;
  568. if (sc->sc_flags & SC_OP_TXAGGR) {
  569. txtid = ATH_AN_2_TID(an, tid);
  570. txtid->state |= AGGR_ADDBA_PROGRESS;
  571. ath_tx_pause_tid(sc, txtid);
  572. *ssn = txtid->seq_start;
  573. }
  574. return 0;
  575. }
  576. int ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
  577. {
  578. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  579. struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
  580. struct ath_txq *txq = &sc->tx.txq[txtid->ac->qnum];
  581. struct ath_buf *bf;
  582. struct list_head bf_head;
  583. INIT_LIST_HEAD(&bf_head);
  584. if (txtid->state & AGGR_CLEANUP)
  585. return 0;
  586. if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
  587. txtid->state &= ~AGGR_ADDBA_PROGRESS;
  588. txtid->addba_exchangeattempts = 0;
  589. return 0;
  590. }
  591. ath_tx_pause_tid(sc, txtid);
  592. /* drop all software retried frames and mark this TID */
  593. spin_lock_bh(&txq->axq_lock);
  594. while (!list_empty(&txtid->buf_q)) {
  595. bf = list_first_entry(&txtid->buf_q, struct ath_buf, list);
  596. if (!bf_isretried(bf)) {
  597. /*
  598. * NB: it's based on the assumption that
  599. * software retried frame will always stay
  600. * at the head of software queue.
  601. */
  602. break;
  603. }
  604. list_move_tail(&bf->list, &bf_head);
  605. ath_tx_update_baw(sc, txtid, bf->bf_seqno);
  606. ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
  607. }
  608. spin_unlock_bh(&txq->axq_lock);
  609. if (txtid->baw_head != txtid->baw_tail) {
  610. txtid->state |= AGGR_CLEANUP;
  611. } else {
  612. txtid->state &= ~AGGR_ADDBA_COMPLETE;
  613. txtid->addba_exchangeattempts = 0;
  614. ath_tx_flush_tid(sc, txtid);
  615. }
  616. return 0;
  617. }
  618. void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
  619. {
  620. struct ath_atx_tid *txtid;
  621. struct ath_node *an;
  622. an = (struct ath_node *)sta->drv_priv;
  623. if (sc->sc_flags & SC_OP_TXAGGR) {
  624. txtid = ATH_AN_2_TID(an, tid);
  625. txtid->baw_size =
  626. IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
  627. txtid->state |= AGGR_ADDBA_COMPLETE;
  628. txtid->state &= ~AGGR_ADDBA_PROGRESS;
  629. ath_tx_resume_tid(sc, txtid);
  630. }
  631. }
  632. bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an, u8 tidno)
  633. {
  634. struct ath_atx_tid *txtid;
  635. if (!(sc->sc_flags & SC_OP_TXAGGR))
  636. return false;
  637. txtid = ATH_AN_2_TID(an, tidno);
  638. if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
  639. if (!(txtid->state & AGGR_ADDBA_PROGRESS) &&
  640. (txtid->addba_exchangeattempts < ADDBA_EXCHANGE_ATTEMPTS)) {
  641. txtid->addba_exchangeattempts++;
  642. return true;
  643. }
  644. }
  645. return false;
  646. }
  647. /********************/
  648. /* Queue Management */
  649. /********************/
  650. static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
  651. struct ath_txq *txq)
  652. {
  653. struct ath_atx_ac *ac, *ac_tmp;
  654. struct ath_atx_tid *tid, *tid_tmp;
  655. list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
  656. list_del(&ac->list);
  657. ac->sched = false;
  658. list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
  659. list_del(&tid->list);
  660. tid->sched = false;
  661. ath_tid_drain(sc, txq, tid);
  662. }
  663. }
  664. }
  665. struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
  666. {
  667. struct ath_hw *ah = sc->sc_ah;
  668. struct ath9k_tx_queue_info qi;
  669. int qnum;
  670. memset(&qi, 0, sizeof(qi));
  671. qi.tqi_subtype = subtype;
  672. qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
  673. qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
  674. qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
  675. qi.tqi_physCompBuf = 0;
  676. /*
  677. * Enable interrupts only for EOL and DESC conditions.
  678. * We mark tx descriptors to receive a DESC interrupt
  679. * when a tx queue gets deep; otherwise waiting for the
  680. * EOL to reap descriptors. Note that this is done to
  681. * reduce interrupt load and this only defers reaping
  682. * descriptors, never transmitting frames. Aside from
  683. * reducing interrupts this also permits more concurrency.
  684. * The only potential downside is if the tx queue backs
  685. * up in which case the top half of the kernel may backup
  686. * due to a lack of tx descriptors.
  687. *
  688. * The UAPSD queue is an exception, since we take a desc-
  689. * based intr on the EOSP frames.
  690. */
  691. if (qtype == ATH9K_TX_QUEUE_UAPSD)
  692. qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
  693. else
  694. qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
  695. TXQ_FLAG_TXDESCINT_ENABLE;
  696. qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
  697. if (qnum == -1) {
  698. /*
  699. * NB: don't print a message, this happens
  700. * normally on parts with too few tx queues
  701. */
  702. return NULL;
  703. }
  704. if (qnum >= ARRAY_SIZE(sc->tx.txq)) {
  705. DPRINTF(sc, ATH_DBG_FATAL,
  706. "qnum %u out of range, max %u!\n",
  707. qnum, (unsigned int)ARRAY_SIZE(sc->tx.txq));
  708. ath9k_hw_releasetxqueue(ah, qnum);
  709. return NULL;
  710. }
  711. if (!ATH_TXQ_SETUP(sc, qnum)) {
  712. struct ath_txq *txq = &sc->tx.txq[qnum];
  713. txq->axq_qnum = qnum;
  714. txq->axq_link = NULL;
  715. INIT_LIST_HEAD(&txq->axq_q);
  716. INIT_LIST_HEAD(&txq->axq_acq);
  717. spin_lock_init(&txq->axq_lock);
  718. txq->axq_depth = 0;
  719. txq->axq_aggr_depth = 0;
  720. txq->axq_totalqueued = 0;
  721. txq->axq_linkbuf = NULL;
  722. sc->tx.txqsetup |= 1<<qnum;
  723. }
  724. return &sc->tx.txq[qnum];
  725. }
  726. static int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
  727. {
  728. int qnum;
  729. switch (qtype) {
  730. case ATH9K_TX_QUEUE_DATA:
  731. if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
  732. DPRINTF(sc, ATH_DBG_FATAL,
  733. "HAL AC %u out of range, max %zu!\n",
  734. haltype, ARRAY_SIZE(sc->tx.hwq_map));
  735. return -1;
  736. }
  737. qnum = sc->tx.hwq_map[haltype];
  738. break;
  739. case ATH9K_TX_QUEUE_BEACON:
  740. qnum = sc->beacon.beaconq;
  741. break;
  742. case ATH9K_TX_QUEUE_CAB:
  743. qnum = sc->beacon.cabq->axq_qnum;
  744. break;
  745. default:
  746. qnum = -1;
  747. }
  748. return qnum;
  749. }
  750. struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
  751. {
  752. struct ath_txq *txq = NULL;
  753. int qnum;
  754. qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
  755. txq = &sc->tx.txq[qnum];
  756. spin_lock_bh(&txq->axq_lock);
  757. if (txq->axq_depth >= (ATH_TXBUF - 20)) {
  758. DPRINTF(sc, ATH_DBG_XMIT,
  759. "TX queue: %d is full, depth: %d\n",
  760. qnum, txq->axq_depth);
  761. ieee80211_stop_queue(sc->hw, skb_get_queue_mapping(skb));
  762. txq->stopped = 1;
  763. spin_unlock_bh(&txq->axq_lock);
  764. return NULL;
  765. }
  766. spin_unlock_bh(&txq->axq_lock);
  767. return txq;
  768. }
  769. int ath_txq_update(struct ath_softc *sc, int qnum,
  770. struct ath9k_tx_queue_info *qinfo)
  771. {
  772. struct ath_hw *ah = sc->sc_ah;
  773. int error = 0;
  774. struct ath9k_tx_queue_info qi;
  775. if (qnum == sc->beacon.beaconq) {
  776. /*
  777. * XXX: for beacon queue, we just save the parameter.
  778. * It will be picked up by ath_beaconq_config when
  779. * it's necessary.
  780. */
  781. sc->beacon.beacon_qi = *qinfo;
  782. return 0;
  783. }
  784. ASSERT(sc->tx.txq[qnum].axq_qnum == qnum);
  785. ath9k_hw_get_txq_props(ah, qnum, &qi);
  786. qi.tqi_aifs = qinfo->tqi_aifs;
  787. qi.tqi_cwmin = qinfo->tqi_cwmin;
  788. qi.tqi_cwmax = qinfo->tqi_cwmax;
  789. qi.tqi_burstTime = qinfo->tqi_burstTime;
  790. qi.tqi_readyTime = qinfo->tqi_readyTime;
  791. if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
  792. DPRINTF(sc, ATH_DBG_FATAL,
  793. "Unable to update hardware queue %u!\n", qnum);
  794. error = -EIO;
  795. } else {
  796. ath9k_hw_resettxqueue(ah, qnum);
  797. }
  798. return error;
  799. }
  800. int ath_cabq_update(struct ath_softc *sc)
  801. {
  802. struct ath9k_tx_queue_info qi;
  803. int qnum = sc->beacon.cabq->axq_qnum;
  804. ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
  805. /*
  806. * Ensure the readytime % is within the bounds.
  807. */
  808. if (sc->config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
  809. sc->config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
  810. else if (sc->config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
  811. sc->config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
  812. qi.tqi_readyTime = (sc->beacon_interval *
  813. sc->config.cabqReadytime) / 100;
  814. ath_txq_update(sc, qnum, &qi);
  815. return 0;
  816. }
  817. /*
  818. * Drain a given TX queue (could be Beacon or Data)
  819. *
  820. * This assumes output has been stopped and
  821. * we do not need to block ath_tx_tasklet.
  822. */
  823. void ath_draintxq(struct ath_softc *sc, struct ath_txq *txq, bool retry_tx)
  824. {
  825. struct ath_buf *bf, *lastbf;
  826. struct list_head bf_head;
  827. INIT_LIST_HEAD(&bf_head);
  828. for (;;) {
  829. spin_lock_bh(&txq->axq_lock);
  830. if (list_empty(&txq->axq_q)) {
  831. txq->axq_link = NULL;
  832. txq->axq_linkbuf = NULL;
  833. spin_unlock_bh(&txq->axq_lock);
  834. break;
  835. }
  836. bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
  837. if (bf->bf_stale) {
  838. list_del(&bf->list);
  839. spin_unlock_bh(&txq->axq_lock);
  840. spin_lock_bh(&sc->tx.txbuflock);
  841. list_add_tail(&bf->list, &sc->tx.txbuf);
  842. spin_unlock_bh(&sc->tx.txbuflock);
  843. continue;
  844. }
  845. lastbf = bf->bf_lastbf;
  846. if (!retry_tx)
  847. lastbf->bf_desc->ds_txstat.ts_flags =
  848. ATH9K_TX_SW_ABORTED;
  849. /* remove ath_buf's of the same mpdu from txq */
  850. list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
  851. txq->axq_depth--;
  852. spin_unlock_bh(&txq->axq_lock);
  853. if (bf_isampdu(bf))
  854. ath_tx_complete_aggr(sc, txq, bf, &bf_head, 0);
  855. else
  856. ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
  857. }
  858. /* flush any pending frames if aggregation is enabled */
  859. if (sc->sc_flags & SC_OP_TXAGGR) {
  860. if (!retry_tx) {
  861. spin_lock_bh(&txq->axq_lock);
  862. ath_txq_drain_pending_buffers(sc, txq);
  863. spin_unlock_bh(&txq->axq_lock);
  864. }
  865. }
  866. }
  867. void ath_drain_all_txq(struct ath_softc *sc, bool retry_tx)
  868. {
  869. struct ath_hw *ah = sc->sc_ah;
  870. struct ath_txq *txq;
  871. int i, npend = 0;
  872. if (sc->sc_flags & SC_OP_INVALID)
  873. return;
  874. /* Stop beacon queue */
  875. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  876. /* Stop data queues */
  877. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  878. if (ATH_TXQ_SETUP(sc, i)) {
  879. txq = &sc->tx.txq[i];
  880. ath9k_hw_stoptxdma(ah, txq->axq_qnum);
  881. npend += ath9k_hw_numtxpending(ah, txq->axq_qnum);
  882. }
  883. }
  884. if (npend) {
  885. int r;
  886. DPRINTF(sc, ATH_DBG_XMIT, "Unable to stop TxDMA. Reset HAL!\n");
  887. spin_lock_bh(&sc->sc_resetlock);
  888. r = ath9k_hw_reset(ah, sc->sc_ah->curchan, true);
  889. if (r)
  890. DPRINTF(sc, ATH_DBG_FATAL,
  891. "Unable to reset hardware; reset status %d\n",
  892. r);
  893. spin_unlock_bh(&sc->sc_resetlock);
  894. }
  895. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  896. if (ATH_TXQ_SETUP(sc, i))
  897. ath_draintxq(sc, &sc->tx.txq[i], retry_tx);
  898. }
  899. }
  900. void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
  901. {
  902. ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
  903. sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
  904. }
  905. void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
  906. {
  907. struct ath_atx_ac *ac;
  908. struct ath_atx_tid *tid;
  909. if (list_empty(&txq->axq_acq))
  910. return;
  911. ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
  912. list_del(&ac->list);
  913. ac->sched = false;
  914. do {
  915. if (list_empty(&ac->tid_q))
  916. return;
  917. tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
  918. list_del(&tid->list);
  919. tid->sched = false;
  920. if (tid->paused)
  921. continue;
  922. if ((txq->axq_depth % 2) == 0)
  923. ath_tx_sched_aggr(sc, txq, tid);
  924. /*
  925. * add tid to round-robin queue if more frames
  926. * are pending for the tid
  927. */
  928. if (!list_empty(&tid->buf_q))
  929. ath_tx_queue_tid(txq, tid);
  930. break;
  931. } while (!list_empty(&ac->tid_q));
  932. if (!list_empty(&ac->tid_q)) {
  933. if (!ac->sched) {
  934. ac->sched = true;
  935. list_add_tail(&ac->list, &txq->axq_acq);
  936. }
  937. }
  938. }
  939. int ath_tx_setup(struct ath_softc *sc, int haltype)
  940. {
  941. struct ath_txq *txq;
  942. if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
  943. DPRINTF(sc, ATH_DBG_FATAL,
  944. "HAL AC %u out of range, max %zu!\n",
  945. haltype, ARRAY_SIZE(sc->tx.hwq_map));
  946. return 0;
  947. }
  948. txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
  949. if (txq != NULL) {
  950. sc->tx.hwq_map[haltype] = txq->axq_qnum;
  951. return 1;
  952. } else
  953. return 0;
  954. }
  955. /***********/
  956. /* TX, DMA */
  957. /***********/
  958. /*
  959. * Insert a chain of ath_buf (descriptors) on a txq and
  960. * assume the descriptors are already chained together by caller.
  961. */
  962. static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
  963. struct list_head *head)
  964. {
  965. struct ath_hw *ah = sc->sc_ah;
  966. struct ath_buf *bf;
  967. /*
  968. * Insert the frame on the outbound list and
  969. * pass it on to the hardware.
  970. */
  971. if (list_empty(head))
  972. return;
  973. bf = list_first_entry(head, struct ath_buf, list);
  974. list_splice_tail_init(head, &txq->axq_q);
  975. txq->axq_depth++;
  976. txq->axq_totalqueued++;
  977. txq->axq_linkbuf = list_entry(txq->axq_q.prev, struct ath_buf, list);
  978. DPRINTF(sc, ATH_DBG_QUEUE,
  979. "qnum: %d, txq depth: %d\n", txq->axq_qnum, txq->axq_depth);
  980. if (txq->axq_link == NULL) {
  981. ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
  982. DPRINTF(sc, ATH_DBG_XMIT,
  983. "TXDP[%u] = %llx (%p)\n",
  984. txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
  985. } else {
  986. *txq->axq_link = bf->bf_daddr;
  987. DPRINTF(sc, ATH_DBG_XMIT, "link[%u] (%p)=%llx (%p)\n",
  988. txq->axq_qnum, txq->axq_link,
  989. ito64(bf->bf_daddr), bf->bf_desc);
  990. }
  991. txq->axq_link = &(bf->bf_lastbf->bf_desc->ds_link);
  992. ath9k_hw_txstart(ah, txq->axq_qnum);
  993. }
  994. static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
  995. {
  996. struct ath_buf *bf = NULL;
  997. spin_lock_bh(&sc->tx.txbuflock);
  998. if (unlikely(list_empty(&sc->tx.txbuf))) {
  999. spin_unlock_bh(&sc->tx.txbuflock);
  1000. return NULL;
  1001. }
  1002. bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
  1003. list_del(&bf->list);
  1004. spin_unlock_bh(&sc->tx.txbuflock);
  1005. return bf;
  1006. }
  1007. static void ath_tx_send_ampdu(struct ath_softc *sc, struct ath_atx_tid *tid,
  1008. struct list_head *bf_head,
  1009. struct ath_tx_control *txctl)
  1010. {
  1011. struct ath_buf *bf;
  1012. bf = list_first_entry(bf_head, struct ath_buf, list);
  1013. bf->bf_state.bf_type |= BUF_AMPDU;
  1014. /*
  1015. * Do not queue to h/w when any of the following conditions is true:
  1016. * - there are pending frames in software queue
  1017. * - the TID is currently paused for ADDBA/BAR request
  1018. * - seqno is not within block-ack window
  1019. * - h/w queue depth exceeds low water mark
  1020. */
  1021. if (!list_empty(&tid->buf_q) || tid->paused ||
  1022. !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
  1023. txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
  1024. /*
  1025. * Add this frame to software queue for scheduling later
  1026. * for aggregation.
  1027. */
  1028. list_move_tail(&bf->list, &tid->buf_q);
  1029. ath_tx_queue_tid(txctl->txq, tid);
  1030. return;
  1031. }
  1032. /* Add sub-frame to BAW */
  1033. ath_tx_addto_baw(sc, tid, bf);
  1034. /* Queue to h/w without aggregation */
  1035. bf->bf_nframes = 1;
  1036. bf->bf_lastbf = bf;
  1037. ath_buf_set_rate(sc, bf);
  1038. ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
  1039. }
  1040. static void ath_tx_send_ht_normal(struct ath_softc *sc, struct ath_txq *txq,
  1041. struct ath_atx_tid *tid,
  1042. struct list_head *bf_head)
  1043. {
  1044. struct ath_buf *bf;
  1045. bf = list_first_entry(bf_head, struct ath_buf, list);
  1046. bf->bf_state.bf_type &= ~BUF_AMPDU;
  1047. /* update starting sequence number for subsequent ADDBA request */
  1048. INCR(tid->seq_start, IEEE80211_SEQ_MAX);
  1049. bf->bf_nframes = 1;
  1050. bf->bf_lastbf = bf;
  1051. ath_buf_set_rate(sc, bf);
  1052. ath_tx_txqaddbuf(sc, txq, bf_head);
  1053. }
  1054. static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
  1055. struct list_head *bf_head)
  1056. {
  1057. struct ath_buf *bf;
  1058. bf = list_first_entry(bf_head, struct ath_buf, list);
  1059. bf->bf_lastbf = bf;
  1060. bf->bf_nframes = 1;
  1061. ath_buf_set_rate(sc, bf);
  1062. ath_tx_txqaddbuf(sc, txq, bf_head);
  1063. }
  1064. static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
  1065. {
  1066. struct ieee80211_hdr *hdr;
  1067. enum ath9k_pkt_type htype;
  1068. __le16 fc;
  1069. hdr = (struct ieee80211_hdr *)skb->data;
  1070. fc = hdr->frame_control;
  1071. if (ieee80211_is_beacon(fc))
  1072. htype = ATH9K_PKT_TYPE_BEACON;
  1073. else if (ieee80211_is_probe_resp(fc))
  1074. htype = ATH9K_PKT_TYPE_PROBE_RESP;
  1075. else if (ieee80211_is_atim(fc))
  1076. htype = ATH9K_PKT_TYPE_ATIM;
  1077. else if (ieee80211_is_pspoll(fc))
  1078. htype = ATH9K_PKT_TYPE_PSPOLL;
  1079. else
  1080. htype = ATH9K_PKT_TYPE_NORMAL;
  1081. return htype;
  1082. }
  1083. static bool is_pae(struct sk_buff *skb)
  1084. {
  1085. struct ieee80211_hdr *hdr;
  1086. __le16 fc;
  1087. hdr = (struct ieee80211_hdr *)skb->data;
  1088. fc = hdr->frame_control;
  1089. if (ieee80211_is_data(fc)) {
  1090. if (ieee80211_is_nullfunc(fc) ||
  1091. /* Port Access Entity (IEEE 802.1X) */
  1092. (skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  1093. return true;
  1094. }
  1095. }
  1096. return false;
  1097. }
  1098. static int get_hw_crypto_keytype(struct sk_buff *skb)
  1099. {
  1100. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1101. if (tx_info->control.hw_key) {
  1102. if (tx_info->control.hw_key->alg == ALG_WEP)
  1103. return ATH9K_KEY_TYPE_WEP;
  1104. else if (tx_info->control.hw_key->alg == ALG_TKIP)
  1105. return ATH9K_KEY_TYPE_TKIP;
  1106. else if (tx_info->control.hw_key->alg == ALG_CCMP)
  1107. return ATH9K_KEY_TYPE_AES;
  1108. }
  1109. return ATH9K_KEY_TYPE_CLEAR;
  1110. }
  1111. static void assign_aggr_tid_seqno(struct sk_buff *skb,
  1112. struct ath_buf *bf)
  1113. {
  1114. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1115. struct ieee80211_hdr *hdr;
  1116. struct ath_node *an;
  1117. struct ath_atx_tid *tid;
  1118. __le16 fc;
  1119. u8 *qc;
  1120. if (!tx_info->control.sta)
  1121. return;
  1122. an = (struct ath_node *)tx_info->control.sta->drv_priv;
  1123. hdr = (struct ieee80211_hdr *)skb->data;
  1124. fc = hdr->frame_control;
  1125. if (ieee80211_is_data_qos(fc)) {
  1126. qc = ieee80211_get_qos_ctl(hdr);
  1127. bf->bf_tidno = qc[0] & 0xf;
  1128. }
  1129. /*
  1130. * For HT capable stations, we save tidno for later use.
  1131. * We also override seqno set by upper layer with the one
  1132. * in tx aggregation state.
  1133. *
  1134. * If fragmentation is on, the sequence number is
  1135. * not overridden, since it has been
  1136. * incremented by the fragmentation routine.
  1137. *
  1138. * FIXME: check if the fragmentation threshold exceeds
  1139. * IEEE80211 max.
  1140. */
  1141. tid = ATH_AN_2_TID(an, bf->bf_tidno);
  1142. hdr->seq_ctrl = cpu_to_le16(tid->seq_next <<
  1143. IEEE80211_SEQ_SEQ_SHIFT);
  1144. bf->bf_seqno = tid->seq_next;
  1145. INCR(tid->seq_next, IEEE80211_SEQ_MAX);
  1146. }
  1147. static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
  1148. struct ath_txq *txq)
  1149. {
  1150. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1151. int flags = 0;
  1152. flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
  1153. flags |= ATH9K_TXDESC_INTREQ;
  1154. if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
  1155. flags |= ATH9K_TXDESC_NOACK;
  1156. return flags;
  1157. }
  1158. /*
  1159. * rix - rate index
  1160. * pktlen - total bytes (delims + data + fcs + pads + pad delims)
  1161. * width - 0 for 20 MHz, 1 for 40 MHz
  1162. * half_gi - to use 4us v/s 3.6 us for symbol time
  1163. */
  1164. static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
  1165. int width, int half_gi, bool shortPreamble)
  1166. {
  1167. const struct ath_rate_table *rate_table = sc->cur_rate_table;
  1168. u32 nbits, nsymbits, duration, nsymbols;
  1169. u8 rc;
  1170. int streams, pktlen;
  1171. pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
  1172. rc = rate_table->info[rix].ratecode;
  1173. /* for legacy rates, use old function to compute packet duration */
  1174. if (!IS_HT_RATE(rc))
  1175. return ath9k_hw_computetxtime(sc->sc_ah, rate_table, pktlen,
  1176. rix, shortPreamble);
  1177. /* find number of symbols: PLCP + data */
  1178. nbits = (pktlen << 3) + OFDM_PLCP_BITS;
  1179. nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
  1180. nsymbols = (nbits + nsymbits - 1) / nsymbits;
  1181. if (!half_gi)
  1182. duration = SYMBOL_TIME(nsymbols);
  1183. else
  1184. duration = SYMBOL_TIME_HALFGI(nsymbols);
  1185. /* addup duration for legacy/ht training and signal fields */
  1186. streams = HT_RC_2_STREAMS(rc);
  1187. duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
  1188. return duration;
  1189. }
  1190. static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
  1191. {
  1192. const struct ath_rate_table *rt = sc->cur_rate_table;
  1193. struct ath9k_11n_rate_series series[4];
  1194. struct sk_buff *skb;
  1195. struct ieee80211_tx_info *tx_info;
  1196. struct ieee80211_tx_rate *rates;
  1197. struct ieee80211_hdr *hdr;
  1198. int i, flags = 0;
  1199. u8 rix = 0, ctsrate = 0;
  1200. bool is_pspoll;
  1201. memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
  1202. skb = bf->bf_mpdu;
  1203. tx_info = IEEE80211_SKB_CB(skb);
  1204. rates = tx_info->control.rates;
  1205. hdr = (struct ieee80211_hdr *)skb->data;
  1206. is_pspoll = ieee80211_is_pspoll(hdr->frame_control);
  1207. /*
  1208. * We check if Short Preamble is needed for the CTS rate by
  1209. * checking the BSS's global flag.
  1210. * But for the rate series, IEEE80211_TX_RC_USE_SHORT_PREAMBLE is used.
  1211. */
  1212. if (sc->sc_flags & SC_OP_PREAMBLE_SHORT)
  1213. ctsrate = rt->info[tx_info->control.rts_cts_rate_idx].ratecode |
  1214. rt->info[tx_info->control.rts_cts_rate_idx].short_preamble;
  1215. else
  1216. ctsrate = rt->info[tx_info->control.rts_cts_rate_idx].ratecode;
  1217. /*
  1218. * ATH9K_TXDESC_RTSENA and ATH9K_TXDESC_CTSENA are mutually exclusive.
  1219. * Check the first rate in the series to decide whether RTS/CTS
  1220. * or CTS-to-self has to be used.
  1221. */
  1222. if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1223. flags = ATH9K_TXDESC_CTSENA;
  1224. else if (rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1225. flags = ATH9K_TXDESC_RTSENA;
  1226. /* FIXME: Handle aggregation protection */
  1227. if (sc->config.ath_aggr_prot &&
  1228. (!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
  1229. flags = ATH9K_TXDESC_RTSENA;
  1230. }
  1231. /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
  1232. if (bf_isaggr(bf) && (bf->bf_al > sc->sc_ah->caps.rts_aggr_limit))
  1233. flags &= ~(ATH9K_TXDESC_RTSENA);
  1234. for (i = 0; i < 4; i++) {
  1235. if (!rates[i].count || (rates[i].idx < 0))
  1236. continue;
  1237. rix = rates[i].idx;
  1238. series[i].Tries = rates[i].count;
  1239. series[i].ChSel = sc->tx_chainmask;
  1240. if (rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  1241. series[i].Rate = rt->info[rix].ratecode |
  1242. rt->info[rix].short_preamble;
  1243. else
  1244. series[i].Rate = rt->info[rix].ratecode;
  1245. if (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1246. series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
  1247. if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1248. series[i].RateFlags |= ATH9K_RATESERIES_2040;
  1249. if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
  1250. series[i].RateFlags |= ATH9K_RATESERIES_HALFGI;
  1251. series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
  1252. (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) != 0,
  1253. (rates[i].flags & IEEE80211_TX_RC_SHORT_GI),
  1254. (rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE));
  1255. }
  1256. /* set dur_update_en for l-sig computation except for PS-Poll frames */
  1257. ath9k_hw_set11n_ratescenario(sc->sc_ah, bf->bf_desc,
  1258. bf->bf_lastbf->bf_desc,
  1259. !is_pspoll, ctsrate,
  1260. 0, series, 4, flags);
  1261. if (sc->config.ath_aggr_prot && flags)
  1262. ath9k_hw_set11n_burstduration(sc->sc_ah, bf->bf_desc, 8192);
  1263. }
  1264. static int ath_tx_setup_buffer(struct ieee80211_hw *hw, struct ath_buf *bf,
  1265. struct sk_buff *skb,
  1266. struct ath_tx_control *txctl)
  1267. {
  1268. struct ath_wiphy *aphy = hw->priv;
  1269. struct ath_softc *sc = aphy->sc;
  1270. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1271. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1272. struct ath_tx_info_priv *tx_info_priv;
  1273. int hdrlen;
  1274. __le16 fc;
  1275. tx_info_priv = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
  1276. if (unlikely(!tx_info_priv))
  1277. return -ENOMEM;
  1278. tx_info->rate_driver_data[0] = tx_info_priv;
  1279. tx_info_priv->aphy = aphy;
  1280. tx_info_priv->frame_type = txctl->frame_type;
  1281. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1282. fc = hdr->frame_control;
  1283. ATH_TXBUF_RESET(bf);
  1284. bf->bf_frmlen = skb->len + FCS_LEN - (hdrlen & 3);
  1285. if (conf_is_ht(&sc->hw->conf) && !is_pae(skb))
  1286. bf->bf_state.bf_type |= BUF_HT;
  1287. bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
  1288. bf->bf_keytype = get_hw_crypto_keytype(skb);
  1289. if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR) {
  1290. bf->bf_frmlen += tx_info->control.hw_key->icv_len;
  1291. bf->bf_keyix = tx_info->control.hw_key->hw_key_idx;
  1292. } else {
  1293. bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
  1294. }
  1295. if (ieee80211_is_data_qos(fc) && (sc->sc_flags & SC_OP_TXAGGR))
  1296. assign_aggr_tid_seqno(skb, bf);
  1297. bf->bf_mpdu = skb;
  1298. bf->bf_dmacontext = dma_map_single(sc->dev, skb->data,
  1299. skb->len, DMA_TO_DEVICE);
  1300. if (unlikely(dma_mapping_error(sc->dev, bf->bf_dmacontext))) {
  1301. bf->bf_mpdu = NULL;
  1302. kfree(tx_info_priv);
  1303. tx_info->rate_driver_data[0] = NULL;
  1304. DPRINTF(sc, ATH_DBG_FATAL, "dma_mapping_error() on TX\n");
  1305. return -ENOMEM;
  1306. }
  1307. bf->bf_buf_addr = bf->bf_dmacontext;
  1308. return 0;
  1309. }
  1310. /* FIXME: tx power */
  1311. static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
  1312. struct ath_tx_control *txctl)
  1313. {
  1314. struct sk_buff *skb = bf->bf_mpdu;
  1315. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1316. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1317. struct ath_node *an = NULL;
  1318. struct list_head bf_head;
  1319. struct ath_desc *ds;
  1320. struct ath_atx_tid *tid;
  1321. struct ath_hw *ah = sc->sc_ah;
  1322. int frm_type;
  1323. __le16 fc;
  1324. frm_type = get_hw_packet_type(skb);
  1325. fc = hdr->frame_control;
  1326. INIT_LIST_HEAD(&bf_head);
  1327. list_add_tail(&bf->list, &bf_head);
  1328. ds = bf->bf_desc;
  1329. ds->ds_link = 0;
  1330. ds->ds_data = bf->bf_buf_addr;
  1331. ath9k_hw_set11n_txdesc(ah, ds, bf->bf_frmlen, frm_type, MAX_RATE_POWER,
  1332. bf->bf_keyix, bf->bf_keytype, bf->bf_flags);
  1333. ath9k_hw_filltxdesc(ah, ds,
  1334. skb->len, /* segment length */
  1335. true, /* first segment */
  1336. true, /* last segment */
  1337. ds); /* first descriptor */
  1338. spin_lock_bh(&txctl->txq->axq_lock);
  1339. if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR) &&
  1340. tx_info->control.sta) {
  1341. an = (struct ath_node *)tx_info->control.sta->drv_priv;
  1342. tid = ATH_AN_2_TID(an, bf->bf_tidno);
  1343. if (!ieee80211_is_data_qos(fc)) {
  1344. ath_tx_send_normal(sc, txctl->txq, &bf_head);
  1345. goto tx_done;
  1346. }
  1347. if (ath_aggr_query(sc, an, bf->bf_tidno)) {
  1348. /*
  1349. * Try aggregation if it's a unicast data frame
  1350. * and the destination is HT capable.
  1351. */
  1352. ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
  1353. } else {
  1354. /*
  1355. * Send this frame as regular when ADDBA
  1356. * exchange is neither complete nor pending.
  1357. */
  1358. ath_tx_send_ht_normal(sc, txctl->txq,
  1359. tid, &bf_head);
  1360. }
  1361. } else {
  1362. ath_tx_send_normal(sc, txctl->txq, &bf_head);
  1363. }
  1364. tx_done:
  1365. spin_unlock_bh(&txctl->txq->axq_lock);
  1366. }
  1367. /* Upon failure caller should free skb */
  1368. int ath_tx_start(struct ieee80211_hw *hw, struct sk_buff *skb,
  1369. struct ath_tx_control *txctl)
  1370. {
  1371. struct ath_wiphy *aphy = hw->priv;
  1372. struct ath_softc *sc = aphy->sc;
  1373. struct ath_buf *bf;
  1374. int r;
  1375. bf = ath_tx_get_buffer(sc);
  1376. if (!bf) {
  1377. DPRINTF(sc, ATH_DBG_XMIT, "TX buffers are full\n");
  1378. return -1;
  1379. }
  1380. r = ath_tx_setup_buffer(hw, bf, skb, txctl);
  1381. if (unlikely(r)) {
  1382. struct ath_txq *txq = txctl->txq;
  1383. DPRINTF(sc, ATH_DBG_FATAL, "TX mem alloc failure\n");
  1384. /* upon ath_tx_processq() this TX queue will be resumed, we
  1385. * guarantee this will happen by knowing beforehand that
  1386. * we will at least have to run TX completionon one buffer
  1387. * on the queue */
  1388. spin_lock_bh(&txq->axq_lock);
  1389. if (sc->tx.txq[txq->axq_qnum].axq_depth > 1) {
  1390. ieee80211_stop_queue(sc->hw,
  1391. skb_get_queue_mapping(skb));
  1392. txq->stopped = 1;
  1393. }
  1394. spin_unlock_bh(&txq->axq_lock);
  1395. spin_lock_bh(&sc->tx.txbuflock);
  1396. list_add_tail(&bf->list, &sc->tx.txbuf);
  1397. spin_unlock_bh(&sc->tx.txbuflock);
  1398. return r;
  1399. }
  1400. ath_tx_start_dma(sc, bf, txctl);
  1401. return 0;
  1402. }
  1403. void ath_tx_cabq(struct ieee80211_hw *hw, struct sk_buff *skb)
  1404. {
  1405. struct ath_wiphy *aphy = hw->priv;
  1406. struct ath_softc *sc = aphy->sc;
  1407. int hdrlen, padsize;
  1408. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1409. struct ath_tx_control txctl;
  1410. memset(&txctl, 0, sizeof(struct ath_tx_control));
  1411. /*
  1412. * As a temporary workaround, assign seq# here; this will likely need
  1413. * to be cleaned up to work better with Beacon transmission and virtual
  1414. * BSSes.
  1415. */
  1416. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1417. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1418. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  1419. sc->tx.seq_no += 0x10;
  1420. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1421. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  1422. }
  1423. /* Add the padding after the header if this is not already done */
  1424. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1425. if (hdrlen & 3) {
  1426. padsize = hdrlen % 4;
  1427. if (skb_headroom(skb) < padsize) {
  1428. DPRINTF(sc, ATH_DBG_XMIT, "TX CABQ padding failed\n");
  1429. dev_kfree_skb_any(skb);
  1430. return;
  1431. }
  1432. skb_push(skb, padsize);
  1433. memmove(skb->data, skb->data + padsize, hdrlen);
  1434. }
  1435. txctl.txq = sc->beacon.cabq;
  1436. DPRINTF(sc, ATH_DBG_XMIT, "transmitting CABQ packet, skb: %p\n", skb);
  1437. if (ath_tx_start(hw, skb, &txctl) != 0) {
  1438. DPRINTF(sc, ATH_DBG_XMIT, "CABQ TX failed\n");
  1439. goto exit;
  1440. }
  1441. return;
  1442. exit:
  1443. dev_kfree_skb_any(skb);
  1444. }
  1445. /*****************/
  1446. /* TX Completion */
  1447. /*****************/
  1448. static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
  1449. int tx_flags)
  1450. {
  1451. struct ieee80211_hw *hw = sc->hw;
  1452. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1453. struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
  1454. int hdrlen, padsize;
  1455. int frame_type = ATH9K_NOT_INTERNAL;
  1456. DPRINTF(sc, ATH_DBG_XMIT, "TX complete: skb: %p\n", skb);
  1457. if (tx_info_priv) {
  1458. hw = tx_info_priv->aphy->hw;
  1459. frame_type = tx_info_priv->frame_type;
  1460. }
  1461. if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
  1462. tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  1463. kfree(tx_info_priv);
  1464. tx_info->rate_driver_data[0] = NULL;
  1465. }
  1466. if (tx_flags & ATH_TX_BAR)
  1467. tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  1468. if (!(tx_flags & (ATH_TX_ERROR | ATH_TX_XRETRY))) {
  1469. /* Frame was ACKed */
  1470. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  1471. }
  1472. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1473. padsize = hdrlen & 3;
  1474. if (padsize && hdrlen >= 24) {
  1475. /*
  1476. * Remove MAC header padding before giving the frame back to
  1477. * mac80211.
  1478. */
  1479. memmove(skb->data + padsize, skb->data, hdrlen);
  1480. skb_pull(skb, padsize);
  1481. }
  1482. if (sc->sc_flags & SC_OP_WAIT_FOR_TX_ACK) {
  1483. sc->sc_flags &= ~SC_OP_WAIT_FOR_TX_ACK;
  1484. DPRINTF(sc, ATH_DBG_PS, "Going back to sleep after having "
  1485. "received TX status (0x%x)\n",
  1486. sc->sc_flags & (SC_OP_WAIT_FOR_BEACON |
  1487. SC_OP_WAIT_FOR_CAB |
  1488. SC_OP_WAIT_FOR_PSPOLL_DATA |
  1489. SC_OP_WAIT_FOR_TX_ACK));
  1490. }
  1491. if (frame_type == ATH9K_NOT_INTERNAL)
  1492. ieee80211_tx_status(hw, skb);
  1493. else
  1494. ath9k_tx_status(hw, skb);
  1495. }
  1496. static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
  1497. struct list_head *bf_q,
  1498. int txok, int sendbar)
  1499. {
  1500. struct sk_buff *skb = bf->bf_mpdu;
  1501. unsigned long flags;
  1502. int tx_flags = 0;
  1503. if (sendbar)
  1504. tx_flags = ATH_TX_BAR;
  1505. if (!txok) {
  1506. tx_flags |= ATH_TX_ERROR;
  1507. if (bf_isxretried(bf))
  1508. tx_flags |= ATH_TX_XRETRY;
  1509. }
  1510. dma_unmap_single(sc->dev, bf->bf_dmacontext, skb->len, DMA_TO_DEVICE);
  1511. ath_tx_complete(sc, skb, tx_flags);
  1512. /*
  1513. * Return the list of ath_buf of this mpdu to free queue
  1514. */
  1515. spin_lock_irqsave(&sc->tx.txbuflock, flags);
  1516. list_splice_tail_init(bf_q, &sc->tx.txbuf);
  1517. spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
  1518. }
  1519. static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
  1520. int txok)
  1521. {
  1522. struct ath_buf *bf_last = bf->bf_lastbf;
  1523. struct ath_desc *ds = bf_last->bf_desc;
  1524. u16 seq_st = 0;
  1525. u32 ba[WME_BA_BMP_SIZE >> 5];
  1526. int ba_index;
  1527. int nbad = 0;
  1528. int isaggr = 0;
  1529. if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
  1530. return 0;
  1531. isaggr = bf_isaggr(bf);
  1532. if (isaggr) {
  1533. seq_st = ATH_DS_BA_SEQ(ds);
  1534. memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
  1535. }
  1536. while (bf) {
  1537. ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
  1538. if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
  1539. nbad++;
  1540. bf = bf->bf_next;
  1541. }
  1542. return nbad;
  1543. }
  1544. static void ath_tx_rc_status(struct ath_buf *bf, struct ath_desc *ds,
  1545. int nbad, int txok, bool update_rc)
  1546. {
  1547. struct sk_buff *skb = bf->bf_mpdu;
  1548. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1549. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1550. struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
  1551. struct ieee80211_hw *hw = tx_info_priv->aphy->hw;
  1552. u8 i, tx_rateindex;
  1553. if (txok)
  1554. tx_info->status.ack_signal = ds->ds_txstat.ts_rssi;
  1555. tx_rateindex = ds->ds_txstat.ts_rateindex;
  1556. WARN_ON(tx_rateindex >= hw->max_rates);
  1557. tx_info_priv->update_rc = update_rc;
  1558. if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
  1559. tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1560. if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
  1561. (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0 && update_rc) {
  1562. if (ieee80211_is_data(hdr->frame_control)) {
  1563. memcpy(&tx_info_priv->tx, &ds->ds_txstat,
  1564. sizeof(tx_info_priv->tx));
  1565. tx_info_priv->n_frames = bf->bf_nframes;
  1566. tx_info_priv->n_bad_frames = nbad;
  1567. }
  1568. }
  1569. for (i = tx_rateindex + 1; i < hw->max_rates; i++)
  1570. tx_info->status.rates[i].count = 0;
  1571. tx_info->status.rates[tx_rateindex].count = bf->bf_retries + 1;
  1572. }
  1573. static void ath_wake_mac80211_queue(struct ath_softc *sc, struct ath_txq *txq)
  1574. {
  1575. int qnum;
  1576. spin_lock_bh(&txq->axq_lock);
  1577. if (txq->stopped &&
  1578. sc->tx.txq[txq->axq_qnum].axq_depth <= (ATH_TXBUF - 20)) {
  1579. qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
  1580. if (qnum != -1) {
  1581. ieee80211_wake_queue(sc->hw, qnum);
  1582. txq->stopped = 0;
  1583. }
  1584. }
  1585. spin_unlock_bh(&txq->axq_lock);
  1586. }
  1587. static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
  1588. {
  1589. struct ath_hw *ah = sc->sc_ah;
  1590. struct ath_buf *bf, *lastbf, *bf_held = NULL;
  1591. struct list_head bf_head;
  1592. struct ath_desc *ds;
  1593. int txok;
  1594. int status;
  1595. DPRINTF(sc, ATH_DBG_QUEUE, "tx queue %d (%x), link %p\n",
  1596. txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
  1597. txq->axq_link);
  1598. for (;;) {
  1599. spin_lock_bh(&txq->axq_lock);
  1600. if (list_empty(&txq->axq_q)) {
  1601. txq->axq_link = NULL;
  1602. txq->axq_linkbuf = NULL;
  1603. spin_unlock_bh(&txq->axq_lock);
  1604. break;
  1605. }
  1606. bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
  1607. /*
  1608. * There is a race condition that a BH gets scheduled
  1609. * after sw writes TxE and before hw re-load the last
  1610. * descriptor to get the newly chained one.
  1611. * Software must keep the last DONE descriptor as a
  1612. * holding descriptor - software does so by marking
  1613. * it with the STALE flag.
  1614. */
  1615. bf_held = NULL;
  1616. if (bf->bf_stale) {
  1617. bf_held = bf;
  1618. if (list_is_last(&bf_held->list, &txq->axq_q)) {
  1619. txq->axq_link = NULL;
  1620. txq->axq_linkbuf = NULL;
  1621. spin_unlock_bh(&txq->axq_lock);
  1622. /*
  1623. * The holding descriptor is the last
  1624. * descriptor in queue. It's safe to remove
  1625. * the last holding descriptor in BH context.
  1626. */
  1627. spin_lock_bh(&sc->tx.txbuflock);
  1628. list_move_tail(&bf_held->list, &sc->tx.txbuf);
  1629. spin_unlock_bh(&sc->tx.txbuflock);
  1630. break;
  1631. } else {
  1632. bf = list_entry(bf_held->list.next,
  1633. struct ath_buf, list);
  1634. }
  1635. }
  1636. lastbf = bf->bf_lastbf;
  1637. ds = lastbf->bf_desc;
  1638. status = ath9k_hw_txprocdesc(ah, ds);
  1639. if (status == -EINPROGRESS) {
  1640. spin_unlock_bh(&txq->axq_lock);
  1641. break;
  1642. }
  1643. if (bf->bf_desc == txq->axq_lastdsWithCTS)
  1644. txq->axq_lastdsWithCTS = NULL;
  1645. if (ds == txq->axq_gatingds)
  1646. txq->axq_gatingds = NULL;
  1647. /*
  1648. * Remove ath_buf's of the same transmit unit from txq,
  1649. * however leave the last descriptor back as the holding
  1650. * descriptor for hw.
  1651. */
  1652. lastbf->bf_stale = true;
  1653. INIT_LIST_HEAD(&bf_head);
  1654. if (!list_is_singular(&lastbf->list))
  1655. list_cut_position(&bf_head,
  1656. &txq->axq_q, lastbf->list.prev);
  1657. txq->axq_depth--;
  1658. if (bf_isaggr(bf))
  1659. txq->axq_aggr_depth--;
  1660. txok = (ds->ds_txstat.ts_status == 0);
  1661. spin_unlock_bh(&txq->axq_lock);
  1662. if (bf_held) {
  1663. spin_lock_bh(&sc->tx.txbuflock);
  1664. list_move_tail(&bf_held->list, &sc->tx.txbuf);
  1665. spin_unlock_bh(&sc->tx.txbuflock);
  1666. }
  1667. if (!bf_isampdu(bf)) {
  1668. /*
  1669. * This frame is sent out as a single frame.
  1670. * Use hardware retry status for this frame.
  1671. */
  1672. bf->bf_retries = ds->ds_txstat.ts_longretry;
  1673. if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
  1674. bf->bf_state.bf_type |= BUF_XRETRY;
  1675. ath_tx_rc_status(bf, ds, 0, txok, true);
  1676. }
  1677. if (bf_isampdu(bf))
  1678. ath_tx_complete_aggr(sc, txq, bf, &bf_head, txok);
  1679. else
  1680. ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
  1681. ath_wake_mac80211_queue(sc, txq);
  1682. spin_lock_bh(&txq->axq_lock);
  1683. if (sc->sc_flags & SC_OP_TXAGGR)
  1684. ath_txq_schedule(sc, txq);
  1685. spin_unlock_bh(&txq->axq_lock);
  1686. }
  1687. }
  1688. void ath_tx_tasklet(struct ath_softc *sc)
  1689. {
  1690. int i;
  1691. u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
  1692. ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
  1693. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1694. if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
  1695. ath_tx_processq(sc, &sc->tx.txq[i]);
  1696. }
  1697. }
  1698. /*****************/
  1699. /* Init, Cleanup */
  1700. /*****************/
  1701. int ath_tx_init(struct ath_softc *sc, int nbufs)
  1702. {
  1703. int error = 0;
  1704. spin_lock_init(&sc->tx.txbuflock);
  1705. error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
  1706. "tx", nbufs, 1);
  1707. if (error != 0) {
  1708. DPRINTF(sc, ATH_DBG_FATAL,
  1709. "Failed to allocate tx descriptors: %d\n", error);
  1710. goto err;
  1711. }
  1712. error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
  1713. "beacon", ATH_BCBUF, 1);
  1714. if (error != 0) {
  1715. DPRINTF(sc, ATH_DBG_FATAL,
  1716. "Failed to allocate beacon descriptors: %d\n", error);
  1717. goto err;
  1718. }
  1719. err:
  1720. if (error != 0)
  1721. ath_tx_cleanup(sc);
  1722. return error;
  1723. }
  1724. void ath_tx_cleanup(struct ath_softc *sc)
  1725. {
  1726. if (sc->beacon.bdma.dd_desc_len != 0)
  1727. ath_descdma_cleanup(sc, &sc->beacon.bdma, &sc->beacon.bbuf);
  1728. if (sc->tx.txdma.dd_desc_len != 0)
  1729. ath_descdma_cleanup(sc, &sc->tx.txdma, &sc->tx.txbuf);
  1730. }
  1731. void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
  1732. {
  1733. struct ath_atx_tid *tid;
  1734. struct ath_atx_ac *ac;
  1735. int tidno, acno;
  1736. for (tidno = 0, tid = &an->tid[tidno];
  1737. tidno < WME_NUM_TID;
  1738. tidno++, tid++) {
  1739. tid->an = an;
  1740. tid->tidno = tidno;
  1741. tid->seq_start = tid->seq_next = 0;
  1742. tid->baw_size = WME_MAX_BA;
  1743. tid->baw_head = tid->baw_tail = 0;
  1744. tid->sched = false;
  1745. tid->paused = false;
  1746. tid->state &= ~AGGR_CLEANUP;
  1747. INIT_LIST_HEAD(&tid->buf_q);
  1748. acno = TID_TO_WME_AC(tidno);
  1749. tid->ac = &an->ac[acno];
  1750. tid->state &= ~AGGR_ADDBA_COMPLETE;
  1751. tid->state &= ~AGGR_ADDBA_PROGRESS;
  1752. tid->addba_exchangeattempts = 0;
  1753. }
  1754. for (acno = 0, ac = &an->ac[acno];
  1755. acno < WME_NUM_AC; acno++, ac++) {
  1756. ac->sched = false;
  1757. INIT_LIST_HEAD(&ac->tid_q);
  1758. switch (acno) {
  1759. case WME_AC_BE:
  1760. ac->qnum = ath_tx_get_qnum(sc,
  1761. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
  1762. break;
  1763. case WME_AC_BK:
  1764. ac->qnum = ath_tx_get_qnum(sc,
  1765. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
  1766. break;
  1767. case WME_AC_VI:
  1768. ac->qnum = ath_tx_get_qnum(sc,
  1769. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
  1770. break;
  1771. case WME_AC_VO:
  1772. ac->qnum = ath_tx_get_qnum(sc,
  1773. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
  1774. break;
  1775. }
  1776. }
  1777. }
  1778. void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
  1779. {
  1780. int i;
  1781. struct ath_atx_ac *ac, *ac_tmp;
  1782. struct ath_atx_tid *tid, *tid_tmp;
  1783. struct ath_txq *txq;
  1784. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1785. if (ATH_TXQ_SETUP(sc, i)) {
  1786. txq = &sc->tx.txq[i];
  1787. spin_lock(&txq->axq_lock);
  1788. list_for_each_entry_safe(ac,
  1789. ac_tmp, &txq->axq_acq, list) {
  1790. tid = list_first_entry(&ac->tid_q,
  1791. struct ath_atx_tid, list);
  1792. if (tid && tid->an != an)
  1793. continue;
  1794. list_del(&ac->list);
  1795. ac->sched = false;
  1796. list_for_each_entry_safe(tid,
  1797. tid_tmp, &ac->tid_q, list) {
  1798. list_del(&tid->list);
  1799. tid->sched = false;
  1800. ath_tid_drain(sc, txq, tid);
  1801. tid->state &= ~AGGR_ADDBA_COMPLETE;
  1802. tid->addba_exchangeattempts = 0;
  1803. tid->state &= ~AGGR_CLEANUP;
  1804. }
  1805. }
  1806. spin_unlock(&txq->axq_lock);
  1807. }
  1808. }
  1809. }