htt_rx.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "core.h"
  18. #include "htc.h"
  19. #include "htt.h"
  20. #include "txrx.h"
  21. #include "debug.h"
  22. #include "trace.h"
  23. #include "mac.h"
  24. #include <linux/log2.h>
  25. #define HTT_RX_RING_SIZE HTT_RX_RING_SIZE_MAX
  26. #define HTT_RX_RING_FILL_LEVEL (((HTT_RX_RING_SIZE) / 2) - 1)
  27. /* when under memory pressure rx ring refill may fail and needs a retry */
  28. #define HTT_RX_RING_REFILL_RETRY_MS 50
  29. #define HTT_RX_RING_REFILL_RESCHED_MS 5
  30. static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);
  31. static struct sk_buff *
  32. ath10k_htt_rx_find_skb_paddr(struct ath10k *ar, u32 paddr)
  33. {
  34. struct ath10k_skb_rxcb *rxcb;
  35. hash_for_each_possible(ar->htt.rx_ring.skb_table, rxcb, hlist, paddr)
  36. if (rxcb->paddr == paddr)
  37. return ATH10K_RXCB_SKB(rxcb);
  38. WARN_ON_ONCE(1);
  39. return NULL;
  40. }
  41. static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
  42. {
  43. struct sk_buff *skb;
  44. struct ath10k_skb_rxcb *rxcb;
  45. struct hlist_node *n;
  46. int i;
  47. if (htt->rx_ring.in_ord_rx) {
  48. hash_for_each_safe(htt->rx_ring.skb_table, i, n, rxcb, hlist) {
  49. skb = ATH10K_RXCB_SKB(rxcb);
  50. dma_unmap_single(htt->ar->dev, rxcb->paddr,
  51. skb->len + skb_tailroom(skb),
  52. DMA_FROM_DEVICE);
  53. hash_del(&rxcb->hlist);
  54. dev_kfree_skb_any(skb);
  55. }
  56. } else {
  57. for (i = 0; i < htt->rx_ring.size; i++) {
  58. skb = htt->rx_ring.netbufs_ring[i];
  59. if (!skb)
  60. continue;
  61. rxcb = ATH10K_SKB_RXCB(skb);
  62. dma_unmap_single(htt->ar->dev, rxcb->paddr,
  63. skb->len + skb_tailroom(skb),
  64. DMA_FROM_DEVICE);
  65. dev_kfree_skb_any(skb);
  66. }
  67. }
  68. htt->rx_ring.fill_cnt = 0;
  69. hash_init(htt->rx_ring.skb_table);
  70. memset(htt->rx_ring.netbufs_ring, 0,
  71. htt->rx_ring.size * sizeof(htt->rx_ring.netbufs_ring[0]));
  72. }
  73. static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
  74. {
  75. struct htt_rx_desc *rx_desc;
  76. struct ath10k_skb_rxcb *rxcb;
  77. struct sk_buff *skb;
  78. dma_addr_t paddr;
  79. int ret = 0, idx;
  80. /* The Full Rx Reorder firmware has no way of telling the host
  81. * implicitly when it copied HTT Rx Ring buffers to MAC Rx Ring.
  82. * To keep things simple make sure ring is always half empty. This
  83. * guarantees there'll be no replenishment overruns possible.
  84. */
  85. BUILD_BUG_ON(HTT_RX_RING_FILL_LEVEL >= HTT_RX_RING_SIZE / 2);
  86. idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
  87. while (num > 0) {
  88. skb = dev_alloc_skb(HTT_RX_BUF_SIZE + HTT_RX_DESC_ALIGN);
  89. if (!skb) {
  90. ret = -ENOMEM;
  91. goto fail;
  92. }
  93. if (!IS_ALIGNED((unsigned long)skb->data, HTT_RX_DESC_ALIGN))
  94. skb_pull(skb,
  95. PTR_ALIGN(skb->data, HTT_RX_DESC_ALIGN) -
  96. skb->data);
  97. /* Clear rx_desc attention word before posting to Rx ring */
  98. rx_desc = (struct htt_rx_desc *)skb->data;
  99. rx_desc->attention.flags = __cpu_to_le32(0);
  100. paddr = dma_map_single(htt->ar->dev, skb->data,
  101. skb->len + skb_tailroom(skb),
  102. DMA_FROM_DEVICE);
  103. if (unlikely(dma_mapping_error(htt->ar->dev, paddr))) {
  104. dev_kfree_skb_any(skb);
  105. ret = -ENOMEM;
  106. goto fail;
  107. }
  108. rxcb = ATH10K_SKB_RXCB(skb);
  109. rxcb->paddr = paddr;
  110. htt->rx_ring.netbufs_ring[idx] = skb;
  111. htt->rx_ring.paddrs_ring[idx] = __cpu_to_le32(paddr);
  112. htt->rx_ring.fill_cnt++;
  113. if (htt->rx_ring.in_ord_rx) {
  114. hash_add(htt->rx_ring.skb_table,
  115. &ATH10K_SKB_RXCB(skb)->hlist,
  116. (u32)paddr);
  117. }
  118. num--;
  119. idx++;
  120. idx &= htt->rx_ring.size_mask;
  121. }
  122. fail:
  123. /*
  124. * Make sure the rx buffer is updated before available buffer
  125. * index to avoid any potential rx ring corruption.
  126. */
  127. mb();
  128. *htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
  129. return ret;
  130. }
  131. static int ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
  132. {
  133. lockdep_assert_held(&htt->rx_ring.lock);
  134. return __ath10k_htt_rx_ring_fill_n(htt, num);
  135. }
  136. static void ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt *htt)
  137. {
  138. int ret, num_deficit, num_to_fill;
  139. /* Refilling the whole RX ring buffer proves to be a bad idea. The
  140. * reason is RX may take up significant amount of CPU cycles and starve
  141. * other tasks, e.g. TX on an ethernet device while acting as a bridge
  142. * with ath10k wlan interface. This ended up with very poor performance
  143. * once CPU the host system was overwhelmed with RX on ath10k.
  144. *
  145. * By limiting the number of refills the replenishing occurs
  146. * progressively. This in turns makes use of the fact tasklets are
  147. * processed in FIFO order. This means actual RX processing can starve
  148. * out refilling. If there's not enough buffers on RX ring FW will not
  149. * report RX until it is refilled with enough buffers. This
  150. * automatically balances load wrt to CPU power.
  151. *
  152. * This probably comes at a cost of lower maximum throughput but
  153. * improves the average and stability.
  154. */
  155. spin_lock_bh(&htt->rx_ring.lock);
  156. num_deficit = htt->rx_ring.fill_level - htt->rx_ring.fill_cnt;
  157. num_to_fill = min(ATH10K_HTT_MAX_NUM_REFILL, num_deficit);
  158. num_deficit -= num_to_fill;
  159. ret = ath10k_htt_rx_ring_fill_n(htt, num_to_fill);
  160. if (ret == -ENOMEM) {
  161. /*
  162. * Failed to fill it to the desired level -
  163. * we'll start a timer and try again next time.
  164. * As long as enough buffers are left in the ring for
  165. * another A-MPDU rx, no special recovery is needed.
  166. */
  167. mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
  168. msecs_to_jiffies(HTT_RX_RING_REFILL_RETRY_MS));
  169. } else if (num_deficit > 0) {
  170. mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
  171. msecs_to_jiffies(HTT_RX_RING_REFILL_RESCHED_MS));
  172. }
  173. spin_unlock_bh(&htt->rx_ring.lock);
  174. }
  175. static void ath10k_htt_rx_ring_refill_retry(unsigned long arg)
  176. {
  177. struct ath10k_htt *htt = (struct ath10k_htt *)arg;
  178. ath10k_htt_rx_msdu_buff_replenish(htt);
  179. }
  180. int ath10k_htt_rx_ring_refill(struct ath10k *ar)
  181. {
  182. struct ath10k_htt *htt = &ar->htt;
  183. int ret;
  184. spin_lock_bh(&htt->rx_ring.lock);
  185. ret = ath10k_htt_rx_ring_fill_n(htt, (htt->rx_ring.fill_level -
  186. htt->rx_ring.fill_cnt));
  187. spin_unlock_bh(&htt->rx_ring.lock);
  188. if (ret)
  189. ath10k_htt_rx_ring_free(htt);
  190. return ret;
  191. }
  192. void ath10k_htt_rx_free(struct ath10k_htt *htt)
  193. {
  194. del_timer_sync(&htt->rx_ring.refill_retry_timer);
  195. skb_queue_purge(&htt->rx_compl_q);
  196. skb_queue_purge(&htt->rx_in_ord_compl_q);
  197. skb_queue_purge(&htt->tx_fetch_ind_q);
  198. ath10k_htt_rx_ring_free(htt);
  199. dma_free_coherent(htt->ar->dev,
  200. (htt->rx_ring.size *
  201. sizeof(htt->rx_ring.paddrs_ring)),
  202. htt->rx_ring.paddrs_ring,
  203. htt->rx_ring.base_paddr);
  204. dma_free_coherent(htt->ar->dev,
  205. sizeof(*htt->rx_ring.alloc_idx.vaddr),
  206. htt->rx_ring.alloc_idx.vaddr,
  207. htt->rx_ring.alloc_idx.paddr);
  208. kfree(htt->rx_ring.netbufs_ring);
  209. }
  210. static inline struct sk_buff *ath10k_htt_rx_netbuf_pop(struct ath10k_htt *htt)
  211. {
  212. struct ath10k *ar = htt->ar;
  213. int idx;
  214. struct sk_buff *msdu;
  215. lockdep_assert_held(&htt->rx_ring.lock);
  216. if (htt->rx_ring.fill_cnt == 0) {
  217. ath10k_warn(ar, "tried to pop sk_buff from an empty rx ring\n");
  218. return NULL;
  219. }
  220. idx = htt->rx_ring.sw_rd_idx.msdu_payld;
  221. msdu = htt->rx_ring.netbufs_ring[idx];
  222. htt->rx_ring.netbufs_ring[idx] = NULL;
  223. htt->rx_ring.paddrs_ring[idx] = 0;
  224. idx++;
  225. idx &= htt->rx_ring.size_mask;
  226. htt->rx_ring.sw_rd_idx.msdu_payld = idx;
  227. htt->rx_ring.fill_cnt--;
  228. dma_unmap_single(htt->ar->dev,
  229. ATH10K_SKB_RXCB(msdu)->paddr,
  230. msdu->len + skb_tailroom(msdu),
  231. DMA_FROM_DEVICE);
  232. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
  233. msdu->data, msdu->len + skb_tailroom(msdu));
  234. return msdu;
  235. }
  236. /* return: < 0 fatal error, 0 - non chained msdu, 1 chained msdu */
  237. static int ath10k_htt_rx_amsdu_pop(struct ath10k_htt *htt,
  238. struct sk_buff_head *amsdu)
  239. {
  240. struct ath10k *ar = htt->ar;
  241. int msdu_len, msdu_chaining = 0;
  242. struct sk_buff *msdu;
  243. struct htt_rx_desc *rx_desc;
  244. lockdep_assert_held(&htt->rx_ring.lock);
  245. for (;;) {
  246. int last_msdu, msdu_len_invalid, msdu_chained;
  247. msdu = ath10k_htt_rx_netbuf_pop(htt);
  248. if (!msdu) {
  249. __skb_queue_purge(amsdu);
  250. return -ENOENT;
  251. }
  252. __skb_queue_tail(amsdu, msdu);
  253. rx_desc = (struct htt_rx_desc *)msdu->data;
  254. /* FIXME: we must report msdu payload since this is what caller
  255. * expects now
  256. */
  257. skb_put(msdu, offsetof(struct htt_rx_desc, msdu_payload));
  258. skb_pull(msdu, offsetof(struct htt_rx_desc, msdu_payload));
  259. /*
  260. * Sanity check - confirm the HW is finished filling in the
  261. * rx data.
  262. * If the HW and SW are working correctly, then it's guaranteed
  263. * that the HW's MAC DMA is done before this point in the SW.
  264. * To prevent the case that we handle a stale Rx descriptor,
  265. * just assert for now until we have a way to recover.
  266. */
  267. if (!(__le32_to_cpu(rx_desc->attention.flags)
  268. & RX_ATTENTION_FLAGS_MSDU_DONE)) {
  269. __skb_queue_purge(amsdu);
  270. return -EIO;
  271. }
  272. msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
  273. & (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
  274. RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
  275. msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.common.info0),
  276. RX_MSDU_START_INFO0_MSDU_LENGTH);
  277. msdu_chained = rx_desc->frag_info.ring2_more_count;
  278. if (msdu_len_invalid)
  279. msdu_len = 0;
  280. skb_trim(msdu, 0);
  281. skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
  282. msdu_len -= msdu->len;
  283. /* Note: Chained buffers do not contain rx descriptor */
  284. while (msdu_chained--) {
  285. msdu = ath10k_htt_rx_netbuf_pop(htt);
  286. if (!msdu) {
  287. __skb_queue_purge(amsdu);
  288. return -ENOENT;
  289. }
  290. __skb_queue_tail(amsdu, msdu);
  291. skb_trim(msdu, 0);
  292. skb_put(msdu, min(msdu_len, HTT_RX_BUF_SIZE));
  293. msdu_len -= msdu->len;
  294. msdu_chaining = 1;
  295. }
  296. last_msdu = __le32_to_cpu(rx_desc->msdu_end.common.info0) &
  297. RX_MSDU_END_INFO0_LAST_MSDU;
  298. trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
  299. sizeof(*rx_desc) - sizeof(u32));
  300. if (last_msdu)
  301. break;
  302. }
  303. if (skb_queue_empty(amsdu))
  304. msdu_chaining = -1;
  305. /*
  306. * Don't refill the ring yet.
  307. *
  308. * First, the elements popped here are still in use - it is not
  309. * safe to overwrite them until the matching call to
  310. * mpdu_desc_list_next. Second, for efficiency it is preferable to
  311. * refill the rx ring with 1 PPDU's worth of rx buffers (something
  312. * like 32 x 3 buffers), rather than one MPDU's worth of rx buffers
  313. * (something like 3 buffers). Consequently, we'll rely on the txrx
  314. * SW to tell us when it is done pulling all the PPDU's rx buffers
  315. * out of the rx ring, and then refill it just once.
  316. */
  317. return msdu_chaining;
  318. }
  319. static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
  320. u32 paddr)
  321. {
  322. struct ath10k *ar = htt->ar;
  323. struct ath10k_skb_rxcb *rxcb;
  324. struct sk_buff *msdu;
  325. lockdep_assert_held(&htt->rx_ring.lock);
  326. msdu = ath10k_htt_rx_find_skb_paddr(ar, paddr);
  327. if (!msdu)
  328. return NULL;
  329. rxcb = ATH10K_SKB_RXCB(msdu);
  330. hash_del(&rxcb->hlist);
  331. htt->rx_ring.fill_cnt--;
  332. dma_unmap_single(htt->ar->dev, rxcb->paddr,
  333. msdu->len + skb_tailroom(msdu),
  334. DMA_FROM_DEVICE);
  335. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
  336. msdu->data, msdu->len + skb_tailroom(msdu));
  337. return msdu;
  338. }
  339. static int ath10k_htt_rx_pop_paddr_list(struct ath10k_htt *htt,
  340. struct htt_rx_in_ord_ind *ev,
  341. struct sk_buff_head *list)
  342. {
  343. struct ath10k *ar = htt->ar;
  344. struct htt_rx_in_ord_msdu_desc *msdu_desc = ev->msdu_descs;
  345. struct htt_rx_desc *rxd;
  346. struct sk_buff *msdu;
  347. int msdu_count;
  348. bool is_offload;
  349. u32 paddr;
  350. lockdep_assert_held(&htt->rx_ring.lock);
  351. msdu_count = __le16_to_cpu(ev->msdu_count);
  352. is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
  353. while (msdu_count--) {
  354. paddr = __le32_to_cpu(msdu_desc->msdu_paddr);
  355. msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
  356. if (!msdu) {
  357. __skb_queue_purge(list);
  358. return -ENOENT;
  359. }
  360. __skb_queue_tail(list, msdu);
  361. if (!is_offload) {
  362. rxd = (void *)msdu->data;
  363. trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
  364. skb_put(msdu, sizeof(*rxd));
  365. skb_pull(msdu, sizeof(*rxd));
  366. skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
  367. if (!(__le32_to_cpu(rxd->attention.flags) &
  368. RX_ATTENTION_FLAGS_MSDU_DONE)) {
  369. ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
  370. return -EIO;
  371. }
  372. }
  373. msdu_desc++;
  374. }
  375. return 0;
  376. }
  377. int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
  378. {
  379. struct ath10k *ar = htt->ar;
  380. dma_addr_t paddr;
  381. void *vaddr;
  382. size_t size;
  383. struct timer_list *timer = &htt->rx_ring.refill_retry_timer;
  384. htt->rx_confused = false;
  385. /* XXX: The fill level could be changed during runtime in response to
  386. * the host processing latency. Is this really worth it?
  387. */
  388. htt->rx_ring.size = HTT_RX_RING_SIZE;
  389. htt->rx_ring.size_mask = htt->rx_ring.size - 1;
  390. htt->rx_ring.fill_level = HTT_RX_RING_FILL_LEVEL;
  391. if (!is_power_of_2(htt->rx_ring.size)) {
  392. ath10k_warn(ar, "htt rx ring size is not power of 2\n");
  393. return -EINVAL;
  394. }
  395. htt->rx_ring.netbufs_ring =
  396. kzalloc(htt->rx_ring.size * sizeof(struct sk_buff *),
  397. GFP_KERNEL);
  398. if (!htt->rx_ring.netbufs_ring)
  399. goto err_netbuf;
  400. size = htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring);
  401. vaddr = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_KERNEL);
  402. if (!vaddr)
  403. goto err_dma_ring;
  404. htt->rx_ring.paddrs_ring = vaddr;
  405. htt->rx_ring.base_paddr = paddr;
  406. vaddr = dma_alloc_coherent(htt->ar->dev,
  407. sizeof(*htt->rx_ring.alloc_idx.vaddr),
  408. &paddr, GFP_KERNEL);
  409. if (!vaddr)
  410. goto err_dma_idx;
  411. htt->rx_ring.alloc_idx.vaddr = vaddr;
  412. htt->rx_ring.alloc_idx.paddr = paddr;
  413. htt->rx_ring.sw_rd_idx.msdu_payld = htt->rx_ring.size_mask;
  414. *htt->rx_ring.alloc_idx.vaddr = 0;
  415. /* Initialize the Rx refill retry timer */
  416. setup_timer(timer, ath10k_htt_rx_ring_refill_retry, (unsigned long)htt);
  417. spin_lock_init(&htt->rx_ring.lock);
  418. htt->rx_ring.fill_cnt = 0;
  419. htt->rx_ring.sw_rd_idx.msdu_payld = 0;
  420. hash_init(htt->rx_ring.skb_table);
  421. skb_queue_head_init(&htt->rx_compl_q);
  422. skb_queue_head_init(&htt->rx_in_ord_compl_q);
  423. skb_queue_head_init(&htt->tx_fetch_ind_q);
  424. atomic_set(&htt->num_mpdus_ready, 0);
  425. ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
  426. htt->rx_ring.size, htt->rx_ring.fill_level);
  427. return 0;
  428. err_dma_idx:
  429. dma_free_coherent(htt->ar->dev,
  430. (htt->rx_ring.size *
  431. sizeof(htt->rx_ring.paddrs_ring)),
  432. htt->rx_ring.paddrs_ring,
  433. htt->rx_ring.base_paddr);
  434. err_dma_ring:
  435. kfree(htt->rx_ring.netbufs_ring);
  436. err_netbuf:
  437. return -ENOMEM;
  438. }
  439. static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
  440. enum htt_rx_mpdu_encrypt_type type)
  441. {
  442. switch (type) {
  443. case HTT_RX_MPDU_ENCRYPT_NONE:
  444. return 0;
  445. case HTT_RX_MPDU_ENCRYPT_WEP40:
  446. case HTT_RX_MPDU_ENCRYPT_WEP104:
  447. return IEEE80211_WEP_IV_LEN;
  448. case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
  449. case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
  450. return IEEE80211_TKIP_IV_LEN;
  451. case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
  452. return IEEE80211_CCMP_HDR_LEN;
  453. case HTT_RX_MPDU_ENCRYPT_WEP128:
  454. case HTT_RX_MPDU_ENCRYPT_WAPI:
  455. break;
  456. }
  457. ath10k_warn(ar, "unsupported encryption type %d\n", type);
  458. return 0;
  459. }
  460. #define MICHAEL_MIC_LEN 8
  461. static int ath10k_htt_rx_crypto_tail_len(struct ath10k *ar,
  462. enum htt_rx_mpdu_encrypt_type type)
  463. {
  464. switch (type) {
  465. case HTT_RX_MPDU_ENCRYPT_NONE:
  466. return 0;
  467. case HTT_RX_MPDU_ENCRYPT_WEP40:
  468. case HTT_RX_MPDU_ENCRYPT_WEP104:
  469. return IEEE80211_WEP_ICV_LEN;
  470. case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
  471. case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
  472. return IEEE80211_TKIP_ICV_LEN;
  473. case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
  474. return IEEE80211_CCMP_MIC_LEN;
  475. case HTT_RX_MPDU_ENCRYPT_WEP128:
  476. case HTT_RX_MPDU_ENCRYPT_WAPI:
  477. break;
  478. }
  479. ath10k_warn(ar, "unsupported encryption type %d\n", type);
  480. return 0;
  481. }
  482. struct amsdu_subframe_hdr {
  483. u8 dst[ETH_ALEN];
  484. u8 src[ETH_ALEN];
  485. __be16 len;
  486. } __packed;
  487. #define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)
  488. static void ath10k_htt_rx_h_rates(struct ath10k *ar,
  489. struct ieee80211_rx_status *status,
  490. struct htt_rx_desc *rxd)
  491. {
  492. struct ieee80211_supported_band *sband;
  493. u8 cck, rate, bw, sgi, mcs, nss;
  494. u8 preamble = 0;
  495. u8 group_id;
  496. u32 info1, info2, info3;
  497. info1 = __le32_to_cpu(rxd->ppdu_start.info1);
  498. info2 = __le32_to_cpu(rxd->ppdu_start.info2);
  499. info3 = __le32_to_cpu(rxd->ppdu_start.info3);
  500. preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
  501. switch (preamble) {
  502. case HTT_RX_LEGACY:
  503. /* To get legacy rate index band is required. Since band can't
  504. * be undefined check if freq is non-zero.
  505. */
  506. if (!status->freq)
  507. return;
  508. cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
  509. rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
  510. rate &= ~RX_PPDU_START_RATE_FLAG;
  511. sband = &ar->mac.sbands[status->band];
  512. status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate, cck);
  513. break;
  514. case HTT_RX_HT:
  515. case HTT_RX_HT_WITH_TXBF:
  516. /* HT-SIG - Table 20-11 in info2 and info3 */
  517. mcs = info2 & 0x1F;
  518. nss = mcs >> 3;
  519. bw = (info2 >> 7) & 1;
  520. sgi = (info3 >> 7) & 1;
  521. status->rate_idx = mcs;
  522. status->encoding = RX_ENC_HT;
  523. if (sgi)
  524. status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
  525. if (bw)
  526. status->bw = RATE_INFO_BW_40;
  527. break;
  528. case HTT_RX_VHT:
  529. case HTT_RX_VHT_WITH_TXBF:
  530. /* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
  531. * TODO check this
  532. */
  533. bw = info2 & 3;
  534. sgi = info3 & 1;
  535. group_id = (info2 >> 4) & 0x3F;
  536. if (GROUP_ID_IS_SU_MIMO(group_id)) {
  537. mcs = (info3 >> 4) & 0x0F;
  538. nss = ((info2 >> 10) & 0x07) + 1;
  539. } else {
  540. /* Hardware doesn't decode VHT-SIG-B into Rx descriptor
  541. * so it's impossible to decode MCS. Also since
  542. * firmware consumes Group Id Management frames host
  543. * has no knowledge regarding group/user position
  544. * mapping so it's impossible to pick the correct Nsts
  545. * from VHT-SIG-A1.
  546. *
  547. * Bandwidth and SGI are valid so report the rateinfo
  548. * on best-effort basis.
  549. */
  550. mcs = 0;
  551. nss = 1;
  552. }
  553. if (mcs > 0x09) {
  554. ath10k_warn(ar, "invalid MCS received %u\n", mcs);
  555. ath10k_warn(ar, "rxd %08x mpdu start %08x %08x msdu start %08x %08x ppdu start %08x %08x %08x %08x %08x\n",
  556. __le32_to_cpu(rxd->attention.flags),
  557. __le32_to_cpu(rxd->mpdu_start.info0),
  558. __le32_to_cpu(rxd->mpdu_start.info1),
  559. __le32_to_cpu(rxd->msdu_start.common.info0),
  560. __le32_to_cpu(rxd->msdu_start.common.info1),
  561. rxd->ppdu_start.info0,
  562. __le32_to_cpu(rxd->ppdu_start.info1),
  563. __le32_to_cpu(rxd->ppdu_start.info2),
  564. __le32_to_cpu(rxd->ppdu_start.info3),
  565. __le32_to_cpu(rxd->ppdu_start.info4));
  566. ath10k_warn(ar, "msdu end %08x mpdu end %08x\n",
  567. __le32_to_cpu(rxd->msdu_end.common.info0),
  568. __le32_to_cpu(rxd->mpdu_end.info0));
  569. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL,
  570. "rx desc msdu payload: ",
  571. rxd->msdu_payload, 50);
  572. }
  573. status->rate_idx = mcs;
  574. status->nss = nss;
  575. if (sgi)
  576. status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
  577. switch (bw) {
  578. /* 20MHZ */
  579. case 0:
  580. break;
  581. /* 40MHZ */
  582. case 1:
  583. status->bw = RATE_INFO_BW_40;
  584. break;
  585. /* 80MHZ */
  586. case 2:
  587. status->bw = RATE_INFO_BW_80;
  588. break;
  589. case 3:
  590. status->bw = RATE_INFO_BW_160;
  591. break;
  592. }
  593. status->encoding = RX_ENC_VHT;
  594. break;
  595. default:
  596. break;
  597. }
  598. }
  599. static struct ieee80211_channel *
  600. ath10k_htt_rx_h_peer_channel(struct ath10k *ar, struct htt_rx_desc *rxd)
  601. {
  602. struct ath10k_peer *peer;
  603. struct ath10k_vif *arvif;
  604. struct cfg80211_chan_def def;
  605. u16 peer_id;
  606. lockdep_assert_held(&ar->data_lock);
  607. if (!rxd)
  608. return NULL;
  609. if (rxd->attention.flags &
  610. __cpu_to_le32(RX_ATTENTION_FLAGS_PEER_IDX_INVALID))
  611. return NULL;
  612. if (!(rxd->msdu_end.common.info0 &
  613. __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU)))
  614. return NULL;
  615. peer_id = MS(__le32_to_cpu(rxd->mpdu_start.info0),
  616. RX_MPDU_START_INFO0_PEER_IDX);
  617. peer = ath10k_peer_find_by_id(ar, peer_id);
  618. if (!peer)
  619. return NULL;
  620. arvif = ath10k_get_arvif(ar, peer->vdev_id);
  621. if (WARN_ON_ONCE(!arvif))
  622. return NULL;
  623. if (ath10k_mac_vif_chan(arvif->vif, &def))
  624. return NULL;
  625. return def.chan;
  626. }
  627. static struct ieee80211_channel *
  628. ath10k_htt_rx_h_vdev_channel(struct ath10k *ar, u32 vdev_id)
  629. {
  630. struct ath10k_vif *arvif;
  631. struct cfg80211_chan_def def;
  632. lockdep_assert_held(&ar->data_lock);
  633. list_for_each_entry(arvif, &ar->arvifs, list) {
  634. if (arvif->vdev_id == vdev_id &&
  635. ath10k_mac_vif_chan(arvif->vif, &def) == 0)
  636. return def.chan;
  637. }
  638. return NULL;
  639. }
  640. static void
  641. ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw *hw,
  642. struct ieee80211_chanctx_conf *conf,
  643. void *data)
  644. {
  645. struct cfg80211_chan_def *def = data;
  646. *def = conf->def;
  647. }
  648. static struct ieee80211_channel *
  649. ath10k_htt_rx_h_any_channel(struct ath10k *ar)
  650. {
  651. struct cfg80211_chan_def def = {};
  652. ieee80211_iter_chan_contexts_atomic(ar->hw,
  653. ath10k_htt_rx_h_any_chan_iter,
  654. &def);
  655. return def.chan;
  656. }
  657. static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
  658. struct ieee80211_rx_status *status,
  659. struct htt_rx_desc *rxd,
  660. u32 vdev_id)
  661. {
  662. struct ieee80211_channel *ch;
  663. spin_lock_bh(&ar->data_lock);
  664. ch = ar->scan_channel;
  665. if (!ch)
  666. ch = ar->rx_channel;
  667. if (!ch)
  668. ch = ath10k_htt_rx_h_peer_channel(ar, rxd);
  669. if (!ch)
  670. ch = ath10k_htt_rx_h_vdev_channel(ar, vdev_id);
  671. if (!ch)
  672. ch = ath10k_htt_rx_h_any_channel(ar);
  673. if (!ch)
  674. ch = ar->tgt_oper_chan;
  675. spin_unlock_bh(&ar->data_lock);
  676. if (!ch)
  677. return false;
  678. status->band = ch->band;
  679. status->freq = ch->center_freq;
  680. return true;
  681. }
  682. static void ath10k_htt_rx_h_signal(struct ath10k *ar,
  683. struct ieee80211_rx_status *status,
  684. struct htt_rx_desc *rxd)
  685. {
  686. /* FIXME: Get real NF */
  687. status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
  688. rxd->ppdu_start.rssi_comb;
  689. status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
  690. }
  691. static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
  692. struct ieee80211_rx_status *status,
  693. struct htt_rx_desc *rxd)
  694. {
  695. /* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
  696. * means all prior MSDUs in a PPDU are reported to mac80211 without the
  697. * TSF. Is it worth holding frames until end of PPDU is known?
  698. *
  699. * FIXME: Can we get/compute 64bit TSF?
  700. */
  701. status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
  702. status->flag |= RX_FLAG_MACTIME_END;
  703. }
  704. static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
  705. struct sk_buff_head *amsdu,
  706. struct ieee80211_rx_status *status,
  707. u32 vdev_id)
  708. {
  709. struct sk_buff *first;
  710. struct htt_rx_desc *rxd;
  711. bool is_first_ppdu;
  712. bool is_last_ppdu;
  713. if (skb_queue_empty(amsdu))
  714. return;
  715. first = skb_peek(amsdu);
  716. rxd = (void *)first->data - sizeof(*rxd);
  717. is_first_ppdu = !!(rxd->attention.flags &
  718. __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
  719. is_last_ppdu = !!(rxd->attention.flags &
  720. __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));
  721. if (is_first_ppdu) {
  722. /* New PPDU starts so clear out the old per-PPDU status. */
  723. status->freq = 0;
  724. status->rate_idx = 0;
  725. status->nss = 0;
  726. status->encoding = RX_ENC_LEGACY;
  727. status->bw = RATE_INFO_BW_20;
  728. status->flag &= ~RX_FLAG_MACTIME_END;
  729. status->flag |= RX_FLAG_NO_SIGNAL_VAL;
  730. ath10k_htt_rx_h_signal(ar, status, rxd);
  731. ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
  732. ath10k_htt_rx_h_rates(ar, status, rxd);
  733. }
  734. if (is_last_ppdu)
  735. ath10k_htt_rx_h_mactime(ar, status, rxd);
  736. }
  737. static const char * const tid_to_ac[] = {
  738. "BE",
  739. "BK",
  740. "BK",
  741. "BE",
  742. "VI",
  743. "VI",
  744. "VO",
  745. "VO",
  746. };
  747. static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
  748. {
  749. u8 *qc;
  750. int tid;
  751. if (!ieee80211_is_data_qos(hdr->frame_control))
  752. return "";
  753. qc = ieee80211_get_qos_ctl(hdr);
  754. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  755. if (tid < 8)
  756. snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
  757. else
  758. snprintf(out, size, "tid %d", tid);
  759. return out;
  760. }
  761. static void ath10k_process_rx(struct ath10k *ar,
  762. struct ieee80211_rx_status *rx_status,
  763. struct sk_buff *skb)
  764. {
  765. struct ieee80211_rx_status *status;
  766. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  767. char tid[32];
  768. status = IEEE80211_SKB_RXCB(skb);
  769. *status = *rx_status;
  770. ath10k_dbg(ar, ATH10K_DBG_DATA,
  771. "rx skb %pK len %u peer %pM %s %s sn %u %s%s%s%s%s%s %srate_idx %u vht_nss %u freq %u band %u flag 0x%x fcs-err %i mic-err %i amsdu-more %i\n",
  772. skb,
  773. skb->len,
  774. ieee80211_get_SA(hdr),
  775. ath10k_get_tid(hdr, tid, sizeof(tid)),
  776. is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
  777. "mcast" : "ucast",
  778. (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
  779. (status->encoding == RX_ENC_LEGACY) ? "legacy" : "",
  780. (status->encoding == RX_ENC_HT) ? "ht" : "",
  781. (status->encoding == RX_ENC_VHT) ? "vht" : "",
  782. (status->bw == RATE_INFO_BW_40) ? "40" : "",
  783. (status->bw == RATE_INFO_BW_80) ? "80" : "",
  784. (status->bw == RATE_INFO_BW_160) ? "160" : "",
  785. status->enc_flags & RX_ENC_FLAG_SHORT_GI ? "sgi " : "",
  786. status->rate_idx,
  787. status->nss,
  788. status->freq,
  789. status->band, status->flag,
  790. !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
  791. !!(status->flag & RX_FLAG_MMIC_ERROR),
  792. !!(status->flag & RX_FLAG_AMSDU_MORE));
  793. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
  794. skb->data, skb->len);
  795. trace_ath10k_rx_hdr(ar, skb->data, skb->len);
  796. trace_ath10k_rx_payload(ar, skb->data, skb->len);
  797. ieee80211_rx_napi(ar->hw, NULL, skb, &ar->napi);
  798. }
  799. static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
  800. struct ieee80211_hdr *hdr)
  801. {
  802. int len = ieee80211_hdrlen(hdr->frame_control);
  803. if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
  804. ar->running_fw->fw_file.fw_features))
  805. len = round_up(len, 4);
  806. return len;
  807. }
  808. static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
  809. struct sk_buff *msdu,
  810. struct ieee80211_rx_status *status,
  811. enum htt_rx_mpdu_encrypt_type enctype,
  812. bool is_decrypted)
  813. {
  814. struct ieee80211_hdr *hdr;
  815. struct htt_rx_desc *rxd;
  816. size_t hdr_len;
  817. size_t crypto_len;
  818. bool is_first;
  819. bool is_last;
  820. rxd = (void *)msdu->data - sizeof(*rxd);
  821. is_first = !!(rxd->msdu_end.common.info0 &
  822. __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
  823. is_last = !!(rxd->msdu_end.common.info0 &
  824. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
  825. /* Delivered decapped frame:
  826. * [802.11 header]
  827. * [crypto param] <-- can be trimmed if !fcs_err &&
  828. * !decrypt_err && !peer_idx_invalid
  829. * [amsdu header] <-- only if A-MSDU
  830. * [rfc1042/llc]
  831. * [payload]
  832. * [FCS] <-- at end, needs to be trimmed
  833. */
  834. /* This probably shouldn't happen but warn just in case */
  835. if (unlikely(WARN_ON_ONCE(!is_first)))
  836. return;
  837. /* This probably shouldn't happen but warn just in case */
  838. if (unlikely(WARN_ON_ONCE(!(is_first && is_last))))
  839. return;
  840. skb_trim(msdu, msdu->len - FCS_LEN);
  841. /* In most cases this will be true for sniffed frames. It makes sense
  842. * to deliver them as-is without stripping the crypto param. This is
  843. * necessary for software based decryption.
  844. *
  845. * If there's no error then the frame is decrypted. At least that is
  846. * the case for frames that come in via fragmented rx indication.
  847. */
  848. if (!is_decrypted)
  849. return;
  850. /* The payload is decrypted so strip crypto params. Start from tail
  851. * since hdr is used to compute some stuff.
  852. */
  853. hdr = (void *)msdu->data;
  854. /* Tail */
  855. if (status->flag & RX_FLAG_IV_STRIPPED)
  856. skb_trim(msdu, msdu->len -
  857. ath10k_htt_rx_crypto_tail_len(ar, enctype));
  858. /* MMIC */
  859. if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
  860. !ieee80211_has_morefrags(hdr->frame_control) &&
  861. enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
  862. skb_trim(msdu, msdu->len - 8);
  863. /* Head */
  864. if (status->flag & RX_FLAG_IV_STRIPPED) {
  865. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  866. crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
  867. memmove((void *)msdu->data + crypto_len,
  868. (void *)msdu->data, hdr_len);
  869. skb_pull(msdu, crypto_len);
  870. }
  871. }
  872. static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
  873. struct sk_buff *msdu,
  874. struct ieee80211_rx_status *status,
  875. const u8 first_hdr[64])
  876. {
  877. struct ieee80211_hdr *hdr;
  878. struct htt_rx_desc *rxd;
  879. size_t hdr_len;
  880. u8 da[ETH_ALEN];
  881. u8 sa[ETH_ALEN];
  882. int l3_pad_bytes;
  883. /* Delivered decapped frame:
  884. * [nwifi 802.11 header] <-- replaced with 802.11 hdr
  885. * [rfc1042/llc]
  886. *
  887. * Note: The nwifi header doesn't have QoS Control and is
  888. * (always?) a 3addr frame.
  889. *
  890. * Note2: There's no A-MSDU subframe header. Even if it's part
  891. * of an A-MSDU.
  892. */
  893. /* pull decapped header and copy SA & DA */
  894. rxd = (void *)msdu->data - sizeof(*rxd);
  895. l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
  896. skb_put(msdu, l3_pad_bytes);
  897. hdr = (struct ieee80211_hdr *)(msdu->data + l3_pad_bytes);
  898. hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
  899. ether_addr_copy(da, ieee80211_get_DA(hdr));
  900. ether_addr_copy(sa, ieee80211_get_SA(hdr));
  901. skb_pull(msdu, hdr_len);
  902. /* push original 802.11 header */
  903. hdr = (struct ieee80211_hdr *)first_hdr;
  904. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  905. memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
  906. /* original 802.11 header has a different DA and in
  907. * case of 4addr it may also have different SA
  908. */
  909. hdr = (struct ieee80211_hdr *)msdu->data;
  910. ether_addr_copy(ieee80211_get_DA(hdr), da);
  911. ether_addr_copy(ieee80211_get_SA(hdr), sa);
  912. }
  913. static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
  914. struct sk_buff *msdu,
  915. enum htt_rx_mpdu_encrypt_type enctype)
  916. {
  917. struct ieee80211_hdr *hdr;
  918. struct htt_rx_desc *rxd;
  919. size_t hdr_len, crypto_len;
  920. void *rfc1042;
  921. bool is_first, is_last, is_amsdu;
  922. int bytes_aligned = ar->hw_params.decap_align_bytes;
  923. rxd = (void *)msdu->data - sizeof(*rxd);
  924. hdr = (void *)rxd->rx_hdr_status;
  925. is_first = !!(rxd->msdu_end.common.info0 &
  926. __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
  927. is_last = !!(rxd->msdu_end.common.info0 &
  928. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
  929. is_amsdu = !(is_first && is_last);
  930. rfc1042 = hdr;
  931. if (is_first) {
  932. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  933. crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
  934. rfc1042 += round_up(hdr_len, bytes_aligned) +
  935. round_up(crypto_len, bytes_aligned);
  936. }
  937. if (is_amsdu)
  938. rfc1042 += sizeof(struct amsdu_subframe_hdr);
  939. return rfc1042;
  940. }
  941. static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
  942. struct sk_buff *msdu,
  943. struct ieee80211_rx_status *status,
  944. const u8 first_hdr[64],
  945. enum htt_rx_mpdu_encrypt_type enctype)
  946. {
  947. struct ieee80211_hdr *hdr;
  948. struct ethhdr *eth;
  949. size_t hdr_len;
  950. void *rfc1042;
  951. u8 da[ETH_ALEN];
  952. u8 sa[ETH_ALEN];
  953. int l3_pad_bytes;
  954. struct htt_rx_desc *rxd;
  955. /* Delivered decapped frame:
  956. * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
  957. * [payload]
  958. */
  959. rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
  960. if (WARN_ON_ONCE(!rfc1042))
  961. return;
  962. rxd = (void *)msdu->data - sizeof(*rxd);
  963. l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
  964. skb_put(msdu, l3_pad_bytes);
  965. skb_pull(msdu, l3_pad_bytes);
  966. /* pull decapped header and copy SA & DA */
  967. eth = (struct ethhdr *)msdu->data;
  968. ether_addr_copy(da, eth->h_dest);
  969. ether_addr_copy(sa, eth->h_source);
  970. skb_pull(msdu, sizeof(struct ethhdr));
  971. /* push rfc1042/llc/snap */
  972. memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
  973. sizeof(struct rfc1042_hdr));
  974. /* push original 802.11 header */
  975. hdr = (struct ieee80211_hdr *)first_hdr;
  976. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  977. memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
  978. /* original 802.11 header has a different DA and in
  979. * case of 4addr it may also have different SA
  980. */
  981. hdr = (struct ieee80211_hdr *)msdu->data;
  982. ether_addr_copy(ieee80211_get_DA(hdr), da);
  983. ether_addr_copy(ieee80211_get_SA(hdr), sa);
  984. }
  985. static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
  986. struct sk_buff *msdu,
  987. struct ieee80211_rx_status *status,
  988. const u8 first_hdr[64])
  989. {
  990. struct ieee80211_hdr *hdr;
  991. size_t hdr_len;
  992. int l3_pad_bytes;
  993. struct htt_rx_desc *rxd;
  994. /* Delivered decapped frame:
  995. * [amsdu header] <-- replaced with 802.11 hdr
  996. * [rfc1042/llc]
  997. * [payload]
  998. */
  999. rxd = (void *)msdu->data - sizeof(*rxd);
  1000. l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
  1001. skb_put(msdu, l3_pad_bytes);
  1002. skb_pull(msdu, sizeof(struct amsdu_subframe_hdr) + l3_pad_bytes);
  1003. hdr = (struct ieee80211_hdr *)first_hdr;
  1004. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  1005. memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
  1006. }
  1007. static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
  1008. struct sk_buff *msdu,
  1009. struct ieee80211_rx_status *status,
  1010. u8 first_hdr[64],
  1011. enum htt_rx_mpdu_encrypt_type enctype,
  1012. bool is_decrypted)
  1013. {
  1014. struct htt_rx_desc *rxd;
  1015. enum rx_msdu_decap_format decap;
  1016. /* First msdu's decapped header:
  1017. * [802.11 header] <-- padded to 4 bytes long
  1018. * [crypto param] <-- padded to 4 bytes long
  1019. * [amsdu header] <-- only if A-MSDU
  1020. * [rfc1042/llc]
  1021. *
  1022. * Other (2nd, 3rd, ..) msdu's decapped header:
  1023. * [amsdu header] <-- only if A-MSDU
  1024. * [rfc1042/llc]
  1025. */
  1026. rxd = (void *)msdu->data - sizeof(*rxd);
  1027. decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
  1028. RX_MSDU_START_INFO1_DECAP_FORMAT);
  1029. switch (decap) {
  1030. case RX_MSDU_DECAP_RAW:
  1031. ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
  1032. is_decrypted);
  1033. break;
  1034. case RX_MSDU_DECAP_NATIVE_WIFI:
  1035. ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr);
  1036. break;
  1037. case RX_MSDU_DECAP_ETHERNET2_DIX:
  1038. ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
  1039. break;
  1040. case RX_MSDU_DECAP_8023_SNAP_LLC:
  1041. ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr);
  1042. break;
  1043. }
  1044. }
  1045. static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
  1046. {
  1047. struct htt_rx_desc *rxd;
  1048. u32 flags, info;
  1049. bool is_ip4, is_ip6;
  1050. bool is_tcp, is_udp;
  1051. bool ip_csum_ok, tcpudp_csum_ok;
  1052. rxd = (void *)skb->data - sizeof(*rxd);
  1053. flags = __le32_to_cpu(rxd->attention.flags);
  1054. info = __le32_to_cpu(rxd->msdu_start.common.info1);
  1055. is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
  1056. is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
  1057. is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
  1058. is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
  1059. ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
  1060. tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
  1061. if (!is_ip4 && !is_ip6)
  1062. return CHECKSUM_NONE;
  1063. if (!is_tcp && !is_udp)
  1064. return CHECKSUM_NONE;
  1065. if (!ip_csum_ok)
  1066. return CHECKSUM_NONE;
  1067. if (!tcpudp_csum_ok)
  1068. return CHECKSUM_NONE;
  1069. return CHECKSUM_UNNECESSARY;
  1070. }
  1071. static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
  1072. {
  1073. msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
  1074. }
  1075. static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
  1076. struct sk_buff_head *amsdu,
  1077. struct ieee80211_rx_status *status)
  1078. {
  1079. struct sk_buff *first;
  1080. struct sk_buff *last;
  1081. struct sk_buff *msdu;
  1082. struct htt_rx_desc *rxd;
  1083. struct ieee80211_hdr *hdr;
  1084. enum htt_rx_mpdu_encrypt_type enctype;
  1085. u8 first_hdr[64];
  1086. u8 *qos;
  1087. size_t hdr_len;
  1088. bool has_fcs_err;
  1089. bool has_crypto_err;
  1090. bool has_tkip_err;
  1091. bool has_peer_idx_invalid;
  1092. bool is_decrypted;
  1093. bool is_mgmt;
  1094. u32 attention;
  1095. if (skb_queue_empty(amsdu))
  1096. return;
  1097. first = skb_peek(amsdu);
  1098. rxd = (void *)first->data - sizeof(*rxd);
  1099. is_mgmt = !!(rxd->attention.flags &
  1100. __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));
  1101. enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
  1102. RX_MPDU_START_INFO0_ENCRYPT_TYPE);
  1103. /* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
  1104. * decapped header. It'll be used for undecapping of each MSDU.
  1105. */
  1106. hdr = (void *)rxd->rx_hdr_status;
  1107. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  1108. memcpy(first_hdr, hdr, hdr_len);
  1109. /* Each A-MSDU subframe will use the original header as the base and be
  1110. * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
  1111. */
  1112. hdr = (void *)first_hdr;
  1113. qos = ieee80211_get_qos_ctl(hdr);
  1114. qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  1115. /* Some attention flags are valid only in the last MSDU. */
  1116. last = skb_peek_tail(amsdu);
  1117. rxd = (void *)last->data - sizeof(*rxd);
  1118. attention = __le32_to_cpu(rxd->attention.flags);
  1119. has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
  1120. has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
  1121. has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
  1122. has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);
  1123. /* Note: If hardware captures an encrypted frame that it can't decrypt,
  1124. * e.g. due to fcs error, missing peer or invalid key data it will
  1125. * report the frame as raw.
  1126. */
  1127. is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
  1128. !has_fcs_err &&
  1129. !has_crypto_err &&
  1130. !has_peer_idx_invalid);
  1131. /* Clear per-MPDU flags while leaving per-PPDU flags intact. */
  1132. status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
  1133. RX_FLAG_MMIC_ERROR |
  1134. RX_FLAG_DECRYPTED |
  1135. RX_FLAG_IV_STRIPPED |
  1136. RX_FLAG_ONLY_MONITOR |
  1137. RX_FLAG_MMIC_STRIPPED);
  1138. if (has_fcs_err)
  1139. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  1140. if (has_tkip_err)
  1141. status->flag |= RX_FLAG_MMIC_ERROR;
  1142. /* Firmware reports all necessary management frames via WMI already.
  1143. * They are not reported to monitor interfaces at all so pass the ones
  1144. * coming via HTT to monitor interfaces instead. This simplifies
  1145. * matters a lot.
  1146. */
  1147. if (is_mgmt)
  1148. status->flag |= RX_FLAG_ONLY_MONITOR;
  1149. if (is_decrypted) {
  1150. status->flag |= RX_FLAG_DECRYPTED;
  1151. if (likely(!is_mgmt))
  1152. status->flag |= RX_FLAG_IV_STRIPPED |
  1153. RX_FLAG_MMIC_STRIPPED;
  1154. }
  1155. skb_queue_walk(amsdu, msdu) {
  1156. ath10k_htt_rx_h_csum_offload(msdu);
  1157. ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
  1158. is_decrypted);
  1159. /* Undecapping involves copying the original 802.11 header back
  1160. * to sk_buff. If frame is protected and hardware has decrypted
  1161. * it then remove the protected bit.
  1162. */
  1163. if (!is_decrypted)
  1164. continue;
  1165. if (is_mgmt)
  1166. continue;
  1167. hdr = (void *)msdu->data;
  1168. hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1169. }
  1170. }
  1171. static void ath10k_htt_rx_h_deliver(struct ath10k *ar,
  1172. struct sk_buff_head *amsdu,
  1173. struct ieee80211_rx_status *status)
  1174. {
  1175. struct sk_buff *msdu;
  1176. while ((msdu = __skb_dequeue(amsdu))) {
  1177. /* Setup per-MSDU flags */
  1178. if (skb_queue_empty(amsdu))
  1179. status->flag &= ~RX_FLAG_AMSDU_MORE;
  1180. else
  1181. status->flag |= RX_FLAG_AMSDU_MORE;
  1182. ath10k_process_rx(ar, status, msdu);
  1183. }
  1184. }
  1185. static int ath10k_unchain_msdu(struct sk_buff_head *amsdu)
  1186. {
  1187. struct sk_buff *skb, *first;
  1188. int space;
  1189. int total_len = 0;
  1190. /* TODO: Might could optimize this by using
  1191. * skb_try_coalesce or similar method to
  1192. * decrease copying, or maybe get mac80211 to
  1193. * provide a way to just receive a list of
  1194. * skb?
  1195. */
  1196. first = __skb_dequeue(amsdu);
  1197. /* Allocate total length all at once. */
  1198. skb_queue_walk(amsdu, skb)
  1199. total_len += skb->len;
  1200. space = total_len - skb_tailroom(first);
  1201. if ((space > 0) &&
  1202. (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
  1203. /* TODO: bump some rx-oom error stat */
  1204. /* put it back together so we can free the
  1205. * whole list at once.
  1206. */
  1207. __skb_queue_head(amsdu, first);
  1208. return -1;
  1209. }
  1210. /* Walk list again, copying contents into
  1211. * msdu_head
  1212. */
  1213. while ((skb = __skb_dequeue(amsdu))) {
  1214. skb_copy_from_linear_data(skb, skb_put(first, skb->len),
  1215. skb->len);
  1216. dev_kfree_skb_any(skb);
  1217. }
  1218. __skb_queue_head(amsdu, first);
  1219. return 0;
  1220. }
  1221. static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
  1222. struct sk_buff_head *amsdu)
  1223. {
  1224. struct sk_buff *first;
  1225. struct htt_rx_desc *rxd;
  1226. enum rx_msdu_decap_format decap;
  1227. first = skb_peek(amsdu);
  1228. rxd = (void *)first->data - sizeof(*rxd);
  1229. decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
  1230. RX_MSDU_START_INFO1_DECAP_FORMAT);
  1231. /* FIXME: Current unchaining logic can only handle simple case of raw
  1232. * msdu chaining. If decapping is other than raw the chaining may be
  1233. * more complex and this isn't handled by the current code. Don't even
  1234. * try re-constructing such frames - it'll be pretty much garbage.
  1235. */
  1236. if (decap != RX_MSDU_DECAP_RAW ||
  1237. skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
  1238. __skb_queue_purge(amsdu);
  1239. return;
  1240. }
  1241. ath10k_unchain_msdu(amsdu);
  1242. }
  1243. static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
  1244. struct sk_buff_head *amsdu,
  1245. struct ieee80211_rx_status *rx_status)
  1246. {
  1247. /* FIXME: It might be a good idea to do some fuzzy-testing to drop
  1248. * invalid/dangerous frames.
  1249. */
  1250. if (!rx_status->freq) {
  1251. ath10k_warn(ar, "no channel configured; ignoring frame(s)!\n");
  1252. return false;
  1253. }
  1254. if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
  1255. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
  1256. return false;
  1257. }
  1258. return true;
  1259. }
  1260. static void ath10k_htt_rx_h_filter(struct ath10k *ar,
  1261. struct sk_buff_head *amsdu,
  1262. struct ieee80211_rx_status *rx_status)
  1263. {
  1264. if (skb_queue_empty(amsdu))
  1265. return;
  1266. if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
  1267. return;
  1268. __skb_queue_purge(amsdu);
  1269. }
  1270. static int ath10k_htt_rx_handle_amsdu(struct ath10k_htt *htt)
  1271. {
  1272. struct ath10k *ar = htt->ar;
  1273. struct ieee80211_rx_status *rx_status = &htt->rx_status;
  1274. struct sk_buff_head amsdu;
  1275. int ret, num_msdus;
  1276. __skb_queue_head_init(&amsdu);
  1277. spin_lock_bh(&htt->rx_ring.lock);
  1278. if (htt->rx_confused) {
  1279. spin_unlock_bh(&htt->rx_ring.lock);
  1280. return -EIO;
  1281. }
  1282. ret = ath10k_htt_rx_amsdu_pop(htt, &amsdu);
  1283. spin_unlock_bh(&htt->rx_ring.lock);
  1284. if (ret < 0) {
  1285. ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
  1286. __skb_queue_purge(&amsdu);
  1287. /* FIXME: It's probably a good idea to reboot the
  1288. * device instead of leaving it inoperable.
  1289. */
  1290. htt->rx_confused = true;
  1291. return ret;
  1292. }
  1293. num_msdus = skb_queue_len(&amsdu);
  1294. ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
  1295. /* only for ret = 1 indicates chained msdus */
  1296. if (ret > 0)
  1297. ath10k_htt_rx_h_unchain(ar, &amsdu);
  1298. ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
  1299. ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status);
  1300. ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
  1301. return num_msdus;
  1302. }
  1303. static void ath10k_htt_rx_proc_rx_ind(struct ath10k_htt *htt,
  1304. struct htt_rx_indication *rx)
  1305. {
  1306. struct ath10k *ar = htt->ar;
  1307. struct htt_rx_indication_mpdu_range *mpdu_ranges;
  1308. int num_mpdu_ranges;
  1309. int i, mpdu_count = 0;
  1310. num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
  1311. HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
  1312. mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
  1313. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
  1314. rx, sizeof(*rx) +
  1315. (sizeof(struct htt_rx_indication_mpdu_range) *
  1316. num_mpdu_ranges));
  1317. for (i = 0; i < num_mpdu_ranges; i++)
  1318. mpdu_count += mpdu_ranges[i].mpdu_count;
  1319. atomic_add(mpdu_count, &htt->num_mpdus_ready);
  1320. }
  1321. static void ath10k_htt_rx_tx_compl_ind(struct ath10k *ar,
  1322. struct sk_buff *skb)
  1323. {
  1324. struct ath10k_htt *htt = &ar->htt;
  1325. struct htt_resp *resp = (struct htt_resp *)skb->data;
  1326. struct htt_tx_done tx_done = {};
  1327. int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
  1328. __le16 msdu_id;
  1329. int i;
  1330. switch (status) {
  1331. case HTT_DATA_TX_STATUS_NO_ACK:
  1332. tx_done.status = HTT_TX_COMPL_STATE_NOACK;
  1333. break;
  1334. case HTT_DATA_TX_STATUS_OK:
  1335. tx_done.status = HTT_TX_COMPL_STATE_ACK;
  1336. break;
  1337. case HTT_DATA_TX_STATUS_DISCARD:
  1338. case HTT_DATA_TX_STATUS_POSTPONE:
  1339. case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
  1340. tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
  1341. break;
  1342. default:
  1343. ath10k_warn(ar, "unhandled tx completion status %d\n", status);
  1344. tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
  1345. break;
  1346. }
  1347. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
  1348. resp->data_tx_completion.num_msdus);
  1349. for (i = 0; i < resp->data_tx_completion.num_msdus; i++) {
  1350. msdu_id = resp->data_tx_completion.msdus[i];
  1351. tx_done.msdu_id = __le16_to_cpu(msdu_id);
  1352. /* kfifo_put: In practice firmware shouldn't fire off per-CE
  1353. * interrupt and main interrupt (MSI/-X range case) for the same
  1354. * HTC service so it should be safe to use kfifo_put w/o lock.
  1355. *
  1356. * From kfifo_put() documentation:
  1357. * Note that with only one concurrent reader and one concurrent
  1358. * writer, you don't need extra locking to use these macro.
  1359. */
  1360. if (!kfifo_put(&htt->txdone_fifo, tx_done)) {
  1361. ath10k_warn(ar, "txdone fifo overrun, msdu_id %d status %d\n",
  1362. tx_done.msdu_id, tx_done.status);
  1363. ath10k_txrx_tx_unref(htt, &tx_done);
  1364. }
  1365. }
  1366. }
  1367. static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
  1368. {
  1369. struct htt_rx_addba *ev = &resp->rx_addba;
  1370. struct ath10k_peer *peer;
  1371. struct ath10k_vif *arvif;
  1372. u16 info0, tid, peer_id;
  1373. info0 = __le16_to_cpu(ev->info0);
  1374. tid = MS(info0, HTT_RX_BA_INFO0_TID);
  1375. peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
  1376. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1377. "htt rx addba tid %hu peer_id %hu size %hhu\n",
  1378. tid, peer_id, ev->window_size);
  1379. spin_lock_bh(&ar->data_lock);
  1380. peer = ath10k_peer_find_by_id(ar, peer_id);
  1381. if (!peer) {
  1382. ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
  1383. peer_id);
  1384. spin_unlock_bh(&ar->data_lock);
  1385. return;
  1386. }
  1387. arvif = ath10k_get_arvif(ar, peer->vdev_id);
  1388. if (!arvif) {
  1389. ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
  1390. peer->vdev_id);
  1391. spin_unlock_bh(&ar->data_lock);
  1392. return;
  1393. }
  1394. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1395. "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
  1396. peer->addr, tid, ev->window_size);
  1397. ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
  1398. spin_unlock_bh(&ar->data_lock);
  1399. }
  1400. static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
  1401. {
  1402. struct htt_rx_delba *ev = &resp->rx_delba;
  1403. struct ath10k_peer *peer;
  1404. struct ath10k_vif *arvif;
  1405. u16 info0, tid, peer_id;
  1406. info0 = __le16_to_cpu(ev->info0);
  1407. tid = MS(info0, HTT_RX_BA_INFO0_TID);
  1408. peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
  1409. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1410. "htt rx delba tid %hu peer_id %hu\n",
  1411. tid, peer_id);
  1412. spin_lock_bh(&ar->data_lock);
  1413. peer = ath10k_peer_find_by_id(ar, peer_id);
  1414. if (!peer) {
  1415. ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
  1416. peer_id);
  1417. spin_unlock_bh(&ar->data_lock);
  1418. return;
  1419. }
  1420. arvif = ath10k_get_arvif(ar, peer->vdev_id);
  1421. if (!arvif) {
  1422. ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
  1423. peer->vdev_id);
  1424. spin_unlock_bh(&ar->data_lock);
  1425. return;
  1426. }
  1427. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1428. "htt rx stop rx ba session sta %pM tid %hu\n",
  1429. peer->addr, tid);
  1430. ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
  1431. spin_unlock_bh(&ar->data_lock);
  1432. }
  1433. static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
  1434. struct sk_buff_head *amsdu)
  1435. {
  1436. struct sk_buff *msdu;
  1437. struct htt_rx_desc *rxd;
  1438. if (skb_queue_empty(list))
  1439. return -ENOBUFS;
  1440. if (WARN_ON(!skb_queue_empty(amsdu)))
  1441. return -EINVAL;
  1442. while ((msdu = __skb_dequeue(list))) {
  1443. __skb_queue_tail(amsdu, msdu);
  1444. rxd = (void *)msdu->data - sizeof(*rxd);
  1445. if (rxd->msdu_end.common.info0 &
  1446. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
  1447. break;
  1448. }
  1449. msdu = skb_peek_tail(amsdu);
  1450. rxd = (void *)msdu->data - sizeof(*rxd);
  1451. if (!(rxd->msdu_end.common.info0 &
  1452. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
  1453. skb_queue_splice_init(amsdu, list);
  1454. return -EAGAIN;
  1455. }
  1456. return 0;
  1457. }
  1458. static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
  1459. struct sk_buff *skb)
  1460. {
  1461. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1462. if (!ieee80211_has_protected(hdr->frame_control))
  1463. return;
  1464. /* Offloaded frames are already decrypted but firmware insists they are
  1465. * protected in the 802.11 header. Strip the flag. Otherwise mac80211
  1466. * will drop the frame.
  1467. */
  1468. hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1469. status->flag |= RX_FLAG_DECRYPTED |
  1470. RX_FLAG_IV_STRIPPED |
  1471. RX_FLAG_MMIC_STRIPPED;
  1472. }
  1473. static int ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
  1474. struct sk_buff_head *list)
  1475. {
  1476. struct ath10k_htt *htt = &ar->htt;
  1477. struct ieee80211_rx_status *status = &htt->rx_status;
  1478. struct htt_rx_offload_msdu *rx;
  1479. struct sk_buff *msdu;
  1480. size_t offset;
  1481. int num_msdu = 0;
  1482. while ((msdu = __skb_dequeue(list))) {
  1483. /* Offloaded frames don't have Rx descriptor. Instead they have
  1484. * a short meta information header.
  1485. */
  1486. rx = (void *)msdu->data;
  1487. skb_put(msdu, sizeof(*rx));
  1488. skb_pull(msdu, sizeof(*rx));
  1489. if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
  1490. ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
  1491. dev_kfree_skb_any(msdu);
  1492. continue;
  1493. }
  1494. skb_put(msdu, __le16_to_cpu(rx->msdu_len));
  1495. /* Offloaded rx header length isn't multiple of 2 nor 4 so the
  1496. * actual payload is unaligned. Align the frame. Otherwise
  1497. * mac80211 complains. This shouldn't reduce performance much
  1498. * because these offloaded frames are rare.
  1499. */
  1500. offset = 4 - ((unsigned long)msdu->data & 3);
  1501. skb_put(msdu, offset);
  1502. memmove(msdu->data + offset, msdu->data, msdu->len);
  1503. skb_pull(msdu, offset);
  1504. /* FIXME: The frame is NWifi. Re-construct QoS Control
  1505. * if possible later.
  1506. */
  1507. memset(status, 0, sizeof(*status));
  1508. status->flag |= RX_FLAG_NO_SIGNAL_VAL;
  1509. ath10k_htt_rx_h_rx_offload_prot(status, msdu);
  1510. ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
  1511. ath10k_process_rx(ar, status, msdu);
  1512. num_msdu++;
  1513. }
  1514. return num_msdu;
  1515. }
  1516. static int ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
  1517. {
  1518. struct ath10k_htt *htt = &ar->htt;
  1519. struct htt_resp *resp = (void *)skb->data;
  1520. struct ieee80211_rx_status *status = &htt->rx_status;
  1521. struct sk_buff_head list;
  1522. struct sk_buff_head amsdu;
  1523. u16 peer_id;
  1524. u16 msdu_count;
  1525. u8 vdev_id;
  1526. u8 tid;
  1527. bool offload;
  1528. bool frag;
  1529. int ret, num_msdus = 0;
  1530. lockdep_assert_held(&htt->rx_ring.lock);
  1531. if (htt->rx_confused)
  1532. return -EIO;
  1533. skb_pull(skb, sizeof(resp->hdr));
  1534. skb_pull(skb, sizeof(resp->rx_in_ord_ind));
  1535. peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
  1536. msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
  1537. vdev_id = resp->rx_in_ord_ind.vdev_id;
  1538. tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
  1539. offload = !!(resp->rx_in_ord_ind.info &
  1540. HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
  1541. frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);
  1542. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1543. "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
  1544. vdev_id, peer_id, tid, offload, frag, msdu_count);
  1545. if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs)) {
  1546. ath10k_warn(ar, "dropping invalid in order rx indication\n");
  1547. return -EINVAL;
  1548. }
  1549. /* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
  1550. * extracted and processed.
  1551. */
  1552. __skb_queue_head_init(&list);
  1553. ret = ath10k_htt_rx_pop_paddr_list(htt, &resp->rx_in_ord_ind, &list);
  1554. if (ret < 0) {
  1555. ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
  1556. htt->rx_confused = true;
  1557. return -EIO;
  1558. }
  1559. /* Offloaded frames are very different and need to be handled
  1560. * separately.
  1561. */
  1562. if (offload)
  1563. num_msdus = ath10k_htt_rx_h_rx_offload(ar, &list);
  1564. while (!skb_queue_empty(&list)) {
  1565. __skb_queue_head_init(&amsdu);
  1566. ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
  1567. switch (ret) {
  1568. case 0:
  1569. /* Note: The in-order indication may report interleaved
  1570. * frames from different PPDUs meaning reported rx rate
  1571. * to mac80211 isn't accurate/reliable. It's still
  1572. * better to report something than nothing though. This
  1573. * should still give an idea about rx rate to the user.
  1574. */
  1575. num_msdus += skb_queue_len(&amsdu);
  1576. ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
  1577. ath10k_htt_rx_h_filter(ar, &amsdu, status);
  1578. ath10k_htt_rx_h_mpdu(ar, &amsdu, status);
  1579. ath10k_htt_rx_h_deliver(ar, &amsdu, status);
  1580. break;
  1581. case -EAGAIN:
  1582. /* fall through */
  1583. default:
  1584. /* Should not happen. */
  1585. ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
  1586. htt->rx_confused = true;
  1587. __skb_queue_purge(&list);
  1588. return -EIO;
  1589. }
  1590. }
  1591. return num_msdus;
  1592. }
  1593. static void ath10k_htt_rx_tx_fetch_resp_id_confirm(struct ath10k *ar,
  1594. const __le32 *resp_ids,
  1595. int num_resp_ids)
  1596. {
  1597. int i;
  1598. u32 resp_id;
  1599. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm num_resp_ids %d\n",
  1600. num_resp_ids);
  1601. for (i = 0; i < num_resp_ids; i++) {
  1602. resp_id = le32_to_cpu(resp_ids[i]);
  1603. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm resp_id %u\n",
  1604. resp_id);
  1605. /* TODO: free resp_id */
  1606. }
  1607. }
  1608. static void ath10k_htt_rx_tx_fetch_ind(struct ath10k *ar, struct sk_buff *skb)
  1609. {
  1610. struct ieee80211_hw *hw = ar->hw;
  1611. struct ieee80211_txq *txq;
  1612. struct htt_resp *resp = (struct htt_resp *)skb->data;
  1613. struct htt_tx_fetch_record *record;
  1614. size_t len;
  1615. size_t max_num_bytes;
  1616. size_t max_num_msdus;
  1617. size_t num_bytes;
  1618. size_t num_msdus;
  1619. const __le32 *resp_ids;
  1620. u16 num_records;
  1621. u16 num_resp_ids;
  1622. u16 peer_id;
  1623. u8 tid;
  1624. int ret;
  1625. int i;
  1626. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind\n");
  1627. len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_ind);
  1628. if (unlikely(skb->len < len)) {
  1629. ath10k_warn(ar, "received corrupted tx_fetch_ind event: buffer too short\n");
  1630. return;
  1631. }
  1632. num_records = le16_to_cpu(resp->tx_fetch_ind.num_records);
  1633. num_resp_ids = le16_to_cpu(resp->tx_fetch_ind.num_resp_ids);
  1634. len += sizeof(resp->tx_fetch_ind.records[0]) * num_records;
  1635. len += sizeof(resp->tx_fetch_ind.resp_ids[0]) * num_resp_ids;
  1636. if (unlikely(skb->len < len)) {
  1637. ath10k_warn(ar, "received corrupted tx_fetch_ind event: too many records/resp_ids\n");
  1638. return;
  1639. }
  1640. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind num records %hu num resps %hu seq %hu\n",
  1641. num_records, num_resp_ids,
  1642. le16_to_cpu(resp->tx_fetch_ind.fetch_seq_num));
  1643. if (!ar->htt.tx_q_state.enabled) {
  1644. ath10k_warn(ar, "received unexpected tx_fetch_ind event: not enabled\n");
  1645. return;
  1646. }
  1647. if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH) {
  1648. ath10k_warn(ar, "received unexpected tx_fetch_ind event: in push mode\n");
  1649. return;
  1650. }
  1651. rcu_read_lock();
  1652. for (i = 0; i < num_records; i++) {
  1653. record = &resp->tx_fetch_ind.records[i];
  1654. peer_id = MS(le16_to_cpu(record->info),
  1655. HTT_TX_FETCH_RECORD_INFO_PEER_ID);
  1656. tid = MS(le16_to_cpu(record->info),
  1657. HTT_TX_FETCH_RECORD_INFO_TID);
  1658. max_num_msdus = le16_to_cpu(record->num_msdus);
  1659. max_num_bytes = le32_to_cpu(record->num_bytes);
  1660. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch record %i peer_id %hu tid %hhu msdus %zu bytes %zu\n",
  1661. i, peer_id, tid, max_num_msdus, max_num_bytes);
  1662. if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
  1663. unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
  1664. ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
  1665. peer_id, tid);
  1666. continue;
  1667. }
  1668. spin_lock_bh(&ar->data_lock);
  1669. txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
  1670. spin_unlock_bh(&ar->data_lock);
  1671. /* It is okay to release the lock and use txq because RCU read
  1672. * lock is held.
  1673. */
  1674. if (unlikely(!txq)) {
  1675. ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
  1676. peer_id, tid);
  1677. continue;
  1678. }
  1679. num_msdus = 0;
  1680. num_bytes = 0;
  1681. while (num_msdus < max_num_msdus &&
  1682. num_bytes < max_num_bytes) {
  1683. ret = ath10k_mac_tx_push_txq(hw, txq);
  1684. if (ret < 0)
  1685. break;
  1686. num_msdus++;
  1687. num_bytes += ret;
  1688. }
  1689. record->num_msdus = cpu_to_le16(num_msdus);
  1690. record->num_bytes = cpu_to_le32(num_bytes);
  1691. ath10k_htt_tx_txq_recalc(hw, txq);
  1692. }
  1693. rcu_read_unlock();
  1694. resp_ids = ath10k_htt_get_tx_fetch_ind_resp_ids(&resp->tx_fetch_ind);
  1695. ath10k_htt_rx_tx_fetch_resp_id_confirm(ar, resp_ids, num_resp_ids);
  1696. ret = ath10k_htt_tx_fetch_resp(ar,
  1697. resp->tx_fetch_ind.token,
  1698. resp->tx_fetch_ind.fetch_seq_num,
  1699. resp->tx_fetch_ind.records,
  1700. num_records);
  1701. if (unlikely(ret)) {
  1702. ath10k_warn(ar, "failed to submit tx fetch resp for token 0x%08x: %d\n",
  1703. le32_to_cpu(resp->tx_fetch_ind.token), ret);
  1704. /* FIXME: request fw restart */
  1705. }
  1706. ath10k_htt_tx_txq_sync(ar);
  1707. }
  1708. static void ath10k_htt_rx_tx_fetch_confirm(struct ath10k *ar,
  1709. struct sk_buff *skb)
  1710. {
  1711. const struct htt_resp *resp = (void *)skb->data;
  1712. size_t len;
  1713. int num_resp_ids;
  1714. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm\n");
  1715. len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_confirm);
  1716. if (unlikely(skb->len < len)) {
  1717. ath10k_warn(ar, "received corrupted tx_fetch_confirm event: buffer too short\n");
  1718. return;
  1719. }
  1720. num_resp_ids = le16_to_cpu(resp->tx_fetch_confirm.num_resp_ids);
  1721. len += sizeof(resp->tx_fetch_confirm.resp_ids[0]) * num_resp_ids;
  1722. if (unlikely(skb->len < len)) {
  1723. ath10k_warn(ar, "received corrupted tx_fetch_confirm event: resp_ids buffer overflow\n");
  1724. return;
  1725. }
  1726. ath10k_htt_rx_tx_fetch_resp_id_confirm(ar,
  1727. resp->tx_fetch_confirm.resp_ids,
  1728. num_resp_ids);
  1729. }
  1730. static void ath10k_htt_rx_tx_mode_switch_ind(struct ath10k *ar,
  1731. struct sk_buff *skb)
  1732. {
  1733. const struct htt_resp *resp = (void *)skb->data;
  1734. const struct htt_tx_mode_switch_record *record;
  1735. struct ieee80211_txq *txq;
  1736. struct ath10k_txq *artxq;
  1737. size_t len;
  1738. size_t num_records;
  1739. enum htt_tx_mode_switch_mode mode;
  1740. bool enable;
  1741. u16 info0;
  1742. u16 info1;
  1743. u16 threshold;
  1744. u16 peer_id;
  1745. u8 tid;
  1746. int i;
  1747. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx mode switch ind\n");
  1748. len = sizeof(resp->hdr) + sizeof(resp->tx_mode_switch_ind);
  1749. if (unlikely(skb->len < len)) {
  1750. ath10k_warn(ar, "received corrupted tx_mode_switch_ind event: buffer too short\n");
  1751. return;
  1752. }
  1753. info0 = le16_to_cpu(resp->tx_mode_switch_ind.info0);
  1754. info1 = le16_to_cpu(resp->tx_mode_switch_ind.info1);
  1755. enable = !!(info0 & HTT_TX_MODE_SWITCH_IND_INFO0_ENABLE);
  1756. num_records = MS(info0, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
  1757. mode = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_MODE);
  1758. threshold = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
  1759. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1760. "htt rx tx mode switch ind info0 0x%04hx info1 0x%04hx enable %d num records %zd mode %d threshold %hu\n",
  1761. info0, info1, enable, num_records, mode, threshold);
  1762. len += sizeof(resp->tx_mode_switch_ind.records[0]) * num_records;
  1763. if (unlikely(skb->len < len)) {
  1764. ath10k_warn(ar, "received corrupted tx_mode_switch_mode_ind event: too many records\n");
  1765. return;
  1766. }
  1767. switch (mode) {
  1768. case HTT_TX_MODE_SWITCH_PUSH:
  1769. case HTT_TX_MODE_SWITCH_PUSH_PULL:
  1770. break;
  1771. default:
  1772. ath10k_warn(ar, "received invalid tx_mode_switch_mode_ind mode %d, ignoring\n",
  1773. mode);
  1774. return;
  1775. }
  1776. if (!enable)
  1777. return;
  1778. ar->htt.tx_q_state.enabled = enable;
  1779. ar->htt.tx_q_state.mode = mode;
  1780. ar->htt.tx_q_state.num_push_allowed = threshold;
  1781. rcu_read_lock();
  1782. for (i = 0; i < num_records; i++) {
  1783. record = &resp->tx_mode_switch_ind.records[i];
  1784. info0 = le16_to_cpu(record->info0);
  1785. peer_id = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_PEER_ID);
  1786. tid = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_TID);
  1787. if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
  1788. unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
  1789. ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
  1790. peer_id, tid);
  1791. continue;
  1792. }
  1793. spin_lock_bh(&ar->data_lock);
  1794. txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
  1795. spin_unlock_bh(&ar->data_lock);
  1796. /* It is okay to release the lock and use txq because RCU read
  1797. * lock is held.
  1798. */
  1799. if (unlikely(!txq)) {
  1800. ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
  1801. peer_id, tid);
  1802. continue;
  1803. }
  1804. spin_lock_bh(&ar->htt.tx_lock);
  1805. artxq = (void *)txq->drv_priv;
  1806. artxq->num_push_allowed = le16_to_cpu(record->num_max_msdus);
  1807. spin_unlock_bh(&ar->htt.tx_lock);
  1808. }
  1809. rcu_read_unlock();
  1810. ath10k_mac_tx_push_pending(ar);
  1811. }
  1812. void ath10k_htt_htc_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
  1813. {
  1814. bool release;
  1815. release = ath10k_htt_t2h_msg_handler(ar, skb);
  1816. /* Free the indication buffer */
  1817. if (release)
  1818. dev_kfree_skb_any(skb);
  1819. }
  1820. static inline bool is_valid_legacy_rate(u8 rate)
  1821. {
  1822. static const u8 legacy_rates[] = {1, 2, 5, 11, 6, 9, 12,
  1823. 18, 24, 36, 48, 54};
  1824. int i;
  1825. for (i = 0; i < ARRAY_SIZE(legacy_rates); i++) {
  1826. if (rate == legacy_rates[i])
  1827. return true;
  1828. }
  1829. return false;
  1830. }
  1831. static void
  1832. ath10k_update_per_peer_tx_stats(struct ath10k *ar,
  1833. struct ieee80211_sta *sta,
  1834. struct ath10k_per_peer_tx_stats *peer_stats)
  1835. {
  1836. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  1837. u8 rate = 0, sgi;
  1838. struct rate_info txrate;
  1839. lockdep_assert_held(&ar->data_lock);
  1840. txrate.flags = ATH10K_HW_PREAMBLE(peer_stats->ratecode);
  1841. txrate.bw = ATH10K_HW_BW(peer_stats->flags);
  1842. txrate.nss = ATH10K_HW_NSS(peer_stats->ratecode);
  1843. txrate.mcs = ATH10K_HW_MCS_RATE(peer_stats->ratecode);
  1844. sgi = ATH10K_HW_GI(peer_stats->flags);
  1845. if (((txrate.flags == WMI_RATE_PREAMBLE_HT) ||
  1846. (txrate.flags == WMI_RATE_PREAMBLE_VHT)) && txrate.mcs > 9) {
  1847. ath10k_warn(ar, "Invalid mcs %hhd peer stats", txrate.mcs);
  1848. return;
  1849. }
  1850. memset(&arsta->txrate, 0, sizeof(arsta->txrate));
  1851. if (txrate.flags == WMI_RATE_PREAMBLE_CCK ||
  1852. txrate.flags == WMI_RATE_PREAMBLE_OFDM) {
  1853. rate = ATH10K_HW_LEGACY_RATE(peer_stats->ratecode);
  1854. if (!is_valid_legacy_rate(rate)) {
  1855. ath10k_warn(ar, "Invalid legacy rate %hhd peer stats",
  1856. rate);
  1857. return;
  1858. }
  1859. /* This is hacky, FW sends CCK rate 5.5Mbps as 6 */
  1860. rate *= 10;
  1861. if (rate == 60 && txrate.flags == WMI_RATE_PREAMBLE_CCK)
  1862. rate = rate - 5;
  1863. arsta->txrate.legacy = rate;
  1864. } else if (txrate.flags == WMI_RATE_PREAMBLE_HT) {
  1865. arsta->txrate.flags = RATE_INFO_FLAGS_MCS;
  1866. arsta->txrate.mcs = txrate.mcs;
  1867. } else {
  1868. arsta->txrate.flags = RATE_INFO_FLAGS_VHT_MCS;
  1869. arsta->txrate.mcs = txrate.mcs;
  1870. }
  1871. if (sgi)
  1872. arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1873. arsta->txrate.nss = txrate.nss;
  1874. arsta->txrate.bw = txrate.bw + RATE_INFO_BW_20;
  1875. }
  1876. static void ath10k_htt_fetch_peer_stats(struct ath10k *ar,
  1877. struct sk_buff *skb)
  1878. {
  1879. struct htt_resp *resp = (struct htt_resp *)skb->data;
  1880. struct ath10k_per_peer_tx_stats *p_tx_stats = &ar->peer_tx_stats;
  1881. struct htt_per_peer_tx_stats_ind *tx_stats;
  1882. struct ieee80211_sta *sta;
  1883. struct ath10k_peer *peer;
  1884. int peer_id, i;
  1885. u8 ppdu_len, num_ppdu;
  1886. num_ppdu = resp->peer_tx_stats.num_ppdu;
  1887. ppdu_len = resp->peer_tx_stats.ppdu_len * sizeof(__le32);
  1888. if (skb->len < sizeof(struct htt_resp_hdr) + num_ppdu * ppdu_len) {
  1889. ath10k_warn(ar, "Invalid peer stats buf length %d\n", skb->len);
  1890. return;
  1891. }
  1892. tx_stats = (struct htt_per_peer_tx_stats_ind *)
  1893. (resp->peer_tx_stats.payload);
  1894. peer_id = __le16_to_cpu(tx_stats->peer_id);
  1895. rcu_read_lock();
  1896. spin_lock_bh(&ar->data_lock);
  1897. peer = ath10k_peer_find_by_id(ar, peer_id);
  1898. if (!peer) {
  1899. ath10k_warn(ar, "Invalid peer id %d peer stats buffer\n",
  1900. peer_id);
  1901. goto out;
  1902. }
  1903. sta = peer->sta;
  1904. for (i = 0; i < num_ppdu; i++) {
  1905. tx_stats = (struct htt_per_peer_tx_stats_ind *)
  1906. (resp->peer_tx_stats.payload + i * ppdu_len);
  1907. p_tx_stats->succ_bytes = __le32_to_cpu(tx_stats->succ_bytes);
  1908. p_tx_stats->retry_bytes = __le32_to_cpu(tx_stats->retry_bytes);
  1909. p_tx_stats->failed_bytes =
  1910. __le32_to_cpu(tx_stats->failed_bytes);
  1911. p_tx_stats->ratecode = tx_stats->ratecode;
  1912. p_tx_stats->flags = tx_stats->flags;
  1913. p_tx_stats->succ_pkts = __le16_to_cpu(tx_stats->succ_pkts);
  1914. p_tx_stats->retry_pkts = __le16_to_cpu(tx_stats->retry_pkts);
  1915. p_tx_stats->failed_pkts = __le16_to_cpu(tx_stats->failed_pkts);
  1916. ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
  1917. }
  1918. out:
  1919. spin_unlock_bh(&ar->data_lock);
  1920. rcu_read_unlock();
  1921. }
  1922. bool ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
  1923. {
  1924. struct ath10k_htt *htt = &ar->htt;
  1925. struct htt_resp *resp = (struct htt_resp *)skb->data;
  1926. enum htt_t2h_msg_type type;
  1927. /* confirm alignment */
  1928. if (!IS_ALIGNED((unsigned long)skb->data, 4))
  1929. ath10k_warn(ar, "unaligned htt message, expect trouble\n");
  1930. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
  1931. resp->hdr.msg_type);
  1932. if (resp->hdr.msg_type >= ar->htt.t2h_msg_types_max) {
  1933. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, unsupported msg_type: 0x%0X\n max: 0x%0X",
  1934. resp->hdr.msg_type, ar->htt.t2h_msg_types_max);
  1935. return true;
  1936. }
  1937. type = ar->htt.t2h_msg_types[resp->hdr.msg_type];
  1938. switch (type) {
  1939. case HTT_T2H_MSG_TYPE_VERSION_CONF: {
  1940. htt->target_version_major = resp->ver_resp.major;
  1941. htt->target_version_minor = resp->ver_resp.minor;
  1942. complete(&htt->target_version_received);
  1943. break;
  1944. }
  1945. case HTT_T2H_MSG_TYPE_RX_IND:
  1946. ath10k_htt_rx_proc_rx_ind(htt, &resp->rx_ind);
  1947. break;
  1948. case HTT_T2H_MSG_TYPE_PEER_MAP: {
  1949. struct htt_peer_map_event ev = {
  1950. .vdev_id = resp->peer_map.vdev_id,
  1951. .peer_id = __le16_to_cpu(resp->peer_map.peer_id),
  1952. };
  1953. memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
  1954. ath10k_peer_map_event(htt, &ev);
  1955. break;
  1956. }
  1957. case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
  1958. struct htt_peer_unmap_event ev = {
  1959. .peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
  1960. };
  1961. ath10k_peer_unmap_event(htt, &ev);
  1962. break;
  1963. }
  1964. case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
  1965. struct htt_tx_done tx_done = {};
  1966. int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
  1967. tx_done.msdu_id = __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
  1968. switch (status) {
  1969. case HTT_MGMT_TX_STATUS_OK:
  1970. tx_done.status = HTT_TX_COMPL_STATE_ACK;
  1971. break;
  1972. case HTT_MGMT_TX_STATUS_RETRY:
  1973. tx_done.status = HTT_TX_COMPL_STATE_NOACK;
  1974. break;
  1975. case HTT_MGMT_TX_STATUS_DROP:
  1976. tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
  1977. break;
  1978. }
  1979. status = ath10k_txrx_tx_unref(htt, &tx_done);
  1980. if (!status) {
  1981. spin_lock_bh(&htt->tx_lock);
  1982. ath10k_htt_tx_mgmt_dec_pending(htt);
  1983. spin_unlock_bh(&htt->tx_lock);
  1984. }
  1985. break;
  1986. }
  1987. case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
  1988. ath10k_htt_rx_tx_compl_ind(htt->ar, skb);
  1989. break;
  1990. case HTT_T2H_MSG_TYPE_SEC_IND: {
  1991. struct ath10k *ar = htt->ar;
  1992. struct htt_security_indication *ev = &resp->security_indication;
  1993. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1994. "sec ind peer_id %d unicast %d type %d\n",
  1995. __le16_to_cpu(ev->peer_id),
  1996. !!(ev->flags & HTT_SECURITY_IS_UNICAST),
  1997. MS(ev->flags, HTT_SECURITY_TYPE));
  1998. complete(&ar->install_key_done);
  1999. break;
  2000. }
  2001. case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
  2002. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
  2003. skb->data, skb->len);
  2004. atomic_inc(&htt->num_mpdus_ready);
  2005. break;
  2006. }
  2007. case HTT_T2H_MSG_TYPE_TEST:
  2008. break;
  2009. case HTT_T2H_MSG_TYPE_STATS_CONF:
  2010. trace_ath10k_htt_stats(ar, skb->data, skb->len);
  2011. break;
  2012. case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
  2013. /* Firmware can return tx frames if it's unable to fully
  2014. * process them and suspects host may be able to fix it. ath10k
  2015. * sends all tx frames as already inspected so this shouldn't
  2016. * happen unless fw has a bug.
  2017. */
  2018. ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
  2019. break;
  2020. case HTT_T2H_MSG_TYPE_RX_ADDBA:
  2021. ath10k_htt_rx_addba(ar, resp);
  2022. break;
  2023. case HTT_T2H_MSG_TYPE_RX_DELBA:
  2024. ath10k_htt_rx_delba(ar, resp);
  2025. break;
  2026. case HTT_T2H_MSG_TYPE_PKTLOG: {
  2027. trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
  2028. skb->len -
  2029. offsetof(struct htt_resp,
  2030. pktlog_msg.payload));
  2031. break;
  2032. }
  2033. case HTT_T2H_MSG_TYPE_RX_FLUSH: {
  2034. /* Ignore this event because mac80211 takes care of Rx
  2035. * aggregation reordering.
  2036. */
  2037. break;
  2038. }
  2039. case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
  2040. __skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
  2041. return false;
  2042. }
  2043. case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND:
  2044. break;
  2045. case HTT_T2H_MSG_TYPE_CHAN_CHANGE: {
  2046. u32 phymode = __le32_to_cpu(resp->chan_change.phymode);
  2047. u32 freq = __le32_to_cpu(resp->chan_change.freq);
  2048. ar->tgt_oper_chan = ieee80211_get_channel(ar->hw->wiphy, freq);
  2049. ath10k_dbg(ar, ATH10K_DBG_HTT,
  2050. "htt chan change freq %u phymode %s\n",
  2051. freq, ath10k_wmi_phymode_str(phymode));
  2052. break;
  2053. }
  2054. case HTT_T2H_MSG_TYPE_AGGR_CONF:
  2055. break;
  2056. case HTT_T2H_MSG_TYPE_TX_FETCH_IND: {
  2057. struct sk_buff *tx_fetch_ind = skb_copy(skb, GFP_ATOMIC);
  2058. if (!tx_fetch_ind) {
  2059. ath10k_warn(ar, "failed to copy htt tx fetch ind\n");
  2060. break;
  2061. }
  2062. skb_queue_tail(&htt->tx_fetch_ind_q, tx_fetch_ind);
  2063. break;
  2064. }
  2065. case HTT_T2H_MSG_TYPE_TX_FETCH_CONFIRM:
  2066. ath10k_htt_rx_tx_fetch_confirm(ar, skb);
  2067. break;
  2068. case HTT_T2H_MSG_TYPE_TX_MODE_SWITCH_IND:
  2069. ath10k_htt_rx_tx_mode_switch_ind(ar, skb);
  2070. break;
  2071. case HTT_T2H_MSG_TYPE_PEER_STATS:
  2072. ath10k_htt_fetch_peer_stats(ar, skb);
  2073. break;
  2074. case HTT_T2H_MSG_TYPE_EN_STATS:
  2075. default:
  2076. ath10k_warn(ar, "htt event (%d) not handled\n",
  2077. resp->hdr.msg_type);
  2078. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
  2079. skb->data, skb->len);
  2080. break;
  2081. }
  2082. return true;
  2083. }
  2084. EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
  2085. void ath10k_htt_rx_pktlog_completion_handler(struct ath10k *ar,
  2086. struct sk_buff *skb)
  2087. {
  2088. trace_ath10k_htt_pktlog(ar, skb->data, skb->len);
  2089. dev_kfree_skb_any(skb);
  2090. }
  2091. EXPORT_SYMBOL(ath10k_htt_rx_pktlog_completion_handler);
  2092. int ath10k_htt_txrx_compl_task(struct ath10k *ar, int budget)
  2093. {
  2094. struct ath10k_htt *htt = &ar->htt;
  2095. struct htt_tx_done tx_done = {};
  2096. struct sk_buff_head tx_ind_q;
  2097. struct sk_buff *skb;
  2098. unsigned long flags;
  2099. int quota = 0, done, num_rx_msdus;
  2100. bool resched_napi = false;
  2101. __skb_queue_head_init(&tx_ind_q);
  2102. /* Since in-ord-ind can deliver more than 1 A-MSDU in single event,
  2103. * process it first to utilize full available quota.
  2104. */
  2105. while (quota < budget) {
  2106. if (skb_queue_empty(&htt->rx_in_ord_compl_q))
  2107. break;
  2108. skb = __skb_dequeue(&htt->rx_in_ord_compl_q);
  2109. if (!skb) {
  2110. resched_napi = true;
  2111. goto exit;
  2112. }
  2113. spin_lock_bh(&htt->rx_ring.lock);
  2114. num_rx_msdus = ath10k_htt_rx_in_ord_ind(ar, skb);
  2115. spin_unlock_bh(&htt->rx_ring.lock);
  2116. if (num_rx_msdus < 0) {
  2117. resched_napi = true;
  2118. goto exit;
  2119. }
  2120. dev_kfree_skb_any(skb);
  2121. if (num_rx_msdus > 0)
  2122. quota += num_rx_msdus;
  2123. if ((quota > ATH10K_NAPI_QUOTA_LIMIT) &&
  2124. !skb_queue_empty(&htt->rx_in_ord_compl_q)) {
  2125. resched_napi = true;
  2126. goto exit;
  2127. }
  2128. }
  2129. while (quota < budget) {
  2130. /* no more data to receive */
  2131. if (!atomic_read(&htt->num_mpdus_ready))
  2132. break;
  2133. num_rx_msdus = ath10k_htt_rx_handle_amsdu(htt);
  2134. if (num_rx_msdus < 0) {
  2135. resched_napi = true;
  2136. goto exit;
  2137. }
  2138. quota += num_rx_msdus;
  2139. atomic_dec(&htt->num_mpdus_ready);
  2140. if ((quota > ATH10K_NAPI_QUOTA_LIMIT) &&
  2141. atomic_read(&htt->num_mpdus_ready)) {
  2142. resched_napi = true;
  2143. goto exit;
  2144. }
  2145. }
  2146. /* From NAPI documentation:
  2147. * The napi poll() function may also process TX completions, in which
  2148. * case if it processes the entire TX ring then it should count that
  2149. * work as the rest of the budget.
  2150. */
  2151. if ((quota < budget) && !kfifo_is_empty(&htt->txdone_fifo))
  2152. quota = budget;
  2153. /* kfifo_get: called only within txrx_tasklet so it's neatly serialized.
  2154. * From kfifo_get() documentation:
  2155. * Note that with only one concurrent reader and one concurrent writer,
  2156. * you don't need extra locking to use these macro.
  2157. */
  2158. while (kfifo_get(&htt->txdone_fifo, &tx_done))
  2159. ath10k_txrx_tx_unref(htt, &tx_done);
  2160. ath10k_mac_tx_push_pending(ar);
  2161. spin_lock_irqsave(&htt->tx_fetch_ind_q.lock, flags);
  2162. skb_queue_splice_init(&htt->tx_fetch_ind_q, &tx_ind_q);
  2163. spin_unlock_irqrestore(&htt->tx_fetch_ind_q.lock, flags);
  2164. while ((skb = __skb_dequeue(&tx_ind_q))) {
  2165. ath10k_htt_rx_tx_fetch_ind(ar, skb);
  2166. dev_kfree_skb_any(skb);
  2167. }
  2168. exit:
  2169. ath10k_htt_rx_msdu_buff_replenish(htt);
  2170. /* In case of rx failure or more data to read, report budget
  2171. * to reschedule NAPI poll
  2172. */
  2173. done = resched_napi ? budget : quota;
  2174. return done;
  2175. }
  2176. EXPORT_SYMBOL(ath10k_htt_txrx_compl_task);