xref: /freebsd/sys/dev/ath/if_ath_rx.c (revision f99e4a2d11f051c280ce97f0feb06463a9e8c23d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 /*
36  * Driver for the Atheros Wireless LAN controller.
37  *
38  * This software is derived from work of Atsushi Onoe; his contribution
39  * is greatly appreciated.
40  */
41 
42 #include "opt_inet.h"
43 #include "opt_ath.h"
44 /*
45  * This is needed for register operations which are performed
46  * by the driver - eg, calls to ath_hal_gettsf32().
47  *
48  * It's also required for any AH_DEBUG checks in here, eg the
49  * module dependencies.
50  */
51 #include "opt_ah.h"
52 #include "opt_wlan.h"
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/sysctl.h>
57 #include <sys/mbuf.h>
58 #include <sys/malloc.h>
59 #include <sys/lock.h>
60 #include <sys/mutex.h>
61 #include <sys/kernel.h>
62 #include <sys/socket.h>
63 #include <sys/sockio.h>
64 #include <sys/errno.h>
65 #include <sys/callout.h>
66 #include <sys/bus.h>
67 #include <sys/endian.h>
68 #include <sys/kthread.h>
69 #include <sys/taskqueue.h>
70 #include <sys/priv.h>
71 #include <sys/module.h>
72 #include <sys/ktr.h>
73 #include <sys/smp.h>	/* for mp_ncpus */
74 
75 #include <machine/bus.h>
76 
77 #include <net/if.h>
78 #include <net/if_var.h>
79 #include <net/if_dl.h>
80 #include <net/if_media.h>
81 #include <net/if_types.h>
82 #include <net/if_arp.h>
83 #include <net/ethernet.h>
84 #include <net/if_llc.h>
85 
86 #include <net80211/ieee80211_var.h>
87 #include <net80211/ieee80211_regdomain.h>
88 #ifdef IEEE80211_SUPPORT_SUPERG
89 #include <net80211/ieee80211_superg.h>
90 #endif
91 #ifdef IEEE80211_SUPPORT_TDMA
92 #include <net80211/ieee80211_tdma.h>
93 #endif
94 
95 #include <net/bpf.h>
96 
97 #ifdef INET
98 #include <netinet/in.h>
99 #include <netinet/if_ether.h>
100 #endif
101 
102 #include <dev/ath/if_athvar.h>
103 #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
104 #include <dev/ath/ath_hal/ah_diagcodes.h>
105 
106 #include <dev/ath/if_ath_debug.h>
107 #include <dev/ath/if_ath_misc.h>
108 #include <dev/ath/if_ath_tsf.h>
109 #include <dev/ath/if_ath_tx.h>
110 #include <dev/ath/if_ath_sysctl.h>
111 #include <dev/ath/if_ath_led.h>
112 #include <dev/ath/if_ath_keycache.h>
113 #include <dev/ath/if_ath_rx.h>
114 #include <dev/ath/if_ath_beacon.h>
115 #include <dev/ath/if_athdfs.h>
116 #include <dev/ath/if_ath_descdma.h>
117 
118 #ifdef ATH_TX99_DIAG
119 #include <dev/ath/ath_tx99/ath_tx99.h>
120 #endif
121 
122 #ifdef	ATH_DEBUG_ALQ
123 #include <dev/ath/if_ath_alq.h>
124 #endif
125 
126 #include <dev/ath/if_ath_lna_div.h>
127 
128 /*
129  * Calculate the receive filter according to the
130  * operating mode and state:
131  *
132  * o always accept unicast, broadcast, and multicast traffic
133  * o accept PHY error frames when hardware doesn't have MIB support
134  *   to count and we need them for ANI (sta mode only until recently)
135  *   and we are not scanning (ANI is disabled)
136  *   NB: older hal's add rx filter bits out of sight and we need to
137  *	 blindly preserve them
138  * o probe request frames are accepted only when operating in
139  *   hostap, adhoc, mesh, or monitor modes
140  * o enable promiscuous mode
141  *   - when in monitor mode
142  *   - if interface marked PROMISC (assumes bridge setting is filtered)
143  * o accept beacons:
144  *   - when operating in station mode for collecting rssi data when
145  *     the station is otherwise quiet, or
146  *   - when operating in adhoc mode so the 802.11 layer creates
147  *     node table entries for peers,
148  *   - when scanning
149  *   - when doing s/w beacon miss (e.g. for ap+sta)
150  *   - when operating in ap mode in 11g to detect overlapping bss that
151  *     require protection
152  *   - when operating in mesh mode to detect neighbors
153  * o accept control frames:
154  *   - when in monitor mode
155  * XXX HT protection for 11n
156  */
157 u_int32_t
158 ath_calcrxfilter(struct ath_softc *sc)
159 {
160 	struct ieee80211com *ic = &sc->sc_ic;
161 	u_int32_t rfilt;
162 
163 	rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
164 	if (!sc->sc_needmib && !sc->sc_scanning)
165 		rfilt |= HAL_RX_FILTER_PHYERR;
166 	if (ic->ic_opmode != IEEE80211_M_STA)
167 		rfilt |= HAL_RX_FILTER_PROBEREQ;
168 	/* XXX ic->ic_monvaps != 0? */
169 	if (ic->ic_opmode == IEEE80211_M_MONITOR || ic->ic_promisc > 0)
170 		rfilt |= HAL_RX_FILTER_PROM;
171 
172 	/*
173 	 * Only listen to all beacons if we're scanning.
174 	 *
175 	 * Otherwise we only really need to hear beacons from
176 	 * our own BSSID.
177 	 *
178 	 * IBSS? software beacon miss? Just receive all beacons.
179 	 * We need to hear beacons/probe requests from everyone so
180 	 * we can merge ibss.
181 	 */
182 	if (ic->ic_opmode == IEEE80211_M_IBSS || sc->sc_swbmiss) {
183 		rfilt |= HAL_RX_FILTER_BEACON;
184 	} else if (ic->ic_opmode == IEEE80211_M_STA) {
185 		if (sc->sc_do_mybeacon && ! sc->sc_scanning) {
186 			rfilt |= HAL_RX_FILTER_MYBEACON;
187 		} else { /* scanning, non-mybeacon chips */
188 			rfilt |= HAL_RX_FILTER_BEACON;
189 		}
190 	}
191 
192 	/*
193 	 * NB: We don't recalculate the rx filter when
194 	 * ic_protmode changes; otherwise we could do
195 	 * this only when ic_protmode != NONE.
196 	 */
197 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
198 	    IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
199 		rfilt |= HAL_RX_FILTER_BEACON;
200 
201 	/*
202 	 * Enable hardware PS-POLL RX only for hostap mode;
203 	 * STA mode sends PS-POLL frames but never
204 	 * receives them.
205 	 */
206 	if (ath_hal_getcapability(sc->sc_ah, HAL_CAP_PSPOLL,
207 	    0, NULL) == HAL_OK &&
208 	    ic->ic_opmode == IEEE80211_M_HOSTAP)
209 		rfilt |= HAL_RX_FILTER_PSPOLL;
210 
211 	if (sc->sc_nmeshvaps) {
212 		rfilt |= HAL_RX_FILTER_BEACON;
213 		if (sc->sc_hasbmatch)
214 			rfilt |= HAL_RX_FILTER_BSSID;
215 		else
216 			rfilt |= HAL_RX_FILTER_PROM;
217 	}
218 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
219 		rfilt |= HAL_RX_FILTER_CONTROL;
220 
221 	/*
222 	 * Enable RX of compressed BAR frames only when doing
223 	 * 802.11n. Required for A-MPDU.
224 	 */
225 	if (IEEE80211_IS_CHAN_HT(ic->ic_curchan))
226 		rfilt |= HAL_RX_FILTER_COMPBAR;
227 
228 	/*
229 	 * Enable radar PHY errors if requested by the
230 	 * DFS module.
231 	 */
232 	if (sc->sc_dodfs)
233 		rfilt |= HAL_RX_FILTER_PHYRADAR;
234 
235 	/*
236 	 * Enable spectral PHY errors if requested by the
237 	 * spectral module.
238 	 */
239 	if (sc->sc_dospectral)
240 		rfilt |= HAL_RX_FILTER_PHYRADAR;
241 
242 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s\n",
243 	    __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode]);
244 	return rfilt;
245 }
246 
247 static int
248 ath_legacy_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
249 {
250 	struct ath_hal *ah = sc->sc_ah;
251 	int error;
252 	struct mbuf *m;
253 	struct ath_desc *ds;
254 
255 	/* XXX TODO: ATH_RX_LOCK_ASSERT(sc); */
256 
257 	m = bf->bf_m;
258 	if (m == NULL) {
259 		/*
260 		 * NB: by assigning a page to the rx dma buffer we
261 		 * implicitly satisfy the Atheros requirement that
262 		 * this buffer be cache-line-aligned and sized to be
263 		 * multiple of the cache line size.  Not doing this
264 		 * causes weird stuff to happen (for the 5210 at least).
265 		 */
266 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
267 		if (m == NULL) {
268 			DPRINTF(sc, ATH_DEBUG_ANY,
269 				"%s: no mbuf/cluster\n", __func__);
270 			sc->sc_stats.ast_rx_nombuf++;
271 			return ENOMEM;
272 		}
273 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
274 
275 		error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
276 					     bf->bf_dmamap, m,
277 					     bf->bf_segs, &bf->bf_nseg,
278 					     BUS_DMA_NOWAIT);
279 		if (error != 0) {
280 			DPRINTF(sc, ATH_DEBUG_ANY,
281 			    "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
282 			    __func__, error);
283 			sc->sc_stats.ast_rx_busdma++;
284 			m_freem(m);
285 			return error;
286 		}
287 		KASSERT(bf->bf_nseg == 1,
288 			("multi-segment packet; nseg %u", bf->bf_nseg));
289 		bf->bf_m = m;
290 	}
291 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
292 
293 	/*
294 	 * Setup descriptors.  For receive we always terminate
295 	 * the descriptor list with a self-linked entry so we'll
296 	 * not get overrun under high load (as can happen with a
297 	 * 5212 when ANI processing enables PHY error frames).
298 	 *
299 	 * To insure the last descriptor is self-linked we create
300 	 * each descriptor as self-linked and add it to the end.  As
301 	 * each additional descriptor is added the previous self-linked
302 	 * entry is ``fixed'' naturally.  This should be safe even
303 	 * if DMA is happening.  When processing RX interrupts we
304 	 * never remove/process the last, self-linked, entry on the
305 	 * descriptor list.  This insures the hardware always has
306 	 * someplace to write a new frame.
307 	 */
308 	/*
309 	 * 11N: we can no longer afford to self link the last descriptor.
310 	 * MAC acknowledges BA status as long as it copies frames to host
311 	 * buffer (or rx fifo). This can incorrectly acknowledge packets
312 	 * to a sender if last desc is self-linked.
313 	 */
314 	ds = bf->bf_desc;
315 	if (sc->sc_rxslink)
316 		ds->ds_link = bf->bf_daddr;	/* link to self */
317 	else
318 		ds->ds_link = 0;		/* terminate the list */
319 	ds->ds_data = bf->bf_segs[0].ds_addr;
320 	ath_hal_setuprxdesc(ah, ds
321 		, m->m_len		/* buffer size */
322 		, 0
323 	);
324 
325 	if (sc->sc_rxlink != NULL)
326 		*sc->sc_rxlink = bf->bf_daddr;
327 	sc->sc_rxlink = &ds->ds_link;
328 	return 0;
329 }
330 
331 /*
332  * Intercept management frames to collect beacon rssi data
333  * and to do ibss merges.
334  */
335 void
336 ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
337 	int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
338 {
339 	struct ieee80211vap *vap = ni->ni_vap;
340 	struct ath_softc *sc = vap->iv_ic->ic_softc;
341 	uint64_t tsf_beacon_old, tsf_beacon;
342 	uint64_t nexttbtt;
343 	int64_t tsf_delta;
344 	int32_t tsf_delta_bmiss;
345 	int32_t tsf_remainder;
346 	uint64_t tsf_beacon_target;
347 	int tsf_intval;
348 
349 	tsf_beacon_old = ((uint64_t) le32dec(ni->ni_tstamp.data + 4)) << 32;
350 	tsf_beacon_old |= le32dec(ni->ni_tstamp.data);
351 
352 #define	TU_TO_TSF(_tu)	(((u_int64_t)(_tu)) << 10)
353 	tsf_intval = 1;
354 	if (ni->ni_intval > 0) {
355 		tsf_intval = TU_TO_TSF(ni->ni_intval);
356 	}
357 #undef	TU_TO_TSF
358 
359 	/*
360 	 * Call up first so subsequent work can use information
361 	 * potentially stored in the node (e.g. for ibss merge).
362 	 */
363 	ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
364 	switch (subtype) {
365 	case IEEE80211_FC0_SUBTYPE_BEACON:
366 		/*
367 		 * Always update the per-node beacon RSSI if we're hearing
368 		 * beacons from that node.
369 		 */
370 		ATH_RSSI_LPF(ATH_NODE(ni)->an_node_stats.ns_avgbrssi, rssi);
371 
372 		/*
373 		 * Only do the following processing if it's for
374 		 * the current BSS.
375 		 *
376 		 * In scan and IBSS mode we receive all beacons,
377 		 * which means we need to filter out stuff
378 		 * that isn't for us or we'll end up constantly
379 		 * trying to sync / merge to BSSes that aren't
380 		 * actually us.
381 		 */
382 		if (IEEE80211_ADDR_EQ(ni->ni_bssid, vap->iv_bss->ni_bssid)) {
383 			/* update rssi statistics for use by the hal */
384 			/* XXX unlocked check against vap->iv_bss? */
385 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
386 
387 
388 			tsf_beacon = ((uint64_t) le32dec(ni->ni_tstamp.data + 4)) << 32;
389 			tsf_beacon |= le32dec(ni->ni_tstamp.data);
390 
391 			nexttbtt = ath_hal_getnexttbtt(sc->sc_ah);
392 
393 			/*
394 			 * Let's calculate the delta and remainder, so we can see
395 			 * if the beacon timer from the AP is varying by more than
396 			 * a few TU.  (Which would be a huge, huge problem.)
397 			 */
398 			tsf_delta = (long long) tsf_beacon - (long long) tsf_beacon_old;
399 
400 			tsf_delta_bmiss = tsf_delta / tsf_intval;
401 
402 			/*
403 			 * If our delta is greater than half the beacon interval,
404 			 * let's round the bmiss value up to the next beacon
405 			 * interval.  Ie, we're running really, really early
406 			 * on the next beacon.
407 			 */
408 			if (tsf_delta % tsf_intval > (tsf_intval / 2))
409 				tsf_delta_bmiss ++;
410 
411 			tsf_beacon_target = tsf_beacon_old +
412 			    (((unsigned long long) tsf_delta_bmiss) * (long long) tsf_intval);
413 
414 			/*
415 			 * The remainder using '%' is between 0 .. intval-1.
416 			 * If we're actually running too fast, then the remainder
417 			 * will be some large number just under intval-1.
418 			 * So we need to look at whether we're running
419 			 * before or after the target beacon interval
420 			 * and if we are, modify how we do the remainder
421 			 * calculation.
422 			 */
423 			if (tsf_beacon < tsf_beacon_target) {
424 				tsf_remainder =
425 				    -(tsf_intval - ((tsf_beacon - tsf_beacon_old) % tsf_intval));
426 			} else {
427 				tsf_remainder = (tsf_beacon - tsf_beacon_old) % tsf_intval;
428 			}
429 
430 			DPRINTF(sc, ATH_DEBUG_BEACON, "%s: old_tsf=%llu (%u), new_tsf=%llu (%u), target_tsf=%llu (%u), delta=%lld, bmiss=%d, remainder=%d\n",
431 			    __func__,
432 			    (unsigned long long) tsf_beacon_old,
433 			    (unsigned int) (tsf_beacon_old >> 10),
434 			    (unsigned long long) tsf_beacon,
435 			    (unsigned int ) (tsf_beacon >> 10),
436 			    (unsigned long long) tsf_beacon_target,
437 			    (unsigned int) (tsf_beacon_target >> 10),
438 			    (long long) tsf_delta,
439 			    tsf_delta_bmiss,
440 			    tsf_remainder);
441 
442 			DPRINTF(sc, ATH_DEBUG_BEACON, "%s: tsf=%llu (%u), nexttbtt=%llu (%u), delta=%d\n",
443 			    __func__,
444 			    (unsigned long long) tsf_beacon,
445 			    (unsigned int) (tsf_beacon >> 10),
446 			    (unsigned long long) nexttbtt,
447 			    (unsigned int) (nexttbtt >> 10),
448 			    (int32_t) tsf_beacon - (int32_t) nexttbtt + tsf_intval);
449 
450 			/* We only do syncbeacon on STA VAPs; not on IBSS */
451 			if (vap->iv_opmode == IEEE80211_M_STA &&
452 			    sc->sc_syncbeacon &&
453 			    ni == vap->iv_bss &&
454 			    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) {
455 				DPRINTF(sc, ATH_DEBUG_BEACON,
456 				    "%s: syncbeacon=1; syncing\n",
457 				    __func__);
458 				/*
459 				 * Resync beacon timers using the tsf of the beacon
460 				 * frame we just received.
461 				 */
462 				ath_beacon_config(sc, vap);
463 				sc->sc_syncbeacon = 0;
464 			}
465 		}
466 
467 		/* fall thru... */
468 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
469 		if (vap->iv_opmode == IEEE80211_M_IBSS &&
470 		    vap->iv_state == IEEE80211_S_RUN &&
471 		    ieee80211_ibss_merge_check(ni)) {
472 			uint32_t rstamp = sc->sc_lastrs->rs_tstamp;
473 			uint64_t tsf = ath_extend_tsf(sc, rstamp,
474 				ath_hal_gettsf64(sc->sc_ah));
475 			/*
476 			 * Handle ibss merge as needed; check the tsf on the
477 			 * frame before attempting the merge.  The 802.11 spec
478 			 * says the station should change it's bssid to match
479 			 * the oldest station with the same ssid, where oldest
480 			 * is determined by the tsf.  Note that hardware
481 			 * reconfiguration happens through callback to
482 			 * ath_newstate as the state machine will go from
483 			 * RUN -> RUN when this happens.
484 			 */
485 			if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
486 				DPRINTF(sc, ATH_DEBUG_STATE,
487 				    "ibss merge, rstamp %u tsf %ju "
488 				    "tstamp %ju\n", rstamp, (uintmax_t)tsf,
489 				    (uintmax_t)ni->ni_tstamp.tsf);
490 				(void) ieee80211_ibss_merge(ni);
491 			}
492 		}
493 		break;
494 	}
495 }
496 
497 #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
498 static void
499 ath_rx_tap_vendor(struct ath_softc *sc, struct mbuf *m,
500     const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
501 {
502 
503 	/* Fill in the extension bitmap */
504 	sc->sc_rx_th.wr_ext_bitmap = htole32(1 << ATH_RADIOTAP_VENDOR_HEADER);
505 
506 	/* Fill in the vendor header */
507 	sc->sc_rx_th.wr_vh.vh_oui[0] = 0x7f;
508 	sc->sc_rx_th.wr_vh.vh_oui[1] = 0x03;
509 	sc->sc_rx_th.wr_vh.vh_oui[2] = 0x00;
510 
511 	/* XXX what should this be? */
512 	sc->sc_rx_th.wr_vh.vh_sub_ns = 0;
513 	sc->sc_rx_th.wr_vh.vh_skip_len =
514 	    htole16(sizeof(struct ath_radiotap_vendor_hdr));
515 
516 	/* General version info */
517 	sc->sc_rx_th.wr_v.vh_version = 1;
518 
519 	sc->sc_rx_th.wr_v.vh_rx_chainmask = sc->sc_rxchainmask;
520 
521 	/* rssi */
522 	sc->sc_rx_th.wr_v.rssi_ctl[0] = rs->rs_rssi_ctl[0];
523 	sc->sc_rx_th.wr_v.rssi_ctl[1] = rs->rs_rssi_ctl[1];
524 	sc->sc_rx_th.wr_v.rssi_ctl[2] = rs->rs_rssi_ctl[2];
525 	sc->sc_rx_th.wr_v.rssi_ext[0] = rs->rs_rssi_ext[0];
526 	sc->sc_rx_th.wr_v.rssi_ext[1] = rs->rs_rssi_ext[1];
527 	sc->sc_rx_th.wr_v.rssi_ext[2] = rs->rs_rssi_ext[2];
528 
529 	/* evm */
530 	sc->sc_rx_th.wr_v.evm[0] = rs->rs_evm0;
531 	sc->sc_rx_th.wr_v.evm[1] = rs->rs_evm1;
532 	sc->sc_rx_th.wr_v.evm[2] = rs->rs_evm2;
533 	/* These are only populated from the AR9300 or later */
534 	sc->sc_rx_th.wr_v.evm[3] = rs->rs_evm3;
535 	sc->sc_rx_th.wr_v.evm[4] = rs->rs_evm4;
536 
537 	/* direction */
538 	sc->sc_rx_th.wr_v.vh_flags = ATH_VENDOR_PKT_RX;
539 
540 	/* RX rate */
541 	sc->sc_rx_th.wr_v.vh_rx_hwrate = rs->rs_rate;
542 
543 	/* RX flags */
544 	sc->sc_rx_th.wr_v.vh_rs_flags = rs->rs_flags;
545 
546 	if (rs->rs_isaggr)
547 		sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_ISAGGR;
548 	if (rs->rs_moreaggr)
549 		sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_MOREAGGR;
550 
551 	/* phyerr info */
552 	if (rs->rs_status & HAL_RXERR_PHY) {
553 		sc->sc_rx_th.wr_v.vh_phyerr_code = rs->rs_phyerr;
554 		sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_RXPHYERR;
555 	} else {
556 		sc->sc_rx_th.wr_v.vh_phyerr_code = 0xff;
557 	}
558 	sc->sc_rx_th.wr_v.vh_rs_status = rs->rs_status;
559 	sc->sc_rx_th.wr_v.vh_rssi = rs->rs_rssi;
560 }
561 #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
562 
563 static void
564 ath_rx_tap(struct ath_softc *sc, struct mbuf *m,
565 	const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
566 {
567 #define	CHAN_HT20	htole32(IEEE80211_CHAN_HT20)
568 #define	CHAN_HT40U	htole32(IEEE80211_CHAN_HT40U)
569 #define	CHAN_HT40D	htole32(IEEE80211_CHAN_HT40D)
570 #define	CHAN_HT		(CHAN_HT20|CHAN_HT40U|CHAN_HT40D)
571 	const HAL_RATE_TABLE *rt;
572 	uint8_t rix;
573 
574 	rt = sc->sc_currates;
575 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
576 	rix = rt->rateCodeToIndex[rs->rs_rate];
577 	sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
578 	sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
579 
580 	/* 802.11 specific flags */
581 	sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT;
582 	if (rs->rs_status & HAL_RXERR_PHY) {
583 		/*
584 		 * PHY error - make sure the channel flags
585 		 * reflect the actual channel configuration,
586 		 * not the received frame.
587 		 */
588 		if (IEEE80211_IS_CHAN_HT40U(sc->sc_curchan))
589 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
590 		else if (IEEE80211_IS_CHAN_HT40D(sc->sc_curchan))
591 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
592 		else if (IEEE80211_IS_CHAN_HT20(sc->sc_curchan))
593 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
594 	} else if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) {	/* HT rate */
595 		struct ieee80211com *ic = &sc->sc_ic;
596 
597 		if ((rs->rs_flags & HAL_RX_2040) == 0)
598 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
599 		else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan))
600 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
601 		else
602 			sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
603 
604 		if (rs->rs_flags & HAL_RX_GI)
605 			sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
606 	}
607 
608 	sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(sc, rs->rs_tstamp, tsf));
609 	if (rs->rs_status & HAL_RXERR_CRC)
610 		sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
611 	/* XXX propagate other error flags from descriptor */
612 	sc->sc_rx_th.wr_antnoise = nf;
613 	sc->sc_rx_th.wr_antsignal = nf + rs->rs_rssi;
614 	sc->sc_rx_th.wr_antenna = rs->rs_antenna;
615 #undef CHAN_HT
616 #undef CHAN_HT20
617 #undef CHAN_HT40U
618 #undef CHAN_HT40D
619 }
620 
621 static void
622 ath_handle_micerror(struct ieee80211com *ic,
623 	struct ieee80211_frame *wh, int keyix)
624 {
625 	struct ieee80211_node *ni;
626 
627 	/* XXX recheck MIC to deal w/ chips that lie */
628 	/* XXX discard MIC errors on !data frames */
629 	ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
630 	if (ni != NULL) {
631 		ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix);
632 		ieee80211_free_node(ni);
633 	}
634 }
635 
636 /*
637  * Process a single packet.
638  *
639  * The mbuf must already be synced, unmapped and removed from bf->bf_m
640  * by this stage.
641  *
642  * The mbuf must be consumed by this routine - either passed up the
643  * net80211 stack, put on the holding queue, or freed.
644  */
645 int
646 ath_rx_pkt(struct ath_softc *sc, struct ath_rx_status *rs, HAL_STATUS status,
647     uint64_t tsf, int nf, HAL_RX_QUEUE qtype, struct ath_buf *bf,
648     struct mbuf *m)
649 {
650 	struct epoch_tracker et;
651 	uint64_t rstamp;
652 	/* XXX TODO: make this an mbuf tag? */
653 	struct ieee80211_rx_stats rxs;
654 	int len, type, i;
655 	struct ieee80211com *ic = &sc->sc_ic;
656 	struct ieee80211_node *ni;
657 	int is_good = 0;
658 	struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
659 
660 	/*
661 	 * Calculate the correct 64 bit TSF given
662 	 * the TSF64 register value and rs_tstamp.
663 	 */
664 	rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
665 
666 	/* 802.11 return codes - These aren't specifically errors */
667 	if (rs->rs_flags & HAL_RX_GI)
668 		sc->sc_stats.ast_rx_halfgi++;
669 	if (rs->rs_flags & HAL_RX_2040)
670 		sc->sc_stats.ast_rx_2040++;
671 	if (rs->rs_flags & HAL_RX_DELIM_CRC_PRE)
672 		sc->sc_stats.ast_rx_pre_crc_err++;
673 	if (rs->rs_flags & HAL_RX_DELIM_CRC_POST)
674 		sc->sc_stats.ast_rx_post_crc_err++;
675 	if (rs->rs_flags & HAL_RX_DECRYPT_BUSY)
676 		sc->sc_stats.ast_rx_decrypt_busy_err++;
677 	if (rs->rs_flags & HAL_RX_HI_RX_CHAIN)
678 		sc->sc_stats.ast_rx_hi_rx_chain++;
679 	if (rs->rs_flags & HAL_RX_STBC)
680 		sc->sc_stats.ast_rx_stbc++;
681 
682 	if (rs->rs_status != 0) {
683 		if (rs->rs_status & HAL_RXERR_CRC)
684 			sc->sc_stats.ast_rx_crcerr++;
685 		if (rs->rs_status & HAL_RXERR_FIFO)
686 			sc->sc_stats.ast_rx_fifoerr++;
687 		if (rs->rs_status & HAL_RXERR_PHY) {
688 			sc->sc_stats.ast_rx_phyerr++;
689 			/* Process DFS radar events */
690 			if ((rs->rs_phyerr == HAL_PHYERR_RADAR) ||
691 			    (rs->rs_phyerr == HAL_PHYERR_FALSE_RADAR_EXT)) {
692 				/* Now pass it to the radar processing code */
693 				ath_dfs_process_phy_err(sc, m, rstamp, rs);
694 			}
695 
696 			/* Be suitably paranoid about receiving phy errors out of the stats array bounds */
697 			if (rs->rs_phyerr < 64)
698 				sc->sc_stats.ast_rx_phy[rs->rs_phyerr]++;
699 			goto rx_error;	/* NB: don't count in ierrors */
700 		}
701 		if (rs->rs_status & HAL_RXERR_DECRYPT) {
702 			/*
703 			 * Decrypt error.  If the error occurred
704 			 * because there was no hardware key, then
705 			 * let the frame through so the upper layers
706 			 * can process it.  This is necessary for 5210
707 			 * parts which have no way to setup a ``clear''
708 			 * key cache entry.
709 			 *
710 			 * XXX do key cache faulting
711 			 */
712 			if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
713 				goto rx_accept;
714 			sc->sc_stats.ast_rx_badcrypt++;
715 		}
716 		/*
717 		 * Similar as above - if the failure was a keymiss
718 		 * just punt it up to the upper layers for now.
719 		 */
720 		if (rs->rs_status & HAL_RXERR_KEYMISS) {
721 			sc->sc_stats.ast_rx_keymiss++;
722 			goto rx_accept;
723 		}
724 		if (rs->rs_status & HAL_RXERR_MIC) {
725 			sc->sc_stats.ast_rx_badmic++;
726 			/*
727 			 * Do minimal work required to hand off
728 			 * the 802.11 header for notification.
729 			 */
730 			/* XXX frag's and qos frames */
731 			len = rs->rs_datalen;
732 			if (len >= sizeof (struct ieee80211_frame)) {
733 				ath_handle_micerror(ic,
734 				    mtod(m, struct ieee80211_frame *),
735 				    sc->sc_splitmic ?
736 					rs->rs_keyix-32 : rs->rs_keyix);
737 			}
738 		}
739 		counter_u64_add(ic->ic_ierrors, 1);
740 rx_error:
741 		/*
742 		 * Cleanup any pending partial frame.
743 		 */
744 		if (re->m_rxpending != NULL) {
745 			m_freem(re->m_rxpending);
746 			re->m_rxpending = NULL;
747 		}
748 		/*
749 		 * When a tap is present pass error frames
750 		 * that have been requested.  By default we
751 		 * pass decrypt+mic errors but others may be
752 		 * interesting (e.g. crc).
753 		 */
754 		if (ieee80211_radiotap_active(ic) &&
755 		    (rs->rs_status & sc->sc_monpass)) {
756 			/* NB: bpf needs the mbuf length setup */
757 			len = rs->rs_datalen;
758 			m->m_pkthdr.len = m->m_len = len;
759 			ath_rx_tap(sc, m, rs, rstamp, nf);
760 #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
761 			ath_rx_tap_vendor(sc, m, rs, rstamp, nf);
762 #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
763 			ieee80211_radiotap_rx_all(ic, m);
764 		}
765 		/* XXX pass MIC errors up for s/w reclaculation */
766 		m_freem(m); m = NULL;
767 		goto rx_next;
768 	}
769 rx_accept:
770 	len = rs->rs_datalen;
771 	m->m_len = len;
772 
773 	if (rs->rs_more) {
774 		/*
775 		 * Frame spans multiple descriptors; save
776 		 * it for the next completed descriptor, it
777 		 * will be used to construct a jumbogram.
778 		 */
779 		if (re->m_rxpending != NULL) {
780 			/* NB: max frame size is currently 2 clusters */
781 			sc->sc_stats.ast_rx_toobig++;
782 			m_freem(re->m_rxpending);
783 		}
784 		m->m_pkthdr.len = len;
785 		re->m_rxpending = m;
786 		m = NULL;
787 		goto rx_next;
788 	} else if (re->m_rxpending != NULL) {
789 		/*
790 		 * This is the second part of a jumbogram,
791 		 * chain it to the first mbuf, adjust the
792 		 * frame length, and clear the rxpending state.
793 		 */
794 		re->m_rxpending->m_next = m;
795 		re->m_rxpending->m_pkthdr.len += len;
796 		m = re->m_rxpending;
797 		re->m_rxpending = NULL;
798 	} else {
799 		/*
800 		 * Normal single-descriptor receive; setup packet length.
801 		 */
802 		m->m_pkthdr.len = len;
803 	}
804 
805 	/*
806 	 * Validate rs->rs_antenna.
807 	 *
808 	 * Some users w/ AR9285 NICs have reported crashes
809 	 * here because rs_antenna field is bogusly large.
810 	 * Let's enforce the maximum antenna limit of 8
811 	 * (and it shouldn't be hard coded, but that's a
812 	 * separate problem) and if there's an issue, print
813 	 * out an error and adjust rs_antenna to something
814 	 * sensible.
815 	 *
816 	 * This code should be removed once the actual
817 	 * root cause of the issue has been identified.
818 	 * For example, it may be that the rs_antenna
819 	 * field is only valid for the last frame of
820 	 * an aggregate and it just happens that it is
821 	 * "mostly" right. (This is a general statement -
822 	 * the majority of the statistics are only valid
823 	 * for the last frame in an aggregate.
824 	 */
825 	if (rs->rs_antenna > 7) {
826 		device_printf(sc->sc_dev, "%s: rs_antenna > 7 (%d)\n",
827 		    __func__, rs->rs_antenna);
828 #ifdef	ATH_DEBUG
829 		ath_printrxbuf(sc, bf, 0, status == HAL_OK);
830 #endif /* ATH_DEBUG */
831 		rs->rs_antenna = 0;	/* XXX better than nothing */
832 	}
833 
834 	/*
835 	 * If this is an AR9285/AR9485, then the receive and LNA
836 	 * configuration is stored in RSSI[2] / EXTRSSI[2].
837 	 * We can extract this out to build a much better
838 	 * receive antenna profile.
839 	 *
840 	 * Yes, this just blurts over the above RX antenna field
841 	 * for now.  It's fine, the AR9285 doesn't really use
842 	 * that.
843 	 *
844 	 * Later on we should store away the fine grained LNA
845 	 * information and keep separate counters just for
846 	 * that.  It'll help when debugging the AR9285/AR9485
847 	 * combined diversity code.
848 	 */
849 	if (sc->sc_rx_lnamixer) {
850 		rs->rs_antenna = 0;
851 
852 		/* Bits 0:1 - the LNA configuration used */
853 		rs->rs_antenna |=
854 		    ((rs->rs_rssi_ctl[2] & HAL_RX_LNA_CFG_USED)
855 		      >> HAL_RX_LNA_CFG_USED_S);
856 
857 		/* Bit 2 - the external RX antenna switch */
858 		if (rs->rs_rssi_ctl[2] & HAL_RX_LNA_EXTCFG)
859 			rs->rs_antenna |= 0x4;
860 	}
861 
862 	sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
863 
864 	/*
865 	 * Populate the rx status block.  When there are bpf
866 	 * listeners we do the additional work to provide
867 	 * complete status.  Otherwise we fill in only the
868 	 * material required by ieee80211_input.  Note that
869 	 * noise setting is filled in above.
870 	 */
871 	if (ieee80211_radiotap_active(ic)) {
872 		ath_rx_tap(sc, m, rs, rstamp, nf);
873 #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
874 		ath_rx_tap_vendor(sc, m, rs, rstamp, nf);
875 #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
876 	}
877 
878 	/*
879 	 * From this point on we assume the frame is at least
880 	 * as large as ieee80211_frame_min; verify that.
881 	 */
882 	if (len < IEEE80211_MIN_LEN) {
883 		if (!ieee80211_radiotap_active(ic)) {
884 			DPRINTF(sc, ATH_DEBUG_RECV,
885 			    "%s: short packet %d\n", __func__, len);
886 			sc->sc_stats.ast_rx_tooshort++;
887 		} else {
888 			/* NB: in particular this captures ack's */
889 			ieee80211_radiotap_rx_all(ic, m);
890 		}
891 		m_freem(m); m = NULL;
892 		goto rx_next;
893 	}
894 
895 	if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
896 		const HAL_RATE_TABLE *rt = sc->sc_currates;
897 		uint8_t rix = rt->rateCodeToIndex[rs->rs_rate];
898 
899 		ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
900 		    sc->sc_hwmap[rix].ieeerate, rs->rs_rssi);
901 	}
902 
903 	m_adj(m, -IEEE80211_CRC_LEN);
904 
905 	/*
906 	 * Locate the node for sender, track state, and then
907 	 * pass the (referenced) node up to the 802.11 layer
908 	 * for its use.
909 	 */
910 	ni = ieee80211_find_rxnode_withkey(ic,
911 		mtod(m, const struct ieee80211_frame_min *),
912 		rs->rs_keyix == HAL_RXKEYIX_INVALID ?
913 			IEEE80211_KEYIX_NONE : rs->rs_keyix);
914 	sc->sc_lastrs = rs;
915 
916 	if (rs->rs_isaggr)
917 		sc->sc_stats.ast_rx_agg++;
918 
919 	/*
920 	 * Populate the per-chain RSSI values where appropriate.
921 	 */
922 	bzero(&rxs, sizeof(rxs));
923 	rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI |
924 	    IEEE80211_R_C_CHAIN |
925 	    IEEE80211_R_C_NF |
926 	    IEEE80211_R_C_RSSI |
927 	    IEEE80211_R_TSF64 |
928 	    IEEE80211_R_TSF_START;	/* XXX TODO: validate */
929 	rxs.c_rssi = rs->rs_rssi;
930 	rxs.c_nf = nf;
931 	rxs.c_chain = 3;	/* XXX TODO: check */
932 	rxs.c_rx_tsf = rstamp;
933 
934 	for (i = 0; i < 3; i++) {
935 		rxs.c_rssi_ctl[i] = rs->rs_rssi_ctl[i];
936 		rxs.c_rssi_ext[i] = rs->rs_rssi_ext[i];
937 		/*
938 		 * XXX note: we currently don't track
939 		 * per-chain noisefloor.
940 		 */
941 		rxs.c_nf_ctl[i] = nf;
942 		rxs.c_nf_ext[i] = nf;
943 	}
944 
945 	NET_EPOCH_ENTER(et);
946 	if (ni != NULL) {
947 		/*
948 		 * Only punt packets for ampdu reorder processing for
949 		 * 11n nodes; net80211 enforces that M_AMPDU is only
950 		 * set for 11n nodes.
951 		 */
952 		if (ni->ni_flags & IEEE80211_NODE_HT)
953 			m->m_flags |= M_AMPDU;
954 
955 		/*
956 		 * Inform rate control about the received RSSI.
957 		 * It can then use this information to potentially drastically
958 		 * alter the available rate based on the RSSI estimate.
959 		 *
960 		 * This is super important when associating to a far away station;
961 		 * you don't want to waste time trying higher rates at some low
962 		 * packet exchange rate (like during DHCP) just to establish
963 		 * that higher MCS rates aren't available.
964 		 */
965 		ATH_RSSI_LPF(ATH_NODE(ni)->an_node_stats.ns_avgrssi,
966 		    rs->rs_rssi);
967 		ath_rate_update_rx_rssi(sc, ATH_NODE(ni),
968 		    ATH_RSSI(ATH_NODE(ni)->an_node_stats.ns_avgrssi));
969 
970 		/*
971 		 * Sending station is known, dispatch directly.
972 		 */
973 		(void) ieee80211_add_rx_params(m, &rxs);
974 		type = ieee80211_input_mimo(ni, m);
975 		ieee80211_free_node(ni);
976 		m = NULL;
977 		/*
978 		 * Arrange to update the last rx timestamp only for
979 		 * frames from our ap when operating in station mode.
980 		 * This assumes the rx key is always setup when
981 		 * associated.
982 		 */
983 		if (ic->ic_opmode == IEEE80211_M_STA &&
984 		    rs->rs_keyix != HAL_RXKEYIX_INVALID)
985 			is_good = 1;
986 	} else {
987 		(void) ieee80211_add_rx_params(m, &rxs);
988 		type = ieee80211_input_mimo_all(ic, m);
989 		m = NULL;
990 	}
991 	NET_EPOCH_EXIT(et);
992 
993 	/*
994 	 * At this point we have passed the frame up the stack; thus
995 	 * the mbuf is no longer ours.
996 	 */
997 
998 	/*
999 	 * Track legacy station RX rssi and do any rx antenna management.
1000 	 */
1001 	ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
1002 	if (sc->sc_diversity) {
1003 		/*
1004 		 * When using fast diversity, change the default rx
1005 		 * antenna if diversity chooses the other antenna 3
1006 		 * times in a row.
1007 		 */
1008 		if (sc->sc_defant != rs->rs_antenna) {
1009 			if (++sc->sc_rxotherant >= 3)
1010 				ath_setdefantenna(sc, rs->rs_antenna);
1011 		} else
1012 			sc->sc_rxotherant = 0;
1013 	}
1014 
1015 	/* Handle slow diversity if enabled */
1016 	if (sc->sc_dolnadiv) {
1017 		ath_lna_rx_comb_scan(sc, rs, ticks, hz);
1018 	}
1019 
1020 	if (sc->sc_softled) {
1021 		/*
1022 		 * Blink for any data frame.  Otherwise do a
1023 		 * heartbeat-style blink when idle.  The latter
1024 		 * is mainly for station mode where we depend on
1025 		 * periodic beacon frames to trigger the poll event.
1026 		 */
1027 		if (type == IEEE80211_FC0_TYPE_DATA) {
1028 			const HAL_RATE_TABLE *rt = sc->sc_currates;
1029 			ath_led_event(sc,
1030 			    rt->rateCodeToIndex[rs->rs_rate]);
1031 		} else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
1032 			ath_led_event(sc, 0);
1033 		}
1034 rx_next:
1035 	/*
1036 	 * Debugging - complain if we didn't NULL the mbuf pointer
1037 	 * here.
1038 	 */
1039 	if (m != NULL) {
1040 		device_printf(sc->sc_dev,
1041 		    "%s: mbuf %p should've been freed!\n",
1042 		    __func__,
1043 		    m);
1044 	}
1045 	return (is_good);
1046 }
1047 
1048 #define	ATH_RX_MAX		128
1049 
1050 /*
1051  * XXX TODO: break out the "get buffers" from "call ath_rx_pkt()" like
1052  * the EDMA code does.
1053  *
1054  * XXX TODO: then, do all of the RX list management stuff inside
1055  * ATH_RX_LOCK() so we don't end up potentially racing.  The EDMA
1056  * code is doing it right.
1057  */
1058 static void
1059 ath_rx_proc(struct ath_softc *sc, int resched)
1060 {
1061 #define	PA2DESC(_sc, _pa) \
1062 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
1063 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
1064 	struct ath_buf *bf;
1065 	struct ath_hal *ah = sc->sc_ah;
1066 #ifdef IEEE80211_SUPPORT_SUPERG
1067 	struct ieee80211com *ic = &sc->sc_ic;
1068 #endif
1069 	struct ath_desc *ds;
1070 	struct ath_rx_status *rs;
1071 	struct mbuf *m;
1072 	int ngood;
1073 	HAL_STATUS status;
1074 	int16_t nf;
1075 	u_int64_t tsf;
1076 	int npkts = 0;
1077 	int kickpcu = 0;
1078 	int ret;
1079 
1080 	/* XXX we must not hold the ATH_LOCK here */
1081 	ATH_UNLOCK_ASSERT(sc);
1082 	ATH_PCU_UNLOCK_ASSERT(sc);
1083 
1084 	ATH_PCU_LOCK(sc);
1085 	sc->sc_rxproc_cnt++;
1086 	kickpcu = sc->sc_kickpcu;
1087 	ATH_PCU_UNLOCK(sc);
1088 
1089 	ATH_LOCK(sc);
1090 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1091 	ATH_UNLOCK(sc);
1092 
1093 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: called\n", __func__);
1094 	ngood = 0;
1095 	nf = ath_hal_getchannoise(ah, sc->sc_curchan);
1096 	sc->sc_stats.ast_rx_noise = nf;
1097 	tsf = ath_hal_gettsf64(ah);
1098 	do {
1099 		/*
1100 		 * Don't process too many packets at a time; give the
1101 		 * TX thread time to also run - otherwise the TX
1102 		 * latency can jump by quite a bit, causing throughput
1103 		 * degredation.
1104 		 */
1105 		if (!kickpcu && npkts >= ATH_RX_MAX)
1106 			break;
1107 
1108 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
1109 		if (sc->sc_rxslink && bf == NULL) {	/* NB: shouldn't happen */
1110 			device_printf(sc->sc_dev, "%s: no buffer!\n", __func__);
1111 			break;
1112 		} else if (bf == NULL) {
1113 			/*
1114 			 * End of List:
1115 			 * this can happen for non-self-linked RX chains
1116 			 */
1117 			sc->sc_stats.ast_rx_hitqueueend++;
1118 			break;
1119 		}
1120 		m = bf->bf_m;
1121 		if (m == NULL) {		/* NB: shouldn't happen */
1122 			/*
1123 			 * If mbuf allocation failed previously there
1124 			 * will be no mbuf; try again to re-populate it.
1125 			 */
1126 			/* XXX make debug msg */
1127 			device_printf(sc->sc_dev, "%s: no mbuf!\n", __func__);
1128 			TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
1129 			goto rx_proc_next;
1130 		}
1131 		ds = bf->bf_desc;
1132 		if (ds->ds_link == bf->bf_daddr) {
1133 			/* NB: never process the self-linked entry at the end */
1134 			sc->sc_stats.ast_rx_hitqueueend++;
1135 			break;
1136 		}
1137 		/* XXX sync descriptor memory */
1138 		/*
1139 		 * Must provide the virtual address of the current
1140 		 * descriptor, the physical address, and the virtual
1141 		 * address of the next descriptor in the h/w chain.
1142 		 * This allows the HAL to look ahead to see if the
1143 		 * hardware is done with a descriptor by checking the
1144 		 * done bit in the following descriptor and the address
1145 		 * of the current descriptor the DMA engine is working
1146 		 * on.  All this is necessary because of our use of
1147 		 * a self-linked list to avoid rx overruns.
1148 		 */
1149 		rs = &bf->bf_status.ds_rxstat;
1150 		status = ath_hal_rxprocdesc(ah, ds,
1151 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
1152 #ifdef ATH_DEBUG
1153 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
1154 			ath_printrxbuf(sc, bf, 0, status == HAL_OK);
1155 #endif
1156 
1157 #ifdef	ATH_DEBUG_ALQ
1158 		if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS))
1159 		    if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS,
1160 		    sc->sc_rx_statuslen, (char *) ds);
1161 #endif	/* ATH_DEBUG_ALQ */
1162 
1163 		if (status == HAL_EINPROGRESS)
1164 			break;
1165 
1166 		TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
1167 		npkts++;
1168 
1169 		/*
1170 		 * Process a single frame.
1171 		 */
1172 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_POSTREAD);
1173 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
1174 		bf->bf_m = NULL;
1175 		if (ath_rx_pkt(sc, rs, status, tsf, nf, HAL_RX_QUEUE_HP, bf, m))
1176 			ngood++;
1177 rx_proc_next:
1178 		/*
1179 		 * If there's a holding buffer, insert that onto
1180 		 * the RX list; the hardware is now definitely not pointing
1181 		 * to it now.
1182 		 */
1183 		ret = 0;
1184 		if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf != NULL) {
1185 			TAILQ_INSERT_TAIL(&sc->sc_rxbuf,
1186 			    sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf,
1187 			    bf_list);
1188 			ret = ath_rxbuf_init(sc,
1189 			    sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf);
1190 		}
1191 		/*
1192 		 * Next, throw our buffer into the holding entry.  The hardware
1193 		 * may use the descriptor to read the link pointer before
1194 		 * DMAing the next descriptor in to write out a packet.
1195 		 */
1196 		sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf = bf;
1197 	} while (ret == 0);
1198 
1199 	/* rx signal state monitoring */
1200 	ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
1201 	if (ngood)
1202 		sc->sc_lastrx = tsf;
1203 
1204 	ATH_KTR(sc, ATH_KTR_RXPROC, 2, "ath_rx_proc: npkts=%d, ngood=%d", npkts, ngood);
1205 	/* Queue DFS tasklet if needed */
1206 	if (resched && ath_dfs_tasklet_needed(sc, sc->sc_curchan))
1207 		taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
1208 
1209 	/*
1210 	 * Now that all the RX frames were handled that
1211 	 * need to be handled, kick the PCU if there's
1212 	 * been an RXEOL condition.
1213 	 */
1214 	if (resched && kickpcu) {
1215 		ATH_PCU_LOCK(sc);
1216 		ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_rx_proc: kickpcu");
1217 		device_printf(sc->sc_dev, "%s: kickpcu; handled %d packets\n",
1218 		    __func__, npkts);
1219 
1220 		/*
1221 		 * Go through the process of fully tearing down
1222 		 * the RX buffers and reinitialising them.
1223 		 *
1224 		 * There's a hardware bug that causes the RX FIFO
1225 		 * to get confused under certain conditions and
1226 		 * constantly write over the same frame, leading
1227 		 * the RX driver code here to get heavily confused.
1228 		 */
1229 		/*
1230 		 * XXX Has RX DMA stopped enough here to just call
1231 		 *     ath_startrecv()?
1232 		 * XXX Do we need to use the holding buffer to restart
1233 		 *     RX DMA by appending entries to the final
1234 		 *     descriptor?  Quite likely.
1235 		 */
1236 #if 1
1237 		ath_startrecv(sc);
1238 #else
1239 		/*
1240 		 * Disabled for now - it'd be nice to be able to do
1241 		 * this in order to limit the amount of CPU time spent
1242 		 * reinitialising the RX side (and thus minimise RX
1243 		 * drops) however there's a hardware issue that
1244 		 * causes things to get too far out of whack.
1245 		 */
1246 		/*
1247 		 * XXX can we hold the PCU lock here?
1248 		 * Are there any net80211 buffer calls involved?
1249 		 */
1250 		bf = TAILQ_FIRST(&sc->sc_rxbuf);
1251 		ath_hal_putrxbuf(ah, bf->bf_daddr, HAL_RX_QUEUE_HP);
1252 		ath_hal_rxena(ah);		/* enable recv descriptors */
1253 		ath_mode_init(sc);		/* set filters, etc. */
1254 		ath_hal_startpcurecv(ah, (!! sc->sc_scanning));	/* re-enable PCU/DMA engine */
1255 #endif
1256 
1257 		ath_hal_intrset(ah, sc->sc_imask);
1258 		sc->sc_kickpcu = 0;
1259 		ATH_PCU_UNLOCK(sc);
1260 	}
1261 
1262 #ifdef IEEE80211_SUPPORT_SUPERG
1263 	if (resched)
1264 		ieee80211_ff_age_all(ic, 100);
1265 #endif
1266 
1267 	/*
1268 	 * Put the hardware to sleep again if we're done with it.
1269 	 */
1270 	ATH_LOCK(sc);
1271 	ath_power_restore_power_state(sc);
1272 	ATH_UNLOCK(sc);
1273 
1274 	/*
1275 	 * If we hit the maximum number of frames in this round,
1276 	 * reschedule for another immediate pass.  This gives
1277 	 * the TX and TX completion routines time to run, which
1278 	 * will reduce latency.
1279 	 */
1280 	if (npkts >= ATH_RX_MAX)
1281 		sc->sc_rx.recv_sched(sc, resched);
1282 
1283 	ATH_PCU_LOCK(sc);
1284 	sc->sc_rxproc_cnt--;
1285 	ATH_PCU_UNLOCK(sc);
1286 }
1287 #undef	PA2DESC
1288 #undef	ATH_RX_MAX
1289 
1290 /*
1291  * Only run the RX proc if it's not already running.
1292  * Since this may get run as part of the reset/flush path,
1293  * the task can't clash with an existing, running tasklet.
1294  */
1295 static void
1296 ath_legacy_rx_tasklet(void *arg, int npending)
1297 {
1298 	struct ath_softc *sc = arg;
1299 
1300 	ATH_KTR(sc, ATH_KTR_RXPROC, 1, "ath_rx_proc: pending=%d", npending);
1301 	DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
1302 	ATH_PCU_LOCK(sc);
1303 	if (sc->sc_inreset_cnt > 0) {
1304 		device_printf(sc->sc_dev,
1305 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
1306 		ATH_PCU_UNLOCK(sc);
1307 		return;
1308 	}
1309 	ATH_PCU_UNLOCK(sc);
1310 
1311 	ath_rx_proc(sc, 1);
1312 }
1313 
1314 static void
1315 ath_legacy_flushrecv(struct ath_softc *sc)
1316 {
1317 
1318 	ath_rx_proc(sc, 0);
1319 }
1320 
1321 static void
1322 ath_legacy_flush_rxpending(struct ath_softc *sc)
1323 {
1324 
1325 	/* XXX ATH_RX_LOCK_ASSERT(sc); */
1326 
1327 	if (sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending != NULL) {
1328 		m_freem(sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending);
1329 		sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending = NULL;
1330 	}
1331 	if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending != NULL) {
1332 		m_freem(sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending);
1333 		sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending = NULL;
1334 	}
1335 }
1336 
1337 static int
1338 ath_legacy_flush_rxholdbf(struct ath_softc *sc)
1339 {
1340 	struct ath_buf *bf;
1341 
1342 	/* XXX ATH_RX_LOCK_ASSERT(sc); */
1343 	/*
1344 	 * If there are RX holding buffers, free them here and return
1345 	 * them to the list.
1346 	 *
1347 	 * XXX should just verify that bf->bf_m is NULL, as it must
1348 	 * be at this point!
1349 	 */
1350 	bf = sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf;
1351 	if (bf != NULL) {
1352 		if (bf->bf_m != NULL)
1353 			m_freem(bf->bf_m);
1354 		bf->bf_m = NULL;
1355 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
1356 		(void) ath_rxbuf_init(sc, bf);
1357 	}
1358 	sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf = NULL;
1359 
1360 	bf = sc->sc_rxedma[HAL_RX_QUEUE_LP].m_holdbf;
1361 	if (bf != NULL) {
1362 		if (bf->bf_m != NULL)
1363 			m_freem(bf->bf_m);
1364 		bf->bf_m = NULL;
1365 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
1366 		(void) ath_rxbuf_init(sc, bf);
1367 	}
1368 	sc->sc_rxedma[HAL_RX_QUEUE_LP].m_holdbf = NULL;
1369 
1370 	return (0);
1371 }
1372 
1373 /*
1374  * Disable the receive h/w in preparation for a reset.
1375  */
1376 static void
1377 ath_legacy_stoprecv(struct ath_softc *sc, int dodelay)
1378 {
1379 #define	PA2DESC(_sc, _pa) \
1380 	((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
1381 		((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
1382 	struct ath_hal *ah = sc->sc_ah;
1383 
1384 	ATH_RX_LOCK(sc);
1385 
1386 	ath_hal_stoppcurecv(ah);	/* disable PCU */
1387 	ath_hal_setrxfilter(ah, 0);	/* clear recv filter */
1388 	ath_hal_stopdmarecv(ah);	/* disable DMA engine */
1389 	/*
1390 	 * TODO: see if this particular DELAY() is required; it may be
1391 	 * masking some missing FIFO flush or DMA sync.
1392 	 */
1393 #if 0
1394 	if (dodelay)
1395 #endif
1396 		DELAY(3000);		/* 3ms is long enough for 1 frame */
1397 #ifdef ATH_DEBUG
1398 	if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
1399 		struct ath_buf *bf;
1400 		u_int ix;
1401 
1402 		device_printf(sc->sc_dev,
1403 		    "%s: rx queue %p, link %p\n",
1404 		    __func__,
1405 		    (caddr_t)(uintptr_t) ath_hal_getrxbuf(ah, HAL_RX_QUEUE_HP),
1406 		    sc->sc_rxlink);
1407 		ix = 0;
1408 		TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
1409 			struct ath_desc *ds = bf->bf_desc;
1410 			struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
1411 			HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
1412 				bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
1413 			if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
1414 				ath_printrxbuf(sc, bf, ix, status == HAL_OK);
1415 			ix++;
1416 		}
1417 	}
1418 #endif
1419 
1420 	(void) ath_legacy_flush_rxpending(sc);
1421 	(void) ath_legacy_flush_rxholdbf(sc);
1422 
1423 	sc->sc_rxlink = NULL;		/* just in case */
1424 
1425 	ATH_RX_UNLOCK(sc);
1426 #undef PA2DESC
1427 }
1428 
1429 /*
1430  * XXX TODO: something was calling startrecv without calling
1431  * stoprecv.  Let's figure out what/why.  It was showing up
1432  * as a mbuf leak (rxpending) and ath_buf leak (holdbf.)
1433  */
1434 
1435 /*
1436  * Enable the receive h/w following a reset.
1437  */
1438 static int
1439 ath_legacy_startrecv(struct ath_softc *sc)
1440 {
1441 	struct ath_hal *ah = sc->sc_ah;
1442 	struct ath_buf *bf;
1443 
1444 	ATH_RX_LOCK(sc);
1445 
1446 	/*
1447 	 * XXX should verify these are already all NULL!
1448 	 */
1449 	sc->sc_rxlink = NULL;
1450 	(void) ath_legacy_flush_rxpending(sc);
1451 	(void) ath_legacy_flush_rxholdbf(sc);
1452 
1453 	/*
1454 	 * Re-chain all of the buffers in the RX buffer list.
1455 	 */
1456 	TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
1457 		int error = ath_rxbuf_init(sc, bf);
1458 		if (error != 0) {
1459 			DPRINTF(sc, ATH_DEBUG_RECV,
1460 				"%s: ath_rxbuf_init failed %d\n",
1461 				__func__, error);
1462 			return error;
1463 		}
1464 	}
1465 
1466 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
1467 	ath_hal_putrxbuf(ah, bf->bf_daddr, HAL_RX_QUEUE_HP);
1468 	ath_hal_rxena(ah);		/* enable recv descriptors */
1469 	ath_mode_init(sc);		/* set filters, etc. */
1470 	ath_hal_startpcurecv(ah, (!! sc->sc_scanning));	/* re-enable PCU/DMA engine */
1471 
1472 	ATH_RX_UNLOCK(sc);
1473 	return 0;
1474 }
1475 
1476 static int
1477 ath_legacy_dma_rxsetup(struct ath_softc *sc)
1478 {
1479 	int error;
1480 
1481 	error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
1482 	    "rx", sizeof(struct ath_desc), ath_rxbuf, 1);
1483 	if (error != 0)
1484 		return (error);
1485 
1486 	return (0);
1487 }
1488 
1489 static int
1490 ath_legacy_dma_rxteardown(struct ath_softc *sc)
1491 {
1492 
1493 	if (sc->sc_rxdma.dd_desc_len != 0)
1494 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
1495 	return (0);
1496 }
1497 
1498 static void
1499 ath_legacy_recv_sched(struct ath_softc *sc, int dosched)
1500 {
1501 
1502 	taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1503 }
1504 
1505 static void
1506 ath_legacy_recv_sched_queue(struct ath_softc *sc, HAL_RX_QUEUE q,
1507     int dosched)
1508 {
1509 
1510 	taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1511 }
1512 
1513 void
1514 ath_recv_setup_legacy(struct ath_softc *sc)
1515 {
1516 
1517 	/* Sensible legacy defaults */
1518 	/*
1519 	 * XXX this should be changed to properly support the
1520 	 * exact RX descriptor size for each HAL.
1521 	 */
1522 	sc->sc_rx_statuslen = sizeof(struct ath_desc);
1523 
1524 	sc->sc_rx.recv_start = ath_legacy_startrecv;
1525 	sc->sc_rx.recv_stop = ath_legacy_stoprecv;
1526 	sc->sc_rx.recv_flush = ath_legacy_flushrecv;
1527 	sc->sc_rx.recv_tasklet = ath_legacy_rx_tasklet;
1528 	sc->sc_rx.recv_rxbuf_init = ath_legacy_rxbuf_init;
1529 
1530 	sc->sc_rx.recv_setup = ath_legacy_dma_rxsetup;
1531 	sc->sc_rx.recv_teardown = ath_legacy_dma_rxteardown;
1532 	sc->sc_rx.recv_sched = ath_legacy_recv_sched;
1533 	sc->sc_rx.recv_sched_queue = ath_legacy_recv_sched_queue;
1534 }
1535