htt_rx.c 70.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2005-2011 Atheros Communications Inc.
 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

18
#include "core.h"
19 20 21 22
#include "htc.h"
#include "htt.h"
#include "txrx.h"
#include "debug.h"
23
#include "trace.h"
24
#include "mac.h"
25 26 27

#include <linux/log2.h>

28 29
#define HTT_RX_RING_SIZE HTT_RX_RING_SIZE_MAX
#define HTT_RX_RING_FILL_LEVEL (((HTT_RX_RING_SIZE) / 2) - 1)
30 31 32 33

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

34 35
#define HTT_RX_RING_REFILL_RESCHED_MS 5

36 37
static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);

38 39 40 41 42 43 44 45 46 47 48 49 50
static struct sk_buff *
ath10k_htt_rx_find_skb_paddr(struct ath10k *ar, u32 paddr)
{
	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;
}

51 52 53
static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
{
	struct sk_buff *skb;
54 55
	struct ath10k_skb_rxcb *rxcb;
	struct hlist_node *n;
56 57
	int i;

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	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);
		}
79 80 81
	}

	htt->rx_ring.fill_cnt = 0;
82 83 84
	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]));
85 86 87 88 89
}

static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
{
	struct htt_rx_desc *rx_desc;
90
	struct ath10k_skb_rxcb *rxcb;
91 92 93 94
	struct sk_buff *skb;
	dma_addr_t paddr;
	int ret = 0, idx;

95 96 97 98 99 100 101
	/* 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);

102
	idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
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
	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;
		}

129 130
		rxcb = ATH10K_SKB_RXCB(skb);
		rxcb->paddr = paddr;
131 132 133 134
		htt->rx_ring.netbufs_ring[idx] = skb;
		htt->rx_ring.paddrs_ring[idx] = __cpu_to_le32(paddr);
		htt->rx_ring.fill_cnt++;

135 136 137 138 139 140
		if (htt->rx_ring.in_ord_rx) {
			hash_add(htt->rx_ring.skb_table,
				 &ATH10K_SKB_RXCB(skb)->hlist,
				 (u32)paddr);
		}

141 142 143 144 145 146
		num--;
		idx++;
		idx &= htt->rx_ring.size_mask;
	}

fail:
147 148 149 150 151
	/*
	 * Make sure the rx buffer is updated before available buffer
	 * index to avoid any potential rx ring corruption.
	 */
	mb();
152
	*htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
153 154 155 156 157 158 159 160 161 162 163
	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)
{
164
	int ret, num_deficit, num_to_fill;
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179
	/* 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
180 181
	 * improves the average and stability.
	 */
182
	spin_lock_bh(&htt->rx_ring.lock);
183 184 185
	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;
186 187 188 189 190 191 192 193 194 195
	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));
196
	} else if (num_deficit > 0) {
197 198
		mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
			  msecs_to_jiffies(HTT_RX_RING_REFILL_RESCHED_MS));
199 200 201 202 203 204 205
	}
	spin_unlock_bh(&htt->rx_ring.lock);
}

static void ath10k_htt_rx_ring_refill_retry(unsigned long arg)
{
	struct ath10k_htt *htt = (struct ath10k_htt *)arg;
206

207 208 209
	ath10k_htt_rx_msdu_buff_replenish(htt);
}

210
int ath10k_htt_rx_ring_refill(struct ath10k *ar)
211
{
212 213
	struct ath10k_htt *htt = &ar->htt;
	int ret;
M
Michal Kazior 已提交
214

215 216 217 218
	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));
	spin_unlock_bh(&htt->rx_ring.lock);
M
Michal Kazior 已提交
219

220 221 222 223
	if (ret)
		ath10k_htt_rx_ring_free(htt);

	return ret;
M
Michal Kazior 已提交
224
}
225

M
Michal Kazior 已提交
226
void ath10k_htt_rx_free(struct ath10k_htt *htt)
M
Michal Kazior 已提交
227
{
228
	del_timer_sync(&htt->rx_ring.refill_retry_timer);
229 230

	skb_queue_purge(&htt->rx_compl_q);
231
	skb_queue_purge(&htt->rx_in_ord_compl_q);
M
Michal Kazior 已提交
232
	skb_queue_purge(&htt->tx_fetch_ind_q);
233

234
	ath10k_htt_rx_ring_free(htt);
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

	dma_free_coherent(htt->ar->dev,
			  (htt->rx_ring.size *
			   sizeof(htt->rx_ring.paddrs_ring)),
			  htt->rx_ring.paddrs_ring,
			  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)
{
252
	struct ath10k *ar = htt->ar;
253 254 255
	int idx;
	struct sk_buff *msdu;

256
	lockdep_assert_held(&htt->rx_ring.lock);
257

258
	if (htt->rx_ring.fill_cnt == 0) {
259
		ath10k_warn(ar, "tried to pop sk_buff from an empty rx ring\n");
260 261
		return NULL;
	}
262 263 264

	idx = htt->rx_ring.sw_rd_idx.msdu_payld;
	msdu = htt->rx_ring.netbufs_ring[idx];
M
Michal Kazior 已提交
265
	htt->rx_ring.netbufs_ring[idx] = NULL;
266
	htt->rx_ring.paddrs_ring[idx] = 0;
267 268 269 270 271 272

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

273
	dma_unmap_single(htt->ar->dev,
274
			 ATH10K_SKB_RXCB(msdu)->paddr,
275 276 277 278 279
			 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));

280 281 282
	return msdu;
}

283
/* return: < 0 fatal error, 0 - non chained msdu, 1 chained msdu */
284
static int ath10k_htt_rx_amsdu_pop(struct ath10k_htt *htt,
285
				   struct sk_buff_head *amsdu)
286
{
287
	struct ath10k *ar = htt->ar;
288
	int msdu_len, msdu_chaining = 0;
M
Michal Kazior 已提交
289
	struct sk_buff *msdu;
290 291
	struct htt_rx_desc *rx_desc;

292 293
	lockdep_assert_held(&htt->rx_ring.lock);

M
Michal Kazior 已提交
294
	for (;;) {
295 296
		int last_msdu, msdu_len_invalid, msdu_chained;

M
Michal Kazior 已提交
297 298 299
		msdu = ath10k_htt_rx_netbuf_pop(htt);
		if (!msdu) {
			__skb_queue_purge(amsdu);
M
Michal Kazior 已提交
300
			return -ENOENT;
M
Michal Kazior 已提交
301 302 303 304
		}

		__skb_queue_tail(amsdu, msdu);

305 306 307
		rx_desc = (struct htt_rx_desc *)msdu->data;

		/* FIXME: we must report msdu payload since this is what caller
308 309
		 * expects now
		 */
310 311 312 313 314 315 316 317 318 319 320 321 322
		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)) {
M
Michal Kazior 已提交
323
			__skb_queue_purge(amsdu);
M
Michal Kazior 已提交
324
			return -EIO;
325 326 327 328 329
		}

		msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
					& (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
					   RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
330
		msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.common.info0),
