Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
   1// SPDX-License-Identifier: ISC
   2/*
   3 * Copyright (c) 2005-2011 Atheros Communications Inc.
   4 * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
   5 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
   6 */
   7
   8#include "core.h"
   9#include "htc.h"
  10#include "htt.h"
  11#include "txrx.h"
  12#include "debug.h"
  13#include "trace.h"
  14#include "mac.h"
  15
  16#include <linux/log2.h>
  17#include <linux/bitfield.h>
  18
  19/* when under memory pressure rx ring refill may fail and needs a retry */
  20#define HTT_RX_RING_REFILL_RETRY_MS 50
  21
  22#define HTT_RX_RING_REFILL_RESCHED_MS 5
  23
  24static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);
  25
  26static struct sk_buff *
  27ath10k_htt_rx_find_skb_paddr(struct ath10k *ar, u64 paddr)
  28{
  29	struct ath10k_skb_rxcb *rxcb;
  30
  31	hash_for_each_possible(ar->htt.rx_ring.skb_table, rxcb, hlist, paddr)
  32		if (rxcb->paddr == paddr)
  33			return ATH10K_RXCB_SKB(rxcb);
  34
  35	WARN_ON_ONCE(1);
  36	return NULL;
  37}
  38
  39static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
  40{
  41	struct sk_buff *skb;
  42	struct ath10k_skb_rxcb *rxcb;
  43	struct hlist_node *n;
  44	int i;
  45
  46	if (htt->rx_ring.in_ord_rx) {
  47		hash_for_each_safe(htt->rx_ring.skb_table, i, n, rxcb, hlist) {
  48			skb = ATH10K_RXCB_SKB(rxcb);
  49			dma_unmap_single(htt->ar->dev, rxcb->paddr,
  50					 skb->len + skb_tailroom(skb),
  51					 DMA_FROM_DEVICE);
  52			hash_del(&rxcb->hlist);
  53			dev_kfree_skb_any(skb);
  54		}
  55	} else {
  56		for (i = 0; i < htt->rx_ring.size; i++) {
  57			skb = htt->rx_ring.netbufs_ring[i];
  58			if (!skb)
  59				continue;
  60
  61			rxcb = ATH10K_SKB_RXCB(skb);
  62			dma_unmap_single(htt->ar->dev, rxcb->paddr,
  63					 skb->len + skb_tailroom(skb),
  64					 DMA_FROM_DEVICE);
  65			dev_kfree_skb_any(skb);
  66		}
  67	}
  68
  69	htt->rx_ring.fill_cnt = 0;
  70	hash_init(htt->rx_ring.skb_table);
  71	memset(htt->rx_ring.netbufs_ring, 0,
  72	       htt->rx_ring.size * sizeof(htt->rx_ring.netbufs_ring[0]));
  73}
  74
  75static size_t ath10k_htt_get_rx_ring_size_32(struct ath10k_htt *htt)
  76{
  77	return htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring_32);
  78}
  79
  80static size_t ath10k_htt_get_rx_ring_size_64(struct ath10k_htt *htt)
  81{
  82	return htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring_64);
  83}
  84
  85static void ath10k_htt_config_paddrs_ring_32(struct ath10k_htt *htt,
  86					     void *vaddr)
  87{
  88	htt->rx_ring.paddrs_ring_32 = vaddr;
  89}
  90
  91static void ath10k_htt_config_paddrs_ring_64(struct ath10k_htt *htt,
  92					     void *vaddr)
  93{
  94	htt->rx_ring.paddrs_ring_64 = vaddr;
  95}
  96
  97static void ath10k_htt_set_paddrs_ring_32(struct ath10k_htt *htt,
  98					  dma_addr_t paddr, int idx)
  99{
 100	htt->rx_ring.paddrs_ring_32[idx] = __cpu_to_le32(paddr);
 101}
 102
 103static void ath10k_htt_set_paddrs_ring_64(struct ath10k_htt *htt,
 104					  dma_addr_t paddr, int idx)
 105{
 106	htt->rx_ring.paddrs_ring_64[idx] = __cpu_to_le64(paddr);
 107}
 108
 109static void ath10k_htt_reset_paddrs_ring_32(struct ath10k_htt *htt, int idx)
 110{
 111	htt->rx_ring.paddrs_ring_32[idx] = 0;
 112}
 113
 114static void ath10k_htt_reset_paddrs_ring_64(struct ath10k_htt *htt, int idx)
 115{
 116	htt->rx_ring.paddrs_ring_64[idx] = 0;
 117}
 118
 119static void *ath10k_htt_get_vaddr_ring_32(struct ath10k_htt *htt)
 120{
 121	return (void *)htt->rx_ring.paddrs_ring_32;
 122}
 123
 124static void *ath10k_htt_get_vaddr_ring_64(struct ath10k_htt *htt)
 125{
 126	return (void *)htt->rx_ring.paddrs_ring_64;
 127}
 128
 129static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
 130{
 131	struct htt_rx_desc *rx_desc;
 132	struct ath10k_skb_rxcb *rxcb;
 133	struct sk_buff *skb;
 134	dma_addr_t paddr;
 135	int ret = 0, idx;
 136
 137	/* The Full Rx Reorder firmware has no way of telling the host
 138	 * implicitly when it copied HTT Rx Ring buffers to MAC Rx Ring.
 139	 * To keep things simple make sure ring is always half empty. This
 140	 * guarantees there'll be no replenishment overruns possible.
 141	 */
 142	BUILD_BUG_ON(HTT_RX_RING_FILL_LEVEL >= HTT_RX_RING_SIZE / 2);
 143
 144	idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
 145
 146	if (idx < 0 || idx >= htt->rx_ring.size) {
 147		ath10k_err(htt->ar, "rx ring index is not valid, firmware malfunctioning?\n");
 148		idx &= htt->rx_ring.size_mask;
 149		ret = -ENOMEM;
 150		goto fail;
 151	}
 152
 153	while (num > 0) {
 154		skb = dev_alloc_skb(HTT_RX_BUF_SIZE + HTT_RX_DESC_ALIGN);
 155		if (!skb) {
 156			ret = -ENOMEM;
 157			goto fail;
 158		}
 159
 160		if (!IS_ALIGNED((unsigned long)skb->data, HTT_RX_DESC_ALIGN))
 161			skb_pull(skb,
 162				 PTR_ALIGN(skb->data, HTT_RX_DESC_ALIGN) -
 163				 skb->data);
 164
 165		/* Clear rx_desc attention word before posting to Rx ring */
 166		rx_desc = (struct htt_rx_desc *)skb->data;
 167		rx_desc->attention.flags = __cpu_to_le32(0);
 168
 169		paddr = dma_map_single(htt->ar->dev, skb->data,
 170				       skb->len + skb_tailroom(skb),
 171				       DMA_FROM_DEVICE);
 172
 173		if (unlikely(dma_mapping_error(htt->ar->dev, paddr))) {
 174			dev_kfree_skb_any(skb);
 175			ret = -ENOMEM;
 176			goto fail;
 177		}
 178
 179		rxcb = ATH10K_SKB_RXCB(skb);
 180		rxcb->paddr = paddr;
 181		htt->rx_ring.netbufs_ring[idx] = skb;
 182		ath10k_htt_set_paddrs_ring(htt, paddr, idx);
 183		htt->rx_ring.fill_cnt++;
 184
 185		if (htt->rx_ring.in_ord_rx) {
 186			hash_add(htt->rx_ring.skb_table,
 187				 &ATH10K_SKB_RXCB(skb)->hlist,
 188				 paddr);
 189		}
 190
 191		num--;
 192		idx++;
 193		idx &= htt->rx_ring.size_mask;
 194	}
 195
 196fail:
 197	/*
 198	 * Make sure the rx buffer is updated before available buffer
 199	 * index to avoid any potential rx ring corruption.
 200	 */
 201	mb();
 202	*htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
 203	return ret;
 204}
 205
 206static int ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
 207{
 208	lockdep_assert_held(&htt->rx_ring.lock);
 209	return __ath10k_htt_rx_ring_fill_n(htt, num);
 210}
 211
 212static void ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt *htt)
 213{
 214	int ret, num_deficit, num_to_fill;
 215
 216	/* Refilling the whole RX ring buffer proves to be a bad idea. The
 217	 * reason is RX may take up significant amount of CPU cycles and starve
 218	 * other tasks, e.g. TX on an ethernet device while acting as a bridge
 219	 * with ath10k wlan interface. This ended up with very poor performance
 220	 * once CPU the host system was overwhelmed with RX on ath10k.
 221	 *
 222	 * By limiting the number of refills the replenishing occurs
 223	 * progressively. This in turns makes use of the fact tasklets are
 224	 * processed in FIFO order. This means actual RX processing can starve
 225	 * out refilling. If there's not enough buffers on RX ring FW will not
 226	 * report RX until it is refilled with enough buffers. This
 227	 * automatically balances load wrt to CPU power.
 228	 *
 229	 * This probably comes at a cost of lower maximum throughput but
 230	 * improves the average and stability.
 231	 */
 232	spin_lock_bh(&htt->rx_ring.lock);
 233	num_deficit = htt->rx_ring.fill_level - htt->rx_ring.fill_cnt;
 234	num_to_fill = min(ATH10K_HTT_MAX_NUM_REFILL, num_deficit);
 235	num_deficit -= num_to_fill;
 236	ret = ath10k_htt_rx_ring_fill_n(htt, num_to_fill);
 237	if (ret == -ENOMEM) {
 238		/*
 239		 * Failed to fill it to the desired level -
 240		 * we'll start a timer and try again next time.
 241		 * As long as enough buffers are left in the ring for
 242		 * another A-MPDU rx, no special recovery is needed.
 243		 */
 244		mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
 245			  msecs_to_jiffies(HTT_RX_RING_REFILL_RETRY_MS));
 246	} else if (num_deficit > 0) {
 247		mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
 248			  msecs_to_jiffies(HTT_RX_RING_REFILL_RESCHED_MS));
 249	}
 250	spin_unlock_bh(&htt->rx_ring.lock);
 251}
 252
 253static void ath10k_htt_rx_ring_refill_retry(struct timer_list *t)
 254{
 255	struct ath10k_htt *htt = from_timer(htt, t, rx_ring.refill_retry_timer);
 256
 257	ath10k_htt_rx_msdu_buff_replenish(htt);
 258}
 259
 260int ath10k_htt_rx_ring_refill(struct ath10k *ar)
 261{
 262	struct ath10k_htt *htt = &ar->htt;
 263	int ret;
 264
 265	if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
 266		return 0;
 267
 268	spin_lock_bh(&htt->rx_ring.lock);
 269	ret = ath10k_htt_rx_ring_fill_n(htt, (htt->rx_ring.fill_level -
 270					      htt->rx_ring.fill_cnt));
 271
 272	if (ret)
 273		ath10k_htt_rx_ring_free(htt);
 274
 275	spin_unlock_bh(&htt->rx_ring.lock);
 276
 277	return ret;
 278}
 279
 280void ath10k_htt_rx_free(struct ath10k_htt *htt)
 281{
 282	if (htt->ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
 283		return;
 284
 285	del_timer_sync(&htt->rx_ring.refill_retry_timer);
 286
 287	skb_queue_purge(&htt->rx_msdus_q);
 288	skb_queue_purge(&htt->rx_in_ord_compl_q);
 289	skb_queue_purge(&htt->tx_fetch_ind_q);
 290
 291	spin_lock_bh(&htt->rx_ring.lock);
 292	ath10k_htt_rx_ring_free(htt);
 293	spin_unlock_bh(&htt->rx_ring.lock);
 294
 295	dma_free_coherent(htt->ar->dev,
 296			  ath10k_htt_get_rx_ring_size(htt),
 297			  ath10k_htt_get_vaddr_ring(htt),
 298			  htt->rx_ring.base_paddr);
 299
 300	dma_free_coherent(htt->ar->dev,
 301			  sizeof(*htt->rx_ring.alloc_idx.vaddr),
 302			  htt->rx_ring.alloc_idx.vaddr,
 303			  htt->rx_ring.alloc_idx.paddr);
 304
 305	kfree(htt->rx_ring.netbufs_ring);
 306}
 307
 308static inline struct sk_buff *ath10k_htt_rx_netbuf_pop(struct ath10k_htt *htt)
 309{
 310	struct ath10k *ar = htt->ar;
 311	int idx;
 312	struct sk_buff *msdu;
 313
 314	lockdep_assert_held(&htt->rx_ring.lock);
 315
 316	if (htt->rx_ring.fill_cnt == 0) {
 317		ath10k_warn(ar, "tried to pop sk_buff from an empty rx ring\n");
 318		return NULL;
 319	}
 320
 321	idx = htt->rx_ring.sw_rd_idx.msdu_payld;
 322	msdu = htt->rx_ring.netbufs_ring[idx];
 323	htt->rx_ring.netbufs_ring[idx] = NULL;
 324	ath10k_htt_reset_paddrs_ring(htt, idx);
 325
 326	idx++;
 327	idx &= htt->rx_ring.size_mask;
 328	htt->rx_ring.sw_rd_idx.msdu_payld = idx;
 329	htt->rx_ring.fill_cnt--;
 330
 331	dma_unmap_single(htt->ar->dev,
 332			 ATH10K_SKB_RXCB(msdu)->paddr,
 333			 msdu->len + skb_tailroom(msdu),
 334			 DMA_FROM_DEVICE);
 335	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
 336			msdu->data, msdu->len + skb_tailroom(msdu));
 337
 338	return msdu;
 339}
 340
 341/* return: < 0 fatal error, 0 - non chained msdu, 1 chained msdu */
 342static int ath10k_htt_rx_amsdu_pop(struct ath10k_htt *htt,
 343				   struct sk_buff_head *amsdu)
 344{
 345	struct ath10k *ar = htt->ar;
 346	int msdu_len, msdu_chaining = 0;
 347	struct sk_buff *msdu;
 348	struct htt_rx_desc *rx_desc;
 349
 350	lockdep_assert_held(&htt->rx_ring.lock);
 351
 352	for (;;) {
 353		int last_msdu, msdu_len_invalid, msdu_chained;
 354
 355		msdu = ath10k_htt_rx_netbuf_pop(htt);
 356		if (!msdu) {
 357			__skb_queue_purge(amsdu);
 358			return -ENOENT;
 359		}
 360
 361		__skb_queue_tail(amsdu, msdu);
 362
 363		rx_desc = (struct htt_rx_desc *)msdu->data;
 364
 365		/* FIXME: we must report msdu payload since this is what caller
 366		 * expects now
 367		 */
 368		skb_put(msdu, offsetof(struct htt_rx_desc, msdu_payload));
 369		skb_pull(msdu, offsetof(struct htt_rx_desc, msdu_payload));
 370
 371		/*
 372		 * Sanity check - confirm the HW is finished filling in the
 373		 * rx data.
 374		 * If the HW and SW are working correctly, then it's guaranteed
 375		 * that the HW's MAC DMA is done before this point in the SW.
 376		 * To prevent the case that we handle a stale Rx descriptor,
 377		 * just assert for now until we have a way to recover.
 378		 */
 379		if (!(__le32_to_cpu(rx_desc->attention.flags)
 380				& RX_ATTENTION_FLAGS_MSDU_DONE)) {
 381			__skb_queue_purge(amsdu);
 382			return -EIO;
 383		}
 384
 385		msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
 386					& (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
 387					   RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
 388		msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.common.info0),
 389			      RX_MSDU_START_INFO0_MSDU_LENGTH);
 390		msdu_chained = rx_desc->frag_info.ring2_more_count;
 391
 392		if (msdu_len_invalid)
 393			msdu_len = 0;
 394
 395		skb_trim(msdu, 0);
 396		skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
 397		msdu_len -= msdu->len;
 398
 399		/* Note: Chained buffers do not contain rx descriptor */
 400		while (msdu_chained--) {
 401			msdu = ath10k_htt_rx_netbuf_pop(htt);
 402			if (!msdu) {
 403				__skb_queue_purge(amsdu);
 404				return -ENOENT;
 405			}
 406
 407			__skb_queue_tail(amsdu, msdu);
 408			skb_trim(msdu, 0);
 409			skb_put(msdu, min(msdu_len, HTT_RX_BUF_SIZE));
 410			msdu_len -= msdu->len;
 411			msdu_chaining = 1;
 412		}
 413
 414		last_msdu = __le32_to_cpu(rx_desc->msdu_end.common.info0) &
 415				RX_MSDU_END_INFO0_LAST_MSDU;
 416
 417		trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
 418					 sizeof(*rx_desc) - sizeof(u32));
 419
 420		if (last_msdu)
 421			break;
 422	}
 423
 424	if (skb_queue_empty(amsdu))
 425		msdu_chaining = -1;
 426
 427	/*
 428	 * Don't refill the ring yet.
 429	 *
 430	 * First, the elements popped here are still in use - it is not
 431	 * safe to overwrite them until the matching call to
 432	 * mpdu_desc_list_next. Second, for efficiency it is preferable to
 433	 * refill the rx ring with 1 PPDU's worth of rx buffers (something
 434	 * like 32 x 3 buffers), rather than one MPDU's worth of rx buffers
 435	 * (something like 3 buffers). Consequently, we'll rely on the txrx
 436	 * SW to tell us when it is done pulling all the PPDU's rx buffers
 437	 * out of the rx ring, and then refill it just once.
 438	 */
 439
 440	return msdu_chaining;
 441}
 442
 443static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
 444					       u64 paddr)
 445{
 446	struct ath10k *ar = htt->ar;
 447	struct ath10k_skb_rxcb *rxcb;
 448	struct sk_buff *msdu;
 449
 450	lockdep_assert_held(&htt->rx_ring.lock);
 451
 452	msdu = ath10k_htt_rx_find_skb_paddr(ar, paddr);
 453	if (!msdu)
 454		return NULL;
 455
 456	rxcb = ATH10K_SKB_RXCB(msdu);
 457	hash_del(&rxcb->hlist);
 458	htt->rx_ring.fill_cnt--;
 459
 460	dma_unmap_single(htt->ar->dev, rxcb->paddr,
 461			 msdu->len + skb_tailroom(msdu),
 462			 DMA_FROM_DEVICE);
 463	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
 464			msdu->data, msdu->len + skb_tailroom(msdu));
 465
 466	return msdu;
 467}
 468
 469static inline void ath10k_htt_append_frag_list(struct sk_buff *skb_head,
 470					       struct sk_buff *frag_list,
 471					       unsigned int frag_len)
 472{
 473	skb_shinfo(skb_head)->frag_list = frag_list;
 474	skb_head->data_len = frag_len;
 475	skb_head->len += skb_head->data_len;
 476}
 477
 478static int ath10k_htt_rx_handle_amsdu_mon_32(struct ath10k_htt *htt,
 479					     struct sk_buff *msdu,
 480					     struct htt_rx_in_ord_msdu_desc **msdu_desc)
 481{
 482	struct ath10k *ar = htt->ar;
 483	u32 paddr;
 484	struct sk_buff *frag_buf;
 485	struct sk_buff *prev_frag_buf;
 486	u8 last_frag;
 487	struct htt_rx_in_ord_msdu_desc *ind_desc = *msdu_desc;
 488	struct htt_rx_desc *rxd;
 489	int amsdu_len = __le16_to_cpu(ind_desc->msdu_len);
 490
 491	rxd = (void *)msdu->data;
 492	trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
 493
 494	skb_put(msdu, sizeof(struct htt_rx_desc));
 495	skb_pull(msdu, sizeof(struct htt_rx_desc));
 496	skb_put(msdu, min(amsdu_len, HTT_RX_MSDU_SIZE));
 497	amsdu_len -= msdu->len;
 498
 499	last_frag = ind_desc->reserved;
 500	if (last_frag) {
 501		if (amsdu_len) {
 502			ath10k_warn(ar, "invalid amsdu len %u, left %d",
 503				    __le16_to_cpu(ind_desc->msdu_len),
 504				    amsdu_len);
 505		}
 506		return 0;
 507	}
 508
 509	ind_desc++;
 510	paddr = __le32_to_cpu(ind_desc->msdu_paddr);
 511	frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
 512	if (!frag_buf) {
 513		ath10k_warn(ar, "failed to pop frag-1 paddr: 0x%x", paddr);
 514		return -ENOENT;
 515	}
 516
 517	skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
 518	ath10k_htt_append_frag_list(msdu, frag_buf, amsdu_len);
 519
 520	amsdu_len -= frag_buf->len;
 521	prev_frag_buf = frag_buf;
 522	last_frag = ind_desc->reserved;
 523	while (!last_frag) {
 524		ind_desc++;
 525		paddr = __le32_to_cpu(ind_desc->msdu_paddr);
 526		frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
 527		if (!frag_buf) {
 528			ath10k_warn(ar, "failed to pop frag-n paddr: 0x%x",
 529				    paddr);
 530			prev_frag_buf->next = NULL;
 531			return -ENOENT;
 532		}
 533
 534		skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
 535		last_frag = ind_desc->reserved;
 536		amsdu_len -= frag_buf->len;
 537
 538		prev_frag_buf->next = frag_buf;
 539		prev_frag_buf = frag_buf;
 540	}
 541
 542	if (amsdu_len) {
 543		ath10k_warn(ar, "invalid amsdu len %u, left %d",
 544			    __le16_to_cpu(ind_desc->msdu_len), amsdu_len);
 545	}
 546
 547	*msdu_desc = ind_desc;
 548
 549	prev_frag_buf->next = NULL;
 550	return 0;
 551}
 552
 553static int
 554ath10k_htt_rx_handle_amsdu_mon_64(struct ath10k_htt *htt,
 555				  struct sk_buff *msdu,
 556				  struct htt_rx_in_ord_msdu_desc_ext **msdu_desc)
 557{
 558	struct ath10k *ar = htt->ar;
 559	u64 paddr;
 560	struct sk_buff *frag_buf;
 561	struct sk_buff *prev_frag_buf;
 562	u8 last_frag;
 563	struct htt_rx_in_ord_msdu_desc_ext *ind_desc = *msdu_desc;
 564	struct htt_rx_desc *rxd;
 565	int amsdu_len = __le16_to_cpu(ind_desc->msdu_len);
 566
 567	rxd = (void *)msdu->data;
 568	trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
 569
 570	skb_put(msdu, sizeof(struct htt_rx_desc));
 571	skb_pull(msdu, sizeof(struct htt_rx_desc));
 572	skb_put(msdu, min(amsdu_len, HTT_RX_MSDU_SIZE));
 573	amsdu_len -= msdu->len;
 574
 575	last_frag = ind_desc->reserved;
 576	if (last_frag) {
 577		if (amsdu_len) {
 578			ath10k_warn(ar, "invalid amsdu len %u, left %d",
 579				    __le16_to_cpu(ind_desc->msdu_len),
 580				    amsdu_len);
 581		}
 582		return 0;
 583	}
 584
 585	ind_desc++;
 586	paddr = __le64_to_cpu(ind_desc->msdu_paddr);
 587	frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
 588	if (!frag_buf) {
 589		ath10k_warn(ar, "failed to pop frag-1 paddr: 0x%llx", paddr);
 590		return -ENOENT;
 591	}
 592
 593	skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
 594	ath10k_htt_append_frag_list(msdu, frag_buf, amsdu_len);
 595
 596	amsdu_len -= frag_buf->len;
 597	prev_frag_buf = frag_buf;
 598	last_frag = ind_desc->reserved;
 599	while (!last_frag) {
 600		ind_desc++;
 601		paddr = __le64_to_cpu(ind_desc->msdu_paddr);
 602		frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
 603		if (!frag_buf) {
 604			ath10k_warn(ar, "failed to pop frag-n paddr: 0x%llx",
 605				    paddr);
 606			prev_frag_buf->next = NULL;
 607			return -ENOENT;
 608		}
 609
 610		skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
 611		last_frag = ind_desc->reserved;
 612		amsdu_len -= frag_buf->len;
 613
 614		prev_frag_buf->next = frag_buf;
 615		prev_frag_buf = frag_buf;
 616	}
 617
 618	if (amsdu_len) {
 619		ath10k_warn(ar, "invalid amsdu len %u, left %d",
 620			    __le16_to_cpu(ind_desc->msdu_len), amsdu_len);
 621	}
 622
 623	*msdu_desc = ind_desc;
 624
 625	prev_frag_buf->next = NULL;
 626	return 0;
 627}
 628
 629static int ath10k_htt_rx_pop_paddr32_list(struct ath10k_htt *htt,
 630					  struct htt_rx_in_ord_ind *ev,
 631					  struct sk_buff_head *list)
 632{
 633	struct ath10k *ar = htt->ar;
 634	struct htt_rx_in_ord_msdu_desc *msdu_desc = ev->msdu_descs32;
 635	struct htt_rx_desc *rxd;
 636	struct sk_buff *msdu;
 637	int msdu_count, ret;
 638	bool is_offload;
 639	u32 paddr;
 640
 641	lockdep_assert_held(&htt->rx_ring.lock);
 642
 643	msdu_count = __le16_to_cpu(ev->msdu_count);
 644	is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
 645
 646	while (msdu_count--) {
 647		paddr = __le32_to_cpu(msdu_desc->msdu_paddr);
 648
 649		msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
 650		if (!msdu) {
 651			__skb_queue_purge(list);
 652			return -ENOENT;
 653		}
 654
 655		if (!is_offload && ar->monitor_arvif) {
 656			ret = ath10k_htt_rx_handle_amsdu_mon_32(htt, msdu,
 657								&msdu_desc);
 658			if (ret) {
 659				__skb_queue_purge(list);
 660				return ret;
 661			}
 662			__skb_queue_tail(list, msdu);
 663			msdu_desc++;
 664			continue;
 665		}
 666
 667		__skb_queue_tail(list, msdu);
 668
 669		if (!is_offload) {
 670			rxd = (void *)msdu->data;
 671
 672			trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
 673
 674			skb_put(msdu, sizeof(*rxd));
 675			skb_pull(msdu, sizeof(*rxd));
 676			skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
 677
 678			if (!(__le32_to_cpu(rxd->attention.flags) &
 679			      RX_ATTENTION_FLAGS_MSDU_DONE)) {
 680				ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
 681				return -EIO;
 682			}
 683		}
 684
 685		msdu_desc++;
 686	}
 687
 688	return 0;
 689}
 690
 691static int ath10k_htt_rx_pop_paddr64_list(struct ath10k_htt *htt,
 692					  struct htt_rx_in_ord_ind *ev,
 693					  struct sk_buff_head *list)
 694{
 695	struct ath10k *ar = htt->ar;
 696	struct htt_rx_in_ord_msdu_desc_ext *msdu_desc = ev->msdu_descs64;
 697	struct htt_rx_desc *rxd;
 698	struct sk_buff *msdu;
 699	int msdu_count, ret;
 700	bool is_offload;
 701	u64 paddr;
 702
 703	lockdep_assert_held(&htt->rx_ring.lock);
 704
 705	msdu_count = __le16_to_cpu(ev->msdu_count);
 706	is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
 707
 708	while (msdu_count--) {
 709		paddr = __le64_to_cpu(msdu_desc->msdu_paddr);
 710		msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
 711		if (!msdu) {
 712			__skb_queue_purge(list);
 713			return -ENOENT;
 714		}
 715
 716		if (!is_offload && ar->monitor_arvif) {
 717			ret = ath10k_htt_rx_handle_amsdu_mon_64(htt, msdu,
 718								&msdu_desc);
 719			if (ret) {
 720				__skb_queue_purge(list);
 721				return ret;
 722			}
 723			__skb_queue_tail(list, msdu);
 724			msdu_desc++;
 725			continue;
 726		}
 727
 728		__skb_queue_tail(list, msdu);
 729
 730		if (!is_offload) {
 731			rxd = (void *)msdu->data;
 732
 733			trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
 734
 735			skb_put(msdu, sizeof(*rxd));
 736			skb_pull(msdu, sizeof(*rxd));
 737			skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
 738
 739			if (!(__le32_to_cpu(rxd->attention.flags) &
 740			      RX_ATTENTION_FLAGS_MSDU_DONE)) {
 741				ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
 742				return -EIO;
 743			}
 744		}
 745
 746		msdu_desc++;
 747	}
 748
 749	return 0;
 750}
 751
 752int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
 753{
 754	struct ath10k *ar = htt->ar;
 755	dma_addr_t paddr;
 756	void *vaddr, *vaddr_ring;
 757	size_t size;
 758	struct timer_list *timer = &htt->rx_ring.refill_retry_timer;
 759
 760	if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
 761		return 0;
 762
 763	htt->rx_confused = false;
 764
 765	/* XXX: The fill level could be changed during runtime in response to
 766	 * the host processing latency. Is this really worth it?
 767	 */
 768	htt->rx_ring.size = HTT_RX_RING_SIZE;
 769	htt->rx_ring.size_mask = htt->rx_ring.size - 1;
 770	htt->rx_ring.fill_level = ar->hw_params.rx_ring_fill_level;
 771
 772	if (!is_power_of_2(htt->rx_ring.size)) {
 773		ath10k_warn(ar, "htt rx ring size is not power of 2\n");
 774		return -EINVAL;
 775	}
 776
 777	htt->rx_ring.netbufs_ring =
 778		kcalloc(htt->rx_ring.size, sizeof(struct sk_buff *),
 779			GFP_KERNEL);
 780	if (!htt->rx_ring.netbufs_ring)
 781		goto err_netbuf;
 782
 783	size = ath10k_htt_get_rx_ring_size(htt);
 784
 785	vaddr_ring = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_KERNEL);
 786	if (!vaddr_ring)
 787		goto err_dma_ring;
 788
 789	ath10k_htt_config_paddrs_ring(htt, vaddr_ring);
 790	htt->rx_ring.base_paddr = paddr;
 791
 792	vaddr = dma_alloc_coherent(htt->ar->dev,
 793				   sizeof(*htt->rx_ring.alloc_idx.vaddr),
 794				   &paddr, GFP_KERNEL);
 795	if (!vaddr)
 796		goto err_dma_idx;
 797
 798	htt->rx_ring.alloc_idx.vaddr = vaddr;
 799	htt->rx_ring.alloc_idx.paddr = paddr;
 800	htt->rx_ring.sw_rd_idx.msdu_payld = htt->rx_ring.size_mask;
 801	*htt->rx_ring.alloc_idx.vaddr = 0;
 802
 803	/* Initialize the Rx refill retry timer */
 804	timer_setup(timer, ath10k_htt_rx_ring_refill_retry, 0);
 805
 806	spin_lock_init(&htt->rx_ring.lock);
 807
 808	htt->rx_ring.fill_cnt = 0;
 809	htt->rx_ring.sw_rd_idx.msdu_payld = 0;
 810	hash_init(htt->rx_ring.skb_table);
 811
 812	skb_queue_head_init(&htt->rx_msdus_q);
 813	skb_queue_head_init(&htt->rx_in_ord_compl_q);
 814	skb_queue_head_init(&htt->tx_fetch_ind_q);
 815	atomic_set(&htt->num_mpdus_ready, 0);
 816
 817	ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
 818		   htt->rx_ring.size, htt->rx_ring.fill_level);
 819	return 0;
 820
 821err_dma_idx:
 822	dma_free_coherent(htt->ar->dev,
 823			  ath10k_htt_get_rx_ring_size(htt),
 824			  vaddr_ring,
 825			  htt->rx_ring.base_paddr);
 826err_dma_ring:
 827	kfree(htt->rx_ring.netbufs_ring);
 828err_netbuf:
 829	return -ENOMEM;
 830}
 831
 832static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
 833					  enum htt_rx_mpdu_encrypt_type type)
 834{
 835	switch (type) {
 836	case HTT_RX_MPDU_ENCRYPT_NONE:
 837		return 0;
 838	case HTT_RX_MPDU_ENCRYPT_WEP40:
 839	case HTT_RX_MPDU_ENCRYPT_WEP104:
 840		return IEEE80211_WEP_IV_LEN;
 841	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
 842	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
 843		return IEEE80211_TKIP_IV_LEN;
 844	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
 845		return IEEE80211_CCMP_HDR_LEN;
 846	case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
 847		return IEEE80211_CCMP_256_HDR_LEN;
 848	case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
 849	case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
 850		return IEEE80211_GCMP_HDR_LEN;
 851	case HTT_RX_MPDU_ENCRYPT_WEP128:
 852	case HTT_RX_MPDU_ENCRYPT_WAPI:
 853		break;
 854	}
 855
 856	ath10k_warn(ar, "unsupported encryption type %d\n", type);
 857	return 0;
 858}
 859
 860#define MICHAEL_MIC_LEN 8
 861
 862static int ath10k_htt_rx_crypto_mic_len(struct ath10k *ar,
 863					enum htt_rx_mpdu_encrypt_type type)
 864{
 865	switch (type) {
 866	case HTT_RX_MPDU_ENCRYPT_NONE:
 867	case HTT_RX_MPDU_ENCRYPT_WEP40:
 868	case HTT_RX_MPDU_ENCRYPT_WEP104:
 869	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
 870	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
 871		return 0;
 872	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
 873		return IEEE80211_CCMP_MIC_LEN;
 874	case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
 875		return IEEE80211_CCMP_256_MIC_LEN;
 876	case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
 877	case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
 878		return IEEE80211_GCMP_MIC_LEN;
 879	case HTT_RX_MPDU_ENCRYPT_WEP128:
 880	case HTT_RX_MPDU_ENCRYPT_WAPI:
 881		break;
 882	}
 883
 884	ath10k_warn(ar, "unsupported encryption type %d\n", type);
 885	return 0;
 886}
 887
 888static int ath10k_htt_rx_crypto_icv_len(struct ath10k *ar,
 889					enum htt_rx_mpdu_encrypt_type type)
 890{
 891	switch (type) {
 892	case HTT_RX_MPDU_ENCRYPT_NONE:
 893	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
 894	case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
 895	case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
 896	case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
 897		return 0;
 898	case HTT_RX_MPDU_ENCRYPT_WEP40:
 899	case HTT_RX_MPDU_ENCRYPT_WEP104:
 900		return IEEE80211_WEP_ICV_LEN;
 901	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
 902	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
 903		return IEEE80211_TKIP_ICV_LEN;
 904	case HTT_RX_MPDU_ENCRYPT_WEP128:
 905	case HTT_RX_MPDU_ENCRYPT_WAPI:
 906		break;
 907	}
 908
 909	ath10k_warn(ar, "unsupported encryption type %d\n", type);
 910	return 0;
 911}
 912
 913struct amsdu_subframe_hdr {
 914	u8 dst[ETH_ALEN];
 915	u8 src[ETH_ALEN];
 916	__be16 len;
 917} __packed;
 918
 919#define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)
 920
 921static inline u8 ath10k_bw_to_mac80211_bw(u8 bw)
 922{
 923	u8 ret = 0;
 924
 925	switch (bw) {
 926	case 0:
 927		ret = RATE_INFO_BW_20;
 928		break;
 929	case 1:
 930		ret = RATE_INFO_BW_40;
 931		break;
 932	case 2:
 933		ret = RATE_INFO_BW_80;
 934		break;
 935	case 3:
 936		ret = RATE_INFO_BW_160;
 937		break;
 938	}
 939
 940	return ret;
 941}
 942
 943static void ath10k_htt_rx_h_rates(struct ath10k *ar,
 944				  struct ieee80211_rx_status *status,
 945				  struct htt_rx_desc *rxd)
 946{
 947	struct ieee80211_supported_band *sband;
 948	u8 cck, rate, bw, sgi, mcs, nss;
 949	u8 preamble = 0;
 950	u8 group_id;
 951	u32 info1, info2, info3;
 952	u32 stbc, nsts_su;
 953
 954	info1 = __le32_to_cpu(rxd->ppdu_start.info1);
 955	info2 = __le32_to_cpu(rxd->ppdu_start.info2);
 956	info3 = __le32_to_cpu(rxd->ppdu_start.info3);
 957
 958	preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
 959
 960	switch (preamble) {
 961	case HTT_RX_LEGACY:
 962		/* To get legacy rate index band is required. Since band can't
 963		 * be undefined check if freq is non-zero.
 964		 */
 965		if (!status->freq)
 966			return;
 967
 968		cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
 969		rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
 970		rate &= ~RX_PPDU_START_RATE_FLAG;
 971
 972		sband = &ar->mac.sbands[status->band];
 973		status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate, cck);
 974		break;
 975	case HTT_RX_HT:
 976	case HTT_RX_HT_WITH_TXBF:
 977		/* HT-SIG - Table 20-11 in info2 and info3 */
 978		mcs = info2 & 0x1F;
 979		nss = mcs >> 3;
 980		bw = (info2 >> 7) & 1;
 981		sgi = (info3 >> 7) & 1;
 982
 983		status->rate_idx = mcs;
 984		status->encoding = RX_ENC_HT;
 985		if (sgi)
 986			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
 987		if (bw)
 988			status->bw = RATE_INFO_BW_40;
 989		break;
 990	case HTT_RX_VHT:
 991	case HTT_RX_VHT_WITH_TXBF:
 992		/* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
 993		 * TODO check this
 994		 */
 995		bw = info2 & 3;
 996		sgi = info3 & 1;
 997		stbc = (info2 >> 3) & 1;
 998		group_id = (info2 >> 4) & 0x3F;
 999
1000		if (GROUP_ID_IS_SU_MIMO(group_id)) {
1001			mcs = (info3 >> 4) & 0x0F;
1002			nsts_su = ((info2 >> 10) & 0x07);
1003			if (stbc)
1004				nss = (nsts_su >> 2) + 1;
1005			else
1006				nss = (nsts_su + 1);
1007		} else {
1008			/* Hardware doesn't decode VHT-SIG-B into Rx descriptor
1009			 * so it's impossible to decode MCS. Also since
1010			 * firmware consumes Group Id Management frames host
1011			 * has no knowledge regarding group/user position
1012			 * mapping so it's impossible to pick the correct Nsts
1013			 * from VHT-SIG-A1.
1014			 *
1015			 * Bandwidth and SGI are valid so report the rateinfo
1016			 * on best-effort basis.
1017			 */
1018			mcs = 0;
1019			nss = 1;
1020		}
1021
1022		if (mcs > 0x09) {
1023			ath10k_warn(ar, "invalid MCS received %u\n", mcs);
1024			ath10k_warn(ar, "rxd %08x mpdu start %08x %08x msdu start %08x %08x ppdu start %08x %08x %08x %08x %08x\n",
1025				    __le32_to_cpu(rxd->attention.flags),
1026				    __le32_to_cpu(rxd->mpdu_start.info0),
1027				    __le32_to_cpu(rxd->mpdu_start.info1),
1028				    __le32_to_cpu(rxd->msdu_start.common.info0),
1029				    __le32_to_cpu(rxd->msdu_start.common.info1),
1030				    rxd->ppdu_start.info0,
1031				    __le32_to_cpu(rxd->ppdu_start.info1),
1032				    __le32_to_cpu(rxd->ppdu_start.info2),
1033				    __le32_to_cpu(rxd->ppdu_start.info3),
1034				    __le32_to_cpu(rxd->ppdu_start.info4));
1035
1036			ath10k_warn(ar, "msdu end %08x mpdu end %08x\n",
1037				    __le32_to_cpu(rxd->msdu_end.common.info0),
1038				    __le32_to_cpu(rxd->mpdu_end.info0));
1039
1040			ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL,
1041					"rx desc msdu payload: ",
1042					rxd->msdu_payload, 50);
1043		}
1044
1045		status->rate_idx = mcs;
1046		status->nss = nss;
1047
1048		if (sgi)
1049			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1050
1051		status->bw = ath10k_bw_to_mac80211_bw(bw);
1052		status->encoding = RX_ENC_VHT;
1053		break;
1054	default:
1055		break;
1056	}
1057}
1058
1059static struct ieee80211_channel *
1060ath10k_htt_rx_h_peer_channel(struct ath10k *ar, struct htt_rx_desc *rxd)
1061{
1062	struct ath10k_peer *peer;
1063	struct ath10k_vif *arvif;
1064	struct cfg80211_chan_def def;
1065	u16 peer_id;
1066
1067	lockdep_assert_held(&ar->data_lock);
1068
1069	if (!rxd)
1070		return NULL;
1071
1072	if (rxd->attention.flags &
1073	    __cpu_to_le32(RX_ATTENTION_FLAGS_PEER_IDX_INVALID))
1074		return NULL;
1075
1076	if (!(rxd->msdu_end.common.info0 &
1077	      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU)))
1078		return NULL;
1079
1080	peer_id = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1081		     RX_MPDU_START_INFO0_PEER_IDX);
1082
1083	peer = ath10k_peer_find_by_id(ar, peer_id);
1084	if (!peer)
1085		return NULL;
1086
1087	arvif = ath10k_get_arvif(ar, peer->vdev_id);
1088	if (WARN_ON_ONCE(!arvif))
1089		return NULL;
1090
1091	if (ath10k_mac_vif_chan(arvif->vif, &def))
1092		return NULL;
1093
1094	return def.chan;
1095}
1096
1097static struct ieee80211_channel *
1098ath10k_htt_rx_h_vdev_channel(struct ath10k *ar, u32 vdev_id)
1099{
1100	struct ath10k_vif *arvif;
1101	struct cfg80211_chan_def def;
1102
1103	lockdep_assert_held(&ar->data_lock);
1104
1105	list_for_each_entry(arvif, &ar->arvifs, list) {
1106		if (arvif->vdev_id == vdev_id &&
1107		    ath10k_mac_vif_chan(arvif->vif, &def) == 0)
1108			return def.chan;
1109	}
1110
1111	return NULL;
1112}
1113
1114static void
1115ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw *hw,
1116			      struct ieee80211_chanctx_conf *conf,
1117			      void *data)
1118{
1119	struct cfg80211_chan_def *def = data;
1120
1121	*def = conf->def;
1122}
1123
1124static struct ieee80211_channel *
1125ath10k_htt_rx_h_any_channel(struct ath10k *ar)
1126{
1127	struct cfg80211_chan_def def = {};
1128
1129	ieee80211_iter_chan_contexts_atomic(ar->hw,
1130					    ath10k_htt_rx_h_any_chan_iter,
1131					    &def);
1132
1133	return def.chan;
1134}
1135
1136static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
1137				    struct ieee80211_rx_status *status,
1138				    struct htt_rx_desc *rxd,
1139				    u32 vdev_id)
1140{
1141	struct ieee80211_channel *ch;
1142
1143	spin_lock_bh(&ar->data_lock);
1144	ch = ar->scan_channel;
1145	if (!ch)
1146		ch = ar->rx_channel;
1147	if (!ch)
1148		ch = ath10k_htt_rx_h_peer_channel(ar, rxd);
1149	if (!ch)
1150		ch = ath10k_htt_rx_h_vdev_channel(ar, vdev_id);
1151	if (!ch)
1152		ch = ath10k_htt_rx_h_any_channel(ar);
1153	if (!ch)
1154		ch = ar->tgt_oper_chan;
1155	spin_unlock_bh(&ar->data_lock);
1156
1157	if (!ch)
1158		return false;
1159
1160	status->band = ch->band;
1161	status->freq = ch->center_freq;
1162
1163	return true;
1164}
1165
1166static void ath10k_htt_rx_h_signal(struct ath10k *ar,
1167				   struct ieee80211_rx_status *status,
1168				   struct htt_rx_desc *rxd)
1169{
1170	int i;
1171
1172	for (i = 0; i < IEEE80211_MAX_CHAINS ; i++) {
1173		status->chains &= ~BIT(i);
1174
1175		if (rxd->ppdu_start.rssi_chains[i].pri20_mhz != 0x80) {
1176			status->chain_signal[i] = ATH10K_DEFAULT_NOISE_FLOOR +
1177				rxd->ppdu_start.rssi_chains[i].pri20_mhz;
1178
1179			status->chains |= BIT(i);
1180		}
1181	}
1182
1183	/* FIXME: Get real NF */
1184	status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
1185			 rxd->ppdu_start.rssi_comb;
1186	status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
1187}
1188
1189static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
1190				    struct ieee80211_rx_status *status,
1191				    struct htt_rx_desc *rxd)
1192{
1193	/* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
1194	 * means all prior MSDUs in a PPDU are reported to mac80211 without the
1195	 * TSF. Is it worth holding frames until end of PPDU is known?
1196	 *
1197	 * FIXME: Can we get/compute 64bit TSF?
1198	 */
1199	status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
1200	status->flag |= RX_FLAG_MACTIME_END;
1201}
1202
1203static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
1204				 struct sk_buff_head *amsdu,
1205				 struct ieee80211_rx_status *status,
1206				 u32 vdev_id)
1207{
1208	struct sk_buff *first;
1209	struct htt_rx_desc *rxd;
1210	bool is_first_ppdu;
1211	bool is_last_ppdu;
1212
1213	if (skb_queue_empty(amsdu))
1214		return;
1215
1216	first = skb_peek(amsdu);
1217	rxd = (void *)first->data - sizeof(*rxd);
1218
1219	is_first_ppdu = !!(rxd->attention.flags &
1220			   __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
1221	is_last_ppdu = !!(rxd->attention.flags &
1222			  __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));
1223
1224	if (is_first_ppdu) {
1225		/* New PPDU starts so clear out the old per-PPDU status. */
1226		status->freq = 0;
1227		status->rate_idx = 0;
1228		status->nss = 0;
1229		status->encoding = RX_ENC_LEGACY;
1230		status->bw = RATE_INFO_BW_20;
1231
1232		status->flag &= ~RX_FLAG_MACTIME_END;
1233		status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1234
1235		status->flag &= ~(RX_FLAG_AMPDU_IS_LAST);
1236		status->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
1237		status->ampdu_reference = ar->ampdu_reference;
1238
1239		ath10k_htt_rx_h_signal(ar, status, rxd);
1240		ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
1241		ath10k_htt_rx_h_rates(ar, status, rxd);
1242	}
1243
1244	if (is_last_ppdu) {
1245		ath10k_htt_rx_h_mactime(ar, status, rxd);
1246
1247		/* set ampdu last segment flag */
1248		status->flag |= RX_FLAG_AMPDU_IS_LAST;
1249		ar->ampdu_reference++;
1250	}
1251}
1252
1253static const char * const tid_to_ac[] = {
1254	"BE",
1255	"BK",
1256	"BK",
1257	"BE",
1258	"VI",
1259	"VI",
1260	"VO",
1261	"VO",
1262};
1263
1264static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
1265{
1266	u8 *qc;
1267	int tid;
1268
1269	if (!ieee80211_is_data_qos(hdr->frame_control))
1270		return "";
1271
1272	qc = ieee80211_get_qos_ctl(hdr);
1273	tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
1274	if (tid < 8)
1275		snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
1276	else
1277		snprintf(out, size, "tid %d", tid);
1278
1279	return out;
1280}
1281
1282static void ath10k_htt_rx_h_queue_msdu(struct ath10k *ar,
1283				       struct ieee80211_rx_status *rx_status,
1284				       struct sk_buff *skb)
1285{
1286	struct ieee80211_rx_status *status;
1287
1288	status = IEEE80211_SKB_RXCB(skb);
1289	*status = *rx_status;
1290
1291	skb_queue_tail(&ar->htt.rx_msdus_q, skb);
1292}
1293
1294static void ath10k_process_rx(struct ath10k *ar, struct sk_buff *skb)
1295{
1296	struct ieee80211_rx_status *status;
1297	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1298	char tid[32];
1299
1300	status = IEEE80211_SKB_RXCB(skb);
1301
1302	if (!(ar->filter_flags & FIF_FCSFAIL) &&
1303	    status->flag & RX_FLAG_FAILED_FCS_CRC) {
1304		ar->stats.rx_crc_err_drop++;
1305		dev_kfree_skb_any(skb);
1306		return;
1307	}
1308
1309	ath10k_dbg(ar, ATH10K_DBG_DATA,
1310		   "rx skb %pK len %u peer %pM %s %s sn %u %s%s%s%s%s%s %srate_idx %u vht_nss %u freq %u band %u flag 0x%x fcs-err %i mic-err %i amsdu-more %i\n",
1311		   skb,
1312		   skb->len,
1313		   ieee80211_get_SA(hdr),
1314		   ath10k_get_tid(hdr, tid, sizeof(tid)),
1315		   is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
1316							"mcast" : "ucast",
1317		   (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
1318		   (status->encoding == RX_ENC_LEGACY) ? "legacy" : "",
1319		   (status->encoding == RX_ENC_HT) ? "ht" : "",
1320		   (status->encoding == RX_ENC_VHT) ? "vht" : "",
1321		   (status->bw == RATE_INFO_BW_40) ? "40" : "",
1322		   (status->bw == RATE_INFO_BW_80) ? "80" : "",
1323		   (status->bw == RATE_INFO_BW_160) ? "160" : "",
1324		   status->enc_flags & RX_ENC_FLAG_SHORT_GI ? "sgi " : "",
1325		   status->rate_idx,
1326		   status->nss,
1327		   status->freq,
1328		   status->band, status->flag,
1329		   !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
1330		   !!(status->flag & RX_FLAG_MMIC_ERROR),
1331		   !!(status->flag & RX_FLAG_AMSDU_MORE));
1332	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
1333			skb->data, skb->len);
1334	trace_ath10k_rx_hdr(ar, skb->data, skb->len);
1335	trace_ath10k_rx_payload(ar, skb->data, skb->len);
1336
1337	ieee80211_rx_napi(ar->hw, NULL, skb, &ar->napi);
1338}
1339
1340static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
1341				      struct ieee80211_hdr *hdr)
1342{
1343	int len = ieee80211_hdrlen(hdr->frame_control);
1344
1345	if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
1346		      ar->running_fw->fw_file.fw_features))
1347		len = round_up(len, 4);
1348
1349	return len;
1350}
1351
1352static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
1353					struct sk_buff *msdu,
1354					struct ieee80211_rx_status *status,
1355					enum htt_rx_mpdu_encrypt_type enctype,
1356					bool is_decrypted,
1357					const u8 first_hdr[64])
1358{
1359	struct ieee80211_hdr *hdr;
1360	struct htt_rx_desc *rxd;
1361	size_t hdr_len;
1362	size_t crypto_len;
1363	bool is_first;
1364	bool is_last;
1365	bool msdu_limit_err;
1366	int bytes_aligned = ar->hw_params.decap_align_bytes;
1367	u8 *qos;
1368
1369	rxd = (void *)msdu->data - sizeof(*rxd);
1370	is_first = !!(rxd->msdu_end.common.info0 &
1371		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
1372	is_last = !!(rxd->msdu_end.common.info0 &
1373		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
1374
1375	/* Delivered decapped frame:
1376	 * [802.11 header]
1377	 * [crypto param] <-- can be trimmed if !fcs_err &&
1378	 *                    !decrypt_err && !peer_idx_invalid
1379	 * [amsdu header] <-- only if A-MSDU
1380	 * [rfc1042/llc]
1381	 * [payload]
1382	 * [FCS] <-- at end, needs to be trimmed
1383	 */
1384
1385	/* Some hardwares(QCA99x0 variants) limit number of msdus in a-msdu when
1386	 * deaggregate, so that unwanted MSDU-deaggregation is avoided for
1387	 * error packets. If limit exceeds, hw sends all remaining MSDUs as
1388	 * a single last MSDU with this msdu limit error set.
1389	 */
1390	msdu_limit_err = ath10k_rx_desc_msdu_limit_error(&ar->hw_params, rxd);
1391
1392	/* If MSDU limit error happens, then don't warn on, the partial raw MSDU
1393	 * without first MSDU is expected in that case, and handled later here.
1394	 */
1395	/* This probably shouldn't happen but warn just in case */
1396	if (WARN_ON_ONCE(!is_first && !msdu_limit_err))
1397		return;
1398
1399	/* This probably shouldn't happen but warn just in case */
1400	if (WARN_ON_ONCE(!(is_first && is_last) && !msdu_limit_err))
1401		return;
1402
1403	skb_trim(msdu, msdu->len - FCS_LEN);
1404
1405	/* Push original 80211 header */
1406	if (unlikely(msdu_limit_err)) {
1407		hdr = (struct ieee80211_hdr *)first_hdr;
1408		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1409		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1410
1411		if (ieee80211_is_data_qos(hdr->frame_control)) {
1412			qos = ieee80211_get_qos_ctl(hdr);
1413			qos[0] |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1414		}
1415
1416		if (crypto_len)
1417			memcpy(skb_push(msdu, crypto_len),
1418			       (void *)hdr + round_up(hdr_len, bytes_aligned),
1419			       crypto_len);
1420
1421		memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1422	}
1423
1424	/* In most cases this will be true for sniffed frames. It makes sense
1425	 * to deliver them as-is without stripping the crypto param. This is
1426	 * necessary for software based decryption.
1427	 *
1428	 * If there's no error then the frame is decrypted. At least that is
1429	 * the case for frames that come in via fragmented rx indication.
1430	 */
1431	if (!is_decrypted)
1432		return;
1433
1434	/* The payload is decrypted so strip crypto params. Start from tail
1435	 * since hdr is used to compute some stuff.
1436	 */
1437
1438	hdr = (void *)msdu->data;
1439
1440	/* Tail */
1441	if (status->flag & RX_FLAG_IV_STRIPPED) {
1442		skb_trim(msdu, msdu->len -
1443			 ath10k_htt_rx_crypto_mic_len(ar, enctype));
1444
1445		skb_trim(msdu, msdu->len -
1446			 ath10k_htt_rx_crypto_icv_len(ar, enctype));
1447	} else {
1448		/* MIC */
1449		if (status->flag & RX_FLAG_MIC_STRIPPED)
1450			skb_trim(msdu, msdu->len -
1451				 ath10k_htt_rx_crypto_mic_len(ar, enctype));
1452
1453		/* ICV */
1454		if (status->flag & RX_FLAG_ICV_STRIPPED)
1455			skb_trim(msdu, msdu->len -
1456				 ath10k_htt_rx_crypto_icv_len(ar, enctype));
1457	}
1458
1459	/* MMIC */
1460	if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
1461	    !ieee80211_has_morefrags(hdr->frame_control) &&
1462	    enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
1463		skb_trim(msdu, msdu->len - MICHAEL_MIC_LEN);
1464
1465	/* Head */
1466	if (status->flag & RX_FLAG_IV_STRIPPED) {
1467		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1468		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1469
1470		memmove((void *)msdu->data + crypto_len,
1471			(void *)msdu->data, hdr_len);
1472		skb_pull(msdu, crypto_len);
1473	}
1474}
1475
1476static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
1477					  struct sk_buff *msdu,
1478					  struct ieee80211_rx_status *status,
1479					  const u8 first_hdr[64],
1480					  enum htt_rx_mpdu_encrypt_type enctype)
1481{
1482	struct ieee80211_hdr *hdr;
1483	struct htt_rx_desc *rxd;
1484	size_t hdr_len;
1485	u8 da[ETH_ALEN];
1486	u8 sa[ETH_ALEN];
1487	int l3_pad_bytes;
1488	int bytes_aligned = ar->hw_params.decap_align_bytes;
1489
1490	/* Delivered decapped frame:
1491	 * [nwifi 802.11 header] <-- replaced with 802.11 hdr
1492	 * [rfc1042/llc]
1493	 *
1494	 * Note: The nwifi header doesn't have QoS Control and is
1495	 * (always?) a 3addr frame.
1496	 *
1497	 * Note2: There's no A-MSDU subframe header. Even if it's part
1498	 * of an A-MSDU.
1499	 */
1500
1501	/* pull decapped header and copy SA & DA */
1502	rxd = (void *)msdu->data - sizeof(*rxd);
1503
1504	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
1505	skb_put(msdu, l3_pad_bytes);
1506
1507	hdr = (struct ieee80211_hdr *)(msdu->data + l3_pad_bytes);
1508
1509	hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
1510	ether_addr_copy(da, ieee80211_get_DA(hdr));
1511	ether_addr_copy(sa, ieee80211_get_SA(hdr));
1512	skb_pull(msdu, hdr_len);
1513
1514	/* push original 802.11 header */
1515	hdr = (struct ieee80211_hdr *)first_hdr;
1516	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1517
1518	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1519		memcpy(skb_push(msdu,
1520				ath10k_htt_rx_crypto_param_len(ar, enctype)),
1521		       (void *)hdr + round_up(hdr_len, bytes_aligned),
1522			ath10k_htt_rx_crypto_param_len(ar, enctype));
1523	}
1524
1525	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1526
1527	/* original 802.11 header has a different DA and in
1528	 * case of 4addr it may also have different SA
1529	 */
1530	hdr = (struct ieee80211_hdr *)msdu->data;
1531	ether_addr_copy(ieee80211_get_DA(hdr), da);
1532	ether_addr_copy(ieee80211_get_SA(hdr), sa);
1533}
1534
1535static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
1536					  struct sk_buff *msdu,
1537					  enum htt_rx_mpdu_encrypt_type enctype)
1538{
1539	struct ieee80211_hdr *hdr;
1540	struct htt_rx_desc *rxd;
1541	size_t hdr_len, crypto_len;
1542	void *rfc1042;
1543	bool is_first, is_last, is_amsdu;
1544	int bytes_aligned = ar->hw_params.decap_align_bytes;
1545
1546	rxd = (void *)msdu->data - sizeof(*rxd);
1547	hdr = (void *)rxd->rx_hdr_status;
1548
1549	is_first = !!(rxd->msdu_end.common.info0 &
1550		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
1551	is_last = !!(rxd->msdu_end.common.info0 &
1552		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
1553	is_amsdu = !(is_first && is_last);
1554
1555	rfc1042 = hdr;
1556
1557	if (is_first) {
1558		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1559		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1560
1561		rfc1042 += round_up(hdr_len, bytes_aligned) +
1562			   round_up(crypto_len, bytes_aligned);
1563	}
1564
1565	if (is_amsdu)
1566		rfc1042 += sizeof(struct amsdu_subframe_hdr);
1567
1568	return rfc1042;
1569}
1570
1571static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
1572					struct sk_buff *msdu,
1573					struct ieee80211_rx_status *status,
1574					const u8 first_hdr[64],
1575					enum htt_rx_mpdu_encrypt_type enctype)
1576{
1577	struct ieee80211_hdr *hdr;
1578	struct ethhdr *eth;
1579	size_t hdr_len;
1580	void *rfc1042;
1581	u8 da[ETH_ALEN];
1582	u8 sa[ETH_ALEN];
1583	int l3_pad_bytes;
1584	struct htt_rx_desc *rxd;
1585	int bytes_aligned = ar->hw_params.decap_align_bytes;
1586
1587	/* Delivered decapped frame:
1588	 * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
1589	 * [payload]
1590	 */
1591
1592	rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
1593	if (WARN_ON_ONCE(!rfc1042))
1594		return;
1595
1596	rxd = (void *)msdu->data - sizeof(*rxd);
1597	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
1598	skb_put(msdu, l3_pad_bytes);
1599	skb_pull(msdu, l3_pad_bytes);
1600
1601	/* pull decapped header and copy SA & DA */
1602	eth = (struct ethhdr *)msdu->data;
1603	ether_addr_copy(da, eth->h_dest);
1604	ether_addr_copy(sa, eth->h_source);
1605	skb_pull(msdu, sizeof(struct ethhdr));
1606
1607	/* push rfc1042/llc/snap */
1608	memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
1609	       sizeof(struct rfc1042_hdr));
1610
1611	/* push original 802.11 header */
1612	hdr = (struct ieee80211_hdr *)first_hdr;
1613	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1614
1615	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1616		memcpy(skb_push(msdu,
1617				ath10k_htt_rx_crypto_param_len(ar, enctype)),
1618		       (void *)hdr + round_up(hdr_len, bytes_aligned),
1619			ath10k_htt_rx_crypto_param_len(ar, enctype));
1620	}
1621
1622	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1623
1624	/* original 802.11 header has a different DA and in
1625	 * case of 4addr it may also have different SA
1626	 */
1627	hdr = (struct ieee80211_hdr *)msdu->data;
1628	ether_addr_copy(ieee80211_get_DA(hdr), da);
1629	ether_addr_copy(ieee80211_get_SA(hdr), sa);
1630}
1631
1632static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
1633					 struct sk_buff *msdu,
1634					 struct ieee80211_rx_status *status,
1635					 const u8 first_hdr[64],
1636					 enum htt_rx_mpdu_encrypt_type enctype)
1637{
1638	struct ieee80211_hdr *hdr;
1639	size_t hdr_len;
1640	int l3_pad_bytes;
1641	struct htt_rx_desc *rxd;
1642	int bytes_aligned = ar->hw_params.decap_align_bytes;
1643
1644	/* Delivered decapped frame:
1645	 * [amsdu header] <-- replaced with 802.11 hdr
1646	 * [rfc1042/llc]
1647	 * [payload]
1648	 */
1649
1650	rxd = (void *)msdu->data - sizeof(*rxd);
1651	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
1652
1653	skb_put(msdu, l3_pad_bytes);
1654	skb_pull(msdu, sizeof(struct amsdu_subframe_hdr) + l3_pad_bytes);
1655
1656	hdr = (struct ieee80211_hdr *)first_hdr;
1657	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1658
1659	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1660		memcpy(skb_push(msdu,
1661				ath10k_htt_rx_crypto_param_len(ar, enctype)),
1662		       (void *)hdr + round_up(hdr_len, bytes_aligned),
1663			ath10k_htt_rx_crypto_param_len(ar, enctype));
1664	}
1665
1666	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1667}
1668
1669static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
1670				    struct sk_buff *msdu,
1671				    struct ieee80211_rx_status *status,
1672				    u8 first_hdr[64],
1673				    enum htt_rx_mpdu_encrypt_type enctype,
1674				    bool is_decrypted)
1675{
1676	struct htt_rx_desc *rxd;
1677	enum rx_msdu_decap_format decap;
1678
1679	/* First msdu's decapped header:
1680	 * [802.11 header] <-- padded to 4 bytes long
1681	 * [crypto param] <-- padded to 4 bytes long
1682	 * [amsdu header] <-- only if A-MSDU
1683	 * [rfc1042/llc]
1684	 *
1685	 * Other (2nd, 3rd, ..) msdu's decapped header:
1686	 * [amsdu header] <-- only if A-MSDU
1687	 * [rfc1042/llc]
1688	 */
1689
1690	rxd = (void *)msdu->data - sizeof(*rxd);
1691	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
1692		   RX_MSDU_START_INFO1_DECAP_FORMAT);
1693
1694	switch (decap) {
1695	case RX_MSDU_DECAP_RAW:
1696		ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
1697					    is_decrypted, first_hdr);
1698		break;
1699	case RX_MSDU_DECAP_NATIVE_WIFI:
1700		ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr,
1701					      enctype);
1702		break;
1703	case RX_MSDU_DECAP_ETHERNET2_DIX:
1704		ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
1705		break;
1706	case RX_MSDU_DECAP_8023_SNAP_LLC:
1707		ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr,
1708					     enctype);
1709		break;
1710	}
1711}
1712
1713static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
1714{
1715	struct htt_rx_desc *rxd;
1716	u32 flags, info;
1717	bool is_ip4, is_ip6;
1718	bool is_tcp, is_udp;
1719	bool ip_csum_ok, tcpudp_csum_ok;
1720
1721	rxd = (void *)skb->data - sizeof(*rxd);
1722	flags = __le32_to_cpu(rxd->attention.flags);
1723	info = __le32_to_cpu(rxd->msdu_start.common.info1);
1724
1725	is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
1726	is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
1727	is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
1728	is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
1729	ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
1730	tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
1731
1732	if (!is_ip4 && !is_ip6)
1733		return CHECKSUM_NONE;
1734	if (!is_tcp && !is_udp)
1735		return CHECKSUM_NONE;
1736	if (!ip_csum_ok)
1737		return CHECKSUM_NONE;
1738	if (!tcpudp_csum_ok)
1739		return CHECKSUM_NONE;
1740
1741	return CHECKSUM_UNNECESSARY;
1742}
1743
1744static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
1745{
1746	msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
1747}
1748
1749static u64 ath10k_htt_rx_h_get_pn(struct ath10k *ar, struct sk_buff *skb,
1750				  u16 offset,
1751				  enum htt_rx_mpdu_encrypt_type enctype)
1752{
1753	struct ieee80211_hdr *hdr;
1754	u64 pn = 0;
1755	u8 *ehdr;
1756
1757	hdr = (struct ieee80211_hdr *)(skb->data + offset);
1758	ehdr = skb->data + offset + ieee80211_hdrlen(hdr->frame_control);
1759
1760	if (enctype == HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2) {
1761		pn = ehdr[0];
1762		pn |= (u64)ehdr[1] << 8;
1763		pn |= (u64)ehdr[4] << 16;
1764		pn |= (u64)ehdr[5] << 24;
1765		pn |= (u64)ehdr[6] << 32;
1766		pn |= (u64)ehdr[7] << 40;
1767	}
1768	return pn;
1769}
1770
1771static bool ath10k_htt_rx_h_frag_multicast_check(struct ath10k *ar,
1772						 struct sk_buff *skb,
1773						 u16 offset)
1774{
1775	struct ieee80211_hdr *hdr;
1776
1777	hdr = (struct ieee80211_hdr *)(skb->data + offset);
1778	return !is_multicast_ether_addr(hdr->addr1);
1779}
1780
1781static bool ath10k_htt_rx_h_frag_pn_check(struct ath10k *ar,
1782					  struct sk_buff *skb,
1783					  u16 peer_id,
1784					  u16 offset,
1785					  enum htt_rx_mpdu_encrypt_type enctype)
1786{
1787	struct ath10k_peer *peer;
1788	union htt_rx_pn_t *last_pn, new_pn = {0};
1789	struct ieee80211_hdr *hdr;
1790	u8 tid, frag_number;
1791	u32 seq;
1792
1793	peer = ath10k_peer_find_by_id(ar, peer_id);
1794	if (!peer) {
1795		ath10k_dbg(ar, ATH10K_DBG_HTT, "invalid peer for frag pn check\n");
1796		return false;
1797	}
1798
1799	hdr = (struct ieee80211_hdr *)(skb->data + offset);
1800	if (ieee80211_is_data_qos(hdr->frame_control))
1801		tid = ieee80211_get_tid(hdr);
1802	else
1803		tid = ATH10K_TXRX_NON_QOS_TID;
1804
1805	last_pn = &peer->frag_tids_last_pn[tid];
1806	new_pn.pn48 = ath10k_htt_rx_h_get_pn(ar, skb, offset, enctype);
1807	frag_number = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
1808	seq = (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
1809
1810	if (frag_number == 0) {
1811		last_pn->pn48 = new_pn.pn48;
1812		peer->frag_tids_seq[tid] = seq;
1813	} else {
1814		if (seq != peer->frag_tids_seq[tid])
1815			return false;
1816
1817		if (new_pn.pn48 != last_pn->pn48 + 1)
1818			return false;
1819
1820		last_pn->pn48 = new_pn.pn48;
1821	}
1822
1823	return true;
1824}
1825
1826static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
1827				 struct sk_buff_head *amsdu,
1828				 struct ieee80211_rx_status *status,
1829				 bool fill_crypt_header,
1830				 u8 *rx_hdr,
1831				 enum ath10k_pkt_rx_err *err,
1832				 u16 peer_id,
1833				 bool frag)
1834{
1835	struct sk_buff *first;
1836	struct sk_buff *last;
1837	struct sk_buff *msdu, *temp;
1838	struct htt_rx_desc *rxd;
1839	struct ieee80211_hdr *hdr;
1840	enum htt_rx_mpdu_encrypt_type enctype;
1841	u8 first_hdr[64];
1842	u8 *qos;
1843	bool has_fcs_err;
1844	bool has_crypto_err;
1845	bool has_tkip_err;
1846	bool has_peer_idx_invalid;
1847	bool is_decrypted;
1848	bool is_mgmt;
1849	u32 attention;
1850	bool frag_pn_check = true, multicast_check = true;
1851
1852	if (skb_queue_empty(amsdu))
1853		return;
1854
1855	first = skb_peek(amsdu);
1856	rxd = (void *)first->data - sizeof(*rxd);
1857
1858	is_mgmt = !!(rxd->attention.flags &
1859		     __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));
1860
1861	enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1862		     RX_MPDU_START_INFO0_ENCRYPT_TYPE);
1863
1864	/* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
1865	 * decapped header. It'll be used for undecapping of each MSDU.
1866	 */
1867	hdr = (void *)rxd->rx_hdr_status;
1868	memcpy(first_hdr, hdr, RX_HTT_HDR_STATUS_LEN);
1869
1870	if (rx_hdr)
1871		memcpy(rx_hdr, hdr, RX_HTT_HDR_STATUS_LEN);
1872
1873	/* Each A-MSDU subframe will use the original header as the base and be
1874	 * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
1875	 */
1876	hdr = (void *)first_hdr;
1877
1878	if (ieee80211_is_data_qos(hdr->frame_control)) {
1879		qos = ieee80211_get_qos_ctl(hdr);
1880		qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1881	}
1882
1883	/* Some attention flags are valid only in the last MSDU. */
1884	last = skb_peek_tail(amsdu);
1885	rxd = (void *)last->data - sizeof(*rxd);
1886	attention = __le32_to_cpu(rxd->attention.flags);
1887
1888	has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
1889	has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
1890	has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
1891	has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);
1892
1893	/* Note: If hardware captures an encrypted frame that it can't decrypt,
1894	 * e.g. due to fcs error, missing peer or invalid key data it will
1895	 * report the frame as raw.
1896	 */
1897	is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
1898			!has_fcs_err &&
1899			!has_crypto_err &&
1900			!has_peer_idx_invalid);
1901
1902	/* Clear per-MPDU flags while leaving per-PPDU flags intact. */
1903	status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
1904			  RX_FLAG_MMIC_ERROR |
1905			  RX_FLAG_DECRYPTED |
1906			  RX_FLAG_IV_STRIPPED |
1907			  RX_FLAG_ONLY_MONITOR |
1908			  RX_FLAG_MMIC_STRIPPED);
1909
1910	if (has_fcs_err)
1911		status->flag |= RX_FLAG_FAILED_FCS_CRC;
1912
1913	if (has_tkip_err)
1914		status->flag |= RX_FLAG_MMIC_ERROR;
1915
1916	if (err) {
1917		if (has_fcs_err)
1918			*err = ATH10K_PKT_RX_ERR_FCS;
1919		else if (has_tkip_err)
1920			*err = ATH10K_PKT_RX_ERR_TKIP;
1921		else if (has_crypto_err)
1922			*err = ATH10K_PKT_RX_ERR_CRYPT;
1923		else if (has_peer_idx_invalid)
1924			*err = ATH10K_PKT_RX_ERR_PEER_IDX_INVAL;
1925	}
1926
1927	/* Firmware reports all necessary management frames via WMI already.
1928	 * They are not reported to monitor interfaces at all so pass the ones
1929	 * coming via HTT to monitor interfaces instead. This simplifies
1930	 * matters a lot.
1931	 */
1932	if (is_mgmt)
1933		status->flag |= RX_FLAG_ONLY_MONITOR;
1934
1935	if (is_decrypted) {
1936		status->flag |= RX_FLAG_DECRYPTED;
1937
1938		if (likely(!is_mgmt))
1939			status->flag |= RX_FLAG_MMIC_STRIPPED;
1940
1941		if (fill_crypt_header)
1942			status->flag |= RX_FLAG_MIC_STRIPPED |
1943					RX_FLAG_ICV_STRIPPED;
1944		else
1945			status->flag |= RX_FLAG_IV_STRIPPED;
1946	}
1947
1948	skb_queue_walk(amsdu, msdu) {
1949		if (frag && !fill_crypt_header && is_decrypted &&
1950		    enctype == HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2)
1951			frag_pn_check = ath10k_htt_rx_h_frag_pn_check(ar,
1952								      msdu,
1953								      peer_id,
1954								      0,
1955								      enctype);
1956
1957		if (frag)
1958			multicast_check = ath10k_htt_rx_h_frag_multicast_check(ar,
1959									       msdu,
1960									       0);
1961
1962		if (!frag_pn_check || !multicast_check) {
1963			/* Discard the fragment with invalid PN or multicast DA
1964			 */
1965			temp = msdu->prev;
1966			__skb_unlink(msdu, amsdu);
1967			dev_kfree_skb_any(msdu);
1968			msdu = temp;
1969			frag_pn_check = true;
1970			multicast_check = true;
1971			continue;
1972		}
1973
1974		ath10k_htt_rx_h_csum_offload(msdu);
1975
1976		if (frag && !fill_crypt_header &&
1977		    enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
1978			status->flag &= ~RX_FLAG_MMIC_STRIPPED;
1979
1980		ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
1981					is_decrypted);
1982
1983		/* Undecapping involves copying the original 802.11 header back
1984		 * to sk_buff. If frame is protected and hardware has decrypted
1985		 * it then remove the protected bit.
1986		 */
1987		if (!is_decrypted)
1988			continue;
1989		if (is_mgmt)
1990			continue;
1991
1992		if (fill_crypt_header)
1993			continue;
1994
1995		hdr = (void *)msdu->data;
1996		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1997
1998		if (frag && !fill_crypt_header &&
1999		    enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
2000			status->flag &= ~RX_FLAG_IV_STRIPPED &
2001					~RX_FLAG_MMIC_STRIPPED;
2002	}
2003}
2004
2005static void ath10k_htt_rx_h_enqueue(struct ath10k *ar,
2006				    struct sk_buff_head *amsdu,
2007				    struct ieee80211_rx_status *status)
2008{
2009	struct sk_buff *msdu;
2010	struct sk_buff *first_subframe;
2011
2012	first_subframe = skb_peek(amsdu);
2013
2014	while ((msdu = __skb_dequeue(amsdu))) {
2015		/* Setup per-MSDU flags */
2016		if (skb_queue_empty(amsdu))
2017			status->flag &= ~RX_FLAG_AMSDU_MORE;
2018		else
2019			status->flag |= RX_FLAG_AMSDU_MORE;
2020
2021		if (msdu == first_subframe) {
2022			first_subframe = NULL;
2023			status->flag &= ~RX_FLAG_ALLOW_SAME_PN;
2024		} else {
2025			status->flag |= RX_FLAG_ALLOW_SAME_PN;
2026		}
2027
2028		ath10k_htt_rx_h_queue_msdu(ar, status, msdu);
2029	}
2030}
2031
2032static int ath10k_unchain_msdu(struct sk_buff_head *amsdu,
2033			       unsigned long *unchain_cnt)
2034{
2035	struct sk_buff *skb, *first;
2036	int space;
2037	int total_len = 0;
2038	int amsdu_len = skb_queue_len(amsdu);
2039
2040	/* TODO:  Might could optimize this by using
2041	 * skb_try_coalesce or similar method to
2042	 * decrease copying, or maybe get mac80211 to
2043	 * provide a way to just receive a list of
2044	 * skb?
2045	 */
2046
2047	first = __skb_dequeue(amsdu);
2048
2049	/* Allocate total length all at once. */
2050	skb_queue_walk(amsdu, skb)
2051		total_len += skb->len;
2052
2053	space = total_len - skb_tailroom(first);
2054	if ((space > 0) &&
2055	    (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
2056		/* TODO:  bump some rx-oom error stat */
2057		/* put it back together so we can free the
2058		 * whole list at once.
2059		 */
2060		__skb_queue_head(amsdu, first);
2061		return -1;
2062	}
2063
2064	/* Walk list again, copying contents into
2065	 * msdu_head
2066	 */
2067	while ((skb = __skb_dequeue(amsdu))) {
2068		skb_copy_from_linear_data(skb, skb_put(first, skb->len),
2069					  skb->len);
2070		dev_kfree_skb_any(skb);
2071	}
2072
2073	__skb_queue_head(amsdu, first);
2074
2075	*unchain_cnt += amsdu_len - 1;
2076
2077	return 0;
2078}
2079
2080static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
2081				    struct sk_buff_head *amsdu,
2082				    unsigned long *drop_cnt,
2083				    unsigned long *unchain_cnt)
2084{
2085	struct sk_buff *first;
2086	struct htt_rx_desc *rxd;
2087	enum rx_msdu_decap_format decap;
2088
2089	first = skb_peek(amsdu);
2090	rxd = (void *)first->data - sizeof(*rxd);
2091	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
2092		   RX_MSDU_START_INFO1_DECAP_FORMAT);
2093
2094	/* FIXME: Current unchaining logic can only handle simple case of raw
2095	 * msdu chaining. If decapping is other than raw the chaining may be
2096	 * more complex and this isn't handled by the current code. Don't even
2097	 * try re-constructing such frames - it'll be pretty much garbage.
2098	 */
2099	if (decap != RX_MSDU_DECAP_RAW ||
2100	    skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
2101		*drop_cnt += skb_queue_len(amsdu);
2102		__skb_queue_purge(amsdu);
2103		return;
2104	}
2105
2106	ath10k_unchain_msdu(amsdu, unchain_cnt);
2107}
2108
2109static bool ath10k_htt_rx_validate_amsdu(struct ath10k *ar,
2110					 struct sk_buff_head *amsdu)
2111{
2112	u8 *subframe_hdr;
2113	struct sk_buff *first;
2114	bool is_first, is_last;
2115	struct htt_rx_desc *rxd;
2116	struct ieee80211_hdr *hdr;
2117	size_t hdr_len, crypto_len;
2118	enum htt_rx_mpdu_encrypt_type enctype;
2119	int bytes_aligned = ar->hw_params.decap_align_bytes;
2120
2121	first = skb_peek(amsdu);
2122
2123	rxd = (void *)first->data - sizeof(*rxd);
2124	hdr = (void *)rxd->rx_hdr_status;
2125
2126	is_first = !!(rxd->msdu_end.common.info0 &
2127		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
2128	is_last = !!(rxd->msdu_end.common.info0 &
2129		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
2130
2131	/* Return in case of non-aggregated msdu */
2132	if (is_first && is_last)
2133		return true;
2134
2135	/* First msdu flag is not set for the first msdu of the list */
2136	if (!is_first)
2137		return false;
2138
2139	enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
2140		     RX_MPDU_START_INFO0_ENCRYPT_TYPE);
2141
2142	hdr_len = ieee80211_hdrlen(hdr->frame_control);
2143	crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
2144
2145	subframe_hdr = (u8 *)hdr + round_up(hdr_len, bytes_aligned) +
2146		       crypto_len;
2147
2148	/* Validate if the amsdu has a proper first subframe.
2149	 * There are chances a single msdu can be received as amsdu when
2150	 * the unauthenticated amsdu flag of a QoS header
2151	 * gets flipped in non-SPP AMSDU's, in such cases the first
2152	 * subframe has llc/snap header in place of a valid da.
2153	 * return false if the da matches rfc1042 pattern
2154	 */
2155	if (ether_addr_equal(subframe_hdr, rfc1042_header))
2156		return false;
2157
2158	return true;
2159}
2160
2161static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
2162					struct sk_buff_head *amsdu,
2163					struct ieee80211_rx_status *rx_status)
2164{
2165	if (!rx_status->freq) {
2166		ath10k_dbg(ar, ATH10K_DBG_HTT, "no channel configured; ignoring frame(s)!\n");
2167		return false;
2168	}
2169
2170	if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
2171		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
2172		return false;
2173	}
2174
2175	if (!ath10k_htt_rx_validate_amsdu(ar, amsdu)) {
2176		ath10k_dbg(ar, ATH10K_DBG_HTT, "invalid amsdu received\n");
2177		return false;
2178	}
2179
2180	return true;
2181}
2182
2183static void ath10k_htt_rx_h_filter(struct ath10k *ar,
2184				   struct sk_buff_head *amsdu,
2185				   struct ieee80211_rx_status *rx_status,
2186				   unsigned long *drop_cnt)
2187{
2188	if (skb_queue_empty(amsdu))
2189		return;
2190
2191	if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
2192		return;
2193
2194	if (drop_cnt)
2195		*drop_cnt += skb_queue_len(amsdu);
2196
2197	__skb_queue_purge(amsdu);
2198}
2199
2200static int ath10k_htt_rx_handle_amsdu(struct ath10k_htt *htt)
2201{
2202	struct ath10k *ar = htt->ar;
2203	struct ieee80211_rx_status *rx_status = &htt->rx_status;
2204	struct sk_buff_head amsdu;
2205	int ret;
2206	unsigned long drop_cnt = 0;
2207	unsigned long unchain_cnt = 0;
2208	unsigned long drop_cnt_filter = 0;
2209	unsigned long msdus_to_queue, num_msdus;
2210	enum ath10k_pkt_rx_err err = ATH10K_PKT_RX_ERR_MAX;
2211	u8 first_hdr[RX_HTT_HDR_STATUS_LEN];
2212
2213	__skb_queue_head_init(&amsdu);
2214
2215	spin_lock_bh(&htt->rx_ring.lock);
2216	if (htt->rx_confused) {
2217		spin_unlock_bh(&htt->rx_ring.lock);
2218		return -EIO;
2219	}
2220	ret = ath10k_htt_rx_amsdu_pop(htt, &amsdu);
2221	spin_unlock_bh(&htt->rx_ring.lock);
2222
2223	if (ret < 0) {
2224		ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
2225		__skb_queue_purge(&amsdu);
2226		/* FIXME: It's probably a good idea to reboot the
2227		 * device instead of leaving it inoperable.
2228		 */
2229		htt->rx_confused = true;
2230		return ret;
2231	}
2232
2233	num_msdus = skb_queue_len(&amsdu);
2234
2235	ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
2236
2237	/* only for ret = 1 indicates chained msdus */
2238	if (ret > 0)
2239		ath10k_htt_rx_h_unchain(ar, &amsdu, &drop_cnt, &unchain_cnt);
2240
2241	ath10k_htt_rx_h_filter(ar, &amsdu, rx_status, &drop_cnt_filter);
2242	ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status, true, first_hdr, &err, 0,
2243			     false);
2244	msdus_to_queue = skb_queue_len(&amsdu);
2245	ath10k_htt_rx_h_enqueue(ar, &amsdu, rx_status);
2246
2247	ath10k_sta_update_rx_tid_stats(ar, first_hdr, num_msdus, err,
2248				       unchain_cnt, drop_cnt, drop_cnt_filter,
2249				       msdus_to_queue);
2250
2251	return 0;
2252}
2253
2254static void ath10k_htt_rx_mpdu_desc_pn_hl(struct htt_hl_rx_desc *rx_desc,
2255					  union htt_rx_pn_t *pn,
2256					  int pn_len_bits)
2257{
2258	switch (pn_len_bits) {
2259	case 48:
2260		pn->pn48 = __le32_to_cpu(rx_desc->pn_31_0) +
2261			   ((u64)(__le32_to_cpu(rx_desc->u0.pn_63_32) & 0xFFFF) << 32);
2262		break;
2263	case 24:
2264		pn->pn24 = __le32_to_cpu(rx_desc->pn_31_0);
2265		break;
2266	}
2267}
2268
2269static bool ath10k_htt_rx_pn_cmp48(union htt_rx_pn_t *new_pn,
2270				   union htt_rx_pn_t *old_pn)
2271{
2272	return ((new_pn->pn48 & 0xffffffffffffULL) <=
2273		(old_pn->pn48 & 0xffffffffffffULL));
2274}
2275
2276static bool ath10k_htt_rx_pn_check_replay_hl(struct ath10k *ar,
2277					     struct ath10k_peer *peer,
2278					     struct htt_rx_indication_hl *rx)
2279{
2280	bool last_pn_valid, pn_invalid = false;
2281	enum htt_txrx_sec_cast_type sec_index;
2282	enum htt_security_types sec_type;
2283	union htt_rx_pn_t new_pn = {0};
2284	struct htt_hl_rx_desc *rx_desc;
2285	union htt_rx_pn_t *last_pn;
2286	u32 rx_desc_info, tid;
2287	int num_mpdu_ranges;
2288
2289	lockdep_assert_held(&ar->data_lock);
2290
2291	if (!peer)
2292		return false;
2293
2294	if (!(rx->fw_desc.flags & FW_RX_DESC_FLAGS_FIRST_MSDU))
2295		return false;
2296
2297	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
2298			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
2299
2300	rx_desc = (struct htt_hl_rx_desc *)&rx->mpdu_ranges[num_mpdu_ranges];
2301	rx_desc_info = __le32_to_cpu(rx_desc->info);
2302
2303	if (!MS(rx_desc_info, HTT_RX_DESC_HL_INFO_ENCRYPTED))
2304		return false;
2305
2306	tid = MS(rx->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);
2307	last_pn_valid = peer->tids_last_pn_valid[tid];
2308	last_pn = &peer->tids_last_pn[tid];
2309
2310	if (MS(rx_desc_info, HTT_RX_DESC_HL_INFO_MCAST_BCAST))
2311		sec_index = HTT_TXRX_SEC_MCAST;
2312	else
2313		sec_index = HTT_TXRX_SEC_UCAST;
2314
2315	sec_type = peer->rx_pn[sec_index].sec_type;
2316	ath10k_htt_rx_mpdu_desc_pn_hl(rx_desc, &new_pn, peer->rx_pn[sec_index].pn_len);
2317
2318	if (sec_type != HTT_SECURITY_AES_CCMP &&
2319	    sec_type != HTT_SECURITY_TKIP &&
2320	    sec_type != HTT_SECURITY_TKIP_NOMIC)
2321		return false;
2322
2323	if (last_pn_valid)
2324		pn_invalid = ath10k_htt_rx_pn_cmp48(&new_pn, last_pn);
2325	else
2326		peer->tids_last_pn_valid[tid] = true;
2327
2328	if (!pn_invalid)
2329		last_pn->pn48 = new_pn.pn48;
2330
2331	return pn_invalid;
2332}
2333
2334static bool ath10k_htt_rx_proc_rx_ind_hl(struct ath10k_htt *htt,
2335					 struct htt_rx_indication_hl *rx,
2336					 struct sk_buff *skb,
2337					 enum htt_rx_pn_check_type check_pn_type,
2338					 enum htt_rx_tkip_demic_type tkip_mic_type)
2339{
2340	struct ath10k *ar = htt->ar;
2341	struct ath10k_peer *peer;
2342	struct htt_rx_indication_mpdu_range *mpdu_ranges;
2343	struct fw_rx_desc_hl *fw_desc;
2344	enum htt_txrx_sec_cast_type sec_index;
2345	enum htt_security_types sec_type;
2346	union htt_rx_pn_t new_pn = {0};
2347	struct htt_hl_rx_desc *rx_desc;
2348	struct ieee80211_hdr *hdr;
2349	struct ieee80211_rx_status *rx_status;
2350	u16 peer_id;
2351	u8 rx_desc_len;
2352	int num_mpdu_ranges;
2353	size_t tot_hdr_len;
2354	struct ieee80211_channel *ch;
2355	bool pn_invalid, qos, first_msdu;
2356	u32 tid, rx_desc_info;
2357
2358	peer_id = __le16_to_cpu(rx->hdr.peer_id);
2359	tid = MS(rx->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);
2360
2361	spin_lock_bh(&ar->data_lock);
2362	peer = ath10k_peer_find_by_id(ar, peer_id);
2363	spin_unlock_bh(&ar->data_lock);
2364	if (!peer && peer_id != HTT_INVALID_PEERID)
2365		ath10k_warn(ar, "Got RX ind from invalid peer: %u\n", peer_id);
2366
2367	if (!peer)
2368		return true;
2369
2370	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
2371			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
2372	mpdu_ranges = htt_rx_ind_get_mpdu_ranges_hl(rx);
2373	fw_desc = &rx->fw_desc;
2374	rx_desc_len = fw_desc->len;
2375
2376	if (fw_desc->u.bits.discard) {
2377		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt discard mpdu\n");
2378		goto err;
2379	}
2380
2381	/* I have not yet seen any case where num_mpdu_ranges > 1.
2382	 * qcacld does not seem handle that case either, so we introduce the
2383	 * same limitiation here as well.
2384	 */
2385	if (num_mpdu_ranges > 1)
2386		ath10k_warn(ar,
2387			    "Unsupported number of MPDU ranges: %d, ignoring all but the first\n",
2388			    num_mpdu_ranges);
2389
2390	if (mpdu_ranges->mpdu_range_status !=
2391	    HTT_RX_IND_MPDU_STATUS_OK &&
2392	    mpdu_ranges->mpdu_range_status !=
2393	    HTT_RX_IND_MPDU_STATUS_TKIP_MIC_ERR) {
2394		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt mpdu_range_status %d\n",
2395			   mpdu_ranges->mpdu_range_status);
2396		goto err;
2397	}
2398
2399	rx_desc = (struct htt_hl_rx_desc *)&rx->mpdu_ranges[num_mpdu_ranges];
2400	rx_desc_info = __le32_to_cpu(rx_desc->info);
2401
2402	if (MS(rx_desc_info, HTT_RX_DESC_HL_INFO_MCAST_BCAST))
2403		sec_index = HTT_TXRX_SEC_MCAST;
2404	else
2405		sec_index = HTT_TXRX_SEC_UCAST;
2406
2407	sec_type = peer->rx_pn[sec_index].sec_type;
2408	first_msdu = rx->fw_desc.flags & FW_RX_DESC_FLAGS_FIRST_MSDU;
2409
2410	ath10k_htt_rx_mpdu_desc_pn_hl(rx_desc, &new_pn, peer->rx_pn[sec_index].pn_len);
2411
2412	if (check_pn_type == HTT_RX_PN_CHECK && tid >= IEEE80211_NUM_TIDS) {
2413		spin_lock_bh(&ar->data_lock);
2414		pn_invalid = ath10k_htt_rx_pn_check_replay_hl(ar, peer, rx);
2415		spin_unlock_bh(&ar->data_lock);
2416
2417		if (pn_invalid)
2418			goto err;
2419	}
2420
2421	/* Strip off all headers before the MAC header before delivery to
2422	 * mac80211
2423	 */
2424	tot_hdr_len = sizeof(struct htt_resp_hdr) + sizeof(rx->hdr) +
2425		      sizeof(rx->ppdu) + sizeof(rx->prefix) +
2426		      sizeof(rx->fw_desc) +
2427		      sizeof(*mpdu_ranges) * num_mpdu_ranges + rx_desc_len;
2428
2429	skb_pull(skb, tot_hdr_len);
2430
2431	hdr = (struct ieee80211_hdr *)skb->data;
2432	qos = ieee80211_is_data_qos(hdr->frame_control);
2433
2434	rx_status = IEEE80211_SKB_RXCB(skb);
2435	memset(rx_status, 0, sizeof(*rx_status));
2436
2437	if (rx->ppdu.combined_rssi == 0) {
2438		/* SDIO firmware does not provide signal */
2439		rx_status->signal = 0;
2440		rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
2441	} else {
2442		rx_status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
2443			rx->ppdu.combined_rssi;
2444		rx_status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
2445	}
2446
2447	spin_lock_bh(&ar->data_lock);
2448	ch = ar->scan_channel;
2449	if (!ch)
2450		ch = ar->rx_channel;
2451	if (!ch)
2452		ch = ath10k_htt_rx_h_any_channel(ar);
2453	if (!ch)
2454		ch = ar->tgt_oper_chan;
2455	spin_unlock_bh(&ar->data_lock);
2456
2457	if (ch) {
2458		rx_status->band = ch->band;
2459		rx_status->freq = ch->center_freq;
2460	}
2461	if (rx->fw_desc.flags & FW_RX_DESC_FLAGS_LAST_MSDU)
2462		rx_status->flag &= ~RX_FLAG_AMSDU_MORE;
2463	else
2464		rx_status->flag |= RX_FLAG_AMSDU_MORE;
2465
2466	/* Not entirely sure about this, but all frames from the chipset has
2467	 * the protected flag set even though they have already been decrypted.
2468	 * Unmasking this flag is necessary in order for mac80211 not to drop
2469	 * the frame.
2470	 * TODO: Verify this is always the case or find out a way to check
2471	 * if there has been hw decryption.
2472	 */
2473	if (ieee80211_has_protected(hdr->frame_control)) {
2474		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
2475		rx_status->flag |= RX_FLAG_DECRYPTED |
2476				   RX_FLAG_IV_STRIPPED |
2477				   RX_FLAG_MMIC_STRIPPED;
2478
2479		if (tid < IEEE80211_NUM_TIDS &&
2480		    first_msdu &&
2481		    check_pn_type == HTT_RX_PN_CHECK &&
2482		   (sec_type == HTT_SECURITY_AES_CCMP ||
2483		    sec_type == HTT_SECURITY_TKIP ||
2484		    sec_type == HTT_SECURITY_TKIP_NOMIC)) {
2485			u8 offset, *ivp, i;
2486			s8 keyidx = 0;
2487			__le64 pn48 = cpu_to_le64(new_pn.pn48);
2488
2489			hdr = (struct ieee80211_hdr *)skb->data;
2490			offset = ieee80211_hdrlen(hdr->frame_control);
2491			hdr->frame_control |= __cpu_to_le16(IEEE80211_FCTL_PROTECTED);
2492			rx_status->flag &= ~RX_FLAG_IV_STRIPPED;
2493
2494			memmove(skb->data - IEEE80211_CCMP_HDR_LEN,
2495				skb->data, offset);
2496			skb_push(skb, IEEE80211_CCMP_HDR_LEN);
2497			ivp = skb->data + offset;
2498			memset(skb->data + offset, 0, IEEE80211_CCMP_HDR_LEN);
2499			/* Ext IV */
2500			ivp[IEEE80211_WEP_IV_LEN - 1] |= ATH10K_IEEE80211_EXTIV;
2501
2502			for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
2503				if (peer->keys[i] &&
2504				    peer->keys[i]->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2505					keyidx = peer->keys[i]->keyidx;
2506			}
2507
2508			/* Key ID */
2509			ivp[IEEE80211_WEP_IV_LEN - 1] |= keyidx << 6;
2510
2511			if (sec_type == HTT_SECURITY_AES_CCMP) {
2512				rx_status->flag |= RX_FLAG_MIC_STRIPPED;
2513				/* pn 0, pn 1 */
2514				memcpy(skb->data + offset, &pn48, 2);
2515				/* pn 1, pn 3 , pn 34 , pn 5 */
2516				memcpy(skb->data + offset + 4, ((u8 *)&pn48) + 2, 4);
2517			} else {
2518				rx_status->flag |= RX_FLAG_ICV_STRIPPED;
2519				/* TSC 0 */
2520				memcpy(skb->data + offset + 2, &pn48, 1);
2521				/* TSC 1 */
2522				memcpy(skb->data + offset, ((u8 *)&pn48) + 1, 1);
2523				/* TSC 2 , TSC 3 , TSC 4 , TSC 5*/
2524				memcpy(skb->data + offset + 4, ((u8 *)&pn48) + 2, 4);
2525			}
2526		}
2527	}
2528
2529	if (tkip_mic_type == HTT_RX_TKIP_MIC)
2530		rx_status->flag &= ~RX_FLAG_IV_STRIPPED &
2531				   ~RX_FLAG_MMIC_STRIPPED;
2532
2533	if (mpdu_ranges->mpdu_range_status == HTT_RX_IND_MPDU_STATUS_TKIP_MIC_ERR)
2534		rx_status->flag |= RX_FLAG_MMIC_ERROR;
2535
2536	if (!qos && tid < IEEE80211_NUM_TIDS) {
2537		u8 offset;
2538		__le16 qos_ctrl = 0;
2539
2540		hdr = (struct ieee80211_hdr *)skb->data;
2541		offset = ieee80211_hdrlen(hdr->frame_control);
2542
2543		hdr->frame_control |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2544		memmove(skb->data - IEEE80211_QOS_CTL_LEN, skb->data, offset);
2545		skb_push(skb, IEEE80211_QOS_CTL_LEN);
2546		qos_ctrl = cpu_to_le16(tid);
2547		memcpy(skb->data + offset, &qos_ctrl, IEEE80211_QOS_CTL_LEN);
2548	}
2549
2550	if (ar->napi.dev)
2551		ieee80211_rx_napi(ar->hw, NULL, skb, &ar->napi);
2552	else
2553		ieee80211_rx_ni(ar->hw, skb);
2554
2555	/* We have delivered the skb to the upper layers (mac80211) so we
2556	 * must not free it.
2557	 */
2558	return false;
2559err:
2560	/* Tell the caller that it must free the skb since we have not
2561	 * consumed it
2562	 */
2563	return true;
2564}
2565
2566static int ath10k_htt_rx_frag_tkip_decap_nomic(struct sk_buff *skb,
2567					       u16 head_len,
2568					       u16 hdr_len)
2569{
2570	u8 *ivp, *orig_hdr;
2571
2572	orig_hdr = skb->data;
2573	ivp = orig_hdr + hdr_len + head_len;
2574
2575	/* the ExtIV bit is always set to 1 for TKIP */
2576	if (!(ivp[IEEE80211_WEP_IV_LEN - 1] & ATH10K_IEEE80211_EXTIV))
2577		return -EINVAL;
2578
2579	memmove(orig_hdr + IEEE80211_TKIP_IV_LEN, orig_hdr, head_len + hdr_len);
2580	skb_pull(skb, IEEE80211_TKIP_IV_LEN);
2581	skb_trim(skb, skb->len - ATH10K_IEEE80211_TKIP_MICLEN);
2582	return 0;
2583}
2584
2585static int ath10k_htt_rx_frag_tkip_decap_withmic(struct sk_buff *skb,
2586						 u16 head_len,
2587						 u16 hdr_len)
2588{
2589	u8 *ivp, *orig_hdr;
2590
2591	orig_hdr = skb->data;
2592	ivp = orig_hdr + hdr_len + head_len;
2593
2594	/* the ExtIV bit is always set to 1 for TKIP */
2595	if (!(ivp[IEEE80211_WEP_IV_LEN - 1] & ATH10K_IEEE80211_EXTIV))
2596		return -EINVAL;
2597
2598	memmove(orig_hdr + IEEE80211_TKIP_IV_LEN, orig_hdr, head_len + hdr_len);
2599	skb_pull(skb, IEEE80211_TKIP_IV_LEN);
2600	skb_trim(skb, skb->len - IEEE80211_TKIP_ICV_LEN);
2601	return 0;
2602}
2603
2604static int ath10k_htt_rx_frag_ccmp_decap(struct sk_buff *skb,
2605					 u16 head_len,
2606					 u16 hdr_len)
2607{
2608	u8 *ivp, *orig_hdr;
2609
2610	orig_hdr = skb->data;
2611	ivp = orig_hdr + hdr_len + head_len;
2612
2613	/* the ExtIV bit is always set to 1 for CCMP */
2614	if (!(ivp[IEEE80211_WEP_IV_LEN - 1] & ATH10K_IEEE80211_EXTIV))
2615		return -EINVAL;
2616
2617	skb_trim(skb, skb->len - IEEE80211_CCMP_MIC_LEN);
2618	memmove(orig_hdr + IEEE80211_CCMP_HDR_LEN, orig_hdr, head_len + hdr_len);
2619	skb_pull(skb, IEEE80211_CCMP_HDR_LEN);
2620	return 0;
2621}
2622
2623static int ath10k_htt_rx_frag_wep_decap(struct sk_buff *skb,
2624					u16 head_len,
2625					u16 hdr_len)
2626{
2627	u8 *orig_hdr;
2628
2629	orig_hdr = skb->data;
2630
2631	memmove(orig_hdr + IEEE80211_WEP_IV_LEN,
2632		orig_hdr, head_len + hdr_len);
2633	skb_pull(skb, IEEE80211_WEP_IV_LEN);
2634	skb_trim(skb, skb->len - IEEE80211_WEP_ICV_LEN);
2635	return 0;
2636}
2637
2638static bool ath10k_htt_rx_proc_rx_frag_ind_hl(struct ath10k_htt *htt,
2639					      struct htt_rx_fragment_indication *rx,
2640					      struct sk_buff *skb)
2641{
2642	struct ath10k *ar = htt->ar;
2643	enum htt_rx_tkip_demic_type tkip_mic = HTT_RX_NON_TKIP_MIC;
2644	enum htt_txrx_sec_cast_type sec_index;
2645	struct htt_rx_indication_hl *rx_hl;
2646	enum htt_security_types sec_type;
2647	u32 tid, frag, seq, rx_desc_info;
2648	union htt_rx_pn_t new_pn = {0};
2649	struct htt_hl_rx_desc *rx_desc;
2650	u16 peer_id, sc, hdr_space;
2651	union htt_rx_pn_t *last_pn;
2652	struct ieee80211_hdr *hdr;
2653	int ret, num_mpdu_ranges;
2654	struct ath10k_peer *peer;
2655	struct htt_resp *resp;
2656	size_t tot_hdr_len;
2657
2658	resp = (struct htt_resp *)(skb->data + HTT_RX_FRAG_IND_INFO0_HEADER_LEN);
2659	skb_pull(skb, HTT_RX_FRAG_IND_INFO0_HEADER_LEN);
2660	skb_trim(skb, skb->len - FCS_LEN);
2661
2662	peer_id = __le16_to_cpu(rx->peer_id);
2663	rx_hl = (struct htt_rx_indication_hl *)(&resp->rx_ind_hl);
2664
2665	spin_lock_bh(&ar->data_lock);
2666	peer = ath10k_peer_find_by_id(ar, peer_id);
2667	if (!peer) {
2668		ath10k_dbg(ar, ATH10K_DBG_HTT, "invalid peer: %u\n", peer_id);
2669		goto err;
2670	}
2671
2672	num_mpdu_ranges = MS(__le32_to_cpu(rx_hl->hdr.info1),
2673			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
2674
2675	tot_hdr_len = sizeof(struct htt_resp_hdr) +
2676		      sizeof(rx_hl->hdr) +
2677		      sizeof(rx_hl->ppdu) +
2678		      sizeof(rx_hl->prefix) +
2679		      sizeof(rx_hl->fw_desc) +
2680		      sizeof(struct htt_rx_indication_mpdu_range) * num_mpdu_ranges;
2681
2682	tid =  MS(rx_hl->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);
2683	rx_desc = (struct htt_hl_rx_desc *)(skb->data + tot_hdr_len);
2684	rx_desc_info = __le32_to_cpu(rx_desc->info);
2685
2686	hdr = (struct ieee80211_hdr *)((u8 *)rx_desc + rx_hl->fw_desc.len);
2687
2688	if (is_multicast_ether_addr(hdr->addr1)) {
2689		/* Discard the fragment with multicast DA */
2690		goto err;
2691	}
2692
2693	if (!MS(rx_desc_info, HTT_RX_DESC_HL_INFO_ENCRYPTED)) {
2694		spin_unlock_bh(&ar->data_lock);
2695		return ath10k_htt_rx_proc_rx_ind_hl(htt, &resp->rx_ind_hl, skb,
2696						    HTT_RX_NON_PN_CHECK,
2697						    HTT_RX_NON_TKIP_MIC);
2698	}
2699
2700	if (ieee80211_has_retry(hdr->frame_control))
2701		goto err;
2702
2703	hdr_space = ieee80211_hdrlen(hdr->frame_control);
2704	sc = __le16_to_cpu(hdr->seq_ctrl);
2705	seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2706	frag = sc & IEEE80211_SCTL_FRAG;
2707
2708	sec_index = MS(rx_desc_info, HTT_RX_DESC_HL_INFO_MCAST_BCAST) ?
2709		    HTT_TXRX_SEC_MCAST : HTT_TXRX_SEC_UCAST;
2710	sec_type = peer->rx_pn[sec_index].sec_type;
2711	ath10k_htt_rx_mpdu_desc_pn_hl(rx_desc, &new_pn, peer->rx_pn[sec_index].pn_len);
2712
2713	switch (sec_type) {
2714	case HTT_SECURITY_TKIP:
2715		tkip_mic = HTT_RX_TKIP_MIC;
2716		ret = ath10k_htt_rx_frag_tkip_decap_withmic(skb,
2717							    tot_hdr_len +
2718							    rx_hl->fw_desc.len,
2719							    hdr_space);
2720		if (ret)
2721			goto err;
2722		break;
2723	case HTT_SECURITY_TKIP_NOMIC:
2724		ret = ath10k_htt_rx_frag_tkip_decap_nomic(skb,
2725							  tot_hdr_len +
2726							  rx_hl->fw_desc.len,
2727							  hdr_space);
2728		if (ret)
2729			goto err;
2730		break;
2731	case HTT_SECURITY_AES_CCMP:
2732		ret = ath10k_htt_rx_frag_ccmp_decap(skb,
2733						    tot_hdr_len + rx_hl->fw_desc.len,
2734						    hdr_space);
2735		if (ret)
2736			goto err;
2737		break;
2738	case HTT_SECURITY_WEP128:
2739	case HTT_SECURITY_WEP104:
2740	case HTT_SECURITY_WEP40:
2741		ret = ath10k_htt_rx_frag_wep_decap(skb,
2742						   tot_hdr_len + rx_hl->fw_desc.len,
2743						   hdr_space);
2744		if (ret)
2745			goto err;
2746		break;
2747	default:
2748		break;
2749	}
2750
2751	resp = (struct htt_resp *)(skb->data);
2752
2753	if (sec_type != HTT_SECURITY_AES_CCMP &&
2754	    sec_type != HTT_SECURITY_TKIP &&
2755	    sec_type != HTT_SECURITY_TKIP_NOMIC) {
2756		spin_unlock_bh(&ar->data_lock);
2757		return ath10k_htt_rx_proc_rx_ind_hl(htt, &resp->rx_ind_hl, skb,
2758						    HTT_RX_NON_PN_CHECK,
2759						    HTT_RX_NON_TKIP_MIC);
2760	}
2761
2762	last_pn = &peer->frag_tids_last_pn[tid];
2763
2764	if (frag == 0) {
2765		if (ath10k_htt_rx_pn_check_replay_hl(ar, peer, &resp->rx_ind_hl))
2766			goto err;
2767
2768		last_pn->pn48 = new_pn.pn48;
2769		peer->frag_tids_seq[tid] = seq;
2770	} else if (sec_type == HTT_SECURITY_AES_CCMP) {
2771		if (seq != peer->frag_tids_seq[tid])
2772			goto err;
2773
2774		if (new_pn.pn48 != last_pn->pn48 + 1)
2775			goto err;
2776
2777		last_pn->pn48 = new_pn.pn48;
2778		last_pn = &peer->tids_last_pn[tid];
2779		last_pn->pn48 = new_pn.pn48;
2780	}
2781
2782	spin_unlock_bh(&ar->data_lock);
2783
2784	return ath10k_htt_rx_proc_rx_ind_hl(htt, &resp->rx_ind_hl, skb,
2785					    HTT_RX_NON_PN_CHECK, tkip_mic);
2786
2787err:
2788	spin_unlock_bh(&ar->data_lock);
2789
2790	/* Tell the caller that it must free the skb since we have not
2791	 * consumed it
2792	 */
2793	return true;
2794}
2795
2796static void ath10k_htt_rx_proc_rx_ind_ll(struct ath10k_htt *htt,
2797					 struct htt_rx_indication *rx)
2798{
2799	struct ath10k *ar = htt->ar;
2800	struct htt_rx_indication_mpdu_range *mpdu_ranges;
2801	int num_mpdu_ranges;
2802	int i, mpdu_count = 0;
2803	u16 peer_id;
2804	u8 tid;
2805
2806	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
2807			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
2808	peer_id = __le16_to_cpu(rx->hdr.peer_id);
2809	tid =  MS(rx->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);
2810
2811	mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
2812
2813	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
2814			rx, struct_size(rx, mpdu_ranges, num_mpdu_ranges));
2815
2816	for (i = 0; i < num_mpdu_ranges; i++)
2817		mpdu_count += mpdu_ranges[i].mpdu_count;
2818
2819	atomic_add(mpdu_count, &htt->num_mpdus_ready);
2820
2821	ath10k_sta_update_rx_tid_stats_ampdu(ar, peer_id, tid, mpdu_ranges,
2822					     num_mpdu_ranges);
2823}
2824
2825static void ath10k_htt_rx_tx_compl_ind(struct ath10k *ar,
2826				       struct sk_buff *skb)
2827{
2828	struct ath10k_htt *htt = &ar->htt;
2829	struct htt_resp *resp = (struct htt_resp *)skb->data;
2830	struct htt_tx_done tx_done = {};
2831	int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
2832	__le16 msdu_id, *msdus;
2833	bool rssi_enabled = false;
2834	u8 msdu_count = 0, num_airtime_records, tid;
2835	int i, htt_pad = 0;
2836	struct htt_data_tx_compl_ppdu_dur *ppdu_info;
2837	struct ath10k_peer *peer;
2838	u16 ppdu_info_offset = 0, peer_id;
2839	u32 tx_duration;
2840
2841	switch (status) {
2842	case HTT_DATA_TX_STATUS_NO_ACK:
2843		tx_done.status = HTT_TX_COMPL_STATE_NOACK;
2844		break;
2845	case HTT_DATA_TX_STATUS_OK:
2846		tx_done.status = HTT_TX_COMPL_STATE_ACK;
2847		break;
2848	case HTT_DATA_TX_STATUS_DISCARD:
2849	case HTT_DATA_TX_STATUS_POSTPONE:
2850	case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
2851		tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
2852		break;
2853	default:
2854		ath10k_warn(ar, "unhandled tx completion status %d\n", status);
2855		tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
2856		break;
2857	}
2858
2859	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
2860		   resp->data_tx_completion.num_msdus);
2861
2862	msdu_count = resp->data_tx_completion.num_msdus;
2863	msdus = resp->data_tx_completion.msdus;
2864	rssi_enabled = ath10k_is_rssi_enable(&ar->hw_params, resp);
2865
2866	if (rssi_enabled)
2867		htt_pad = ath10k_tx_data_rssi_get_pad_bytes(&ar->hw_params,
2868							    resp);
2869
2870	for (i = 0; i < msdu_count; i++) {
2871		msdu_id = msdus[i];
2872		tx_done.msdu_id = __le16_to_cpu(msdu_id);
2873
2874		if (rssi_enabled) {
2875			/* Total no of MSDUs should be even,
2876			 * if odd MSDUs are sent firmware fills
2877			 * last msdu id with 0xffff
2878			 */
2879			if (msdu_count & 0x01) {
2880				msdu_id = msdus[msdu_count +  i + 1 + htt_pad];
2881				tx_done.ack_rssi = __le16_to_cpu(msdu_id);
2882			} else {
2883				msdu_id = msdus[msdu_count +  i + htt_pad];
2884				tx_done.ack_rssi = __le16_to_cpu(msdu_id);
2885			}
2886		}
2887
2888		/* kfifo_put: In practice firmware shouldn't fire off per-CE
2889		 * interrupt and main interrupt (MSI/-X range case) for the same
2890		 * HTC service so it should be safe to use kfifo_put w/o lock.
2891		 *
2892		 * From kfifo_put() documentation:
2893		 *  Note that with only one concurrent reader and one concurrent
2894		 *  writer, you don't need extra locking to use these macro.
2895		 */
2896		if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL) {
2897			ath10k_txrx_tx_unref(htt, &tx_done);
2898		} else if (!kfifo_put(&htt->txdone_fifo, tx_done)) {
2899			ath10k_warn(ar, "txdone fifo overrun, msdu_id %d status %d\n",
2900				    tx_done.msdu_id, tx_done.status);
2901			ath10k_txrx_tx_unref(htt, &tx_done);
2902		}
2903	}
2904
2905	if (!(resp->data_tx_completion.flags2 & HTT_TX_CMPL_FLAG_PPDU_DURATION_PRESENT))
2906		return;
2907
2908	ppdu_info_offset = (msdu_count & 0x01) ? msdu_count + 1 : msdu_count;
2909
2910	if (rssi_enabled)
2911		ppdu_info_offset += ppdu_info_offset;
2912
2913	if (resp->data_tx_completion.flags2 &
2914	    (HTT_TX_CMPL_FLAG_PPID_PRESENT | HTT_TX_CMPL_FLAG_PA_PRESENT))
2915		ppdu_info_offset += 2;
2916
2917	ppdu_info = (struct htt_data_tx_compl_ppdu_dur *)&msdus[ppdu_info_offset];
2918	num_airtime_records = FIELD_GET(HTT_TX_COMPL_PPDU_DUR_INFO0_NUM_ENTRIES_MASK,
2919					__le32_to_cpu(ppdu_info->info0));
2920
2921	for (i = 0; i < num_airtime_records; i++) {
2922		struct htt_data_tx_ppdu_dur *ppdu_dur;
2923		u32 info0;
2924
2925		ppdu_dur = &ppdu_info->ppdu_dur[i];
2926		info0 = __le32_to_cpu(ppdu_dur->info0);
2927
2928		peer_id = FIELD_GET(HTT_TX_PPDU_DUR_INFO0_PEER_ID_MASK,
2929				    info0);
2930		rcu_read_lock();
2931		spin_lock_bh(&ar->data_lock);
2932
2933		peer = ath10k_peer_find_by_id(ar, peer_id);
2934		if (!peer || !peer->sta) {
2935			spin_unlock_bh(&ar->data_lock);
2936			rcu_read_unlock();
2937			continue;
2938		}
2939
2940		tid = FIELD_GET(HTT_TX_PPDU_DUR_INFO0_TID_MASK, info0) &
2941						IEEE80211_QOS_CTL_TID_MASK;
2942		tx_duration = __le32_to_cpu(ppdu_dur->tx_duration);
2943
2944		ieee80211_sta_register_airtime(peer->sta, tid, tx_duration, 0);
2945
2946		spin_unlock_bh(&ar->data_lock);
2947		rcu_read_unlock();
2948	}
2949}
2950
2951static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
2952{
2953	struct htt_rx_addba *ev = &resp->rx_addba;
2954	struct ath10k_peer *peer;
2955	struct ath10k_vif *arvif;
2956	u16 info0, tid, peer_id;
2957
2958	info0 = __le16_to_cpu(ev->info0);
2959	tid = MS(info0, HTT_RX_BA_INFO0_TID);
2960	peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
2961
2962	ath10k_dbg(ar, ATH10K_DBG_HTT,
2963		   "htt rx addba tid %u peer_id %u size %u\n",
2964		   tid, peer_id, ev->window_size);
2965
2966	spin_lock_bh(&ar->data_lock);
2967	peer = ath10k_peer_find_by_id(ar, peer_id);
2968	if (!peer) {
2969		ath10k_warn(ar, "received addba event for invalid peer_id: %u\n",
2970			    peer_id);
2971		spin_unlock_bh(&ar->data_lock);
2972		return;
2973	}
2974
2975	arvif = ath10k_get_arvif(ar, peer->vdev_id);
2976	if (!arvif) {
2977		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
2978			    peer->vdev_id);
2979		spin_unlock_bh(&ar->data_lock);
2980		return;
2981	}
2982
2983	ath10k_dbg(ar, ATH10K_DBG_HTT,
2984		   "htt rx start rx ba session sta %pM tid %u size %u\n",
2985		   peer->addr, tid, ev->window_size);
2986
2987	ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
2988	spin_unlock_bh(&ar->data_lock);
2989}
2990
2991static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
2992{
2993	struct htt_rx_delba *ev = &resp->rx_delba;
2994	struct ath10k_peer *peer;
2995	struct ath10k_vif *arvif;
2996	u16 info0, tid, peer_id;
2997
2998	info0 = __le16_to_cpu(ev->info0);
2999	tid = MS(info0, HTT_RX_BA_INFO0_TID);
3000	peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
3001
3002	ath10k_dbg(ar, ATH10K_DBG_HTT,
3003		   "htt rx delba tid %u peer_id %u\n",
3004		   tid, peer_id);
3005
3006	spin_lock_bh(&ar->data_lock);
3007	peer = ath10k_peer_find_by_id(ar, peer_id);
3008	if (!peer) {
3009		ath10k_warn(ar, "received addba event for invalid peer_id: %u\n",
3010			    peer_id);
3011		spin_unlock_bh(&ar->data_lock);
3012		return;
3013	}
3014
3015	arvif = ath10k_get_arvif(ar, peer->vdev_id);
3016	if (!arvif) {
3017		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
3018			    peer->vdev_id);
3019		spin_unlock_bh(&ar->data_lock);
3020		return;
3021	}
3022
3023	ath10k_dbg(ar, ATH10K_DBG_HTT,
3024		   "htt rx stop rx ba session sta %pM tid %u\n",
3025		   peer->addr, tid);
3026
3027	ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
3028	spin_unlock_bh(&ar->data_lock);
3029}
3030
3031static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
3032				       struct sk_buff_head *amsdu)
3033{
3034	struct sk_buff *msdu;
3035	struct htt_rx_desc *rxd;
3036
3037	if (skb_queue_empty(list))
3038		return -ENOBUFS;
3039
3040	if (WARN_ON(!skb_queue_empty(amsdu)))
3041		return -EINVAL;
3042
3043	while ((msdu = __skb_dequeue(list))) {
3044		__skb_queue_tail(amsdu, msdu);
3045
3046		rxd = (void *)msdu->data - sizeof(*rxd);
3047		if (rxd->msdu_end.common.info0 &
3048		    __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
3049			break;
3050	}
3051
3052	msdu = skb_peek_tail(amsdu);
3053	rxd = (void *)msdu->data - sizeof(*rxd);
3054	if (!(rxd->msdu_end.common.info0 &
3055	      __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
3056		skb_queue_splice_init(amsdu, list);
3057		return -EAGAIN;
3058	}
3059
3060	return 0;
3061}
3062
3063static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
3064					    struct sk_buff *skb)
3065{
3066	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3067
3068	if (!ieee80211_has_protected(hdr->frame_control))
3069		return;
3070
3071	/* Offloaded frames are already decrypted but firmware insists they are
3072	 * protected in the 802.11 header. Strip the flag.  Otherwise mac80211
3073	 * will drop the frame.
3074	 */
3075
3076	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3077	status->flag |= RX_FLAG_DECRYPTED |
3078			RX_FLAG_IV_STRIPPED |
3079			RX_FLAG_MMIC_STRIPPED;
3080}
3081
3082static void ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
3083				       struct sk_buff_head *list)
3084{
3085	struct ath10k_htt *htt = &ar->htt;
3086	struct ieee80211_rx_status *status = &htt->rx_status;
3087	struct htt_rx_offload_msdu *rx;
3088	struct sk_buff *msdu;
3089	size_t offset;
3090
3091	while ((msdu = __skb_dequeue(list))) {
3092		/* Offloaded frames don't have Rx descriptor. Instead they have
3093		 * a short meta information header.
3094		 */
3095
3096		rx = (void *)msdu->data;
3097
3098		skb_put(msdu, sizeof(*rx));
3099		skb_pull(msdu, sizeof(*rx));
3100
3101		if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
3102			ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
3103			dev_kfree_skb_any(msdu);
3104			continue;
3105		}
3106
3107		skb_put(msdu, __le16_to_cpu(rx->msdu_len));
3108
3109		/* Offloaded rx header length isn't multiple of 2 nor 4 so the
3110		 * actual payload is unaligned. Align the frame.  Otherwise
3111		 * mac80211 complains.  This shouldn't reduce performance much
3112		 * because these offloaded frames are rare.
3113		 */
3114		offset = 4 - ((unsigned long)msdu->data & 3);
3115		skb_put(msdu, offset);
3116		memmove(msdu->data + offset, msdu->data, msdu->len);
3117		skb_pull(msdu, offset);
3118
3119		/* FIXME: The frame is NWifi. Re-construct QoS Control
3120		 * if possible later.
3121		 */
3122
3123		memset(status, 0, sizeof(*status));
3124		status->flag |= RX_FLAG_NO_SIGNAL_VAL;
3125
3126		ath10k_htt_rx_h_rx_offload_prot(status, msdu);
3127		ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
3128		ath10k_htt_rx_h_queue_msdu(ar, status, msdu);
3129	}
3130}
3131
3132static int ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
3133{
3134	struct ath10k_htt *htt = &ar->htt;
3135	struct htt_resp *resp = (void *)skb->data;
3136	struct ieee80211_rx_status *status = &htt->rx_status;
3137	struct sk_buff_head list;
3138	struct sk_buff_head amsdu;
3139	u16 peer_id;
3140	u16 msdu_count;
3141	u8 vdev_id;
3142	u8 tid;
3143	bool offload;
3144	bool frag;
3145	int ret;
3146
3147	lockdep_assert_held(&htt->rx_ring.lock);
3148
3149	if (htt->rx_confused)
3150		return -EIO;
3151
3152	skb_pull(skb, sizeof(resp->hdr));
3153	skb_pull(skb, sizeof(resp->rx_in_ord_ind));
3154
3155	peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
3156	msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
3157	vdev_id = resp->rx_in_ord_ind.vdev_id;
3158	tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
3159	offload = !!(resp->rx_in_ord_ind.info &
3160			HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
3161	frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);
3162
3163	ath10k_dbg(ar, ATH10K_DBG_HTT,
3164		   "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
3165		   vdev_id, peer_id, tid, offload, frag, msdu_count);
3166
3167	if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs32)) {
3168		ath10k_warn(ar, "dropping invalid in order rx indication\n");
3169		return -EINVAL;
3170	}
3171
3172	/* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
3173	 * extracted and processed.
3174	 */
3175	__skb_queue_head_init(&list);
3176	if (ar->hw_params.target_64bit)
3177		ret = ath10k_htt_rx_pop_paddr64_list(htt, &resp->rx_in_ord_ind,
3178						     &list);
3179	else
3180		ret = ath10k_htt_rx_pop_paddr32_list(htt, &resp->rx_in_ord_ind,
3181						     &list);
3182
3183	if (ret < 0) {
3184		ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
3185		htt->rx_confused = true;
3186		return -EIO;
3187	}
3188
3189	/* Offloaded frames are very different and need to be handled
3190	 * separately.
3191	 */
3192	if (offload)
3193		ath10k_htt_rx_h_rx_offload(ar, &list);
3194
3195	while (!skb_queue_empty(&list)) {
3196		__skb_queue_head_init(&amsdu);
3197		ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
3198		switch (ret) {
3199		case 0:
3200			/* Note: The in-order indication may report interleaved
3201			 * frames from different PPDUs meaning reported rx rate
3202			 * to mac80211 isn't accurate/reliable. It's still
3203			 * better to report something than nothing though. This
3204			 * should still give an idea about rx rate to the user.
3205			 */
3206			ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
3207			ath10k_htt_rx_h_filter(ar, &amsdu, status, NULL);
3208			ath10k_htt_rx_h_mpdu(ar, &amsdu, status, false, NULL,
3209					     NULL, peer_id, frag);
3210			ath10k_htt_rx_h_enqueue(ar, &amsdu, status);
3211			break;
3212		case -EAGAIN:
3213			fallthrough;
3214		default:
3215			/* Should not happen. */
3216			ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
3217			htt->rx_confused = true;
3218			__skb_queue_purge(&list);
3219			return -EIO;
3220		}
3221	}
3222	return ret;
3223}
3224
3225static void ath10k_htt_rx_tx_fetch_resp_id_confirm(struct ath10k *ar,
3226						   const __le32 *resp_ids,
3227						   int num_resp_ids)
3228{
3229	int i;
3230	u32 resp_id;
3231
3232	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm num_resp_ids %d\n",
3233		   num_resp_ids);
3234
3235	for (i = 0; i < num_resp_ids; i++) {
3236		resp_id = le32_to_cpu(resp_ids[i]);
3237
3238		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm resp_id %u\n",
3239			   resp_id);
3240
3241		/* TODO: free resp_id */
3242	}
3243}
3244
3245static void ath10k_htt_rx_tx_fetch_ind(struct ath10k *ar, struct sk_buff *skb)
3246{
3247	struct ieee80211_hw *hw = ar->hw;
3248	struct ieee80211_txq *txq;
3249	struct htt_resp *resp = (struct htt_resp *)skb->data;
3250	struct htt_tx_fetch_record *record;
3251	size_t len;
3252	size_t max_num_bytes;
3253	size_t max_num_msdus;
3254	size_t num_bytes;
3255	size_t num_msdus;
3256	const __le32 *resp_ids;
3257	u16 num_records;
3258	u16 num_resp_ids;
3259	u16 peer_id;
3260	u8 tid;
3261	int ret;
3262	int i;
3263	bool may_tx;
3264
3265	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind\n");
3266
3267	len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_ind);
3268	if (unlikely(skb->len < len)) {
3269		ath10k_warn(ar, "received corrupted tx_fetch_ind event: buffer too short\n");
3270		return;
3271	}
3272
3273	num_records = le16_to_cpu(resp->tx_fetch_ind.num_records);
3274	num_resp_ids = le16_to_cpu(resp->tx_fetch_ind.num_resp_ids);
3275
3276	len += sizeof(resp->tx_fetch_ind.records[0]) * num_records;
3277	len += sizeof(resp->tx_fetch_ind.resp_ids[0]) * num_resp_ids;
3278
3279	if (unlikely(skb->len < len)) {
3280		ath10k_warn(ar, "received corrupted tx_fetch_ind event: too many records/resp_ids\n");
3281		return;
3282	}
3283
3284	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind num records %u num resps %u seq %u\n",
3285		   num_records, num_resp_ids,
3286		   le16_to_cpu(resp->tx_fetch_ind.fetch_seq_num));
3287
3288	if (!ar->htt.tx_q_state.enabled) {
3289		ath10k_warn(ar, "received unexpected tx_fetch_ind event: not enabled\n");
3290		return;
3291	}
3292
3293	if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH) {
3294		ath10k_warn(ar, "received unexpected tx_fetch_ind event: in push mode\n");
3295		return;
3296	}
3297
3298	rcu_read_lock();
3299
3300	for (i = 0; i < num_records; i++) {
3301		record = &resp->tx_fetch_ind.records[i];
3302		peer_id = MS(le16_to_cpu(record->info),
3303			     HTT_TX_FETCH_RECORD_INFO_PEER_ID);
3304		tid = MS(le16_to_cpu(record->info),
3305			 HTT_TX_FETCH_RECORD_INFO_TID);
3306		max_num_msdus = le16_to_cpu(record->num_msdus);
3307		max_num_bytes = le32_to_cpu(record->num_bytes);
3308
3309		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch record %i peer_id %u tid %u msdus %zu bytes %zu\n",
3310			   i, peer_id, tid, max_num_msdus, max_num_bytes);
3311
3312		if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
3313		    unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
3314			ath10k_warn(ar, "received out of range peer_id %u tid %u\n",
3315				    peer_id, tid);
3316			continue;
3317		}
3318
3319		spin_lock_bh(&ar->data_lock);
3320		txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
3321		spin_unlock_bh(&ar->data_lock);
3322
3323		/* It is okay to release the lock and use txq because RCU read
3324		 * lock is held.
3325		 */
3326
3327		if (unlikely(!txq)) {
3328			ath10k_warn(ar, "failed to lookup txq for peer_id %u tid %u\n",
3329				    peer_id, tid);
3330			continue;
3331		}
3332
3333		num_msdus = 0;
3334		num_bytes = 0;
3335
3336		ieee80211_txq_schedule_start(hw, txq->ac);
3337		may_tx = ieee80211_txq_may_transmit(hw, txq);
3338		while (num_msdus < max_num_msdus &&
3339		       num_bytes < max_num_bytes) {
3340			if (!may_tx)
3341				break;
3342
3343			ret = ath10k_mac_tx_push_txq(hw, txq);
3344			if (ret < 0)
3345				break;
3346
3347			num_msdus++;
3348			num_bytes += ret;
3349		}
3350		ieee80211_return_txq(hw, txq, false);
3351		ieee80211_txq_schedule_end(hw, txq->ac);
3352
3353		record->num_msdus = cpu_to_le16(num_msdus);
3354		record->num_bytes = cpu_to_le32(num_bytes);
3355
3356		ath10k_htt_tx_txq_recalc(hw, txq);
3357	}
3358
3359	rcu_read_unlock();
3360
3361	resp_ids = ath10k_htt_get_tx_fetch_ind_resp_ids(&resp->tx_fetch_ind);
3362	ath10k_htt_rx_tx_fetch_resp_id_confirm(ar, resp_ids, num_resp_ids);
3363
3364	ret = ath10k_htt_tx_fetch_resp(ar,
3365				       resp->tx_fetch_ind.token,
3366				       resp->tx_fetch_ind.fetch_seq_num,
3367				       resp->tx_fetch_ind.records,
3368				       num_records);
3369	if (unlikely(ret)) {
3370		ath10k_warn(ar, "failed to submit tx fetch resp for token 0x%08x: %d\n",
3371			    le32_to_cpu(resp->tx_fetch_ind.token), ret);
3372		/* FIXME: request fw restart */
3373	}
3374
3375	ath10k_htt_tx_txq_sync(ar);
3376}
3377
3378static void ath10k_htt_rx_tx_fetch_confirm(struct ath10k *ar,
3379					   struct sk_buff *skb)
3380{
3381	const struct htt_resp *resp = (void *)skb->data;
3382	size_t len;
3383	int num_resp_ids;
3384
3385	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm\n");
3386
3387	len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_confirm);
3388	if (unlikely(skb->len < len)) {
3389		ath10k_warn(ar, "received corrupted tx_fetch_confirm event: buffer too short\n");
3390		return;
3391	}
3392
3393	num_resp_ids = le16_to_cpu(resp->tx_fetch_confirm.num_resp_ids);
3394	len += sizeof(resp->tx_fetch_confirm.resp_ids[0]) * num_resp_ids;
3395
3396	if (unlikely(skb->len < len)) {
3397		ath10k_warn(ar, "received corrupted tx_fetch_confirm event: resp_ids buffer overflow\n");
3398		return;
3399	}
3400
3401	ath10k_htt_rx_tx_fetch_resp_id_confirm(ar,
3402					       resp->tx_fetch_confirm.resp_ids,
3403					       num_resp_ids);
3404}
3405
3406static void ath10k_htt_rx_tx_mode_switch_ind(struct ath10k *ar,
3407					     struct sk_buff *skb)
3408{
3409	const struct htt_resp *resp = (void *)skb->data;
3410	const struct htt_tx_mode_switch_record *record;
3411	struct ieee80211_txq *txq;
3412	struct ath10k_txq *artxq;
3413	size_t len;
3414	size_t num_records;
3415	enum htt_tx_mode_switch_mode mode;
3416	bool enable;
3417	u16 info0;
3418	u16 info1;
3419	u16 threshold;
3420	u16 peer_id;
3421	u8 tid;
3422	int i;
3423
3424	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx mode switch ind\n");
3425
3426	len = sizeof(resp->hdr) + sizeof(resp->tx_mode_switch_ind);
3427	if (unlikely(skb->len < len)) {
3428		ath10k_warn(ar, "received corrupted tx_mode_switch_ind event: buffer too short\n");
3429		return;
3430	}
3431
3432	info0 = le16_to_cpu(resp->tx_mode_switch_ind.info0);
3433	info1 = le16_to_cpu(resp->tx_mode_switch_ind.info1);
3434
3435	enable = !!(info0 & HTT_TX_MODE_SWITCH_IND_INFO0_ENABLE);
3436	num_records = MS(info0, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
3437	mode = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_MODE);
3438	threshold = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
3439
3440	ath10k_dbg(ar, ATH10K_DBG_HTT,
3441		   "htt rx tx mode switch ind info0 0x%04hx info1 0x%04x enable %d num records %zd mode %d threshold %u\n",
3442		   info0, info1, enable, num_records, mode, threshold);
3443
3444	len += sizeof(resp->tx_mode_switch_ind.records[0]) * num_records;
3445
3446	if (unlikely(skb->len < len)) {
3447		ath10k_warn(ar, "received corrupted tx_mode_switch_mode_ind event: too many records\n");
3448		return;
3449	}
3450
3451	switch (mode) {
3452	case HTT_TX_MODE_SWITCH_PUSH:
3453	case HTT_TX_MODE_SWITCH_PUSH_PULL:
3454		break;
3455	default:
3456		ath10k_warn(ar, "received invalid tx_mode_switch_mode_ind mode %d, ignoring\n",
3457			    mode);
3458		return;
3459	}
3460
3461	if (!enable)
3462		return;
3463
3464	ar->htt.tx_q_state.enabled = enable;
3465	ar->htt.tx_q_state.mode = mode;
3466	ar->htt.tx_q_state.num_push_allowed = threshold;
3467
3468	rcu_read_lock();
3469
3470	for (i = 0; i < num_records; i++) {
3471		record = &resp->tx_mode_switch_ind.records[i];
3472		info0 = le16_to_cpu(record->info0);
3473		peer_id = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_PEER_ID);
3474		tid = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_TID);
3475
3476		if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
3477		    unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
3478			ath10k_warn(ar, "received out of range peer_id %u tid %u\n",
3479				    peer_id, tid);
3480			continue;
3481		}
3482
3483		spin_lock_bh(&ar->data_lock);
3484		txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
3485		spin_unlock_bh(&ar->data_lock);
3486
3487		/* It is okay to release the lock and use txq because RCU read
3488		 * lock is held.
3489		 */
3490
3491		if (unlikely(!txq)) {
3492			ath10k_warn(ar, "failed to lookup txq for peer_id %u tid %u\n",
3493				    peer_id, tid);
3494			continue;
3495		}
3496
3497		spin_lock_bh(&ar->htt.tx_lock);
3498		artxq = (void *)txq->drv_priv;
3499		artxq->num_push_allowed = le16_to_cpu(record->num_max_msdus);
3500		spin_unlock_bh(&ar->htt.tx_lock);
3501	}
3502
3503	rcu_read_unlock();
3504
3505	ath10k_mac_tx_push_pending(ar);
3506}
3507
3508void ath10k_htt_htc_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
3509{
3510	bool release;
3511
3512	release = ath10k_htt_t2h_msg_handler(ar, skb);
3513
3514	/* Free the indication buffer */
3515	if (release)
3516		dev_kfree_skb_any(skb);
3517}
3518
3519static inline s8 ath10k_get_legacy_rate_idx(struct ath10k *ar, u8 rate)
3520{
3521	static const u8 legacy_rates[] = {1, 2, 5, 11, 6, 9, 12,
3522					  18, 24, 36, 48, 54};
3523	int i;
3524
3525	for (i = 0; i < ARRAY_SIZE(legacy_rates); i++) {
3526		if (rate == legacy_rates[i])
3527			return i;
3528	}
3529
3530	ath10k_warn(ar, "Invalid legacy rate %d peer stats", rate);
3531	return -EINVAL;
3532}
3533
3534static void
3535ath10k_accumulate_per_peer_tx_stats(struct ath10k *ar,
3536				    struct ath10k_sta *arsta,
3537				    struct ath10k_per_peer_tx_stats *pstats,
3538				    s8 legacy_rate_idx)
3539{
3540	struct rate_info *txrate = &arsta->txrate;
3541	struct ath10k_htt_tx_stats *tx_stats;
3542	int idx, ht_idx, gi, mcs, bw, nss;
3543	unsigned long flags;
3544
3545	if (!arsta->tx_stats)
3546		return;
3547
3548	tx_stats = arsta->tx_stats;
3549	flags = txrate->flags;
3550	gi = test_bit(ATH10K_RATE_INFO_FLAGS_SGI_BIT, &flags);
3551	mcs = ATH10K_HW_MCS_RATE(pstats->ratecode);
3552	bw = txrate->bw;
3553	nss = txrate->nss;
3554	ht_idx = mcs + (nss - 1) * 8;
3555	idx = mcs * 8 + 8 * 10 * (nss - 1);
3556	idx += bw * 2 + gi;
3557
3558#define STATS_OP_FMT(name) tx_stats->stats[ATH10K_STATS_TYPE_##name]
3559
3560	if (txrate->flags & RATE_INFO_FLAGS_VHT_MCS) {
3561		STATS_OP_FMT(SUCC).vht[0][mcs] += pstats->succ_bytes;
3562		STATS_OP_FMT(SUCC).vht[1][mcs] += pstats->succ_pkts;
3563		STATS_OP_FMT(FAIL).vht[0][mcs] += pstats->failed_bytes;
3564		STATS_OP_FMT(FAIL).vht[1][mcs] += pstats->failed_pkts;
3565		STATS_OP_FMT(RETRY).vht[0][mcs] += pstats->retry_bytes;
3566		STATS_OP_FMT(RETRY).vht[1][mcs] += pstats->retry_pkts;
3567	} else if (txrate->flags & RATE_INFO_FLAGS_MCS) {
3568		STATS_OP_FMT(SUCC).ht[0][ht_idx] += pstats->succ_bytes;
3569		STATS_OP_FMT(SUCC).ht[1][ht_idx] += pstats->succ_pkts;
3570		STATS_OP_FMT(FAIL).ht[0][ht_idx] += pstats->failed_bytes;
3571		STATS_OP_FMT(FAIL).ht[1][ht_idx] += pstats->failed_pkts;
3572		STATS_OP_FMT(RETRY).ht[0][ht_idx] += pstats->retry_bytes;
3573		STATS_OP_FMT(RETRY).ht[1][ht_idx] += pstats->retry_pkts;
3574	} else {
3575		mcs = legacy_rate_idx;
3576
3577		STATS_OP_FMT(SUCC).legacy[0][mcs] += pstats->succ_bytes;
3578		STATS_OP_FMT(SUCC).legacy[1][mcs] += pstats->succ_pkts;
3579		STATS_OP_FMT(FAIL).legacy[0][mcs] += pstats->failed_bytes;
3580		STATS_OP_FMT(FAIL).legacy[1][mcs] += pstats->failed_pkts;
3581		STATS_OP_FMT(RETRY).legacy[0][mcs] += pstats->retry_bytes;
3582		STATS_OP_FMT(RETRY).legacy[1][mcs] += pstats->retry_pkts;
3583	}
3584
3585	if (ATH10K_HW_AMPDU(pstats->flags)) {
3586		tx_stats->ba_fails += ATH10K_HW_BA_FAIL(pstats->flags);
3587
3588		if (txrate->flags & RATE_INFO_FLAGS_MCS) {
3589			STATS_OP_FMT(AMPDU).ht[0][ht_idx] +=
3590				pstats->succ_bytes + pstats->retry_bytes;
3591			STATS_OP_FMT(AMPDU).ht[1][ht_idx] +=
3592				pstats->succ_pkts + pstats->retry_pkts;
3593		} else {
3594			STATS_OP_FMT(AMPDU).vht[0][mcs] +=
3595				pstats->succ_bytes + pstats->retry_bytes;
3596			STATS_OP_FMT(AMPDU).vht[1][mcs] +=
3597				pstats->succ_pkts + pstats->retry_pkts;
3598		}
3599		STATS_OP_FMT(AMPDU).bw[0][bw] +=
3600			pstats->succ_bytes + pstats->retry_bytes;
3601		STATS_OP_FMT(AMPDU).nss[0][nss - 1] +=
3602			pstats->succ_bytes + pstats->retry_bytes;
3603		STATS_OP_FMT(AMPDU).gi[0][gi] +=
3604			pstats->succ_bytes + pstats->retry_bytes;
3605		STATS_OP_FMT(AMPDU).rate_table[0][idx] +=
3606			pstats->succ_bytes + pstats->retry_bytes;
3607		STATS_OP_FMT(AMPDU).bw[1][bw] +=
3608			pstats->succ_pkts + pstats->retry_pkts;
3609		STATS_OP_FMT(AMPDU).nss[1][nss - 1] +=
3610			pstats->succ_pkts + pstats->retry_pkts;
3611		STATS_OP_FMT(AMPDU).gi[1][gi] +=
3612			pstats->succ_pkts + pstats->retry_pkts;
3613		STATS_OP_FMT(AMPDU).rate_table[1][idx] +=
3614			pstats->succ_pkts + pstats->retry_pkts;
3615	} else {
3616		tx_stats->ack_fails +=
3617				ATH10K_HW_BA_FAIL(pstats->flags);
3618	}
3619
3620	STATS_OP_FMT(SUCC).bw[0][bw] += pstats->succ_bytes;
3621	STATS_OP_FMT(SUCC).nss[0][nss - 1] += pstats->succ_bytes;
3622	STATS_OP_FMT(SUCC).gi[0][gi] += pstats->succ_bytes;
3623
3624	STATS_OP_FMT(SUCC).bw[1][bw] += pstats->succ_pkts;
3625	STATS_OP_FMT(SUCC).nss[1][nss - 1] += pstats->succ_pkts;
3626	STATS_OP_FMT(SUCC).gi[1][gi] += pstats->succ_pkts;
3627
3628	STATS_OP_FMT(FAIL).bw[0][bw] += pstats->failed_bytes;
3629	STATS_OP_FMT(FAIL).nss[0][nss - 1] += pstats->failed_bytes;
3630	STATS_OP_FMT(FAIL).gi[0][gi] += pstats->failed_bytes;
3631
3632	STATS_OP_FMT(FAIL).bw[1][bw] += pstats->failed_pkts;
3633	STATS_OP_FMT(FAIL).nss[1][nss - 1] += pstats->failed_pkts;
3634	STATS_OP_FMT(FAIL).gi[1][gi] += pstats->failed_pkts;
3635
3636	STATS_OP_FMT(RETRY).bw[0][bw] += pstats->retry_bytes;
3637	STATS_OP_FMT(RETRY).nss[0][nss - 1] += pstats->retry_bytes;
3638	STATS_OP_FMT(RETRY).gi[0][gi] += pstats->retry_bytes;
3639
3640	STATS_OP_FMT(RETRY).bw[1][bw] += pstats->retry_pkts;
3641	STATS_OP_FMT(RETRY).nss[1][nss - 1] += pstats->retry_pkts;
3642	STATS_OP_FMT(RETRY).gi[1][gi] += pstats->retry_pkts;
3643
3644	if (txrate->flags >= RATE_INFO_FLAGS_MCS) {
3645		STATS_OP_FMT(SUCC).rate_table[0][idx] += pstats->succ_bytes;
3646		STATS_OP_FMT(SUCC).rate_table[1][idx] += pstats->succ_pkts;
3647		STATS_OP_FMT(FAIL).rate_table[0][idx] += pstats->failed_bytes;
3648		STATS_OP_FMT(FAIL).rate_table[1][idx] += pstats->failed_pkts;
3649		STATS_OP_FMT(RETRY).rate_table[0][idx] += pstats->retry_bytes;
3650		STATS_OP_FMT(RETRY).rate_table[1][idx] += pstats->retry_pkts;
3651	}
3652
3653	tx_stats->tx_duration += pstats->duration;
3654}
3655
3656static void
3657ath10k_update_per_peer_tx_stats(struct ath10k *ar,
3658				struct ieee80211_sta *sta,
3659				struct ath10k_per_peer_tx_stats *peer_stats)
3660{
3661	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
3662	struct ieee80211_chanctx_conf *conf = NULL;
3663	u8 rate = 0, sgi;
3664	s8 rate_idx = 0;
3665	bool skip_auto_rate;
3666	struct rate_info txrate;
3667
3668	lockdep_assert_held(&ar->data_lock);
3669
3670	txrate.flags = ATH10K_HW_PREAMBLE(peer_stats->ratecode);
3671	txrate.bw = ATH10K_HW_BW(peer_stats->flags);
3672	txrate.nss = ATH10K_HW_NSS(peer_stats->ratecode);
3673	txrate.mcs = ATH10K_HW_MCS_RATE(peer_stats->ratecode);
3674	sgi = ATH10K_HW_GI(peer_stats->flags);
3675	skip_auto_rate = ATH10K_FW_SKIPPED_RATE_CTRL(peer_stats->flags);
3676
3677	/* Firmware's rate control skips broadcast/management frames,
3678	 * if host has configure fixed rates and in some other special cases.
3679	 */
3680	if (skip_auto_rate)
3681		return;
3682
3683	if (txrate.flags == WMI_RATE_PREAMBLE_VHT && txrate.mcs > 9) {
3684		ath10k_warn(ar, "Invalid VHT mcs %d peer stats",  txrate.mcs);
3685		return;
3686	}
3687
3688	if (txrate.flags == WMI_RATE_PREAMBLE_HT &&
3689	    (txrate.mcs > 7 || txrate.nss < 1)) {
3690		ath10k_warn(ar, "Invalid HT mcs %d nss %d peer stats",
3691			    txrate.mcs, txrate.nss);
3692		return;
3693	}
3694
3695	memset(&arsta->txrate, 0, sizeof(arsta->txrate));
3696	memset(&arsta->tx_info.status, 0, sizeof(arsta->tx_info.status));
3697	if (txrate.flags == WMI_RATE_PREAMBLE_CCK ||
3698	    txrate.flags == WMI_RATE_PREAMBLE_OFDM) {
3699		rate = ATH10K_HW_LEGACY_RATE(peer_stats->ratecode);
3700		/* This is hacky, FW sends CCK rate 5.5Mbps as 6 */
3701		if (rate == 6 && txrate.flags == WMI_RATE_PREAMBLE_CCK)
3702			rate = 5;
3703		rate_idx = ath10k_get_legacy_rate_idx(ar, rate);
3704		if (rate_idx < 0)
3705			return;
3706		arsta->txrate.legacy = rate;
3707	} else if (txrate.flags == WMI_RATE_PREAMBLE_HT) {
3708		arsta->txrate.flags = RATE_INFO_FLAGS_MCS;
3709		arsta->txrate.mcs = txrate.mcs + 8 * (txrate.nss - 1);
3710	} else {
3711		arsta->txrate.flags = RATE_INFO_FLAGS_VHT_MCS;
3712		arsta->txrate.mcs = txrate.mcs;
3713	}
3714
3715	switch (txrate.flags) {
3716	case WMI_RATE_PREAMBLE_OFDM:
3717		if (arsta->arvif && arsta->arvif->vif)
3718			conf = rcu_dereference(arsta->arvif->vif->chanctx_conf);
3719		if (conf && conf->def.chan->band == NL80211_BAND_5GHZ)
3720			arsta->tx_info.status.rates[0].idx = rate_idx - 4;
3721		break;
3722	case WMI_RATE_PREAMBLE_CCK:
3723		arsta->tx_info.status.rates[0].idx = rate_idx;
3724		if (sgi)
3725			arsta->tx_info.status.rates[0].flags |=
3726				(IEEE80211_TX_RC_USE_SHORT_PREAMBLE |
3727				 IEEE80211_TX_RC_SHORT_GI);
3728		break;
3729	case WMI_RATE_PREAMBLE_HT:
3730		arsta->tx_info.status.rates[0].idx =
3731				txrate.mcs + ((txrate.nss - 1) * 8);
3732		if (sgi)
3733			arsta->tx_info.status.rates[0].flags |=
3734					IEEE80211_TX_RC_SHORT_GI;
3735		arsta->tx_info.status.rates[0].flags |= IEEE80211_TX_RC_MCS;
3736		break;
3737	case WMI_RATE_PREAMBLE_VHT:
3738		ieee80211_rate_set_vht(&arsta->tx_info.status.rates[0],
3739				       txrate.mcs, txrate.nss);
3740		if (sgi)
3741			arsta->tx_info.status.rates[0].flags |=
3742						IEEE80211_TX_RC_SHORT_GI;
3743		arsta->tx_info.status.rates[0].flags |= IEEE80211_TX_RC_VHT_MCS;
3744		break;
3745	}
3746
3747	arsta->txrate.nss = txrate.nss;
3748	arsta->txrate.bw = ath10k_bw_to_mac80211_bw(txrate.bw);
3749	arsta->last_tx_bitrate = cfg80211_calculate_bitrate(&arsta->txrate);
3750	if (sgi)
3751		arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
3752
3753	switch (arsta->txrate.bw) {
3754	case RATE_INFO_BW_40:
3755		arsta->tx_info.status.rates[0].flags |=
3756				IEEE80211_TX_RC_40_MHZ_WIDTH;
3757		break;
3758	case RATE_INFO_BW_80:
3759		arsta->tx_info.status.rates[0].flags |=
3760				IEEE80211_TX_RC_80_MHZ_WIDTH;
3761		break;
3762	}
3763
3764	if (peer_stats->succ_pkts) {
3765		arsta->tx_info.flags = IEEE80211_TX_STAT_ACK;
3766		arsta->tx_info.status.rates[0].count = 1;
3767		ieee80211_tx_rate_update(ar->hw, sta, &arsta->tx_info);
3768	}
3769
3770	if (ar->htt.disable_tx_comp) {
3771		arsta->tx_failed += peer_stats->failed_pkts;
3772		ath10k_dbg(ar, ATH10K_DBG_HTT, "tx failed %d\n",
3773			   arsta->tx_failed);
3774	}
3775
3776	arsta->tx_retries += peer_stats->retry_pkts;
3777	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx retries %d", arsta->tx_retries);
3778
3779	if (ath10k_debug_is_extd_tx_stats_enabled(ar))
3780		ath10k_accumulate_per_peer_tx_stats(ar, arsta, peer_stats,
3781						    rate_idx);
3782}
3783
3784static void ath10k_htt_fetch_peer_stats(struct ath10k *ar,
3785					struct sk_buff *skb)
3786{
3787	struct htt_resp *resp = (struct htt_resp *)skb->data;
3788	struct ath10k_per_peer_tx_stats *p_tx_stats = &ar->peer_tx_stats;
3789	struct htt_per_peer_tx_stats_ind *tx_stats;
3790	struct ieee80211_sta *sta;
3791	struct ath10k_peer *peer;
3792	int peer_id, i;
3793	u8 ppdu_len, num_ppdu;
3794
3795	num_ppdu = resp->peer_tx_stats.num_ppdu;
3796	ppdu_len = resp->peer_tx_stats.ppdu_len * sizeof(__le32);
3797
3798	if (skb->len < sizeof(struct htt_resp_hdr) + num_ppdu * ppdu_len) {
3799		ath10k_warn(ar, "Invalid peer stats buf length %d\n", skb->len);
3800		return;
3801	}
3802
3803	tx_stats = (struct htt_per_peer_tx_stats_ind *)
3804			(resp->peer_tx_stats.payload);
3805	peer_id = __le16_to_cpu(tx_stats->peer_id);
3806
3807	rcu_read_lock();
3808	spin_lock_bh(&ar->data_lock);
3809	peer = ath10k_peer_find_by_id(ar, peer_id);
3810	if (!peer || !peer->sta) {
3811		ath10k_warn(ar, "Invalid peer id %d peer stats buffer\n",
3812			    peer_id);
3813		goto out;
3814	}
3815
3816	sta = peer->sta;
3817	for (i = 0; i < num_ppdu; i++) {
3818		tx_stats = (struct htt_per_peer_tx_stats_ind *)
3819			   (resp->peer_tx_stats.payload + i * ppdu_len);
3820
3821		p_tx_stats->succ_bytes = __le32_to_cpu(tx_stats->succ_bytes);
3822		p_tx_stats->retry_bytes = __le32_to_cpu(tx_stats->retry_bytes);
3823		p_tx_stats->failed_bytes =
3824				__le32_to_cpu(tx_stats->failed_bytes);
3825		p_tx_stats->ratecode = tx_stats->ratecode;
3826		p_tx_stats->flags = tx_stats->flags;
3827		p_tx_stats->succ_pkts = __le16_to_cpu(tx_stats->succ_pkts);
3828		p_tx_stats->retry_pkts = __le16_to_cpu(tx_stats->retry_pkts);
3829		p_tx_stats->failed_pkts = __le16_to_cpu(tx_stats->failed_pkts);
3830		p_tx_stats->duration = __le16_to_cpu(tx_stats->tx_duration);
3831
3832		ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
3833	}
3834
3835out:
3836	spin_unlock_bh(&ar->data_lock);
3837	rcu_read_unlock();
3838}
3839
3840static void ath10k_fetch_10_2_tx_stats(struct ath10k *ar, u8 *data)
3841{
3842	struct ath10k_pktlog_hdr *hdr = (struct ath10k_pktlog_hdr *)data;
3843	struct ath10k_per_peer_tx_stats *p_tx_stats = &ar->peer_tx_stats;
3844	struct ath10k_10_2_peer_tx_stats *tx_stats;
3845	struct ieee80211_sta *sta;
3846	struct ath10k_peer *peer;
3847	u16 log_type = __le16_to_cpu(hdr->log_type);
3848	u32 peer_id = 0, i;
3849
3850	if (log_type != ATH_PKTLOG_TYPE_TX_STAT)
3851		return;
3852
3853	tx_stats = (struct ath10k_10_2_peer_tx_stats *)((hdr->payload) +
3854		    ATH10K_10_2_TX_STATS_OFFSET);
3855
3856	if (!tx_stats->tx_ppdu_cnt)
3857		return;
3858
3859	peer_id = tx_stats->peer_id;
3860
3861	rcu_read_lock();
3862	spin_lock_bh(&ar->data_lock);
3863	peer = ath10k_peer_find_by_id(ar, peer_id);
3864	if (!peer || !peer->sta) {
3865		ath10k_warn(ar, "Invalid peer id %d in peer stats buffer\n",
3866			    peer_id);
3867		goto out;
3868	}
3869
3870	sta = peer->sta;
3871	for (i = 0; i < tx_stats->tx_ppdu_cnt; i++) {
3872		p_tx_stats->succ_bytes =
3873			__le16_to_cpu(tx_stats->success_bytes[i]);
3874		p_tx_stats->retry_bytes =
3875			__le16_to_cpu(tx_stats->retry_bytes[i]);
3876		p_tx_stats->failed_bytes =
3877			__le16_to_cpu(tx_stats->failed_bytes[i]);
3878		p_tx_stats->ratecode = tx_stats->ratecode[i];
3879		p_tx_stats->flags = tx_stats->flags[i];
3880		p_tx_stats->succ_pkts = tx_stats->success_pkts[i];
3881		p_tx_stats->retry_pkts = tx_stats->retry_pkts[i];
3882		p_tx_stats->failed_pkts = tx_stats->failed_pkts[i];
3883
3884		ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
3885	}
3886	spin_unlock_bh(&ar->data_lock);
3887	rcu_read_unlock();
3888
3889	return;
3890
3891out:
3892	spin_unlock_bh(&ar->data_lock);
3893	rcu_read_unlock();
3894}
3895
3896static int ath10k_htt_rx_pn_len(enum htt_security_types sec_type)
3897{
3898	switch (sec_type) {
3899	case HTT_SECURITY_TKIP:
3900	case HTT_SECURITY_TKIP_NOMIC:
3901	case HTT_SECURITY_AES_CCMP:
3902		return 48;
3903	default:
3904		return 0;
3905	}
3906}
3907
3908static void ath10k_htt_rx_sec_ind_handler(struct ath10k *ar,
3909					  struct htt_security_indication *ev)
3910{
3911	enum htt_txrx_sec_cast_type sec_index;
3912	enum htt_security_types sec_type;
3913	struct ath10k_peer *peer;
3914
3915	spin_lock_bh(&ar->data_lock);
3916
3917	peer = ath10k_peer_find_by_id(ar, __le16_to_cpu(ev->peer_id));
3918	if (!peer) {
3919		ath10k_warn(ar, "failed to find peer id %d for security indication",
3920			    __le16_to_cpu(ev->peer_id));
3921		goto out;
3922	}
3923
3924	sec_type = MS(ev->flags, HTT_SECURITY_TYPE);
3925
3926	if (ev->flags & HTT_SECURITY_IS_UNICAST)
3927		sec_index = HTT_TXRX_SEC_UCAST;
3928	else
3929		sec_index = HTT_TXRX_SEC_MCAST;
3930
3931	peer->rx_pn[sec_index].sec_type = sec_type;
3932	peer->rx_pn[sec_index].pn_len = ath10k_htt_rx_pn_len(sec_type);
3933
3934	memset(peer->tids_last_pn_valid, 0, sizeof(peer->tids_last_pn_valid));
3935	memset(peer->tids_last_pn, 0, sizeof(peer->tids_last_pn));
3936
3937out:
3938	spin_unlock_bh(&ar->data_lock);
3939}
3940
3941bool ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
3942{
3943	struct ath10k_htt *htt = &ar->htt;
3944	struct htt_resp *resp = (struct htt_resp *)skb->data;
3945	enum htt_t2h_msg_type type;
3946
3947	/* confirm alignment */
3948	if (!IS_ALIGNED((unsigned long)skb->data, 4))
3949		ath10k_warn(ar, "unaligned htt message, expect trouble\n");
3950
3951	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
3952		   resp->hdr.msg_type);
3953
3954	if (resp->hdr.msg_type >= ar->htt.t2h_msg_types_max) {
3955		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, unsupported msg_type: 0x%0X\n max: 0x%0X",
3956			   resp->hdr.msg_type, ar->htt.t2h_msg_types_max);
3957		return true;
3958	}
3959	type = ar->htt.t2h_msg_types[resp->hdr.msg_type];
3960
3961	switch (type) {
3962	case HTT_T2H_MSG_TYPE_VERSION_CONF: {
3963		htt->target_version_major = resp->ver_resp.major;
3964		htt->target_version_minor = resp->ver_resp.minor;
3965		complete(&htt->target_version_received);
3966		break;
3967	}
3968	case HTT_T2H_MSG_TYPE_RX_IND:
3969		if (ar->bus_param.dev_type != ATH10K_DEV_TYPE_HL) {
3970			ath10k_htt_rx_proc_rx_ind_ll(htt, &resp->rx_ind);
3971		} else {
3972			skb_queue_tail(&htt->rx_indication_head, skb);
3973			return false;
3974		}
3975		break;
3976	case HTT_T2H_MSG_TYPE_PEER_MAP: {
3977		struct htt_peer_map_event ev = {
3978			.vdev_id = resp->peer_map.vdev_id,
3979			.peer_id = __le16_to_cpu(resp->peer_map.peer_id),
3980		};
3981		memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
3982		ath10k_peer_map_event(htt, &ev);
3983		break;
3984	}
3985	case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
3986		struct htt_peer_unmap_event ev = {
3987			.peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
3988		};
3989		ath10k_peer_unmap_event(htt, &ev);
3990		break;
3991	}
3992	case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
3993		struct htt_tx_done tx_done = {};
3994		struct ath10k_htt *htt = &ar->htt;
3995		struct ath10k_htc *htc = &ar->htc;
3996		struct ath10k_htc_ep *ep = &ar->htc.endpoint[htt->eid];
3997		int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
3998		int info = __le32_to_cpu(resp->mgmt_tx_completion.info);
3999
4000		tx_done.msdu_id = __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
4001
4002		switch (status) {
4003		case HTT_MGMT_TX_STATUS_OK:
4004			tx_done.status = HTT_TX_COMPL_STATE_ACK;
4005			if (test_bit(WMI_SERVICE_HTT_MGMT_TX_COMP_VALID_FLAGS,
4006				     ar->wmi.svc_map) &&
4007			    (resp->mgmt_tx_completion.flags &
4008			     HTT_MGMT_TX_CMPL_FLAG_ACK_RSSI)) {
4009				tx_done.ack_rssi =
4010				FIELD_GET(HTT_MGMT_TX_CMPL_INFO_ACK_RSSI_MASK,
4011					  info);
4012			}
4013			break;
4014		case HTT_MGMT_TX_STATUS_RETRY:
4015			tx_done.status = HTT_TX_COMPL_STATE_NOACK;
4016			break;
4017		case HTT_MGMT_TX_STATUS_DROP:
4018			tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
4019			break;
4020		}
4021
4022		if (htt->disable_tx_comp) {
4023			spin_lock_bh(&htc->tx_lock);
4024			ep->tx_credits++;
4025			spin_unlock_bh(&htc->tx_lock);
4026		}
4027
4028		status = ath10k_txrx_tx_unref(htt, &tx_done);
4029		if (!status) {
4030			spin_lock_bh(&htt->tx_lock);
4031			ath10k_htt_tx_mgmt_dec_pending(htt);
4032			spin_unlock_bh(&htt->tx_lock);
4033		}
4034		break;
4035	}
4036	case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
4037		ath10k_htt_rx_tx_compl_ind(htt->ar, skb);
4038		break;
4039	case HTT_T2H_MSG_TYPE_SEC_IND: {
4040		struct ath10k *ar = htt->ar;
4041		struct htt_security_indication *ev = &resp->security_indication;
4042
4043		ath10k_htt_rx_sec_ind_handler(ar, ev);
4044		ath10k_dbg(ar, ATH10K_DBG_HTT,
4045			   "sec ind peer_id %d unicast %d type %d\n",
4046			  __le16_to_cpu(ev->peer_id),
4047			  !!(ev->flags & HTT_SECURITY_IS_UNICAST),
4048			  MS(ev->flags, HTT_SECURITY_TYPE));
4049		complete(&ar->install_key_done);
4050		break;
4051	}
4052	case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
4053		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
4054				skb->data, skb->len);
4055		atomic_inc(&htt->num_mpdus_ready);
4056
4057		return ath10k_htt_rx_proc_rx_frag_ind(htt,
4058						      &resp->rx_frag_ind,
4059						      skb);
4060	}
4061	case HTT_T2H_MSG_TYPE_TEST:
4062		break;
4063	case HTT_T2H_MSG_TYPE_STATS_CONF:
4064		trace_ath10k_htt_stats(ar, skb->data, skb->len);
4065		break;
4066	case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
4067		/* Firmware can return tx frames if it's unable to fully
4068		 * process them and suspects host may be able to fix it. ath10k
4069		 * sends all tx frames as already inspected so this shouldn't
4070		 * happen unless fw has a bug.
4071		 */
4072		ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
4073		break;
4074	case HTT_T2H_MSG_TYPE_RX_ADDBA:
4075		ath10k_htt_rx_addba(ar, resp);
4076		break;
4077	case HTT_T2H_MSG_TYPE_RX_DELBA:
4078		ath10k_htt_rx_delba(ar, resp);
4079		break;
4080	case HTT_T2H_MSG_TYPE_PKTLOG: {
4081		trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
4082					skb->len -
4083					offsetof(struct htt_resp,
4084						 pktlog_msg.payload));
4085
4086		if (ath10k_peer_stats_enabled(ar))
4087			ath10k_fetch_10_2_tx_stats(ar,
4088						   resp->pktlog_msg.payload);
4089		break;
4090	}
4091	case HTT_T2H_MSG_TYPE_RX_FLUSH: {
4092		/* Ignore this event because mac80211 takes care of Rx
4093		 * aggregation reordering.
4094		 */
4095		break;
4096	}
4097	case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
4098		skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
4099		return false;
4100	}
4101	case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND: {
4102		struct ath10k_htt *htt = &ar->htt;
4103		struct ath10k_htc *htc = &ar->htc;
4104		struct ath10k_htc_ep *ep = &ar->htc.endpoint[htt->eid];
4105		u32 msg_word = __le32_to_cpu(*(__le32 *)resp);
4106		int htt_credit_delta;
4107
4108		htt_credit_delta = HTT_TX_CREDIT_DELTA_ABS_GET(msg_word);
4109		if (HTT_TX_CREDIT_SIGN_BIT_GET(msg_word))
4110			htt_credit_delta = -htt_credit_delta;
4111
4112		ath10k_dbg(ar, ATH10K_DBG_HTT,
4113			   "htt credit update delta %d\n",
4114			   htt_credit_delta);
4115
4116		if (htt->disable_tx_comp) {
4117			spin_lock_bh(&htc->tx_lock);
4118			ep->tx_credits += htt_credit_delta;
4119			spin_unlock_bh(&htc->tx_lock);
4120			ath10k_dbg(ar, ATH10K_DBG_HTT,
4121				   "htt credit total %d\n",
4122				   ep->tx_credits);
4123			ep->ep_ops.ep_tx_credits(htc->ar);
4124		}
4125		break;
4126	}
4127	case HTT_T2H_MSG_TYPE_CHAN_CHANGE: {
4128		u32 phymode = __le32_to_cpu(resp->chan_change.phymode);
4129		u32 freq = __le32_to_cpu(resp->chan_change.freq);
4130
4131		ar->tgt_oper_chan = ieee80211_get_channel(ar->hw->wiphy, freq);
4132		ath10k_dbg(ar, ATH10K_DBG_HTT,
4133			   "htt chan change freq %u phymode %s\n",
4134			   freq, ath10k_wmi_phymode_str(phymode));
4135		break;
4136	}
4137	case HTT_T2H_MSG_TYPE_AGGR_CONF:
4138		break;
4139	case HTT_T2H_MSG_TYPE_TX_FETCH_IND: {
4140		struct sk_buff *tx_fetch_ind = skb_copy(skb, GFP_ATOMIC);
4141
4142		if (!tx_fetch_ind) {
4143			ath10k_warn(ar, "failed to copy htt tx fetch ind\n");
4144			break;
4145		}
4146		skb_queue_tail(&htt->tx_fetch_ind_q, tx_fetch_ind);
4147		break;
4148	}
4149	case HTT_T2H_MSG_TYPE_TX_FETCH_CONFIRM:
4150		ath10k_htt_rx_tx_fetch_confirm(ar, skb);
4151		break;
4152	case HTT_T2H_MSG_TYPE_TX_MODE_SWITCH_IND:
4153		ath10k_htt_rx_tx_mode_switch_ind(ar, skb);
4154		break;
4155	case HTT_T2H_MSG_TYPE_PEER_STATS:
4156		ath10k_htt_fetch_peer_stats(ar, skb);
4157		break;
4158	case HTT_T2H_MSG_TYPE_EN_STATS:
4159	default:
4160		ath10k_warn(ar, "htt event (%d) not handled\n",
4161			    resp->hdr.msg_type);
4162		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
4163				skb->data, skb->len);
4164		break;
4165	}
4166	return true;
4167}
4168EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
4169
4170void ath10k_htt_rx_pktlog_completion_handler(struct ath10k *ar,
4171					     struct sk_buff *skb)
4172{
4173	trace_ath10k_htt_pktlog(ar, skb->data, skb->len);
4174	dev_kfree_skb_any(skb);
4175}
4176EXPORT_SYMBOL(ath10k_htt_rx_pktlog_completion_handler);
4177
4178static int ath10k_htt_rx_deliver_msdu(struct ath10k *ar, int quota, int budget)
4179{
4180	struct sk_buff *skb;
4181
4182	while (quota < budget) {
4183		if (skb_queue_empty(&ar->htt.rx_msdus_q))
4184			break;
4185
4186		skb = skb_dequeue(&ar->htt.rx_msdus_q);
4187		if (!skb)
4188			break;
4189		ath10k_process_rx(ar, skb);
4190		quota++;
4191	}
4192
4193	return quota;
4194}
4195
4196int ath10k_htt_rx_hl_indication(struct ath10k *ar, int budget)
4197{
4198	struct htt_resp *resp;
4199	struct ath10k_htt *htt = &ar->htt;
4200	struct sk_buff *skb;
4201	bool release;
4202	int quota;
4203
4204	for (quota = 0; quota < budget; quota++) {
4205		skb = skb_dequeue(&htt->rx_indication_head);
4206		if (!skb)
4207			break;
4208
4209		resp = (struct htt_resp *)skb->data;
4210
4211		release = ath10k_htt_rx_proc_rx_ind_hl(htt,
4212						       &resp->rx_ind_hl,
4213						       skb,
4214						       HTT_RX_PN_CHECK,
4215						       HTT_RX_NON_TKIP_MIC);
4216
4217		if (release)
4218			dev_kfree_skb_any(skb);
4219
4220		ath10k_dbg(ar, ATH10K_DBG_HTT, "rx indication poll pending count:%d\n",
4221			   skb_queue_len(&htt->rx_indication_head));
4222	}
4223	return quota;
4224}
4225EXPORT_SYMBOL(ath10k_htt_rx_hl_indication);
4226
4227int ath10k_htt_txrx_compl_task(struct ath10k *ar, int budget)
4228{
4229	struct ath10k_htt *htt = &ar->htt;
4230	struct htt_tx_done tx_done = {};
4231	struct sk_buff_head tx_ind_q;
4232	struct sk_buff *skb;
4233	unsigned long flags;
4234	int quota = 0, done, ret;
4235	bool resched_napi = false;
4236
4237	__skb_queue_head_init(&tx_ind_q);
4238
4239	/* Process pending frames before dequeuing more data
4240	 * from hardware.
4241	 */
4242	quota = ath10k_htt_rx_deliver_msdu(ar, quota, budget);
4243	if (quota == budget) {
4244		resched_napi = true;
4245		goto exit;
4246	}
4247
4248	while ((skb = skb_dequeue(&htt->rx_in_ord_compl_q))) {
4249		spin_lock_bh(&htt->rx_ring.lock);
4250		ret = ath10k_htt_rx_in_ord_ind(ar, skb);
4251		spin_unlock_bh(&htt->rx_ring.lock);
4252
4253		dev_kfree_skb_any(skb);
4254		if (ret == -EIO) {
4255			resched_napi = true;
4256			goto exit;
4257		}
4258	}
4259
4260	while (atomic_read(&htt->num_mpdus_ready)) {
4261		ret = ath10k_htt_rx_handle_amsdu(htt);
4262		if (ret == -EIO) {
4263			resched_napi = true;
4264			goto exit;
4265		}
4266		atomic_dec(&htt->num_mpdus_ready);
4267	}
4268
4269	/* Deliver received data after processing data from hardware */
4270	quota = ath10k_htt_rx_deliver_msdu(ar, quota, budget);
4271
4272	/* From NAPI documentation:
4273	 *  The napi poll() function may also process TX completions, in which
4274	 *  case if it processes the entire TX ring then it should count that
4275	 *  work as the rest of the budget.
4276	 */
4277	if ((quota < budget) && !kfifo_is_empty(&htt->txdone_fifo))
4278		quota = budget;
4279
4280	/* kfifo_get: called only within txrx_tasklet so it's neatly serialized.
4281	 * From kfifo_get() documentation:
4282	 *  Note that with only one concurrent reader and one concurrent writer,
4283	 *  you don't need extra locking to use these macro.
4284	 */
4285	while (kfifo_get(&htt->txdone_fifo, &tx_done))
4286		ath10k_txrx_tx_unref(htt, &tx_done);
4287
4288	ath10k_mac_tx_push_pending(ar);
4289
4290	spin_lock_irqsave(&htt->tx_fetch_ind_q.lock, flags);
4291	skb_queue_splice_init(&htt->tx_fetch_ind_q, &tx_ind_q);
4292	spin_unlock_irqrestore(&htt->tx_fetch_ind_q.lock, flags);
4293
4294	while ((skb = __skb_dequeue(&tx_ind_q))) {
4295		ath10k_htt_rx_tx_fetch_ind(ar, skb);
4296		dev_kfree_skb_any(skb);
4297	}
4298
4299exit:
4300	ath10k_htt_rx_msdu_buff_replenish(htt);
4301	/* In case of rx failure or more data to read, report budget
4302	 * to reschedule NAPI poll
4303	 */
4304	done = resched_napi ? budget : quota;
4305
4306	return done;
4307}
4308EXPORT_SYMBOL(ath10k_htt_txrx_compl_task);
4309
4310static const struct ath10k_htt_rx_ops htt_rx_ops_32 = {
4311	.htt_get_rx_ring_size = ath10k_htt_get_rx_ring_size_32,
4312	.htt_config_paddrs_ring = ath10k_htt_config_paddrs_ring_32,
4313	.htt_set_paddrs_ring = ath10k_htt_set_paddrs_ring_32,
4314	.htt_get_vaddr_ring = ath10k_htt_get_vaddr_ring_32,
4315	.htt_reset_paddrs_ring = ath10k_htt_reset_paddrs_ring_32,
4316};
4317
4318static const struct ath10k_htt_rx_ops htt_rx_ops_64 = {
4319	.htt_get_rx_ring_size = ath10k_htt_get_rx_ring_size_64,
4320	.htt_config_paddrs_ring = ath10k_htt_config_paddrs_ring_64,
4321	.htt_set_paddrs_ring = ath10k_htt_set_paddrs_ring_64,
4322	.htt_get_vaddr_ring = ath10k_htt_get_vaddr_ring_64,
4323	.htt_reset_paddrs_ring = ath10k_htt_reset_paddrs_ring_64,
4324};
4325
4326static const struct ath10k_htt_rx_ops htt_rx_ops_hl = {
4327	.htt_rx_proc_rx_frag_ind = ath10k_htt_rx_proc_rx_frag_ind_hl,
4328};
4329
4330void ath10k_htt_set_rx_ops(struct ath10k_htt *htt)
4331{
4332	struct ath10k *ar = htt->ar;
4333
4334	if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
4335		htt->rx_ops = &htt_rx_ops_hl;
4336	else if (ar->hw_params.target_64bit)
4337		htt->rx_ops = &htt_rx_ops_64;
4338	else
4339		htt->rx_ops = &htt_rx_ops_32;
4340}