htt_rx.c 113 KB
Newer Older
1
// SPDX-License-Identifier: ISC
2 3
/*
 * Copyright (c) 2005-2011 Atheros Communications Inc.
Kalle Valo's avatar
Kalle Valo committed
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 10 11 12
#include "htc.h"
#include "htt.h"
#include "txrx.h"
#include "debug.h"
13
#include "trace.h"
14
#include "mac.h"
15 16

#include <linux/log2.h>
17
#include <linux/bitfield.h>
18 19 20 21

/* when under memory pressure rx ring refill may fail and needs a retry */
#define HTT_RX_RING_REFILL_RETRY_MS 50

22 23
#define HTT_RX_RING_REFILL_RESCHED_MS 5

24 25
static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);

26
static struct sk_buff *
27
ath10k_htt_rx_find_skb_paddr(struct ath10k *ar, u64 paddr)
28 29 30 31 32 33 34 35 36 37 38
{
	struct ath10k_skb_rxcb *rxcb;

	hash_for_each_possible(ar->htt.rx_ring.skb_table, rxcb, hlist, paddr)
		if (rxcb->paddr == paddr)
			return ATH10K_RXCB_SKB(rxcb);

	WARN_ON_ONCE(1);
	return NULL;
}

39 40 41
static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
{
	struct sk_buff *skb;
42 43
	struct ath10k_skb_rxcb *rxcb;
	struct hlist_node *n;
44 45
	int i;

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	if (htt->rx_ring.in_ord_rx) {
		hash_for_each_safe(htt->rx_ring.skb_table, i, n, rxcb, hlist) {
			skb = ATH10K_RXCB_SKB(rxcb);
			dma_unmap_single(htt->ar->dev, rxcb->paddr,
					 skb->len + skb_tailroom(skb),
					 DMA_FROM_DEVICE);
			hash_del(&rxcb->hlist);
			dev_kfree_skb_any(skb);
		}
	} else {
		for (i = 0; i < htt->rx_ring.size; i++) {
			skb = htt->rx_ring.netbufs_ring[i];
			if (!skb)
				continue;

			rxcb = ATH10K_SKB_RXCB(skb);
			dma_unmap_single(htt->ar->dev, rxcb->paddr,
					 skb->len + skb_tailroom(skb),
					 DMA_FROM_DEVICE);
			dev_kfree_skb_any(skb);
		}
67 68 69
	}

	htt->rx_ring.fill_cnt = 0;
70 71 72
	hash_init(htt->rx_ring.skb_table);
	memset(htt->rx_ring.netbufs_ring, 0,
	       htt->rx_ring.size * sizeof(htt->rx_ring.netbufs_ring[0]));
73 74
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static size_t ath10k_htt_get_rx_ring_size_32(struct ath10k_htt *htt)
{
	return htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring_32);
}

static size_t ath10k_htt_get_rx_ring_size_64(struct ath10k_htt *htt)
{
	return htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring_64);
}

static void ath10k_htt_config_paddrs_ring_32(struct ath10k_htt *htt,
					     void *vaddr)
{
	htt->rx_ring.paddrs_ring_32 = vaddr;
}

static void ath10k_htt_config_paddrs_ring_64(struct ath10k_htt *htt,
					     void *vaddr)
{
	htt->rx_ring.paddrs_ring_64 = vaddr;
}

static void ath10k_htt_set_paddrs_ring_32(struct ath10k_htt *htt,
					  dma_addr_t paddr, int idx)
{
	htt->rx_ring.paddrs_ring_32[idx] = __cpu_to_le32(paddr);
}

static void ath10k_htt_set_paddrs_ring_64(struct ath10k_htt *htt,
					  dma_addr_t paddr, int idx)
{
	htt->rx_ring.paddrs_ring_64[idx] = __cpu_to_le64(paddr);
}

static void ath10k_htt_reset_paddrs_ring_32(struct ath10k_htt *htt, int idx)
{
	htt->rx_ring.paddrs_ring_32[idx] = 0;
}

static void ath10k_htt_reset_paddrs_ring_64(struct ath10k_htt *htt, int idx)
{
	htt->rx_ring.paddrs_ring_64[idx] = 0;
}

static void *ath10k_htt_get_vaddr_ring_32(struct ath10k_htt *htt)
{
	return (void *)htt->rx_ring.paddrs_ring_32;
}

static void *ath10k_htt_get_vaddr_ring_64(struct ath10k_htt *htt)
{
	return (void *)htt->rx_ring.paddrs_ring_64;
}

129 130 131
static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
{
	struct htt_rx_desc *rx_desc;
132
	struct ath10k_skb_rxcb *rxcb;
133 134 135 136
	struct sk_buff *skb;
	dma_addr_t paddr;
	int ret = 0, idx;

137 138 139 140 141 142 143
	/* The Full Rx Reorder firmware has no way of telling the host
	 * implicitly when it copied HTT Rx Ring buffers to MAC Rx Ring.
	 * To keep things simple make sure ring is always half empty. This
	 * guarantees there'll be no replenishment overruns possible.
	 */
	BUILD_BUG_ON(HTT_RX_RING_FILL_LEVEL >= HTT_RX_RING_SIZE / 2);

144
	idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	while (num > 0) {
		skb = dev_alloc_skb(HTT_RX_BUF_SIZE + HTT_RX_DESC_ALIGN);
		if (!skb) {
			ret = -ENOMEM;
			goto fail;
		}

		if (!IS_ALIGNED((unsigned long)skb->data, HTT_RX_DESC_ALIGN))
			skb_pull(skb,
				 PTR_ALIGN(skb->data, HTT_RX_DESC_ALIGN) -
				 skb->data);

		/* Clear rx_desc attention word before posting to Rx ring */
		rx_desc = (struct htt_rx_desc *)skb->data;
		rx_desc->attention.flags = __cpu_to_le32(0);

		paddr = dma_map_single(htt->ar->dev, skb->data,
				       skb->len + skb_tailroom(skb),
				       DMA_FROM_DEVICE);

		if (unlikely(dma_mapping_error(htt->ar->dev, paddr))) {
			dev_kfree_skb_any(skb);
			ret = -ENOMEM;
			goto fail;
		}

171 172
		rxcb = ATH10K_SKB_RXCB(skb);
		rxcb->paddr = paddr;
173
		htt->rx_ring.netbufs_ring[idx] = skb;
174
		ath10k_htt_set_paddrs_ring(htt, paddr, idx);
175 176
		htt->rx_ring.fill_cnt++;

177 178 179
		if (htt->rx_ring.in_ord_rx) {
			hash_add(htt->rx_ring.skb_table,
				 &ATH10K_SKB_RXCB(skb)->hlist,
180
				 paddr);
181 182
		}

183 184 185 186 187 188
		num--;
		idx++;
		idx &= htt->rx_ring.size_mask;
	}

fail:
189 190 191 192 193
	/*
	 * Make sure the rx buffer is updated before available buffer
	 * index to avoid any potential rx ring corruption.
	 */
	mb();
194
	*htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
195 196 197 198 199 200 201 202 203 204 205
	return ret;
}

static int ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
{
	lockdep_assert_held(&htt->rx_ring.lock);
	return __ath10k_htt_rx_ring_fill_n(htt, num);
}

static void ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt *htt)
{
206
	int ret, num_deficit, num_to_fill;
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221
	/* Refilling the whole RX ring buffer proves to be a bad idea. The
	 * reason is RX may take up significant amount of CPU cycles and starve
	 * other tasks, e.g. TX on an ethernet device while acting as a bridge
	 * with ath10k wlan interface. This ended up with very poor performance
	 * once CPU the host system was overwhelmed with RX on ath10k.
	 *
	 * By limiting the number of refills the replenishing occurs
	 * progressively. This in turns makes use of the fact tasklets are
	 * processed in FIFO order. This means actual RX processing can starve
	 * out refilling. If there's not enough buffers on RX ring FW will not
	 * report RX until it is refilled with enough buffers. This
	 * automatically balances load wrt to CPU power.
	 *
	 * This probably comes at a cost of lower maximum throughput but
222 223
	 * improves the average and stability.
	 */
224
	spin_lock_bh(&htt->rx_ring.lock);
225 226 227
	num_deficit = htt->rx_ring.fill_level - htt->rx_ring.fill_cnt;
	num_to_fill = min(ATH10K_HTT_MAX_NUM_REFILL, num_deficit);
	num_deficit -= num_to_fill;
228 229 230 231 232 233 234 235 236 237
	ret = ath10k_htt_rx_ring_fill_n(htt, num_to_fill);
	if (ret == -ENOMEM) {
		/*
		 * Failed to fill it to the desired level -
		 * we'll start a timer and try again next time.
		 * As long as enough buffers are left in the ring for
		 * another A-MPDU rx, no special recovery is needed.
		 */
		mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
			  msecs_to_jiffies(HTT_RX_RING_REFILL_RETRY_MS));
238
	} else if (num_deficit > 0) {
239 240
		mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
			  msecs_to_jiffies(HTT_RX_RING_REFILL_RESCHED_MS));
241 242 243 244
	}
	spin_unlock_bh(&htt->rx_ring.lock);
}

245
static void ath10k_htt_rx_ring_refill_retry(struct timer_list *t)
246
{
247
	struct ath10k_htt *htt = from_timer(htt, t, rx_ring.refill_retry_timer);
248

249 250 251
	ath10k_htt_rx_msdu_buff_replenish(htt);
}

252
int ath10k_htt_rx_ring_refill(struct ath10k *ar)
253
{
254 255
	struct ath10k_htt *htt = &ar->htt;
	int ret;
256

257
	if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
258 259
		return 0;

260 261 262
	spin_lock_bh(&htt->rx_ring.lock);
	ret = ath10k_htt_rx_ring_fill_n(htt, (htt->rx_ring.fill_level -
					      htt->rx_ring.fill_cnt));
263

264 265 266
	if (ret)
		ath10k_htt_rx_ring_free(htt);

267 268
	spin_unlock_bh(&htt->rx_ring.lock);

269
	return ret;
270
}
271

272
void ath10k_htt_rx_free(struct ath10k_htt *htt)
273
{
274
	if (htt->ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
275 276
		return;

277
	del_timer_sync(&htt->rx_ring.refill_retry_timer);
278

279
	skb_queue_purge(&htt->rx_msdus_q);
280
	skb_queue_purge(&htt->rx_in_ord_compl_q);
281
	skb_queue_purge(&htt->tx_fetch_ind_q);
282

283
	spin_lock_bh(&htt->rx_ring.lock);
284
	ath10k_htt_rx_ring_free(htt);
285
	spin_unlock_bh(&htt->rx_ring.lock);
286 287

	dma_free_coherent(htt->ar->dev,
288 289
			  ath10k_htt_get_rx_ring_size(htt),
			  ath10k_htt_get_vaddr_ring(htt),
290 291 292 293 294 295 296 297 298 299 300 301
			  htt->rx_ring.base_paddr);

	dma_free_coherent(htt->ar->dev,
			  sizeof(*htt->rx_ring.alloc_idx.vaddr),
			  htt->rx_ring.alloc_idx.vaddr,
			  htt->rx_ring.alloc_idx.paddr);

	kfree(htt->rx_ring.netbufs_ring);
}

static inline struct sk_buff *ath10k_htt_rx_netbuf_pop(struct ath10k_htt *htt)
{
302
	struct ath10k *ar = htt->ar;
303 304 305
	int idx;
	struct sk_buff *msdu;

306
	lockdep_assert_held(&htt->rx_ring.lock);
307

308
	if (htt->rx_ring.fill_cnt == 0) {
309
		ath10k_warn(ar, "tried to pop sk_buff from an empty rx ring\n");
310 311
		return NULL;
	}
312 313 314

	idx = htt->rx_ring.sw_rd_idx.msdu_payld;
	msdu = htt->rx_ring.netbufs_ring[idx];
315
	htt->rx_ring.netbufs_ring[idx] = NULL;
316
	ath10k_htt_reset_paddrs_ring(htt, idx);
317 318 319 320 321 322

	idx++;
	idx &= htt->rx_ring.size_mask;
	htt->rx_ring.sw_rd_idx.msdu_payld = idx;
	htt->rx_ring.fill_cnt--;

323
	dma_unmap_single(htt->ar->dev,
324
			 ATH10K_SKB_RXCB(msdu)->paddr,
325 326 327 328 329
			 msdu->len + skb_tailroom(msdu),
			 DMA_FROM_DEVICE);
	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
			msdu->data, msdu->len + skb_tailroom(msdu));

330 331 332
	return msdu;
}

333
/* return: < 0 fatal error, 0 - non chained msdu, 1 chained msdu */
334
static int ath10k_htt_rx_amsdu_pop(struct ath10k_htt *htt,
335
				   struct sk_buff_head *amsdu)
336
{
337
	struct ath10k *ar = htt->ar;
338
	int msdu_len, msdu_chaining = 0;
339
	struct sk_buff *msdu;
340 341
	struct htt_rx_desc *rx_desc;

342 343
	lockdep_assert_held(&htt->rx_ring.lock);

344
	for (;;) {
345 346
		int last_msdu, msdu_len_invalid, msdu_chained;

347 348 349
		msdu = ath10k_htt_rx_netbuf_pop(htt);
		if (!msdu) {
			__skb_queue_purge(amsdu);
350
			return -ENOENT;
351 352 353 354
		}

		__skb_queue_tail(amsdu, msdu);

355 356 357
		rx_desc = (struct htt_rx_desc *)msdu->data;

		/* FIXME: we must report msdu payload since this is what caller
358 359
		 * expects now
		 */
360 361 362 363 364 365 366 367 368 369 370 371 372
		skb_put(msdu, offsetof(struct htt_rx_desc, msdu_payload));
		skb_pull(msdu, offsetof(struct htt_rx_desc, msdu_payload));

		/*
		 * Sanity check - confirm the HW is finished filling in the
		 * rx data.
		 * If the HW and SW are working correctly, then it's guaranteed
		 * that the HW's MAC DMA is done before this point in the SW.
		 * To prevent the case that we handle a stale Rx descriptor,
		 * just assert for now until we have a way to recover.
		 */
		if (!(__le32_to_cpu(rx_desc->attention.flags)
				& RX_ATTENTION_FLAGS_MSDU_DONE)) {
373
			__skb_queue_purge(amsdu);
374
			return -EIO;
375 376 377 378 379
		}

		msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
					& (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
					   RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
380
		msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.common.info0),
381 382 383 384 385 386 387 388 389 390
			      RX_MSDU_START_INFO0_MSDU_LENGTH);
		msdu_chained = rx_desc->frag_info.ring2_more_count;

		if (msdu_len_invalid)
			msdu_len = 0;

		skb_trim(msdu, 0);
		skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
		msdu_len -= msdu->len;

391
		/* Note: Chained buffers do not contain rx descriptor */
392
		while (msdu_chained--) {
393 394 395
			msdu = ath10k_htt_rx_netbuf_pop(htt);
			if (!msdu) {
				__skb_queue_purge(amsdu);
396
				return -ENOENT;
397 398
			}

399 400 401 402
			__skb_queue_tail(amsdu, msdu);
			skb_trim(msdu, 0);
			skb_put(msdu, min(msdu_len, HTT_RX_BUF_SIZE));
			msdu_len -= msdu->len;
403
			msdu_chaining = 1;
404 405
		}

406
		last_msdu = __le32_to_cpu(rx_desc->msdu_end.common.info0) &
407 408
				RX_MSDU_END_INFO0_LAST_MSDU;

409
		trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
410
					 sizeof(*rx_desc) - sizeof(u32));
411

412 413
		if (last_msdu)
			break;
414 415
	}

416
	if (skb_queue_empty(amsdu))
417 418
		msdu_chaining = -1;

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	/*
	 * Don't refill the ring yet.
	 *
	 * First, the elements popped here are still in use - it is not
	 * safe to overwrite them until the matching call to
	 * mpdu_desc_list_next. Second, for efficiency it is preferable to
	 * refill the rx ring with 1 PPDU's worth of rx buffers (something
	 * like 32 x 3 buffers), rather than one MPDU's worth of rx buffers
	 * (something like 3 buffers). Consequently, we'll rely on the txrx
	 * SW to tell us when it is done pulling all the PPDU's rx buffers
	 * out of the rx ring, and then refill it just once.
	 */

	return msdu_chaining;
}

435
static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
436
					       u64 paddr)
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
{
	struct ath10k *ar = htt->ar;
	struct ath10k_skb_rxcb *rxcb;
	struct sk_buff *msdu;

	lockdep_assert_held(&htt->rx_ring.lock);

	msdu = ath10k_htt_rx_find_skb_paddr(ar, paddr);
	if (!msdu)
		return NULL;

	rxcb = ATH10K_SKB_RXCB(msdu);
	hash_del(&rxcb->hlist);
	htt->rx_ring.fill_cnt--;

	dma_unmap_single(htt->ar->dev, rxcb->paddr,
			 msdu->len + skb_tailroom(msdu),
			 DMA_FROM_DEVICE);
	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
			msdu->data, msdu->len + skb_tailroom(msdu));

	return msdu;
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
static inline void ath10k_htt_append_frag_list(struct sk_buff *skb_head,
					       struct sk_buff *frag_list,
					       unsigned int frag_len)
{
	skb_shinfo(skb_head)->frag_list = frag_list;
	skb_head->data_len = frag_len;
	skb_head->len += skb_head->data_len;
}

static int ath10k_htt_rx_handle_amsdu_mon_32(struct ath10k_htt *htt,
					     struct sk_buff *msdu,
					     struct htt_rx_in_ord_msdu_desc **msdu_desc)
{
	struct ath10k *ar = htt->ar;
	u32 paddr;
	struct sk_buff *frag_buf;
	struct sk_buff *prev_frag_buf;
	u8 last_frag;
	struct htt_rx_in_ord_msdu_desc *ind_desc = *msdu_desc;
	struct htt_rx_desc *rxd;
	int amsdu_len = __le16_to_cpu(ind_desc->msdu_len);

	rxd = (void *)msdu->data;
	trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));

	skb_put(msdu, sizeof(struct htt_rx_desc));
	skb_pull(msdu, sizeof(struct htt_rx_desc));
	skb_put(msdu, min(amsdu_len, HTT_RX_MSDU_SIZE));
	amsdu_len -= msdu->len;

	last_frag = ind_desc->reserved;
	if (last_frag) {
		if (amsdu_len) {
			ath10k_warn(ar, "invalid amsdu len %u, left %d",
				    __le16_to_cpu(ind_desc->msdu_len),
				    amsdu_len);
		}
		return 0;
	}

	ind_desc++;
	paddr = __le32_to_cpu(ind_desc->msdu_paddr);
	frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
	if (!frag_buf) {
		ath10k_warn(ar, "failed to pop frag-1 paddr: 0x%x", paddr);
		return -ENOENT;
	}

	skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
	ath10k_htt_append_frag_list(msdu, frag_buf, amsdu_len);

	amsdu_len -= frag_buf->len;
	prev_frag_buf = frag_buf;
	last_frag = ind_desc->reserved;
	while (!last_frag) {
		ind_desc++;
		paddr = __le32_to_cpu(ind_desc->msdu_paddr);
		frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
		if (!frag_buf) {
			ath10k_warn(ar, "failed to pop frag-n paddr: 0x%x",
				    paddr);
			prev_frag_buf->next = NULL;
			return -ENOENT;
		}

		skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
		last_frag = ind_desc->reserved;
		amsdu_len -= frag_buf->len;

		prev_frag_buf->next = frag_buf;
		prev_frag_buf = frag_buf;
	}

	if (amsdu_len) {
		ath10k_warn(ar, "invalid amsdu len %u, left %d",
			    __le16_to_cpu(ind_desc->msdu_len), amsdu_len);
	}

	*msdu_desc = ind_desc;

	prev_frag_buf->next = NULL;
	return 0;
}

static int
ath10k_htt_rx_handle_amsdu_mon_64(struct ath10k_htt *htt,
				  struct sk_buff *msdu,
				  struct htt_rx_in_ord_msdu_desc_ext **msdu_desc)
{
	struct ath10k *ar = htt->ar;
	u64 paddr;
	struct sk_buff *frag_buf;
	struct sk_buff *prev_frag_buf;
	u8 last_frag;
	struct htt_rx_in_ord_msdu_desc_ext *ind_desc = *msdu_desc;
	struct htt_rx_desc *rxd;
	int amsdu_len = __le16_to_cpu(ind_desc->msdu_len);

