Linux 4.4.145
[linux/fpc-iii.git] / drivers / net / wireless / ath / ath9k / recv.c
blob994daf6c629798332ffe362efa5dd3481a93cdf1
1 /*
2 * Copyright (c) 2008-2011 Atheros Communications Inc.
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.
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.
17 #include <linux/dma-mapping.h>
18 #include "ath9k.h"
19 #include "ar9003_mac.h"
21 #define SKB_CB_ATHBUF(__skb) (*((struct ath_rxbuf **)__skb->cb))
23 static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
25 return sc->ps_enabled &&
26 (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
30 * Setup and link descriptors.
32 * 11N: we can no longer afford to self link the last descriptor.
33 * MAC acknowledges BA status as long as it copies frames to host
34 * buffer (or rx fifo). This can incorrectly acknowledge packets
35 * to a sender if last desc is self-linked.
37 static void ath_rx_buf_link(struct ath_softc *sc, struct ath_rxbuf *bf,
38 bool flush)
40 struct ath_hw *ah = sc->sc_ah;
41 struct ath_common *common = ath9k_hw_common(ah);
42 struct ath_desc *ds;
43 struct sk_buff *skb;
45 ds = bf->bf_desc;
46 ds->ds_link = 0; /* link to null */
47 ds->ds_data = bf->bf_buf_addr;
49 /* virtual addr of the beginning of the buffer. */
50 skb = bf->bf_mpdu;
51 BUG_ON(skb == NULL);
52 ds->ds_vdata = skb->data;
55 * setup rx descriptors. The rx_bufsize here tells the hardware
56 * how much data it can DMA to us and that we are prepared
57 * to process
59 ath9k_hw_setuprxdesc(ah, ds,
60 common->rx_bufsize,
61 0);
63 if (sc->rx.rxlink)
64 *sc->rx.rxlink = bf->bf_daddr;
65 else if (!flush)
66 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
68 sc->rx.rxlink = &ds->ds_link;
71 static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_rxbuf *bf,
72 bool flush)
74 if (sc->rx.buf_hold)
75 ath_rx_buf_link(sc, sc->rx.buf_hold, flush);
77 sc->rx.buf_hold = bf;
80 static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
82 /* XXX block beacon interrupts */
83 ath9k_hw_setantenna(sc->sc_ah, antenna);
84 sc->rx.defant = antenna;
85 sc->rx.rxotherant = 0;
88 static void ath_opmode_init(struct ath_softc *sc)
90 struct ath_hw *ah = sc->sc_ah;
91 struct ath_common *common = ath9k_hw_common(ah);
93 u32 rfilt, mfilt[2];
95 /* configure rx filter */
96 rfilt = ath_calcrxfilter(sc);
97 ath9k_hw_setrxfilter(ah, rfilt);
99 /* configure bssid mask */
100 ath_hw_setbssidmask(common);
102 /* configure operational mode */
103 ath9k_hw_setopmode(ah);
105 /* calculate and install multicast filter */
106 mfilt[0] = mfilt[1] = ~0;
107 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
110 static bool ath_rx_edma_buf_link(struct ath_softc *sc,
111 enum ath9k_rx_qtype qtype)
113 struct ath_hw *ah = sc->sc_ah;
114 struct ath_rx_edma *rx_edma;
115 struct sk_buff *skb;
116 struct ath_rxbuf *bf;
118 rx_edma = &sc->rx.rx_edma[qtype];
119 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
120 return false;
122 bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
123 list_del_init(&bf->list);
125 skb = bf->bf_mpdu;
127 memset(skb->data, 0, ah->caps.rx_status_len);
128 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
129 ah->caps.rx_status_len, DMA_TO_DEVICE);
131 SKB_CB_ATHBUF(skb) = bf;
132 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
133 __skb_queue_tail(&rx_edma->rx_fifo, skb);
135 return true;
138 static void ath_rx_addbuffer_edma(struct ath_softc *sc,
139 enum ath9k_rx_qtype qtype)
141 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
142 struct ath_rxbuf *bf, *tbf;
144 if (list_empty(&sc->rx.rxbuf)) {
145 ath_dbg(common, QUEUE, "No free rx buf available\n");
146 return;
149 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
150 if (!ath_rx_edma_buf_link(sc, qtype))
151 break;
155 static void ath_rx_remove_buffer(struct ath_softc *sc,
156 enum ath9k_rx_qtype qtype)
158 struct ath_rxbuf *bf;
159 struct ath_rx_edma *rx_edma;
160 struct sk_buff *skb;
162 rx_edma = &sc->rx.rx_edma[qtype];
164 while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
165 bf = SKB_CB_ATHBUF(skb);
166 BUG_ON(!bf);
167 list_add_tail(&bf->list, &sc->rx.rxbuf);
171 static void ath_rx_edma_cleanup(struct ath_softc *sc)
173 struct ath_hw *ah = sc->sc_ah;
174 struct ath_common *common = ath9k_hw_common(ah);
175 struct ath_rxbuf *bf;
177 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
178 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
180 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
181 if (bf->bf_mpdu) {
182 dma_unmap_single(sc->dev, bf->bf_buf_addr,
183 common->rx_bufsize,
184 DMA_BIDIRECTIONAL);
185 dev_kfree_skb_any(bf->bf_mpdu);
186 bf->bf_buf_addr = 0;
187 bf->bf_mpdu = NULL;
192 static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
194 __skb_queue_head_init(&rx_edma->rx_fifo);
195 rx_edma->rx_fifo_hwsize = size;
198 static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
200 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
201 struct ath_hw *ah = sc->sc_ah;
202 struct sk_buff *skb;
203 struct ath_rxbuf *bf;
204 int error = 0, i;
205 u32 size;
207 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
208 ah->caps.rx_status_len);
210 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
211 ah->caps.rx_lp_qdepth);
212 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
213 ah->caps.rx_hp_qdepth);
215 size = sizeof(struct ath_rxbuf) * nbufs;
216 bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
217 if (!bf)
218 return -ENOMEM;
220 INIT_LIST_HEAD(&sc->rx.rxbuf);
222 for (i = 0; i < nbufs; i++, bf++) {
223 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
224 if (!skb) {
225 error = -ENOMEM;
226 goto rx_init_fail;
229 memset(skb->data, 0, common->rx_bufsize);
230 bf->bf_mpdu = skb;
232 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
233 common->rx_bufsize,
234 DMA_BIDIRECTIONAL);
235 if (unlikely(dma_mapping_error(sc->dev,
236 bf->bf_buf_addr))) {
237 dev_kfree_skb_any(skb);
238 bf->bf_mpdu = NULL;
239 bf->bf_buf_addr = 0;
240 ath_err(common,
241 "dma_mapping_error() on RX init\n");
242 error = -ENOMEM;
243 goto rx_init_fail;
246 list_add_tail(&bf->list, &sc->rx.rxbuf);
249 return 0;
251 rx_init_fail:
252 ath_rx_edma_cleanup(sc);
253 return error;
256 static void ath_edma_start_recv(struct ath_softc *sc)
258 ath9k_hw_rxena(sc->sc_ah);
259 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
260 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
261 ath_opmode_init(sc);
262 ath9k_hw_startpcureceive(sc->sc_ah, sc->cur_chan->offchannel);
265 static void ath_edma_stop_recv(struct ath_softc *sc)
267 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
268 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
271 int ath_rx_init(struct ath_softc *sc, int nbufs)
273 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
274 struct sk_buff *skb;
275 struct ath_rxbuf *bf;
276 int error = 0;
278 spin_lock_init(&sc->sc_pcu_lock);
280 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
281 sc->sc_ah->caps.rx_status_len;
283 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
284 return ath_rx_edma_init(sc, nbufs);
286 ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
287 common->cachelsz, common->rx_bufsize);
289 /* Initialize rx descriptors */
291 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
292 "rx", nbufs, 1, 0);
293 if (error != 0) {
294 ath_err(common,
295 "failed to allocate rx descriptors: %d\n",
296 error);
297 goto err;
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;
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;
316 bf->bf_buf_addr = 0;
317 ath_err(common,
318 "dma_mapping_error() on RX init\n");
319 error = -ENOMEM;
320 goto err;
323 sc->rx.rxlink = NULL;
324 err:
325 if (error)
326 ath_rx_cleanup(sc);
328 return error;
331 void ath_rx_cleanup(struct ath_softc *sc)
333 struct ath_hw *ah = sc->sc_ah;
334 struct ath_common *common = ath9k_hw_common(ah);
335 struct sk_buff *skb;
336 struct ath_rxbuf *bf;
338 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
339 ath_rx_edma_cleanup(sc);
340 return;
343 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
344 skb = bf->bf_mpdu;
345 if (skb) {
346 dma_unmap_single(sc->dev, bf->bf_buf_addr,
347 common->rx_bufsize,
348 DMA_FROM_DEVICE);
349 dev_kfree_skb(skb);
350 bf->bf_buf_addr = 0;
351 bf->bf_mpdu = NULL;
357 * Calculate the receive filter according to the
358 * operating mode and state:
360 * o always accept unicast, broadcast, and multicast traffic
361 * o maintain current state of phy error reception (the hal
362 * may enable phy error frames for noise immunity work)
363 * o probe request frames are accepted only when operating in
364 * hostap, adhoc, or monitor modes
365 * o enable promiscuous mode according to the interface state
366 * o accept beacons:
367 * - when operating in adhoc mode so the 802.11 layer creates
368 * node table entries for peers,
369 * - when operating in station mode for collecting rssi data when
370 * the station is otherwise quiet, or
371 * - when operating as a repeater so we see repeater-sta beacons
372 * - when scanning
375 u32 ath_calcrxfilter(struct ath_softc *sc)
377 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
378 u32 rfilt;
380 if (config_enabled(CONFIG_ATH9K_TX99))
381 return 0;
383 rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
384 | ATH9K_RX_FILTER_MCAST;
386 /* if operating on a DFS channel, enable radar pulse detection */
387 if (sc->hw->conf.radar_enabled)
388 rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
390 spin_lock_bh(&sc->chan_lock);
392 if (sc->cur_chan->rxfilter & FIF_PROBE_REQ)
393 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
395 if (sc->sc_ah->is_monitoring)
396 rfilt |= ATH9K_RX_FILTER_PROM;
398 if ((sc->cur_chan->rxfilter & FIF_CONTROL) ||
399 sc->sc_ah->dynack.enabled)
400 rfilt |= ATH9K_RX_FILTER_CONTROL;
402 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
403 (sc->cur_chan->nvifs <= 1) &&
404 !(sc->cur_chan->rxfilter & FIF_BCN_PRBRESP_PROMISC))
405 rfilt |= ATH9K_RX_FILTER_MYBEACON;
406 else if (sc->sc_ah->opmode != NL80211_IFTYPE_OCB)
407 rfilt |= ATH9K_RX_FILTER_BEACON;
409 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
410 (sc->cur_chan->rxfilter & FIF_PSPOLL))
411 rfilt |= ATH9K_RX_FILTER_PSPOLL;
413 if (sc->cur_chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
414 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
416 if (sc->cur_chan->nvifs > 1 || (sc->cur_chan->rxfilter & FIF_OTHER_BSS)) {
417 /* This is needed for older chips */
418 if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
419 rfilt |= ATH9K_RX_FILTER_PROM;
420 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
423 if (AR_SREV_9550(sc->sc_ah) || AR_SREV_9531(sc->sc_ah) ||
424 AR_SREV_9561(sc->sc_ah))
425 rfilt |= ATH9K_RX_FILTER_4ADDRESS;
427 if (ath9k_is_chanctx_enabled() &&
428 test_bit(ATH_OP_SCANNING, &common->op_flags))
429 rfilt |= ATH9K_RX_FILTER_BEACON;
431 spin_unlock_bh(&sc->chan_lock);
433 return rfilt;
437 void ath_startrecv(struct ath_softc *sc)
439 struct ath_hw *ah = sc->sc_ah;
440 struct ath_rxbuf *bf, *tbf;
442 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
443 ath_edma_start_recv(sc);
444 return;
447 if (list_empty(&sc->rx.rxbuf))
448 goto start_recv;
450 sc->rx.buf_hold = NULL;
451 sc->rx.rxlink = NULL;
452 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
453 ath_rx_buf_link(sc, bf, false);
456 /* We could have deleted elements so the list may be empty now */
457 if (list_empty(&sc->rx.rxbuf))
458 goto start_recv;
460 bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
461 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
462 ath9k_hw_rxena(ah);
464 start_recv:
465 ath_opmode_init(sc);
466 ath9k_hw_startpcureceive(ah, sc->cur_chan->offchannel);
469 static void ath_flushrecv(struct ath_softc *sc)
471 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
472 ath_rx_tasklet(sc, 1, true);
473 ath_rx_tasklet(sc, 1, false);
476 bool ath_stoprecv(struct ath_softc *sc)
478 struct ath_hw *ah = sc->sc_ah;
479 bool stopped, reset = false;
481 ath9k_hw_abortpcurecv(ah);
482 ath9k_hw_setrxfilter(ah, 0);
483 stopped = ath9k_hw_stopdmarecv(ah, &reset);
485 ath_flushrecv(sc);
487 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
488 ath_edma_stop_recv(sc);
489 else
490 sc->rx.rxlink = NULL;
492 if (!(ah->ah_flags & AH_UNPLUGGED) &&
493 unlikely(!stopped)) {
494 ath_dbg(ath9k_hw_common(sc->sc_ah), RESET,
495 "Failed to stop Rx DMA\n");
496 RESET_STAT_INC(sc, RESET_RX_DMA_ERROR);
498 return stopped && !reset;
501 static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
503 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
504 struct ieee80211_mgmt *mgmt;
505 u8 *pos, *end, id, elen;
506 struct ieee80211_tim_ie *tim;
508 mgmt = (struct ieee80211_mgmt *)skb->data;
509 pos = mgmt->u.beacon.variable;
510 end = skb->data + skb->len;
512 while (pos + 2 < end) {
513 id = *pos++;
514 elen = *pos++;
515 if (pos + elen > end)
516 break;
518 if (id == WLAN_EID_TIM) {
519 if (elen < sizeof(*tim))
520 break;
521 tim = (struct ieee80211_tim_ie *) pos;
522 if (tim->dtim_count != 0)
523 break;
524 return tim->bitmap_ctrl & 0x01;
527 pos += elen;
530 return false;
533 static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
535 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
536 bool skip_beacon = false;
538 if (skb->len < 24 + 8 + 2 + 2)
539 return;
541 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
543 if (sc->ps_flags & PS_BEACON_SYNC) {
544 sc->ps_flags &= ~PS_BEACON_SYNC;
545 ath_dbg(common, PS,
546 "Reconfigure beacon timers based on synchronized timestamp\n");
548 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
549 if (ath9k_is_chanctx_enabled()) {
550 if (sc->cur_chan == &sc->offchannel.chan)
551 skip_beacon = true;
553 #endif
555 if (!skip_beacon &&
556 !(WARN_ON_ONCE(sc->cur_chan->beacon.beacon_interval == 0)))
557 ath9k_set_beacon(sc);
559 ath9k_p2p_beacon_sync(sc);
562 if (ath_beacon_dtim_pending_cab(skb)) {
564 * Remain awake waiting for buffered broadcast/multicast
565 * frames. If the last broadcast/multicast frame is not
566 * received properly, the next beacon frame will work as
567 * a backup trigger for returning into NETWORK SLEEP state,
568 * so we are waiting for it as well.
570 ath_dbg(common, PS,
571 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
572 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
573 return;
576 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
578 * This can happen if a broadcast frame is dropped or the AP
579 * fails to send a frame indicating that all CAB frames have
580 * been delivered.
582 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
583 ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
587 static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
589 struct ieee80211_hdr *hdr;
590 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
592 hdr = (struct ieee80211_hdr *)skb->data;
594 /* Process Beacon and CAB receive in PS state */
595 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
596 && mybeacon) {
597 ath_rx_ps_beacon(sc, skb);
598 } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
599 (ieee80211_is_data(hdr->frame_control) ||
600 ieee80211_is_action(hdr->frame_control)) &&
601 is_multicast_ether_addr(hdr->addr1) &&
602 !ieee80211_has_moredata(hdr->frame_control)) {
604 * No more broadcast/multicast frames to be received at this
605 * point.
607 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
608 ath_dbg(common, PS,
609 "All PS CAB frames received, back to sleep\n");
610 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
611 !is_multicast_ether_addr(hdr->addr1) &&
612 !ieee80211_has_morefrags(hdr->frame_control)) {
613 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
614 ath_dbg(common, PS,
615 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
616 sc->ps_flags & (PS_WAIT_FOR_BEACON |
617 PS_WAIT_FOR_CAB |
618 PS_WAIT_FOR_PSPOLL_DATA |
619 PS_WAIT_FOR_TX_ACK));
623 static bool ath_edma_get_buffers(struct ath_softc *sc,
624 enum ath9k_rx_qtype qtype,
625 struct ath_rx_status *rs,
626 struct ath_rxbuf **dest)
628 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
629 struct ath_hw *ah = sc->sc_ah;
630 struct ath_common *common = ath9k_hw_common(ah);
631 struct sk_buff *skb;
632 struct ath_rxbuf *bf;
633 int ret;
635 skb = skb_peek(&rx_edma->rx_fifo);
636 if (!skb)
637 return false;
639 bf = SKB_CB_ATHBUF(skb);
640 BUG_ON(!bf);
642 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
643 common->rx_bufsize, DMA_FROM_DEVICE);
645 ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
646 if (ret == -EINPROGRESS) {
647 /*let device gain the buffer again*/
648 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
649 common->rx_bufsize, DMA_FROM_DEVICE);
650 return false;
653 __skb_unlink(skb, &rx_edma->rx_fifo);
654 if (ret == -EINVAL) {
655 /* corrupt descriptor, skip this one and the following one */
656 list_add_tail(&bf->list, &sc->rx.rxbuf);
657 ath_rx_edma_buf_link(sc, qtype);
659 skb = skb_peek(&rx_edma->rx_fifo);
660 if (skb) {
661 bf = SKB_CB_ATHBUF(skb);
662 BUG_ON(!bf);
664 __skb_unlink(skb, &rx_edma->rx_fifo);
665 list_add_tail(&bf->list, &sc->rx.rxbuf);
666 ath_rx_edma_buf_link(sc, qtype);
669 bf = NULL;
672 *dest = bf;
673 return true;
676 static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
677 struct ath_rx_status *rs,
678 enum ath9k_rx_qtype qtype)
680 struct ath_rxbuf *bf = NULL;
682 while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
683 if (!bf)
684 continue;
686 return bf;
688 return NULL;
691 static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc,
692 struct ath_rx_status *rs)
694 struct ath_hw *ah = sc->sc_ah;
695 struct ath_common *common = ath9k_hw_common(ah);
696 struct ath_desc *ds;
697 struct ath_rxbuf *bf;
698 int ret;
700 if (list_empty(&sc->rx.rxbuf)) {
701 sc->rx.rxlink = NULL;
702 return NULL;
705 bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
706 if (bf == sc->rx.buf_hold)
707 return NULL;
709 ds = bf->bf_desc;
712 * Must provide the virtual address of the current
713 * descriptor, the physical address, and the virtual
714 * address of the next descriptor in the h/w chain.
715 * This allows the HAL to look ahead to see if the
716 * hardware is done with a descriptor by checking the
717 * done bit in the following descriptor and the address
718 * of the current descriptor the DMA engine is working
719 * on. All this is necessary because of our use of
720 * a self-linked list to avoid rx overruns.
722 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
723 if (ret == -EINPROGRESS) {
724 struct ath_rx_status trs;
725 struct ath_rxbuf *tbf;
726 struct ath_desc *tds;
728 memset(&trs, 0, sizeof(trs));
729 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
730 sc->rx.rxlink = NULL;
731 return NULL;
734 tbf = list_entry(bf->list.next, struct ath_rxbuf, list);
737 * On some hardware the descriptor status words could
738 * get corrupted, including the done bit. Because of
739 * this, check if the next descriptor's done bit is
740 * set or not.
742 * If the next descriptor's done bit is set, the current
743 * descriptor has been corrupted. Force s/w to discard
744 * this descriptor and continue...
747 tds = tbf->bf_desc;
748 ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
749 if (ret == -EINPROGRESS)
750 return NULL;
753 * Re-check previous descriptor, in case it has been filled
754 * in the mean time.
756 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
757 if (ret == -EINPROGRESS) {
759 * mark descriptor as zero-length and set the 'more'
760 * flag to ensure that both buffers get discarded
762 rs->rs_datalen = 0;
763 rs->rs_more = true;
767 list_del(&bf->list);
768 if (!bf->bf_mpdu)
769 return bf;
772 * Synchronize the DMA transfer with CPU before
773 * 1. accessing the frame
774 * 2. requeueing the same buffer to h/w
776 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
777 common->rx_bufsize,
778 DMA_FROM_DEVICE);
780 return bf;
783 static void ath9k_process_tsf(struct ath_rx_status *rs,
784 struct ieee80211_rx_status *rxs,
785 u64 tsf)
787 u32 tsf_lower = tsf & 0xffffffff;
789 rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
790 if (rs->rs_tstamp > tsf_lower &&
791 unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
792 rxs->mactime -= 0x100000000ULL;
794 if (rs->rs_tstamp < tsf_lower &&
795 unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
796 rxs->mactime += 0x100000000ULL;
800 * For Decrypt or Demic errors, we only mark packet status here and always push
801 * up the frame up to let mac80211 handle the actual error case, be it no
802 * decryption key or real decryption error. This let us keep statistics there.
804 static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
805 struct sk_buff *skb,
806 struct ath_rx_status *rx_stats,
807 struct ieee80211_rx_status *rx_status,
808 bool *decrypt_error, u64 tsf)
810 struct ieee80211_hw *hw = sc->hw;
811 struct ath_hw *ah = sc->sc_ah;
812 struct ath_common *common = ath9k_hw_common(ah);
813 struct ieee80211_hdr *hdr;
814 bool discard_current = sc->rx.discard_next;
817 * Discard corrupt descriptors which are marked in
818 * ath_get_next_rx_buf().
820 if (discard_current)
821 goto corrupt;
823 sc->rx.discard_next = false;
826 * Discard zero-length packets.
828 if (!rx_stats->rs_datalen) {
829 RX_STAT_INC(rx_len_err);
830 goto corrupt;
834 * rs_status follows rs_datalen so if rs_datalen is too large
835 * we can take a hint that hardware corrupted it, so ignore
836 * those frames.
838 if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
839 RX_STAT_INC(rx_len_err);
840 goto corrupt;
843 /* Only use status info from the last fragment */
844 if (rx_stats->rs_more)
845 return 0;
848 * Return immediately if the RX descriptor has been marked
849 * as corrupt based on the various error bits.
851 * This is different from the other corrupt descriptor
852 * condition handled above.
854 if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC)
855 goto corrupt;
857 hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
859 ath9k_process_tsf(rx_stats, rx_status, tsf);
860 ath_debug_stat_rx(sc, rx_stats);
863 * Process PHY errors and return so that the packet
864 * can be dropped.
866 if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
867 ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
868 if (ath_cmn_process_fft(&sc->spec_priv, hdr, rx_stats, rx_status->mactime))
869 RX_STAT_INC(rx_spectral);
871 return -EINVAL;
875 * everything but the rate is checked here, the rate check is done
876 * separately to avoid doing two lookups for a rate for each frame.
878 spin_lock_bh(&sc->chan_lock);
879 if (!ath9k_cmn_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error,
880 sc->cur_chan->rxfilter)) {
881 spin_unlock_bh(&sc->chan_lock);
882 return -EINVAL;
884 spin_unlock_bh(&sc->chan_lock);
886 if (ath_is_mybeacon(common, hdr)) {
887 RX_STAT_INC(rx_beacons);
888 rx_stats->is_mybeacon = true;
892 * This shouldn't happen, but have a safety check anyway.
894 if (WARN_ON(!ah->curchan))
895 return -EINVAL;
897 if (ath9k_cmn_process_rate(common, hw, rx_stats, rx_status)) {
899 * No valid hardware bitrate found -- we should not get here
900 * because hardware has already validated this frame as OK.
902 ath_dbg(common, ANY, "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
903 rx_stats->rs_rate);
904 RX_STAT_INC(rx_rate_err);
905 return -EINVAL;
908 if (ath9k_is_chanctx_enabled()) {
909 if (rx_stats->is_mybeacon)
910 ath_chanctx_beacon_recv_ev(sc,
911 ATH_CHANCTX_EVENT_BEACON_RECEIVED);
914 ath9k_cmn_process_rssi(common, hw, rx_stats, rx_status);
916 rx_status->band = ah->curchan->chan->band;
917 rx_status->freq = ah->curchan->chan->center_freq;
918 rx_status->antenna = rx_stats->rs_antenna;
919 rx_status->flag |= RX_FLAG_MACTIME_END;
921 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
922 if (ieee80211_is_data_present(hdr->frame_control) &&
923 !ieee80211_is_qos_nullfunc(hdr->frame_control))
924 sc->rx.num_pkts++;
925 #endif
927 return 0;
929 corrupt:
930 sc->rx.discard_next = rx_stats->rs_more;
931 return -EINVAL;
935 * Run the LNA combining algorithm only in these cases:
937 * Standalone WLAN cards with both LNA/Antenna diversity
938 * enabled in the EEPROM.
940 * WLAN+BT cards which are in the supported card list
941 * in ath_pci_id_table and the user has loaded the
942 * driver with "bt_ant_diversity" set to true.
944 static void ath9k_antenna_check(struct ath_softc *sc,
945 struct ath_rx_status *rs)
947 struct ath_hw *ah = sc->sc_ah;
948 struct ath9k_hw_capabilities *pCap = &ah->caps;
949 struct ath_common *common = ath9k_hw_common(ah);
951 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
952 return;
955 * Change the default rx antenna if rx diversity
956 * chooses the other antenna 3 times in a row.
958 if (sc->rx.defant != rs->rs_antenna) {
959 if (++sc->rx.rxotherant >= 3)
960 ath_setdefantenna(sc, rs->rs_antenna);
961 } else {
962 sc->rx.rxotherant = 0;
965 if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
966 if (common->bt_ant_diversity)
967 ath_ant_comb_scan(sc, rs);
968 } else {
969 ath_ant_comb_scan(sc, rs);
973 static void ath9k_apply_ampdu_details(struct ath_softc *sc,
974 struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
976 if (rs->rs_isaggr) {
977 rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
979 rxs->ampdu_reference = sc->rx.ampdu_ref;
981 if (!rs->rs_moreaggr) {
982 rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
983 sc->rx.ampdu_ref++;
986 if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
987 rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
991 int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
993 struct ath_rxbuf *bf;
994 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
995 struct ieee80211_rx_status *rxs;
996 struct ath_hw *ah = sc->sc_ah;
997 struct ath_common *common = ath9k_hw_common(ah);
998 struct ieee80211_hw *hw = sc->hw;
999 int retval;
1000 struct ath_rx_status rs;
1001 enum ath9k_rx_qtype qtype;
1002 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1003 int dma_type;
1004 u64 tsf = 0;
1005 unsigned long flags;
1006 dma_addr_t new_buf_addr;
1007 unsigned int budget = 512;
1008 struct ieee80211_hdr *hdr;
1010 if (edma)
1011 dma_type = DMA_BIDIRECTIONAL;
1012 else
1013 dma_type = DMA_FROM_DEVICE;
1015 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1017 tsf = ath9k_hw_gettsf64(ah);
1019 do {
1020 bool decrypt_error = false;
1022 memset(&rs, 0, sizeof(rs));
1023 if (edma)
1024 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1025 else
1026 bf = ath_get_next_rx_buf(sc, &rs);
1028 if (!bf)
1029 break;
1031 skb = bf->bf_mpdu;
1032 if (!skb)
1033 continue;
1036 * Take frame header from the first fragment and RX status from
1037 * the last one.
1039 if (sc->rx.frag)
1040 hdr_skb = sc->rx.frag;
1041 else
1042 hdr_skb = skb;
1044 rxs = IEEE80211_SKB_RXCB(hdr_skb);
1045 memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1047 retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
1048 &decrypt_error, tsf);
1049 if (retval)
1050 goto requeue_drop_frag;
1052 /* Ensure we always have an skb to requeue once we are done
1053 * processing the current buffer's skb */
1054 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1056 /* If there is no memory we ignore the current RX'd frame,
1057 * tell hardware it can give us a new frame using the old
1058 * skb and put it at the tail of the sc->rx.rxbuf list for
1059 * processing. */
1060 if (!requeue_skb) {
1061 RX_STAT_INC(rx_oom_err);
1062 goto requeue_drop_frag;
1065 /* We will now give hardware our shiny new allocated skb */
1066 new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1067 common->rx_bufsize, dma_type);
1068 if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
1069 dev_kfree_skb_any(requeue_skb);
1070 goto requeue_drop_frag;
1073 /* Unmap the frame */
1074 dma_unmap_single(sc->dev, bf->bf_buf_addr,
1075 common->rx_bufsize, dma_type);
1077 bf->bf_mpdu = requeue_skb;
1078 bf->bf_buf_addr = new_buf_addr;
1080 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1081 if (ah->caps.rx_status_len)
1082 skb_pull(skb, ah->caps.rx_status_len);
1084 if (!rs.rs_more)
1085 ath9k_cmn_rx_skb_postprocess(common, hdr_skb, &rs,
1086 rxs, decrypt_error);
1088 if (rs.rs_more) {
1089 RX_STAT_INC(rx_frags);
1091 * rs_more indicates chained descriptors which can be
1092 * used to link buffers together for a sort of
1093 * scatter-gather operation.
1095 if (sc->rx.frag) {
1096 /* too many fragments - cannot handle frame */
1097 dev_kfree_skb_any(sc->rx.frag);
1098 dev_kfree_skb_any(skb);
1099 RX_STAT_INC(rx_too_many_frags_err);
1100 skb = NULL;
1102 sc->rx.frag = skb;
1103 goto requeue;
1106 if (sc->rx.frag) {
1107 int space = skb->len - skb_tailroom(hdr_skb);
1109 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1110 dev_kfree_skb(skb);
1111 RX_STAT_INC(rx_oom_err);
1112 goto requeue_drop_frag;
1115 sc->rx.frag = NULL;
1117 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1118 skb->len);
1119 dev_kfree_skb_any(skb);
1120 skb = hdr_skb;
1123 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1124 skb_trim(skb, skb->len - 8);
1126 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1127 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
1128 PS_WAIT_FOR_CAB |
1129 PS_WAIT_FOR_PSPOLL_DATA)) ||
1130 ath9k_check_auto_sleep(sc))
1131 ath_rx_ps(sc, skb, rs.is_mybeacon);
1132 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1134 ath9k_antenna_check(sc, &rs);
1135 ath9k_apply_ampdu_details(sc, &rs, rxs);
1136 ath_debug_rate_stats(sc, &rs, skb);
1138 hdr = (struct ieee80211_hdr *)skb->data;
1139 if (ieee80211_is_ack(hdr->frame_control))
1140 ath_dynack_sample_ack_ts(sc->sc_ah, skb, rs.rs_tstamp);
1142 ieee80211_rx(hw, skb);
1144 requeue_drop_frag:
1145 if (sc->rx.frag) {
1146 dev_kfree_skb_any(sc->rx.frag);
1147 sc->rx.frag = NULL;
1149 requeue:
1150 list_add_tail(&bf->list, &sc->rx.rxbuf);
1152 if (!edma) {
1153 ath_rx_buf_relink(sc, bf, flush);
1154 if (!flush)
1155 ath9k_hw_rxena(ah);
1156 } else if (!flush) {
1157 ath_rx_edma_buf_link(sc, qtype);
1160 if (!budget--)
1161 break;
1162 } while (1);
1164 if (!(ah->imask & ATH9K_INT_RXEOL)) {
1165 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
1166 ath9k_hw_set_interrupts(ah);
1169 return 0;