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