xmit.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  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. /*
  53. * Insert a chain of ath_buf (descriptors) on a txq and
  54. * assume the descriptors are already chained together by caller.
  55. * NB: must be called with txq lock held
  56. */
  57. static void ath_tx_txqaddbuf(struct ath_softc *sc, struct ath_txq *txq,
  58. struct list_head *head)
  59. {
  60. struct ath_hal *ah = sc->sc_ah;
  61. struct ath_buf *bf;
  62. /*
  63. * Insert the frame on the outbound list and
  64. * pass it on to the hardware.
  65. */
  66. if (list_empty(head))
  67. return;
  68. bf = list_first_entry(head, struct ath_buf, list);
  69. list_splice_tail_init(head, &txq->axq_q);
  70. txq->axq_depth++;
  71. txq->axq_totalqueued++;
  72. txq->axq_linkbuf = list_entry(txq->axq_q.prev, struct ath_buf, list);
  73. DPRINTF(sc, ATH_DBG_QUEUE,
  74. "%s: txq depth = %d\n", __func__, txq->axq_depth);
  75. if (txq->axq_link == NULL) {
  76. ath9k_hw_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
  77. DPRINTF(sc, ATH_DBG_XMIT,
  78. "%s: TXDP[%u] = %llx (%p)\n",
  79. __func__, txq->axq_qnum,
  80. ito64(bf->bf_daddr), bf->bf_desc);
  81. } else {
  82. *txq->axq_link = bf->bf_daddr;
  83. DPRINTF(sc, ATH_DBG_XMIT, "%s: link[%u] (%p)=%llx (%p)\n",
  84. __func__,
  85. txq->axq_qnum, txq->axq_link,
  86. ito64(bf->bf_daddr), bf->bf_desc);
  87. }
  88. txq->axq_link = &(bf->bf_lastbf->bf_desc->ds_link);
  89. ath9k_hw_txstart(ah, txq->axq_qnum);
  90. }
  91. /* Check if it's okay to send out aggregates */
  92. static int ath_aggr_query(struct ath_softc *sc, struct ath_node *an, u8 tidno)
  93. {
  94. struct ath_atx_tid *tid;
  95. tid = ATH_AN_2_TID(an, tidno);
  96. if (tid->state & AGGR_ADDBA_COMPLETE ||
  97. tid->state & AGGR_ADDBA_PROGRESS)
  98. return 1;
  99. else
  100. return 0;
  101. }
  102. /* Calculate Atheros packet type from IEEE80211 packet header */
  103. static enum ath9k_pkt_type get_hw_packet_type(struct sk_buff *skb)
  104. {
  105. struct ieee80211_hdr *hdr;
  106. enum ath9k_pkt_type htype;
  107. __le16 fc;
  108. hdr = (struct ieee80211_hdr *)skb->data;
  109. fc = hdr->frame_control;
  110. if (ieee80211_is_beacon(fc))
  111. htype = ATH9K_PKT_TYPE_BEACON;
  112. else if (ieee80211_is_probe_resp(fc))
  113. htype = ATH9K_PKT_TYPE_PROBE_RESP;
  114. else if (ieee80211_is_atim(fc))
  115. htype = ATH9K_PKT_TYPE_ATIM;
  116. else if (ieee80211_is_pspoll(fc))
  117. htype = ATH9K_PKT_TYPE_PSPOLL;
  118. else
  119. htype = ATH9K_PKT_TYPE_NORMAL;
  120. return htype;
  121. }
  122. static bool is_pae(struct sk_buff *skb)
  123. {
  124. struct ieee80211_hdr *hdr;
  125. __le16 fc;
  126. hdr = (struct ieee80211_hdr *)skb->data;
  127. fc = hdr->frame_control;
  128. if (ieee80211_is_data(fc)) {
  129. if (ieee80211_is_nullfunc(fc) ||
  130. /* Port Access Entity (IEEE 802.1X) */
  131. (skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. static int get_hw_crypto_keytype(struct sk_buff *skb)
  138. {
  139. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  140. if (tx_info->control.hw_key) {
  141. if (tx_info->control.hw_key->alg == ALG_WEP)
  142. return ATH9K_KEY_TYPE_WEP;
  143. else if (tx_info->control.hw_key->alg == ALG_TKIP)
  144. return ATH9K_KEY_TYPE_TKIP;
  145. else if (tx_info->control.hw_key->alg == ALG_CCMP)
  146. return ATH9K_KEY_TYPE_AES;
  147. }
  148. return ATH9K_KEY_TYPE_CLEAR;
  149. }
  150. static void setup_rate_retries(struct ath_softc *sc, struct sk_buff *skb)
  151. {
  152. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  153. struct ieee80211_tx_rate *rates = tx_info->control.rates;
  154. struct ieee80211_hdr *hdr;
  155. __le16 fc;
  156. hdr = (struct ieee80211_hdr *)skb->data;
  157. fc = hdr->frame_control;
  158. if (ieee80211_has_morefrags(fc) ||
  159. (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)) {
  160. rates[1].count = rates[2].count = rates[3].count = 0;
  161. rates[1].idx = rates[2].idx = rates[3].idx = 0;
  162. /* reset tries but keep rate index */
  163. rates[0].count = ATH_TXMAXTRY;
  164. }
  165. }
  166. /* Called only when tx aggregation is enabled and HT is supported */
  167. static void assign_aggr_tid_seqno(struct sk_buff *skb,
  168. struct ath_buf *bf)
  169. {
  170. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  171. struct ieee80211_hdr *hdr;
  172. struct ath_node *an;
  173. struct ath_atx_tid *tid;
  174. __le16 fc;
  175. u8 *qc;
  176. if (!tx_info->control.sta)
  177. return;
  178. an = (struct ath_node *)tx_info->control.sta->drv_priv;
  179. hdr = (struct ieee80211_hdr *)skb->data;
  180. fc = hdr->frame_control;
  181. /* Get tidno */
  182. if (ieee80211_is_data_qos(fc)) {
  183. qc = ieee80211_get_qos_ctl(hdr);
  184. bf->bf_tidno = qc[0] & 0xf;
  185. }
  186. /* Get seqno */
  187. if (ieee80211_is_data(fc) && !is_pae(skb)) {
  188. /* For HT capable stations, we save tidno for later use.
  189. * We also override seqno set by upper layer with the one
  190. * in tx aggregation state.
  191. *
  192. * If fragmentation is on, the sequence number is
  193. * not overridden, since it has been
  194. * incremented by the fragmentation routine.
  195. *
  196. * FIXME: check if the fragmentation threshold exceeds
  197. * IEEE80211 max.
  198. */
  199. tid = ATH_AN_2_TID(an, bf->bf_tidno);
  200. hdr->seq_ctrl = cpu_to_le16(tid->seq_next <<
  201. IEEE80211_SEQ_SEQ_SHIFT);
  202. bf->bf_seqno = tid->seq_next;
  203. INCR(tid->seq_next, IEEE80211_SEQ_MAX);
  204. }
  205. }
  206. static int setup_tx_flags(struct ath_softc *sc, struct sk_buff *skb,
  207. struct ath_txq *txq)
  208. {
  209. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  210. int flags = 0;
  211. flags |= ATH9K_TXDESC_CLRDMASK; /* needed for crypto errors */
  212. flags |= ATH9K_TXDESC_INTREQ;
  213. if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
  214. flags |= ATH9K_TXDESC_NOACK;
  215. if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  216. flags |= ATH9K_TXDESC_RTSENA;
  217. return flags;
  218. }
  219. static struct ath_buf *ath_tx_get_buffer(struct ath_softc *sc)
  220. {
  221. struct ath_buf *bf = NULL;
  222. spin_lock_bh(&sc->sc_txbuflock);
  223. if (unlikely(list_empty(&sc->sc_txbuf))) {
  224. spin_unlock_bh(&sc->sc_txbuflock);
  225. return NULL;
  226. }
  227. bf = list_first_entry(&sc->sc_txbuf, struct ath_buf, list);
  228. list_del(&bf->list);
  229. spin_unlock_bh(&sc->sc_txbuflock);
  230. return bf;
  231. }
  232. /* To complete a chain of buffers associated a frame */
  233. static void ath_tx_complete_buf(struct ath_softc *sc,
  234. struct ath_buf *bf,
  235. struct list_head *bf_q,
  236. int txok, int sendbar)
  237. {
  238. struct sk_buff *skb = bf->bf_mpdu;
  239. struct ath_xmit_status tx_status;
  240. /*
  241. * Set retry information.
  242. * NB: Don't use the information in the descriptor, because the frame
  243. * could be software retried.
  244. */
  245. tx_status.retries = bf->bf_retries;
  246. tx_status.flags = 0;
  247. if (sendbar)
  248. tx_status.flags = ATH_TX_BAR;
  249. if (!txok) {
  250. tx_status.flags |= ATH_TX_ERROR;
  251. if (bf_isxretried(bf))
  252. tx_status.flags |= ATH_TX_XRETRY;
  253. }
  254. /* Unmap this frame */
  255. pci_unmap_single(sc->pdev,
  256. bf->bf_dmacontext,
  257. skb->len,
  258. PCI_DMA_TODEVICE);
  259. /* complete this frame */
  260. ath_tx_complete(sc, skb, &tx_status);
  261. /*
  262. * Return the list of ath_buf of this mpdu to free queue
  263. */
  264. spin_lock_bh(&sc->sc_txbuflock);
  265. list_splice_tail_init(bf_q, &sc->sc_txbuf);
  266. spin_unlock_bh(&sc->sc_txbuflock);
  267. }
  268. /*
  269. * queue up a dest/ac pair for tx scheduling
  270. * NB: must be called with txq lock held
  271. */
  272. static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
  273. {
  274. struct ath_atx_ac *ac = tid->ac;
  275. /*
  276. * if tid is paused, hold off
  277. */
  278. if (tid->paused)
  279. return;
  280. /*
  281. * add tid to ac atmost once
  282. */
  283. if (tid->sched)
  284. return;
  285. tid->sched = true;
  286. list_add_tail(&tid->list, &ac->tid_q);
  287. /*
  288. * add node ac to txq atmost once
  289. */
  290. if (ac->sched)
  291. return;
  292. ac->sched = true;
  293. list_add_tail(&ac->list, &txq->axq_acq);
  294. }
  295. /* pause a tid */
  296. static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
  297. {
  298. struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
  299. spin_lock_bh(&txq->axq_lock);
  300. tid->paused++;
  301. spin_unlock_bh(&txq->axq_lock);
  302. }
  303. /* resume a tid and schedule aggregate */
  304. void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
  305. {
  306. struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
  307. ASSERT(tid->paused > 0);
  308. spin_lock_bh(&txq->axq_lock);
  309. tid->paused--;
  310. if (tid->paused > 0)
  311. goto unlock;
  312. if (list_empty(&tid->buf_q))
  313. goto unlock;
  314. /*
  315. * Add this TID to scheduler and try to send out aggregates
  316. */
  317. ath_tx_queue_tid(txq, tid);
  318. ath_txq_schedule(sc, txq);
  319. unlock:
  320. spin_unlock_bh(&txq->axq_lock);
  321. }
  322. /* Compute the number of bad frames */
  323. static int ath_tx_num_badfrms(struct ath_softc *sc, struct ath_buf *bf,
  324. int txok)
  325. {
  326. struct ath_buf *bf_last = bf->bf_lastbf;
  327. struct ath_desc *ds = bf_last->bf_desc;
  328. u16 seq_st = 0;
  329. u32 ba[WME_BA_BMP_SIZE >> 5];
  330. int ba_index;
  331. int nbad = 0;
  332. int isaggr = 0;
  333. if (ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
  334. return 0;
  335. isaggr = bf_isaggr(bf);
  336. if (isaggr) {
  337. seq_st = ATH_DS_BA_SEQ(ds);
  338. memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
  339. }
  340. while (bf) {
  341. ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
  342. if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
  343. nbad++;
  344. bf = bf->bf_next;
  345. }
  346. return nbad;
  347. }
  348. static void ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
  349. {
  350. struct sk_buff *skb;
  351. struct ieee80211_hdr *hdr;
  352. bf->bf_state.bf_type |= BUF_RETRY;
  353. bf->bf_retries++;
  354. skb = bf->bf_mpdu;
  355. hdr = (struct ieee80211_hdr *)skb->data;
  356. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
  357. }
  358. /* Update block ack window */
  359. static void ath_tx_update_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
  360. int seqno)
  361. {
  362. int index, cindex;
  363. index = ATH_BA_INDEX(tid->seq_start, seqno);
  364. cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
  365. tid->tx_buf[cindex] = NULL;
  366. while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
  367. INCR(tid->seq_start, IEEE80211_SEQ_MAX);
  368. INCR(tid->baw_head, ATH_TID_MAX_BUFS);
  369. }
  370. }
  371. /*
  372. * ath_pkt_dur - compute packet duration (NB: not NAV)
  373. *
  374. * rix - rate index
  375. * pktlen - total bytes (delims + data + fcs + pads + pad delims)
  376. * width - 0 for 20 MHz, 1 for 40 MHz
  377. * half_gi - to use 4us v/s 3.6 us for symbol time
  378. */
  379. static u32 ath_pkt_duration(struct ath_softc *sc, u8 rix, struct ath_buf *bf,
  380. int width, int half_gi, bool shortPreamble)
  381. {
  382. const struct ath9k_rate_table *rt = sc->sc_currates;
  383. u32 nbits, nsymbits, duration, nsymbols;
  384. u8 rc;
  385. int streams, pktlen;
  386. pktlen = bf_isaggr(bf) ? bf->bf_al : bf->bf_frmlen;
  387. rc = rt->info[rix].rateCode;
  388. /*
  389. * for legacy rates, use old function to compute packet duration
  390. */
  391. if (!IS_HT_RATE(rc))
  392. return ath9k_hw_computetxtime(sc->sc_ah, rt, pktlen, rix,
  393. shortPreamble);
  394. /*
  395. * find number of symbols: PLCP + data
  396. */
  397. nbits = (pktlen << 3) + OFDM_PLCP_BITS;
  398. nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
  399. nsymbols = (nbits + nsymbits - 1) / nsymbits;
  400. if (!half_gi)
  401. duration = SYMBOL_TIME(nsymbols);
  402. else
  403. duration = SYMBOL_TIME_HALFGI(nsymbols);
  404. /*
  405. * addup duration for legacy/ht training and signal fields
  406. */
  407. streams = HT_RC_2_STREAMS(rc);
  408. duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
  409. return duration;
  410. }
  411. /* Rate module function to set rate related fields in tx descriptor */
  412. static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
  413. {
  414. struct ath_hal *ah = sc->sc_ah;
  415. const struct ath9k_rate_table *rt;
  416. struct ath_desc *ds = bf->bf_desc;
  417. struct ath_desc *lastds = bf->bf_lastbf->bf_desc;
  418. struct ath9k_11n_rate_series series[4];
  419. int i, flags, rtsctsena = 0;
  420. u32 ctsduration = 0;
  421. u8 rix = 0, cix, ctsrate = 0;
  422. struct ath_node *an = NULL;
  423. struct sk_buff *skb;
  424. struct ieee80211_tx_info *tx_info;
  425. struct ieee80211_tx_rate *rates;
  426. skb = (struct sk_buff *)bf->bf_mpdu;
  427. tx_info = IEEE80211_SKB_CB(skb);
  428. rates = tx_info->rate_driver_data[0];
  429. if (tx_info->control.sta)
  430. an = (struct ath_node *)tx_info->control.sta->drv_priv;
  431. /*
  432. * get the cix for the lowest valid rix.
  433. */
  434. rt = sc->sc_currates;
  435. for (i = 3; i >= 0; i--) {
  436. if (rates[i].count) {
  437. rix = rates[i].idx;
  438. break;
  439. }
  440. }
  441. flags = (bf->bf_flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA));
  442. cix = rt->info[rix].controlRate;
  443. /*
  444. * If 802.11g protection is enabled, determine whether
  445. * to use RTS/CTS or just CTS. Note that this is only
  446. * done for OFDM/HT unicast frames.
  447. */
  448. if (sc->sc_protmode != PROT_M_NONE &&
  449. (rt->info[rix].phy == PHY_OFDM ||
  450. rt->info[rix].phy == PHY_HT) &&
  451. (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
  452. if (sc->sc_protmode == PROT_M_RTSCTS)
  453. flags = ATH9K_TXDESC_RTSENA;
  454. else if (sc->sc_protmode == PROT_M_CTSONLY)
  455. flags = ATH9K_TXDESC_CTSENA;
  456. cix = rt->info[sc->sc_protrix].controlRate;
  457. rtsctsena = 1;
  458. }
  459. /* For 11n, the default behavior is to enable RTS for
  460. * hw retried frames. We enable the global flag here and
  461. * let rate series flags determine which rates will actually
  462. * use RTS.
  463. */
  464. if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) && bf_isdata(bf)) {
  465. /*
  466. * 802.11g protection not needed, use our default behavior
  467. */
  468. if (!rtsctsena)
  469. flags = ATH9K_TXDESC_RTSENA;
  470. }
  471. /*
  472. * Set protection if aggregate protection on
  473. */
  474. if (sc->sc_config.ath_aggr_prot &&
  475. (!bf_isaggr(bf) || (bf_isaggr(bf) && bf->bf_al < 8192))) {
  476. flags = ATH9K_TXDESC_RTSENA;
  477. cix = rt->info[sc->sc_protrix].controlRate;
  478. rtsctsena = 1;
  479. }
  480. /*
  481. * For AR5416 - RTS cannot be followed by a frame larger than 8K.
  482. */
  483. if (bf_isaggr(bf) && (bf->bf_al > ah->ah_caps.rts_aggr_limit)) {
  484. /*
  485. * Ensure that in the case of SM Dynamic power save
  486. * while we are bursting the second aggregate the
  487. * RTS is cleared.
  488. */
  489. flags &= ~(ATH9K_TXDESC_RTSENA);
  490. }
  491. /*
  492. * CTS transmit rate is derived from the transmit rate
  493. * by looking in the h/w rate table. We must also factor
  494. * in whether or not a short preamble is to be used.
  495. * NB: cix is set above where RTS/CTS is enabled
  496. */
  497. BUG_ON(cix == 0xff);
  498. ctsrate = rt->info[cix].rateCode |
  499. (bf_isshpreamble(bf) ? rt->info[cix].shortPreamble : 0);
  500. /*
  501. * Setup HAL rate series
  502. */
  503. memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
  504. for (i = 0; i < 4; i++) {
  505. if (!rates[i].count)
  506. continue;
  507. rix = rates[i].idx;
  508. series[i].Rate = rt->info[rix].rateCode |
  509. (bf_isshpreamble(bf) ? rt->info[rix].shortPreamble : 0);
  510. series[i].Tries = rates[i].count;
  511. series[i].RateFlags = (
  512. (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) ?
  513. ATH9K_RATESERIES_RTS_CTS : 0) |
  514. ((rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ?
  515. ATH9K_RATESERIES_2040 : 0) |
  516. ((rates[i].flags & IEEE80211_TX_RC_SHORT_GI) ?
  517. ATH9K_RATESERIES_HALFGI : 0);
  518. series[i].PktDuration = ath_pkt_duration(sc, rix, bf,
  519. (rates[i].flags & IEEE80211_TX_RC_40_MHZ_WIDTH) != 0,
  520. (rates[i].flags & IEEE80211_TX_RC_SHORT_GI),
  521. bf_isshpreamble(bf));
  522. if (bf_isht(bf) && an)
  523. series[i].ChSel = ath_chainmask_sel_logic(sc, an);
  524. else
  525. series[i].ChSel = sc->sc_tx_chainmask;
  526. if (rtsctsena)
  527. series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
  528. }
  529. /*
  530. * set dur_update_en for l-sig computation except for PS-Poll frames
  531. */
  532. ath9k_hw_set11n_ratescenario(ah, ds, lastds,
  533. !bf_ispspoll(bf),
  534. ctsrate,
  535. ctsduration,
  536. series, 4, flags);
  537. if (sc->sc_config.ath_aggr_prot && flags)
  538. ath9k_hw_set11n_burstduration(ah, ds, 8192);
  539. }
  540. /*
  541. * Function to send a normal HT (non-AMPDU) frame
  542. * NB: must be called with txq lock held
  543. */
  544. static int ath_tx_send_normal(struct ath_softc *sc,
  545. struct ath_txq *txq,
  546. struct ath_atx_tid *tid,
  547. struct list_head *bf_head)
  548. {
  549. struct ath_buf *bf;
  550. BUG_ON(list_empty(bf_head));
  551. bf = list_first_entry(bf_head, struct ath_buf, list);
  552. bf->bf_state.bf_type &= ~BUF_AMPDU; /* regular HT frame */
  553. /* update starting sequence number for subsequent ADDBA request */
  554. INCR(tid->seq_start, IEEE80211_SEQ_MAX);
  555. /* Queue to h/w without aggregation */
  556. bf->bf_nframes = 1;
  557. bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
  558. ath_buf_set_rate(sc, bf);
  559. ath_tx_txqaddbuf(sc, txq, bf_head);
  560. return 0;
  561. }
  562. /* flush tid's software queue and send frames as non-ampdu's */
  563. static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
  564. {
  565. struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
  566. struct ath_buf *bf;
  567. struct list_head bf_head;
  568. INIT_LIST_HEAD(&bf_head);
  569. ASSERT(tid->paused > 0);
  570. spin_lock_bh(&txq->axq_lock);
  571. tid->paused--;
  572. if (tid->paused > 0) {
  573. spin_unlock_bh(&txq->axq_lock);
  574. return;
  575. }
  576. while (!list_empty(&tid->buf_q)) {
  577. bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
  578. ASSERT(!bf_isretried(bf));
  579. list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
  580. ath_tx_send_normal(sc, txq, tid, &bf_head);
  581. }
  582. spin_unlock_bh(&txq->axq_lock);
  583. }
  584. /* Completion routine of an aggregate */
  585. static void ath_tx_complete_aggr_rifs(struct ath_softc *sc,
  586. struct ath_txq *txq,
  587. struct ath_buf *bf,
  588. struct list_head *bf_q,
  589. int txok)
  590. {
  591. struct ath_node *an = NULL;
  592. struct sk_buff *skb;
  593. struct ieee80211_tx_info *tx_info;
  594. struct ath_atx_tid *tid = NULL;
  595. struct ath_buf *bf_last = bf->bf_lastbf;
  596. struct ath_desc *ds = bf_last->bf_desc;
  597. struct ath_buf *bf_next, *bf_lastq = NULL;
  598. struct list_head bf_head, bf_pending;
  599. u16 seq_st = 0;
  600. u32 ba[WME_BA_BMP_SIZE >> 5];
  601. int isaggr, txfail, txpending, sendbar = 0, needreset = 0;
  602. skb = (struct sk_buff *)bf->bf_mpdu;
  603. tx_info = IEEE80211_SKB_CB(skb);
  604. if (tx_info->control.sta) {
  605. an = (struct ath_node *)tx_info->control.sta->drv_priv;
  606. tid = ATH_AN_2_TID(an, bf->bf_tidno);
  607. }
  608. isaggr = bf_isaggr(bf);
  609. if (isaggr) {
  610. if (txok) {
  611. if (ATH_DS_TX_BA(ds)) {
  612. /*
  613. * extract starting sequence and
  614. * block-ack bitmap
  615. */
  616. seq_st = ATH_DS_BA_SEQ(ds);
  617. memcpy(ba,
  618. ATH_DS_BA_BITMAP(ds),
  619. WME_BA_BMP_SIZE >> 3);
  620. } else {
  621. memset(ba, 0, WME_BA_BMP_SIZE >> 3);
  622. /*
  623. * AR5416 can become deaf/mute when BA
  624. * issue happens. Chip needs to be reset.
  625. * But AP code may have sychronization issues
  626. * when perform internal reset in this routine.
  627. * Only enable reset in STA mode for now.
  628. */
  629. if (sc->sc_ah->ah_opmode == ATH9K_M_STA)
  630. needreset = 1;
  631. }
  632. } else {
  633. memset(ba, 0, WME_BA_BMP_SIZE >> 3);
  634. }
  635. }
  636. INIT_LIST_HEAD(&bf_pending);
  637. INIT_LIST_HEAD(&bf_head);
  638. while (bf) {
  639. txfail = txpending = 0;
  640. bf_next = bf->bf_next;
  641. if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
  642. /* transmit completion, subframe is
  643. * acked by block ack */
  644. } else if (!isaggr && txok) {
  645. /* transmit completion */
  646. } else {
  647. if (!(tid->state & AGGR_CLEANUP) &&
  648. ds->ds_txstat.ts_flags != ATH9K_TX_SW_ABORTED) {
  649. if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
  650. ath_tx_set_retry(sc, bf);
  651. txpending = 1;
  652. } else {
  653. bf->bf_state.bf_type |= BUF_XRETRY;
  654. txfail = 1;
  655. sendbar = 1;
  656. }
  657. } else {
  658. /*
  659. * cleanup in progress, just fail
  660. * the un-acked sub-frames
  661. */
  662. txfail = 1;
  663. }
  664. }
  665. /*
  666. * Remove ath_buf's of this sub-frame from aggregate queue.
  667. */
  668. if (bf_next == NULL) { /* last subframe in the aggregate */
  669. ASSERT(bf->bf_lastfrm == bf_last);
  670. /*
  671. * The last descriptor of the last sub frame could be
  672. * a holding descriptor for h/w. If that's the case,
  673. * bf->bf_lastfrm won't be in the bf_q.
  674. * Make sure we handle bf_q properly here.
  675. */
  676. if (!list_empty(bf_q)) {
  677. bf_lastq = list_entry(bf_q->prev,
  678. struct ath_buf, list);
  679. list_cut_position(&bf_head,
  680. bf_q, &bf_lastq->list);
  681. } else {
  682. /*
  683. * XXX: if the last subframe only has one
  684. * descriptor which is also being used as
  685. * a holding descriptor. Then the ath_buf
  686. * is not in the bf_q at all.
  687. */
  688. INIT_LIST_HEAD(&bf_head);
  689. }
  690. } else {
  691. ASSERT(!list_empty(bf_q));
  692. list_cut_position(&bf_head,
  693. bf_q, &bf->bf_lastfrm->list);
  694. }
  695. if (!txpending) {
  696. /*
  697. * complete the acked-ones/xretried ones; update
  698. * block-ack window
  699. */
  700. spin_lock_bh(&txq->axq_lock);
  701. ath_tx_update_baw(sc, tid, bf->bf_seqno);
  702. spin_unlock_bh(&txq->axq_lock);
  703. /* complete this sub-frame */
  704. ath_tx_complete_buf(sc, bf, &bf_head, !txfail, sendbar);
  705. } else {
  706. /*
  707. * retry the un-acked ones
  708. */
  709. /*
  710. * XXX: if the last descriptor is holding descriptor,
  711. * in order to requeue the frame to software queue, we
  712. * need to allocate a new descriptor and
  713. * copy the content of holding descriptor to it.
  714. */
  715. if (bf->bf_next == NULL &&
  716. bf_last->bf_status & ATH_BUFSTATUS_STALE) {
  717. struct ath_buf *tbf;
  718. /* allocate new descriptor */
  719. spin_lock_bh(&sc->sc_txbuflock);
  720. ASSERT(!list_empty((&sc->sc_txbuf)));
  721. tbf = list_first_entry(&sc->sc_txbuf,
  722. struct ath_buf, list);
  723. list_del(&tbf->list);
  724. spin_unlock_bh(&sc->sc_txbuflock);
  725. ATH_TXBUF_RESET(tbf);
  726. /* copy descriptor content */
  727. tbf->bf_mpdu = bf_last->bf_mpdu;
  728. tbf->bf_buf_addr = bf_last->bf_buf_addr;
  729. *(tbf->bf_desc) = *(bf_last->bf_desc);
  730. /* link it to the frame */
  731. if (bf_lastq) {
  732. bf_lastq->bf_desc->ds_link =
  733. tbf->bf_daddr;
  734. bf->bf_lastfrm = tbf;
  735. ath9k_hw_cleartxdesc(sc->sc_ah,
  736. bf->bf_lastfrm->bf_desc);
  737. } else {
  738. tbf->bf_state = bf_last->bf_state;
  739. tbf->bf_lastfrm = tbf;
  740. ath9k_hw_cleartxdesc(sc->sc_ah,
  741. tbf->bf_lastfrm->bf_desc);
  742. /* copy the DMA context */
  743. tbf->bf_dmacontext =
  744. bf_last->bf_dmacontext;
  745. }
  746. list_add_tail(&tbf->list, &bf_head);
  747. } else {
  748. /*
  749. * Clear descriptor status words for
  750. * software retry
  751. */
  752. ath9k_hw_cleartxdesc(sc->sc_ah,
  753. bf->bf_lastfrm->bf_desc);
  754. }
  755. /*
  756. * Put this buffer to the temporary pending
  757. * queue to retain ordering
  758. */
  759. list_splice_tail_init(&bf_head, &bf_pending);
  760. }
  761. bf = bf_next;
  762. }
  763. if (tid->state & AGGR_CLEANUP) {
  764. /* check to see if we're done with cleaning the h/w queue */
  765. spin_lock_bh(&txq->axq_lock);
  766. if (tid->baw_head == tid->baw_tail) {
  767. tid->state &= ~AGGR_ADDBA_COMPLETE;
  768. tid->addba_exchangeattempts = 0;
  769. spin_unlock_bh(&txq->axq_lock);
  770. tid->state &= ~AGGR_CLEANUP;
  771. /* send buffered frames as singles */
  772. ath_tx_flush_tid(sc, tid);
  773. } else
  774. spin_unlock_bh(&txq->axq_lock);
  775. return;
  776. }
  777. /*
  778. * prepend un-acked frames to the beginning of the pending frame queue
  779. */
  780. if (!list_empty(&bf_pending)) {
  781. spin_lock_bh(&txq->axq_lock);
  782. /* Note: we _prepend_, we _do_not_ at to
  783. * the end of the queue ! */
  784. list_splice(&bf_pending, &tid->buf_q);
  785. ath_tx_queue_tid(txq, tid);
  786. spin_unlock_bh(&txq->axq_lock);
  787. }
  788. if (needreset)
  789. ath_reset(sc, false);
  790. return;
  791. }
  792. /* Process completed xmit descriptors from the specified queue */
  793. static int ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
  794. {
  795. struct ath_hal *ah = sc->sc_ah;
  796. struct ath_buf *bf, *lastbf, *bf_held = NULL;
  797. struct list_head bf_head;
  798. struct ath_desc *ds, *tmp_ds;
  799. struct sk_buff *skb;
  800. struct ieee80211_tx_info *tx_info;
  801. struct ath_tx_info_priv *tx_info_priv;
  802. int nacked, txok, nbad = 0, isrifs = 0;
  803. int status;
  804. DPRINTF(sc, ATH_DBG_QUEUE,
  805. "%s: tx queue %d (%x), link %p\n", __func__,
  806. txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
  807. txq->axq_link);
  808. nacked = 0;
  809. for (;;) {
  810. spin_lock_bh(&txq->axq_lock);
  811. if (list_empty(&txq->axq_q)) {
  812. txq->axq_link = NULL;
  813. txq->axq_linkbuf = NULL;
  814. spin_unlock_bh(&txq->axq_lock);
  815. break;
  816. }
  817. bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
  818. /*
  819. * There is a race condition that a BH gets scheduled
  820. * after sw writes TxE and before hw re-load the last
  821. * descriptor to get the newly chained one.
  822. * Software must keep the last DONE descriptor as a
  823. * holding descriptor - software does so by marking
  824. * it with the STALE flag.
  825. */
  826. bf_held = NULL;
  827. if (bf->bf_status & ATH_BUFSTATUS_STALE) {
  828. bf_held = bf;
  829. if (list_is_last(&bf_held->list, &txq->axq_q)) {
  830. /* FIXME:
  831. * The holding descriptor is the last
  832. * descriptor in queue. It's safe to remove
  833. * the last holding descriptor in BH context.
  834. */
  835. spin_unlock_bh(&txq->axq_lock);
  836. break;
  837. } else {
  838. /* Lets work with the next buffer now */
  839. bf = list_entry(bf_held->list.next,
  840. struct ath_buf, list);
  841. }
  842. }
  843. lastbf = bf->bf_lastbf;
  844. ds = lastbf->bf_desc; /* NB: last decriptor */
  845. status = ath9k_hw_txprocdesc(ah, ds);
  846. if (status == -EINPROGRESS) {
  847. spin_unlock_bh(&txq->axq_lock);
  848. break;
  849. }
  850. if (bf->bf_desc == txq->axq_lastdsWithCTS)
  851. txq->axq_lastdsWithCTS = NULL;
  852. if (ds == txq->axq_gatingds)
  853. txq->axq_gatingds = NULL;
  854. /*
  855. * Remove ath_buf's of the same transmit unit from txq,
  856. * however leave the last descriptor back as the holding
  857. * descriptor for hw.
  858. */
  859. lastbf->bf_status |= ATH_BUFSTATUS_STALE;
  860. INIT_LIST_HEAD(&bf_head);
  861. if (!list_is_singular(&lastbf->list))
  862. list_cut_position(&bf_head,
  863. &txq->axq_q, lastbf->list.prev);
  864. txq->axq_depth--;
  865. if (bf_isaggr(bf))
  866. txq->axq_aggr_depth--;
  867. txok = (ds->ds_txstat.ts_status == 0);
  868. spin_unlock_bh(&txq->axq_lock);
  869. if (bf_held) {
  870. list_del(&bf_held->list);
  871. spin_lock_bh(&sc->sc_txbuflock);
  872. list_add_tail(&bf_held->list, &sc->sc_txbuf);
  873. spin_unlock_bh(&sc->sc_txbuflock);
  874. }
  875. if (!bf_isampdu(bf)) {
  876. /*
  877. * This frame is sent out as a single frame.
  878. * Use hardware retry status for this frame.
  879. */
  880. bf->bf_retries = ds->ds_txstat.ts_longretry;
  881. if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
  882. bf->bf_state.bf_type |= BUF_XRETRY;
  883. nbad = 0;
  884. } else {
  885. nbad = ath_tx_num_badfrms(sc, bf, txok);
  886. }
  887. skb = bf->bf_mpdu;
  888. tx_info = IEEE80211_SKB_CB(skb);
  889. tx_info_priv =
  890. (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
  891. if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
  892. tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  893. if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
  894. (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
  895. if (ds->ds_txstat.ts_status == 0)
  896. nacked++;
  897. if (bf_isdata(bf)) {
  898. if (isrifs)
  899. tmp_ds = bf->bf_rifslast->bf_desc;
  900. else
  901. tmp_ds = ds;
  902. memcpy(&tx_info_priv->tx,
  903. &tmp_ds->ds_txstat,
  904. sizeof(tx_info_priv->tx));
  905. tx_info_priv->n_frames = bf->bf_nframes;
  906. tx_info_priv->n_bad_frames = nbad;
  907. }
  908. }
  909. /*
  910. * Complete this transmit unit
  911. */
  912. if (bf_isampdu(bf))
  913. ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, txok);
  914. else
  915. ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
  916. /* Wake up mac80211 queue */
  917. spin_lock_bh(&txq->axq_lock);
  918. if (txq->stopped && ath_txq_depth(sc, txq->axq_qnum) <=
  919. (ATH_TXBUF - 20)) {
  920. int qnum;
  921. qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
  922. if (qnum != -1) {
  923. ieee80211_wake_queue(sc->hw, qnum);
  924. txq->stopped = 0;
  925. }
  926. }
  927. /*
  928. * schedule any pending packets if aggregation is enabled
  929. */
  930. if (sc->sc_flags & SC_OP_TXAGGR)
  931. ath_txq_schedule(sc, txq);
  932. spin_unlock_bh(&txq->axq_lock);
  933. }
  934. return nacked;
  935. }
  936. static void ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
  937. {
  938. struct ath_hal *ah = sc->sc_ah;
  939. (void) ath9k_hw_stoptxdma(ah, txq->axq_qnum);
  940. DPRINTF(sc, ATH_DBG_XMIT, "%s: tx queue [%u] %x, link %p\n",
  941. __func__, txq->axq_qnum,
  942. ath9k_hw_gettxbuf(ah, txq->axq_qnum), txq->axq_link);
  943. }
  944. /* Drain only the data queues */
  945. static void ath_drain_txdataq(struct ath_softc *sc, bool retry_tx)
  946. {
  947. struct ath_hal *ah = sc->sc_ah;
  948. int i, status, npend = 0;
  949. if (!(sc->sc_flags & SC_OP_INVALID)) {
  950. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  951. if (ATH_TXQ_SETUP(sc, i)) {
  952. ath_tx_stopdma(sc, &sc->sc_txq[i]);
  953. /* The TxDMA may not really be stopped.
  954. * Double check the hal tx pending count */
  955. npend += ath9k_hw_numtxpending(ah,
  956. sc->sc_txq[i].axq_qnum);
  957. }
  958. }
  959. }
  960. if (npend) {
  961. /* TxDMA not stopped, reset the hal */
  962. DPRINTF(sc, ATH_DBG_XMIT,
  963. "%s: Unable to stop TxDMA. Reset HAL!\n", __func__);
  964. spin_lock_bh(&sc->sc_resetlock);
  965. if (!ath9k_hw_reset(ah,
  966. sc->sc_ah->ah_curchan,
  967. sc->sc_ht_info.tx_chan_width,
  968. sc->sc_tx_chainmask, sc->sc_rx_chainmask,
  969. sc->sc_ht_extprotspacing, true, &status)) {
  970. DPRINTF(sc, ATH_DBG_FATAL,
  971. "%s: unable to reset hardware; hal status %u\n",
  972. __func__,
  973. status);
  974. }
  975. spin_unlock_bh(&sc->sc_resetlock);
  976. }
  977. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  978. if (ATH_TXQ_SETUP(sc, i))
  979. ath_tx_draintxq(sc, &sc->sc_txq[i], retry_tx);
  980. }
  981. }
  982. /* Add a sub-frame to block ack window */
  983. static void ath_tx_addto_baw(struct ath_softc *sc,
  984. struct ath_atx_tid *tid,
  985. struct ath_buf *bf)
  986. {
  987. int index, cindex;
  988. if (bf_isretried(bf))
  989. return;
  990. index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
  991. cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
  992. ASSERT(tid->tx_buf[cindex] == NULL);
  993. tid->tx_buf[cindex] = bf;
  994. if (index >= ((tid->baw_tail - tid->baw_head) &
  995. (ATH_TID_MAX_BUFS - 1))) {
  996. tid->baw_tail = cindex;
  997. INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
  998. }
  999. }
  1000. /*
  1001. * Function to send an A-MPDU
  1002. * NB: must be called with txq lock held
  1003. */
  1004. static int ath_tx_send_ampdu(struct ath_softc *sc,
  1005. struct ath_atx_tid *tid,
  1006. struct list_head *bf_head,
  1007. struct ath_tx_control *txctl)
  1008. {
  1009. struct ath_buf *bf;
  1010. BUG_ON(list_empty(bf_head));
  1011. bf = list_first_entry(bf_head, struct ath_buf, list);
  1012. bf->bf_state.bf_type |= BUF_AMPDU;
  1013. /*
  1014. * Do not queue to h/w when any of the following conditions is true:
  1015. * - there are pending frames in software queue
  1016. * - the TID is currently paused for ADDBA/BAR request
  1017. * - seqno is not within block-ack window
  1018. * - h/w queue depth exceeds low water mark
  1019. */
  1020. if (!list_empty(&tid->buf_q) || tid->paused ||
  1021. !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
  1022. txctl->txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
  1023. /*
  1024. * Add this frame to software queue for scheduling later
  1025. * for aggregation.
  1026. */
  1027. list_splice_tail_init(bf_head, &tid->buf_q);
  1028. ath_tx_queue_tid(txctl->txq, tid);
  1029. return 0;
  1030. }
  1031. /* Add sub-frame to BAW */
  1032. ath_tx_addto_baw(sc, tid, bf);
  1033. /* Queue to h/w without aggregation */
  1034. bf->bf_nframes = 1;
  1035. bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
  1036. ath_buf_set_rate(sc, bf);
  1037. ath_tx_txqaddbuf(sc, txctl->txq, bf_head);
  1038. return 0;
  1039. }
  1040. /*
  1041. * looks up the rate
  1042. * returns aggr limit based on lowest of the rates
  1043. */
  1044. static u32 ath_lookup_rate(struct ath_softc *sc,
  1045. struct ath_buf *bf,
  1046. struct ath_atx_tid *tid)
  1047. {
  1048. struct ath_rate_table *rate_table = sc->hw_rate_table[sc->sc_curmode];
  1049. const struct ath9k_rate_table *rt = sc->sc_currates;
  1050. struct sk_buff *skb;
  1051. struct ieee80211_tx_info *tx_info;
  1052. struct ieee80211_tx_rate *rates;
  1053. struct ath_tx_info_priv *tx_info_priv;
  1054. u32 max_4ms_framelen, frame_length;
  1055. u16 aggr_limit, legacy = 0, maxampdu;
  1056. int i;
  1057. skb = (struct sk_buff *)bf->bf_mpdu;
  1058. tx_info = IEEE80211_SKB_CB(skb);
  1059. rates = tx_info->control.rates;
  1060. tx_info_priv =
  1061. (struct ath_tx_info_priv *)tx_info->rate_driver_data[0];
  1062. /*
  1063. * Find the lowest frame length among the rate series that will have a
  1064. * 4ms transmit duration.
  1065. * TODO - TXOP limit needs to be considered.
  1066. */
  1067. max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
  1068. for (i = 0; i < 4; i++) {
  1069. if (rates[i].count) {
  1070. if (rt->info[rates[i].idx].phy != PHY_HT) {
  1071. legacy = 1;
  1072. break;
  1073. }
  1074. frame_length =
  1075. rate_table->info[rates[i].idx].max_4ms_framelen;
  1076. max_4ms_framelen = min(max_4ms_framelen, frame_length);
  1077. }
  1078. }
  1079. /*
  1080. * limit aggregate size by the minimum rate if rate selected is
  1081. * not a probe rate, if rate selected is a probe rate then
  1082. * avoid aggregation of this packet.
  1083. */
  1084. if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
  1085. return 0;
  1086. aggr_limit = min(max_4ms_framelen,
  1087. (u32)ATH_AMPDU_LIMIT_DEFAULT);
  1088. /*
  1089. * h/w can accept aggregates upto 16 bit lengths (65535).
  1090. * The IE, however can hold upto 65536, which shows up here
  1091. * as zero. Ignore 65536 since we are constrained by hw.
  1092. */
  1093. maxampdu = tid->an->maxampdu;
  1094. if (maxampdu)
  1095. aggr_limit = min(aggr_limit, maxampdu);
  1096. return aggr_limit;
  1097. }
  1098. /*
  1099. * returns the number of delimiters to be added to
  1100. * meet the minimum required mpdudensity.
  1101. * caller should make sure that the rate is HT rate .
  1102. */
  1103. static int ath_compute_num_delims(struct ath_softc *sc,
  1104. struct ath_atx_tid *tid,
  1105. struct ath_buf *bf,
  1106. u16 frmlen)
  1107. {
  1108. const struct ath9k_rate_table *rt = sc->sc_currates;
  1109. struct sk_buff *skb = bf->bf_mpdu;
  1110. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1111. u32 nsymbits, nsymbols, mpdudensity;
  1112. u16 minlen;
  1113. u8 rc, flags, rix;
  1114. int width, half_gi, ndelim, mindelim;
  1115. /* Select standard number of delimiters based on frame length alone */
  1116. ndelim = ATH_AGGR_GET_NDELIM(frmlen);
  1117. /*
  1118. * If encryption enabled, hardware requires some more padding between
  1119. * subframes.
  1120. * TODO - this could be improved to be dependent on the rate.
  1121. * The hardware can keep up at lower rates, but not higher rates
  1122. */
  1123. if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
  1124. ndelim += ATH_AGGR_ENCRYPTDELIM;
  1125. /*
  1126. * Convert desired mpdu density from microeconds to bytes based
  1127. * on highest rate in rate series (i.e. first rate) to determine
  1128. * required minimum length for subframe. Take into account
  1129. * whether high rate is 20 or 40Mhz and half or full GI.
  1130. */
  1131. mpdudensity = tid->an->mpdudensity;
  1132. /*
  1133. * If there is no mpdu density restriction, no further calculation
  1134. * is needed.
  1135. */
  1136. if (mpdudensity == 0)
  1137. return ndelim;
  1138. rix = tx_info->control.rates[0].idx;
  1139. flags = tx_info->control.rates[0].flags;
  1140. rc = rt->info[rix].rateCode;
  1141. width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
  1142. half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
  1143. if (half_gi)
  1144. nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
  1145. else
  1146. nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
  1147. if (nsymbols == 0)
  1148. nsymbols = 1;
  1149. nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
  1150. minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
  1151. /* Is frame shorter than required minimum length? */
  1152. if (frmlen < minlen) {
  1153. /* Get the minimum number of delimiters required. */
  1154. mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
  1155. ndelim = max(mindelim, ndelim);
  1156. }
  1157. return ndelim;
  1158. }
  1159. /*
  1160. * For aggregation from software buffer queue.
  1161. * NB: must be called with txq lock held
  1162. */
  1163. static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
  1164. struct ath_atx_tid *tid,
  1165. struct list_head *bf_q,
  1166. struct ath_buf **bf_last,
  1167. struct aggr_rifs_param *param,
  1168. int *prev_frames)
  1169. {
  1170. #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
  1171. struct ath_buf *bf, *tbf, *bf_first, *bf_prev = NULL;
  1172. struct list_head bf_head;
  1173. int rl = 0, nframes = 0, ndelim;
  1174. u16 aggr_limit = 0, al = 0, bpad = 0,
  1175. al_delta, h_baw = tid->baw_size / 2;
  1176. enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
  1177. int prev_al = 0;
  1178. INIT_LIST_HEAD(&bf_head);
  1179. BUG_ON(list_empty(&tid->buf_q));
  1180. bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
  1181. do {
  1182. bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
  1183. /*
  1184. * do not step over block-ack window
  1185. */
  1186. if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
  1187. status = ATH_AGGR_BAW_CLOSED;
  1188. break;
  1189. }
  1190. if (!rl) {
  1191. aggr_limit = ath_lookup_rate(sc, bf, tid);
  1192. rl = 1;
  1193. }
  1194. /*
  1195. * do not exceed aggregation limit
  1196. */
  1197. al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
  1198. if (nframes && (aggr_limit <
  1199. (al + bpad + al_delta + prev_al))) {
  1200. status = ATH_AGGR_LIMITED;
  1201. break;
  1202. }
  1203. /*
  1204. * do not exceed subframe limit
  1205. */
  1206. if ((nframes + *prev_frames) >=
  1207. min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
  1208. status = ATH_AGGR_LIMITED;
  1209. break;
  1210. }
  1211. /*
  1212. * add padding for previous frame to aggregation length
  1213. */
  1214. al += bpad + al_delta;
  1215. /*
  1216. * Get the delimiters needed to meet the MPDU
  1217. * density for this node.
  1218. */
  1219. ndelim = ath_compute_num_delims(sc, tid, bf_first, bf->bf_frmlen);
  1220. bpad = PADBYTES(al_delta) + (ndelim << 2);
  1221. bf->bf_next = NULL;
  1222. bf->bf_lastfrm->bf_desc->ds_link = 0;
  1223. /*
  1224. * this packet is part of an aggregate
  1225. * - remove all descriptors belonging to this frame from
  1226. * software queue
  1227. * - add it to block ack window
  1228. * - set up descriptors for aggregation
  1229. */
  1230. list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
  1231. ath_tx_addto_baw(sc, tid, bf);
  1232. list_for_each_entry(tbf, &bf_head, list) {
  1233. ath9k_hw_set11n_aggr_middle(sc->sc_ah,
  1234. tbf->bf_desc, ndelim);
  1235. }
  1236. /*
  1237. * link buffers of this frame to the aggregate
  1238. */
  1239. list_splice_tail_init(&bf_head, bf_q);
  1240. nframes++;
  1241. if (bf_prev) {
  1242. bf_prev->bf_next = bf;
  1243. bf_prev->bf_lastfrm->bf_desc->ds_link = bf->bf_daddr;
  1244. }
  1245. bf_prev = bf;
  1246. #ifdef AGGR_NOSHORT
  1247. /*
  1248. * terminate aggregation on a small packet boundary
  1249. */
  1250. if (bf->bf_frmlen < ATH_AGGR_MINPLEN) {
  1251. status = ATH_AGGR_SHORTPKT;
  1252. break;
  1253. }
  1254. #endif
  1255. } while (!list_empty(&tid->buf_q));
  1256. bf_first->bf_al = al;
  1257. bf_first->bf_nframes = nframes;
  1258. *bf_last = bf_prev;
  1259. return status;
  1260. #undef PADBYTES
  1261. }
  1262. /*
  1263. * process pending frames possibly doing a-mpdu aggregation
  1264. * NB: must be called with txq lock held
  1265. */
  1266. static void ath_tx_sched_aggr(struct ath_softc *sc,
  1267. struct ath_txq *txq, struct ath_atx_tid *tid)
  1268. {
  1269. struct ath_buf *bf, *tbf, *bf_last, *bf_lastaggr = NULL;
  1270. enum ATH_AGGR_STATUS status;
  1271. struct list_head bf_q;
  1272. struct aggr_rifs_param param = {0, 0, 0, 0, NULL};
  1273. int prev_frames = 0;
  1274. do {
  1275. if (list_empty(&tid->buf_q))
  1276. return;
  1277. INIT_LIST_HEAD(&bf_q);
  1278. status = ath_tx_form_aggr(sc, tid, &bf_q, &bf_lastaggr, &param,
  1279. &prev_frames);
  1280. /*
  1281. * no frames picked up to be aggregated; block-ack
  1282. * window is not open
  1283. */
  1284. if (list_empty(&bf_q))
  1285. break;
  1286. bf = list_first_entry(&bf_q, struct ath_buf, list);
  1287. bf_last = list_entry(bf_q.prev, struct ath_buf, list);
  1288. bf->bf_lastbf = bf_last;
  1289. /*
  1290. * if only one frame, send as non-aggregate
  1291. */
  1292. if (bf->bf_nframes == 1) {
  1293. ASSERT(bf->bf_lastfrm == bf_last);
  1294. bf->bf_state.bf_type &= ~BUF_AGGR;
  1295. /*
  1296. * clear aggr bits for every descriptor
  1297. * XXX TODO: is there a way to optimize it?
  1298. */
  1299. list_for_each_entry(tbf, &bf_q, list) {
  1300. ath9k_hw_clr11n_aggr(sc->sc_ah, tbf->bf_desc);
  1301. }
  1302. ath_buf_set_rate(sc, bf);
  1303. ath_tx_txqaddbuf(sc, txq, &bf_q);
  1304. continue;
  1305. }
  1306. /*
  1307. * setup first desc with rate and aggr info
  1308. */
  1309. bf->bf_state.bf_type |= BUF_AGGR;
  1310. ath_buf_set_rate(sc, bf);
  1311. ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
  1312. /*
  1313. * anchor last frame of aggregate correctly
  1314. */
  1315. ASSERT(bf_lastaggr);
  1316. ASSERT(bf_lastaggr->bf_lastfrm == bf_last);
  1317. tbf = bf_lastaggr;
  1318. ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
  1319. /* XXX: We don't enter into this loop, consider removing this */
  1320. while (!list_empty(&bf_q) && !list_is_last(&tbf->list, &bf_q)) {
  1321. tbf = list_entry(tbf->list.next, struct ath_buf, list);
  1322. ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
  1323. }
  1324. txq->axq_aggr_depth++;
  1325. /*
  1326. * Normal aggregate, queue to hardware
  1327. */
  1328. ath_tx_txqaddbuf(sc, txq, &bf_q);
  1329. } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
  1330. status != ATH_AGGR_BAW_CLOSED);
  1331. }
  1332. /* Called with txq lock held */
  1333. static void ath_tid_drain(struct ath_softc *sc,
  1334. struct ath_txq *txq,
  1335. struct ath_atx_tid *tid)
  1336. {
  1337. struct ath_buf *bf;
  1338. struct list_head bf_head;
  1339. INIT_LIST_HEAD(&bf_head);
  1340. for (;;) {
  1341. if (list_empty(&tid->buf_q))
  1342. break;
  1343. bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
  1344. list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
  1345. /* update baw for software retried frame */
  1346. if (bf_isretried(bf))
  1347. ath_tx_update_baw(sc, tid, bf->bf_seqno);
  1348. /*
  1349. * do not indicate packets while holding txq spinlock.
  1350. * unlock is intentional here
  1351. */
  1352. spin_unlock(&txq->axq_lock);
  1353. /* complete this sub-frame */
  1354. ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
  1355. spin_lock(&txq->axq_lock);
  1356. }
  1357. /*
  1358. * TODO: For frame(s) that are in the retry state, we will reuse the
  1359. * sequence number(s) without setting the retry bit. The
  1360. * alternative is to give up on these and BAR the receiver's window
  1361. * forward.
  1362. */
  1363. tid->seq_next = tid->seq_start;
  1364. tid->baw_tail = tid->baw_head;
  1365. }
  1366. /*
  1367. * Drain all pending buffers
  1368. * NB: must be called with txq lock held
  1369. */
  1370. static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
  1371. struct ath_txq *txq)
  1372. {
  1373. struct ath_atx_ac *ac, *ac_tmp;
  1374. struct ath_atx_tid *tid, *tid_tmp;
  1375. list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
  1376. list_del(&ac->list);
  1377. ac->sched = false;
  1378. list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
  1379. list_del(&tid->list);
  1380. tid->sched = false;
  1381. ath_tid_drain(sc, txq, tid);
  1382. }
  1383. }
  1384. }
  1385. static void ath_tx_setup_buffer(struct ath_softc *sc, struct ath_buf *bf,
  1386. struct sk_buff *skb, struct scatterlist *sg,
  1387. struct ath_tx_control *txctl)
  1388. {
  1389. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1390. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1391. struct ath_tx_info_priv *tx_info_priv;
  1392. int hdrlen;
  1393. __le16 fc;
  1394. tx_info_priv = kzalloc(sizeof(*tx_info_priv), GFP_KERNEL);
  1395. tx_info->rate_driver_data[0] = tx_info_priv;
  1396. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1397. fc = hdr->frame_control;
  1398. ATH_TXBUF_RESET(bf);
  1399. /* Frame type */
  1400. bf->bf_frmlen = skb->len + FCS_LEN - (hdrlen & 3);
  1401. ieee80211_is_data(fc) ?
  1402. (bf->bf_state.bf_type |= BUF_DATA) :
  1403. (bf->bf_state.bf_type &= ~BUF_DATA);
  1404. ieee80211_is_back_req(fc) ?
  1405. (bf->bf_state.bf_type |= BUF_BAR) :
  1406. (bf->bf_state.bf_type &= ~BUF_BAR);
  1407. ieee80211_is_pspoll(fc) ?
  1408. (bf->bf_state.bf_type |= BUF_PSPOLL) :
  1409. (bf->bf_state.bf_type &= ~BUF_PSPOLL);
  1410. (sc->sc_flags & SC_OP_PREAMBLE_SHORT) ?
  1411. (bf->bf_state.bf_type |= BUF_SHORT_PREAMBLE) :
  1412. (bf->bf_state.bf_type &= ~BUF_SHORT_PREAMBLE);
  1413. (sc->hw->conf.ht.enabled && !is_pae(skb) &&
  1414. (tx_info->flags & IEEE80211_TX_CTL_AMPDU)) ?
  1415. (bf->bf_state.bf_type |= BUF_HT) :
  1416. (bf->bf_state.bf_type &= ~BUF_HT);
  1417. bf->bf_flags = setup_tx_flags(sc, skb, txctl->txq);
  1418. /* Crypto */
  1419. bf->bf_keytype = get_hw_crypto_keytype(skb);
  1420. if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR) {
  1421. bf->bf_frmlen += tx_info->control.hw_key->icv_len;
  1422. bf->bf_keyix = tx_info->control.hw_key->hw_key_idx;
  1423. } else {
  1424. bf->bf_keyix = ATH9K_TXKEYIX_INVALID;
  1425. }
  1426. /* Rate series */
  1427. setup_rate_retries(sc, skb);
  1428. /* Assign seqno, tidno */
  1429. if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR))
  1430. assign_aggr_tid_seqno(skb, bf);
  1431. /* DMA setup */
  1432. bf->bf_mpdu = skb;
  1433. bf->bf_dmacontext = pci_map_single(sc->pdev, skb->data,
  1434. skb->len, PCI_DMA_TODEVICE);
  1435. bf->bf_buf_addr = bf->bf_dmacontext;
  1436. }
  1437. /* FIXME: tx power */
  1438. static void ath_tx_start_dma(struct ath_softc *sc, struct ath_buf *bf,
  1439. struct scatterlist *sg, u32 n_sg,
  1440. struct ath_tx_control *txctl)
  1441. {
  1442. struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
  1443. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1444. struct ath_node *an = NULL;
  1445. struct list_head bf_head;
  1446. struct ath_desc *ds;
  1447. struct ath_atx_tid *tid;
  1448. struct ath_hal *ah = sc->sc_ah;
  1449. int frm_type;
  1450. frm_type = get_hw_packet_type(skb);
  1451. INIT_LIST_HEAD(&bf_head);
  1452. list_add_tail(&bf->list, &bf_head);
  1453. /* setup descriptor */
  1454. ds = bf->bf_desc;
  1455. ds->ds_link = 0;
  1456. ds->ds_data = bf->bf_buf_addr;
  1457. /* Formulate first tx descriptor with tx controls */
  1458. ath9k_hw_set11n_txdesc(ah, ds, bf->bf_frmlen, frm_type, MAX_RATE_POWER,
  1459. bf->bf_keyix, bf->bf_keytype, bf->bf_flags);
  1460. ath9k_hw_filltxdesc(ah, ds,
  1461. sg_dma_len(sg), /* segment length */
  1462. true, /* first segment */
  1463. (n_sg == 1) ? true : false, /* last segment */
  1464. ds); /* first descriptor */
  1465. bf->bf_lastfrm = bf;
  1466. spin_lock_bh(&txctl->txq->axq_lock);
  1467. if (bf_isht(bf) && (sc->sc_flags & SC_OP_TXAGGR) &&
  1468. tx_info->control.sta) {
  1469. an = (struct ath_node *)tx_info->control.sta->drv_priv;
  1470. tid = ATH_AN_2_TID(an, bf->bf_tidno);
  1471. if (ath_aggr_query(sc, an, bf->bf_tidno)) {
  1472. /*
  1473. * Try aggregation if it's a unicast data frame
  1474. * and the destination is HT capable.
  1475. */
  1476. ath_tx_send_ampdu(sc, tid, &bf_head, txctl);
  1477. } else {
  1478. /*
  1479. * Send this frame as regular when ADDBA
  1480. * exchange is neither complete nor pending.
  1481. */
  1482. ath_tx_send_normal(sc, txctl->txq,
  1483. tid, &bf_head);
  1484. }
  1485. } else {
  1486. bf->bf_lastbf = bf;
  1487. bf->bf_nframes = 1;
  1488. ath_buf_set_rate(sc, bf);
  1489. ath_tx_txqaddbuf(sc, txctl->txq, &bf_head);
  1490. }
  1491. spin_unlock_bh(&txctl->txq->axq_lock);
  1492. }
  1493. int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb,
  1494. struct ath_tx_control *txctl)
  1495. {
  1496. struct ath_buf *bf;
  1497. struct scatterlist sg;
  1498. /* Check if a tx buffer is available */
  1499. bf = ath_tx_get_buffer(sc);
  1500. if (!bf) {
  1501. DPRINTF(sc, ATH_DBG_XMIT, "%s: TX buffers are full\n",
  1502. __func__);
  1503. return -1;
  1504. }
  1505. ath_tx_setup_buffer(sc, bf, skb, &sg, txctl);
  1506. /* Setup S/G */
  1507. memset(&sg, 0, sizeof(struct scatterlist));
  1508. sg_dma_address(&sg) = bf->bf_dmacontext;
  1509. sg_dma_len(&sg) = skb->len;
  1510. ath_tx_start_dma(sc, bf, &sg, 1, txctl);
  1511. return 0;
  1512. }
  1513. /* Initialize TX queue and h/w */
  1514. int ath_tx_init(struct ath_softc *sc, int nbufs)
  1515. {
  1516. int error = 0;
  1517. do {
  1518. spin_lock_init(&sc->sc_txbuflock);
  1519. /* Setup tx descriptors */
  1520. error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
  1521. "tx", nbufs, 1);
  1522. if (error != 0) {
  1523. DPRINTF(sc, ATH_DBG_FATAL,
  1524. "%s: failed to allocate tx descriptors: %d\n",
  1525. __func__, error);
  1526. break;
  1527. }
  1528. /* XXX allocate beacon state together with vap */
  1529. error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
  1530. "beacon", ATH_BCBUF, 1);
  1531. if (error != 0) {
  1532. DPRINTF(sc, ATH_DBG_FATAL,
  1533. "%s: failed to allocate "
  1534. "beacon descripotrs: %d\n",
  1535. __func__, error);
  1536. break;
  1537. }
  1538. } while (0);
  1539. if (error != 0)
  1540. ath_tx_cleanup(sc);
  1541. return error;
  1542. }
  1543. /* Reclaim all tx queue resources */
  1544. int ath_tx_cleanup(struct ath_softc *sc)
  1545. {
  1546. /* cleanup beacon descriptors */
  1547. if (sc->sc_bdma.dd_desc_len != 0)
  1548. ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
  1549. /* cleanup tx descriptors */
  1550. if (sc->sc_txdma.dd_desc_len != 0)
  1551. ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
  1552. return 0;
  1553. }
  1554. /* Setup a h/w transmit queue */
  1555. struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
  1556. {
  1557. struct ath_hal *ah = sc->sc_ah;
  1558. struct ath9k_tx_queue_info qi;
  1559. int qnum;
  1560. memset(&qi, 0, sizeof(qi));
  1561. qi.tqi_subtype = subtype;
  1562. qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
  1563. qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
  1564. qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
  1565. qi.tqi_physCompBuf = 0;
  1566. /*
  1567. * Enable interrupts only for EOL and DESC conditions.
  1568. * We mark tx descriptors to receive a DESC interrupt
  1569. * when a tx queue gets deep; otherwise waiting for the
  1570. * EOL to reap descriptors. Note that this is done to
  1571. * reduce interrupt load and this only defers reaping
  1572. * descriptors, never transmitting frames. Aside from
  1573. * reducing interrupts this also permits more concurrency.
  1574. * The only potential downside is if the tx queue backs
  1575. * up in which case the top half of the kernel may backup
  1576. * due to a lack of tx descriptors.
  1577. *
  1578. * The UAPSD queue is an exception, since we take a desc-
  1579. * based intr on the EOSP frames.
  1580. */
  1581. if (qtype == ATH9K_TX_QUEUE_UAPSD)
  1582. qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
  1583. else
  1584. qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
  1585. TXQ_FLAG_TXDESCINT_ENABLE;
  1586. qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
  1587. if (qnum == -1) {
  1588. /*
  1589. * NB: don't print a message, this happens
  1590. * normally on parts with too few tx queues
  1591. */
  1592. return NULL;
  1593. }
  1594. if (qnum >= ARRAY_SIZE(sc->sc_txq)) {
  1595. DPRINTF(sc, ATH_DBG_FATAL,
  1596. "%s: hal qnum %u out of range, max %u!\n",
  1597. __func__, qnum, (unsigned int)ARRAY_SIZE(sc->sc_txq));
  1598. ath9k_hw_releasetxqueue(ah, qnum);
  1599. return NULL;
  1600. }
  1601. if (!ATH_TXQ_SETUP(sc, qnum)) {
  1602. struct ath_txq *txq = &sc->sc_txq[qnum];
  1603. txq->axq_qnum = qnum;
  1604. txq->axq_link = NULL;
  1605. INIT_LIST_HEAD(&txq->axq_q);
  1606. INIT_LIST_HEAD(&txq->axq_acq);
  1607. spin_lock_init(&txq->axq_lock);
  1608. txq->axq_depth = 0;
  1609. txq->axq_aggr_depth = 0;
  1610. txq->axq_totalqueued = 0;
  1611. txq->axq_linkbuf = NULL;
  1612. sc->sc_txqsetup |= 1<<qnum;
  1613. }
  1614. return &sc->sc_txq[qnum];
  1615. }
  1616. /* Reclaim resources for a setup queue */
  1617. void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
  1618. {
  1619. ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
  1620. sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
  1621. }
  1622. /*
  1623. * Setup a hardware data transmit queue for the specified
  1624. * access control. The hal may not support all requested
  1625. * queues in which case it will return a reference to a
  1626. * previously setup queue. We record the mapping from ac's
  1627. * to h/w queues for use by ath_tx_start and also track
  1628. * the set of h/w queues being used to optimize work in the
  1629. * transmit interrupt handler and related routines.
  1630. */
  1631. int ath_tx_setup(struct ath_softc *sc, int haltype)
  1632. {
  1633. struct ath_txq *txq;
  1634. if (haltype >= ARRAY_SIZE(sc->sc_haltype2q)) {
  1635. DPRINTF(sc, ATH_DBG_FATAL,
  1636. "%s: HAL AC %u out of range, max %zu!\n",
  1637. __func__, haltype, ARRAY_SIZE(sc->sc_haltype2q));
  1638. return 0;
  1639. }
  1640. txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
  1641. if (txq != NULL) {
  1642. sc->sc_haltype2q[haltype] = txq->axq_qnum;
  1643. return 1;
  1644. } else
  1645. return 0;
  1646. }
  1647. int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
  1648. {
  1649. int qnum;
  1650. switch (qtype) {
  1651. case ATH9K_TX_QUEUE_DATA:
  1652. if (haltype >= ARRAY_SIZE(sc->sc_haltype2q)) {
  1653. DPRINTF(sc, ATH_DBG_FATAL,
  1654. "%s: HAL AC %u out of range, max %zu!\n",
  1655. __func__,
  1656. haltype, ARRAY_SIZE(sc->sc_haltype2q));
  1657. return -1;
  1658. }
  1659. qnum = sc->sc_haltype2q[haltype];
  1660. break;
  1661. case ATH9K_TX_QUEUE_BEACON:
  1662. qnum = sc->sc_bhalq;
  1663. break;
  1664. case ATH9K_TX_QUEUE_CAB:
  1665. qnum = sc->sc_cabq->axq_qnum;
  1666. break;
  1667. default:
  1668. qnum = -1;
  1669. }
  1670. return qnum;
  1671. }
  1672. /* Get a transmit queue, if available */
  1673. struct ath_txq *ath_test_get_txq(struct ath_softc *sc, struct sk_buff *skb)
  1674. {
  1675. struct ath_txq *txq = NULL;
  1676. int qnum;
  1677. qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
  1678. txq = &sc->sc_txq[qnum];
  1679. spin_lock_bh(&txq->axq_lock);
  1680. /* Try to avoid running out of descriptors */
  1681. if (txq->axq_depth >= (ATH_TXBUF - 20)) {
  1682. DPRINTF(sc, ATH_DBG_FATAL,
  1683. "%s: TX queue: %d is full, depth: %d\n",
  1684. __func__, qnum, txq->axq_depth);
  1685. ieee80211_stop_queue(sc->hw, skb_get_queue_mapping(skb));
  1686. txq->stopped = 1;
  1687. spin_unlock_bh(&txq->axq_lock);
  1688. return NULL;
  1689. }
  1690. spin_unlock_bh(&txq->axq_lock);
  1691. return txq;
  1692. }
  1693. /* Update parameters for a transmit queue */
  1694. int ath_txq_update(struct ath_softc *sc, int qnum,
  1695. struct ath9k_tx_queue_info *qinfo)
  1696. {
  1697. struct ath_hal *ah = sc->sc_ah;
  1698. int error = 0;
  1699. struct ath9k_tx_queue_info qi;
  1700. if (qnum == sc->sc_bhalq) {
  1701. /*
  1702. * XXX: for beacon queue, we just save the parameter.
  1703. * It will be picked up by ath_beaconq_config when
  1704. * it's necessary.
  1705. */
  1706. sc->sc_beacon_qi = *qinfo;
  1707. return 0;
  1708. }
  1709. ASSERT(sc->sc_txq[qnum].axq_qnum == qnum);
  1710. ath9k_hw_get_txq_props(ah, qnum, &qi);
  1711. qi.tqi_aifs = qinfo->tqi_aifs;
  1712. qi.tqi_cwmin = qinfo->tqi_cwmin;
  1713. qi.tqi_cwmax = qinfo->tqi_cwmax;
  1714. qi.tqi_burstTime = qinfo->tqi_burstTime;
  1715. qi.tqi_readyTime = qinfo->tqi_readyTime;
  1716. if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
  1717. DPRINTF(sc, ATH_DBG_FATAL,
  1718. "%s: unable to update hardware queue %u!\n",
  1719. __func__, qnum);
  1720. error = -EIO;
  1721. } else {
  1722. ath9k_hw_resettxqueue(ah, qnum); /* push to h/w */
  1723. }
  1724. return error;
  1725. }
  1726. int ath_cabq_update(struct ath_softc *sc)
  1727. {
  1728. struct ath9k_tx_queue_info qi;
  1729. int qnum = sc->sc_cabq->axq_qnum;
  1730. struct ath_beacon_config conf;
  1731. ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
  1732. /*
  1733. * Ensure the readytime % is within the bounds.
  1734. */
  1735. if (sc->sc_config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
  1736. sc->sc_config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
  1737. else if (sc->sc_config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
  1738. sc->sc_config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
  1739. ath_get_beaconconfig(sc, ATH_IF_ID_ANY, &conf);
  1740. qi.tqi_readyTime =
  1741. (conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
  1742. ath_txq_update(sc, qnum, &qi);
  1743. return 0;
  1744. }
  1745. /* Deferred processing of transmit interrupt */
  1746. void ath_tx_tasklet(struct ath_softc *sc)
  1747. {
  1748. int i;
  1749. u32 qcumask = ((1 << ATH9K_NUM_TX_QUEUES) - 1);
  1750. ath9k_hw_gettxintrtxqs(sc->sc_ah, &qcumask);
  1751. /*
  1752. * Process each active queue.
  1753. */
  1754. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1755. if (ATH_TXQ_SETUP(sc, i) && (qcumask & (1 << i)))
  1756. ath_tx_processq(sc, &sc->sc_txq[i]);
  1757. }
  1758. }
  1759. void ath_tx_draintxq(struct ath_softc *sc,
  1760. struct ath_txq *txq, bool retry_tx)
  1761. {
  1762. struct ath_buf *bf, *lastbf;
  1763. struct list_head bf_head;
  1764. INIT_LIST_HEAD(&bf_head);
  1765. /*
  1766. * NB: this assumes output has been stopped and
  1767. * we do not need to block ath_tx_tasklet
  1768. */
  1769. for (;;) {
  1770. spin_lock_bh(&txq->axq_lock);
  1771. if (list_empty(&txq->axq_q)) {
  1772. txq->axq_link = NULL;
  1773. txq->axq_linkbuf = NULL;
  1774. spin_unlock_bh(&txq->axq_lock);
  1775. break;
  1776. }
  1777. bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
  1778. if (bf->bf_status & ATH_BUFSTATUS_STALE) {
  1779. list_del(&bf->list);
  1780. spin_unlock_bh(&txq->axq_lock);
  1781. spin_lock_bh(&sc->sc_txbuflock);
  1782. list_add_tail(&bf->list, &sc->sc_txbuf);
  1783. spin_unlock_bh(&sc->sc_txbuflock);
  1784. continue;
  1785. }
  1786. lastbf = bf->bf_lastbf;
  1787. if (!retry_tx)
  1788. lastbf->bf_desc->ds_txstat.ts_flags =
  1789. ATH9K_TX_SW_ABORTED;
  1790. /* remove ath_buf's of the same mpdu from txq */
  1791. list_cut_position(&bf_head, &txq->axq_q, &lastbf->list);
  1792. txq->axq_depth--;
  1793. spin_unlock_bh(&txq->axq_lock);
  1794. if (bf_isampdu(bf))
  1795. ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, 0);
  1796. else
  1797. ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
  1798. }
  1799. /* flush any pending frames if aggregation is enabled */
  1800. if (sc->sc_flags & SC_OP_TXAGGR) {
  1801. if (!retry_tx) {
  1802. spin_lock_bh(&txq->axq_lock);
  1803. ath_txq_drain_pending_buffers(sc, txq);
  1804. spin_unlock_bh(&txq->axq_lock);
  1805. }
  1806. }
  1807. }
  1808. /* Drain the transmit queues and reclaim resources */
  1809. void ath_draintxq(struct ath_softc *sc, bool retry_tx)
  1810. {
  1811. /* stop beacon queue. The beacon will be freed when
  1812. * we go to INIT state */
  1813. if (!(sc->sc_flags & SC_OP_INVALID)) {
  1814. (void) ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
  1815. DPRINTF(sc, ATH_DBG_XMIT, "%s: beacon queue %x\n", __func__,
  1816. ath9k_hw_gettxbuf(sc->sc_ah, sc->sc_bhalq));
  1817. }
  1818. ath_drain_txdataq(sc, retry_tx);
  1819. }
  1820. u32 ath_txq_depth(struct ath_softc *sc, int qnum)
  1821. {
  1822. return sc->sc_txq[qnum].axq_depth;
  1823. }
  1824. u32 ath_txq_aggr_depth(struct ath_softc *sc, int qnum)
  1825. {
  1826. return sc->sc_txq[qnum].axq_aggr_depth;
  1827. }
  1828. bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an, u8 tidno)
  1829. {
  1830. struct ath_atx_tid *txtid;
  1831. if (!(sc->sc_flags & SC_OP_TXAGGR))
  1832. return false;
  1833. txtid = ATH_AN_2_TID(an, tidno);
  1834. if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
  1835. if (!(txtid->state & AGGR_ADDBA_PROGRESS) &&
  1836. (txtid->addba_exchangeattempts < ADDBA_EXCHANGE_ATTEMPTS)) {
  1837. txtid->addba_exchangeattempts++;
  1838. return true;
  1839. }
  1840. }
  1841. return false;
  1842. }
  1843. /* Start TX aggregation */
  1844. int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
  1845. u16 tid, u16 *ssn)
  1846. {
  1847. struct ath_atx_tid *txtid;
  1848. struct ath_node *an;
  1849. an = (struct ath_node *)sta->drv_priv;
  1850. if (sc->sc_flags & SC_OP_TXAGGR) {
  1851. txtid = ATH_AN_2_TID(an, tid);
  1852. txtid->state |= AGGR_ADDBA_PROGRESS;
  1853. ath_tx_pause_tid(sc, txtid);
  1854. }
  1855. return 0;
  1856. }
  1857. /* Stop tx aggregation */
  1858. int ath_tx_aggr_stop(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
  1859. {
  1860. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  1861. ath_tx_aggr_teardown(sc, an, tid);
  1862. return 0;
  1863. }
  1864. /* Resume tx aggregation */
  1865. void ath_tx_aggr_resume(struct ath_softc *sc, struct ieee80211_sta *sta, u16 tid)
  1866. {
  1867. struct ath_atx_tid *txtid;
  1868. struct ath_node *an;
  1869. an = (struct ath_node *)sta->drv_priv;
  1870. if (sc->sc_flags & SC_OP_TXAGGR) {
  1871. txtid = ATH_AN_2_TID(an, tid);
  1872. txtid->baw_size =
  1873. IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
  1874. txtid->state |= AGGR_ADDBA_COMPLETE;
  1875. txtid->state &= ~AGGR_ADDBA_PROGRESS;
  1876. ath_tx_resume_tid(sc, txtid);
  1877. }
  1878. }
  1879. /*
  1880. * Performs transmit side cleanup when TID changes from aggregated to
  1881. * unaggregated.
  1882. * - Pause the TID and mark cleanup in progress
  1883. * - Discard all retry frames from the s/w queue.
  1884. */
  1885. void ath_tx_aggr_teardown(struct ath_softc *sc, struct ath_node *an, u8 tid)
  1886. {
  1887. struct ath_atx_tid *txtid = ATH_AN_2_TID(an, tid);
  1888. struct ath_txq *txq = &sc->sc_txq[txtid->ac->qnum];
  1889. struct ath_buf *bf;
  1890. struct list_head bf_head;
  1891. INIT_LIST_HEAD(&bf_head);
  1892. DPRINTF(sc, ATH_DBG_AGGR, "%s: teardown TX aggregation\n", __func__);
  1893. if (txtid->state & AGGR_CLEANUP) /* cleanup is in progress */
  1894. return;
  1895. if (!(txtid->state & AGGR_ADDBA_COMPLETE)) {
  1896. txtid->addba_exchangeattempts = 0;
  1897. return;
  1898. }
  1899. /* TID must be paused first */
  1900. ath_tx_pause_tid(sc, txtid);
  1901. /* drop all software retried frames and mark this TID */
  1902. spin_lock_bh(&txq->axq_lock);
  1903. while (!list_empty(&txtid->buf_q)) {
  1904. bf = list_first_entry(&txtid->buf_q, struct ath_buf, list);
  1905. if (!bf_isretried(bf)) {
  1906. /*
  1907. * NB: it's based on the assumption that
  1908. * software retried frame will always stay
  1909. * at the head of software queue.
  1910. */
  1911. break;
  1912. }
  1913. list_cut_position(&bf_head,
  1914. &txtid->buf_q, &bf->bf_lastfrm->list);
  1915. ath_tx_update_baw(sc, txtid, bf->bf_seqno);
  1916. /* complete this sub-frame */
  1917. ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
  1918. }
  1919. if (txtid->baw_head != txtid->baw_tail) {
  1920. spin_unlock_bh(&txq->axq_lock);
  1921. txtid->state |= AGGR_CLEANUP;
  1922. } else {
  1923. txtid->state &= ~AGGR_ADDBA_COMPLETE;
  1924. txtid->addba_exchangeattempts = 0;
  1925. spin_unlock_bh(&txq->axq_lock);
  1926. ath_tx_flush_tid(sc, txtid);
  1927. }
  1928. }
  1929. /*
  1930. * Tx scheduling logic
  1931. * NB: must be called with txq lock held
  1932. */
  1933. void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
  1934. {
  1935. struct ath_atx_ac *ac;
  1936. struct ath_atx_tid *tid;
  1937. /* nothing to schedule */
  1938. if (list_empty(&txq->axq_acq))
  1939. return;
  1940. /*
  1941. * get the first node/ac pair on the queue
  1942. */
  1943. ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
  1944. list_del(&ac->list);
  1945. ac->sched = false;
  1946. /*
  1947. * process a single tid per destination
  1948. */
  1949. do {
  1950. /* nothing to schedule */
  1951. if (list_empty(&ac->tid_q))
  1952. return;
  1953. tid = list_first_entry(&ac->tid_q, struct ath_atx_tid, list);
  1954. list_del(&tid->list);
  1955. tid->sched = false;
  1956. if (tid->paused) /* check next tid to keep h/w busy */
  1957. continue;
  1958. if ((txq->axq_depth % 2) == 0)
  1959. ath_tx_sched_aggr(sc, txq, tid);
  1960. /*
  1961. * add tid to round-robin queue if more frames
  1962. * are pending for the tid
  1963. */
  1964. if (!list_empty(&tid->buf_q))
  1965. ath_tx_queue_tid(txq, tid);
  1966. /* only schedule one TID at a time */
  1967. break;
  1968. } while (!list_empty(&ac->tid_q));
  1969. /*
  1970. * schedule AC if more TIDs need processing
  1971. */
  1972. if (!list_empty(&ac->tid_q)) {
  1973. /*
  1974. * add dest ac to txq if not already added
  1975. */
  1976. if (!ac->sched) {
  1977. ac->sched = true;
  1978. list_add_tail(&ac->list, &txq->axq_acq);
  1979. }
  1980. }
  1981. }
  1982. /* Initialize per-node transmit state */
  1983. void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an)
  1984. {
  1985. struct ath_atx_tid *tid;
  1986. struct ath_atx_ac *ac;
  1987. int tidno, acno;
  1988. /*
  1989. * Init per tid tx state
  1990. */
  1991. for (tidno = 0, tid = &an->an_aggr.tx.tid[tidno];
  1992. tidno < WME_NUM_TID;
  1993. tidno++, tid++) {
  1994. tid->an = an;
  1995. tid->tidno = tidno;
  1996. tid->seq_start = tid->seq_next = 0;
  1997. tid->baw_size = WME_MAX_BA;
  1998. tid->baw_head = tid->baw_tail = 0;
  1999. tid->sched = false;
  2000. tid->paused = false;
  2001. tid->state &= ~AGGR_CLEANUP;
  2002. INIT_LIST_HEAD(&tid->buf_q);
  2003. acno = TID_TO_WME_AC(tidno);
  2004. tid->ac = &an->an_aggr.tx.ac[acno];
  2005. /* ADDBA state */
  2006. tid->state &= ~AGGR_ADDBA_COMPLETE;
  2007. tid->state &= ~AGGR_ADDBA_PROGRESS;
  2008. tid->addba_exchangeattempts = 0;
  2009. }
  2010. /*
  2011. * Init per ac tx state
  2012. */
  2013. for (acno = 0, ac = &an->an_aggr.tx.ac[acno];
  2014. acno < WME_NUM_AC; acno++, ac++) {
  2015. ac->sched = false;
  2016. INIT_LIST_HEAD(&ac->tid_q);
  2017. switch (acno) {
  2018. case WME_AC_BE:
  2019. ac->qnum = ath_tx_get_qnum(sc,
  2020. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
  2021. break;
  2022. case WME_AC_BK:
  2023. ac->qnum = ath_tx_get_qnum(sc,
  2024. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BK);
  2025. break;
  2026. case WME_AC_VI:
  2027. ac->qnum = ath_tx_get_qnum(sc,
  2028. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VI);
  2029. break;
  2030. case WME_AC_VO:
  2031. ac->qnum = ath_tx_get_qnum(sc,
  2032. ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_VO);
  2033. break;
  2034. }
  2035. }
  2036. }
  2037. /* Cleanupthe pending buffers for the node. */
  2038. void ath_tx_node_cleanup(struct ath_softc *sc, struct ath_node *an)
  2039. {
  2040. int i;
  2041. struct ath_atx_ac *ac, *ac_tmp;
  2042. struct ath_atx_tid *tid, *tid_tmp;
  2043. struct ath_txq *txq;
  2044. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  2045. if (ATH_TXQ_SETUP(sc, i)) {
  2046. txq = &sc->sc_txq[i];
  2047. spin_lock(&txq->axq_lock);
  2048. list_for_each_entry_safe(ac,
  2049. ac_tmp, &txq->axq_acq, list) {
  2050. tid = list_first_entry(&ac->tid_q,
  2051. struct ath_atx_tid, list);
  2052. if (tid && tid->an != an)
  2053. continue;
  2054. list_del(&ac->list);
  2055. ac->sched = false;
  2056. list_for_each_entry_safe(tid,
  2057. tid_tmp, &ac->tid_q, list) {
  2058. list_del(&tid->list);
  2059. tid->sched = false;
  2060. ath_tid_drain(sc, txq, tid);
  2061. tid->state &= ~AGGR_ADDBA_COMPLETE;
  2062. tid->addba_exchangeattempts = 0;
  2063. tid->state &= ~AGGR_CLEANUP;
  2064. }
  2065. }
  2066. spin_unlock(&txq->axq_lock);
  2067. }
  2068. }
  2069. }
  2070. void ath_tx_cabq(struct ath_softc *sc, struct sk_buff *skb)
  2071. {
  2072. int hdrlen, padsize;
  2073. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2074. struct ath_tx_control txctl;
  2075. memset(&txctl, 0, sizeof(struct ath_tx_control));
  2076. /*
  2077. * As a temporary workaround, assign seq# here; this will likely need
  2078. * to be cleaned up to work better with Beacon transmission and virtual
  2079. * BSSes.
  2080. */
  2081. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  2082. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  2083. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  2084. sc->seq_no += 0x10;
  2085. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  2086. hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
  2087. }
  2088. /* Add the padding after the header if this is not already done */
  2089. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  2090. if (hdrlen & 3) {
  2091. padsize = hdrlen % 4;
  2092. if (skb_headroom(skb) < padsize) {
  2093. DPRINTF(sc, ATH_DBG_XMIT, "%s: TX CABQ padding "
  2094. "failed\n", __func__);
  2095. dev_kfree_skb_any(skb);
  2096. return;
  2097. }
  2098. skb_push(skb, padsize);
  2099. memmove(skb->data, skb->data + padsize, hdrlen);
  2100. }
  2101. txctl.txq = sc->sc_cabq;
  2102. DPRINTF(sc, ATH_DBG_XMIT, "%s: transmitting CABQ packet, skb: %p\n",
  2103. __func__,
  2104. skb);
  2105. if (ath_tx_start(sc, skb, &txctl) != 0) {
  2106. DPRINTF(sc, ATH_DBG_XMIT, "%s: TX failed\n", __func__);
  2107. goto exit;
  2108. }
  2109. return;
  2110. exit:
  2111. dev_kfree_skb_any(skb);
  2112. }