xref: /onnv-gate/usr/src/uts/common/io/ath/ath_main.c (revision 6235:4d6d819d5820)
11000Sxc151355 /*
2*6235Sxc151355  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
31000Sxc151355  * Use is subject to license terms.
41000Sxc151355  */
51000Sxc151355 
61000Sxc151355 /*
71000Sxc151355  * Copyright (c) 2002-2004 Sam Leffler, Errno Consulting
81000Sxc151355  * All rights reserved.
91000Sxc151355  *
101000Sxc151355  * Redistribution and use in source and binary forms, with or without
111000Sxc151355  * modification, are permitted provided that the following conditions
121000Sxc151355  * are met:
131000Sxc151355  * 1. Redistributions of source code must retain the above copyright
141000Sxc151355  * notice, this list of conditions and the following disclaimer,
151000Sxc151355  * without modification.
161000Sxc151355  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
171000Sxc151355  * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
181000Sxc151355  * redistribution must be conditioned upon including a substantially
191000Sxc151355  * similar Disclaimer requirement for further binary redistribution.
201000Sxc151355  * 3. Neither the names of the above-listed copyright holders nor the names
211000Sxc151355  * of any contributors may be used to endorse or promote products derived
221000Sxc151355  * from this software without specific prior written permission.
231000Sxc151355  *
241000Sxc151355  * NO WARRANTY
251000Sxc151355  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
261000Sxc151355  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
271000Sxc151355  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
281000Sxc151355  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
291000Sxc151355  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
301000Sxc151355  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311000Sxc151355  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
321000Sxc151355  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
331000Sxc151355  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
341000Sxc151355  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
351000Sxc151355  * THE POSSIBILITY OF SUCH DAMAGES.
361000Sxc151355  *
371000Sxc151355  */
381000Sxc151355 
391000Sxc151355 #pragma ident	"%Z%%M%	%I%	%E% SMI"
401000Sxc151355 
411000Sxc151355 /*
421000Sxc151355  * Driver for the Atheros Wireless LAN controller.
431000Sxc151355  *
443147Sxc151355  * The Atheros driver calls into net80211 module for IEEE80211 protocol
453147Sxc151355  * management functionalities. The driver includes a LLD(Low Level Driver)
463147Sxc151355  * part to implement H/W related operations.
471000Sxc151355  * The following is the high level structure of ath driver.
481000Sxc151355  * (The arrows between modules indicate function call direction.)
491000Sxc151355  *
501000Sxc151355  *
513147Sxc151355  *                                                  |
523147Sxc151355  *                                                  | GLD thread
533147Sxc151355  *                                                  V
543147Sxc151355  *         ==================  =========================================
553147Sxc151355  *         |                |  |[1]                                    |
563147Sxc151355  *         |                |  |  GLDv3 Callback functions registered  |
573147Sxc151355  *         |   Net80211     |  =========================       by      |
583147Sxc151355  *         |    module      |          |               |     driver    |
593147Sxc151355  *         |                |          V               |               |
603147Sxc151355  *         |                |========================  |               |
613147Sxc151355  *         |   Functions exported by net80211       |  |               |
623147Sxc151355  *         |                                        |  |               |
633147Sxc151355  *         ==========================================  =================
643147Sxc151355  *                         |                                  |
653147Sxc151355  *                         V                                  |
663147Sxc151355  *         +----------------------------------+               |
673147Sxc151355  *         |[2]                               |               |
683147Sxc151355  *         |    Net80211 Callback functions   |               |
693147Sxc151355  *         |      registered by LLD           |               |
703147Sxc151355  *         +----------------------------------+               |
713147Sxc151355  *                         |                                  |
723147Sxc151355  *                         V                                  v
733147Sxc151355  *         +-----------------------------------------------------------+
743147Sxc151355  *         |[3]                                                        |
753147Sxc151355  *         |                LLD Internal functions                     |
763147Sxc151355  *         |                                                           |
773147Sxc151355  *         +-----------------------------------------------------------+
783147Sxc151355  *                                    ^
793147Sxc151355  *                                    | Software interrupt thread
803147Sxc151355  *                                    |
811000Sxc151355  *
821000Sxc151355  * The short description of each module is as below:
833147Sxc151355  *      Module 1: GLD callback functions, which are intercepting the calls from
843147Sxc151355  *                GLD to LLD.
853147Sxc151355  *      Module 2: Net80211 callback functions registered by LLD, which
863147Sxc151355  *                calls into LLD for H/W related functions needed by net80211.
873147Sxc151355  *      Module 3: LLD Internal functions, which are responsible for allocing
881000Sxc151355  *                descriptor/buffer, handling interrupt and other H/W
891000Sxc151355  *                operations.
901000Sxc151355  *
911000Sxc151355  * All functions are running in 3 types of thread:
921000Sxc151355  * 1. GLD callbacks threads, such as ioctl, intr, etc.
933147Sxc151355  * 2. Clock interruptt thread which is responsible for scan, rate control and
943147Sxc151355  *    calibration.
951000Sxc151355  * 3. Software Interrupt thread originated in LLD.
961000Sxc151355  *
971000Sxc151355  * The lock strategy is as below:
981000Sxc151355  * There have 4 queues for tx, each queue has one asc_txqlock[i] to
991000Sxc151355  *      prevent conflicts access to queue resource from different thread.
1001000Sxc151355  *
1011000Sxc151355  * All the transmit buffers are contained in asc_txbuf which are
1021000Sxc151355  *      protected by asc_txbuflock.
1031000Sxc151355  *
1041000Sxc151355  * Each receive buffers are contained in asc_rxbuf which are protected
1051000Sxc151355  *      by asc_rxbuflock.
1061000Sxc151355  *
1071000Sxc151355  * In ath struct, asc_genlock is a general lock, protecting most other
1081000Sxc151355  *      operational data in ath_softc struct and HAL accesses.
1091000Sxc151355  *      It is acquired by the interupt handler and most "mode-ctrl" routines.
1101000Sxc151355  *
1111000Sxc151355  * Any of the locks can be acquired singly, but where multiple
1121000Sxc151355  * locks are acquired, they *must* be in the order:
1133147Sxc151355  *    asc_genlock >> asc_txqlock[i] >> asc_txbuflock >> asc_rxbuflock
1141000Sxc151355  */
1151000Sxc151355 
1161000Sxc151355 #include <sys/param.h>
1171000Sxc151355 #include <sys/types.h>
1181000Sxc151355 #include <sys/signal.h>
1191000Sxc151355 #include <sys/stream.h>
1201000Sxc151355 #include <sys/termio.h>
1211000Sxc151355 #include <sys/errno.h>
1221000Sxc151355 #include <sys/file.h>
1231000Sxc151355 #include <sys/cmn_err.h>
1241000Sxc151355 #include <sys/stropts.h>
1251000Sxc151355 #include <sys/strsubr.h>
1261000Sxc151355 #include <sys/strtty.h>
1271000Sxc151355 #include <sys/kbio.h>
1281000Sxc151355 #include <sys/cred.h>
1291000Sxc151355 #include <sys/stat.h>
1301000Sxc151355 #include <sys/consdev.h>
1311000Sxc151355 #include <sys/kmem.h>
1321000Sxc151355 #include <sys/modctl.h>
1331000Sxc151355 #include <sys/ddi.h>
1341000Sxc151355 #include <sys/sunddi.h>
1351000Sxc151355 #include <sys/pci.h>
1361000Sxc151355 #include <sys/errno.h>
1373147Sxc151355 #include <sys/mac.h>
1381000Sxc151355 #include <sys/dlpi.h>
1391000Sxc151355 #include <sys/ethernet.h>
1401000Sxc151355 #include <sys/list.h>
1411000Sxc151355 #include <sys/byteorder.h>
1421000Sxc151355 #include <sys/strsun.h>
1431000Sxc151355 #include <sys/policy.h>
1441000Sxc151355 #include <inet/common.h>
1451000Sxc151355 #include <inet/nd.h>
1461000Sxc151355 #include <inet/mi.h>
1471000Sxc151355 #include <inet/wifi_ioctl.h>
1483147Sxc151355 #include <sys/mac_wifi.h>
1491000Sxc151355 #include "ath_hal.h"
1501000Sxc151355 #include "ath_impl.h"
1511000Sxc151355 #include "ath_aux.h"
1521000Sxc151355 #include "ath_rate.h"
1531000Sxc151355 
1543147Sxc151355 #define	ATH_MAX_RSSI	63	/* max rssi */
1553147Sxc151355 
1561000Sxc151355 extern void ath_halfix_init(void);
1571000Sxc151355 extern void ath_halfix_finit(void);
1581000Sxc151355 extern int32_t ath_getset(ath_t *asc, mblk_t *mp, uint32_t cmd);
1591000Sxc151355 
1601000Sxc151355 /*
1611000Sxc151355  * PIO access attributes for registers
1621000Sxc151355  */
1631000Sxc151355 static ddi_device_acc_attr_t ath_reg_accattr = {
1641000Sxc151355 	DDI_DEVICE_ATTR_V0,
1651000Sxc151355 	DDI_STRUCTURE_LE_ACC,
1661000Sxc151355 	DDI_STRICTORDER_ACC
1671000Sxc151355 };
1681000Sxc151355 
1691000Sxc151355 /*
1701000Sxc151355  * DMA access attributes for descriptors: NOT to be byte swapped.
1711000Sxc151355  */
1721000Sxc151355 static ddi_device_acc_attr_t ath_desc_accattr = {
1731000Sxc151355 	DDI_DEVICE_ATTR_V0,
1741000Sxc151355 	DDI_STRUCTURE_LE_ACC,
1751000Sxc151355 	DDI_STRICTORDER_ACC
1761000Sxc151355 };
1771000Sxc151355 
1781000Sxc151355 /*
1791000Sxc151355  * Describes the chip's DMA engine
1801000Sxc151355  */
181*6235Sxc151355 static ddi_dma_attr_t ath_dma_attr = {
182*6235Sxc151355 	DMA_ATTR_V0,		/* version number */
183*6235Sxc151355 	0,			/* low address */
184*6235Sxc151355 	0xffffffffU,		/* high address */
185*6235Sxc151355 	0x3ffffU,		/* counter register max */
186*6235Sxc151355 	1,			/* alignment */
187*6235Sxc151355 	0xFFF,			/* burst sizes */
188*6235Sxc151355 	1,			/* minimum transfer size */
189*6235Sxc151355 	0x3ffffU,		/* max transfer size */
190*6235Sxc151355 	0xffffffffU,		/* address register max */
191*6235Sxc151355 	1,			/* no scatter-gather */
192*6235Sxc151355 	1,			/* granularity of device */
193*6235Sxc151355 	0,			/* DMA flags */
194*6235Sxc151355 };
195*6235Sxc151355 
196*6235Sxc151355 static ddi_dma_attr_t ath_desc_dma_attr = {
197*6235Sxc151355 	DMA_ATTR_V0,		/* version number */
198*6235Sxc151355 	0,			/* low address */
199*6235Sxc151355 	0xffffffffU,		/* high address */
200*6235Sxc151355 	0xffffffffU,		/* counter register max */
201*6235Sxc151355 	0x1000,			/* alignment */
202*6235Sxc151355 	0xFFF,			/* burst sizes */
203*6235Sxc151355 	1,			/* minimum transfer size */
204*6235Sxc151355 	0xffffffffU,		/* max transfer size */
205*6235Sxc151355 	0xffffffffU,		/* address register max */
206*6235Sxc151355 	1,			/* no scatter-gather */
207*6235Sxc151355 	1,			/* granularity of device */
208*6235Sxc151355 	0,			/* DMA flags */
2091000Sxc151355 };
2101000Sxc151355 
2111000Sxc151355 static kmutex_t ath_loglock;
2121000Sxc151355 static void *ath_soft_state_p = NULL;
2133147Sxc151355 static int ath_dwelltime = 150;		/* scan interval, ms */
2143147Sxc151355 
2153147Sxc151355 static int	ath_m_stat(void *,  uint_t, uint64_t *);
2163147Sxc151355 static int	ath_m_start(void *);
2173147Sxc151355 static void	ath_m_stop(void *);
2183147Sxc151355 static int	ath_m_promisc(void *, boolean_t);
2193147Sxc151355 static int	ath_m_multicst(void *, boolean_t, const uint8_t *);
2203147Sxc151355 static int	ath_m_unicst(void *, const uint8_t *);
2213147Sxc151355 static mblk_t	*ath_m_tx(void *, mblk_t *);
2223147Sxc151355 static void	ath_m_ioctl(void *, queue_t *, mblk_t *);
2233147Sxc151355 static mac_callbacks_t ath_m_callbacks = {
2243147Sxc151355 	MC_IOCTL,
2253147Sxc151355 	ath_m_stat,
2263147Sxc151355 	ath_m_start,
2273147Sxc151355 	ath_m_stop,
2283147Sxc151355 	ath_m_promisc,
2293147Sxc151355 	ath_m_multicst,
2303147Sxc151355 	ath_m_unicst,
2313147Sxc151355 	ath_m_tx,
2323147Sxc151355 	NULL,		/* mc_resources; */
2333147Sxc151355 	ath_m_ioctl,
2343147Sxc151355 	NULL		/* mc_getcapab */
2353147Sxc151355 };
2361000Sxc151355 
2371000Sxc151355 /*
2381000Sxc151355  * Available debug flags:
2391000Sxc151355  * ATH_DBG_INIT, ATH_DBG_GLD, ATH_DBG_HAL, ATH_DBG_INT, ATH_DBG_ATTACH,
2401000Sxc151355  * ATH_DBG_DETACH, ATH_DBG_AUX, ATH_DBG_WIFICFG, ATH_DBG_OSDEP
2411000Sxc151355  */
2421000Sxc151355 uint32_t ath_dbg_flags = 0;
2431000Sxc151355 
2441000Sxc151355 /*
2451000Sxc151355  * Exception/warning cases not leading to panic.
2461000Sxc151355  */
2471000Sxc151355 void
2481000Sxc151355 ath_problem(const int8_t *fmt, ...)
2491000Sxc151355 {
2501000Sxc151355 	va_list args;
2511000Sxc151355 
2521000Sxc151355 	mutex_enter(&ath_loglock);
2531000Sxc151355 
2541000Sxc151355 	va_start(args, fmt);
2551000Sxc151355 	vcmn_err(CE_WARN, fmt, args);
2561000Sxc151355 	va_end(args);
2571000Sxc151355 
2581000Sxc151355 	mutex_exit(&ath_loglock);
2591000Sxc151355 }
2601000Sxc151355 
2611000Sxc151355 /*
2621000Sxc151355  * Normal log information independent of debug.
2631000Sxc151355  */
2641000Sxc151355 void
2651000Sxc151355 ath_log(const int8_t *fmt, ...)
2661000Sxc151355 {
2671000Sxc151355 	va_list args;
2681000Sxc151355 
2691000Sxc151355 	mutex_enter(&ath_loglock);
2701000Sxc151355 
2711000Sxc151355 	va_start(args, fmt);
2721000Sxc151355 	vcmn_err(CE_CONT, fmt, args);
2731000Sxc151355 	va_end(args);
2741000Sxc151355 
2751000Sxc151355 	mutex_exit(&ath_loglock);
2761000Sxc151355 }
2771000Sxc151355 
2781000Sxc151355 void
2791000Sxc151355 ath_dbg(uint32_t dbg_flags, const int8_t *fmt, ...)
2801000Sxc151355 {
2811000Sxc151355 	va_list args;
2821000Sxc151355 
2831000Sxc151355 	if (dbg_flags & ath_dbg_flags) {
2841000Sxc151355 		mutex_enter(&ath_loglock);
2851000Sxc151355 		va_start(args, fmt);
2861000Sxc151355 		vcmn_err(CE_CONT, fmt, args);
2871000Sxc151355 		va_end(args);
2881000Sxc151355 		mutex_exit(&ath_loglock);
2891000Sxc151355 	}
2901000Sxc151355 }
2911000Sxc151355 
2921000Sxc151355 void
2931000Sxc151355 ath_setup_desc(ath_t *asc, struct ath_buf *bf)
2941000Sxc151355 {
2951000Sxc151355 	struct ath_desc *ds;
2961000Sxc151355 
2971000Sxc151355 	ds = bf->bf_desc;
2981000Sxc151355 	ds->ds_link = bf->bf_daddr;
2991000Sxc151355 	ds->ds_data = bf->bf_dma.cookie.dmac_address;
3003147Sxc151355 	ds->ds_vdata = bf->bf_dma.mem_va;
3011000Sxc151355 	ATH_HAL_SETUPRXDESC(asc->asc_ah, ds,
3021000Sxc151355 	    bf->bf_dma.alength,		/* buffer size */
3031000Sxc151355 	    0);
3041000Sxc151355 
3051000Sxc151355 	if (asc->asc_rxlink != NULL)
3061000Sxc151355 		*asc->asc_rxlink = bf->bf_daddr;
3071000Sxc151355 	asc->asc_rxlink = &ds->ds_link;
3081000Sxc151355 }
3091000Sxc151355 
3101000Sxc151355 
3111000Sxc151355 /*
3121000Sxc151355  * Allocate an area of memory and a DMA handle for accessing it
3131000Sxc151355  */
3141000Sxc151355 static int
315*6235Sxc151355 ath_alloc_dma_mem(dev_info_t *devinfo, ddi_dma_attr_t *dma_attr, size_t memsize,
316*6235Sxc151355     ddi_device_acc_attr_t *attr_p, uint_t alloc_flags,
317*6235Sxc151355     uint_t bind_flags, dma_area_t *dma_p)
3181000Sxc151355 {
3191000Sxc151355 	int err;
3201000Sxc151355 
3211000Sxc151355 	/*
3221000Sxc151355 	 * Allocate handle
3231000Sxc151355 	 */
324*6235Sxc151355 	err = ddi_dma_alloc_handle(devinfo, dma_attr,
3255420Sxc151355 	    DDI_DMA_SLEEP, NULL, &dma_p->dma_hdl);
3261000Sxc151355 	if (err != DDI_SUCCESS)
3271000Sxc151355 		return (DDI_FAILURE);
3281000Sxc151355 
3291000Sxc151355 	/*
3301000Sxc151355 	 * Allocate memory
3311000Sxc151355 	 */
3321000Sxc151355 	err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, attr_p,
3331000Sxc151355 	    alloc_flags, DDI_DMA_SLEEP, NULL, &dma_p->mem_va,
3341000Sxc151355 	    &dma_p->alength, &dma_p->acc_hdl);
3351000Sxc151355 	if (err != DDI_SUCCESS)
3361000Sxc151355 		return (DDI_FAILURE);
3371000Sxc151355 
3381000Sxc151355 	/*
3391000Sxc151355 	 * Bind the two together
3401000Sxc151355 	 */
3411000Sxc151355 	err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
3425420Sxc151355 	    dma_p->mem_va, dma_p->alength, bind_flags,
3435420Sxc151355 	    DDI_DMA_SLEEP, NULL, &dma_p->cookie, &dma_p->ncookies);
3441000Sxc151355 	if (err != DDI_DMA_MAPPED)
3451000Sxc151355 		return (DDI_FAILURE);
3461000Sxc151355 
3471000Sxc151355 	dma_p->nslots = ~0U;
3481000Sxc151355 	dma_p->size = ~0U;
3491000Sxc151355 	dma_p->token = ~0U;
3501000Sxc151355 	dma_p->offset = 0;
3511000Sxc151355 	return (DDI_SUCCESS);
3521000Sxc151355 }
3531000Sxc151355 
3541000Sxc151355 /*
3551000Sxc151355  * Free one allocated area of DMAable memory
3561000Sxc151355  */
3571000Sxc151355 static void
3581000Sxc151355 ath_free_dma_mem(dma_area_t *dma_p)
3591000Sxc151355 {
3601000Sxc151355 	if (dma_p->dma_hdl != NULL) {
3611000Sxc151355 		(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
3621000Sxc151355 		if (dma_p->acc_hdl != NULL) {
3631000Sxc151355 			ddi_dma_mem_free(&dma_p->acc_hdl);
3641000Sxc151355 			dma_p->acc_hdl = NULL;
3651000Sxc151355 		}
3661000Sxc151355 		ddi_dma_free_handle(&dma_p->dma_hdl);
3671000Sxc151355 		dma_p->ncookies = 0;
3681000Sxc151355 		dma_p->dma_hdl = NULL;
3691000Sxc151355 	}
3701000Sxc151355 }
3711000Sxc151355 
3721000Sxc151355 
3731000Sxc151355 static int
3741000Sxc151355 ath_desc_alloc(dev_info_t *devinfo, ath_t *asc)
3751000Sxc151355 {
3761000Sxc151355 	int i, err;
3771000Sxc151355 	size_t size;
3781000Sxc151355 	struct ath_desc *ds;
3791000Sxc151355 	struct ath_buf *bf;
3801000Sxc151355 
3811000Sxc151355 	size = sizeof (struct ath_desc) * (ATH_TXBUF + ATH_RXBUF);
3821000Sxc151355 
383*6235Sxc151355 	err = ath_alloc_dma_mem(devinfo, &ath_desc_dma_attr, size,
384*6235Sxc151355 	    &ath_desc_accattr, DDI_DMA_CONSISTENT,
385*6235Sxc151355 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &asc->asc_desc_dma);
3861000Sxc151355 
3871000Sxc151355 	/* virtual address of the first descriptor */
3881000Sxc151355 	asc->asc_desc = (struct ath_desc *)asc->asc_desc_dma.mem_va;
3891000Sxc151355 
3901000Sxc151355 	ds = asc->asc_desc;
3911000Sxc151355 	ATH_DEBUG((ATH_DBG_INIT, "ath: ath_desc_alloc(): DMA map: "
3921000Sxc151355 	    "%p (%d) -> %p\n",
3931000Sxc151355 	    asc->asc_desc, asc->asc_desc_dma.alength,
3941000Sxc151355 	    asc->asc_desc_dma.cookie.dmac_address));
3951000Sxc151355 
3961000Sxc151355 	/* allocate data structures to describe TX/RX DMA buffers */
3971000Sxc151355 	asc->asc_vbuflen = sizeof (struct ath_buf) * (ATH_TXBUF + ATH_RXBUF);
3981000Sxc151355 	bf = (struct ath_buf *)kmem_zalloc(asc->asc_vbuflen, KM_SLEEP);
3991000Sxc151355 	asc->asc_vbufptr = bf;
4001000Sxc151355 
4011000Sxc151355 	/* DMA buffer size for each TX/RX packet */
4021000Sxc151355 	asc->asc_dmabuf_size = roundup(1000 + sizeof (struct ieee80211_frame) +
4031000Sxc151355 	    IEEE80211_MTU + IEEE80211_CRC_LEN +
4041000Sxc151355 	    (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
4051000Sxc151355 	    IEEE80211_WEP_CRCLEN), asc->asc_cachelsz);
4061000Sxc151355 
4071000Sxc151355 	/* create RX buffer list and allocate DMA memory */
4081000Sxc151355 	list_create(&asc->asc_rxbuf_list, sizeof (struct ath_buf),
4091000Sxc151355 	    offsetof(struct ath_buf, bf_node));
4101000Sxc151355 	for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
4111000Sxc151355 		bf->bf_desc = ds;
4121000Sxc151355 		bf->bf_daddr = asc->asc_desc_dma.cookie.dmac_address +
4131000Sxc151355 		    ((caddr_t)ds - (caddr_t)asc->asc_desc);
4141000Sxc151355 		list_insert_tail(&asc->asc_rxbuf_list, bf);
4151000Sxc151355 
4161000Sxc151355 		/* alloc DMA memory */
417*6235Sxc151355 		err = ath_alloc_dma_mem(devinfo, &ath_dma_attr,
418*6235Sxc151355 		    asc->asc_dmabuf_size, &ath_desc_accattr,
4191000Sxc151355 		    DDI_DMA_STREAMING, DDI_DMA_READ | DDI_DMA_STREAMING,
4201000Sxc151355 		    &bf->bf_dma);
4211000Sxc151355 		if (err != DDI_SUCCESS)
4221000Sxc151355 			return (err);
4231000Sxc151355 	}
4241000Sxc151355 
4251000Sxc151355 	/* create TX buffer list and allocate DMA memory */
4261000Sxc151355 	list_create(&asc->asc_txbuf_list, sizeof (struct ath_buf),
4271000Sxc151355 	    offsetof(struct ath_buf, bf_node));
4281000Sxc151355 	for (i = 0; i < ATH_TXBUF; i++, bf++, ds++) {
4291000Sxc151355 		bf->bf_desc = ds;
4301000Sxc151355 		bf->bf_daddr = asc->asc_desc_dma.cookie.dmac_address +
4311000Sxc151355 		    ((caddr_t)ds - (caddr_t)asc->asc_desc);
4321000Sxc151355 		list_insert_tail(&asc->asc_txbuf_list, bf);
4331000Sxc151355 
4341000Sxc151355 		/* alloc DMA memory */
435*6235Sxc151355 		err = ath_alloc_dma_mem(devinfo, &ath_dma_attr,
436*6235Sxc151355 		    asc->asc_dmabuf_size, &ath_desc_accattr,
4371000Sxc151355 		    DDI_DMA_STREAMING, DDI_DMA_STREAMING, &bf->bf_dma);
4381000Sxc151355 		if (err != DDI_SUCCESS)
4391000Sxc151355 			return (err);
4401000Sxc151355 	}
4411000Sxc151355 
4421000Sxc151355 	return (DDI_SUCCESS);
4431000Sxc151355 }
4441000Sxc151355 
4451000Sxc151355 static void
4461000Sxc151355 ath_desc_free(ath_t *asc)
4471000Sxc151355 {
4481000Sxc151355 	struct ath_buf *bf;
4491000Sxc151355 
4501000Sxc151355 	/* Free TX DMA buffer */
4511000Sxc151355 	bf = list_head(&asc->asc_txbuf_list);
4521000Sxc151355 	while (bf != NULL) {
4531000Sxc151355 		ath_free_dma_mem(&bf->bf_dma);
4541000Sxc151355 		list_remove(&asc->asc_txbuf_list, bf);
4551000Sxc151355 		bf = list_head(&asc->asc_txbuf_list);
4561000Sxc151355 	}
4571000Sxc151355 	list_destroy(&asc->asc_txbuf_list);
4581000Sxc151355 
4591000Sxc151355 	/* Free RX DMA uffer */
4601000Sxc151355 	bf = list_head(&asc->asc_rxbuf_list);
4611000Sxc151355 	while (bf != NULL) {
4621000Sxc151355 		ath_free_dma_mem(&bf->bf_dma);
4631000Sxc151355 		list_remove(&asc->asc_rxbuf_list, bf);
4641000Sxc151355 		bf = list_head(&asc->asc_rxbuf_list);
4651000Sxc151355 	}
4661000Sxc151355 	list_destroy(&asc->asc_rxbuf_list);
4671000Sxc151355 
4681000Sxc151355 	/* Free descriptor DMA buffer */
4691000Sxc151355 	ath_free_dma_mem(&asc->asc_desc_dma);
4701000Sxc151355 
4711000Sxc151355 	kmem_free((void *)asc->asc_vbufptr, asc->asc_vbuflen);
4721000Sxc151355 	asc->asc_vbufptr = NULL;
4731000Sxc151355 }
4741000Sxc151355 
4751000Sxc151355 static void
4761000Sxc151355 ath_printrxbuf(struct ath_buf *bf, int32_t done)
4771000Sxc151355 {
4781000Sxc151355 	struct ath_desc *ds = bf->bf_desc;
4791000Sxc151355 
4801000Sxc151355 	ATH_DEBUG((ATH_DBG_RECV, "ath: R (%p %p) %08x %08x %08x "
4811000Sxc151355 	    "%08x %08x %08x %c\n",
4821000Sxc151355 	    ds, bf->bf_daddr,
4831000Sxc151355 	    ds->ds_link, ds->ds_data,
4841000Sxc151355 	    ds->ds_ctl0, ds->ds_ctl1,
4851000Sxc151355 	    ds->ds_hw[0], ds->ds_hw[1],
4861000Sxc151355 	    !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!'));
4871000Sxc151355 }
4881000Sxc151355 
4891000Sxc151355 static void
4901000Sxc151355 ath_rx_handler(ath_t *asc)
4911000Sxc151355 {
4923147Sxc151355 	ieee80211com_t *ic = (ieee80211com_t *)asc;
4931000Sxc151355 	struct ath_buf *bf;
4941000Sxc151355 	struct ath_hal *ah = asc->asc_ah;
4951000Sxc151355 	struct ath_desc *ds;
4961000Sxc151355 	mblk_t *rx_mp;
4973147Sxc151355 	struct ieee80211_frame *wh;
4981000Sxc151355 	int32_t len, loop = 1;
4991000Sxc151355 	uint8_t phyerr;
5001000Sxc151355 	HAL_STATUS status;
5011000Sxc151355 	HAL_NODE_STATS hal_node_stats;
5023147Sxc151355 	struct ieee80211_node *in;
5031000Sxc151355 
5041000Sxc151355 	do {
5051000Sxc151355 		mutex_enter(&asc->asc_rxbuflock);
5061000Sxc151355 		bf = list_head(&asc->asc_rxbuf_list);
5071000Sxc151355 		if (bf == NULL) {
5081000Sxc151355 			ATH_DEBUG((ATH_DBG_RECV, "ath: ath_rx_handler(): "
5091000Sxc151355 			    "no buffer\n"));
5101000Sxc151355 			mutex_exit(&asc->asc_rxbuflock);
5111000Sxc151355 			break;
5121000Sxc151355 		}
5131000Sxc151355 		ASSERT(bf->bf_dma.cookie.dmac_address != NULL);
5141000Sxc151355 		ds = bf->bf_desc;
5151000Sxc151355 		if (ds->ds_link == bf->bf_daddr) {
5161000Sxc151355 			/*
5171000Sxc151355 			 * Never process the self-linked entry at the end,
5181000Sxc151355 			 * this may be met at heavy load.
5191000Sxc151355 			 */
5201000Sxc151355 			mutex_exit(&asc->asc_rxbuflock);
5211000Sxc151355 			break;
5221000Sxc151355 		}
5231000Sxc151355 
5241000Sxc151355 		status = ATH_HAL_RXPROCDESC(ah, ds,
5251000Sxc151355 		    bf->bf_daddr,
5261000Sxc151355 		    ATH_PA2DESC(asc, ds->ds_link));
5271000Sxc151355 		if (status == HAL_EINPROGRESS) {
5281000Sxc151355 			mutex_exit(&asc->asc_rxbuflock);
5291000Sxc151355 			break;
5301000Sxc151355 		}
5311000Sxc151355 		list_remove(&asc->asc_rxbuf_list, bf);
5321000Sxc151355 		mutex_exit(&asc->asc_rxbuflock);
5331000Sxc151355 
5341000Sxc151355 		if (ds->ds_rxstat.rs_status != 0) {
5351000Sxc151355 			if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
5361000Sxc151355 				asc->asc_stats.ast_rx_crcerr++;
5371000Sxc151355 			if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
5381000Sxc151355 				asc->asc_stats.ast_rx_fifoerr++;
5391000Sxc151355 			if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT)
5401000Sxc151355 				asc->asc_stats.ast_rx_badcrypt++;
5411000Sxc151355 			if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
5421000Sxc151355 				asc->asc_stats.ast_rx_phyerr++;
5431000Sxc151355 				phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
5441000Sxc151355 				asc->asc_stats.ast_rx_phy[phyerr]++;
5451000Sxc151355 			}
5461000Sxc151355 			goto rx_next;
5471000Sxc151355 		}
5481000Sxc151355 		len = ds->ds_rxstat.rs_datalen;
5491000Sxc151355 
5501000Sxc151355 		/* less than sizeof(struct ieee80211_frame) */
5511000Sxc151355 		if (len < 20) {
5521000Sxc151355 			asc->asc_stats.ast_rx_tooshort++;
5531000Sxc151355 			goto rx_next;
5541000Sxc151355 		}
5551000Sxc151355 
5561000Sxc151355 		if ((rx_mp = allocb(asc->asc_dmabuf_size, BPRI_MED)) == NULL) {
5571000Sxc151355 			ath_problem("ath: ath_rx_handler(): "
5581000Sxc151355 			    "allocing mblk buffer failed.\n");
5591000Sxc151355 			return;
5601000Sxc151355 		}
5611000Sxc151355 
5621000Sxc151355 		ATH_DMA_SYNC(bf->bf_dma, DDI_DMA_SYNC_FORCPU);
5631000Sxc151355 		bcopy(bf->bf_dma.mem_va, rx_mp->b_rptr, len);
5641000Sxc151355 
5651000Sxc151355 		rx_mp->b_wptr += len;
5661000Sxc151355 		wh = (struct ieee80211_frame *)rx_mp->b_rptr;
5673147Sxc151355 		if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
5681000Sxc151355 		    IEEE80211_FC0_TYPE_CTL) {
5691000Sxc151355 			/*
5701000Sxc151355 			 * Ignore control frame received in promisc mode.
5711000Sxc151355 			 */
5721000Sxc151355 			freemsg(rx_mp);
5731000Sxc151355 			goto rx_next;
5741000Sxc151355 		}
5751000Sxc151355 		/* Remove the CRC at the end of IEEE80211 frame */
5761000Sxc151355 		rx_mp->b_wptr -= IEEE80211_CRC_LEN;
5771000Sxc151355 #ifdef DEBUG
5781000Sxc151355 		ath_printrxbuf(bf, status == HAL_OK);
5791000Sxc151355 #endif /* DEBUG */
5803147Sxc151355 		/*
5813147Sxc151355 		 * Locate the node for sender, track state, and then
5823147Sxc151355 		 * pass the (referenced) node up to the 802.11 layer
5833147Sxc151355 		 * for its use.
5843147Sxc151355 		 */
5853147Sxc151355 		in = ieee80211_find_rxnode(ic, wh);
5863147Sxc151355 
5873147Sxc151355 		/*
5883147Sxc151355 		 * Send frame up for processing.
5893147Sxc151355 		 */
5903147Sxc151355 		(void) ieee80211_input(ic, rx_mp, in,
5911000Sxc151355 		    ds->ds_rxstat.rs_rssi,
5923147Sxc151355 		    ds->ds_rxstat.rs_tstamp);
5933147Sxc151355 
5943147Sxc151355 		ieee80211_free_node(in);
5953147Sxc151355 
5961000Sxc151355 rx_next:
5971000Sxc151355 		mutex_enter(&asc->asc_rxbuflock);
5981000Sxc151355 		list_insert_tail(&asc->asc_rxbuf_list, bf);
5991000Sxc151355 		mutex_exit(&asc->asc_rxbuflock);
6001000Sxc151355 		ath_setup_desc(asc, bf);
6011000Sxc151355 	} while (loop);
6021000Sxc151355 
6031000Sxc151355 	/* rx signal state monitoring */
6043147Sxc151355 	ATH_HAL_RXMONITOR(ah, &hal_node_stats, &asc->asc_curchan);
6051000Sxc151355 }
6061000Sxc151355 
6071000Sxc151355 static void
6081000Sxc151355 ath_printtxbuf(struct ath_buf *bf, int done)
6091000Sxc151355 {
6101000Sxc151355 	struct ath_desc *ds = bf->bf_desc;
6111000Sxc151355 
6121000Sxc151355 	ATH_DEBUG((ATH_DBG_SEND, "ath: T(%p %p) %08x %08x %08x %08x %08x"
6131000Sxc151355 	    " %08x %08x %08x %c\n",
6141000Sxc151355 	    ds, bf->bf_daddr,
6151000Sxc151355 	    ds->ds_link, ds->ds_data,
6161000Sxc151355 	    ds->ds_ctl0, ds->ds_ctl1,
6171000Sxc151355 	    ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
6181000Sxc151355 	    !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!'));
6191000Sxc151355 }
6201000Sxc151355 
6211000Sxc151355 /*
6221000Sxc151355  * The input parameter mp has following assumption:
6233147Sxc151355  * For data packets, GLDv3 mac_wifi plugin allocates and fills the
6243147Sxc151355  * ieee80211 header. For management packets, net80211 allocates and
6253147Sxc151355  * fills the ieee80211 header. In both cases, enough spaces in the
6263147Sxc151355  * header are left for encryption option.
6271000Sxc151355  */
6281000Sxc151355 static int32_t
6293147Sxc151355 ath_tx_start(ath_t *asc, struct ieee80211_node *in, struct ath_buf *bf,
6303147Sxc151355     mblk_t *mp)
6311000Sxc151355 {
6323147Sxc151355 	ieee80211com_t *ic = (ieee80211com_t *)asc;
6331000Sxc151355 	struct ieee80211_frame *wh;
6341000Sxc151355 	struct ath_hal *ah = asc->asc_ah;
6353147Sxc151355 	uint32_t subtype, flags, ctsduration;
6361000Sxc151355 	int32_t keyix, iswep, hdrlen, pktlen, mblen, mbslen, try0;
6373147Sxc151355 	uint8_t rix, cix, txrate, ctsrate;
6381000Sxc151355 	struct ath_desc *ds;
6391000Sxc151355 	struct ath_txq *txq;
6401000Sxc151355 	HAL_PKT_TYPE atype;
6411000Sxc151355 	const HAL_RATE_TABLE *rt;
6421000Sxc151355 	HAL_BOOL shortPreamble;
6431000Sxc151355 	struct ath_node *an;
6443147Sxc151355 	caddr_t dest;
6451000Sxc151355 
6461000Sxc151355 	/*
6471000Sxc151355 	 * CRC are added by H/W, not encaped by driver,
6481000Sxc151355 	 * but we must count it in pkt length.
6491000Sxc151355 	 */
6501000Sxc151355 	pktlen = IEEE80211_CRC_LEN;
6511000Sxc151355 
6523147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
6533147Sxc151355 	iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
6541000Sxc151355 	keyix = HAL_TXKEYIX_INVALID;
6551000Sxc151355 	hdrlen = sizeof (struct ieee80211_frame);
6563147Sxc151355 	if (iswep != 0) {
6573147Sxc151355 		const struct ieee80211_cipher *cip;
6583147Sxc151355 		struct ieee80211_key *k;
6591000Sxc151355 
6603147Sxc151355 		/*
6613147Sxc151355 		 * Construct the 802.11 header+trailer for an encrypted
6623147Sxc151355 		 * frame. The only reason this can fail is because of an
6633147Sxc151355 		 * unknown or unsupported cipher/key type.
6643147Sxc151355 		 */
6653147Sxc151355 		k = ieee80211_crypto_encap(ic, mp);
6663147Sxc151355 		if (k == NULL) {
6673147Sxc151355 			ATH_DEBUG((ATH_DBG_AUX, "crypto_encap failed\n"));
6683147Sxc151355 			/*
6693147Sxc151355 			 * This can happen when the key is yanked after the
6703147Sxc151355 			 * frame was queued.  Just discard the frame; the
6713147Sxc151355 			 * 802.11 layer counts failures and provides
6723147Sxc151355 			 * debugging/diagnostics.
6733147Sxc151355 			 */
6743147Sxc151355 			return (EIO);
6753147Sxc151355 		}
6763147Sxc151355 		cip = k->wk_cipher;
6771000Sxc151355 		/*
6783147Sxc151355 		 * Adjust the packet + header lengths for the crypto
6793147Sxc151355 		 * additions and calculate the h/w key index.  When
6803147Sxc151355 		 * a s/w mic is done the frame will have had any mic
6813147Sxc151355 		 * added to it prior to entry so m0->m_pkthdr.len above will
6823147Sxc151355 		 * account for it. Otherwise we need to add it to the
6833147Sxc151355 		 * packet length.
6841000Sxc151355 		 */
6853147Sxc151355 		hdrlen += cip->ic_header;
6864126Szf162725 		pktlen += cip->ic_trailer;
6873147Sxc151355 		if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0)
6883147Sxc151355 			pktlen += cip->ic_miclen;
6893147Sxc151355 		keyix = k->wk_keyix;
6901000Sxc151355 
6913147Sxc151355 		/* packet header may have moved, reset our local pointer */
6923147Sxc151355 		wh = (struct ieee80211_frame *)mp->b_rptr;
6931000Sxc151355 	}
6941000Sxc151355 
6953147Sxc151355 	dest = bf->bf_dma.mem_va;
6963147Sxc151355 	for (; mp != NULL; mp = mp->b_cont) {
6973147Sxc151355 		mblen = MBLKL(mp);
6983147Sxc151355 		bcopy(mp->b_rptr, dest, mblen);
6993147Sxc151355 		dest += mblen;
7003147Sxc151355 	}
7013147Sxc151355 	mbslen = dest - bf->bf_dma.mem_va;
7023147Sxc151355 	pktlen += mbslen;
7033147Sxc151355 
7041000Sxc151355 	bf->bf_in = in;
7051000Sxc151355 
7061000Sxc151355 	/* setup descriptors */
7071000Sxc151355 	ds = bf->bf_desc;
7081000Sxc151355 	rt = asc->asc_currates;
7093147Sxc151355 	ASSERT(rt != NULL);
7101000Sxc151355 
7111000Sxc151355 	/*
7121000Sxc151355 	 * The 802.11 layer marks whether or not we should
7131000Sxc151355 	 * use short preamble based on the current mode and
7141000Sxc151355 	 * negotiated parameters.
7151000Sxc151355 	 */
7163147Sxc151355 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
7171000Sxc151355 	    (in->in_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
7181000Sxc151355 		shortPreamble = AH_TRUE;
7191000Sxc151355 		asc->asc_stats.ast_tx_shortpre++;
7201000Sxc151355 	} else {
7211000Sxc151355 		shortPreamble = AH_FALSE;
7221000Sxc151355 	}
7231000Sxc151355 
7241000Sxc151355 	an = ATH_NODE(in);
7251000Sxc151355 
7261000Sxc151355 	/*
7271000Sxc151355 	 * Calculate Atheros packet type from IEEE80211 packet header
7281000Sxc151355 	 * and setup for rate calculations.
7291000Sxc151355 	 */
7303147Sxc151355 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
7311000Sxc151355 	case IEEE80211_FC0_TYPE_MGT:
7323147Sxc151355 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
7331000Sxc151355 		if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
7341000Sxc151355 			atype = HAL_PKT_TYPE_BEACON;
7351000Sxc151355 		else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
7361000Sxc151355 			atype = HAL_PKT_TYPE_PROBE_RESP;
7371000Sxc151355 		else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
7381000Sxc151355 			atype = HAL_PKT_TYPE_ATIM;
7391000Sxc151355 		else
7401000Sxc151355 			atype = HAL_PKT_TYPE_NORMAL;
7411000Sxc151355 		rix = 0;	/* lowest rate */
7421000Sxc151355 		try0 = ATH_TXMAXTRY;
7431000Sxc151355 		if (shortPreamble)
7441000Sxc151355 			txrate = an->an_tx_mgtratesp;
7451000Sxc151355 		else
7461000Sxc151355 			txrate = an->an_tx_mgtrate;
7471000Sxc151355 		/* force all ctl frames to highest queue */
7481000Sxc151355 		txq = asc->asc_ac2q[WME_AC_VO];
7491000Sxc151355 		break;
7501000Sxc151355 	case IEEE80211_FC0_TYPE_CTL:
7511000Sxc151355 		atype = HAL_PKT_TYPE_PSPOLL;
7523147Sxc151355 		subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
7531000Sxc151355 		rix = 0;	/* lowest rate */
7541000Sxc151355 		try0 = ATH_TXMAXTRY;
7551000Sxc151355 		if (shortPreamble)
7561000Sxc151355 			txrate = an->an_tx_mgtratesp;
7571000Sxc151355 		else
7581000Sxc151355 			txrate = an->an_tx_mgtrate;
7591000Sxc151355 		/* force all ctl frames to highest queue */
7601000Sxc151355 		txq = asc->asc_ac2q[WME_AC_VO];
7611000Sxc151355 		break;
7621000Sxc151355 	case IEEE80211_FC0_TYPE_DATA:
7631000Sxc151355 		atype = HAL_PKT_TYPE_NORMAL;
7641000Sxc151355 		rix = an->an_tx_rix0;
7651000Sxc151355 		try0 = an->an_tx_try0;
7661000Sxc151355 		if (shortPreamble)
7671000Sxc151355 			txrate = an->an_tx_rate0sp;
7681000Sxc151355 		else
7691000Sxc151355 			txrate = an->an_tx_rate0;
7701000Sxc151355 		/* Always use background queue */
7711000Sxc151355 		txq = asc->asc_ac2q[WME_AC_BK];
7721000Sxc151355 		break;
7731000Sxc151355 	default:
7741000Sxc151355 		/* Unknown 802.11 frame */
7751000Sxc151355 		asc->asc_stats.ast_tx_invalid++;
7761000Sxc151355 		return (1);
7771000Sxc151355 	}
7781000Sxc151355 	/*
7791000Sxc151355 	 * Calculate miscellaneous flags.
7801000Sxc151355 	 */
7811000Sxc151355 	flags = HAL_TXDESC_CLRDMASK;
7823147Sxc151355 	if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
7831000Sxc151355 		flags |= HAL_TXDESC_NOACK;	/* no ack on broad/multicast */
7841000Sxc151355 		asc->asc_stats.ast_tx_noack++;
7853147Sxc151355 	} else if (pktlen > ic->ic_rtsthreshold) {
7861000Sxc151355 		flags |= HAL_TXDESC_RTSENA;	/* RTS based on frame length */
7871000Sxc151355 		asc->asc_stats.ast_tx_rts++;
7881000Sxc151355 	}
7891000Sxc151355 
7901000Sxc151355 	/*
7911000Sxc151355 	 * Calculate duration.  This logically belongs in the 802.11
7921000Sxc151355 	 * layer but it lacks sufficient information to calculate it.
7931000Sxc151355 	 */
7941000Sxc151355 	if ((flags & HAL_TXDESC_NOACK) == 0 &&
7953147Sxc151355 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
7961000Sxc151355 	    IEEE80211_FC0_TYPE_CTL) {
7971000Sxc151355 		uint16_t dur;
7981000Sxc151355 		dur = ath_hal_computetxtime(ah, rt, IEEE80211_ACK_SIZE,
7991000Sxc151355 		    rix, shortPreamble);
8003147Sxc151355 		*(uint16_t *)wh->i_dur = LE_16(dur);
8011000Sxc151355 	}
8021000Sxc151355 
8031000Sxc151355 	/*
8041000Sxc151355 	 * Calculate RTS/CTS rate and duration if needed.
8051000Sxc151355 	 */
8061000Sxc151355 	ctsduration = 0;
8071000Sxc151355 	if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
8081000Sxc151355 		/*
8091000Sxc151355 		 * CTS transmit rate is derived from the transmit rate
8101000Sxc151355 		 * by looking in the h/w rate table.  We must also factor
8111000Sxc151355 		 * in whether or not a short preamble is to be used.
8121000Sxc151355 		 */
8131000Sxc151355 		cix = rt->info[rix].controlRate;
8141000Sxc151355 		ctsrate = rt->info[cix].rateCode;
8151000Sxc151355 		if (shortPreamble)
8161000Sxc151355 			ctsrate |= rt->info[cix].shortPreamble;
8171000Sxc151355 		/*
8181000Sxc151355 		 * Compute the transmit duration based on the size
8191000Sxc151355 		 * of an ACK frame.  We call into the HAL to do the
8201000Sxc151355 		 * computation since it depends on the characteristics
8211000Sxc151355 		 * of the actual PHY being used.
8221000Sxc151355 		 */
8231000Sxc151355 		if (flags & HAL_TXDESC_RTSENA) {	/* SIFS + CTS */
8241000Sxc151355 			ctsduration += ath_hal_computetxtime(ah,
8251000Sxc151355 			    rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
8261000Sxc151355 		}
8271000Sxc151355 		/* SIFS + data */
8281000Sxc151355 		ctsduration += ath_hal_computetxtime(ah,
8291000Sxc151355 		    rt, pktlen, rix, shortPreamble);
8301000Sxc151355 		if ((flags & HAL_TXDESC_NOACK) == 0) {	/* SIFS + ACK */
8311000Sxc151355 			ctsduration += ath_hal_computetxtime(ah,
8321000Sxc151355 			    rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
8331000Sxc151355 		}
8341000Sxc151355 	} else
8351000Sxc151355 		ctsrate = 0;
8361000Sxc151355 
8371000Sxc151355 	if (++txq->axq_intrcnt >= ATH_TXINTR_PERIOD) {
8381000Sxc151355 		flags |= HAL_TXDESC_INTREQ;
8391000Sxc151355 		txq->axq_intrcnt = 0;
8401000Sxc151355 	}
8411000Sxc151355 
8421000Sxc151355 	/*
8431000Sxc151355 	 * Formulate first tx descriptor with tx controls.
8441000Sxc151355 	 */
8451000Sxc151355 	ATH_HAL_SETUPTXDESC(ah, ds,
8461000Sxc151355 	    pktlen,			/* packet length */
8471000Sxc151355 	    hdrlen,			/* header length */
8481000Sxc151355 	    atype,			/* Atheros packet type */
8491000Sxc151355 	    MIN(in->in_txpower, 60),	/* txpower */
8501000Sxc151355 	    txrate, try0,		/* series 0 rate/tries */
8513147Sxc151355 	    keyix,			/* key cache index */
8523147Sxc151355 	    an->an_tx_antenna,		/* antenna mode */
8531000Sxc151355 	    flags,			/* flags */
8541000Sxc151355 	    ctsrate,			/* rts/cts rate */
8551000Sxc151355 	    ctsduration);		/* rts/cts duration */
8563147Sxc151355 	bf->bf_flags = flags;
8571000Sxc151355 
8581000Sxc151355 	ATH_DEBUG((ATH_DBG_SEND, "ath: ath_xmit(): to %s totlen=%d "
8591000Sxc151355 	    "an->an_tx_rate1sp=%d tx_rate2sp=%d tx_rate3sp=%d "
8601000Sxc151355 	    "qnum=%d rix=%d sht=%d dur = %d\n",
8613147Sxc151355 	    ieee80211_macaddr_sprintf(wh->i_addr1), mbslen, an->an_tx_rate1sp,
8621000Sxc151355 	    an->an_tx_rate2sp, an->an_tx_rate3sp,
8633147Sxc151355 	    txq->axq_qnum, rix, shortPreamble, *(uint16_t *)wh->i_dur));
8641000Sxc151355 
8651000Sxc151355 	/*
8661000Sxc151355 	 * Setup the multi-rate retry state only when we're
8671000Sxc151355 	 * going to use it.  This assumes ath_hal_setuptxdesc
8681000Sxc151355 	 * initializes the descriptors (so we don't have to)
8691000Sxc151355 	 * when the hardware supports multi-rate retry and
8701000Sxc151355 	 * we don't use it.
8711000Sxc151355 	 */
8721000Sxc151355 	if (try0 != ATH_TXMAXTRY)
8731000Sxc151355 		ATH_HAL_SETUPXTXDESC(ah, ds,
8741000Sxc151355 		    an->an_tx_rate1sp, 2,	/* series 1 */
8751000Sxc151355 		    an->an_tx_rate2sp, 2,	/* series 2 */
8761000Sxc151355 		    an->an_tx_rate3sp, 2);	/* series 3 */
8771000Sxc151355 
8781000Sxc151355 	ds->ds_link = 0;
8791000Sxc151355 	ds->ds_data = bf->bf_dma.cookie.dmac_address;
8801000Sxc151355 	ATH_HAL_FILLTXDESC(ah, ds,
8811000Sxc151355 	    mbslen,		/* segment length */
8821000Sxc151355 	    AH_TRUE,		/* first segment */
8831000Sxc151355 	    AH_TRUE,		/* last segment */
8841000Sxc151355 	    ds);		/* first descriptor */
8851000Sxc151355 
8861000Sxc151355 	ATH_DMA_SYNC(bf->bf_dma, DDI_DMA_SYNC_FORDEV);
8871000Sxc151355 
8881000Sxc151355 	mutex_enter(&txq->axq_lock);
8891000Sxc151355 	list_insert_tail(&txq->axq_list, bf);
8901000Sxc151355 	if (txq->axq_link == NULL) {
8911000Sxc151355 		ATH_HAL_PUTTXBUF(ah, txq->axq_qnum, bf->bf_daddr);
8921000Sxc151355 	} else {
8931000Sxc151355 		*txq->axq_link = bf->bf_daddr;
8941000Sxc151355 	}
8951000Sxc151355 	txq->axq_link = &ds->ds_link;
8961000Sxc151355 	mutex_exit(&txq->axq_lock);
8971000Sxc151355 
8981000Sxc151355 	ATH_HAL_TXSTART(ah, txq->axq_qnum);
8991000Sxc151355 
9003147Sxc151355 	ic->ic_stats.is_tx_frags++;
9013147Sxc151355 	ic->ic_stats.is_tx_bytes += pktlen;
9023147Sxc151355 
9031000Sxc151355 	return (0);
9041000Sxc151355 }
9051000Sxc151355 
9063147Sxc151355 /*
9073147Sxc151355  * Transmit a management frame.  On failure we reclaim the skbuff.
9083147Sxc151355  * Note that management frames come directly from the 802.11 layer
9093147Sxc151355  * and do not honor the send queue flow control.  Need to investigate
9103147Sxc151355  * using priority queueing so management frames can bypass data.
9113147Sxc151355  */
9121000Sxc151355 static int
9133147Sxc151355 ath_xmit(ieee80211com_t *ic, mblk_t *mp, uint8_t type)
9141000Sxc151355 {
9153147Sxc151355 	ath_t *asc = (ath_t *)ic;
9163147Sxc151355 	struct ath_hal *ah = asc->asc_ah;
9173147Sxc151355 	struct ieee80211_node *in = NULL;
9181000Sxc151355 	struct ath_buf *bf = NULL;
9193147Sxc151355 	struct ieee80211_frame *wh;
9203147Sxc151355 	int error = 0;
9213147Sxc151355 
9223147Sxc151355 	ASSERT(mp->b_next == NULL);
9233147Sxc151355 
9243147Sxc151355 	/* Grab a TX buffer */
9253147Sxc151355 	mutex_enter(&asc->asc_txbuflock);
9263147Sxc151355 	bf = list_head(&asc->asc_txbuf_list);
9273147Sxc151355 	if (bf != NULL)
9283147Sxc151355 		list_remove(&asc->asc_txbuf_list, bf);
9293147Sxc151355 	if (list_empty(&asc->asc_txbuf_list)) {
9303147Sxc151355 		ATH_DEBUG((ATH_DBG_SEND, "ath: ath_mgmt_send(): "
9313147Sxc151355 		    "stop queue\n"));
9323147Sxc151355 		asc->asc_stats.ast_tx_qstop++;
9333147Sxc151355 	}
9343147Sxc151355 	mutex_exit(&asc->asc_txbuflock);
9353147Sxc151355 	if (bf == NULL) {
9363147Sxc151355 		ATH_DEBUG((ATH_DBG_SEND, "ath: ath_mgmt_send(): discard, "
9373147Sxc151355 		    "no xmit buf\n"));
9383147Sxc151355 		ic->ic_stats.is_tx_nobuf++;
9393147Sxc151355 		if ((type & IEEE80211_FC0_TYPE_MASK) ==
9403147Sxc151355 		    IEEE80211_FC0_TYPE_DATA) {
9413147Sxc151355 			asc->asc_stats.ast_tx_nobuf++;
9423147Sxc151355 			mutex_enter(&asc->asc_resched_lock);
9433147Sxc151355 			asc->asc_resched_needed = B_TRUE;
9443147Sxc151355 			mutex_exit(&asc->asc_resched_lock);
9453147Sxc151355 		} else {
9463147Sxc151355 			asc->asc_stats.ast_tx_nobufmgt++;
9473147Sxc151355 			freemsg(mp);
9483147Sxc151355 		}
9493147Sxc151355 		return (ENOMEM);
9503147Sxc151355 	}
9513147Sxc151355 
9523147Sxc151355 	wh = (struct ieee80211_frame *)mp->b_rptr;
9533147Sxc151355 
9543147Sxc151355 	/* Locate node */
9553147Sxc151355 	in = ieee80211_find_txnode(ic,  wh->i_addr1);
9563147Sxc151355 	if (in == NULL) {
9573147Sxc151355 		error = EIO;
9583147Sxc151355 		goto bad;
9593147Sxc151355 	}
9603147Sxc151355 
9613147Sxc151355 	in->in_inact = 0;
9623147Sxc151355 	switch (type & IEEE80211_FC0_TYPE_MASK) {
9633147Sxc151355 	case IEEE80211_FC0_TYPE_DATA:
9643147Sxc151355 		(void) ieee80211_encap(ic, mp, in);
9653147Sxc151355 		break;
9663147Sxc151355 	default:
9673147Sxc151355 		if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
9683147Sxc151355 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
9693147Sxc151355 			/* fill time stamp */
9703147Sxc151355 			uint64_t tsf;
9713147Sxc151355 			uint32_t *tstamp;
9723147Sxc151355 
9733147Sxc151355 			tsf = ATH_HAL_GETTSF64(ah);
9743147Sxc151355 			/* adjust 100us delay to xmit */
9753147Sxc151355 			tsf += 100;
9763147Sxc151355 			tstamp = (uint32_t *)&wh[1];
9773147Sxc151355 			tstamp[0] = LE_32(tsf & 0xffffffff);
9783147Sxc151355 			tstamp[1] = LE_32(tsf >> 32);
9793147Sxc151355 		}
9803147Sxc151355 		asc->asc_stats.ast_tx_mgmt++;
9813147Sxc151355 		break;
9823147Sxc151355 	}
9833147Sxc151355 
9843147Sxc151355 	error = ath_tx_start(asc, in, bf, mp);
9853147Sxc151355 	if (error != 0) {
9863147Sxc151355 bad:
9873147Sxc151355 		ic->ic_stats.is_tx_failed++;
9883147Sxc151355 		if (bf != NULL) {
9893147Sxc151355 			mutex_enter(&asc->asc_txbuflock);
9903147Sxc151355 			list_insert_tail(&asc->asc_txbuf_list, bf);
9913147Sxc151355 			mutex_exit(&asc->asc_txbuflock);
9923147Sxc151355 		}
9933147Sxc151355 	}
9943147Sxc151355 	if (in != NULL)
9953147Sxc151355 		ieee80211_free_node(in);
9963147Sxc151355 	if ((type & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_DATA ||
9973147Sxc151355 	    error == 0) {
9983147Sxc151355 		freemsg(mp);
9993147Sxc151355 	}
10003147Sxc151355 
10013147Sxc151355 	return (error);
10023147Sxc151355 }
10033147Sxc151355 
10043147Sxc151355 static mblk_t *
10053147Sxc151355 ath_m_tx(void *arg, mblk_t *mp)
10063147Sxc151355 {
10073147Sxc151355 	ath_t *asc = arg;
10083147Sxc151355 	ieee80211com_t *ic = (ieee80211com_t *)asc;
10093147Sxc151355 	mblk_t *next;
10104126Szf162725 	int error = 0;
10111000Sxc151355 
10121000Sxc151355 	/*
10131000Sxc151355 	 * No data frames go out unless we're associated; this
10141000Sxc151355 	 * should not happen as the 802.11 layer does not enable
10151000Sxc151355 	 * the xmit queue until we enter the RUN state.
10161000Sxc151355 	 */
10173147Sxc151355 	if (ic->ic_state != IEEE80211_S_RUN) {
10183147Sxc151355 		ATH_DEBUG((ATH_DBG_SEND, "ath: ath_m_tx(): "
10193147Sxc151355 		    "discard, state %u\n", ic->ic_state));
10203631Sxh158540 		asc->asc_stats.ast_tx_discard++;
10213315Sxc151355 		freemsgchain(mp);
10223315Sxc151355 		return (NULL);
10231000Sxc151355 	}
10241000Sxc151355 
10253147Sxc151355 	while (mp != NULL) {
10263147Sxc151355 		next = mp->b_next;
10273147Sxc151355 		mp->b_next = NULL;
10284126Szf162725 		error = ath_xmit(ic, mp, IEEE80211_FC0_TYPE_DATA);
10294126Szf162725 		if (error != 0) {
10303147Sxc151355 			mp->b_next = next;
10314126Szf162725 			if (error == ENOMEM) {
10324126Szf162725 				break;
10334126Szf162725 			} else {
10344126Szf162725 				freemsgchain(mp);	/* CR6501759 issues */
10354126Szf162725 				return (NULL);
10364126Szf162725 			}
10373147Sxc151355 		}
10383147Sxc151355 		mp = next;
10391000Sxc151355 	}
10401000Sxc151355 
10413147Sxc151355 	return (mp);
10421000Sxc151355 
10431000Sxc151355 }
10441000Sxc151355 
10453147Sxc151355 static int
10461000Sxc151355 ath_tx_processq(ath_t *asc, struct ath_txq *txq)
10471000Sxc151355 {
10483147Sxc151355 	ieee80211com_t *ic = (ieee80211com_t *)asc;
10491000Sxc151355 	struct ath_hal *ah = asc->asc_ah;
10501000Sxc151355 	struct ath_buf *bf;
10511000Sxc151355 	struct ath_desc *ds;
10521000Sxc151355 	struct ieee80211_node *in;
10533147Sxc151355 	int32_t sr, lr, nacked = 0;
10541000Sxc151355 	HAL_STATUS status;
10551000Sxc151355 	struct ath_node *an;
10561000Sxc151355 
10571000Sxc151355 	for (;;) {
10581000Sxc151355 		mutex_enter(&txq->axq_lock);
10591000Sxc151355 		bf = list_head(&txq->axq_list);
10601000Sxc151355 		if (bf == NULL) {
10611000Sxc151355 			txq->axq_link = NULL;
10621000Sxc151355 			mutex_exit(&txq->axq_lock);
10631000Sxc151355 			break;
10641000Sxc151355 		}
10651000Sxc151355 		ds = bf->bf_desc;	/* last decriptor */
10661000Sxc151355 		status = ATH_HAL_TXPROCDESC(ah, ds);
10671000Sxc151355 #ifdef DEBUG
10681000Sxc151355 		ath_printtxbuf(bf, status == HAL_OK);
10691000Sxc151355 #endif
10701000Sxc151355 		if (status == HAL_EINPROGRESS) {
10711000Sxc151355 			mutex_exit(&txq->axq_lock);
10721000Sxc151355 			break;
10731000Sxc151355 		}
10741000Sxc151355 		list_remove(&txq->axq_list, bf);
10751000Sxc151355 		mutex_exit(&txq->axq_lock);
10761000Sxc151355 		in = bf->bf_in;
10771000Sxc151355 		if (in != NULL) {
10781000Sxc151355 			an = ATH_NODE(in);
10791000Sxc151355 			/* Successful transmition */
10801000Sxc151355 			if (ds->ds_txstat.ts_status == 0) {
10811000Sxc151355 				an->an_tx_ok++;
10821000Sxc151355 				an->an_tx_antenna =
10831000Sxc151355 				    ds->ds_txstat.ts_antenna;
10841000Sxc151355 				if (ds->ds_txstat.ts_rate &
10851000Sxc151355 				    HAL_TXSTAT_ALTRATE)
10861000Sxc151355 					asc->asc_stats.ast_tx_altrate++;
10871000Sxc151355 				asc->asc_stats.ast_tx_rssidelta =
10881000Sxc151355 				    ds->ds_txstat.ts_rssi -
10891000Sxc151355 				    asc->asc_stats.ast_tx_rssi;
10901000Sxc151355 				asc->asc_stats.ast_tx_rssi =
10911000Sxc151355 				    ds->ds_txstat.ts_rssi;
10921000Sxc151355 			} else {
10931000Sxc151355 				an->an_tx_err++;
10941000Sxc151355 				if (ds->ds_txstat.ts_status &
10951000Sxc151355 				    HAL_TXERR_XRETRY)
10961000Sxc151355 					asc->asc_stats.
10971000Sxc151355 					    ast_tx_xretries++;
10981000Sxc151355 				if (ds->ds_txstat.ts_status &
10991000Sxc151355 				    HAL_TXERR_FIFO)
11001000Sxc151355 					asc->asc_stats.ast_tx_fifoerr++;
11011000Sxc151355 				if (ds->ds_txstat.ts_status &
11021000Sxc151355 				    HAL_TXERR_FILT)
11031000Sxc151355 					asc->asc_stats.
11041000Sxc151355 					    ast_tx_filtered++;
11051000Sxc151355 				an->an_tx_antenna = 0;	/* invalidate */
11061000Sxc151355 			}
11071000Sxc151355 			sr = ds->ds_txstat.ts_shortretry;
11081000Sxc151355 			lr = ds->ds_txstat.ts_longretry;
11091000Sxc151355 			asc->asc_stats.ast_tx_shortretry += sr;
11101000Sxc151355 			asc->asc_stats.ast_tx_longretry += lr;
11113147Sxc151355 			/*
11123147Sxc151355 			 * Hand the descriptor to the rate control algorithm.
11133147Sxc151355 			 */
11143147Sxc151355 			if ((ds->ds_txstat.ts_status & HAL_TXERR_FILT) == 0 &&
11153147Sxc151355 			    (bf->bf_flags & HAL_TXDESC_NOACK) == 0) {
11163147Sxc151355 				/*
11173147Sxc151355 				 * If frame was ack'd update the last rx time
11183147Sxc151355 				 * used to workaround phantom bmiss interrupts.
11193147Sxc151355 				 */
11203147Sxc151355 				if (ds->ds_txstat.ts_status == 0) {
11213147Sxc151355 					nacked++;
11223147Sxc151355 					an->an_tx_ok++;
11233147Sxc151355 				} else {
11243147Sxc151355 					an->an_tx_err++;
11253147Sxc151355 				}
11263147Sxc151355 				an->an_tx_retr += sr + lr;
11273147Sxc151355 			}
11281000Sxc151355 		}
11291000Sxc151355 		bf->bf_in = NULL;
11301000Sxc151355 		mutex_enter(&asc->asc_txbuflock);
11311000Sxc151355 		list_insert_tail(&asc->asc_txbuf_list, bf);
11321000Sxc151355 		mutex_exit(&asc->asc_txbuflock);
11331000Sxc151355 		/*
11341000Sxc151355 		 * Reschedule stalled outbound packets
11351000Sxc151355 		 */
11363147Sxc151355 		mutex_enter(&asc->asc_resched_lock);
11373147Sxc151355 		if (asc->asc_resched_needed) {
11383147Sxc151355 			asc->asc_resched_needed = B_FALSE;
11393147Sxc151355 			mac_tx_update(ic->ic_mach);
11401000Sxc151355 		}
11413147Sxc151355 		mutex_exit(&asc->asc_resched_lock);
11421000Sxc151355 	}
11433147Sxc151355 	return (nacked);
11441000Sxc151355 }
11451000Sxc151355 
11461000Sxc151355 
11471000Sxc151355 static void
11481000Sxc151355 ath_tx_handler(ath_t *asc)
11491000Sxc151355 {
11501000Sxc151355 	int i;
11511000Sxc151355 
11521000Sxc151355 	/*
11531000Sxc151355 	 * Process each active queue.
11541000Sxc151355 	 */
11551000Sxc151355 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
11561000Sxc151355 		if (ATH_TXQ_SETUP(asc, i)) {
11573147Sxc151355 			(void) ath_tx_processq(asc, &asc->asc_txq[i]);
11581000Sxc151355 		}
11591000Sxc151355 	}
11601000Sxc151355 }
11611000Sxc151355 
11621000Sxc151355 static struct ieee80211_node *
11633147Sxc151355 ath_node_alloc(ieee80211com_t *ic)
11641000Sxc151355 {
11651000Sxc151355 	struct ath_node *an;
11663147Sxc151355 	ath_t *asc = (ath_t *)ic;
11671000Sxc151355 
11681000Sxc151355 	an = kmem_zalloc(sizeof (struct ath_node), KM_SLEEP);
11691000Sxc151355 	ath_rate_update(asc, &an->an_node, 0);
11701000Sxc151355 	return (&an->an_node);
11711000Sxc151355 }
11721000Sxc151355 
11731000Sxc151355 static void
11743147Sxc151355 ath_node_free(struct ieee80211_node *in)
11751000Sxc151355 {
11763147Sxc151355 	ieee80211com_t *ic = in->in_ic;
11773147Sxc151355 	ath_t *asc = (ath_t *)ic;
11781000Sxc151355 	struct ath_buf *bf;
11791000Sxc151355 	struct ath_txq *txq;
11801000Sxc151355 	int32_t i;
11811000Sxc151355 
11821000Sxc151355 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
11831000Sxc151355 		if (ATH_TXQ_SETUP(asc, i)) {
11841000Sxc151355 			txq = &asc->asc_txq[i];
11851000Sxc151355 			mutex_enter(&txq->axq_lock);
11861000Sxc151355 			bf = list_head(&txq->axq_list);
11871000Sxc151355 			while (bf != NULL) {
11881000Sxc151355 				if (bf->bf_in == in) {
11891000Sxc151355 					bf->bf_in = NULL;
11901000Sxc151355 				}
11911000Sxc151355 				bf = list_next(&txq->axq_list, bf);
11921000Sxc151355 			}
11931000Sxc151355 			mutex_exit(&txq->axq_lock);
11941000Sxc151355 		}
11951000Sxc151355 	}
11963147Sxc151355 	ic->ic_node_cleanup(in);
11974126Szf162725 	if (in->in_wpa_ie != NULL)
11984126Szf162725 		ieee80211_free(in->in_wpa_ie);
11991000Sxc151355 	kmem_free(in, sizeof (struct ath_node));
12001000Sxc151355 }
12011000Sxc151355 
12021000Sxc151355 static void
12033147Sxc151355 ath_next_scan(void *arg)
12041000Sxc151355 {
12053147Sxc151355 	ieee80211com_t *ic = arg;
12063147Sxc151355 	ath_t *asc = (ath_t *)ic;
12073147Sxc151355 
12083147Sxc151355 	asc->asc_scan_timer = 0;
12093147Sxc151355 	if (ic->ic_state == IEEE80211_S_SCAN) {
12103147Sxc151355 		asc->asc_scan_timer = timeout(ath_next_scan, (void *)asc,
12113147Sxc151355 		    drv_usectohz(ath_dwelltime * 1000));
12123147Sxc151355 		ieee80211_next_scan(ic);
12133147Sxc151355 	}
12141000Sxc151355 }
12151000Sxc151355 
12163147Sxc151355 static void
12173147Sxc151355 ath_stop_scantimer(ath_t *asc)
12181000Sxc151355 {
12193147Sxc151355 	timeout_id_t tmp_id = 0;
12201000Sxc151355 
12213147Sxc151355 	while ((asc->asc_scan_timer != 0) && (tmp_id != asc->asc_scan_timer)) {
12223147Sxc151355 		tmp_id = asc->asc_scan_timer;
12233147Sxc151355 		(void) untimeout(tmp_id);
12241000Sxc151355 	}
12253147Sxc151355 	asc->asc_scan_timer = 0;
12261000Sxc151355 }
12271000Sxc151355 
12281000Sxc151355 static int32_t
12293147Sxc151355 ath_newstate(ieee80211com_t *ic, enum ieee80211_state nstate, int arg)
12301000Sxc151355 {
12313147Sxc151355 	ath_t *asc = (ath_t *)ic;
12321000Sxc151355 	struct ath_hal *ah = asc->asc_ah;
12331000Sxc151355 	struct ieee80211_node *in;
12341000Sxc151355 	int32_t i, error;
12351000Sxc151355 	uint8_t *bssid;
12361000Sxc151355 	uint32_t rfilt;
12371000Sxc151355 	enum ieee80211_state ostate;
12381000Sxc151355 
12391000Sxc151355 	static const HAL_LED_STATE leds[] = {
12401000Sxc151355 	    HAL_LED_INIT,	/* IEEE80211_S_INIT */
12411000Sxc151355 	    HAL_LED_SCAN,	/* IEEE80211_S_SCAN */
12421000Sxc151355 	    HAL_LED_AUTH,	/* IEEE80211_S_AUTH */
12431000Sxc151355 	    HAL_LED_ASSOC, 	/* IEEE80211_S_ASSOC */
12441000Sxc151355 	    HAL_LED_RUN, 	/* IEEE80211_S_RUN */
12451000Sxc151355 	};
12463147Sxc151355 	if (!ATH_IS_RUNNING(asc))
12471000Sxc151355 		return (0);
12481000Sxc151355 
12493147Sxc151355 	ostate = ic->ic_state;
12503147Sxc151355 	if (nstate != IEEE80211_S_SCAN)
12513147Sxc151355 		ath_stop_scantimer(asc);
12521000Sxc151355 
12533147Sxc151355 	ATH_LOCK(asc);
12541000Sxc151355 	ATH_HAL_SETLEDSTATE(ah, leds[nstate]);	/* set LED */
12551000Sxc151355 
12561000Sxc151355 	if (nstate == IEEE80211_S_INIT) {
12571000Sxc151355 		asc->asc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
12583147Sxc151355 		ATH_HAL_INTRSET(ah, asc->asc_imask &~ HAL_INT_GLOBAL);
12593147Sxc151355 		ATH_UNLOCK(asc);
12603147Sxc151355 		goto done;
12611000Sxc151355 	}
12623147Sxc151355 	in = ic->ic_bss;
12633147Sxc151355 	error = ath_chan_set(asc, ic->ic_curchan);
12643147Sxc151355 	if (error != 0) {
12653147Sxc151355 		if (nstate != IEEE80211_S_SCAN) {
12663147Sxc151355 			ATH_UNLOCK(asc);
12673147Sxc151355 			ieee80211_reset_chan(ic);
12683147Sxc151355 			goto bad;
12693147Sxc151355 		}
12703147Sxc151355 	}
12711000Sxc151355 
12721000Sxc151355 	rfilt = ath_calcrxfilter(asc);
12731000Sxc151355 	if (nstate == IEEE80211_S_SCAN)
12743147Sxc151355 		bssid = ic->ic_macaddr;
12751000Sxc151355 	else
12761000Sxc151355 		bssid = in->in_bssid;
12771000Sxc151355 	ATH_HAL_SETRXFILTER(ah, rfilt);
12781000Sxc151355 
12793147Sxc151355 	if (nstate == IEEE80211_S_RUN && ic->ic_opmode != IEEE80211_M_IBSS)
12801000Sxc151355 		ATH_HAL_SETASSOCID(ah, bssid, in->in_associd);
12811000Sxc151355 	else
12821000Sxc151355 		ATH_HAL_SETASSOCID(ah, bssid, 0);
12833147Sxc151355 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
12841000Sxc151355 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
12851000Sxc151355 			if (ATH_HAL_KEYISVALID(ah, i))
12861000Sxc151355 				ATH_HAL_KEYSETMAC(ah, i, bssid);
12871000Sxc151355 		}
12881000Sxc151355 	}
12891000Sxc151355 
12901000Sxc151355 	if ((nstate == IEEE80211_S_RUN) &&
12911000Sxc151355 	    (ostate != IEEE80211_S_RUN)) {
12921000Sxc151355 		/* Configure the beacon and sleep timers. */
12931000Sxc151355 		ath_beacon_config(asc);
12941000Sxc151355 	} else {
12951000Sxc151355 		asc->asc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
12961000Sxc151355 		ATH_HAL_INTRSET(ah, asc->asc_imask);
12971000Sxc151355 	}
12981000Sxc151355 	/*
12991000Sxc151355 	 * Reset the rate control state.
13001000Sxc151355 	 */
13011000Sxc151355 	ath_rate_ctl_reset(asc, nstate);
13021000Sxc151355 
13033147Sxc151355 	if (nstate == IEEE80211_S_RUN && (ostate != IEEE80211_S_RUN)) {
13041000Sxc151355 		nvlist_t *attr_list = NULL;
13051000Sxc151355 		sysevent_id_t eid;
13061000Sxc151355 		int32_t err = 0;
13071000Sxc151355 		char *str_name = "ATH";
13081000Sxc151355 		char str_value[256] = {0};
13091000Sxc151355 
13101000Sxc151355 		ATH_DEBUG((ATH_DBG_80211, "ath: ath new state(RUN): "
13111000Sxc151355 		    "ic_flags=0x%08x iv=%d"
13121000Sxc151355 		    " bssid=%s capinfo=0x%04x chan=%d\n",
13133147Sxc151355 		    ic->ic_flags,
13141000Sxc151355 		    in->in_intval,
13153147Sxc151355 		    ieee80211_macaddr_sprintf(in->in_bssid),
13161000Sxc151355 		    in->in_capinfo,
13173147Sxc151355 		    ieee80211_chan2ieee(ic, in->in_chan)));
13181000Sxc151355 
13191000Sxc151355 		(void) sprintf(str_value, "%s%s%d", "-i ",
13201000Sxc151355 		    ddi_driver_name(asc->asc_dev),
13211000Sxc151355 		    ddi_get_instance(asc->asc_dev));
13221000Sxc151355 		if (nvlist_alloc(&attr_list,
13231000Sxc151355 		    NV_UNIQUE_NAME_TYPE, KM_SLEEP) == 0) {
13241000Sxc151355 			err = nvlist_add_string(attr_list,
13251000Sxc151355 			    str_name, str_value);
13261000Sxc151355 			if (err != DDI_SUCCESS)
13271000Sxc151355 				ATH_DEBUG((ATH_DBG_80211, "ath: "
13281000Sxc151355 				    "ath_new_state: error log event\n"));
13291000Sxc151355 			err = ddi_log_sysevent(asc->asc_dev,
13301000Sxc151355 			    DDI_VENDOR_SUNW, "class",
13311000Sxc151355 			    "subclass", attr_list,
13321000Sxc151355 			    &eid, DDI_NOSLEEP);
13331000Sxc151355 			if (err != DDI_SUCCESS)
13341000Sxc151355 				ATH_DEBUG((ATH_DBG_80211, "ath: "
13351000Sxc151355 				    "ath_new_state(): error log event\n"));
13361000Sxc151355 			nvlist_free(attr_list);
13371000Sxc151355 		}
13381000Sxc151355 	}
13391000Sxc151355 
13403147Sxc151355 	ATH_UNLOCK(asc);
13413147Sxc151355 done:
13423147Sxc151355 	/*
13433147Sxc151355 	 * Invoke the parent method to complete the work.
13443147Sxc151355 	 */
13453147Sxc151355 	error = asc->asc_newstate(ic, nstate, arg);
13463147Sxc151355 	/*
13473147Sxc151355 	 * Finally, start any timers.
13483147Sxc151355 	 */
13493147Sxc151355 	if (nstate == IEEE80211_S_RUN) {
13503147Sxc151355 		ieee80211_start_watchdog(ic, 1);
13513147Sxc151355 	} else if ((nstate == IEEE80211_S_SCAN) && (ostate != nstate)) {
13523147Sxc151355 		/* start ap/neighbor scan timer */
13533147Sxc151355 		ASSERT(asc->asc_scan_timer == 0);
13543147Sxc151355 		asc->asc_scan_timer = timeout(ath_next_scan, (void *)asc,
13553147Sxc151355 		    drv_usectohz(ath_dwelltime * 1000));
13563147Sxc151355 	}
13571000Sxc151355 bad:
13581000Sxc151355 	return (error);
13591000Sxc151355 }
13601000Sxc151355 
13611000Sxc151355 /*
13621000Sxc151355  * Periodically recalibrate the PHY to account
13631000Sxc151355  * for temperature/environment changes.
13641000Sxc151355  */
13651000Sxc151355 static void
13663147Sxc151355 ath_calibrate(ath_t *asc)
13671000Sxc151355 {
13681000Sxc151355 	struct ath_hal *ah = asc->asc_ah;
13693147Sxc151355 	HAL_BOOL iqcaldone;
13701000Sxc151355 
13711000Sxc151355 	asc->asc_stats.ast_per_cal++;
13721000Sxc151355 
13731000Sxc151355 	if (ATH_HAL_GETRFGAIN(ah) == HAL_RFGAIN_NEED_CHANGE) {
13741000Sxc151355 		/*
13751000Sxc151355 		 * Rfgain is out of bounds, reset the chip
13761000Sxc151355 		 * to load new gain values.
13771000Sxc151355 		 */
13781000Sxc151355 		ATH_DEBUG((ATH_DBG_HAL, "ath: ath_calibrate(): "
13791000Sxc151355 		    "Need change RFgain\n"));
13801000Sxc151355 		asc->asc_stats.ast_per_rfgain++;
13813147Sxc151355 		(void) ath_reset(&asc->asc_isc);
13821000Sxc151355 	}
13833147Sxc151355 	if (!ATH_HAL_CALIBRATE(ah, &asc->asc_curchan, &iqcaldone)) {
13841000Sxc151355 		ATH_DEBUG((ATH_DBG_HAL, "ath: ath_calibrate(): "
13851000Sxc151355 		    "calibration of channel %u failed\n",
13863147Sxc151355 		    asc->asc_curchan.channel));
13871000Sxc151355 		asc->asc_stats.ast_per_calfail++;
13881000Sxc151355 	}
13891000Sxc151355 }
13901000Sxc151355 
13913147Sxc151355 static void
13923147Sxc151355 ath_watchdog(void *arg)
13933147Sxc151355 {
13943147Sxc151355 	ath_t *asc = arg;
13953147Sxc151355 	ieee80211com_t *ic = &asc->asc_isc;
13963147Sxc151355 	int ntimer = 0;
13973147Sxc151355 
13983147Sxc151355 	ATH_LOCK(asc);
13993147Sxc151355 	ic->ic_watchdog_timer = 0;
14003147Sxc151355 	if (!ATH_IS_RUNNING(asc)) {
14013147Sxc151355 		ATH_UNLOCK(asc);
14023147Sxc151355 		return;
14033147Sxc151355 	}
14043147Sxc151355 
14053147Sxc151355 	if (ic->ic_state == IEEE80211_S_RUN) {
14063147Sxc151355 		/* periodic recalibration */
14073147Sxc151355 		ath_calibrate(asc);
14083147Sxc151355 
14093147Sxc151355 		/*
14103147Sxc151355 		 * Start the background rate control thread if we
14113147Sxc151355 		 * are not configured to use a fixed xmit rate.
14123147Sxc151355 		 */
14133147Sxc151355 		if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
14143147Sxc151355 			asc->asc_stats.ast_rate_calls ++;
14153147Sxc151355 			if (ic->ic_opmode == IEEE80211_M_STA)
14163147Sxc151355 				ath_rate_ctl(ic, ic->ic_bss);
14173147Sxc151355 			else
14183147Sxc151355 				ieee80211_iterate_nodes(&ic->ic_sta,
14193147Sxc151355 				    ath_rate_cb, asc);
14203147Sxc151355 		}
14213147Sxc151355 
14223147Sxc151355 		ntimer = 1;
14233147Sxc151355 	}
14243147Sxc151355 	ATH_UNLOCK(asc);
14253147Sxc151355 
14263147Sxc151355 	ieee80211_watchdog(ic);
14273147Sxc151355 	if (ntimer != 0)
14283147Sxc151355 		ieee80211_start_watchdog(ic, ntimer);
14293147Sxc151355 }
14303147Sxc151355 
14311000Sxc151355 static uint_t
14323147Sxc151355 ath_intr(caddr_t arg)
14331000Sxc151355 {
14343147Sxc151355 	ath_t *asc = (ath_t *)arg;
14351000Sxc151355 	struct ath_hal *ah = asc->asc_ah;
14361000Sxc151355 	HAL_INT status;
14373147Sxc151355 	ieee80211com_t *ic = (ieee80211com_t *)asc;
14383147Sxc151355 
14393147Sxc151355 	ATH_LOCK(asc);
14401000Sxc151355 
14413147Sxc151355 	if (!ATH_IS_RUNNING(asc)) {
14423147Sxc151355 		/*
14433147Sxc151355 		 * The hardware is not ready/present, don't touch anything.
14443147Sxc151355 		 * Note this can happen early on if the IRQ is shared.
14453147Sxc151355 		 */
14463147Sxc151355 		ATH_UNLOCK(asc);
14473147Sxc151355 		return (DDI_INTR_UNCLAIMED);
14483147Sxc151355 	}
14491000Sxc151355 
14501000Sxc151355 	if (!ATH_HAL_INTRPEND(ah)) {	/* shared irq, not for us */
14513147Sxc151355 		ATH_UNLOCK(asc);
14521000Sxc151355 		return (DDI_INTR_UNCLAIMED);
14531000Sxc151355 	}
14541000Sxc151355 
14551000Sxc151355 	ATH_HAL_GETISR(ah, &status);
14561000Sxc151355 	status &= asc->asc_imask;
14571000Sxc151355 	if (status & HAL_INT_FATAL) {
14581000Sxc151355 		asc->asc_stats.ast_hardware++;
14591000Sxc151355 		goto reset;
14601000Sxc151355 	} else if (status & HAL_INT_RXORN) {
14611000Sxc151355 		asc->asc_stats.ast_rxorn++;
14621000Sxc151355 		goto reset;
14631000Sxc151355 	} else {
14641000Sxc151355 		if (status & HAL_INT_RXEOL) {
14651000Sxc151355 			asc->asc_stats.ast_rxeol++;
14661000Sxc151355 			asc->asc_rxlink = NULL;
14671000Sxc151355 		}
14681000Sxc151355 		if (status & HAL_INT_TXURN) {
14691000Sxc151355 			asc->asc_stats.ast_txurn++;
14701000Sxc151355 			ATH_HAL_UPDATETXTRIGLEVEL(ah, AH_TRUE);
14711000Sxc151355 		}
14723147Sxc151355 
14731000Sxc151355 		if (status & HAL_INT_RX) {
14741000Sxc151355 			asc->asc_rx_pend = 1;
14751000Sxc151355 			ddi_trigger_softintr(asc->asc_softint_id);
14761000Sxc151355 		}
14771000Sxc151355 		if (status & HAL_INT_TX) {
14781000Sxc151355 			ath_tx_handler(asc);
14791000Sxc151355 		}
14803147Sxc151355 		ATH_UNLOCK(asc);
14811000Sxc151355 
14821000Sxc151355 		if (status & HAL_INT_SWBA) {
14831000Sxc151355 			/* This will occur only in Host-AP or Ad-Hoc mode */
14841000Sxc151355 			return (DDI_INTR_CLAIMED);
14851000Sxc151355 		}
14861000Sxc151355 		if (status & HAL_INT_BMISS) {
14873147Sxc151355 			if (ic->ic_state == IEEE80211_S_RUN) {
14883147Sxc151355 				(void) ieee80211_new_state(ic,
14891000Sxc151355 				    IEEE80211_S_ASSOC, -1);
14901000Sxc151355 			}
14911000Sxc151355 		}
14921000Sxc151355 	}
14931000Sxc151355 
14941000Sxc151355 	return (DDI_INTR_CLAIMED);
14951000Sxc151355 reset:
14963147Sxc151355 	(void) ath_reset(ic);
14973147Sxc151355 	ATH_UNLOCK(asc);
14981000Sxc151355 	return (DDI_INTR_CLAIMED);
14991000Sxc151355 }
15001000Sxc151355 
15011000Sxc151355 static uint_t
15021000Sxc151355 ath_softint_handler(caddr_t data)
15031000Sxc151355 {
15041000Sxc151355 	ath_t *asc = (ath_t *)data;
15051000Sxc151355 
15061000Sxc151355 	/*
15071000Sxc151355 	 * Check if the soft interrupt is triggered by another
15081000Sxc151355 	 * driver at the same level.
15091000Sxc151355 	 */
15103147Sxc151355 	ATH_LOCK(asc);
15111000Sxc151355 	if (asc->asc_rx_pend) { /* Soft interrupt for this driver */
15121000Sxc151355 		asc->asc_rx_pend = 0;
15133147Sxc151355 		ATH_UNLOCK(asc);
15143147Sxc151355 		ath_rx_handler(asc);
15151000Sxc151355 		return (DDI_INTR_CLAIMED);
15161000Sxc151355 	}
15173147Sxc151355 	ATH_UNLOCK(asc);
15181000Sxc151355 	return (DDI_INTR_UNCLAIMED);
15191000Sxc151355 }
15201000Sxc151355 
15211000Sxc151355 /*
15221000Sxc151355  * following are gld callback routine
15231000Sxc151355  * ath_gld_send, ath_gld_ioctl, ath_gld_gstat
15241000Sxc151355  * are listed in other corresponding sections.
15251000Sxc151355  * reset the hardware w/o losing operational state.  this is
15261000Sxc151355  * basically a more efficient way of doing ath_gld_stop, ath_gld_start,
15271000Sxc151355  * followed by state transitions to the current 802.11
15281000Sxc151355  * operational state.  used to recover from errors rx overrun
15291000Sxc151355  * and to reset the hardware when rf gain settings must be reset.
15301000Sxc151355  */
15311000Sxc151355 
15323147Sxc151355 static void
15333147Sxc151355 ath_stop_locked(ath_t *asc)
15341000Sxc151355 {
15353147Sxc151355 	ieee80211com_t *ic = (ieee80211com_t *)asc;
15363147Sxc151355 	struct ath_hal *ah = asc->asc_ah;
15371000Sxc151355 
15383147Sxc151355 	ATH_LOCK_ASSERT(asc);
15393147Sxc151355 	/*
15403147Sxc151355 	 * Shutdown the hardware and driver:
15413147Sxc151355 	 *    reset 802.11 state machine
15423147Sxc151355 	 *    turn off timers
15433147Sxc151355 	 *    disable interrupts
15443147Sxc151355 	 *    turn off the radio
15453147Sxc151355 	 *    clear transmit machinery
15463147Sxc151355 	 *    clear receive machinery
15473147Sxc151355 	 *    drain and release tx queues
15483147Sxc151355 	 *    reclaim beacon resources
15493147Sxc151355 	 *    power down hardware
15503147Sxc151355 	 *
15513147Sxc151355 	 * Note that some of this work is not possible if the
15523147Sxc151355 	 * hardware is gone (invalid).
15533147Sxc151355 	 */
15543147Sxc151355 	ATH_UNLOCK(asc);
15553147Sxc151355 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
15563147Sxc151355 	ieee80211_stop_watchdog(ic);
15573147Sxc151355 	ATH_LOCK(asc);
15583147Sxc151355 	ATH_HAL_INTRSET(ah, 0);
15593147Sxc151355 	ath_draintxq(asc);
15603147Sxc151355 	if (ATH_IS_RUNNING(asc)) {
15613147Sxc151355 		ath_stoprecv(asc);
15623147Sxc151355 		ATH_HAL_PHYDISABLE(ah);
15633147Sxc151355 	} else {
15643147Sxc151355 		asc->asc_rxlink = NULL;
15653147Sxc151355 	}
15661000Sxc151355 }
15671000Sxc151355 
15683147Sxc151355 static void
15693147Sxc151355 ath_m_stop(void *arg)
15701000Sxc151355 {
15713147Sxc151355 	ath_t *asc = arg;
15721000Sxc151355 	struct ath_hal *ah = asc->asc_ah;
15731000Sxc151355 
15743147Sxc151355 	ATH_LOCK(asc);
15753147Sxc151355 	ath_stop_locked(asc);
15763147Sxc151355 	ATH_HAL_SETPOWER(ah, HAL_PM_AWAKE);
15771000Sxc151355 	asc->asc_invalid = 1;
15783147Sxc151355 	ATH_UNLOCK(asc);
15791000Sxc151355 }
15801000Sxc151355 
15811000Sxc151355 int
15823147Sxc151355 ath_m_start(void *arg)
15831000Sxc151355 {
15843147Sxc151355 	ath_t *asc = arg;
15853147Sxc151355 	ieee80211com_t *ic = (ieee80211com_t *)asc;
15861000Sxc151355 	struct ath_hal *ah = asc->asc_ah;
15871000Sxc151355 	HAL_STATUS status;
15881000Sxc151355 
15893147Sxc151355 	ATH_LOCK(asc);
15901000Sxc151355 	/*
15911000Sxc151355 	 * Stop anything previously setup.  This is safe
15921000Sxc151355 	 * whether this is the first time through or not.
15931000Sxc151355 	 */
15943147Sxc151355 	ath_stop_locked(asc);
15951000Sxc151355 
15961000Sxc151355 	/*
15971000Sxc151355 	 * The basic interface to setting the hardware in a good
15981000Sxc151355 	 * state is ``reset''.  On return the hardware is known to
15991000Sxc151355 	 * be powered up and with interrupts disabled.  This must
16001000Sxc151355 	 * be followed by initialization of the appropriate bits
16011000Sxc151355 	 * and then setup of the interrupt mask.
16021000Sxc151355 	 */
16033147Sxc151355 	asc->asc_curchan.channel = ic->ic_curchan->ich_freq;
16043147Sxc151355 	asc->asc_curchan.channelFlags = ath_chan2flags(ic, ic->ic_curchan);
16053147Sxc151355 	if (!ATH_HAL_RESET(ah, (HAL_OPMODE)ic->ic_opmode,
16063147Sxc151355 	    &asc->asc_curchan, AH_FALSE, &status)) {
16073147Sxc151355 		ATH_DEBUG((ATH_DBG_HAL, "ath: ath_m_start(): "
1608*6235Sxc151355 		    "reset hardware failed: '%s' (HAL status %u)\n",
1609*6235Sxc151355 		    ath_get_hal_status_desc(status), status));
16103147Sxc151355 		ATH_UNLOCK(asc);
16113147Sxc151355 		return (ENOTACTIVE);
16121000Sxc151355 	}
16131000Sxc151355 
16143147Sxc151355 	(void) ath_startrecv(asc);
16151000Sxc151355 
16161000Sxc151355 	/*
16171000Sxc151355 	 * Enable interrupts.
16181000Sxc151355 	 */
16191000Sxc151355 	asc->asc_imask = HAL_INT_RX | HAL_INT_TX
16201000Sxc151355 	    | HAL_INT_RXEOL | HAL_INT_RXORN
16211000Sxc151355 	    | HAL_INT_FATAL | HAL_INT_GLOBAL;
16221000Sxc151355 	ATH_HAL_INTRSET(ah, asc->asc_imask);
16231000Sxc151355 
16243147Sxc151355 	ic->ic_state = IEEE80211_S_INIT;
16251000Sxc151355 
16261000Sxc151355 	/*
16271000Sxc151355 	 * The hardware should be ready to go now so it's safe
16281000Sxc151355 	 * to kick the 802.11 state machine as it's likely to
16291000Sxc151355 	 * immediately call back to us to send mgmt frames.
16301000Sxc151355 	 */
16313147Sxc151355 	ath_chan_change(asc, ic->ic_curchan);
16321000Sxc151355 	asc->asc_invalid = 0;
16333147Sxc151355 	ATH_UNLOCK(asc);
16343147Sxc151355 	return (0);
16351000Sxc151355 }
16361000Sxc151355 
16371000Sxc151355 
16383147Sxc151355 static int
16393147Sxc151355 ath_m_unicst(void *arg, const uint8_t *macaddr)
16401000Sxc151355 {
16413147Sxc151355 	ath_t *asc = arg;
16421000Sxc151355 	struct ath_hal *ah = asc->asc_ah;
16431000Sxc151355 
16441000Sxc151355 	ATH_DEBUG((ATH_DBG_GLD, "ath: ath_gld_saddr(): "
16451000Sxc151355 	    "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
16461000Sxc151355 	    macaddr[0], macaddr[1], macaddr[2],
16471000Sxc151355 	    macaddr[3], macaddr[4], macaddr[5]));
16481000Sxc151355 
16493147Sxc151355 	ATH_LOCK(asc);
16503147Sxc151355 	IEEE80211_ADDR_COPY(asc->asc_isc.ic_macaddr, macaddr);
16513147Sxc151355 	ATH_HAL_SETMAC(ah, asc->asc_isc.ic_macaddr);
16521000Sxc151355 
16533147Sxc151355 	(void) ath_reset(&asc->asc_isc);
16543147Sxc151355 	ATH_UNLOCK(asc);
16553147Sxc151355 	return (0);
16561000Sxc151355 }
16571000Sxc151355 
16581000Sxc151355 static int
16593147Sxc151355 ath_m_promisc(void *arg, boolean_t on)
16601000Sxc151355 {
16613147Sxc151355 	ath_t *asc = arg;
16621000Sxc151355 	struct ath_hal *ah = asc->asc_ah;
16631000Sxc151355 	uint32_t rfilt;
16641000Sxc151355 
16653147Sxc151355 	ATH_LOCK(asc);
16661000Sxc151355 	rfilt = ATH_HAL_GETRXFILTER(ah);
16673147Sxc151355 	if (on)
16683147Sxc151355 		rfilt |= HAL_RX_FILTER_PROM;
16693147Sxc151355 	else
16701000Sxc151355 		rfilt &= ~HAL_RX_FILTER_PROM;
1671*6235Sxc151355 	asc->asc_promisc = on;
16723147Sxc151355 	ATH_HAL_SETRXFILTER(ah, rfilt);
16733147Sxc151355 	ATH_UNLOCK(asc);
16741000Sxc151355 
16753147Sxc151355 	return (0);
16761000Sxc151355 }
16771000Sxc151355 
16781000Sxc151355 static int
16793147Sxc151355 ath_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
16801000Sxc151355 {
16813147Sxc151355 	ath_t *asc = arg;
16823147Sxc151355 	struct ath_hal *ah = asc->asc_ah;
1683*6235Sxc151355 	uint32_t val, index, bit;
16841000Sxc151355 	uint8_t pos;
1685*6235Sxc151355 	uint32_t *mfilt = asc->asc_mcast_hash;
16861000Sxc151355 
16873147Sxc151355 	ATH_LOCK(asc);
16881000Sxc151355 	/* calculate XOR of eight 6bit values */
16891000Sxc151355 	val = ATH_LE_READ_4(mca + 0);
16901000Sxc151355 	pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
16911000Sxc151355 	val = ATH_LE_READ_4(mca + 3);
16921000Sxc151355 	pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
16931000Sxc151355 	pos &= 0x3f;
1694*6235Sxc151355 	index = pos / 32;
1695*6235Sxc151355 	bit = 1 << (pos % 32);
1696*6235Sxc151355 
1697*6235Sxc151355 	if (add) {	/* enable multicast */
1698*6235Sxc151355 		asc->asc_mcast_refs[pos]++;
1699*6235Sxc151355 		mfilt[index] |= bit;
1700*6235Sxc151355 	} else {	/* disable multicast */
1701*6235Sxc151355 		if (--asc->asc_mcast_refs[pos] == 0)
1702*6235Sxc151355 			mfilt[index] &= ~bit;
1703*6235Sxc151355 	}
17041000Sxc151355 	ATH_HAL_SETMCASTFILTER(ah, mfilt[0], mfilt[1]);
17051000Sxc151355 
17063147Sxc151355 	ATH_UNLOCK(asc);
17073147Sxc151355 	return (0);
17081000Sxc151355 }
17091000Sxc151355 
17101000Sxc151355 static void
17113147Sxc151355 ath_m_ioctl(void *arg, queue_t *wq, mblk_t *mp)
17121000Sxc151355 {
17133147Sxc151355 	ath_t *asc = arg;
17143147Sxc151355 	int32_t err;
17151000Sxc151355 
17163147Sxc151355 	err = ieee80211_ioctl(&asc->asc_isc, wq, mp);
17173147Sxc151355 	ATH_LOCK(asc);
17183147Sxc151355 	if (err == ENETRESET) {
17193147Sxc151355 		if (ATH_IS_RUNNING(asc)) {
17203147Sxc151355 			ATH_UNLOCK(asc);
17213147Sxc151355 			(void) ath_m_start(asc);
17223147Sxc151355 			(void) ieee80211_new_state(&asc->asc_isc,
17233147Sxc151355 			    IEEE80211_S_SCAN, -1);
17243147Sxc151355 			ATH_LOCK(asc);
17253147Sxc151355 		}
17261000Sxc151355 	}
17273147Sxc151355 	ATH_UNLOCK(asc);
17281000Sxc151355 }
17291000Sxc151355 
17301000Sxc151355 static int
17313147Sxc151355 ath_m_stat(void *arg, uint_t stat, uint64_t *val)
17321000Sxc151355 {
17333147Sxc151355 	ath_t *asc = arg;
17343147Sxc151355 	ieee80211com_t *ic = (ieee80211com_t *)asc;
17353147Sxc151355 	struct ieee80211_node *in = ic->ic_bss;
17361000Sxc151355 	struct ieee80211_rateset *rs = &in->in_rates;
17371000Sxc151355 
17383147Sxc151355 	ATH_LOCK(asc);
17393147Sxc151355 	switch (stat) {
17403147Sxc151355 	case MAC_STAT_IFSPEED:
17413147Sxc151355 		*val = (rs->ir_rates[in->in_txrate] & IEEE80211_RATE_VAL) / 2 *
17423147Sxc151355 		    1000000ull;
17433147Sxc151355 		break;
17443147Sxc151355 	case MAC_STAT_NOXMTBUF:
17453147Sxc151355 		*val = asc->asc_stats.ast_tx_nobuf +
17463147Sxc151355 		    asc->asc_stats.ast_tx_nobufmgt;
17473147Sxc151355 		break;
17483147Sxc151355 	case MAC_STAT_IERRORS:
17493147Sxc151355 		*val = asc->asc_stats.ast_rx_tooshort;
17503147Sxc151355 		break;
17513147Sxc151355 	case MAC_STAT_RBYTES:
17523147Sxc151355 		*val = ic->ic_stats.is_rx_bytes;
17533147Sxc151355 		break;
17543147Sxc151355 	case MAC_STAT_IPACKETS:
17553147Sxc151355 		*val = ic->ic_stats.is_rx_frags;
17563147Sxc151355 		break;
17573147Sxc151355 	case MAC_STAT_OBYTES:
17583147Sxc151355 		*val = ic->ic_stats.is_tx_bytes;
17593147Sxc151355 		break;
17603147Sxc151355 	case MAC_STAT_OPACKETS:
17613147Sxc151355 		*val = ic->ic_stats.is_tx_frags;
17623147Sxc151355 		break;
17633631Sxh158540 	case MAC_STAT_OERRORS:
17643147Sxc151355 	case WIFI_STAT_TX_FAILED:
17653147Sxc151355 		*val = asc->asc_stats.ast_tx_fifoerr +
17663631Sxh158540 		    asc->asc_stats.ast_tx_xretries +
17673631Sxh158540 		    asc->asc_stats.ast_tx_discard;
17683147Sxc151355 		break;
17693147Sxc151355 	case WIFI_STAT_TX_RETRANS:
17703147Sxc151355 		*val = asc->asc_stats.ast_tx_xretries;
17713147Sxc151355 		break;
17723147Sxc151355 	case WIFI_STAT_FCS_ERRORS:
17733147Sxc151355 		*val = asc->asc_stats.ast_rx_crcerr;
17743147Sxc151355 		break;
17753147Sxc151355 	case WIFI_STAT_WEP_ERRORS:
17763147Sxc151355 		*val = asc->asc_stats.ast_rx_badcrypt;
17773147Sxc151355 		break;
17783147Sxc151355 	case WIFI_STAT_TX_FRAGS:
17793147Sxc151355 	case WIFI_STAT_MCAST_TX:
17803147Sxc151355 	case WIFI_STAT_RTS_SUCCESS:
17813147Sxc151355 	case WIFI_STAT_RTS_FAILURE:
17823147Sxc151355 	case WIFI_STAT_ACK_FAILURE:
17833147Sxc151355 	case WIFI_STAT_RX_FRAGS:
17843147Sxc151355 	case WIFI_STAT_MCAST_RX:
17853147Sxc151355 	case WIFI_STAT_RX_DUPS:
17863147Sxc151355 		ATH_UNLOCK(asc);
17873147Sxc151355 		return (ieee80211_stat(ic, stat, val));
17883147Sxc151355 	default:
17893147Sxc151355 		ATH_UNLOCK(asc);
17903147Sxc151355 		return (ENOTSUP);
17913147Sxc151355 	}
17923147Sxc151355 	ATH_UNLOCK(asc);
17931000Sxc151355 
17943147Sxc151355 	return (0);
17951000Sxc151355 }
17961000Sxc151355 
17971000Sxc151355 static int
17981000Sxc151355 ath_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
17991000Sxc151355 {
18001000Sxc151355 	ath_t *asc;
18013147Sxc151355 	ieee80211com_t *ic;
18021000Sxc151355 	struct ath_hal *ah;
18031000Sxc151355 	uint8_t csz;
18041000Sxc151355 	HAL_STATUS status;
18051000Sxc151355 	caddr_t regs;
18061000Sxc151355 	uint32_t i, val;
18071000Sxc151355 	uint16_t vendor_id, device_id, command;
18081000Sxc151355 	const char *athname;
18091000Sxc151355 	int32_t ath_countrycode = CTRY_DEFAULT;	/* country code */
18101000Sxc151355 	int32_t err, ath_regdomain = 0; /* regulatory domain */
18111000Sxc151355 	char strbuf[32];
18123147Sxc151355 	int instance;
18133147Sxc151355 	wifi_data_t wd = { 0 };
18143147Sxc151355 	mac_register_t *macp;
18151000Sxc151355 
18163147Sxc151355 	if (cmd != DDI_ATTACH)
18171000Sxc151355 		return (DDI_FAILURE);
18181000Sxc151355 
18193147Sxc151355 	instance = ddi_get_instance(devinfo);
18203147Sxc151355 	if (ddi_soft_state_zalloc(ath_soft_state_p, instance) != DDI_SUCCESS) {
18211000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
18221000Sxc151355 		    "Unable to alloc softstate\n"));
18231000Sxc151355 		return (DDI_FAILURE);
18241000Sxc151355 	}
18251000Sxc151355 
18261000Sxc151355 	asc = ddi_get_soft_state(ath_soft_state_p, ddi_get_instance(devinfo));
18273147Sxc151355 	ic = (ieee80211com_t *)asc;
18281000Sxc151355 	asc->asc_dev = devinfo;
18291000Sxc151355 
18301000Sxc151355 	mutex_init(&asc->asc_genlock, NULL, MUTEX_DRIVER, NULL);
18311000Sxc151355 	mutex_init(&asc->asc_txbuflock, NULL, MUTEX_DRIVER, NULL);
18321000Sxc151355 	mutex_init(&asc->asc_rxbuflock, NULL, MUTEX_DRIVER, NULL);
18333147Sxc151355 	mutex_init(&asc->asc_resched_lock, NULL, MUTEX_DRIVER, NULL);
18341000Sxc151355 
18351000Sxc151355 	err = pci_config_setup(devinfo, &asc->asc_cfg_handle);
18361000Sxc151355 	if (err != DDI_SUCCESS) {
18371000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
18381000Sxc151355 		    "pci_config_setup() failed"));
18391000Sxc151355 		goto attach_fail0;
18401000Sxc151355 	}
18411000Sxc151355 
18425420Sxc151355 	/*
18435420Sxc151355 	 * Cache line size is used to size and align various
18445420Sxc151355 	 * structures used to communicate with the hardware.
18455420Sxc151355 	 */
18461000Sxc151355 	csz = pci_config_get8(asc->asc_cfg_handle, PCI_CONF_CACHE_LINESZ);
18475420Sxc151355 	if (csz == 0) {
18485420Sxc151355 		/*
18495420Sxc151355 		 * We must have this setup properly for rx buffer
18505420Sxc151355 		 * DMA to work so force a reasonable value here if it
18515420Sxc151355 		 * comes up zero.
18525420Sxc151355 		 */
18535420Sxc151355 		csz = ATH_DEF_CACHE_BYTES / sizeof (uint32_t);
18545420Sxc151355 		pci_config_put8(asc->asc_cfg_handle, PCI_CONF_CACHE_LINESZ,
18555420Sxc151355 		    csz);
18565420Sxc151355 	}
18571000Sxc151355 	asc->asc_cachelsz = csz << 2;
18581000Sxc151355 	vendor_id = pci_config_get16(asc->asc_cfg_handle, PCI_CONF_VENID);
18591000Sxc151355 	device_id = pci_config_get16(asc->asc_cfg_handle, PCI_CONF_DEVID);
18601000Sxc151355 	ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): vendor 0x%x, "
18611000Sxc151355 	    "device id 0x%x, cache size %d\n", vendor_id, device_id, csz));
18621000Sxc151355 
18631000Sxc151355 	athname = ath_hal_probe(vendor_id, device_id);
18641000Sxc151355 	ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): athname: %s\n",
18651000Sxc151355 	    athname ? athname : "Atheros ???"));
18661000Sxc151355 
18671000Sxc151355 	/*
18681000Sxc151355 	 * Enable response to memory space accesses,
18691000Sxc151355 	 * and enabe bus master.
18701000Sxc151355 	 */
18711000Sxc151355 	command = PCI_COMM_MAE | PCI_COMM_ME;
18721000Sxc151355 	pci_config_put16(asc->asc_cfg_handle, PCI_CONF_COMM, command);
18731000Sxc151355 	ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
18741000Sxc151355 	    "set command reg to 0x%x \n", command));
18751000Sxc151355 
18761000Sxc151355 	pci_config_put8(asc->asc_cfg_handle, PCI_CONF_LATENCY_TIMER, 0xa8);
18771000Sxc151355 	val = pci_config_get32(asc->asc_cfg_handle, 0x40);
18781000Sxc151355 	if ((val & 0x0000ff00) != 0)
18791000Sxc151355 		pci_config_put32(asc->asc_cfg_handle, 0x40, val & 0xffff00ff);
18801000Sxc151355 
18811000Sxc151355 	err = ddi_regs_map_setup(devinfo, 1,
18821000Sxc151355 	    &regs, 0, 0, &ath_reg_accattr, &asc->asc_io_handle);
18831000Sxc151355 	ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
18841000Sxc151355 	    "regs map1 = %x err=%d\n", regs, err));
18851000Sxc151355 	if (err != DDI_SUCCESS) {
18861000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
18871000Sxc151355 		    "ddi_regs_map_setup() failed"));
18881000Sxc151355 		goto attach_fail1;
18891000Sxc151355 	}
18901000Sxc151355 
18911000Sxc151355 	ah = ath_hal_attach(device_id, asc, 0, regs, &status);
18921000Sxc151355 	if (ah == NULL) {
18931000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
1894*6235Sxc151355 		    "unable to attach hw: '%s' (HAL status %u)\n",
1895*6235Sxc151355 		    ath_get_hal_status_desc(status), status));
18961000Sxc151355 		goto attach_fail2;
18971000Sxc151355 	}
18981000Sxc151355 	ATH_HAL_INTRSET(ah, 0);
18991000Sxc151355 	asc->asc_ah = ah;
19001000Sxc151355 
19011000Sxc151355 	if (ah->ah_abi != HAL_ABI_VERSION) {
19021000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
19031000Sxc151355 		    "HAL ABI mismatch detected (0x%x != 0x%x)\n",
19041000Sxc151355 		    ah->ah_abi, HAL_ABI_VERSION));
19051000Sxc151355 		goto attach_fail3;
19061000Sxc151355 	}
19071000Sxc151355 
19081000Sxc151355 	ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
19091000Sxc151355 	    "HAL ABI version 0x%x\n", ah->ah_abi));
19101000Sxc151355 	ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
19111000Sxc151355 	    "HAL mac version %d.%d, phy version %d.%d\n",
19121000Sxc151355 	    ah->ah_macVersion, ah->ah_macRev,
19131000Sxc151355 	    ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf));
19141000Sxc151355 	if (ah->ah_analog5GhzRev)
19151000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
19161000Sxc151355 		    "HAL 5ghz radio version %d.%d\n",
19171000Sxc151355 		    ah->ah_analog5GhzRev >> 4,
19181000Sxc151355 		    ah->ah_analog5GhzRev & 0xf));
19191000Sxc151355 	if (ah->ah_analog2GhzRev)
19201000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
19211000Sxc151355 		    "HAL 2ghz radio version %d.%d\n",
19221000Sxc151355 		    ah->ah_analog2GhzRev >> 4,
19231000Sxc151355 		    ah->ah_analog2GhzRev & 0xf));
19241000Sxc151355 
19251000Sxc151355 	/*
19261000Sxc151355 	 * Check if the MAC has multi-rate retry support.
19271000Sxc151355 	 * We do this by trying to setup a fake extended
19281000Sxc151355 	 * descriptor.  MAC's that don't have support will
19291000Sxc151355 	 * return false w/o doing anything.  MAC's that do
19301000Sxc151355 	 * support it will return true w/o doing anything.
19311000Sxc151355 	 */
19321000Sxc151355 	asc->asc_mrretry = ATH_HAL_SETUPXTXDESC(ah, NULL, 0, 0, 0, 0, 0, 0);
19331000Sxc151355 	ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
19341000Sxc151355 	    "multi rate retry support=%x\n",
19351000Sxc151355 	    asc->asc_mrretry));
19361000Sxc151355 
19374126Szf162725 	/*
19384126Szf162725 	 * Get the hardware key cache size.
19394126Szf162725 	 */
19404126Szf162725 	asc->asc_keymax = ATH_HAL_KEYCACHESIZE(ah);
19414126Szf162725 	if (asc->asc_keymax > sizeof (asc->asc_keymap) * NBBY) {
19424126Szf162725 		ATH_DEBUG((ATH_DBG_ATTACH, "ath_attach:"
19434126Szf162725 		    " Warning, using only %u entries in %u key cache\n",
19444126Szf162725 		    sizeof (asc->asc_keymap) * NBBY, asc->asc_keymax));
19454126Szf162725 		asc->asc_keymax = sizeof (asc->asc_keymap) * NBBY;
19464126Szf162725 	}
19474126Szf162725 	/*
19484126Szf162725 	 * Reset the key cache since some parts do not
19494126Szf162725 	 * reset the contents on initial power up.
19504126Szf162725 	 */
19514126Szf162725 	for (i = 0; i < asc->asc_keymax; i++)
19524126Szf162725 		ATH_HAL_KEYRESET(ah, i);
19534126Szf162725 
19544126Szf162725 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
19554126Szf162725 		setbit(asc->asc_keymap, i);
19564126Szf162725 		setbit(asc->asc_keymap, i+32);
19574126Szf162725 		setbit(asc->asc_keymap, i+64);
19584126Szf162725 		setbit(asc->asc_keymap, i+32+64);
19594126Szf162725 	}
19604126Szf162725 
19611000Sxc151355 	ATH_HAL_GETREGDOMAIN(ah, (uint32_t *)&ath_regdomain);
19621000Sxc151355 	ATH_HAL_GETCOUNTRYCODE(ah, &ath_countrycode);
19631000Sxc151355 	/*
19641000Sxc151355 	 * Collect the channel list using the default country
19651000Sxc151355 	 * code and including outdoor channels.  The 802.11 layer
19661000Sxc151355 	 * is resposible for filtering this list to a set of
19671000Sxc151355 	 * channels that it considers ok to use.
19681000Sxc151355 	 */
19691000Sxc151355 	asc->asc_have11g = 0;
19701000Sxc151355 
19711000Sxc151355 	/* enable outdoor use, enable extended channels */
19721000Sxc151355 	err = ath_getchannels(asc, ath_countrycode, AH_FALSE, AH_TRUE);
19731000Sxc151355 	if (err != 0)
19741000Sxc151355 		goto attach_fail3;
19751000Sxc151355 
19761000Sxc151355 	/*
19771000Sxc151355 	 * Setup rate tables for all potential media types.
19781000Sxc151355 	 */
19791000Sxc151355 	ath_rate_setup(asc, IEEE80211_MODE_11A);
19801000Sxc151355 	ath_rate_setup(asc, IEEE80211_MODE_11B);
19811000Sxc151355 	ath_rate_setup(asc, IEEE80211_MODE_11G);
19823147Sxc151355 	ath_rate_setup(asc, IEEE80211_MODE_TURBO_A);
19831000Sxc151355 
19841000Sxc151355 	/* Setup here so ath_rate_update is happy */
19851000Sxc151355 	ath_setcurmode(asc, IEEE80211_MODE_11A);
19861000Sxc151355 
19871000Sxc151355 	err = ath_desc_alloc(devinfo, asc);
19881000Sxc151355 	if (err != DDI_SUCCESS) {
19891000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
19901000Sxc151355 		    "failed to allocate descriptors: %d\n", err));
19911000Sxc151355 		goto attach_fail3;
19921000Sxc151355 	}
19931000Sxc151355 
19941000Sxc151355 	/* Setup transmit queues in the HAL */
19951000Sxc151355 	if (ath_txq_setup(asc))
19961000Sxc151355 		goto attach_fail4;
19971000Sxc151355 
19983147Sxc151355 	ATH_HAL_GETMAC(ah, ic->ic_macaddr);
19991000Sxc151355 
20003147Sxc151355 	/*
20013147Sxc151355 	 * Initialize pointers to device specific functions which
20023147Sxc151355 	 * will be used by the generic layer.
20033147Sxc151355 	 */
20041000Sxc151355 	/* 11g support is identified when we fetch the channel set */
20051000Sxc151355 	if (asc->asc_have11g)
20064206Szf162725 		ic->ic_caps |= IEEE80211_C_SHPREAMBLE |
20074206Szf162725 		    IEEE80211_C_SHSLOT;		/* short slot time */
20083147Sxc151355 	/*
20093147Sxc151355 	 * Query the hal to figure out h/w crypto support.
20103147Sxc151355 	 */
20113147Sxc151355 	if (ATH_HAL_CIPHERSUPPORTED(ah, HAL_CIPHER_WEP))
20123147Sxc151355 		ic->ic_caps |= IEEE80211_C_WEP;
20133147Sxc151355 	if (ATH_HAL_CIPHERSUPPORTED(ah, HAL_CIPHER_AES_OCB))
20143147Sxc151355 		ic->ic_caps |= IEEE80211_C_AES;
20154126Szf162725 	if (ATH_HAL_CIPHERSUPPORTED(ah, HAL_CIPHER_AES_CCM)) {
20164126Szf162725 		ATH_DEBUG((ATH_DBG_ATTACH, "Atheros support H/W CCMP\n"));
20173147Sxc151355 		ic->ic_caps |= IEEE80211_C_AES_CCM;
20184126Szf162725 	}
20194126Szf162725 	if (ATH_HAL_CIPHERSUPPORTED(ah, HAL_CIPHER_CKIP))
20203147Sxc151355 		ic->ic_caps |= IEEE80211_C_CKIP;
20214126Szf162725 	if (ATH_HAL_CIPHERSUPPORTED(ah, HAL_CIPHER_TKIP)) {
20224126Szf162725 		ATH_DEBUG((ATH_DBG_ATTACH, "Atheros support H/W TKIP\n"));
20234126Szf162725 		ic->ic_caps |= IEEE80211_C_TKIP;
20243147Sxc151355 		/*
20253147Sxc151355 		 * Check if h/w does the MIC and/or whether the
20263147Sxc151355 		 * separate key cache entries are required to
20273147Sxc151355 		 * handle both tx+rx MIC keys.
20283147Sxc151355 		 */
20294126Szf162725 		if (ATH_HAL_CIPHERSUPPORTED(ah, HAL_CIPHER_MIC)) {
20304126Szf162725 			ATH_DEBUG((ATH_DBG_ATTACH, "Support H/W TKIP MIC\n"));
20313147Sxc151355 			ic->ic_caps |= IEEE80211_C_TKIPMIC;
20324126Szf162725 		}
20333147Sxc151355 		if (ATH_HAL_TKIPSPLIT(ah))
20343147Sxc151355 			asc->asc_splitmic = 1;
20353147Sxc151355 	}
20364126Szf162725 	ic->ic_caps |= IEEE80211_C_WPA;	/* Support WPA/WPA2 */
20374126Szf162725 
20383147Sxc151355 	asc->asc_hasclrkey = ATH_HAL_CIPHERSUPPORTED(ah, HAL_CIPHER_CLR);
20393147Sxc151355 	ic->ic_phytype = IEEE80211_T_OFDM;
20403147Sxc151355 	ic->ic_opmode = IEEE80211_M_STA;
20413147Sxc151355 	ic->ic_state = IEEE80211_S_INIT;
20423147Sxc151355 	ic->ic_maxrssi = ATH_MAX_RSSI;
20433147Sxc151355 	ic->ic_set_shortslot = ath_set_shortslot;
20443147Sxc151355 	ic->ic_xmit = ath_xmit;
20453147Sxc151355 	ieee80211_attach(ic);
20461000Sxc151355 
20474126Szf162725 	/* different instance has different WPA door */
20484126Szf162725 	(void) snprintf(ic->ic_wpadoor, MAX_IEEE80211STR, "%s_%s%d", WPA_DOOR,
20495420Sxc151355 	    ddi_driver_name(devinfo),
20505420Sxc151355 	    ddi_get_instance(devinfo));
20514126Szf162725 
20523147Sxc151355 	/* Override 80211 default routines */
20533147Sxc151355 	ic->ic_reset = ath_reset;
20543147Sxc151355 	asc->asc_newstate = ic->ic_newstate;
20553147Sxc151355 	ic->ic_newstate = ath_newstate;
20563147Sxc151355 	ic->ic_watchdog = ath_watchdog;
20573147Sxc151355 	ic->ic_node_alloc = ath_node_alloc;
20583147Sxc151355 	ic->ic_node_free = ath_node_free;
20593147Sxc151355 	ic->ic_crypto.cs_key_alloc = ath_key_alloc;
20603147Sxc151355 	ic->ic_crypto.cs_key_delete = ath_key_delete;
20613147Sxc151355 	ic->ic_crypto.cs_key_set = ath_key_set;
20623147Sxc151355 	ieee80211_media_init(ic);
20634296Szf162725 	/*
20644296Szf162725 	 * initialize default tx key
20654296Szf162725 	 */
20664296Szf162725 	ic->ic_def_txkey = 0;
20671000Sxc151355 
20681000Sxc151355 	asc->asc_rx_pend = 0;
20691000Sxc151355 	ATH_HAL_INTRSET(ah, 0);
20701000Sxc151355 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW,
20711000Sxc151355 	    &asc->asc_softint_id, NULL, 0, ath_softint_handler, (caddr_t)asc);
20721000Sxc151355 	if (err != DDI_SUCCESS) {
20731000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
20743147Sxc151355 		    "ddi_add_softintr() failed\n"));
20751000Sxc151355 		goto attach_fail5;
20761000Sxc151355 	}
20771000Sxc151355 
20781000Sxc151355 	if (ddi_get_iblock_cookie(devinfo, 0, &asc->asc_iblock)
20791000Sxc151355 	    != DDI_SUCCESS) {
20801000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
20811000Sxc151355 		    "Can not get iblock cookie for INT\n"));
20821000Sxc151355 		goto attach_fail6;
20831000Sxc151355 	}
20841000Sxc151355 
20853147Sxc151355 	if (ddi_add_intr(devinfo, 0, NULL, NULL, ath_intr,
20863147Sxc151355 	    (caddr_t)asc) != DDI_SUCCESS) {
20871000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
20881000Sxc151355 		    "Can not set intr for ATH driver\n"));
20891000Sxc151355 		goto attach_fail6;
20901000Sxc151355 	}
20913147Sxc151355 
20923147Sxc151355 	/*
20933147Sxc151355 	 * Provide initial settings for the WiFi plugin; whenever this
20943147Sxc151355 	 * information changes, we need to call mac_plugindata_update()
20953147Sxc151355 	 */
20963147Sxc151355 	wd.wd_opmode = ic->ic_opmode;
20973147Sxc151355 	wd.wd_secalloc = WIFI_SEC_NONE;
20983147Sxc151355 	IEEE80211_ADDR_COPY(wd.wd_bssid, ic->ic_bss->in_bssid);
20993147Sxc151355 
21003147Sxc151355 	if ((macp = mac_alloc(MAC_VERSION)) == NULL) {
21013147Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
21023147Sxc151355 		    "MAC version mismatch\n"));
21033147Sxc151355 		goto attach_fail7;
21043147Sxc151355 	}
21051000Sxc151355 
21063147Sxc151355 	macp->m_type_ident	= MAC_PLUGIN_IDENT_WIFI;
21073147Sxc151355 	macp->m_driver		= asc;
21083147Sxc151355 	macp->m_dip		= devinfo;
21093147Sxc151355 	macp->m_src_addr	= ic->ic_macaddr;
21103147Sxc151355 	macp->m_callbacks	= &ath_m_callbacks;
21113147Sxc151355 	macp->m_min_sdu		= 0;
21123147Sxc151355 	macp->m_max_sdu		= IEEE80211_MTU;
21133147Sxc151355 	macp->m_pdata		= &wd;
21143147Sxc151355 	macp->m_pdata_size	= sizeof (wd);
21153147Sxc151355 
21163147Sxc151355 	err = mac_register(macp, &ic->ic_mach);
21173147Sxc151355 	mac_free(macp);
21183147Sxc151355 	if (err != 0) {
21191000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "ath: ath_attach(): "
21203147Sxc151355 		    "mac_register err %x\n", err));
21211000Sxc151355 		goto attach_fail7;
21221000Sxc151355 	}
21231000Sxc151355 
21241000Sxc151355 	/* Create minor node of type DDI_NT_NET_WIFI */
21251000Sxc151355 	(void) snprintf(strbuf, sizeof (strbuf), "%s%d",
21263147Sxc151355 	    ATH_NODENAME, instance);
21271000Sxc151355 	err = ddi_create_minor_node(devinfo, strbuf, S_IFCHR,
21283147Sxc151355 	    instance + 1, DDI_NT_NET_WIFI, 0);
21291000Sxc151355 	if (err != DDI_SUCCESS)
21301000Sxc151355 		ATH_DEBUG((ATH_DBG_ATTACH, "WARN: ath: ath_attach(): "
21311000Sxc151355 		    "Create minor node failed - %d\n", err));
21321000Sxc151355 
21333147Sxc151355 	mac_link_update(ic->ic_mach, LINK_STATE_DOWN);
21341000Sxc151355 	asc->asc_invalid = 1;
2135*6235Sxc151355 	asc->asc_promisc = B_FALSE;
2136*6235Sxc151355 	bzero(asc->asc_mcast_refs, sizeof (asc->asc_mcast_refs));
2137*6235Sxc151355 	bzero(asc->asc_mcast_hash, sizeof (asc->asc_mcast_hash));
21381000Sxc151355 	return (DDI_SUCCESS);
21391000Sxc151355 attach_fail7:
21401000Sxc151355 	ddi_remove_intr(devinfo, 0, asc->asc_iblock);
21411000Sxc151355 attach_fail6:
21421000Sxc151355 	ddi_remove_softintr(asc->asc_softint_id);
21431000Sxc151355 attach_fail5:
21443147Sxc151355 	(void) ieee80211_detach(ic);
21451000Sxc151355 attach_fail4:
21461000Sxc151355 	ath_desc_free(asc);
21471000Sxc151355 attach_fail3:
21481000Sxc151355 	ah->ah_detach(asc->asc_ah);
21491000Sxc151355 attach_fail2:
21501000Sxc151355 	ddi_regs_map_free(&asc->asc_io_handle);
21511000Sxc151355 attach_fail1:
21521000Sxc151355 	pci_config_teardown(&asc->asc_cfg_handle);
21531000Sxc151355 attach_fail0:
21541000Sxc151355 	asc->asc_invalid = 1;
21551000Sxc151355 	mutex_destroy(&asc->asc_txbuflock);
21561000Sxc151355 	for (i = 0; i < HAL_NUM_TX_QUEUES; i++) {
21571000Sxc151355 		if (ATH_TXQ_SETUP(asc, i)) {
21581000Sxc151355 			struct ath_txq *txq = &asc->asc_txq[i];
21591000Sxc151355 			mutex_destroy(&txq->axq_lock);
21601000Sxc151355 		}
21611000Sxc151355 	}
21621000Sxc151355 	mutex_destroy(&asc->asc_rxbuflock);
21631000Sxc151355 	mutex_destroy(&asc->asc_genlock);
21643147Sxc151355 	mutex_destroy(&asc->asc_resched_lock);
21653147Sxc151355 	ddi_soft_state_free(ath_soft_state_p, instance);
21661000Sxc151355 
21671000Sxc151355 	return (DDI_FAILURE);
21681000Sxc151355 }
21691000Sxc151355 
21701000Sxc151355 static int32_t
21711000Sxc151355 ath_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
21721000Sxc151355 {
21731000Sxc151355 	ath_t *asc;
21741000Sxc151355 
21751000Sxc151355 	asc = ddi_get_soft_state(ath_soft_state_p, ddi_get_instance(devinfo));
21761000Sxc151355 	ASSERT(asc != NULL);
21771000Sxc151355 
21783147Sxc151355 	if (cmd != DDI_DETACH)
21791000Sxc151355 		return (DDI_FAILURE);
21801000Sxc151355 
21813147Sxc151355 	ath_stop_scantimer(asc);
21821000Sxc151355 
21831000Sxc151355 	/* disable interrupts */
21841000Sxc151355 	ATH_HAL_INTRSET(asc->asc_ah, 0);
21851000Sxc151355 
21863147Sxc151355 	/*
21873147Sxc151355 	 * Unregister from the MAC layer subsystem
21883147Sxc151355 	 */
21893147Sxc151355 	if (mac_unregister(asc->asc_isc.ic_mach) != 0)
21903147Sxc151355 		return (DDI_FAILURE);
21913147Sxc151355 
21921000Sxc151355 	/* free intterrupt resources */
21931000Sxc151355 	ddi_remove_intr(devinfo, 0, asc->asc_iblock);
21941000Sxc151355 	ddi_remove_softintr(asc->asc_softint_id);
21951000Sxc151355 
21963147Sxc151355 	/*
21973147Sxc151355 	 * NB: the order of these is important:
21983147Sxc151355 	 * o call the 802.11 layer before detaching the hal to
21993147Sxc151355 	 *   insure callbacks into the driver to delete global
22003147Sxc151355 	 *   key cache entries can be handled
22013147Sxc151355 	 * o reclaim the tx queue data structures after calling
22023147Sxc151355 	 *   the 802.11 layer as we'll get called back to reclaim
22033147Sxc151355 	 *   node state and potentially want to use them
22043147Sxc151355 	 * o to cleanup the tx queues the hal is called, so detach
22053147Sxc151355 	 *   it last
22063147Sxc151355 	 */
22073147Sxc151355 	ieee80211_detach(&asc->asc_isc);
22081000Sxc151355 	ath_desc_free(asc);
22093147Sxc151355 	ath_txq_cleanup(asc);
22101000Sxc151355 	asc->asc_ah->ah_detach(asc->asc_ah);
22111000Sxc151355 
22121000Sxc151355 	/* free io handle */
22131000Sxc151355 	ddi_regs_map_free(&asc->asc_io_handle);
22141000Sxc151355 	pci_config_teardown(&asc->asc_cfg_handle);
22151000Sxc151355 
22161000Sxc151355 	/* destroy locks */
22171000Sxc151355 	mutex_destroy(&asc->asc_rxbuflock);
22181000Sxc151355 	mutex_destroy(&asc->asc_genlock);
22193147Sxc151355 	mutex_destroy(&asc->asc_resched_lock);
22201000Sxc151355 
22211000Sxc151355 	ddi_remove_minor_node(devinfo, NULL);
22221000Sxc151355 	ddi_soft_state_free(ath_soft_state_p, ddi_get_instance(devinfo));
22231000Sxc151355 
22241000Sxc151355 	return (DDI_SUCCESS);
22251000Sxc151355 }
22261000Sxc151355 
22273147Sxc151355 DDI_DEFINE_STREAM_OPS(ath_dev_ops, nulldev, nulldev, ath_attach, ath_detach,
22283147Sxc151355     nodev, NULL, D_MP, NULL);
22291000Sxc151355 
22301000Sxc151355 static struct modldrv ath_modldrv = {
22311000Sxc151355 	&mod_driverops,		/* Type of module.  This one is a driver */
2232*6235Sxc151355 	"ath driver 1.3.1/HAL 0.9.17.2",	/* short description */
22331000Sxc151355 	&ath_dev_ops		/* driver specific ops */
22341000Sxc151355 };
22351000Sxc151355 
22361000Sxc151355 static struct modlinkage modlinkage = {
22371000Sxc151355 	MODREV_1, (void *)&ath_modldrv, NULL
22381000Sxc151355 };
22391000Sxc151355 
22401000Sxc151355 
22411000Sxc151355 int
22421000Sxc151355 _info(struct modinfo *modinfop)
22431000Sxc151355 {
22441000Sxc151355 	return (mod_info(&modlinkage, modinfop));
22451000Sxc151355 }
22461000Sxc151355 
22471000Sxc151355 int
22481000Sxc151355 _init(void)
22491000Sxc151355 {
22501000Sxc151355 	int status;
22511000Sxc151355 
22521000Sxc151355 	status = ddi_soft_state_init(&ath_soft_state_p, sizeof (ath_t), 1);
22531000Sxc151355 	if (status != 0)
22541000Sxc151355 		return (status);
22551000Sxc151355 
22561000Sxc151355 	mutex_init(&ath_loglock, NULL, MUTEX_DRIVER, NULL);
22573147Sxc151355 	ath_halfix_init();
22583147Sxc151355 	mac_init_ops(&ath_dev_ops, "ath");
22591000Sxc151355 	status = mod_install(&modlinkage);
22601000Sxc151355 	if (status != 0) {
22613147Sxc151355 		mac_fini_ops(&ath_dev_ops);
22623147Sxc151355 		ath_halfix_finit();
22633147Sxc151355 		mutex_destroy(&ath_loglock);
22641000Sxc151355 		ddi_soft_state_fini(&ath_soft_state_p);
22651000Sxc151355 	}
22661000Sxc151355 
22671000Sxc151355 	return (status);
22681000Sxc151355 }
22691000Sxc151355 
22701000Sxc151355 int
22711000Sxc151355 _fini(void)
22721000Sxc151355 {
22731000Sxc151355 	int status;
22741000Sxc151355 
22751000Sxc151355 	status = mod_remove(&modlinkage);
22761000Sxc151355 	if (status == 0) {
22773147Sxc151355 		mac_fini_ops(&ath_dev_ops);
22783147Sxc151355 		ath_halfix_finit();
22793147Sxc151355 		mutex_destroy(&ath_loglock);
22801000Sxc151355 		ddi_soft_state_fini(&ath_soft_state_p);
22811000Sxc151355 	}
22821000Sxc151355 	return (status);
22831000Sxc151355 }
2284