xmit.c 64 KB

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