xmit.c 56 KB

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