331 332 333 334 335 336 337 338 339 340
			      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;

M
Michal Kazior 已提交
341
		/* Note: Chained buffers do not contain rx descriptor */
342
		while (msdu_chained--) {
M
Michal Kazior 已提交
343 344 345
			msdu = ath10k_htt_rx_netbuf_pop(htt);
			if (!msdu) {
				__skb_queue_purge(amsdu);
M
Michal Kazior 已提交
346
				return -ENOENT;
347 348
			}

M
Michal Kazior 已提交
349 350 351 352
			__skb_queue_tail(amsdu, msdu);
			skb_trim(msdu, 0);
			skb_put(msdu, min(msdu_len, HTT_RX_BUF_SIZE));
			msdu_len -= msdu->len;
353
			msdu_chaining = 1;
354 355
		}

356
		last_msdu = __le32_to_cpu(rx_desc->msdu_end.common.info0) &
357 358
				RX_MSDU_END_INFO0_LAST_MSDU;

359
		trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
360
					 sizeof(*rx_desc) - sizeof(u32));
361

M
Michal Kazior 已提交
362 363
		if (last_msdu)
			break;
364 365
	}

M
Michal Kazior 已提交
366
	if (skb_queue_empty(amsdu))
367 368
		msdu_chaining = -1;

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	/*
	 * 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;
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
					       u32 paddr)
{
	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;
}

static int ath10k_htt_rx_pop_paddr_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 *msdu_desc = ev->msdu_descs;
	struct htt_rx_desc *rxd;
	struct sk_buff *msdu;
	int msdu_count;
	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;
		}

		__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;
}

M
Michal Kazior 已提交
461
int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
462
{
463
	struct ath10k *ar = htt->ar;
464 465
	dma_addr_t paddr;
	void *vaddr;
466
	size_t size;
467 468
	struct timer_list *timer = &htt->rx_ring.refill_retry_timer;

469 470
	htt->rx_confused = false;

471 472 473 474 475 476 477
	/* 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;
	htt->rx_ring.fill_level = HTT_RX_RING_FILL_LEVEL;

478
	if (!is_power_of_2(htt->rx_ring.size)) {
479
		ath10k_warn(ar, "htt rx ring size is not power of 2\n");
480 481 482 483
		return -EINVAL;
	}

	htt->rx_ring.netbufs_ring =
M
Michal Kazior 已提交
484
		kzalloc(htt->rx_ring.size * sizeof(struct sk_buff *),
485 486 487 488
			GFP_KERNEL);
	if (!htt->rx_ring.netbufs_ring)
		goto err_netbuf;

489 490
	size = htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring);

F
Felix Fietkau 已提交
491
	vaddr = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_KERNEL);
492 493 494 495 496 497 498 499
	if (!vaddr)
		goto err_dma_ring;

	htt->rx_ring.paddrs_ring = vaddr;
	htt->rx_ring.base_paddr = paddr;

	vaddr = dma_alloc_coherent(htt->ar->dev,
				   sizeof(*htt->rx_ring.alloc_idx.vaddr),
F
Felix Fietkau 已提交
500
				   &paddr, GFP_KERNEL);
501 502 503 504 505
	if (!vaddr)
		goto err_dma_idx;

	htt->rx_ring.alloc_idx.vaddr = vaddr;
	htt->rx_ring.alloc_idx.paddr = paddr;
506
	htt->rx_ring.sw_rd_idx.msdu_payld = htt->rx_ring.size_mask;
507 508 509 510 511 512 513 514
	*htt->rx_ring.alloc_idx.vaddr = 0;

	/* Initialize the Rx refill retry timer */
	setup_timer(timer, ath10k_htt_rx_ring_refill_retry, (unsigned long)htt);

	spin_lock_init(&htt->rx_ring.lock);

	htt->rx_ring.fill_cnt = 0;
515 516
	htt->rx_ring.sw_rd_idx.msdu_payld = 0;
	hash_init(htt->rx_ring.skb_table);
517

518
	skb_queue_head_init(&htt->rx_compl_q);
519
	skb_queue_head_init(&htt->rx_in_ord_compl_q);
M
Michal Kazior 已提交
520
	skb_queue_head_init(&htt->tx_fetch_ind_q);
521
	atomic_set(&htt->num_mpdus_ready, 0);
522

523
	ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
		   htt->rx_ring.size, htt->rx_ring.fill_level);
	return 0;

err_dma_idx:
	dma_free_coherent(htt->ar->dev,
			  (htt->rx_ring.size *
			   sizeof(htt->rx_ring.paddrs_ring)),
			  htt->rx_ring.paddrs_ring,
			  htt->rx_ring.base_paddr);
err_dma_ring:
	kfree(htt->rx_ring.netbufs_ring);
err_netbuf:
	return -ENOMEM;
}

539 540
static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
					  enum htt_rx_mpdu_encrypt_type type)
