xmit.c 63 KB

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