xmit.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871
  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. tx_info->flags |= ATH9K_TXDESC_NOACK;
  298. if (tx_info->flags & IEEE80211_TX_CTL_USE_RTS_CTS)
  299. tx_info->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_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 & ATH_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, sc->sc_flags & ATH_PREAMBLE_SHORT);
  370. }
  371. if (ieee80211_has_morefrags(fc) ||
  372. (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)) {
  373. /*
  374. ** Force hardware to use computed duration for next
  375. ** fragment by disabling multi-rate retry, which
  376. ** updates duration based on the multi-rate
  377. ** duration table.
  378. */
  379. rcs[1].tries = rcs[2].tries = rcs[3].tries = 0;
  380. rcs[1].rix = rcs[2].rix = rcs[3].rix = 0;
  381. /* reset tries but keep rate index */
  382. rcs[0].tries = ATH_TXMAXTRY;
  383. }
  384. hdr->duration_id = cpu_to_le16(dur);
  385. }
  386. /*
  387. * Determine if a tx interrupt should be generated for
  388. * this descriptor. We take a tx interrupt to reap
  389. * descriptors when the h/w hits an EOL condition or
  390. * when the descriptor is specifically marked to generate
  391. * an interrupt. We periodically mark descriptors in this
  392. * way to insure timely replenishing of the supply needed
  393. * for sending frames. Defering interrupts reduces system
  394. * load and potentially allows more concurrent work to be
  395. * done but if done to aggressively can cause senders to
  396. * backup.
  397. *
  398. * NB: use >= to deal with sc_txintrperiod changing
  399. * dynamically through sysctl.
  400. */
  401. spin_lock_bh(&txq->axq_lock);
  402. if ((++txq->axq_intrcnt >= sc->sc_txintrperiod)) {
  403. txctl->flags |= ATH9K_TXDESC_INTREQ;
  404. txq->axq_intrcnt = 0;
  405. }
  406. spin_unlock_bh(&txq->axq_lock);
  407. if (is_multicast_ether_addr(hdr->addr1)) {
  408. antenna = sc->sc_mcastantenna + 1;
  409. sc->sc_mcastantenna = (sc->sc_mcastantenna + 1) & 0x1;
  410. } else
  411. antenna = sc->sc_txantenna;
  412. #ifdef USE_LEGACY_HAL
  413. txctl->antenna = antenna;
  414. #endif
  415. return 0;
  416. }
  417. /* To complete a chain of buffers associated a frame */
  418. static void ath_tx_complete_buf(struct ath_softc *sc,
  419. struct ath_buf *bf,
  420. struct list_head *bf_q,
  421. int txok, int sendbar)
  422. {
  423. struct sk_buff *skb = bf->bf_mpdu;
  424. struct ath_xmit_status tx_status;
  425. dma_addr_t *pa;
  426. /*
  427. * Set retry information.
  428. * NB: Don't use the information in the descriptor, because the frame
  429. * could be software retried.
  430. */
  431. tx_status.retries = bf->bf_retries;
  432. tx_status.flags = 0;
  433. if (sendbar)
  434. tx_status.flags = ATH_TX_BAR;
  435. if (!txok) {
  436. tx_status.flags |= ATH_TX_ERROR;
  437. if (bf->bf_isxretried)
  438. tx_status.flags |= ATH_TX_XRETRY;
  439. }
  440. /* Unmap this frame */
  441. pa = get_dma_mem_context(bf, bf_dmacontext);
  442. pci_unmap_single(sc->pdev,
  443. *pa,
  444. skb->len,
  445. PCI_DMA_TODEVICE);
  446. /* complete this frame */
  447. ath_tx_complete(sc, skb, &tx_status, bf->bf_node);
  448. /*
  449. * Return the list of ath_buf of this mpdu to free queue
  450. */
  451. spin_lock_bh(&sc->sc_txbuflock);
  452. list_splice_tail_init(bf_q, &sc->sc_txbuf);
  453. spin_unlock_bh(&sc->sc_txbuflock);
  454. }
  455. /*
  456. * queue up a dest/ac pair for tx scheduling
  457. * NB: must be called with txq lock held
  458. */
  459. static void ath_tx_queue_tid(struct ath_txq *txq, struct ath_atx_tid *tid)
  460. {
  461. struct ath_atx_ac *ac = tid->ac;
  462. /*
  463. * if tid is paused, hold off
  464. */
  465. if (tid->paused)
  466. return;
  467. /*
  468. * add tid to ac atmost once
  469. */
  470. if (tid->sched)
  471. return;
  472. tid->sched = true;
  473. list_add_tail(&tid->list, &ac->tid_q);
  474. /*
  475. * add node ac to txq atmost once
  476. */
  477. if (ac->sched)
  478. return;
  479. ac->sched = true;
  480. list_add_tail(&ac->list, &txq->axq_acq);
  481. }
  482. /* pause a tid */
  483. static void ath_tx_pause_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
  484. {
  485. struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
  486. spin_lock_bh(&txq->axq_lock);
  487. tid->paused++;
  488. spin_unlock_bh(&txq->axq_lock);
  489. }
  490. /* resume a tid and schedule aggregate */
  491. void ath_tx_resume_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
  492. {
  493. struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
  494. ASSERT(tid->paused > 0);
  495. spin_lock_bh(&txq->axq_lock);
  496. tid->paused--;
  497. if (tid->paused > 0)
  498. goto unlock;
  499. if (list_empty(&tid->buf_q))
  500. goto unlock;
  501. /*
  502. * Add this TID to scheduler and try to send out aggregates
  503. */
  504. ath_tx_queue_tid(txq, tid);
  505. ath_txq_schedule(sc, txq);
  506. unlock:
  507. spin_unlock_bh(&txq->axq_lock);
  508. }
  509. /* Compute the number of bad frames */
  510. static int ath_tx_num_badfrms(struct ath_softc *sc,
  511. struct ath_buf *bf, int txok)
  512. {
  513. struct ath_node *an = bf->bf_node;
  514. int isnodegone = (an->an_flags & ATH_NODE_CLEAN);
  515. struct ath_buf *bf_last = bf->bf_lastbf;
  516. struct ath_desc *ds = bf_last->bf_desc;
  517. u16 seq_st = 0;
  518. u32 ba[WME_BA_BMP_SIZE >> 5];
  519. int ba_index;
  520. int nbad = 0;
  521. int isaggr = 0;
  522. if (isnodegone || ds->ds_txstat.ts_flags == ATH9K_TX_SW_ABORTED)
  523. return 0;
  524. isaggr = bf->bf_isaggr;
  525. if (isaggr) {
  526. seq_st = ATH_DS_BA_SEQ(ds);
  527. memcpy(ba, ATH_DS_BA_BITMAP(ds), WME_BA_BMP_SIZE >> 3);
  528. }
  529. while (bf) {
  530. ba_index = ATH_BA_INDEX(seq_st, bf->bf_seqno);
  531. if (!txok || (isaggr && !ATH_BA_ISSET(ba, ba_index)))
  532. nbad++;
  533. bf = bf->bf_next;
  534. }
  535. return nbad;
  536. }
  537. static void ath_tx_set_retry(struct ath_softc *sc, struct ath_buf *bf)
  538. {
  539. struct sk_buff *skb;
  540. struct ieee80211_hdr *hdr;
  541. bf->bf_isretried = 1;
  542. bf->bf_retries++;
  543. skb = bf->bf_mpdu;
  544. hdr = (struct ieee80211_hdr *)skb->data;
  545. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
  546. }
  547. /* Update block ack window */
  548. static void ath_tx_update_baw(struct ath_softc *sc,
  549. struct ath_atx_tid *tid, int seqno)
  550. {
  551. int index, cindex;
  552. index = ATH_BA_INDEX(tid->seq_start, seqno);
  553. cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
  554. tid->tx_buf[cindex] = NULL;
  555. while (tid->baw_head != tid->baw_tail && !tid->tx_buf[tid->baw_head]) {
  556. INCR(tid->seq_start, IEEE80211_SEQ_MAX);
  557. INCR(tid->baw_head, ATH_TID_MAX_BUFS);
  558. }
  559. }
  560. /*
  561. * ath_pkt_dur - compute packet duration (NB: not NAV)
  562. *
  563. * rix - rate index
  564. * pktlen - total bytes (delims + data + fcs + pads + pad delims)
  565. * width - 0 for 20 MHz, 1 for 40 MHz
  566. * half_gi - to use 4us v/s 3.6 us for symbol time
  567. */
  568. static u32 ath_pkt_duration(struct ath_softc *sc,
  569. u8 rix,
  570. struct ath_buf *bf,
  571. int width,
  572. int half_gi,
  573. bool shortPreamble)
  574. {
  575. const struct ath9k_rate_table *rt = sc->sc_currates;
  576. u32 nbits, nsymbits, duration, nsymbols;
  577. u8 rc;
  578. int streams, pktlen;
  579. pktlen = bf->bf_isaggr ? bf->bf_al : bf->bf_frmlen;
  580. rc = rt->info[rix].rateCode;
  581. /*
  582. * for legacy rates, use old function to compute packet duration
  583. */
  584. if (!IS_HT_RATE(rc))
  585. return ath9k_hw_computetxtime(sc->sc_ah,
  586. rt,
  587. pktlen,
  588. rix,
  589. shortPreamble);
  590. /*
  591. * find number of symbols: PLCP + data
  592. */
  593. nbits = (pktlen << 3) + OFDM_PLCP_BITS;
  594. nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
  595. nsymbols = (nbits + nsymbits - 1) / nsymbits;
  596. if (!half_gi)
  597. duration = SYMBOL_TIME(nsymbols);
  598. else
  599. duration = SYMBOL_TIME_HALFGI(nsymbols);
  600. /*
  601. * addup duration for legacy/ht training and signal fields
  602. */
  603. streams = HT_RC_2_STREAMS(rc);
  604. duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
  605. return duration;
  606. }
  607. /* Rate module function to set rate related fields in tx descriptor */
  608. static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf)
  609. {
  610. struct ath_hal *ah = sc->sc_ah;
  611. const struct ath9k_rate_table *rt;
  612. struct ath_desc *ds = bf->bf_desc;
  613. struct ath_desc *lastds = bf->bf_lastbf->bf_desc;
  614. struct ath9k_11n_rate_series series[4];
  615. int i, flags, rtsctsena = 0, dynamic_mimops = 0;
  616. u32 ctsduration = 0;
  617. u8 rix = 0, cix, ctsrate = 0;
  618. u32 aggr_limit_with_rts = sc->sc_rtsaggrlimit;
  619. struct ath_node *an = (struct ath_node *) bf->bf_node;
  620. /*
  621. * get the cix for the lowest valid rix.
  622. */
  623. rt = sc->sc_currates;
  624. for (i = 4; i--;) {
  625. if (bf->bf_rcs[i].tries) {
  626. rix = bf->bf_rcs[i].rix;
  627. break;
  628. }
  629. }
  630. flags = (bf->bf_flags & (ATH9K_TXDESC_RTSENA | ATH9K_TXDESC_CTSENA));
  631. cix = rt->info[rix].controlRate;
  632. /*
  633. * If 802.11g protection is enabled, determine whether
  634. * to use RTS/CTS or just CTS. Note that this is only
  635. * done for OFDM/HT unicast frames.
  636. */
  637. if (sc->sc_protmode != PROT_M_NONE &&
  638. (rt->info[rix].phy == PHY_OFDM ||
  639. rt->info[rix].phy == PHY_HT) &&
  640. (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
  641. if (sc->sc_protmode == PROT_M_RTSCTS)
  642. flags = ATH9K_TXDESC_RTSENA;
  643. else if (sc->sc_protmode == PROT_M_CTSONLY)
  644. flags = ATH9K_TXDESC_CTSENA;
  645. cix = rt->info[sc->sc_protrix].controlRate;
  646. rtsctsena = 1;
  647. }
  648. /* For 11n, the default behavior is to enable RTS for
  649. * hw retried frames. We enable the global flag here and
  650. * let rate series flags determine which rates will actually
  651. * use RTS.
  652. */
  653. if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) && bf->bf_isdata) {
  654. BUG_ON(!an);
  655. /*
  656. * 802.11g protection not needed, use our default behavior
  657. */
  658. if (!rtsctsena)
  659. flags = ATH9K_TXDESC_RTSENA;
  660. /*
  661. * For dynamic MIMO PS, RTS needs to precede the first aggregate
  662. * and the second aggregate should have any protection at all.
  663. */
  664. if (an->an_smmode == ATH_SM_PWRSAV_DYNAMIC) {
  665. if (!bf->bf_aggrburst) {
  666. flags = ATH9K_TXDESC_RTSENA;
  667. dynamic_mimops = 1;
  668. } else {
  669. flags = 0;
  670. }
  671. }
  672. }
  673. /*
  674. * Set protection if aggregate protection on
  675. */
  676. if (sc->sc_config.ath_aggr_prot &&
  677. (!bf->bf_isaggr || (bf->bf_isaggr && bf->bf_al < 8192))) {
  678. flags = ATH9K_TXDESC_RTSENA;
  679. cix = rt->info[sc->sc_protrix].controlRate;
  680. rtsctsena = 1;
  681. }
  682. /*
  683. * For AR5416 - RTS cannot be followed by a frame larger than 8K.
  684. */
  685. if (bf->bf_isaggr && (bf->bf_al > aggr_limit_with_rts)) {
  686. /*
  687. * Ensure that in the case of SM Dynamic power save
  688. * while we are bursting the second aggregate the
  689. * RTS is cleared.
  690. */
  691. flags &= ~(ATH9K_TXDESC_RTSENA);
  692. }
  693. /*
  694. * CTS transmit rate is derived from the transmit rate
  695. * by looking in the h/w rate table. We must also factor
  696. * in whether or not a short preamble is to be used.
  697. */
  698. /* NB: cix is set above where RTS/CTS is enabled */
  699. BUG_ON(cix == 0xff);
  700. ctsrate = rt->info[cix].rateCode |
  701. (bf->bf_shpreamble ? rt->info[cix].shortPreamble : 0);
  702. /*
  703. * Setup HAL rate series
  704. */
  705. memzero(series, sizeof(struct ath9k_11n_rate_series) * 4);
  706. for (i = 0; i < 4; i++) {
  707. if (!bf->bf_rcs[i].tries)
  708. continue;
  709. rix = bf->bf_rcs[i].rix;
  710. series[i].Rate = rt->info[rix].rateCode |
  711. (bf->bf_shpreamble ? rt->info[rix].shortPreamble : 0);
  712. series[i].Tries = bf->bf_rcs[i].tries;
  713. series[i].RateFlags = (
  714. (bf->bf_rcs[i].flags & ATH_RC_RTSCTS_FLAG) ?
  715. ATH9K_RATESERIES_RTS_CTS : 0) |
  716. ((bf->bf_rcs[i].flags & ATH_RC_CW40_FLAG) ?
  717. ATH9K_RATESERIES_2040 : 0) |
  718. ((bf->bf_rcs[i].flags & ATH_RC_SGI_FLAG) ?
  719. ATH9K_RATESERIES_HALFGI : 0);
  720. series[i].PktDuration = ath_pkt_duration(
  721. sc, rix, bf,
  722. (bf->bf_rcs[i].flags & ATH_RC_CW40_FLAG) != 0,
  723. (bf->bf_rcs[i].flags & ATH_RC_SGI_FLAG),
  724. bf->bf_shpreamble);
  725. if ((an->an_smmode == ATH_SM_PWRSAV_STATIC) &&
  726. (bf->bf_rcs[i].flags & ATH_RC_DS_FLAG) == 0) {
  727. /*
  728. * When sending to an HT node that has enabled static
  729. * SM/MIMO power save, send at single stream rates but
  730. * use maximum allowed transmit chains per user,
  731. * hardware, regulatory, or country limits for
  732. * better range.
  733. */
  734. series[i].ChSel = sc->sc_tx_chainmask;
  735. } else {
  736. if (bf->bf_ht)
  737. series[i].ChSel =
  738. ath_chainmask_sel_logic(sc, an);
  739. else
  740. series[i].ChSel = sc->sc_tx_chainmask;
  741. }
  742. if (rtsctsena)
  743. series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
  744. /*
  745. * Set RTS for all rates if node is in dynamic powersave
  746. * mode and we are using dual stream rates.
  747. */
  748. if (dynamic_mimops && (bf->bf_rcs[i].flags & ATH_RC_DS_FLAG))
  749. series[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
  750. }
  751. /*
  752. * For non-HT devices, calculate RTS/CTS duration in software
  753. * and disable multi-rate retry.
  754. */
  755. if (flags && !(ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)) {
  756. /*
  757. * Compute the transmit duration based on the frame
  758. * size and the size of an ACK frame. We call into the
  759. * HAL to do the computation since it depends on the
  760. * characteristics of the actual PHY being used.
  761. *
  762. * NB: CTS is assumed the same size as an ACK so we can
  763. * use the precalculated ACK durations.
  764. */
  765. if (flags & ATH9K_TXDESC_RTSENA) { /* SIFS + CTS */
  766. ctsduration += bf->bf_shpreamble ?
  767. rt->info[cix].spAckDuration :
  768. rt->info[cix].lpAckDuration;
  769. }
  770. ctsduration += series[0].PktDuration;
  771. if ((bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) { /* SIFS + ACK */
  772. ctsduration += bf->bf_shpreamble ?
  773. rt->info[rix].spAckDuration :
  774. rt->info[rix].lpAckDuration;
  775. }
  776. /*
  777. * Disable multi-rate retry when using RTS/CTS by clearing
  778. * series 1, 2 and 3.
  779. */
  780. memzero(&series[1], sizeof(struct ath9k_11n_rate_series) * 3);
  781. }
  782. /*
  783. * set dur_update_en for l-sig computation except for PS-Poll frames
  784. */
  785. ath9k_hw_set11n_ratescenario(ah, ds, lastds,
  786. !bf->bf_ispspoll,
  787. ctsrate,
  788. ctsduration,
  789. series, 4, flags);
  790. if (sc->sc_config.ath_aggr_prot && flags)
  791. ath9k_hw_set11n_burstduration(ah, ds, 8192);
  792. }
  793. /*
  794. * Function to send a normal HT (non-AMPDU) frame
  795. * NB: must be called with txq lock held
  796. */
  797. static int ath_tx_send_normal(struct ath_softc *sc,
  798. struct ath_txq *txq,
  799. struct ath_atx_tid *tid,
  800. struct list_head *bf_head)
  801. {
  802. struct ath_buf *bf;
  803. struct sk_buff *skb;
  804. struct ieee80211_tx_info *tx_info;
  805. struct ath_tx_info_priv *tx_info_priv;
  806. BUG_ON(list_empty(bf_head));
  807. bf = list_first_entry(bf_head, struct ath_buf, list);
  808. bf->bf_isampdu = 0; /* regular HT frame */
  809. skb = (struct sk_buff *)bf->bf_mpdu;
  810. tx_info = IEEE80211_SKB_CB(skb);
  811. tx_info_priv = (struct ath_tx_info_priv *)tx_info->driver_data[0];
  812. memcpy(bf->bf_rcs, tx_info_priv->rcs, 4 * sizeof(tx_info_priv->rcs[0]));
  813. /* update starting sequence number for subsequent ADDBA request */
  814. INCR(tid->seq_start, IEEE80211_SEQ_MAX);
  815. /* Queue to h/w without aggregation */
  816. bf->bf_nframes = 1;
  817. bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
  818. ath_buf_set_rate(sc, bf);
  819. ath_tx_txqaddbuf(sc, txq, bf_head);
  820. return 0;
  821. }
  822. /* flush tid's software queue and send frames as non-ampdu's */
  823. static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
  824. {
  825. struct ath_txq *txq = &sc->sc_txq[tid->ac->qnum];
  826. struct ath_buf *bf;
  827. struct list_head bf_head;
  828. INIT_LIST_HEAD(&bf_head);
  829. ASSERT(tid->paused > 0);
  830. spin_lock_bh(&txq->axq_lock);
  831. tid->paused--;
  832. if (tid->paused > 0) {
  833. spin_unlock_bh(&txq->axq_lock);
  834. return;
  835. }
  836. while (!list_empty(&tid->buf_q)) {
  837. bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
  838. ASSERT(!bf->bf_isretried);
  839. list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
  840. ath_tx_send_normal(sc, txq, tid, &bf_head);
  841. }
  842. spin_unlock_bh(&txq->axq_lock);
  843. }
  844. /* Completion routine of an aggregate */
  845. static void ath_tx_complete_aggr_rifs(struct ath_softc *sc,
  846. struct ath_txq *txq,
  847. struct ath_buf *bf,
  848. struct list_head *bf_q,
  849. int txok)
  850. {
  851. struct ath_node *an = bf->bf_node;
  852. struct ath_atx_tid *tid = ATH_AN_2_TID(an, bf->bf_tidno);
  853. struct ath_buf *bf_last = bf->bf_lastbf;
  854. struct ath_desc *ds = bf_last->bf_desc;
  855. struct ath_buf *bf_next, *bf_lastq = NULL;
  856. struct list_head bf_head, bf_pending;
  857. u16 seq_st = 0;
  858. u32 ba[WME_BA_BMP_SIZE >> 5];
  859. int isaggr, txfail, txpending, sendbar = 0, needreset = 0;
  860. int isnodegone = (an->an_flags & ATH_NODE_CLEAN);
  861. isaggr = bf->bf_isaggr;
  862. if (isaggr) {
  863. if (txok) {
  864. if (ATH_DS_TX_BA(ds)) {
  865. /*
  866. * extract starting sequence and
  867. * block-ack bitmap
  868. */
  869. seq_st = ATH_DS_BA_SEQ(ds);
  870. memcpy(ba,
  871. ATH_DS_BA_BITMAP(ds),
  872. WME_BA_BMP_SIZE >> 3);
  873. } else {
  874. memzero(ba, WME_BA_BMP_SIZE >> 3);
  875. /*
  876. * AR5416 can become deaf/mute when BA
  877. * issue happens. Chip needs to be reset.
  878. * But AP code may have sychronization issues
  879. * when perform internal reset in this routine.
  880. * Only enable reset in STA mode for now.
  881. */
  882. if (sc->sc_opmode == ATH9K_M_STA)
  883. needreset = 1;
  884. }
  885. } else {
  886. memzero(ba, WME_BA_BMP_SIZE >> 3);
  887. }
  888. }
  889. INIT_LIST_HEAD(&bf_pending);
  890. INIT_LIST_HEAD(&bf_head);
  891. while (bf) {
  892. txfail = txpending = 0;
  893. bf_next = bf->bf_next;
  894. if (ATH_BA_ISSET(ba, ATH_BA_INDEX(seq_st, bf->bf_seqno))) {
  895. /* transmit completion, subframe is
  896. * acked by block ack */
  897. } else if (!isaggr && txok) {
  898. /* transmit completion */
  899. } else {
  900. if (!tid->cleanup_inprogress && !isnodegone &&
  901. ds->ds_txstat.ts_flags != ATH9K_TX_SW_ABORTED) {
  902. if (bf->bf_retries < ATH_MAX_SW_RETRIES) {
  903. ath_tx_set_retry(sc, bf);
  904. txpending = 1;
  905. } else {
  906. bf->bf_isxretried = 1;
  907. txfail = 1;
  908. sendbar = 1;
  909. }
  910. } else {
  911. /*
  912. * cleanup in progress, just fail
  913. * the un-acked sub-frames
  914. */
  915. txfail = 1;
  916. }
  917. }
  918. /*
  919. * Remove ath_buf's of this sub-frame from aggregate queue.
  920. */
  921. if (bf_next == NULL) { /* last subframe in the aggregate */
  922. ASSERT(bf->bf_lastfrm == bf_last);
  923. /*
  924. * The last descriptor of the last sub frame could be
  925. * a holding descriptor for h/w. If that's the case,
  926. * bf->bf_lastfrm won't be in the bf_q.
  927. * Make sure we handle bf_q properly here.
  928. */
  929. if (!list_empty(bf_q)) {
  930. bf_lastq = list_entry(bf_q->prev,
  931. struct ath_buf, list);
  932. list_cut_position(&bf_head,
  933. bf_q, &bf_lastq->list);
  934. } else {
  935. /*
  936. * XXX: if the last subframe only has one
  937. * descriptor which is also being used as
  938. * a holding descriptor. Then the ath_buf
  939. * is not in the bf_q at all.
  940. */
  941. INIT_LIST_HEAD(&bf_head);
  942. }
  943. } else {
  944. ASSERT(!list_empty(bf_q));
  945. list_cut_position(&bf_head,
  946. bf_q, &bf->bf_lastfrm->list);
  947. }
  948. if (!txpending) {
  949. /*
  950. * complete the acked-ones/xretried ones; update
  951. * block-ack window
  952. */
  953. spin_lock_bh(&txq->axq_lock);
  954. ath_tx_update_baw(sc, tid, bf->bf_seqno);
  955. spin_unlock_bh(&txq->axq_lock);
  956. /* complete this sub-frame */
  957. ath_tx_complete_buf(sc, bf, &bf_head, !txfail, sendbar);
  958. } else {
  959. /*
  960. * retry the un-acked ones
  961. */
  962. /*
  963. * XXX: if the last descriptor is holding descriptor,
  964. * in order to requeue the frame to software queue, we
  965. * need to allocate a new descriptor and
  966. * copy the content of holding descriptor to it.
  967. */
  968. if (bf->bf_next == NULL &&
  969. bf_last->bf_status & ATH_BUFSTATUS_STALE) {
  970. struct ath_buf *tbf;
  971. /* allocate new descriptor */
  972. spin_lock_bh(&sc->sc_txbuflock);
  973. ASSERT(!list_empty((&sc->sc_txbuf)));
  974. tbf = list_first_entry(&sc->sc_txbuf,
  975. struct ath_buf, list);
  976. list_del(&tbf->list);
  977. spin_unlock_bh(&sc->sc_txbuflock);
  978. ATH_TXBUF_RESET(tbf);
  979. /* copy descriptor content */
  980. tbf->bf_mpdu = bf_last->bf_mpdu;
  981. tbf->bf_node = bf_last->bf_node;
  982. tbf->bf_buf_addr = bf_last->bf_buf_addr;
  983. *(tbf->bf_desc) = *(bf_last->bf_desc);
  984. /* link it to the frame */
  985. if (bf_lastq) {
  986. bf_lastq->bf_desc->ds_link =
  987. tbf->bf_daddr;
  988. bf->bf_lastfrm = tbf;
  989. ath9k_hw_cleartxdesc(sc->sc_ah,
  990. bf->bf_lastfrm->bf_desc);
  991. } else {
  992. tbf->bf_state = bf_last->bf_state;
  993. tbf->bf_lastfrm = tbf;
  994. ath9k_hw_cleartxdesc(sc->sc_ah,
  995. tbf->bf_lastfrm->bf_desc);
  996. /* copy the DMA context */
  997. copy_dma_mem_context(
  998. get_dma_mem_context(tbf,
  999. bf_dmacontext),
  1000. get_dma_mem_context(bf_last,
  1001. bf_dmacontext));
  1002. }
  1003. list_add_tail(&tbf->list, &bf_head);
  1004. } else {
  1005. /*
  1006. * Clear descriptor status words for
  1007. * software retry
  1008. */
  1009. ath9k_hw_cleartxdesc(sc->sc_ah,
  1010. bf->bf_lastfrm->bf_desc);
  1011. }
  1012. /*
  1013. * Put this buffer to the temporary pending
  1014. * queue to retain ordering
  1015. */
  1016. list_splice_tail_init(&bf_head, &bf_pending);
  1017. }
  1018. bf = bf_next;
  1019. }
  1020. /*
  1021. * node is already gone. no more assocication
  1022. * with the node. the node might have been freed
  1023. * any node acces can result in panic.note tid
  1024. * is part of the node.
  1025. */
  1026. if (isnodegone)
  1027. return;
  1028. if (tid->cleanup_inprogress) {
  1029. /* check to see if we're done with cleaning the h/w queue */
  1030. spin_lock_bh(&txq->axq_lock);
  1031. if (tid->baw_head == tid->baw_tail) {
  1032. tid->addba_exchangecomplete = 0;
  1033. tid->addba_exchangeattempts = 0;
  1034. spin_unlock_bh(&txq->axq_lock);
  1035. tid->cleanup_inprogress = false;
  1036. /* send buffered frames as singles */
  1037. ath_tx_flush_tid(sc, tid);
  1038. } else
  1039. spin_unlock_bh(&txq->axq_lock);
  1040. return;
  1041. }
  1042. /*
  1043. * prepend un-acked frames to the beginning of the pending frame queue
  1044. */
  1045. if (!list_empty(&bf_pending)) {
  1046. spin_lock_bh(&txq->axq_lock);
  1047. /* Note: we _prepend_, we _do_not_ at to
  1048. * the end of the queue ! */
  1049. list_splice(&bf_pending, &tid->buf_q);
  1050. ath_tx_queue_tid(txq, tid);
  1051. spin_unlock_bh(&txq->axq_lock);
  1052. }
  1053. if (needreset)
  1054. ath_internal_reset(sc);
  1055. return;
  1056. }
  1057. /* Process completed xmit descriptors from the specified queue */
  1058. static int ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
  1059. {
  1060. struct ath_hal *ah = sc->sc_ah;
  1061. struct ath_buf *bf, *lastbf, *bf_held = NULL;
  1062. struct list_head bf_head;
  1063. struct ath_desc *ds, *tmp_ds;
  1064. struct sk_buff *skb;
  1065. struct ieee80211_tx_info *tx_info;
  1066. struct ath_tx_info_priv *tx_info_priv;
  1067. int nacked, txok, nbad = 0, isrifs = 0;
  1068. int status;
  1069. DPRINTF(sc, ATH_DBG_QUEUE,
  1070. "%s: tx queue %d (%x), link %p\n", __func__,
  1071. txq->axq_qnum, ath9k_hw_gettxbuf(sc->sc_ah, txq->axq_qnum),
  1072. txq->axq_link);
  1073. nacked = 0;
  1074. for (;;) {
  1075. spin_lock_bh(&txq->axq_lock);
  1076. txq->axq_intrcnt = 0; /* reset periodic desc intr count */
  1077. if (list_empty(&txq->axq_q)) {
  1078. txq->axq_link = NULL;
  1079. txq->axq_linkbuf = NULL;
  1080. spin_unlock_bh(&txq->axq_lock);
  1081. break;
  1082. }
  1083. bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
  1084. /*
  1085. * There is a race condition that a BH gets scheduled
  1086. * after sw writes TxE and before hw re-load the last
  1087. * descriptor to get the newly chained one.
  1088. * Software must keep the last DONE descriptor as a
  1089. * holding descriptor - software does so by marking
  1090. * it with the STALE flag.
  1091. */
  1092. bf_held = NULL;
  1093. if (bf->bf_status & ATH_BUFSTATUS_STALE) {
  1094. bf_held = bf;
  1095. if (list_is_last(&bf_held->list, &txq->axq_q)) {
  1096. /* FIXME:
  1097. * The holding descriptor is the last
  1098. * descriptor in queue. It's safe to remove
  1099. * the last holding descriptor in BH context.
  1100. */
  1101. spin_unlock_bh(&txq->axq_lock);
  1102. break;
  1103. } else {
  1104. /* Lets work with the next buffer now */
  1105. bf = list_entry(bf_held->list.next,
  1106. struct ath_buf, list);
  1107. }
  1108. }
  1109. lastbf = bf->bf_lastbf;
  1110. ds = lastbf->bf_desc; /* NB: last decriptor */
  1111. status = ath9k_hw_txprocdesc(ah, ds);
  1112. if (status == -EINPROGRESS) {
  1113. spin_unlock_bh(&txq->axq_lock);
  1114. break;
  1115. }
  1116. if (bf->bf_desc == txq->axq_lastdsWithCTS)
  1117. txq->axq_lastdsWithCTS = NULL;
  1118. if (ds == txq->axq_gatingds)
  1119. txq->axq_gatingds = NULL;
  1120. /*
  1121. * Remove ath_buf's of the same transmit unit from txq,
  1122. * however leave the last descriptor back as the holding
  1123. * descriptor for hw.
  1124. */
  1125. lastbf->bf_status |= ATH_BUFSTATUS_STALE;
  1126. INIT_LIST_HEAD(&bf_head);
  1127. if (!list_is_singular(&lastbf->list))
  1128. list_cut_position(&bf_head,
  1129. &txq->axq_q, lastbf->list.prev);
  1130. txq->axq_depth--;
  1131. if (bf->bf_isaggr)
  1132. txq->axq_aggr_depth--;
  1133. txok = (ds->ds_txstat.ts_status == 0);
  1134. spin_unlock_bh(&txq->axq_lock);
  1135. if (bf_held) {
  1136. list_del(&bf_held->list);
  1137. spin_lock_bh(&sc->sc_txbuflock);
  1138. list_add_tail(&bf_held->list, &sc->sc_txbuf);
  1139. spin_unlock_bh(&sc->sc_txbuflock);
  1140. }
  1141. if (!bf->bf_isampdu) {
  1142. /*
  1143. * This frame is sent out as a single frame.
  1144. * Use hardware retry status for this frame.
  1145. */
  1146. bf->bf_retries = ds->ds_txstat.ts_longretry;
  1147. if (ds->ds_txstat.ts_status & ATH9K_TXERR_XRETRY)
  1148. bf->bf_isxretried = 1;
  1149. nbad = 0;
  1150. } else {
  1151. nbad = ath_tx_num_badfrms(sc, bf, txok);
  1152. }
  1153. skb = bf->bf_mpdu;
  1154. tx_info = IEEE80211_SKB_CB(skb);
  1155. tx_info_priv = (struct ath_tx_info_priv *)
  1156. tx_info->driver_data[0];
  1157. if (ds->ds_txstat.ts_status & ATH9K_TXERR_FILT)
  1158. tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1159. if ((ds->ds_txstat.ts_status & ATH9K_TXERR_FILT) == 0 &&
  1160. (bf->bf_flags & ATH9K_TXDESC_NOACK) == 0) {
  1161. if (ds->ds_txstat.ts_status == 0)
  1162. nacked++;
  1163. if (bf->bf_isdata) {
  1164. if (isrifs)
  1165. tmp_ds = bf->bf_rifslast->bf_desc;
  1166. else
  1167. tmp_ds = ds;
  1168. memcpy(&tx_info_priv->tx,
  1169. &tmp_ds->ds_txstat,
  1170. sizeof(tx_info_priv->tx));
  1171. tx_info_priv->n_frames = bf->bf_nframes;
  1172. tx_info_priv->n_bad_frames = nbad;
  1173. }
  1174. }
  1175. /*
  1176. * Complete this transmit unit
  1177. */
  1178. if (bf->bf_isampdu)
  1179. ath_tx_complete_aggr_rifs(sc, txq, bf, &bf_head, txok);
  1180. else
  1181. ath_tx_complete_buf(sc, bf, &bf_head, txok, 0);
  1182. /* Wake up mac80211 queue */
  1183. spin_lock_bh(&txq->axq_lock);
  1184. if (txq->stopped && ath_txq_depth(sc, txq->axq_qnum) <=
  1185. (ATH_TXBUF - 20)) {
  1186. int qnum;
  1187. qnum = ath_get_mac80211_qnum(txq->axq_qnum, sc);
  1188. if (qnum != -1) {
  1189. ieee80211_wake_queue(sc->hw, qnum);
  1190. txq->stopped = 0;
  1191. }
  1192. }
  1193. /*
  1194. * schedule any pending packets if aggregation is enabled
  1195. */
  1196. if (sc->sc_txaggr)
  1197. ath_txq_schedule(sc, txq);
  1198. spin_unlock_bh(&txq->axq_lock);
  1199. }
  1200. return nacked;
  1201. }
  1202. static void ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
  1203. {
  1204. struct ath_hal *ah = sc->sc_ah;
  1205. (void) ath9k_hw_stoptxdma(ah, txq->axq_qnum);
  1206. DPRINTF(sc, ATH_DBG_XMIT, "%s: tx queue [%u] %x, link %p\n",
  1207. __func__, txq->axq_qnum,
  1208. ath9k_hw_gettxbuf(ah, txq->axq_qnum), txq->axq_link);
  1209. }
  1210. /* Drain only the data queues */
  1211. static void ath_drain_txdataq(struct ath_softc *sc, bool retry_tx)
  1212. {
  1213. struct ath_hal *ah = sc->sc_ah;
  1214. int i;
  1215. int npend = 0;
  1216. enum ath9k_ht_macmode ht_macmode = ath_cwm_macmode(sc);
  1217. /* XXX return value */
  1218. if (!sc->sc_invalid) {
  1219. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1220. if (ATH_TXQ_SETUP(sc, i)) {
  1221. ath_tx_stopdma(sc, &sc->sc_txq[i]);
  1222. /* The TxDMA may not really be stopped.
  1223. * Double check the hal tx pending count */
  1224. npend += ath9k_hw_numtxpending(ah,
  1225. sc->sc_txq[i].axq_qnum);
  1226. }
  1227. }
  1228. }
  1229. if (npend) {
  1230. int status;
  1231. /* TxDMA not stopped, reset the hal */
  1232. DPRINTF(sc, ATH_DBG_XMIT,
  1233. "%s: Unable to stop TxDMA. Reset HAL!\n", __func__);
  1234. spin_lock_bh(&sc->sc_resetlock);
  1235. if (!ath9k_hw_reset(ah, sc->sc_opmode,
  1236. &sc->sc_curchan, ht_macmode,
  1237. sc->sc_tx_chainmask, sc->sc_rx_chainmask,
  1238. sc->sc_ht_extprotspacing, true, &status)) {
  1239. DPRINTF(sc, ATH_DBG_FATAL,
  1240. "%s: unable to reset hardware; hal status %u\n",
  1241. __func__,
  1242. status);
  1243. }
  1244. spin_unlock_bh(&sc->sc_resetlock);
  1245. }
  1246. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1247. if (ATH_TXQ_SETUP(sc, i))
  1248. ath_tx_draintxq(sc, &sc->sc_txq[i], retry_tx);
  1249. }
  1250. }
  1251. /* Add a sub-frame to block ack window */
  1252. static void ath_tx_addto_baw(struct ath_softc *sc,
  1253. struct ath_atx_tid *tid,
  1254. struct ath_buf *bf)
  1255. {
  1256. int index, cindex;
  1257. if (bf->bf_isretried)
  1258. return;
  1259. index = ATH_BA_INDEX(tid->seq_start, bf->bf_seqno);
  1260. cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
  1261. ASSERT(tid->tx_buf[cindex] == NULL);
  1262. tid->tx_buf[cindex] = bf;
  1263. if (index >= ((tid->baw_tail - tid->baw_head) &
  1264. (ATH_TID_MAX_BUFS - 1))) {
  1265. tid->baw_tail = cindex;
  1266. INCR(tid->baw_tail, ATH_TID_MAX_BUFS);
  1267. }
  1268. }
  1269. /*
  1270. * Function to send an A-MPDU
  1271. * NB: must be called with txq lock held
  1272. */
  1273. static int ath_tx_send_ampdu(struct ath_softc *sc,
  1274. struct ath_txq *txq,
  1275. struct ath_atx_tid *tid,
  1276. struct list_head *bf_head,
  1277. struct ath_tx_control *txctl)
  1278. {
  1279. struct ath_buf *bf;
  1280. struct sk_buff *skb;
  1281. struct ieee80211_tx_info *tx_info;
  1282. struct ath_tx_info_priv *tx_info_priv;
  1283. BUG_ON(list_empty(bf_head));
  1284. bf = list_first_entry(bf_head, struct ath_buf, list);
  1285. bf->bf_isampdu = 1;
  1286. bf->bf_seqno = txctl->seqno; /* save seqno and tidno in buffer */
  1287. bf->bf_tidno = txctl->tidno;
  1288. /*
  1289. * Do not queue to h/w when any of the following conditions is true:
  1290. * - there are pending frames in software queue
  1291. * - the TID is currently paused for ADDBA/BAR request
  1292. * - seqno is not within block-ack window
  1293. * - h/w queue depth exceeds low water mark
  1294. */
  1295. if (!list_empty(&tid->buf_q) || tid->paused ||
  1296. !BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno) ||
  1297. txq->axq_depth >= ATH_AGGR_MIN_QDEPTH) {
  1298. /*
  1299. * Add this frame to software queue for scheduling later
  1300. * for aggregation.
  1301. */
  1302. list_splice_tail_init(bf_head, &tid->buf_q);
  1303. ath_tx_queue_tid(txq, tid);
  1304. return 0;
  1305. }
  1306. skb = (struct sk_buff *)bf->bf_mpdu;
  1307. tx_info = IEEE80211_SKB_CB(skb);
  1308. tx_info_priv = (struct ath_tx_info_priv *)tx_info->driver_data[0];
  1309. memcpy(bf->bf_rcs, tx_info_priv->rcs, 4 * sizeof(tx_info_priv->rcs[0]));
  1310. /* Add sub-frame to BAW */
  1311. ath_tx_addto_baw(sc, tid, bf);
  1312. /* Queue to h/w without aggregation */
  1313. bf->bf_nframes = 1;
  1314. bf->bf_lastbf = bf->bf_lastfrm; /* one single frame */
  1315. ath_buf_set_rate(sc, bf);
  1316. ath_tx_txqaddbuf(sc, txq, bf_head);
  1317. return 0;
  1318. }
  1319. /*
  1320. * looks up the rate
  1321. * returns aggr limit based on lowest of the rates
  1322. */
  1323. static u32 ath_lookup_rate(struct ath_softc *sc,
  1324. struct ath_buf *bf)
  1325. {
  1326. const struct ath9k_rate_table *rt = sc->sc_currates;
  1327. struct sk_buff *skb;
  1328. struct ieee80211_tx_info *tx_info;
  1329. struct ath_tx_info_priv *tx_info_priv;
  1330. u32 max_4ms_framelen, frame_length;
  1331. u16 aggr_limit, legacy = 0, maxampdu;
  1332. int i;
  1333. skb = (struct sk_buff *)bf->bf_mpdu;
  1334. tx_info = IEEE80211_SKB_CB(skb);
  1335. tx_info_priv = (struct ath_tx_info_priv *)
  1336. tx_info->driver_data[0];
  1337. memcpy(bf->bf_rcs,
  1338. tx_info_priv->rcs, 4 * sizeof(tx_info_priv->rcs[0]));
  1339. /*
  1340. * Find the lowest frame length among the rate series that will have a
  1341. * 4ms transmit duration.
  1342. * TODO - TXOP limit needs to be considered.
  1343. */
  1344. max_4ms_framelen = ATH_AMPDU_LIMIT_MAX;
  1345. for (i = 0; i < 4; i++) {
  1346. if (bf->bf_rcs[i].tries) {
  1347. frame_length = bf->bf_rcs[i].max_4ms_framelen;
  1348. if (rt->info[bf->bf_rcs[i].rix].phy != PHY_HT) {
  1349. legacy = 1;
  1350. break;
  1351. }
  1352. max_4ms_framelen = min(max_4ms_framelen, frame_length);
  1353. }
  1354. }
  1355. /*
  1356. * limit aggregate size by the minimum rate if rate selected is
  1357. * not a probe rate, if rate selected is a probe rate then
  1358. * avoid aggregation of this packet.
  1359. */
  1360. if (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE || legacy)
  1361. return 0;
  1362. aggr_limit = min(max_4ms_framelen,
  1363. (u32)ATH_AMPDU_LIMIT_DEFAULT);
  1364. /*
  1365. * h/w can accept aggregates upto 16 bit lengths (65535).
  1366. * The IE, however can hold upto 65536, which shows up here
  1367. * as zero. Ignore 65536 since we are constrained by hw.
  1368. */
  1369. maxampdu = sc->sc_ht_info.maxampdu;
  1370. if (maxampdu)
  1371. aggr_limit = min(aggr_limit, maxampdu);
  1372. return aggr_limit;
  1373. }
  1374. /*
  1375. * returns the number of delimiters to be added to
  1376. * meet the minimum required mpdudensity.
  1377. * caller should make sure that the rate is HT rate .
  1378. */
  1379. static int ath_compute_num_delims(struct ath_softc *sc,
  1380. struct ath_buf *bf,
  1381. u16 frmlen)
  1382. {
  1383. const struct ath9k_rate_table *rt = sc->sc_currates;
  1384. u32 nsymbits, nsymbols, mpdudensity;
  1385. u16 minlen;
  1386. u8 rc, flags, rix;
  1387. int width, half_gi, ndelim, mindelim;
  1388. /* Select standard number of delimiters based on frame length alone */
  1389. ndelim = ATH_AGGR_GET_NDELIM(frmlen);
  1390. /*
  1391. * If encryption enabled, hardware requires some more padding between
  1392. * subframes.
  1393. * TODO - this could be improved to be dependent on the rate.
  1394. * The hardware can keep up at lower rates, but not higher rates
  1395. */
  1396. if (bf->bf_keytype != ATH9K_KEY_TYPE_CLEAR)
  1397. ndelim += ATH_AGGR_ENCRYPTDELIM;
  1398. /*
  1399. * Convert desired mpdu density from microeconds to bytes based
  1400. * on highest rate in rate series (i.e. first rate) to determine
  1401. * required minimum length for subframe. Take into account
  1402. * whether high rate is 20 or 40Mhz and half or full GI.
  1403. */
  1404. mpdudensity = sc->sc_ht_info.mpdudensity;
  1405. /*
  1406. * If there is no mpdu density restriction, no further calculation
  1407. * is needed.
  1408. */
  1409. if (mpdudensity == 0)
  1410. return ndelim;
  1411. rix = bf->bf_rcs[0].rix;
  1412. flags = bf->bf_rcs[0].flags;
  1413. rc = rt->info[rix].rateCode;
  1414. width = (flags & ATH_RC_CW40_FLAG) ? 1 : 0;
  1415. half_gi = (flags & ATH_RC_SGI_FLAG) ? 1 : 0;
  1416. if (half_gi)
  1417. nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
  1418. else
  1419. nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
  1420. if (nsymbols == 0)
  1421. nsymbols = 1;
  1422. nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
  1423. minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
  1424. /* Is frame shorter than required minimum length? */
  1425. if (frmlen < minlen) {
  1426. /* Get the minimum number of delimiters required. */
  1427. mindelim = (minlen - frmlen) / ATH_AGGR_DELIM_SZ;
  1428. ndelim = max(mindelim, ndelim);
  1429. }
  1430. return ndelim;
  1431. }
  1432. /*
  1433. * For aggregation from software buffer queue.
  1434. * NB: must be called with txq lock held
  1435. */
  1436. static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
  1437. struct ath_atx_tid *tid,
  1438. struct list_head *bf_q,
  1439. struct ath_buf **bf_last,
  1440. struct aggr_rifs_param *param,
  1441. int *prev_frames)
  1442. {
  1443. #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
  1444. struct ath_buf *bf, *tbf, *bf_first, *bf_prev = NULL;
  1445. struct list_head bf_head;
  1446. int rl = 0, nframes = 0, ndelim;
  1447. u16 aggr_limit = 0, al = 0, bpad = 0,
  1448. al_delta, h_baw = tid->baw_size / 2;
  1449. enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
  1450. int prev_al = 0, is_ds_rate = 0;
  1451. INIT_LIST_HEAD(&bf_head);
  1452. BUG_ON(list_empty(&tid->buf_q));
  1453. bf_first = list_first_entry(&tid->buf_q, struct ath_buf, list);
  1454. do {
  1455. bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
  1456. /*
  1457. * do not step over block-ack window
  1458. */
  1459. if (!BAW_WITHIN(tid->seq_start, tid->baw_size, bf->bf_seqno)) {
  1460. status = ATH_AGGR_BAW_CLOSED;
  1461. break;
  1462. }
  1463. if (!rl) {
  1464. aggr_limit = ath_lookup_rate(sc, bf);
  1465. rl = 1;
  1466. /*
  1467. * Is rate dual stream
  1468. */
  1469. is_ds_rate =
  1470. (bf->bf_rcs[0].flags & ATH_RC_DS_FLAG) ? 1 : 0;
  1471. }
  1472. /*
  1473. * do not exceed aggregation limit
  1474. */
  1475. al_delta = ATH_AGGR_DELIM_SZ + bf->bf_frmlen;
  1476. if (nframes && (aggr_limit <
  1477. (al + bpad + al_delta + prev_al))) {
  1478. status = ATH_AGGR_LIMITED;
  1479. break;
  1480. }
  1481. /*
  1482. * do not exceed subframe limit
  1483. */
  1484. if ((nframes + *prev_frames) >=
  1485. min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
  1486. status = ATH_AGGR_LIMITED;
  1487. break;
  1488. }
  1489. /*
  1490. * add padding for previous frame to aggregation length
  1491. */
  1492. al += bpad + al_delta;
  1493. /*
  1494. * Get the delimiters needed to meet the MPDU
  1495. * density for this node.
  1496. */
  1497. ndelim = ath_compute_num_delims(sc, bf_first, bf->bf_frmlen);
  1498. bpad = PADBYTES(al_delta) + (ndelim << 2);
  1499. bf->bf_next = NULL;
  1500. bf->bf_lastfrm->bf_desc->ds_link = 0;
  1501. /*
  1502. * this packet is part of an aggregate
  1503. * - remove all descriptors belonging to this frame from
  1504. * software queue
  1505. * - add it to block ack window
  1506. * - set up descriptors for aggregation
  1507. */
  1508. list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
  1509. ath_tx_addto_baw(sc, tid, bf);
  1510. list_for_each_entry(tbf, &bf_head, list) {
  1511. ath9k_hw_set11n_aggr_middle(sc->sc_ah,
  1512. tbf->bf_desc, ndelim);
  1513. }
  1514. /*
  1515. * link buffers of this frame to the aggregate
  1516. */
  1517. list_splice_tail_init(&bf_head, bf_q);
  1518. nframes++;
  1519. if (bf_prev) {
  1520. bf_prev->bf_next = bf;
  1521. bf_prev->bf_lastfrm->bf_desc->ds_link = bf->bf_daddr;
  1522. }
  1523. bf_prev = bf;
  1524. #ifdef AGGR_NOSHORT
  1525. /*
  1526. * terminate aggregation on a small packet boundary
  1527. */
  1528. if (bf->bf_frmlen < ATH_AGGR_MINPLEN) {
  1529. status = ATH_AGGR_SHORTPKT;
  1530. break;
  1531. }
  1532. #endif
  1533. } while (!list_empty(&tid->buf_q));
  1534. bf_first->bf_al = al;
  1535. bf_first->bf_nframes = nframes;
  1536. *bf_last = bf_prev;
  1537. return status;
  1538. #undef PADBYTES
  1539. }
  1540. /*
  1541. * process pending frames possibly doing a-mpdu aggregation
  1542. * NB: must be called with txq lock held
  1543. */
  1544. static void ath_tx_sched_aggr(struct ath_softc *sc,
  1545. struct ath_txq *txq, struct ath_atx_tid *tid)
  1546. {
  1547. struct ath_buf *bf, *tbf, *bf_last, *bf_lastaggr = NULL;
  1548. enum ATH_AGGR_STATUS status;
  1549. struct list_head bf_q;
  1550. struct aggr_rifs_param param = {0, 0, 0, 0, NULL};
  1551. int prev_frames = 0;
  1552. do {
  1553. if (list_empty(&tid->buf_q))
  1554. return;
  1555. INIT_LIST_HEAD(&bf_q);
  1556. status = ath_tx_form_aggr(sc, tid, &bf_q, &bf_lastaggr, &param,
  1557. &prev_frames);
  1558. /*
  1559. * no frames picked up to be aggregated; block-ack
  1560. * window is not open
  1561. */
  1562. if (list_empty(&bf_q))
  1563. break;
  1564. bf = list_first_entry(&bf_q, struct ath_buf, list);
  1565. bf_last = list_entry(bf_q.prev, struct ath_buf, list);
  1566. bf->bf_lastbf = bf_last;
  1567. /*
  1568. * if only one frame, send as non-aggregate
  1569. */
  1570. if (bf->bf_nframes == 1) {
  1571. ASSERT(bf->bf_lastfrm == bf_last);
  1572. bf->bf_isaggr = 0;
  1573. /*
  1574. * clear aggr bits for every descriptor
  1575. * XXX TODO: is there a way to optimize it?
  1576. */
  1577. list_for_each_entry(tbf, &bf_q, list) {
  1578. ath9k_hw_clr11n_aggr(sc->sc_ah, tbf->bf_desc);
  1579. }
  1580. ath_buf_set_rate(sc, bf);
  1581. ath_tx_txqaddbuf(sc, txq, &bf_q);
  1582. continue;
  1583. }
  1584. /*
  1585. * setup first desc with rate and aggr info
  1586. */
  1587. bf->bf_isaggr = 1;
  1588. ath_buf_set_rate(sc, bf);
  1589. ath9k_hw_set11n_aggr_first(sc->sc_ah, bf->bf_desc, bf->bf_al);
  1590. /*
  1591. * anchor last frame of aggregate correctly
  1592. */
  1593. ASSERT(bf_lastaggr);
  1594. ASSERT(bf_lastaggr->bf_lastfrm == bf_last);
  1595. tbf = bf_lastaggr;
  1596. ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
  1597. /* XXX: We don't enter into this loop, consider removing this */
  1598. while (!list_empty(&bf_q) && !list_is_last(&tbf->list, &bf_q)) {
  1599. tbf = list_entry(tbf->list.next, struct ath_buf, list);
  1600. ath9k_hw_set11n_aggr_last(sc->sc_ah, tbf->bf_desc);
  1601. }
  1602. txq->axq_aggr_depth++;
  1603. /*
  1604. * Normal aggregate, queue to hardware
  1605. */
  1606. ath_tx_txqaddbuf(sc, txq, &bf_q);
  1607. } while (txq->axq_depth < ATH_AGGR_MIN_QDEPTH &&
  1608. status != ATH_AGGR_BAW_CLOSED);
  1609. }
  1610. /* Called with txq lock held */
  1611. static void ath_tid_drain(struct ath_softc *sc,
  1612. struct ath_txq *txq,
  1613. struct ath_atx_tid *tid,
  1614. bool bh_flag)
  1615. {
  1616. struct ath_buf *bf;
  1617. struct list_head bf_head;
  1618. INIT_LIST_HEAD(&bf_head);
  1619. for (;;) {
  1620. if (list_empty(&tid->buf_q))
  1621. break;
  1622. bf = list_first_entry(&tid->buf_q, struct ath_buf, list);
  1623. list_cut_position(&bf_head, &tid->buf_q, &bf->bf_lastfrm->list);
  1624. /* update baw for software retried frame */
  1625. if (bf->bf_isretried)
  1626. ath_tx_update_baw(sc, tid, bf->bf_seqno);
  1627. /*
  1628. * do not indicate packets while holding txq spinlock.
  1629. * unlock is intentional here
  1630. */
  1631. if (likely(bh_flag))
  1632. spin_unlock_bh(&txq->axq_lock);
  1633. else
  1634. spin_unlock(&txq->axq_lock);
  1635. /* complete this sub-frame */
  1636. ath_tx_complete_buf(sc, bf, &bf_head, 0, 0);
  1637. if (likely(bh_flag))
  1638. spin_lock_bh(&txq->axq_lock);
  1639. else
  1640. spin_lock(&txq->axq_lock);
  1641. }
  1642. /*
  1643. * TODO: For frame(s) that are in the retry state, we will reuse the
  1644. * sequence number(s) without setting the retry bit. The
  1645. * alternative is to give up on these and BAR the receiver's window
  1646. * forward.
  1647. */
  1648. tid->seq_next = tid->seq_start;
  1649. tid->baw_tail = tid->baw_head;
  1650. }
  1651. /*
  1652. * Drain all pending buffers
  1653. * NB: must be called with txq lock held
  1654. */
  1655. static void ath_txq_drain_pending_buffers(struct ath_softc *sc,
  1656. struct ath_txq *txq,
  1657. bool bh_flag)
  1658. {
  1659. struct ath_atx_ac *ac, *ac_tmp;
  1660. struct ath_atx_tid *tid, *tid_tmp;
  1661. list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
  1662. list_del(&ac->list);
  1663. ac->sched = false;
  1664. list_for_each_entry_safe(tid, tid_tmp, &ac->tid_q, list) {
  1665. list_del(&tid->list);
  1666. tid->sched = false;
  1667. ath_tid_drain(sc, txq, tid, bh_flag);
  1668. }
  1669. }
  1670. }
  1671. static int ath_tx_start_dma(struct ath_softc *sc,
  1672. struct sk_buff *skb,
  1673. struct scatterlist *sg,
  1674. u32 n_sg,
  1675. struct ath_tx_control *txctl)
  1676. {
  1677. struct ath_node *an = txctl->an;
  1678. struct ath_buf *bf = NULL;
  1679. struct list_head bf_head;
  1680. struct ath_desc *ds;
  1681. struct ath_hal *ah = sc->sc_ah;
  1682. struct ath_txq *txq = &sc->sc_txq[txctl->qnum];
  1683. struct ath_tx_info_priv *tx_info_priv;
  1684. struct ath_rc_series *rcs;
  1685. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1686. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1687. __le16 fc = hdr->frame_control;
  1688. /* For each sglist entry, allocate an ath_buf for DMA */
  1689. INIT_LIST_HEAD(&bf_head);
  1690. spin_lock_bh(&sc->sc_txbuflock);
  1691. if (unlikely(list_empty(&sc->sc_txbuf))) {
  1692. spin_unlock_bh(&sc->sc_txbuflock);
  1693. return -ENOMEM;
  1694. }
  1695. bf = list_first_entry(&sc->sc_txbuf, struct ath_buf, list);
  1696. list_del(&bf->list);
  1697. spin_unlock_bh(&sc->sc_txbuflock);
  1698. list_add_tail(&bf->list, &bf_head);
  1699. /* set up this buffer */
  1700. ATH_TXBUF_RESET(bf);
  1701. bf->bf_frmlen = txctl->frmlen;
  1702. bf->bf_isdata = ieee80211_is_data(fc);
  1703. bf->bf_isbar = ieee80211_is_back_req(fc);
  1704. bf->bf_ispspoll = ieee80211_is_pspoll(fc);
  1705. bf->bf_flags = txctl->flags;
  1706. bf->bf_shpreamble = sc->sc_flags & ATH_PREAMBLE_SHORT;
  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. copy_dma_mem_context(get_dma_mem_context(bf, bf_dmacontext),
  1725. get_dma_mem_context(txctl, dmacontext));
  1726. /*
  1727. * Formulate first tx descriptor with tx controls.
  1728. */
  1729. ath9k_hw_set11n_txdesc(ah,
  1730. ds,
  1731. bf->bf_frmlen, /* frame length */
  1732. txctl->atype, /* Atheros packet type */
  1733. min(txctl->txpower, (u16)60), /* txpower */
  1734. txctl->keyix, /* key cache index */
  1735. txctl->keytype, /* key type */
  1736. txctl->flags); /* flags */
  1737. ath9k_hw_filltxdesc(ah,
  1738. ds,
  1739. sg_dma_len(sg), /* segment length */
  1740. true, /* first segment */
  1741. (n_sg == 1) ? true : false, /* last segment */
  1742. ds); /* first descriptor */
  1743. bf->bf_lastfrm = bf;
  1744. bf->bf_ht = txctl->ht;
  1745. spin_lock_bh(&txq->axq_lock);
  1746. if (txctl->ht && sc->sc_txaggr) {
  1747. struct ath_atx_tid *tid = ATH_AN_2_TID(an, txctl->tidno);
  1748. if (ath_aggr_query(sc, an, txctl->tidno)) {
  1749. /*
  1750. * Try aggregation if it's a unicast data frame
  1751. * and the destination is HT capable.
  1752. */
  1753. ath_tx_send_ampdu(sc, txq, tid, &bf_head, txctl);
  1754. } else {
  1755. /*
  1756. * Send this frame as regular when ADDBA exchange
  1757. * is neither complete nor pending.
  1758. */
  1759. ath_tx_send_normal(sc, txq, tid, &bf_head);
  1760. }
  1761. } else {
  1762. bf->bf_lastbf = bf;
  1763. bf->bf_nframes = 1;
  1764. ath_buf_set_rate(sc, bf);
  1765. if (ieee80211_is_back_req(fc)) {
  1766. /* This is required for resuming tid
  1767. * during BAR completion */
  1768. bf->bf_tidno = txctl->tidno;
  1769. }
  1770. if (is_multicast_ether_addr(hdr->addr1)) {
  1771. struct ath_vap *avp = sc->sc_vaps[txctl->if_id];
  1772. /*
  1773. * When servicing one or more stations in power-save
  1774. * mode (or) if there is some mcast data waiting on
  1775. * mcast queue (to prevent out of order delivery of
  1776. * mcast,bcast packets) multicast frames must be
  1777. * buffered until after the beacon. We use the private
  1778. * mcast queue for that.
  1779. */
  1780. /* XXX? more bit in 802.11 frame header */
  1781. spin_lock_bh(&avp->av_mcastq.axq_lock);
  1782. if (txctl->ps || avp->av_mcastq.axq_depth)
  1783. ath_tx_mcastqaddbuf(sc,
  1784. &avp->av_mcastq, &bf_head);
  1785. else
  1786. ath_tx_txqaddbuf(sc, txq, &bf_head);
  1787. spin_unlock_bh(&avp->av_mcastq.axq_lock);
  1788. } else
  1789. ath_tx_txqaddbuf(sc, txq, &bf_head);
  1790. }
  1791. spin_unlock_bh(&txq->axq_lock);
  1792. return 0;
  1793. }
  1794. static void xmit_map_sg(struct ath_softc *sc,
  1795. struct sk_buff *skb,
  1796. dma_addr_t *pa,
  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. *pa = pci_map_single(sc->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
  1803. /* setup S/G list */
  1804. memset(&sg, 0, sizeof(struct scatterlist));
  1805. sg_dma_address(&sg) = *pa;
  1806. sg_dma_len(&sg) = skb->len;
  1807. if (ath_tx_start_dma(sc, skb, &sg, 1, txctl) != 0) {
  1808. /*
  1809. * We have to do drop frame here.
  1810. */
  1811. pci_unmap_single(sc->pdev, *pa, skb->len, PCI_DMA_TODEVICE);
  1812. tx_status.retries = 0;
  1813. tx_status.flags = ATH_TX_ERROR;
  1814. if (txctl->ht && sc->sc_txaggr) {
  1815. /* Reclaim the seqno. */
  1816. tid = ATH_AN_2_TID((struct ath_node *)
  1817. txctl->an, txctl->tidno);
  1818. DECR(tid->seq_next, IEEE80211_SEQ_MAX);
  1819. }
  1820. ath_tx_complete(sc, skb, &tx_status, txctl->an);
  1821. }
  1822. }
  1823. /* Initialize TX queue and h/w */
  1824. int ath_tx_init(struct ath_softc *sc, int nbufs)
  1825. {
  1826. int error = 0;
  1827. do {
  1828. spin_lock_init(&sc->sc_txbuflock);
  1829. /* Setup tx descriptors */
  1830. error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
  1831. "tx", nbufs * ATH_FRAG_PER_MSDU, ATH_TXDESC);
  1832. if (error != 0) {
  1833. DPRINTF(sc, ATH_DBG_FATAL,
  1834. "%s: failed to allocate tx descriptors: %d\n",
  1835. __func__, error);
  1836. break;
  1837. }
  1838. /* XXX allocate beacon state together with vap */
  1839. error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
  1840. "beacon", ATH_BCBUF, 1);
  1841. if (error != 0) {
  1842. DPRINTF(sc, ATH_DBG_FATAL,
  1843. "%s: failed to allocate "
  1844. "beacon descripotrs: %d\n",
  1845. __func__, error);
  1846. break;
  1847. }
  1848. } while (0);
  1849. if (error != 0)
  1850. ath_tx_cleanup(sc);
  1851. return error;
  1852. }
  1853. /* Reclaim all tx queue resources */
  1854. int ath_tx_cleanup(struct ath_softc *sc)
  1855. {
  1856. /* cleanup beacon descriptors */
  1857. if (sc->sc_bdma.dd_desc_len != 0)
  1858. ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
  1859. /* cleanup tx descriptors */
  1860. if (sc->sc_txdma.dd_desc_len != 0)
  1861. ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
  1862. return 0;
  1863. }
  1864. /* Setup a h/w transmit queue */
  1865. struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
  1866. {
  1867. struct ath_hal *ah = sc->sc_ah;
  1868. struct ath9k_tx_queue_info qi;
  1869. int qnum;
  1870. memzero(&qi, sizeof(qi));
  1871. qi.tqi_subtype = subtype;
  1872. qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
  1873. qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
  1874. qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
  1875. qi.tqi_physCompBuf = 0;
  1876. /*
  1877. * Enable interrupts only for EOL and DESC conditions.
  1878. * We mark tx descriptors to receive a DESC interrupt
  1879. * when a tx queue gets deep; otherwise waiting for the
  1880. * EOL to reap descriptors. Note that this is done to
  1881. * reduce interrupt load and this only defers reaping
  1882. * descriptors, never transmitting frames. Aside from
  1883. * reducing interrupts this also permits more concurrency.
  1884. * The only potential downside is if the tx queue backs
  1885. * up in which case the top half of the kernel may backup
  1886. * due to a lack of tx descriptors.
  1887. *
  1888. * The UAPSD queue is an exception, since we take a desc-
  1889. * based intr on the EOSP frames.
  1890. */
  1891. if (qtype == ATH9K_TX_QUEUE_UAPSD)
  1892. qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
  1893. else
  1894. qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |
  1895. TXQ_FLAG_TXDESCINT_ENABLE;
  1896. qnum = ath9k_hw_setuptxqueue(ah, qtype, &qi);
  1897. if (qnum == -1) {
  1898. /*
  1899. * NB: don't print a message, this happens
  1900. * normally on parts with too few tx queues
  1901. */
  1902. return NULL;
  1903. }
  1904. if (qnum >= ARRAY_SIZE(sc->sc_txq)) {
  1905. DPRINTF(sc, ATH_DBG_FATAL,
  1906. "%s: hal qnum %u out of range, max %u!\n",
  1907. __func__, qnum, (unsigned int)ARRAY_SIZE(sc->sc_txq));
  1908. ath9k_hw_releasetxqueue(ah, qnum);
  1909. return NULL;
  1910. }
  1911. if (!ATH_TXQ_SETUP(sc, qnum)) {
  1912. struct ath_txq *txq = &sc->sc_txq[qnum];
  1913. txq->axq_qnum = qnum;
  1914. txq->axq_link = NULL;
  1915. INIT_LIST_HEAD(&txq->axq_q);
  1916. INIT_LIST_HEAD(&txq->axq_acq);
  1917. spin_lock_init(&txq->axq_lock);
  1918. txq->axq_depth = 0;
  1919. txq->axq_aggr_depth = 0;
  1920. txq->axq_totalqueued = 0;
  1921. txq->axq_intrcnt = 0;
  1922. txq->axq_linkbuf = NULL;
  1923. sc->sc_txqsetup |= 1<<qnum;
  1924. }
  1925. return &sc->sc_txq[qnum];
  1926. }
  1927. /* Reclaim resources for a setup queue */
  1928. void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
  1929. {
  1930. ath9k_hw_releasetxqueue(sc->sc_ah, txq->axq_qnum);
  1931. sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
  1932. }
  1933. /*
  1934. * Setup a hardware data transmit queue for the specified
  1935. * access control. The hal may not support all requested
  1936. * queues in which case it will return a reference to a
  1937. * previously setup queue. We record the mapping from ac's
  1938. * to h/w queues for use by ath_tx_start and also track
  1939. * the set of h/w queues being used to optimize work in the
  1940. * transmit interrupt handler and related routines.
  1941. */
  1942. int ath_tx_setup(struct ath_softc *sc, int haltype)
  1943. {
  1944. struct ath_txq *txq;
  1945. if (haltype >= ARRAY_SIZE(sc->sc_haltype2q)) {
  1946. DPRINTF(sc, ATH_DBG_FATAL,
  1947. "%s: HAL AC %u out of range, max %zu!\n",
  1948. __func__, haltype, ARRAY_SIZE(sc->sc_haltype2q));
  1949. return 0;
  1950. }
  1951. txq = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, haltype);
  1952. if (txq != NULL) {
  1953. sc->sc_haltype2q[haltype] = txq->axq_qnum;
  1954. return 1;
  1955. } else
  1956. return 0;
  1957. }
  1958. int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype)
  1959. {
  1960. int qnum;
  1961. switch (qtype) {
  1962. case ATH9K_TX_QUEUE_DATA:
  1963. if (haltype >= ARRAY_SIZE(sc->sc_haltype2q)) {
  1964. DPRINTF(sc, ATH_DBG_FATAL,
  1965. "%s: HAL AC %u out of range, max %zu!\n",
  1966. __func__,
  1967. haltype, ARRAY_SIZE(sc->sc_haltype2q));
  1968. return -1;
  1969. }
  1970. qnum = sc->sc_haltype2q[haltype];
  1971. break;
  1972. case ATH9K_TX_QUEUE_BEACON:
  1973. qnum = sc->sc_bhalq;
  1974. break;
  1975. case ATH9K_TX_QUEUE_CAB:
  1976. qnum = sc->sc_cabq->axq_qnum;
  1977. break;
  1978. default:
  1979. qnum = -1;
  1980. }
  1981. return qnum;
  1982. }
  1983. /* Update parameters for a transmit queue */
  1984. int ath_txq_update(struct ath_softc *sc, int qnum,
  1985. struct ath9k_tx_queue_info *qinfo)
  1986. {
  1987. struct ath_hal *ah = sc->sc_ah;
  1988. int error = 0;
  1989. struct ath9k_tx_queue_info qi;
  1990. if (qnum == sc->sc_bhalq) {
  1991. /*
  1992. * XXX: for beacon queue, we just save the parameter.
  1993. * It will be picked up by ath_beaconq_config when
  1994. * it's necessary.
  1995. */
  1996. sc->sc_beacon_qi = *qinfo;
  1997. return 0;
  1998. }
  1999. ASSERT(sc->sc_txq[qnum].axq_qnum == qnum);
  2000. ath9k_hw_get_txq_props(ah, qnum, &qi);
  2001. qi.tqi_aifs = qinfo->tqi_aifs;
  2002. qi.tqi_cwmin = qinfo->tqi_cwmin;
  2003. qi.tqi_cwmax = qinfo->tqi_cwmax;
  2004. qi.tqi_burstTime = qinfo->tqi_burstTime;
  2005. qi.tqi_readyTime = qinfo->tqi_readyTime;
  2006. if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
  2007. DPRINTF(sc, ATH_DBG_FATAL,
  2008. "%s: unable to update hardware queue %u!\n",
  2009. __func__, qnum);
  2010. error = -EIO;
  2011. } else {
  2012. ath9k_hw_resettxqueue(ah, qnum); /* push to h/w */
  2013. }
  2014. return error;
  2015. }
  2016. int ath_cabq_update(struct ath_softc *sc)
  2017. {
  2018. struct ath9k_tx_queue_info qi;
  2019. int qnum = sc->sc_cabq->axq_qnum;
  2020. struct ath_beacon_config conf;
  2021. ath9k_hw_get_txq_props(sc->sc_ah, qnum, &qi);
  2022. /*
  2023. * Ensure the readytime % is within the bounds.
  2024. */
  2025. if (sc->sc_config.cabqReadytime < ATH9K_READY_TIME_LO_BOUND)
  2026. sc->sc_config.cabqReadytime = ATH9K_READY_TIME_LO_BOUND;
  2027. else if (sc->sc_config.cabqReadytime > ATH9K_READY_TIME_HI_BOUND)
  2028. sc->sc_config.cabqReadytime = ATH9K_READY_TIME_HI_BOUND;
  2029. ath_get_beaconconfig(sc, ATH_IF_ID_ANY, &conf);
  2030. qi.tqi_readyTime =
  2031. (conf.beacon_interval * sc->sc_config.cabqReadytime) / 100;
  2032. ath_txq_update(sc, qnum, &qi);
  2033. return 0;
  2034. }
  2035. int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb)
  2036. {
  2037. struct ath_tx_control txctl;
  2038. int error = 0;
  2039. error = ath_tx_prepare(sc, skb, &txctl);
  2040. if (error == 0)
  2041. /*
  2042. * Start DMA mapping.
  2043. * ath_tx_start_dma() will be called either synchronously
  2044. * or asynchrounsly once DMA is complete.
  2045. */
  2046. xmit_map_sg(sc, skb,
  2047. get_dma_mem_context(&txctl, dmacontext),
  2048. &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->bf_isampdu)
  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_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_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_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_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->bf_isretried) {
  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_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_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. }