541 542
{
	switch (type) {
543 544
	case HTT_RX_MPDU_ENCRYPT_NONE:
		return 0;
545 546
	case HTT_RX_MPDU_ENCRYPT_WEP40:
	case HTT_RX_MPDU_ENCRYPT_WEP104:
547
		return IEEE80211_WEP_IV_LEN;
548 549
	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
550
		return IEEE80211_TKIP_IV_LEN;
551
	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
552 553 554 555
		return IEEE80211_CCMP_HDR_LEN;
	case HTT_RX_MPDU_ENCRYPT_WEP128:
	case HTT_RX_MPDU_ENCRYPT_WAPI:
		break;
556 557
	}

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

562 563
#define MICHAEL_MIC_LEN 8

564 565
static int ath10k_htt_rx_crypto_tail_len(struct ath10k *ar,
					 enum htt_rx_mpdu_encrypt_type type)
566 567 568
{
	switch (type) {
	case HTT_RX_MPDU_ENCRYPT_NONE:
569
		return 0;
570 571
	case HTT_RX_MPDU_ENCRYPT_WEP40:
	case HTT_RX_MPDU_ENCRYPT_WEP104:
572
		return IEEE80211_WEP_ICV_LEN;
573 574
	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
575
		return IEEE80211_TKIP_ICV_LEN;
576
	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
577 578 579 580
		return IEEE80211_CCMP_MIC_LEN;
	case HTT_RX_MPDU_ENCRYPT_WEP128:
	case HTT_RX_MPDU_ENCRYPT_WAPI:
		break;
581 582
	}

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

587 588 589 590 591 592
struct amsdu_subframe_hdr {
	u8 dst[ETH_ALEN];
	u8 src[ETH_ALEN];
	__be16 len;
} __packed;

593 594
#define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)

595
static void ath10k_htt_rx_h_rates(struct ath10k *ar,
596 597
				  struct ieee80211_rx_status *status,
				  struct htt_rx_desc *rxd)
598
{
599 600
	struct ieee80211_supported_band *sband;
	u8 cck, rate, bw, sgi, mcs, nss;
601
	u8 preamble = 0;
602
	u8 group_id;
603
	u32 info1, info2, info3;
604

605 606 607 608 609
	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);
610 611 612

	switch (preamble) {
	case HTT_RX_LEGACY:
613 614 615 616 617 618
		/* To get legacy rate index band is required. Since band can't
		 * be undefined check if freq is non-zero.
		 */
		if (!status->freq)
			return;

619 620
		cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
		rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
621
		rate &= ~RX_PPDU_START_RATE_FLAG;
622

623
		sband = &ar->mac.sbands[status->band];
624
		status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate, cck);
625 626 627
		break;
	case HTT_RX_HT:
	case HTT_RX_HT_WITH_TXBF:
628 629
		/* HT-SIG - Table 20-11 in info2 and info3 */
		mcs = info2 & 0x1F;
630
		nss = mcs >> 3;
631 632
		bw = (info2 >> 7) & 1;
		sgi = (info3 >> 7) & 1;
633 634

		status->rate_idx = mcs;
635
		status->encoding = RX_ENC_HT;
636
		if (sgi)
637
			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
638
		if (bw)
639
			status->bw = RATE_INFO_BW_40;
640 641 642
		break;
	case HTT_RX_VHT:
	case HTT_RX_VHT_WITH_TXBF:
643
		/* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
644 645
		 * TODO check this
		 */
646 647
		bw = info2 & 3;
		sgi = info3 & 1;
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
		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;
		}
667

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
		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);
		}

691
		status->rate_idx = mcs;
692
		status->nss = nss;
693 694

		if (sgi)
695
			status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
696 697 698 699 700 701 702

		switch (bw) {
		/* 20MHZ */
		case 0:
			break;
		/* 40MHZ */
		case 1:
703
			status->bw = RATE_INFO_BW_40;
704 705 706
			break;
		/* 80MHZ */
		case 2:
707
			status->bw = RATE_INFO_BW_80;
708 709
			break;
		case 3:
710
			status->bw = RATE_INFO_BW_160;
711
			break;
712 713
		}

714
		status->encoding = RX_ENC_VHT;
715 716 717 718 719 720
		break;
	default:
		break;
	}
}

M
Michal Kazior 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
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;

738
	if (!(rxd->msdu_end.common.info0 &
M
Michal Kazior 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751 752
	      __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;

753
	if (ath10k_mac_vif_chan(arvif->vif, &def))
M
Michal Kazior 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
		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;
}

798
static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
M
Michal Kazior 已提交
799 800 801
				    struct ieee80211_rx_status *status,
				    struct htt_rx_desc *rxd,
				    u32 vdev_id)
802 803 804 805 806 807 808
{
	struct ieee80211_channel *ch;

	spin_lock_bh(&ar->data_lock);
	ch = ar->scan_channel;
	if (!ch)
		ch = ar->rx_channel;
M
Michal Kazior 已提交
809 810 811 812 813 814
	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);
815 816
	if (!ch)
		ch = ar->tgt_oper_chan;
817 818 819 820 821 822 823 824 825 826 827
	spin_unlock_bh(&ar->data_lock);

	if (!ch)
		return false;

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

	return true;
}

828 829 830 831
static void ath10k_htt_rx_h_signal(struct ath10k *ar,
				   struct ieee80211_rx_status *status,
				   struct htt_rx_desc *rxd)
{
832 833 834 835 836 837 838 839 840 841 842 843 844
	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);
		}
	}

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	/* 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?
	 */
861
	status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
862 863 864 865 866
	status->flag |= RX_FLAG_MACTIME_END;
}

static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
				 struct sk_buff_head *amsdu,
M
Michal Kazior 已提交
867 868
				 struct ieee80211_rx_status *status,
				 u32 vdev_id)
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
{
	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;
890
		status->nss = 0;
891 892
		status->encoding = RX_ENC_LEGACY;
		status->bw = RATE_INFO_BW_20;
893
		status->flag &= ~RX_FLAG_MACTIME_END;
894 895 896
		status->flag |= RX_FLAG_NO_SIGNAL_VAL;

		ath10k_htt_rx_h_signal(ar, status, rxd);
M
Michal Kazior 已提交
897
		ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
898 899 900 901 902 903 904
		ath10k_htt_rx_h_rates(ar, status, rxd);
	}

	if (is_last_ppdu)
		ath10k_htt_rx_h_mactime(ar, status, rxd);
}

905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
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;
}

934 935 936
static void ath10k_process_rx(struct ath10k *ar,
			      struct ieee80211_rx_status *rx_status,
			      struct sk_buff *skb)
937 938
{
	struct ieee80211_rx_status *status;
939 940
	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
	char tid[32];
941

942 943
	status = IEEE80211_SKB_RXCB(skb);
	*status = *rx_status;
944

945
	ath10k_dbg(ar, ATH10K_DBG_DATA,
946
		   "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",
947 948
		   skb,
		   skb->len,
949 950 951 952 953
		   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,
954 955 956 957 958 959
		   (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" : "",
960
		   status->enc_flags & RX_ENC_FLAG_SHORT_GI ? "sgi " : "",
961
		   status->rate_idx,
962
		   status->nss,
963
		   status->freq,
964
		   status->band, status->flag,
965
		   !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
966 967
		   !!(status->flag & RX_FLAG_MMIC_ERROR),
		   !!(status->flag & RX_FLAG_AMSDU_MORE));
968
	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
969
			skb->data, skb->len);
970 971
	trace_ath10k_rx_hdr(ar, skb->data, skb->len);
	trace_ath10k_rx_payload(ar, skb->data, skb->len);
972

973
	ieee80211_rx_napi(ar->hw, NULL, skb, &ar->napi);
974 975
}

