xmit.c 63 KB

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