xmit.c 70 KB

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