976 977
static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
				      struct ieee80211_hdr *hdr)
M
Michal Kazior 已提交
978
{
979 980 981
	int len = ieee80211_hdrlen(hdr->frame_control);

	if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
982
		      ar->running_fw->fw_file.fw_features))
983 984 985
		len = round_up(len, 4);

	return len;
M
Michal Kazior 已提交
986 987
}

M
Michal Kazior 已提交
988 989 990 991 992
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,
					bool is_decrypted)
993
{
M
Michal Kazior 已提交
994
	struct ieee80211_hdr *hdr;
995
	struct htt_rx_desc *rxd;
M
Michal Kazior 已提交
996 997 998 999 1000 1001
	size_t hdr_len;
	size_t crypto_len;
	bool is_first;
	bool is_last;

	rxd = (void *)msdu->data - sizeof(*rxd);
1002
	is_first = !!(rxd->msdu_end.common.info0 &
M
Michal Kazior 已提交
1003
		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
1004
	is_last = !!(rxd->msdu_end.common.info0 &
M
Michal Kazior 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
		     __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
	 */

	/* This probably shouldn't happen but warn just in case */
	if (unlikely(WARN_ON_ONCE(!is_first)))
		return;

	/* This probably shouldn't happen but warn just in case */
	if (unlikely(WARN_ON_ONCE(!(is_first && is_last))))
		return;

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

	/* In most cases this will be true for sniffed frames. It makes sense
1028 1029
	 * to deliver them as-is without stripping the crypto param. This is
	 * necessary for software based decryption.
M
Michal Kazior 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	 *
	 * 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 */
1044 1045 1046
	if (status->flag & RX_FLAG_IV_STRIPPED)
		skb_trim(msdu, msdu->len -
			 ath10k_htt_rx_crypto_tail_len(ar, enctype));
M
Michal Kazior 已提交
1047 1048

	/* MMIC */
1049 1050
	if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
	    !ieee80211_has_morefrags(hdr->frame_control) &&
M
Michal Kazior 已提交
1051 1052 1053 1054
	    enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
		skb_trim(msdu, msdu->len - 8);

	/* Head */
1055 1056 1057
	if (status->flag & RX_FLAG_IV_STRIPPED) {
		hdr_len = ieee80211_hdrlen(hdr->frame_control);
		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
M
Michal Kazior 已提交
1058

1059 1060 1061 1062
		memmove((void *)msdu->data + crypto_len,
			(void *)msdu->data, hdr_len);
		skb_pull(msdu, crypto_len);
	}
M
Michal Kazior 已提交
1063 1064 1065 1066 1067 1068 1069
}

static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
					  struct sk_buff *msdu,
					  struct ieee80211_rx_status *status,
					  const u8 first_hdr[64])
{
1070
	struct ieee80211_hdr *hdr;
1071
	struct htt_rx_desc *rxd;
M
Michal Kazior 已提交
1072 1073 1074
	size_t hdr_len;
	u8 da[ETH_ALEN];
	u8 sa[ETH_ALEN];
1075
	int l3_pad_bytes;
1076

M
Michal Kazior 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
	/* 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.
	 */
M
Michal Kazior 已提交
1087

M
Michal Kazior 已提交
1088
	/* pull decapped header and copy SA & DA */
1089 1090 1091 1092 1093 1094
	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);
1095

1096
	hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
M
Michal Kazior 已提交
1097 1098 1099
	ether_addr_copy(da, ieee80211_get_DA(hdr));
	ether_addr_copy(sa, ieee80211_get_SA(hdr));
	skb_pull(msdu, hdr_len);
1100

M
Michal Kazior 已提交
1101 1102
	/* push original 802.11 header */
	hdr = (struct ieee80211_hdr *)first_hdr;
1103
	hdr_len = ieee80211_hdrlen(hdr->frame_control);
M
Michal Kazior 已提交
1104
	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1105

M
Michal Kazior 已提交
1106 1107 1108 1109 1110 1111 1112
	/* 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);
}
1113

M
Michal Kazior 已提交
1114 1115 1116 1117 1118 1119 1120 1121 1122
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;
1123
	int bytes_aligned = ar->hw_params.decap_align_bytes;
1124

M
Michal Kazior 已提交
1125 1126
	rxd = (void *)msdu->data - sizeof(*rxd);
	hdr = (void *)rxd->rx_hdr_status;
1127

1128
	is_first = !!(rxd->msdu_end.common.info0 &
M
Michal Kazior 已提交
1129
		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
1130
	is_last = !!(rxd->msdu_end.common.info0 &
M
Michal Kazior 已提交
1131 1132
		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
	is_amsdu = !(is_first && is_last);
1133

M
Michal Kazior 已提交
1134
	rfc1042 = hdr;
1135

M
Michal Kazior 已提交
1136 1137 1138
	if (is_first) {
		hdr_len = ieee80211_hdrlen(hdr->frame_control);
		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1139

1140 1141
		rfc1042 += round_up(hdr_len, bytes_aligned) +
			   round_up(crypto_len, bytes_aligned);
1142
	}
1143

M
Michal Kazior 已提交
1144 1145 1146 1147
	if (is_amsdu)
		rfc1042 += sizeof(struct amsdu_subframe_hdr);

	return rfc1042;
1148 1149
}

M
Michal Kazior 已提交
1150 1151 1152 1153 1154
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)
1155 1156
{
	struct ieee80211_hdr *hdr;
M
Michal Kazior 已提交
1157 1158
	struct ethhdr *eth;
	size_t hdr_len;
1159
	void *rfc1042;
M
Michal Kazior 已提交
1160 1161
	u8 da[ETH_ALEN];
	u8 sa[ETH_ALEN];
1162 1163
	int l3_pad_bytes;
	struct htt_rx_desc *rxd;
1164

M
Michal Kazior 已提交
1165 1166 1167 1168 1169 1170 1171 1172 1173
	/* 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;

1174 1175 1176 1177 1178
	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);

M
Michal Kazior 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
	/* 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);
	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,
					 const u8 first_hdr[64])
{
	struct ieee80211_hdr *hdr;
	size_t hdr_len;
1209 1210
	int l3_pad_bytes;
	struct htt_rx_desc *rxd;
M
Michal Kazior 已提交
1211 1212 1213 1214 1215 1216 1217

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

1218 1219 1220 1221 1222
	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);
M
Michal Kazior 已提交
1223 1224

	hdr = (struct ieee80211_hdr *)first_hdr;
1225
	hdr_len = ieee80211_hdrlen(hdr->frame_control);
M
Michal Kazior 已提交
1226 1227
	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
}
1228

M
Michal Kazior 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237
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;
1238

M
Michal Kazior 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
	/* 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);
1251
	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
M
Michal Kazior 已提交
1252 1253 1254
		   RX_MSDU_START_INFO1_DECAP_FORMAT);

	switch (decap) {
1255
	case RX_MSDU_DECAP_RAW:
M
Michal Kazior 已提交
1256 1257
		ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
					    is_decrypted);
1258 1259
		break;
	case RX_MSDU_DECAP_NATIVE_WIFI:
M
Michal Kazior 已提交
1260
		ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr);
1261 1262
		break;
	case RX_MSDU_DECAP_ETHERNET2_DIX:
M
Michal Kazior 已提交
1263
		ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
1264 1265
		break;
	case RX_MSDU_DECAP_8023_SNAP_LLC:
M
Michal Kazior 已提交
1266
		ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr);
1267
		break;
1268 1269 1270
	}
}

1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
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);
1281
	info = __le32_to_cpu(rxd->msdu_start.common.info1);
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301

	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;
}

M
Michal Kazior 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
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,
				 struct ieee80211_rx_status *status)
{
	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;
	size_t hdr_len;
	bool has_fcs_err;
	bool has_crypto_err;
	bool has_tkip_err;
	bool has_peer_idx_invalid;
	bool is_decrypted;
1325
	bool is_mgmt;
M
Michal Kazior 已提交
1326 1327 1328 1329 1330 1331 1332 1333
	u32 attention;

	if (skb_queue_empty(amsdu))
		return;

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

1334 1335 1336
	is_mgmt = !!(rxd->attention.flags &
		     __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));

M
Michal Kazior 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
	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;
	hdr_len = ieee80211_hdrlen(hdr->frame_control);
	memcpy(first_hdr, hdr, hdr_len);

	/* 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;
	qos = ieee80211_get_qos_ctl(hdr);
	qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;

	/* 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 |
1378
			  RX_FLAG_ONLY_MONITOR |
M
Michal Kazior 已提交
1379 1380 1381 1382 1383 1384 1385 1386
			  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;

1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
	/* 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))
			status->flag |= RX_FLAG_IV_STRIPPED |
					RX_FLAG_MMIC_STRIPPED;
}
M
Michal Kazior 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413

	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;
1414 1415
		if (is_mgmt)
			continue;
M
Michal Kazior 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438

		hdr = (void *)msdu->data;
		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
	}
}

static void ath10k_htt_rx_h_deliver(struct ath10k *ar,
				    struct sk_buff_head *amsdu,
				    struct ieee80211_rx_status *status)
{
	struct sk_buff *msdu;

	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;

		ath10k_process_rx(ar, status, msdu);
	}
}

M
Michal Kazior 已提交
1439
static int ath10k_unchain_msdu(struct sk_buff_head *amsdu)
B
Ben Greear 已提交
1440
{
M
Michal Kazior 已提交
1441
	struct sk_buff *skb, *first;
B
Ben Greear 已提交
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
	int space;
	int total_len = 0;

	/* 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?
	 */

M
Michal Kazior 已提交
1452
	first = __skb_dequeue(amsdu);
B
Ben Greear 已提交
1453 1454

	/* Allocate total length all at once. */
M
Michal Kazior 已提交
1455 1456
	skb_queue_walk(amsdu, skb)
		total_len += skb->len;
B
Ben Greear 已提交
1457

M
Michal Kazior 已提交
1458
	space = total_len - skb_tailroom(first);
B
Ben Greear 已提交
1459
	if ((space > 0) &&
M
Michal Kazior 已提交
1460
	    (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
B
Ben Greear 已提交
1461 1462 1463 1464
		/* TODO:  bump some rx-oom error stat */
		/* put it back together so we can free the
		 * whole list at once.
		 */
M
Michal Kazior 已提交
1465
		__skb_queue_head(amsdu, first);
B
Ben Greear 已提交
1466 1467 1468 1469 1470 1471
		return -1;
	}

	/* Walk list again, copying contents into
	 * msdu_head
	 */
M
Michal Kazior 已提交
1472 1473 1474 1475
	while ((skb = __skb_dequeue(amsdu))) {
		skb_copy_from_linear_data(skb, skb_put(first, skb->len),
					  skb->len);
		dev_kfree_skb_any(skb);
B
Ben Greear 已提交
1476 1477
	}

M
Michal Kazior 已提交
1478
	__skb_queue_head(amsdu, first);
B
Ben Greear 已提交
1479 1480 1481
	return 0;
}

M
Michal Kazior 已提交
1482
static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
1483
				    struct sk_buff_head *amsdu)
