2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Driver for the Atheros Wireless LAN controller.
36 * This software is derived from work of Atsushi Onoe; his contribution
37 * is greatly appreciated.
43 * This is needed for register operations which are performed
44 * by the driver - eg, calls to ath_hal_gettsf32().
46 * It's also required for any AH_DEBUG checks in here, eg the
47 * module dependencies.
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/sysctl.h>
56 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/kernel.h>
60 #include <sys/socket.h>
61 #include <sys/sockio.h>
62 #include <sys/errno.h>
63 #include <sys/callout.h>
65 #include <sys/endian.h>
66 #include <sys/kthread.h>
67 #include <sys/taskqueue.h>
69 #include <sys/module.h>
71 #include <sys/smp.h> /* for mp_ncpus */
73 #include <machine/bus.h>
76 #include <net/if_dl.h>
77 #include <net/if_media.h>
78 #include <net/if_types.h>
79 #include <net/if_arp.h>
80 #include <net/ethernet.h>
81 #include <net/if_llc.h>
83 #include <net80211/ieee80211_var.h>
84 #include <net80211/ieee80211_regdomain.h>
85 #ifdef IEEE80211_SUPPORT_SUPERG
86 #include <net80211/ieee80211_superg.h>
88 #ifdef IEEE80211_SUPPORT_TDMA
89 #include <net80211/ieee80211_tdma.h>
95 #include <netinet/in.h>
96 #include <netinet/if_ether.h>
99 #include <dev/ath/if_athvar.h>
100 #include <dev/ath/ath_hal/ah_devid.h> /* XXX for softled */
101 #include <dev/ath/ath_hal/ah_diagcodes.h>
103 #include <dev/ath/if_ath_debug.h>
104 #include <dev/ath/if_ath_misc.h>
105 #include <dev/ath/if_ath_tsf.h>
106 #include <dev/ath/if_ath_tx.h>
107 #include <dev/ath/if_ath_sysctl.h>
108 #include <dev/ath/if_ath_led.h>
109 #include <dev/ath/if_ath_keycache.h>
110 #include <dev/ath/if_ath_rx.h>
111 #include <dev/ath/if_ath_beacon.h>
112 #include <dev/ath/if_athdfs.h>
115 #include <dev/ath/ath_tx99/ath_tx99.h>
119 #include <dev/ath/if_ath_alq.h>
123 * Calculate the receive filter according to the
124 * operating mode and state:
126 * o always accept unicast, broadcast, and multicast traffic
127 * o accept PHY error frames when hardware doesn't have MIB support
128 * to count and we need them for ANI (sta mode only until recently)
129 * and we are not scanning (ANI is disabled)
130 * NB: older hal's add rx filter bits out of sight and we need to
131 * blindly preserve them
132 * o probe request frames are accepted only when operating in
133 * hostap, adhoc, mesh, or monitor modes
134 * o enable promiscuous mode
135 * - when in monitor mode
136 * - if interface marked PROMISC (assumes bridge setting is filtered)
138 * - when operating in station mode for collecting rssi data when
139 * the station is otherwise quiet, or
140 * - when operating in adhoc mode so the 802.11 layer creates
141 * node table entries for peers,
143 * - when doing s/w beacon miss (e.g. for ap+sta)
144 * - when operating in ap mode in 11g to detect overlapping bss that
146 * - when operating in mesh mode to detect neighbors
147 * o accept control frames:
148 * - when in monitor mode
149 * XXX HT protection for 11n
152 ath_calcrxfilter(struct ath_softc *sc)
154 struct ifnet *ifp = sc->sc_ifp;
155 struct ieee80211com *ic = ifp->if_l2com;
158 rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
159 if (!sc->sc_needmib && !sc->sc_scanning)
160 rfilt |= HAL_RX_FILTER_PHYERR;
161 if (ic->ic_opmode != IEEE80211_M_STA)
162 rfilt |= HAL_RX_FILTER_PROBEREQ;
163 /* XXX ic->ic_monvaps != 0? */
164 if (ic->ic_opmode == IEEE80211_M_MONITOR || (ifp->if_flags & IFF_PROMISC))
165 rfilt |= HAL_RX_FILTER_PROM;
166 if (ic->ic_opmode == IEEE80211_M_STA ||
167 ic->ic_opmode == IEEE80211_M_IBSS ||
168 sc->sc_swbmiss || sc->sc_scanning)
169 rfilt |= HAL_RX_FILTER_BEACON;
171 * NB: We don't recalculate the rx filter when
172 * ic_protmode changes; otherwise we could do
173 * this only when ic_protmode != NONE.
175 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
176 IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
177 rfilt |= HAL_RX_FILTER_BEACON;
180 * Enable hardware PS-POLL RX only for hostap mode;
181 * STA mode sends PS-POLL frames but never
184 if (ath_hal_getcapability(sc->sc_ah, HAL_CAP_PSPOLL,
185 0, NULL) == HAL_OK &&
186 ic->ic_opmode == IEEE80211_M_HOSTAP)
187 rfilt |= HAL_RX_FILTER_PSPOLL;
189 if (sc->sc_nmeshvaps) {
190 rfilt |= HAL_RX_FILTER_BEACON;
191 if (sc->sc_hasbmatch)
192 rfilt |= HAL_RX_FILTER_BSSID;
194 rfilt |= HAL_RX_FILTER_PROM;
196 if (ic->ic_opmode == IEEE80211_M_MONITOR)
197 rfilt |= HAL_RX_FILTER_CONTROL;
200 * Enable RX of compressed BAR frames only when doing
201 * 802.11n. Required for A-MPDU.
203 if (IEEE80211_IS_CHAN_HT(ic->ic_curchan))
204 rfilt |= HAL_RX_FILTER_COMPBAR;
207 * Enable radar PHY errors if requested by the
211 rfilt |= HAL_RX_FILTER_PHYRADAR;
214 * Enable spectral PHY errors if requested by the
217 if (sc->sc_dospectral)
218 rfilt |= HAL_RX_FILTER_PHYRADAR;
220 DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n",
221 __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags);
226 ath_legacy_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
228 struct ath_hal *ah = sc->sc_ah;
236 * NB: by assigning a page to the rx dma buffer we
237 * implicitly satisfy the Atheros requirement that
238 * this buffer be cache-line-aligned and sized to be
239 * multiple of the cache line size. Not doing this
240 * causes weird stuff to happen (for the 5210 at least).
242 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
244 DPRINTF(sc, ATH_DEBUG_ANY,
245 "%s: no mbuf/cluster\n", __func__);
246 sc->sc_stats.ast_rx_nombuf++;
249 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
251 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
253 bf->bf_segs, &bf->bf_nseg,
256 DPRINTF(sc, ATH_DEBUG_ANY,
257 "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
259 sc->sc_stats.ast_rx_busdma++;
263 KASSERT(bf->bf_nseg == 1,
264 ("multi-segment packet; nseg %u", bf->bf_nseg));
267 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
270 * Setup descriptors. For receive we always terminate
271 * the descriptor list with a self-linked entry so we'll
272 * not get overrun under high load (as can happen with a
273 * 5212 when ANI processing enables PHY error frames).
275 * To insure the last descriptor is self-linked we create
276 * each descriptor as self-linked and add it to the end. As
277 * each additional descriptor is added the previous self-linked
278 * entry is ``fixed'' naturally. This should be safe even
279 * if DMA is happening. When processing RX interrupts we
280 * never remove/process the last, self-linked, entry on the
281 * descriptor list. This insures the hardware always has
282 * someplace to write a new frame.
285 * 11N: we can no longer afford to self link the last descriptor.
286 * MAC acknowledges BA status as long as it copies frames to host
287 * buffer (or rx fifo). This can incorrectly acknowledge packets
288 * to a sender if last desc is self-linked.
292 ds->ds_link = bf->bf_daddr; /* link to self */
294 ds->ds_link = 0; /* terminate the list */
295 ds->ds_data = bf->bf_segs[0].ds_addr;
296 ath_hal_setuprxdesc(ah, ds
297 , m->m_len /* buffer size */
301 if (sc->sc_rxlink != NULL)
302 *sc->sc_rxlink = bf->bf_daddr;
303 sc->sc_rxlink = &ds->ds_link;
308 * Intercept management frames to collect beacon rssi data
309 * and to do ibss merges.
312 ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
313 int subtype, int rssi, int nf)
315 struct ieee80211vap *vap = ni->ni_vap;
316 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
319 * Call up first so subsequent work can use information
320 * potentially stored in the node (e.g. for ibss merge).
322 ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
324 case IEEE80211_FC0_SUBTYPE_BEACON:
325 /* update rssi statistics for use by the hal */
326 /* XXX unlocked check against vap->iv_bss? */
327 ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
328 if (sc->sc_syncbeacon &&
329 ni == vap->iv_bss && vap->iv_state == IEEE80211_S_RUN) {
331 * Resync beacon timers using the tsf of the beacon
332 * frame we just received.
334 ath_beacon_config(sc, vap);
337 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
338 if (vap->iv_opmode == IEEE80211_M_IBSS &&
339 vap->iv_state == IEEE80211_S_RUN) {
340 uint32_t rstamp = sc->sc_lastrs->rs_tstamp;
341 uint64_t tsf = ath_extend_tsf(sc, rstamp,
342 ath_hal_gettsf64(sc->sc_ah));
344 * Handle ibss merge as needed; check the tsf on the
345 * frame before attempting the merge. The 802.11 spec
346 * says the station should change it's bssid to match
347 * the oldest station with the same ssid, where oldest
348 * is determined by the tsf. Note that hardware
349 * reconfiguration happens through callback to
350 * ath_newstate as the state machine will go from
351 * RUN -> RUN when this happens.
353 if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
354 DPRINTF(sc, ATH_DEBUG_STATE,
355 "ibss merge, rstamp %u tsf %ju "
356 "tstamp %ju\n", rstamp, (uintmax_t)tsf,
357 (uintmax_t)ni->ni_tstamp.tsf);
358 (void) ieee80211_ibss_merge(ni);
365 #ifdef ATH_ENABLE_RADIOTAP_VENDOR_EXT
367 ath_rx_tap_vendor(struct ifnet *ifp, struct mbuf *m,
368 const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
370 struct ath_softc *sc = ifp->if_softc;
372 /* Fill in the extension bitmap */
373 sc->sc_rx_th.wr_ext_bitmap = htole32(1 << ATH_RADIOTAP_VENDOR_HEADER);
375 /* Fill in the vendor header */
376 sc->sc_rx_th.wr_vh.vh_oui[0] = 0x7f;
377 sc->sc_rx_th.wr_vh.vh_oui[1] = 0x03;
378 sc->sc_rx_th.wr_vh.vh_oui[2] = 0x00;
380 /* XXX what should this be? */
381 sc->sc_rx_th.wr_vh.vh_sub_ns = 0;
382 sc->sc_rx_th.wr_vh.vh_skip_len =
383 htole16(sizeof(struct ath_radiotap_vendor_hdr));
385 /* General version info */
386 sc->sc_rx_th.wr_v.vh_version = 1;
388 sc->sc_rx_th.wr_v.vh_rx_chainmask = sc->sc_rxchainmask;
391 sc->sc_rx_th.wr_v.rssi_ctl[0] = rs->rs_rssi_ctl[0];
392 sc->sc_rx_th.wr_v.rssi_ctl[1] = rs->rs_rssi_ctl[1];
393 sc->sc_rx_th.wr_v.rssi_ctl[2] = rs->rs_rssi_ctl[2];
394 sc->sc_rx_th.wr_v.rssi_ext[0] = rs->rs_rssi_ext[0];
395 sc->sc_rx_th.wr_v.rssi_ext[1] = rs->rs_rssi_ext[1];
396 sc->sc_rx_th.wr_v.rssi_ext[2] = rs->rs_rssi_ext[2];
399 sc->sc_rx_th.wr_v.evm[0] = rs->rs_evm0;
400 sc->sc_rx_th.wr_v.evm[1] = rs->rs_evm1;
401 sc->sc_rx_th.wr_v.evm[2] = rs->rs_evm2;
402 /* These are only populated from the AR9300 or later */
403 sc->sc_rx_th.wr_v.evm[3] = rs->rs_evm3;
404 sc->sc_rx_th.wr_v.evm[4] = rs->rs_evm4;
407 sc->sc_rx_th.wr_v.vh_flags = ATH_VENDOR_PKT_RX;
410 sc->sc_rx_th.wr_v.vh_rx_hwrate = rs->rs_rate;
413 sc->sc_rx_th.wr_v.vh_rs_flags = rs->rs_flags;
416 sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_ISAGGR;
418 sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_MOREAGGR;
421 if (rs->rs_status & HAL_RXERR_PHY) {
422 sc->sc_rx_th.wr_v.vh_phyerr_code = rs->rs_phyerr;
423 sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_RXPHYERR;
425 sc->sc_rx_th.wr_v.vh_phyerr_code = 0xff;
427 sc->sc_rx_th.wr_v.vh_rs_status = rs->rs_status;
428 sc->sc_rx_th.wr_v.vh_rssi = rs->rs_rssi;
430 #endif /* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
433 ath_rx_tap(struct ifnet *ifp, struct mbuf *m,
434 const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
436 #define CHAN_HT20 htole32(IEEE80211_CHAN_HT20)
437 #define CHAN_HT40U htole32(IEEE80211_CHAN_HT40U)
438 #define CHAN_HT40D htole32(IEEE80211_CHAN_HT40D)
439 #define CHAN_HT (CHAN_HT20|CHAN_HT40U|CHAN_HT40D)
440 struct ath_softc *sc = ifp->if_softc;
441 const HAL_RATE_TABLE *rt;
444 rt = sc->sc_currates;
445 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
446 rix = rt->rateCodeToIndex[rs->rs_rate];
447 sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
448 sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
449 #ifdef AH_SUPPORT_AR5416
450 sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT;
451 if (rs->rs_status & HAL_RXERR_PHY) {
453 * PHY error - make sure the channel flags
454 * reflect the actual channel configuration,
455 * not the received frame.
457 if (IEEE80211_IS_CHAN_HT40U(sc->sc_curchan))
458 sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
459 else if (IEEE80211_IS_CHAN_HT40D(sc->sc_curchan))
460 sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
461 else if (IEEE80211_IS_CHAN_HT20(sc->sc_curchan))
462 sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
463 } else if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) { /* HT rate */
464 struct ieee80211com *ic = ifp->if_l2com;
466 if ((rs->rs_flags & HAL_RX_2040) == 0)
467 sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
468 else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan))
469 sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
471 sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
472 if ((rs->rs_flags & HAL_RX_GI) == 0)
473 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
477 sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(sc, rs->rs_tstamp, tsf));
478 if (rs->rs_status & HAL_RXERR_CRC)
479 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
480 /* XXX propagate other error flags from descriptor */
481 sc->sc_rx_th.wr_antnoise = nf;
482 sc->sc_rx_th.wr_antsignal = nf + rs->rs_rssi;
483 sc->sc_rx_th.wr_antenna = rs->rs_antenna;
491 ath_handle_micerror(struct ieee80211com *ic,
492 struct ieee80211_frame *wh, int keyix)
494 struct ieee80211_node *ni;
496 /* XXX recheck MIC to deal w/ chips that lie */
497 /* XXX discard MIC errors on !data frames */
498 ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
500 ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix);
501 ieee80211_free_node(ni);
506 * Process a single packet.
508 * The mbuf must already be synced, unmapped and removed from bf->bf_m
511 * The mbuf must be consumed by this routine - either passed up the
512 * net80211 stack, put on the holding queue, or freed.
515 ath_rx_pkt(struct ath_softc *sc, struct ath_rx_status *rs, HAL_STATUS status,
516 uint64_t tsf, int nf, HAL_RX_QUEUE qtype, struct ath_buf *bf,
519 struct ath_hal *ah = sc->sc_ah;
522 struct ifnet *ifp = sc->sc_ifp;
523 struct ieee80211com *ic = ifp->if_l2com;
524 struct ieee80211_node *ni;
526 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
529 * Calculate the correct 64 bit TSF given
530 * the TSF64 register value and rs_tstamp.
532 rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
534 /* These aren't specifically errors */
535 #ifdef AH_SUPPORT_AR5416
536 if (rs->rs_flags & HAL_RX_GI)
537 sc->sc_stats.ast_rx_halfgi++;
538 if (rs->rs_flags & HAL_RX_2040)
539 sc->sc_stats.ast_rx_2040++;
540 if (rs->rs_flags & HAL_RX_DELIM_CRC_PRE)
541 sc->sc_stats.ast_rx_pre_crc_err++;
542 if (rs->rs_flags & HAL_RX_DELIM_CRC_POST)
543 sc->sc_stats.ast_rx_post_crc_err++;
544 if (rs->rs_flags & HAL_RX_DECRYPT_BUSY)
545 sc->sc_stats.ast_rx_decrypt_busy_err++;
546 if (rs->rs_flags & HAL_RX_HI_RX_CHAIN)
547 sc->sc_stats.ast_rx_hi_rx_chain++;
548 if (rs->rs_flags & HAL_RX_STBC)
549 sc->sc_stats.ast_rx_stbc++;
550 #endif /* AH_SUPPORT_AR5416 */
552 if (rs->rs_status != 0) {
553 if (rs->rs_status & HAL_RXERR_CRC)
554 sc->sc_stats.ast_rx_crcerr++;
555 if (rs->rs_status & HAL_RXERR_FIFO)
556 sc->sc_stats.ast_rx_fifoerr++;
557 if (rs->rs_status & HAL_RXERR_PHY) {
558 sc->sc_stats.ast_rx_phyerr++;
559 /* Process DFS radar events */
560 if ((rs->rs_phyerr == HAL_PHYERR_RADAR) ||
561 (rs->rs_phyerr == HAL_PHYERR_FALSE_RADAR_EXT)) {
562 /* Now pass it to the radar processing code */
563 ath_dfs_process_phy_err(sc, m, rstamp, rs);
566 /* Be suitably paranoid about receiving phy errors out of the stats array bounds */
567 if (rs->rs_phyerr < 64)
568 sc->sc_stats.ast_rx_phy[rs->rs_phyerr]++;
569 goto rx_error; /* NB: don't count in ierrors */
571 if (rs->rs_status & HAL_RXERR_DECRYPT) {
573 * Decrypt error. If the error occurred
574 * because there was no hardware key, then
575 * let the frame through so the upper layers
576 * can process it. This is necessary for 5210
577 * parts which have no way to setup a ``clear''
580 * XXX do key cache faulting
582 if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
584 sc->sc_stats.ast_rx_badcrypt++;
587 * Similar as above - if the failure was a keymiss
588 * just punt it up to the upper layers for now.
590 if (rs->rs_status & HAL_RXERR_KEYMISS) {
591 sc->sc_stats.ast_rx_keymiss++;
594 if (rs->rs_status & HAL_RXERR_MIC) {
595 sc->sc_stats.ast_rx_badmic++;
597 * Do minimal work required to hand off
598 * the 802.11 header for notification.
600 /* XXX frag's and qos frames */
601 len = rs->rs_datalen;
602 if (len >= sizeof (struct ieee80211_frame)) {
603 ath_handle_micerror(ic,
604 mtod(m, struct ieee80211_frame *),
606 rs->rs_keyix-32 : rs->rs_keyix);
612 * Cleanup any pending partial frame.
614 if (re->m_rxpending != NULL) {
615 m_freem(re->m_rxpending);
616 re->m_rxpending = NULL;
619 * When a tap is present pass error frames
620 * that have been requested. By default we
621 * pass decrypt+mic errors but others may be
622 * interesting (e.g. crc).
624 if (ieee80211_radiotap_active(ic) &&
625 (rs->rs_status & sc->sc_monpass)) {
626 /* NB: bpf needs the mbuf length setup */
627 len = rs->rs_datalen;
628 m->m_pkthdr.len = m->m_len = len;
629 ath_rx_tap(ifp, m, rs, rstamp, nf);
630 #ifdef ATH_ENABLE_RADIOTAP_VENDOR_EXT
631 ath_rx_tap_vendor(ifp, m, rs, rstamp, nf);
632 #endif /* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
633 ieee80211_radiotap_rx_all(ic, m);
635 /* XXX pass MIC errors up for s/w reclaculation */
636 m_freem(m); m = NULL;
640 len = rs->rs_datalen;
645 * Frame spans multiple descriptors; save
646 * it for the next completed descriptor, it
647 * will be used to construct a jumbogram.
649 if (re->m_rxpending != NULL) {
650 /* NB: max frame size is currently 2 clusters */
651 sc->sc_stats.ast_rx_toobig++;
652 m_freem(re->m_rxpending);
654 m->m_pkthdr.rcvif = ifp;
655 m->m_pkthdr.len = len;
659 } else if (re->m_rxpending != NULL) {
661 * This is the second part of a jumbogram,
662 * chain it to the first mbuf, adjust the
663 * frame length, and clear the rxpending state.
665 re->m_rxpending->m_next = m;
666 re->m_rxpending->m_pkthdr.len += len;
668 re->m_rxpending = NULL;
671 * Normal single-descriptor receive; setup
672 * the rcvif and packet length.
674 m->m_pkthdr.rcvif = ifp;
675 m->m_pkthdr.len = len;
679 * Validate rs->rs_antenna.
681 * Some users w/ AR9285 NICs have reported crashes
682 * here because rs_antenna field is bogusly large.
683 * Let's enforce the maximum antenna limit of 8
684 * (and it shouldn't be hard coded, but that's a
685 * separate problem) and if there's an issue, print
686 * out an error and adjust rs_antenna to something
689 * This code should be removed once the actual
690 * root cause of the issue has been identified.
691 * For example, it may be that the rs_antenna
692 * field is only valid for the lsat frame of
693 * an aggregate and it just happens that it is
694 * "mostly" right. (This is a general statement -
695 * the majority of the statistics are only valid
696 * for the last frame in an aggregate.
698 if (rs->rs_antenna > 7) {
699 device_printf(sc->sc_dev, "%s: rs_antenna > 7 (%d)\n",
700 __func__, rs->rs_antenna);
702 ath_printrxbuf(sc, bf, 0, status == HAL_OK);
703 #endif /* ATH_DEBUG */
704 rs->rs_antenna = 0; /* XXX better than nothing */
708 * If this is an AR9285/AR9485, then the receive and LNA
709 * configuration is stored in RSSI[2] / EXTRSSI[2].
710 * We can extract this out to build a much better
711 * receive antenna profile.
713 * Yes, this just blurts over the above RX antenna field
714 * for now. It's fine, the AR9285 doesn't really use
717 * Later on we should store away the fine grained LNA
718 * information and keep separate counters just for
719 * that. It'll help when debugging the AR9285/AR9485
720 * combined diversity code.
722 if (sc->sc_rx_lnamixer) {
725 /* Bits 0:1 - the LNA configuration used */
727 ((rs->rs_rssi_ctl[2] & HAL_RX_LNA_CFG_USED)
728 >> HAL_RX_LNA_CFG_USED_S);
730 /* Bit 2 - the external RX antenna switch */
731 if (rs->rs_rssi_ctl[2] & HAL_RX_LNA_EXTCFG)
732 rs->rs_antenna |= 0x4;
736 sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
739 * Populate the rx status block. When there are bpf
740 * listeners we do the additional work to provide
741 * complete status. Otherwise we fill in only the
742 * material required by ieee80211_input. Note that
743 * noise setting is filled in above.
745 if (ieee80211_radiotap_active(ic)) {
746 ath_rx_tap(ifp, m, rs, rstamp, nf);
747 #ifdef ATH_ENABLE_RADIOTAP_VENDOR_EXT
748 ath_rx_tap_vendor(ifp, m, rs, rstamp, nf);
749 #endif /* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
753 * From this point on we assume the frame is at least
754 * as large as ieee80211_frame_min; verify that.
756 if (len < IEEE80211_MIN_LEN) {
757 if (!ieee80211_radiotap_active(ic)) {
758 DPRINTF(sc, ATH_DEBUG_RECV,
759 "%s: short packet %d\n", __func__, len);
760 sc->sc_stats.ast_rx_tooshort++;
762 /* NB: in particular this captures ack's */
763 ieee80211_radiotap_rx_all(ic, m);
765 m_freem(m); m = NULL;
769 if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
770 const HAL_RATE_TABLE *rt = sc->sc_currates;
771 uint8_t rix = rt->rateCodeToIndex[rs->rs_rate];
773 ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
774 sc->sc_hwmap[rix].ieeerate, rs->rs_rssi);
777 m_adj(m, -IEEE80211_CRC_LEN);
780 * Locate the node for sender, track state, and then
781 * pass the (referenced) node up to the 802.11 layer
784 ni = ieee80211_find_rxnode_withkey(ic,
785 mtod(m, const struct ieee80211_frame_min *),
786 rs->rs_keyix == HAL_RXKEYIX_INVALID ?
787 IEEE80211_KEYIX_NONE : rs->rs_keyix);
790 #ifdef AH_SUPPORT_AR5416
792 sc->sc_stats.ast_rx_agg++;
793 #endif /* AH_SUPPORT_AR5416 */
797 * Only punt packets for ampdu reorder processing for
798 * 11n nodes; net80211 enforces that M_AMPDU is only
801 if (ni->ni_flags & IEEE80211_NODE_HT)
802 m->m_flags |= M_AMPDU;
805 * Sending station is known, dispatch directly.
807 type = ieee80211_input(ni, m, rs->rs_rssi, nf);
808 ieee80211_free_node(ni);
811 * Arrange to update the last rx timestamp only for
812 * frames from our ap when operating in station mode.
813 * This assumes the rx key is always setup when
816 if (ic->ic_opmode == IEEE80211_M_STA &&
817 rs->rs_keyix != HAL_RXKEYIX_INVALID)
820 type = ieee80211_input_all(ic, m, rs->rs_rssi, nf);
825 * At this point we have passed the frame up the stack; thus
826 * the mbuf is no longer ours.
830 * Track rx rssi and do any rx antenna management.
832 ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
833 if (sc->sc_diversity) {
835 * When using fast diversity, change the default rx
836 * antenna if diversity chooses the other antenna 3
839 if (sc->sc_defant != rs->rs_antenna) {
840 if (++sc->sc_rxotherant >= 3)
841 ath_setdefantenna(sc, rs->rs_antenna);
843 sc->sc_rxotherant = 0;
846 /* Newer school diversity - kite specific for now */
847 /* XXX perhaps migrate the normal diversity code to this? */
848 if ((ah)->ah_rxAntCombDiversity)
849 (*(ah)->ah_rxAntCombDiversity)(ah, rs, ticks, hz);
851 if (sc->sc_softled) {
853 * Blink for any data frame. Otherwise do a
854 * heartbeat-style blink when idle. The latter
855 * is mainly for station mode where we depend on
856 * periodic beacon frames to trigger the poll event.
858 if (type == IEEE80211_FC0_TYPE_DATA) {
859 const HAL_RATE_TABLE *rt = sc->sc_currates;
861 rt->rateCodeToIndex[rs->rs_rate]);
862 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
863 ath_led_event(sc, 0);
867 * Debugging - complain if we didn't NULL the mbuf pointer
871 device_printf(sc->sc_dev,
872 "%s: mbuf %p should've been freed!\n",
879 #define ATH_RX_MAX 128
882 ath_rx_proc(struct ath_softc *sc, int resched)
884 #define PA2DESC(_sc, _pa) \
885 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
886 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
888 struct ifnet *ifp = sc->sc_ifp;
889 struct ath_hal *ah = sc->sc_ah;
890 #ifdef IEEE80211_SUPPORT_SUPERG
891 struct ieee80211com *ic = ifp->if_l2com;
894 struct ath_rx_status *rs;
903 /* XXX we must not hold the ATH_LOCK here */
904 ATH_UNLOCK_ASSERT(sc);
905 ATH_PCU_UNLOCK_ASSERT(sc);
909 kickpcu = sc->sc_kickpcu;
912 DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: called\n", __func__);
914 nf = ath_hal_getchannoise(ah, sc->sc_curchan);
915 sc->sc_stats.ast_rx_noise = nf;
916 tsf = ath_hal_gettsf64(ah);
919 * Don't process too many packets at a time; give the
920 * TX thread time to also run - otherwise the TX
921 * latency can jump by quite a bit, causing throughput
924 if (!kickpcu && npkts >= ATH_RX_MAX)
927 bf = TAILQ_FIRST(&sc->sc_rxbuf);
928 if (sc->sc_rxslink && bf == NULL) { /* NB: shouldn't happen */
929 if_printf(ifp, "%s: no buffer!\n", __func__);
931 } else if (bf == NULL) {
934 * this can happen for non-self-linked RX chains
936 sc->sc_stats.ast_rx_hitqueueend++;
940 if (m == NULL) { /* NB: shouldn't happen */
942 * If mbuf allocation failed previously there
943 * will be no mbuf; try again to re-populate it.
945 /* XXX make debug msg */
946 if_printf(ifp, "%s: no mbuf!\n", __func__);
947 TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
951 if (ds->ds_link == bf->bf_daddr) {
952 /* NB: never process the self-linked entry at the end */
953 sc->sc_stats.ast_rx_hitqueueend++;
956 /* XXX sync descriptor memory */
958 * Must provide the virtual address of the current
959 * descriptor, the physical address, and the virtual
960 * address of the next descriptor in the h/w chain.
961 * This allows the HAL to look ahead to see if the
962 * hardware is done with a descriptor by checking the
963 * done bit in the following descriptor and the address
964 * of the current descriptor the DMA engine is working
965 * on. All this is necessary because of our use of
966 * a self-linked list to avoid rx overruns.
968 rs = &bf->bf_status.ds_rxstat;
969 status = ath_hal_rxprocdesc(ah, ds,
970 bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
972 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
973 ath_printrxbuf(sc, bf, 0, status == HAL_OK);
977 if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS))
978 if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS,
979 sc->sc_rx_statuslen, (char *) ds);
980 #endif /* ATH_DEBUG_ALQ */
982 if (status == HAL_EINPROGRESS)
985 TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
989 * Process a single frame.
991 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_POSTREAD);
992 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
994 if (ath_rx_pkt(sc, rs, status, tsf, nf, HAL_RX_QUEUE_HP, bf, m))
997 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
998 } while (ath_rxbuf_init(sc, bf) == 0);
1000 /* rx signal state monitoring */
1001 ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
1003 sc->sc_lastrx = tsf;
1005 ATH_KTR(sc, ATH_KTR_RXPROC, 2, "ath_rx_proc: npkts=%d, ngood=%d", npkts, ngood);
1006 /* Queue DFS tasklet if needed */
1007 if (resched && ath_dfs_tasklet_needed(sc, sc->sc_curchan))
1008 taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
1011 * Now that all the RX frames were handled that
1012 * need to be handled, kick the PCU if there's
1013 * been an RXEOL condition.
1015 if (resched && kickpcu) {
1017 ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_rx_proc: kickpcu");
1018 device_printf(sc->sc_dev, "%s: kickpcu; handled %d packets\n",
1022 * Go through the process of fully tearing down
1023 * the RX buffers and reinitialising them.
1025 * There's a hardware bug that causes the RX FIFO
1026 * to get confused under certain conditions and
1027 * constantly write over the same frame, leading
1028 * the RX driver code here to get heavily confused.
1034 * Disabled for now - it'd be nice to be able to do
1035 * this in order to limit the amount of CPU time spent
1036 * reinitialising the RX side (and thus minimise RX
1037 * drops) however there's a hardware issue that
1038 * causes things to get too far out of whack.
1041 * XXX can we hold the PCU lock here?
1042 * Are there any net80211 buffer calls involved?
1044 bf = TAILQ_FIRST(&sc->sc_rxbuf);
1045 ath_hal_putrxbuf(ah, bf->bf_daddr, HAL_RX_QUEUE_HP);
1046 ath_hal_rxena(ah); /* enable recv descriptors */
1047 ath_mode_init(sc); /* set filters, etc. */
1048 ath_hal_startpcurecv(ah); /* re-enable PCU/DMA engine */
1051 ath_hal_intrset(ah, sc->sc_imask);
1056 /* XXX check this inside of IF_LOCK? */
1057 if (resched && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
1058 #ifdef IEEE80211_SUPPORT_SUPERG
1059 ieee80211_ff_age_all(ic, 100);
1061 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1067 * If we hit the maximum number of frames in this round,
1068 * reschedule for another immediate pass. This gives
1069 * the TX and TX completion routines time to run, which
1070 * will reduce latency.
1072 if (npkts >= ATH_RX_MAX)
1073 sc->sc_rx.recv_sched(sc, resched);
1076 sc->sc_rxproc_cnt--;
1083 * Only run the RX proc if it's not already running.
1084 * Since this may get run as part of the reset/flush path,
1085 * the task can't clash with an existing, running tasklet.
1088 ath_legacy_rx_tasklet(void *arg, int npending)
1090 struct ath_softc *sc = arg;
1092 ATH_KTR(sc, ATH_KTR_RXPROC, 1, "ath_rx_proc: pending=%d", npending);
1093 DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
1095 if (sc->sc_inreset_cnt > 0) {
1096 device_printf(sc->sc_dev,
1097 "%s: sc_inreset_cnt > 0; skipping\n", __func__);
1107 ath_legacy_flushrecv(struct ath_softc *sc)
1114 * Disable the receive h/w in preparation for a reset.
1117 ath_legacy_stoprecv(struct ath_softc *sc, int dodelay)
1119 #define PA2DESC(_sc, _pa) \
1120 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
1121 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
1122 struct ath_hal *ah = sc->sc_ah;
1124 ath_hal_stoppcurecv(ah); /* disable PCU */
1125 ath_hal_setrxfilter(ah, 0); /* clear recv filter */
1126 ath_hal_stopdmarecv(ah); /* disable DMA engine */
1128 * TODO: see if this particular DELAY() is required; it may be
1129 * masking some missing FIFO flush or DMA sync.
1134 DELAY(3000); /* 3ms is long enough for 1 frame */
1136 if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
1140 device_printf(sc->sc_dev,
1141 "%s: rx queue %p, link %p\n",
1143 (caddr_t)(uintptr_t) ath_hal_getrxbuf(ah, HAL_RX_QUEUE_HP),
1146 TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
1147 struct ath_desc *ds = bf->bf_desc;
1148 struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
1149 HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
1150 bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
1151 if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
1152 ath_printrxbuf(sc, bf, ix, status == HAL_OK);
1158 * Free both high/low RX pending, just in case.
1160 if (sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending != NULL) {
1161 m_freem(sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending);
1162 sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending = NULL;
1164 if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending != NULL) {
1165 m_freem(sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending);
1166 sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending = NULL;
1168 sc->sc_rxlink = NULL; /* just in case */
1173 * Enable the receive h/w following a reset.
1176 ath_legacy_startrecv(struct ath_softc *sc)
1178 struct ath_hal *ah = sc->sc_ah;
1181 sc->sc_rxlink = NULL;
1182 sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending = NULL;
1183 sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending = NULL;
1184 TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
1185 int error = ath_rxbuf_init(sc, bf);
1187 DPRINTF(sc, ATH_DEBUG_RECV,
1188 "%s: ath_rxbuf_init failed %d\n",
1194 bf = TAILQ_FIRST(&sc->sc_rxbuf);
1195 ath_hal_putrxbuf(ah, bf->bf_daddr, HAL_RX_QUEUE_HP);
1196 ath_hal_rxena(ah); /* enable recv descriptors */
1197 ath_mode_init(sc); /* set filters, etc. */
1198 ath_hal_startpcurecv(ah); /* re-enable PCU/DMA engine */
1203 ath_legacy_dma_rxsetup(struct ath_softc *sc)
1207 error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
1208 "rx", sizeof(struct ath_desc), ath_rxbuf, 1);
1216 ath_legacy_dma_rxteardown(struct ath_softc *sc)
1219 if (sc->sc_rxdma.dd_desc_len != 0)
1220 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
1225 ath_legacy_recv_sched(struct ath_softc *sc, int dosched)
1228 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1232 ath_legacy_recv_sched_queue(struct ath_softc *sc, HAL_RX_QUEUE q,
1236 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1240 ath_recv_setup_legacy(struct ath_softc *sc)
1243 /* Sensible legacy defaults */
1245 * XXX this should be changed to properly support the
1246 * exact RX descriptor size for each HAL.
1248 sc->sc_rx_statuslen = sizeof(struct ath_desc);
1250 sc->sc_rx.recv_start = ath_legacy_startrecv;
1251 sc->sc_rx.recv_stop = ath_legacy_stoprecv;
1252 sc->sc_rx.recv_flush = ath_legacy_flushrecv;
1253 sc->sc_rx.recv_tasklet = ath_legacy_rx_tasklet;
1254 sc->sc_rx.recv_rxbuf_init = ath_legacy_rxbuf_init;
1256 sc->sc_rx.recv_setup = ath_legacy_dma_rxsetup;
1257 sc->sc_rx.recv_teardown = ath_legacy_dma_rxteardown;
1258 sc->sc_rx.recv_sched = ath_legacy_recv_sched;
1259 sc->sc_rx.recv_sched_queue = ath_legacy_recv_sched_queue;