xmit.c 68 KB

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