1484
{
M
Michal Kazior 已提交
1485 1486 1487
	struct sk_buff *first;
	struct htt_rx_desc *rxd;
	enum rx_msdu_decap_format decap;
1488

M
Michal Kazior 已提交
1489 1490
	first = skb_peek(amsdu);
	rxd = (void *)first->data - sizeof(*rxd);
1491
	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
M
Michal Kazior 已提交
1492
		   RX_MSDU_START_INFO1_DECAP_FORMAT);
1493

M
Michal Kazior 已提交
1494 1495 1496 1497 1498 1499 1500 1501 1502
	/* 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) {
		__skb_queue_purge(amsdu);
		return;
1503 1504
	}

M
Michal Kazior 已提交
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
	ath10k_unchain_msdu(amsdu);
}

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) {
		ath10k_warn(ar, "no channel configured; ignoring frame(s)!\n");
1518 1519 1520
		return false;
	}

M
Michal Kazior 已提交
1521 1522
	if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
1523 1524 1525 1526 1527 1528
		return false;
	}

	return true;
}

M
Michal Kazior 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
static void ath10k_htt_rx_h_filter(struct ath10k *ar,
				   struct sk_buff_head *amsdu,
				   struct ieee80211_rx_status *rx_status)
{
	if (skb_queue_empty(amsdu))
		return;

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

	__skb_queue_purge(amsdu);
}

1542
static int ath10k_htt_rx_handle_amsdu(struct ath10k_htt *htt)
1543
{
1544
	struct ath10k *ar = htt->ar;
1545
	struct ieee80211_rx_status *rx_status = &htt->rx_status;
M
Michal Kazior 已提交
1546
	struct sk_buff_head amsdu;
1547
	int ret, num_msdus;
1548

1549
	__skb_queue_head_init(&amsdu);
1550

1551 1552 1553 1554 1555 1556 1557
	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);
M
Michal Kazior 已提交
1558

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
	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;
	}

1569
	num_msdus = skb_queue_len(&amsdu);
1570
	ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
1571 1572 1573 1574 1575

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

1576 1577 1578
	ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
	ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status);
	ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
1579

1580
	return num_msdus;
1581 1582
}

1583 1584
static void ath10k_htt_rx_proc_rx_ind(struct ath10k_htt *htt,
				      struct htt_rx_indication *rx)
1585
{
1586
	struct ath10k *ar = htt->ar;
1587 1588
	struct htt_rx_indication_mpdu_range *mpdu_ranges;
	int num_mpdu_ranges;
1589
	int i, mpdu_count = 0;
1590 1591 1592 1593 1594

	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(rx);

1595
	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
1596 1597 1598 1599
			rx, sizeof(*rx) +
			(sizeof(struct htt_rx_indication_mpdu_range) *
				num_mpdu_ranges));

M
Michal Kazior 已提交
1600 1601 1602
	for (i = 0; i < num_mpdu_ranges; i++)
		mpdu_count += mpdu_ranges[i].mpdu_count;

1603
	atomic_add(mpdu_count, &htt->num_mpdus_ready);
1604 1605
}

1606
static void ath10k_htt_rx_tx_compl_ind(struct ath10k *ar,
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
				       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);
	__le16 msdu_id;
	int i;

	switch (status) {
	case HTT_DATA_TX_STATUS_NO_ACK:
1618
		tx_done.status = HTT_TX_COMPL_STATE_NOACK;
1619 1620
		break;
	case HTT_DATA_TX_STATUS_OK:
1621
		tx_done.status = HTT_TX_COMPL_STATE_ACK;
1622 1623 1624 1625
		break;
	case HTT_DATA_TX_STATUS_DISCARD:
	case HTT_DATA_TX_STATUS_POSTPONE:
	case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
1626
		tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
1627 1628
		break;
	default:
1629
		ath10k_warn(ar, "unhandled tx completion status %d\n", status);
1630
		tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
1631 1632 1633
		break;
	}

1634
	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
1635 1636 1637 1638 1639
		   resp->data_tx_completion.num_msdus);

	for (i = 0; i < resp->data_tx_completion.num_msdus; i++) {
		msdu_id = resp->data_tx_completion.msdus[i];
		tx_done.msdu_id = __le16_to_cpu(msdu_id);
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653

		/* 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.
		 */
		if (!kfifo_put(&htt->txdone_fifo, tx_done)) {
			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);
		}
