xmit.c 68 KB

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