htt_rx.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709
  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(struct timer_list *t)
  176. {
  177. struct ath10k_htt *htt = from_timer(htt, t, rx_ring.refill_retry_timer);
  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. timer_setup(timer, ath10k_htt_rx_ring_refill_retry, 0);
  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_AES_CCM256_WPA2:
  454. return IEEE80211_CCMP_256_HDR_LEN;
  455. case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
  456. case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
  457. return IEEE80211_GCMP_HDR_LEN;
  458. case HTT_RX_MPDU_ENCRYPT_WEP128:
  459. case HTT_RX_MPDU_ENCRYPT_WAPI:
  460. break;
  461. }
  462. ath10k_warn(ar, "unsupported encryption type %d\n", type);
  463. return 0;
  464. }
  465. #define MICHAEL_MIC_LEN 8
  466. static int ath10k_htt_rx_crypto_tail_len(struct ath10k *ar,
  467. enum htt_rx_mpdu_encrypt_type type)
  468. {
  469. switch (type) {
  470. case HTT_RX_MPDU_ENCRYPT_NONE:
  471. return 0;
  472. case HTT_RX_MPDU_ENCRYPT_WEP40:
  473. case HTT_RX_MPDU_ENCRYPT_WEP104:
  474. return IEEE80211_WEP_ICV_LEN;
  475. case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
  476. case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
  477. return IEEE80211_TKIP_ICV_LEN;
  478. case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
  479. return IEEE80211_CCMP_MIC_LEN;
  480. case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
  481. return IEEE80211_CCMP_256_MIC_LEN;
  482. case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
  483. case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
  484. return IEEE80211_GCMP_MIC_LEN;
  485. case HTT_RX_MPDU_ENCRYPT_WEP128:
  486. case HTT_RX_MPDU_ENCRYPT_WAPI:
  487. break;
  488. }
  489. ath10k_warn(ar, "unsupported encryption type %d\n", type);
  490. return 0;
  491. }
  492. struct amsdu_subframe_hdr {
  493. u8 dst[ETH_ALEN];
  494. u8 src[ETH_ALEN];
  495. __be16 len;
  496. } __packed;
  497. #define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)
  498. static void ath10k_htt_rx_h_rates(struct ath10k *ar,
  499. struct ieee80211_rx_status *status,
  500. struct htt_rx_desc *rxd)
  501. {
  502. struct ieee80211_supported_band *sband;
  503. u8 cck, rate, bw, sgi, mcs, nss;
  504. u8 preamble = 0;
  505. u8 group_id;
  506. u32 info1, info2, info3;
  507. info1 = __le32_to_cpu(rxd->ppdu_start.info1);
  508. info2 = __le32_to_cpu(rxd->ppdu_start.info2);
  509. info3 = __le32_to_cpu(rxd->ppdu_start.info3);
  510. preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
  511. switch (preamble) {
  512. case HTT_RX_LEGACY:
  513. /* To get legacy rate index band is required. Since band can't
  514. * be undefined check if freq is non-zero.
  515. */
  516. if (!status->freq)
  517. return;
  518. cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
  519. rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
  520. rate &= ~RX_PPDU_START_RATE_FLAG;
  521. sband = &ar->mac.sbands[status->band];
  522. status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate, cck);
  523. break;
  524. case HTT_RX_HT:
  525. case HTT_RX_HT_WITH_TXBF:
  526. /* HT-SIG - Table 20-11 in info2 and info3 */
  527. mcs = info2 & 0x1F;
  528. nss = mcs >> 3;
  529. bw = (info2 >> 7) & 1;
  530. sgi = (info3 >> 7) & 1;
  531. status->rate_idx = mcs;
  532. status->encoding = RX_ENC_HT;
  533. if (sgi)
  534. status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
  535. if (bw)
  536. status->bw = RATE_INFO_BW_40;
  537. break;
  538. case HTT_RX_VHT:
  539. case HTT_RX_VHT_WITH_TXBF:
  540. /* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
  541. * TODO check this
  542. */
  543. bw = info2 & 3;
  544. sgi = info3 & 1;
  545. group_id = (info2 >> 4) & 0x3F;
  546. if (GROUP_ID_IS_SU_MIMO(group_id)) {
  547. mcs = (info3 >> 4) & 0x0F;
  548. nss = ((info2 >> 10) & 0x07) + 1;
  549. } else {
  550. /* Hardware doesn't decode VHT-SIG-B into Rx descriptor
  551. * so it's impossible to decode MCS. Also since
  552. * firmware consumes Group Id Management frames host
  553. * has no knowledge regarding group/user position
  554. * mapping so it's impossible to pick the correct Nsts
  555. * from VHT-SIG-A1.
  556. *
  557. * Bandwidth and SGI are valid so report the rateinfo
  558. * on best-effort basis.
  559. */
  560. mcs = 0;
  561. nss = 1;
  562. }
  563. if (mcs > 0x09) {
  564. ath10k_warn(ar, "invalid MCS received %u\n", mcs);
  565. ath10k_warn(ar, "rxd %08x mpdu start %08x %08x msdu start %08x %08x ppdu start %08x %08x %08x %08x %08x\n",
  566. __le32_to_cpu(rxd->attention.flags),
  567. __le32_to_cpu(rxd->mpdu_start.info0),
  568. __le32_to_cpu(rxd->mpdu_start.info1),
  569. __le32_to_cpu(rxd->msdu_start.common.info0),
  570. __le32_to_cpu(rxd->msdu_start.common.info1),
  571. rxd->ppdu_start.info0,
  572. __le32_to_cpu(rxd->ppdu_start.info1),
  573. __le32_to_cpu(rxd->ppdu_start.info2),
  574. __le32_to_cpu(rxd->ppdu_start.info3),
  575. __le32_to_cpu(rxd->ppdu_start.info4));
  576. ath10k_warn(ar, "msdu end %08x mpdu end %08x\n",
  577. __le32_to_cpu(rxd->msdu_end.common.info0),
  578. __le32_to_cpu(rxd->mpdu_end.info0));
  579. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL,
  580. "rx desc msdu payload: ",
  581. rxd->msdu_payload, 50);
  582. }
  583. status->rate_idx = mcs;
  584. status->nss = nss;
  585. if (sgi)
  586. status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
  587. switch (bw) {
  588. /* 20MHZ */
  589. case 0:
  590. break;
  591. /* 40MHZ */
  592. case 1:
  593. status->bw = RATE_INFO_BW_40;
  594. break;
  595. /* 80MHZ */
  596. case 2:
  597. status->bw = RATE_INFO_BW_80;
  598. break;
  599. case 3:
  600. status->bw = RATE_INFO_BW_160;
  601. break;
  602. }
  603. status->encoding = RX_ENC_VHT;
  604. break;
  605. default:
  606. break;
  607. }
  608. }
  609. static struct ieee80211_channel *
  610. ath10k_htt_rx_h_peer_channel(struct ath10k *ar, struct htt_rx_desc *rxd)
  611. {
  612. struct ath10k_peer *peer;
  613. struct ath10k_vif *arvif;
  614. struct cfg80211_chan_def def;
  615. u16 peer_id;
  616. lockdep_assert_held(&ar->data_lock);
  617. if (!rxd)
  618. return NULL;
  619. if (rxd->attention.flags &
  620. __cpu_to_le32(RX_ATTENTION_FLAGS_PEER_IDX_INVALID))
  621. return NULL;
  622. if (!(rxd->msdu_end.common.info0 &
  623. __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU)))
  624. return NULL;
  625. peer_id = MS(__le32_to_cpu(rxd->mpdu_start.info0),
  626. RX_MPDU_START_INFO0_PEER_IDX);
  627. peer = ath10k_peer_find_by_id(ar, peer_id);
  628. if (!peer)
  629. return NULL;
  630. arvif = ath10k_get_arvif(ar, peer->vdev_id);
  631. if (WARN_ON_ONCE(!arvif))
  632. return NULL;
  633. if (ath10k_mac_vif_chan(arvif->vif, &def))
  634. return NULL;
  635. return def.chan;
  636. }
  637. static struct ieee80211_channel *
  638. ath10k_htt_rx_h_vdev_channel(struct ath10k *ar, u32 vdev_id)
  639. {
  640. struct ath10k_vif *arvif;
  641. struct cfg80211_chan_def def;
  642. lockdep_assert_held(&ar->data_lock);
  643. list_for_each_entry(arvif, &ar->arvifs, list) {
  644. if (arvif->vdev_id == vdev_id &&
  645. ath10k_mac_vif_chan(arvif->vif, &def) == 0)
  646. return def.chan;
  647. }
  648. return NULL;
  649. }
  650. static void
  651. ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw *hw,
  652. struct ieee80211_chanctx_conf *conf,
  653. void *data)
  654. {
  655. struct cfg80211_chan_def *def = data;
  656. *def = conf->def;
  657. }
  658. static struct ieee80211_channel *
  659. ath10k_htt_rx_h_any_channel(struct ath10k *ar)
  660. {
  661. struct cfg80211_chan_def def = {};
  662. ieee80211_iter_chan_contexts_atomic(ar->hw,
  663. ath10k_htt_rx_h_any_chan_iter,
  664. &def);
  665. return def.chan;
  666. }
  667. static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
  668. struct ieee80211_rx_status *status,
  669. struct htt_rx_desc *rxd,
  670. u32 vdev_id)
  671. {
  672. struct ieee80211_channel *ch;
  673. spin_lock_bh(&ar->data_lock);
  674. ch = ar->scan_channel;
  675. if (!ch)
  676. ch = ar->rx_channel;
  677. if (!ch)
  678. ch = ath10k_htt_rx_h_peer_channel(ar, rxd);
  679. if (!ch)
  680. ch = ath10k_htt_rx_h_vdev_channel(ar, vdev_id);
  681. if (!ch)
  682. ch = ath10k_htt_rx_h_any_channel(ar);
  683. if (!ch)
  684. ch = ar->tgt_oper_chan;
  685. spin_unlock_bh(&ar->data_lock);
  686. if (!ch)
  687. return false;
  688. status->band = ch->band;
  689. status->freq = ch->center_freq;
  690. return true;
  691. }
  692. static void ath10k_htt_rx_h_signal(struct ath10k *ar,
  693. struct ieee80211_rx_status *status,
  694. struct htt_rx_desc *rxd)
  695. {
  696. int i;
  697. for (i = 0; i < IEEE80211_MAX_CHAINS ; i++) {
  698. status->chains &= ~BIT(i);
  699. if (rxd->ppdu_start.rssi_chains[i].pri20_mhz != 0x80) {
  700. status->chain_signal[i] = ATH10K_DEFAULT_NOISE_FLOOR +
  701. rxd->ppdu_start.rssi_chains[i].pri20_mhz;
  702. status->chains |= BIT(i);
  703. }
  704. }
  705. /* FIXME: Get real NF */
  706. status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
  707. rxd->ppdu_start.rssi_comb;
  708. status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
  709. }
  710. static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
  711. struct ieee80211_rx_status *status,
  712. struct htt_rx_desc *rxd)
  713. {
  714. /* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
  715. * means all prior MSDUs in a PPDU are reported to mac80211 without the
  716. * TSF. Is it worth holding frames until end of PPDU is known?
  717. *
  718. * FIXME: Can we get/compute 64bit TSF?
  719. */
  720. status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
  721. status->flag |= RX_FLAG_MACTIME_END;
  722. }
  723. static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
  724. struct sk_buff_head *amsdu,
  725. struct ieee80211_rx_status *status,
  726. u32 vdev_id)
  727. {
  728. struct sk_buff *first;
  729. struct htt_rx_desc *rxd;
  730. bool is_first_ppdu;
  731. bool is_last_ppdu;
  732. if (skb_queue_empty(amsdu))
  733. return;
  734. first = skb_peek(amsdu);
  735. rxd = (void *)first->data - sizeof(*rxd);
  736. is_first_ppdu = !!(rxd->attention.flags &
  737. __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
  738. is_last_ppdu = !!(rxd->attention.flags &
  739. __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));
  740. if (is_first_ppdu) {
  741. /* New PPDU starts so clear out the old per-PPDU status. */
  742. status->freq = 0;
  743. status->rate_idx = 0;
  744. status->nss = 0;
  745. status->encoding = RX_ENC_LEGACY;
  746. status->bw = RATE_INFO_BW_20;
  747. status->flag &= ~RX_FLAG_MACTIME_END;
  748. status->flag |= RX_FLAG_NO_SIGNAL_VAL;
  749. status->flag &= ~(RX_FLAG_AMPDU_IS_LAST);
  750. status->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  751. status->ampdu_reference = ar->ampdu_reference;
  752. ath10k_htt_rx_h_signal(ar, status, rxd);
  753. ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
  754. ath10k_htt_rx_h_rates(ar, status, rxd);
  755. }
  756. if (is_last_ppdu) {
  757. ath10k_htt_rx_h_mactime(ar, status, rxd);
  758. /* set ampdu last segment flag */
  759. status->flag |= RX_FLAG_AMPDU_IS_LAST;
  760. ar->ampdu_reference++;
  761. }
  762. }
  763. static const char * const tid_to_ac[] = {
  764. "BE",
  765. "BK",
  766. "BK",
  767. "BE",
  768. "VI",
  769. "VI",
  770. "VO",
  771. "VO",
  772. };
  773. static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
  774. {
  775. u8 *qc;
  776. int tid;
  777. if (!ieee80211_is_data_qos(hdr->frame_control))
  778. return "";
  779. qc = ieee80211_get_qos_ctl(hdr);
  780. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  781. if (tid < 8)
  782. snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
  783. else
  784. snprintf(out, size, "tid %d", tid);
  785. return out;
  786. }
  787. static void ath10k_process_rx(struct ath10k *ar,
  788. struct ieee80211_rx_status *rx_status,
  789. struct sk_buff *skb)
  790. {
  791. struct ieee80211_rx_status *status;
  792. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  793. char tid[32];
  794. status = IEEE80211_SKB_RXCB(skb);
  795. *status = *rx_status;
  796. ath10k_dbg(ar, ATH10K_DBG_DATA,
  797. "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",
  798. skb,
  799. skb->len,
  800. ieee80211_get_SA(hdr),
  801. ath10k_get_tid(hdr, tid, sizeof(tid)),
  802. is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
  803. "mcast" : "ucast",
  804. (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
  805. (status->encoding == RX_ENC_LEGACY) ? "legacy" : "",
  806. (status->encoding == RX_ENC_HT) ? "ht" : "",
  807. (status->encoding == RX_ENC_VHT) ? "vht" : "",
  808. (status->bw == RATE_INFO_BW_40) ? "40" : "",
  809. (status->bw == RATE_INFO_BW_80) ? "80" : "",
  810. (status->bw == RATE_INFO_BW_160) ? "160" : "",
  811. status->enc_flags & RX_ENC_FLAG_SHORT_GI ? "sgi " : "",
  812. status->rate_idx,
  813. status->nss,
  814. status->freq,
  815. status->band, status->flag,
  816. !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
  817. !!(status->flag & RX_FLAG_MMIC_ERROR),
  818. !!(status->flag & RX_FLAG_AMSDU_MORE));
  819. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
  820. skb->data, skb->len);
  821. trace_ath10k_rx_hdr(ar, skb->data, skb->len);
  822. trace_ath10k_rx_payload(ar, skb->data, skb->len);
  823. ieee80211_rx_napi(ar->hw, NULL, skb, &ar->napi);
  824. }
  825. static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
  826. struct ieee80211_hdr *hdr)
  827. {
  828. int len = ieee80211_hdrlen(hdr->frame_control);
  829. if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
  830. ar->running_fw->fw_file.fw_features))
  831. len = round_up(len, 4);
  832. return len;
  833. }
  834. static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
  835. struct sk_buff *msdu,
  836. struct ieee80211_rx_status *status,
  837. enum htt_rx_mpdu_encrypt_type enctype,
  838. bool is_decrypted)
  839. {
  840. struct ieee80211_hdr *hdr;
  841. struct htt_rx_desc *rxd;
  842. size_t hdr_len;
  843. size_t crypto_len;
  844. bool is_first;
  845. bool is_last;
  846. rxd = (void *)msdu->data - sizeof(*rxd);
  847. is_first = !!(rxd->msdu_end.common.info0 &
  848. __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
  849. is_last = !!(rxd->msdu_end.common.info0 &
  850. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
  851. /* Delivered decapped frame:
  852. * [802.11 header]
  853. * [crypto param] <-- can be trimmed if !fcs_err &&
  854. * !decrypt_err && !peer_idx_invalid
  855. * [amsdu header] <-- only if A-MSDU
  856. * [rfc1042/llc]
  857. * [payload]
  858. * [FCS] <-- at end, needs to be trimmed
  859. */
  860. /* This probably shouldn't happen but warn just in case */
  861. if (unlikely(WARN_ON_ONCE(!is_first)))
  862. return;
  863. /* This probably shouldn't happen but warn just in case */
  864. if (unlikely(WARN_ON_ONCE(!(is_first && is_last))))
  865. return;
  866. skb_trim(msdu, msdu->len - FCS_LEN);
  867. /* In most cases this will be true for sniffed frames. It makes sense
  868. * to deliver them as-is without stripping the crypto param. This is
  869. * necessary for software based decryption.
  870. *
  871. * If there's no error then the frame is decrypted. At least that is
  872. * the case for frames that come in via fragmented rx indication.
  873. */
  874. if (!is_decrypted)
  875. return;
  876. /* The payload is decrypted so strip crypto params. Start from tail
  877. * since hdr is used to compute some stuff.
  878. */
  879. hdr = (void *)msdu->data;
  880. /* Tail */
  881. if (status->flag & RX_FLAG_IV_STRIPPED) {
  882. skb_trim(msdu, msdu->len -
  883. ath10k_htt_rx_crypto_tail_len(ar, enctype));
  884. } else {
  885. /* MIC */
  886. if ((status->flag & RX_FLAG_MIC_STRIPPED) &&
  887. enctype == HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2)
  888. skb_trim(msdu, msdu->len - 8);
  889. /* ICV */
  890. if (status->flag & RX_FLAG_ICV_STRIPPED &&
  891. enctype != HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2)
  892. skb_trim(msdu, msdu->len -
  893. ath10k_htt_rx_crypto_tail_len(ar, enctype));
  894. }
  895. /* MMIC */
  896. if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
  897. !ieee80211_has_morefrags(hdr->frame_control) &&
  898. enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
  899. skb_trim(msdu, msdu->len - 8);
  900. /* Head */
  901. if (status->flag & RX_FLAG_IV_STRIPPED) {
  902. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  903. crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
  904. memmove((void *)msdu->data + crypto_len,
  905. (void *)msdu->data, hdr_len);
  906. skb_pull(msdu, crypto_len);
  907. }
  908. }
  909. static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
  910. struct sk_buff *msdu,
  911. struct ieee80211_rx_status *status,
  912. const u8 first_hdr[64],
  913. enum htt_rx_mpdu_encrypt_type enctype)
  914. {
  915. struct ieee80211_hdr *hdr;
  916. struct htt_rx_desc *rxd;
  917. size_t hdr_len;
  918. u8 da[ETH_ALEN];
  919. u8 sa[ETH_ALEN];
  920. int l3_pad_bytes;
  921. int bytes_aligned = ar->hw_params.decap_align_bytes;
  922. /* Delivered decapped frame:
  923. * [nwifi 802.11 header] <-- replaced with 802.11 hdr
  924. * [rfc1042/llc]
  925. *
  926. * Note: The nwifi header doesn't have QoS Control and is
  927. * (always?) a 3addr frame.
  928. *
  929. * Note2: There's no A-MSDU subframe header. Even if it's part
  930. * of an A-MSDU.
  931. */
  932. /* pull decapped header and copy SA & DA */
  933. rxd = (void *)msdu->data - sizeof(*rxd);
  934. l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
  935. skb_put(msdu, l3_pad_bytes);
  936. hdr = (struct ieee80211_hdr *)(msdu->data + l3_pad_bytes);
  937. hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
  938. ether_addr_copy(da, ieee80211_get_DA(hdr));
  939. ether_addr_copy(sa, ieee80211_get_SA(hdr));
  940. skb_pull(msdu, hdr_len);
  941. /* push original 802.11 header */
  942. hdr = (struct ieee80211_hdr *)first_hdr;
  943. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  944. if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
  945. memcpy(skb_push(msdu,
  946. ath10k_htt_rx_crypto_param_len(ar, enctype)),
  947. (void *)hdr + round_up(hdr_len, bytes_aligned),
  948. ath10k_htt_rx_crypto_param_len(ar, enctype));
  949. }
  950. memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
  951. /* original 802.11 header has a different DA and in
  952. * case of 4addr it may also have different SA
  953. */
  954. hdr = (struct ieee80211_hdr *)msdu->data;
  955. ether_addr_copy(ieee80211_get_DA(hdr), da);
  956. ether_addr_copy(ieee80211_get_SA(hdr), sa);
  957. }
  958. static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
  959. struct sk_buff *msdu,
  960. enum htt_rx_mpdu_encrypt_type enctype)
  961. {
  962. struct ieee80211_hdr *hdr;
  963. struct htt_rx_desc *rxd;
  964. size_t hdr_len, crypto_len;
  965. void *rfc1042;
  966. bool is_first, is_last, is_amsdu;
  967. int bytes_aligned = ar->hw_params.decap_align_bytes;
  968. rxd = (void *)msdu->data - sizeof(*rxd);
  969. hdr = (void *)rxd->rx_hdr_status;
  970. is_first = !!(rxd->msdu_end.common.info0 &
  971. __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
  972. is_last = !!(rxd->msdu_end.common.info0 &
  973. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
  974. is_amsdu = !(is_first && is_last);
  975. rfc1042 = hdr;
  976. if (is_first) {
  977. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  978. crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
  979. rfc1042 += round_up(hdr_len, bytes_aligned) +
  980. round_up(crypto_len, bytes_aligned);
  981. }
  982. if (is_amsdu)
  983. rfc1042 += sizeof(struct amsdu_subframe_hdr);
  984. return rfc1042;
  985. }
  986. static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
  987. struct sk_buff *msdu,
  988. struct ieee80211_rx_status *status,
  989. const u8 first_hdr[64],
  990. enum htt_rx_mpdu_encrypt_type enctype)
  991. {
  992. struct ieee80211_hdr *hdr;
  993. struct ethhdr *eth;
  994. size_t hdr_len;
  995. void *rfc1042;
  996. u8 da[ETH_ALEN];
  997. u8 sa[ETH_ALEN];
  998. int l3_pad_bytes;
  999. struct htt_rx_desc *rxd;
  1000. int bytes_aligned = ar->hw_params.decap_align_bytes;
  1001. /* Delivered decapped frame:
  1002. * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
  1003. * [payload]
  1004. */
  1005. rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
  1006. if (WARN_ON_ONCE(!rfc1042))
  1007. return;
  1008. rxd = (void *)msdu->data - sizeof(*rxd);
  1009. l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
  1010. skb_put(msdu, l3_pad_bytes);
  1011. skb_pull(msdu, l3_pad_bytes);
  1012. /* pull decapped header and copy SA & DA */
  1013. eth = (struct ethhdr *)msdu->data;
  1014. ether_addr_copy(da, eth->h_dest);
  1015. ether_addr_copy(sa, eth->h_source);
  1016. skb_pull(msdu, sizeof(struct ethhdr));
  1017. /* push rfc1042/llc/snap */
  1018. memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
  1019. sizeof(struct rfc1042_hdr));
  1020. /* push original 802.11 header */
  1021. hdr = (struct ieee80211_hdr *)first_hdr;
  1022. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  1023. if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
  1024. memcpy(skb_push(msdu,
  1025. ath10k_htt_rx_crypto_param_len(ar, enctype)),
  1026. (void *)hdr + round_up(hdr_len, bytes_aligned),
  1027. ath10k_htt_rx_crypto_param_len(ar, enctype));
  1028. }
  1029. memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
  1030. /* original 802.11 header has a different DA and in
  1031. * case of 4addr it may also have different SA
  1032. */
  1033. hdr = (struct ieee80211_hdr *)msdu->data;
  1034. ether_addr_copy(ieee80211_get_DA(hdr), da);
  1035. ether_addr_copy(ieee80211_get_SA(hdr), sa);
  1036. }
  1037. static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
  1038. struct sk_buff *msdu,
  1039. struct ieee80211_rx_status *status,
  1040. const u8 first_hdr[64],
  1041. enum htt_rx_mpdu_encrypt_type enctype)
  1042. {
  1043. struct ieee80211_hdr *hdr;
  1044. size_t hdr_len;
  1045. int l3_pad_bytes;
  1046. struct htt_rx_desc *rxd;
  1047. int bytes_aligned = ar->hw_params.decap_align_bytes;
  1048. /* Delivered decapped frame:
  1049. * [amsdu header] <-- replaced with 802.11 hdr
  1050. * [rfc1042/llc]
  1051. * [payload]
  1052. */
  1053. rxd = (void *)msdu->data - sizeof(*rxd);
  1054. l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
  1055. skb_put(msdu, l3_pad_bytes);
  1056. skb_pull(msdu, sizeof(struct amsdu_subframe_hdr) + l3_pad_bytes);
  1057. hdr = (struct ieee80211_hdr *)first_hdr;
  1058. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  1059. if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
  1060. memcpy(skb_push(msdu,
  1061. ath10k_htt_rx_crypto_param_len(ar, enctype)),
  1062. (void *)hdr + round_up(hdr_len, bytes_aligned),
  1063. ath10k_htt_rx_crypto_param_len(ar, enctype));
  1064. }
  1065. memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
  1066. }
  1067. static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
  1068. struct sk_buff *msdu,
  1069. struct ieee80211_rx_status *status,
  1070. u8 first_hdr[64],
  1071. enum htt_rx_mpdu_encrypt_type enctype,
  1072. bool is_decrypted)
  1073. {
  1074. struct htt_rx_desc *rxd;
  1075. enum rx_msdu_decap_format decap;
  1076. /* First msdu's decapped header:
  1077. * [802.11 header] <-- padded to 4 bytes long
  1078. * [crypto param] <-- padded to 4 bytes long
  1079. * [amsdu header] <-- only if A-MSDU
  1080. * [rfc1042/llc]
  1081. *
  1082. * Other (2nd, 3rd, ..) msdu's decapped header:
  1083. * [amsdu header] <-- only if A-MSDU
  1084. * [rfc1042/llc]
  1085. */
  1086. rxd = (void *)msdu->data - sizeof(*rxd);
  1087. decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
  1088. RX_MSDU_START_INFO1_DECAP_FORMAT);
  1089. switch (decap) {
  1090. case RX_MSDU_DECAP_RAW:
  1091. ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
  1092. is_decrypted);
  1093. break;
  1094. case RX_MSDU_DECAP_NATIVE_WIFI:
  1095. ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr,
  1096. enctype);
  1097. break;
  1098. case RX_MSDU_DECAP_ETHERNET2_DIX:
  1099. ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
  1100. break;
  1101. case RX_MSDU_DECAP_8023_SNAP_LLC:
  1102. ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr,
  1103. enctype);
  1104. break;
  1105. }
  1106. }
  1107. static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
  1108. {
  1109. struct htt_rx_desc *rxd;
  1110. u32 flags, info;
  1111. bool is_ip4, is_ip6;
  1112. bool is_tcp, is_udp;
  1113. bool ip_csum_ok, tcpudp_csum_ok;
  1114. rxd = (void *)skb->data - sizeof(*rxd);
  1115. flags = __le32_to_cpu(rxd->attention.flags);
  1116. info = __le32_to_cpu(rxd->msdu_start.common.info1);
  1117. is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
  1118. is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
  1119. is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
  1120. is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
  1121. ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
  1122. tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
  1123. if (!is_ip4 && !is_ip6)
  1124. return CHECKSUM_NONE;
  1125. if (!is_tcp && !is_udp)
  1126. return CHECKSUM_NONE;
  1127. if (!ip_csum_ok)
  1128. return CHECKSUM_NONE;
  1129. if (!tcpudp_csum_ok)
  1130. return CHECKSUM_NONE;
  1131. return CHECKSUM_UNNECESSARY;
  1132. }
  1133. static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
  1134. {
  1135. msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
  1136. }
  1137. static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
  1138. struct sk_buff_head *amsdu,
  1139. struct ieee80211_rx_status *status,
  1140. bool fill_crypt_header)
  1141. {
  1142. struct sk_buff *first;
  1143. struct sk_buff *last;
  1144. struct sk_buff *msdu;
  1145. struct htt_rx_desc *rxd;
  1146. struct ieee80211_hdr *hdr;
  1147. enum htt_rx_mpdu_encrypt_type enctype;
  1148. u8 first_hdr[64];
  1149. u8 *qos;
  1150. bool has_fcs_err;
  1151. bool has_crypto_err;
  1152. bool has_tkip_err;
  1153. bool has_peer_idx_invalid;
  1154. bool is_decrypted;
  1155. bool is_mgmt;
  1156. u32 attention;
  1157. if (skb_queue_empty(amsdu))
  1158. return;
  1159. first = skb_peek(amsdu);
  1160. rxd = (void *)first->data - sizeof(*rxd);
  1161. is_mgmt = !!(rxd->attention.flags &
  1162. __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));
  1163. enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
  1164. RX_MPDU_START_INFO0_ENCRYPT_TYPE);
  1165. /* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
  1166. * decapped header. It'll be used for undecapping of each MSDU.
  1167. */
  1168. hdr = (void *)rxd->rx_hdr_status;
  1169. memcpy(first_hdr, hdr, RX_HTT_HDR_STATUS_LEN);
  1170. /* Each A-MSDU subframe will use the original header as the base and be
  1171. * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
  1172. */
  1173. hdr = (void *)first_hdr;
  1174. if (ieee80211_is_data_qos(hdr->frame_control)) {
  1175. qos = ieee80211_get_qos_ctl(hdr);
  1176. qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  1177. }
  1178. /* Some attention flags are valid only in the last MSDU. */
  1179. last = skb_peek_tail(amsdu);
  1180. rxd = (void *)last->data - sizeof(*rxd);
  1181. attention = __le32_to_cpu(rxd->attention.flags);
  1182. has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
  1183. has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
  1184. has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
  1185. has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);
  1186. /* Note: If hardware captures an encrypted frame that it can't decrypt,
  1187. * e.g. due to fcs error, missing peer or invalid key data it will
  1188. * report the frame as raw.
  1189. */
  1190. is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
  1191. !has_fcs_err &&
  1192. !has_crypto_err &&
  1193. !has_peer_idx_invalid);
  1194. /* Clear per-MPDU flags while leaving per-PPDU flags intact. */
  1195. status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
  1196. RX_FLAG_MMIC_ERROR |
  1197. RX_FLAG_DECRYPTED |
  1198. RX_FLAG_IV_STRIPPED |
  1199. RX_FLAG_ONLY_MONITOR |
  1200. RX_FLAG_MMIC_STRIPPED);
  1201. if (has_fcs_err)
  1202. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  1203. if (has_tkip_err)
  1204. status->flag |= RX_FLAG_MMIC_ERROR;
  1205. /* Firmware reports all necessary management frames via WMI already.
  1206. * They are not reported to monitor interfaces at all so pass the ones
  1207. * coming via HTT to monitor interfaces instead. This simplifies
  1208. * matters a lot.
  1209. */
  1210. if (is_mgmt)
  1211. status->flag |= RX_FLAG_ONLY_MONITOR;
  1212. if (is_decrypted) {
  1213. status->flag |= RX_FLAG_DECRYPTED;
  1214. if (likely(!is_mgmt))
  1215. status->flag |= RX_FLAG_MMIC_STRIPPED;
  1216. if (fill_crypt_header)
  1217. status->flag |= RX_FLAG_MIC_STRIPPED |
  1218. RX_FLAG_ICV_STRIPPED;
  1219. else
  1220. status->flag |= RX_FLAG_IV_STRIPPED;
  1221. }
  1222. skb_queue_walk(amsdu, msdu) {
  1223. ath10k_htt_rx_h_csum_offload(msdu);
  1224. ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
  1225. is_decrypted);
  1226. /* Undecapping involves copying the original 802.11 header back
  1227. * to sk_buff. If frame is protected and hardware has decrypted
  1228. * it then remove the protected bit.
  1229. */
  1230. if (!is_decrypted)
  1231. continue;
  1232. if (is_mgmt)
  1233. continue;
  1234. if (fill_crypt_header)
  1235. continue;
  1236. hdr = (void *)msdu->data;
  1237. hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1238. }
  1239. }
  1240. static void ath10k_htt_rx_h_deliver(struct ath10k *ar,
  1241. struct sk_buff_head *amsdu,
  1242. struct ieee80211_rx_status *status)
  1243. {
  1244. struct sk_buff *msdu;
  1245. struct sk_buff *first_subframe;
  1246. first_subframe = skb_peek(amsdu);
  1247. while ((msdu = __skb_dequeue(amsdu))) {
  1248. /* Setup per-MSDU flags */
  1249. if (skb_queue_empty(amsdu))
  1250. status->flag &= ~RX_FLAG_AMSDU_MORE;
  1251. else
  1252. status->flag |= RX_FLAG_AMSDU_MORE;
  1253. if (msdu == first_subframe) {
  1254. first_subframe = NULL;
  1255. status->flag &= ~RX_FLAG_ALLOW_SAME_PN;
  1256. } else {
  1257. status->flag |= RX_FLAG_ALLOW_SAME_PN;
  1258. }
  1259. ath10k_process_rx(ar, status, msdu);
  1260. }
  1261. }
  1262. static int ath10k_unchain_msdu(struct sk_buff_head *amsdu)
  1263. {
  1264. struct sk_buff *skb, *first;
  1265. int space;
  1266. int total_len = 0;
  1267. /* TODO: Might could optimize this by using
  1268. * skb_try_coalesce or similar method to
  1269. * decrease copying, or maybe get mac80211 to
  1270. * provide a way to just receive a list of
  1271. * skb?
  1272. */
  1273. first = __skb_dequeue(amsdu);
  1274. /* Allocate total length all at once. */
  1275. skb_queue_walk(amsdu, skb)
  1276. total_len += skb->len;
  1277. space = total_len - skb_tailroom(first);
  1278. if ((space > 0) &&
  1279. (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
  1280. /* TODO: bump some rx-oom error stat */
  1281. /* put it back together so we can free the
  1282. * whole list at once.
  1283. */
  1284. __skb_queue_head(amsdu, first);
  1285. return -1;
  1286. }
  1287. /* Walk list again, copying contents into
  1288. * msdu_head
  1289. */
  1290. while ((skb = __skb_dequeue(amsdu))) {
  1291. skb_copy_from_linear_data(skb, skb_put(first, skb->len),
  1292. skb->len);
  1293. dev_kfree_skb_any(skb);
  1294. }
  1295. __skb_queue_head(amsdu, first);
  1296. return 0;
  1297. }
  1298. static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
  1299. struct sk_buff_head *amsdu)
  1300. {
  1301. struct sk_buff *first;
  1302. struct htt_rx_desc *rxd;
  1303. enum rx_msdu_decap_format decap;
  1304. first = skb_peek(amsdu);
  1305. rxd = (void *)first->data - sizeof(*rxd);
  1306. decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
  1307. RX_MSDU_START_INFO1_DECAP_FORMAT);
  1308. /* FIXME: Current unchaining logic can only handle simple case of raw
  1309. * msdu chaining. If decapping is other than raw the chaining may be
  1310. * more complex and this isn't handled by the current code. Don't even
  1311. * try re-constructing such frames - it'll be pretty much garbage.
  1312. */
  1313. if (decap != RX_MSDU_DECAP_RAW ||
  1314. skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
  1315. __skb_queue_purge(amsdu);
  1316. return;
  1317. }
  1318. ath10k_unchain_msdu(amsdu);
  1319. }
  1320. static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
  1321. struct sk_buff_head *amsdu,
  1322. struct ieee80211_rx_status *rx_status)
  1323. {
  1324. /* FIXME: It might be a good idea to do some fuzzy-testing to drop
  1325. * invalid/dangerous frames.
  1326. */
  1327. if (!rx_status->freq) {
  1328. ath10k_dbg(ar, ATH10K_DBG_HTT, "no channel configured; ignoring frame(s)!\n");
  1329. return false;
  1330. }
  1331. if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
  1332. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
  1333. return false;
  1334. }
  1335. return true;
  1336. }
  1337. static void ath10k_htt_rx_h_filter(struct ath10k *ar,
  1338. struct sk_buff_head *amsdu,
  1339. struct ieee80211_rx_status *rx_status)
  1340. {
  1341. if (skb_queue_empty(amsdu))
  1342. return;
  1343. if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
  1344. return;
  1345. __skb_queue_purge(amsdu);
  1346. }
  1347. static int ath10k_htt_rx_handle_amsdu(struct ath10k_htt *htt)
  1348. {
  1349. struct ath10k *ar = htt->ar;
  1350. struct ieee80211_rx_status *rx_status = &htt->rx_status;
  1351. struct sk_buff_head amsdu;
  1352. int ret, num_msdus;
  1353. __skb_queue_head_init(&amsdu);
  1354. spin_lock_bh(&htt->rx_ring.lock);
  1355. if (htt->rx_confused) {
  1356. spin_unlock_bh(&htt->rx_ring.lock);
  1357. return -EIO;
  1358. }
  1359. ret = ath10k_htt_rx_amsdu_pop(htt, &amsdu);
  1360. spin_unlock_bh(&htt->rx_ring.lock);
  1361. if (ret < 0) {
  1362. ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
  1363. __skb_queue_purge(&amsdu);
  1364. /* FIXME: It's probably a good idea to reboot the
  1365. * device instead of leaving it inoperable.
  1366. */
  1367. htt->rx_confused = true;
  1368. return ret;
  1369. }
  1370. num_msdus = skb_queue_len(&amsdu);
  1371. ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
  1372. /* only for ret = 1 indicates chained msdus */
  1373. if (ret > 0)
  1374. ath10k_htt_rx_h_unchain(ar, &amsdu);
  1375. ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
  1376. ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status, true);
  1377. ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
  1378. return num_msdus;
  1379. }
  1380. static void ath10k_htt_rx_proc_rx_ind(struct ath10k_htt *htt,
  1381. struct htt_rx_indication *rx)
  1382. {
  1383. struct ath10k *ar = htt->ar;
  1384. struct htt_rx_indication_mpdu_range *mpdu_ranges;
  1385. int num_mpdu_ranges;
  1386. int i, mpdu_count = 0;
  1387. num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
  1388. HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
  1389. mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
  1390. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
  1391. rx, sizeof(*rx) +
  1392. (sizeof(struct htt_rx_indication_mpdu_range) *
  1393. num_mpdu_ranges));
  1394. for (i = 0; i < num_mpdu_ranges; i++)
  1395. mpdu_count += mpdu_ranges[i].mpdu_count;
  1396. atomic_add(mpdu_count, &htt->num_mpdus_ready);
  1397. }
  1398. static void ath10k_htt_rx_tx_compl_ind(struct ath10k *ar,
  1399. struct sk_buff *skb)
  1400. {
  1401. struct ath10k_htt *htt = &ar->htt;
  1402. struct htt_resp *resp = (struct htt_resp *)skb->data;
  1403. struct htt_tx_done tx_done = {};
  1404. int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
  1405. __le16 msdu_id;
  1406. int i;
  1407. switch (status) {
  1408. case HTT_DATA_TX_STATUS_NO_ACK:
  1409. tx_done.status = HTT_TX_COMPL_STATE_NOACK;
  1410. break;
  1411. case HTT_DATA_TX_STATUS_OK:
  1412. tx_done.status = HTT_TX_COMPL_STATE_ACK;
  1413. break;
  1414. case HTT_DATA_TX_STATUS_DISCARD:
  1415. case HTT_DATA_TX_STATUS_POSTPONE:
  1416. case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
  1417. tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
  1418. break;
  1419. default:
  1420. ath10k_warn(ar, "unhandled tx completion status %d\n", status);
  1421. tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
  1422. break;
  1423. }
  1424. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
  1425. resp->data_tx_completion.num_msdus);
  1426. for (i = 0; i < resp->data_tx_completion.num_msdus; i++) {
  1427. msdu_id = resp->data_tx_completion.msdus[i];
  1428. tx_done.msdu_id = __le16_to_cpu(msdu_id);
  1429. /* kfifo_put: In practice firmware shouldn't fire off per-CE
  1430. * interrupt and main interrupt (MSI/-X range case) for the same
  1431. * HTC service so it should be safe to use kfifo_put w/o lock.
  1432. *
  1433. * From kfifo_put() documentation:
  1434. * Note that with only one concurrent reader and one concurrent
  1435. * writer, you don't need extra locking to use these macro.
  1436. */
  1437. if (!kfifo_put(&htt->txdone_fifo, tx_done)) {
  1438. ath10k_warn(ar, "txdone fifo overrun, msdu_id %d status %d\n",
  1439. tx_done.msdu_id, tx_done.status);
  1440. ath10k_txrx_tx_unref(htt, &tx_done);
  1441. }
  1442. }
  1443. }
  1444. static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
  1445. {
  1446. struct htt_rx_addba *ev = &resp->rx_addba;
  1447. struct ath10k_peer *peer;
  1448. struct ath10k_vif *arvif;
  1449. u16 info0, tid, peer_id;
  1450. info0 = __le16_to_cpu(ev->info0);
  1451. tid = MS(info0, HTT_RX_BA_INFO0_TID);
  1452. peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
  1453. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1454. "htt rx addba tid %hu peer_id %hu size %hhu\n",
  1455. tid, peer_id, ev->window_size);
  1456. spin_lock_bh(&ar->data_lock);
  1457. peer = ath10k_peer_find_by_id(ar, peer_id);
  1458. if (!peer) {
  1459. ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
  1460. peer_id);
  1461. spin_unlock_bh(&ar->data_lock);
  1462. return;
  1463. }
  1464. arvif = ath10k_get_arvif(ar, peer->vdev_id);
  1465. if (!arvif) {
  1466. ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
  1467. peer->vdev_id);
  1468. spin_unlock_bh(&ar->data_lock);
  1469. return;
  1470. }
  1471. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1472. "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
  1473. peer->addr, tid, ev->window_size);
  1474. ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
  1475. spin_unlock_bh(&ar->data_lock);
  1476. }
  1477. static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
  1478. {
  1479. struct htt_rx_delba *ev = &resp->rx_delba;
  1480. struct ath10k_peer *peer;
  1481. struct ath10k_vif *arvif;
  1482. u16 info0, tid, peer_id;
  1483. info0 = __le16_to_cpu(ev->info0);
  1484. tid = MS(info0, HTT_RX_BA_INFO0_TID);
  1485. peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
  1486. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1487. "htt rx delba tid %hu peer_id %hu\n",
  1488. tid, peer_id);
  1489. spin_lock_bh(&ar->data_lock);
  1490. peer = ath10k_peer_find_by_id(ar, peer_id);
  1491. if (!peer) {
  1492. ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
  1493. peer_id);
  1494. spin_unlock_bh(&ar->data_lock);
  1495. return;
  1496. }
  1497. arvif = ath10k_get_arvif(ar, peer->vdev_id);
  1498. if (!arvif) {
  1499. ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
  1500. peer->vdev_id);
  1501. spin_unlock_bh(&ar->data_lock);
  1502. return;
  1503. }
  1504. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1505. "htt rx stop rx ba session sta %pM tid %hu\n",
  1506. peer->addr, tid);
  1507. ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
  1508. spin_unlock_bh(&ar->data_lock);
  1509. }
  1510. static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
  1511. struct sk_buff_head *amsdu)
  1512. {
  1513. struct sk_buff *msdu;
  1514. struct htt_rx_desc *rxd;
  1515. if (skb_queue_empty(list))
  1516. return -ENOBUFS;
  1517. if (WARN_ON(!skb_queue_empty(amsdu)))
  1518. return -EINVAL;
  1519. while ((msdu = __skb_dequeue(list))) {
  1520. __skb_queue_tail(amsdu, msdu);
  1521. rxd = (void *)msdu->data - sizeof(*rxd);
  1522. if (rxd->msdu_end.common.info0 &
  1523. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
  1524. break;
  1525. }
  1526. msdu = skb_peek_tail(amsdu);
  1527. rxd = (void *)msdu->data - sizeof(*rxd);
  1528. if (!(rxd->msdu_end.common.info0 &
  1529. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
  1530. skb_queue_splice_init(amsdu, list);
  1531. return -EAGAIN;
  1532. }
  1533. return 0;
  1534. }
  1535. static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
  1536. struct sk_buff *skb)
  1537. {
  1538. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1539. if (!ieee80211_has_protected(hdr->frame_control))
  1540. return;
  1541. /* Offloaded frames are already decrypted but firmware insists they are
  1542. * protected in the 802.11 header. Strip the flag. Otherwise mac80211
  1543. * will drop the frame.
  1544. */
  1545. hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1546. status->flag |= RX_FLAG_DECRYPTED |
  1547. RX_FLAG_IV_STRIPPED |
  1548. RX_FLAG_MMIC_STRIPPED;
  1549. }
  1550. static int ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
  1551. struct sk_buff_head *list)
  1552. {
  1553. struct ath10k_htt *htt = &ar->htt;
  1554. struct ieee80211_rx_status *status = &htt->rx_status;
  1555. struct htt_rx_offload_msdu *rx;
  1556. struct sk_buff *msdu;
  1557. size_t offset;
  1558. int num_msdu = 0;
  1559. while ((msdu = __skb_dequeue(list))) {
  1560. /* Offloaded frames don't have Rx descriptor. Instead they have
  1561. * a short meta information header.
  1562. */
  1563. rx = (void *)msdu->data;
  1564. skb_put(msdu, sizeof(*rx));
  1565. skb_pull(msdu, sizeof(*rx));
  1566. if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
  1567. ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
  1568. dev_kfree_skb_any(msdu);
  1569. continue;
  1570. }
  1571. skb_put(msdu, __le16_to_cpu(rx->msdu_len));
  1572. /* Offloaded rx header length isn't multiple of 2 nor 4 so the
  1573. * actual payload is unaligned. Align the frame. Otherwise
  1574. * mac80211 complains. This shouldn't reduce performance much
  1575. * because these offloaded frames are rare.
  1576. */
  1577. offset = 4 - ((unsigned long)msdu->data & 3);
  1578. skb_put(msdu, offset);
  1579. memmove(msdu->data + offset, msdu->data, msdu->len);
  1580. skb_pull(msdu, offset);
  1581. /* FIXME: The frame is NWifi. Re-construct QoS Control
  1582. * if possible later.
  1583. */
  1584. memset(status, 0, sizeof(*status));
  1585. status->flag |= RX_FLAG_NO_SIGNAL_VAL;
  1586. ath10k_htt_rx_h_rx_offload_prot(status, msdu);
  1587. ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
  1588. ath10k_process_rx(ar, status, msdu);
  1589. num_msdu++;
  1590. }
  1591. return num_msdu;
  1592. }
  1593. static int ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
  1594. {
  1595. struct ath10k_htt *htt = &ar->htt;
  1596. struct htt_resp *resp = (void *)skb->data;
  1597. struct ieee80211_rx_status *status = &htt->rx_status;
  1598. struct sk_buff_head list;
  1599. struct sk_buff_head amsdu;
  1600. u16 peer_id;
  1601. u16 msdu_count;
  1602. u8 vdev_id;
  1603. u8 tid;
  1604. bool offload;
  1605. bool frag;
  1606. int ret, num_msdus = 0;
  1607. lockdep_assert_held(&htt->rx_ring.lock);
  1608. if (htt->rx_confused)
  1609. return -EIO;
  1610. skb_pull(skb, sizeof(resp->hdr));
  1611. skb_pull(skb, sizeof(resp->rx_in_ord_ind));
  1612. peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
  1613. msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
  1614. vdev_id = resp->rx_in_ord_ind.vdev_id;
  1615. tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
  1616. offload = !!(resp->rx_in_ord_ind.info &
  1617. HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
  1618. frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);
  1619. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1620. "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
  1621. vdev_id, peer_id, tid, offload, frag, msdu_count);
  1622. if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs)) {
  1623. ath10k_warn(ar, "dropping invalid in order rx indication\n");
  1624. return -EINVAL;
  1625. }
  1626. /* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
  1627. * extracted and processed.
  1628. */
  1629. __skb_queue_head_init(&list);
  1630. ret = ath10k_htt_rx_pop_paddr_list(htt, &resp->rx_in_ord_ind, &list);
  1631. if (ret < 0) {
  1632. ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
  1633. htt->rx_confused = true;
  1634. return -EIO;
  1635. }
  1636. /* Offloaded frames are very different and need to be handled
  1637. * separately.
  1638. */
  1639. if (offload)
  1640. num_msdus = ath10k_htt_rx_h_rx_offload(ar, &list);
  1641. while (!skb_queue_empty(&list)) {
  1642. __skb_queue_head_init(&amsdu);
  1643. ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
  1644. switch (ret) {
  1645. case 0:
  1646. /* Note: The in-order indication may report interleaved
  1647. * frames from different PPDUs meaning reported rx rate
  1648. * to mac80211 isn't accurate/reliable. It's still
  1649. * better to report something than nothing though. This
  1650. * should still give an idea about rx rate to the user.
  1651. */
  1652. num_msdus += skb_queue_len(&amsdu);
  1653. ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
  1654. ath10k_htt_rx_h_filter(ar, &amsdu, status);
  1655. ath10k_htt_rx_h_mpdu(ar, &amsdu, status, false);
  1656. ath10k_htt_rx_h_deliver(ar, &amsdu, status);
  1657. break;
  1658. case -EAGAIN:
  1659. /* fall through */
  1660. default:
  1661. /* Should not happen. */
  1662. ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
  1663. htt->rx_confused = true;
  1664. __skb_queue_purge(&list);
  1665. return -EIO;
  1666. }
  1667. }
  1668. return num_msdus;
  1669. }
  1670. static void ath10k_htt_rx_tx_fetch_resp_id_confirm(struct ath10k *ar,
  1671. const __le32 *resp_ids,
  1672. int num_resp_ids)
  1673. {
  1674. int i;
  1675. u32 resp_id;
  1676. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm num_resp_ids %d\n",
  1677. num_resp_ids);
  1678. for (i = 0; i < num_resp_ids; i++) {
  1679. resp_id = le32_to_cpu(resp_ids[i]);
  1680. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm resp_id %u\n",
  1681. resp_id);
  1682. /* TODO: free resp_id */
  1683. }
  1684. }
  1685. static void ath10k_htt_rx_tx_fetch_ind(struct ath10k *ar, struct sk_buff *skb)
  1686. {
  1687. struct ieee80211_hw *hw = ar->hw;
  1688. struct ieee80211_txq *txq;
  1689. struct htt_resp *resp = (struct htt_resp *)skb->data;
  1690. struct htt_tx_fetch_record *record;
  1691. size_t len;
  1692. size_t max_num_bytes;
  1693. size_t max_num_msdus;
  1694. size_t num_bytes;
  1695. size_t num_msdus;
  1696. const __le32 *resp_ids;
  1697. u16 num_records;
  1698. u16 num_resp_ids;
  1699. u16 peer_id;
  1700. u8 tid;
  1701. int ret;
  1702. int i;
  1703. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind\n");
  1704. len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_ind);
  1705. if (unlikely(skb->len < len)) {
  1706. ath10k_warn(ar, "received corrupted tx_fetch_ind event: buffer too short\n");
  1707. return;
  1708. }
  1709. num_records = le16_to_cpu(resp->tx_fetch_ind.num_records);
  1710. num_resp_ids = le16_to_cpu(resp->tx_fetch_ind.num_resp_ids);
  1711. len += sizeof(resp->tx_fetch_ind.records[0]) * num_records;
  1712. len += sizeof(resp->tx_fetch_ind.resp_ids[0]) * num_resp_ids;
  1713. if (unlikely(skb->len < len)) {
  1714. ath10k_warn(ar, "received corrupted tx_fetch_ind event: too many records/resp_ids\n");
  1715. return;
  1716. }
  1717. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind num records %hu num resps %hu seq %hu\n",
  1718. num_records, num_resp_ids,
  1719. le16_to_cpu(resp->tx_fetch_ind.fetch_seq_num));
  1720. if (!ar->htt.tx_q_state.enabled) {
  1721. ath10k_warn(ar, "received unexpected tx_fetch_ind event: not enabled\n");
  1722. return;
  1723. }
  1724. if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH) {
  1725. ath10k_warn(ar, "received unexpected tx_fetch_ind event: in push mode\n");
  1726. return;
  1727. }
  1728. rcu_read_lock();
  1729. for (i = 0; i < num_records; i++) {
  1730. record = &resp->tx_fetch_ind.records[i];
  1731. peer_id = MS(le16_to_cpu(record->info),
  1732. HTT_TX_FETCH_RECORD_INFO_PEER_ID);
  1733. tid = MS(le16_to_cpu(record->info),
  1734. HTT_TX_FETCH_RECORD_INFO_TID);
  1735. max_num_msdus = le16_to_cpu(record->num_msdus);
  1736. max_num_bytes = le32_to_cpu(record->num_bytes);
  1737. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch record %i peer_id %hu tid %hhu msdus %zu bytes %zu\n",
  1738. i, peer_id, tid, max_num_msdus, max_num_bytes);
  1739. if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
  1740. unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
  1741. ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
  1742. peer_id, tid);
  1743. continue;
  1744. }
  1745. spin_lock_bh(&ar->data_lock);
  1746. txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
  1747. spin_unlock_bh(&ar->data_lock);
  1748. /* It is okay to release the lock and use txq because RCU read
  1749. * lock is held.
  1750. */
  1751. if (unlikely(!txq)) {
  1752. ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
  1753. peer_id, tid);
  1754. continue;
  1755. }
  1756. num_msdus = 0;
  1757. num_bytes = 0;
  1758. while (num_msdus < max_num_msdus &&
  1759. num_bytes < max_num_bytes) {
  1760. ret = ath10k_mac_tx_push_txq(hw, txq);
  1761. if (ret < 0)
  1762. break;
  1763. num_msdus++;
  1764. num_bytes += ret;
  1765. }
  1766. record->num_msdus = cpu_to_le16(num_msdus);
  1767. record->num_bytes = cpu_to_le32(num_bytes);
  1768. ath10k_htt_tx_txq_recalc(hw, txq);
  1769. }
  1770. rcu_read_unlock();
  1771. resp_ids = ath10k_htt_get_tx_fetch_ind_resp_ids(&resp->tx_fetch_ind);
  1772. ath10k_htt_rx_tx_fetch_resp_id_confirm(ar, resp_ids, num_resp_ids);
  1773. ret = ath10k_htt_tx_fetch_resp(ar,
  1774. resp->tx_fetch_ind.token,
  1775. resp->tx_fetch_ind.fetch_seq_num,
  1776. resp->tx_fetch_ind.records,
  1777. num_records);
  1778. if (unlikely(ret)) {
  1779. ath10k_warn(ar, "failed to submit tx fetch resp for token 0x%08x: %d\n",
  1780. le32_to_cpu(resp->tx_fetch_ind.token), ret);
  1781. /* FIXME: request fw restart */
  1782. }
  1783. ath10k_htt_tx_txq_sync(ar);
  1784. }
  1785. static void ath10k_htt_rx_tx_fetch_confirm(struct ath10k *ar,
  1786. struct sk_buff *skb)
  1787. {
  1788. const struct htt_resp *resp = (void *)skb->data;
  1789. size_t len;
  1790. int num_resp_ids;
  1791. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm\n");
  1792. len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_confirm);
  1793. if (unlikely(skb->len < len)) {
  1794. ath10k_warn(ar, "received corrupted tx_fetch_confirm event: buffer too short\n");
  1795. return;
  1796. }
  1797. num_resp_ids = le16_to_cpu(resp->tx_fetch_confirm.num_resp_ids);
  1798. len += sizeof(resp->tx_fetch_confirm.resp_ids[0]) * num_resp_ids;
  1799. if (unlikely(skb->len < len)) {
  1800. ath10k_warn(ar, "received corrupted tx_fetch_confirm event: resp_ids buffer overflow\n");
  1801. return;
  1802. }
  1803. ath10k_htt_rx_tx_fetch_resp_id_confirm(ar,
  1804. resp->tx_fetch_confirm.resp_ids,
  1805. num_resp_ids);
  1806. }
  1807. static void ath10k_htt_rx_tx_mode_switch_ind(struct ath10k *ar,
  1808. struct sk_buff *skb)
  1809. {
  1810. const struct htt_resp *resp = (void *)skb->data;
  1811. const struct htt_tx_mode_switch_record *record;
  1812. struct ieee80211_txq *txq;
  1813. struct ath10k_txq *artxq;
  1814. size_t len;
  1815. size_t num_records;
  1816. enum htt_tx_mode_switch_mode mode;
  1817. bool enable;
  1818. u16 info0;
  1819. u16 info1;
  1820. u16 threshold;
  1821. u16 peer_id;
  1822. u8 tid;
  1823. int i;
  1824. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx mode switch ind\n");
  1825. len = sizeof(resp->hdr) + sizeof(resp->tx_mode_switch_ind);
  1826. if (unlikely(skb->len < len)) {
  1827. ath10k_warn(ar, "received corrupted tx_mode_switch_ind event: buffer too short\n");
  1828. return;
  1829. }
  1830. info0 = le16_to_cpu(resp->tx_mode_switch_ind.info0);
  1831. info1 = le16_to_cpu(resp->tx_mode_switch_ind.info1);
  1832. enable = !!(info0 & HTT_TX_MODE_SWITCH_IND_INFO0_ENABLE);
  1833. num_records = MS(info0, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
  1834. mode = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_MODE);
  1835. threshold = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
  1836. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1837. "htt rx tx mode switch ind info0 0x%04hx info1 0x%04hx enable %d num records %zd mode %d threshold %hu\n",
  1838. info0, info1, enable, num_records, mode, threshold);
  1839. len += sizeof(resp->tx_mode_switch_ind.records[0]) * num_records;
  1840. if (unlikely(skb->len < len)) {
  1841. ath10k_warn(ar, "received corrupted tx_mode_switch_mode_ind event: too many records\n");
  1842. return;
  1843. }
  1844. switch (mode) {
  1845. case HTT_TX_MODE_SWITCH_PUSH:
  1846. case HTT_TX_MODE_SWITCH_PUSH_PULL:
  1847. break;
  1848. default:
  1849. ath10k_warn(ar, "received invalid tx_mode_switch_mode_ind mode %d, ignoring\n",
  1850. mode);
  1851. return;
  1852. }
  1853. if (!enable)
  1854. return;
  1855. ar->htt.tx_q_state.enabled = enable;
  1856. ar->htt.tx_q_state.mode = mode;
  1857. ar->htt.tx_q_state.num_push_allowed = threshold;
  1858. rcu_read_lock();
  1859. for (i = 0; i < num_records; i++) {
  1860. record = &resp->tx_mode_switch_ind.records[i];
  1861. info0 = le16_to_cpu(record->info0);
  1862. peer_id = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_PEER_ID);
  1863. tid = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_TID);
  1864. if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
  1865. unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
  1866. ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
  1867. peer_id, tid);
  1868. continue;
  1869. }
  1870. spin_lock_bh(&ar->data_lock);
  1871. txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
  1872. spin_unlock_bh(&ar->data_lock);
  1873. /* It is okay to release the lock and use txq because RCU read
  1874. * lock is held.
  1875. */
  1876. if (unlikely(!txq)) {
  1877. ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
  1878. peer_id, tid);
  1879. continue;
  1880. }
  1881. spin_lock_bh(&ar->htt.tx_lock);
  1882. artxq = (void *)txq->drv_priv;
  1883. artxq->num_push_allowed = le16_to_cpu(record->num_max_msdus);
  1884. spin_unlock_bh(&ar->htt.tx_lock);
  1885. }
  1886. rcu_read_unlock();
  1887. ath10k_mac_tx_push_pending(ar);
  1888. }
  1889. void ath10k_htt_htc_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
  1890. {
  1891. bool release;
  1892. release = ath10k_htt_t2h_msg_handler(ar, skb);
  1893. /* Free the indication buffer */
  1894. if (release)
  1895. dev_kfree_skb_any(skb);
  1896. }
  1897. static inline bool is_valid_legacy_rate(u8 rate)
  1898. {
  1899. static const u8 legacy_rates[] = {1, 2, 5, 11, 6, 9, 12,
  1900. 18, 24, 36, 48, 54};
  1901. int i;
  1902. for (i = 0; i < ARRAY_SIZE(legacy_rates); i++) {
  1903. if (rate == legacy_rates[i])
  1904. return true;
  1905. }
  1906. return false;
  1907. }
  1908. static void
  1909. ath10k_update_per_peer_tx_stats(struct ath10k *ar,
  1910. struct ieee80211_sta *sta,
  1911. struct ath10k_per_peer_tx_stats *peer_stats)
  1912. {
  1913. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  1914. u8 rate = 0, sgi;
  1915. struct rate_info txrate;
  1916. lockdep_assert_held(&ar->data_lock);
  1917. txrate.flags = ATH10K_HW_PREAMBLE(peer_stats->ratecode);
  1918. txrate.bw = ATH10K_HW_BW(peer_stats->flags);
  1919. txrate.nss = ATH10K_HW_NSS(peer_stats->ratecode);
  1920. txrate.mcs = ATH10K_HW_MCS_RATE(peer_stats->ratecode);
  1921. sgi = ATH10K_HW_GI(peer_stats->flags);
  1922. if (txrate.flags == WMI_RATE_PREAMBLE_VHT && txrate.mcs > 9) {
  1923. ath10k_warn(ar, "Invalid VHT mcs %hhd peer stats", txrate.mcs);
  1924. return;
  1925. }
  1926. if (txrate.flags == WMI_RATE_PREAMBLE_HT &&
  1927. (txrate.mcs > 7 || txrate.nss < 1)) {
  1928. ath10k_warn(ar, "Invalid HT mcs %hhd nss %hhd peer stats",
  1929. txrate.mcs, txrate.nss);
  1930. return;
  1931. }
  1932. memset(&arsta->txrate, 0, sizeof(arsta->txrate));
  1933. if (txrate.flags == WMI_RATE_PREAMBLE_CCK ||
  1934. txrate.flags == WMI_RATE_PREAMBLE_OFDM) {
  1935. rate = ATH10K_HW_LEGACY_RATE(peer_stats->ratecode);
  1936. if (!is_valid_legacy_rate(rate)) {
  1937. ath10k_warn(ar, "Invalid legacy rate %hhd peer stats",
  1938. rate);
  1939. return;
  1940. }
  1941. /* This is hacky, FW sends CCK rate 5.5Mbps as 6 */
  1942. rate *= 10;
  1943. if (rate == 60 && txrate.flags == WMI_RATE_PREAMBLE_CCK)
  1944. rate = rate - 5;
  1945. arsta->txrate.legacy = rate;
  1946. } else if (txrate.flags == WMI_RATE_PREAMBLE_HT) {
  1947. arsta->txrate.flags = RATE_INFO_FLAGS_MCS;
  1948. arsta->txrate.mcs = txrate.mcs + 8 * (txrate.nss - 1);
  1949. } else {
  1950. arsta->txrate.flags = RATE_INFO_FLAGS_VHT_MCS;
  1951. arsta->txrate.mcs = txrate.mcs;
  1952. }
  1953. if (sgi)
  1954. arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1955. arsta->txrate.nss = txrate.nss;
  1956. arsta->txrate.bw = txrate.bw + RATE_INFO_BW_20;
  1957. }
  1958. static void ath10k_htt_fetch_peer_stats(struct ath10k *ar,
  1959. struct sk_buff *skb)
  1960. {
  1961. struct htt_resp *resp = (struct htt_resp *)skb->data;
  1962. struct ath10k_per_peer_tx_stats *p_tx_stats = &ar->peer_tx_stats;
  1963. struct htt_per_peer_tx_stats_ind *tx_stats;
  1964. struct ieee80211_sta *sta;
  1965. struct ath10k_peer *peer;
  1966. int peer_id, i;
  1967. u8 ppdu_len, num_ppdu;
  1968. num_ppdu = resp->peer_tx_stats.num_ppdu;
  1969. ppdu_len = resp->peer_tx_stats.ppdu_len * sizeof(__le32);
  1970. if (skb->len < sizeof(struct htt_resp_hdr) + num_ppdu * ppdu_len) {
  1971. ath10k_warn(ar, "Invalid peer stats buf length %d\n", skb->len);
  1972. return;
  1973. }
  1974. tx_stats = (struct htt_per_peer_tx_stats_ind *)
  1975. (resp->peer_tx_stats.payload);
  1976. peer_id = __le16_to_cpu(tx_stats->peer_id);
  1977. rcu_read_lock();
  1978. spin_lock_bh(&ar->data_lock);
  1979. peer = ath10k_peer_find_by_id(ar, peer_id);
  1980. if (!peer) {
  1981. ath10k_warn(ar, "Invalid peer id %d peer stats buffer\n",
  1982. peer_id);
  1983. goto out;
  1984. }
  1985. sta = peer->sta;
  1986. for (i = 0; i < num_ppdu; i++) {
  1987. tx_stats = (struct htt_per_peer_tx_stats_ind *)
  1988. (resp->peer_tx_stats.payload + i * ppdu_len);
  1989. p_tx_stats->succ_bytes = __le32_to_cpu(tx_stats->succ_bytes);
  1990. p_tx_stats->retry_bytes = __le32_to_cpu(tx_stats->retry_bytes);
  1991. p_tx_stats->failed_bytes =
  1992. __le32_to_cpu(tx_stats->failed_bytes);
  1993. p_tx_stats->ratecode = tx_stats->ratecode;
  1994. p_tx_stats->flags = tx_stats->flags;
  1995. p_tx_stats->succ_pkts = __le16_to_cpu(tx_stats->succ_pkts);
  1996. p_tx_stats->retry_pkts = __le16_to_cpu(tx_stats->retry_pkts);
  1997. p_tx_stats->failed_pkts = __le16_to_cpu(tx_stats->failed_pkts);
  1998. ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
  1999. }
  2000. out:
  2001. spin_unlock_bh(&ar->data_lock);
  2002. rcu_read_unlock();
  2003. }
  2004. bool ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
  2005. {
  2006. struct ath10k_htt *htt = &ar->htt;
  2007. struct htt_resp *resp = (struct htt_resp *)skb->data;
  2008. enum htt_t2h_msg_type type;
  2009. /* confirm alignment */
  2010. if (!IS_ALIGNED((unsigned long)skb->data, 4))
  2011. ath10k_warn(ar, "unaligned htt message, expect trouble\n");
  2012. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
  2013. resp->hdr.msg_type);
  2014. if (resp->hdr.msg_type >= ar->htt.t2h_msg_types_max) {
  2015. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, unsupported msg_type: 0x%0X\n max: 0x%0X",
  2016. resp->hdr.msg_type, ar->htt.t2h_msg_types_max);
  2017. return true;
  2018. }
  2019. type = ar->htt.t2h_msg_types[resp->hdr.msg_type];
  2020. switch (type) {
  2021. case HTT_T2H_MSG_TYPE_VERSION_CONF: {
  2022. htt->target_version_major = resp->ver_resp.major;
  2023. htt->target_version_minor = resp->ver_resp.minor;
  2024. complete(&htt->target_version_received);
  2025. break;
  2026. }
  2027. case HTT_T2H_MSG_TYPE_RX_IND:
  2028. ath10k_htt_rx_proc_rx_ind(htt, &resp->rx_ind);
  2029. break;
  2030. case HTT_T2H_MSG_TYPE_PEER_MAP: {
  2031. struct htt_peer_map_event ev = {
  2032. .vdev_id = resp->peer_map.vdev_id,
  2033. .peer_id = __le16_to_cpu(resp->peer_map.peer_id),
  2034. };
  2035. memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
  2036. ath10k_peer_map_event(htt, &ev);
  2037. break;
  2038. }
  2039. case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
  2040. struct htt_peer_unmap_event ev = {
  2041. .peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
  2042. };
  2043. ath10k_peer_unmap_event(htt, &ev);
  2044. break;
  2045. }
  2046. case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
  2047. struct htt_tx_done tx_done = {};
  2048. int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
  2049. tx_done.msdu_id = __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
  2050. switch (status) {
  2051. case HTT_MGMT_TX_STATUS_OK:
  2052. tx_done.status = HTT_TX_COMPL_STATE_ACK;
  2053. break;
  2054. case HTT_MGMT_TX_STATUS_RETRY:
  2055. tx_done.status = HTT_TX_COMPL_STATE_NOACK;
  2056. break;
  2057. case HTT_MGMT_TX_STATUS_DROP:
  2058. tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
  2059. break;
  2060. }
  2061. status = ath10k_txrx_tx_unref(htt, &tx_done);
  2062. if (!status) {
  2063. spin_lock_bh(&htt->tx_lock);
  2064. ath10k_htt_tx_mgmt_dec_pending(htt);
  2065. spin_unlock_bh(&htt->tx_lock);
  2066. }
  2067. break;
  2068. }
  2069. case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
  2070. ath10k_htt_rx_tx_compl_ind(htt->ar, skb);
  2071. break;
  2072. case HTT_T2H_MSG_TYPE_SEC_IND: {
  2073. struct ath10k *ar = htt->ar;
  2074. struct htt_security_indication *ev = &resp->security_indication;
  2075. ath10k_dbg(ar, ATH10K_DBG_HTT,
  2076. "sec ind peer_id %d unicast %d type %d\n",
  2077. __le16_to_cpu(ev->peer_id),
  2078. !!(ev->flags & HTT_SECURITY_IS_UNICAST),
  2079. MS(ev->flags, HTT_SECURITY_TYPE));
  2080. complete(&ar->install_key_done);
  2081. break;
  2082. }
  2083. case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
  2084. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
  2085. skb->data, skb->len);
  2086. atomic_inc(&htt->num_mpdus_ready);
  2087. break;
  2088. }
  2089. case HTT_T2H_MSG_TYPE_TEST:
  2090. break;
  2091. case HTT_T2H_MSG_TYPE_STATS_CONF:
  2092. trace_ath10k_htt_stats(ar, skb->data, skb->len);
  2093. break;
  2094. case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
  2095. /* Firmware can return tx frames if it's unable to fully
  2096. * process them and suspects host may be able to fix it. ath10k
  2097. * sends all tx frames as already inspected so this shouldn't
  2098. * happen unless fw has a bug.
  2099. */
  2100. ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
  2101. break;
  2102. case HTT_T2H_MSG_TYPE_RX_ADDBA:
  2103. ath10k_htt_rx_addba(ar, resp);
  2104. break;
  2105. case HTT_T2H_MSG_TYPE_RX_DELBA:
  2106. ath10k_htt_rx_delba(ar, resp);
  2107. break;
  2108. case HTT_T2H_MSG_TYPE_PKTLOG: {
  2109. trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
  2110. skb->len -
  2111. offsetof(struct htt_resp,
  2112. pktlog_msg.payload));
  2113. break;
  2114. }
  2115. case HTT_T2H_MSG_TYPE_RX_FLUSH: {
  2116. /* Ignore this event because mac80211 takes care of Rx
  2117. * aggregation reordering.
  2118. */
  2119. break;
  2120. }
  2121. case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
  2122. __skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
  2123. return false;
  2124. }
  2125. case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND:
  2126. break;
  2127. case HTT_T2H_MSG_TYPE_CHAN_CHANGE: {
  2128. u32 phymode = __le32_to_cpu(resp->chan_change.phymode);
  2129. u32 freq = __le32_to_cpu(resp->chan_change.freq);
  2130. ar->tgt_oper_chan = ieee80211_get_channel(ar->hw->wiphy, freq);
  2131. ath10k_dbg(ar, ATH10K_DBG_HTT,
  2132. "htt chan change freq %u phymode %s\n",
  2133. freq, ath10k_wmi_phymode_str(phymode));
  2134. break;
  2135. }
  2136. case HTT_T2H_MSG_TYPE_AGGR_CONF:
  2137. break;
  2138. case HTT_T2H_MSG_TYPE_TX_FETCH_IND: {
  2139. struct sk_buff *tx_fetch_ind = skb_copy(skb, GFP_ATOMIC);
  2140. if (!tx_fetch_ind) {
  2141. ath10k_warn(ar, "failed to copy htt tx fetch ind\n");
  2142. break;
  2143. }
  2144. skb_queue_tail(&htt->tx_fetch_ind_q, tx_fetch_ind);
  2145. break;
  2146. }
  2147. case HTT_T2H_MSG_TYPE_TX_FETCH_CONFIRM:
  2148. ath10k_htt_rx_tx_fetch_confirm(ar, skb);
  2149. break;
  2150. case HTT_T2H_MSG_TYPE_TX_MODE_SWITCH_IND:
  2151. ath10k_htt_rx_tx_mode_switch_ind(ar, skb);
  2152. break;
  2153. case HTT_T2H_MSG_TYPE_PEER_STATS:
  2154. ath10k_htt_fetch_peer_stats(ar, skb);
  2155. break;
  2156. case HTT_T2H_MSG_TYPE_EN_STATS:
  2157. default:
  2158. ath10k_warn(ar, "htt event (%d) not handled\n",
  2159. resp->hdr.msg_type);
  2160. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
  2161. skb->data, skb->len);
  2162. break;
  2163. }
  2164. return true;
  2165. }
  2166. EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
  2167. void ath10k_htt_rx_pktlog_completion_handler(struct ath10k *ar,
  2168. struct sk_buff *skb)
  2169. {
  2170. trace_ath10k_htt_pktlog(ar, skb->data, skb->len);
  2171. dev_kfree_skb_any(skb);
  2172. }
  2173. EXPORT_SYMBOL(ath10k_htt_rx_pktlog_completion_handler);
  2174. int ath10k_htt_txrx_compl_task(struct ath10k *ar, int budget)
  2175. {
  2176. struct ath10k_htt *htt = &ar->htt;
  2177. struct htt_tx_done tx_done = {};
  2178. struct sk_buff_head tx_ind_q;
  2179. struct sk_buff *skb;
  2180. unsigned long flags;
  2181. int quota = 0, done, num_rx_msdus;
  2182. bool resched_napi = false;
  2183. __skb_queue_head_init(&tx_ind_q);
  2184. /* Since in-ord-ind can deliver more than 1 A-MSDU in single event,
  2185. * process it first to utilize full available quota.
  2186. */
  2187. while (quota < budget) {
  2188. if (skb_queue_empty(&htt->rx_in_ord_compl_q))
  2189. break;
  2190. skb = __skb_dequeue(&htt->rx_in_ord_compl_q);
  2191. if (!skb) {
  2192. resched_napi = true;
  2193. goto exit;
  2194. }
  2195. spin_lock_bh(&htt->rx_ring.lock);
  2196. num_rx_msdus = ath10k_htt_rx_in_ord_ind(ar, skb);
  2197. spin_unlock_bh(&htt->rx_ring.lock);
  2198. if (num_rx_msdus < 0) {
  2199. resched_napi = true;
  2200. goto exit;
  2201. }
  2202. dev_kfree_skb_any(skb);
  2203. if (num_rx_msdus > 0)
  2204. quota += num_rx_msdus;
  2205. if ((quota > ATH10K_NAPI_QUOTA_LIMIT) &&
  2206. !skb_queue_empty(&htt->rx_in_ord_compl_q)) {
  2207. resched_napi = true;
  2208. goto exit;
  2209. }
  2210. }
  2211. while (quota < budget) {
  2212. /* no more data to receive */
  2213. if (!atomic_read(&htt->num_mpdus_ready))
  2214. break;
  2215. num_rx_msdus = ath10k_htt_rx_handle_amsdu(htt);
  2216. if (num_rx_msdus < 0) {
  2217. resched_napi = true;
  2218. goto exit;
  2219. }
  2220. quota += num_rx_msdus;
  2221. atomic_dec(&htt->num_mpdus_ready);
  2222. if ((quota > ATH10K_NAPI_QUOTA_LIMIT) &&
  2223. atomic_read(&htt->num_mpdus_ready)) {
  2224. resched_napi = true;
  2225. goto exit;
  2226. }
  2227. }
  2228. /* From NAPI documentation:
  2229. * The napi poll() function may also process TX completions, in which
  2230. * case if it processes the entire TX ring then it should count that
  2231. * work as the rest of the budget.
  2232. */
  2233. if ((quota < budget) && !kfifo_is_empty(&htt->txdone_fifo))
  2234. quota = budget;
  2235. /* kfifo_get: called only within txrx_tasklet so it's neatly serialized.
  2236. * From kfifo_get() documentation:
  2237. * Note that with only one concurrent reader and one concurrent writer,
  2238. * you don't need extra locking to use these macro.
  2239. */
  2240. while (kfifo_get(&htt->txdone_fifo, &tx_done))
  2241. ath10k_txrx_tx_unref(htt, &tx_done);
  2242. ath10k_mac_tx_push_pending(ar);
  2243. spin_lock_irqsave(&htt->tx_fetch_ind_q.lock, flags);
  2244. skb_queue_splice_init(&htt->tx_fetch_ind_q, &tx_ind_q);
  2245. spin_unlock_irqrestore(&htt->tx_fetch_ind_q.lock, flags);
  2246. while ((skb = __skb_dequeue(&tx_ind_q))) {
  2247. ath10k_htt_rx_tx_fetch_ind(ar, skb);
  2248. dev_kfree_skb_any(skb);
  2249. }
  2250. exit:
  2251. ath10k_htt_rx_msdu_buff_replenish(htt);
  2252. /* In case of rx failure or more data to read, report budget
  2253. * to reschedule NAPI poll
  2254. */
  2255. done = resched_napi ? budget : quota;
  2256. return done;
  2257. }
  2258. EXPORT_SYMBOL(ath10k_htt_txrx_compl_task);