1654 1655 1656
	}
}

1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
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);

1668
	ath10k_dbg(ar, ATH10K_DBG_HTT,
1669 1670 1671 1672 1673 1674
		   "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) {
1675
		ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
1676 1677 1678 1679 1680 1681 1682
			    peer_id);
		spin_unlock_bh(&ar->data_lock);
		return;
	}

	arvif = ath10k_get_arvif(ar, peer->vdev_id);
	if (!arvif) {
1683
		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
1684 1685 1686 1687 1688
			    peer->vdev_id);
		spin_unlock_bh(&ar->data_lock);
		return;
	}

1689
	ath10k_dbg(ar, ATH10K_DBG_HTT,
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
		   "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);

1708
	ath10k_dbg(ar, ATH10K_DBG_HTT,
1709 1710 1711 1712 1713 1714
		   "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) {
1715
		ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
1716 1717 1718 1719 1720 1721 1722
			    peer_id);
		spin_unlock_bh(&ar->data_lock);
		return;
	}

	arvif = ath10k_get_arvif(ar, peer->vdev_id);
	if (!arvif) {
1723
		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
1724 1725 1726 1727 1728
			    peer->vdev_id);
		spin_unlock_bh(&ar->data_lock);
		return;
	}

1729
	ath10k_dbg(ar, ATH10K_DBG_HTT,
1730 1731 1732 1733 1734 1735 1736
		   "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);
}

1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
				       struct sk_buff_head *amsdu)
{
	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;

	while ((msdu = __skb_dequeue(list))) {
		__skb_queue_tail(amsdu, msdu);

		rxd = (void *)msdu->data - sizeof(*rxd);
1753
		if (rxd->msdu_end.common.info0 &
1754 1755 1756 1757 1758 1759
		    __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
			break;
	}

	msdu = skb_peek_tail(amsdu);
	rxd = (void *)msdu->data - sizeof(*rxd);
1760
	if (!(rxd->msdu_end.common.info0 &
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
	      __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;
}

1788 1789
static int ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
				      struct sk_buff_head *list)
1790 1791 1792 1793 1794 1795
{
	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;
1796
	int num_msdu = 0;
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833

	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);
M
Michal Kazior 已提交
1834
		ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
1835
		ath10k_process_rx(ar, status, msdu);
1836
		num_msdu++;
1837
	}
1838
	return num_msdu;
1839 1840
}

1841
static int ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
{
	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;
1854
	int ret, num_msdus = 0;
1855 1856 1857 1858

	lockdep_assert_held(&htt->rx_ring.lock);

	if (htt->rx_confused)
1859
		return -EIO;
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877

	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);

	if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs)) {
		ath10k_warn(ar, "dropping invalid in order rx indication\n");
1878
		return -EINVAL;
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
	}

	/* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
	 * extracted and processed.
	 */
	__skb_queue_head_init(&list);
	ret = ath10k_htt_rx_pop_paddr_list(htt, &resp->rx_in_ord_ind, &list);
	if (ret < 0) {
		ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
		htt->rx_confused = true;
1889
		return -EIO;
1890 1891 1892 1893 1894 1895
	}

	/* Offloaded frames are very different and need to be handled
	 * separately.
	 */
	if (offload)
