xmit.c 63 KB

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