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_hal *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 sc_rxbufsize here tells the harware
45 * how much data it can DMA to us and that we are prepared
47 ath9k_hw_setuprxdesc(ah,
52 if (sc->sc_rxlink == NULL)
53 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
55 *sc->sc_rxlink = bf->bf_daddr;
57 sc->sc_rxlink = &ds->ds_link;
61 static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
63 /* XXX block beacon interrupts */
64 ath9k_hw_setantenna(sc->sc_ah, antenna);
65 sc->sc_defant = antenna;
66 sc->sc_rxotherant = 0;
70 * Extend 15-bit time stamp from rx descriptor to
71 * a full 64-bit TSF using the current h/w TSF.
73 static u64 ath_extend_tsf(struct ath_softc *sc, u32 rstamp)
77 tsf = ath9k_hw_gettsf64(sc->sc_ah);
78 if ((tsf & 0x7fff) < rstamp)
80 return (tsf & ~0x7fff) | rstamp;
83 static struct sk_buff *ath_rxbuf_alloc(struct ath_softc *sc, u32 len)
89 * Cache-line-align. This is important (for the
90 * 5210 at least) as not doing so causes bogus data
94 /* Note: the kernel can allocate a value greater than
95 * what we ask it to give us. We really only need 4 KB as that
96 * is this hardware supports and in fact we need at least 3849
97 * as that is the MAX AMSDU size this hardware supports.
98 * Unfortunately this means we may get 8 KB here from the
99 * kernel... and that is actually what is observed on some
101 skb = dev_alloc_skb(len + sc->sc_cachelsz - 1);
103 off = ((unsigned long) skb->data) % sc->sc_cachelsz;
105 skb_reserve(skb, sc->sc_cachelsz - off);
107 DPRINTF(sc, ATH_DBG_FATAL,
108 "skbuff alloc of size %u failed\n", len);
115 static int ath_rate2idx(struct ath_softc *sc, int rate)
117 int i = 0, cur_band, n_rates;
118 struct ieee80211_hw *hw = sc->hw;
120 cur_band = hw->conf.channel->band;
121 n_rates = sc->sbands[cur_band].n_bitrates;
123 for (i = 0; i < n_rates; i++) {
124 if (sc->sbands[cur_band].bitrates[i].bitrate == rate)
129 * NB:mac80211 validates rx rate index against the supported legacy rate
130 * index only (should be done against ht rates also), return the highest
131 * legacy rate index for rx rate which does not match any one of the
132 * supported basic and extended rates to make mac80211 happy.
133 * The following hack will be cleaned up once the issue with
134 * the rx rate index validation in mac80211 is fixed.
143 * For Decrypt or Demic errors, we only mark packet status here and always push
144 * up the frame up to let mac80211 handle the actual error case, be it no
145 * decryption key or real decryption error. This let us keep statistics there.
147 static int ath_rx_prepare(struct sk_buff *skb, struct ath_desc *ds,
148 struct ieee80211_rx_status *rx_status, bool *decrypt_error,
149 struct ath_softc *sc)
151 struct ath_rate_table *rate_table = sc->cur_rate_table;
152 struct ieee80211_hdr *hdr;
157 hdr = (struct ieee80211_hdr *)skb->data;
158 fc = hdr->frame_control;
159 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
161 if (ds->ds_rxstat.rs_more) {
163 * Frame spans multiple descriptors; this cannot happen yet
164 * as we don't support jumbograms. If not in monitor mode,
165 * discard the frame. Enable this if you want to see
166 * error frames in Monitor mode.
168 if (sc->sc_ah->ah_opmode != NL80211_IFTYPE_MONITOR)
170 } else if (ds->ds_rxstat.rs_status != 0) {
171 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_CRC)
172 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
173 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_PHY)
176 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_DECRYPT) {
177 *decrypt_error = true;
178 } else if (ds->ds_rxstat.rs_status & ATH9K_RXERR_MIC) {
179 if (ieee80211_is_ctl(fc))
181 * Sometimes, we get invalid
182 * MIC failures on valid control frames.
183 * Remove these mic errors.
185 ds->ds_rxstat.rs_status &= ~ATH9K_RXERR_MIC;
187 rx_status->flag |= RX_FLAG_MMIC_ERROR;
190 * Reject error frames with the exception of
191 * decryption and MIC failures. For monitor mode,
192 * we also ignore the CRC error.
194 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_MONITOR) {
195 if (ds->ds_rxstat.rs_status &
196 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
200 if (ds->ds_rxstat.rs_status &
201 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
207 ratecode = ds->ds_rxstat.rs_rate;
208 rix = rate_table->rateCodeToIndex[ratecode];
209 ratekbps = rate_table->info[rix].ratekbps;
212 if (ratecode & 0x80) {
213 if (ds->ds_rxstat.rs_flags & ATH9K_RX_2040)
214 ratekbps = (ratekbps * 27) / 13;
215 if (ds->ds_rxstat.rs_flags & ATH9K_RX_GI)
216 ratekbps = (ratekbps * 10) / 9;
219 rx_status->mactime = ath_extend_tsf(sc, ds->ds_rxstat.rs_tstamp);
220 rx_status->band = sc->hw->conf.channel->band;
221 rx_status->freq = sc->hw->conf.channel->center_freq;
222 rx_status->noise = sc->sc_ani.sc_noise_floor;
223 rx_status->signal = rx_status->noise + ds->ds_rxstat.rs_rssi;
224 rx_status->rate_idx = ath_rate2idx(sc, (ratekbps / 100));
225 rx_status->antenna = ds->ds_rxstat.rs_antenna;
227 /* at 45 you will be able to use MCS 15 reliably. A more elaborate
228 * scheme can be used here but it requires tables of SNR/throughput for
229 * each possible mode used. */
230 rx_status->qual = ds->ds_rxstat.rs_rssi * 100 / 45;
232 /* rssi can be more than 45 though, anything above that
233 * should be considered at 100% */
234 if (rx_status->qual > 100)
235 rx_status->qual = 100;
237 rx_status->flag |= RX_FLAG_TSFT;
244 static void ath_opmode_init(struct ath_softc *sc)
246 struct ath_hal *ah = sc->sc_ah;
249 /* configure rx filter */
250 rfilt = ath_calcrxfilter(sc);
251 ath9k_hw_setrxfilter(ah, rfilt);
253 /* configure bssid mask */
254 if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
255 ath9k_hw_setbssidmask(ah, sc->sc_bssidmask);
257 /* configure operational mode */
258 ath9k_hw_setopmode(ah);
260 /* Handle any link-level address change. */
261 ath9k_hw_setmac(ah, sc->sc_myaddr);
263 /* calculate and install multicast filter */
264 mfilt[0] = mfilt[1] = ~0;
265 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
268 int ath_rx_init(struct ath_softc *sc, int nbufs)
275 spin_lock_init(&sc->sc_rxflushlock);
276 sc->sc_flags &= ~SC_OP_RXFLUSH;
277 spin_lock_init(&sc->sc_rxbuflock);
279 sc->sc_rxbufsize = roundup(IEEE80211_MAX_MPDU_LEN,
283 DPRINTF(sc, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
284 sc->sc_cachelsz, sc->sc_rxbufsize);
286 /* Initialize rx descriptors */
288 error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
291 DPRINTF(sc, ATH_DBG_FATAL,
292 "failed to allocate rx descriptors: %d\n", error);
296 list_for_each_entry(bf, &sc->sc_rxbuf, list) {
297 skb = ath_rxbuf_alloc(sc, sc->sc_rxbufsize);
304 bf->bf_buf_addr = pci_map_single(sc->pdev, skb->data,
307 if (unlikely(pci_dma_mapping_error(sc->pdev,
309 dev_kfree_skb_any(skb);
311 DPRINTF(sc, ATH_DBG_CONFIG,
312 "pci_dma_mapping_error() on RX init\n");
316 bf->bf_dmacontext = bf->bf_buf_addr;
318 sc->sc_rxlink = NULL;
328 void ath_rx_cleanup(struct ath_softc *sc)
333 list_for_each_entry(bf, &sc->sc_rxbuf, list) {
339 if (sc->sc_rxdma.dd_desc_len != 0)
340 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
344 * Calculate the receive filter according to the
345 * operating mode and state:
347 * o always accept unicast, broadcast, and multicast traffic
348 * o maintain current state of phy error reception (the hal
349 * may enable phy error frames for noise immunity work)
350 * o probe request frames are accepted only when operating in
351 * hostap, adhoc, or monitor modes
352 * o enable promiscuous mode according to the interface state
354 * - when operating in adhoc mode so the 802.11 layer creates
355 * node table entries for peers,
356 * - when operating in station mode for collecting rssi data when
357 * the station is otherwise quiet, or
358 * - when operating as a repeater so we see repeater-sta beacons
362 u32 ath_calcrxfilter(struct ath_softc *sc)
364 #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
368 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
369 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
370 | ATH9K_RX_FILTER_MCAST;
372 /* If not a STA, enable processing of Probe Requests */
373 if (sc->sc_ah->ah_opmode != NL80211_IFTYPE_STATION)
374 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
376 /* Can't set HOSTAP into promiscous mode */
377 if (((sc->sc_ah->ah_opmode != NL80211_IFTYPE_AP) &&
378 (sc->rx_filter & FIF_PROMISC_IN_BSS)) ||
379 (sc->sc_ah->ah_opmode == NL80211_IFTYPE_MONITOR)) {
380 rfilt |= ATH9K_RX_FILTER_PROM;
381 /* ??? To prevent from sending ACK */
382 rfilt &= ~ATH9K_RX_FILTER_UCAST;
385 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_STATION ||
386 sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC)
387 rfilt |= ATH9K_RX_FILTER_BEACON;
389 /* If in HOSTAP mode, want to enable reception of PSPOLL frames
391 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_AP)
392 rfilt |= (ATH9K_RX_FILTER_BEACON | ATH9K_RX_FILTER_PSPOLL);
396 #undef RX_FILTER_PRESERVE
399 int ath_startrecv(struct ath_softc *sc)
401 struct ath_hal *ah = sc->sc_ah;
402 struct ath_buf *bf, *tbf;
404 spin_lock_bh(&sc->sc_rxbuflock);
405 if (list_empty(&sc->sc_rxbuf))
408 sc->sc_rxlink = NULL;
409 list_for_each_entry_safe(bf, tbf, &sc->sc_rxbuf, list) {
410 ath_rx_buf_link(sc, bf);
413 /* We could have deleted elements so the list may be empty now */
414 if (list_empty(&sc->sc_rxbuf))
417 bf = list_first_entry(&sc->sc_rxbuf, struct ath_buf, list);
418 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
422 spin_unlock_bh(&sc->sc_rxbuflock);
424 ath9k_hw_startpcureceive(ah);
429 bool ath_stoprecv(struct ath_softc *sc)
431 struct ath_hal *ah = sc->sc_ah;
434 ath9k_hw_stoppcurecv(ah);
435 ath9k_hw_setrxfilter(ah, 0);
436 stopped = ath9k_hw_stopdmarecv(ah);
437 mdelay(3); /* 3ms is long enough for 1 frame */
438 sc->sc_rxlink = NULL;
443 void ath_flushrecv(struct ath_softc *sc)
445 spin_lock_bh(&sc->sc_rxflushlock);
446 sc->sc_flags |= SC_OP_RXFLUSH;
447 ath_rx_tasklet(sc, 1);
448 sc->sc_flags &= ~SC_OP_RXFLUSH;
449 spin_unlock_bh(&sc->sc_rxflushlock);
452 int ath_rx_tasklet(struct ath_softc *sc, int flush)
454 #define PA2DESC(_sc, _pa) \
455 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
456 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
460 struct sk_buff *skb = NULL, *requeue_skb;
461 struct ieee80211_rx_status rx_status;
462 struct ath_hal *ah = sc->sc_ah;
463 struct ieee80211_hdr *hdr;
464 int hdrlen, padsize, retval;
465 bool decrypt_error = false;
468 spin_lock_bh(&sc->sc_rxbuflock);
471 /* If handling rx interrupt and flush is in progress => exit */
472 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
475 if (list_empty(&sc->sc_rxbuf)) {
476 sc->sc_rxlink = NULL;
480 bf = list_first_entry(&sc->sc_rxbuf, struct ath_buf, list);
484 * Must provide the virtual address of the current
485 * descriptor, the physical address, and the virtual
486 * address of the next descriptor in the h/w chain.
487 * This allows the HAL to look ahead to see if the
488 * hardware is done with a descriptor by checking the
489 * done bit in the following descriptor and the address
490 * of the current descriptor the DMA engine is working
491 * on. All this is necessary because of our use of
492 * a self-linked list to avoid rx overruns.
494 retval = ath9k_hw_rxprocdesc(ah, ds,
496 PA2DESC(sc, ds->ds_link),
498 if (retval == -EINPROGRESS) {
500 struct ath_desc *tds;
502 if (list_is_last(&bf->list, &sc->sc_rxbuf)) {
503 sc->sc_rxlink = NULL;
507 tbf = list_entry(bf->list.next, struct ath_buf, list);
510 * On some hardware the descriptor status words could
511 * get corrupted, including the done bit. Because of
512 * this, check if the next descriptor's done bit is
515 * If the next descriptor's done bit is set, the current
516 * descriptor has been corrupted. Force s/w to discard
517 * this descriptor and continue...
521 retval = ath9k_hw_rxprocdesc(ah, tds, tbf->bf_daddr,
522 PA2DESC(sc, tds->ds_link), 0);
523 if (retval == -EINPROGRESS) {
533 * If we're asked to flush receive queue, directly
534 * chain it back at the queue without processing it.
539 if (!ds->ds_rxstat.rs_datalen)
542 /* The status portion of the descriptor could get corrupted. */
543 if (sc->sc_rxbufsize < ds->ds_rxstat.rs_datalen)
546 if (!ath_rx_prepare(skb, ds, &rx_status, &decrypt_error, sc))
549 /* Ensure we always have an skb to requeue once we are done
550 * processing the current buffer's skb */
551 requeue_skb = ath_rxbuf_alloc(sc, sc->sc_rxbufsize);
553 /* If there is no memory we ignore the current RX'd frame,
554 * tell hardware it can give us a new frame using the old
555 * skb and put it at the tail of the sc->sc_rxbuf list for
560 pci_dma_sync_single_for_cpu(sc->pdev,
564 pci_unmap_single(sc->pdev, bf->bf_buf_addr,
568 skb_put(skb, ds->ds_rxstat.rs_datalen);
569 skb->protocol = cpu_to_be16(ETH_P_CONTROL);
571 /* see if any padding is done by the hw and remove it */
572 hdr = (struct ieee80211_hdr *)skb->data;
573 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
576 padsize = hdrlen % 4;
577 memmove(skb->data + padsize, skb->data, hdrlen);
578 skb_pull(skb, padsize);
581 keyix = ds->ds_rxstat.rs_keyix;
583 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
584 rx_status.flag |= RX_FLAG_DECRYPTED;
585 } else if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED)
586 && !decrypt_error && skb->len >= hdrlen + 4) {
587 keyix = skb->data[hdrlen + 3] >> 6;
589 if (test_bit(keyix, sc->sc_keymap))
590 rx_status.flag |= RX_FLAG_DECRYPTED;
593 /* Send the frame to mac80211 */
594 __ieee80211_rx(sc->hw, skb, &rx_status);
596 /* We will now give hardware our shiny new allocated skb */
597 bf->bf_mpdu = requeue_skb;
598 bf->bf_buf_addr = pci_map_single(sc->pdev, requeue_skb->data,
601 if (unlikely(pci_dma_mapping_error(sc->pdev,
603 dev_kfree_skb_any(requeue_skb);
605 DPRINTF(sc, ATH_DBG_CONFIG,
606 "pci_dma_mapping_error() on RX\n");
609 bf->bf_dmacontext = bf->bf_buf_addr;
612 * change the default rx antenna if rx diversity chooses the
613 * other antenna 3 times in a row.
615 if (sc->sc_defant != ds->ds_rxstat.rs_antenna) {
616 if (++sc->sc_rxotherant >= 3)
617 ath_setdefantenna(sc, ds->ds_rxstat.rs_antenna);
619 sc->sc_rxotherant = 0;
622 list_move_tail(&bf->list, &sc->sc_rxbuf);
623 ath_rx_buf_link(sc, bf);
626 spin_unlock_bh(&sc->sc_rxbuflock);