xmit.c 55 KB

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