xmit.c 55 KB

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