Linux 4.4.145
[linux/fpc-iii.git] / drivers / net / wireless / ath / ath10k / htt_rx.c
blobb32c47fe926dee59a22291377dac9ceeee4018e7
1 /*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
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.
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.
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"
26 #include <linux/log2.h>
28 #define HTT_RX_RING_SIZE HTT_RX_RING_SIZE_MAX
29 #define HTT_RX_RING_FILL_LEVEL (((HTT_RX_RING_SIZE) / 2) - 1)
31 /* when under memory pressure rx ring refill may fail and needs a retry */
32 #define HTT_RX_RING_REFILL_RETRY_MS 50
34 static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);
35 static void ath10k_htt_txrx_compl_task(unsigned long ptr);
37 static struct sk_buff *
38 ath10k_htt_rx_find_skb_paddr(struct ath10k *ar, u32 paddr)
40 struct ath10k_skb_rxcb *rxcb;
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);
46 WARN_ON_ONCE(1);
47 return NULL;
50 static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
52 struct sk_buff *skb;
53 struct ath10k_skb_rxcb *rxcb;
54 struct hlist_node *n;
55 int i;
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);
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;
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);
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]));
86 static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
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;
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.
99 BUILD_BUG_ON(HTT_RX_RING_FILL_LEVEL >= HTT_RX_RING_SIZE / 2);
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;
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);
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);
118 paddr = dma_map_single(htt->ar->dev, skb->data,
119 skb->len + skb_tailroom(skb),
120 DMA_FROM_DEVICE);
122 if (unlikely(dma_mapping_error(htt->ar->dev, paddr))) {
123 dev_kfree_skb_any(skb);
124 ret = -ENOMEM;
125 goto fail;
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++;
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);
140 num--;
141 idx++;
142 idx &= htt->rx_ring.size_mask;
145 fail:
147 * Make sure the rx buffer is updated before available buffer
148 * index to avoid any potential rx ring corruption.
150 mb();
151 *htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
152 return ret;
155 static int ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
157 lockdep_assert_held(&htt->rx_ring.lock);
158 return __ath10k_htt_rx_ring_fill_n(htt, num);
161 static void ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt *htt)
163 int ret, num_deficit, num_to_fill;
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.
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.
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) {
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.
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);
197 spin_unlock_bh(&htt->rx_ring.lock);
200 static void ath10k_htt_rx_ring_refill_retry(unsigned long arg)
202 struct ath10k_htt *htt = (struct ath10k_htt *)arg;
204 ath10k_htt_rx_msdu_buff_replenish(htt);
207 int ath10k_htt_rx_ring_refill(struct ath10k *ar)
209 struct ath10k_htt *htt = &ar->htt;
210 int ret;
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);
217 if (ret)
218 ath10k_htt_rx_ring_free(htt);
220 return ret;
223 void ath10k_htt_rx_free(struct ath10k_htt *htt)
225 del_timer_sync(&htt->rx_ring.refill_retry_timer);
226 tasklet_kill(&htt->rx_replenish_task);
227 tasklet_kill(&htt->txrx_compl_task);
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);
233 ath10k_htt_rx_ring_free(htt);
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);
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);
246 kfree(htt->rx_ring.netbufs_ring);
249 static inline struct sk_buff *ath10k_htt_rx_netbuf_pop(struct ath10k_htt *htt)
251 struct ath10k *ar = htt->ar;
252 int idx;
253 struct sk_buff *msdu;
255 lockdep_assert_held(&htt->rx_ring.lock);
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;
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;
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--;
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));
279 return msdu;
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)
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;
292 lockdep_assert_held(&htt->rx_ring.lock);
294 for (;;) {
295 int last_msdu, msdu_len_invalid, msdu_chained;
297 msdu = ath10k_htt_rx_netbuf_pop(htt);
298 if (!msdu) {
299 __skb_queue_purge(amsdu);
300 return -ENOENT;
303 __skb_queue_tail(amsdu, msdu);
305 rx_desc = (struct htt_rx_desc *)msdu->data;
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));
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.
320 if (!(__le32_to_cpu(rx_desc->attention.flags)
321 & RX_ATTENTION_FLAGS_MSDU_DONE)) {
322 __skb_queue_purge(amsdu);
323 return -EIO;
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.)
340 if (*fw_desc_len > 0) {
341 rx_desc->fw_desc.info0 = **fw_desc;
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).
349 /* or more, if there's extension data */
350 (*fw_desc)++;
351 (*fw_desc_len)--;
352 } else {
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.
361 * FIX THIS - the FW descriptors are actually for
362 * MSDUs in the end of this A-MSDU instead of the
363 * beginning.
365 rx_desc->fw_desc.info0 = 0;
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;
375 if (msdu_len_invalid)
376 msdu_len = 0;
378 skb_trim(msdu, 0);
379 skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
380 msdu_len -= msdu->len;
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;
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;
397 last_msdu = __le32_to_cpu(rx_desc->msdu_end.common.info0) &
398 RX_MSDU_END_INFO0_LAST_MSDU;
400 trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
401 sizeof(*rx_desc) - sizeof(u32));
403 if (last_msdu)
404 break;
407 if (skb_queue_empty(amsdu))
408 msdu_chaining = -1;
411 * Don't refill the ring yet.
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.
423 return msdu_chaining;
426 static void ath10k_htt_rx_replenish_task(unsigned long ptr)
428 struct ath10k_htt *htt = (struct ath10k_htt *)ptr;
430 ath10k_htt_rx_msdu_buff_replenish(htt);
433 static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
434 u32 paddr)
436 struct ath10k *ar = htt->ar;
437 struct ath10k_skb_rxcb *rxcb;
438 struct sk_buff *msdu;
440 lockdep_assert_held(&htt->rx_ring.lock);
442 msdu = ath10k_htt_rx_find_skb_paddr(ar, paddr);
443 if (!msdu)
444 return NULL;
446 rxcb = ATH10K_SKB_RXCB(msdu);
447 hash_del(&rxcb->hlist);
448 htt->rx_ring.fill_cnt--;
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));
456 return msdu;
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)
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;
471 lockdep_assert_held(&htt->rx_ring.lock);
473 msdu_count = __le16_to_cpu(ev->msdu_count);
474 is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
476 while (msdu_count--) {
477 paddr = __le32_to_cpu(msdu_desc->msdu_paddr);
479 msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
480 if (!msdu) {
481 __skb_queue_purge(list);
482 return -ENOENT;
485 __skb_queue_tail(list, msdu);
487 if (!is_offload) {
488 rxd = (void *)msdu->data;
490 trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
492 skb_put(msdu, sizeof(*rxd));
493 skb_pull(msdu, sizeof(*rxd));
494 skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
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;
503 msdu_desc++;
506 return 0;
509 int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
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;
517 htt->rx_confused = false;
519 /* XXX: The fill level could be changed during runtime in response to
520 * the host processing latency. Is this really worth it?
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;
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;
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;
537 size = htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring);
539 vaddr = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_DMA);
540 if (!vaddr)
541 goto err_dma_ring;
543 htt->rx_ring.paddrs_ring = vaddr;
544 htt->rx_ring.base_paddr = paddr;
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;
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;
557 /* Initialize the Rx refill retry timer */
558 setup_timer(timer, ath10k_htt_rx_ring_refill_retry, (unsigned long)htt);
560 spin_lock_init(&htt->rx_ring.lock);
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);
566 tasklet_init(&htt->rx_replenish_task, ath10k_htt_rx_replenish_task,
567 (unsigned long)htt);
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);
573 tasklet_init(&htt->txrx_compl_task, ath10k_htt_txrx_compl_task,
574 (unsigned long)htt);
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;
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;
592 static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
593 enum htt_rx_mpdu_encrypt_type type)
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;
611 ath10k_warn(ar, "unsupported encryption type %d\n", type);
612 return 0;
615 #define MICHAEL_MIC_LEN 8
617 static int ath10k_htt_rx_crypto_tail_len(struct ath10k *ar,
618 enum htt_rx_mpdu_encrypt_type type)
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;
636 ath10k_warn(ar, "unsupported encryption type %d\n", type);
637 return 0;
640 struct amsdu_subframe_hdr {
641 u8 dst[ETH_ALEN];
642 u8 src[ETH_ALEN];
643 __be16 len;
644 } __packed;
646 #define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)
648 static void ath10k_htt_rx_h_rates(struct ath10k *ar,
649 struct ieee80211_rx_status *status,
650 struct htt_rx_desc *rxd)
652 struct ieee80211_supported_band *sband;
653 u8 cck, rate, bw, sgi, mcs, nss;
654 u8 preamble = 0;
655 u8 group_id;
656 u32 info1, info2, info3;
658 info1 = __le32_to_cpu(rxd->ppdu_start.info1);
659 info2 = __le32_to_cpu(rxd->ppdu_start.info2);
660 info3 = __le32_to_cpu(rxd->ppdu_start.info3);
662 preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
664 switch (preamble) {
665 case HTT_RX_LEGACY:
666 /* To get legacy rate index band is required. Since band can't
667 * be undefined check if freq is non-zero.
669 if (!status->freq)
670 return;
672 cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
673 rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
674 rate &= ~RX_PPDU_START_RATE_FLAG;
676 sband = &ar->mac.sbands[status->band];
677 status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate);
678 break;
679 case HTT_RX_HT:
680 case HTT_RX_HT_WITH_TXBF:
681 /* HT-SIG - Table 20-11 in info2 and info3 */
682 mcs = info2 & 0x1F;
683 nss = mcs >> 3;
684 bw = (info2 >> 7) & 1;
685 sgi = (info3 >> 7) & 1;
687 status->rate_idx = mcs;
688 status->flag |= RX_FLAG_HT;
689 if (sgi)
690 status->flag |= RX_FLAG_SHORT_GI;
691 if (bw)
692 status->flag |= RX_FLAG_40MHZ;
693 break;
694 case HTT_RX_VHT:
695 case HTT_RX_VHT_WITH_TXBF:
696 /* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
697 TODO check this */
698 bw = info2 & 3;
699 sgi = info3 & 1;
700 group_id = (info2 >> 4) & 0x3F;
702 if (GROUP_ID_IS_SU_MIMO(group_id)) {
703 mcs = (info3 >> 4) & 0x0F;
704 nss = ((info2 >> 10) & 0x07) + 1;
705 } else {
706 /* Hardware doesn't decode VHT-SIG-B into Rx descriptor
707 * so it's impossible to decode MCS. Also since
708 * firmware consumes Group Id Management frames host
709 * has no knowledge regarding group/user position
710 * mapping so it's impossible to pick the correct Nsts
711 * from VHT-SIG-A1.
713 * Bandwidth and SGI are valid so report the rateinfo
714 * on best-effort basis.
716 mcs = 0;
717 nss = 1;
720 if (mcs > 0x09) {
721 ath10k_warn(ar, "invalid MCS received %u\n", mcs);
722 ath10k_warn(ar, "rxd %08x mpdu start %08x %08x msdu start %08x %08x ppdu start %08x %08x %08x %08x %08x\n",
723 __le32_to_cpu(rxd->attention.flags),
724 __le32_to_cpu(rxd->mpdu_start.info0),
725 __le32_to_cpu(rxd->mpdu_start.info1),
726 __le32_to_cpu(rxd->msdu_start.common.info0),
727 __le32_to_cpu(rxd->msdu_start.common.info1),
728 rxd->ppdu_start.info0,
729 __le32_to_cpu(rxd->ppdu_start.info1),
730 __le32_to_cpu(rxd->ppdu_start.info2),
731 __le32_to_cpu(rxd->ppdu_start.info3),
732 __le32_to_cpu(rxd->ppdu_start.info4));
734 ath10k_warn(ar, "msdu end %08x mpdu end %08x\n",
735 __le32_to_cpu(rxd->msdu_end.common.info0),
736 __le32_to_cpu(rxd->mpdu_end.info0));
738 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL,
739 "rx desc msdu payload: ",
740 rxd->msdu_payload, 50);
743 status->rate_idx = mcs;
744 status->vht_nss = nss;
746 if (sgi)
747 status->flag |= RX_FLAG_SHORT_GI;
749 switch (bw) {
750 /* 20MHZ */
751 case 0:
752 break;
753 /* 40MHZ */
754 case 1:
755 status->flag |= RX_FLAG_40MHZ;
756 break;
757 /* 80MHZ */
758 case 2:
759 status->vht_flag |= RX_VHT_FLAG_80MHZ;
762 status->flag |= RX_FLAG_VHT;
763 break;
764 default:
765 break;
769 static struct ieee80211_channel *
770 ath10k_htt_rx_h_peer_channel(struct ath10k *ar, struct htt_rx_desc *rxd)
772 struct ath10k_peer *peer;
773 struct ath10k_vif *arvif;
774 struct cfg80211_chan_def def;
775 u16 peer_id;
777 lockdep_assert_held(&ar->data_lock);
779 if (!rxd)
780 return NULL;
782 if (rxd->attention.flags &
783 __cpu_to_le32(RX_ATTENTION_FLAGS_PEER_IDX_INVALID))
784 return NULL;
786 if (!(rxd->msdu_end.common.info0 &
787 __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU)))
788 return NULL;
790 peer_id = MS(__le32_to_cpu(rxd->mpdu_start.info0),
791 RX_MPDU_START_INFO0_PEER_IDX);
793 peer = ath10k_peer_find_by_id(ar, peer_id);
794 if (!peer)
795 return NULL;
797 arvif = ath10k_get_arvif(ar, peer->vdev_id);
798 if (WARN_ON_ONCE(!arvif))
799 return NULL;
801 if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
802 return NULL;
804 return def.chan;
807 static struct ieee80211_channel *
808 ath10k_htt_rx_h_vdev_channel(struct ath10k *ar, u32 vdev_id)
810 struct ath10k_vif *arvif;
811 struct cfg80211_chan_def def;
813 lockdep_assert_held(&ar->data_lock);
815 list_for_each_entry(arvif, &ar->arvifs, list) {
816 if (arvif->vdev_id == vdev_id &&
817 ath10k_mac_vif_chan(arvif->vif, &def) == 0)
818 return def.chan;
821 return NULL;
824 static void
825 ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw *hw,
826 struct ieee80211_chanctx_conf *conf,
827 void *data)
829 struct cfg80211_chan_def *def = data;
831 *def = conf->def;
834 static struct ieee80211_channel *
835 ath10k_htt_rx_h_any_channel(struct ath10k *ar)
837 struct cfg80211_chan_def def = {};
839 ieee80211_iter_chan_contexts_atomic(ar->hw,
840 ath10k_htt_rx_h_any_chan_iter,
841 &def);
843 return def.chan;
846 static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
847 struct ieee80211_rx_status *status,
848 struct htt_rx_desc *rxd,
849 u32 vdev_id)
851 struct ieee80211_channel *ch;
853 spin_lock_bh(&ar->data_lock);
854 ch = ar->scan_channel;
855 if (!ch)
856 ch = ar->rx_channel;
857 if (!ch)
858 ch = ath10k_htt_rx_h_peer_channel(ar, rxd);
859 if (!ch)
860 ch = ath10k_htt_rx_h_vdev_channel(ar, vdev_id);
861 if (!ch)
862 ch = ath10k_htt_rx_h_any_channel(ar);
863 spin_unlock_bh(&ar->data_lock);
865 if (!ch)
866 return false;
868 status->band = ch->band;
869 status->freq = ch->center_freq;
871 return true;
874 static void ath10k_htt_rx_h_signal(struct ath10k *ar,
875 struct ieee80211_rx_status *status,
876 struct htt_rx_desc *rxd)
878 /* FIXME: Get real NF */
879 status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
880 rxd->ppdu_start.rssi_comb;
881 status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
884 static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
885 struct ieee80211_rx_status *status,
886 struct htt_rx_desc *rxd)
888 /* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
889 * means all prior MSDUs in a PPDU are reported to mac80211 without the
890 * TSF. Is it worth holding frames until end of PPDU is known?
892 * FIXME: Can we get/compute 64bit TSF?
894 status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
895 status->flag |= RX_FLAG_MACTIME_END;
898 static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
899 struct sk_buff_head *amsdu,
900 struct ieee80211_rx_status *status,
901 u32 vdev_id)
903 struct sk_buff *first;
904 struct htt_rx_desc *rxd;
905 bool is_first_ppdu;
906 bool is_last_ppdu;
908 if (skb_queue_empty(amsdu))
909 return;
911 first = skb_peek(amsdu);
912 rxd = (void *)first->data - sizeof(*rxd);
914 is_first_ppdu = !!(rxd->attention.flags &
915 __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
916 is_last_ppdu = !!(rxd->attention.flags &
917 __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));
919 if (is_first_ppdu) {
920 /* New PPDU starts so clear out the old per-PPDU status. */
921 status->freq = 0;
922 status->rate_idx = 0;
923 status->vht_nss = 0;
924 status->vht_flag &= ~RX_VHT_FLAG_80MHZ;
925 status->flag &= ~(RX_FLAG_HT |
926 RX_FLAG_VHT |
927 RX_FLAG_SHORT_GI |
928 RX_FLAG_40MHZ |
929 RX_FLAG_MACTIME_END);
930 status->flag |= RX_FLAG_NO_SIGNAL_VAL;
932 ath10k_htt_rx_h_signal(ar, status, rxd);
933 ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
934 ath10k_htt_rx_h_rates(ar, status, rxd);
937 if (is_last_ppdu)
938 ath10k_htt_rx_h_mactime(ar, status, rxd);
941 static const char * const tid_to_ac[] = {
942 "BE",
943 "BK",
944 "BK",
945 "BE",
946 "VI",
947 "VI",
948 "VO",
949 "VO",
952 static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
954 u8 *qc;
955 int tid;
957 if (!ieee80211_is_data_qos(hdr->frame_control))
958 return "";
960 qc = ieee80211_get_qos_ctl(hdr);
961 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
962 if (tid < 8)
963 snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
964 else
965 snprintf(out, size, "tid %d", tid);
967 return out;
970 static void ath10k_process_rx(struct ath10k *ar,
971 struct ieee80211_rx_status *rx_status,
972 struct sk_buff *skb)
974 struct ieee80211_rx_status *status;
975 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
976 char tid[32];
978 status = IEEE80211_SKB_RXCB(skb);
979 *status = *rx_status;
981 ath10k_dbg(ar, ATH10K_DBG_DATA,
982 "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",
983 skb,
984 skb->len,
985 ieee80211_get_SA(hdr),
986 ath10k_get_tid(hdr, tid, sizeof(tid)),
987 is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
988 "mcast" : "ucast",
989 (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
990 status->flag == 0 ? "legacy" : "",
991 status->flag & RX_FLAG_HT ? "ht" : "",
992 status->flag & RX_FLAG_VHT ? "vht" : "",
993 status->flag & RX_FLAG_40MHZ ? "40" : "",
994 status->vht_flag & RX_VHT_FLAG_80MHZ ? "80" : "",
995 status->flag & RX_FLAG_SHORT_GI ? "sgi " : "",
996 status->rate_idx,
997 status->vht_nss,
998 status->freq,
999 status->band, status->flag,
1000 !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
1001 !!(status->flag & RX_FLAG_MMIC_ERROR),
1002 !!(status->flag & RX_FLAG_AMSDU_MORE));
1003 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
1004 skb->data, skb->len);
1005 trace_ath10k_rx_hdr(ar, skb->data, skb->len);
1006 trace_ath10k_rx_payload(ar, skb->data, skb->len);
1008 ieee80211_rx(ar->hw, skb);
1011 static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
1012 struct ieee80211_hdr *hdr)
1014 int len = ieee80211_hdrlen(hdr->frame_control);
1016 if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
1017 ar->fw_features))
1018 len = round_up(len, 4);
1020 return len;
1023 static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
1024 struct sk_buff *msdu,
1025 struct ieee80211_rx_status *status,
1026 enum htt_rx_mpdu_encrypt_type enctype,
1027 bool is_decrypted)
1029 struct ieee80211_hdr *hdr;
1030 struct htt_rx_desc *rxd;
1031 size_t hdr_len;
1032 size_t crypto_len;
1033 bool is_first;
1034 bool is_last;
1036 rxd = (void *)msdu->data - sizeof(*rxd);
1037 is_first = !!(rxd->msdu_end.common.info0 &
1038 __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
1039 is_last = !!(rxd->msdu_end.common.info0 &
1040 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
1042 /* Delivered decapped frame:
1043 * [802.11 header]
1044 * [crypto param] <-- can be trimmed if !fcs_err &&
1045 * !decrypt_err && !peer_idx_invalid
1046 * [amsdu header] <-- only if A-MSDU
1047 * [rfc1042/llc]
1048 * [payload]
1049 * [FCS] <-- at end, needs to be trimmed
1052 /* This probably shouldn't happen but warn just in case */
1053 if (unlikely(WARN_ON_ONCE(!is_first)))
1054 return;
1056 /* This probably shouldn't happen but warn just in case */
1057 if (unlikely(WARN_ON_ONCE(!(is_first && is_last))))
1058 return;
1060 skb_trim(msdu, msdu->len - FCS_LEN);
1062 /* In most cases this will be true for sniffed frames. It makes sense
1063 * to deliver them as-is without stripping the crypto param. This is
1064 * necessary for software based decryption.
1066 * If there's no error then the frame is decrypted. At least that is
1067 * the case for frames that come in via fragmented rx indication.
1069 if (!is_decrypted)
1070 return;
1072 /* The payload is decrypted so strip crypto params. Start from tail
1073 * since hdr is used to compute some stuff.
1076 hdr = (void *)msdu->data;
1078 /* Tail */
1079 if (status->flag & RX_FLAG_IV_STRIPPED) {
1080 skb_trim(msdu, msdu->len -
1081 ath10k_htt_rx_crypto_tail_len(ar, enctype));
1082 } else {
1083 /* MIC */
1084 if ((status->flag & RX_FLAG_MIC_STRIPPED) &&
1085 enctype == HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2)
1086 skb_trim(msdu, msdu->len - 8);
1088 /* ICV */
1089 if (status->flag & RX_FLAG_ICV_STRIPPED &&
1090 enctype != HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2)
1091 skb_trim(msdu, msdu->len -
1092 ath10k_htt_rx_crypto_tail_len(ar, enctype));
1095 /* MMIC */
1096 if (!ieee80211_has_morefrags(hdr->frame_control) &&
1097 enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
1098 skb_trim(msdu, msdu->len - 8);
1100 /* Head */
1101 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1102 crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1104 memmove((void *)msdu->data + crypto_len,
1105 (void *)msdu->data, hdr_len);
1106 skb_pull(msdu, crypto_len);
1109 static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
1110 struct sk_buff *msdu,
1111 struct ieee80211_rx_status *status,
1112 const u8 first_hdr[64],
1113 enum htt_rx_mpdu_encrypt_type enctype)
1115 struct ieee80211_hdr *hdr;
1116 size_t hdr_len;
1117 u8 da[ETH_ALEN];
1118 u8 sa[ETH_ALEN];
1119 int bytes_aligned = ar->hw_params.decap_align_bytes;
1121 /* Delivered decapped frame:
1122 * [nwifi 802.11 header] <-- replaced with 802.11 hdr
1123 * [rfc1042/llc]
1125 * Note: The nwifi header doesn't have QoS Control and is
1126 * (always?) a 3addr frame.
1128 * Note2: There's no A-MSDU subframe header. Even if it's part
1129 * of an A-MSDU.
1132 /* pull decapped header and copy SA & DA */
1133 hdr = (struct ieee80211_hdr *)msdu->data;
1134 hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
1135 ether_addr_copy(da, ieee80211_get_DA(hdr));
1136 ether_addr_copy(sa, ieee80211_get_SA(hdr));
1137 skb_pull(msdu, hdr_len);
1139 /* push original 802.11 header */
1140 hdr = (struct ieee80211_hdr *)first_hdr;
1141 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1143 if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1144 memcpy(skb_push(msdu,
1145 ath10k_htt_rx_crypto_param_len(ar, enctype)),
1146 (void *)hdr + round_up(hdr_len, bytes_aligned),
1147 ath10k_htt_rx_crypto_param_len(ar, enctype));
1150 memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1152 /* original 802.11 header has a different DA and in
1153 * case of 4addr it may also have different SA
1155 hdr = (struct ieee80211_hdr *)msdu->data;
1156 ether_addr_copy(ieee80211_get_DA(hdr), da);
1157 ether_addr_copy(ieee80211_get_SA(hdr), sa);
1160 static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
1161 struct sk_buff *msdu,
1162 enum htt_rx_mpdu_encrypt_type enctype)
1164 struct ieee80211_hdr *hdr;
1165 struct htt_rx_desc *rxd;
1166 size_t hdr_len, crypto_len;
1167 void *rfc1042;
1168 bool is_first, is_last, is_amsdu;
1169 int bytes_aligned = ar->hw_params.decap_align_bytes;
1171 rxd = (void *)msdu->data - sizeof(*rxd);
1172 hdr = (void *)rxd->rx_hdr_status;
1174 is_first = !!(rxd->msdu_end.common.info0 &
1175 __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
1176 is_last = !!(rxd->msdu_end.common.info0 &
1177 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
1178 is_amsdu = !(is_first && is_last);
1180 rfc1042 = hdr;
1182 if (is_first) {
1183 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1184 crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
1186 rfc1042 += round_up(hdr_len, bytes_aligned) +
1187 round_up(crypto_len, bytes_aligned);
1190 if (is_amsdu)
1191 rfc1042 += sizeof(struct amsdu_subframe_hdr);
1193 return rfc1042;
1196 static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
1197 struct sk_buff *msdu,
1198 struct ieee80211_rx_status *status,
1199 const u8 first_hdr[64],
1200 enum htt_rx_mpdu_encrypt_type enctype)
1202 struct ieee80211_hdr *hdr;
1203 struct ethhdr *eth;
1204 size_t hdr_len;
1205 void *rfc1042;
1206 u8 da[ETH_ALEN];
1207 u8 sa[ETH_ALEN];
1208 int bytes_aligned = ar->hw_params.decap_align_bytes;
1210 /* Delivered decapped frame:
1211 * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
1212 * [payload]
1215 rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
1216 if (WARN_ON_ONCE(!rfc1042))
1217 return;
1219 /* pull decapped header and copy SA & DA */
1220 eth = (struct ethhdr *)msdu->data;
1221 ether_addr_copy(da, eth->h_dest);
1222 ether_addr_copy(sa, eth->h_source);
1223 skb_pull(msdu, sizeof(struct ethhdr));
1225 /* push rfc1042/llc/snap */
1226 memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
1227 sizeof(struct rfc1042_hdr));
1229 /* push original 802.11 header */
1230 hdr = (struct ieee80211_hdr *)first_hdr;
1231 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1233 if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1234 memcpy(skb_push(msdu,
1235 ath10k_htt_rx_crypto_param_len(ar, enctype)),
1236 (void *)hdr + round_up(hdr_len, bytes_aligned),
1237 ath10k_htt_rx_crypto_param_len(ar, enctype));
1240 memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1242 /* original 802.11 header has a different DA and in
1243 * case of 4addr it may also have different SA
1245 hdr = (struct ieee80211_hdr *)msdu->data;
1246 ether_addr_copy(ieee80211_get_DA(hdr), da);
1247 ether_addr_copy(ieee80211_get_SA(hdr), sa);
1250 static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
1251 struct sk_buff *msdu,
1252 struct ieee80211_rx_status *status,
1253 const u8 first_hdr[64],
1254 enum htt_rx_mpdu_encrypt_type enctype)
1256 struct ieee80211_hdr *hdr;
1257 size_t hdr_len;
1258 int bytes_aligned = ar->hw_params.decap_align_bytes;
1260 /* Delivered decapped frame:
1261 * [amsdu header] <-- replaced with 802.11 hdr
1262 * [rfc1042/llc]
1263 * [payload]
1266 skb_pull(msdu, sizeof(struct amsdu_subframe_hdr));
1268 hdr = (struct ieee80211_hdr *)first_hdr;
1269 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1271 if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1272 memcpy(skb_push(msdu,
1273 ath10k_htt_rx_crypto_param_len(ar, enctype)),
1274 (void *)hdr + round_up(hdr_len, bytes_aligned),
1275 ath10k_htt_rx_crypto_param_len(ar, enctype));
1278 memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1281 static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
1282 struct sk_buff *msdu,
1283 struct ieee80211_rx_status *status,
1284 u8 first_hdr[64],
1285 enum htt_rx_mpdu_encrypt_type enctype,
1286 bool is_decrypted)
1288 struct htt_rx_desc *rxd;
1289 enum rx_msdu_decap_format decap;
1291 /* First msdu's decapped header:
1292 * [802.11 header] <-- padded to 4 bytes long
1293 * [crypto param] <-- padded to 4 bytes long
1294 * [amsdu header] <-- only if A-MSDU
1295 * [rfc1042/llc]
1297 * Other (2nd, 3rd, ..) msdu's decapped header:
1298 * [amsdu header] <-- only if A-MSDU
1299 * [rfc1042/llc]
1302 rxd = (void *)msdu->data - sizeof(*rxd);
1303 decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
1304 RX_MSDU_START_INFO1_DECAP_FORMAT);
1306 switch (decap) {
1307 case RX_MSDU_DECAP_RAW:
1308 ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
1309 is_decrypted);
1310 break;
1311 case RX_MSDU_DECAP_NATIVE_WIFI:
1312 ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr,
1313 enctype);
1314 break;
1315 case RX_MSDU_DECAP_ETHERNET2_DIX:
1316 ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
1317 break;
1318 case RX_MSDU_DECAP_8023_SNAP_LLC:
1319 ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr,
1320 enctype);
1321 break;
1325 static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
1327 struct htt_rx_desc *rxd;
1328 u32 flags, info;
1329 bool is_ip4, is_ip6;
1330 bool is_tcp, is_udp;
1331 bool ip_csum_ok, tcpudp_csum_ok;
1333 rxd = (void *)skb->data - sizeof(*rxd);
1334 flags = __le32_to_cpu(rxd->attention.flags);
1335 info = __le32_to_cpu(rxd->msdu_start.common.info1);
1337 is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
1338 is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
1339 is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
1340 is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
1341 ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
1342 tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
1344 if (!is_ip4 && !is_ip6)
1345 return CHECKSUM_NONE;
1346 if (!is_tcp && !is_udp)
1347 return CHECKSUM_NONE;
1348 if (!ip_csum_ok)
1349 return CHECKSUM_NONE;
1350 if (!tcpudp_csum_ok)
1351 return CHECKSUM_NONE;
1353 return CHECKSUM_UNNECESSARY;
1356 static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
1358 msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
1361 static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
1362 struct sk_buff_head *amsdu,
1363 struct ieee80211_rx_status *status,
1364 bool fill_crypt_header)
1366 struct sk_buff *first;
1367 struct sk_buff *last;
1368 struct sk_buff *msdu;
1369 struct htt_rx_desc *rxd;
1370 struct ieee80211_hdr *hdr;
1371 enum htt_rx_mpdu_encrypt_type enctype;
1372 u8 first_hdr[64];
1373 u8 *qos;
1374 bool has_fcs_err;
1375 bool has_crypto_err;
1376 bool has_tkip_err;
1377 bool has_peer_idx_invalid;
1378 bool is_decrypted;
1379 u32 attention;
1381 if (skb_queue_empty(amsdu))
1382 return;
1384 first = skb_peek(amsdu);
1385 rxd = (void *)first->data - sizeof(*rxd);
1387 enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1388 RX_MPDU_START_INFO0_ENCRYPT_TYPE);
1390 /* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
1391 * decapped header. It'll be used for undecapping of each MSDU.
1393 hdr = (void *)rxd->rx_hdr_status;
1394 memcpy(first_hdr, hdr, RX_HTT_HDR_STATUS_LEN);
1396 /* Each A-MSDU subframe will use the original header as the base and be
1397 * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
1399 hdr = (void *)first_hdr;
1401 if (ieee80211_is_data_qos(hdr->frame_control)) {
1402 qos = ieee80211_get_qos_ctl(hdr);
1403 qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1406 /* Some attention flags are valid only in the last MSDU. */
1407 last = skb_peek_tail(amsdu);
1408 rxd = (void *)last->data - sizeof(*rxd);
1409 attention = __le32_to_cpu(rxd->attention.flags);
1411 has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
1412 has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
1413 has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
1414 has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);
1416 /* Note: If hardware captures an encrypted frame that it can't decrypt,
1417 * e.g. due to fcs error, missing peer or invalid key data it will
1418 * report the frame as raw.
1420 is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
1421 !has_fcs_err &&
1422 !has_crypto_err &&
1423 !has_peer_idx_invalid);
1425 /* Clear per-MPDU flags while leaving per-PPDU flags intact. */
1426 status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
1427 RX_FLAG_MMIC_ERROR |
1428 RX_FLAG_DECRYPTED |
1429 RX_FLAG_IV_STRIPPED |
1430 RX_FLAG_MMIC_STRIPPED);
1432 if (has_fcs_err)
1433 status->flag |= RX_FLAG_FAILED_FCS_CRC;
1435 if (has_tkip_err)
1436 status->flag |= RX_FLAG_MMIC_ERROR;
1438 if (is_decrypted) {
1439 status->flag |= RX_FLAG_DECRYPTED |
1440 RX_FLAG_MMIC_STRIPPED;
1442 if (fill_crypt_header)
1443 status->flag |= RX_FLAG_MIC_STRIPPED |
1444 RX_FLAG_ICV_STRIPPED;
1445 else
1446 status->flag |= RX_FLAG_IV_STRIPPED;
1449 skb_queue_walk(amsdu, msdu) {
1450 ath10k_htt_rx_h_csum_offload(msdu);
1451 ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
1452 is_decrypted);
1454 /* Undecapping involves copying the original 802.11 header back
1455 * to sk_buff. If frame is protected and hardware has decrypted
1456 * it then remove the protected bit.
1458 if (!is_decrypted)
1459 continue;
1461 if (fill_crypt_header)
1462 continue;
1464 hdr = (void *)msdu->data;
1465 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1469 static void ath10k_htt_rx_h_deliver(struct ath10k *ar,
1470 struct sk_buff_head *amsdu,
1471 struct ieee80211_rx_status *status)
1473 struct sk_buff *msdu;
1474 struct sk_buff *first_subframe;
1476 first_subframe = skb_peek(amsdu);
1478 while ((msdu = __skb_dequeue(amsdu))) {
1479 /* Setup per-MSDU flags */
1480 if (skb_queue_empty(amsdu))
1481 status->flag &= ~RX_FLAG_AMSDU_MORE;
1482 else
1483 status->flag |= RX_FLAG_AMSDU_MORE;
1485 if (msdu == first_subframe) {
1486 first_subframe = NULL;
1487 status->flag &= ~RX_FLAG_ALLOW_SAME_PN;
1488 } else {
1489 status->flag |= RX_FLAG_ALLOW_SAME_PN;
1492 ath10k_process_rx(ar, status, msdu);
1496 static int ath10k_unchain_msdu(struct sk_buff_head *amsdu)
1498 struct sk_buff *skb, *first;
1499 int space;
1500 int total_len = 0;
1502 /* TODO: Might could optimize this by using
1503 * skb_try_coalesce or similar method to
1504 * decrease copying, or maybe get mac80211 to
1505 * provide a way to just receive a list of
1506 * skb?
1509 first = __skb_dequeue(amsdu);
1511 /* Allocate total length all at once. */
1512 skb_queue_walk(amsdu, skb)
1513 total_len += skb->len;
1515 space = total_len - skb_tailroom(first);
1516 if ((space > 0) &&
1517 (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
1518 /* TODO: bump some rx-oom error stat */
1519 /* put it back together so we can free the
1520 * whole list at once.
1522 __skb_queue_head(amsdu, first);
1523 return -1;
1526 /* Walk list again, copying contents into
1527 * msdu_head
1529 while ((skb = __skb_dequeue(amsdu))) {
1530 skb_copy_from_linear_data(skb, skb_put(first, skb->len),
1531 skb->len);
1532 dev_kfree_skb_any(skb);
1535 __skb_queue_head(amsdu, first);
1536 return 0;
1539 static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
1540 struct sk_buff_head *amsdu,
1541 bool chained)
1543 struct sk_buff *first;
1544 struct htt_rx_desc *rxd;
1545 enum rx_msdu_decap_format decap;
1547 first = skb_peek(amsdu);
1548 rxd = (void *)first->data - sizeof(*rxd);
1549 decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
1550 RX_MSDU_START_INFO1_DECAP_FORMAT);
1552 if (!chained)
1553 return;
1555 /* FIXME: Current unchaining logic can only handle simple case of raw
1556 * msdu chaining. If decapping is other than raw the chaining may be
1557 * more complex and this isn't handled by the current code. Don't even
1558 * try re-constructing such frames - it'll be pretty much garbage.
1560 if (decap != RX_MSDU_DECAP_RAW ||
1561 skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
1562 __skb_queue_purge(amsdu);
1563 return;
1566 ath10k_unchain_msdu(amsdu);
1569 static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
1570 struct sk_buff_head *amsdu,
1571 struct ieee80211_rx_status *rx_status)
1573 struct sk_buff *msdu;
1574 struct htt_rx_desc *rxd;
1575 bool is_mgmt;
1576 bool has_fcs_err;
1578 msdu = skb_peek(amsdu);
1579 rxd = (void *)msdu->data - sizeof(*rxd);
1581 /* FIXME: It might be a good idea to do some fuzzy-testing to drop
1582 * invalid/dangerous frames.
1585 if (!rx_status->freq) {
1586 ath10k_warn(ar, "no channel configured; ignoring frame(s)!\n");
1587 return false;
1590 is_mgmt = !!(rxd->attention.flags &
1591 __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));
1592 has_fcs_err = !!(rxd->attention.flags &
1593 __cpu_to_le32(RX_ATTENTION_FLAGS_FCS_ERR));
1595 /* Management frames are handled via WMI events. The pros of such
1596 * approach is that channel is explicitly provided in WMI events
1597 * whereas HTT doesn't provide channel information for Rxed frames.
1599 * However some firmware revisions don't report corrupted frames via
1600 * WMI so don't drop them.
1602 if (is_mgmt && !has_fcs_err) {
1603 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx mgmt ctrl\n");
1604 return false;
1607 if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
1608 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
1609 return false;
1612 return true;
1615 static void ath10k_htt_rx_h_filter(struct ath10k *ar,
1616 struct sk_buff_head *amsdu,
1617 struct ieee80211_rx_status *rx_status)
1619 if (skb_queue_empty(amsdu))
1620 return;
1622 if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
1623 return;
1625 __skb_queue_purge(amsdu);
1628 static void ath10k_htt_rx_handler(struct ath10k_htt *htt,
1629 struct htt_rx_indication *rx)
1631 struct ath10k *ar = htt->ar;
1632 struct ieee80211_rx_status *rx_status = &htt->rx_status;
1633 struct htt_rx_indication_mpdu_range *mpdu_ranges;
1634 struct sk_buff_head amsdu;
1635 int num_mpdu_ranges;
1636 int fw_desc_len;
1637 u8 *fw_desc;
1638 int i, ret, mpdu_count = 0;
1640 lockdep_assert_held(&htt->rx_ring.lock);
1642 if (htt->rx_confused)
1643 return;
1645 fw_desc_len = __le16_to_cpu(rx->prefix.fw_rx_desc_bytes);
1646 fw_desc = (u8 *)&rx->fw_desc;
1648 num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
1649 HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
1650 mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
1652 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
1653 rx, sizeof(*rx) +
1654 (sizeof(struct htt_rx_indication_mpdu_range) *
1655 num_mpdu_ranges));
1657 for (i = 0; i < num_mpdu_ranges; i++)
1658 mpdu_count += mpdu_ranges[i].mpdu_count;
1660 while (mpdu_count--) {
1661 __skb_queue_head_init(&amsdu);
1662 ret = ath10k_htt_rx_amsdu_pop(htt, &fw_desc,
1663 &fw_desc_len, &amsdu);
1664 if (ret < 0) {
1665 ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
1666 __skb_queue_purge(&amsdu);
1667 /* FIXME: It's probably a good idea to reboot the
1668 * device instead of leaving it inoperable.
1670 htt->rx_confused = true;
1671 break;
1674 ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
1675 ath10k_htt_rx_h_unchain(ar, &amsdu, ret > 0);
1676 ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
1677 ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status, true);
1678 ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
1681 tasklet_schedule(&htt->rx_replenish_task);
1684 static void ath10k_htt_rx_frag_handler(struct ath10k_htt *htt,
1685 struct htt_rx_fragment_indication *frag)
1687 struct ath10k *ar = htt->ar;
1688 struct ieee80211_rx_status *rx_status = &htt->rx_status;
1689 struct sk_buff_head amsdu;
1690 int ret;
1691 u8 *fw_desc;
1692 int fw_desc_len;
1694 fw_desc_len = __le16_to_cpu(frag->fw_rx_desc_bytes);
1695 fw_desc = (u8 *)frag->fw_msdu_rx_desc;
1697 __skb_queue_head_init(&amsdu);
1699 spin_lock_bh(&htt->rx_ring.lock);
1700 ret = ath10k_htt_rx_amsdu_pop(htt, &fw_desc, &fw_desc_len,
1701 &amsdu);
1702 spin_unlock_bh(&htt->rx_ring.lock);
1704 tasklet_schedule(&htt->rx_replenish_task);
1706 ath10k_dbg(ar, ATH10K_DBG_HTT_DUMP, "htt rx frag ahead\n");
1708 if (ret) {
1709 ath10k_warn(ar, "failed to pop amsdu from httr rx ring for fragmented rx %d\n",
1710 ret);
1711 __skb_queue_purge(&amsdu);
1712 return;
1715 if (skb_queue_len(&amsdu) != 1) {
1716 ath10k_warn(ar, "failed to pop frag amsdu: too many msdus\n");
1717 __skb_queue_purge(&amsdu);
1718 return;
1721 ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
1722 ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
1723 ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status, true);
1724 ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
1726 if (fw_desc_len > 0) {
1727 ath10k_dbg(ar, ATH10K_DBG_HTT,
1728 "expecting more fragmented rx in one indication %d\n",
1729 fw_desc_len);
1733 static void ath10k_htt_rx_frm_tx_compl(struct ath10k *ar,
1734 struct sk_buff *skb)
1736 struct ath10k_htt *htt = &ar->htt;
1737 struct htt_resp *resp = (struct htt_resp *)skb->data;
1738 struct htt_tx_done tx_done = {};
1739 int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
1740 __le16 msdu_id;
1741 int i;
1743 switch (status) {
1744 case HTT_DATA_TX_STATUS_NO_ACK:
1745 tx_done.no_ack = true;
1746 break;
1747 case HTT_DATA_TX_STATUS_OK:
1748 tx_done.success = true;
1749 break;
1750 case HTT_DATA_TX_STATUS_DISCARD:
1751 case HTT_DATA_TX_STATUS_POSTPONE:
1752 case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
1753 tx_done.discard = true;
1754 break;
1755 default:
1756 ath10k_warn(ar, "unhandled tx completion status %d\n", status);
1757 tx_done.discard = true;
1758 break;
1761 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
1762 resp->data_tx_completion.num_msdus);
1764 for (i = 0; i < resp->data_tx_completion.num_msdus; i++) {
1765 msdu_id = resp->data_tx_completion.msdus[i];
1766 tx_done.msdu_id = __le16_to_cpu(msdu_id);
1767 ath10k_txrx_tx_unref(htt, &tx_done);
1771 static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
1773 struct htt_rx_addba *ev = &resp->rx_addba;
1774 struct ath10k_peer *peer;
1775 struct ath10k_vif *arvif;
1776 u16 info0, tid, peer_id;
1778 info0 = __le16_to_cpu(ev->info0);
1779 tid = MS(info0, HTT_RX_BA_INFO0_TID);
1780 peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1782 ath10k_dbg(ar, ATH10K_DBG_HTT,
1783 "htt rx addba tid %hu peer_id %hu size %hhu\n",
1784 tid, peer_id, ev->window_size);
1786 spin_lock_bh(&ar->data_lock);
1787 peer = ath10k_peer_find_by_id(ar, peer_id);
1788 if (!peer) {
1789 ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
1790 peer_id);
1791 spin_unlock_bh(&ar->data_lock);
1792 return;
1795 arvif = ath10k_get_arvif(ar, peer->vdev_id);
1796 if (!arvif) {
1797 ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
1798 peer->vdev_id);
1799 spin_unlock_bh(&ar->data_lock);
1800 return;
1803 ath10k_dbg(ar, ATH10K_DBG_HTT,
1804 "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
1805 peer->addr, tid, ev->window_size);
1807 ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1808 spin_unlock_bh(&ar->data_lock);
1811 static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
1813 struct htt_rx_delba *ev = &resp->rx_delba;
1814 struct ath10k_peer *peer;
1815 struct ath10k_vif *arvif;
1816 u16 info0, tid, peer_id;
1818 info0 = __le16_to_cpu(ev->info0);
1819 tid = MS(info0, HTT_RX_BA_INFO0_TID);
1820 peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1822 ath10k_dbg(ar, ATH10K_DBG_HTT,
1823 "htt rx delba tid %hu peer_id %hu\n",
1824 tid, peer_id);
1826 spin_lock_bh(&ar->data_lock);
1827 peer = ath10k_peer_find_by_id(ar, peer_id);
1828 if (!peer) {
1829 ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
1830 peer_id);
1831 spin_unlock_bh(&ar->data_lock);
1832 return;
1835 arvif = ath10k_get_arvif(ar, peer->vdev_id);
1836 if (!arvif) {
1837 ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
1838 peer->vdev_id);
1839 spin_unlock_bh(&ar->data_lock);
1840 return;
1843 ath10k_dbg(ar, ATH10K_DBG_HTT,
1844 "htt rx stop rx ba session sta %pM tid %hu\n",
1845 peer->addr, tid);
1847 ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1848 spin_unlock_bh(&ar->data_lock);
1851 static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
1852 struct sk_buff_head *amsdu)
1854 struct sk_buff *msdu;
1855 struct htt_rx_desc *rxd;
1857 if (skb_queue_empty(list))
1858 return -ENOBUFS;
1860 if (WARN_ON(!skb_queue_empty(amsdu)))
1861 return -EINVAL;
1863 while ((msdu = __skb_dequeue(list))) {
1864 __skb_queue_tail(amsdu, msdu);
1866 rxd = (void *)msdu->data - sizeof(*rxd);
1867 if (rxd->msdu_end.common.info0 &
1868 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
1869 break;
1872 msdu = skb_peek_tail(amsdu);
1873 rxd = (void *)msdu->data - sizeof(*rxd);
1874 if (!(rxd->msdu_end.common.info0 &
1875 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
1876 skb_queue_splice_init(amsdu, list);
1877 return -EAGAIN;
1880 return 0;
1883 static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
1884 struct sk_buff *skb)
1886 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1888 if (!ieee80211_has_protected(hdr->frame_control))
1889 return;
1891 /* Offloaded frames are already decrypted but firmware insists they are
1892 * protected in the 802.11 header. Strip the flag. Otherwise mac80211
1893 * will drop the frame.
1896 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1897 status->flag |= RX_FLAG_DECRYPTED |
1898 RX_FLAG_IV_STRIPPED |
1899 RX_FLAG_MMIC_STRIPPED;
1902 static void ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
1903 struct sk_buff_head *list)
1905 struct ath10k_htt *htt = &ar->htt;
1906 struct ieee80211_rx_status *status = &htt->rx_status;
1907 struct htt_rx_offload_msdu *rx;
1908 struct sk_buff *msdu;
1909 size_t offset;
1911 while ((msdu = __skb_dequeue(list))) {
1912 /* Offloaded frames don't have Rx descriptor. Instead they have
1913 * a short meta information header.
1916 rx = (void *)msdu->data;
1918 skb_put(msdu, sizeof(*rx));
1919 skb_pull(msdu, sizeof(*rx));
1921 if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
1922 ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
1923 dev_kfree_skb_any(msdu);
1924 continue;
1927 skb_put(msdu, __le16_to_cpu(rx->msdu_len));
1929 /* Offloaded rx header length isn't multiple of 2 nor 4 so the
1930 * actual payload is unaligned. Align the frame. Otherwise
1931 * mac80211 complains. This shouldn't reduce performance much
1932 * because these offloaded frames are rare.
1934 offset = 4 - ((unsigned long)msdu->data & 3);
1935 skb_put(msdu, offset);
1936 memmove(msdu->data + offset, msdu->data, msdu->len);
1937 skb_pull(msdu, offset);
1939 /* FIXME: The frame is NWifi. Re-construct QoS Control
1940 * if possible later.
1943 memset(status, 0, sizeof(*status));
1944 status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1946 ath10k_htt_rx_h_rx_offload_prot(status, msdu);
1947 ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
1948 ath10k_process_rx(ar, status, msdu);
1952 static void ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
1954 struct ath10k_htt *htt = &ar->htt;
1955 struct htt_resp *resp = (void *)skb->data;
1956 struct ieee80211_rx_status *status = &htt->rx_status;
1957 struct sk_buff_head list;
1958 struct sk_buff_head amsdu;
1959 u16 peer_id;
1960 u16 msdu_count;
1961 u8 vdev_id;
1962 u8 tid;
1963 bool offload;
1964 bool frag;
1965 int ret;
1967 lockdep_assert_held(&htt->rx_ring.lock);
1969 if (htt->rx_confused)
1970 return;
1972 skb_pull(skb, sizeof(resp->hdr));
1973 skb_pull(skb, sizeof(resp->rx_in_ord_ind));
1975 peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
1976 msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
1977 vdev_id = resp->rx_in_ord_ind.vdev_id;
1978 tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
1979 offload = !!(resp->rx_in_ord_ind.info &
1980 HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
1981 frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);
1983 ath10k_dbg(ar, ATH10K_DBG_HTT,
1984 "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
1985 vdev_id, peer_id, tid, offload, frag, msdu_count);
1987 if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs)) {
1988 ath10k_warn(ar, "dropping invalid in order rx indication\n");
1989 return;
1992 /* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
1993 * extracted and processed.
1995 __skb_queue_head_init(&list);
1996 ret = ath10k_htt_rx_pop_paddr_list(htt, &resp->rx_in_ord_ind, &list);
1997 if (ret < 0) {
1998 ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
1999 htt->rx_confused = true;
2000 return;
2003 /* Offloaded frames are very different and need to be handled
2004 * separately.
2006 if (offload)
2007 ath10k_htt_rx_h_rx_offload(ar, &list);
2009 while (!skb_queue_empty(&list)) {
2010 __skb_queue_head_init(&amsdu);
2011 ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
2012 switch (ret) {
2013 case 0:
2014 /* Note: The in-order indication may report interleaved
2015 * frames from different PPDUs meaning reported rx rate
2016 * to mac80211 isn't accurate/reliable. It's still
2017 * better to report something than nothing though. This
2018 * should still give an idea about rx rate to the user.
2020 ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
2021 ath10k_htt_rx_h_filter(ar, &amsdu, status);
2022 ath10k_htt_rx_h_mpdu(ar, &amsdu, status, false);
2023 ath10k_htt_rx_h_deliver(ar, &amsdu, status);
2024 break;
2025 case -EAGAIN:
2026 /* fall through */
2027 default:
2028 /* Should not happen. */
2029 ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
2030 htt->rx_confused = true;
2031 __skb_queue_purge(&list);
2032 return;
2036 tasklet_schedule(&htt->rx_replenish_task);
2039 void ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
2041 struct ath10k_htt *htt = &ar->htt;
2042 struct htt_resp *resp = (struct htt_resp *)skb->data;
2043 enum htt_t2h_msg_type type;
2045 /* confirm alignment */
2046 if (!IS_ALIGNED((unsigned long)skb->data, 4))
2047 ath10k_warn(ar, "unaligned htt message, expect trouble\n");
2049 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
2050 resp->hdr.msg_type);
2052 if (resp->hdr.msg_type >= ar->htt.t2h_msg_types_max) {
2053 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, unsupported msg_type: 0x%0X\n max: 0x%0X",
2054 resp->hdr.msg_type, ar->htt.t2h_msg_types_max);
2055 dev_kfree_skb_any(skb);
2056 return;
2058 type = ar->htt.t2h_msg_types[resp->hdr.msg_type];
2060 switch (type) {
2061 case HTT_T2H_MSG_TYPE_VERSION_CONF: {
2062 htt->target_version_major = resp->ver_resp.major;
2063 htt->target_version_minor = resp->ver_resp.minor;
2064 complete(&htt->target_version_received);
2065 break;
2067 case HTT_T2H_MSG_TYPE_RX_IND:
2068 spin_lock_bh(&htt->rx_ring.lock);
2069 __skb_queue_tail(&htt->rx_compl_q, skb);
2070 spin_unlock_bh(&htt->rx_ring.lock);
2071 tasklet_schedule(&htt->txrx_compl_task);
2072 return;
2073 case HTT_T2H_MSG_TYPE_PEER_MAP: {
2074 struct htt_peer_map_event ev = {
2075 .vdev_id = resp->peer_map.vdev_id,
2076 .peer_id = __le16_to_cpu(resp->peer_map.peer_id),
2078 memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
2079 ath10k_peer_map_event(htt, &ev);
2080 break;
2082 case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
2083 struct htt_peer_unmap_event ev = {
2084 .peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
2086 ath10k_peer_unmap_event(htt, &ev);
2087 break;
2089 case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
2090 struct htt_tx_done tx_done = {};
2091 int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
2093 tx_done.msdu_id =
2094 __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
2096 switch (status) {
2097 case HTT_MGMT_TX_STATUS_OK:
2098 tx_done.success = true;
2099 break;
2100 case HTT_MGMT_TX_STATUS_RETRY:
2101 tx_done.no_ack = true;
2102 break;
2103 case HTT_MGMT_TX_STATUS_DROP:
2104 tx_done.discard = true;
2105 break;
2108 ath10k_txrx_tx_unref(htt, &tx_done);
2109 break;
2111 case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
2112 skb_queue_tail(&htt->tx_compl_q, skb);
2113 tasklet_schedule(&htt->txrx_compl_task);
2114 return;
2115 case HTT_T2H_MSG_TYPE_SEC_IND: {
2116 struct ath10k *ar = htt->ar;
2117 struct htt_security_indication *ev = &resp->security_indication;
2119 ath10k_dbg(ar, ATH10K_DBG_HTT,
2120 "sec ind peer_id %d unicast %d type %d\n",
2121 __le16_to_cpu(ev->peer_id),
2122 !!(ev->flags & HTT_SECURITY_IS_UNICAST),
2123 MS(ev->flags, HTT_SECURITY_TYPE));
2124 complete(&ar->install_key_done);
2125 break;
2127 case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
2128 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
2129 skb->data, skb->len);
2130 ath10k_htt_rx_frag_handler(htt, &resp->rx_frag_ind);
2131 break;
2133 case HTT_T2H_MSG_TYPE_TEST:
2134 break;
2135 case HTT_T2H_MSG_TYPE_STATS_CONF:
2136 trace_ath10k_htt_stats(ar, skb->data, skb->len);
2137 break;
2138 case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
2139 /* Firmware can return tx frames if it's unable to fully
2140 * process them and suspects host may be able to fix it. ath10k
2141 * sends all tx frames as already inspected so this shouldn't
2142 * happen unless fw has a bug.
2144 ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
2145 break;
2146 case HTT_T2H_MSG_TYPE_RX_ADDBA:
2147 ath10k_htt_rx_addba(ar, resp);
2148 break;
2149 case HTT_T2H_MSG_TYPE_RX_DELBA:
2150 ath10k_htt_rx_delba(ar, resp);
2151 break;
2152 case HTT_T2H_MSG_TYPE_PKTLOG: {
2153 struct ath10k_pktlog_hdr *hdr =
2154 (struct ath10k_pktlog_hdr *)resp->pktlog_msg.payload;
2156 trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
2157 sizeof(*hdr) +
2158 __le16_to_cpu(hdr->size));
2159 break;
2161 case HTT_T2H_MSG_TYPE_RX_FLUSH: {
2162 /* Ignore this event because mac80211 takes care of Rx
2163 * aggregation reordering.
2165 break;
2167 case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
2168 spin_lock_bh(&htt->rx_ring.lock);
2169 __skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
2170 spin_unlock_bh(&htt->rx_ring.lock);
2171 tasklet_schedule(&htt->txrx_compl_task);
2172 return;
2174 case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND:
2175 break;
2176 case HTT_T2H_MSG_TYPE_CHAN_CHANGE:
2177 break;
2178 case HTT_T2H_MSG_TYPE_AGGR_CONF:
2179 break;
2180 case HTT_T2H_MSG_TYPE_EN_STATS:
2181 case HTT_T2H_MSG_TYPE_TX_FETCH_IND:
2182 case HTT_T2H_MSG_TYPE_TX_FETCH_CONF:
2183 case HTT_T2H_MSG_TYPE_TX_LOW_LATENCY_IND:
2184 default:
2185 ath10k_warn(ar, "htt event (%d) not handled\n",
2186 resp->hdr.msg_type);
2187 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
2188 skb->data, skb->len);
2189 break;
2192 /* Free the indication buffer */
2193 dev_kfree_skb_any(skb);
2195 EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
2197 static void ath10k_htt_txrx_compl_task(unsigned long ptr)
2199 struct ath10k_htt *htt = (struct ath10k_htt *)ptr;
2200 struct ath10k *ar = htt->ar;
2201 struct htt_resp *resp;
2202 struct sk_buff *skb;
2204 while ((skb = skb_dequeue(&htt->tx_compl_q))) {
2205 ath10k_htt_rx_frm_tx_compl(htt->ar, skb);
2206 dev_kfree_skb_any(skb);
2209 spin_lock_bh(&htt->rx_ring.lock);
2210 while ((skb = __skb_dequeue(&htt->rx_compl_q))) {
2211 resp = (struct htt_resp *)skb->data;
2212 ath10k_htt_rx_handler(htt, &resp->rx_ind);
2213 dev_kfree_skb_any(skb);
2216 while ((skb = __skb_dequeue(&htt->rx_in_ord_compl_q))) {
2217 ath10k_htt_rx_in_ord_ind(ar, skb);
2218 dev_kfree_skb_any(skb);
2220 spin_unlock_bh(&htt->rx_ring.lock);