xmit.c 72 KB

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