xmit.c 70 KB

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