2 * Copyright (c) 2008 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.
20 * Setup and link descriptors.
22 * 11N: we can no longer afford to self link the last descriptor.
23 * MAC acknowledges BA status as long as it copies frames to host
24 * buffer (or rx fifo). This can incorrectly acknowledge packets
25 * to a sender if last desc is self-linked.
27 static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
29 struct ath_hw *ah = sc->sc_ah;
36 ds->ds_link = 0; /* link to null */
37 ds->ds_data = bf->bf_buf_addr;
39 /* virtual addr of the beginning of the buffer. */
42 ds->ds_vdata = skb->data;
44 /* setup rx descriptors. The rx.bufsize here tells the harware
45 * how much data it can DMA to us and that we are prepared
47 ath9k_hw_setuprxdesc(ah, ds,
51 if (sc->rx.rxlink == NULL)
52 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
54 *sc->rx.rxlink = bf->bf_daddr;
56 sc->rx.rxlink = &ds->ds_link;
60 static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
62 /* XXX block beacon interrupts */
63 ath9k_hw_setantenna(sc->sc_ah, antenna);
64 sc->rx.defant = antenna;
65 sc->rx.rxotherant = 0;
69 * Extend 15-bit time stamp from rx descriptor to
70 * a full 64-bit TSF using the current h/w TSF.
72 static u64 ath_extend_tsf(struct ath_softc *sc, u32 rstamp)
76 tsf = ath9k_hw_gettsf64(sc->sc_ah);
77 if ((tsf & 0x7fff) < rstamp)
79 return (tsf & ~0x7fff) | rstamp;
82 static struct sk_buff *ath_rxbuf_alloc(struct ath_softc *sc, u32 len)
88 * Cache-line-align. This is important (for the
89 * 5210 at least) as not doing so causes bogus data
93 /* Note: the kernel can allocate a value greater than
94 * what we ask it to give us. We really only need 4 KB as that
95 * is this hardware supports and in fact we need at least 3849
96 * as that is the MAX AMSDU size this hardware supports.
97 * Unfortunately this means we may get 8 KB here from the
98 * kernel... and that is actually what is observed on some
100 skb = dev_alloc_skb(len + sc->cachelsz - 1);
102 off = ((unsigned long) skb->data) % sc->cachelsz;
104 skb_reserve(skb, sc->cachelsz - off);
106 DPRINTF(sc, ATH_DBG_FATAL,
107 "skbuff alloc of size %u failed\n", len);
115 * For Decrypt or Demic errors, we only mark packet status here and always push
116 * up the frame up to let mac80211 handle the actual error case, be it no
117 * decryption key or real decryption error. This let us keep statistics there.
119 static int ath_rx_prepare(struct sk_buff *skb, struct ath_desc *ds,
120 struct ieee80211_rx_status *rx_status, bool *decrypt_error,
121 struct ath_softc *sc)
123 struct ieee80211_hdr *hdr;
127 hdr = (struct ieee80211_hdr *)skb->data;
128 fc = hdr->frame_control;
129 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
131 if (ds->ds_rxstat.rs_more) {
133 * Frame spans multiple descriptors; this cannot happen yet
134 * as we don't support jumbograms. If not in monitor mode,
135 * discard the frame. Enable this if you want to see
136 * error frames in Monitor mode.
138 if (sc->sc_ah->ah_opmode != NL80211_IFTYPE_MONITOR)
140 } else if (ds->ds_rxstat.rs_status != 0) {
141 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_CRC)
142 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
143 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_PHY)
146 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_DECRYPT) {
147 *decrypt_error = true;
148 } else if (ds->ds_rxstat.rs_status & ATH9K_RXERR_MIC) {
149 if (ieee80211_is_ctl(fc))
151 * Sometimes, we get invalid
152 * MIC failures on valid control frames.
153 * Remove these mic errors.
155 ds->ds_rxstat.rs_status &= ~ATH9K_RXERR_MIC;
157 rx_status->flag |= RX_FLAG_MMIC_ERROR;
160 * Reject error frames with the exception of
161 * decryption and MIC failures. For monitor mode,
162 * we also ignore the CRC error.
164 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_MONITOR) {
165 if (ds->ds_rxstat.rs_status &
166 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
170 if (ds->ds_rxstat.rs_status &
171 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
177 ratecode = ds->ds_rxstat.rs_rate;
179 if (ratecode & 0x80) {
181 rx_status->flag |= RX_FLAG_HT;
182 if (ds->ds_rxstat.rs_flags & ATH9K_RX_2040)
183 rx_status->flag |= RX_FLAG_40MHZ;
184 if (ds->ds_rxstat.rs_flags & ATH9K_RX_GI)
185 rx_status->flag |= RX_FLAG_SHORT_GI;
186 rx_status->rate_idx = ratecode & 0x7f;
188 int i = 0, cur_band, n_rates;
189 struct ieee80211_hw *hw = sc->hw;
191 cur_band = hw->conf.channel->band;
192 n_rates = sc->sbands[cur_band].n_bitrates;
194 for (i = 0; i < n_rates; i++) {
195 if (sc->sbands[cur_band].bitrates[i].hw_value ==
197 rx_status->rate_idx = i;
201 if (sc->sbands[cur_band].bitrates[i].hw_value_short ==
203 rx_status->rate_idx = i;
204 rx_status->flag |= RX_FLAG_SHORTPRE;
210 rx_status->mactime = ath_extend_tsf(sc, ds->ds_rxstat.rs_tstamp);
211 rx_status->band = sc->hw->conf.channel->band;
212 rx_status->freq = sc->hw->conf.channel->center_freq;
213 rx_status->noise = sc->ani.noise_floor;
214 rx_status->signal = rx_status->noise + ds->ds_rxstat.rs_rssi;
215 rx_status->antenna = ds->ds_rxstat.rs_antenna;
217 /* at 45 you will be able to use MCS 15 reliably. A more elaborate
218 * scheme can be used here but it requires tables of SNR/throughput for
219 * each possible mode used. */
220 rx_status->qual = ds->ds_rxstat.rs_rssi * 100 / 45;
222 /* rssi can be more than 45 though, anything above that
223 * should be considered at 100% */
224 if (rx_status->qual > 100)
225 rx_status->qual = 100;
227 rx_status->flag |= RX_FLAG_TSFT;
234 static void ath_opmode_init(struct ath_softc *sc)
236 struct ath_hw *ah = sc->sc_ah;
239 /* configure rx filter */
240 rfilt = ath_calcrxfilter(sc);
241 ath9k_hw_setrxfilter(ah, rfilt);
243 /* configure bssid mask */
244 if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
245 ath9k_hw_setbssidmask(sc);
247 /* configure operational mode */
248 ath9k_hw_setopmode(ah);
250 /* Handle any link-level address change. */
251 ath9k_hw_setmac(ah, sc->sc_ah->macaddr);
253 /* calculate and install multicast filter */
254 mfilt[0] = mfilt[1] = ~0;
255 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
258 int ath_rx_init(struct ath_softc *sc, int nbufs)
265 spin_lock_init(&sc->rx.rxflushlock);
266 sc->sc_flags &= ~SC_OP_RXFLUSH;
267 spin_lock_init(&sc->rx.rxbuflock);
269 sc->rx.bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
273 DPRINTF(sc, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
274 sc->cachelsz, sc->rx.bufsize);
276 /* Initialize rx descriptors */
278 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
281 DPRINTF(sc, ATH_DBG_FATAL,
282 "failed to allocate rx descriptors: %d\n", error);
286 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
287 skb = ath_rxbuf_alloc(sc, sc->rx.bufsize);
294 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
297 if (unlikely(dma_mapping_error(sc->dev,
299 dev_kfree_skb_any(skb);
301 DPRINTF(sc, ATH_DBG_CONFIG,
302 "dma_mapping_error() on RX init\n");
306 bf->bf_dmacontext = bf->bf_buf_addr;
308 sc->rx.rxlink = NULL;
318 void ath_rx_cleanup(struct ath_softc *sc)
323 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
329 if (sc->rx.rxdma.dd_desc_len != 0)
330 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
334 * Calculate the receive filter according to the
335 * operating mode and state:
337 * o always accept unicast, broadcast, and multicast traffic
338 * o maintain current state of phy error reception (the hal
339 * may enable phy error frames for noise immunity work)
340 * o probe request frames are accepted only when operating in
341 * hostap, adhoc, or monitor modes
342 * o enable promiscuous mode according to the interface state
344 * - when operating in adhoc mode so the 802.11 layer creates
345 * node table entries for peers,
346 * - when operating in station mode for collecting rssi data when
347 * the station is otherwise quiet, or
348 * - when operating as a repeater so we see repeater-sta beacons
352 u32 ath_calcrxfilter(struct ath_softc *sc)
354 #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
358 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
359 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
360 | ATH9K_RX_FILTER_MCAST;
362 /* If not a STA, enable processing of Probe Requests */
363 if (sc->sc_ah->ah_opmode != NL80211_IFTYPE_STATION)
364 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
366 /* Can't set HOSTAP into promiscous mode */
367 if (((sc->sc_ah->ah_opmode != NL80211_IFTYPE_AP) &&
368 (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
369 (sc->sc_ah->ah_opmode == NL80211_IFTYPE_MONITOR)) {
370 rfilt |= ATH9K_RX_FILTER_PROM;
371 /* ??? To prevent from sending ACK */
372 rfilt &= ~ATH9K_RX_FILTER_UCAST;
375 if (sc->rx.rxfilter & FIF_CONTROL)
376 rfilt |= ATH9K_RX_FILTER_CONTROL;
378 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_STATION ||
379 sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC)
380 rfilt |= ATH9K_RX_FILTER_BEACON;
382 /* If in HOSTAP mode, want to enable reception of PSPOLL frames
384 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_AP)
385 rfilt |= (ATH9K_RX_FILTER_BEACON | ATH9K_RX_FILTER_PSPOLL);
389 #undef RX_FILTER_PRESERVE
392 int ath_startrecv(struct ath_softc *sc)
394 struct ath_hw *ah = sc->sc_ah;
395 struct ath_buf *bf, *tbf;
397 spin_lock_bh(&sc->rx.rxbuflock);
398 if (list_empty(&sc->rx.rxbuf))
401 sc->rx.rxlink = NULL;
402 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
403 ath_rx_buf_link(sc, bf);
406 /* We could have deleted elements so the list may be empty now */
407 if (list_empty(&sc->rx.rxbuf))
410 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
411 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
415 spin_unlock_bh(&sc->rx.rxbuflock);
417 ath9k_hw_startpcureceive(ah);
422 bool ath_stoprecv(struct ath_softc *sc)
424 struct ath_hw *ah = sc->sc_ah;
427 ath9k_hw_stoppcurecv(ah);
428 ath9k_hw_setrxfilter(ah, 0);
429 stopped = ath9k_hw_stopdmarecv(ah);
430 mdelay(3); /* 3ms is long enough for 1 frame */
431 sc->rx.rxlink = NULL;
436 void ath_flushrecv(struct ath_softc *sc)
438 spin_lock_bh(&sc->rx.rxflushlock);
439 sc->sc_flags |= SC_OP_RXFLUSH;
440 ath_rx_tasklet(sc, 1);
441 sc->sc_flags &= ~SC_OP_RXFLUSH;
442 spin_unlock_bh(&sc->rx.rxflushlock);
445 int ath_rx_tasklet(struct ath_softc *sc, int flush)
447 #define PA2DESC(_sc, _pa) \
448 ((struct ath_desc *)((caddr_t)(_sc)->rx.rxdma.dd_desc + \
449 ((_pa) - (_sc)->rx.rxdma.dd_desc_paddr)))
453 struct sk_buff *skb = NULL, *requeue_skb;
454 struct ieee80211_rx_status rx_status;
455 struct ath_hw *ah = sc->sc_ah;
456 struct ieee80211_hdr *hdr;
457 int hdrlen, padsize, retval;
458 bool decrypt_error = false;
461 spin_lock_bh(&sc->rx.rxbuflock);
464 /* If handling rx interrupt and flush is in progress => exit */
465 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
468 if (list_empty(&sc->rx.rxbuf)) {
469 sc->rx.rxlink = NULL;
473 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
477 * Must provide the virtual address of the current
478 * descriptor, the physical address, and the virtual
479 * address of the next descriptor in the h/w chain.
480 * This allows the HAL to look ahead to see if the
481 * hardware is done with a descriptor by checking the
482 * done bit in the following descriptor and the address
483 * of the current descriptor the DMA engine is working
484 * on. All this is necessary because of our use of
485 * a self-linked list to avoid rx overruns.
487 retval = ath9k_hw_rxprocdesc(ah, ds,
489 PA2DESC(sc, ds->ds_link),
491 if (retval == -EINPROGRESS) {
493 struct ath_desc *tds;
495 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
496 sc->rx.rxlink = NULL;
500 tbf = list_entry(bf->list.next, struct ath_buf, list);
503 * On some hardware the descriptor status words could
504 * get corrupted, including the done bit. Because of
505 * this, check if the next descriptor's done bit is
508 * If the next descriptor's done bit is set, the current
509 * descriptor has been corrupted. Force s/w to discard
510 * this descriptor and continue...
514 retval = ath9k_hw_rxprocdesc(ah, tds, tbf->bf_daddr,
515 PA2DESC(sc, tds->ds_link), 0);
516 if (retval == -EINPROGRESS) {
526 * Synchronize the DMA transfer with CPU before
527 * 1. accessing the frame
528 * 2. requeueing the same buffer to h/w
530 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
535 * If we're asked to flush receive queue, directly
536 * chain it back at the queue without processing it.
541 if (!ds->ds_rxstat.rs_datalen)
544 /* The status portion of the descriptor could get corrupted. */
545 if (sc->rx.bufsize < ds->ds_rxstat.rs_datalen)
548 if (!ath_rx_prepare(skb, ds, &rx_status, &decrypt_error, sc))
551 /* Ensure we always have an skb to requeue once we are done
552 * processing the current buffer's skb */
553 requeue_skb = ath_rxbuf_alloc(sc, sc->rx.bufsize);
555 /* If there is no memory we ignore the current RX'd frame,
556 * tell hardware it can give us a new frame using the old
557 * skb and put it at the tail of the sc->rx.rxbuf list for
562 /* Unmap the frame */
563 dma_unmap_single(sc->dev, bf->bf_buf_addr,
567 skb_put(skb, ds->ds_rxstat.rs_datalen);
568 skb->protocol = cpu_to_be16(ETH_P_CONTROL);
570 /* see if any padding is done by the hw and remove it */
571 hdr = (struct ieee80211_hdr *)skb->data;
572 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
574 /* The MAC header is padded to have 32-bit boundary if the
575 * packet payload is non-zero. The general calculation for
576 * padsize would take into account odd header lengths:
577 * padsize = (4 - hdrlen % 4) % 4; However, since only
578 * even-length headers are used, padding can only be 0 or 2
579 * bytes and we can optimize this a bit. In addition, we must
580 * not try to remove padding from short control frames that do
581 * not have payload. */
582 padsize = hdrlen & 3;
583 if (padsize && hdrlen >= 24) {
584 memmove(skb->data + padsize, skb->data, hdrlen);
585 skb_pull(skb, padsize);
588 keyix = ds->ds_rxstat.rs_keyix;
590 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
591 rx_status.flag |= RX_FLAG_DECRYPTED;
592 } else if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED)
593 && !decrypt_error && skb->len >= hdrlen + 4) {
594 keyix = skb->data[hdrlen + 3] >> 6;
596 if (test_bit(keyix, sc->keymap))
597 rx_status.flag |= RX_FLAG_DECRYPTED;
599 if (ah->sw_mgmt_crypto &&
600 (rx_status.flag & RX_FLAG_DECRYPTED) &&
601 ieee80211_is_mgmt(hdr->frame_control)) {
602 /* Use software decrypt for management frames. */
603 rx_status.flag &= ~RX_FLAG_DECRYPTED;
606 /* Send the frame to mac80211 */
607 __ieee80211_rx(sc->hw, skb, &rx_status);
609 /* We will now give hardware our shiny new allocated skb */
610 bf->bf_mpdu = requeue_skb;
611 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
614 if (unlikely(dma_mapping_error(sc->dev,
616 dev_kfree_skb_any(requeue_skb);
618 DPRINTF(sc, ATH_DBG_CONFIG,
619 "dma_mapping_error() on RX\n");
622 bf->bf_dmacontext = bf->bf_buf_addr;
625 * change the default rx antenna if rx diversity chooses the
626 * other antenna 3 times in a row.
628 if (sc->rx.defant != ds->ds_rxstat.rs_antenna) {
629 if (++sc->rx.rxotherant >= 3)
630 ath_setdefantenna(sc, ds->ds_rxstat.rs_antenna);
632 sc->rx.rxotherant = 0;
635 if (ieee80211_is_beacon(hdr->frame_control) &&
636 (sc->sc_flags & SC_OP_WAIT_FOR_BEACON)) {
637 sc->sc_flags &= ~SC_OP_WAIT_FOR_BEACON;
638 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
641 list_move_tail(&bf->list, &sc->rx.rxbuf);
642 ath_rx_buf_link(sc, bf);
645 spin_unlock_bh(&sc->rx.rxbuflock);