xmit.c 64 KB

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