a7d29c8718c3e09f7160c84b09147efb3d8afa86
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / htt_rx.c
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 /* slightly larger than one large A-MPDU */
29 #define HTT_RX_RING_SIZE_MIN 128
30
31 /* roughly 20 ms @ 1 Gbps of 1500B MSDUs */
32 #define HTT_RX_RING_SIZE_MAX 2048
33
34 #define HTT_RX_AVG_FRM_BYTES 1000
35
36 /* ms, very conservative */
37 #define HTT_RX_HOST_LATENCY_MAX_MS 20
38
39 /* ms, conservative */
40 #define HTT_RX_HOST_LATENCY_WORST_LIKELY_MS 10
41
42 /* when under memory pressure rx ring refill may fail and needs a retry */
43 #define HTT_RX_RING_REFILL_RETRY_MS 50
44
45 static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);
46 static void ath10k_htt_txrx_compl_task(unsigned long ptr);
47
48 static int ath10k_htt_rx_ring_size(struct ath10k_htt *htt)
49 {
50         int size;
51
52         /*
53          * It is expected that the host CPU will typically be able to
54          * service the rx indication from one A-MPDU before the rx
55          * indication from the subsequent A-MPDU happens, roughly 1-2 ms
56          * later. However, the rx ring should be sized very conservatively,
57          * to accomodate the worst reasonable delay before the host CPU
58          * services a rx indication interrupt.
59          *
60          * The rx ring need not be kept full of empty buffers. In theory,
61          * the htt host SW can dynamically track the low-water mark in the
62          * rx ring, and dynamically adjust the level to which the rx ring
63          * is filled with empty buffers, to dynamically meet the desired
64          * low-water mark.
65          *
66          * In contrast, it's difficult to resize the rx ring itself, once
67          * it's in use. Thus, the ring itself should be sized very
68          * conservatively, while the degree to which the ring is filled
69          * with empty buffers should be sized moderately conservatively.
70          */
71
72         /* 1e6 bps/mbps / 1e3 ms per sec = 1000 */
73         size =
74             htt->max_throughput_mbps +
75             1000  /
76             (8 * HTT_RX_AVG_FRM_BYTES) * HTT_RX_HOST_LATENCY_MAX_MS;
77
78         if (size < HTT_RX_RING_SIZE_MIN)
79                 size = HTT_RX_RING_SIZE_MIN;
80
81         if (size > HTT_RX_RING_SIZE_MAX)
82                 size = HTT_RX_RING_SIZE_MAX;
83
84         size = roundup_pow_of_two(size);
85
86         return size;
87 }
88
89 static int ath10k_htt_rx_ring_fill_level(struct ath10k_htt *htt)
90 {
91         int size;
92
93         /* 1e6 bps/mbps / 1e3 ms per sec = 1000 */
94         size =
95             htt->max_throughput_mbps *
96             1000  /
97             (8 * HTT_RX_AVG_FRM_BYTES) * HTT_RX_HOST_LATENCY_WORST_LIKELY_MS;
98
99         /*
100          * Make sure the fill level is at least 1 less than the ring size.
101          * Leaving 1 element empty allows the SW to easily distinguish
102          * between a full ring vs. an empty ring.
103          */
104         if (size >= htt->rx_ring.size)
105                 size = htt->rx_ring.size - 1;
106
107         return size;
108 }
109
110 static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
111 {
112         struct sk_buff *skb;
113         struct ath10k_skb_cb *cb;
114         int i;
115
116         for (i = 0; i < htt->rx_ring.fill_cnt; i++) {
117                 skb = htt->rx_ring.netbufs_ring[i];
118                 cb = ATH10K_SKB_CB(skb);
119                 dma_unmap_single(htt->ar->dev, cb->paddr,
120                                  skb->len + skb_tailroom(skb),
121                                  DMA_FROM_DEVICE);
122                 dev_kfree_skb_any(skb);
123         }
124
125         htt->rx_ring.fill_cnt = 0;
126 }
127
128 static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
129 {
130         struct htt_rx_desc *rx_desc;
131         struct sk_buff *skb;
132         dma_addr_t paddr;
133         int ret = 0, idx;
134
135         idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
136         while (num > 0) {
137                 skb = dev_alloc_skb(HTT_RX_BUF_SIZE + HTT_RX_DESC_ALIGN);
138                 if (!skb) {
139                         ret = -ENOMEM;
140                         goto fail;
141                 }
142
143                 if (!IS_ALIGNED((unsigned long)skb->data, HTT_RX_DESC_ALIGN))
144                         skb_pull(skb,
145                                  PTR_ALIGN(skb->data, HTT_RX_DESC_ALIGN) -
146                                  skb->data);
147
148                 /* Clear rx_desc attention word before posting to Rx ring */
149                 rx_desc = (struct htt_rx_desc *)skb->data;
150                 rx_desc->attention.flags = __cpu_to_le32(0);
151
152                 paddr = dma_map_single(htt->ar->dev, skb->data,
153                                        skb->len + skb_tailroom(skb),
154                                        DMA_FROM_DEVICE);
155
156                 if (unlikely(dma_mapping_error(htt->ar->dev, paddr))) {
157                         dev_kfree_skb_any(skb);
158                         ret = -ENOMEM;
159                         goto fail;
160                 }
161
162                 ATH10K_SKB_CB(skb)->paddr = paddr;
163                 htt->rx_ring.netbufs_ring[idx] = skb;
164                 htt->rx_ring.paddrs_ring[idx] = __cpu_to_le32(paddr);
165                 htt->rx_ring.fill_cnt++;
166
167                 num--;
168                 idx++;
169                 idx &= htt->rx_ring.size_mask;
170         }
171
172 fail:
173         *htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
174         return ret;
175 }
176
177 static int ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
178 {
179         lockdep_assert_held(&htt->rx_ring.lock);
180         return __ath10k_htt_rx_ring_fill_n(htt, num);
181 }
182
183 static void ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt *htt)
184 {
185         int ret, num_deficit, num_to_fill;
186
187         /* Refilling the whole RX ring buffer proves to be a bad idea. The
188          * reason is RX may take up significant amount of CPU cycles and starve
189          * other tasks, e.g. TX on an ethernet device while acting as a bridge
190          * with ath10k wlan interface. This ended up with very poor performance
191          * once CPU the host system was overwhelmed with RX on ath10k.
192          *
193          * By limiting the number of refills the replenishing occurs
194          * progressively. This in turns makes use of the fact tasklets are
195          * processed in FIFO order. This means actual RX processing can starve
196          * out refilling. If there's not enough buffers on RX ring FW will not
197          * report RX until it is refilled with enough buffers. This
198          * automatically balances load wrt to CPU power.
199          *
200          * This probably comes at a cost of lower maximum throughput but
201          * improves the avarage and stability. */
202         spin_lock_bh(&htt->rx_ring.lock);
203         num_deficit = htt->rx_ring.fill_level - htt->rx_ring.fill_cnt;
204         num_to_fill = min(ATH10K_HTT_MAX_NUM_REFILL, num_deficit);
205         num_deficit -= num_to_fill;
206         ret = ath10k_htt_rx_ring_fill_n(htt, num_to_fill);
207         if (ret == -ENOMEM) {
208                 /*
209                  * Failed to fill it to the desired level -
210                  * we'll start a timer and try again next time.
211                  * As long as enough buffers are left in the ring for
212                  * another A-MPDU rx, no special recovery is needed.
213                  */
214                 mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
215                           msecs_to_jiffies(HTT_RX_RING_REFILL_RETRY_MS));
216         } else if (num_deficit > 0) {
217                 tasklet_schedule(&htt->rx_replenish_task);
218         }
219         spin_unlock_bh(&htt->rx_ring.lock);
220 }
221
222 static void ath10k_htt_rx_ring_refill_retry(unsigned long arg)
223 {
224         struct ath10k_htt *htt = (struct ath10k_htt *)arg;
225
226         ath10k_htt_rx_msdu_buff_replenish(htt);
227 }
228
229 static void ath10k_htt_rx_ring_clean_up(struct ath10k_htt *htt)
230 {
231         struct sk_buff *skb;
232         int i;
233
234         for (i = 0; i < htt->rx_ring.size; i++) {
235                 skb = htt->rx_ring.netbufs_ring[i];
236                 if (!skb)
237                         continue;
238
239                 dma_unmap_single(htt->ar->dev, ATH10K_SKB_CB(skb)->paddr,
240                                  skb->len + skb_tailroom(skb),
241                                  DMA_FROM_DEVICE);
242                 dev_kfree_skb_any(skb);
243                 htt->rx_ring.netbufs_ring[i] = NULL;
244         }
245 }
246
247 void ath10k_htt_rx_free(struct ath10k_htt *htt)
248 {
249         del_timer_sync(&htt->rx_ring.refill_retry_timer);
250         tasklet_kill(&htt->rx_replenish_task);
251         tasklet_kill(&htt->txrx_compl_task);
252
253         skb_queue_purge(&htt->tx_compl_q);
254         skb_queue_purge(&htt->rx_compl_q);
255
256         ath10k_htt_rx_ring_clean_up(htt);
257
258         dma_free_coherent(htt->ar->dev,
259                           (htt->rx_ring.size *
260                            sizeof(htt->rx_ring.paddrs_ring)),
261                           htt->rx_ring.paddrs_ring,
262                           htt->rx_ring.base_paddr);
263
264         dma_free_coherent(htt->ar->dev,
265                           sizeof(*htt->rx_ring.alloc_idx.vaddr),
266                           htt->rx_ring.alloc_idx.vaddr,
267                           htt->rx_ring.alloc_idx.paddr);
268
269         kfree(htt->rx_ring.netbufs_ring);
270 }
271
272 static inline struct sk_buff *ath10k_htt_rx_netbuf_pop(struct ath10k_htt *htt)
273 {
274         struct ath10k *ar = htt->ar;
275         int idx;
276         struct sk_buff *msdu;
277
278         lockdep_assert_held(&htt->rx_ring.lock);
279
280         if (htt->rx_ring.fill_cnt == 0) {
281                 ath10k_warn(ar, "tried to pop sk_buff from an empty rx ring\n");
282                 return NULL;
283         }
284
285         idx = htt->rx_ring.sw_rd_idx.msdu_payld;
286         msdu = htt->rx_ring.netbufs_ring[idx];
287         htt->rx_ring.netbufs_ring[idx] = NULL;
288
289         idx++;
290         idx &= htt->rx_ring.size_mask;
291         htt->rx_ring.sw_rd_idx.msdu_payld = idx;
292         htt->rx_ring.fill_cnt--;
293
294         return msdu;
295 }
296
297 static void ath10k_htt_rx_free_msdu_chain(struct sk_buff *skb)
298 {
299         struct sk_buff *next;
300
301         while (skb) {
302                 next = skb->next;
303                 dev_kfree_skb_any(skb);
304                 skb = next;
305         }
306 }
307
308 /* return: < 0 fatal error, 0 - non chained msdu, 1 chained msdu */
309 static int ath10k_htt_rx_amsdu_pop(struct ath10k_htt *htt,
310                                    u8 **fw_desc, int *fw_desc_len,
311                                    struct sk_buff **head_msdu,
312                                    struct sk_buff **tail_msdu,
313                                    u32 *attention)
314 {
315         struct ath10k *ar = htt->ar;
316         int msdu_len, msdu_chaining = 0;
317         struct sk_buff *msdu, *next;
318         struct htt_rx_desc *rx_desc;
319         u32 tsf;
320
321         lockdep_assert_held(&htt->rx_ring.lock);
322
323         if (htt->rx_confused) {
324                 ath10k_warn(ar, "htt is confused. refusing rx\n");
325                 return -1;
326         }
327
328         msdu = *head_msdu = ath10k_htt_rx_netbuf_pop(htt);
329         while (msdu) {
330                 int last_msdu, msdu_len_invalid, msdu_chained;
331
332                 dma_unmap_single(htt->ar->dev,
333                                  ATH10K_SKB_CB(msdu)->paddr,
334                                  msdu->len + skb_tailroom(msdu),
335                                  DMA_FROM_DEVICE);
336
337                 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx pop: ",
338                                 msdu->data, msdu->len + skb_tailroom(msdu));
339                 trace_ath10k_htt_rx_pop_msdu(ar, msdu->data, msdu->len +
340                                              skb_tailroom(msdu));
341
342                 rx_desc = (struct htt_rx_desc *)msdu->data;
343
344                 /* FIXME: we must report msdu payload since this is what caller
345                  *        expects now */
346                 skb_put(msdu, offsetof(struct htt_rx_desc, msdu_payload));
347                 skb_pull(msdu, offsetof(struct htt_rx_desc, msdu_payload));
348
349                 /*
350                  * Sanity check - confirm the HW is finished filling in the
351                  * rx data.
352                  * If the HW and SW are working correctly, then it's guaranteed
353                  * that the HW's MAC DMA is done before this point in the SW.
354                  * To prevent the case that we handle a stale Rx descriptor,
355                  * just assert for now until we have a way to recover.
356                  */
357                 if (!(__le32_to_cpu(rx_desc->attention.flags)
358                                 & RX_ATTENTION_FLAGS_MSDU_DONE)) {
359                         ath10k_htt_rx_free_msdu_chain(*head_msdu);
360                         *head_msdu = NULL;
361                         msdu = NULL;
362                         ath10k_err(ar, "htt rx stopped. cannot recover\n");
363                         htt->rx_confused = true;
364                         break;
365                 }
366
367                 *attention |= __le32_to_cpu(rx_desc->attention.flags) &
368                                             (RX_ATTENTION_FLAGS_TKIP_MIC_ERR |
369                                              RX_ATTENTION_FLAGS_DECRYPT_ERR |
370                                              RX_ATTENTION_FLAGS_FCS_ERR |
371                                              RX_ATTENTION_FLAGS_MGMT_TYPE);
372                 /*
373                  * Copy the FW rx descriptor for this MSDU from the rx
374                  * indication message into the MSDU's netbuf. HL uses the
375                  * same rx indication message definition as LL, and simply
376                  * appends new info (fields from the HW rx desc, and the
377                  * MSDU payload itself). So, the offset into the rx
378                  * indication message only has to account for the standard
379                  * offset of the per-MSDU FW rx desc info within the
380                  * message, and how many bytes of the per-MSDU FW rx desc
381                  * info have already been consumed. (And the endianness of
382                  * the host, since for a big-endian host, the rx ind
383                  * message contents, including the per-MSDU rx desc bytes,
384                  * were byteswapped during upload.)
385                  */
386                 if (*fw_desc_len > 0) {
387                         rx_desc->fw_desc.info0 = **fw_desc;
388                         /*
389                          * The target is expected to only provide the basic
390                          * per-MSDU rx descriptors. Just to be sure, verify
391                          * that the target has not attached extension data
392                          * (e.g. LRO flow ID).
393                          */
394
395                         /* or more, if there's extension data */
396                         (*fw_desc)++;
397                         (*fw_desc_len)--;
398                 } else {
399                         /*
400                          * When an oversized AMSDU happened, FW will lost
401                          * some of MSDU status - in this case, the FW
402                          * descriptors provided will be less than the
403                          * actual MSDUs inside this MPDU. Mark the FW
404                          * descriptors so that it will still deliver to
405                          * upper stack, if no CRC error for this MPDU.
406                          *
407                          * FIX THIS - the FW descriptors are actually for
408                          * MSDUs in the end of this A-MSDU instead of the
409                          * beginning.
410                          */
411                         rx_desc->fw_desc.info0 = 0;
412                 }
413
414                 msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
415                                         & (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
416                                            RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
417                 msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.info0),
418                               RX_MSDU_START_INFO0_MSDU_LENGTH);
419                 msdu_chained = rx_desc->frag_info.ring2_more_count;
420
421                 if (msdu_len_invalid)
422                         msdu_len = 0;
423
424                 skb_trim(msdu, 0);
425                 skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
426                 msdu_len -= msdu->len;
427
428                 /* FIXME: Do chained buffers include htt_rx_desc or not? */
429                 while (msdu_chained--) {
430                         struct sk_buff *next = ath10k_htt_rx_netbuf_pop(htt);
431
432                         dma_unmap_single(htt->ar->dev,
433                                          ATH10K_SKB_CB(next)->paddr,
434                                          next->len + skb_tailroom(next),
435                                          DMA_FROM_DEVICE);
436
437                         ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL,
438                                         "htt rx chained: ", next->data,
439                                         next->len + skb_tailroom(next));
440                         trace_ath10k_htt_rx_pop_msdu(ar, msdu->data, msdu->len +
441                                                      skb_tailroom(msdu));
442
443                         skb_trim(next, 0);
444                         skb_put(next, min(msdu_len, HTT_RX_BUF_SIZE));
445                         msdu_len -= next->len;
446
447                         msdu->next = next;
448                         msdu = next;
449                         msdu_chaining = 1;
450                 }
451
452                 last_msdu = __le32_to_cpu(rx_desc->msdu_end.info0) &
453                                 RX_MSDU_END_INFO0_LAST_MSDU;
454
455                 tsf = __le32_to_cpu(rx_desc->ppdu_end.tsf_timestamp);
456                 trace_ath10k_htt_rx_desc(ar, tsf, &rx_desc->attention,
457                                          sizeof(*rx_desc) - sizeof(u32));
458                 if (last_msdu) {
459                         msdu->next = NULL;
460                         break;
461                 }
462
463                 next = ath10k_htt_rx_netbuf_pop(htt);
464                 msdu->next = next;
465                 msdu = next;
466         }
467         *tail_msdu = msdu;
468
469         if (*head_msdu == NULL)
470                 msdu_chaining = -1;
471
472         /*
473          * Don't refill the ring yet.
474          *
475          * First, the elements popped here are still in use - it is not
476          * safe to overwrite them until the matching call to
477          * mpdu_desc_list_next. Second, for efficiency it is preferable to
478          * refill the rx ring with 1 PPDU's worth of rx buffers (something
479          * like 32 x 3 buffers), rather than one MPDU's worth of rx buffers
480          * (something like 3 buffers). Consequently, we'll rely on the txrx
481          * SW to tell us when it is done pulling all the PPDU's rx buffers
482          * out of the rx ring, and then refill it just once.
483          */
484
485         return msdu_chaining;
486 }
487
488 static void ath10k_htt_rx_replenish_task(unsigned long ptr)
489 {
490         struct ath10k_htt *htt = (struct ath10k_htt *)ptr;
491
492         ath10k_htt_rx_msdu_buff_replenish(htt);
493 }
494
495 int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
496 {
497         struct ath10k *ar = htt->ar;
498         dma_addr_t paddr;
499         void *vaddr;
500         size_t size;
501         struct timer_list *timer = &htt->rx_ring.refill_retry_timer;
502
503         htt->rx_ring.size = ath10k_htt_rx_ring_size(htt);
504         if (!is_power_of_2(htt->rx_ring.size)) {
505                 ath10k_warn(ar, "htt rx ring size is not power of 2\n");
506                 return -EINVAL;
507         }
508
509         htt->rx_ring.size_mask = htt->rx_ring.size - 1;
510
511         /*
512          * Set the initial value for the level to which the rx ring
513          * should be filled, based on the max throughput and the
514          * worst likely latency for the host to fill the rx ring
515          * with new buffers. In theory, this fill level can be
516          * dynamically adjusted from the initial value set here, to
517          * reflect the actual host latency rather than a
518          * conservative assumption about the host latency.
519          */
520         htt->rx_ring.fill_level = ath10k_htt_rx_ring_fill_level(htt);
521
522         htt->rx_ring.netbufs_ring =
523                 kzalloc(htt->rx_ring.size * sizeof(struct sk_buff *),
524                         GFP_KERNEL);
525         if (!htt->rx_ring.netbufs_ring)
526                 goto err_netbuf;
527
528         size = htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring);
529
530         vaddr = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_DMA);
531         if (!vaddr)
532                 goto err_dma_ring;
533
534         htt->rx_ring.paddrs_ring = vaddr;
535         htt->rx_ring.base_paddr = paddr;
536
537         vaddr = dma_alloc_coherent(htt->ar->dev,
538                                    sizeof(*htt->rx_ring.alloc_idx.vaddr),
539                                    &paddr, GFP_DMA);
540         if (!vaddr)
541                 goto err_dma_idx;
542
543         htt->rx_ring.alloc_idx.vaddr = vaddr;
544         htt->rx_ring.alloc_idx.paddr = paddr;
545         htt->rx_ring.sw_rd_idx.msdu_payld = 0;
546         *htt->rx_ring.alloc_idx.vaddr = 0;
547
548         /* Initialize the Rx refill retry timer */
549         setup_timer(timer, ath10k_htt_rx_ring_refill_retry, (unsigned long)htt);
550
551         spin_lock_init(&htt->rx_ring.lock);
552
553         htt->rx_ring.fill_cnt = 0;
554         if (__ath10k_htt_rx_ring_fill_n(htt, htt->rx_ring.fill_level))
555                 goto err_fill_ring;
556
557         tasklet_init(&htt->rx_replenish_task, ath10k_htt_rx_replenish_task,
558                      (unsigned long)htt);
559
560         skb_queue_head_init(&htt->tx_compl_q);
561         skb_queue_head_init(&htt->rx_compl_q);
562
563         tasklet_init(&htt->txrx_compl_task, ath10k_htt_txrx_compl_task,
564                      (unsigned long)htt);
565
566         ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
567                    htt->rx_ring.size, htt->rx_ring.fill_level);
568         return 0;
569
570 err_fill_ring:
571         ath10k_htt_rx_ring_free(htt);
572         dma_free_coherent(htt->ar->dev,
573                           sizeof(*htt->rx_ring.alloc_idx.vaddr),
574                           htt->rx_ring.alloc_idx.vaddr,
575                           htt->rx_ring.alloc_idx.paddr);
576 err_dma_idx:
577         dma_free_coherent(htt->ar->dev,
578                           (htt->rx_ring.size *
579                            sizeof(htt->rx_ring.paddrs_ring)),
580                           htt->rx_ring.paddrs_ring,
581                           htt->rx_ring.base_paddr);
582 err_dma_ring:
583         kfree(htt->rx_ring.netbufs_ring);
584 err_netbuf:
585         return -ENOMEM;
586 }
587
588 static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
589                                           enum htt_rx_mpdu_encrypt_type type)
590 {
591         switch (type) {
592         case HTT_RX_MPDU_ENCRYPT_NONE:
593                 return 0;
594         case HTT_RX_MPDU_ENCRYPT_WEP40:
595         case HTT_RX_MPDU_ENCRYPT_WEP104:
596                 return IEEE80211_WEP_IV_LEN;
597         case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
598         case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
599                 return IEEE80211_TKIP_IV_LEN;
600         case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
601                 return IEEE80211_CCMP_HDR_LEN;
602         case HTT_RX_MPDU_ENCRYPT_WEP128:
603         case HTT_RX_MPDU_ENCRYPT_WAPI:
604                 break;
605         }
606
607         ath10k_warn(ar, "unsupported encryption type %d\n", type);
608         return 0;
609 }
610
611 #define MICHAEL_MIC_LEN 8
612
613 static int ath10k_htt_rx_crypto_tail_len(struct ath10k *ar,
614                                          enum htt_rx_mpdu_encrypt_type type)
615 {
616         switch (type) {
617         case HTT_RX_MPDU_ENCRYPT_NONE:
618                 return 0;
619         case HTT_RX_MPDU_ENCRYPT_WEP40:
620         case HTT_RX_MPDU_ENCRYPT_WEP104:
621                 return IEEE80211_WEP_ICV_LEN;
622         case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
623         case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
624                 return IEEE80211_TKIP_ICV_LEN;
625         case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
626                 return IEEE80211_CCMP_MIC_LEN;
627         case HTT_RX_MPDU_ENCRYPT_WEP128:
628         case HTT_RX_MPDU_ENCRYPT_WAPI:
629                 break;
630         }
631
632         ath10k_warn(ar, "unsupported encryption type %d\n", type);
633         return 0;
634 }
635
636 /* Applies for first msdu in chain, before altering it. */
637 static struct ieee80211_hdr *ath10k_htt_rx_skb_get_hdr(struct sk_buff *skb)
638 {
639         struct htt_rx_desc *rxd;
640         enum rx_msdu_decap_format fmt;
641
642         rxd = (void *)skb->data - sizeof(*rxd);
643         fmt = MS(__le32_to_cpu(rxd->msdu_start.info1),
644                  RX_MSDU_START_INFO1_DECAP_FORMAT);
645
646         if (fmt == RX_MSDU_DECAP_RAW)
647                 return (void *)skb->data;
648
649         return (void *)skb->data - RX_HTT_HDR_STATUS_LEN;
650 }
651
652 /* This function only applies for first msdu in an msdu chain */
653 static bool ath10k_htt_rx_hdr_is_amsdu(struct ieee80211_hdr *hdr)
654 {
655         u8 *qc;
656
657         if (ieee80211_is_data_qos(hdr->frame_control)) {
658                 qc = ieee80211_get_qos_ctl(hdr);
659                 if (qc[0] & 0x80)
660                         return true;
661         }
662         return false;
663 }
664
665 struct rfc1042_hdr {
666         u8 llc_dsap;
667         u8 llc_ssap;
668         u8 llc_ctrl;
669         u8 snap_oui[3];
670         __be16 snap_type;
671 } __packed;
672
673 struct amsdu_subframe_hdr {
674         u8 dst[ETH_ALEN];
675         u8 src[ETH_ALEN];
676         __be16 len;
677 } __packed;
678
679 static const u8 rx_legacy_rate_idx[] = {
680         3,      /* 0x00  - 11Mbps  */
681         2,      /* 0x01  - 5.5Mbps */
682         1,      /* 0x02  - 2Mbps   */
683         0,      /* 0x03  - 1Mbps   */
684         3,      /* 0x04  - 11Mbps  */
685         2,      /* 0x05  - 5.5Mbps */
686         1,      /* 0x06  - 2Mbps   */
687         0,      /* 0x07  - 1Mbps   */
688         10,     /* 0x08  - 48Mbps  */
689         8,      /* 0x09  - 24Mbps  */
690         6,      /* 0x0A  - 12Mbps  */
691         4,      /* 0x0B  - 6Mbps   */
692         11,     /* 0x0C  - 54Mbps  */
693         9,      /* 0x0D  - 36Mbps  */
694         7,      /* 0x0E  - 18Mbps  */
695         5,      /* 0x0F  - 9Mbps   */
696 };
697
698 static void ath10k_htt_rx_h_rates(struct ath10k *ar,
699                                   enum ieee80211_band band,
700                                   u8 info0, u32 info1, u32 info2,
701                                   struct ieee80211_rx_status *status)
702 {
703         u8 cck, rate, rate_idx, bw, sgi, mcs, nss;
704         u8 preamble = 0;
705
706         /* Check if valid fields */
707         if (!(info0 & HTT_RX_INDICATION_INFO0_START_VALID))
708                 return;
709
710         preamble = MS(info1, HTT_RX_INDICATION_INFO1_PREAMBLE_TYPE);
711
712         switch (preamble) {
713         case HTT_RX_LEGACY:
714                 cck = info0 & HTT_RX_INDICATION_INFO0_LEGACY_RATE_CCK;
715                 rate = MS(info0, HTT_RX_INDICATION_INFO0_LEGACY_RATE);
716                 rate_idx = 0;
717
718                 if (rate < 0x08 || rate > 0x0F)
719                         break;
720
721                 switch (band) {
722                 case IEEE80211_BAND_2GHZ:
723                         if (cck)
724                                 rate &= ~BIT(3);
725                         rate_idx = rx_legacy_rate_idx[rate];
726                         break;
727                 case IEEE80211_BAND_5GHZ:
728                         rate_idx = rx_legacy_rate_idx[rate];
729                         /* We are using same rate table registering
730                            HW - ath10k_rates[]. In case of 5GHz skip
731                            CCK rates, so -4 here */
732                         rate_idx -= 4;
733                         break;
734                 default:
735                         break;
736                 }
737
738                 status->rate_idx = rate_idx;
739                 break;
740         case HTT_RX_HT:
741         case HTT_RX_HT_WITH_TXBF:
742                 /* HT-SIG - Table 20-11 in info1 and info2 */
743                 mcs = info1 & 0x1F;
744                 nss = mcs >> 3;
745                 bw = (info1 >> 7) & 1;
746                 sgi = (info2 >> 7) & 1;
747
748                 status->rate_idx = mcs;
749                 status->flag |= RX_FLAG_HT;
750                 if (sgi)
751                         status->flag |= RX_FLAG_SHORT_GI;
752                 if (bw)
753                         status->flag |= RX_FLAG_40MHZ;
754                 break;
755         case HTT_RX_VHT:
756         case HTT_RX_VHT_WITH_TXBF:
757                 /* VHT-SIG-A1 in info 1, VHT-SIG-A2 in info2
758                    TODO check this */
759                 mcs = (info2 >> 4) & 0x0F;
760                 nss = ((info1 >> 10) & 0x07) + 1;
761                 bw = info1 & 3;
762                 sgi = info2 & 1;
763
764                 status->rate_idx = mcs;
765                 status->vht_nss = nss;
766
767                 if (sgi)
768                         status->flag |= RX_FLAG_SHORT_GI;
769
770                 switch (bw) {
771                 /* 20MHZ */
772                 case 0:
773                         break;
774                 /* 40MHZ */
775                 case 1:
776                         status->flag |= RX_FLAG_40MHZ;
777                         break;
778                 /* 80MHZ */
779                 case 2:
780                         status->vht_flag |= RX_VHT_FLAG_80MHZ;
781                 }
782
783                 status->flag |= RX_FLAG_VHT;
784                 break;
785         default:
786                 break;
787         }
788 }
789
790 static void ath10k_htt_rx_h_protected(struct ath10k_htt *htt,
791                                       struct ieee80211_rx_status *rx_status,
792                                       struct sk_buff *skb,
793                                       enum htt_rx_mpdu_encrypt_type enctype,
794                                       enum rx_msdu_decap_format fmt,
795                                       bool dot11frag)
796 {
797         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
798
799         rx_status->flag &= ~(RX_FLAG_DECRYPTED |
800                              RX_FLAG_IV_STRIPPED |
801                              RX_FLAG_MMIC_STRIPPED);
802
803         if (enctype == HTT_RX_MPDU_ENCRYPT_NONE)
804                 return;
805
806         /*
807          * There's no explicit rx descriptor flag to indicate whether a given
808          * frame has been decrypted or not. We're forced to use the decap
809          * format as an implicit indication. However fragmentation rx is always
810          * raw and it probably never reports undecrypted raws.
811          *
812          * This makes sure sniffed frames are reported as-is without stripping
813          * the protected flag.
814          */
815         if (fmt == RX_MSDU_DECAP_RAW && !dot11frag)
816                 return;
817
818         rx_status->flag |= RX_FLAG_DECRYPTED |
819                            RX_FLAG_IV_STRIPPED |
820                            RX_FLAG_MMIC_STRIPPED;
821         hdr->frame_control = __cpu_to_le16(__le16_to_cpu(hdr->frame_control) &
822                                            ~IEEE80211_FCTL_PROTECTED);
823 }
824
825 static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
826                                     struct ieee80211_rx_status *status)
827 {
828         struct ieee80211_channel *ch;
829
830         spin_lock_bh(&ar->data_lock);
831         ch = ar->scan_channel;
832         if (!ch)
833                 ch = ar->rx_channel;
834         spin_unlock_bh(&ar->data_lock);
835
836         if (!ch)
837                 return false;
838
839         status->band = ch->band;
840         status->freq = ch->center_freq;
841
842         return true;
843 }
844
845 static const char * const tid_to_ac[] = {
846         "BE",
847         "BK",
848         "BK",
849         "BE",
850         "VI",
851         "VI",
852         "VO",
853         "VO",
854 };
855
856 static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
857 {
858         u8 *qc;
859         int tid;
860
861         if (!ieee80211_is_data_qos(hdr->frame_control))
862                 return "";
863
864         qc = ieee80211_get_qos_ctl(hdr);
865         tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
866         if (tid < 8)
867                 snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
868         else
869                 snprintf(out, size, "tid %d", tid);
870
871         return out;
872 }
873
874 static void ath10k_process_rx(struct ath10k *ar,
875                               struct ieee80211_rx_status *rx_status,
876                               struct sk_buff *skb)
877 {
878         struct ieee80211_rx_status *status;
879         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
880         char tid[32];
881
882         status = IEEE80211_SKB_RXCB(skb);
883         *status = *rx_status;
884
885         ath10k_dbg(ar, ATH10K_DBG_DATA,
886                    "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",
887                    skb,
888                    skb->len,
889                    ieee80211_get_SA(hdr),
890                    ath10k_get_tid(hdr, tid, sizeof(tid)),
891                    is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
892                                                         "mcast" : "ucast",
893                    (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
894                    status->flag == 0 ? "legacy" : "",
895                    status->flag & RX_FLAG_HT ? "ht" : "",
896                    status->flag & RX_FLAG_VHT ? "vht" : "",
897                    status->flag & RX_FLAG_40MHZ ? "40" : "",
898                    status->vht_flag & RX_VHT_FLAG_80MHZ ? "80" : "",
899                    status->flag & RX_FLAG_SHORT_GI ? "sgi " : "",
900                    status->rate_idx,
901                    status->vht_nss,
902                    status->freq,
903                    status->band, status->flag,
904                    !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
905                    !!(status->flag & RX_FLAG_MMIC_ERROR),
906                    !!(status->flag & RX_FLAG_AMSDU_MORE));
907         ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
908                         skb->data, skb->len);
909
910         ieee80211_rx(ar->hw, skb);
911 }
912
913 static int ath10k_htt_rx_nwifi_hdrlen(struct ieee80211_hdr *hdr)
914 {
915         /* nwifi header is padded to 4 bytes. this fixes 4addr rx */
916         return round_up(ieee80211_hdrlen(hdr->frame_control), 4);
917 }
918
919 static void ath10k_htt_rx_amsdu(struct ath10k_htt *htt,
920                                 struct ieee80211_rx_status *rx_status,
921                                 struct sk_buff *skb_in)
922 {
923         struct ath10k *ar = htt->ar;
924         struct htt_rx_desc *rxd;
925         struct sk_buff *skb = skb_in;
926         struct sk_buff *first;
927         enum rx_msdu_decap_format fmt;
928         enum htt_rx_mpdu_encrypt_type enctype;
929         struct ieee80211_hdr *hdr;
930         u8 hdr_buf[64], da[ETH_ALEN], sa[ETH_ALEN], *qos;
931         unsigned int hdr_len;
932
933         rxd = (void *)skb->data - sizeof(*rxd);
934         enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
935                      RX_MPDU_START_INFO0_ENCRYPT_TYPE);
936
937         hdr = (struct ieee80211_hdr *)rxd->rx_hdr_status;
938         hdr_len = ieee80211_hdrlen(hdr->frame_control);
939         memcpy(hdr_buf, hdr, hdr_len);
940         hdr = (struct ieee80211_hdr *)hdr_buf;
941
942         first = skb;
943         while (skb) {
944                 void *decap_hdr;
945                 int len;
946
947                 rxd = (void *)skb->data - sizeof(*rxd);
948                 fmt = MS(__le32_to_cpu(rxd->msdu_start.info1),
949                          RX_MSDU_START_INFO1_DECAP_FORMAT);
950                 decap_hdr = (void *)rxd->rx_hdr_status;
951
952                 skb->ip_summed = ath10k_htt_rx_get_csum_state(skb);
953
954                 /* First frame in an A-MSDU chain has more decapped data. */
955                 if (skb == first) {
956                         len = round_up(ieee80211_hdrlen(hdr->frame_control), 4);
957                         len += round_up(ath10k_htt_rx_crypto_param_len(ar,
958                                                 enctype), 4);
959                         decap_hdr += len;
960                 }
961
962                 switch (fmt) {
963                 case RX_MSDU_DECAP_RAW:
964                         /* remove trailing FCS */
965                         skb_trim(skb, skb->len - FCS_LEN);
966                         break;
967                 case RX_MSDU_DECAP_NATIVE_WIFI:
968                         /* pull decapped header and copy SA & DA */
969                         hdr = (struct ieee80211_hdr *)skb->data;
970                         hdr_len = ath10k_htt_rx_nwifi_hdrlen(hdr);
971                         ether_addr_copy(da, ieee80211_get_DA(hdr));
972                         ether_addr_copy(sa, ieee80211_get_SA(hdr));
973                         skb_pull(skb, hdr_len);
974
975                         /* push original 802.11 header */
976                         hdr = (struct ieee80211_hdr *)hdr_buf;
977                         hdr_len = ieee80211_hdrlen(hdr->frame_control);
978                         memcpy(skb_push(skb, hdr_len), hdr, hdr_len);
979
980                         /* original A-MSDU header has the bit set but we're
981                          * not including A-MSDU subframe header */
982                         hdr = (struct ieee80211_hdr *)skb->data;
983                         qos = ieee80211_get_qos_ctl(hdr);
984                         qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
985
986                         /* original 802.11 header has a different DA and in
987                          * case of 4addr it may also have different SA
988                          */
989                         ether_addr_copy(ieee80211_get_DA(hdr), da);
990                         ether_addr_copy(ieee80211_get_SA(hdr), sa);
991                         break;
992                 case RX_MSDU_DECAP_ETHERNET2_DIX:
993                         /* strip ethernet header and insert decapped 802.11
994                          * header, amsdu subframe header and rfc1042 header */
995
996                         len = 0;
997                         len += sizeof(struct rfc1042_hdr);
998                         len += sizeof(struct amsdu_subframe_hdr);
999
1000                         skb_pull(skb, sizeof(struct ethhdr));
1001                         memcpy(skb_push(skb, len), decap_hdr, len);
1002                         memcpy(skb_push(skb, hdr_len), hdr, hdr_len);
1003                         break;
1004                 case RX_MSDU_DECAP_8023_SNAP_LLC:
1005                         /* insert decapped 802.11 header making a singly
1006                          * A-MSDU */
1007                         memcpy(skb_push(skb, hdr_len), hdr, hdr_len);
1008                         break;
1009                 }
1010
1011                 skb_in = skb;
1012                 ath10k_htt_rx_h_protected(htt, rx_status, skb_in, enctype, fmt,
1013                                           false);
1014                 skb = skb->next;
1015                 skb_in->next = NULL;
1016
1017                 if (skb)
1018                         rx_status->flag |= RX_FLAG_AMSDU_MORE;
1019                 else
1020                         rx_status->flag &= ~RX_FLAG_AMSDU_MORE;
1021
1022                 ath10k_process_rx(htt->ar, rx_status, skb_in);
1023         }
1024
1025         /* FIXME: It might be nice to re-assemble the A-MSDU when there's a
1026          * monitor interface active for sniffing purposes. */
1027 }
1028
1029 static void ath10k_htt_rx_msdu(struct ath10k_htt *htt,
1030                                struct ieee80211_rx_status *rx_status,
1031                                struct sk_buff *skb)
1032 {
1033         struct ath10k *ar = htt->ar;
1034         struct htt_rx_desc *rxd;
1035         struct ieee80211_hdr *hdr;
1036         enum rx_msdu_decap_format fmt;
1037         enum htt_rx_mpdu_encrypt_type enctype;
1038         int hdr_len;
1039         void *rfc1042;
1040
1041         /* This shouldn't happen. If it does than it may be a FW bug. */
1042         if (skb->next) {
1043                 ath10k_warn(ar, "htt rx received chained non A-MSDU frame\n");
1044                 ath10k_htt_rx_free_msdu_chain(skb->next);
1045                 skb->next = NULL;
1046         }
1047
1048         rxd = (void *)skb->data - sizeof(*rxd);
1049         fmt = MS(__le32_to_cpu(rxd->msdu_start.info1),
1050                  RX_MSDU_START_INFO1_DECAP_FORMAT);
1051         enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1052                      RX_MPDU_START_INFO0_ENCRYPT_TYPE);
1053         hdr = (struct ieee80211_hdr *)rxd->rx_hdr_status;
1054         hdr_len = ieee80211_hdrlen(hdr->frame_control);
1055
1056         skb->ip_summed = ath10k_htt_rx_get_csum_state(skb);
1057
1058         switch (fmt) {
1059         case RX_MSDU_DECAP_RAW:
1060                 /* remove trailing FCS */
1061                 skb_trim(skb, skb->len - FCS_LEN);
1062                 break;
1063         case RX_MSDU_DECAP_NATIVE_WIFI:
1064                 /* Pull decapped header */
1065                 hdr = (struct ieee80211_hdr *)skb->data;
1066                 hdr_len = ath10k_htt_rx_nwifi_hdrlen(hdr);
1067                 skb_pull(skb, hdr_len);
1068
1069                 /* Push original header */
1070                 hdr = (struct ieee80211_hdr *)rxd->rx_hdr_status;
1071                 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1072                 memcpy(skb_push(skb, hdr_len), hdr, hdr_len);
1073                 break;
1074         case RX_MSDU_DECAP_ETHERNET2_DIX:
1075                 /* strip ethernet header and insert decapped 802.11 header and
1076                  * rfc1042 header */
1077
1078                 rfc1042 = hdr;
1079                 rfc1042 += roundup(hdr_len, 4);
1080                 rfc1042 += roundup(ath10k_htt_rx_crypto_param_len(ar,
1081                                         enctype), 4);
1082
1083                 skb_pull(skb, sizeof(struct ethhdr));
1084                 memcpy(skb_push(skb, sizeof(struct rfc1042_hdr)),
1085                        rfc1042, sizeof(struct rfc1042_hdr));
1086                 memcpy(skb_push(skb, hdr_len), hdr, hdr_len);
1087                 break;
1088         case RX_MSDU_DECAP_8023_SNAP_LLC:
1089                 /* remove A-MSDU subframe header and insert
1090                  * decapped 802.11 header. rfc1042 header is already there */
1091
1092                 skb_pull(skb, sizeof(struct amsdu_subframe_hdr));
1093                 memcpy(skb_push(skb, hdr_len), hdr, hdr_len);
1094                 break;
1095         }
1096
1097         ath10k_htt_rx_h_protected(htt, rx_status, skb, enctype, fmt, false);
1098
1099         ath10k_process_rx(htt->ar, rx_status, skb);
1100 }
1101
1102 static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
1103 {
1104         struct htt_rx_desc *rxd;
1105         u32 flags, info;
1106         bool is_ip4, is_ip6;
1107         bool is_tcp, is_udp;
1108         bool ip_csum_ok, tcpudp_csum_ok;
1109
1110         rxd = (void *)skb->data - sizeof(*rxd);
1111         flags = __le32_to_cpu(rxd->attention.flags);
1112         info = __le32_to_cpu(rxd->msdu_start.info1);
1113
1114         is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
1115         is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
1116         is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
1117         is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
1118         ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
1119         tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
1120
1121         if (!is_ip4 && !is_ip6)
1122                 return CHECKSUM_NONE;
1123         if (!is_tcp && !is_udp)
1124                 return CHECKSUM_NONE;
1125         if (!ip_csum_ok)
1126                 return CHECKSUM_NONE;
1127         if (!tcpudp_csum_ok)
1128                 return CHECKSUM_NONE;
1129
1130         return CHECKSUM_UNNECESSARY;
1131 }
1132
1133 static int ath10k_unchain_msdu(struct sk_buff *msdu_head)
1134 {
1135         struct sk_buff *next = msdu_head->next;
1136         struct sk_buff *to_free = next;
1137         int space;
1138         int total_len = 0;
1139
1140         /* TODO:  Might could optimize this by using
1141          * skb_try_coalesce or similar method to
1142          * decrease copying, or maybe get mac80211 to
1143          * provide a way to just receive a list of
1144          * skb?
1145          */
1146
1147         msdu_head->next = NULL;
1148
1149         /* Allocate total length all at once. */
1150         while (next) {
1151                 total_len += next->len;
1152                 next = next->next;
1153         }
1154
1155         space = total_len - skb_tailroom(msdu_head);
1156         if ((space > 0) &&
1157             (pskb_expand_head(msdu_head, 0, space, GFP_ATOMIC) < 0)) {
1158                 /* TODO:  bump some rx-oom error stat */
1159                 /* put it back together so we can free the
1160                  * whole list at once.
1161                  */
1162                 msdu_head->next = to_free;
1163                 return -1;
1164         }
1165
1166         /* Walk list again, copying contents into
1167          * msdu_head
1168          */
1169         next = to_free;
1170         while (next) {
1171                 skb_copy_from_linear_data(next, skb_put(msdu_head, next->len),
1172                                           next->len);
1173                 next = next->next;
1174         }
1175
1176         /* If here, we have consolidated skb.  Free the
1177          * fragments and pass the main skb on up the
1178          * stack.
1179          */
1180         ath10k_htt_rx_free_msdu_chain(to_free);
1181         return 0;
1182 }
1183
1184 static bool ath10k_htt_rx_amsdu_allowed(struct ath10k_htt *htt,
1185                                         struct sk_buff *head,
1186                                         enum htt_rx_mpdu_status status,
1187                                         bool channel_set,
1188                                         u32 attention)
1189 {
1190         struct ath10k *ar = htt->ar;
1191
1192         if (head->len == 0) {
1193                 ath10k_dbg(ar, ATH10K_DBG_HTT,
1194                            "htt rx dropping due to zero-len\n");
1195                 return false;
1196         }
1197
1198         if (attention & RX_ATTENTION_FLAGS_DECRYPT_ERR) {
1199                 ath10k_dbg(ar, ATH10K_DBG_HTT,
1200                            "htt rx dropping due to decrypt-err\n");
1201                 return false;
1202         }
1203
1204         if (!channel_set) {
1205                 ath10k_warn(ar, "no channel configured; ignoring frame!\n");
1206                 return false;
1207         }
1208
1209         /* Skip mgmt frames while we handle this in WMI */
1210         if (attention & RX_ATTENTION_FLAGS_MGMT_TYPE) {
1211                 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx mgmt ctrl\n");
1212                 return false;
1213         }
1214
1215         if (status != HTT_RX_IND_MPDU_STATUS_OK &&
1216             status != HTT_RX_IND_MPDU_STATUS_TKIP_MIC_ERR &&
1217             status != HTT_RX_IND_MPDU_STATUS_ERR_INV_PEER &&
1218             !htt->ar->monitor_started) {
1219                 ath10k_dbg(ar, ATH10K_DBG_HTT,
1220                            "htt rx ignoring frame w/ status %d\n",
1221                            status);
1222                 return false;
1223         }
1224
1225         if (test_bit(ATH10K_CAC_RUNNING, &htt->ar->dev_flags)) {
1226                 ath10k_dbg(ar, ATH10K_DBG_HTT,
1227                            "htt rx CAC running\n");
1228                 return false;
1229         }
1230
1231         return true;
1232 }
1233
1234 static void ath10k_htt_rx_handler(struct ath10k_htt *htt,
1235                                   struct htt_rx_indication *rx)
1236 {
1237         struct ath10k *ar = htt->ar;
1238         struct ieee80211_rx_status *rx_status = &htt->rx_status;
1239         struct htt_rx_indication_mpdu_range *mpdu_ranges;
1240         enum htt_rx_mpdu_status status;
1241         struct ieee80211_hdr *hdr;
1242         int num_mpdu_ranges;
1243         u32 attention;
1244         int fw_desc_len;
1245         u8 *fw_desc;
1246         bool channel_set;
1247         int i, j;
1248         int ret;
1249
1250         lockdep_assert_held(&htt->rx_ring.lock);
1251
1252         fw_desc_len = __le16_to_cpu(rx->prefix.fw_rx_desc_bytes);
1253         fw_desc = (u8 *)&rx->fw_desc;
1254
1255         num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
1256                              HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
1257         mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
1258
1259         /* Fill this once, while this is per-ppdu */
1260         if (rx->ppdu.info0 & HTT_RX_INDICATION_INFO0_START_VALID) {
1261                 memset(rx_status, 0, sizeof(*rx_status));
1262                 rx_status->signal  = ATH10K_DEFAULT_NOISE_FLOOR +
1263                                      rx->ppdu.combined_rssi;
1264         }
1265
1266         if (rx->ppdu.info0 & HTT_RX_INDICATION_INFO0_END_VALID) {
1267                 /* TSF available only in 32-bit */
1268                 rx_status->mactime = __le32_to_cpu(rx->ppdu.tsf) & 0xffffffff;
1269                 rx_status->flag |= RX_FLAG_MACTIME_END;
1270         }
1271
1272         channel_set = ath10k_htt_rx_h_channel(htt->ar, rx_status);
1273
1274         if (channel_set) {
1275                 ath10k_htt_rx_h_rates(htt->ar, rx_status->band,
1276                                       rx->ppdu.info0,
1277                                       __le32_to_cpu(rx->ppdu.info1),
1278                                       __le32_to_cpu(rx->ppdu.info2),
1279                                       rx_status);
1280         }
1281
1282         ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
1283                         rx, sizeof(*rx) +
1284                         (sizeof(struct htt_rx_indication_mpdu_range) *
1285                                 num_mpdu_ranges));
1286
1287         for (i = 0; i < num_mpdu_ranges; i++) {
1288                 status = mpdu_ranges[i].mpdu_range_status;
1289
1290                 for (j = 0; j < mpdu_ranges[i].mpdu_count; j++) {
1291                         struct sk_buff *msdu_head, *msdu_tail;
1292
1293                         attention = 0;
1294                         msdu_head = NULL;
1295                         msdu_tail = NULL;
1296                         ret = ath10k_htt_rx_amsdu_pop(htt,
1297                                                       &fw_desc,
1298                                                       &fw_desc_len,
1299                                                       &msdu_head,
1300                                                       &msdu_tail,
1301                                                       &attention);
1302
1303                         if (ret < 0) {
1304                                 ath10k_warn(ar, "failed to pop amsdu from htt rx ring %d\n",
1305                                             ret);
1306                                 ath10k_htt_rx_free_msdu_chain(msdu_head);
1307                                 continue;
1308                         }
1309
1310                         if (!ath10k_htt_rx_amsdu_allowed(htt, msdu_head,
1311                                                          status,
1312                                                          channel_set,
1313                                                          attention)) {
1314                                 ath10k_htt_rx_free_msdu_chain(msdu_head);
1315                                 continue;
1316                         }
1317
1318                         if (ret > 0 &&
1319                             ath10k_unchain_msdu(msdu_head) < 0) {
1320                                 ath10k_htt_rx_free_msdu_chain(msdu_head);
1321                                 continue;
1322                         }
1323
1324                         if (attention & RX_ATTENTION_FLAGS_FCS_ERR)
1325                                 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
1326                         else
1327                                 rx_status->flag &= ~RX_FLAG_FAILED_FCS_CRC;
1328
1329                         if (attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR)
1330                                 rx_status->flag |= RX_FLAG_MMIC_ERROR;
1331                         else
1332                                 rx_status->flag &= ~RX_FLAG_MMIC_ERROR;
1333
1334                         hdr = ath10k_htt_rx_skb_get_hdr(msdu_head);
1335
1336                         if (ath10k_htt_rx_hdr_is_amsdu(hdr))
1337                                 ath10k_htt_rx_amsdu(htt, rx_status, msdu_head);
1338                         else
1339                                 ath10k_htt_rx_msdu(htt, rx_status, msdu_head);
1340                 }
1341         }
1342
1343         tasklet_schedule(&htt->rx_replenish_task);
1344 }
1345
1346 static void ath10k_htt_rx_frag_handler(struct ath10k_htt *htt,
1347                                        struct htt_rx_fragment_indication *frag)
1348 {
1349         struct ath10k *ar = htt->ar;
1350         struct sk_buff *msdu_head, *msdu_tail;
1351         enum htt_rx_mpdu_encrypt_type enctype;
1352         struct htt_rx_desc *rxd;
1353         enum rx_msdu_decap_format fmt;
1354         struct ieee80211_rx_status *rx_status = &htt->rx_status;
1355         struct ieee80211_hdr *hdr;
1356         int ret;
1357         bool tkip_mic_err;
1358         bool decrypt_err;
1359         u8 *fw_desc;
1360         int fw_desc_len, hdrlen, paramlen;
1361         int trim;
1362         u32 attention = 0;
1363
1364         fw_desc_len = __le16_to_cpu(frag->fw_rx_desc_bytes);
1365         fw_desc = (u8 *)frag->fw_msdu_rx_desc;
1366
1367         msdu_head = NULL;
1368         msdu_tail = NULL;
1369
1370         spin_lock_bh(&htt->rx_ring.lock);
1371         ret = ath10k_htt_rx_amsdu_pop(htt, &fw_desc, &fw_desc_len,
1372                                       &msdu_head, &msdu_tail,
1373                                       &attention);
1374         spin_unlock_bh(&htt->rx_ring.lock);
1375
1376         ath10k_dbg(ar, ATH10K_DBG_HTT_DUMP, "htt rx frag ahead\n");
1377
1378         if (ret) {
1379                 ath10k_warn(ar, "failed to pop amsdu from httr rx ring for fragmented rx %d\n",
1380                             ret);
1381                 ath10k_htt_rx_free_msdu_chain(msdu_head);
1382                 return;
1383         }
1384
1385         /* FIXME: implement signal strength */
1386         rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1387
1388         hdr = (struct ieee80211_hdr *)msdu_head->data;
1389         rxd = (void *)msdu_head->data - sizeof(*rxd);
1390         tkip_mic_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
1391         decrypt_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
1392         fmt = MS(__le32_to_cpu(rxd->msdu_start.info1),
1393                  RX_MSDU_START_INFO1_DECAP_FORMAT);
1394
1395         if (fmt != RX_MSDU_DECAP_RAW) {
1396                 ath10k_warn(ar, "we dont support non-raw fragmented rx yet\n");
1397                 dev_kfree_skb_any(msdu_head);
1398                 goto end;
1399         }
1400
1401         enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1402                      RX_MPDU_START_INFO0_ENCRYPT_TYPE);
1403         ath10k_htt_rx_h_protected(htt, rx_status, msdu_head, enctype, fmt,
1404                                   true);
1405         msdu_head->ip_summed = ath10k_htt_rx_get_csum_state(msdu_head);
1406
1407         if (tkip_mic_err)
1408                 ath10k_warn(ar, "tkip mic error\n");
1409
1410         if (decrypt_err) {
1411                 ath10k_warn(ar, "decryption err in fragmented rx\n");
1412                 dev_kfree_skb_any(msdu_head);
1413                 goto end;
1414         }
1415
1416         if (enctype != HTT_RX_MPDU_ENCRYPT_NONE) {
1417                 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1418                 paramlen = ath10k_htt_rx_crypto_param_len(ar, enctype);
1419
1420                 /* It is more efficient to move the header than the payload */
1421                 memmove((void *)msdu_head->data + paramlen,
1422                         (void *)msdu_head->data,
1423                         hdrlen);
1424                 skb_pull(msdu_head, paramlen);
1425                 hdr = (struct ieee80211_hdr *)msdu_head->data;
1426         }
1427
1428         /* remove trailing FCS */
1429         trim  = 4;
1430
1431         /* remove crypto trailer */
1432         trim += ath10k_htt_rx_crypto_tail_len(ar, enctype);
1433
1434         /* last fragment of TKIP frags has MIC */
1435         if (!ieee80211_has_morefrags(hdr->frame_control) &&
1436             enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
1437                 trim += MICHAEL_MIC_LEN;
1438
1439         if (trim > msdu_head->len) {
1440                 ath10k_warn(ar, "htt rx fragment: trailer longer than the frame itself? drop\n");
1441                 dev_kfree_skb_any(msdu_head);
1442                 goto end;
1443         }
1444
1445         skb_trim(msdu_head, msdu_head->len - trim);
1446
1447         ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx frag mpdu: ",
1448                         msdu_head->data, msdu_head->len);
1449         ath10k_process_rx(htt->ar, rx_status, msdu_head);
1450
1451 end:
1452         if (fw_desc_len > 0) {
1453                 ath10k_dbg(ar, ATH10K_DBG_HTT,
1454                            "expecting more fragmented rx in one indication %d\n",
1455                            fw_desc_len);
1456         }
1457 }
1458
1459 static void ath10k_htt_rx_frm_tx_compl(struct ath10k *ar,
1460                                        struct sk_buff *skb)
1461 {
1462         struct ath10k_htt *htt = &ar->htt;
1463         struct htt_resp *resp = (struct htt_resp *)skb->data;
1464         struct htt_tx_done tx_done = {};
1465         int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
1466         __le16 msdu_id;
1467         int i;
1468
1469         lockdep_assert_held(&htt->tx_lock);
1470
1471         switch (status) {
1472         case HTT_DATA_TX_STATUS_NO_ACK:
1473                 tx_done.no_ack = true;
1474                 break;
1475         case HTT_DATA_TX_STATUS_OK:
1476                 break;
1477         case HTT_DATA_TX_STATUS_DISCARD:
1478         case HTT_DATA_TX_STATUS_POSTPONE:
1479         case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
1480                 tx_done.discard = true;
1481                 break;
1482         default:
1483                 ath10k_warn(ar, "unhandled tx completion status %d\n", status);
1484                 tx_done.discard = true;
1485                 break;
1486         }
1487
1488         ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
1489                    resp->data_tx_completion.num_msdus);
1490
1491         for (i = 0; i < resp->data_tx_completion.num_msdus; i++) {
1492                 msdu_id = resp->data_tx_completion.msdus[i];
1493                 tx_done.msdu_id = __le16_to_cpu(msdu_id);
1494                 ath10k_txrx_tx_unref(htt, &tx_done);
1495         }
1496 }
1497
1498 static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
1499 {
1500         struct htt_rx_addba *ev = &resp->rx_addba;
1501         struct ath10k_peer *peer;
1502         struct ath10k_vif *arvif;
1503         u16 info0, tid, peer_id;
1504
1505         info0 = __le16_to_cpu(ev->info0);
1506         tid = MS(info0, HTT_RX_BA_INFO0_TID);
1507         peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1508
1509         ath10k_dbg(ar, ATH10K_DBG_HTT,
1510                    "htt rx addba tid %hu peer_id %hu size %hhu\n",
1511                    tid, peer_id, ev->window_size);
1512
1513         spin_lock_bh(&ar->data_lock);
1514         peer = ath10k_peer_find_by_id(ar, peer_id);
1515         if (!peer) {
1516                 ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
1517                             peer_id);
1518                 spin_unlock_bh(&ar->data_lock);
1519                 return;
1520         }
1521
1522         arvif = ath10k_get_arvif(ar, peer->vdev_id);
1523         if (!arvif) {
1524                 ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
1525                             peer->vdev_id);
1526                 spin_unlock_bh(&ar->data_lock);
1527                 return;
1528         }
1529
1530         ath10k_dbg(ar, ATH10K_DBG_HTT,
1531                    "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
1532                    peer->addr, tid, ev->window_size);
1533
1534         ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1535         spin_unlock_bh(&ar->data_lock);
1536 }
1537
1538 static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
1539 {
1540         struct htt_rx_delba *ev = &resp->rx_delba;
1541         struct ath10k_peer *peer;
1542         struct ath10k_vif *arvif;
1543         u16 info0, tid, peer_id;
1544
1545         info0 = __le16_to_cpu(ev->info0);
1546         tid = MS(info0, HTT_RX_BA_INFO0_TID);
1547         peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1548
1549         ath10k_dbg(ar, ATH10K_DBG_HTT,
1550                    "htt rx delba tid %hu peer_id %hu\n",
1551                    tid, peer_id);
1552
1553         spin_lock_bh(&ar->data_lock);
1554         peer = ath10k_peer_find_by_id(ar, peer_id);
1555         if (!peer) {
1556                 ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
1557                             peer_id);
1558                 spin_unlock_bh(&ar->data_lock);
1559                 return;
1560         }
1561
1562         arvif = ath10k_get_arvif(ar, peer->vdev_id);
1563         if (!arvif) {
1564                 ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
1565                             peer->vdev_id);
1566                 spin_unlock_bh(&ar->data_lock);
1567                 return;
1568         }
1569
1570         ath10k_dbg(ar, ATH10K_DBG_HTT,
1571                    "htt rx stop rx ba session sta %pM tid %hu\n",
1572                    peer->addr, tid);
1573
1574         ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1575         spin_unlock_bh(&ar->data_lock);
1576 }
1577
1578 void ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
1579 {
1580         struct ath10k_htt *htt = &ar->htt;
1581         struct htt_resp *resp = (struct htt_resp *)skb->data;
1582
1583         /* confirm alignment */
1584         if (!IS_ALIGNED((unsigned long)skb->data, 4))
1585                 ath10k_warn(ar, "unaligned htt message, expect trouble\n");
1586
1587         ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
1588                    resp->hdr.msg_type);
1589         switch (resp->hdr.msg_type) {
1590         case HTT_T2H_MSG_TYPE_VERSION_CONF: {
1591                 htt->target_version_major = resp->ver_resp.major;
1592                 htt->target_version_minor = resp->ver_resp.minor;
1593                 complete(&htt->target_version_received);
1594                 break;
1595         }
1596         case HTT_T2H_MSG_TYPE_RX_IND:
1597                 spin_lock_bh(&htt->rx_ring.lock);
1598                 __skb_queue_tail(&htt->rx_compl_q, skb);
1599                 spin_unlock_bh(&htt->rx_ring.lock);
1600                 tasklet_schedule(&htt->txrx_compl_task);
1601                 return;
1602         case HTT_T2H_MSG_TYPE_PEER_MAP: {
1603                 struct htt_peer_map_event ev = {
1604                         .vdev_id = resp->peer_map.vdev_id,
1605                         .peer_id = __le16_to_cpu(resp->peer_map.peer_id),
1606                 };
1607                 memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
1608                 ath10k_peer_map_event(htt, &ev);
1609                 break;
1610         }
1611         case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
1612                 struct htt_peer_unmap_event ev = {
1613                         .peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
1614                 };
1615                 ath10k_peer_unmap_event(htt, &ev);
1616                 break;
1617         }
1618         case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
1619                 struct htt_tx_done tx_done = {};
1620                 int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
1621
1622                 tx_done.msdu_id =
1623                         __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
1624
1625                 switch (status) {
1626                 case HTT_MGMT_TX_STATUS_OK:
1627                         break;
1628                 case HTT_MGMT_TX_STATUS_RETRY:
1629                         tx_done.no_ack = true;
1630                         break;
1631                 case HTT_MGMT_TX_STATUS_DROP:
1632                         tx_done.discard = true;
1633                         break;
1634                 }
1635
1636                 spin_lock_bh(&htt->tx_lock);
1637                 ath10k_txrx_tx_unref(htt, &tx_done);
1638                 spin_unlock_bh(&htt->tx_lock);
1639                 break;
1640         }
1641         case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
1642                 spin_lock_bh(&htt->tx_lock);
1643                 __skb_queue_tail(&htt->tx_compl_q, skb);
1644                 spin_unlock_bh(&htt->tx_lock);
1645                 tasklet_schedule(&htt->txrx_compl_task);
1646                 return;
1647         case HTT_T2H_MSG_TYPE_SEC_IND: {
1648                 struct ath10k *ar = htt->ar;
1649                 struct htt_security_indication *ev = &resp->security_indication;
1650
1651                 ath10k_dbg(ar, ATH10K_DBG_HTT,
1652                            "sec ind peer_id %d unicast %d type %d\n",
1653                           __le16_to_cpu(ev->peer_id),
1654                           !!(ev->flags & HTT_SECURITY_IS_UNICAST),
1655                           MS(ev->flags, HTT_SECURITY_TYPE));
1656                 complete(&ar->install_key_done);
1657                 break;
1658         }
1659         case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
1660                 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
1661                                 skb->data, skb->len);
1662                 ath10k_htt_rx_frag_handler(htt, &resp->rx_frag_ind);
1663                 break;
1664         }
1665         case HTT_T2H_MSG_TYPE_TEST:
1666                 /* FIX THIS */
1667                 break;
1668         case HTT_T2H_MSG_TYPE_STATS_CONF:
1669                 trace_ath10k_htt_stats(ar, skb->data, skb->len);
1670                 break;
1671         case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
1672                 /* Firmware can return tx frames if it's unable to fully
1673                  * process them and suspects host may be able to fix it. ath10k
1674                  * sends all tx frames as already inspected so this shouldn't
1675                  * happen unless fw has a bug.
1676                  */
1677                 ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
1678                 break;
1679         case HTT_T2H_MSG_TYPE_RX_ADDBA:
1680                 ath10k_htt_rx_addba(ar, resp);
1681                 break;
1682         case HTT_T2H_MSG_TYPE_RX_DELBA:
1683                 ath10k_htt_rx_delba(ar, resp);
1684                 break;
1685         case HTT_T2H_MSG_TYPE_PKTLOG: {
1686                 struct ath10k_pktlog_hdr *hdr =
1687                         (struct ath10k_pktlog_hdr *)resp->pktlog_msg.payload;
1688
1689                 trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
1690                                         sizeof(*hdr) +
1691                                         __le16_to_cpu(hdr->size));
1692                 break;
1693         }
1694         case HTT_T2H_MSG_TYPE_RX_FLUSH: {
1695                 /* Ignore this event because mac80211 takes care of Rx
1696                  * aggregation reordering.
1697                  */
1698                 break;
1699         }
1700         default:
1701                 ath10k_warn(ar, "htt event (%d) not handled\n",
1702                             resp->hdr.msg_type);
1703                 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
1704                                 skb->data, skb->len);
1705                 break;
1706         };
1707
1708         /* Free the indication buffer */
1709         dev_kfree_skb_any(skb);
1710 }
1711
1712 static void ath10k_htt_txrx_compl_task(unsigned long ptr)
1713 {
1714         struct ath10k_htt *htt = (struct ath10k_htt *)ptr;
1715         struct htt_resp *resp;
1716         struct sk_buff *skb;
1717
1718         spin_lock_bh(&htt->tx_lock);
1719         while ((skb = __skb_dequeue(&htt->tx_compl_q))) {
1720                 ath10k_htt_rx_frm_tx_compl(htt->ar, skb);
1721                 dev_kfree_skb_any(skb);
1722         }
1723         spin_unlock_bh(&htt->tx_lock);
1724
1725         spin_lock_bh(&htt->rx_ring.lock);
1726         while ((skb = __skb_dequeue(&htt->rx_compl_q))) {
1727                 resp = (struct htt_resp *)skb->data;
1728                 ath10k_htt_rx_handler(htt, &resp->rx_ind);
1729                 dev_kfree_skb_any(skb);
1730         }
1731         spin_unlock_bh(&htt->rx_ring.lock);
1732 }