htt_rx.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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. static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);
  30. static void ath10k_htt_txrx_compl_task(unsigned long ptr);
  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. spin_lock_bh(&htt->rx_ring.lock);
  155. num_deficit = htt->rx_ring.fill_level - htt->rx_ring.fill_cnt;
  156. num_to_fill = min(ATH10K_HTT_MAX_NUM_REFILL, num_deficit);
  157. num_deficit -= num_to_fill;
  158. ret = ath10k_htt_rx_ring_fill_n(htt, num_to_fill);
  159. if (ret == -ENOMEM) {
  160. /*
  161. * Failed to fill it to the desired level -
  162. * we'll start a timer and try again next time.
  163. * As long as enough buffers are left in the ring for
  164. * another A-MPDU rx, no special recovery is needed.
  165. */
  166. mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
  167. msecs_to_jiffies(HTT_RX_RING_REFILL_RETRY_MS));
  168. } else if (num_deficit > 0) {
  169. tasklet_schedule(&htt->rx_replenish_task);
  170. }
  171. spin_unlock_bh(&htt->rx_ring.lock);
  172. }
  173. static void ath10k_htt_rx_ring_refill_retry(unsigned long arg)
  174. {
  175. struct ath10k_htt *htt = (struct ath10k_htt *)arg;
  176. ath10k_htt_rx_msdu_buff_replenish(htt);
  177. }
  178. int ath10k_htt_rx_ring_refill(struct ath10k *ar)
  179. {
  180. struct ath10k_htt *htt = &ar->htt;
  181. int ret;
  182. spin_lock_bh(&htt->rx_ring.lock);
  183. ret = ath10k_htt_rx_ring_fill_n(htt, (htt->rx_ring.fill_level -
  184. htt->rx_ring.fill_cnt));
  185. spin_unlock_bh(&htt->rx_ring.lock);
  186. if (ret)
  187. ath10k_htt_rx_ring_free(htt);
  188. return ret;
  189. }
  190. void ath10k_htt_rx_free(struct ath10k_htt *htt)
  191. {
  192. del_timer_sync(&htt->rx_ring.refill_retry_timer);
  193. tasklet_kill(&htt->rx_replenish_task);
  194. tasklet_kill(&htt->txrx_compl_task);
  195. skb_queue_purge(&htt->tx_compl_q);
  196. skb_queue_purge(&htt->rx_compl_q);
  197. skb_queue_purge(&htt->rx_in_ord_compl_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. u8 **fw_desc, int *fw_desc_len,
  239. struct sk_buff_head *amsdu)
  240. {
  241. struct ath10k *ar = htt->ar;
  242. int msdu_len, msdu_chaining = 0;
  243. struct sk_buff *msdu;
  244. struct htt_rx_desc *rx_desc;
  245. lockdep_assert_held(&htt->rx_ring.lock);
  246. for (;;) {
  247. int last_msdu, msdu_len_invalid, msdu_chained;
  248. msdu = ath10k_htt_rx_netbuf_pop(htt);
  249. if (!msdu) {
  250. __skb_queue_purge(amsdu);
  251. return -ENOENT;
  252. }
  253. __skb_queue_tail(amsdu, msdu);
  254. rx_desc = (struct htt_rx_desc *)msdu->data;
  255. /* FIXME: we must report msdu payload since this is what caller
  256. * expects now */
  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. /*
  273. * Copy the FW rx descriptor for this MSDU from the rx
  274. * indication message into the MSDU's netbuf. HL uses the
  275. * same rx indication message definition as LL, and simply
  276. * appends new info (fields from the HW rx desc, and the
  277. * MSDU payload itself). So, the offset into the rx
  278. * indication message only has to account for the standard
  279. * offset of the per-MSDU FW rx desc info within the
  280. * message, and how many bytes of the per-MSDU FW rx desc
  281. * info have already been consumed. (And the endianness of
  282. * the host, since for a big-endian host, the rx ind
  283. * message contents, including the per-MSDU rx desc bytes,
  284. * were byteswapped during upload.)
  285. */
  286. if (*fw_desc_len > 0) {
  287. rx_desc->fw_desc.info0 = **fw_desc;
  288. /*
  289. * The target is expected to only provide the basic
  290. * per-MSDU rx descriptors. Just to be sure, verify
  291. * that the target has not attached extension data
  292. * (e.g. LRO flow ID).
  293. */
  294. /* or more, if there's extension data */
  295. (*fw_desc)++;
  296. (*fw_desc_len)--;
  297. } else {
  298. /*
  299. * When an oversized AMSDU happened, FW will lost
  300. * some of MSDU status - in this case, the FW
  301. * descriptors provided will be less than the
  302. * actual MSDUs inside this MPDU. Mark the FW
  303. * descriptors so that it will still deliver to
  304. * upper stack, if no CRC error for this MPDU.
  305. *
  306. * FIX THIS - the FW descriptors are actually for
  307. * MSDUs in the end of this A-MSDU instead of the
  308. * beginning.
  309. */
  310. rx_desc->fw_desc.info0 = 0;
  311. }
  312. msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
  313. & (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
  314. RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
  315. msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.info0),
  316. RX_MSDU_START_INFO0_MSDU_LENGTH);
  317. msdu_chained = rx_desc->frag_info.ring2_more_count;
  318. if (msdu_len_invalid)
  319. msdu_len = 0;
  320. skb_trim(msdu, 0);
  321. skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
  322. msdu_len -= msdu->len;
  323. /* Note: Chained buffers do not contain rx descriptor */
  324. while (msdu_chained--) {
  325. msdu = ath10k_htt_rx_netbuf_pop(htt);
  326. if (!msdu) {
  327. __skb_queue_purge(amsdu);
  328. return -ENOENT;
  329. }
  330. __skb_queue_tail(amsdu, msdu);
  331. skb_trim(msdu, 0);
  332. skb_put(msdu, min(msdu_len, HTT_RX_BUF_SIZE));
  333. msdu_len -= msdu->len;
  334. msdu_chaining = 1;
  335. }
  336. last_msdu = __le32_to_cpu(rx_desc->msdu_end.info0) &
  337. RX_MSDU_END_INFO0_LAST_MSDU;
  338. trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
  339. sizeof(*rx_desc) - sizeof(u32));
  340. if (last_msdu)
  341. break;
  342. }
  343. if (skb_queue_empty(amsdu))
  344. msdu_chaining = -1;
  345. /*
  346. * Don't refill the ring yet.
  347. *
  348. * First, the elements popped here are still in use - it is not
  349. * safe to overwrite them until the matching call to
  350. * mpdu_desc_list_next. Second, for efficiency it is preferable to
  351. * refill the rx ring with 1 PPDU's worth of rx buffers (something
  352. * like 32 x 3 buffers), rather than one MPDU's worth of rx buffers
  353. * (something like 3 buffers). Consequently, we'll rely on the txrx
  354. * SW to tell us when it is done pulling all the PPDU's rx buffers
  355. * out of the rx ring, and then refill it just once.
  356. */
  357. return msdu_chaining;
  358. }
  359. static void ath10k_htt_rx_replenish_task(unsigned long ptr)
  360. {
  361. struct ath10k_htt *htt = (struct ath10k_htt *)ptr;
  362. ath10k_htt_rx_msdu_buff_replenish(htt);
  363. }
  364. static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
  365. u32 paddr)
  366. {
  367. struct ath10k *ar = htt->ar;
  368. struct ath10k_skb_rxcb *rxcb;
  369. struct sk_buff *msdu;
  370. lockdep_assert_held(&htt->rx_ring.lock);
  371. msdu = ath10k_htt_rx_find_skb_paddr(ar, paddr);
  372. if (!msdu)
  373. return NULL;
  374. rxcb = ATH10K_SKB_RXCB(msdu);
  375. hash_del(&rxcb->hlist);
  376. htt->rx_ring.fill_cnt--;
  377. dma_unmap_single(htt->ar->dev, rxcb->paddr,
  378. msdu->len + skb_tailroom(msdu),
  379. DMA_FROM_DEVICE);
  380. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
  381. msdu->data, msdu->len + skb_tailroom(msdu));
  382. return msdu;
  383. }
  384. static int ath10k_htt_rx_pop_paddr_list(struct ath10k_htt *htt,
  385. struct htt_rx_in_ord_ind *ev,
  386. struct sk_buff_head *list)
  387. {
  388. struct ath10k *ar = htt->ar;
  389. struct htt_rx_in_ord_msdu_desc *msdu_desc = ev->msdu_descs;
  390. struct htt_rx_desc *rxd;
  391. struct sk_buff *msdu;
  392. int msdu_count;
  393. bool is_offload;
  394. u32 paddr;
  395. lockdep_assert_held(&htt->rx_ring.lock);
  396. msdu_count = __le16_to_cpu(ev->msdu_count);
  397. is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
  398. while (msdu_count--) {
  399. paddr = __le32_to_cpu(msdu_desc->msdu_paddr);
  400. msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
  401. if (!msdu) {
  402. __skb_queue_purge(list);
  403. return -ENOENT;
  404. }
  405. __skb_queue_tail(list, msdu);
  406. if (!is_offload) {
  407. rxd = (void *)msdu->data;
  408. trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
  409. skb_put(msdu, sizeof(*rxd));
  410. skb_pull(msdu, sizeof(*rxd));
  411. skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
  412. if (!(__le32_to_cpu(rxd->attention.flags) &
  413. RX_ATTENTION_FLAGS_MSDU_DONE)) {
  414. ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
  415. return -EIO;
  416. }
  417. }
  418. msdu_desc++;
  419. }
  420. return 0;
  421. }
  422. int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
  423. {
  424. struct ath10k *ar = htt->ar;
  425. dma_addr_t paddr;
  426. void *vaddr;
  427. size_t size;
  428. struct timer_list *timer = &htt->rx_ring.refill_retry_timer;
  429. htt->rx_confused = false;
  430. /* XXX: The fill level could be changed during runtime in response to
  431. * the host processing latency. Is this really worth it?
  432. */
  433. htt->rx_ring.size = HTT_RX_RING_SIZE;
  434. htt->rx_ring.size_mask = htt->rx_ring.size - 1;
  435. htt->rx_ring.fill_level = HTT_RX_RING_FILL_LEVEL;
  436. if (!is_power_of_2(htt->rx_ring.size)) {
  437. ath10k_warn(ar, "htt rx ring size is not power of 2\n");
  438. return -EINVAL;
  439. }
  440. htt->rx_ring.netbufs_ring =
  441. kzalloc(htt->rx_ring.size * sizeof(struct sk_buff *),
  442. GFP_KERNEL);
  443. if (!htt->rx_ring.netbufs_ring)
  444. goto err_netbuf;
  445. size = htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring);
  446. vaddr = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_DMA);
  447. if (!vaddr)
  448. goto err_dma_ring;
  449. htt->rx_ring.paddrs_ring = vaddr;
  450. htt->rx_ring.base_paddr = paddr;
  451. vaddr = dma_alloc_coherent(htt->ar->dev,
  452. sizeof(*htt->rx_ring.alloc_idx.vaddr),
  453. &paddr, GFP_DMA);
  454. if (!vaddr)
  455. goto err_dma_idx;
  456. htt->rx_ring.alloc_idx.vaddr = vaddr;
  457. htt->rx_ring.alloc_idx.paddr = paddr;
  458. htt->rx_ring.sw_rd_idx.msdu_payld = htt->rx_ring.size_mask;
  459. *htt->rx_ring.alloc_idx.vaddr = 0;
  460. /* Initialize the Rx refill retry timer */
  461. setup_timer(timer, ath10k_htt_rx_ring_refill_retry, (unsigned long)htt);
  462. spin_lock_init(&htt->rx_ring.lock);
  463. htt->rx_ring.fill_cnt = 0;
  464. htt->rx_ring.sw_rd_idx.msdu_payld = 0;
  465. hash_init(htt->rx_ring.skb_table);
  466. tasklet_init(&htt->rx_replenish_task, ath10k_htt_rx_replenish_task,
  467. (unsigned long)htt);
  468. skb_queue_head_init(&htt->tx_compl_q);
  469. skb_queue_head_init(&htt->rx_compl_q);
  470. skb_queue_head_init(&htt->rx_in_ord_compl_q);
  471. tasklet_init(&htt->txrx_compl_task, ath10k_htt_txrx_compl_task,
  472. (unsigned long)htt);
  473. ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
  474. htt->rx_ring.size, htt->rx_ring.fill_level);
  475. return 0;
  476. err_dma_idx:
  477. dma_free_coherent(htt->ar->dev,
  478. (htt->rx_ring.size *
  479. sizeof(htt->rx_ring.paddrs_ring)),
  480. htt->rx_ring.paddrs_ring,
  481. htt->rx_ring.base_paddr);
  482. err_dma_ring:
  483. kfree(htt->rx_ring.netbufs_ring);
  484. err_netbuf:
  485. return -ENOMEM;
  486. }
  487. static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
  488. enum htt_rx_mpdu_encrypt_type type)
  489. {
  490. switch (type) {
  491. case HTT_RX_MPDU_ENCRYPT_NONE:
  492. return 0;
  493. case HTT_RX_MPDU_ENCRYPT_WEP40:
  494. case HTT_RX_MPDU_ENCRYPT_WEP104:
  495. return IEEE80211_WEP_IV_LEN;
  496. case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
  497. case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
  498. return IEEE80211_TKIP_IV_LEN;
  499. case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
  500. return IEEE80211_CCMP_HDR_LEN;
  501. case HTT_RX_MPDU_ENCRYPT_WEP128:
  502. case HTT_RX_MPDU_ENCRYPT_WAPI:
  503. break;
  504. }
  505. ath10k_warn(ar, "unsupported encryption type %d\n", type);
  506. return 0;
  507. }
  508. #define MICHAEL_MIC_LEN 8
  509. static int ath10k_htt_rx_crypto_tail_len(struct ath10k *ar,
  510. enum htt_rx_mpdu_encrypt_type type)
  511. {
  512. switch (type) {
  513. case HTT_RX_MPDU_ENCRYPT_NONE:
  514. return 0;
  515. case HTT_RX_MPDU_ENCRYPT_WEP40:
  516. case HTT_RX_MPDU_ENCRYPT_WEP104:
  517. return IEEE80211_WEP_ICV_LEN;
  518. case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
  519. case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
  520. return IEEE80211_TKIP_ICV_LEN;
  521. case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
  522. return IEEE80211_CCMP_MIC_LEN;
  523. case HTT_RX_MPDU_ENCRYPT_WEP128:
  524. case HTT_RX_MPDU_ENCRYPT_WAPI:
  525. break;
  526. }
  527. ath10k_warn(ar, "unsupported encryption type %d\n", type);
  528. return 0;
  529. }
  530. struct rfc1042_hdr {
  531. u8 llc_dsap;
  532. u8 llc_ssap;
  533. u8 llc_ctrl;
  534. u8 snap_oui[3];
  535. __be16 snap_type;
  536. } __packed;
  537. struct amsdu_subframe_hdr {
  538. u8 dst[ETH_ALEN];
  539. u8 src[ETH_ALEN];
  540. __be16 len;
  541. } __packed;
  542. static const u8 rx_legacy_rate_idx[] = {
  543. 3, /* 0x00 - 11Mbps */
  544. 2, /* 0x01 - 5.5Mbps */
  545. 1, /* 0x02 - 2Mbps */
  546. 0, /* 0x03 - 1Mbps */
  547. 3, /* 0x04 - 11Mbps */
  548. 2, /* 0x05 - 5.5Mbps */
  549. 1, /* 0x06 - 2Mbps */
  550. 0, /* 0x07 - 1Mbps */
  551. 10, /* 0x08 - 48Mbps */
  552. 8, /* 0x09 - 24Mbps */
  553. 6, /* 0x0A - 12Mbps */
  554. 4, /* 0x0B - 6Mbps */
  555. 11, /* 0x0C - 54Mbps */
  556. 9, /* 0x0D - 36Mbps */
  557. 7, /* 0x0E - 18Mbps */
  558. 5, /* 0x0F - 9Mbps */
  559. };
  560. static void ath10k_htt_rx_h_rates(struct ath10k *ar,
  561. struct ieee80211_rx_status *status,
  562. struct htt_rx_desc *rxd)
  563. {
  564. enum ieee80211_band band;
  565. u8 cck, rate, rate_idx, bw, sgi, mcs, nss;
  566. u8 preamble = 0;
  567. u32 info1, info2, info3;
  568. /* Band value can't be set as undefined but freq can be 0 - use that to
  569. * determine whether band is provided.
  570. *
  571. * FIXME: Perhaps this can go away if CCK rate reporting is a little
  572. * reworked?
  573. */
  574. if (!status->freq)
  575. return;
  576. band = status->band;
  577. info1 = __le32_to_cpu(rxd->ppdu_start.info1);
  578. info2 = __le32_to_cpu(rxd->ppdu_start.info2);
  579. info3 = __le32_to_cpu(rxd->ppdu_start.info3);
  580. preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
  581. switch (preamble) {
  582. case HTT_RX_LEGACY:
  583. cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
  584. rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
  585. rate_idx = 0;
  586. if (rate < 0x08 || rate > 0x0F)
  587. break;
  588. switch (band) {
  589. case IEEE80211_BAND_2GHZ:
  590. if (cck)
  591. rate &= ~BIT(3);
  592. rate_idx = rx_legacy_rate_idx[rate];
  593. break;
  594. case IEEE80211_BAND_5GHZ:
  595. rate_idx = rx_legacy_rate_idx[rate];
  596. /* We are using same rate table registering
  597. HW - ath10k_rates[]. In case of 5GHz skip
  598. CCK rates, so -4 here */
  599. rate_idx -= 4;
  600. break;
  601. default:
  602. break;
  603. }
  604. status->rate_idx = rate_idx;
  605. break;
  606. case HTT_RX_HT:
  607. case HTT_RX_HT_WITH_TXBF:
  608. /* HT-SIG - Table 20-11 in info2 and info3 */
  609. mcs = info2 & 0x1F;
  610. nss = mcs >> 3;
  611. bw = (info2 >> 7) & 1;
  612. sgi = (info3 >> 7) & 1;
  613. status->rate_idx = mcs;
  614. status->flag |= RX_FLAG_HT;
  615. if (sgi)
  616. status->flag |= RX_FLAG_SHORT_GI;
  617. if (bw)
  618. status->flag |= RX_FLAG_40MHZ;
  619. break;
  620. case HTT_RX_VHT:
  621. case HTT_RX_VHT_WITH_TXBF:
  622. /* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
  623. TODO check this */
  624. mcs = (info3 >> 4) & 0x0F;
  625. nss = ((info2 >> 10) & 0x07) + 1;
  626. bw = info2 & 3;
  627. sgi = info3 & 1;
  628. status->rate_idx = mcs;
  629. status->vht_nss = nss;
  630. if (sgi)
  631. status->flag |= RX_FLAG_SHORT_GI;
  632. switch (bw) {
  633. /* 20MHZ */
  634. case 0:
  635. break;
  636. /* 40MHZ */
  637. case 1:
  638. status->flag |= RX_FLAG_40MHZ;
  639. break;
  640. /* 80MHZ */
  641. case 2:
  642. status->vht_flag |= RX_VHT_FLAG_80MHZ;
  643. }
  644. status->flag |= RX_FLAG_VHT;
  645. break;
  646. default:
  647. break;
  648. }
  649. }
  650. static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
  651. struct ieee80211_rx_status *status)
  652. {
  653. struct ieee80211_channel *ch;
  654. spin_lock_bh(&ar->data_lock);
  655. ch = ar->scan_channel;
  656. if (!ch)
  657. ch = ar->rx_channel;
  658. spin_unlock_bh(&ar->data_lock);
  659. if (!ch)
  660. return false;
  661. status->band = ch->band;
  662. status->freq = ch->center_freq;
  663. return true;
  664. }
  665. static void ath10k_htt_rx_h_signal(struct ath10k *ar,
  666. struct ieee80211_rx_status *status,
  667. struct htt_rx_desc *rxd)
  668. {
  669. /* FIXME: Get real NF */
  670. status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
  671. rxd->ppdu_start.rssi_comb;
  672. status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
  673. }
  674. static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
  675. struct ieee80211_rx_status *status,
  676. struct htt_rx_desc *rxd)
  677. {
  678. /* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
  679. * means all prior MSDUs in a PPDU are reported to mac80211 without the
  680. * TSF. Is it worth holding frames until end of PPDU is known?
  681. *
  682. * FIXME: Can we get/compute 64bit TSF?
  683. */
  684. status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
  685. status->flag |= RX_FLAG_MACTIME_END;
  686. }
  687. static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
  688. struct sk_buff_head *amsdu,
  689. struct ieee80211_rx_status *status)
  690. {
  691. struct sk_buff *first;
  692. struct htt_rx_desc *rxd;
  693. bool is_first_ppdu;
  694. bool is_last_ppdu;
  695. if (skb_queue_empty(amsdu))
  696. return;
  697. first = skb_peek(amsdu);
  698. rxd = (void *)first->data - sizeof(*rxd);
  699. is_first_ppdu = !!(rxd->attention.flags &
  700. __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
  701. is_last_ppdu = !!(rxd->attention.flags &
  702. __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));
  703. if (is_first_ppdu) {
  704. /* New PPDU starts so clear out the old per-PPDU status. */
  705. status->freq = 0;
  706. status->rate_idx = 0;
  707. status->vht_nss = 0;
  708. status->vht_flag &= ~RX_VHT_FLAG_80MHZ;
  709. status->flag &= ~(RX_FLAG_HT |
  710. RX_FLAG_VHT |
  711. RX_FLAG_SHORT_GI |
  712. RX_FLAG_40MHZ |
  713. RX_FLAG_MACTIME_END);
  714. status->flag |= RX_FLAG_NO_SIGNAL_VAL;
  715. ath10k_htt_rx_h_signal(ar, status, rxd);
  716. ath10k_htt_rx_h_channel(ar, status);
  717. ath10k_htt_rx_h_rates(ar, status, rxd);
  718. }
  719. if (is_last_ppdu)
  720. ath10k_htt_rx_h_mactime(ar, status, rxd);
  721. }
  722. static const char * const tid_to_ac[] = {
  723. "BE",
  724. "BK",
  725. "BK",
  726. "BE",
  727. "VI",
  728. "VI",
  729. "VO",
  730. "VO",
  731. };
  732. static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
  733. {
  734. u8 *qc;
  735. int tid;
  736. if (!ieee80211_is_data_qos(hdr->frame_control))
  737. return "";
  738. qc = ieee80211_get_qos_ctl(hdr);
  739. tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
  740. if (tid < 8)
  741. snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
  742. else
  743. snprintf(out, size, "tid %d", tid);
  744. return out;
  745. }
  746. static void ath10k_process_rx(struct ath10k *ar,
  747. struct ieee80211_rx_status *rx_status,
  748. struct sk_buff *skb)
  749. {
  750. struct ieee80211_rx_status *status;
  751. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  752. char tid[32];
  753. status = IEEE80211_SKB_RXCB(skb);
  754. *status = *rx_status;
  755. ath10k_dbg(ar, ATH10K_DBG_DATA,
  756. "rx skb %p len %u peer %pM %s %s sn %u %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",
  757. skb,
  758. skb->len,
  759. ieee80211_get_SA(hdr),
  760. ath10k_get_tid(hdr, tid, sizeof(tid)),
  761. is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
  762. "mcast" : "ucast",
  763. (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
  764. status->flag == 0 ? "legacy" : "",
  765. status->flag & RX_FLAG_HT ? "ht" : "",
  766. status->flag & RX_FLAG_VHT ? "vht" : "",
  767. status->flag & RX_FLAG_40MHZ ? "40" : "",
  768. status->vht_flag & RX_VHT_FLAG_80MHZ ? "80" : "",
  769. status->flag & RX_FLAG_SHORT_GI ? "sgi " : "",
  770. status->rate_idx,
  771. status->vht_nss,
  772. status->freq,
  773. status->band, status->flag,
  774. !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
  775. !!(status->flag & RX_FLAG_MMIC_ERROR),
  776. !!(status->flag & RX_FLAG_AMSDU_MORE));
  777. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
  778. skb->data, skb->len);
  779. trace_ath10k_rx_hdr(ar, skb->data, skb->len);
  780. trace_ath10k_rx_payload(ar, skb->data, skb->len);
  781. ieee80211_rx(ar->hw, skb);
  782. }
  783. static int ath10k_htt_rx_nwifi_hdrlen(struct ieee80211_hdr *hdr)
  784. {
  785. /* nwifi header is padded to 4 bytes. this fixes 4addr rx */
  786. return round_up(ieee80211_hdrlen(hdr->frame_control), 4);
  787. }
  788. static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
  789. struct sk_buff *msdu,
  790. struct ieee80211_rx_status *status,
  791. enum htt_rx_mpdu_encrypt_type enctype,
  792. bool is_decrypted)
  793. {
  794. struct ieee80211_hdr *hdr;
  795. struct htt_rx_desc *rxd;
  796. size_t hdr_len;
  797. size_t crypto_len;
  798. bool is_first;
  799. bool is_last;
  800. rxd = (void *)msdu->data - sizeof(*rxd);
  801. is_first = !!(rxd->msdu_end.info0 &
  802. __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
  803. is_last = !!(rxd->msdu_end.info0 &
  804. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
  805. /* Delivered decapped frame:
  806. * [802.11 header]
  807. * [crypto param] <-- can be trimmed if !fcs_err &&
  808. * !decrypt_err && !peer_idx_invalid
  809. * [amsdu header] <-- only if A-MSDU
  810. * [rfc1042/llc]
  811. * [payload]
  812. * [FCS] <-- at end, needs to be trimmed
  813. */
  814. /* This probably shouldn't happen but warn just in case */
  815. if (unlikely(WARN_ON_ONCE(!is_first)))
  816. return;
  817. /* This probably shouldn't happen but warn just in case */
  818. if (unlikely(WARN_ON_ONCE(!(is_first && is_last))))
  819. return;
  820. skb_trim(msdu, msdu->len - FCS_LEN);
  821. /* In most cases this will be true for sniffed frames. It makes sense
  822. * to deliver them as-is without stripping the crypto param. This would
  823. * also make sense for software based decryption (which is not
  824. * implemented in ath10k).
  825. *
  826. * If there's no error then the frame is decrypted. At least that is
  827. * the case for frames that come in via fragmented rx indication.
  828. */
  829. if (!is_decrypted)
  830. return;
  831. /* The payload is decrypted so strip crypto params. Start from tail
  832. * since hdr is used to compute some stuff.
  833. */
  834. hdr = (void *)msdu->data;
  835. /* Tail */
  836. skb_trim(msdu, msdu->len - ath10k_htt_rx_crypto_tail_len(ar, enctype));
  837. /* MMIC */
  838. if (!ieee80211_has_morefrags(hdr->frame_control) &&
  839. enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
  840. skb_trim(msdu, msdu->len - 8);
  841. /* Head */
  842. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  843. crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
  844. memmove((void *)msdu->data + crypto_len,
  845. (void *)msdu->data, hdr_len);
  846. skb_pull(msdu, crypto_len);
  847. }
  848. static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
  849. struct sk_buff *msdu,
  850. struct ieee80211_rx_status *status,
  851. const u8 first_hdr[64])
  852. {
  853. struct ieee80211_hdr *hdr;
  854. size_t hdr_len;
  855. u8 da[ETH_ALEN];
  856. u8 sa[ETH_ALEN];
  857. /* Delivered decapped frame:
  858. * [nwifi 802.11 header] <-- replaced with 802.11 hdr
  859. * [rfc1042/llc]
  860. *
  861. * Note: The nwifi header doesn't have QoS Control and is
  862. * (always?) a 3addr frame.
  863. *
  864. * Note2: There's no A-MSDU subframe header. Even if it's part
  865. * of an A-MSDU.
  866. */
  867. /* pull decapped header and copy SA & DA */
  868. hdr = (struct ieee80211_hdr *)msdu->data;
  869. hdr_len = ath10k_htt_rx_nwifi_hdrlen(hdr);
  870. ether_addr_copy(da, ieee80211_get_DA(hdr));
  871. ether_addr_copy(sa, ieee80211_get_SA(hdr));
  872. skb_pull(msdu, hdr_len);
  873. /* push original 802.11 header */
  874. hdr = (struct ieee80211_hdr *)first_hdr;
  875. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  876. memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
  877. /* original 802.11 header has a different DA and in
  878. * case of 4addr it may also have different SA
  879. */
  880. hdr = (struct ieee80211_hdr *)msdu->data;
  881. ether_addr_copy(ieee80211_get_DA(hdr), da);
  882. ether_addr_copy(ieee80211_get_SA(hdr), sa);
  883. }
  884. static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
  885. struct sk_buff *msdu,
  886. enum htt_rx_mpdu_encrypt_type enctype)
  887. {
  888. struct ieee80211_hdr *hdr;
  889. struct htt_rx_desc *rxd;
  890. size_t hdr_len, crypto_len;
  891. void *rfc1042;
  892. bool is_first, is_last, is_amsdu;
  893. rxd = (void *)msdu->data - sizeof(*rxd);
  894. hdr = (void *)rxd->rx_hdr_status;
  895. is_first = !!(rxd->msdu_end.info0 &
  896. __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
  897. is_last = !!(rxd->msdu_end.info0 &
  898. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
  899. is_amsdu = !(is_first && is_last);
  900. rfc1042 = hdr;
  901. if (is_first) {
  902. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  903. crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
  904. rfc1042 += round_up(hdr_len, 4) +
  905. round_up(crypto_len, 4);
  906. }
  907. if (is_amsdu)
  908. rfc1042 += sizeof(struct amsdu_subframe_hdr);
  909. return rfc1042;
  910. }
  911. static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
  912. struct sk_buff *msdu,
  913. struct ieee80211_rx_status *status,
  914. const u8 first_hdr[64],
  915. enum htt_rx_mpdu_encrypt_type enctype)
  916. {
  917. struct ieee80211_hdr *hdr;
  918. struct ethhdr *eth;
  919. size_t hdr_len;
  920. void *rfc1042;
  921. u8 da[ETH_ALEN];
  922. u8 sa[ETH_ALEN];
  923. /* Delivered decapped frame:
  924. * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
  925. * [payload]
  926. */
  927. rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
  928. if (WARN_ON_ONCE(!rfc1042))
  929. return;
  930. /* pull decapped header and copy SA & DA */
  931. eth = (struct ethhdr *)msdu->data;
  932. ether_addr_copy(da, eth->h_dest);
  933. ether_addr_copy(sa, eth->h_source);
  934. skb_pull(msdu, sizeof(struct ethhdr));
  935. /* push rfc1042/llc/snap */
  936. memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
  937. sizeof(struct rfc1042_hdr));
  938. /* push original 802.11 header */
  939. hdr = (struct ieee80211_hdr *)first_hdr;
  940. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  941. memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
  942. /* original 802.11 header has a different DA and in
  943. * case of 4addr it may also have different SA
  944. */
  945. hdr = (struct ieee80211_hdr *)msdu->data;
  946. ether_addr_copy(ieee80211_get_DA(hdr), da);
  947. ether_addr_copy(ieee80211_get_SA(hdr), sa);
  948. }
  949. static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
  950. struct sk_buff *msdu,
  951. struct ieee80211_rx_status *status,
  952. const u8 first_hdr[64])
  953. {
  954. struct ieee80211_hdr *hdr;
  955. size_t hdr_len;
  956. /* Delivered decapped frame:
  957. * [amsdu header] <-- replaced with 802.11 hdr
  958. * [rfc1042/llc]
  959. * [payload]
  960. */
  961. skb_pull(msdu, sizeof(struct amsdu_subframe_hdr));
  962. hdr = (struct ieee80211_hdr *)first_hdr;
  963. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  964. memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
  965. }
  966. static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
  967. struct sk_buff *msdu,
  968. struct ieee80211_rx_status *status,
  969. u8 first_hdr[64],
  970. enum htt_rx_mpdu_encrypt_type enctype,
  971. bool is_decrypted)
  972. {
  973. struct htt_rx_desc *rxd;
  974. enum rx_msdu_decap_format decap;
  975. struct ieee80211_hdr *hdr;
  976. /* First msdu's decapped header:
  977. * [802.11 header] <-- padded to 4 bytes long
  978. * [crypto param] <-- padded to 4 bytes long
  979. * [amsdu header] <-- only if A-MSDU
  980. * [rfc1042/llc]
  981. *
  982. * Other (2nd, 3rd, ..) msdu's decapped header:
  983. * [amsdu header] <-- only if A-MSDU
  984. * [rfc1042/llc]
  985. */
  986. rxd = (void *)msdu->data - sizeof(*rxd);
  987. hdr = (void *)rxd->rx_hdr_status;
  988. decap = MS(__le32_to_cpu(rxd->msdu_start.info1),
  989. RX_MSDU_START_INFO1_DECAP_FORMAT);
  990. switch (decap) {
  991. case RX_MSDU_DECAP_RAW:
  992. ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
  993. is_decrypted);
  994. break;
  995. case RX_MSDU_DECAP_NATIVE_WIFI:
  996. ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr);
  997. break;
  998. case RX_MSDU_DECAP_ETHERNET2_DIX:
  999. ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
  1000. break;
  1001. case RX_MSDU_DECAP_8023_SNAP_LLC:
  1002. ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr);
  1003. break;
  1004. }
  1005. }
  1006. static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
  1007. {
  1008. struct htt_rx_desc *rxd;
  1009. u32 flags, info;
  1010. bool is_ip4, is_ip6;
  1011. bool is_tcp, is_udp;
  1012. bool ip_csum_ok, tcpudp_csum_ok;
  1013. rxd = (void *)skb->data - sizeof(*rxd);
  1014. flags = __le32_to_cpu(rxd->attention.flags);
  1015. info = __le32_to_cpu(rxd->msdu_start.info1);
  1016. is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
  1017. is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
  1018. is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
  1019. is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
  1020. ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
  1021. tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
  1022. if (!is_ip4 && !is_ip6)
  1023. return CHECKSUM_NONE;
  1024. if (!is_tcp && !is_udp)
  1025. return CHECKSUM_NONE;
  1026. if (!ip_csum_ok)
  1027. return CHECKSUM_NONE;
  1028. if (!tcpudp_csum_ok)
  1029. return CHECKSUM_NONE;
  1030. return CHECKSUM_UNNECESSARY;
  1031. }
  1032. static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
  1033. {
  1034. msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
  1035. }
  1036. static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
  1037. struct sk_buff_head *amsdu,
  1038. struct ieee80211_rx_status *status)
  1039. {
  1040. struct sk_buff *first;
  1041. struct sk_buff *last;
  1042. struct sk_buff *msdu;
  1043. struct htt_rx_desc *rxd;
  1044. struct ieee80211_hdr *hdr;
  1045. enum htt_rx_mpdu_encrypt_type enctype;
  1046. u8 first_hdr[64];
  1047. u8 *qos;
  1048. size_t hdr_len;
  1049. bool has_fcs_err;
  1050. bool has_crypto_err;
  1051. bool has_tkip_err;
  1052. bool has_peer_idx_invalid;
  1053. bool is_decrypted;
  1054. u32 attention;
  1055. if (skb_queue_empty(amsdu))
  1056. return;
  1057. first = skb_peek(amsdu);
  1058. rxd = (void *)first->data - sizeof(*rxd);
  1059. enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
  1060. RX_MPDU_START_INFO0_ENCRYPT_TYPE);
  1061. /* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
  1062. * decapped header. It'll be used for undecapping of each MSDU.
  1063. */
  1064. hdr = (void *)rxd->rx_hdr_status;
  1065. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  1066. memcpy(first_hdr, hdr, hdr_len);
  1067. /* Each A-MSDU subframe will use the original header as the base and be
  1068. * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
  1069. */
  1070. hdr = (void *)first_hdr;
  1071. qos = ieee80211_get_qos_ctl(hdr);
  1072. qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  1073. /* Some attention flags are valid only in the last MSDU. */
  1074. last = skb_peek_tail(amsdu);
  1075. rxd = (void *)last->data - sizeof(*rxd);
  1076. attention = __le32_to_cpu(rxd->attention.flags);
  1077. has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
  1078. has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
  1079. has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
  1080. has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);
  1081. /* Note: If hardware captures an encrypted frame that it can't decrypt,
  1082. * e.g. due to fcs error, missing peer or invalid key data it will
  1083. * report the frame as raw.
  1084. */
  1085. is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
  1086. !has_fcs_err &&
  1087. !has_crypto_err &&
  1088. !has_peer_idx_invalid);
  1089. /* Clear per-MPDU flags while leaving per-PPDU flags intact. */
  1090. status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
  1091. RX_FLAG_MMIC_ERROR |
  1092. RX_FLAG_DECRYPTED |
  1093. RX_FLAG_IV_STRIPPED |
  1094. RX_FLAG_MMIC_STRIPPED);
  1095. if (has_fcs_err)
  1096. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  1097. if (has_tkip_err)
  1098. status->flag |= RX_FLAG_MMIC_ERROR;
  1099. if (is_decrypted)
  1100. status->flag |= RX_FLAG_DECRYPTED |
  1101. RX_FLAG_IV_STRIPPED |
  1102. RX_FLAG_MMIC_STRIPPED;
  1103. skb_queue_walk(amsdu, msdu) {
  1104. ath10k_htt_rx_h_csum_offload(msdu);
  1105. ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
  1106. is_decrypted);
  1107. /* Undecapping involves copying the original 802.11 header back
  1108. * to sk_buff. If frame is protected and hardware has decrypted
  1109. * it then remove the protected bit.
  1110. */
  1111. if (!is_decrypted)
  1112. continue;
  1113. hdr = (void *)msdu->data;
  1114. hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1115. }
  1116. }
  1117. static void ath10k_htt_rx_h_deliver(struct ath10k *ar,
  1118. struct sk_buff_head *amsdu,
  1119. struct ieee80211_rx_status *status)
  1120. {
  1121. struct sk_buff *msdu;
  1122. while ((msdu = __skb_dequeue(amsdu))) {
  1123. /* Setup per-MSDU flags */
  1124. if (skb_queue_empty(amsdu))
  1125. status->flag &= ~RX_FLAG_AMSDU_MORE;
  1126. else
  1127. status->flag |= RX_FLAG_AMSDU_MORE;
  1128. ath10k_process_rx(ar, status, msdu);
  1129. }
  1130. }
  1131. static int ath10k_unchain_msdu(struct sk_buff_head *amsdu)
  1132. {
  1133. struct sk_buff *skb, *first;
  1134. int space;
  1135. int total_len = 0;
  1136. /* TODO: Might could optimize this by using
  1137. * skb_try_coalesce or similar method to
  1138. * decrease copying, or maybe get mac80211 to
  1139. * provide a way to just receive a list of
  1140. * skb?
  1141. */
  1142. first = __skb_dequeue(amsdu);
  1143. /* Allocate total length all at once. */
  1144. skb_queue_walk(amsdu, skb)
  1145. total_len += skb->len;
  1146. space = total_len - skb_tailroom(first);
  1147. if ((space > 0) &&
  1148. (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
  1149. /* TODO: bump some rx-oom error stat */
  1150. /* put it back together so we can free the
  1151. * whole list at once.
  1152. */
  1153. __skb_queue_head(amsdu, first);
  1154. return -1;
  1155. }
  1156. /* Walk list again, copying contents into
  1157. * msdu_head
  1158. */
  1159. while ((skb = __skb_dequeue(amsdu))) {
  1160. skb_copy_from_linear_data(skb, skb_put(first, skb->len),
  1161. skb->len);
  1162. dev_kfree_skb_any(skb);
  1163. }
  1164. __skb_queue_head(amsdu, first);
  1165. return 0;
  1166. }
  1167. static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
  1168. struct sk_buff_head *amsdu,
  1169. bool chained)
  1170. {
  1171. struct sk_buff *first;
  1172. struct htt_rx_desc *rxd;
  1173. enum rx_msdu_decap_format decap;
  1174. first = skb_peek(amsdu);
  1175. rxd = (void *)first->data - sizeof(*rxd);
  1176. decap = MS(__le32_to_cpu(rxd->msdu_start.info1),
  1177. RX_MSDU_START_INFO1_DECAP_FORMAT);
  1178. if (!chained)
  1179. return;
  1180. /* FIXME: Current unchaining logic can only handle simple case of raw
  1181. * msdu chaining. If decapping is other than raw the chaining may be
  1182. * more complex and this isn't handled by the current code. Don't even
  1183. * try re-constructing such frames - it'll be pretty much garbage.
  1184. */
  1185. if (decap != RX_MSDU_DECAP_RAW ||
  1186. skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
  1187. __skb_queue_purge(amsdu);
  1188. return;
  1189. }
  1190. ath10k_unchain_msdu(amsdu);
  1191. }
  1192. static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
  1193. struct sk_buff_head *amsdu,
  1194. struct ieee80211_rx_status *rx_status)
  1195. {
  1196. struct sk_buff *msdu;
  1197. struct htt_rx_desc *rxd;
  1198. bool is_mgmt;
  1199. bool has_fcs_err;
  1200. msdu = skb_peek(amsdu);
  1201. rxd = (void *)msdu->data - sizeof(*rxd);
  1202. /* FIXME: It might be a good idea to do some fuzzy-testing to drop
  1203. * invalid/dangerous frames.
  1204. */
  1205. if (!rx_status->freq) {
  1206. ath10k_warn(ar, "no channel configured; ignoring frame(s)!\n");
  1207. return false;
  1208. }
  1209. is_mgmt = !!(rxd->attention.flags &
  1210. __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));
  1211. has_fcs_err = !!(rxd->attention.flags &
  1212. __cpu_to_le32(RX_ATTENTION_FLAGS_FCS_ERR));
  1213. /* Management frames are handled via WMI events. The pros of such
  1214. * approach is that channel is explicitly provided in WMI events
  1215. * whereas HTT doesn't provide channel information for Rxed frames.
  1216. *
  1217. * However some firmware revisions don't report corrupted frames via
  1218. * WMI so don't drop them.
  1219. */
  1220. if (is_mgmt && !has_fcs_err) {
  1221. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx mgmt ctrl\n");
  1222. return false;
  1223. }
  1224. if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
  1225. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
  1226. return false;
  1227. }
  1228. return true;
  1229. }
  1230. static void ath10k_htt_rx_h_filter(struct ath10k *ar,
  1231. struct sk_buff_head *amsdu,
  1232. struct ieee80211_rx_status *rx_status)
  1233. {
  1234. if (skb_queue_empty(amsdu))
  1235. return;
  1236. if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
  1237. return;
  1238. __skb_queue_purge(amsdu);
  1239. }
  1240. static void ath10k_htt_rx_handler(struct ath10k_htt *htt,
  1241. struct htt_rx_indication *rx)
  1242. {
  1243. struct ath10k *ar = htt->ar;
  1244. struct ieee80211_rx_status *rx_status = &htt->rx_status;
  1245. struct htt_rx_indication_mpdu_range *mpdu_ranges;
  1246. struct sk_buff_head amsdu;
  1247. int num_mpdu_ranges;
  1248. int fw_desc_len;
  1249. u8 *fw_desc;
  1250. int i, ret, mpdu_count = 0;
  1251. lockdep_assert_held(&htt->rx_ring.lock);
  1252. if (htt->rx_confused)
  1253. return;
  1254. fw_desc_len = __le16_to_cpu(rx->prefix.fw_rx_desc_bytes);
  1255. fw_desc = (u8 *)&rx->fw_desc;
  1256. num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
  1257. HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
  1258. mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
  1259. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
  1260. rx, sizeof(*rx) +
  1261. (sizeof(struct htt_rx_indication_mpdu_range) *
  1262. num_mpdu_ranges));
  1263. for (i = 0; i < num_mpdu_ranges; i++)
  1264. mpdu_count += mpdu_ranges[i].mpdu_count;
  1265. while (mpdu_count--) {
  1266. __skb_queue_head_init(&amsdu);
  1267. ret = ath10k_htt_rx_amsdu_pop(htt, &fw_desc,
  1268. &fw_desc_len, &amsdu);
  1269. if (ret < 0) {
  1270. ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
  1271. __skb_queue_purge(&amsdu);
  1272. /* FIXME: It's probably a good idea to reboot the
  1273. * device instead of leaving it inoperable.
  1274. */
  1275. htt->rx_confused = true;
  1276. break;
  1277. }
  1278. ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status);
  1279. ath10k_htt_rx_h_unchain(ar, &amsdu, ret > 0);
  1280. ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
  1281. ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status);
  1282. ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
  1283. }
  1284. tasklet_schedule(&htt->rx_replenish_task);
  1285. }
  1286. static void ath10k_htt_rx_frag_handler(struct ath10k_htt *htt,
  1287. struct htt_rx_fragment_indication *frag)
  1288. {
  1289. struct ath10k *ar = htt->ar;
  1290. struct ieee80211_rx_status *rx_status = &htt->rx_status;
  1291. struct sk_buff_head amsdu;
  1292. int ret;
  1293. u8 *fw_desc;
  1294. int fw_desc_len;
  1295. fw_desc_len = __le16_to_cpu(frag->fw_rx_desc_bytes);
  1296. fw_desc = (u8 *)frag->fw_msdu_rx_desc;
  1297. __skb_queue_head_init(&amsdu);
  1298. spin_lock_bh(&htt->rx_ring.lock);
  1299. ret = ath10k_htt_rx_amsdu_pop(htt, &fw_desc, &fw_desc_len,
  1300. &amsdu);
  1301. spin_unlock_bh(&htt->rx_ring.lock);
  1302. tasklet_schedule(&htt->rx_replenish_task);
  1303. ath10k_dbg(ar, ATH10K_DBG_HTT_DUMP, "htt rx frag ahead\n");
  1304. if (ret) {
  1305. ath10k_warn(ar, "failed to pop amsdu from httr rx ring for fragmented rx %d\n",
  1306. ret);
  1307. __skb_queue_purge(&amsdu);
  1308. return;
  1309. }
  1310. if (skb_queue_len(&amsdu) != 1) {
  1311. ath10k_warn(ar, "failed to pop frag amsdu: too many msdus\n");
  1312. __skb_queue_purge(&amsdu);
  1313. return;
  1314. }
  1315. ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status);
  1316. ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
  1317. ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status);
  1318. ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
  1319. if (fw_desc_len > 0) {
  1320. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1321. "expecting more fragmented rx in one indication %d\n",
  1322. fw_desc_len);
  1323. }
  1324. }
  1325. static void ath10k_htt_rx_frm_tx_compl(struct ath10k *ar,
  1326. struct sk_buff *skb)
  1327. {
  1328. struct ath10k_htt *htt = &ar->htt;
  1329. struct htt_resp *resp = (struct htt_resp *)skb->data;
  1330. struct htt_tx_done tx_done = {};
  1331. int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
  1332. __le16 msdu_id;
  1333. int i;
  1334. lockdep_assert_held(&htt->tx_lock);
  1335. switch (status) {
  1336. case HTT_DATA_TX_STATUS_NO_ACK:
  1337. tx_done.no_ack = true;
  1338. break;
  1339. case HTT_DATA_TX_STATUS_OK:
  1340. break;
  1341. case HTT_DATA_TX_STATUS_DISCARD:
  1342. case HTT_DATA_TX_STATUS_POSTPONE:
  1343. case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
  1344. tx_done.discard = true;
  1345. break;
  1346. default:
  1347. ath10k_warn(ar, "unhandled tx completion status %d\n", status);
  1348. tx_done.discard = true;
  1349. break;
  1350. }
  1351. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
  1352. resp->data_tx_completion.num_msdus);
  1353. for (i = 0; i < resp->data_tx_completion.num_msdus; i++) {
  1354. msdu_id = resp->data_tx_completion.msdus[i];
  1355. tx_done.msdu_id = __le16_to_cpu(msdu_id);
  1356. ath10k_txrx_tx_unref(htt, &tx_done);
  1357. }
  1358. }
  1359. static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
  1360. {
  1361. struct htt_rx_addba *ev = &resp->rx_addba;
  1362. struct ath10k_peer *peer;
  1363. struct ath10k_vif *arvif;
  1364. u16 info0, tid, peer_id;
  1365. info0 = __le16_to_cpu(ev->info0);
  1366. tid = MS(info0, HTT_RX_BA_INFO0_TID);
  1367. peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
  1368. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1369. "htt rx addba tid %hu peer_id %hu size %hhu\n",
  1370. tid, peer_id, ev->window_size);
  1371. spin_lock_bh(&ar->data_lock);
  1372. peer = ath10k_peer_find_by_id(ar, peer_id);
  1373. if (!peer) {
  1374. ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
  1375. peer_id);
  1376. spin_unlock_bh(&ar->data_lock);
  1377. return;
  1378. }
  1379. arvif = ath10k_get_arvif(ar, peer->vdev_id);
  1380. if (!arvif) {
  1381. ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
  1382. peer->vdev_id);
  1383. spin_unlock_bh(&ar->data_lock);
  1384. return;
  1385. }
  1386. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1387. "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
  1388. peer->addr, tid, ev->window_size);
  1389. ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
  1390. spin_unlock_bh(&ar->data_lock);
  1391. }
  1392. static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
  1393. {
  1394. struct htt_rx_delba *ev = &resp->rx_delba;
  1395. struct ath10k_peer *peer;
  1396. struct ath10k_vif *arvif;
  1397. u16 info0, tid, peer_id;
  1398. info0 = __le16_to_cpu(ev->info0);
  1399. tid = MS(info0, HTT_RX_BA_INFO0_TID);
  1400. peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
  1401. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1402. "htt rx delba tid %hu peer_id %hu\n",
  1403. tid, peer_id);
  1404. spin_lock_bh(&ar->data_lock);
  1405. peer = ath10k_peer_find_by_id(ar, peer_id);
  1406. if (!peer) {
  1407. ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
  1408. peer_id);
  1409. spin_unlock_bh(&ar->data_lock);
  1410. return;
  1411. }
  1412. arvif = ath10k_get_arvif(ar, peer->vdev_id);
  1413. if (!arvif) {
  1414. ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
  1415. peer->vdev_id);
  1416. spin_unlock_bh(&ar->data_lock);
  1417. return;
  1418. }
  1419. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1420. "htt rx stop rx ba session sta %pM tid %hu\n",
  1421. peer->addr, tid);
  1422. ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
  1423. spin_unlock_bh(&ar->data_lock);
  1424. }
  1425. static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
  1426. struct sk_buff_head *amsdu)
  1427. {
  1428. struct sk_buff *msdu;
  1429. struct htt_rx_desc *rxd;
  1430. if (skb_queue_empty(list))
  1431. return -ENOBUFS;
  1432. if (WARN_ON(!skb_queue_empty(amsdu)))
  1433. return -EINVAL;
  1434. while ((msdu = __skb_dequeue(list))) {
  1435. __skb_queue_tail(amsdu, msdu);
  1436. rxd = (void *)msdu->data - sizeof(*rxd);
  1437. if (rxd->msdu_end.info0 &
  1438. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
  1439. break;
  1440. }
  1441. msdu = skb_peek_tail(amsdu);
  1442. rxd = (void *)msdu->data - sizeof(*rxd);
  1443. if (!(rxd->msdu_end.info0 &
  1444. __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
  1445. skb_queue_splice_init(amsdu, list);
  1446. return -EAGAIN;
  1447. }
  1448. return 0;
  1449. }
  1450. static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
  1451. struct sk_buff *skb)
  1452. {
  1453. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1454. if (!ieee80211_has_protected(hdr->frame_control))
  1455. return;
  1456. /* Offloaded frames are already decrypted but firmware insists they are
  1457. * protected in the 802.11 header. Strip the flag. Otherwise mac80211
  1458. * will drop the frame.
  1459. */
  1460. hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1461. status->flag |= RX_FLAG_DECRYPTED |
  1462. RX_FLAG_IV_STRIPPED |
  1463. RX_FLAG_MMIC_STRIPPED;
  1464. }
  1465. static void ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
  1466. struct sk_buff_head *list)
  1467. {
  1468. struct ath10k_htt *htt = &ar->htt;
  1469. struct ieee80211_rx_status *status = &htt->rx_status;
  1470. struct htt_rx_offload_msdu *rx;
  1471. struct sk_buff *msdu;
  1472. size_t offset;
  1473. while ((msdu = __skb_dequeue(list))) {
  1474. /* Offloaded frames don't have Rx descriptor. Instead they have
  1475. * a short meta information header.
  1476. */
  1477. rx = (void *)msdu->data;
  1478. skb_put(msdu, sizeof(*rx));
  1479. skb_pull(msdu, sizeof(*rx));
  1480. if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
  1481. ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
  1482. dev_kfree_skb_any(msdu);
  1483. continue;
  1484. }
  1485. skb_put(msdu, __le16_to_cpu(rx->msdu_len));
  1486. /* Offloaded rx header length isn't multiple of 2 nor 4 so the
  1487. * actual payload is unaligned. Align the frame. Otherwise
  1488. * mac80211 complains. This shouldn't reduce performance much
  1489. * because these offloaded frames are rare.
  1490. */
  1491. offset = 4 - ((unsigned long)msdu->data & 3);
  1492. skb_put(msdu, offset);
  1493. memmove(msdu->data + offset, msdu->data, msdu->len);
  1494. skb_pull(msdu, offset);
  1495. /* FIXME: The frame is NWifi. Re-construct QoS Control
  1496. * if possible later.
  1497. */
  1498. memset(status, 0, sizeof(*status));
  1499. status->flag |= RX_FLAG_NO_SIGNAL_VAL;
  1500. ath10k_htt_rx_h_rx_offload_prot(status, msdu);
  1501. ath10k_htt_rx_h_channel(ar, status);
  1502. ath10k_process_rx(ar, status, msdu);
  1503. }
  1504. }
  1505. static void ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
  1506. {
  1507. struct ath10k_htt *htt = &ar->htt;
  1508. struct htt_resp *resp = (void *)skb->data;
  1509. struct ieee80211_rx_status *status = &htt->rx_status;
  1510. struct sk_buff_head list;
  1511. struct sk_buff_head amsdu;
  1512. u16 peer_id;
  1513. u16 msdu_count;
  1514. u8 vdev_id;
  1515. u8 tid;
  1516. bool offload;
  1517. bool frag;
  1518. int ret;
  1519. lockdep_assert_held(&htt->rx_ring.lock);
  1520. if (htt->rx_confused)
  1521. return;
  1522. skb_pull(skb, sizeof(resp->hdr));
  1523. skb_pull(skb, sizeof(resp->rx_in_ord_ind));
  1524. peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
  1525. msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
  1526. vdev_id = resp->rx_in_ord_ind.vdev_id;
  1527. tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
  1528. offload = !!(resp->rx_in_ord_ind.info &
  1529. HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
  1530. frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);
  1531. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1532. "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
  1533. vdev_id, peer_id, tid, offload, frag, msdu_count);
  1534. if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs)) {
  1535. ath10k_warn(ar, "dropping invalid in order rx indication\n");
  1536. return;
  1537. }
  1538. /* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
  1539. * extracted and processed.
  1540. */
  1541. __skb_queue_head_init(&list);
  1542. ret = ath10k_htt_rx_pop_paddr_list(htt, &resp->rx_in_ord_ind, &list);
  1543. if (ret < 0) {
  1544. ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
  1545. htt->rx_confused = true;
  1546. return;
  1547. }
  1548. /* Offloaded frames are very different and need to be handled
  1549. * separately.
  1550. */
  1551. if (offload)
  1552. ath10k_htt_rx_h_rx_offload(ar, &list);
  1553. while (!skb_queue_empty(&list)) {
  1554. __skb_queue_head_init(&amsdu);
  1555. ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
  1556. switch (ret) {
  1557. case 0:
  1558. /* Note: The in-order indication may report interleaved
  1559. * frames from different PPDUs meaning reported rx rate
  1560. * to mac80211 isn't accurate/reliable. It's still
  1561. * better to report something than nothing though. This
  1562. * should still give an idea about rx rate to the user.
  1563. */
  1564. ath10k_htt_rx_h_ppdu(ar, &amsdu, status);
  1565. ath10k_htt_rx_h_filter(ar, &amsdu, status);
  1566. ath10k_htt_rx_h_mpdu(ar, &amsdu, status);
  1567. ath10k_htt_rx_h_deliver(ar, &amsdu, status);
  1568. break;
  1569. case -EAGAIN:
  1570. /* fall through */
  1571. default:
  1572. /* Should not happen. */
  1573. ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
  1574. htt->rx_confused = true;
  1575. __skb_queue_purge(&list);
  1576. return;
  1577. }
  1578. }
  1579. tasklet_schedule(&htt->rx_replenish_task);
  1580. }
  1581. void ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
  1582. {
  1583. struct ath10k_htt *htt = &ar->htt;
  1584. struct htt_resp *resp = (struct htt_resp *)skb->data;
  1585. /* confirm alignment */
  1586. if (!IS_ALIGNED((unsigned long)skb->data, 4))
  1587. ath10k_warn(ar, "unaligned htt message, expect trouble\n");
  1588. ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
  1589. resp->hdr.msg_type);
  1590. switch (resp->hdr.msg_type) {
  1591. case HTT_T2H_MSG_TYPE_VERSION_CONF: {
  1592. htt->target_version_major = resp->ver_resp.major;
  1593. htt->target_version_minor = resp->ver_resp.minor;
  1594. complete(&htt->target_version_received);
  1595. break;
  1596. }
  1597. case HTT_T2H_MSG_TYPE_RX_IND:
  1598. spin_lock_bh(&htt->rx_ring.lock);
  1599. __skb_queue_tail(&htt->rx_compl_q, skb);
  1600. spin_unlock_bh(&htt->rx_ring.lock);
  1601. tasklet_schedule(&htt->txrx_compl_task);
  1602. return;
  1603. case HTT_T2H_MSG_TYPE_PEER_MAP: {
  1604. struct htt_peer_map_event ev = {
  1605. .vdev_id = resp->peer_map.vdev_id,
  1606. .peer_id = __le16_to_cpu(resp->peer_map.peer_id),
  1607. };
  1608. memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
  1609. ath10k_peer_map_event(htt, &ev);
  1610. break;
  1611. }
  1612. case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
  1613. struct htt_peer_unmap_event ev = {
  1614. .peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
  1615. };
  1616. ath10k_peer_unmap_event(htt, &ev);
  1617. break;
  1618. }
  1619. case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
  1620. struct htt_tx_done tx_done = {};
  1621. int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
  1622. tx_done.msdu_id =
  1623. __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
  1624. switch (status) {
  1625. case HTT_MGMT_TX_STATUS_OK:
  1626. break;
  1627. case HTT_MGMT_TX_STATUS_RETRY:
  1628. tx_done.no_ack = true;
  1629. break;
  1630. case HTT_MGMT_TX_STATUS_DROP:
  1631. tx_done.discard = true;
  1632. break;
  1633. }
  1634. spin_lock_bh(&htt->tx_lock);
  1635. ath10k_txrx_tx_unref(htt, &tx_done);
  1636. spin_unlock_bh(&htt->tx_lock);
  1637. break;
  1638. }
  1639. case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
  1640. spin_lock_bh(&htt->tx_lock);
  1641. __skb_queue_tail(&htt->tx_compl_q, skb);
  1642. spin_unlock_bh(&htt->tx_lock);
  1643. tasklet_schedule(&htt->txrx_compl_task);
  1644. return;
  1645. case HTT_T2H_MSG_TYPE_SEC_IND: {
  1646. struct ath10k *ar = htt->ar;
  1647. struct htt_security_indication *ev = &resp->security_indication;
  1648. ath10k_dbg(ar, ATH10K_DBG_HTT,
  1649. "sec ind peer_id %d unicast %d type %d\n",
  1650. __le16_to_cpu(ev->peer_id),
  1651. !!(ev->flags & HTT_SECURITY_IS_UNICAST),
  1652. MS(ev->flags, HTT_SECURITY_TYPE));
  1653. complete(&ar->install_key_done);
  1654. break;
  1655. }
  1656. case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
  1657. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
  1658. skb->data, skb->len);
  1659. ath10k_htt_rx_frag_handler(htt, &resp->rx_frag_ind);
  1660. break;
  1661. }
  1662. case HTT_T2H_MSG_TYPE_TEST:
  1663. /* FIX THIS */
  1664. break;
  1665. case HTT_T2H_MSG_TYPE_STATS_CONF:
  1666. trace_ath10k_htt_stats(ar, skb->data, skb->len);
  1667. break;
  1668. case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
  1669. /* Firmware can return tx frames if it's unable to fully
  1670. * process them and suspects host may be able to fix it. ath10k
  1671. * sends all tx frames as already inspected so this shouldn't
  1672. * happen unless fw has a bug.
  1673. */
  1674. ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
  1675. break;
  1676. case HTT_T2H_MSG_TYPE_RX_ADDBA:
  1677. ath10k_htt_rx_addba(ar, resp);
  1678. break;
  1679. case HTT_T2H_MSG_TYPE_RX_DELBA:
  1680. ath10k_htt_rx_delba(ar, resp);
  1681. break;
  1682. case HTT_T2H_MSG_TYPE_PKTLOG: {
  1683. struct ath10k_pktlog_hdr *hdr =
  1684. (struct ath10k_pktlog_hdr *)resp->pktlog_msg.payload;
  1685. trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
  1686. sizeof(*hdr) +
  1687. __le16_to_cpu(hdr->size));
  1688. break;
  1689. }
  1690. case HTT_T2H_MSG_TYPE_RX_FLUSH: {
  1691. /* Ignore this event because mac80211 takes care of Rx
  1692. * aggregation reordering.
  1693. */
  1694. break;
  1695. }
  1696. case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
  1697. spin_lock_bh(&htt->rx_ring.lock);
  1698. __skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
  1699. spin_unlock_bh(&htt->rx_ring.lock);
  1700. tasklet_schedule(&htt->txrx_compl_task);
  1701. return;
  1702. }
  1703. case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND:
  1704. /* FIXME: This WMI-TLV event is overlapping with 10.2
  1705. * CHAN_CHANGE - both being 0xF. Neither is being used in
  1706. * practice so no immediate action is necessary. Nevertheless
  1707. * HTT may need an abstraction layer like WMI has one day.
  1708. */
  1709. break;
  1710. default:
  1711. ath10k_warn(ar, "htt event (%d) not handled\n",
  1712. resp->hdr.msg_type);
  1713. ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
  1714. skb->data, skb->len);
  1715. break;
  1716. };
  1717. /* Free the indication buffer */
  1718. dev_kfree_skb_any(skb);
  1719. }
  1720. static void ath10k_htt_txrx_compl_task(unsigned long ptr)
  1721. {
  1722. struct ath10k_htt *htt = (struct ath10k_htt *)ptr;
  1723. struct ath10k *ar = htt->ar;
  1724. struct htt_resp *resp;
  1725. struct sk_buff *skb;
  1726. spin_lock_bh(&htt->tx_lock);
  1727. while ((skb = __skb_dequeue(&htt->tx_compl_q))) {
  1728. ath10k_htt_rx_frm_tx_compl(htt->ar, skb);
  1729. dev_kfree_skb_any(skb);
  1730. }
  1731. spin_unlock_bh(&htt->tx_lock);
  1732. spin_lock_bh(&htt->rx_ring.lock);
  1733. while ((skb = __skb_dequeue(&htt->rx_compl_q))) {
  1734. resp = (struct htt_resp *)skb->data;
  1735. ath10k_htt_rx_handler(htt, &resp->rx_ind);
  1736. dev_kfree_skb_any(skb);
  1737. }
  1738. while ((skb = __skb_dequeue(&htt->rx_in_ord_compl_q))) {
  1739. ath10k_htt_rx_in_ord_ind(ar, skb);
  1740. dev_kfree_skb_any(skb);
  1741. }
  1742. spin_unlock_bh(&htt->rx_ring.lock);
  1743. }