1f0553ca9SKalle Valo // SPDX-License-Identifier: ISC
25e3dd157SKalle Valo /*
35e3dd157SKalle Valo  * Copyright (c) 2005-2011 Atheros Communications Inc.
48b1083d6SKalle Valo  * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
5caee728aSVasanthakumar Thiagarajan  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
65e3dd157SKalle Valo  */
75e3dd157SKalle Valo 
8edb8236dSMichal Kazior #include "core.h"
95e3dd157SKalle Valo #include "htc.h"
105e3dd157SKalle Valo #include "htt.h"
115e3dd157SKalle Valo #include "txrx.h"
125e3dd157SKalle Valo #include "debug.h"
13a9bf0506SKalle Valo #include "trace.h"
14aa5b4fbcSMichal Kazior #include "mac.h"
155e3dd157SKalle Valo 
165e3dd157SKalle Valo #include <linux/log2.h>
17235b9c42SVenkateswara Naralasetty #include <linux/bitfield.h>
185e3dd157SKalle Valo 
195e3dd157SKalle Valo /* when under memory pressure rx ring refill may fail and needs a retry */
205e3dd157SKalle Valo #define HTT_RX_RING_REFILL_RETRY_MS 50
215e3dd157SKalle Valo 
225c86d97bSRajkumar Manoharan #define HTT_RX_RING_REFILL_RESCHED_MS 5
235c86d97bSRajkumar Manoharan 
24f6dc2095SMichal Kazior static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);
25f6dc2095SMichal Kazior 
26c545070eSMichal Kazior static struct sk_buff *
27a91a626bSGovind Singh ath10k_htt_rx_find_skb_paddr(struct ath10k *ar, u64 paddr)
28c545070eSMichal Kazior {
29c545070eSMichal Kazior 	struct ath10k_skb_rxcb *rxcb;
30c545070eSMichal Kazior 
31c545070eSMichal Kazior 	hash_for_each_possible(ar->htt.rx_ring.skb_table, rxcb, hlist, paddr)
32c545070eSMichal Kazior 		if (rxcb->paddr == paddr)
33c545070eSMichal Kazior 			return ATH10K_RXCB_SKB(rxcb);
34c545070eSMichal Kazior 
35c545070eSMichal Kazior 	WARN_ON_ONCE(1);
36c545070eSMichal Kazior 	return NULL;
37c545070eSMichal Kazior }
38c545070eSMichal Kazior 
395e3dd157SKalle Valo static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
405e3dd157SKalle Valo {
415e3dd157SKalle Valo 	struct sk_buff *skb;
42c545070eSMichal Kazior 	struct ath10k_skb_rxcb *rxcb;
43c545070eSMichal Kazior 	struct hlist_node *n;
445e3dd157SKalle Valo 	int i;
455e3dd157SKalle Valo 
46c545070eSMichal Kazior 	if (htt->rx_ring.in_ord_rx) {
47c545070eSMichal Kazior 		hash_for_each_safe(htt->rx_ring.skb_table, i, n, rxcb, hlist) {
48c545070eSMichal Kazior 			skb = ATH10K_RXCB_SKB(rxcb);
49c545070eSMichal Kazior 			dma_unmap_single(htt->ar->dev, rxcb->paddr,
50c545070eSMichal Kazior 					 skb->len + skb_tailroom(skb),
51c545070eSMichal Kazior 					 DMA_FROM_DEVICE);
52c545070eSMichal Kazior 			hash_del(&rxcb->hlist);
53c545070eSMichal Kazior 			dev_kfree_skb_any(skb);
54c545070eSMichal Kazior 		}
55c545070eSMichal Kazior 	} else {
56c545070eSMichal Kazior 		for (i = 0; i < htt->rx_ring.size; i++) {
575e3dd157SKalle Valo 			skb = htt->rx_ring.netbufs_ring[i];
58c545070eSMichal Kazior 			if (!skb)
59c545070eSMichal Kazior 				continue;
60c545070eSMichal Kazior 
61c545070eSMichal Kazior 			rxcb = ATH10K_SKB_RXCB(skb);
62c545070eSMichal Kazior 			dma_unmap_single(htt->ar->dev, rxcb->paddr,
635e3dd157SKalle Valo 					 skb->len + skb_tailroom(skb),
645e3dd157SKalle Valo 					 DMA_FROM_DEVICE);
655e3dd157SKalle Valo 			dev_kfree_skb_any(skb);
665e3dd157SKalle Valo 		}
67c545070eSMichal Kazior 	}
685e3dd157SKalle Valo 
695e3dd157SKalle Valo 	htt->rx_ring.fill_cnt = 0;
70c545070eSMichal Kazior 	hash_init(htt->rx_ring.skb_table);
71c545070eSMichal Kazior 	memset(htt->rx_ring.netbufs_ring, 0,
72c545070eSMichal Kazior 	       htt->rx_ring.size * sizeof(htt->rx_ring.netbufs_ring[0]));
735e3dd157SKalle Valo }
745e3dd157SKalle Valo 
75a91a626bSGovind Singh static size_t ath10k_htt_get_rx_ring_size_32(struct ath10k_htt *htt)
76a91a626bSGovind Singh {
77a91a626bSGovind Singh 	return htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring_32);
78a91a626bSGovind Singh }
79a91a626bSGovind Singh 
80a91a626bSGovind Singh static size_t ath10k_htt_get_rx_ring_size_64(struct ath10k_htt *htt)
81a91a626bSGovind Singh {
82a91a626bSGovind Singh 	return htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring_64);
83a91a626bSGovind Singh }
84a91a626bSGovind Singh 
85a91a626bSGovind Singh static void ath10k_htt_config_paddrs_ring_32(struct ath10k_htt *htt,
86a91a626bSGovind Singh 					     void *vaddr)
87a91a626bSGovind Singh {
88a91a626bSGovind Singh 	htt->rx_ring.paddrs_ring_32 = vaddr;
89a91a626bSGovind Singh }
90a91a626bSGovind Singh 
91a91a626bSGovind Singh static void ath10k_htt_config_paddrs_ring_64(struct ath10k_htt *htt,
92a91a626bSGovind Singh 					     void *vaddr)
93a91a626bSGovind Singh {
94a91a626bSGovind Singh 	htt->rx_ring.paddrs_ring_64 = vaddr;
95a91a626bSGovind Singh }
96a91a626bSGovind Singh 
97a91a626bSGovind Singh static void ath10k_htt_set_paddrs_ring_32(struct ath10k_htt *htt,
98a91a626bSGovind Singh 					  dma_addr_t paddr, int idx)
99a91a626bSGovind Singh {
100a91a626bSGovind Singh 	htt->rx_ring.paddrs_ring_32[idx] = __cpu_to_le32(paddr);
101a91a626bSGovind Singh }
102a91a626bSGovind Singh 
103a91a626bSGovind Singh static void ath10k_htt_set_paddrs_ring_64(struct ath10k_htt *htt,
104a91a626bSGovind Singh 					  dma_addr_t paddr, int idx)
105a91a626bSGovind Singh {
106a91a626bSGovind Singh 	htt->rx_ring.paddrs_ring_64[idx] = __cpu_to_le64(paddr);
107a91a626bSGovind Singh }
108a91a626bSGovind Singh 
109a91a626bSGovind Singh static void ath10k_htt_reset_paddrs_ring_32(struct ath10k_htt *htt, int idx)
110a91a626bSGovind Singh {
111a91a626bSGovind Singh 	htt->rx_ring.paddrs_ring_32[idx] = 0;
112a91a626bSGovind Singh }
113a91a626bSGovind Singh 
114a91a626bSGovind Singh static void ath10k_htt_reset_paddrs_ring_64(struct ath10k_htt *htt, int idx)
115a91a626bSGovind Singh {
116a91a626bSGovind Singh 	htt->rx_ring.paddrs_ring_64[idx] = 0;
117a91a626bSGovind Singh }
118a91a626bSGovind Singh 
119a91a626bSGovind Singh static void *ath10k_htt_get_vaddr_ring_32(struct ath10k_htt *htt)
120a91a626bSGovind Singh {
121a91a626bSGovind Singh 	return (void *)htt->rx_ring.paddrs_ring_32;
122a91a626bSGovind Singh }
123a91a626bSGovind Singh 
124a91a626bSGovind Singh static void *ath10k_htt_get_vaddr_ring_64(struct ath10k_htt *htt)
125a91a626bSGovind Singh {
126a91a626bSGovind Singh 	return (void *)htt->rx_ring.paddrs_ring_64;
127a91a626bSGovind Singh }
128a91a626bSGovind Singh 
1295e3dd157SKalle Valo static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
1305e3dd157SKalle Valo {
1315e3dd157SKalle Valo 	struct htt_rx_desc *rx_desc;
132c545070eSMichal Kazior 	struct ath10k_skb_rxcb *rxcb;
1335e3dd157SKalle Valo 	struct sk_buff *skb;
1345e3dd157SKalle Valo 	dma_addr_t paddr;
1355e3dd157SKalle Valo 	int ret = 0, idx;
1365e3dd157SKalle Valo 
137c545070eSMichal Kazior 	/* The Full Rx Reorder firmware has no way of telling the host
138c545070eSMichal Kazior 	 * implicitly when it copied HTT Rx Ring buffers to MAC Rx Ring.
139c545070eSMichal Kazior 	 * To keep things simple make sure ring is always half empty. This
140c545070eSMichal Kazior 	 * guarantees there'll be no replenishment overruns possible.
141c545070eSMichal Kazior 	 */
142c545070eSMichal Kazior 	BUILD_BUG_ON(HTT_RX_RING_FILL_LEVEL >= HTT_RX_RING_SIZE / 2);
143c545070eSMichal Kazior 
1448cc7f26cSKalle Valo 	idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
1455e3dd157SKalle Valo 	while (num > 0) {
1465e3dd157SKalle Valo 		skb = dev_alloc_skb(HTT_RX_BUF_SIZE + HTT_RX_DESC_ALIGN);
1475e3dd157SKalle Valo 		if (!skb) {
1485e3dd157SKalle Valo 			ret = -ENOMEM;
1495e3dd157SKalle Valo 			goto fail;
1505e3dd157SKalle Valo 		}
1515e3dd157SKalle Valo 
1525e3dd157SKalle Valo 		if (!IS_ALIGNED((unsigned long)skb->data, HTT_RX_DESC_ALIGN))
1535e3dd157SKalle Valo 			skb_pull(skb,
1545e3dd157SKalle Valo 				 PTR_ALIGN(skb->data, HTT_RX_DESC_ALIGN) -
1555e3dd157SKalle Valo 				 skb->data);
1565e3dd157SKalle Valo 
1575e3dd157SKalle Valo 		/* Clear rx_desc attention word before posting to Rx ring */
1585e3dd157SKalle Valo 		rx_desc = (struct htt_rx_desc *)skb->data;
1595e3dd157SKalle Valo 		rx_desc->attention.flags = __cpu_to_le32(0);
1605e3dd157SKalle Valo 
1615e3dd157SKalle Valo 		paddr = dma_map_single(htt->ar->dev, skb->data,
1625e3dd157SKalle Valo 				       skb->len + skb_tailroom(skb),
1635e3dd157SKalle Valo 				       DMA_FROM_DEVICE);
1645e3dd157SKalle Valo 
1655e3dd157SKalle Valo 		if (unlikely(dma_mapping_error(htt->ar->dev, paddr))) {
1665e3dd157SKalle Valo 			dev_kfree_skb_any(skb);
1675e3dd157SKalle Valo 			ret = -ENOMEM;
1685e3dd157SKalle Valo 			goto fail;
1695e3dd157SKalle Valo 		}
1705e3dd157SKalle Valo 
171c545070eSMichal Kazior 		rxcb = ATH10K_SKB_RXCB(skb);
172c545070eSMichal Kazior 		rxcb->paddr = paddr;
1735e3dd157SKalle Valo 		htt->rx_ring.netbufs_ring[idx] = skb;
1749a5511d5SErik Stromdahl 		ath10k_htt_set_paddrs_ring(htt, paddr, idx);
1755e3dd157SKalle Valo 		htt->rx_ring.fill_cnt++;
1765e3dd157SKalle Valo 
177c545070eSMichal Kazior 		if (htt->rx_ring.in_ord_rx) {
178c545070eSMichal Kazior 			hash_add(htt->rx_ring.skb_table,
179c545070eSMichal Kazior 				 &ATH10K_SKB_RXCB(skb)->hlist,
180a91a626bSGovind Singh 				 paddr);
181c545070eSMichal Kazior 		}
182c545070eSMichal Kazior 
1835e3dd157SKalle Valo 		num--;
1845e3dd157SKalle Valo 		idx++;
1855e3dd157SKalle Valo 		idx &= htt->rx_ring.size_mask;
1865e3dd157SKalle Valo 	}
1875e3dd157SKalle Valo 
1885e3dd157SKalle Valo fail:
1895de6dfc8SVasanthakumar Thiagarajan 	/*
1905de6dfc8SVasanthakumar Thiagarajan 	 * Make sure the rx buffer is updated before available buffer
1915de6dfc8SVasanthakumar Thiagarajan 	 * index to avoid any potential rx ring corruption.
1925de6dfc8SVasanthakumar Thiagarajan 	 */
1935de6dfc8SVasanthakumar Thiagarajan 	mb();
1948cc7f26cSKalle Valo 	*htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
1955e3dd157SKalle Valo 	return ret;
1965e3dd157SKalle Valo }
1975e3dd157SKalle Valo 
1985e3dd157SKalle Valo static int ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
1995e3dd157SKalle Valo {
2005e3dd157SKalle Valo 	lockdep_assert_held(&htt->rx_ring.lock);
2015e3dd157SKalle Valo 	return __ath10k_htt_rx_ring_fill_n(htt, num);
2025e3dd157SKalle Valo }
2035e3dd157SKalle Valo 
2045e3dd157SKalle Valo static void ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt *htt)
2055e3dd157SKalle Valo {
2066e712d42SMichal Kazior 	int ret, num_deficit, num_to_fill;
2075e3dd157SKalle Valo 
2086e712d42SMichal Kazior 	/* Refilling the whole RX ring buffer proves to be a bad idea. The
2096e712d42SMichal Kazior 	 * reason is RX may take up significant amount of CPU cycles and starve
2106e712d42SMichal Kazior 	 * other tasks, e.g. TX on an ethernet device while acting as a bridge
2116e712d42SMichal Kazior 	 * with ath10k wlan interface. This ended up with very poor performance
2126e712d42SMichal Kazior 	 * once CPU the host system was overwhelmed with RX on ath10k.
2136e712d42SMichal Kazior 	 *
2146e712d42SMichal Kazior 	 * By limiting the number of refills the replenishing occurs
2156e712d42SMichal Kazior 	 * progressively. This in turns makes use of the fact tasklets are
2166e712d42SMichal Kazior 	 * processed in FIFO order. This means actual RX processing can starve
2176e712d42SMichal Kazior 	 * out refilling. If there's not enough buffers on RX ring FW will not
2186e712d42SMichal Kazior 	 * report RX until it is refilled with enough buffers. This
2196e712d42SMichal Kazior 	 * automatically balances load wrt to CPU power.
2206e712d42SMichal Kazior 	 *
2216e712d42SMichal Kazior 	 * This probably comes at a cost of lower maximum throughput but
222d6dfe25cSMarcin Rokicki 	 * improves the average and stability.
223d6dfe25cSMarcin Rokicki 	 */
2245e3dd157SKalle Valo 	spin_lock_bh(&htt->rx_ring.lock);
2256e712d42SMichal Kazior 	num_deficit = htt->rx_ring.fill_level - htt->rx_ring.fill_cnt;
2266e712d42SMichal Kazior 	num_to_fill = min(ATH10K_HTT_MAX_NUM_REFILL, num_deficit);
2276e712d42SMichal Kazior 	num_deficit -= num_to_fill;
2285e3dd157SKalle Valo 	ret = ath10k_htt_rx_ring_fill_n(htt, num_to_fill);
2295e3dd157SKalle Valo 	if (ret == -ENOMEM) {
2305e3dd157SKalle Valo 		/*
2315e3dd157SKalle Valo 		 * Failed to fill it to the desired level -
2325e3dd157SKalle Valo 		 * we'll start a timer and try again next time.
2335e3dd157SKalle Valo 		 * As long as enough buffers are left in the ring for
2345e3dd157SKalle Valo 		 * another A-MPDU rx, no special recovery is needed.
2355e3dd157SKalle Valo 		 */
2365e3dd157SKalle Valo 		mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
2375e3dd157SKalle Valo 			  msecs_to_jiffies(HTT_RX_RING_REFILL_RETRY_MS));
2386e712d42SMichal Kazior 	} else if (num_deficit > 0) {
2395c86d97bSRajkumar Manoharan 		mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
2405c86d97bSRajkumar Manoharan 			  msecs_to_jiffies(HTT_RX_RING_REFILL_RESCHED_MS));
2415e3dd157SKalle Valo 	}
2425e3dd157SKalle Valo 	spin_unlock_bh(&htt->rx_ring.lock);
2435e3dd157SKalle Valo }
2445e3dd157SKalle Valo 
2457ac76764SKees Cook static void ath10k_htt_rx_ring_refill_retry(struct timer_list *t)
2465e3dd157SKalle Valo {
2477ac76764SKees Cook 	struct ath10k_htt *htt = from_timer(htt, t, rx_ring.refill_retry_timer);
248af762c0bSKalle Valo 
2495e3dd157SKalle Valo 	ath10k_htt_rx_msdu_buff_replenish(htt);
2505e3dd157SKalle Valo }
2515e3dd157SKalle Valo 
252c545070eSMichal Kazior int ath10k_htt_rx_ring_refill(struct ath10k *ar)
2533e841fd0SMichal Kazior {
254c545070eSMichal Kazior 	struct ath10k_htt *htt = &ar->htt;
255c545070eSMichal Kazior 	int ret;
2563e841fd0SMichal Kazior 
257de8781d7SGovind Singh 	if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
258f88d4934SErik Stromdahl 		return 0;
259f88d4934SErik Stromdahl 
260c545070eSMichal Kazior 	spin_lock_bh(&htt->rx_ring.lock);
261c545070eSMichal Kazior 	ret = ath10k_htt_rx_ring_fill_n(htt, (htt->rx_ring.fill_level -
262c545070eSMichal Kazior 					      htt->rx_ring.fill_cnt));
2633e841fd0SMichal Kazior 
264c545070eSMichal Kazior 	if (ret)
265c545070eSMichal Kazior 		ath10k_htt_rx_ring_free(htt);
266c545070eSMichal Kazior 
267168f75f1SBen Greear 	spin_unlock_bh(&htt->rx_ring.lock);
268168f75f1SBen Greear 
269c545070eSMichal Kazior 	return ret;
2703e841fd0SMichal Kazior }
2713e841fd0SMichal Kazior 
27295bf21f9SMichal Kazior void ath10k_htt_rx_free(struct ath10k_htt *htt)
2735e3dd157SKalle Valo {
274de8781d7SGovind Singh 	if (htt->ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
275f88d4934SErik Stromdahl 		return;
276f88d4934SErik Stromdahl 
2775e3dd157SKalle Valo 	del_timer_sync(&htt->rx_ring.refill_retry_timer);
2786c5151a9SMichal Kazior 
279deba1b9eSRajkumar Manoharan 	skb_queue_purge(&htt->rx_msdus_q);
280c545070eSMichal Kazior 	skb_queue_purge(&htt->rx_in_ord_compl_q);
281426e10eaSMichal Kazior 	skb_queue_purge(&htt->tx_fetch_ind_q);
2825e3dd157SKalle Valo 
283168f75f1SBen Greear 	spin_lock_bh(&htt->rx_ring.lock);
284c545070eSMichal Kazior 	ath10k_htt_rx_ring_free(htt);
285168f75f1SBen Greear 	spin_unlock_bh(&htt->rx_ring.lock);
2865e3dd157SKalle Valo 
2875e3dd157SKalle Valo 	dma_free_coherent(htt->ar->dev,
2889a5511d5SErik Stromdahl 			  ath10k_htt_get_rx_ring_size(htt),
2899a5511d5SErik Stromdahl 			  ath10k_htt_get_vaddr_ring(htt),
2905e3dd157SKalle Valo 			  htt->rx_ring.base_paddr);
2915e3dd157SKalle Valo 
2925e3dd157SKalle Valo 	dma_free_coherent(htt->ar->dev,
2935e3dd157SKalle Valo 			  sizeof(*htt->rx_ring.alloc_idx.vaddr),
2945e3dd157SKalle Valo 			  htt->rx_ring.alloc_idx.vaddr,
2955e3dd157SKalle Valo 			  htt->rx_ring.alloc_idx.paddr);
2965e3dd157SKalle Valo 
2975e3dd157SKalle Valo 	kfree(htt->rx_ring.netbufs_ring);
2985e3dd157SKalle Valo }
2995e3dd157SKalle Valo 
3005e3dd157SKalle Valo static inline struct sk_buff *ath10k_htt_rx_netbuf_pop(struct ath10k_htt *htt)
3015e3dd157SKalle Valo {
3027aa7a72aSMichal Kazior 	struct ath10k *ar = htt->ar;
3035e3dd157SKalle Valo 	int idx;
3045e3dd157SKalle Valo 	struct sk_buff *msdu;
3055e3dd157SKalle Valo 
30645967089SMichal Kazior 	lockdep_assert_held(&htt->rx_ring.lock);
3075e3dd157SKalle Valo 
3088d60ee87SMichal Kazior 	if (htt->rx_ring.fill_cnt == 0) {
3097aa7a72aSMichal Kazior 		ath10k_warn(ar, "tried to pop sk_buff from an empty rx ring\n");
3108d60ee87SMichal Kazior 		return NULL;
3118d60ee87SMichal Kazior 	}
3125e3dd157SKalle Valo 
3135e3dd157SKalle Valo 	idx = htt->rx_ring.sw_rd_idx.msdu_payld;
3145e3dd157SKalle Valo 	msdu = htt->rx_ring.netbufs_ring[idx];
3153e841fd0SMichal Kazior 	htt->rx_ring.netbufs_ring[idx] = NULL;
3169a5511d5SErik Stromdahl 	ath10k_htt_reset_paddrs_ring(htt, idx);
3175e3dd157SKalle Valo 
3185e3dd157SKalle Valo 	idx++;
3195e3dd157SKalle Valo 	idx &= htt->rx_ring.size_mask;
3205e3dd157SKalle Valo 	htt->rx_ring.sw_rd_idx.msdu_payld = idx;
3215e3dd157SKalle Valo 	htt->rx_ring.fill_cnt--;
3225e3dd157SKalle Valo 
3234de02806SMichal Kazior 	dma_unmap_single(htt->ar->dev,
3248582bf3bSMichal Kazior 			 ATH10K_SKB_RXCB(msdu)->paddr,
3254de02806SMichal Kazior 			 msdu->len + skb_tailroom(msdu),
3264de02806SMichal Kazior 			 DMA_FROM_DEVICE);
3274de02806SMichal Kazior 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
3284de02806SMichal Kazior 			msdu->data, msdu->len + skb_tailroom(msdu));
3294de02806SMichal Kazior 
3305e3dd157SKalle Valo 	return msdu;
3315e3dd157SKalle Valo }
3325e3dd157SKalle Valo 
333d84dd60fSJanusz Dziedzic /* return: < 0 fatal error, 0 - non chained msdu, 1 chained msdu */
3345e3dd157SKalle Valo static int ath10k_htt_rx_amsdu_pop(struct ath10k_htt *htt,
335f0e2770fSMichal Kazior 				   struct sk_buff_head *amsdu)
3365e3dd157SKalle Valo {
3377aa7a72aSMichal Kazior 	struct ath10k *ar = htt->ar;
3385e3dd157SKalle Valo 	int msdu_len, msdu_chaining = 0;
3399aa505d2SMichal Kazior 	struct sk_buff *msdu;
3405e3dd157SKalle Valo 	struct htt_rx_desc *rx_desc;
3415e3dd157SKalle Valo 
34245967089SMichal Kazior 	lockdep_assert_held(&htt->rx_ring.lock);
34345967089SMichal Kazior 
3449aa505d2SMichal Kazior 	for (;;) {
3455e3dd157SKalle Valo 		int last_msdu, msdu_len_invalid, msdu_chained;
3465e3dd157SKalle Valo 
3479aa505d2SMichal Kazior 		msdu = ath10k_htt_rx_netbuf_pop(htt);
3489aa505d2SMichal Kazior 		if (!msdu) {
3499aa505d2SMichal Kazior 			__skb_queue_purge(amsdu);
350e0bd7513SMichal Kazior 			return -ENOENT;
3519aa505d2SMichal Kazior 		}
3529aa505d2SMichal Kazior 
3539aa505d2SMichal Kazior 		__skb_queue_tail(amsdu, msdu);
3549aa505d2SMichal Kazior 
3555e3dd157SKalle Valo 		rx_desc = (struct htt_rx_desc *)msdu->data;
3565e3dd157SKalle Valo 
3575e3dd157SKalle Valo 		/* FIXME: we must report msdu payload since this is what caller
358d6dfe25cSMarcin Rokicki 		 * expects now
359d6dfe25cSMarcin Rokicki 		 */
3605e3dd157SKalle Valo 		skb_put(msdu, offsetof(struct htt_rx_desc, msdu_payload));
3615e3dd157SKalle Valo 		skb_pull(msdu, offsetof(struct htt_rx_desc, msdu_payload));
3625e3dd157SKalle Valo 
3635e3dd157SKalle Valo 		/*
3645e3dd157SKalle Valo 		 * Sanity check - confirm the HW is finished filling in the
3655e3dd157SKalle Valo 		 * rx data.
3665e3dd157SKalle Valo 		 * If the HW and SW are working correctly, then it's guaranteed
3675e3dd157SKalle Valo 		 * that the HW's MAC DMA is done before this point in the SW.
3685e3dd157SKalle Valo 		 * To prevent the case that we handle a stale Rx descriptor,
3695e3dd157SKalle Valo 		 * just assert for now until we have a way to recover.
3705e3dd157SKalle Valo 		 */
3715e3dd157SKalle Valo 		if (!(__le32_to_cpu(rx_desc->attention.flags)
3725e3dd157SKalle Valo 				& RX_ATTENTION_FLAGS_MSDU_DONE)) {
3739aa505d2SMichal Kazior 			__skb_queue_purge(amsdu);
374e0bd7513SMichal Kazior 			return -EIO;
3755e3dd157SKalle Valo 		}
3765e3dd157SKalle Valo 
3775e3dd157SKalle Valo 		msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
3785e3dd157SKalle Valo 					& (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
3795e3dd157SKalle Valo 					   RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
3801f5dbfbbSPeter Oh 		msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.common.info0),
3815e3dd157SKalle Valo 			      RX_MSDU_START_INFO0_MSDU_LENGTH);
3825e3dd157SKalle Valo 		msdu_chained = rx_desc->frag_info.ring2_more_count;
3835e3dd157SKalle Valo 
3845e3dd157SKalle Valo 		if (msdu_len_invalid)
3855e3dd157SKalle Valo 			msdu_len = 0;
3865e3dd157SKalle Valo 
3875e3dd157SKalle Valo 		skb_trim(msdu, 0);
3885e3dd157SKalle Valo 		skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
3895e3dd157SKalle Valo 		msdu_len -= msdu->len;
3905e3dd157SKalle Valo 
3919aa505d2SMichal Kazior 		/* Note: Chained buffers do not contain rx descriptor */
3925e3dd157SKalle Valo 		while (msdu_chained--) {
3939aa505d2SMichal Kazior 			msdu = ath10k_htt_rx_netbuf_pop(htt);
3949aa505d2SMichal Kazior 			if (!msdu) {
3959aa505d2SMichal Kazior 				__skb_queue_purge(amsdu);
396e0bd7513SMichal Kazior 				return -ENOENT;
397b30595aeSMichal Kazior 			}
398b30595aeSMichal Kazior 
3999aa505d2SMichal Kazior 			__skb_queue_tail(amsdu, msdu);
4009aa505d2SMichal Kazior 			skb_trim(msdu, 0);
4019aa505d2SMichal Kazior 			skb_put(msdu, min(msdu_len, HTT_RX_BUF_SIZE));
4029aa505d2SMichal Kazior 			msdu_len -= msdu->len;
403ede9c8e0SMichal Kazior 			msdu_chaining = 1;
4045e3dd157SKalle Valo 		}
4055e3dd157SKalle Valo 
4061f5dbfbbSPeter Oh 		last_msdu = __le32_to_cpu(rx_desc->msdu_end.common.info0) &
4075e3dd157SKalle Valo 				RX_MSDU_END_INFO0_LAST_MSDU;
4085e3dd157SKalle Valo 
409b04e204fSMichal Kazior 		trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
410a0883cf7SRajkumar Manoharan 					 sizeof(*rx_desc) - sizeof(u32));
4119aa505d2SMichal Kazior 
4129aa505d2SMichal Kazior 		if (last_msdu)
4135e3dd157SKalle Valo 			break;
414d8bb26b9SKalle Valo 	}
415d8bb26b9SKalle Valo 
4169aa505d2SMichal Kazior 	if (skb_queue_empty(amsdu))
417d84dd60fSJanusz Dziedzic 		msdu_chaining = -1;
418d84dd60fSJanusz Dziedzic 
4195e3dd157SKalle Valo 	/*
4205e3dd157SKalle Valo 	 * Don't refill the ring yet.
4215e3dd157SKalle Valo 	 *
4225e3dd157SKalle Valo 	 * First, the elements popped here are still in use - it is not
4235e3dd157SKalle Valo 	 * safe to overwrite them until the matching call to
4245e3dd157SKalle Valo 	 * mpdu_desc_list_next. Second, for efficiency it is preferable to
4255e3dd157SKalle Valo 	 * refill the rx ring with 1 PPDU's worth of rx buffers (something
4265e3dd157SKalle Valo 	 * like 32 x 3 buffers), rather than one MPDU's worth of rx buffers
4275e3dd157SKalle Valo 	 * (something like 3 buffers). Consequently, we'll rely on the txrx
4285e3dd157SKalle Valo 	 * SW to tell us when it is done pulling all the PPDU's rx buffers
4295e3dd157SKalle Valo 	 * out of the rx ring, and then refill it just once.
4305e3dd157SKalle Valo 	 */
4315e3dd157SKalle Valo 
4325e3dd157SKalle Valo 	return msdu_chaining;
4335e3dd157SKalle Valo }
4345e3dd157SKalle Valo 
435c545070eSMichal Kazior static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
436a91a626bSGovind Singh 					       u64 paddr)
437c545070eSMichal Kazior {
438c545070eSMichal Kazior 	struct ath10k *ar = htt->ar;
439c545070eSMichal Kazior 	struct ath10k_skb_rxcb *rxcb;
440c545070eSMichal Kazior 	struct sk_buff *msdu;
441c545070eSMichal Kazior 
442c545070eSMichal Kazior 	lockdep_assert_held(&htt->rx_ring.lock);
443c545070eSMichal Kazior 
444c545070eSMichal Kazior 	msdu = ath10k_htt_rx_find_skb_paddr(ar, paddr);
445c545070eSMichal Kazior 	if (!msdu)
446c545070eSMichal Kazior 		return NULL;
447c545070eSMichal Kazior 
448c545070eSMichal Kazior 	rxcb = ATH10K_SKB_RXCB(msdu);
449c545070eSMichal Kazior 	hash_del(&rxcb->hlist);
450c545070eSMichal Kazior 	htt->rx_ring.fill_cnt--;
451c545070eSMichal Kazior 
452c545070eSMichal Kazior 	dma_unmap_single(htt->ar->dev, rxcb->paddr,
453c545070eSMichal Kazior 			 msdu->len + skb_tailroom(msdu),
454c545070eSMichal Kazior 			 DMA_FROM_DEVICE);
455c545070eSMichal Kazior 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
456c545070eSMichal Kazior 			msdu->data, msdu->len + skb_tailroom(msdu));
457c545070eSMichal Kazior 
458c545070eSMichal Kazior 	return msdu;
459c545070eSMichal Kazior }
460c545070eSMichal Kazior 
46185bd0107SYu Wang static inline void ath10k_htt_append_frag_list(struct sk_buff *skb_head,
46285bd0107SYu Wang 					       struct sk_buff *frag_list,
46385bd0107SYu Wang 					       unsigned int frag_len)
46485bd0107SYu Wang {
46585bd0107SYu Wang 	skb_shinfo(skb_head)->frag_list = frag_list;
46685bd0107SYu Wang 	skb_head->data_len = frag_len;
46785bd0107SYu Wang 	skb_head->len += skb_head->data_len;
46885bd0107SYu Wang }
46985bd0107SYu Wang 
47085bd0107SYu Wang static int ath10k_htt_rx_handle_amsdu_mon_32(struct ath10k_htt *htt,
47185bd0107SYu Wang 					     struct sk_buff *msdu,
47285bd0107SYu Wang 					     struct htt_rx_in_ord_msdu_desc **msdu_desc)
47385bd0107SYu Wang {
47485bd0107SYu Wang 	struct ath10k *ar = htt->ar;
47585bd0107SYu Wang 	u32 paddr;
47685bd0107SYu Wang 	struct sk_buff *frag_buf;
47785bd0107SYu Wang 	struct sk_buff *prev_frag_buf;
47885bd0107SYu Wang 	u8 last_frag;
47985bd0107SYu Wang 	struct htt_rx_in_ord_msdu_desc *ind_desc = *msdu_desc;
48085bd0107SYu Wang 	struct htt_rx_desc *rxd;
48185bd0107SYu Wang 	int amsdu_len = __le16_to_cpu(ind_desc->msdu_len);
48285bd0107SYu Wang 
48385bd0107SYu Wang 	rxd = (void *)msdu->data;
48485bd0107SYu Wang 	trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
48585bd0107SYu Wang 
48685bd0107SYu Wang 	skb_put(msdu, sizeof(struct htt_rx_desc));
48785bd0107SYu Wang 	skb_pull(msdu, sizeof(struct htt_rx_desc));
48885bd0107SYu Wang 	skb_put(msdu, min(amsdu_len, HTT_RX_MSDU_SIZE));
48985bd0107SYu Wang 	amsdu_len -= msdu->len;
49085bd0107SYu Wang 
49185bd0107SYu Wang 	last_frag = ind_desc->reserved;
49285bd0107SYu Wang 	if (last_frag) {
49385bd0107SYu Wang 		if (amsdu_len) {
49485bd0107SYu Wang 			ath10k_warn(ar, "invalid amsdu len %u, left %d",
49585bd0107SYu Wang 				    __le16_to_cpu(ind_desc->msdu_len),
49685bd0107SYu Wang 				    amsdu_len);
49785bd0107SYu Wang 		}
49885bd0107SYu Wang 		return 0;
49985bd0107SYu Wang 	}
50085bd0107SYu Wang 
50185bd0107SYu Wang 	ind_desc++;
50285bd0107SYu Wang 	paddr = __le32_to_cpu(ind_desc->msdu_paddr);
50385bd0107SYu Wang 	frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
50485bd0107SYu Wang 	if (!frag_buf) {
50585bd0107SYu Wang 		ath10k_warn(ar, "failed to pop frag-1 paddr: 0x%x", paddr);
50685bd0107SYu Wang 		return -ENOENT;
50785bd0107SYu Wang 	}
50885bd0107SYu Wang 
50985bd0107SYu Wang 	skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
51085bd0107SYu Wang 	ath10k_htt_append_frag_list(msdu, frag_buf, amsdu_len);
51185bd0107SYu Wang 
51285bd0107SYu Wang 	amsdu_len -= frag_buf->len;
51385bd0107SYu Wang 	prev_frag_buf = frag_buf;
51485bd0107SYu Wang 	last_frag = ind_desc->reserved;
51585bd0107SYu Wang 	while (!last_frag) {
51685bd0107SYu Wang 		ind_desc++;
51785bd0107SYu Wang 		paddr = __le32_to_cpu(ind_desc->msdu_paddr);
51885bd0107SYu Wang 		frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
51985bd0107SYu Wang 		if (!frag_buf) {
52085bd0107SYu Wang 			ath10k_warn(ar, "failed to pop frag-n paddr: 0x%x",
52185bd0107SYu Wang 				    paddr);
52285bd0107SYu Wang 			prev_frag_buf->next = NULL;
52385bd0107SYu Wang 			return -ENOENT;
52485bd0107SYu Wang 		}
52585bd0107SYu Wang 
52685bd0107SYu Wang 		skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
52785bd0107SYu Wang 		last_frag = ind_desc->reserved;
52885bd0107SYu Wang 		amsdu_len -= frag_buf->len;
52985bd0107SYu Wang 
53085bd0107SYu Wang 		prev_frag_buf->next = frag_buf;
53185bd0107SYu Wang 		prev_frag_buf = frag_buf;
53285bd0107SYu Wang 	}
53385bd0107SYu Wang 
53485bd0107SYu Wang 	if (amsdu_len) {
53585bd0107SYu Wang 		ath10k_warn(ar, "invalid amsdu len %u, left %d",
53685bd0107SYu Wang 			    __le16_to_cpu(ind_desc->msdu_len), amsdu_len);
53785bd0107SYu Wang 	}
53885bd0107SYu Wang 
53985bd0107SYu Wang 	*msdu_desc = ind_desc;
54085bd0107SYu Wang 
54185bd0107SYu Wang 	prev_frag_buf->next = NULL;
54285bd0107SYu Wang 	return 0;
54385bd0107SYu Wang }
54485bd0107SYu Wang 
54585bd0107SYu Wang static int
54685bd0107SYu Wang ath10k_htt_rx_handle_amsdu_mon_64(struct ath10k_htt *htt,
54785bd0107SYu Wang 				  struct sk_buff *msdu,
54885bd0107SYu Wang 				  struct htt_rx_in_ord_msdu_desc_ext **msdu_desc)
54985bd0107SYu Wang {
55085bd0107SYu Wang 	struct ath10k *ar = htt->ar;
55185bd0107SYu Wang 	u64 paddr;
55285bd0107SYu Wang 	struct sk_buff *frag_buf;
55385bd0107SYu Wang 	struct sk_buff *prev_frag_buf;
55485bd0107SYu Wang 	u8 last_frag;
55585bd0107SYu Wang 	struct htt_rx_in_ord_msdu_desc_ext *ind_desc = *msdu_desc;
55685bd0107SYu Wang 	struct htt_rx_desc *rxd;
55785bd0107SYu Wang 	int amsdu_len = __le16_to_cpu(ind_desc->msdu_len);
55885bd0107SYu Wang 
55985bd0107SYu Wang 	rxd = (void *)msdu->data;
56085bd0107SYu Wang 	trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
56185bd0107SYu Wang 
56285bd0107SYu Wang 	skb_put(msdu, sizeof(struct htt_rx_desc));
56385bd0107SYu Wang 	skb_pull(msdu, sizeof(struct htt_rx_desc));
56485bd0107SYu Wang 	skb_put(msdu, min(amsdu_len, HTT_RX_MSDU_SIZE));
56585bd0107SYu Wang 	amsdu_len -= msdu->len;
56685bd0107SYu Wang 
56785bd0107SYu Wang 	last_frag = ind_desc->reserved;
56885bd0107SYu Wang 	if (last_frag) {
56985bd0107SYu Wang 		if (amsdu_len) {
57085bd0107SYu Wang 			ath10k_warn(ar, "invalid amsdu len %u, left %d",
57185bd0107SYu Wang 				    __le16_to_cpu(ind_desc->msdu_len),
57285bd0107SYu Wang 				    amsdu_len);
57385bd0107SYu Wang 		}
57485bd0107SYu Wang 		return 0;
57585bd0107SYu Wang 	}
57685bd0107SYu Wang 
57785bd0107SYu Wang 	ind_desc++;
57885bd0107SYu Wang 	paddr = __le64_to_cpu(ind_desc->msdu_paddr);
57985bd0107SYu Wang 	frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
58085bd0107SYu Wang 	if (!frag_buf) {
58185bd0107SYu Wang 		ath10k_warn(ar, "failed to pop frag-1 paddr: 0x%llx", paddr);
58285bd0107SYu Wang 		return -ENOENT;
58385bd0107SYu Wang 	}
58485bd0107SYu Wang 
58585bd0107SYu Wang 	skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
58685bd0107SYu Wang 	ath10k_htt_append_frag_list(msdu, frag_buf, amsdu_len);
58785bd0107SYu Wang 
58885bd0107SYu Wang 	amsdu_len -= frag_buf->len;
58985bd0107SYu Wang 	prev_frag_buf = frag_buf;
59085bd0107SYu Wang 	last_frag = ind_desc->reserved;
59185bd0107SYu Wang 	while (!last_frag) {
59285bd0107SYu Wang 		ind_desc++;
59385bd0107SYu Wang 		paddr = __le64_to_cpu(ind_desc->msdu_paddr);
59485bd0107SYu Wang 		frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
59585bd0107SYu Wang 		if (!frag_buf) {
59685bd0107SYu Wang 			ath10k_warn(ar, "failed to pop frag-n paddr: 0x%llx",
59785bd0107SYu Wang 				    paddr);
59885bd0107SYu Wang 			prev_frag_buf->next = NULL;
59985bd0107SYu Wang 			return -ENOENT;
60085bd0107SYu Wang 		}
60185bd0107SYu Wang 
60285bd0107SYu Wang 		skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
60385bd0107SYu Wang 		last_frag = ind_desc->reserved;
60485bd0107SYu Wang 		amsdu_len -= frag_buf->len;
60585bd0107SYu Wang 
60685bd0107SYu Wang 		prev_frag_buf->next = frag_buf;
60785bd0107SYu Wang 		prev_frag_buf = frag_buf;
60885bd0107SYu Wang 	}
60985bd0107SYu Wang 
61085bd0107SYu Wang 	if (amsdu_len) {
61185bd0107SYu Wang 		ath10k_warn(ar, "invalid amsdu len %u, left %d",
61285bd0107SYu Wang 			    __le16_to_cpu(ind_desc->msdu_len), amsdu_len);
61385bd0107SYu Wang 	}
61485bd0107SYu Wang 
61585bd0107SYu Wang 	*msdu_desc = ind_desc;
61685bd0107SYu Wang 
61785bd0107SYu Wang 	prev_frag_buf->next = NULL;
61885bd0107SYu Wang 	return 0;
61985bd0107SYu Wang }
62085bd0107SYu Wang 
6213b0b55b1SGovind Singh static int ath10k_htt_rx_pop_paddr32_list(struct ath10k_htt *htt,
622c545070eSMichal Kazior 					  struct htt_rx_in_ord_ind *ev,
623c545070eSMichal Kazior 					  struct sk_buff_head *list)
624c545070eSMichal Kazior {
625c545070eSMichal Kazior 	struct ath10k *ar = htt->ar;
6263b0b55b1SGovind Singh 	struct htt_rx_in_ord_msdu_desc *msdu_desc = ev->msdu_descs32;
627c545070eSMichal Kazior 	struct htt_rx_desc *rxd;
628c545070eSMichal Kazior 	struct sk_buff *msdu;
62985bd0107SYu Wang 	int msdu_count, ret;
630c545070eSMichal Kazior 	bool is_offload;
631c545070eSMichal Kazior 	u32 paddr;
632c545070eSMichal Kazior 
633c545070eSMichal Kazior 	lockdep_assert_held(&htt->rx_ring.lock);
634c545070eSMichal Kazior 
635c545070eSMichal Kazior 	msdu_count = __le16_to_cpu(ev->msdu_count);
636c545070eSMichal Kazior 	is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
637c545070eSMichal Kazior 
638c545070eSMichal Kazior 	while (msdu_count--) {
639c545070eSMichal Kazior 		paddr = __le32_to_cpu(msdu_desc->msdu_paddr);
640c545070eSMichal Kazior 
641c545070eSMichal Kazior 		msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
642c545070eSMichal Kazior 		if (!msdu) {
643c545070eSMichal Kazior 			__skb_queue_purge(list);
644c545070eSMichal Kazior 			return -ENOENT;
645c545070eSMichal Kazior 		}
646c545070eSMichal Kazior 
64785bd0107SYu Wang 		if (!is_offload && ar->monitor_arvif) {
64885bd0107SYu Wang 			ret = ath10k_htt_rx_handle_amsdu_mon_32(htt, msdu,
64985bd0107SYu Wang 								&msdu_desc);
65085bd0107SYu Wang 			if (ret) {
65185bd0107SYu Wang 				__skb_queue_purge(list);
65285bd0107SYu Wang 				return ret;
65385bd0107SYu Wang 			}
65485bd0107SYu Wang 			__skb_queue_tail(list, msdu);
65585bd0107SYu Wang 			msdu_desc++;
65685bd0107SYu Wang 			continue;
65785bd0107SYu Wang 		}
65885bd0107SYu Wang 
659c545070eSMichal Kazior 		__skb_queue_tail(list, msdu);
660c545070eSMichal Kazior 
661c545070eSMichal Kazior 		if (!is_offload) {
662c545070eSMichal Kazior 			rxd = (void *)msdu->data;
663c545070eSMichal Kazior 
664c545070eSMichal Kazior 			trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
665c545070eSMichal Kazior 
666c545070eSMichal Kazior 			skb_put(msdu, sizeof(*rxd));
667c545070eSMichal Kazior 			skb_pull(msdu, sizeof(*rxd));
668c545070eSMichal Kazior 			skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
669c545070eSMichal Kazior 
670c545070eSMichal Kazior 			if (!(__le32_to_cpu(rxd->attention.flags) &
671c545070eSMichal Kazior 			      RX_ATTENTION_FLAGS_MSDU_DONE)) {
672c545070eSMichal Kazior 				ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
673c545070eSMichal Kazior 				return -EIO;
674c545070eSMichal Kazior 			}
675c545070eSMichal Kazior 		}
676c545070eSMichal Kazior 
677c545070eSMichal Kazior 		msdu_desc++;
678c545070eSMichal Kazior 	}
679c545070eSMichal Kazior 
680c545070eSMichal Kazior 	return 0;
681c545070eSMichal Kazior }
682c545070eSMichal Kazior 
6833b0b55b1SGovind Singh static int ath10k_htt_rx_pop_paddr64_list(struct ath10k_htt *htt,
6843b0b55b1SGovind Singh 					  struct htt_rx_in_ord_ind *ev,
6853b0b55b1SGovind Singh 					  struct sk_buff_head *list)
6863b0b55b1SGovind Singh {
6873b0b55b1SGovind Singh 	struct ath10k *ar = htt->ar;
6883b0b55b1SGovind Singh 	struct htt_rx_in_ord_msdu_desc_ext *msdu_desc = ev->msdu_descs64;
6893b0b55b1SGovind Singh 	struct htt_rx_desc *rxd;
6903b0b55b1SGovind Singh 	struct sk_buff *msdu;
69185bd0107SYu Wang 	int msdu_count, ret;
6923b0b55b1SGovind Singh 	bool is_offload;
6933b0b55b1SGovind Singh 	u64 paddr;
6943b0b55b1SGovind Singh 
6953b0b55b1SGovind Singh 	lockdep_assert_held(&htt->rx_ring.lock);
6963b0b55b1SGovind Singh 
6973b0b55b1SGovind Singh 	msdu_count = __le16_to_cpu(ev->msdu_count);
6983b0b55b1SGovind Singh 	is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
6993b0b55b1SGovind Singh 
7003b0b55b1SGovind Singh 	while (msdu_count--) {
7013b0b55b1SGovind Singh 		paddr = __le64_to_cpu(msdu_desc->msdu_paddr);
7023b0b55b1SGovind Singh 		msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
7033b0b55b1SGovind Singh 		if (!msdu) {
7043b0b55b1SGovind Singh 			__skb_queue_purge(list);
7053b0b55b1SGovind Singh 			return -ENOENT;
7063b0b55b1SGovind Singh 		}
7073b0b55b1SGovind Singh 
70885bd0107SYu Wang 		if (!is_offload && ar->monitor_arvif) {
70985bd0107SYu Wang 			ret = ath10k_htt_rx_handle_amsdu_mon_64(htt, msdu,
71085bd0107SYu Wang 								&msdu_desc);
71185bd0107SYu Wang 			if (ret) {
71285bd0107SYu Wang 				__skb_queue_purge(list);
71385bd0107SYu Wang 				return ret;
71485bd0107SYu Wang 			}
71585bd0107SYu Wang 			__skb_queue_tail(list, msdu);
71685bd0107SYu Wang 			msdu_desc++;
71785bd0107SYu Wang 			continue;
71885bd0107SYu Wang 		}
71985bd0107SYu Wang 
7203b0b55b1SGovind Singh 		__skb_queue_tail(list, msdu);
7213b0b55b1SGovind Singh 
7223b0b55b1SGovind Singh 		if (!is_offload) {
7233b0b55b1SGovind Singh 			rxd = (void *)msdu->data;
7243b0b55b1SGovind Singh 
7253b0b55b1SGovind Singh 			trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
7263b0b55b1SGovind Singh 
7273b0b55b1SGovind Singh 			skb_put(msdu, sizeof(*rxd));
7283b0b55b1SGovind Singh 			skb_pull(msdu, sizeof(*rxd));
7293b0b55b1SGovind Singh 			skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
7303b0b55b1SGovind Singh 
7313b0b55b1SGovind Singh 			if (!(__le32_to_cpu(rxd->attention.flags) &
7323b0b55b1SGovind Singh 			      RX_ATTENTION_FLAGS_MSDU_DONE)) {
7333b0b55b1SGovind Singh 				ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
7343b0b55b1SGovind Singh 				return -EIO;
7353b0b55b1SGovind Singh 			}
7363b0b55b1SGovind Singh 		}
7373b0b55b1SGovind Singh 
7383b0b55b1SGovind Singh 		msdu_desc++;
7393b0b55b1SGovind Singh 	}
7403b0b55b1SGovind Singh 
7413b0b55b1SGovind Singh 	return 0;
7423b0b55b1SGovind Singh }
7433b0b55b1SGovind Singh 
74495bf21f9SMichal Kazior int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
7455e3dd157SKalle Valo {
7467aa7a72aSMichal Kazior 	struct ath10k *ar = htt->ar;
7475e3dd157SKalle Valo 	dma_addr_t paddr;
748a91a626bSGovind Singh 	void *vaddr, *vaddr_ring;
749bd8bdbb6SKalle Valo 	size_t size;
7505e3dd157SKalle Valo 	struct timer_list *timer = &htt->rx_ring.refill_retry_timer;
7515e3dd157SKalle Valo 
752de8781d7SGovind Singh 	if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
753f88d4934SErik Stromdahl 		return 0;
754f88d4934SErik Stromdahl 
75551fc7d74SMichal Kazior 	htt->rx_confused = false;
75651fc7d74SMichal Kazior 
757fe2407a8SMichal Kazior 	/* XXX: The fill level could be changed during runtime in response to
758fe2407a8SMichal Kazior 	 * the host processing latency. Is this really worth it?
759fe2407a8SMichal Kazior 	 */
760fe2407a8SMichal Kazior 	htt->rx_ring.size = HTT_RX_RING_SIZE;
761fe2407a8SMichal Kazior 	htt->rx_ring.size_mask = htt->rx_ring.size - 1;
762bb8d0d15SGovind Singh 	htt->rx_ring.fill_level = ar->hw_params.rx_ring_fill_level;
763fe2407a8SMichal Kazior 
7645e3dd157SKalle Valo 	if (!is_power_of_2(htt->rx_ring.size)) {
7657aa7a72aSMichal Kazior 		ath10k_warn(ar, "htt rx ring size is not power of 2\n");
7665e3dd157SKalle Valo 		return -EINVAL;
7675e3dd157SKalle Valo 	}
7685e3dd157SKalle Valo 
7695e3dd157SKalle Valo 	htt->rx_ring.netbufs_ring =
7706396bb22SKees Cook 		kcalloc(htt->rx_ring.size, sizeof(struct sk_buff *),
7715e3dd157SKalle Valo 			GFP_KERNEL);
7725e3dd157SKalle Valo 	if (!htt->rx_ring.netbufs_ring)
7735e3dd157SKalle Valo 		goto err_netbuf;
7745e3dd157SKalle Valo 
7759a5511d5SErik Stromdahl 	size = ath10k_htt_get_rx_ring_size(htt);
776bd8bdbb6SKalle Valo 
777a91a626bSGovind Singh 	vaddr_ring = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_KERNEL);
778a91a626bSGovind Singh 	if (!vaddr_ring)
7795e3dd157SKalle Valo 		goto err_dma_ring;
7805e3dd157SKalle Valo 
7819a5511d5SErik Stromdahl 	ath10k_htt_config_paddrs_ring(htt, vaddr_ring);
7825e3dd157SKalle Valo 	htt->rx_ring.base_paddr = paddr;
7835e3dd157SKalle Valo 
7845e3dd157SKalle Valo 	vaddr = dma_alloc_coherent(htt->ar->dev,
7855e3dd157SKalle Valo 				   sizeof(*htt->rx_ring.alloc_idx.vaddr),
786d6cb23b5SFelix Fietkau 				   &paddr, GFP_KERNEL);
7875e3dd157SKalle Valo 	if (!vaddr)
7885e3dd157SKalle Valo 		goto err_dma_idx;
7895e3dd157SKalle Valo 
7905e3dd157SKalle Valo 	htt->rx_ring.alloc_idx.vaddr = vaddr;
7915e3dd157SKalle Valo 	htt->rx_ring.alloc_idx.paddr = paddr;
792c545070eSMichal Kazior 	htt->rx_ring.sw_rd_idx.msdu_payld = htt->rx_ring.size_mask;
7935e3dd157SKalle Valo 	*htt->rx_ring.alloc_idx.vaddr = 0;
7945e3dd157SKalle Valo 
7955e3dd157SKalle Valo 	/* Initialize the Rx refill retry timer */
7967ac76764SKees Cook 	timer_setup(timer, ath10k_htt_rx_ring_refill_retry, 0);
7975e3dd157SKalle Valo 
7985e3dd157SKalle Valo 	spin_lock_init(&htt->rx_ring.lock);
7995e3dd157SKalle Valo 
8005e3dd157SKalle Valo 	htt->rx_ring.fill_cnt = 0;
801c545070eSMichal Kazior 	htt->rx_ring.sw_rd_idx.msdu_payld = 0;
802c545070eSMichal Kazior 	hash_init(htt->rx_ring.skb_table);
8035e3dd157SKalle Valo 
804deba1b9eSRajkumar Manoharan 	skb_queue_head_init(&htt->rx_msdus_q);
805c545070eSMichal Kazior 	skb_queue_head_init(&htt->rx_in_ord_compl_q);
806426e10eaSMichal Kazior 	skb_queue_head_init(&htt->tx_fetch_ind_q);
8073128b3d8SRajkumar Manoharan 	atomic_set(&htt->num_mpdus_ready, 0);
8086c5151a9SMichal Kazior 
8097aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
8105e3dd157SKalle Valo 		   htt->rx_ring.size, htt->rx_ring.fill_level);
8115e3dd157SKalle Valo 	return 0;
8125e3dd157SKalle Valo 
8135e3dd157SKalle Valo err_dma_idx:
8145e3dd157SKalle Valo 	dma_free_coherent(htt->ar->dev,
8159a5511d5SErik Stromdahl 			  ath10k_htt_get_rx_ring_size(htt),
816a91a626bSGovind Singh 			  vaddr_ring,
8175e3dd157SKalle Valo 			  htt->rx_ring.base_paddr);
8185e3dd157SKalle Valo err_dma_ring:
8195e3dd157SKalle Valo 	kfree(htt->rx_ring.netbufs_ring);
8205e3dd157SKalle Valo err_netbuf:
8215e3dd157SKalle Valo 	return -ENOMEM;
8225e3dd157SKalle Valo }
8235e3dd157SKalle Valo 
8247aa7a72aSMichal Kazior static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
8257aa7a72aSMichal Kazior 					  enum htt_rx_mpdu_encrypt_type type)
8265e3dd157SKalle Valo {
8275e3dd157SKalle Valo 	switch (type) {
828890d3b2aSMichal Kazior 	case HTT_RX_MPDU_ENCRYPT_NONE:
829890d3b2aSMichal Kazior 		return 0;
8305e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_WEP40:
8315e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_WEP104:
832890d3b2aSMichal Kazior 		return IEEE80211_WEP_IV_LEN;
8335e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
8345e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
835890d3b2aSMichal Kazior 		return IEEE80211_TKIP_IV_LEN;
8365e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
837890d3b2aSMichal Kazior 		return IEEE80211_CCMP_HDR_LEN;
8387eccb738SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
8397eccb738SVasanthakumar Thiagarajan 		return IEEE80211_CCMP_256_HDR_LEN;
8407eccb738SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
8417eccb738SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
8427eccb738SVasanthakumar Thiagarajan 		return IEEE80211_GCMP_HDR_LEN;
843890d3b2aSMichal Kazior 	case HTT_RX_MPDU_ENCRYPT_WEP128:
844890d3b2aSMichal Kazior 	case HTT_RX_MPDU_ENCRYPT_WAPI:
845890d3b2aSMichal Kazior 		break;
846890d3b2aSMichal Kazior 	}
847890d3b2aSMichal Kazior 
848890d3b2aSMichal Kazior 	ath10k_warn(ar, "unsupported encryption type %d\n", type);
8495e3dd157SKalle Valo 	return 0;
8505e3dd157SKalle Valo }
8515e3dd157SKalle Valo 
852890d3b2aSMichal Kazior #define MICHAEL_MIC_LEN 8
8535e3dd157SKalle Valo 
854307aeb31SVasanthakumar Thiagarajan static int ath10k_htt_rx_crypto_mic_len(struct ath10k *ar,
8557aa7a72aSMichal Kazior 					enum htt_rx_mpdu_encrypt_type type)
8565e3dd157SKalle Valo {
8575e3dd157SKalle Valo 	switch (type) {
8585e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_NONE:
8595e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_WEP40:
8605e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_WEP104:
8615e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
8625e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
863307aeb31SVasanthakumar Thiagarajan 		return 0;
8645e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
865890d3b2aSMichal Kazior 		return IEEE80211_CCMP_MIC_LEN;
8667eccb738SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
8677eccb738SVasanthakumar Thiagarajan 		return IEEE80211_CCMP_256_MIC_LEN;
8687eccb738SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
8697eccb738SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
8707eccb738SVasanthakumar Thiagarajan 		return IEEE80211_GCMP_MIC_LEN;
871890d3b2aSMichal Kazior 	case HTT_RX_MPDU_ENCRYPT_WEP128:
872890d3b2aSMichal Kazior 	case HTT_RX_MPDU_ENCRYPT_WAPI:
873890d3b2aSMichal Kazior 		break;
8745e3dd157SKalle Valo 	}
8755e3dd157SKalle Valo 
876890d3b2aSMichal Kazior 	ath10k_warn(ar, "unsupported encryption type %d\n", type);
8775e3dd157SKalle Valo 	return 0;
8785e3dd157SKalle Valo }
8795e3dd157SKalle Valo 
880307aeb31SVasanthakumar Thiagarajan static int ath10k_htt_rx_crypto_icv_len(struct ath10k *ar,
881307aeb31SVasanthakumar Thiagarajan 					enum htt_rx_mpdu_encrypt_type type)
882307aeb31SVasanthakumar Thiagarajan {
883307aeb31SVasanthakumar Thiagarajan 	switch (type) {
884307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_NONE:
885307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
886307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
887307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
888307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
889307aeb31SVasanthakumar Thiagarajan 		return 0;
890307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_WEP40:
891307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_WEP104:
892307aeb31SVasanthakumar Thiagarajan 		return IEEE80211_WEP_ICV_LEN;
893307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
894307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
895307aeb31SVasanthakumar Thiagarajan 		return IEEE80211_TKIP_ICV_LEN;
896307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_WEP128:
897307aeb31SVasanthakumar Thiagarajan 	case HTT_RX_MPDU_ENCRYPT_WAPI:
898307aeb31SVasanthakumar Thiagarajan 		break;
899307aeb31SVasanthakumar Thiagarajan 	}
900307aeb31SVasanthakumar Thiagarajan 
901307aeb31SVasanthakumar Thiagarajan 	ath10k_warn(ar, "unsupported encryption type %d\n", type);
902307aeb31SVasanthakumar Thiagarajan 	return 0;
903307aeb31SVasanthakumar Thiagarajan }
904307aeb31SVasanthakumar Thiagarajan 
905f6dc2095SMichal Kazior struct amsdu_subframe_hdr {
906f6dc2095SMichal Kazior 	u8 dst[ETH_ALEN];
907f6dc2095SMichal Kazior 	u8 src[ETH_ALEN];
908f6dc2095SMichal Kazior 	__be16 len;
909f6dc2095SMichal Kazior } __packed;
910f6dc2095SMichal Kazior 
9116986fdd6SMichal Kazior #define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)
9126986fdd6SMichal Kazior 
91391493e8eSChristian Lamparter static inline u8 ath10k_bw_to_mac80211_bw(u8 bw)
91491493e8eSChristian Lamparter {
91591493e8eSChristian Lamparter 	u8 ret = 0;
91691493e8eSChristian Lamparter 
91791493e8eSChristian Lamparter 	switch (bw) {
91891493e8eSChristian Lamparter 	case 0:
91991493e8eSChristian Lamparter 		ret = RATE_INFO_BW_20;
92091493e8eSChristian Lamparter 		break;
92191493e8eSChristian Lamparter 	case 1:
92291493e8eSChristian Lamparter 		ret = RATE_INFO_BW_40;
92391493e8eSChristian Lamparter 		break;
92491493e8eSChristian Lamparter 	case 2:
92591493e8eSChristian Lamparter 		ret = RATE_INFO_BW_80;
92691493e8eSChristian Lamparter 		break;
92791493e8eSChristian Lamparter 	case 3:
92891493e8eSChristian Lamparter 		ret = RATE_INFO_BW_160;
92991493e8eSChristian Lamparter 		break;
93091493e8eSChristian Lamparter 	}
93191493e8eSChristian Lamparter 
93291493e8eSChristian Lamparter 	return ret;
93391493e8eSChristian Lamparter }
93491493e8eSChristian Lamparter 
93587326c97SJanusz Dziedzic static void ath10k_htt_rx_h_rates(struct ath10k *ar,
936b9fd8a84SMichal Kazior 				  struct ieee80211_rx_status *status,
937b9fd8a84SMichal Kazior 				  struct htt_rx_desc *rxd)
93873539b40SJanusz Dziedzic {
9395528e032SMichal Kazior 	struct ieee80211_supported_band *sband;
9405528e032SMichal Kazior 	u8 cck, rate, bw, sgi, mcs, nss;
94173539b40SJanusz Dziedzic 	u8 preamble = 0;
9426986fdd6SMichal Kazior 	u8 group_id;
943b9fd8a84SMichal Kazior 	u32 info1, info2, info3;
94473539b40SJanusz Dziedzic 
945b9fd8a84SMichal Kazior 	info1 = __le32_to_cpu(rxd->ppdu_start.info1);
946b9fd8a84SMichal Kazior 	info2 = __le32_to_cpu(rxd->ppdu_start.info2);
947b9fd8a84SMichal Kazior 	info3 = __le32_to_cpu(rxd->ppdu_start.info3);
948b9fd8a84SMichal Kazior 
949b9fd8a84SMichal Kazior 	preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
95073539b40SJanusz Dziedzic 
95173539b40SJanusz Dziedzic 	switch (preamble) {
95273539b40SJanusz Dziedzic 	case HTT_RX_LEGACY:
9535528e032SMichal Kazior 		/* To get legacy rate index band is required. Since band can't
9545528e032SMichal Kazior 		 * be undefined check if freq is non-zero.
9555528e032SMichal Kazior 		 */
9565528e032SMichal Kazior 		if (!status->freq)
9575528e032SMichal Kazior 			return;
9585528e032SMichal Kazior 
959b9fd8a84SMichal Kazior 		cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
960b9fd8a84SMichal Kazior 		rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
9615528e032SMichal Kazior 		rate &= ~RX_PPDU_START_RATE_FLAG;
96273539b40SJanusz Dziedzic 
9635528e032SMichal Kazior 		sband = &ar->mac.sbands[status->band];
9644b7f353bSYanbo Li 		status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate, cck);
96573539b40SJanusz Dziedzic 		break;
96673539b40SJanusz Dziedzic 	case HTT_RX_HT:
96773539b40SJanusz Dziedzic 	case HTT_RX_HT_WITH_TXBF:
968b9fd8a84SMichal Kazior 		/* HT-SIG - Table 20-11 in info2 and info3 */
969b9fd8a84SMichal Kazior 		mcs = info2 & 0x1F;
97073539b40SJanusz Dziedzic 		nss = mcs >> 3;
971b9fd8a84SMichal Kazior 		bw = (info2 >> 7) & 1;
972b9fd8a84SMichal Kazior 		sgi = (info3 >> 7) & 1;
97373539b40SJanusz Dziedzic 
97473539b40SJanusz Dziedzic 		status->rate_idx = mcs;
975da6a4352SJohannes Berg 		status->encoding = RX_ENC_HT;
97673539b40SJanusz Dziedzic 		if (sgi)
9777fdd69c5SJohannes Berg 			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
97873539b40SJanusz Dziedzic 		if (bw)
979da6a4352SJohannes Berg 			status->bw = RATE_INFO_BW_40;
98073539b40SJanusz Dziedzic 		break;
98173539b40SJanusz Dziedzic 	case HTT_RX_VHT:
98273539b40SJanusz Dziedzic 	case HTT_RX_VHT_WITH_TXBF:
983b9fd8a84SMichal Kazior 		/* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
984d6dfe25cSMarcin Rokicki 		 * TODO check this
985d6dfe25cSMarcin Rokicki 		 */
986b9fd8a84SMichal Kazior 		bw = info2 & 3;
987b9fd8a84SMichal Kazior 		sgi = info3 & 1;
9886986fdd6SMichal Kazior 		group_id = (info2 >> 4) & 0x3F;
9896986fdd6SMichal Kazior 
9906986fdd6SMichal Kazior 		if (GROUP_ID_IS_SU_MIMO(group_id)) {
9916986fdd6SMichal Kazior 			mcs = (info3 >> 4) & 0x0F;
9926986fdd6SMichal Kazior 			nss = ((info2 >> 10) & 0x07) + 1;
9936986fdd6SMichal Kazior 		} else {
9946986fdd6SMichal Kazior 			/* Hardware doesn't decode VHT-SIG-B into Rx descriptor
9956986fdd6SMichal Kazior 			 * so it's impossible to decode MCS. Also since
9966986fdd6SMichal Kazior 			 * firmware consumes Group Id Management frames host
9976986fdd6SMichal Kazior 			 * has no knowledge regarding group/user position
9986986fdd6SMichal Kazior 			 * mapping so it's impossible to pick the correct Nsts
9996986fdd6SMichal Kazior 			 * from VHT-SIG-A1.
10006986fdd6SMichal Kazior 			 *
10016986fdd6SMichal Kazior 			 * Bandwidth and SGI are valid so report the rateinfo
10026986fdd6SMichal Kazior 			 * on best-effort basis.
10036986fdd6SMichal Kazior 			 */
10046986fdd6SMichal Kazior 			mcs = 0;
10056986fdd6SMichal Kazior 			nss = 1;
10066986fdd6SMichal Kazior 		}
100773539b40SJanusz Dziedzic 
10086ccea107SManikanta Pubbisetty 		if (mcs > 0x09) {
10096ccea107SManikanta Pubbisetty 			ath10k_warn(ar, "invalid MCS received %u\n", mcs);
10106ccea107SManikanta Pubbisetty 			ath10k_warn(ar, "rxd %08x mpdu start %08x %08x msdu start %08x %08x ppdu start %08x %08x %08x %08x %08x\n",
10116ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->attention.flags),
10126ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->mpdu_start.info0),
10136ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->mpdu_start.info1),
10146ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->msdu_start.common.info0),
10156ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->msdu_start.common.info1),
10166ccea107SManikanta Pubbisetty 				    rxd->ppdu_start.info0,
10176ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->ppdu_start.info1),
10186ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->ppdu_start.info2),
10196ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->ppdu_start.info3),
10206ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->ppdu_start.info4));
10216ccea107SManikanta Pubbisetty 
10226ccea107SManikanta Pubbisetty 			ath10k_warn(ar, "msdu end %08x mpdu end %08x\n",
10236ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->msdu_end.common.info0),
10246ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->mpdu_end.info0));
10256ccea107SManikanta Pubbisetty 
10266ccea107SManikanta Pubbisetty 			ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL,
10276ccea107SManikanta Pubbisetty 					"rx desc msdu payload: ",
10286ccea107SManikanta Pubbisetty 					rxd->msdu_payload, 50);
10296ccea107SManikanta Pubbisetty 		}
10306ccea107SManikanta Pubbisetty 
103173539b40SJanusz Dziedzic 		status->rate_idx = mcs;
10328613c948SJohannes Berg 		status->nss = nss;
103373539b40SJanusz Dziedzic 
103473539b40SJanusz Dziedzic 		if (sgi)
10357fdd69c5SJohannes Berg 			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
103673539b40SJanusz Dziedzic 
103791493e8eSChristian Lamparter 		status->bw = ath10k_bw_to_mac80211_bw(bw);
1038da6a4352SJohannes Berg 		status->encoding = RX_ENC_VHT;
103973539b40SJanusz Dziedzic 		break;
104073539b40SJanusz Dziedzic 	default:
104173539b40SJanusz Dziedzic 		break;
104273539b40SJanusz Dziedzic 	}
104373539b40SJanusz Dziedzic }
104473539b40SJanusz Dziedzic 
1045500ff9f9SMichal Kazior static struct ieee80211_channel *
1046500ff9f9SMichal Kazior ath10k_htt_rx_h_peer_channel(struct ath10k *ar, struct htt_rx_desc *rxd)
1047500ff9f9SMichal Kazior {
1048500ff9f9SMichal Kazior 	struct ath10k_peer *peer;
1049500ff9f9SMichal Kazior 	struct ath10k_vif *arvif;
1050500ff9f9SMichal Kazior 	struct cfg80211_chan_def def;
1051500ff9f9SMichal Kazior 	u16 peer_id;
1052500ff9f9SMichal Kazior 
1053500ff9f9SMichal Kazior 	lockdep_assert_held(&ar->data_lock);
1054500ff9f9SMichal Kazior 
1055500ff9f9SMichal Kazior 	if (!rxd)
1056500ff9f9SMichal Kazior 		return NULL;
1057500ff9f9SMichal Kazior 
1058500ff9f9SMichal Kazior 	if (rxd->attention.flags &
1059500ff9f9SMichal Kazior 	    __cpu_to_le32(RX_ATTENTION_FLAGS_PEER_IDX_INVALID))
1060500ff9f9SMichal Kazior 		return NULL;
1061500ff9f9SMichal Kazior 
10621f5dbfbbSPeter Oh 	if (!(rxd->msdu_end.common.info0 &
1063500ff9f9SMichal Kazior 	      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU)))
1064500ff9f9SMichal Kazior 		return NULL;
1065500ff9f9SMichal Kazior 
1066500ff9f9SMichal Kazior 	peer_id = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1067500ff9f9SMichal Kazior 		     RX_MPDU_START_INFO0_PEER_IDX);
1068500ff9f9SMichal Kazior 
1069500ff9f9SMichal Kazior 	peer = ath10k_peer_find_by_id(ar, peer_id);
1070500ff9f9SMichal Kazior 	if (!peer)
1071500ff9f9SMichal Kazior 		return NULL;
1072500ff9f9SMichal Kazior 
1073500ff9f9SMichal Kazior 	arvif = ath10k_get_arvif(ar, peer->vdev_id);
1074500ff9f9SMichal Kazior 	if (WARN_ON_ONCE(!arvif))
1075500ff9f9SMichal Kazior 		return NULL;
1076500ff9f9SMichal Kazior 
1077569fba2cSMohammed Shafi Shajakhan 	if (ath10k_mac_vif_chan(arvif->vif, &def))
1078500ff9f9SMichal Kazior 		return NULL;
1079500ff9f9SMichal Kazior 
1080500ff9f9SMichal Kazior 	return def.chan;
1081500ff9f9SMichal Kazior }
1082500ff9f9SMichal Kazior 
1083500ff9f9SMichal Kazior static struct ieee80211_channel *
1084500ff9f9SMichal Kazior ath10k_htt_rx_h_vdev_channel(struct ath10k *ar, u32 vdev_id)
1085500ff9f9SMichal Kazior {
1086500ff9f9SMichal Kazior 	struct ath10k_vif *arvif;
1087500ff9f9SMichal Kazior 	struct cfg80211_chan_def def;
1088500ff9f9SMichal Kazior 
1089500ff9f9SMichal Kazior 	lockdep_assert_held(&ar->data_lock);
1090500ff9f9SMichal Kazior 
1091500ff9f9SMichal Kazior 	list_for_each_entry(arvif, &ar->arvifs, list) {
1092500ff9f9SMichal Kazior 		if (arvif->vdev_id == vdev_id &&
1093500ff9f9SMichal Kazior 		    ath10k_mac_vif_chan(arvif->vif, &def) == 0)
1094500ff9f9SMichal Kazior 			return def.chan;
1095500ff9f9SMichal Kazior 	}
1096500ff9f9SMichal Kazior 
1097500ff9f9SMichal Kazior 	return NULL;
1098500ff9f9SMichal Kazior }
1099500ff9f9SMichal Kazior 
1100500ff9f9SMichal Kazior static void
1101500ff9f9SMichal Kazior ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw *hw,
1102500ff9f9SMichal Kazior 			      struct ieee80211_chanctx_conf *conf,
1103500ff9f9SMichal Kazior 			      void *data)
1104500ff9f9SMichal Kazior {
1105500ff9f9SMichal Kazior 	struct cfg80211_chan_def *def = data;
1106500ff9f9SMichal Kazior 
1107500ff9f9SMichal Kazior 	*def = conf->def;
1108500ff9f9SMichal Kazior }
1109500ff9f9SMichal Kazior 
1110500ff9f9SMichal Kazior static struct ieee80211_channel *
1111500ff9f9SMichal Kazior ath10k_htt_rx_h_any_channel(struct ath10k *ar)
1112500ff9f9SMichal Kazior {
1113500ff9f9SMichal Kazior 	struct cfg80211_chan_def def = {};
1114500ff9f9SMichal Kazior 
1115500ff9f9SMichal Kazior 	ieee80211_iter_chan_contexts_atomic(ar->hw,
1116500ff9f9SMichal Kazior 					    ath10k_htt_rx_h_any_chan_iter,
1117500ff9f9SMichal Kazior 					    &def);
1118500ff9f9SMichal Kazior 
1119500ff9f9SMichal Kazior 	return def.chan;
1120500ff9f9SMichal Kazior }
1121500ff9f9SMichal Kazior 
112236653f05SJanusz Dziedzic static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
1123500ff9f9SMichal Kazior 				    struct ieee80211_rx_status *status,
1124500ff9f9SMichal Kazior 				    struct htt_rx_desc *rxd,
1125500ff9f9SMichal Kazior 				    u32 vdev_id)
112636653f05SJanusz Dziedzic {
112736653f05SJanusz Dziedzic 	struct ieee80211_channel *ch;
112836653f05SJanusz Dziedzic 
112936653f05SJanusz Dziedzic 	spin_lock_bh(&ar->data_lock);
113036653f05SJanusz Dziedzic 	ch = ar->scan_channel;
113136653f05SJanusz Dziedzic 	if (!ch)
113236653f05SJanusz Dziedzic 		ch = ar->rx_channel;
1133500ff9f9SMichal Kazior 	if (!ch)
1134500ff9f9SMichal Kazior 		ch = ath10k_htt_rx_h_peer_channel(ar, rxd);
1135500ff9f9SMichal Kazior 	if (!ch)
1136500ff9f9SMichal Kazior 		ch = ath10k_htt_rx_h_vdev_channel(ar, vdev_id);
1137500ff9f9SMichal Kazior 	if (!ch)
1138500ff9f9SMichal Kazior 		ch = ath10k_htt_rx_h_any_channel(ar);
11392ce9b25cSRajkumar Manoharan 	if (!ch)
11402ce9b25cSRajkumar Manoharan 		ch = ar->tgt_oper_chan;
114136653f05SJanusz Dziedzic 	spin_unlock_bh(&ar->data_lock);
114236653f05SJanusz Dziedzic 
114336653f05SJanusz Dziedzic 	if (!ch)
114436653f05SJanusz Dziedzic 		return false;
114536653f05SJanusz Dziedzic 
114636653f05SJanusz Dziedzic 	status->band = ch->band;
114736653f05SJanusz Dziedzic 	status->freq = ch->center_freq;
114836653f05SJanusz Dziedzic 
114936653f05SJanusz Dziedzic 	return true;
115036653f05SJanusz Dziedzic }
115136653f05SJanusz Dziedzic 
1152b9fd8a84SMichal Kazior static void ath10k_htt_rx_h_signal(struct ath10k *ar,
1153b9fd8a84SMichal Kazior 				   struct ieee80211_rx_status *status,
1154b9fd8a84SMichal Kazior 				   struct htt_rx_desc *rxd)
1155b9fd8a84SMichal Kazior {
11568241253dSNorik Dzhandzhapanyan 	int i;
11578241253dSNorik Dzhandzhapanyan 
11588241253dSNorik Dzhandzhapanyan 	for (i = 0; i < IEEE80211_MAX_CHAINS ; i++) {
11598241253dSNorik Dzhandzhapanyan 		status->chains &= ~BIT(i);
11608241253dSNorik Dzhandzhapanyan 
11618241253dSNorik Dzhandzhapanyan 		if (rxd->ppdu_start.rssi_chains[i].pri20_mhz != 0x80) {
11628241253dSNorik Dzhandzhapanyan 			status->chain_signal[i] = ATH10K_DEFAULT_NOISE_FLOOR +
11638241253dSNorik Dzhandzhapanyan 				rxd->ppdu_start.rssi_chains[i].pri20_mhz;
11648241253dSNorik Dzhandzhapanyan 
11658241253dSNorik Dzhandzhapanyan 			status->chains |= BIT(i);
11668241253dSNorik Dzhandzhapanyan 		}
11678241253dSNorik Dzhandzhapanyan 	}
11688241253dSNorik Dzhandzhapanyan 
1169b9fd8a84SMichal Kazior 	/* FIXME: Get real NF */
1170b9fd8a84SMichal Kazior 	status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
1171b9fd8a84SMichal Kazior 			 rxd->ppdu_start.rssi_comb;
1172b9fd8a84SMichal Kazior 	status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
1173b9fd8a84SMichal Kazior }
1174b9fd8a84SMichal Kazior 
1175b9fd8a84SMichal Kazior static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
1176b9fd8a84SMichal Kazior 				    struct ieee80211_rx_status *status,
1177b9fd8a84SMichal Kazior 				    struct htt_rx_desc *rxd)
1178b9fd8a84SMichal Kazior {
1179b9fd8a84SMichal Kazior 	/* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
1180b9fd8a84SMichal Kazior 	 * means all prior MSDUs in a PPDU are reported to mac80211 without the
1181b9fd8a84SMichal Kazior 	 * TSF. Is it worth holding frames until end of PPDU is known?
1182b9fd8a84SMichal Kazior 	 *
1183b9fd8a84SMichal Kazior 	 * FIXME: Can we get/compute 64bit TSF?
1184b9fd8a84SMichal Kazior 	 */
11853ec79e3aSMichal Kazior 	status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
1186b9fd8a84SMichal Kazior 	status->flag |= RX_FLAG_MACTIME_END;
1187b9fd8a84SMichal Kazior }
1188b9fd8a84SMichal Kazior 
1189b9fd8a84SMichal Kazior static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
1190b9fd8a84SMichal Kazior 				 struct sk_buff_head *amsdu,
1191500ff9f9SMichal Kazior 				 struct ieee80211_rx_status *status,
1192500ff9f9SMichal Kazior 				 u32 vdev_id)
1193b9fd8a84SMichal Kazior {
1194b9fd8a84SMichal Kazior 	struct sk_buff *first;
1195b9fd8a84SMichal Kazior 	struct htt_rx_desc *rxd;
1196b9fd8a84SMichal Kazior 	bool is_first_ppdu;
1197b9fd8a84SMichal Kazior 	bool is_last_ppdu;
1198b9fd8a84SMichal Kazior 
1199b9fd8a84SMichal Kazior 	if (skb_queue_empty(amsdu))
1200b9fd8a84SMichal Kazior 		return;
1201b9fd8a84SMichal Kazior 
1202b9fd8a84SMichal Kazior 	first = skb_peek(amsdu);
1203b9fd8a84SMichal Kazior 	rxd = (void *)first->data - sizeof(*rxd);
1204b9fd8a84SMichal Kazior 
1205b9fd8a84SMichal Kazior 	is_first_ppdu = !!(rxd->attention.flags &
1206b9fd8a84SMichal Kazior 			   __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
1207b9fd8a84SMichal Kazior 	is_last_ppdu = !!(rxd->attention.flags &
1208b9fd8a84SMichal Kazior 			  __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));
1209b9fd8a84SMichal Kazior 
1210b9fd8a84SMichal Kazior 	if (is_first_ppdu) {
1211b9fd8a84SMichal Kazior 		/* New PPDU starts so clear out the old per-PPDU status. */
1212b9fd8a84SMichal Kazior 		status->freq = 0;
1213b9fd8a84SMichal Kazior 		status->rate_idx = 0;
12148613c948SJohannes Berg 		status->nss = 0;
1215da6a4352SJohannes Berg 		status->encoding = RX_ENC_LEGACY;
1216da6a4352SJohannes Berg 		status->bw = RATE_INFO_BW_20;
121747cc0ca9SMatthias Frei 
12187fdd69c5SJohannes Berg 		status->flag &= ~RX_FLAG_MACTIME_END;
1219b9fd8a84SMichal Kazior 		status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1220b9fd8a84SMichal Kazior 
122147cc0ca9SMatthias Frei 		status->flag &= ~(RX_FLAG_AMPDU_IS_LAST);
122247cc0ca9SMatthias Frei 		status->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
122347cc0ca9SMatthias Frei 		status->ampdu_reference = ar->ampdu_reference;
122447cc0ca9SMatthias Frei 
1225b9fd8a84SMichal Kazior 		ath10k_htt_rx_h_signal(ar, status, rxd);
1226500ff9f9SMichal Kazior 		ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
1227b9fd8a84SMichal Kazior 		ath10k_htt_rx_h_rates(ar, status, rxd);
1228b9fd8a84SMichal Kazior 	}
1229b9fd8a84SMichal Kazior 
123047cc0ca9SMatthias Frei 	if (is_last_ppdu) {
1231b9fd8a84SMichal Kazior 		ath10k_htt_rx_h_mactime(ar, status, rxd);
123247cc0ca9SMatthias Frei 
123347cc0ca9SMatthias Frei 		/* set ampdu last segment flag */
123447cc0ca9SMatthias Frei 		status->flag |= RX_FLAG_AMPDU_IS_LAST;
123547cc0ca9SMatthias Frei 		ar->ampdu_reference++;
123647cc0ca9SMatthias Frei 	}
1237b9fd8a84SMichal Kazior }
1238b9fd8a84SMichal Kazior 
123976f5329aSJanusz Dziedzic static const char * const tid_to_ac[] = {
124076f5329aSJanusz Dziedzic 	"BE",
124176f5329aSJanusz Dziedzic 	"BK",
124276f5329aSJanusz Dziedzic 	"BK",
124376f5329aSJanusz Dziedzic 	"BE",
124476f5329aSJanusz Dziedzic 	"VI",
124576f5329aSJanusz Dziedzic 	"VI",
124676f5329aSJanusz Dziedzic 	"VO",
124776f5329aSJanusz Dziedzic 	"VO",
124876f5329aSJanusz Dziedzic };
124976f5329aSJanusz Dziedzic 
125076f5329aSJanusz Dziedzic static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
125176f5329aSJanusz Dziedzic {
125276f5329aSJanusz Dziedzic 	u8 *qc;
125376f5329aSJanusz Dziedzic 	int tid;
125476f5329aSJanusz Dziedzic 
125576f5329aSJanusz Dziedzic 	if (!ieee80211_is_data_qos(hdr->frame_control))
125676f5329aSJanusz Dziedzic 		return "";
125776f5329aSJanusz Dziedzic 
125876f5329aSJanusz Dziedzic 	qc = ieee80211_get_qos_ctl(hdr);
125976f5329aSJanusz Dziedzic 	tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
126076f5329aSJanusz Dziedzic 	if (tid < 8)
126176f5329aSJanusz Dziedzic 		snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
126276f5329aSJanusz Dziedzic 	else
126376f5329aSJanusz Dziedzic 		snprintf(out, size, "tid %d", tid);
126476f5329aSJanusz Dziedzic 
126576f5329aSJanusz Dziedzic 	return out;
126676f5329aSJanusz Dziedzic }
126776f5329aSJanusz Dziedzic 
1268deba1b9eSRajkumar Manoharan static void ath10k_htt_rx_h_queue_msdu(struct ath10k *ar,
126985f6d7cfSJanusz Dziedzic 				       struct ieee80211_rx_status *rx_status,
127085f6d7cfSJanusz Dziedzic 				       struct sk_buff *skb)
127173539b40SJanusz Dziedzic {
127273539b40SJanusz Dziedzic 	struct ieee80211_rx_status *status;
1273deba1b9eSRajkumar Manoharan 
1274deba1b9eSRajkumar Manoharan 	status = IEEE80211_SKB_RXCB(skb);
1275deba1b9eSRajkumar Manoharan 	*status = *rx_status;
1276deba1b9eSRajkumar Manoharan 
127762652555SBob Copeland 	skb_queue_tail(&ar->htt.rx_msdus_q, skb);
1278deba1b9eSRajkumar Manoharan }
1279deba1b9eSRajkumar Manoharan 
1280deba1b9eSRajkumar Manoharan static void ath10k_process_rx(struct ath10k *ar, struct sk_buff *skb)
1281deba1b9eSRajkumar Manoharan {
1282deba1b9eSRajkumar Manoharan 	struct ieee80211_rx_status *status;
128376f5329aSJanusz Dziedzic 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
128476f5329aSJanusz Dziedzic 	char tid[32];
128573539b40SJanusz Dziedzic 
128685f6d7cfSJanusz Dziedzic 	status = IEEE80211_SKB_RXCB(skb);
128773539b40SJanusz Dziedzic 
12887aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_DATA,
12897fdd69c5SJohannes Berg 		   "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",
129085f6d7cfSJanusz Dziedzic 		   skb,
129185f6d7cfSJanusz Dziedzic 		   skb->len,
129276f5329aSJanusz Dziedzic 		   ieee80211_get_SA(hdr),
129376f5329aSJanusz Dziedzic 		   ath10k_get_tid(hdr, tid, sizeof(tid)),
129476f5329aSJanusz Dziedzic 		   is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
129576f5329aSJanusz Dziedzic 							"mcast" : "ucast",
129676f5329aSJanusz Dziedzic 		   (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
1297da6a4352SJohannes Berg 		   (status->encoding == RX_ENC_LEGACY) ? "legacy" : "",
1298da6a4352SJohannes Berg 		   (status->encoding == RX_ENC_HT) ? "ht" : "",
1299da6a4352SJohannes Berg 		   (status->encoding == RX_ENC_VHT) ? "vht" : "",
1300da6a4352SJohannes Berg 		   (status->bw == RATE_INFO_BW_40) ? "40" : "",
1301da6a4352SJohannes Berg 		   (status->bw == RATE_INFO_BW_80) ? "80" : "",
1302da6a4352SJohannes Berg 		   (status->bw == RATE_INFO_BW_160) ? "160" : "",
13037fdd69c5SJohannes Berg 		   status->enc_flags & RX_ENC_FLAG_SHORT_GI ? "sgi " : "",
130473539b40SJanusz Dziedzic 		   status->rate_idx,
13058613c948SJohannes Berg 		   status->nss,
130673539b40SJanusz Dziedzic 		   status->freq,
130787326c97SJanusz Dziedzic 		   status->band, status->flag,
130878433f96SJanusz Dziedzic 		   !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
130976f5329aSJanusz Dziedzic 		   !!(status->flag & RX_FLAG_MMIC_ERROR),
131076f5329aSJanusz Dziedzic 		   !!(status->flag & RX_FLAG_AMSDU_MORE));
13117aa7a72aSMichal Kazior 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
131285f6d7cfSJanusz Dziedzic 			skb->data, skb->len);
13135ce8e7fdSRajkumar Manoharan 	trace_ath10k_rx_hdr(ar, skb->data, skb->len);
13145ce8e7fdSRajkumar Manoharan 	trace_ath10k_rx_payload(ar, skb->data, skb->len);
131573539b40SJanusz Dziedzic 
13163c97f5deSRajkumar Manoharan 	ieee80211_rx_napi(ar->hw, NULL, skb, &ar->napi);
131773539b40SJanusz Dziedzic }
131873539b40SJanusz Dziedzic 
131948f4ca34SMichal Kazior static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
132048f4ca34SMichal Kazior 				      struct ieee80211_hdr *hdr)
1321d960c369SMichal Kazior {
132248f4ca34SMichal Kazior 	int len = ieee80211_hdrlen(hdr->frame_control);
132348f4ca34SMichal Kazior 
132448f4ca34SMichal Kazior 	if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
1325c4cdf753SKalle Valo 		      ar->running_fw->fw_file.fw_features))
132648f4ca34SMichal Kazior 		len = round_up(len, 4);
132748f4ca34SMichal Kazior 
132848f4ca34SMichal Kazior 	return len;
1329d960c369SMichal Kazior }
1330d960c369SMichal Kazior 
1331581c25f8SMichal Kazior static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
1332581c25f8SMichal Kazior 					struct sk_buff *msdu,
1333581c25f8SMichal Kazior 					struct ieee80211_rx_status *status,
1334581c25f8SMichal Kazior 					enum htt_rx_mpdu_encrypt_type enctype,
1335a2864772SBhagavathi Perumal S 					bool is_decrypted,
1336a2864772SBhagavathi Perumal S 					const u8 first_hdr[64])
13375e3dd157SKalle Valo {
1338f6dc2095SMichal Kazior 	struct ieee80211_hdr *hdr;
1339581c25f8SMichal Kazior 	struct htt_rx_desc *rxd;
1340581c25f8SMichal Kazior 	size_t hdr_len;
1341581c25f8SMichal Kazior 	size_t crypto_len;
1342581c25f8SMichal Kazior 	bool is_first;
1343581c25f8SMichal Kazior 	bool is_last;
1344a2864772SBhagavathi Perumal S 	bool msdu_limit_err;
1345a2864772SBhagavathi Perumal S 	int bytes_aligned = ar->hw_params.decap_align_bytes;
1346a2864772SBhagavathi Perumal S 	u8 *qos;
13475e3dd157SKalle Valo 
1348581c25f8SMichal Kazior 	rxd = (void *)msdu->data - sizeof(*rxd);
13491f5dbfbbSPeter Oh 	is_first = !!(rxd->msdu_end.common.info0 &
1350581c25f8SMichal Kazior 		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
13511f5dbfbbSPeter Oh 	is_last = !!(rxd->msdu_end.common.info0 &
1352581c25f8SMichal Kazior 		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
13539aa505d2SMichal Kazior 
1354581c25f8SMichal Kazior 	/* Delivered decapped frame:
1355581c25f8SMichal Kazior 	 * [802.11 header]
1356581c25f8SMichal Kazior 	 * [crypto param] <-- can be trimmed if !fcs_err &&
1357581c25f8SMichal Kazior 	 *                    !decrypt_err && !peer_idx_invalid
1358581c25f8SMichal Kazior 	 * [amsdu header] <-- only if A-MSDU
1359581c25f8SMichal Kazior 	 * [rfc1042/llc]
1360581c25f8SMichal Kazior 	 * [payload]
1361581c25f8SMichal Kazior 	 * [FCS] <-- at end, needs to be trimmed
1362581c25f8SMichal Kazior 	 */
13635e3dd157SKalle Valo 
1364a2864772SBhagavathi Perumal S 	/* Some hardwares(QCA99x0 variants) limit number of msdus in a-msdu when
1365a2864772SBhagavathi Perumal S 	 * deaggregate, so that unwanted MSDU-deaggregation is avoided for
1366a2864772SBhagavathi Perumal S 	 * error packets. If limit exceeds, hw sends all remaining MSDUs as
1367a2864772SBhagavathi Perumal S 	 * a single last MSDU with this msdu limit error set.
1368a2864772SBhagavathi Perumal S 	 */
1369a2864772SBhagavathi Perumal S 	msdu_limit_err = ath10k_rx_desc_msdu_limit_error(&ar->hw_params, rxd);
1370a2864772SBhagavathi Perumal S 
1371a2864772SBhagavathi Perumal S 	/* If MSDU limit error happens, then don't warn on, the partial raw MSDU
1372a2864772SBhagavathi Perumal S 	 * without first MSDU is expected in that case, and handled later here.
1373a2864772SBhagavathi Perumal S 	 */
1374581c25f8SMichal Kazior 	/* This probably shouldn't happen but warn just in case */
1375a2864772SBhagavathi Perumal S 	if (WARN_ON_ONCE(!is_first && !msdu_limit_err))
1376581c25f8SMichal Kazior 		return;
1377581c25f8SMichal Kazior 
1378581c25f8SMichal Kazior 	/* This probably shouldn't happen but warn just in case */
1379a2864772SBhagavathi Perumal S 	if (WARN_ON_ONCE(!(is_first && is_last) && !msdu_limit_err))
1380581c25f8SMichal Kazior 		return;
1381581c25f8SMichal Kazior 
1382581c25f8SMichal Kazior 	skb_trim(msdu, msdu->len - FCS_LEN);
1383581c25f8SMichal Kazior 
1384a2864772SBhagavathi Perumal S 	/* Push original 80211 header */
1385a2864772SBhagavathi Perumal S 	if (unlikely(msdu_limit_err)) {
1386a2864772SBhagavathi Perumal S 		hdr = (struct ieee80211_hdr *)first_hdr;
1387a2864772SBhagavathi Perumal S 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1388a2864772SBhagavathi Perumal S 		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1389a2864772SBhagavathi Perumal S 
1390a2864772SBhagavathi Perumal S 		if (ieee80211_is_data_qos(hdr->frame_control)) {
1391a2864772SBhagavathi Perumal S 			qos = ieee80211_get_qos_ctl(hdr);
1392a2864772SBhagavathi Perumal S 			qos[0] |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1393a2864772SBhagavathi Perumal S 		}
1394a2864772SBhagavathi Perumal S 
1395a2864772SBhagavathi Perumal S 		if (crypto_len)
1396a2864772SBhagavathi Perumal S 			memcpy(skb_push(msdu, crypto_len),
1397a2864772SBhagavathi Perumal S 			       (void *)hdr + round_up(hdr_len, bytes_aligned),
1398a2864772SBhagavathi Perumal S 			       crypto_len);
1399a2864772SBhagavathi Perumal S 
1400a2864772SBhagavathi Perumal S 		memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1401a2864772SBhagavathi Perumal S 	}
1402a2864772SBhagavathi Perumal S 
1403581c25f8SMichal Kazior 	/* In most cases this will be true for sniffed frames. It makes sense
1404ccec9038SDavid Liu 	 * to deliver them as-is without stripping the crypto param. This is
1405ccec9038SDavid Liu 	 * necessary for software based decryption.
1406581c25f8SMichal Kazior 	 *
1407581c25f8SMichal Kazior 	 * If there's no error then the frame is decrypted. At least that is
1408581c25f8SMichal Kazior 	 * the case for frames that come in via fragmented rx indication.
1409581c25f8SMichal Kazior 	 */
1410581c25f8SMichal Kazior 	if (!is_decrypted)
1411581c25f8SMichal Kazior 		return;
1412581c25f8SMichal Kazior 
1413581c25f8SMichal Kazior 	/* The payload is decrypted so strip crypto params. Start from tail
1414581c25f8SMichal Kazior 	 * since hdr is used to compute some stuff.
1415581c25f8SMichal Kazior 	 */
1416581c25f8SMichal Kazior 
1417581c25f8SMichal Kazior 	hdr = (void *)msdu->data;
1418581c25f8SMichal Kazior 
1419581c25f8SMichal Kazior 	/* Tail */
14207eccb738SVasanthakumar Thiagarajan 	if (status->flag & RX_FLAG_IV_STRIPPED) {
142160549cabSGrzegorz Bajorski 		skb_trim(msdu, msdu->len -
1422307aeb31SVasanthakumar Thiagarajan 			 ath10k_htt_rx_crypto_mic_len(ar, enctype));
1423307aeb31SVasanthakumar Thiagarajan 
1424307aeb31SVasanthakumar Thiagarajan 		skb_trim(msdu, msdu->len -
1425307aeb31SVasanthakumar Thiagarajan 			 ath10k_htt_rx_crypto_icv_len(ar, enctype));
14267eccb738SVasanthakumar Thiagarajan 	} else {
14277eccb738SVasanthakumar Thiagarajan 		/* MIC */
1428307aeb31SVasanthakumar Thiagarajan 		if (status->flag & RX_FLAG_MIC_STRIPPED)
1429307aeb31SVasanthakumar Thiagarajan 			skb_trim(msdu, msdu->len -
1430307aeb31SVasanthakumar Thiagarajan 				 ath10k_htt_rx_crypto_mic_len(ar, enctype));
14317eccb738SVasanthakumar Thiagarajan 
14327eccb738SVasanthakumar Thiagarajan 		/* ICV */
1433307aeb31SVasanthakumar Thiagarajan 		if (status->flag & RX_FLAG_ICV_STRIPPED)
14347eccb738SVasanthakumar Thiagarajan 			skb_trim(msdu, msdu->len -
1435307aeb31SVasanthakumar Thiagarajan 				 ath10k_htt_rx_crypto_icv_len(ar, enctype));
14367eccb738SVasanthakumar Thiagarajan 	}
1437581c25f8SMichal Kazior 
1438581c25f8SMichal Kazior 	/* MMIC */
143960549cabSGrzegorz Bajorski 	if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
144060549cabSGrzegorz Bajorski 	    !ieee80211_has_morefrags(hdr->frame_control) &&
1441581c25f8SMichal Kazior 	    enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
1442307aeb31SVasanthakumar Thiagarajan 		skb_trim(msdu, msdu->len - MICHAEL_MIC_LEN);
1443581c25f8SMichal Kazior 
1444581c25f8SMichal Kazior 	/* Head */
144560549cabSGrzegorz Bajorski 	if (status->flag & RX_FLAG_IV_STRIPPED) {
1446f6dc2095SMichal Kazior 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1447581c25f8SMichal Kazior 		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
14485e3dd157SKalle Valo 
1449581c25f8SMichal Kazior 		memmove((void *)msdu->data + crypto_len,
1450581c25f8SMichal Kazior 			(void *)msdu->data, hdr_len);
1451581c25f8SMichal Kazior 		skb_pull(msdu, crypto_len);
14525e3dd157SKalle Valo 	}
145360549cabSGrzegorz Bajorski }
14545e3dd157SKalle Valo 
1455581c25f8SMichal Kazior static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
1456581c25f8SMichal Kazior 					  struct sk_buff *msdu,
1457581c25f8SMichal Kazior 					  struct ieee80211_rx_status *status,
14587eccb738SVasanthakumar Thiagarajan 					  const u8 first_hdr[64],
14597eccb738SVasanthakumar Thiagarajan 					  enum htt_rx_mpdu_encrypt_type enctype)
1460581c25f8SMichal Kazior {
1461581c25f8SMichal Kazior 	struct ieee80211_hdr *hdr;
14629e19e132SVasanthakumar Thiagarajan 	struct htt_rx_desc *rxd;
1463581c25f8SMichal Kazior 	size_t hdr_len;
1464581c25f8SMichal Kazior 	u8 da[ETH_ALEN];
1465581c25f8SMichal Kazior 	u8 sa[ETH_ALEN];
14669e19e132SVasanthakumar Thiagarajan 	int l3_pad_bytes;
14677eccb738SVasanthakumar Thiagarajan 	int bytes_aligned = ar->hw_params.decap_align_bytes;
1468581c25f8SMichal Kazior 
1469581c25f8SMichal Kazior 	/* Delivered decapped frame:
1470581c25f8SMichal Kazior 	 * [nwifi 802.11 header] <-- replaced with 802.11 hdr
1471581c25f8SMichal Kazior 	 * [rfc1042/llc]
1472581c25f8SMichal Kazior 	 *
1473581c25f8SMichal Kazior 	 * Note: The nwifi header doesn't have QoS Control and is
1474581c25f8SMichal Kazior 	 * (always?) a 3addr frame.
1475581c25f8SMichal Kazior 	 *
1476581c25f8SMichal Kazior 	 * Note2: There's no A-MSDU subframe header. Even if it's part
1477581c25f8SMichal Kazior 	 * of an A-MSDU.
1478581c25f8SMichal Kazior 	 */
1479581c25f8SMichal Kazior 
148072bdeb86SMichal Kazior 	/* pull decapped header and copy SA & DA */
14819e19e132SVasanthakumar Thiagarajan 	rxd = (void *)msdu->data - sizeof(*rxd);
14829e19e132SVasanthakumar Thiagarajan 
14839e19e132SVasanthakumar Thiagarajan 	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
14849e19e132SVasanthakumar Thiagarajan 	skb_put(msdu, l3_pad_bytes);
14859e19e132SVasanthakumar Thiagarajan 
14869e19e132SVasanthakumar Thiagarajan 	hdr = (struct ieee80211_hdr *)(msdu->data + l3_pad_bytes);
1487b8d55fcaSYanbo Li 
148848f4ca34SMichal Kazior 	hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
1489b25f32cbSKalle Valo 	ether_addr_copy(da, ieee80211_get_DA(hdr));
1490b25f32cbSKalle Valo 	ether_addr_copy(sa, ieee80211_get_SA(hdr));
1491581c25f8SMichal Kazior 	skb_pull(msdu, hdr_len);
1492784f69d3SMichal Kazior 
1493784f69d3SMichal Kazior 	/* push original 802.11 header */
1494581c25f8SMichal Kazior 	hdr = (struct ieee80211_hdr *)first_hdr;
1495784f69d3SMichal Kazior 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
14967eccb738SVasanthakumar Thiagarajan 
14977eccb738SVasanthakumar Thiagarajan 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
14987eccb738SVasanthakumar Thiagarajan 		memcpy(skb_push(msdu,
14997eccb738SVasanthakumar Thiagarajan 				ath10k_htt_rx_crypto_param_len(ar, enctype)),
15007eccb738SVasanthakumar Thiagarajan 		       (void *)hdr + round_up(hdr_len, bytes_aligned),
15017eccb738SVasanthakumar Thiagarajan 			ath10k_htt_rx_crypto_param_len(ar, enctype));
15027eccb738SVasanthakumar Thiagarajan 	}
15037eccb738SVasanthakumar Thiagarajan 
1504581c25f8SMichal Kazior 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1505784f69d3SMichal Kazior 
150672bdeb86SMichal Kazior 	/* original 802.11 header has a different DA and in
150772bdeb86SMichal Kazior 	 * case of 4addr it may also have different SA
150872bdeb86SMichal Kazior 	 */
1509581c25f8SMichal Kazior 	hdr = (struct ieee80211_hdr *)msdu->data;
1510b25f32cbSKalle Valo 	ether_addr_copy(ieee80211_get_DA(hdr), da);
1511b25f32cbSKalle Valo 	ether_addr_copy(ieee80211_get_SA(hdr), sa);
15125e3dd157SKalle Valo }
15135e3dd157SKalle Valo 
1514581c25f8SMichal Kazior static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
1515581c25f8SMichal Kazior 					  struct sk_buff *msdu,
1516581c25f8SMichal Kazior 					  enum htt_rx_mpdu_encrypt_type enctype)
15175e3dd157SKalle Valo {
15185e3dd157SKalle Valo 	struct ieee80211_hdr *hdr;
1519581c25f8SMichal Kazior 	struct htt_rx_desc *rxd;
1520581c25f8SMichal Kazior 	size_t hdr_len, crypto_len;
1521e3fbf8d2SMichal Kazior 	void *rfc1042;
1522581c25f8SMichal Kazior 	bool is_first, is_last, is_amsdu;
15232f38c3c0SVasanthakumar Thiagarajan 	int bytes_aligned = ar->hw_params.decap_align_bytes;
15245e3dd157SKalle Valo 
1525581c25f8SMichal Kazior 	rxd = (void *)msdu->data - sizeof(*rxd);
1526581c25f8SMichal Kazior 	hdr = (void *)rxd->rx_hdr_status;
15275e3dd157SKalle Valo 
15281f5dbfbbSPeter Oh 	is_first = !!(rxd->msdu_end.common.info0 &
1529581c25f8SMichal Kazior 		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
15301f5dbfbbSPeter Oh 	is_last = !!(rxd->msdu_end.common.info0 &
1531581c25f8SMichal Kazior 		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
1532581c25f8SMichal Kazior 	is_amsdu = !(is_first && is_last);
1533e3fbf8d2SMichal Kazior 
1534e3fbf8d2SMichal Kazior 	rfc1042 = hdr;
1535e3fbf8d2SMichal Kazior 
1536581c25f8SMichal Kazior 	if (is_first) {
1537581c25f8SMichal Kazior 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1538581c25f8SMichal Kazior 		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1539e3fbf8d2SMichal Kazior 
15402f38c3c0SVasanthakumar Thiagarajan 		rfc1042 += round_up(hdr_len, bytes_aligned) +
15412f38c3c0SVasanthakumar Thiagarajan 			   round_up(crypto_len, bytes_aligned);
15425e3dd157SKalle Valo 	}
15435e3dd157SKalle Valo 
1544581c25f8SMichal Kazior 	if (is_amsdu)
1545581c25f8SMichal Kazior 		rfc1042 += sizeof(struct amsdu_subframe_hdr);
1546f6dc2095SMichal Kazior 
1547581c25f8SMichal Kazior 	return rfc1042;
1548581c25f8SMichal Kazior }
1549581c25f8SMichal Kazior 
1550581c25f8SMichal Kazior static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
1551581c25f8SMichal Kazior 					struct sk_buff *msdu,
1552581c25f8SMichal Kazior 					struct ieee80211_rx_status *status,
1553581c25f8SMichal Kazior 					const u8 first_hdr[64],
1554581c25f8SMichal Kazior 					enum htt_rx_mpdu_encrypt_type enctype)
1555581c25f8SMichal Kazior {
1556581c25f8SMichal Kazior 	struct ieee80211_hdr *hdr;
1557581c25f8SMichal Kazior 	struct ethhdr *eth;
1558581c25f8SMichal Kazior 	size_t hdr_len;
1559581c25f8SMichal Kazior 	void *rfc1042;
1560581c25f8SMichal Kazior 	u8 da[ETH_ALEN];
1561581c25f8SMichal Kazior 	u8 sa[ETH_ALEN];
15629e19e132SVasanthakumar Thiagarajan 	int l3_pad_bytes;
15639e19e132SVasanthakumar Thiagarajan 	struct htt_rx_desc *rxd;
15647eccb738SVasanthakumar Thiagarajan 	int bytes_aligned = ar->hw_params.decap_align_bytes;
1565581c25f8SMichal Kazior 
1566581c25f8SMichal Kazior 	/* Delivered decapped frame:
1567581c25f8SMichal Kazior 	 * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
1568581c25f8SMichal Kazior 	 * [payload]
1569581c25f8SMichal Kazior 	 */
1570581c25f8SMichal Kazior 
1571581c25f8SMichal Kazior 	rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
1572581c25f8SMichal Kazior 	if (WARN_ON_ONCE(!rfc1042))
1573581c25f8SMichal Kazior 		return;
1574581c25f8SMichal Kazior 
15759e19e132SVasanthakumar Thiagarajan 	rxd = (void *)msdu->data - sizeof(*rxd);
15769e19e132SVasanthakumar Thiagarajan 	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
15779e19e132SVasanthakumar Thiagarajan 	skb_put(msdu, l3_pad_bytes);
15789e19e132SVasanthakumar Thiagarajan 	skb_pull(msdu, l3_pad_bytes);
15799e19e132SVasanthakumar Thiagarajan 
1580581c25f8SMichal Kazior 	/* pull decapped header and copy SA & DA */
1581581c25f8SMichal Kazior 	eth = (struct ethhdr *)msdu->data;
1582581c25f8SMichal Kazior 	ether_addr_copy(da, eth->h_dest);
1583581c25f8SMichal Kazior 	ether_addr_copy(sa, eth->h_source);
1584581c25f8SMichal Kazior 	skb_pull(msdu, sizeof(struct ethhdr));
1585581c25f8SMichal Kazior 
1586581c25f8SMichal Kazior 	/* push rfc1042/llc/snap */
1587581c25f8SMichal Kazior 	memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
1588581c25f8SMichal Kazior 	       sizeof(struct rfc1042_hdr));
1589581c25f8SMichal Kazior 
1590581c25f8SMichal Kazior 	/* push original 802.11 header */
1591581c25f8SMichal Kazior 	hdr = (struct ieee80211_hdr *)first_hdr;
1592581c25f8SMichal Kazior 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
15937eccb738SVasanthakumar Thiagarajan 
15947eccb738SVasanthakumar Thiagarajan 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
15957eccb738SVasanthakumar Thiagarajan 		memcpy(skb_push(msdu,
15967eccb738SVasanthakumar Thiagarajan 				ath10k_htt_rx_crypto_param_len(ar, enctype)),
15977eccb738SVasanthakumar Thiagarajan 		       (void *)hdr + round_up(hdr_len, bytes_aligned),
15987eccb738SVasanthakumar Thiagarajan 			ath10k_htt_rx_crypto_param_len(ar, enctype));
15997eccb738SVasanthakumar Thiagarajan 	}
16007eccb738SVasanthakumar Thiagarajan 
1601581c25f8SMichal Kazior 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1602581c25f8SMichal Kazior 
1603581c25f8SMichal Kazior 	/* original 802.11 header has a different DA and in
1604581c25f8SMichal Kazior 	 * case of 4addr it may also have different SA
1605581c25f8SMichal Kazior 	 */
1606581c25f8SMichal Kazior 	hdr = (struct ieee80211_hdr *)msdu->data;
1607581c25f8SMichal Kazior 	ether_addr_copy(ieee80211_get_DA(hdr), da);
1608581c25f8SMichal Kazior 	ether_addr_copy(ieee80211_get_SA(hdr), sa);
1609581c25f8SMichal Kazior }
1610581c25f8SMichal Kazior 
1611581c25f8SMichal Kazior static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
1612581c25f8SMichal Kazior 					 struct sk_buff *msdu,
1613581c25f8SMichal Kazior 					 struct ieee80211_rx_status *status,
16147eccb738SVasanthakumar Thiagarajan 					 const u8 first_hdr[64],
16157eccb738SVasanthakumar Thiagarajan 					 enum htt_rx_mpdu_encrypt_type enctype)
1616581c25f8SMichal Kazior {
1617581c25f8SMichal Kazior 	struct ieee80211_hdr *hdr;
1618581c25f8SMichal Kazior 	size_t hdr_len;
16199e19e132SVasanthakumar Thiagarajan 	int l3_pad_bytes;
16209e19e132SVasanthakumar Thiagarajan 	struct htt_rx_desc *rxd;
16217eccb738SVasanthakumar Thiagarajan 	int bytes_aligned = ar->hw_params.decap_align_bytes;
1622581c25f8SMichal Kazior 
1623581c25f8SMichal Kazior 	/* Delivered decapped frame:
1624581c25f8SMichal Kazior 	 * [amsdu header] <-- replaced with 802.11 hdr
1625581c25f8SMichal Kazior 	 * [rfc1042/llc]
1626581c25f8SMichal Kazior 	 * [payload]
1627581c25f8SMichal Kazior 	 */
1628581c25f8SMichal Kazior 
16299e19e132SVasanthakumar Thiagarajan 	rxd = (void *)msdu->data - sizeof(*rxd);
16309e19e132SVasanthakumar Thiagarajan 	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
16319e19e132SVasanthakumar Thiagarajan 
16329e19e132SVasanthakumar Thiagarajan 	skb_put(msdu, l3_pad_bytes);
16339e19e132SVasanthakumar Thiagarajan 	skb_pull(msdu, sizeof(struct amsdu_subframe_hdr) + l3_pad_bytes);
1634581c25f8SMichal Kazior 
1635581c25f8SMichal Kazior 	hdr = (struct ieee80211_hdr *)first_hdr;
1636581c25f8SMichal Kazior 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
16377eccb738SVasanthakumar Thiagarajan 
16387eccb738SVasanthakumar Thiagarajan 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
16397eccb738SVasanthakumar Thiagarajan 		memcpy(skb_push(msdu,
16407eccb738SVasanthakumar Thiagarajan 				ath10k_htt_rx_crypto_param_len(ar, enctype)),
16417eccb738SVasanthakumar Thiagarajan 		       (void *)hdr + round_up(hdr_len, bytes_aligned),
16427eccb738SVasanthakumar Thiagarajan 			ath10k_htt_rx_crypto_param_len(ar, enctype));
16437eccb738SVasanthakumar Thiagarajan 	}
16447eccb738SVasanthakumar Thiagarajan 
1645581c25f8SMichal Kazior 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1646581c25f8SMichal Kazior }
1647581c25f8SMichal Kazior 
1648581c25f8SMichal Kazior static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
1649581c25f8SMichal Kazior 				    struct sk_buff *msdu,
1650581c25f8SMichal Kazior 				    struct ieee80211_rx_status *status,
1651581c25f8SMichal Kazior 				    u8 first_hdr[64],
1652581c25f8SMichal Kazior 				    enum htt_rx_mpdu_encrypt_type enctype,
1653581c25f8SMichal Kazior 				    bool is_decrypted)
1654581c25f8SMichal Kazior {
1655581c25f8SMichal Kazior 	struct htt_rx_desc *rxd;
1656581c25f8SMichal Kazior 	enum rx_msdu_decap_format decap;
1657581c25f8SMichal Kazior 
1658581c25f8SMichal Kazior 	/* First msdu's decapped header:
1659581c25f8SMichal Kazior 	 * [802.11 header] <-- padded to 4 bytes long
1660581c25f8SMichal Kazior 	 * [crypto param] <-- padded to 4 bytes long
1661581c25f8SMichal Kazior 	 * [amsdu header] <-- only if A-MSDU
1662581c25f8SMichal Kazior 	 * [rfc1042/llc]
1663581c25f8SMichal Kazior 	 *
1664581c25f8SMichal Kazior 	 * Other (2nd, 3rd, ..) msdu's decapped header:
1665581c25f8SMichal Kazior 	 * [amsdu header] <-- only if A-MSDU
1666581c25f8SMichal Kazior 	 * [rfc1042/llc]
1667581c25f8SMichal Kazior 	 */
1668581c25f8SMichal Kazior 
1669581c25f8SMichal Kazior 	rxd = (void *)msdu->data - sizeof(*rxd);
16701f5dbfbbSPeter Oh 	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
1671581c25f8SMichal Kazior 		   RX_MSDU_START_INFO1_DECAP_FORMAT);
1672581c25f8SMichal Kazior 
1673581c25f8SMichal Kazior 	switch (decap) {
1674581c25f8SMichal Kazior 	case RX_MSDU_DECAP_RAW:
1675581c25f8SMichal Kazior 		ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
1676a2864772SBhagavathi Perumal S 					    is_decrypted, first_hdr);
1677581c25f8SMichal Kazior 		break;
1678581c25f8SMichal Kazior 	case RX_MSDU_DECAP_NATIVE_WIFI:
16797eccb738SVasanthakumar Thiagarajan 		ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr,
16807eccb738SVasanthakumar Thiagarajan 					      enctype);
1681581c25f8SMichal Kazior 		break;
1682581c25f8SMichal Kazior 	case RX_MSDU_DECAP_ETHERNET2_DIX:
1683581c25f8SMichal Kazior 		ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
1684581c25f8SMichal Kazior 		break;
1685581c25f8SMichal Kazior 	case RX_MSDU_DECAP_8023_SNAP_LLC:
16867eccb738SVasanthakumar Thiagarajan 		ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr,
16877eccb738SVasanthakumar Thiagarajan 					     enctype);
1688581c25f8SMichal Kazior 		break;
1689581c25f8SMichal Kazior 	}
16905e3dd157SKalle Valo }
16915e3dd157SKalle Valo 
1692605f81aaSMichal Kazior static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
1693605f81aaSMichal Kazior {
1694605f81aaSMichal Kazior 	struct htt_rx_desc *rxd;
1695605f81aaSMichal Kazior 	u32 flags, info;
1696605f81aaSMichal Kazior 	bool is_ip4, is_ip6;
1697605f81aaSMichal Kazior 	bool is_tcp, is_udp;
1698605f81aaSMichal Kazior 	bool ip_csum_ok, tcpudp_csum_ok;
1699605f81aaSMichal Kazior 
1700605f81aaSMichal Kazior 	rxd = (void *)skb->data - sizeof(*rxd);
1701605f81aaSMichal Kazior 	flags = __le32_to_cpu(rxd->attention.flags);
17021f5dbfbbSPeter Oh 	info = __le32_to_cpu(rxd->msdu_start.common.info1);
1703605f81aaSMichal Kazior 
1704605f81aaSMichal Kazior 	is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
1705605f81aaSMichal Kazior 	is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
1706605f81aaSMichal Kazior 	is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
1707605f81aaSMichal Kazior 	is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
1708605f81aaSMichal Kazior 	ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
1709605f81aaSMichal Kazior 	tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
1710605f81aaSMichal Kazior 
1711605f81aaSMichal Kazior 	if (!is_ip4 && !is_ip6)
1712605f81aaSMichal Kazior 		return CHECKSUM_NONE;
1713605f81aaSMichal Kazior 	if (!is_tcp && !is_udp)
1714605f81aaSMichal Kazior 		return CHECKSUM_NONE;
1715605f81aaSMichal Kazior 	if (!ip_csum_ok)
1716605f81aaSMichal Kazior 		return CHECKSUM_NONE;
1717605f81aaSMichal Kazior 	if (!tcpudp_csum_ok)
1718605f81aaSMichal Kazior 		return CHECKSUM_NONE;
1719605f81aaSMichal Kazior 
1720605f81aaSMichal Kazior 	return CHECKSUM_UNNECESSARY;
1721605f81aaSMichal Kazior }
1722605f81aaSMichal Kazior 
1723581c25f8SMichal Kazior static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
1724581c25f8SMichal Kazior {
1725581c25f8SMichal Kazior 	msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
1726581c25f8SMichal Kazior }
1727581c25f8SMichal Kazior 
1728581c25f8SMichal Kazior static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
1729581c25f8SMichal Kazior 				 struct sk_buff_head *amsdu,
17307eccb738SVasanthakumar Thiagarajan 				 struct ieee80211_rx_status *status,
1731caee728aSVasanthakumar Thiagarajan 				 bool fill_crypt_header,
1732caee728aSVasanthakumar Thiagarajan 				 u8 *rx_hdr,
1733caee728aSVasanthakumar Thiagarajan 				 enum ath10k_pkt_rx_err *err)
1734581c25f8SMichal Kazior {
1735581c25f8SMichal Kazior 	struct sk_buff *first;
1736581c25f8SMichal Kazior 	struct sk_buff *last;
1737581c25f8SMichal Kazior 	struct sk_buff *msdu;
1738581c25f8SMichal Kazior 	struct htt_rx_desc *rxd;
1739581c25f8SMichal Kazior 	struct ieee80211_hdr *hdr;
1740581c25f8SMichal Kazior 	enum htt_rx_mpdu_encrypt_type enctype;
1741581c25f8SMichal Kazior 	u8 first_hdr[64];
1742581c25f8SMichal Kazior 	u8 *qos;
1743581c25f8SMichal Kazior 	bool has_fcs_err;
1744581c25f8SMichal Kazior 	bool has_crypto_err;
1745581c25f8SMichal Kazior 	bool has_tkip_err;
1746581c25f8SMichal Kazior 	bool has_peer_idx_invalid;
1747581c25f8SMichal Kazior 	bool is_decrypted;
174860549cabSGrzegorz Bajorski 	bool is_mgmt;
1749581c25f8SMichal Kazior 	u32 attention;
1750581c25f8SMichal Kazior 
1751581c25f8SMichal Kazior 	if (skb_queue_empty(amsdu))
1752581c25f8SMichal Kazior 		return;
1753581c25f8SMichal Kazior 
1754581c25f8SMichal Kazior 	first = skb_peek(amsdu);
1755581c25f8SMichal Kazior 	rxd = (void *)first->data - sizeof(*rxd);
1756581c25f8SMichal Kazior 
175760549cabSGrzegorz Bajorski 	is_mgmt = !!(rxd->attention.flags &
175860549cabSGrzegorz Bajorski 		     __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));
175960549cabSGrzegorz Bajorski 
1760581c25f8SMichal Kazior 	enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1761581c25f8SMichal Kazior 		     RX_MPDU_START_INFO0_ENCRYPT_TYPE);
1762581c25f8SMichal Kazior 
1763581c25f8SMichal Kazior 	/* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
1764581c25f8SMichal Kazior 	 * decapped header. It'll be used for undecapping of each MSDU.
1765581c25f8SMichal Kazior 	 */
1766581c25f8SMichal Kazior 	hdr = (void *)rxd->rx_hdr_status;
17677eccb738SVasanthakumar Thiagarajan 	memcpy(first_hdr, hdr, RX_HTT_HDR_STATUS_LEN);
1768581c25f8SMichal Kazior 
1769caee728aSVasanthakumar Thiagarajan 	if (rx_hdr)
1770caee728aSVasanthakumar Thiagarajan 		memcpy(rx_hdr, hdr, RX_HTT_HDR_STATUS_LEN);
1771caee728aSVasanthakumar Thiagarajan 
1772581c25f8SMichal Kazior 	/* Each A-MSDU subframe will use the original header as the base and be
1773581c25f8SMichal Kazior 	 * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
1774581c25f8SMichal Kazior 	 */
1775581c25f8SMichal Kazior 	hdr = (void *)first_hdr;
17767eccb738SVasanthakumar Thiagarajan 
17777eccb738SVasanthakumar Thiagarajan 	if (ieee80211_is_data_qos(hdr->frame_control)) {
1778581c25f8SMichal Kazior 		qos = ieee80211_get_qos_ctl(hdr);
1779581c25f8SMichal Kazior 		qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
17807eccb738SVasanthakumar Thiagarajan 	}
1781581c25f8SMichal Kazior 
1782581c25f8SMichal Kazior 	/* Some attention flags are valid only in the last MSDU. */
1783581c25f8SMichal Kazior 	last = skb_peek_tail(amsdu);
1784581c25f8SMichal Kazior 	rxd = (void *)last->data - sizeof(*rxd);
1785581c25f8SMichal Kazior 	attention = __le32_to_cpu(rxd->attention.flags);
1786581c25f8SMichal Kazior 
1787581c25f8SMichal Kazior 	has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
1788581c25f8SMichal Kazior 	has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
1789581c25f8SMichal Kazior 	has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
1790581c25f8SMichal Kazior 	has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);
1791581c25f8SMichal Kazior 
1792581c25f8SMichal Kazior 	/* Note: If hardware captures an encrypted frame that it can't decrypt,
1793581c25f8SMichal Kazior 	 * e.g. due to fcs error, missing peer or invalid key data it will
1794581c25f8SMichal Kazior 	 * report the frame as raw.
1795581c25f8SMichal Kazior 	 */
1796581c25f8SMichal Kazior 	is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
1797581c25f8SMichal Kazior 			!has_fcs_err &&
1798581c25f8SMichal Kazior 			!has_crypto_err &&
1799581c25f8SMichal Kazior 			!has_peer_idx_invalid);
1800581c25f8SMichal Kazior 
1801581c25f8SMichal Kazior 	/* Clear per-MPDU flags while leaving per-PPDU flags intact. */
1802581c25f8SMichal Kazior 	status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
1803581c25f8SMichal Kazior 			  RX_FLAG_MMIC_ERROR |
1804581c25f8SMichal Kazior 			  RX_FLAG_DECRYPTED |
1805581c25f8SMichal Kazior 			  RX_FLAG_IV_STRIPPED |
180660549cabSGrzegorz Bajorski 			  RX_FLAG_ONLY_MONITOR |
1807581c25f8SMichal Kazior 			  RX_FLAG_MMIC_STRIPPED);
1808581c25f8SMichal Kazior 
1809581c25f8SMichal Kazior 	if (has_fcs_err)
1810581c25f8SMichal Kazior 		status->flag |= RX_FLAG_FAILED_FCS_CRC;
1811581c25f8SMichal Kazior 
1812581c25f8SMichal Kazior 	if (has_tkip_err)
1813581c25f8SMichal Kazior 		status->flag |= RX_FLAG_MMIC_ERROR;
1814581c25f8SMichal Kazior 
1815caee728aSVasanthakumar Thiagarajan 	if (err) {
1816caee728aSVasanthakumar Thiagarajan 		if (has_fcs_err)
1817caee728aSVasanthakumar Thiagarajan 			*err = ATH10K_PKT_RX_ERR_FCS;
1818caee728aSVasanthakumar Thiagarajan 		else if (has_tkip_err)
1819caee728aSVasanthakumar Thiagarajan 			*err = ATH10K_PKT_RX_ERR_TKIP;
1820caee728aSVasanthakumar Thiagarajan 		else if (has_crypto_err)
1821caee728aSVasanthakumar Thiagarajan 			*err = ATH10K_PKT_RX_ERR_CRYPT;
1822caee728aSVasanthakumar Thiagarajan 		else if (has_peer_idx_invalid)
1823caee728aSVasanthakumar Thiagarajan 			*err = ATH10K_PKT_RX_ERR_PEER_IDX_INVAL;
1824caee728aSVasanthakumar Thiagarajan 	}
1825caee728aSVasanthakumar Thiagarajan 
182660549cabSGrzegorz Bajorski 	/* Firmware reports all necessary management frames via WMI already.
182760549cabSGrzegorz Bajorski 	 * They are not reported to monitor interfaces at all so pass the ones
182860549cabSGrzegorz Bajorski 	 * coming via HTT to monitor interfaces instead. This simplifies
182960549cabSGrzegorz Bajorski 	 * matters a lot.
183060549cabSGrzegorz Bajorski 	 */
183160549cabSGrzegorz Bajorski 	if (is_mgmt)
183260549cabSGrzegorz Bajorski 		status->flag |= RX_FLAG_ONLY_MONITOR;
183360549cabSGrzegorz Bajorski 
183460549cabSGrzegorz Bajorski 	if (is_decrypted) {
183560549cabSGrzegorz Bajorski 		status->flag |= RX_FLAG_DECRYPTED;
183660549cabSGrzegorz Bajorski 
183760549cabSGrzegorz Bajorski 		if (likely(!is_mgmt))
18387eccb738SVasanthakumar Thiagarajan 			status->flag |= RX_FLAG_MMIC_STRIPPED;
18397eccb738SVasanthakumar Thiagarajan 
18407eccb738SVasanthakumar Thiagarajan 		if (fill_crypt_header)
18417eccb738SVasanthakumar Thiagarajan 			status->flag |= RX_FLAG_MIC_STRIPPED |
18427eccb738SVasanthakumar Thiagarajan 					RX_FLAG_ICV_STRIPPED;
18437eccb738SVasanthakumar Thiagarajan 		else
18447eccb738SVasanthakumar Thiagarajan 			status->flag |= RX_FLAG_IV_STRIPPED;
184560549cabSGrzegorz Bajorski 	}
1846581c25f8SMichal Kazior 
1847581c25f8SMichal Kazior 	skb_queue_walk(amsdu, msdu) {
1848581c25f8SMichal Kazior 		ath10k_htt_rx_h_csum_offload(msdu);
1849581c25f8SMichal Kazior 		ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
1850581c25f8SMichal Kazior 					is_decrypted);
1851581c25f8SMichal Kazior 
1852581c25f8SMichal Kazior 		/* Undecapping involves copying the original 802.11 header back
1853581c25f8SMichal Kazior 		 * to sk_buff. If frame is protected and hardware has decrypted
1854581c25f8SMichal Kazior 		 * it then remove the protected bit.
1855581c25f8SMichal Kazior 		 */
1856581c25f8SMichal Kazior 		if (!is_decrypted)
1857581c25f8SMichal Kazior 			continue;
185860549cabSGrzegorz Bajorski 		if (is_mgmt)
185960549cabSGrzegorz Bajorski 			continue;
1860581c25f8SMichal Kazior 
18617eccb738SVasanthakumar Thiagarajan 		if (fill_crypt_header)
18627eccb738SVasanthakumar Thiagarajan 			continue;
18637eccb738SVasanthakumar Thiagarajan 
1864581c25f8SMichal Kazior 		hdr = (void *)msdu->data;
1865581c25f8SMichal Kazior 		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1866581c25f8SMichal Kazior 	}
1867581c25f8SMichal Kazior }
1868581c25f8SMichal Kazior 
1869deba1b9eSRajkumar Manoharan static void ath10k_htt_rx_h_enqueue(struct ath10k *ar,
1870581c25f8SMichal Kazior 				    struct sk_buff_head *amsdu,
1871581c25f8SMichal Kazior 				    struct ieee80211_rx_status *status)
1872581c25f8SMichal Kazior {
1873581c25f8SMichal Kazior 	struct sk_buff *msdu;
18747eccb738SVasanthakumar Thiagarajan 	struct sk_buff *first_subframe;
18757eccb738SVasanthakumar Thiagarajan 
18767eccb738SVasanthakumar Thiagarajan 	first_subframe = skb_peek(amsdu);
1877581c25f8SMichal Kazior 
1878581c25f8SMichal Kazior 	while ((msdu = __skb_dequeue(amsdu))) {
1879581c25f8SMichal Kazior 		/* Setup per-MSDU flags */
1880581c25f8SMichal Kazior 		if (skb_queue_empty(amsdu))
1881581c25f8SMichal Kazior 			status->flag &= ~RX_FLAG_AMSDU_MORE;
1882581c25f8SMichal Kazior 		else
1883581c25f8SMichal Kazior 			status->flag |= RX_FLAG_AMSDU_MORE;
1884581c25f8SMichal Kazior 
18857eccb738SVasanthakumar Thiagarajan 		if (msdu == first_subframe) {
18867eccb738SVasanthakumar Thiagarajan 			first_subframe = NULL;
18877eccb738SVasanthakumar Thiagarajan 			status->flag &= ~RX_FLAG_ALLOW_SAME_PN;
18887eccb738SVasanthakumar Thiagarajan 		} else {
18897eccb738SVasanthakumar Thiagarajan 			status->flag |= RX_FLAG_ALLOW_SAME_PN;
18907eccb738SVasanthakumar Thiagarajan 		}
18917eccb738SVasanthakumar Thiagarajan 
1892deba1b9eSRajkumar Manoharan 		ath10k_htt_rx_h_queue_msdu(ar, status, msdu);
1893581c25f8SMichal Kazior 	}
1894581c25f8SMichal Kazior }
1895581c25f8SMichal Kazior 
1896caee728aSVasanthakumar Thiagarajan static int ath10k_unchain_msdu(struct sk_buff_head *amsdu,
189728bbe237SKalle Valo 			       unsigned long *unchain_cnt)
1898bfa35368SBen Greear {
18999aa505d2SMichal Kazior 	struct sk_buff *skb, *first;
1900bfa35368SBen Greear 	int space;
1901bfa35368SBen Greear 	int total_len = 0;
1902caee728aSVasanthakumar Thiagarajan 	int amsdu_len = skb_queue_len(amsdu);
1903bfa35368SBen Greear 
1904bfa35368SBen Greear 	/* TODO:  Might could optimize this by using
1905bfa35368SBen Greear 	 * skb_try_coalesce or similar method to
1906bfa35368SBen Greear 	 * decrease copying, or maybe get mac80211 to
1907bfa35368SBen Greear 	 * provide a way to just receive a list of
1908bfa35368SBen Greear 	 * skb?
1909bfa35368SBen Greear 	 */
1910bfa35368SBen Greear 
19119aa505d2SMichal Kazior 	first = __skb_dequeue(amsdu);
1912bfa35368SBen Greear 
1913bfa35368SBen Greear 	/* Allocate total length all at once. */
19149aa505d2SMichal Kazior 	skb_queue_walk(amsdu, skb)
19159aa505d2SMichal Kazior 		total_len += skb->len;
1916bfa35368SBen Greear 
19179aa505d2SMichal Kazior 	space = total_len - skb_tailroom(first);
1918bfa35368SBen Greear 	if ((space > 0) &&
19199aa505d2SMichal Kazior 	    (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
1920bfa35368SBen Greear 		/* TODO:  bump some rx-oom error stat */
1921bfa35368SBen Greear 		/* put it back together so we can free the
1922bfa35368SBen Greear 		 * whole list at once.
1923bfa35368SBen Greear 		 */
19249aa505d2SMichal Kazior 		__skb_queue_head(amsdu, first);
1925bfa35368SBen Greear 		return -1;
1926bfa35368SBen Greear 	}
1927bfa35368SBen Greear 
1928bfa35368SBen Greear 	/* Walk list again, copying contents into
1929bfa35368SBen Greear 	 * msdu_head
1930bfa35368SBen Greear 	 */
19319aa505d2SMichal Kazior 	while ((skb = __skb_dequeue(amsdu))) {
19329aa505d2SMichal Kazior 		skb_copy_from_linear_data(skb, skb_put(first, skb->len),
19339aa505d2SMichal Kazior 					  skb->len);
19349aa505d2SMichal Kazior 		dev_kfree_skb_any(skb);
1935bfa35368SBen Greear 	}
1936bfa35368SBen Greear 
19379aa505d2SMichal Kazior 	__skb_queue_head(amsdu, first);
1938caee728aSVasanthakumar Thiagarajan 
1939caee728aSVasanthakumar Thiagarajan 	*unchain_cnt += amsdu_len - 1;
1940caee728aSVasanthakumar Thiagarajan 
1941bfa35368SBen Greear 	return 0;
1942bfa35368SBen Greear }
1943bfa35368SBen Greear 
1944581c25f8SMichal Kazior static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
1945caee728aSVasanthakumar Thiagarajan 				    struct sk_buff_head *amsdu,
194628bbe237SKalle Valo 				    unsigned long *drop_cnt,
194728bbe237SKalle Valo 				    unsigned long *unchain_cnt)
19482acc4eb2SJanusz Dziedzic {
1949581c25f8SMichal Kazior 	struct sk_buff *first;
1950581c25f8SMichal Kazior 	struct htt_rx_desc *rxd;
1951581c25f8SMichal Kazior 	enum rx_msdu_decap_format decap;
19527aa7a72aSMichal Kazior 
1953581c25f8SMichal Kazior 	first = skb_peek(amsdu);
1954581c25f8SMichal Kazior 	rxd = (void *)first->data - sizeof(*rxd);
19551f5dbfbbSPeter Oh 	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
1956581c25f8SMichal Kazior 		   RX_MSDU_START_INFO1_DECAP_FORMAT);
1957581c25f8SMichal Kazior 
1958581c25f8SMichal Kazior 	/* FIXME: Current unchaining logic can only handle simple case of raw
1959581c25f8SMichal Kazior 	 * msdu chaining. If decapping is other than raw the chaining may be
1960581c25f8SMichal Kazior 	 * more complex and this isn't handled by the current code. Don't even
1961581c25f8SMichal Kazior 	 * try re-constructing such frames - it'll be pretty much garbage.
1962581c25f8SMichal Kazior 	 */
1963581c25f8SMichal Kazior 	if (decap != RX_MSDU_DECAP_RAW ||
1964581c25f8SMichal Kazior 	    skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
1965caee728aSVasanthakumar Thiagarajan 		*drop_cnt += skb_queue_len(amsdu);
1966581c25f8SMichal Kazior 		__skb_queue_purge(amsdu);
1967581c25f8SMichal Kazior 		return;
1968581c25f8SMichal Kazior 	}
1969581c25f8SMichal Kazior 
1970caee728aSVasanthakumar Thiagarajan 	ath10k_unchain_msdu(amsdu, unchain_cnt);
1971581c25f8SMichal Kazior }
1972581c25f8SMichal Kazior 
1973581c25f8SMichal Kazior static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
1974581c25f8SMichal Kazior 					struct sk_buff_head *amsdu,
1975581c25f8SMichal Kazior 					struct ieee80211_rx_status *rx_status)
1976581c25f8SMichal Kazior {
1977581c25f8SMichal Kazior 	/* FIXME: It might be a good idea to do some fuzzy-testing to drop
1978581c25f8SMichal Kazior 	 * invalid/dangerous frames.
1979581c25f8SMichal Kazior 	 */
1980581c25f8SMichal Kazior 
1981581c25f8SMichal Kazior 	if (!rx_status->freq) {
1982984eb905SGabriel Craciunescu 		ath10k_dbg(ar, ATH10K_DBG_HTT, "no channel configured; ignoring frame(s)!\n");
19832acc4eb2SJanusz Dziedzic 		return false;
19842acc4eb2SJanusz Dziedzic 	}
19852acc4eb2SJanusz Dziedzic 
1986581c25f8SMichal Kazior 	if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
1987581c25f8SMichal Kazior 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
19882acc4eb2SJanusz Dziedzic 		return false;
19892acc4eb2SJanusz Dziedzic 	}
19902acc4eb2SJanusz Dziedzic 
19912acc4eb2SJanusz Dziedzic 	return true;
19922acc4eb2SJanusz Dziedzic }
19932acc4eb2SJanusz Dziedzic 
1994581c25f8SMichal Kazior static void ath10k_htt_rx_h_filter(struct ath10k *ar,
1995581c25f8SMichal Kazior 				   struct sk_buff_head *amsdu,
1996caee728aSVasanthakumar Thiagarajan 				   struct ieee80211_rx_status *rx_status,
199728bbe237SKalle Valo 				   unsigned long *drop_cnt)
1998581c25f8SMichal Kazior {
1999581c25f8SMichal Kazior 	if (skb_queue_empty(amsdu))
2000581c25f8SMichal Kazior 		return;
2001581c25f8SMichal Kazior 
2002581c25f8SMichal Kazior 	if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
2003581c25f8SMichal Kazior 		return;
2004581c25f8SMichal Kazior 
2005caee728aSVasanthakumar Thiagarajan 	if (drop_cnt)
2006caee728aSVasanthakumar Thiagarajan 		*drop_cnt += skb_queue_len(amsdu);
2007caee728aSVasanthakumar Thiagarajan 
2008581c25f8SMichal Kazior 	__skb_queue_purge(amsdu);
2009581c25f8SMichal Kazior }
2010581c25f8SMichal Kazior 
201118235664SRajkumar Manoharan static int ath10k_htt_rx_handle_amsdu(struct ath10k_htt *htt)
201218235664SRajkumar Manoharan {
201318235664SRajkumar Manoharan 	struct ath10k *ar = htt->ar;
2014237e15dfSAshok Raj Nagarajan 	struct ieee80211_rx_status *rx_status = &htt->rx_status;
201518235664SRajkumar Manoharan 	struct sk_buff_head amsdu;
2016deba1b9eSRajkumar Manoharan 	int ret;
201728bbe237SKalle Valo 	unsigned long drop_cnt = 0;
201828bbe237SKalle Valo 	unsigned long unchain_cnt = 0;
201928bbe237SKalle Valo 	unsigned long drop_cnt_filter = 0;
202028bbe237SKalle Valo 	unsigned long msdus_to_queue, num_msdus;
2021caee728aSVasanthakumar Thiagarajan 	enum ath10k_pkt_rx_err err = ATH10K_PKT_RX_ERR_MAX;
2022caee728aSVasanthakumar Thiagarajan 	u8 first_hdr[RX_HTT_HDR_STATUS_LEN];
202318235664SRajkumar Manoharan 
202418235664SRajkumar Manoharan 	__skb_queue_head_init(&amsdu);
202518235664SRajkumar Manoharan 
202618235664SRajkumar Manoharan 	spin_lock_bh(&htt->rx_ring.lock);
202718235664SRajkumar Manoharan 	if (htt->rx_confused) {
202818235664SRajkumar Manoharan 		spin_unlock_bh(&htt->rx_ring.lock);
202918235664SRajkumar Manoharan 		return -EIO;
203018235664SRajkumar Manoharan 	}
203118235664SRajkumar Manoharan 	ret = ath10k_htt_rx_amsdu_pop(htt, &amsdu);
203218235664SRajkumar Manoharan 	spin_unlock_bh(&htt->rx_ring.lock);
203318235664SRajkumar Manoharan 
203418235664SRajkumar Manoharan 	if (ret < 0) {
203518235664SRajkumar Manoharan 		ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
203618235664SRajkumar Manoharan 		__skb_queue_purge(&amsdu);
203718235664SRajkumar Manoharan 		/* FIXME: It's probably a good idea to reboot the
203818235664SRajkumar Manoharan 		 * device instead of leaving it inoperable.
203918235664SRajkumar Manoharan 		 */
204018235664SRajkumar Manoharan 		htt->rx_confused = true;
204118235664SRajkumar Manoharan 		return ret;
204218235664SRajkumar Manoharan 	}
204318235664SRajkumar Manoharan 
2044caee728aSVasanthakumar Thiagarajan 	num_msdus = skb_queue_len(&amsdu);
2045caee728aSVasanthakumar Thiagarajan 
2046237e15dfSAshok Raj Nagarajan 	ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
20477543d116SMohammed Shafi Shajakhan 
20487543d116SMohammed Shafi Shajakhan 	/* only for ret = 1 indicates chained msdus */
20497543d116SMohammed Shafi Shajakhan 	if (ret > 0)
2050caee728aSVasanthakumar Thiagarajan 		ath10k_htt_rx_h_unchain(ar, &amsdu, &drop_cnt, &unchain_cnt);
20517543d116SMohammed Shafi Shajakhan 
2052caee728aSVasanthakumar Thiagarajan 	ath10k_htt_rx_h_filter(ar, &amsdu, rx_status, &drop_cnt_filter);
2053caee728aSVasanthakumar Thiagarajan 	ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status, true, first_hdr, &err);
2054caee728aSVasanthakumar Thiagarajan 	msdus_to_queue = skb_queue_len(&amsdu);
2055deba1b9eSRajkumar Manoharan 	ath10k_htt_rx_h_enqueue(ar, &amsdu, rx_status);
205618235664SRajkumar Manoharan 
2057caee728aSVasanthakumar Thiagarajan 	ath10k_sta_update_rx_tid_stats(ar, first_hdr, num_msdus, err,
2058caee728aSVasanthakumar Thiagarajan 				       unchain_cnt, drop_cnt, drop_cnt_filter,
2059caee728aSVasanthakumar Thiagarajan 				       msdus_to_queue);
2060caee728aSVasanthakumar Thiagarajan 
2061deba1b9eSRajkumar Manoharan 	return 0;
206218235664SRajkumar Manoharan }
206318235664SRajkumar Manoharan 
2064130c7749SWen Gong static void ath10k_htt_rx_mpdu_desc_pn_hl(struct htt_hl_rx_desc *rx_desc,
2065130c7749SWen Gong 					  union htt_rx_pn_t *pn,
2066130c7749SWen Gong 					  int pn_len_bits)
2067130c7749SWen Gong {
2068130c7749SWen Gong 	switch (pn_len_bits) {
2069130c7749SWen Gong 	case 48:
2070130c7749SWen Gong 		pn->pn48 = __le32_to_cpu(rx_desc->pn_31_0) +
2071130c7749SWen Gong 			   ((u64)(__le32_to_cpu(rx_desc->u0.pn_63_32) & 0xFFFF) << 32);
2072130c7749SWen Gong 		break;
2073130c7749SWen Gong 	case 24:
2074130c7749SWen Gong 		pn->pn24 = __le32_to_cpu(rx_desc->pn_31_0);
2075130c7749SWen Gong 		break;
2076130c7749SWen Gong 	};
2077130c7749SWen Gong }
2078130c7749SWen Gong 
2079130c7749SWen Gong static bool ath10k_htt_rx_pn_cmp48(union htt_rx_pn_t *new_pn,
2080130c7749SWen Gong 				   union htt_rx_pn_t *old_pn)
2081130c7749SWen Gong {
2082130c7749SWen Gong 	return ((new_pn->pn48 & 0xffffffffffffULL) <=
2083130c7749SWen Gong 		(old_pn->pn48 & 0xffffffffffffULL));
2084130c7749SWen Gong }
2085130c7749SWen Gong 
2086130c7749SWen Gong static bool ath10k_htt_rx_pn_check_replay_hl(struct ath10k *ar,
2087130c7749SWen Gong 					     struct ath10k_peer *peer,
2088130c7749SWen Gong 					     struct htt_rx_indication_hl *rx)
2089130c7749SWen Gong {
2090130c7749SWen Gong 	bool last_pn_valid, pn_invalid = false;
2091130c7749SWen Gong 	enum htt_txrx_sec_cast_type sec_index;
2092130c7749SWen Gong 	enum htt_security_types sec_type;
2093130c7749SWen Gong 	union htt_rx_pn_t new_pn = {0};
2094130c7749SWen Gong 	struct htt_hl_rx_desc *rx_desc;
2095130c7749SWen Gong 	union htt_rx_pn_t *last_pn;
2096130c7749SWen Gong 	u32 rx_desc_info, tid;
2097130c7749SWen Gong 	int num_mpdu_ranges;
2098130c7749SWen Gong 
2099130c7749SWen Gong 	lockdep_assert_held(&ar->data_lock);
2100130c7749SWen Gong 
2101130c7749SWen Gong 	if (!peer)
2102130c7749SWen Gong 		return false;
2103130c7749SWen Gong 
2104130c7749SWen Gong 	if (!(rx->fw_desc.flags & FW_RX_DESC_FLAGS_FIRST_MSDU))
2105130c7749SWen Gong 		return false;
2106130c7749SWen Gong 
2107130c7749SWen Gong 	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
2108130c7749SWen Gong 			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
2109130c7749SWen Gong 
2110130c7749SWen Gong 	rx_desc = (struct htt_hl_rx_desc *)&rx->mpdu_ranges[num_mpdu_ranges];
2111130c7749SWen Gong 	rx_desc_info = __le32_to_cpu(rx_desc->info);
2112130c7749SWen Gong 
2113130c7749SWen Gong 	if (!MS(rx_desc_info, HTT_RX_DESC_HL_INFO_ENCRYPTED))
2114130c7749SWen Gong 		return false;
2115130c7749SWen Gong 
2116130c7749SWen Gong 	tid = MS(rx->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);
2117130c7749SWen Gong 	last_pn_valid = peer->tids_last_pn_valid[tid];
2118130c7749SWen Gong 	last_pn = &peer->tids_last_pn[tid];
2119130c7749SWen Gong 
2120130c7749SWen Gong 	if (MS(rx_desc_info, HTT_RX_DESC_HL_INFO_MCAST_BCAST))
2121130c7749SWen Gong 		sec_index = HTT_TXRX_SEC_MCAST;
2122130c7749SWen Gong 	else
2123130c7749SWen Gong 		sec_index = HTT_TXRX_SEC_UCAST;
2124130c7749SWen Gong 
2125130c7749SWen Gong 	sec_type = peer->rx_pn[sec_index].sec_type;
2126130c7749SWen Gong 	ath10k_htt_rx_mpdu_desc_pn_hl(rx_desc, &new_pn, peer->rx_pn[sec_index].pn_len);
2127130c7749SWen Gong 
2128130c7749SWen Gong 	if (sec_type != HTT_SECURITY_AES_CCMP &&
2129130c7749SWen Gong 	    sec_type != HTT_SECURITY_TKIP &&
2130130c7749SWen Gong 	    sec_type != HTT_SECURITY_TKIP_NOMIC)
2131130c7749SWen Gong 		return false;
2132130c7749SWen Gong 
2133130c7749SWen Gong 	if (last_pn_valid)
2134130c7749SWen Gong 		pn_invalid = ath10k_htt_rx_pn_cmp48(&new_pn, last_pn);
2135130c7749SWen Gong 	else
2136130c7749SWen Gong 		peer->tids_last_pn_valid[tid] = 1;
2137130c7749SWen Gong 
2138130c7749SWen Gong 	if (!pn_invalid)
2139130c7749SWen Gong 		last_pn->pn48 = new_pn.pn48;
2140130c7749SWen Gong 
2141130c7749SWen Gong 	return pn_invalid;
2142130c7749SWen Gong }
2143130c7749SWen Gong 
2144f88d4934SErik Stromdahl static bool ath10k_htt_rx_proc_rx_ind_hl(struct ath10k_htt *htt,
2145f88d4934SErik Stromdahl 					 struct htt_rx_indication_hl *rx,
2146130c7749SWen Gong 					 struct sk_buff *skb,
2147130c7749SWen Gong 					 enum htt_rx_pn_check_type check_pn_type,
2148130c7749SWen Gong 					 enum htt_rx_tkip_demic_type tkip_mic_type)
2149f88d4934SErik Stromdahl {
2150f88d4934SErik Stromdahl 	struct ath10k *ar = htt->ar;
2151f88d4934SErik Stromdahl 	struct ath10k_peer *peer;
2152f88d4934SErik Stromdahl 	struct htt_rx_indication_mpdu_range *mpdu_ranges;
2153f88d4934SErik Stromdahl 	struct fw_rx_desc_hl *fw_desc;
2154f88d4934SErik Stromdahl 	struct ieee80211_hdr *hdr;
2155f88d4934SErik Stromdahl 	struct ieee80211_rx_status *rx_status;
2156f88d4934SErik Stromdahl 	u16 peer_id;
2157f88d4934SErik Stromdahl 	u8 rx_desc_len;
2158f88d4934SErik Stromdahl 	int num_mpdu_ranges;
2159f88d4934SErik Stromdahl 	size_t tot_hdr_len;
2160f88d4934SErik Stromdahl 	struct ieee80211_channel *ch;
2161f88d4934SErik Stromdahl 
2162f88d4934SErik Stromdahl 	peer_id = __le16_to_cpu(rx->hdr.peer_id);
2163f88d4934SErik Stromdahl 
2164f88d4934SErik Stromdahl 	spin_lock_bh(&ar->data_lock);
2165f88d4934SErik Stromdahl 	peer = ath10k_peer_find_by_id(ar, peer_id);
2166f88d4934SErik Stromdahl 	spin_unlock_bh(&ar->data_lock);
2167f88d4934SErik Stromdahl 	if (!peer)
2168f88d4934SErik Stromdahl 		ath10k_warn(ar, "Got RX ind from invalid peer: %u\n", peer_id);
2169f88d4934SErik Stromdahl 
2170f88d4934SErik Stromdahl 	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
2171f88d4934SErik Stromdahl 			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
2172f88d4934SErik Stromdahl 	mpdu_ranges = htt_rx_ind_get_mpdu_ranges_hl(rx);
2173f88d4934SErik Stromdahl 	fw_desc = &rx->fw_desc;
2174f88d4934SErik Stromdahl 	rx_desc_len = fw_desc->len;
2175f88d4934SErik Stromdahl 
2176f88d4934SErik Stromdahl 	/* I have not yet seen any case where num_mpdu_ranges > 1.
2177f88d4934SErik Stromdahl 	 * qcacld does not seem handle that case either, so we introduce the
2178f88d4934SErik Stromdahl 	 * same limitiation here as well.
2179f88d4934SErik Stromdahl 	 */
2180f88d4934SErik Stromdahl 	if (num_mpdu_ranges > 1)
2181f88d4934SErik Stromdahl 		ath10k_warn(ar,
2182f88d4934SErik Stromdahl 			    "Unsupported number of MPDU ranges: %d, ignoring all but the first\n",
2183f88d4934SErik Stromdahl 			    num_mpdu_ranges);
2184f88d4934SErik Stromdahl 
2185f88d4934SErik Stromdahl 	if (mpdu_ranges->mpdu_range_status !=
2186f88d4934SErik Stromdahl 	    HTT_RX_IND_MPDU_STATUS_OK) {
2187f88d4934SErik Stromdahl 		ath10k_warn(ar, "MPDU range status: %d\n",
2188f88d4934SErik Stromdahl 			    mpdu_ranges->mpdu_range_status);
2189f88d4934SErik Stromdahl 		goto err;
2190f88d4934SErik Stromdahl 	}
2191f88d4934SErik Stromdahl 
2192130c7749SWen Gong 	if (check_pn_type == HTT_RX_PN_CHECK &&
2193130c7749SWen Gong 	    ath10k_htt_rx_pn_check_replay_hl(ar, peer, rx))
2194130c7749SWen Gong 		goto err;
2195130c7749SWen Gong 
2196f88d4934SErik Stromdahl 	/* Strip off all headers before the MAC header before delivery to
2197f88d4934SErik Stromdahl 	 * mac80211
2198f88d4934SErik Stromdahl 	 */
2199f88d4934SErik Stromdahl 	tot_hdr_len = sizeof(struct htt_resp_hdr) + sizeof(rx->hdr) +
2200f88d4934SErik Stromdahl 		      sizeof(rx->ppdu) + sizeof(rx->prefix) +
2201f88d4934SErik Stromdahl 		      sizeof(rx->fw_desc) +
2202f88d4934SErik Stromdahl 		      sizeof(*mpdu_ranges) * num_mpdu_ranges + rx_desc_len;
2203130c7749SWen Gong 
2204f88d4934SErik Stromdahl 	skb_pull(skb, tot_hdr_len);
2205f88d4934SErik Stromdahl 
2206f88d4934SErik Stromdahl 	hdr = (struct ieee80211_hdr *)skb->data;
2207f88d4934SErik Stromdahl 	rx_status = IEEE80211_SKB_RXCB(skb);
2208f88d4934SErik Stromdahl 	rx_status->chains |= BIT(0);
22097d444522SAlagu Sankar 	if (rx->ppdu.combined_rssi == 0) {
22107d444522SAlagu Sankar 		/* SDIO firmware does not provide signal */
22117d444522SAlagu Sankar 		rx_status->signal = 0;
22127d444522SAlagu Sankar 		rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
22137d444522SAlagu Sankar 	} else {
2214f88d4934SErik Stromdahl 		rx_status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
2215f88d4934SErik Stromdahl 			rx->ppdu.combined_rssi;
2216f88d4934SErik Stromdahl 		rx_status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
22177d444522SAlagu Sankar 	}
2218f88d4934SErik Stromdahl 
2219f88d4934SErik Stromdahl 	spin_lock_bh(&ar->data_lock);
2220f88d4934SErik Stromdahl 	ch = ar->scan_channel;
2221f88d4934SErik Stromdahl 	if (!ch)
2222f88d4934SErik Stromdahl 		ch = ar->rx_channel;
2223f88d4934SErik Stromdahl 	if (!ch)
2224f88d4934SErik Stromdahl 		ch = ath10k_htt_rx_h_any_channel(ar);
2225f88d4934SErik Stromdahl 	if (!ch)
2226f88d4934SErik Stromdahl 		ch = ar->tgt_oper_chan;
2227f88d4934SErik Stromdahl 	spin_unlock_bh(&ar->data_lock);
2228f88d4934SErik Stromdahl 
2229f88d4934SErik Stromdahl 	if (ch) {
2230f88d4934SErik Stromdahl 		rx_status->band = ch->band;
2231f88d4934SErik Stromdahl 		rx_status->freq = ch->center_freq;
2232f88d4934SErik Stromdahl 	}
2233f88d4934SErik Stromdahl 	if (rx->fw_desc.flags & FW_RX_DESC_FLAGS_LAST_MSDU)
2234f88d4934SErik Stromdahl 		rx_status->flag &= ~RX_FLAG_AMSDU_MORE;
2235f88d4934SErik Stromdahl 	else
2236f88d4934SErik Stromdahl 		rx_status->flag |= RX_FLAG_AMSDU_MORE;
2237f88d4934SErik Stromdahl 
2238f88d4934SErik Stromdahl 	/* Not entirely sure about this, but all frames from the chipset has
2239f88d4934SErik Stromdahl 	 * the protected flag set even though they have already been decrypted.
2240f88d4934SErik Stromdahl 	 * Unmasking this flag is necessary in order for mac80211 not to drop
2241f88d4934SErik Stromdahl 	 * the frame.
2242f88d4934SErik Stromdahl 	 * TODO: Verify this is always the case or find out a way to check
2243f88d4934SErik Stromdahl 	 * if there has been hw decryption.
2244f88d4934SErik Stromdahl 	 */
2245f88d4934SErik Stromdahl 	if (ieee80211_has_protected(hdr->frame_control)) {
2246f88d4934SErik Stromdahl 		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
2247f88d4934SErik Stromdahl 		rx_status->flag |= RX_FLAG_DECRYPTED |
2248f88d4934SErik Stromdahl 				   RX_FLAG_IV_STRIPPED |
2249f88d4934SErik Stromdahl 				   RX_FLAG_MMIC_STRIPPED;
2250f88d4934SErik Stromdahl 	}
2251f88d4934SErik Stromdahl 
2252130c7749SWen Gong 	if (tkip_mic_type == HTT_RX_TKIP_MIC)
2253130c7749SWen Gong 		rx_status->flag &= ~RX_FLAG_IV_STRIPPED &
2254130c7749SWen Gong 				   ~RX_FLAG_MMIC_STRIPPED;
2255130c7749SWen Gong 
2256f88d4934SErik Stromdahl 	ieee80211_rx_ni(ar->hw, skb);
2257f88d4934SErik Stromdahl 
2258f88d4934SErik Stromdahl 	/* We have delivered the skb to the upper layers (mac80211) so we
2259f88d4934SErik Stromdahl 	 * must not free it.
2260f88d4934SErik Stromdahl 	 */
2261f88d4934SErik Stromdahl 	return false;
2262f88d4934SErik Stromdahl err:
2263f88d4934SErik Stromdahl 	/* Tell the caller that it must free the skb since we have not
2264f88d4934SErik Stromdahl 	 * consumed it
2265f88d4934SErik Stromdahl 	 */
2266f88d4934SErik Stromdahl 	return true;
2267f88d4934SErik Stromdahl }
2268f88d4934SErik Stromdahl 
226933f97472SWen Gong static int ath10k_htt_rx_frag_tkip_decap_nomic(struct sk_buff *skb,
227033f97472SWen Gong 					       u16 head_len,
227133f97472SWen Gong 					       u16 hdr_len)
227233f97472SWen Gong {
227333f97472SWen Gong 	u8 *ivp, *orig_hdr;
227433f97472SWen Gong 
227533f97472SWen Gong 	orig_hdr = skb->data;
227633f97472SWen Gong 	ivp = orig_hdr + hdr_len + head_len;
227733f97472SWen Gong 
227833f97472SWen Gong 	/* the ExtIV bit is always set to 1 for TKIP */
227933f97472SWen Gong 	if (!(ivp[IEEE80211_WEP_IV_LEN - 1] & ATH10K_IEEE80211_EXTIV))
228033f97472SWen Gong 		return -EINVAL;
228133f97472SWen Gong 
228233f97472SWen Gong 	memmove(orig_hdr + IEEE80211_TKIP_IV_LEN, orig_hdr, head_len + hdr_len);
228333f97472SWen Gong 	skb_pull(skb, IEEE80211_TKIP_IV_LEN);
228433f97472SWen Gong 	skb_trim(skb, skb->len - ATH10K_IEEE80211_TKIP_MICLEN);
228533f97472SWen Gong 	return 0;
228633f97472SWen Gong }
228733f97472SWen Gong 
228833f97472SWen Gong static int ath10k_htt_rx_frag_tkip_decap_withmic(struct sk_buff *skb,
228933f97472SWen Gong 						 u16 head_len,
229033f97472SWen Gong 						 u16 hdr_len)
229133f97472SWen Gong {
229233f97472SWen Gong 	u8 *ivp, *orig_hdr;
229333f97472SWen Gong 
229433f97472SWen Gong 	orig_hdr = skb->data;
229533f97472SWen Gong 	ivp = orig_hdr + hdr_len + head_len;
229633f97472SWen Gong 
229733f97472SWen Gong 	/* the ExtIV bit is always set to 1 for TKIP */
229833f97472SWen Gong 	if (!(ivp[IEEE80211_WEP_IV_LEN - 1] & ATH10K_IEEE80211_EXTIV))
229933f97472SWen Gong 		return -EINVAL;
230033f97472SWen Gong 
230133f97472SWen Gong 	memmove(orig_hdr + IEEE80211_TKIP_IV_LEN, orig_hdr, head_len + hdr_len);
230233f97472SWen Gong 	skb_pull(skb, IEEE80211_TKIP_IV_LEN);
230333f97472SWen Gong 	skb_trim(skb, skb->len - IEEE80211_TKIP_ICV_LEN);
230433f97472SWen Gong 	return 0;
230533f97472SWen Gong }
230633f97472SWen Gong 
230733f97472SWen Gong static int ath10k_htt_rx_frag_ccmp_decap(struct sk_buff *skb,
230833f97472SWen Gong 					 u16 head_len,
230933f97472SWen Gong 					 u16 hdr_len)
231033f97472SWen Gong {
231133f97472SWen Gong 	u8 *ivp, *orig_hdr;
231233f97472SWen Gong 
231333f97472SWen Gong 	orig_hdr = skb->data;
231433f97472SWen Gong 	ivp = orig_hdr + hdr_len + head_len;
231533f97472SWen Gong 
231633f97472SWen Gong 	/* the ExtIV bit is always set to 1 for CCMP */
231733f97472SWen Gong 	if (!(ivp[IEEE80211_WEP_IV_LEN - 1] & ATH10K_IEEE80211_EXTIV))
231833f97472SWen Gong 		return -EINVAL;
231933f97472SWen Gong 
232033f97472SWen Gong 	skb_trim(skb, skb->len - IEEE80211_CCMP_MIC_LEN);
232133f97472SWen Gong 	memmove(orig_hdr + IEEE80211_CCMP_HDR_LEN, orig_hdr, head_len + hdr_len);
232233f97472SWen Gong 	skb_pull(skb, IEEE80211_CCMP_HDR_LEN);
232333f97472SWen Gong 	return 0;
232433f97472SWen Gong }
232533f97472SWen Gong 
232633f97472SWen Gong static int ath10k_htt_rx_frag_wep_decap(struct sk_buff *skb,
232733f97472SWen Gong 					u16 head_len,
232833f97472SWen Gong 					u16 hdr_len)
232933f97472SWen Gong {
233033f97472SWen Gong 	u8 *orig_hdr;
233133f97472SWen Gong 
233233f97472SWen Gong 	orig_hdr = skb->data;
233333f97472SWen Gong 
233433f97472SWen Gong 	memmove(orig_hdr + IEEE80211_WEP_IV_LEN,
233533f97472SWen Gong 		orig_hdr, head_len + hdr_len);
233633f97472SWen Gong 	skb_pull(skb, IEEE80211_WEP_IV_LEN);
233733f97472SWen Gong 	skb_trim(skb, skb->len - IEEE80211_WEP_ICV_LEN);
233833f97472SWen Gong 	return 0;
233933f97472SWen Gong }
234033f97472SWen Gong 
234133f97472SWen Gong static bool ath10k_htt_rx_proc_rx_frag_ind_hl(struct ath10k_htt *htt,
234233f97472SWen Gong 					      struct htt_rx_fragment_indication *rx,
234333f97472SWen Gong 					      struct sk_buff *skb)
234433f97472SWen Gong {
234533f97472SWen Gong 	struct ath10k *ar = htt->ar;
234633f97472SWen Gong 	enum htt_rx_tkip_demic_type tkip_mic = HTT_RX_NON_TKIP_MIC;
234733f97472SWen Gong 	enum htt_txrx_sec_cast_type sec_index;
234833f97472SWen Gong 	struct htt_rx_indication_hl *rx_hl;
234933f97472SWen Gong 	enum htt_security_types sec_type;
235033f97472SWen Gong 	u32 tid, frag, seq, rx_desc_info;
235133f97472SWen Gong 	union htt_rx_pn_t new_pn = {0};
235233f97472SWen Gong 	struct htt_hl_rx_desc *rx_desc;
235333f97472SWen Gong 	u16 peer_id, sc, hdr_space;
235433f97472SWen Gong 	union htt_rx_pn_t *last_pn;
235533f97472SWen Gong 	struct ieee80211_hdr *hdr;
235633f97472SWen Gong 	int ret, num_mpdu_ranges;
235733f97472SWen Gong 	struct ath10k_peer *peer;
235833f97472SWen Gong 	struct htt_resp *resp;
235933f97472SWen Gong 	size_t tot_hdr_len;
236033f97472SWen Gong 
236133f97472SWen Gong 	resp = (struct htt_resp *)(skb->data + HTT_RX_FRAG_IND_INFO0_HEADER_LEN);
236233f97472SWen Gong 	skb_pull(skb, HTT_RX_FRAG_IND_INFO0_HEADER_LEN);
236333f97472SWen Gong 	skb_trim(skb, skb->len - FCS_LEN);
236433f97472SWen Gong 
236533f97472SWen Gong 	peer_id = __le16_to_cpu(rx->peer_id);
236633f97472SWen Gong 	rx_hl = (struct htt_rx_indication_hl *)(&resp->rx_ind_hl);
236733f97472SWen Gong 
236833f97472SWen Gong 	spin_lock_bh(&ar->data_lock);
236933f97472SWen Gong 	peer = ath10k_peer_find_by_id(ar, peer_id);
237033f97472SWen Gong 	if (!peer) {
237133f97472SWen Gong 		ath10k_dbg(ar, ATH10K_DBG_HTT, "invalid peer: %u\n", peer_id);
237233f97472SWen Gong 		goto err;
237333f97472SWen Gong 	}
237433f97472SWen Gong 
237533f97472SWen Gong 	num_mpdu_ranges = MS(__le32_to_cpu(rx_hl->hdr.info1),
237633f97472SWen Gong 			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
237733f97472SWen Gong 
237833f97472SWen Gong 	tot_hdr_len = sizeof(struct htt_resp_hdr) +
237933f97472SWen Gong 		      sizeof(rx_hl->hdr) +
238033f97472SWen Gong 		      sizeof(rx_hl->ppdu) +
238133f97472SWen Gong 		      sizeof(rx_hl->prefix) +
238233f97472SWen Gong 		      sizeof(rx_hl->fw_desc) +
238333f97472SWen Gong 		      sizeof(struct htt_rx_indication_mpdu_range) * num_mpdu_ranges;
238433f97472SWen Gong 
238533f97472SWen Gong 	tid =  MS(rx_hl->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);
238633f97472SWen Gong 	rx_desc = (struct htt_hl_rx_desc *)(skb->data + tot_hdr_len);
238733f97472SWen Gong 	rx_desc_info = __le32_to_cpu(rx_desc->info);
238833f97472SWen Gong 
238933f97472SWen Gong 	if (!MS(rx_desc_info, HTT_RX_DESC_HL_INFO_ENCRYPTED)) {
239033f97472SWen Gong 		spin_unlock_bh(&ar->data_lock);
239133f97472SWen Gong 		return ath10k_htt_rx_proc_rx_ind_hl(htt, &resp->rx_ind_hl, skb,
239233f97472SWen Gong 						    HTT_RX_NON_PN_CHECK,
239333f97472SWen Gong 						    HTT_RX_NON_TKIP_MIC);
239433f97472SWen Gong 	}
239533f97472SWen Gong 
239633f97472SWen Gong 	hdr = (struct ieee80211_hdr *)((u8 *)rx_desc + rx_hl->fw_desc.len);
239733f97472SWen Gong 
239833f97472SWen Gong 	if (ieee80211_has_retry(hdr->frame_control))
239933f97472SWen Gong 		goto err;
240033f97472SWen Gong 
240133f97472SWen Gong 	hdr_space = ieee80211_hdrlen(hdr->frame_control);
240233f97472SWen Gong 	sc = __le16_to_cpu(hdr->seq_ctrl);
240333f97472SWen Gong 	seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
240433f97472SWen Gong 	frag = sc & IEEE80211_SCTL_FRAG;
240533f97472SWen Gong 
240633f97472SWen Gong 	sec_index = MS(rx_desc_info, HTT_RX_DESC_HL_INFO_MCAST_BCAST) ?
240733f97472SWen Gong 		    HTT_TXRX_SEC_MCAST : HTT_TXRX_SEC_UCAST;
240833f97472SWen Gong 	sec_type = peer->rx_pn[sec_index].sec_type;
240933f97472SWen Gong 	ath10k_htt_rx_mpdu_desc_pn_hl(rx_desc, &new_pn, peer->rx_pn[sec_index].pn_len);
241033f97472SWen Gong 
241133f97472SWen Gong 	switch (sec_type) {
241233f97472SWen Gong 	case HTT_SECURITY_TKIP:
241333f97472SWen Gong 		tkip_mic = HTT_RX_TKIP_MIC;
241433f97472SWen Gong 		ret = ath10k_htt_rx_frag_tkip_decap_withmic(skb,
241533f97472SWen Gong 							    tot_hdr_len +
241633f97472SWen Gong 							    rx_hl->fw_desc.len,
241733f97472SWen Gong 							    hdr_space);
241833f97472SWen Gong 		if (ret)
241933f97472SWen Gong 			goto err;
242033f97472SWen Gong 		break;
242133f97472SWen Gong 	case HTT_SECURITY_TKIP_NOMIC:
242233f97472SWen Gong 		ret = ath10k_htt_rx_frag_tkip_decap_nomic(skb,
242333f97472SWen Gong 							  tot_hdr_len +
242433f97472SWen Gong 							  rx_hl->fw_desc.len,
242533f97472SWen Gong 							  hdr_space);
242633f97472SWen Gong 		if (ret)
242733f97472SWen Gong 			goto err;
242833f97472SWen Gong 		break;
242933f97472SWen Gong 	case HTT_SECURITY_AES_CCMP:
243033f97472SWen Gong 		ret = ath10k_htt_rx_frag_ccmp_decap(skb,
243133f97472SWen Gong 						    tot_hdr_len + rx_hl->fw_desc.len,
243233f97472SWen Gong 						    hdr_space);
243333f97472SWen Gong 		if (ret)
243433f97472SWen Gong 			goto err;
243533f97472SWen Gong 		break;
243633f97472SWen Gong 	case HTT_SECURITY_WEP128:
243733f97472SWen Gong 	case HTT_SECURITY_WEP104:
243833f97472SWen Gong 	case HTT_SECURITY_WEP40:
243933f97472SWen Gong 		ret = ath10k_htt_rx_frag_wep_decap(skb,
244033f97472SWen Gong 						   tot_hdr_len + rx_hl->fw_desc.len,
244133f97472SWen Gong 						   hdr_space);
244233f97472SWen Gong 		if (ret)
244333f97472SWen Gong 			goto err;
244433f97472SWen Gong 		break;
244533f97472SWen Gong 	default:
244633f97472SWen Gong 		break;
244733f97472SWen Gong 	}
244833f97472SWen Gong 
244933f97472SWen Gong 	resp = (struct htt_resp *)(skb->data);
245033f97472SWen Gong 
245133f97472SWen Gong 	if (sec_type != HTT_SECURITY_AES_CCMP &&
245233f97472SWen Gong 	    sec_type != HTT_SECURITY_TKIP &&
245333f97472SWen Gong 	    sec_type != HTT_SECURITY_TKIP_NOMIC) {
245433f97472SWen Gong 		spin_unlock_bh(&ar->data_lock);
245533f97472SWen Gong 		return ath10k_htt_rx_proc_rx_ind_hl(htt, &resp->rx_ind_hl, skb,
245633f97472SWen Gong 						    HTT_RX_NON_PN_CHECK,
245733f97472SWen Gong 						    HTT_RX_NON_TKIP_MIC);
245833f97472SWen Gong 	}
245933f97472SWen Gong 
246033f97472SWen Gong 	last_pn = &peer->frag_tids_last_pn[tid];
246133f97472SWen Gong 
246233f97472SWen Gong 	if (frag == 0) {
246333f97472SWen Gong 		if (ath10k_htt_rx_pn_check_replay_hl(ar, peer, &resp->rx_ind_hl))
246433f97472SWen Gong 			goto err;
246533f97472SWen Gong 
246633f97472SWen Gong 		last_pn->pn48 = new_pn.pn48;
246733f97472SWen Gong 		peer->frag_tids_seq[tid] = seq;
246833f97472SWen Gong 	} else if (sec_type == HTT_SECURITY_AES_CCMP) {
246933f97472SWen Gong 		if (seq != peer->frag_tids_seq[tid])
247033f97472SWen Gong 			goto err;
247133f97472SWen Gong 
247233f97472SWen Gong 		if (new_pn.pn48 != last_pn->pn48 + 1)
247333f97472SWen Gong 			goto err;
247433f97472SWen Gong 
247533f97472SWen Gong 		last_pn->pn48 = new_pn.pn48;
247633f97472SWen Gong 		last_pn = &peer->tids_last_pn[tid];
247733f97472SWen Gong 		last_pn->pn48 = new_pn.pn48;
247833f97472SWen Gong 	}
247933f97472SWen Gong 
248033f97472SWen Gong 	spin_unlock_bh(&ar->data_lock);
248133f97472SWen Gong 
248233f97472SWen Gong 	return ath10k_htt_rx_proc_rx_ind_hl(htt, &resp->rx_ind_hl, skb,
248333f97472SWen Gong 					    HTT_RX_NON_PN_CHECK, tkip_mic);
248433f97472SWen Gong 
248533f97472SWen Gong err:
248633f97472SWen Gong 	spin_unlock_bh(&ar->data_lock);
248733f97472SWen Gong 
248833f97472SWen Gong 	/* Tell the caller that it must free the skb since we have not
248933f97472SWen Gong 	 * consumed it
249033f97472SWen Gong 	 */
249133f97472SWen Gong 	return true;
249233f97472SWen Gong }
249333f97472SWen Gong 
2494f88d4934SErik Stromdahl static void ath10k_htt_rx_proc_rx_ind_ll(struct ath10k_htt *htt,
24955e3dd157SKalle Valo 					 struct htt_rx_indication *rx)
24965e3dd157SKalle Valo {
24977aa7a72aSMichal Kazior 	struct ath10k *ar = htt->ar;
24985e3dd157SKalle Valo 	struct htt_rx_indication_mpdu_range *mpdu_ranges;
24995e3dd157SKalle Valo 	int num_mpdu_ranges;
250018235664SRajkumar Manoharan 	int i, mpdu_count = 0;
2501caee728aSVasanthakumar Thiagarajan 	u16 peer_id;
2502caee728aSVasanthakumar Thiagarajan 	u8 tid;
25035e3dd157SKalle Valo 
25045e3dd157SKalle Valo 	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
25055e3dd157SKalle Valo 			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
2506caee728aSVasanthakumar Thiagarajan 	peer_id = __le16_to_cpu(rx->hdr.peer_id);
2507caee728aSVasanthakumar Thiagarajan 	tid =  MS(rx->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);
2508caee728aSVasanthakumar Thiagarajan 
25095e3dd157SKalle Valo 	mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
25105e3dd157SKalle Valo 
25117aa7a72aSMichal Kazior 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
251235b50e70SGustavo A. R. Silva 			rx, struct_size(rx, mpdu_ranges, num_mpdu_ranges));
25135e3dd157SKalle Valo 
2514d540690dSMichal Kazior 	for (i = 0; i < num_mpdu_ranges; i++)
2515d540690dSMichal Kazior 		mpdu_count += mpdu_ranges[i].mpdu_count;
2516d540690dSMichal Kazior 
25173128b3d8SRajkumar Manoharan 	atomic_add(mpdu_count, &htt->num_mpdus_ready);
2518caee728aSVasanthakumar Thiagarajan 
2519caee728aSVasanthakumar Thiagarajan 	ath10k_sta_update_rx_tid_stats_ampdu(ar, peer_id, tid, mpdu_ranges,
2520caee728aSVasanthakumar Thiagarajan 					     num_mpdu_ranges);
25215e3dd157SKalle Valo }
25225e3dd157SKalle Valo 
252359465fe4SRajkumar Manoharan static void ath10k_htt_rx_tx_compl_ind(struct ath10k *ar,
25246c5151a9SMichal Kazior 				       struct sk_buff *skb)
25256c5151a9SMichal Kazior {
25266c5151a9SMichal Kazior 	struct ath10k_htt *htt = &ar->htt;
25276c5151a9SMichal Kazior 	struct htt_resp *resp = (struct htt_resp *)skb->data;
25286c5151a9SMichal Kazior 	struct htt_tx_done tx_done = {};
25296c5151a9SMichal Kazior 	int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
2530c7fd8d23SBalaji Pothunoori 	__le16 msdu_id, *msdus;
2531c7fd8d23SBalaji Pothunoori 	bool rssi_enabled = false;
2532bb31b7cbSManikanta Pubbisetty 	u8 msdu_count = 0, num_airtime_records, tid;
25336ddc3860SAbhishek Ambure 	int i, htt_pad = 0;
2534bb31b7cbSManikanta Pubbisetty 	struct htt_data_tx_compl_ppdu_dur *ppdu_info;
2535bb31b7cbSManikanta Pubbisetty 	struct ath10k_peer *peer;
2536bb31b7cbSManikanta Pubbisetty 	u16 ppdu_info_offset = 0, peer_id;
2537bb31b7cbSManikanta Pubbisetty 	u32 tx_duration;
25386c5151a9SMichal Kazior 
25396c5151a9SMichal Kazior 	switch (status) {
25406c5151a9SMichal Kazior 	case HTT_DATA_TX_STATUS_NO_ACK:
254159465fe4SRajkumar Manoharan 		tx_done.status = HTT_TX_COMPL_STATE_NOACK;
25426c5151a9SMichal Kazior 		break;
25436c5151a9SMichal Kazior 	case HTT_DATA_TX_STATUS_OK:
254459465fe4SRajkumar Manoharan 		tx_done.status = HTT_TX_COMPL_STATE_ACK;
25456c5151a9SMichal Kazior 		break;
25466c5151a9SMichal Kazior 	case HTT_DATA_TX_STATUS_DISCARD:
25476c5151a9SMichal Kazior 	case HTT_DATA_TX_STATUS_POSTPONE:
25486c5151a9SMichal Kazior 	case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
254959465fe4SRajkumar Manoharan 		tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
25506c5151a9SMichal Kazior 		break;
25516c5151a9SMichal Kazior 	default:
25527aa7a72aSMichal Kazior 		ath10k_warn(ar, "unhandled tx completion status %d\n", status);
255359465fe4SRajkumar Manoharan 		tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
25546c5151a9SMichal Kazior 		break;
25556c5151a9SMichal Kazior 	}
25566c5151a9SMichal Kazior 
25577aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
25586c5151a9SMichal Kazior 		   resp->data_tx_completion.num_msdus);
25596c5151a9SMichal Kazior 
2560c7fd8d23SBalaji Pothunoori 	msdu_count = resp->data_tx_completion.num_msdus;
2561bb31b7cbSManikanta Pubbisetty 	msdus = resp->data_tx_completion.msdus;
25626ddc3860SAbhishek Ambure 	rssi_enabled = ath10k_is_rssi_enable(&ar->hw_params, resp);
2563c7fd8d23SBalaji Pothunoori 
25646ddc3860SAbhishek Ambure 	if (rssi_enabled)
25656ddc3860SAbhishek Ambure 		htt_pad = ath10k_tx_data_rssi_get_pad_bytes(&ar->hw_params,
25666ddc3860SAbhishek Ambure 							    resp);
2567c7fd8d23SBalaji Pothunoori 
2568c7fd8d23SBalaji Pothunoori 	for (i = 0; i < msdu_count; i++) {
2569c7fd8d23SBalaji Pothunoori 		msdu_id = msdus[i];
25706c5151a9SMichal Kazior 		tx_done.msdu_id = __le16_to_cpu(msdu_id);
257159465fe4SRajkumar Manoharan 
2572c7fd8d23SBalaji Pothunoori 		if (rssi_enabled) {
2573c7fd8d23SBalaji Pothunoori 			/* Total no of MSDUs should be even,
2574c7fd8d23SBalaji Pothunoori 			 * if odd MSDUs are sent firmware fills
2575c7fd8d23SBalaji Pothunoori 			 * last msdu id with 0xffff
2576c7fd8d23SBalaji Pothunoori 			 */
2577c7fd8d23SBalaji Pothunoori 			if (msdu_count & 0x01) {
25786ddc3860SAbhishek Ambure 				msdu_id = msdus[msdu_count +  i + 1 + htt_pad];
2579c7fd8d23SBalaji Pothunoori 				tx_done.ack_rssi = __le16_to_cpu(msdu_id);
2580c7fd8d23SBalaji Pothunoori 			} else {
25816ddc3860SAbhishek Ambure 				msdu_id = msdus[msdu_count +  i + htt_pad];
2582c7fd8d23SBalaji Pothunoori 				tx_done.ack_rssi = __le16_to_cpu(msdu_id);
2583c7fd8d23SBalaji Pothunoori 			}
2584c7fd8d23SBalaji Pothunoori 		}
2585c7fd8d23SBalaji Pothunoori 
258659465fe4SRajkumar Manoharan 		/* kfifo_put: In practice firmware shouldn't fire off per-CE
258759465fe4SRajkumar Manoharan 		 * interrupt and main interrupt (MSI/-X range case) for the same
258859465fe4SRajkumar Manoharan 		 * HTC service so it should be safe to use kfifo_put w/o lock.
258959465fe4SRajkumar Manoharan 		 *
259059465fe4SRajkumar Manoharan 		 * From kfifo_put() documentation:
259159465fe4SRajkumar Manoharan 		 *  Note that with only one concurrent reader and one concurrent
259259465fe4SRajkumar Manoharan 		 *  writer, you don't need extra locking to use these macro.
259359465fe4SRajkumar Manoharan 		 */
2594e2a6b711SAlagu Sankar 		if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL) {
2595e2a6b711SAlagu Sankar 			ath10k_txrx_tx_unref(htt, &tx_done);
2596e2a6b711SAlagu Sankar 		} else if (!kfifo_put(&htt->txdone_fifo, tx_done)) {
259759465fe4SRajkumar Manoharan 			ath10k_warn(ar, "txdone fifo overrun, msdu_id %d status %d\n",
259859465fe4SRajkumar Manoharan 				    tx_done.msdu_id, tx_done.status);
25996c5151a9SMichal Kazior 			ath10k_txrx_tx_unref(htt, &tx_done);
26006c5151a9SMichal Kazior 		}
26016c5151a9SMichal Kazior 	}
2602bb31b7cbSManikanta Pubbisetty 
2603bb31b7cbSManikanta Pubbisetty 	if (!(resp->data_tx_completion.flags2 & HTT_TX_CMPL_FLAG_PPDU_DURATION_PRESENT))
2604bb31b7cbSManikanta Pubbisetty 		return;
2605bb31b7cbSManikanta Pubbisetty 
2606bb31b7cbSManikanta Pubbisetty 	ppdu_info_offset = (msdu_count & 0x01) ? msdu_count + 1 : msdu_count;
2607bb31b7cbSManikanta Pubbisetty 
2608bb31b7cbSManikanta Pubbisetty 	if (rssi_enabled)
2609bb31b7cbSManikanta Pubbisetty 		ppdu_info_offset += ppdu_info_offset;
2610bb31b7cbSManikanta Pubbisetty 
2611bb31b7cbSManikanta Pubbisetty 	if (resp->data_tx_completion.flags2 &
2612bb31b7cbSManikanta Pubbisetty 	    (HTT_TX_CMPL_FLAG_PPID_PRESENT | HTT_TX_CMPL_FLAG_PA_PRESENT))
2613bb31b7cbSManikanta Pubbisetty 		ppdu_info_offset += 2;
2614bb31b7cbSManikanta Pubbisetty 
2615bb31b7cbSManikanta Pubbisetty 	ppdu_info = (struct htt_data_tx_compl_ppdu_dur *)&msdus[ppdu_info_offset];
2616bb31b7cbSManikanta Pubbisetty 	num_airtime_records = FIELD_GET(HTT_TX_COMPL_PPDU_DUR_INFO0_NUM_ENTRIES_MASK,
2617bb31b7cbSManikanta Pubbisetty 					__le32_to_cpu(ppdu_info->info0));
2618bb31b7cbSManikanta Pubbisetty 
2619bb31b7cbSManikanta Pubbisetty 	for (i = 0; i < num_airtime_records; i++) {
2620bb31b7cbSManikanta Pubbisetty 		struct htt_data_tx_ppdu_dur *ppdu_dur;
2621bb31b7cbSManikanta Pubbisetty 		u32 info0;
2622bb31b7cbSManikanta Pubbisetty 
2623bb31b7cbSManikanta Pubbisetty 		ppdu_dur = &ppdu_info->ppdu_dur[i];
2624bb31b7cbSManikanta Pubbisetty 		info0 = __le32_to_cpu(ppdu_dur->info0);
2625bb31b7cbSManikanta Pubbisetty 
2626bb31b7cbSManikanta Pubbisetty 		peer_id = FIELD_GET(HTT_TX_PPDU_DUR_INFO0_PEER_ID_MASK,
2627bb31b7cbSManikanta Pubbisetty 				    info0);
2628bb31b7cbSManikanta Pubbisetty 		rcu_read_lock();
2629bb31b7cbSManikanta Pubbisetty 		spin_lock_bh(&ar->data_lock);
2630bb31b7cbSManikanta Pubbisetty 
2631bb31b7cbSManikanta Pubbisetty 		peer = ath10k_peer_find_by_id(ar, peer_id);
2632bb31b7cbSManikanta Pubbisetty 		if (!peer) {
2633bb31b7cbSManikanta Pubbisetty 			spin_unlock_bh(&ar->data_lock);
2634bb31b7cbSManikanta Pubbisetty 			rcu_read_unlock();
2635bb31b7cbSManikanta Pubbisetty 			continue;
2636bb31b7cbSManikanta Pubbisetty 		}
2637bb31b7cbSManikanta Pubbisetty 
2638bb31b7cbSManikanta Pubbisetty 		tid = FIELD_GET(HTT_TX_PPDU_DUR_INFO0_TID_MASK, info0);
2639bb31b7cbSManikanta Pubbisetty 		tx_duration = __le32_to_cpu(ppdu_dur->tx_duration);
2640bb31b7cbSManikanta Pubbisetty 
2641bb31b7cbSManikanta Pubbisetty 		ieee80211_sta_register_airtime(peer->sta, tid, tx_duration, 0);
2642bb31b7cbSManikanta Pubbisetty 
2643bb31b7cbSManikanta Pubbisetty 		spin_unlock_bh(&ar->data_lock);
2644bb31b7cbSManikanta Pubbisetty 		rcu_read_unlock();
2645bb31b7cbSManikanta Pubbisetty 	}
264659465fe4SRajkumar Manoharan }
26476c5151a9SMichal Kazior 
2648aa5b4fbcSMichal Kazior static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
2649aa5b4fbcSMichal Kazior {
2650aa5b4fbcSMichal Kazior 	struct htt_rx_addba *ev = &resp->rx_addba;
2651aa5b4fbcSMichal Kazior 	struct ath10k_peer *peer;
2652aa5b4fbcSMichal Kazior 	struct ath10k_vif *arvif;
2653aa5b4fbcSMichal Kazior 	u16 info0, tid, peer_id;
2654aa5b4fbcSMichal Kazior 
2655aa5b4fbcSMichal Kazior 	info0 = __le16_to_cpu(ev->info0);
2656aa5b4fbcSMichal Kazior 	tid = MS(info0, HTT_RX_BA_INFO0_TID);
2657aa5b4fbcSMichal Kazior 	peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
2658aa5b4fbcSMichal Kazior 
26597aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
2660aa5b4fbcSMichal Kazior 		   "htt rx addba tid %hu peer_id %hu size %hhu\n",
2661aa5b4fbcSMichal Kazior 		   tid, peer_id, ev->window_size);
2662aa5b4fbcSMichal Kazior 
2663aa5b4fbcSMichal Kazior 	spin_lock_bh(&ar->data_lock);
2664aa5b4fbcSMichal Kazior 	peer = ath10k_peer_find_by_id(ar, peer_id);
2665aa5b4fbcSMichal Kazior 	if (!peer) {
26667aa7a72aSMichal Kazior 		ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
2667aa5b4fbcSMichal Kazior 			    peer_id);
2668aa5b4fbcSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
2669aa5b4fbcSMichal Kazior 		return;
2670aa5b4fbcSMichal Kazior 	}
2671aa5b4fbcSMichal Kazior 
2672aa5b4fbcSMichal Kazior 	arvif = ath10k_get_arvif(ar, peer->vdev_id);
2673aa5b4fbcSMichal Kazior 	if (!arvif) {
26747aa7a72aSMichal Kazior 		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
2675aa5b4fbcSMichal Kazior 			    peer->vdev_id);
2676aa5b4fbcSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
2677aa5b4fbcSMichal Kazior 		return;
2678aa5b4fbcSMichal Kazior 	}
2679aa5b4fbcSMichal Kazior 
26807aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
2681aa5b4fbcSMichal Kazior 		   "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
2682aa5b4fbcSMichal Kazior 		   peer->addr, tid, ev->window_size);
2683aa5b4fbcSMichal Kazior 
2684aa5b4fbcSMichal Kazior 	ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
2685aa5b4fbcSMichal Kazior 	spin_unlock_bh(&ar->data_lock);
2686aa5b4fbcSMichal Kazior }
2687aa5b4fbcSMichal Kazior 
2688aa5b4fbcSMichal Kazior static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
2689aa5b4fbcSMichal Kazior {
2690aa5b4fbcSMichal Kazior 	struct htt_rx_delba *ev = &resp->rx_delba;
2691aa5b4fbcSMichal Kazior 	struct ath10k_peer *peer;
2692aa5b4fbcSMichal Kazior 	struct ath10k_vif *arvif;
2693aa5b4fbcSMichal Kazior 	u16 info0, tid, peer_id;
2694aa5b4fbcSMichal Kazior 
2695aa5b4fbcSMichal Kazior 	info0 = __le16_to_cpu(ev->info0);
2696aa5b4fbcSMichal Kazior 	tid = MS(info0, HTT_RX_BA_INFO0_TID);
2697aa5b4fbcSMichal Kazior 	peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
2698aa5b4fbcSMichal Kazior 
26997aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
2700aa5b4fbcSMichal Kazior 		   "htt rx delba tid %hu peer_id %hu\n",
2701aa5b4fbcSMichal Kazior 		   tid, peer_id);
2702aa5b4fbcSMichal Kazior 
2703aa5b4fbcSMichal Kazior 	spin_lock_bh(&ar->data_lock);
2704aa5b4fbcSMichal Kazior 	peer = ath10k_peer_find_by_id(ar, peer_id);
2705aa5b4fbcSMichal Kazior 	if (!peer) {
27067aa7a72aSMichal Kazior 		ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
2707aa5b4fbcSMichal Kazior 			    peer_id);
2708aa5b4fbcSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
2709aa5b4fbcSMichal Kazior 		return;
2710aa5b4fbcSMichal Kazior 	}
2711aa5b4fbcSMichal Kazior 
2712aa5b4fbcSMichal Kazior 	arvif = ath10k_get_arvif(ar, peer->vdev_id);
2713aa5b4fbcSMichal Kazior 	if (!arvif) {
27147aa7a72aSMichal Kazior 		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
2715aa5b4fbcSMichal Kazior 			    peer->vdev_id);
2716aa5b4fbcSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
2717aa5b4fbcSMichal Kazior 		return;
2718aa5b4fbcSMichal Kazior 	}
2719aa5b4fbcSMichal Kazior 
27207aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
2721aa5b4fbcSMichal Kazior 		   "htt rx stop rx ba session sta %pM tid %hu\n",
2722aa5b4fbcSMichal Kazior 		   peer->addr, tid);
2723aa5b4fbcSMichal Kazior 
2724aa5b4fbcSMichal Kazior 	ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
2725aa5b4fbcSMichal Kazior 	spin_unlock_bh(&ar->data_lock);
2726aa5b4fbcSMichal Kazior }
2727aa5b4fbcSMichal Kazior 
2728c545070eSMichal Kazior static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
2729e48e9c42SKalle Valo 				       struct sk_buff_head *amsdu)
2730c545070eSMichal Kazior {
2731c545070eSMichal Kazior 	struct sk_buff *msdu;
2732c545070eSMichal Kazior 	struct htt_rx_desc *rxd;
2733c545070eSMichal Kazior 
2734c545070eSMichal Kazior 	if (skb_queue_empty(list))
2735c545070eSMichal Kazior 		return -ENOBUFS;
2736c545070eSMichal Kazior 
2737c545070eSMichal Kazior 	if (WARN_ON(!skb_queue_empty(amsdu)))
2738c545070eSMichal Kazior 		return -EINVAL;
2739c545070eSMichal Kazior 
2740e48e9c42SKalle Valo 	while ((msdu = __skb_dequeue(list))) {
2741c545070eSMichal Kazior 		__skb_queue_tail(amsdu, msdu);
2742c545070eSMichal Kazior 
2743c545070eSMichal Kazior 		rxd = (void *)msdu->data - sizeof(*rxd);
27441f5dbfbbSPeter Oh 		if (rxd->msdu_end.common.info0 &
2745c545070eSMichal Kazior 		    __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
2746c545070eSMichal Kazior 			break;
2747c545070eSMichal Kazior 	}
2748c545070eSMichal Kazior 
2749c545070eSMichal Kazior 	msdu = skb_peek_tail(amsdu);
2750c545070eSMichal Kazior 	rxd = (void *)msdu->data - sizeof(*rxd);
27511f5dbfbbSPeter Oh 	if (!(rxd->msdu_end.common.info0 &
2752c545070eSMichal Kazior 	      __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
2753c545070eSMichal Kazior 		skb_queue_splice_init(amsdu, list);
2754c545070eSMichal Kazior 		return -EAGAIN;
2755c545070eSMichal Kazior 	}
2756c545070eSMichal Kazior 
2757c545070eSMichal Kazior 	return 0;
2758c545070eSMichal Kazior }
2759c545070eSMichal Kazior 
2760c545070eSMichal Kazior static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
2761c545070eSMichal Kazior 					    struct sk_buff *skb)
2762c545070eSMichal Kazior {
2763c545070eSMichal Kazior 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2764c545070eSMichal Kazior 
2765c545070eSMichal Kazior 	if (!ieee80211_has_protected(hdr->frame_control))
2766c545070eSMichal Kazior 		return;
2767c545070eSMichal Kazior 
2768c545070eSMichal Kazior 	/* Offloaded frames are already decrypted but firmware insists they are
2769c545070eSMichal Kazior 	 * protected in the 802.11 header. Strip the flag.  Otherwise mac80211
2770c545070eSMichal Kazior 	 * will drop the frame.
2771c545070eSMichal Kazior 	 */
2772c545070eSMichal Kazior 
2773c545070eSMichal Kazior 	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
2774c545070eSMichal Kazior 	status->flag |= RX_FLAG_DECRYPTED |
2775c545070eSMichal Kazior 			RX_FLAG_IV_STRIPPED |
2776c545070eSMichal Kazior 			RX_FLAG_MMIC_STRIPPED;
2777c545070eSMichal Kazior }
2778c545070eSMichal Kazior 
2779deba1b9eSRajkumar Manoharan static void ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
2780c545070eSMichal Kazior 				       struct sk_buff_head *list)
2781c545070eSMichal Kazior {
2782c545070eSMichal Kazior 	struct ath10k_htt *htt = &ar->htt;
2783c545070eSMichal Kazior 	struct ieee80211_rx_status *status = &htt->rx_status;
2784c545070eSMichal Kazior 	struct htt_rx_offload_msdu *rx;
2785c545070eSMichal Kazior 	struct sk_buff *msdu;
2786c545070eSMichal Kazior 	size_t offset;
2787c545070eSMichal Kazior 
2788c545070eSMichal Kazior 	while ((msdu = __skb_dequeue(list))) {
2789c545070eSMichal Kazior 		/* Offloaded frames don't have Rx descriptor. Instead they have
2790c545070eSMichal Kazior 		 * a short meta information header.
2791c545070eSMichal Kazior 		 */
2792c545070eSMichal Kazior 
2793c545070eSMichal Kazior 		rx = (void *)msdu->data;
2794c545070eSMichal Kazior 
2795c545070eSMichal Kazior 		skb_put(msdu, sizeof(*rx));
2796c545070eSMichal Kazior 		skb_pull(msdu, sizeof(*rx));
2797c545070eSMichal Kazior 
2798c545070eSMichal Kazior 		if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
2799c545070eSMichal Kazior 			ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
2800c545070eSMichal Kazior 			dev_kfree_skb_any(msdu);
2801c545070eSMichal Kazior 			continue;
2802c545070eSMichal Kazior 		}
2803c545070eSMichal Kazior 
2804c545070eSMichal Kazior 		skb_put(msdu, __le16_to_cpu(rx->msdu_len));
2805c545070eSMichal Kazior 
2806c545070eSMichal Kazior 		/* Offloaded rx header length isn't multiple of 2 nor 4 so the
2807c545070eSMichal Kazior 		 * actual payload is unaligned. Align the frame.  Otherwise
2808c545070eSMichal Kazior 		 * mac80211 complains.  This shouldn't reduce performance much
2809c545070eSMichal Kazior 		 * because these offloaded frames are rare.
2810c545070eSMichal Kazior 		 */
2811c545070eSMichal Kazior 		offset = 4 - ((unsigned long)msdu->data & 3);
2812c545070eSMichal Kazior 		skb_put(msdu, offset);
2813c545070eSMichal Kazior 		memmove(msdu->data + offset, msdu->data, msdu->len);
2814c545070eSMichal Kazior 		skb_pull(msdu, offset);
2815c545070eSMichal Kazior 
2816c545070eSMichal Kazior 		/* FIXME: The frame is NWifi. Re-construct QoS Control
2817c545070eSMichal Kazior 		 * if possible later.
2818c545070eSMichal Kazior 		 */
2819c545070eSMichal Kazior 
2820c545070eSMichal Kazior 		memset(status, 0, sizeof(*status));
2821c545070eSMichal Kazior 		status->flag |= RX_FLAG_NO_SIGNAL_VAL;
2822c545070eSMichal Kazior 
2823c545070eSMichal Kazior 		ath10k_htt_rx_h_rx_offload_prot(status, msdu);
2824500ff9f9SMichal Kazior 		ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
2825deba1b9eSRajkumar Manoharan 		ath10k_htt_rx_h_queue_msdu(ar, status, msdu);
2826c545070eSMichal Kazior 	}
2827c545070eSMichal Kazior }
2828c545070eSMichal Kazior 
2829e48e9c42SKalle Valo static int ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
2830c545070eSMichal Kazior {
2831c545070eSMichal Kazior 	struct ath10k_htt *htt = &ar->htt;
2832c545070eSMichal Kazior 	struct htt_resp *resp = (void *)skb->data;
2833c545070eSMichal Kazior 	struct ieee80211_rx_status *status = &htt->rx_status;
2834c545070eSMichal Kazior 	struct sk_buff_head list;
2835c545070eSMichal Kazior 	struct sk_buff_head amsdu;
2836c545070eSMichal Kazior 	u16 peer_id;
2837c545070eSMichal Kazior 	u16 msdu_count;
2838c545070eSMichal Kazior 	u8 vdev_id;
2839c545070eSMichal Kazior 	u8 tid;
2840c545070eSMichal Kazior 	bool offload;
2841c545070eSMichal Kazior 	bool frag;
2842deba1b9eSRajkumar Manoharan 	int ret;
2843c545070eSMichal Kazior 
2844c545070eSMichal Kazior 	lockdep_assert_held(&htt->rx_ring.lock);
2845c545070eSMichal Kazior 
2846c545070eSMichal Kazior 	if (htt->rx_confused)
28473c97f5deSRajkumar Manoharan 		return -EIO;
2848c545070eSMichal Kazior 
2849c545070eSMichal Kazior 	skb_pull(skb, sizeof(resp->hdr));
2850c545070eSMichal Kazior 	skb_pull(skb, sizeof(resp->rx_in_ord_ind));
2851c545070eSMichal Kazior 
2852c545070eSMichal Kazior 	peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
2853c545070eSMichal Kazior 	msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
2854c545070eSMichal Kazior 	vdev_id = resp->rx_in_ord_ind.vdev_id;
2855c545070eSMichal Kazior 	tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
2856c545070eSMichal Kazior 	offload = !!(resp->rx_in_ord_ind.info &
2857c545070eSMichal Kazior 			HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
2858c545070eSMichal Kazior 	frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);
2859c545070eSMichal Kazior 
2860c545070eSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
2861c545070eSMichal Kazior 		   "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
2862c545070eSMichal Kazior 		   vdev_id, peer_id, tid, offload, frag, msdu_count);
2863c545070eSMichal Kazior 
28643b0b55b1SGovind Singh 	if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs32)) {
2865c545070eSMichal Kazior 		ath10k_warn(ar, "dropping invalid in order rx indication\n");
28663c97f5deSRajkumar Manoharan 		return -EINVAL;
2867c545070eSMichal Kazior 	}
2868c545070eSMichal Kazior 
2869c545070eSMichal Kazior 	/* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
2870c545070eSMichal Kazior 	 * extracted and processed.
2871c545070eSMichal Kazior 	 */
2872c545070eSMichal Kazior 	__skb_queue_head_init(&list);
28733b0b55b1SGovind Singh 	if (ar->hw_params.target_64bit)
28743b0b55b1SGovind Singh 		ret = ath10k_htt_rx_pop_paddr64_list(htt, &resp->rx_in_ord_ind,
28753b0b55b1SGovind Singh 						     &list);
28763b0b55b1SGovind Singh 	else
28773b0b55b1SGovind Singh 		ret = ath10k_htt_rx_pop_paddr32_list(htt, &resp->rx_in_ord_ind,
28783b0b55b1SGovind Singh 						     &list);
28793b0b55b1SGovind Singh 
2880c545070eSMichal Kazior 	if (ret < 0) {
2881c545070eSMichal Kazior 		ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
2882c545070eSMichal Kazior 		htt->rx_confused = true;
28833c97f5deSRajkumar Manoharan 		return -EIO;
2884c545070eSMichal Kazior 	}
2885c545070eSMichal Kazior 
2886c545070eSMichal Kazior 	/* Offloaded frames are very different and need to be handled
2887c545070eSMichal Kazior 	 * separately.
2888c545070eSMichal Kazior 	 */
2889c545070eSMichal Kazior 	if (offload)
2890deba1b9eSRajkumar Manoharan 		ath10k_htt_rx_h_rx_offload(ar, &list);
2891c545070eSMichal Kazior 
2892e48e9c42SKalle Valo 	while (!skb_queue_empty(&list)) {
2893c545070eSMichal Kazior 		__skb_queue_head_init(&amsdu);
2894e48e9c42SKalle Valo 		ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
2895c545070eSMichal Kazior 		switch (ret) {
2896c545070eSMichal Kazior 		case 0:
2897c545070eSMichal Kazior 			/* Note: The in-order indication may report interleaved
2898c545070eSMichal Kazior 			 * frames from different PPDUs meaning reported rx rate
2899c545070eSMichal Kazior 			 * to mac80211 isn't accurate/reliable. It's still
2900c545070eSMichal Kazior 			 * better to report something than nothing though. This
2901c545070eSMichal Kazior 			 * should still give an idea about rx rate to the user.
2902c545070eSMichal Kazior 			 */
2903500ff9f9SMichal Kazior 			ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
2904caee728aSVasanthakumar Thiagarajan 			ath10k_htt_rx_h_filter(ar, &amsdu, status, NULL);
2905caee728aSVasanthakumar Thiagarajan 			ath10k_htt_rx_h_mpdu(ar, &amsdu, status, false, NULL,
2906caee728aSVasanthakumar Thiagarajan 					     NULL);
2907deba1b9eSRajkumar Manoharan 			ath10k_htt_rx_h_enqueue(ar, &amsdu, status);
2908c545070eSMichal Kazior 			break;
2909c545070eSMichal Kazior 		case -EAGAIN:
2910c545070eSMichal Kazior 			/* fall through */
2911c545070eSMichal Kazior 		default:
2912c545070eSMichal Kazior 			/* Should not happen. */
2913c545070eSMichal Kazior 			ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
2914c545070eSMichal Kazior 			htt->rx_confused = true;
2915c545070eSMichal Kazior 			__skb_queue_purge(&list);
29163c97f5deSRajkumar Manoharan 			return -EIO;
2917c545070eSMichal Kazior 		}
2918c545070eSMichal Kazior 	}
2919deba1b9eSRajkumar Manoharan 	return ret;
2920c545070eSMichal Kazior }
2921c545070eSMichal Kazior 
2922839ae637SMichal Kazior static void ath10k_htt_rx_tx_fetch_resp_id_confirm(struct ath10k *ar,
2923839ae637SMichal Kazior 						   const __le32 *resp_ids,
2924839ae637SMichal Kazior 						   int num_resp_ids)
2925839ae637SMichal Kazior {
2926839ae637SMichal Kazior 	int i;
2927839ae637SMichal Kazior 	u32 resp_id;
2928839ae637SMichal Kazior 
2929839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm num_resp_ids %d\n",
2930839ae637SMichal Kazior 		   num_resp_ids);
2931839ae637SMichal Kazior 
2932839ae637SMichal Kazior 	for (i = 0; i < num_resp_ids; i++) {
2933839ae637SMichal Kazior 		resp_id = le32_to_cpu(resp_ids[i]);
2934839ae637SMichal Kazior 
2935839ae637SMichal Kazior 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm resp_id %u\n",
2936839ae637SMichal Kazior 			   resp_id);
2937839ae637SMichal Kazior 
2938839ae637SMichal Kazior 		/* TODO: free resp_id */
2939839ae637SMichal Kazior 	}
2940839ae637SMichal Kazior }
2941839ae637SMichal Kazior 
2942839ae637SMichal Kazior static void ath10k_htt_rx_tx_fetch_ind(struct ath10k *ar, struct sk_buff *skb)
2943839ae637SMichal Kazior {
2944426e10eaSMichal Kazior 	struct ieee80211_hw *hw = ar->hw;
2945426e10eaSMichal Kazior 	struct ieee80211_txq *txq;
2946839ae637SMichal Kazior 	struct htt_resp *resp = (struct htt_resp *)skb->data;
2947839ae637SMichal Kazior 	struct htt_tx_fetch_record *record;
2948839ae637SMichal Kazior 	size_t len;
2949839ae637SMichal Kazior 	size_t max_num_bytes;
2950839ae637SMichal Kazior 	size_t max_num_msdus;
2951426e10eaSMichal Kazior 	size_t num_bytes;
2952426e10eaSMichal Kazior 	size_t num_msdus;
2953839ae637SMichal Kazior 	const __le32 *resp_ids;
2954839ae637SMichal Kazior 	u16 num_records;
2955839ae637SMichal Kazior 	u16 num_resp_ids;
2956839ae637SMichal Kazior 	u16 peer_id;
2957839ae637SMichal Kazior 	u8 tid;
2958426e10eaSMichal Kazior 	int ret;
2959839ae637SMichal Kazior 	int i;
2960bb2edb73SToke Høiland-Jørgensen 	bool may_tx;
2961839ae637SMichal Kazior 
2962839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind\n");
2963839ae637SMichal Kazior 
2964839ae637SMichal Kazior 	len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_ind);
2965839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
2966839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_fetch_ind event: buffer too short\n");
2967839ae637SMichal Kazior 		return;
2968839ae637SMichal Kazior 	}
2969839ae637SMichal Kazior 
2970839ae637SMichal Kazior 	num_records = le16_to_cpu(resp->tx_fetch_ind.num_records);
2971839ae637SMichal Kazior 	num_resp_ids = le16_to_cpu(resp->tx_fetch_ind.num_resp_ids);
2972839ae637SMichal Kazior 
2973839ae637SMichal Kazior 	len += sizeof(resp->tx_fetch_ind.records[0]) * num_records;
2974839ae637SMichal Kazior 	len += sizeof(resp->tx_fetch_ind.resp_ids[0]) * num_resp_ids;
2975839ae637SMichal Kazior 
2976839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
2977839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_fetch_ind event: too many records/resp_ids\n");
2978839ae637SMichal Kazior 		return;
2979839ae637SMichal Kazior 	}
2980839ae637SMichal Kazior 
2981839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind num records %hu num resps %hu seq %hu\n",
2982839ae637SMichal Kazior 		   num_records, num_resp_ids,
2983839ae637SMichal Kazior 		   le16_to_cpu(resp->tx_fetch_ind.fetch_seq_num));
2984839ae637SMichal Kazior 
2985426e10eaSMichal Kazior 	if (!ar->htt.tx_q_state.enabled) {
2986426e10eaSMichal Kazior 		ath10k_warn(ar, "received unexpected tx_fetch_ind event: not enabled\n");
2987426e10eaSMichal Kazior 		return;
2988426e10eaSMichal Kazior 	}
2989426e10eaSMichal Kazior 
2990426e10eaSMichal Kazior 	if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH) {
2991426e10eaSMichal Kazior 		ath10k_warn(ar, "received unexpected tx_fetch_ind event: in push mode\n");
2992426e10eaSMichal Kazior 		return;
2993426e10eaSMichal Kazior 	}
2994426e10eaSMichal Kazior 
2995426e10eaSMichal Kazior 	rcu_read_lock();
2996839ae637SMichal Kazior 
2997839ae637SMichal Kazior 	for (i = 0; i < num_records; i++) {
2998839ae637SMichal Kazior 		record = &resp->tx_fetch_ind.records[i];
2999839ae637SMichal Kazior 		peer_id = MS(le16_to_cpu(record->info),
3000839ae637SMichal Kazior 			     HTT_TX_FETCH_RECORD_INFO_PEER_ID);
3001839ae637SMichal Kazior 		tid = MS(le16_to_cpu(record->info),
3002839ae637SMichal Kazior 			 HTT_TX_FETCH_RECORD_INFO_TID);
3003839ae637SMichal Kazior 		max_num_msdus = le16_to_cpu(record->num_msdus);
3004839ae637SMichal Kazior 		max_num_bytes = le32_to_cpu(record->num_bytes);
3005839ae637SMichal Kazior 
3006839ae637SMichal Kazior 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch record %i peer_id %hu tid %hhu msdus %zu bytes %zu\n",
3007839ae637SMichal Kazior 			   i, peer_id, tid, max_num_msdus, max_num_bytes);
3008839ae637SMichal Kazior 
3009839ae637SMichal Kazior 		if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
3010839ae637SMichal Kazior 		    unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
3011839ae637SMichal Kazior 			ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
3012839ae637SMichal Kazior 				    peer_id, tid);
3013839ae637SMichal Kazior 			continue;
3014839ae637SMichal Kazior 		}
3015839ae637SMichal Kazior 
3016426e10eaSMichal Kazior 		spin_lock_bh(&ar->data_lock);
3017426e10eaSMichal Kazior 		txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
3018426e10eaSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
3019426e10eaSMichal Kazior 
3020426e10eaSMichal Kazior 		/* It is okay to release the lock and use txq because RCU read
3021426e10eaSMichal Kazior 		 * lock is held.
3022426e10eaSMichal Kazior 		 */
3023426e10eaSMichal Kazior 
3024426e10eaSMichal Kazior 		if (unlikely(!txq)) {
3025426e10eaSMichal Kazior 			ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
3026426e10eaSMichal Kazior 				    peer_id, tid);
3027426e10eaSMichal Kazior 			continue;
3028839ae637SMichal Kazior 		}
3029839ae637SMichal Kazior 
3030426e10eaSMichal Kazior 		num_msdus = 0;
3031426e10eaSMichal Kazior 		num_bytes = 0;
3032426e10eaSMichal Kazior 
3033bb2edb73SToke Høiland-Jørgensen 		ieee80211_txq_schedule_start(hw, txq->ac);
3034bb2edb73SToke Høiland-Jørgensen 		may_tx = ieee80211_txq_may_transmit(hw, txq);
3035426e10eaSMichal Kazior 		while (num_msdus < max_num_msdus &&
3036426e10eaSMichal Kazior 		       num_bytes < max_num_bytes) {
3037bb2edb73SToke Høiland-Jørgensen 			if (!may_tx)
3038bb2edb73SToke Høiland-Jørgensen 				break;
3039bb2edb73SToke Høiland-Jørgensen 
3040426e10eaSMichal Kazior 			ret = ath10k_mac_tx_push_txq(hw, txq);
3041426e10eaSMichal Kazior 			if (ret < 0)
3042426e10eaSMichal Kazior 				break;
3043426e10eaSMichal Kazior 
3044426e10eaSMichal Kazior 			num_msdus++;
3045426e10eaSMichal Kazior 			num_bytes += ret;
3046426e10eaSMichal Kazior 		}
30472b4a6698SFelix Fietkau 		ieee80211_return_txq(hw, txq, false);
3048bb2edb73SToke Høiland-Jørgensen 		ieee80211_txq_schedule_end(hw, txq->ac);
3049426e10eaSMichal Kazior 
3050426e10eaSMichal Kazior 		record->num_msdus = cpu_to_le16(num_msdus);
3051426e10eaSMichal Kazior 		record->num_bytes = cpu_to_le32(num_bytes);
3052426e10eaSMichal Kazior 
3053426e10eaSMichal Kazior 		ath10k_htt_tx_txq_recalc(hw, txq);
3054426e10eaSMichal Kazior 	}
3055426e10eaSMichal Kazior 
3056426e10eaSMichal Kazior 	rcu_read_unlock();
3057426e10eaSMichal Kazior 
3058839ae637SMichal Kazior 	resp_ids = ath10k_htt_get_tx_fetch_ind_resp_ids(&resp->tx_fetch_ind);
3059839ae637SMichal Kazior 	ath10k_htt_rx_tx_fetch_resp_id_confirm(ar, resp_ids, num_resp_ids);
3060839ae637SMichal Kazior 
3061426e10eaSMichal Kazior 	ret = ath10k_htt_tx_fetch_resp(ar,
3062426e10eaSMichal Kazior 				       resp->tx_fetch_ind.token,
3063426e10eaSMichal Kazior 				       resp->tx_fetch_ind.fetch_seq_num,
3064426e10eaSMichal Kazior 				       resp->tx_fetch_ind.records,
3065426e10eaSMichal Kazior 				       num_records);
3066426e10eaSMichal Kazior 	if (unlikely(ret)) {
3067426e10eaSMichal Kazior 		ath10k_warn(ar, "failed to submit tx fetch resp for token 0x%08x: %d\n",
3068426e10eaSMichal Kazior 			    le32_to_cpu(resp->tx_fetch_ind.token), ret);
3069426e10eaSMichal Kazior 		/* FIXME: request fw restart */
3070426e10eaSMichal Kazior 	}
3071426e10eaSMichal Kazior 
3072426e10eaSMichal Kazior 	ath10k_htt_tx_txq_sync(ar);
3073839ae637SMichal Kazior }
3074839ae637SMichal Kazior 
3075839ae637SMichal Kazior static void ath10k_htt_rx_tx_fetch_confirm(struct ath10k *ar,
3076839ae637SMichal Kazior 					   struct sk_buff *skb)
3077839ae637SMichal Kazior {
3078839ae637SMichal Kazior 	const struct htt_resp *resp = (void *)skb->data;
3079839ae637SMichal Kazior 	size_t len;
3080839ae637SMichal Kazior 	int num_resp_ids;
3081839ae637SMichal Kazior 
3082839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm\n");
3083839ae637SMichal Kazior 
3084839ae637SMichal Kazior 	len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_confirm);
3085839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
3086839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_fetch_confirm event: buffer too short\n");
3087839ae637SMichal Kazior 		return;
3088839ae637SMichal Kazior 	}
3089839ae637SMichal Kazior 
3090839ae637SMichal Kazior 	num_resp_ids = le16_to_cpu(resp->tx_fetch_confirm.num_resp_ids);
3091839ae637SMichal Kazior 	len += sizeof(resp->tx_fetch_confirm.resp_ids[0]) * num_resp_ids;
3092839ae637SMichal Kazior 
3093839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
3094839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_fetch_confirm event: resp_ids buffer overflow\n");
3095839ae637SMichal Kazior 		return;
3096839ae637SMichal Kazior 	}
3097839ae637SMichal Kazior 
3098839ae637SMichal Kazior 	ath10k_htt_rx_tx_fetch_resp_id_confirm(ar,
3099839ae637SMichal Kazior 					       resp->tx_fetch_confirm.resp_ids,
3100839ae637SMichal Kazior 					       num_resp_ids);
3101839ae637SMichal Kazior }
3102839ae637SMichal Kazior 
3103839ae637SMichal Kazior static void ath10k_htt_rx_tx_mode_switch_ind(struct ath10k *ar,
3104839ae637SMichal Kazior 					     struct sk_buff *skb)
3105839ae637SMichal Kazior {
3106839ae637SMichal Kazior 	const struct htt_resp *resp = (void *)skb->data;
3107839ae637SMichal Kazior 	const struct htt_tx_mode_switch_record *record;
3108426e10eaSMichal Kazior 	struct ieee80211_txq *txq;
3109426e10eaSMichal Kazior 	struct ath10k_txq *artxq;
3110839ae637SMichal Kazior 	size_t len;
3111839ae637SMichal Kazior 	size_t num_records;
3112839ae637SMichal Kazior 	enum htt_tx_mode_switch_mode mode;
3113839ae637SMichal Kazior 	bool enable;
3114839ae637SMichal Kazior 	u16 info0;
3115839ae637SMichal Kazior 	u16 info1;
3116839ae637SMichal Kazior 	u16 threshold;
3117839ae637SMichal Kazior 	u16 peer_id;
3118839ae637SMichal Kazior 	u8 tid;
3119839ae637SMichal Kazior 	int i;
3120839ae637SMichal Kazior 
3121839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx mode switch ind\n");
3122839ae637SMichal Kazior 
3123839ae637SMichal Kazior 	len = sizeof(resp->hdr) + sizeof(resp->tx_mode_switch_ind);
3124839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
3125839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_mode_switch_ind event: buffer too short\n");
3126839ae637SMichal Kazior 		return;
3127839ae637SMichal Kazior 	}
3128839ae637SMichal Kazior 
3129839ae637SMichal Kazior 	info0 = le16_to_cpu(resp->tx_mode_switch_ind.info0);
3130839ae637SMichal Kazior 	info1 = le16_to_cpu(resp->tx_mode_switch_ind.info1);
3131839ae637SMichal Kazior 
3132839ae637SMichal Kazior 	enable = !!(info0 & HTT_TX_MODE_SWITCH_IND_INFO0_ENABLE);
3133839ae637SMichal Kazior 	num_records = MS(info0, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
3134839ae637SMichal Kazior 	mode = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_MODE);
3135839ae637SMichal Kazior 	threshold = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
3136839ae637SMichal Kazior 
3137839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
3138839ae637SMichal Kazior 		   "htt rx tx mode switch ind info0 0x%04hx info1 0x%04hx enable %d num records %zd mode %d threshold %hu\n",
3139839ae637SMichal Kazior 		   info0, info1, enable, num_records, mode, threshold);
3140839ae637SMichal Kazior 
3141839ae637SMichal Kazior 	len += sizeof(resp->tx_mode_switch_ind.records[0]) * num_records;
3142839ae637SMichal Kazior 
3143839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
3144839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_mode_switch_mode_ind event: too many records\n");
3145839ae637SMichal Kazior 		return;
3146839ae637SMichal Kazior 	}
3147839ae637SMichal Kazior 
3148839ae637SMichal Kazior 	switch (mode) {
3149839ae637SMichal Kazior 	case HTT_TX_MODE_SWITCH_PUSH:
3150839ae637SMichal Kazior 	case HTT_TX_MODE_SWITCH_PUSH_PULL:
3151839ae637SMichal Kazior 		break;
3152839ae637SMichal Kazior 	default:
3153839ae637SMichal Kazior 		ath10k_warn(ar, "received invalid tx_mode_switch_mode_ind mode %d, ignoring\n",
3154839ae637SMichal Kazior 			    mode);
3155839ae637SMichal Kazior 		return;
3156839ae637SMichal Kazior 	}
3157839ae637SMichal Kazior 
3158839ae637SMichal Kazior 	if (!enable)
3159839ae637SMichal Kazior 		return;
3160839ae637SMichal Kazior 
3161426e10eaSMichal Kazior 	ar->htt.tx_q_state.enabled = enable;
3162426e10eaSMichal Kazior 	ar->htt.tx_q_state.mode = mode;
3163426e10eaSMichal Kazior 	ar->htt.tx_q_state.num_push_allowed = threshold;
3164426e10eaSMichal Kazior 
3165426e10eaSMichal Kazior 	rcu_read_lock();
3166839ae637SMichal Kazior 
3167839ae637SMichal Kazior 	for (i = 0; i < num_records; i++) {
3168839ae637SMichal Kazior 		record = &resp->tx_mode_switch_ind.records[i];
3169839ae637SMichal Kazior 		info0 = le16_to_cpu(record->info0);
3170839ae637SMichal Kazior 		peer_id = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_PEER_ID);
3171839ae637SMichal Kazior 		tid = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_TID);
3172839ae637SMichal Kazior 
3173839ae637SMichal Kazior 		if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
3174839ae637SMichal Kazior 		    unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
3175839ae637SMichal Kazior 			ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
3176839ae637SMichal Kazior 				    peer_id, tid);
3177839ae637SMichal Kazior 			continue;
3178839ae637SMichal Kazior 		}
3179839ae637SMichal Kazior 
3180426e10eaSMichal Kazior 		spin_lock_bh(&ar->data_lock);
3181426e10eaSMichal Kazior 		txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
3182426e10eaSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
3183426e10eaSMichal Kazior 
3184426e10eaSMichal Kazior 		/* It is okay to release the lock and use txq because RCU read
3185426e10eaSMichal Kazior 		 * lock is held.
3186426e10eaSMichal Kazior 		 */
3187426e10eaSMichal Kazior 
3188426e10eaSMichal Kazior 		if (unlikely(!txq)) {
3189426e10eaSMichal Kazior 			ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
3190426e10eaSMichal Kazior 				    peer_id, tid);
3191426e10eaSMichal Kazior 			continue;
3192839ae637SMichal Kazior 		}
3193839ae637SMichal Kazior 
3194426e10eaSMichal Kazior 		spin_lock_bh(&ar->htt.tx_lock);
3195426e10eaSMichal Kazior 		artxq = (void *)txq->drv_priv;
3196426e10eaSMichal Kazior 		artxq->num_push_allowed = le16_to_cpu(record->num_max_msdus);
3197426e10eaSMichal Kazior 		spin_unlock_bh(&ar->htt.tx_lock);
3198426e10eaSMichal Kazior 	}
3199426e10eaSMichal Kazior 
3200426e10eaSMichal Kazior 	rcu_read_unlock();
3201426e10eaSMichal Kazior 
3202426e10eaSMichal Kazior 	ath10k_mac_tx_push_pending(ar);
3203839ae637SMichal Kazior }
3204839ae637SMichal Kazior 
3205e3a91f87SRajkumar Manoharan void ath10k_htt_htc_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
3206e3a91f87SRajkumar Manoharan {
3207e3a91f87SRajkumar Manoharan 	bool release;
3208e3a91f87SRajkumar Manoharan 
3209e3a91f87SRajkumar Manoharan 	release = ath10k_htt_t2h_msg_handler(ar, skb);
3210e3a91f87SRajkumar Manoharan 
3211e3a91f87SRajkumar Manoharan 	/* Free the indication buffer */
3212e3a91f87SRajkumar Manoharan 	if (release)
3213e3a91f87SRajkumar Manoharan 		dev_kfree_skb_any(skb);
3214e3a91f87SRajkumar Manoharan }
3215e3a91f87SRajkumar Manoharan 
32169a9cf0e6SAnilkumar Kolli static inline s8 ath10k_get_legacy_rate_idx(struct ath10k *ar, u8 rate)
3217cec17c38SAnilkumar Kolli {
3218cec17c38SAnilkumar Kolli 	static const u8 legacy_rates[] = {1, 2, 5, 11, 6, 9, 12,
3219cec17c38SAnilkumar Kolli 					  18, 24, 36, 48, 54};
3220cec17c38SAnilkumar Kolli 	int i;
3221cec17c38SAnilkumar Kolli 
3222cec17c38SAnilkumar Kolli 	for (i = 0; i < ARRAY_SIZE(legacy_rates); i++) {
3223cec17c38SAnilkumar Kolli 		if (rate == legacy_rates[i])
32240189dbd7SAnilkumar Kolli 			return i;
3225cec17c38SAnilkumar Kolli 	}
3226cec17c38SAnilkumar Kolli 
32270189dbd7SAnilkumar Kolli 	ath10k_warn(ar, "Invalid legacy rate %hhd peer stats", rate);
32280189dbd7SAnilkumar Kolli 	return -EINVAL;
3229cec17c38SAnilkumar Kolli }
3230cec17c38SAnilkumar Kolli 
3231cec17c38SAnilkumar Kolli static void
3232a904417fSAnilkumar Kolli ath10k_accumulate_per_peer_tx_stats(struct ath10k *ar,
3233a904417fSAnilkumar Kolli 				    struct ath10k_sta *arsta,
3234a904417fSAnilkumar Kolli 				    struct ath10k_per_peer_tx_stats *pstats,
32359a9cf0e6SAnilkumar Kolli 				    s8 legacy_rate_idx)
3236a904417fSAnilkumar Kolli {
3237a904417fSAnilkumar Kolli 	struct rate_info *txrate = &arsta->txrate;
3238a904417fSAnilkumar Kolli 	struct ath10k_htt_tx_stats *tx_stats;
3239e88975caSAnilkumar Kolli 	int idx, ht_idx, gi, mcs, bw, nss;
32408e55fdaaSSurabhi Vishnoi 	unsigned long flags;
3241a904417fSAnilkumar Kolli 
3242a904417fSAnilkumar Kolli 	if (!arsta->tx_stats)
3243a904417fSAnilkumar Kolli 		return;
3244a904417fSAnilkumar Kolli 
3245a904417fSAnilkumar Kolli 	tx_stats = arsta->tx_stats;
32468e55fdaaSSurabhi Vishnoi 	flags = txrate->flags;
32478e55fdaaSSurabhi Vishnoi 	gi = test_bit(ATH10K_RATE_INFO_FLAGS_SGI_BIT, &flags);
3248d23c2cdaSSurabhi Vishnoi 	mcs = ATH10K_HW_MCS_RATE(pstats->ratecode);
3249a904417fSAnilkumar Kolli 	bw = txrate->bw;
3250a904417fSAnilkumar Kolli 	nss = txrate->nss;
3251d23c2cdaSSurabhi Vishnoi 	ht_idx = mcs + (nss - 1) * 8;
3252d23c2cdaSSurabhi Vishnoi 	idx = mcs * 8 + 8 * 10 * (nss - 1);
3253e88975caSAnilkumar Kolli 	idx += bw * 2 + gi;
3254a904417fSAnilkumar Kolli 
3255a904417fSAnilkumar Kolli #define STATS_OP_FMT(name) tx_stats->stats[ATH10K_STATS_TYPE_##name]
3256a904417fSAnilkumar Kolli 
32579e0b341aSBrandon Huang 	if (txrate->flags & RATE_INFO_FLAGS_VHT_MCS) {
3258a904417fSAnilkumar Kolli 		STATS_OP_FMT(SUCC).vht[0][mcs] += pstats->succ_bytes;
3259a904417fSAnilkumar Kolli 		STATS_OP_FMT(SUCC).vht[1][mcs] += pstats->succ_pkts;
3260a904417fSAnilkumar Kolli 		STATS_OP_FMT(FAIL).vht[0][mcs] += pstats->failed_bytes;
3261a904417fSAnilkumar Kolli 		STATS_OP_FMT(FAIL).vht[1][mcs] += pstats->failed_pkts;
3262a904417fSAnilkumar Kolli 		STATS_OP_FMT(RETRY).vht[0][mcs] += pstats->retry_bytes;
3263a904417fSAnilkumar Kolli 		STATS_OP_FMT(RETRY).vht[1][mcs] += pstats->retry_pkts;
32649e0b341aSBrandon Huang 	} else if (txrate->flags & RATE_INFO_FLAGS_MCS) {
3265a904417fSAnilkumar Kolli 		STATS_OP_FMT(SUCC).ht[0][ht_idx] += pstats->succ_bytes;
3266a904417fSAnilkumar Kolli 		STATS_OP_FMT(SUCC).ht[1][ht_idx] += pstats->succ_pkts;
3267a904417fSAnilkumar Kolli 		STATS_OP_FMT(FAIL).ht[0][ht_idx] += pstats->failed_bytes;
3268a904417fSAnilkumar Kolli 		STATS_OP_FMT(FAIL).ht[1][ht_idx] += pstats->failed_pkts;
3269a904417fSAnilkumar Kolli 		STATS_OP_FMT(RETRY).ht[0][ht_idx] += pstats->retry_bytes;
3270a904417fSAnilkumar Kolli 		STATS_OP_FMT(RETRY).ht[1][ht_idx] += pstats->retry_pkts;
3271a904417fSAnilkumar Kolli 	} else {
3272a904417fSAnilkumar Kolli 		mcs = legacy_rate_idx;
3273a904417fSAnilkumar Kolli 
3274a904417fSAnilkumar Kolli 		STATS_OP_FMT(SUCC).legacy[0][mcs] += pstats->succ_bytes;
3275a904417fSAnilkumar Kolli 		STATS_OP_FMT(SUCC).legacy[1][mcs] += pstats->succ_pkts;
3276a904417fSAnilkumar Kolli 		STATS_OP_FMT(FAIL).legacy[0][mcs] += pstats->failed_bytes;
3277a904417fSAnilkumar Kolli 		STATS_OP_FMT(FAIL).legacy[1][mcs] += pstats->failed_pkts;
3278a904417fSAnilkumar Kolli 		STATS_OP_FMT(RETRY).legacy[0][mcs] += pstats->retry_bytes;
3279a904417fSAnilkumar Kolli 		STATS_OP_FMT(RETRY).legacy[1][mcs] += pstats->retry_pkts;
3280a904417fSAnilkumar Kolli 	}
3281a904417fSAnilkumar Kolli 
3282a904417fSAnilkumar Kolli 	if (ATH10K_HW_AMPDU(pstats->flags)) {
3283a904417fSAnilkumar Kolli 		tx_stats->ba_fails += ATH10K_HW_BA_FAIL(pstats->flags);
3284a904417fSAnilkumar Kolli 
32859e0b341aSBrandon Huang 		if (txrate->flags & RATE_INFO_FLAGS_MCS) {
3286a904417fSAnilkumar Kolli 			STATS_OP_FMT(AMPDU).ht[0][ht_idx] +=
3287a904417fSAnilkumar Kolli 				pstats->succ_bytes + pstats->retry_bytes;
3288a904417fSAnilkumar Kolli 			STATS_OP_FMT(AMPDU).ht[1][ht_idx] +=
3289a904417fSAnilkumar Kolli 				pstats->succ_pkts + pstats->retry_pkts;
3290a904417fSAnilkumar Kolli 		} else {
3291a904417fSAnilkumar Kolli 			STATS_OP_FMT(AMPDU).vht[0][mcs] +=
3292a904417fSAnilkumar Kolli 				pstats->succ_bytes + pstats->retry_bytes;
3293a904417fSAnilkumar Kolli 			STATS_OP_FMT(AMPDU).vht[1][mcs] +=
3294a904417fSAnilkumar Kolli 				pstats->succ_pkts + pstats->retry_pkts;
3295a904417fSAnilkumar Kolli 		}
3296a904417fSAnilkumar Kolli 		STATS_OP_FMT(AMPDU).bw[0][bw] +=
3297a904417fSAnilkumar Kolli 			pstats->succ_bytes + pstats->retry_bytes;
32983a08ac3eSSurabhi Vishnoi 		STATS_OP_FMT(AMPDU).nss[0][nss - 1] +=
3299a904417fSAnilkumar Kolli 			pstats->succ_bytes + pstats->retry_bytes;
3300a904417fSAnilkumar Kolli 		STATS_OP_FMT(AMPDU).gi[0][gi] +=
3301a904417fSAnilkumar Kolli 			pstats->succ_bytes + pstats->retry_bytes;
3302e88975caSAnilkumar Kolli 		STATS_OP_FMT(AMPDU).rate_table[0][idx] +=
3303e88975caSAnilkumar Kolli 			pstats->succ_bytes + pstats->retry_bytes;
3304a904417fSAnilkumar Kolli 		STATS_OP_FMT(AMPDU).bw[1][bw] +=
3305a904417fSAnilkumar Kolli 			pstats->succ_pkts + pstats->retry_pkts;
33063a08ac3eSSurabhi Vishnoi 		STATS_OP_FMT(AMPDU).nss[1][nss - 1] +=
3307a904417fSAnilkumar Kolli 			pstats->succ_pkts + pstats->retry_pkts;
3308a904417fSAnilkumar Kolli 		STATS_OP_FMT(AMPDU).gi[1][gi] +=
3309a904417fSAnilkumar Kolli 			pstats->succ_pkts + pstats->retry_pkts;
3310e88975caSAnilkumar Kolli 		STATS_OP_FMT(AMPDU).rate_table[1][idx] +=
3311e88975caSAnilkumar Kolli 			pstats->succ_pkts + pstats->retry_pkts;
3312a904417fSAnilkumar Kolli 	} else {
3313a904417fSAnilkumar Kolli 		tx_stats->ack_fails +=
3314a904417fSAnilkumar Kolli 				ATH10K_HW_BA_FAIL(pstats->flags);
3315a904417fSAnilkumar Kolli 	}
3316a904417fSAnilkumar Kolli 
3317a904417fSAnilkumar Kolli 	STATS_OP_FMT(SUCC).bw[0][bw] += pstats->succ_bytes;
33183a08ac3eSSurabhi Vishnoi 	STATS_OP_FMT(SUCC).nss[0][nss - 1] += pstats->succ_bytes;
3319a904417fSAnilkumar Kolli 	STATS_OP_FMT(SUCC).gi[0][gi] += pstats->succ_bytes;
3320a904417fSAnilkumar Kolli 
3321a904417fSAnilkumar Kolli 	STATS_OP_FMT(SUCC).bw[1][bw] += pstats->succ_pkts;
33223a08ac3eSSurabhi Vishnoi 	STATS_OP_FMT(SUCC).nss[1][nss - 1] += pstats->succ_pkts;
3323a904417fSAnilkumar Kolli 	STATS_OP_FMT(SUCC).gi[1][gi] += pstats->succ_pkts;
3324a904417fSAnilkumar Kolli 
3325a904417fSAnilkumar Kolli 	STATS_OP_FMT(FAIL).bw[0][bw] += pstats->failed_bytes;
33263a08ac3eSSurabhi Vishnoi 	STATS_OP_FMT(FAIL).nss[0][nss - 1] += pstats->failed_bytes;
3327a904417fSAnilkumar Kolli 	STATS_OP_FMT(FAIL).gi[0][gi] += pstats->failed_bytes;
3328a904417fSAnilkumar Kolli 
3329a904417fSAnilkumar Kolli 	STATS_OP_FMT(FAIL).bw[1][bw] += pstats->failed_pkts;
33303a08ac3eSSurabhi Vishnoi 	STATS_OP_FMT(FAIL).nss[1][nss - 1] += pstats->failed_pkts;
3331a904417fSAnilkumar Kolli 	STATS_OP_FMT(FAIL).gi[1][gi] += pstats->failed_pkts;
3332a904417fSAnilkumar Kolli 
3333a904417fSAnilkumar Kolli 	STATS_OP_FMT(RETRY).bw[0][bw] += pstats->retry_bytes;
33343a08ac3eSSurabhi Vishnoi 	STATS_OP_FMT(RETRY).nss[0][nss - 1] += pstats->retry_bytes;
3335a904417fSAnilkumar Kolli 	STATS_OP_FMT(RETRY).gi[0][gi] += pstats->retry_bytes;
3336a904417fSAnilkumar Kolli 
3337a904417fSAnilkumar Kolli 	STATS_OP_FMT(RETRY).bw[1][bw] += pstats->retry_pkts;
33383a08ac3eSSurabhi Vishnoi 	STATS_OP_FMT(RETRY).nss[1][nss - 1] += pstats->retry_pkts;
3339a904417fSAnilkumar Kolli 	STATS_OP_FMT(RETRY).gi[1][gi] += pstats->retry_pkts;
3340e88975caSAnilkumar Kolli 
3341e88975caSAnilkumar Kolli 	if (txrate->flags >= RATE_INFO_FLAGS_MCS) {
3342e88975caSAnilkumar Kolli 		STATS_OP_FMT(SUCC).rate_table[0][idx] += pstats->succ_bytes;
3343e88975caSAnilkumar Kolli 		STATS_OP_FMT(SUCC).rate_table[1][idx] += pstats->succ_pkts;
3344e88975caSAnilkumar Kolli 		STATS_OP_FMT(FAIL).rate_table[0][idx] += pstats->failed_bytes;
3345e88975caSAnilkumar Kolli 		STATS_OP_FMT(FAIL).rate_table[1][idx] += pstats->failed_pkts;
3346e88975caSAnilkumar Kolli 		STATS_OP_FMT(RETRY).rate_table[0][idx] += pstats->retry_bytes;
3347e88975caSAnilkumar Kolli 		STATS_OP_FMT(RETRY).rate_table[1][idx] += pstats->retry_pkts;
3348e88975caSAnilkumar Kolli 	}
334905655029SSurabhi Vishnoi 
335005655029SSurabhi Vishnoi 	tx_stats->tx_duration += pstats->duration;
3351cec17c38SAnilkumar Kolli }
3352cec17c38SAnilkumar Kolli 
3353cec17c38SAnilkumar Kolli static void
3354cec17c38SAnilkumar Kolli ath10k_update_per_peer_tx_stats(struct ath10k *ar,
3355cec17c38SAnilkumar Kolli 				struct ieee80211_sta *sta,
3356cec17c38SAnilkumar Kolli 				struct ath10k_per_peer_tx_stats *peer_stats)
3357cec17c38SAnilkumar Kolli {
3358cec17c38SAnilkumar Kolli 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
33599a9cf0e6SAnilkumar Kolli 	struct ieee80211_chanctx_conf *conf = NULL;
33609d9cdbf3SGustavo A. R. Silva 	u8 rate = 0, sgi;
33619d9cdbf3SGustavo A. R. Silva 	s8 rate_idx = 0;
33629a9cf0e6SAnilkumar Kolli 	bool skip_auto_rate;
3363cec17c38SAnilkumar Kolli 	struct rate_info txrate;
3364cec17c38SAnilkumar Kolli 
3365cec17c38SAnilkumar Kolli 	lockdep_assert_held(&ar->data_lock);
3366cec17c38SAnilkumar Kolli 
3367cec17c38SAnilkumar Kolli 	txrate.flags = ATH10K_HW_PREAMBLE(peer_stats->ratecode);
3368cec17c38SAnilkumar Kolli 	txrate.bw = ATH10K_HW_BW(peer_stats->flags);
3369cec17c38SAnilkumar Kolli 	txrate.nss = ATH10K_HW_NSS(peer_stats->ratecode);
3370cec17c38SAnilkumar Kolli 	txrate.mcs = ATH10K_HW_MCS_RATE(peer_stats->ratecode);
3371cec17c38SAnilkumar Kolli 	sgi = ATH10K_HW_GI(peer_stats->flags);
33729a9cf0e6SAnilkumar Kolli 	skip_auto_rate = ATH10K_FW_SKIPPED_RATE_CTRL(peer_stats->flags);
33739a9cf0e6SAnilkumar Kolli 
33749a9cf0e6SAnilkumar Kolli 	/* Firmware's rate control skips broadcast/management frames,
33759a9cf0e6SAnilkumar Kolli 	 * if host has configure fixed rates and in some other special cases.
33769a9cf0e6SAnilkumar Kolli 	 */
33779a9cf0e6SAnilkumar Kolli 	if (skip_auto_rate)
33789a9cf0e6SAnilkumar Kolli 		return;
3379cec17c38SAnilkumar Kolli 
3380c1dd8016SSven Eckelmann 	if (txrate.flags == WMI_RATE_PREAMBLE_VHT && txrate.mcs > 9) {
3381c1dd8016SSven Eckelmann 		ath10k_warn(ar, "Invalid VHT mcs %hhd peer stats",  txrate.mcs);
3382c1dd8016SSven Eckelmann 		return;
3383c1dd8016SSven Eckelmann 	}
3384c1dd8016SSven Eckelmann 
3385c1dd8016SSven Eckelmann 	if (txrate.flags == WMI_RATE_PREAMBLE_HT &&
3386c1dd8016SSven Eckelmann 	    (txrate.mcs > 7 || txrate.nss < 1)) {
3387c1dd8016SSven Eckelmann 		ath10k_warn(ar, "Invalid HT mcs %hhd nss %hhd peer stats",
3388c1dd8016SSven Eckelmann 			    txrate.mcs, txrate.nss);
3389cec17c38SAnilkumar Kolli 		return;
3390cec17c38SAnilkumar Kolli 	}
3391cec17c38SAnilkumar Kolli 
33920f8a2b77SMohammed Shafi Shajakhan 	memset(&arsta->txrate, 0, sizeof(arsta->txrate));
33939a9cf0e6SAnilkumar Kolli 	memset(&arsta->tx_info.status, 0, sizeof(arsta->tx_info.status));
3394cec17c38SAnilkumar Kolli 	if (txrate.flags == WMI_RATE_PREAMBLE_CCK ||
3395cec17c38SAnilkumar Kolli 	    txrate.flags == WMI_RATE_PREAMBLE_OFDM) {
3396cec17c38SAnilkumar Kolli 		rate = ATH10K_HW_LEGACY_RATE(peer_stats->ratecode);
3397cec17c38SAnilkumar Kolli 		/* This is hacky, FW sends CCK rate 5.5Mbps as 6 */
33980189dbd7SAnilkumar Kolli 		if (rate == 6 && txrate.flags == WMI_RATE_PREAMBLE_CCK)
33990189dbd7SAnilkumar Kolli 			rate = 5;
34000189dbd7SAnilkumar Kolli 		rate_idx = ath10k_get_legacy_rate_idx(ar, rate);
34010189dbd7SAnilkumar Kolli 		if (rate_idx < 0)
34020189dbd7SAnilkumar Kolli 			return;
3403cd591027SMohammed Shafi Shajakhan 		arsta->txrate.legacy = rate;
3404cec17c38SAnilkumar Kolli 	} else if (txrate.flags == WMI_RATE_PREAMBLE_HT) {
3405cec17c38SAnilkumar Kolli 		arsta->txrate.flags = RATE_INFO_FLAGS_MCS;
3406c1dd8016SSven Eckelmann 		arsta->txrate.mcs = txrate.mcs + 8 * (txrate.nss - 1);
3407cec17c38SAnilkumar Kolli 	} else {
3408cec17c38SAnilkumar Kolli 		arsta->txrate.flags = RATE_INFO_FLAGS_VHT_MCS;
3409cec17c38SAnilkumar Kolli 		arsta->txrate.mcs = txrate.mcs;
3410cec17c38SAnilkumar Kolli 	}
3411cec17c38SAnilkumar Kolli 
34129a9cf0e6SAnilkumar Kolli 	switch (txrate.flags) {
34139a9cf0e6SAnilkumar Kolli 	case WMI_RATE_PREAMBLE_OFDM:
34149a9cf0e6SAnilkumar Kolli 		if (arsta->arvif && arsta->arvif->vif)
34159a9cf0e6SAnilkumar Kolli 			conf = rcu_dereference(arsta->arvif->vif->chanctx_conf);
34169a9cf0e6SAnilkumar Kolli 		if (conf && conf->def.chan->band == NL80211_BAND_5GHZ)
34179a9cf0e6SAnilkumar Kolli 			arsta->tx_info.status.rates[0].idx = rate_idx - 4;
34189a9cf0e6SAnilkumar Kolli 		break;
34199a9cf0e6SAnilkumar Kolli 	case WMI_RATE_PREAMBLE_CCK:
34209a9cf0e6SAnilkumar Kolli 		arsta->tx_info.status.rates[0].idx = rate_idx;
3421cec17c38SAnilkumar Kolli 		if (sgi)
34229a9cf0e6SAnilkumar Kolli 			arsta->tx_info.status.rates[0].flags |=
34239a9cf0e6SAnilkumar Kolli 				(IEEE80211_TX_RC_USE_SHORT_PREAMBLE |
34249a9cf0e6SAnilkumar Kolli 				 IEEE80211_TX_RC_SHORT_GI);
34259a9cf0e6SAnilkumar Kolli 		break;
34269a9cf0e6SAnilkumar Kolli 	case WMI_RATE_PREAMBLE_HT:
34279a9cf0e6SAnilkumar Kolli 		arsta->tx_info.status.rates[0].idx =
34289a9cf0e6SAnilkumar Kolli 				txrate.mcs + ((txrate.nss - 1) * 8);
34299a9cf0e6SAnilkumar Kolli 		if (sgi)
34309a9cf0e6SAnilkumar Kolli 			arsta->tx_info.status.rates[0].flags |=
34319a9cf0e6SAnilkumar Kolli 					IEEE80211_TX_RC_SHORT_GI;
34329a9cf0e6SAnilkumar Kolli 		arsta->tx_info.status.rates[0].flags |= IEEE80211_TX_RC_MCS;
34339a9cf0e6SAnilkumar Kolli 		break;
34349a9cf0e6SAnilkumar Kolli 	case WMI_RATE_PREAMBLE_VHT:
34359a9cf0e6SAnilkumar Kolli 		ieee80211_rate_set_vht(&arsta->tx_info.status.rates[0],
34369a9cf0e6SAnilkumar Kolli 				       txrate.mcs, txrate.nss);
34379a9cf0e6SAnilkumar Kolli 		if (sgi)
34389a9cf0e6SAnilkumar Kolli 			arsta->tx_info.status.rates[0].flags |=
34399a9cf0e6SAnilkumar Kolli 						IEEE80211_TX_RC_SHORT_GI;
34409a9cf0e6SAnilkumar Kolli 		arsta->tx_info.status.rates[0].flags |= IEEE80211_TX_RC_VHT_MCS;
34419a9cf0e6SAnilkumar Kolli 		break;
34429a9cf0e6SAnilkumar Kolli 	}
3443cec17c38SAnilkumar Kolli 
3444cec17c38SAnilkumar Kolli 	arsta->txrate.nss = txrate.nss;
344591493e8eSChristian Lamparter 	arsta->txrate.bw = ath10k_bw_to_mac80211_bw(txrate.bw);
3446d1ce37b7SKan Yan 	arsta->last_tx_bitrate = cfg80211_calculate_bitrate(&arsta->txrate);
34479a9cf0e6SAnilkumar Kolli 	if (sgi)
34489a9cf0e6SAnilkumar Kolli 		arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
34499a9cf0e6SAnilkumar Kolli 
34509a9cf0e6SAnilkumar Kolli 	switch (arsta->txrate.bw) {
34519a9cf0e6SAnilkumar Kolli 	case RATE_INFO_BW_40:
34529a9cf0e6SAnilkumar Kolli 		arsta->tx_info.status.rates[0].flags |=
34539a9cf0e6SAnilkumar Kolli 				IEEE80211_TX_RC_40_MHZ_WIDTH;
34549a9cf0e6SAnilkumar Kolli 		break;
34559a9cf0e6SAnilkumar Kolli 	case RATE_INFO_BW_80:
34569a9cf0e6SAnilkumar Kolli 		arsta->tx_info.status.rates[0].flags |=
34579a9cf0e6SAnilkumar Kolli 				IEEE80211_TX_RC_80_MHZ_WIDTH;
34589a9cf0e6SAnilkumar Kolli 		break;
34599a9cf0e6SAnilkumar Kolli 	}
34609a9cf0e6SAnilkumar Kolli 
34619a9cf0e6SAnilkumar Kolli 	if (peer_stats->succ_pkts) {
34629a9cf0e6SAnilkumar Kolli 		arsta->tx_info.flags = IEEE80211_TX_STAT_ACK;
34639a9cf0e6SAnilkumar Kolli 		arsta->tx_info.status.rates[0].count = 1;
34649a9cf0e6SAnilkumar Kolli 		ieee80211_tx_rate_update(ar->hw, sta, &arsta->tx_info);
34659a9cf0e6SAnilkumar Kolli 	}
3466a904417fSAnilkumar Kolli 
3467a904417fSAnilkumar Kolli 	if (ath10k_debug_is_extd_tx_stats_enabled(ar))
3468a904417fSAnilkumar Kolli 		ath10k_accumulate_per_peer_tx_stats(ar, arsta, peer_stats,
3469a904417fSAnilkumar Kolli 						    rate_idx);
3470cec17c38SAnilkumar Kolli }
3471cec17c38SAnilkumar Kolli 
3472cec17c38SAnilkumar Kolli static void ath10k_htt_fetch_peer_stats(struct ath10k *ar,
3473cec17c38SAnilkumar Kolli 					struct sk_buff *skb)
3474cec17c38SAnilkumar Kolli {
3475cec17c38SAnilkumar Kolli 	struct htt_resp *resp = (struct htt_resp *)skb->data;
3476cec17c38SAnilkumar Kolli 	struct ath10k_per_peer_tx_stats *p_tx_stats = &ar->peer_tx_stats;
3477cec17c38SAnilkumar Kolli 	struct htt_per_peer_tx_stats_ind *tx_stats;
3478cec17c38SAnilkumar Kolli 	struct ieee80211_sta *sta;
3479cec17c38SAnilkumar Kolli 	struct ath10k_peer *peer;
3480cec17c38SAnilkumar Kolli 	int peer_id, i;
3481cec17c38SAnilkumar Kolli 	u8 ppdu_len, num_ppdu;
3482cec17c38SAnilkumar Kolli 
3483cec17c38SAnilkumar Kolli 	num_ppdu = resp->peer_tx_stats.num_ppdu;
3484cec17c38SAnilkumar Kolli 	ppdu_len = resp->peer_tx_stats.ppdu_len * sizeof(__le32);
3485cec17c38SAnilkumar Kolli 
3486cec17c38SAnilkumar Kolli 	if (skb->len < sizeof(struct htt_resp_hdr) + num_ppdu * ppdu_len) {
3487cec17c38SAnilkumar Kolli 		ath10k_warn(ar, "Invalid peer stats buf length %d\n", skb->len);
3488cec17c38SAnilkumar Kolli 		return;
3489cec17c38SAnilkumar Kolli 	}
3490cec17c38SAnilkumar Kolli 
3491cec17c38SAnilkumar Kolli 	tx_stats = (struct htt_per_peer_tx_stats_ind *)
3492cec17c38SAnilkumar Kolli 			(resp->peer_tx_stats.payload);
3493cec17c38SAnilkumar Kolli 	peer_id = __le16_to_cpu(tx_stats->peer_id);
3494cec17c38SAnilkumar Kolli 
3495cec17c38SAnilkumar Kolli 	rcu_read_lock();
3496cec17c38SAnilkumar Kolli 	spin_lock_bh(&ar->data_lock);
3497cec17c38SAnilkumar Kolli 	peer = ath10k_peer_find_by_id(ar, peer_id);
34982d3b5585SZhi Chen 	if (!peer || !peer->sta) {
3499cec17c38SAnilkumar Kolli 		ath10k_warn(ar, "Invalid peer id %d peer stats buffer\n",
3500cec17c38SAnilkumar Kolli 			    peer_id);
3501cec17c38SAnilkumar Kolli 		goto out;
3502cec17c38SAnilkumar Kolli 	}
3503cec17c38SAnilkumar Kolli 
3504cec17c38SAnilkumar Kolli 	sta = peer->sta;
3505cec17c38SAnilkumar Kolli 	for (i = 0; i < num_ppdu; i++) {
3506cec17c38SAnilkumar Kolli 		tx_stats = (struct htt_per_peer_tx_stats_ind *)
3507cec17c38SAnilkumar Kolli 			   (resp->peer_tx_stats.payload + i * ppdu_len);
3508cec17c38SAnilkumar Kolli 
3509cec17c38SAnilkumar Kolli 		p_tx_stats->succ_bytes = __le32_to_cpu(tx_stats->succ_bytes);
3510cec17c38SAnilkumar Kolli 		p_tx_stats->retry_bytes = __le32_to_cpu(tx_stats->retry_bytes);
3511cec17c38SAnilkumar Kolli 		p_tx_stats->failed_bytes =
3512cec17c38SAnilkumar Kolli 				__le32_to_cpu(tx_stats->failed_bytes);
3513cec17c38SAnilkumar Kolli 		p_tx_stats->ratecode = tx_stats->ratecode;
3514cec17c38SAnilkumar Kolli 		p_tx_stats->flags = tx_stats->flags;
3515cec17c38SAnilkumar Kolli 		p_tx_stats->succ_pkts = __le16_to_cpu(tx_stats->succ_pkts);
3516cec17c38SAnilkumar Kolli 		p_tx_stats->retry_pkts = __le16_to_cpu(tx_stats->retry_pkts);
3517cec17c38SAnilkumar Kolli 		p_tx_stats->failed_pkts = __le16_to_cpu(tx_stats->failed_pkts);
351805655029SSurabhi Vishnoi 		p_tx_stats->duration = __le16_to_cpu(tx_stats->tx_duration);
3519cec17c38SAnilkumar Kolli 
3520cec17c38SAnilkumar Kolli 		ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
3521cec17c38SAnilkumar Kolli 	}
3522cec17c38SAnilkumar Kolli 
3523cec17c38SAnilkumar Kolli out:
3524cec17c38SAnilkumar Kolli 	spin_unlock_bh(&ar->data_lock);
3525cec17c38SAnilkumar Kolli 	rcu_read_unlock();
3526cec17c38SAnilkumar Kolli }
3527cec17c38SAnilkumar Kolli 
3528e8123bb7SAnilkumar Kolli static void ath10k_fetch_10_2_tx_stats(struct ath10k *ar, u8 *data)
3529e8123bb7SAnilkumar Kolli {
3530e8123bb7SAnilkumar Kolli 	struct ath10k_pktlog_hdr *hdr = (struct ath10k_pktlog_hdr *)data;
3531e8123bb7SAnilkumar Kolli 	struct ath10k_per_peer_tx_stats *p_tx_stats = &ar->peer_tx_stats;
3532e8123bb7SAnilkumar Kolli 	struct ath10k_10_2_peer_tx_stats *tx_stats;
3533e8123bb7SAnilkumar Kolli 	struct ieee80211_sta *sta;
3534e8123bb7SAnilkumar Kolli 	struct ath10k_peer *peer;
3535e8123bb7SAnilkumar Kolli 	u16 log_type = __le16_to_cpu(hdr->log_type);
3536e8123bb7SAnilkumar Kolli 	u32 peer_id = 0, i;
3537e8123bb7SAnilkumar Kolli 
3538e8123bb7SAnilkumar Kolli 	if (log_type != ATH_PKTLOG_TYPE_TX_STAT)
3539e8123bb7SAnilkumar Kolli 		return;
3540e8123bb7SAnilkumar Kolli 
3541e8123bb7SAnilkumar Kolli 	tx_stats = (struct ath10k_10_2_peer_tx_stats *)((hdr->payload) +
3542e8123bb7SAnilkumar Kolli 		    ATH10K_10_2_TX_STATS_OFFSET);
3543e8123bb7SAnilkumar Kolli 
3544e8123bb7SAnilkumar Kolli 	if (!tx_stats->tx_ppdu_cnt)
3545e8123bb7SAnilkumar Kolli 		return;
3546e8123bb7SAnilkumar Kolli 
3547e8123bb7SAnilkumar Kolli 	peer_id = tx_stats->peer_id;
3548e8123bb7SAnilkumar Kolli 
3549e8123bb7SAnilkumar Kolli 	rcu_read_lock();
3550e8123bb7SAnilkumar Kolli 	spin_lock_bh(&ar->data_lock);
3551e8123bb7SAnilkumar Kolli 	peer = ath10k_peer_find_by_id(ar, peer_id);
35522d3b5585SZhi Chen 	if (!peer || !peer->sta) {
3553e8123bb7SAnilkumar Kolli 		ath10k_warn(ar, "Invalid peer id %d in peer stats buffer\n",
3554e8123bb7SAnilkumar Kolli 			    peer_id);
3555e8123bb7SAnilkumar Kolli 		goto out;
3556e8123bb7SAnilkumar Kolli 	}
3557e8123bb7SAnilkumar Kolli 
3558e8123bb7SAnilkumar Kolli 	sta = peer->sta;
3559e8123bb7SAnilkumar Kolli 	for (i = 0; i < tx_stats->tx_ppdu_cnt; i++) {
3560e8123bb7SAnilkumar Kolli 		p_tx_stats->succ_bytes =
3561e8123bb7SAnilkumar Kolli 			__le16_to_cpu(tx_stats->success_bytes[i]);
3562e8123bb7SAnilkumar Kolli 		p_tx_stats->retry_bytes =
3563e8123bb7SAnilkumar Kolli 			__le16_to_cpu(tx_stats->retry_bytes[i]);
3564e8123bb7SAnilkumar Kolli 		p_tx_stats->failed_bytes =
3565e8123bb7SAnilkumar Kolli 			__le16_to_cpu(tx_stats->failed_bytes[i]);
3566e8123bb7SAnilkumar Kolli 		p_tx_stats->ratecode = tx_stats->ratecode[i];
3567e8123bb7SAnilkumar Kolli 		p_tx_stats->flags = tx_stats->flags[i];
3568e8123bb7SAnilkumar Kolli 		p_tx_stats->succ_pkts = tx_stats->success_pkts[i];
3569e8123bb7SAnilkumar Kolli 		p_tx_stats->retry_pkts = tx_stats->retry_pkts[i];
3570e8123bb7SAnilkumar Kolli 		p_tx_stats->failed_pkts = tx_stats->failed_pkts[i];
3571e8123bb7SAnilkumar Kolli 
3572e8123bb7SAnilkumar Kolli 		ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
3573e8123bb7SAnilkumar Kolli 	}
3574e8123bb7SAnilkumar Kolli 	spin_unlock_bh(&ar->data_lock);
3575e8123bb7SAnilkumar Kolli 	rcu_read_unlock();
3576e8123bb7SAnilkumar Kolli 
3577e8123bb7SAnilkumar Kolli 	return;
3578e8123bb7SAnilkumar Kolli 
3579e8123bb7SAnilkumar Kolli out:
3580e8123bb7SAnilkumar Kolli 	spin_unlock_bh(&ar->data_lock);
3581e8123bb7SAnilkumar Kolli 	rcu_read_unlock();
3582e8123bb7SAnilkumar Kolli }
3583e8123bb7SAnilkumar Kolli 
358428ce53b6SWen Gong static int ath10k_htt_rx_pn_len(enum htt_security_types sec_type)
358528ce53b6SWen Gong {
358628ce53b6SWen Gong 	switch (sec_type) {
358728ce53b6SWen Gong 	case HTT_SECURITY_TKIP:
358828ce53b6SWen Gong 	case HTT_SECURITY_TKIP_NOMIC:
358928ce53b6SWen Gong 	case HTT_SECURITY_AES_CCMP:
359028ce53b6SWen Gong 		return 48;
359128ce53b6SWen Gong 	default:
359228ce53b6SWen Gong 		return 0;
359328ce53b6SWen Gong 	}
359428ce53b6SWen Gong }
359528ce53b6SWen Gong 
359628ce53b6SWen Gong static void ath10k_htt_rx_sec_ind_handler(struct ath10k *ar,
359728ce53b6SWen Gong 					  struct htt_security_indication *ev)
359828ce53b6SWen Gong {
359928ce53b6SWen Gong 	enum htt_txrx_sec_cast_type sec_index;
360028ce53b6SWen Gong 	enum htt_security_types sec_type;
360128ce53b6SWen Gong 	struct ath10k_peer *peer;
360228ce53b6SWen Gong 
360328ce53b6SWen Gong 	spin_lock_bh(&ar->data_lock);
360428ce53b6SWen Gong 
360528ce53b6SWen Gong 	peer = ath10k_peer_find_by_id(ar, __le16_to_cpu(ev->peer_id));
360628ce53b6SWen Gong 	if (!peer) {
360728ce53b6SWen Gong 		ath10k_warn(ar, "failed to find peer id %d for security indication",
360828ce53b6SWen Gong 			    __le16_to_cpu(ev->peer_id));
360928ce53b6SWen Gong 		goto out;
361028ce53b6SWen Gong 	}
361128ce53b6SWen Gong 
361228ce53b6SWen Gong 	sec_type = MS(ev->flags, HTT_SECURITY_TYPE);
361328ce53b6SWen Gong 
361428ce53b6SWen Gong 	if (ev->flags & HTT_SECURITY_IS_UNICAST)
361528ce53b6SWen Gong 		sec_index = HTT_TXRX_SEC_UCAST;
361628ce53b6SWen Gong 	else
361728ce53b6SWen Gong 		sec_index = HTT_TXRX_SEC_MCAST;
361828ce53b6SWen Gong 
361928ce53b6SWen Gong 	peer->rx_pn[sec_index].sec_type = sec_type;
362028ce53b6SWen Gong 	peer->rx_pn[sec_index].pn_len = ath10k_htt_rx_pn_len(sec_type);
362128ce53b6SWen Gong 
362228ce53b6SWen Gong 	memset(peer->tids_last_pn_valid, 0, sizeof(peer->tids_last_pn_valid));
362328ce53b6SWen Gong 	memset(peer->tids_last_pn, 0, sizeof(peer->tids_last_pn));
362428ce53b6SWen Gong 
362528ce53b6SWen Gong out:
362628ce53b6SWen Gong 	spin_unlock_bh(&ar->data_lock);
362728ce53b6SWen Gong }
362828ce53b6SWen Gong 
3629e3a91f87SRajkumar Manoharan bool ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
36305e3dd157SKalle Valo {
3631edb8236dSMichal Kazior 	struct ath10k_htt *htt = &ar->htt;
36325e3dd157SKalle Valo 	struct htt_resp *resp = (struct htt_resp *)skb->data;
36338348db29SRajkumar Manoharan 	enum htt_t2h_msg_type type;
36345e3dd157SKalle Valo 
36355e3dd157SKalle Valo 	/* confirm alignment */
36365e3dd157SKalle Valo 	if (!IS_ALIGNED((unsigned long)skb->data, 4))
36377aa7a72aSMichal Kazior 		ath10k_warn(ar, "unaligned htt message, expect trouble\n");
36385e3dd157SKalle Valo 
36397aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
36405e3dd157SKalle Valo 		   resp->hdr.msg_type);
36418348db29SRajkumar Manoharan 
36428348db29SRajkumar Manoharan 	if (resp->hdr.msg_type >= ar->htt.t2h_msg_types_max) {
36438348db29SRajkumar Manoharan 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, unsupported msg_type: 0x%0X\n max: 0x%0X",
36448348db29SRajkumar Manoharan 			   resp->hdr.msg_type, ar->htt.t2h_msg_types_max);
3645e3a91f87SRajkumar Manoharan 		return true;
36468348db29SRajkumar Manoharan 	}
36478348db29SRajkumar Manoharan 	type = ar->htt.t2h_msg_types[resp->hdr.msg_type];
36488348db29SRajkumar Manoharan 
36498348db29SRajkumar Manoharan 	switch (type) {
36505e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_VERSION_CONF: {
36515e3dd157SKalle Valo 		htt->target_version_major = resp->ver_resp.major;
36525e3dd157SKalle Valo 		htt->target_version_minor = resp->ver_resp.minor;
36535e3dd157SKalle Valo 		complete(&htt->target_version_received);
36545e3dd157SKalle Valo 		break;
36555e3dd157SKalle Valo 	}
36566c5151a9SMichal Kazior 	case HTT_T2H_MSG_TYPE_RX_IND:
3657de8781d7SGovind Singh 		if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
3658f88d4934SErik Stromdahl 			return ath10k_htt_rx_proc_rx_ind_hl(htt,
3659f88d4934SErik Stromdahl 							    &resp->rx_ind_hl,
3660130c7749SWen Gong 							    skb,
3661130c7749SWen Gong 							    HTT_RX_PN_CHECK,
3662130c7749SWen Gong 							    HTT_RX_NON_TKIP_MIC);
3663f88d4934SErik Stromdahl 		else
3664f88d4934SErik Stromdahl 			ath10k_htt_rx_proc_rx_ind_ll(htt, &resp->rx_ind);
36653128b3d8SRajkumar Manoharan 		break;
36665e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_PEER_MAP: {
36675e3dd157SKalle Valo 		struct htt_peer_map_event ev = {
36685e3dd157SKalle Valo 			.vdev_id = resp->peer_map.vdev_id,
36695e3dd157SKalle Valo 			.peer_id = __le16_to_cpu(resp->peer_map.peer_id),
36705e3dd157SKalle Valo 		};
36715e3dd157SKalle Valo 		memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
36725e3dd157SKalle Valo 		ath10k_peer_map_event(htt, &ev);
36735e3dd157SKalle Valo 		break;
36745e3dd157SKalle Valo 	}
36755e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
36765e3dd157SKalle Valo 		struct htt_peer_unmap_event ev = {
36775e3dd157SKalle Valo 			.peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
36785e3dd157SKalle Valo 		};
36795e3dd157SKalle Valo 		ath10k_peer_unmap_event(htt, &ev);
36805e3dd157SKalle Valo 		break;
36815e3dd157SKalle Valo 	}
36825e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
36835e3dd157SKalle Valo 		struct htt_tx_done tx_done = {};
36845e3dd157SKalle Valo 		int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
3685235b9c42SVenkateswara Naralasetty 		int info = __le32_to_cpu(resp->mgmt_tx_completion.info);
36865e3dd157SKalle Valo 
368759465fe4SRajkumar Manoharan 		tx_done.msdu_id = __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
36885e3dd157SKalle Valo 
36895e3dd157SKalle Valo 		switch (status) {
36905e3dd157SKalle Valo 		case HTT_MGMT_TX_STATUS_OK:
369159465fe4SRajkumar Manoharan 			tx_done.status = HTT_TX_COMPL_STATE_ACK;
3692235b9c42SVenkateswara Naralasetty 			if (test_bit(WMI_SERVICE_HTT_MGMT_TX_COMP_VALID_FLAGS,
3693235b9c42SVenkateswara Naralasetty 				     ar->wmi.svc_map) &&
3694235b9c42SVenkateswara Naralasetty 			    (resp->mgmt_tx_completion.flags &
3695235b9c42SVenkateswara Naralasetty 			     HTT_MGMT_TX_CMPL_FLAG_ACK_RSSI)) {
3696235b9c42SVenkateswara Naralasetty 				tx_done.ack_rssi =
3697235b9c42SVenkateswara Naralasetty 				FIELD_GET(HTT_MGMT_TX_CMPL_INFO_ACK_RSSI_MASK,
3698235b9c42SVenkateswara Naralasetty 					  info);
3699235b9c42SVenkateswara Naralasetty 			}
37005e3dd157SKalle Valo 			break;
37015e3dd157SKalle Valo 		case HTT_MGMT_TX_STATUS_RETRY:
370259465fe4SRajkumar Manoharan 			tx_done.status = HTT_TX_COMPL_STATE_NOACK;
37035e3dd157SKalle Valo 			break;
37045e3dd157SKalle Valo 		case HTT_MGMT_TX_STATUS_DROP:
370559465fe4SRajkumar Manoharan 			tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
37065e3dd157SKalle Valo 			break;
37075e3dd157SKalle Valo 		}
37085e3dd157SKalle Valo 
3709cac08552SRajkumar Manoharan 		status = ath10k_txrx_tx_unref(htt, &tx_done);
3710cac08552SRajkumar Manoharan 		if (!status) {
3711cac08552SRajkumar Manoharan 			spin_lock_bh(&htt->tx_lock);
3712cac08552SRajkumar Manoharan 			ath10k_htt_tx_mgmt_dec_pending(htt);
3713cac08552SRajkumar Manoharan 			spin_unlock_bh(&htt->tx_lock);
3714cac08552SRajkumar Manoharan 		}
37155e3dd157SKalle Valo 		break;
37165e3dd157SKalle Valo 	}
37176c5151a9SMichal Kazior 	case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
371859465fe4SRajkumar Manoharan 		ath10k_htt_rx_tx_compl_ind(htt->ar, skb);
371959465fe4SRajkumar Manoharan 		break;
37205e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_SEC_IND: {
37215e3dd157SKalle Valo 		struct ath10k *ar = htt->ar;
37225e3dd157SKalle Valo 		struct htt_security_indication *ev = &resp->security_indication;
37235e3dd157SKalle Valo 
372428ce53b6SWen Gong 		ath10k_htt_rx_sec_ind_handler(ar, ev);
37257aa7a72aSMichal Kazior 		ath10k_dbg(ar, ATH10K_DBG_HTT,
37265e3dd157SKalle Valo 			   "sec ind peer_id %d unicast %d type %d\n",
37275e3dd157SKalle Valo 			  __le16_to_cpu(ev->peer_id),
37285e3dd157SKalle Valo 			  !!(ev->flags & HTT_SECURITY_IS_UNICAST),
37295e3dd157SKalle Valo 			  MS(ev->flags, HTT_SECURITY_TYPE));
37305e3dd157SKalle Valo 		complete(&ar->install_key_done);
37315e3dd157SKalle Valo 		break;
37325e3dd157SKalle Valo 	}
37335e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
37347aa7a72aSMichal Kazior 		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
37355e3dd157SKalle Valo 				skb->data, skb->len);
37363c97f5deSRajkumar Manoharan 		atomic_inc(&htt->num_mpdus_ready);
373733f97472SWen Gong 
373833f97472SWen Gong 		return ath10k_htt_rx_proc_rx_frag_ind(htt,
373933f97472SWen Gong 						      &resp->rx_frag_ind,
374033f97472SWen Gong 						      skb);
37415e3dd157SKalle Valo 		break;
37425e3dd157SKalle Valo 	}
37435e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_TEST:
37445e3dd157SKalle Valo 		break;
37455e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_STATS_CONF:
3746d35a6c18SMichal Kazior 		trace_ath10k_htt_stats(ar, skb->data, skb->len);
3747a9bf0506SKalle Valo 		break;
3748a9bf0506SKalle Valo 	case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
3749708b9bdeSMichal Kazior 		/* Firmware can return tx frames if it's unable to fully
3750708b9bdeSMichal Kazior 		 * process them and suspects host may be able to fix it. ath10k
3751708b9bdeSMichal Kazior 		 * sends all tx frames as already inspected so this shouldn't
3752708b9bdeSMichal Kazior 		 * happen unless fw has a bug.
3753708b9bdeSMichal Kazior 		 */
37547aa7a72aSMichal Kazior 		ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
3755708b9bdeSMichal Kazior 		break;
37565e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_RX_ADDBA:
3757aa5b4fbcSMichal Kazior 		ath10k_htt_rx_addba(ar, resp);
3758aa5b4fbcSMichal Kazior 		break;
37595e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_RX_DELBA:
3760aa5b4fbcSMichal Kazior 		ath10k_htt_rx_delba(ar, resp);
3761aa5b4fbcSMichal Kazior 		break;
3762bfdd7937SRajkumar Manoharan 	case HTT_T2H_MSG_TYPE_PKTLOG: {
3763bfdd7937SRajkumar Manoharan 		trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
376434293f75SAshok Raj Nagarajan 					skb->len -
376534293f75SAshok Raj Nagarajan 					offsetof(struct htt_resp,
376634293f75SAshok Raj Nagarajan 						 pktlog_msg.payload));
3767e8123bb7SAnilkumar Kolli 
3768e8123bb7SAnilkumar Kolli 		if (ath10k_peer_stats_enabled(ar))
3769e8123bb7SAnilkumar Kolli 			ath10k_fetch_10_2_tx_stats(ar,
3770e8123bb7SAnilkumar Kolli 						   resp->pktlog_msg.payload);
3771bfdd7937SRajkumar Manoharan 		break;
3772bfdd7937SRajkumar Manoharan 	}
3773aa5b4fbcSMichal Kazior 	case HTT_T2H_MSG_TYPE_RX_FLUSH: {
3774aa5b4fbcSMichal Kazior 		/* Ignore this event because mac80211 takes care of Rx
3775aa5b4fbcSMichal Kazior 		 * aggregation reordering.
3776aa5b4fbcSMichal Kazior 		 */
3777aa5b4fbcSMichal Kazior 		break;
3778aa5b4fbcSMichal Kazior 	}
3779c545070eSMichal Kazior 	case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
378062652555SBob Copeland 		skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
3781e3a91f87SRajkumar Manoharan 		return false;
3782c545070eSMichal Kazior 	}
3783c545070eSMichal Kazior 	case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND:
37848348db29SRajkumar Manoharan 		break;
37852ce9b25cSRajkumar Manoharan 	case HTT_T2H_MSG_TYPE_CHAN_CHANGE: {
37862ce9b25cSRajkumar Manoharan 		u32 phymode = __le32_to_cpu(resp->chan_change.phymode);
37872ce9b25cSRajkumar Manoharan 		u32 freq = __le32_to_cpu(resp->chan_change.freq);
37882ce9b25cSRajkumar Manoharan 
3789543b921bSArend Van Spriel 		ar->tgt_oper_chan = ieee80211_get_channel(ar->hw->wiphy, freq);
37902ce9b25cSRajkumar Manoharan 		ath10k_dbg(ar, ATH10K_DBG_HTT,
37912ce9b25cSRajkumar Manoharan 			   "htt chan change freq %u phymode %s\n",
37922ce9b25cSRajkumar Manoharan 			   freq, ath10k_wmi_phymode_str(phymode));
3793c545070eSMichal Kazior 		break;
37942ce9b25cSRajkumar Manoharan 	}
3795ccec9038SDavid Liu 	case HTT_T2H_MSG_TYPE_AGGR_CONF:
3796ccec9038SDavid Liu 		break;
3797b2fdbccdSRajkumar Manoharan 	case HTT_T2H_MSG_TYPE_TX_FETCH_IND: {
3798b2fdbccdSRajkumar Manoharan 		struct sk_buff *tx_fetch_ind = skb_copy(skb, GFP_ATOMIC);
3799b2fdbccdSRajkumar Manoharan 
3800b2fdbccdSRajkumar Manoharan 		if (!tx_fetch_ind) {
3801b2fdbccdSRajkumar Manoharan 			ath10k_warn(ar, "failed to copy htt tx fetch ind\n");
3802b2fdbccdSRajkumar Manoharan 			break;
3803b2fdbccdSRajkumar Manoharan 		}
3804b2fdbccdSRajkumar Manoharan 		skb_queue_tail(&htt->tx_fetch_ind_q, tx_fetch_ind);
3805b2fdbccdSRajkumar Manoharan 		break;
3806b2fdbccdSRajkumar Manoharan 	}
3807df94e702SMichal Kazior 	case HTT_T2H_MSG_TYPE_TX_FETCH_CONFIRM:
3808839ae637SMichal Kazior 		ath10k_htt_rx_tx_fetch_confirm(ar, skb);
3809839ae637SMichal Kazior 		break;
3810df94e702SMichal Kazior 	case HTT_T2H_MSG_TYPE_TX_MODE_SWITCH_IND:
3811839ae637SMichal Kazior 		ath10k_htt_rx_tx_mode_switch_ind(ar, skb);
38129b158736SMichal Kazior 		break;
3813cec17c38SAnilkumar Kolli 	case HTT_T2H_MSG_TYPE_PEER_STATS:
3814cec17c38SAnilkumar Kolli 		ath10k_htt_fetch_peer_stats(ar, skb);
3815cec17c38SAnilkumar Kolli 		break;
38169b158736SMichal Kazior 	case HTT_T2H_MSG_TYPE_EN_STATS:
38175e3dd157SKalle Valo 	default:
38182358a544SMichal Kazior 		ath10k_warn(ar, "htt event (%d) not handled\n",
38195e3dd157SKalle Valo 			    resp->hdr.msg_type);
38207aa7a72aSMichal Kazior 		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
38215e3dd157SKalle Valo 				skb->data, skb->len);
38225e3dd157SKalle Valo 		break;
3823dab55d10SWaldemar Rymarkiewicz 	}
3824e3a91f87SRajkumar Manoharan 	return true;
38255e3dd157SKalle Valo }
38263f0f7ed4SRajkumar Manoharan EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
38276c5151a9SMichal Kazior 
3828afb0bf7fSVivek Natarajan void ath10k_htt_rx_pktlog_completion_handler(struct ath10k *ar,
3829afb0bf7fSVivek Natarajan 					     struct sk_buff *skb)
3830afb0bf7fSVivek Natarajan {
383153a5c9bcSAshok Raj Nagarajan 	trace_ath10k_htt_pktlog(ar, skb->data, skb->len);
3832afb0bf7fSVivek Natarajan 	dev_kfree_skb_any(skb);
3833afb0bf7fSVivek Natarajan }
3834afb0bf7fSVivek Natarajan EXPORT_SYMBOL(ath10k_htt_rx_pktlog_completion_handler);
3835afb0bf7fSVivek Natarajan 
3836deba1b9eSRajkumar Manoharan static int ath10k_htt_rx_deliver_msdu(struct ath10k *ar, int quota, int budget)
3837deba1b9eSRajkumar Manoharan {
3838deba1b9eSRajkumar Manoharan 	struct sk_buff *skb;
3839deba1b9eSRajkumar Manoharan 
3840deba1b9eSRajkumar Manoharan 	while (quota < budget) {
3841deba1b9eSRajkumar Manoharan 		if (skb_queue_empty(&ar->htt.rx_msdus_q))
3842deba1b9eSRajkumar Manoharan 			break;
3843deba1b9eSRajkumar Manoharan 
384462652555SBob Copeland 		skb = skb_dequeue(&ar->htt.rx_msdus_q);
3845deba1b9eSRajkumar Manoharan 		if (!skb)
3846deba1b9eSRajkumar Manoharan 			break;
3847deba1b9eSRajkumar Manoharan 		ath10k_process_rx(ar, skb);
3848deba1b9eSRajkumar Manoharan 		quota++;
3849deba1b9eSRajkumar Manoharan 	}
3850deba1b9eSRajkumar Manoharan 
3851deba1b9eSRajkumar Manoharan 	return quota;
3852deba1b9eSRajkumar Manoharan }
3853deba1b9eSRajkumar Manoharan 
38543c97f5deSRajkumar Manoharan int ath10k_htt_txrx_compl_task(struct ath10k *ar, int budget)
38556c5151a9SMichal Kazior {
38563c97f5deSRajkumar Manoharan 	struct ath10k_htt *htt = &ar->htt;
385759465fe4SRajkumar Manoharan 	struct htt_tx_done tx_done = {};
3858426e10eaSMichal Kazior 	struct sk_buff_head tx_ind_q;
38596c5151a9SMichal Kazior 	struct sk_buff *skb;
3860d742c969SMichal Kazior 	unsigned long flags;
3861deba1b9eSRajkumar Manoharan 	int quota = 0, done, ret;
38623c97f5deSRajkumar Manoharan 	bool resched_napi = false;
38636c5151a9SMichal Kazior 
3864426e10eaSMichal Kazior 	__skb_queue_head_init(&tx_ind_q);
3865da6416caSRajkumar Manoharan 
3866deba1b9eSRajkumar Manoharan 	/* Process pending frames before dequeuing more data
3867deba1b9eSRajkumar Manoharan 	 * from hardware.
38683c97f5deSRajkumar Manoharan 	 */
3869deba1b9eSRajkumar Manoharan 	quota = ath10k_htt_rx_deliver_msdu(ar, quota, budget);
3870deba1b9eSRajkumar Manoharan 	if (quota == budget) {
38713c97f5deSRajkumar Manoharan 		resched_napi = true;
38723c97f5deSRajkumar Manoharan 		goto exit;
38733c97f5deSRajkumar Manoharan 	}
38743c97f5deSRajkumar Manoharan 
387562652555SBob Copeland 	while ((skb = skb_dequeue(&htt->rx_in_ord_compl_q))) {
38763c97f5deSRajkumar Manoharan 		spin_lock_bh(&htt->rx_ring.lock);
3877deba1b9eSRajkumar Manoharan 		ret = ath10k_htt_rx_in_ord_ind(ar, skb);
38783c97f5deSRajkumar Manoharan 		spin_unlock_bh(&htt->rx_ring.lock);
38793c97f5deSRajkumar Manoharan 
38803c97f5deSRajkumar Manoharan 		dev_kfree_skb_any(skb);
3881deba1b9eSRajkumar Manoharan 		if (ret == -EIO) {
38823c97f5deSRajkumar Manoharan 			resched_napi = true;
38833c97f5deSRajkumar Manoharan 			goto exit;
38843c97f5deSRajkumar Manoharan 		}
38853c97f5deSRajkumar Manoharan 	}
38863c97f5deSRajkumar Manoharan 
3887deba1b9eSRajkumar Manoharan 	while (atomic_read(&htt->num_mpdus_ready)) {
3888deba1b9eSRajkumar Manoharan 		ret = ath10k_htt_rx_handle_amsdu(htt);
3889deba1b9eSRajkumar Manoharan 		if (ret == -EIO) {
38903c97f5deSRajkumar Manoharan 			resched_napi = true;
38913c97f5deSRajkumar Manoharan 			goto exit;
38923c97f5deSRajkumar Manoharan 		}
38933c97f5deSRajkumar Manoharan 		atomic_dec(&htt->num_mpdus_ready);
38943c97f5deSRajkumar Manoharan 	}
3895deba1b9eSRajkumar Manoharan 
3896deba1b9eSRajkumar Manoharan 	/* Deliver received data after processing data from hardware */
3897deba1b9eSRajkumar Manoharan 	quota = ath10k_htt_rx_deliver_msdu(ar, quota, budget);
38983c97f5deSRajkumar Manoharan 
38993c97f5deSRajkumar Manoharan 	/* From NAPI documentation:
39003c97f5deSRajkumar Manoharan 	 *  The napi poll() function may also process TX completions, in which
39013c97f5deSRajkumar Manoharan 	 *  case if it processes the entire TX ring then it should count that
39023c97f5deSRajkumar Manoharan 	 *  work as the rest of the budget.
39033c97f5deSRajkumar Manoharan 	 */
39043c97f5deSRajkumar Manoharan 	if ((quota < budget) && !kfifo_is_empty(&htt->txdone_fifo))
39053c97f5deSRajkumar Manoharan 		quota = budget;
3906426e10eaSMichal Kazior 
390759465fe4SRajkumar Manoharan 	/* kfifo_get: called only within txrx_tasklet so it's neatly serialized.
390859465fe4SRajkumar Manoharan 	 * From kfifo_get() documentation:
390959465fe4SRajkumar Manoharan 	 *  Note that with only one concurrent reader and one concurrent writer,
391059465fe4SRajkumar Manoharan 	 *  you don't need extra locking to use these macro.
391159465fe4SRajkumar Manoharan 	 */
391259465fe4SRajkumar Manoharan 	while (kfifo_get(&htt->txdone_fifo, &tx_done))
391359465fe4SRajkumar Manoharan 		ath10k_txrx_tx_unref(htt, &tx_done);
39146c5151a9SMichal Kazior 
391518f53fe0SRajkumar Manoharan 	ath10k_mac_tx_push_pending(ar);
391618f53fe0SRajkumar Manoharan 
39173c97f5deSRajkumar Manoharan 	spin_lock_irqsave(&htt->tx_fetch_ind_q.lock, flags);
39183c97f5deSRajkumar Manoharan 	skb_queue_splice_init(&htt->tx_fetch_ind_q, &tx_ind_q);
39193c97f5deSRajkumar Manoharan 	spin_unlock_irqrestore(&htt->tx_fetch_ind_q.lock, flags);
39203c97f5deSRajkumar Manoharan 
3921426e10eaSMichal Kazior 	while ((skb = __skb_dequeue(&tx_ind_q))) {
3922426e10eaSMichal Kazior 		ath10k_htt_rx_tx_fetch_ind(ar, skb);
39236c5151a9SMichal Kazior 		dev_kfree_skb_any(skb);
39246c5151a9SMichal Kazior 	}
39256c5151a9SMichal Kazior 
39263c97f5deSRajkumar Manoharan exit:
39275c86d97bSRajkumar Manoharan 	ath10k_htt_rx_msdu_buff_replenish(htt);
39283c97f5deSRajkumar Manoharan 	/* In case of rx failure or more data to read, report budget
39293c97f5deSRajkumar Manoharan 	 * to reschedule NAPI poll
39303c97f5deSRajkumar Manoharan 	 */
39313c97f5deSRajkumar Manoharan 	done = resched_napi ? budget : quota;
39323c97f5deSRajkumar Manoharan 
39333c97f5deSRajkumar Manoharan 	return done;
39346c5151a9SMichal Kazior }
39353c97f5deSRajkumar Manoharan EXPORT_SYMBOL(ath10k_htt_txrx_compl_task);
3936a91a626bSGovind Singh 
3937a91a626bSGovind Singh static const struct ath10k_htt_rx_ops htt_rx_ops_32 = {
3938a91a626bSGovind Singh 	.htt_get_rx_ring_size = ath10k_htt_get_rx_ring_size_32,
3939a91a626bSGovind Singh 	.htt_config_paddrs_ring = ath10k_htt_config_paddrs_ring_32,
3940a91a626bSGovind Singh 	.htt_set_paddrs_ring = ath10k_htt_set_paddrs_ring_32,
3941a91a626bSGovind Singh 	.htt_get_vaddr_ring = ath10k_htt_get_vaddr_ring_32,
3942a91a626bSGovind Singh 	.htt_reset_paddrs_ring = ath10k_htt_reset_paddrs_ring_32,
3943a91a626bSGovind Singh };
3944a91a626bSGovind Singh 
3945a91a626bSGovind Singh static const struct ath10k_htt_rx_ops htt_rx_ops_64 = {
3946a91a626bSGovind Singh 	.htt_get_rx_ring_size = ath10k_htt_get_rx_ring_size_64,
3947a91a626bSGovind Singh 	.htt_config_paddrs_ring = ath10k_htt_config_paddrs_ring_64,
3948a91a626bSGovind Singh 	.htt_set_paddrs_ring = ath10k_htt_set_paddrs_ring_64,
3949a91a626bSGovind Singh 	.htt_get_vaddr_ring = ath10k_htt_get_vaddr_ring_64,
3950a91a626bSGovind Singh 	.htt_reset_paddrs_ring = ath10k_htt_reset_paddrs_ring_64,
3951a91a626bSGovind Singh };
3952a91a626bSGovind Singh 
3953d4e7f553SErik Stromdahl static const struct ath10k_htt_rx_ops htt_rx_ops_hl = {
395433f97472SWen Gong 	.htt_rx_proc_rx_frag_ind = ath10k_htt_rx_proc_rx_frag_ind_hl,
3955d4e7f553SErik Stromdahl };
3956d4e7f553SErik Stromdahl 
3957a91a626bSGovind Singh void ath10k_htt_set_rx_ops(struct ath10k_htt *htt)
3958a91a626bSGovind Singh {
3959a91a626bSGovind Singh 	struct ath10k *ar = htt->ar;
3960a91a626bSGovind Singh 
3961de8781d7SGovind Singh 	if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
3962d4e7f553SErik Stromdahl 		htt->rx_ops = &htt_rx_ops_hl;
3963d4e7f553SErik Stromdahl 	else if (ar->hw_params.target_64bit)
3964a91a626bSGovind Singh 		htt->rx_ops = &htt_rx_ops_64;
3965a91a626bSGovind Singh 	else
3966a91a626bSGovind Singh 		htt->rx_ops = &htt_rx_ops_32;
3967a91a626bSGovind Singh }
3968