xmit.c 69 KB

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