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