Linux Audio

Check our new training course

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