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