xmit.c 70 KB

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