xmit.c 62 KB

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