xmit.c 70 KB

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