xmit.c 64 KB

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