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