xmit.c 56 KB

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