	rxd = (void *)msdu->data;
	trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));

	skb_put(msdu, sizeof(struct htt_rx_desc));
	skb_pull(msdu, sizeof(struct htt_rx_desc));
	skb_put(msdu, min(amsdu_len, HTT_RX_MSDU_SIZE));
	amsdu_len -= msdu->len;

	last_frag = ind_desc->reserved;
	if (last_frag) {
		if (amsdu_len) {
			ath10k_warn(ar, "invalid amsdu len %u, left %d",
				    __le16_to_cpu(ind_desc->msdu_len),
				    amsdu_len);
		}
		return 0;
	}

	ind_desc++;
	paddr = __le64_to_cpu(ind_desc->msdu_paddr);
	frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
	if (!frag_buf) {
		ath10k_warn(ar, "failed to pop frag-1 paddr: 0x%llx", paddr);
		return -ENOENT;
	}

	skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
	ath10k_htt_append_frag_list(msdu, frag_buf, amsdu_len);

	amsdu_len -= frag_buf->len;
	prev_frag_buf = frag_buf;
	last_frag = ind_desc->reserved;
	while (!last_frag) {
		ind_desc++;
		paddr = __le64_to_cpu(ind_desc->msdu_paddr);
		frag_buf = ath10k_htt_rx_pop_paddr(htt, paddr);
		if (!frag_buf) {
			ath10k_warn(ar, "failed to pop frag-n paddr: 0x%llx",
				    paddr);
			prev_frag_buf->next = NULL;
			return -ENOENT;
		}

		skb_put(frag_buf, min(amsdu_len, HTT_RX_BUF_SIZE));
		last_frag = ind_desc->reserved;
		amsdu_len -= frag_buf->len;

		prev_frag_buf->next = frag_buf;
		prev_frag_buf = frag_buf;
	}

	if (amsdu_len) {
		ath10k_warn(ar, "invalid amsdu len %u, left %d",
			    __le16_to_cpu(ind_desc->msdu_len), amsdu_len);
	}

	*msdu_desc = ind_desc;

	prev_frag_buf->next = NULL;
	return 0;
}

621 622 623
static int ath10k_htt_rx_pop_paddr32_list(struct ath10k_htt *htt,
					  struct htt_rx_in_ord_ind *ev,
					  struct sk_buff_head *list)
624 625
{
	struct ath10k *ar = htt->ar;
626
	struct htt_rx_in_ord_msdu_desc *msdu_desc = ev->msdu_descs32;
627 628
	struct htt_rx_desc *rxd;
	struct sk_buff *msdu;
629
	int msdu_count, ret;
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	bool is_offload;
	u32 paddr;

	lockdep_assert_held(&htt->rx_ring.lock);

	msdu_count = __le16_to_cpu(ev->msdu_count);
	is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);

	while (msdu_count--) {
		paddr = __le32_to_cpu(msdu_desc->msdu_paddr);

		msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
		if (!msdu) {
			__skb_queue_purge(list);
			return -ENOENT;
		}

647 648 649 650 651 652 653 654 655 656 657 658
		if (!is_offload && ar->monitor_arvif) {
			ret = ath10k_htt_rx_handle_amsdu_mon_32(htt, msdu,
								&msdu_desc);
			if (ret) {
				__skb_queue_purge(list);
				return ret;
			}
			__skb_queue_tail(list, msdu);
			msdu_desc++;
			continue;
		}

659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
		__skb_queue_tail(list, msdu);

		if (!is_offload) {
			rxd = (void *)msdu->data;

			trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));

			skb_put(msdu, sizeof(*rxd));
			skb_pull(msdu, sizeof(*rxd));
			skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));

			if (!(__le32_to_cpu(rxd->attention.flags) &
			      RX_ATTENTION_FLAGS_MSDU_DONE)) {
				ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
				return -EIO;
			}
		}

		msdu_desc++;
	}

	return 0;
}

683 684 685 686 687 688 689 690
static int ath10k_htt_rx_pop_paddr64_list(struct ath10k_htt *htt,
					  struct htt_rx_in_ord_ind *ev,
					  struct sk_buff_head *list)
{
	struct ath10k *ar = htt->ar;
	struct htt_rx_in_ord_msdu_desc_ext *msdu_desc = ev->msdu_descs64;
	struct htt_rx_desc *rxd;
	struct sk_buff *msdu;
691
	int msdu_count, ret;
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	bool is_offload;
	u64 paddr;

	lockdep_assert_held(&htt->rx_ring.lock);

	msdu_count = __le16_to_cpu(ev->msdu_count);
	is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);

	while (msdu_count--) {
		paddr = __le64_to_cpu(msdu_desc->msdu_paddr);
		msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
		if (!msdu) {
			__skb_queue_purge(list);
			return -ENOENT;
		}

708 709 710 711 712 713 714 715 716 717 718 719
		if (!is_offload && ar->monitor_arvif) {
			ret = ath10k_htt_rx_handle_amsdu_mon_64(htt, msdu,
								&msdu_desc);
			if (ret) {
				__skb_queue_purge(list);
				return ret;
			}
			__skb_queue_tail(list, msdu);
			msdu_desc++;
			continue;
		}

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
		__skb_queue_tail(list, msdu);

		if (!is_offload) {
			rxd = (void *)msdu->data;

			trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));

			skb_put(msdu, sizeof(*rxd));
			skb_pull(msdu, sizeof(*rxd));
			skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));

			if (!(__le32_to_cpu(rxd->attention.flags) &
			      RX_ATTENTION_FLAGS_MSDU_DONE)) {
				ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
				return -EIO;
			}
		}

		msdu_desc++;
	}

	return 0;
}

744
int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
745
{
746
	struct ath10k *ar = htt->ar;
747
	dma_addr_t paddr;
748
	void *vaddr, *vaddr_ring;
749
	size_t size;
750 751
	struct timer_list *timer = &htt->rx_ring.refill_retry_timer;

752
	if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
753 754
		return 0;

755 756
	htt->rx_confused = false;

757 758 759 760 761
	/* XXX: The fill level could be changed during runtime in response to
	 * the host processing latency. Is this really worth it?
	 */
	htt->rx_ring.size = HTT_RX_RING_SIZE;
	htt->rx_ring.size_mask = htt->rx_ring.size - 1;
762
	htt->rx_ring.fill_level = ar->hw_params.rx_ring_fill_level;
763

764
	if (!is_power_of_2(htt->rx_ring.size)) {
765
		ath10k_warn(ar, "htt rx ring size is not power of 2\n");
766 767 768 769
		return -EINVAL;
	}

	htt->rx_ring.netbufs_ring =
770
		kcalloc(htt->rx_ring.size, sizeof(struct sk_buff *),
771 772 773 774
			GFP_KERNEL);
	if (!htt->rx_ring.netbufs_ring)
		goto err_netbuf;

775
	size = ath10k_htt_get_rx_ring_size(htt);
776

777 778
	vaddr_ring = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_KERNEL);
	if (!vaddr_ring)
779 780
		goto err_dma_ring;

781
	ath10k_htt_config_paddrs_ring(htt, vaddr_ring);
782 783 784 785
	htt->rx_ring.base_paddr = paddr;

	vaddr = dma_alloc_coherent(htt->ar->dev,
				   sizeof(*htt->rx_ring.alloc_idx.vaddr),
786
				   &paddr, GFP_KERNEL);
787 788 789 790 791
	if (!vaddr)
		goto err_dma_idx;

	htt->rx_ring.alloc_idx.vaddr = vaddr;
	htt->rx_ring.alloc_idx.paddr = paddr;
792
	htt->rx_ring.sw_rd_idx.msdu_payld = htt->rx_ring.size_mask;
793 794 795
	*htt->rx_ring.alloc_idx.vaddr = 0;

	/* Initialize the Rx refill retry timer */
796
	timer_setup(timer, ath10k_htt_rx_ring_refill_retry, 0);
797 798 799 800

	spin_lock_init(&htt->rx_ring.lock);

	htt->rx_ring.fill_cnt = 0;
801 802
	htt->rx_ring.sw_rd_idx.msdu_payld = 0;
	hash_init(htt->rx_ring.skb_table);
803

804
	skb_queue_head_init(&htt->rx_msdus_q);
805
	skb_queue_head_init(&htt->rx_in_ord_compl_q);
806
	skb_queue_head_init(&htt->tx_fetch_ind_q);
807
	atomic_set(&htt->num_mpdus_ready, 0);
808

809
	ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
810 811 812 813 814
		   htt->rx_ring.size, htt->rx_ring.fill_level);
	return 0;

err_dma_idx:
	dma_free_coherent(htt->ar->dev,
815
			  ath10k_htt_get_rx_ring_size(htt),
816
			  vaddr_ring,
817 818 819 820 821 822 823
			  htt->rx_ring.base_paddr);
err_dma_ring:
	kfree(htt->rx_ring.netbufs_ring);
err_netbuf:
	return -ENOMEM;
}

824 825
static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
					  enum htt_rx_mpdu_encrypt_type type)
826 827
{
	switch (type) {
828 829
	case HTT_RX_MPDU_ENCRYPT_NONE:
		return 0;
830 831
	case HTT_RX_MPDU_ENCRYPT_WEP40:
	case HTT_RX_MPDU_ENCRYPT_WEP104:
832
		return IEEE80211_WEP_IV_LEN;
833 834
	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
835
		return IEEE80211_TKIP_IV_LEN;
836
	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
837
		return IEEE80211_CCMP_HDR_LEN;
838 839 840 841 842
	case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
		return IEEE80211_CCMP_256_HDR_LEN;
	case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
	case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
		return IEEE80211_GCMP_HDR_LEN;
843 844 845
	case HTT_RX_MPDU_ENCRYPT_WEP128:
	case HTT_RX_MPDU_ENCRYPT_WAPI:
		break;
846 847
	}

848
	ath10k_warn(ar, "unsupported encryption type %d\n", type);
849 850 851
	return 0;
}

852 853
#define MICHAEL_MIC_LEN 8

854 855
static int ath10k_htt_rx_crypto_mic_len(struct ath10k *ar,
					enum htt_rx_mpdu_encrypt_type type)
856 857 858 859 860 861 862
{
	switch (type) {
	case HTT_RX_MPDU_ENCRYPT_NONE:
	case HTT_RX_MPDU_ENCRYPT_WEP40:
	case HTT_RX_MPDU_ENCRYPT_WEP104:
	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
863
		return 0;
864
	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
865
		return IEEE80211_CCMP_MIC_LEN;
866 867 868 869 870
	case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
		return IEEE80211_CCMP_256_MIC_LEN;
	case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
	case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
		return IEEE80211_GCMP_MIC_LEN;
871 872 873
	case HTT_RX_MPDU_ENCRYPT_WEP128:
	case HTT_RX_MPDU_ENCRYPT_WAPI:
		break;
874 875
	}

876
	ath10k_warn(ar, "unsupported encryption type %d\n", type);
877 878 879
	return 0;
}

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
static int ath10k_htt_rx_crypto_icv_len(struct ath10k *ar,
					enum htt_rx_mpdu_encrypt_type type)
{
	switch (type) {
	case HTT_RX_MPDU_ENCRYPT_NONE:
	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
	case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
	case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
	case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
		return 0;
	case HTT_RX_MPDU_ENCRYPT_WEP40:
	case HTT_RX_MPDU_ENCRYPT_WEP104:
		return IEEE80211_WEP_ICV_LEN;
	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
		return IEEE80211_TKIP_ICV_LEN;
	case HTT_RX_MPDU_ENCRYPT_WEP128:
	case HTT_RX_MPDU_ENCRYPT_WAPI:
		break;
	}

	ath10k_warn(ar, "unsupported encryption type %d\n", type);
	return 0;
}

905 906 907 908 909 910
struct amsdu_subframe_hdr {
	u8 dst[ETH_ALEN];
	u8 src[ETH_ALEN];
	__be16 len;
} __packed;

911 912
#define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
static inline u8 ath10k_bw_to_mac80211_bw(u8 bw)
{
	u8 ret = 0;

	switch (bw) {
	case 0:
		ret = RATE_INFO_BW_20;
		break;
	case 1:
		ret = RATE_INFO_BW_40;
		break;
	case 2:
		ret = RATE_INFO_BW_80;
		break;
	case 3:
		ret = RATE_INFO_BW_160;
		break;
	}

	return ret;
}

935
static void ath10k_htt_rx_h_rates(struct ath10k *ar,
936 937
				  struct ieee80211_rx_status *status,
				  struct htt_rx_desc *rxd)
938
{
939 940
	struct ieee80211_supported_band *sband;
	u8 cck, rate, bw, sgi, mcs, nss;
941
	u8 preamble = 0;
942
	u8 group_id;
943
	u32 info1, info2, info3;
944

945 946 947 948 949
	info1 = __le32_to_cpu(rxd->ppdu_start.info1);
	info2 = __le32_to_cpu(rxd->ppdu_start.info2);
	info3 = __le32_to_cpu(rxd->ppdu_start.info3);

	preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
950 951 952

	switch (preamble) {
	case HTT_RX_LEGACY:
953 954 955 956 957 958
		/* To get legacy rate index band is required. Since band can't
		 * be undefined check if freq is non-zero.
		 */
		if (!status->freq)
			return;

959 960
		cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
		rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
961
		rate &= ~RX_PPDU_START_RATE_FLAG;
962

963
		sband = &ar->mac.sbands[status->band];
964
		status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate, cck);
965 966 967
		break;
	case HTT_RX_HT:
	case HTT_RX_HT_WITH_TXBF:
968 969
		/* HT-SIG - Table 20-11 in info2 and info3 */
		mcs = info2 & 0x1F;
970
		nss = mcs >> 3;
971 972
		bw = (info2 >> 7) & 1;
		sgi = (info3 >> 7) & 1;
973 974

		status->rate_idx = mcs;
975
		status->encoding = RX_ENC_HT;
976
		if (sgi)
977
			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
978
		if (bw)
979
			status->bw = RATE_INFO_BW_40;
980 981 982
		break;
	case HTT_RX_VHT:
	case HTT_RX_VHT_WITH_TXBF:
983
		/* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
984 985
		 * TODO check this
		 */
986 987
		bw = info2 & 3;
		sgi = info3 & 1;
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
		group_id = (info2 >> 4) & 0x3F;

		if (GROUP_ID_IS_SU_MIMO(group_id)) {
			mcs = (info3 >> 4) & 0x0F;
			nss = ((info2 >> 10) & 0x07) + 1;
		} else {
			/* Hardware doesn't decode VHT-SIG-B into Rx descriptor
			 * so it's impossible to decode MCS. Also since
			 * firmware consumes Group Id Management frames host
			 * has no knowledge regarding group/user position
			 * mapping so it's impossible to pick the correct Nsts
			 * from VHT-SIG-A1.
			 *
			 * Bandwidth and SGI are valid so report the rateinfo
			 * on best-effort basis.
			 */
			mcs = 0;
			nss = 1;
		}
1007

1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
		if (mcs > 0x09) {
			ath10k_warn(ar, "invalid MCS received %u\n", mcs);
			ath10k_warn(ar, "rxd %08x mpdu start %08x %08x msdu start %08x %08x ppdu start %08x %08x %08x %08x %08x\n",
				    __le32_to_cpu(rxd->attention.flags),
				    __le32_to_cpu(rxd->mpdu_start.info0),
				    __le32_to_cpu(rxd->mpdu_start.info1),
				    __le32_to_cpu(rxd->msdu_start.common.info0),
				    __le32_to_cpu(rxd->msdu_start.common.info1),
				    rxd->ppdu_start.info0,
				    __le32_to_cpu(rxd->ppdu_start.info1),
				    __le32_to_cpu(rxd->ppdu_start.info2),
				    __le32_to_cpu(rxd->ppdu_start.info3),
				    __le32_to_cpu(rxd->ppdu_start.info4));

			ath10k_warn(ar, "msdu end %08x mpdu end %08x\n",
				    __le32_to_cpu(rxd->msdu_end.common.info0),
				    __le32_to_cpu(rxd->mpdu_end.info0));

			ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL,
					"rx desc msdu payload: ",
					rxd->msdu_payload, 50);
		}

1031
		status->rate_idx = mcs;
1032
		status->nss = nss;
1033 1034

		if (sgi)
1035
			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1036

1037
		status->bw = ath10k_bw_to_mac80211_bw(bw);
1038
		status->encoding = RX_ENC_VHT;
1039 1040 1041 1042 1043 1044
		break;
	default:
		break;
	}
}

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
static struct ieee80211_channel *
ath10k_htt_rx_h_peer_channel(struct ath10k *ar, struct htt_rx_desc *rxd)
{
	struct ath10k_peer *peer;
	struct ath10k_vif *arvif;
	struct cfg80211_chan_def def;
	u16 peer_id;

	lockdep_assert_held(&ar->data_lock);

	if (!rxd)
		return NULL;

	if (rxd->attention.flags &
	    __cpu_to_le32(RX_ATTENTION_FLAGS_PEER_IDX_INVALID))
		return NULL;

1062
	if (!(rxd->msdu_end.common.info0 &
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU)))
		return NULL;

	peer_id = MS(__le32_to_cpu(rxd->mpdu_start.info0),
		     RX_MPDU_START_INFO0_PEER_IDX);

	peer = ath10k_peer_find_by_id(ar, peer_id);
	if (!peer)
		return NULL;

	arvif = ath10k_get_arvif(ar, peer->vdev_id);
	if (WARN_ON_ONCE(!arvif))
		return NULL;

1077
	if (ath10k_mac_vif_chan(arvif->vif, &def))
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
		return NULL;

	return def.chan;
}

static struct ieee80211_channel *
ath10k_htt_rx_h_vdev_channel(struct ath10k *ar, u32 vdev_id)
{
	struct ath10k_vif *arvif;
	struct cfg80211_chan_def def;

	lockdep_assert_held(&ar->data_lock);

	list_for_each_entry(arvif, &ar->arvifs, list) {
		if (arvif->vdev_id == vdev_id &&
		    ath10k_mac_vif_chan(arvif->vif, &def) == 0)
			return def.chan;
	}

	return NULL;
}

static void
ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw *hw,
			      struct ieee80211_chanctx_conf *conf,
			      void *data)
{
	struct cfg80211_chan_def *def = data;

	*def = conf->def;
}

static struct ieee80211_channel *
ath10k_htt_rx_h_any_channel(struct ath10k *ar)
{
	struct cfg80211_chan_def def = {};

	ieee80211_iter_chan_contexts_atomic(ar->hw,
					    ath10k_htt_rx_h_any_chan_iter,
					    &def);

	return def.chan;
}

1122
static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
1123 1124 1125
				    struct ieee80211_rx_status *status,
				    struct htt_rx_desc *rxd,
				    u32 vdev_id)
1126 1127 1128 1129 1130 1131 1132
{
	struct ieee80211_channel *ch;

	spin_lock_bh(&ar->data_lock);
	ch = ar->scan_channel;
	if (!ch)
		ch = ar->rx_channel;
1133 1134 1135 1136 1137 1138
	if (!ch)
		ch = ath10k_htt_rx_h_peer_channel(ar, rxd);
	if (!ch)
		ch = ath10k_htt_rx_h_vdev_channel(ar, vdev_id);
	if (!ch)
		ch = ath10k_htt_rx_h_any_channel(ar);
1139 1140
	if (!ch)
		ch = ar->tgt_oper_chan;
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
	spin_unlock_bh(&ar->data_lock);

	if (!ch)
		return false;

	status->band = ch->band;
	status->freq = ch->center_freq;

	return true;
}

1152 1153 1154 1155
static void ath10k_htt_rx_h_signal(struct ath10k *ar,
				   struct ieee80211_rx_status *status,
				   struct htt_rx_desc *rxd)
{
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
	int i;

	for (i = 0; i < IEEE80211_MAX_CHAINS ; i++) {
		status->chains &= ~BIT(i);

		if (rxd->ppdu_start.rssi_chains[i].pri20_mhz != 0x80) {
			status->chain_signal[i] = ATH10K_DEFAULT_NOISE_FLOOR +
				rxd->ppdu_start.rssi_chains[i].pri20_mhz;

			status->chains |= BIT(i);
		}
	}

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	/* FIXME: Get real NF */
	status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
			 rxd->ppdu_start.rssi_comb;
	status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
}

static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
				    struct ieee80211_rx_status *status,
				    struct htt_rx_desc *rxd)
{
	/* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
	 * means all prior MSDUs in a PPDU are reported to mac80211 without the
	 * TSF. Is it worth holding frames until end of PPDU is known?
	 *
	 * FIXME: Can we get/compute 64bit TSF?
	 */
1185
	status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
1186 1187 1188 1189 1190
	status->flag |= RX_FLAG_MACTIME_END;
}

