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