xmit.c 55 KB

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