static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
				 struct sk_buff_head *amsdu,
1191 1192
				 struct ieee80211_rx_status *status,
				 u32 vdev_id)
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
{
	struct sk_buff *first;
	struct htt_rx_desc *rxd;
	bool is_first_ppdu;
	bool is_last_ppdu;

	if (skb_queue_empty(amsdu))
		return;

	first = skb_peek(amsdu);
	rxd = (void *)first->data - sizeof(*rxd);

	is_first_ppdu = !!(rxd->attention.flags &
			   __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
	is_last_ppdu = !!(rxd->attention.flags &
			  __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));

	if (is_first_ppdu) {
		/* New PPDU starts so clear out the old per-PPDU status. */
		status->freq = 0;
		status->rate_idx = 0;
1214
		status->nss = 0;
1215 1216
		status->encoding = RX_ENC_LEGACY;
		status->bw = RATE_INFO_BW_20;
1217

1218
		status->flag &= ~RX_FLAG_MACTIME_END;
1219 1220
		status->flag |= RX_FLAG_NO_SIGNAL_VAL;

1221 1222 1223 1224
		status->flag &= ~(RX_FLAG_AMPDU_IS_LAST);
		status->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
		status->ampdu_reference = ar->ampdu_reference;

1225
		ath10k_htt_rx_h_signal(ar, status, rxd);
1226
		ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
1227 1228 1229
		ath10k_htt_rx_h_rates(ar, status, rxd);
	}

1230
	if (is_last_ppdu) {
1231
		ath10k_htt_rx_h_mactime(ar, status, rxd);
1232 1233 1234 1235 1236

		/* set ampdu last segment flag */
		status->flag |= RX_FLAG_AMPDU_IS_LAST;
		ar->ampdu_reference++;
	}
1237 1238
}

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
static const char * const tid_to_ac[] = {
	"BE",
	"BK",
	"BK",
	"BE",
	"VI",
	"VI",
	"VO",
	"VO",
};

static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
{
	u8 *qc;
	int tid;

	if (!ieee80211_is_data_qos(hdr->frame_control))
		return "";

	qc = ieee80211_get_qos_ctl(hdr);
	tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
	if (tid < 8)
		snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
	else
		snprintf(out, size, "tid %d", tid);

	return out;
}

1268 1269 1270 1271 1272 1273 1274 1275 1276
static void ath10k_htt_rx_h_queue_msdu(struct ath10k *ar,
				       struct ieee80211_rx_status *rx_status,
				       struct sk_buff *skb)
{
	struct ieee80211_rx_status *status;

	status = IEEE80211_SKB_RXCB(skb);
	*status = *rx_status;

1277
	skb_queue_tail(&ar->htt.rx_msdus_q, skb);
1278 1279 1280
}

static void ath10k_process_rx(struct ath10k *ar, struct sk_buff *skb)
1281 1282
{
	struct ieee80211_rx_status *status;
1283 1284
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
	char tid[32];
1285

1286
	status = IEEE80211_SKB_RXCB(skb);
1287

1288 1289 1290 1291 1292 1293 1294
	if (!(ar->filter_flags & FIF_FCSFAIL) &&
	    status->flag & RX_FLAG_FAILED_FCS_CRC) {
		ar->stats.rx_crc_err_drop++;
		dev_kfree_skb_any(skb);
		return;
	}

1295
	ath10k_dbg(ar, ATH10K_DBG_DATA,
1296
		   "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",
1297 1298
		   skb,
		   skb->len,
1299 1300 1301 1302 1303
		   ieee80211_get_SA(hdr),
		   ath10k_get_tid(hdr, tid, sizeof(tid)),
		   is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
							"mcast" : "ucast",
		   (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
1304 1305 1306 1307 1308 1309
		   (status->encoding == RX_ENC_LEGACY) ? "legacy" : "",
		   (status->encoding == RX_ENC_HT) ? "ht" : "",
		   (status->encoding == RX_ENC_VHT) ? "vht" : "",
		   (status->bw == RATE_INFO_BW_40) ? "40" : "",
		   (status->bw == RATE_INFO_BW_80) ? "80" : "",
		   (status->bw == RATE_INFO_BW_160) ? "160" : "",
1310
		   status->enc_flags & RX_ENC_FLAG_SHORT_GI ? "sgi " : "",
1311
		   status->rate_idx,
1312
		   status->nss,
1313
		   status->freq,
1314
		   status->band, status->flag,
1315
		   !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
1316 1317
		   !!(status->flag & RX_FLAG_MMIC_ERROR),
		   !!(status->flag & RX_FLAG_AMSDU_MORE));
1318
	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
1319
			skb->data, skb->len);
1320 1321
	trace_ath10k_rx_hdr(ar, skb->data, skb->len);
	trace_ath10k_rx_payload(ar, skb->data, skb->len);
1322

1323
	ieee80211_rx_napi(ar->hw, NULL, skb, &ar->napi);
1324 1325
}

1326 1327
static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
				      struct ieee80211_hdr *hdr)
Michal Kazior's avatar
Michal Kazior committed
1328
{
1329 1330 1331
	int len = ieee80211_hdrlen(hdr->frame_control);

	if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
1332
		      ar->running_fw->fw_file.fw_features))
1333 1334 1335
		len = round_up(len, 4);

	return len;
Michal Kazior's avatar
Michal Kazior committed
1336 1337
}

Michal Kazior's avatar
Michal Kazior committed
1338 1339 1340 1341
static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
					struct sk_buff *msdu,
					struct ieee80211_rx_status *status,
					enum htt_rx_mpdu_encrypt_type enctype,
1342 1343
					bool is_decrypted,
					const u8 first_hdr[64])
1344
{
Michal Kazior's avatar
Michal Kazior committed
1345
	struct ieee80211_hdr *hdr;
1346
	struct htt_rx_desc *rxd;
Michal Kazior's avatar
Michal Kazior committed
1347 1348 1349 1350
	size_t hdr_len;
	size_t crypto_len;
	bool is_first;
	bool is_last;
1351 1352 1353
	bool msdu_limit_err;
	int bytes_aligned = ar->hw_params.decap_align_bytes;
	u8 *qos;
Michal Kazior's avatar
Michal Kazior committed
1354 1355

	rxd = (void *)msdu->data - sizeof(*rxd);
1356
	is_first = !!(rxd->msdu_end.common.info0 &
Michal Kazior's avatar
Michal Kazior committed
1357
		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
1358
	is_last = !!(rxd->msdu_end.common.info0 &
Michal Kazior's avatar
Michal Kazior committed
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));

	/* Delivered decapped frame:
	 * [802.11 header]
	 * [crypto param] <-- can be trimmed if !fcs_err &&
	 *                    !decrypt_err && !peer_idx_invalid
	 * [amsdu header] <-- only if A-MSDU
	 * [rfc1042/llc]
	 * [payload]
	 * [FCS] <-- at end, needs to be trimmed
	 */

1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	/* Some hardwares(QCA99x0 variants) limit number of msdus in a-msdu when
	 * deaggregate, so that unwanted MSDU-deaggregation is avoided for
	 * error packets. If limit exceeds, hw sends all remaining MSDUs as
	 * a single last MSDU with this msdu limit error set.
	 */
	msdu_limit_err = ath10k_rx_desc_msdu_limit_error(&ar->hw_params, rxd);

	/* If MSDU limit error happens, then don't warn on, the partial raw MSDU
	 * without first MSDU is expected in that case, and handled later here.
	 */
Michal Kazior's avatar
Michal Kazior committed
1381
	/* This probably shouldn't happen but warn just in case */
1382
	if (WARN_ON_ONCE(!is_first && !msdu_limit_err))
Michal Kazior's avatar
Michal Kazior committed
1383 1384 1385
		return;

	/* This probably shouldn't happen but warn just in case */
1386
	if (WARN_ON_ONCE(!(is_first && is_last) && !msdu_limit_err))
Michal Kazior's avatar
Michal Kazior committed
1387 1388 1389 1390
		return;

	skb_trim(msdu, msdu->len - FCS_LEN);

1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
	/* Push original 80211 header */
	if (unlikely(msdu_limit_err)) {
		hdr = (struct ieee80211_hdr *)first_hdr;
		hdr_len = ieee80211_hdrlen(hdr->frame_control);
		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);

		if (ieee80211_is_data_qos(hdr->frame_control)) {
			qos = ieee80211_get_qos_ctl(hdr);
			qos[0] |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
		}

		if (crypto_len)
			memcpy(skb_push(msdu, crypto_len),
			       (void *)hdr + round_up(hdr_len, bytes_aligned),
			       crypto_len);

		memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
	}

Michal Kazior's avatar
Michal Kazior committed
1410
	/* In most cases this will be true for sniffed frames. It makes sense
1411 1412
	 * to deliver them as-is without stripping the crypto param. This is
	 * necessary for software based decryption.
Michal Kazior's avatar
Michal Kazior committed
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
	 *
	 * If there's no error then the frame is decrypted. At least that is
	 * the case for frames that come in via fragmented rx indication.
	 */
	if (!is_decrypted)
		return;

	/* The payload is decrypted so strip crypto params. Start from tail
	 * since hdr is used to compute some stuff.
	 */

	hdr = (void *)msdu->data;

	/* Tail */
1427
	if (status->flag & RX_FLAG_IV_STRIPPED) {
1428
		skb_trim(msdu, msdu->len -
1429 1430 1431 1432
			 ath10k_htt_rx_crypto_mic_len(ar, enctype));

		skb_trim(msdu, msdu->len -
			 ath10k_htt_rx_crypto_icv_len(ar, enctype));
1433 1434
	} else {
		/* MIC */
1435 1436 1437
		if (status->flag & RX_FLAG_MIC_STRIPPED)
			skb_trim(msdu, msdu->len -
				 ath10k_htt_rx_crypto_mic_len(ar, enctype));
1438 1439

		/* ICV */
1440
		if (status->flag & RX_FLAG_ICV_STRIPPED)
1441
			skb_trim(msdu, msdu->len -
1442
				 ath10k_htt_rx_crypto_icv_len(ar, enctype));
1443
	}
Michal Kazior's avatar
Michal Kazior committed
1444 1445

	/* MMIC */
1446 1447
	if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
	    !ieee80211_has_morefrags(hdr->frame_control) &&
Michal Kazior's avatar
Michal Kazior committed
1448
	    enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
1449
		skb_trim(msdu, msdu->len - MICHAEL_MIC_LEN);
Michal Kazior's avatar
Michal Kazior committed
1450 1451

	/* Head */
1452 1453 1454
	if (status->flag & RX_FLAG_IV_STRIPPED) {
		hdr_len = ieee80211_hdrlen(hdr->frame_control);
		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
Michal Kazior's avatar
Michal Kazior committed
1455

1456 1457 1458 1459
		memmove((void *)msdu->data + crypto_len,
			(void *)msdu->data, hdr_len);
		skb_pull(msdu, crypto_len);
	}
Michal Kazior's avatar
Michal Kazior committed
1460 1461 1462 1463 1464
}

static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
					  struct sk_buff *msdu,
					  struct ieee80211_rx_status *status,
1465 1466
					  const u8 first_hdr[64],
					  enum htt_rx_mpdu_encrypt_type enctype)
Michal Kazior's avatar
Michal Kazior committed
1467
{
1468
	struct ieee80211_hdr *hdr;
1469
	struct htt_rx_desc *rxd;
Michal Kazior's avatar
Michal Kazior committed
1470 1471 1472
	size_t hdr_len;
	u8 da[ETH_ALEN];
	u8 sa[ETH_ALEN];
1473
	int l3_pad_bytes;
1474
	int bytes_aligned = ar->hw_params.decap_align_bytes;
1475

Michal Kazior's avatar
Michal Kazior committed
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
	/* Delivered decapped frame:
	 * [nwifi 802.11 header] <-- replaced with 802.11 hdr
	 * [rfc1042/llc]
	 *
	 * Note: The nwifi header doesn't have QoS Control and is
	 * (always?) a 3addr frame.
	 *
	 * Note2: There's no A-MSDU subframe header. Even if it's part
	 * of an A-MSDU.
	 */
1486

Michal Kazior's avatar
Michal Kazior committed
1487
	/* pull decapped header and copy SA & DA */
1488 1489 1490 1491 1492 1493
	rxd = (void *)msdu->data - sizeof(*rxd);

	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
	skb_put(msdu, l3_pad_bytes);

	hdr = (struct ieee80211_hdr *)(msdu->data + l3_pad_bytes);
1494

1495
	hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
Michal Kazior's avatar
Michal Kazior committed
1496 1497 1498
	ether_addr_copy(da, ieee80211_get_DA(hdr));
	ether_addr_copy(sa, ieee80211_get_SA(hdr));
	skb_pull(msdu, hdr_len);
1499

Michal Kazior's avatar
Michal Kazior committed
1500 1501
	/* push original 802.11 header */
	hdr = (struct ieee80211_hdr *)first_hdr;
1502
	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1503 1504 1505 1506 1507 1508 1509 1510

	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
		memcpy(skb_push(msdu,
				ath10k_htt_rx_crypto_param_len(ar, enctype)),
		       (void *)hdr + round_up(hdr_len, bytes_aligned),
			ath10k_htt_rx_crypto_param_len(ar, enctype));
	}

Michal Kazior's avatar
Michal Kazior committed
1511
	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1512

Michal Kazior's avatar
Michal Kazior committed
1513 1514 1515 1516 1517 1518 1519
	/* original 802.11 header has a different DA and in
	 * case of 4addr it may also have different SA
	 */
	hdr = (struct ieee80211_hdr *)msdu->data;
	ether_addr_copy(ieee80211_get_DA(hdr), da);
	ether_addr_copy(ieee80211_get_SA(hdr), sa);
}
1520

Michal Kazior's avatar
Michal Kazior committed
1521 1522 1523 1524 1525 1526 1527 1528 1529
static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
					  struct sk_buff *msdu,
					  enum htt_rx_mpdu_encrypt_type enctype)
{
	struct ieee80211_hdr *hdr;
	struct htt_rx_desc *rxd;
	size_t hdr_len, crypto_len;
	void *rfc1042;
	bool is_first, is_last, is_amsdu;
1530
	int bytes_aligned = ar->hw_params.decap_align_bytes;
1531

Michal Kazior's avatar
Michal Kazior committed
1532 1533
	rxd = (void *)msdu->data - sizeof(*rxd);
	hdr = (void *)rxd->rx_hdr_status;
1534

1535
	is_first = !!(rxd->msdu_end.common.info0 &
Michal Kazior's avatar
Michal Kazior committed
1536
		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
1537
	is_last = !!(rxd->msdu_end.common.info0 &
Michal Kazior's avatar
Michal Kazior committed
1538 1539
		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
	is_amsdu = !(is_first && is_last);
1540

Michal Kazior's avatar
Michal Kazior committed
1541
	rfc1042 = hdr;
1542

Michal Kazior's avatar
Michal Kazior committed
1543 1544 1545
	if (is_first) {
		hdr_len = ieee80211_hdrlen(hdr->frame_control);
		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1546

1547 1548
		rfc1042 += round_up(hdr_len, bytes_aligned) +
			   round_up(crypto_len, bytes_aligned);
1549
	}
1550

Michal Kazior's avatar
Michal Kazior committed
1551 1552 1553 1554
	if (is_amsdu)
		rfc1042 += sizeof(struct amsdu_subframe_hdr);

	return rfc1042;
1555 1556
}

Michal Kazior's avatar
Michal Kazior committed
1557 1558 1559 1560 1561
static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
					struct sk_buff *msdu,
					struct ieee80211_rx_status *status,
					const u8 first_hdr[64],
					enum htt_rx_mpdu_encrypt_type enctype)
1562 1563
{
	struct ieee80211_hdr *hdr;
Michal Kazior's avatar
Michal Kazior committed
1564 1565
	struct ethhdr *eth;
	size_t hdr_len;
1566
	void *rfc1042;
Michal Kazior's avatar
Michal Kazior committed
1567 1568
	u8 da[ETH_ALEN];
	u8 sa[ETH_ALEN];
1569 1570
	int l3_pad_bytes;
	struct htt_rx_desc *rxd;
1571
	int bytes_aligned = ar->hw_params.decap_align_bytes;
1572

Michal Kazior's avatar
Michal Kazior committed
1573 1574 1575 1576 1577 1578 1579 1580 1581
	/* Delivered decapped frame:
	 * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
	 * [payload]
	 */

	rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
	if (WARN_ON_ONCE(!rfc1042))
		return;

1582 1583 1584 1585 1586
	rxd = (void *)msdu->data - sizeof(*rxd);
	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
	skb_put(msdu, l3_pad_bytes);
	skb_pull(msdu, l3_pad_bytes);

Michal Kazior's avatar
Michal Kazior committed
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
	/* pull decapped header and copy SA & DA */
	eth = (struct ethhdr *)msdu->data;
	ether_addr_copy(da, eth->h_dest);
	ether_addr_copy(sa, eth->h_source);
	skb_pull(msdu, sizeof(struct ethhdr));

	/* push rfc1042/llc/snap */
	memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
	       sizeof(struct rfc1042_hdr));

	/* push original 802.11 header */
	hdr = (struct ieee80211_hdr *)first_hdr;
	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1600 1601 1602 1603 1604 1605 1606 1607

	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
		memcpy(skb_push(msdu,
				ath10k_htt_rx_crypto_param_len(ar, enctype)),
		       (void *)hdr + round_up(hdr_len, bytes_aligned),
			ath10k_htt_rx_crypto_param_len(ar, enctype));
	}

Michal Kazior's avatar
Michal Kazior committed
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);

	/* original 802.11 header has a different DA and in
	 * case of 4addr it may also have different SA
	 */
	hdr = (struct ieee80211_hdr *)msdu->data;
	ether_addr_copy(ieee80211_get_DA(hdr), da);
	ether_addr_copy(ieee80211_get_SA(hdr), sa);
}

static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
					 struct sk_buff *msdu,
					 struct ieee80211_rx_status *status,
1621 1622
					 const u8 first_hdr[64],
					 enum htt_rx_mpdu_encrypt_type enctype)
Michal Kazior's avatar
Michal Kazior committed
1623 1624 1625
{
	struct ieee80211_hdr *hdr;
	size_t hdr_len;
1626 1627
	int l3_pad_bytes;
	struct htt_rx_desc *rxd;
1628
	int bytes_aligned = ar->hw_params.decap_align_bytes;
Michal Kazior's avatar
Michal Kazior committed
1629 1630 1631 1632 1633 1634 1635

	/* Delivered decapped frame:
	 * [amsdu header] <-- replaced with 802.11 hdr
	 * [rfc1042/llc]
	 * [payload]
	 */

1636 1637 1638 1639 1640
	rxd = (void *)msdu->data - sizeof(*rxd);
	l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);

	skb_put(msdu, l3_pad_bytes);
	skb_pull(msdu, sizeof(struct amsdu_subframe_hdr) + l3_pad_bytes);
Michal Kazior's avatar
Michal Kazior committed
1641 1642

	hdr = (struct ieee80211_hdr *)first_hdr;
1643
	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1644 1645 1646 1647 1648 1649 1650 1651

	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
		memcpy(skb_push(msdu,
				ath10k_htt_rx_crypto_param_len(ar, enctype)),
		       (void *)hdr + round_up(hdr_len, bytes_aligned),
			ath10k_htt_rx_crypto_param_len(ar, enctype));
	}

Michal Kazior's avatar
Michal Kazior committed
1652 1653
	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
}
1654

Michal Kazior's avatar
Michal Kazior committed
1655 1656 1657 1658 1659 1660 1661 1662 1663
static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
				    struct sk_buff *msdu,
				    struct ieee80211_rx_status *status,
				    u8 first_hdr[64],
				    enum htt_rx_mpdu_encrypt_type enctype,
				    bool is_decrypted)
{
	struct htt_rx_desc *rxd;
	enum rx_msdu_decap_format decap;
1664

Michal Kazior's avatar
Michal Kazior committed
1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
	/* First msdu's decapped header:
	 * [802.11 header] <-- padded to 4 bytes long
	 * [crypto param] <-- padded to 4 bytes long
	 * [amsdu header] <-- only if A-MSDU
	 * [rfc1042/llc]
	 *
	 * Other (2nd, 3rd, ..) msdu's decapped header:
	 * [amsdu header] <-- only if A-MSDU
	 * [rfc1042/llc]
	 */

	rxd = (void *)msdu->data - sizeof(*rxd);
1677
	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
Michal Kazior's avatar
Michal Kazior committed
1678 1679 1680
		   RX_MSDU_START_INFO1_DECAP_FORMAT);

	switch (decap) {
1681
	case RX_MSDU_DECAP_RAW:
Michal Kazior's avatar
Michal Kazior committed
1682
		ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
1683
					    is_decrypted, first_hdr);
