xmit.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783
  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. if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU)) {
  729. bf->bf_state.bf_type = 0;
  730. return bf;
  731. }
  732. bf->bf_state.bf_type = BUF_AMPDU | BUF_AGGR;
  733. seqno = bf->bf_state.seqno;
  734. /* do not step over block-ack window */
  735. if (!BAW_WITHIN(tid->seq_start, tid->baw_size, seqno))
  736. break;
  737. if (tid->bar_index > ATH_BA_INDEX(tid->seq_start, seqno)) {
  738. struct ath_tx_status ts = {};
  739. struct list_head bf_head;
  740. INIT_LIST_HEAD(&bf_head);
  741. list_add(&bf->list, &bf_head);
  742. __skb_unlink(skb, *q);
  743. ath_tx_update_baw(sc, tid, seqno);
  744. ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
  745. continue;
  746. }
  747. return bf;
  748. }
  749. return NULL;
  750. }
  751. static bool
  752. ath_tx_form_aggr(struct ath_softc *sc, struct ath_txq *txq,
  753. struct ath_atx_tid *tid, struct list_head *bf_q,
  754. struct ath_buf *bf_first, struct sk_buff_head *tid_q,
  755. int *aggr_len)
  756. {
  757. #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
  758. struct ath_buf *bf = bf_first, *bf_prev = NULL;
  759. int nframes = 0, ndelim;
  760. u16 aggr_limit = 0, al = 0, bpad = 0,
  761. al_delta, h_baw = tid->baw_size / 2;
  762. struct ieee80211_tx_info *tx_info;
  763. struct ath_frame_info *fi;
  764. struct sk_buff *skb;
  765. bool closed = false;
  766. bf = bf_first;
  767. aggr_limit = ath_lookup_rate(sc, bf, tid);
  768. do {
  769. skb = bf->bf_mpdu;
  770. fi = get_frame_info(skb);
  771. /* do not exceed aggregation limit */
  772. al_delta = ATH_AGGR_DELIM_SZ + fi->framelen;
  773. if (nframes) {
  774. if (aggr_limit < al + bpad + al_delta ||
  775. ath_lookup_legacy(bf) || nframes >= h_baw)
  776. break;
  777. tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
  778. if ((tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) ||
  779. !(tx_info->flags & IEEE80211_TX_CTL_AMPDU))
  780. break;
  781. }
  782. /* add padding for previous frame to aggregation length */
  783. al += bpad + al_delta;
  784. /*
  785. * Get the delimiters needed to meet the MPDU
  786. * density for this node.
  787. */
  788. ndelim = ath_compute_num_delims(sc, tid, bf_first, fi->framelen,
  789. !nframes);
  790. bpad = PADBYTES(al_delta) + (ndelim << 2);
  791. nframes++;
  792. bf->bf_next = NULL;
  793. /* link buffers of this frame to the aggregate */
  794. if (!fi->baw_tracked)
  795. ath_tx_addto_baw(sc, tid, bf);
  796. bf->bf_state.ndelim = ndelim;
  797. __skb_unlink(skb, tid_q);
  798. list_add_tail(&bf->list, bf_q);
  799. if (bf_prev)
  800. bf_prev->bf_next = bf;
  801. bf_prev = bf;
  802. bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
  803. if (!bf) {
  804. closed = true;
  805. break;
  806. }
  807. } while (ath_tid_has_buffered(tid));
  808. bf = bf_first;
  809. bf->bf_lastbf = bf_prev;
  810. if (bf == bf_prev) {
  811. al = get_frame_info(bf->bf_mpdu)->framelen;
  812. bf->bf_state.bf_type = BUF_AMPDU;
  813. } else {
  814. TX_STAT_INC(txq->axq_qnum, a_aggr);
  815. }
  816. *aggr_len = al;
  817. return closed;
  818. #undef PADBYTES
  819. }
  820. /*
  821. * rix - rate index
  822. * pktlen - total bytes (delims + data + fcs + pads + pad delims)
  823. * width - 0 for 20 MHz, 1 for 40 MHz
  824. * half_gi - to use 4us v/s 3.6 us for symbol time
  825. */
  826. static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, int pktlen,
  827. int width, int half_gi, bool shortPreamble)
  828. {
  829. u32 nbits, nsymbits, duration, nsymbols;
  830. int streams;
  831. /* find number of symbols: PLCP + data */
  832. streams = HT_RC_2_STREAMS(rix);
  833. nbits = (pktlen << 3) + OFDM_PLCP_BITS;
  834. nsymbits = bits_per_symbol[rix % 8][width] * streams;
  835. nsymbols = (nbits + nsymbits - 1) / nsymbits;
  836. if (!half_gi)
  837. duration = SYMBOL_TIME(nsymbols);
  838. else
  839. duration = SYMBOL_TIME_HALFGI(nsymbols);
  840. /* addup duration for legacy/ht training and signal fields */
  841. duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
  842. return duration;
  843. }
  844. static int ath_max_framelen(int usec, int mcs, bool ht40, bool sgi)
  845. {
  846. int streams = HT_RC_2_STREAMS(mcs);
  847. int symbols, bits;
  848. int bytes = 0;
  849. usec -= L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
  850. symbols = sgi ? TIME_SYMBOLS_HALFGI(usec) : TIME_SYMBOLS(usec);
  851. bits = symbols * bits_per_symbol[mcs % 8][ht40] * streams;
  852. bits -= OFDM_PLCP_BITS;
  853. bytes = bits / 8;
  854. if (bytes > 65532)
  855. bytes = 65532;
  856. return bytes;
  857. }
  858. void ath_update_max_aggr_framelen(struct ath_softc *sc, int queue, int txop)
  859. {
  860. u16 *cur_ht20, *cur_ht20_sgi, *cur_ht40, *cur_ht40_sgi;
  861. int mcs;
  862. /* 4ms is the default (and maximum) duration */
  863. if (!txop || txop > 4096)
  864. txop = 4096;
  865. cur_ht20 = sc->tx.max_aggr_framelen[queue][MCS_HT20];
  866. cur_ht20_sgi = sc->tx.max_aggr_framelen[queue][MCS_HT20_SGI];
  867. cur_ht40 = sc->tx.max_aggr_framelen[queue][MCS_HT40];
  868. cur_ht40_sgi = sc->tx.max_aggr_framelen[queue][MCS_HT40_SGI];
  869. for (mcs = 0; mcs < 32; mcs++) {
  870. cur_ht20[mcs] = ath_max_framelen(txop, mcs, false, false);
  871. cur_ht20_sgi[mcs] = ath_max_framelen(txop, mcs, false, true);
  872. cur_ht40[mcs] = ath_max_framelen(txop, mcs, true, false);
  873. cur_ht40_sgi[mcs] = ath_max_framelen(txop, mcs, true, true);
  874. }
  875. }
  876. static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf,
  877. struct ath_tx_info *info, int len, bool rts)
  878. {
  879. struct ath_hw *ah = sc->sc_ah;
  880. struct ath_common *common = ath9k_hw_common(ah);
  881. struct sk_buff *skb;
  882. struct ieee80211_tx_info *tx_info;
  883. struct ieee80211_tx_rate *rates;
  884. const struct ieee80211_rate *rate;
  885. struct ieee80211_hdr *hdr;
  886. struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
  887. u32 rts_thresh = sc->hw->wiphy->rts_threshold;
  888. int i;
  889. u8 rix = 0;
  890. skb = bf->bf_mpdu;
  891. tx_info = IEEE80211_SKB_CB(skb);
  892. rates = bf->rates;
  893. hdr = (struct ieee80211_hdr *)skb->data;
  894. /* set dur_update_en for l-sig computation except for PS-Poll frames */
  895. info->dur_update = !ieee80211_is_pspoll(hdr->frame_control);
  896. info->rtscts_rate = fi->rtscts_rate;
  897. for (i = 0; i < ARRAY_SIZE(bf->rates); i++) {
  898. bool is_40, is_sgi, is_sp;
  899. int phy;
  900. if (!rates[i].count || (rates[i].idx < 0))
  901. continue;
  902. rix = rates[i].idx;
  903. info->rates[i].Tries = rates[i].count;
  904. /*
  905. * Handle RTS threshold for unaggregated HT frames.
  906. */
  907. if (bf_isampdu(bf) && !bf_isaggr(bf) &&
  908. (rates[i].flags & IEEE80211_TX_RC_MCS) &&
  909. unlikely(rts_thresh != (u32) -1)) {
  910. if (!rts_thresh || (len > rts_thresh))
  911. rts = true;
  912. }
  913. if (rts || rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  914. info->rates[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
  915. info->flags |= ATH9K_TXDESC_RTSENA;
  916. } else if (rates[i].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  917. info->rates[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
  918. info->flags |= ATH9K_TXDESC_CTSENA;
  919. }
  920. if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  921. info->rates[i].RateFlags |= ATH9K_RATESERIES_2040;
  922. if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
  923. info->rates[i].RateFlags |= ATH9K_RATESERIES_HALFGI;
  924. is_sgi = !!(rates[i].flags & IEEE80211_TX_RC_SHORT_GI);
  925. is_40 = !!(rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH);
  926. is_sp = !!(rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
  927. if (rates[i].flags & IEEE80211_TX_RC_MCS) {
  928. /* MCS rates */
  929. info->rates[i].Rate = rix | 0x80;
  930. info->rates[i].ChSel = ath_txchainmask_reduction(sc,
  931. ah->txchainmask, info->rates[i].Rate);
  932. info->rates[i].PktDuration = ath_pkt_duration(sc, rix, len,
  933. is_40, is_sgi, is_sp);
  934. if (rix < 8 && (tx_info->flags & IEEE80211_TX_CTL_STBC))
  935. info->rates[i].RateFlags |= ATH9K_RATESERIES_STBC;
  936. continue;
  937. }
  938. /* legacy rates */
  939. rate = &common->sbands[tx_info->band].bitrates[rates[i].idx];
  940. if ((tx_info->band == IEEE80211_BAND_2GHZ) &&
  941. !(rate->flags & IEEE80211_RATE_ERP_G))
  942. phy = WLAN_RC_PHY_CCK;
  943. else
  944. phy = WLAN_RC_PHY_OFDM;
  945. info->rates[i].Rate = rate->hw_value;
  946. if (rate->hw_value_short) {
  947. if (rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  948. info->rates[i].Rate |= rate->hw_value_short;
  949. } else {
  950. is_sp = false;
  951. }
  952. if (bf->bf_state.bfs_paprd)
  953. info->rates[i].ChSel = ah->txchainmask;
  954. else
  955. info->rates[i].ChSel = ath_txchainmask_reduction(sc,
  956. ah->txchainmask, info->rates[i].Rate);
  957. info->rates[i].PktDuration = ath9k_hw_computetxtime(sc->sc_ah,
  958. phy, rate->bitrate * 100, len, rix, is_sp);
  959. }
  960. /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
  961. if (bf_isaggr(bf) && (len > sc->sc_ah->caps.rts_aggr_limit))
  962. info->flags &= ~ATH9K_TXDESC_RTSENA;
  963. /* ATH9K_TXDESC_RTSENA and ATH9K_TXDESC_CTSENA are mutually exclusive. */
  964. if (info->flags & ATH9K_TXDESC_RTSENA)
  965. info->flags &= ~ATH9K_TXDESC_CTSENA;
  966. }
  967. static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
  968. {
  969. struct ieee80211_hdr *hdr;
  970. enum ath9k_pkt_type htype;
  971. __le16 fc;
  972. hdr = (struct ieee80211_hdr *)skb->data;
  973. fc = hdr->frame_control;
  974. if (ieee80211_is_beacon(fc))
  975. htype = ATH9K_PKT_TYPE_BEACON;
  976. else if (ieee80211_is_probe_resp(fc))
  977. htype = ATH9K_PKT_TYPE_PROBE_RESP;
  978. else if (ieee80211_is_atim(fc))
  979. htype = ATH9K_PKT_TYPE_ATIM;
  980. else if (ieee80211_is_pspoll(fc))
  981. htype = ATH9K_PKT_TYPE_PSPOLL;
  982. else
  983. htype = ATH9K_PKT_TYPE_NORMAL;
  984. return htype;
  985. }
  986. static void ath_tx_fill_desc(struct ath_softc *sc, struct ath_buf *bf,
  987. struct ath_txq *txq, int len)
  988. {
  989. struct ath_hw *ah = sc->sc_ah;
  990. struct ath_buf *bf_first = NULL;
  991. struct ath_tx_info info;
  992. u32 rts_thresh = sc->hw->wiphy->rts_threshold;
  993. bool rts = false;
  994. memset(&info, 0, sizeof(info));
  995. info.is_first = true;
  996. info.is_last = true;
  997. info.txpower = MAX_RATE_POWER;
  998. info.qcu = txq->axq_qnum;
  999. while (bf) {
  1000. struct sk_buff *skb = bf->bf_mpdu;
  1001. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1002. struct ath_frame_info *fi = get_frame_info(skb);
  1003. bool aggr = !!(bf->bf_state.bf_type & BUF_AGGR);
  1004. info.type = get_hw_packet_type(skb);
  1005. if (bf->bf_next)
  1006. info.link = bf->bf_next->bf_daddr;
  1007. else
  1008. info.link = (sc->tx99_state) ? bf->bf_daddr : 0;
  1009. if (!bf_first) {
  1010. bf_first = bf;
  1011. if (!sc->tx99_state)
  1012. info.flags = ATH9K_TXDESC_INTREQ;
  1013. if ((tx_info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT) ||
  1014. txq == sc->tx.uapsdq)
  1015. info.flags |= ATH9K_TXDESC_CLRDMASK;
  1016. if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
  1017. info.flags |= ATH9K_TXDESC_NOACK;
  1018. if (tx_info->flags & IEEE80211_TX_CTL_LDPC)
  1019. info.flags |= ATH9K_TXDESC_LDPC;
  1020. if (bf->bf_state.bfs_paprd)
  1021. info.flags |= (u32) bf->bf_state.bfs_paprd <<
  1022. ATH9K_TXDESC_PAPRD_S;
  1023. /*
  1024. * mac80211 doesn't handle RTS threshold for HT because
  1025. * the decision has to be taken based on AMPDU length
  1026. * and aggregation is done entirely inside ath9k.
  1027. * Set the RTS/CTS flag for the first subframe based
  1028. * on the threshold.
  1029. */
  1030. if (aggr && (bf == bf_first) &&
  1031. unlikely(rts_thresh != (u32) -1)) {
  1032. /*
  1033. * "len" is the size of the entire AMPDU.
  1034. */
  1035. if (!rts_thresh || (len > rts_thresh))
  1036. rts = true;
  1037. }
  1038. if (!aggr)
  1039. len = fi->framelen;
  1040. ath_buf_set_rate(sc, bf, &info, len, rts);
  1041. }
  1042. info.buf_addr[0] = bf->bf_buf_addr;
  1043. info.buf_len[0] = skb->len;
  1044. info.pkt_len = fi->framelen;
  1045. info.keyix = fi->keyix;
  1046. info.keytype = fi->keytype;
  1047. if (aggr) {
  1048. if (bf == bf_first)
  1049. info.aggr = AGGR_BUF_FIRST;
  1050. else if (bf == bf_first->bf_lastbf)
  1051. info.aggr = AGGR_BUF_LAST;
  1052. else
  1053. info.aggr = AGGR_BUF_MIDDLE;
  1054. info.ndelim = bf->bf_state.ndelim;
  1055. info.aggr_len = len;
  1056. }
  1057. if (bf == bf_first->bf_lastbf)
  1058. bf_first = NULL;
  1059. ath9k_hw_set_txdesc(ah, bf->bf_desc, &info);
  1060. bf = bf->bf_next;
  1061. }
  1062. }
  1063. static void
  1064. ath_tx_form_burst(struct ath_softc *sc, struct ath_txq *txq,
  1065. struct ath_atx_tid *tid, struct list_head *bf_q,
  1066. struct ath_buf *bf_first, struct sk_buff_head *tid_q)
  1067. {
  1068. struct ath_buf *bf = bf_first, *bf_prev = NULL;
  1069. struct sk_buff *skb;
  1070. int nframes = 0;
  1071. do {
  1072. struct ieee80211_tx_info *tx_info;
  1073. skb = bf->bf_mpdu;
  1074. nframes++;
  1075. __skb_unlink(skb, tid_q);
  1076. list_add_tail(&bf->list, bf_q);
  1077. if (bf_prev)
  1078. bf_prev->bf_next = bf;
  1079. bf_prev = bf;
  1080. if (nframes >= 2)
  1081. break;
  1082. bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
  1083. if (!bf)
  1084. break;
  1085. tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
  1086. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
  1087. break;
  1088. ath_set_rates(tid->an->vif, tid->an->sta, bf);
  1089. } while (1);
  1090. }
  1091. static bool ath_tx_sched_aggr(struct ath_softc *sc, struct ath_txq *txq,
  1092. struct ath_atx_tid *tid, bool *stop)
  1093. {
  1094. struct ath_buf *bf;
  1095. struct ieee80211_tx_info *tx_info;
  1096. struct sk_buff_head *tid_q;
  1097. struct list_head bf_q;
  1098. int aggr_len = 0;
  1099. bool aggr, last = true;
  1100. if (!ath_tid_has_buffered(tid))
  1101. return false;
  1102. INIT_LIST_HEAD(&bf_q);
  1103. bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
  1104. if (!bf)
  1105. return false;
  1106. tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
  1107. aggr = !!(tx_info->flags & IEEE80211_TX_CTL_AMPDU);
  1108. if ((aggr && txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH) ||
  1109. (!aggr && txq->axq_depth >= ATH_NON_AGGR_MIN_QDEPTH)) {
  1110. *stop = true;
  1111. return false;
  1112. }
  1113. ath_set_rates(tid->an->vif, tid->an->sta, bf);
  1114. if (aggr)
  1115. last = ath_tx_form_aggr(sc, txq, tid, &bf_q, bf,
  1116. tid_q, &aggr_len);
  1117. else
  1118. ath_tx_form_burst(sc, txq, tid, &bf_q, bf, tid_q);
  1119. if (list_empty(&bf_q))
  1120. return false;
  1121. if (tid->ac->clear_ps_filter || tid->an->no_ps_filter) {
  1122. tid->ac->clear_ps_filter = false;
  1123. tx_info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1124. }
  1125. ath_tx_fill_desc(sc, bf, txq, aggr_len);
  1126. ath_tx_txqaddbuf(sc, txq, &bf_q, false);
  1127. return true;
  1128. }
  1129. int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
  1130. u16 tid, u16 *ssn)
  1131. {
  1132. struct ath_atx_tid *txtid;
  1133. struct ath_txq *txq;
  1134. struct ath_node *an;
  1135. u8 density;
  1136. an = (struct ath_node *)sta->drv_priv;
  1137. txtid = ATH_AN_2_TID(an, tid);
  1138. txq = txtid->ac->txq;
  1139. ath_txq_lock(sc, txq);
  1140. /* update ampdu factor/density, they may have changed. This may happen
  1141. * in HT IBSS when a beacon with HT-info is received after the station
  1142. * has already been added.
  1143. */
  1144. if (sta->ht_cap.ht_supported) {
  1145. an->maxampdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  1146. sta->ht_cap.ampdu_factor)) - 1;
  1147. density = ath9k_parse_mpdudensity(sta->ht_cap.ampdu_density);
  1148. an->mpdudensity = density;
  1149. }
  1150. /* force sequence number allocation for pending frames */
  1151. ath_tx_tid_change_state(sc, txtid);
  1152. txtid->active = true;
  1153. *ssn = txtid->seq_start = txtid->seq_next;
  1154. txtid->bar_index = -1;
  1155. memset(txtid->tx_buf, 0, sizeof(txtid->tx_buf));
  1156. txtid->baw_head = txtid->baw_tail = 0;
  1157. ath_txq_unlock_complete(sc, txq);
  1158. return 0;
  1159. }
  1160. void ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
  1161. {
  1162. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  1163. struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
  1164. struct ath_txq *txq = txtid->ac->txq;
  1165. ath_txq_lock(sc, txq);
  1166. txtid->active = false;
  1167. ath_tx_flush_tid(sc, txtid);
  1168. ath_tx_tid_change_state(sc, txtid);
  1169. ath_txq_unlock_complete(sc, txq);
  1170. }
  1171. void ath_tx_aggr_sleep(struct ieee80211_sta *sta, struct ath_softc *sc,
  1172. struct ath_node *an)
  1173. {
  1174. struct ath_atx_tid *tid;
  1175. struct ath_atx_ac *ac;
  1176. struct ath_txq *txq;
  1177. bool buffered;
  1178. int tidno;
  1179. for (tidno = 0, tid = &an->tid[tidno];
  1180. tidno < IEEE80211_NUM_TIDS; tidno++, tid++) {
  1181. ac = tid->ac;
  1182. txq = ac->txq;
  1183. ath_txq_lock(sc, txq);
  1184. if (!tid->sched) {
  1185. ath_txq_unlock(sc, txq);
  1186. continue;
  1187. }
  1188. buffered = ath_tid_has_buffered(tid);
  1189. tid->sched = false;
  1190. list_del(&tid->list);
  1191. if (ac->sched) {
  1192. ac->sched = false;
  1193. list_del(&ac->list);
  1194. }
  1195. ath_txq_unlock(sc, txq);
  1196. ieee80211_sta_set_buffered(sta, tidno, buffered);
  1197. }
  1198. }
  1199. void ath_tx_aggr_wakeup(struct ath_softc *sc, struct ath_node *an)
  1200. {
  1201. struct ath_atx_tid *tid;
  1202. struct ath_atx_ac *ac;
  1203. struct ath_txq *txq;
  1204. int tidno;
  1205. for (tidno = 0, tid = &an->tid[tidno];
  1206. tidno < IEEE80211_NUM_TIDS; tidno++, tid++) {
  1207. ac = tid->ac;
  1208. txq = ac->txq;
  1209. ath_txq_lock(sc, txq);
  1210. ac->clear_ps_filter = true;
  1211. if (ath_tid_has_buffered(tid)) {
  1212. ath_tx_queue_tid(txq, tid);
  1213. ath_txq_schedule(sc, txq);
  1214. }
  1215. ath_txq_unlock_complete(sc, txq);
  1216. }
  1217. }
  1218. void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta,
  1219. u16 tidno)
  1220. {
  1221. struct ath_atx_tid *tid;
  1222. struct ath_node *an;
  1223. struct ath_txq *txq;
  1224. an = (struct ath_node *)sta->drv_priv;
  1225. tid = ATH_AN_2_TID(an, tidno);
  1226. txq = tid->ac->txq;
  1227. ath_txq_lock(sc, txq);
  1228. tid->baw_size = IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
  1229. if (ath_tid_has_buffered(tid)) {
  1230. ath_tx_queue_tid(txq, tid);
  1231. ath_txq_schedule(sc, txq);
  1232. }
  1233. ath_txq_unlock_complete(sc, txq);
  1234. }
  1235. void ath9k_release_buffered_frames(struct ieee80211_hw *hw,
  1236. struct ieee80211_sta *sta,
  1237. u16 tids, int nframes,
  1238. enum ieee80211_frame_release_type reason,
  1239. bool more_data)
  1240. {
  1241. struct ath_softc *sc = hw->priv;
  1242. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  1243. struct ath_txq *txq = sc->tx.uapsdq;
  1244. struct ieee80211_tx_info *info;
  1245. struct list_head bf_q;
  1246. struct ath_buf *bf_tail = NULL, *bf;
  1247. struct sk_buff_head *tid_q;
  1248. int sent = 0;
  1249. int i;
  1250. INIT_LIST_HEAD(&bf_q);
  1251. for (i = 0; tids && nframes; i++, tids >>= 1) {
  1252. struct ath_atx_tid *tid;
  1253. if (!(tids & 1))
  1254. continue;
  1255. tid = ATH_AN_2_TID(an, i);
  1256. ath_txq_lock(sc, tid->ac->txq);
  1257. while (nframes > 0) {
  1258. bf = ath_tx_get_tid_subframe(sc, sc->tx.uapsdq, tid, &tid_q);
  1259. if (!bf)
  1260. break;
  1261. __skb_unlink(bf->bf_mpdu, tid_q);
  1262. list_add_tail(&bf->list, &bf_q);
  1263. ath_set_rates(tid->an->vif, tid->an->sta, bf);
  1264. if (bf_isampdu(bf)) {
  1265. ath_tx_addto_baw(sc, tid, bf);
  1266. bf->bf_state.bf_type &= ~BUF_AGGR;
  1267. }
  1268. if (bf_tail)
  1269. bf_tail->bf_next = bf;
  1270. bf_tail = bf;
  1271. nframes--;
  1272. sent++;
  1273. TX_STAT_INC(txq->axq_qnum, a_queued_hw);
  1274. if (an->sta && !ath_tid_has_buffered(tid))
  1275. ieee80211_sta_set_buffered(an->sta, i, false);
  1276. }
  1277. ath_txq_unlock_complete(sc, tid->ac->txq);
  1278. }
  1279. if (list_empty(&bf_q))
  1280. return;
  1281. info = IEEE80211_SKB_CB(bf_tail->bf_mpdu);
  1282. info->flags |= IEEE80211_TX_STATUS_EOSP;
  1283. bf = list_first_entry(&bf_q, struct ath_buf, list);
  1284. ath_txq_lock(sc, txq);
  1285. ath_tx_fill_desc(sc, bf, txq, 0);
  1286. ath_tx_txqaddbuf(sc, txq, &bf_q, false);
  1287. ath_txq_unlock(sc, txq);
  1288. }
  1289. /********************/
  1290. /* Queue Management */
  1291. /********************/
  1292. struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
  1293. {
  1294. struct ath_hw *ah = sc->sc_ah;
  1295. struct ath9k_tx_queue_info qi;
  1296. static const int subtype_txq_to_hwq[] = {
  1297. [IEEE80211_AC_BE] = ATH_TXQ_AC_BE,
  1298. [IEEE80211_AC_BK] = ATH_TXQ_AC_BK,
  1299. [IEEE80211_AC_VI] = ATH_TXQ_AC_VI,
  1300. [IEEE80211_AC_VO] = ATH_TXQ_AC_VO,
  1301. };
  1302. int axq_qnum, i;
  1303. memset(&qi, 0, sizeof(qi));
  1304. qi.tqi_subtype = subtype_txq_to_hwq[subtype];
  1305. qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
  1306. qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
  1307. qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
  1308. qi.tqi_physCompBuf = 0;
  1309. /*
  1310. * Enable interrupts only for EOL and DESC conditions.
  1311. * We mark tx descriptors to receive a DESC interrupt
  1312. * when a tx queue gets deep; otherwise waiting for the
  1313. * EOL to reap descriptors. Note that this is done to
  1314. * reduce interrupt load and this only defers reaping
  1315. * descriptors, never transmitting frames. Aside from
  1316. * reducing interrupts this also permits more concurrency.
  1317. * The only potential downside is if the tx queue backs
  1318. * up in which case the top half of the kernel may backup
  1319. * due to a lack of tx descriptors.
  1320. *
  1321. * The UAPSD queue is an exception, since we take a desc-
  1322. * based intr on the EOSP frames.
  1323. */
  1324. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  1325. qi.tqi_qflags = TXQ_FLAG_TXINT_ENABLE;
  1326. } else {
  1327. if (qtype == ATH9K_TX_QUEUE_UAPSD)
  1328. qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
  1329. else
  1330. qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
  1331. TXQ_FLAG_TXDESCINT_ENABLE;
  1332. }
  1333. axq_qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
  1334. if (axq_qnum == -1) {
  1335. /*
  1336. * NB: don't print a message, this happens
  1337. * normally on parts with too few tx queues
  1338. */
  1339. return NULL;
  1340. }
  1341. if (!ATH_TXQ_SETUP(sc, axq_qnum)) {
  1342. struct ath_txq *txq = &sc->tx.txq[axq_qnum];
  1343. txq->axq_qnum = axq_qnum;
  1344. txq->mac80211_qnum = -1;
  1345. txq->axq_link = NULL;
  1346. __skb_queue_head_init(&txq->complete_q);
  1347. INIT_LIST_HEAD(&txq->axq_q);
  1348. INIT_LIST_HEAD(&txq->axq_acq);
  1349. spin_lock_init(&txq->axq_lock);
  1350. txq->axq_depth = 0;
  1351. txq->axq_ampdu_depth = 0;
  1352. txq->axq_tx_inprogress = false;
  1353. sc->tx.txqsetup |= 1<<axq_qnum;
  1354. txq->txq_headidx = txq->txq_tailidx = 0;
  1355. for (i = 0; i < ATH_TXFIFO_DEPTH; i++)
  1356. INIT_LIST_HEAD(&txq->txq_fifo[i]);
  1357. }
  1358. return &sc->tx.txq[axq_qnum];
  1359. }
  1360. int ath_txq_update(struct ath_softc *sc, int qnum,
  1361. struct ath9k_tx_queue_info *qinfo)
  1362. {
  1363. struct ath_hw *ah = sc->sc_ah;
  1364. int error = 0;
  1365. struct ath9k_tx_queue_info qi;
  1366. BUG_ON(sc->tx.txq[qnum].axq_qnum != qnum);
  1367. ath9k_hw_get_txq_props(ah, qnum, &qi);
  1368. qi.tqi_aifs = qinfo->tqi_aifs;
  1369. qi.tqi_cwmin = qinfo->tqi_cwmin;
  1370. qi.tqi_cwmax = qinfo->tqi_cwmax;
  1371. qi.tqi_burstTime = qinfo->tqi_burstTime;
  1372. qi.tqi_readyTime = qinfo->tqi_readyTime;
  1373. if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
  1374. ath_err(ath9k_hw_common(sc->sc_ah),
  1375. "Unable to update hardware queue %u!\n", qnum);
  1376. error = -EIO;
  1377. } else {
  1378. ath9k_hw_resettxqueue(ah, qnum);
  1379. }
  1380. return error;
  1381. }
  1382. int ath_cabq_update(struct ath_softc *sc)
  1383. {
  1384. struct ath9k_tx_queue_info qi;
  1385. struct ath_beacon_config *cur_conf = &sc->cur_beacon_conf;
  1386. int qnum = sc->beacon.cabq->axq_qnum;
  1387. ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
  1388. qi.tqi_readyTime = (TU_TO_USEC(cur_conf->beacon_interval) *
  1389. ATH_CABQ_READY_TIME) / 100;
  1390. ath_txq_update(sc, qnum, &qi);
  1391. return 0;
  1392. }
  1393. static void ath_drain_txq_list(struct ath_softc *sc, struct ath_txq *txq,
  1394. struct list_head *list)
  1395. {
  1396. struct ath_buf *bf, *lastbf;
  1397. struct list_head bf_head;
  1398. struct ath_tx_status ts;
  1399. memset(&ts, 0, sizeof(ts));
  1400. ts.ts_status = ATH9K_TX_FLUSH;
  1401. INIT_LIST_HEAD(&bf_head);
  1402. while (!list_empty(list)) {
  1403. bf = list_first_entry(list, struct ath_buf, list);
  1404. if (bf->bf_state.stale) {
  1405. list_del(&bf->list);
  1406. ath_tx_return_buffer(sc, bf);
  1407. continue;
  1408. }
  1409. lastbf = bf->bf_lastbf;
  1410. list_cut_position(&bf_head, list, &lastbf->list);
  1411. ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
  1412. }
  1413. }
  1414. /*
  1415. * Drain a given TX queue (could be Beacon or Data)
  1416. *
  1417. * This assumes output has been stopped and
  1418. * we do not need to block ath_tx_tasklet.
  1419. */
  1420. void ath_draintxq(struct ath_softc *sc, struct ath_txq *txq)
  1421. {
  1422. ath_txq_lock(sc, txq);
  1423. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  1424. int idx = txq->txq_tailidx;
  1425. while (!list_empty(&txq->txq_fifo[idx])) {
  1426. ath_drain_txq_list(sc, txq, &txq->txq_fifo[idx]);
  1427. INCR(idx, ATH_TXFIFO_DEPTH);
  1428. }
  1429. txq->txq_tailidx = idx;
  1430. }
  1431. txq->axq_link = NULL;
  1432. txq->axq_tx_inprogress = false;
  1433. ath_drain_txq_list(sc, txq, &txq->axq_q);
  1434. ath_txq_unlock_complete(sc, txq);
  1435. }
  1436. bool ath_drain_all_txq(struct ath_softc *sc)
  1437. {
  1438. struct ath_hw *ah = sc->sc_ah;
  1439. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1440. struct ath_txq *txq;
  1441. int i;
  1442. u32 npend = 0;
  1443. if (test_bit(ATH_OP_INVALID, &common->op_flags))
  1444. return true;
  1445. ath9k_hw_abort_tx_dma(ah);
  1446. /* Check if any queue remains active */
  1447. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1448. if (!ATH_TXQ_SETUP(sc, i))
  1449. continue;
  1450. if (!sc->tx.txq[i].axq_depth)
  1451. continue;
  1452. if (ath9k_hw_numtxpending(ah, sc->tx.txq[i].axq_qnum))
  1453. npend |= BIT(i);
  1454. }
  1455. if (npend)
  1456. ath_err(common, "Failed to stop TX DMA, queues=0x%03x!\n", npend);
  1457. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1458. if (!ATH_TXQ_SETUP(sc, i))
  1459. continue;
  1460. /*
  1461. * The caller will resume queues with ieee80211_wake_queues.
  1462. * Mark the queue as not stopped to prevent ath_tx_complete
  1463. * from waking the queue too early.
  1464. */
  1465. txq = &sc->tx.txq[i];
  1466. txq->stopped = false;
  1467. ath_draintxq(sc, txq);
  1468. }
  1469. return !npend;
  1470. }
  1471. void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
  1472. {
  1473. ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
  1474. sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
  1475. }
  1476. /* For each axq_acq entry, for each tid, try to schedule packets
  1477. * for transmit until ampdu_depth has reached min Q depth.
  1478. */
  1479. void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
  1480. {
  1481. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1482. struct ath_atx_ac *ac, *last_ac;
  1483. struct ath_atx_tid *tid, *last_tid;
  1484. bool sent = false;
  1485. if (test_bit(ATH_OP_HW_RESET, &common->op_flags) ||
  1486. list_empty(&txq->axq_acq))
  1487. return;
  1488. rcu_read_lock();
  1489. last_ac = list_entry(txq->axq_acq.prev, struct ath_atx_ac, list);
  1490. while (!list_empty(&txq->axq_acq)) {
  1491. bool stop = false;
  1492. ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
  1493. last_tid = list_entry(ac->tid_q.prev, struct ath_atx_tid, list);
  1494. list_del(&ac->list);
  1495. ac->sched = false;
  1496. while (!list_empty(&ac->tid_q)) {
  1497. tid = list_first_entry(&ac->tid_q, struct ath_atx_tid,
  1498. list);
  1499. list_del(&tid->list);
  1500. tid->sched = false;
  1501. if (ath_tx_sched_aggr(sc, txq, tid, &stop))
  1502. sent = true;
  1503. /*
  1504. * add tid to round-robin queue if more frames
  1505. * are pending for the tid
  1506. */
  1507. if (ath_tid_has_buffered(tid))
  1508. ath_tx_queue_tid(txq, tid);
  1509. if (stop || tid == last_tid)
  1510. break;
  1511. }
  1512. if (!list_empty(&ac->tid_q) && !ac->sched) {
  1513. ac->sched = true;
  1514. list_add_tail(&ac->list, &txq->axq_acq);
  1515. }
  1516. if (stop)
  1517. break;
  1518. if (ac == last_ac) {
  1519. if (!sent)
  1520. break;
  1521. sent = false;
  1522. last_ac = list_entry(txq->axq_acq.prev,
  1523. struct ath_atx_ac, list);
  1524. }
  1525. }
  1526. rcu_read_unlock();
  1527. }
  1528. /***********/
  1529. /* TX, DMA */
  1530. /***********/
  1531. /*
  1532. * Insert a chain of ath_buf (descriptors) on a txq and
  1533. * assume the descriptors are already chained together by caller.
  1534. */
  1535. static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
  1536. struct list_head *head, bool internal)
  1537. {
  1538. struct ath_hw *ah = sc->sc_ah;
  1539. struct ath_common *common = ath9k_hw_common(ah);
  1540. struct ath_buf *bf, *bf_last;
  1541. bool puttxbuf = false;
  1542. bool edma;
  1543. /*
  1544. * Insert the frame on the outbound list and
  1545. * pass it on to the hardware.
  1546. */
  1547. if (list_empty(head))
  1548. return;
  1549. edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  1550. bf = list_first_entry(head, struct ath_buf, list);
  1551. bf_last = list_entry(head->prev, struct ath_buf, list);
  1552. ath_dbg(common, QUEUE, "qnum: %d, txq depth: %d\n",
  1553. txq->axq_qnum, txq->axq_depth);
  1554. if (edma && list_empty(&txq->txq_fifo[txq->txq_headidx])) {
  1555. list_splice_tail_init(head, &txq->txq_fifo[txq->txq_headidx]);
  1556. INCR(txq->txq_headidx, ATH_TXFIFO_DEPTH);
  1557. puttxbuf = true;
  1558. } else {
  1559. list_splice_tail_init(head, &txq->axq_q);
  1560. if (txq->axq_link) {
  1561. ath9k_hw_set_desc_link(ah, txq->axq_link, bf->bf_daddr);
  1562. ath_dbg(common, XMIT, "link[%u] (%p)=%llx (%p)\n",
  1563. txq->axq_qnum, txq->axq_link,
  1564. ito64(bf->bf_daddr), bf->bf_desc);
  1565. } else if (!edma)
  1566. puttxbuf = true;
  1567. txq->axq_link = bf_last->bf_desc;
  1568. }
  1569. if (puttxbuf) {
  1570. TX_STAT_INC(txq->axq_qnum, puttxbuf);
  1571. ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
  1572. ath_dbg(common, XMIT, "TXDP[%u] = %llx (%p)\n",
  1573. txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
  1574. }
  1575. if (!edma || sc->tx99_state) {
  1576. TX_STAT_INC(txq->axq_qnum, txstart);
  1577. ath9k_hw_txstart(ah, txq->axq_qnum);
  1578. }
  1579. if (!internal) {
  1580. while (bf) {
  1581. txq->axq_depth++;
  1582. if (bf_is_ampdu_not_probing(bf))
  1583. txq->axq_ampdu_depth++;
  1584. bf_last = bf->bf_lastbf;
  1585. bf = bf_last->bf_next;
  1586. bf_last->bf_next = NULL;
  1587. }
  1588. }
  1589. }
  1590. static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
  1591. struct ath_atx_tid *tid, struct sk_buff *skb)
  1592. {
  1593. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1594. struct ath_frame_info *fi = get_frame_info(skb);
  1595. struct list_head bf_head;
  1596. struct ath_buf *bf = fi->bf;
  1597. INIT_LIST_HEAD(&bf_head);
  1598. list_add_tail(&bf->list, &bf_head);
  1599. bf->bf_state.bf_type = 0;
  1600. if (tid && (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) {
  1601. bf->bf_state.bf_type = BUF_AMPDU;
  1602. ath_tx_addto_baw(sc, tid, bf);
  1603. }
  1604. bf->bf_next = NULL;
  1605. bf->bf_lastbf = bf;
  1606. ath_tx_fill_desc(sc, bf, txq, fi->framelen);
  1607. ath_tx_txqaddbuf(sc, txq, &bf_head, false);
  1608. TX_STAT_INC(txq->axq_qnum, queued);
  1609. }
  1610. static void setup_frame_info(struct ieee80211_hw *hw,
  1611. struct ieee80211_sta *sta,
  1612. struct sk_buff *skb,
  1613. int framelen)
  1614. {
  1615. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1616. struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
  1617. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1618. const struct ieee80211_rate *rate;
  1619. struct ath_frame_info *fi = get_frame_info(skb);
  1620. struct ath_node *an = NULL;
  1621. enum ath9k_key_type keytype;
  1622. bool short_preamble = false;
  1623. /*
  1624. * We check if Short Preamble is needed for the CTS rate by
  1625. * checking the BSS's global flag.
  1626. * But for the rate series, IEEE80211_TX_RC_USE_SHORT_PREAMBLE is used.
  1627. */
  1628. if (tx_info->control.vif &&
  1629. tx_info->control.vif->bss_conf.use_short_preamble)
  1630. short_preamble = true;
  1631. rate = ieee80211_get_rts_cts_rate(hw, tx_info);
  1632. keytype = ath9k_cmn_get_hw_crypto_keytype(skb);
  1633. if (sta)
  1634. an = (struct ath_node *) sta->drv_priv;
  1635. memset(fi, 0, sizeof(*fi));
  1636. if (hw_key)
  1637. fi->keyix = hw_key->hw_key_idx;
  1638. else if (an && ieee80211_is_data(hdr->frame_control) && an->ps_key > 0)
  1639. fi->keyix = an->ps_key;
  1640. else
  1641. fi->keyix = ATH9K_TXKEYIX_INVALID;
  1642. fi->keytype = keytype;
  1643. fi->framelen = framelen;
  1644. if (!rate)
  1645. return;
  1646. fi->rtscts_rate = rate->hw_value;
  1647. if (short_preamble)
  1648. fi->rtscts_rate |= rate->hw_value_short;
  1649. }
  1650. u8 ath_txchainmask_reduction(struct ath_softc *sc, u8 chainmask, u32 rate)
  1651. {
  1652. struct ath_hw *ah = sc->sc_ah;
  1653. struct ath9k_channel *curchan = ah->curchan;
  1654. if ((ah->caps.hw_caps & ATH9K_HW_CAP_APM) && IS_CHAN_5GHZ(curchan) &&
  1655. (chainmask == 0x7) && (rate < 0x90))
  1656. return 0x3;
  1657. else if (AR_SREV_9462(ah) && ath9k_hw_btcoex_is_enabled(ah) &&
  1658. IS_CCK_RATE(rate))
  1659. return 0x2;
  1660. else
  1661. return chainmask;
  1662. }
  1663. /*
  1664. * Assign a descriptor (and sequence number if necessary,
  1665. * and map buffer for DMA. Frees skb on error
  1666. */
  1667. static struct ath_buf *ath_tx_setup_buffer(struct ath_softc *sc,
  1668. struct ath_txq *txq,
  1669. struct ath_atx_tid *tid,
  1670. struct sk_buff *skb)
  1671. {
  1672. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1673. struct ath_frame_info *fi = get_frame_info(skb);
  1674. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1675. struct ath_buf *bf;
  1676. int fragno;
  1677. u16 seqno;
  1678. bf = ath_tx_get_buffer(sc);
  1679. if (!bf) {
  1680. ath_dbg(common, XMIT, "TX buffers are full\n");
  1681. return NULL;
  1682. }
  1683. ATH_TXBUF_RESET(bf);
  1684. if (tid && ieee80211_is_data_present(hdr->frame_control)) {
  1685. fragno = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  1686. seqno = tid->seq_next;
  1687. hdr->seq_ctrl = cpu_to_le16(tid->seq_next << IEEE80211_SEQ_SEQ_SHIFT);
  1688. if (fragno)
  1689. hdr->seq_ctrl |= cpu_to_le16(fragno);
  1690. if (!ieee80211_has_morefrags(hdr->frame_control))
  1691. INCR(tid->seq_next, IEEE80211_SEQ_MAX);
  1692. bf->bf_state.seqno = seqno;
  1693. }
  1694. bf->bf_mpdu = skb;
  1695. bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
  1696. skb->len, DMA_TO_DEVICE);
  1697. if (unlikely(dma_mapping_error(sc->dev, bf->bf_buf_addr))) {
  1698. bf->bf_mpdu = NULL;
  1699. bf->bf_buf_addr = 0;
  1700. ath_err(ath9k_hw_common(sc->sc_ah),
  1701. "dma_mapping_error() on TX\n");
  1702. ath_tx_return_buffer(sc, bf);
  1703. return NULL;
  1704. }
  1705. fi->bf = bf;
  1706. return bf;
  1707. }
  1708. static int ath_tx_prepare(struct ieee80211_hw *hw, struct sk_buff *skb,
  1709. struct ath_tx_control *txctl)
  1710. {
  1711. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1712. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1713. struct ieee80211_sta *sta = txctl->sta;
  1714. struct ieee80211_vif *vif = info->control.vif;
  1715. struct ath_vif *avp;
  1716. struct ath_softc *sc = hw->priv;
  1717. int frmlen = skb->len + FCS_LEN;
  1718. int padpos, padsize;
  1719. /* NOTE: sta can be NULL according to net/mac80211.h */
  1720. if (sta)
  1721. txctl->an = (struct ath_node *)sta->drv_priv;
  1722. else if (vif && ieee80211_is_data(hdr->frame_control)) {
  1723. avp = (void *)vif->drv_priv;
  1724. txctl->an = &avp->mcast_node;
  1725. }
  1726. if (info->control.hw_key)
  1727. frmlen += info->control.hw_key->icv_len;
  1728. /*
  1729. * As a temporary workaround, assign seq# here; this will likely need
  1730. * to be cleaned up to work better with Beacon transmission and virtual
  1731. * BSSes.
  1732. */
  1733. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1734. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  1735. sc->tx.seq_no += 0x10;
  1736. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1737. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  1738. }
  1739. if ((vif && vif->type != NL80211_IFTYPE_AP &&
  1740. vif->type != NL80211_IFTYPE_AP_VLAN) ||
  1741. !ieee80211_is_data(hdr->frame_control))
  1742. info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
  1743. /* Add the padding after the header if this is not already done */
  1744. padpos = ieee80211_hdrlen(hdr->frame_control);
  1745. padsize = padpos & 3;
  1746. if (padsize && skb->len > padpos) {
  1747. if (skb_headroom(skb) < padsize)
  1748. return -ENOMEM;
  1749. skb_push(skb, padsize);
  1750. memmove(skb->data, skb->data + padsize, padpos);
  1751. }
  1752. setup_frame_info(hw, sta, skb, frmlen);
  1753. return 0;
  1754. }
  1755. /* Upon failure caller should free skb */
  1756. int ath_tx_start(struct ieee80211_hw *hw, struct sk_buff *skb,
  1757. struct ath_tx_control *txctl)
  1758. {
  1759. struct ieee80211_hdr *hdr;
  1760. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1761. struct ieee80211_sta *sta = txctl->sta;
  1762. struct ieee80211_vif *vif = info->control.vif;
  1763. struct ath_softc *sc = hw->priv;
  1764. struct ath_txq *txq = txctl->txq;
  1765. struct ath_atx_tid *tid = NULL;
  1766. struct ath_buf *bf;
  1767. int q;
  1768. int ret;
  1769. ret = ath_tx_prepare(hw, skb, txctl);
  1770. if (ret)
  1771. return ret;
  1772. hdr = (struct ieee80211_hdr *) skb->data;
  1773. /*
  1774. * At this point, the vif, hw_key and sta pointers in the tx control
  1775. * info are no longer valid (overwritten by the ath_frame_info data.
  1776. */
  1777. q = skb_get_queue_mapping(skb);
  1778. ath_txq_lock(sc, txq);
  1779. if (txq == sc->tx.txq_map[q] &&
  1780. ++txq->pending_frames > sc->tx.txq_max_pending[q] &&
  1781. !txq->stopped) {
  1782. ieee80211_stop_queue(sc->hw, q);
  1783. txq->stopped = true;
  1784. }
  1785. if (txctl->an && ieee80211_is_data_present(hdr->frame_control))
  1786. tid = ath_get_skb_tid(sc, txctl->an, skb);
  1787. if (info->flags & IEEE80211_TX_CTL_PS_RESPONSE) {
  1788. ath_txq_unlock(sc, txq);
  1789. txq = sc->tx.uapsdq;
  1790. ath_txq_lock(sc, txq);
  1791. } else if (txctl->an &&
  1792. ieee80211_is_data_present(hdr->frame_control)) {
  1793. WARN_ON(tid->ac->txq != txctl->txq);
  1794. if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
  1795. tid->ac->clear_ps_filter = true;
  1796. /*
  1797. * Add this frame to software queue for scheduling later
  1798. * for aggregation.
  1799. */
  1800. TX_STAT_INC(txq->axq_qnum, a_queued_sw);
  1801. __skb_queue_tail(&tid->buf_q, skb);
  1802. if (!txctl->an->sleeping)
  1803. ath_tx_queue_tid(txq, tid);
  1804. ath_txq_schedule(sc, txq);
  1805. goto out;
  1806. }
  1807. bf = ath_tx_setup_buffer(sc, txq, tid, skb);
  1808. if (!bf) {
  1809. ath_txq_skb_done(sc, txq, skb);
  1810. if (txctl->paprd)
  1811. dev_kfree_skb_any(skb);
  1812. else
  1813. ieee80211_free_txskb(sc->hw, skb);
  1814. goto out;
  1815. }
  1816. bf->bf_state.bfs_paprd = txctl->paprd;
  1817. if (txctl->paprd)
  1818. bf->bf_state.bfs_paprd_timestamp = jiffies;
  1819. ath_set_rates(vif, sta, bf);
  1820. ath_tx_send_normal(sc, txq, tid, skb);
  1821. out:
  1822. ath_txq_unlock(sc, txq);
  1823. return 0;
  1824. }
  1825. void ath_tx_cabq(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1826. struct sk_buff *skb)
  1827. {
  1828. struct ath_softc *sc = hw->priv;
  1829. struct ath_tx_control txctl = {
  1830. .txq = sc->beacon.cabq
  1831. };
  1832. struct ath_tx_info info = {};
  1833. struct ieee80211_hdr *hdr;
  1834. struct ath_buf *bf_tail = NULL;
  1835. struct ath_buf *bf;
  1836. LIST_HEAD(bf_q);
  1837. int duration = 0;
  1838. int max_duration;
  1839. max_duration =
  1840. sc->cur_beacon_conf.beacon_interval * 1000 *
  1841. sc->cur_beacon_conf.dtim_period / ATH_BCBUF;
  1842. do {
  1843. struct ath_frame_info *fi = get_frame_info(skb);
  1844. if (ath_tx_prepare(hw, skb, &txctl))
  1845. break;
  1846. bf = ath_tx_setup_buffer(sc, txctl.txq, NULL, skb);
  1847. if (!bf)
  1848. break;
  1849. bf->bf_lastbf = bf;
  1850. ath_set_rates(vif, NULL, bf);
  1851. ath_buf_set_rate(sc, bf, &info, fi->framelen, false);
  1852. duration += info.rates[0].PktDuration;
  1853. if (bf_tail)
  1854. bf_tail->bf_next = bf;
  1855. list_add_tail(&bf->list, &bf_q);
  1856. bf_tail = bf;
  1857. skb = NULL;
  1858. if (duration > max_duration)
  1859. break;
  1860. skb = ieee80211_get_buffered_bc(hw, vif);
  1861. } while(skb);
  1862. if (skb)
  1863. ieee80211_free_txskb(hw, skb);
  1864. if (list_empty(&bf_q))
  1865. return;
  1866. bf = list_first_entry(&bf_q, struct ath_buf, list);
  1867. hdr = (struct ieee80211_hdr *) bf->bf_mpdu->data;
  1868. if (hdr->frame_control & IEEE80211_FCTL_MOREDATA) {
  1869. hdr->frame_control &= ~IEEE80211_FCTL_MOREDATA;
  1870. dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
  1871. sizeof(*hdr), DMA_TO_DEVICE);
  1872. }
  1873. ath_txq_lock(sc, txctl.txq);
  1874. ath_tx_fill_desc(sc, bf, txctl.txq, 0);
  1875. ath_tx_txqaddbuf(sc, txctl.txq, &bf_q, false);
  1876. TX_STAT_INC(txctl.txq->axq_qnum, queued);
  1877. ath_txq_unlock(sc, txctl.txq);
  1878. }
  1879. /*****************/
  1880. /* TX Completion */
  1881. /*****************/
  1882. static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
  1883. int tx_flags, struct ath_txq *txq)
  1884. {
  1885. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1886. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1887. struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
  1888. int padpos, padsize;
  1889. unsigned long flags;
  1890. ath_dbg(common, XMIT, "TX complete: skb: %p\n", skb);
  1891. if (sc->sc_ah->caldata)
  1892. set_bit(PAPRD_PACKET_SENT, &sc->sc_ah->caldata->cal_flags);
  1893. if (!(tx_flags & ATH_TX_ERROR))
  1894. /* Frame was ACKed */
  1895. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  1896. padpos = ieee80211_hdrlen(hdr->frame_control);
  1897. padsize = padpos & 3;
  1898. if (padsize && skb->len>padpos+padsize) {
  1899. /*
  1900. * Remove MAC header padding before giving the frame back to
  1901. * mac80211.
  1902. */
  1903. memmove(skb->data + padsize, skb->data, padpos);
  1904. skb_pull(skb, padsize);
  1905. }
  1906. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1907. if ((sc->ps_flags & PS_WAIT_FOR_TX_ACK) && !txq->axq_depth) {
  1908. sc->ps_flags &= ~PS_WAIT_FOR_TX_ACK;
  1909. ath_dbg(common, PS,
  1910. "Going back to sleep after having received TX status (0x%lx)\n",
  1911. sc->ps_flags & (PS_WAIT_FOR_BEACON |
  1912. PS_WAIT_FOR_CAB |
  1913. PS_WAIT_FOR_PSPOLL_DATA |
  1914. PS_WAIT_FOR_TX_ACK));
  1915. }
  1916. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1917. __skb_queue_tail(&txq->complete_q, skb);
  1918. ath_txq_skb_done(sc, txq, skb);
  1919. }
  1920. static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
  1921. struct ath_txq *txq, struct list_head *bf_q,
  1922. struct ath_tx_status *ts, int txok)
  1923. {
  1924. struct sk_buff *skb = bf->bf_mpdu;
  1925. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1926. unsigned long flags;
  1927. int tx_flags = 0;
  1928. if (!txok)
  1929. tx_flags |= ATH_TX_ERROR;
  1930. if (ts->ts_status & ATH9K_TXERR_FILT)
  1931. tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1932. dma_unmap_single(sc->dev, bf->bf_buf_addr, skb->len, DMA_TO_DEVICE);
  1933. bf->bf_buf_addr = 0;
  1934. if (sc->tx99_state)
  1935. goto skip_tx_complete;
  1936. if (bf->bf_state.bfs_paprd) {
  1937. if (time_after(jiffies,
  1938. bf->bf_state.bfs_paprd_timestamp +
  1939. msecs_to_jiffies(ATH_PAPRD_TIMEOUT)))
  1940. dev_kfree_skb_any(skb);
  1941. else
  1942. complete(&sc->paprd_complete);
  1943. } else {
  1944. ath_debug_stat_tx(sc, bf, ts, txq, tx_flags);
  1945. ath_tx_complete(sc, skb, tx_flags, txq);
  1946. }
  1947. skip_tx_complete:
  1948. /* At this point, skb (bf->bf_mpdu) is consumed...make sure we don't
  1949. * accidentally reference it later.
  1950. */
  1951. bf->bf_mpdu = NULL;
  1952. /*
  1953. * Return the list of ath_buf of this mpdu to free queue
  1954. */
  1955. spin_lock_irqsave(&sc->tx.txbuflock, flags);
  1956. list_splice_tail_init(bf_q, &sc->tx.txbuf);
  1957. spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
  1958. }
  1959. static void ath_tx_rc_status(struct ath_softc *sc, struct ath_buf *bf,
  1960. struct ath_tx_status *ts, int nframes, int nbad,
  1961. int txok)
  1962. {
  1963. struct sk_buff *skb = bf->bf_mpdu;
  1964. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1965. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1966. struct ieee80211_hw *hw = sc->hw;
  1967. struct ath_hw *ah = sc->sc_ah;
  1968. u8 i, tx_rateindex;
  1969. if (txok)
  1970. tx_info->status.ack_signal = ts->ts_rssi;
  1971. tx_rateindex = ts->ts_rateindex;
  1972. WARN_ON(tx_rateindex >= hw->max_rates);
  1973. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
  1974. tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
  1975. BUG_ON(nbad > nframes);
  1976. }
  1977. tx_info->status.ampdu_len = nframes;
  1978. tx_info->status.ampdu_ack_len = nframes - nbad;
  1979. if ((ts->ts_status & ATH9K_TXERR_FILT) == 0 &&
  1980. (tx_info->flags & IEEE80211_TX_CTL_NO_ACK) == 0) {
  1981. /*
  1982. * If an underrun error is seen assume it as an excessive
  1983. * retry only if max frame trigger level has been reached
  1984. * (2 KB for single stream, and 4 KB for dual stream).
  1985. * Adjust the long retry as if the frame was tried
  1986. * hw->max_rate_tries times to affect how rate control updates
  1987. * PER for the failed rate.
  1988. * In case of congestion on the bus penalizing this type of
  1989. * underruns should help hardware actually transmit new frames
  1990. * successfully by eventually preferring slower rates.
  1991. * This itself should also alleviate congestion on the bus.
  1992. */
  1993. if (unlikely(ts->ts_flags & (ATH9K_TX_DATA_UNDERRUN |
  1994. ATH9K_TX_DELIM_UNDERRUN)) &&
  1995. ieee80211_is_data(hdr->frame_control) &&
  1996. ah->tx_trig_level >= sc->sc_ah->config.max_txtrig_level)
  1997. tx_info->status.rates[tx_rateindex].count =
  1998. hw->max_rate_tries;
  1999. }
  2000. for (i = tx_rateindex + 1; i < hw->max_rates; i++) {
  2001. tx_info->status.rates[i].count = 0;
  2002. tx_info->status.rates[i].idx = -1;
  2003. }
  2004. tx_info->status.rates[tx_rateindex].count = ts->ts_longretry + 1;
  2005. }
  2006. static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
  2007. {
  2008. struct ath_hw *ah = sc->sc_ah;
  2009. struct ath_common *common = ath9k_hw_common(ah);
  2010. struct ath_buf *bf, *lastbf, *bf_held = NULL;
  2011. struct list_head bf_head;
  2012. struct ath_desc *ds;
  2013. struct ath_tx_status ts;
  2014. int status;
  2015. ath_dbg(common, QUEUE, "tx queue %d (%x), link %p\n",
  2016. txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
  2017. txq->axq_link);
  2018. ath_txq_lock(sc, txq);
  2019. for (;;) {
  2020. if (test_bit(ATH_OP_HW_RESET, &common->op_flags))
  2021. break;
  2022. if (list_empty(&txq->axq_q)) {
  2023. txq->axq_link = NULL;
  2024. ath_txq_schedule(sc, txq);
  2025. break;
  2026. }
  2027. bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
  2028. /*
  2029. * There is a race condition that a BH gets scheduled
  2030. * after sw writes TxE and before hw re-load the last
  2031. * descriptor to get the newly chained one.
  2032. * Software must keep the last DONE descriptor as a
  2033. * holding descriptor - software does so by marking
  2034. * it with the STALE flag.
  2035. */
  2036. bf_held = NULL;
  2037. if (bf->bf_state.stale) {
  2038. bf_held = bf;
  2039. if (list_is_last(&bf_held->list, &txq->axq_q))
  2040. break;
  2041. bf = list_entry(bf_held->list.next, struct ath_buf,
  2042. list);
  2043. }
  2044. lastbf = bf->bf_lastbf;
  2045. ds = lastbf->bf_desc;
  2046. memset(&ts, 0, sizeof(ts));
  2047. status = ath9k_hw_txprocdesc(ah, ds, &ts);
  2048. if (status == -EINPROGRESS)
  2049. break;
  2050. TX_STAT_INC(txq->axq_qnum, txprocdesc);
  2051. /*
  2052. * Remove ath_buf's of the same transmit unit from txq,
  2053. * however leave the last descriptor back as the holding
  2054. * descriptor for hw.
  2055. */
  2056. lastbf->bf_state.stale = true;
  2057. INIT_LIST_HEAD(&bf_head);
  2058. if (!list_is_singular(&lastbf->list))
  2059. list_cut_position(&bf_head,
  2060. &txq->axq_q, lastbf->list.prev);
  2061. if (bf_held) {
  2062. list_del(&bf_held->list);
  2063. ath_tx_return_buffer(sc, bf_held);
  2064. }
  2065. ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
  2066. }
  2067. ath_txq_unlock_complete(sc, txq);
  2068. }
  2069. void ath_tx_tasklet(struct ath_softc *sc)
  2070. {
  2071. struct ath_hw *ah = sc->sc_ah;
  2072. u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1) & ah->intr_txqs;
  2073. int i;
  2074. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  2075. if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
  2076. ath_tx_processq(sc, &sc->tx.txq[i]);
  2077. }
  2078. }
  2079. void ath_tx_edma_tasklet(struct ath_softc *sc)
  2080. {
  2081. struct ath_tx_status ts;
  2082. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2083. struct ath_hw *ah = sc->sc_ah;
  2084. struct ath_txq *txq;
  2085. struct ath_buf *bf, *lastbf;
  2086. struct list_head bf_head;
  2087. struct list_head *fifo_list;
  2088. int status;
  2089. for (;;) {
  2090. if (test_bit(ATH_OP_HW_RESET, &common->op_flags))
  2091. break;
  2092. status = ath9k_hw_txprocdesc(ah, NULL, (void *)&ts);
  2093. if (status == -EINPROGRESS)
  2094. break;
  2095. if (status == -EIO) {
  2096. ath_dbg(common, XMIT, "Error processing tx status\n");
  2097. break;
  2098. }
  2099. /* Process beacon completions separately */
  2100. if (ts.qid == sc->beacon.beaconq) {
  2101. sc->beacon.tx_processed = true;
  2102. sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
  2103. ath9k_csa_update(sc);
  2104. continue;
  2105. }
  2106. txq = &sc->tx.txq[ts.qid];
  2107. ath_txq_lock(sc, txq);
  2108. TX_STAT_INC(txq->axq_qnum, txprocdesc);
  2109. fifo_list = &txq->txq_fifo[txq->txq_tailidx];
  2110. if (list_empty(fifo_list)) {
  2111. ath_txq_unlock(sc, txq);
  2112. return;
  2113. }
  2114. bf = list_first_entry(fifo_list, struct ath_buf, list);
  2115. if (bf->bf_state.stale) {
  2116. list_del(&bf->list);
  2117. ath_tx_return_buffer(sc, bf);
  2118. bf = list_first_entry(fifo_list, struct ath_buf, list);
  2119. }
  2120. lastbf = bf->bf_lastbf;
  2121. INIT_LIST_HEAD(&bf_head);
  2122. if (list_is_last(&lastbf->list, fifo_list)) {
  2123. list_splice_tail_init(fifo_list, &bf_head);
  2124. INCR(txq->txq_tailidx, ATH_TXFIFO_DEPTH);
  2125. if (!list_empty(&txq->axq_q)) {
  2126. struct list_head bf_q;
  2127. INIT_LIST_HEAD(&bf_q);
  2128. txq->axq_link = NULL;
  2129. list_splice_tail_init(&txq->axq_q, &bf_q);
  2130. ath_tx_txqaddbuf(sc, txq, &bf_q, true);
  2131. }
  2132. } else {
  2133. lastbf->bf_state.stale = true;
  2134. if (bf != lastbf)
  2135. list_cut_position(&bf_head, fifo_list,
  2136. lastbf->list.prev);
  2137. }
  2138. ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
  2139. ath_txq_unlock_complete(sc, txq);
  2140. }
  2141. }
  2142. /*****************/
  2143. /* Init, Cleanup */
  2144. /*****************/
  2145. static int ath_txstatus_setup(struct ath_softc *sc, int size)
  2146. {
  2147. struct ath_descdma *dd = &sc->txsdma;
  2148. u8 txs_len = sc->sc_ah->caps.txs_len;
  2149. dd->dd_desc_len = size * txs_len;
  2150. dd->dd_desc = dmam_alloc_coherent(sc->dev, dd->dd_desc_len,
  2151. &dd->dd_desc_paddr, GFP_KERNEL);
  2152. if (!dd->dd_desc)
  2153. return -ENOMEM;
  2154. return 0;
  2155. }
  2156. static int ath_tx_edma_init(struct ath_softc *sc)
  2157. {
  2158. int err;
  2159. err = ath_txstatus_setup(sc, ATH_TXSTATUS_RING_SIZE);
  2160. if (!err)
  2161. ath9k_hw_setup_statusring(sc->sc_ah, sc->txsdma.dd_desc,
  2162. sc->txsdma.dd_desc_paddr,
  2163. ATH_TXSTATUS_RING_SIZE);
  2164. return err;
  2165. }
  2166. int ath_tx_init(struct ath_softc *sc, int nbufs)
  2167. {
  2168. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2169. int error = 0;
  2170. spin_lock_init(&sc->tx.txbuflock);
  2171. error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
  2172. "tx", nbufs, 1, 1);
  2173. if (error != 0) {
  2174. ath_err(common,
  2175. "Failed to allocate tx descriptors: %d\n", error);
  2176. return error;
  2177. }
  2178. error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
  2179. "beacon", ATH_BCBUF, 1, 1);
  2180. if (error != 0) {
  2181. ath_err(common,
  2182. "Failed to allocate beacon descriptors: %d\n", error);
  2183. return error;
  2184. }
  2185. INIT_DELAYED_WORK(&sc->tx_complete_work, ath_tx_complete_poll_work);
  2186. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  2187. error = ath_tx_edma_init(sc);
  2188. return error;
  2189. }
  2190. void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
  2191. {
  2192. struct ath_atx_tid *tid;
  2193. struct ath_atx_ac *ac;
  2194. int tidno, acno;
  2195. for (tidno = 0, tid = &an->tid[tidno];
  2196. tidno < IEEE80211_NUM_TIDS;
  2197. tidno++, tid++) {
  2198. tid->an = an;
  2199. tid->tidno = tidno;
  2200. tid->seq_start = tid->seq_next = 0;
  2201. tid->baw_size = WME_MAX_BA;
  2202. tid->baw_head = tid->baw_tail = 0;
  2203. tid->sched = false;
  2204. tid->active = false;
  2205. __skb_queue_head_init(&tid->buf_q);
  2206. __skb_queue_head_init(&tid->retry_q);
  2207. acno = TID_TO_WME_AC(tidno);
  2208. tid->ac = &an->ac[acno];
  2209. }
  2210. for (acno = 0, ac = &an->ac[acno];
  2211. acno < IEEE80211_NUM_ACS; acno++, ac++) {
  2212. ac->sched = false;
  2213. ac->clear_ps_filter = true;
  2214. ac->txq = sc->tx.txq_map[acno];
  2215. INIT_LIST_HEAD(&ac->tid_q);
  2216. }
  2217. }
  2218. void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
  2219. {
  2220. struct ath_atx_ac *ac;
  2221. struct ath_atx_tid *tid;
  2222. struct ath_txq *txq;
  2223. int tidno;
  2224. for (tidno = 0, tid = &an->tid[tidno];
  2225. tidno < IEEE80211_NUM_TIDS; tidno++, tid++) {
  2226. ac = tid->ac;
  2227. txq = ac->txq;
  2228. ath_txq_lock(sc, txq);
  2229. if (tid->sched) {
  2230. list_del(&tid->list);
  2231. tid->sched = false;
  2232. }
  2233. if (ac->sched) {
  2234. list_del(&ac->list);
  2235. tid->ac->sched = false;
  2236. }
  2237. ath_tid_drain(sc, txq, tid);
  2238. tid->active = false;
  2239. ath_txq_unlock(sc, txq);
  2240. }
  2241. }
  2242. #ifdef CONFIG_ATH9K_TX99
  2243. int ath9k_tx99_send(struct ath_softc *sc, struct sk_buff *skb,
  2244. struct ath_tx_control *txctl)
  2245. {
  2246. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  2247. struct ath_frame_info *fi = get_frame_info(skb);
  2248. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2249. struct ath_buf *bf;
  2250. int padpos, padsize;
  2251. padpos = ieee80211_hdrlen(hdr->frame_control);
  2252. padsize = padpos & 3;
  2253. if (padsize && skb->len > padpos) {
  2254. if (skb_headroom(skb) < padsize) {
  2255. ath_dbg(common, XMIT,
  2256. "tx99 padding failed\n");
  2257. return -EINVAL;
  2258. }
  2259. skb_push(skb, padsize);
  2260. memmove(skb->data, skb->data + padsize, padpos);
  2261. }
  2262. fi->keyix = ATH9K_TXKEYIX_INVALID;
  2263. fi->framelen = skb->len + FCS_LEN;
  2264. fi->keytype = ATH9K_KEY_TYPE_CLEAR;
  2265. bf = ath_tx_setup_buffer(sc, txctl->txq, NULL, skb);
  2266. if (!bf) {
  2267. ath_dbg(common, XMIT, "tx99 buffer setup failed\n");
  2268. return -EINVAL;
  2269. }
  2270. ath_set_rates(sc->tx99_vif, NULL, bf);
  2271. ath9k_hw_set_desc_link(sc->sc_ah, bf->bf_desc, bf->bf_daddr);
  2272. ath9k_hw_tx99_start(sc->sc_ah, txctl->txq->axq_qnum);
  2273. ath_tx_send_normal(sc, txctl->txq, NULL, skb);
  2274. return 0;
  2275. }
  2276. #endif /* CONFIG_ATH9K_TX99 */