xref: /freebsd/sys/dev/ath/if_ath_rx.c (revision add58488d2f31d61ebbe01c6d94df2dba49be0cb)
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>
7676039bc8SGleb Smirnoff #include <net/if_var.h>
77e60c4fc2SAdrian Chadd #include <net/if_dl.h>
78e60c4fc2SAdrian Chadd #include <net/if_media.h>
79e60c4fc2SAdrian Chadd #include <net/if_types.h>
80e60c4fc2SAdrian Chadd #include <net/if_arp.h>
81e60c4fc2SAdrian Chadd #include <net/ethernet.h>
82e60c4fc2SAdrian Chadd #include <net/if_llc.h>
83e60c4fc2SAdrian Chadd 
84e60c4fc2SAdrian Chadd #include <net80211/ieee80211_var.h>
85e60c4fc2SAdrian Chadd #include <net80211/ieee80211_regdomain.h>
86e60c4fc2SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
87e60c4fc2SAdrian Chadd #include <net80211/ieee80211_superg.h>
88e60c4fc2SAdrian Chadd #endif
89e60c4fc2SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
90e60c4fc2SAdrian Chadd #include <net80211/ieee80211_tdma.h>
91e60c4fc2SAdrian Chadd #endif
92e60c4fc2SAdrian Chadd 
93e60c4fc2SAdrian Chadd #include <net/bpf.h>
94e60c4fc2SAdrian Chadd 
95e60c4fc2SAdrian Chadd #ifdef INET
96e60c4fc2SAdrian Chadd #include <netinet/in.h>
97e60c4fc2SAdrian Chadd #include <netinet/if_ether.h>
98e60c4fc2SAdrian Chadd #endif
99e60c4fc2SAdrian Chadd 
100e60c4fc2SAdrian Chadd #include <dev/ath/if_athvar.h>
101e60c4fc2SAdrian Chadd #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
102e60c4fc2SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
103e60c4fc2SAdrian Chadd 
104e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_debug.h>
105e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_misc.h>
106e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_tsf.h>
107e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_tx.h>
108e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_sysctl.h>
109e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_led.h>
110e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_keycache.h>
111e60c4fc2SAdrian Chadd #include <dev/ath/if_ath_rx.h>
112a35dae8dSAdrian Chadd #include <dev/ath/if_ath_beacon.h>
113e60c4fc2SAdrian Chadd #include <dev/ath/if_athdfs.h>
114e60c4fc2SAdrian Chadd 
115e60c4fc2SAdrian Chadd #ifdef ATH_TX99_DIAG
116e60c4fc2SAdrian Chadd #include <dev/ath/ath_tx99/ath_tx99.h>
117e60c4fc2SAdrian Chadd #endif
118e60c4fc2SAdrian Chadd 
119b69b0dccSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
120b69b0dccSAdrian Chadd #include <dev/ath/if_ath_alq.h>
121b69b0dccSAdrian Chadd #endif
122b69b0dccSAdrian Chadd 
123216ca234SAdrian Chadd #include <dev/ath/if_ath_lna_div.h>
124216ca234SAdrian Chadd 
125e60c4fc2SAdrian Chadd /*
126e60c4fc2SAdrian Chadd  * Calculate the receive filter according to the
127e60c4fc2SAdrian Chadd  * operating mode and state:
128e60c4fc2SAdrian Chadd  *
129e60c4fc2SAdrian Chadd  * o always accept unicast, broadcast, and multicast traffic
130e60c4fc2SAdrian Chadd  * o accept PHY error frames when hardware doesn't have MIB support
131e60c4fc2SAdrian Chadd  *   to count and we need them for ANI (sta mode only until recently)
132e60c4fc2SAdrian Chadd  *   and we are not scanning (ANI is disabled)
133e60c4fc2SAdrian Chadd  *   NB: older hal's add rx filter bits out of sight and we need to
134e60c4fc2SAdrian Chadd  *	 blindly preserve them
135e60c4fc2SAdrian Chadd  * o probe request frames are accepted only when operating in
136e60c4fc2SAdrian Chadd  *   hostap, adhoc, mesh, or monitor modes
137e60c4fc2SAdrian Chadd  * o enable promiscuous mode
138e60c4fc2SAdrian Chadd  *   - when in monitor mode
139e60c4fc2SAdrian Chadd  *   - if interface marked PROMISC (assumes bridge setting is filtered)
140e60c4fc2SAdrian Chadd  * o accept beacons:
141e60c4fc2SAdrian Chadd  *   - when operating in station mode for collecting rssi data when
142e60c4fc2SAdrian Chadd  *     the station is otherwise quiet, or
143e60c4fc2SAdrian Chadd  *   - when operating in adhoc mode so the 802.11 layer creates
144e60c4fc2SAdrian Chadd  *     node table entries for peers,
145e60c4fc2SAdrian Chadd  *   - when scanning
146e60c4fc2SAdrian Chadd  *   - when doing s/w beacon miss (e.g. for ap+sta)
147e60c4fc2SAdrian Chadd  *   - when operating in ap mode in 11g to detect overlapping bss that
148e60c4fc2SAdrian Chadd  *     require protection
149e60c4fc2SAdrian Chadd  *   - when operating in mesh mode to detect neighbors
150e60c4fc2SAdrian Chadd  * o accept control frames:
151e60c4fc2SAdrian Chadd  *   - when in monitor mode
152e60c4fc2SAdrian Chadd  * XXX HT protection for 11n
153e60c4fc2SAdrian Chadd  */
154e60c4fc2SAdrian Chadd u_int32_t
155e60c4fc2SAdrian Chadd ath_calcrxfilter(struct ath_softc *sc)
156e60c4fc2SAdrian Chadd {
157e60c4fc2SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
158e60c4fc2SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
159e60c4fc2SAdrian Chadd 	u_int32_t rfilt;
160e60c4fc2SAdrian Chadd 
161e60c4fc2SAdrian Chadd 	rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
162e60c4fc2SAdrian Chadd 	if (!sc->sc_needmib && !sc->sc_scanning)
163e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PHYERR;
164e60c4fc2SAdrian Chadd 	if (ic->ic_opmode != IEEE80211_M_STA)
165e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PROBEREQ;
166e60c4fc2SAdrian Chadd 	/* XXX ic->ic_monvaps != 0? */
167e60c4fc2SAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_MONITOR || (ifp->if_flags & IFF_PROMISC))
168e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PROM;
169f5c30c4eSAdrian Chadd 
170f5c30c4eSAdrian Chadd 	/*
171f5c30c4eSAdrian Chadd 	 * Only listen to all beacons if we're scanning.
172f5c30c4eSAdrian Chadd 	 *
173f5c30c4eSAdrian Chadd 	 * Otherwise we only really need to hear beacons from
174f5c30c4eSAdrian Chadd 	 * our own BSSID.
175f5c30c4eSAdrian Chadd 	 */
176e60c4fc2SAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_STA ||
177f5c30c4eSAdrian Chadd 	    ic->ic_opmode == IEEE80211_M_IBSS || sc->sc_swbmiss) {
178f5c30c4eSAdrian Chadd 		if (sc->sc_do_mybeacon && ! sc->sc_scanning) {
179f5c30c4eSAdrian Chadd 			rfilt |= HAL_RX_FILTER_MYBEACON;
180f5c30c4eSAdrian Chadd 		} else { /* scanning, non-mybeacon chips */
181e60c4fc2SAdrian Chadd 			rfilt |= HAL_RX_FILTER_BEACON;
182f5c30c4eSAdrian Chadd 		}
183f5c30c4eSAdrian Chadd 	}
184f5c30c4eSAdrian Chadd 
185e60c4fc2SAdrian Chadd 	/*
186e60c4fc2SAdrian Chadd 	 * NB: We don't recalculate the rx filter when
187e60c4fc2SAdrian Chadd 	 * ic_protmode changes; otherwise we could do
188e60c4fc2SAdrian Chadd 	 * this only when ic_protmode != NONE.
189e60c4fc2SAdrian Chadd 	 */
190e60c4fc2SAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
191e60c4fc2SAdrian Chadd 	    IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
192e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_BEACON;
193e60c4fc2SAdrian Chadd 
194e60c4fc2SAdrian Chadd 	/*
195e60c4fc2SAdrian Chadd 	 * Enable hardware PS-POLL RX only for hostap mode;
196e60c4fc2SAdrian Chadd 	 * STA mode sends PS-POLL frames but never
197e60c4fc2SAdrian Chadd 	 * receives them.
198e60c4fc2SAdrian Chadd 	 */
199e60c4fc2SAdrian Chadd 	if (ath_hal_getcapability(sc->sc_ah, HAL_CAP_PSPOLL,
200e60c4fc2SAdrian Chadd 	    0, NULL) == HAL_OK &&
201e60c4fc2SAdrian Chadd 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
202e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PSPOLL;
203e60c4fc2SAdrian Chadd 
204e60c4fc2SAdrian Chadd 	if (sc->sc_nmeshvaps) {
205e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_BEACON;
206e60c4fc2SAdrian Chadd 		if (sc->sc_hasbmatch)
207e60c4fc2SAdrian Chadd 			rfilt |= HAL_RX_FILTER_BSSID;
208e60c4fc2SAdrian Chadd 		else
209e60c4fc2SAdrian Chadd 			rfilt |= HAL_RX_FILTER_PROM;
210e60c4fc2SAdrian Chadd 	}
211e60c4fc2SAdrian Chadd 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
212e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_CONTROL;
213e60c4fc2SAdrian Chadd 
214e60c4fc2SAdrian Chadd 	/*
215e60c4fc2SAdrian Chadd 	 * Enable RX of compressed BAR frames only when doing
216e60c4fc2SAdrian Chadd 	 * 802.11n. Required for A-MPDU.
217e60c4fc2SAdrian Chadd 	 */
218e60c4fc2SAdrian Chadd 	if (IEEE80211_IS_CHAN_HT(ic->ic_curchan))
219e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_COMPBAR;
220e60c4fc2SAdrian Chadd 
221e60c4fc2SAdrian Chadd 	/*
222e60c4fc2SAdrian Chadd 	 * Enable radar PHY errors if requested by the
223e60c4fc2SAdrian Chadd 	 * DFS module.
224e60c4fc2SAdrian Chadd 	 */
225e60c4fc2SAdrian Chadd 	if (sc->sc_dodfs)
226e60c4fc2SAdrian Chadd 		rfilt |= HAL_RX_FILTER_PHYRADAR;
227e60c4fc2SAdrian Chadd 
228f29c6bdeSAdrian Chadd 	/*
229f29c6bdeSAdrian Chadd 	 * Enable spectral PHY errors if requested by the
230f29c6bdeSAdrian Chadd 	 * spectral module.
231f29c6bdeSAdrian Chadd 	 */
232f29c6bdeSAdrian Chadd 	if (sc->sc_dospectral)
233f29c6bdeSAdrian Chadd 		rfilt |= HAL_RX_FILTER_PHYRADAR;
234f29c6bdeSAdrian Chadd 
235e60c4fc2SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n",
236e60c4fc2SAdrian Chadd 	    __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags);
237e60c4fc2SAdrian Chadd 	return rfilt;
238e60c4fc2SAdrian Chadd }
239e60c4fc2SAdrian Chadd 
240f8cc9b09SAdrian Chadd static int
241f8cc9b09SAdrian Chadd ath_legacy_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
242e60c4fc2SAdrian Chadd {
243e60c4fc2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
244e60c4fc2SAdrian Chadd 	int error;
245e60c4fc2SAdrian Chadd 	struct mbuf *m;
246e60c4fc2SAdrian Chadd 	struct ath_desc *ds;
247e60c4fc2SAdrian Chadd 
24867aaf739SAdrian Chadd 	/* XXX TODO: ATH_RX_LOCK_ASSERT(sc); */
24967aaf739SAdrian Chadd 
250e60c4fc2SAdrian Chadd 	m = bf->bf_m;
251e60c4fc2SAdrian Chadd 	if (m == NULL) {
252e60c4fc2SAdrian Chadd 		/*
253e60c4fc2SAdrian Chadd 		 * NB: by assigning a page to the rx dma buffer we
254e60c4fc2SAdrian Chadd 		 * implicitly satisfy the Atheros requirement that
255e60c4fc2SAdrian Chadd 		 * this buffer be cache-line-aligned and sized to be
256e60c4fc2SAdrian Chadd 		 * multiple of the cache line size.  Not doing this
257e60c4fc2SAdrian Chadd 		 * causes weird stuff to happen (for the 5210 at least).
258e60c4fc2SAdrian Chadd 		 */
259c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
260e60c4fc2SAdrian Chadd 		if (m == NULL) {
261e60c4fc2SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_ANY,
262e60c4fc2SAdrian Chadd 				"%s: no mbuf/cluster\n", __func__);
263e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_nombuf++;
264e60c4fc2SAdrian Chadd 			return ENOMEM;
265e60c4fc2SAdrian Chadd 		}
266e60c4fc2SAdrian Chadd 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
267e60c4fc2SAdrian Chadd 
268e60c4fc2SAdrian Chadd 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
269e60c4fc2SAdrian Chadd 					     bf->bf_dmamap, m,
270e60c4fc2SAdrian Chadd 					     bf->bf_segs, &bf->bf_nseg,
271e60c4fc2SAdrian Chadd 					     BUS_DMA_NOWAIT);
272e60c4fc2SAdrian Chadd 		if (error != 0) {
273e60c4fc2SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_ANY,
274e60c4fc2SAdrian Chadd 			    "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
275e60c4fc2SAdrian Chadd 			    __func__, error);
276e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_busdma++;
277e60c4fc2SAdrian Chadd 			m_freem(m);
278e60c4fc2SAdrian Chadd 			return error;
279e60c4fc2SAdrian Chadd 		}
280e60c4fc2SAdrian Chadd 		KASSERT(bf->bf_nseg == 1,
281e60c4fc2SAdrian Chadd 			("multi-segment packet; nseg %u", bf->bf_nseg));
282e60c4fc2SAdrian Chadd 		bf->bf_m = m;
283e60c4fc2SAdrian Chadd 	}
284e60c4fc2SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
285e60c4fc2SAdrian Chadd 
286e60c4fc2SAdrian Chadd 	/*
287e60c4fc2SAdrian Chadd 	 * Setup descriptors.  For receive we always terminate
288e60c4fc2SAdrian Chadd 	 * the descriptor list with a self-linked entry so we'll
289e60c4fc2SAdrian Chadd 	 * not get overrun under high load (as can happen with a
290e60c4fc2SAdrian Chadd 	 * 5212 when ANI processing enables PHY error frames).
291e60c4fc2SAdrian Chadd 	 *
292e60c4fc2SAdrian Chadd 	 * To insure the last descriptor is self-linked we create
293e60c4fc2SAdrian Chadd 	 * each descriptor as self-linked and add it to the end.  As
294e60c4fc2SAdrian Chadd 	 * each additional descriptor is added the previous self-linked
295e60c4fc2SAdrian Chadd 	 * entry is ``fixed'' naturally.  This should be safe even
296e60c4fc2SAdrian Chadd 	 * if DMA is happening.  When processing RX interrupts we
297e60c4fc2SAdrian Chadd 	 * never remove/process the last, self-linked, entry on the
298e60c4fc2SAdrian Chadd 	 * descriptor list.  This insures the hardware always has
299e60c4fc2SAdrian Chadd 	 * someplace to write a new frame.
300e60c4fc2SAdrian Chadd 	 */
301e60c4fc2SAdrian Chadd 	/*
302e60c4fc2SAdrian Chadd 	 * 11N: we can no longer afford to self link the last descriptor.
303e60c4fc2SAdrian Chadd 	 * MAC acknowledges BA status as long as it copies frames to host
304e60c4fc2SAdrian Chadd 	 * buffer (or rx fifo). This can incorrectly acknowledge packets
305e60c4fc2SAdrian Chadd 	 * to a sender if last desc is self-linked.
306e60c4fc2SAdrian Chadd 	 */
307e60c4fc2SAdrian Chadd 	ds = bf->bf_desc;
308e60c4fc2SAdrian Chadd 	if (sc->sc_rxslink)
309e60c4fc2SAdrian Chadd 		ds->ds_link = bf->bf_daddr;	/* link to self */
310e60c4fc2SAdrian Chadd 	else
311e60c4fc2SAdrian Chadd 		ds->ds_link = 0;		/* terminate the list */
312e60c4fc2SAdrian Chadd 	ds->ds_data = bf->bf_segs[0].ds_addr;
313e60c4fc2SAdrian Chadd 	ath_hal_setuprxdesc(ah, ds
314e60c4fc2SAdrian Chadd 		, m->m_len		/* buffer size */
315e60c4fc2SAdrian Chadd 		, 0
316e60c4fc2SAdrian Chadd 	);
317e60c4fc2SAdrian Chadd 
318e60c4fc2SAdrian Chadd 	if (sc->sc_rxlink != NULL)
319e60c4fc2SAdrian Chadd 		*sc->sc_rxlink = bf->bf_daddr;
320e60c4fc2SAdrian Chadd 	sc->sc_rxlink = &ds->ds_link;
321e60c4fc2SAdrian Chadd 	return 0;
322e60c4fc2SAdrian Chadd }
323e60c4fc2SAdrian Chadd 
324e60c4fc2SAdrian Chadd /*
325e60c4fc2SAdrian Chadd  * Intercept management frames to collect beacon rssi data
326e60c4fc2SAdrian Chadd  * and to do ibss merges.
327e60c4fc2SAdrian Chadd  */
328e60c4fc2SAdrian Chadd void
329e60c4fc2SAdrian Chadd ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
330e60c4fc2SAdrian Chadd 	int subtype, int rssi, int nf)
331e60c4fc2SAdrian Chadd {
332e60c4fc2SAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
333e60c4fc2SAdrian Chadd 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
334f5c30c4eSAdrian Chadd 	uint64_t tsf_beacon_old, tsf_beacon;
335f5c30c4eSAdrian Chadd 	uint64_t nexttbtt;
336f5c30c4eSAdrian Chadd 	int64_t tsf_delta;
337f5c30c4eSAdrian Chadd 	int32_t tsf_delta_bmiss;
338f5c30c4eSAdrian Chadd 	int32_t tsf_remainder;
339f5c30c4eSAdrian Chadd 	uint64_t tsf_beacon_target;
3408cc3f9c9SAdrian Chadd 	int tsf_intval;
341f5c30c4eSAdrian Chadd 
342f5c30c4eSAdrian Chadd 	tsf_beacon_old = ((uint64_t) LE_READ_4(ni->ni_tstamp.data + 4)) << 32;
343f5c30c4eSAdrian Chadd 	tsf_beacon_old |= LE_READ_4(ni->ni_tstamp.data);
344e60c4fc2SAdrian Chadd 
3458cc3f9c9SAdrian Chadd #define	TU_TO_TSF(_tu)	(((u_int64_t)(_tu)) << 10)
3468cc3f9c9SAdrian Chadd 	tsf_intval = 1;
347*add58488SAdrian Chadd 	if (ni->ni_intval > 0) {
3488cc3f9c9SAdrian Chadd 		tsf_intval = TU_TO_TSF(ni->ni_intval);
3498cc3f9c9SAdrian Chadd 	}
3508cc3f9c9SAdrian Chadd #undef	TU_TO_TSF
3518cc3f9c9SAdrian Chadd 
352e60c4fc2SAdrian Chadd 	/*
353e60c4fc2SAdrian Chadd 	 * Call up first so subsequent work can use information
354e60c4fc2SAdrian Chadd 	 * potentially stored in the node (e.g. for ibss merge).
355e60c4fc2SAdrian Chadd 	 */
356e60c4fc2SAdrian Chadd 	ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
357e60c4fc2SAdrian Chadd 	switch (subtype) {
358e60c4fc2SAdrian Chadd 	case IEEE80211_FC0_SUBTYPE_BEACON:
359e60c4fc2SAdrian Chadd 		/* update rssi statistics for use by the hal */
360e60c4fc2SAdrian Chadd 		/* XXX unlocked check against vap->iv_bss? */
361e60c4fc2SAdrian Chadd 		ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
362f5c30c4eSAdrian Chadd 
363f5c30c4eSAdrian Chadd 		tsf_beacon = ((uint64_t) LE_READ_4(ni->ni_tstamp.data + 4)) << 32;
364f5c30c4eSAdrian Chadd 		tsf_beacon |= LE_READ_4(ni->ni_tstamp.data);
365f5c30c4eSAdrian Chadd 
366f5c30c4eSAdrian Chadd 		nexttbtt = ath_hal_getnexttbtt(sc->sc_ah);
367f5c30c4eSAdrian Chadd 
368f5c30c4eSAdrian Chadd 		/*
369f5c30c4eSAdrian Chadd 		 * Let's calculate the delta and remainder, so we can see
370f5c30c4eSAdrian Chadd 		 * if the beacon timer from the AP is varying by more than
371f5c30c4eSAdrian Chadd 		 * a few TU.  (Which would be a huge, huge problem.)
372f5c30c4eSAdrian Chadd 		 */
373f5c30c4eSAdrian Chadd 		tsf_delta = (long long) tsf_beacon - (long long) tsf_beacon_old;
374f5c30c4eSAdrian Chadd 
3758cc3f9c9SAdrian Chadd 		tsf_delta_bmiss = tsf_delta / tsf_intval;
376f5c30c4eSAdrian Chadd 
377f5c30c4eSAdrian Chadd 		/*
378f5c30c4eSAdrian Chadd 		 * If our delta is greater than half the beacon interval,
379f5c30c4eSAdrian Chadd 		 * let's round the bmiss value up to the next beacon
380f5c30c4eSAdrian Chadd 		 * interval.  Ie, we're running really, really early
381f5c30c4eSAdrian Chadd 		 * on the next beacon.
382f5c30c4eSAdrian Chadd 		 */
3838cc3f9c9SAdrian Chadd 		if (tsf_delta % tsf_intval > (tsf_intval / 2))
384f5c30c4eSAdrian Chadd 			tsf_delta_bmiss ++;
385f5c30c4eSAdrian Chadd 
386f5c30c4eSAdrian Chadd 		tsf_beacon_target = tsf_beacon_old +
3878cc3f9c9SAdrian Chadd 		    (((unsigned long long) tsf_delta_bmiss) * (long long) tsf_intval);
388f5c30c4eSAdrian Chadd 
389f5c30c4eSAdrian Chadd 		/*
3908cc3f9c9SAdrian Chadd 		 * The remainder using '%' is between 0 .. intval-1.
391f5c30c4eSAdrian Chadd 		 * If we're actually running too fast, then the remainder
3928cc3f9c9SAdrian Chadd 		 * will be some large number just under intval-1.
393f5c30c4eSAdrian Chadd 		 * So we need to look at whether we're running
394f5c30c4eSAdrian Chadd 		 * before or after the target beacon interval
395f5c30c4eSAdrian Chadd 		 * and if we are, modify how we do the remainder
396f5c30c4eSAdrian Chadd 		 * calculation.
397f5c30c4eSAdrian Chadd 		 */
398f5c30c4eSAdrian Chadd 		if (tsf_beacon < tsf_beacon_target) {
3998cc3f9c9SAdrian Chadd 			tsf_remainder =
4008cc3f9c9SAdrian Chadd 			    -(tsf_intval - ((tsf_beacon - tsf_beacon_old) % tsf_intval));
401f5c30c4eSAdrian Chadd 		} else {
4028cc3f9c9SAdrian Chadd 			tsf_remainder = (tsf_beacon - tsf_beacon_old) % tsf_intval;
403f5c30c4eSAdrian Chadd 		}
404f5c30c4eSAdrian Chadd 
405f5c30c4eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_BEACON, "%s: old_tsf=%llu, new_tsf=%llu, target_tsf=%llu, delta=%lld, bmiss=%d, remainder=%d\n",
406f5c30c4eSAdrian Chadd 		    __func__,
407f5c30c4eSAdrian Chadd 		    (unsigned long long) tsf_beacon_old,
408f5c30c4eSAdrian Chadd 		    (unsigned long long) tsf_beacon,
409f5c30c4eSAdrian Chadd 		    (unsigned long long) tsf_beacon_target,
410f5c30c4eSAdrian Chadd 		    (long long) tsf_delta,
411f5c30c4eSAdrian Chadd 		    tsf_delta_bmiss,
412f5c30c4eSAdrian Chadd 		    tsf_remainder);
413f5c30c4eSAdrian Chadd 
414f5c30c4eSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_BEACON, "%s: tsf=%llu, nexttbtt=%llu, delta=%d\n",
415f5c30c4eSAdrian Chadd 		    __func__,
416f5c30c4eSAdrian Chadd 		    (unsigned long long) tsf_beacon,
417f5c30c4eSAdrian Chadd 		    (unsigned long long) nexttbtt,
4188cc3f9c9SAdrian Chadd 		    (int32_t) tsf_beacon - (int32_t) nexttbtt + tsf_intval);
419f5c30c4eSAdrian Chadd 
420e60c4fc2SAdrian Chadd 		if (sc->sc_syncbeacon &&
421f5c30c4eSAdrian Chadd 		    ni == vap->iv_bss &&
422f5c30c4eSAdrian Chadd 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) {
423f5c30c4eSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_BEACON,
424f5c30c4eSAdrian Chadd 			    "%s: syncbeacon=1; syncing\n",
425f5c30c4eSAdrian Chadd 			    __func__);
426e60c4fc2SAdrian Chadd 			/*
427e60c4fc2SAdrian Chadd 			 * Resync beacon timers using the tsf of the beacon
428e60c4fc2SAdrian Chadd 			 * frame we just received.
429e60c4fc2SAdrian Chadd 			 */
430e60c4fc2SAdrian Chadd 			ath_beacon_config(sc, vap);
431f5c30c4eSAdrian Chadd 			sc->sc_syncbeacon = 0;
432e60c4fc2SAdrian Chadd 		}
433f5c30c4eSAdrian Chadd 
434f5c30c4eSAdrian Chadd 
435e60c4fc2SAdrian Chadd 		/* fall thru... */
436e60c4fc2SAdrian Chadd 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
437e60c4fc2SAdrian Chadd 		if (vap->iv_opmode == IEEE80211_M_IBSS &&
438e60c4fc2SAdrian Chadd 		    vap->iv_state == IEEE80211_S_RUN) {
439e60c4fc2SAdrian Chadd 			uint32_t rstamp = sc->sc_lastrs->rs_tstamp;
440e60c4fc2SAdrian Chadd 			uint64_t tsf = ath_extend_tsf(sc, rstamp,
441e60c4fc2SAdrian Chadd 				ath_hal_gettsf64(sc->sc_ah));
442e60c4fc2SAdrian Chadd 			/*
443e60c4fc2SAdrian Chadd 			 * Handle ibss merge as needed; check the tsf on the
444e60c4fc2SAdrian Chadd 			 * frame before attempting the merge.  The 802.11 spec
445e60c4fc2SAdrian Chadd 			 * says the station should change it's bssid to match
446e60c4fc2SAdrian Chadd 			 * the oldest station with the same ssid, where oldest
447e60c4fc2SAdrian Chadd 			 * is determined by the tsf.  Note that hardware
448e60c4fc2SAdrian Chadd 			 * reconfiguration happens through callback to
449e60c4fc2SAdrian Chadd 			 * ath_newstate as the state machine will go from
450e60c4fc2SAdrian Chadd 			 * RUN -> RUN when this happens.
451e60c4fc2SAdrian Chadd 			 */
452e60c4fc2SAdrian Chadd 			if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
453e60c4fc2SAdrian Chadd 				DPRINTF(sc, ATH_DEBUG_STATE,
454e60c4fc2SAdrian Chadd 				    "ibss merge, rstamp %u tsf %ju "
455e60c4fc2SAdrian Chadd 				    "tstamp %ju\n", rstamp, (uintmax_t)tsf,
456e60c4fc2SAdrian Chadd 				    (uintmax_t)ni->ni_tstamp.tsf);
457e60c4fc2SAdrian Chadd 				(void) ieee80211_ibss_merge(ni);
458e60c4fc2SAdrian Chadd 			}
459e60c4fc2SAdrian Chadd 		}
460e60c4fc2SAdrian Chadd 		break;
461e60c4fc2SAdrian Chadd 	}
462e60c4fc2SAdrian Chadd }
463e60c4fc2SAdrian Chadd 
464e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
465e1b5ab97SAdrian Chadd static void
466e1b5ab97SAdrian Chadd ath_rx_tap_vendor(struct ifnet *ifp, struct mbuf *m,
467e1b5ab97SAdrian Chadd     const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
468e1b5ab97SAdrian Chadd {
469e1b5ab97SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
470e1b5ab97SAdrian Chadd 
471e1b5ab97SAdrian Chadd 	/* Fill in the extension bitmap */
472e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_ext_bitmap = htole32(1 << ATH_RADIOTAP_VENDOR_HEADER);
473e1b5ab97SAdrian Chadd 
474e1b5ab97SAdrian Chadd 	/* Fill in the vendor header */
475e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_vh.vh_oui[0] = 0x7f;
476e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_vh.vh_oui[1] = 0x03;
477e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_vh.vh_oui[2] = 0x00;
478e1b5ab97SAdrian Chadd 
479e1b5ab97SAdrian Chadd 	/* XXX what should this be? */
480e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_vh.vh_sub_ns = 0;
481e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_vh.vh_skip_len =
482e1b5ab97SAdrian Chadd 	    htole16(sizeof(struct ath_radiotap_vendor_hdr));
483e1b5ab97SAdrian Chadd 
484e1b5ab97SAdrian Chadd 	/* General version info */
485e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.vh_version = 1;
486e1b5ab97SAdrian Chadd 
487e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.vh_rx_chainmask = sc->sc_rxchainmask;
488e1b5ab97SAdrian Chadd 
489e1b5ab97SAdrian Chadd 	/* rssi */
490e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.rssi_ctl[0] = rs->rs_rssi_ctl[0];
491e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.rssi_ctl[1] = rs->rs_rssi_ctl[1];
492e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.rssi_ctl[2] = rs->rs_rssi_ctl[2];
493e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.rssi_ext[0] = rs->rs_rssi_ext[0];
494e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.rssi_ext[1] = rs->rs_rssi_ext[1];
495e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.rssi_ext[2] = rs->rs_rssi_ext[2];
496e1b5ab97SAdrian Chadd 
497e1b5ab97SAdrian Chadd 	/* evm */
498e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.evm[0] = rs->rs_evm0;
499e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.evm[1] = rs->rs_evm1;
500e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.evm[2] = rs->rs_evm2;
5011896b088SAdrian Chadd 	/* These are only populated from the AR9300 or later */
5021896b088SAdrian Chadd 	sc->sc_rx_th.wr_v.evm[3] = rs->rs_evm3;
5031896b088SAdrian Chadd 	sc->sc_rx_th.wr_v.evm[4] = rs->rs_evm4;
504e1b5ab97SAdrian Chadd 
5050e168bb8SAdrian Chadd 	/* direction */
5060e168bb8SAdrian Chadd 	sc->sc_rx_th.wr_v.vh_flags = ATH_VENDOR_PKT_RX;
5070e168bb8SAdrian Chadd 
5080e168bb8SAdrian Chadd 	/* RX rate */
5090e168bb8SAdrian Chadd 	sc->sc_rx_th.wr_v.vh_rx_hwrate = rs->rs_rate;
5100e168bb8SAdrian Chadd 
5110e168bb8SAdrian Chadd 	/* RX flags */
5120e168bb8SAdrian Chadd 	sc->sc_rx_th.wr_v.vh_rs_flags = rs->rs_flags;
5130e168bb8SAdrian Chadd 
5140e168bb8SAdrian Chadd 	if (rs->rs_isaggr)
5150e168bb8SAdrian Chadd 		sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_ISAGGR;
5160e168bb8SAdrian Chadd 	if (rs->rs_moreaggr)
5170e168bb8SAdrian Chadd 		sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_MOREAGGR;
5180e168bb8SAdrian Chadd 
519e1b5ab97SAdrian Chadd 	/* phyerr info */
5200e168bb8SAdrian Chadd 	if (rs->rs_status & HAL_RXERR_PHY) {
521e1b5ab97SAdrian Chadd 		sc->sc_rx_th.wr_v.vh_phyerr_code = rs->rs_phyerr;
5220e168bb8SAdrian Chadd 		sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_RXPHYERR;
5230e168bb8SAdrian Chadd 	} else {
524e1b5ab97SAdrian Chadd 		sc->sc_rx_th.wr_v.vh_phyerr_code = 0xff;
5250e168bb8SAdrian Chadd 	}
526e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.vh_rs_status = rs->rs_status;
527e1b5ab97SAdrian Chadd 	sc->sc_rx_th.wr_v.vh_rssi = rs->rs_rssi;
528e1b5ab97SAdrian Chadd }
529e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
530e1b5ab97SAdrian Chadd 
531e60c4fc2SAdrian Chadd static void
532e60c4fc2SAdrian Chadd ath_rx_tap(struct ifnet *ifp, struct mbuf *m,
533e60c4fc2SAdrian Chadd 	const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
534e60c4fc2SAdrian Chadd {
535e60c4fc2SAdrian Chadd #define	CHAN_HT20	htole32(IEEE80211_CHAN_HT20)
536e60c4fc2SAdrian Chadd #define	CHAN_HT40U	htole32(IEEE80211_CHAN_HT40U)
537e60c4fc2SAdrian Chadd #define	CHAN_HT40D	htole32(IEEE80211_CHAN_HT40D)
538e60c4fc2SAdrian Chadd #define	CHAN_HT		(CHAN_HT20|CHAN_HT40U|CHAN_HT40D)
539e60c4fc2SAdrian Chadd 	struct ath_softc *sc = ifp->if_softc;
540e60c4fc2SAdrian Chadd 	const HAL_RATE_TABLE *rt;
541e60c4fc2SAdrian Chadd 	uint8_t rix;
542e60c4fc2SAdrian Chadd 
543e60c4fc2SAdrian Chadd 	rt = sc->sc_currates;
544e60c4fc2SAdrian Chadd 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
545e60c4fc2SAdrian Chadd 	rix = rt->rateCodeToIndex[rs->rs_rate];
546e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
547e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
548e60c4fc2SAdrian Chadd #ifdef AH_SUPPORT_AR5416
549e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT;
55055caa1dfSAdrian Chadd 	if (rs->rs_status & HAL_RXERR_PHY) {
55155caa1dfSAdrian Chadd 		/*
55255caa1dfSAdrian Chadd 		 * PHY error - make sure the channel flags
55355caa1dfSAdrian Chadd 		 * reflect the actual channel configuration,
55455caa1dfSAdrian Chadd 		 * not the received frame.
55555caa1dfSAdrian Chadd 		 */
556b8f355bfSAdrian Chadd 		if (IEEE80211_IS_CHAN_HT40U(sc->sc_curchan))
55755caa1dfSAdrian Chadd 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
558b8f355bfSAdrian Chadd 		else if (IEEE80211_IS_CHAN_HT40D(sc->sc_curchan))
55955caa1dfSAdrian Chadd 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
560b8f355bfSAdrian Chadd 		else if (IEEE80211_IS_CHAN_HT20(sc->sc_curchan))
56155caa1dfSAdrian Chadd 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
56255caa1dfSAdrian Chadd 	} else if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) {	/* HT rate */
563e60c4fc2SAdrian Chadd 		struct ieee80211com *ic = ifp->if_l2com;
564e60c4fc2SAdrian Chadd 
565e60c4fc2SAdrian Chadd 		if ((rs->rs_flags & HAL_RX_2040) == 0)
566e60c4fc2SAdrian Chadd 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
567e60c4fc2SAdrian Chadd 		else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan))
568e60c4fc2SAdrian Chadd 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
569e60c4fc2SAdrian Chadd 		else
570e60c4fc2SAdrian Chadd 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
571e60c4fc2SAdrian Chadd 		if ((rs->rs_flags & HAL_RX_GI) == 0)
572e60c4fc2SAdrian Chadd 			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
573e60c4fc2SAdrian Chadd 	}
57455caa1dfSAdrian Chadd 
575e60c4fc2SAdrian Chadd #endif
576e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(sc, rs->rs_tstamp, tsf));
577e60c4fc2SAdrian Chadd 	if (rs->rs_status & HAL_RXERR_CRC)
578e60c4fc2SAdrian Chadd 		sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
579e60c4fc2SAdrian Chadd 	/* XXX propagate other error flags from descriptor */
580e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_antnoise = nf;
581e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_antsignal = nf + rs->rs_rssi;
582e60c4fc2SAdrian Chadd 	sc->sc_rx_th.wr_antenna = rs->rs_antenna;
583e60c4fc2SAdrian Chadd #undef CHAN_HT
584e60c4fc2SAdrian Chadd #undef CHAN_HT20
585e60c4fc2SAdrian Chadd #undef CHAN_HT40U
586e60c4fc2SAdrian Chadd #undef CHAN_HT40D
587e60c4fc2SAdrian Chadd }
588e60c4fc2SAdrian Chadd 
589e60c4fc2SAdrian Chadd static void
590e60c4fc2SAdrian Chadd ath_handle_micerror(struct ieee80211com *ic,
591e60c4fc2SAdrian Chadd 	struct ieee80211_frame *wh, int keyix)
592e60c4fc2SAdrian Chadd {
593e60c4fc2SAdrian Chadd 	struct ieee80211_node *ni;
594e60c4fc2SAdrian Chadd 
595e60c4fc2SAdrian Chadd 	/* XXX recheck MIC to deal w/ chips that lie */
596e60c4fc2SAdrian Chadd 	/* XXX discard MIC errors on !data frames */
597e60c4fc2SAdrian Chadd 	ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
598e60c4fc2SAdrian Chadd 	if (ni != NULL) {
599e60c4fc2SAdrian Chadd 		ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix);
600e60c4fc2SAdrian Chadd 		ieee80211_free_node(ni);
601e60c4fc2SAdrian Chadd 	}
602e60c4fc2SAdrian Chadd }
603e60c4fc2SAdrian Chadd 
6048cc724d9SAdrian Chadd /*
6058cc724d9SAdrian Chadd  * Process a single packet.
6068cc724d9SAdrian Chadd  *
6078cc724d9SAdrian Chadd  * The mbuf must already be synced, unmapped and removed from bf->bf_m
6088cc724d9SAdrian Chadd  * by this stage.
6098cc724d9SAdrian Chadd  *
6108cc724d9SAdrian Chadd  * The mbuf must be consumed by this routine - either passed up the
6118cc724d9SAdrian Chadd  * net80211 stack, put on the holding queue, or freed.
6128cc724d9SAdrian Chadd  */
613d434a377SAdrian Chadd int
614d542f7f6SAdrian Chadd ath_rx_pkt(struct ath_softc *sc, struct ath_rx_status *rs, HAL_STATUS status,
6158cc724d9SAdrian Chadd     uint64_t tsf, int nf, HAL_RX_QUEUE qtype, struct ath_buf *bf,
6168cc724d9SAdrian Chadd     struct mbuf *m)
617e60c4fc2SAdrian Chadd {
618d542f7f6SAdrian Chadd 	uint64_t rstamp;
619d542f7f6SAdrian Chadd 	int len, type;
620e60c4fc2SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
621e60c4fc2SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
622e60c4fc2SAdrian Chadd 	struct ieee80211_node *ni;
623d542f7f6SAdrian Chadd 	int is_good = 0;
624d434a377SAdrian Chadd 	struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
625e60c4fc2SAdrian Chadd 
626e60c4fc2SAdrian Chadd 	/*
627e60c4fc2SAdrian Chadd 	 * Calculate the correct 64 bit TSF given
628e60c4fc2SAdrian Chadd 	 * the TSF64 register value and rs_tstamp.
629e60c4fc2SAdrian Chadd 	 */
630e60c4fc2SAdrian Chadd 	rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
631e60c4fc2SAdrian Chadd 
632e60c4fc2SAdrian Chadd 	/* These aren't specifically errors */
633e60c4fc2SAdrian Chadd #ifdef	AH_SUPPORT_AR5416
634e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_GI)
635e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_halfgi++;
636e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_2040)
637e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_2040++;
638e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_DELIM_CRC_PRE)
639e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_pre_crc_err++;
640e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_DELIM_CRC_POST)
641e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_post_crc_err++;
642e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_DECRYPT_BUSY)
643e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_decrypt_busy_err++;
644e60c4fc2SAdrian Chadd 	if (rs->rs_flags & HAL_RX_HI_RX_CHAIN)
645e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_hi_rx_chain++;
6462c47932cSAdrian Chadd 	if (rs->rs_flags & HAL_RX_STBC)
6472c47932cSAdrian Chadd 		sc->sc_stats.ast_rx_stbc++;
648e60c4fc2SAdrian Chadd #endif /* AH_SUPPORT_AR5416 */
649e60c4fc2SAdrian Chadd 
650e60c4fc2SAdrian Chadd 	if (rs->rs_status != 0) {
651e60c4fc2SAdrian Chadd 		if (rs->rs_status & HAL_RXERR_CRC)
652e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_crcerr++;
653e60c4fc2SAdrian Chadd 		if (rs->rs_status & HAL_RXERR_FIFO)
654e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_fifoerr++;
655e60c4fc2SAdrian Chadd 		if (rs->rs_status & HAL_RXERR_PHY) {
656e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_phyerr++;
657e60c4fc2SAdrian Chadd 			/* Process DFS radar events */
658e60c4fc2SAdrian Chadd 			if ((rs->rs_phyerr == HAL_PHYERR_RADAR) ||
659e60c4fc2SAdrian Chadd 			    (rs->rs_phyerr == HAL_PHYERR_FALSE_RADAR_EXT)) {
660e60c4fc2SAdrian Chadd 				/* Now pass it to the radar processing code */
661d77363adSAdrian Chadd 				ath_dfs_process_phy_err(sc, m, rstamp, rs);
662e60c4fc2SAdrian Chadd 			}
663e60c4fc2SAdrian Chadd 
664e60c4fc2SAdrian Chadd 			/* Be suitably paranoid about receiving phy errors out of the stats array bounds */
665e60c4fc2SAdrian Chadd 			if (rs->rs_phyerr < 64)
666e60c4fc2SAdrian Chadd 				sc->sc_stats.ast_rx_phy[rs->rs_phyerr]++;
667e60c4fc2SAdrian Chadd 			goto rx_error;	/* NB: don't count in ierrors */
668e60c4fc2SAdrian Chadd 		}
669e60c4fc2SAdrian Chadd 		if (rs->rs_status & HAL_RXERR_DECRYPT) {
670e60c4fc2SAdrian Chadd 			/*
671e60c4fc2SAdrian Chadd 			 * Decrypt error.  If the error occurred
672e60c4fc2SAdrian Chadd 			 * because there was no hardware key, then
673e60c4fc2SAdrian Chadd 			 * let the frame through so the upper layers
674e60c4fc2SAdrian Chadd 			 * can process it.  This is necessary for 5210
675e60c4fc2SAdrian Chadd 			 * parts which have no way to setup a ``clear''
676e60c4fc2SAdrian Chadd 			 * key cache entry.
677e60c4fc2SAdrian Chadd 			 *
678e60c4fc2SAdrian Chadd 			 * XXX do key cache faulting
679e60c4fc2SAdrian Chadd 			 */
680e60c4fc2SAdrian Chadd 			if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
681e60c4fc2SAdrian Chadd 				goto rx_accept;
682e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_badcrypt++;
683e60c4fc2SAdrian Chadd 		}
684c7f5bb7aSAdrian Chadd 		/*
685c7f5bb7aSAdrian Chadd 		 * Similar as above - if the failure was a keymiss
686c7f5bb7aSAdrian Chadd 		 * just punt it up to the upper layers for now.
687c7f5bb7aSAdrian Chadd 		 */
688c7f5bb7aSAdrian Chadd 		if (rs->rs_status & HAL_RXERR_KEYMISS) {
689c7f5bb7aSAdrian Chadd 			sc->sc_stats.ast_rx_keymiss++;
690c7f5bb7aSAdrian Chadd 			goto rx_accept;
691c7f5bb7aSAdrian Chadd 		}
692e60c4fc2SAdrian Chadd 		if (rs->rs_status & HAL_RXERR_MIC) {
693e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_badmic++;
694e60c4fc2SAdrian Chadd 			/*
695e60c4fc2SAdrian Chadd 			 * Do minimal work required to hand off
696e60c4fc2SAdrian Chadd 			 * the 802.11 header for notification.
697e60c4fc2SAdrian Chadd 			 */
698e60c4fc2SAdrian Chadd 			/* XXX frag's and qos frames */
699e60c4fc2SAdrian Chadd 			len = rs->rs_datalen;
700e60c4fc2SAdrian Chadd 			if (len >= sizeof (struct ieee80211_frame)) {
701e60c4fc2SAdrian Chadd 				ath_handle_micerror(ic,
702e60c4fc2SAdrian Chadd 				    mtod(m, struct ieee80211_frame *),
703e60c4fc2SAdrian Chadd 				    sc->sc_splitmic ?
704e60c4fc2SAdrian Chadd 					rs->rs_keyix-32 : rs->rs_keyix);
705e60c4fc2SAdrian Chadd 			}
706e60c4fc2SAdrian Chadd 		}
707e60c4fc2SAdrian Chadd 		ifp->if_ierrors++;
708e60c4fc2SAdrian Chadd rx_error:
709e60c4fc2SAdrian Chadd 		/*
710e60c4fc2SAdrian Chadd 		 * Cleanup any pending partial frame.
711e60c4fc2SAdrian Chadd 		 */
712d434a377SAdrian Chadd 		if (re->m_rxpending != NULL) {
713d434a377SAdrian Chadd 			m_freem(re->m_rxpending);
714d434a377SAdrian Chadd 			re->m_rxpending = NULL;
715e60c4fc2SAdrian Chadd 		}
716e60c4fc2SAdrian Chadd 		/*
717e60c4fc2SAdrian Chadd 		 * When a tap is present pass error frames
718e60c4fc2SAdrian Chadd 		 * that have been requested.  By default we
719e60c4fc2SAdrian Chadd 		 * pass decrypt+mic errors but others may be
720e60c4fc2SAdrian Chadd 		 * interesting (e.g. crc).
721e60c4fc2SAdrian Chadd 		 */
722e60c4fc2SAdrian Chadd 		if (ieee80211_radiotap_active(ic) &&
723e60c4fc2SAdrian Chadd 		    (rs->rs_status & sc->sc_monpass)) {
724e60c4fc2SAdrian Chadd 			/* NB: bpf needs the mbuf length setup */
725e60c4fc2SAdrian Chadd 			len = rs->rs_datalen;
726e60c4fc2SAdrian Chadd 			m->m_pkthdr.len = m->m_len = len;
727e60c4fc2SAdrian Chadd 			ath_rx_tap(ifp, m, rs, rstamp, nf);
728e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
729e1b5ab97SAdrian Chadd 			ath_rx_tap_vendor(ifp, m, rs, rstamp, nf);
730e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
731e60c4fc2SAdrian Chadd 			ieee80211_radiotap_rx_all(ic, m);
732e60c4fc2SAdrian Chadd 		}
733e60c4fc2SAdrian Chadd 		/* XXX pass MIC errors up for s/w reclaculation */
7348cc724d9SAdrian Chadd 		m_freem(m); m = NULL;
735e60c4fc2SAdrian Chadd 		goto rx_next;
736e60c4fc2SAdrian Chadd 	}
737e60c4fc2SAdrian Chadd rx_accept:
738e60c4fc2SAdrian Chadd 	len = rs->rs_datalen;
739e60c4fc2SAdrian Chadd 	m->m_len = len;
740e60c4fc2SAdrian Chadd 
741e60c4fc2SAdrian Chadd 	if (rs->rs_more) {
742e60c4fc2SAdrian Chadd 		/*
743e60c4fc2SAdrian Chadd 		 * Frame spans multiple descriptors; save
744e60c4fc2SAdrian Chadd 		 * it for the next completed descriptor, it
745e60c4fc2SAdrian Chadd 		 * will be used to construct a jumbogram.
746e60c4fc2SAdrian Chadd 		 */
747d434a377SAdrian Chadd 		if (re->m_rxpending != NULL) {
748e60c4fc2SAdrian Chadd 			/* NB: max frame size is currently 2 clusters */
749e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_toobig++;
750d434a377SAdrian Chadd 			m_freem(re->m_rxpending);
751e60c4fc2SAdrian Chadd 		}
752e60c4fc2SAdrian Chadd 		m->m_pkthdr.rcvif = ifp;
753e60c4fc2SAdrian Chadd 		m->m_pkthdr.len = len;
754d434a377SAdrian Chadd 		re->m_rxpending = m;
7558cc724d9SAdrian Chadd 		m = NULL;
756e60c4fc2SAdrian Chadd 		goto rx_next;
757d434a377SAdrian Chadd 	} else if (re->m_rxpending != NULL) {
758e60c4fc2SAdrian Chadd 		/*
759e60c4fc2SAdrian Chadd 		 * This is the second part of a jumbogram,
760e60c4fc2SAdrian Chadd 		 * chain it to the first mbuf, adjust the
761e60c4fc2SAdrian Chadd 		 * frame length, and clear the rxpending state.
762e60c4fc2SAdrian Chadd 		 */
763d434a377SAdrian Chadd 		re->m_rxpending->m_next = m;
764d434a377SAdrian Chadd 		re->m_rxpending->m_pkthdr.len += len;
765d434a377SAdrian Chadd 		m = re->m_rxpending;
766d434a377SAdrian Chadd 		re->m_rxpending = NULL;
767e60c4fc2SAdrian Chadd 	} else {
768e60c4fc2SAdrian Chadd 		/*
769e60c4fc2SAdrian Chadd 		 * Normal single-descriptor receive; setup
770e60c4fc2SAdrian Chadd 		 * the rcvif and packet length.
771e60c4fc2SAdrian Chadd 		 */
772e60c4fc2SAdrian Chadd 		m->m_pkthdr.rcvif = ifp;
773e60c4fc2SAdrian Chadd 		m->m_pkthdr.len = len;
774e60c4fc2SAdrian Chadd 	}
775e60c4fc2SAdrian Chadd 
776e60c4fc2SAdrian Chadd 	/*
777e60c4fc2SAdrian Chadd 	 * Validate rs->rs_antenna.
778e60c4fc2SAdrian Chadd 	 *
779e60c4fc2SAdrian Chadd 	 * Some users w/ AR9285 NICs have reported crashes
780e60c4fc2SAdrian Chadd 	 * here because rs_antenna field is bogusly large.
781e60c4fc2SAdrian Chadd 	 * Let's enforce the maximum antenna limit of 8
782e60c4fc2SAdrian Chadd 	 * (and it shouldn't be hard coded, but that's a
783e60c4fc2SAdrian Chadd 	 * separate problem) and if there's an issue, print
784e60c4fc2SAdrian Chadd 	 * out an error and adjust rs_antenna to something
785e60c4fc2SAdrian Chadd 	 * sensible.
786e60c4fc2SAdrian Chadd 	 *
787e60c4fc2SAdrian Chadd 	 * This code should be removed once the actual
788e60c4fc2SAdrian Chadd 	 * root cause of the issue has been identified.
789e60c4fc2SAdrian Chadd 	 * For example, it may be that the rs_antenna
790e60c4fc2SAdrian Chadd 	 * field is only valid for the lsat frame of
791e60c4fc2SAdrian Chadd 	 * an aggregate and it just happens that it is
792e60c4fc2SAdrian Chadd 	 * "mostly" right. (This is a general statement -
793e60c4fc2SAdrian Chadd 	 * the majority of the statistics are only valid
794e60c4fc2SAdrian Chadd 	 * for the last frame in an aggregate.
795e60c4fc2SAdrian Chadd 	 */
796e60c4fc2SAdrian Chadd 	if (rs->rs_antenna > 7) {
797e60c4fc2SAdrian Chadd 		device_printf(sc->sc_dev, "%s: rs_antenna > 7 (%d)\n",
798e60c4fc2SAdrian Chadd 		    __func__, rs->rs_antenna);
799e60c4fc2SAdrian Chadd #ifdef	ATH_DEBUG
800e60c4fc2SAdrian Chadd 		ath_printrxbuf(sc, bf, 0, status == HAL_OK);
801e60c4fc2SAdrian Chadd #endif /* ATH_DEBUG */
802e60c4fc2SAdrian Chadd 		rs->rs_antenna = 0;	/* XXX better than nothing */
803e60c4fc2SAdrian Chadd 	}
804e60c4fc2SAdrian Chadd 
8053df7a8abSAdrian Chadd 	/*
8063df7a8abSAdrian Chadd 	 * If this is an AR9285/AR9485, then the receive and LNA
8073df7a8abSAdrian Chadd 	 * configuration is stored in RSSI[2] / EXTRSSI[2].
8083df7a8abSAdrian Chadd 	 * We can extract this out to build a much better
8093df7a8abSAdrian Chadd 	 * receive antenna profile.
8103df7a8abSAdrian Chadd 	 *
8113df7a8abSAdrian Chadd 	 * Yes, this just blurts over the above RX antenna field
8123df7a8abSAdrian Chadd 	 * for now.  It's fine, the AR9285 doesn't really use
8133df7a8abSAdrian Chadd 	 * that.
8143df7a8abSAdrian Chadd 	 *
8153df7a8abSAdrian Chadd 	 * Later on we should store away the fine grained LNA
8163df7a8abSAdrian Chadd 	 * information and keep separate counters just for
8173df7a8abSAdrian Chadd 	 * that.  It'll help when debugging the AR9285/AR9485
8183df7a8abSAdrian Chadd 	 * combined diversity code.
8193df7a8abSAdrian Chadd 	 */
8203df7a8abSAdrian Chadd 	if (sc->sc_rx_lnamixer) {
8213df7a8abSAdrian Chadd 		rs->rs_antenna = 0;
8223df7a8abSAdrian Chadd 
8233df7a8abSAdrian Chadd 		/* Bits 0:1 - the LNA configuration used */
8243df7a8abSAdrian Chadd 		rs->rs_antenna |=
8253df7a8abSAdrian Chadd 		    ((rs->rs_rssi_ctl[2] & HAL_RX_LNA_CFG_USED)
8263df7a8abSAdrian Chadd 		      >> HAL_RX_LNA_CFG_USED_S);
8273df7a8abSAdrian Chadd 
8283df7a8abSAdrian Chadd 		/* Bit 2 - the external RX antenna switch */
8293df7a8abSAdrian Chadd 		if (rs->rs_rssi_ctl[2] & HAL_RX_LNA_EXTCFG)
8303df7a8abSAdrian Chadd 			rs->rs_antenna |= 0x4;
8313df7a8abSAdrian Chadd 	}
8323df7a8abSAdrian Chadd 
833e60c4fc2SAdrian Chadd 	ifp->if_ipackets++;
834e60c4fc2SAdrian Chadd 	sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
835e60c4fc2SAdrian Chadd 
836e60c4fc2SAdrian Chadd 	/*
837e60c4fc2SAdrian Chadd 	 * Populate the rx status block.  When there are bpf
838e60c4fc2SAdrian Chadd 	 * listeners we do the additional work to provide
839e60c4fc2SAdrian Chadd 	 * complete status.  Otherwise we fill in only the
840e60c4fc2SAdrian Chadd 	 * material required by ieee80211_input.  Note that
841e60c4fc2SAdrian Chadd 	 * noise setting is filled in above.
842e60c4fc2SAdrian Chadd 	 */
843e1b5ab97SAdrian Chadd 	if (ieee80211_radiotap_active(ic)) {
844e60c4fc2SAdrian Chadd 		ath_rx_tap(ifp, m, rs, rstamp, nf);
845e1b5ab97SAdrian Chadd #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
846e1b5ab97SAdrian Chadd 		ath_rx_tap_vendor(ifp, m, rs, rstamp, nf);
847e1b5ab97SAdrian Chadd #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
848e1b5ab97SAdrian Chadd 	}
849e60c4fc2SAdrian Chadd 
850e60c4fc2SAdrian Chadd 	/*
851e60c4fc2SAdrian Chadd 	 * From this point on we assume the frame is at least
852e60c4fc2SAdrian Chadd 	 * as large as ieee80211_frame_min; verify that.
853e60c4fc2SAdrian Chadd 	 */
854e60c4fc2SAdrian Chadd 	if (len < IEEE80211_MIN_LEN) {
855e60c4fc2SAdrian Chadd 		if (!ieee80211_radiotap_active(ic)) {
856e60c4fc2SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_RECV,
857e60c4fc2SAdrian Chadd 			    "%s: short packet %d\n", __func__, len);
858e60c4fc2SAdrian Chadd 			sc->sc_stats.ast_rx_tooshort++;
859e60c4fc2SAdrian Chadd 		} else {
860e60c4fc2SAdrian Chadd 			/* NB: in particular this captures ack's */
861e60c4fc2SAdrian Chadd 			ieee80211_radiotap_rx_all(ic, m);
862e60c4fc2SAdrian Chadd 		}
8638cc724d9SAdrian Chadd 		m_freem(m); m = NULL;
864e60c4fc2SAdrian Chadd 		goto rx_next;
865e60c4fc2SAdrian Chadd 	}
866e60c4fc2SAdrian Chadd 
867e60c4fc2SAdrian Chadd 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
868e60c4fc2SAdrian Chadd 		const HAL_RATE_TABLE *rt = sc->sc_currates;
869e60c4fc2SAdrian Chadd 		uint8_t rix = rt->rateCodeToIndex[rs->rs_rate];
870e60c4fc2SAdrian Chadd 
871e60c4fc2SAdrian Chadd 		ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
872e60c4fc2SAdrian Chadd 		    sc->sc_hwmap[rix].ieeerate, rs->rs_rssi);
873e60c4fc2SAdrian Chadd 	}
874e60c4fc2SAdrian Chadd 
875e60c4fc2SAdrian Chadd 	m_adj(m, -IEEE80211_CRC_LEN);
876e60c4fc2SAdrian Chadd 
877e60c4fc2SAdrian Chadd 	/*
878e60c4fc2SAdrian Chadd 	 * Locate the node for sender, track state, and then
879e60c4fc2SAdrian Chadd 	 * pass the (referenced) node up to the 802.11 layer
880e60c4fc2SAdrian Chadd 	 * for its use.
881e60c4fc2SAdrian Chadd 	 */
882e60c4fc2SAdrian Chadd 	ni = ieee80211_find_rxnode_withkey(ic,
883e60c4fc2SAdrian Chadd 		mtod(m, const struct ieee80211_frame_min *),
884e60c4fc2SAdrian Chadd 		rs->rs_keyix == HAL_RXKEYIX_INVALID ?
885e60c4fc2SAdrian Chadd 			IEEE80211_KEYIX_NONE : rs->rs_keyix);
886e60c4fc2SAdrian Chadd 	sc->sc_lastrs = rs;
887e60c4fc2SAdrian Chadd 
888e60c4fc2SAdrian Chadd #ifdef	AH_SUPPORT_AR5416
889e60c4fc2SAdrian Chadd 	if (rs->rs_isaggr)
890e60c4fc2SAdrian Chadd 		sc->sc_stats.ast_rx_agg++;
891e60c4fc2SAdrian Chadd #endif /* AH_SUPPORT_AR5416 */
892e60c4fc2SAdrian Chadd 
893e60c4fc2SAdrian Chadd 	if (ni != NULL) {
894e60c4fc2SAdrian Chadd 		/*
895e60c4fc2SAdrian Chadd 		 * Only punt packets for ampdu reorder processing for
896e60c4fc2SAdrian Chadd 		 * 11n nodes; net80211 enforces that M_AMPDU is only
897e60c4fc2SAdrian Chadd 		 * set for 11n nodes.
898e60c4fc2SAdrian Chadd 		 */
899e60c4fc2SAdrian Chadd 		if (ni->ni_flags & IEEE80211_NODE_HT)
900e60c4fc2SAdrian Chadd 			m->m_flags |= M_AMPDU;
901e60c4fc2SAdrian Chadd 
902e60c4fc2SAdrian Chadd 		/*
903e60c4fc2SAdrian Chadd 		 * Sending station is known, dispatch directly.
904e60c4fc2SAdrian Chadd 		 */
905e60c4fc2SAdrian Chadd 		type = ieee80211_input(ni, m, rs->rs_rssi, nf);
906e60c4fc2SAdrian Chadd 		ieee80211_free_node(ni);
9078cc724d9SAdrian Chadd 		m = NULL;
908e60c4fc2SAdrian Chadd 		/*
909e60c4fc2SAdrian Chadd 		 * Arrange to update the last rx timestamp only for
910e60c4fc2SAdrian Chadd 		 * frames from our ap when operating in station mode.
911e60c4fc2SAdrian Chadd 		 * This assumes the rx key is always setup when
912e60c4fc2SAdrian Chadd 		 * associated.
913e60c4fc2SAdrian Chadd 		 */
914e60c4fc2SAdrian Chadd 		if (ic->ic_opmode == IEEE80211_M_STA &&
915e60c4fc2SAdrian Chadd 		    rs->rs_keyix != HAL_RXKEYIX_INVALID)
916d542f7f6SAdrian Chadd 			is_good = 1;
917e60c4fc2SAdrian Chadd 	} else {
918e60c4fc2SAdrian Chadd 		type = ieee80211_input_all(ic, m, rs->rs_rssi, nf);
9198cc724d9SAdrian Chadd 		m = NULL;
920e60c4fc2SAdrian Chadd 	}
9218cc724d9SAdrian Chadd 
9228cc724d9SAdrian Chadd 	/*
9238cc724d9SAdrian Chadd 	 * At this point we have passed the frame up the stack; thus
9248cc724d9SAdrian Chadd 	 * the mbuf is no longer ours.
9258cc724d9SAdrian Chadd 	 */
9268cc724d9SAdrian Chadd 
927e60c4fc2SAdrian Chadd 	/*
928e60c4fc2SAdrian Chadd 	 * Track rx rssi and do any rx antenna management.
929e60c4fc2SAdrian Chadd 	 */
930e60c4fc2SAdrian Chadd 	ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
931e60c4fc2SAdrian Chadd 	if (sc->sc_diversity) {
932e60c4fc2SAdrian Chadd 		/*
933e60c4fc2SAdrian Chadd 		 * When using fast diversity, change the default rx
934e60c4fc2SAdrian Chadd 		 * antenna if diversity chooses the other antenna 3
935e60c4fc2SAdrian Chadd 		 * times in a row.
936e60c4fc2SAdrian Chadd 		 */
937e60c4fc2SAdrian Chadd 		if (sc->sc_defant != rs->rs_antenna) {
938e60c4fc2SAdrian Chadd 			if (++sc->sc_rxotherant >= 3)
939e60c4fc2SAdrian Chadd 				ath_setdefantenna(sc, rs->rs_antenna);
940e60c4fc2SAdrian Chadd 		} else
941e60c4fc2SAdrian Chadd 			sc->sc_rxotherant = 0;
942e60c4fc2SAdrian Chadd 	}
943e60c4fc2SAdrian Chadd 
944216ca234SAdrian Chadd 	/* Handle slow diversity if enabled */
945216ca234SAdrian Chadd 	if (sc->sc_dolnadiv) {
946216ca234SAdrian Chadd 		ath_lna_rx_comb_scan(sc, rs, ticks, hz);
947216ca234SAdrian Chadd 	}
948e60c4fc2SAdrian Chadd 
949e60c4fc2SAdrian Chadd 	if (sc->sc_softled) {
950e60c4fc2SAdrian Chadd 		/*
951e60c4fc2SAdrian Chadd 		 * Blink for any data frame.  Otherwise do a
952e60c4fc2SAdrian Chadd 		 * heartbeat-style blink when idle.  The latter
953e60c4fc2SAdrian Chadd 		 * is mainly for station mode where we depend on
954e60c4fc2SAdrian Chadd 		 * periodic beacon frames to trigger the poll event.
955e60c4fc2SAdrian Chadd 		 */
956e60c4fc2SAdrian Chadd 		if (type == IEEE80211_FC0_TYPE_DATA) {
957e60c4fc2SAdrian Chadd 			const HAL_RATE_TABLE *rt = sc->sc_currates;
958e60c4fc2SAdrian Chadd 			ath_led_event(sc,
959e60c4fc2SAdrian Chadd 			    rt->rateCodeToIndex[rs->rs_rate]);
960e60c4fc2SAdrian Chadd 		} else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
961e60c4fc2SAdrian Chadd 			ath_led_event(sc, 0);
962e60c4fc2SAdrian Chadd 		}
963e60c4fc2SAdrian Chadd rx_next:
9648cc724d9SAdrian Chadd 	/*
9658cc724d9SAdrian Chadd 	 * Debugging - complain if we didn't NULL the mbuf pointer
9668cc724d9SAdrian Chadd 	 * here.
9678cc724d9SAdrian Chadd 	 */
9688cc724d9SAdrian Chadd 	if (m != NULL) {
9698cc724d9SAdrian Chadd 		device_printf(sc->sc_dev,
9708cc724d9SAdrian Chadd 		    "%s: mbuf %p should've been freed!\n",
9718cc724d9SAdrian Chadd 		    __func__,
9728cc724d9SAdrian Chadd 		    m);
9738cc724d9SAdrian Chadd 	}
974d542f7f6SAdrian Chadd 	return (is_good);
975d542f7f6SAdrian Chadd }
976d542f7f6SAdrian Chadd 
977516f6796SAdrian Chadd #define	ATH_RX_MAX		128
978516f6796SAdrian Chadd 
97967aaf739SAdrian Chadd /*
98067aaf739SAdrian Chadd  * XXX TODO: break out the "get buffers" from "call ath_rx_pkt()" like
98167aaf739SAdrian Chadd  * the EDMA code does.
98267aaf739SAdrian Chadd  *
98367aaf739SAdrian Chadd  * XXX TODO: then, do all of the RX list management stuff inside
98467aaf739SAdrian Chadd  * ATH_RX_LOCK() so we don't end up potentially racing.  The EDMA
98567aaf739SAdrian Chadd  * code is doing it right.
98667aaf739SAdrian Chadd  */
987f8cc9b09SAdrian Chadd static void
988d542f7f6SAdrian Chadd ath_rx_proc(struct ath_softc *sc, int resched)
989d542f7f6SAdrian Chadd {
990d542f7f6SAdrian Chadd #define	PA2DESC(_sc, _pa) \
991d542f7f6SAdrian Chadd 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
992d542f7f6SAdrian Chadd 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
993d542f7f6SAdrian Chadd 	struct ath_buf *bf;
994d542f7f6SAdrian Chadd 	struct ifnet *ifp = sc->sc_ifp;
995d542f7f6SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
996803f0c59SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
997803f0c59SAdrian Chadd 	struct ieee80211com *ic = ifp->if_l2com;
998803f0c59SAdrian Chadd #endif
999d542f7f6SAdrian Chadd 	struct ath_desc *ds;
1000d542f7f6SAdrian Chadd 	struct ath_rx_status *rs;
1001d542f7f6SAdrian Chadd 	struct mbuf *m;
1002d542f7f6SAdrian Chadd 	int ngood;
1003d542f7f6SAdrian Chadd 	HAL_STATUS status;
1004d542f7f6SAdrian Chadd 	int16_t nf;
1005d542f7f6SAdrian Chadd 	u_int64_t tsf;
1006d542f7f6SAdrian Chadd 	int npkts = 0;
1007233af52dSAdrian Chadd 	int kickpcu = 0;
100867aaf739SAdrian Chadd 	int ret;
1009d542f7f6SAdrian Chadd 
1010d542f7f6SAdrian Chadd 	/* XXX we must not hold the ATH_LOCK here */
1011d542f7f6SAdrian Chadd 	ATH_UNLOCK_ASSERT(sc);
1012d542f7f6SAdrian Chadd 	ATH_PCU_UNLOCK_ASSERT(sc);
1013d542f7f6SAdrian Chadd 
1014d542f7f6SAdrian Chadd 	ATH_PCU_LOCK(sc);
1015d542f7f6SAdrian Chadd 	sc->sc_rxproc_cnt++;
1016233af52dSAdrian Chadd 	kickpcu = sc->sc_kickpcu;
1017d542f7f6SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1018d542f7f6SAdrian Chadd 
1019f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1020f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1021f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1022f5c30c4eSAdrian Chadd 
1023d542f7f6SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: called\n", __func__);
1024d542f7f6SAdrian Chadd 	ngood = 0;
1025d542f7f6SAdrian Chadd 	nf = ath_hal_getchannoise(ah, sc->sc_curchan);
1026d542f7f6SAdrian Chadd 	sc->sc_stats.ast_rx_noise = nf;
1027d542f7f6SAdrian Chadd 	tsf = ath_hal_gettsf64(ah);
1028d542f7f6SAdrian Chadd 	do {
1029516f6796SAdrian Chadd 		/*
1030516f6796SAdrian Chadd 		 * Don't process too many packets at a time; give the
1031516f6796SAdrian Chadd 		 * TX thread time to also run - otherwise the TX
1032516f6796SAdrian Chadd 		 * latency can jump by quite a bit, causing throughput
1033516f6796SAdrian Chadd 		 * degredation.
1034516f6796SAdrian Chadd 		 */
1035233af52dSAdrian Chadd 		if (!kickpcu && npkts >= ATH_RX_MAX)
1036516f6796SAdrian Chadd 			break;
1037516f6796SAdrian Chadd 
1038d542f7f6SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
1039d542f7f6SAdrian Chadd 		if (sc->sc_rxslink && bf == NULL) {	/* NB: shouldn't happen */
1040d542f7f6SAdrian Chadd 			if_printf(ifp, "%s: no buffer!\n", __func__);
1041d542f7f6SAdrian Chadd 			break;
1042d542f7f6SAdrian Chadd 		} else if (bf == NULL) {
1043d542f7f6SAdrian Chadd 			/*
1044d542f7f6SAdrian Chadd 			 * End of List:
1045d542f7f6SAdrian Chadd 			 * this can happen for non-self-linked RX chains
1046d542f7f6SAdrian Chadd 			 */
1047d542f7f6SAdrian Chadd 			sc->sc_stats.ast_rx_hitqueueend++;
1048d542f7f6SAdrian Chadd 			break;
1049d542f7f6SAdrian Chadd 		}
1050d542f7f6SAdrian Chadd 		m = bf->bf_m;
1051d542f7f6SAdrian Chadd 		if (m == NULL) {		/* NB: shouldn't happen */
1052d542f7f6SAdrian Chadd 			/*
1053d542f7f6SAdrian Chadd 			 * If mbuf allocation failed previously there
1054d542f7f6SAdrian Chadd 			 * will be no mbuf; try again to re-populate it.
1055d542f7f6SAdrian Chadd 			 */
1056d542f7f6SAdrian Chadd 			/* XXX make debug msg */
1057d542f7f6SAdrian Chadd 			if_printf(ifp, "%s: no mbuf!\n", __func__);
1058d542f7f6SAdrian Chadd 			TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
1059d542f7f6SAdrian Chadd 			goto rx_proc_next;
1060d542f7f6SAdrian Chadd 		}
1061d542f7f6SAdrian Chadd 		ds = bf->bf_desc;
1062d542f7f6SAdrian Chadd 		if (ds->ds_link == bf->bf_daddr) {
1063d542f7f6SAdrian Chadd 			/* NB: never process the self-linked entry at the end */
1064d542f7f6SAdrian Chadd 			sc->sc_stats.ast_rx_hitqueueend++;
1065d542f7f6SAdrian Chadd 			break;
1066d542f7f6SAdrian Chadd 		}
1067d542f7f6SAdrian Chadd 		/* XXX sync descriptor memory */
1068d542f7f6SAdrian Chadd 		/*
1069d542f7f6SAdrian Chadd 		 * Must provide the virtual address of the current
1070d542f7f6SAdrian Chadd 		 * descriptor, the physical address, and the virtual
1071d542f7f6SAdrian Chadd 		 * address of the next descriptor in the h/w chain.
1072d542f7f6SAdrian Chadd 		 * This allows the HAL to look ahead to see if the
1073d542f7f6SAdrian Chadd 		 * hardware is done with a descriptor by checking the
1074d542f7f6SAdrian Chadd 		 * done bit in the following descriptor and the address
1075d542f7f6SAdrian Chadd 		 * of the current descriptor the DMA engine is working
1076d542f7f6SAdrian Chadd 		 * on.  All this is necessary because of our use of
1077d542f7f6SAdrian Chadd 		 * a self-linked list to avoid rx overruns.
1078d542f7f6SAdrian Chadd 		 */
1079d542f7f6SAdrian Chadd 		rs = &bf->bf_status.ds_rxstat;
1080d542f7f6SAdrian Chadd 		status = ath_hal_rxprocdesc(ah, ds,
1081d542f7f6SAdrian Chadd 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
1082d542f7f6SAdrian Chadd #ifdef ATH_DEBUG
1083d542f7f6SAdrian Chadd 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
1084d542f7f6SAdrian Chadd 			ath_printrxbuf(sc, bf, 0, status == HAL_OK);
1085d542f7f6SAdrian Chadd #endif
1086bb327d28SAdrian Chadd 
1087bb327d28SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1088bb327d28SAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS))
1089bb327d28SAdrian Chadd 		    if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS,
1090bb327d28SAdrian Chadd 		    sc->sc_rx_statuslen, (char *) ds);
1091bb327d28SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
1092bb327d28SAdrian Chadd 
1093d542f7f6SAdrian Chadd 		if (status == HAL_EINPROGRESS)
1094d542f7f6SAdrian Chadd 			break;
1095d542f7f6SAdrian Chadd 
1096d542f7f6SAdrian Chadd 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
1097d542f7f6SAdrian Chadd 		npkts++;
1098d542f7f6SAdrian Chadd 
1099d542f7f6SAdrian Chadd 		/*
1100d542f7f6SAdrian Chadd 		 * Process a single frame.
1101d542f7f6SAdrian Chadd 		 */
11028cc724d9SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_POSTREAD);
11038cc724d9SAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
11048cc724d9SAdrian Chadd 		bf->bf_m = NULL;
11058cc724d9SAdrian Chadd 		if (ath_rx_pkt(sc, rs, status, tsf, nf, HAL_RX_QUEUE_HP, bf, m))
1106d542f7f6SAdrian Chadd 			ngood++;
1107d542f7f6SAdrian Chadd rx_proc_next:
110867aaf739SAdrian Chadd 		/*
110967aaf739SAdrian Chadd 		 * If there's a holding buffer, insert that onto
111067aaf739SAdrian Chadd 		 * the RX list; the hardware is now definitely not pointing
111167aaf739SAdrian Chadd 		 * to it now.
111267aaf739SAdrian Chadd 		 */
111367aaf739SAdrian Chadd 		ret = 0;
111467aaf739SAdrian Chadd 		if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf != NULL) {
111567aaf739SAdrian Chadd 			TAILQ_INSERT_TAIL(&sc->sc_rxbuf,
111667aaf739SAdrian Chadd 			    sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf,
111767aaf739SAdrian Chadd 			    bf_list);
111867aaf739SAdrian Chadd 			ret = ath_rxbuf_init(sc,
111967aaf739SAdrian Chadd 			    sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf);
112067aaf739SAdrian Chadd 		}
112167aaf739SAdrian Chadd 		/*
112267aaf739SAdrian Chadd 		 * Next, throw our buffer into the holding entry.  The hardware
112367aaf739SAdrian Chadd 		 * may use the descriptor to read the link pointer before
112467aaf739SAdrian Chadd 		 * DMAing the next descriptor in to write out a packet.
112567aaf739SAdrian Chadd 		 */
112667aaf739SAdrian Chadd 		sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf = bf;
112767aaf739SAdrian Chadd 	} while (ret == 0);
1128e60c4fc2SAdrian Chadd 
1129e60c4fc2SAdrian Chadd 	/* rx signal state monitoring */
1130e60c4fc2SAdrian Chadd 	ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
1131e60c4fc2SAdrian Chadd 	if (ngood)
1132e60c4fc2SAdrian Chadd 		sc->sc_lastrx = tsf;
1133e60c4fc2SAdrian Chadd 
113403682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_RXPROC, 2, "ath_rx_proc: npkts=%d, ngood=%d", npkts, ngood);
1135e60c4fc2SAdrian Chadd 	/* Queue DFS tasklet if needed */
1136e60c4fc2SAdrian Chadd 	if (resched && ath_dfs_tasklet_needed(sc, sc->sc_curchan))
1137e60c4fc2SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
1138e60c4fc2SAdrian Chadd 
1139e60c4fc2SAdrian Chadd 	/*
1140e60c4fc2SAdrian Chadd 	 * Now that all the RX frames were handled that
1141e60c4fc2SAdrian Chadd 	 * need to be handled, kick the PCU if there's
1142e60c4fc2SAdrian Chadd 	 * been an RXEOL condition.
1143e60c4fc2SAdrian Chadd 	 */
11441844ff16SAdrian Chadd 	if (resched && kickpcu) {
1145e60c4fc2SAdrian Chadd 		ATH_PCU_LOCK(sc);
114603682514SAdrian Chadd 		ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_rx_proc: kickpcu");
1147e60c4fc2SAdrian Chadd 		device_printf(sc->sc_dev, "%s: kickpcu; handled %d packets\n",
1148e60c4fc2SAdrian Chadd 		    __func__, npkts);
1149e60c4fc2SAdrian Chadd 
11501844ff16SAdrian Chadd 		/*
11511844ff16SAdrian Chadd 		 * Go through the process of fully tearing down
11521844ff16SAdrian Chadd 		 * the RX buffers and reinitialising them.
11531844ff16SAdrian Chadd 		 *
11541844ff16SAdrian Chadd 		 * There's a hardware bug that causes the RX FIFO
11551844ff16SAdrian Chadd 		 * to get confused under certain conditions and
11561844ff16SAdrian Chadd 		 * constantly write over the same frame, leading
11571844ff16SAdrian Chadd 		 * the RX driver code here to get heavily confused.
11581844ff16SAdrian Chadd 		 */
115967aaf739SAdrian Chadd 		/*
116067aaf739SAdrian Chadd 		 * XXX Has RX DMA stopped enough here to just call
116167aaf739SAdrian Chadd 		 *     ath_startrecv()?
116267aaf739SAdrian Chadd 		 * XXX Do we need to use the holding buffer to restart
116367aaf739SAdrian Chadd 		 *     RX DMA by appending entries to the final
116467aaf739SAdrian Chadd 		 *     descriptor?  Quite likely.
116567aaf739SAdrian Chadd 		 */
11661844ff16SAdrian Chadd #if 1
1167233af52dSAdrian Chadd 		ath_startrecv(sc);
1168233af52dSAdrian Chadd #else
1169e60c4fc2SAdrian Chadd 		/*
11701844ff16SAdrian Chadd 		 * Disabled for now - it'd be nice to be able to do
11711844ff16SAdrian Chadd 		 * this in order to limit the amount of CPU time spent
11721844ff16SAdrian Chadd 		 * reinitialising the RX side (and thus minimise RX
11731844ff16SAdrian Chadd 		 * drops) however there's a hardware issue that
11741844ff16SAdrian Chadd 		 * causes things to get too far out of whack.
11751844ff16SAdrian Chadd 		 */
11761844ff16SAdrian Chadd 		/*
1177e60c4fc2SAdrian Chadd 		 * XXX can we hold the PCU lock here?
1178e60c4fc2SAdrian Chadd 		 * Are there any net80211 buffer calls involved?
1179e60c4fc2SAdrian Chadd 		 */
1180e60c4fc2SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
1181d60a0680SAdrian Chadd 		ath_hal_putrxbuf(ah, bf->bf_daddr, HAL_RX_QUEUE_HP);
1182e60c4fc2SAdrian Chadd 		ath_hal_rxena(ah);		/* enable recv descriptors */
1183e60c4fc2SAdrian Chadd 		ath_mode_init(sc);		/* set filters, etc. */
1184e60c4fc2SAdrian Chadd 		ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
1185233af52dSAdrian Chadd #endif
1186e60c4fc2SAdrian Chadd 
1187e60c4fc2SAdrian Chadd 		ath_hal_intrset(ah, sc->sc_imask);
1188e60c4fc2SAdrian Chadd 		sc->sc_kickpcu = 0;
1189e60c4fc2SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
11901844ff16SAdrian Chadd 	}
1191e60c4fc2SAdrian Chadd 
1192e60c4fc2SAdrian Chadd 	/* XXX check this inside of IF_LOCK? */
1193e60c4fc2SAdrian Chadd 	if (resched && (ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
1194e60c4fc2SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
1195e60c4fc2SAdrian Chadd 		ieee80211_ff_age_all(ic, 100);
1196e60c4fc2SAdrian Chadd #endif
1197e60c4fc2SAdrian Chadd 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
119814d33c7eSAdrian Chadd 			ath_tx_kick(sc);
1199e60c4fc2SAdrian Chadd 	}
1200e60c4fc2SAdrian Chadd #undef PA2DESC
1201e60c4fc2SAdrian Chadd 
1202516f6796SAdrian Chadd 	/*
1203f5c30c4eSAdrian Chadd 	 * Put the hardware to sleep again if we're done with it.
1204f5c30c4eSAdrian Chadd 	 */
1205f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
1206f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
1207f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
1208f5c30c4eSAdrian Chadd 
1209f5c30c4eSAdrian Chadd 	/*
1210516f6796SAdrian Chadd 	 * If we hit the maximum number of frames in this round,
1211516f6796SAdrian Chadd 	 * reschedule for another immediate pass.  This gives
1212516f6796SAdrian Chadd 	 * the TX and TX completion routines time to run, which
1213516f6796SAdrian Chadd 	 * will reduce latency.
1214516f6796SAdrian Chadd 	 */
1215516f6796SAdrian Chadd 	if (npkts >= ATH_RX_MAX)
1216f0db652cSAdrian Chadd 		sc->sc_rx.recv_sched(sc, resched);
1217516f6796SAdrian Chadd 
1218e60c4fc2SAdrian Chadd 	ATH_PCU_LOCK(sc);
1219e60c4fc2SAdrian Chadd 	sc->sc_rxproc_cnt--;
1220e60c4fc2SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1221e60c4fc2SAdrian Chadd }
1222e60c4fc2SAdrian Chadd 
1223516f6796SAdrian Chadd #undef	ATH_RX_MAX
1224516f6796SAdrian Chadd 
1225e60c4fc2SAdrian Chadd /*
1226f8cc9b09SAdrian Chadd  * Only run the RX proc if it's not already running.
1227f8cc9b09SAdrian Chadd  * Since this may get run as part of the reset/flush path,
1228f8cc9b09SAdrian Chadd  * the task can't clash with an existing, running tasklet.
1229f8cc9b09SAdrian Chadd  */
1230f8cc9b09SAdrian Chadd static void
1231f8cc9b09SAdrian Chadd ath_legacy_rx_tasklet(void *arg, int npending)
1232f8cc9b09SAdrian Chadd {
1233f8cc9b09SAdrian Chadd 	struct ath_softc *sc = arg;
1234f8cc9b09SAdrian Chadd 
123503682514SAdrian Chadd 	ATH_KTR(sc, ATH_KTR_RXPROC, 1, "ath_rx_proc: pending=%d", npending);
1236f8cc9b09SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
1237f8cc9b09SAdrian Chadd 	ATH_PCU_LOCK(sc);
1238f8cc9b09SAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
1239f8cc9b09SAdrian Chadd 		device_printf(sc->sc_dev,
1240f8cc9b09SAdrian Chadd 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
1241f8cc9b09SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
1242f8cc9b09SAdrian Chadd 		return;
1243f8cc9b09SAdrian Chadd 	}
1244f8cc9b09SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
1245f8cc9b09SAdrian Chadd 
1246f8cc9b09SAdrian Chadd 	ath_rx_proc(sc, 1);
1247f8cc9b09SAdrian Chadd }
1248f8cc9b09SAdrian Chadd 
1249f8cc9b09SAdrian Chadd static void
1250f8cc9b09SAdrian Chadd ath_legacy_flushrecv(struct ath_softc *sc)
1251f8cc9b09SAdrian Chadd {
1252f8cc9b09SAdrian Chadd 
1253f8cc9b09SAdrian Chadd 	ath_rx_proc(sc, 0);
1254f8cc9b09SAdrian Chadd }
1255f8cc9b09SAdrian Chadd 
125667aaf739SAdrian Chadd static void
125767aaf739SAdrian Chadd ath_legacy_flush_rxpending(struct ath_softc *sc)
125867aaf739SAdrian Chadd {
125967aaf739SAdrian Chadd 
126067aaf739SAdrian Chadd 	/* XXX ATH_RX_LOCK_ASSERT(sc); */
126167aaf739SAdrian Chadd 
126267aaf739SAdrian Chadd 	if (sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending != NULL) {
126367aaf739SAdrian Chadd 		m_freem(sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending);
126467aaf739SAdrian Chadd 		sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending = NULL;
126567aaf739SAdrian Chadd 	}
126667aaf739SAdrian Chadd 	if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending != NULL) {
126767aaf739SAdrian Chadd 		m_freem(sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending);
126867aaf739SAdrian Chadd 		sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending = NULL;
126967aaf739SAdrian Chadd 	}
127067aaf739SAdrian Chadd }
127167aaf739SAdrian Chadd 
127267aaf739SAdrian Chadd static int
127367aaf739SAdrian Chadd ath_legacy_flush_rxholdbf(struct ath_softc *sc)
127467aaf739SAdrian Chadd {
127567aaf739SAdrian Chadd 	struct ath_buf *bf;
127667aaf739SAdrian Chadd 
127767aaf739SAdrian Chadd 	/* XXX ATH_RX_LOCK_ASSERT(sc); */
127867aaf739SAdrian Chadd 	/*
127967aaf739SAdrian Chadd 	 * If there are RX holding buffers, free them here and return
128067aaf739SAdrian Chadd 	 * them to the list.
128167aaf739SAdrian Chadd 	 *
128267aaf739SAdrian Chadd 	 * XXX should just verify that bf->bf_m is NULL, as it must
128367aaf739SAdrian Chadd 	 * be at this point!
128467aaf739SAdrian Chadd 	 */
128567aaf739SAdrian Chadd 	bf = sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf;
128667aaf739SAdrian Chadd 	if (bf != NULL) {
128767aaf739SAdrian Chadd 		if (bf->bf_m != NULL)
128867aaf739SAdrian Chadd 			m_freem(bf->bf_m);
128967aaf739SAdrian Chadd 		bf->bf_m = NULL;
129067aaf739SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
129167aaf739SAdrian Chadd 		(void) ath_rxbuf_init(sc, bf);
129267aaf739SAdrian Chadd 	}
129367aaf739SAdrian Chadd 	sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf = NULL;
129467aaf739SAdrian Chadd 
129567aaf739SAdrian Chadd 	bf = sc->sc_rxedma[HAL_RX_QUEUE_LP].m_holdbf;
129667aaf739SAdrian Chadd 	if (bf != NULL) {
129767aaf739SAdrian Chadd 		if (bf->bf_m != NULL)
129867aaf739SAdrian Chadd 			m_freem(bf->bf_m);
129967aaf739SAdrian Chadd 		bf->bf_m = NULL;
130067aaf739SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
130167aaf739SAdrian Chadd 		(void) ath_rxbuf_init(sc, bf);
130267aaf739SAdrian Chadd 	}
130367aaf739SAdrian Chadd 	sc->sc_rxedma[HAL_RX_QUEUE_LP].m_holdbf = NULL;
130467aaf739SAdrian Chadd 
130567aaf739SAdrian Chadd 	return (0);
130667aaf739SAdrian Chadd }
130767aaf739SAdrian Chadd 
1308f8cc9b09SAdrian Chadd /*
1309e60c4fc2SAdrian Chadd  * Disable the receive h/w in preparation for a reset.
1310e60c4fc2SAdrian Chadd  */
1311f8cc9b09SAdrian Chadd static void
1312f8cc9b09SAdrian Chadd ath_legacy_stoprecv(struct ath_softc *sc, int dodelay)
1313e60c4fc2SAdrian Chadd {
1314e60c4fc2SAdrian Chadd #define	PA2DESC(_sc, _pa) \
1315e60c4fc2SAdrian Chadd 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
1316e60c4fc2SAdrian Chadd 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
1317e60c4fc2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1318e60c4fc2SAdrian Chadd 
131967aaf739SAdrian Chadd 	ATH_RX_LOCK(sc);
132067aaf739SAdrian Chadd 
1321e60c4fc2SAdrian Chadd 	ath_hal_stoppcurecv(ah);	/* disable PCU */
1322e60c4fc2SAdrian Chadd 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
1323e60c4fc2SAdrian Chadd 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
1324e60c4fc2SAdrian Chadd 	/*
1325e60c4fc2SAdrian Chadd 	 * TODO: see if this particular DELAY() is required; it may be
1326e60c4fc2SAdrian Chadd 	 * masking some missing FIFO flush or DMA sync.
1327e60c4fc2SAdrian Chadd 	 */
1328e60c4fc2SAdrian Chadd #if 0
1329e60c4fc2SAdrian Chadd 	if (dodelay)
1330e60c4fc2SAdrian Chadd #endif
1331e60c4fc2SAdrian Chadd 		DELAY(3000);		/* 3ms is long enough for 1 frame */
1332e60c4fc2SAdrian Chadd #ifdef ATH_DEBUG
1333e60c4fc2SAdrian Chadd 	if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
1334e60c4fc2SAdrian Chadd 		struct ath_buf *bf;
1335e60c4fc2SAdrian Chadd 		u_int ix;
1336e60c4fc2SAdrian Chadd 
1337e60c4fc2SAdrian Chadd 		device_printf(sc->sc_dev,
1338e60c4fc2SAdrian Chadd 		    "%s: rx queue %p, link %p\n",
1339e60c4fc2SAdrian Chadd 		    __func__,
1340d60a0680SAdrian Chadd 		    (caddr_t)(uintptr_t) ath_hal_getrxbuf(ah, HAL_RX_QUEUE_HP),
1341e60c4fc2SAdrian Chadd 		    sc->sc_rxlink);
1342e60c4fc2SAdrian Chadd 		ix = 0;
1343e60c4fc2SAdrian Chadd 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
1344e60c4fc2SAdrian Chadd 			struct ath_desc *ds = bf->bf_desc;
1345e60c4fc2SAdrian Chadd 			struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
1346e60c4fc2SAdrian Chadd 			HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
1347e60c4fc2SAdrian Chadd 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
1348e60c4fc2SAdrian Chadd 			if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
1349e60c4fc2SAdrian Chadd 				ath_printrxbuf(sc, bf, ix, status == HAL_OK);
1350e60c4fc2SAdrian Chadd 			ix++;
1351e60c4fc2SAdrian Chadd 		}
1352e60c4fc2SAdrian Chadd 	}
1353e60c4fc2SAdrian Chadd #endif
135467aaf739SAdrian Chadd 
135567aaf739SAdrian Chadd 	(void) ath_legacy_flush_rxpending(sc);
135667aaf739SAdrian Chadd 	(void) ath_legacy_flush_rxholdbf(sc);
135767aaf739SAdrian Chadd 
1358e60c4fc2SAdrian Chadd 	sc->sc_rxlink = NULL;		/* just in case */
135967aaf739SAdrian Chadd 
136067aaf739SAdrian Chadd 	ATH_RX_UNLOCK(sc);
1361e60c4fc2SAdrian Chadd #undef PA2DESC
1362e60c4fc2SAdrian Chadd }
1363e60c4fc2SAdrian Chadd 
1364e60c4fc2SAdrian Chadd /*
136567aaf739SAdrian Chadd  * XXX TODO: something was calling startrecv without calling
136667aaf739SAdrian Chadd  * stoprecv.  Let's figure out what/why.  It was showing up
136767aaf739SAdrian Chadd  * as a mbuf leak (rxpending) and ath_buf leak (holdbf.)
136867aaf739SAdrian Chadd  */
136967aaf739SAdrian Chadd 
137067aaf739SAdrian Chadd /*
1371e60c4fc2SAdrian Chadd  * Enable the receive h/w following a reset.
1372e60c4fc2SAdrian Chadd  */
1373f8cc9b09SAdrian Chadd static int
1374f8cc9b09SAdrian Chadd ath_legacy_startrecv(struct ath_softc *sc)
1375e60c4fc2SAdrian Chadd {
1376e60c4fc2SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
1377e60c4fc2SAdrian Chadd 	struct ath_buf *bf;
1378e60c4fc2SAdrian Chadd 
137967aaf739SAdrian Chadd 	ATH_RX_LOCK(sc);
138067aaf739SAdrian Chadd 
138167aaf739SAdrian Chadd 	/*
138267aaf739SAdrian Chadd 	 * XXX should verify these are already all NULL!
138367aaf739SAdrian Chadd 	 */
1384e60c4fc2SAdrian Chadd 	sc->sc_rxlink = NULL;
138567aaf739SAdrian Chadd 	(void) ath_legacy_flush_rxpending(sc);
138667aaf739SAdrian Chadd 	(void) ath_legacy_flush_rxholdbf(sc);
138767aaf739SAdrian Chadd 
138867aaf739SAdrian Chadd 	/*
138967aaf739SAdrian Chadd 	 * Re-chain all of the buffers in the RX buffer list.
139067aaf739SAdrian Chadd 	 */
1391e60c4fc2SAdrian Chadd 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
1392e60c4fc2SAdrian Chadd 		int error = ath_rxbuf_init(sc, bf);
1393e60c4fc2SAdrian Chadd 		if (error != 0) {
1394e60c4fc2SAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_RECV,
1395e60c4fc2SAdrian Chadd 				"%s: ath_rxbuf_init failed %d\n",
1396e60c4fc2SAdrian Chadd 				__func__, error);
1397e60c4fc2SAdrian Chadd 			return error;
1398e60c4fc2SAdrian Chadd 		}
1399e60c4fc2SAdrian Chadd 	}
1400e60c4fc2SAdrian Chadd 
1401e60c4fc2SAdrian Chadd 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
1402d60a0680SAdrian Chadd 	ath_hal_putrxbuf(ah, bf->bf_daddr, HAL_RX_QUEUE_HP);
1403e60c4fc2SAdrian Chadd 	ath_hal_rxena(ah);		/* enable recv descriptors */
1404e60c4fc2SAdrian Chadd 	ath_mode_init(sc);		/* set filters, etc. */
1405e60c4fc2SAdrian Chadd 	ath_hal_startpcurecv(ah);	/* re-enable PCU/DMA engine */
140667aaf739SAdrian Chadd 
140767aaf739SAdrian Chadd 	ATH_RX_UNLOCK(sc);
1408e60c4fc2SAdrian Chadd 	return 0;
1409e60c4fc2SAdrian Chadd }
1410f8cc9b09SAdrian Chadd 
14113d184db2SAdrian Chadd static int
14123d184db2SAdrian Chadd ath_legacy_dma_rxsetup(struct ath_softc *sc)
14133d184db2SAdrian Chadd {
14143d184db2SAdrian Chadd 	int error;
14153d184db2SAdrian Chadd 
14163d184db2SAdrian Chadd 	error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
14171006fc0cSAdrian Chadd 	    "rx", sizeof(struct ath_desc), ath_rxbuf, 1);
14183d184db2SAdrian Chadd 	if (error != 0)
14193d184db2SAdrian Chadd 		return (error);
14203d184db2SAdrian Chadd 
14213d184db2SAdrian Chadd 	return (0);
14223d184db2SAdrian Chadd }
14233d184db2SAdrian Chadd 
14243d184db2SAdrian Chadd static int
14253d184db2SAdrian Chadd ath_legacy_dma_rxteardown(struct ath_softc *sc)
14263d184db2SAdrian Chadd {
14273d184db2SAdrian Chadd 
14283d184db2SAdrian Chadd 	if (sc->sc_rxdma.dd_desc_len != 0)
14293d184db2SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
14303d184db2SAdrian Chadd 	return (0);
14313d184db2SAdrian Chadd }
1432f8cc9b09SAdrian Chadd 
1433f0db652cSAdrian Chadd static void
1434f0db652cSAdrian Chadd ath_legacy_recv_sched(struct ath_softc *sc, int dosched)
1435f0db652cSAdrian Chadd {
1436f0db652cSAdrian Chadd 
1437f0db652cSAdrian Chadd 	taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1438f0db652cSAdrian Chadd }
1439f0db652cSAdrian Chadd 
1440f0db652cSAdrian Chadd static void
1441f0db652cSAdrian Chadd ath_legacy_recv_sched_queue(struct ath_softc *sc, HAL_RX_QUEUE q,
1442f0db652cSAdrian Chadd     int dosched)
1443f0db652cSAdrian Chadd {
1444f0db652cSAdrian Chadd 
1445f0db652cSAdrian Chadd 	taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1446f0db652cSAdrian Chadd }
1447f0db652cSAdrian Chadd 
1448f8cc9b09SAdrian Chadd void
1449f8cc9b09SAdrian Chadd ath_recv_setup_legacy(struct ath_softc *sc)
1450f8cc9b09SAdrian Chadd {
1451f8cc9b09SAdrian Chadd 
14521006fc0cSAdrian Chadd 	/* Sensible legacy defaults */
1453bb327d28SAdrian Chadd 	/*
1454bb327d28SAdrian Chadd 	 * XXX this should be changed to properly support the
1455bb327d28SAdrian Chadd 	 * exact RX descriptor size for each HAL.
1456bb327d28SAdrian Chadd 	 */
1457bb327d28SAdrian Chadd 	sc->sc_rx_statuslen = sizeof(struct ath_desc);
14581006fc0cSAdrian Chadd 
1459f8cc9b09SAdrian Chadd 	sc->sc_rx.recv_start = ath_legacy_startrecv;
1460f8cc9b09SAdrian Chadd 	sc->sc_rx.recv_stop = ath_legacy_stoprecv;
1461f8cc9b09SAdrian Chadd 	sc->sc_rx.recv_flush = ath_legacy_flushrecv;
1462f8cc9b09SAdrian Chadd 	sc->sc_rx.recv_tasklet = ath_legacy_rx_tasklet;
1463f8cc9b09SAdrian Chadd 	sc->sc_rx.recv_rxbuf_init = ath_legacy_rxbuf_init;
14643d184db2SAdrian Chadd 
14653d184db2SAdrian Chadd 	sc->sc_rx.recv_setup = ath_legacy_dma_rxsetup;
14663d184db2SAdrian Chadd 	sc->sc_rx.recv_teardown = ath_legacy_dma_rxteardown;
1467f0db652cSAdrian Chadd 	sc->sc_rx.recv_sched = ath_legacy_recv_sched;
1468f0db652cSAdrian Chadd 	sc->sc_rx.recv_sched_queue = ath_legacy_recv_sched_queue;
1469f8cc9b09SAdrian Chadd }
1470