1684 1685
		break;
	case RX_MSDU_DECAP_NATIVE_WIFI:
1686 1687
		ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr,
					      enctype);
1688 1689
		break;
	case RX_MSDU_DECAP_ETHERNET2_DIX:
Michal Kazior's avatar
Michal Kazior committed
1690
		ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
1691 1692
		break;
	case RX_MSDU_DECAP_8023_SNAP_LLC:
1693 1694
		ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr,
					     enctype);
1695
		break;
1696 1697 1698
	}
}

1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
{
	struct htt_rx_desc *rxd;
	u32 flags, info;
	bool is_ip4, is_ip6;
	bool is_tcp, is_udp;
	bool ip_csum_ok, tcpudp_csum_ok;

	rxd = (void *)skb->data - sizeof(*rxd);
	flags = __le32_to_cpu(rxd->attention.flags);
1709
	info = __le32_to_cpu(rxd->msdu_start.common.info1);
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729

	is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
	is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
	is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
	is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
	ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
	tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);

	if (!is_ip4 && !is_ip6)
		return CHECKSUM_NONE;
	if (!is_tcp && !is_udp)
		return CHECKSUM_NONE;
	if (!ip_csum_ok)
		return CHECKSUM_NONE;
	if (!tcpudp_csum_ok)
		return CHECKSUM_NONE;

	return CHECKSUM_UNNECESSARY;
}

Michal Kazior's avatar
Michal Kazior committed
1730 1731 1732 1733 1734 1735 1736
static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
{
	msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
}

static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
				 struct sk_buff_head *amsdu,
1737
				 struct ieee80211_rx_status *status,
1738 1739 1740
				 bool fill_crypt_header,
				 u8 *rx_hdr,
				 enum ath10k_pkt_rx_err *err)
Michal Kazior's avatar
Michal Kazior committed
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
{
	struct sk_buff *first;
	struct sk_buff *last;
	struct sk_buff *msdu;
	struct htt_rx_desc *rxd;
	struct ieee80211_hdr *hdr;
	enum htt_rx_mpdu_encrypt_type enctype;
	u8 first_hdr[64];
	u8 *qos;
	bool has_fcs_err;
	bool has_crypto_err;
	bool has_tkip_err;
	bool has_peer_idx_invalid;
	bool is_decrypted;
1755
	bool is_mgmt;
Michal Kazior's avatar
Michal Kazior committed
1756 1757 1758 1759 1760 1761 1762 1763
	u32 attention;

	if (skb_queue_empty(amsdu))
		return;

	first = skb_peek(amsdu);
	rxd = (void *)first->data - sizeof(*rxd);

1764 1765 1766
	is_mgmt = !!(rxd->attention.flags &
		     __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));

Michal Kazior's avatar
Michal Kazior committed
1767 1768 1769 1770 1771 1772 1773
	enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
		     RX_MPDU_START_INFO0_ENCRYPT_TYPE);

	/* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
	 * decapped header. It'll be used for undecapping of each MSDU.
	 */
	hdr = (void *)rxd->rx_hdr_status;
1774
	memcpy(first_hdr, hdr, RX_HTT_HDR_STATUS_LEN);
Michal Kazior's avatar
Michal Kazior committed
1775

1776 1777 1778
	if (rx_hdr)
		memcpy(rx_hdr, hdr, RX_HTT_HDR_STATUS_LEN);

Michal Kazior's avatar
Michal Kazior committed
1779 1780 1781 1782
	/* Each A-MSDU subframe will use the original header as the base and be
	 * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
	 */
	hdr = (void *)first_hdr;
1783 1784 1785 1786 1787

	if (ieee80211_is_data_qos(hdr->frame_control)) {
		qos = ieee80211_get_qos_ctl(hdr);
		qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
	}
Michal Kazior's avatar
Michal Kazior committed
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812

	/* Some attention flags are valid only in the last MSDU. */
	last = skb_peek_tail(amsdu);
	rxd = (void *)last->data - sizeof(*rxd);
	attention = __le32_to_cpu(rxd->attention.flags);

	has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
	has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
	has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
	has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);

	/* Note: If hardware captures an encrypted frame that it can't decrypt,
	 * e.g. due to fcs error, missing peer or invalid key data it will
	 * report the frame as raw.
	 */
	is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
			!has_fcs_err &&
			!has_crypto_err &&
			!has_peer_idx_invalid);

	/* Clear per-MPDU flags while leaving per-PPDU flags intact. */
	status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
			  RX_FLAG_MMIC_ERROR |
			  RX_FLAG_DECRYPTED |
			  RX_FLAG_IV_STRIPPED |
1813
			  RX_FLAG_ONLY_MONITOR |
Michal Kazior's avatar
Michal Kazior committed
1814 1815 1816 1817 1818 1819 1820 1821
			  RX_FLAG_MMIC_STRIPPED);

	if (has_fcs_err)
		status->flag |= RX_FLAG_FAILED_FCS_CRC;

	if (has_tkip_err)
		status->flag |= RX_FLAG_MMIC_ERROR;

1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
	if (err) {
		if (has_fcs_err)
			*err = ATH10K_PKT_RX_ERR_FCS;
		else if (has_tkip_err)
			*err = ATH10K_PKT_RX_ERR_TKIP;
		else if (has_crypto_err)
			*err = ATH10K_PKT_RX_ERR_CRYPT;
		else if (has_peer_idx_invalid)
			*err = ATH10K_PKT_RX_ERR_PEER_IDX_INVAL;
	}

1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
	/* Firmware reports all necessary management frames via WMI already.
	 * They are not reported to monitor interfaces at all so pass the ones
	 * coming via HTT to monitor interfaces instead. This simplifies
	 * matters a lot.
	 */
	if (is_mgmt)
		status->flag |= RX_FLAG_ONLY_MONITOR;

	if (is_decrypted) {
		status->flag |= RX_FLAG_DECRYPTED;

		if (likely(!is_mgmt))
1845 1846 1847 1848 1849 1850 1851 1852
			status->flag |= RX_FLAG_MMIC_STRIPPED;

		if (fill_crypt_header)
			status->flag |= RX_FLAG_MIC_STRIPPED |
					RX_FLAG_ICV_STRIPPED;
		else
			status->flag |= RX_FLAG_IV_STRIPPED;
	}
Michal Kazior's avatar
Michal Kazior committed
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864

	skb_queue_walk(amsdu, msdu) {
		ath10k_htt_rx_h_csum_offload(msdu);
		ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
					is_decrypted);

		/* Undecapping involves copying the original 802.11 header back
		 * to sk_buff. If frame is protected and hardware has decrypted
		 * it then remove the protected bit.
		 */
		if (!is_decrypted)
			continue;
1865 1866
		if (is_mgmt)
			continue;
Michal Kazior's avatar
Michal Kazior committed
1867

1868 1869 1870
		if (fill_crypt_header)
			continue;

Michal Kazior's avatar
Michal Kazior committed
1871 1872 1873 1874 1875
		hdr = (void *)msdu->data;
		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
	}
}

1876
static void ath10k_htt_rx_h_enqueue(struct ath10k *ar,
Michal Kazior's avatar
Michal Kazior committed
1877 1878 1879 1880
				    struct sk_buff_head *amsdu,
				    struct ieee80211_rx_status *status)
{
	struct sk_buff *msdu;
1881 1882 1883
	struct sk_buff *first_subframe;

	first_subframe = skb_peek(amsdu);
Michal Kazior's avatar
Michal Kazior committed
1884 1885 1886 1887 1888 1889 1890 1891

	while ((msdu = __skb_dequeue(amsdu))) {
		/* Setup per-MSDU flags */
		if (skb_queue_empty(amsdu))
			status->flag &= ~RX_FLAG_AMSDU_MORE;
		else
			status->flag |= RX_FLAG_AMSDU_MORE;

1892 1893 1894 1895 1896 1897 1898
		if (msdu == first_subframe) {
			first_subframe = NULL;
			status->flag &= ~RX_FLAG_ALLOW_SAME_PN;
		} else {
			status->flag |= RX_FLAG_ALLOW_SAME_PN;
		}

1899
		ath10k_htt_rx_h_queue_msdu(ar, status, msdu);
Michal Kazior's avatar
Michal Kazior committed
1900 1901 1902
	}
}

1903
static int ath10k_unchain_msdu(struct sk_buff_head *amsdu,
1904
			       unsigned long *unchain_cnt)
Ben Greear's avatar
Ben Greear committed
1905
{
1906
	struct sk_buff *skb, *first;
Ben Greear's avatar
Ben Greear committed
1907 1908
	int space;
	int total_len = 0;
1909
	int amsdu_len = skb_queue_len(amsdu);
Ben Greear's avatar
Ben Greear committed
1910 1911 1912 1913 1914 1915 1916 1917

	/* TODO:  Might could optimize this by using
	 * skb_try_coalesce or similar method to
	 * decrease copying, or maybe get mac80211 to
	 * provide a way to just receive a list of
	 * skb?
	 */

1918
	first = __skb_dequeue(amsdu);
Ben Greear's avatar
Ben Greear committed
1919 1920

	/* Allocate total length all at once. */
1921 1922
	skb_queue_walk(amsdu, skb)
		total_len += skb->len;
Ben Greear's avatar
Ben Greear committed
1923

1924
	space = total_len - skb_tailroom(first);
Ben Greear's avatar
Ben Greear committed
1925
	if ((space > 0) &&
1926
	    (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
Ben Greear's avatar
Ben Greear committed
1927 1928 1929 1930
		/* TODO:  bump some rx-oom error stat */
		/* put it back together so we can free the
		 * whole list at once.
		 */
1931
		__skb_queue_head(amsdu, first);
Ben Greear's avatar
Ben Greear committed
1932 1933 1934 1935 1936 1937
		return -1;
	}

	/* Walk list again, copying contents into
	 * msdu_head
	 */
1938 1939 1940 1941
	while ((skb = __skb_dequeue(amsdu))) {
		skb_copy_from_linear_data(skb, skb_put(first, skb->len),
					  skb->len);
		dev_kfree_skb_any(skb);
Ben Greear's avatar
Ben Greear committed
1942 1943
	}

1944
	__skb_queue_head(amsdu, first);
1945 1946 1947

	*unchain_cnt += amsdu_len - 1;

Ben Greear's avatar
Ben Greear committed
1948 1949 1950
	return 0;
}

Michal Kazior's avatar
Michal Kazior committed
1951
static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
1952
				    struct sk_buff_head *amsdu,
1953 1954
				    unsigned long *drop_cnt,
				    unsigned long *unchain_cnt)
1955
{
Michal Kazior's avatar
Michal Kazior committed
1956 1957 1958
	struct sk_buff *first;
	struct htt_rx_desc *rxd;
	enum rx_msdu_decap_format decap;
1959

Michal Kazior's avatar
Michal Kazior committed
1960 1961
	first = skb_peek(amsdu);
	rxd = (void *)first->data - sizeof(*rxd);
1962
	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
Michal Kazior's avatar
Michal Kazior committed
1963
		   RX_MSDU_START_INFO1_DECAP_FORMAT);
1964

Michal Kazior's avatar
Michal Kazior committed
1965 1966 1967 1968 1969 1970 1971
	/* FIXME: Current unchaining logic can only handle simple case of raw
	 * msdu chaining. If decapping is other than raw the chaining may be
	 * more complex and this isn't handled by the current code. Don't even
	 * try re-constructing such frames - it'll be pretty much garbage.
	 */
	if (decap != RX_MSDU_DECAP_RAW ||
	    skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
1972
		*drop_cnt += skb_queue_len(amsdu);
Michal Kazior's avatar
Michal Kazior committed
1973 1974
		__skb_queue_purge(amsdu);
		return;
1975 1976
	}

1977
	ath10k_unchain_msdu(amsdu, unchain_cnt);
Michal Kazior's avatar
Michal Kazior committed
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
}

static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
					struct sk_buff_head *amsdu,
					struct ieee80211_rx_status *rx_status)
{
	/* FIXME: It might be a good idea to do some fuzzy-testing to drop
	 * invalid/dangerous frames.
	 */

	if (!rx_status->freq) {
1989
		ath10k_dbg(ar, ATH10K_DBG_HTT, "no channel configured; ignoring frame(s)!\n");
1990 1991 1992
		return false;
	}

Michal Kazior's avatar
Michal Kazior committed
1993 1994
	if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
1995 1996 1997 1998 1999 2000
		return false;
	}

	return true;
}

Michal Kazior's avatar
Michal Kazior committed
2001 2002
static void ath10k_htt_rx_h_filter(struct ath10k *ar,
				   struct sk_buff_head *amsdu,
2003
				   struct ieee80211_rx_status *rx_status,
2004
				   unsigned long *drop_cnt)
Michal Kazior's avatar
Michal Kazior committed
2005 2006 2007 2008 2009 2010 2011
{
	if (skb_queue_empty(amsdu))
		return;

	if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
		return;

2012 2013 2014
	if (drop_cnt)
		*drop_cnt += skb_queue_len(amsdu);

Michal Kazior's avatar
Michal Kazior committed
2015 2016 2017
	__skb_queue_purge(amsdu);
}

2018
static int ath10k_htt_rx_handle_amsdu(struct ath10k_htt *htt)
2019
{
2020
	struct ath10k *ar = htt->ar;
2021
	struct ieee80211_rx_status *rx_status = &htt->rx_status;
2022
	struct sk_buff_head amsdu;
2023
	int ret;
2024 2025 2026 2027
	unsigned long drop_cnt = 0;
	unsigned long unchain_cnt = 0;
	unsigned long drop_cnt_filter = 0;
	unsigned long msdus_to_queue, num_msdus;
2028 2029
	enum ath10k_pkt_rx_err err = ATH10K_PKT_RX_ERR_MAX;
	u8 first_hdr[RX_HTT_HDR_STATUS_LEN];
2030

2031
	__skb_queue_head_init(&amsdu);
2032

2033 2034 2035 2036 2037 2038 2039
	spin_lock_bh(&htt->rx_ring.lock);
	if (htt->rx_confused) {
		spin_unlock_bh(&htt->rx_ring.lock);
		return -EIO;
	}
	ret = ath10k_htt_rx_amsdu_pop(htt, &amsdu);
	spin_unlock_bh(&htt->rx_ring.lock);
2040

2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
	if (ret < 0) {
		ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
		__skb_queue_purge(&amsdu);
		/* FIXME: It's probably a good idea to reboot the
		 * device instead of leaving it inoperable.
		 */
		htt->rx_confused = true;
		return ret;
	}

2051 2052
	num_msdus = skb_queue_len(&amsdu);

2053
	ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
2054 2055 2056

	/* only for ret = 1 indicates chained msdus */
	if (ret > 0)
2057
		ath10k_htt_rx_h_unchain(ar, &amsdu, &drop_cnt, &unchain_cnt);
2058

2059 2060 2061
	ath10k_htt_rx_h_filter(ar, &amsdu, rx_status, &drop_cnt_filter);
	ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status, true, first_hdr, &err);
	msdus_to_queue = skb_queue_len(&amsdu);
2062
	ath10k_htt_rx_h_enqueue(ar, &amsdu, rx_status);
2063

2064 2065 2066 2067
	ath10k_sta_update_rx_tid_stats(ar, first_hdr, num_msdus, err,
				       unchain_cnt, drop_cnt, drop_cnt_filter,
				       msdus_to_queue);

2068
	return 0;
2069 2070
}

2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
static void ath10k_htt_rx_mpdu_desc_pn_hl(struct htt_hl_rx_desc *rx_desc,
					  union htt_rx_pn_t *pn,
					  int pn_len_bits)
{
	switch (pn_len_bits) {
	case 48:
		pn->pn48 = __le32_to_cpu(rx_desc->pn_31_0) +
			   ((u64)(__le32_to_cpu(rx_desc->u0.pn_63_32) & 0xFFFF) << 32);
		break;
	case 24:
		pn->pn24 = __le32_to_cpu(rx_desc->pn_31_0);
		break;
2083
	}
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
}

static bool ath10k_htt_rx_pn_cmp48(union htt_rx_pn_t *new_pn,
				   union htt_rx_pn_t *old_pn)
{
	return ((new_pn->pn48 & 0xffffffffffffULL) <=
		(old_pn->pn48 & 0xffffffffffffULL));
}

static bool ath10k_htt_rx_pn_check_replay_hl(struct ath10k *ar,
					     struct ath10k_peer *peer,
					     struct htt_rx_indication_hl *rx)
{
	bool last_pn_valid, pn_invalid = false;
	enum htt_txrx_sec_cast_type sec_index;
	enum htt_security_types sec_type;
	union htt_rx_pn_t new_pn = {0};
	struct htt_hl_rx_desc *rx_desc;
	union htt_rx_pn_t *last_pn;
	u32 rx_desc_info, tid;
	int num_mpdu_ranges;

	lockdep_assert_held(&ar->data_lock);

	if (!peer)
		return false;

	if (!(rx->fw_desc.flags & FW_RX_DESC_FLAGS_FIRST_MSDU))
		return false;

	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);

	rx_desc = (struct htt_hl_rx_desc *)&rx->mpdu_ranges[num_mpdu_ranges];
	rx_desc_info = __le32_to_cpu(rx_desc->info);

	if (!MS(rx_desc_info, HTT_RX_DESC_HL_INFO_ENCRYPTED))
		return false;

	tid = MS(rx->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);
	last_pn_valid = peer->tids_last_pn_valid[tid];
	last_pn = &peer->tids_last_pn[tid];

	if (MS(rx_desc_info, HTT_RX_DESC_HL_INFO_MCAST_BCAST))
		sec_index = HTT_TXRX_SEC_MCAST;
	else
		sec_index = HTT_TXRX_SEC_UCAST;

	sec_type = peer->rx_pn[sec_index].sec_type;
	ath10k_htt_rx_mpdu_desc_pn_hl(rx_desc, &new_pn, peer->rx_pn[sec_index].pn_len);

	if (sec_type != HTT_SECURITY_AES_CCMP &&
	    sec_type != HTT_SECURITY_TKIP &&
	    sec_type != HTT_SECURITY_TKIP_NOMIC)
		return false;

	if (last_pn_valid)
		pn_invalid = ath10k_htt_rx_pn_cmp48(&new_pn, last_pn);
	else
2143
		peer->tids_last_pn_valid[tid] = true;
2144 2145 2146 2147 2148 2149 2150

	if (!pn_invalid)
		last_pn->pn48 = new_pn.pn48;

	return pn_invalid;
}

2151 2152
static bool ath10k_htt_rx_proc_rx_ind_hl(struct ath10k_htt *htt,
					 struct htt_rx_indication_hl *rx,
2153 2154 2155
					 struct sk_buff *skb,
					 enum htt_rx_pn_check_type check_pn_type,
					 enum htt_rx_tkip_demic_type tkip_mic_type)
2156 2157 2158 2159 2160
{
	struct ath10k *ar = htt->ar;
	struct ath10k_peer *peer;
	struct htt_rx_indication_mpdu_range *mpdu_ranges;
	struct fw_rx_desc_hl *fw_desc;
2161 2162 2163 2164
	enum htt_txrx_sec_cast_type sec_index;
	enum htt_security_types sec_type;
	union htt_rx_pn_t new_pn = {0};
	struct htt_hl_rx_desc *rx_desc;
2165 2166 2167 2168 2169 2170 2171
	struct ieee80211_hdr *hdr;
	struct ieee80211_rx_status *rx_status;
	u16 peer_id;
	u8 rx_desc_len;
	int num_mpdu_ranges;
	size_t tot_hdr_len;
	struct ieee80211_channel *ch;
2172 2173
	bool pn_invalid, qos, first_msdu;
	u32 tid, rx_desc_info;
2174 2175

	peer_id = __le16_to_cpu(rx->hdr.peer_id);
2176
	tid = MS(rx->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);
2177 2178 2179 2180

	spin_lock_bh(&ar->data_lock);
	peer = ath10k_peer_find_by_id(ar, peer_id);
	spin_unlock_bh(&ar->data_lock);
2181
	if (!peer && peer_id != HTT_INVALID_PEERID)
2182 2183
		ath10k_warn(ar, "Got RX ind from invalid peer: %u\n", peer_id);

2184 2185 2186
	if (!peer)
		return true;

2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202
	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
	mpdu_ranges = htt_rx_ind_get_mpdu_ranges_hl(rx);
	fw_desc = &rx->fw_desc;
	rx_desc_len = fw_desc->len;

	/* I have not yet seen any case where num_mpdu_ranges > 1.
	 * qcacld does not seem handle that case either, so we introduce the
	 * same limitiation here as well.
	 */
	if (num_mpdu_ranges > 1)
		ath10k_warn(ar,
			    "Unsupported number of MPDU ranges: %d, ignoring all but the first\n",
			    num_mpdu_ranges);

	if (mpdu_ranges->mpdu_range_status !=
2203 2204 2205
	    HTT_RX_IND_MPDU_STATUS_OK &&
	    mpdu_ranges->mpdu_range_status !=
	    HTT_RX_IND_MPDU_STATUS_TKIP_MIC_ERR) {
2206 2207
		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt mpdu_range_status %d\n",
			   mpdu_ranges->mpdu_range_status);
2208 2209 2210
		goto err;
	}