1896
		num_msdus = ath10k_htt_rx_h_rx_offload(ar, &list);
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908

	while (!skb_queue_empty(&list)) {
		__skb_queue_head_init(&amsdu);
		ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
		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.
			 */
1909
			num_msdus += skb_queue_len(&amsdu);
M
Michal Kazior 已提交
1910
			ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
			ath10k_htt_rx_h_filter(ar, &amsdu, status);
			ath10k_htt_rx_h_mpdu(ar, &amsdu, status);
			ath10k_htt_rx_h_deliver(ar, &amsdu, status);
			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);
1922
			return -EIO;
1923 1924
		}
	}
1925
	return num_msdus;
1926 1927
}

1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
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)
{
M
Michal Kazior 已提交
1950 1951
	struct ieee80211_hw *hw = ar->hw;
	struct ieee80211_txq *txq;
1952 1953 1954 1955 1956
	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;
M
Michal Kazior 已提交
1957 1958
	size_t num_bytes;
	size_t num_msdus;
1959 1960 1961 1962 1963
	const __le32 *resp_ids;
	u16 num_records;
	u16 num_resp_ids;
	u16 peer_id;
	u8 tid;
M
Michal Kazior 已提交
1964
	int ret;
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
	int i;

	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));

M
Michal Kazior 已提交
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
	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();
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020

	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;
		}

M
Michal Kazior 已提交
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
		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;

		while (num_msdus < max_num_msdus &&
		       num_bytes < max_num_bytes) {
			ret = ath10k_mac_tx_push_txq(hw, txq);
			if (ret < 0)
				break;

			num_msdus++;
			num_bytes += ret;
		}

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

		ath10k_htt_tx_txq_recalc(hw, txq);
2052 2053
	}

M
Michal Kazior 已提交
2054 2055
	rcu_read_unlock();

2056 2057 2058
	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);

M
Michal Kazior 已提交
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
	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 */
	}
2069

M
Michal Kazior 已提交
2070
	ath10k_htt_tx_txq_sync(ar);
2071 2072
}

2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
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;
M
Michal Kazior 已提交
2106 2107
	struct ieee80211_txq *txq;
	struct ath10k_txq *artxq;
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 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
	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;

M
Michal Kazior 已提交
2159 2160 2161 2162 2163
	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();
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177

	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;
		}

M
Michal Kazior 已提交
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
		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);
2196 2197
	}

M
Michal Kazior 已提交
2198 2199 2200
	rcu_read_unlock();

	ath10k_mac_tx_push_pending(ar);
2201 2202
}

2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
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);
}

2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
static inline bool is_valid_legacy_rate(u8 rate)
{
	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])
			return true;
	}

	return false;
}

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;
	u8 rate = 0, sgi;
	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);

2245 2246 2247 2248 2249 2250 2251 2252 2253
	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);
2254 2255 2256
		return;
	}

2257 2258
	memset(&arsta->txrate, 0, sizeof(arsta->txrate));

2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
	if (txrate.flags == WMI_RATE_PREAMBLE_CCK ||
	    txrate.flags == WMI_RATE_PREAMBLE_OFDM) {
		rate = ATH10K_HW_LEGACY_RATE(peer_stats->ratecode);

		if (!is_valid_legacy_rate(rate)) {
			ath10k_warn(ar, "Invalid legacy rate %hhd peer stats",
				    rate);
			return;
		}

		/* This is hacky, FW sends CCK rate 5.5Mbps as 6 */
		rate *= 10;
		if (rate == 60 && txrate.flags == WMI_RATE_PREAMBLE_CCK)
			rate = rate - 5;
2273
		arsta->txrate.legacy = rate;
2274 2275
	} else if (txrate.flags == WMI_RATE_PREAMBLE_HT) {
		arsta->txrate.flags = RATE_INFO_FLAGS_MCS;
2276
		arsta->txrate.mcs = txrate.mcs + 8 * (txrate.nss - 1);
2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 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 2339 2340 2341 2342 2343
	} else {
		arsta->txrate.flags = RATE_INFO_FLAGS_VHT_MCS;
		arsta->txrate.mcs = txrate.mcs;
	}

	if (sgi)
		arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;

	arsta->txrate.nss = txrate.nss;
	arsta->txrate.bw = txrate.bw + RATE_INFO_BW_20;
}

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);
	if (!peer) {
		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);

		ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
	}

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

2344
bool ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
2345
{
2346
	struct ath10k_htt *htt = &ar->htt;
2347
	struct htt_resp *resp = (struct htt_resp *)skb->data;
2348
	enum htt_t2h_msg_type type;
2349 2350 2351

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

2354
	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
2355
		   resp->hdr.msg_type);
2356 2357 2358 2359

	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);
2360
		return true;
2361 2362 2363 2364
	}
	type = ar->htt.t2h_msg_types[resp->hdr.msg_type];

	switch (type) {
2365 2366 2367 2368 2369 2370
	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;
	}
2371
	case HTT_T2H_MSG_TYPE_RX_IND:
2372 2373
		ath10k_htt_rx_proc_rx_ind(htt, &resp->rx_ind);
		break;
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
	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 = {};
		int status = __le32_to_cpu(resp->mgmt_tx_completion.status);

2394
		tx_done.msdu_id = __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
2395 2396 2397

		switch (status) {
		case HTT_MGMT_TX_STATUS_OK:
2398
			tx_done.status = HTT_TX_COMPL_STATE_ACK;
2399 2400
			break;
		case HTT_MGMT_TX_STATUS_RETRY:
2401
			tx_done.status = HTT_TX_COMPL_STATE_NOACK;
2402 2403
			break;
		case HTT_MGMT_TX_STATUS_DROP:
2404
			tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
2405 2406 2407
			break;
		}

2408 2409 2410 2411 2412 2413
		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);
		}
2414 2415
		break;
	}
2416
	case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
2417 2418
		ath10k_htt_rx_tx_compl_ind(htt->ar, skb);
		break;
2419 2420 2421 2422
	case HTT_T2H_MSG_TYPE_SEC_IND: {
		struct ath10k *ar = htt->ar;
		struct htt_security_indication *ev = &resp->security_indication;

2423
		ath10k_dbg(ar, ATH10K_DBG_HTT,
2424 2425 2426 2427 2428 2429 2430 2431
			   "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: {
2432
		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
2433
				skb->data, skb->len);
2434
		atomic_inc(&htt->num_mpdus_ready);
2435 2436 2437 2438 2439
		break;
	}
	case HTT_T2H_MSG_TYPE_TEST:
		break;
	case HTT_T2H_MSG_TYPE_STATS_CONF:
