xref: /freebsd/sys/dev/ath/if_ath.c (revision ef0cb5db0af0d5d5b75b74f8e534fe601b7176d7)
1 /*-
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
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_rx_edma.h>
113 #include <dev/ath/if_ath_tx_edma.h>
114 #include <dev/ath/if_ath_beacon.h>
115 #include <dev/ath/if_ath_btcoex.h>
116 #include <dev/ath/if_ath_spectral.h>
117 #include <dev/ath/if_ath_lna_div.h>
118 #include <dev/ath/if_athdfs.h>
119 
120 #ifdef ATH_TX99_DIAG
121 #include <dev/ath/ath_tx99/ath_tx99.h>
122 #endif
123 
124 #ifdef	ATH_DEBUG_ALQ
125 #include <dev/ath/if_ath_alq.h>
126 #endif
127 
128 /*
129  * Only enable this if you're working on PS-POLL support.
130  */
131 #define	ATH_SW_PSQ
132 
133 /*
134  * ATH_BCBUF determines the number of vap's that can transmit
135  * beacons and also (currently) the number of vap's that can
136  * have unique mac addresses/bssid.  When staggering beacons
137  * 4 is probably a good max as otherwise the beacons become
138  * very closely spaced and there is limited time for cab q traffic
139  * to go out.  You can burst beacons instead but that is not good
140  * for stations in power save and at some point you really want
141  * another radio (and channel).
142  *
143  * The limit on the number of mac addresses is tied to our use of
144  * the U/L bit and tracking addresses in a byte; it would be
145  * worthwhile to allow more for applications like proxy sta.
146  */
147 CTASSERT(ATH_BCBUF <= 8);
148 
149 static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
150 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
151 		    const uint8_t [IEEE80211_ADDR_LEN],
152 		    const uint8_t [IEEE80211_ADDR_LEN]);
153 static void	ath_vap_delete(struct ieee80211vap *);
154 static void	ath_init(void *);
155 static void	ath_stop_locked(struct ifnet *);
156 static void	ath_stop(struct ifnet *);
157 static int	ath_reset_vap(struct ieee80211vap *, u_long);
158 static int	ath_transmit(struct ifnet *ifp, struct mbuf *m);
159 static void	ath_qflush(struct ifnet *ifp);
160 static int	ath_media_change(struct ifnet *);
161 static void	ath_watchdog(void *);
162 static int	ath_ioctl(struct ifnet *, u_long, caddr_t);
163 static void	ath_fatal_proc(void *, int);
164 static void	ath_bmiss_vap(struct ieee80211vap *);
165 static void	ath_bmiss_proc(void *, int);
166 static void	ath_key_update_begin(struct ieee80211vap *);
167 static void	ath_key_update_end(struct ieee80211vap *);
168 static void	ath_update_mcast_hw(struct ath_softc *);
169 static void	ath_update_mcast(struct ieee80211com *);
170 static void	ath_update_promisc(struct ieee80211com *);
171 static void	ath_updateslot(struct ieee80211com *);
172 static void	ath_bstuck_proc(void *, int);
173 static void	ath_reset_proc(void *, int);
174 static int	ath_desc_alloc(struct ath_softc *);
175 static void	ath_desc_free(struct ath_softc *);
176 static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
177 			const uint8_t [IEEE80211_ADDR_LEN]);
178 static void	ath_node_cleanup(struct ieee80211_node *);
179 static void	ath_node_free(struct ieee80211_node *);
180 static void	ath_node_getsignal(const struct ieee80211_node *,
181 			int8_t *, int8_t *);
182 static void	ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
183 static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
184 static int	ath_tx_setup(struct ath_softc *, int, int);
185 static void	ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
186 static void	ath_tx_cleanup(struct ath_softc *);
187 static int	ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq,
188 		    int dosched);
189 static void	ath_tx_proc_q0(void *, int);
190 static void	ath_tx_proc_q0123(void *, int);
191 static void	ath_tx_proc(void *, int);
192 static void	ath_txq_sched_tasklet(void *, int);
193 static int	ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
194 static void	ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
195 static void	ath_scan_start(struct ieee80211com *);
196 static void	ath_scan_end(struct ieee80211com *);
197 static void	ath_set_channel(struct ieee80211com *);
198 #ifdef	ATH_ENABLE_11N
199 static void	ath_update_chw(struct ieee80211com *);
200 #endif	/* ATH_ENABLE_11N */
201 static void	ath_calibrate(void *);
202 static int	ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
203 static void	ath_setup_stationkey(struct ieee80211_node *);
204 static void	ath_newassoc(struct ieee80211_node *, int);
205 static int	ath_setregdomain(struct ieee80211com *,
206 		    struct ieee80211_regdomain *, int,
207 		    struct ieee80211_channel []);
208 static void	ath_getradiocaps(struct ieee80211com *, int, int *,
209 		    struct ieee80211_channel []);
210 static int	ath_getchannels(struct ath_softc *);
211 
212 static int	ath_rate_setup(struct ath_softc *, u_int mode);
213 static void	ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
214 
215 static void	ath_announce(struct ath_softc *);
216 
217 static void	ath_dfs_tasklet(void *, int);
218 static void	ath_node_powersave(struct ieee80211_node *, int);
219 static int	ath_node_set_tim(struct ieee80211_node *, int);
220 static void	ath_node_recv_pspoll(struct ieee80211_node *, struct mbuf *);
221 
222 #ifdef IEEE80211_SUPPORT_TDMA
223 #include <dev/ath/if_ath_tdma.h>
224 #endif
225 
226 SYSCTL_DECL(_hw_ath);
227 
228 /* XXX validate sysctl values */
229 static	int ath_longcalinterval = 30;		/* long cals every 30 secs */
230 SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
231 	    0, "long chip calibration interval (secs)");
232 static	int ath_shortcalinterval = 100;		/* short cals every 100 ms */
233 SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
234 	    0, "short chip calibration interval (msecs)");
235 static	int ath_resetcalinterval = 20*60;	/* reset cal state 20 mins */
236 SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
237 	    0, "reset chip calibration results (secs)");
238 static	int ath_anicalinterval = 100;		/* ANI calibration - 100 msec */
239 SYSCTL_INT(_hw_ath, OID_AUTO, anical, CTLFLAG_RW, &ath_anicalinterval,
240 	    0, "ANI calibration (msecs)");
241 
242 int ath_rxbuf = ATH_RXBUF;		/* # rx buffers to allocate */
243 SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RWTUN, &ath_rxbuf,
244 	    0, "rx buffers allocated");
245 int ath_txbuf = ATH_TXBUF;		/* # tx buffers to allocate */
246 SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RWTUN, &ath_txbuf,
247 	    0, "tx buffers allocated");
248 int ath_txbuf_mgmt = ATH_MGMT_TXBUF;	/* # mgmt tx buffers to allocate */
249 SYSCTL_INT(_hw_ath, OID_AUTO, txbuf_mgmt, CTLFLAG_RWTUN, &ath_txbuf_mgmt,
250 	    0, "tx (mgmt) buffers allocated");
251 
252 int ath_bstuck_threshold = 4;		/* max missed beacons */
253 SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
254 	    0, "max missed beacon xmits before chip reset");
255 
256 MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
257 
258 void
259 ath_legacy_attach_comp_func(struct ath_softc *sc)
260 {
261 
262 	/*
263 	 * Special case certain configurations.  Note the
264 	 * CAB queue is handled by these specially so don't
265 	 * include them when checking the txq setup mask.
266 	 */
267 	switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
268 	case 0x01:
269 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
270 		break;
271 	case 0x0f:
272 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
273 		break;
274 	default:
275 		TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
276 		break;
277 	}
278 }
279 
280 /*
281  * Set the target power mode.
282  *
283  * If this is called during a point in time where
284  * the hardware is being programmed elsewhere, it will
285  * simply store it away and update it when all current
286  * uses of the hardware are completed.
287  */
288 void
289 _ath_power_setpower(struct ath_softc *sc, int power_state, const char *file, int line)
290 {
291 	ATH_LOCK_ASSERT(sc);
292 
293 	sc->sc_target_powerstate = power_state;
294 
295 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
296 	    __func__,
297 	    file,
298 	    line,
299 	    power_state,
300 	    sc->sc_powersave_refcnt);
301 
302 	if (sc->sc_powersave_refcnt == 0 &&
303 	    power_state != sc->sc_cur_powerstate) {
304 		sc->sc_cur_powerstate = power_state;
305 		ath_hal_setpower(sc->sc_ah, power_state);
306 
307 		/*
308 		 * If the NIC is force-awake, then set the
309 		 * self-gen frame state appropriately.
310 		 *
311 		 * If the nic is in network sleep or full-sleep,
312 		 * we let the above call leave the self-gen
313 		 * state as "sleep".
314 		 */
315 		if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
316 		    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
317 			ath_hal_setselfgenpower(sc->sc_ah,
318 			    sc->sc_target_selfgen_state);
319 		}
320 	}
321 }
322 
323 /*
324  * Set the current self-generated frames state.
325  *
326  * This is separate from the target power mode.  The chip may be
327  * awake but the desired state is "sleep", so frames sent to the
328  * destination has PWRMGT=1 in the 802.11 header.  The NIC also
329  * needs to know to set PWRMGT=1 in self-generated frames.
330  */
331 void
332 _ath_power_set_selfgen(struct ath_softc *sc, int power_state, const char *file, int line)
333 {
334 
335 	ATH_LOCK_ASSERT(sc);
336 
337 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
338 	    __func__,
339 	    file,
340 	    line,
341 	    power_state,
342 	    sc->sc_target_selfgen_state);
343 
344 	sc->sc_target_selfgen_state = power_state;
345 
346 	/*
347 	 * If the NIC is force-awake, then set the power state.
348 	 * Network-state and full-sleep will already transition it to
349 	 * mark self-gen frames as sleeping - and we can't
350 	 * guarantee the NIC is awake to program the self-gen frame
351 	 * setting anyway.
352 	 */
353 	if (sc->sc_cur_powerstate == HAL_PM_AWAKE) {
354 		ath_hal_setselfgenpower(sc->sc_ah, power_state);
355 	}
356 }
357 
358 /*
359  * Set the hardware power mode and take a reference.
360  *
361  * This doesn't update the target power mode in the driver;
362  * it just updates the hardware power state.
363  *
364  * XXX it should only ever force the hardware awake; it should
365  * never be called to set it asleep.
366  */
367 void
368 _ath_power_set_power_state(struct ath_softc *sc, int power_state, const char *file, int line)
369 {
370 	ATH_LOCK_ASSERT(sc);
371 
372 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) state=%d, refcnt=%d\n",
373 	    __func__,
374 	    file,
375 	    line,
376 	    power_state,
377 	    sc->sc_powersave_refcnt);
378 
379 	sc->sc_powersave_refcnt++;
380 
381 	if (power_state != sc->sc_cur_powerstate) {
382 		ath_hal_setpower(sc->sc_ah, power_state);
383 		sc->sc_cur_powerstate = power_state;
384 
385 		/*
386 		 * Adjust the self-gen powerstate if appropriate.
387 		 */
388 		if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
389 		    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
390 			ath_hal_setselfgenpower(sc->sc_ah,
391 			    sc->sc_target_selfgen_state);
392 		}
393 
394 	}
395 }
396 
397 /*
398  * Restore the power save mode to what it once was.
399  *
400  * This will decrement the reference counter and once it hits
401  * zero, it'll restore the powersave state.
402  */
403 void
404 _ath_power_restore_power_state(struct ath_softc *sc, const char *file, int line)
405 {
406 
407 	ATH_LOCK_ASSERT(sc);
408 
409 	DPRINTF(sc, ATH_DEBUG_PWRSAVE, "%s: (%s:%d) refcnt=%d, target state=%d\n",
410 	    __func__,
411 	    file,
412 	    line,
413 	    sc->sc_powersave_refcnt,
414 	    sc->sc_target_powerstate);
415 
416 	if (sc->sc_powersave_refcnt == 0)
417 		device_printf(sc->sc_dev, "%s: refcnt=0?\n", __func__);
418 	else
419 		sc->sc_powersave_refcnt--;
420 
421 	if (sc->sc_powersave_refcnt == 0 &&
422 	    sc->sc_target_powerstate != sc->sc_cur_powerstate) {
423 		sc->sc_cur_powerstate = sc->sc_target_powerstate;
424 		ath_hal_setpower(sc->sc_ah, sc->sc_target_powerstate);
425 	}
426 
427 	/*
428 	 * Adjust the self-gen powerstate if appropriate.
429 	 */
430 	if (sc->sc_cur_powerstate == HAL_PM_AWAKE &&
431 	    sc->sc_target_selfgen_state != HAL_PM_AWAKE) {
432 		ath_hal_setselfgenpower(sc->sc_ah,
433 		    sc->sc_target_selfgen_state);
434 	}
435 
436 }
437 
438 /*
439  * Configure the initial HAL configuration values based on bus
440  * specific parameters.
441  *
442  * Some PCI IDs and other information may need tweaking.
443  *
444  * XXX TODO: ath9k and the Atheros HAL only program comm2g_switch_enable
445  * if BT antenna diversity isn't enabled.
446  *
447  * So, let's also figure out how to enable BT diversity for AR9485.
448  */
449 static void
450 ath_setup_hal_config(struct ath_softc *sc, HAL_OPS_CONFIG *ah_config)
451 {
452 	/* XXX TODO: only for PCI devices? */
453 
454 	if (sc->sc_pci_devinfo & (ATH_PCI_CUS198 | ATH_PCI_CUS230)) {
455 		ah_config->ath_hal_ext_lna_ctl_gpio = 0x200; /* bit 9 */
456 		ah_config->ath_hal_ext_atten_margin_cfg = AH_TRUE;
457 		ah_config->ath_hal_min_gainidx = AH_TRUE;
458 		ah_config->ath_hal_ant_ctrl_comm2g_switch_enable = 0x000bbb88;
459 		/* XXX low_rssi_thresh */
460 		/* XXX fast_div_bias */
461 		device_printf(sc->sc_dev, "configuring for %s\n",
462 		    (sc->sc_pci_devinfo & ATH_PCI_CUS198) ?
463 		    "CUS198" : "CUS230");
464 	}
465 
466 	if (sc->sc_pci_devinfo & ATH_PCI_CUS217)
467 		device_printf(sc->sc_dev, "CUS217 card detected\n");
468 
469 	if (sc->sc_pci_devinfo & ATH_PCI_CUS252)
470 		device_printf(sc->sc_dev, "CUS252 card detected\n");
471 
472 	if (sc->sc_pci_devinfo & ATH_PCI_AR9565_1ANT)
473 		device_printf(sc->sc_dev, "WB335 1-ANT card detected\n");
474 
475 	if (sc->sc_pci_devinfo & ATH_PCI_AR9565_2ANT)
476 		device_printf(sc->sc_dev, "WB335 2-ANT card detected\n");
477 
478 	if (sc->sc_pci_devinfo & ATH_PCI_KILLER)
479 		device_printf(sc->sc_dev, "Killer Wireless card detected\n");
480 
481 #if 0
482         /*
483          * Some WB335 cards do not support antenna diversity. Since
484          * we use a hardcoded value for AR9565 instead of using the
485          * EEPROM/OTP data, remove the combining feature from
486          * the HW capabilities bitmap.
487          */
488         if (sc->sc_pci_devinfo & (ATH9K_PCI_AR9565_1ANT | ATH9K_PCI_AR9565_2ANT)) {
489                 if (!(sc->sc_pci_devinfo & ATH9K_PCI_BT_ANT_DIV))
490                         pCap->hw_caps &= ~ATH9K_HW_CAP_ANT_DIV_COMB;
491         }
492 
493         if (sc->sc_pci_devinfo & ATH9K_PCI_BT_ANT_DIV) {
494                 pCap->hw_caps |= ATH9K_HW_CAP_BT_ANT_DIV;
495                 device_printf(sc->sc_dev, "Set BT/WLAN RX diversity capability\n");
496         }
497 #endif
498 
499         if (sc->sc_pci_devinfo & ATH_PCI_D3_L1_WAR) {
500                 ah_config->ath_hal_pcie_waen = 0x0040473b;
501                 device_printf(sc->sc_dev, "Enable WAR for ASPM D3/L1\n");
502         }
503 
504 #if 0
505         if (sc->sc_pci_devinfo & ATH9K_PCI_NO_PLL_PWRSAVE) {
506                 ah->config.no_pll_pwrsave = true;
507                 device_printf(sc->sc_dev, "Disable PLL PowerSave\n");
508         }
509 #endif
510 
511 }
512 
513 /*
514  * Attempt to fetch the MAC address from the kernel environment.
515  *
516  * Returns 0, macaddr in macaddr if successful; -1 otherwise.
517  */
518 static int
519 ath_fetch_mac_kenv(struct ath_softc *sc, uint8_t *macaddr)
520 {
521 	char devid_str[32];
522 	int local_mac = 0;
523 	char *local_macstr;
524 
525 	/*
526 	 * Fetch from the kenv rather than using hints.
527 	 *
528 	 * Hints would be nice but the transition to dynamic
529 	 * hints/kenv doesn't happen early enough for this
530 	 * to work reliably (eg on anything embedded.)
531 	 */
532 	snprintf(devid_str, 32, "hint.%s.%d.macaddr",
533 	    device_get_name(sc->sc_dev),
534 	    device_get_unit(sc->sc_dev));
535 
536 	if ((local_macstr = kern_getenv(devid_str)) != NULL) {
537 		uint32_t tmpmac[ETHER_ADDR_LEN];
538 		int count;
539 		int i;
540 
541 		/* Have a MAC address; should use it */
542 		device_printf(sc->sc_dev,
543 		    "Overriding MAC address from environment: '%s'\n",
544 		    local_macstr);
545 
546 		/* Extract out the MAC address */
547 		count = sscanf(local_macstr, "%x%*c%x%*c%x%*c%x%*c%x%*c%x",
548 		    &tmpmac[0], &tmpmac[1],
549 		    &tmpmac[2], &tmpmac[3],
550 		    &tmpmac[4], &tmpmac[5]);
551 		if (count == 6) {
552 			/* Valid! */
553 			local_mac = 1;
554 			for (i = 0; i < ETHER_ADDR_LEN; i++)
555 				macaddr[i] = tmpmac[i];
556 		}
557 		/* Done! */
558 		freeenv(local_macstr);
559 		local_macstr = NULL;
560 	}
561 
562 	if (local_mac)
563 		return (0);
564 	return (-1);
565 }
566 
567 #define	HAL_MODE_HT20 (HAL_MODE_11NG_HT20 | HAL_MODE_11NA_HT20)
568 #define	HAL_MODE_HT40 \
569 	(HAL_MODE_11NG_HT40PLUS | HAL_MODE_11NG_HT40MINUS | \
570 	HAL_MODE_11NA_HT40PLUS | HAL_MODE_11NA_HT40MINUS)
571 int
572 ath_attach(u_int16_t devid, struct ath_softc *sc)
573 {
574 	struct ifnet *ifp;
575 	struct ieee80211com *ic;
576 	struct ath_hal *ah = NULL;
577 	HAL_STATUS status;
578 	int error = 0, i;
579 	u_int wmodes;
580 	uint8_t macaddr[IEEE80211_ADDR_LEN];
581 	int rx_chainmask, tx_chainmask;
582 	HAL_OPS_CONFIG ah_config;
583 
584 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
585 
586 	CURVNET_SET(vnet0);
587 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
588 	if (ifp == NULL) {
589 		device_printf(sc->sc_dev, "can not if_alloc()\n");
590 		error = ENOSPC;
591 		CURVNET_RESTORE();
592 		goto bad;
593 	}
594 	ic = ifp->if_l2com;
595 	ic->ic_softc = sc;
596 	ic->ic_name = device_get_nameunit(sc->sc_dev);
597 
598 	/* set these up early for if_printf use */
599 	if_initname(ifp, device_get_name(sc->sc_dev),
600 		device_get_unit(sc->sc_dev));
601 	CURVNET_RESTORE();
602 
603 	/*
604 	 * Configure the initial configuration data.
605 	 *
606 	 * This is stuff that may be needed early during attach
607 	 * rather than done via configuration calls later.
608 	 */
609 	bzero(&ah_config, sizeof(ah_config));
610 	ath_setup_hal_config(sc, &ah_config);
611 
612 	ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh,
613 	    sc->sc_eepromdata, &ah_config, &status);
614 	if (ah == NULL) {
615 		if_printf(ifp, "unable to attach hardware; HAL status %u\n",
616 			status);
617 		error = ENXIO;
618 		goto bad;
619 	}
620 	sc->sc_ah = ah;
621 	sc->sc_invalid = 0;	/* ready to go, enable interrupt handling */
622 #ifdef	ATH_DEBUG
623 	sc->sc_debug = ath_debug;
624 #endif
625 
626 	/*
627 	 * Setup the DMA/EDMA functions based on the current
628 	 * hardware support.
629 	 *
630 	 * This is required before the descriptors are allocated.
631 	 */
632 	if (ath_hal_hasedma(sc->sc_ah)) {
633 		sc->sc_isedma = 1;
634 		ath_recv_setup_edma(sc);
635 		ath_xmit_setup_edma(sc);
636 	} else {
637 		ath_recv_setup_legacy(sc);
638 		ath_xmit_setup_legacy(sc);
639 	}
640 
641 	if (ath_hal_hasmybeacon(sc->sc_ah)) {
642 		sc->sc_do_mybeacon = 1;
643 	}
644 
645 	/*
646 	 * Check if the MAC has multi-rate retry support.
647 	 * We do this by trying to setup a fake extended
648 	 * descriptor.  MAC's that don't have support will
649 	 * return false w/o doing anything.  MAC's that do
650 	 * support it will return true w/o doing anything.
651 	 */
652 	sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
653 
654 	/*
655 	 * Check if the device has hardware counters for PHY
656 	 * errors.  If so we need to enable the MIB interrupt
657 	 * so we can act on stat triggers.
658 	 */
659 	if (ath_hal_hwphycounters(ah))
660 		sc->sc_needmib = 1;
661 
662 	/*
663 	 * Get the hardware key cache size.
664 	 */
665 	sc->sc_keymax = ath_hal_keycachesize(ah);
666 	if (sc->sc_keymax > ATH_KEYMAX) {
667 		if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
668 			ATH_KEYMAX, sc->sc_keymax);
669 		sc->sc_keymax = ATH_KEYMAX;
670 	}
671 	/*
672 	 * Reset the key cache since some parts do not
673 	 * reset the contents on initial power up.
674 	 */
675 	for (i = 0; i < sc->sc_keymax; i++)
676 		ath_hal_keyreset(ah, i);
677 
678 	/*
679 	 * Collect the default channel list.
680 	 */
681 	error = ath_getchannels(sc);
682 	if (error != 0)
683 		goto bad;
684 
685 	/*
686 	 * Setup rate tables for all potential media types.
687 	 */
688 	ath_rate_setup(sc, IEEE80211_MODE_11A);
689 	ath_rate_setup(sc, IEEE80211_MODE_11B);
690 	ath_rate_setup(sc, IEEE80211_MODE_11G);
691 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
692 	ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
693 	ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
694 	ath_rate_setup(sc, IEEE80211_MODE_11NA);
695 	ath_rate_setup(sc, IEEE80211_MODE_11NG);
696 	ath_rate_setup(sc, IEEE80211_MODE_HALF);
697 	ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
698 
699 	/* NB: setup here so ath_rate_update is happy */
700 	ath_setcurmode(sc, IEEE80211_MODE_11A);
701 
702 	/*
703 	 * Allocate TX descriptors and populate the lists.
704 	 */
705 	error = ath_desc_alloc(sc);
706 	if (error != 0) {
707 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
708 		    error);
709 		goto bad;
710 	}
711 	error = ath_txdma_setup(sc);
712 	if (error != 0) {
713 		if_printf(ifp, "failed to allocate TX descriptors: %d\n",
714 		    error);
715 		goto bad;
716 	}
717 
718 	/*
719 	 * Allocate RX descriptors and populate the lists.
720 	 */
721 	error = ath_rxdma_setup(sc);
722 	if (error != 0) {
723 		if_printf(ifp, "failed to allocate RX descriptors: %d\n",
724 		    error);
725 		goto bad;
726 	}
727 
728 	callout_init_mtx(&sc->sc_cal_ch, &sc->sc_mtx, 0);
729 	callout_init_mtx(&sc->sc_wd_ch, &sc->sc_mtx, 0);
730 
731 	ATH_TXBUF_LOCK_INIT(sc);
732 
733 	sc->sc_tq = taskqueue_create("ath_taskq", M_NOWAIT,
734 		taskqueue_thread_enqueue, &sc->sc_tq);
735 	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET,
736 		"%s taskq", ifp->if_xname);
737 
738 	TASK_INIT(&sc->sc_rxtask, 0, sc->sc_rx.recv_tasklet, sc);
739 	TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
740 	TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
741 	TASK_INIT(&sc->sc_resettask,0, ath_reset_proc, sc);
742 	TASK_INIT(&sc->sc_txqtask, 0, ath_txq_sched_tasklet, sc);
743 	TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
744 
745 	/*
746 	 * Allocate hardware transmit queues: one queue for
747 	 * beacon frames and one data queue for each QoS
748 	 * priority.  Note that the hal handles resetting
749 	 * these queues at the needed time.
750 	 *
751 	 * XXX PS-Poll
752 	 */
753 	sc->sc_bhalq = ath_beaconq_setup(sc);
754 	if (sc->sc_bhalq == (u_int) -1) {
755 		if_printf(ifp, "unable to setup a beacon xmit queue!\n");
756 		error = EIO;
757 		goto bad2;
758 	}
759 	sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
760 	if (sc->sc_cabq == NULL) {
761 		if_printf(ifp, "unable to setup CAB xmit queue!\n");
762 		error = EIO;
763 		goto bad2;
764 	}
765 	/* NB: insure BK queue is the lowest priority h/w queue */
766 	if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
767 		if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
768 			ieee80211_wme_acnames[WME_AC_BK]);
769 		error = EIO;
770 		goto bad2;
771 	}
772 	if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
773 	    !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
774 	    !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
775 		/*
776 		 * Not enough hardware tx queues to properly do WME;
777 		 * just punt and assign them all to the same h/w queue.
778 		 * We could do a better job of this if, for example,
779 		 * we allocate queues when we switch from station to
780 		 * AP mode.
781 		 */
782 		if (sc->sc_ac2q[WME_AC_VI] != NULL)
783 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
784 		if (sc->sc_ac2q[WME_AC_BE] != NULL)
785 			ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
786 		sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
787 		sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
788 		sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
789 	}
790 
791 	/*
792 	 * Attach the TX completion function.
793 	 *
794 	 * The non-EDMA chips may have some special case optimisations;
795 	 * this method gives everyone a chance to attach cleanly.
796 	 */
797 	sc->sc_tx.xmit_attach_comp_func(sc);
798 
799 	/*
800 	 * Setup rate control.  Some rate control modules
801 	 * call back to change the anntena state so expose
802 	 * the necessary entry points.
803 	 * XXX maybe belongs in struct ath_ratectrl?
804 	 */
805 	sc->sc_setdefantenna = ath_setdefantenna;
806 	sc->sc_rc = ath_rate_attach(sc);
807 	if (sc->sc_rc == NULL) {
808 		error = EIO;
809 		goto bad2;
810 	}
811 
812 	/* Attach DFS module */
813 	if (! ath_dfs_attach(sc)) {
814 		device_printf(sc->sc_dev,
815 		    "%s: unable to attach DFS\n", __func__);
816 		error = EIO;
817 		goto bad2;
818 	}
819 
820 	/* Attach spectral module */
821 	if (ath_spectral_attach(sc) < 0) {
822 		device_printf(sc->sc_dev,
823 		    "%s: unable to attach spectral\n", __func__);
824 		error = EIO;
825 		goto bad2;
826 	}
827 
828 	/* Attach bluetooth coexistence module */
829 	if (ath_btcoex_attach(sc) < 0) {
830 		device_printf(sc->sc_dev,
831 		    "%s: unable to attach bluetooth coexistence\n", __func__);
832 		error = EIO;
833 		goto bad2;
834 	}
835 
836 	/* Attach LNA diversity module */
837 	if (ath_lna_div_attach(sc) < 0) {
838 		device_printf(sc->sc_dev,
839 		    "%s: unable to attach LNA diversity\n", __func__);
840 		error = EIO;
841 		goto bad2;
842 	}
843 
844 	/* Start DFS processing tasklet */
845 	TASK_INIT(&sc->sc_dfstask, 0, ath_dfs_tasklet, sc);
846 
847 	/* Configure LED state */
848 	sc->sc_blinking = 0;
849 	sc->sc_ledstate = 1;
850 	sc->sc_ledon = 0;			/* low true */
851 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
852 	callout_init(&sc->sc_ledtimer, 1);
853 
854 	/*
855 	 * Don't setup hardware-based blinking.
856 	 *
857 	 * Although some NICs may have this configured in the
858 	 * default reset register values, the user may wish
859 	 * to alter which pins have which function.
860 	 *
861 	 * The reference driver attaches the MAC network LED to GPIO1 and
862 	 * the MAC power LED to GPIO2.  However, the DWA-552 cardbus
863 	 * NIC has these reversed.
864 	 */
865 	sc->sc_hardled = (1 == 0);
866 	sc->sc_led_net_pin = -1;
867 	sc->sc_led_pwr_pin = -1;
868 	/*
869 	 * Auto-enable soft led processing for IBM cards and for
870 	 * 5211 minipci cards.  Users can also manually enable/disable
871 	 * support with a sysctl.
872 	 */
873 	sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
874 	ath_led_config(sc);
875 	ath_hal_setledstate(ah, HAL_LED_INIT);
876 
877 	ifp->if_softc = sc;
878 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
879 	ifp->if_transmit = ath_transmit;
880 	ifp->if_qflush = ath_qflush;
881 	ifp->if_ioctl = ath_ioctl;
882 	ifp->if_init = ath_init;
883 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
884 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
885 	IFQ_SET_READY(&ifp->if_snd);
886 
887 	ic->ic_ifp = ifp;
888 	/* XXX not right but it's not used anywhere important */
889 	ic->ic_phytype = IEEE80211_T_OFDM;
890 	ic->ic_opmode = IEEE80211_M_STA;
891 	ic->ic_caps =
892 		  IEEE80211_C_STA		/* station mode */
893 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
894 		| IEEE80211_C_HOSTAP		/* hostap mode */
895 		| IEEE80211_C_MONITOR		/* monitor mode */
896 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
897 		| IEEE80211_C_WDS		/* 4-address traffic works */
898 		| IEEE80211_C_MBSS		/* mesh point link mode */
899 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
900 		| IEEE80211_C_SHSLOT		/* short slot time supported */
901 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
902 #ifndef	ATH_ENABLE_11N
903 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
904 #endif
905 		| IEEE80211_C_TXFRAG		/* handle tx frags */
906 #ifdef	ATH_ENABLE_DFS
907 		| IEEE80211_C_DFS		/* Enable radar detection */
908 #endif
909 		| IEEE80211_C_PMGT		/* Station side power mgmt */
910 		| IEEE80211_C_SWSLEEP
911 		;
912 	/*
913 	 * Query the hal to figure out h/w crypto support.
914 	 */
915 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
916 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
917 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
918 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
919 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
920 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
921 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
922 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
923 	if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
924 		ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
925 		/*
926 		 * Check if h/w does the MIC and/or whether the
927 		 * separate key cache entries are required to
928 		 * handle both tx+rx MIC keys.
929 		 */
930 		if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
931 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
932 		/*
933 		 * If the h/w supports storing tx+rx MIC keys
934 		 * in one cache slot automatically enable use.
935 		 */
936 		if (ath_hal_hastkipsplit(ah) ||
937 		    !ath_hal_settkipsplit(ah, AH_FALSE))
938 			sc->sc_splitmic = 1;
939 		/*
940 		 * If the h/w can do TKIP MIC together with WME then
941 		 * we use it; otherwise we force the MIC to be done
942 		 * in software by the net80211 layer.
943 		 */
944 		if (ath_hal_haswmetkipmic(ah))
945 			sc->sc_wmetkipmic = 1;
946 	}
947 	sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
948 	/*
949 	 * Check for multicast key search support.
950 	 */
951 	if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
952 	    !ath_hal_getmcastkeysearch(sc->sc_ah)) {
953 		ath_hal_setmcastkeysearch(sc->sc_ah, 1);
954 	}
955 	sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
956 	/*
957 	 * Mark key cache slots associated with global keys
958 	 * as in use.  If we knew TKIP was not to be used we
959 	 * could leave the +32, +64, and +32+64 slots free.
960 	 */
961 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
962 		setbit(sc->sc_keymap, i);
963 		setbit(sc->sc_keymap, i+64);
964 		if (sc->sc_splitmic) {
965 			setbit(sc->sc_keymap, i+32);
966 			setbit(sc->sc_keymap, i+32+64);
967 		}
968 	}
969 	/*
970 	 * TPC support can be done either with a global cap or
971 	 * per-packet support.  The latter is not available on
972 	 * all parts.  We're a bit pedantic here as all parts
973 	 * support a global cap.
974 	 */
975 	if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
976 		ic->ic_caps |= IEEE80211_C_TXPMGT;
977 
978 	/*
979 	 * Mark WME capability only if we have sufficient
980 	 * hardware queues to do proper priority scheduling.
981 	 */
982 	if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
983 		ic->ic_caps |= IEEE80211_C_WME;
984 	/*
985 	 * Check for misc other capabilities.
986 	 */
987 	if (ath_hal_hasbursting(ah))
988 		ic->ic_caps |= IEEE80211_C_BURST;
989 	sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
990 	sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
991 	sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
992 	sc->sc_rxslink = ath_hal_self_linked_final_rxdesc(ah);
993 	sc->sc_rxtsf32 = ath_hal_has_long_rxdesc_tsf(ah);
994 	sc->sc_hasenforcetxop = ath_hal_hasenforcetxop(ah);
995 	sc->sc_rx_lnamixer = ath_hal_hasrxlnamixer(ah);
996 	sc->sc_hasdivcomb = ath_hal_hasdivantcomb(ah);
997 
998 	if (ath_hal_hasfastframes(ah))
999 		ic->ic_caps |= IEEE80211_C_FF;
1000 	wmodes = ath_hal_getwirelessmodes(ah);
1001 	if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
1002 		ic->ic_caps |= IEEE80211_C_TURBOP;
1003 #ifdef IEEE80211_SUPPORT_TDMA
1004 	if (ath_hal_macversion(ah) > 0x78) {
1005 		ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
1006 		ic->ic_tdma_update = ath_tdma_update;
1007 	}
1008 #endif
1009 
1010 	/*
1011 	 * TODO: enforce that at least this many frames are available
1012 	 * in the txbuf list before allowing data frames (raw or
1013 	 * otherwise) to be transmitted.
1014 	 */
1015 	sc->sc_txq_data_minfree = 10;
1016 	/*
1017 	 * Leave this as default to maintain legacy behaviour.
1018 	 * Shortening the cabq/mcastq may end up causing some
1019 	 * undesirable behaviour.
1020 	 */
1021 	sc->sc_txq_mcastq_maxdepth = ath_txbuf;
1022 
1023 	/*
1024 	 * How deep can the node software TX queue get whilst it's asleep.
1025 	 */
1026 	sc->sc_txq_node_psq_maxdepth = 16;
1027 
1028 	/*
1029 	 * Default the maximum queue depth for a given node
1030 	 * to 1/4'th the TX buffers, or 64, whichever
1031 	 * is larger.
1032 	 */
1033 	sc->sc_txq_node_maxdepth = MAX(64, ath_txbuf / 4);
1034 
1035 	/* Enable CABQ by default */
1036 	sc->sc_cabq_enable = 1;
1037 
1038 	/*
1039 	 * Allow the TX and RX chainmasks to be overridden by
1040 	 * environment variables and/or device.hints.
1041 	 *
1042 	 * This must be done early - before the hardware is
1043 	 * calibrated or before the 802.11n stream calculation
1044 	 * is done.
1045 	 */
1046 	if (resource_int_value(device_get_name(sc->sc_dev),
1047 	    device_get_unit(sc->sc_dev), "rx_chainmask",
1048 	    &rx_chainmask) == 0) {
1049 		device_printf(sc->sc_dev, "Setting RX chainmask to 0x%x\n",
1050 		    rx_chainmask);
1051 		(void) ath_hal_setrxchainmask(sc->sc_ah, rx_chainmask);
1052 	}
1053 	if (resource_int_value(device_get_name(sc->sc_dev),
1054 	    device_get_unit(sc->sc_dev), "tx_chainmask",
1055 	    &tx_chainmask) == 0) {
1056 		device_printf(sc->sc_dev, "Setting TX chainmask to 0x%x\n",
1057 		    tx_chainmask);
1058 		(void) ath_hal_settxchainmask(sc->sc_ah, tx_chainmask);
1059 	}
1060 
1061 	/*
1062 	 * Query the TX/RX chainmask configuration.
1063 	 *
1064 	 * This is only relevant for 11n devices.
1065 	 */
1066 	ath_hal_getrxchainmask(ah, &sc->sc_rxchainmask);
1067 	ath_hal_gettxchainmask(ah, &sc->sc_txchainmask);
1068 
1069 	/*
1070 	 * Disable MRR with protected frames by default.
1071 	 * Only 802.11n series NICs can handle this.
1072 	 */
1073 	sc->sc_mrrprot = 0;	/* XXX should be a capability */
1074 
1075 	/*
1076 	 * Query the enterprise mode information the HAL.
1077 	 */
1078 	if (ath_hal_getcapability(ah, HAL_CAP_ENTERPRISE_MODE, 0,
1079 	    &sc->sc_ent_cfg) == HAL_OK)
1080 		sc->sc_use_ent = 1;
1081 
1082 #ifdef	ATH_ENABLE_11N
1083 	/*
1084 	 * Query HT capabilities
1085 	 */
1086 	if (ath_hal_getcapability(ah, HAL_CAP_HT, 0, NULL) == HAL_OK &&
1087 	    (wmodes & (HAL_MODE_HT20 | HAL_MODE_HT40))) {
1088 		uint32_t rxs, txs;
1089 
1090 		device_printf(sc->sc_dev, "[HT] enabling HT modes\n");
1091 
1092 		sc->sc_mrrprot = 1;	/* XXX should be a capability */
1093 
1094 		ic->ic_htcaps = IEEE80211_HTC_HT	/* HT operation */
1095 			    | IEEE80211_HTC_AMPDU	/* A-MPDU tx/rx */
1096 			    | IEEE80211_HTC_AMSDU	/* A-MSDU tx/rx */
1097 			    | IEEE80211_HTCAP_MAXAMSDU_3839
1098 			    				/* max A-MSDU length */
1099 			    | IEEE80211_HTCAP_SMPS_OFF;	/* SM power save off */
1100 			;
1101 
1102 		/*
1103 		 * Enable short-GI for HT20 only if the hardware
1104 		 * advertises support.
1105 		 * Notably, anything earlier than the AR9287 doesn't.
1106 		 */
1107 		if ((ath_hal_getcapability(ah,
1108 		    HAL_CAP_HT20_SGI, 0, NULL) == HAL_OK) &&
1109 		    (wmodes & HAL_MODE_HT20)) {
1110 			device_printf(sc->sc_dev,
1111 			    "[HT] enabling short-GI in 20MHz mode\n");
1112 			ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
1113 		}
1114 
1115 		if (wmodes & HAL_MODE_HT40)
1116 			ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40
1117 			    |  IEEE80211_HTCAP_SHORTGI40;
1118 
1119 		/*
1120 		 * TX/RX streams need to be taken into account when
1121 		 * negotiating which MCS rates it'll receive and
1122 		 * what MCS rates are available for TX.
1123 		 */
1124 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 0, &txs);
1125 		(void) ath_hal_getcapability(ah, HAL_CAP_STREAMS, 1, &rxs);
1126 		ic->ic_txstream = txs;
1127 		ic->ic_rxstream = rxs;
1128 
1129 		/*
1130 		 * Setup TX and RX STBC based on what the HAL allows and
1131 		 * the currently configured chainmask set.
1132 		 * Ie - don't enable STBC TX if only one chain is enabled.
1133 		 * STBC RX is fine on a single RX chain; it just won't
1134 		 * provide any real benefit.
1135 		 */
1136 		if (ath_hal_getcapability(ah, HAL_CAP_RX_STBC, 0,
1137 		    NULL) == HAL_OK) {
1138 			sc->sc_rx_stbc = 1;
1139 			device_printf(sc->sc_dev,
1140 			    "[HT] 1 stream STBC receive enabled\n");
1141 			ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
1142 		}
1143 		if (txs > 1 && ath_hal_getcapability(ah, HAL_CAP_TX_STBC, 0,
1144 		    NULL) == HAL_OK) {
1145 			sc->sc_tx_stbc = 1;
1146 			device_printf(sc->sc_dev,
1147 			    "[HT] 1 stream STBC transmit enabled\n");
1148 			ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
1149 		}
1150 
1151 		(void) ath_hal_getcapability(ah, HAL_CAP_RTS_AGGR_LIMIT, 1,
1152 		    &sc->sc_rts_aggr_limit);
1153 		if (sc->sc_rts_aggr_limit != (64 * 1024))
1154 			device_printf(sc->sc_dev,
1155 			    "[HT] RTS aggregates limited to %d KiB\n",
1156 			    sc->sc_rts_aggr_limit / 1024);
1157 
1158 		device_printf(sc->sc_dev,
1159 		    "[HT] %d RX streams; %d TX streams\n", rxs, txs);
1160 	}
1161 #endif
1162 
1163 	/*
1164 	 * Initial aggregation settings.
1165 	 */
1166 	sc->sc_hwq_limit_aggr = ATH_AGGR_MIN_QDEPTH;
1167 	sc->sc_hwq_limit_nonaggr = ATH_NONAGGR_MIN_QDEPTH;
1168 	sc->sc_tid_hwq_lo = ATH_AGGR_SCHED_LOW;
1169 	sc->sc_tid_hwq_hi = ATH_AGGR_SCHED_HIGH;
1170 	sc->sc_aggr_limit = ATH_AGGR_MAXSIZE;
1171 	sc->sc_delim_min_pad = 0;
1172 
1173 	/*
1174 	 * Check if the hardware requires PCI register serialisation.
1175 	 * Some of the Owl based MACs require this.
1176 	 */
1177 	if (mp_ncpus > 1 &&
1178 	    ath_hal_getcapability(ah, HAL_CAP_SERIALISE_WAR,
1179 	     0, NULL) == HAL_OK) {
1180 		sc->sc_ah->ah_config.ah_serialise_reg_war = 1;
1181 		device_printf(sc->sc_dev,
1182 		    "Enabling register serialisation\n");
1183 	}
1184 
1185 	/*
1186 	 * Initialise the deferred completed RX buffer list.
1187 	 */
1188 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]);
1189 	TAILQ_INIT(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]);
1190 
1191 	/*
1192 	 * Indicate we need the 802.11 header padded to a
1193 	 * 32-bit boundary for 4-address and QoS frames.
1194 	 */
1195 	ic->ic_flags |= IEEE80211_F_DATAPAD;
1196 
1197 	/*
1198 	 * Query the hal about antenna support.
1199 	 */
1200 	sc->sc_defant = ath_hal_getdefantenna(ah);
1201 
1202 	/*
1203 	 * Not all chips have the VEOL support we want to
1204 	 * use with IBSS beacons; check here for it.
1205 	 */
1206 	sc->sc_hasveol = ath_hal_hasveol(ah);
1207 
1208 	/* get mac address from kenv first, then hardware */
1209 	if (ath_fetch_mac_kenv(sc, macaddr) == 0) {
1210 		/* Tell the HAL now about the new MAC */
1211 		ath_hal_setmac(ah, macaddr);
1212 	} else {
1213 		ath_hal_getmac(ah, macaddr);
1214 	}
1215 
1216 	if (sc->sc_hasbmask)
1217 		ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
1218 
1219 	/* NB: used to size node table key mapping array */
1220 	ic->ic_max_keyix = sc->sc_keymax;
1221 	/* call MI attach routine. */
1222 	ieee80211_ifattach(ic, macaddr);
1223 	ic->ic_setregdomain = ath_setregdomain;
1224 	ic->ic_getradiocaps = ath_getradiocaps;
1225 	sc->sc_opmode = HAL_M_STA;
1226 
1227 	/* override default methods */
1228 	ic->ic_newassoc = ath_newassoc;
1229 	ic->ic_updateslot = ath_updateslot;
1230 	ic->ic_wme.wme_update = ath_wme_update;
1231 	ic->ic_vap_create = ath_vap_create;
1232 	ic->ic_vap_delete = ath_vap_delete;
1233 	ic->ic_raw_xmit = ath_raw_xmit;
1234 	ic->ic_update_mcast = ath_update_mcast;
1235 	ic->ic_update_promisc = ath_update_promisc;
1236 	ic->ic_node_alloc = ath_node_alloc;
1237 	sc->sc_node_free = ic->ic_node_free;
1238 	ic->ic_node_free = ath_node_free;
1239 	sc->sc_node_cleanup = ic->ic_node_cleanup;
1240 	ic->ic_node_cleanup = ath_node_cleanup;
1241 	ic->ic_node_getsignal = ath_node_getsignal;
1242 	ic->ic_scan_start = ath_scan_start;
1243 	ic->ic_scan_end = ath_scan_end;
1244 	ic->ic_set_channel = ath_set_channel;
1245 #ifdef	ATH_ENABLE_11N
1246 	/* 802.11n specific - but just override anyway */
1247 	sc->sc_addba_request = ic->ic_addba_request;
1248 	sc->sc_addba_response = ic->ic_addba_response;
1249 	sc->sc_addba_stop = ic->ic_addba_stop;
1250 	sc->sc_bar_response = ic->ic_bar_response;
1251 	sc->sc_addba_response_timeout = ic->ic_addba_response_timeout;
1252 
1253 	ic->ic_addba_request = ath_addba_request;
1254 	ic->ic_addba_response = ath_addba_response;
1255 	ic->ic_addba_response_timeout = ath_addba_response_timeout;
1256 	ic->ic_addba_stop = ath_addba_stop;
1257 	ic->ic_bar_response = ath_bar_response;
1258 
1259 	ic->ic_update_chw = ath_update_chw;
1260 #endif	/* ATH_ENABLE_11N */
1261 
1262 #ifdef	ATH_ENABLE_RADIOTAP_VENDOR_EXT
1263 	/*
1264 	 * There's one vendor bitmap entry in the RX radiotap
1265 	 * header; make sure that's taken into account.
1266 	 */
1267 	ieee80211_radiotap_attachv(ic,
1268 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th), 0,
1269 		ATH_TX_RADIOTAP_PRESENT,
1270 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th), 1,
1271 		ATH_RX_RADIOTAP_PRESENT);
1272 #else
1273 	/*
1274 	 * No vendor bitmap/extensions are present.
1275 	 */
1276 	ieee80211_radiotap_attach(ic,
1277 	    &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
1278 		ATH_TX_RADIOTAP_PRESENT,
1279 	    &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
1280 		ATH_RX_RADIOTAP_PRESENT);
1281 #endif	/* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
1282 
1283 	/*
1284 	 * Setup the ALQ logging if required
1285 	 */
1286 #ifdef	ATH_DEBUG_ALQ
1287 	if_ath_alq_init(&sc->sc_alq, device_get_nameunit(sc->sc_dev));
1288 	if_ath_alq_setcfg(&sc->sc_alq,
1289 	    sc->sc_ah->ah_macVersion,
1290 	    sc->sc_ah->ah_macRev,
1291 	    sc->sc_ah->ah_phyRev,
1292 	    sc->sc_ah->ah_magic);
1293 #endif
1294 
1295 	/*
1296 	 * Setup dynamic sysctl's now that country code and
1297 	 * regdomain are available from the hal.
1298 	 */
1299 	ath_sysctlattach(sc);
1300 	ath_sysctl_stats_attach(sc);
1301 	ath_sysctl_hal_attach(sc);
1302 
1303 	if (bootverbose)
1304 		ieee80211_announce(ic);
1305 	ath_announce(sc);
1306 
1307 	/*
1308 	 * Put it to sleep for now.
1309 	 */
1310 	ATH_LOCK(sc);
1311 	ath_power_setpower(sc, HAL_PM_FULL_SLEEP);
1312 	ATH_UNLOCK(sc);
1313 
1314 	return 0;
1315 bad2:
1316 	ath_tx_cleanup(sc);
1317 	ath_desc_free(sc);
1318 	ath_txdma_teardown(sc);
1319 	ath_rxdma_teardown(sc);
1320 bad:
1321 	if (ah)
1322 		ath_hal_detach(ah);
1323 
1324 	/*
1325 	 * To work around scoping issues with CURVNET_SET/CURVNET_RESTORE..
1326 	 */
1327 	if (ifp != NULL && ifp->if_vnet) {
1328 		CURVNET_SET(ifp->if_vnet);
1329 		if_free(ifp);
1330 		CURVNET_RESTORE();
1331 	} else if (ifp != NULL)
1332 		if_free(ifp);
1333 	sc->sc_invalid = 1;
1334 	return error;
1335 }
1336 
1337 int
1338 ath_detach(struct ath_softc *sc)
1339 {
1340 	struct ifnet *ifp = sc->sc_ifp;
1341 
1342 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1343 		__func__, ifp->if_flags);
1344 
1345 	/*
1346 	 * NB: the order of these is important:
1347 	 * o stop the chip so no more interrupts will fire
1348 	 * o call the 802.11 layer before detaching the hal to
1349 	 *   insure callbacks into the driver to delete global
1350 	 *   key cache entries can be handled
1351 	 * o free the taskqueue which drains any pending tasks
1352 	 * o reclaim the tx queue data structures after calling
1353 	 *   the 802.11 layer as we'll get called back to reclaim
1354 	 *   node state and potentially want to use them
1355 	 * o to cleanup the tx queues the hal is called, so detach
1356 	 *   it last
1357 	 * Other than that, it's straightforward...
1358 	 */
1359 
1360 	/*
1361 	 * XXX Wake the hardware up first.  ath_stop() will still
1362 	 * wake it up first, but I'd rather do it here just to
1363 	 * ensure it's awake.
1364 	 */
1365 	ATH_LOCK(sc);
1366 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1367 	ath_power_setpower(sc, HAL_PM_AWAKE);
1368 	ATH_UNLOCK(sc);
1369 
1370 	/*
1371 	 * Stop things cleanly.
1372 	 */
1373 	ath_stop(ifp);
1374 
1375 	ieee80211_ifdetach(ifp->if_l2com);
1376 	taskqueue_free(sc->sc_tq);
1377 #ifdef ATH_TX99_DIAG
1378 	if (sc->sc_tx99 != NULL)
1379 		sc->sc_tx99->detach(sc->sc_tx99);
1380 #endif
1381 	ath_rate_detach(sc->sc_rc);
1382 #ifdef	ATH_DEBUG_ALQ
1383 	if_ath_alq_tidyup(&sc->sc_alq);
1384 #endif
1385 	ath_lna_div_detach(sc);
1386 	ath_btcoex_detach(sc);
1387 	ath_spectral_detach(sc);
1388 	ath_dfs_detach(sc);
1389 	ath_desc_free(sc);
1390 	ath_txdma_teardown(sc);
1391 	ath_rxdma_teardown(sc);
1392 	ath_tx_cleanup(sc);
1393 	ath_hal_detach(sc->sc_ah);	/* NB: sets chip in full sleep */
1394 
1395 	CURVNET_SET(ifp->if_vnet);
1396 	if_free(ifp);
1397 	CURVNET_RESTORE();
1398 
1399 	return 0;
1400 }
1401 
1402 /*
1403  * MAC address handling for multiple BSS on the same radio.
1404  * The first vap uses the MAC address from the EEPROM.  For
1405  * subsequent vap's we set the U/L bit (bit 1) in the MAC
1406  * address and use the next six bits as an index.
1407  */
1408 static void
1409 assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
1410 {
1411 	int i;
1412 
1413 	if (clone && sc->sc_hasbmask) {
1414 		/* NB: we only do this if h/w supports multiple bssid */
1415 		for (i = 0; i < 8; i++)
1416 			if ((sc->sc_bssidmask & (1<<i)) == 0)
1417 				break;
1418 		if (i != 0)
1419 			mac[0] |= (i << 2)|0x2;
1420 	} else
1421 		i = 0;
1422 	sc->sc_bssidmask |= 1<<i;
1423 	sc->sc_hwbssidmask[0] &= ~mac[0];
1424 	if (i == 0)
1425 		sc->sc_nbssid0++;
1426 }
1427 
1428 static void
1429 reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
1430 {
1431 	int i = mac[0] >> 2;
1432 	uint8_t mask;
1433 
1434 	if (i != 0 || --sc->sc_nbssid0 == 0) {
1435 		sc->sc_bssidmask &= ~(1<<i);
1436 		/* recalculate bssid mask from remaining addresses */
1437 		mask = 0xff;
1438 		for (i = 1; i < 8; i++)
1439 			if (sc->sc_bssidmask & (1<<i))
1440 				mask &= ~((i<<2)|0x2);
1441 		sc->sc_hwbssidmask[0] |= mask;
1442 	}
1443 }
1444 
1445 /*
1446  * Assign a beacon xmit slot.  We try to space out
1447  * assignments so when beacons are staggered the
1448  * traffic coming out of the cab q has maximal time
1449  * to go out before the next beacon is scheduled.
1450  */
1451 static int
1452 assign_bslot(struct ath_softc *sc)
1453 {
1454 	u_int slot, free;
1455 
1456 	free = 0;
1457 	for (slot = 0; slot < ATH_BCBUF; slot++)
1458 		if (sc->sc_bslot[slot] == NULL) {
1459 			if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
1460 			    sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
1461 				return slot;
1462 			free = slot;
1463 			/* NB: keep looking for a double slot */
1464 		}
1465 	return free;
1466 }
1467 
1468 static struct ieee80211vap *
1469 ath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
1470     enum ieee80211_opmode opmode, int flags,
1471     const uint8_t bssid[IEEE80211_ADDR_LEN],
1472     const uint8_t mac0[IEEE80211_ADDR_LEN])
1473 {
1474 	struct ath_softc *sc = ic->ic_ifp->if_softc;
1475 	struct ath_vap *avp;
1476 	struct ieee80211vap *vap;
1477 	uint8_t mac[IEEE80211_ADDR_LEN];
1478 	int needbeacon, error;
1479 	enum ieee80211_opmode ic_opmode;
1480 
1481 	avp = (struct ath_vap *) malloc(sizeof(struct ath_vap),
1482 	    M_80211_VAP, M_WAITOK | M_ZERO);
1483 	needbeacon = 0;
1484 	IEEE80211_ADDR_COPY(mac, mac0);
1485 
1486 	ATH_LOCK(sc);
1487 	ic_opmode = opmode;		/* default to opmode of new vap */
1488 	switch (opmode) {
1489 	case IEEE80211_M_STA:
1490 		if (sc->sc_nstavaps != 0) {	/* XXX only 1 for now */
1491 			device_printf(sc->sc_dev, "only 1 sta vap supported\n");
1492 			goto bad;
1493 		}
1494 		if (sc->sc_nvaps) {
1495 			/*
1496 			 * With multiple vaps we must fall back
1497 			 * to s/w beacon miss handling.
1498 			 */
1499 			flags |= IEEE80211_CLONE_NOBEACONS;
1500 		}
1501 		if (flags & IEEE80211_CLONE_NOBEACONS) {
1502 			/*
1503 			 * Station mode w/o beacons are implemented w/ AP mode.
1504 			 */
1505 			ic_opmode = IEEE80211_M_HOSTAP;
1506 		}
1507 		break;
1508 	case IEEE80211_M_IBSS:
1509 		if (sc->sc_nvaps != 0) {	/* XXX only 1 for now */
1510 			device_printf(sc->sc_dev,
1511 			    "only 1 ibss vap supported\n");
1512 			goto bad;
1513 		}
1514 		needbeacon = 1;
1515 		break;
1516 	case IEEE80211_M_AHDEMO:
1517 #ifdef IEEE80211_SUPPORT_TDMA
1518 		if (flags & IEEE80211_CLONE_TDMA) {
1519 			if (sc->sc_nvaps != 0) {
1520 				device_printf(sc->sc_dev,
1521 				    "only 1 tdma vap supported\n");
1522 				goto bad;
1523 			}
1524 			needbeacon = 1;
1525 			flags |= IEEE80211_CLONE_NOBEACONS;
1526 		}
1527 		/* fall thru... */
1528 #endif
1529 	case IEEE80211_M_MONITOR:
1530 		if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
1531 			/*
1532 			 * Adopt existing mode.  Adding a monitor or ahdemo
1533 			 * vap to an existing configuration is of dubious
1534 			 * value but should be ok.
1535 			 */
1536 			/* XXX not right for monitor mode */
1537 			ic_opmode = ic->ic_opmode;
1538 		}
1539 		break;
1540 	case IEEE80211_M_HOSTAP:
1541 	case IEEE80211_M_MBSS:
1542 		needbeacon = 1;
1543 		break;
1544 	case IEEE80211_M_WDS:
1545 		if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
1546 			device_printf(sc->sc_dev,
1547 			    "wds not supported in sta mode\n");
1548 			goto bad;
1549 		}
1550 		/*
1551 		 * Silently remove any request for a unique
1552 		 * bssid; WDS vap's always share the local
1553 		 * mac address.
1554 		 */
1555 		flags &= ~IEEE80211_CLONE_BSSID;
1556 		if (sc->sc_nvaps == 0)
1557 			ic_opmode = IEEE80211_M_HOSTAP;
1558 		else
1559 			ic_opmode = ic->ic_opmode;
1560 		break;
1561 	default:
1562 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
1563 		goto bad;
1564 	}
1565 	/*
1566 	 * Check that a beacon buffer is available; the code below assumes it.
1567 	 */
1568 	if (needbeacon & TAILQ_EMPTY(&sc->sc_bbuf)) {
1569 		device_printf(sc->sc_dev, "no beacon buffer available\n");
1570 		goto bad;
1571 	}
1572 
1573 	/* STA, AHDEMO? */
1574 	if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
1575 		assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
1576 		ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1577 	}
1578 
1579 	vap = &avp->av_vap;
1580 	/* XXX can't hold mutex across if_alloc */
1581 	ATH_UNLOCK(sc);
1582 	error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
1583 	    bssid, mac);
1584 	ATH_LOCK(sc);
1585 	if (error != 0) {
1586 		device_printf(sc->sc_dev, "%s: error %d creating vap\n",
1587 		    __func__, error);
1588 		goto bad2;
1589 	}
1590 
1591 	/* h/w crypto support */
1592 	vap->iv_key_alloc = ath_key_alloc;
1593 	vap->iv_key_delete = ath_key_delete;
1594 	vap->iv_key_set = ath_key_set;
1595 	vap->iv_key_update_begin = ath_key_update_begin;
1596 	vap->iv_key_update_end = ath_key_update_end;
1597 
1598 	/* override various methods */
1599 	avp->av_recv_mgmt = vap->iv_recv_mgmt;
1600 	vap->iv_recv_mgmt = ath_recv_mgmt;
1601 	vap->iv_reset = ath_reset_vap;
1602 	vap->iv_update_beacon = ath_beacon_update;
1603 	avp->av_newstate = vap->iv_newstate;
1604 	vap->iv_newstate = ath_newstate;
1605 	avp->av_bmiss = vap->iv_bmiss;
1606 	vap->iv_bmiss = ath_bmiss_vap;
1607 
1608 	avp->av_node_ps = vap->iv_node_ps;
1609 	vap->iv_node_ps = ath_node_powersave;
1610 
1611 	avp->av_set_tim = vap->iv_set_tim;
1612 	vap->iv_set_tim = ath_node_set_tim;
1613 
1614 	avp->av_recv_pspoll = vap->iv_recv_pspoll;
1615 	vap->iv_recv_pspoll = ath_node_recv_pspoll;
1616 
1617 	/* Set default parameters */
1618 
1619 	/*
1620 	 * Anything earlier than some AR9300 series MACs don't
1621 	 * support a smaller MPDU density.
1622 	 */
1623 	vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_8;
1624 	/*
1625 	 * All NICs can handle the maximum size, however
1626 	 * AR5416 based MACs can only TX aggregates w/ RTS
1627 	 * protection when the total aggregate size is <= 8k.
1628 	 * However, for now that's enforced by the TX path.
1629 	 */
1630 	vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_64K;
1631 
1632 	avp->av_bslot = -1;
1633 	if (needbeacon) {
1634 		/*
1635 		 * Allocate beacon state and setup the q for buffered
1636 		 * multicast frames.  We know a beacon buffer is
1637 		 * available because we checked above.
1638 		 */
1639 		avp->av_bcbuf = TAILQ_FIRST(&sc->sc_bbuf);
1640 		TAILQ_REMOVE(&sc->sc_bbuf, avp->av_bcbuf, bf_list);
1641 		if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1642 			/*
1643 			 * Assign the vap to a beacon xmit slot.  As above
1644 			 * this cannot fail to find a free one.
1645 			 */
1646 			avp->av_bslot = assign_bslot(sc);
1647 			KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1648 			    ("beacon slot %u not empty", avp->av_bslot));
1649 			sc->sc_bslot[avp->av_bslot] = vap;
1650 			sc->sc_nbcnvaps++;
1651 		}
1652 		if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1653 			/*
1654 			 * Multple vaps are to transmit beacons and we
1655 			 * have h/w support for TSF adjusting; enable
1656 			 * use of staggered beacons.
1657 			 */
1658 			sc->sc_stagbeacons = 1;
1659 		}
1660 		ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1661 	}
1662 
1663 	ic->ic_opmode = ic_opmode;
1664 	if (opmode != IEEE80211_M_WDS) {
1665 		sc->sc_nvaps++;
1666 		if (opmode == IEEE80211_M_STA)
1667 			sc->sc_nstavaps++;
1668 		if (opmode == IEEE80211_M_MBSS)
1669 			sc->sc_nmeshvaps++;
1670 	}
1671 	switch (ic_opmode) {
1672 	case IEEE80211_M_IBSS:
1673 		sc->sc_opmode = HAL_M_IBSS;
1674 		break;
1675 	case IEEE80211_M_STA:
1676 		sc->sc_opmode = HAL_M_STA;
1677 		break;
1678 	case IEEE80211_M_AHDEMO:
1679 #ifdef IEEE80211_SUPPORT_TDMA
1680 		if (vap->iv_caps & IEEE80211_C_TDMA) {
1681 			sc->sc_tdma = 1;
1682 			/* NB: disable tsf adjust */
1683 			sc->sc_stagbeacons = 0;
1684 		}
1685 		/*
1686 		 * NB: adhoc demo mode is a pseudo mode; to the hal it's
1687 		 * just ap mode.
1688 		 */
1689 		/* fall thru... */
1690 #endif
1691 	case IEEE80211_M_HOSTAP:
1692 	case IEEE80211_M_MBSS:
1693 		sc->sc_opmode = HAL_M_HOSTAP;
1694 		break;
1695 	case IEEE80211_M_MONITOR:
1696 		sc->sc_opmode = HAL_M_MONITOR;
1697 		break;
1698 	default:
1699 		/* XXX should not happen */
1700 		break;
1701 	}
1702 	if (sc->sc_hastsfadd) {
1703 		/*
1704 		 * Configure whether or not TSF adjust should be done.
1705 		 */
1706 		ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1707 	}
1708 	if (flags & IEEE80211_CLONE_NOBEACONS) {
1709 		/*
1710 		 * Enable s/w beacon miss handling.
1711 		 */
1712 		sc->sc_swbmiss = 1;
1713 	}
1714 	ATH_UNLOCK(sc);
1715 
1716 	/* complete setup */
1717 	ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1718 	return vap;
1719 bad2:
1720 	reclaim_address(sc, mac);
1721 	ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1722 bad:
1723 	free(avp, M_80211_VAP);
1724 	ATH_UNLOCK(sc);
1725 	return NULL;
1726 }
1727 
1728 static void
1729 ath_vap_delete(struct ieee80211vap *vap)
1730 {
1731 	struct ieee80211com *ic = vap->iv_ic;
1732 	struct ifnet *ifp = ic->ic_ifp;
1733 	struct ath_softc *sc = ifp->if_softc;
1734 	struct ath_hal *ah = sc->sc_ah;
1735 	struct ath_vap *avp = ATH_VAP(vap);
1736 
1737 	ATH_LOCK(sc);
1738 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1739 	ATH_UNLOCK(sc);
1740 
1741 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
1742 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1743 		/*
1744 		 * Quiesce the hardware while we remove the vap.  In
1745 		 * particular we need to reclaim all references to
1746 		 * the vap state by any frames pending on the tx queues.
1747 		 */
1748 		ath_hal_intrset(ah, 0);		/* disable interrupts */
1749 		/* XXX Do all frames from all vaps/nodes need draining here? */
1750 		ath_stoprecv(sc, 1);		/* stop recv side */
1751 		ath_draintxq(sc, ATH_RESET_DEFAULT);		/* stop hw xmit side */
1752 	}
1753 
1754 	/* .. leave the hardware awake for now. */
1755 
1756 	ieee80211_vap_detach(vap);
1757 
1758 	/*
1759 	 * XXX Danger Will Robinson! Danger!
1760 	 *
1761 	 * Because ieee80211_vap_detach() can queue a frame (the station
1762 	 * diassociate message?) after we've drained the TXQ and
1763 	 * flushed the software TXQ, we will end up with a frame queued
1764 	 * to a node whose vap is about to be freed.
1765 	 *
1766 	 * To work around this, flush the hardware/software again.
1767 	 * This may be racy - the ath task may be running and the packet
1768 	 * may be being scheduled between sw->hw txq. Tsk.
1769 	 *
1770 	 * TODO: figure out why a new node gets allocated somewhere around
1771 	 * here (after the ath_tx_swq() call; and after an ath_stop_locked()
1772 	 * call!)
1773 	 */
1774 
1775 	ath_draintxq(sc, ATH_RESET_DEFAULT);
1776 
1777 	ATH_LOCK(sc);
1778 	/*
1779 	 * Reclaim beacon state.  Note this must be done before
1780 	 * the vap instance is reclaimed as we may have a reference
1781 	 * to it in the buffer for the beacon frame.
1782 	 */
1783 	if (avp->av_bcbuf != NULL) {
1784 		if (avp->av_bslot != -1) {
1785 			sc->sc_bslot[avp->av_bslot] = NULL;
1786 			sc->sc_nbcnvaps--;
1787 		}
1788 		ath_beacon_return(sc, avp->av_bcbuf);
1789 		avp->av_bcbuf = NULL;
1790 		if (sc->sc_nbcnvaps == 0) {
1791 			sc->sc_stagbeacons = 0;
1792 			if (sc->sc_hastsfadd)
1793 				ath_hal_settsfadjust(sc->sc_ah, 0);
1794 		}
1795 		/*
1796 		 * Reclaim any pending mcast frames for the vap.
1797 		 */
1798 		ath_tx_draintxq(sc, &avp->av_mcastq);
1799 	}
1800 	/*
1801 	 * Update bookkeeping.
1802 	 */
1803 	if (vap->iv_opmode == IEEE80211_M_STA) {
1804 		sc->sc_nstavaps--;
1805 		if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1806 			sc->sc_swbmiss = 0;
1807 	} else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1808 	    vap->iv_opmode == IEEE80211_M_MBSS) {
1809 		reclaim_address(sc, vap->iv_myaddr);
1810 		ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1811 		if (vap->iv_opmode == IEEE80211_M_MBSS)
1812 			sc->sc_nmeshvaps--;
1813 	}
1814 	if (vap->iv_opmode != IEEE80211_M_WDS)
1815 		sc->sc_nvaps--;
1816 #ifdef IEEE80211_SUPPORT_TDMA
1817 	/* TDMA operation ceases when the last vap is destroyed */
1818 	if (sc->sc_tdma && sc->sc_nvaps == 0) {
1819 		sc->sc_tdma = 0;
1820 		sc->sc_swbmiss = 0;
1821 	}
1822 #endif
1823 	free(avp, M_80211_VAP);
1824 
1825 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1826 		/*
1827 		 * Restart rx+tx machines if still running (RUNNING will
1828 		 * be reset if we just destroyed the last vap).
1829 		 */
1830 		if (ath_startrecv(sc) != 0)
1831 			if_printf(ifp, "%s: unable to restart recv logic\n",
1832 			    __func__);
1833 		if (sc->sc_beacons) {		/* restart beacons */
1834 #ifdef IEEE80211_SUPPORT_TDMA
1835 			if (sc->sc_tdma)
1836 				ath_tdma_config(sc, NULL);
1837 			else
1838 #endif
1839 				ath_beacon_config(sc, NULL);
1840 		}
1841 		ath_hal_intrset(ah, sc->sc_imask);
1842 	}
1843 
1844 	/* Ok, let the hardware asleep. */
1845 	ath_power_restore_power_state(sc);
1846 	ATH_UNLOCK(sc);
1847 }
1848 
1849 void
1850 ath_suspend(struct ath_softc *sc)
1851 {
1852 	struct ifnet *ifp = sc->sc_ifp;
1853 	struct ieee80211com *ic = ifp->if_l2com;
1854 
1855 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1856 		__func__, ifp->if_flags);
1857 
1858 	sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1859 
1860 	ieee80211_suspend_all(ic);
1861 	/*
1862 	 * NB: don't worry about putting the chip in low power
1863 	 * mode; pci will power off our socket on suspend and
1864 	 * CardBus detaches the device.
1865 	 *
1866 	 * XXX TODO: well, that's great, except for non-cardbus
1867 	 * devices!
1868 	 */
1869 
1870 	/*
1871 	 * XXX This doesn't wait until all pending taskqueue
1872 	 * items and parallel transmit/receive/other threads
1873 	 * are running!
1874 	 */
1875 	ath_hal_intrset(sc->sc_ah, 0);
1876 	taskqueue_block(sc->sc_tq);
1877 
1878 	ATH_LOCK(sc);
1879 	callout_stop(&sc->sc_cal_ch);
1880 	ATH_UNLOCK(sc);
1881 
1882 	/*
1883 	 * XXX ensure sc_invalid is 1
1884 	 */
1885 
1886 	/* Disable the PCIe PHY, complete with workarounds */
1887 	ath_hal_enablepcie(sc->sc_ah, 1, 1);
1888 }
1889 
1890 /*
1891  * Reset the key cache since some parts do not reset the
1892  * contents on resume.  First we clear all entries, then
1893  * re-load keys that the 802.11 layer assumes are setup
1894  * in h/w.
1895  */
1896 static void
1897 ath_reset_keycache(struct ath_softc *sc)
1898 {
1899 	struct ifnet *ifp = sc->sc_ifp;
1900 	struct ieee80211com *ic = ifp->if_l2com;
1901 	struct ath_hal *ah = sc->sc_ah;
1902 	int i;
1903 
1904 	ATH_LOCK(sc);
1905 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1906 	for (i = 0; i < sc->sc_keymax; i++)
1907 		ath_hal_keyreset(ah, i);
1908 	ath_power_restore_power_state(sc);
1909 	ATH_UNLOCK(sc);
1910 	ieee80211_crypto_reload_keys(ic);
1911 }
1912 
1913 /*
1914  * Fetch the current chainmask configuration based on the current
1915  * operating channel and options.
1916  */
1917 static void
1918 ath_update_chainmasks(struct ath_softc *sc, struct ieee80211_channel *chan)
1919 {
1920 
1921 	/*
1922 	 * Set TX chainmask to the currently configured chainmask;
1923 	 * the TX chainmask depends upon the current operating mode.
1924 	 */
1925 	sc->sc_cur_rxchainmask = sc->sc_rxchainmask;
1926 	if (IEEE80211_IS_CHAN_HT(chan)) {
1927 		sc->sc_cur_txchainmask = sc->sc_txchainmask;
1928 	} else {
1929 		sc->sc_cur_txchainmask = 1;
1930 	}
1931 
1932 	DPRINTF(sc, ATH_DEBUG_RESET,
1933 	    "%s: TX chainmask is now 0x%x, RX is now 0x%x\n",
1934 	    __func__,
1935 	    sc->sc_cur_txchainmask,
1936 	    sc->sc_cur_rxchainmask);
1937 }
1938 
1939 void
1940 ath_resume(struct ath_softc *sc)
1941 {
1942 	struct ifnet *ifp = sc->sc_ifp;
1943 	struct ieee80211com *ic = ifp->if_l2com;
1944 	struct ath_hal *ah = sc->sc_ah;
1945 	HAL_STATUS status;
1946 
1947 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1948 		__func__, ifp->if_flags);
1949 
1950 	/* Re-enable PCIe, re-enable the PCIe bus */
1951 	ath_hal_enablepcie(ah, 0, 0);
1952 
1953 	/*
1954 	 * Must reset the chip before we reload the
1955 	 * keycache as we were powered down on suspend.
1956 	 */
1957 	ath_update_chainmasks(sc,
1958 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan);
1959 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
1960 	    sc->sc_cur_rxchainmask);
1961 
1962 	/* Ensure we set the current power state to on */
1963 	ATH_LOCK(sc);
1964 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
1965 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
1966 	ath_power_setpower(sc, HAL_PM_AWAKE);
1967 	ATH_UNLOCK(sc);
1968 
1969 	ath_hal_reset(ah, sc->sc_opmode,
1970 	    sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1971 	    AH_FALSE, &status);
1972 	ath_reset_keycache(sc);
1973 
1974 	ATH_RX_LOCK(sc);
1975 	sc->sc_rx_stopped = 1;
1976 	sc->sc_rx_resetted = 1;
1977 	ATH_RX_UNLOCK(sc);
1978 
1979 	/* Let DFS at it in case it's a DFS channel */
1980 	ath_dfs_radar_enable(sc, ic->ic_curchan);
1981 
1982 	/* Let spectral at in case spectral is enabled */
1983 	ath_spectral_enable(sc, ic->ic_curchan);
1984 
1985 	/*
1986 	 * Let bluetooth coexistence at in case it's needed for this channel
1987 	 */
1988 	ath_btcoex_enable(sc, ic->ic_curchan);
1989 
1990 	/*
1991 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
1992 	 * support it.
1993 	 */
1994 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
1995 		ath_hal_setenforcetxop(sc->sc_ah, 1);
1996 	else
1997 		ath_hal_setenforcetxop(sc->sc_ah, 0);
1998 
1999 	/* Restore the LED configuration */
2000 	ath_led_config(sc);
2001 	ath_hal_setledstate(ah, HAL_LED_INIT);
2002 
2003 	if (sc->sc_resume_up)
2004 		ieee80211_resume_all(ic);
2005 
2006 	ATH_LOCK(sc);
2007 	ath_power_restore_power_state(sc);
2008 	ATH_UNLOCK(sc);
2009 
2010 	/* XXX beacons ? */
2011 }
2012 
2013 void
2014 ath_shutdown(struct ath_softc *sc)
2015 {
2016 	struct ifnet *ifp = sc->sc_ifp;
2017 
2018 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
2019 		__func__, ifp->if_flags);
2020 
2021 	ath_stop(ifp);
2022 	/* NB: no point powering down chip as we're about to reboot */
2023 }
2024 
2025 /*
2026  * Interrupt handler.  Most of the actual processing is deferred.
2027  */
2028 void
2029 ath_intr(void *arg)
2030 {
2031 	struct ath_softc *sc = arg;
2032 	struct ifnet *ifp = sc->sc_ifp;
2033 	struct ath_hal *ah = sc->sc_ah;
2034 	HAL_INT status = 0;
2035 	uint32_t txqs;
2036 
2037 	/*
2038 	 * If we're inside a reset path, just print a warning and
2039 	 * clear the ISR. The reset routine will finish it for us.
2040 	 */
2041 	ATH_PCU_LOCK(sc);
2042 	if (sc->sc_inreset_cnt) {
2043 		HAL_INT status;
2044 		ath_hal_getisr(ah, &status);	/* clear ISR */
2045 		ath_hal_intrset(ah, 0);		/* disable further intr's */
2046 		DPRINTF(sc, ATH_DEBUG_ANY,
2047 		    "%s: in reset, ignoring: status=0x%x\n",
2048 		    __func__, status);
2049 		ATH_PCU_UNLOCK(sc);
2050 		return;
2051 	}
2052 
2053 	if (sc->sc_invalid) {
2054 		/*
2055 		 * The hardware is not ready/present, don't touch anything.
2056 		 * Note this can happen early on if the IRQ is shared.
2057 		 */
2058 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
2059 		ATH_PCU_UNLOCK(sc);
2060 		return;
2061 	}
2062 	if (!ath_hal_intrpend(ah)) {		/* shared irq, not for us */
2063 		ATH_PCU_UNLOCK(sc);
2064 		return;
2065 	}
2066 
2067 	ATH_LOCK(sc);
2068 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2069 	ATH_UNLOCK(sc);
2070 
2071 	if ((ifp->if_flags & IFF_UP) == 0 ||
2072 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2073 		HAL_INT status;
2074 
2075 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
2076 			__func__, ifp->if_flags);
2077 		ath_hal_getisr(ah, &status);	/* clear ISR */
2078 		ath_hal_intrset(ah, 0);		/* disable further intr's */
2079 		ATH_PCU_UNLOCK(sc);
2080 
2081 		ATH_LOCK(sc);
2082 		ath_power_restore_power_state(sc);
2083 		ATH_UNLOCK(sc);
2084 		return;
2085 	}
2086 
2087 	/*
2088 	 * Figure out the reason(s) for the interrupt.  Note
2089 	 * that the hal returns a pseudo-ISR that may include
2090 	 * bits we haven't explicitly enabled so we mask the
2091 	 * value to insure we only process bits we requested.
2092 	 */
2093 	ath_hal_getisr(ah, &status);		/* NB: clears ISR too */
2094 	DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
2095 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1, "ath_intr: mask=0x%.8x", status);
2096 #ifdef	ATH_DEBUG_ALQ
2097 	if_ath_alq_post_intr(&sc->sc_alq, status, ah->ah_intrstate,
2098 	    ah->ah_syncstate);
2099 #endif	/* ATH_DEBUG_ALQ */
2100 #ifdef	ATH_KTR_INTR_DEBUG
2101 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 5,
2102 	    "ath_intr: ISR=0x%.8x, ISR_S0=0x%.8x, ISR_S1=0x%.8x, ISR_S2=0x%.8x, ISR_S5=0x%.8x",
2103 	    ah->ah_intrstate[0],
2104 	    ah->ah_intrstate[1],
2105 	    ah->ah_intrstate[2],
2106 	    ah->ah_intrstate[3],
2107 	    ah->ah_intrstate[6]);
2108 #endif
2109 
2110 	/* Squirrel away SYNC interrupt debugging */
2111 	if (ah->ah_syncstate != 0) {
2112 		int i;
2113 		for (i = 0; i < 32; i++)
2114 			if (ah->ah_syncstate & (i << i))
2115 				sc->sc_intr_stats.sync_intr[i]++;
2116 	}
2117 
2118 	status &= sc->sc_imask;			/* discard unasked for bits */
2119 
2120 	/* Short-circuit un-handled interrupts */
2121 	if (status == 0x0) {
2122 		ATH_PCU_UNLOCK(sc);
2123 
2124 		ATH_LOCK(sc);
2125 		ath_power_restore_power_state(sc);
2126 		ATH_UNLOCK(sc);
2127 
2128 		return;
2129 	}
2130 
2131 	/*
2132 	 * Take a note that we're inside the interrupt handler, so
2133 	 * the reset routines know to wait.
2134 	 */
2135 	sc->sc_intr_cnt++;
2136 	ATH_PCU_UNLOCK(sc);
2137 
2138 	/*
2139 	 * Handle the interrupt. We won't run concurrent with the reset
2140 	 * or channel change routines as they'll wait for sc_intr_cnt
2141 	 * to be 0 before continuing.
2142 	 */
2143 	if (status & HAL_INT_FATAL) {
2144 		sc->sc_stats.ast_hardware++;
2145 		ath_hal_intrset(ah, 0);		/* disable intr's until reset */
2146 		taskqueue_enqueue(sc->sc_tq, &sc->sc_fataltask);
2147 	} else {
2148 		if (status & HAL_INT_SWBA) {
2149 			/*
2150 			 * Software beacon alert--time to send a beacon.
2151 			 * Handle beacon transmission directly; deferring
2152 			 * this is too slow to meet timing constraints
2153 			 * under load.
2154 			 */
2155 #ifdef IEEE80211_SUPPORT_TDMA
2156 			if (sc->sc_tdma) {
2157 				if (sc->sc_tdmaswba == 0) {
2158 					struct ieee80211com *ic = ifp->if_l2com;
2159 					struct ieee80211vap *vap =
2160 					    TAILQ_FIRST(&ic->ic_vaps);
2161 					ath_tdma_beacon_send(sc, vap);
2162 					sc->sc_tdmaswba =
2163 					    vap->iv_tdma->tdma_bintval;
2164 				} else
2165 					sc->sc_tdmaswba--;
2166 			} else
2167 #endif
2168 			{
2169 				ath_beacon_proc(sc, 0);
2170 #ifdef IEEE80211_SUPPORT_SUPERG
2171 				/*
2172 				 * Schedule the rx taskq in case there's no
2173 				 * traffic so any frames held on the staging
2174 				 * queue are aged and potentially flushed.
2175 				 */
2176 				sc->sc_rx.recv_sched(sc, 1);
2177 #endif
2178 			}
2179 		}
2180 		if (status & HAL_INT_RXEOL) {
2181 			int imask;
2182 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXEOL");
2183 			if (! sc->sc_isedma) {
2184 				ATH_PCU_LOCK(sc);
2185 				/*
2186 				 * NB: the hardware should re-read the link when
2187 				 *     RXE bit is written, but it doesn't work at
2188 				 *     least on older hardware revs.
2189 				 */
2190 				sc->sc_stats.ast_rxeol++;
2191 				/*
2192 				 * Disable RXEOL/RXORN - prevent an interrupt
2193 				 * storm until the PCU logic can be reset.
2194 				 * In case the interface is reset some other
2195 				 * way before "sc_kickpcu" is called, don't
2196 				 * modify sc_imask - that way if it is reset
2197 				 * by a call to ath_reset() somehow, the
2198 				 * interrupt mask will be correctly reprogrammed.
2199 				 */
2200 				imask = sc->sc_imask;
2201 				imask &= ~(HAL_INT_RXEOL | HAL_INT_RXORN);
2202 				ath_hal_intrset(ah, imask);
2203 				/*
2204 				 * Only blank sc_rxlink if we've not yet kicked
2205 				 * the PCU.
2206 				 *
2207 				 * This isn't entirely correct - the correct solution
2208 				 * would be to have a PCU lock and engage that for
2209 				 * the duration of the PCU fiddling; which would include
2210 				 * running the RX process. Otherwise we could end up
2211 				 * messing up the RX descriptor chain and making the
2212 				 * RX desc list much shorter.
2213 				 */
2214 				if (! sc->sc_kickpcu)
2215 					sc->sc_rxlink = NULL;
2216 				sc->sc_kickpcu = 1;
2217 				ATH_PCU_UNLOCK(sc);
2218 			}
2219 			/*
2220 			 * Enqueue an RX proc to handle whatever
2221 			 * is in the RX queue.
2222 			 * This will then kick the PCU if required.
2223 			 */
2224 			sc->sc_rx.recv_sched(sc, 1);
2225 		}
2226 		if (status & HAL_INT_TXURN) {
2227 			sc->sc_stats.ast_txurn++;
2228 			/* bump tx trigger level */
2229 			ath_hal_updatetxtriglevel(ah, AH_TRUE);
2230 		}
2231 		/*
2232 		 * Handle both the legacy and RX EDMA interrupt bits.
2233 		 * Note that HAL_INT_RXLP is also HAL_INT_RXDESC.
2234 		 */
2235 		if (status & (HAL_INT_RX | HAL_INT_RXHP | HAL_INT_RXLP)) {
2236 			sc->sc_stats.ast_rx_intr++;
2237 			sc->sc_rx.recv_sched(sc, 1);
2238 		}
2239 		if (status & HAL_INT_TX) {
2240 			sc->sc_stats.ast_tx_intr++;
2241 			/*
2242 			 * Grab all the currently set bits in the HAL txq bitmap
2243 			 * and blank them. This is the only place we should be
2244 			 * doing this.
2245 			 */
2246 			if (! sc->sc_isedma) {
2247 				ATH_PCU_LOCK(sc);
2248 				txqs = 0xffffffff;
2249 				ath_hal_gettxintrtxqs(sc->sc_ah, &txqs);
2250 				ATH_KTR(sc, ATH_KTR_INTERRUPTS, 3,
2251 				    "ath_intr: TX; txqs=0x%08x, txq_active was 0x%08x, now 0x%08x",
2252 				    txqs,
2253 				    sc->sc_txq_active,
2254 				    sc->sc_txq_active | txqs);
2255 				sc->sc_txq_active |= txqs;
2256 				ATH_PCU_UNLOCK(sc);
2257 			}
2258 			taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
2259 		}
2260 		if (status & HAL_INT_BMISS) {
2261 			sc->sc_stats.ast_bmiss++;
2262 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
2263 		}
2264 		if (status & HAL_INT_GTT)
2265 			sc->sc_stats.ast_tx_timeout++;
2266 		if (status & HAL_INT_CST)
2267 			sc->sc_stats.ast_tx_cst++;
2268 		if (status & HAL_INT_MIB) {
2269 			sc->sc_stats.ast_mib++;
2270 			ATH_PCU_LOCK(sc);
2271 			/*
2272 			 * Disable interrupts until we service the MIB
2273 			 * interrupt; otherwise it will continue to fire.
2274 			 */
2275 			ath_hal_intrset(ah, 0);
2276 			/*
2277 			 * Let the hal handle the event.  We assume it will
2278 			 * clear whatever condition caused the interrupt.
2279 			 */
2280 			ath_hal_mibevent(ah, &sc->sc_halstats);
2281 			/*
2282 			 * Don't reset the interrupt if we've just
2283 			 * kicked the PCU, or we may get a nested
2284 			 * RXEOL before the rxproc has had a chance
2285 			 * to run.
2286 			 */
2287 			if (sc->sc_kickpcu == 0)
2288 				ath_hal_intrset(ah, sc->sc_imask);
2289 			ATH_PCU_UNLOCK(sc);
2290 		}
2291 		if (status & HAL_INT_RXORN) {
2292 			/* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
2293 			ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_intr: RXORN");
2294 			sc->sc_stats.ast_rxorn++;
2295 		}
2296 		if (status & HAL_INT_TSFOOR) {
2297 			device_printf(sc->sc_dev, "%s: TSFOOR\n", __func__);
2298 			sc->sc_syncbeacon = 1;
2299 		}
2300 	}
2301 	ATH_PCU_LOCK(sc);
2302 	sc->sc_intr_cnt--;
2303 	ATH_PCU_UNLOCK(sc);
2304 
2305 	ATH_LOCK(sc);
2306 	ath_power_restore_power_state(sc);
2307 	ATH_UNLOCK(sc);
2308 }
2309 
2310 static void
2311 ath_fatal_proc(void *arg, int pending)
2312 {
2313 	struct ath_softc *sc = arg;
2314 	struct ifnet *ifp = sc->sc_ifp;
2315 	u_int32_t *state;
2316 	u_int32_t len;
2317 	void *sp;
2318 
2319 	if_printf(ifp, "hardware error; resetting\n");
2320 	/*
2321 	 * Fatal errors are unrecoverable.  Typically these
2322 	 * are caused by DMA errors.  Collect h/w state from
2323 	 * the hal so we can diagnose what's going on.
2324 	 */
2325 	if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
2326 		KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
2327 		state = sp;
2328 		if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
2329 		    state[0], state[1] , state[2], state[3],
2330 		    state[4], state[5]);
2331 	}
2332 	ath_reset(ifp, ATH_RESET_NOLOSS);
2333 }
2334 
2335 static void
2336 ath_bmiss_vap(struct ieee80211vap *vap)
2337 {
2338 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
2339 
2340 	/*
2341 	 * Workaround phantom bmiss interrupts by sanity-checking
2342 	 * the time of our last rx'd frame.  If it is within the
2343 	 * beacon miss interval then ignore the interrupt.  If it's
2344 	 * truly a bmiss we'll get another interrupt soon and that'll
2345 	 * be dispatched up for processing.  Note this applies only
2346 	 * for h/w beacon miss events.
2347 	 */
2348 
2349 	/*
2350 	 * XXX TODO: Just read the TSF during the interrupt path;
2351 	 * that way we don't have to wake up again just to read it
2352 	 * again.
2353 	 */
2354 	ATH_LOCK(sc);
2355 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2356 	ATH_UNLOCK(sc);
2357 
2358 	if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
2359 		struct ifnet *ifp = vap->iv_ic->ic_ifp;
2360 		struct ath_softc *sc = ifp->if_softc;
2361 		u_int64_t lastrx = sc->sc_lastrx;
2362 		u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
2363 		/* XXX should take a locked ref to iv_bss */
2364 		u_int bmisstimeout =
2365 			vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
2366 
2367 		DPRINTF(sc, ATH_DEBUG_BEACON,
2368 		    "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
2369 		    __func__, (unsigned long long) tsf,
2370 		    (unsigned long long)(tsf - lastrx),
2371 		    (unsigned long long) lastrx, bmisstimeout);
2372 
2373 		if (tsf - lastrx <= bmisstimeout) {
2374 			sc->sc_stats.ast_bmiss_phantom++;
2375 
2376 			ATH_LOCK(sc);
2377 			ath_power_restore_power_state(sc);
2378 			ATH_UNLOCK(sc);
2379 
2380 			return;
2381 		}
2382 	}
2383 
2384 	/*
2385 	 * There's no need to keep the hardware awake during the call
2386 	 * to av_bmiss().
2387 	 */
2388 	ATH_LOCK(sc);
2389 	ath_power_restore_power_state(sc);
2390 	ATH_UNLOCK(sc);
2391 
2392 	/*
2393 	 * Attempt to force a beacon resync.
2394 	 */
2395 	sc->sc_syncbeacon = 1;
2396 
2397 	ATH_VAP(vap)->av_bmiss(vap);
2398 }
2399 
2400 /* XXX this needs a force wakeup! */
2401 int
2402 ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
2403 {
2404 	uint32_t rsize;
2405 	void *sp;
2406 
2407 	if (!ath_hal_getdiagstate(ah, HAL_DIAG_CHECK_HANGS, &mask, sizeof(mask), &sp, &rsize))
2408 		return 0;
2409 	KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
2410 	*hangs = *(uint32_t *)sp;
2411 	return 1;
2412 }
2413 
2414 static void
2415 ath_bmiss_proc(void *arg, int pending)
2416 {
2417 	struct ath_softc *sc = arg;
2418 	struct ifnet *ifp = sc->sc_ifp;
2419 	uint32_t hangs;
2420 
2421 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
2422 
2423 	ATH_LOCK(sc);
2424 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2425 	ATH_UNLOCK(sc);
2426 
2427 	ath_beacon_miss(sc);
2428 
2429 	/*
2430 	 * Do a reset upon any becaon miss event.
2431 	 *
2432 	 * It may be a non-recognised RX clear hang which needs a reset
2433 	 * to clear.
2434 	 */
2435 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
2436 		ath_reset(ifp, ATH_RESET_NOLOSS);
2437 		if_printf(ifp, "bb hang detected (0x%x), resetting\n", hangs);
2438 	} else {
2439 		ath_reset(ifp, ATH_RESET_NOLOSS);
2440 		ieee80211_beacon_miss(ifp->if_l2com);
2441 	}
2442 
2443 	/* Force a beacon resync, in case they've drifted */
2444 	sc->sc_syncbeacon = 1;
2445 
2446 	ATH_LOCK(sc);
2447 	ath_power_restore_power_state(sc);
2448 	ATH_UNLOCK(sc);
2449 }
2450 
2451 /*
2452  * Handle TKIP MIC setup to deal hardware that doesn't do MIC
2453  * calcs together with WME.  If necessary disable the crypto
2454  * hardware and mark the 802.11 state so keys will be setup
2455  * with the MIC work done in software.
2456  */
2457 static void
2458 ath_settkipmic(struct ath_softc *sc)
2459 {
2460 	struct ifnet *ifp = sc->sc_ifp;
2461 	struct ieee80211com *ic = ifp->if_l2com;
2462 
2463 	if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
2464 		if (ic->ic_flags & IEEE80211_F_WME) {
2465 			ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
2466 			ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
2467 		} else {
2468 			ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
2469 			ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
2470 		}
2471 	}
2472 }
2473 
2474 static void
2475 ath_init(void *arg)
2476 {
2477 	struct ath_softc *sc = (struct ath_softc *) arg;
2478 	struct ifnet *ifp = sc->sc_ifp;
2479 	struct ieee80211com *ic = ifp->if_l2com;
2480 	struct ath_hal *ah = sc->sc_ah;
2481 	HAL_STATUS status;
2482 
2483 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
2484 		__func__, ifp->if_flags);
2485 
2486 	ATH_LOCK(sc);
2487 	/*
2488 	 * Force the sleep state awake.
2489 	 */
2490 	ath_power_setselfgen(sc, HAL_PM_AWAKE);
2491 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2492 	ath_power_setpower(sc, HAL_PM_AWAKE);
2493 
2494 	/*
2495 	 * Stop anything previously setup.  This is safe
2496 	 * whether this is the first time through or not.
2497 	 */
2498 	ath_stop_locked(ifp);
2499 
2500 	/*
2501 	 * The basic interface to setting the hardware in a good
2502 	 * state is ``reset''.  On return the hardware is known to
2503 	 * be powered up and with interrupts disabled.  This must
2504 	 * be followed by initialization of the appropriate bits
2505 	 * and then setup of the interrupt mask.
2506 	 */
2507 	ath_settkipmic(sc);
2508 	ath_update_chainmasks(sc, ic->ic_curchan);
2509 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
2510 	    sc->sc_cur_rxchainmask);
2511 
2512 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
2513 		if_printf(ifp, "unable to reset hardware; hal status %u\n",
2514 			status);
2515 		ATH_UNLOCK(sc);
2516 		return;
2517 	}
2518 
2519 	ATH_RX_LOCK(sc);
2520 	sc->sc_rx_stopped = 1;
2521 	sc->sc_rx_resetted = 1;
2522 	ATH_RX_UNLOCK(sc);
2523 
2524 	ath_chan_change(sc, ic->ic_curchan);
2525 
2526 	/* Let DFS at it in case it's a DFS channel */
2527 	ath_dfs_radar_enable(sc, ic->ic_curchan);
2528 
2529 	/* Let spectral at in case spectral is enabled */
2530 	ath_spectral_enable(sc, ic->ic_curchan);
2531 
2532 	/*
2533 	 * Let bluetooth coexistence at in case it's needed for this channel
2534 	 */
2535 	ath_btcoex_enable(sc, ic->ic_curchan);
2536 
2537 	/*
2538 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2539 	 * support it.
2540 	 */
2541 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2542 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2543 	else
2544 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2545 
2546 	/*
2547 	 * Likewise this is set during reset so update
2548 	 * state cached in the driver.
2549 	 */
2550 	sc->sc_diversity = ath_hal_getdiversity(ah);
2551 	sc->sc_lastlongcal = ticks;
2552 	sc->sc_resetcal = 1;
2553 	sc->sc_lastcalreset = 0;
2554 	sc->sc_lastani = ticks;
2555 	sc->sc_lastshortcal = ticks;
2556 	sc->sc_doresetcal = AH_FALSE;
2557 	/*
2558 	 * Beacon timers were cleared here; give ath_newstate()
2559 	 * a hint that the beacon timers should be poked when
2560 	 * things transition to the RUN state.
2561 	 */
2562 	sc->sc_beacons = 0;
2563 
2564 	/*
2565 	 * Setup the hardware after reset: the key cache
2566 	 * is filled as needed and the receive engine is
2567 	 * set going.  Frame transmit is handled entirely
2568 	 * in the frame output path; there's nothing to do
2569 	 * here except setup the interrupt mask.
2570 	 */
2571 	if (ath_startrecv(sc) != 0) {
2572 		if_printf(ifp, "unable to start recv logic\n");
2573 		ath_power_restore_power_state(sc);
2574 		ATH_UNLOCK(sc);
2575 		return;
2576 	}
2577 
2578 	/*
2579 	 * Enable interrupts.
2580 	 */
2581 	sc->sc_imask = HAL_INT_RX | HAL_INT_TX
2582 		  | HAL_INT_RXORN | HAL_INT_TXURN
2583 		  | HAL_INT_FATAL | HAL_INT_GLOBAL;
2584 
2585 	/*
2586 	 * Enable RX EDMA bits.  Note these overlap with
2587 	 * HAL_INT_RX and HAL_INT_RXDESC respectively.
2588 	 */
2589 	if (sc->sc_isedma)
2590 		sc->sc_imask |= (HAL_INT_RXHP | HAL_INT_RXLP);
2591 
2592 	/*
2593 	 * If we're an EDMA NIC, we don't care about RXEOL.
2594 	 * Writing a new descriptor in will simply restart
2595 	 * RX DMA.
2596 	 */
2597 	if (! sc->sc_isedma)
2598 		sc->sc_imask |= HAL_INT_RXEOL;
2599 
2600 	/*
2601 	 * Enable MIB interrupts when there are hardware phy counters.
2602 	 * Note we only do this (at the moment) for station mode.
2603 	 */
2604 	if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
2605 		sc->sc_imask |= HAL_INT_MIB;
2606 
2607 	/*
2608 	 * XXX add capability for this.
2609 	 *
2610 	 * If we're in STA mode (and maybe IBSS?) then register for
2611 	 * TSFOOR interrupts.
2612 	 */
2613 	if (ic->ic_opmode == IEEE80211_M_STA)
2614 		sc->sc_imask |= HAL_INT_TSFOOR;
2615 
2616 	/* Enable global TX timeout and carrier sense timeout if available */
2617 	if (ath_hal_gtxto_supported(ah))
2618 		sc->sc_imask |= HAL_INT_GTT;
2619 
2620 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: imask=0x%x\n",
2621 		__func__, sc->sc_imask);
2622 
2623 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2624 	callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
2625 	ath_hal_intrset(ah, sc->sc_imask);
2626 
2627 	ath_power_restore_power_state(sc);
2628 	ATH_UNLOCK(sc);
2629 
2630 #ifdef ATH_TX99_DIAG
2631 	if (sc->sc_tx99 != NULL)
2632 		sc->sc_tx99->start(sc->sc_tx99);
2633 	else
2634 #endif
2635 	ieee80211_start_all(ic);		/* start all vap's */
2636 }
2637 
2638 static void
2639 ath_stop_locked(struct ifnet *ifp)
2640 {
2641 	struct ath_softc *sc = ifp->if_softc;
2642 	struct ath_hal *ah = sc->sc_ah;
2643 
2644 	DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
2645 		__func__, sc->sc_invalid, ifp->if_flags);
2646 
2647 	ATH_LOCK_ASSERT(sc);
2648 
2649 	/*
2650 	 * Wake the hardware up before fiddling with it.
2651 	 */
2652 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2653 
2654 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2655 		/*
2656 		 * Shutdown the hardware and driver:
2657 		 *    reset 802.11 state machine
2658 		 *    turn off timers
2659 		 *    disable interrupts
2660 		 *    turn off the radio
2661 		 *    clear transmit machinery
2662 		 *    clear receive machinery
2663 		 *    drain and release tx queues
2664 		 *    reclaim beacon resources
2665 		 *    power down hardware
2666 		 *
2667 		 * Note that some of this work is not possible if the
2668 		 * hardware is gone (invalid).
2669 		 */
2670 #ifdef ATH_TX99_DIAG
2671 		if (sc->sc_tx99 != NULL)
2672 			sc->sc_tx99->stop(sc->sc_tx99);
2673 #endif
2674 		callout_stop(&sc->sc_wd_ch);
2675 		sc->sc_wd_timer = 0;
2676 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2677 		if (!sc->sc_invalid) {
2678 			if (sc->sc_softled) {
2679 				callout_stop(&sc->sc_ledtimer);
2680 				ath_hal_gpioset(ah, sc->sc_ledpin,
2681 					!sc->sc_ledon);
2682 				sc->sc_blinking = 0;
2683 			}
2684 			ath_hal_intrset(ah, 0);
2685 		}
2686 		/* XXX we should stop RX regardless of whether it's valid */
2687 		if (!sc->sc_invalid) {
2688 			ath_stoprecv(sc, 1);
2689 			ath_hal_phydisable(ah);
2690 		} else
2691 			sc->sc_rxlink = NULL;
2692 		ath_draintxq(sc, ATH_RESET_DEFAULT);
2693 		ath_beacon_free(sc);	/* XXX not needed */
2694 	}
2695 
2696 	/* And now, restore the current power state */
2697 	ath_power_restore_power_state(sc);
2698 }
2699 
2700 /*
2701  * Wait until all pending TX/RX has completed.
2702  *
2703  * This waits until all existing transmit, receive and interrupts
2704  * have completed.  It's assumed that the caller has first
2705  * grabbed the reset lock so it doesn't try to do overlapping
2706  * chip resets.
2707  */
2708 #define	MAX_TXRX_ITERATIONS	100
2709 static void
2710 ath_txrx_stop_locked(struct ath_softc *sc)
2711 {
2712 	int i = MAX_TXRX_ITERATIONS;
2713 
2714 	ATH_UNLOCK_ASSERT(sc);
2715 	ATH_PCU_LOCK_ASSERT(sc);
2716 
2717 	/*
2718 	 * Sleep until all the pending operations have completed.
2719 	 *
2720 	 * The caller must ensure that reset has been incremented
2721 	 * or the pending operations may continue being queued.
2722 	 */
2723 	while (sc->sc_rxproc_cnt || sc->sc_txproc_cnt ||
2724 	    sc->sc_txstart_cnt || sc->sc_intr_cnt) {
2725 		if (i <= 0)
2726 			break;
2727 		msleep(sc, &sc->sc_pcu_mtx, 0, "ath_txrx_stop",
2728 		    msecs_to_ticks(10));
2729 		i--;
2730 	}
2731 
2732 	if (i <= 0)
2733 		device_printf(sc->sc_dev,
2734 		    "%s: didn't finish after %d iterations\n",
2735 		    __func__, MAX_TXRX_ITERATIONS);
2736 }
2737 #undef	MAX_TXRX_ITERATIONS
2738 
2739 #if 0
2740 static void
2741 ath_txrx_stop(struct ath_softc *sc)
2742 {
2743 	ATH_UNLOCK_ASSERT(sc);
2744 	ATH_PCU_UNLOCK_ASSERT(sc);
2745 
2746 	ATH_PCU_LOCK(sc);
2747 	ath_txrx_stop_locked(sc);
2748 	ATH_PCU_UNLOCK(sc);
2749 }
2750 #endif
2751 
2752 static void
2753 ath_txrx_start(struct ath_softc *sc)
2754 {
2755 
2756 	taskqueue_unblock(sc->sc_tq);
2757 }
2758 
2759 /*
2760  * Grab the reset lock, and wait around until noone else
2761  * is trying to do anything with it.
2762  *
2763  * This is totally horrible but we can't hold this lock for
2764  * long enough to do TX/RX or we end up with net80211/ip stack
2765  * LORs and eventual deadlock.
2766  *
2767  * "dowait" signals whether to spin, waiting for the reset
2768  * lock count to reach 0. This should (for now) only be used
2769  * during the reset path, as the rest of the code may not
2770  * be locking-reentrant enough to behave correctly.
2771  *
2772  * Another, cleaner way should be found to serialise all of
2773  * these operations.
2774  */
2775 #define	MAX_RESET_ITERATIONS	25
2776 static int
2777 ath_reset_grablock(struct ath_softc *sc, int dowait)
2778 {
2779 	int w = 0;
2780 	int i = MAX_RESET_ITERATIONS;
2781 
2782 	ATH_PCU_LOCK_ASSERT(sc);
2783 	do {
2784 		if (sc->sc_inreset_cnt == 0) {
2785 			w = 1;
2786 			break;
2787 		}
2788 		if (dowait == 0) {
2789 			w = 0;
2790 			break;
2791 		}
2792 		ATH_PCU_UNLOCK(sc);
2793 		/*
2794 		 * 1 tick is likely not enough time for long calibrations
2795 		 * to complete.  So we should wait quite a while.
2796 		 */
2797 		pause("ath_reset_grablock", msecs_to_ticks(100));
2798 		i--;
2799 		ATH_PCU_LOCK(sc);
2800 	} while (i > 0);
2801 
2802 	/*
2803 	 * We always increment the refcounter, regardless
2804 	 * of whether we succeeded to get it in an exclusive
2805 	 * way.
2806 	 */
2807 	sc->sc_inreset_cnt++;
2808 
2809 	if (i <= 0)
2810 		device_printf(sc->sc_dev,
2811 		    "%s: didn't finish after %d iterations\n",
2812 		    __func__, MAX_RESET_ITERATIONS);
2813 
2814 	if (w == 0)
2815 		device_printf(sc->sc_dev,
2816 		    "%s: warning, recursive reset path!\n",
2817 		    __func__);
2818 
2819 	return w;
2820 }
2821 #undef MAX_RESET_ITERATIONS
2822 
2823 /*
2824  * XXX TODO: write ath_reset_releaselock
2825  */
2826 
2827 static void
2828 ath_stop(struct ifnet *ifp)
2829 {
2830 	struct ath_softc *sc = ifp->if_softc;
2831 
2832 	ATH_LOCK(sc);
2833 	ath_stop_locked(ifp);
2834 	ATH_UNLOCK(sc);
2835 }
2836 
2837 /*
2838  * Reset the hardware w/o losing operational state.  This is
2839  * basically a more efficient way of doing ath_stop, ath_init,
2840  * followed by state transitions to the current 802.11
2841  * operational state.  Used to recover from various errors and
2842  * to reset or reload hardware state.
2843  */
2844 int
2845 ath_reset(struct ifnet *ifp, ATH_RESET_TYPE reset_type)
2846 {
2847 	struct ath_softc *sc = ifp->if_softc;
2848 	struct ieee80211com *ic = ifp->if_l2com;
2849 	struct ath_hal *ah = sc->sc_ah;
2850 	HAL_STATUS status;
2851 	int i;
2852 
2853 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: called\n", __func__);
2854 
2855 	/* Ensure ATH_LOCK isn't held; ath_rx_proc can't be locked */
2856 	ATH_PCU_UNLOCK_ASSERT(sc);
2857 	ATH_UNLOCK_ASSERT(sc);
2858 
2859 	/* Try to (stop any further TX/RX from occuring */
2860 	taskqueue_block(sc->sc_tq);
2861 
2862 	/*
2863 	 * Wake the hardware up.
2864 	 */
2865 	ATH_LOCK(sc);
2866 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
2867 	ATH_UNLOCK(sc);
2868 
2869 	ATH_PCU_LOCK(sc);
2870 
2871 	/*
2872 	 * Grab the reset lock before TX/RX is stopped.
2873 	 *
2874 	 * This is needed to ensure that when the TX/RX actually does finish,
2875 	 * no further TX/RX/reset runs in parallel with this.
2876 	 */
2877 	if (ath_reset_grablock(sc, 1) == 0) {
2878 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
2879 		    __func__);
2880 	}
2881 
2882 	/* disable interrupts */
2883 	ath_hal_intrset(ah, 0);
2884 
2885 	/*
2886 	 * Now, ensure that any in progress TX/RX completes before we
2887 	 * continue.
2888 	 */
2889 	ath_txrx_stop_locked(sc);
2890 
2891 	ATH_PCU_UNLOCK(sc);
2892 
2893 	/*
2894 	 * Regardless of whether we're doing a no-loss flush or
2895 	 * not, stop the PCU and handle what's in the RX queue.
2896 	 * That way frames aren't dropped which shouldn't be.
2897 	 */
2898 	ath_stoprecv(sc, (reset_type != ATH_RESET_NOLOSS));
2899 	ath_rx_flush(sc);
2900 
2901 	/*
2902 	 * Should now wait for pending TX/RX to complete
2903 	 * and block future ones from occuring. This needs to be
2904 	 * done before the TX queue is drained.
2905 	 */
2906 	ath_draintxq(sc, reset_type);	/* stop xmit side */
2907 
2908 	ath_settkipmic(sc);		/* configure TKIP MIC handling */
2909 	/* NB: indicate channel change so we do a full reset */
2910 	ath_update_chainmasks(sc, ic->ic_curchan);
2911 	ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
2912 	    sc->sc_cur_rxchainmask);
2913 	if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
2914 		if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
2915 			__func__, status);
2916 	sc->sc_diversity = ath_hal_getdiversity(ah);
2917 
2918 	ATH_RX_LOCK(sc);
2919 	sc->sc_rx_stopped = 1;
2920 	sc->sc_rx_resetted = 1;
2921 	ATH_RX_UNLOCK(sc);
2922 
2923 	/* Let DFS at it in case it's a DFS channel */
2924 	ath_dfs_radar_enable(sc, ic->ic_curchan);
2925 
2926 	/* Let spectral at in case spectral is enabled */
2927 	ath_spectral_enable(sc, ic->ic_curchan);
2928 
2929 	/*
2930 	 * Let bluetooth coexistence at in case it's needed for this channel
2931 	 */
2932 	ath_btcoex_enable(sc, ic->ic_curchan);
2933 
2934 	/*
2935 	 * If we're doing TDMA, enforce the TXOP limitation for chips that
2936 	 * support it.
2937 	 */
2938 	if (sc->sc_hasenforcetxop && sc->sc_tdma)
2939 		ath_hal_setenforcetxop(sc->sc_ah, 1);
2940 	else
2941 		ath_hal_setenforcetxop(sc->sc_ah, 0);
2942 
2943 	if (ath_startrecv(sc) != 0)	/* restart recv */
2944 		if_printf(ifp, "%s: unable to start recv logic\n", __func__);
2945 	/*
2946 	 * We may be doing a reset in response to an ioctl
2947 	 * that changes the channel so update any state that
2948 	 * might change as a result.
2949 	 */
2950 	ath_chan_change(sc, ic->ic_curchan);
2951 	if (sc->sc_beacons) {		/* restart beacons */
2952 #ifdef IEEE80211_SUPPORT_TDMA
2953 		if (sc->sc_tdma)
2954 			ath_tdma_config(sc, NULL);
2955 		else
2956 #endif
2957 			ath_beacon_config(sc, NULL);
2958 	}
2959 
2960 	/*
2961 	 * Release the reset lock and re-enable interrupts here.
2962 	 * If an interrupt was being processed in ath_intr(),
2963 	 * it would disable interrupts at this point. So we have
2964 	 * to atomically enable interrupts and decrement the
2965 	 * reset counter - this way ath_intr() doesn't end up
2966 	 * disabling interrupts without a corresponding enable
2967 	 * in the rest or channel change path.
2968 	 *
2969 	 * Grab the TX reference in case we need to transmit.
2970 	 * That way a parallel transmit doesn't.
2971 	 */
2972 	ATH_PCU_LOCK(sc);
2973 	sc->sc_inreset_cnt--;
2974 	sc->sc_txstart_cnt++;
2975 	/* XXX only do this if sc_inreset_cnt == 0? */
2976 	ath_hal_intrset(ah, sc->sc_imask);
2977 	ATH_PCU_UNLOCK(sc);
2978 
2979 	/*
2980 	 * TX and RX can be started here. If it were started with
2981 	 * sc_inreset_cnt > 0, the TX and RX path would abort.
2982 	 * Thus if this is a nested call through the reset or
2983 	 * channel change code, TX completion will occur but
2984 	 * RX completion and ath_start / ath_tx_start will not
2985 	 * run.
2986 	 */
2987 
2988 	/* Restart TX/RX as needed */
2989 	ath_txrx_start(sc);
2990 
2991 	/* XXX TODO: we need to hold the tx refcount here! */
2992 
2993 	/* Restart TX completion and pending TX */
2994 	if (reset_type == ATH_RESET_NOLOSS) {
2995 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
2996 			if (ATH_TXQ_SETUP(sc, i)) {
2997 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
2998 				ath_txq_restart_dma(sc, &sc->sc_txq[i]);
2999 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
3000 
3001 				ATH_TX_LOCK(sc);
3002 				ath_txq_sched(sc, &sc->sc_txq[i]);
3003 				ATH_TX_UNLOCK(sc);
3004 			}
3005 		}
3006 	}
3007 
3008 	/*
3009 	 * This may have been set during an ath_start() call which
3010 	 * set this once it detected a concurrent TX was going on.
3011 	 * So, clear it.
3012 	 */
3013 	IF_LOCK(&ifp->if_snd);
3014 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3015 	IF_UNLOCK(&ifp->if_snd);
3016 
3017 	ATH_LOCK(sc);
3018 	ath_power_restore_power_state(sc);
3019 	ATH_UNLOCK(sc);
3020 
3021 	ATH_PCU_LOCK(sc);
3022 	sc->sc_txstart_cnt--;
3023 	ATH_PCU_UNLOCK(sc);
3024 
3025 	/* Handle any frames in the TX queue */
3026 	/*
3027 	 * XXX should this be done by the caller, rather than
3028 	 * ath_reset() ?
3029 	 */
3030 	ath_tx_kick(sc);		/* restart xmit */
3031 	return 0;
3032 }
3033 
3034 static int
3035 ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
3036 {
3037 	struct ieee80211com *ic = vap->iv_ic;
3038 	struct ifnet *ifp = ic->ic_ifp;
3039 	struct ath_softc *sc = ifp->if_softc;
3040 	struct ath_hal *ah = sc->sc_ah;
3041 
3042 	switch (cmd) {
3043 	case IEEE80211_IOC_TXPOWER:
3044 		/*
3045 		 * If per-packet TPC is enabled, then we have nothing
3046 		 * to do; otherwise we need to force the global limit.
3047 		 * All this can happen directly; no need to reset.
3048 		 */
3049 		if (!ath_hal_gettpc(ah))
3050 			ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
3051 		return 0;
3052 	}
3053 	/* XXX? Full or NOLOSS? */
3054 	return ath_reset(ifp, ATH_RESET_FULL);
3055 }
3056 
3057 struct ath_buf *
3058 _ath_getbuf_locked(struct ath_softc *sc, ath_buf_type_t btype)
3059 {
3060 	struct ath_buf *bf;
3061 
3062 	ATH_TXBUF_LOCK_ASSERT(sc);
3063 
3064 	if (btype == ATH_BUFTYPE_MGMT)
3065 		bf = TAILQ_FIRST(&sc->sc_txbuf_mgmt);
3066 	else
3067 		bf = TAILQ_FIRST(&sc->sc_txbuf);
3068 
3069 	if (bf == NULL) {
3070 		sc->sc_stats.ast_tx_getnobuf++;
3071 	} else {
3072 		if (bf->bf_flags & ATH_BUF_BUSY) {
3073 			sc->sc_stats.ast_tx_getbusybuf++;
3074 			bf = NULL;
3075 		}
3076 	}
3077 
3078 	if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0) {
3079 		if (btype == ATH_BUFTYPE_MGMT)
3080 			TAILQ_REMOVE(&sc->sc_txbuf_mgmt, bf, bf_list);
3081 		else {
3082 			TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
3083 			sc->sc_txbuf_cnt--;
3084 
3085 			/*
3086 			 * This shuldn't happen; however just to be
3087 			 * safe print a warning and fudge the txbuf
3088 			 * count.
3089 			 */
3090 			if (sc->sc_txbuf_cnt < 0) {
3091 				device_printf(sc->sc_dev,
3092 				    "%s: sc_txbuf_cnt < 0?\n",
3093 				    __func__);
3094 				sc->sc_txbuf_cnt = 0;
3095 			}
3096 		}
3097 	} else
3098 		bf = NULL;
3099 
3100 	if (bf == NULL) {
3101 		/* XXX should check which list, mgmt or otherwise */
3102 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
3103 		    TAILQ_FIRST(&sc->sc_txbuf) == NULL ?
3104 			"out of xmit buffers" : "xmit buffer busy");
3105 		return NULL;
3106 	}
3107 
3108 	/* XXX TODO: should do this at buffer list initialisation */
3109 	/* XXX (then, ensure the buffer has the right flag set) */
3110 	bf->bf_flags = 0;
3111 	if (btype == ATH_BUFTYPE_MGMT)
3112 		bf->bf_flags |= ATH_BUF_MGMT;
3113 	else
3114 		bf->bf_flags &= (~ATH_BUF_MGMT);
3115 
3116 	/* Valid bf here; clear some basic fields */
3117 	bf->bf_next = NULL;	/* XXX just to be sure */
3118 	bf->bf_last = NULL;	/* XXX again, just to be sure */
3119 	bf->bf_comp = NULL;	/* XXX again, just to be sure */
3120 	bzero(&bf->bf_state, sizeof(bf->bf_state));
3121 
3122 	/*
3123 	 * Track the descriptor ID only if doing EDMA
3124 	 */
3125 	if (sc->sc_isedma) {
3126 		bf->bf_descid = sc->sc_txbuf_descid;
3127 		sc->sc_txbuf_descid++;
3128 	}
3129 
3130 	return bf;
3131 }
3132 
3133 /*
3134  * When retrying a software frame, buffers marked ATH_BUF_BUSY
3135  * can't be thrown back on the queue as they could still be
3136  * in use by the hardware.
3137  *
3138  * This duplicates the buffer, or returns NULL.
3139  *
3140  * The descriptor is also copied but the link pointers and
3141  * the DMA segments aren't copied; this frame should thus
3142  * be again passed through the descriptor setup/chain routines
3143  * so the link is correct.
3144  *
3145  * The caller must free the buffer using ath_freebuf().
3146  */
3147 struct ath_buf *
3148 ath_buf_clone(struct ath_softc *sc, struct ath_buf *bf)
3149 {
3150 	struct ath_buf *tbf;
3151 
3152 	tbf = ath_getbuf(sc,
3153 	    (bf->bf_flags & ATH_BUF_MGMT) ?
3154 	     ATH_BUFTYPE_MGMT : ATH_BUFTYPE_NORMAL);
3155 	if (tbf == NULL)
3156 		return NULL;	/* XXX failure? Why? */
3157 
3158 	/* Copy basics */
3159 	tbf->bf_next = NULL;
3160 	tbf->bf_nseg = bf->bf_nseg;
3161 	tbf->bf_flags = bf->bf_flags & ATH_BUF_FLAGS_CLONE;
3162 	tbf->bf_status = bf->bf_status;
3163 	tbf->bf_m = bf->bf_m;
3164 	tbf->bf_node = bf->bf_node;
3165 	KASSERT((bf->bf_node != NULL), ("%s: bf_node=NULL!", __func__));
3166 	/* will be setup by the chain/setup function */
3167 	tbf->bf_lastds = NULL;
3168 	/* for now, last == self */
3169 	tbf->bf_last = tbf;
3170 	tbf->bf_comp = bf->bf_comp;
3171 
3172 	/* NOTE: DMA segments will be setup by the setup/chain functions */
3173 
3174 	/* The caller has to re-init the descriptor + links */
3175 
3176 	/*
3177 	 * Free the DMA mapping here, before we NULL the mbuf.
3178 	 * We must only call bus_dmamap_unload() once per mbuf chain
3179 	 * or behaviour is undefined.
3180 	 */
3181 	if (bf->bf_m != NULL) {
3182 		/*
3183 		 * XXX is this POSTWRITE call required?
3184 		 */
3185 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
3186 		    BUS_DMASYNC_POSTWRITE);
3187 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3188 	}
3189 
3190 	bf->bf_m = NULL;
3191 	bf->bf_node = NULL;
3192 
3193 	/* Copy state */
3194 	memcpy(&tbf->bf_state, &bf->bf_state, sizeof(bf->bf_state));
3195 
3196 	return tbf;
3197 }
3198 
3199 struct ath_buf *
3200 ath_getbuf(struct ath_softc *sc, ath_buf_type_t btype)
3201 {
3202 	struct ath_buf *bf;
3203 
3204 	ATH_TXBUF_LOCK(sc);
3205 	bf = _ath_getbuf_locked(sc, btype);
3206 	/*
3207 	 * If a mgmt buffer was requested but we're out of those,
3208 	 * try requesting a normal one.
3209 	 */
3210 	if (bf == NULL && btype == ATH_BUFTYPE_MGMT)
3211 		bf = _ath_getbuf_locked(sc, ATH_BUFTYPE_NORMAL);
3212 	ATH_TXBUF_UNLOCK(sc);
3213 	if (bf == NULL) {
3214 		struct ifnet *ifp = sc->sc_ifp;
3215 
3216 		DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
3217 		sc->sc_stats.ast_tx_qstop++;
3218 		IF_LOCK(&ifp->if_snd);
3219 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3220 		IF_UNLOCK(&ifp->if_snd);
3221 	}
3222 	return bf;
3223 }
3224 
3225 static void
3226 ath_qflush(struct ifnet *ifp)
3227 {
3228 
3229 	/* XXX TODO */
3230 }
3231 
3232 /*
3233  * Transmit a single frame.
3234  *
3235  * net80211 will free the node reference if the transmit
3236  * fails, so don't free the node reference here.
3237  */
3238 static int
3239 ath_transmit(struct ifnet *ifp, struct mbuf *m)
3240 {
3241 	struct ieee80211com *ic = ifp->if_l2com;
3242 	struct ath_softc *sc = ic->ic_ifp->if_softc;
3243 	struct ieee80211_node *ni;
3244 	struct mbuf *next;
3245 	struct ath_buf *bf;
3246 	ath_bufhead frags;
3247 	int retval = 0;
3248 
3249 	/*
3250 	 * Tell the reset path that we're currently transmitting.
3251 	 */
3252 	ATH_PCU_LOCK(sc);
3253 	if (sc->sc_inreset_cnt > 0) {
3254 		DPRINTF(sc, ATH_DEBUG_XMIT,
3255 		    "%s: sc_inreset_cnt > 0; bailing\n", __func__);
3256 		ATH_PCU_UNLOCK(sc);
3257 		IF_LOCK(&ifp->if_snd);
3258 		sc->sc_stats.ast_tx_qstop++;
3259 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3260 		IF_UNLOCK(&ifp->if_snd);
3261 		ATH_KTR(sc, ATH_KTR_TX, 0, "ath_start_task: OACTIVE, finish");
3262 		return (ENOBUFS);	/* XXX should be EINVAL or? */
3263 	}
3264 	sc->sc_txstart_cnt++;
3265 	ATH_PCU_UNLOCK(sc);
3266 
3267 	/* Wake the hardware up already */
3268 	ATH_LOCK(sc);
3269 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3270 	ATH_UNLOCK(sc);
3271 
3272 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: start");
3273 	/*
3274 	 * Grab the TX lock - it's ok to do this here; we haven't
3275 	 * yet started transmitting.
3276 	 */
3277 	ATH_TX_LOCK(sc);
3278 
3279 	/*
3280 	 * Node reference, if there's one.
3281 	 */
3282 	ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
3283 
3284 	/*
3285 	 * Enforce how deep a node queue can get.
3286 	 *
3287 	 * XXX it would be nicer if we kept an mbuf queue per
3288 	 * node and only whacked them into ath_bufs when we
3289 	 * are ready to schedule some traffic from them.
3290 	 * .. that may come later.
3291 	 *
3292 	 * XXX we should also track the per-node hardware queue
3293 	 * depth so it is easy to limit the _SUM_ of the swq and
3294 	 * hwq frames.  Since we only schedule two HWQ frames
3295 	 * at a time, this should be OK for now.
3296 	 */
3297 	if ((!(m->m_flags & M_EAPOL)) &&
3298 	    (ATH_NODE(ni)->an_swq_depth > sc->sc_txq_node_maxdepth)) {
3299 		sc->sc_stats.ast_tx_nodeq_overflow++;
3300 		m_freem(m);
3301 		m = NULL;
3302 		retval = ENOBUFS;
3303 		goto finish;
3304 	}
3305 
3306 	/*
3307 	 * Check how many TX buffers are available.
3308 	 *
3309 	 * If this is for non-EAPOL traffic, just leave some
3310 	 * space free in order for buffer cloning and raw
3311 	 * frame transmission to occur.
3312 	 *
3313 	 * If it's for EAPOL traffic, ignore this for now.
3314 	 * Management traffic will be sent via the raw transmit
3315 	 * method which bypasses this check.
3316 	 *
3317 	 * This is needed to ensure that EAPOL frames during
3318 	 * (re) keying have a chance to go out.
3319 	 *
3320 	 * See kern/138379 for more information.
3321 	 */
3322 	if ((!(m->m_flags & M_EAPOL)) &&
3323 	    (sc->sc_txbuf_cnt <= sc->sc_txq_data_minfree)) {
3324 		sc->sc_stats.ast_tx_nobuf++;
3325 		m_freem(m);
3326 		m = NULL;
3327 		retval = ENOBUFS;
3328 		goto finish;
3329 	}
3330 
3331 	/*
3332 	 * Grab a TX buffer and associated resources.
3333 	 *
3334 	 * If it's an EAPOL frame, allocate a MGMT ath_buf.
3335 	 * That way even with temporary buffer exhaustion due to
3336 	 * the data path doesn't leave us without the ability
3337 	 * to transmit management frames.
3338 	 *
3339 	 * Otherwise allocate a normal buffer.
3340 	 */
3341 	if (m->m_flags & M_EAPOL)
3342 		bf = ath_getbuf(sc, ATH_BUFTYPE_MGMT);
3343 	else
3344 		bf = ath_getbuf(sc, ATH_BUFTYPE_NORMAL);
3345 
3346 	if (bf == NULL) {
3347 		/*
3348 		 * If we failed to allocate a buffer, fail.
3349 		 *
3350 		 * We shouldn't fail normally, due to the check
3351 		 * above.
3352 		 */
3353 		sc->sc_stats.ast_tx_nobuf++;
3354 		IF_LOCK(&ifp->if_snd);
3355 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3356 		IF_UNLOCK(&ifp->if_snd);
3357 		m_freem(m);
3358 		m = NULL;
3359 		retval = ENOBUFS;
3360 		goto finish;
3361 	}
3362 
3363 	/*
3364 	 * At this point we have a buffer; so we need to free it
3365 	 * if we hit any error conditions.
3366 	 */
3367 
3368 	/*
3369 	 * Check for fragmentation.  If this frame
3370 	 * has been broken up verify we have enough
3371 	 * buffers to send all the fragments so all
3372 	 * go out or none...
3373 	 */
3374 	TAILQ_INIT(&frags);
3375 	if ((m->m_flags & M_FRAG) &&
3376 	    !ath_txfrag_setup(sc, &frags, m, ni)) {
3377 		DPRINTF(sc, ATH_DEBUG_XMIT,
3378 		    "%s: out of txfrag buffers\n", __func__);
3379 		sc->sc_stats.ast_tx_nofrag++;
3380 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3381 		ath_freetx(m);
3382 		goto bad;
3383 	}
3384 
3385 	/*
3386 	 * At this point if we have any TX fragments, then we will
3387 	 * have bumped the node reference once for each of those.
3388 	 */
3389 
3390 	/*
3391 	 * XXX Is there anything actually _enforcing_ that the
3392 	 * fragments are being transmitted in one hit, rather than
3393 	 * being interleaved with other transmissions on that
3394 	 * hardware queue?
3395 	 *
3396 	 * The ATH TX output lock is the only thing serialising this
3397 	 * right now.
3398 	 */
3399 
3400 	/*
3401 	 * Calculate the "next fragment" length field in ath_buf
3402 	 * in order to let the transmit path know enough about
3403 	 * what to next write to the hardware.
3404 	 */
3405 	if (m->m_flags & M_FRAG) {
3406 		struct ath_buf *fbf = bf;
3407 		struct ath_buf *n_fbf = NULL;
3408 		struct mbuf *fm = m->m_nextpkt;
3409 
3410 		/*
3411 		 * We need to walk the list of fragments and set
3412 		 * the next size to the following buffer.
3413 		 * However, the first buffer isn't in the frag
3414 		 * list, so we have to do some gymnastics here.
3415 		 */
3416 		TAILQ_FOREACH(n_fbf, &frags, bf_list) {
3417 			fbf->bf_nextfraglen = fm->m_pkthdr.len;
3418 			fbf = n_fbf;
3419 			fm = fm->m_nextpkt;
3420 		}
3421 	}
3422 
3423 	/*
3424 	 * Bump the ifp output counter.
3425 	 *
3426 	 * XXX should use atomics?
3427 	 */
3428 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3429 nextfrag:
3430 	/*
3431 	 * Pass the frame to the h/w for transmission.
3432 	 * Fragmented frames have each frag chained together
3433 	 * with m_nextpkt.  We know there are sufficient ath_buf's
3434 	 * to send all the frags because of work done by
3435 	 * ath_txfrag_setup.  We leave m_nextpkt set while
3436 	 * calling ath_tx_start so it can use it to extend the
3437 	 * the tx duration to cover the subsequent frag and
3438 	 * so it can reclaim all the mbufs in case of an error;
3439 	 * ath_tx_start clears m_nextpkt once it commits to
3440 	 * handing the frame to the hardware.
3441 	 *
3442 	 * Note: if this fails, then the mbufs are freed but
3443 	 * not the node reference.
3444 	 */
3445 	next = m->m_nextpkt;
3446 	if (ath_tx_start(sc, ni, bf, m)) {
3447 bad:
3448 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3449 reclaim:
3450 		bf->bf_m = NULL;
3451 		bf->bf_node = NULL;
3452 		ATH_TXBUF_LOCK(sc);
3453 		ath_returnbuf_head(sc, bf);
3454 		/*
3455 		 * Free the rest of the node references and
3456 		 * buffers for the fragment list.
3457 		 */
3458 		ath_txfrag_cleanup(sc, &frags, ni);
3459 		ATH_TXBUF_UNLOCK(sc);
3460 		retval = ENOBUFS;
3461 		goto finish;
3462 	}
3463 
3464 	/*
3465 	 * Check here if the node is in power save state.
3466 	 */
3467 	ath_tx_update_tim(sc, ni, 1);
3468 
3469 	if (next != NULL) {
3470 		/*
3471 		 * Beware of state changing between frags.
3472 		 * XXX check sta power-save state?
3473 		 */
3474 		if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
3475 			DPRINTF(sc, ATH_DEBUG_XMIT,
3476 			    "%s: flush fragmented packet, state %s\n",
3477 			    __func__,
3478 			    ieee80211_state_name[ni->ni_vap->iv_state]);
3479 			/* XXX dmamap */
3480 			ath_freetx(next);
3481 			goto reclaim;
3482 		}
3483 		m = next;
3484 		bf = TAILQ_FIRST(&frags);
3485 		KASSERT(bf != NULL, ("no buf for txfrag"));
3486 		TAILQ_REMOVE(&frags, bf, bf_list);
3487 		goto nextfrag;
3488 	}
3489 
3490 	/*
3491 	 * Bump watchdog timer.
3492 	 */
3493 	sc->sc_wd_timer = 5;
3494 
3495 finish:
3496 	ATH_TX_UNLOCK(sc);
3497 
3498 	/*
3499 	 * Finished transmitting!
3500 	 */
3501 	ATH_PCU_LOCK(sc);
3502 	sc->sc_txstart_cnt--;
3503 	ATH_PCU_UNLOCK(sc);
3504 
3505 	/* Sleep the hardware if required */
3506 	ATH_LOCK(sc);
3507 	ath_power_restore_power_state(sc);
3508 	ATH_UNLOCK(sc);
3509 
3510 	ATH_KTR(sc, ATH_KTR_TX, 0, "ath_transmit: finished");
3511 
3512 	return (retval);
3513 }
3514 
3515 static int
3516 ath_media_change(struct ifnet *ifp)
3517 {
3518 	int error = ieee80211_media_change(ifp);
3519 	/* NB: only the fixed rate can change and that doesn't need a reset */
3520 	return (error == ENETRESET ? 0 : error);
3521 }
3522 
3523 /*
3524  * Block/unblock tx+rx processing while a key change is done.
3525  * We assume the caller serializes key management operations
3526  * so we only need to worry about synchronization with other
3527  * uses that originate in the driver.
3528  */
3529 static void
3530 ath_key_update_begin(struct ieee80211vap *vap)
3531 {
3532 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
3533 	struct ath_softc *sc = ifp->if_softc;
3534 
3535 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3536 	taskqueue_block(sc->sc_tq);
3537 }
3538 
3539 static void
3540 ath_key_update_end(struct ieee80211vap *vap)
3541 {
3542 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
3543 	struct ath_softc *sc = ifp->if_softc;
3544 
3545 	DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
3546 	taskqueue_unblock(sc->sc_tq);
3547 }
3548 
3549 static void
3550 ath_update_promisc(struct ieee80211com *ic)
3551 {
3552 	struct ath_softc *sc = ic->ic_softc;
3553 	u_int32_t rfilt;
3554 
3555 	/* configure rx filter */
3556 	ATH_LOCK(sc);
3557 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3558 	rfilt = ath_calcrxfilter(sc);
3559 	ath_hal_setrxfilter(sc->sc_ah, rfilt);
3560 	ath_power_restore_power_state(sc);
3561 	ATH_UNLOCK(sc);
3562 
3563 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
3564 }
3565 
3566 /*
3567  * Driver-internal mcast update call.
3568  *
3569  * Assumes the hardware is already awake.
3570  */
3571 static void
3572 ath_update_mcast_hw(struct ath_softc *sc)
3573 {
3574 	struct ifnet *ifp = sc->sc_ifp;
3575 	u_int32_t mfilt[2];
3576 
3577 	/* calculate and install multicast filter */
3578 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
3579 		struct ifmultiaddr *ifma;
3580 		/*
3581 		 * Merge multicast addresses to form the hardware filter.
3582 		 */
3583 		mfilt[0] = mfilt[1] = 0;
3584 		if_maddr_rlock(ifp);	/* XXX need some fiddling to remove? */
3585 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3586 			caddr_t dl;
3587 			u_int32_t val;
3588 			u_int8_t pos;
3589 
3590 			/* calculate XOR of eight 6bit values */
3591 			dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
3592 			val = LE_READ_4(dl + 0);
3593 			pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3594 			val = LE_READ_4(dl + 3);
3595 			pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3596 			pos &= 0x3f;
3597 			mfilt[pos / 32] |= (1 << (pos % 32));
3598 		}
3599 		if_maddr_runlock(ifp);
3600 	} else
3601 		mfilt[0] = mfilt[1] = ~0;
3602 
3603 	ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
3604 
3605 	DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
3606 		__func__, mfilt[0], mfilt[1]);
3607 }
3608 
3609 /*
3610  * Called from the net80211 layer - force the hardware
3611  * awake before operating.
3612  */
3613 static void
3614 ath_update_mcast(struct ieee80211com *ic)
3615 {
3616 	struct ath_softc *sc = ic->ic_softc;
3617 
3618 	ATH_LOCK(sc);
3619 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3620 	ATH_UNLOCK(sc);
3621 
3622 	ath_update_mcast_hw(sc);
3623 
3624 	ATH_LOCK(sc);
3625 	ath_power_restore_power_state(sc);
3626 	ATH_UNLOCK(sc);
3627 }
3628 
3629 void
3630 ath_mode_init(struct ath_softc *sc)
3631 {
3632 	struct ifnet *ifp = sc->sc_ifp;
3633 	struct ath_hal *ah = sc->sc_ah;
3634 	u_int32_t rfilt;
3635 
3636 	/* configure rx filter */
3637 	rfilt = ath_calcrxfilter(sc);
3638 	ath_hal_setrxfilter(ah, rfilt);
3639 
3640 	/* configure operational mode */
3641 	ath_hal_setopmode(ah);
3642 
3643 	DPRINTF(sc, ATH_DEBUG_STATE | ATH_DEBUG_MODE,
3644 	    "%s: ah=%p, ifp=%p, if_addr=%p\n",
3645 	    __func__,
3646 	    ah,
3647 	    ifp,
3648 	    (ifp == NULL) ? NULL : ifp->if_addr);
3649 
3650 	/* handle any link-level address change */
3651 	ath_hal_setmac(ah, IF_LLADDR(ifp));
3652 
3653 	/* calculate and install multicast filter */
3654 	ath_update_mcast_hw(sc);
3655 }
3656 
3657 /*
3658  * Set the slot time based on the current setting.
3659  */
3660 void
3661 ath_setslottime(struct ath_softc *sc)
3662 {
3663 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3664 	struct ath_hal *ah = sc->sc_ah;
3665 	u_int usec;
3666 
3667 	if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
3668 		usec = 13;
3669 	else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
3670 		usec = 21;
3671 	else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
3672 		/* honor short/long slot time only in 11g */
3673 		/* XXX shouldn't honor on pure g or turbo g channel */
3674 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
3675 			usec = HAL_SLOT_TIME_9;
3676 		else
3677 			usec = HAL_SLOT_TIME_20;
3678 	} else
3679 		usec = HAL_SLOT_TIME_9;
3680 
3681 	DPRINTF(sc, ATH_DEBUG_RESET,
3682 	    "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
3683 	    __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
3684 	    ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
3685 
3686 	/* Wake up the hardware first before updating the slot time */
3687 	ATH_LOCK(sc);
3688 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
3689 	ath_hal_setslottime(ah, usec);
3690 	ath_power_restore_power_state(sc);
3691 	sc->sc_updateslot = OK;
3692 	ATH_UNLOCK(sc);
3693 }
3694 
3695 /*
3696  * Callback from the 802.11 layer to update the
3697  * slot time based on the current setting.
3698  */
3699 static void
3700 ath_updateslot(struct ieee80211com *ic)
3701 {
3702 	struct ath_softc *sc = ic->ic_softc;
3703 
3704 	/*
3705 	 * When not coordinating the BSS, change the hardware
3706 	 * immediately.  For other operation we defer the change
3707 	 * until beacon updates have propagated to the stations.
3708 	 *
3709 	 * XXX sc_updateslot isn't changed behind a lock?
3710 	 */
3711 	if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
3712 	    ic->ic_opmode == IEEE80211_M_MBSS)
3713 		sc->sc_updateslot = UPDATE;
3714 	else
3715 		ath_setslottime(sc);
3716 }
3717 
3718 /*
3719  * Append the contents of src to dst; both queues
3720  * are assumed to be locked.
3721  */
3722 void
3723 ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
3724 {
3725 
3726 	ATH_TXQ_LOCK_ASSERT(src);
3727 	ATH_TXQ_LOCK_ASSERT(dst);
3728 
3729 	TAILQ_CONCAT(&dst->axq_q, &src->axq_q, bf_list);
3730 	dst->axq_link = src->axq_link;
3731 	src->axq_link = NULL;
3732 	dst->axq_depth += src->axq_depth;
3733 	dst->axq_aggr_depth += src->axq_aggr_depth;
3734 	src->axq_depth = 0;
3735 	src->axq_aggr_depth = 0;
3736 }
3737 
3738 /*
3739  * Reset the hardware, with no loss.
3740  *
3741  * This can't be used for a general case reset.
3742  */
3743 static void
3744 ath_reset_proc(void *arg, int pending)
3745 {
3746 	struct ath_softc *sc = arg;
3747 	struct ifnet *ifp = sc->sc_ifp;
3748 
3749 #if 0
3750 	if_printf(ifp, "%s: resetting\n", __func__);
3751 #endif
3752 	ath_reset(ifp, ATH_RESET_NOLOSS);
3753 }
3754 
3755 /*
3756  * Reset the hardware after detecting beacons have stopped.
3757  */
3758 static void
3759 ath_bstuck_proc(void *arg, int pending)
3760 {
3761 	struct ath_softc *sc = arg;
3762 	struct ifnet *ifp = sc->sc_ifp;
3763 	uint32_t hangs = 0;
3764 
3765 	if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0)
3766 		if_printf(ifp, "bb hang detected (0x%x)\n", hangs);
3767 
3768 #ifdef	ATH_DEBUG_ALQ
3769 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_STUCK_BEACON))
3770 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_STUCK_BEACON, 0, NULL);
3771 #endif
3772 
3773 	if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
3774 		sc->sc_bmisscount);
3775 	sc->sc_stats.ast_bstuck++;
3776 	/*
3777 	 * This assumes that there's no simultaneous channel mode change
3778 	 * occuring.
3779 	 */
3780 	ath_reset(ifp, ATH_RESET_NOLOSS);
3781 }
3782 
3783 static void
3784 ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
3785 {
3786 	bus_addr_t *paddr = (bus_addr_t*) arg;
3787 	KASSERT(error == 0, ("error %u on bus_dma callback", error));
3788 	*paddr = segs->ds_addr;
3789 }
3790 
3791 /*
3792  * Allocate the descriptors and appropriate DMA tag/setup.
3793  *
3794  * For some situations (eg EDMA TX completion), there isn't a requirement
3795  * for the ath_buf entries to be allocated.
3796  */
3797 int
3798 ath_descdma_alloc_desc(struct ath_softc *sc,
3799 	struct ath_descdma *dd, ath_bufhead *head,
3800 	const char *name, int ds_size, int ndesc)
3801 {
3802 #define	DS2PHYS(_dd, _ds) \
3803 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
3804 #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
3805 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3806 	struct ifnet *ifp = sc->sc_ifp;
3807 	int error;
3808 
3809 	dd->dd_descsize = ds_size;
3810 
3811 	DPRINTF(sc, ATH_DEBUG_RESET,
3812 	    "%s: %s DMA: %u desc, %d bytes per descriptor\n",
3813 	    __func__, name, ndesc, dd->dd_descsize);
3814 
3815 	dd->dd_name = name;
3816 	dd->dd_desc_len = dd->dd_descsize * ndesc;
3817 
3818 	/*
3819 	 * Merlin work-around:
3820 	 * Descriptors that cross the 4KB boundary can't be used.
3821 	 * Assume one skipped descriptor per 4KB page.
3822 	 */
3823 	if (! ath_hal_split4ktrans(sc->sc_ah)) {
3824 		int numpages = dd->dd_desc_len / 4096;
3825 		dd->dd_desc_len += ds_size * numpages;
3826 	}
3827 
3828 	/*
3829 	 * Setup DMA descriptor area.
3830 	 *
3831 	 * BUS_DMA_ALLOCNOW is not used; we never use bounce
3832 	 * buffers for the descriptors themselves.
3833 	 */
3834 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),	/* parent */
3835 		       PAGE_SIZE, 0,		/* alignment, bounds */
3836 		       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
3837 		       BUS_SPACE_MAXADDR,	/* highaddr */
3838 		       NULL, NULL,		/* filter, filterarg */
3839 		       dd->dd_desc_len,		/* maxsize */
3840 		       1,			/* nsegments */
3841 		       dd->dd_desc_len,		/* maxsegsize */
3842 		       0,			/* flags */
3843 		       NULL,			/* lockfunc */
3844 		       NULL,			/* lockarg */
3845 		       &dd->dd_dmat);
3846 	if (error != 0) {
3847 		if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
3848 		return error;
3849 	}
3850 
3851 	/* allocate descriptors */
3852 	error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
3853 				 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
3854 				 &dd->dd_dmamap);
3855 	if (error != 0) {
3856 		if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3857 			"error %u\n", ndesc, dd->dd_name, error);
3858 		goto fail1;
3859 	}
3860 
3861 	error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3862 				dd->dd_desc, dd->dd_desc_len,
3863 				ath_load_cb, &dd->dd_desc_paddr,
3864 				BUS_DMA_NOWAIT);
3865 	if (error != 0) {
3866 		if_printf(ifp, "unable to map %s descriptors, error %u\n",
3867 			dd->dd_name, error);
3868 		goto fail2;
3869 	}
3870 
3871 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3872 	    __func__, dd->dd_name, (uint8_t *) dd->dd_desc,
3873 	    (u_long) dd->dd_desc_len, (caddr_t) dd->dd_desc_paddr,
3874 	    /*XXX*/ (u_long) dd->dd_desc_len);
3875 
3876 	return (0);
3877 
3878 fail2:
3879 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3880 fail1:
3881 	bus_dma_tag_destroy(dd->dd_dmat);
3882 	memset(dd, 0, sizeof(*dd));
3883 	return error;
3884 #undef DS2PHYS
3885 #undef ATH_DESC_4KB_BOUND_CHECK
3886 }
3887 
3888 int
3889 ath_descdma_setup(struct ath_softc *sc,
3890 	struct ath_descdma *dd, ath_bufhead *head,
3891 	const char *name, int ds_size, int nbuf, int ndesc)
3892 {
3893 #define	DS2PHYS(_dd, _ds) \
3894 	((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
3895 #define	ATH_DESC_4KB_BOUND_CHECK(_daddr, _len) \
3896 	((((u_int32_t)(_daddr) & 0xFFF) > (0x1000 - (_len))) ? 1 : 0)
3897 	struct ifnet *ifp = sc->sc_ifp;
3898 	uint8_t *ds;
3899 	struct ath_buf *bf;
3900 	int i, bsize, error;
3901 
3902 	/* Allocate descriptors */
3903 	error = ath_descdma_alloc_desc(sc, dd, head, name, ds_size,
3904 	    nbuf * ndesc);
3905 
3906 	/* Assume any errors during allocation were dealt with */
3907 	if (error != 0) {
3908 		return (error);
3909 	}
3910 
3911 	ds = (uint8_t *) dd->dd_desc;
3912 
3913 	/* allocate rx buffers */
3914 	bsize = sizeof(struct ath_buf) * nbuf;
3915 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
3916 	if (bf == NULL) {
3917 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3918 			dd->dd_name, bsize);
3919 		goto fail3;
3920 	}
3921 	dd->dd_bufptr = bf;
3922 
3923 	TAILQ_INIT(head);
3924 	for (i = 0; i < nbuf; i++, bf++, ds += (ndesc * dd->dd_descsize)) {
3925 		bf->bf_desc = (struct ath_desc *) ds;
3926 		bf->bf_daddr = DS2PHYS(dd, ds);
3927 		if (! ath_hal_split4ktrans(sc->sc_ah)) {
3928 			/*
3929 			 * Merlin WAR: Skip descriptor addresses which
3930 			 * cause 4KB boundary crossing along any point
3931 			 * in the descriptor.
3932 			 */
3933 			 if (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr,
3934 			     dd->dd_descsize)) {
3935 				/* Start at the next page */
3936 				ds += 0x1000 - (bf->bf_daddr & 0xFFF);
3937 				bf->bf_desc = (struct ath_desc *) ds;
3938 				bf->bf_daddr = DS2PHYS(dd, ds);
3939 			}
3940 		}
3941 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3942 				&bf->bf_dmamap);
3943 		if (error != 0) {
3944 			if_printf(ifp, "unable to create dmamap for %s "
3945 				"buffer %u, error %u\n", dd->dd_name, i, error);
3946 			ath_descdma_cleanup(sc, dd, head);
3947 			return error;
3948 		}
3949 		bf->bf_lastds = bf->bf_desc;	/* Just an initial value */
3950 		TAILQ_INSERT_TAIL(head, bf, bf_list);
3951 	}
3952 
3953 	/*
3954 	 * XXX TODO: ensure that ds doesn't overflow the descriptor
3955 	 * allocation otherwise weird stuff will occur and crash your
3956 	 * machine.
3957 	 */
3958 	return 0;
3959 	/* XXX this should likely just call ath_descdma_cleanup() */
3960 fail3:
3961 	bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3962 	bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3963 	bus_dma_tag_destroy(dd->dd_dmat);
3964 	memset(dd, 0, sizeof(*dd));
3965 	return error;
3966 #undef DS2PHYS
3967 #undef ATH_DESC_4KB_BOUND_CHECK
3968 }
3969 
3970 /*
3971  * Allocate ath_buf entries but no descriptor contents.
3972  *
3973  * This is for RX EDMA where the descriptors are the header part of
3974  * the RX buffer.
3975  */
3976 int
3977 ath_descdma_setup_rx_edma(struct ath_softc *sc,
3978 	struct ath_descdma *dd, ath_bufhead *head,
3979 	const char *name, int nbuf, int rx_status_len)
3980 {
3981 	struct ifnet *ifp = sc->sc_ifp;
3982 	struct ath_buf *bf;
3983 	int i, bsize, error;
3984 
3985 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers\n",
3986 	    __func__, name, nbuf);
3987 
3988 	dd->dd_name = name;
3989 	/*
3990 	 * This is (mostly) purely for show.  We're not allocating any actual
3991 	 * descriptors here as EDMA RX has the descriptor be part
3992 	 * of the RX buffer.
3993 	 *
3994 	 * However, dd_desc_len is used by ath_descdma_free() to determine
3995 	 * whether we have already freed this DMA mapping.
3996 	 */
3997 	dd->dd_desc_len = rx_status_len * nbuf;
3998 	dd->dd_descsize = rx_status_len;
3999 
4000 	/* allocate rx buffers */
4001 	bsize = sizeof(struct ath_buf) * nbuf;
4002 	bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
4003 	if (bf == NULL) {
4004 		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
4005 			dd->dd_name, bsize);
4006 		error = ENOMEM;
4007 		goto fail3;
4008 	}
4009 	dd->dd_bufptr = bf;
4010 
4011 	TAILQ_INIT(head);
4012 	for (i = 0; i < nbuf; i++, bf++) {
4013 		bf->bf_desc = NULL;
4014 		bf->bf_daddr = 0;
4015 		bf->bf_lastds = NULL;	/* Just an initial value */
4016 
4017 		error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
4018 				&bf->bf_dmamap);
4019 		if (error != 0) {
4020 			if_printf(ifp, "unable to create dmamap for %s "
4021 				"buffer %u, error %u\n", dd->dd_name, i, error);
4022 			ath_descdma_cleanup(sc, dd, head);
4023 			return error;
4024 		}
4025 		TAILQ_INSERT_TAIL(head, bf, bf_list);
4026 	}
4027 	return 0;
4028 fail3:
4029 	memset(dd, 0, sizeof(*dd));
4030 	return error;
4031 }
4032 
4033 void
4034 ath_descdma_cleanup(struct ath_softc *sc,
4035 	struct ath_descdma *dd, ath_bufhead *head)
4036 {
4037 	struct ath_buf *bf;
4038 	struct ieee80211_node *ni;
4039 	int do_warning = 0;
4040 
4041 	if (dd->dd_dmamap != 0) {
4042 		bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
4043 		bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
4044 		bus_dma_tag_destroy(dd->dd_dmat);
4045 	}
4046 
4047 	if (head != NULL) {
4048 		TAILQ_FOREACH(bf, head, bf_list) {
4049 			if (bf->bf_m) {
4050 				/*
4051 				 * XXX warn if there's buffers here.
4052 				 * XXX it should have been freed by the
4053 				 * owner!
4054 				 */
4055 
4056 				if (do_warning == 0) {
4057 					do_warning = 1;
4058 					device_printf(sc->sc_dev,
4059 					    "%s: %s: mbuf should've been"
4060 					    " unmapped/freed!\n",
4061 					    __func__,
4062 					    dd->dd_name);
4063 				}
4064 				bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
4065 				    BUS_DMASYNC_POSTREAD);
4066 				bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
4067 				m_freem(bf->bf_m);
4068 				bf->bf_m = NULL;
4069 			}
4070 			if (bf->bf_dmamap != NULL) {
4071 				bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
4072 				bf->bf_dmamap = NULL;
4073 			}
4074 			ni = bf->bf_node;
4075 			bf->bf_node = NULL;
4076 			if (ni != NULL) {
4077 				/*
4078 				 * Reclaim node reference.
4079 				 */
4080 				ieee80211_free_node(ni);
4081 			}
4082 		}
4083 	}
4084 
4085 	if (head != NULL)
4086 		TAILQ_INIT(head);
4087 
4088 	if (dd->dd_bufptr != NULL)
4089 		free(dd->dd_bufptr, M_ATHDEV);
4090 	memset(dd, 0, sizeof(*dd));
4091 }
4092 
4093 static int
4094 ath_desc_alloc(struct ath_softc *sc)
4095 {
4096 	int error;
4097 
4098 	error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
4099 		    "tx", sc->sc_tx_desclen, ath_txbuf, ATH_MAX_SCATTER);
4100 	if (error != 0) {
4101 		return error;
4102 	}
4103 	sc->sc_txbuf_cnt = ath_txbuf;
4104 
4105 	error = ath_descdma_setup(sc, &sc->sc_txdma_mgmt, &sc->sc_txbuf_mgmt,
4106 		    "tx_mgmt", sc->sc_tx_desclen, ath_txbuf_mgmt,
4107 		    ATH_TXDESC);
4108 	if (error != 0) {
4109 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
4110 		return error;
4111 	}
4112 
4113 	/*
4114 	 * XXX mark txbuf_mgmt frames with ATH_BUF_MGMT, so the
4115 	 * flag doesn't have to be set in ath_getbuf_locked().
4116 	 */
4117 
4118 	error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
4119 			"beacon", sc->sc_tx_desclen, ATH_BCBUF, 1);
4120 	if (error != 0) {
4121 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
4122 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
4123 		    &sc->sc_txbuf_mgmt);
4124 		return error;
4125 	}
4126 	return 0;
4127 }
4128 
4129 static void
4130 ath_desc_free(struct ath_softc *sc)
4131 {
4132 
4133 	if (sc->sc_bdma.dd_desc_len != 0)
4134 		ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
4135 	if (sc->sc_txdma.dd_desc_len != 0)
4136 		ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
4137 	if (sc->sc_txdma_mgmt.dd_desc_len != 0)
4138 		ath_descdma_cleanup(sc, &sc->sc_txdma_mgmt,
4139 		    &sc->sc_txbuf_mgmt);
4140 }
4141 
4142 static struct ieee80211_node *
4143 ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
4144 {
4145 	struct ieee80211com *ic = vap->iv_ic;
4146 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4147 	const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
4148 	struct ath_node *an;
4149 
4150 	an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
4151 	if (an == NULL) {
4152 		/* XXX stat+msg */
4153 		return NULL;
4154 	}
4155 	ath_rate_node_init(sc, an);
4156 
4157 	/* Setup the mutex - there's no associd yet so set the name to NULL */
4158 	snprintf(an->an_name, sizeof(an->an_name), "%s: node %p",
4159 	    device_get_nameunit(sc->sc_dev), an);
4160 	mtx_init(&an->an_mtx, an->an_name, NULL, MTX_DEF);
4161 
4162 	/* XXX setup ath_tid */
4163 	ath_tx_tid_init(sc, an);
4164 
4165 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__, mac, ":", an);
4166 	return &an->an_node;
4167 }
4168 
4169 static void
4170 ath_node_cleanup(struct ieee80211_node *ni)
4171 {
4172 	struct ieee80211com *ic = ni->ni_ic;
4173 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4174 
4175 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
4176 	    ni->ni_macaddr, ":", ATH_NODE(ni));
4177 
4178 	/* Cleanup ath_tid, free unused bufs, unlink bufs in TXQ */
4179 	ath_tx_node_flush(sc, ATH_NODE(ni));
4180 	ath_rate_node_cleanup(sc, ATH_NODE(ni));
4181 	sc->sc_node_cleanup(ni);
4182 }
4183 
4184 static void
4185 ath_node_free(struct ieee80211_node *ni)
4186 {
4187 	struct ieee80211com *ic = ni->ni_ic;
4188 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4189 
4190 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: an %p\n", __func__,
4191 	    ni->ni_macaddr, ":", ATH_NODE(ni));
4192 	mtx_destroy(&ATH_NODE(ni)->an_mtx);
4193 	sc->sc_node_free(ni);
4194 }
4195 
4196 static void
4197 ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
4198 {
4199 	struct ieee80211com *ic = ni->ni_ic;
4200 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4201 	struct ath_hal *ah = sc->sc_ah;
4202 
4203 	*rssi = ic->ic_node_getrssi(ni);
4204 	if (ni->ni_chan != IEEE80211_CHAN_ANYC)
4205 		*noise = ath_hal_getchannoise(ah, ni->ni_chan);
4206 	else
4207 		*noise = -95;		/* nominally correct */
4208 }
4209 
4210 /*
4211  * Set the default antenna.
4212  */
4213 void
4214 ath_setdefantenna(struct ath_softc *sc, u_int antenna)
4215 {
4216 	struct ath_hal *ah = sc->sc_ah;
4217 
4218 	/* XXX block beacon interrupts */
4219 	ath_hal_setdefantenna(ah, antenna);
4220 	if (sc->sc_defant != antenna)
4221 		sc->sc_stats.ast_ant_defswitch++;
4222 	sc->sc_defant = antenna;
4223 	sc->sc_rxotherant = 0;
4224 }
4225 
4226 static void
4227 ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
4228 {
4229 	txq->axq_qnum = qnum;
4230 	txq->axq_ac = 0;
4231 	txq->axq_depth = 0;
4232 	txq->axq_aggr_depth = 0;
4233 	txq->axq_intrcnt = 0;
4234 	txq->axq_link = NULL;
4235 	txq->axq_softc = sc;
4236 	TAILQ_INIT(&txq->axq_q);
4237 	TAILQ_INIT(&txq->axq_tidq);
4238 	TAILQ_INIT(&txq->fifo.axq_q);
4239 	ATH_TXQ_LOCK_INIT(sc, txq);
4240 }
4241 
4242 /*
4243  * Setup a h/w transmit queue.
4244  */
4245 static struct ath_txq *
4246 ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
4247 {
4248 #define	N(a)	(sizeof(a)/sizeof(a[0]))
4249 	struct ath_hal *ah = sc->sc_ah;
4250 	HAL_TXQ_INFO qi;
4251 	int qnum;
4252 
4253 	memset(&qi, 0, sizeof(qi));
4254 	qi.tqi_subtype = subtype;
4255 	qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
4256 	qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
4257 	qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
4258 	/*
4259 	 * Enable interrupts only for EOL and DESC conditions.
4260 	 * We mark tx descriptors to receive a DESC interrupt
4261 	 * when a tx queue gets deep; otherwise waiting for the
4262 	 * EOL to reap descriptors.  Note that this is done to
4263 	 * reduce interrupt load and this only defers reaping
4264 	 * descriptors, never transmitting frames.  Aside from
4265 	 * reducing interrupts this also permits more concurrency.
4266 	 * The only potential downside is if the tx queue backs
4267 	 * up in which case the top half of the kernel may backup
4268 	 * due to a lack of tx descriptors.
4269 	 */
4270 	if (sc->sc_isedma)
4271 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
4272 		    HAL_TXQ_TXOKINT_ENABLE;
4273 	else
4274 		qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE |
4275 		    HAL_TXQ_TXDESCINT_ENABLE;
4276 
4277 	qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
4278 	if (qnum == -1) {
4279 		/*
4280 		 * NB: don't print a message, this happens
4281 		 * normally on parts with too few tx queues
4282 		 */
4283 		return NULL;
4284 	}
4285 	if (qnum >= N(sc->sc_txq)) {
4286 		device_printf(sc->sc_dev,
4287 			"hal qnum %u out of range, max %zu!\n",
4288 			qnum, N(sc->sc_txq));
4289 		ath_hal_releasetxqueue(ah, qnum);
4290 		return NULL;
4291 	}
4292 	if (!ATH_TXQ_SETUP(sc, qnum)) {
4293 		ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
4294 		sc->sc_txqsetup |= 1<<qnum;
4295 	}
4296 	return &sc->sc_txq[qnum];
4297 #undef N
4298 }
4299 
4300 /*
4301  * Setup a hardware data transmit queue for the specified
4302  * access control.  The hal may not support all requested
4303  * queues in which case it will return a reference to a
4304  * previously setup queue.  We record the mapping from ac's
4305  * to h/w queues for use by ath_tx_start and also track
4306  * the set of h/w queues being used to optimize work in the
4307  * transmit interrupt handler and related routines.
4308  */
4309 static int
4310 ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
4311 {
4312 #define	N(a)	(sizeof(a)/sizeof(a[0]))
4313 	struct ath_txq *txq;
4314 
4315 	if (ac >= N(sc->sc_ac2q)) {
4316 		device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
4317 			ac, N(sc->sc_ac2q));
4318 		return 0;
4319 	}
4320 	txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
4321 	if (txq != NULL) {
4322 		txq->axq_ac = ac;
4323 		sc->sc_ac2q[ac] = txq;
4324 		return 1;
4325 	} else
4326 		return 0;
4327 #undef N
4328 }
4329 
4330 /*
4331  * Update WME parameters for a transmit queue.
4332  */
4333 static int
4334 ath_txq_update(struct ath_softc *sc, int ac)
4335 {
4336 #define	ATH_EXPONENT_TO_VALUE(v)	((1<<v)-1)
4337 #define	ATH_TXOP_TO_US(v)		(v<<5)
4338 	struct ifnet *ifp = sc->sc_ifp;
4339 	struct ieee80211com *ic = ifp->if_l2com;
4340 	struct ath_txq *txq = sc->sc_ac2q[ac];
4341 	struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
4342 	struct ath_hal *ah = sc->sc_ah;
4343 	HAL_TXQ_INFO qi;
4344 
4345 	ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
4346 #ifdef IEEE80211_SUPPORT_TDMA
4347 	if (sc->sc_tdma) {
4348 		/*
4349 		 * AIFS is zero so there's no pre-transmit wait.  The
4350 		 * burst time defines the slot duration and is configured
4351 		 * through net80211.  The QCU is setup to not do post-xmit
4352 		 * back off, lockout all lower-priority QCU's, and fire
4353 		 * off the DMA beacon alert timer which is setup based
4354 		 * on the slot configuration.
4355 		 */
4356 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
4357 			      | HAL_TXQ_TXERRINT_ENABLE
4358 			      | HAL_TXQ_TXURNINT_ENABLE
4359 			      | HAL_TXQ_TXEOLINT_ENABLE
4360 			      | HAL_TXQ_DBA_GATED
4361 			      | HAL_TXQ_BACKOFF_DISABLE
4362 			      | HAL_TXQ_ARB_LOCKOUT_GLOBAL
4363 			      ;
4364 		qi.tqi_aifs = 0;
4365 		/* XXX +dbaprep? */
4366 		qi.tqi_readyTime = sc->sc_tdmaslotlen;
4367 		qi.tqi_burstTime = qi.tqi_readyTime;
4368 	} else {
4369 #endif
4370 		/*
4371 		 * XXX shouldn't this just use the default flags
4372 		 * used in the previous queue setup?
4373 		 */
4374 		qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
4375 			      | HAL_TXQ_TXERRINT_ENABLE
4376 			      | HAL_TXQ_TXDESCINT_ENABLE
4377 			      | HAL_TXQ_TXURNINT_ENABLE
4378 			      | HAL_TXQ_TXEOLINT_ENABLE
4379 			      ;
4380 		qi.tqi_aifs = wmep->wmep_aifsn;
4381 		qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
4382 		qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
4383 		qi.tqi_readyTime = 0;
4384 		qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
4385 #ifdef IEEE80211_SUPPORT_TDMA
4386 	}
4387 #endif
4388 
4389 	DPRINTF(sc, ATH_DEBUG_RESET,
4390 	    "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
4391 	    __func__, txq->axq_qnum, qi.tqi_qflags,
4392 	    qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4393 
4394 	if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
4395 		if_printf(ifp, "unable to update hardware queue "
4396 			"parameters for %s traffic!\n",
4397 			ieee80211_wme_acnames[ac]);
4398 		return 0;
4399 	} else {
4400 		ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4401 		return 1;
4402 	}
4403 #undef ATH_TXOP_TO_US
4404 #undef ATH_EXPONENT_TO_VALUE
4405 }
4406 
4407 /*
4408  * Callback from the 802.11 layer to update WME parameters.
4409  */
4410 int
4411 ath_wme_update(struct ieee80211com *ic)
4412 {
4413 	struct ath_softc *sc = ic->ic_ifp->if_softc;
4414 
4415 	return !ath_txq_update(sc, WME_AC_BE) ||
4416 	    !ath_txq_update(sc, WME_AC_BK) ||
4417 	    !ath_txq_update(sc, WME_AC_VI) ||
4418 	    !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4419 }
4420 
4421 /*
4422  * Reclaim resources for a setup queue.
4423  */
4424 static void
4425 ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4426 {
4427 
4428 	ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4429 	sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4430 	ATH_TXQ_LOCK_DESTROY(txq);
4431 }
4432 
4433 /*
4434  * Reclaim all tx queue resources.
4435  */
4436 static void
4437 ath_tx_cleanup(struct ath_softc *sc)
4438 {
4439 	int i;
4440 
4441 	ATH_TXBUF_LOCK_DESTROY(sc);
4442 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4443 		if (ATH_TXQ_SETUP(sc, i))
4444 			ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4445 }
4446 
4447 /*
4448  * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4449  * using the current rates in sc_rixmap.
4450  */
4451 int
4452 ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
4453 {
4454 	int rix = sc->sc_rixmap[rate];
4455 	/* NB: return lowest rix for invalid rate */
4456 	return (rix == 0xff ? 0 : rix);
4457 }
4458 
4459 static void
4460 ath_tx_update_stats(struct ath_softc *sc, struct ath_tx_status *ts,
4461     struct ath_buf *bf)
4462 {
4463 	struct ieee80211_node *ni = bf->bf_node;
4464 	struct ifnet *ifp = sc->sc_ifp;
4465 	struct ieee80211com *ic = ifp->if_l2com;
4466 	int sr, lr, pri;
4467 
4468 	if (ts->ts_status == 0) {
4469 		u_int8_t txant = ts->ts_antenna;
4470 		sc->sc_stats.ast_ant_tx[txant]++;
4471 		sc->sc_ant_tx[txant]++;
4472 		if (ts->ts_finaltsi != 0)
4473 			sc->sc_stats.ast_tx_altrate++;
4474 		pri = M_WME_GETAC(bf->bf_m);
4475 		if (pri >= WME_AC_VO)
4476 			ic->ic_wme.wme_hipri_traffic++;
4477 		if ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)
4478 			ni->ni_inact = ni->ni_inact_reload;
4479 	} else {
4480 		if (ts->ts_status & HAL_TXERR_XRETRY)
4481 			sc->sc_stats.ast_tx_xretries++;
4482 		if (ts->ts_status & HAL_TXERR_FIFO)
4483 			sc->sc_stats.ast_tx_fifoerr++;
4484 		if (ts->ts_status & HAL_TXERR_FILT)
4485 			sc->sc_stats.ast_tx_filtered++;
4486 		if (ts->ts_status & HAL_TXERR_XTXOP)
4487 			sc->sc_stats.ast_tx_xtxop++;
4488 		if (ts->ts_status & HAL_TXERR_TIMER_EXPIRED)
4489 			sc->sc_stats.ast_tx_timerexpired++;
4490 
4491 		if (bf->bf_m->m_flags & M_FF)
4492 			sc->sc_stats.ast_ff_txerr++;
4493 	}
4494 	/* XXX when is this valid? */
4495 	if (ts->ts_flags & HAL_TX_DESC_CFG_ERR)
4496 		sc->sc_stats.ast_tx_desccfgerr++;
4497 	/*
4498 	 * This can be valid for successful frame transmission!
4499 	 * If there's a TX FIFO underrun during aggregate transmission,
4500 	 * the MAC will pad the rest of the aggregate with delimiters.
4501 	 * If a BA is returned, the frame is marked as "OK" and it's up
4502 	 * to the TX completion code to notice which frames weren't
4503 	 * successfully transmitted.
4504 	 */
4505 	if (ts->ts_flags & HAL_TX_DATA_UNDERRUN)
4506 		sc->sc_stats.ast_tx_data_underrun++;
4507 	if (ts->ts_flags & HAL_TX_DELIM_UNDERRUN)
4508 		sc->sc_stats.ast_tx_delim_underrun++;
4509 
4510 	sr = ts->ts_shortretry;
4511 	lr = ts->ts_longretry;
4512 	sc->sc_stats.ast_tx_shortretry += sr;
4513 	sc->sc_stats.ast_tx_longretry += lr;
4514 
4515 }
4516 
4517 /*
4518  * The default completion. If fail is 1, this means
4519  * "please don't retry the frame, and just return -1 status
4520  * to the net80211 stack.
4521  */
4522 void
4523 ath_tx_default_comp(struct ath_softc *sc, struct ath_buf *bf, int fail)
4524 {
4525 	struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
4526 	int st;
4527 
4528 	if (fail == 1)
4529 		st = -1;
4530 	else
4531 		st = ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) ?
4532 		    ts->ts_status : HAL_TXERR_XRETRY;
4533 
4534 #if 0
4535 	if (bf->bf_state.bfs_dobaw)
4536 		device_printf(sc->sc_dev,
4537 		    "%s: bf %p: seqno %d: dobaw should've been cleared!\n",
4538 		    __func__,
4539 		    bf,
4540 		    SEQNO(bf->bf_state.bfs_seqno));
4541 #endif
4542 	if (bf->bf_next != NULL)
4543 		device_printf(sc->sc_dev,
4544 		    "%s: bf %p: seqno %d: bf_next not NULL!\n",
4545 		    __func__,
4546 		    bf,
4547 		    SEQNO(bf->bf_state.bfs_seqno));
4548 
4549 	/*
4550 	 * Check if the node software queue is empty; if so
4551 	 * then clear the TIM.
4552 	 *
4553 	 * This needs to be done before the buffer is freed as
4554 	 * otherwise the node reference will have been released
4555 	 * and the node may not actually exist any longer.
4556 	 *
4557 	 * XXX I don't like this belonging here, but it's cleaner
4558 	 * to do it here right now then all the other places
4559 	 * where ath_tx_default_comp() is called.
4560 	 *
4561 	 * XXX TODO: during drain, ensure that the callback is
4562 	 * being called so we get a chance to update the TIM.
4563 	 */
4564 	if (bf->bf_node) {
4565 		ATH_TX_LOCK(sc);
4566 		ath_tx_update_tim(sc, bf->bf_node, 0);
4567 		ATH_TX_UNLOCK(sc);
4568 	}
4569 
4570 	/*
4571 	 * Do any tx complete callback.  Note this must
4572 	 * be done before releasing the node reference.
4573 	 * This will free the mbuf, release the net80211
4574 	 * node and recycle the ath_buf.
4575 	 */
4576 	ath_tx_freebuf(sc, bf, st);
4577 }
4578 
4579 /*
4580  * Update rate control with the given completion status.
4581  */
4582 void
4583 ath_tx_update_ratectrl(struct ath_softc *sc, struct ieee80211_node *ni,
4584     struct ath_rc_series *rc, struct ath_tx_status *ts, int frmlen,
4585     int nframes, int nbad)
4586 {
4587 	struct ath_node *an;
4588 
4589 	/* Only for unicast frames */
4590 	if (ni == NULL)
4591 		return;
4592 
4593 	an = ATH_NODE(ni);
4594 	ATH_NODE_UNLOCK_ASSERT(an);
4595 
4596 	if ((ts->ts_status & HAL_TXERR_FILT) == 0) {
4597 		ATH_NODE_LOCK(an);
4598 		ath_rate_tx_complete(sc, an, rc, ts, frmlen, nframes, nbad);
4599 		ATH_NODE_UNLOCK(an);
4600 	}
4601 }
4602 
4603 /*
4604  * Process the completion of the given buffer.
4605  *
4606  * This calls the rate control update and then the buffer completion.
4607  * This will either free the buffer or requeue it.  In any case, the
4608  * bf pointer should be treated as invalid after this function is called.
4609  */
4610 void
4611 ath_tx_process_buf_completion(struct ath_softc *sc, struct ath_txq *txq,
4612     struct ath_tx_status *ts, struct ath_buf *bf)
4613 {
4614 	struct ieee80211_node *ni = bf->bf_node;
4615 
4616 	ATH_TX_UNLOCK_ASSERT(sc);
4617 	ATH_TXQ_UNLOCK_ASSERT(txq);
4618 
4619 	/* If unicast frame, update general statistics */
4620 	if (ni != NULL) {
4621 		/* update statistics */
4622 		ath_tx_update_stats(sc, ts, bf);
4623 	}
4624 
4625 	/*
4626 	 * Call the completion handler.
4627 	 * The completion handler is responsible for
4628 	 * calling the rate control code.
4629 	 *
4630 	 * Frames with no completion handler get the
4631 	 * rate control code called here.
4632 	 */
4633 	if (bf->bf_comp == NULL) {
4634 		if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4635 		    (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0) {
4636 			/*
4637 			 * XXX assume this isn't an aggregate
4638 			 * frame.
4639 			 */
4640 			ath_tx_update_ratectrl(sc, ni,
4641 			     bf->bf_state.bfs_rc, ts,
4642 			    bf->bf_state.bfs_pktlen, 1,
4643 			    (ts->ts_status == 0 ? 0 : 1));
4644 		}
4645 		ath_tx_default_comp(sc, bf, 0);
4646 	} else
4647 		bf->bf_comp(sc, bf, 0);
4648 }
4649 
4650 
4651 
4652 /*
4653  * Process completed xmit descriptors from the specified queue.
4654  * Kick the packet scheduler if needed. This can occur from this
4655  * particular task.
4656  */
4657 static int
4658 ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq, int dosched)
4659 {
4660 	struct ath_hal *ah = sc->sc_ah;
4661 	struct ath_buf *bf;
4662 	struct ath_desc *ds;
4663 	struct ath_tx_status *ts;
4664 	struct ieee80211_node *ni;
4665 #ifdef	IEEE80211_SUPPORT_SUPERG
4666 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4667 #endif	/* IEEE80211_SUPPORT_SUPERG */
4668 	int nacked;
4669 	HAL_STATUS status;
4670 
4671 	DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4672 		__func__, txq->axq_qnum,
4673 		(caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4674 		txq->axq_link);
4675 
4676 	ATH_KTR(sc, ATH_KTR_TXCOMP, 4,
4677 	    "ath_tx_processq: txq=%u head %p link %p depth %p",
4678 	    txq->axq_qnum,
4679 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4680 	    txq->axq_link,
4681 	    txq->axq_depth);
4682 
4683 	nacked = 0;
4684 	for (;;) {
4685 		ATH_TXQ_LOCK(txq);
4686 		txq->axq_intrcnt = 0;	/* reset periodic desc intr count */
4687 		bf = TAILQ_FIRST(&txq->axq_q);
4688 		if (bf == NULL) {
4689 			ATH_TXQ_UNLOCK(txq);
4690 			break;
4691 		}
4692 		ds = bf->bf_lastds;	/* XXX must be setup correctly! */
4693 		ts = &bf->bf_status.ds_txstat;
4694 
4695 		status = ath_hal_txprocdesc(ah, ds, ts);
4696 #ifdef ATH_DEBUG
4697 		if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
4698 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4699 			    status == HAL_OK);
4700 		else if ((sc->sc_debug & ATH_DEBUG_RESET) && (dosched == 0))
4701 			ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4702 			    status == HAL_OK);
4703 #endif
4704 #ifdef	ATH_DEBUG_ALQ
4705 		if (if_ath_alq_checkdebug(&sc->sc_alq,
4706 		    ATH_ALQ_EDMA_TXSTATUS)) {
4707 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_TXSTATUS,
4708 			sc->sc_tx_statuslen,
4709 			(char *) ds);
4710 		}
4711 #endif
4712 
4713 		if (status == HAL_EINPROGRESS) {
4714 			ATH_KTR(sc, ATH_KTR_TXCOMP, 3,
4715 			    "ath_tx_processq: txq=%u, bf=%p ds=%p, HAL_EINPROGRESS",
4716 			    txq->axq_qnum, bf, ds);
4717 			ATH_TXQ_UNLOCK(txq);
4718 			break;
4719 		}
4720 		ATH_TXQ_REMOVE(txq, bf, bf_list);
4721 
4722 		/*
4723 		 * Sanity check.
4724 		 */
4725 		if (txq->axq_qnum != bf->bf_state.bfs_tx_queue) {
4726 			device_printf(sc->sc_dev,
4727 			    "%s: TXQ=%d: bf=%p, bfs_tx_queue=%d\n",
4728 			    __func__,
4729 			    txq->axq_qnum,
4730 			    bf,
4731 			    bf->bf_state.bfs_tx_queue);
4732 		}
4733 		if (txq->axq_qnum != bf->bf_last->bf_state.bfs_tx_queue) {
4734 			device_printf(sc->sc_dev,
4735 			    "%s: TXQ=%d: bf_last=%p, bfs_tx_queue=%d\n",
4736 			    __func__,
4737 			    txq->axq_qnum,
4738 			    bf->bf_last,
4739 			    bf->bf_last->bf_state.bfs_tx_queue);
4740 		}
4741 
4742 #if 0
4743 		if (txq->axq_depth > 0) {
4744 			/*
4745 			 * More frames follow.  Mark the buffer busy
4746 			 * so it's not re-used while the hardware may
4747 			 * still re-read the link field in the descriptor.
4748 			 *
4749 			 * Use the last buffer in an aggregate as that
4750 			 * is where the hardware may be - intermediate
4751 			 * descriptors won't be "busy".
4752 			 */
4753 			bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4754 		} else
4755 			txq->axq_link = NULL;
4756 #else
4757 		bf->bf_last->bf_flags |= ATH_BUF_BUSY;
4758 #endif
4759 		if (bf->bf_state.bfs_aggr)
4760 			txq->axq_aggr_depth--;
4761 
4762 		ni = bf->bf_node;
4763 
4764 		ATH_KTR(sc, ATH_KTR_TXCOMP, 5,
4765 		    "ath_tx_processq: txq=%u, bf=%p, ds=%p, ni=%p, ts_status=0x%08x",
4766 		    txq->axq_qnum, bf, ds, ni, ts->ts_status);
4767 		/*
4768 		 * If unicast frame was ack'd update RSSI,
4769 		 * including the last rx time used to
4770 		 * workaround phantom bmiss interrupts.
4771 		 */
4772 		if (ni != NULL && ts->ts_status == 0 &&
4773 		    ((bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) == 0)) {
4774 			nacked++;
4775 			sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
4776 			ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
4777 				ts->ts_rssi);
4778 		}
4779 		ATH_TXQ_UNLOCK(txq);
4780 
4781 		/*
4782 		 * Update statistics and call completion
4783 		 */
4784 		ath_tx_process_buf_completion(sc, txq, ts, bf);
4785 
4786 		/* XXX at this point, bf and ni may be totally invalid */
4787 	}
4788 #ifdef IEEE80211_SUPPORT_SUPERG
4789 	/*
4790 	 * Flush fast-frame staging queue when traffic slows.
4791 	 */
4792 	if (txq->axq_depth <= 1)
4793 		ieee80211_ff_flush(ic, txq->axq_ac);
4794 #endif
4795 
4796 	/* Kick the software TXQ scheduler */
4797 	if (dosched) {
4798 		ATH_TX_LOCK(sc);
4799 		ath_txq_sched(sc, txq);
4800 		ATH_TX_UNLOCK(sc);
4801 	}
4802 
4803 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
4804 	    "ath_tx_processq: txq=%u: done",
4805 	    txq->axq_qnum);
4806 
4807 	return nacked;
4808 }
4809 
4810 #define	TXQACTIVE(t, q)		( (t) & (1 << (q)))
4811 
4812 /*
4813  * Deferred processing of transmit interrupt; special-cased
4814  * for a single hardware transmit queue (e.g. 5210 and 5211).
4815  */
4816 static void
4817 ath_tx_proc_q0(void *arg, int npending)
4818 {
4819 	struct ath_softc *sc = arg;
4820 	struct ifnet *ifp = sc->sc_ifp;
4821 	uint32_t txqs;
4822 
4823 	ATH_PCU_LOCK(sc);
4824 	sc->sc_txproc_cnt++;
4825 	txqs = sc->sc_txq_active;
4826 	sc->sc_txq_active &= ~txqs;
4827 	ATH_PCU_UNLOCK(sc);
4828 
4829 	ATH_LOCK(sc);
4830 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4831 	ATH_UNLOCK(sc);
4832 
4833 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
4834 	    "ath_tx_proc_q0: txqs=0x%08x", txqs);
4835 
4836 	if (TXQACTIVE(txqs, 0) && ath_tx_processq(sc, &sc->sc_txq[0], 1))
4837 		/* XXX why is lastrx updated in tx code? */
4838 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4839 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
4840 		ath_tx_processq(sc, sc->sc_cabq, 1);
4841 	IF_LOCK(&ifp->if_snd);
4842 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4843 	IF_UNLOCK(&ifp->if_snd);
4844 	sc->sc_wd_timer = 0;
4845 
4846 	if (sc->sc_softled)
4847 		ath_led_event(sc, sc->sc_txrix);
4848 
4849 	ATH_PCU_LOCK(sc);
4850 	sc->sc_txproc_cnt--;
4851 	ATH_PCU_UNLOCK(sc);
4852 
4853 	ATH_LOCK(sc);
4854 	ath_power_restore_power_state(sc);
4855 	ATH_UNLOCK(sc);
4856 
4857 	ath_tx_kick(sc);
4858 }
4859 
4860 /*
4861  * Deferred processing of transmit interrupt; special-cased
4862  * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
4863  */
4864 static void
4865 ath_tx_proc_q0123(void *arg, int npending)
4866 {
4867 	struct ath_softc *sc = arg;
4868 	struct ifnet *ifp = sc->sc_ifp;
4869 	int nacked;
4870 	uint32_t txqs;
4871 
4872 	ATH_PCU_LOCK(sc);
4873 	sc->sc_txproc_cnt++;
4874 	txqs = sc->sc_txq_active;
4875 	sc->sc_txq_active &= ~txqs;
4876 	ATH_PCU_UNLOCK(sc);
4877 
4878 	ATH_LOCK(sc);
4879 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4880 	ATH_UNLOCK(sc);
4881 
4882 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1,
4883 	    "ath_tx_proc_q0123: txqs=0x%08x", txqs);
4884 
4885 	/*
4886 	 * Process each active queue.
4887 	 */
4888 	nacked = 0;
4889 	if (TXQACTIVE(txqs, 0))
4890 		nacked += ath_tx_processq(sc, &sc->sc_txq[0], 1);
4891 	if (TXQACTIVE(txqs, 1))
4892 		nacked += ath_tx_processq(sc, &sc->sc_txq[1], 1);
4893 	if (TXQACTIVE(txqs, 2))
4894 		nacked += ath_tx_processq(sc, &sc->sc_txq[2], 1);
4895 	if (TXQACTIVE(txqs, 3))
4896 		nacked += ath_tx_processq(sc, &sc->sc_txq[3], 1);
4897 	if (TXQACTIVE(txqs, sc->sc_cabq->axq_qnum))
4898 		ath_tx_processq(sc, sc->sc_cabq, 1);
4899 	if (nacked)
4900 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4901 
4902 	IF_LOCK(&ifp->if_snd);
4903 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4904 	IF_UNLOCK(&ifp->if_snd);
4905 	sc->sc_wd_timer = 0;
4906 
4907 	if (sc->sc_softled)
4908 		ath_led_event(sc, sc->sc_txrix);
4909 
4910 	ATH_PCU_LOCK(sc);
4911 	sc->sc_txproc_cnt--;
4912 	ATH_PCU_UNLOCK(sc);
4913 
4914 	ATH_LOCK(sc);
4915 	ath_power_restore_power_state(sc);
4916 	ATH_UNLOCK(sc);
4917 
4918 	ath_tx_kick(sc);
4919 }
4920 
4921 /*
4922  * Deferred processing of transmit interrupt.
4923  */
4924 static void
4925 ath_tx_proc(void *arg, int npending)
4926 {
4927 	struct ath_softc *sc = arg;
4928 	struct ifnet *ifp = sc->sc_ifp;
4929 	int i, nacked;
4930 	uint32_t txqs;
4931 
4932 	ATH_PCU_LOCK(sc);
4933 	sc->sc_txproc_cnt++;
4934 	txqs = sc->sc_txq_active;
4935 	sc->sc_txq_active &= ~txqs;
4936 	ATH_PCU_UNLOCK(sc);
4937 
4938 	ATH_LOCK(sc);
4939 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4940 	ATH_UNLOCK(sc);
4941 
4942 	ATH_KTR(sc, ATH_KTR_TXCOMP, 1, "ath_tx_proc: txqs=0x%08x", txqs);
4943 
4944 	/*
4945 	 * Process each active queue.
4946 	 */
4947 	nacked = 0;
4948 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4949 		if (ATH_TXQ_SETUP(sc, i) && TXQACTIVE(txqs, i))
4950 			nacked += ath_tx_processq(sc, &sc->sc_txq[i], 1);
4951 	if (nacked)
4952 		sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
4953 
4954 	/* XXX check this inside of IF_LOCK? */
4955 	IF_LOCK(&ifp->if_snd);
4956 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4957 	IF_UNLOCK(&ifp->if_snd);
4958 	sc->sc_wd_timer = 0;
4959 
4960 	if (sc->sc_softled)
4961 		ath_led_event(sc, sc->sc_txrix);
4962 
4963 	ATH_PCU_LOCK(sc);
4964 	sc->sc_txproc_cnt--;
4965 	ATH_PCU_UNLOCK(sc);
4966 
4967 	ATH_LOCK(sc);
4968 	ath_power_restore_power_state(sc);
4969 	ATH_UNLOCK(sc);
4970 
4971 	ath_tx_kick(sc);
4972 }
4973 #undef	TXQACTIVE
4974 
4975 /*
4976  * Deferred processing of TXQ rescheduling.
4977  */
4978 static void
4979 ath_txq_sched_tasklet(void *arg, int npending)
4980 {
4981 	struct ath_softc *sc = arg;
4982 	int i;
4983 
4984 	/* XXX is skipping ok? */
4985 	ATH_PCU_LOCK(sc);
4986 #if 0
4987 	if (sc->sc_inreset_cnt > 0) {
4988 		device_printf(sc->sc_dev,
4989 		    "%s: sc_inreset_cnt > 0; skipping\n", __func__);
4990 		ATH_PCU_UNLOCK(sc);
4991 		return;
4992 	}
4993 #endif
4994 	sc->sc_txproc_cnt++;
4995 	ATH_PCU_UNLOCK(sc);
4996 
4997 	ATH_LOCK(sc);
4998 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
4999 	ATH_UNLOCK(sc);
5000 
5001 	ATH_TX_LOCK(sc);
5002 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5003 		if (ATH_TXQ_SETUP(sc, i)) {
5004 			ath_txq_sched(sc, &sc->sc_txq[i]);
5005 		}
5006 	}
5007 	ATH_TX_UNLOCK(sc);
5008 
5009 	ATH_LOCK(sc);
5010 	ath_power_restore_power_state(sc);
5011 	ATH_UNLOCK(sc);
5012 
5013 	ATH_PCU_LOCK(sc);
5014 	sc->sc_txproc_cnt--;
5015 	ATH_PCU_UNLOCK(sc);
5016 }
5017 
5018 void
5019 ath_returnbuf_tail(struct ath_softc *sc, struct ath_buf *bf)
5020 {
5021 
5022 	ATH_TXBUF_LOCK_ASSERT(sc);
5023 
5024 	if (bf->bf_flags & ATH_BUF_MGMT)
5025 		TAILQ_INSERT_TAIL(&sc->sc_txbuf_mgmt, bf, bf_list);
5026 	else {
5027 		TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
5028 		sc->sc_txbuf_cnt++;
5029 		if (sc->sc_txbuf_cnt > ath_txbuf) {
5030 			device_printf(sc->sc_dev,
5031 			    "%s: sc_txbuf_cnt > %d?\n",
5032 			    __func__,
5033 			    ath_txbuf);
5034 			sc->sc_txbuf_cnt = ath_txbuf;
5035 		}
5036 	}
5037 }
5038 
5039 void
5040 ath_returnbuf_head(struct ath_softc *sc, struct ath_buf *bf)
5041 {
5042 
5043 	ATH_TXBUF_LOCK_ASSERT(sc);
5044 
5045 	if (bf->bf_flags & ATH_BUF_MGMT)
5046 		TAILQ_INSERT_HEAD(&sc->sc_txbuf_mgmt, bf, bf_list);
5047 	else {
5048 		TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
5049 		sc->sc_txbuf_cnt++;
5050 		if (sc->sc_txbuf_cnt > ATH_TXBUF) {
5051 			device_printf(sc->sc_dev,
5052 			    "%s: sc_txbuf_cnt > %d?\n",
5053 			    __func__,
5054 			    ATH_TXBUF);
5055 			sc->sc_txbuf_cnt = ATH_TXBUF;
5056 		}
5057 	}
5058 }
5059 
5060 /*
5061  * Free the holding buffer if it exists
5062  */
5063 void
5064 ath_txq_freeholdingbuf(struct ath_softc *sc, struct ath_txq *txq)
5065 {
5066 	ATH_TXBUF_UNLOCK_ASSERT(sc);
5067 	ATH_TXQ_LOCK_ASSERT(txq);
5068 
5069 	if (txq->axq_holdingbf == NULL)
5070 		return;
5071 
5072 	txq->axq_holdingbf->bf_flags &= ~ATH_BUF_BUSY;
5073 
5074 	ATH_TXBUF_LOCK(sc);
5075 	ath_returnbuf_tail(sc, txq->axq_holdingbf);
5076 	ATH_TXBUF_UNLOCK(sc);
5077 
5078 	txq->axq_holdingbf = NULL;
5079 }
5080 
5081 /*
5082  * Add this buffer to the holding queue, freeing the previous
5083  * one if it exists.
5084  */
5085 static void
5086 ath_txq_addholdingbuf(struct ath_softc *sc, struct ath_buf *bf)
5087 {
5088 	struct ath_txq *txq;
5089 
5090 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
5091 
5092 	ATH_TXBUF_UNLOCK_ASSERT(sc);
5093 	ATH_TXQ_LOCK_ASSERT(txq);
5094 
5095 	/* XXX assert ATH_BUF_BUSY is set */
5096 
5097 	/* XXX assert the tx queue is under the max number */
5098 	if (bf->bf_state.bfs_tx_queue > HAL_NUM_TX_QUEUES) {
5099 		device_printf(sc->sc_dev, "%s: bf=%p: invalid tx queue (%d)\n",
5100 		    __func__,
5101 		    bf,
5102 		    bf->bf_state.bfs_tx_queue);
5103 		bf->bf_flags &= ~ATH_BUF_BUSY;
5104 		ath_returnbuf_tail(sc, bf);
5105 		return;
5106 	}
5107 	ath_txq_freeholdingbuf(sc, txq);
5108 	txq->axq_holdingbf = bf;
5109 }
5110 
5111 /*
5112  * Return a buffer to the pool and update the 'busy' flag on the
5113  * previous 'tail' entry.
5114  *
5115  * This _must_ only be called when the buffer is involved in a completed
5116  * TX. The logic is that if it was part of an active TX, the previous
5117  * buffer on the list is now not involved in a halted TX DMA queue, waiting
5118  * for restart (eg for TDMA.)
5119  *
5120  * The caller must free the mbuf and recycle the node reference.
5121  *
5122  * XXX This method of handling busy / holding buffers is insanely stupid.
5123  * It requires bf_state.bfs_tx_queue to be correctly assigned.  It would
5124  * be much nicer if buffers in the processq() methods would instead be
5125  * always completed there (pushed onto a txq or ath_bufhead) so we knew
5126  * exactly what hardware queue they came from in the first place.
5127  */
5128 void
5129 ath_freebuf(struct ath_softc *sc, struct ath_buf *bf)
5130 {
5131 	struct ath_txq *txq;
5132 
5133 	txq = &sc->sc_txq[bf->bf_state.bfs_tx_queue];
5134 
5135 	KASSERT((bf->bf_node == NULL), ("%s: bf->bf_node != NULL\n", __func__));
5136 	KASSERT((bf->bf_m == NULL), ("%s: bf->bf_m != NULL\n", __func__));
5137 
5138 	/*
5139 	 * If this buffer is busy, push it onto the holding queue.
5140 	 */
5141 	if (bf->bf_flags & ATH_BUF_BUSY) {
5142 		ATH_TXQ_LOCK(txq);
5143 		ath_txq_addholdingbuf(sc, bf);
5144 		ATH_TXQ_UNLOCK(txq);
5145 		return;
5146 	}
5147 
5148 	/*
5149 	 * Not a busy buffer, so free normally
5150 	 */
5151 	ATH_TXBUF_LOCK(sc);
5152 	ath_returnbuf_tail(sc, bf);
5153 	ATH_TXBUF_UNLOCK(sc);
5154 }
5155 
5156 /*
5157  * This is currently used by ath_tx_draintxq() and
5158  * ath_tx_tid_free_pkts().
5159  *
5160  * It recycles a single ath_buf.
5161  */
5162 void
5163 ath_tx_freebuf(struct ath_softc *sc, struct ath_buf *bf, int status)
5164 {
5165 	struct ieee80211_node *ni = bf->bf_node;
5166 	struct mbuf *m0 = bf->bf_m;
5167 
5168 	/*
5169 	 * Make sure that we only sync/unload if there's an mbuf.
5170 	 * If not (eg we cloned a buffer), the unload will have already
5171 	 * occured.
5172 	 */
5173 	if (bf->bf_m != NULL) {
5174 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
5175 		    BUS_DMASYNC_POSTWRITE);
5176 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
5177 	}
5178 
5179 	bf->bf_node = NULL;
5180 	bf->bf_m = NULL;
5181 
5182 	/* Free the buffer, it's not needed any longer */
5183 	ath_freebuf(sc, bf);
5184 
5185 	/* Pass the buffer back to net80211 - completing it */
5186 	ieee80211_tx_complete(ni, m0, status);
5187 }
5188 
5189 static struct ath_buf *
5190 ath_tx_draintxq_get_one(struct ath_softc *sc, struct ath_txq *txq)
5191 {
5192 	struct ath_buf *bf;
5193 
5194 	ATH_TXQ_LOCK_ASSERT(txq);
5195 
5196 	/*
5197 	 * Drain the FIFO queue first, then if it's
5198 	 * empty, move to the normal frame queue.
5199 	 */
5200 	bf = TAILQ_FIRST(&txq->fifo.axq_q);
5201 	if (bf != NULL) {
5202 		/*
5203 		 * Is it the last buffer in this set?
5204 		 * Decrement the FIFO counter.
5205 		 */
5206 		if (bf->bf_flags & ATH_BUF_FIFOEND) {
5207 			if (txq->axq_fifo_depth == 0) {
5208 				device_printf(sc->sc_dev,
5209 				    "%s: Q%d: fifo_depth=0, fifo.axq_depth=%d?\n",
5210 				    __func__,
5211 				    txq->axq_qnum,
5212 				    txq->fifo.axq_depth);
5213 			} else
5214 				txq->axq_fifo_depth--;
5215 		}
5216 		ATH_TXQ_REMOVE(&txq->fifo, bf, bf_list);
5217 		return (bf);
5218 	}
5219 
5220 	/*
5221 	 * Debugging!
5222 	 */
5223 	if (txq->axq_fifo_depth != 0 || txq->fifo.axq_depth != 0) {
5224 		device_printf(sc->sc_dev,
5225 		    "%s: Q%d: fifo_depth=%d, fifo.axq_depth=%d\n",
5226 		    __func__,
5227 		    txq->axq_qnum,
5228 		    txq->axq_fifo_depth,
5229 		    txq->fifo.axq_depth);
5230 	}
5231 
5232 	/*
5233 	 * Now drain the pending queue.
5234 	 */
5235 	bf = TAILQ_FIRST(&txq->axq_q);
5236 	if (bf == NULL) {
5237 		txq->axq_link = NULL;
5238 		return (NULL);
5239 	}
5240 	ATH_TXQ_REMOVE(txq, bf, bf_list);
5241 	return (bf);
5242 }
5243 
5244 void
5245 ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
5246 {
5247 #ifdef ATH_DEBUG
5248 	struct ath_hal *ah = sc->sc_ah;
5249 #endif
5250 	struct ath_buf *bf;
5251 	u_int ix;
5252 
5253 	/*
5254 	 * NB: this assumes output has been stopped and
5255 	 *     we do not need to block ath_tx_proc
5256 	 */
5257 	for (ix = 0;; ix++) {
5258 		ATH_TXQ_LOCK(txq);
5259 		bf = ath_tx_draintxq_get_one(sc, txq);
5260 		if (bf == NULL) {
5261 			ATH_TXQ_UNLOCK(txq);
5262 			break;
5263 		}
5264 		if (bf->bf_state.bfs_aggr)
5265 			txq->axq_aggr_depth--;
5266 #ifdef ATH_DEBUG
5267 		if (sc->sc_debug & ATH_DEBUG_RESET) {
5268 			struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5269 			int status = 0;
5270 
5271 			/*
5272 			 * EDMA operation has a TX completion FIFO
5273 			 * separate from the TX descriptor, so this
5274 			 * method of checking the "completion" status
5275 			 * is wrong.
5276 			 */
5277 			if (! sc->sc_isedma) {
5278 				status = (ath_hal_txprocdesc(ah,
5279 				    bf->bf_lastds,
5280 				    &bf->bf_status.ds_txstat) == HAL_OK);
5281 			}
5282 			ath_printtxbuf(sc, bf, txq->axq_qnum, ix, status);
5283 			ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
5284 			    bf->bf_m->m_len, 0, -1);
5285 		}
5286 #endif /* ATH_DEBUG */
5287 		/*
5288 		 * Since we're now doing magic in the completion
5289 		 * functions, we -must- call it for aggregation
5290 		 * destinations or BAW tracking will get upset.
5291 		 */
5292 		/*
5293 		 * Clear ATH_BUF_BUSY; the completion handler
5294 		 * will free the buffer.
5295 		 */
5296 		ATH_TXQ_UNLOCK(txq);
5297 		bf->bf_flags &= ~ATH_BUF_BUSY;
5298 		if (bf->bf_comp)
5299 			bf->bf_comp(sc, bf, 1);
5300 		else
5301 			ath_tx_default_comp(sc, bf, 1);
5302 	}
5303 
5304 	/*
5305 	 * Free the holding buffer if it exists
5306 	 */
5307 	ATH_TXQ_LOCK(txq);
5308 	ath_txq_freeholdingbuf(sc, txq);
5309 	ATH_TXQ_UNLOCK(txq);
5310 
5311 	/*
5312 	 * Drain software queued frames which are on
5313 	 * active TIDs.
5314 	 */
5315 	ath_tx_txq_drain(sc, txq);
5316 }
5317 
5318 static void
5319 ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
5320 {
5321 	struct ath_hal *ah = sc->sc_ah;
5322 
5323 	ATH_TXQ_LOCK_ASSERT(txq);
5324 
5325 	DPRINTF(sc, ATH_DEBUG_RESET,
5326 	    "%s: tx queue [%u] %p, active=%d, hwpending=%d, flags 0x%08x, "
5327 	    "link %p, holdingbf=%p\n",
5328 	    __func__,
5329 	    txq->axq_qnum,
5330 	    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
5331 	    (int) (!! ath_hal_txqenabled(ah, txq->axq_qnum)),
5332 	    (int) ath_hal_numtxpending(ah, txq->axq_qnum),
5333 	    txq->axq_flags,
5334 	    txq->axq_link,
5335 	    txq->axq_holdingbf);
5336 
5337 	(void) ath_hal_stoptxdma(ah, txq->axq_qnum);
5338 	/* We've stopped TX DMA, so mark this as stopped. */
5339 	txq->axq_flags &= ~ATH_TXQ_PUTRUNNING;
5340 
5341 #ifdef	ATH_DEBUG
5342 	if ((sc->sc_debug & ATH_DEBUG_RESET)
5343 	    && (txq->axq_holdingbf != NULL)) {
5344 		ath_printtxbuf(sc, txq->axq_holdingbf, txq->axq_qnum, 0, 0);
5345 	}
5346 #endif
5347 }
5348 
5349 int
5350 ath_stoptxdma(struct ath_softc *sc)
5351 {
5352 	struct ath_hal *ah = sc->sc_ah;
5353 	int i;
5354 
5355 	/* XXX return value */
5356 	if (sc->sc_invalid)
5357 		return 0;
5358 
5359 	if (!sc->sc_invalid) {
5360 		/* don't touch the hardware if marked invalid */
5361 		DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
5362 		    __func__, sc->sc_bhalq,
5363 		    (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
5364 		    NULL);
5365 
5366 		/* stop the beacon queue */
5367 		(void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
5368 
5369 		/* Stop the data queues */
5370 		for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5371 			if (ATH_TXQ_SETUP(sc, i)) {
5372 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
5373 				ath_tx_stopdma(sc, &sc->sc_txq[i]);
5374 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
5375 			}
5376 		}
5377 	}
5378 
5379 	return 1;
5380 }
5381 
5382 #ifdef	ATH_DEBUG
5383 void
5384 ath_tx_dump(struct ath_softc *sc, struct ath_txq *txq)
5385 {
5386 	struct ath_hal *ah = sc->sc_ah;
5387 	struct ath_buf *bf;
5388 	int i = 0;
5389 
5390 	if (! (sc->sc_debug & ATH_DEBUG_RESET))
5391 		return;
5392 
5393 	device_printf(sc->sc_dev, "%s: Q%d: begin\n",
5394 	    __func__, txq->axq_qnum);
5395 	TAILQ_FOREACH(bf, &txq->axq_q, bf_list) {
5396 		ath_printtxbuf(sc, bf, txq->axq_qnum, i,
5397 			ath_hal_txprocdesc(ah, bf->bf_lastds,
5398 			    &bf->bf_status.ds_txstat) == HAL_OK);
5399 		i++;
5400 	}
5401 	device_printf(sc->sc_dev, "%s: Q%d: end\n",
5402 	    __func__, txq->axq_qnum);
5403 }
5404 #endif /* ATH_DEBUG */
5405 
5406 /*
5407  * Drain the transmit queues and reclaim resources.
5408  */
5409 void
5410 ath_legacy_tx_drain(struct ath_softc *sc, ATH_RESET_TYPE reset_type)
5411 {
5412 	struct ath_hal *ah = sc->sc_ah;
5413 	struct ifnet *ifp = sc->sc_ifp;
5414 	int i;
5415 	struct ath_buf *bf_last;
5416 
5417 	(void) ath_stoptxdma(sc);
5418 
5419 	/*
5420 	 * Dump the queue contents
5421 	 */
5422 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
5423 		/*
5424 		 * XXX TODO: should we just handle the completed TX frames
5425 		 * here, whether or not the reset is a full one or not?
5426 		 */
5427 		if (ATH_TXQ_SETUP(sc, i)) {
5428 #ifdef	ATH_DEBUG
5429 			if (sc->sc_debug & ATH_DEBUG_RESET)
5430 				ath_tx_dump(sc, &sc->sc_txq[i]);
5431 #endif	/* ATH_DEBUG */
5432 			if (reset_type == ATH_RESET_NOLOSS) {
5433 				ath_tx_processq(sc, &sc->sc_txq[i], 0);
5434 				ATH_TXQ_LOCK(&sc->sc_txq[i]);
5435 				/*
5436 				 * Free the holding buffer; DMA is now
5437 				 * stopped.
5438 				 */
5439 				ath_txq_freeholdingbuf(sc, &sc->sc_txq[i]);
5440 				/*
5441 				 * Setup the link pointer to be the
5442 				 * _last_ buffer/descriptor in the list.
5443 				 * If there's nothing in the list, set it
5444 				 * to NULL.
5445 				 */
5446 				bf_last = ATH_TXQ_LAST(&sc->sc_txq[i],
5447 				    axq_q_s);
5448 				if (bf_last != NULL) {
5449 					ath_hal_gettxdesclinkptr(ah,
5450 					    bf_last->bf_lastds,
5451 					    &sc->sc_txq[i].axq_link);
5452 				} else {
5453 					sc->sc_txq[i].axq_link = NULL;
5454 				}
5455 				ATH_TXQ_UNLOCK(&sc->sc_txq[i]);
5456 			} else
5457 				ath_tx_draintxq(sc, &sc->sc_txq[i]);
5458 		}
5459 	}
5460 #ifdef ATH_DEBUG
5461 	if (sc->sc_debug & ATH_DEBUG_RESET) {
5462 		struct ath_buf *bf = TAILQ_FIRST(&sc->sc_bbuf);
5463 		if (bf != NULL && bf->bf_m != NULL) {
5464 			ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
5465 				ath_hal_txprocdesc(ah, bf->bf_lastds,
5466 				    &bf->bf_status.ds_txstat) == HAL_OK);
5467 			ieee80211_dump_pkt(ifp->if_l2com,
5468 			    mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
5469 			    0, -1);
5470 		}
5471 	}
5472 #endif /* ATH_DEBUG */
5473 	IF_LOCK(&ifp->if_snd);
5474 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5475 	IF_UNLOCK(&ifp->if_snd);
5476 	sc->sc_wd_timer = 0;
5477 }
5478 
5479 /*
5480  * Update internal state after a channel change.
5481  */
5482 static void
5483 ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
5484 {
5485 	enum ieee80211_phymode mode;
5486 
5487 	/*
5488 	 * Change channels and update the h/w rate map
5489 	 * if we're switching; e.g. 11a to 11b/g.
5490 	 */
5491 	mode = ieee80211_chan2mode(chan);
5492 	if (mode != sc->sc_curmode)
5493 		ath_setcurmode(sc, mode);
5494 	sc->sc_curchan = chan;
5495 }
5496 
5497 /*
5498  * Set/change channels.  If the channel is really being changed,
5499  * it's done by resetting the chip.  To accomplish this we must
5500  * first cleanup any pending DMA, then restart stuff after a la
5501  * ath_init.
5502  */
5503 static int
5504 ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
5505 {
5506 	struct ifnet *ifp = sc->sc_ifp;
5507 	struct ieee80211com *ic = ifp->if_l2com;
5508 	struct ath_hal *ah = sc->sc_ah;
5509 	int ret = 0;
5510 
5511 	/* Treat this as an interface reset */
5512 	ATH_PCU_UNLOCK_ASSERT(sc);
5513 	ATH_UNLOCK_ASSERT(sc);
5514 
5515 	/* (Try to) stop TX/RX from occuring */
5516 	taskqueue_block(sc->sc_tq);
5517 
5518 	ATH_PCU_LOCK(sc);
5519 
5520 	/* Disable interrupts */
5521 	ath_hal_intrset(ah, 0);
5522 
5523 	/* Stop new RX/TX/interrupt completion */
5524 	if (ath_reset_grablock(sc, 1) == 0) {
5525 		device_printf(sc->sc_dev, "%s: concurrent reset! Danger!\n",
5526 		    __func__);
5527 	}
5528 
5529 	/* Stop pending RX/TX completion */
5530 	ath_txrx_stop_locked(sc);
5531 
5532 	ATH_PCU_UNLOCK(sc);
5533 
5534 	DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
5535 	    __func__, ieee80211_chan2ieee(ic, chan),
5536 	    chan->ic_freq, chan->ic_flags);
5537 	if (chan != sc->sc_curchan) {
5538 		HAL_STATUS status;
5539 		/*
5540 		 * To switch channels clear any pending DMA operations;
5541 		 * wait long enough for the RX fifo to drain, reset the
5542 		 * hardware at the new frequency, and then re-enable
5543 		 * the relevant bits of the h/w.
5544 		 */
5545 #if 0
5546 		ath_hal_intrset(ah, 0);		/* disable interrupts */
5547 #endif
5548 		ath_stoprecv(sc, 1);		/* turn off frame recv */
5549 		/*
5550 		 * First, handle completed TX/RX frames.
5551 		 */
5552 		ath_rx_flush(sc);
5553 		ath_draintxq(sc, ATH_RESET_NOLOSS);
5554 		/*
5555 		 * Next, flush the non-scheduled frames.
5556 		 */
5557 		ath_draintxq(sc, ATH_RESET_FULL);	/* clear pending tx frames */
5558 
5559 		ath_update_chainmasks(sc, chan);
5560 		ath_hal_setchainmasks(sc->sc_ah, sc->sc_cur_txchainmask,
5561 		    sc->sc_cur_rxchainmask);
5562 		if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
5563 			if_printf(ifp, "%s: unable to reset "
5564 			    "channel %u (%u MHz, flags 0x%x), hal status %u\n",
5565 			    __func__, ieee80211_chan2ieee(ic, chan),
5566 			    chan->ic_freq, chan->ic_flags, status);
5567 			ret = EIO;
5568 			goto finish;
5569 		}
5570 		sc->sc_diversity = ath_hal_getdiversity(ah);
5571 
5572 		ATH_RX_LOCK(sc);
5573 		sc->sc_rx_stopped = 1;
5574 		sc->sc_rx_resetted = 1;
5575 		ATH_RX_UNLOCK(sc);
5576 
5577 		/* Let DFS at it in case it's a DFS channel */
5578 		ath_dfs_radar_enable(sc, chan);
5579 
5580 		/* Let spectral at in case spectral is enabled */
5581 		ath_spectral_enable(sc, chan);
5582 
5583 		/*
5584 		 * Let bluetooth coexistence at in case it's needed for this
5585 		 * channel
5586 		 */
5587 		ath_btcoex_enable(sc, ic->ic_curchan);
5588 
5589 		/*
5590 		 * If we're doing TDMA, enforce the TXOP limitation for chips
5591 		 * that support it.
5592 		 */
5593 		if (sc->sc_hasenforcetxop && sc->sc_tdma)
5594 			ath_hal_setenforcetxop(sc->sc_ah, 1);
5595 		else
5596 			ath_hal_setenforcetxop(sc->sc_ah, 0);
5597 
5598 		/*
5599 		 * Re-enable rx framework.
5600 		 */
5601 		if (ath_startrecv(sc) != 0) {
5602 			if_printf(ifp, "%s: unable to restart recv logic\n",
5603 			    __func__);
5604 			ret = EIO;
5605 			goto finish;
5606 		}
5607 
5608 		/*
5609 		 * Change channels and update the h/w rate map
5610 		 * if we're switching; e.g. 11a to 11b/g.
5611 		 */
5612 		ath_chan_change(sc, chan);
5613 
5614 		/*
5615 		 * Reset clears the beacon timers; reset them
5616 		 * here if needed.
5617 		 */
5618 		if (sc->sc_beacons) {		/* restart beacons */
5619 #ifdef IEEE80211_SUPPORT_TDMA
5620 			if (sc->sc_tdma)
5621 				ath_tdma_config(sc, NULL);
5622 			else
5623 #endif
5624 			ath_beacon_config(sc, NULL);
5625 		}
5626 
5627 		/*
5628 		 * Re-enable interrupts.
5629 		 */
5630 #if 0
5631 		ath_hal_intrset(ah, sc->sc_imask);
5632 #endif
5633 	}
5634 
5635 finish:
5636 	ATH_PCU_LOCK(sc);
5637 	sc->sc_inreset_cnt--;
5638 	/* XXX only do this if sc_inreset_cnt == 0? */
5639 	ath_hal_intrset(ah, sc->sc_imask);
5640 	ATH_PCU_UNLOCK(sc);
5641 
5642 	IF_LOCK(&ifp->if_snd);
5643 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5644 	IF_UNLOCK(&ifp->if_snd);
5645 	ath_txrx_start(sc);
5646 	/* XXX ath_start? */
5647 
5648 	return ret;
5649 }
5650 
5651 /*
5652  * Periodically recalibrate the PHY to account
5653  * for temperature/environment changes.
5654  */
5655 static void
5656 ath_calibrate(void *arg)
5657 {
5658 	struct ath_softc *sc = arg;
5659 	struct ath_hal *ah = sc->sc_ah;
5660 	struct ifnet *ifp = sc->sc_ifp;
5661 	struct ieee80211com *ic = ifp->if_l2com;
5662 	HAL_BOOL longCal, isCalDone = AH_TRUE;
5663 	HAL_BOOL aniCal, shortCal = AH_FALSE;
5664 	int nextcal;
5665 
5666 	ATH_LOCK_ASSERT(sc);
5667 
5668 	/*
5669 	 * Force the hardware awake for ANI work.
5670 	 */
5671 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5672 
5673 	/* Skip trying to do this if we're in reset */
5674 	if (sc->sc_inreset_cnt)
5675 		goto restart;
5676 
5677 	if (ic->ic_flags & IEEE80211_F_SCAN)	/* defer, off channel */
5678 		goto restart;
5679 	longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5680 	aniCal = (ticks - sc->sc_lastani >= ath_anicalinterval*hz/1000);
5681 	if (sc->sc_doresetcal)
5682 		shortCal = (ticks - sc->sc_lastshortcal >= ath_shortcalinterval*hz/1000);
5683 
5684 	DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: shortCal=%d; longCal=%d; aniCal=%d\n", __func__, shortCal, longCal, aniCal);
5685 	if (aniCal) {
5686 		sc->sc_stats.ast_ani_cal++;
5687 		sc->sc_lastani = ticks;
5688 		ath_hal_ani_poll(ah, sc->sc_curchan);
5689 	}
5690 
5691 	if (longCal) {
5692 		sc->sc_stats.ast_per_cal++;
5693 		sc->sc_lastlongcal = ticks;
5694 		if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
5695 			/*
5696 			 * Rfgain is out of bounds, reset the chip
5697 			 * to load new gain values.
5698 			 */
5699 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5700 				"%s: rfgain change\n", __func__);
5701 			sc->sc_stats.ast_per_rfgain++;
5702 			sc->sc_resetcal = 0;
5703 			sc->sc_doresetcal = AH_TRUE;
5704 			taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
5705 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
5706 			ath_power_restore_power_state(sc);
5707 			return;
5708 		}
5709 		/*
5710 		 * If this long cal is after an idle period, then
5711 		 * reset the data collection state so we start fresh.
5712 		 */
5713 		if (sc->sc_resetcal) {
5714 			(void) ath_hal_calreset(ah, sc->sc_curchan);
5715 			sc->sc_lastcalreset = ticks;
5716 			sc->sc_lastshortcal = ticks;
5717 			sc->sc_resetcal = 0;
5718 			sc->sc_doresetcal = AH_TRUE;
5719 		}
5720 	}
5721 
5722 	/* Only call if we're doing a short/long cal, not for ANI calibration */
5723 	if (shortCal || longCal) {
5724 		isCalDone = AH_FALSE;
5725 		if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
5726 			if (longCal) {
5727 				/*
5728 				 * Calibrate noise floor data again in case of change.
5729 				 */
5730 				ath_hal_process_noisefloor(ah);
5731 			}
5732 		} else {
5733 			DPRINTF(sc, ATH_DEBUG_ANY,
5734 				"%s: calibration of channel %u failed\n",
5735 				__func__, sc->sc_curchan->ic_freq);
5736 			sc->sc_stats.ast_per_calfail++;
5737 		}
5738 		if (shortCal)
5739 			sc->sc_lastshortcal = ticks;
5740 	}
5741 	if (!isCalDone) {
5742 restart:
5743 		/*
5744 		 * Use a shorter interval to potentially collect multiple
5745 		 * data samples required to complete calibration.  Once
5746 		 * we're told the work is done we drop back to a longer
5747 		 * interval between requests.  We're more aggressive doing
5748 		 * work when operating as an AP to improve operation right
5749 		 * after startup.
5750 		 */
5751 		sc->sc_lastshortcal = ticks;
5752 		nextcal = ath_shortcalinterval*hz/1000;
5753 		if (sc->sc_opmode != HAL_M_HOSTAP)
5754 			nextcal *= 10;
5755 		sc->sc_doresetcal = AH_TRUE;
5756 	} else {
5757 		/* nextcal should be the shortest time for next event */
5758 		nextcal = ath_longcalinterval*hz;
5759 		if (sc->sc_lastcalreset == 0)
5760 			sc->sc_lastcalreset = sc->sc_lastlongcal;
5761 		else if (ticks - sc->sc_lastcalreset >= ath_resetcalinterval*hz)
5762 			sc->sc_resetcal = 1;	/* setup reset next trip */
5763 		sc->sc_doresetcal = AH_FALSE;
5764 	}
5765 	/* ANI calibration may occur more often than short/long/resetcal */
5766 	if (ath_anicalinterval > 0)
5767 		nextcal = MIN(nextcal, ath_anicalinterval*hz/1000);
5768 
5769 	if (nextcal != 0) {
5770 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: next +%u (%sisCalDone)\n",
5771 		    __func__, nextcal, isCalDone ? "" : "!");
5772 		callout_reset(&sc->sc_cal_ch, nextcal, ath_calibrate, sc);
5773 	} else {
5774 		DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: calibration disabled\n",
5775 		    __func__);
5776 		/* NB: don't rearm timer */
5777 	}
5778 	/*
5779 	 * Restore power state now that we're done.
5780 	 */
5781 	ath_power_restore_power_state(sc);
5782 }
5783 
5784 static void
5785 ath_scan_start(struct ieee80211com *ic)
5786 {
5787 	struct ifnet *ifp = ic->ic_ifp;
5788 	struct ath_softc *sc = ifp->if_softc;
5789 	struct ath_hal *ah = sc->sc_ah;
5790 	u_int32_t rfilt;
5791 
5792 	/* XXX calibration timer? */
5793 
5794 	ATH_LOCK(sc);
5795 	sc->sc_scanning = 1;
5796 	sc->sc_syncbeacon = 0;
5797 	rfilt = ath_calcrxfilter(sc);
5798 	ATH_UNLOCK(sc);
5799 
5800 	ATH_PCU_LOCK(sc);
5801 	ath_hal_setrxfilter(ah, rfilt);
5802 	ath_hal_setassocid(ah, ifp->if_broadcastaddr, 0);
5803 	ATH_PCU_UNLOCK(sc);
5804 
5805 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0\n",
5806 		 __func__, rfilt, ether_sprintf(ifp->if_broadcastaddr));
5807 }
5808 
5809 static void
5810 ath_scan_end(struct ieee80211com *ic)
5811 {
5812 	struct ifnet *ifp = ic->ic_ifp;
5813 	struct ath_softc *sc = ifp->if_softc;
5814 	struct ath_hal *ah = sc->sc_ah;
5815 	u_int32_t rfilt;
5816 
5817 	ATH_LOCK(sc);
5818 	sc->sc_scanning = 0;
5819 	rfilt = ath_calcrxfilter(sc);
5820 	ATH_UNLOCK(sc);
5821 
5822 	ATH_PCU_LOCK(sc);
5823 	ath_hal_setrxfilter(ah, rfilt);
5824 	ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
5825 
5826 	ath_hal_process_noisefloor(ah);
5827 	ATH_PCU_UNLOCK(sc);
5828 
5829 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
5830 		 __func__, rfilt, ether_sprintf(sc->sc_curbssid),
5831 		 sc->sc_curaid);
5832 }
5833 
5834 #ifdef	ATH_ENABLE_11N
5835 /*
5836  * For now, just do a channel change.
5837  *
5838  * Later, we'll go through the hard slog of suspending tx/rx, changing rate
5839  * control state and resetting the hardware without dropping frames out
5840  * of the queue.
5841  *
5842  * The unfortunate trouble here is making absolutely sure that the
5843  * channel width change has propagated enough so the hardware
5844  * absolutely isn't handed bogus frames for it's current operating
5845  * mode. (Eg, 40MHz frames in 20MHz mode.) Since TX and RX can and
5846  * does occur in parallel, we need to make certain we've blocked
5847  * any further ongoing TX (and RX, that can cause raw TX)
5848  * before we do this.
5849  */
5850 static void
5851 ath_update_chw(struct ieee80211com *ic)
5852 {
5853 	struct ifnet *ifp = ic->ic_ifp;
5854 	struct ath_softc *sc = ifp->if_softc;
5855 
5856 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: called\n", __func__);
5857 	ath_set_channel(ic);
5858 }
5859 #endif	/* ATH_ENABLE_11N */
5860 
5861 static void
5862 ath_set_channel(struct ieee80211com *ic)
5863 {
5864 	struct ifnet *ifp = ic->ic_ifp;
5865 	struct ath_softc *sc = ifp->if_softc;
5866 
5867 	ATH_LOCK(sc);
5868 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5869 	ATH_UNLOCK(sc);
5870 
5871 	(void) ath_chan_set(sc, ic->ic_curchan);
5872 	/*
5873 	 * If we are returning to our bss channel then mark state
5874 	 * so the next recv'd beacon's tsf will be used to sync the
5875 	 * beacon timers.  Note that since we only hear beacons in
5876 	 * sta/ibss mode this has no effect in other operating modes.
5877 	 */
5878 	ATH_LOCK(sc);
5879 	if (!sc->sc_scanning && ic->ic_curchan == ic->ic_bsschan)
5880 		sc->sc_syncbeacon = 1;
5881 	ath_power_restore_power_state(sc);
5882 	ATH_UNLOCK(sc);
5883 }
5884 
5885 /*
5886  * Walk the vap list and check if there any vap's in RUN state.
5887  */
5888 static int
5889 ath_isanyrunningvaps(struct ieee80211vap *this)
5890 {
5891 	struct ieee80211com *ic = this->iv_ic;
5892 	struct ieee80211vap *vap;
5893 
5894 	IEEE80211_LOCK_ASSERT(ic);
5895 
5896 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
5897 		if (vap != this && vap->iv_state >= IEEE80211_S_RUN)
5898 			return 1;
5899 	}
5900 	return 0;
5901 }
5902 
5903 static int
5904 ath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
5905 {
5906 	struct ieee80211com *ic = vap->iv_ic;
5907 	struct ath_softc *sc = ic->ic_ifp->if_softc;
5908 	struct ath_vap *avp = ATH_VAP(vap);
5909 	struct ath_hal *ah = sc->sc_ah;
5910 	struct ieee80211_node *ni = NULL;
5911 	int i, error, stamode;
5912 	u_int32_t rfilt;
5913 	int csa_run_transition = 0;
5914 	enum ieee80211_state ostate = vap->iv_state;
5915 
5916 	static const HAL_LED_STATE leds[] = {
5917 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
5918 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
5919 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
5920 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
5921 	    HAL_LED_RUN, 	/* IEEE80211_S_CAC */
5922 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
5923 	    HAL_LED_RUN, 	/* IEEE80211_S_CSA */
5924 	    HAL_LED_RUN, 	/* IEEE80211_S_SLEEP */
5925 	};
5926 
5927 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
5928 		ieee80211_state_name[ostate],
5929 		ieee80211_state_name[nstate]);
5930 
5931 	/*
5932 	 * net80211 _should_ have the comlock asserted at this point.
5933 	 * There are some comments around the calls to vap->iv_newstate
5934 	 * which indicate that it (newstate) may end up dropping the
5935 	 * lock.  This and the subsequent lock assert check after newstate
5936 	 * are an attempt to catch these and figure out how/why.
5937 	 */
5938 	IEEE80211_LOCK_ASSERT(ic);
5939 
5940 	/* Before we touch the hardware - wake it up */
5941 	ATH_LOCK(sc);
5942 	/*
5943 	 * If the NIC is in anything other than SLEEP state,
5944 	 * we need to ensure that self-generated frames are
5945 	 * set for PWRMGT=0.  Otherwise we may end up with
5946 	 * strange situations.
5947 	 *
5948 	 * XXX TODO: is this actually the case? :-)
5949 	 */
5950 	if (nstate != IEEE80211_S_SLEEP)
5951 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
5952 
5953 	/*
5954 	 * Now, wake the thing up.
5955 	 */
5956 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
5957 
5958 	/*
5959 	 * And stop the calibration callout whilst we have
5960 	 * ATH_LOCK held.
5961 	 */
5962 	callout_stop(&sc->sc_cal_ch);
5963 	ATH_UNLOCK(sc);
5964 
5965 	if (ostate == IEEE80211_S_CSA && nstate == IEEE80211_S_RUN)
5966 		csa_run_transition = 1;
5967 
5968 	ath_hal_setledstate(ah, leds[nstate]);	/* set LED */
5969 
5970 	if (nstate == IEEE80211_S_SCAN) {
5971 		/*
5972 		 * Scanning: turn off beacon miss and don't beacon.
5973 		 * Mark beacon state so when we reach RUN state we'll
5974 		 * [re]setup beacons.  Unblock the task q thread so
5975 		 * deferred interrupt processing is done.
5976 		 */
5977 
5978 		/* Ensure we stay awake during scan */
5979 		ATH_LOCK(sc);
5980 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
5981 		ath_power_setpower(sc, HAL_PM_AWAKE);
5982 		ATH_UNLOCK(sc);
5983 
5984 		ath_hal_intrset(ah,
5985 		    sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
5986 		sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
5987 		sc->sc_beacons = 0;
5988 		taskqueue_unblock(sc->sc_tq);
5989 	}
5990 
5991 	ni = ieee80211_ref_node(vap->iv_bss);
5992 	rfilt = ath_calcrxfilter(sc);
5993 	stamode = (vap->iv_opmode == IEEE80211_M_STA ||
5994 		   vap->iv_opmode == IEEE80211_M_AHDEMO ||
5995 		   vap->iv_opmode == IEEE80211_M_IBSS);
5996 
5997 	/*
5998 	 * XXX Dont need to do this (and others) if we've transitioned
5999 	 * from SLEEP->RUN.
6000 	 */
6001 	if (stamode && nstate == IEEE80211_S_RUN) {
6002 		sc->sc_curaid = ni->ni_associd;
6003 		IEEE80211_ADDR_COPY(sc->sc_curbssid, ni->ni_bssid);
6004 		ath_hal_setassocid(ah, sc->sc_curbssid, sc->sc_curaid);
6005 	}
6006 	DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s aid 0x%x\n",
6007 	   __func__, rfilt, ether_sprintf(sc->sc_curbssid), sc->sc_curaid);
6008 	ath_hal_setrxfilter(ah, rfilt);
6009 
6010 	/* XXX is this to restore keycache on resume? */
6011 	if (vap->iv_opmode != IEEE80211_M_STA &&
6012 	    (vap->iv_flags & IEEE80211_F_PRIVACY)) {
6013 		for (i = 0; i < IEEE80211_WEP_NKID; i++)
6014 			if (ath_hal_keyisvalid(ah, i))
6015 				ath_hal_keysetmac(ah, i, ni->ni_bssid);
6016 	}
6017 
6018 	/*
6019 	 * Invoke the parent method to do net80211 work.
6020 	 */
6021 	error = avp->av_newstate(vap, nstate, arg);
6022 	if (error != 0)
6023 		goto bad;
6024 
6025 	/*
6026 	 * See above: ensure av_newstate() doesn't drop the lock
6027 	 * on us.
6028 	 */
6029 	IEEE80211_LOCK_ASSERT(ic);
6030 
6031 	if (nstate == IEEE80211_S_RUN) {
6032 		/* NB: collect bss node again, it may have changed */
6033 		ieee80211_free_node(ni);
6034 		ni = ieee80211_ref_node(vap->iv_bss);
6035 
6036 		DPRINTF(sc, ATH_DEBUG_STATE,
6037 		    "%s(RUN): iv_flags 0x%08x bintvl %d bssid %s "
6038 		    "capinfo 0x%04x chan %d\n", __func__,
6039 		    vap->iv_flags, ni->ni_intval, ether_sprintf(ni->ni_bssid),
6040 		    ni->ni_capinfo, ieee80211_chan2ieee(ic, ic->ic_curchan));
6041 
6042 		switch (vap->iv_opmode) {
6043 #ifdef IEEE80211_SUPPORT_TDMA
6044 		case IEEE80211_M_AHDEMO:
6045 			if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
6046 				break;
6047 			/* fall thru... */
6048 #endif
6049 		case IEEE80211_M_HOSTAP:
6050 		case IEEE80211_M_IBSS:
6051 		case IEEE80211_M_MBSS:
6052 			/*
6053 			 * Allocate and setup the beacon frame.
6054 			 *
6055 			 * Stop any previous beacon DMA.  This may be
6056 			 * necessary, for example, when an ibss merge
6057 			 * causes reconfiguration; there will be a state
6058 			 * transition from RUN->RUN that means we may
6059 			 * be called with beacon transmission active.
6060 			 */
6061 			ath_hal_stoptxdma(ah, sc->sc_bhalq);
6062 
6063 			error = ath_beacon_alloc(sc, ni);
6064 			if (error != 0)
6065 				goto bad;
6066 			/*
6067 			 * If joining an adhoc network defer beacon timer
6068 			 * configuration to the next beacon frame so we
6069 			 * have a current TSF to use.  Otherwise we're
6070 			 * starting an ibss/bss so there's no need to delay;
6071 			 * if this is the first vap moving to RUN state, then
6072 			 * beacon state needs to be [re]configured.
6073 			 */
6074 			if (vap->iv_opmode == IEEE80211_M_IBSS &&
6075 			    ni->ni_tstamp.tsf != 0) {
6076 				sc->sc_syncbeacon = 1;
6077 			} else if (!sc->sc_beacons) {
6078 #ifdef IEEE80211_SUPPORT_TDMA
6079 				if (vap->iv_caps & IEEE80211_C_TDMA)
6080 					ath_tdma_config(sc, vap);
6081 				else
6082 #endif
6083 					ath_beacon_config(sc, vap);
6084 				sc->sc_beacons = 1;
6085 			}
6086 			break;
6087 		case IEEE80211_M_STA:
6088 			/*
6089 			 * Defer beacon timer configuration to the next
6090 			 * beacon frame so we have a current TSF to use
6091 			 * (any TSF collected when scanning is likely old).
6092 			 * However if it's due to a CSA -> RUN transition,
6093 			 * force a beacon update so we pick up a lack of
6094 			 * beacons from an AP in CAC and thus force a
6095 			 * scan.
6096 			 *
6097 			 * And, there's also corner cases here where
6098 			 * after a scan, the AP may have disappeared.
6099 			 * In that case, we may not receive an actual
6100 			 * beacon to update the beacon timer and thus we
6101 			 * won't get notified of the missing beacons.
6102 			 */
6103 			if (ostate != IEEE80211_S_RUN &&
6104 			    ostate != IEEE80211_S_SLEEP) {
6105 				DPRINTF(sc, ATH_DEBUG_BEACON,
6106 				    "%s: STA; syncbeacon=1\n", __func__);
6107 				sc->sc_syncbeacon = 1;
6108 
6109 				if (csa_run_transition)
6110 					ath_beacon_config(sc, vap);
6111 
6112 			/*
6113 			 * PR: kern/175227
6114 			 *
6115 			 * Reconfigure beacons during reset; as otherwise
6116 			 * we won't get the beacon timers reprogrammed
6117 			 * after a reset and thus we won't pick up a
6118 			 * beacon miss interrupt.
6119 			 *
6120 			 * Hopefully we'll see a beacon before the BMISS
6121 			 * timer fires (too often), leading to a STA
6122 			 * disassociation.
6123 			 */
6124 				sc->sc_beacons = 1;
6125 			}
6126 			break;
6127 		case IEEE80211_M_MONITOR:
6128 			/*
6129 			 * Monitor mode vaps have only INIT->RUN and RUN->RUN
6130 			 * transitions so we must re-enable interrupts here to
6131 			 * handle the case of a single monitor mode vap.
6132 			 */
6133 			ath_hal_intrset(ah, sc->sc_imask);
6134 			break;
6135 		case IEEE80211_M_WDS:
6136 			break;
6137 		default:
6138 			break;
6139 		}
6140 		/*
6141 		 * Let the hal process statistics collected during a
6142 		 * scan so it can provide calibrated noise floor data.
6143 		 */
6144 		ath_hal_process_noisefloor(ah);
6145 		/*
6146 		 * Reset rssi stats; maybe not the best place...
6147 		 */
6148 		sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
6149 		sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
6150 		sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
6151 
6152 		/*
6153 		 * Force awake for RUN mode.
6154 		 */
6155 		ATH_LOCK(sc);
6156 		ath_power_setselfgen(sc, HAL_PM_AWAKE);
6157 		ath_power_setpower(sc, HAL_PM_AWAKE);
6158 
6159 		/*
6160 		 * Finally, start any timers and the task q thread
6161 		 * (in case we didn't go through SCAN state).
6162 		 */
6163 		if (ath_longcalinterval != 0) {
6164 			/* start periodic recalibration timer */
6165 			callout_reset(&sc->sc_cal_ch, 1, ath_calibrate, sc);
6166 		} else {
6167 			DPRINTF(sc, ATH_DEBUG_CALIBRATE,
6168 			    "%s: calibration disabled\n", __func__);
6169 		}
6170 		ATH_UNLOCK(sc);
6171 
6172 		taskqueue_unblock(sc->sc_tq);
6173 	} else if (nstate == IEEE80211_S_INIT) {
6174 		/*
6175 		 * If there are no vaps left in RUN state then
6176 		 * shutdown host/driver operation:
6177 		 * o disable interrupts
6178 		 * o disable the task queue thread
6179 		 * o mark beacon processing as stopped
6180 		 */
6181 		if (!ath_isanyrunningvaps(vap)) {
6182 			sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
6183 			/* disable interrupts  */
6184 			ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
6185 			taskqueue_block(sc->sc_tq);
6186 			sc->sc_beacons = 0;
6187 		}
6188 #ifdef IEEE80211_SUPPORT_TDMA
6189 		ath_hal_setcca(ah, AH_TRUE);
6190 #endif
6191 	} else if (nstate == IEEE80211_S_SLEEP) {
6192 		/* We're going to sleep, so transition appropriately */
6193 		/* For now, only do this if we're a single STA vap */
6194 		if (sc->sc_nvaps == 1 &&
6195 		    vap->iv_opmode == IEEE80211_M_STA) {
6196 			DPRINTF(sc, ATH_DEBUG_BEACON, "%s: syncbeacon=%d\n", __func__, sc->sc_syncbeacon);
6197 			ATH_LOCK(sc);
6198 			/*
6199 			 * Always at least set the self-generated
6200 			 * frame config to set PWRMGT=1.
6201 			 */
6202 			ath_power_setselfgen(sc, HAL_PM_NETWORK_SLEEP);
6203 
6204 			/*
6205 			 * If we're not syncing beacons, transition
6206 			 * to NETWORK_SLEEP.
6207 			 *
6208 			 * We stay awake if syncbeacon > 0 in case
6209 			 * we need to listen for some beacons otherwise
6210 			 * our beacon timer config may be wrong.
6211 			 */
6212 			if (sc->sc_syncbeacon == 0) {
6213 				ath_power_setpower(sc, HAL_PM_NETWORK_SLEEP);
6214 			}
6215 			ATH_UNLOCK(sc);
6216 		}
6217 	}
6218 bad:
6219 	ieee80211_free_node(ni);
6220 
6221 	/*
6222 	 * Restore the power state - either to what it was, or
6223 	 * to network_sleep if it's alright.
6224 	 */
6225 	ATH_LOCK(sc);
6226 	ath_power_restore_power_state(sc);
6227 	ATH_UNLOCK(sc);
6228 	return error;
6229 }
6230 
6231 /*
6232  * Allocate a key cache slot to the station so we can
6233  * setup a mapping from key index to node. The key cache
6234  * slot is needed for managing antenna state and for
6235  * compression when stations do not use crypto.  We do
6236  * it uniliaterally here; if crypto is employed this slot
6237  * will be reassigned.
6238  */
6239 static void
6240 ath_setup_stationkey(struct ieee80211_node *ni)
6241 {
6242 	struct ieee80211vap *vap = ni->ni_vap;
6243 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
6244 	ieee80211_keyix keyix, rxkeyix;
6245 
6246 	/* XXX should take a locked ref to vap->iv_bss */
6247 	if (!ath_key_alloc(vap, &ni->ni_ucastkey, &keyix, &rxkeyix)) {
6248 		/*
6249 		 * Key cache is full; we'll fall back to doing
6250 		 * the more expensive lookup in software.  Note
6251 		 * this also means no h/w compression.
6252 		 */
6253 		/* XXX msg+statistic */
6254 	} else {
6255 		/* XXX locking? */
6256 		ni->ni_ucastkey.wk_keyix = keyix;
6257 		ni->ni_ucastkey.wk_rxkeyix = rxkeyix;
6258 		/* NB: must mark device key to get called back on delete */
6259 		ni->ni_ucastkey.wk_flags |= IEEE80211_KEY_DEVKEY;
6260 		IEEE80211_ADDR_COPY(ni->ni_ucastkey.wk_macaddr, ni->ni_macaddr);
6261 		/* NB: this will create a pass-thru key entry */
6262 		ath_keyset(sc, vap, &ni->ni_ucastkey, vap->iv_bss);
6263 	}
6264 }
6265 
6266 /*
6267  * Setup driver-specific state for a newly associated node.
6268  * Note that we're called also on a re-associate, the isnew
6269  * param tells us if this is the first time or not.
6270  */
6271 static void
6272 ath_newassoc(struct ieee80211_node *ni, int isnew)
6273 {
6274 	struct ath_node *an = ATH_NODE(ni);
6275 	struct ieee80211vap *vap = ni->ni_vap;
6276 	struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
6277 	const struct ieee80211_txparam *tp = ni->ni_txparms;
6278 
6279 	an->an_mcastrix = ath_tx_findrix(sc, tp->mcastrate);
6280 	an->an_mgmtrix = ath_tx_findrix(sc, tp->mgmtrate);
6281 
6282 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: %6D: reassoc; isnew=%d, is_powersave=%d\n",
6283 	    __func__,
6284 	    ni->ni_macaddr,
6285 	    ":",
6286 	    isnew,
6287 	    an->an_is_powersave);
6288 
6289 	ATH_NODE_LOCK(an);
6290 	ath_rate_newassoc(sc, an, isnew);
6291 	ATH_NODE_UNLOCK(an);
6292 
6293 	if (isnew &&
6294 	    (vap->iv_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey &&
6295 	    ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
6296 		ath_setup_stationkey(ni);
6297 
6298 	/*
6299 	 * If we're reassociating, make sure that any paused queues
6300 	 * get unpaused.
6301 	 *
6302 	 * Now, we may hvae frames in the hardware queue for this node.
6303 	 * So if we are reassociating and there are frames in the queue,
6304 	 * we need to go through the cleanup path to ensure that they're
6305 	 * marked as non-aggregate.
6306 	 */
6307 	if (! isnew) {
6308 		DPRINTF(sc, ATH_DEBUG_NODE,
6309 		    "%s: %6D: reassoc; is_powersave=%d\n",
6310 		    __func__,
6311 		    ni->ni_macaddr,
6312 		    ":",
6313 		    an->an_is_powersave);
6314 
6315 		/* XXX for now, we can't hold the lock across assoc */
6316 		ath_tx_node_reassoc(sc, an);
6317 
6318 		/* XXX for now, we can't hold the lock across wakeup */
6319 		if (an->an_is_powersave)
6320 			ath_tx_node_wakeup(sc, an);
6321 	}
6322 }
6323 
6324 static int
6325 ath_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *reg,
6326 	int nchans, struct ieee80211_channel chans[])
6327 {
6328 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6329 	struct ath_hal *ah = sc->sc_ah;
6330 	HAL_STATUS status;
6331 
6332 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
6333 	    "%s: rd %u cc %u location %c%s\n",
6334 	    __func__, reg->regdomain, reg->country, reg->location,
6335 	    reg->ecm ? " ecm" : "");
6336 
6337 	status = ath_hal_set_channels(ah, chans, nchans,
6338 	    reg->country, reg->regdomain);
6339 	if (status != HAL_OK) {
6340 		DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: failed, status %u\n",
6341 		    __func__, status);
6342 		return EINVAL;		/* XXX */
6343 	}
6344 
6345 	return 0;
6346 }
6347 
6348 static void
6349 ath_getradiocaps(struct ieee80211com *ic,
6350 	int maxchans, int *nchans, struct ieee80211_channel chans[])
6351 {
6352 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6353 	struct ath_hal *ah = sc->sc_ah;
6354 
6355 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN, "%s: use rd %u cc %d\n",
6356 	    __func__, SKU_DEBUG, CTRY_DEFAULT);
6357 
6358 	/* XXX check return */
6359 	(void) ath_hal_getchannels(ah, chans, maxchans, nchans,
6360 	    HAL_MODE_ALL, CTRY_DEFAULT, SKU_DEBUG, AH_TRUE);
6361 
6362 }
6363 
6364 static int
6365 ath_getchannels(struct ath_softc *sc)
6366 {
6367 	struct ifnet *ifp = sc->sc_ifp;
6368 	struct ieee80211com *ic = ifp->if_l2com;
6369 	struct ath_hal *ah = sc->sc_ah;
6370 	HAL_STATUS status;
6371 
6372 	/*
6373 	 * Collect channel set based on EEPROM contents.
6374 	 */
6375 	status = ath_hal_init_channels(ah, ic->ic_channels, IEEE80211_CHAN_MAX,
6376 	    &ic->ic_nchans, HAL_MODE_ALL, CTRY_DEFAULT, SKU_NONE, AH_TRUE);
6377 	if (status != HAL_OK) {
6378 		if_printf(ifp, "%s: unable to collect channel list from hal, "
6379 		    "status %d\n", __func__, status);
6380 		return EINVAL;
6381 	}
6382 	(void) ath_hal_getregdomain(ah, &sc->sc_eerd);
6383 	ath_hal_getcountrycode(ah, &sc->sc_eecc);	/* NB: cannot fail */
6384 	/* XXX map Atheros sku's to net80211 SKU's */
6385 	/* XXX net80211 types too small */
6386 	ic->ic_regdomain.regdomain = (uint16_t) sc->sc_eerd;
6387 	ic->ic_regdomain.country = (uint16_t) sc->sc_eecc;
6388 	ic->ic_regdomain.isocc[0] = ' ';	/* XXX don't know */
6389 	ic->ic_regdomain.isocc[1] = ' ';
6390 
6391 	ic->ic_regdomain.ecm = 1;
6392 	ic->ic_regdomain.location = 'I';
6393 
6394 	DPRINTF(sc, ATH_DEBUG_REGDOMAIN,
6395 	    "%s: eeprom rd %u cc %u (mapped rd %u cc %u) location %c%s\n",
6396 	    __func__, sc->sc_eerd, sc->sc_eecc,
6397 	    ic->ic_regdomain.regdomain, ic->ic_regdomain.country,
6398 	    ic->ic_regdomain.location, ic->ic_regdomain.ecm ? " ecm" : "");
6399 	return 0;
6400 }
6401 
6402 static int
6403 ath_rate_setup(struct ath_softc *sc, u_int mode)
6404 {
6405 	struct ath_hal *ah = sc->sc_ah;
6406 	const HAL_RATE_TABLE *rt;
6407 
6408 	switch (mode) {
6409 	case IEEE80211_MODE_11A:
6410 		rt = ath_hal_getratetable(ah, HAL_MODE_11A);
6411 		break;
6412 	case IEEE80211_MODE_HALF:
6413 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_HALF_RATE);
6414 		break;
6415 	case IEEE80211_MODE_QUARTER:
6416 		rt = ath_hal_getratetable(ah, HAL_MODE_11A_QUARTER_RATE);
6417 		break;
6418 	case IEEE80211_MODE_11B:
6419 		rt = ath_hal_getratetable(ah, HAL_MODE_11B);
6420 		break;
6421 	case IEEE80211_MODE_11G:
6422 		rt = ath_hal_getratetable(ah, HAL_MODE_11G);
6423 		break;
6424 	case IEEE80211_MODE_TURBO_A:
6425 		rt = ath_hal_getratetable(ah, HAL_MODE_108A);
6426 		break;
6427 	case IEEE80211_MODE_TURBO_G:
6428 		rt = ath_hal_getratetable(ah, HAL_MODE_108G);
6429 		break;
6430 	case IEEE80211_MODE_STURBO_A:
6431 		rt = ath_hal_getratetable(ah, HAL_MODE_TURBO);
6432 		break;
6433 	case IEEE80211_MODE_11NA:
6434 		rt = ath_hal_getratetable(ah, HAL_MODE_11NA_HT20);
6435 		break;
6436 	case IEEE80211_MODE_11NG:
6437 		rt = ath_hal_getratetable(ah, HAL_MODE_11NG_HT20);
6438 		break;
6439 	default:
6440 		DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
6441 			__func__, mode);
6442 		return 0;
6443 	}
6444 	sc->sc_rates[mode] = rt;
6445 	return (rt != NULL);
6446 }
6447 
6448 static void
6449 ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
6450 {
6451 #define	N(a)	(sizeof(a)/sizeof(a[0]))
6452 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
6453 	static const struct {
6454 		u_int		rate;		/* tx/rx 802.11 rate */
6455 		u_int16_t	timeOn;		/* LED on time (ms) */
6456 		u_int16_t	timeOff;	/* LED off time (ms) */
6457 	} blinkrates[] = {
6458 		{ 108,  40,  10 },
6459 		{  96,  44,  11 },
6460 		{  72,  50,  13 },
6461 		{  48,  57,  14 },
6462 		{  36,  67,  16 },
6463 		{  24,  80,  20 },
6464 		{  22, 100,  25 },
6465 		{  18, 133,  34 },
6466 		{  12, 160,  40 },
6467 		{  10, 200,  50 },
6468 		{   6, 240,  58 },
6469 		{   4, 267,  66 },
6470 		{   2, 400, 100 },
6471 		{   0, 500, 130 },
6472 		/* XXX half/quarter rates */
6473 	};
6474 	const HAL_RATE_TABLE *rt;
6475 	int i, j;
6476 
6477 	memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
6478 	rt = sc->sc_rates[mode];
6479 	KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
6480 	for (i = 0; i < rt->rateCount; i++) {
6481 		uint8_t ieeerate = rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
6482 		if (rt->info[i].phy != IEEE80211_T_HT)
6483 			sc->sc_rixmap[ieeerate] = i;
6484 		else
6485 			sc->sc_rixmap[ieeerate | IEEE80211_RATE_MCS] = i;
6486 	}
6487 	memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
6488 	for (i = 0; i < N(sc->sc_hwmap); i++) {
6489 		if (i >= rt->rateCount) {
6490 			sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
6491 			sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
6492 			continue;
6493 		}
6494 		sc->sc_hwmap[i].ieeerate =
6495 			rt->info[i].dot11Rate & IEEE80211_RATE_VAL;
6496 		if (rt->info[i].phy == IEEE80211_T_HT)
6497 			sc->sc_hwmap[i].ieeerate |= IEEE80211_RATE_MCS;
6498 		sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
6499 		if (rt->info[i].shortPreamble ||
6500 		    rt->info[i].phy == IEEE80211_T_OFDM)
6501 			sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
6502 		sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags;
6503 		for (j = 0; j < N(blinkrates)-1; j++)
6504 			if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
6505 				break;
6506 		/* NB: this uses the last entry if the rate isn't found */
6507 		/* XXX beware of overlow */
6508 		sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
6509 		sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
6510 	}
6511 	sc->sc_currates = rt;
6512 	sc->sc_curmode = mode;
6513 	/*
6514 	 * All protection frames are transmited at 2Mb/s for
6515 	 * 11g, otherwise at 1Mb/s.
6516 	 */
6517 	if (mode == IEEE80211_MODE_11G)
6518 		sc->sc_protrix = ath_tx_findrix(sc, 2*2);
6519 	else
6520 		sc->sc_protrix = ath_tx_findrix(sc, 2*1);
6521 	/* NB: caller is responsible for resetting rate control state */
6522 #undef N
6523 }
6524 
6525 static void
6526 ath_watchdog(void *arg)
6527 {
6528 	struct ath_softc *sc = arg;
6529 	int do_reset = 0;
6530 
6531 	ATH_LOCK_ASSERT(sc);
6532 
6533 	if (sc->sc_wd_timer != 0 && --sc->sc_wd_timer == 0) {
6534 		struct ifnet *ifp = sc->sc_ifp;
6535 		uint32_t hangs;
6536 
6537 		ath_power_set_power_state(sc, HAL_PM_AWAKE);
6538 
6539 		if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) &&
6540 		    hangs != 0) {
6541 			if_printf(ifp, "%s hang detected (0x%x)\n",
6542 			    hangs & 0xff ? "bb" : "mac", hangs);
6543 		} else
6544 			if_printf(ifp, "device timeout\n");
6545 		do_reset = 1;
6546 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
6547 		sc->sc_stats.ast_watchdog++;
6548 
6549 		ath_power_restore_power_state(sc);
6550 	}
6551 
6552 	/*
6553 	 * We can't hold the lock across the ath_reset() call.
6554 	 *
6555 	 * And since this routine can't hold a lock and sleep,
6556 	 * do the reset deferred.
6557 	 */
6558 	if (do_reset) {
6559 		taskqueue_enqueue(sc->sc_tq, &sc->sc_resettask);
6560 	}
6561 
6562 	callout_schedule(&sc->sc_wd_ch, hz);
6563 }
6564 
6565 /*
6566  * Fetch the rate control statistics for the given node.
6567  */
6568 static int
6569 ath_ioctl_ratestats(struct ath_softc *sc, struct ath_rateioctl *rs)
6570 {
6571 	struct ath_node *an;
6572 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
6573 	struct ieee80211_node *ni;
6574 	int error = 0;
6575 
6576 	/* Perform a lookup on the given node */
6577 	ni = ieee80211_find_node(&ic->ic_sta, rs->is_u.macaddr);
6578 	if (ni == NULL) {
6579 		error = EINVAL;
6580 		goto bad;
6581 	}
6582 
6583 	/* Lock the ath_node */
6584 	an = ATH_NODE(ni);
6585 	ATH_NODE_LOCK(an);
6586 
6587 	/* Fetch the rate control stats for this node */
6588 	error = ath_rate_fetch_node_stats(sc, an, rs);
6589 
6590 	/* No matter what happens here, just drop through */
6591 
6592 	/* Unlock the ath_node */
6593 	ATH_NODE_UNLOCK(an);
6594 
6595 	/* Unref the node */
6596 	ieee80211_node_decref(ni);
6597 
6598 bad:
6599 	return (error);
6600 }
6601 
6602 #ifdef ATH_DIAGAPI
6603 /*
6604  * Diagnostic interface to the HAL.  This is used by various
6605  * tools to do things like retrieve register contents for
6606  * debugging.  The mechanism is intentionally opaque so that
6607  * it can change frequently w/o concern for compatiblity.
6608  */
6609 static int
6610 ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
6611 {
6612 	struct ath_hal *ah = sc->sc_ah;
6613 	u_int id = ad->ad_id & ATH_DIAG_ID;
6614 	void *indata = NULL;
6615 	void *outdata = NULL;
6616 	u_int32_t insize = ad->ad_in_size;
6617 	u_int32_t outsize = ad->ad_out_size;
6618 	int error = 0;
6619 
6620 	if (ad->ad_id & ATH_DIAG_IN) {
6621 		/*
6622 		 * Copy in data.
6623 		 */
6624 		indata = malloc(insize, M_TEMP, M_NOWAIT);
6625 		if (indata == NULL) {
6626 			error = ENOMEM;
6627 			goto bad;
6628 		}
6629 		error = copyin(ad->ad_in_data, indata, insize);
6630 		if (error)
6631 			goto bad;
6632 	}
6633 	if (ad->ad_id & ATH_DIAG_DYN) {
6634 		/*
6635 		 * Allocate a buffer for the results (otherwise the HAL
6636 		 * returns a pointer to a buffer where we can read the
6637 		 * results).  Note that we depend on the HAL leaving this
6638 		 * pointer for us to use below in reclaiming the buffer;
6639 		 * may want to be more defensive.
6640 		 */
6641 		outdata = malloc(outsize, M_TEMP, M_NOWAIT);
6642 		if (outdata == NULL) {
6643 			error = ENOMEM;
6644 			goto bad;
6645 		}
6646 	}
6647 
6648 
6649 	ATH_LOCK(sc);
6650 	if (id != HAL_DIAG_REGS)
6651 		ath_power_set_power_state(sc, HAL_PM_AWAKE);
6652 	ATH_UNLOCK(sc);
6653 
6654 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
6655 		if (outsize < ad->ad_out_size)
6656 			ad->ad_out_size = outsize;
6657 		if (outdata != NULL)
6658 			error = copyout(outdata, ad->ad_out_data,
6659 					ad->ad_out_size);
6660 	} else {
6661 		error = EINVAL;
6662 	}
6663 
6664 	ATH_LOCK(sc);
6665 	if (id != HAL_DIAG_REGS)
6666 		ath_power_restore_power_state(sc);
6667 	ATH_UNLOCK(sc);
6668 
6669 bad:
6670 	if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
6671 		free(indata, M_TEMP);
6672 	if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
6673 		free(outdata, M_TEMP);
6674 	return error;
6675 }
6676 #endif /* ATH_DIAGAPI */
6677 
6678 static int
6679 ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
6680 {
6681 #define	IS_RUNNING(ifp) \
6682 	((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
6683 	struct ath_softc *sc = ifp->if_softc;
6684 	struct ieee80211com *ic = ifp->if_l2com;
6685 	struct ifreq *ifr = (struct ifreq *)data;
6686 	const HAL_RATE_TABLE *rt;
6687 	int error = 0;
6688 
6689 	switch (cmd) {
6690 	case SIOCSIFFLAGS:
6691 		if (IS_RUNNING(ifp)) {
6692 			/*
6693 			 * To avoid rescanning another access point,
6694 			 * do not call ath_init() here.  Instead,
6695 			 * only reflect promisc mode settings.
6696 			 */
6697 			ATH_LOCK(sc);
6698 			ath_power_set_power_state(sc, HAL_PM_AWAKE);
6699 			ath_mode_init(sc);
6700 			ath_power_restore_power_state(sc);
6701 			ATH_UNLOCK(sc);
6702 		} else if (ifp->if_flags & IFF_UP) {
6703 			/*
6704 			 * Beware of being called during attach/detach
6705 			 * to reset promiscuous mode.  In that case we
6706 			 * will still be marked UP but not RUNNING.
6707 			 * However trying to re-init the interface
6708 			 * is the wrong thing to do as we've already
6709 			 * torn down much of our state.  There's
6710 			 * probably a better way to deal with this.
6711 			 */
6712 			if (!sc->sc_invalid)
6713 				ath_init(sc);	/* XXX lose error */
6714 		} else {
6715 			ATH_LOCK(sc);
6716 			ath_stop_locked(ifp);
6717 			if (!sc->sc_invalid)
6718 				ath_power_setpower(sc, HAL_PM_FULL_SLEEP);
6719 			ATH_UNLOCK(sc);
6720 		}
6721 		break;
6722 	case SIOCGIFMEDIA:
6723 	case SIOCSIFMEDIA:
6724 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
6725 		break;
6726 	case SIOCGATHSTATS:
6727 		/* NB: embed these numbers to get a consistent view */
6728 		sc->sc_stats.ast_tx_packets = ifp->if_get_counter(ifp,
6729 		    IFCOUNTER_OPACKETS);
6730 		sc->sc_stats.ast_rx_packets = ifp->if_get_counter(ifp,
6731 		    IFCOUNTER_IPACKETS);
6732 		sc->sc_stats.ast_tx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgtxrssi);
6733 		sc->sc_stats.ast_rx_rssi = ATH_RSSI(sc->sc_halstats.ns_avgrssi);
6734 #ifdef IEEE80211_SUPPORT_TDMA
6735 		sc->sc_stats.ast_tdma_tsfadjp = TDMA_AVG(sc->sc_avgtsfdeltap);
6736 		sc->sc_stats.ast_tdma_tsfadjm = TDMA_AVG(sc->sc_avgtsfdeltam);
6737 #endif
6738 		rt = sc->sc_currates;
6739 		sc->sc_stats.ast_tx_rate =
6740 		    rt->info[sc->sc_txrix].dot11Rate &~ IEEE80211_RATE_BASIC;
6741 		if (rt->info[sc->sc_txrix].phy & IEEE80211_T_HT)
6742 			sc->sc_stats.ast_tx_rate |= IEEE80211_RATE_MCS;
6743 		return copyout(&sc->sc_stats,
6744 		    ifr->ifr_data, sizeof (sc->sc_stats));
6745 	case SIOCGATHAGSTATS:
6746 		return copyout(&sc->sc_aggr_stats,
6747 		    ifr->ifr_data, sizeof (sc->sc_aggr_stats));
6748 	case SIOCZATHSTATS:
6749 		error = priv_check(curthread, PRIV_DRIVER);
6750 		if (error == 0) {
6751 			memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
6752 			memset(&sc->sc_aggr_stats, 0,
6753 			    sizeof(sc->sc_aggr_stats));
6754 			memset(&sc->sc_intr_stats, 0,
6755 			    sizeof(sc->sc_intr_stats));
6756 		}
6757 		break;
6758 #ifdef ATH_DIAGAPI
6759 	case SIOCGATHDIAG:
6760 		error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
6761 		break;
6762 	case SIOCGATHPHYERR:
6763 		error = ath_ioctl_phyerr(sc,(struct ath_diag*) ifr);
6764 		break;
6765 #endif
6766 	case SIOCGATHSPECTRAL:
6767 		error = ath_ioctl_spectral(sc,(struct ath_diag*) ifr);
6768 		break;
6769 	case SIOCGATHNODERATESTATS:
6770 		error = ath_ioctl_ratestats(sc, (struct ath_rateioctl *) ifr);
6771 		break;
6772 	case SIOCGIFADDR:
6773 		error = ether_ioctl(ifp, cmd, data);
6774 		break;
6775 	default:
6776 		error = EINVAL;
6777 		break;
6778 	}
6779 	return error;
6780 #undef IS_RUNNING
6781 }
6782 
6783 /*
6784  * Announce various information on device/driver attach.
6785  */
6786 static void
6787 ath_announce(struct ath_softc *sc)
6788 {
6789 	struct ifnet *ifp = sc->sc_ifp;
6790 	struct ath_hal *ah = sc->sc_ah;
6791 
6792 	if_printf(ifp, "AR%s mac %d.%d RF%s phy %d.%d\n",
6793 		ath_hal_mac_name(ah), ah->ah_macVersion, ah->ah_macRev,
6794 		ath_hal_rf_name(ah), ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
6795 	if_printf(ifp, "2GHz radio: 0x%.4x; 5GHz radio: 0x%.4x\n",
6796 		ah->ah_analog2GhzRev, ah->ah_analog5GhzRev);
6797 	if (bootverbose) {
6798 		int i;
6799 		for (i = 0; i <= WME_AC_VO; i++) {
6800 			struct ath_txq *txq = sc->sc_ac2q[i];
6801 			if_printf(ifp, "Use hw queue %u for %s traffic\n",
6802 				txq->axq_qnum, ieee80211_wme_acnames[i]);
6803 		}
6804 		if_printf(ifp, "Use hw queue %u for CAB traffic\n",
6805 			sc->sc_cabq->axq_qnum);
6806 		if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
6807 	}
6808 	if (ath_rxbuf != ATH_RXBUF)
6809 		if_printf(ifp, "using %u rx buffers\n", ath_rxbuf);
6810 	if (ath_txbuf != ATH_TXBUF)
6811 		if_printf(ifp, "using %u tx buffers\n", ath_txbuf);
6812 	if (sc->sc_mcastkey && bootverbose)
6813 		if_printf(ifp, "using multicast key search\n");
6814 }
6815 
6816 static void
6817 ath_dfs_tasklet(void *p, int npending)
6818 {
6819 	struct ath_softc *sc = (struct ath_softc *) p;
6820 	struct ifnet *ifp = sc->sc_ifp;
6821 	struct ieee80211com *ic = ifp->if_l2com;
6822 
6823 	/*
6824 	 * If previous processing has found a radar event,
6825 	 * signal this to the net80211 layer to begin DFS
6826 	 * processing.
6827 	 */
6828 	if (ath_dfs_process_radar_event(sc, sc->sc_curchan)) {
6829 		/* DFS event found, initiate channel change */
6830 		/*
6831 		 * XXX doesn't currently tell us whether the event
6832 		 * XXX was found in the primary or extension
6833 		 * XXX channel!
6834 		 */
6835 		IEEE80211_LOCK(ic);
6836 		ieee80211_dfs_notify_radar(ic, sc->sc_curchan);
6837 		IEEE80211_UNLOCK(ic);
6838 	}
6839 }
6840 
6841 /*
6842  * Enable/disable power save.  This must be called with
6843  * no TX driver locks currently held, so it should only
6844  * be called from the RX path (which doesn't hold any
6845  * TX driver locks.)
6846  */
6847 static void
6848 ath_node_powersave(struct ieee80211_node *ni, int enable)
6849 {
6850 #ifdef	ATH_SW_PSQ
6851 	struct ath_node *an = ATH_NODE(ni);
6852 	struct ieee80211com *ic = ni->ni_ic;
6853 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6854 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6855 
6856 	/* XXX and no TXQ locks should be held here */
6857 
6858 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE, "%s: %6D: enable=%d\n",
6859 	    __func__,
6860 	    ni->ni_macaddr,
6861 	    ":",
6862 	    !! enable);
6863 
6864 	/* Suspend or resume software queue handling */
6865 	if (enable)
6866 		ath_tx_node_sleep(sc, an);
6867 	else
6868 		ath_tx_node_wakeup(sc, an);
6869 
6870 	/* Update net80211 state */
6871 	avp->av_node_ps(ni, enable);
6872 #else
6873 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6874 
6875 	/* Update net80211 state */
6876 	avp->av_node_ps(ni, enable);
6877 #endif/* ATH_SW_PSQ */
6878 }
6879 
6880 /*
6881  * Notification from net80211 that the powersave queue state has
6882  * changed.
6883  *
6884  * Since the software queue also may have some frames:
6885  *
6886  * + if the node software queue has frames and the TID state
6887  *   is 0, we set the TIM;
6888  * + if the node and the stack are both empty, we clear the TIM bit.
6889  * + If the stack tries to set the bit, always set it.
6890  * + If the stack tries to clear the bit, only clear it if the
6891  *   software queue in question is also cleared.
6892  *
6893  * TODO: this is called during node teardown; so let's ensure this
6894  * is all correctly handled and that the TIM bit is cleared.
6895  * It may be that the node flush is called _AFTER_ the net80211
6896  * stack clears the TIM.
6897  *
6898  * Here is the racy part.  Since it's possible >1 concurrent,
6899  * overlapping TXes will appear complete with a TX completion in
6900  * another thread, it's possible that the concurrent TIM calls will
6901  * clash.  We can't hold the node lock here because setting the
6902  * TIM grabs the net80211 comlock and this may cause a LOR.
6903  * The solution is either to totally serialise _everything_ at
6904  * this point (ie, all TX, completion and any reset/flush go into
6905  * one taskqueue) or a new "ath TIM lock" needs to be created that
6906  * just wraps the driver state change and this call to avp->av_set_tim().
6907  *
6908  * The same race exists in the net80211 power save queue handling
6909  * as well.  Since multiple transmitting threads may queue frames
6910  * into the driver, as well as ps-poll and the driver transmitting
6911  * frames (and thus clearing the psq), it's quite possible that
6912  * a packet entering the PSQ and a ps-poll being handled will
6913  * race, causing the TIM to be cleared and not re-set.
6914  */
6915 static int
6916 ath_node_set_tim(struct ieee80211_node *ni, int enable)
6917 {
6918 #ifdef	ATH_SW_PSQ
6919 	struct ieee80211com *ic = ni->ni_ic;
6920 	struct ath_softc *sc = ic->ic_ifp->if_softc;
6921 	struct ath_node *an = ATH_NODE(ni);
6922 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
6923 	int changed = 0;
6924 
6925 	ATH_TX_LOCK(sc);
6926 	an->an_stack_psq = enable;
6927 
6928 	/*
6929 	 * This will get called for all operating modes,
6930 	 * even if avp->av_set_tim is unset.
6931 	 * It's currently set for hostap/ibss modes; but
6932 	 * the same infrastructure is used for both STA
6933 	 * and AP/IBSS node power save.
6934 	 */
6935 	if (avp->av_set_tim == NULL) {
6936 		ATH_TX_UNLOCK(sc);
6937 		return (0);
6938 	}
6939 
6940 	/*
6941 	 * If setting the bit, always set it here.
6942 	 * If clearing the bit, only clear it if the
6943 	 * software queue is also empty.
6944 	 *
6945 	 * If the node has left power save, just clear the TIM
6946 	 * bit regardless of the state of the power save queue.
6947 	 *
6948 	 * XXX TODO: although atomics are used, it's quite possible
6949 	 * that a race will occur between this and setting/clearing
6950 	 * in another thread.  TX completion will occur always in
6951 	 * one thread, however setting/clearing the TIM bit can come
6952 	 * from a variety of different process contexts!
6953 	 */
6954 	if (enable && an->an_tim_set == 1) {
6955 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
6956 		    "%s: %6D: enable=%d, tim_set=1, ignoring\n",
6957 		    __func__,
6958 		    ni->ni_macaddr,
6959 		    ":",
6960 		    enable);
6961 		ATH_TX_UNLOCK(sc);
6962 	} else if (enable) {
6963 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
6964 		    "%s: %6D: enable=%d, enabling TIM\n",
6965 		    __func__,
6966 		    ni->ni_macaddr,
6967 		    ":",
6968 		    enable);
6969 		an->an_tim_set = 1;
6970 		ATH_TX_UNLOCK(sc);
6971 		changed = avp->av_set_tim(ni, enable);
6972 	} else if (an->an_swq_depth == 0) {
6973 		/* disable */
6974 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
6975 		    "%s: %6D: enable=%d, an_swq_depth == 0, disabling\n",
6976 		    __func__,
6977 		    ni->ni_macaddr,
6978 		    ":",
6979 		    enable);
6980 		an->an_tim_set = 0;
6981 		ATH_TX_UNLOCK(sc);
6982 		changed = avp->av_set_tim(ni, enable);
6983 	} else if (! an->an_is_powersave) {
6984 		/*
6985 		 * disable regardless; the node isn't in powersave now
6986 		 */
6987 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
6988 		    "%s: %6D: enable=%d, an_pwrsave=0, disabling\n",
6989 		    __func__,
6990 		    ni->ni_macaddr,
6991 		    ":",
6992 		    enable);
6993 		an->an_tim_set = 0;
6994 		ATH_TX_UNLOCK(sc);
6995 		changed = avp->av_set_tim(ni, enable);
6996 	} else {
6997 		/*
6998 		 * psq disable, node is currently in powersave, node
6999 		 * software queue isn't empty, so don't clear the TIM bit
7000 		 * for now.
7001 		 */
7002 		ATH_TX_UNLOCK(sc);
7003 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
7004 		    "%s: %6D: enable=%d, an_swq_depth > 0, ignoring\n",
7005 		    __func__,
7006 		    ni->ni_macaddr,
7007 		    ":",
7008 		    enable);
7009 		changed = 0;
7010 	}
7011 
7012 	return (changed);
7013 #else
7014 	struct ath_vap *avp = ATH_VAP(ni->ni_vap);
7015 
7016 	/*
7017 	 * Some operating modes don't set av_set_tim(), so don't
7018 	 * update it here.
7019 	 */
7020 	if (avp->av_set_tim == NULL)
7021 		return (0);
7022 
7023 	return (avp->av_set_tim(ni, enable));
7024 #endif /* ATH_SW_PSQ */
7025 }
7026 
7027 /*
7028  * Set or update the TIM from the software queue.
7029  *
7030  * Check the software queue depth before attempting to do lock
7031  * anything; that avoids trying to obtain the lock.  Then,
7032  * re-check afterwards to ensure nothing has changed in the
7033  * meantime.
7034  *
7035  * set:   This is designed to be called from the TX path, after
7036  *        a frame has been queued; to see if the swq > 0.
7037  *
7038  * clear: This is designed to be called from the buffer completion point
7039  *        (right now it's ath_tx_default_comp()) where the state of
7040  *        a software queue has changed.
7041  *
7042  * It makes sense to place it at buffer free / completion rather
7043  * than after each software queue operation, as there's no real
7044  * point in churning the TIM bit as the last frames in the software
7045  * queue are transmitted.  If they fail and we retry them, we'd
7046  * just be setting the TIM bit again anyway.
7047  */
7048 void
7049 ath_tx_update_tim(struct ath_softc *sc, struct ieee80211_node *ni,
7050      int enable)
7051 {
7052 #ifdef	ATH_SW_PSQ
7053 	struct ath_node *an;
7054 	struct ath_vap *avp;
7055 
7056 	/* Don't do this for broadcast/etc frames */
7057 	if (ni == NULL)
7058 		return;
7059 
7060 	an = ATH_NODE(ni);
7061 	avp = ATH_VAP(ni->ni_vap);
7062 
7063 	/*
7064 	 * And for operating modes without the TIM handler set, let's
7065 	 * just skip those.
7066 	 */
7067 	if (avp->av_set_tim == NULL)
7068 		return;
7069 
7070 	ATH_TX_LOCK_ASSERT(sc);
7071 
7072 	if (enable) {
7073 		if (an->an_is_powersave &&
7074 		    an->an_tim_set == 0 &&
7075 		    an->an_swq_depth != 0) {
7076 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
7077 			    "%s: %6D: swq_depth>0, tim_set=0, set!\n",
7078 			    __func__,
7079 			    ni->ni_macaddr,
7080 			    ":");
7081 			an->an_tim_set = 1;
7082 			(void) avp->av_set_tim(ni, 1);
7083 		}
7084 	} else {
7085 		/*
7086 		 * Don't bother grabbing the lock unless the queue is empty.
7087 		 */
7088 		if (an->an_swq_depth != 0)
7089 			return;
7090 
7091 		if (an->an_is_powersave &&
7092 		    an->an_stack_psq == 0 &&
7093 		    an->an_tim_set == 1 &&
7094 		    an->an_swq_depth == 0) {
7095 			DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
7096 			    "%s: %6D: swq_depth=0, tim_set=1, psq_set=0,"
7097 			    " clear!\n",
7098 			    __func__,
7099 			    ni->ni_macaddr,
7100 			    ":");
7101 			an->an_tim_set = 0;
7102 			(void) avp->av_set_tim(ni, 0);
7103 		}
7104 	}
7105 #else
7106 	return;
7107 #endif	/* ATH_SW_PSQ */
7108 }
7109 
7110 /*
7111  * Received a ps-poll frame from net80211.
7112  *
7113  * Here we get a chance to serve out a software-queued frame ourselves
7114  * before we punt it to net80211 to transmit us one itself - either
7115  * because there's traffic in the net80211 psq, or a NULL frame to
7116  * indicate there's nothing else.
7117  */
7118 static void
7119 ath_node_recv_pspoll(struct ieee80211_node *ni, struct mbuf *m)
7120 {
7121 #ifdef	ATH_SW_PSQ
7122 	struct ath_node *an;
7123 	struct ath_vap *avp;
7124 	struct ieee80211com *ic = ni->ni_ic;
7125 	struct ath_softc *sc = ic->ic_ifp->if_softc;
7126 	int tid;
7127 
7128 	/* Just paranoia */
7129 	if (ni == NULL)
7130 		return;
7131 
7132 	/*
7133 	 * Unassociated (temporary node) station.
7134 	 */
7135 	if (ni->ni_associd == 0)
7136 		return;
7137 
7138 	/*
7139 	 * We do have an active node, so let's begin looking into it.
7140 	 */
7141 	an = ATH_NODE(ni);
7142 	avp = ATH_VAP(ni->ni_vap);
7143 
7144 	/*
7145 	 * For now, we just call the original ps-poll method.
7146 	 * Once we're ready to flip this on:
7147 	 *
7148 	 * + Set leak to 1, as no matter what we're going to have
7149 	 *   to send a frame;
7150 	 * + Check the software queue and if there's something in it,
7151 	 *   schedule the highest TID thas has traffic from this node.
7152 	 *   Then make sure we schedule the software scheduler to
7153 	 *   run so it picks up said frame.
7154 	 *
7155 	 * That way whatever happens, we'll at least send _a_ frame
7156 	 * to the given node.
7157 	 *
7158 	 * Again, yes, it's crappy QoS if the node has multiple
7159 	 * TIDs worth of traffic - but let's get it working first
7160 	 * before we optimise it.
7161 	 *
7162 	 * Also yes, there's definitely latency here - we're not
7163 	 * direct dispatching to the hardware in this path (and
7164 	 * we're likely being called from the packet receive path,
7165 	 * so going back into TX may be a little hairy!) but again
7166 	 * I'd like to get this working first before optimising
7167 	 * turn-around time.
7168 	 */
7169 
7170 	ATH_TX_LOCK(sc);
7171 
7172 	/*
7173 	 * Legacy - we're called and the node isn't asleep.
7174 	 * Immediately punt.
7175 	 */
7176 	if (! an->an_is_powersave) {
7177 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
7178 		    "%s: %6D: not in powersave?\n",
7179 		    __func__,
7180 		    ni->ni_macaddr,
7181 		    ":");
7182 		ATH_TX_UNLOCK(sc);
7183 		avp->av_recv_pspoll(ni, m);
7184 		return;
7185 	}
7186 
7187 	/*
7188 	 * We're in powersave.
7189 	 *
7190 	 * Leak a frame.
7191 	 */
7192 	an->an_leak_count = 1;
7193 
7194 	/*
7195 	 * Now, if there's no frames in the node, just punt to
7196 	 * recv_pspoll.
7197 	 *
7198 	 * Don't bother checking if the TIM bit is set, we really
7199 	 * only care if there are any frames here!
7200 	 */
7201 	if (an->an_swq_depth == 0) {
7202 		ATH_TX_UNLOCK(sc);
7203 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
7204 		    "%s: %6D: SWQ empty; punting to net80211\n",
7205 		    __func__,
7206 		    ni->ni_macaddr,
7207 		    ":");
7208 		avp->av_recv_pspoll(ni, m);
7209 		return;
7210 	}
7211 
7212 	/*
7213 	 * Ok, let's schedule the highest TID that has traffic
7214 	 * and then schedule something.
7215 	 */
7216 	for (tid = IEEE80211_TID_SIZE - 1; tid >= 0; tid--) {
7217 		struct ath_tid *atid = &an->an_tid[tid];
7218 		/*
7219 		 * No frames? Skip.
7220 		 */
7221 		if (atid->axq_depth == 0)
7222 			continue;
7223 		ath_tx_tid_sched(sc, atid);
7224 		/*
7225 		 * XXX we could do a direct call to the TXQ
7226 		 * scheduler code here to optimise latency
7227 		 * at the expense of a REALLY deep callstack.
7228 		 */
7229 		ATH_TX_UNLOCK(sc);
7230 		taskqueue_enqueue(sc->sc_tq, &sc->sc_txqtask);
7231 		DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
7232 		    "%s: %6D: leaking frame to TID %d\n",
7233 		    __func__,
7234 		    ni->ni_macaddr,
7235 		    ":",
7236 		    tid);
7237 		return;
7238 	}
7239 
7240 	ATH_TX_UNLOCK(sc);
7241 
7242 	/*
7243 	 * XXX nothing in the TIDs at this point? Eek.
7244 	 */
7245 	DPRINTF(sc, ATH_DEBUG_NODE_PWRSAVE,
7246 	    "%s: %6D: TIDs empty, but ath_node showed traffic?!\n",
7247 	    __func__,
7248 	    ni->ni_macaddr,
7249 	    ":");
7250 	avp->av_recv_pspoll(ni, m);
7251 #else
7252 	avp->av_recv_pspoll(ni, m);
7253 #endif	/* ATH_SW_PSQ */
7254 }
7255 
7256 MODULE_VERSION(if_ath, 1);
7257 MODULE_DEPEND(if_ath, wlan, 1, 1, 1);          /* 802.11 media layer */
7258 #if	defined(IEEE80211_ALQ) || defined(AH_DEBUG_ALQ) || defined(ATH_DEBUG_ALQ)
7259 MODULE_DEPEND(if_ath, alq, 1, 1, 1);
7260 #endif
7261