xmit.c 65 KB

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