2440
		trace_ath10k_htt_stats(ar, skb->data, skb->len);
2441 2442
		break;
	case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
2443 2444 2445 2446 2447
		/* 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.
		 */
2448
		ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
2449
		break;
2450
	case HTT_T2H_MSG_TYPE_RX_ADDBA:
2451 2452
		ath10k_htt_rx_addba(ar, resp);
		break;
2453
	case HTT_T2H_MSG_TYPE_RX_DELBA:
2454 2455
		ath10k_htt_rx_delba(ar, resp);
		break;
2456 2457
	case HTT_T2H_MSG_TYPE_PKTLOG: {
		trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
2458 2459 2460
					skb->len -
					offsetof(struct htt_resp,
						 pktlog_msg.payload));
2461 2462
		break;
	}
2463 2464 2465 2466 2467 2468
	case HTT_T2H_MSG_TYPE_RX_FLUSH: {
		/* Ignore this event because mac80211 takes care of Rx
		 * aggregation reordering.
		 */
		break;
	}
2469
	case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
2470
		__skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
2471
		return false;
2472 2473
	}
	case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND:
2474
		break;
2475 2476 2477 2478
	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);

2479
		ar->tgt_oper_chan = ieee80211_get_channel(ar->hw->wiphy, freq);
2480 2481 2482
		ath10k_dbg(ar, ATH10K_DBG_HTT,
			   "htt chan change freq %u phymode %s\n",
			   freq, ath10k_wmi_phymode_str(phymode));
2483
		break;
2484
	}
2485 2486
	case HTT_T2H_MSG_TYPE_AGGR_CONF:
		break;
2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
	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;
	}
M
Michal Kazior 已提交
2497
	case HTT_T2H_MSG_TYPE_TX_FETCH_CONFIRM:
2498 2499
		ath10k_htt_rx_tx_fetch_confirm(ar, skb);
		break;
M
Michal Kazior 已提交
2500
	case HTT_T2H_MSG_TYPE_TX_MODE_SWITCH_IND:
2501
		ath10k_htt_rx_tx_mode_switch_ind(ar, skb);
2502
		break;
2503 2504 2505
	case HTT_T2H_MSG_TYPE_PEER_STATS:
		ath10k_htt_fetch_peer_stats(ar, skb);
		break;
2506
	case HTT_T2H_MSG_TYPE_EN_STATS:
2507
	default:
2508 2509
		ath10k_warn(ar, "htt event (%d) not handled\n",
			    resp->hdr.msg_type);
2510
		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
2511 2512
				skb->data, skb->len);
		break;
2513
	}
2514
	return true;
2515
}
2516
EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
2517

2518 2519 2520
void ath10k_htt_rx_pktlog_completion_handler(struct ath10k *ar,
					     struct sk_buff *skb)
{
2521
	trace_ath10k_htt_pktlog(ar, skb->data, skb->len);
2522 2523 2524 2525
	dev_kfree_skb_any(skb);
}
EXPORT_SYMBOL(ath10k_htt_rx_pktlog_completion_handler);

2526
int ath10k_htt_txrx_compl_task(struct ath10k *ar, int budget)
2527
{
2528
	struct ath10k_htt *htt = &ar->htt;
2529
	struct htt_tx_done tx_done = {};
M
Michal Kazior 已提交
2530
	struct sk_buff_head tx_ind_q;
2531
	struct sk_buff *skb;
2532
	unsigned long flags;
2533 2534
	int quota = 0, done, num_rx_msdus;
	bool resched_napi = false;
2535

M
Michal Kazior 已提交
2536
	__skb_queue_head_init(&tx_ind_q);
2537

2538 2539 2540 2541 2542 2543
	/* Since in-ord-ind can deliver more than 1 A-MSDU in single event,
	 * process it first to utilize full available quota.
	 */
	while (quota < budget) {
		if (skb_queue_empty(&htt->rx_in_ord_compl_q))
			break;
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
		skb = __skb_dequeue(&htt->rx_in_ord_compl_q);
		if (!skb) {
			resched_napi = true;
			goto exit;
		}

		spin_lock_bh(&htt->rx_ring.lock);
		num_rx_msdus = ath10k_htt_rx_in_ord_ind(ar, skb);
		spin_unlock_bh(&htt->rx_ring.lock);
		if (num_rx_msdus < 0) {
			resched_napi = true;
			goto exit;
		}

		dev_kfree_skb_any(skb);
		if (num_rx_msdus > 0)
			quota += num_rx_msdus;

		if ((quota > ATH10K_NAPI_QUOTA_LIMIT) &&
		    !skb_queue_empty(&htt->rx_in_ord_compl_q)) {
			resched_napi = true;
			goto exit;
		}
	}

	while (quota < budget) {
		/* no more data to receive */
		if (!atomic_read(&htt->num_mpdus_ready))
			break;

		num_rx_msdus = ath10k_htt_rx_handle_amsdu(htt);
		if (num_rx_msdus < 0) {
			resched_napi = true;
			goto exit;
		}

		quota += num_rx_msdus;
		atomic_dec(&htt->num_mpdus_ready);
		if ((quota > ATH10K_NAPI_QUOTA_LIMIT) &&
		    atomic_read(&htt->num_mpdus_ready)) {
			resched_napi = true;
			goto exit;
		}
	}

	/* 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;
M
Michal Kazior 已提交
2597

2598 2599 2600 2601 2602 2603 2604
	/* 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);
2605

2606 2607
	ath10k_mac_tx_push_pending(ar);

2608 2609 2610 2611
	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);

M
Michal Kazior 已提交
2612 2613
	while ((skb = __skb_dequeue(&tx_ind_q))) {
		ath10k_htt_rx_tx_fetch_ind(ar, skb);
2614 2615 2616
		dev_kfree_skb_any(skb);
	}

2617
exit:
2618
	ath10k_htt_rx_msdu_buff_replenish(htt);
2619 2620 2621 2622 2623 2624
	/* In case of rx failure or more data to read, report budget
	 * to reschedule NAPI poll
	 */
	done = resched_napi ? budget : quota;

	return done;
2625
}
2626
EXPORT_SYMBOL(ath10k_htt_txrx_compl_task);