• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "core.h"
19 #include "htc.h"
20 #include "htt.h"
21 #include "txrx.h"
22 #include "debug.h"
23 #include "trace.h"
24 #include "mac.h"
25 
26 #include <linux/log2.h>
27 
28 #define HTT_RX_RING_SIZE HTT_RX_RING_SIZE_MAX
29 #define HTT_RX_RING_FILL_LEVEL (((HTT_RX_RING_SIZE) / 2) - 1)
30 
31 /* when under memory pressure rx ring refill may fail and needs a retry */
32 #define HTT_RX_RING_REFILL_RETRY_MS 50
33 
34 static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);
35 static void ath10k_htt_txrx_compl_task(unsigned long ptr);
36 
37 static struct sk_buff *
ath10k_htt_rx_find_skb_paddr(struct ath10k * ar,u32 paddr)38 ath10k_htt_rx_find_skb_paddr(struct ath10k *ar, u32 paddr)
39 {
40 	struct ath10k_skb_rxcb *rxcb;
41 
42 	hash_for_each_possible(ar->htt.rx_ring.skb_table, rxcb, hlist, paddr)
43 		if (rxcb->paddr == paddr)
44 			return ATH10K_RXCB_SKB(rxcb);
45 
46 	WARN_ON_ONCE(1);
47 	return NULL;
48 }
49 
ath10k_htt_rx_ring_free(struct ath10k_htt * htt)50 static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
51 {
52 	struct sk_buff *skb;
53 	struct ath10k_skb_rxcb *rxcb;
54 	struct hlist_node *n;
55 	int i;
56 
57 	if (htt->rx_ring.in_ord_rx) {
58 		hash_for_each_safe(htt->rx_ring.skb_table, i, n, rxcb, hlist) {
59 			skb = ATH10K_RXCB_SKB(rxcb);
60 			dma_unmap_single(htt->ar->dev, rxcb->paddr,
61 					 skb->len + skb_tailroom(skb),
62 					 DMA_FROM_DEVICE);
63 			hash_del(&rxcb->hlist);
64 			dev_kfree_skb_any(skb);
65 		}
66 	} else {
67 		for (i = 0; i < htt->rx_ring.size; i++) {
68 			skb = htt->rx_ring.netbufs_ring[i];
69 			if (!skb)
70 				continue;
71 
72 			rxcb = ATH10K_SKB_RXCB(skb);
73 			dma_unmap_single(htt->ar->dev, rxcb->paddr,
74 					 skb->len + skb_tailroom(skb),
75 					 DMA_FROM_DEVICE);
76 			dev_kfree_skb_any(skb);
77 		}
78 	}
79 
80 	htt->rx_ring.fill_cnt = 0;
81 	hash_init(htt->rx_ring.skb_table);
82 	memset(htt->rx_ring.netbufs_ring, 0,
83 	       htt->rx_ring.size * sizeof(htt->rx_ring.netbufs_ring[0]));
84 }
85 
__ath10k_htt_rx_ring_fill_n(struct ath10k_htt * htt,int num)86 static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
87 {
88 	struct htt_rx_desc *rx_desc;
89 	struct ath10k_skb_rxcb *rxcb;
90 	struct sk_buff *skb;
91 	dma_addr_t paddr;
92 	int ret = 0, idx;
93 
94 	/* The Full Rx Reorder firmware has no way of telling the host
95 	 * implicitly when it copied HTT Rx Ring buffers to MAC Rx Ring.
96 	 * To keep things simple make sure ring is always half empty. This
97 	 * guarantees there'll be no replenishment overruns possible.
98 	 */
99 	BUILD_BUG_ON(HTT_RX_RING_FILL_LEVEL >= HTT_RX_RING_SIZE / 2);
100 
101 	idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
102 
103 	if (idx < 0 || idx >= htt->rx_ring.size) {
104 		ath10k_err(htt->ar, "rx ring index is not valid, firmware malfunctioning?\n");
105 		idx &= htt->rx_ring.size_mask;
106 		ret = -ENOMEM;
107 		goto fail;
108 	}
109 
110 	while (num > 0) {
111 		skb = dev_alloc_skb(HTT_RX_BUF_SIZE + HTT_RX_DESC_ALIGN);
112 		if (!skb) {
113 			ret = -ENOMEM;
114 			goto fail;
115 		}
116 
117 		if (!IS_ALIGNED((unsigned long)skb->data, HTT_RX_DESC_ALIGN))
118 			skb_pull(skb,
119 				 PTR_ALIGN(skb->data, HTT_RX_DESC_ALIGN) -
120 				 skb->data);
121 
122 		/* Clear rx_desc attention word before posting to Rx ring */
123 		rx_desc = (struct htt_rx_desc *)skb->data;
124 		rx_desc->attention.flags = __cpu_to_le32(0);
125 
126 		paddr = dma_map_single(htt->ar->dev, skb->data,
127 				       skb->len + skb_tailroom(skb),
128 				       DMA_FROM_DEVICE);
129 
130 		if (unlikely(dma_mapping_error(htt->ar->dev, paddr))) {
131 			dev_kfree_skb_any(skb);
132 			ret = -ENOMEM;
133 			goto fail;
134 		}
135 
136 		rxcb = ATH10K_SKB_RXCB(skb);
137 		rxcb->paddr = paddr;
138 		htt->rx_ring.netbufs_ring[idx] = skb;
139 		htt->rx_ring.paddrs_ring[idx] = __cpu_to_le32(paddr);
140 		htt->rx_ring.fill_cnt++;
141 
142 		if (htt->rx_ring.in_ord_rx) {
143 			hash_add(htt->rx_ring.skb_table,
144 				 &ATH10K_SKB_RXCB(skb)->hlist,
145 				 (u32)paddr);
146 		}
147 
148 		num--;
149 		idx++;
150 		idx &= htt->rx_ring.size_mask;
151 	}
152 
153 fail:
154 	/*
155 	 * Make sure the rx buffer is updated before available buffer
156 	 * index to avoid any potential rx ring corruption.
157 	 */
158 	mb();
159 	*htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
160 	return ret;
161 }
162 
ath10k_htt_rx_ring_fill_n(struct ath10k_htt * htt,int num)163 static int ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
164 {
165 	lockdep_assert_held(&htt->rx_ring.lock);
166 	return __ath10k_htt_rx_ring_fill_n(htt, num);
167 }
168 
ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt * htt)169 static void ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt *htt)
170 {
171 	int ret, num_deficit, num_to_fill;
172 
173 	/* Refilling the whole RX ring buffer proves to be a bad idea. The
174 	 * reason is RX may take up significant amount of CPU cycles and starve
175 	 * other tasks, e.g. TX on an ethernet device while acting as a bridge
176 	 * with ath10k wlan interface. This ended up with very poor performance
177 	 * once CPU the host system was overwhelmed with RX on ath10k.
178 	 *
179 	 * By limiting the number of refills the replenishing occurs
180 	 * progressively. This in turns makes use of the fact tasklets are
181 	 * processed in FIFO order. This means actual RX processing can starve
182 	 * out refilling. If there's not enough buffers on RX ring FW will not
183 	 * report RX until it is refilled with enough buffers. This
184 	 * automatically balances load wrt to CPU power.
185 	 *
186 	 * This probably comes at a cost of lower maximum throughput but
187 	 * improves the average and stability. */
188 	spin_lock_bh(&htt->rx_ring.lock);
189 	num_deficit = htt->rx_ring.fill_level - htt->rx_ring.fill_cnt;
190 	num_to_fill = min(ATH10K_HTT_MAX_NUM_REFILL, num_deficit);
191 	num_deficit -= num_to_fill;
192 	ret = ath10k_htt_rx_ring_fill_n(htt, num_to_fill);
193 	if (ret == -ENOMEM) {
194 		/*
195 		 * Failed to fill it to the desired level -
196 		 * we'll start a timer and try again next time.
197 		 * As long as enough buffers are left in the ring for
198 		 * another A-MPDU rx, no special recovery is needed.
199 		 */
200 		mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
201 			  msecs_to_jiffies(HTT_RX_RING_REFILL_RETRY_MS));
202 	} else if (num_deficit > 0) {
203 		tasklet_schedule(&htt->rx_replenish_task);
204 	}
205 	spin_unlock_bh(&htt->rx_ring.lock);
206 }
207 
ath10k_htt_rx_ring_refill_retry(unsigned long arg)208 static void ath10k_htt_rx_ring_refill_retry(unsigned long arg)
209 {
210 	struct ath10k_htt *htt = (struct ath10k_htt *)arg;
211 
212 	ath10k_htt_rx_msdu_buff_replenish(htt);
213 }
214 
ath10k_htt_rx_ring_refill(struct ath10k * ar)215 int ath10k_htt_rx_ring_refill(struct ath10k *ar)
216 {
217 	struct ath10k_htt *htt = &ar->htt;
218 	int ret;
219 
220 	spin_lock_bh(&htt->rx_ring.lock);
221 	ret = ath10k_htt_rx_ring_fill_n(htt, (htt->rx_ring.fill_level -
222 					      htt->rx_ring.fill_cnt));
223 
224 	if (ret)
225 		ath10k_htt_rx_ring_free(htt);
226 
227 	spin_unlock_bh(&htt->rx_ring.lock);
228 
229 	return ret;
230 }
231 
ath10k_htt_rx_free(struct ath10k_htt * htt)232 void ath10k_htt_rx_free(struct ath10k_htt *htt)
233 {
234 	del_timer_sync(&htt->rx_ring.refill_retry_timer);
235 	tasklet_kill(&htt->rx_replenish_task);
236 	tasklet_kill(&htt->txrx_compl_task);
237 
238 	skb_queue_purge(&htt->tx_compl_q);
239 	skb_queue_purge(&htt->rx_compl_q);
240 	skb_queue_purge(&htt->rx_in_ord_compl_q);
241 
242 	spin_lock_bh(&htt->rx_ring.lock);
243 	ath10k_htt_rx_ring_free(htt);
244 	spin_unlock_bh(&htt->rx_ring.lock);
245 
246 	dma_free_coherent(htt->ar->dev,
247 			  (htt->rx_ring.size *
248 			   sizeof(htt->rx_ring.paddrs_ring)),
249 			  htt->rx_ring.paddrs_ring,
250 			  htt->rx_ring.base_paddr);
251 
252 	dma_free_coherent(htt->ar->dev,
253 			  sizeof(*htt->rx_ring.alloc_idx.vaddr),
254 			  htt->rx_ring.alloc_idx.vaddr,
255 			  htt->rx_ring.alloc_idx.paddr);
256 
257 	kfree(htt->rx_ring.netbufs_ring);
258 }
259 
ath10k_htt_rx_netbuf_pop(struct ath10k_htt * htt)260 static inline struct sk_buff *ath10k_htt_rx_netbuf_pop(struct ath10k_htt *htt)
261 {
262 	struct ath10k *ar = htt->ar;
263 	int idx;
264 	struct sk_buff *msdu;
265 
266 	lockdep_assert_held(&htt->rx_ring.lock);
267 
268 	if (htt->rx_ring.fill_cnt == 0) {
269 		ath10k_warn(ar, "tried to pop sk_buff from an empty rx ring\n");
270 		return NULL;
271 	}
272 
273 	idx = htt->rx_ring.sw_rd_idx.msdu_payld;
274 	msdu = htt->rx_ring.netbufs_ring[idx];
275 	htt->rx_ring.netbufs_ring[idx] = NULL;
276 	htt->rx_ring.paddrs_ring[idx] = 0;
277 
278 	idx++;
279 	idx &= htt->rx_ring.size_mask;
280 	htt->rx_ring.sw_rd_idx.msdu_payld = idx;
281 	htt->rx_ring.fill_cnt--;
282 
283 	dma_unmap_single(htt->ar->dev,
284 			 ATH10K_SKB_RXCB(msdu)->paddr,
285 			 msdu->len + skb_tailroom(msdu),
286 			 DMA_FROM_DEVICE);
287 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
288 			msdu->data, msdu->len + skb_tailroom(msdu));
289 
290 	return msdu;
291 }
292 
293 /* return: < 0 fatal error, 0 - non chained msdu, 1 chained msdu */
ath10k_htt_rx_amsdu_pop(struct ath10k_htt * htt,u8 ** fw_desc,int * fw_desc_len,struct sk_buff_head * amsdu)294 static int ath10k_htt_rx_amsdu_pop(struct ath10k_htt *htt,
295 				   u8 **fw_desc, int *fw_desc_len,
296 				   struct sk_buff_head *amsdu)
297 {
298 	struct ath10k *ar = htt->ar;
299 	int msdu_len, msdu_chaining = 0;
300 	struct sk_buff *msdu;
301 	struct htt_rx_desc *rx_desc;
302 
303 	lockdep_assert_held(&htt->rx_ring.lock);
304 
305 	for (;;) {
306 		int last_msdu, msdu_len_invalid, msdu_chained;
307 
308 		msdu = ath10k_htt_rx_netbuf_pop(htt);
309 		if (!msdu) {
310 			__skb_queue_purge(amsdu);
311 			return -ENOENT;
312 		}
313 
314 		__skb_queue_tail(amsdu, msdu);
315 
316 		rx_desc = (struct htt_rx_desc *)msdu->data;
317 
318 		/* FIXME: we must report msdu payload since this is what caller
319 		 *        expects now */
320 		skb_put(msdu, offsetof(struct htt_rx_desc, msdu_payload));
321 		skb_pull(msdu, offsetof(struct htt_rx_desc, msdu_payload));
322 
323 		/*
324 		 * Sanity check - confirm the HW is finished filling in the
325 		 * rx data.
326 		 * If the HW and SW are working correctly, then it's guaranteed
327 		 * that the HW's MAC DMA is done before this point in the SW.
328 		 * To prevent the case that we handle a stale Rx descriptor,
329 		 * just assert for now until we have a way to recover.
330 		 */
331 		if (!(__le32_to_cpu(rx_desc->attention.flags)
332 				& RX_ATTENTION_FLAGS_MSDU_DONE)) {
333 			__skb_queue_purge(amsdu);
334 			return -EIO;
335 		}
336 
337 		/*
338 		 * Copy the FW rx descriptor for this MSDU from the rx
339 		 * indication message into the MSDU's netbuf. HL uses the
340 		 * same rx indication message definition as LL, and simply
341 		 * appends new info (fields from the HW rx desc, and the
342 		 * MSDU payload itself). So, the offset into the rx
343 		 * indication message only has to account for the standard
344 		 * offset of the per-MSDU FW rx desc info within the
345 		 * message, and how many bytes of the per-MSDU FW rx desc
346 		 * info have already been consumed. (And the endianness of
347 		 * the host, since for a big-endian host, the rx ind
348 		 * message contents, including the per-MSDU rx desc bytes,
349 		 * were byteswapped during upload.)
350 		 */
351 		if (*fw_desc_len > 0) {
352 			rx_desc->fw_desc.info0 = **fw_desc;
353 			/*
354 			 * The target is expected to only provide the basic
355 			 * per-MSDU rx descriptors. Just to be sure, verify
356 			 * that the target has not attached extension data
357 			 * (e.g. LRO flow ID).
358 			 */
359 
360 			/* or more, if there's extension data */
361 			(*fw_desc)++;
362 			(*fw_desc_len)--;
363 		} else {
364 			/*
365 			 * When an oversized AMSDU happened, FW will lost
366 			 * some of MSDU status - in this case, the FW
367 			 * descriptors provided will be less than the
368 			 * actual MSDUs inside this MPDU. Mark the FW
369 			 * descriptors so that it will still deliver to
370 			 * upper stack, if no CRC error for this MPDU.
371 			 *
372 			 * FIX THIS - the FW descriptors are actually for
373 			 * MSDUs in the end of this A-MSDU instead of the
374 			 * beginning.
375 			 */
376 			rx_desc->fw_desc.info0 = 0;
377 		}
378 
379 		msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
380 					& (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
381 					   RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
382 		msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.common.info0),
383 			      RX_MSDU_START_INFO0_MSDU_LENGTH);
384 		msdu_chained = rx_desc->frag_info.ring2_more_count;
385 
386 		if (msdu_len_invalid)
387 			msdu_len = 0;
388 
389 		skb_trim(msdu, 0);
390 		skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
391 		msdu_len -= msdu->len;
392 
393 		/* Note: Chained buffers do not contain rx descriptor */
394 		while (msdu_chained--) {
395 			msdu = ath10k_htt_rx_netbuf_pop(htt);
396 			if (!msdu) {
397 				__skb_queue_purge(amsdu);
398 				return -ENOENT;
399 			}
400 
401 			__skb_queue_tail(amsdu, msdu);
402 			skb_trim(msdu, 0);
403 			skb_put(msdu, min(msdu_len, HTT_RX_BUF_SIZE));
404 			msdu_len -= msdu->len;
405 			msdu_chaining = 1;
406 		}
407 
408 		last_msdu = __le32_to_cpu(rx_desc->msdu_end.common.info0) &
409 				RX_MSDU_END_INFO0_LAST_MSDU;
410 
411 		trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
412 					 sizeof(*rx_desc) - sizeof(u32));
413 
414 		if (last_msdu)
415 			break;
416 	}
417 
418 	if (skb_queue_empty(amsdu))
419 		msdu_chaining = -1;
420 
421 	/*
422 	 * Don't refill the ring yet.
423 	 *
424 	 * First, the elements popped here are still in use - it is not
425 	 * safe to overwrite them until the matching call to
426 	 * mpdu_desc_list_next. Second, for efficiency it is preferable to
427 	 * refill the rx ring with 1 PPDU's worth of rx buffers (something
428 	 * like 32 x 3 buffers), rather than one MPDU's worth of rx buffers
429 	 * (something like 3 buffers). Consequently, we'll rely on the txrx
430 	 * SW to tell us when it is done pulling all the PPDU's rx buffers
431 	 * out of the rx ring, and then refill it just once.
432 	 */
433 
434 	return msdu_chaining;
435 }
436 
ath10k_htt_rx_replenish_task(unsigned long ptr)437 static void ath10k_htt_rx_replenish_task(unsigned long ptr)
438 {
439 	struct ath10k_htt *htt = (struct ath10k_htt *)ptr;
440 
441 	ath10k_htt_rx_msdu_buff_replenish(htt);
442 }
443 
ath10k_htt_rx_pop_paddr(struct ath10k_htt * htt,u32 paddr)444 static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
445 					       u32 paddr)
446 {
447 	struct ath10k *ar = htt->ar;
448 	struct ath10k_skb_rxcb *rxcb;
449 	struct sk_buff *msdu;
450 
451 	lockdep_assert_held(&htt->rx_ring.lock);
452 
453 	msdu = ath10k_htt_rx_find_skb_paddr(ar, paddr);
454 	if (!msdu)
455 		return NULL;
456 
457 	rxcb = ATH10K_SKB_RXCB(msdu);
458 	hash_del(&rxcb->hlist);
459 	htt->rx_ring.fill_cnt--;
460 
461 	dma_unmap_single(htt->ar->dev, rxcb->paddr,
462 			 msdu->len + skb_tailroom(msdu),
463 			 DMA_FROM_DEVICE);
464 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
465 			msdu->data, msdu->len + skb_tailroom(msdu));
466 
467 	return msdu;
468 }
469 
ath10k_htt_rx_pop_paddr_list(struct ath10k_htt * htt,struct htt_rx_in_ord_ind * ev,struct sk_buff_head * list)470 static int ath10k_htt_rx_pop_paddr_list(struct ath10k_htt *htt,
471 					struct htt_rx_in_ord_ind *ev,
472 					struct sk_buff_head *list)
473 {
474 	struct ath10k *ar = htt->ar;
475 	struct htt_rx_in_ord_msdu_desc *msdu_desc = ev->msdu_descs;
476 	struct htt_rx_desc *rxd;
477 	struct sk_buff *msdu;
478 	int msdu_count;
479 	bool is_offload;
480 	u32 paddr;
481 
482 	lockdep_assert_held(&htt->rx_ring.lock);
483 
484 	msdu_count = __le16_to_cpu(ev->msdu_count);
485 	is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
486 
487 	while (msdu_count--) {
488 		paddr = __le32_to_cpu(msdu_desc->msdu_paddr);
489 
490 		msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
491 		if (!msdu) {
492 			__skb_queue_purge(list);
493 			return -ENOENT;
494 		}
495 
496 		__skb_queue_tail(list, msdu);
497 
498 		if (!is_offload) {
499 			rxd = (void *)msdu->data;
500 
501 			trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
502 
503 			skb_put(msdu, sizeof(*rxd));
504 			skb_pull(msdu, sizeof(*rxd));
505 			skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
506 
507 			if (!(__le32_to_cpu(rxd->attention.flags) &
508 			      RX_ATTENTION_FLAGS_MSDU_DONE)) {
509 				ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
510 				return -EIO;
511 			}
512 		}
513 
514 		msdu_desc++;
515 	}
516 
517 	return 0;
518 }
519 
ath10k_htt_rx_alloc(struct ath10k_htt * htt)520 int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
521 {
522 	struct ath10k *ar = htt->ar;
523 	dma_addr_t paddr;
524 	void *vaddr;
525 	size_t size;
526 	struct timer_list *timer = &htt->rx_ring.refill_retry_timer;
527 
528 	htt->rx_confused = false;
529 
530 	/* XXX: The fill level could be changed during runtime in response to
531 	 * the host processing latency. Is this really worth it?
532 	 */
533 	htt->rx_ring.size = HTT_RX_RING_SIZE;
534 	htt->rx_ring.size_mask = htt->rx_ring.size - 1;
535 	htt->rx_ring.fill_level = HTT_RX_RING_FILL_LEVEL;
536 
537 	if (!is_power_of_2(htt->rx_ring.size)) {
538 		ath10k_warn(ar, "htt rx ring size is not power of 2\n");
539 		return -EINVAL;
540 	}
541 
542 	htt->rx_ring.netbufs_ring =
543 		kzalloc(htt->rx_ring.size * sizeof(struct sk_buff *),
544 			GFP_KERNEL);
545 	if (!htt->rx_ring.netbufs_ring)
546 		goto err_netbuf;
547 
548 	size = htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring);
549 
550 	vaddr = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_DMA);
551 	if (!vaddr)
552 		goto err_dma_ring;
553 
554 	htt->rx_ring.paddrs_ring = vaddr;
555 	htt->rx_ring.base_paddr = paddr;
556 
557 	vaddr = dma_alloc_coherent(htt->ar->dev,
558 				   sizeof(*htt->rx_ring.alloc_idx.vaddr),
559 				   &paddr, GFP_DMA);
560 	if (!vaddr)
561 		goto err_dma_idx;
562 
563 	htt->rx_ring.alloc_idx.vaddr = vaddr;
564 	htt->rx_ring.alloc_idx.paddr = paddr;
565 	htt->rx_ring.sw_rd_idx.msdu_payld = htt->rx_ring.size_mask;
566 	*htt->rx_ring.alloc_idx.vaddr = 0;
567 
568 	/* Initialize the Rx refill retry timer */
569 	setup_timer(timer, ath10k_htt_rx_ring_refill_retry, (unsigned long)htt);
570 
571 	spin_lock_init(&htt->rx_ring.lock);
572 
573 	htt->rx_ring.fill_cnt = 0;
574 	htt->rx_ring.sw_rd_idx.msdu_payld = 0;
575 	hash_init(htt->rx_ring.skb_table);
576 
577 	tasklet_init(&htt->rx_replenish_task, ath10k_htt_rx_replenish_task,
578 		     (unsigned long)htt);
579 
580 	skb_queue_head_init(&htt->tx_compl_q);
581 	skb_queue_head_init(&htt->rx_compl_q);
582 	skb_queue_head_init(&htt->rx_in_ord_compl_q);
583 
584 	tasklet_init(&htt->txrx_compl_task, ath10k_htt_txrx_compl_task,
585 		     (unsigned long)htt);
586 
587 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
588 		   htt->rx_ring.size, htt->rx_ring.fill_level);
589 	return 0;
590 
591 err_dma_idx:
592 	dma_free_coherent(htt->ar->dev,
593 			  (htt->rx_ring.size *
594 			   sizeof(htt->rx_ring.paddrs_ring)),
595 			  htt->rx_ring.paddrs_ring,
596 			  htt->rx_ring.base_paddr);
597 err_dma_ring:
598 	kfree(htt->rx_ring.netbufs_ring);
599 err_netbuf:
600 	return -ENOMEM;
601 }
602 
ath10k_htt_rx_crypto_param_len(struct ath10k * ar,enum htt_rx_mpdu_encrypt_type type)603 static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
604 					  enum htt_rx_mpdu_encrypt_type type)
605 {
606 	switch (type) {
607 	case HTT_RX_MPDU_ENCRYPT_NONE:
608 		return 0;
609 	case HTT_RX_MPDU_ENCRYPT_WEP40:
610 	case HTT_RX_MPDU_ENCRYPT_WEP104:
611 		return IEEE80211_WEP_IV_LEN;
612 	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
613 	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
614 		return IEEE80211_TKIP_IV_LEN;
615 	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
616 		return IEEE80211_CCMP_HDR_LEN;
617 	case HTT_RX_MPDU_ENCRYPT_WEP128:
618 	case HTT_RX_MPDU_ENCRYPT_WAPI:
619 		break;
620 	}
621 
622 	ath10k_warn(ar, "unsupported encryption type %d\n", type);
623 	return 0;
624 }
625 
626 #define MICHAEL_MIC_LEN 8
627 
ath10k_htt_rx_crypto_tail_len(struct ath10k * ar,enum htt_rx_mpdu_encrypt_type type)628 static int ath10k_htt_rx_crypto_tail_len(struct ath10k *ar,
629 					 enum htt_rx_mpdu_encrypt_type type)
630 {
631 	switch (type) {
632 	case HTT_RX_MPDU_ENCRYPT_NONE:
633 		return 0;
634 	case HTT_RX_MPDU_ENCRYPT_WEP40:
635 	case HTT_RX_MPDU_ENCRYPT_WEP104:
636 		return IEEE80211_WEP_ICV_LEN;
637 	case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
638 	case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
639 		return IEEE80211_TKIP_ICV_LEN;
640 	case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
641 		return IEEE80211_CCMP_MIC_LEN;
642 	case HTT_RX_MPDU_ENCRYPT_WEP128:
643 	case HTT_RX_MPDU_ENCRYPT_WAPI:
644 		break;
645 	}
646 
647 	ath10k_warn(ar, "unsupported encryption type %d\n", type);
648 	return 0;
649 }
650 
651 struct amsdu_subframe_hdr {
652 	u8 dst[ETH_ALEN];
653 	u8 src[ETH_ALEN];
654 	__be16 len;
655 } __packed;
656 
657 #define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)
658 
ath10k_htt_rx_h_rates(struct ath10k * ar,struct ieee80211_rx_status * status,struct htt_rx_desc * rxd)659 static void ath10k_htt_rx_h_rates(struct ath10k *ar,
660 				  struct ieee80211_rx_status *status,
661 				  struct htt_rx_desc *rxd)
662 {
663 	struct ieee80211_supported_band *sband;
664 	u8 cck, rate, bw, sgi, mcs, nss;
665 	u8 preamble = 0;
666 	u8 group_id;
667 	u32 info1, info2, info3;
668 	u32 stbc, nsts_su;
669 
670 	info1 = __le32_to_cpu(rxd->ppdu_start.info1);
671 	info2 = __le32_to_cpu(rxd->ppdu_start.info2);
672 	info3 = __le32_to_cpu(rxd->ppdu_start.info3);
673 
674 	preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
675 
676 	switch (preamble) {
677 	case HTT_RX_LEGACY:
678 		/* To get legacy rate index band is required. Since band can't
679 		 * be undefined check if freq is non-zero.
680 		 */
681 		if (!status->freq)
682 			return;
683 
684 		cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
685 		rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
686 		rate &= ~RX_PPDU_START_RATE_FLAG;
687 
688 		sband = &ar->mac.sbands[status->band];
689 		status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate);
690 		break;
691 	case HTT_RX_HT:
692 	case HTT_RX_HT_WITH_TXBF:
693 		/* HT-SIG - Table 20-11 in info2 and info3 */
694 		mcs = info2 & 0x1F;
695 		nss = mcs >> 3;
696 		bw = (info2 >> 7) & 1;
697 		sgi = (info3 >> 7) & 1;
698 
699 		status->rate_idx = mcs;
700 		status->flag |= RX_FLAG_HT;
701 		if (sgi)
702 			status->flag |= RX_FLAG_SHORT_GI;
703 		if (bw)
704 			status->flag |= RX_FLAG_40MHZ;
705 		break;
706 	case HTT_RX_VHT:
707 	case HTT_RX_VHT_WITH_TXBF:
708 		/* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
709 		   TODO check this */
710 		bw = info2 & 3;
711 		sgi = info3 & 1;
712 		stbc = (info2 >> 3) & 1;
713 		group_id = (info2 >> 4) & 0x3F;
714 
715 		if (GROUP_ID_IS_SU_MIMO(group_id)) {
716 			mcs = (info3 >> 4) & 0x0F;
717 			nsts_su = ((info2 >> 10) & 0x07);
718 			if (stbc)
719 				nss = (nsts_su >> 2) + 1;
720 			else
721 				nss = (nsts_su + 1);
722 		} else {
723 			/* Hardware doesn't decode VHT-SIG-B into Rx descriptor
724 			 * so it's impossible to decode MCS. Also since
725 			 * firmware consumes Group Id Management frames host
726 			 * has no knowledge regarding group/user position
727 			 * mapping so it's impossible to pick the correct Nsts
728 			 * from VHT-SIG-A1.
729 			 *
730 			 * Bandwidth and SGI are valid so report the rateinfo
731 			 * on best-effort basis.
732 			 */
733 			mcs = 0;
734 			nss = 1;
735 		}
736 
737 		if (mcs > 0x09) {
738 			ath10k_warn(ar, "invalid MCS received %u\n", mcs);
739 			ath10k_warn(ar, "rxd %08x mpdu start %08x %08x msdu start %08x %08x ppdu start %08x %08x %08x %08x %08x\n",
740 				    __le32_to_cpu(rxd->attention.flags),
741 				    __le32_to_cpu(rxd->mpdu_start.info0),
742 				    __le32_to_cpu(rxd->mpdu_start.info1),
743 				    __le32_to_cpu(rxd->msdu_start.common.info0),
744 				    __le32_to_cpu(rxd->msdu_start.common.info1),
745 				    rxd->ppdu_start.info0,
746 				    __le32_to_cpu(rxd->ppdu_start.info1),
747 				    __le32_to_cpu(rxd->ppdu_start.info2),
748 				    __le32_to_cpu(rxd->ppdu_start.info3),
749 				    __le32_to_cpu(rxd->ppdu_start.info4));
750 
751 			ath10k_warn(ar, "msdu end %08x mpdu end %08x\n",
752 				    __le32_to_cpu(rxd->msdu_end.common.info0),
753 				    __le32_to_cpu(rxd->mpdu_end.info0));
754 
755 			ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL,
756 					"rx desc msdu payload: ",
757 					rxd->msdu_payload, 50);
758 		}
759 
760 		status->rate_idx = mcs;
761 		status->vht_nss = nss;
762 
763 		if (sgi)
764 			status->flag |= RX_FLAG_SHORT_GI;
765 
766 		switch (bw) {
767 		/* 20MHZ */
768 		case 0:
769 			break;
770 		/* 40MHZ */
771 		case 1:
772 			status->flag |= RX_FLAG_40MHZ;
773 			break;
774 		/* 80MHZ */
775 		case 2:
776 			status->vht_flag |= RX_VHT_FLAG_80MHZ;
777 		}
778 
779 		status->flag |= RX_FLAG_VHT;
780 		break;
781 	default:
782 		break;
783 	}
784 }
785 
786 static struct ieee80211_channel *
ath10k_htt_rx_h_peer_channel(struct ath10k * ar,struct htt_rx_desc * rxd)787 ath10k_htt_rx_h_peer_channel(struct ath10k *ar, struct htt_rx_desc *rxd)
788 {
789 	struct ath10k_peer *peer;
790 	struct ath10k_vif *arvif;
791 	struct cfg80211_chan_def def;
792 	u16 peer_id;
793 
794 	lockdep_assert_held(&ar->data_lock);
795 
796 	if (!rxd)
797 		return NULL;
798 
799 	if (rxd->attention.flags &
800 	    __cpu_to_le32(RX_ATTENTION_FLAGS_PEER_IDX_INVALID))
801 		return NULL;
802 
803 	if (!(rxd->msdu_end.common.info0 &
804 	      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU)))
805 		return NULL;
806 
807 	peer_id = MS(__le32_to_cpu(rxd->mpdu_start.info0),
808 		     RX_MPDU_START_INFO0_PEER_IDX);
809 
810 	peer = ath10k_peer_find_by_id(ar, peer_id);
811 	if (!peer)
812 		return NULL;
813 
814 	arvif = ath10k_get_arvif(ar, peer->vdev_id);
815 	if (WARN_ON_ONCE(!arvif))
816 		return NULL;
817 
818 	if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
819 		return NULL;
820 
821 	return def.chan;
822 }
823 
824 static struct ieee80211_channel *
ath10k_htt_rx_h_vdev_channel(struct ath10k * ar,u32 vdev_id)825 ath10k_htt_rx_h_vdev_channel(struct ath10k *ar, u32 vdev_id)
826 {
827 	struct ath10k_vif *arvif;
828 	struct cfg80211_chan_def def;
829 
830 	lockdep_assert_held(&ar->data_lock);
831 
832 	list_for_each_entry(arvif, &ar->arvifs, list) {
833 		if (arvif->vdev_id == vdev_id &&
834 		    ath10k_mac_vif_chan(arvif->vif, &def) == 0)
835 			return def.chan;
836 	}
837 
838 	return NULL;
839 }
840 
841 static void
ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * conf,void * data)842 ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw *hw,
843 			      struct ieee80211_chanctx_conf *conf,
844 			      void *data)
845 {
846 	struct cfg80211_chan_def *def = data;
847 
848 	*def = conf->def;
849 }
850 
851 static struct ieee80211_channel *
ath10k_htt_rx_h_any_channel(struct ath10k * ar)852 ath10k_htt_rx_h_any_channel(struct ath10k *ar)
853 {
854 	struct cfg80211_chan_def def = {};
855 
856 	ieee80211_iter_chan_contexts_atomic(ar->hw,
857 					    ath10k_htt_rx_h_any_chan_iter,
858 					    &def);
859 
860 	return def.chan;
861 }
862 
ath10k_htt_rx_h_channel(struct ath10k * ar,struct ieee80211_rx_status * status,struct htt_rx_desc * rxd,u32 vdev_id)863 static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
864 				    struct ieee80211_rx_status *status,
865 				    struct htt_rx_desc *rxd,
866 				    u32 vdev_id)
867 {
868 	struct ieee80211_channel *ch;
869 
870 	spin_lock_bh(&ar->data_lock);
871 	ch = ar->scan_channel;
872 	if (!ch)
873 		ch = ar->rx_channel;
874 	if (!ch)
875 		ch = ath10k_htt_rx_h_peer_channel(ar, rxd);
876 	if (!ch)
877 		ch = ath10k_htt_rx_h_vdev_channel(ar, vdev_id);
878 	if (!ch)
879 		ch = ath10k_htt_rx_h_any_channel(ar);
880 	spin_unlock_bh(&ar->data_lock);
881 
882 	if (!ch)
883 		return false;
884 
885 	status->band = ch->band;
886 	status->freq = ch->center_freq;
887 
888 	return true;
889 }
890 
ath10k_htt_rx_h_signal(struct ath10k * ar,struct ieee80211_rx_status * status,struct htt_rx_desc * rxd)891 static void ath10k_htt_rx_h_signal(struct ath10k *ar,
892 				   struct ieee80211_rx_status *status,
893 				   struct htt_rx_desc *rxd)
894 {
895 	/* FIXME: Get real NF */
896 	status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
897 			 rxd->ppdu_start.rssi_comb;
898 	status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
899 }
900 
ath10k_htt_rx_h_mactime(struct ath10k * ar,struct ieee80211_rx_status * status,struct htt_rx_desc * rxd)901 static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
902 				    struct ieee80211_rx_status *status,
903 				    struct htt_rx_desc *rxd)
904 {
905 	/* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
906 	 * means all prior MSDUs in a PPDU are reported to mac80211 without the
907 	 * TSF. Is it worth holding frames until end of PPDU is known?
908 	 *
909 	 * FIXME: Can we get/compute 64bit TSF?
910 	 */
911 	status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
912 	status->flag |= RX_FLAG_MACTIME_END;
913 }
914 
ath10k_htt_rx_h_ppdu(struct ath10k * ar,struct sk_buff_head * amsdu,struct ieee80211_rx_status * status,u32 vdev_id)915 static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
916 				 struct sk_buff_head *amsdu,
917 				 struct ieee80211_rx_status *status,
918 				 u32 vdev_id)
919 {
920 	struct sk_buff *first;
921 	struct htt_rx_desc *rxd;
922 	bool is_first_ppdu;
923 	bool is_last_ppdu;
924 
925 	if (skb_queue_empty(amsdu))
926 		return;
927 
928 	first = skb_peek(amsdu);
929 	rxd = (void *)first->data - sizeof(*rxd);
930 
931 	is_first_ppdu = !!(rxd->attention.flags &
932 			   __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
933 	is_last_ppdu = !!(rxd->attention.flags &
934 			  __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));
935 
936 	if (is_first_ppdu) {
937 		/* New PPDU starts so clear out the old per-PPDU status. */
938 		status->freq = 0;
939 		status->rate_idx = 0;
940 		status->vht_nss = 0;
941 		status->vht_flag &= ~RX_VHT_FLAG_80MHZ;
942 		status->flag &= ~(RX_FLAG_HT |
943 				  RX_FLAG_VHT |
944 				  RX_FLAG_SHORT_GI |
945 				  RX_FLAG_40MHZ |
946 				  RX_FLAG_MACTIME_END);
947 		status->flag |= RX_FLAG_NO_SIGNAL_VAL;
948 
949 		ath10k_htt_rx_h_signal(ar, status, rxd);
950 		ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
951 		ath10k_htt_rx_h_rates(ar, status, rxd);
952 	}
953 
954 	if (is_last_ppdu)
955 		ath10k_htt_rx_h_mactime(ar, status, rxd);
956 }
957 
958 static const char * const tid_to_ac[] = {
959 	"BE",
960 	"BK",
961 	"BK",
962 	"BE",
963 	"VI",
964 	"VI",
965 	"VO",
966 	"VO",
967 };
968 
ath10k_get_tid(struct ieee80211_hdr * hdr,char * out,size_t size)969 static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
970 {
971 	u8 *qc;
972 	int tid;
973 
974 	if (!ieee80211_is_data_qos(hdr->frame_control))
975 		return "";
976 
977 	qc = ieee80211_get_qos_ctl(hdr);
978 	tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
979 	if (tid < 8)
980 		snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
981 	else
982 		snprintf(out, size, "tid %d", tid);
983 
984 	return out;
985 }
986 
ath10k_process_rx(struct ath10k * ar,struct ieee80211_rx_status * rx_status,struct sk_buff * skb)987 static void ath10k_process_rx(struct ath10k *ar,
988 			      struct ieee80211_rx_status *rx_status,
989 			      struct sk_buff *skb)
990 {
991 	struct ieee80211_rx_status *status;
992 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
993 	char tid[32];
994 
995 	status = IEEE80211_SKB_RXCB(skb);
996 	*status = *rx_status;
997 
998 	ath10k_dbg(ar, ATH10K_DBG_DATA,
999 		   "rx skb %p len %u peer %pM %s %s sn %u %s%s%s%s%s %srate_idx %u vht_nss %u freq %u band %u flag 0x%llx fcs-err %i mic-err %i amsdu-more %i\n",
1000 		   skb,
1001 		   skb->len,
1002 		   ieee80211_get_SA(hdr),
1003 		   ath10k_get_tid(hdr, tid, sizeof(tid)),
1004 		   is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
1005 							"mcast" : "ucast",
1006 		   (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
1007 		   status->flag == 0 ? "legacy" : "",
1008 		   status->flag & RX_FLAG_HT ? "ht" : "",
1009 		   status->flag & RX_FLAG_VHT ? "vht" : "",
1010 		   status->flag & RX_FLAG_40MHZ ? "40" : "",
1011 		   status->vht_flag & RX_VHT_FLAG_80MHZ ? "80" : "",
1012 		   status->flag & RX_FLAG_SHORT_GI ? "sgi " : "",
1013 		   status->rate_idx,
1014 		   status->vht_nss,
1015 		   status->freq,
1016 		   status->band, status->flag,
1017 		   !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
1018 		   !!(status->flag & RX_FLAG_MMIC_ERROR),
1019 		   !!(status->flag & RX_FLAG_AMSDU_MORE));
1020 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
1021 			skb->data, skb->len);
1022 	trace_ath10k_rx_hdr(ar, skb->data, skb->len);
1023 	trace_ath10k_rx_payload(ar, skb->data, skb->len);
1024 
1025 	ieee80211_rx(ar->hw, skb);
1026 }
1027 
ath10k_htt_rx_nwifi_hdrlen(struct ath10k * ar,struct ieee80211_hdr * hdr)1028 static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
1029 				      struct ieee80211_hdr *hdr)
1030 {
1031 	int len = ieee80211_hdrlen(hdr->frame_control);
1032 
1033 	if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
1034 		      ar->fw_features))
1035 		len = round_up(len, 4);
1036 
1037 	return len;
1038 }
1039 
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)1040 static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
1041 					struct sk_buff *msdu,
1042 					struct ieee80211_rx_status *status,
1043 					enum htt_rx_mpdu_encrypt_type enctype,
1044 					bool is_decrypted)
1045 {
1046 	struct ieee80211_hdr *hdr;
1047 	struct htt_rx_desc *rxd;
1048 	size_t hdr_len;
1049 	size_t crypto_len;
1050 	bool is_first;
1051 	bool is_last;
1052 
1053 	rxd = (void *)msdu->data - sizeof(*rxd);
1054 	is_first = !!(rxd->msdu_end.common.info0 &
1055 		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
1056 	is_last = !!(rxd->msdu_end.common.info0 &
1057 		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
1058 
1059 	/* Delivered decapped frame:
1060 	 * [802.11 header]
1061 	 * [crypto param] <-- can be trimmed if !fcs_err &&
1062 	 *                    !decrypt_err && !peer_idx_invalid
1063 	 * [amsdu header] <-- only if A-MSDU
1064 	 * [rfc1042/llc]
1065 	 * [payload]
1066 	 * [FCS] <-- at end, needs to be trimmed
1067 	 */
1068 
1069 	/* This probably shouldn't happen but warn just in case */
1070 	if (unlikely(WARN_ON_ONCE(!is_first)))
1071 		return;
1072 
1073 	/* This probably shouldn't happen but warn just in case */
1074 	if (unlikely(WARN_ON_ONCE(!(is_first && is_last))))
1075 		return;
1076 
1077 	skb_trim(msdu, msdu->len - FCS_LEN);
1078 
1079 	/* In most cases this will be true for sniffed frames. It makes sense
1080 	 * to deliver them as-is without stripping the crypto param. This is
1081 	 * necessary for software based decryption.
1082 	 *
1083 	 * If there's no error then the frame is decrypted. At least that is
1084 	 * the case for frames that come in via fragmented rx indication.
1085 	 */
1086 	if (!is_decrypted)
1087 		return;
1088 
1089 	/* The payload is decrypted so strip crypto params. Start from tail
1090 	 * since hdr is used to compute some stuff.
1091 	 */
1092 
1093 	hdr = (void *)msdu->data;
1094 
1095 	/* Tail */
1096 	if (status->flag & RX_FLAG_IV_STRIPPED) {
1097 		skb_trim(msdu, msdu->len -
1098 			 ath10k_htt_rx_crypto_tail_len(ar, enctype));
1099 	} else {
1100 		/* MIC */
1101 		if ((status->flag & RX_FLAG_MIC_STRIPPED) &&
1102 		    enctype == HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2)
1103 			skb_trim(msdu, msdu->len - 8);
1104 
1105 		/* ICV */
1106 		if (status->flag & RX_FLAG_ICV_STRIPPED &&
1107 		    enctype != HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2)
1108 			skb_trim(msdu, msdu->len -
1109 				 ath10k_htt_rx_crypto_tail_len(ar, enctype));
1110 	}
1111 
1112 	/* MMIC */
1113 	if (!ieee80211_has_morefrags(hdr->frame_control) &&
1114 	    enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
1115 		skb_trim(msdu, msdu->len - 8);
1116 
1117 	/* Head */
1118 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1119 	crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1120 
1121 	memmove((void *)msdu->data + crypto_len,
1122 		(void *)msdu->data, hdr_len);
1123 	skb_pull(msdu, crypto_len);
1124 }
1125 
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)1126 static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
1127 					  struct sk_buff *msdu,
1128 					  struct ieee80211_rx_status *status,
1129 					  const u8 first_hdr[64],
1130 					  enum htt_rx_mpdu_encrypt_type enctype)
1131 {
1132 	struct ieee80211_hdr *hdr;
1133 	size_t hdr_len;
1134 	u8 da[ETH_ALEN];
1135 	u8 sa[ETH_ALEN];
1136 	int bytes_aligned = ar->hw_params.decap_align_bytes;
1137 
1138 	/* Delivered decapped frame:
1139 	 * [nwifi 802.11 header] <-- replaced with 802.11 hdr
1140 	 * [rfc1042/llc]
1141 	 *
1142 	 * Note: The nwifi header doesn't have QoS Control and is
1143 	 * (always?) a 3addr frame.
1144 	 *
1145 	 * Note2: There's no A-MSDU subframe header. Even if it's part
1146 	 * of an A-MSDU.
1147 	 */
1148 
1149 	/* pull decapped header and copy SA & DA */
1150 	hdr = (struct ieee80211_hdr *)msdu->data;
1151 	hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
1152 	ether_addr_copy(da, ieee80211_get_DA(hdr));
1153 	ether_addr_copy(sa, ieee80211_get_SA(hdr));
1154 	skb_pull(msdu, hdr_len);
1155 
1156 	/* push original 802.11 header */
1157 	hdr = (struct ieee80211_hdr *)first_hdr;
1158 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1159 
1160 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1161 		memcpy(skb_push(msdu,
1162 				ath10k_htt_rx_crypto_param_len(ar, enctype)),
1163 		       (void *)hdr + round_up(hdr_len, bytes_aligned),
1164 			ath10k_htt_rx_crypto_param_len(ar, enctype));
1165 	}
1166 
1167 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1168 
1169 	/* original 802.11 header has a different DA and in
1170 	 * case of 4addr it may also have different SA
1171 	 */
1172 	hdr = (struct ieee80211_hdr *)msdu->data;
1173 	ether_addr_copy(ieee80211_get_DA(hdr), da);
1174 	ether_addr_copy(ieee80211_get_SA(hdr), sa);
1175 }
1176 
ath10k_htt_rx_h_find_rfc1042(struct ath10k * ar,struct sk_buff * msdu,enum htt_rx_mpdu_encrypt_type enctype)1177 static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
1178 					  struct sk_buff *msdu,
1179 					  enum htt_rx_mpdu_encrypt_type enctype)
1180 {
1181 	struct ieee80211_hdr *hdr;
1182 	struct htt_rx_desc *rxd;
1183 	size_t hdr_len, crypto_len;
1184 	void *rfc1042;
1185 	bool is_first, is_last, is_amsdu;
1186 	int bytes_aligned = ar->hw_params.decap_align_bytes;
1187 
1188 	rxd = (void *)msdu->data - sizeof(*rxd);
1189 	hdr = (void *)rxd->rx_hdr_status;
1190 
1191 	is_first = !!(rxd->msdu_end.common.info0 &
1192 		      __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
1193 	is_last = !!(rxd->msdu_end.common.info0 &
1194 		     __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
1195 	is_amsdu = !(is_first && is_last);
1196 
1197 	rfc1042 = hdr;
1198 
1199 	if (is_first) {
1200 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1201 		crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1202 
1203 		rfc1042 += round_up(hdr_len, bytes_aligned) +
1204 			   round_up(crypto_len, bytes_aligned);
1205 	}
1206 
1207 	if (is_amsdu)
1208 		rfc1042 += sizeof(struct amsdu_subframe_hdr);
1209 
1210 	return rfc1042;
1211 }
1212 
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)1213 static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
1214 					struct sk_buff *msdu,
1215 					struct ieee80211_rx_status *status,
1216 					const u8 first_hdr[64],
1217 					enum htt_rx_mpdu_encrypt_type enctype)
1218 {
1219 	struct ieee80211_hdr *hdr;
1220 	struct ethhdr *eth;
1221 	size_t hdr_len;
1222 	void *rfc1042;
1223 	u8 da[ETH_ALEN];
1224 	u8 sa[ETH_ALEN];
1225 	int bytes_aligned = ar->hw_params.decap_align_bytes;
1226 
1227 	/* Delivered decapped frame:
1228 	 * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
1229 	 * [payload]
1230 	 */
1231 
1232 	rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
1233 	if (WARN_ON_ONCE(!rfc1042))
1234 		return;
1235 
1236 	/* pull decapped header and copy SA & DA */
1237 	eth = (struct ethhdr *)msdu->data;
1238 	ether_addr_copy(da, eth->h_dest);
1239 	ether_addr_copy(sa, eth->h_source);
1240 	skb_pull(msdu, sizeof(struct ethhdr));
1241 
1242 	/* push rfc1042/llc/snap */
1243 	memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
1244 	       sizeof(struct rfc1042_hdr));
1245 
1246 	/* push original 802.11 header */
1247 	hdr = (struct ieee80211_hdr *)first_hdr;
1248 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1249 
1250 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1251 		memcpy(skb_push(msdu,
1252 				ath10k_htt_rx_crypto_param_len(ar, enctype)),
1253 		       (void *)hdr + round_up(hdr_len, bytes_aligned),
1254 			ath10k_htt_rx_crypto_param_len(ar, enctype));
1255 	}
1256 
1257 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1258 
1259 	/* original 802.11 header has a different DA and in
1260 	 * case of 4addr it may also have different SA
1261 	 */
1262 	hdr = (struct ieee80211_hdr *)msdu->data;
1263 	ether_addr_copy(ieee80211_get_DA(hdr), da);
1264 	ether_addr_copy(ieee80211_get_SA(hdr), sa);
1265 }
1266 
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)1267 static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
1268 					 struct sk_buff *msdu,
1269 					 struct ieee80211_rx_status *status,
1270 					 const u8 first_hdr[64],
1271 					 enum htt_rx_mpdu_encrypt_type enctype)
1272 {
1273 	struct ieee80211_hdr *hdr;
1274 	size_t hdr_len;
1275 	int bytes_aligned = ar->hw_params.decap_align_bytes;
1276 
1277 	/* Delivered decapped frame:
1278 	 * [amsdu header] <-- replaced with 802.11 hdr
1279 	 * [rfc1042/llc]
1280 	 * [payload]
1281 	 */
1282 
1283 	skb_pull(msdu, sizeof(struct amsdu_subframe_hdr));
1284 
1285 	hdr = (struct ieee80211_hdr *)first_hdr;
1286 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1287 
1288 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1289 		memcpy(skb_push(msdu,
1290 				ath10k_htt_rx_crypto_param_len(ar, enctype)),
1291 		       (void *)hdr + round_up(hdr_len, bytes_aligned),
1292 			ath10k_htt_rx_crypto_param_len(ar, enctype));
1293 	}
1294 
1295 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1296 }
1297 
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)1298 static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
1299 				    struct sk_buff *msdu,
1300 				    struct ieee80211_rx_status *status,
1301 				    u8 first_hdr[64],
1302 				    enum htt_rx_mpdu_encrypt_type enctype,
1303 				    bool is_decrypted)
1304 {
1305 	struct htt_rx_desc *rxd;
1306 	enum rx_msdu_decap_format decap;
1307 
1308 	/* First msdu's decapped header:
1309 	 * [802.11 header] <-- padded to 4 bytes long
1310 	 * [crypto param] <-- padded to 4 bytes long
1311 	 * [amsdu header] <-- only if A-MSDU
1312 	 * [rfc1042/llc]
1313 	 *
1314 	 * Other (2nd, 3rd, ..) msdu's decapped header:
1315 	 * [amsdu header] <-- only if A-MSDU
1316 	 * [rfc1042/llc]
1317 	 */
1318 
1319 	rxd = (void *)msdu->data - sizeof(*rxd);
1320 	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
1321 		   RX_MSDU_START_INFO1_DECAP_FORMAT);
1322 
1323 	switch (decap) {
1324 	case RX_MSDU_DECAP_RAW:
1325 		ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
1326 					    is_decrypted);
1327 		break;
1328 	case RX_MSDU_DECAP_NATIVE_WIFI:
1329 		ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr,
1330 					      enctype);
1331 		break;
1332 	case RX_MSDU_DECAP_ETHERNET2_DIX:
1333 		ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
1334 		break;
1335 	case RX_MSDU_DECAP_8023_SNAP_LLC:
1336 		ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr,
1337 					     enctype);
1338 		break;
1339 	}
1340 }
1341 
ath10k_htt_rx_get_csum_state(struct sk_buff * skb)1342 static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
1343 {
1344 	struct htt_rx_desc *rxd;
1345 	u32 flags, info;
1346 	bool is_ip4, is_ip6;
1347 	bool is_tcp, is_udp;
1348 	bool ip_csum_ok, tcpudp_csum_ok;
1349 
1350 	rxd = (void *)skb->data - sizeof(*rxd);
1351 	flags = __le32_to_cpu(rxd->attention.flags);
1352 	info = __le32_to_cpu(rxd->msdu_start.common.info1);
1353 
1354 	is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
1355 	is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
1356 	is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
1357 	is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
1358 	ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
1359 	tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
1360 
1361 	if (!is_ip4 && !is_ip6)
1362 		return CHECKSUM_NONE;
1363 	if (!is_tcp && !is_udp)
1364 		return CHECKSUM_NONE;
1365 	if (!ip_csum_ok)
1366 		return CHECKSUM_NONE;
1367 	if (!tcpudp_csum_ok)
1368 		return CHECKSUM_NONE;
1369 
1370 	return CHECKSUM_UNNECESSARY;
1371 }
1372 
ath10k_htt_rx_h_csum_offload(struct sk_buff * msdu)1373 static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
1374 {
1375 	msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
1376 }
1377 
ath10k_htt_rx_h_mpdu(struct ath10k * ar,struct sk_buff_head * amsdu,struct ieee80211_rx_status * status,bool fill_crypt_header)1378 static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
1379 				 struct sk_buff_head *amsdu,
1380 				 struct ieee80211_rx_status *status,
1381 				 bool fill_crypt_header)
1382 {
1383 	struct sk_buff *first;
1384 	struct sk_buff *last;
1385 	struct sk_buff *msdu;
1386 	struct htt_rx_desc *rxd;
1387 	struct ieee80211_hdr *hdr;
1388 	enum htt_rx_mpdu_encrypt_type enctype;
1389 	u8 first_hdr[64];
1390 	u8 *qos;
1391 	bool has_fcs_err;
1392 	bool has_crypto_err;
1393 	bool has_tkip_err;
1394 	bool has_peer_idx_invalid;
1395 	bool is_decrypted;
1396 	u32 attention;
1397 
1398 	if (skb_queue_empty(amsdu))
1399 		return;
1400 
1401 	first = skb_peek(amsdu);
1402 	rxd = (void *)first->data - sizeof(*rxd);
1403 
1404 	enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1405 		     RX_MPDU_START_INFO0_ENCRYPT_TYPE);
1406 
1407 	/* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
1408 	 * decapped header. It'll be used for undecapping of each MSDU.
1409 	 */
1410 	hdr = (void *)rxd->rx_hdr_status;
1411 	memcpy(first_hdr, hdr, RX_HTT_HDR_STATUS_LEN);
1412 
1413 	/* Each A-MSDU subframe will use the original header as the base and be
1414 	 * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
1415 	 */
1416 	hdr = (void *)first_hdr;
1417 
1418 	if (ieee80211_is_data_qos(hdr->frame_control)) {
1419 		qos = ieee80211_get_qos_ctl(hdr);
1420 		qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1421 	}
1422 
1423 	/* Some attention flags are valid only in the last MSDU. */
1424 	last = skb_peek_tail(amsdu);
1425 	rxd = (void *)last->data - sizeof(*rxd);
1426 	attention = __le32_to_cpu(rxd->attention.flags);
1427 
1428 	has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
1429 	has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
1430 	has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
1431 	has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);
1432 
1433 	/* Note: If hardware captures an encrypted frame that it can't decrypt,
1434 	 * e.g. due to fcs error, missing peer or invalid key data it will
1435 	 * report the frame as raw.
1436 	 */
1437 	is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
1438 			!has_fcs_err &&
1439 			!has_crypto_err &&
1440 			!has_peer_idx_invalid);
1441 
1442 	/* Clear per-MPDU flags while leaving per-PPDU flags intact. */
1443 	status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
1444 			  RX_FLAG_MMIC_ERROR |
1445 			  RX_FLAG_DECRYPTED |
1446 			  RX_FLAG_IV_STRIPPED |
1447 			  RX_FLAG_MMIC_STRIPPED);
1448 
1449 	if (has_fcs_err)
1450 		status->flag |= RX_FLAG_FAILED_FCS_CRC;
1451 
1452 	if (has_tkip_err)
1453 		status->flag |= RX_FLAG_MMIC_ERROR;
1454 
1455 	if (is_decrypted) {
1456 		status->flag |= RX_FLAG_DECRYPTED |
1457 				RX_FLAG_MMIC_STRIPPED;
1458 
1459 		if (fill_crypt_header)
1460 			status->flag |= RX_FLAG_MIC_STRIPPED |
1461 					RX_FLAG_ICV_STRIPPED;
1462 		else
1463 			status->flag |= RX_FLAG_IV_STRIPPED;
1464 	}
1465 
1466 	skb_queue_walk(amsdu, msdu) {
1467 		ath10k_htt_rx_h_csum_offload(msdu);
1468 		ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
1469 					is_decrypted);
1470 
1471 		/* Undecapping involves copying the original 802.11 header back
1472 		 * to sk_buff. If frame is protected and hardware has decrypted
1473 		 * it then remove the protected bit.
1474 		 */
1475 		if (!is_decrypted)
1476 			continue;
1477 
1478 		if (fill_crypt_header)
1479 			continue;
1480 
1481 		hdr = (void *)msdu->data;
1482 		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1483 	}
1484 }
1485 
ath10k_htt_rx_h_deliver(struct ath10k * ar,struct sk_buff_head * amsdu,struct ieee80211_rx_status * status)1486 static void ath10k_htt_rx_h_deliver(struct ath10k *ar,
1487 				    struct sk_buff_head *amsdu,
1488 				    struct ieee80211_rx_status *status)
1489 {
1490 	struct sk_buff *msdu;
1491 	struct sk_buff *first_subframe;
1492 
1493 	first_subframe = skb_peek(amsdu);
1494 
1495 	while ((msdu = __skb_dequeue(amsdu))) {
1496 		/* Setup per-MSDU flags */
1497 		if (skb_queue_empty(amsdu))
1498 			status->flag &= ~RX_FLAG_AMSDU_MORE;
1499 		else
1500 			status->flag |= RX_FLAG_AMSDU_MORE;
1501 
1502 		if (msdu == first_subframe) {
1503 			first_subframe = NULL;
1504 			status->flag &= ~RX_FLAG_ALLOW_SAME_PN;
1505 		} else {
1506 			status->flag |= RX_FLAG_ALLOW_SAME_PN;
1507 		}
1508 
1509 		ath10k_process_rx(ar, status, msdu);
1510 	}
1511 }
1512 
ath10k_unchain_msdu(struct sk_buff_head * amsdu)1513 static int ath10k_unchain_msdu(struct sk_buff_head *amsdu)
1514 {
1515 	struct sk_buff *skb, *first;
1516 	int space;
1517 	int total_len = 0;
1518 
1519 	/* TODO:  Might could optimize this by using
1520 	 * skb_try_coalesce or similar method to
1521 	 * decrease copying, or maybe get mac80211 to
1522 	 * provide a way to just receive a list of
1523 	 * skb?
1524 	 */
1525 
1526 	first = __skb_dequeue(amsdu);
1527 
1528 	/* Allocate total length all at once. */
1529 	skb_queue_walk(amsdu, skb)
1530 		total_len += skb->len;
1531 
1532 	space = total_len - skb_tailroom(first);
1533 	if ((space > 0) &&
1534 	    (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
1535 		/* TODO:  bump some rx-oom error stat */
1536 		/* put it back together so we can free the
1537 		 * whole list at once.
1538 		 */
1539 		__skb_queue_head(amsdu, first);
1540 		return -1;
1541 	}
1542 
1543 	/* Walk list again, copying contents into
1544 	 * msdu_head
1545 	 */
1546 	while ((skb = __skb_dequeue(amsdu))) {
1547 		skb_copy_from_linear_data(skb, skb_put(first, skb->len),
1548 					  skb->len);
1549 		dev_kfree_skb_any(skb);
1550 	}
1551 
1552 	__skb_queue_head(amsdu, first);
1553 	return 0;
1554 }
1555 
ath10k_htt_rx_h_unchain(struct ath10k * ar,struct sk_buff_head * amsdu,bool chained)1556 static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
1557 				    struct sk_buff_head *amsdu,
1558 				    bool chained)
1559 {
1560 	struct sk_buff *first;
1561 	struct htt_rx_desc *rxd;
1562 	enum rx_msdu_decap_format decap;
1563 
1564 	first = skb_peek(amsdu);
1565 	rxd = (void *)first->data - sizeof(*rxd);
1566 	decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
1567 		   RX_MSDU_START_INFO1_DECAP_FORMAT);
1568 
1569 	if (!chained)
1570 		return;
1571 
1572 	/* FIXME: Current unchaining logic can only handle simple case of raw
1573 	 * msdu chaining. If decapping is other than raw the chaining may be
1574 	 * more complex and this isn't handled by the current code. Don't even
1575 	 * try re-constructing such frames - it'll be pretty much garbage.
1576 	 */
1577 	if (decap != RX_MSDU_DECAP_RAW ||
1578 	    skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
1579 		__skb_queue_purge(amsdu);
1580 		return;
1581 	}
1582 
1583 	ath10k_unchain_msdu(amsdu);
1584 }
1585 
ath10k_htt_rx_amsdu_allowed(struct ath10k * ar,struct sk_buff_head * amsdu,struct ieee80211_rx_status * rx_status)1586 static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
1587 					struct sk_buff_head *amsdu,
1588 					struct ieee80211_rx_status *rx_status)
1589 {
1590 	struct sk_buff *msdu;
1591 	struct htt_rx_desc *rxd;
1592 	bool is_mgmt;
1593 	bool has_fcs_err;
1594 
1595 	msdu = skb_peek(amsdu);
1596 	rxd = (void *)msdu->data - sizeof(*rxd);
1597 
1598 	/* FIXME: It might be a good idea to do some fuzzy-testing to drop
1599 	 * invalid/dangerous frames.
1600 	 */
1601 
1602 	if (!rx_status->freq) {
1603 		ath10k_warn(ar, "no channel configured; ignoring frame(s)!\n");
1604 		return false;
1605 	}
1606 
1607 	is_mgmt = !!(rxd->attention.flags &
1608 		     __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));
1609 	has_fcs_err = !!(rxd->attention.flags &
1610 			 __cpu_to_le32(RX_ATTENTION_FLAGS_FCS_ERR));
1611 
1612 	/* Management frames are handled via WMI events. The pros of such
1613 	 * approach is that channel is explicitly provided in WMI events
1614 	 * whereas HTT doesn't provide channel information for Rxed frames.
1615 	 *
1616 	 * However some firmware revisions don't report corrupted frames via
1617 	 * WMI so don't drop them.
1618 	 */
1619 	if (is_mgmt && !has_fcs_err) {
1620 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx mgmt ctrl\n");
1621 		return false;
1622 	}
1623 
1624 	if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
1625 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
1626 		return false;
1627 	}
1628 
1629 	return true;
1630 }
1631 
ath10k_htt_rx_h_filter(struct ath10k * ar,struct sk_buff_head * amsdu,struct ieee80211_rx_status * rx_status)1632 static void ath10k_htt_rx_h_filter(struct ath10k *ar,
1633 				   struct sk_buff_head *amsdu,
1634 				   struct ieee80211_rx_status *rx_status)
1635 {
1636 	if (skb_queue_empty(amsdu))
1637 		return;
1638 
1639 	if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
1640 		return;
1641 
1642 	__skb_queue_purge(amsdu);
1643 }
1644 
ath10k_htt_rx_handler(struct ath10k_htt * htt,struct htt_rx_indication * rx)1645 static void ath10k_htt_rx_handler(struct ath10k_htt *htt,
1646 				  struct htt_rx_indication *rx)
1647 {
1648 	struct ath10k *ar = htt->ar;
1649 	struct ieee80211_rx_status *rx_status = &htt->rx_status;
1650 	struct htt_rx_indication_mpdu_range *mpdu_ranges;
1651 	struct sk_buff_head amsdu;
1652 	int num_mpdu_ranges;
1653 	int fw_desc_len;
1654 	u8 *fw_desc;
1655 	int i, ret, mpdu_count = 0;
1656 
1657 	lockdep_assert_held(&htt->rx_ring.lock);
1658 
1659 	if (htt->rx_confused)
1660 		return;
1661 
1662 	fw_desc_len = __le16_to_cpu(rx->prefix.fw_rx_desc_bytes);
1663 	fw_desc = (u8 *)&rx->fw_desc;
1664 
1665 	num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
1666 			     HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
1667 	mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
1668 
1669 	ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
1670 			rx, sizeof(*rx) +
1671 			(sizeof(struct htt_rx_indication_mpdu_range) *
1672 				num_mpdu_ranges));
1673 
1674 	for (i = 0; i < num_mpdu_ranges; i++)
1675 		mpdu_count += mpdu_ranges[i].mpdu_count;
1676 
1677 	while (mpdu_count--) {
1678 		__skb_queue_head_init(&amsdu);
1679 		ret = ath10k_htt_rx_amsdu_pop(htt, &fw_desc,
1680 					      &fw_desc_len, &amsdu);
1681 		if (ret < 0) {
1682 			ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
1683 			__skb_queue_purge(&amsdu);
1684 			/* FIXME: It's probably a good idea to reboot the
1685 			 * device instead of leaving it inoperable.
1686 			 */
1687 			htt->rx_confused = true;
1688 			break;
1689 		}
1690 
1691 		ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
1692 		ath10k_htt_rx_h_unchain(ar, &amsdu, ret > 0);
1693 		ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
1694 		ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status, true);
1695 		ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
1696 	}
1697 
1698 	tasklet_schedule(&htt->rx_replenish_task);
1699 }
1700 
ath10k_htt_rx_frag_handler(struct ath10k_htt * htt,struct htt_rx_fragment_indication * frag)1701 static void ath10k_htt_rx_frag_handler(struct ath10k_htt *htt,
1702 				       struct htt_rx_fragment_indication *frag)
1703 {
1704 	struct ath10k *ar = htt->ar;
1705 	struct ieee80211_rx_status *rx_status = &htt->rx_status;
1706 	struct sk_buff_head amsdu;
1707 	int ret;
1708 	u8 *fw_desc;
1709 	int fw_desc_len;
1710 
1711 	fw_desc_len = __le16_to_cpu(frag->fw_rx_desc_bytes);
1712 	fw_desc = (u8 *)frag->fw_msdu_rx_desc;
1713 
1714 	__skb_queue_head_init(&amsdu);
1715 
1716 	spin_lock_bh(&htt->rx_ring.lock);
1717 	ret = ath10k_htt_rx_amsdu_pop(htt, &fw_desc, &fw_desc_len,
1718 				      &amsdu);
1719 	spin_unlock_bh(&htt->rx_ring.lock);
1720 
1721 	tasklet_schedule(&htt->rx_replenish_task);
1722 
1723 	ath10k_dbg(ar, ATH10K_DBG_HTT_DUMP, "htt rx frag ahead\n");
1724 
1725 	if (ret) {
1726 		ath10k_warn(ar, "failed to pop amsdu from httr rx ring for fragmented rx %d\n",
1727 			    ret);
1728 		__skb_queue_purge(&amsdu);
1729 		return;
1730 	}
1731 
1732 	if (skb_queue_len(&amsdu) != 1) {
1733 		ath10k_warn(ar, "failed to pop frag amsdu: too many msdus\n");
1734 		__skb_queue_purge(&amsdu);
1735 		return;
1736 	}
1737 
1738 	ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
1739 	ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
1740 	ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status, true);
1741 	ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
1742 
1743 	if (fw_desc_len > 0) {
1744 		ath10k_dbg(ar, ATH10K_DBG_HTT,
1745 			   "expecting more fragmented rx in one indication %d\n",
1746 			   fw_desc_len);
1747 	}
1748 }
1749 
ath10k_htt_rx_frm_tx_compl(struct ath10k * ar,struct sk_buff * skb)1750 static void ath10k_htt_rx_frm_tx_compl(struct ath10k *ar,
1751 				       struct sk_buff *skb)
1752 {
1753 	struct ath10k_htt *htt = &ar->htt;
1754 	struct htt_resp *resp = (struct htt_resp *)skb->data;
1755 	struct htt_tx_done tx_done = {};
1756 	int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
1757 	__le16 msdu_id;
1758 	int i;
1759 
1760 	switch (status) {
1761 	case HTT_DATA_TX_STATUS_NO_ACK:
1762 		tx_done.no_ack = true;
1763 		break;
1764 	case HTT_DATA_TX_STATUS_OK:
1765 		tx_done.success = true;
1766 		break;
1767 	case HTT_DATA_TX_STATUS_DISCARD:
1768 	case HTT_DATA_TX_STATUS_POSTPONE:
1769 	case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
1770 		tx_done.discard = true;
1771 		break;
1772 	default:
1773 		ath10k_warn(ar, "unhandled tx completion status %d\n", status);
1774 		tx_done.discard = true;
1775 		break;
1776 	}
1777 
1778 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
1779 		   resp->data_tx_completion.num_msdus);
1780 
1781 	for (i = 0; i < resp->data_tx_completion.num_msdus; i++) {
1782 		msdu_id = resp->data_tx_completion.msdus[i];
1783 		tx_done.msdu_id = __le16_to_cpu(msdu_id);
1784 		ath10k_txrx_tx_unref(htt, &tx_done);
1785 	}
1786 }
1787 
ath10k_htt_rx_addba(struct ath10k * ar,struct htt_resp * resp)1788 static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
1789 {
1790 	struct htt_rx_addba *ev = &resp->rx_addba;
1791 	struct ath10k_peer *peer;
1792 	struct ath10k_vif *arvif;
1793 	u16 info0, tid, peer_id;
1794 
1795 	info0 = __le16_to_cpu(ev->info0);
1796 	tid = MS(info0, HTT_RX_BA_INFO0_TID);
1797 	peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1798 
1799 	ath10k_dbg(ar, ATH10K_DBG_HTT,
1800 		   "htt rx addba tid %hu peer_id %hu size %hhu\n",
1801 		   tid, peer_id, ev->window_size);
1802 
1803 	spin_lock_bh(&ar->data_lock);
1804 	peer = ath10k_peer_find_by_id(ar, peer_id);
1805 	if (!peer) {
1806 		ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
1807 			    peer_id);
1808 		spin_unlock_bh(&ar->data_lock);
1809 		return;
1810 	}
1811 
1812 	arvif = ath10k_get_arvif(ar, peer->vdev_id);
1813 	if (!arvif) {
1814 		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
1815 			    peer->vdev_id);
1816 		spin_unlock_bh(&ar->data_lock);
1817 		return;
1818 	}
1819 
1820 	ath10k_dbg(ar, ATH10K_DBG_HTT,
1821 		   "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
1822 		   peer->addr, tid, ev->window_size);
1823 
1824 	ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1825 	spin_unlock_bh(&ar->data_lock);
1826 }
1827 
ath10k_htt_rx_delba(struct ath10k * ar,struct htt_resp * resp)1828 static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
1829 {
1830 	struct htt_rx_delba *ev = &resp->rx_delba;
1831 	struct ath10k_peer *peer;
1832 	struct ath10k_vif *arvif;
1833 	u16 info0, tid, peer_id;
1834 
1835 	info0 = __le16_to_cpu(ev->info0);
1836 	tid = MS(info0, HTT_RX_BA_INFO0_TID);
1837 	peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1838 
1839 	ath10k_dbg(ar, ATH10K_DBG_HTT,
1840 		   "htt rx delba tid %hu peer_id %hu\n",
1841 		   tid, peer_id);
1842 
1843 	spin_lock_bh(&ar->data_lock);
1844 	peer = ath10k_peer_find_by_id(ar, peer_id);
1845 	if (!peer) {
1846 		ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
1847 			    peer_id);
1848 		spin_unlock_bh(&ar->data_lock);
1849 		return;
1850 	}
1851 
1852 	arvif = ath10k_get_arvif(ar, peer->vdev_id);
1853 	if (!arvif) {
1854 		ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
1855 			    peer->vdev_id);
1856 		spin_unlock_bh(&ar->data_lock);
1857 		return;
1858 	}
1859 
1860 	ath10k_dbg(ar, ATH10K_DBG_HTT,
1861 		   "htt rx stop rx ba session sta %pM tid %hu\n",
1862 		   peer->addr, tid);
1863 
1864 	ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1865 	spin_unlock_bh(&ar->data_lock);
1866 }
1867 
ath10k_htt_rx_extract_amsdu(struct sk_buff_head * list,struct sk_buff_head * amsdu)1868 static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
1869 				       struct sk_buff_head *amsdu)
1870 {
1871 	struct sk_buff *msdu;
1872 	struct htt_rx_desc *rxd;
1873 
1874 	if (skb_queue_empty(list))
1875 		return -ENOBUFS;
1876 
1877 	if (WARN_ON(!skb_queue_empty(amsdu)))
1878 		return -EINVAL;
1879 
1880 	while ((msdu = __skb_dequeue(list))) {
1881 		__skb_queue_tail(amsdu, msdu);
1882 
1883 		rxd = (void *)msdu->data - sizeof(*rxd);
1884 		if (rxd->msdu_end.common.info0 &
1885 		    __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
1886 			break;
1887 	}
1888 
1889 	msdu = skb_peek_tail(amsdu);
1890 	rxd = (void *)msdu->data - sizeof(*rxd);
1891 	if (!(rxd->msdu_end.common.info0 &
1892 	      __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
1893 		skb_queue_splice_init(amsdu, list);
1894 		return -EAGAIN;
1895 	}
1896 
1897 	return 0;
1898 }
1899 
ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status * status,struct sk_buff * skb)1900 static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
1901 					    struct sk_buff *skb)
1902 {
1903 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1904 
1905 	if (!ieee80211_has_protected(hdr->frame_control))
1906 		return;
1907 
1908 	/* Offloaded frames are already decrypted but firmware insists they are
1909 	 * protected in the 802.11 header. Strip the flag.  Otherwise mac80211
1910 	 * will drop the frame.
1911 	 */
1912 
1913 	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1914 	status->flag |= RX_FLAG_DECRYPTED |
1915 			RX_FLAG_IV_STRIPPED |
1916 			RX_FLAG_MMIC_STRIPPED;
1917 }
1918 
ath10k_htt_rx_h_rx_offload(struct ath10k * ar,struct sk_buff_head * list)1919 static void ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
1920 				       struct sk_buff_head *list)
1921 {
1922 	struct ath10k_htt *htt = &ar->htt;
1923 	struct ieee80211_rx_status *status = &htt->rx_status;
1924 	struct htt_rx_offload_msdu *rx;
1925 	struct sk_buff *msdu;
1926 	size_t offset;
1927 
1928 	while ((msdu = __skb_dequeue(list))) {
1929 		/* Offloaded frames don't have Rx descriptor. Instead they have
1930 		 * a short meta information header.
1931 		 */
1932 
1933 		rx = (void *)msdu->data;
1934 
1935 		skb_put(msdu, sizeof(*rx));
1936 		skb_pull(msdu, sizeof(*rx));
1937 
1938 		if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
1939 			ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
1940 			dev_kfree_skb_any(msdu);
1941 			continue;
1942 		}
1943 
1944 		skb_put(msdu, __le16_to_cpu(rx->msdu_len));
1945 
1946 		/* Offloaded rx header length isn't multiple of 2 nor 4 so the
1947 		 * actual payload is unaligned. Align the frame.  Otherwise
1948 		 * mac80211 complains.  This shouldn't reduce performance much
1949 		 * because these offloaded frames are rare.
1950 		 */
1951 		offset = 4 - ((unsigned long)msdu->data & 3);
1952 		skb_put(msdu, offset);
1953 		memmove(msdu->data + offset, msdu->data, msdu->len);
1954 		skb_pull(msdu, offset);
1955 
1956 		/* FIXME: The frame is NWifi. Re-construct QoS Control
1957 		 * if possible later.
1958 		 */
1959 
1960 		memset(status, 0, sizeof(*status));
1961 		status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1962 
1963 		ath10k_htt_rx_h_rx_offload_prot(status, msdu);
1964 		ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
1965 		ath10k_process_rx(ar, status, msdu);
1966 	}
1967 }
1968 
ath10k_htt_rx_in_ord_ind(struct ath10k * ar,struct sk_buff * skb)1969 static void ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
1970 {
1971 	struct ath10k_htt *htt = &ar->htt;
1972 	struct htt_resp *resp = (void *)skb->data;
1973 	struct ieee80211_rx_status *status = &htt->rx_status;
1974 	struct sk_buff_head list;
1975 	struct sk_buff_head amsdu;
1976 	u16 peer_id;
1977 	u16 msdu_count;
1978 	u8 vdev_id;
1979 	u8 tid;
1980 	bool offload;
1981 	bool frag;
1982 	int ret;
1983 
1984 	lockdep_assert_held(&htt->rx_ring.lock);
1985 
1986 	if (htt->rx_confused)
1987 		return;
1988 
1989 	skb_pull(skb, sizeof(resp->hdr));
1990 	skb_pull(skb, sizeof(resp->rx_in_ord_ind));
1991 
1992 	peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
1993 	msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
1994 	vdev_id = resp->rx_in_ord_ind.vdev_id;
1995 	tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
1996 	offload = !!(resp->rx_in_ord_ind.info &
1997 			HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
1998 	frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);
1999 
2000 	ath10k_dbg(ar, ATH10K_DBG_HTT,
2001 		   "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
2002 		   vdev_id, peer_id, tid, offload, frag, msdu_count);
2003 
2004 	if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs)) {
2005 		ath10k_warn(ar, "dropping invalid in order rx indication\n");
2006 		return;
2007 	}
2008 
2009 	/* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
2010 	 * extracted and processed.
2011 	 */
2012 	__skb_queue_head_init(&list);
2013 	ret = ath10k_htt_rx_pop_paddr_list(htt, &resp->rx_in_ord_ind, &list);
2014 	if (ret < 0) {
2015 		ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
2016 		htt->rx_confused = true;
2017 		return;
2018 	}
2019 
2020 	/* Offloaded frames are very different and need to be handled
2021 	 * separately.
2022 	 */
2023 	if (offload)
2024 		ath10k_htt_rx_h_rx_offload(ar, &list);
2025 
2026 	while (!skb_queue_empty(&list)) {
2027 		__skb_queue_head_init(&amsdu);
2028 		ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
2029 		switch (ret) {
2030 		case 0:
2031 			/* Note: The in-order indication may report interleaved
2032 			 * frames from different PPDUs meaning reported rx rate
2033 			 * to mac80211 isn't accurate/reliable. It's still
2034 			 * better to report something than nothing though. This
2035 			 * should still give an idea about rx rate to the user.
2036 			 */
2037 			ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
2038 			ath10k_htt_rx_h_filter(ar, &amsdu, status);
2039 			ath10k_htt_rx_h_mpdu(ar, &amsdu, status, false);
2040 			ath10k_htt_rx_h_deliver(ar, &amsdu, status);
2041 			break;
2042 		case -EAGAIN:
2043 			/* fall through */
2044 		default:
2045 			/* Should not happen. */
2046 			ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
2047 			htt->rx_confused = true;
2048 			__skb_queue_purge(&list);
2049 			return;
2050 		}
2051 	}
2052 
2053 	tasklet_schedule(&htt->rx_replenish_task);
2054 }
2055 
ath10k_htt_t2h_msg_handler(struct ath10k * ar,struct sk_buff * skb)2056 void ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
2057 {
2058 	struct ath10k_htt *htt = &ar->htt;
2059 	struct htt_resp *resp = (struct htt_resp *)skb->data;
2060 	enum htt_t2h_msg_type type;
2061 
2062 	/* confirm alignment */
2063 	if (!IS_ALIGNED((unsigned long)skb->data, 4))
2064 		ath10k_warn(ar, "unaligned htt message, expect trouble\n");
2065 
2066 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
2067 		   resp->hdr.msg_type);
2068 
2069 	if (resp->hdr.msg_type >= ar->htt.t2h_msg_types_max) {
2070 		ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, unsupported msg_type: 0x%0X\n max: 0x%0X",
2071 			   resp->hdr.msg_type, ar->htt.t2h_msg_types_max);
2072 		dev_kfree_skb_any(skb);
2073 		return;
2074 	}
2075 	type = ar->htt.t2h_msg_types[resp->hdr.msg_type];
2076 
2077 	switch (type) {
2078 	case HTT_T2H_MSG_TYPE_VERSION_CONF: {
2079 		htt->target_version_major = resp->ver_resp.major;
2080 		htt->target_version_minor = resp->ver_resp.minor;
2081 		complete(&htt->target_version_received);
2082 		break;
2083 	}
2084 	case HTT_T2H_MSG_TYPE_RX_IND:
2085 		spin_lock_bh(&htt->rx_ring.lock);
2086 		__skb_queue_tail(&htt->rx_compl_q, skb);
2087 		spin_unlock_bh(&htt->rx_ring.lock);
2088 		tasklet_schedule(&htt->txrx_compl_task);
2089 		return;
2090 	case HTT_T2H_MSG_TYPE_PEER_MAP: {
2091 		struct htt_peer_map_event ev = {
2092 			.vdev_id = resp->peer_map.vdev_id,
2093 			.peer_id = __le16_to_cpu(resp->peer_map.peer_id),
2094 		};
2095 		memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
2096 		ath10k_peer_map_event(htt, &ev);
2097 		break;
2098 	}
2099 	case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
2100 		struct htt_peer_unmap_event ev = {
2101 			.peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
2102 		};
2103 		ath10k_peer_unmap_event(htt, &ev);
2104 		break;
2105 	}
2106 	case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
2107 		struct htt_tx_done tx_done = {};
2108 		int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
2109 
2110 		tx_done.msdu_id =
2111 			__le32_to_cpu(resp->mgmt_tx_completion.desc_id);
2112 
2113 		switch (status) {
2114 		case HTT_MGMT_TX_STATUS_OK:
2115 			tx_done.success = true;
2116 			break;
2117 		case HTT_MGMT_TX_STATUS_RETRY:
2118 			tx_done.no_ack = true;
2119 			break;
2120 		case HTT_MGMT_TX_STATUS_DROP:
2121 			tx_done.discard = true;
2122 			break;
2123 		}
2124 
2125 		ath10k_txrx_tx_unref(htt, &tx_done);
2126 		break;
2127 	}
2128 	case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
2129 		skb_queue_tail(&htt->tx_compl_q, skb);
2130 		tasklet_schedule(&htt->txrx_compl_task);
2131 		return;
2132 	case HTT_T2H_MSG_TYPE_SEC_IND: {
2133 		struct ath10k *ar = htt->ar;
2134 		struct htt_security_indication *ev = &resp->security_indication;
2135 
2136 		ath10k_dbg(ar, ATH10K_DBG_HTT,
2137 			   "sec ind peer_id %d unicast %d type %d\n",
2138 			  __le16_to_cpu(ev->peer_id),
2139 			  !!(ev->flags & HTT_SECURITY_IS_UNICAST),
2140 			  MS(ev->flags, HTT_SECURITY_TYPE));
2141 		complete(&ar->install_key_done);
2142 		break;
2143 	}
2144 	case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
2145 		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
2146 				skb->data, skb->len);
2147 		ath10k_htt_rx_frag_handler(htt, &resp->rx_frag_ind);
2148 		break;
2149 	}
2150 	case HTT_T2H_MSG_TYPE_TEST:
2151 		break;
2152 	case HTT_T2H_MSG_TYPE_STATS_CONF:
2153 		trace_ath10k_htt_stats(ar, skb->data, skb->len);
2154 		break;
2155 	case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
2156 		/* Firmware can return tx frames if it's unable to fully
2157 		 * process them and suspects host may be able to fix it. ath10k
2158 		 * sends all tx frames as already inspected so this shouldn't
2159 		 * happen unless fw has a bug.
2160 		 */
2161 		ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
2162 		break;
2163 	case HTT_T2H_MSG_TYPE_RX_ADDBA:
2164 		ath10k_htt_rx_addba(ar, resp);
2165 		break;
2166 	case HTT_T2H_MSG_TYPE_RX_DELBA:
2167 		ath10k_htt_rx_delba(ar, resp);
2168 		break;
2169 	case HTT_T2H_MSG_TYPE_PKTLOG: {
2170 		struct ath10k_pktlog_hdr *hdr =
2171 			(struct ath10k_pktlog_hdr *)resp->pktlog_msg.payload;
2172 
2173 		trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
2174 					sizeof(*hdr) +
2175 					__le16_to_cpu(hdr->size));
2176 		break;
2177 	}
2178 	case HTT_T2H_MSG_TYPE_RX_FLUSH: {
2179 		/* Ignore this event because mac80211 takes care of Rx
2180 		 * aggregation reordering.
2181 		 */
2182 		break;
2183 	}
2184 	case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
2185 		spin_lock_bh(&htt->rx_ring.lock);
2186 		__skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
2187 		spin_unlock_bh(&htt->rx_ring.lock);
2188 		tasklet_schedule(&htt->txrx_compl_task);
2189 		return;
2190 	}
2191 	case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND:
2192 		break;
2193 	case HTT_T2H_MSG_TYPE_CHAN_CHANGE:
2194 		break;
2195 	case HTT_T2H_MSG_TYPE_AGGR_CONF:
2196 		break;
2197 	case HTT_T2H_MSG_TYPE_EN_STATS:
2198 	case HTT_T2H_MSG_TYPE_TX_FETCH_IND:
2199 	case HTT_T2H_MSG_TYPE_TX_FETCH_CONF:
2200 	case HTT_T2H_MSG_TYPE_TX_LOW_LATENCY_IND:
2201 	default:
2202 		ath10k_warn(ar, "htt event (%d) not handled\n",
2203 			    resp->hdr.msg_type);
2204 		ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
2205 				skb->data, skb->len);
2206 		break;
2207 	};
2208 
2209 	/* Free the indication buffer */
2210 	dev_kfree_skb_any(skb);
2211 }
2212 EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
2213 
ath10k_htt_txrx_compl_task(unsigned long ptr)2214 static void ath10k_htt_txrx_compl_task(unsigned long ptr)
2215 {
2216 	struct ath10k_htt *htt = (struct ath10k_htt *)ptr;
2217 	struct ath10k *ar = htt->ar;
2218 	struct htt_resp *resp;
2219 	struct sk_buff *skb;
2220 
2221 	while ((skb = skb_dequeue(&htt->tx_compl_q))) {
2222 		ath10k_htt_rx_frm_tx_compl(htt->ar, skb);
2223 		dev_kfree_skb_any(skb);
2224 	}
2225 
2226 	spin_lock_bh(&htt->rx_ring.lock);
2227 	while ((skb = __skb_dequeue(&htt->rx_compl_q))) {
2228 		resp = (struct htt_resp *)skb->data;
2229 		ath10k_htt_rx_handler(htt, &resp->rx_ind);
2230 		dev_kfree_skb_any(skb);
2231 	}
2232 
2233 	while ((skb = __skb_dequeue(&htt->rx_in_ord_compl_q))) {
2234 		ath10k_htt_rx_in_ord_ind(ar, skb);
2235 		dev_kfree_skb_any(skb);
2236 	}
2237 	spin_unlock_bh(&htt->rx_ring.lock);
2238 }
2239