xmit.c 56 KB

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