blob: ac16ce746afb6aacd6196659ac3252ba2ecc43b0 [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
Michal Kaziorf6dc2092013-09-26 10:12:22 +030034static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb);
Michal Kazior6c5151a2014-02-27 18:50:04 +020035static void ath10k_htt_txrx_compl_task(unsigned long ptr);
Michal Kaziorf6dc2092013-09-26 10:12:22 +030036
Michal Kaziorc5450702015-01-24 12:14:48 +020037static struct sk_buff *
38ath10k_htt_rx_find_skb_paddr(struct ath10k *ar, u32 paddr)
39{
40 struct ath10k_skb_rxcb *rxcb;
41
42 hash_for_each_possible(ar->htt.rx_ring.skb_table, rxcb, hlist, paddr)
43 if (rxcb->paddr == paddr)
44 return ATH10K_RXCB_SKB(rxcb);
45
46 WARN_ON_ONCE(1);
47 return NULL;
48}
49
Kalle Valo5e3dd152013-06-12 20:52:10 +030050static void ath10k_htt_rx_ring_free(struct ath10k_htt *htt)
51{
52 struct sk_buff *skb;
Michal Kaziorc5450702015-01-24 12:14:48 +020053 struct ath10k_skb_rxcb *rxcb;
54 struct hlist_node *n;
Kalle Valo5e3dd152013-06-12 20:52:10 +030055 int i;
56
Michal Kaziorc5450702015-01-24 12:14:48 +020057 if (htt->rx_ring.in_ord_rx) {
58 hash_for_each_safe(htt->rx_ring.skb_table, i, n, rxcb, hlist) {
59 skb = ATH10K_RXCB_SKB(rxcb);
60 dma_unmap_single(htt->ar->dev, rxcb->paddr,
61 skb->len + skb_tailroom(skb),
62 DMA_FROM_DEVICE);
63 hash_del(&rxcb->hlist);
64 dev_kfree_skb_any(skb);
65 }
66 } else {
67 for (i = 0; i < htt->rx_ring.size; i++) {
68 skb = htt->rx_ring.netbufs_ring[i];
69 if (!skb)
70 continue;
71
72 rxcb = ATH10K_SKB_RXCB(skb);
73 dma_unmap_single(htt->ar->dev, rxcb->paddr,
74 skb->len + skb_tailroom(skb),
75 DMA_FROM_DEVICE);
76 dev_kfree_skb_any(skb);
77 }
Kalle Valo5e3dd152013-06-12 20:52:10 +030078 }
79
80 htt->rx_ring.fill_cnt = 0;
Michal Kaziorc5450702015-01-24 12:14:48 +020081 hash_init(htt->rx_ring.skb_table);
82 memset(htt->rx_ring.netbufs_ring, 0,
83 htt->rx_ring.size * sizeof(htt->rx_ring.netbufs_ring[0]));
Kalle Valo5e3dd152013-06-12 20:52:10 +030084}
85
86static int __ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
87{
88 struct htt_rx_desc *rx_desc;
Michal Kaziorc5450702015-01-24 12:14:48 +020089 struct ath10k_skb_rxcb *rxcb;
Kalle Valo5e3dd152013-06-12 20:52:10 +030090 struct sk_buff *skb;
91 dma_addr_t paddr;
92 int ret = 0, idx;
93
Michal Kaziorc5450702015-01-24 12:14:48 +020094 /* The Full Rx Reorder firmware has no way of telling the host
95 * implicitly when it copied HTT Rx Ring buffers to MAC Rx Ring.
96 * To keep things simple make sure ring is always half empty. This
97 * guarantees there'll be no replenishment overruns possible.
98 */
99 BUILD_BUG_ON(HTT_RX_RING_FILL_LEVEL >= HTT_RX_RING_SIZE / 2);
100
Kalle Valo8cc7f262014-09-14 12:50:39 +0300101 idx = __le32_to_cpu(*htt->rx_ring.alloc_idx.vaddr);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300102 while (num > 0) {
103 skb = dev_alloc_skb(HTT_RX_BUF_SIZE + HTT_RX_DESC_ALIGN);
104 if (!skb) {
105 ret = -ENOMEM;
106 goto fail;
107 }
108
109 if (!IS_ALIGNED((unsigned long)skb->data, HTT_RX_DESC_ALIGN))
110 skb_pull(skb,
111 PTR_ALIGN(skb->data, HTT_RX_DESC_ALIGN) -
112 skb->data);
113
114 /* Clear rx_desc attention word before posting to Rx ring */
115 rx_desc = (struct htt_rx_desc *)skb->data;
116 rx_desc->attention.flags = __cpu_to_le32(0);
117
118 paddr = dma_map_single(htt->ar->dev, skb->data,
119 skb->len + skb_tailroom(skb),
120 DMA_FROM_DEVICE);
121
122 if (unlikely(dma_mapping_error(htt->ar->dev, paddr))) {
123 dev_kfree_skb_any(skb);
124 ret = -ENOMEM;
125 goto fail;
126 }
127
Michal Kaziorc5450702015-01-24 12:14:48 +0200128 rxcb = ATH10K_SKB_RXCB(skb);
129 rxcb->paddr = paddr;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300130 htt->rx_ring.netbufs_ring[idx] = skb;
131 htt->rx_ring.paddrs_ring[idx] = __cpu_to_le32(paddr);
132 htt->rx_ring.fill_cnt++;
133
Michal Kaziorc5450702015-01-24 12:14:48 +0200134 if (htt->rx_ring.in_ord_rx) {
135 hash_add(htt->rx_ring.skb_table,
136 &ATH10K_SKB_RXCB(skb)->hlist,
137 (u32)paddr);
138 }
139
Kalle Valo5e3dd152013-06-12 20:52:10 +0300140 num--;
141 idx++;
142 idx &= htt->rx_ring.size_mask;
143 }
144
145fail:
Vasanthakumar Thiagarajan5de6dfc2015-01-09 22:49:46 +0530146 /*
147 * Make sure the rx buffer is updated before available buffer
148 * index to avoid any potential rx ring corruption.
149 */
150 mb();
Kalle Valo8cc7f262014-09-14 12:50:39 +0300151 *htt->rx_ring.alloc_idx.vaddr = __cpu_to_le32(idx);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300152 return ret;
153}
154
155static int ath10k_htt_rx_ring_fill_n(struct ath10k_htt *htt, int num)
156{
157 lockdep_assert_held(&htt->rx_ring.lock);
158 return __ath10k_htt_rx_ring_fill_n(htt, num);
159}
160
161static void ath10k_htt_rx_msdu_buff_replenish(struct ath10k_htt *htt)
162{
Michal Kazior6e712d42013-09-24 10:18:36 +0200163 int ret, num_deficit, num_to_fill;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300164
Michal Kazior6e712d42013-09-24 10:18:36 +0200165 /* Refilling the whole RX ring buffer proves to be a bad idea. The
166 * reason is RX may take up significant amount of CPU cycles and starve
167 * other tasks, e.g. TX on an ethernet device while acting as a bridge
168 * with ath10k wlan interface. This ended up with very poor performance
169 * once CPU the host system was overwhelmed with RX on ath10k.
170 *
171 * By limiting the number of refills the replenishing occurs
172 * progressively. This in turns makes use of the fact tasklets are
173 * processed in FIFO order. This means actual RX processing can starve
174 * out refilling. If there's not enough buffers on RX ring FW will not
175 * report RX until it is refilled with enough buffers. This
176 * automatically balances load wrt to CPU power.
177 *
178 * This probably comes at a cost of lower maximum throughput but
Ben Greear3eafdfd2015-02-15 16:50:39 +0200179 * improves the average and stability. */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300180 spin_lock_bh(&htt->rx_ring.lock);
Michal Kazior6e712d42013-09-24 10:18:36 +0200181 num_deficit = htt->rx_ring.fill_level - htt->rx_ring.fill_cnt;
182 num_to_fill = min(ATH10K_HTT_MAX_NUM_REFILL, num_deficit);
183 num_deficit -= num_to_fill;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300184 ret = ath10k_htt_rx_ring_fill_n(htt, num_to_fill);
185 if (ret == -ENOMEM) {
186 /*
187 * Failed to fill it to the desired level -
188 * we'll start a timer and try again next time.
189 * As long as enough buffers are left in the ring for
190 * another A-MPDU rx, no special recovery is needed.
191 */
192 mod_timer(&htt->rx_ring.refill_retry_timer, jiffies +
193 msecs_to_jiffies(HTT_RX_RING_REFILL_RETRY_MS));
Michal Kazior6e712d42013-09-24 10:18:36 +0200194 } else if (num_deficit > 0) {
195 tasklet_schedule(&htt->rx_replenish_task);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300196 }
197 spin_unlock_bh(&htt->rx_ring.lock);
198}
199
200static void ath10k_htt_rx_ring_refill_retry(unsigned long arg)
201{
202 struct ath10k_htt *htt = (struct ath10k_htt *)arg;
Kalle Valoaf762c02014-09-14 12:50:17 +0300203
Kalle Valo5e3dd152013-06-12 20:52:10 +0300204 ath10k_htt_rx_msdu_buff_replenish(htt);
205}
206
Michal Kaziorc5450702015-01-24 12:14:48 +0200207int ath10k_htt_rx_ring_refill(struct ath10k *ar)
Michal Kazior3e841fd2014-05-14 16:23:31 +0300208{
Michal Kaziorc5450702015-01-24 12:14:48 +0200209 struct ath10k_htt *htt = &ar->htt;
210 int ret;
Michal Kazior3e841fd2014-05-14 16:23:31 +0300211
Michal Kaziorc5450702015-01-24 12:14:48 +0200212 spin_lock_bh(&htt->rx_ring.lock);
213 ret = ath10k_htt_rx_ring_fill_n(htt, (htt->rx_ring.fill_level -
214 htt->rx_ring.fill_cnt));
215 spin_unlock_bh(&htt->rx_ring.lock);
Michal Kazior3e841fd2014-05-14 16:23:31 +0300216
Michal Kaziorc5450702015-01-24 12:14:48 +0200217 if (ret)
218 ath10k_htt_rx_ring_free(htt);
219
220 return ret;
Michal Kazior3e841fd2014-05-14 16:23:31 +0300221}
222
Michal Kazior95bf21f2014-05-16 17:15:39 +0300223void ath10k_htt_rx_free(struct ath10k_htt *htt)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300224{
Kalle Valo5e3dd152013-06-12 20:52:10 +0300225 del_timer_sync(&htt->rx_ring.refill_retry_timer);
Michal Kazior6e712d42013-09-24 10:18:36 +0200226 tasklet_kill(&htt->rx_replenish_task);
Michal Kazior6c5151a2014-02-27 18:50:04 +0200227 tasklet_kill(&htt->txrx_compl_task);
228
Michal Kazior6c5151a2014-02-27 18:50:04 +0200229 skb_queue_purge(&htt->rx_compl_q);
Michal Kaziorc5450702015-01-24 12:14:48 +0200230 skb_queue_purge(&htt->rx_in_ord_compl_q);
Michal Kazior426e10e2016-03-06 16:14:43 +0200231 skb_queue_purge(&htt->tx_fetch_ind_q);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300232
Michal Kaziorc5450702015-01-24 12:14:48 +0200233 ath10k_htt_rx_ring_free(htt);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300234
235 dma_free_coherent(htt->ar->dev,
236 (htt->rx_ring.size *
237 sizeof(htt->rx_ring.paddrs_ring)),
238 htt->rx_ring.paddrs_ring,
239 htt->rx_ring.base_paddr);
240
241 dma_free_coherent(htt->ar->dev,
242 sizeof(*htt->rx_ring.alloc_idx.vaddr),
243 htt->rx_ring.alloc_idx.vaddr,
244 htt->rx_ring.alloc_idx.paddr);
245
246 kfree(htt->rx_ring.netbufs_ring);
247}
248
249static inline struct sk_buff *ath10k_htt_rx_netbuf_pop(struct ath10k_htt *htt)
250{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200251 struct ath10k *ar = htt->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300252 int idx;
253 struct sk_buff *msdu;
254
Michal Kazior45967082014-02-27 18:50:05 +0200255 lockdep_assert_held(&htt->rx_ring.lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300256
Michal Kazior8d60ee82014-02-27 18:50:05 +0200257 if (htt->rx_ring.fill_cnt == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200258 ath10k_warn(ar, "tried to pop sk_buff from an empty rx ring\n");
Michal Kazior8d60ee82014-02-27 18:50:05 +0200259 return NULL;
260 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300261
262 idx = htt->rx_ring.sw_rd_idx.msdu_payld;
263 msdu = htt->rx_ring.netbufs_ring[idx];
Michal Kazior3e841fd2014-05-14 16:23:31 +0300264 htt->rx_ring.netbufs_ring[idx] = NULL;
Michal Kaziorc5450702015-01-24 12:14:48 +0200265 htt->rx_ring.paddrs_ring[idx] = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300266
267 idx++;
268 idx &= htt->rx_ring.size_mask;
269 htt->rx_ring.sw_rd_idx.msdu_payld = idx;
270 htt->rx_ring.fill_cnt--;
271
Michal Kazior4de02802014-10-23 17:04:23 +0300272 dma_unmap_single(htt->ar->dev,
Michal Kazior8582bf32015-01-24 12:14:47 +0200273 ATH10K_SKB_RXCB(msdu)->paddr,
Michal Kazior4de02802014-10-23 17:04:23 +0300274 msdu->len + skb_tailroom(msdu),
275 DMA_FROM_DEVICE);
276 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
277 msdu->data, msdu->len + skb_tailroom(msdu));
Michal Kazior4de02802014-10-23 17:04:23 +0300278
Kalle Valo5e3dd152013-06-12 20:52:10 +0300279 return msdu;
280}
281
Janusz Dziedzicd84dd602014-03-24 21:23:20 +0100282/* return: < 0 fatal error, 0 - non chained msdu, 1 chained msdu */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300283static int ath10k_htt_rx_amsdu_pop(struct ath10k_htt *htt,
Michal Kaziorf0e27702014-11-18 09:24:49 +0200284 struct sk_buff_head *amsdu)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300285{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200286 struct ath10k *ar = htt->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300287 int msdu_len, msdu_chaining = 0;
Michal Kazior9aa505d2014-11-18 09:24:47 +0200288 struct sk_buff *msdu;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300289 struct htt_rx_desc *rx_desc;
290
Michal Kazior45967082014-02-27 18:50:05 +0200291 lockdep_assert_held(&htt->rx_ring.lock);
292
Michal Kazior9aa505d2014-11-18 09:24:47 +0200293 for (;;) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300294 int last_msdu, msdu_len_invalid, msdu_chained;
295
Michal Kazior9aa505d2014-11-18 09:24:47 +0200296 msdu = ath10k_htt_rx_netbuf_pop(htt);
297 if (!msdu) {
Michal Kazior9aa505d2014-11-18 09:24:47 +0200298 __skb_queue_purge(amsdu);
Michal Kaziore0bd7512014-11-18 09:24:48 +0200299 return -ENOENT;
Michal Kazior9aa505d2014-11-18 09:24:47 +0200300 }
301
302 __skb_queue_tail(amsdu, msdu);
303
Kalle Valo5e3dd152013-06-12 20:52:10 +0300304 rx_desc = (struct htt_rx_desc *)msdu->data;
305
306 /* FIXME: we must report msdu payload since this is what caller
307 * expects now */
308 skb_put(msdu, offsetof(struct htt_rx_desc, msdu_payload));
309 skb_pull(msdu, offsetof(struct htt_rx_desc, msdu_payload));
310
311 /*
312 * Sanity check - confirm the HW is finished filling in the
313 * rx data.
314 * If the HW and SW are working correctly, then it's guaranteed
315 * that the HW's MAC DMA is done before this point in the SW.
316 * To prevent the case that we handle a stale Rx descriptor,
317 * just assert for now until we have a way to recover.
318 */
319 if (!(__le32_to_cpu(rx_desc->attention.flags)
320 & RX_ATTENTION_FLAGS_MSDU_DONE)) {
Michal Kazior9aa505d2014-11-18 09:24:47 +0200321 __skb_queue_purge(amsdu);
Michal Kaziore0bd7512014-11-18 09:24:48 +0200322 return -EIO;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300323 }
324
Kalle Valo5e3dd152013-06-12 20:52:10 +0300325 msdu_len_invalid = !!(__le32_to_cpu(rx_desc->attention.flags)
326 & (RX_ATTENTION_FLAGS_MPDU_LENGTH_ERR |
327 RX_ATTENTION_FLAGS_MSDU_LENGTH_ERR));
Peter Oh1f5dbfb2015-07-15 19:01:21 -0700328 msdu_len = MS(__le32_to_cpu(rx_desc->msdu_start.common.info0),
Kalle Valo5e3dd152013-06-12 20:52:10 +0300329 RX_MSDU_START_INFO0_MSDU_LENGTH);
330 msdu_chained = rx_desc->frag_info.ring2_more_count;
331
332 if (msdu_len_invalid)
333 msdu_len = 0;
334
335 skb_trim(msdu, 0);
336 skb_put(msdu, min(msdu_len, HTT_RX_MSDU_SIZE));
337 msdu_len -= msdu->len;
338
Michal Kazior9aa505d2014-11-18 09:24:47 +0200339 /* Note: Chained buffers do not contain rx descriptor */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300340 while (msdu_chained--) {
Michal Kazior9aa505d2014-11-18 09:24:47 +0200341 msdu = ath10k_htt_rx_netbuf_pop(htt);
342 if (!msdu) {
Michal Kazior9aa505d2014-11-18 09:24:47 +0200343 __skb_queue_purge(amsdu);
Michal Kaziore0bd7512014-11-18 09:24:48 +0200344 return -ENOENT;
Michal Kaziorb30595a2014-10-23 17:04:24 +0300345 }
346
Michal Kazior9aa505d2014-11-18 09:24:47 +0200347 __skb_queue_tail(amsdu, msdu);
348 skb_trim(msdu, 0);
349 skb_put(msdu, min(msdu_len, HTT_RX_BUF_SIZE));
350 msdu_len -= msdu->len;
Michal Kaziorede9c8e2014-05-14 16:23:31 +0300351 msdu_chaining = 1;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300352 }
353
Peter Oh1f5dbfb2015-07-15 19:01:21 -0700354 last_msdu = __le32_to_cpu(rx_desc->msdu_end.common.info0) &
Kalle Valo5e3dd152013-06-12 20:52:10 +0300355 RX_MSDU_END_INFO0_LAST_MSDU;
356
Michal Kaziorb04e2042014-10-23 17:04:27 +0300357 trace_ath10k_htt_rx_desc(ar, &rx_desc->attention,
Rajkumar Manoharana0883cf2014-10-03 08:02:47 +0300358 sizeof(*rx_desc) - sizeof(u32));
Michal Kazior9aa505d2014-11-18 09:24:47 +0200359
360 if (last_msdu)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300361 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300362 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300363
Michal Kazior9aa505d2014-11-18 09:24:47 +0200364 if (skb_queue_empty(amsdu))
Janusz Dziedzicd84dd602014-03-24 21:23:20 +0100365 msdu_chaining = -1;
366
Kalle Valo5e3dd152013-06-12 20:52:10 +0300367 /*
368 * Don't refill the ring yet.
369 *
370 * First, the elements popped here are still in use - it is not
371 * safe to overwrite them until the matching call to
372 * mpdu_desc_list_next. Second, for efficiency it is preferable to
373 * refill the rx ring with 1 PPDU's worth of rx buffers (something
374 * like 32 x 3 buffers), rather than one MPDU's worth of rx buffers
375 * (something like 3 buffers). Consequently, we'll rely on the txrx
376 * SW to tell us when it is done pulling all the PPDU's rx buffers
377 * out of the rx ring, and then refill it just once.
378 */
379
380 return msdu_chaining;
381}
382
Michal Kazior6e712d42013-09-24 10:18:36 +0200383static void ath10k_htt_rx_replenish_task(unsigned long ptr)
384{
385 struct ath10k_htt *htt = (struct ath10k_htt *)ptr;
Kalle Valoaf762c02014-09-14 12:50:17 +0300386
Michal Kazior6e712d42013-09-24 10:18:36 +0200387 ath10k_htt_rx_msdu_buff_replenish(htt);
388}
389
Michal Kaziorc5450702015-01-24 12:14:48 +0200390static struct sk_buff *ath10k_htt_rx_pop_paddr(struct ath10k_htt *htt,
391 u32 paddr)
392{
393 struct ath10k *ar = htt->ar;
394 struct ath10k_skb_rxcb *rxcb;
395 struct sk_buff *msdu;
396
397 lockdep_assert_held(&htt->rx_ring.lock);
398
399 msdu = ath10k_htt_rx_find_skb_paddr(ar, paddr);
400 if (!msdu)
401 return NULL;
402
403 rxcb = ATH10K_SKB_RXCB(msdu);
404 hash_del(&rxcb->hlist);
405 htt->rx_ring.fill_cnt--;
406
407 dma_unmap_single(htt->ar->dev, rxcb->paddr,
408 msdu->len + skb_tailroom(msdu),
409 DMA_FROM_DEVICE);
410 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx netbuf pop: ",
411 msdu->data, msdu->len + skb_tailroom(msdu));
412
413 return msdu;
414}
415
416static int ath10k_htt_rx_pop_paddr_list(struct ath10k_htt *htt,
417 struct htt_rx_in_ord_ind *ev,
418 struct sk_buff_head *list)
419{
420 struct ath10k *ar = htt->ar;
421 struct htt_rx_in_ord_msdu_desc *msdu_desc = ev->msdu_descs;
422 struct htt_rx_desc *rxd;
423 struct sk_buff *msdu;
424 int msdu_count;
425 bool is_offload;
426 u32 paddr;
427
428 lockdep_assert_held(&htt->rx_ring.lock);
429
430 msdu_count = __le16_to_cpu(ev->msdu_count);
431 is_offload = !!(ev->info & HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
432
433 while (msdu_count--) {
434 paddr = __le32_to_cpu(msdu_desc->msdu_paddr);
435
436 msdu = ath10k_htt_rx_pop_paddr(htt, paddr);
437 if (!msdu) {
438 __skb_queue_purge(list);
439 return -ENOENT;
440 }
441
442 __skb_queue_tail(list, msdu);
443
444 if (!is_offload) {
445 rxd = (void *)msdu->data;
446
447 trace_ath10k_htt_rx_desc(ar, rxd, sizeof(*rxd));
448
449 skb_put(msdu, sizeof(*rxd));
450 skb_pull(msdu, sizeof(*rxd));
451 skb_put(msdu, __le16_to_cpu(msdu_desc->msdu_len));
452
453 if (!(__le32_to_cpu(rxd->attention.flags) &
454 RX_ATTENTION_FLAGS_MSDU_DONE)) {
455 ath10k_warn(htt->ar, "tried to pop an incomplete frame, oops!\n");
456 return -EIO;
457 }
458 }
459
460 msdu_desc++;
461 }
462
463 return 0;
464}
465
Michal Kazior95bf21f2014-05-16 17:15:39 +0300466int ath10k_htt_rx_alloc(struct ath10k_htt *htt)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300467{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200468 struct ath10k *ar = htt->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300469 dma_addr_t paddr;
470 void *vaddr;
Kalle Valobd8bdbb2014-09-14 12:50:00 +0300471 size_t size;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300472 struct timer_list *timer = &htt->rx_ring.refill_retry_timer;
473
Michal Kazior51fc7d72014-10-23 17:04:24 +0300474 htt->rx_confused = false;
475
Michal Kaziorfe2407a2014-11-27 11:12:43 +0100476 /* XXX: The fill level could be changed during runtime in response to
477 * the host processing latency. Is this really worth it?
478 */
479 htt->rx_ring.size = HTT_RX_RING_SIZE;
480 htt->rx_ring.size_mask = htt->rx_ring.size - 1;
481 htt->rx_ring.fill_level = HTT_RX_RING_FILL_LEVEL;
482
Kalle Valo5e3dd152013-06-12 20:52:10 +0300483 if (!is_power_of_2(htt->rx_ring.size)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200484 ath10k_warn(ar, "htt rx ring size is not power of 2\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300485 return -EINVAL;
486 }
487
Kalle Valo5e3dd152013-06-12 20:52:10 +0300488 htt->rx_ring.netbufs_ring =
Michal Kazior3e841fd2014-05-14 16:23:31 +0300489 kzalloc(htt->rx_ring.size * sizeof(struct sk_buff *),
Kalle Valo5e3dd152013-06-12 20:52:10 +0300490 GFP_KERNEL);
491 if (!htt->rx_ring.netbufs_ring)
492 goto err_netbuf;
493
Kalle Valobd8bdbb2014-09-14 12:50:00 +0300494 size = htt->rx_ring.size * sizeof(htt->rx_ring.paddrs_ring);
495
Felix Fietkaud6cb23b52015-11-24 11:36:52 +0100496 vaddr = dma_alloc_coherent(htt->ar->dev, size, &paddr, GFP_KERNEL);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300497 if (!vaddr)
498 goto err_dma_ring;
499
500 htt->rx_ring.paddrs_ring = vaddr;
501 htt->rx_ring.base_paddr = paddr;
502
503 vaddr = dma_alloc_coherent(htt->ar->dev,
504 sizeof(*htt->rx_ring.alloc_idx.vaddr),
Felix Fietkaud6cb23b52015-11-24 11:36:52 +0100505 &paddr, GFP_KERNEL);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300506 if (!vaddr)
507 goto err_dma_idx;
508
509 htt->rx_ring.alloc_idx.vaddr = vaddr;
510 htt->rx_ring.alloc_idx.paddr = paddr;
Michal Kaziorc5450702015-01-24 12:14:48 +0200511 htt->rx_ring.sw_rd_idx.msdu_payld = htt->rx_ring.size_mask;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300512 *htt->rx_ring.alloc_idx.vaddr = 0;
513
514 /* Initialize the Rx refill retry timer */
515 setup_timer(timer, ath10k_htt_rx_ring_refill_retry, (unsigned long)htt);
516
517 spin_lock_init(&htt->rx_ring.lock);
518
519 htt->rx_ring.fill_cnt = 0;
Michal Kaziorc5450702015-01-24 12:14:48 +0200520 htt->rx_ring.sw_rd_idx.msdu_payld = 0;
521 hash_init(htt->rx_ring.skb_table);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300522
Michal Kazior6e712d42013-09-24 10:18:36 +0200523 tasklet_init(&htt->rx_replenish_task, ath10k_htt_rx_replenish_task,
524 (unsigned long)htt);
525
Michal Kazior6c5151a2014-02-27 18:50:04 +0200526 skb_queue_head_init(&htt->rx_compl_q);
Michal Kaziorc5450702015-01-24 12:14:48 +0200527 skb_queue_head_init(&htt->rx_in_ord_compl_q);
Michal Kazior426e10e2016-03-06 16:14:43 +0200528 skb_queue_head_init(&htt->tx_fetch_ind_q);
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +0530529 atomic_set(&htt->num_mpdus_ready, 0);
Michal Kazior6c5151a2014-02-27 18:50:04 +0200530
531 tasklet_init(&htt->txrx_compl_task, ath10k_htt_txrx_compl_task,
532 (unsigned long)htt);
533
Michal Kazior7aa7a722014-08-25 12:09:38 +0200534 ath10k_dbg(ar, ATH10K_DBG_BOOT, "htt rx ring size %d fill_level %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300535 htt->rx_ring.size, htt->rx_ring.fill_level);
536 return 0;
537
Kalle Valo5e3dd152013-06-12 20:52:10 +0300538err_dma_idx:
539 dma_free_coherent(htt->ar->dev,
540 (htt->rx_ring.size *
541 sizeof(htt->rx_ring.paddrs_ring)),
542 htt->rx_ring.paddrs_ring,
543 htt->rx_ring.base_paddr);
544err_dma_ring:
545 kfree(htt->rx_ring.netbufs_ring);
546err_netbuf:
547 return -ENOMEM;
548}
549
Michal Kazior7aa7a722014-08-25 12:09:38 +0200550static int ath10k_htt_rx_crypto_param_len(struct ath10k *ar,
551 enum htt_rx_mpdu_encrypt_type type)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300552{
553 switch (type) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300554 case HTT_RX_MPDU_ENCRYPT_NONE:
555 return 0;
Michal Kazior890d3b22014-10-23 17:04:22 +0300556 case HTT_RX_MPDU_ENCRYPT_WEP40:
557 case HTT_RX_MPDU_ENCRYPT_WEP104:
558 return IEEE80211_WEP_IV_LEN;
559 case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
560 case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
561 return IEEE80211_TKIP_IV_LEN;
562 case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
563 return IEEE80211_CCMP_HDR_LEN;
564 case HTT_RX_MPDU_ENCRYPT_WEP128:
565 case HTT_RX_MPDU_ENCRYPT_WAPI:
566 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300567 }
568
Michal Kazior890d3b22014-10-23 17:04:22 +0300569 ath10k_warn(ar, "unsupported encryption type %d\n", type);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300570 return 0;
571}
572
Michal Kazior890d3b22014-10-23 17:04:22 +0300573#define MICHAEL_MIC_LEN 8
574
Michal Kazior7aa7a722014-08-25 12:09:38 +0200575static int ath10k_htt_rx_crypto_tail_len(struct ath10k *ar,
576 enum htt_rx_mpdu_encrypt_type type)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300577{
578 switch (type) {
579 case HTT_RX_MPDU_ENCRYPT_NONE:
Michal Kazior890d3b22014-10-23 17:04:22 +0300580 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300581 case HTT_RX_MPDU_ENCRYPT_WEP40:
582 case HTT_RX_MPDU_ENCRYPT_WEP104:
Michal Kazior890d3b22014-10-23 17:04:22 +0300583 return IEEE80211_WEP_ICV_LEN;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300584 case HTT_RX_MPDU_ENCRYPT_TKIP_WITHOUT_MIC:
585 case HTT_RX_MPDU_ENCRYPT_TKIP_WPA:
Michal Kazior890d3b22014-10-23 17:04:22 +0300586 return IEEE80211_TKIP_ICV_LEN;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300587 case HTT_RX_MPDU_ENCRYPT_AES_CCM_WPA2:
Michal Kazior890d3b22014-10-23 17:04:22 +0300588 return IEEE80211_CCMP_MIC_LEN;
589 case HTT_RX_MPDU_ENCRYPT_WEP128:
590 case HTT_RX_MPDU_ENCRYPT_WAPI:
591 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300592 }
593
Michal Kazior890d3b22014-10-23 17:04:22 +0300594 ath10k_warn(ar, "unsupported encryption type %d\n", type);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300595 return 0;
596}
597
Michal Kaziorf6dc2092013-09-26 10:12:22 +0300598struct amsdu_subframe_hdr {
599 u8 dst[ETH_ALEN];
600 u8 src[ETH_ALEN];
601 __be16 len;
602} __packed;
603
Michal Kazior6986fdd2015-08-27 14:47:33 +0200604#define GROUP_ID_IS_SU_MIMO(x) ((x) == 0 || (x) == 63)
605
Janusz Dziedzic87326c92014-03-24 21:23:19 +0100606static void ath10k_htt_rx_h_rates(struct ath10k *ar,
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200607 struct ieee80211_rx_status *status,
608 struct htt_rx_desc *rxd)
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100609{
Michal Kazior5528e032015-03-30 09:51:56 +0300610 struct ieee80211_supported_band *sband;
611 u8 cck, rate, bw, sgi, mcs, nss;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100612 u8 preamble = 0;
Michal Kazior6986fdd2015-08-27 14:47:33 +0200613 u8 group_id;
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200614 u32 info1, info2, info3;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100615
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200616 info1 = __le32_to_cpu(rxd->ppdu_start.info1);
617 info2 = __le32_to_cpu(rxd->ppdu_start.info2);
618 info3 = __le32_to_cpu(rxd->ppdu_start.info3);
619
620 preamble = MS(info1, RX_PPDU_START_INFO1_PREAMBLE_TYPE);
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100621
622 switch (preamble) {
623 case HTT_RX_LEGACY:
Michal Kazior5528e032015-03-30 09:51:56 +0300624 /* To get legacy rate index band is required. Since band can't
625 * be undefined check if freq is non-zero.
626 */
627 if (!status->freq)
628 return;
629
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200630 cck = info1 & RX_PPDU_START_INFO1_L_SIG_RATE_SELECT;
631 rate = MS(info1, RX_PPDU_START_INFO1_L_SIG_RATE);
Michal Kazior5528e032015-03-30 09:51:56 +0300632 rate &= ~RX_PPDU_START_RATE_FLAG;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100633
Michal Kazior5528e032015-03-30 09:51:56 +0300634 sband = &ar->mac.sbands[status->band];
Yanbo Li4b7f3532015-11-12 10:36:10 -0800635 status->rate_idx = ath10k_mac_hw_rate_to_idx(sband, rate, cck);
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100636 break;
637 case HTT_RX_HT:
638 case HTT_RX_HT_WITH_TXBF:
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200639 /* HT-SIG - Table 20-11 in info2 and info3 */
640 mcs = info2 & 0x1F;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100641 nss = mcs >> 3;
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200642 bw = (info2 >> 7) & 1;
643 sgi = (info3 >> 7) & 1;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100644
645 status->rate_idx = mcs;
646 status->flag |= RX_FLAG_HT;
647 if (sgi)
648 status->flag |= RX_FLAG_SHORT_GI;
649 if (bw)
650 status->flag |= RX_FLAG_40MHZ;
651 break;
652 case HTT_RX_VHT:
653 case HTT_RX_VHT_WITH_TXBF:
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200654 /* VHT-SIG-A1 in info2, VHT-SIG-A2 in info3
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100655 TODO check this */
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;
702 status->vht_nss = nss;
703
704 if (sgi)
705 status->flag |= RX_FLAG_SHORT_GI;
706
707 switch (bw) {
708 /* 20MHZ */
709 case 0:
710 break;
711 /* 40MHZ */
712 case 1:
713 status->flag |= RX_FLAG_40MHZ;
714 break;
715 /* 80MHZ */
716 case 2:
717 status->vht_flag |= RX_VHT_FLAG_80MHZ;
718 }
719
720 status->flag |= RX_FLAG_VHT;
721 break;
722 default:
723 break;
724 }
725}
726
Michal Kazior500ff9f2015-03-31 10:26:21 +0000727static struct ieee80211_channel *
728ath10k_htt_rx_h_peer_channel(struct ath10k *ar, struct htt_rx_desc *rxd)
729{
730 struct ath10k_peer *peer;
731 struct ath10k_vif *arvif;
732 struct cfg80211_chan_def def;
733 u16 peer_id;
734
735 lockdep_assert_held(&ar->data_lock);
736
737 if (!rxd)
738 return NULL;
739
740 if (rxd->attention.flags &
741 __cpu_to_le32(RX_ATTENTION_FLAGS_PEER_IDX_INVALID))
742 return NULL;
743
Peter Oh1f5dbfb2015-07-15 19:01:21 -0700744 if (!(rxd->msdu_end.common.info0 &
Michal Kazior500ff9f2015-03-31 10:26:21 +0000745 __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU)))
746 return NULL;
747
748 peer_id = MS(__le32_to_cpu(rxd->mpdu_start.info0),
749 RX_MPDU_START_INFO0_PEER_IDX);
750
751 peer = ath10k_peer_find_by_id(ar, peer_id);
752 if (!peer)
753 return NULL;
754
755 arvif = ath10k_get_arvif(ar, peer->vdev_id);
756 if (WARN_ON_ONCE(!arvif))
757 return NULL;
758
759 if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
760 return NULL;
761
762 return def.chan;
763}
764
765static struct ieee80211_channel *
766ath10k_htt_rx_h_vdev_channel(struct ath10k *ar, u32 vdev_id)
767{
768 struct ath10k_vif *arvif;
769 struct cfg80211_chan_def def;
770
771 lockdep_assert_held(&ar->data_lock);
772
773 list_for_each_entry(arvif, &ar->arvifs, list) {
774 if (arvif->vdev_id == vdev_id &&
775 ath10k_mac_vif_chan(arvif->vif, &def) == 0)
776 return def.chan;
777 }
778
779 return NULL;
780}
781
782static void
783ath10k_htt_rx_h_any_chan_iter(struct ieee80211_hw *hw,
784 struct ieee80211_chanctx_conf *conf,
785 void *data)
786{
787 struct cfg80211_chan_def *def = data;
788
789 *def = conf->def;
790}
791
792static struct ieee80211_channel *
793ath10k_htt_rx_h_any_channel(struct ath10k *ar)
794{
795 struct cfg80211_chan_def def = {};
796
797 ieee80211_iter_chan_contexts_atomic(ar->hw,
798 ath10k_htt_rx_h_any_chan_iter,
799 &def);
800
801 return def.chan;
802}
803
Janusz Dziedzic36653f02014-03-24 21:23:18 +0100804static bool ath10k_htt_rx_h_channel(struct ath10k *ar,
Michal Kazior500ff9f2015-03-31 10:26:21 +0000805 struct ieee80211_rx_status *status,
806 struct htt_rx_desc *rxd,
807 u32 vdev_id)
Janusz Dziedzic36653f02014-03-24 21:23:18 +0100808{
809 struct ieee80211_channel *ch;
810
811 spin_lock_bh(&ar->data_lock);
812 ch = ar->scan_channel;
813 if (!ch)
814 ch = ar->rx_channel;
Michal Kazior500ff9f2015-03-31 10:26:21 +0000815 if (!ch)
816 ch = ath10k_htt_rx_h_peer_channel(ar, rxd);
817 if (!ch)
818 ch = ath10k_htt_rx_h_vdev_channel(ar, vdev_id);
819 if (!ch)
820 ch = ath10k_htt_rx_h_any_channel(ar);
Rajkumar Manoharan2ce9b252016-03-08 22:57:23 +0530821 if (!ch)
822 ch = ar->tgt_oper_chan;
Janusz Dziedzic36653f02014-03-24 21:23:18 +0100823 spin_unlock_bh(&ar->data_lock);
824
825 if (!ch)
826 return false;
827
828 status->band = ch->band;
829 status->freq = ch->center_freq;
830
831 return true;
832}
833
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200834static void ath10k_htt_rx_h_signal(struct ath10k *ar,
835 struct ieee80211_rx_status *status,
836 struct htt_rx_desc *rxd)
837{
838 /* FIXME: Get real NF */
839 status->signal = ATH10K_DEFAULT_NOISE_FLOOR +
840 rxd->ppdu_start.rssi_comb;
841 status->flag &= ~RX_FLAG_NO_SIGNAL_VAL;
842}
843
844static void ath10k_htt_rx_h_mactime(struct ath10k *ar,
845 struct ieee80211_rx_status *status,
846 struct htt_rx_desc *rxd)
847{
848 /* FIXME: TSF is known only at the end of PPDU, in the last MPDU. This
849 * means all prior MSDUs in a PPDU are reported to mac80211 without the
850 * TSF. Is it worth holding frames until end of PPDU is known?
851 *
852 * FIXME: Can we get/compute 64bit TSF?
853 */
Michal Kazior3ec79e32015-01-24 12:14:48 +0200854 status->mactime = __le32_to_cpu(rxd->ppdu_end.common.tsf_timestamp);
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200855 status->flag |= RX_FLAG_MACTIME_END;
856}
857
858static void ath10k_htt_rx_h_ppdu(struct ath10k *ar,
859 struct sk_buff_head *amsdu,
Michal Kazior500ff9f2015-03-31 10:26:21 +0000860 struct ieee80211_rx_status *status,
861 u32 vdev_id)
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200862{
863 struct sk_buff *first;
864 struct htt_rx_desc *rxd;
865 bool is_first_ppdu;
866 bool is_last_ppdu;
867
868 if (skb_queue_empty(amsdu))
869 return;
870
871 first = skb_peek(amsdu);
872 rxd = (void *)first->data - sizeof(*rxd);
873
874 is_first_ppdu = !!(rxd->attention.flags &
875 __cpu_to_le32(RX_ATTENTION_FLAGS_FIRST_MPDU));
876 is_last_ppdu = !!(rxd->attention.flags &
877 __cpu_to_le32(RX_ATTENTION_FLAGS_LAST_MPDU));
878
879 if (is_first_ppdu) {
880 /* New PPDU starts so clear out the old per-PPDU status. */
881 status->freq = 0;
882 status->rate_idx = 0;
883 status->vht_nss = 0;
884 status->vht_flag &= ~RX_VHT_FLAG_80MHZ;
885 status->flag &= ~(RX_FLAG_HT |
886 RX_FLAG_VHT |
887 RX_FLAG_SHORT_GI |
888 RX_FLAG_40MHZ |
889 RX_FLAG_MACTIME_END);
890 status->flag |= RX_FLAG_NO_SIGNAL_VAL;
891
892 ath10k_htt_rx_h_signal(ar, status, rxd);
Michal Kazior500ff9f2015-03-31 10:26:21 +0000893 ath10k_htt_rx_h_channel(ar, status, rxd, vdev_id);
Michal Kaziorb9fd8a82014-11-18 09:24:49 +0200894 ath10k_htt_rx_h_rates(ar, status, rxd);
895 }
896
897 if (is_last_ppdu)
898 ath10k_htt_rx_h_mactime(ar, status, rxd);
899}
900
Janusz Dziedzic76f53292014-07-28 23:59:43 +0300901static const char * const tid_to_ac[] = {
902 "BE",
903 "BK",
904 "BK",
905 "BE",
906 "VI",
907 "VI",
908 "VO",
909 "VO",
910};
911
912static char *ath10k_get_tid(struct ieee80211_hdr *hdr, char *out, size_t size)
913{
914 u8 *qc;
915 int tid;
916
917 if (!ieee80211_is_data_qos(hdr->frame_control))
918 return "";
919
920 qc = ieee80211_get_qos_ctl(hdr);
921 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
922 if (tid < 8)
923 snprintf(out, size, "tid %d (%s)", tid, tid_to_ac[tid]);
924 else
925 snprintf(out, size, "tid %d", tid);
926
927 return out;
928}
929
Janusz Dziedzic85f6d7c2014-03-24 21:23:22 +0100930static void ath10k_process_rx(struct ath10k *ar,
931 struct ieee80211_rx_status *rx_status,
932 struct sk_buff *skb)
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100933{
934 struct ieee80211_rx_status *status;
Janusz Dziedzic76f53292014-07-28 23:59:43 +0300935 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
936 char tid[32];
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100937
Janusz Dziedzic85f6d7c2014-03-24 21:23:22 +0100938 status = IEEE80211_SKB_RXCB(skb);
939 *status = *rx_status;
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100940
Michal Kazior7aa7a722014-08-25 12:09:38 +0200941 ath10k_dbg(ar, ATH10K_DBG_DATA,
Janusz Dziedzic76f53292014-07-28 23:59:43 +0300942 "rx skb %p len %u peer %pM %s %s sn %u %s%s%s%s%s %srate_idx %u vht_nss %u freq %u band %u flag 0x%x fcs-err %i mic-err %i amsdu-more %i\n",
Janusz Dziedzic85f6d7c2014-03-24 21:23:22 +0100943 skb,
944 skb->len,
Janusz Dziedzic76f53292014-07-28 23:59:43 +0300945 ieee80211_get_SA(hdr),
946 ath10k_get_tid(hdr, tid, sizeof(tid)),
947 is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
948 "mcast" : "ucast",
949 (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100950 status->flag == 0 ? "legacy" : "",
951 status->flag & RX_FLAG_HT ? "ht" : "",
952 status->flag & RX_FLAG_VHT ? "vht" : "",
953 status->flag & RX_FLAG_40MHZ ? "40" : "",
954 status->vht_flag & RX_VHT_FLAG_80MHZ ? "80" : "",
955 status->flag & RX_FLAG_SHORT_GI ? "sgi " : "",
956 status->rate_idx,
957 status->vht_nss,
958 status->freq,
Janusz Dziedzic87326c92014-03-24 21:23:19 +0100959 status->band, status->flag,
Janusz Dziedzic78433f92014-03-24 21:23:21 +0100960 !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
Janusz Dziedzic76f53292014-07-28 23:59:43 +0300961 !!(status->flag & RX_FLAG_MMIC_ERROR),
962 !!(status->flag & RX_FLAG_AMSDU_MORE));
Michal Kazior7aa7a722014-08-25 12:09:38 +0200963 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
Janusz Dziedzic85f6d7c2014-03-24 21:23:22 +0100964 skb->data, skb->len);
Rajkumar Manoharan5ce8e7f2014-11-05 19:14:31 +0530965 trace_ath10k_rx_hdr(ar, skb->data, skb->len);
966 trace_ath10k_rx_payload(ar, skb->data, skb->len);
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100967
Janusz Dziedzic85f6d7c2014-03-24 21:23:22 +0100968 ieee80211_rx(ar->hw, skb);
Janusz Dziedzic73539b42014-03-24 21:23:15 +0100969}
970
Michal Kazior48f4ca32015-05-19 14:09:34 +0200971static int ath10k_htt_rx_nwifi_hdrlen(struct ath10k *ar,
972 struct ieee80211_hdr *hdr)
Michal Kaziord960c362014-02-25 09:29:57 +0200973{
Michal Kazior48f4ca32015-05-19 14:09:34 +0200974 int len = ieee80211_hdrlen(hdr->frame_control);
975
976 if (!test_bit(ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING,
977 ar->fw_features))
978 len = round_up(len, 4);
979
980 return len;
Michal Kaziord960c362014-02-25 09:29:57 +0200981}
982
Michal Kazior581c25f2014-11-18 09:24:48 +0200983static void ath10k_htt_rx_h_undecap_raw(struct ath10k *ar,
984 struct sk_buff *msdu,
985 struct ieee80211_rx_status *status,
986 enum htt_rx_mpdu_encrypt_type enctype,
987 bool is_decrypted)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300988{
Michal Kaziorf6dc2092013-09-26 10:12:22 +0300989 struct ieee80211_hdr *hdr;
Michal Kazior581c25f2014-11-18 09:24:48 +0200990 struct htt_rx_desc *rxd;
991 size_t hdr_len;
992 size_t crypto_len;
993 bool is_first;
994 bool is_last;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300995
Michal Kazior581c25f2014-11-18 09:24:48 +0200996 rxd = (void *)msdu->data - sizeof(*rxd);
Peter Oh1f5dbfb2015-07-15 19:01:21 -0700997 is_first = !!(rxd->msdu_end.common.info0 &
Michal Kazior581c25f2014-11-18 09:24:48 +0200998 __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
Peter Oh1f5dbfb2015-07-15 19:01:21 -0700999 is_last = !!(rxd->msdu_end.common.info0 &
Michal Kazior581c25f2014-11-18 09:24:48 +02001000 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
Michal Kazior9aa505d2014-11-18 09:24:47 +02001001
Michal Kazior581c25f2014-11-18 09:24:48 +02001002 /* Delivered decapped frame:
1003 * [802.11 header]
1004 * [crypto param] <-- can be trimmed if !fcs_err &&
1005 * !decrypt_err && !peer_idx_invalid
1006 * [amsdu header] <-- only if A-MSDU
1007 * [rfc1042/llc]
1008 * [payload]
1009 * [FCS] <-- at end, needs to be trimmed
1010 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001011
Michal Kazior581c25f2014-11-18 09:24:48 +02001012 /* This probably shouldn't happen but warn just in case */
1013 if (unlikely(WARN_ON_ONCE(!is_first)))
1014 return;
1015
1016 /* This probably shouldn't happen but warn just in case */
1017 if (unlikely(WARN_ON_ONCE(!(is_first && is_last))))
1018 return;
1019
1020 skb_trim(msdu, msdu->len - FCS_LEN);
1021
1022 /* In most cases this will be true for sniffed frames. It makes sense
David Liuccec9032015-07-24 20:25:32 +03001023 * to deliver them as-is without stripping the crypto param. This is
1024 * necessary for software based decryption.
Michal Kazior581c25f2014-11-18 09:24:48 +02001025 *
1026 * If there's no error then the frame is decrypted. At least that is
1027 * the case for frames that come in via fragmented rx indication.
1028 */
1029 if (!is_decrypted)
1030 return;
1031
1032 /* The payload is decrypted so strip crypto params. Start from tail
1033 * since hdr is used to compute some stuff.
1034 */
1035
1036 hdr = (void *)msdu->data;
1037
1038 /* Tail */
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001039 if (status->flag & RX_FLAG_IV_STRIPPED)
1040 skb_trim(msdu, msdu->len -
1041 ath10k_htt_rx_crypto_tail_len(ar, enctype));
Michal Kazior581c25f2014-11-18 09:24:48 +02001042
1043 /* MMIC */
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001044 if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
1045 !ieee80211_has_morefrags(hdr->frame_control) &&
Michal Kazior581c25f2014-11-18 09:24:48 +02001046 enctype == HTT_RX_MPDU_ENCRYPT_TKIP_WPA)
1047 skb_trim(msdu, msdu->len - 8);
1048
1049 /* Head */
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001050 if (status->flag & RX_FLAG_IV_STRIPPED) {
1051 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1052 crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001053
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001054 memmove((void *)msdu->data + crypto_len,
1055 (void *)msdu->data, hdr_len);
1056 skb_pull(msdu, crypto_len);
1057 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001058}
1059
Michal Kazior581c25f2014-11-18 09:24:48 +02001060static void ath10k_htt_rx_h_undecap_nwifi(struct ath10k *ar,
1061 struct sk_buff *msdu,
1062 struct ieee80211_rx_status *status,
1063 const u8 first_hdr[64])
Kalle Valo5e3dd152013-06-12 20:52:10 +03001064{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001065 struct ieee80211_hdr *hdr;
Michal Kazior581c25f2014-11-18 09:24:48 +02001066 size_t hdr_len;
1067 u8 da[ETH_ALEN];
1068 u8 sa[ETH_ALEN];
Kalle Valo5e3dd152013-06-12 20:52:10 +03001069
Michal Kazior581c25f2014-11-18 09:24:48 +02001070 /* Delivered decapped frame:
1071 * [nwifi 802.11 header] <-- replaced with 802.11 hdr
1072 * [rfc1042/llc]
1073 *
1074 * Note: The nwifi header doesn't have QoS Control and is
1075 * (always?) a 3addr frame.
1076 *
1077 * Note2: There's no A-MSDU subframe header. Even if it's part
1078 * of an A-MSDU.
1079 */
1080
1081 /* pull decapped header and copy SA & DA */
Yanbo Lib8d55fc2015-11-16 22:22:02 +02001082 if ((ar->hw_params.hw_4addr_pad == ATH10K_HW_4ADDR_PAD_BEFORE) &&
1083 ieee80211_has_a4(((struct ieee80211_hdr *)first_hdr)->frame_control)) {
1084 /* The QCA99X0 4 address mode pad 2 bytes at the
1085 * beginning of MSDU
1086 */
1087 hdr = (struct ieee80211_hdr *)(msdu->data + 2);
1088 /* The skb length need be extended 2 as the 2 bytes at the tail
1089 * be excluded due to the padding
1090 */
1091 skb_put(msdu, 2);
1092 } else {
1093 hdr = (struct ieee80211_hdr *)(msdu->data);
1094 }
1095
Michal Kazior48f4ca32015-05-19 14:09:34 +02001096 hdr_len = ath10k_htt_rx_nwifi_hdrlen(ar, hdr);
Michal Kazior581c25f2014-11-18 09:24:48 +02001097 ether_addr_copy(da, ieee80211_get_DA(hdr));
1098 ether_addr_copy(sa, ieee80211_get_SA(hdr));
1099 skb_pull(msdu, hdr_len);
1100
1101 /* push original 802.11 header */
1102 hdr = (struct ieee80211_hdr *)first_hdr;
Michal Kaziore3fbf8d2013-09-26 10:12:23 +03001103 hdr_len = ieee80211_hdrlen(hdr->frame_control);
Michal Kazior581c25f2014-11-18 09:24:48 +02001104 memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001105
Michal Kazior581c25f2014-11-18 09:24:48 +02001106 /* original 802.11 header has a different DA and in
1107 * case of 4addr it may also have different SA
1108 */
1109 hdr = (struct ieee80211_hdr *)msdu->data;
1110 ether_addr_copy(ieee80211_get_DA(hdr), da);
1111 ether_addr_copy(ieee80211_get_SA(hdr), sa);
1112}
Michal Kaziorf6dc2092013-09-26 10:12:22 +03001113
Michal Kazior581c25f2014-11-18 09:24:48 +02001114static void *ath10k_htt_rx_h_find_rfc1042(struct ath10k *ar,
1115 struct sk_buff *msdu,
1116 enum htt_rx_mpdu_encrypt_type enctype)
1117{
1118 struct ieee80211_hdr *hdr;
1119 struct htt_rx_desc *rxd;
1120 size_t hdr_len, crypto_len;
1121 void *rfc1042;
1122 bool is_first, is_last, is_amsdu;
Michal Kazior784f69d2013-09-26 10:12:23 +03001123
Michal Kazior581c25f2014-11-18 09:24:48 +02001124 rxd = (void *)msdu->data - sizeof(*rxd);
1125 hdr = (void *)rxd->rx_hdr_status;
1126
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001127 is_first = !!(rxd->msdu_end.common.info0 &
Michal Kazior581c25f2014-11-18 09:24:48 +02001128 __cpu_to_le32(RX_MSDU_END_INFO0_FIRST_MSDU));
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001129 is_last = !!(rxd->msdu_end.common.info0 &
Michal Kazior581c25f2014-11-18 09:24:48 +02001130 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU));
1131 is_amsdu = !(is_first && is_last);
1132
1133 rfc1042 = hdr;
1134
1135 if (is_first) {
Michal Kazior784f69d2013-09-26 10:12:23 +03001136 hdr_len = ieee80211_hdrlen(hdr->frame_control);
Michal Kazior581c25f2014-11-18 09:24:48 +02001137 crypto_len = ath10k_htt_rx_crypto_param_len(ar, enctype);
Michal Kaziore3fbf8d2013-09-26 10:12:23 +03001138
Michal Kazior581c25f2014-11-18 09:24:48 +02001139 rfc1042 += round_up(hdr_len, 4) +
1140 round_up(crypto_len, 4);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001141 }
1142
Michal Kazior581c25f2014-11-18 09:24:48 +02001143 if (is_amsdu)
1144 rfc1042 += sizeof(struct amsdu_subframe_hdr);
Michal Kaziorf6dc2092013-09-26 10:12:22 +03001145
Michal Kazior581c25f2014-11-18 09:24:48 +02001146 return rfc1042;
1147}
1148
1149static void ath10k_htt_rx_h_undecap_eth(struct ath10k *ar,
1150 struct sk_buff *msdu,
1151 struct ieee80211_rx_status *status,
1152 const u8 first_hdr[64],
1153 enum htt_rx_mpdu_encrypt_type enctype)
1154{
1155 struct ieee80211_hdr *hdr;
1156 struct ethhdr *eth;
1157 size_t hdr_len;
1158 void *rfc1042;
1159 u8 da[ETH_ALEN];
1160 u8 sa[ETH_ALEN];
1161
1162 /* Delivered decapped frame:
1163 * [eth header] <-- replaced with 802.11 hdr & rfc1042/llc
1164 * [payload]
1165 */
1166
1167 rfc1042 = ath10k_htt_rx_h_find_rfc1042(ar, msdu, enctype);
1168 if (WARN_ON_ONCE(!rfc1042))
1169 return;
1170
1171 /* pull decapped header and copy SA & DA */
1172 eth = (struct ethhdr *)msdu->data;
1173 ether_addr_copy(da, eth->h_dest);
1174 ether_addr_copy(sa, eth->h_source);
1175 skb_pull(msdu, sizeof(struct ethhdr));
1176
1177 /* push rfc1042/llc/snap */
1178 memcpy(skb_push(msdu, sizeof(struct rfc1042_hdr)), rfc1042,
1179 sizeof(struct rfc1042_hdr));
1180
1181 /* push original 802.11 header */
1182 hdr = (struct ieee80211_hdr *)first_hdr;
1183 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1184 memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1185
1186 /* original 802.11 header has a different DA and in
1187 * case of 4addr it may also have different SA
1188 */
1189 hdr = (struct ieee80211_hdr *)msdu->data;
1190 ether_addr_copy(ieee80211_get_DA(hdr), da);
1191 ether_addr_copy(ieee80211_get_SA(hdr), sa);
1192}
1193
1194static void ath10k_htt_rx_h_undecap_snap(struct ath10k *ar,
1195 struct sk_buff *msdu,
1196 struct ieee80211_rx_status *status,
1197 const u8 first_hdr[64])
1198{
1199 struct ieee80211_hdr *hdr;
1200 size_t hdr_len;
1201
1202 /* Delivered decapped frame:
1203 * [amsdu header] <-- replaced with 802.11 hdr
1204 * [rfc1042/llc]
1205 * [payload]
1206 */
1207
1208 skb_pull(msdu, sizeof(struct amsdu_subframe_hdr));
1209
1210 hdr = (struct ieee80211_hdr *)first_hdr;
1211 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1212 memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1213}
1214
1215static void ath10k_htt_rx_h_undecap(struct ath10k *ar,
1216 struct sk_buff *msdu,
1217 struct ieee80211_rx_status *status,
1218 u8 first_hdr[64],
1219 enum htt_rx_mpdu_encrypt_type enctype,
1220 bool is_decrypted)
1221{
1222 struct htt_rx_desc *rxd;
1223 enum rx_msdu_decap_format decap;
Michal Kazior581c25f2014-11-18 09:24:48 +02001224
1225 /* First msdu's decapped header:
1226 * [802.11 header] <-- padded to 4 bytes long
1227 * [crypto param] <-- padded to 4 bytes long
1228 * [amsdu header] <-- only if A-MSDU
1229 * [rfc1042/llc]
1230 *
1231 * Other (2nd, 3rd, ..) msdu's decapped header:
1232 * [amsdu header] <-- only if A-MSDU
1233 * [rfc1042/llc]
1234 */
1235
1236 rxd = (void *)msdu->data - sizeof(*rxd);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001237 decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
Michal Kazior581c25f2014-11-18 09:24:48 +02001238 RX_MSDU_START_INFO1_DECAP_FORMAT);
1239
1240 switch (decap) {
1241 case RX_MSDU_DECAP_RAW:
1242 ath10k_htt_rx_h_undecap_raw(ar, msdu, status, enctype,
1243 is_decrypted);
1244 break;
1245 case RX_MSDU_DECAP_NATIVE_WIFI:
1246 ath10k_htt_rx_h_undecap_nwifi(ar, msdu, status, first_hdr);
1247 break;
1248 case RX_MSDU_DECAP_ETHERNET2_DIX:
1249 ath10k_htt_rx_h_undecap_eth(ar, msdu, status, first_hdr, enctype);
1250 break;
1251 case RX_MSDU_DECAP_8023_SNAP_LLC:
1252 ath10k_htt_rx_h_undecap_snap(ar, msdu, status, first_hdr);
1253 break;
1254 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001255}
1256
Michal Kazior605f81a2013-07-31 10:47:56 +02001257static int ath10k_htt_rx_get_csum_state(struct sk_buff *skb)
1258{
1259 struct htt_rx_desc *rxd;
1260 u32 flags, info;
1261 bool is_ip4, is_ip6;
1262 bool is_tcp, is_udp;
1263 bool ip_csum_ok, tcpudp_csum_ok;
1264
1265 rxd = (void *)skb->data - sizeof(*rxd);
1266 flags = __le32_to_cpu(rxd->attention.flags);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001267 info = __le32_to_cpu(rxd->msdu_start.common.info1);
Michal Kazior605f81a2013-07-31 10:47:56 +02001268
1269 is_ip4 = !!(info & RX_MSDU_START_INFO1_IPV4_PROTO);
1270 is_ip6 = !!(info & RX_MSDU_START_INFO1_IPV6_PROTO);
1271 is_tcp = !!(info & RX_MSDU_START_INFO1_TCP_PROTO);
1272 is_udp = !!(info & RX_MSDU_START_INFO1_UDP_PROTO);
1273 ip_csum_ok = !(flags & RX_ATTENTION_FLAGS_IP_CHKSUM_FAIL);
1274 tcpudp_csum_ok = !(flags & RX_ATTENTION_FLAGS_TCP_UDP_CHKSUM_FAIL);
1275
1276 if (!is_ip4 && !is_ip6)
1277 return CHECKSUM_NONE;
1278 if (!is_tcp && !is_udp)
1279 return CHECKSUM_NONE;
1280 if (!ip_csum_ok)
1281 return CHECKSUM_NONE;
1282 if (!tcpudp_csum_ok)
1283 return CHECKSUM_NONE;
1284
1285 return CHECKSUM_UNNECESSARY;
1286}
1287
Michal Kazior581c25f2014-11-18 09:24:48 +02001288static void ath10k_htt_rx_h_csum_offload(struct sk_buff *msdu)
1289{
1290 msdu->ip_summed = ath10k_htt_rx_get_csum_state(msdu);
1291}
1292
1293static void ath10k_htt_rx_h_mpdu(struct ath10k *ar,
1294 struct sk_buff_head *amsdu,
1295 struct ieee80211_rx_status *status)
1296{
1297 struct sk_buff *first;
1298 struct sk_buff *last;
1299 struct sk_buff *msdu;
1300 struct htt_rx_desc *rxd;
1301 struct ieee80211_hdr *hdr;
1302 enum htt_rx_mpdu_encrypt_type enctype;
1303 u8 first_hdr[64];
1304 u8 *qos;
1305 size_t hdr_len;
1306 bool has_fcs_err;
1307 bool has_crypto_err;
1308 bool has_tkip_err;
1309 bool has_peer_idx_invalid;
1310 bool is_decrypted;
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001311 bool is_mgmt;
Michal Kazior581c25f2014-11-18 09:24:48 +02001312 u32 attention;
1313
1314 if (skb_queue_empty(amsdu))
1315 return;
1316
1317 first = skb_peek(amsdu);
1318 rxd = (void *)first->data - sizeof(*rxd);
1319
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001320 is_mgmt = !!(rxd->attention.flags &
1321 __cpu_to_le32(RX_ATTENTION_FLAGS_MGMT_TYPE));
1322
Michal Kazior581c25f2014-11-18 09:24:48 +02001323 enctype = MS(__le32_to_cpu(rxd->mpdu_start.info0),
1324 RX_MPDU_START_INFO0_ENCRYPT_TYPE);
1325
1326 /* First MSDU's Rx descriptor in an A-MSDU contains full 802.11
1327 * decapped header. It'll be used for undecapping of each MSDU.
1328 */
1329 hdr = (void *)rxd->rx_hdr_status;
1330 hdr_len = ieee80211_hdrlen(hdr->frame_control);
1331 memcpy(first_hdr, hdr, hdr_len);
1332
1333 /* Each A-MSDU subframe will use the original header as the base and be
1334 * reported as a separate MSDU so strip the A-MSDU bit from QoS Ctl.
1335 */
1336 hdr = (void *)first_hdr;
1337 qos = ieee80211_get_qos_ctl(hdr);
1338 qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1339
1340 /* Some attention flags are valid only in the last MSDU. */
1341 last = skb_peek_tail(amsdu);
1342 rxd = (void *)last->data - sizeof(*rxd);
1343 attention = __le32_to_cpu(rxd->attention.flags);
1344
1345 has_fcs_err = !!(attention & RX_ATTENTION_FLAGS_FCS_ERR);
1346 has_crypto_err = !!(attention & RX_ATTENTION_FLAGS_DECRYPT_ERR);
1347 has_tkip_err = !!(attention & RX_ATTENTION_FLAGS_TKIP_MIC_ERR);
1348 has_peer_idx_invalid = !!(attention & RX_ATTENTION_FLAGS_PEER_IDX_INVALID);
1349
1350 /* Note: If hardware captures an encrypted frame that it can't decrypt,
1351 * e.g. due to fcs error, missing peer or invalid key data it will
1352 * report the frame as raw.
1353 */
1354 is_decrypted = (enctype != HTT_RX_MPDU_ENCRYPT_NONE &&
1355 !has_fcs_err &&
1356 !has_crypto_err &&
1357 !has_peer_idx_invalid);
1358
1359 /* Clear per-MPDU flags while leaving per-PPDU flags intact. */
1360 status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
1361 RX_FLAG_MMIC_ERROR |
1362 RX_FLAG_DECRYPTED |
1363 RX_FLAG_IV_STRIPPED |
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001364 RX_FLAG_ONLY_MONITOR |
Michal Kazior581c25f2014-11-18 09:24:48 +02001365 RX_FLAG_MMIC_STRIPPED);
1366
1367 if (has_fcs_err)
1368 status->flag |= RX_FLAG_FAILED_FCS_CRC;
1369
1370 if (has_tkip_err)
1371 status->flag |= RX_FLAG_MMIC_ERROR;
1372
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001373 /* Firmware reports all necessary management frames via WMI already.
1374 * They are not reported to monitor interfaces at all so pass the ones
1375 * coming via HTT to monitor interfaces instead. This simplifies
1376 * matters a lot.
1377 */
1378 if (is_mgmt)
1379 status->flag |= RX_FLAG_ONLY_MONITOR;
1380
1381 if (is_decrypted) {
1382 status->flag |= RX_FLAG_DECRYPTED;
1383
1384 if (likely(!is_mgmt))
1385 status->flag |= RX_FLAG_IV_STRIPPED |
1386 RX_FLAG_MMIC_STRIPPED;
1387}
Michal Kazior581c25f2014-11-18 09:24:48 +02001388
1389 skb_queue_walk(amsdu, msdu) {
1390 ath10k_htt_rx_h_csum_offload(msdu);
1391 ath10k_htt_rx_h_undecap(ar, msdu, status, first_hdr, enctype,
1392 is_decrypted);
1393
1394 /* Undecapping involves copying the original 802.11 header back
1395 * to sk_buff. If frame is protected and hardware has decrypted
1396 * it then remove the protected bit.
1397 */
1398 if (!is_decrypted)
1399 continue;
Grzegorz Bajorski60549ca2015-11-30 13:56:59 +01001400 if (is_mgmt)
1401 continue;
Michal Kazior581c25f2014-11-18 09:24:48 +02001402
1403 hdr = (void *)msdu->data;
1404 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1405 }
1406}
1407
1408static void ath10k_htt_rx_h_deliver(struct ath10k *ar,
1409 struct sk_buff_head *amsdu,
1410 struct ieee80211_rx_status *status)
1411{
1412 struct sk_buff *msdu;
1413
1414 while ((msdu = __skb_dequeue(amsdu))) {
1415 /* Setup per-MSDU flags */
1416 if (skb_queue_empty(amsdu))
1417 status->flag &= ~RX_FLAG_AMSDU_MORE;
1418 else
1419 status->flag |= RX_FLAG_AMSDU_MORE;
1420
1421 ath10k_process_rx(ar, status, msdu);
1422 }
1423}
1424
Michal Kazior9aa505d2014-11-18 09:24:47 +02001425static int ath10k_unchain_msdu(struct sk_buff_head *amsdu)
Ben Greearbfa35362014-03-03 14:07:09 -08001426{
Michal Kazior9aa505d2014-11-18 09:24:47 +02001427 struct sk_buff *skb, *first;
Ben Greearbfa35362014-03-03 14:07:09 -08001428 int space;
1429 int total_len = 0;
1430
1431 /* TODO: Might could optimize this by using
1432 * skb_try_coalesce or similar method to
1433 * decrease copying, or maybe get mac80211 to
1434 * provide a way to just receive a list of
1435 * skb?
1436 */
1437
Michal Kazior9aa505d2014-11-18 09:24:47 +02001438 first = __skb_dequeue(amsdu);
Ben Greearbfa35362014-03-03 14:07:09 -08001439
1440 /* Allocate total length all at once. */
Michal Kazior9aa505d2014-11-18 09:24:47 +02001441 skb_queue_walk(amsdu, skb)
1442 total_len += skb->len;
Ben Greearbfa35362014-03-03 14:07:09 -08001443
Michal Kazior9aa505d2014-11-18 09:24:47 +02001444 space = total_len - skb_tailroom(first);
Ben Greearbfa35362014-03-03 14:07:09 -08001445 if ((space > 0) &&
Michal Kazior9aa505d2014-11-18 09:24:47 +02001446 (pskb_expand_head(first, 0, space, GFP_ATOMIC) < 0)) {
Ben Greearbfa35362014-03-03 14:07:09 -08001447 /* TODO: bump some rx-oom error stat */
1448 /* put it back together so we can free the
1449 * whole list at once.
1450 */
Michal Kazior9aa505d2014-11-18 09:24:47 +02001451 __skb_queue_head(amsdu, first);
Ben Greearbfa35362014-03-03 14:07:09 -08001452 return -1;
1453 }
1454
1455 /* Walk list again, copying contents into
1456 * msdu_head
1457 */
Michal Kazior9aa505d2014-11-18 09:24:47 +02001458 while ((skb = __skb_dequeue(amsdu))) {
1459 skb_copy_from_linear_data(skb, skb_put(first, skb->len),
1460 skb->len);
1461 dev_kfree_skb_any(skb);
Ben Greearbfa35362014-03-03 14:07:09 -08001462 }
1463
Michal Kazior9aa505d2014-11-18 09:24:47 +02001464 __skb_queue_head(amsdu, first);
Ben Greearbfa35362014-03-03 14:07:09 -08001465 return 0;
1466}
1467
Michal Kazior581c25f2014-11-18 09:24:48 +02001468static void ath10k_htt_rx_h_unchain(struct ath10k *ar,
1469 struct sk_buff_head *amsdu,
1470 bool chained)
Janusz Dziedzic2acc4eb2014-03-19 07:09:40 +01001471{
Michal Kazior581c25f2014-11-18 09:24:48 +02001472 struct sk_buff *first;
1473 struct htt_rx_desc *rxd;
1474 enum rx_msdu_decap_format decap;
Michal Kazior7aa7a722014-08-25 12:09:38 +02001475
Michal Kazior581c25f2014-11-18 09:24:48 +02001476 first = skb_peek(amsdu);
1477 rxd = (void *)first->data - sizeof(*rxd);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001478 decap = MS(__le32_to_cpu(rxd->msdu_start.common.info1),
Michal Kazior581c25f2014-11-18 09:24:48 +02001479 RX_MSDU_START_INFO1_DECAP_FORMAT);
1480
1481 if (!chained)
1482 return;
1483
1484 /* FIXME: Current unchaining logic can only handle simple case of raw
1485 * msdu chaining. If decapping is other than raw the chaining may be
1486 * more complex and this isn't handled by the current code. Don't even
1487 * try re-constructing such frames - it'll be pretty much garbage.
1488 */
1489 if (decap != RX_MSDU_DECAP_RAW ||
1490 skb_queue_len(amsdu) != 1 + rxd->frag_info.ring2_more_count) {
1491 __skb_queue_purge(amsdu);
1492 return;
1493 }
1494
1495 ath10k_unchain_msdu(amsdu);
1496}
1497
1498static bool ath10k_htt_rx_amsdu_allowed(struct ath10k *ar,
1499 struct sk_buff_head *amsdu,
1500 struct ieee80211_rx_status *rx_status)
1501{
Michal Kazior581c25f2014-11-18 09:24:48 +02001502 /* FIXME: It might be a good idea to do some fuzzy-testing to drop
1503 * invalid/dangerous frames.
1504 */
1505
1506 if (!rx_status->freq) {
1507 ath10k_warn(ar, "no channel configured; ignoring frame(s)!\n");
Janusz Dziedzic2acc4eb2014-03-19 07:09:40 +01001508 return false;
1509 }
1510
Michal Kazior581c25f2014-11-18 09:24:48 +02001511 if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags)) {
1512 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx cac running\n");
Janusz Dziedzic2acc4eb2014-03-19 07:09:40 +01001513 return false;
1514 }
1515
1516 return true;
1517}
1518
Michal Kazior581c25f2014-11-18 09:24:48 +02001519static void ath10k_htt_rx_h_filter(struct ath10k *ar,
1520 struct sk_buff_head *amsdu,
1521 struct ieee80211_rx_status *rx_status)
1522{
1523 if (skb_queue_empty(amsdu))
1524 return;
1525
1526 if (ath10k_htt_rx_amsdu_allowed(ar, amsdu, rx_status))
1527 return;
1528
1529 __skb_queue_purge(amsdu);
1530}
1531
Rajkumar Manoharan18235662016-03-22 17:22:14 +05301532static int ath10k_htt_rx_handle_amsdu(struct ath10k_htt *htt)
1533{
1534 struct ath10k *ar = htt->ar;
1535 static struct ieee80211_rx_status rx_status;
1536 struct sk_buff_head amsdu;
1537 int ret;
1538
1539 __skb_queue_head_init(&amsdu);
1540
1541 spin_lock_bh(&htt->rx_ring.lock);
1542 if (htt->rx_confused) {
1543 spin_unlock_bh(&htt->rx_ring.lock);
1544 return -EIO;
1545 }
1546 ret = ath10k_htt_rx_amsdu_pop(htt, &amsdu);
1547 spin_unlock_bh(&htt->rx_ring.lock);
1548
1549 if (ret < 0) {
1550 ath10k_warn(ar, "rx ring became corrupted: %d\n", ret);
1551 __skb_queue_purge(&amsdu);
1552 /* FIXME: It's probably a good idea to reboot the
1553 * device instead of leaving it inoperable.
1554 */
1555 htt->rx_confused = true;
1556 return ret;
1557 }
1558
1559 ath10k_htt_rx_h_ppdu(ar, &amsdu, &rx_status, 0xffff);
1560 ath10k_htt_rx_h_unchain(ar, &amsdu, ret > 0);
1561 ath10k_htt_rx_h_filter(ar, &amsdu, &rx_status);
1562 ath10k_htt_rx_h_mpdu(ar, &amsdu, &rx_status);
1563 ath10k_htt_rx_h_deliver(ar, &amsdu, &rx_status);
1564
1565 return 0;
1566}
1567
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05301568static void ath10k_htt_rx_proc_rx_ind(struct ath10k_htt *htt,
1569 struct htt_rx_indication *rx)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001570{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001571 struct ath10k *ar = htt->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001572 struct htt_rx_indication_mpdu_range *mpdu_ranges;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001573 int num_mpdu_ranges;
Rajkumar Manoharan18235662016-03-22 17:22:14 +05301574 int i, mpdu_count = 0;
Michal Kaziore0bd7512014-11-18 09:24:48 +02001575
Kalle Valo5e3dd152013-06-12 20:52:10 +03001576 num_mpdu_ranges = MS(__le32_to_cpu(rx->hdr.info1),
1577 HTT_RX_INDICATION_INFO1_NUM_MPDU_RANGES);
1578 mpdu_ranges = htt_rx_ind_get_mpdu_ranges(rx);
1579
Michal Kazior7aa7a722014-08-25 12:09:38 +02001580 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt rx ind: ",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001581 rx, sizeof(*rx) +
1582 (sizeof(struct htt_rx_indication_mpdu_range) *
1583 num_mpdu_ranges));
1584
Michal Kaziord5406902014-11-18 09:24:47 +02001585 for (i = 0; i < num_mpdu_ranges; i++)
1586 mpdu_count += mpdu_ranges[i].mpdu_count;
Janusz Dziedzicd84dd602014-03-24 21:23:20 +01001587
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05301588 atomic_add(mpdu_count, &htt->num_mpdus_ready);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001589
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05301590 tasklet_schedule(&htt->txrx_compl_task);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001591}
1592
Rajkumar Manoharan18235662016-03-22 17:22:14 +05301593static void ath10k_htt_rx_frag_handler(struct ath10k_htt *htt)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001594{
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05301595 atomic_inc(&htt->num_mpdus_ready);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001596
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05301597 tasklet_schedule(&htt->txrx_compl_task);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001598}
1599
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301600static void ath10k_htt_rx_tx_compl_ind(struct ath10k *ar,
Michal Kazior6c5151a2014-02-27 18:50:04 +02001601 struct sk_buff *skb)
1602{
1603 struct ath10k_htt *htt = &ar->htt;
1604 struct htt_resp *resp = (struct htt_resp *)skb->data;
1605 struct htt_tx_done tx_done = {};
1606 int status = MS(resp->data_tx_completion.flags, HTT_DATA_TX_STATUS);
1607 __le16 msdu_id;
1608 int i;
1609
1610 switch (status) {
1611 case HTT_DATA_TX_STATUS_NO_ACK:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301612 tx_done.status = HTT_TX_COMPL_STATE_NOACK;
Michal Kazior6c5151a2014-02-27 18:50:04 +02001613 break;
1614 case HTT_DATA_TX_STATUS_OK:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301615 tx_done.status = HTT_TX_COMPL_STATE_ACK;
Michal Kazior6c5151a2014-02-27 18:50:04 +02001616 break;
1617 case HTT_DATA_TX_STATUS_DISCARD:
1618 case HTT_DATA_TX_STATUS_POSTPONE:
1619 case HTT_DATA_TX_STATUS_DOWNLOAD_FAIL:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301620 tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
Michal Kazior6c5151a2014-02-27 18:50:04 +02001621 break;
1622 default:
Michal Kazior7aa7a722014-08-25 12:09:38 +02001623 ath10k_warn(ar, "unhandled tx completion status %d\n", status);
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301624 tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
Michal Kazior6c5151a2014-02-27 18:50:04 +02001625 break;
1626 }
1627
Michal Kazior7aa7a722014-08-25 12:09:38 +02001628 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt tx completion num_msdus %d\n",
Michal Kazior6c5151a2014-02-27 18:50:04 +02001629 resp->data_tx_completion.num_msdus);
1630
1631 for (i = 0; i < resp->data_tx_completion.num_msdus; i++) {
1632 msdu_id = resp->data_tx_completion.msdus[i];
1633 tx_done.msdu_id = __le16_to_cpu(msdu_id);
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05301634
1635 /* kfifo_put: In practice firmware shouldn't fire off per-CE
1636 * interrupt and main interrupt (MSI/-X range case) for the same
1637 * HTC service so it should be safe to use kfifo_put w/o lock.
1638 *
1639 * From kfifo_put() documentation:
1640 * Note that with only one concurrent reader and one concurrent
1641 * writer, you don't need extra locking to use these macro.
1642 */
1643 if (!kfifo_put(&htt->txdone_fifo, tx_done)) {
1644 ath10k_warn(ar, "txdone fifo overrun, msdu_id %d status %d\n",
1645 tx_done.msdu_id, tx_done.status);
1646 ath10k_txrx_tx_unref(htt, &tx_done);
1647 }
Michal Kazior6c5151a2014-02-27 18:50:04 +02001648 }
1649}
1650
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001651static void ath10k_htt_rx_addba(struct ath10k *ar, struct htt_resp *resp)
1652{
1653 struct htt_rx_addba *ev = &resp->rx_addba;
1654 struct ath10k_peer *peer;
1655 struct ath10k_vif *arvif;
1656 u16 info0, tid, peer_id;
1657
1658 info0 = __le16_to_cpu(ev->info0);
1659 tid = MS(info0, HTT_RX_BA_INFO0_TID);
1660 peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1661
Michal Kazior7aa7a722014-08-25 12:09:38 +02001662 ath10k_dbg(ar, ATH10K_DBG_HTT,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001663 "htt rx addba tid %hu peer_id %hu size %hhu\n",
1664 tid, peer_id, ev->window_size);
1665
1666 spin_lock_bh(&ar->data_lock);
1667 peer = ath10k_peer_find_by_id(ar, peer_id);
1668 if (!peer) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001669 ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001670 peer_id);
1671 spin_unlock_bh(&ar->data_lock);
1672 return;
1673 }
1674
1675 arvif = ath10k_get_arvif(ar, peer->vdev_id);
1676 if (!arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001677 ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001678 peer->vdev_id);
1679 spin_unlock_bh(&ar->data_lock);
1680 return;
1681 }
1682
Michal Kazior7aa7a722014-08-25 12:09:38 +02001683 ath10k_dbg(ar, ATH10K_DBG_HTT,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001684 "htt rx start rx ba session sta %pM tid %hu size %hhu\n",
1685 peer->addr, tid, ev->window_size);
1686
1687 ieee80211_start_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1688 spin_unlock_bh(&ar->data_lock);
1689}
1690
1691static void ath10k_htt_rx_delba(struct ath10k *ar, struct htt_resp *resp)
1692{
1693 struct htt_rx_delba *ev = &resp->rx_delba;
1694 struct ath10k_peer *peer;
1695 struct ath10k_vif *arvif;
1696 u16 info0, tid, peer_id;
1697
1698 info0 = __le16_to_cpu(ev->info0);
1699 tid = MS(info0, HTT_RX_BA_INFO0_TID);
1700 peer_id = MS(info0, HTT_RX_BA_INFO0_PEER_ID);
1701
Michal Kazior7aa7a722014-08-25 12:09:38 +02001702 ath10k_dbg(ar, ATH10K_DBG_HTT,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001703 "htt rx delba tid %hu peer_id %hu\n",
1704 tid, peer_id);
1705
1706 spin_lock_bh(&ar->data_lock);
1707 peer = ath10k_peer_find_by_id(ar, peer_id);
1708 if (!peer) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001709 ath10k_warn(ar, "received addba event for invalid peer_id: %hu\n",
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001710 peer_id);
1711 spin_unlock_bh(&ar->data_lock);
1712 return;
1713 }
1714
1715 arvif = ath10k_get_arvif(ar, peer->vdev_id);
1716 if (!arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001717 ath10k_warn(ar, "received addba event for invalid vdev_id: %u\n",
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001718 peer->vdev_id);
1719 spin_unlock_bh(&ar->data_lock);
1720 return;
1721 }
1722
Michal Kazior7aa7a722014-08-25 12:09:38 +02001723 ath10k_dbg(ar, ATH10K_DBG_HTT,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02001724 "htt rx stop rx ba session sta %pM tid %hu\n",
1725 peer->addr, tid);
1726
1727 ieee80211_stop_rx_ba_session_offl(arvif->vif, peer->addr, tid);
1728 spin_unlock_bh(&ar->data_lock);
1729}
1730
Michal Kaziorc5450702015-01-24 12:14:48 +02001731static int ath10k_htt_rx_extract_amsdu(struct sk_buff_head *list,
1732 struct sk_buff_head *amsdu)
1733{
1734 struct sk_buff *msdu;
1735 struct htt_rx_desc *rxd;
1736
1737 if (skb_queue_empty(list))
1738 return -ENOBUFS;
1739
1740 if (WARN_ON(!skb_queue_empty(amsdu)))
1741 return -EINVAL;
1742
1743 while ((msdu = __skb_dequeue(list))) {
1744 __skb_queue_tail(amsdu, msdu);
1745
1746 rxd = (void *)msdu->data - sizeof(*rxd);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001747 if (rxd->msdu_end.common.info0 &
Michal Kaziorc5450702015-01-24 12:14:48 +02001748 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))
1749 break;
1750 }
1751
1752 msdu = skb_peek_tail(amsdu);
1753 rxd = (void *)msdu->data - sizeof(*rxd);
Peter Oh1f5dbfb2015-07-15 19:01:21 -07001754 if (!(rxd->msdu_end.common.info0 &
Michal Kaziorc5450702015-01-24 12:14:48 +02001755 __cpu_to_le32(RX_MSDU_END_INFO0_LAST_MSDU))) {
1756 skb_queue_splice_init(amsdu, list);
1757 return -EAGAIN;
1758 }
1759
1760 return 0;
1761}
1762
1763static void ath10k_htt_rx_h_rx_offload_prot(struct ieee80211_rx_status *status,
1764 struct sk_buff *skb)
1765{
1766 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1767
1768 if (!ieee80211_has_protected(hdr->frame_control))
1769 return;
1770
1771 /* Offloaded frames are already decrypted but firmware insists they are
1772 * protected in the 802.11 header. Strip the flag. Otherwise mac80211
1773 * will drop the frame.
1774 */
1775
1776 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1777 status->flag |= RX_FLAG_DECRYPTED |
1778 RX_FLAG_IV_STRIPPED |
1779 RX_FLAG_MMIC_STRIPPED;
1780}
1781
1782static void ath10k_htt_rx_h_rx_offload(struct ath10k *ar,
1783 struct sk_buff_head *list)
1784{
1785 struct ath10k_htt *htt = &ar->htt;
1786 struct ieee80211_rx_status *status = &htt->rx_status;
1787 struct htt_rx_offload_msdu *rx;
1788 struct sk_buff *msdu;
1789 size_t offset;
1790
1791 while ((msdu = __skb_dequeue(list))) {
1792 /* Offloaded frames don't have Rx descriptor. Instead they have
1793 * a short meta information header.
1794 */
1795
1796 rx = (void *)msdu->data;
1797
1798 skb_put(msdu, sizeof(*rx));
1799 skb_pull(msdu, sizeof(*rx));
1800
1801 if (skb_tailroom(msdu) < __le16_to_cpu(rx->msdu_len)) {
1802 ath10k_warn(ar, "dropping frame: offloaded rx msdu is too long!\n");
1803 dev_kfree_skb_any(msdu);
1804 continue;
1805 }
1806
1807 skb_put(msdu, __le16_to_cpu(rx->msdu_len));
1808
1809 /* Offloaded rx header length isn't multiple of 2 nor 4 so the
1810 * actual payload is unaligned. Align the frame. Otherwise
1811 * mac80211 complains. This shouldn't reduce performance much
1812 * because these offloaded frames are rare.
1813 */
1814 offset = 4 - ((unsigned long)msdu->data & 3);
1815 skb_put(msdu, offset);
1816 memmove(msdu->data + offset, msdu->data, msdu->len);
1817 skb_pull(msdu, offset);
1818
1819 /* FIXME: The frame is NWifi. Re-construct QoS Control
1820 * if possible later.
1821 */
1822
1823 memset(status, 0, sizeof(*status));
1824 status->flag |= RX_FLAG_NO_SIGNAL_VAL;
1825
1826 ath10k_htt_rx_h_rx_offload_prot(status, msdu);
Michal Kazior500ff9f2015-03-31 10:26:21 +00001827 ath10k_htt_rx_h_channel(ar, status, NULL, rx->vdev_id);
Michal Kaziorc5450702015-01-24 12:14:48 +02001828 ath10k_process_rx(ar, status, msdu);
1829 }
1830}
1831
1832static void ath10k_htt_rx_in_ord_ind(struct ath10k *ar, struct sk_buff *skb)
1833{
1834 struct ath10k_htt *htt = &ar->htt;
1835 struct htt_resp *resp = (void *)skb->data;
1836 struct ieee80211_rx_status *status = &htt->rx_status;
1837 struct sk_buff_head list;
1838 struct sk_buff_head amsdu;
1839 u16 peer_id;
1840 u16 msdu_count;
1841 u8 vdev_id;
1842 u8 tid;
1843 bool offload;
1844 bool frag;
1845 int ret;
1846
1847 lockdep_assert_held(&htt->rx_ring.lock);
1848
1849 if (htt->rx_confused)
1850 return;
1851
1852 skb_pull(skb, sizeof(resp->hdr));
1853 skb_pull(skb, sizeof(resp->rx_in_ord_ind));
1854
1855 peer_id = __le16_to_cpu(resp->rx_in_ord_ind.peer_id);
1856 msdu_count = __le16_to_cpu(resp->rx_in_ord_ind.msdu_count);
1857 vdev_id = resp->rx_in_ord_ind.vdev_id;
1858 tid = SM(resp->rx_in_ord_ind.info, HTT_RX_IN_ORD_IND_INFO_TID);
1859 offload = !!(resp->rx_in_ord_ind.info &
1860 HTT_RX_IN_ORD_IND_INFO_OFFLOAD_MASK);
1861 frag = !!(resp->rx_in_ord_ind.info & HTT_RX_IN_ORD_IND_INFO_FRAG_MASK);
1862
1863 ath10k_dbg(ar, ATH10K_DBG_HTT,
1864 "htt rx in ord vdev %i peer %i tid %i offload %i frag %i msdu count %i\n",
1865 vdev_id, peer_id, tid, offload, frag, msdu_count);
1866
1867 if (skb->len < msdu_count * sizeof(*resp->rx_in_ord_ind.msdu_descs)) {
1868 ath10k_warn(ar, "dropping invalid in order rx indication\n");
1869 return;
1870 }
1871
1872 /* The event can deliver more than 1 A-MSDU. Each A-MSDU is later
1873 * extracted and processed.
1874 */
1875 __skb_queue_head_init(&list);
1876 ret = ath10k_htt_rx_pop_paddr_list(htt, &resp->rx_in_ord_ind, &list);
1877 if (ret < 0) {
1878 ath10k_warn(ar, "failed to pop paddr list: %d\n", ret);
1879 htt->rx_confused = true;
1880 return;
1881 }
1882
1883 /* Offloaded frames are very different and need to be handled
1884 * separately.
1885 */
1886 if (offload)
1887 ath10k_htt_rx_h_rx_offload(ar, &list);
1888
1889 while (!skb_queue_empty(&list)) {
1890 __skb_queue_head_init(&amsdu);
1891 ret = ath10k_htt_rx_extract_amsdu(&list, &amsdu);
1892 switch (ret) {
1893 case 0:
1894 /* Note: The in-order indication may report interleaved
1895 * frames from different PPDUs meaning reported rx rate
1896 * to mac80211 isn't accurate/reliable. It's still
1897 * better to report something than nothing though. This
1898 * should still give an idea about rx rate to the user.
1899 */
Michal Kazior500ff9f2015-03-31 10:26:21 +00001900 ath10k_htt_rx_h_ppdu(ar, &amsdu, status, vdev_id);
Michal Kaziorc5450702015-01-24 12:14:48 +02001901 ath10k_htt_rx_h_filter(ar, &amsdu, status);
1902 ath10k_htt_rx_h_mpdu(ar, &amsdu, status);
1903 ath10k_htt_rx_h_deliver(ar, &amsdu, status);
1904 break;
1905 case -EAGAIN:
1906 /* fall through */
1907 default:
1908 /* Should not happen. */
1909 ath10k_warn(ar, "failed to extract amsdu: %d\n", ret);
1910 htt->rx_confused = true;
1911 __skb_queue_purge(&list);
1912 return;
1913 }
1914 }
1915
1916 tasklet_schedule(&htt->rx_replenish_task);
1917}
1918
Michal Kazior839ae632016-03-06 16:14:32 +02001919static void ath10k_htt_rx_tx_fetch_resp_id_confirm(struct ath10k *ar,
1920 const __le32 *resp_ids,
1921 int num_resp_ids)
1922{
1923 int i;
1924 u32 resp_id;
1925
1926 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm num_resp_ids %d\n",
1927 num_resp_ids);
1928
1929 for (i = 0; i < num_resp_ids; i++) {
1930 resp_id = le32_to_cpu(resp_ids[i]);
1931
1932 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm resp_id %u\n",
1933 resp_id);
1934
1935 /* TODO: free resp_id */
1936 }
1937}
1938
1939static void ath10k_htt_rx_tx_fetch_ind(struct ath10k *ar, struct sk_buff *skb)
1940{
Michal Kazior426e10e2016-03-06 16:14:43 +02001941 struct ieee80211_hw *hw = ar->hw;
1942 struct ieee80211_txq *txq;
Michal Kazior839ae632016-03-06 16:14:32 +02001943 struct htt_resp *resp = (struct htt_resp *)skb->data;
1944 struct htt_tx_fetch_record *record;
1945 size_t len;
1946 size_t max_num_bytes;
1947 size_t max_num_msdus;
Michal Kazior426e10e2016-03-06 16:14:43 +02001948 size_t num_bytes;
1949 size_t num_msdus;
Michal Kazior839ae632016-03-06 16:14:32 +02001950 const __le32 *resp_ids;
1951 u16 num_records;
1952 u16 num_resp_ids;
1953 u16 peer_id;
1954 u8 tid;
Michal Kazior426e10e2016-03-06 16:14:43 +02001955 int ret;
Michal Kazior839ae632016-03-06 16:14:32 +02001956 int i;
1957
1958 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind\n");
1959
1960 len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_ind);
1961 if (unlikely(skb->len < len)) {
1962 ath10k_warn(ar, "received corrupted tx_fetch_ind event: buffer too short\n");
1963 return;
1964 }
1965
1966 num_records = le16_to_cpu(resp->tx_fetch_ind.num_records);
1967 num_resp_ids = le16_to_cpu(resp->tx_fetch_ind.num_resp_ids);
1968
1969 len += sizeof(resp->tx_fetch_ind.records[0]) * num_records;
1970 len += sizeof(resp->tx_fetch_ind.resp_ids[0]) * num_resp_ids;
1971
1972 if (unlikely(skb->len < len)) {
1973 ath10k_warn(ar, "received corrupted tx_fetch_ind event: too many records/resp_ids\n");
1974 return;
1975 }
1976
1977 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch ind num records %hu num resps %hu seq %hu\n",
1978 num_records, num_resp_ids,
1979 le16_to_cpu(resp->tx_fetch_ind.fetch_seq_num));
1980
Michal Kazior426e10e2016-03-06 16:14:43 +02001981 if (!ar->htt.tx_q_state.enabled) {
1982 ath10k_warn(ar, "received unexpected tx_fetch_ind event: not enabled\n");
1983 return;
1984 }
1985
1986 if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH) {
1987 ath10k_warn(ar, "received unexpected tx_fetch_ind event: in push mode\n");
1988 return;
1989 }
1990
1991 rcu_read_lock();
Michal Kazior839ae632016-03-06 16:14:32 +02001992
1993 for (i = 0; i < num_records; i++) {
1994 record = &resp->tx_fetch_ind.records[i];
1995 peer_id = MS(le16_to_cpu(record->info),
1996 HTT_TX_FETCH_RECORD_INFO_PEER_ID);
1997 tid = MS(le16_to_cpu(record->info),
1998 HTT_TX_FETCH_RECORD_INFO_TID);
1999 max_num_msdus = le16_to_cpu(record->num_msdus);
2000 max_num_bytes = le32_to_cpu(record->num_bytes);
2001
2002 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch record %i peer_id %hu tid %hhu msdus %zu bytes %zu\n",
2003 i, peer_id, tid, max_num_msdus, max_num_bytes);
2004
2005 if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
2006 unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
2007 ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
2008 peer_id, tid);
2009 continue;
2010 }
2011
Michal Kazior426e10e2016-03-06 16:14:43 +02002012 spin_lock_bh(&ar->data_lock);
2013 txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
2014 spin_unlock_bh(&ar->data_lock);
2015
2016 /* It is okay to release the lock and use txq because RCU read
2017 * lock is held.
2018 */
2019
2020 if (unlikely(!txq)) {
2021 ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
2022 peer_id, tid);
2023 continue;
2024 }
2025
2026 num_msdus = 0;
2027 num_bytes = 0;
2028
2029 while (num_msdus < max_num_msdus &&
2030 num_bytes < max_num_bytes) {
2031 ret = ath10k_mac_tx_push_txq(hw, txq);
2032 if (ret < 0)
2033 break;
2034
2035 num_msdus++;
2036 num_bytes += ret;
2037 }
2038
2039 record->num_msdus = cpu_to_le16(num_msdus);
2040 record->num_bytes = cpu_to_le32(num_bytes);
2041
2042 ath10k_htt_tx_txq_recalc(hw, txq);
Michal Kazior839ae632016-03-06 16:14:32 +02002043 }
2044
Michal Kazior426e10e2016-03-06 16:14:43 +02002045 rcu_read_unlock();
2046
Michal Kazior839ae632016-03-06 16:14:32 +02002047 resp_ids = ath10k_htt_get_tx_fetch_ind_resp_ids(&resp->tx_fetch_ind);
2048 ath10k_htt_rx_tx_fetch_resp_id_confirm(ar, resp_ids, num_resp_ids);
2049
Michal Kazior426e10e2016-03-06 16:14:43 +02002050 ret = ath10k_htt_tx_fetch_resp(ar,
2051 resp->tx_fetch_ind.token,
2052 resp->tx_fetch_ind.fetch_seq_num,
2053 resp->tx_fetch_ind.records,
2054 num_records);
2055 if (unlikely(ret)) {
2056 ath10k_warn(ar, "failed to submit tx fetch resp for token 0x%08x: %d\n",
2057 le32_to_cpu(resp->tx_fetch_ind.token), ret);
2058 /* FIXME: request fw restart */
2059 }
2060
2061 ath10k_htt_tx_txq_sync(ar);
Michal Kazior839ae632016-03-06 16:14:32 +02002062}
2063
2064static void ath10k_htt_rx_tx_fetch_confirm(struct ath10k *ar,
2065 struct sk_buff *skb)
2066{
2067 const struct htt_resp *resp = (void *)skb->data;
2068 size_t len;
2069 int num_resp_ids;
2070
2071 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx fetch confirm\n");
2072
2073 len = sizeof(resp->hdr) + sizeof(resp->tx_fetch_confirm);
2074 if (unlikely(skb->len < len)) {
2075 ath10k_warn(ar, "received corrupted tx_fetch_confirm event: buffer too short\n");
2076 return;
2077 }
2078
2079 num_resp_ids = le16_to_cpu(resp->tx_fetch_confirm.num_resp_ids);
2080 len += sizeof(resp->tx_fetch_confirm.resp_ids[0]) * num_resp_ids;
2081
2082 if (unlikely(skb->len < len)) {
2083 ath10k_warn(ar, "received corrupted tx_fetch_confirm event: resp_ids buffer overflow\n");
2084 return;
2085 }
2086
2087 ath10k_htt_rx_tx_fetch_resp_id_confirm(ar,
2088 resp->tx_fetch_confirm.resp_ids,
2089 num_resp_ids);
2090}
2091
2092static void ath10k_htt_rx_tx_mode_switch_ind(struct ath10k *ar,
2093 struct sk_buff *skb)
2094{
2095 const struct htt_resp *resp = (void *)skb->data;
2096 const struct htt_tx_mode_switch_record *record;
Michal Kazior426e10e2016-03-06 16:14:43 +02002097 struct ieee80211_txq *txq;
2098 struct ath10k_txq *artxq;
Michal Kazior839ae632016-03-06 16:14:32 +02002099 size_t len;
2100 size_t num_records;
2101 enum htt_tx_mode_switch_mode mode;
2102 bool enable;
2103 u16 info0;
2104 u16 info1;
2105 u16 threshold;
2106 u16 peer_id;
2107 u8 tid;
2108 int i;
2109
2110 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx tx mode switch ind\n");
2111
2112 len = sizeof(resp->hdr) + sizeof(resp->tx_mode_switch_ind);
2113 if (unlikely(skb->len < len)) {
2114 ath10k_warn(ar, "received corrupted tx_mode_switch_ind event: buffer too short\n");
2115 return;
2116 }
2117
2118 info0 = le16_to_cpu(resp->tx_mode_switch_ind.info0);
2119 info1 = le16_to_cpu(resp->tx_mode_switch_ind.info1);
2120
2121 enable = !!(info0 & HTT_TX_MODE_SWITCH_IND_INFO0_ENABLE);
2122 num_records = MS(info0, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
2123 mode = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_MODE);
2124 threshold = MS(info1, HTT_TX_MODE_SWITCH_IND_INFO1_THRESHOLD);
2125
2126 ath10k_dbg(ar, ATH10K_DBG_HTT,
2127 "htt rx tx mode switch ind info0 0x%04hx info1 0x%04hx enable %d num records %zd mode %d threshold %hu\n",
2128 info0, info1, enable, num_records, mode, threshold);
2129
2130 len += sizeof(resp->tx_mode_switch_ind.records[0]) * num_records;
2131
2132 if (unlikely(skb->len < len)) {
2133 ath10k_warn(ar, "received corrupted tx_mode_switch_mode_ind event: too many records\n");
2134 return;
2135 }
2136
2137 switch (mode) {
2138 case HTT_TX_MODE_SWITCH_PUSH:
2139 case HTT_TX_MODE_SWITCH_PUSH_PULL:
2140 break;
2141 default:
2142 ath10k_warn(ar, "received invalid tx_mode_switch_mode_ind mode %d, ignoring\n",
2143 mode);
2144 return;
2145 }
2146
2147 if (!enable)
2148 return;
2149
Michal Kazior426e10e2016-03-06 16:14:43 +02002150 ar->htt.tx_q_state.enabled = enable;
2151 ar->htt.tx_q_state.mode = mode;
2152 ar->htt.tx_q_state.num_push_allowed = threshold;
2153
2154 rcu_read_lock();
Michal Kazior839ae632016-03-06 16:14:32 +02002155
2156 for (i = 0; i < num_records; i++) {
2157 record = &resp->tx_mode_switch_ind.records[i];
2158 info0 = le16_to_cpu(record->info0);
2159 peer_id = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_PEER_ID);
2160 tid = MS(info0, HTT_TX_MODE_SWITCH_RECORD_INFO0_TID);
2161
2162 if (unlikely(peer_id >= ar->htt.tx_q_state.num_peers) ||
2163 unlikely(tid >= ar->htt.tx_q_state.num_tids)) {
2164 ath10k_warn(ar, "received out of range peer_id %hu tid %hhu\n",
2165 peer_id, tid);
2166 continue;
2167 }
2168
Michal Kazior426e10e2016-03-06 16:14:43 +02002169 spin_lock_bh(&ar->data_lock);
2170 txq = ath10k_mac_txq_lookup(ar, peer_id, tid);
2171 spin_unlock_bh(&ar->data_lock);
2172
2173 /* It is okay to release the lock and use txq because RCU read
2174 * lock is held.
2175 */
2176
2177 if (unlikely(!txq)) {
2178 ath10k_warn(ar, "failed to lookup txq for peer_id %hu tid %hhu\n",
2179 peer_id, tid);
2180 continue;
2181 }
2182
2183 spin_lock_bh(&ar->htt.tx_lock);
2184 artxq = (void *)txq->drv_priv;
2185 artxq->num_push_allowed = le16_to_cpu(record->num_max_msdus);
2186 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazior839ae632016-03-06 16:14:32 +02002187 }
2188
Michal Kazior426e10e2016-03-06 16:14:43 +02002189 rcu_read_unlock();
2190
2191 ath10k_mac_tx_push_pending(ar);
Michal Kazior839ae632016-03-06 16:14:32 +02002192}
2193
Rajkumar Manoharan2ce9b252016-03-08 22:57:23 +05302194static inline enum ieee80211_band phy_mode_to_band(u32 phy_mode)
2195{
2196 enum ieee80211_band band;
2197
2198 switch (phy_mode) {
2199 case MODE_11A:
2200 case MODE_11NA_HT20:
2201 case MODE_11NA_HT40:
2202 case MODE_11AC_VHT20:
2203 case MODE_11AC_VHT40:
2204 case MODE_11AC_VHT80:
2205 band = IEEE80211_BAND_5GHZ;
2206 break;
2207 case MODE_11G:
2208 case MODE_11B:
2209 case MODE_11GONLY:
2210 case MODE_11NG_HT20:
2211 case MODE_11NG_HT40:
2212 case MODE_11AC_VHT20_2G:
2213 case MODE_11AC_VHT40_2G:
2214 case MODE_11AC_VHT80_2G:
2215 default:
2216 band = IEEE80211_BAND_2GHZ;
2217 }
2218
2219 return band;
2220}
2221
Rajkumar Manoharane3a91f872016-03-22 17:22:16 +05302222void ath10k_htt_htc_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
2223{
2224 bool release;
2225
2226 release = ath10k_htt_t2h_msg_handler(ar, skb);
2227
2228 /* Free the indication buffer */
2229 if (release)
2230 dev_kfree_skb_any(skb);
2231}
2232
2233bool ath10k_htt_t2h_msg_handler(struct ath10k *ar, struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002234{
Michal Kazioredb82362013-07-05 16:15:14 +03002235 struct ath10k_htt *htt = &ar->htt;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002236 struct htt_resp *resp = (struct htt_resp *)skb->data;
Rajkumar Manoharan8348db22015-03-25 13:12:27 +02002237 enum htt_t2h_msg_type type;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002238
2239 /* confirm alignment */
2240 if (!IS_ALIGNED((unsigned long)skb->data, 4))
Michal Kazior7aa7a722014-08-25 12:09:38 +02002241 ath10k_warn(ar, "unaligned htt message, expect trouble\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002242
Michal Kazior7aa7a722014-08-25 12:09:38 +02002243 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, msg_type: 0x%0X\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002244 resp->hdr.msg_type);
Rajkumar Manoharan8348db22015-03-25 13:12:27 +02002245
2246 if (resp->hdr.msg_type >= ar->htt.t2h_msg_types_max) {
2247 ath10k_dbg(ar, ATH10K_DBG_HTT, "htt rx, unsupported msg_type: 0x%0X\n max: 0x%0X",
2248 resp->hdr.msg_type, ar->htt.t2h_msg_types_max);
Rajkumar Manoharane3a91f872016-03-22 17:22:16 +05302249 return true;
Rajkumar Manoharan8348db22015-03-25 13:12:27 +02002250 }
2251 type = ar->htt.t2h_msg_types[resp->hdr.msg_type];
2252
2253 switch (type) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002254 case HTT_T2H_MSG_TYPE_VERSION_CONF: {
2255 htt->target_version_major = resp->ver_resp.major;
2256 htt->target_version_minor = resp->ver_resp.minor;
2257 complete(&htt->target_version_received);
2258 break;
2259 }
Michal Kazior6c5151a2014-02-27 18:50:04 +02002260 case HTT_T2H_MSG_TYPE_RX_IND:
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05302261 ath10k_htt_rx_proc_rx_ind(htt, &resp->rx_ind);
2262 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002263 case HTT_T2H_MSG_TYPE_PEER_MAP: {
2264 struct htt_peer_map_event ev = {
2265 .vdev_id = resp->peer_map.vdev_id,
2266 .peer_id = __le16_to_cpu(resp->peer_map.peer_id),
2267 };
2268 memcpy(ev.addr, resp->peer_map.addr, sizeof(ev.addr));
2269 ath10k_peer_map_event(htt, &ev);
2270 break;
2271 }
2272 case HTT_T2H_MSG_TYPE_PEER_UNMAP: {
2273 struct htt_peer_unmap_event ev = {
2274 .peer_id = __le16_to_cpu(resp->peer_unmap.peer_id),
2275 };
2276 ath10k_peer_unmap_event(htt, &ev);
2277 break;
2278 }
2279 case HTT_T2H_MSG_TYPE_MGMT_TX_COMPLETION: {
2280 struct htt_tx_done tx_done = {};
2281 int status = __le32_to_cpu(resp->mgmt_tx_completion.status);
2282
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302283 tx_done.msdu_id = __le32_to_cpu(resp->mgmt_tx_completion.desc_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002284
2285 switch (status) {
2286 case HTT_MGMT_TX_STATUS_OK:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302287 tx_done.status = HTT_TX_COMPL_STATE_ACK;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002288 break;
2289 case HTT_MGMT_TX_STATUS_RETRY:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302290 tx_done.status = HTT_TX_COMPL_STATE_NOACK;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002291 break;
2292 case HTT_MGMT_TX_STATUS_DROP:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302293 tx_done.status = HTT_TX_COMPL_STATE_DISCARD;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002294 break;
2295 }
2296
Rajkumar Manoharancac08552016-03-09 20:25:46 +05302297 status = ath10k_txrx_tx_unref(htt, &tx_done);
2298 if (!status) {
2299 spin_lock_bh(&htt->tx_lock);
2300 ath10k_htt_tx_mgmt_dec_pending(htt);
2301 spin_unlock_bh(&htt->tx_lock);
2302 }
Michal Kazior29946872016-03-06 16:14:34 +02002303 ath10k_mac_tx_push_pending(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002304 break;
2305 }
Michal Kazior6c5151a2014-02-27 18:50:04 +02002306 case HTT_T2H_MSG_TYPE_TX_COMPL_IND:
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302307 ath10k_htt_rx_tx_compl_ind(htt->ar, skb);
Michal Kazior6c5151a2014-02-27 18:50:04 +02002308 tasklet_schedule(&htt->txrx_compl_task);
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302309 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002310 case HTT_T2H_MSG_TYPE_SEC_IND: {
2311 struct ath10k *ar = htt->ar;
2312 struct htt_security_indication *ev = &resp->security_indication;
2313
Michal Kazior7aa7a722014-08-25 12:09:38 +02002314 ath10k_dbg(ar, ATH10K_DBG_HTT,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002315 "sec ind peer_id %d unicast %d type %d\n",
2316 __le16_to_cpu(ev->peer_id),
2317 !!(ev->flags & HTT_SECURITY_IS_UNICAST),
2318 MS(ev->flags, HTT_SECURITY_TYPE));
2319 complete(&ar->install_key_done);
2320 break;
2321 }
2322 case HTT_T2H_MSG_TYPE_RX_FRAG_IND: {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002323 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002324 skb->data, skb->len);
Rajkumar Manoharan18235662016-03-22 17:22:14 +05302325 ath10k_htt_rx_frag_handler(htt);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002326 break;
2327 }
2328 case HTT_T2H_MSG_TYPE_TEST:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002329 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002330 case HTT_T2H_MSG_TYPE_STATS_CONF:
Michal Kaziord35a6c12014-09-02 11:00:21 +03002331 trace_ath10k_htt_stats(ar, skb->data, skb->len);
Kalle Valoa9bf0502013-09-03 11:43:55 +03002332 break;
2333 case HTT_T2H_MSG_TYPE_TX_INSPECT_IND:
Michal Kazior708b9bd2014-07-21 20:52:59 +03002334 /* Firmware can return tx frames if it's unable to fully
2335 * process them and suspects host may be able to fix it. ath10k
2336 * sends all tx frames as already inspected so this shouldn't
2337 * happen unless fw has a bug.
2338 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02002339 ath10k_warn(ar, "received an unexpected htt tx inspect event\n");
Michal Kazior708b9bd2014-07-21 20:52:59 +03002340 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002341 case HTT_T2H_MSG_TYPE_RX_ADDBA:
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02002342 ath10k_htt_rx_addba(ar, resp);
2343 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002344 case HTT_T2H_MSG_TYPE_RX_DELBA:
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02002345 ath10k_htt_rx_delba(ar, resp);
2346 break;
Rajkumar Manoharanbfdd7932014-10-03 08:02:40 +03002347 case HTT_T2H_MSG_TYPE_PKTLOG: {
2348 struct ath10k_pktlog_hdr *hdr =
2349 (struct ath10k_pktlog_hdr *)resp->pktlog_msg.payload;
2350
2351 trace_ath10k_htt_pktlog(ar, resp->pktlog_msg.payload,
2352 sizeof(*hdr) +
2353 __le16_to_cpu(hdr->size));
2354 break;
2355 }
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02002356 case HTT_T2H_MSG_TYPE_RX_FLUSH: {
2357 /* Ignore this event because mac80211 takes care of Rx
2358 * aggregation reordering.
2359 */
2360 break;
2361 }
Michal Kaziorc5450702015-01-24 12:14:48 +02002362 case HTT_T2H_MSG_TYPE_RX_IN_ORD_PADDR_IND: {
Rajkumar Manoharane7827e52016-02-12 11:40:58 +05302363 skb_queue_tail(&htt->rx_in_ord_compl_q, skb);
Michal Kaziorc5450702015-01-24 12:14:48 +02002364 tasklet_schedule(&htt->txrx_compl_task);
Rajkumar Manoharane3a91f872016-03-22 17:22:16 +05302365 return false;
Michal Kaziorc5450702015-01-24 12:14:48 +02002366 }
2367 case HTT_T2H_MSG_TYPE_TX_CREDIT_UPDATE_IND:
Rajkumar Manoharan8348db22015-03-25 13:12:27 +02002368 break;
Rajkumar Manoharan2ce9b252016-03-08 22:57:23 +05302369 case HTT_T2H_MSG_TYPE_CHAN_CHANGE: {
2370 u32 phymode = __le32_to_cpu(resp->chan_change.phymode);
2371 u32 freq = __le32_to_cpu(resp->chan_change.freq);
2372
2373 ar->tgt_oper_chan =
2374 __ieee80211_get_channel(ar->hw->wiphy, freq);
2375 ath10k_dbg(ar, ATH10K_DBG_HTT,
2376 "htt chan change freq %u phymode %s\n",
2377 freq, ath10k_wmi_phymode_str(phymode));
Michal Kaziorc5450702015-01-24 12:14:48 +02002378 break;
Rajkumar Manoharan2ce9b252016-03-08 22:57:23 +05302379 }
David Liuccec9032015-07-24 20:25:32 +03002380 case HTT_T2H_MSG_TYPE_AGGR_CONF:
2381 break;
Rajkumar Manoharanb2fdbcc2016-03-22 17:22:12 +05302382 case HTT_T2H_MSG_TYPE_TX_FETCH_IND: {
2383 struct sk_buff *tx_fetch_ind = skb_copy(skb, GFP_ATOMIC);
2384
2385 if (!tx_fetch_ind) {
2386 ath10k_warn(ar, "failed to copy htt tx fetch ind\n");
2387 break;
2388 }
2389 skb_queue_tail(&htt->tx_fetch_ind_q, tx_fetch_ind);
Michal Kazior426e10e2016-03-06 16:14:43 +02002390 tasklet_schedule(&htt->txrx_compl_task);
Rajkumar Manoharanb2fdbcc2016-03-22 17:22:12 +05302391 break;
2392 }
Michal Kaziordf94e702016-01-21 14:13:23 +01002393 case HTT_T2H_MSG_TYPE_TX_FETCH_CONFIRM:
Michal Kazior839ae632016-03-06 16:14:32 +02002394 ath10k_htt_rx_tx_fetch_confirm(ar, skb);
2395 break;
Michal Kaziordf94e702016-01-21 14:13:23 +01002396 case HTT_T2H_MSG_TYPE_TX_MODE_SWITCH_IND:
Michal Kazior839ae632016-03-06 16:14:32 +02002397 ath10k_htt_rx_tx_mode_switch_ind(ar, skb);
Michal Kazior9b158732016-01-21 14:13:27 +01002398 break;
2399 case HTT_T2H_MSG_TYPE_EN_STATS:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002400 default:
Michal Kazior2358a542014-10-02 13:32:55 +02002401 ath10k_warn(ar, "htt event (%d) not handled\n",
2402 resp->hdr.msg_type);
Michal Kazior7aa7a722014-08-25 12:09:38 +02002403 ath10k_dbg_dump(ar, ATH10K_DBG_HTT_DUMP, NULL, "htt event: ",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002404 skb->data, skb->len);
2405 break;
2406 };
Rajkumar Manoharane3a91f872016-03-22 17:22:16 +05302407 return true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002408}
Rajkumar Manoharan3f0f7ed2015-10-12 18:27:03 +05302409EXPORT_SYMBOL(ath10k_htt_t2h_msg_handler);
Michal Kazior6c5151a2014-02-27 18:50:04 +02002410
Vivek Natarajanafb0bf72015-10-30 14:57:58 +05302411void ath10k_htt_rx_pktlog_completion_handler(struct ath10k *ar,
2412 struct sk_buff *skb)
2413{
Ashok Raj Nagarajan53a5c9b2016-02-05 21:12:48 +05302414 trace_ath10k_htt_pktlog(ar, skb->data, skb->len);
Vivek Natarajanafb0bf72015-10-30 14:57:58 +05302415 dev_kfree_skb_any(skb);
2416}
2417EXPORT_SYMBOL(ath10k_htt_rx_pktlog_completion_handler);
2418
Michal Kazior6c5151a2014-02-27 18:50:04 +02002419static void ath10k_htt_txrx_compl_task(unsigned long ptr)
2420{
2421 struct ath10k_htt *htt = (struct ath10k_htt *)ptr;
Michal Kaziorc5450702015-01-24 12:14:48 +02002422 struct ath10k *ar = htt->ar;
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302423 struct htt_tx_done tx_done = {};
Rajkumar Manoharanda6416c2016-02-12 11:40:59 +05302424 struct sk_buff_head rx_q;
2425 struct sk_buff_head rx_ind_q;
Michal Kazior426e10e2016-03-06 16:14:43 +02002426 struct sk_buff_head tx_ind_q;
Michal Kazior6c5151a2014-02-27 18:50:04 +02002427 struct sk_buff *skb;
Michal Kaziord742c962016-01-13 14:52:52 +01002428 unsigned long flags;
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05302429 int num_mpdus;
Michal Kazior6c5151a2014-02-27 18:50:04 +02002430
Rajkumar Manoharanda6416c2016-02-12 11:40:59 +05302431 __skb_queue_head_init(&rx_q);
2432 __skb_queue_head_init(&rx_ind_q);
Michal Kazior426e10e2016-03-06 16:14:43 +02002433 __skb_queue_head_init(&tx_ind_q);
Michal Kaziord742c962016-01-13 14:52:52 +01002434
Rajkumar Manoharanda6416c2016-02-12 11:40:59 +05302435 spin_lock_irqsave(&htt->rx_in_ord_compl_q.lock, flags);
2436 skb_queue_splice_init(&htt->rx_in_ord_compl_q, &rx_ind_q);
2437 spin_unlock_irqrestore(&htt->rx_in_ord_compl_q.lock, flags);
2438
Michal Kazior426e10e2016-03-06 16:14:43 +02002439 spin_lock_irqsave(&htt->tx_fetch_ind_q.lock, flags);
2440 skb_queue_splice_init(&htt->tx_fetch_ind_q, &tx_ind_q);
2441 spin_unlock_irqrestore(&htt->tx_fetch_ind_q.lock, flags);
2442
Rajkumar Manoharan59465fe2016-03-22 17:22:11 +05302443 /* kfifo_get: called only within txrx_tasklet so it's neatly serialized.
2444 * From kfifo_get() documentation:
2445 * Note that with only one concurrent reader and one concurrent writer,
2446 * you don't need extra locking to use these macro.
2447 */
2448 while (kfifo_get(&htt->txdone_fifo, &tx_done))
2449 ath10k_txrx_tx_unref(htt, &tx_done);
Michal Kazior6c5151a2014-02-27 18:50:04 +02002450
Michal Kazior426e10e2016-03-06 16:14:43 +02002451 while ((skb = __skb_dequeue(&tx_ind_q))) {
2452 ath10k_htt_rx_tx_fetch_ind(ar, skb);
2453 dev_kfree_skb_any(skb);
2454 }
2455
Michal Kazior29946872016-03-06 16:14:34 +02002456 ath10k_mac_tx_push_pending(ar);
2457
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05302458 num_mpdus = atomic_read(&htt->num_mpdus_ready);
2459 atomic_sub(num_mpdus, &htt->num_mpdus_ready);
2460
2461 while (num_mpdus--) {
2462 if (ath10k_htt_rx_handle_amsdu(htt))
2463 break;
Michal Kazior6c5151a2014-02-27 18:50:04 +02002464 }
Michal Kaziorc5450702015-01-24 12:14:48 +02002465
Rajkumar Manoharanda6416c2016-02-12 11:40:59 +05302466 while ((skb = __skb_dequeue(&rx_ind_q))) {
Rajkumar Manoharane7827e52016-02-12 11:40:58 +05302467 spin_lock_bh(&htt->rx_ring.lock);
Michal Kaziorc5450702015-01-24 12:14:48 +02002468 ath10k_htt_rx_in_ord_ind(ar, skb);
Rajkumar Manoharane7827e52016-02-12 11:40:58 +05302469 spin_unlock_bh(&htt->rx_ring.lock);
Michal Kaziorc5450702015-01-24 12:14:48 +02002470 dev_kfree_skb_any(skb);
2471 }
Rajkumar Manoharan3128b3d2016-03-22 17:22:15 +05302472
2473 tasklet_schedule(&htt->rx_replenish_task);
Michal Kazior6c5151a2014-02-27 18:50:04 +02002474}