2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
	rx_desc = (struct htt_hl_rx_desc *)&rx->mpdu_ranges[num_mpdu_ranges];
	rx_desc_info = __le32_to_cpu(rx_desc->info);

	if (MS(rx_desc_info, HTT_RX_DESC_HL_INFO_MCAST_BCAST))
		sec_index = HTT_TXRX_SEC_MCAST;
	else
		sec_index = HTT_TXRX_SEC_UCAST;

	sec_type = peer->rx_pn[sec_index].sec_type;
	first_msdu = rx->fw_desc.flags & FW_RX_DESC_FLAGS_FIRST_MSDU;

	ath10k_htt_rx_mpdu_desc_pn_hl(rx_desc, &new_pn, peer->rx_pn[sec_index].pn_len);

	if (check_pn_type == HTT_RX_PN_CHECK && tid >= IEEE80211_NUM_TIDS) {
2225 2226 2227
		spin_lock_bh(&ar->data_lock);
		pn_invalid = ath10k_htt_rx_pn_check_replay_hl(ar, peer, rx);
		spin_unlock_bh(&ar->data_lock);
2228

2229 2230 2231
		if (pn_invalid)
			goto err;
	}
2232

2233 2234 2235 2236 2237 2238 2239
	/* Strip off all headers before the MAC header before delivery to
	 * mac80211
	 */
	tot_hdr_len = sizeof(struct htt_resp_hdr) + sizeof(rx->hdr) +
		      sizeof(rx->ppdu) + sizeof(rx->prefix) +
		      sizeof(rx->fw_desc) +
		      sizeof(*mpdu_ranges) * num_mpdu_ranges + rx_desc_len;
2240

2241 2242 2243
	skb_pull(skb, tot_hdr_len);

	hdr = (struct ieee80211_hdr *)skb->data;
2244
	qos = ieee80211_is_data_qos(hdr->frame_control);
2245

2246
	rx_status = IEEE80211_SKB_RXCB(skb);
2247 2248
	memset(rx_status, 0, sizeof(*rx_status));

2249 2250 2251 2252 2253 2254 2255 2256 2257
	if (rx->ppdu.combined_rssi == 0) {
		/* SDIO firmware does not provide signal */
		rx_status->signal = 0;
		rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
	} else {
		rx_status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
			rx->ppdu.combined_rssi;
		rx_status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
	}
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289

	spin_lock_bh(&ar->data_lock);
	ch = ar->scan_channel;
	if (!ch)
		ch = ar->rx_channel;
	if (!ch)
		ch = ath10k_htt_rx_h_any_channel(ar);
	if (!ch)
		ch = ar->tgt_oper_chan;
	spin_unlock_bh(&ar->data_lock);

	if (ch) {
		rx_status->band = ch->band;
		rx_status->freq = ch->center_freq;
	}
	if (rx->fw_desc.flags & FW_RX_DESC_FLAGS_LAST_MSDU)
		rx_status->flag &= ~RX_FLAG_AMSDU_MORE;
	else
		rx_status->flag |= RX_FLAG_AMSDU_MORE;

	/* Not entirely sure about this, but all frames from the chipset has
	 * the protected flag set even though they have already been decrypted.
	 * Unmasking this flag is necessary in order for mac80211 not to drop
	 * the frame.
	 * TODO: Verify this is always the case or find out a way to check
	 * if there has been hw decryption.
	 */
	if (ieee80211_has_protected(hdr->frame_control)) {
		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
		rx_status->flag |= RX_FLAG_DECRYPTED |
				   RX_FLAG_IV_STRIPPED |
				   RX_FLAG_MMIC_STRIPPED;
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338

		if (tid < IEEE80211_NUM_TIDS &&
		    first_msdu &&
		    check_pn_type == HTT_RX_PN_CHECK &&
		   (sec_type == HTT_SECURITY_AES_CCMP ||
		    sec_type == HTT_SECURITY_TKIP ||
		    sec_type == HTT_SECURITY_TKIP_NOMIC)) {
			u8 offset, *ivp, i;
			s8 keyidx = 0;
			__le64 pn48 = cpu_to_le64(new_pn.pn48);

			hdr = (struct ieee80211_hdr *)skb->data;
			offset = ieee80211_hdrlen(hdr->frame_control);
			hdr->frame_control |= __cpu_to_le16(IEEE80211_FCTL_PROTECTED);
			rx_status->flag &= ~RX_FLAG_IV_STRIPPED;

			memmove(skb->data - IEEE80211_CCMP_HDR_LEN,
				skb->data, offset);
			skb_push(skb, IEEE80211_CCMP_HDR_LEN);
			ivp = skb->data + offset;
			memset(skb->data + offset, 0, IEEE80211_CCMP_HDR_LEN);
			/* Ext IV */
			ivp[IEEE80211_WEP_IV_LEN - 1] |= ATH10K_IEEE80211_EXTIV;

			for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
				if (peer->keys[i] &&
				    peer->keys[i]->flags & IEEE80211_KEY_FLAG_PAIRWISE)
					keyidx = peer->keys[i]->keyidx;
			}

			/* Key ID */
			ivp[IEEE80211_WEP_IV_LEN - 1] |= keyidx << 6;

			if (sec_type == HTT_SECURITY_AES_CCMP) {
				rx_status->flag |= RX_FLAG_MIC_STRIPPED;
				/* pn 0, pn 1 */
				memcpy(skb->data + offset, &pn48, 2);
				/* pn 1, pn 3 , pn 34 , pn 5 */
				memcpy(skb->data + offset + 4, ((u8 *)&pn48) + 2, 4);
			} else {
				rx_status->flag |= RX_FLAG_ICV_STRIPPED;
				/* TSC 0 */
				memcpy(skb->data + offset + 2, &pn48, 1);
				/* TSC 1 */
				memcpy(skb->data + offset, ((u8 *)&pn48) + 1, 1);
				/* TSC 2 , TSC 3 , TSC 4 , TSC 5*/
				memcpy(skb->data + offset + 4, ((u8 *)&pn48) + 2, 4);
			}
		}
2339 2340
	}

2341 2342 2343 2344
	if (tkip_mic_type == HTT_RX_TKIP_MIC)
		rx_status->flag &= ~RX_FLAG_IV_STRIPPED &
				   ~RX_FLAG_MMIC_STRIPPED;

2345 2346 2347
	if (mpdu_ranges->mpdu_range_status == HTT_RX_IND_MPDU_STATUS_TKIP_MIC_ERR)
		rx_status->flag |= RX_FLAG_MMIC_ERROR;

2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361
	if (!qos && tid < IEEE80211_NUM_TIDS) {
		u8 offset;
		__le16 qos_ctrl = 0;

		hdr = (struct ieee80211_hdr *)skb->data;
		offset = ieee80211_hdrlen(hdr->frame_control);

		hdr->frame_control |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
		memmove(skb->data - IEEE80211_QOS_CTL_LEN, skb->data, offset);
		skb_push(skb, IEEE80211_QOS_CTL_LEN);
		qos_ctrl = cpu_to_le16(tid);
		memcpy(skb->data + offset, &qos_ctrl, IEEE80211_QOS_CTL_LEN);
	}

2362 2363 2364 2365
	if (ar->napi.dev)
		ieee80211_rx_napi(ar->hw, NULL, skb, &ar->napi);
	else
		ieee80211_rx_ni(ar->hw, skb);
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377

	/* We have delivered the skb to the upper layers (mac80211) so we
	 * must not free it.
	 */
	return false;
err:
	/* Tell the caller that it must free the skb since we have not
	 * consumed it
	 */
	return true;
}

2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
static int ath10k_htt_rx_frag_tkip_decap_nomic(struct sk_buff *skb,
					       u16 head_len,
					       u16 hdr_len)
{
	u8 *ivp, *orig_hdr;

	orig_hdr = skb->data;
	ivp = orig_hdr + hdr_len + head_len;

	/* the ExtIV bit is always set to 1 for TKIP */
	if (!(ivp[IEEE80211_WEP_IV_LEN - 1] & ATH10K_IEEE80211_EXTIV))
		return -EINVAL;

	memmove(orig_hdr + IEEE80211_TKIP_IV_LEN, orig_hdr, head_len + hdr_len);
	skb_pull(skb, IEEE80211_TKIP_IV_LEN);
	skb_trim(skb, skb->len - ATH10K_IEEE80211_TKIP_MICLEN);
	return 0;
}

static int ath10k_htt_rx_frag_tkip_decap_withmic(struct sk_buff *skb,
						 u16 head_len,
						 u16 hdr_len)
{
	u8 *ivp, *orig_hdr;

	orig_hdr = skb->data;
	ivp = orig_hdr + hdr_len + head_len;

	/* the ExtIV bit is always set to 1 for TKIP */
	if (!(ivp[IEEE80211_WEP_IV_LEN - 1] & ATH10K_IEEE80211_EXTIV))
		return -EINVAL;

	memmove(orig_hdr + IEEE80211_TKIP_IV_LEN, orig_hdr, head_len + hdr_len);
	skb_pull(skb, IEEE80211_TKIP_IV_LEN);
	skb_trim(skb, skb->len - IEEE80211_TKIP_ICV_LEN);
	return 0;
}

static int ath10k_htt_rx_frag_ccmp_decap(struct sk_buff *skb,
					 u16 head_len,
					 u16 hdr_len)
{
	u8 *ivp, *orig_hdr;

	orig_hdr = skb->data;
	ivp = orig_hdr + hdr_len + head_len;

	/* the ExtIV bit is always set to 1 for CCMP */
	if (!(ivp[IEEE80211_WEP_IV_LEN - 1] & ATH10K_IEEE80211_EXTIV))
		return -EINVAL;

	skb_trim(skb, skb->len - IEEE80211_CCMP_MIC_LEN);
	memmove(orig_hdr + IEEE80211_CCMP_HDR_LEN, orig_hdr, head_len + hdr_len);
	skb_pull(skb, IEEE80211_CCMP_HDR_LEN);
	return 0;
}

static int ath10k_htt_rx_frag_wep_decap(struct sk_buff *skb,
					u16 head_len,
					u16 hdr_len)
{
	u8 *orig_hdr;

	orig_hdr = skb->data;

	memmove(orig_hdr + IEEE80211_WEP_IV_LEN,
		orig_hdr, head_len + hdr_len);
	skb_pull(skb, IEEE80211_WEP_IV_LEN);
	skb_trim(skb, skb->len - IEEE80211_WEP_ICV_LEN);
	return 0;
}

static bool ath10k_htt_rx_proc_rx_frag_ind_hl(struct ath10k_htt *htt,
					      struct htt_rx_fragment_indication *rx,
					      struct sk_buff *skb)
{
	struct ath10k *ar = htt->ar;
	enum htt_rx_tkip_demic_type tkip_mic = HTT_RX_NON_TKIP_MIC;
	enum htt_txrx_sec_cast_type sec_index;
	struct htt_rx_indication_hl *rx_hl;
	enum htt_security_types sec_type;
	u32 tid, frag, seq, rx_desc_info;
	union htt_rx_pn_t new_pn = {0};
	struct htt_hl_rx_desc *rx_desc;
	u16 peer_id, sc, hdr_space;
	union htt_rx_pn_t *last_pn;
	struct ieee80211_hdr *hdr;
	int ret, num_mpdu_ranges;
	struct ath10k_peer *peer;
	struct htt_resp *resp;
	size_t tot_hdr_len;

	resp = (struct htt_resp *)(skb->data + HTT_RX_FRAG_IND_INFO0_HEADER_LEN);
	skb_pull(skb, HTT_RX_FRAG_IND_INFO0_HEADER_LEN);
	skb_trim(skb, skb->len - FCS_LEN);

	peer_id = __le16_to_cpu(rx->peer_id);
	rx_hl = (struct htt_rx_indication_hl *)(&resp->rx_ind_hl);

	spin_lock_bh(&ar->data_lock);
	peer = ath10k_peer_find_by_id(ar, peer_id);
	if (!peer) {
		ath10k_dbg(ar, ATH10K_DBG_HTT, "invalid peer: %u\n", peer_id);
		goto err;
	}

	num_mpdu_ranges = MS(__le32_to_cpu(rx_hl->hdr.info1),
			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);

	tot_hdr_len = sizeof(struct htt_resp_hdr) +
		      sizeof(rx_hl->hdr) +
		      sizeof(rx_hl->ppdu) +
		      sizeof(rx_hl->prefix) +
		      sizeof(rx_hl->fw_desc) +
		      sizeof(struct htt_rx_indication_mpdu_range) * num_mpdu_ranges;

	tid =  MS(rx_hl->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);
	rx_desc = (struct htt_hl_rx_desc *)(skb->data + tot_hdr_len);
	rx_desc_info = __le32_to_cpu(rx_desc->info);

	if (!MS(rx_desc_info, HTT_RX_DESC_HL_INFO_ENCRYPTED)) {
		spin_unlock_bh(&ar->data_lock);
		return ath10k_htt_rx_proc_rx_ind_hl(htt, &resp->rx_ind_hl, skb,
						    HTT_RX_NON_PN_CHECK,
						    HTT_RX_NON_TKIP_MIC);
	}

	hdr = (struct ieee80211_hdr *)((u8 *)rx_desc + rx_hl->fw_desc.len);

	if (ieee80211_has_retry(hdr->frame_control))
		goto err;

	hdr_space = ieee80211_hdrlen(hdr->frame_control);
	sc = __le16_to_cpu(hdr->seq_ctrl);
	seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
	frag = sc & IEEE80211_SCTL_FRAG;

	sec_index = MS(rx_desc_info, HTT_RX_DESC_HL_INFO_MCAST_BCAST) ?
		    HTT_TXRX_SEC_MCAST : HTT_TXRX_SEC_UCAST;
	sec_type = peer->rx_pn[sec_index].sec_type;
	ath10k_htt_rx_mpdu_desc_pn_hl(rx_desc, &new_pn, peer->rx_pn[sec_index].pn_len);

	switch (sec_type) {
	case HTT_SECURITY_TKIP:
		tkip_mic = HTT_RX_TKIP_MIC;
		ret = ath10k_htt_rx_frag_tkip_decap_withmic(skb,
							    tot_hdr_len +
							    rx_hl->fw_desc.len,
							    hdr_space);
		if (ret)
			goto err;
		break;
	case HTT_SECURITY_TKIP_NOMIC:
		ret = ath10k_htt_rx_frag_tkip_decap_nomic(skb,
							  tot_hdr_len +
							  rx_hl->fw_desc.len,
							  hdr_space);
		if (ret)
			goto err;
		break;
	case HTT_SECURITY_AES_CCMP:
		ret = ath10k_htt_rx_frag_ccmp_decap(skb,
						    tot_hdr_len + rx_hl->fw_desc.len,
						    hdr_space);
		if (ret)
			goto err;
		break;
	case HTT_SECURITY_WEP128:
	case HTT_SECURITY_WEP104:
	case HTT_SECURITY_WEP40:
		ret = ath10k_htt_rx_frag_wep_decap(skb,
						   tot_hdr_len + rx_hl->fw_desc.len,
						   hdr_space);
		if (ret)
			goto err;
		break;
	default:
		break;
	}

	resp = (struct htt_resp *)(skb->data);

	if (sec_type != HTT_SECURITY_AES_CCMP &&
	    sec_type != HTT_SECURITY_TKIP &&
	    sec_type != HTT_SECURITY_TKIP_NOMIC) {
		spin_unlock_bh(&ar->data_lock);
		return ath10k_htt_rx_proc_rx_ind_hl(htt, &resp->rx_ind_hl, skb,
						    HTT_RX_NON_PN_CHECK,
						    HTT_RX_NON_TKIP_MIC);
	}

	last_pn = &peer->frag_tids_last_pn[tid];

	if (frag == 0) {
		if (ath10k_htt_rx_pn_check_replay_hl(ar, peer, &resp->rx_ind_hl))
			goto err;

		last_pn->pn48 = new_pn.pn48;
		peer->frag_tids_seq[tid] = seq;
	} else if (sec_type == HTT_SECURITY_AES_CCMP) {
		if (seq != peer->frag_tids_seq[tid])
			goto err;

		if (new_pn.pn48 != last_pn->pn48 + 1)
			goto err;

		last_pn->pn48 = new_pn.pn48;
		last_pn = &peer->tids_last_pn[tid];
		last_pn->pn48 = new_pn.pn48;
	}

	spin_unlock_bh(&ar->data_lock);

	return ath10k_htt_rx_proc_rx_ind_hl(htt, &resp->rx_ind_hl, skb,
					    HTT_RX_NON_PN_CHECK, tkip_mic);

err:
	spin_unlock_bh(&ar->data_lock);

	/* Tell the caller that it must free the skb since we have not
	 * consumed it
	 */
	return true;
}

2603 2604
static void ath10k_htt_rx_proc_rx_ind_ll(struct ath10k_htt *htt,
					 struct htt_rx_indication *rx)
2605
{
2606
	struct ath10k *ar = htt->ar;
2607 2608
	struct htt_rx_indication_mpdu_range *mpdu_ranges;
	int num_mpdu_ranges;
2609
	int i, mpdu_count = 0;
2610 2611
	u16 peer_id;
	u8 tid;
2612 2613 2614

	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
2615 2616 2617
	peer_id = __le16_to_cpu(rx->hdr.peer_id);
	tid =  MS(rx->hdr.info0, HTT_RX_INDICATION_INFO0_EXT_TID);

2618 2619
	mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);

2620
	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
2621
			rx, struct_size(rx, mpdu_ranges, num_mpdu_ranges));
2622

Michal Kazior's avatar
Michal Kazior committed
2623 2624 2625
	for (i = 0; i < num_mpdu_ranges; i++)
		mpdu_count += mpdu_ranges[i].mpdu_count;

2626
	atomic_add(mpdu_count, &htt->num_mpdus_ready);
2627 2628 2629

	ath10k_sta_update_rx_tid_stats_ampdu(ar, peer_id, tid, mpdu_ranges,
					     num_mpdu_ranges);
2630 2631
}

2632
static void ath10k_htt_rx_tx_compl_ind(struct ath10k *ar,
2633 2634 2635 2636 2637 2638
				       struct sk_buff *skb)
{
	struct ath10k_htt *htt = &ar->htt;
	struct htt_resp *resp = (struct htt_resp *)skb->data;
	struct htt_tx_done tx_done = {};
	int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
2639 2640
	__le16 msdu_id, *msdus;
	bool rssi_enabled = false;
2641
	u8 msdu_count = 0, num_airtime_records, tid;
2642
	int i, htt_pad = 0;
2643 2644 2645 2646
	struct htt_data_tx_compl_ppdu_dur *ppdu_info;
	struct ath10k_peer *peer;
	u16 ppdu_info_offset = 0, peer_id;
	u32 tx_duration;
2647 2648 2649

	switch (status) {
	case HTT_DATA_TX_STATUS_NO_ACK:
2650
		tx_done.status = HTT_TX_COMPL_STATE_NOACK;
2651 2652
		break;
	case HTT_DATA_TX_STATUS_OK:
2653
		tx_done.status = HTT_TX_COMPL_STATE_ACK;
2654 2655 2656 2657
		break;
	case HTT_DATA_TX_STATUS_DISCARD:
	case HTT_DATA_TX_STATUS_POSTPONE:
	case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
2658
		tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
2659 2660
		break;
	default:
2661
		ath10k_warn(ar, "unhandled tx completion status %d\n", status);
2662
		tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
2663 2664 2665
		break;
	}

2666
	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
2667 2668
		   resp->data_tx_completion.num_msdus);

