xmit.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  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_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->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 u32 ath_txq_depth(struct ath_softc *sc, int qnum)
  636. {
  637. return sc->tx.txq[qnum].axq_depth;
  638. }
  639. static void ath_get_beaconconfig(struct ath_softc *sc, int if_id,
  640. struct ath_beacon_config *conf)
  641. {
  642. struct ieee80211_hw *hw = sc->hw;
  643. /* fill in beacon config data */
  644. conf->beacon_interval = hw->conf.beacon_int;
  645. conf->listen_interval = 100;
  646. conf->dtim_count = 1;
  647. conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
  648. }
  649. static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
  650. struct ath_txq *txq)
  651. {
  652. struct ath_atx_ac *ac, *ac_tmp;
  653. struct ath_atx_tid *tid, *tid_tmp;
  654. list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
  655. list_del(&ac->list);
  656. ac->sched = false;
  657. list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
  658. list_del(&tid->list);
  659. tid->sched = false;
  660. ath_tid_drain(sc, txq, tid);
  661. }
  662. }
  663. }
  664. struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
  665. {
  666. struct ath_hal *ah = sc->sc_ah;
  667. struct ath9k_tx_queue_info qi;
  668. int qnum;
  669. memset(&qi, 0, sizeof(qi));
  670. qi.tqi_subtype = subtype;
  671. qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
  672. qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
  673. qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
  674. qi.tqi_physCompBuf = 0;
  675. /*
  676. * Enable interrupts only for EOL and DESC conditions.
  677. * We mark tx descriptors to receive a DESC interrupt
  678. * when a tx queue gets deep; otherwise waiting for the
  679. * EOL to reap descriptors. Note that this is done to
  680. * reduce interrupt load and this only defers reaping
  681. * descriptors, never transmitting frames. Aside from
  682. * reducing interrupts this also permits more concurrency.
  683. * The only potential downside is if the tx queue backs
  684. * up in which case the top half of the kernel may backup
  685. * due to a lack of tx descriptors.
  686. *
  687. * The UAPSD queue is an exception, since we take a desc-
  688. * based intr on the EOSP frames.
  689. */
  690. if (qtype == ATH9K_TX_QUEUE_UAPSD)
  691. qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
  692. else
  693. qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
  694. TXQ_FLAG_TXDESCINT_ENABLE;
  695. qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
  696. if (qnum == -1) {
  697. /*
  698. * NB: don't print a message, this happens
  699. * normally on parts with too few tx queues
  700. */
  701. return NULL;
  702. }
  703. if (qnum >= ARRAY_SIZE(sc->tx.txq)) {
  704. DPRINTF(sc, ATH_DBG_FATAL,
  705. "qnum %u out of range, max %u!\n",
  706. qnum, (unsigned int)ARRAY_SIZE(sc->tx.txq));
  707. ath9k_hw_releasetxqueue(ah, qnum);
  708. return NULL;
  709. }
  710. if (!ATH_TXQ_SETUP(sc, qnum)) {
  711. struct ath_txq *txq = &sc->tx.txq[qnum];
  712. txq->axq_qnum = qnum;
  713. txq->axq_link = NULL;
  714. INIT_LIST_HEAD(&txq->axq_q);
  715. INIT_LIST_HEAD(&txq->axq_acq);
  716. spin_lock_init(&txq->axq_lock);
  717. txq->axq_depth = 0;
  718. txq->axq_aggr_depth = 0;
  719. txq->axq_totalqueued = 0;
  720. txq->axq_linkbuf = NULL;
  721. sc->tx.txqsetup |= 1<<qnum;
  722. }
  723. return &sc->tx.txq[qnum];
  724. }
  725. static int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
  726. {
  727. int qnum;
  728. switch (qtype) {
  729. case ATH9K_TX_QUEUE_DATA:
  730. if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
  731. DPRINTF(sc, ATH_DBG_FATAL,
  732. "HAL AC %u out of range, max %zu!\n",
  733. haltype, ARRAY_SIZE(sc->tx.hwq_map));
  734. return -1;
  735. }
  736. qnum = sc->tx.hwq_map[haltype];
  737. break;
  738. case ATH9K_TX_QUEUE_BEACON:
  739. qnum = sc->beacon.beaconq;
  740. break;
  741. case ATH9K_TX_QUEUE_CAB:
  742. qnum = sc->beacon.cabq->axq_qnum;
  743. break;
  744. default:
  745. qnum = -1;
  746. }
  747. return qnum;
  748. }
  749. struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
  750. {
  751. struct ath_txq *txq = NULL;
  752. int qnum;
  753. qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
  754. txq = &sc->tx.txq[qnum];
  755. spin_lock_bh(&txq->axq_lock);
  756. if (txq->axq_depth >= (ATH_TXBUF - 20)) {
  757. DPRINTF(sc, ATH_DBG_FATAL,
  758. "TX queue: %d is full, depth: %d\n",
  759. qnum, txq->axq_depth);
  760. ieee80211_stop_queue(sc->hw, skb_get_queue_mapping(skb));
  761. txq->stopped = 1;
  762. spin_unlock_bh(&txq->axq_lock);
  763. return NULL;
  764. }
  765. spin_unlock_bh(&txq->axq_lock);
  766. return txq;
  767. }
  768. int ath_txq_update(struct ath_softc *sc, int qnum,
  769. struct ath9k_tx_queue_info *qinfo)
  770. {
  771. struct ath_hal *ah = sc->sc_ah;
  772. int error = 0;
  773. struct ath9k_tx_queue_info qi;
  774. if (qnum == sc->beacon.beaconq) {
  775. /*
  776. * XXX: for beacon queue, we just save the parameter.
  777. * It will be picked up by ath_beaconq_config when
  778. * it's necessary.
  779. */
  780. sc->beacon.beacon_qi = *qinfo;
  781. return 0;
  782. }
  783. ASSERT(sc->tx.txq[qnum].axq_qnum == qnum);
  784. ath9k_hw_get_txq_props(ah, qnum, &qi);
  785. qi.tqi_aifs = qinfo->tqi_aifs;
  786. qi.tqi_cwmin = qinfo->tqi_cwmin;
  787. qi.tqi_cwmax = qinfo->tqi_cwmax;
  788. qi.tqi_burstTime = qinfo->tqi_burstTime;
  789. qi.tqi_readyTime = qinfo->tqi_readyTime;
  790. if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
  791. DPRINTF(sc, ATH_DBG_FATAL,
  792. "Unable to update hardware queue %u!\n", qnum);
  793. error = -EIO;
  794. } else {
  795. ath9k_hw_resettxqueue(ah, qnum);
  796. }
  797. return error;
  798. }
  799. int ath_cabq_update(struct ath_softc *sc)
  800. {
  801. struct ath9k_tx_queue_info qi;
  802. int qnum = sc->beacon.cabq->axq_qnum;
  803. struct ath_beacon_config conf;
  804. ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
  805. /*
  806. * Ensure the readytime % is within the bounds.
  807. */
  808. if (sc->sc_config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
  809. sc->sc_config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
  810. else if (sc->sc_config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
  811. sc->sc_config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
  812. ath_get_beaconconfig(sc, ATH_IF_ID_ANY, &conf);
  813. qi.tqi_readyTime =
  814. (conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
  815. ath_txq_update(sc, qnum, &qi);
  816. return 0;
  817. }
  818. /*
  819. * Drain a given TX queue (could be Beacon or Data)
  820. *
  821. * This assumes output has been stopped and
  822. * we do not need to block ath_tx_tasklet.
  823. */
  824. void ath_draintxq(struct ath_softc *sc, struct ath_txq *txq, bool retry_tx)
  825. {
  826. struct ath_buf *bf, *lastbf;
  827. struct list_head bf_head;
  828. INIT_LIST_HEAD(&bf_head);
  829. for (;;) {
  830. spin_lock_bh(&txq->axq_lock);
  831. if (list_empty(&txq->axq_q)) {
  832. txq->axq_link = NULL;
  833. txq->axq_linkbuf = NULL;
  834. spin_unlock_bh(&txq->axq_lock);
  835. break;
  836. }
  837. bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
  838. if (bf->bf_status & ATH_BUFSTATUS_STALE) {
  839. list_del(&bf->list);
  840. spin_unlock_bh(&txq->axq_lock);
  841. spin_lock_bh(&sc->tx.txbuflock);
  842. list_add_tail(&bf->list, &sc->tx.txbuf);
  843. spin_unlock_bh(&sc->tx.txbuflock);
  844. continue;
  845. }
  846. lastbf = bf->bf_lastbf;
  847. if (!retry_tx)
  848. lastbf->bf_desc->ds_txstat.ts_flags =
  849. ATH9K_TX_SW_ABORTED;
  850. /* remove ath_buf's of the same mpdu from txq */
  851. list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
  852. txq->axq_depth--;
  853. spin_unlock_bh(&txq->axq_lock);
  854. if (bf_isampdu(bf))
  855. ath_tx_complete_aggr(sc, txq, bf, &bf_head, 0);
  856. else
  857. ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
  858. }
  859. /* flush any pending frames if aggregation is enabled */
  860. if (sc->sc_flags & SC_OP_TXAGGR) {
  861. if (!retry_tx) {
  862. spin_lock_bh(&txq->axq_lock);
  863. ath_txq_drain_pending_buffers(sc, txq);
  864. spin_unlock_bh(&txq->axq_lock);
  865. }
  866. }
  867. }
  868. void ath_drain_all_txq(struct ath_softc *sc, bool retry_tx)
  869. {
  870. struct ath_hal *ah = sc->sc_ah;
  871. struct ath_txq *txq;
  872. int i, npend = 0;
  873. if (sc->sc_flags & SC_OP_INVALID)
  874. return;
  875. /* Stop beacon queue */
  876. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  877. /* Stop data queues */
  878. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  879. if (ATH_TXQ_SETUP(sc, i)) {
  880. txq = &sc->tx.txq[i];
  881. ath9k_hw_stoptxdma(ah, txq->axq_qnum);
  882. npend += ath9k_hw_numtxpending(ah, txq->axq_qnum);
  883. }
  884. }
  885. if (npend) {
  886. int r;
  887. DPRINTF(sc, ATH_DBG_XMIT, "Unable to stop TxDMA. Reset HAL!\n");
  888. spin_lock_bh(&sc->sc_resetlock);
  889. r = ath9k_hw_reset(ah, sc->sc_ah->ah_curchan, true);
  890. if (r)
  891. DPRINTF(sc, ATH_DBG_FATAL,
  892. "Unable to reset hardware; reset status %u\n",
  893. r);
  894. spin_unlock_bh(&sc->sc_resetlock);
  895. }
  896. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  897. if (ATH_TXQ_SETUP(sc, i))
  898. ath_draintxq(sc, &sc->tx.txq[i], retry_tx);
  899. }
  900. }
  901. void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
  902. {
  903. ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
  904. sc->tx.txqsetup &= ~(1<<txq->axq_qnum);
  905. }
  906. void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
  907. {
  908. struct ath_atx_ac *ac;
  909. struct ath_atx_tid *tid;
  910. if (list_empty(&txq->axq_acq))
  911. return;
  912. ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
  913. list_del(&ac->list);
  914. ac->sched = false;
  915. do {
  916. if (list_empty(&ac->tid_q))
  917. return;
  918. tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
  919. list_del(&tid->list);
  920. tid->sched = false;
  921. if (tid->paused)
  922. continue;
  923. if ((txq->axq_depth % 2) == 0)
  924. ath_tx_sched_aggr(sc, txq, tid);
  925. /*
  926. * add tid to round-robin queue if more frames
  927. * are pending for the tid
  928. */
  929. if (!list_empty(&tid->buf_q))
  930. ath_tx_queue_tid(txq, tid);
  931. break;
  932. } while (!list_empty(&ac->tid_q));
  933. if (!list_empty(&ac->tid_q)) {
  934. if (!ac->sched) {
  935. ac->sched = true;
  936. list_add_tail(&ac->list, &txq->axq_acq);
  937. }
  938. }
  939. }
  940. int ath_tx_setup(struct ath_softc *sc, int haltype)
  941. {
  942. struct ath_txq *txq;
  943. if (haltype >= ARRAY_SIZE(sc->tx.hwq_map)) {
  944. DPRINTF(sc, ATH_DBG_FATAL,
  945. "HAL AC %u out of range, max %zu!\n",
  946. haltype, ARRAY_SIZE(sc->tx.hwq_map));
  947. return 0;
  948. }
  949. txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
  950. if (txq != NULL) {
  951. sc->tx.hwq_map[haltype] = txq->axq_qnum;
  952. return 1;
  953. } else
  954. return 0;
  955. }
  956. /***********/
  957. /* TX, DMA */
  958. /***********/
  959. /*
  960. * Insert a chain of ath_buf (descriptors) on a txq and
  961. * assume the descriptors are already chained together by caller.
  962. */
  963. static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
  964. struct list_head *head)
  965. {
  966. struct ath_hal *ah = sc->sc_ah;
  967. struct ath_buf *bf;
  968. /*
  969. * Insert the frame on the outbound list and
  970. * pass it on to the hardware.
  971. */
  972. if (list_empty(head))
  973. return;
  974. bf = list_first_entry(head, struct ath_buf, list);
  975. list_splice_tail_init(head, &txq->axq_q);
  976. txq->axq_depth++;
  977. txq->axq_totalqueued++;
  978. txq->axq_linkbuf = list_entry(txq->axq_q.prev, struct ath_buf, list);
  979. DPRINTF(sc, ATH_DBG_QUEUE,
  980. "qnum: %d, txq depth: %d\n", txq->axq_qnum, txq->axq_depth);
  981. if (txq->axq_link == NULL) {
  982. ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
  983. DPRINTF(sc, ATH_DBG_XMIT,
  984. "TXDP[%u] = %llx (%p)\n",
  985. txq->axq_qnum, ito64(bf->bf_daddr), bf->bf_desc);
  986. } else {
  987. *txq->axq_link = bf->bf_daddr;
  988. DPRINTF(sc, ATH_DBG_XMIT, "link[%u] (%p)=%llx (%p)\n",
  989. txq->axq_qnum, txq->axq_link,
  990. ito64(bf->bf_daddr), bf->bf_desc);
  991. }
  992. txq->axq_link = &(bf->bf_lastbf->bf_desc->ds_link);
  993. ath9k_hw_txstart(ah, txq->axq_qnum);
  994. }
  995. static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
  996. {
  997. struct ath_buf *bf = NULL;
  998. spin_lock_bh(&sc->tx.txbuflock);
  999. if (unlikely(list_empty(&sc->tx.txbuf))) {
  1000. spin_unlock_bh(&sc->tx.txbuflock);
  1001. return NULL;
  1002. }
  1003. bf = list_first_entry(&sc->tx.txbuf, struct ath_buf, list);
  1004. list_del(&bf->list);
  1005. spin_unlock_bh(&sc->tx.txbuflock);
  1006. return bf;
  1007. }
  1008. static void ath_tx_send_ampdu(struct ath_softc *sc, struct ath_atx_tid *tid,
  1009. struct list_head *bf_head,
  1010. struct ath_tx_control *txctl)
  1011. {
  1012. struct ath_buf *bf;
  1013. bf = list_first_entry(bf_head, struct ath_buf, list);
  1014. bf->bf_state.bf_type |= BUF_AMPDU;
  1015. /*
  1016. * Do not queue to h/w when any of the following conditions is true:
  1017. * - there are pending frames in software queue
  1018. * - the TID is currently paused for ADDBA/BAR request
  1019. * - seqno is not within block-ack window
  1020. * - h/w queue depth exceeds low water mark
  1021. */
  1022. if (!list_empty(&tid->buf_q) || tid->paused ||
  1023. !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
  1024. txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
  1025. /*
  1026. * Add this frame to software queue for scheduling later
  1027. * for aggregation.
  1028. */
  1029. list_move_tail(&bf->list, &tid->buf_q);
  1030. ath_tx_queue_tid(txctl->txq, tid);
  1031. return;
  1032. }
  1033. /* Add sub-frame to BAW */
  1034. ath_tx_addto_baw(sc, tid, bf);
  1035. /* Queue to h/w without aggregation */
  1036. bf->bf_nframes = 1;
  1037. bf->bf_lastbf = bf;
  1038. ath_buf_set_rate(sc, bf);
  1039. ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
  1040. }
  1041. static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
  1042. struct ath_atx_tid *tid,
  1043. struct list_head *bf_head)
  1044. {
  1045. struct ath_buf *bf;
  1046. bf = list_first_entry(bf_head, struct ath_buf, list);
  1047. bf->bf_state.bf_type &= ~BUF_AMPDU;
  1048. /* update starting sequence number for subsequent ADDBA request */
  1049. INCR(tid->seq_start, IEEE80211_SEQ_MAX);
  1050. bf->bf_nframes = 1;
  1051. bf->bf_lastbf = bf;
  1052. ath_buf_set_rate(sc, bf);
  1053. ath_tx_txqaddbuf(sc, txq, bf_head);
  1054. }
  1055. static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
  1056. {
  1057. struct ieee80211_hdr *hdr;
  1058. enum ath9k_pkt_type htype;
  1059. __le16 fc;
  1060. hdr = (struct ieee80211_hdr *)skb->data;
  1061. fc = hdr->frame_control;
  1062. if (ieee80211_is_beacon(fc))
  1063. htype = ATH9K_PKT_TYPE_BEACON;
  1064. else if (ieee80211_is_probe_resp(fc))
  1065. htype = ATH9K_PKT_TYPE_PROBE_RESP;
  1066. else if (ieee80211_is_atim(fc))
  1067. htype = ATH9K_PKT_TYPE_ATIM;
  1068. else if (ieee80211_is_pspoll(fc))
  1069. htype = ATH9K_PKT_TYPE_PSPOLL;
  1070. else
  1071. htype = ATH9K_PKT_TYPE_NORMAL;
  1072. return htype;
  1073. }
  1074. static bool is_pae(struct sk_buff *skb)
  1075. {
  1076. struct ieee80211_hdr *hdr;
  1077. __le16 fc;
  1078. hdr = (struct ieee80211_hdr *)skb->data;
  1079. fc = hdr->frame_control;
  1080. if (ieee80211_is_data(fc)) {
  1081. if (ieee80211_is_nullfunc(fc) ||
  1082. /* Port Access Entity (IEEE 802.1X) */
  1083. (skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  1084. return true;
  1085. }
  1086. }
  1087. return false;
  1088. }
  1089. static int get_hw_crypto_keytype(struct sk_buff *skb)
  1090. {
  1091. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1092. if (tx_info->control.hw_key) {
  1093. if (tx_info->control.hw_key->alg == ALG_WEP)
  1094. return ATH9K_KEY_TYPE_WEP;
  1095. else if (tx_info->control.hw_key->alg == ALG_TKIP)
  1096. return ATH9K_KEY_TYPE_TKIP;
  1097. else if (tx_info->control.hw_key->alg == ALG_CCMP)
  1098. return ATH9K_KEY_TYPE_AES;
  1099. }
  1100. return ATH9K_KEY_TYPE_CLEAR;
  1101. }
  1102. static void assign_aggr_tid_seqno(struct sk_buff *skb,
  1103. struct ath_buf *bf)
  1104. {
  1105. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1106. struct ieee80211_hdr *hdr;
  1107. struct ath_node *an;
  1108. struct ath_atx_tid *tid;
  1109. __le16 fc;
  1110. u8 *qc;
  1111. if (!tx_info->control.sta)
  1112. return;
  1113. an = (struct ath_node *)tx_info->control.sta->drv_priv;
  1114. hdr = (struct ieee80211_hdr *)skb->data;
  1115. fc = hdr->frame_control;
  1116. if (ieee80211_is_data_qos(fc)) {
  1117. qc = ieee80211_get_qos_ctl(hdr);
  1118. bf->bf_tidno = qc[0] & 0xf;
  1119. }
  1120. /*
  1121. * For HT capable stations, we save tidno for later use.
  1122. * We also override seqno set by upper layer with the one
  1123. * in tx aggregation state.
  1124. *
  1125. * If fragmentation is on, the sequence number is
  1126. * not overridden, since it has been
  1127. * incremented by the fragmentation routine.
  1128. *
  1129. * FIXME: check if the fragmentation threshold exceeds
  1130. * IEEE80211 max.
  1131. */
  1132. tid = ATH_AN_2_TID(an, bf->bf_tidno);
  1133. hdr->seq_ctrl = cpu_to_le16(tid->seq_next <<
  1134. IEEE80211_SEQ_SEQ_SHIFT);
  1135. bf->bf_seqno = tid->seq_next;
  1136. INCR(tid->seq_next, IEEE80211_SEQ_MAX);
  1137. }
  1138. static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
  1139. struct ath_txq *txq)
  1140. {
  1141. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1142. int flags = 0;
  1143. flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
  1144. flags |= ATH9K_TXDESC_INTREQ;
  1145. if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
  1146. flags |= ATH9K_TXDESC_NOACK;
  1147. return flags;
  1148. }
  1149. /*
  1150. * rix - rate index
  1151. * pktlen - total bytes (delims + data + fcs + pads + pad delims)
  1152. * width - 0 for 20 MHz, 1 for 40 MHz
  1153. * half_gi - to use 4us v/s 3.6 us for symbol time
  1154. */
  1155. static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
  1156. int width, int half_gi, bool shortPreamble)
  1157. {
  1158. struct ath_rate_table *rate_table = sc->cur_rate_table;
  1159. u32 nbits, nsymbits, duration, nsymbols;
  1160. u8 rc;
  1161. int streams, pktlen;
  1162. pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
  1163. rc = rate_table->info[rix].ratecode;
  1164. /* for legacy rates, use old function to compute packet duration */
  1165. if (!IS_HT_RATE(rc))
  1166. return ath9k_hw_computetxtime(sc->sc_ah, rate_table, pktlen,
  1167. rix, shortPreamble);
  1168. /* find number of symbols: PLCP + data */
  1169. nbits = (pktlen << 3) + OFDM_PLCP_BITS;
  1170. nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
  1171. nsymbols = (nbits + nsymbits - 1) / nsymbits;
  1172. if (!half_gi)
  1173. duration = SYMBOL_TIME(nsymbols);
  1174. else
  1175. duration = SYMBOL_TIME_HALFGI(nsymbols);
  1176. /* addup duration for legacy/ht training and signal fields */
  1177. streams = HT_RC_2_STREAMS(rc);
  1178. duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
  1179. return duration;
  1180. }
  1181. static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
  1182. {
  1183. struct ath_rate_table *rt = sc->cur_rate_table;
  1184. struct ath9k_11n_rate_series series[4];
  1185. struct sk_buff *skb;
  1186. struct ieee80211_tx_info *tx_info;
  1187. struct ieee80211_tx_rate *rates;
  1188. struct ieee80211_hdr *hdr;
  1189. int i, flags = 0;
  1190. u8 rix = 0, ctsrate = 0;
  1191. bool is_pspoll;
  1192. memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
  1193. skb = (struct sk_buff *)bf->bf_mpdu;
  1194. tx_info = IEEE80211_SKB_CB(skb);
  1195. rates = tx_info->control.rates;
  1196. hdr = (struct ieee80211_hdr *)skb->data;
  1197. is_pspoll = ieee80211_is_pspoll(hdr->frame_control);
  1198. /*
  1199. * We check if Short Preamble is needed for the CTS rate by
  1200. * checking the BSS's global flag.
  1201. * But for the rate series, IEEE80211_TX_RC_USE_SHORT_PREAMBLE is used.
  1202. */
  1203. if (sc->sc_flags & SC_OP_PREAMBLE_SHORT)
  1204. ctsrate = rt->info[tx_info->control.rts_cts_rate_idx].ratecode |
  1205. rt->info[tx_info->control.rts_cts_rate_idx].short_preamble;
  1206. else
  1207. ctsrate = rt->info[tx_info->control.rts_cts_rate_idx].ratecode;
  1208. /*
  1209. * ATH9K_TXDESC_RTSENA and ATH9K_TXDESC_CTSENA are mutually exclusive.
  1210. * Check the first rate in the series to decide whether RTS/CTS
  1211. * or CTS-to-self has to be used.
  1212. */
  1213. if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1214. flags = ATH9K_TXDESC_CTSENA;
  1215. else if (rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1216. flags = ATH9K_TXDESC_RTSENA;
  1217. /* FIXME: Handle aggregation protection */
  1218. if (sc->sc_config.ath_aggr_prot &&
  1219. (!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
  1220. flags = ATH9K_TXDESC_RTSENA;
  1221. }
  1222. /* For AR5416 - RTS cannot be followed by a frame larger than 8K */
  1223. if (bf_isaggr(bf) && (bf->bf_al > sc->sc_ah->ah_caps.rts_aggr_limit))
  1224. flags &= ~(ATH9K_TXDESC_RTSENA);
  1225. for (i = 0; i < 4; i++) {
  1226. if (!rates[i].count || (rates[i].idx < 0))
  1227. continue;
  1228. rix = rates[i].idx;
  1229. series[i].Tries = rates[i].count;
  1230. series[i].ChSel = sc->sc_tx_chainmask;
  1231. if (rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  1232. series[i].Rate = rt->info[rix].ratecode |
  1233. rt->info[rix].short_preamble;
  1234. else
  1235. series[i].Rate = rt->info[rix].ratecode;
  1236. if (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1237. series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
  1238. if (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1239. series[i].RateFlags |= ATH9K_RATESERIES_2040;
  1240. if (rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
  1241. series[i].RateFlags |= ATH9K_RATESERIES_HALFGI;
  1242. series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
  1243. (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) != 0,
  1244. (rates[i].flags & IEEE80211_TX_RC_SHORT_GI),
  1245. (rates[i].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE));
  1246. }
  1247. /* set dur_update_en for l-sig computation except for PS-Poll frames */
  1248. ath9k_hw_set11n_ratescenario(sc->sc_ah, bf->bf_desc,
  1249. bf->bf_lastbf->bf_desc,
  1250. !is_pspoll, ctsrate,
  1251. 0, series, 4, flags);
  1252. if (sc->sc_config.ath_aggr_prot && flags)
  1253. ath9k_hw_set11n_burstduration(sc->sc_ah, bf->bf_desc, 8192);
  1254. }
  1255. static int ath_tx_setup_buffer(struct ath_softc *sc, struct ath_buf *bf,
  1256. struct sk_buff *skb,
  1257. struct ath_tx_control *txctl)
  1258. {
  1259. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1260. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1261. struct ath_tx_info_priv *tx_info_priv;
  1262. int hdrlen;
  1263. __le16 fc;
  1264. tx_info_priv = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
  1265. if (unlikely(!tx_info_priv))
  1266. return -ENOMEM;
  1267. tx_info->rate_driver_data[0] = tx_info_priv;
  1268. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1269. fc = hdr->frame_control;
  1270. ATH_TXBUF_RESET(bf);
  1271. bf->bf_frmlen = skb->len + FCS_LEN - (hdrlen & 3);
  1272. if ((conf_is_ht(&sc->hw->conf) && !is_pae(skb) &&
  1273. (tx_info->flags & IEEE80211_TX_CTL_AMPDU)))
  1274. bf->bf_state.bf_type |= BUF_HT;
  1275. bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
  1276. bf->bf_keytype = get_hw_crypto_keytype(skb);
  1277. if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR) {
  1278. bf->bf_frmlen += tx_info->control.hw_key->icv_len;
  1279. bf->bf_keyix = tx_info->control.hw_key->hw_key_idx;
  1280. } else {
  1281. bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
  1282. }
  1283. if (ieee80211_is_data_qos(fc) && (sc->sc_flags & SC_OP_TXAGGR))
  1284. assign_aggr_tid_seqno(skb, bf);
  1285. bf->bf_mpdu = skb;
  1286. bf->bf_dmacontext = dma_map_single(sc->dev, skb->data,
  1287. skb->len, DMA_TO_DEVICE);
  1288. if (unlikely(dma_mapping_error(sc->dev, bf->bf_dmacontext))) {
  1289. bf->bf_mpdu = NULL;
  1290. DPRINTF(sc, ATH_DBG_CONFIG,
  1291. "dma_mapping_error() on TX\n");
  1292. return -ENOMEM;
  1293. }
  1294. bf->bf_buf_addr = bf->bf_dmacontext;
  1295. return 0;
  1296. }
  1297. /* FIXME: tx power */
  1298. static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
  1299. struct ath_tx_control *txctl)
  1300. {
  1301. struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
  1302. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1303. struct ath_node *an = NULL;
  1304. struct list_head bf_head;
  1305. struct ath_desc *ds;
  1306. struct ath_atx_tid *tid;
  1307. struct ath_hal *ah = sc->sc_ah;
  1308. int frm_type;
  1309. frm_type = get_hw_packet_type(skb);
  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 (ath_aggr_query(sc, an, bf->bf_tidno)) {
  1328. /*
  1329. * Try aggregation if it's a unicast data frame
  1330. * and the destination is HT capable.
  1331. */
  1332. ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
  1333. } else {
  1334. /*
  1335. * Send this frame as regular when ADDBA
  1336. * exchange is neither complete nor pending.
  1337. */
  1338. ath_tx_send_normal(sc, txctl->txq,
  1339. tid, &bf_head);
  1340. }
  1341. } else {
  1342. bf->bf_lastbf = bf;
  1343. bf->bf_nframes = 1;
  1344. ath_buf_set_rate(sc, bf);
  1345. ath_tx_txqaddbuf(sc, txctl->txq, &bf_head);
  1346. }
  1347. spin_unlock_bh(&txctl->txq->axq_lock);
  1348. }
  1349. /* Upon failure caller should free skb */
  1350. int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb,
  1351. struct ath_tx_control *txctl)
  1352. {
  1353. struct ath_buf *bf;
  1354. int r;
  1355. bf = ath_tx_get_buffer(sc);
  1356. if (!bf) {
  1357. DPRINTF(sc, ATH_DBG_XMIT, "TX buffers are full\n");
  1358. return -1;
  1359. }
  1360. r = ath_tx_setup_buffer(sc, bf, skb, txctl);
  1361. if (unlikely(r)) {
  1362. struct ath_txq *txq = txctl->txq;
  1363. DPRINTF(sc, ATH_DBG_FATAL, "TX mem alloc failure\n");
  1364. /* upon ath_tx_processq() this TX queue will be resumed, we
  1365. * guarantee this will happen by knowing beforehand that
  1366. * we will at least have to run TX completionon one buffer
  1367. * on the queue */
  1368. spin_lock_bh(&txq->axq_lock);
  1369. if (ath_txq_depth(sc, txq->axq_qnum) > 1) {
  1370. ieee80211_stop_queue(sc->hw,
  1371. skb_get_queue_mapping(skb));
  1372. txq->stopped = 1;
  1373. }
  1374. spin_unlock_bh(&txq->axq_lock);
  1375. spin_lock_bh(&sc->tx.txbuflock);
  1376. list_add_tail(&bf->list, &sc->tx.txbuf);
  1377. spin_unlock_bh(&sc->tx.txbuflock);
  1378. return r;
  1379. }
  1380. ath_tx_start_dma(sc, bf, txctl);
  1381. return 0;
  1382. }
  1383. void ath_tx_cabq(struct ath_softc *sc, struct sk_buff *skb)
  1384. {
  1385. int hdrlen, padsize;
  1386. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1387. struct ath_tx_control txctl;
  1388. memset(&txctl, 0, sizeof(struct ath_tx_control));
  1389. /*
  1390. * As a temporary workaround, assign seq# here; this will likely need
  1391. * to be cleaned up to work better with Beacon transmission and virtual
  1392. * BSSes.
  1393. */
  1394. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1395. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1396. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  1397. sc->tx.seq_no += 0x10;
  1398. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1399. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  1400. }
  1401. /* Add the padding after the header if this is not already done */
  1402. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1403. if (hdrlen & 3) {
  1404. padsize = hdrlen % 4;
  1405. if (skb_headroom(skb) < padsize) {
  1406. DPRINTF(sc, ATH_DBG_XMIT, "TX CABQ padding failed\n");
  1407. dev_kfree_skb_any(skb);
  1408. return;
  1409. }
  1410. skb_push(skb, padsize);
  1411. memmove(skb->data, skb->data + padsize, hdrlen);
  1412. }
  1413. txctl.txq = sc->beacon.cabq;
  1414. DPRINTF(sc, ATH_DBG_XMIT, "transmitting CABQ packet, skb: %p\n", skb);
  1415. if (ath_tx_start(sc, skb, &txctl) != 0) {
  1416. DPRINTF(sc, ATH_DBG_XMIT, "CABQ TX failed\n");
  1417. goto exit;
  1418. }
  1419. return;
  1420. exit:
  1421. dev_kfree_skb_any(skb);
  1422. }
  1423. /*****************/
  1424. /* TX Completion */
  1425. /*****************/
  1426. static void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
  1427. struct ath_xmit_status *tx_status)
  1428. {
  1429. struct ieee80211_hw *hw = sc->hw;
  1430. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1431. struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
  1432. int hdrlen, padsize;
  1433. DPRINTF(sc, ATH_DBG_XMIT, "TX complete: skb: %p\n", skb);
  1434. if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
  1435. tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  1436. kfree(tx_info_priv);
  1437. tx_info->rate_driver_data[0] = NULL;
  1438. }
  1439. if (tx_status->flags & ATH_TX_BAR) {
  1440. tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  1441. tx_status->flags &= ~ATH_TX_BAR;
  1442. }
  1443. if (!(tx_status->flags & (ATH_TX_ERROR | ATH_TX_XRETRY))) {
  1444. /* Frame was ACKed */
  1445. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  1446. }
  1447. tx_info->status.rates[0].count = tx_status->retries + 1;
  1448. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1449. padsize = hdrlen & 3;
  1450. if (padsize && hdrlen >= 24) {
  1451. /*
  1452. * Remove MAC header padding before giving the frame back to
  1453. * mac80211.
  1454. */
  1455. memmove(skb->data + padsize, skb->data, hdrlen);
  1456. skb_pull(skb, padsize);
  1457. }
  1458. ieee80211_tx_status(hw, skb);
  1459. }
  1460. static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
  1461. struct list_head *bf_q,
  1462. int txok, int sendbar)
  1463. {
  1464. struct sk_buff *skb = bf->bf_mpdu;
  1465. struct ath_xmit_status tx_status;
  1466. unsigned long flags;
  1467. /*
  1468. * Set retry information.
  1469. * NB: Don't use the information in the descriptor, because the frame
  1470. * could be software retried.
  1471. */
  1472. tx_status.retries = bf->bf_retries;
  1473. tx_status.flags = 0;
  1474. if (sendbar)
  1475. tx_status.flags = ATH_TX_BAR;
  1476. if (!txok) {
  1477. tx_status.flags |= ATH_TX_ERROR;
  1478. if (bf_isxretried(bf))
  1479. tx_status.flags |= ATH_TX_XRETRY;
  1480. }
  1481. dma_unmap_single(sc->dev, bf->bf_dmacontext, skb->len, DMA_TO_DEVICE);
  1482. ath_tx_complete(sc, skb, &tx_status);
  1483. /*
  1484. * Return the list of ath_buf of this mpdu to free queue
  1485. */
  1486. spin_lock_irqsave(&sc->tx.txbuflock, flags);
  1487. list_splice_tail_init(bf_q, &sc->tx.txbuf);
  1488. spin_unlock_irqrestore(&sc->tx.txbuflock, flags);
  1489. }
  1490. static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
  1491. int txok)
  1492. {
  1493. struct ath_buf *bf_last = bf->bf_lastbf;
  1494. struct ath_desc *ds = bf_last->bf_desc;
  1495. u16 seq_st = 0;
  1496. u32 ba[WME_BA_BMP_SIZE >> 5];
  1497. int ba_index;
  1498. int nbad = 0;
  1499. int isaggr = 0;
  1500. if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
  1501. return 0;
  1502. isaggr = bf_isaggr(bf);
  1503. if (isaggr) {
  1504. seq_st = ATH_DS_BA_SEQ(ds);
  1505. memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
  1506. }
  1507. while (bf) {
  1508. ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
  1509. if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
  1510. nbad++;
  1511. bf = bf->bf_next;
  1512. }
  1513. return nbad;
  1514. }
  1515. static void ath_tx_rc_status(struct ath_buf *bf, struct ath_desc *ds, int nbad)
  1516. {
  1517. struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
  1518. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1519. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1520. struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
  1521. tx_info_priv->update_rc = false;
  1522. if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
  1523. tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1524. if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
  1525. (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
  1526. if (ieee80211_is_data(hdr->frame_control)) {
  1527. memcpy(&tx_info_priv->tx, &ds->ds_txstat,
  1528. sizeof(tx_info_priv->tx));
  1529. tx_info_priv->n_frames = bf->bf_nframes;
  1530. tx_info_priv->n_bad_frames = nbad;
  1531. tx_info_priv->update_rc = true;
  1532. }
  1533. }
  1534. }
  1535. static void ath_wake_mac80211_queue(struct ath_softc *sc, struct ath_txq *txq)
  1536. {
  1537. int qnum;
  1538. spin_lock_bh(&txq->axq_lock);
  1539. if (txq->stopped &&
  1540. ath_txq_depth(sc, txq->axq_qnum) <= (ATH_TXBUF - 20)) {
  1541. qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
  1542. if (qnum != -1) {
  1543. ieee80211_wake_queue(sc->hw, qnum);
  1544. txq->stopped = 0;
  1545. }
  1546. }
  1547. spin_unlock_bh(&txq->axq_lock);
  1548. }
  1549. static void ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
  1550. {
  1551. struct ath_hal *ah = sc->sc_ah;
  1552. struct ath_buf *bf, *lastbf, *bf_held = NULL;
  1553. struct list_head bf_head;
  1554. struct ath_desc *ds;
  1555. int txok, nbad = 0;
  1556. int status;
  1557. DPRINTF(sc, ATH_DBG_QUEUE, "tx queue %d (%x), link %p\n",
  1558. txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
  1559. txq->axq_link);
  1560. for (;;) {
  1561. spin_lock_bh(&txq->axq_lock);
  1562. if (list_empty(&txq->axq_q)) {
  1563. txq->axq_link = NULL;
  1564. txq->axq_linkbuf = NULL;
  1565. spin_unlock_bh(&txq->axq_lock);
  1566. break;
  1567. }
  1568. bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
  1569. /*
  1570. * There is a race condition that a BH gets scheduled
  1571. * after sw writes TxE and before hw re-load the last
  1572. * descriptor to get the newly chained one.
  1573. * Software must keep the last DONE descriptor as a
  1574. * holding descriptor - software does so by marking
  1575. * it with the STALE flag.
  1576. */
  1577. bf_held = NULL;
  1578. if (bf->bf_status & ATH_BUFSTATUS_STALE) {
  1579. bf_held = bf;
  1580. if (list_is_last(&bf_held->list, &txq->axq_q)) {
  1581. txq->axq_link = NULL;
  1582. txq->axq_linkbuf = NULL;
  1583. spin_unlock_bh(&txq->axq_lock);
  1584. /*
  1585. * The holding descriptor is the last
  1586. * descriptor in queue. It's safe to remove
  1587. * the last holding descriptor in BH context.
  1588. */
  1589. spin_lock_bh(&sc->tx.txbuflock);
  1590. list_move_tail(&bf_held->list, &sc->tx.txbuf);
  1591. spin_unlock_bh(&sc->tx.txbuflock);
  1592. break;
  1593. } else {
  1594. bf = list_entry(bf_held->list.next,
  1595. struct ath_buf, list);
  1596. }
  1597. }
  1598. lastbf = bf->bf_lastbf;
  1599. ds = lastbf->bf_desc;
  1600. status = ath9k_hw_txprocdesc(ah, ds);
  1601. if (status == -EINPROGRESS) {
  1602. spin_unlock_bh(&txq->axq_lock);
  1603. break;
  1604. }
  1605. if (bf->bf_desc == txq->axq_lastdsWithCTS)
  1606. txq->axq_lastdsWithCTS = NULL;
  1607. if (ds == txq->axq_gatingds)
  1608. txq->axq_gatingds = NULL;
  1609. /*
  1610. * Remove ath_buf's of the same transmit unit from txq,
  1611. * however leave the last descriptor back as the holding
  1612. * descriptor for hw.
  1613. */
  1614. lastbf->bf_status |= ATH_BUFSTATUS_STALE;
  1615. INIT_LIST_HEAD(&bf_head);
  1616. if (!list_is_singular(&lastbf->list))
  1617. list_cut_position(&bf_head,
  1618. &txq->axq_q, lastbf->list.prev);
  1619. txq->axq_depth--;
  1620. if (bf_isaggr(bf))
  1621. txq->axq_aggr_depth--;
  1622. txok = (ds->ds_txstat.ts_status == 0);
  1623. spin_unlock_bh(&txq->axq_lock);
  1624. if (bf_held) {
  1625. spin_lock_bh(&sc->tx.txbuflock);
  1626. list_move_tail(&bf_held->list, &sc->tx.txbuf);
  1627. spin_unlock_bh(&sc->tx.txbuflock);
  1628. }
  1629. if (!bf_isampdu(bf)) {
  1630. /*
  1631. * This frame is sent out as a single frame.
  1632. * Use hardware retry status for this frame.
  1633. */
  1634. bf->bf_retries = ds->ds_txstat.ts_longretry;
  1635. if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
  1636. bf->bf_state.bf_type |= BUF_XRETRY;
  1637. nbad = 0;
  1638. } else {
  1639. nbad = ath_tx_num_badfrms(sc, bf, txok);
  1640. }
  1641. ath_tx_rc_status(bf, ds, nbad);
  1642. if (bf_isampdu(bf))
  1643. ath_tx_complete_aggr(sc, txq, bf, &bf_head, txok);
  1644. else
  1645. ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
  1646. ath_wake_mac80211_queue(sc, txq);
  1647. spin_lock_bh(&txq->axq_lock);
  1648. if (sc->sc_flags & SC_OP_TXAGGR)
  1649. ath_txq_schedule(sc, txq);
  1650. spin_unlock_bh(&txq->axq_lock);
  1651. }
  1652. }
  1653. void ath_tx_tasklet(struct ath_softc *sc)
  1654. {
  1655. int i;
  1656. u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
  1657. ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
  1658. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1659. if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
  1660. ath_tx_processq(sc, &sc->tx.txq[i]);
  1661. }
  1662. }
  1663. /*****************/
  1664. /* Init, Cleanup */
  1665. /*****************/
  1666. int ath_tx_init(struct ath_softc *sc, int nbufs)
  1667. {
  1668. int error = 0;
  1669. do {
  1670. spin_lock_init(&sc->tx.txbuflock);
  1671. error = ath_descdma_setup(sc, &sc->tx.txdma, &sc->tx.txbuf,
  1672. "tx", nbufs, 1);
  1673. if (error != 0) {
  1674. DPRINTF(sc, ATH_DBG_FATAL,
  1675. "Failed to allocate tx descriptors: %d\n",
  1676. error);
  1677. break;
  1678. }
  1679. error = ath_descdma_setup(sc, &sc->beacon.bdma, &sc->beacon.bbuf,
  1680. "beacon", ATH_BCBUF, 1);
  1681. if (error != 0) {
  1682. DPRINTF(sc, ATH_DBG_FATAL,
  1683. "Failed to allocate beacon descriptors: %d\n",
  1684. error);
  1685. break;
  1686. }
  1687. } while (0);
  1688. if (error != 0)
  1689. ath_tx_cleanup(sc);
  1690. return error;
  1691. }
  1692. int ath_tx_cleanup(struct ath_softc *sc)
  1693. {
  1694. if (sc->beacon.bdma.dd_desc_len != 0)
  1695. ath_descdma_cleanup(sc, &sc->beacon.bdma, &sc->beacon.bbuf);
  1696. if (sc->tx.txdma.dd_desc_len != 0)
  1697. ath_descdma_cleanup(sc, &sc->tx.txdma, &sc->tx.txbuf);
  1698. return 0;
  1699. }
  1700. void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
  1701. {
  1702. struct ath_atx_tid *tid;
  1703. struct ath_atx_ac *ac;
  1704. int tidno, acno;
  1705. for (tidno = 0, tid = &an->tid[tidno];
  1706. tidno < WME_NUM_TID;
  1707. tidno++, tid++) {
  1708. tid->an = an;
  1709. tid->tidno = tidno;
  1710. tid->seq_start = tid->seq_next = 0;
  1711. tid->baw_size = WME_MAX_BA;
  1712. tid->baw_head = tid->baw_tail = 0;
  1713. tid->sched = false;
  1714. tid->paused = false;
  1715. tid->state &= ~AGGR_CLEANUP;
  1716. INIT_LIST_HEAD(&tid->buf_q);
  1717. acno = TID_TO_WME_AC(tidno);
  1718. tid->ac = &an->ac[acno];
  1719. tid->state &= ~AGGR_ADDBA_COMPLETE;
  1720. tid->state &= ~AGGR_ADDBA_PROGRESS;
  1721. tid->addba_exchangeattempts = 0;
  1722. }
  1723. for (acno = 0, ac = &an->ac[acno];
  1724. acno < WME_NUM_AC; acno++, ac++) {
  1725. ac->sched = false;
  1726. INIT_LIST_HEAD(&ac->tid_q);
  1727. switch (acno) {
  1728. case WME_AC_BE:
  1729. ac->qnum = ath_tx_get_qnum(sc,
  1730. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
  1731. break;
  1732. case WME_AC_BK:
  1733. ac->qnum = ath_tx_get_qnum(sc,
  1734. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
  1735. break;
  1736. case WME_AC_VI:
  1737. ac->qnum = ath_tx_get_qnum(sc,
  1738. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
  1739. break;
  1740. case WME_AC_VO:
  1741. ac->qnum = ath_tx_get_qnum(sc,
  1742. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
  1743. break;
  1744. }
  1745. }
  1746. }
  1747. void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
  1748. {
  1749. int i;
  1750. struct ath_atx_ac *ac, *ac_tmp;
  1751. struct ath_atx_tid *tid, *tid_tmp;
  1752. struct ath_txq *txq;
  1753. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1754. if (ATH_TXQ_SETUP(sc, i)) {
  1755. txq = &sc->tx.txq[i];
  1756. spin_lock(&txq->axq_lock);
  1757. list_for_each_entry_safe(ac,
  1758. ac_tmp, &txq->axq_acq, list) {
  1759. tid = list_first_entry(&ac->tid_q,
  1760. struct ath_atx_tid, list);
  1761. if (tid && tid->an != an)
  1762. continue;
  1763. list_del(&ac->list);
  1764. ac->sched = false;
  1765. list_for_each_entry_safe(tid,
  1766. tid_tmp, &ac->tid_q, list) {
  1767. list_del(&tid->list);
  1768. tid->sched = false;
  1769. ath_tid_drain(sc, txq, tid);
  1770. tid->state &= ~AGGR_ADDBA_COMPLETE;
  1771. tid->addba_exchangeattempts = 0;
  1772. tid->state &= ~AGGR_CLEANUP;
  1773. }
  1774. }
  1775. spin_unlock(&txq->axq_lock);
  1776. }
  1777. }
  1778. }