xmit.c 54 KB

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