xmit.c 64 KB

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