xmit.c 63 KB

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