2669
	msdu_count = resp->data_tx_completion.num_msdus;
2670
	msdus = resp->data_tx_completion.msdus;
2671
	rssi_enabled = ath10k_is_rssi_enable(&ar->hw_params, resp);
2672

2673 2674 2675
	if (rssi_enabled)
		htt_pad = ath10k_tx_data_rssi_get_pad_bytes(&ar->hw_params,
							    resp);
2676 2677 2678

	for (i = 0; i < msdu_count; i++) {
		msdu_id = msdus[i];
2679
		tx_done.msdu_id = __le16_to_cpu(msdu_id);
2680

2681 2682 2683 2684 2685 2686
		if (rssi_enabled) {
			/* Total no of MSDUs should be even,
			 * if odd MSDUs are sent firmware fills
			 * last msdu id with 0xffff
			 */
			if (msdu_count & 0x01) {
2687
				msdu_id = msdus[msdu_count +  i + 1 + htt_pad];
2688 2689
				tx_done.ack_rssi = __le16_to_cpu(msdu_id);
			} else {
2690
				msdu_id = msdus[msdu_count +  i + htt_pad];
2691 2692 2693 2694
				tx_done.ack_rssi = __le16_to_cpu(msdu_id);
			}
		}

2695 2696 2697 2698 2699 2700 2701 2702
		/* kfifo_put: In practice firmware shouldn't fire off per-CE
		 * interrupt and main interrupt (MSI/-X range case) for the same
		 * HTC service so it should be safe to use kfifo_put w/o lock.
		 *
		 * From kfifo_put() documentation:
		 *  Note that with only one concurrent reader and one concurrent
		 *  writer, you don't need extra locking to use these macro.
		 */
2703 2704 2705
		if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL) {
			ath10k_txrx_tx_unref(htt, &tx_done);
		} else if (!kfifo_put(&htt->txdone_fifo, tx_done)) {
2706 2707 2708 2709
			ath10k_warn(ar, "txdone fifo overrun, msdu_id %d status %d\n",
				    tx_done.msdu_id, tx_done.status);
			ath10k_txrx_tx_unref(htt, &tx_done);
		}
2710
	}
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740

	if (!(resp->data_tx_completion.flags2 & HTT_TX_CMPL_FLAG_PPDU_DURATION_PRESENT))
		return;

	ppdu_info_offset = (msdu_count & 0x01) ? msdu_count + 1 : msdu_count;

	if (rssi_enabled)
		ppdu_info_offset += ppdu_info_offset;

	if (resp->data_tx_completion.flags2 &
	    (HTT_TX_CMPL_FLAG_PPID_PRESENT | HTT_TX_CMPL_FLAG_PA_PRESENT))
		ppdu_info_offset += 2;

	ppdu_info = (struct htt_data_tx_compl_ppdu_dur *)&msdus[ppdu_info_offset];
	num_airtime_records = FIELD_GET(HTT_TX_COMPL_PPDU_DUR_INFO0_NUM_ENTRIES_MASK,
					__le32_to_cpu(ppdu_info->info0));

	for (i = 0; i < num_airtime_records; i++) {
		struct htt_data_tx_ppdu_dur *ppdu_dur;
		u32 info0;

		ppdu_dur = &ppdu_info->ppdu_dur[i];
		info0 = __le32_to_cpu(ppdu_dur->info0);

		peer_id = FIELD_GET(HTT_TX_PPDU_DUR_INFO0_PEER_ID_MASK,
				    info0);
		rcu_read_lock();
		spin_lock_bh(&ar->data_lock);

		peer = ath10k_peer_find_by_id(ar, peer_id);
2741
		if (!peer || !peer->sta) {
2742 2743 2744 2745 2746
			spin_unlock_bh(&ar->data_lock);
			rcu_read_unlock();
			continue;
		}

2747 2748
		tid = FIELD_GET(HTT_TX_PPDU_DUR_INFO0_TID_MASK, info0) &
						IEEE80211_QOS_CTL_TID_MASK;
2749 2750 2751 2752 2753 2754 2755
		tx_duration = __le32_to_cpu(ppdu_dur->tx_duration);

		ieee80211_sta_register_airtime(peer->sta, tid, tx_duration, 0);

		spin_unlock_bh(&ar->data_lock);
		rcu_read_unlock();
	}
2756 2757
}

2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768
static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
{
	struct htt_rx_addba *ev = &resp->rx_addba;
	struct ath10k_peer *peer;
	struct ath10k_vif *arvif;
	u16 info0, tid, peer_id;

	info0 = __le16_to_cpu(ev->info0);
	tid = MS(info0, HTT_RX_BA_INFO0_TID);
	peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);

2769
	ath10k_dbg(ar, ATH10K_DBG_HTT,
2770 2771 2772 2773 2774 2775
		   "htt rx addba tid %hu peer_id %hu size %hhu\n",
		   tid, peer_id, ev->window_size);

	spin_lock_bh(&ar->data_lock);
	peer = ath10k_peer_find_by_id(ar, peer_id);
	if (!peer) {
2776
		ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
2777 2778 2779 2780 2781 2782 2783
			    peer_id);
		spin_unlock_bh(&ar->data_lock);
		return;
	}

	arvif = ath10k_get_arvif(ar, peer->vdev_id);
	if (!arvif) {
2784
		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
2785 2786 2787 2788 2789
			    peer->vdev_id);
		spin_unlock_bh(&ar->data_lock);
		return;
	}

2790
	ath10k_dbg(ar, ATH10K_DBG_HTT,
2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808
		   "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
		   peer->addr, tid, ev->window_size);

	ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
	spin_unlock_bh(&ar->data_lock);
}

static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
{
	struct htt_rx_delba *ev = &resp->rx_delba;
	struct ath10k_peer *peer;
	struct ath10k_vif *arvif;
	u16 info0, tid, peer_id;

	info0 = __le16_to_cpu(ev->info0);
	tid = MS(info0, HTT_RX_BA_INFO0_TID);
	peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);

2809
	ath10k_dbg(ar, ATH10K_DBG_HTT,
2810 2811 2812 2813 2814 2815
		   "htt rx delba tid %hu peer_id %hu\n",
		   tid, peer_id);

	spin_lock_bh(&ar->data_lock);
	peer = ath10k_peer_find_by_id(ar, peer_id);
	if (!peer) {
2816
		ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
2817 2818 2819 2820 2821 2822 2823
			    peer_id);
		spin_unlock_bh(&ar->data_lock);
		return;
	}

	arvif = ath10k_get_arvif(ar, peer->vdev_id);
	if (!arvif) {
2824
		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
2825 2826 2827 2828 2829
			    peer->vdev_id);
		spin_unlock_bh(&ar->data_lock);
		return;
	}

2830
	ath10k_dbg(ar, ATH10K_DBG_HTT,
2831 2832 2833 2834 2835 2836 2837
		   "htt rx stop rx ba session sta %pM tid %hu\n",
		   peer->addr, tid);

	ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
	spin_unlock_bh(&ar->data_lock);
}

2838
static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
2839
				       struct sk_buff_head *amsdu)
2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
{
	struct sk_buff *msdu;
	struct htt_rx_desc *rxd;

	if (skb_queue_empty(list))
		return -ENOBUFS;

	if (WARN_ON(!skb_queue_empty(amsdu)))
		return -EINVAL;

2850
	while ((msdu = __skb_dequeue(list))) {
2851 2852 2853
		__skb_queue_tail(amsdu, msdu);

		rxd = (void *)msdu->data - sizeof(*rxd);
2854
		if (rxd->msdu_end.common.info0 &
2855 2856 2857 2858 2859 2860
		    __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
			break;
	}

	msdu = skb_peek_tail(amsdu);
	rxd = (void *)msdu->data - sizeof(*rxd);
2861
	if (!(rxd->msdu_end.common.info0 &
2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888
	      __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
		skb_queue_splice_init(amsdu, list);
		return -EAGAIN;
	}

	return 0;
}

static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
					    struct sk_buff *skb)
{
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;

	if (!ieee80211_has_protected(hdr->frame_control))
		return;

	/* Offloaded frames are already decrypted but firmware insists they are
	 * protected in the 802.11 header. Strip the flag.  Otherwise mac80211
	 * will drop the frame.
	 */

	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
	status->flag |= RX_FLAG_DECRYPTED |
			RX_FLAG_IV_STRIPPED |
			RX_FLAG_MMIC_STRIPPED;
}

2889 2890
static void ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
				       struct sk_buff_head *list)
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933
{
	struct ath10k_htt *htt = &ar->htt;
	struct ieee80211_rx_status *status = &htt->rx_status;
	struct htt_rx_offload_msdu *rx;
	struct sk_buff *msdu;
	size_t offset;

	while ((msdu = __skb_dequeue(list))) {
		/* Offloaded frames don't have Rx descriptor. Instead they have
		 * a short meta information header.
		 */

		rx = (void *)msdu->data;

		skb_put(msdu, sizeof(*rx));
		skb_pull(msdu, sizeof(*rx));

		if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
			ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
			dev_kfree_skb_any(msdu);
			continue;
		}

		skb_put(msdu, __le16_to_cpu(rx->msdu_len));

		/* Offloaded rx header length isn't multiple of 2 nor 4 so the
		 * actual payload is unaligned. Align the frame.  Otherwise
		 * mac80211 complains.  This shouldn't reduce performance much
		 * because these offloaded frames are rare.
		 */
		offset = 4 - ((unsigned long)msdu->data & 3);
		skb_put(msdu, offset);
		memmove(msdu->data + offset, msdu->data, msdu->len);
		skb_pull(msdu, offset);

		/* FIXME: The frame is NWifi. Re-construct QoS Control
		 * if possible later.
		 */

		memset(status, 0, sizeof(*status));
		status->flag |= RX_FLAG_NO_SIGNAL_VAL;

		ath10k_htt_rx_h_rx_offload_prot(status, msdu);
2934
		ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
2935
		ath10k_htt_rx_h_queue_msdu(ar, status, msdu);
2936 2937 2938
	}
}

2939
static int ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951
{
	struct ath10k_htt *htt = &ar->htt;
	struct htt_resp *resp = (void *)skb->data;
	struct ieee80211_rx_status *status = &htt->rx_status;
	struct sk_buff_head list;
	struct sk_buff_head amsdu;
	u16 peer_id;
	u16 msdu_count;
	u8 vdev_id;
	u8 tid;
	bool offload;
	bool frag;
2952
	int ret;
2953 2954 2955 2956

	lockdep_assert_held(&htt->rx_ring.lock);

	if (htt->rx_confused)
2957
		return -EIO;
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973

	skb_pull(skb, sizeof(resp->hdr));
	skb_pull(skb, sizeof(resp->rx_in_ord_ind));

	peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
	msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
	vdev_id = resp->rx_in_ord_ind.vdev_id;
	tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
	offload = !!(resp->rx_in_ord_ind.info &
			HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
	frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);

	ath10k_dbg(ar, ATH10K_DBG_HTT,
		   "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
		   vdev_id, peer_id, tid, offload, frag, msdu_count);

2974
	if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs32)) {
2975
		ath10k_warn(ar, "dropping invalid in order rx indication\n");
2976
		return -EINVAL;
2977 2978 2979 2980 2981 2982
	}

	/* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
	 * extracted and processed.
	 */
	__skb_queue_head_init(&list);
2983 2984 2985 2986 2987 2988 2989
	if (ar->hw_params.target_64bit)
		ret = ath10k_htt_rx_pop_paddr64_list(htt, &resp->rx_in_ord_ind,
						     &list);
	else
		ret = ath10k_htt_rx_pop_paddr32_list(htt, &resp->rx_in_ord_ind,
						     &list);

2990 2991 2992
	if (ret < 0) {
		ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
		htt->rx_confused = true;
2993
		return -EIO;
2994 2995 2996 2997 2998 2999
	}

	/* Offloaded frames are very different and need to be handled
	 * separately.
	 */
	if (offload)
3000
		ath10k_htt_rx_h_rx_offload(ar, &list);
3001

3002
	while (!skb_queue_empty(&list)) {
3003
		__skb_queue_head_init(&amsdu);
3004
		ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
3005 3006 3007 3008 3009 3010 3011 3012
		switch (ret) {
		case 0:
			/* Note: The in-order indication may report interleaved
			 * frames from different PPDUs meaning reported rx rate
			 * to mac80211 isn't accurate/reliable. It's still
			 * better to report something than nothing though. This
			 * should still give an idea about rx rate to the user.
			 */
3013
			ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
3014 3015 3016
			ath10k_htt_rx_h_filter(ar, &amsdu, status, NULL);
			ath10k_htt_rx_h_mpdu(ar, &amsdu, status, false, NULL,
					     NULL);
3017
			ath10k_htt_rx_h_enqueue(ar, &amsdu, status);
3018 3019 3020 3021 3022 3023 3024 3025
			break;
		case -EAGAIN:
			/* fall through */
		default:
			/* Should not happen. */
			ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
			htt->rx_confused = true;
			__skb_queue_purge(&list);
3026
			return -EIO;
3027 3028
		}
	}
3029
	return ret;
3030 3031
}

3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053
static void ath10k_htt_rx_tx_fetch_resp_id_confirm(struct ath10k *ar,
						   const __le32 *resp_ids,
						   int num_resp_ids)
{
	int i;
	u32 resp_id;

	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm num_resp_ids %d\n",
		   num_resp_ids);

	for (i = 0; i < num_resp_ids; i++) {
		resp_id = le32_to_cpu(resp_ids[i]);

		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm resp_id %u\n",
			   resp_id);

		/* TODO: free resp_id */
	}
}

static void ath10k_htt_rx_tx_fetch_ind(struct ath10k *ar, struct sk_buff *skb)
{
3054 3055
	struct ieee80211_hw *hw = ar->hw;
	struct ieee80211_txq *txq;
3056 3057 3058 3059 3060
	struct htt_resp *resp = (struct htt_resp *)skb->data;
	struct htt_tx_fetch_record *record;
	size_t len;
	size_t max_num_bytes;
	size_t max_num_msdus;
3061 3062
	size_t num_bytes;
	size_t num_msdus;
3063 3064 3065 3066 3067
	const __le32 *resp_ids;
	u16 num_records;
	u16 num_resp_ids;
	u16 peer_id;
	u8 tid;
3068
	int ret;
3069
	int i;
3070
	bool may_tx;
3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094

	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind\n");

	len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_ind);
	if (unlikely(skb->len < len)) {
		ath10k_warn(ar, "received corrupted tx_fetch_ind event: buffer too short\n");
		return;
	}

	num_records = le16_to_cpu(resp->tx_fetch_ind.num_records);
	num_resp_ids = le16_to_cpu(resp->tx_fetch_ind.num_resp_ids);

	len += sizeof(resp->tx_fetch_ind.records[0]) * num_records;
	len += sizeof(resp->tx_fetch_ind.resp_ids[0]) * num_resp_ids;

	if (unlikely(skb->len < len)) {
		ath10k_warn(ar, "received corrupted tx_fetch_ind event: too many records/resp_ids\n");
		return;
	}

	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind num records %hu num resps %hu seq %hu\n",
		   num_records, num_resp_ids,
		   le16_to_cpu(resp->tx_fetch_ind.fetch_seq_num));

3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105
	if (!ar->htt.tx_q_state.enabled) {
		ath10k_warn(ar, "received unexpected tx_fetch_ind event: not enabled\n");
		return;
	}

	if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH) {
		ath10k_warn(ar, "received unexpected tx_fetch_ind event: in push mode\n");
		return;
	}

	rcu_read_lock();
3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125

	for (i = 0; i < num_records; i++) {
		record = &resp->tx_fetch_ind.records[i];
		peer_id = MS(le16_to_cpu(record->info),
			     HTT_TX_FETCH_RECORD_INFO_PEER_ID);
		tid = MS(le16_to_cpu(record->info),
			 HTT_TX_FETCH_RECORD_INFO_TID);
		max_num_msdus = le16_to_cpu(record->num_msdus);
		max_num_bytes = le32_to_cpu(record->num_bytes);

		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch record %i peer_id %hu tid %hhu msdus %zu bytes %zu\n",
			   i, peer_id, tid, max_num_msdus, max_num_bytes);

		if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
		    unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
			ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
				    peer_id, tid);
			continue;
		}

3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142
		spin_lock_bh(&ar->data_lock);
		txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
		spin_unlock_bh(&ar->data_lock);

		/* It is okay to release the lock and use txq because RCU read
		 * lock is held.
		 */

		if (unlikely(!txq)) {
			ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
				    peer_id, tid);
			continue;
		}

		num_msdus = 0;
		num_bytes = 0;

3143 3144
		ieee80211_txq_schedule_start(hw, txq->ac);
		may_tx = ieee80211_txq_may_transmit(hw, txq);
3145 3146
		while (num_msdus < max_num_msdus &&
		       num_bytes < max_num_bytes) {
3147 3148 3149
			if (!may_tx)
				break;

3150 3151 3152 3153 3154 3155 3156
			ret = ath10k_mac_tx_push_txq(hw, txq);
			if (ret < 0)
				break;

			num_msdus++;
			num_bytes += ret;
		}
3157
		ieee80211_return_txq(hw, txq, false);
3158
		ieee80211_txq_schedule_end(hw, txq->ac);
3159 3160 3161 3162 3163

		record->num_msdus = cpu_to_le16(num_msdus);
		record->num_bytes = cpu_to_le32(num_bytes);

		ath10k_htt_tx_txq_recalc(hw, txq);
3164 3165
	}

3166 3167
	rcu_read_unlock();

3168 3169 3170
	resp_ids = ath10k_htt_get_tx_fetch_ind_resp_ids(&resp->tx_fetch_ind);
	ath10k_htt_rx_tx_fetch_resp_id_confirm(ar, resp_ids, num_resp_ids);

3171 3172 3173 3174 3175 3176 3177 3178 3179 3180
	ret = ath10k_htt_tx_fetch_resp(ar,
				       resp->tx_fetch_ind.token,
				       resp->tx_fetch_ind.fetch_seq_num,
				       resp->tx_fetch_ind.records,
				       num_records);
	if (unlikely(ret)) {
		ath10k_warn(ar, "failed to submit tx fetch resp for token 0x%08x: %d\n",
			    le32_to_cpu(resp->tx_fetch_ind.token), ret);
		/* FIXME: request fw restart */
	}
3181

3182
	ath10k_htt_tx_txq_sync(ar);
3183 3184
}

3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217
static void ath10k_htt_rx_tx_fetch_confirm(struct ath10k *ar,
					   struct sk_buff *skb)
{
	const struct htt_resp *resp = (void *)skb->data;
	size_t len;
	int num_resp_ids;

	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm\n");

	len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_confirm);
	if (unlikely(skb->len < len)) {
		ath10k_warn(ar, "received corrupted tx_fetch_confirm event: buffer too short\n");
		return;
	}

	num_resp_ids = le16_to_cpu(resp->tx_fetch_confirm.num_resp_ids);
	len += sizeof(resp->tx_fetch_confirm.resp_ids[0]) * num_resp_ids;

	if (unlikely(skb->len < len)) {
		ath10k_warn(ar, "received corrupted tx_fetch_confirm event: resp_ids buffer overflow\n");
		return;
	}

	ath10k_htt_rx_tx_fetch_resp_id_confirm(ar,
					       resp->tx_fetch_confirm.resp_ids,
					       num_resp_ids);
}

static void ath10k_htt_rx_tx_mode_switch_ind(struct ath10k *ar,
					     struct sk_buff *skb)
{
	const struct htt_resp *resp = (void *)skb->data;
	const struct htt_tx_mode_switch_record *record;
3218 3219
	struct ieee80211_txq *txq;
	struct ath10k_txq *artxq;
3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270
	size_t len;
	size_t num_records;
	enum htt_tx_mode_switch_mode mode;
	bool enable;
	u16 info0;
	u16 info1;
	u16 threshold;
	u16 peer_id;
	u8 tid;
	int i;

	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx mode switch ind\n");

	len = sizeof(resp->hdr) + sizeof(resp->tx_mode_switch_ind);
	if (unlikely(skb->len < len)) {
		ath10k_warn(ar, "received corrupted tx_mode_switch_ind event: buffer too short\n");
		return;
	}

	info0 = le16_to_cpu(resp->tx_mode_switch_ind.info0);
	info1 = le16_to_cpu(resp->tx_mode_switch_ind.info1);

