xref: /freebsd/sys/dev/ath/if_ath_rx.c (revision d542f7f6867d9d9e080eafaf7991667d0b3d5c36)
1e60c4fc2SAdrian Chadd /*-
2e60c4fc2SAdrian Chadd  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3e60c4fc2SAdrian Chadd  * All rights reserved.
4e60c4fc2SAdrian Chadd  *
5e60c4fc2SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
6e60c4fc2SAdrian Chadd  * modification, are permitted provided that the following conditions
7e60c4fc2SAdrian Chadd  * are met:
8e60c4fc2SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
9e60c4fc2SAdrian Chadd  *    notice, this list of conditions and the following disclaimer,
10e60c4fc2SAdrian Chadd  *    without modification.
11e60c4fc2SAdrian Chadd  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12e60c4fc2SAdrian Chadd  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13e60c4fc2SAdrian Chadd  *    redistribution must be conditioned upon including a substantially
14e60c4fc2SAdrian Chadd  *    similar Disclaimer requirement for further binary redistribution.
15e60c4fc2SAdrian Chadd  *
16e60c4fc2SAdrian Chadd  * NO WARRANTY
17e60c4fc2SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18e60c4fc2SAdrian Chadd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19e60c4fc2SAdrian Chadd  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20e60c4fc2SAdrian Chadd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21e60c4fc2SAdrian Chadd  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22e60c4fc2SAdrian Chadd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23e60c4fc2SAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24e60c4fc2SAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25e60c4fc2SAdrian Chadd  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26e60c4fc2SAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27e60c4fc2SAdrian Chadd  * THE POSSIBILITY OF SUCH DAMAGES.
28e60c4fc2SAdrian Chadd  */
29e60c4fc2SAdrian Chadd 
30e60c4fc2SAdrian Chadd #include <sys/cdefs.h>
31e60c4fc2SAdrian Chadd __FBSDID("$FreeBSD$");
32e60c4fc2SAdrian Chadd 
33e60c4fc2SAdrian Chadd /*
34e60c4fc2SAdrian Chadd  * Driver for the Atheros Wireless LAN controller.
35e60c4fc2SAdrian Chadd  *
36e60c4fc2SAdrian Chadd  * This software is derived from work of Atsushi Onoe; his contribution
37e60c4fc2SAdrian Chadd  * is greatly appreciated.
38e60c4fc2SAdrian Chadd  */
39e60c4fc2SAdrian Chadd 
40e60c4fc2SAdrian Chadd #include "opt_inet.h"
41e60c4fc2SAdrian Chadd #include "opt_ath.h"
42e60c4fc2SAdrian Chadd /*
43e60c4fc2SAdrian Chadd  * This is needed for register operations which are performed
44e60c4fc2SAdrian Chadd  * by the driver - eg, calls to ath_hal_gettsf32().
45e60c4fc2SAdrian Chadd  *
46e60c4fc2SAdrian Chadd  * It's also required for any AH_DEBUG checks in here, eg the
47e60c4fc2SAdrian Chadd  * module dependencies.
48e60c4fc2SAdrian Chadd  */
49e60c4fc2SAdrian Chadd #include "opt_ah.h"
50e60c4fc2SAdrian Chadd #include "opt_wlan.h"
51e60c4fc2SAdrian Chadd 
52e60c4fc2SAdrian Chadd #include <sys/param.h>
53e60c4fc2SAdrian Chadd #include <sys/systm.h>
54e60c4fc2SAdrian Chadd #include <sys/sysctl.h>
55e60c4fc2SAdrian Chadd #include <sys/mbuf.h>
56e60c4fc2SAdrian Chadd #include <sys/malloc.h>
57e60c4fc2SAdrian Chadd #include <sys/lock.h>
58e60c4fc2SAdrian Chadd #include <sys/mutex.h>
59e60c4fc2SAdrian Chadd #include <sys/kernel.h>
60e60c4fc2SAdrian Chadd #include <sys/socket.h>
61e60c4fc2SAdrian Chadd #include <sys/sockio.h>
62e60c4fc2SAdrian Chadd #include <sys/errno.h>
63e60c4fc2SAdrian Chadd #include <sys/callout.h>
64e60c4fc2SAdrian Chadd #include <sys/bus.h>
65e60c4fc2SAdrian Chadd #include <sys/endian.h>
66e60c4fc2SAdrian Chadd #include <sys/kthread.h>
67e60c4fc2SAdrian Chadd #include <sys/taskqueue.h>
68e60c4fc2SAdrian Chadd #include <sys/priv.h>
69e60c4fc2SAdrian Chadd #include <sys/module.h>
70e60c4fc2SAdrian Chadd #include <sys/ktr.h>
71e60c4fc2SAdrian Chadd #include <sys/smp.h>	/* for mp_ncpus */
72e60c4fc2SAdrian Chadd 
73e60c4fc2SAdrian Chadd #include <machine/bus.h>
74e60c4fc2SAdrian Chadd 
75e60c4fc2SAdrian Chadd #include <net/if.h>
76e60c4fc2SAdrian Chadd #include <net/if_dl.h>
77e60c4fc2SAdrian Chadd #include <net/if_media.h>
78e60c4fc2SAdrian Chadd #include <net/if_types.h>
79e60c4fc2SAdrian Chadd #include <net/if_arp.h>
80e60c4fc2SAdrian Chadd #include <net/ethernet.h>
81e60c4fc2SAdrian Chadd #include <net/if_llc.h>
82e60c4fc2SAdrian Chadd 
83e60c4fc2SAdrian Chadd #include <net80211/ieee80211_var.h>
84e60c4fc2SAdrian Chadd #include <net80211/ieee80211_regdomain.h>
85e60c4fc2SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
86e60c4fc2SAdrian Chadd #include <net80211/ieee80211_superg.h>
87e60c4fc2SAdrian Chadd #endif
88e60c4fc2SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
89e60c4fc2SAdrian Chadd #include <net80211/ieee80211_tdma.h>
90e60c4fc2SAdrian Chadd #endif
91e60c4fc2SAdrian Chadd 
92e60c4fc2SAdrian Chadd #include <net/bpf.h>
93e60c4fc2SAdrian Chadd 
94e60c4fc2SAdrian Chadd #ifdef INET
95e60c4fc2SAdrian Chadd #include <netinet/in.h>
96e60c4fc2SAdrian Chadd #include <netinet/if_ether.h>
97e60c4fc2SAdrian Chadd #endif
98e60c4fc2SAdrian Chadd 
99e60c4fc2SAdrian Chadd #include <dev/ath/if_athvar.h>
100e60c4fc2SAdrian Chadd #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
101e60c4fc2SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
102e60c4fc2SAdrian Chadd 
103e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_debug.h>
104e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_misc.h>
105e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_tsf.h>
106e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_tx.h>
107e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_sysctl.h>
108e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_led.h>
109e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_keycache.h>
110e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_rx.h>
111a35dae8dSAdrian Chadd #include <dev/ath/if_ath_beacon.h>
112e60c4fc2SAdrian Chadd #include <dev/ath/if_athdfs.h>
113e60c4fc2SAdrian Chadd 
114e60c4fc2SAdrian Chadd #ifdef ATH_TX99_DIAG
115e60c4fc2SAdrian Chadd #include <dev/ath/ath_tx99/ath_tx99.h>
116e60c4fc2SAdrian Chadd #endif
117e60c4fc2SAdrian Chadd 
118e60c4fc2SAdrian Chadd #define	ATH_KTR_INTR	KTR_SPARE4
119e60c4fc2SAdrian Chadd #define	ATH_KTR_ERR	KTR_SPARE3
120e60c4fc2SAdrian Chadd 
121e60c4fc2SAdrian Chadd /*
122e60c4fc2SAdrian Chadd  * Calculate the receive filter according to the
123e60c4fc2SAdrian Chadd  * operating mode and state:
124e60c4fc2SAdrian Chadd  *
125e60c4fc2SAdrian Chadd  * o always accept unicast, broadcast, and multicast traffic
126e60c4fc2SAdrian Chadd  * o accept PHY error frames when hardware doesn't have MIB support
127e60c4fc2SAdrian Chadd  *   to count and we need them for ANI (sta mode only until recently)
128e60c4fc2SAdrian Chadd  *   and we are not scanning (ANI is disabled)
129e60c4fc2SAdrian Chadd  *   NB: older hal's add rx filter bits out of sight and we need to
130e60c4fc2SAdrian Chadd  *	 blindly preserve them
131e60c4fc2SAdrian Chadd  * o probe request frames are accepted only when operating in
132e60c4fc2SAdrian Chadd  *   hostap, adhoc, mesh, or monitor modes
133e60c4fc2SAdrian Chadd  * o enable promiscuous mode
134e60c4fc2SAdrian Chadd  *   - when in monitor mode
135e60c4fc2SAdrian Chadd  *   - if interface marked PROMISC (assumes bridge setting is filtered)
136e60c4fc2SAdrian Chadd  * o accept beacons:
137e60c4fc2SAdrian Chadd  *   - when operating in station mode for collecting rssi data when
138e60c4fc2SAdrian Chadd  *     the station is otherwise quiet, or
139e60c4fc2SAdrian Chadd  *   - when operating in adhoc mode so the 802.11 layer creates
140e60c4fc2SAdrian Chadd  *     node table entries for peers,
141e60c4fc2SAdrian Chadd  *   - when scanning
142e60c4fc2SAdrian Chadd  *   - when doing s/w beacon miss (e.g. for ap+sta)
143e60c4fc2SAdrian Chadd  *   - when operating in ap mode in 11g to detect overlapping bss that
144e60c4fc2SAdrian Chadd  *     require protection
145e60c4fc2SAdrian Chadd  *   - when operating in mesh mode to detect neighbors
146e60c4fc2SAdrian Chadd  * o accept control frames:
147e60c4fc2SAdrian Chadd  *   - when in monitor mode
148e60c4fc2SAdrian Chadd  * XXX HT protection for 11n
149e60c4fc2SAdrian Chadd  */
150e60c4fc2SAdrian Chadd u_int32_t
151e60c4fc2SAdrian Chadd ath_calcrxfilter(struct ath_softc *sc)
152e60c4fc2SAdrian Chadd {
153e60c4fc2SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
154e60c4fc2SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
155e60c4fc2SAdrian Chadd 	u_int32_t rfilt;
156e60c4fc2SAdrian Chadd 
157e60c4fc2SAdrian Chadd 	rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
158e60c4fc2SAdrian Chadd 	if (!sc->sc_needmib && !sc->sc_scanning)
159e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PHYERR;
160e60c4fc2SAdrian Chadd 	if (ic->ic_opmode != IEEE80211_M_STA)
161e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PROBEREQ;
162e60c4fc2SAdrian Chadd 	/* XXX ic->ic_monvaps != 0? */
163e60c4fc2SAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_MONITOR || (ifp->if_flags & IFF_PROMISC))
164e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PROM;
165e60c4fc2SAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_STA ||
166e60c4fc2SAdrian Chadd 	    ic->ic_opmode == IEEE80211_M_IBSS ||
167e60c4fc2SAdrian Chadd 	    sc->sc_swbmiss || sc->sc_scanning)
168e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_BEACON;
169e60c4fc2SAdrian Chadd 	/*
170e60c4fc2SAdrian Chadd 	 * NB: We don't recalculate the rx filter when
171e60c4fc2SAdrian Chadd 	 * ic_protmode changes; otherwise we could do
172e60c4fc2SAdrian Chadd 	 * this only when ic_protmode != NONE.
173e60c4fc2SAdrian Chadd 	 */
174e60c4fc2SAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
175e60c4fc2SAdrian Chadd 	    IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
176e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_BEACON;
177e60c4fc2SAdrian Chadd 
178e60c4fc2SAdrian Chadd 	/*
179e60c4fc2SAdrian Chadd 	 * Enable hardware PS-POLL RX only for hostap mode;
180e60c4fc2SAdrian Chadd 	 * STA mode sends PS-POLL frames but never
181e60c4fc2SAdrian Chadd 	 * receives them.
182e60c4fc2SAdrian Chadd 	 */
183e60c4fc2SAdrian Chadd 	if (ath_hal_getcapability(sc->sc_ah, HAL_CAP_PSPOLL,
184e60c4fc2SAdrian Chadd 	    0, NULL) == HAL_OK &&
185e60c4fc2SAdrian Chadd 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
186e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PSPOLL;
187e60c4fc2SAdrian Chadd 
188e60c4fc2SAdrian Chadd 	if (sc->sc_nmeshvaps) {
189e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_BEACON;
190e60c4fc2SAdrian Chadd 		if (sc->sc_hasbmatch)
191e60c4fc2SAdrian Chadd 			rfilt |= HAL_RX_FILTER_BSSID;
192e60c4fc2SAdrian Chadd 		else
193e60c4fc2SAdrian Chadd 			rfilt |= HAL_RX_FILTER_PROM;
194e60c4fc2SAdrian Chadd 	}
195e60c4fc2SAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
196e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_CONTROL;
197e60c4fc2SAdrian Chadd 
198e60c4fc2SAdrian Chadd 	/*
199e60c4fc2SAdrian Chadd 	 * Enable RX of compressed BAR frames only when doing
200e60c4fc2SAdrian Chadd 	 * 802.11n. Required for A-MPDU.
201e60c4fc2SAdrian Chadd 	 */
202e60c4fc2SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(ic->ic_curchan))
203e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_COMPBAR;
204e60c4fc2SAdrian Chadd 
205e60c4fc2SAdrian Chadd 	/*
206e60c4fc2SAdrian Chadd 	 * Enable radar PHY errors if requested by the
207e60c4fc2SAdrian Chadd 	 * DFS module.
208e60c4fc2SAdrian Chadd 	 */
209e60c4fc2SAdrian Chadd 	if (sc->sc_dodfs)
210e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PHYRADAR;
211e60c4fc2SAdrian Chadd 
212e60c4fc2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n",
213e60c4fc2SAdrian Chadd 	    __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags);
214e60c4fc2SAdrian Chadd 	return rfilt;
215e60c4fc2SAdrian Chadd }
216e60c4fc2SAdrian Chadd 
217e60c4fc2SAdrian Chadd int
218e60c4fc2SAdrian Chadd ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
219e60c4fc2SAdrian Chadd {
220e60c4fc2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
221e60c4fc2SAdrian Chadd 	int error;
222e60c4fc2SAdrian Chadd 	struct mbuf *m;
223e60c4fc2SAdrian Chadd 	struct ath_desc *ds;
224e60c4fc2SAdrian Chadd 
225e60c4fc2SAdrian Chadd 	m = bf->bf_m;
226e60c4fc2SAdrian Chadd 	if (m == NULL) {
227e60c4fc2SAdrian Chadd 		/*
228e60c4fc2SAdrian Chadd 		 * NB: by assigning a page to the rx dma buffer we
229e60c4fc2SAdrian Chadd 		 * implicitly satisfy the Atheros requirement that
230e60c4fc2SAdrian Chadd 		 * this buffer be cache-line-aligned and sized to be
231e60c4fc2SAdrian Chadd 		 * multiple of the cache line size.  Not doing this
232e60c4fc2SAdrian Chadd 		 * causes weird stuff to happen (for the 5210 at least).
233e60c4fc2SAdrian Chadd 		 */
234e60c4fc2SAdrian Chadd 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
235e60c4fc2SAdrian Chadd 		if (m == NULL) {
236e60c4fc2SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_ANY,
237e60c4fc2SAdrian Chadd 				"%s: no mbuf/cluster\n", __func__);
238e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_nombuf++;
239e60c4fc2SAdrian Chadd 			return ENOMEM;
240e60c4fc2SAdrian Chadd 		}
241e60c4fc2SAdrian Chadd 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
242e60c4fc2SAdrian Chadd 
243e60c4fc2SAdrian Chadd 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
244e60c4fc2SAdrian Chadd 					     bf->bf_dmamap, m,
245e60c4fc2SAdrian Chadd 					     bf->bf_segs, &bf->bf_nseg,
246e60c4fc2SAdrian Chadd 					     BUS_DMA_NOWAIT);
247e60c4fc2SAdrian Chadd 		if (error != 0) {
248e60c4fc2SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_ANY,
249e60c4fc2SAdrian Chadd 			    "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
250e60c4fc2SAdrian Chadd 			    __func__, error);
251e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_busdma++;
252e60c4fc2SAdrian Chadd 			m_freem(m);
253e60c4fc2SAdrian Chadd 			return error;
254e60c4fc2SAdrian Chadd 		}
255e60c4fc2SAdrian Chadd 		KASSERT(bf->bf_nseg == 1,
256e60c4fc2SAdrian Chadd 			("multi-segment packet; nseg %u", bf->bf_nseg));
257e60c4fc2SAdrian Chadd 		bf->bf_m = m;
258e60c4fc2SAdrian Chadd 	}
259e60c4fc2SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
260e60c4fc2SAdrian Chadd 
261e60c4fc2SAdrian Chadd 	/*
262e60c4fc2SAdrian Chadd 	 * Setup descriptors.  For receive we always terminate
263e60c4fc2SAdrian Chadd 	 * the descriptor list with a self-linked entry so we'll
264e60c4fc2SAdrian Chadd 	 * not get overrun under high load (as can happen with a
265e60c4fc2SAdrian Chadd 	 * 5212 when ANI processing enables PHY error frames).
266e60c4fc2SAdrian Chadd 	 *
267e60c4fc2SAdrian Chadd 	 * To insure the last descriptor is self-linked we create
268e60c4fc2SAdrian Chadd 	 * each descriptor as self-linked and add it to the end.  As
269e60c4fc2SAdrian Chadd 	 * each additional descriptor is added the previous self-linked
270e60c4fc2SAdrian Chadd 	 * entry is ``fixed'' naturally.  This should be safe even
271e60c4fc2SAdrian Chadd 	 * if DMA is happening.  When processing RX interrupts we
272e60c4fc2SAdrian Chadd 	 * never remove/process the last, self-linked, entry on the
273e60c4fc2SAdrian Chadd 	 * descriptor list.  This insures the hardware always has
274e60c4fc2SAdrian Chadd 	 * someplace to write a new frame.
275e60c4fc2SAdrian Chadd 	 */
276e60c4fc2SAdrian Chadd 	/*
277e60c4fc2SAdrian Chadd 	 * 11N: we can no longer afford to self link the last descriptor.
278e60c4fc2SAdrian Chadd 	 * MAC acknowledges BA status as long as it copies frames to host
279e60c4fc2SAdrian Chadd 	 * buffer (or rx fifo). This can incorrectly acknowledge packets
280e60c4fc2SAdrian Chadd 	 * to a sender if last desc is self-linked.
281e60c4fc2SAdrian Chadd 	 */
282e60c4fc2SAdrian Chadd 	ds = bf->bf_desc;
283e60c4fc2SAdrian Chadd 	if (sc->sc_rxslink)
284e60c4fc2SAdrian Chadd 		ds->ds_link = bf->bf_daddr;	/* link to self */
285e60c4fc2SAdrian Chadd 	else
286e60c4fc2SAdrian Chadd 		ds->ds_link = 0;		/* terminate the list */
287e60c4fc2SAdrian Chadd 	ds->ds_data = bf->bf_segs[0].ds_addr;
288e60c4fc2SAdrian Chadd 	ath_hal_setuprxdesc(ah, ds
289e60c4fc2SAdrian Chadd 		, m->m_len		/* buffer size */
290e60c4fc2SAdrian Chadd 		, 0
291e60c4fc2SAdrian Chadd 	);
292e60c4fc2SAdrian Chadd 
293e60c4fc2SAdrian Chadd 	if (sc->sc_rxlink != NULL)
294e60c4fc2SAdrian Chadd 		*sc->sc_rxlink = bf->bf_daddr;
295e60c4fc2SAdrian Chadd 	sc->sc_rxlink = &ds->ds_link;
296e60c4fc2SAdrian Chadd 	return 0;
297e60c4fc2SAdrian Chadd }
298e60c4fc2SAdrian Chadd 
299e60c4fc2SAdrian Chadd /*
300e60c4fc2SAdrian Chadd  * Intercept management frames to collect beacon rssi data
301e60c4fc2SAdrian Chadd  * and to do ibss merges.
302e60c4fc2SAdrian Chadd  */
303e60c4fc2SAdrian Chadd void
304e60c4fc2SAdrian Chadd ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
305e60c4fc2SAdrian Chadd 	int subtype, int rssi, int nf)
306e60c4fc2SAdrian Chadd {
307e60c4fc2SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
308e60c4fc2SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
309e60c4fc2SAdrian Chadd 
310e60c4fc2SAdrian Chadd 	/*
311e60c4fc2SAdrian Chadd 	 * Call up first so subsequent work can use information
312e60c4fc2SAdrian Chadd 	 * potentially stored in the node (e.g. for ibss merge).
313e60c4fc2SAdrian Chadd 	 */
314e60c4fc2SAdrian Chadd 	ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
315e60c4fc2SAdrian Chadd 	switch (subtype) {
316e60c4fc2SAdrian Chadd 	case IEEE80211_FC0_SUBTYPE_BEACON:
317e60c4fc2SAdrian Chadd 		/* update rssi statistics for use by the hal */
318e60c4fc2SAdrian Chadd 		/* XXX unlocked check against vap->iv_bss? */
319e60c4fc2SAdrian Chadd 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
320e60c4fc2SAdrian Chadd 		if (sc->sc_syncbeacon &&
321e60c4fc2SAdrian Chadd 		    ni == vap->iv_bss && vap->iv_state == IEEE80211_S_RUN) {
322e60c4fc2SAdrian Chadd 			/*
323e60c4fc2SAdrian Chadd 			 * Resync beacon timers using the tsf of the beacon
324e60c4fc2SAdrian Chadd 			 * frame we just received.
325e60c4fc2SAdrian Chadd 			 */
326e60c4fc2SAdrian Chadd 			ath_beacon_config(sc, vap);
327e60c4fc2SAdrian Chadd 		}
328e60c4fc2SAdrian Chadd 		/* fall thru... */
329e60c4fc2SAdrian Chadd 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
330e60c4fc2SAdrian Chadd 		if (vap->iv_opmode == IEEE80211_M_IBSS &&
331e60c4fc2SAdrian Chadd 		    vap->iv_state == IEEE80211_S_RUN) {
332e60c4fc2SAdrian Chadd 			uint32_t rstamp = sc->sc_lastrs->rs_tstamp;
333e60c4fc2SAdrian Chadd 			uint64_t tsf = ath_extend_tsf(sc, rstamp,
334e60c4fc2SAdrian Chadd 				ath_hal_gettsf64(sc->sc_ah));
335e60c4fc2SAdrian Chadd 			/*
336e60c4fc2SAdrian Chadd 			 * Handle ibss merge as needed; check the tsf on the
337e60c4fc2SAdrian Chadd 			 * frame before attempting the merge.  The 802.11 spec
338e60c4fc2SAdrian Chadd 			 * says the station should change it's bssid to match
339e60c4fc2SAdrian Chadd 			 * the oldest station with the same ssid, where oldest
340e60c4fc2SAdrian Chadd 			 * is determined by the tsf.  Note that hardware
341e60c4fc2SAdrian Chadd 			 * reconfiguration happens through callback to
342e60c4fc2SAdrian Chadd 			 * ath_newstate as the state machine will go from
343e60c4fc2SAdrian Chadd 			 * RUN -> RUN when this happens.
344e60c4fc2SAdrian Chadd 			 */
345e60c4fc2SAdrian Chadd 			if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
346e60c4fc2SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_STATE,
347e60c4fc2SAdrian Chadd 				    "ibss merge, rstamp %u tsf %ju "
348e60c4fc2SAdrian Chadd 				    "tstamp %ju\n", rstamp, (uintmax_t)tsf,
349e60c4fc2SAdrian Chadd 				    (uintmax_t)ni->ni_tstamp.tsf);
350e60c4fc2SAdrian Chadd 				(void) ieee80211_ibss_merge(ni);
351e60c4fc2SAdrian Chadd 			}
352e60c4fc2SAdrian Chadd 		}
353e60c4fc2SAdrian Chadd 		break;
354e60c4fc2SAdrian Chadd 	}
355e60c4fc2SAdrian Chadd }
356e60c4fc2SAdrian Chadd 
357e60c4fc2SAdrian Chadd static void
358e60c4fc2SAdrian Chadd ath_rx_tap(struct ifnet *ifp, struct mbuf *m,
359e60c4fc2SAdrian Chadd 	const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
360e60c4fc2SAdrian Chadd {
361e60c4fc2SAdrian Chadd #define	CHAN_HT20	htole32(IEEE80211_CHAN_HT20)
362e60c4fc2SAdrian Chadd #define	CHAN_HT40U	htole32(IEEE80211_CHAN_HT40U)
363e60c4fc2SAdrian Chadd #define	CHAN_HT40D	htole32(IEEE80211_CHAN_HT40D)
364e60c4fc2SAdrian Chadd #define	CHAN_HT		(CHAN_HT20|CHAN_HT40U|CHAN_HT40D)
365e60c4fc2SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
366e60c4fc2SAdrian Chadd 	const HAL_RATE_TABLE *rt;
367e60c4fc2SAdrian Chadd 	uint8_t rix;
368e60c4fc2SAdrian Chadd 
369e60c4fc2SAdrian Chadd 	rt = sc->sc_currates;
370e60c4fc2SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
371e60c4fc2SAdrian Chadd 	rix = rt->rateCodeToIndex[rs->rs_rate];
372e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
373e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
374e60c4fc2SAdrian Chadd #ifdef AH_SUPPORT_AR5416
375e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT;
376e60c4fc2SAdrian Chadd 	if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) {	/* HT rate */
377e60c4fc2SAdrian Chadd 		struct ieee80211com *ic = ifp->if_l2com;
378e60c4fc2SAdrian Chadd 
379e60c4fc2SAdrian Chadd 		if ((rs->rs_flags & HAL_RX_2040) == 0)
380e60c4fc2SAdrian Chadd 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
381e60c4fc2SAdrian Chadd 		else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan))
382e60c4fc2SAdrian Chadd 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
383e60c4fc2SAdrian Chadd 		else
384e60c4fc2SAdrian Chadd 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
385e60c4fc2SAdrian Chadd 		if ((rs->rs_flags & HAL_RX_GI) == 0)
386e60c4fc2SAdrian Chadd 			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
387e60c4fc2SAdrian Chadd 	}
388e60c4fc2SAdrian Chadd #endif
389e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(sc, rs->rs_tstamp, tsf));
390e60c4fc2SAdrian Chadd 	if (rs->rs_status & HAL_RXERR_CRC)
391e60c4fc2SAdrian Chadd 		sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
392e60c4fc2SAdrian Chadd 	/* XXX propagate other error flags from descriptor */
393e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_antnoise = nf;
394e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_antsignal = nf + rs->rs_rssi;
395e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_antenna = rs->rs_antenna;
396e60c4fc2SAdrian Chadd #undef CHAN_HT
397e60c4fc2SAdrian Chadd #undef CHAN_HT20
398e60c4fc2SAdrian Chadd #undef CHAN_HT40U
399e60c4fc2SAdrian Chadd #undef CHAN_HT40D
400e60c4fc2SAdrian Chadd }
401e60c4fc2SAdrian Chadd 
402e60c4fc2SAdrian Chadd static void
403e60c4fc2SAdrian Chadd ath_handle_micerror(struct ieee80211com *ic,
404e60c4fc2SAdrian Chadd 	struct ieee80211_frame *wh, int keyix)
405e60c4fc2SAdrian Chadd {
406e60c4fc2SAdrian Chadd 	struct ieee80211_node *ni;
407e60c4fc2SAdrian Chadd 
408e60c4fc2SAdrian Chadd 	/* XXX recheck MIC to deal w/ chips that lie */
409e60c4fc2SAdrian Chadd 	/* XXX discard MIC errors on !data frames */
410e60c4fc2SAdrian Chadd 	ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
411e60c4fc2SAdrian Chadd 	if (ni != NULL) {
412e60c4fc2SAdrian Chadd 		ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix);
413e60c4fc2SAdrian Chadd 		ieee80211_free_node(ni);
414e60c4fc2SAdrian Chadd 	}
415e60c4fc2SAdrian Chadd }
416e60c4fc2SAdrian Chadd 
417e60c4fc2SAdrian Chadd /*
418e60c4fc2SAdrian Chadd  * Only run the RX proc if it's not already running.
419e60c4fc2SAdrian Chadd  * Since this may get run as part of the reset/flush path,
420e60c4fc2SAdrian Chadd  * the task can't clash with an existing, running tasklet.
421e60c4fc2SAdrian Chadd  */
422e60c4fc2SAdrian Chadd void
423e60c4fc2SAdrian Chadd ath_rx_tasklet(void *arg, int npending)
424e60c4fc2SAdrian Chadd {
425e60c4fc2SAdrian Chadd 	struct ath_softc *sc = arg;
426e60c4fc2SAdrian Chadd 
427e60c4fc2SAdrian Chadd 	CTR1(ATH_KTR_INTR, "ath_rx_proc: pending=%d", npending);
428e60c4fc2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
429e60c4fc2SAdrian Chadd 	ATH_PCU_LOCK(sc);
430e60c4fc2SAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
431e60c4fc2SAdrian Chadd 		device_printf(sc->sc_dev,
432e60c4fc2SAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
433e60c4fc2SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
434e60c4fc2SAdrian Chadd 		return;
435e60c4fc2SAdrian Chadd 	}
436e60c4fc2SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
437e60c4fc2SAdrian Chadd 	ath_rx_proc(sc, 1);
438e60c4fc2SAdrian Chadd }
439e60c4fc2SAdrian Chadd 
440*d542f7f6SAdrian Chadd static int
441*d542f7f6SAdrian Chadd ath_rx_pkt(struct ath_softc *sc, struct ath_rx_status *rs, HAL_STATUS status,
442*d542f7f6SAdrian Chadd     uint64_t tsf, int nf, struct ath_buf *bf)
443e60c4fc2SAdrian Chadd {
444*d542f7f6SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
445*d542f7f6SAdrian Chadd 	struct mbuf *m = bf->bf_m;
446*d542f7f6SAdrian Chadd 	uint64_t rstamp;
447*d542f7f6SAdrian Chadd 	int len, type;
448e60c4fc2SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
449e60c4fc2SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
450e60c4fc2SAdrian Chadd 	struct ieee80211_node *ni;
451*d542f7f6SAdrian Chadd 	int is_good = 0;
452e60c4fc2SAdrian Chadd 
453e60c4fc2SAdrian Chadd 	/*
454e60c4fc2SAdrian Chadd 	 * Calculate the correct 64 bit TSF given
455e60c4fc2SAdrian Chadd 	 * the TSF64 register value and rs_tstamp.
456e60c4fc2SAdrian Chadd 	 */
457e60c4fc2SAdrian Chadd 	rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
458e60c4fc2SAdrian Chadd 
459e60c4fc2SAdrian Chadd 	/* These aren't specifically errors */
460e60c4fc2SAdrian Chadd #ifdef	AH_SUPPORT_AR5416
461e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_GI)
462e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_halfgi++;
463e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_2040)
464e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_2040++;
465e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_DELIM_CRC_PRE)
466e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_pre_crc_err++;
467e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_DELIM_CRC_POST)
468e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_post_crc_err++;
469e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_DECRYPT_BUSY)
470e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_decrypt_busy_err++;
471e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_HI_RX_CHAIN)
472e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_hi_rx_chain++;
473e60c4fc2SAdrian Chadd #endif /* AH_SUPPORT_AR5416 */
474e60c4fc2SAdrian Chadd 
475e60c4fc2SAdrian Chadd 	if (rs->rs_status != 0) {
476e60c4fc2SAdrian Chadd 		if (rs->rs_status & HAL_RXERR_CRC)
477e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_crcerr++;
478e60c4fc2SAdrian Chadd 		if (rs->rs_status & HAL_RXERR_FIFO)
479e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_fifoerr++;
480e60c4fc2SAdrian Chadd 		if (rs->rs_status & HAL_RXERR_PHY) {
481e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_phyerr++;
482e60c4fc2SAdrian Chadd 			/* Process DFS radar events */
483e60c4fc2SAdrian Chadd 			if ((rs->rs_phyerr == HAL_PHYERR_RADAR) ||
484e60c4fc2SAdrian Chadd 			    (rs->rs_phyerr == HAL_PHYERR_FALSE_RADAR_EXT)) {
485e60c4fc2SAdrian Chadd 				/* Since we're touching the frame data, sync it */
486e60c4fc2SAdrian Chadd 				bus_dmamap_sync(sc->sc_dmat,
487e60c4fc2SAdrian Chadd 				    bf->bf_dmamap,
488e60c4fc2SAdrian Chadd 				    BUS_DMASYNC_POSTREAD);
489e60c4fc2SAdrian Chadd 				/* Now pass it to the radar processing code */
490e60c4fc2SAdrian Chadd 				ath_dfs_process_phy_err(sc, mtod(m, char *), rstamp, rs);
491e60c4fc2SAdrian Chadd 			}
492e60c4fc2SAdrian Chadd 
493e60c4fc2SAdrian Chadd 			/* Be suitably paranoid about receiving phy errors out of the stats array bounds */
494e60c4fc2SAdrian Chadd 			if (rs->rs_phyerr < 64)
495e60c4fc2SAdrian Chadd 				sc->sc_stats.ast_rx_phy[rs->rs_phyerr]++;
496e60c4fc2SAdrian Chadd 			goto rx_error;	/* NB: don't count in ierrors */
497e60c4fc2SAdrian Chadd 		}
498e60c4fc2SAdrian Chadd 		if (rs->rs_status & HAL_RXERR_DECRYPT) {
499e60c4fc2SAdrian Chadd 			/*
500e60c4fc2SAdrian Chadd 			 * Decrypt error.  If the error occurred
501e60c4fc2SAdrian Chadd 			 * because there was no hardware key, then
502e60c4fc2SAdrian Chadd 			 * let the frame through so the upper layers
503e60c4fc2SAdrian Chadd 			 * can process it.  This is necessary for 5210
504e60c4fc2SAdrian Chadd 			 * parts which have no way to setup a ``clear''
505e60c4fc2SAdrian Chadd 			 * key cache entry.
506e60c4fc2SAdrian Chadd 			 *
507e60c4fc2SAdrian Chadd 			 * XXX do key cache faulting
508e60c4fc2SAdrian Chadd 			 */
509e60c4fc2SAdrian Chadd 			if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
510e60c4fc2SAdrian Chadd 				goto rx_accept;
511e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_badcrypt++;
512e60c4fc2SAdrian Chadd 		}
513e60c4fc2SAdrian Chadd 		if (rs->rs_status & HAL_RXERR_MIC) {
514e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_badmic++;
515e60c4fc2SAdrian Chadd 			/*
516e60c4fc2SAdrian Chadd 			 * Do minimal work required to hand off
517e60c4fc2SAdrian Chadd 			 * the 802.11 header for notification.
518e60c4fc2SAdrian Chadd 			 */
519e60c4fc2SAdrian Chadd 			/* XXX frag's and qos frames */
520e60c4fc2SAdrian Chadd 			len = rs->rs_datalen;
521e60c4fc2SAdrian Chadd 			if (len >= sizeof (struct ieee80211_frame)) {
522e60c4fc2SAdrian Chadd 				bus_dmamap_sync(sc->sc_dmat,
523e60c4fc2SAdrian Chadd 				    bf->bf_dmamap,
524e60c4fc2SAdrian Chadd 				    BUS_DMASYNC_POSTREAD);
525e60c4fc2SAdrian Chadd 				ath_handle_micerror(ic,
526e60c4fc2SAdrian Chadd 				    mtod(m, struct ieee80211_frame *),
527e60c4fc2SAdrian Chadd 				    sc->sc_splitmic ?
528e60c4fc2SAdrian Chadd 					rs->rs_keyix-32 : rs->rs_keyix);
529e60c4fc2SAdrian Chadd 			}
530e60c4fc2SAdrian Chadd 		}
531e60c4fc2SAdrian Chadd 		ifp->if_ierrors++;
532e60c4fc2SAdrian Chadd rx_error:
533e60c4fc2SAdrian Chadd 		/*
534e60c4fc2SAdrian Chadd 		 * Cleanup any pending partial frame.
535e60c4fc2SAdrian Chadd 		 */
536e60c4fc2SAdrian Chadd 		if (sc->sc_rxpending != NULL) {
537e60c4fc2SAdrian Chadd 			m_freem(sc->sc_rxpending);
538e60c4fc2SAdrian Chadd 			sc->sc_rxpending = NULL;
539e60c4fc2SAdrian Chadd 		}
540e60c4fc2SAdrian Chadd 		/*
541e60c4fc2SAdrian Chadd 		 * When a tap is present pass error frames
542e60c4fc2SAdrian Chadd 		 * that have been requested.  By default we
543e60c4fc2SAdrian Chadd 		 * pass decrypt+mic errors but others may be
544e60c4fc2SAdrian Chadd 		 * interesting (e.g. crc).
545e60c4fc2SAdrian Chadd 		 */
546e60c4fc2SAdrian Chadd 		if (ieee80211_radiotap_active(ic) &&
547e60c4fc2SAdrian Chadd 		    (rs->rs_status & sc->sc_monpass)) {
548e60c4fc2SAdrian Chadd 			bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
549e60c4fc2SAdrian Chadd 			    BUS_DMASYNC_POSTREAD);
550e60c4fc2SAdrian Chadd 			/* NB: bpf needs the mbuf length setup */
551e60c4fc2SAdrian Chadd 			len = rs->rs_datalen;
552e60c4fc2SAdrian Chadd 			m->m_pkthdr.len = m->m_len = len;
553e60c4fc2SAdrian Chadd 			bf->bf_m = NULL;
554e60c4fc2SAdrian Chadd 			ath_rx_tap(ifp, m, rs, rstamp, nf);
555e60c4fc2SAdrian Chadd 			ieee80211_radiotap_rx_all(ic, m);
556e60c4fc2SAdrian Chadd 			m_freem(m);
557e60c4fc2SAdrian Chadd 		}
558e60c4fc2SAdrian Chadd 		/* XXX pass MIC errors up for s/w reclaculation */
559e60c4fc2SAdrian Chadd 		goto rx_next;
560e60c4fc2SAdrian Chadd 	}
561e60c4fc2SAdrian Chadd rx_accept:
562e60c4fc2SAdrian Chadd 	/*
563e60c4fc2SAdrian Chadd 	 * Sync and unmap the frame.  At this point we're
564e60c4fc2SAdrian Chadd 	 * committed to passing the mbuf somewhere so clear
565e60c4fc2SAdrian Chadd 	 * bf_m; this means a new mbuf must be allocated
566e60c4fc2SAdrian Chadd 	 * when the rx descriptor is setup again to receive
567e60c4fc2SAdrian Chadd 	 * another frame.
568e60c4fc2SAdrian Chadd 	 */
569*d542f7f6SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_POSTREAD);
570e60c4fc2SAdrian Chadd 	bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
571e60c4fc2SAdrian Chadd 	bf->bf_m = NULL;
572e60c4fc2SAdrian Chadd 
573e60c4fc2SAdrian Chadd 	len = rs->rs_datalen;
574e60c4fc2SAdrian Chadd 	m->m_len = len;
575e60c4fc2SAdrian Chadd 
576e60c4fc2SAdrian Chadd 	if (rs->rs_more) {
577e60c4fc2SAdrian Chadd 		/*
578e60c4fc2SAdrian Chadd 		 * Frame spans multiple descriptors; save
579e60c4fc2SAdrian Chadd 		 * it for the next completed descriptor, it
580e60c4fc2SAdrian Chadd 		 * will be used to construct a jumbogram.
581e60c4fc2SAdrian Chadd 		 */
582e60c4fc2SAdrian Chadd 		if (sc->sc_rxpending != NULL) {
583e60c4fc2SAdrian Chadd 			/* NB: max frame size is currently 2 clusters */
584e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_toobig++;
585e60c4fc2SAdrian Chadd 			m_freem(sc->sc_rxpending);
586e60c4fc2SAdrian Chadd 		}
587e60c4fc2SAdrian Chadd 		m->m_pkthdr.rcvif = ifp;
588e60c4fc2SAdrian Chadd 		m->m_pkthdr.len = len;
589e60c4fc2SAdrian Chadd 		sc->sc_rxpending = m;
590e60c4fc2SAdrian Chadd 		goto rx_next;
591e60c4fc2SAdrian Chadd 	} else if (sc->sc_rxpending != NULL) {
592e60c4fc2SAdrian Chadd 		/*
593e60c4fc2SAdrian Chadd 		 * This is the second part of a jumbogram,
594e60c4fc2SAdrian Chadd 		 * chain it to the first mbuf, adjust the
595e60c4fc2SAdrian Chadd 		 * frame length, and clear the rxpending state.
596e60c4fc2SAdrian Chadd 		 */
597e60c4fc2SAdrian Chadd 		sc->sc_rxpending->m_next = m;
598e60c4fc2SAdrian Chadd 		sc->sc_rxpending->m_pkthdr.len += len;
599e60c4fc2SAdrian Chadd 		m = sc->sc_rxpending;
600e60c4fc2SAdrian Chadd 		sc->sc_rxpending = NULL;
601e60c4fc2SAdrian Chadd 	} else {
602e60c4fc2SAdrian Chadd 		/*
603e60c4fc2SAdrian Chadd 		 * Normal single-descriptor receive; setup
604e60c4fc2SAdrian Chadd 		 * the rcvif and packet length.
605e60c4fc2SAdrian Chadd 		 */
606e60c4fc2SAdrian Chadd 		m->m_pkthdr.rcvif = ifp;
607e60c4fc2SAdrian Chadd 		m->m_pkthdr.len = len;
608e60c4fc2SAdrian Chadd 	}
609e60c4fc2SAdrian Chadd 
610e60c4fc2SAdrian Chadd 	/*
611e60c4fc2SAdrian Chadd 	 * Validate rs->rs_antenna.
612e60c4fc2SAdrian Chadd 	 *
613e60c4fc2SAdrian Chadd 	 * Some users w/ AR9285 NICs have reported crashes
614e60c4fc2SAdrian Chadd 	 * here because rs_antenna field is bogusly large.
615e60c4fc2SAdrian Chadd 	 * Let's enforce the maximum antenna limit of 8
616e60c4fc2SAdrian Chadd 	 * (and it shouldn't be hard coded, but that's a
617e60c4fc2SAdrian Chadd 	 * separate problem) and if there's an issue, print
618e60c4fc2SAdrian Chadd 	 * out an error and adjust rs_antenna to something
619e60c4fc2SAdrian Chadd 	 * sensible.
620e60c4fc2SAdrian Chadd 	 *
621e60c4fc2SAdrian Chadd 	 * This code should be removed once the actual
622e60c4fc2SAdrian Chadd 	 * root cause of the issue has been identified.
623e60c4fc2SAdrian Chadd 	 * For example, it may be that the rs_antenna
624e60c4fc2SAdrian Chadd 	 * field is only valid for the lsat frame of
625e60c4fc2SAdrian Chadd 	 * an aggregate and it just happens that it is
626e60c4fc2SAdrian Chadd 	 * "mostly" right. (This is a general statement -
627e60c4fc2SAdrian Chadd 	 * the majority of the statistics are only valid
628e60c4fc2SAdrian Chadd 	 * for the last frame in an aggregate.
629e60c4fc2SAdrian Chadd 	 */
630e60c4fc2SAdrian Chadd 	if (rs->rs_antenna > 7) {
631e60c4fc2SAdrian Chadd 		device_printf(sc->sc_dev, "%s: rs_antenna > 7 (%d)\n",
632e60c4fc2SAdrian Chadd 		    __func__, rs->rs_antenna);
633e60c4fc2SAdrian Chadd #ifdef	ATH_DEBUG
634e60c4fc2SAdrian Chadd 		ath_printrxbuf(sc, bf, 0, status == HAL_OK);
635e60c4fc2SAdrian Chadd #endif /* ATH_DEBUG */
636e60c4fc2SAdrian Chadd 		rs->rs_antenna = 0;	/* XXX better than nothing */
637e60c4fc2SAdrian Chadd 	}
638e60c4fc2SAdrian Chadd 
639e60c4fc2SAdrian Chadd 	ifp->if_ipackets++;
640e60c4fc2SAdrian Chadd 	sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
641e60c4fc2SAdrian Chadd 
642e60c4fc2SAdrian Chadd 	/*
643e60c4fc2SAdrian Chadd 	 * Populate the rx status block.  When there are bpf
644e60c4fc2SAdrian Chadd 	 * listeners we do the additional work to provide
645e60c4fc2SAdrian Chadd 	 * complete status.  Otherwise we fill in only the
646e60c4fc2SAdrian Chadd 	 * material required by ieee80211_input.  Note that
647e60c4fc2SAdrian Chadd 	 * noise setting is filled in above.
648e60c4fc2SAdrian Chadd 	 */
649e60c4fc2SAdrian Chadd 	if (ieee80211_radiotap_active(ic))
650e60c4fc2SAdrian Chadd 		ath_rx_tap(ifp, m, rs, rstamp, nf);
651e60c4fc2SAdrian Chadd 
652e60c4fc2SAdrian Chadd 	/*
653e60c4fc2SAdrian Chadd 	 * From this point on we assume the frame is at least
654e60c4fc2SAdrian Chadd 	 * as large as ieee80211_frame_min; verify that.
655e60c4fc2SAdrian Chadd 	 */
656e60c4fc2SAdrian Chadd 	if (len < IEEE80211_MIN_LEN) {
657e60c4fc2SAdrian Chadd 		if (!ieee80211_radiotap_active(ic)) {
658e60c4fc2SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_RECV,
659e60c4fc2SAdrian Chadd 			    "%s: short packet %d\n", __func__, len);
660e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_tooshort++;
661e60c4fc2SAdrian Chadd 		} else {
662e60c4fc2SAdrian Chadd 			/* NB: in particular this captures ack's */
663e60c4fc2SAdrian Chadd 			ieee80211_radiotap_rx_all(ic, m);
664e60c4fc2SAdrian Chadd 		}
665e60c4fc2SAdrian Chadd 		m_freem(m);
666e60c4fc2SAdrian Chadd 		goto rx_next;
667e60c4fc2SAdrian Chadd 	}
668e60c4fc2SAdrian Chadd 
669e60c4fc2SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
670e60c4fc2SAdrian Chadd 		const HAL_RATE_TABLE *rt = sc->sc_currates;
671e60c4fc2SAdrian Chadd 		uint8_t rix = rt->rateCodeToIndex[rs->rs_rate];
672e60c4fc2SAdrian Chadd 
673e60c4fc2SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
674e60c4fc2SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, rs->rs_rssi);
675e60c4fc2SAdrian Chadd 	}
676e60c4fc2SAdrian Chadd 
677e60c4fc2SAdrian Chadd 	m_adj(m, -IEEE80211_CRC_LEN);
678e60c4fc2SAdrian Chadd 
679e60c4fc2SAdrian Chadd 	/*
680e60c4fc2SAdrian Chadd 	 * Locate the node for sender, track state, and then
681e60c4fc2SAdrian Chadd 	 * pass the (referenced) node up to the 802.11 layer
682e60c4fc2SAdrian Chadd 	 * for its use.
683e60c4fc2SAdrian Chadd 	 */
684e60c4fc2SAdrian Chadd 	ni = ieee80211_find_rxnode_withkey(ic,
685e60c4fc2SAdrian Chadd 		mtod(m, const struct ieee80211_frame_min *),
686e60c4fc2SAdrian Chadd 		rs->rs_keyix == HAL_RXKEYIX_INVALID ?
687e60c4fc2SAdrian Chadd 			IEEE80211_KEYIX_NONE : rs->rs_keyix);
688e60c4fc2SAdrian Chadd 	sc->sc_lastrs = rs;
689e60c4fc2SAdrian Chadd 
690e60c4fc2SAdrian Chadd #ifdef	AH_SUPPORT_AR5416
691e60c4fc2SAdrian Chadd 	if (rs->rs_isaggr)
692e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_agg++;
693e60c4fc2SAdrian Chadd #endif /* AH_SUPPORT_AR5416 */
694e60c4fc2SAdrian Chadd 
695e60c4fc2SAdrian Chadd 	if (ni != NULL) {
696e60c4fc2SAdrian Chadd 		/*
697e60c4fc2SAdrian Chadd 		 * Only punt packets for ampdu reorder processing for
698e60c4fc2SAdrian Chadd 		 * 11n nodes; net80211 enforces that M_AMPDU is only
699e60c4fc2SAdrian Chadd 		 * set for 11n nodes.
700e60c4fc2SAdrian Chadd 		 */
701e60c4fc2SAdrian Chadd 		if (ni->ni_flags & IEEE80211_NODE_HT)
702e60c4fc2SAdrian Chadd 			m->m_flags |= M_AMPDU;
703e60c4fc2SAdrian Chadd 
704e60c4fc2SAdrian Chadd 		/*
705e60c4fc2SAdrian Chadd 		 * Sending station is known, dispatch directly.
706e60c4fc2SAdrian Chadd 		 */
707e60c4fc2SAdrian Chadd 		type = ieee80211_input(ni, m, rs->rs_rssi, nf);
708e60c4fc2SAdrian Chadd 		ieee80211_free_node(ni);
709e60c4fc2SAdrian Chadd 		/*
710e60c4fc2SAdrian Chadd 		 * Arrange to update the last rx timestamp only for
711e60c4fc2SAdrian Chadd 		 * frames from our ap when operating in station mode.
712e60c4fc2SAdrian Chadd 		 * This assumes the rx key is always setup when
713e60c4fc2SAdrian Chadd 		 * associated.
714e60c4fc2SAdrian Chadd 		 */
715e60c4fc2SAdrian Chadd 		if (ic->ic_opmode == IEEE80211_M_STA &&
716e60c4fc2SAdrian Chadd 		    rs->rs_keyix != HAL_RXKEYIX_INVALID)
717*d542f7f6SAdrian Chadd 			is_good = 1;
718e60c4fc2SAdrian Chadd 	} else {
719e60c4fc2SAdrian Chadd 		type = ieee80211_input_all(ic, m, rs->rs_rssi, nf);
720e60c4fc2SAdrian Chadd 	}
721e60c4fc2SAdrian Chadd 	/*
722e60c4fc2SAdrian Chadd 	 * Track rx rssi and do any rx antenna management.
723e60c4fc2SAdrian Chadd 	 */
724e60c4fc2SAdrian Chadd 	ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
725e60c4fc2SAdrian Chadd 	if (sc->sc_diversity) {
726e60c4fc2SAdrian Chadd 		/*
727e60c4fc2SAdrian Chadd 		 * When using fast diversity, change the default rx
728e60c4fc2SAdrian Chadd 		 * antenna if diversity chooses the other antenna 3
729e60c4fc2SAdrian Chadd 		 * times in a row.
730e60c4fc2SAdrian Chadd 		 */
731e60c4fc2SAdrian Chadd 		if (sc->sc_defant != rs->rs_antenna) {
732e60c4fc2SAdrian Chadd 			if (++sc->sc_rxotherant >= 3)
733e60c4fc2SAdrian Chadd 				ath_setdefantenna(sc, rs->rs_antenna);
734e60c4fc2SAdrian Chadd 		} else
735e60c4fc2SAdrian Chadd 			sc->sc_rxotherant = 0;
736e60c4fc2SAdrian Chadd 	}
737e60c4fc2SAdrian Chadd 
738e60c4fc2SAdrian Chadd 	/* Newer school diversity - kite specific for now */
739e60c4fc2SAdrian Chadd 	/* XXX perhaps migrate the normal diversity code to this? */
740e60c4fc2SAdrian Chadd 	if ((ah)->ah_rxAntCombDiversity)
741e60c4fc2SAdrian Chadd 		(*(ah)->ah_rxAntCombDiversity)(ah, rs, ticks, hz);
742e60c4fc2SAdrian Chadd 
743e60c4fc2SAdrian Chadd 	if (sc->sc_softled) {
744e60c4fc2SAdrian Chadd 		/*
745e60c4fc2SAdrian Chadd 		 * Blink for any data frame.  Otherwise do a
746e60c4fc2SAdrian Chadd 		 * heartbeat-style blink when idle.  The latter
747e60c4fc2SAdrian Chadd 		 * is mainly for station mode where we depend on
748e60c4fc2SAdrian Chadd 		 * periodic beacon frames to trigger the poll event.
749e60c4fc2SAdrian Chadd 		 */
750e60c4fc2SAdrian Chadd 		if (type == IEEE80211_FC0_TYPE_DATA) {
751e60c4fc2SAdrian Chadd 			const HAL_RATE_TABLE *rt = sc->sc_currates;
752e60c4fc2SAdrian Chadd 			ath_led_event(sc,
753e60c4fc2SAdrian Chadd 			    rt->rateCodeToIndex[rs->rs_rate]);
754e60c4fc2SAdrian Chadd 		} else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
755e60c4fc2SAdrian Chadd 			ath_led_event(sc, 0);
756e60c4fc2SAdrian Chadd 		}
757e60c4fc2SAdrian Chadd rx_next:
758*d542f7f6SAdrian Chadd 	return (is_good);
759*d542f7f6SAdrian Chadd }
760*d542f7f6SAdrian Chadd 
761*d542f7f6SAdrian Chadd void
762*d542f7f6SAdrian Chadd ath_rx_proc(struct ath_softc *sc, int resched)
763*d542f7f6SAdrian Chadd {
764*d542f7f6SAdrian Chadd #define	PA2DESC(_sc, _pa) \
765*d542f7f6SAdrian Chadd 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
766*d542f7f6SAdrian Chadd 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
767*d542f7f6SAdrian Chadd 	struct ath_buf *bf;
768*d542f7f6SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
769*d542f7f6SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
770*d542f7f6SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
771*d542f7f6SAdrian Chadd 	struct ath_desc *ds;
772*d542f7f6SAdrian Chadd 	struct ath_rx_status *rs;
773*d542f7f6SAdrian Chadd 	struct mbuf *m;
774*d542f7f6SAdrian Chadd 	int ngood;
775*d542f7f6SAdrian Chadd 	HAL_STATUS status;
776*d542f7f6SAdrian Chadd 	int16_t nf;
777*d542f7f6SAdrian Chadd 	u_int64_t tsf;
778*d542f7f6SAdrian Chadd 	int npkts = 0;
779*d542f7f6SAdrian Chadd 
780*d542f7f6SAdrian Chadd 	/* XXX we must not hold the ATH_LOCK here */
781*d542f7f6SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
782*d542f7f6SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
783*d542f7f6SAdrian Chadd 
784*d542f7f6SAdrian Chadd 	ATH_PCU_LOCK(sc);
785*d542f7f6SAdrian Chadd 	sc->sc_rxproc_cnt++;
786*d542f7f6SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
787*d542f7f6SAdrian Chadd 
788*d542f7f6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: called\n", __func__);
789*d542f7f6SAdrian Chadd 	ngood = 0;
790*d542f7f6SAdrian Chadd 	nf = ath_hal_getchannoise(ah, sc->sc_curchan);
791*d542f7f6SAdrian Chadd 	sc->sc_stats.ast_rx_noise = nf;
792*d542f7f6SAdrian Chadd 	tsf = ath_hal_gettsf64(ah);
793*d542f7f6SAdrian Chadd 	do {
794*d542f7f6SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
795*d542f7f6SAdrian Chadd 		if (sc->sc_rxslink && bf == NULL) {	/* NB: shouldn't happen */
796*d542f7f6SAdrian Chadd 			if_printf(ifp, "%s: no buffer!\n", __func__);
797*d542f7f6SAdrian Chadd 			break;
798*d542f7f6SAdrian Chadd 		} else if (bf == NULL) {
799*d542f7f6SAdrian Chadd 			/*
800*d542f7f6SAdrian Chadd 			 * End of List:
801*d542f7f6SAdrian Chadd 			 * this can happen for non-self-linked RX chains
802*d542f7f6SAdrian Chadd 			 */
803*d542f7f6SAdrian Chadd 			sc->sc_stats.ast_rx_hitqueueend++;
804*d542f7f6SAdrian Chadd 			break;
805*d542f7f6SAdrian Chadd 		}
806*d542f7f6SAdrian Chadd 		m = bf->bf_m;
807*d542f7f6SAdrian Chadd 		if (m == NULL) {		/* NB: shouldn't happen */
808*d542f7f6SAdrian Chadd 			/*
809*d542f7f6SAdrian Chadd 			 * If mbuf allocation failed previously there
810*d542f7f6SAdrian Chadd 			 * will be no mbuf; try again to re-populate it.
811*d542f7f6SAdrian Chadd 			 */
812*d542f7f6SAdrian Chadd 			/* XXX make debug msg */
813*d542f7f6SAdrian Chadd 			if_printf(ifp, "%s: no mbuf!\n", __func__);
814*d542f7f6SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
815*d542f7f6SAdrian Chadd 			goto rx_proc_next;
816*d542f7f6SAdrian Chadd 		}
817*d542f7f6SAdrian Chadd 		ds = bf->bf_desc;
818*d542f7f6SAdrian Chadd 		if (ds->ds_link == bf->bf_daddr) {
819*d542f7f6SAdrian Chadd 			/* NB: never process the self-linked entry at the end */
820*d542f7f6SAdrian Chadd 			sc->sc_stats.ast_rx_hitqueueend++;
821*d542f7f6SAdrian Chadd 			break;
822*d542f7f6SAdrian Chadd 		}
823*d542f7f6SAdrian Chadd 		/* XXX sync descriptor memory */
824*d542f7f6SAdrian Chadd 		/*
825*d542f7f6SAdrian Chadd 		 * Must provide the virtual address of the current
826*d542f7f6SAdrian Chadd 		 * descriptor, the physical address, and the virtual
827*d542f7f6SAdrian Chadd 		 * address of the next descriptor in the h/w chain.
828*d542f7f6SAdrian Chadd 		 * This allows the HAL to look ahead to see if the
829*d542f7f6SAdrian Chadd 		 * hardware is done with a descriptor by checking the
830*d542f7f6SAdrian Chadd 		 * done bit in the following descriptor and the address
831*d542f7f6SAdrian Chadd 		 * of the current descriptor the DMA engine is working
832*d542f7f6SAdrian Chadd 		 * on.  All this is necessary because of our use of
833*d542f7f6SAdrian Chadd 		 * a self-linked list to avoid rx overruns.
834*d542f7f6SAdrian Chadd 		 */
835*d542f7f6SAdrian Chadd 		rs = &bf->bf_status.ds_rxstat;
836*d542f7f6SAdrian Chadd 		status = ath_hal_rxprocdesc(ah, ds,
837*d542f7f6SAdrian Chadd 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
838*d542f7f6SAdrian Chadd #ifdef ATH_DEBUG
839*d542f7f6SAdrian Chadd 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
840*d542f7f6SAdrian Chadd 			ath_printrxbuf(sc, bf, 0, status == HAL_OK);
841*d542f7f6SAdrian Chadd #endif
842*d542f7f6SAdrian Chadd 		if (status == HAL_EINPROGRESS)
843*d542f7f6SAdrian Chadd 			break;
844*d542f7f6SAdrian Chadd 
845*d542f7f6SAdrian Chadd 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
846*d542f7f6SAdrian Chadd 		npkts++;
847*d542f7f6SAdrian Chadd 
848*d542f7f6SAdrian Chadd 		/*
849*d542f7f6SAdrian Chadd 		 * Process a single frame.
850*d542f7f6SAdrian Chadd 		 */
851*d542f7f6SAdrian Chadd 		if (ath_rx_pkt(sc, rs, status, tsf, nf, bf))
852*d542f7f6SAdrian Chadd 			ngood++;
853*d542f7f6SAdrian Chadd rx_proc_next:
854e60c4fc2SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
855e60c4fc2SAdrian Chadd 	} while (ath_rxbuf_init(sc, bf) == 0);
856e60c4fc2SAdrian Chadd 
857e60c4fc2SAdrian Chadd 	/* rx signal state monitoring */
858e60c4fc2SAdrian Chadd 	ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
859e60c4fc2SAdrian Chadd 	if (ngood)
860e60c4fc2SAdrian Chadd 		sc->sc_lastrx = tsf;
861e60c4fc2SAdrian Chadd 
862e60c4fc2SAdrian Chadd 	CTR2(ATH_KTR_INTR, "ath_rx_proc: npkts=%d, ngood=%d", npkts, ngood);
863e60c4fc2SAdrian Chadd 	/* Queue DFS tasklet if needed */
864e60c4fc2SAdrian Chadd 	if (resched && ath_dfs_tasklet_needed(sc, sc->sc_curchan))
865e60c4fc2SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
866e60c4fc2SAdrian Chadd 
867e60c4fc2SAdrian Chadd 	/*
868e60c4fc2SAdrian Chadd 	 * Now that all the RX frames were handled that
869e60c4fc2SAdrian Chadd 	 * need to be handled, kick the PCU if there's
870e60c4fc2SAdrian Chadd 	 * been an RXEOL condition.
871e60c4fc2SAdrian Chadd 	 */
872e60c4fc2SAdrian Chadd 	ATH_PCU_LOCK(sc);
873e60c4fc2SAdrian Chadd 	if (resched && sc->sc_kickpcu) {
874e60c4fc2SAdrian Chadd 		CTR0(ATH_KTR_ERR, "ath_rx_proc: kickpcu");
875e60c4fc2SAdrian Chadd 		device_printf(sc->sc_dev, "%s: kickpcu; handled %d packets\n",
876e60c4fc2SAdrian Chadd 		    __func__, npkts);
877e60c4fc2SAdrian Chadd 
878e60c4fc2SAdrian Chadd 		/* XXX rxslink? */
879e60c4fc2SAdrian Chadd 		/*
880e60c4fc2SAdrian Chadd 		 * XXX can we hold the PCU lock here?
881e60c4fc2SAdrian Chadd 		 * Are there any net80211 buffer calls involved?
882e60c4fc2SAdrian Chadd 		 */
883e60c4fc2SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
884e60c4fc2SAdrian Chadd 		ath_hal_putrxbuf(ah, bf->bf_daddr);
885e60c4fc2SAdrian Chadd 		ath_hal_rxena(ah);		/* enable recv descriptors */
886e60c4fc2SAdrian Chadd 		ath_mode_init(sc);		/* set filters, etc. */
887e60c4fc2SAdrian Chadd 		ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
888e60c4fc2SAdrian Chadd 
889e60c4fc2SAdrian Chadd 		ath_hal_intrset(ah, sc->sc_imask);
890e60c4fc2SAdrian Chadd 		sc->sc_kickpcu = 0;
891e60c4fc2SAdrian Chadd 	}
892e60c4fc2SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
893e60c4fc2SAdrian Chadd 
894e60c4fc2SAdrian Chadd 	/* XXX check this inside of IF_LOCK? */
895e60c4fc2SAdrian Chadd 	if (resched && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
896e60c4fc2SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
897e60c4fc2SAdrian Chadd 		ieee80211_ff_age_all(ic, 100);
898e60c4fc2SAdrian Chadd #endif
899e60c4fc2SAdrian Chadd 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
900e60c4fc2SAdrian Chadd 			ath_start(ifp);
901e60c4fc2SAdrian Chadd 	}
902e60c4fc2SAdrian Chadd #undef PA2DESC
903e60c4fc2SAdrian Chadd 
904e60c4fc2SAdrian Chadd 	ATH_PCU_LOCK(sc);
905e60c4fc2SAdrian Chadd 	sc->sc_rxproc_cnt--;
906e60c4fc2SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
907e60c4fc2SAdrian Chadd }
908e60c4fc2SAdrian Chadd 
909e60c4fc2SAdrian Chadd /*
910e60c4fc2SAdrian Chadd  * Disable the receive h/w in preparation for a reset.
911e60c4fc2SAdrian Chadd  */
912e60c4fc2SAdrian Chadd void
913e60c4fc2SAdrian Chadd ath_stoprecv(struct ath_softc *sc, int dodelay)
914e60c4fc2SAdrian Chadd {
915e60c4fc2SAdrian Chadd #define	PA2DESC(_sc, _pa) \
916e60c4fc2SAdrian Chadd 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
917e60c4fc2SAdrian Chadd 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
918e60c4fc2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
919e60c4fc2SAdrian Chadd 
920e60c4fc2SAdrian Chadd 	ath_hal_stoppcurecv(ah);	/* disable PCU */
921e60c4fc2SAdrian Chadd 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
922e60c4fc2SAdrian Chadd 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
923e60c4fc2SAdrian Chadd 	/*
924e60c4fc2SAdrian Chadd 	 * TODO: see if this particular DELAY() is required; it may be
925e60c4fc2SAdrian Chadd 	 * masking some missing FIFO flush or DMA sync.
926e60c4fc2SAdrian Chadd 	 */
927e60c4fc2SAdrian Chadd #if 0
928e60c4fc2SAdrian Chadd 	if (dodelay)
929e60c4fc2SAdrian Chadd #endif
930e60c4fc2SAdrian Chadd 		DELAY(3000);		/* 3ms is long enough for 1 frame */
931e60c4fc2SAdrian Chadd #ifdef ATH_DEBUG
932e60c4fc2SAdrian Chadd 	if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
933e60c4fc2SAdrian Chadd 		struct ath_buf *bf;
934e60c4fc2SAdrian Chadd 		u_int ix;
935e60c4fc2SAdrian Chadd 
936e60c4fc2SAdrian Chadd 		device_printf(sc->sc_dev,
937e60c4fc2SAdrian Chadd 		    "%s: rx queue %p, link %p\n",
938e60c4fc2SAdrian Chadd 		    __func__,
939e60c4fc2SAdrian Chadd 		    (caddr_t)(uintptr_t) ath_hal_getrxbuf(ah),
940e60c4fc2SAdrian Chadd 		    sc->sc_rxlink);
941e60c4fc2SAdrian Chadd 		ix = 0;
942e60c4fc2SAdrian Chadd 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
943e60c4fc2SAdrian Chadd 			struct ath_desc *ds = bf->bf_desc;
944e60c4fc2SAdrian Chadd 			struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
945e60c4fc2SAdrian Chadd 			HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
946e60c4fc2SAdrian Chadd 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
947e60c4fc2SAdrian Chadd 			if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
948e60c4fc2SAdrian Chadd 				ath_printrxbuf(sc, bf, ix, status == HAL_OK);
949e60c4fc2SAdrian Chadd 			ix++;
950e60c4fc2SAdrian Chadd 		}
951e60c4fc2SAdrian Chadd 	}
952e60c4fc2SAdrian Chadd #endif
953e60c4fc2SAdrian Chadd 	if (sc->sc_rxpending != NULL) {
954e60c4fc2SAdrian Chadd 		m_freem(sc->sc_rxpending);
955e60c4fc2SAdrian Chadd 		sc->sc_rxpending = NULL;
956e60c4fc2SAdrian Chadd 	}
957e60c4fc2SAdrian Chadd 	sc->sc_rxlink = NULL;		/* just in case */
958e60c4fc2SAdrian Chadd #undef PA2DESC
959e60c4fc2SAdrian Chadd }
960e60c4fc2SAdrian Chadd 
961e60c4fc2SAdrian Chadd /*
962e60c4fc2SAdrian Chadd  * Enable the receive h/w following a reset.
963e60c4fc2SAdrian Chadd  */
964e60c4fc2SAdrian Chadd int
965e60c4fc2SAdrian Chadd ath_startrecv(struct ath_softc *sc)
966e60c4fc2SAdrian Chadd {
967e60c4fc2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
968e60c4fc2SAdrian Chadd 	struct ath_buf *bf;
969e60c4fc2SAdrian Chadd 
970e60c4fc2SAdrian Chadd 	sc->sc_rxlink = NULL;
971e60c4fc2SAdrian Chadd 	sc->sc_rxpending = NULL;
972e60c4fc2SAdrian Chadd 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
973e60c4fc2SAdrian Chadd 		int error = ath_rxbuf_init(sc, bf);
974e60c4fc2SAdrian Chadd 		if (error != 0) {
975e60c4fc2SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_RECV,
976e60c4fc2SAdrian Chadd 				"%s: ath_rxbuf_init failed %d\n",
977e60c4fc2SAdrian Chadd 				__func__, error);
978e60c4fc2SAdrian Chadd 			return error;
979e60c4fc2SAdrian Chadd 		}
980e60c4fc2SAdrian Chadd 	}
981e60c4fc2SAdrian Chadd 
982e60c4fc2SAdrian Chadd 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
983e60c4fc2SAdrian Chadd 	ath_hal_putrxbuf(ah, bf->bf_daddr);
984e60c4fc2SAdrian Chadd 	ath_hal_rxena(ah);		/* enable recv descriptors */
985e60c4fc2SAdrian Chadd 	ath_mode_init(sc);		/* set filters, etc. */
986e60c4fc2SAdrian Chadd 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
987e60c4fc2SAdrian Chadd 	return 0;
988e60c4fc2SAdrian Chadd }
989