]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/ath/ath9k/recv.c
ath9k: add support for the new rate control API
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / ath / ath9k / recv.c
CommitLineData
f078f209 1/*
5b68138e 2 * Copyright (c) 2008-2011 Atheros Communications Inc.
f078f209
LR
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
b7f080cf 17#include <linux/dma-mapping.h>
e93d083f 18#include <linux/relay.h>
394cf0a1 19#include "ath9k.h"
b622a720 20#include "ar9003_mac.h"
f078f209 21
b5c80475
FF
22#define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
23
ededf1f8
VT
24static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
25{
26 return sc->ps_enabled &&
27 (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
28}
29
f078f209
LR
30/*
31 * Setup and link descriptors.
32 *
33 * 11N: we can no longer afford to self link the last descriptor.
34 * MAC acknowledges BA status as long as it copies frames to host
35 * buffer (or rx fifo). This can incorrectly acknowledge packets
36 * to a sender if last desc is self-linked.
f078f209 37 */
f078f209
LR
38static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
39{
cbe61d8a 40 struct ath_hw *ah = sc->sc_ah;
cc861f74 41 struct ath_common *common = ath9k_hw_common(ah);
f078f209
LR
42 struct ath_desc *ds;
43 struct sk_buff *skb;
44
45 ATH_RXBUF_RESET(bf);
46
47 ds = bf->bf_desc;
be0418ad 48 ds->ds_link = 0; /* link to null */
f078f209
LR
49 ds->ds_data = bf->bf_buf_addr;
50
be0418ad 51 /* virtual addr of the beginning of the buffer. */
f078f209 52 skb = bf->bf_mpdu;
9680e8a3 53 BUG_ON(skb == NULL);
f078f209
LR
54 ds->ds_vdata = skb->data;
55
cc861f74
LR
56 /*
57 * setup rx descriptors. The rx_bufsize here tells the hardware
b4b6cda2 58 * how much data it can DMA to us and that we are prepared
cc861f74
LR
59 * to process
60 */
b77f483f 61 ath9k_hw_setuprxdesc(ah, ds,
cc861f74 62 common->rx_bufsize,
f078f209
LR
63 0);
64
b77f483f 65 if (sc->rx.rxlink == NULL)
f078f209
LR
66 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
67 else
b77f483f 68 *sc->rx.rxlink = bf->bf_daddr;
f078f209 69
b77f483f 70 sc->rx.rxlink = &ds->ds_link;
f078f209
LR
71}
72
ff37e337
S
73static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
74{
75 /* XXX block beacon interrupts */
76 ath9k_hw_setantenna(sc->sc_ah, antenna);
b77f483f
S
77 sc->rx.defant = antenna;
78 sc->rx.rxotherant = 0;
ff37e337
S
79}
80
f078f209
LR
81static void ath_opmode_init(struct ath_softc *sc)
82{
cbe61d8a 83 struct ath_hw *ah = sc->sc_ah;
1510718d
LR
84 struct ath_common *common = ath9k_hw_common(ah);
85
f078f209
LR
86 u32 rfilt, mfilt[2];
87
88 /* configure rx filter */
89 rfilt = ath_calcrxfilter(sc);
90 ath9k_hw_setrxfilter(ah, rfilt);
91
92 /* configure bssid mask */
364734fa 93 ath_hw_setbssidmask(common);
f078f209
LR
94
95 /* configure operational mode */
96 ath9k_hw_setopmode(ah);
97
f078f209
LR
98 /* calculate and install multicast filter */
99 mfilt[0] = mfilt[1] = ~0;
f078f209 100 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
f078f209
LR
101}
102
b5c80475
FF
103static bool ath_rx_edma_buf_link(struct ath_softc *sc,
104 enum ath9k_rx_qtype qtype)
f078f209 105{
b5c80475
FF
106 struct ath_hw *ah = sc->sc_ah;
107 struct ath_rx_edma *rx_edma;
f078f209
LR
108 struct sk_buff *skb;
109 struct ath_buf *bf;
f078f209 110
b5c80475
FF
111 rx_edma = &sc->rx.rx_edma[qtype];
112 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
113 return false;
f078f209 114
b5c80475
FF
115 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
116 list_del_init(&bf->list);
f078f209 117
b5c80475
FF
118 skb = bf->bf_mpdu;
119
120 ATH_RXBUF_RESET(bf);
121 memset(skb->data, 0, ah->caps.rx_status_len);
122 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
123 ah->caps.rx_status_len, DMA_TO_DEVICE);
f078f209 124
b5c80475
FF
125 SKB_CB_ATHBUF(skb) = bf;
126 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
127 skb_queue_tail(&rx_edma->rx_fifo, skb);
f078f209 128
b5c80475
FF
129 return true;
130}
131
132static void ath_rx_addbuffer_edma(struct ath_softc *sc,
133 enum ath9k_rx_qtype qtype, int size)
134{
b5c80475 135 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
6a01f0c0 136 struct ath_buf *bf, *tbf;
b5c80475 137
b5c80475 138 if (list_empty(&sc->rx.rxbuf)) {
d2182b69 139 ath_dbg(common, QUEUE, "No free rx buf available\n");
b5c80475 140 return;
797fe5cb 141 }
f078f209 142
6a01f0c0 143 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
b5c80475
FF
144 if (!ath_rx_edma_buf_link(sc, qtype))
145 break;
146
b5c80475
FF
147}
148
149static void ath_rx_remove_buffer(struct ath_softc *sc,
150 enum ath9k_rx_qtype qtype)
151{
152 struct ath_buf *bf;
153 struct ath_rx_edma *rx_edma;
154 struct sk_buff *skb;
155
156 rx_edma = &sc->rx.rx_edma[qtype];
157
158 while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
159 bf = SKB_CB_ATHBUF(skb);
160 BUG_ON(!bf);
161 list_add_tail(&bf->list, &sc->rx.rxbuf);
162 }
163}
164
165static void ath_rx_edma_cleanup(struct ath_softc *sc)
166{
ba542385
MSS
167 struct ath_hw *ah = sc->sc_ah;
168 struct ath_common *common = ath9k_hw_common(ah);
b5c80475
FF
169 struct ath_buf *bf;
170
171 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
172 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
173
797fe5cb 174 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
ba542385
MSS
175 if (bf->bf_mpdu) {
176 dma_unmap_single(sc->dev, bf->bf_buf_addr,
177 common->rx_bufsize,
178 DMA_BIDIRECTIONAL);
b5c80475 179 dev_kfree_skb_any(bf->bf_mpdu);
ba542385
MSS
180 bf->bf_buf_addr = 0;
181 bf->bf_mpdu = NULL;
182 }
b5c80475 183 }
b5c80475
FF
184}
185
186static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
187{
188 skb_queue_head_init(&rx_edma->rx_fifo);
b5c80475
FF
189 rx_edma->rx_fifo_hwsize = size;
190}
191
192static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
193{
194 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
195 struct ath_hw *ah = sc->sc_ah;
196 struct sk_buff *skb;
197 struct ath_buf *bf;
198 int error = 0, i;
199 u32 size;
200
b5c80475
FF
201 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
202 ah->caps.rx_status_len);
203
204 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
205 ah->caps.rx_lp_qdepth);
206 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
207 ah->caps.rx_hp_qdepth);
208
209 size = sizeof(struct ath_buf) * nbufs;
b81950b1 210 bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
b5c80475
FF
211 if (!bf)
212 return -ENOMEM;
213
214 INIT_LIST_HEAD(&sc->rx.rxbuf);
b5c80475
FF
215
216 for (i = 0; i < nbufs; i++, bf++) {
cc861f74 217 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
b5c80475 218 if (!skb) {
797fe5cb 219 error = -ENOMEM;
b5c80475 220 goto rx_init_fail;
f078f209 221 }
f078f209 222
b5c80475 223 memset(skb->data, 0, common->rx_bufsize);
797fe5cb 224 bf->bf_mpdu = skb;
b5c80475 225
797fe5cb 226 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
cc861f74 227 common->rx_bufsize,
b5c80475 228 DMA_BIDIRECTIONAL);
797fe5cb 229 if (unlikely(dma_mapping_error(sc->dev,
b5c80475
FF
230 bf->bf_buf_addr))) {
231 dev_kfree_skb_any(skb);
232 bf->bf_mpdu = NULL;
6cf9e995 233 bf->bf_buf_addr = 0;
3800276a 234 ath_err(common,
b5c80475
FF
235 "dma_mapping_error() on RX init\n");
236 error = -ENOMEM;
237 goto rx_init_fail;
238 }
239
240 list_add_tail(&bf->list, &sc->rx.rxbuf);
241 }
242
243 return 0;
244
245rx_init_fail:
246 ath_rx_edma_cleanup(sc);
247 return error;
248}
249
250static void ath_edma_start_recv(struct ath_softc *sc)
251{
b5c80475
FF
252 ath9k_hw_rxena(sc->sc_ah);
253
254 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
255 sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
256
257 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
258 sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
259
b5c80475
FF
260 ath_opmode_init(sc);
261
4cb54fa3 262 ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
b5c80475
FF
263}
264
265static void ath_edma_stop_recv(struct ath_softc *sc)
266{
b5c80475
FF
267 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
268 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
b5c80475
FF
269}
270
271int ath_rx_init(struct ath_softc *sc, int nbufs)
272{
273 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
274 struct sk_buff *skb;
275 struct ath_buf *bf;
276 int error = 0;
277
4bdd1e97 278 spin_lock_init(&sc->sc_pcu_lock);
b5c80475 279
0d95521e
FF
280 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
281 sc->sc_ah->caps.rx_status_len;
282
b5c80475
FF
283 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
284 return ath_rx_edma_init(sc, nbufs);
285 } else {
d2182b69 286 ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
226afe68 287 common->cachelsz, common->rx_bufsize);
b5c80475
FF
288
289 /* Initialize rx descriptors */
290
291 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
4adfcded 292 "rx", nbufs, 1, 0);
b5c80475 293 if (error != 0) {
3800276a
JP
294 ath_err(common,
295 "failed to allocate rx descriptors: %d\n",
296 error);
797fe5cb
S
297 goto err;
298 }
b5c80475
FF
299
300 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
301 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
302 GFP_KERNEL);
303 if (skb == NULL) {
304 error = -ENOMEM;
305 goto err;
306 }
307
308 bf->bf_mpdu = skb;
309 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
310 common->rx_bufsize,
311 DMA_FROM_DEVICE);
312 if (unlikely(dma_mapping_error(sc->dev,
313 bf->bf_buf_addr))) {
314 dev_kfree_skb_any(skb);
315 bf->bf_mpdu = NULL;
6cf9e995 316 bf->bf_buf_addr = 0;
3800276a
JP
317 ath_err(common,
318 "dma_mapping_error() on RX init\n");
b5c80475
FF
319 error = -ENOMEM;
320 goto err;
321 }
b5c80475
FF
322 }
323 sc->rx.rxlink = NULL;
797fe5cb 324 }
f078f209 325
797fe5cb 326err:
f078f209
LR
327 if (error)
328 ath_rx_cleanup(sc);
329
330 return error;
331}
332
f078f209
LR
333void ath_rx_cleanup(struct ath_softc *sc)
334{
cc861f74
LR
335 struct ath_hw *ah = sc->sc_ah;
336 struct ath_common *common = ath9k_hw_common(ah);
f078f209
LR
337 struct sk_buff *skb;
338 struct ath_buf *bf;
339
b5c80475
FF
340 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
341 ath_rx_edma_cleanup(sc);
342 return;
343 } else {
344 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
345 skb = bf->bf_mpdu;
346 if (skb) {
347 dma_unmap_single(sc->dev, bf->bf_buf_addr,
348 common->rx_bufsize,
349 DMA_FROM_DEVICE);
350 dev_kfree_skb(skb);
6cf9e995
BG
351 bf->bf_buf_addr = 0;
352 bf->bf_mpdu = NULL;
b5c80475 353 }
051b9191 354 }
b5c80475 355 }
f078f209
LR
356}
357
358/*
359 * Calculate the receive filter according to the
360 * operating mode and state:
361 *
362 * o always accept unicast, broadcast, and multicast traffic
363 * o maintain current state of phy error reception (the hal
364 * may enable phy error frames for noise immunity work)
365 * o probe request frames are accepted only when operating in
366 * hostap, adhoc, or monitor modes
367 * o enable promiscuous mode according to the interface state
368 * o accept beacons:
369 * - when operating in adhoc mode so the 802.11 layer creates
370 * node table entries for peers,
371 * - when operating in station mode for collecting rssi data when
372 * the station is otherwise quiet, or
373 * - when operating as a repeater so we see repeater-sta beacons
374 * - when scanning
375 */
376
377u32 ath_calcrxfilter(struct ath_softc *sc)
378{
f078f209
LR
379 u32 rfilt;
380
ac06697c 381 rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
f078f209
LR
382 | ATH9K_RX_FILTER_MCAST;
383
73e4937d
ZK
384 /* if operating on a DFS channel, enable radar pulse detection */
385 if (sc->hw->conf.radar_enabled)
386 rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
387
9c1d8e4a 388 if (sc->rx.rxfilter & FIF_PROBE_REQ)
f078f209
LR
389 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
390
217ba9da
JM
391 /*
392 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
393 * mode interface or when in monitor mode. AP mode does not need this
394 * since it receives all in-BSS frames anyway.
395 */
2e286947 396 if (sc->sc_ah->is_monitoring)
f078f209 397 rfilt |= ATH9K_RX_FILTER_PROM;
f078f209 398
d42c6b71
S
399 if (sc->rx.rxfilter & FIF_CONTROL)
400 rfilt |= ATH9K_RX_FILTER_CONTROL;
401
dbaaa147 402 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
cfda6695 403 (sc->nvifs <= 1) &&
dbaaa147
VT
404 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
405 rfilt |= ATH9K_RX_FILTER_MYBEACON;
406 else
f078f209
LR
407 rfilt |= ATH9K_RX_FILTER_BEACON;
408
264bbec8 409 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
66afad01 410 (sc->rx.rxfilter & FIF_PSPOLL))
dbaaa147 411 rfilt |= ATH9K_RX_FILTER_PSPOLL;
be0418ad 412
7ea310be
S
413 if (conf_is_ht(&sc->hw->conf))
414 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
415
7545daf4 416 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
a549459c
TW
417 /* This is needed for older chips */
418 if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
5eb6ba83 419 rfilt |= ATH9K_RX_FILTER_PROM;
b93bce2a
JM
420 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
421 }
422
b3d7aa43
GJ
423 if (AR_SREV_9550(sc->sc_ah))
424 rfilt |= ATH9K_RX_FILTER_4ADDRESS;
425
f078f209 426 return rfilt;
7dcfdcd9 427
f078f209
LR
428}
429
f078f209
LR
430int ath_startrecv(struct ath_softc *sc)
431{
cbe61d8a 432 struct ath_hw *ah = sc->sc_ah;
f078f209
LR
433 struct ath_buf *bf, *tbf;
434
b5c80475
FF
435 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
436 ath_edma_start_recv(sc);
437 return 0;
438 }
439
b77f483f 440 if (list_empty(&sc->rx.rxbuf))
f078f209
LR
441 goto start_recv;
442
b77f483f
S
443 sc->rx.rxlink = NULL;
444 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
f078f209
LR
445 ath_rx_buf_link(sc, bf);
446 }
447
448 /* We could have deleted elements so the list may be empty now */
b77f483f 449 if (list_empty(&sc->rx.rxbuf))
f078f209
LR
450 goto start_recv;
451
b77f483f 452 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
f078f209 453 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
be0418ad 454 ath9k_hw_rxena(ah);
f078f209
LR
455
456start_recv:
be0418ad 457 ath_opmode_init(sc);
4cb54fa3 458 ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
be0418ad 459
f078f209
LR
460 return 0;
461}
462
4b883f02
FF
463static void ath_flushrecv(struct ath_softc *sc)
464{
465 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
466 ath_rx_tasklet(sc, 1, true);
467 ath_rx_tasklet(sc, 1, false);
468}
469
f078f209
LR
470bool ath_stoprecv(struct ath_softc *sc)
471{
cbe61d8a 472 struct ath_hw *ah = sc->sc_ah;
5882da02 473 bool stopped, reset = false;
f078f209 474
d47844a0 475 ath9k_hw_abortpcurecv(ah);
be0418ad 476 ath9k_hw_setrxfilter(ah, 0);
5882da02 477 stopped = ath9k_hw_stopdmarecv(ah, &reset);
b5c80475 478
4b883f02
FF
479 ath_flushrecv(sc);
480
b5c80475
FF
481 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
482 ath_edma_stop_recv(sc);
483 else
484 sc->rx.rxlink = NULL;
be0418ad 485
d584747b
RM
486 if (!(ah->ah_flags & AH_UNPLUGGED) &&
487 unlikely(!stopped)) {
d7fd1b50
BG
488 ath_err(ath9k_hw_common(sc->sc_ah),
489 "Could not stop RX, we could be "
490 "confusing the DMA engine when we start RX up\n");
491 ATH_DBG_WARN_ON_ONCE(!stopped);
492 }
2232d31b 493 return stopped && !reset;
f078f209
LR
494}
495
cc65965c
JM
496static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
497{
498 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
499 struct ieee80211_mgmt *mgmt;
500 u8 *pos, *end, id, elen;
501 struct ieee80211_tim_ie *tim;
502
503 mgmt = (struct ieee80211_mgmt *)skb->data;
504 pos = mgmt->u.beacon.variable;
505 end = skb->data + skb->len;
506
507 while (pos + 2 < end) {
508 id = *pos++;
509 elen = *pos++;
510 if (pos + elen > end)
511 break;
512
513 if (id == WLAN_EID_TIM) {
514 if (elen < sizeof(*tim))
515 break;
516 tim = (struct ieee80211_tim_ie *) pos;
517 if (tim->dtim_count != 0)
518 break;
519 return tim->bitmap_ctrl & 0x01;
520 }
521
522 pos += elen;
523 }
524
525 return false;
526}
527
cc65965c
JM
528static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
529{
1510718d 530 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
cc65965c
JM
531
532 if (skb->len < 24 + 8 + 2 + 2)
533 return;
534
1b04b930 535 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
293dc5df 536
1b04b930
S
537 if (sc->ps_flags & PS_BEACON_SYNC) {
538 sc->ps_flags &= ~PS_BEACON_SYNC;
d2182b69 539 ath_dbg(common, PS,
1a6404a1 540 "Reconfigure beacon timers based on synchronized timestamp\n");
ef4ad633 541 ath9k_set_beacon(sc);
ccdfeab6
JM
542 }
543
cc65965c
JM
544 if (ath_beacon_dtim_pending_cab(skb)) {
545 /*
546 * Remain awake waiting for buffered broadcast/multicast
58f5fffd
GJ
547 * frames. If the last broadcast/multicast frame is not
548 * received properly, the next beacon frame will work as
549 * a backup trigger for returning into NETWORK SLEEP state,
550 * so we are waiting for it as well.
cc65965c 551 */
d2182b69 552 ath_dbg(common, PS,
226afe68 553 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
1b04b930 554 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
cc65965c
JM
555 return;
556 }
557
1b04b930 558 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
cc65965c
JM
559 /*
560 * This can happen if a broadcast frame is dropped or the AP
561 * fails to send a frame indicating that all CAB frames have
562 * been delivered.
563 */
1b04b930 564 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
d2182b69 565 ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
cc65965c 566 }
cc65965c
JM
567}
568
f73c604c 569static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
cc65965c
JM
570{
571 struct ieee80211_hdr *hdr;
c46917bb 572 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
cc65965c
JM
573
574 hdr = (struct ieee80211_hdr *)skb->data;
575
576 /* Process Beacon and CAB receive in PS state */
ededf1f8 577 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
07c15a3f 578 && mybeacon) {
cc65965c 579 ath_rx_ps_beacon(sc, skb);
07c15a3f
SM
580 } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
581 (ieee80211_is_data(hdr->frame_control) ||
582 ieee80211_is_action(hdr->frame_control)) &&
583 is_multicast_ether_addr(hdr->addr1) &&
584 !ieee80211_has_moredata(hdr->frame_control)) {
cc65965c
JM
585 /*
586 * No more broadcast/multicast frames to be received at this
587 * point.
588 */
3fac6dfd 589 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
d2182b69 590 ath_dbg(common, PS,
226afe68 591 "All PS CAB frames received, back to sleep\n");
1b04b930 592 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
9a23f9ca
JM
593 !is_multicast_ether_addr(hdr->addr1) &&
594 !ieee80211_has_morefrags(hdr->frame_control)) {
1b04b930 595 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
d2182b69 596 ath_dbg(common, PS,
226afe68 597 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
1b04b930
S
598 sc->ps_flags & (PS_WAIT_FOR_BEACON |
599 PS_WAIT_FOR_CAB |
600 PS_WAIT_FOR_PSPOLL_DATA |
601 PS_WAIT_FOR_TX_ACK));
cc65965c
JM
602 }
603}
604
b5c80475 605static bool ath_edma_get_buffers(struct ath_softc *sc,
3a2923e8
FF
606 enum ath9k_rx_qtype qtype,
607 struct ath_rx_status *rs,
608 struct ath_buf **dest)
f078f209 609{
b5c80475
FF
610 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
611 struct ath_hw *ah = sc->sc_ah;
612 struct ath_common *common = ath9k_hw_common(ah);
613 struct sk_buff *skb;
614 struct ath_buf *bf;
615 int ret;
616
617 skb = skb_peek(&rx_edma->rx_fifo);
618 if (!skb)
619 return false;
620
621 bf = SKB_CB_ATHBUF(skb);
622 BUG_ON(!bf);
623
ce9426d1 624 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
b5c80475
FF
625 common->rx_bufsize, DMA_FROM_DEVICE);
626
3a2923e8 627 ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
ce9426d1
ML
628 if (ret == -EINPROGRESS) {
629 /*let device gain the buffer again*/
630 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
631 common->rx_bufsize, DMA_FROM_DEVICE);
b5c80475 632 return false;
ce9426d1 633 }
b5c80475
FF
634
635 __skb_unlink(skb, &rx_edma->rx_fifo);
636 if (ret == -EINVAL) {
637 /* corrupt descriptor, skip this one and the following one */
638 list_add_tail(&bf->list, &sc->rx.rxbuf);
639 ath_rx_edma_buf_link(sc, qtype);
b5c80475 640
3a2923e8
FF
641 skb = skb_peek(&rx_edma->rx_fifo);
642 if (skb) {
643 bf = SKB_CB_ATHBUF(skb);
644 BUG_ON(!bf);
645
646 __skb_unlink(skb, &rx_edma->rx_fifo);
647 list_add_tail(&bf->list, &sc->rx.rxbuf);
648 ath_rx_edma_buf_link(sc, qtype);
3a2923e8 649 }
6bb51c70
TH
650
651 bf = NULL;
b5c80475 652 }
b5c80475 653
3a2923e8 654 *dest = bf;
b5c80475
FF
655 return true;
656}
f078f209 657
b5c80475
FF
658static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
659 struct ath_rx_status *rs,
660 enum ath9k_rx_qtype qtype)
661{
3a2923e8 662 struct ath_buf *bf = NULL;
b5c80475 663
3a2923e8
FF
664 while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
665 if (!bf)
666 continue;
b5c80475 667
3a2923e8
FF
668 return bf;
669 }
670 return NULL;
b5c80475
FF
671}
672
673static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
674 struct ath_rx_status *rs)
675{
676 struct ath_hw *ah = sc->sc_ah;
677 struct ath_common *common = ath9k_hw_common(ah);
f078f209 678 struct ath_desc *ds;
b5c80475
FF
679 struct ath_buf *bf;
680 int ret;
681
682 if (list_empty(&sc->rx.rxbuf)) {
683 sc->rx.rxlink = NULL;
684 return NULL;
685 }
686
687 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
688 ds = bf->bf_desc;
689
690 /*
691 * Must provide the virtual address of the current
692 * descriptor, the physical address, and the virtual
693 * address of the next descriptor in the h/w chain.
694 * This allows the HAL to look ahead to see if the
695 * hardware is done with a descriptor by checking the
696 * done bit in the following descriptor and the address
697 * of the current descriptor the DMA engine is working
698 * on. All this is necessary because of our use of
699 * a self-linked list to avoid rx overruns.
700 */
3de21116 701 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
b5c80475
FF
702 if (ret == -EINPROGRESS) {
703 struct ath_rx_status trs;
704 struct ath_buf *tbf;
705 struct ath_desc *tds;
706
707 memset(&trs, 0, sizeof(trs));
708 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
709 sc->rx.rxlink = NULL;
710 return NULL;
711 }
712
713 tbf = list_entry(bf->list.next, struct ath_buf, list);
714
715 /*
716 * On some hardware the descriptor status words could
717 * get corrupted, including the done bit. Because of
718 * this, check if the next descriptor's done bit is
719 * set or not.
720 *
721 * If the next descriptor's done bit is set, the current
722 * descriptor has been corrupted. Force s/w to discard
723 * this descriptor and continue...
724 */
725
726 tds = tbf->bf_desc;
3de21116 727 ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
b5c80475
FF
728 if (ret == -EINPROGRESS)
729 return NULL;
723e7113
FF
730
731 /*
732 * mark descriptor as zero-length and set the 'more'
733 * flag to ensure that both buffers get discarded
734 */
735 rs->rs_datalen = 0;
736 rs->rs_more = true;
b5c80475
FF
737 }
738
a3dc48e8 739 list_del(&bf->list);
b5c80475
FF
740 if (!bf->bf_mpdu)
741 return bf;
742
743 /*
744 * Synchronize the DMA transfer with CPU before
745 * 1. accessing the frame
746 * 2. requeueing the same buffer to h/w
747 */
ce9426d1 748 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
b5c80475
FF
749 common->rx_bufsize,
750 DMA_FROM_DEVICE);
751
752 return bf;
753}
754
d435700f
S
755/* Assumes you've already done the endian to CPU conversion */
756static bool ath9k_rx_accept(struct ath_common *common,
9f167f64 757 struct ieee80211_hdr *hdr,
d435700f
S
758 struct ieee80211_rx_status *rxs,
759 struct ath_rx_status *rx_stats,
760 bool *decrypt_error)
761{
ec205999 762 struct ath_softc *sc = (struct ath_softc *) common->priv;
66760eac 763 bool is_mc, is_valid_tkip, strip_mic, mic_error;
d435700f 764 struct ath_hw *ah = common->ah;
d435700f 765 __le16 fc;
b7b1b512 766 u8 rx_status_len = ah->caps.rx_status_len;
d435700f 767
d435700f
S
768 fc = hdr->frame_control;
769
66760eac
FF
770 is_mc = !!is_multicast_ether_addr(hdr->addr1);
771 is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
772 test_bit(rx_stats->rs_keyix, common->tkip_keymap);
152e585d 773 strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
2a5783b8 774 ieee80211_has_protected(fc) &&
152e585d 775 !(rx_stats->rs_status &
846d9363
FF
776 (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
777 ATH9K_RXERR_KEYMISS));
66760eac 778
f88373fa
FF
779 /*
780 * Key miss events are only relevant for pairwise keys where the
781 * descriptor does contain a valid key index. This has been observed
782 * mostly with CCMP encryption.
783 */
bed3d9c0
FF
784 if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
785 !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
f88373fa
FF
786 rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
787
15072189
BG
788 if (!rx_stats->rs_datalen) {
789 RX_STAT_INC(rx_len_err);
d435700f 790 return false;
15072189
BG
791 }
792
d435700f
S
793 /*
794 * rs_status follows rs_datalen so if rs_datalen is too large
795 * we can take a hint that hardware corrupted it, so ignore
796 * those frames.
797 */
15072189
BG
798 if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len)) {
799 RX_STAT_INC(rx_len_err);
d435700f 800 return false;
15072189 801 }
d435700f 802
0d95521e 803 /* Only use error bits from the last fragment */
d435700f 804 if (rx_stats->rs_more)
0d95521e 805 return true;
d435700f 806
66760eac
FF
807 mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
808 !ieee80211_has_morefrags(fc) &&
809 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
810 (rx_stats->rs_status & ATH9K_RXERR_MIC);
811
d435700f
S
812 /*
813 * The rx_stats->rs_status will not be set until the end of the
814 * chained descriptors so it can be ignored if rs_more is set. The
815 * rs_more will be false at the last element of the chained
816 * descriptors.
817 */
818 if (rx_stats->rs_status != 0) {
846d9363
FF
819 u8 status_mask;
820
66760eac 821 if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
d435700f 822 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
66760eac
FF
823 mic_error = false;
824 }
d435700f
S
825 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
826 return false;
827
846d9363
FF
828 if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
829 (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
d435700f 830 *decrypt_error = true;
66760eac 831 mic_error = false;
d435700f 832 }
66760eac 833
d435700f
S
834 /*
835 * Reject error frames with the exception of
836 * decryption and MIC failures. For monitor mode,
837 * we also ignore the CRC error.
838 */
846d9363
FF
839 status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
840 ATH9K_RXERR_KEYMISS;
841
ec205999 842 if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
846d9363
FF
843 status_mask |= ATH9K_RXERR_CRC;
844
845 if (rx_stats->rs_status & ~status_mask)
846 return false;
d435700f 847 }
66760eac
FF
848
849 /*
850 * For unicast frames the MIC error bit can have false positives,
851 * so all MIC error reports need to be validated in software.
852 * False negatives are not common, so skip software verification
853 * if the hardware considers the MIC valid.
854 */
855 if (strip_mic)
856 rxs->flag |= RX_FLAG_MMIC_STRIPPED;
857 else if (is_mc && mic_error)
858 rxs->flag |= RX_FLAG_MMIC_ERROR;
859
d435700f
S
860 return true;
861}
862
863static int ath9k_process_rate(struct ath_common *common,
864 struct ieee80211_hw *hw,
865 struct ath_rx_status *rx_stats,
9f167f64 866 struct ieee80211_rx_status *rxs)
d435700f
S
867{
868 struct ieee80211_supported_band *sband;
869 enum ieee80211_band band;
870 unsigned int i = 0;
990e08a0 871 struct ath_softc __maybe_unused *sc = common->priv;
d435700f 872
675a0b04 873 band = hw->conf.chandef.chan->band;
d435700f
S
874 sband = hw->wiphy->bands[band];
875
876 if (rx_stats->rs_rate & 0x80) {
877 /* HT rate */
878 rxs->flag |= RX_FLAG_HT;
879 if (rx_stats->rs_flags & ATH9K_RX_2040)
880 rxs->flag |= RX_FLAG_40MHZ;
881 if (rx_stats->rs_flags & ATH9K_RX_GI)
882 rxs->flag |= RX_FLAG_SHORT_GI;
883 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
884 return 0;
885 }
886
887 for (i = 0; i < sband->n_bitrates; i++) {
888 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
889 rxs->rate_idx = i;
890 return 0;
891 }
892 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
893 rxs->flag |= RX_FLAG_SHORTPRE;
894 rxs->rate_idx = i;
895 return 0;
896 }
897 }
898
899 /*
900 * No valid hardware bitrate found -- we should not get here
901 * because hardware has already validated this frame as OK.
902 */
d2182b69 903 ath_dbg(common, ANY,
226afe68
JP
904 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
905 rx_stats->rs_rate);
15072189 906 RX_STAT_INC(rx_rate_err);
d435700f
S
907 return -EINVAL;
908}
909
910static void ath9k_process_rssi(struct ath_common *common,
911 struct ieee80211_hw *hw,
9f167f64 912 struct ieee80211_hdr *hdr,
d435700f
S
913 struct ath_rx_status *rx_stats)
914{
9ac58615 915 struct ath_softc *sc = hw->priv;
d435700f 916 struct ath_hw *ah = common->ah;
9fa23e17 917 int last_rssi;
2ef16755 918 int rssi = rx_stats->rs_rssi;
d435700f 919
cf3af748
RM
920 if (!rx_stats->is_mybeacon ||
921 ((ah->opmode != NL80211_IFTYPE_STATION) &&
922 (ah->opmode != NL80211_IFTYPE_ADHOC)))
9fa23e17
FF
923 return;
924
9fa23e17 925 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
9ac58615 926 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
d435700f 927
9ac58615 928 last_rssi = sc->last_rssi;
d435700f 929 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
2ef16755
FF
930 rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
931 if (rssi < 0)
932 rssi = 0;
d435700f
S
933
934 /* Update Beacon RSSI, this is used by ANI. */
2ef16755 935 ah->stats.avgbrssi = rssi;
d435700f
S
936}
937
938/*
939 * For Decrypt or Demic errors, we only mark packet status here and always push
940 * up the frame up to let mac80211 handle the actual error case, be it no
941 * decryption key or real decryption error. This let us keep statistics there.
942 */
723e7113 943static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
9f167f64 944 struct ieee80211_hdr *hdr,
d435700f
S
945 struct ath_rx_status *rx_stats,
946 struct ieee80211_rx_status *rx_status,
947 bool *decrypt_error)
948{
723e7113
FF
949 struct ieee80211_hw *hw = sc->hw;
950 struct ath_hw *ah = sc->sc_ah;
951 struct ath_common *common = ath9k_hw_common(ah);
952 bool discard_current = sc->rx.discard_next;
953
954 sc->rx.discard_next = rx_stats->rs_more;
955 if (discard_current)
956 return -EINVAL;
f749b946 957
d435700f
S
958 /*
959 * everything but the rate is checked here, the rate check is done
960 * separately to avoid doing two lookups for a rate for each frame.
961 */
9f167f64 962 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
d435700f
S
963 return -EINVAL;
964
0d95521e
FF
965 /* Only use status info from the last fragment */
966 if (rx_stats->rs_more)
967 return 0;
968
9f167f64 969 ath9k_process_rssi(common, hw, hdr, rx_stats);
d435700f 970
9f167f64 971 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
d435700f
S
972 return -EINVAL;
973
675a0b04
KB
974 rx_status->band = hw->conf.chandef.chan->band;
975 rx_status->freq = hw->conf.chandef.chan->center_freq;
f749b946 976 rx_status->signal = ah->noise + rx_stats->rs_rssi;
d435700f 977 rx_status->antenna = rx_stats->rs_antenna;
96d21371 978 rx_status->flag |= RX_FLAG_MACTIME_END;
2ef16755
FF
979 if (rx_stats->rs_moreaggr)
980 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
d435700f 981
723e7113 982 sc->rx.discard_next = false;
d435700f
S
983 return 0;
984}
985
986static void ath9k_rx_skb_postprocess(struct ath_common *common,
987 struct sk_buff *skb,
988 struct ath_rx_status *rx_stats,
989 struct ieee80211_rx_status *rxs,
990 bool decrypt_error)
991{
992 struct ath_hw *ah = common->ah;
993 struct ieee80211_hdr *hdr;
994 int hdrlen, padpos, padsize;
995 u8 keyix;
996 __le16 fc;
997
998 /* see if any padding is done by the hw and remove it */
999 hdr = (struct ieee80211_hdr *) skb->data;
1000 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1001 fc = hdr->frame_control;
c60c9929 1002 padpos = ieee80211_hdrlen(fc);
d435700f
S
1003
1004 /* The MAC header is padded to have 32-bit boundary if the
1005 * packet payload is non-zero. The general calculation for
1006 * padsize would take into account odd header lengths:
1007 * padsize = (4 - padpos % 4) % 4; However, since only
1008 * even-length headers are used, padding can only be 0 or 2
1009 * bytes and we can optimize this a bit. In addition, we must
1010 * not try to remove padding from short control frames that do
1011 * not have payload. */
1012 padsize = padpos & 3;
1013 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1014 memmove(skb->data + padsize, skb->data, padpos);
1015 skb_pull(skb, padsize);
1016 }
1017
1018 keyix = rx_stats->rs_keyix;
1019
1020 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1021 ieee80211_has_protected(fc)) {
1022 rxs->flag |= RX_FLAG_DECRYPTED;
1023 } else if (ieee80211_has_protected(fc)
1024 && !decrypt_error && skb->len >= hdrlen + 4) {
1025 keyix = skb->data[hdrlen + 3] >> 6;
1026
1027 if (test_bit(keyix, common->keymap))
1028 rxs->flag |= RX_FLAG_DECRYPTED;
1029 }
1030 if (ah->sw_mgmt_crypto &&
1031 (rxs->flag & RX_FLAG_DECRYPTED) &&
1032 ieee80211_is_mgmt(fc))
1033 /* Use software decrypt for management frames. */
1034 rxs->flag &= ~RX_FLAG_DECRYPTED;
1035}
b5c80475 1036
ab2e2fc8 1037#ifdef CONFIG_ATH9K_DEBUGFS
e93d083f
SW
1038static s8 fix_rssi_inv_only(u8 rssi_val)
1039{
1040 if (rssi_val == 128)
1041 rssi_val = 0;
1042 return (s8) rssi_val;
1043}
ab2e2fc8 1044#endif
e93d083f 1045
9b99e665
SW
1046/* returns 1 if this was a spectral frame, even if not handled. */
1047static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
1048 struct ath_rx_status *rs, u64 tsf)
e93d083f 1049{
bd2ffe14 1050#ifdef CONFIG_ATH9K_DEBUGFS
e93d083f
SW
1051 struct ath_hw *ah = sc->sc_ah;
1052 u8 bins[SPECTRAL_HT20_NUM_BINS];
1053 u8 *vdata = (u8 *)hdr;
1054 struct fft_sample_ht20 fft_sample;
1055 struct ath_radar_info *radar_info;
1056 struct ath_ht20_mag_info *mag_info;
1057 int len = rs->rs_datalen;
4ab0b0aa 1058 int dc_pos;
12824374 1059 u16 length, max_magnitude;
e93d083f
SW
1060
1061 /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
1062 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
1063 * yet, but this is supposed to be possible as well.
1064 */
1065 if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
1066 rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
1067 rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
9b99e665
SW
1068 return 0;
1069
1070 /* check if spectral scan bit is set. This does not have to be checked
1071 * if received through a SPECTRAL phy error, but shouldn't hurt.
1072 */
1073 radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
1074 if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
1075 return 0;
e93d083f
SW
1076
1077 /* Variation in the data length is possible and will be fixed later.
1078 * Note that we only support HT20 for now.
1079 *
1080 * TODO: add HT20_40 support as well.
1081 */
1082 if ((len > SPECTRAL_HT20_TOTAL_DATA_LEN + 2) ||
1083 (len < SPECTRAL_HT20_TOTAL_DATA_LEN - 1))
9b99e665 1084 return 1;
e93d083f
SW
1085
1086 fft_sample.tlv.type = ATH_FFT_SAMPLE_HT20;
12824374
SE
1087 length = sizeof(fft_sample) - sizeof(fft_sample.tlv);
1088 fft_sample.tlv.length = __cpu_to_be16(length);
e93d083f 1089
4ab0b0aa 1090 fft_sample.freq = __cpu_to_be16(ah->curchan->chan->center_freq);
e93d083f
SW
1091 fft_sample.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
1092 fft_sample.noise = ah->noise;
1093
1094 switch (len - SPECTRAL_HT20_TOTAL_DATA_LEN) {
1095 case 0:
1096 /* length correct, nothing to do. */
1097 memcpy(bins, vdata, SPECTRAL_HT20_NUM_BINS);
1098 break;
1099 case -1:
1100 /* first byte missing, duplicate it. */
1101 memcpy(&bins[1], vdata, SPECTRAL_HT20_NUM_BINS - 1);
1102 bins[0] = vdata[0];
1103 break;
1104 case 2:
1105 /* MAC added 2 extra bytes at bin 30 and 32, remove them. */
1106 memcpy(bins, vdata, 30);
1107 bins[30] = vdata[31];
1108 memcpy(&bins[31], &vdata[33], SPECTRAL_HT20_NUM_BINS - 31);
1109 break;
1110 case 1:
1111 /* MAC added 2 extra bytes AND first byte is missing. */
1112 bins[0] = vdata[0];
1113 memcpy(&bins[0], vdata, 30);
1114 bins[31] = vdata[31];
1115 memcpy(&bins[32], &vdata[33], SPECTRAL_HT20_NUM_BINS - 32);
1116 break;
1117 default:
9b99e665 1118 return 1;
e93d083f
SW
1119 }
1120
1121 /* DC value (value in the middle) is the blind spot of the spectral
1122 * sample and invalid, interpolate it.
1123 */
1124 dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
1125 bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
1126
1127 /* mag data is at the end of the frame, in front of radar_info */
1128 mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
1129
4ab0b0aa
SE
1130 /* copy raw bins without scaling them */
1131 memcpy(fft_sample.data, bins, SPECTRAL_HT20_NUM_BINS);
1132 fft_sample.max_exp = mag_info->max_exp & 0xf;
e93d083f 1133
12824374
SE
1134 max_magnitude = spectral_max_magnitude(mag_info->all_bins);
1135 fft_sample.max_magnitude = __cpu_to_be16(max_magnitude);
e93d083f
SW
1136 fft_sample.max_index = spectral_max_index(mag_info->all_bins);
1137 fft_sample.bitmap_weight = spectral_bitmap_weight(mag_info->all_bins);
4ab0b0aa 1138 fft_sample.tsf = __cpu_to_be64(tsf);
e93d083f
SW
1139
1140 ath_debug_send_fft_sample(sc, &fft_sample.tlv);
9b99e665
SW
1141 return 1;
1142#else
1143 return 0;
e93d083f
SW
1144#endif
1145}
1146
21fbbca3
CL
1147static void ath9k_apply_ampdu_details(struct ath_softc *sc,
1148 struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
1149{
1150 if (rs->rs_isaggr) {
1151 rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
1152
1153 rxs->ampdu_reference = sc->rx.ampdu_ref;
1154
1155 if (!rs->rs_moreaggr) {
1156 rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
1157 sc->rx.ampdu_ref++;
1158 }
1159
1160 if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
1161 rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
1162 }
1163}
1164
b5c80475
FF
1165int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1166{
1167 struct ath_buf *bf;
0d95521e 1168 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
5ca42627 1169 struct ieee80211_rx_status *rxs;
cbe61d8a 1170 struct ath_hw *ah = sc->sc_ah;
27c51f1a 1171 struct ath_common *common = ath9k_hw_common(ah);
7545daf4 1172 struct ieee80211_hw *hw = sc->hw;
be0418ad 1173 struct ieee80211_hdr *hdr;
c9b14170 1174 int retval;
29bffa96 1175 struct ath_rx_status rs;
b5c80475
FF
1176 enum ath9k_rx_qtype qtype;
1177 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1178 int dma_type;
5c6dd921 1179 u8 rx_status_len = ah->caps.rx_status_len;
a6d2055b
FF
1180 u64 tsf = 0;
1181 u32 tsf_lower = 0;
8ab2cd09 1182 unsigned long flags;
2e1cd495 1183 dma_addr_t new_buf_addr;
be0418ad 1184
b5c80475 1185 if (edma)
b5c80475 1186 dma_type = DMA_BIDIRECTIONAL;
56824223
ML
1187 else
1188 dma_type = DMA_FROM_DEVICE;
b5c80475
FF
1189
1190 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
f078f209 1191
a6d2055b
FF
1192 tsf = ath9k_hw_gettsf64(ah);
1193 tsf_lower = tsf & 0xffffffff;
1194
f078f209 1195 do {
e1352fde 1196 bool decrypt_error = false;
f078f209 1197
29bffa96 1198 memset(&rs, 0, sizeof(rs));
b5c80475
FF
1199 if (edma)
1200 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1201 else
1202 bf = ath_get_next_rx_buf(sc, &rs);
f078f209 1203
b5c80475
FF
1204 if (!bf)
1205 break;
f078f209 1206
f078f209 1207 skb = bf->bf_mpdu;
be0418ad 1208 if (!skb)
f078f209 1209 continue;
f078f209 1210
0d95521e
FF
1211 /*
1212 * Take frame header from the first fragment and RX status from
1213 * the last one.
1214 */
1215 if (sc->rx.frag)
1216 hdr_skb = sc->rx.frag;
1217 else
1218 hdr_skb = skb;
1219
1220 hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1221 rxs = IEEE80211_SKB_RXCB(hdr_skb);
15072189
BG
1222 if (ieee80211_is_beacon(hdr->frame_control)) {
1223 RX_STAT_INC(rx_beacons);
1224 if (!is_zero_ether_addr(common->curbssid) &&
2e42e474 1225 ether_addr_equal(hdr->addr3, common->curbssid))
15072189
BG
1226 rs.is_mybeacon = true;
1227 else
1228 rs.is_mybeacon = false;
1229 }
cf3af748
RM
1230 else
1231 rs.is_mybeacon = false;
5ca42627 1232
be41b052
MSS
1233 if (ieee80211_is_data_present(hdr->frame_control) &&
1234 !ieee80211_is_qos_nullfunc(hdr->frame_control))
1235 sc->rx.num_pkts++;
1236
29bffa96 1237 ath_debug_stat_rx(sc, &rs);
1395d3f0 1238
ffb1c56a
AN
1239 memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1240
a6d2055b
FF
1241 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1242 if (rs.rs_tstamp > tsf_lower &&
1243 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1244 rxs->mactime -= 0x100000000ULL;
1245
1246 if (rs.rs_tstamp < tsf_lower &&
1247 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1248 rxs->mactime += 0x100000000ULL;
1249
73e4937d
ZK
1250 if (rs.rs_phyerr == ATH9K_PHYERR_RADAR)
1251 ath9k_dfs_process_phyerr(sc, hdr, &rs, rxs->mactime);
1252
9b99e665
SW
1253 if (rs.rs_status & ATH9K_RXERR_PHY) {
1254 if (ath_process_fft(sc, hdr, &rs, rxs->mactime)) {
1255 RX_STAT_INC(rx_spectral);
1256 goto requeue_drop_frag;
1257 }
1258 }
e93d083f 1259
723e7113
FF
1260 retval = ath9k_rx_skb_preprocess(sc, hdr, &rs, rxs,
1261 &decrypt_error);
83c76570
ZK
1262 if (retval)
1263 goto requeue_drop_frag;
1264
01e18918
RM
1265 if (rs.is_mybeacon) {
1266 sc->hw_busy_count = 0;
1267 ath_start_rx_poll(sc, 3);
1268 }
cb71d9ba
LR
1269 /* Ensure we always have an skb to requeue once we are done
1270 * processing the current buffer's skb */
cc861f74 1271 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
cb71d9ba
LR
1272
1273 /* If there is no memory we ignore the current RX'd frame,
1274 * tell hardware it can give us a new frame using the old
b77f483f 1275 * skb and put it at the tail of the sc->rx.rxbuf list for
cb71d9ba 1276 * processing. */
15072189
BG
1277 if (!requeue_skb) {
1278 RX_STAT_INC(rx_oom_err);
0d95521e 1279 goto requeue_drop_frag;
15072189 1280 }
f078f209 1281
2e1cd495
FF
1282 /* We will now give hardware our shiny new allocated skb */
1283 new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1284 common->rx_bufsize, dma_type);
1285 if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
1286 dev_kfree_skb_any(requeue_skb);
1287 goto requeue_drop_frag;
1288 }
1289
1290 bf->bf_mpdu = requeue_skb;
1291 bf->bf_buf_addr = new_buf_addr;
1292
9bf9fca8 1293 /* Unmap the frame */
7da3c55c 1294 dma_unmap_single(sc->dev, bf->bf_buf_addr,
2e1cd495 1295 common->rx_bufsize, dma_type);
f078f209 1296
b5c80475
FF
1297 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1298 if (ah->caps.rx_status_len)
1299 skb_pull(skb, ah->caps.rx_status_len);
be0418ad 1300
0d95521e
FF
1301 if (!rs.rs_more)
1302 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1303 rxs, decrypt_error);
be0418ad 1304
0d95521e 1305 if (rs.rs_more) {
15072189 1306 RX_STAT_INC(rx_frags);
0d95521e
FF
1307 /*
1308 * rs_more indicates chained descriptors which can be
1309 * used to link buffers together for a sort of
1310 * scatter-gather operation.
1311 */
1312 if (sc->rx.frag) {
1313 /* too many fragments - cannot handle frame */
1314 dev_kfree_skb_any(sc->rx.frag);
1315 dev_kfree_skb_any(skb);
15072189 1316 RX_STAT_INC(rx_too_many_frags_err);
0d95521e
FF
1317 skb = NULL;
1318 }
1319 sc->rx.frag = skb;
1320 goto requeue;
1321 }
3747c3ee
FF
1322 if (rs.rs_status & ATH9K_RXERR_CORRUPT_DESC)
1323 goto requeue_drop_frag;
0d95521e
FF
1324
1325 if (sc->rx.frag) {
1326 int space = skb->len - skb_tailroom(hdr_skb);
1327
0d95521e
FF
1328 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1329 dev_kfree_skb(skb);
15072189 1330 RX_STAT_INC(rx_oom_err);
0d95521e
FF
1331 goto requeue_drop_frag;
1332 }
1333
b5447ff9
ED
1334 sc->rx.frag = NULL;
1335
0d95521e
FF
1336 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1337 skb->len);
1338 dev_kfree_skb_any(skb);
1339 skb = hdr_skb;
1340 }
1341
eb840a80
MSS
1342
1343 if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
1344
1345 /*
1346 * change the default rx antenna if rx diversity
1347 * chooses the other antenna 3 times in a row.
1348 */
1349 if (sc->rx.defant != rs.rs_antenna) {
1350 if (++sc->rx.rxotherant >= 3)
1351 ath_setdefantenna(sc, rs.rs_antenna);
1352 } else {
1353 sc->rx.rxotherant = 0;
1354 }
1355
f078f209 1356 }
3cbb5dd7 1357
66760eac
FF
1358 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1359 skb_trim(skb, skb->len - 8);
1360
8ab2cd09 1361 spin_lock_irqsave(&sc->sc_pm_lock, flags);
aaef24b4 1362 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
f73c604c
RM
1363 PS_WAIT_FOR_CAB |
1364 PS_WAIT_FOR_PSPOLL_DATA)) ||
1365 ath9k_check_auto_sleep(sc))
1366 ath_rx_ps(sc, skb, rs.is_mybeacon);
8ab2cd09 1367 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
cc65965c 1368
43c35284 1369 if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
102885a5
VT
1370 ath_ant_comb_scan(sc, &rs);
1371
21fbbca3
CL
1372 ath9k_apply_ampdu_details(sc, &rs, rxs);
1373
7545daf4 1374 ieee80211_rx(hw, skb);
cc65965c 1375
0d95521e
FF
1376requeue_drop_frag:
1377 if (sc->rx.frag) {
1378 dev_kfree_skb_any(sc->rx.frag);
1379 sc->rx.frag = NULL;
1380 }
cb71d9ba 1381requeue:
a3dc48e8
FF
1382 list_add_tail(&bf->list, &sc->rx.rxbuf);
1383 if (flush)
1384 continue;
1385
b5c80475 1386 if (edma) {
b5c80475
FF
1387 ath_rx_edma_buf_link(sc, qtype);
1388 } else {
b5c80475 1389 ath_rx_buf_link(sc, bf);
a3dc48e8 1390 ath9k_hw_rxena(ah);
b5c80475 1391 }
be0418ad
S
1392 } while (1);
1393
29ab0b36
RM
1394 if (!(ah->imask & ATH9K_INT_RXEOL)) {
1395 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
72d874c6 1396 ath9k_hw_set_interrupts(ah);
29ab0b36
RM
1397 }
1398
f078f209 1399 return 0;
f078f209 1400}