	enable = !!(info0 & HTT_TX_MODE_SWITCH_IND_INFO0_ENABLE);
	num_records = MS(info0, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
	mode = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_MODE);
	threshold = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);

	ath10k_dbg(ar, ATH10K_DBG_HTT,
		   "htt rx tx mode switch ind info0 0x%04hx info1 0x%04hx enable %d num records %zd mode %d threshold %hu\n",
		   info0, info1, enable, num_records, mode, threshold);

	len += sizeof(resp->tx_mode_switch_ind.records[0]) * num_records;

	if (unlikely(skb->len < len)) {
		ath10k_warn(ar, "received corrupted tx_mode_switch_mode_ind event: too many records\n");
		return;
	}

	switch (mode) {
	case HTT_TX_MODE_SWITCH_PUSH:
	case HTT_TX_MODE_SWITCH_PUSH_PULL:
		break;
	default:
		ath10k_warn(ar, "received invalid tx_mode_switch_mode_ind mode %d, ignoring\n",
			    mode);
		return;
	}

	if (!enable)
		return;

3271 3272 3273 3274 3275
	ar->htt.tx_q_state.enabled = enable;
	ar->htt.tx_q_state.mode = mode;
	ar->htt.tx_q_state.num_push_allowed = threshold;

	rcu_read_lock();
3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289

	for (i = 0; i < num_records; i++) {
		record = &resp->tx_mode_switch_ind.records[i];
		info0 = le16_to_cpu(record->info0);
		peer_id = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_PEER_ID);
		tid = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_TID);

		if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
		    unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
			ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
				    peer_id, tid);
			continue;
		}

3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307
		spin_lock_bh(&ar->data_lock);
		txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
		spin_unlock_bh(&ar->data_lock);

		/* It is okay to release the lock and use txq because RCU read
		 * lock is held.
		 */

		if (unlikely(!txq)) {
			ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
				    peer_id, tid);
			continue;
		}

		spin_lock_bh(&ar->htt.tx_lock);
		artxq = (void *)txq->drv_priv;
		artxq->num_push_allowed = le16_to_cpu(record->num_max_msdus);
		spin_unlock_bh(&ar->htt.tx_lock);
3308 3309
	}

3310 3311 3312
	rcu_read_unlock();

	ath10k_mac_tx_push_pending(ar);
3313 3314
}

3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325
void ath10k_htt_htc_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
{
	bool release;

	release = ath10k_htt_t2h_msg_handler(ar, skb);

	/* Free the indication buffer */
	if (release)
		dev_kfree_skb_any(skb);
}

3326
static inline s8 ath10k_get_legacy_rate_idx(struct ath10k *ar, u8 rate)
3327 3328 3329 3330 3331 3332 3333
{
	static const u8 legacy_rates[] = {1, 2, 5, 11, 6, 9, 12,
					  18, 24, 36, 48, 54};
	int i;

	for (i = 0; i < ARRAY_SIZE(legacy_rates); i++) {
		if (rate == legacy_rates[i])
3334
			return i;
3335 3336
	}

3337 3338
	ath10k_warn(ar, "Invalid legacy rate %hhd peer stats", rate);
	return -EINVAL;
3339 3340
}

3341 3342 3343 3344
static void
ath10k_accumulate_per_peer_tx_stats(struct ath10k *ar,
				    struct ath10k_sta *arsta,
				    struct ath10k_per_peer_tx_stats *pstats,
3345
				    s8 legacy_rate_idx)
3346 3347 3348
{
	struct rate_info *txrate = &arsta->txrate;
	struct ath10k_htt_tx_stats *tx_stats;
3349
	int idx, ht_idx, gi, mcs, bw, nss;
3350
	unsigned long flags;
3351 3352 3353 3354 3355

	if (!arsta->tx_stats)
		return;

	tx_stats = arsta->tx_stats;
3356 3357
	flags = txrate->flags;
	gi = test_bit(ATH10K_RATE_INFO_FLAGS_SGI_BIT, &flags);
3358
	mcs = ATH10K_HW_MCS_RATE(pstats->ratecode);
3359 3360
	bw = txrate->bw;
	nss = txrate->nss;
3361 3362
	ht_idx = mcs + (nss - 1) * 8;
	idx = mcs * 8 + 8 * 10 * (nss - 1);
3363
	idx += bw * 2 + gi;
3364 3365 3366

#define STATS_OP_FMT(name) tx_stats->stats[ATH10K_STATS_TYPE_##name]

3367
	if (txrate->flags & RATE_INFO_FLAGS_VHT_MCS) {
3368 3369 3370 3371 3372 3373
		STATS_OP_FMT(SUCC).vht[0][mcs] += pstats->succ_bytes;
		STATS_OP_FMT(SUCC).vht[1][mcs] += pstats->succ_pkts;
		STATS_OP_FMT(FAIL).vht[0][mcs] += pstats->failed_bytes;
		STATS_OP_FMT(FAIL).vht[1][mcs] += pstats->failed_pkts;
		STATS_OP_FMT(RETRY).vht[0][mcs] += pstats->retry_bytes;
		STATS_OP_FMT(RETRY).vht[1][mcs] += pstats->retry_pkts;
3374
	} else if (txrate->flags & RATE_INFO_FLAGS_MCS) {
3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394
		STATS_OP_FMT(SUCC).ht[0][ht_idx] += pstats->succ_bytes;
		STATS_OP_FMT(SUCC).ht[1][ht_idx] += pstats->succ_pkts;
		STATS_OP_FMT(FAIL).ht[0][ht_idx] += pstats->failed_bytes;
		STATS_OP_FMT(FAIL).ht[1][ht_idx] += pstats->failed_pkts;
		STATS_OP_FMT(RETRY).ht[0][ht_idx] += pstats->retry_bytes;
		STATS_OP_FMT(RETRY).ht[1][ht_idx] += pstats->retry_pkts;
	} else {
		mcs = legacy_rate_idx;

		STATS_OP_FMT(SUCC).legacy[0][mcs] += pstats->succ_bytes;
		STATS_OP_FMT(SUCC).legacy[1][mcs] += pstats->succ_pkts;
		STATS_OP_FMT(FAIL).legacy[0][mcs] += pstats->failed_bytes;
		STATS_OP_FMT(FAIL).legacy[1][mcs] += pstats->failed_pkts;
		STATS_OP_FMT(RETRY).legacy[0][mcs] += pstats->retry_bytes;
		STATS_OP_FMT(RETRY).legacy[1][mcs] += pstats->retry_pkts;
	}

	if (ATH10K_HW_AMPDU(pstats->flags)) {
		tx_stats->ba_fails += ATH10K_HW_BA_FAIL(pstats->flags);

3395
		if (txrate->flags & RATE_INFO_FLAGS_MCS) {
3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407
			STATS_OP_FMT(AMPDU).ht[0][ht_idx] +=
				pstats->succ_bytes + pstats->retry_bytes;
			STATS_OP_FMT(AMPDU).ht[1][ht_idx] +=
				pstats->succ_pkts + pstats->retry_pkts;
		} else {
			STATS_OP_FMT(AMPDU).vht[0][mcs] +=
				pstats->succ_bytes + pstats->retry_bytes;
			STATS_OP_FMT(AMPDU).vht[1][mcs] +=
				pstats->succ_pkts + pstats->retry_pkts;
		}
		STATS_OP_FMT(AMPDU).bw[0][bw] +=
			pstats->succ_bytes + pstats->retry_bytes;
3408
		STATS_OP_FMT(AMPDU).nss[0][nss - 1] +=
3409 3410 3411
			pstats->succ_bytes + pstats->retry_bytes;
		STATS_OP_FMT(AMPDU).gi[0][gi] +=
			pstats->succ_bytes + pstats->retry_bytes;
3412 3413
		STATS_OP_FMT(AMPDU).rate_table[0][idx] +=
			pstats->succ_bytes + pstats->retry_bytes;
3414 3415
		STATS_OP_FMT(AMPDU).bw[1][bw] +=
			pstats->succ_pkts + pstats->retry_pkts;
3416
		STATS_OP_FMT(AMPDU).nss[1][nss - 1] +=
3417 3418 3419
			pstats->succ_pkts + pstats->retry_pkts;
		STATS_OP_FMT(AMPDU).gi[1][gi] +=
			pstats->succ_pkts + pstats->retry_pkts;
3420 3421
		STATS_OP_FMT(AMPDU).rate_table[1][idx] +=
			pstats->succ_pkts + pstats->retry_pkts;
3422 3423 3424 3425 3426 3427
	} else {
		tx_stats->ack_fails +=
				ATH10K_HW_BA_FAIL(pstats->flags);
	}

	STATS_OP_FMT(SUCC).bw[0][bw] += pstats->succ_bytes;
3428
	STATS_OP_FMT(SUCC).nss[0][nss - 1] += pstats->succ_bytes;
3429 3430 3431
	STATS_OP_FMT(SUCC).gi[0][gi] += pstats->succ_bytes;

	STATS_OP_FMT(SUCC).bw[1][bw] += pstats->succ_pkts;
3432
	STATS_OP_FMT(SUCC).nss[1][nss - 1] += pstats->succ_pkts;
3433 3434 3435
	STATS_OP_FMT(SUCC).gi[1][gi] += pstats->succ_pkts;

	STATS_OP_FMT(FAIL).bw[0][bw] += pstats->failed_bytes;
3436
	STATS_OP_FMT(FAIL).nss[0][nss - 1] += pstats->failed_bytes;
3437 3438 3439
	STATS_OP_FMT(FAIL).gi[0][gi] += pstats->failed_bytes;

	STATS_OP_FMT(FAIL).bw[1][bw] += pstats->failed_pkts;
3440
	STATS_OP_FMT(FAIL).nss[1][nss - 1] += pstats->failed_pkts;
3441 3442 3443
	STATS_OP_FMT(FAIL).gi[1][gi] += pstats->failed_pkts;

	STATS_OP_FMT(RETRY).bw[0][bw] += pstats->retry_bytes;
3444
	STATS_OP_FMT(RETRY).nss[0][nss - 1] += pstats->retry_bytes;
3445 3446 3447
	STATS_OP_FMT(RETRY).gi[0][gi] += pstats->retry_bytes;

	STATS_OP_FMT(RETRY).bw[1][bw] += pstats->retry_pkts;
3448
	STATS_OP_FMT(RETRY).nss[1][nss - 1] += pstats->retry_pkts;
3449
	STATS_OP_FMT(RETRY).gi[1][gi] += pstats->retry_pkts;
3450 3451 3452 3453 3454 3455 3456 3457 3458

	if (txrate->flags >= RATE_INFO_FLAGS_MCS) {
		STATS_OP_FMT(SUCC).rate_table[0][idx] += pstats->succ_bytes;
		STATS_OP_FMT(SUCC).rate_table[1][idx] += pstats->succ_pkts;
		STATS_OP_FMT(FAIL).rate_table[0][idx] += pstats->failed_bytes;
		STATS_OP_FMT(FAIL).rate_table[1][idx] += pstats->failed_pkts;
		STATS_OP_FMT(RETRY).rate_table[0][idx] += pstats->retry_bytes;
		STATS_OP_FMT(RETRY).rate_table[1][idx] += pstats->retry_pkts;
	}
3459 3460

	tx_stats->tx_duration += pstats->duration;
3461 3462 3463 3464 3465 3466 3467 3468
}

static void
ath10k_update_per_peer_tx_stats(struct ath10k *ar,
				struct ieee80211_sta *sta,
				struct ath10k_per_peer_tx_stats *peer_stats)
{
	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
3469
	struct ieee80211_chanctx_conf *conf = NULL;
3470 3471
	u8 rate = 0, sgi;
	s8 rate_idx = 0;
3472
	bool skip_auto_rate;
3473 3474 3475 3476 3477 3478 3479 3480 3481
	struct rate_info txrate;

	lockdep_assert_held(&ar->data_lock);

	txrate.flags = ATH10K_HW_PREAMBLE(peer_stats->ratecode);
	txrate.bw = ATH10K_HW_BW(peer_stats->flags);
	txrate.nss = ATH10K_HW_NSS(peer_stats->ratecode);
	txrate.mcs = ATH10K_HW_MCS_RATE(peer_stats->ratecode);
	sgi = ATH10K_HW_GI(peer_stats->flags);
3482 3483 3484 3485 3486 3487 3488
	skip_auto_rate = ATH10K_FW_SKIPPED_RATE_CTRL(peer_stats->flags);

	/* Firmware's rate control skips broadcast/management frames,
	 * if host has configure fixed rates and in some other special cases.
	 */
	if (skip_auto_rate)
		return;
3489

3490 3491 3492 3493 3494 3495 3496 3497 3498
	if (txrate.flags == WMI_RATE_PREAMBLE_VHT && txrate.mcs > 9) {
		ath10k_warn(ar, "Invalid VHT mcs %hhd peer stats",  txrate.mcs);
		return;
	}

	if (txrate.flags == WMI_RATE_PREAMBLE_HT &&
	    (txrate.mcs > 7 || txrate.nss < 1)) {
		ath10k_warn(ar, "Invalid HT mcs %hhd nss %hhd peer stats",
			    txrate.mcs, txrate.nss);
3499 3500 3501
		return;
	}

3502
	memset(&arsta->txrate, 0, sizeof(arsta->txrate));
3503
	memset(&arsta->tx_info.status, 0, sizeof(arsta->tx_info.status));
3504 3505 3506 3507
	if (txrate.flags == WMI_RATE_PREAMBLE_CCK ||
	    txrate.flags == WMI_RATE_PREAMBLE_OFDM) {
		rate = ATH10K_HW_LEGACY_RATE(peer_stats->ratecode);
		/* This is hacky, FW sends CCK rate 5.5Mbps as 6 */
3508 3509 3510 3511 3512
		if (rate == 6 && txrate.flags == WMI_RATE_PREAMBLE_CCK)
			rate = 5;
		rate_idx = ath10k_get_legacy_rate_idx(ar, rate);
		if (rate_idx < 0)
			return;
3513
		arsta->txrate.legacy = rate;
3514 3515
	} else if (txrate.flags == WMI_RATE_PREAMBLE_HT) {
		arsta->txrate.flags = RATE_INFO_FLAGS_MCS;
3516
		arsta->txrate.mcs = txrate.mcs + 8 * (txrate.nss - 1);
3517 3518 3519 3520 3521
	} else {
		arsta->txrate.flags = RATE_INFO_FLAGS_VHT_MCS;
		arsta->txrate.mcs = txrate.mcs;
	}

3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
	switch (txrate.flags) {
	case WMI_RATE_PREAMBLE_OFDM:
		if (arsta->arvif && arsta->arvif->vif)
			conf = rcu_dereference(arsta->arvif->vif->chanctx_conf);
		if (conf && conf->def.chan->band == NL80211_BAND_5GHZ)
			arsta->tx_info.status.rates[0].idx = rate_idx - 4;
		break;
	case WMI_RATE_PREAMBLE_CCK:
		arsta->tx_info.status.rates[0].idx = rate_idx;
		if (sgi)
			arsta->tx_info.status.rates[0].flags |=
				(IEEE80211_TX_RC_USE_SHORT_PREAMBLE |
				 IEEE80211_TX_RC_SHORT_GI);
		break;
	case WMI_RATE_PREAMBLE_HT:
		arsta->tx_info.status.rates[0].idx =
				txrate.mcs + ((txrate.nss - 1) * 8);
		if (sgi)
			arsta->tx_info.status.rates[0].flags |=
					IEEE80211_TX_RC_SHORT_GI;
		arsta->tx_info.status.rates[0].flags |= IEEE80211_TX_RC_MCS;
		break;
	case WMI_RATE_PREAMBLE_VHT:
		ieee80211_rate_set_vht(&arsta->tx_info.status.rates[0],
				       txrate.mcs, txrate.nss);
		if (sgi)
			arsta->tx_info.status.rates[0].flags |=
						IEEE80211_TX_RC_SHORT_GI;
		arsta->tx_info.status.rates[0].flags |= IEEE80211_TX_RC_VHT_MCS;
		break;
	}
3553 3554

	arsta->txrate.nss = txrate.nss;
3555
	arsta->txrate.bw = ath10k_bw_to_mac80211_bw(txrate.bw);
3556
	arsta->last_tx_bitrate = cfg80211_calculate_bitrate(&arsta->txrate);
3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575
	if (sgi)
		arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;

	switch (arsta->txrate.bw) {
	case RATE_INFO_BW_40:
		arsta->tx_info.status.rates[0].flags |=
				IEEE80211_TX_RC_40_MHZ_WIDTH;
		break;
	case RATE_INFO_BW_80:
		arsta->tx_info.status.rates[0].flags |=
				IEEE80211_TX_RC_80_MHZ_WIDTH;
		break;
	}

	if (peer_stats->succ_pkts) {
		arsta->tx_info.flags = IEEE80211_TX_STAT_ACK;
		arsta->tx_info.status.rates[0].count = 1;
		ieee80211_tx_rate_update(ar->hw, sta, &arsta->tx_info);
	}
3576 3577 3578 3579

	if (ath10k_debug_is_extd_tx_stats_enabled(ar))
		ath10k_accumulate_per_peer_tx_stats(ar, arsta, peer_stats,
						    rate_idx);
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607
}

static void ath10k_htt_fetch_peer_stats(struct ath10k *ar,
					struct sk_buff *skb)
{
	struct htt_resp *resp = (struct htt_resp *)skb->data;
	struct ath10k_per_peer_tx_stats *p_tx_stats = &ar->peer_tx_stats;
	struct htt_per_peer_tx_stats_ind *tx_stats;
	struct ieee80211_sta *sta;
	struct ath10k_peer *peer;
	int peer_id, i;
	u8 ppdu_len, num_ppdu;

	num_ppdu = resp->peer_tx_stats.num_ppdu;
	ppdu_len = resp->peer_tx_stats.ppdu_len * sizeof(__le32);

	if (skb->len < sizeof(struct htt_resp_hdr) + num_ppdu * ppdu_len) {
		ath10k_warn(ar, "Invalid peer stats buf length %d\n", skb->len);
		return;
	}

	tx_stats = (struct htt_per_peer_tx_stats_ind *)
			(resp->peer_tx_stats.payload);
	peer_id = __le16_to_cpu(tx_stats->peer_id);

	rcu_read_lock();
	spin_lock_bh(&ar->data_lock);
	peer = ath10k_peer_find_by_id(ar, peer_id);
3608
	if (!peer || !peer->sta) {
3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627
		ath10k_warn(ar, "Invalid peer id %d peer stats buffer\n",
			    peer_id);
		goto out;
	}

	sta = peer->sta;
	for (i = 0; i < num_ppdu; i++) {
		tx_stats = (struct htt_per_peer_tx_stats_ind *)
			   (resp->peer_tx_stats.payload + i * ppdu_len);

		p_tx_stats->succ_bytes = __le32_to_cpu(tx_stats->succ_bytes);
		p_tx_stats->retry_bytes = __le32_to_cpu(tx_stats->retry_bytes);
		p_tx_stats->failed_bytes =
				__le32_to_cpu(tx_stats->failed_bytes);
		p_tx_stats->ratecode = tx_stats->ratecode;
		p_tx_stats->flags = tx_stats->flags;
		p_tx_stats->succ_pkts = __le16_to_cpu(tx_stats->succ_pkts);
		p_tx_stats->retry_pkts = __le16_to_cpu(tx_stats->retry_pkts);
		p_tx_stats->failed_pkts = __le16_to_cpu(tx_stats->failed_pkts);
3628
		p_tx_stats->duration = __le16_to_cpu(tx_stats->tx_duration);
3629 3630 3631 3632 3633 3634 3635 3636 3637

		ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
	}

out:
	spin_unlock_bh(&ar->data_lock);
	rcu_read_unlock();
}

