xmit.c 55 KB

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