15e3dd157SKalle Valo /*
25e3dd157SKalle Valo  * Copyright (c) 2005-2011 Atheros Communications Inc.
35e3dd157SKalle Valo  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
45e3dd157SKalle Valo  *
55e3dd157SKalle Valo  * Permission to use, copy, modify, and/or distribute this software for any
65e3dd157SKalle Valo  * purpose with or without fee is hereby granted, provided that the above
75e3dd157SKalle Valo  * copyright notice and this permission notice appear in all copies.
85e3dd157SKalle Valo  *
95e3dd157SKalle Valo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
105e3dd157SKalle Valo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
115e3dd157SKalle Valo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
125e3dd157SKalle Valo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
135e3dd157SKalle Valo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
145e3dd157SKalle Valo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
155e3dd157SKalle Valo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
165e3dd157SKalle Valo  */
175e3dd157SKalle Valo 
18edb8236dSMichal Kazior #include "core.h"
195e3dd157SKalle Valo #include "htc.h"
205e3dd157SKalle Valo #include "htt.h"
215e3dd157SKalle Valo #include "txrx.h"
225e3dd157SKalle Valo #include "debug.h"
23a9bf0506SKalle Valo #include "trace.h"
24aa5b4fbcSMichal Kazior #include "mac.h"
255e3dd157SKalle Valo 
265e3dd157SKalle Valo #include <linux/log2.h>
275e3dd157SKalle Valo 
28c545070eSMichal Kazior #define HTT_RX_RING_SIZE HTT_RX_RING_SIZE_MAX
29c545070eSMichal Kazior #define HTT_RX_RING_FILL_LEVEL (((HTT_RX_RING_SIZE) / 2) - 1)
305e3dd157SKalle Valo 
315e3dd157SKalle Valo /* when under memory pressure rx ring refill may fail and needs a retry */
325e3dd157SKalle Valo #define HTT_RX_RING_REFILL_RETRY_MS 50
335e3dd157SKalle Valo 
345c86d97bSRajkumar Manoharan #define HTT_RX_RING_REFILL_RESCHED_MS 5
355c86d97bSRajkumar Manoharan 
36f6dc2095SMichal Kazior static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);
37f6dc2095SMichal Kazior 
38c545070eSMichal Kazior static struct sk_buff *
39c545070eSMichal Kazior ath10k_htt_rx_find_skb_paddr(struct ath10k *ar, u32 paddr)
40c545070eSMichal Kazior {
41c545070eSMichal Kazior 	struct ath10k_skb_rxcb *rxcb;
42c545070eSMichal Kazior 
43c545070eSMichal Kazior 	hash_for_each_possible(ar->htt.rx_ring.skb_table, rxcb, hlist, paddr)
44c545070eSMichal Kazior 		if (rxcb->paddr == paddr)
45c545070eSMichal Kazior 			return ATH10K_RXCB_SKB(rxcb);
46c545070eSMichal Kazior 
47c545070eSMichal Kazior 	WARN_ON_ONCE(1);
48c545070eSMichal Kazior 	return NULL;
49c545070eSMichal Kazior }
50c545070eSMichal Kazior 
515e3dd157SKalle Valo static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
525e3dd157SKalle Valo {
535e3dd157SKalle Valo 	struct sk_buff *skb;
54c545070eSMichal Kazior 	struct ath10k_skb_rxcb *rxcb;
55c545070eSMichal Kazior 	struct hlist_node *n;
565e3dd157SKalle Valo 	int i;
575e3dd157SKalle Valo 
58c545070eSMichal Kazior 	if (htt->rx_ring.in_ord_rx) {
59c545070eSMichal Kazior 		hash_for_each_safe(htt->rx_ring.skb_table, i, n, rxcb, hlist) {
60c545070eSMichal Kazior 			skb = ATH10K_RXCB_SKB(rxcb);
61c545070eSMichal Kazior 			dma_unmap_single(htt->ar->dev, rxcb->paddr,
62c545070eSMichal Kazior 					 skb->len + skb_tailroom(skb),
63c545070eSMichal Kazior 					 DMA_FROM_DEVICE);
64c545070eSMichal Kazior 			hash_del(&rxcb->hlist);
65c545070eSMichal Kazior 			dev_kfree_skb_any(skb);
66c545070eSMichal Kazior 		}
67c545070eSMichal Kazior 	} else {
68c545070eSMichal Kazior 		for (i = 0; i < htt->rx_ring.size; i++) {
695e3dd157SKalle Valo 			skb = htt->rx_ring.netbufs_ring[i];
70c545070eSMichal Kazior 			if (!skb)
71c545070eSMichal Kazior 				continue;
72c545070eSMichal Kazior 
73c545070eSMichal Kazior 			rxcb = ATH10K_SKB_RXCB(skb);
74c545070eSMichal Kazior 			dma_unmap_single(htt->ar->dev, rxcb->paddr,
755e3dd157SKalle Valo 					 skb->len + skb_tailroom(skb),
765e3dd157SKalle Valo 					 DMA_FROM_DEVICE);
775e3dd157SKalle Valo 			dev_kfree_skb_any(skb);
785e3dd157SKalle Valo 		}
79c545070eSMichal Kazior 	}
805e3dd157SKalle Valo 
815e3dd157SKalle Valo 	htt->rx_ring.fill_cnt = 0;
82c545070eSMichal Kazior 	hash_init(htt->rx_ring.skb_table);
83c545070eSMichal Kazior 	memset(htt->rx_ring.netbufs_ring, 0,
84c545070eSMichal Kazior 	       htt->rx_ring.size * sizeof(htt->rx_ring.netbufs_ring[0]));
855e3dd157SKalle Valo }
865e3dd157SKalle Valo 
875e3dd157SKalle Valo static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
885e3dd157SKalle Valo {
895e3dd157SKalle Valo 	struct htt_rx_desc *rx_desc;
90c545070eSMichal Kazior 	struct ath10k_skb_rxcb *rxcb;
915e3dd157SKalle Valo 	struct sk_buff *skb;
925e3dd157SKalle Valo 	dma_addr_t paddr;
935e3dd157SKalle Valo 	int ret = 0, idx;
945e3dd157SKalle Valo 
95c545070eSMichal Kazior 	/* The Full Rx Reorder firmware has no way of telling the host
96c545070eSMichal Kazior 	 * implicitly when it copied HTT Rx Ring buffers to MAC Rx Ring.
97c545070eSMichal Kazior 	 * To keep things simple make sure ring is always half empty. This
98c545070eSMichal Kazior 	 * guarantees there'll be no replenishment overruns possible.
99c545070eSMichal Kazior 	 */
100c545070eSMichal Kazior 	BUILD_BUG_ON(HTT_RX_RING_FILL_LEVEL >= HTT_RX_RING_SIZE / 2);
101c545070eSMichal Kazior 
1028cc7f26cSKalle Valo 	idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
1035e3dd157SKalle Valo 	while (num > 0) {
1045e3dd157SKalle Valo 		skb = dev_alloc_skb(HTT_RX_BUF_SIZE + HTT_RX_DESC_ALIGN);
1055e3dd157SKalle Valo 		if (!skb) {
1065e3dd157SKalle Valo 			ret = -ENOMEM;
1075e3dd157SKalle Valo 			goto fail;
1085e3dd157SKalle Valo 		}
1095e3dd157SKalle Valo 
1105e3dd157SKalle Valo 		if (!IS_ALIGNED((unsigned long)skb->data, HTT_RX_DESC_ALIGN))
1115e3dd157SKalle Valo 			skb_pull(skb,
1125e3dd157SKalle Valo 				 PTR_ALIGN(skb->data, HTT_RX_DESC_ALIGN) -
1135e3dd157SKalle Valo 				 skb->data);
1145e3dd157SKalle Valo 
1155e3dd157SKalle Valo 		/* Clear rx_desc attention word before posting to Rx ring */
1165e3dd157SKalle Valo 		rx_desc = (struct htt_rx_desc *)skb->data;
1175e3dd157SKalle Valo 		rx_desc->attention.flags = __cpu_to_le32(0);
1185e3dd157SKalle Valo 
1195e3dd157SKalle Valo 		paddr = dma_map_single(htt->ar->dev, skb->data,
1205e3dd157SKalle Valo 				       skb->len + skb_tailroom(skb),
1215e3dd157SKalle Valo 				       DMA_FROM_DEVICE);
1225e3dd157SKalle Valo 
1235e3dd157SKalle Valo 		if (unlikely(dma_mapping_error(htt->ar->dev, paddr))) {
1245e3dd157SKalle Valo 			dev_kfree_skb_any(skb);
1255e3dd157SKalle Valo 			ret = -ENOMEM;
1265e3dd157SKalle Valo 			goto fail;
1275e3dd157SKalle Valo 		}
1285e3dd157SKalle Valo 
129c545070eSMichal Kazior 		rxcb = ATH10K_SKB_RXCB(skb);
130c545070eSMichal Kazior 		rxcb->paddr = paddr;
1315e3dd157SKalle Valo 		htt->rx_ring.netbufs_ring[idx] = skb;
1325e3dd157SKalle Valo 		htt->rx_ring.paddrs_ring[idx] = __cpu_to_le32(paddr);
1335e3dd157SKalle Valo 		htt->rx_ring.fill_cnt++;
1345e3dd157SKalle Valo 
135c545070eSMichal Kazior 		if (htt->rx_ring.in_ord_rx) {
136c545070eSMichal Kazior 			hash_add(htt->rx_ring.skb_table,
137c545070eSMichal Kazior 				 &ATH10K_SKB_RXCB(skb)->hlist,
138c545070eSMichal Kazior 				 (u32)paddr);
139c545070eSMichal Kazior 		}
140c545070eSMichal Kazior 
1415e3dd157SKalle Valo 		num--;
1425e3dd157SKalle Valo 		idx++;
1435e3dd157SKalle Valo 		idx &= htt->rx_ring.size_mask;
1445e3dd157SKalle Valo 	}
1455e3dd157SKalle Valo 
1465e3dd157SKalle Valo fail:
1475de6dfc8SVasanthakumar Thiagarajan 	/*
1485de6dfc8SVasanthakumar Thiagarajan 	 * Make sure the rx buffer is updated before available buffer
1495de6dfc8SVasanthakumar Thiagarajan 	 * index to avoid any potential rx ring corruption.
1505de6dfc8SVasanthakumar Thiagarajan 	 */
1515de6dfc8SVasanthakumar Thiagarajan 	mb();
1528cc7f26cSKalle Valo 	*htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
1535e3dd157SKalle Valo 	return ret;
1545e3dd157SKalle Valo }
1555e3dd157SKalle Valo 
1565e3dd157SKalle Valo static int ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
1575e3dd157SKalle Valo {
1585e3dd157SKalle Valo 	lockdep_assert_held(&htt->rx_ring.lock);
1595e3dd157SKalle Valo 	return __ath10k_htt_rx_ring_fill_n(htt, num);
1605e3dd157SKalle Valo }
1615e3dd157SKalle Valo 
1625e3dd157SKalle Valo static void ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt *htt)
1635e3dd157SKalle Valo {
1646e712d42SMichal Kazior 	int ret, num_deficit, num_to_fill;
1655e3dd157SKalle Valo 
1666e712d42SMichal Kazior 	/* Refilling the whole RX ring buffer proves to be a bad idea. The
1676e712d42SMichal Kazior 	 * reason is RX may take up significant amount of CPU cycles and starve
1686e712d42SMichal Kazior 	 * other tasks, e.g. TX on an ethernet device while acting as a bridge
1696e712d42SMichal Kazior 	 * with ath10k wlan interface. This ended up with very poor performance
1706e712d42SMichal Kazior 	 * once CPU the host system was overwhelmed with RX on ath10k.
1716e712d42SMichal Kazior 	 *
1726e712d42SMichal Kazior 	 * By limiting the number of refills the replenishing occurs
1736e712d42SMichal Kazior 	 * progressively. This in turns makes use of the fact tasklets are
1746e712d42SMichal Kazior 	 * processed in FIFO order. This means actual RX processing can starve
1756e712d42SMichal Kazior 	 * out refilling. If there's not enough buffers on RX ring FW will not
1766e712d42SMichal Kazior 	 * report RX until it is refilled with enough buffers. This
1776e712d42SMichal Kazior 	 * automatically balances load wrt to CPU power.
1786e712d42SMichal Kazior 	 *
1796e712d42SMichal Kazior 	 * This probably comes at a cost of lower maximum throughput but
1803eafdfd6SBen Greear 	 * improves the average and stability. */
1815e3dd157SKalle Valo 	spin_lock_bh(&htt->rx_ring.lock);
1826e712d42SMichal Kazior 	num_deficit = htt->rx_ring.fill_level - htt->rx_ring.fill_cnt;
1836e712d42SMichal Kazior 	num_to_fill = min(ATH10K_HTT_MAX_NUM_REFILL, num_deficit);
1846e712d42SMichal Kazior 	num_deficit -= num_to_fill;
1855e3dd157SKalle Valo 	ret = ath10k_htt_rx_ring_fill_n(htt, num_to_fill);
1865e3dd157SKalle Valo 	if (ret == -ENOMEM) {
1875e3dd157SKalle Valo 		/*
1885e3dd157SKalle Valo 		 * Failed to fill it to the desired level -
1895e3dd157SKalle Valo 		 * we'll start a timer and try again next time.
1905e3dd157SKalle Valo 		 * As long as enough buffers are left in the ring for
1915e3dd157SKalle Valo 		 * another A-MPDU rx, no special recovery is needed.
1925e3dd157SKalle Valo 		 */
1935e3dd157SKalle Valo 		mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
1945e3dd157SKalle Valo 			  msecs_to_jiffies(HTT_RX_RING_REFILL_RETRY_MS));
1956e712d42SMichal Kazior 	} else if (num_deficit > 0) {
1965c86d97bSRajkumar Manoharan 		mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
1975c86d97bSRajkumar Manoharan 			  msecs_to_jiffies(HTT_RX_RING_REFILL_RESCHED_MS));
1985e3dd157SKalle Valo 	}
1995e3dd157SKalle Valo 	spin_unlock_bh(&htt->rx_ring.lock);
2005e3dd157SKalle Valo }
2015e3dd157SKalle Valo 
2025e3dd157SKalle Valo static void ath10k_htt_rx_ring_refill_retry(unsigned long arg)
2035e3dd157SKalle Valo {
2045e3dd157SKalle Valo 	struct ath10k_htt *htt = (struct ath10k_htt *)arg;
205af762c0bSKalle Valo 
2065e3dd157SKalle Valo 	ath10k_htt_rx_msdu_buff_replenish(htt);
2075e3dd157SKalle Valo }
2085e3dd157SKalle Valo 
209c545070eSMichal Kazior int ath10k_htt_rx_ring_refill(struct ath10k *ar)
2103e841fd0SMichal Kazior {
211c545070eSMichal Kazior 	struct ath10k_htt *htt = &ar->htt;
212c545070eSMichal Kazior 	int ret;
2133e841fd0SMichal Kazior 
214c545070eSMichal Kazior 	spin_lock_bh(&htt->rx_ring.lock);
215c545070eSMichal Kazior 	ret = ath10k_htt_rx_ring_fill_n(htt, (htt->rx_ring.fill_level -
216c545070eSMichal Kazior 					      htt->rx_ring.fill_cnt));
217c545070eSMichal Kazior 	spin_unlock_bh(&htt->rx_ring.lock);
2183e841fd0SMichal Kazior 
219c545070eSMichal Kazior 	if (ret)
220c545070eSMichal Kazior 		ath10k_htt_rx_ring_free(htt);
221c545070eSMichal Kazior 
222c545070eSMichal Kazior 	return ret;
2233e841fd0SMichal Kazior }
2243e841fd0SMichal Kazior 
22595bf21f9SMichal Kazior void ath10k_htt_rx_free(struct ath10k_htt *htt)
2265e3dd157SKalle Valo {
2275e3dd157SKalle Valo 	del_timer_sync(&htt->rx_ring.refill_retry_timer);
2286c5151a9SMichal Kazior 
2296c5151a9SMichal Kazior 	skb_queue_purge(&htt->rx_compl_q);
230c545070eSMichal Kazior 	skb_queue_purge(&htt->rx_in_ord_compl_q);
231426e10eaSMichal Kazior 	skb_queue_purge(&htt->tx_fetch_ind_q);
2325e3dd157SKalle Valo 
233c545070eSMichal Kazior 	ath10k_htt_rx_ring_free(htt);
2345e3dd157SKalle Valo 
2355e3dd157SKalle Valo 	dma_free_coherent(htt->ar->dev,
2365e3dd157SKalle Valo 			  (htt->rx_ring.size *
2375e3dd157SKalle Valo 			   sizeof(htt->rx_ring.paddrs_ring)),
2385e3dd157SKalle Valo 			  htt->rx_ring.paddrs_ring,
2395e3dd157SKalle Valo 			  htt->rx_ring.base_paddr);
2405e3dd157SKalle Valo 
2415e3dd157SKalle Valo 	dma_free_coherent(htt->ar->dev,
2425e3dd157SKalle Valo 			  sizeof(*htt->rx_ring.alloc_idx.vaddr),
2435e3dd157SKalle Valo 			  htt->rx_ring.alloc_idx.vaddr,
2445e3dd157SKalle Valo 			  htt->rx_ring.alloc_idx.paddr);
2455e3dd157SKalle Valo 
2465e3dd157SKalle Valo 	kfree(htt->rx_ring.netbufs_ring);
2475e3dd157SKalle Valo }
2485e3dd157SKalle Valo 
2495e3dd157SKalle Valo static inline struct sk_buff *ath10k_htt_rx_netbuf_pop(struct ath10k_htt *htt)
2505e3dd157SKalle Valo {
2517aa7a72aSMichal Kazior 	struct ath10k *ar = htt->ar;
2525e3dd157SKalle Valo 	int idx;
2535e3dd157SKalle Valo 	struct sk_buff *msdu;
2545e3dd157SKalle Valo 
25545967089SMichal Kazior 	lockdep_assert_held(&htt->rx_ring.lock);
2565e3dd157SKalle Valo 
2578d60ee87SMichal Kazior 	if (htt->rx_ring.fill_cnt == 0) {
2587aa7a72aSMichal Kazior 		ath10k_warn(ar, "tried to pop sk_buff from an empty rx ring\n");
2598d60ee87SMichal Kazior 		return NULL;
2608d60ee87SMichal Kazior 	}
2615e3dd157SKalle Valo 
2625e3dd157SKalle Valo 	idx = htt->rx_ring.sw_rd_idx.msdu_payld;
2635e3dd157SKalle Valo 	msdu = htt->rx_ring.netbufs_ring[idx];
2643e841fd0SMichal Kazior 	htt->rx_ring.netbufs_ring[idx] = NULL;
265c545070eSMichal Kazior 	htt->rx_ring.paddrs_ring[idx] = 0;
2665e3dd157SKalle Valo 
2675e3dd157SKalle Valo 	idx++;
2685e3dd157SKalle Valo 	idx &= htt->rx_ring.size_mask;
2695e3dd157SKalle Valo 	htt->rx_ring.sw_rd_idx.msdu_payld = idx;
2705e3dd157SKalle Valo 	htt->rx_ring.fill_cnt--;
2715e3dd157SKalle Valo 
2724de02806SMichal Kazior 	dma_unmap_single(htt->ar->dev,
2738582bf3bSMichal Kazior 			 ATH10K_SKB_RXCB(msdu)->paddr,
2744de02806SMichal Kazior 			 msdu->len + skb_tailroom(msdu),
2754de02806SMichal Kazior 			 DMA_FROM_DEVICE);
2764de02806SMichal Kazior 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
2774de02806SMichal Kazior 			msdu->data, msdu->len + skb_tailroom(msdu));
2784de02806SMichal Kazior 
2795e3dd157SKalle Valo 	return msdu;
2805e3dd157SKalle Valo }
2815e3dd157SKalle Valo 
282d84dd60fSJanusz Dziedzic /* return: < 0 fatal error, 0 - non chained msdu, 1 chained msdu */
2835e3dd157SKalle Valo static int ath10k_htt_rx_amsdu_pop(struct ath10k_htt *htt,
284f0e2770fSMichal Kazior 				   struct sk_buff_head *amsdu)
2855e3dd157SKalle Valo {
2867aa7a72aSMichal Kazior 	struct ath10k *ar = htt->ar;
2875e3dd157SKalle Valo 	int msdu_len, msdu_chaining = 0;
2889aa505d2SMichal Kazior 	struct sk_buff *msdu;
2895e3dd157SKalle Valo 	struct htt_rx_desc *rx_desc;
2905e3dd157SKalle Valo 
29145967089SMichal Kazior 	lockdep_assert_held(&htt->rx_ring.lock);
29245967089SMichal Kazior 
2939aa505d2SMichal Kazior 	for (;;) {
2945e3dd157SKalle Valo 		int last_msdu, msdu_len_invalid, msdu_chained;
2955e3dd157SKalle Valo 
2969aa505d2SMichal Kazior 		msdu = ath10k_htt_rx_netbuf_pop(htt);
2979aa505d2SMichal Kazior 		if (!msdu) {
2989aa505d2SMichal Kazior 			__skb_queue_purge(amsdu);
299e0bd7513SMichal Kazior 			return -ENOENT;
3009aa505d2SMichal Kazior 		}
3019aa505d2SMichal Kazior 
3029aa505d2SMichal Kazior 		__skb_queue_tail(amsdu, msdu);
3039aa505d2SMichal Kazior 
3045e3dd157SKalle Valo 		rx_desc = (struct htt_rx_desc *)msdu->data;
3055e3dd157SKalle Valo 
3065e3dd157SKalle Valo 		/* FIXME: we must report msdu payload since this is what caller
3075e3dd157SKalle Valo 		 *        expects now */
3085e3dd157SKalle Valo 		skb_put(msdu, offsetof(struct htt_rx_desc, msdu_payload));
3095e3dd157SKalle Valo 		skb_pull(msdu, offsetof(struct htt_rx_desc, msdu_payload));
3105e3dd157SKalle Valo 
3115e3dd157SKalle Valo 		/*
3125e3dd157SKalle Valo 		 * Sanity check - confirm the HW is finished filling in the
3135e3dd157SKalle Valo 		 * rx data.
3145e3dd157SKalle Valo 		 * If the HW and SW are working correctly, then it's guaranteed
3155e3dd157SKalle Valo 		 * that the HW's MAC DMA is done before this point in the SW.
3165e3dd157SKalle Valo 		 * To prevent the case that we handle a stale Rx descriptor,
3175e3dd157SKalle Valo 		 * just assert for now until we have a way to recover.
3185e3dd157SKalle Valo 		 */
3195e3dd157SKalle Valo 		if (!(__le32_to_cpu(rx_desc->attention.flags)
3205e3dd157SKalle Valo 				& RX_ATTENTION_FLAGS_MSDU_DONE)) {
3219aa505d2SMichal Kazior 			__skb_queue_purge(amsdu);
322e0bd7513SMichal Kazior 			return -EIO;
3235e3dd157SKalle Valo 		}
3245e3dd157SKalle Valo 
3255e3dd157SKalle Valo 		msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
3265e3dd157SKalle Valo 					& (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
3275e3dd157SKalle Valo 					   RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
3281f5dbfbbSPeter Oh 		msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.common.info0),
3295e3dd157SKalle Valo 			      RX_MSDU_START_INFO0_MSDU_LENGTH);
3305e3dd157SKalle Valo 		msdu_chained = rx_desc->frag_info.ring2_more_count;
3315e3dd157SKalle Valo 
3325e3dd157SKalle Valo 		if (msdu_len_invalid)
3335e3dd157SKalle Valo 			msdu_len = 0;
3345e3dd157SKalle Valo 
3355e3dd157SKalle Valo 		skb_trim(msdu, 0);
3365e3dd157SKalle Valo 		skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
3375e3dd157SKalle Valo 		msdu_len -= msdu->len;
3385e3dd157SKalle Valo 
3399aa505d2SMichal Kazior 		/* Note: Chained buffers do not contain rx descriptor */
3405e3dd157SKalle Valo 		while (msdu_chained--) {
3419aa505d2SMichal Kazior 			msdu = ath10k_htt_rx_netbuf_pop(htt);
3429aa505d2SMichal Kazior 			if (!msdu) {
3439aa505d2SMichal Kazior 				__skb_queue_purge(amsdu);
344e0bd7513SMichal Kazior 				return -ENOENT;
345b30595aeSMichal Kazior 			}
346b30595aeSMichal Kazior 
3479aa505d2SMichal Kazior 			__skb_queue_tail(amsdu, msdu);
3489aa505d2SMichal Kazior 			skb_trim(msdu, 0);
3499aa505d2SMichal Kazior 			skb_put(msdu, min(msdu_len, HTT_RX_BUF_SIZE));
3509aa505d2SMichal Kazior 			msdu_len -= msdu->len;
351ede9c8e0SMichal Kazior 			msdu_chaining = 1;
3525e3dd157SKalle Valo 		}
3535e3dd157SKalle Valo 
3541f5dbfbbSPeter Oh 		last_msdu = __le32_to_cpu(rx_desc->msdu_end.common.info0) &
3555e3dd157SKalle Valo 				RX_MSDU_END_INFO0_LAST_MSDU;
3565e3dd157SKalle Valo 
357b04e204fSMichal Kazior 		trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
358a0883cf7SRajkumar Manoharan 					 sizeof(*rx_desc) - sizeof(u32));
3599aa505d2SMichal Kazior 
3609aa505d2SMichal Kazior 		if (last_msdu)
3615e3dd157SKalle Valo 			break;
362d8bb26b9SKalle Valo 	}
363d8bb26b9SKalle Valo 
3649aa505d2SMichal Kazior 	if (skb_queue_empty(amsdu))
365d84dd60fSJanusz Dziedzic 		msdu_chaining = -1;
366d84dd60fSJanusz Dziedzic 
3675e3dd157SKalle Valo 	/*
3685e3dd157SKalle Valo 	 * Don't refill the ring yet.
3695e3dd157SKalle Valo 	 *
3705e3dd157SKalle Valo 	 * First, the elements popped here are still in use - it is not
3715e3dd157SKalle Valo 	 * safe to overwrite them until the matching call to
3725e3dd157SKalle Valo 	 * mpdu_desc_list_next. Second, for efficiency it is preferable to
3735e3dd157SKalle Valo 	 * refill the rx ring with 1 PPDU's worth of rx buffers (something
3745e3dd157SKalle Valo 	 * like 32 x 3 buffers), rather than one MPDU's worth of rx buffers
3755e3dd157SKalle Valo 	 * (something like 3 buffers). Consequently, we'll rely on the txrx
3765e3dd157SKalle Valo 	 * SW to tell us when it is done pulling all the PPDU's rx buffers
3775e3dd157SKalle Valo 	 * out of the rx ring, and then refill it just once.
3785e3dd157SKalle Valo 	 */
3795e3dd157SKalle Valo 
3805e3dd157SKalle Valo 	return msdu_chaining;
3815e3dd157SKalle Valo }
3825e3dd157SKalle Valo 
383c545070eSMichal Kazior static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
384c545070eSMichal Kazior 					       u32 paddr)
385c545070eSMichal Kazior {
386c545070eSMichal Kazior 	struct ath10k *ar = htt->ar;
387c545070eSMichal Kazior 	struct ath10k_skb_rxcb *rxcb;
388c545070eSMichal Kazior 	struct sk_buff *msdu;
389c545070eSMichal Kazior 
390c545070eSMichal Kazior 	lockdep_assert_held(&htt->rx_ring.lock);
391c545070eSMichal Kazior 
392c545070eSMichal Kazior 	msdu = ath10k_htt_rx_find_skb_paddr(ar, paddr);
393c545070eSMichal Kazior 	if (!msdu)
394c545070eSMichal Kazior 		return NULL;
395c545070eSMichal Kazior 
396c545070eSMichal Kazior 	rxcb = ATH10K_SKB_RXCB(msdu);
397c545070eSMichal Kazior 	hash_del(&rxcb->hlist);
398c545070eSMichal Kazior 	htt->rx_ring.fill_cnt--;
399c545070eSMichal Kazior 
400c545070eSMichal Kazior 	dma_unmap_single(htt->ar->dev, rxcb->paddr,
401c545070eSMichal Kazior 			 msdu->len + skb_tailroom(msdu),
402c545070eSMichal Kazior 			 DMA_FROM_DEVICE);
403c545070eSMichal Kazior 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
404c545070eSMichal Kazior 			msdu->data, msdu->len + skb_tailroom(msdu));
405c545070eSMichal Kazior 
406c545070eSMichal Kazior 	return msdu;
407c545070eSMichal Kazior }
408c545070eSMichal Kazior 
409c545070eSMichal Kazior static int ath10k_htt_rx_pop_paddr_list(struct ath10k_htt *htt,
410c545070eSMichal Kazior 					struct htt_rx_in_ord_ind *ev,
411c545070eSMichal Kazior 					struct sk_buff_head *list)
412c545070eSMichal Kazior {
413c545070eSMichal Kazior 	struct ath10k *ar = htt->ar;
414c545070eSMichal Kazior 	struct htt_rx_in_ord_msdu_desc *msdu_desc = ev->msdu_descs;
415c545070eSMichal Kazior 	struct htt_rx_desc *rxd;
416c545070eSMichal Kazior 	struct sk_buff *msdu;
417c545070eSMichal Kazior 	int msdu_count;
418c545070eSMichal Kazior 	bool is_offload;
419c545070eSMichal Kazior 	u32 paddr;
420c545070eSMichal Kazior 
421c545070eSMichal Kazior 	lockdep_assert_held(&htt->rx_ring.lock);
422c545070eSMichal Kazior 
423c545070eSMichal Kazior 	msdu_count = __le16_to_cpu(ev->msdu_count);
424c545070eSMichal Kazior 	is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
425c545070eSMichal Kazior 
426c545070eSMichal Kazior 	while (msdu_count--) {
427c545070eSMichal Kazior 		paddr = __le32_to_cpu(msdu_desc->msdu_paddr);
428c545070eSMichal Kazior 
429c545070eSMichal Kazior 		msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
430c545070eSMichal Kazior 		if (!msdu) {
431c545070eSMichal Kazior 			__skb_queue_purge(list);
432c545070eSMichal Kazior 			return -ENOENT;
433c545070eSMichal Kazior 		}
434c545070eSMichal Kazior 
435c545070eSMichal Kazior 		__skb_queue_tail(list, msdu);
436c545070eSMichal Kazior 
437c545070eSMichal Kazior 		if (!is_offload) {
438c545070eSMichal Kazior 			rxd = (void *)msdu->data;
439c545070eSMichal Kazior 
440c545070eSMichal Kazior 			trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
441c545070eSMichal Kazior 
442c545070eSMichal Kazior 			skb_put(msdu, sizeof(*rxd));
443c545070eSMichal Kazior 			skb_pull(msdu, sizeof(*rxd));
444c545070eSMichal Kazior 			skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
445c545070eSMichal Kazior 
446c545070eSMichal Kazior 			if (!(__le32_to_cpu(rxd->attention.flags) &
447c545070eSMichal Kazior 			      RX_ATTENTION_FLAGS_MSDU_DONE)) {
448c545070eSMichal Kazior 				ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
449c545070eSMichal Kazior 				return -EIO;
450c545070eSMichal Kazior 			}
451c545070eSMichal Kazior 		}
452c545070eSMichal Kazior 
453c545070eSMichal Kazior 		msdu_desc++;
454c545070eSMichal Kazior 	}
455c545070eSMichal Kazior 
456c545070eSMichal Kazior 	return 0;
457c545070eSMichal Kazior }
458c545070eSMichal Kazior 
45995bf21f9SMichal Kazior int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
4605e3dd157SKalle Valo {
4617aa7a72aSMichal Kazior 	struct ath10k *ar = htt->ar;
4625e3dd157SKalle Valo 	dma_addr_t paddr;
4635e3dd157SKalle Valo 	void *vaddr;
464bd8bdbb6SKalle Valo 	size_t size;
4655e3dd157SKalle Valo 	struct timer_list *timer = &htt->rx_ring.refill_retry_timer;
4665e3dd157SKalle Valo 
46751fc7d74SMichal Kazior 	htt->rx_confused = false;
46851fc7d74SMichal Kazior 
469fe2407a8SMichal Kazior 	/* XXX: The fill level could be changed during runtime in response to
470fe2407a8SMichal Kazior 	 * the host processing latency. Is this really worth it?
471fe2407a8SMichal Kazior 	 */
472fe2407a8SMichal Kazior 	htt->rx_ring.size = HTT_RX_RING_SIZE;
473fe2407a8SMichal Kazior 	htt->rx_ring.size_mask = htt->rx_ring.size - 1;
474fe2407a8SMichal Kazior 	htt->rx_ring.fill_level = HTT_RX_RING_FILL_LEVEL;
475fe2407a8SMichal Kazior 
4765e3dd157SKalle Valo 	if (!is_power_of_2(htt->rx_ring.size)) {
4777aa7a72aSMichal Kazior 		ath10k_warn(ar, "htt rx ring size is not power of 2\n");
4785e3dd157SKalle Valo 		return -EINVAL;
4795e3dd157SKalle Valo 	}
4805e3dd157SKalle Valo 
4815e3dd157SKalle Valo 	htt->rx_ring.netbufs_ring =
4823e841fd0SMichal Kazior 		kzalloc(htt->rx_ring.size * sizeof(struct sk_buff *),
4835e3dd157SKalle Valo 			GFP_KERNEL);
4845e3dd157SKalle Valo 	if (!htt->rx_ring.netbufs_ring)
4855e3dd157SKalle Valo 		goto err_netbuf;
4865e3dd157SKalle Valo 
487bd8bdbb6SKalle Valo 	size = htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring);
488bd8bdbb6SKalle Valo 
489d6cb23b5SFelix Fietkau 	vaddr = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_KERNEL);
4905e3dd157SKalle Valo 	if (!vaddr)
4915e3dd157SKalle Valo 		goto err_dma_ring;
4925e3dd157SKalle Valo 
4935e3dd157SKalle Valo 	htt->rx_ring.paddrs_ring = vaddr;
4945e3dd157SKalle Valo 	htt->rx_ring.base_paddr = paddr;
4955e3dd157SKalle Valo 
4965e3dd157SKalle Valo 	vaddr = dma_alloc_coherent(htt->ar->dev,
4975e3dd157SKalle Valo 				   sizeof(*htt->rx_ring.alloc_idx.vaddr),
498d6cb23b5SFelix Fietkau 				   &paddr, GFP_KERNEL);
4995e3dd157SKalle Valo 	if (!vaddr)
5005e3dd157SKalle Valo 		goto err_dma_idx;
5015e3dd157SKalle Valo 
5025e3dd157SKalle Valo 	htt->rx_ring.alloc_idx.vaddr = vaddr;
5035e3dd157SKalle Valo 	htt->rx_ring.alloc_idx.paddr = paddr;
504c545070eSMichal Kazior 	htt->rx_ring.sw_rd_idx.msdu_payld = htt->rx_ring.size_mask;
5055e3dd157SKalle Valo 	*htt->rx_ring.alloc_idx.vaddr = 0;
5065e3dd157SKalle Valo 
5075e3dd157SKalle Valo 	/* Initialize the Rx refill retry timer */
5085e3dd157SKalle Valo 	setup_timer(timer, ath10k_htt_rx_ring_refill_retry, (unsigned long)htt);
5095e3dd157SKalle Valo 
5105e3dd157SKalle Valo 	spin_lock_init(&htt->rx_ring.lock);
5115e3dd157SKalle Valo 
5125e3dd157SKalle Valo 	htt->rx_ring.fill_cnt = 0;
513c545070eSMichal Kazior 	htt->rx_ring.sw_rd_idx.msdu_payld = 0;
514c545070eSMichal Kazior 	hash_init(htt->rx_ring.skb_table);
5155e3dd157SKalle Valo 
5166c5151a9SMichal Kazior 	skb_queue_head_init(&htt->rx_compl_q);
517c545070eSMichal Kazior 	skb_queue_head_init(&htt->rx_in_ord_compl_q);
518426e10eaSMichal Kazior 	skb_queue_head_init(&htt->tx_fetch_ind_q);
5193128b3d8SRajkumar Manoharan 	atomic_set(&htt->num_mpdus_ready, 0);
5206c5151a9SMichal Kazior 
5217aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
5225e3dd157SKalle Valo 		   htt->rx_ring.size, htt->rx_ring.fill_level);
5235e3dd157SKalle Valo 	return 0;
5245e3dd157SKalle Valo 
5255e3dd157SKalle Valo err_dma_idx:
5265e3dd157SKalle Valo 	dma_free_coherent(htt->ar->dev,
5275e3dd157SKalle Valo 			  (htt->rx_ring.size *
5285e3dd157SKalle Valo 			   sizeof(htt->rx_ring.paddrs_ring)),
5295e3dd157SKalle Valo 			  htt->rx_ring.paddrs_ring,
5305e3dd157SKalle Valo 			  htt->rx_ring.base_paddr);
5315e3dd157SKalle Valo err_dma_ring:
5325e3dd157SKalle Valo 	kfree(htt->rx_ring.netbufs_ring);
5335e3dd157SKalle Valo err_netbuf:
5345e3dd157SKalle Valo 	return -ENOMEM;
5355e3dd157SKalle Valo }
5365e3dd157SKalle Valo 
5377aa7a72aSMichal Kazior static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
5387aa7a72aSMichal Kazior 					  enum htt_rx_mpdu_encrypt_type type)
5395e3dd157SKalle Valo {
5405e3dd157SKalle Valo 	switch (type) {
541890d3b2aSMichal Kazior 	case HTT_RX_MPDU_ENCRYPT_NONE:
542890d3b2aSMichal Kazior 		return 0;
5435e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_WEP40:
5445e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_WEP104:
545890d3b2aSMichal Kazior 		return IEEE80211_WEP_IV_LEN;
5465e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
5475e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
548890d3b2aSMichal Kazior 		return IEEE80211_TKIP_IV_LEN;
5495e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
550890d3b2aSMichal Kazior 		return IEEE80211_CCMP_HDR_LEN;
551890d3b2aSMichal Kazior 	case HTT_RX_MPDU_ENCRYPT_WEP128:
552890d3b2aSMichal Kazior 	case HTT_RX_MPDU_ENCRYPT_WAPI:
553890d3b2aSMichal Kazior 		break;
554890d3b2aSMichal Kazior 	}
555890d3b2aSMichal Kazior 
556890d3b2aSMichal Kazior 	ath10k_warn(ar, "unsupported encryption type %d\n", type);
5575e3dd157SKalle Valo 	return 0;
5585e3dd157SKalle Valo }
5595e3dd157SKalle Valo 
560890d3b2aSMichal Kazior #define MICHAEL_MIC_LEN 8
5615e3dd157SKalle Valo 
5627aa7a72aSMichal Kazior static int ath10k_htt_rx_crypto_tail_len(struct ath10k *ar,
5637aa7a72aSMichal Kazior 					 enum htt_rx_mpdu_encrypt_type type)
5645e3dd157SKalle Valo {
5655e3dd157SKalle Valo 	switch (type) {
5665e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_NONE:
567890d3b2aSMichal Kazior 		return 0;
5685e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_WEP40:
5695e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_WEP104:
570890d3b2aSMichal Kazior 		return IEEE80211_WEP_ICV_LEN;
5715e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
5725e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
573890d3b2aSMichal Kazior 		return IEEE80211_TKIP_ICV_LEN;
5745e3dd157SKalle Valo 	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
575890d3b2aSMichal Kazior 		return IEEE80211_CCMP_MIC_LEN;
576890d3b2aSMichal Kazior 	case HTT_RX_MPDU_ENCRYPT_WEP128:
577890d3b2aSMichal Kazior 	case HTT_RX_MPDU_ENCRYPT_WAPI:
578890d3b2aSMichal Kazior 		break;
5795e3dd157SKalle Valo 	}
5805e3dd157SKalle Valo 
581890d3b2aSMichal Kazior 	ath10k_warn(ar, "unsupported encryption type %d\n", type);
5825e3dd157SKalle Valo 	return 0;
5835e3dd157SKalle Valo }
5845e3dd157SKalle Valo 
585f6dc2095SMichal Kazior struct amsdu_subframe_hdr {
586f6dc2095SMichal Kazior 	u8 dst[ETH_ALEN];
587f6dc2095SMichal Kazior 	u8 src[ETH_ALEN];
588f6dc2095SMichal Kazior 	__be16 len;
589f6dc2095SMichal Kazior } __packed;
590f6dc2095SMichal Kazior 
5916986fdd6SMichal Kazior #define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)
5926986fdd6SMichal Kazior 
59387326c97SJanusz Dziedzic static void ath10k_htt_rx_h_rates(struct ath10k *ar,
594b9fd8a84SMichal Kazior 				  struct ieee80211_rx_status *status,
595b9fd8a84SMichal Kazior 				  struct htt_rx_desc *rxd)
59673539b40SJanusz Dziedzic {
5975528e032SMichal Kazior 	struct ieee80211_supported_band *sband;
5985528e032SMichal Kazior 	u8 cck, rate, bw, sgi, mcs, nss;
59973539b40SJanusz Dziedzic 	u8 preamble = 0;
6006986fdd6SMichal Kazior 	u8 group_id;
601b9fd8a84SMichal Kazior 	u32 info1, info2, info3;
60273539b40SJanusz Dziedzic 
603b9fd8a84SMichal Kazior 	info1 = __le32_to_cpu(rxd->ppdu_start.info1);
604b9fd8a84SMichal Kazior 	info2 = __le32_to_cpu(rxd->ppdu_start.info2);
605b9fd8a84SMichal Kazior 	info3 = __le32_to_cpu(rxd->ppdu_start.info3);
606b9fd8a84SMichal Kazior 
607b9fd8a84SMichal Kazior 	preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
60873539b40SJanusz Dziedzic 
60973539b40SJanusz Dziedzic 	switch (preamble) {
61073539b40SJanusz Dziedzic 	case HTT_RX_LEGACY:
6115528e032SMichal Kazior 		/* To get legacy rate index band is required. Since band can't
6125528e032SMichal Kazior 		 * be undefined check if freq is non-zero.
6135528e032SMichal Kazior 		 */
6145528e032SMichal Kazior 		if (!status->freq)
6155528e032SMichal Kazior 			return;
6165528e032SMichal Kazior 
617b9fd8a84SMichal Kazior 		cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
618b9fd8a84SMichal Kazior 		rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
6195528e032SMichal Kazior 		rate &= ~RX_PPDU_START_RATE_FLAG;
62073539b40SJanusz Dziedzic 
6215528e032SMichal Kazior 		sband = &ar->mac.sbands[status->band];
6224b7f353bSYanbo Li 		status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate, cck);
62373539b40SJanusz Dziedzic 		break;
62473539b40SJanusz Dziedzic 	case HTT_RX_HT:
62573539b40SJanusz Dziedzic 	case HTT_RX_HT_WITH_TXBF:
626b9fd8a84SMichal Kazior 		/* HT-SIG - Table 20-11 in info2 and info3 */
627b9fd8a84SMichal Kazior 		mcs = info2 & 0x1F;
62873539b40SJanusz Dziedzic 		nss = mcs >> 3;
629b9fd8a84SMichal Kazior 		bw = (info2 >> 7) & 1;
630b9fd8a84SMichal Kazior 		sgi = (info3 >> 7) & 1;
63173539b40SJanusz Dziedzic 
63273539b40SJanusz Dziedzic 		status->rate_idx = mcs;
63373539b40SJanusz Dziedzic 		status->flag |= RX_FLAG_HT;
63473539b40SJanusz Dziedzic 		if (sgi)
63573539b40SJanusz Dziedzic 			status->flag |= RX_FLAG_SHORT_GI;
63673539b40SJanusz Dziedzic 		if (bw)
63773539b40SJanusz Dziedzic 			status->flag |= RX_FLAG_40MHZ;
63873539b40SJanusz Dziedzic 		break;
63973539b40SJanusz Dziedzic 	case HTT_RX_VHT:
64073539b40SJanusz Dziedzic 	case HTT_RX_VHT_WITH_TXBF:
641b9fd8a84SMichal Kazior 		/* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
64273539b40SJanusz Dziedzic 		   TODO check this */
643b9fd8a84SMichal Kazior 		bw = info2 & 3;
644b9fd8a84SMichal Kazior 		sgi = info3 & 1;
6456986fdd6SMichal Kazior 		group_id = (info2 >> 4) & 0x3F;
6466986fdd6SMichal Kazior 
6476986fdd6SMichal Kazior 		if (GROUP_ID_IS_SU_MIMO(group_id)) {
6486986fdd6SMichal Kazior 			mcs = (info3 >> 4) & 0x0F;
6496986fdd6SMichal Kazior 			nss = ((info2 >> 10) & 0x07) + 1;
6506986fdd6SMichal Kazior 		} else {
6516986fdd6SMichal Kazior 			/* Hardware doesn't decode VHT-SIG-B into Rx descriptor
6526986fdd6SMichal Kazior 			 * so it's impossible to decode MCS. Also since
6536986fdd6SMichal Kazior 			 * firmware consumes Group Id Management frames host
6546986fdd6SMichal Kazior 			 * has no knowledge regarding group/user position
6556986fdd6SMichal Kazior 			 * mapping so it's impossible to pick the correct Nsts
6566986fdd6SMichal Kazior 			 * from VHT-SIG-A1.
6576986fdd6SMichal Kazior 			 *
6586986fdd6SMichal Kazior 			 * Bandwidth and SGI are valid so report the rateinfo
6596986fdd6SMichal Kazior 			 * on best-effort basis.
6606986fdd6SMichal Kazior 			 */
6616986fdd6SMichal Kazior 			mcs = 0;
6626986fdd6SMichal Kazior 			nss = 1;
6636986fdd6SMichal Kazior 		}
66473539b40SJanusz Dziedzic 
6656ccea107SManikanta Pubbisetty 		if (mcs > 0x09) {
6666ccea107SManikanta Pubbisetty 			ath10k_warn(ar, "invalid MCS received %u\n", mcs);
6676ccea107SManikanta Pubbisetty 			ath10k_warn(ar, "rxd %08x mpdu start %08x %08x msdu start %08x %08x ppdu start %08x %08x %08x %08x %08x\n",
6686ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->attention.flags),
6696ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->mpdu_start.info0),
6706ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->mpdu_start.info1),
6716ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->msdu_start.common.info0),
6726ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->msdu_start.common.info1),
6736ccea107SManikanta Pubbisetty 				    rxd->ppdu_start.info0,
6746ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->ppdu_start.info1),
6756ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->ppdu_start.info2),
6766ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->ppdu_start.info3),
6776ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->ppdu_start.info4));
6786ccea107SManikanta Pubbisetty 
6796ccea107SManikanta Pubbisetty 			ath10k_warn(ar, "msdu end %08x mpdu end %08x\n",
6806ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->msdu_end.common.info0),
6816ccea107SManikanta Pubbisetty 				    __le32_to_cpu(rxd->mpdu_end.info0));
6826ccea107SManikanta Pubbisetty 
6836ccea107SManikanta Pubbisetty 			ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL,
6846ccea107SManikanta Pubbisetty 					"rx desc msdu payload: ",
6856ccea107SManikanta Pubbisetty 					rxd->msdu_payload, 50);
6866ccea107SManikanta Pubbisetty 		}
6876ccea107SManikanta Pubbisetty 
68873539b40SJanusz Dziedzic 		status->rate_idx = mcs;
68973539b40SJanusz Dziedzic 		status->vht_nss = nss;
69073539b40SJanusz Dziedzic 
69173539b40SJanusz Dziedzic 		if (sgi)
69273539b40SJanusz Dziedzic 			status->flag |= RX_FLAG_SHORT_GI;
69373539b40SJanusz Dziedzic 
69473539b40SJanusz Dziedzic 		switch (bw) {
69573539b40SJanusz Dziedzic 		/* 20MHZ */
69673539b40SJanusz Dziedzic 		case 0:
69773539b40SJanusz Dziedzic 			break;
69873539b40SJanusz Dziedzic 		/* 40MHZ */
69973539b40SJanusz Dziedzic 		case 1:
70073539b40SJanusz Dziedzic 			status->flag |= RX_FLAG_40MHZ;
70173539b40SJanusz Dziedzic 			break;
70273539b40SJanusz Dziedzic 		/* 80MHZ */
70373539b40SJanusz Dziedzic 		case 2:
70473539b40SJanusz Dziedzic 			status->vht_flag |= RX_VHT_FLAG_80MHZ;
705bc1efd73SSebastian Gottschall 			break;
706bc1efd73SSebastian Gottschall 		case 3:
707bc1efd73SSebastian Gottschall 			status->vht_flag |= RX_VHT_FLAG_160MHZ;
708bc1efd73SSebastian Gottschall 			break;
70973539b40SJanusz Dziedzic 		}
71073539b40SJanusz Dziedzic 
71173539b40SJanusz Dziedzic 		status->flag |= RX_FLAG_VHT;
71273539b40SJanusz Dziedzic 		break;
71373539b40SJanusz Dziedzic 	default:
71473539b40SJanusz Dziedzic 		break;
71573539b40SJanusz Dziedzic 	}
71673539b40SJanusz Dziedzic }
71773539b40SJanusz Dziedzic 
718500ff9f9SMichal Kazior static struct ieee80211_channel *
719500ff9f9SMichal Kazior ath10k_htt_rx_h_peer_channel(struct ath10k *ar, struct htt_rx_desc *rxd)
720500ff9f9SMichal Kazior {
721500ff9f9SMichal Kazior 	struct ath10k_peer *peer;
722500ff9f9SMichal Kazior 	struct ath10k_vif *arvif;
723500ff9f9SMichal Kazior 	struct cfg80211_chan_def def;
724500ff9f9SMichal Kazior 	u16 peer_id;
725500ff9f9SMichal Kazior 
726500ff9f9SMichal Kazior 	lockdep_assert_held(&ar->data_lock);
727500ff9f9SMichal Kazior 
728500ff9f9SMichal Kazior 	if (!rxd)
729500ff9f9SMichal Kazior 		return NULL;
730500ff9f9SMichal Kazior 
731500ff9f9SMichal Kazior 	if (rxd->attention.flags &
732500ff9f9SMichal Kazior 	    __cpu_to_le32(RX_ATTENTION_FLAGS_PEER_IDX_INVALID))
733500ff9f9SMichal Kazior 		return NULL;
734500ff9f9SMichal Kazior 
7351f5dbfbbSPeter Oh 	if (!(rxd->msdu_end.common.info0 &
736500ff9f9SMichal Kazior 	      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU)))
737500ff9f9SMichal Kazior 		return NULL;
738500ff9f9SMichal Kazior 
739500ff9f9SMichal Kazior 	peer_id = MS(__le32_to_cpu(rxd->mpdu_start.info0),
740500ff9f9SMichal Kazior 		     RX_MPDU_START_INFO0_PEER_IDX);
741500ff9f9SMichal Kazior 
742500ff9f9SMichal Kazior 	peer = ath10k_peer_find_by_id(ar, peer_id);
743500ff9f9SMichal Kazior 	if (!peer)
744500ff9f9SMichal Kazior 		return NULL;
745500ff9f9SMichal Kazior 
746500ff9f9SMichal Kazior 	arvif = ath10k_get_arvif(ar, peer->vdev_id);
747500ff9f9SMichal Kazior 	if (WARN_ON_ONCE(!arvif))
748500ff9f9SMichal Kazior 		return NULL;
749500ff9f9SMichal Kazior 
750569fba2cSMohammed Shafi Shajakhan 	if (ath10k_mac_vif_chan(arvif->vif, &def))
751500ff9f9SMichal Kazior 		return NULL;
752500ff9f9SMichal Kazior 
753500ff9f9SMichal Kazior 	return def.chan;
754500ff9f9SMichal Kazior }
755500ff9f9SMichal Kazior 
756500ff9f9SMichal Kazior static struct ieee80211_channel *
757500ff9f9SMichal Kazior ath10k_htt_rx_h_vdev_channel(struct ath10k *ar, u32 vdev_id)
758500ff9f9SMichal Kazior {
759500ff9f9SMichal Kazior 	struct ath10k_vif *arvif;
760500ff9f9SMichal Kazior 	struct cfg80211_chan_def def;
761500ff9f9SMichal Kazior 
762500ff9f9SMichal Kazior 	lockdep_assert_held(&ar->data_lock);
763500ff9f9SMichal Kazior 
764500ff9f9SMichal Kazior 	list_for_each_entry(arvif, &ar->arvifs, list) {
765500ff9f9SMichal Kazior 		if (arvif->vdev_id == vdev_id &&
766500ff9f9SMichal Kazior 		    ath10k_mac_vif_chan(arvif->vif, &def) == 0)
767500ff9f9SMichal Kazior 			return def.chan;
768500ff9f9SMichal Kazior 	}
769500ff9f9SMichal Kazior 
770500ff9f9SMichal Kazior 	return NULL;
771500ff9f9SMichal Kazior }
772500ff9f9SMichal Kazior 
773500ff9f9SMichal Kazior static void
774500ff9f9SMichal Kazior ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw *hw,
775500ff9f9SMichal Kazior 			      struct ieee80211_chanctx_conf *conf,
776500ff9f9SMichal Kazior 			      void *data)
777500ff9f9SMichal Kazior {
778500ff9f9SMichal Kazior 	struct cfg80211_chan_def *def = data;
779500ff9f9SMichal Kazior 
780500ff9f9SMichal Kazior 	*def = conf->def;
781500ff9f9SMichal Kazior }
782500ff9f9SMichal Kazior 
783500ff9f9SMichal Kazior static struct ieee80211_channel *
784500ff9f9SMichal Kazior ath10k_htt_rx_h_any_channel(struct ath10k *ar)
785500ff9f9SMichal Kazior {
786500ff9f9SMichal Kazior 	struct cfg80211_chan_def def = {};
787500ff9f9SMichal Kazior 
788500ff9f9SMichal Kazior 	ieee80211_iter_chan_contexts_atomic(ar->hw,
789500ff9f9SMichal Kazior 					    ath10k_htt_rx_h_any_chan_iter,
790500ff9f9SMichal Kazior 					    &def);
791500ff9f9SMichal Kazior 
792500ff9f9SMichal Kazior 	return def.chan;
793500ff9f9SMichal Kazior }
794500ff9f9SMichal Kazior 
79536653f05SJanusz Dziedzic static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
796500ff9f9SMichal Kazior 				    struct ieee80211_rx_status *status,
797500ff9f9SMichal Kazior 				    struct htt_rx_desc *rxd,
798500ff9f9SMichal Kazior 				    u32 vdev_id)
79936653f05SJanusz Dziedzic {
80036653f05SJanusz Dziedzic 	struct ieee80211_channel *ch;
80136653f05SJanusz Dziedzic 
80236653f05SJanusz Dziedzic 	spin_lock_bh(&ar->data_lock);
80336653f05SJanusz Dziedzic 	ch = ar->scan_channel;
80436653f05SJanusz Dziedzic 	if (!ch)
80536653f05SJanusz Dziedzic 		ch = ar->rx_channel;
806500ff9f9SMichal Kazior 	if (!ch)
807500ff9f9SMichal Kazior 		ch = ath10k_htt_rx_h_peer_channel(ar, rxd);
808500ff9f9SMichal Kazior 	if (!ch)
809500ff9f9SMichal Kazior 		ch = ath10k_htt_rx_h_vdev_channel(ar, vdev_id);
810500ff9f9SMichal Kazior 	if (!ch)
811500ff9f9SMichal Kazior 		ch = ath10k_htt_rx_h_any_channel(ar);
8122ce9b25cSRajkumar Manoharan 	if (!ch)
8132ce9b25cSRajkumar Manoharan 		ch = ar->tgt_oper_chan;
81436653f05SJanusz Dziedzic 	spin_unlock_bh(&ar->data_lock);
81536653f05SJanusz Dziedzic 
81636653f05SJanusz Dziedzic 	if (!ch)
81736653f05SJanusz Dziedzic 		return false;
81836653f05SJanusz Dziedzic 
81936653f05SJanusz Dziedzic 	status->band = ch->band;
82036653f05SJanusz Dziedzic 	status->freq = ch->center_freq;
82136653f05SJanusz Dziedzic 
82236653f05SJanusz Dziedzic 	return true;
82336653f05SJanusz Dziedzic }
82436653f05SJanusz Dziedzic 
825b9fd8a84SMichal Kazior static void ath10k_htt_rx_h_signal(struct ath10k *ar,
826b9fd8a84SMichal Kazior 				   struct ieee80211_rx_status *status,
827b9fd8a84SMichal Kazior 				   struct htt_rx_desc *rxd)
828b9fd8a84SMichal Kazior {
829b9fd8a84SMichal Kazior 	/* FIXME: Get real NF */
830b9fd8a84SMichal Kazior 	status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
831b9fd8a84SMichal Kazior 			 rxd->ppdu_start.rssi_comb;
832b9fd8a84SMichal Kazior 	status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
833b9fd8a84SMichal Kazior }
834b9fd8a84SMichal Kazior 
835b9fd8a84SMichal Kazior static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
836b9fd8a84SMichal Kazior 				    struct ieee80211_rx_status *status,
837b9fd8a84SMichal Kazior 				    struct htt_rx_desc *rxd)
838b9fd8a84SMichal Kazior {
839b9fd8a84SMichal Kazior 	/* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
840b9fd8a84SMichal Kazior 	 * means all prior MSDUs in a PPDU are reported to mac80211 without the
841b9fd8a84SMichal Kazior 	 * TSF. Is it worth holding frames until end of PPDU is known?
842b9fd8a84SMichal Kazior 	 *
843b9fd8a84SMichal Kazior 	 * FIXME: Can we get/compute 64bit TSF?
844b9fd8a84SMichal Kazior 	 */
8453ec79e3aSMichal Kazior 	status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
846b9fd8a84SMichal Kazior 	status->flag |= RX_FLAG_MACTIME_END;
847b9fd8a84SMichal Kazior }
848b9fd8a84SMichal Kazior 
849b9fd8a84SMichal Kazior static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
850b9fd8a84SMichal Kazior 				 struct sk_buff_head *amsdu,
851500ff9f9SMichal Kazior 				 struct ieee80211_rx_status *status,
852500ff9f9SMichal Kazior 				 u32 vdev_id)
853b9fd8a84SMichal Kazior {
854b9fd8a84SMichal Kazior 	struct sk_buff *first;
855b9fd8a84SMichal Kazior 	struct htt_rx_desc *rxd;
856b9fd8a84SMichal Kazior 	bool is_first_ppdu;
857b9fd8a84SMichal Kazior 	bool is_last_ppdu;
858b9fd8a84SMichal Kazior 
859b9fd8a84SMichal Kazior 	if (skb_queue_empty(amsdu))
860b9fd8a84SMichal Kazior 		return;
861b9fd8a84SMichal Kazior 
862b9fd8a84SMichal Kazior 	first = skb_peek(amsdu);
863b9fd8a84SMichal Kazior 	rxd = (void *)first->data - sizeof(*rxd);
864b9fd8a84SMichal Kazior 
865b9fd8a84SMichal Kazior 	is_first_ppdu = !!(rxd->attention.flags &
866b9fd8a84SMichal Kazior 			   __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
867b9fd8a84SMichal Kazior 	is_last_ppdu = !!(rxd->attention.flags &
868b9fd8a84SMichal Kazior 			  __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));
869b9fd8a84SMichal Kazior 
870b9fd8a84SMichal Kazior 	if (is_first_ppdu) {
871b9fd8a84SMichal Kazior 		/* New PPDU starts so clear out the old per-PPDU status. */
872b9fd8a84SMichal Kazior 		status->freq = 0;
873b9fd8a84SMichal Kazior 		status->rate_idx = 0;
874b9fd8a84SMichal Kazior 		status->vht_nss = 0;
875b9fd8a84SMichal Kazior 		status->vht_flag &= ~RX_VHT_FLAG_80MHZ;
876b9fd8a84SMichal Kazior 		status->flag &= ~(RX_FLAG_HT |
877b9fd8a84SMichal Kazior 				  RX_FLAG_VHT |
878b9fd8a84SMichal Kazior 				  RX_FLAG_SHORT_GI |
879b9fd8a84SMichal Kazior 				  RX_FLAG_40MHZ |
880b9fd8a84SMichal Kazior 				  RX_FLAG_MACTIME_END);
881b9fd8a84SMichal Kazior 		status->flag |= RX_FLAG_NO_SIGNAL_VAL;
882b9fd8a84SMichal Kazior 
883b9fd8a84SMichal Kazior 		ath10k_htt_rx_h_signal(ar, status, rxd);
884500ff9f9SMichal Kazior 		ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
885b9fd8a84SMichal Kazior 		ath10k_htt_rx_h_rates(ar, status, rxd);
886b9fd8a84SMichal Kazior 	}
887b9fd8a84SMichal Kazior 
888b9fd8a84SMichal Kazior 	if (is_last_ppdu)
889b9fd8a84SMichal Kazior 		ath10k_htt_rx_h_mactime(ar, status, rxd);
890b9fd8a84SMichal Kazior }
891b9fd8a84SMichal Kazior 
89276f5329aSJanusz Dziedzic static const char * const tid_to_ac[] = {
89376f5329aSJanusz Dziedzic 	"BE",
89476f5329aSJanusz Dziedzic 	"BK",
89576f5329aSJanusz Dziedzic 	"BK",
89676f5329aSJanusz Dziedzic 	"BE",
89776f5329aSJanusz Dziedzic 	"VI",
89876f5329aSJanusz Dziedzic 	"VI",
89976f5329aSJanusz Dziedzic 	"VO",
90076f5329aSJanusz Dziedzic 	"VO",
90176f5329aSJanusz Dziedzic };
90276f5329aSJanusz Dziedzic 
90376f5329aSJanusz Dziedzic static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
90476f5329aSJanusz Dziedzic {
90576f5329aSJanusz Dziedzic 	u8 *qc;
90676f5329aSJanusz Dziedzic 	int tid;
90776f5329aSJanusz Dziedzic 
90876f5329aSJanusz Dziedzic 	if (!ieee80211_is_data_qos(hdr->frame_control))
90976f5329aSJanusz Dziedzic 		return "";
91076f5329aSJanusz Dziedzic 
91176f5329aSJanusz Dziedzic 	qc = ieee80211_get_qos_ctl(hdr);
91276f5329aSJanusz Dziedzic 	tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
91376f5329aSJanusz Dziedzic 	if (tid < 8)
91476f5329aSJanusz Dziedzic 		snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
91576f5329aSJanusz Dziedzic 	else
91676f5329aSJanusz Dziedzic 		snprintf(out, size, "tid %d", tid);
91776f5329aSJanusz Dziedzic 
91876f5329aSJanusz Dziedzic 	return out;
91976f5329aSJanusz Dziedzic }
92076f5329aSJanusz Dziedzic 
92185f6d7cfSJanusz Dziedzic static void ath10k_process_rx(struct ath10k *ar,
92285f6d7cfSJanusz Dziedzic 			      struct ieee80211_rx_status *rx_status,
92385f6d7cfSJanusz Dziedzic 			      struct sk_buff *skb)
92473539b40SJanusz Dziedzic {
92573539b40SJanusz Dziedzic 	struct ieee80211_rx_status *status;
92676f5329aSJanusz Dziedzic 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
92776f5329aSJanusz Dziedzic 	char tid[32];
92873539b40SJanusz Dziedzic 
92985f6d7cfSJanusz Dziedzic 	status = IEEE80211_SKB_RXCB(skb);
93085f6d7cfSJanusz Dziedzic 	*status = *rx_status;
93173539b40SJanusz Dziedzic 
9327aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_DATA,
933bc1efd73SSebastian Gottschall 		   "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%llx fcs-err %i mic-err %i amsdu-more %i\n",
93485f6d7cfSJanusz Dziedzic 		   skb,
93585f6d7cfSJanusz Dziedzic 		   skb->len,
93676f5329aSJanusz Dziedzic 		   ieee80211_get_SA(hdr),
93776f5329aSJanusz Dziedzic 		   ath10k_get_tid(hdr, tid, sizeof(tid)),
93876f5329aSJanusz Dziedzic 		   is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
93976f5329aSJanusz Dziedzic 							"mcast" : "ucast",
94076f5329aSJanusz Dziedzic 		   (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
941026441c9SMohammed Shafi Shajakhan 		   (status->flag & (RX_FLAG_HT | RX_FLAG_VHT)) == 0 ?
942026441c9SMohammed Shafi Shajakhan 							"legacy" : "",
94373539b40SJanusz Dziedzic 		   status->flag & RX_FLAG_HT ? "ht" : "",
94473539b40SJanusz Dziedzic 		   status->flag & RX_FLAG_VHT ? "vht" : "",
94573539b40SJanusz Dziedzic 		   status->flag & RX_FLAG_40MHZ ? "40" : "",
94673539b40SJanusz Dziedzic 		   status->vht_flag & RX_VHT_FLAG_80MHZ ? "80" : "",
947bc1efd73SSebastian Gottschall 		   status->vht_flag & RX_VHT_FLAG_160MHZ ? "160" : "",
94873539b40SJanusz Dziedzic 		   status->flag & RX_FLAG_SHORT_GI ? "sgi " : "",
94973539b40SJanusz Dziedzic 		   status->rate_idx,
95073539b40SJanusz Dziedzic 		   status->vht_nss,
95173539b40SJanusz Dziedzic 		   status->freq,
95287326c97SJanusz Dziedzic 		   status->band, status->flag,
95378433f96SJanusz Dziedzic 		   !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
95476f5329aSJanusz Dziedzic 		   !!(status->flag & RX_FLAG_MMIC_ERROR),
95576f5329aSJanusz Dziedzic 		   !!(status->flag & RX_FLAG_AMSDU_MORE));
9567aa7a72aSMichal Kazior 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
95785f6d7cfSJanusz Dziedzic 			skb->data, skb->len);
9585ce8e7fdSRajkumar Manoharan 	trace_ath10k_rx_hdr(ar, skb->data, skb->len);
9595ce8e7fdSRajkumar Manoharan 	trace_ath10k_rx_payload(ar, skb->data, skb->len);
96073539b40SJanusz Dziedzic 
9613c97f5deSRajkumar Manoharan 	ieee80211_rx_napi(ar->hw, NULL, skb, &ar->napi);
96273539b40SJanusz Dziedzic }
96373539b40SJanusz Dziedzic 
96448f4ca34SMichal Kazior static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
96548f4ca34SMichal Kazior 				      struct ieee80211_hdr *hdr)
966d960c369SMichal Kazior {
96748f4ca34SMichal Kazior 	int len = ieee80211_hdrlen(hdr->frame_control);
96848f4ca34SMichal Kazior 
96948f4ca34SMichal Kazior 	if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
970c4cdf753SKalle Valo 		      ar->running_fw->fw_file.fw_features))
97148f4ca34SMichal Kazior 		len = round_up(len, 4);
97248f4ca34SMichal Kazior 
97348f4ca34SMichal Kazior 	return len;
974d960c369SMichal Kazior }
975d960c369SMichal Kazior 
976581c25f8SMichal Kazior static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
977581c25f8SMichal Kazior 					struct sk_buff *msdu,
978581c25f8SMichal Kazior 					struct ieee80211_rx_status *status,
979581c25f8SMichal Kazior 					enum htt_rx_mpdu_encrypt_type enctype,
980581c25f8SMichal Kazior 					bool is_decrypted)
9815e3dd157SKalle Valo {
982f6dc2095SMichal Kazior 	struct ieee80211_hdr *hdr;
983581c25f8SMichal Kazior 	struct htt_rx_desc *rxd;
984581c25f8SMichal Kazior 	size_t hdr_len;
985581c25f8SMichal Kazior 	size_t crypto_len;
986581c25f8SMichal Kazior 	bool is_first;
987581c25f8SMichal Kazior 	bool is_last;
9885e3dd157SKalle Valo 
989581c25f8SMichal Kazior 	rxd = (void *)msdu->data - sizeof(*rxd);
9901f5dbfbbSPeter Oh 	is_first = !!(rxd->msdu_end.common.info0 &
991581c25f8SMichal Kazior 		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
9921f5dbfbbSPeter Oh 	is_last = !!(rxd->msdu_end.common.info0 &
993581c25f8SMichal Kazior 		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
9949aa505d2SMichal Kazior 
995581c25f8SMichal Kazior 	/* Delivered decapped frame:
996581c25f8SMichal Kazior 	 * [802.11 header]
997581c25f8SMichal Kazior 	 * [crypto param] <-- can be trimmed if !fcs_err &&
998581c25f8SMichal Kazior 	 *                    !decrypt_err && !peer_idx_invalid
999581c25f8SMichal Kazior 	 * [amsdu header] <-- only if A-MSDU
1000581c25f8SMichal Kazior 	 * [rfc1042/llc]
1001581c25f8SMichal Kazior 	 * [payload]
1002581c25f8SMichal Kazior 	 * [FCS] <-- at end, needs to be trimmed
1003581c25f8SMichal Kazior 	 */
10045e3dd157SKalle Valo 
1005581c25f8SMichal Kazior 	/* This probably shouldn't happen but warn just in case */
1006581c25f8SMichal Kazior 	if (unlikely(WARN_ON_ONCE(!is_first)))
1007581c25f8SMichal Kazior 		return;
1008581c25f8SMichal Kazior 
1009581c25f8SMichal Kazior 	/* This probably shouldn't happen but warn just in case */
1010581c25f8SMichal Kazior 	if (unlikely(WARN_ON_ONCE(!(is_first && is_last))))
1011581c25f8SMichal Kazior 		return;
1012581c25f8SMichal Kazior 
1013581c25f8SMichal Kazior 	skb_trim(msdu, msdu->len - FCS_LEN);
1014581c25f8SMichal Kazior 
1015581c25f8SMichal Kazior 	/* In most cases this will be true for sniffed frames. It makes sense
1016ccec9038SDavid Liu 	 * to deliver them as-is without stripping the crypto param. This is
1017ccec9038SDavid Liu 	 * necessary for software based decryption.
1018581c25f8SMichal Kazior 	 *
1019581c25f8SMichal Kazior 	 * If there's no error then the frame is decrypted. At least that is
1020581c25f8SMichal Kazior 	 * the case for frames that come in via fragmented rx indication.
1021581c25f8SMichal Kazior 	 */
1022581c25f8SMichal Kazior 	if (!is_decrypted)
1023581c25f8SMichal Kazior 		return;
1024581c25f8SMichal Kazior 
1025581c25f8SMichal Kazior 	/* The payload is decrypted so strip crypto params. Start from tail
1026581c25f8SMichal Kazior 	 * since hdr is used to compute some stuff.
1027581c25f8SMichal Kazior 	 */
1028581c25f8SMichal Kazior 
1029581c25f8SMichal Kazior 	hdr = (void *)msdu->data;
1030581c25f8SMichal Kazior 
1031581c25f8SMichal Kazior 	/* Tail */
103260549cabSGrzegorz Bajorski 	if (status->flag & RX_FLAG_IV_STRIPPED)
103360549cabSGrzegorz Bajorski 		skb_trim(msdu, msdu->len -
103460549cabSGrzegorz Bajorski 			 ath10k_htt_rx_crypto_tail_len(ar, enctype));
1035581c25f8SMichal Kazior 
1036581c25f8SMichal Kazior 	/* MMIC */
103760549cabSGrzegorz Bajorski 	if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
103860549cabSGrzegorz Bajorski 	    !ieee80211_has_morefrags(hdr->frame_control) &&
1039581c25f8SMichal Kazior 	    enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
1040581c25f8SMichal Kazior 		skb_trim(msdu, msdu->len - 8);
1041581c25f8SMichal Kazior 
1042581c25f8SMichal Kazior 	/* Head */
104360549cabSGrzegorz Bajorski 	if (status->flag & RX_FLAG_IV_STRIPPED) {
1044f6dc2095SMichal Kazior 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1045581c25f8SMichal Kazior 		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
10465e3dd157SKalle Valo 
1047581c25f8SMichal Kazior 		memmove((void *)msdu->data + crypto_len,
1048581c25f8SMichal Kazior 			(void *)msdu->data, hdr_len);
1049581c25f8SMichal Kazior 		skb_pull(msdu, crypto_len);
10505e3dd157SKalle Valo 	}
105160549cabSGrzegorz Bajorski }
10525e3dd157SKalle Valo 
1053581c25f8SMichal Kazior static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
1054581c25f8SMichal Kazior 					  struct sk_buff *msdu,
1055581c25f8SMichal Kazior 					  struct ieee80211_rx_status *status,
1056581c25f8SMichal Kazior 					  const u8 first_hdr[64])
1057581c25f8SMichal Kazior {
1058581c25f8SMichal Kazior 	struct ieee80211_hdr *hdr;
10599e19e132SVasanthakumar Thiagarajan 	struct htt_rx_desc *rxd;
1060581c25f8SMichal Kazior 	size_t hdr_len;
1061581c25f8SMichal Kazior 	u8 da[ETH_ALEN];
1062581c25f8SMichal Kazior 	u8 sa[ETH_ALEN];
10639e19e132SVasanthakumar Thiagarajan 	int l3_pad_bytes;
1064581c25f8SMichal Kazior 
1065581c25f8SMichal Kazior 	/* Delivered decapped frame:
1066581c25f8SMichal Kazior 	 * [nwifi 802.11 header] <-- replaced with 802.11 hdr
1067581c25f8SMichal Kazior 	 * [rfc1042/llc]
1068581c25f8SMichal Kazior 	 *
1069581c25f8SMichal Kazior 	 * Note: The nwifi header doesn't have QoS Control and is
1070581c25f8SMichal Kazior 	 * (always?) a 3addr frame.
1071581c25f8SMichal Kazior 	 *
1072581c25f8SMichal Kazior 	 * Note2: There's no A-MSDU subframe header. Even if it's part
1073581c25f8SMichal Kazior 	 * of an A-MSDU.
1074581c25f8SMichal Kazior 	 */
1075581c25f8SMichal Kazior 
107672bdeb86SMichal Kazior 	/* pull decapped header and copy SA & DA */
10779e19e132SVasanthakumar Thiagarajan 	rxd = (void *)msdu->data - sizeof(*rxd);
10789e19e132SVasanthakumar Thiagarajan 
10799e19e132SVasanthakumar Thiagarajan 	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
10809e19e132SVasanthakumar Thiagarajan 	skb_put(msdu, l3_pad_bytes);
10819e19e132SVasanthakumar Thiagarajan 
10829e19e132SVasanthakumar Thiagarajan 	hdr = (struct ieee80211_hdr *)(msdu->data + l3_pad_bytes);
1083b8d55fcaSYanbo Li 
108448f4ca34SMichal Kazior 	hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
1085b25f32cbSKalle Valo 	ether_addr_copy(da, ieee80211_get_DA(hdr));
1086b25f32cbSKalle Valo 	ether_addr_copy(sa, ieee80211_get_SA(hdr));
1087581c25f8SMichal Kazior 	skb_pull(msdu, hdr_len);
1088784f69d3SMichal Kazior 
1089784f69d3SMichal Kazior 	/* push original 802.11 header */
1090581c25f8SMichal Kazior 	hdr = (struct ieee80211_hdr *)first_hdr;
1091784f69d3SMichal Kazior 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1092581c25f8SMichal Kazior 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1093784f69d3SMichal Kazior 
109472bdeb86SMichal Kazior 	/* original 802.11 header has a different DA and in
109572bdeb86SMichal Kazior 	 * case of 4addr it may also have different SA
109672bdeb86SMichal Kazior 	 */
1097581c25f8SMichal Kazior 	hdr = (struct ieee80211_hdr *)msdu->data;
1098b25f32cbSKalle Valo 	ether_addr_copy(ieee80211_get_DA(hdr), da);
1099b25f32cbSKalle Valo 	ether_addr_copy(ieee80211_get_SA(hdr), sa);
11005e3dd157SKalle Valo }
11015e3dd157SKalle Valo 
1102581c25f8SMichal Kazior static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
1103581c25f8SMichal Kazior 					  struct sk_buff *msdu,
1104581c25f8SMichal Kazior 					  enum htt_rx_mpdu_encrypt_type enctype)
11055e3dd157SKalle Valo {
11065e3dd157SKalle Valo 	struct ieee80211_hdr *hdr;
1107581c25f8SMichal Kazior 	struct htt_rx_desc *rxd;
1108581c25f8SMichal Kazior 	size_t hdr_len, crypto_len;
1109e3fbf8d2SMichal Kazior 	void *rfc1042;
1110581c25f8SMichal Kazior 	bool is_first, is_last, is_amsdu;
11112f38c3c0SVasanthakumar Thiagarajan 	int bytes_aligned = ar->hw_params.decap_align_bytes;
11125e3dd157SKalle Valo 
1113581c25f8SMichal Kazior 	rxd = (void *)msdu->data - sizeof(*rxd);
1114581c25f8SMichal Kazior 	hdr = (void *)rxd->rx_hdr_status;
11155e3dd157SKalle Valo 
11161f5dbfbbSPeter Oh 	is_first = !!(rxd->msdu_end.common.info0 &
1117581c25f8SMichal Kazior 		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
11181f5dbfbbSPeter Oh 	is_last = !!(rxd->msdu_end.common.info0 &
1119581c25f8SMichal Kazior 		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
1120581c25f8SMichal Kazior 	is_amsdu = !(is_first && is_last);
1121e3fbf8d2SMichal Kazior 
1122e3fbf8d2SMichal Kazior 	rfc1042 = hdr;
1123e3fbf8d2SMichal Kazior 
1124581c25f8SMichal Kazior 	if (is_first) {
1125581c25f8SMichal Kazior 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1126581c25f8SMichal Kazior 		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1127e3fbf8d2SMichal Kazior 
11282f38c3c0SVasanthakumar Thiagarajan 		rfc1042 += round_up(hdr_len, bytes_aligned) +
11292f38c3c0SVasanthakumar Thiagarajan 			   round_up(crypto_len, bytes_aligned);
11305e3dd157SKalle Valo 	}
11315e3dd157SKalle Valo 
1132581c25f8SMichal Kazior 	if (is_amsdu)
1133581c25f8SMichal Kazior 		rfc1042 += sizeof(struct amsdu_subframe_hdr);
1134f6dc2095SMichal Kazior 
1135581c25f8SMichal Kazior 	return rfc1042;
1136581c25f8SMichal Kazior }
1137581c25f8SMichal Kazior 
1138581c25f8SMichal Kazior static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
1139581c25f8SMichal Kazior 					struct sk_buff *msdu,
1140581c25f8SMichal Kazior 					struct ieee80211_rx_status *status,
1141581c25f8SMichal Kazior 					const u8 first_hdr[64],
1142581c25f8SMichal Kazior 					enum htt_rx_mpdu_encrypt_type enctype)
1143581c25f8SMichal Kazior {
1144581c25f8SMichal Kazior 	struct ieee80211_hdr *hdr;
1145581c25f8SMichal Kazior 	struct ethhdr *eth;
1146581c25f8SMichal Kazior 	size_t hdr_len;
1147581c25f8SMichal Kazior 	void *rfc1042;
1148581c25f8SMichal Kazior 	u8 da[ETH_ALEN];
1149581c25f8SMichal Kazior 	u8 sa[ETH_ALEN];
11509e19e132SVasanthakumar Thiagarajan 	int l3_pad_bytes;
11519e19e132SVasanthakumar Thiagarajan 	struct htt_rx_desc *rxd;
1152581c25f8SMichal Kazior 
1153581c25f8SMichal Kazior 	/* Delivered decapped frame:
1154581c25f8SMichal Kazior 	 * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
1155581c25f8SMichal Kazior 	 * [payload]
1156581c25f8SMichal Kazior 	 */
1157581c25f8SMichal Kazior 
1158581c25f8SMichal Kazior 	rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
1159581c25f8SMichal Kazior 	if (WARN_ON_ONCE(!rfc1042))
1160581c25f8SMichal Kazior 		return;
1161581c25f8SMichal Kazior 
11629e19e132SVasanthakumar Thiagarajan 	rxd = (void *)msdu->data - sizeof(*rxd);
11639e19e132SVasanthakumar Thiagarajan 	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
11649e19e132SVasanthakumar Thiagarajan 	skb_put(msdu, l3_pad_bytes);
11659e19e132SVasanthakumar Thiagarajan 	skb_pull(msdu, l3_pad_bytes);
11669e19e132SVasanthakumar Thiagarajan 
1167581c25f8SMichal Kazior 	/* pull decapped header and copy SA & DA */
1168581c25f8SMichal Kazior 	eth = (struct ethhdr *)msdu->data;
1169581c25f8SMichal Kazior 	ether_addr_copy(da, eth->h_dest);
1170581c25f8SMichal Kazior 	ether_addr_copy(sa, eth->h_source);
1171581c25f8SMichal Kazior 	skb_pull(msdu, sizeof(struct ethhdr));
1172581c25f8SMichal Kazior 
1173581c25f8SMichal Kazior 	/* push rfc1042/llc/snap */
1174581c25f8SMichal Kazior 	memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
1175581c25f8SMichal Kazior 	       sizeof(struct rfc1042_hdr));
1176581c25f8SMichal Kazior 
1177581c25f8SMichal Kazior 	/* push original 802.11 header */
1178581c25f8SMichal Kazior 	hdr = (struct ieee80211_hdr *)first_hdr;
1179581c25f8SMichal Kazior 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1180581c25f8SMichal Kazior 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1181581c25f8SMichal Kazior 
1182581c25f8SMichal Kazior 	/* original 802.11 header has a different DA and in
1183581c25f8SMichal Kazior 	 * case of 4addr it may also have different SA
1184581c25f8SMichal Kazior 	 */
1185581c25f8SMichal Kazior 	hdr = (struct ieee80211_hdr *)msdu->data;
1186581c25f8SMichal Kazior 	ether_addr_copy(ieee80211_get_DA(hdr), da);
1187581c25f8SMichal Kazior 	ether_addr_copy(ieee80211_get_SA(hdr), sa);
1188581c25f8SMichal Kazior }
1189581c25f8SMichal Kazior 
1190581c25f8SMichal Kazior static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
1191581c25f8SMichal Kazior 					 struct sk_buff *msdu,
1192581c25f8SMichal Kazior 					 struct ieee80211_rx_status *status,
1193581c25f8SMichal Kazior 					 const u8 first_hdr[64])
1194581c25f8SMichal Kazior {
1195581c25f8SMichal Kazior 	struct ieee80211_hdr *hdr;
1196581c25f8SMichal Kazior 	size_t hdr_len;
11979e19e132SVasanthakumar Thiagarajan 	int l3_pad_bytes;
11989e19e132SVasanthakumar Thiagarajan 	struct htt_rx_desc *rxd;
1199581c25f8SMichal Kazior 
1200581c25f8SMichal Kazior 	/* Delivered decapped frame:
1201581c25f8SMichal Kazior 	 * [amsdu header] <-- replaced with 802.11 hdr
1202581c25f8SMichal Kazior 	 * [rfc1042/llc]
1203581c25f8SMichal Kazior 	 * [payload]
1204581c25f8SMichal Kazior 	 */
1205581c25f8SMichal Kazior 
12069e19e132SVasanthakumar Thiagarajan 	rxd = (void *)msdu->data - sizeof(*rxd);
12079e19e132SVasanthakumar Thiagarajan 	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
12089e19e132SVasanthakumar Thiagarajan 
12099e19e132SVasanthakumar Thiagarajan 	skb_put(msdu, l3_pad_bytes);
12109e19e132SVasanthakumar Thiagarajan 	skb_pull(msdu, sizeof(struct amsdu_subframe_hdr) + l3_pad_bytes);
1211581c25f8SMichal Kazior 
1212581c25f8SMichal Kazior 	hdr = (struct ieee80211_hdr *)first_hdr;
1213581c25f8SMichal Kazior 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1214581c25f8SMichal Kazior 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1215581c25f8SMichal Kazior }
1216581c25f8SMichal Kazior 
1217581c25f8SMichal Kazior static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
1218581c25f8SMichal Kazior 				    struct sk_buff *msdu,
1219581c25f8SMichal Kazior 				    struct ieee80211_rx_status *status,
1220581c25f8SMichal Kazior 				    u8 first_hdr[64],
1221581c25f8SMichal Kazior 				    enum htt_rx_mpdu_encrypt_type enctype,
1222581c25f8SMichal Kazior 				    bool is_decrypted)
1223581c25f8SMichal Kazior {
1224581c25f8SMichal Kazior 	struct htt_rx_desc *rxd;
1225581c25f8SMichal Kazior 	enum rx_msdu_decap_format decap;
1226581c25f8SMichal Kazior 
1227581c25f8SMichal Kazior 	/* First msdu's decapped header:
1228581c25f8SMichal Kazior 	 * [802.11 header] <-- padded to 4 bytes long
1229581c25f8SMichal Kazior 	 * [crypto param] <-- padded to 4 bytes long
1230581c25f8SMichal Kazior 	 * [amsdu header] <-- only if A-MSDU
1231581c25f8SMichal Kazior 	 * [rfc1042/llc]
1232581c25f8SMichal Kazior 	 *
1233581c25f8SMichal Kazior 	 * Other (2nd, 3rd, ..) msdu's decapped header:
1234581c25f8SMichal Kazior 	 * [amsdu header] <-- only if A-MSDU
1235581c25f8SMichal Kazior 	 * [rfc1042/llc]
1236581c25f8SMichal Kazior 	 */
1237581c25f8SMichal Kazior 
1238581c25f8SMichal Kazior 	rxd = (void *)msdu->data - sizeof(*rxd);
12391f5dbfbbSPeter Oh 	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
1240581c25f8SMichal Kazior 		   RX_MSDU_START_INFO1_DECAP_FORMAT);
1241581c25f8SMichal Kazior 
1242581c25f8SMichal Kazior 	switch (decap) {
1243581c25f8SMichal Kazior 	case RX_MSDU_DECAP_RAW:
1244581c25f8SMichal Kazior 		ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
1245581c25f8SMichal Kazior 					    is_decrypted);
1246581c25f8SMichal Kazior 		break;
1247581c25f8SMichal Kazior 	case RX_MSDU_DECAP_NATIVE_WIFI:
1248581c25f8SMichal Kazior 		ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr);
1249581c25f8SMichal Kazior 		break;
1250581c25f8SMichal Kazior 	case RX_MSDU_DECAP_ETHERNET2_DIX:
1251581c25f8SMichal Kazior 		ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
1252581c25f8SMichal Kazior 		break;
1253581c25f8SMichal Kazior 	case RX_MSDU_DECAP_8023_SNAP_LLC:
1254581c25f8SMichal Kazior 		ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr);
1255581c25f8SMichal Kazior 		break;
1256581c25f8SMichal Kazior 	}
12575e3dd157SKalle Valo }
12585e3dd157SKalle Valo 
1259605f81aaSMichal Kazior static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
1260605f81aaSMichal Kazior {
1261605f81aaSMichal Kazior 	struct htt_rx_desc *rxd;
1262605f81aaSMichal Kazior 	u32 flags, info;
1263605f81aaSMichal Kazior 	bool is_ip4, is_ip6;
1264605f81aaSMichal Kazior 	bool is_tcp, is_udp;
1265605f81aaSMichal Kazior 	bool ip_csum_ok, tcpudp_csum_ok;
1266605f81aaSMichal Kazior 
1267605f81aaSMichal Kazior 	rxd = (void *)skb->data - sizeof(*rxd);
1268605f81aaSMichal Kazior 	flags = __le32_to_cpu(rxd->attention.flags);
12691f5dbfbbSPeter Oh 	info = __le32_to_cpu(rxd->msdu_start.common.info1);
1270605f81aaSMichal Kazior 
1271605f81aaSMichal Kazior 	is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
1272605f81aaSMichal Kazior 	is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
1273605f81aaSMichal Kazior 	is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
1274605f81aaSMichal Kazior 	is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
1275605f81aaSMichal Kazior 	ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
1276605f81aaSMichal Kazior 	tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
1277605f81aaSMichal Kazior 
1278605f81aaSMichal Kazior 	if (!is_ip4 && !is_ip6)
1279605f81aaSMichal Kazior 		return CHECKSUM_NONE;
1280605f81aaSMichal Kazior 	if (!is_tcp && !is_udp)
1281605f81aaSMichal Kazior 		return CHECKSUM_NONE;
1282605f81aaSMichal Kazior 	if (!ip_csum_ok)
1283605f81aaSMichal Kazior 		return CHECKSUM_NONE;
1284605f81aaSMichal Kazior 	if (!tcpudp_csum_ok)
1285605f81aaSMichal Kazior 		return CHECKSUM_NONE;
1286605f81aaSMichal Kazior 
1287605f81aaSMichal Kazior 	return CHECKSUM_UNNECESSARY;
1288605f81aaSMichal Kazior }
1289605f81aaSMichal Kazior 
1290581c25f8SMichal Kazior static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
1291581c25f8SMichal Kazior {
1292581c25f8SMichal Kazior 	msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
1293581c25f8SMichal Kazior }
1294581c25f8SMichal Kazior 
1295581c25f8SMichal Kazior static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
1296581c25f8SMichal Kazior 				 struct sk_buff_head *amsdu,
1297581c25f8SMichal Kazior 				 struct ieee80211_rx_status *status)
1298581c25f8SMichal Kazior {
1299581c25f8SMichal Kazior 	struct sk_buff *first;
1300581c25f8SMichal Kazior 	struct sk_buff *last;
1301581c25f8SMichal Kazior 	struct sk_buff *msdu;
1302581c25f8SMichal Kazior 	struct htt_rx_desc *rxd;
1303581c25f8SMichal Kazior 	struct ieee80211_hdr *hdr;
1304581c25f8SMichal Kazior 	enum htt_rx_mpdu_encrypt_type enctype;
1305581c25f8SMichal Kazior 	u8 first_hdr[64];
1306581c25f8SMichal Kazior 	u8 *qos;
1307581c25f8SMichal Kazior 	size_t hdr_len;
1308581c25f8SMichal Kazior 	bool has_fcs_err;
1309581c25f8SMichal Kazior 	bool has_crypto_err;
1310581c25f8SMichal Kazior 	bool has_tkip_err;
1311581c25f8SMichal Kazior 	bool has_peer_idx_invalid;
1312581c25f8SMichal Kazior 	bool is_decrypted;
131360549cabSGrzegorz Bajorski 	bool is_mgmt;
1314581c25f8SMichal Kazior 	u32 attention;
1315581c25f8SMichal Kazior 
1316581c25f8SMichal Kazior 	if (skb_queue_empty(amsdu))
1317581c25f8SMichal Kazior 		return;
1318581c25f8SMichal Kazior 
1319581c25f8SMichal Kazior 	first = skb_peek(amsdu);
1320581c25f8SMichal Kazior 	rxd = (void *)first->data - sizeof(*rxd);
1321581c25f8SMichal Kazior 
132260549cabSGrzegorz Bajorski 	is_mgmt = !!(rxd->attention.flags &
132360549cabSGrzegorz Bajorski 		     __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));
132460549cabSGrzegorz Bajorski 
1325581c25f8SMichal Kazior 	enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1326581c25f8SMichal Kazior 		     RX_MPDU_START_INFO0_ENCRYPT_TYPE);
1327581c25f8SMichal Kazior 
1328581c25f8SMichal Kazior 	/* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
1329581c25f8SMichal Kazior 	 * decapped header. It'll be used for undecapping of each MSDU.
1330581c25f8SMichal Kazior 	 */
1331581c25f8SMichal Kazior 	hdr = (void *)rxd->rx_hdr_status;
1332581c25f8SMichal Kazior 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1333581c25f8SMichal Kazior 	memcpy(first_hdr, hdr, hdr_len);
1334581c25f8SMichal Kazior 
1335581c25f8SMichal Kazior 	/* Each A-MSDU subframe will use the original header as the base and be
1336581c25f8SMichal Kazior 	 * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
1337581c25f8SMichal Kazior 	 */
1338581c25f8SMichal Kazior 	hdr = (void *)first_hdr;
1339581c25f8SMichal Kazior 	qos = ieee80211_get_qos_ctl(hdr);
1340581c25f8SMichal Kazior 	qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1341581c25f8SMichal Kazior 
1342581c25f8SMichal Kazior 	/* Some attention flags are valid only in the last MSDU. */
1343581c25f8SMichal Kazior 	last = skb_peek_tail(amsdu);
1344581c25f8SMichal Kazior 	rxd = (void *)last->data - sizeof(*rxd);
1345581c25f8SMichal Kazior 	attention = __le32_to_cpu(rxd->attention.flags);
1346581c25f8SMichal Kazior 
1347581c25f8SMichal Kazior 	has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
1348581c25f8SMichal Kazior 	has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
1349581c25f8SMichal Kazior 	has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
1350581c25f8SMichal Kazior 	has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);
1351581c25f8SMichal Kazior 
1352581c25f8SMichal Kazior 	/* Note: If hardware captures an encrypted frame that it can't decrypt,
1353581c25f8SMichal Kazior 	 * e.g. due to fcs error, missing peer or invalid key data it will
1354581c25f8SMichal Kazior 	 * report the frame as raw.
1355581c25f8SMichal Kazior 	 */
1356581c25f8SMichal Kazior 	is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
1357581c25f8SMichal Kazior 			!has_fcs_err &&
1358581c25f8SMichal Kazior 			!has_crypto_err &&
1359581c25f8SMichal Kazior 			!has_peer_idx_invalid);
1360581c25f8SMichal Kazior 
1361581c25f8SMichal Kazior 	/* Clear per-MPDU flags while leaving per-PPDU flags intact. */
1362581c25f8SMichal Kazior 	status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
1363581c25f8SMichal Kazior 			  RX_FLAG_MMIC_ERROR |
1364581c25f8SMichal Kazior 			  RX_FLAG_DECRYPTED |
1365581c25f8SMichal Kazior 			  RX_FLAG_IV_STRIPPED |
136660549cabSGrzegorz Bajorski 			  RX_FLAG_ONLY_MONITOR |
1367581c25f8SMichal Kazior 			  RX_FLAG_MMIC_STRIPPED);
1368581c25f8SMichal Kazior 
1369581c25f8SMichal Kazior 	if (has_fcs_err)
1370581c25f8SMichal Kazior 		status->flag |= RX_FLAG_FAILED_FCS_CRC;
1371581c25f8SMichal Kazior 
1372581c25f8SMichal Kazior 	if (has_tkip_err)
1373581c25f8SMichal Kazior 		status->flag |= RX_FLAG_MMIC_ERROR;
1374581c25f8SMichal Kazior 
137560549cabSGrzegorz Bajorski 	/* Firmware reports all necessary management frames via WMI already.
137660549cabSGrzegorz Bajorski 	 * They are not reported to monitor interfaces at all so pass the ones
137760549cabSGrzegorz Bajorski 	 * coming via HTT to monitor interfaces instead. This simplifies
137860549cabSGrzegorz Bajorski 	 * matters a lot.
137960549cabSGrzegorz Bajorski 	 */
138060549cabSGrzegorz Bajorski 	if (is_mgmt)
138160549cabSGrzegorz Bajorski 		status->flag |= RX_FLAG_ONLY_MONITOR;
138260549cabSGrzegorz Bajorski 
138360549cabSGrzegorz Bajorski 	if (is_decrypted) {
138460549cabSGrzegorz Bajorski 		status->flag |= RX_FLAG_DECRYPTED;
138560549cabSGrzegorz Bajorski 
138660549cabSGrzegorz Bajorski 		if (likely(!is_mgmt))
138760549cabSGrzegorz Bajorski 			status->flag |= RX_FLAG_IV_STRIPPED |
1388581c25f8SMichal Kazior 					RX_FLAG_MMIC_STRIPPED;
138960549cabSGrzegorz Bajorski }
1390581c25f8SMichal Kazior 
1391581c25f8SMichal Kazior 	skb_queue_walk(amsdu, msdu) {
1392581c25f8SMichal Kazior 		ath10k_htt_rx_h_csum_offload(msdu);
1393581c25f8SMichal Kazior 		ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
1394581c25f8SMichal Kazior 					is_decrypted);
1395581c25f8SMichal Kazior 
1396581c25f8SMichal Kazior 		/* Undecapping involves copying the original 802.11 header back
1397581c25f8SMichal Kazior 		 * to sk_buff. If frame is protected and hardware has decrypted
1398581c25f8SMichal Kazior 		 * it then remove the protected bit.
1399581c25f8SMichal Kazior 		 */
1400581c25f8SMichal Kazior 		if (!is_decrypted)
1401581c25f8SMichal Kazior 			continue;
140260549cabSGrzegorz Bajorski 		if (is_mgmt)
140360549cabSGrzegorz Bajorski 			continue;
1404581c25f8SMichal Kazior 
1405581c25f8SMichal Kazior 		hdr = (void *)msdu->data;
1406581c25f8SMichal Kazior 		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1407581c25f8SMichal Kazior 	}
1408581c25f8SMichal Kazior }
1409581c25f8SMichal Kazior 
1410581c25f8SMichal Kazior static void ath10k_htt_rx_h_deliver(struct ath10k *ar,
1411581c25f8SMichal Kazior 				    struct sk_buff_head *amsdu,
1412581c25f8SMichal Kazior 				    struct ieee80211_rx_status *status)
1413581c25f8SMichal Kazior {
1414581c25f8SMichal Kazior 	struct sk_buff *msdu;
1415581c25f8SMichal Kazior 
1416581c25f8SMichal Kazior 	while ((msdu = __skb_dequeue(amsdu))) {
1417581c25f8SMichal Kazior 		/* Setup per-MSDU flags */
1418581c25f8SMichal Kazior 		if (skb_queue_empty(amsdu))
1419581c25f8SMichal Kazior 			status->flag &= ~RX_FLAG_AMSDU_MORE;
1420581c25f8SMichal Kazior 		else
1421581c25f8SMichal Kazior 			status->flag |= RX_FLAG_AMSDU_MORE;
1422581c25f8SMichal Kazior 
1423581c25f8SMichal Kazior 		ath10k_process_rx(ar, status, msdu);
1424581c25f8SMichal Kazior 	}
1425581c25f8SMichal Kazior }
1426581c25f8SMichal Kazior 
14279aa505d2SMichal Kazior static int ath10k_unchain_msdu(struct sk_buff_head *amsdu)
1428bfa35368SBen Greear {
14299aa505d2SMichal Kazior 	struct sk_buff *skb, *first;
1430bfa35368SBen Greear 	int space;
1431bfa35368SBen Greear 	int total_len = 0;
1432bfa35368SBen Greear 
1433bfa35368SBen Greear 	/* TODO:  Might could optimize this by using
1434bfa35368SBen Greear 	 * skb_try_coalesce or similar method to
1435bfa35368SBen Greear 	 * decrease copying, or maybe get mac80211 to
1436bfa35368SBen Greear 	 * provide a way to just receive a list of
1437bfa35368SBen Greear 	 * skb?
1438bfa35368SBen Greear 	 */
1439bfa35368SBen Greear 
14409aa505d2SMichal Kazior 	first = __skb_dequeue(amsdu);
1441bfa35368SBen Greear 
1442bfa35368SBen Greear 	/* Allocate total length all at once. */
14439aa505d2SMichal Kazior 	skb_queue_walk(amsdu, skb)
14449aa505d2SMichal Kazior 		total_len += skb->len;
1445bfa35368SBen Greear 
14469aa505d2SMichal Kazior 	space = total_len - skb_tailroom(first);
1447bfa35368SBen Greear 	if ((space > 0) &&
14489aa505d2SMichal Kazior 	    (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
1449bfa35368SBen Greear 		/* TODO:  bump some rx-oom error stat */
1450bfa35368SBen Greear 		/* put it back together so we can free the
1451bfa35368SBen Greear 		 * whole list at once.
1452bfa35368SBen Greear 		 */
14539aa505d2SMichal Kazior 		__skb_queue_head(amsdu, first);
1454bfa35368SBen Greear 		return -1;
1455bfa35368SBen Greear 	}
1456bfa35368SBen Greear 
1457bfa35368SBen Greear 	/* Walk list again, copying contents into
1458bfa35368SBen Greear 	 * msdu_head
1459bfa35368SBen Greear 	 */
14609aa505d2SMichal Kazior 	while ((skb = __skb_dequeue(amsdu))) {
14619aa505d2SMichal Kazior 		skb_copy_from_linear_data(skb, skb_put(first, skb->len),
14629aa505d2SMichal Kazior 					  skb->len);
14639aa505d2SMichal Kazior 		dev_kfree_skb_any(skb);
1464bfa35368SBen Greear 	}
1465bfa35368SBen Greear 
14669aa505d2SMichal Kazior 	__skb_queue_head(amsdu, first);
1467bfa35368SBen Greear 	return 0;
1468bfa35368SBen Greear }
1469bfa35368SBen Greear 
1470581c25f8SMichal Kazior static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
14717543d116SMohammed Shafi Shajakhan 				    struct sk_buff_head *amsdu)
14722acc4eb2SJanusz Dziedzic {
1473581c25f8SMichal Kazior 	struct sk_buff *first;
1474581c25f8SMichal Kazior 	struct htt_rx_desc *rxd;
1475581c25f8SMichal Kazior 	enum rx_msdu_decap_format decap;
14767aa7a72aSMichal Kazior 
1477581c25f8SMichal Kazior 	first = skb_peek(amsdu);
1478581c25f8SMichal Kazior 	rxd = (void *)first->data - sizeof(*rxd);
14791f5dbfbbSPeter Oh 	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
1480581c25f8SMichal Kazior 		   RX_MSDU_START_INFO1_DECAP_FORMAT);
1481581c25f8SMichal Kazior 
1482581c25f8SMichal Kazior 	/* FIXME: Current unchaining logic can only handle simple case of raw
1483581c25f8SMichal Kazior 	 * msdu chaining. If decapping is other than raw the chaining may be
1484581c25f8SMichal Kazior 	 * more complex and this isn't handled by the current code. Don't even
1485581c25f8SMichal Kazior 	 * try re-constructing such frames - it'll be pretty much garbage.
1486581c25f8SMichal Kazior 	 */
1487581c25f8SMichal Kazior 	if (decap != RX_MSDU_DECAP_RAW ||
1488581c25f8SMichal Kazior 	    skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
1489581c25f8SMichal Kazior 		__skb_queue_purge(amsdu);
1490581c25f8SMichal Kazior 		return;
1491581c25f8SMichal Kazior 	}
1492581c25f8SMichal Kazior 
1493581c25f8SMichal Kazior 	ath10k_unchain_msdu(amsdu);
1494581c25f8SMichal Kazior }
1495581c25f8SMichal Kazior 
1496581c25f8SMichal Kazior static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
1497581c25f8SMichal Kazior 					struct sk_buff_head *amsdu,
1498581c25f8SMichal Kazior 					struct ieee80211_rx_status *rx_status)
1499581c25f8SMichal Kazior {
1500581c25f8SMichal Kazior 	/* FIXME: It might be a good idea to do some fuzzy-testing to drop
1501581c25f8SMichal Kazior 	 * invalid/dangerous frames.
1502581c25f8SMichal Kazior 	 */
1503581c25f8SMichal Kazior 
1504581c25f8SMichal Kazior 	if (!rx_status->freq) {
1505581c25f8SMichal Kazior 		ath10k_warn(ar, "no channel configured; ignoring frame(s)!\n");
15062acc4eb2SJanusz Dziedzic 		return false;
15072acc4eb2SJanusz Dziedzic 	}
15082acc4eb2SJanusz Dziedzic 
1509581c25f8SMichal Kazior 	if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
1510581c25f8SMichal Kazior 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
15112acc4eb2SJanusz Dziedzic 		return false;
15122acc4eb2SJanusz Dziedzic 	}
15132acc4eb2SJanusz Dziedzic 
15142acc4eb2SJanusz Dziedzic 	return true;
15152acc4eb2SJanusz Dziedzic }
15162acc4eb2SJanusz Dziedzic 
1517581c25f8SMichal Kazior static void ath10k_htt_rx_h_filter(struct ath10k *ar,
1518581c25f8SMichal Kazior 				   struct sk_buff_head *amsdu,
1519581c25f8SMichal Kazior 				   struct ieee80211_rx_status *rx_status)
1520581c25f8SMichal Kazior {
1521581c25f8SMichal Kazior 	if (skb_queue_empty(amsdu))
1522581c25f8SMichal Kazior 		return;
1523581c25f8SMichal Kazior 
1524581c25f8SMichal Kazior 	if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
1525581c25f8SMichal Kazior 		return;
1526581c25f8SMichal Kazior 
1527581c25f8SMichal Kazior 	__skb_queue_purge(amsdu);
1528581c25f8SMichal Kazior }
1529581c25f8SMichal Kazior 
153018235664SRajkumar Manoharan static int ath10k_htt_rx_handle_amsdu(struct ath10k_htt *htt)
153118235664SRajkumar Manoharan {
153218235664SRajkumar Manoharan 	struct ath10k *ar = htt->ar;
1533237e15dfSAshok Raj Nagarajan 	struct ieee80211_rx_status *rx_status = &htt->rx_status;
153418235664SRajkumar Manoharan 	struct sk_buff_head amsdu;
15353c97f5deSRajkumar Manoharan 	int ret, num_msdus;
153618235664SRajkumar Manoharan 
153718235664SRajkumar Manoharan 	__skb_queue_head_init(&amsdu);
153818235664SRajkumar Manoharan 
153918235664SRajkumar Manoharan 	spin_lock_bh(&htt->rx_ring.lock);
154018235664SRajkumar Manoharan 	if (htt->rx_confused) {
154118235664SRajkumar Manoharan 		spin_unlock_bh(&htt->rx_ring.lock);
154218235664SRajkumar Manoharan 		return -EIO;
154318235664SRajkumar Manoharan 	}
154418235664SRajkumar Manoharan 	ret = ath10k_htt_rx_amsdu_pop(htt, &amsdu);
154518235664SRajkumar Manoharan 	spin_unlock_bh(&htt->rx_ring.lock);
154618235664SRajkumar Manoharan 
154718235664SRajkumar Manoharan 	if (ret < 0) {
154818235664SRajkumar Manoharan 		ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
154918235664SRajkumar Manoharan 		__skb_queue_purge(&amsdu);
155018235664SRajkumar Manoharan 		/* FIXME: It's probably a good idea to reboot the
155118235664SRajkumar Manoharan 		 * device instead of leaving it inoperable.
155218235664SRajkumar Manoharan 		 */
155318235664SRajkumar Manoharan 		htt->rx_confused = true;
155418235664SRajkumar Manoharan 		return ret;
155518235664SRajkumar Manoharan 	}
155618235664SRajkumar Manoharan 
15573c97f5deSRajkumar Manoharan 	num_msdus = skb_queue_len(&amsdu);
1558237e15dfSAshok Raj Nagarajan 	ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
15597543d116SMohammed Shafi Shajakhan 
15607543d116SMohammed Shafi Shajakhan 	/* only for ret = 1 indicates chained msdus */
15617543d116SMohammed Shafi Shajakhan 	if (ret > 0)
15627543d116SMohammed Shafi Shajakhan 		ath10k_htt_rx_h_unchain(ar, &amsdu);
15637543d116SMohammed Shafi Shajakhan 
1564237e15dfSAshok Raj Nagarajan 	ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
1565237e15dfSAshok Raj Nagarajan 	ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status);
1566237e15dfSAshok Raj Nagarajan 	ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
156718235664SRajkumar Manoharan 
15683c97f5deSRajkumar Manoharan 	return num_msdus;
156918235664SRajkumar Manoharan }
157018235664SRajkumar Manoharan 
15713128b3d8SRajkumar Manoharan static void ath10k_htt_rx_proc_rx_ind(struct ath10k_htt *htt,
15725e3dd157SKalle Valo 				      struct htt_rx_indication *rx)
15735e3dd157SKalle Valo {
15747aa7a72aSMichal Kazior 	struct ath10k *ar = htt->ar;
15755e3dd157SKalle Valo 	struct htt_rx_indication_mpdu_range *mpdu_ranges;
15765e3dd157SKalle Valo 	int num_mpdu_ranges;
157718235664SRajkumar Manoharan 	int i, mpdu_count = 0;
15785e3dd157SKalle Valo 
15795e3dd157SKalle Valo 	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
15805e3dd157SKalle Valo 			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
15815e3dd157SKalle Valo 	mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
15825e3dd157SKalle Valo 
15837aa7a72aSMichal Kazior 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
15845e3dd157SKalle Valo 			rx, sizeof(*rx) +
15855e3dd157SKalle Valo 			(sizeof(struct htt_rx_indication_mpdu_range) *
15865e3dd157SKalle Valo 				num_mpdu_ranges));
15875e3dd157SKalle Valo 
1588d540690dSMichal Kazior 	for (i = 0; i < num_mpdu_ranges; i++)
1589d540690dSMichal Kazior 		mpdu_count += mpdu_ranges[i].mpdu_count;
1590d540690dSMichal Kazior 
15913128b3d8SRajkumar Manoharan 	atomic_add(mpdu_count, &htt->num_mpdus_ready);
15925e3dd157SKalle Valo }
15935e3dd157SKalle Valo 
159459465fe4SRajkumar Manoharan static void ath10k_htt_rx_tx_compl_ind(struct ath10k *ar,
15956c5151a9SMichal Kazior 				       struct sk_buff *skb)
15966c5151a9SMichal Kazior {
15976c5151a9SMichal Kazior 	struct ath10k_htt *htt = &ar->htt;
15986c5151a9SMichal Kazior 	struct htt_resp *resp = (struct htt_resp *)skb->data;
15996c5151a9SMichal Kazior 	struct htt_tx_done tx_done = {};
16006c5151a9SMichal Kazior 	int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
16016c5151a9SMichal Kazior 	__le16 msdu_id;
16026c5151a9SMichal Kazior 	int i;
16036c5151a9SMichal Kazior 
16046c5151a9SMichal Kazior 	switch (status) {
16056c5151a9SMichal Kazior 	case HTT_DATA_TX_STATUS_NO_ACK:
160659465fe4SRajkumar Manoharan 		tx_done.status = HTT_TX_COMPL_STATE_NOACK;
16076c5151a9SMichal Kazior 		break;
16086c5151a9SMichal Kazior 	case HTT_DATA_TX_STATUS_OK:
160959465fe4SRajkumar Manoharan 		tx_done.status = HTT_TX_COMPL_STATE_ACK;
16106c5151a9SMichal Kazior 		break;
16116c5151a9SMichal Kazior 	case HTT_DATA_TX_STATUS_DISCARD:
16126c5151a9SMichal Kazior 	case HTT_DATA_TX_STATUS_POSTPONE:
16136c5151a9SMichal Kazior 	case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
161459465fe4SRajkumar Manoharan 		tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
16156c5151a9SMichal Kazior 		break;
16166c5151a9SMichal Kazior 	default:
16177aa7a72aSMichal Kazior 		ath10k_warn(ar, "unhandled tx completion status %d\n", status);
161859465fe4SRajkumar Manoharan 		tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
16196c5151a9SMichal Kazior 		break;
16206c5151a9SMichal Kazior 	}
16216c5151a9SMichal Kazior 
16227aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
16236c5151a9SMichal Kazior 		   resp->data_tx_completion.num_msdus);
16246c5151a9SMichal Kazior 
16256c5151a9SMichal Kazior 	for (i = 0; i < resp->data_tx_completion.num_msdus; i++) {
16266c5151a9SMichal Kazior 		msdu_id = resp->data_tx_completion.msdus[i];
16276c5151a9SMichal Kazior 		tx_done.msdu_id = __le16_to_cpu(msdu_id);
162859465fe4SRajkumar Manoharan 
162959465fe4SRajkumar Manoharan 		/* kfifo_put: In practice firmware shouldn't fire off per-CE
163059465fe4SRajkumar Manoharan 		 * interrupt and main interrupt (MSI/-X range case) for the same
163159465fe4SRajkumar Manoharan 		 * HTC service so it should be safe to use kfifo_put w/o lock.
163259465fe4SRajkumar Manoharan 		 *
163359465fe4SRajkumar Manoharan 		 * From kfifo_put() documentation:
163459465fe4SRajkumar Manoharan 		 *  Note that with only one concurrent reader and one concurrent
163559465fe4SRajkumar Manoharan 		 *  writer, you don't need extra locking to use these macro.
163659465fe4SRajkumar Manoharan 		 */
163759465fe4SRajkumar Manoharan 		if (!kfifo_put(&htt->txdone_fifo, tx_done)) {
163859465fe4SRajkumar Manoharan 			ath10k_warn(ar, "txdone fifo overrun, msdu_id %d status %d\n",
163959465fe4SRajkumar Manoharan 				    tx_done.msdu_id, tx_done.status);
16406c5151a9SMichal Kazior 			ath10k_txrx_tx_unref(htt, &tx_done);
16416c5151a9SMichal Kazior 		}
16426c5151a9SMichal Kazior 	}
164359465fe4SRajkumar Manoharan }
16446c5151a9SMichal Kazior 
1645aa5b4fbcSMichal Kazior static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
1646aa5b4fbcSMichal Kazior {
1647aa5b4fbcSMichal Kazior 	struct htt_rx_addba *ev = &resp->rx_addba;
1648aa5b4fbcSMichal Kazior 	struct ath10k_peer *peer;
1649aa5b4fbcSMichal Kazior 	struct ath10k_vif *arvif;
1650aa5b4fbcSMichal Kazior 	u16 info0, tid, peer_id;
1651aa5b4fbcSMichal Kazior 
1652aa5b4fbcSMichal Kazior 	info0 = __le16_to_cpu(ev->info0);
1653aa5b4fbcSMichal Kazior 	tid = MS(info0, HTT_RX_BA_INFO0_TID);
1654aa5b4fbcSMichal Kazior 	peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1655aa5b4fbcSMichal Kazior 
16567aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
1657aa5b4fbcSMichal Kazior 		   "htt rx addba tid %hu peer_id %hu size %hhu\n",
1658aa5b4fbcSMichal Kazior 		   tid, peer_id, ev->window_size);
1659aa5b4fbcSMichal Kazior 
1660aa5b4fbcSMichal Kazior 	spin_lock_bh(&ar->data_lock);
1661aa5b4fbcSMichal Kazior 	peer = ath10k_peer_find_by_id(ar, peer_id);
1662aa5b4fbcSMichal Kazior 	if (!peer) {
16637aa7a72aSMichal Kazior 		ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
1664aa5b4fbcSMichal Kazior 			    peer_id);
1665aa5b4fbcSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
1666aa5b4fbcSMichal Kazior 		return;
1667aa5b4fbcSMichal Kazior 	}
1668aa5b4fbcSMichal Kazior 
1669aa5b4fbcSMichal Kazior 	arvif = ath10k_get_arvif(ar, peer->vdev_id);
1670aa5b4fbcSMichal Kazior 	if (!arvif) {
16717aa7a72aSMichal Kazior 		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
1672aa5b4fbcSMichal Kazior 			    peer->vdev_id);
1673aa5b4fbcSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
1674aa5b4fbcSMichal Kazior 		return;
1675aa5b4fbcSMichal Kazior 	}
1676aa5b4fbcSMichal Kazior 
16777aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
1678aa5b4fbcSMichal Kazior 		   "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
1679aa5b4fbcSMichal Kazior 		   peer->addr, tid, ev->window_size);
1680aa5b4fbcSMichal Kazior 
1681aa5b4fbcSMichal Kazior 	ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1682aa5b4fbcSMichal Kazior 	spin_unlock_bh(&ar->data_lock);
1683aa5b4fbcSMichal Kazior }
1684aa5b4fbcSMichal Kazior 
1685aa5b4fbcSMichal Kazior static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
1686aa5b4fbcSMichal Kazior {
1687aa5b4fbcSMichal Kazior 	struct htt_rx_delba *ev = &resp->rx_delba;
1688aa5b4fbcSMichal Kazior 	struct ath10k_peer *peer;
1689aa5b4fbcSMichal Kazior 	struct ath10k_vif *arvif;
1690aa5b4fbcSMichal Kazior 	u16 info0, tid, peer_id;
1691aa5b4fbcSMichal Kazior 
1692aa5b4fbcSMichal Kazior 	info0 = __le16_to_cpu(ev->info0);
1693aa5b4fbcSMichal Kazior 	tid = MS(info0, HTT_RX_BA_INFO0_TID);
1694aa5b4fbcSMichal Kazior 	peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1695aa5b4fbcSMichal Kazior 
16967aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
1697aa5b4fbcSMichal Kazior 		   "htt rx delba tid %hu peer_id %hu\n",
1698aa5b4fbcSMichal Kazior 		   tid, peer_id);
1699aa5b4fbcSMichal Kazior 
1700aa5b4fbcSMichal Kazior 	spin_lock_bh(&ar->data_lock);
1701aa5b4fbcSMichal Kazior 	peer = ath10k_peer_find_by_id(ar, peer_id);
1702aa5b4fbcSMichal Kazior 	if (!peer) {
17037aa7a72aSMichal Kazior 		ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
1704aa5b4fbcSMichal Kazior 			    peer_id);
1705aa5b4fbcSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
1706aa5b4fbcSMichal Kazior 		return;
1707aa5b4fbcSMichal Kazior 	}
1708aa5b4fbcSMichal Kazior 
1709aa5b4fbcSMichal Kazior 	arvif = ath10k_get_arvif(ar, peer->vdev_id);
1710aa5b4fbcSMichal Kazior 	if (!arvif) {
17117aa7a72aSMichal Kazior 		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
1712aa5b4fbcSMichal Kazior 			    peer->vdev_id);
1713aa5b4fbcSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
1714aa5b4fbcSMichal Kazior 		return;
1715aa5b4fbcSMichal Kazior 	}
1716aa5b4fbcSMichal Kazior 
17177aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
1718aa5b4fbcSMichal Kazior 		   "htt rx stop rx ba session sta %pM tid %hu\n",
1719aa5b4fbcSMichal Kazior 		   peer->addr, tid);
1720aa5b4fbcSMichal Kazior 
1721aa5b4fbcSMichal Kazior 	ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1722aa5b4fbcSMichal Kazior 	spin_unlock_bh(&ar->data_lock);
1723aa5b4fbcSMichal Kazior }
1724aa5b4fbcSMichal Kazior 
1725c545070eSMichal Kazior static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
1726c545070eSMichal Kazior 				       struct sk_buff_head *amsdu)
1727c545070eSMichal Kazior {
1728c545070eSMichal Kazior 	struct sk_buff *msdu;
1729c545070eSMichal Kazior 	struct htt_rx_desc *rxd;
1730c545070eSMichal Kazior 
1731c545070eSMichal Kazior 	if (skb_queue_empty(list))
1732c545070eSMichal Kazior 		return -ENOBUFS;
1733c545070eSMichal Kazior 
1734c545070eSMichal Kazior 	if (WARN_ON(!skb_queue_empty(amsdu)))
1735c545070eSMichal Kazior 		return -EINVAL;
1736c545070eSMichal Kazior 
1737c545070eSMichal Kazior 	while ((msdu = __skb_dequeue(list))) {
1738c545070eSMichal Kazior 		__skb_queue_tail(amsdu, msdu);
1739c545070eSMichal Kazior 
1740c545070eSMichal Kazior 		rxd = (void *)msdu->data - sizeof(*rxd);
17411f5dbfbbSPeter Oh 		if (rxd->msdu_end.common.info0 &
1742c545070eSMichal Kazior 		    __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
1743c545070eSMichal Kazior 			break;
1744c545070eSMichal Kazior 	}
1745c545070eSMichal Kazior 
1746c545070eSMichal Kazior 	msdu = skb_peek_tail(amsdu);
1747c545070eSMichal Kazior 	rxd = (void *)msdu->data - sizeof(*rxd);
17481f5dbfbbSPeter Oh 	if (!(rxd->msdu_end.common.info0 &
1749c545070eSMichal Kazior 	      __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
1750c545070eSMichal Kazior 		skb_queue_splice_init(amsdu, list);
1751c545070eSMichal Kazior 		return -EAGAIN;
1752c545070eSMichal Kazior 	}
1753c545070eSMichal Kazior 
1754c545070eSMichal Kazior 	return 0;
1755c545070eSMichal Kazior }
1756c545070eSMichal Kazior 
1757c545070eSMichal Kazior static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
1758c545070eSMichal Kazior 					    struct sk_buff *skb)
1759c545070eSMichal Kazior {
1760c545070eSMichal Kazior 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1761c545070eSMichal Kazior 
1762c545070eSMichal Kazior 	if (!ieee80211_has_protected(hdr->frame_control))
1763c545070eSMichal Kazior 		return;
1764c545070eSMichal Kazior 
1765c545070eSMichal Kazior 	/* Offloaded frames are already decrypted but firmware insists they are
1766c545070eSMichal Kazior 	 * protected in the 802.11 header. Strip the flag.  Otherwise mac80211
1767c545070eSMichal Kazior 	 * will drop the frame.
1768c545070eSMichal Kazior 	 */
1769c545070eSMichal Kazior 
1770c545070eSMichal Kazior 	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1771c545070eSMichal Kazior 	status->flag |= RX_FLAG_DECRYPTED |
1772c545070eSMichal Kazior 			RX_FLAG_IV_STRIPPED |
1773c545070eSMichal Kazior 			RX_FLAG_MMIC_STRIPPED;
1774c545070eSMichal Kazior }
1775c545070eSMichal Kazior 
17763c97f5deSRajkumar Manoharan static int ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
1777c545070eSMichal Kazior 				      struct sk_buff_head *list)
1778c545070eSMichal Kazior {
1779c545070eSMichal Kazior 	struct ath10k_htt *htt = &ar->htt;
1780c545070eSMichal Kazior 	struct ieee80211_rx_status *status = &htt->rx_status;
1781c545070eSMichal Kazior 	struct htt_rx_offload_msdu *rx;
1782c545070eSMichal Kazior 	struct sk_buff *msdu;
1783c545070eSMichal Kazior 	size_t offset;
17843c97f5deSRajkumar Manoharan 	int num_msdu = 0;
1785c545070eSMichal Kazior 
1786c545070eSMichal Kazior 	while ((msdu = __skb_dequeue(list))) {
1787c545070eSMichal Kazior 		/* Offloaded frames don't have Rx descriptor. Instead they have
1788c545070eSMichal Kazior 		 * a short meta information header.
1789c545070eSMichal Kazior 		 */
1790c545070eSMichal Kazior 
1791c545070eSMichal Kazior 		rx = (void *)msdu->data;
1792c545070eSMichal Kazior 
1793c545070eSMichal Kazior 		skb_put(msdu, sizeof(*rx));
1794c545070eSMichal Kazior 		skb_pull(msdu, sizeof(*rx));
1795c545070eSMichal Kazior 
1796c545070eSMichal Kazior 		if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
1797c545070eSMichal Kazior 			ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
1798c545070eSMichal Kazior 			dev_kfree_skb_any(msdu);
1799c545070eSMichal Kazior 			continue;
1800c545070eSMichal Kazior 		}
1801c545070eSMichal Kazior 
1802c545070eSMichal Kazior 		skb_put(msdu, __le16_to_cpu(rx->msdu_len));
1803c545070eSMichal Kazior 
1804c545070eSMichal Kazior 		/* Offloaded rx header length isn't multiple of 2 nor 4 so the
1805c545070eSMichal Kazior 		 * actual payload is unaligned. Align the frame.  Otherwise
1806c545070eSMichal Kazior 		 * mac80211 complains.  This shouldn't reduce performance much
1807c545070eSMichal Kazior 		 * because these offloaded frames are rare.
1808c545070eSMichal Kazior 		 */
1809c545070eSMichal Kazior 		offset = 4 - ((unsigned long)msdu->data & 3);
1810c545070eSMichal Kazior 		skb_put(msdu, offset);
1811c545070eSMichal Kazior 		memmove(msdu->data + offset, msdu->data, msdu->len);
1812c545070eSMichal Kazior 		skb_pull(msdu, offset);
1813c545070eSMichal Kazior 
1814c545070eSMichal Kazior 		/* FIXME: The frame is NWifi. Re-construct QoS Control
1815c545070eSMichal Kazior 		 * if possible later.
1816c545070eSMichal Kazior 		 */
1817c545070eSMichal Kazior 
1818c545070eSMichal Kazior 		memset(status, 0, sizeof(*status));
1819c545070eSMichal Kazior 		status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1820c545070eSMichal Kazior 
1821c545070eSMichal Kazior 		ath10k_htt_rx_h_rx_offload_prot(status, msdu);
1822500ff9f9SMichal Kazior 		ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
1823c545070eSMichal Kazior 		ath10k_process_rx(ar, status, msdu);
18243c97f5deSRajkumar Manoharan 		num_msdu++;
1825c545070eSMichal Kazior 	}
18263c97f5deSRajkumar Manoharan 	return num_msdu;
1827c545070eSMichal Kazior }
1828c545070eSMichal Kazior 
18293c97f5deSRajkumar Manoharan static int ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
1830c545070eSMichal Kazior {
1831c545070eSMichal Kazior 	struct ath10k_htt *htt = &ar->htt;
1832c545070eSMichal Kazior 	struct htt_resp *resp = (void *)skb->data;
1833c545070eSMichal Kazior 	struct ieee80211_rx_status *status = &htt->rx_status;
1834c545070eSMichal Kazior 	struct sk_buff_head list;
1835c545070eSMichal Kazior 	struct sk_buff_head amsdu;
1836c545070eSMichal Kazior 	u16 peer_id;
1837c545070eSMichal Kazior 	u16 msdu_count;
1838c545070eSMichal Kazior 	u8 vdev_id;
1839c545070eSMichal Kazior 	u8 tid;
1840c545070eSMichal Kazior 	bool offload;
1841c545070eSMichal Kazior 	bool frag;
18423c97f5deSRajkumar Manoharan 	int ret, num_msdus = 0;
1843c545070eSMichal Kazior 
1844c545070eSMichal Kazior 	lockdep_assert_held(&htt->rx_ring.lock);
1845c545070eSMichal Kazior 
1846c545070eSMichal Kazior 	if (htt->rx_confused)
18473c97f5deSRajkumar Manoharan 		return -EIO;
1848c545070eSMichal Kazior 
1849c545070eSMichal Kazior 	skb_pull(skb, sizeof(resp->hdr));
1850c545070eSMichal Kazior 	skb_pull(skb, sizeof(resp->rx_in_ord_ind));
1851c545070eSMichal Kazior 
1852c545070eSMichal Kazior 	peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
1853c545070eSMichal Kazior 	msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
1854c545070eSMichal Kazior 	vdev_id = resp->rx_in_ord_ind.vdev_id;
1855c545070eSMichal Kazior 	tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
1856c545070eSMichal Kazior 	offload = !!(resp->rx_in_ord_ind.info &
1857c545070eSMichal Kazior 			HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
1858c545070eSMichal Kazior 	frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);
1859c545070eSMichal Kazior 
1860c545070eSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
1861c545070eSMichal Kazior 		   "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
1862c545070eSMichal Kazior 		   vdev_id, peer_id, tid, offload, frag, msdu_count);
1863c545070eSMichal Kazior 
1864c545070eSMichal Kazior 	if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs)) {
1865c545070eSMichal Kazior 		ath10k_warn(ar, "dropping invalid in order rx indication\n");
18663c97f5deSRajkumar Manoharan 		return -EINVAL;
1867c545070eSMichal Kazior 	}
1868c545070eSMichal Kazior 
1869c545070eSMichal Kazior 	/* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
1870c545070eSMichal Kazior 	 * extracted and processed.
1871c545070eSMichal Kazior 	 */
1872c545070eSMichal Kazior 	__skb_queue_head_init(&list);
1873c545070eSMichal Kazior 	ret = ath10k_htt_rx_pop_paddr_list(htt, &resp->rx_in_ord_ind, &list);
1874c545070eSMichal Kazior 	if (ret < 0) {
1875c545070eSMichal Kazior 		ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
1876c545070eSMichal Kazior 		htt->rx_confused = true;
18773c97f5deSRajkumar Manoharan 		return -EIO;
1878c545070eSMichal Kazior 	}
1879c545070eSMichal Kazior 
1880c545070eSMichal Kazior 	/* Offloaded frames are very different and need to be handled
1881c545070eSMichal Kazior 	 * separately.
1882c545070eSMichal Kazior 	 */
1883c545070eSMichal Kazior 	if (offload)
18843c97f5deSRajkumar Manoharan 		num_msdus = ath10k_htt_rx_h_rx_offload(ar, &list);
1885c545070eSMichal Kazior 
1886c545070eSMichal Kazior 	while (!skb_queue_empty(&list)) {
1887c545070eSMichal Kazior 		__skb_queue_head_init(&amsdu);
1888c545070eSMichal Kazior 		ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
1889c545070eSMichal Kazior 		switch (ret) {
1890c545070eSMichal Kazior 		case 0:
1891c545070eSMichal Kazior 			/* Note: The in-order indication may report interleaved
1892c545070eSMichal Kazior 			 * frames from different PPDUs meaning reported rx rate
1893c545070eSMichal Kazior 			 * to mac80211 isn't accurate/reliable. It's still
1894c545070eSMichal Kazior 			 * better to report something than nothing though. This
1895c545070eSMichal Kazior 			 * should still give an idea about rx rate to the user.
1896c545070eSMichal Kazior 			 */
18973c97f5deSRajkumar Manoharan 			num_msdus += skb_queue_len(&amsdu);
1898500ff9f9SMichal Kazior 			ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
1899c545070eSMichal Kazior 			ath10k_htt_rx_h_filter(ar, &amsdu, status);
1900c545070eSMichal Kazior 			ath10k_htt_rx_h_mpdu(ar, &amsdu, status);
1901c545070eSMichal Kazior 			ath10k_htt_rx_h_deliver(ar, &amsdu, status);
1902c545070eSMichal Kazior 			break;
1903c545070eSMichal Kazior 		case -EAGAIN:
1904c545070eSMichal Kazior 			/* fall through */
1905c545070eSMichal Kazior 		default:
1906c545070eSMichal Kazior 			/* Should not happen. */
1907c545070eSMichal Kazior 			ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
1908c545070eSMichal Kazior 			htt->rx_confused = true;
1909c545070eSMichal Kazior 			__skb_queue_purge(&list);
19103c97f5deSRajkumar Manoharan 			return -EIO;
1911c545070eSMichal Kazior 		}
1912c545070eSMichal Kazior 	}
19133c97f5deSRajkumar Manoharan 	return num_msdus;
1914c545070eSMichal Kazior }
1915c545070eSMichal Kazior 
1916839ae637SMichal Kazior static void ath10k_htt_rx_tx_fetch_resp_id_confirm(struct ath10k *ar,
1917839ae637SMichal Kazior 						   const __le32 *resp_ids,
1918839ae637SMichal Kazior 						   int num_resp_ids)
1919839ae637SMichal Kazior {
1920839ae637SMichal Kazior 	int i;
1921839ae637SMichal Kazior 	u32 resp_id;
1922839ae637SMichal Kazior 
1923839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm num_resp_ids %d\n",
1924839ae637SMichal Kazior 		   num_resp_ids);
1925839ae637SMichal Kazior 
1926839ae637SMichal Kazior 	for (i = 0; i < num_resp_ids; i++) {
1927839ae637SMichal Kazior 		resp_id = le32_to_cpu(resp_ids[i]);
1928839ae637SMichal Kazior 
1929839ae637SMichal Kazior 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm resp_id %u\n",
1930839ae637SMichal Kazior 			   resp_id);
1931839ae637SMichal Kazior 
1932839ae637SMichal Kazior 		/* TODO: free resp_id */
1933839ae637SMichal Kazior 	}
1934839ae637SMichal Kazior }
1935839ae637SMichal Kazior 
1936839ae637SMichal Kazior static void ath10k_htt_rx_tx_fetch_ind(struct ath10k *ar, struct sk_buff *skb)
1937839ae637SMichal Kazior {
1938426e10eaSMichal Kazior 	struct ieee80211_hw *hw = ar->hw;
1939426e10eaSMichal Kazior 	struct ieee80211_txq *txq;
1940839ae637SMichal Kazior 	struct htt_resp *resp = (struct htt_resp *)skb->data;
1941839ae637SMichal Kazior 	struct htt_tx_fetch_record *record;
1942839ae637SMichal Kazior 	size_t len;
1943839ae637SMichal Kazior 	size_t max_num_bytes;
1944839ae637SMichal Kazior 	size_t max_num_msdus;
1945426e10eaSMichal Kazior 	size_t num_bytes;
1946426e10eaSMichal Kazior 	size_t num_msdus;
1947839ae637SMichal Kazior 	const __le32 *resp_ids;
1948839ae637SMichal Kazior 	u16 num_records;
1949839ae637SMichal Kazior 	u16 num_resp_ids;
1950839ae637SMichal Kazior 	u16 peer_id;
1951839ae637SMichal Kazior 	u8 tid;
1952426e10eaSMichal Kazior 	int ret;
1953839ae637SMichal Kazior 	int i;
1954839ae637SMichal Kazior 
1955839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind\n");
1956839ae637SMichal Kazior 
1957839ae637SMichal Kazior 	len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_ind);
1958839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
1959839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_fetch_ind event: buffer too short\n");
1960839ae637SMichal Kazior 		return;
1961839ae637SMichal Kazior 	}
1962839ae637SMichal Kazior 
1963839ae637SMichal Kazior 	num_records = le16_to_cpu(resp->tx_fetch_ind.num_records);
1964839ae637SMichal Kazior 	num_resp_ids = le16_to_cpu(resp->tx_fetch_ind.num_resp_ids);
1965839ae637SMichal Kazior 
1966839ae637SMichal Kazior 	len += sizeof(resp->tx_fetch_ind.records[0]) * num_records;
1967839ae637SMichal Kazior 	len += sizeof(resp->tx_fetch_ind.resp_ids[0]) * num_resp_ids;
1968839ae637SMichal Kazior 
1969839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
1970839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_fetch_ind event: too many records/resp_ids\n");
1971839ae637SMichal Kazior 		return;
1972839ae637SMichal Kazior 	}
1973839ae637SMichal Kazior 
1974839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind num records %hu num resps %hu seq %hu\n",
1975839ae637SMichal Kazior 		   num_records, num_resp_ids,
1976839ae637SMichal Kazior 		   le16_to_cpu(resp->tx_fetch_ind.fetch_seq_num));
1977839ae637SMichal Kazior 
1978426e10eaSMichal Kazior 	if (!ar->htt.tx_q_state.enabled) {
1979426e10eaSMichal Kazior 		ath10k_warn(ar, "received unexpected tx_fetch_ind event: not enabled\n");
1980426e10eaSMichal Kazior 		return;
1981426e10eaSMichal Kazior 	}
1982426e10eaSMichal Kazior 
1983426e10eaSMichal Kazior 	if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH) {
1984426e10eaSMichal Kazior 		ath10k_warn(ar, "received unexpected tx_fetch_ind event: in push mode\n");
1985426e10eaSMichal Kazior 		return;
1986426e10eaSMichal Kazior 	}
1987426e10eaSMichal Kazior 
1988426e10eaSMichal Kazior 	rcu_read_lock();
1989839ae637SMichal Kazior 
1990839ae637SMichal Kazior 	for (i = 0; i < num_records; i++) {
1991839ae637SMichal Kazior 		record = &resp->tx_fetch_ind.records[i];
1992839ae637SMichal Kazior 		peer_id = MS(le16_to_cpu(record->info),
1993839ae637SMichal Kazior 			     HTT_TX_FETCH_RECORD_INFO_PEER_ID);
1994839ae637SMichal Kazior 		tid = MS(le16_to_cpu(record->info),
1995839ae637SMichal Kazior 			 HTT_TX_FETCH_RECORD_INFO_TID);
1996839ae637SMichal Kazior 		max_num_msdus = le16_to_cpu(record->num_msdus);
1997839ae637SMichal Kazior 		max_num_bytes = le32_to_cpu(record->num_bytes);
1998839ae637SMichal Kazior 
1999839ae637SMichal Kazior 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch record %i peer_id %hu tid %hhu msdus %zu bytes %zu\n",
2000839ae637SMichal Kazior 			   i, peer_id, tid, max_num_msdus, max_num_bytes);
2001839ae637SMichal Kazior 
2002839ae637SMichal Kazior 		if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
2003839ae637SMichal Kazior 		    unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
2004839ae637SMichal Kazior 			ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
2005839ae637SMichal Kazior 				    peer_id, tid);
2006839ae637SMichal Kazior 			continue;
2007839ae637SMichal Kazior 		}
2008839ae637SMichal Kazior 
2009426e10eaSMichal Kazior 		spin_lock_bh(&ar->data_lock);
2010426e10eaSMichal Kazior 		txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
2011426e10eaSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
2012426e10eaSMichal Kazior 
2013426e10eaSMichal Kazior 		/* It is okay to release the lock and use txq because RCU read
2014426e10eaSMichal Kazior 		 * lock is held.
2015426e10eaSMichal Kazior 		 */
2016426e10eaSMichal Kazior 
2017426e10eaSMichal Kazior 		if (unlikely(!txq)) {
2018426e10eaSMichal Kazior 			ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
2019426e10eaSMichal Kazior 				    peer_id, tid);
2020426e10eaSMichal Kazior 			continue;
2021839ae637SMichal Kazior 		}
2022839ae637SMichal Kazior 
2023426e10eaSMichal Kazior 		num_msdus = 0;
2024426e10eaSMichal Kazior 		num_bytes = 0;
2025426e10eaSMichal Kazior 
2026426e10eaSMichal Kazior 		while (num_msdus < max_num_msdus &&
2027426e10eaSMichal Kazior 		       num_bytes < max_num_bytes) {
2028426e10eaSMichal Kazior 			ret = ath10k_mac_tx_push_txq(hw, txq);
2029426e10eaSMichal Kazior 			if (ret < 0)
2030426e10eaSMichal Kazior 				break;
2031426e10eaSMichal Kazior 
2032426e10eaSMichal Kazior 			num_msdus++;
2033426e10eaSMichal Kazior 			num_bytes += ret;
2034426e10eaSMichal Kazior 		}
2035426e10eaSMichal Kazior 
2036426e10eaSMichal Kazior 		record->num_msdus = cpu_to_le16(num_msdus);
2037426e10eaSMichal Kazior 		record->num_bytes = cpu_to_le32(num_bytes);
2038426e10eaSMichal Kazior 
2039426e10eaSMichal Kazior 		ath10k_htt_tx_txq_recalc(hw, txq);
2040426e10eaSMichal Kazior 	}
2041426e10eaSMichal Kazior 
2042426e10eaSMichal Kazior 	rcu_read_unlock();
2043426e10eaSMichal Kazior 
2044839ae637SMichal Kazior 	resp_ids = ath10k_htt_get_tx_fetch_ind_resp_ids(&resp->tx_fetch_ind);
2045839ae637SMichal Kazior 	ath10k_htt_rx_tx_fetch_resp_id_confirm(ar, resp_ids, num_resp_ids);
2046839ae637SMichal Kazior 
2047426e10eaSMichal Kazior 	ret = ath10k_htt_tx_fetch_resp(ar,
2048426e10eaSMichal Kazior 				       resp->tx_fetch_ind.token,
2049426e10eaSMichal Kazior 				       resp->tx_fetch_ind.fetch_seq_num,
2050426e10eaSMichal Kazior 				       resp->tx_fetch_ind.records,
2051426e10eaSMichal Kazior 				       num_records);
2052426e10eaSMichal Kazior 	if (unlikely(ret)) {
2053426e10eaSMichal Kazior 		ath10k_warn(ar, "failed to submit tx fetch resp for token 0x%08x: %d\n",
2054426e10eaSMichal Kazior 			    le32_to_cpu(resp->tx_fetch_ind.token), ret);
2055426e10eaSMichal Kazior 		/* FIXME: request fw restart */
2056426e10eaSMichal Kazior 	}
2057426e10eaSMichal Kazior 
2058426e10eaSMichal Kazior 	ath10k_htt_tx_txq_sync(ar);
2059839ae637SMichal Kazior }
2060839ae637SMichal Kazior 
2061839ae637SMichal Kazior static void ath10k_htt_rx_tx_fetch_confirm(struct ath10k *ar,
2062839ae637SMichal Kazior 					   struct sk_buff *skb)
2063839ae637SMichal Kazior {
2064839ae637SMichal Kazior 	const struct htt_resp *resp = (void *)skb->data;
2065839ae637SMichal Kazior 	size_t len;
2066839ae637SMichal Kazior 	int num_resp_ids;
2067839ae637SMichal Kazior 
2068839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm\n");
2069839ae637SMichal Kazior 
2070839ae637SMichal Kazior 	len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_confirm);
2071839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
2072839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_fetch_confirm event: buffer too short\n");
2073839ae637SMichal Kazior 		return;
2074839ae637SMichal Kazior 	}
2075839ae637SMichal Kazior 
2076839ae637SMichal Kazior 	num_resp_ids = le16_to_cpu(resp->tx_fetch_confirm.num_resp_ids);
2077839ae637SMichal Kazior 	len += sizeof(resp->tx_fetch_confirm.resp_ids[0]) * num_resp_ids;
2078839ae637SMichal Kazior 
2079839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
2080839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_fetch_confirm event: resp_ids buffer overflow\n");
2081839ae637SMichal Kazior 		return;
2082839ae637SMichal Kazior 	}
2083839ae637SMichal Kazior 
2084839ae637SMichal Kazior 	ath10k_htt_rx_tx_fetch_resp_id_confirm(ar,
2085839ae637SMichal Kazior 					       resp->tx_fetch_confirm.resp_ids,
2086839ae637SMichal Kazior 					       num_resp_ids);
2087839ae637SMichal Kazior }
2088839ae637SMichal Kazior 
2089839ae637SMichal Kazior static void ath10k_htt_rx_tx_mode_switch_ind(struct ath10k *ar,
2090839ae637SMichal Kazior 					     struct sk_buff *skb)
2091839ae637SMichal Kazior {
2092839ae637SMichal Kazior 	const struct htt_resp *resp = (void *)skb->data;
2093839ae637SMichal Kazior 	const struct htt_tx_mode_switch_record *record;
2094426e10eaSMichal Kazior 	struct ieee80211_txq *txq;
2095426e10eaSMichal Kazior 	struct ath10k_txq *artxq;
2096839ae637SMichal Kazior 	size_t len;
2097839ae637SMichal Kazior 	size_t num_records;
2098839ae637SMichal Kazior 	enum htt_tx_mode_switch_mode mode;
2099839ae637SMichal Kazior 	bool enable;
2100839ae637SMichal Kazior 	u16 info0;
2101839ae637SMichal Kazior 	u16 info1;
2102839ae637SMichal Kazior 	u16 threshold;
2103839ae637SMichal Kazior 	u16 peer_id;
2104839ae637SMichal Kazior 	u8 tid;
2105839ae637SMichal Kazior 	int i;
2106839ae637SMichal Kazior 
2107839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx mode switch ind\n");
2108839ae637SMichal Kazior 
2109839ae637SMichal Kazior 	len = sizeof(resp->hdr) + sizeof(resp->tx_mode_switch_ind);
2110839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
2111839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_mode_switch_ind event: buffer too short\n");
2112839ae637SMichal Kazior 		return;
2113839ae637SMichal Kazior 	}
2114839ae637SMichal Kazior 
2115839ae637SMichal Kazior 	info0 = le16_to_cpu(resp->tx_mode_switch_ind.info0);
2116839ae637SMichal Kazior 	info1 = le16_to_cpu(resp->tx_mode_switch_ind.info1);
2117839ae637SMichal Kazior 
2118839ae637SMichal Kazior 	enable = !!(info0 & HTT_TX_MODE_SWITCH_IND_INFO0_ENABLE);
2119839ae637SMichal Kazior 	num_records = MS(info0, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
2120839ae637SMichal Kazior 	mode = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_MODE);
2121839ae637SMichal Kazior 	threshold = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
2122839ae637SMichal Kazior 
2123839ae637SMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT,
2124839ae637SMichal Kazior 		   "htt rx tx mode switch ind info0 0x%04hx info1 0x%04hx enable %d num records %zd mode %d threshold %hu\n",
2125839ae637SMichal Kazior 		   info0, info1, enable, num_records, mode, threshold);
2126839ae637SMichal Kazior 
2127839ae637SMichal Kazior 	len += sizeof(resp->tx_mode_switch_ind.records[0]) * num_records;
2128839ae637SMichal Kazior 
2129839ae637SMichal Kazior 	if (unlikely(skb->len < len)) {
2130839ae637SMichal Kazior 		ath10k_warn(ar, "received corrupted tx_mode_switch_mode_ind event: too many records\n");
2131839ae637SMichal Kazior 		return;
2132839ae637SMichal Kazior 	}
2133839ae637SMichal Kazior 
2134839ae637SMichal Kazior 	switch (mode) {
2135839ae637SMichal Kazior 	case HTT_TX_MODE_SWITCH_PUSH:
2136839ae637SMichal Kazior 	case HTT_TX_MODE_SWITCH_PUSH_PULL:
2137839ae637SMichal Kazior 		break;
2138839ae637SMichal Kazior 	default:
2139839ae637SMichal Kazior 		ath10k_warn(ar, "received invalid tx_mode_switch_mode_ind mode %d, ignoring\n",
2140839ae637SMichal Kazior 			    mode);
2141839ae637SMichal Kazior 		return;
2142839ae637SMichal Kazior 	}
2143839ae637SMichal Kazior 
2144839ae637SMichal Kazior 	if (!enable)
2145839ae637SMichal Kazior 		return;
2146839ae637SMichal Kazior 
2147426e10eaSMichal Kazior 	ar->htt.tx_q_state.enabled = enable;
2148426e10eaSMichal Kazior 	ar->htt.tx_q_state.mode = mode;
2149426e10eaSMichal Kazior 	ar->htt.tx_q_state.num_push_allowed = threshold;
2150426e10eaSMichal Kazior 
2151426e10eaSMichal Kazior 	rcu_read_lock();
2152839ae637SMichal Kazior 
2153839ae637SMichal Kazior 	for (i = 0; i < num_records; i++) {
2154839ae637SMichal Kazior 		record = &resp->tx_mode_switch_ind.records[i];
2155839ae637SMichal Kazior 		info0 = le16_to_cpu(record->info0);
2156839ae637SMichal Kazior 		peer_id = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_PEER_ID);
2157839ae637SMichal Kazior 		tid = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_TID);
2158839ae637SMichal Kazior 
2159839ae637SMichal Kazior 		if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
2160839ae637SMichal Kazior 		    unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
2161839ae637SMichal Kazior 			ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
2162839ae637SMichal Kazior 				    peer_id, tid);
2163839ae637SMichal Kazior 			continue;
2164839ae637SMichal Kazior 		}
2165839ae637SMichal Kazior 
2166426e10eaSMichal Kazior 		spin_lock_bh(&ar->data_lock);
2167426e10eaSMichal Kazior 		txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
2168426e10eaSMichal Kazior 		spin_unlock_bh(&ar->data_lock);
2169426e10eaSMichal Kazior 
2170426e10eaSMichal Kazior 		/* It is okay to release the lock and use txq because RCU read
2171426e10eaSMichal Kazior 		 * lock is held.
2172426e10eaSMichal Kazior 		 */
2173426e10eaSMichal Kazior 
2174426e10eaSMichal Kazior 		if (unlikely(!txq)) {
2175426e10eaSMichal Kazior 			ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
2176426e10eaSMichal Kazior 				    peer_id, tid);
2177426e10eaSMichal Kazior 			continue;
2178839ae637SMichal Kazior 		}
2179839ae637SMichal Kazior 
2180426e10eaSMichal Kazior 		spin_lock_bh(&ar->htt.tx_lock);
2181426e10eaSMichal Kazior 		artxq = (void *)txq->drv_priv;
2182426e10eaSMichal Kazior 		artxq->num_push_allowed = le16_to_cpu(record->num_max_msdus);
2183426e10eaSMichal Kazior 		spin_unlock_bh(&ar->htt.tx_lock);
2184426e10eaSMichal Kazior 	}
2185426e10eaSMichal Kazior 
2186426e10eaSMichal Kazior 	rcu_read_unlock();
2187426e10eaSMichal Kazior 
2188426e10eaSMichal Kazior 	ath10k_mac_tx_push_pending(ar);
2189839ae637SMichal Kazior }
2190839ae637SMichal Kazior 
2191e3a91f87SRajkumar Manoharan void ath10k_htt_htc_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
2192e3a91f87SRajkumar Manoharan {
2193e3a91f87SRajkumar Manoharan 	bool release;
2194e3a91f87SRajkumar Manoharan 
2195e3a91f87SRajkumar Manoharan 	release = ath10k_htt_t2h_msg_handler(ar, skb);
2196e3a91f87SRajkumar Manoharan 
2197e3a91f87SRajkumar Manoharan 	/* Free the indication buffer */
2198e3a91f87SRajkumar Manoharan 	if (release)
2199e3a91f87SRajkumar Manoharan 		dev_kfree_skb_any(skb);
2200e3a91f87SRajkumar Manoharan }
2201e3a91f87SRajkumar Manoharan 
2202cec17c38SAnilkumar Kolli static inline bool is_valid_legacy_rate(u8 rate)
2203cec17c38SAnilkumar Kolli {
2204cec17c38SAnilkumar Kolli 	static const u8 legacy_rates[] = {1, 2, 5, 11, 6, 9, 12,
2205cec17c38SAnilkumar Kolli 					  18, 24, 36, 48, 54};
2206cec17c38SAnilkumar Kolli 	int i;
2207cec17c38SAnilkumar Kolli 
2208cec17c38SAnilkumar Kolli 	for (i = 0; i < ARRAY_SIZE(legacy_rates); i++) {
2209cec17c38SAnilkumar Kolli 		if (rate == legacy_rates[i])
2210cec17c38SAnilkumar Kolli 			return true;
2211cec17c38SAnilkumar Kolli 	}
2212cec17c38SAnilkumar Kolli 
2213cec17c38SAnilkumar Kolli 	return false;
2214cec17c38SAnilkumar Kolli }
2215cec17c38SAnilkumar Kolli 
2216cec17c38SAnilkumar Kolli static void
2217cec17c38SAnilkumar Kolli ath10k_update_per_peer_tx_stats(struct ath10k *ar,
2218cec17c38SAnilkumar Kolli 				struct ieee80211_sta *sta,
2219cec17c38SAnilkumar Kolli 				struct ath10k_per_peer_tx_stats *peer_stats)
2220cec17c38SAnilkumar Kolli {
2221cec17c38SAnilkumar Kolli 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
2222cec17c38SAnilkumar Kolli 	u8 rate = 0, sgi;
2223cec17c38SAnilkumar Kolli 	struct rate_info txrate;
2224cec17c38SAnilkumar Kolli 
2225cec17c38SAnilkumar Kolli 	lockdep_assert_held(&ar->data_lock);
2226cec17c38SAnilkumar Kolli 
2227cec17c38SAnilkumar Kolli 	txrate.flags = ATH10K_HW_PREAMBLE(peer_stats->ratecode);
2228cec17c38SAnilkumar Kolli 	txrate.bw = ATH10K_HW_BW(peer_stats->flags);
2229cec17c38SAnilkumar Kolli 	txrate.nss = ATH10K_HW_NSS(peer_stats->ratecode);
2230cec17c38SAnilkumar Kolli 	txrate.mcs = ATH10K_HW_MCS_RATE(peer_stats->ratecode);
2231cec17c38SAnilkumar Kolli 	sgi = ATH10K_HW_GI(peer_stats->flags);
2232cec17c38SAnilkumar Kolli 
2233cec17c38SAnilkumar Kolli 	if (((txrate.flags == WMI_RATE_PREAMBLE_HT) ||
2234cec17c38SAnilkumar Kolli 	     (txrate.flags == WMI_RATE_PREAMBLE_VHT)) && txrate.mcs > 9) {
2235cec17c38SAnilkumar Kolli 		ath10k_warn(ar, "Invalid mcs %hhd peer stats", txrate.mcs);
2236cec17c38SAnilkumar Kolli 		return;
2237cec17c38SAnilkumar Kolli 	}
2238cec17c38SAnilkumar Kolli 
2239cec17c38SAnilkumar Kolli 	if (txrate.flags == WMI_RATE_PREAMBLE_CCK ||
2240cec17c38SAnilkumar Kolli 	    txrate.flags == WMI_RATE_PREAMBLE_OFDM) {
2241cec17c38SAnilkumar Kolli 		rate = ATH10K_HW_LEGACY_RATE(peer_stats->ratecode);
2242cec17c38SAnilkumar Kolli 
2243cec17c38SAnilkumar Kolli 		if (!is_valid_legacy_rate(rate)) {
2244cec17c38SAnilkumar Kolli 			ath10k_warn(ar, "Invalid legacy rate %hhd peer stats",
2245cec17c38SAnilkumar Kolli 				    rate);
2246cec17c38SAnilkumar Kolli 			return;
2247cec17c38SAnilkumar Kolli 		}
2248cec17c38SAnilkumar Kolli 
2249cec17c38SAnilkumar Kolli 		/* This is hacky, FW sends CCK rate 5.5Mbps as 6 */
2250cec17c38SAnilkumar Kolli 		rate *= 10;
2251cec17c38SAnilkumar Kolli 		if (rate == 60 && txrate.flags == WMI_RATE_PREAMBLE_CCK)
2252cec17c38SAnilkumar Kolli 			rate = rate - 5;
2253cd591027SMohammed Shafi Shajakhan 		arsta->txrate.legacy = rate;
2254cec17c38SAnilkumar Kolli 	} else if (txrate.flags == WMI_RATE_PREAMBLE_HT) {
2255cec17c38SAnilkumar Kolli 		arsta->txrate.flags = RATE_INFO_FLAGS_MCS;
2256cec17c38SAnilkumar Kolli 		arsta->txrate.mcs = txrate.mcs;
2257cec17c38SAnilkumar Kolli 	} else {
2258cec17c38SAnilkumar Kolli 		arsta->txrate.flags = RATE_INFO_FLAGS_VHT_MCS;
2259cec17c38SAnilkumar Kolli 		arsta->txrate.mcs = txrate.mcs;
2260cec17c38SAnilkumar Kolli 	}
2261cec17c38SAnilkumar Kolli 
2262cec17c38SAnilkumar Kolli 	if (sgi)
2263cec17c38SAnilkumar Kolli 		arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
2264cec17c38SAnilkumar Kolli 
2265cec17c38SAnilkumar Kolli 	arsta->txrate.nss = txrate.nss;
2266cec17c38SAnilkumar Kolli 	arsta->txrate.bw = txrate.bw + RATE_INFO_BW_20;
2267cec17c38SAnilkumar Kolli }
2268cec17c38SAnilkumar Kolli 
2269cec17c38SAnilkumar Kolli static void ath10k_htt_fetch_peer_stats(struct ath10k *ar,
2270cec17c38SAnilkumar Kolli 					struct sk_buff *skb)
2271cec17c38SAnilkumar Kolli {
2272cec17c38SAnilkumar Kolli 	struct htt_resp *resp = (struct htt_resp *)skb->data;
2273cec17c38SAnilkumar Kolli 	struct ath10k_per_peer_tx_stats *p_tx_stats = &ar->peer_tx_stats;
2274cec17c38SAnilkumar Kolli 	struct htt_per_peer_tx_stats_ind *tx_stats;
2275cec17c38SAnilkumar Kolli 	struct ieee80211_sta *sta;
2276cec17c38SAnilkumar Kolli 	struct ath10k_peer *peer;
2277cec17c38SAnilkumar Kolli 	int peer_id, i;
2278cec17c38SAnilkumar Kolli 	u8 ppdu_len, num_ppdu;
2279cec17c38SAnilkumar Kolli 
2280cec17c38SAnilkumar Kolli 	num_ppdu = resp->peer_tx_stats.num_ppdu;
2281cec17c38SAnilkumar Kolli 	ppdu_len = resp->peer_tx_stats.ppdu_len * sizeof(__le32);
2282cec17c38SAnilkumar Kolli 
2283cec17c38SAnilkumar Kolli 	if (skb->len < sizeof(struct htt_resp_hdr) + num_ppdu * ppdu_len) {
2284cec17c38SAnilkumar Kolli 		ath10k_warn(ar, "Invalid peer stats buf length %d\n", skb->len);
2285cec17c38SAnilkumar Kolli 		return;
2286cec17c38SAnilkumar Kolli 	}
2287cec17c38SAnilkumar Kolli 
2288cec17c38SAnilkumar Kolli 	tx_stats = (struct htt_per_peer_tx_stats_ind *)
2289cec17c38SAnilkumar Kolli 			(resp->peer_tx_stats.payload);
2290cec17c38SAnilkumar Kolli 	peer_id = __le16_to_cpu(tx_stats->peer_id);
2291cec17c38SAnilkumar Kolli 
2292cec17c38SAnilkumar Kolli 	rcu_read_lock();
2293cec17c38SAnilkumar Kolli 	spin_lock_bh(&ar->data_lock);
2294cec17c38SAnilkumar Kolli 	peer = ath10k_peer_find_by_id(ar, peer_id);
2295cec17c38SAnilkumar Kolli 	if (!peer) {
2296cec17c38SAnilkumar Kolli 		ath10k_warn(ar, "Invalid peer id %d peer stats buffer\n",
2297cec17c38SAnilkumar Kolli 			    peer_id);
2298cec17c38SAnilkumar Kolli 		goto out;
2299cec17c38SAnilkumar Kolli 	}
2300cec17c38SAnilkumar Kolli 
2301cec17c38SAnilkumar Kolli 	sta = peer->sta;
2302cec17c38SAnilkumar Kolli 	for (i = 0; i < num_ppdu; i++) {
2303cec17c38SAnilkumar Kolli 		tx_stats = (struct htt_per_peer_tx_stats_ind *)
2304cec17c38SAnilkumar Kolli 			   (resp->peer_tx_stats.payload + i * ppdu_len);
2305cec17c38SAnilkumar Kolli 
2306cec17c38SAnilkumar Kolli 		p_tx_stats->succ_bytes = __le32_to_cpu(tx_stats->succ_bytes);
2307cec17c38SAnilkumar Kolli 		p_tx_stats->retry_bytes = __le32_to_cpu(tx_stats->retry_bytes);
2308cec17c38SAnilkumar Kolli 		p_tx_stats->failed_bytes =
2309cec17c38SAnilkumar Kolli 				__le32_to_cpu(tx_stats->failed_bytes);
2310cec17c38SAnilkumar Kolli 		p_tx_stats->ratecode = tx_stats->ratecode;
2311cec17c38SAnilkumar Kolli 		p_tx_stats->flags = tx_stats->flags;
2312cec17c38SAnilkumar Kolli 		p_tx_stats->succ_pkts = __le16_to_cpu(tx_stats->succ_pkts);
2313cec17c38SAnilkumar Kolli 		p_tx_stats->retry_pkts = __le16_to_cpu(tx_stats->retry_pkts);
2314cec17c38SAnilkumar Kolli 		p_tx_stats->failed_pkts = __le16_to_cpu(tx_stats->failed_pkts);
2315cec17c38SAnilkumar Kolli 
2316cec17c38SAnilkumar Kolli 		ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
2317cec17c38SAnilkumar Kolli 	}
2318cec17c38SAnilkumar Kolli 
2319cec17c38SAnilkumar Kolli out:
2320cec17c38SAnilkumar Kolli 	spin_unlock_bh(&ar->data_lock);
2321cec17c38SAnilkumar Kolli 	rcu_read_unlock();
2322cec17c38SAnilkumar Kolli }
2323cec17c38SAnilkumar Kolli 
2324e3a91f87SRajkumar Manoharan bool ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
23255e3dd157SKalle Valo {
2326edb8236dSMichal Kazior 	struct ath10k_htt *htt = &ar->htt;
23275e3dd157SKalle Valo 	struct htt_resp *resp = (struct htt_resp *)skb->data;
23288348db29SRajkumar Manoharan 	enum htt_t2h_msg_type type;
23295e3dd157SKalle Valo 
23305e3dd157SKalle Valo 	/* confirm alignment */
23315e3dd157SKalle Valo 	if (!IS_ALIGNED((unsigned long)skb->data, 4))
23327aa7a72aSMichal Kazior 		ath10k_warn(ar, "unaligned htt message, expect trouble\n");
23335e3dd157SKalle Valo 
23347aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
23355e3dd157SKalle Valo 		   resp->hdr.msg_type);
23368348db29SRajkumar Manoharan 
23378348db29SRajkumar Manoharan 	if (resp->hdr.msg_type >= ar->htt.t2h_msg_types_max) {
23388348db29SRajkumar Manoharan 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, unsupported msg_type: 0x%0X\n max: 0x%0X",
23398348db29SRajkumar Manoharan 			   resp->hdr.msg_type, ar->htt.t2h_msg_types_max);
2340e3a91f87SRajkumar Manoharan 		return true;
23418348db29SRajkumar Manoharan 	}
23428348db29SRajkumar Manoharan 	type = ar->htt.t2h_msg_types[resp->hdr.msg_type];
23438348db29SRajkumar Manoharan 
23448348db29SRajkumar Manoharan 	switch (type) {
23455e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_VERSION_CONF: {
23465e3dd157SKalle Valo 		htt->target_version_major = resp->ver_resp.major;
23475e3dd157SKalle Valo 		htt->target_version_minor = resp->ver_resp.minor;
23485e3dd157SKalle Valo 		complete(&htt->target_version_received);
23495e3dd157SKalle Valo 		break;
23505e3dd157SKalle Valo 	}
23516c5151a9SMichal Kazior 	case HTT_T2H_MSG_TYPE_RX_IND:
23523128b3d8SRajkumar Manoharan 		ath10k_htt_rx_proc_rx_ind(htt, &resp->rx_ind);
23533128b3d8SRajkumar Manoharan 		break;
23545e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_PEER_MAP: {
23555e3dd157SKalle Valo 		struct htt_peer_map_event ev = {
23565e3dd157SKalle Valo 			.vdev_id = resp->peer_map.vdev_id,
23575e3dd157SKalle Valo 			.peer_id = __le16_to_cpu(resp->peer_map.peer_id),
23585e3dd157SKalle Valo 		};
23595e3dd157SKalle Valo 		memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
23605e3dd157SKalle Valo 		ath10k_peer_map_event(htt, &ev);
23615e3dd157SKalle Valo 		break;
23625e3dd157SKalle Valo 	}
23635e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
23645e3dd157SKalle Valo 		struct htt_peer_unmap_event ev = {
23655e3dd157SKalle Valo 			.peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
23665e3dd157SKalle Valo 		};
23675e3dd157SKalle Valo 		ath10k_peer_unmap_event(htt, &ev);
23685e3dd157SKalle Valo 		break;
23695e3dd157SKalle Valo 	}
23705e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
23715e3dd157SKalle Valo 		struct htt_tx_done tx_done = {};
23725e3dd157SKalle Valo 		int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
23735e3dd157SKalle Valo 
237459465fe4SRajkumar Manoharan 		tx_done.msdu_id = __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
23755e3dd157SKalle Valo 
23765e3dd157SKalle Valo 		switch (status) {
23775e3dd157SKalle Valo 		case HTT_MGMT_TX_STATUS_OK:
237859465fe4SRajkumar Manoharan 			tx_done.status = HTT_TX_COMPL_STATE_ACK;
23795e3dd157SKalle Valo 			break;
23805e3dd157SKalle Valo 		case HTT_MGMT_TX_STATUS_RETRY:
238159465fe4SRajkumar Manoharan 			tx_done.status = HTT_TX_COMPL_STATE_NOACK;
23825e3dd157SKalle Valo 			break;
23835e3dd157SKalle Valo 		case HTT_MGMT_TX_STATUS_DROP:
238459465fe4SRajkumar Manoharan 			tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
23855e3dd157SKalle Valo 			break;
23865e3dd157SKalle Valo 		}
23875e3dd157SKalle Valo 
2388cac08552SRajkumar Manoharan 		status = ath10k_txrx_tx_unref(htt, &tx_done);
2389cac08552SRajkumar Manoharan 		if (!status) {
2390cac08552SRajkumar Manoharan 			spin_lock_bh(&htt->tx_lock);
2391cac08552SRajkumar Manoharan 			ath10k_htt_tx_mgmt_dec_pending(htt);
2392cac08552SRajkumar Manoharan 			spin_unlock_bh(&htt->tx_lock);
2393cac08552SRajkumar Manoharan 		}
23945e3dd157SKalle Valo 		break;
23955e3dd157SKalle Valo 	}
23966c5151a9SMichal Kazior 	case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
239759465fe4SRajkumar Manoharan 		ath10k_htt_rx_tx_compl_ind(htt->ar, skb);
239859465fe4SRajkumar Manoharan 		break;
23995e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_SEC_IND: {
24005e3dd157SKalle Valo 		struct ath10k *ar = htt->ar;
24015e3dd157SKalle Valo 		struct htt_security_indication *ev = &resp->security_indication;
24025e3dd157SKalle Valo 
24037aa7a72aSMichal Kazior 		ath10k_dbg(ar, ATH10K_DBG_HTT,
24045e3dd157SKalle Valo 			   "sec ind peer_id %d unicast %d type %d\n",
24055e3dd157SKalle Valo 			  __le16_to_cpu(ev->peer_id),
24065e3dd157SKalle Valo 			  !!(ev->flags & HTT_SECURITY_IS_UNICAST),
24075e3dd157SKalle Valo 			  MS(ev->flags, HTT_SECURITY_TYPE));
24085e3dd157SKalle Valo 		complete(&ar->install_key_done);
24095e3dd157SKalle Valo 		break;
24105e3dd157SKalle Valo 	}
24115e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
24127aa7a72aSMichal Kazior 		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
24135e3dd157SKalle Valo 				skb->data, skb->len);
24143c97f5deSRajkumar Manoharan 		atomic_inc(&htt->num_mpdus_ready);
24155e3dd157SKalle Valo 		break;
24165e3dd157SKalle Valo 	}
24175e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_TEST:
24185e3dd157SKalle Valo 		break;
24195e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_STATS_CONF:
2420d35a6c18SMichal Kazior 		trace_ath10k_htt_stats(ar, skb->data, skb->len);
2421a9bf0506SKalle Valo 		break;
2422a9bf0506SKalle Valo 	case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
2423708b9bdeSMichal Kazior 		/* Firmware can return tx frames if it's unable to fully
2424708b9bdeSMichal Kazior 		 * process them and suspects host may be able to fix it. ath10k
2425708b9bdeSMichal Kazior 		 * sends all tx frames as already inspected so this shouldn't
2426708b9bdeSMichal Kazior 		 * happen unless fw has a bug.
2427708b9bdeSMichal Kazior 		 */
24287aa7a72aSMichal Kazior 		ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
2429708b9bdeSMichal Kazior 		break;
24305e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_RX_ADDBA:
2431aa5b4fbcSMichal Kazior 		ath10k_htt_rx_addba(ar, resp);
2432aa5b4fbcSMichal Kazior 		break;
24335e3dd157SKalle Valo 	case HTT_T2H_MSG_TYPE_RX_DELBA:
2434aa5b4fbcSMichal Kazior 		ath10k_htt_rx_delba(ar, resp);
2435aa5b4fbcSMichal Kazior 		break;
2436bfdd7937SRajkumar Manoharan 	case HTT_T2H_MSG_TYPE_PKTLOG: {
2437bfdd7937SRajkumar Manoharan 		trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
243834293f75SAshok Raj Nagarajan 					skb->len -
243934293f75SAshok Raj Nagarajan 					offsetof(struct htt_resp,
244034293f75SAshok Raj Nagarajan 						 pktlog_msg.payload));
2441bfdd7937SRajkumar Manoharan 		break;
2442bfdd7937SRajkumar Manoharan 	}
2443aa5b4fbcSMichal Kazior 	case HTT_T2H_MSG_TYPE_RX_FLUSH: {
2444aa5b4fbcSMichal Kazior 		/* Ignore this event because mac80211 takes care of Rx
2445aa5b4fbcSMichal Kazior 		 * aggregation reordering.
2446aa5b4fbcSMichal Kazior 		 */
2447aa5b4fbcSMichal Kazior 		break;
2448aa5b4fbcSMichal Kazior 	}
2449c545070eSMichal Kazior 	case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
24503c97f5deSRajkumar Manoharan 		__skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
2451e3a91f87SRajkumar Manoharan 		return false;
2452c545070eSMichal Kazior 	}
2453c545070eSMichal Kazior 	case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND:
24548348db29SRajkumar Manoharan 		break;
24552ce9b25cSRajkumar Manoharan 	case HTT_T2H_MSG_TYPE_CHAN_CHANGE: {
24562ce9b25cSRajkumar Manoharan 		u32 phymode = __le32_to_cpu(resp->chan_change.phymode);
24572ce9b25cSRajkumar Manoharan 		u32 freq = __le32_to_cpu(resp->chan_change.freq);
24582ce9b25cSRajkumar Manoharan 
24592ce9b25cSRajkumar Manoharan 		ar->tgt_oper_chan =
24602ce9b25cSRajkumar Manoharan 			__ieee80211_get_channel(ar->hw->wiphy, freq);
24612ce9b25cSRajkumar Manoharan 		ath10k_dbg(ar, ATH10K_DBG_HTT,
24622ce9b25cSRajkumar Manoharan 			   "htt chan change freq %u phymode %s\n",
24632ce9b25cSRajkumar Manoharan 			   freq, ath10k_wmi_phymode_str(phymode));
2464c545070eSMichal Kazior 		break;
24652ce9b25cSRajkumar Manoharan 	}
2466ccec9038SDavid Liu 	case HTT_T2H_MSG_TYPE_AGGR_CONF:
2467ccec9038SDavid Liu 		break;
2468b2fdbccdSRajkumar Manoharan 	case HTT_T2H_MSG_TYPE_TX_FETCH_IND: {
2469b2fdbccdSRajkumar Manoharan 		struct sk_buff *tx_fetch_ind = skb_copy(skb, GFP_ATOMIC);
2470b2fdbccdSRajkumar Manoharan 
2471b2fdbccdSRajkumar Manoharan 		if (!tx_fetch_ind) {
2472b2fdbccdSRajkumar Manoharan 			ath10k_warn(ar, "failed to copy htt tx fetch ind\n");
2473b2fdbccdSRajkumar Manoharan 			break;
2474b2fdbccdSRajkumar Manoharan 		}
2475b2fdbccdSRajkumar Manoharan 		skb_queue_tail(&htt->tx_fetch_ind_q, tx_fetch_ind);
2476b2fdbccdSRajkumar Manoharan 		break;
2477b2fdbccdSRajkumar Manoharan 	}
2478df94e702SMichal Kazior 	case HTT_T2H_MSG_TYPE_TX_FETCH_CONFIRM:
2479839ae637SMichal Kazior 		ath10k_htt_rx_tx_fetch_confirm(ar, skb);
2480839ae637SMichal Kazior 		break;
2481df94e702SMichal Kazior 	case HTT_T2H_MSG_TYPE_TX_MODE_SWITCH_IND:
2482839ae637SMichal Kazior 		ath10k_htt_rx_tx_mode_switch_ind(ar, skb);
24839b158736SMichal Kazior 		break;
2484cec17c38SAnilkumar Kolli 	case HTT_T2H_MSG_TYPE_PEER_STATS:
2485cec17c38SAnilkumar Kolli 		ath10k_htt_fetch_peer_stats(ar, skb);
2486cec17c38SAnilkumar Kolli 		break;
24879b158736SMichal Kazior 	case HTT_T2H_MSG_TYPE_EN_STATS:
24885e3dd157SKalle Valo 	default:
24892358a544SMichal Kazior 		ath10k_warn(ar, "htt event (%d) not handled\n",
24905e3dd157SKalle Valo 			    resp->hdr.msg_type);
24917aa7a72aSMichal Kazior 		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
24925e3dd157SKalle Valo 				skb->data, skb->len);
24935e3dd157SKalle Valo 		break;
24945e3dd157SKalle Valo 	};
2495e3a91f87SRajkumar Manoharan 	return true;
24965e3dd157SKalle Valo }
24973f0f7ed4SRajkumar Manoharan EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
24986c5151a9SMichal Kazior 
2499afb0bf7fSVivek Natarajan void ath10k_htt_rx_pktlog_completion_handler(struct ath10k *ar,
2500afb0bf7fSVivek Natarajan 					     struct sk_buff *skb)
2501afb0bf7fSVivek Natarajan {
250253a5c9bcSAshok Raj Nagarajan 	trace_ath10k_htt_pktlog(ar, skb->data, skb->len);
2503afb0bf7fSVivek Natarajan 	dev_kfree_skb_any(skb);
2504afb0bf7fSVivek Natarajan }
2505afb0bf7fSVivek Natarajan EXPORT_SYMBOL(ath10k_htt_rx_pktlog_completion_handler);
2506afb0bf7fSVivek Natarajan 
25073c97f5deSRajkumar Manoharan int ath10k_htt_txrx_compl_task(struct ath10k *ar, int budget)
25086c5151a9SMichal Kazior {
25093c97f5deSRajkumar Manoharan 	struct ath10k_htt *htt = &ar->htt;
251059465fe4SRajkumar Manoharan 	struct htt_tx_done tx_done = {};
2511426e10eaSMichal Kazior 	struct sk_buff_head tx_ind_q;
25126c5151a9SMichal Kazior 	struct sk_buff *skb;
2513d742c969SMichal Kazior 	unsigned long flags;
25143c97f5deSRajkumar Manoharan 	int quota = 0, done, num_rx_msdus;
25153c97f5deSRajkumar Manoharan 	bool resched_napi = false;
25166c5151a9SMichal Kazior 
2517426e10eaSMichal Kazior 	__skb_queue_head_init(&tx_ind_q);
2518da6416caSRajkumar Manoharan 
25193c97f5deSRajkumar Manoharan 	/* Since in-ord-ind can deliver more than 1 A-MSDU in single event,
25203c97f5deSRajkumar Manoharan 	 * process it first to utilize full available quota.
25213c97f5deSRajkumar Manoharan 	 */
25223c97f5deSRajkumar Manoharan 	while (quota < budget) {
25233c97f5deSRajkumar Manoharan 		if (skb_queue_empty(&htt->rx_in_ord_compl_q))
25243c97f5deSRajkumar Manoharan 			break;
2525da6416caSRajkumar Manoharan 
25263c97f5deSRajkumar Manoharan 		skb = __skb_dequeue(&htt->rx_in_ord_compl_q);
25273c97f5deSRajkumar Manoharan 		if (!skb) {
25283c97f5deSRajkumar Manoharan 			resched_napi = true;
25293c97f5deSRajkumar Manoharan 			goto exit;
25303c97f5deSRajkumar Manoharan 		}
25313c97f5deSRajkumar Manoharan 
25323c97f5deSRajkumar Manoharan 		spin_lock_bh(&htt->rx_ring.lock);
25333c97f5deSRajkumar Manoharan 		num_rx_msdus = ath10k_htt_rx_in_ord_ind(ar, skb);
25343c97f5deSRajkumar Manoharan 		spin_unlock_bh(&htt->rx_ring.lock);
25353c97f5deSRajkumar Manoharan 		if (num_rx_msdus < 0) {
25363c97f5deSRajkumar Manoharan 			resched_napi = true;
25373c97f5deSRajkumar Manoharan 			goto exit;
25383c97f5deSRajkumar Manoharan 		}
25393c97f5deSRajkumar Manoharan 
25403c97f5deSRajkumar Manoharan 		dev_kfree_skb_any(skb);
25413c97f5deSRajkumar Manoharan 		if (num_rx_msdus > 0)
25423c97f5deSRajkumar Manoharan 			quota += num_rx_msdus;
25433c97f5deSRajkumar Manoharan 
25443c97f5deSRajkumar Manoharan 		if ((quota > ATH10K_NAPI_QUOTA_LIMIT) &&
25453c97f5deSRajkumar Manoharan 		    !skb_queue_empty(&htt->rx_in_ord_compl_q)) {
25463c97f5deSRajkumar Manoharan 			resched_napi = true;
25473c97f5deSRajkumar Manoharan 			goto exit;
25483c97f5deSRajkumar Manoharan 		}
25493c97f5deSRajkumar Manoharan 	}
25503c97f5deSRajkumar Manoharan 
25513c97f5deSRajkumar Manoharan 	while (quota < budget) {
25523c97f5deSRajkumar Manoharan 		/* no more data to receive */
25533c97f5deSRajkumar Manoharan 		if (!atomic_read(&htt->num_mpdus_ready))
25543c97f5deSRajkumar Manoharan 			break;
25553c97f5deSRajkumar Manoharan 
25563c97f5deSRajkumar Manoharan 		num_rx_msdus = ath10k_htt_rx_handle_amsdu(htt);
25573c97f5deSRajkumar Manoharan 		if (num_rx_msdus < 0) {
25583c97f5deSRajkumar Manoharan 			resched_napi = true;
25593c97f5deSRajkumar Manoharan 			goto exit;
25603c97f5deSRajkumar Manoharan 		}
25613c97f5deSRajkumar Manoharan 
25623c97f5deSRajkumar Manoharan 		quota += num_rx_msdus;
25633c97f5deSRajkumar Manoharan 		atomic_dec(&htt->num_mpdus_ready);
25643c97f5deSRajkumar Manoharan 		if ((quota > ATH10K_NAPI_QUOTA_LIMIT) &&
25653c97f5deSRajkumar Manoharan 		    atomic_read(&htt->num_mpdus_ready)) {
25663c97f5deSRajkumar Manoharan 			resched_napi = true;
25673c97f5deSRajkumar Manoharan 			goto exit;
25683c97f5deSRajkumar Manoharan 		}
25693c97f5deSRajkumar Manoharan 	}
25703c97f5deSRajkumar Manoharan 
25713c97f5deSRajkumar Manoharan 	/* From NAPI documentation:
25723c97f5deSRajkumar Manoharan 	 *  The napi poll() function may also process TX completions, in which
25733c97f5deSRajkumar Manoharan 	 *  case if it processes the entire TX ring then it should count that
25743c97f5deSRajkumar Manoharan 	 *  work as the rest of the budget.
25753c97f5deSRajkumar Manoharan 	 */
25763c97f5deSRajkumar Manoharan 	if ((quota < budget) && !kfifo_is_empty(&htt->txdone_fifo))
25773c97f5deSRajkumar Manoharan 		quota = budget;
2578426e10eaSMichal Kazior 
257959465fe4SRajkumar Manoharan 	/* kfifo_get: called only within txrx_tasklet so it's neatly serialized.
258059465fe4SRajkumar Manoharan 	 * From kfifo_get() documentation:
258159465fe4SRajkumar Manoharan 	 *  Note that with only one concurrent reader and one concurrent writer,
258259465fe4SRajkumar Manoharan 	 *  you don't need extra locking to use these macro.
258359465fe4SRajkumar Manoharan 	 */
258459465fe4SRajkumar Manoharan 	while (kfifo_get(&htt->txdone_fifo, &tx_done))
258559465fe4SRajkumar Manoharan 		ath10k_txrx_tx_unref(htt, &tx_done);
25866c5151a9SMichal Kazior 
258718f53fe0SRajkumar Manoharan 	ath10k_mac_tx_push_pending(ar);
258818f53fe0SRajkumar Manoharan 
25893c97f5deSRajkumar Manoharan 	spin_lock_irqsave(&htt->tx_fetch_ind_q.lock, flags);
25903c97f5deSRajkumar Manoharan 	skb_queue_splice_init(&htt->tx_fetch_ind_q, &tx_ind_q);
25913c97f5deSRajkumar Manoharan 	spin_unlock_irqrestore(&htt->tx_fetch_ind_q.lock, flags);
25923c97f5deSRajkumar Manoharan 
2593426e10eaSMichal Kazior 	while ((skb = __skb_dequeue(&tx_ind_q))) {
2594426e10eaSMichal Kazior 		ath10k_htt_rx_tx_fetch_ind(ar, skb);
25956c5151a9SMichal Kazior 		dev_kfree_skb_any(skb);
25966c5151a9SMichal Kazior 	}
25976c5151a9SMichal Kazior 
25983c97f5deSRajkumar Manoharan exit:
25995c86d97bSRajkumar Manoharan 	ath10k_htt_rx_msdu_buff_replenish(htt);
26003c97f5deSRajkumar Manoharan 	/* In case of rx failure or more data to read, report budget
26013c97f5deSRajkumar Manoharan 	 * to reschedule NAPI poll
26023c97f5deSRajkumar Manoharan 	 */
26033c97f5deSRajkumar Manoharan 	done = resched_napi ? budget : quota;
26043c97f5deSRajkumar Manoharan 
26053c97f5deSRajkumar Manoharan 	return done;
26066c5151a9SMichal Kazior }
26073c97f5deSRajkumar Manoharan EXPORT_SYMBOL(ath10k_htt_txrx_compl_task);
2608