xmit.c 68 KB

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