blob: e31438541ee1defbcd9e19c73cf66a12f07316d3 [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
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
Michal Kazioredb82362013-07-05 16:15:14 +030018#include "core.h"
Kalle Valo5e3dd152013-06-12 20:52:10 +030019#include "htc.h"
20#include "htt.h"
21#include "txrx.h"
22#include "debug.h"
Kalle Valoa9bf0502013-09-03 11:43:55 +030023#include "trace.h"
Michal Kazioraa5b4fb2014-07-23 12:20:33 +020024#include "mac.h"
Kalle Valo5e3dd152013-06-12 20:52:10 +030025
26#include <linux/log2.h>
27
Michal Kaziorc5450702015-01-24 12:14:48 +020028#define HTT_RX_RING_SIZE HTT_RX_RING_SIZE_MAX
29#define HTT_RX_RING_FILL_LEVEL (((HTT_RX_RING_SIZE) / 2) - 1)
Kalle Valo5e3dd152013-06-12 20:52:10 +030030
31/* when under memory pressure rx ring refill may fail and needs a retry */
32#define HTT_RX_RING_REFILL_RETRY_MS 50
33
Rajkumar Manoharan5c86d972016-03-22 17:22:19 +053034#define HTT_RX_RING_REFILL_RESCHED_MS 5
35
Michal Kaziorf6dc2092013-09-26 10:12:22 +030036static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);
Michal Kaziorf6dc2092013-09-26 10:12:22 +030037
Michal Kaziorc5450702015-01-24 12:14:48 +020038static struct sk_buff *
39ath10k_htt_rx_find_skb_paddr(struct ath10k *ar, u32 paddr)
40{
41 struct ath10k_skb_rxcb *rxcb;
42
43 hash_for_each_possible(ar->htt.rx_ring.skb_table, rxcb, hlist, paddr)
44 if (rxcb->paddr == paddr)
45 return ATH10K_RXCB_SKB(rxcb);
46
47 WARN_ON_ONCE(1);
48 return NULL;
49}
50
Kalle Valo5e3dd152013-06-12 20:52:10 +030051static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
52{
53 struct sk_buff *skb;
Michal Kaziorc5450702015-01-24 12:14:48 +020054 struct ath10k_skb_rxcb *rxcb;
55 struct hlist_node *n;
Kalle Valo5e3dd152013-06-12 20:52:10 +030056 int i;
57
Michal Kaziorc5450702015-01-24 12:14:48 +020058 if (htt->rx_ring.in_ord_rx) {
59 hash_for_each_safe(htt->rx_ring.skb_table, i, n, rxcb, hlist) {
60 skb = ATH10K_RXCB_SKB(rxcb);
61 dma_unmap_single(htt->ar->dev, rxcb->paddr,
62 skb->len + skb_tailroom(skb),
63 DMA_FROM_DEVICE);
64 hash_del(&rxcb->hlist);
65 dev_kfree_skb_any(skb);
66 }
67 } else {
68 for (i = 0; i < htt->rx_ring.size; i++) {
69 skb = htt->rx_ring.netbufs_ring[i];
70 if (!skb)
71 continue;
72
73 rxcb = ATH10K_SKB_RXCB(skb);
74 dma_unmap_single(htt->ar->dev, rxcb->paddr,
75 skb->len + skb_tailroom(skb),
76 DMA_FROM_DEVICE);
77 dev_kfree_skb_any(skb);
78 }
Kalle Valo5e3dd152013-06-12 20:52:10 +030079 }
80
81 htt->rx_ring.fill_cnt = 0;
Michal Kaziorc5450702015-01-24 12:14:48 +020082 hash_init(htt->rx_ring.skb_table);
83 memset(htt->rx_ring.netbufs_ring, 0,
84 htt->rx_ring.size * sizeof(htt->rx_ring.netbufs_ring[0]));
Kalle Valo5e3dd152013-06-12 20:52:10 +030085}
86
87static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
88{
89 struct htt_rx_desc *rx_desc;
Michal Kaziorc5450702015-01-24 12:14:48 +020090 struct ath10k_skb_rxcb *rxcb;
Kalle Valo5e3dd152013-06-12 20:52:10 +030091 struct sk_buff *skb;
92 dma_addr_t paddr;
93 int ret = 0, idx;
94
Michal Kaziorc5450702015-01-24 12:14:48 +020095 /* The Full Rx Reorder firmware has no way of telling the host
96 * implicitly when it copied HTT Rx Ring buffers to MAC Rx Ring.
97 * To keep things simple make sure ring is always half empty. This
98 * guarantees there'll be no replenishment overruns possible.
99 */
100 BUILD_BUG_ON(HTT_RX_RING_FILL_LEVEL >= HTT_RX_RING_SIZE / 2);
101
Kalle Valo8cc7f262014-09-14 12:50:39 +0300102 idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300103 while (num > 0) {
104 skb = dev_alloc_skb(HTT_RX_BUF_SIZE + HTT_RX_DESC_ALIGN);
105 if (!skb) {
106 ret = -ENOMEM;
107 goto fail;
108 }
109
110 if (!IS_ALIGNED((unsigned long)skb->data, HTT_RX_DESC_ALIGN))
111 skb_pull(skb,
112 PTR_ALIGN(skb->data, HTT_RX_DESC_ALIGN) -
113 skb->data);
114
115 /* Clear rx_desc attention word before posting to Rx ring */
116 rx_desc = (struct htt_rx_desc *)skb->data;
117 rx_desc->attention.flags = __cpu_to_le32(0);
118
119 paddr = dma_map_single(htt->ar->dev, skb->data,
120 skb->len + skb_tailroom(skb),
121 DMA_FROM_DEVICE);
122
123 if (unlikely(dma_mapping_error(htt->ar->dev, paddr))) {
124 dev_kfree_skb_any(skb);
125 ret = -ENOMEM;
126 goto fail;
127 }
128
Michal Kaziorc5450702015-01-24 12:14:48 +0200129 rxcb = ATH10K_SKB_RXCB(skb);
130 rxcb->paddr = paddr;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300131 htt->rx_ring.netbufs_ring[idx] = skb;
132 htt->rx_ring.paddrs_ring[idx] = __cpu_to_le32(paddr);
133 htt->rx_ring.fill_cnt++;
134
Michal Kaziorc5450702015-01-24 12:14:48 +0200135 if (htt->rx_ring.in_ord_rx) {
136 hash_add(htt->rx_ring.skb_table,
137 &ATH10K_SKB_RXCB(skb)->hlist,
138 (u32)paddr);
139 }
140
Kalle Valo5e3dd152013-06-12 20:52:10 +0300141 num--;
142 idx++;
143 idx &= htt->rx_ring.size_mask;
144 }
145
146fail:
Vasanthakumar Thiagarajan5de6dfc2015-01-09 22:49:46 +0530147 /*
148 * Make sure the rx buffer is updated before available buffer
149 * index to avoid any potential rx ring corruption.
150 */
151 mb();
Kalle Valo8cc7f262014-09-14 12:50:39 +0300152 *htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300153 return ret;
154}
155
156static int ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
157{
158 lockdep_assert_held(&htt->rx_ring.lock);
159 return __ath10k_htt_rx_ring_fill_n(htt, num);
160}
161
162static void ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt *htt)
163{
Michal Kazior6e712d42013-09-24 10:18:36 +0200164 int ret, num_deficit, num_to_fill;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300165
Michal Kazior6e712d42013-09-24 10:18:36 +0200166 /* Refilling the whole RX ring buffer proves to be a bad idea. The
167 * reason is RX may take up significant amount of CPU cycles and starve
168 * other tasks, e.g. TX on an ethernet device while acting as a bridge
169 * with ath10k wlan interface. This ended up with very poor performance
170 * once CPU the host system was overwhelmed with RX on ath10k.
171 *
172 * By limiting the number of refills the replenishing occurs
173 * progressively. This in turns makes use of the fact tasklets are
174 * processed in FIFO order. This means actual RX processing can starve
175 * out refilling. If there's not enough buffers on RX ring FW will not
176 * report RX until it is refilled with enough buffers. This
177 * automatically balances load wrt to CPU power.
178 *
179 * This probably comes at a cost of lower maximum throughput but
Marcin Rokickid6dfe25c2017-02-20 14:39:57 +0100180 * improves the average and stability.
181 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300182 spin_lock_bh(&htt->rx_ring.lock);
Michal Kazior6e712d42013-09-24 10:18:36 +0200183 num_deficit = htt->rx_ring.fill_level - htt->rx_ring.fill_cnt;
184 num_to_fill = min(ATH10K_HTT_MAX_NUM_REFILL, num_deficit);
185 num_deficit -= num_to_fill;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300186 ret = ath10k_htt_rx_ring_fill_n(htt, num_to_fill);
187 if (ret == -ENOMEM) {
188 /*
189 * Failed to fill it to the desired level -
190 * we'll start a timer and try again next time.
191 * As long as enough buffers are left in the ring for
192 * another A-MPDU rx, no special recovery is needed.
193 */
194 mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
195 msecs_to_jiffies(HTT_RX_RING_REFILL_RETRY_MS));
Michal Kazior6e712d42013-09-24 10:18:36 +0200196 } else if (num_deficit > 0) {
Rajkumar Manoharan5c86d972016-03-22 17:22:19 +0530197 mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
198 msecs_to_jiffies(HTT_RX_RING_REFILL_RESCHED_MS));
Kalle Valo5e3dd152013-06-12 20:52:10 +0300199 }
200 spin_unlock_bh(&htt->rx_ring.lock);
201}
202
Kees Cook7ac76762017-10-24 02:29:54 -0700203static void ath10k_htt_rx_ring_refill_retry(struct timer_list *t)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300204{
Kees Cook7ac76762017-10-24 02:29:54 -0700205 struct ath10k_htt *htt = from_timer(htt, t, rx_ring.refill_retry_timer);
Kalle Valoaf762c02014-09-14 12:50:17 +0300206
Kalle Valo5e3dd152013-06-12 20:52:10 +0300207 ath10k_htt_rx_msdu_buff_replenish(htt);
208}
209
Michal Kaziorc5450702015-01-24 12:14:48 +0200210int ath10k_htt_rx_ring_refill(struct ath10k *ar)
Michal Kazior3e841fd2014-05-14 16:23:31 +0300211{
Michal Kaziorc5450702015-01-24 12:14:48 +0200212 struct ath10k_htt *htt = &ar->htt;
213 int ret;
Michal Kazior3e841fd2014-05-14 16:23:31 +0300214
Michal Kaziorc5450702015-01-24 12:14:48 +0200215 spin_lock_bh(&htt->rx_ring.lock);
216 ret = ath10k_htt_rx_ring_fill_n(htt, (htt->rx_ring.fill_level -
217 htt->rx_ring.fill_cnt));
218 spin_unlock_bh(&htt->rx_ring.lock);
Michal Kazior3e841fd2014-05-14 16:23:31 +0300219
Michal Kaziorc5450702015-01-24 12:14:48 +0200220 if (ret)
221 ath10k_htt_rx_ring_free(htt);
222
223 return ret;
Michal Kazior3e841fd2014-05-14 16:23:31 +0300224}
225
Michal Kazior95bf21f2014-05-16 17:15:39 +0300226void ath10k_htt_rx_free(struct ath10k_htt *htt)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300227{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300228 del_timer_sync(&htt->rx_ring.refill_retry_timer);
Michal Kazior6c5151a2014-02-27 18:50:04 +0200229
Michal Kazior6c5151a2014-02-27 18:50:04 +0200230 skb_queue_purge(&htt->rx_compl_q);
Michal Kaziorc5450702015-01-24 12:14:48 +0200231 skb_queue_purge(&htt->rx_in_ord_compl_q);
Michal Kazior426e10e2016-03-06 16:14:43 +0200232 skb_queue_purge(&htt->tx_fetch_ind_q);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300233
Michal Kaziorc5450702015-01-24 12:14:48 +0200234 ath10k_htt_rx_ring_free(htt);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300235
236 dma_free_coherent(htt->ar->dev,
237 (htt->rx_ring.size *
238 sizeof(htt->rx_ring.paddrs_ring)),
239 htt->rx_ring.paddrs_ring,
240 htt->rx_ring.base_paddr);
241
242 dma_free_coherent(htt->ar->dev,
243 sizeof(*htt->rx_ring.alloc_idx.vaddr),
244 htt->rx_ring.alloc_idx.vaddr,
245 htt->rx_ring.alloc_idx.paddr);
246
247 kfree(htt->rx_ring.netbufs_ring);
248}
249
250static inline struct sk_buff *ath10k_htt_rx_netbuf_pop(struct ath10k_htt *htt)
251{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200252 struct ath10k *ar = htt->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300253 int idx;
254 struct sk_buff *msdu;
255
Michal Kazior45967082014-02-27 18:50:05 +0200256 lockdep_assert_held(&htt->rx_ring.lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300257
Michal Kazior8d60ee82014-02-27 18:50:05 +0200258 if (htt->rx_ring.fill_cnt == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200259 ath10k_warn(ar, "tried to pop sk_buff from an empty rx ring\n");
Michal Kazior8d60ee82014-02-27 18:50:05 +0200260 return NULL;
261 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300262
263 idx = htt->rx_ring.sw_rd_idx.msdu_payld;
264 msdu = htt->rx_ring.netbufs_ring[idx];
Michal Kazior3e841fd2014-05-14 16:23:31 +0300265 htt->rx_ring.netbufs_ring[idx] = NULL;
Michal Kaziorc5450702015-01-24 12:14:48 +0200266 htt->rx_ring.paddrs_ring[idx] = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300267
268 idx++;
269 idx &= htt->rx_ring.size_mask;
270 htt->rx_ring.sw_rd_idx.msdu_payld = idx;
271 htt->rx_ring.fill_cnt--;
272
Michal Kazior4de02802014-10-23 17:04:23 +0300273 dma_unmap_single(htt->ar->dev,
Michal Kazior8582bf32015-01-24 12:14:47 +0200274 ATH10K_SKB_RXCB(msdu)->paddr,
Michal Kazior4de02802014-10-23 17:04:23 +0300275 msdu->len + skb_tailroom(msdu),
276 DMA_FROM_DEVICE);
277 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
278 msdu->data, msdu->len + skb_tailroom(msdu));
Michal Kazior4de02802014-10-23 17:04:23 +0300279
Kalle Valo5e3dd152013-06-12 20:52:10 +0300280 return msdu;
281}
282
Janusz Dziedzicd84dd602014-03-24 21:23:20 +0100283/* return: < 0 fatal error, 0 - non chained msdu, 1 chained msdu */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300284static int ath10k_htt_rx_amsdu_pop(struct ath10k_htt *htt,
Michal Kaziorf0e27702014-11-18 09:24:49 +0200285 struct sk_buff_head *amsdu)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300286{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200287 struct ath10k *ar = htt->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300288 int msdu_len, msdu_chaining = 0;
Michal Kazior9aa505d2014-11-18 09:24:47 +0200289 struct sk_buff *msdu;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300290 struct htt_rx_desc *rx_desc;
291
Michal Kazior45967082014-02-27 18:50:05 +0200292 lockdep_assert_held(&htt->rx_ring.lock);
293
Michal Kazior9aa505d2014-11-18 09:24:47 +0200294 for (;;) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300295 int last_msdu, msdu_len_invalid, msdu_chained;
296
Michal Kazior9aa505d2014-11-18 09:24:47 +0200297 msdu = ath10k_htt_rx_netbuf_pop(htt);
298 if (!msdu) {
Michal Kazior9aa505d2014-11-18 09:24:47 +0200299 __skb_queue_purge(amsdu);
Michal Kaziore0bd7512014-11-18 09:24:48 +0200300 return -ENOENT;
Michal Kazior9aa505d2014-11-18 09:24:47 +0200301 }
302
303 __skb_queue_tail(amsdu, msdu);
304
Kalle Valo5e3dd152013-06-12 20:52:10 +0300305 rx_desc = (struct htt_rx_desc *)msdu->data;
306
307 /* FIXME: we must report msdu payload since this is what caller
Marcin Rokickid6dfe25c2017-02-20 14:39:57 +0100308 * expects now
309 */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300310 skb_put(msdu, offsetof(struct htt_rx_desc, msdu_payload));
311 skb_pull(msdu, offsetof(struct htt_rx_desc, msdu_payload));
312
313 /*
314 * Sanity check - confirm the HW is finished filling in the
315 * rx data.
316 * If the HW and SW are working correctly, then it's guaranteed
317 * that the HW's MAC DMA is done before this point in the SW.
318 * To prevent the case that we handle a stale Rx descriptor,
319 * just assert for now until we have a way to recover.
320 */
321 if (!(__le32_to_cpu(rx_desc->attention.flags)
322 & RX_ATTENTION_FLAGS_MSDU_DONE)) {
Michal Kazior9aa505d2014-11-18 09:24:47 +0200323 __skb_queue_purge(amsdu);
Michal Kaziore0bd7512014-11-18 09:24:48 +0200324 return -EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300325 }
326
Kalle Valo5e3dd152013-06-12 20:52:10 +0300327 msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
328 & (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
329 RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
Peter Oh1f5dbfb2015-07-15 19:01:21 -0700330 msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.common.info0),
Kalle Valo5e3dd152013-06-12 20:52:10 +0300331 RX_MSDU_START_INFO0_MSDU_LENGTH);
332 msdu_chained = rx_desc->frag_info.ring2_more_count;
333
334 if (msdu_len_invalid)
335 msdu_len = 0;
336
337 skb_trim(msdu, 0);
338 skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
339 msdu_len -= msdu->len;
340
Michal Kazior9aa505d2014-11-18 09:24:47 +0200341 /* Note: Chained buffers do not contain rx descriptor */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300342 while (msdu_chained--) {
Michal Kazior9aa505d2014-11-18 09:24:47 +0200343 msdu = ath10k_htt_rx_netbuf_pop(htt);
344 if (!msdu) {
Michal Kazior9aa505d2014-11-18 09:24:47 +0200345 __skb_queue_purge(amsdu);
Michal Kaziore0bd7512014-11-18 09:24:48 +0200346 return -ENOENT;
Michal Kaziorb30595a2014-10-23 17:04:24 +0300347 }
348
Michal Kazior9aa505d2014-11-18 09:24:47 +0200349 __skb_queue_tail(amsdu, msdu);
350 skb_trim(msdu, 0);
351 skb_put(msdu, min(msdu_len, HTT_RX_BUF_SIZE));
352 msdu_len -= msdu->len;
Michal Kaziorede9c8e2014-05-14 16:23:31 +0300353 msdu_chaining = 1;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300354 }
355
Peter Oh1f5dbfb2015-07-15 19:01:21 -0700356 last_msdu = __le32_to_cpu(rx_desc->msdu_end.common.info0) &
Kalle Valo5e3dd152013-06-12 20:52:10 +0300357 RX_MSDU_END_INFO0_LAST_MSDU;
358
Michal Kaziorb04e2042014-10-23 17:04:27 +0300359 trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
Rajkumar Manoharana0883cf2014-10-03 08:02:47 +0300360 sizeof(*rx_desc) - sizeof(u32));
Michal Kazior9aa505d2014-11-18 09:24:47 +0200361
362 if (last_msdu)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300363 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300364 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300365
Michal Kazior9aa505d2014-11-18 09:24:47 +0200366 if (skb_queue_empty(amsdu))
Janusz Dziedzicd84dd602014-03-24 21:23:20 +0100367 msdu_chaining = -1;
368
Kalle Valo5e3dd152013-06-12 20:52:10 +0300369 /*
370 * Don't refill the ring yet.
371 *
372 * First, the elements popped here are still in use - it is not
373 * safe to overwrite them until the matching call to
374 * mpdu_desc_list_next. Second, for efficiency it is preferable to
375 * refill the rx ring with 1 PPDU's worth of rx buffers (something
376 * like 32 x 3 buffers), rather than one MPDU's worth of rx buffers
377 * (something like 3 buffers). Consequently, we'll rely on the txrx
378 * SW to tell us when it is done pulling all the PPDU's rx buffers
379 * out of the rx ring, and then refill it just once.
380 */
381
382 return msdu_chaining;
383}
384
Michal Kaziorc5450702015-01-24 12:14:48 +0200385static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
386 u32 paddr)
387{
388 struct ath10k *ar = htt->ar;
389 struct ath10k_skb_rxcb *rxcb;
390 struct sk_buff *msdu;
391
392 lockdep_assert_held(&htt->rx_ring.lock);
393
394 msdu = ath10k_htt_rx_find_skb_paddr(ar, paddr);
395 if (!msdu)
396 return NULL;
397
398 rxcb = ATH10K_SKB_RXCB(msdu);
399 hash_del(&rxcb->hlist);
400 htt->rx_ring.fill_cnt--;
401
402 dma_unmap_single(htt->ar->dev, rxcb->paddr,
403 msdu->len + skb_tailroom(msdu),
404 DMA_FROM_DEVICE);
405 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
406 msdu->data, msdu->len + skb_tailroom(msdu));
407
408 return msdu;
409}
410
411static int ath10k_htt_rx_pop_paddr_list(struct ath10k_htt *htt,
412 struct htt_rx_in_ord_ind *ev,
413 struct sk_buff_head *list)
414{
415 struct ath10k *ar = htt->ar;
416 struct htt_rx_in_ord_msdu_desc *msdu_desc = ev->msdu_descs;
417 struct htt_rx_desc *rxd;
418 struct sk_buff *msdu;
419 int msdu_count;
420 bool is_offload;
421 u32 paddr;
422
423 lockdep_assert_held(&htt->rx_ring.lock);
424
425 msdu_count = __le16_to_cpu(ev->msdu_count);
426 is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
427
428 while (msdu_count--) {
429 paddr = __le32_to_cpu(msdu_desc->msdu_paddr);
430
431 msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
432 if (!msdu) {
433 __skb_queue_purge(list);
434 return -ENOENT;
435 }
436
437 __skb_queue_tail(list, msdu);
438
439 if (!is_offload) {
440 rxd = (void *)msdu->data;
441
442 trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
443
444 skb_put(msdu, sizeof(*rxd));
445 skb_pull(msdu, sizeof(*rxd));
446 skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
447
448 if (!(__le32_to_cpu(rxd->attention.flags) &
449 RX_ATTENTION_FLAGS_MSDU_DONE)) {
450 ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
451 return -EIO;
452 }
453 }
454
455 msdu_desc++;
456 }
457
458 return 0;
459}
460
Michal Kazior95bf21f2014-05-16 17:15:39 +0300461int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300462{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200463 struct ath10k *ar = htt->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300464 dma_addr_t paddr;
465 void *vaddr;
Kalle Valobd8bdbb2014-09-14 12:50:00 +0300466 size_t size;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300467 struct timer_list *timer = &htt->rx_ring.refill_retry_timer;
468
Michal Kazior51fc7d72014-10-23 17:04:24 +0300469 htt->rx_confused = false;
470
Michal Kaziorfe2407a2014-11-27 11:12:43 +0100471 /* XXX: The fill level could be changed during runtime in response to
472 * the host processing latency. Is this really worth it?
473 */
474 htt->rx_ring.size = HTT_RX_RING_SIZE;
475 htt->rx_ring.size_mask = htt->rx_ring.size - 1;
476 htt->rx_ring.fill_level = HTT_RX_RING_FILL_LEVEL;
477
Kalle Valo5e3dd152013-06-12 20:52:10 +0300478 if (!is_power_of_2(htt->rx_ring.size)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200479 ath10k_warn(ar, "htt rx ring size is not power of 2\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300480 return -EINVAL;
481 }
482
Kalle Valo5e3dd152013-06-12 20:52:10 +0300483 htt->rx_ring.netbufs_ring =
Michal Kazior3e841fd2014-05-14 16:23:31 +0300484 kzalloc(htt->rx_ring.size * sizeof(struct sk_buff *),
Kalle Valo5e3dd152013-06-12 20:52:10 +0300485 GFP_KERNEL);
486 if (!htt->rx_ring.netbufs_ring)
487 goto err_netbuf;
488
Kalle Valobd8bdbb2014-09-14 12:50:00 +0300489 size = htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring);
490
Felix Fietkaud6cb23b52015-11-24 11:36:52 +0100491 vaddr = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_KERNEL);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300492 if (!vaddr)
493 goto err_dma_ring;
494
495 htt->rx_ring.paddrs_ring = vaddr;
496 htt->rx_ring.base_paddr = paddr;
497
498 vaddr = dma_alloc_coherent(htt->ar->dev,
499 sizeof(*htt->rx_ring.alloc_idx.vaddr),
Felix Fietkaud6cb23b52015-11-24 11:36:52 +0100500 &paddr, GFP_KERNEL);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300501 if (!vaddr)
502 goto err_dma_idx;
503
504 htt->rx_ring.alloc_idx.vaddr = vaddr;
505 htt->rx_ring.alloc_idx.paddr = paddr;
Michal Kaziorc5450702015-01-24 12:14:48 +0200506 htt->rx_ring.sw_rd_idx.msdu_payld = htt->rx_ring.size_mask;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300507 *htt->rx_ring.alloc_idx.vaddr = 0;
508
509 /* Initialize the Rx refill retry timer */
Kees Cook7ac76762017-10-24 02:29:54 -0700510 timer_setup(timer, ath10k_htt_rx_ring_refill_retry, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300511
512 spin_lock_init(&htt->rx_ring.lock);
513
514 htt->rx_ring.fill_cnt = 0;
Michal Kaziorc5450702015-01-24 12:14:48 +0200515 htt->rx_ring.sw_rd_idx.msdu_payld = 0;
516 hash_init(htt->rx_ring.skb_table);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300517
Michal Kazior6c5151a2014-02-27 18:50:04 +0200518 skb_queue_head_init(&htt->rx_compl_q);
Michal Kaziorc5450702015-01-24 12:14:48 +0200519 skb_queue_head_init(&htt->rx_in_ord_compl_q);
Michal Kazior426e10e2016-03-06 16:14:43 +0200520 skb_queue_head_init(&htt->tx_fetch_ind_q);
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +0530521 atomic_set(&htt->num_mpdus_ready, 0);
Michal Kazior6c5151a2014-02-27 18:50:04 +0200522
Michal Kazior7aa7a722014-08-25 12:09:38 +0200523 ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300524 htt->rx_ring.size, htt->rx_ring.fill_level);
525 return 0;
526
Kalle Valo5e3dd152013-06-12 20:52:10 +0300527err_dma_idx:
528 dma_free_coherent(htt->ar->dev,
529 (htt->rx_ring.size *
530 sizeof(htt->rx_ring.paddrs_ring)),
531 htt->rx_ring.paddrs_ring,
532 htt->rx_ring.base_paddr);
533err_dma_ring:
534 kfree(htt->rx_ring.netbufs_ring);
535err_netbuf:
536 return -ENOMEM;
537}
538
Michal Kazior7aa7a722014-08-25 12:09:38 +0200539static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
540 enum htt_rx_mpdu_encrypt_type type)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300541{
542 switch (type) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300543 case HTT_RX_MPDU_ENCRYPT_NONE:
544 return 0;
Michal Kazior890d3b22014-10-23 17:04:22 +0300545 case HTT_RX_MPDU_ENCRYPT_WEP40:
546 case HTT_RX_MPDU_ENCRYPT_WEP104:
547 return IEEE80211_WEP_IV_LEN;
548 case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
549 case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
550 return IEEE80211_TKIP_IV_LEN;
551 case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
552 return IEEE80211_CCMP_HDR_LEN;
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +0300553 case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
554 return IEEE80211_CCMP_256_HDR_LEN;
555 case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
556 case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
557 return IEEE80211_GCMP_HDR_LEN;
Michal Kazior890d3b22014-10-23 17:04:22 +0300558 case HTT_RX_MPDU_ENCRYPT_WEP128:
559 case HTT_RX_MPDU_ENCRYPT_WAPI:
560 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300561 }
562
Michal Kazior890d3b22014-10-23 17:04:22 +0300563 ath10k_warn(ar, "unsupported encryption type %d\n", type);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300564 return 0;
565}
566
Michal Kazior890d3b22014-10-23 17:04:22 +0300567#define MICHAEL_MIC_LEN 8
568
Michal Kazior7aa7a722014-08-25 12:09:38 +0200569static int ath10k_htt_rx_crypto_tail_len(struct ath10k *ar,
570 enum htt_rx_mpdu_encrypt_type type)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300571{
572 switch (type) {
573 case HTT_RX_MPDU_ENCRYPT_NONE:
Michal Kazior890d3b22014-10-23 17:04:22 +0300574 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300575 case HTT_RX_MPDU_ENCRYPT_WEP40:
576 case HTT_RX_MPDU_ENCRYPT_WEP104:
Michal Kazior890d3b22014-10-23 17:04:22 +0300577 return IEEE80211_WEP_ICV_LEN;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300578 case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
579 case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
Michal Kazior890d3b22014-10-23 17:04:22 +0300580 return IEEE80211_TKIP_ICV_LEN;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300581 case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
Michal Kazior890d3b22014-10-23 17:04:22 +0300582 return IEEE80211_CCMP_MIC_LEN;
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +0300583 case HTT_RX_MPDU_ENCRYPT_AES_CCM256_WPA2:
584 return IEEE80211_CCMP_256_MIC_LEN;
585 case HTT_RX_MPDU_ENCRYPT_AES_GCMP_WPA2:
586 case HTT_RX_MPDU_ENCRYPT_AES_GCMP256_WPA2:
587 return IEEE80211_GCMP_MIC_LEN;
Michal Kazior890d3b22014-10-23 17:04:22 +0300588 case HTT_RX_MPDU_ENCRYPT_WEP128:
589 case HTT_RX_MPDU_ENCRYPT_WAPI:
590 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300591 }
592
Michal Kazior890d3b22014-10-23 17:04:22 +0300593 ath10k_warn(ar, "unsupported encryption type %d\n", type);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300594 return 0;
595}
596
Michal Kaziorf6dc2092013-09-26 10:12:22 +0300597struct amsdu_subframe_hdr {
598 u8 dst[ETH_ALEN];
599 u8 src[ETH_ALEN];
600 __be16 len;
601} __packed;
602
Michal Kazior6986fdd2015-08-27 14:47:33 +0200603#define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)
604
Janusz Dziedzic87326c92014-03-24 21:23:19 +0100605static void ath10k_htt_rx_h_rates(struct ath10k *ar,
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200606 struct ieee80211_rx_status *status,
607 struct htt_rx_desc *rxd)
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100608{
Michal Kazior5528e032015-03-30 09:51:56 +0300609 struct ieee80211_supported_band *sband;
610 u8 cck, rate, bw, sgi, mcs, nss;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100611 u8 preamble = 0;
Michal Kazior6986fdd2015-08-27 14:47:33 +0200612 u8 group_id;
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200613 u32 info1, info2, info3;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100614
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200615 info1 = __le32_to_cpu(rxd->ppdu_start.info1);
616 info2 = __le32_to_cpu(rxd->ppdu_start.info2);
617 info3 = __le32_to_cpu(rxd->ppdu_start.info3);
618
619 preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100620
621 switch (preamble) {
622 case HTT_RX_LEGACY:
Michal Kazior5528e032015-03-30 09:51:56 +0300623 /* To get legacy rate index band is required. Since band can't
624 * be undefined check if freq is non-zero.
625 */
626 if (!status->freq)
627 return;
628
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200629 cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
630 rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
Michal Kazior5528e032015-03-30 09:51:56 +0300631 rate &= ~RX_PPDU_START_RATE_FLAG;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100632
Michal Kazior5528e032015-03-30 09:51:56 +0300633 sband = &ar->mac.sbands[status->band];
Yanbo Li4b7f3532015-11-12 10:36:10 -0800634 status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate, cck);
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100635 break;
636 case HTT_RX_HT:
637 case HTT_RX_HT_WITH_TXBF:
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200638 /* HT-SIG - Table 20-11 in info2 and info3 */
639 mcs = info2 & 0x1F;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100640 nss = mcs >> 3;
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200641 bw = (info2 >> 7) & 1;
642 sgi = (info3 >> 7) & 1;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100643
644 status->rate_idx = mcs;
Johannes Bergda6a4352017-04-26 12:14:59 +0200645 status->encoding = RX_ENC_HT;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100646 if (sgi)
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200647 status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100648 if (bw)
Johannes Bergda6a4352017-04-26 12:14:59 +0200649 status->bw = RATE_INFO_BW_40;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100650 break;
651 case HTT_RX_VHT:
652 case HTT_RX_VHT_WITH_TXBF:
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200653 /* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
Marcin Rokickid6dfe25c2017-02-20 14:39:57 +0100654 * TODO check this
655 */
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200656 bw = info2 & 3;
657 sgi = info3 & 1;
Michal Kazior6986fdd2015-08-27 14:47:33 +0200658 group_id = (info2 >> 4) & 0x3F;
659
660 if (GROUP_ID_IS_SU_MIMO(group_id)) {
661 mcs = (info3 >> 4) & 0x0F;
662 nss = ((info2 >> 10) & 0x07) + 1;
663 } else {
664 /* Hardware doesn't decode VHT-SIG-B into Rx descriptor
665 * so it's impossible to decode MCS. Also since
666 * firmware consumes Group Id Management frames host
667 * has no knowledge regarding group/user position
668 * mapping so it's impossible to pick the correct Nsts
669 * from VHT-SIG-A1.
670 *
671 * Bandwidth and SGI are valid so report the rateinfo
672 * on best-effort basis.
673 */
674 mcs = 0;
675 nss = 1;
676 }
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100677
Manikanta Pubbisetty6ccea102015-09-02 17:05:27 +0300678 if (mcs > 0x09) {
679 ath10k_warn(ar, "invalid MCS received %u\n", mcs);
680 ath10k_warn(ar, "rxd %08x mpdu start %08x %08x msdu start %08x %08x ppdu start %08x %08x %08x %08x %08x\n",
681 __le32_to_cpu(rxd->attention.flags),
682 __le32_to_cpu(rxd->mpdu_start.info0),
683 __le32_to_cpu(rxd->mpdu_start.info1),
684 __le32_to_cpu(rxd->msdu_start.common.info0),
685 __le32_to_cpu(rxd->msdu_start.common.info1),
686 rxd->ppdu_start.info0,
687 __le32_to_cpu(rxd->ppdu_start.info1),
688 __le32_to_cpu(rxd->ppdu_start.info2),
689 __le32_to_cpu(rxd->ppdu_start.info3),
690 __le32_to_cpu(rxd->ppdu_start.info4));
691
692 ath10k_warn(ar, "msdu end %08x mpdu end %08x\n",
693 __le32_to_cpu(rxd->msdu_end.common.info0),
694 __le32_to_cpu(rxd->mpdu_end.info0));
695
696 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL,
697 "rx desc msdu payload: ",
698 rxd->msdu_payload, 50);
699 }
700
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100701 status->rate_idx = mcs;
Johannes Berg8613c942017-04-26 13:51:41 +0200702 status->nss = nss;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100703
704 if (sgi)
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200705 status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100706
707 switch (bw) {
708 /* 20MHZ */
709 case 0:
710 break;
711 /* 40MHZ */
712 case 1:
Johannes Bergda6a4352017-04-26 12:14:59 +0200713 status->bw = RATE_INFO_BW_40;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100714 break;
715 /* 80MHZ */
716 case 2:
Johannes Bergda6a4352017-04-26 12:14:59 +0200717 status->bw = RATE_INFO_BW_80;
Sebastian Gottschallbc1efd72017-01-12 13:02:12 +0200718 break;
719 case 3:
Johannes Bergda6a4352017-04-26 12:14:59 +0200720 status->bw = RATE_INFO_BW_160;
Sebastian Gottschallbc1efd72017-01-12 13:02:12 +0200721 break;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100722 }
723
Johannes Bergda6a4352017-04-26 12:14:59 +0200724 status->encoding = RX_ENC_VHT;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100725 break;
726 default:
727 break;
728 }
729}
730
Michal Kazior500ff9f2015-03-31 10:26:21 +0000731static struct ieee80211_channel *
732ath10k_htt_rx_h_peer_channel(struct ath10k *ar, struct htt_rx_desc *rxd)
733{
734 struct ath10k_peer *peer;
735 struct ath10k_vif *arvif;
736 struct cfg80211_chan_def def;
737 u16 peer_id;
738
739 lockdep_assert_held(&ar->data_lock);
740
741 if (!rxd)
742 return NULL;
743
744 if (rxd->attention.flags &
745 __cpu_to_le32(RX_ATTENTION_FLAGS_PEER_IDX_INVALID))
746 return NULL;
747
Peter Oh1f5dbfb2015-07-15 19:01:21 -0700748 if (!(rxd->msdu_end.common.info0 &
Michal Kazior500ff9f2015-03-31 10:26:21 +0000749 __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU)))
750 return NULL;
751
752 peer_id = MS(__le32_to_cpu(rxd->mpdu_start.info0),
753 RX_MPDU_START_INFO0_PEER_IDX);
754
755 peer = ath10k_peer_find_by_id(ar, peer_id);
756 if (!peer)
757 return NULL;
758
759 arvif = ath10k_get_arvif(ar, peer->vdev_id);
760 if (WARN_ON_ONCE(!arvif))
761 return NULL;
762
Mohammed Shafi Shajakhan569fba22016-06-29 19:29:24 +0300763 if (ath10k_mac_vif_chan(arvif->vif, &def))
Michal Kazior500ff9f2015-03-31 10:26:21 +0000764 return NULL;
765
766 return def.chan;
767}
768
769static struct ieee80211_channel *
770ath10k_htt_rx_h_vdev_channel(struct ath10k *ar, u32 vdev_id)
771{
772 struct ath10k_vif *arvif;
773 struct cfg80211_chan_def def;
774
775 lockdep_assert_held(&ar->data_lock);
776
777 list_for_each_entry(arvif, &ar->arvifs, list) {
778 if (arvif->vdev_id == vdev_id &&
779 ath10k_mac_vif_chan(arvif->vif, &def) == 0)
780 return def.chan;
781 }
782
783 return NULL;
784}
785
786static void
787ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw *hw,
788 struct ieee80211_chanctx_conf *conf,
789 void *data)
790{
791 struct cfg80211_chan_def *def = data;
792
793 *def = conf->def;
794}
795
796static struct ieee80211_channel *
797ath10k_htt_rx_h_any_channel(struct ath10k *ar)
798{
799 struct cfg80211_chan_def def = {};
800
801 ieee80211_iter_chan_contexts_atomic(ar->hw,
802 ath10k_htt_rx_h_any_chan_iter,
803 &def);
804
805 return def.chan;
806}
807
Janusz Dziedzic36653f052014-03-24 21:23:18 +0100808static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
Michal Kazior500ff9f2015-03-31 10:26:21 +0000809 struct ieee80211_rx_status *status,
810 struct htt_rx_desc *rxd,
811 u32 vdev_id)
Janusz Dziedzic36653f052014-03-24 21:23:18 +0100812{
813 struct ieee80211_channel *ch;
814
815 spin_lock_bh(&ar->data_lock);
816 ch = ar->scan_channel;
817 if (!ch)
818 ch = ar->rx_channel;
Michal Kazior500ff9f2015-03-31 10:26:21 +0000819 if (!ch)
820 ch = ath10k_htt_rx_h_peer_channel(ar, rxd);
821 if (!ch)
822 ch = ath10k_htt_rx_h_vdev_channel(ar, vdev_id);
823 if (!ch)
824 ch = ath10k_htt_rx_h_any_channel(ar);
Rajkumar Manoharan2ce9b252016-03-08 22:57:23 +0530825 if (!ch)
826 ch = ar->tgt_oper_chan;
Janusz Dziedzic36653f052014-03-24 21:23:18 +0100827 spin_unlock_bh(&ar->data_lock);
828
829 if (!ch)
830 return false;
831
832 status->band = ch->band;
833 status->freq = ch->center_freq;
834
835 return true;
836}
837
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200838static void ath10k_htt_rx_h_signal(struct ath10k *ar,
839 struct ieee80211_rx_status *status,
840 struct htt_rx_desc *rxd)
841{
Norik Dzhandzhapanyan82412532017-06-12 18:03:34 +0300842 int i;
843
844 for (i = 0; i < IEEE80211_MAX_CHAINS ; i++) {
845 status->chains &= ~BIT(i);
846
847 if (rxd->ppdu_start.rssi_chains[i].pri20_mhz != 0x80) {
848 status->chain_signal[i] = ATH10K_DEFAULT_NOISE_FLOOR +
849 rxd->ppdu_start.rssi_chains[i].pri20_mhz;
850
851 status->chains |= BIT(i);
852 }
853 }
854
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200855 /* FIXME: Get real NF */
856 status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
857 rxd->ppdu_start.rssi_comb;
858 status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
859}
860
861static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
862 struct ieee80211_rx_status *status,
863 struct htt_rx_desc *rxd)
864{
865 /* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
866 * means all prior MSDUs in a PPDU are reported to mac80211 without the
867 * TSF. Is it worth holding frames until end of PPDU is known?
868 *
869 * FIXME: Can we get/compute 64bit TSF?
870 */
Michal Kazior3ec79e32015-01-24 12:14:48 +0200871 status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200872 status->flag |= RX_FLAG_MACTIME_END;
873}
874
875static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
876 struct sk_buff_head *amsdu,
Michal Kazior500ff9f2015-03-31 10:26:21 +0000877 struct ieee80211_rx_status *status,
878 u32 vdev_id)
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200879{
880 struct sk_buff *first;
881 struct htt_rx_desc *rxd;
882 bool is_first_ppdu;
883 bool is_last_ppdu;
884
885 if (skb_queue_empty(amsdu))
886 return;
887
888 first = skb_peek(amsdu);
889 rxd = (void *)first->data - sizeof(*rxd);
890
891 is_first_ppdu = !!(rxd->attention.flags &
892 __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
893 is_last_ppdu = !!(rxd->attention.flags &
894 __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));
895
896 if (is_first_ppdu) {
897 /* New PPDU starts so clear out the old per-PPDU status. */
898 status->freq = 0;
899 status->rate_idx = 0;
Johannes Berg8613c942017-04-26 13:51:41 +0200900 status->nss = 0;
Johannes Bergda6a4352017-04-26 12:14:59 +0200901 status->encoding = RX_ENC_LEGACY;
902 status->bw = RATE_INFO_BW_20;
Matthias Frei47cc0ca2017-07-28 15:15:36 +0300903
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200904 status->flag &= ~RX_FLAG_MACTIME_END;
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200905 status->flag |= RX_FLAG_NO_SIGNAL_VAL;
906
Matthias Frei47cc0ca2017-07-28 15:15:36 +0300907 status->flag &= ~(RX_FLAG_AMPDU_IS_LAST);
908 status->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
909 status->ampdu_reference = ar->ampdu_reference;
910
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200911 ath10k_htt_rx_h_signal(ar, status, rxd);
Michal Kazior500ff9f2015-03-31 10:26:21 +0000912 ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200913 ath10k_htt_rx_h_rates(ar, status, rxd);
914 }
915
Matthias Frei47cc0ca2017-07-28 15:15:36 +0300916 if (is_last_ppdu) {
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200917 ath10k_htt_rx_h_mactime(ar, status, rxd);
Matthias Frei47cc0ca2017-07-28 15:15:36 +0300918
919 /* set ampdu last segment flag */
920 status->flag |= RX_FLAG_AMPDU_IS_LAST;
921 ar->ampdu_reference++;
922 }
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200923}
924
Janusz Dziedzic76f53292014-07-28 23:59:43 +0300925static const char * const tid_to_ac[] = {
926 "BE",
927 "BK",
928 "BK",
929 "BE",
930 "VI",
931 "VI",
932 "VO",
933 "VO",
934};
935
936static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
937{
938 u8 *qc;
939 int tid;
940
941 if (!ieee80211_is_data_qos(hdr->frame_control))
942 return "";
943
944 qc = ieee80211_get_qos_ctl(hdr);
945 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
946 if (tid < 8)
947 snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
948 else
949 snprintf(out, size, "tid %d", tid);
950
951 return out;
952}
953
Janusz Dziedzic85f6d7c2014-03-24 21:23:22 +0100954static void ath10k_process_rx(struct ath10k *ar,
955 struct ieee80211_rx_status *rx_status,
956 struct sk_buff *skb)
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100957{
958 struct ieee80211_rx_status *status;
Janusz Dziedzic76f53292014-07-28 23:59:43 +0300959 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
960 char tid[32];
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100961
Janusz Dziedzic85f6d7c2014-03-24 21:23:22 +0100962 status = IEEE80211_SKB_RXCB(skb);
963 *status = *rx_status;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100964
Michal Kazior7aa7a722014-08-25 12:09:38 +0200965 ath10k_dbg(ar, ATH10K_DBG_DATA,
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200966 "rx skb %pK len %u peer %pM %s %s sn %u %s%s%s%s%s%s %srate_idx %u vht_nss %u freq %u band %u flag 0x%x fcs-err %i mic-err %i amsdu-more %i\n",
Janusz Dziedzic85f6d7c2014-03-24 21:23:22 +0100967 skb,
968 skb->len,
Janusz Dziedzic76f53292014-07-28 23:59:43 +0300969 ieee80211_get_SA(hdr),
970 ath10k_get_tid(hdr, tid, sizeof(tid)),
971 is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
972 "mcast" : "ucast",
973 (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
Johannes Bergda6a4352017-04-26 12:14:59 +0200974 (status->encoding == RX_ENC_LEGACY) ? "legacy" : "",
975 (status->encoding == RX_ENC_HT) ? "ht" : "",
976 (status->encoding == RX_ENC_VHT) ? "vht" : "",
977 (status->bw == RATE_INFO_BW_40) ? "40" : "",
978 (status->bw == RATE_INFO_BW_80) ? "80" : "",
979 (status->bw == RATE_INFO_BW_160) ? "160" : "",
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200980 status->enc_flags & RX_ENC_FLAG_SHORT_GI ? "sgi " : "",
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100981 status->rate_idx,
Johannes Berg8613c942017-04-26 13:51:41 +0200982 status->nss,
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100983 status->freq,
Janusz Dziedzic87326c92014-03-24 21:23:19 +0100984 status->band, status->flag,
Janusz Dziedzic78433f92014-03-24 21:23:21 +0100985 !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
Janusz Dziedzic76f53292014-07-28 23:59:43 +0300986 !!(status->flag & RX_FLAG_MMIC_ERROR),
987 !!(status->flag & RX_FLAG_AMSDU_MORE));
Michal Kazior7aa7a722014-08-25 12:09:38 +0200988 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
Janusz Dziedzic85f6d7c2014-03-24 21:23:22 +0100989 skb->data, skb->len);
Rajkumar Manoharan5ce8e7f2014-11-05 19:14:31 +0530990 trace_ath10k_rx_hdr(ar, skb->data, skb->len);
991 trace_ath10k_rx_payload(ar, skb->data, skb->len);
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100992
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +0300993 ieee80211_rx_napi(ar->hw, NULL, skb, &ar->napi);
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100994}
995
Michal Kazior48f4ca32015-05-19 14:09:34 +0200996static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
997 struct ieee80211_hdr *hdr)
Michal Kaziord960c362014-02-25 09:29:57 +0200998{
Michal Kazior48f4ca32015-05-19 14:09:34 +0200999 int len = ieee80211_hdrlen(hdr->frame_control);
1000
1001 if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
Kalle Valoc4cdf752016-04-20 19:45:18 +03001002 ar->running_fw->fw_file.fw_features))
Michal Kazior48f4ca32015-05-19 14:09:34 +02001003 len = round_up(len, 4);
1004
1005 return len;
Michal Kaziord960c362014-02-25 09:29:57 +02001006}
1007
Michal Kazior581c25f2014-11-18 09:24:48 +02001008static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
1009 struct sk_buff *msdu,
1010 struct ieee80211_rx_status *status,
1011 enum htt_rx_mpdu_encrypt_type enctype,
1012 bool is_decrypted)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001013{
Michal Kaziorf6dc2092013-09-26 10:12:22 +03001014 struct ieee80211_hdr *hdr;
Michal Kazior581c25f2014-11-18 09:24:48 +02001015 struct htt_rx_desc *rxd;
1016 size_t hdr_len;
1017 size_t crypto_len;
1018 bool is_first;
1019 bool is_last;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001020
Michal Kazior581c25f2014-11-18 09:24:48 +02001021 rxd = (void *)msdu->data - sizeof(*rxd);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001022 is_first = !!(rxd->msdu_end.common.info0 &
Michal Kazior581c25f2014-11-18 09:24:48 +02001023 __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001024 is_last = !!(rxd->msdu_end.common.info0 &
Michal Kazior581c25f2014-11-18 09:24:48 +02001025 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
Michal Kazior9aa505d2014-11-18 09:24:47 +02001026
Michal Kazior581c25f2014-11-18 09:24:48 +02001027 /* Delivered decapped frame:
1028 * [802.11 header]
1029 * [crypto param] <-- can be trimmed if !fcs_err &&
1030 * !decrypt_err && !peer_idx_invalid
1031 * [amsdu header] <-- only if A-MSDU
1032 * [rfc1042/llc]
1033 * [payload]
1034 * [FCS] <-- at end, needs to be trimmed
1035 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001036
Michal Kazior581c25f2014-11-18 09:24:48 +02001037 /* This probably shouldn't happen but warn just in case */
1038 if (unlikely(WARN_ON_ONCE(!is_first)))
1039 return;
1040
1041 /* This probably shouldn't happen but warn just in case */
1042 if (unlikely(WARN_ON_ONCE(!(is_first && is_last))))
1043 return;
1044
1045 skb_trim(msdu, msdu->len - FCS_LEN);
1046
1047 /* In most cases this will be true for sniffed frames. It makes sense
David Liuccec9032015-07-24 20:25:32 +03001048 * to deliver them as-is without stripping the crypto param. This is
1049 * necessary for software based decryption.
Michal Kazior581c25f2014-11-18 09:24:48 +02001050 *
1051 * If there's no error then the frame is decrypted. At least that is
1052 * the case for frames that come in via fragmented rx indication.
1053 */
1054 if (!is_decrypted)
1055 return;
1056
1057 /* The payload is decrypted so strip crypto params. Start from tail
1058 * since hdr is used to compute some stuff.
1059 */
1060
1061 hdr = (void *)msdu->data;
1062
1063 /* Tail */
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001064 if (status->flag & RX_FLAG_IV_STRIPPED) {
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001065 skb_trim(msdu, msdu->len -
1066 ath10k_htt_rx_crypto_tail_len(ar, enctype));
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001067 } else {
1068 /* MIC */
1069 if ((status->flag & RX_FLAG_MIC_STRIPPED) &&
1070 enctype == HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2)
1071 skb_trim(msdu, msdu->len - 8);
1072
1073 /* ICV */
1074 if (status->flag & RX_FLAG_ICV_STRIPPED &&
1075 enctype != HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2)
1076 skb_trim(msdu, msdu->len -
1077 ath10k_htt_rx_crypto_tail_len(ar, enctype));
1078 }
Michal Kazior581c25f2014-11-18 09:24:48 +02001079
1080 /* MMIC */
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001081 if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
1082 !ieee80211_has_morefrags(hdr->frame_control) &&
Michal Kazior581c25f2014-11-18 09:24:48 +02001083 enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
1084 skb_trim(msdu, msdu->len - 8);
1085
1086 /* Head */
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001087 if (status->flag & RX_FLAG_IV_STRIPPED) {
1088 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1089 crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001090
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001091 memmove((void *)msdu->data + crypto_len,
1092 (void *)msdu->data, hdr_len);
1093 skb_pull(msdu, crypto_len);
1094 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001095}
1096
Michal Kazior581c25f2014-11-18 09:24:48 +02001097static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
1098 struct sk_buff *msdu,
1099 struct ieee80211_rx_status *status,
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001100 const u8 first_hdr[64],
1101 enum htt_rx_mpdu_encrypt_type enctype)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001102{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001103 struct ieee80211_hdr *hdr;
Vasanthakumar Thiagarajan9e19e132016-09-09 17:25:29 +03001104 struct htt_rx_desc *rxd;
Michal Kazior581c25f2014-11-18 09:24:48 +02001105 size_t hdr_len;
1106 u8 da[ETH_ALEN];
1107 u8 sa[ETH_ALEN];
Vasanthakumar Thiagarajan9e19e132016-09-09 17:25:29 +03001108 int l3_pad_bytes;
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001109 int bytes_aligned = ar->hw_params.decap_align_bytes;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001110
Michal Kazior581c25f2014-11-18 09:24:48 +02001111 /* Delivered decapped frame:
1112 * [nwifi 802.11 header] <-- replaced with 802.11 hdr
1113 * [rfc1042/llc]
1114 *
1115 * Note: The nwifi header doesn't have QoS Control and is
1116 * (always?) a 3addr frame.
1117 *
1118 * Note2: There's no A-MSDU subframe header. Even if it's part
1119 * of an A-MSDU.
1120 */
1121
1122 /* pull decapped header and copy SA & DA */
Vasanthakumar Thiagarajan9e19e132016-09-09 17:25:29 +03001123 rxd = (void *)msdu->data - sizeof(*rxd);
1124
1125 l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
1126 skb_put(msdu, l3_pad_bytes);
1127
1128 hdr = (struct ieee80211_hdr *)(msdu->data + l3_pad_bytes);
Yanbo Lib8d55fc2015-11-16 22:22:02 +02001129
Michal Kazior48f4ca32015-05-19 14:09:34 +02001130 hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
Michal Kazior581c25f2014-11-18 09:24:48 +02001131 ether_addr_copy(da, ieee80211_get_DA(hdr));
1132 ether_addr_copy(sa, ieee80211_get_SA(hdr));
1133 skb_pull(msdu, hdr_len);
1134
1135 /* push original 802.11 header */
1136 hdr = (struct ieee80211_hdr *)first_hdr;
Michal Kaziore3fbf8d2013-09-26 10:12:23 +03001137 hdr_len = ieee80211_hdrlen(hdr->frame_control);
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001138
1139 if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1140 memcpy(skb_push(msdu,
1141 ath10k_htt_rx_crypto_param_len(ar, enctype)),
1142 (void *)hdr + round_up(hdr_len, bytes_aligned),
1143 ath10k_htt_rx_crypto_param_len(ar, enctype));
1144 }
1145
Michal Kazior581c25f2014-11-18 09:24:48 +02001146 memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001147
Michal Kazior581c25f2014-11-18 09:24:48 +02001148 /* original 802.11 header has a different DA and in
1149 * case of 4addr it may also have different SA
1150 */
1151 hdr = (struct ieee80211_hdr *)msdu->data;
1152 ether_addr_copy(ieee80211_get_DA(hdr), da);
1153 ether_addr_copy(ieee80211_get_SA(hdr), sa);
1154}
Michal Kaziorf6dc2092013-09-26 10:12:22 +03001155
Michal Kazior581c25f2014-11-18 09:24:48 +02001156static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
1157 struct sk_buff *msdu,
1158 enum htt_rx_mpdu_encrypt_type enctype)
1159{
1160 struct ieee80211_hdr *hdr;
1161 struct htt_rx_desc *rxd;
1162 size_t hdr_len, crypto_len;
1163 void *rfc1042;
1164 bool is_first, is_last, is_amsdu;
Vasanthakumar Thiagarajan2f38c3c2016-09-26 21:56:24 +03001165 int bytes_aligned = ar->hw_params.decap_align_bytes;
Michal Kazior784f69d2013-09-26 10:12:23 +03001166
Michal Kazior581c25f2014-11-18 09:24:48 +02001167 rxd = (void *)msdu->data - sizeof(*rxd);
1168 hdr = (void *)rxd->rx_hdr_status;
1169
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001170 is_first = !!(rxd->msdu_end.common.info0 &
Michal Kazior581c25f2014-11-18 09:24:48 +02001171 __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001172 is_last = !!(rxd->msdu_end.common.info0 &
Michal Kazior581c25f2014-11-18 09:24:48 +02001173 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
1174 is_amsdu = !(is_first && is_last);
1175
1176 rfc1042 = hdr;
1177
1178 if (is_first) {
Michal Kazior784f69d2013-09-26 10:12:23 +03001179 hdr_len = ieee80211_hdrlen(hdr->frame_control);
Michal Kazior581c25f2014-11-18 09:24:48 +02001180 crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
Michal Kaziore3fbf8d2013-09-26 10:12:23 +03001181
Vasanthakumar Thiagarajan2f38c3c2016-09-26 21:56:24 +03001182 rfc1042 += round_up(hdr_len, bytes_aligned) +
1183 round_up(crypto_len, bytes_aligned);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001184 }
1185
Michal Kazior581c25f2014-11-18 09:24:48 +02001186 if (is_amsdu)
1187 rfc1042 += sizeof(struct amsdu_subframe_hdr);
Michal Kaziorf6dc2092013-09-26 10:12:22 +03001188
Michal Kazior581c25f2014-11-18 09:24:48 +02001189 return rfc1042;
1190}
1191
1192static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
1193 struct sk_buff *msdu,
1194 struct ieee80211_rx_status *status,
1195 const u8 first_hdr[64],
1196 enum htt_rx_mpdu_encrypt_type enctype)
1197{
1198 struct ieee80211_hdr *hdr;
1199 struct ethhdr *eth;
1200 size_t hdr_len;
1201 void *rfc1042;
1202 u8 da[ETH_ALEN];
1203 u8 sa[ETH_ALEN];
Vasanthakumar Thiagarajan9e19e132016-09-09 17:25:29 +03001204 int l3_pad_bytes;
1205 struct htt_rx_desc *rxd;
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001206 int bytes_aligned = ar->hw_params.decap_align_bytes;
Michal Kazior581c25f2014-11-18 09:24:48 +02001207
1208 /* Delivered decapped frame:
1209 * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
1210 * [payload]
1211 */
1212
1213 rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
1214 if (WARN_ON_ONCE(!rfc1042))
1215 return;
1216
Vasanthakumar Thiagarajan9e19e132016-09-09 17:25:29 +03001217 rxd = (void *)msdu->data - sizeof(*rxd);
1218 l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
1219 skb_put(msdu, l3_pad_bytes);
1220 skb_pull(msdu, l3_pad_bytes);
1221
Michal Kazior581c25f2014-11-18 09:24:48 +02001222 /* pull decapped header and copy SA & DA */
1223 eth = (struct ethhdr *)msdu->data;
1224 ether_addr_copy(da, eth->h_dest);
1225 ether_addr_copy(sa, eth->h_source);
1226 skb_pull(msdu, sizeof(struct ethhdr));
1227
1228 /* push rfc1042/llc/snap */
1229 memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
1230 sizeof(struct rfc1042_hdr));
1231
1232 /* push original 802.11 header */
1233 hdr = (struct ieee80211_hdr *)first_hdr;
1234 hdr_len = ieee80211_hdrlen(hdr->frame_control);
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001235
1236 if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1237 memcpy(skb_push(msdu,
1238 ath10k_htt_rx_crypto_param_len(ar, enctype)),
1239 (void *)hdr + round_up(hdr_len, bytes_aligned),
1240 ath10k_htt_rx_crypto_param_len(ar, enctype));
1241 }
1242
Michal Kazior581c25f2014-11-18 09:24:48 +02001243 memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1244
1245 /* original 802.11 header has a different DA and in
1246 * case of 4addr it may also have different SA
1247 */
1248 hdr = (struct ieee80211_hdr *)msdu->data;
1249 ether_addr_copy(ieee80211_get_DA(hdr), da);
1250 ether_addr_copy(ieee80211_get_SA(hdr), sa);
1251}
1252
1253static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
1254 struct sk_buff *msdu,
1255 struct ieee80211_rx_status *status,
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001256 const u8 first_hdr[64],
1257 enum htt_rx_mpdu_encrypt_type enctype)
Michal Kazior581c25f2014-11-18 09:24:48 +02001258{
1259 struct ieee80211_hdr *hdr;
1260 size_t hdr_len;
Vasanthakumar Thiagarajan9e19e132016-09-09 17:25:29 +03001261 int l3_pad_bytes;
1262 struct htt_rx_desc *rxd;
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001263 int bytes_aligned = ar->hw_params.decap_align_bytes;
Michal Kazior581c25f2014-11-18 09:24:48 +02001264
1265 /* Delivered decapped frame:
1266 * [amsdu header] <-- replaced with 802.11 hdr
1267 * [rfc1042/llc]
1268 * [payload]
1269 */
1270
Vasanthakumar Thiagarajan9e19e132016-09-09 17:25:29 +03001271 rxd = (void *)msdu->data - sizeof(*rxd);
1272 l3_pad_bytes = ath10k_rx_desc_get_l3_pad_bytes(&ar->hw_params, rxd);
1273
1274 skb_put(msdu, l3_pad_bytes);
1275 skb_pull(msdu, sizeof(struct amsdu_subframe_hdr) + l3_pad_bytes);
Michal Kazior581c25f2014-11-18 09:24:48 +02001276
1277 hdr = (struct ieee80211_hdr *)first_hdr;
1278 hdr_len = ieee80211_hdrlen(hdr->frame_control);
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001279
1280 if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1281 memcpy(skb_push(msdu,
1282 ath10k_htt_rx_crypto_param_len(ar, enctype)),
1283 (void *)hdr + round_up(hdr_len, bytes_aligned),
1284 ath10k_htt_rx_crypto_param_len(ar, enctype));
1285 }
1286
Michal Kazior581c25f2014-11-18 09:24:48 +02001287 memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1288}
1289
1290static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
1291 struct sk_buff *msdu,
1292 struct ieee80211_rx_status *status,
1293 u8 first_hdr[64],
1294 enum htt_rx_mpdu_encrypt_type enctype,
1295 bool is_decrypted)
1296{
1297 struct htt_rx_desc *rxd;
1298 enum rx_msdu_decap_format decap;
Michal Kazior581c25f2014-11-18 09:24:48 +02001299
1300 /* First msdu's decapped header:
1301 * [802.11 header] <-- padded to 4 bytes long
1302 * [crypto param] <-- padded to 4 bytes long
1303 * [amsdu header] <-- only if A-MSDU
1304 * [rfc1042/llc]
1305 *
1306 * Other (2nd, 3rd, ..) msdu's decapped header:
1307 * [amsdu header] <-- only if A-MSDU
1308 * [rfc1042/llc]
1309 */
1310
1311 rxd = (void *)msdu->data - sizeof(*rxd);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001312 decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
Michal Kazior581c25f2014-11-18 09:24:48 +02001313 RX_MSDU_START_INFO1_DECAP_FORMAT);
1314
1315 switch (decap) {
1316 case RX_MSDU_DECAP_RAW:
1317 ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
1318 is_decrypted);
1319 break;
1320 case RX_MSDU_DECAP_NATIVE_WIFI:
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001321 ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr,
1322 enctype);
Michal Kazior581c25f2014-11-18 09:24:48 +02001323 break;
1324 case RX_MSDU_DECAP_ETHERNET2_DIX:
1325 ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
1326 break;
1327 case RX_MSDU_DECAP_8023_SNAP_LLC:
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001328 ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr,
1329 enctype);
Michal Kazior581c25f2014-11-18 09:24:48 +02001330 break;
1331 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001332}
1333
Michal Kazior605f81a2013-07-31 10:47:56 +02001334static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
1335{
1336 struct htt_rx_desc *rxd;
1337 u32 flags, info;
1338 bool is_ip4, is_ip6;
1339 bool is_tcp, is_udp;
1340 bool ip_csum_ok, tcpudp_csum_ok;
1341
1342 rxd = (void *)skb->data - sizeof(*rxd);
1343 flags = __le32_to_cpu(rxd->attention.flags);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001344 info = __le32_to_cpu(rxd->msdu_start.common.info1);
Michal Kazior605f81a2013-07-31 10:47:56 +02001345
1346 is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
1347 is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
1348 is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
1349 is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
1350 ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
1351 tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
1352
1353 if (!is_ip4 && !is_ip6)
1354 return CHECKSUM_NONE;
1355 if (!is_tcp && !is_udp)
1356 return CHECKSUM_NONE;
1357 if (!ip_csum_ok)
1358 return CHECKSUM_NONE;
1359 if (!tcpudp_csum_ok)
1360 return CHECKSUM_NONE;
1361
1362 return CHECKSUM_UNNECESSARY;
1363}
1364
Michal Kazior581c25f2014-11-18 09:24:48 +02001365static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
1366{
1367 msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
1368}
1369
1370static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
1371 struct sk_buff_head *amsdu,
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001372 struct ieee80211_rx_status *status,
1373 bool fill_crypt_header)
Michal Kazior581c25f2014-11-18 09:24:48 +02001374{
1375 struct sk_buff *first;
1376 struct sk_buff *last;
1377 struct sk_buff *msdu;
1378 struct htt_rx_desc *rxd;
1379 struct ieee80211_hdr *hdr;
1380 enum htt_rx_mpdu_encrypt_type enctype;
1381 u8 first_hdr[64];
1382 u8 *qos;
Michal Kazior581c25f2014-11-18 09:24:48 +02001383 bool has_fcs_err;
1384 bool has_crypto_err;
1385 bool has_tkip_err;
1386 bool has_peer_idx_invalid;
1387 bool is_decrypted;
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001388 bool is_mgmt;
Michal Kazior581c25f2014-11-18 09:24:48 +02001389 u32 attention;
1390
1391 if (skb_queue_empty(amsdu))
1392 return;
1393
1394 first = skb_peek(amsdu);
1395 rxd = (void *)first->data - sizeof(*rxd);
1396
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001397 is_mgmt = !!(rxd->attention.flags &
1398 __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));
1399
Michal Kazior581c25f2014-11-18 09:24:48 +02001400 enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1401 RX_MPDU_START_INFO0_ENCRYPT_TYPE);
1402
1403 /* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
1404 * decapped header. It'll be used for undecapping of each MSDU.
1405 */
1406 hdr = (void *)rxd->rx_hdr_status;
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001407 memcpy(first_hdr, hdr, RX_HTT_HDR_STATUS_LEN);
Michal Kazior581c25f2014-11-18 09:24:48 +02001408
1409 /* Each A-MSDU subframe will use the original header as the base and be
1410 * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
1411 */
1412 hdr = (void *)first_hdr;
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001413
1414 if (ieee80211_is_data_qos(hdr->frame_control)) {
1415 qos = ieee80211_get_qos_ctl(hdr);
1416 qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1417 }
Michal Kazior581c25f2014-11-18 09:24:48 +02001418
1419 /* Some attention flags are valid only in the last MSDU. */
1420 last = skb_peek_tail(amsdu);
1421 rxd = (void *)last->data - sizeof(*rxd);
1422 attention = __le32_to_cpu(rxd->attention.flags);
1423
1424 has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
1425 has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
1426 has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
1427 has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);
1428
1429 /* Note: If hardware captures an encrypted frame that it can't decrypt,
1430 * e.g. due to fcs error, missing peer or invalid key data it will
1431 * report the frame as raw.
1432 */
1433 is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
1434 !has_fcs_err &&
1435 !has_crypto_err &&
1436 !has_peer_idx_invalid);
1437
1438 /* Clear per-MPDU flags while leaving per-PPDU flags intact. */
1439 status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
1440 RX_FLAG_MMIC_ERROR |
1441 RX_FLAG_DECRYPTED |
1442 RX_FLAG_IV_STRIPPED |
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001443 RX_FLAG_ONLY_MONITOR |
Michal Kazior581c25f2014-11-18 09:24:48 +02001444 RX_FLAG_MMIC_STRIPPED);
1445
1446 if (has_fcs_err)
1447 status->flag |= RX_FLAG_FAILED_FCS_CRC;
1448
1449 if (has_tkip_err)
1450 status->flag |= RX_FLAG_MMIC_ERROR;
1451
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001452 /* Firmware reports all necessary management frames via WMI already.
1453 * They are not reported to monitor interfaces at all so pass the ones
1454 * coming via HTT to monitor interfaces instead. This simplifies
1455 * matters a lot.
1456 */
1457 if (is_mgmt)
1458 status->flag |= RX_FLAG_ONLY_MONITOR;
1459
1460 if (is_decrypted) {
1461 status->flag |= RX_FLAG_DECRYPTED;
1462
1463 if (likely(!is_mgmt))
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001464 status->flag |= RX_FLAG_MMIC_STRIPPED;
1465
1466 if (fill_crypt_header)
1467 status->flag |= RX_FLAG_MIC_STRIPPED |
1468 RX_FLAG_ICV_STRIPPED;
1469 else
1470 status->flag |= RX_FLAG_IV_STRIPPED;
1471 }
Michal Kazior581c25f2014-11-18 09:24:48 +02001472
1473 skb_queue_walk(amsdu, msdu) {
1474 ath10k_htt_rx_h_csum_offload(msdu);
1475 ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
1476 is_decrypted);
1477
1478 /* Undecapping involves copying the original 802.11 header back
1479 * to sk_buff. If frame is protected and hardware has decrypted
1480 * it then remove the protected bit.
1481 */
1482 if (!is_decrypted)
1483 continue;
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001484 if (is_mgmt)
1485 continue;
Michal Kazior581c25f2014-11-18 09:24:48 +02001486
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001487 if (fill_crypt_header)
1488 continue;
1489
Michal Kazior581c25f2014-11-18 09:24:48 +02001490 hdr = (void *)msdu->data;
1491 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1492 }
1493}
1494
1495static void ath10k_htt_rx_h_deliver(struct ath10k *ar,
1496 struct sk_buff_head *amsdu,
1497 struct ieee80211_rx_status *status)
1498{
1499 struct sk_buff *msdu;
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001500 struct sk_buff *first_subframe;
1501
1502 first_subframe = skb_peek(amsdu);
Michal Kazior581c25f2014-11-18 09:24:48 +02001503
1504 while ((msdu = __skb_dequeue(amsdu))) {
1505 /* Setup per-MSDU flags */
1506 if (skb_queue_empty(amsdu))
1507 status->flag &= ~RX_FLAG_AMSDU_MORE;
1508 else
1509 status->flag |= RX_FLAG_AMSDU_MORE;
1510
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001511 if (msdu == first_subframe) {
1512 first_subframe = NULL;
1513 status->flag &= ~RX_FLAG_ALLOW_SAME_PN;
1514 } else {
1515 status->flag |= RX_FLAG_ALLOW_SAME_PN;
1516 }
1517
Michal Kazior581c25f2014-11-18 09:24:48 +02001518 ath10k_process_rx(ar, status, msdu);
1519 }
1520}
1521
Michal Kazior9aa505d2014-11-18 09:24:47 +02001522static int ath10k_unchain_msdu(struct sk_buff_head *amsdu)
Ben Greearbfa35362014-03-03 14:07:09 -08001523{
Michal Kazior9aa505d2014-11-18 09:24:47 +02001524 struct sk_buff *skb, *first;
Ben Greearbfa35362014-03-03 14:07:09 -08001525 int space;
1526 int total_len = 0;
1527
1528 /* TODO: Might could optimize this by using
1529 * skb_try_coalesce or similar method to
1530 * decrease copying, or maybe get mac80211 to
1531 * provide a way to just receive a list of
1532 * skb?
1533 */
1534
Michal Kazior9aa505d2014-11-18 09:24:47 +02001535 first = __skb_dequeue(amsdu);
Ben Greearbfa35362014-03-03 14:07:09 -08001536
1537 /* Allocate total length all at once. */
Michal Kazior9aa505d2014-11-18 09:24:47 +02001538 skb_queue_walk(amsdu, skb)
1539 total_len += skb->len;
Ben Greearbfa35362014-03-03 14:07:09 -08001540
Michal Kazior9aa505d2014-11-18 09:24:47 +02001541 space = total_len - skb_tailroom(first);
Ben Greearbfa35362014-03-03 14:07:09 -08001542 if ((space > 0) &&
Michal Kazior9aa505d2014-11-18 09:24:47 +02001543 (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
Ben Greearbfa35362014-03-03 14:07:09 -08001544 /* TODO: bump some rx-oom error stat */
1545 /* put it back together so we can free the
1546 * whole list at once.
1547 */
Michal Kazior9aa505d2014-11-18 09:24:47 +02001548 __skb_queue_head(amsdu, first);
Ben Greearbfa35362014-03-03 14:07:09 -08001549 return -1;
1550 }
1551
1552 /* Walk list again, copying contents into
1553 * msdu_head
1554 */
Michal Kazior9aa505d2014-11-18 09:24:47 +02001555 while ((skb = __skb_dequeue(amsdu))) {
1556 skb_copy_from_linear_data(skb, skb_put(first, skb->len),
1557 skb->len);
1558 dev_kfree_skb_any(skb);
Ben Greearbfa35362014-03-03 14:07:09 -08001559 }
1560
Michal Kazior9aa505d2014-11-18 09:24:47 +02001561 __skb_queue_head(amsdu, first);
Ben Greearbfa35362014-03-03 14:07:09 -08001562 return 0;
1563}
1564
Michal Kazior581c25f2014-11-18 09:24:48 +02001565static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
Mohammed Shafi Shajakhan7543d112016-10-04 21:19:59 +05301566 struct sk_buff_head *amsdu)
Janusz Dziedzic2acc4eb2014-03-19 07:09:40 +01001567{
Michal Kazior581c25f2014-11-18 09:24:48 +02001568 struct sk_buff *first;
1569 struct htt_rx_desc *rxd;
1570 enum rx_msdu_decap_format decap;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001571
Michal Kazior581c25f2014-11-18 09:24:48 +02001572 first = skb_peek(amsdu);
1573 rxd = (void *)first->data - sizeof(*rxd);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001574 decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
Michal Kazior581c25f2014-11-18 09:24:48 +02001575 RX_MSDU_START_INFO1_DECAP_FORMAT);
1576
Michal Kazior581c25f2014-11-18 09:24:48 +02001577 /* FIXME: Current unchaining logic can only handle simple case of raw
1578 * msdu chaining. If decapping is other than raw the chaining may be
1579 * more complex and this isn't handled by the current code. Don't even
1580 * try re-constructing such frames - it'll be pretty much garbage.
1581 */
1582 if (decap != RX_MSDU_DECAP_RAW ||
1583 skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
1584 __skb_queue_purge(amsdu);
1585 return;
1586 }
1587
1588 ath10k_unchain_msdu(amsdu);
1589}
1590
1591static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
1592 struct sk_buff_head *amsdu,
1593 struct ieee80211_rx_status *rx_status)
1594{
Michal Kazior581c25f2014-11-18 09:24:48 +02001595 /* FIXME: It might be a good idea to do some fuzzy-testing to drop
1596 * invalid/dangerous frames.
1597 */
1598
1599 if (!rx_status->freq) {
Gabriel Craciunescu984eb902017-08-03 15:01:02 +03001600 ath10k_dbg(ar, ATH10K_DBG_HTT, "no channel configured; ignoring frame(s)!\n");
Janusz Dziedzic2acc4eb2014-03-19 07:09:40 +01001601 return false;
1602 }
1603
Michal Kazior581c25f2014-11-18 09:24:48 +02001604 if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
1605 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
Janusz Dziedzic2acc4eb2014-03-19 07:09:40 +01001606 return false;
1607 }
1608
1609 return true;
1610}
1611
Michal Kazior581c25f2014-11-18 09:24:48 +02001612static void ath10k_htt_rx_h_filter(struct ath10k *ar,
1613 struct sk_buff_head *amsdu,
1614 struct ieee80211_rx_status *rx_status)
1615{
1616 if (skb_queue_empty(amsdu))
1617 return;
1618
1619 if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
1620 return;
1621
1622 __skb_queue_purge(amsdu);
1623}
1624
Rajkumar Manoharan18235662016-03-22 17:22:14 +05301625static int ath10k_htt_rx_handle_amsdu(struct ath10k_htt *htt)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001626{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001627 struct ath10k *ar = htt->ar;
Ashok Raj Nagarajan237e15d2016-08-19 13:37:37 +03001628 struct ieee80211_rx_status *rx_status = &htt->rx_status;
Michal Kazior9aa505d2014-11-18 09:24:47 +02001629 struct sk_buff_head amsdu;
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001630 int ret, num_msdus;
Rajkumar Manoharan18235662016-03-22 17:22:14 +05301631
1632 __skb_queue_head_init(&amsdu);
1633
1634 spin_lock_bh(&htt->rx_ring.lock);
1635 if (htt->rx_confused) {
1636 spin_unlock_bh(&htt->rx_ring.lock);
1637 return -EIO;
1638 }
1639 ret = ath10k_htt_rx_amsdu_pop(htt, &amsdu);
1640 spin_unlock_bh(&htt->rx_ring.lock);
1641
1642 if (ret < 0) {
1643 ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
1644 __skb_queue_purge(&amsdu);
1645 /* FIXME: It's probably a good idea to reboot the
1646 * device instead of leaving it inoperable.
1647 */
1648 htt->rx_confused = true;
1649 return ret;
1650 }
1651
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001652 num_msdus = skb_queue_len(&amsdu);
Ashok Raj Nagarajan237e15d2016-08-19 13:37:37 +03001653 ath10k_htt_rx_h_ppdu(ar, &amsdu, rx_status, 0xffff);
Mohammed Shafi Shajakhan7543d112016-10-04 21:19:59 +05301654
1655 /* only for ret = 1 indicates chained msdus */
1656 if (ret > 0)
1657 ath10k_htt_rx_h_unchain(ar, &amsdu);
1658
Ashok Raj Nagarajan237e15d2016-08-19 13:37:37 +03001659 ath10k_htt_rx_h_filter(ar, &amsdu, rx_status);
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001660 ath10k_htt_rx_h_mpdu(ar, &amsdu, rx_status, true);
Ashok Raj Nagarajan237e15d2016-08-19 13:37:37 +03001661 ath10k_htt_rx_h_deliver(ar, &amsdu, rx_status);
Rajkumar Manoharan18235662016-03-22 17:22:14 +05301662
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001663 return num_msdus;
Rajkumar Manoharan18235662016-03-22 17:22:14 +05301664}
1665
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05301666static void ath10k_htt_rx_proc_rx_ind(struct ath10k_htt *htt,
1667 struct htt_rx_indication *rx)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001668{
1669 struct ath10k *ar = htt->ar;
Janusz Dziedzic6df92a32014-03-24 21:24:57 +01001670 struct htt_rx_indication_mpdu_range *mpdu_ranges;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001671 int num_mpdu_ranges;
Rajkumar Manoharan18235662016-03-22 17:22:14 +05301672 int i, mpdu_count = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001673
1674 num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
1675 HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
1676 mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
1677
Michal Kazior7aa7a722014-08-25 12:09:38 +02001678 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001679 rx, sizeof(*rx) +
1680 (sizeof(struct htt_rx_indication_mpdu_range) *
1681 num_mpdu_ranges));
1682
Michal Kaziord5406902014-11-18 09:24:47 +02001683 for (i = 0; i < num_mpdu_ranges; i++)
1684 mpdu_count += mpdu_ranges[i].mpdu_count;
Janusz Dziedzicd84dd602014-03-24 21:23:20 +01001685
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05301686 atomic_add(mpdu_count, &htt->num_mpdus_ready);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001687}
1688
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301689static void ath10k_htt_rx_tx_compl_ind(struct ath10k *ar,
Michal Kazior6c5151a2014-02-27 18:50:04 +02001690 struct sk_buff *skb)
1691{
1692 struct ath10k_htt *htt = &ar->htt;
1693 struct htt_resp *resp = (struct htt_resp *)skb->data;
1694 struct htt_tx_done tx_done = {};
1695 int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
1696 __le16 msdu_id;
1697 int i;
1698
1699 switch (status) {
1700 case HTT_DATA_TX_STATUS_NO_ACK:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301701 tx_done.status = HTT_TX_COMPL_STATE_NOACK;
Michal Kazior6c5151a2014-02-27 18:50:04 +02001702 break;
1703 case HTT_DATA_TX_STATUS_OK:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301704 tx_done.status = HTT_TX_COMPL_STATE_ACK;
Michal Kazior6c5151a2014-02-27 18:50:04 +02001705 break;
1706 case HTT_DATA_TX_STATUS_DISCARD:
1707 case HTT_DATA_TX_STATUS_POSTPONE:
1708 case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301709 tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
Michal Kazior6c5151a2014-02-27 18:50:04 +02001710 break;
1711 default:
Michal Kazior7aa7a722014-08-25 12:09:38 +02001712 ath10k_warn(ar, "unhandled tx completion status %d\n", status);
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301713 tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
Michal Kazior6c5151a2014-02-27 18:50:04 +02001714 break;
1715 }
1716
Michal Kazior7aa7a722014-08-25 12:09:38 +02001717 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
Michal Kazior6c5151a2014-02-27 18:50:04 +02001718 resp->data_tx_completion.num_msdus);
1719
1720 for (i = 0; i < resp->data_tx_completion.num_msdus; i++) {
1721 msdu_id = resp->data_tx_completion.msdus[i];
1722 tx_done.msdu_id = __le16_to_cpu(msdu_id);
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301723
1724 /* kfifo_put: In practice firmware shouldn't fire off per-CE
1725 * interrupt and main interrupt (MSI/-X range case) for the same
1726 * HTC service so it should be safe to use kfifo_put w/o lock.
1727 *
1728 * From kfifo_put() documentation:
1729 * Note that with only one concurrent reader and one concurrent
1730 * writer, you don't need extra locking to use these macro.
1731 */
1732 if (!kfifo_put(&htt->txdone_fifo, tx_done)) {
1733 ath10k_warn(ar, "txdone fifo overrun, msdu_id %d status %d\n",
1734 tx_done.msdu_id, tx_done.status);
1735 ath10k_txrx_tx_unref(htt, &tx_done);
1736 }
Michal Kazior6c5151a2014-02-27 18:50:04 +02001737 }
1738}
1739
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001740static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
1741{
1742 struct htt_rx_addba *ev = &resp->rx_addba;
1743 struct ath10k_peer *peer;
1744 struct ath10k_vif *arvif;
1745 u16 info0, tid, peer_id;
1746
1747 info0 = __le16_to_cpu(ev->info0);
1748 tid = MS(info0, HTT_RX_BA_INFO0_TID);
1749 peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1750
Michal Kazior7aa7a722014-08-25 12:09:38 +02001751 ath10k_dbg(ar, ATH10K_DBG_HTT,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001752 "htt rx addba tid %hu peer_id %hu size %hhu\n",
1753 tid, peer_id, ev->window_size);
1754
1755 spin_lock_bh(&ar->data_lock);
1756 peer = ath10k_peer_find_by_id(ar, peer_id);
1757 if (!peer) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001758 ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001759 peer_id);
1760 spin_unlock_bh(&ar->data_lock);
1761 return;
1762 }
1763
1764 arvif = ath10k_get_arvif(ar, peer->vdev_id);
1765 if (!arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001766 ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001767 peer->vdev_id);
1768 spin_unlock_bh(&ar->data_lock);
1769 return;
1770 }
1771
Michal Kazior7aa7a722014-08-25 12:09:38 +02001772 ath10k_dbg(ar, ATH10K_DBG_HTT,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001773 "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
1774 peer->addr, tid, ev->window_size);
1775
1776 ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1777 spin_unlock_bh(&ar->data_lock);
1778}
1779
1780static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
1781{
1782 struct htt_rx_delba *ev = &resp->rx_delba;
1783 struct ath10k_peer *peer;
1784 struct ath10k_vif *arvif;
1785 u16 info0, tid, peer_id;
1786
1787 info0 = __le16_to_cpu(ev->info0);
1788 tid = MS(info0, HTT_RX_BA_INFO0_TID);
1789 peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1790
Michal Kazior7aa7a722014-08-25 12:09:38 +02001791 ath10k_dbg(ar, ATH10K_DBG_HTT,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001792 "htt rx delba tid %hu peer_id %hu\n",
1793 tid, peer_id);
1794
1795 spin_lock_bh(&ar->data_lock);
1796 peer = ath10k_peer_find_by_id(ar, peer_id);
1797 if (!peer) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001798 ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001799 peer_id);
1800 spin_unlock_bh(&ar->data_lock);
1801 return;
1802 }
1803
1804 arvif = ath10k_get_arvif(ar, peer->vdev_id);
1805 if (!arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001806 ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001807 peer->vdev_id);
1808 spin_unlock_bh(&ar->data_lock);
1809 return;
1810 }
1811
Michal Kazior7aa7a722014-08-25 12:09:38 +02001812 ath10k_dbg(ar, ATH10K_DBG_HTT,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001813 "htt rx stop rx ba session sta %pM tid %hu\n",
1814 peer->addr, tid);
1815
1816 ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1817 spin_unlock_bh(&ar->data_lock);
1818}
1819
Michal Kaziorc5450702015-01-24 12:14:48 +02001820static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
Kalle Valoe48e9c422017-10-29 09:45:07 +02001821 struct sk_buff_head *amsdu)
Michal Kaziorc5450702015-01-24 12:14:48 +02001822{
1823 struct sk_buff *msdu;
1824 struct htt_rx_desc *rxd;
1825
1826 if (skb_queue_empty(list))
1827 return -ENOBUFS;
1828
1829 if (WARN_ON(!skb_queue_empty(amsdu)))
1830 return -EINVAL;
1831
Kalle Valoe48e9c422017-10-29 09:45:07 +02001832 while ((msdu = __skb_dequeue(list))) {
Michal Kaziorc5450702015-01-24 12:14:48 +02001833 __skb_queue_tail(amsdu, msdu);
1834
1835 rxd = (void *)msdu->data - sizeof(*rxd);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001836 if (rxd->msdu_end.common.info0 &
Michal Kaziorc5450702015-01-24 12:14:48 +02001837 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
1838 break;
1839 }
1840
1841 msdu = skb_peek_tail(amsdu);
1842 rxd = (void *)msdu->data - sizeof(*rxd);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001843 if (!(rxd->msdu_end.common.info0 &
Michal Kaziorc5450702015-01-24 12:14:48 +02001844 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
1845 skb_queue_splice_init(amsdu, list);
1846 return -EAGAIN;
1847 }
1848
1849 return 0;
1850}
1851
1852static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
1853 struct sk_buff *skb)
1854{
1855 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1856
1857 if (!ieee80211_has_protected(hdr->frame_control))
1858 return;
1859
1860 /* Offloaded frames are already decrypted but firmware insists they are
1861 * protected in the 802.11 header. Strip the flag. Otherwise mac80211
1862 * will drop the frame.
1863 */
1864
1865 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1866 status->flag |= RX_FLAG_DECRYPTED |
1867 RX_FLAG_IV_STRIPPED |
1868 RX_FLAG_MMIC_STRIPPED;
1869}
1870
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001871static int ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
1872 struct sk_buff_head *list)
Michal Kaziorc5450702015-01-24 12:14:48 +02001873{
1874 struct ath10k_htt *htt = &ar->htt;
1875 struct ieee80211_rx_status *status = &htt->rx_status;
1876 struct htt_rx_offload_msdu *rx;
1877 struct sk_buff *msdu;
1878 size_t offset;
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001879 int num_msdu = 0;
Michal Kaziorc5450702015-01-24 12:14:48 +02001880
1881 while ((msdu = __skb_dequeue(list))) {
1882 /* Offloaded frames don't have Rx descriptor. Instead they have
1883 * a short meta information header.
1884 */
1885
1886 rx = (void *)msdu->data;
1887
1888 skb_put(msdu, sizeof(*rx));
1889 skb_pull(msdu, sizeof(*rx));
1890
1891 if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
1892 ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
1893 dev_kfree_skb_any(msdu);
1894 continue;
1895 }
1896
1897 skb_put(msdu, __le16_to_cpu(rx->msdu_len));
1898
1899 /* Offloaded rx header length isn't multiple of 2 nor 4 so the
1900 * actual payload is unaligned. Align the frame. Otherwise
1901 * mac80211 complains. This shouldn't reduce performance much
1902 * because these offloaded frames are rare.
1903 */
1904 offset = 4 - ((unsigned long)msdu->data & 3);
1905 skb_put(msdu, offset);
1906 memmove(msdu->data + offset, msdu->data, msdu->len);
1907 skb_pull(msdu, offset);
1908
1909 /* FIXME: The frame is NWifi. Re-construct QoS Control
1910 * if possible later.
1911 */
1912
1913 memset(status, 0, sizeof(*status));
1914 status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1915
1916 ath10k_htt_rx_h_rx_offload_prot(status, msdu);
Michal Kazior500ff9f2015-03-31 10:26:21 +00001917 ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
Michal Kaziorc5450702015-01-24 12:14:48 +02001918 ath10k_process_rx(ar, status, msdu);
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001919 num_msdu++;
Michal Kaziorc5450702015-01-24 12:14:48 +02001920 }
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001921 return num_msdu;
Michal Kaziorc5450702015-01-24 12:14:48 +02001922}
1923
Kalle Valoe48e9c422017-10-29 09:45:07 +02001924static int ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
Michal Kaziorc5450702015-01-24 12:14:48 +02001925{
1926 struct ath10k_htt *htt = &ar->htt;
1927 struct htt_resp *resp = (void *)skb->data;
1928 struct ieee80211_rx_status *status = &htt->rx_status;
1929 struct sk_buff_head list;
1930 struct sk_buff_head amsdu;
1931 u16 peer_id;
1932 u16 msdu_count;
1933 u8 vdev_id;
1934 u8 tid;
1935 bool offload;
1936 bool frag;
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001937 int ret, num_msdus = 0;
Michal Kaziorc5450702015-01-24 12:14:48 +02001938
1939 lockdep_assert_held(&htt->rx_ring.lock);
1940
1941 if (htt->rx_confused)
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001942 return -EIO;
Michal Kaziorc5450702015-01-24 12:14:48 +02001943
1944 skb_pull(skb, sizeof(resp->hdr));
1945 skb_pull(skb, sizeof(resp->rx_in_ord_ind));
1946
1947 peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
1948 msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
1949 vdev_id = resp->rx_in_ord_ind.vdev_id;
1950 tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
1951 offload = !!(resp->rx_in_ord_ind.info &
1952 HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
1953 frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);
1954
1955 ath10k_dbg(ar, ATH10K_DBG_HTT,
1956 "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
1957 vdev_id, peer_id, tid, offload, frag, msdu_count);
1958
1959 if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs)) {
1960 ath10k_warn(ar, "dropping invalid in order rx indication\n");
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001961 return -EINVAL;
Michal Kaziorc5450702015-01-24 12:14:48 +02001962 }
1963
1964 /* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
1965 * extracted and processed.
1966 */
1967 __skb_queue_head_init(&list);
1968 ret = ath10k_htt_rx_pop_paddr_list(htt, &resp->rx_in_ord_ind, &list);
1969 if (ret < 0) {
1970 ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
1971 htt->rx_confused = true;
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001972 return -EIO;
Michal Kaziorc5450702015-01-24 12:14:48 +02001973 }
1974
1975 /* Offloaded frames are very different and need to be handled
1976 * separately.
1977 */
1978 if (offload)
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001979 num_msdus = ath10k_htt_rx_h_rx_offload(ar, &list);
Michal Kaziorc5450702015-01-24 12:14:48 +02001980
Kalle Valoe48e9c422017-10-29 09:45:07 +02001981 while (!skb_queue_empty(&list)) {
Michal Kaziorc5450702015-01-24 12:14:48 +02001982 __skb_queue_head_init(&amsdu);
Kalle Valoe48e9c422017-10-29 09:45:07 +02001983 ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
Michal Kaziorc5450702015-01-24 12:14:48 +02001984 switch (ret) {
1985 case 0:
1986 /* Note: The in-order indication may report interleaved
1987 * frames from different PPDUs meaning reported rx rate
1988 * to mac80211 isn't accurate/reliable. It's still
1989 * better to report something than nothing though. This
1990 * should still give an idea about rx rate to the user.
1991 */
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03001992 num_msdus += skb_queue_len(&amsdu);
Michal Kazior500ff9f2015-03-31 10:26:21 +00001993 ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
Michal Kaziorc5450702015-01-24 12:14:48 +02001994 ath10k_htt_rx_h_filter(ar, &amsdu, status);
Vasanthakumar Thiagarajan7eccb732017-10-27 18:35:31 +03001995 ath10k_htt_rx_h_mpdu(ar, &amsdu, status, false);
Michal Kaziorc5450702015-01-24 12:14:48 +02001996 ath10k_htt_rx_h_deliver(ar, &amsdu, status);
1997 break;
1998 case -EAGAIN:
1999 /* fall through */
2000 default:
2001 /* Should not happen. */
2002 ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
2003 htt->rx_confused = true;
2004 __skb_queue_purge(&list);
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002005 return -EIO;
Michal Kaziorc5450702015-01-24 12:14:48 +02002006 }
2007 }
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002008 return num_msdus;
Michal Kaziorc5450702015-01-24 12:14:48 +02002009}
2010
Michal Kazior839ae632016-03-06 16:14:32 +02002011static void ath10k_htt_rx_tx_fetch_resp_id_confirm(struct ath10k *ar,
2012 const __le32 *resp_ids,
2013 int num_resp_ids)
2014{
2015 int i;
2016 u32 resp_id;
2017
2018 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm num_resp_ids %d\n",
2019 num_resp_ids);
2020
2021 for (i = 0; i < num_resp_ids; i++) {
2022 resp_id = le32_to_cpu(resp_ids[i]);
2023
2024 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm resp_id %u\n",
2025 resp_id);
2026
2027 /* TODO: free resp_id */
2028 }
2029}
2030
2031static void ath10k_htt_rx_tx_fetch_ind(struct ath10k *ar, struct sk_buff *skb)
2032{
Michal Kazior426e10e2016-03-06 16:14:43 +02002033 struct ieee80211_hw *hw = ar->hw;
2034 struct ieee80211_txq *txq;
Michal Kazior839ae632016-03-06 16:14:32 +02002035 struct htt_resp *resp = (struct htt_resp *)skb->data;
2036 struct htt_tx_fetch_record *record;
2037 size_t len;
2038 size_t max_num_bytes;
2039 size_t max_num_msdus;
Michal Kazior426e10e2016-03-06 16:14:43 +02002040 size_t num_bytes;
2041 size_t num_msdus;
Michal Kazior839ae632016-03-06 16:14:32 +02002042 const __le32 *resp_ids;
2043 u16 num_records;
2044 u16 num_resp_ids;
2045 u16 peer_id;
2046 u8 tid;
Michal Kazior426e10e2016-03-06 16:14:43 +02002047 int ret;
Michal Kazior839ae632016-03-06 16:14:32 +02002048 int i;
2049
2050 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind\n");
2051
2052 len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_ind);
2053 if (unlikely(skb->len < len)) {
2054 ath10k_warn(ar, "received corrupted tx_fetch_ind event: buffer too short\n");
2055 return;
2056 }
2057
2058 num_records = le16_to_cpu(resp->tx_fetch_ind.num_records);
2059 num_resp_ids = le16_to_cpu(resp->tx_fetch_ind.num_resp_ids);
2060
2061 len += sizeof(resp->tx_fetch_ind.records[0]) * num_records;
2062 len += sizeof(resp->tx_fetch_ind.resp_ids[0]) * num_resp_ids;
2063
2064 if (unlikely(skb->len < len)) {
2065 ath10k_warn(ar, "received corrupted tx_fetch_ind event: too many records/resp_ids\n");
2066 return;
2067 }
2068
2069 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind num records %hu num resps %hu seq %hu\n",
2070 num_records, num_resp_ids,
2071 le16_to_cpu(resp->tx_fetch_ind.fetch_seq_num));
2072
Michal Kazior426e10e2016-03-06 16:14:43 +02002073 if (!ar->htt.tx_q_state.enabled) {
2074 ath10k_warn(ar, "received unexpected tx_fetch_ind event: not enabled\n");
2075 return;
2076 }
2077
2078 if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH) {
2079 ath10k_warn(ar, "received unexpected tx_fetch_ind event: in push mode\n");
2080 return;
2081 }
2082
2083 rcu_read_lock();
Michal Kazior839ae632016-03-06 16:14:32 +02002084
2085 for (i = 0; i < num_records; i++) {
2086 record = &resp->tx_fetch_ind.records[i];
2087 peer_id = MS(le16_to_cpu(record->info),
2088 HTT_TX_FETCH_RECORD_INFO_PEER_ID);
2089 tid = MS(le16_to_cpu(record->info),
2090 HTT_TX_FETCH_RECORD_INFO_TID);
2091 max_num_msdus = le16_to_cpu(record->num_msdus);
2092 max_num_bytes = le32_to_cpu(record->num_bytes);
2093
2094 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch record %i peer_id %hu tid %hhu msdus %zu bytes %zu\n",
2095 i, peer_id, tid, max_num_msdus, max_num_bytes);
2096
2097 if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
2098 unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
2099 ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
2100 peer_id, tid);
2101 continue;
2102 }
2103
Michal Kazior426e10e2016-03-06 16:14:43 +02002104 spin_lock_bh(&ar->data_lock);
2105 txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
2106 spin_unlock_bh(&ar->data_lock);
2107
2108 /* It is okay to release the lock and use txq because RCU read
2109 * lock is held.
2110 */
2111
2112 if (unlikely(!txq)) {
2113 ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
2114 peer_id, tid);
2115 continue;
2116 }
2117
2118 num_msdus = 0;
2119 num_bytes = 0;
2120
2121 while (num_msdus < max_num_msdus &&
2122 num_bytes < max_num_bytes) {
2123 ret = ath10k_mac_tx_push_txq(hw, txq);
2124 if (ret < 0)
2125 break;
2126
2127 num_msdus++;
2128 num_bytes += ret;
2129 }
2130
2131 record->num_msdus = cpu_to_le16(num_msdus);
2132 record->num_bytes = cpu_to_le32(num_bytes);
2133
2134 ath10k_htt_tx_txq_recalc(hw, txq);
Michal Kazior839ae632016-03-06 16:14:32 +02002135 }
2136
Michal Kazior426e10e2016-03-06 16:14:43 +02002137 rcu_read_unlock();
2138
Michal Kazior839ae632016-03-06 16:14:32 +02002139 resp_ids = ath10k_htt_get_tx_fetch_ind_resp_ids(&resp->tx_fetch_ind);
2140 ath10k_htt_rx_tx_fetch_resp_id_confirm(ar, resp_ids, num_resp_ids);
2141
Michal Kazior426e10e2016-03-06 16:14:43 +02002142 ret = ath10k_htt_tx_fetch_resp(ar,
2143 resp->tx_fetch_ind.token,
2144 resp->tx_fetch_ind.fetch_seq_num,
2145 resp->tx_fetch_ind.records,
2146 num_records);
2147 if (unlikely(ret)) {
2148 ath10k_warn(ar, "failed to submit tx fetch resp for token 0x%08x: %d\n",
2149 le32_to_cpu(resp->tx_fetch_ind.token), ret);
2150 /* FIXME: request fw restart */
2151 }
2152
2153 ath10k_htt_tx_txq_sync(ar);
Michal Kazior839ae632016-03-06 16:14:32 +02002154}
2155
2156static void ath10k_htt_rx_tx_fetch_confirm(struct ath10k *ar,
2157 struct sk_buff *skb)
2158{
2159 const struct htt_resp *resp = (void *)skb->data;
2160 size_t len;
2161 int num_resp_ids;
2162
2163 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm\n");
2164
2165 len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_confirm);
2166 if (unlikely(skb->len < len)) {
2167 ath10k_warn(ar, "received corrupted tx_fetch_confirm event: buffer too short\n");
2168 return;
2169 }
2170
2171 num_resp_ids = le16_to_cpu(resp->tx_fetch_confirm.num_resp_ids);
2172 len += sizeof(resp->tx_fetch_confirm.resp_ids[0]) * num_resp_ids;
2173
2174 if (unlikely(skb->len < len)) {
2175 ath10k_warn(ar, "received corrupted tx_fetch_confirm event: resp_ids buffer overflow\n");
2176 return;
2177 }
2178
2179 ath10k_htt_rx_tx_fetch_resp_id_confirm(ar,
2180 resp->tx_fetch_confirm.resp_ids,
2181 num_resp_ids);
2182}
2183
2184static void ath10k_htt_rx_tx_mode_switch_ind(struct ath10k *ar,
2185 struct sk_buff *skb)
2186{
2187 const struct htt_resp *resp = (void *)skb->data;
2188 const struct htt_tx_mode_switch_record *record;
Michal Kazior426e10e2016-03-06 16:14:43 +02002189 struct ieee80211_txq *txq;
2190 struct ath10k_txq *artxq;
Michal Kazior839ae632016-03-06 16:14:32 +02002191 size_t len;
2192 size_t num_records;
2193 enum htt_tx_mode_switch_mode mode;
2194 bool enable;
2195 u16 info0;
2196 u16 info1;
2197 u16 threshold;
2198 u16 peer_id;
2199 u8 tid;
2200 int i;
2201
2202 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx mode switch ind\n");
2203
2204 len = sizeof(resp->hdr) + sizeof(resp->tx_mode_switch_ind);
2205 if (unlikely(skb->len < len)) {
2206 ath10k_warn(ar, "received corrupted tx_mode_switch_ind event: buffer too short\n");
2207 return;
2208 }
2209
2210 info0 = le16_to_cpu(resp->tx_mode_switch_ind.info0);
2211 info1 = le16_to_cpu(resp->tx_mode_switch_ind.info1);
2212
2213 enable = !!(info0 & HTT_TX_MODE_SWITCH_IND_INFO0_ENABLE);
2214 num_records = MS(info0, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
2215 mode = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_MODE);
2216 threshold = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
2217
2218 ath10k_dbg(ar, ATH10K_DBG_HTT,
2219 "htt rx tx mode switch ind info0 0x%04hx info1 0x%04hx enable %d num records %zd mode %d threshold %hu\n",
2220 info0, info1, enable, num_records, mode, threshold);
2221
2222 len += sizeof(resp->tx_mode_switch_ind.records[0]) * num_records;
2223
2224 if (unlikely(skb->len < len)) {
2225 ath10k_warn(ar, "received corrupted tx_mode_switch_mode_ind event: too many records\n");
2226 return;
2227 }
2228
2229 switch (mode) {
2230 case HTT_TX_MODE_SWITCH_PUSH:
2231 case HTT_TX_MODE_SWITCH_PUSH_PULL:
2232 break;
2233 default:
2234 ath10k_warn(ar, "received invalid tx_mode_switch_mode_ind mode %d, ignoring\n",
2235 mode);
2236 return;
2237 }
2238
2239 if (!enable)
2240 return;
2241
Michal Kazior426e10e2016-03-06 16:14:43 +02002242 ar->htt.tx_q_state.enabled = enable;
2243 ar->htt.tx_q_state.mode = mode;
2244 ar->htt.tx_q_state.num_push_allowed = threshold;
2245
2246 rcu_read_lock();
Michal Kazior839ae632016-03-06 16:14:32 +02002247
2248 for (i = 0; i < num_records; i++) {
2249 record = &resp->tx_mode_switch_ind.records[i];
2250 info0 = le16_to_cpu(record->info0);
2251 peer_id = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_PEER_ID);
2252 tid = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_TID);
2253
2254 if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
2255 unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
2256 ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
2257 peer_id, tid);
2258 continue;
2259 }
2260
Michal Kazior426e10e2016-03-06 16:14:43 +02002261 spin_lock_bh(&ar->data_lock);
2262 txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
2263 spin_unlock_bh(&ar->data_lock);
2264
2265 /* It is okay to release the lock and use txq because RCU read
2266 * lock is held.
2267 */
2268
2269 if (unlikely(!txq)) {
2270 ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
2271 peer_id, tid);
2272 continue;
2273 }
2274
2275 spin_lock_bh(&ar->htt.tx_lock);
2276 artxq = (void *)txq->drv_priv;
2277 artxq->num_push_allowed = le16_to_cpu(record->num_max_msdus);
2278 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazior839ae632016-03-06 16:14:32 +02002279 }
2280
Michal Kazior426e10e2016-03-06 16:14:43 +02002281 rcu_read_unlock();
2282
2283 ath10k_mac_tx_push_pending(ar);
Michal Kazior839ae632016-03-06 16:14:32 +02002284}
2285
Rajkumar Manoharane3a91f82016-03-22 17:22:16 +05302286void ath10k_htt_htc_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
2287{
2288 bool release;
2289
2290 release = ath10k_htt_t2h_msg_handler(ar, skb);
2291
2292 /* Free the indication buffer */
2293 if (release)
2294 dev_kfree_skb_any(skb);
2295}
2296
Anilkumar Kollicec17c32016-11-23 16:58:10 +02002297static inline bool is_valid_legacy_rate(u8 rate)
2298{
2299 static const u8 legacy_rates[] = {1, 2, 5, 11, 6, 9, 12,
2300 18, 24, 36, 48, 54};
2301 int i;
2302
2303 for (i = 0; i < ARRAY_SIZE(legacy_rates); i++) {
2304 if (rate == legacy_rates[i])
2305 return true;
2306 }
2307
2308 return false;
2309}
2310
2311static void
2312ath10k_update_per_peer_tx_stats(struct ath10k *ar,
2313 struct ieee80211_sta *sta,
2314 struct ath10k_per_peer_tx_stats *peer_stats)
2315{
2316 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
2317 u8 rate = 0, sgi;
2318 struct rate_info txrate;
2319
2320 lockdep_assert_held(&ar->data_lock);
2321
2322 txrate.flags = ATH10K_HW_PREAMBLE(peer_stats->ratecode);
2323 txrate.bw = ATH10K_HW_BW(peer_stats->flags);
2324 txrate.nss = ATH10K_HW_NSS(peer_stats->ratecode);
2325 txrate.mcs = ATH10K_HW_MCS_RATE(peer_stats->ratecode);
2326 sgi = ATH10K_HW_GI(peer_stats->flags);
2327
Sven Eckelmannc1dd8012017-05-19 11:54:02 +03002328 if (txrate.flags == WMI_RATE_PREAMBLE_VHT && txrate.mcs > 9) {
2329 ath10k_warn(ar, "Invalid VHT mcs %hhd peer stats", txrate.mcs);
2330 return;
2331 }
2332
2333 if (txrate.flags == WMI_RATE_PREAMBLE_HT &&
2334 (txrate.mcs > 7 || txrate.nss < 1)) {
2335 ath10k_warn(ar, "Invalid HT mcs %hhd nss %hhd peer stats",
2336 txrate.mcs, txrate.nss);
Anilkumar Kollicec17c32016-11-23 16:58:10 +02002337 return;
2338 }
2339
Mohammed Shafi Shajakhan0f8a2b72017-01-13 16:00:03 +05302340 memset(&arsta->txrate, 0, sizeof(arsta->txrate));
2341
Anilkumar Kollicec17c32016-11-23 16:58:10 +02002342 if (txrate.flags == WMI_RATE_PREAMBLE_CCK ||
2343 txrate.flags == WMI_RATE_PREAMBLE_OFDM) {
2344 rate = ATH10K_HW_LEGACY_RATE(peer_stats->ratecode);
2345
2346 if (!is_valid_legacy_rate(rate)) {
2347 ath10k_warn(ar, "Invalid legacy rate %hhd peer stats",
2348 rate);
2349 return;
2350 }
2351
2352 /* This is hacky, FW sends CCK rate 5.5Mbps as 6 */
2353 rate *= 10;
2354 if (rate == 60 && txrate.flags == WMI_RATE_PREAMBLE_CCK)
2355 rate = rate - 5;
Mohammed Shafi Shajakhancd591022017-01-12 13:02:22 +02002356 arsta->txrate.legacy = rate;
Anilkumar Kollicec17c32016-11-23 16:58:10 +02002357 } else if (txrate.flags == WMI_RATE_PREAMBLE_HT) {
2358 arsta->txrate.flags = RATE_INFO_FLAGS_MCS;
Sven Eckelmannc1dd8012017-05-19 11:54:02 +03002359 arsta->txrate.mcs = txrate.mcs + 8 * (txrate.nss - 1);
Anilkumar Kollicec17c32016-11-23 16:58:10 +02002360 } else {
2361 arsta->txrate.flags = RATE_INFO_FLAGS_VHT_MCS;
2362 arsta->txrate.mcs = txrate.mcs;
2363 }
2364
2365 if (sgi)
2366 arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
2367
2368 arsta->txrate.nss = txrate.nss;
2369 arsta->txrate.bw = txrate.bw + RATE_INFO_BW_20;
2370}
2371
2372static void ath10k_htt_fetch_peer_stats(struct ath10k *ar,
2373 struct sk_buff *skb)
2374{
2375 struct htt_resp *resp = (struct htt_resp *)skb->data;
2376 struct ath10k_per_peer_tx_stats *p_tx_stats = &ar->peer_tx_stats;
2377 struct htt_per_peer_tx_stats_ind *tx_stats;
2378 struct ieee80211_sta *sta;
2379 struct ath10k_peer *peer;
2380 int peer_id, i;
2381 u8 ppdu_len, num_ppdu;
2382
2383 num_ppdu = resp->peer_tx_stats.num_ppdu;
2384 ppdu_len = resp->peer_tx_stats.ppdu_len * sizeof(__le32);
2385
2386 if (skb->len < sizeof(struct htt_resp_hdr) + num_ppdu * ppdu_len) {
2387 ath10k_warn(ar, "Invalid peer stats buf length %d\n", skb->len);
2388 return;
2389 }
2390
2391 tx_stats = (struct htt_per_peer_tx_stats_ind *)
2392 (resp->peer_tx_stats.payload);
2393 peer_id = __le16_to_cpu(tx_stats->peer_id);
2394
2395 rcu_read_lock();
2396 spin_lock_bh(&ar->data_lock);
2397 peer = ath10k_peer_find_by_id(ar, peer_id);
2398 if (!peer) {
2399 ath10k_warn(ar, "Invalid peer id %d peer stats buffer\n",
2400 peer_id);
2401 goto out;
2402 }
2403
2404 sta = peer->sta;
2405 for (i = 0; i < num_ppdu; i++) {
2406 tx_stats = (struct htt_per_peer_tx_stats_ind *)
2407 (resp->peer_tx_stats.payload + i * ppdu_len);
2408
2409 p_tx_stats->succ_bytes = __le32_to_cpu(tx_stats->succ_bytes);
2410 p_tx_stats->retry_bytes = __le32_to_cpu(tx_stats->retry_bytes);
2411 p_tx_stats->failed_bytes =
2412 __le32_to_cpu(tx_stats->failed_bytes);
2413 p_tx_stats->ratecode = tx_stats->ratecode;
2414 p_tx_stats->flags = tx_stats->flags;
2415 p_tx_stats->succ_pkts = __le16_to_cpu(tx_stats->succ_pkts);
2416 p_tx_stats->retry_pkts = __le16_to_cpu(tx_stats->retry_pkts);
2417 p_tx_stats->failed_pkts = __le16_to_cpu(tx_stats->failed_pkts);
2418
2419 ath10k_update_per_peer_tx_stats(ar, sta, p_tx_stats);
2420 }
2421
2422out:
2423 spin_unlock_bh(&ar->data_lock);
2424 rcu_read_unlock();
2425}
2426
Rajkumar Manoharane3a91f82016-03-22 17:22:16 +05302427bool ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002428{
Michal Kazioredb82362013-07-05 16:15:14 +03002429 struct ath10k_htt *htt = &ar->htt;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002430 struct htt_resp *resp = (struct htt_resp *)skb->data;
Rajkumar Manoharan8348db22015-03-25 13:12:27 +02002431 enum htt_t2h_msg_type type;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002432
2433 /* confirm alignment */
2434 if (!IS_ALIGNED((unsigned long)skb->data, 4))
Michal Kazior7aa7a722014-08-25 12:09:38 +02002435 ath10k_warn(ar, "unaligned htt message, expect trouble\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002436
Michal Kazior7aa7a722014-08-25 12:09:38 +02002437 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002438 resp->hdr.msg_type);
Rajkumar Manoharan8348db22015-03-25 13:12:27 +02002439
2440 if (resp->hdr.msg_type >= ar->htt.t2h_msg_types_max) {
2441 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, unsupported msg_type: 0x%0X\n max: 0x%0X",
2442 resp->hdr.msg_type, ar->htt.t2h_msg_types_max);
Rajkumar Manoharane3a91f82016-03-22 17:22:16 +05302443 return true;
Rajkumar Manoharan8348db22015-03-25 13:12:27 +02002444 }
2445 type = ar->htt.t2h_msg_types[resp->hdr.msg_type];
2446
2447 switch (type) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002448 case HTT_T2H_MSG_TYPE_VERSION_CONF: {
2449 htt->target_version_major = resp->ver_resp.major;
2450 htt->target_version_minor = resp->ver_resp.minor;
2451 complete(&htt->target_version_received);
2452 break;
2453 }
Michal Kazior6c5151a2014-02-27 18:50:04 +02002454 case HTT_T2H_MSG_TYPE_RX_IND:
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05302455 ath10k_htt_rx_proc_rx_ind(htt, &resp->rx_ind);
2456 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002457 case HTT_T2H_MSG_TYPE_PEER_MAP: {
2458 struct htt_peer_map_event ev = {
2459 .vdev_id = resp->peer_map.vdev_id,
2460 .peer_id = __le16_to_cpu(resp->peer_map.peer_id),
2461 };
2462 memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
2463 ath10k_peer_map_event(htt, &ev);
2464 break;
2465 }
2466 case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
2467 struct htt_peer_unmap_event ev = {
2468 .peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
2469 };
2470 ath10k_peer_unmap_event(htt, &ev);
2471 break;
2472 }
2473 case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
2474 struct htt_tx_done tx_done = {};
2475 int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
2476
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302477 tx_done.msdu_id = __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002478
2479 switch (status) {
2480 case HTT_MGMT_TX_STATUS_OK:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302481 tx_done.status = HTT_TX_COMPL_STATE_ACK;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002482 break;
2483 case HTT_MGMT_TX_STATUS_RETRY:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302484 tx_done.status = HTT_TX_COMPL_STATE_NOACK;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002485 break;
2486 case HTT_MGMT_TX_STATUS_DROP:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302487 tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002488 break;
2489 }
2490
Rajkumar Manoharancac08552016-03-09 20:25:46 +05302491 status = ath10k_txrx_tx_unref(htt, &tx_done);
2492 if (!status) {
2493 spin_lock_bh(&htt->tx_lock);
2494 ath10k_htt_tx_mgmt_dec_pending(htt);
2495 spin_unlock_bh(&htt->tx_lock);
2496 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002497 break;
2498 }
Michal Kazior6c5151a2014-02-27 18:50:04 +02002499 case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302500 ath10k_htt_rx_tx_compl_ind(htt->ar, skb);
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302501 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002502 case HTT_T2H_MSG_TYPE_SEC_IND: {
2503 struct ath10k *ar = htt->ar;
2504 struct htt_security_indication *ev = &resp->security_indication;
2505
Michal Kazior7aa7a722014-08-25 12:09:38 +02002506 ath10k_dbg(ar, ATH10K_DBG_HTT,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002507 "sec ind peer_id %d unicast %d type %d\n",
2508 __le16_to_cpu(ev->peer_id),
2509 !!(ev->flags & HTT_SECURITY_IS_UNICAST),
2510 MS(ev->flags, HTT_SECURITY_TYPE));
2511 complete(&ar->install_key_done);
2512 break;
2513 }
2514 case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002515 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002516 skb->data, skb->len);
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002517 atomic_inc(&htt->num_mpdus_ready);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002518 break;
2519 }
2520 case HTT_T2H_MSG_TYPE_TEST:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002521 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002522 case HTT_T2H_MSG_TYPE_STATS_CONF:
Michal Kaziord35a6c12014-09-02 11:00:21 +03002523 trace_ath10k_htt_stats(ar, skb->data, skb->len);
Kalle Valoa9bf0502013-09-03 11:43:55 +03002524 break;
2525 case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
Michal Kazior708b9bd2014-07-21 20:52:59 +03002526 /* Firmware can return tx frames if it's unable to fully
2527 * process them and suspects host may be able to fix it. ath10k
2528 * sends all tx frames as already inspected so this shouldn't
2529 * happen unless fw has a bug.
2530 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02002531 ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
Michal Kazior708b9bd2014-07-21 20:52:59 +03002532 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002533 case HTT_T2H_MSG_TYPE_RX_ADDBA:
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02002534 ath10k_htt_rx_addba(ar, resp);
2535 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002536 case HTT_T2H_MSG_TYPE_RX_DELBA:
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02002537 ath10k_htt_rx_delba(ar, resp);
2538 break;
Rajkumar Manoharanbfdd7932014-10-03 08:02:40 +03002539 case HTT_T2H_MSG_TYPE_PKTLOG: {
Rajkumar Manoharanbfdd7932014-10-03 08:02:40 +03002540 trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
Ashok Raj Nagarajan34293f72016-06-30 15:23:55 +03002541 skb->len -
2542 offsetof(struct htt_resp,
2543 pktlog_msg.payload));
Rajkumar Manoharanbfdd7932014-10-03 08:02:40 +03002544 break;
2545 }
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02002546 case HTT_T2H_MSG_TYPE_RX_FLUSH: {
2547 /* Ignore this event because mac80211 takes care of Rx
2548 * aggregation reordering.
2549 */
2550 break;
2551 }
Michal Kaziorc5450702015-01-24 12:14:48 +02002552 case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002553 __skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
Rajkumar Manoharane3a91f82016-03-22 17:22:16 +05302554 return false;
Michal Kaziorc5450702015-01-24 12:14:48 +02002555 }
2556 case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND:
Rajkumar Manoharan8348db22015-03-25 13:12:27 +02002557 break;
Rajkumar Manoharan2ce9b252016-03-08 22:57:23 +05302558 case HTT_T2H_MSG_TYPE_CHAN_CHANGE: {
2559 u32 phymode = __le32_to_cpu(resp->chan_change.phymode);
2560 u32 freq = __le32_to_cpu(resp->chan_change.freq);
2561
Arend Van Spriel543b9212016-11-17 12:48:53 +00002562 ar->tgt_oper_chan = ieee80211_get_channel(ar->hw->wiphy, freq);
Rajkumar Manoharan2ce9b252016-03-08 22:57:23 +05302563 ath10k_dbg(ar, ATH10K_DBG_HTT,
2564 "htt chan change freq %u phymode %s\n",
2565 freq, ath10k_wmi_phymode_str(phymode));
Michal Kaziorc5450702015-01-24 12:14:48 +02002566 break;
Rajkumar Manoharan2ce9b252016-03-08 22:57:23 +05302567 }
David Liuccec9032015-07-24 20:25:32 +03002568 case HTT_T2H_MSG_TYPE_AGGR_CONF:
2569 break;
Rajkumar Manoharanb2fdbcc2016-03-22 17:22:12 +05302570 case HTT_T2H_MSG_TYPE_TX_FETCH_IND: {
2571 struct sk_buff *tx_fetch_ind = skb_copy(skb, GFP_ATOMIC);
2572
2573 if (!tx_fetch_ind) {
2574 ath10k_warn(ar, "failed to copy htt tx fetch ind\n");
2575 break;
2576 }
2577 skb_queue_tail(&htt->tx_fetch_ind_q, tx_fetch_ind);
Rajkumar Manoharanb2fdbcc2016-03-22 17:22:12 +05302578 break;
2579 }
Michal Kaziordf94e702016-01-21 14:13:23 +01002580 case HTT_T2H_MSG_TYPE_TX_FETCH_CONFIRM:
Michal Kazior839ae632016-03-06 16:14:32 +02002581 ath10k_htt_rx_tx_fetch_confirm(ar, skb);
2582 break;
Michal Kaziordf94e702016-01-21 14:13:23 +01002583 case HTT_T2H_MSG_TYPE_TX_MODE_SWITCH_IND:
Michal Kazior839ae632016-03-06 16:14:32 +02002584 ath10k_htt_rx_tx_mode_switch_ind(ar, skb);
Michal Kazior9b158732016-01-21 14:13:27 +01002585 break;
Anilkumar Kollicec17c32016-11-23 16:58:10 +02002586 case HTT_T2H_MSG_TYPE_PEER_STATS:
2587 ath10k_htt_fetch_peer_stats(ar, skb);
2588 break;
Michal Kazior9b158732016-01-21 14:13:27 +01002589 case HTT_T2H_MSG_TYPE_EN_STATS:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002590 default:
Michal Kazior2358a542014-10-02 13:32:55 +02002591 ath10k_warn(ar, "htt event (%d) not handled\n",
2592 resp->hdr.msg_type);
Michal Kazior7aa7a722014-08-25 12:09:38 +02002593 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002594 skb->data, skb->len);
2595 break;
Waldemar Rymarkiewiczdab55d12017-02-02 18:53:42 +01002596 }
Rajkumar Manoharane3a91f82016-03-22 17:22:16 +05302597 return true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002598}
Rajkumar Manoharan3f0f7ed2015-10-12 18:27:03 +05302599EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
Michal Kazior6c5151a2014-02-27 18:50:04 +02002600
Vivek Natarajanafb0bf72015-10-30 14:57:58 +05302601void ath10k_htt_rx_pktlog_completion_handler(struct ath10k *ar,
2602 struct sk_buff *skb)
2603{
Ashok Raj Nagarajan53a5c9b2016-02-05 21:12:48 +05302604 trace_ath10k_htt_pktlog(ar, skb->data, skb->len);
Vivek Natarajanafb0bf72015-10-30 14:57:58 +05302605 dev_kfree_skb_any(skb);
2606}
2607EXPORT_SYMBOL(ath10k_htt_rx_pktlog_completion_handler);
2608
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002609int ath10k_htt_txrx_compl_task(struct ath10k *ar, int budget)
Michal Kazior6c5151a2014-02-27 18:50:04 +02002610{
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002611 struct ath10k_htt *htt = &ar->htt;
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302612 struct htt_tx_done tx_done = {};
Michal Kazior426e10e2016-03-06 16:14:43 +02002613 struct sk_buff_head tx_ind_q;
Michal Kazior6c5151a2014-02-27 18:50:04 +02002614 struct sk_buff *skb;
Michal Kaziord742c962016-01-13 14:52:52 +01002615 unsigned long flags;
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002616 int quota = 0, done, num_rx_msdus;
2617 bool resched_napi = false;
Michal Kazior6c5151a2014-02-27 18:50:04 +02002618
Michal Kazior426e10e2016-03-06 16:14:43 +02002619 __skb_queue_head_init(&tx_ind_q);
Rajkumar Manoharanda6416c2016-02-12 11:40:59 +05302620
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002621 /* Since in-ord-ind can deliver more than 1 A-MSDU in single event,
2622 * process it first to utilize full available quota.
2623 */
2624 while (quota < budget) {
2625 if (skb_queue_empty(&htt->rx_in_ord_compl_q))
2626 break;
Rajkumar Manoharanda6416c2016-02-12 11:40:59 +05302627
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002628 skb = __skb_dequeue(&htt->rx_in_ord_compl_q);
2629 if (!skb) {
2630 resched_napi = true;
2631 goto exit;
2632 }
2633
2634 spin_lock_bh(&htt->rx_ring.lock);
Kalle Valoe48e9c422017-10-29 09:45:07 +02002635 num_rx_msdus = ath10k_htt_rx_in_ord_ind(ar, skb);
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002636 spin_unlock_bh(&htt->rx_ring.lock);
2637 if (num_rx_msdus < 0) {
2638 resched_napi = true;
2639 goto exit;
2640 }
2641
2642 dev_kfree_skb_any(skb);
2643 if (num_rx_msdus > 0)
2644 quota += num_rx_msdus;
2645
2646 if ((quota > ATH10K_NAPI_QUOTA_LIMIT) &&
2647 !skb_queue_empty(&htt->rx_in_ord_compl_q)) {
2648 resched_napi = true;
2649 goto exit;
2650 }
2651 }
2652
2653 while (quota < budget) {
2654 /* no more data to receive */
2655 if (!atomic_read(&htt->num_mpdus_ready))
2656 break;
2657
2658 num_rx_msdus = ath10k_htt_rx_handle_amsdu(htt);
2659 if (num_rx_msdus < 0) {
2660 resched_napi = true;
2661 goto exit;
2662 }
2663
2664 quota += num_rx_msdus;
2665 atomic_dec(&htt->num_mpdus_ready);
2666 if ((quota > ATH10K_NAPI_QUOTA_LIMIT) &&
2667 atomic_read(&htt->num_mpdus_ready)) {
2668 resched_napi = true;
2669 goto exit;
2670 }
2671 }
2672
2673 /* From NAPI documentation:
2674 * The napi poll() function may also process TX completions, in which
2675 * case if it processes the entire TX ring then it should count that
2676 * work as the rest of the budget.
2677 */
2678 if ((quota < budget) && !kfifo_is_empty(&htt->txdone_fifo))
2679 quota = budget;
Michal Kazior426e10e2016-03-06 16:14:43 +02002680
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302681 /* kfifo_get: called only within txrx_tasklet so it's neatly serialized.
2682 * From kfifo_get() documentation:
2683 * Note that with only one concurrent reader and one concurrent writer,
2684 * you don't need extra locking to use these macro.
2685 */
2686 while (kfifo_get(&htt->txdone_fifo, &tx_done))
2687 ath10k_txrx_tx_unref(htt, &tx_done);
Michal Kazior6c5151a2014-02-27 18:50:04 +02002688
Rajkumar Manoharan18f53fe2016-09-02 19:46:10 +03002689 ath10k_mac_tx_push_pending(ar);
2690
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002691 spin_lock_irqsave(&htt->tx_fetch_ind_q.lock, flags);
2692 skb_queue_splice_init(&htt->tx_fetch_ind_q, &tx_ind_q);
2693 spin_unlock_irqrestore(&htt->tx_fetch_ind_q.lock, flags);
2694
Michal Kazior426e10e2016-03-06 16:14:43 +02002695 while ((skb = __skb_dequeue(&tx_ind_q))) {
2696 ath10k_htt_rx_tx_fetch_ind(ar, skb);
Michal Kazior6c5151a2014-02-27 18:50:04 +02002697 dev_kfree_skb_any(skb);
2698 }
2699
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002700exit:
Rajkumar Manoharan5c86d972016-03-22 17:22:19 +05302701 ath10k_htt_rx_msdu_buff_replenish(htt);
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002702 /* In case of rx failure or more data to read, report budget
2703 * to reschedule NAPI poll
2704 */
2705 done = resched_napi ? budget : quota;
2706
2707 return done;
Michal Kazior6c5151a2014-02-27 18:50:04 +02002708}
Rajkumar Manoharan3c97f5d2016-09-02 19:46:09 +03002709EXPORT_SYMBOL(ath10k_htt_txrx_compl_task);