3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
static void ath10k_fetch_10_2_tx_stats(struct ath10k *ar, u8 *data)
{
	struct ath10k_pktlog_hdr *hdr = (struct ath10k_pktlog_hdr *)data;
	struct ath10k_per_peer_tx_stats *p_tx_stats = &ar->peer_tx_stats;
	struct ath10k_10_2_peer_tx_stats *tx_stats;
	struct ieee80211_sta *sta;
	struct ath10k_peer *peer;
	u16 log_type = __le16_to_cpu(hdr->log_type);
	u32 peer_id = 0, i;

	if (log_type != ATH_PKTLOG_TYPE_TX_STAT)
		return;

	tx_stats = (struct ath10k_10_2_peer_tx_stats *)((hdr->payload) +
		    ATH10K_10_2_TX_STATS_OFFSET);

	if (!tx_stats->tx_ppdu_cnt)
		return;

	peer_id = tx_stats->peer_id;

	rcu_read_lock();
	spin_lock_bh(&ar->data_lock);
	peer = ath10k_peer_find_by_id(ar, peer_id);
3662
	if (!peer || !peer->sta) {
3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693
		ath10k_warn(ar, "Invalid peer id %d in peer stats buffer\n",
			    peer_id);
		goto out;
	}

	sta = peer->sta;
	for (i = 0; i < tx_stats->tx_ppdu_cnt; i++) {
		p_tx_stats->succ_bytes =
			__le16_to_cpu(tx_stats->success_bytes[i]);
		p_tx_stats->retry_bytes =
			__le16_to_cpu(tx_stats->retry_bytes[i]);
		p_tx_stats->failed_bytes =
			__le16_to_cpu(tx_stats->failed_bytes[i]);
		p_tx_stats->ratecode = tx_stats->ratecode[i];
		p_tx_stats->flags = tx_stats->flags[i];
		p_tx_stats->succ_pkts = tx_stats->success_pkts[i];
		p_tx_stats->retry_pkts = tx_stats->retry_pkts[i];
		p_tx_stats->failed_pkts = tx_stats->failed_pkts[i];

		ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
	}
	spin_unlock_bh(&ar->data_lock);
	rcu_read_unlock();

	return;

out:
	spin_unlock_bh(&ar->data_lock);
	rcu_read_unlock();
}

3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738
static int ath10k_htt_rx_pn_len(enum htt_security_types sec_type)
{
	switch (sec_type) {
	case HTT_SECURITY_TKIP:
	case HTT_SECURITY_TKIP_NOMIC:
	case HTT_SECURITY_AES_CCMP:
		return 48;
	default:
		return 0;
	}
}

static void ath10k_htt_rx_sec_ind_handler(struct ath10k *ar,
					  struct htt_security_indication *ev)
{
	enum htt_txrx_sec_cast_type sec_index;
	enum htt_security_types sec_type;
	struct ath10k_peer *peer;

	spin_lock_bh(&ar->data_lock);

	peer = ath10k_peer_find_by_id(ar, __le16_to_cpu(ev->peer_id));
	if (!peer) {
		ath10k_warn(ar, "failed to find peer id %d for security indication",
			    __le16_to_cpu(ev->peer_id));
		goto out;
	}

	sec_type = MS(ev->flags, HTT_SECURITY_TYPE);

	if (ev->flags & HTT_SECURITY_IS_UNICAST)
		sec_index = HTT_TXRX_SEC_UCAST;
	else
		sec_index = HTT_TXRX_SEC_MCAST;

	peer->rx_pn[sec_index].sec_type = sec_type;
	peer->rx_pn[sec_index].pn_len = ath10k_htt_rx_pn_len(sec_type);

	memset(peer->tids_last_pn_valid, 0, sizeof(peer->tids_last_pn_valid));
	memset(peer->tids_last_pn, 0, sizeof(peer->tids_last_pn));

out:
	spin_unlock_bh(&ar->data_lock);
}

3739
bool ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
3740
{
3741
	struct ath10k_htt *htt = &ar->htt;
3742
	struct htt_resp *resp = (struct htt_resp *)skb->data;
3743
	enum htt_t2h_msg_type type;
3744 3745 3746

	/* confirm alignment */
	if (!IS_ALIGNED((unsigned long)skb->data, 4))
3747
		ath10k_warn(ar, "unaligned htt message, expect trouble\n");
3748

3749
	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
3750
		   resp->hdr.msg_type);
3751 3752 3753 3754

	if (resp->hdr.msg_type >= ar->htt.t2h_msg_types_max) {
		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, unsupported msg_type: 0x%0X\n max: 0x%0X",
			   resp->hdr.msg_type, ar->htt.t2h_msg_types_max);
3755
		return true;
3756 3757 3758 3759
	}
	type = ar->htt.t2h_msg_types[resp->hdr.msg_type];

	switch (type) {
3760 3761 3762 3763 3764 3765
	case HTT_T2H_MSG_TYPE_VERSION_CONF: {
		htt->target_version_major = resp->ver_resp.major;
		htt->target_version_minor = resp->ver_resp.minor;
		complete(&htt->target_version_received);
		break;
	}
3766
	case HTT_T2H_MSG_TYPE_RX_IND:
3767
		if (ar->bus_param.dev_type != ATH10K_DEV_TYPE_HL) {
3768
			ath10k_htt_rx_proc_rx_ind_ll(htt, &resp->rx_ind);
3769 3770 3771 3772
		} else {
			skb_queue_tail(&htt->rx_indication_head, skb);
			return false;
		}
3773
		break;
3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791
	case HTT_T2H_MSG_TYPE_PEER_MAP: {
		struct htt_peer_map_event ev = {
			.vdev_id = resp->peer_map.vdev_id,
			.peer_id = __le16_to_cpu(resp->peer_map.peer_id),
		};
		memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
		ath10k_peer_map_event(htt, &ev);
		break;
	}
	case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
		struct htt_peer_unmap_event ev = {
			.peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
		};
		ath10k_peer_unmap_event(htt, &ev);
		break;
	}
	case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
		struct htt_tx_done tx_done = {};
3792 3793 3794
		struct ath10k_htt *htt = &ar->htt;
		struct ath10k_htc *htc = &ar->htc;
		struct ath10k_htc_ep *ep = &ar->htc.endpoint[htt->eid];
3795
		int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
3796
		int info = __le32_to_cpu(resp->mgmt_tx_completion.info);
3797

3798
		tx_done.msdu_id = __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
3799 3800 3801

		switch (status) {
		case HTT_MGMT_TX_STATUS_OK:
3802
			tx_done.status = HTT_TX_COMPL_STATE_ACK;
3803 3804 3805 3806 3807 3808 3809 3810
			if (test_bit(WMI_SERVICE_HTT_MGMT_TX_COMP_VALID_FLAGS,
				     ar->wmi.svc_map) &&
			    (resp->mgmt_tx_completion.flags &
			     HTT_MGMT_TX_CMPL_FLAG_ACK_RSSI)) {
				tx_done.ack_rssi =
				FIELD_GET(HTT_MGMT_TX_CMPL_INFO_ACK_RSSI_MASK,
					  info);
			}
3811 3812
			break;
		case HTT_MGMT_TX_STATUS_RETRY:
3813
			tx_done.status = HTT_TX_COMPL_STATE_NOACK;
3814 3815
			break;
		case HTT_MGMT_TX_STATUS_DROP:
3816
			tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
3817 3818 3819
			break;
		}

3820 3821 3822 3823 3824 3825
		if (htt->disable_tx_comp) {
			spin_lock_bh(&htc->tx_lock);
			ep->tx_credits++;
			spin_unlock_bh(&htc->tx_lock);
		}

3826 3827 3828 3829 3830 3831
		status = ath10k_txrx_tx_unref(htt, &tx_done);
		if (!status) {
			spin_lock_bh(&htt->tx_lock);
			ath10k_htt_tx_mgmt_dec_pending(htt);
			spin_unlock_bh(&htt->tx_lock);
		}
3832 3833
		break;
	}
3834
	case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
3835 3836
		ath10k_htt_rx_tx_compl_ind(htt->ar, skb);
		break;
3837 3838 3839 3840
	case HTT_T2H_MSG_TYPE_SEC_IND: {
		struct ath10k *ar = htt->ar;
		struct htt_security_indication *ev = &resp->security_indication;

3841
		ath10k_htt_rx_sec_ind_handler(ar, ev);
3842
		ath10k_dbg(ar, ATH10K_DBG_HTT,
3843 3844 3845 3846 3847 3848 3849 3850
			   "sec ind peer_id %d unicast %d type %d\n",
			  __le16_to_cpu(ev->peer_id),
			  !!(ev->flags & HTT_SECURITY_IS_UNICAST),
			  MS(ev->flags, HTT_SECURITY_TYPE));
		complete(&ar->install_key_done);
		break;
	}
	case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
3851
		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
3852
				skb->data, skb->len);
3853
		atomic_inc(&htt->num_mpdus_ready);
3854 3855 3856 3857

		return ath10k_htt_rx_proc_rx_frag_ind(htt,
						      &resp->rx_frag_ind,
						      skb);
3858 3859 3860 3861 3862
		break;
	}
	case HTT_T2H_MSG_TYPE_TEST:
		break;
	case HTT_T2H_MSG_TYPE_STATS_CONF:
3863
		trace_ath10k_htt_stats(ar, skb->data, skb->len);
3864 3865
		break;
	case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
3866 3867 3868 3869 3870
		/* Firmware can return tx frames if it's unable to fully
		 * process them and suspects host may be able to fix it. ath10k
		 * sends all tx frames as already inspected so this shouldn't
		 * happen unless fw has a bug.
		 */
3871
		ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
3872
		break;
3873
	case HTT_T2H_MSG_TYPE_RX_ADDBA:
3874 3875
		ath10k_htt_rx_addba(ar, resp);
		break;
3876
	case HTT_T2H_MSG_TYPE_RX_DELBA:
3877 3878
		ath10k_htt_rx_delba(ar, resp);
		break;
3879 3880
	case HTT_T2H_MSG_TYPE_PKTLOG: {
		trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
3881 3882 3883
					skb->len -
					offsetof(struct htt_resp,
						 pktlog_msg.payload));
3884 3885 3886 3887

		if (ath10k_peer_stats_enabled(ar))
			ath10k_fetch_10_2_tx_stats(ar,
						   resp->pktlog_msg.payload);
3888 3889
		break;
	}
3890 3891 3892 3893 3894 3895
	case HTT_T2H_MSG_TYPE_RX_FLUSH: {
		/* Ignore this event because mac80211 takes care of Rx
		 * aggregation reordering.
		 */
		break;
	}
3896
	case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
3897
		skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
3898
		return false;
3899
	}
3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921
	case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND: {
		struct ath10k_htt *htt = &ar->htt;
		struct ath10k_htc *htc = &ar->htc;
		struct ath10k_htc_ep *ep = &ar->htc.endpoint[htt->eid];
		u32 msg_word = __le32_to_cpu(*(__le32 *)resp);
		int htt_credit_delta;

		htt_credit_delta = HTT_TX_CREDIT_DELTA_ABS_GET(msg_word);
		if (HTT_TX_CREDIT_SIGN_BIT_GET(msg_word))
			htt_credit_delta = -htt_credit_delta;

		ath10k_dbg(ar, ATH10K_DBG_HTT,
			   "htt credit update delta %d\n",
			   htt_credit_delta);

		if (htt->disable_tx_comp) {
			spin_lock_bh(&htc->tx_lock);
			ep->tx_credits += htt_credit_delta;
			spin_unlock_bh(&htc->tx_lock);
			ath10k_dbg(ar, ATH10K_DBG_HTT,
				   "htt credit total %d\n",
				   ep->tx_credits);
3922
			ep->ep_ops.ep_tx_credits(htc->ar);
3923
		}
3924
		break;
3925
	}
3926 3927 3928 3929
	case HTT_T2H_MSG_TYPE_CHAN_CHANGE: {
		u32 phymode = __le32_to_cpu(resp->chan_change.phymode);
		u32 freq = __le32_to_cpu(resp->chan_change.freq);

3930
		ar->tgt_oper_chan = ieee80211_get_channel(ar->hw->wiphy, freq);
3931 3932 3933
		ath10k_dbg(ar, ATH10K_DBG_HTT,
			   "htt chan change freq %u phymode %s\n",
			   freq, ath10k_wmi_phymode_str(phymode));
3934
		break;
3935
	}
3936 3937
	case HTT_T2H_MSG_TYPE_AGGR_CONF:
		break;
3938 3939 3940 3941 3942 3943 3944 3945 3946 3947
	case HTT_T2H_MSG_TYPE_TX_FETCH_IND: {
		struct sk_buff *tx_fetch_ind = skb_copy(skb, GFP_ATOMIC);

		if (!tx_fetch_ind) {
			ath10k_warn(ar, "failed to copy htt tx fetch ind\n");
			break;
		}
		skb_queue_tail(&htt->tx_fetch_ind_q, tx_fetch_ind);
		break;
	}
3948
	case HTT_T2H_MSG_TYPE_TX_FETCH_CONFIRM:
3949 3950
		ath10k_htt_rx_tx_fetch_confirm(ar, skb);
		break;
3951
	case HTT_T2H_MSG_TYPE_TX_MODE_SWITCH_IND:
3952
		ath10k_htt_rx_tx_mode_switch_ind(ar, skb);
3953
		break;
3954 3955 3956
	case HTT_T2H_MSG_TYPE_PEER_STATS:
		ath10k_htt_fetch_peer_stats(ar, skb);
		break;
3957
	case HTT_T2H_MSG_TYPE_EN_STATS:
3958
	default:
3959 3960
		ath10k_warn(ar, "htt event (%d) not handled\n",
			    resp->hdr.msg_type);
3961
		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
3962 3963
				skb->data, skb->len);
		break;
3964
	}
3965
	return true;
3966
}
3967
EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
3968

3969 3970 3971
void ath10k_htt_rx_pktlog_completion_handler(struct ath10k *ar,
					     struct sk_buff *skb)
{
3972
	trace_ath10k_htt_pktlog(ar, skb->data, skb->len);
3973 3974 3975 3976
	dev_kfree_skb_any(skb);
}
EXPORT_SYMBOL(ath10k_htt_rx_pktlog_completion_handler);

3977 3978 3979 3980 3981 3982 3983 3984
static int ath10k_htt_rx_deliver_msdu(struct ath10k *ar, int quota, int budget)
{
	struct sk_buff *skb;

	while (quota < budget) {
		if (skb_queue_empty(&ar->htt.rx_msdus_q))
			break;

3985
		skb = skb_dequeue(&ar->htt.rx_msdus_q);
3986 3987 3988 3989 3990 3991 3992 3993 3994
		if (!skb)
			break;
		ath10k_process_rx(ar, skb);
		quota++;
	}

	return quota;
}

3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025
int ath10k_htt_rx_hl_indication(struct ath10k *ar, int budget)
{
	struct htt_resp *resp;
	struct ath10k_htt *htt = &ar->htt;
	struct sk_buff *skb;
	bool release;
	int quota;

	for (quota = 0; quota < budget; quota++) {
		skb = skb_dequeue(&htt->rx_indication_head);
		if (!skb)
			break;

		resp = (struct htt_resp *)skb->data;

		release = ath10k_htt_rx_proc_rx_ind_hl(htt,
						       &resp->rx_ind_hl,
						       skb,
						       HTT_RX_PN_CHECK,
						       HTT_RX_NON_TKIP_MIC);

		if (release)
			dev_kfree_skb_any(skb);

		ath10k_dbg(ar, ATH10K_DBG_HTT, "rx indication poll pending count:%d\n",
			   skb_queue_len(&htt->rx_indication_head));
	}
	return quota;
}
EXPORT_SYMBOL(ath10k_htt_rx_hl_indication);

4026
int ath10k_htt_txrx_compl_task(struct ath10k *ar, int budget)
4027
{
4028
	struct ath10k_htt *htt = &ar->htt;
4029
	struct htt_tx_done tx_done = {};
4030
	struct sk_buff_head tx_ind_q;
4031
	struct sk_buff *skb;
4032
	unsigned long flags;
4033
	int quota = 0, done, ret;
4034
	bool resched_napi = false;
4035

4036
	__skb_queue_head_init(&tx_ind_q);
4037

4038 4039
	/* Process pending frames before dequeuing more data
	 * from hardware.
4040
	 */
4041 4042 4043 4044 4045
	quota = ath10k_htt_rx_deliver_msdu(ar, quota, budget);
	if (quota == budget) {
		resched_napi = true;
		goto exit;
	}
4046

4047
	while ((skb = skb_dequeue(&htt->rx_in_ord_compl_q))) {
4048
		spin_lock_bh(&htt->rx_ring.lock);
4049
		ret = ath10k_htt_rx_in_ord_ind(ar, skb);
4050 4051 4052
		spin_unlock_bh(&htt->rx_ring.lock);

		dev_kfree_skb_any(skb);
4053
		if (ret == -EIO) {
4054 4055 4056 4057 4058
			resched_napi = true;
			goto exit;
		}
	}

4059 4060 4061
	while (atomic_read(&htt->num_mpdus_ready)) {
		ret = ath10k_htt_rx_handle_amsdu(htt);
		if (ret == -EIO) {
4062 4063 4064 4065 4066 4067
			resched_napi = true;
			goto exit;
		}
		atomic_dec(&htt->num_mpdus_ready);
	}

4068 4069 4070
	/* Deliver received data after processing data from hardware */
	quota = ath10k_htt_rx_deliver_msdu(ar, quota, budget);

4071 4072 4073 4074 4075 4076 4077
	/* From NAPI documentation:
	 *  The napi poll() function may also process TX completions, in which
	 *  case if it processes the entire TX ring then it should count that
	 *  work as the rest of the budget.
	 */
	if ((quota < budget) && !kfifo_is_empty(&htt->txdone_fifo))
		quota = budget;
4078

4079 4080 4081 4082 4083 4084 4085
	/* kfifo_get: called only within txrx_tasklet so it's neatly serialized.
	 * From kfifo_get() documentation:
	 *  Note that with only one concurrent reader and one concurrent writer,
	 *  you don't need extra locking to use these macro.
	 */
	while (kfifo_get(&htt->txdone_fifo, &tx_done))
		ath10k_txrx_tx_unref(htt, &tx_done);
4086

4087 4088
	ath10k_mac_tx_push_pending(ar);

4089 4090 4091 4092
	spin_lock_irqsave(&htt->tx_fetch_ind_q.lock, flags);
	skb_queue_splice_init(&htt->tx_fetch_ind_q, &tx_ind_q);
	spin_unlock_irqrestore(&htt->tx_fetch_ind_q.lock, flags);

4093 4094
	while ((skb = __skb_dequeue(&tx_ind_q))) {
		ath10k_htt_rx_tx_fetch_ind(ar, skb);
4095 4096 4097
		dev_kfree_skb_any(skb);
	}

4098
exit:
4099
	ath10k_htt_rx_msdu_buff_replenish(htt);
4100 4101 4102 4103 4104 4105
	/* In case of rx failure or more data to read, report budget
	 * to reschedule NAPI poll
	 */
	done = resched_napi ? budget : quota;

	return done;
4106
}
4107
EXPORT_SYMBOL(ath10k_htt_txrx_compl_task);
4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124

static const struct ath10k_htt_rx_ops htt_rx_ops_32 = {
	.htt_get_rx_ring_size = ath10k_htt_get_rx_ring_size_32,
	.htt_config_paddrs_ring = ath10k_htt_config_paddrs_ring_32,
	.htt_set_paddrs_ring = ath10k_htt_set_paddrs_ring_32,
	.htt_get_vaddr_ring = ath10k_htt_get_vaddr_ring_32,
	.htt_reset_paddrs_ring = ath10k_htt_reset_paddrs_ring_32,
};

static const struct ath10k_htt_rx_ops htt_rx_ops_64 = {
	.htt_get_rx_ring_size = ath10k_htt_get_rx_ring_size_64,
	.htt_config_paddrs_ring = ath10k_htt_config_paddrs_ring_64,
	.htt_set_paddrs_ring = ath10k_htt_set_paddrs_ring_64,
	.htt_get_vaddr_ring = ath10k_htt_get_vaddr_ring_64,
	.htt_reset_paddrs_ring = ath10k_htt_reset_paddrs_ring_64,
};

Erik Stromdahl's avatar
Erik Stromdahl committed
4125
static const struct ath10k_htt_rx_ops htt_rx_ops_hl = {
4126
	.htt_rx_proc_rx_frag_ind = ath10k_htt_rx_proc_rx_frag_ind_hl,
Erik Stromdahl's avatar
Erik Stromdahl committed
4127 4128
};

4129 4130 4131 4132
void ath10k_htt_set_rx_ops(struct ath10k_htt *htt)
{
	struct ath10k *ar = htt->ar;

4133
	if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
Erik Stromdahl's avatar
Erik Stromdahl committed
4134 4135
		htt->rx_ops = &htt_rx_ops_hl;
	else if (ar->hw_params.target_64bit)
4136 4137 4138 4139
		htt->rx_ops = &htt_rx_ops_64;
	else
		htt->rx_ops = &htt_rx_ops_32;
}