xmit.c 63 KB

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