xref: /onnv-gate/usr/src/uts/common/io/ipw/ipw2100.c (revision 4784:5fb6917c89df)
13847Seh146360 /*
23847Seh146360  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
33847Seh146360  * Use is subject to license terms.
43847Seh146360  */
53847Seh146360 
63847Seh146360 /*
73847Seh146360  * Copyright(c) 2004
83847Seh146360  *	Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
93847Seh146360  *
103847Seh146360  * Redistribution and use in source and binary forms, with or without
113847Seh146360  * modification, are permitted provided that the following conditions
123847Seh146360  * are met:
133847Seh146360  * 1. Redistributions of source code must retain the above copyright
143847Seh146360  *    notice unmodified, this list of conditions, and the following
153847Seh146360  *    disclaimer.
163847Seh146360  * 2. Redistributions in binary form must reproduce the above copyright
173847Seh146360  *    notice, this list of conditions and the following disclaimer in the
183847Seh146360  *    documentation and/or other materials provided with the distribution.
193847Seh146360  *
203847Seh146360  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
213847Seh146360  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
223847Seh146360  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
233847Seh146360  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
243847Seh146360  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
253847Seh146360  * DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
263847Seh146360  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
273847Seh146360  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
283847Seh146360  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
293847Seh146360  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
303847Seh146360  * SUCH DAMAGE.
313847Seh146360  */
323847Seh146360 
333847Seh146360 #pragma ident	"%Z%%M%	%I%	%E% SMI"
343847Seh146360 
353847Seh146360 #include <sys/types.h>
363847Seh146360 #include <sys/byteorder.h>
373847Seh146360 #include <sys/conf.h>
383847Seh146360 #include <sys/cmn_err.h>
393847Seh146360 #include <sys/stat.h>
403847Seh146360 #include <sys/ddi.h>
413847Seh146360 #include <sys/sunddi.h>
423847Seh146360 #include <sys/strsubr.h>
433847Seh146360 #include <sys/ethernet.h>
443847Seh146360 #include <inet/common.h>
453847Seh146360 #include <inet/nd.h>
463847Seh146360 #include <inet/mi.h>
473847Seh146360 #include <sys/note.h>
483847Seh146360 #include <sys/stream.h>
493847Seh146360 #include <sys/strsun.h>
503847Seh146360 #include <sys/modctl.h>
513847Seh146360 #include <sys/devops.h>
523847Seh146360 #include <sys/dlpi.h>
533847Seh146360 #include <sys/mac.h>
543847Seh146360 #include <sys/mac_wifi.h>
553847Seh146360 #include <sys/varargs.h>
563847Seh146360 #include <sys/policy.h>
573847Seh146360 
583847Seh146360 #include "ipw2100.h"
593847Seh146360 #include "ipw2100_impl.h"
603847Seh146360 #include <inet/wifi_ioctl.h>
613847Seh146360 
623847Seh146360 /*
633847Seh146360  * kCF framework include files
643847Seh146360  */
653847Seh146360 #include <sys/crypto/common.h>
663847Seh146360 #include <sys/crypto/api.h>
673847Seh146360 
683847Seh146360 static void   *ipw2100_ssp	= NULL;
693847Seh146360 static char   ipw2100_ident[]	= IPW2100_DRV_DESC " " IPW2100_DRV_REV;
703847Seh146360 
713847Seh146360 /*
723847Seh146360  * PIO access attribute for register
733847Seh146360  */
743847Seh146360 static ddi_device_acc_attr_t ipw2100_csr_accattr = {
753847Seh146360 	DDI_DEVICE_ATTR_V0,
763847Seh146360 	DDI_STRUCTURE_LE_ACC,
773847Seh146360 	DDI_STRICTORDER_ACC
783847Seh146360 };
793847Seh146360 
803847Seh146360 static ddi_device_acc_attr_t ipw2100_dma_accattr = {
813847Seh146360 	DDI_DEVICE_ATTR_V0,
823847Seh146360 	DDI_NEVERSWAP_ACC,
833847Seh146360 	DDI_STRICTORDER_ACC
843847Seh146360 };
853847Seh146360 
863847Seh146360 static ddi_dma_attr_t ipw2100_dma_attr = {
873847Seh146360 	DMA_ATTR_V0,
883847Seh146360 	0x0000000000000000ULL,
893847Seh146360 	0x00000000ffffffffULL,
903847Seh146360 	0x00000000ffffffffULL,
913847Seh146360 	0x0000000000000004ULL,
923847Seh146360 	0xfff,
933847Seh146360 	1,
943847Seh146360 	0x00000000ffffffffULL,
953847Seh146360 	0x00000000ffffffffULL,
963847Seh146360 	1,
973847Seh146360 	1,
983847Seh146360 	0
993847Seh146360 };
1003847Seh146360 
1013847Seh146360 static const struct ieee80211_rateset ipw2100_rateset_11b = { 4,
1023847Seh146360 	{2, 4, 11, 22}
1033847Seh146360 };
1043847Seh146360 
1053847Seh146360 /*
1063847Seh146360  * For mfthread only
1073847Seh146360  */
1083847Seh146360 extern pri_t minclsyspri;
1093847Seh146360 
1103847Seh146360 /*
1113847Seh146360  * ipw2100 specific hardware operations
1123847Seh146360  */
1133847Seh146360 static void	ipw2100_hwconf_get(struct ipw2100_softc *sc);
1143847Seh146360 static int	ipw2100_chip_reset(struct ipw2100_softc *sc);
1153847Seh146360 static void	ipw2100_master_stop(struct ipw2100_softc *sc);
1163847Seh146360 static void	ipw2100_stop(struct ipw2100_softc *sc);
1173847Seh146360 static int	ipw2100_config(struct ipw2100_softc *sc);
1183847Seh146360 static int	ipw2100_cmd(struct ipw2100_softc *sc, uint32_t type,
1193847Seh146360     void *buf, size_t len);
1203847Seh146360 static int	ipw2100_dma_region_alloc(struct ipw2100_softc *sc,
1213847Seh146360     struct dma_region *dr, size_t size, uint_t dir, uint_t flags);
1223847Seh146360 static void	ipw2100_dma_region_free(struct dma_region *dr);
1233847Seh146360 static void	ipw2100_tables_init(struct ipw2100_softc *sc);
1243847Seh146360 static void	ipw2100_ring_hwsetup(struct ipw2100_softc *sc);
1253847Seh146360 static int	ipw2100_ring_alloc(struct ipw2100_softc *sc);
1263847Seh146360 static void	ipw2100_ring_free(struct ipw2100_softc *sc);
1273847Seh146360 static void	ipw2100_ring_reset(struct ipw2100_softc *sc);
1283847Seh146360 static int	ipw2100_ring_init(struct ipw2100_softc *sc);
1293847Seh146360 
1303847Seh146360 /*
1313847Seh146360  * GLD specific operations
1323847Seh146360  */
1333847Seh146360 static int	ipw2100_m_stat(void *arg, uint_t stat, uint64_t *val);
1343847Seh146360 static int	ipw2100_m_start(void *arg);
1353847Seh146360 static void	ipw2100_m_stop(void *arg);
1363847Seh146360 static int	ipw2100_m_unicst(void *arg, const uint8_t *macaddr);
1373847Seh146360 static int	ipw2100_m_multicst(void *arg, boolean_t add, const uint8_t *m);
1383847Seh146360 static int	ipw2100_m_promisc(void *arg, boolean_t on);
1393847Seh146360 static mblk_t  *ipw2100_m_tx(void *arg, mblk_t *mp);
1403847Seh146360 static void	ipw2100_m_ioctl(void *arg, queue_t *wq, mblk_t *mp);
1413847Seh146360 
1423847Seh146360 /*
1433847Seh146360  * Interrupt and Data transferring operations
1443847Seh146360  */
1453847Seh146360 static uint_t	ipw2100_intr(caddr_t arg);
1463847Seh146360 static int	ipw2100_send(struct ieee80211com *ic, mblk_t *mp, uint8_t type);
1473847Seh146360 static void	ipw2100_rcvpkt(struct ipw2100_softc *sc,
1483847Seh146360     struct ipw2100_status *status, uint8_t *rxbuf);
1493847Seh146360 
1503847Seh146360 /*
1513847Seh146360  * WiFi specific operations
1523847Seh146360  */
1533847Seh146360 static int	ipw2100_newstate(struct ieee80211com *ic,
1543847Seh146360     enum ieee80211_state state, int arg);
1553847Seh146360 static void	ipw2100_thread(struct ipw2100_softc *sc);
1563847Seh146360 
1573847Seh146360 /*
1583847Seh146360  * IOCTL Handler
1593847Seh146360  */
1603847Seh146360 static int	ipw2100_ioctl(struct ipw2100_softc *sc, queue_t *q, mblk_t *m);
1613847Seh146360 static int	ipw2100_getset(struct ipw2100_softc *sc,
1623847Seh146360     mblk_t *m, uint32_t cmd, boolean_t *need_net80211);
1633847Seh146360 static int	ipw_wificfg_radio(struct ipw2100_softc *sc,
1643847Seh146360     uint32_t cmd,  wldp_t *outfp);
1653847Seh146360 static int	ipw_wificfg_desrates(wldp_t *outfp);
1663847Seh146360 static int	ipw_wificfg_disassoc(struct ipw2100_softc *sc,
1673847Seh146360     wldp_t *outfp);
1683847Seh146360 
1693847Seh146360 /*
1703847Seh146360  * Mac Call Back entries
1713847Seh146360  */
1723847Seh146360 mac_callbacks_t	ipw2100_m_callbacks = {
1733847Seh146360 	MC_IOCTL,
1743847Seh146360 	ipw2100_m_stat,
1753847Seh146360 	ipw2100_m_start,
1763847Seh146360 	ipw2100_m_stop,
1773847Seh146360 	ipw2100_m_promisc,
1783847Seh146360 	ipw2100_m_multicst,
1793847Seh146360 	ipw2100_m_unicst,
1803847Seh146360 	ipw2100_m_tx,
1813847Seh146360 	NULL,
1823847Seh146360 	ipw2100_m_ioctl
1833847Seh146360 };
1843847Seh146360 
1853847Seh146360 
1863847Seh146360 /*
1873847Seh146360  * DEBUG Facility
1883847Seh146360  */
1893847Seh146360 #define	MAX_MSG (128)
1903847Seh146360 uint32_t ipw2100_debug = 0;
1913847Seh146360 /*
1923847Seh146360  * supported debug marsks:
1933847Seh146360  *	| IPW2100_DBG_INIT
1943847Seh146360  *	| IPW2100_DBG_GLD
1953847Seh146360  *	| IPW2100_DBG_TABLE
1963847Seh146360  *	| IPW2100_DBG_SOFTINT
1973847Seh146360  *	| IPW2100_DBG_CSR
1983847Seh146360  *	| IPW2100_DBG_INT
1993847Seh146360  *	| IPW2100_DBG_FW
2003847Seh146360  *	| IPW2100_DBG_IOCTL
2013847Seh146360  *	| IPW2100_DBG_HWCAP
2023847Seh146360  *	| IPW2100_DBG_STATISTIC
2033847Seh146360  *	| IPW2100_DBG_RING
2043847Seh146360  *	| IPW2100_DBG_WIFI
2053847Seh146360  */
2063847Seh146360 
2073847Seh146360 /*
2083847Seh146360  * global tuning parameters to work around unknown hardware issues
2093847Seh146360  */
2103847Seh146360 static uint32_t delay_config_stable 	= 100000;	/* 100ms */
2113847Seh146360 static uint32_t delay_fatal_recover	= 100000 * 20;	/* 2s */
2123847Seh146360 static uint32_t delay_aux_thread 	= 100000;	/* 100ms */
2133847Seh146360 
2143847Seh146360 void
2153847Seh146360 ipw2100_dbg(dev_info_t *dip, int level, const char *fmt, ...)
2163847Seh146360 {
2173847Seh146360 	va_list	ap;
2183847Seh146360 	char    buf[MAX_MSG];
2193847Seh146360 	int	instance;
2203847Seh146360 
2213847Seh146360 	va_start(ap, fmt);
2223847Seh146360 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
2233847Seh146360 	va_end(ap);
2243847Seh146360 
2253847Seh146360 	if (dip) {
2263847Seh146360 		instance = ddi_get_instance(dip);
2273847Seh146360 		cmn_err(level, "%s%d: %s", IPW2100_DRV_NAME, instance, buf);
2283847Seh146360 	} else
2293847Seh146360 		cmn_err(level, "%s: %s", IPW2100_DRV_NAME, buf);
2303847Seh146360 }
2313847Seh146360 
2323847Seh146360 /*
2333847Seh146360  * device operations
2343847Seh146360  */
2353847Seh146360 int
2363847Seh146360 ipw2100_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2373847Seh146360 {
2383847Seh146360 	struct ipw2100_softc	*sc;
2393847Seh146360 	ddi_acc_handle_t	cfgh;
2403847Seh146360 	caddr_t			regs;
2413847Seh146360 	struct ieee80211com	*ic;
2423847Seh146360 	int			instance, err, i;
2433847Seh146360 	char			strbuf[32];
2443847Seh146360 	wifi_data_t		wd = { 0 };
2453847Seh146360 	mac_register_t		*macp;
2463847Seh146360 
2473847Seh146360 	if (cmd != DDI_ATTACH) {
2483847Seh146360 		err = DDI_FAILURE;
2493847Seh146360 		goto fail1;
2503847Seh146360 	}
2513847Seh146360 
2523847Seh146360 	instance = ddi_get_instance(dip);
2533847Seh146360 	err = ddi_soft_state_zalloc(ipw2100_ssp, instance);
2543847Seh146360 	if (err != DDI_SUCCESS) {
2553847Seh146360 		IPW2100_WARN((dip, CE_WARN,
2563847Seh146360 		    "ipw2100_attach(): unable to allocate soft state\n"));
2573847Seh146360 		goto fail1;
2583847Seh146360 	}
2593847Seh146360 	sc = ddi_get_soft_state(ipw2100_ssp, instance);
2603847Seh146360 	sc->sc_dip = dip;
2613847Seh146360 
2623847Seh146360 	/*
2633847Seh146360 	 * Map config spaces register
2643847Seh146360 	 */
2653847Seh146360 	err = ddi_regs_map_setup(dip, IPW2100_PCI_CFG_RNUM, &regs,
2663847Seh146360 	    0, 0, &ipw2100_csr_accattr, &cfgh);
2673847Seh146360 	if (err != DDI_SUCCESS) {
2683847Seh146360 		IPW2100_WARN((dip, CE_WARN,
2693847Seh146360 		    "ipw2100_attach(): unable to map spaces regs\n"));
2703847Seh146360 		goto fail2;
2713847Seh146360 	}
2723847Seh146360 	ddi_put8(cfgh, (uint8_t *)(regs + 0x41), 0);
2733847Seh146360 	ddi_regs_map_free(&cfgh);
2743847Seh146360 
2753847Seh146360 	/*
2763847Seh146360 	 * Map operating registers
2773847Seh146360 	 */
2783847Seh146360 	err = ddi_regs_map_setup(dip, IPW2100_PCI_CSR_RNUM, &sc->sc_regs,
2793847Seh146360 	    0, 0, &ipw2100_csr_accattr, &sc->sc_ioh);
2803847Seh146360 	if (err != DDI_SUCCESS) {
2813847Seh146360 		IPW2100_WARN((dip, CE_WARN,
2823847Seh146360 		    "ipw2100_attach(): unable to map device regs\n"));
2833847Seh146360 		goto fail2;
2843847Seh146360 	}
2853847Seh146360 
2863847Seh146360 	/*
2873847Seh146360 	 * Reset the chip
2883847Seh146360 	 */
2893847Seh146360 	err = ipw2100_chip_reset(sc);
2903847Seh146360 	if (err != DDI_SUCCESS) {
2913847Seh146360 		IPW2100_WARN((dip, CE_WARN,
2923847Seh146360 		    "ipw2100_attach(): reset failed\n"));
2933847Seh146360 		goto fail3;
2943847Seh146360 	}
2953847Seh146360 
2963847Seh146360 	/*
2973847Seh146360 	 * Get the hw conf, including MAC address, then init all rings.
2983847Seh146360 	 */
2993847Seh146360 	ipw2100_hwconf_get(sc);
3003847Seh146360 	err = ipw2100_ring_init(sc);
3013847Seh146360 	if (err != DDI_SUCCESS) {
3023847Seh146360 		IPW2100_WARN((dip, CE_WARN,
3033847Seh146360 		    "ipw2100_attach(): "
3043847Seh146360 		    "unable to allocate and initialize rings\n"));
3053847Seh146360 		goto fail3;
3063847Seh146360 	}
3073847Seh146360 
3083847Seh146360 	/*
3093847Seh146360 	 * Initialize mutexs and condvars
3103847Seh146360 	 */
3113847Seh146360 	err = ddi_get_iblock_cookie(dip, 0, &sc->sc_iblk);
3123847Seh146360 	if (err != DDI_SUCCESS) {
3133847Seh146360 		IPW2100_WARN((dip, CE_WARN,
3143847Seh146360 		    "ipw2100_attach(): ddi_get_iblock_cookie() failed\n"));
3153847Seh146360 		goto fail4;
3163847Seh146360 	}
3173847Seh146360 	/*
3183847Seh146360 	 * interrupt lock
3193847Seh146360 	 */
3203847Seh146360 	mutex_init(&sc->sc_ilock, "interrupt-lock", MUTEX_DRIVER,
3213847Seh146360 	    (void *) sc->sc_iblk);
3223847Seh146360 	cv_init(&sc->sc_fw_cond, "firmware", CV_DRIVER, NULL);
3233847Seh146360 	cv_init(&sc->sc_cmd_cond, "command", CV_DRIVER, NULL);
3243847Seh146360 	/*
3253847Seh146360 	 * tx ring lock
3263847Seh146360 	 */
3273847Seh146360 	mutex_init(&sc->sc_tx_lock, "tx-ring", MUTEX_DRIVER,
3283847Seh146360 	    (void *) sc->sc_iblk);
3293847Seh146360 	cv_init(&sc->sc_tx_cond, "tx-ring", CV_DRIVER, NULL);
3303847Seh146360 	/*
3313847Seh146360 	 * rescheuled lock
3323847Seh146360 	 */
3333847Seh146360 	mutex_init(&sc->sc_resched_lock, "reschedule-lock", MUTEX_DRIVER,
3343847Seh146360 	    (void *) sc->sc_iblk);
3353847Seh146360 	/*
3363847Seh146360 	 * initialize the mfthread
3373847Seh146360 	 */
3383847Seh146360 	mutex_init(&sc->sc_mflock, "function-lock", MUTEX_DRIVER,
3393847Seh146360 	    (void *) sc->sc_iblk);
3403847Seh146360 	cv_init(&sc->sc_mfthread_cv, NULL, CV_DRIVER, NULL);
3413847Seh146360 	sc->sc_mf_thread = NULL;
3423847Seh146360 	sc->sc_mfthread_switch = 0;
3433847Seh146360 	/*
3443847Seh146360 	 * Initialize the wifi part, which will be used by
3453847Seh146360 	 * generic layer
3463847Seh146360 	 */
3473847Seh146360 	ic = &sc->sc_ic;
3483847Seh146360 	ic->ic_phytype  = IEEE80211_T_DS;
3493847Seh146360 	ic->ic_opmode   = IEEE80211_M_STA;
3503847Seh146360 	ic->ic_state    = IEEE80211_S_INIT;
3513847Seh146360 	ic->ic_maxrssi  = 49;
3523847Seh146360 	/*
3533847Seh146360 	 * Future, could use s/w to handle encryption: IEEE80211_C_WEP
3543847Seh146360 	 * and need to add support for IEEE80211_C_IBSS
3553847Seh146360 	 */
3563847Seh146360 	ic->ic_caps = IEEE80211_C_SHPREAMBLE | IEEE80211_C_TXPMGT |
3573847Seh146360 	    IEEE80211_C_PMGT;
3583847Seh146360 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ipw2100_rateset_11b;
3593847Seh146360 	IEEE80211_ADDR_COPY(ic->ic_macaddr, sc->sc_macaddr);
3603847Seh146360 	for (i = 1; i < 16; i++) {
3613847Seh146360 		if (sc->sc_chmask &(1 << i)) {
3623847Seh146360 			/* IEEE80211_CHAN_B */
3633847Seh146360 			ic->ic_sup_channels[i].ich_freq  = ieee80211_ieee2mhz(i,
3643847Seh146360 			    IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_CCK);
3653847Seh146360 			ic->ic_sup_channels[i].ich_flags =
3663847Seh146360 			    IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_CCK;
3673847Seh146360 		}
3683847Seh146360 	}
3693847Seh146360 	ic->ic_ibss_chan = &ic->ic_sup_channels[0];
3703847Seh146360 	ic->ic_xmit = ipw2100_send;
3713847Seh146360 	/*
3723847Seh146360 	 * init Wifi layer
3733847Seh146360 	 */
3743847Seh146360 	ieee80211_attach(ic);
3753847Seh146360 
3763847Seh146360 	/*
3773847Seh146360 	 * Override 80211 default routines
3783847Seh146360 	 */
3793847Seh146360 	ieee80211_media_init(ic);
3803847Seh146360 	sc->sc_newstate = ic->ic_newstate;
3813847Seh146360 	ic->ic_newstate = ipw2100_newstate;
3823847Seh146360 	/*
3833847Seh146360 	 * initialize default tx key
3843847Seh146360 	 */
3853847Seh146360 	ic->ic_def_txkey = 0;
3863847Seh146360 	/*
3873847Seh146360 	 * Set the Authentication to AUTH_Open only.
3883847Seh146360 	 */
3893847Seh146360 	sc->sc_authmode = IEEE80211_AUTH_OPEN;
3903847Seh146360 
3913847Seh146360 	/*
3923847Seh146360 	 * Add the interrupt handler
3933847Seh146360 	 */
3943847Seh146360 	err = ddi_add_intr(dip, 0, &sc->sc_iblk, NULL,
3953847Seh146360 	    ipw2100_intr, (caddr_t)sc);
3963847Seh146360 	if (err != DDI_SUCCESS) {
3973847Seh146360 		IPW2100_WARN((dip, CE_WARN,
3983847Seh146360 		    "ipw2100_attach(): ddi_add_intr() failed\n"));
3993847Seh146360 		goto fail5;
4003847Seh146360 	}
4013847Seh146360 
4023847Seh146360 	/*
4033847Seh146360 	 * Initialize pointer to device specific functions
4043847Seh146360 	 */
4053847Seh146360 	wd.wd_secalloc = WIFI_SEC_NONE;
4063847Seh146360 	wd.wd_opmode = ic->ic_opmode;
4073847Seh146360 	IEEE80211_ADDR_COPY(wd.wd_bssid, ic->ic_macaddr);
4083847Seh146360 
4093847Seh146360 	macp = mac_alloc(MAC_VERSION);
4103847Seh146360 	if (err != 0) {
4113847Seh146360 		IPW2100_WARN((dip, CE_WARN,
4123847Seh146360 		    "ipw2100_attach(): mac_alloc() failed\n"));
4133847Seh146360 		goto fail6;
4143847Seh146360 	}
4153847Seh146360 
4163847Seh146360 	macp->m_type_ident	= MAC_PLUGIN_IDENT_WIFI;
4173847Seh146360 	macp->m_driver		= sc;
4183847Seh146360 	macp->m_dip		= dip;
4193847Seh146360 	macp->m_src_addr	= ic->ic_macaddr;
4203847Seh146360 	macp->m_callbacks	= &ipw2100_m_callbacks;
4213847Seh146360 	macp->m_min_sdu		= 0;
4223847Seh146360 	macp->m_max_sdu		= IEEE80211_MTU;
4233847Seh146360 	macp->m_pdata		= &wd;
4243847Seh146360 	macp->m_pdata_size	= sizeof (wd);
4253847Seh146360 
4263847Seh146360 	/*
4273847Seh146360 	 * Register the macp to mac
4283847Seh146360 	 */
4293847Seh146360 	err = mac_register(macp, &ic->ic_mach);
4303847Seh146360 	mac_free(macp);
4313847Seh146360 	if (err != DDI_SUCCESS) {
4323847Seh146360 		IPW2100_WARN((dip, CE_WARN,
4333847Seh146360 		    "ipw2100_attach(): mac_register() failed\n"));
4343847Seh146360 		goto fail6;
4353847Seh146360 	}
4363847Seh146360 
4373847Seh146360 	/*
4383847Seh146360 	 * Create minor node of type DDI_NT_NET_WIFI
4393847Seh146360 	 */
4403847Seh146360 	(void) snprintf(strbuf, sizeof (strbuf), "%s%d",
4413847Seh146360 	    IPW2100_DRV_NAME, instance);
4423847Seh146360 	err = ddi_create_minor_node(dip, strbuf, S_IFCHR,
4433847Seh146360 	    instance + 1, DDI_NT_NET_WIFI, 0);
4443847Seh146360 	if (err != DDI_SUCCESS)
4453847Seh146360 		IPW2100_WARN((dip, CE_WARN,
4463847Seh146360 		    "ipw2100_attach(): ddi_create_minor_node() failed\n"));
4473847Seh146360 
4483847Seh146360 	/*
4493847Seh146360 	 * Cache firmware, always return true
4503847Seh146360 	 */
4513847Seh146360 	(void) ipw2100_cache_firmware(sc);
4523847Seh146360 
4533847Seh146360 	/*
4543847Seh146360 	 * Notify link is down now
4553847Seh146360 	 */
4563847Seh146360 	mac_link_update(ic->ic_mach, LINK_STATE_DOWN);
4573847Seh146360 
4583847Seh146360 	/*
4593847Seh146360 	 * create the mf thread to handle the link status,
4603847Seh146360 	 * recovery fatal error, etc.
4613847Seh146360 	 */
4623847Seh146360 	sc->sc_mfthread_switch = 1;
4633847Seh146360 	if (sc->sc_mf_thread == NULL)
4643847Seh146360 		sc->sc_mf_thread = thread_create((caddr_t)NULL, 0,
4653847Seh146360 		    ipw2100_thread, sc, 0, &p0, TS_RUN, minclsyspri);
4663847Seh146360 
4673847Seh146360 	return (DDI_SUCCESS);
4683847Seh146360 
4693847Seh146360 fail6:
4703847Seh146360 	ddi_remove_intr(dip, 0, sc->sc_iblk);
4713847Seh146360 fail5:
4723847Seh146360 	ieee80211_detach(ic);
4733847Seh146360 
4743847Seh146360 	mutex_destroy(&sc->sc_ilock);
4753847Seh146360 	mutex_destroy(&sc->sc_tx_lock);
4763847Seh146360 	mutex_destroy(&sc->sc_mflock);
4773847Seh146360 	mutex_destroy(&sc->sc_resched_lock);
4783847Seh146360 	cv_destroy(&sc->sc_mfthread_cv);
4793847Seh146360 	cv_destroy(&sc->sc_tx_cond);
4803847Seh146360 	cv_destroy(&sc->sc_cmd_cond);
4813847Seh146360 	cv_destroy(&sc->sc_fw_cond);
4823847Seh146360 fail4:
4833847Seh146360 	ipw2100_ring_free(sc);
4843847Seh146360 fail3:
4853847Seh146360 	ddi_regs_map_free(&sc->sc_ioh);
4863847Seh146360 fail2:
4873847Seh146360 	ddi_soft_state_free(ipw2100_ssp, instance);
4883847Seh146360 fail1:
4893847Seh146360 	return (err);
4903847Seh146360 }
4913847Seh146360 
4923847Seh146360 int
4933847Seh146360 ipw2100_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4943847Seh146360 {
4953847Seh146360 	struct ipw2100_softc	*sc =
4963847Seh146360 	    ddi_get_soft_state(ipw2100_ssp, ddi_get_instance(dip));
4973847Seh146360 	int err;
4983847Seh146360 
4993847Seh146360 	ASSERT(sc != NULL);
5003847Seh146360 
5013847Seh146360 	if (cmd != DDI_DETACH)
5023847Seh146360 		return (DDI_FAILURE);
5033847Seh146360 
5043847Seh146360 	/*
5053847Seh146360 	 * Destroy the mf_thread
5063847Seh146360 	 */
5073847Seh146360 	mutex_enter(&sc->sc_mflock);
5083847Seh146360 	sc->sc_mfthread_switch = 0;
5093847Seh146360 	while (sc->sc_mf_thread != NULL) {
5103847Seh146360 		if (cv_wait_sig(&sc->sc_mfthread_cv, &sc->sc_mflock) == 0)
5113847Seh146360 			break;
5123847Seh146360 	}
5133847Seh146360 	mutex_exit(&sc->sc_mflock);
5143847Seh146360 
5153847Seh146360 	/*
5163847Seh146360 	 * Unregiste from the MAC layer subsystem
5173847Seh146360 	 */
5183847Seh146360 	err = mac_unregister(sc->sc_ic.ic_mach);
5193847Seh146360 	if (err != DDI_SUCCESS)
5203847Seh146360 		return (err);
5213847Seh146360 
5223847Seh146360 	ddi_remove_intr(dip, 0, sc->sc_iblk);
5233847Seh146360 
5243847Seh146360 	/*
5253847Seh146360 	 * destroy the cv
5263847Seh146360 	 */
5273847Seh146360 	mutex_destroy(&sc->sc_ilock);
5283847Seh146360 	mutex_destroy(&sc->sc_tx_lock);
5293847Seh146360 	mutex_destroy(&sc->sc_mflock);
5303847Seh146360 	mutex_destroy(&sc->sc_resched_lock);
5313847Seh146360 	cv_destroy(&sc->sc_mfthread_cv);
5323847Seh146360 	cv_destroy(&sc->sc_tx_cond);
5333847Seh146360 	cv_destroy(&sc->sc_cmd_cond);
5343847Seh146360 	cv_destroy(&sc->sc_fw_cond);
5353847Seh146360 
5363847Seh146360 	/*
5373847Seh146360 	 * detach ieee80211
5383847Seh146360 	 */
5393847Seh146360 	ieee80211_detach(&sc->sc_ic);
5403847Seh146360 
5413847Seh146360 	(void) ipw2100_free_firmware(sc);
5423847Seh146360 	ipw2100_ring_free(sc);
5433847Seh146360 
5443847Seh146360 	ddi_regs_map_free(&sc->sc_ioh);
5453847Seh146360 	ddi_remove_minor_node(dip, NULL);
5463847Seh146360 	ddi_soft_state_free(ipw2100_ssp, ddi_get_instance(dip));
5473847Seh146360 
5483847Seh146360 	return (DDI_SUCCESS);
5493847Seh146360 }
5503847Seh146360 
551*4784Seh146360 /* ARGSUSED */
552*4784Seh146360 int
553*4784Seh146360 ipw2100_reset(dev_info_t *dip, ddi_reset_cmd_t cmd)
554*4784Seh146360 {
555*4784Seh146360 	struct ipw2100_softc	*sc =
556*4784Seh146360 	    ddi_get_soft_state(ipw2100_ssp, ddi_get_instance(dip));
557*4784Seh146360 	ASSERT(sc != NULL);
558*4784Seh146360 
559*4784Seh146360 	ipw2100_stop(sc);
560*4784Seh146360 
561*4784Seh146360 	return (DDI_SUCCESS);
562*4784Seh146360 }
563*4784Seh146360 
5643847Seh146360 static void
5653847Seh146360 ipw2100_tables_init(struct ipw2100_softc *sc)
5663847Seh146360 {
5673847Seh146360 	sc->sc_table1_base = ipw2100_csr_get32(sc, IPW2100_CSR_TABLE1_BASE);
5683847Seh146360 	sc->sc_table2_base = ipw2100_csr_get32(sc, IPW2100_CSR_TABLE2_BASE);
5693847Seh146360 }
5703847Seh146360 
5713847Seh146360 static void
5723847Seh146360 ipw2100_stop(struct ipw2100_softc *sc)
5733847Seh146360 {
5743847Seh146360 	struct ieee80211com	*ic = &sc->sc_ic;
5753847Seh146360 
5763847Seh146360 	ipw2100_master_stop(sc);
5773847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_RST, IPW2100_RST_SW_RESET);
5783847Seh146360 	sc->sc_flags &= ~IPW2100_FLAG_FW_INITED;
5793847Seh146360 
5803847Seh146360 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
5813847Seh146360 }
5823847Seh146360 
5833847Seh146360 static int
5843847Seh146360 ipw2100_config(struct ipw2100_softc *sc)
5853847Seh146360 {
5863847Seh146360 	struct ieee80211com		*ic = &sc->sc_ic;
5873847Seh146360 	struct ipw2100_security		sec;
5883847Seh146360 	struct ipw2100_wep_key		wkey;
5893847Seh146360 	struct ipw2100_scan_options	sopt;
5903847Seh146360 	struct ipw2100_configuration	cfg;
5913847Seh146360 	uint32_t			data;
5923847Seh146360 	int				err, i;
5933847Seh146360 
5943847Seh146360 	/*
5953847Seh146360 	 * operation mode
5963847Seh146360 	 */
5973847Seh146360 	switch (ic->ic_opmode) {
5983847Seh146360 	case IEEE80211_M_STA:
5993847Seh146360 	case IEEE80211_M_HOSTAP:
6003847Seh146360 		data = LE_32(IPW2100_MODE_BSS);
6013847Seh146360 		break;
6023847Seh146360 
6033847Seh146360 	case IEEE80211_M_IBSS:
6043847Seh146360 	case IEEE80211_M_AHDEMO:
6053847Seh146360 		data = LE_32(IPW2100_MODE_IBSS);
6063847Seh146360 		break;
6073847Seh146360 	}
6083847Seh146360 
6093847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
6103847Seh146360 	    "ipw2100_config(): Setting mode to %u\n", LE_32(data)));
6113847Seh146360 
6123847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_MODE,
6133847Seh146360 	    &data, sizeof (data));
6143847Seh146360 	if (err != DDI_SUCCESS)
6153847Seh146360 		return (err);
6163847Seh146360 
6173847Seh146360 	/*
6183847Seh146360 	 * operation channel if IBSS or MONITOR
6193847Seh146360 	 */
6203847Seh146360 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
6213847Seh146360 
6223847Seh146360 		data = LE_32(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
6233847Seh146360 
6243847Seh146360 		IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
6253847Seh146360 		    "ipw2100_config(): Setting channel to %u\n", LE_32(data)));
6263847Seh146360 
6273847Seh146360 		err = ipw2100_cmd(sc, IPW2100_CMD_SET_CHANNEL,
6283847Seh146360 		    &data, sizeof (data));
6293847Seh146360 		if (err != DDI_SUCCESS)
6303847Seh146360 			return (err);
6313847Seh146360 	}
6323847Seh146360 
6333847Seh146360 	/*
6343847Seh146360 	 * set MAC address
6353847Seh146360 	 */
6363847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
6373847Seh146360 	    "ipw2100_config(): Setting MAC address to "
6383847Seh146360 	    "%02x:%02x:%02x:%02x:%02x:%02x\n",
6393847Seh146360 	    ic->ic_macaddr[0], ic->ic_macaddr[1], ic->ic_macaddr[2],
6403847Seh146360 	    ic->ic_macaddr[3], ic->ic_macaddr[4], ic->ic_macaddr[5]));
6413847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_MAC_ADDRESS, ic->ic_macaddr,
6423847Seh146360 	    IEEE80211_ADDR_LEN);
6433847Seh146360 	if (err != DDI_SUCCESS)
6443847Seh146360 		return (err);
6453847Seh146360 
6463847Seh146360 	/*
6473847Seh146360 	 * configuration capabilities
6483847Seh146360 	 */
6493847Seh146360 	cfg.flags = IPW2100_CFG_BSS_MASK | IPW2100_CFG_IBSS_MASK |
6503847Seh146360 	    IPW2100_CFG_PREAMBLE_AUTO | IPW2100_CFG_802_1x_ENABLE;
6513847Seh146360 	if (ic->ic_opmode == IEEE80211_M_IBSS)
6523847Seh146360 		cfg.flags |= IPW2100_CFG_IBSS_AUTO_START;
6533847Seh146360 	if (sc->if_flags & IFF_PROMISC)
6543847Seh146360 		cfg.flags |= IPW2100_CFG_PROMISCUOUS;
6553847Seh146360 	cfg.flags	= LE_32(cfg.flags);
6563847Seh146360 	cfg.bss_chan	= LE_32(sc->sc_chmask >> 1);
6573847Seh146360 	cfg.ibss_chan	= LE_32(sc->sc_chmask >> 1);
6583847Seh146360 
6593847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
6603847Seh146360 	    "ipw2100_config(): Setting configuration to 0x%x\n",
6613847Seh146360 	    LE_32(cfg.flags)));
6623847Seh146360 
6633847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_CONFIGURATION,
6643847Seh146360 	    &cfg, sizeof (cfg));
6653847Seh146360 
6663847Seh146360 	if (err != DDI_SUCCESS)
6673847Seh146360 		return (err);
6683847Seh146360 
6693847Seh146360 	/*
6703847Seh146360 	 * set 802.11 Tx rates
6713847Seh146360 	 */
6723847Seh146360 	data = LE_32(0x3);  /* 1, 2 */
6733847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
6743847Seh146360 	    "ipw2100_config(): Setting 802.11 Tx rates to 0x%x\n",
6753847Seh146360 	    LE_32(data)));
6763847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_BASIC_TX_RATES,
6773847Seh146360 	    &data, sizeof (data));
6783847Seh146360 	if (err != DDI_SUCCESS)
6793847Seh146360 		return (err);
6803847Seh146360 
6813847Seh146360 	/*
6823847Seh146360 	 * set 802.11b Tx rates
6833847Seh146360 	 */
6843847Seh146360 	data = LE_32(0xf);  /* 1, 2, 5.5, 11 */
6853847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
6863847Seh146360 	    "ipw2100_config(): Setting 802.11b Tx rates to 0x%x\n",
6873847Seh146360 	    LE_32(data)));
6883847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_TX_RATES, &data, sizeof (data));
6893847Seh146360 	if (err != DDI_SUCCESS)
6903847Seh146360 		return (err);
6913847Seh146360 
6923847Seh146360 	/*
6933847Seh146360 	 * set power mode
6943847Seh146360 	 */
6953847Seh146360 	data = LE_32(IPW2100_POWER_MODE_CAM);
6963847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
6973847Seh146360 	    "ipw2100_config(): Setting power mode to %u\n", LE_32(data)));
6983847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_POWER_MODE, &data, sizeof (data));
6993847Seh146360 	if (err != DDI_SUCCESS)
7003847Seh146360 		return (err);
7013847Seh146360 
7023847Seh146360 	/*
7033847Seh146360 	 * set power index
7043847Seh146360 	 */
7053847Seh146360 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
7063847Seh146360 		data = LE_32(32);
7073847Seh146360 		IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
7083847Seh146360 		    "ipw2100_config(): Setting Tx power index to %u\n",
7093847Seh146360 		    LE_32(data)));
7103847Seh146360 		err = ipw2100_cmd(sc, IPW2100_CMD_SET_TX_POWER_INDEX,
7113847Seh146360 		    &data, sizeof (data));
7123847Seh146360 		if (err != DDI_SUCCESS)
7133847Seh146360 			return (err);
7143847Seh146360 	}
7153847Seh146360 
7163847Seh146360 	/*
7173847Seh146360 	 * set RTS threshold
7183847Seh146360 	 */
7193847Seh146360 	ic->ic_rtsthreshold = 2346;
7203847Seh146360 	data = LE_32(ic->ic_rtsthreshold);
7213847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
7223847Seh146360 	    "ipw2100_config(): Setting RTS threshold to %u\n", LE_32(data)));
7233847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_RTS_THRESHOLD,
7243847Seh146360 	    &data, sizeof (data));
7253847Seh146360 	if (err != DDI_SUCCESS)
7263847Seh146360 		return (err);
7273847Seh146360 
7283847Seh146360 	/*
7293847Seh146360 	 * set frag threshold
7303847Seh146360 	 */
7313847Seh146360 	ic->ic_fragthreshold = 2346;
7323847Seh146360 	data = LE_32(ic->ic_fragthreshold);
7333847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
7343847Seh146360 	    "ipw2100_config(): Setting frag threshold to %u\n", LE_32(data)));
7353847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_FRAG_THRESHOLD,
7363847Seh146360 	    &data, sizeof (data));
7373847Seh146360 	if (err != DDI_SUCCESS)
7383847Seh146360 		return (err);
7393847Seh146360 
7403847Seh146360 	/*
7413847Seh146360 	 * set ESSID
7423847Seh146360 	 */
7433847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
7443847Seh146360 	    "ipw2100_config(): Setting ESSID to %u, ESSID[0]%c\n",
7453847Seh146360 	    ic->ic_des_esslen, ic->ic_des_essid[0]));
7463847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_ESSID,
7473847Seh146360 	    ic->ic_des_essid, ic->ic_des_esslen);
7483847Seh146360 	if (err != DDI_SUCCESS)
7493847Seh146360 		return (err);
7503847Seh146360 
7513847Seh146360 	/*
7523847Seh146360 	 * no mandatory BSSID
7533847Seh146360 	 */
7543847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_MANDATORY_BSSID, NULL, 0);
7553847Seh146360 	if (err != DDI_SUCCESS)
7563847Seh146360 		return (err);
7573847Seh146360 
7583847Seh146360 	/*
7593847Seh146360 	 * set BSSID, if any
7603847Seh146360 	 */
7613847Seh146360 	if (ic->ic_flags & IEEE80211_F_DESBSSID) {
7623847Seh146360 		IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
7633847Seh146360 		    "ipw2100_config(): Setting BSSID to %u\n",
7643847Seh146360 		    IEEE80211_ADDR_LEN));
7653847Seh146360 		err = ipw2100_cmd(sc, IPW2100_CMD_SET_DESIRED_BSSID,
7663847Seh146360 		    ic->ic_des_bssid, IEEE80211_ADDR_LEN);
7673847Seh146360 		if (err != DDI_SUCCESS)
7683847Seh146360 			return (err);
7693847Seh146360 	}
7703847Seh146360 
7713847Seh146360 	/*
7723847Seh146360 	 * set security information
7733847Seh146360 	 */
7743847Seh146360 	(void) memset(&sec, 0, sizeof (sec));
7753847Seh146360 	/*
7763847Seh146360 	 * use the value set to ic_bss to retrieve current sharedmode
7773847Seh146360 	 */
7783847Seh146360 	sec.authmode = (ic->ic_bss->in_authmode == WL_SHAREDKEY) ?
7793847Seh146360 	    IPW2100_AUTH_SHARED : IPW2100_AUTH_OPEN;
7803847Seh146360 	sec.ciphers = LE_32(IPW2100_CIPHER_NONE);
7813847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
7823847Seh146360 	    "ipw2100_config(): Setting authmode to %u\n", sec.authmode));
7833847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_SECURITY_INFORMATION,
7843847Seh146360 	    &sec, sizeof (sec));
7853847Seh146360 	if (err != DDI_SUCCESS)
7863847Seh146360 		return (err);
7873847Seh146360 
7883847Seh146360 	/*
7893847Seh146360 	 * set WEP if any
7903847Seh146360 	 */
7913847Seh146360 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
7923847Seh146360 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
7933847Seh146360 			if (ic->ic_nw_keys[i].wk_keylen == 0)
7943847Seh146360 				continue;
7953847Seh146360 			wkey.idx = (uint8_t)i;
7963847Seh146360 			wkey.len = ic->ic_nw_keys[i].wk_keylen;
7973847Seh146360 			(void) memset(wkey.key, 0, sizeof (wkey.key));
7983847Seh146360 			if (ic->ic_nw_keys[i].wk_keylen)
7993847Seh146360 				(void) memcpy(wkey.key,
8003847Seh146360 				    ic->ic_nw_keys[i].wk_key,
8013847Seh146360 				    ic->ic_nw_keys[i].wk_keylen);
8023847Seh146360 			err = ipw2100_cmd(sc, IPW2100_CMD_SET_WEP_KEY,
8033847Seh146360 			    &wkey, sizeof (wkey));
8043847Seh146360 			if (err != DDI_SUCCESS)
8053847Seh146360 				return (err);
8063847Seh146360 		}
8073847Seh146360 		data = LE_32(ic->ic_def_txkey);
8083847Seh146360 		err = ipw2100_cmd(sc, IPW2100_CMD_SET_WEP_KEY_INDEX,
8093847Seh146360 		    &data, sizeof (data));
8103847Seh146360 		if (err != DDI_SUCCESS)
8113847Seh146360 			return (err);
8123847Seh146360 	}
8133847Seh146360 
8143847Seh146360 	/*
8153847Seh146360 	 * turn on WEP
8163847Seh146360 	 */
8173847Seh146360 	data = LE_32((ic->ic_flags & IEEE80211_F_PRIVACY) ? 0x8 : 0);
8183847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
8193847Seh146360 	    "ipw2100_config(): Setting WEP flags to %u\n", LE_32(data)));
8203847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_WEP_FLAGS, &data, sizeof (data));
8213847Seh146360 	if (err != DDI_SUCCESS)
8223847Seh146360 		return (err);
8233847Seh146360 
8243847Seh146360 	/*
8253847Seh146360 	 * set beacon interval if IBSS or HostAP
8263847Seh146360 	 */
8273847Seh146360 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
8283847Seh146360 	    ic->ic_opmode == IEEE80211_M_HOSTAP) {
8293847Seh146360 
8303847Seh146360 		data = LE_32(ic->ic_lintval);
8313847Seh146360 		IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
8323847Seh146360 		    "ipw2100_config(): Setting beacon interval to %u\n",
8333847Seh146360 		    LE_32(data)));
8343847Seh146360 		err = ipw2100_cmd(sc, IPW2100_CMD_SET_BEACON_INTERVAL,
8353847Seh146360 		    &data, sizeof (data));
8363847Seh146360 		if (err != DDI_SUCCESS)
8373847Seh146360 			return (err);
8383847Seh146360 	}
8393847Seh146360 
8403847Seh146360 	/*
8413847Seh146360 	 * set scan options
8423847Seh146360 	 */
8433847Seh146360 	sopt.flags = LE_32(0);
8443847Seh146360 	sopt.channels = LE_32(sc->sc_chmask >> 1);
8453847Seh146360 	err = ipw2100_cmd(sc, IPW2100_CMD_SET_SCAN_OPTIONS,
8463847Seh146360 	    &sopt, sizeof (sopt));
8473847Seh146360 	if (err != DDI_SUCCESS)
8483847Seh146360 		return (err);
8493847Seh146360 
8503847Seh146360 en_adapter:
8513847Seh146360 
8523847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
8533847Seh146360 	    "ipw2100_config(): Enabling adapter\n"));
8543847Seh146360 
8553847Seh146360 	return (ipw2100_cmd(sc, IPW2100_CMD_ENABLE, NULL, 0));
8563847Seh146360 }
8573847Seh146360 
8583847Seh146360 static int
8593847Seh146360 ipw2100_cmd(struct ipw2100_softc *sc, uint32_t type, void *buf, size_t len)
8603847Seh146360 {
8613847Seh146360 	struct ipw2100_bd	*txbd;
8623847Seh146360 	clock_t			clk;
8633847Seh146360 	uint32_t		idx;
8643847Seh146360 
8653847Seh146360 	/*
8663847Seh146360 	 * prepare command buffer
8673847Seh146360 	 */
8683847Seh146360 	sc->sc_cmd->type = LE_32(type);
8693847Seh146360 	sc->sc_cmd->subtype = LE_32(0);
8703847Seh146360 	sc->sc_cmd->seq = LE_32(0);
8713847Seh146360 	/*
8723847Seh146360 	 * copy data if any
8733847Seh146360 	 */
8743847Seh146360 	if (len && buf)
8753847Seh146360 		(void) memcpy(sc->sc_cmd->data, buf, len);
8763847Seh146360 	sc->sc_cmd->len = LE_32(len);
8773847Seh146360 
8783847Seh146360 	/*
8793847Seh146360 	 * get host & device descriptor to submit command
8803847Seh146360 	 */
8813847Seh146360 	mutex_enter(&sc->sc_tx_lock);
8823847Seh146360 
8833847Seh146360 	IPW2100_DBG(IPW2100_DBG_RING, (sc->sc_dip, CE_CONT,
8843847Seh146360 	    "ipw2100_cmd(): tx-free=%d\n", sc->sc_tx_free));
8853847Seh146360 
8863847Seh146360 	/*
8873847Seh146360 	 * command need 1 descriptor
8883847Seh146360 	 */
8893847Seh146360 	while (sc->sc_tx_free < 1)  {
8903847Seh146360 		sc->sc_flags |= IPW2100_FLAG_CMD_WAIT;
8913847Seh146360 		cv_wait(&sc->sc_tx_cond, &sc->sc_tx_lock);
8923847Seh146360 	}
8933847Seh146360 	idx = sc->sc_tx_cur;
8943847Seh146360 
8953847Seh146360 	IPW2100_DBG(IPW2100_DBG_RING, (sc->sc_dip, CE_CONT,
8963847Seh146360 	    "ipw2100_cmd(): tx-cur=%d\n", idx));
8973847Seh146360 
8983847Seh146360 	sc->sc_done = 0;
8993847Seh146360 
9003847Seh146360 	txbd		= &sc->sc_txbd[idx];
9013847Seh146360 	txbd->phyaddr	= LE_32(sc->sc_dma_cmd.dr_pbase);
9023847Seh146360 	txbd->len	= LE_32(sizeof (struct ipw2100_cmd));
9033847Seh146360 	txbd->flags	= IPW2100_BD_FLAG_TX_FRAME_COMMAND
9043847Seh146360 	    | IPW2100_BD_FLAG_TX_LAST_FRAGMENT;
9053847Seh146360 	txbd->nfrag	= 1;
9063847Seh146360 	/*
9073847Seh146360 	 * sync for device
9083847Seh146360 	 */
9093847Seh146360 	(void) ddi_dma_sync(sc->sc_dma_cmd.dr_hnd, 0,
9103847Seh146360 	    sizeof (struct ipw2100_cmd), DDI_DMA_SYNC_FORDEV);
9113847Seh146360 	(void) ddi_dma_sync(sc->sc_dma_txbd.dr_hnd,
9123847Seh146360 	    idx * sizeof (struct ipw2100_bd),
9133847Seh146360 	    sizeof (struct ipw2100_bd), DDI_DMA_SYNC_FORDEV);
9143847Seh146360 
9153847Seh146360 	/*
9163847Seh146360 	 * ring move forward
9173847Seh146360 	 */
9183847Seh146360 	sc->sc_tx_cur = RING_FORWARD(sc->sc_tx_cur, 1, IPW2100_NUM_TXBD);
9193847Seh146360 	sc->sc_tx_free--;
9203847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_TX_WRITE_INDEX, sc->sc_tx_cur);
9213847Seh146360 	mutex_exit(&sc->sc_tx_lock);
9223847Seh146360 
9233847Seh146360 	/*
9243847Seh146360 	 * wait for command done
9253847Seh146360 	 */
9263847Seh146360 	mutex_enter(&sc->sc_ilock);
9273847Seh146360 	while (sc->sc_done == 0) {
9283847Seh146360 		/*
9293847Seh146360 		 * pending for the response
9303847Seh146360 		 */
9313847Seh146360 		clk = ddi_get_lbolt() + drv_usectohz(1000000);  /* 1 second */
9323847Seh146360 		if (cv_timedwait(&sc->sc_cmd_cond, &sc->sc_ilock, clk) < 0)
9333847Seh146360 			break;
9343847Seh146360 	}
9353847Seh146360 	mutex_exit(&sc->sc_ilock);
9363847Seh146360 
9373847Seh146360 	IPW2100_DBG(IPW2100_DBG_RING, (sc->sc_dip, CE_CONT,
9383847Seh146360 	    "ipw2100_cmd(): cmd-done=%s\n", sc->sc_done ? "yes" : "no"));
9393847Seh146360 
9403847Seh146360 	if (sc->sc_done == 0)
9413847Seh146360 		return (DDI_FAILURE);
9423847Seh146360 
9433847Seh146360 	return (DDI_SUCCESS);
9443847Seh146360 }
9453847Seh146360 
9463847Seh146360 int
9473847Seh146360 ipw2100_init(struct ipw2100_softc *sc)
9483847Seh146360 {
9493847Seh146360 	int	err;
9503847Seh146360 
9513847Seh146360 	/*
9523847Seh146360 	 * no firmware is available, return fail directly
9533847Seh146360 	 */
9543847Seh146360 	if (!(sc->sc_flags & IPW2100_FLAG_FW_CACHED)) {
9553847Seh146360 		IPW2100_WARN((sc->sc_dip, CE_WARN,
9563847Seh146360 		    "ipw2100_init(): no firmware is available\n"));
9573847Seh146360 		return (DDI_FAILURE);
9583847Seh146360 	}
9593847Seh146360 
9603847Seh146360 	ipw2100_stop(sc);
9613847Seh146360 
9623847Seh146360 	err = ipw2100_chip_reset(sc);
9633847Seh146360 	if (err != DDI_SUCCESS) {
9643847Seh146360 		IPW2100_WARN((sc->sc_dip, CE_WARN,
9653847Seh146360 		    "ipw2100_init(): could not reset adapter\n"));
9663847Seh146360 		goto fail;
9673847Seh146360 	}
9683847Seh146360 
9693847Seh146360 	/*
9703847Seh146360 	 * load microcode
9713847Seh146360 	 */
9723847Seh146360 	err = ipw2100_load_uc(sc);
9733847Seh146360 	if (err != DDI_SUCCESS) {
9743847Seh146360 		IPW2100_WARN((sc->sc_dip, CE_WARN,
9753847Seh146360 		    "ipw2100_init(): could not load microcode, try again\n"));
9763847Seh146360 		goto fail;
9773847Seh146360 	}
9783847Seh146360 
9793847Seh146360 	ipw2100_master_stop(sc);
9803847Seh146360 
9813847Seh146360 	ipw2100_ring_hwsetup(sc);
9823847Seh146360 
9833847Seh146360 	/*
9843847Seh146360 	 * load firmware
9853847Seh146360 	 */
9863847Seh146360 	err = ipw2100_load_fw(sc);
9873847Seh146360 	if (err != DDI_SUCCESS) {
9883847Seh146360 		IPW2100_WARN((sc->sc_dip, CE_WARN,
9893847Seh146360 		    "ipw2100_init(): could not load firmware, try again\n"));
9903847Seh146360 		goto fail;
9913847Seh146360 	}
9923847Seh146360 
9933847Seh146360 	/*
9943847Seh146360 	 * initialize tables
9953847Seh146360 	 */
9963847Seh146360 	ipw2100_tables_init(sc);
9973847Seh146360 	ipw2100_table1_put32(sc, IPW2100_INFO_LOCK, 0);
9983847Seh146360 
9993847Seh146360 	/*
10003847Seh146360 	 * Hardware will be enabled after configuration
10013847Seh146360 	 */
10023847Seh146360 	err = ipw2100_config(sc);
10033847Seh146360 	if (err != DDI_SUCCESS) {
10043847Seh146360 		IPW2100_WARN((sc->sc_dip, CE_WARN,
10053847Seh146360 		    "ipw2100_init(): device configuration failed\n"));
10063847Seh146360 		goto fail;
10073847Seh146360 	}
10083847Seh146360 
10093847Seh146360 	delay(drv_usectohz(delay_config_stable));
10103847Seh146360 
10113847Seh146360 	return (DDI_SUCCESS);
10123847Seh146360 
10133847Seh146360 fail:
10143847Seh146360 	ipw2100_stop(sc);
10153847Seh146360 
10163847Seh146360 	return (err);
10173847Seh146360 }
10183847Seh146360 
10193847Seh146360 /*
10203847Seh146360  * get hardware configurations from EEPROM embedded within chip
10213847Seh146360  */
10223847Seh146360 static void
10233847Seh146360 ipw2100_hwconf_get(struct ipw2100_softc *sc)
10243847Seh146360 {
10253847Seh146360 	int		i;
10263847Seh146360 	uint16_t	val;
10273847Seh146360 
10283847Seh146360 	/*
10293847Seh146360 	 * MAC address
10303847Seh146360 	 */
10313847Seh146360 	i = 0;
10323847Seh146360 	val = ipw2100_rom_get16(sc, IPW2100_ROM_MAC + 0);
10333847Seh146360 	sc->sc_macaddr[i++] = val >> 8;
10343847Seh146360 	sc->sc_macaddr[i++] = val & 0xff;
10353847Seh146360 	val = ipw2100_rom_get16(sc, IPW2100_ROM_MAC + 1);
10363847Seh146360 	sc->sc_macaddr[i++] = val >> 8;
10373847Seh146360 	sc->sc_macaddr[i++] = val & 0xff;
10383847Seh146360 	val = ipw2100_rom_get16(sc, IPW2100_ROM_MAC + 2);
10393847Seh146360 	sc->sc_macaddr[i++] = val >> 8;
10403847Seh146360 	sc->sc_macaddr[i++] = val & 0xff;
10413847Seh146360 
10423847Seh146360 	/*
10433847Seh146360 	 * formatted MAC address string
10443847Seh146360 	 */
10453847Seh146360 	(void) snprintf(sc->sc_macstr, sizeof (sc->sc_macstr),
1046*4784Seh146360 	    "%02x:%02x:%02x:%02x:%02x:%02x",
1047*4784Seh146360 	    sc->sc_macaddr[0], sc->sc_macaddr[1],
1048*4784Seh146360 	    sc->sc_macaddr[2], sc->sc_macaddr[3],
1049*4784Seh146360 	    sc->sc_macaddr[4], sc->sc_macaddr[5]);
10503847Seh146360 
10513847Seh146360 	/*
10523847Seh146360 	 * channel mask
10533847Seh146360 	 */
10543847Seh146360 	val = ipw2100_rom_get16(sc, IPW2100_ROM_CHANNEL_LIST);
10553847Seh146360 	if (val == 0)
10563847Seh146360 		val = 0x7ff;
10573847Seh146360 	sc->sc_chmask = val << 1;
10583847Seh146360 	IPW2100_DBG(IPW2100_DBG_HWCAP, (sc->sc_dip, CE_CONT,
10593847Seh146360 	    "ipw2100_hwconf_get(): channel-mask=0x%08x\n", sc->sc_chmask));
10603847Seh146360 
10613847Seh146360 	/*
10623847Seh146360 	 * radio switch
10633847Seh146360 	 */
10643847Seh146360 	val = ipw2100_rom_get16(sc, IPW2100_ROM_RADIO);
10653847Seh146360 	if (val & 0x08)
10663847Seh146360 		sc->sc_flags |= IPW2100_FLAG_HAS_RADIO_SWITCH;
10673847Seh146360 
10683847Seh146360 	IPW2100_DBG(IPW2100_DBG_HWCAP, (sc->sc_dip, CE_CONT,
10693847Seh146360 	    "ipw2100_hwconf_get(): has-radio-switch=%s(%u)\n",
10703847Seh146360 	    (sc->sc_flags & IPW2100_FLAG_HAS_RADIO_SWITCH)?  "yes" : "no",
10713847Seh146360 	    val));
10723847Seh146360 }
10733847Seh146360 
10743847Seh146360 /*
10753847Seh146360  * all ipw2100 interrupts will be masked by this routine
10763847Seh146360  */
10773847Seh146360 static void
10783847Seh146360 ipw2100_master_stop(struct ipw2100_softc *sc)
10793847Seh146360 {
10803847Seh146360 	uint32_t	tmp;
10813847Seh146360 	int		ntries;
10823847Seh146360 
10833847Seh146360 	/*
10843847Seh146360 	 * disable interrupts
10853847Seh146360 	 */
10863847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_INTR_MASK, 0);
10873847Seh146360 
10883847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_RST, IPW2100_RST_STOP_MASTER);
10893847Seh146360 	for (ntries = 0; ntries < 50; ntries++) {
10903847Seh146360 		if (ipw2100_csr_get32(sc, IPW2100_CSR_RST)
10913847Seh146360 		    & IPW2100_RST_MASTER_DISABLED)
10923847Seh146360 			break;
10933847Seh146360 		drv_usecwait(10);
10943847Seh146360 	}
10953847Seh146360 	if (ntries == 50)
10963847Seh146360 		IPW2100_WARN((sc->sc_dip, CE_WARN,
10973847Seh146360 		    "ipw2100_master_stop(): timeout when stop master\n"));
10983847Seh146360 
10993847Seh146360 	tmp = ipw2100_csr_get32(sc, IPW2100_CSR_RST);
11003847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_RST,
11013847Seh146360 	    tmp | IPW2100_RST_PRINCETON_RESET);
11023847Seh146360 
11033847Seh146360 	sc->sc_flags &= ~IPW2100_FLAG_FW_INITED;
11043847Seh146360 }
11053847Seh146360 
11063847Seh146360 /*
11073847Seh146360  * all ipw2100 interrupts will be masked by this routine
11083847Seh146360  */
11093847Seh146360 static int
11103847Seh146360 ipw2100_chip_reset(struct ipw2100_softc *sc)
11113847Seh146360 {
11123847Seh146360 	int		ntries;
11133847Seh146360 	uint32_t	tmp;
11143847Seh146360 
11153847Seh146360 	ipw2100_master_stop(sc);
11163847Seh146360 
11173847Seh146360 	/*
11183847Seh146360 	 * move adatper to DO state
11193847Seh146360 	 */
11203847Seh146360 	tmp = ipw2100_csr_get32(sc, IPW2100_CSR_CTL);
11213847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_CTL, tmp | IPW2100_CTL_INIT);
11223847Seh146360 
11233847Seh146360 	/*
11243847Seh146360 	 * wait for clock stabilization
11253847Seh146360 	 */
11263847Seh146360 	for (ntries = 0; ntries < 1000; ntries++) {
11273847Seh146360 		if (ipw2100_csr_get32(sc, IPW2100_CSR_CTL)
11283847Seh146360 		    & IPW2100_CTL_CLOCK_READY)
11293847Seh146360 			break;
11303847Seh146360 		drv_usecwait(200);
11313847Seh146360 	}
11323847Seh146360 	if (ntries == 1000)
11333847Seh146360 		return (DDI_FAILURE);
11343847Seh146360 
11353847Seh146360 	tmp = ipw2100_csr_get32(sc, IPW2100_CSR_RST);
11363847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_RST, tmp | IPW2100_RST_SW_RESET);
11373847Seh146360 
11383847Seh146360 	drv_usecwait(10);
11393847Seh146360 
11403847Seh146360 	tmp = ipw2100_csr_get32(sc, IPW2100_CSR_CTL);
11413847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_CTL, tmp | IPW2100_CTL_INIT);
11423847Seh146360 
11433847Seh146360 	return (DDI_SUCCESS);
11443847Seh146360 }
11453847Seh146360 
11463847Seh146360 /*
11473847Seh146360  * get the radio status from IPW_CSR_IO, invoked by wificonfig/dladm
11483847Seh146360  */
11493847Seh146360 int
11503847Seh146360 ipw2100_get_radio(struct ipw2100_softc *sc)
11513847Seh146360 {
11523847Seh146360 	if (ipw2100_csr_get32(sc, IPW2100_CSR_IO) & IPW2100_IO_RADIO_DISABLED)
11533847Seh146360 		return (0);
11543847Seh146360 	else
11553847Seh146360 		return (1);
11563847Seh146360 
11573847Seh146360 }
11583847Seh146360 /*
11593847Seh146360  * This function is used to get the statistic, invoked by wificonfig/dladm
11603847Seh146360  */
11613847Seh146360 void
11623847Seh146360 ipw2100_get_statistics(struct ipw2100_softc *sc)
11633847Seh146360 {
11643847Seh146360 	struct ieee80211com	*ic = &sc->sc_ic;
11653847Seh146360 	uint32_t		addr, size, i;
11663847Seh146360 	uint32_t		atbl[256], *datatbl;
11673847Seh146360 
11683847Seh146360 	datatbl = atbl;
11693847Seh146360 
11703847Seh146360 	if (!(sc->sc_flags & IPW2100_FLAG_FW_INITED)) {
11713847Seh146360 		IPW2100_DBG(IPW2100_DBG_STATISTIC, (sc->sc_dip, CE_CONT,
11723847Seh146360 		    "ipw2100_get_statistic(): fw doesn't download yet."));
11733847Seh146360 		return;
11743847Seh146360 	}
11753847Seh146360 
11763847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_AUTOINC_ADDR, sc->sc_table1_base);
11773847Seh146360 
11783847Seh146360 	size = ipw2100_csr_get32(sc, IPW2100_CSR_AUTOINC_DATA);
11793847Seh146360 	atbl[0] = size;
11803847Seh146360 	for (i = 1, ++datatbl; i < size; i++, datatbl++) {
11813847Seh146360 		addr = ipw2100_csr_get32(sc, IPW2100_CSR_AUTOINC_DATA);
11823847Seh146360 		*datatbl = ipw2100_imem_get32(sc, addr);
11833847Seh146360 	}
11843847Seh146360 
11853847Seh146360 	/*
11863847Seh146360 	 * To retrieve the statistic information into proper places. There are
11873847Seh146360 	 * lot of information.
11883847Seh146360 	 */
11893847Seh146360 	IPW2100_DBG(IPW2100_DBG_STATISTIC, (sc->sc_dip, CE_CONT,
11903847Seh146360 	    "ipw2100_get_statistic(): \n"
11913847Seh146360 	    "operating mode = %u\n"
11923847Seh146360 	    "type of authentification= %u\n"
11933847Seh146360 	    "average RSSI= %u\n"
11943847Seh146360 	    "current channel = %d\n",
11953847Seh146360 	    atbl[191], atbl[199], atbl[173], atbl[189]));
11963847Seh146360 	/* WIFI_STAT_TX_FRAGS */
11973847Seh146360 	ic->ic_stats.is_tx_frags = (uint32_t)atbl[2];
11983847Seh146360 	/* WIFI_STAT_MCAST_TX = (all frame - unicast frame) */
11993847Seh146360 	ic->ic_stats.is_tx_mcast = (uint32_t)atbl[2] - (uint32_t)atbl[3];
12003847Seh146360 	/* WIFI_STAT_TX_RETRANS */
12013847Seh146360 	ic->ic_stats.is_tx_retries = (uint32_t)atbl[42];
12023847Seh146360 	/* WIFI_STAT_TX_FAILED */
12033847Seh146360 	ic->ic_stats.is_tx_failed = (uint32_t)atbl[51];
12043847Seh146360 	/* MAC_STAT_OBYTES */
12053847Seh146360 	ic->ic_stats.is_tx_bytes = (uint32_t)atbl[41];
12063847Seh146360 	/* WIFI_STAT_RX_FRAGS */
12073847Seh146360 	ic->ic_stats.is_rx_frags = (uint32_t)atbl[61];
12083847Seh146360 	/* WIFI_STAT_MCAST_RX */
12093847Seh146360 	ic->ic_stats.is_rx_mcast = (uint32_t)atbl[71];
12103847Seh146360 	/* MAC_STAT_IBYTES */
12113847Seh146360 	ic->ic_stats.is_rx_bytes = (uint32_t)atbl[101];
12123847Seh146360 	/* WIFI_STAT_ACK_FAILURE */
12133847Seh146360 	ic->ic_stats.is_ack_failure = (uint32_t)atbl[59];
12143847Seh146360 	/* WIFI_STAT_RTS_SUCCESS */
12153847Seh146360 	ic->ic_stats.is_rts_success = (uint32_t)atbl[22];
12163847Seh146360 }
12173847Seh146360 
12183847Seh146360 /*
12193847Seh146360  * dma region alloc
12203847Seh146360  */
12213847Seh146360 static int
12223847Seh146360 ipw2100_dma_region_alloc(struct ipw2100_softc *sc,
12233847Seh146360     struct dma_region *dr, size_t size, uint_t dir, uint_t flags)
12243847Seh146360 {
12253847Seh146360 	dev_info_t	*dip = sc->sc_dip;
12263847Seh146360 	int		err;
12273847Seh146360 
12283847Seh146360 	IPW2100_DBG(IPW2100_DBG_DMA, (dip, CE_CONT,
12293847Seh146360 	    "ipw2100_dma_region_alloc() name=%s size=%u\n",
12303847Seh146360 	    dr->dr_name, size));
12313847Seh146360 
12323847Seh146360 	err = ddi_dma_alloc_handle(dip, &ipw2100_dma_attr, DDI_DMA_SLEEP, NULL,
12333847Seh146360 	    &dr->dr_hnd);
12343847Seh146360 	if (err != DDI_SUCCESS) {
12353847Seh146360 		IPW2100_DBG(IPW2100_DBG_DMA, (dip, CE_CONT,
12363847Seh146360 		    "ipw2100_dma_region_alloc(): "
12373847Seh146360 		    "ddi_dma_alloc_handle() failed\n"));
12383847Seh146360 		goto fail0;
12393847Seh146360 	}
12403847Seh146360 
12413847Seh146360 	err = ddi_dma_mem_alloc(dr->dr_hnd, size, &ipw2100_dma_accattr,
12423847Seh146360 	    flags, DDI_DMA_SLEEP, NULL, &dr->dr_base,
12433847Seh146360 	    &dr->dr_size, &dr->dr_acc);
12443847Seh146360 	if (err != DDI_SUCCESS) {
12453847Seh146360 		IPW2100_DBG(IPW2100_DBG_DMA, (dip, CE_CONT,
12463847Seh146360 		    "ipw2100_dma_region_alloc(): "
12473847Seh146360 		    "ddi_dma_mem_alloc() failed\n"));
12483847Seh146360 		goto fail1;
12493847Seh146360 	}
12503847Seh146360 
12513847Seh146360 	err = ddi_dma_addr_bind_handle(dr->dr_hnd, NULL,
12523847Seh146360 	    dr->dr_base, dr->dr_size, dir | flags, DDI_DMA_SLEEP, NULL,
12533847Seh146360 	    &dr->dr_cookie, &dr->dr_ccnt);
12543847Seh146360 	if (err != DDI_DMA_MAPPED) {
12553847Seh146360 		IPW2100_DBG(IPW2100_DBG_DMA, (dip, CE_CONT,
12563847Seh146360 		    "ipw2100_dma_region_alloc(): "
12573847Seh146360 		    "ddi_dma_addr_bind_handle() failed\n"));
12583847Seh146360 		goto fail2;
12593847Seh146360 	}
12603847Seh146360 
12613847Seh146360 	if (dr->dr_ccnt != 1) {
12623847Seh146360 		err = DDI_FAILURE;
12633847Seh146360 		goto fail3;
12643847Seh146360 	}
12653847Seh146360 	dr->dr_pbase = dr->dr_cookie.dmac_address;
12663847Seh146360 
12673847Seh146360 	IPW2100_DBG(IPW2100_DBG_DMA, (dip, CE_CONT,
12683847Seh146360 	    "ipw2100_dma_region_alloc(): get physical-base=0x%08x\n",
12693847Seh146360 	    dr->dr_pbase));
12703847Seh146360 
12713847Seh146360 	return (DDI_SUCCESS);
12723847Seh146360 
12733847Seh146360 fail3:
12743847Seh146360 	(void) ddi_dma_unbind_handle(dr->dr_hnd);
12753847Seh146360 fail2:
12763847Seh146360 	ddi_dma_mem_free(&dr->dr_acc);
12773847Seh146360 fail1:
12783847Seh146360 	ddi_dma_free_handle(&dr->dr_hnd);
12793847Seh146360 fail0:
12803847Seh146360 	return (err);
12813847Seh146360 }
12823847Seh146360 
12833847Seh146360 static void
12843847Seh146360 ipw2100_dma_region_free(struct dma_region *dr)
12853847Seh146360 {
12863847Seh146360 	(void) ddi_dma_unbind_handle(dr->dr_hnd);
12873847Seh146360 	ddi_dma_mem_free(&dr->dr_acc);
12883847Seh146360 	ddi_dma_free_handle(&dr->dr_hnd);
12893847Seh146360 }
12903847Seh146360 
12913847Seh146360 static int
12923847Seh146360 ipw2100_ring_alloc(struct ipw2100_softc *sc)
12933847Seh146360 {
12943847Seh146360 	int	err, i;
12953847Seh146360 
12963847Seh146360 	/*
12973847Seh146360 	 * tx ring
12983847Seh146360 	 */
12993847Seh146360 	sc->sc_dma_txbd.dr_name = "ipw2100-tx-ring-bd";
13003847Seh146360 	err = ipw2100_dma_region_alloc(sc, &sc->sc_dma_txbd,
13013847Seh146360 	    IPW2100_TXBD_SIZE, DDI_DMA_WRITE, DDI_DMA_CONSISTENT);
13023847Seh146360 	if (err != DDI_SUCCESS)
13033847Seh146360 		goto fail0;
13043847Seh146360 	/*
13053847Seh146360 	 * tx bufs
13063847Seh146360 	 */
13073847Seh146360 	for (i = 0; i < IPW2100_NUM_TXBUF; i++) {
13083847Seh146360 		sc->sc_dma_txbufs[i].dr_name = "ipw2100-tx-buf";
13093847Seh146360 		err = ipw2100_dma_region_alloc(sc, &sc->sc_dma_txbufs[i],
13103847Seh146360 		    IPW2100_TXBUF_SIZE, DDI_DMA_WRITE, DDI_DMA_STREAMING);
13113847Seh146360 		if (err != DDI_SUCCESS) {
13123847Seh146360 			while (i >= 0) {
13133847Seh146360 				ipw2100_dma_region_free(&sc->sc_dma_txbufs[i]);
13143847Seh146360 				i--;
13153847Seh146360 			}
13163847Seh146360 			goto fail1;
13173847Seh146360 		}
13183847Seh146360 	}
13193847Seh146360 	/*
13203847Seh146360 	 * rx ring
13213847Seh146360 	 */
13223847Seh146360 	sc->sc_dma_rxbd.dr_name = "ipw2100-rx-ring-bd";
13233847Seh146360 	err = ipw2100_dma_region_alloc(sc, &sc->sc_dma_rxbd,
13243847Seh146360 	    IPW2100_RXBD_SIZE, DDI_DMA_WRITE, DDI_DMA_CONSISTENT);
13253847Seh146360 	if (err != DDI_SUCCESS)
13263847Seh146360 		goto fail2;
13273847Seh146360 	/*
13283847Seh146360 	 * rx bufs
13293847Seh146360 	 */
13303847Seh146360 	for (i = 0; i < IPW2100_NUM_RXBUF; i++) {
13313847Seh146360 		sc->sc_dma_rxbufs[i].dr_name = "ipw2100-rx-buf";
13323847Seh146360 		err = ipw2100_dma_region_alloc(sc, &sc->sc_dma_rxbufs[i],
13333847Seh146360 		    IPW2100_RXBUF_SIZE, DDI_DMA_READ, DDI_DMA_STREAMING);
13343847Seh146360 		if (err != DDI_SUCCESS) {
13353847Seh146360 			while (i >= 0) {
13363847Seh146360 				ipw2100_dma_region_free(&sc->sc_dma_rxbufs[i]);
13373847Seh146360 				i--;
13383847Seh146360 			}
13393847Seh146360 			goto fail3;
13403847Seh146360 		}
13413847Seh146360 	}
13423847Seh146360 	/*
13433847Seh146360 	 * status
13443847Seh146360 	 */
13453847Seh146360 	sc->sc_dma_status.dr_name = "ipw2100-rx-status";
13463847Seh146360 	err = ipw2100_dma_region_alloc(sc, &sc->sc_dma_status,
13473847Seh146360 	    IPW2100_STATUS_SIZE, DDI_DMA_READ, DDI_DMA_CONSISTENT);
13483847Seh146360 	if (err != DDI_SUCCESS)
13493847Seh146360 		goto fail4;
13503847Seh146360 	/*
13513847Seh146360 	 * command
13523847Seh146360 	 */
13533847Seh146360 	sc->sc_dma_cmd.dr_name = "ipw2100-cmd";
13543847Seh146360 	err = ipw2100_dma_region_alloc(sc, &sc->sc_dma_cmd, IPW2100_CMD_SIZE,
13553847Seh146360 	    DDI_DMA_WRITE, DDI_DMA_CONSISTENT);
13563847Seh146360 	if (err != DDI_SUCCESS)
13573847Seh146360 		goto fail5;
13583847Seh146360 
13593847Seh146360 	return (DDI_SUCCESS);
13603847Seh146360 
13613847Seh146360 fail5:
13623847Seh146360 	ipw2100_dma_region_free(&sc->sc_dma_status);
13633847Seh146360 fail4:
13643847Seh146360 	for (i = 0; i < IPW2100_NUM_RXBUF; i++)
13653847Seh146360 		ipw2100_dma_region_free(&sc->sc_dma_rxbufs[i]);
13663847Seh146360 fail3:
13673847Seh146360 	ipw2100_dma_region_free(&sc->sc_dma_rxbd);
13683847Seh146360 fail2:
13693847Seh146360 	for (i = 0; i < IPW2100_NUM_TXBUF; i++)
13703847Seh146360 		ipw2100_dma_region_free(&sc->sc_dma_txbufs[i]);
13713847Seh146360 fail1:
13723847Seh146360 	ipw2100_dma_region_free(&sc->sc_dma_txbd);
13733847Seh146360 fail0:
13743847Seh146360 	return (err);
13753847Seh146360 }
13763847Seh146360 
13773847Seh146360 static void
13783847Seh146360 ipw2100_ring_free(struct ipw2100_softc *sc)
13793847Seh146360 {
13803847Seh146360 	int	i;
13813847Seh146360 
13823847Seh146360 	/*
13833847Seh146360 	 * tx ring
13843847Seh146360 	 */
13853847Seh146360 	ipw2100_dma_region_free(&sc->sc_dma_txbd);
13863847Seh146360 	/*
13873847Seh146360 	 * tx buf
13883847Seh146360 	 */
13893847Seh146360 	for (i = 0; i < IPW2100_NUM_TXBUF; i++)
13903847Seh146360 		ipw2100_dma_region_free(&sc->sc_dma_txbufs[i]);
13913847Seh146360 	/*
13923847Seh146360 	 * rx ring
13933847Seh146360 	 */
13943847Seh146360 	ipw2100_dma_region_free(&sc->sc_dma_rxbd);
13953847Seh146360 	/*
13963847Seh146360 	 * rx buf
13973847Seh146360 	 */
13983847Seh146360 	for (i = 0; i < IPW2100_NUM_RXBUF; i++)
13993847Seh146360 		ipw2100_dma_region_free(&sc->sc_dma_rxbufs[i]);
14003847Seh146360 	/*
14013847Seh146360 	 * status
14023847Seh146360 	 */
14033847Seh146360 	ipw2100_dma_region_free(&sc->sc_dma_status);
14043847Seh146360 	/*
14053847Seh146360 	 * command
14063847Seh146360 	 */
14073847Seh146360 	ipw2100_dma_region_free(&sc->sc_dma_cmd);
14083847Seh146360 }
14093847Seh146360 
14103847Seh146360 static void
14113847Seh146360 ipw2100_ring_reset(struct ipw2100_softc *sc)
14123847Seh146360 {
14133847Seh146360 	int	i;
14143847Seh146360 
14153847Seh146360 	/*
14163847Seh146360 	 * tx ring
14173847Seh146360 	 */
14183847Seh146360 	sc->sc_tx_cur   = 0;
14193847Seh146360 	sc->sc_tx_free  = IPW2100_NUM_TXBD;
14203847Seh146360 	sc->sc_txbd	= (struct ipw2100_bd *)sc->sc_dma_txbd.dr_base;
14213847Seh146360 	for (i = 0; i < IPW2100_NUM_TXBUF; i++)
14223847Seh146360 		sc->sc_txbufs[i] =
14233847Seh146360 		    (struct ipw2100_txb *)sc->sc_dma_txbufs[i].dr_base;
14243847Seh146360 	/*
14253847Seh146360 	 * rx ring
14263847Seh146360 	 */
14273847Seh146360 	sc->sc_rx_cur   = 0;
14283847Seh146360 	sc->sc_rx_free  = IPW2100_NUM_RXBD;
14293847Seh146360 	sc->sc_status   = (struct ipw2100_status *)sc->sc_dma_status.dr_base;
14303847Seh146360 	sc->sc_rxbd	= (struct ipw2100_bd *)sc->sc_dma_rxbd.dr_base;
14313847Seh146360 	for (i = 0; i < IPW2100_NUM_RXBUF; i++) {
14323847Seh146360 		sc->sc_rxbufs[i] =
14333847Seh146360 		    (struct ipw2100_rxb *)sc->sc_dma_rxbufs[i].dr_base;
14343847Seh146360 		/*
14353847Seh146360 		 * initialize Rx buffer descriptors, both host and device
14363847Seh146360 		 */
14373847Seh146360 		sc->sc_rxbd[i].phyaddr  = LE_32(sc->sc_dma_rxbufs[i].dr_pbase);
14383847Seh146360 		sc->sc_rxbd[i].len	= LE_32(sc->sc_dma_rxbufs[i].dr_size);
14393847Seh146360 		sc->sc_rxbd[i].flags	= 0;
14403847Seh146360 		sc->sc_rxbd[i].nfrag	= 1;
14413847Seh146360 	}
14423847Seh146360 	/*
14433847Seh146360 	 * command
14443847Seh146360 	 */
14453847Seh146360 	sc->sc_cmd = (struct ipw2100_cmd *)sc->sc_dma_cmd.dr_base;
14463847Seh146360 }
14473847Seh146360 
14483847Seh146360 /*
14493847Seh146360  * tx, rx rings and command initialization
14503847Seh146360  */
14513847Seh146360 static int
14523847Seh146360 ipw2100_ring_init(struct ipw2100_softc *sc)
14533847Seh146360 {
14543847Seh146360 	int	err;
14553847Seh146360 
14563847Seh146360 	err = ipw2100_ring_alloc(sc);
14573847Seh146360 	if (err != DDI_SUCCESS)
14583847Seh146360 		return (err);
14593847Seh146360 
14603847Seh146360 	ipw2100_ring_reset(sc);
14613847Seh146360 
14623847Seh146360 	return (DDI_SUCCESS);
14633847Seh146360 }
14643847Seh146360 
14653847Seh146360 static void
14663847Seh146360 ipw2100_ring_hwsetup(struct ipw2100_softc *sc)
14673847Seh146360 {
14683847Seh146360 	ipw2100_ring_reset(sc);
14693847Seh146360 	/*
14703847Seh146360 	 * tx ring
14713847Seh146360 	 */
14723847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_TX_BD_BASE, sc->sc_dma_txbd.dr_pbase);
14733847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_TX_BD_SIZE, IPW2100_NUM_TXBD);
14743847Seh146360 	/*
14753847Seh146360 	 * no new packet to transmit, tx-rd-index == tx-wr-index
14763847Seh146360 	 */
14773847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_TX_READ_INDEX, sc->sc_tx_cur);
14783847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_TX_WRITE_INDEX, sc->sc_tx_cur);
14793847Seh146360 	/*
14803847Seh146360 	 * rx ring
14813847Seh146360 	 */
14823847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_RX_BD_BASE, sc->sc_dma_rxbd.dr_pbase);
14833847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_RX_BD_SIZE, IPW2100_NUM_RXBD);
14843847Seh146360 	/*
14853847Seh146360 	 * all rx buffer are empty, rx-rd-index == 0 && rx-wr-index == N-1
14863847Seh146360 	 */
14873847Seh146360 	IPW2100_DBG(IPW2100_DBG_RING, (sc->sc_dip, CE_CONT,
14883847Seh146360 	    "ipw2100_ring_hwsetup(): rx-cur=%u, backward=%u\n",
14893847Seh146360 	    sc->sc_rx_cur, RING_BACKWARD(sc->sc_rx_cur, 1, IPW2100_NUM_RXBD)));
14903847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_RX_READ_INDEX, sc->sc_rx_cur);
14913847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_RX_WRITE_INDEX,
14923847Seh146360 	    RING_BACKWARD(sc->sc_rx_cur, 1, IPW2100_NUM_RXBD));
14933847Seh146360 	/*
14943847Seh146360 	 * status
14953847Seh146360 	 */
14963847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_RX_STATUS_BASE,
14973847Seh146360 	    sc->sc_dma_status.dr_pbase);
14983847Seh146360 }
14993847Seh146360 
15003847Seh146360 /*
15013847Seh146360  * ieee80211_new_state() is not be used, since the hardware can handle the
15023847Seh146360  * state transfer. Here, we just keep the status of the hardware notification
15033847Seh146360  * result.
15043847Seh146360  */
15053847Seh146360 /* ARGSUSED */
15063847Seh146360 static int
15073847Seh146360 ipw2100_newstate(struct ieee80211com *ic, enum ieee80211_state state, int arg)
15083847Seh146360 {
15093847Seh146360 	struct ipw2100_softc	*sc = (struct ipw2100_softc *)ic;
15103847Seh146360 	struct ieee80211_node	*in;
15113847Seh146360 	uint8_t			macaddr[IEEE80211_ADDR_LEN];
15123847Seh146360 	uint32_t		len;
15133847Seh146360 	wifi_data_t		wd = { 0 };
15143847Seh146360 
15153847Seh146360 	IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
15163847Seh146360 	    "ipw2100_newstate(): %s -> %s\n",
15173847Seh146360 	    ieee80211_state_name[ic->ic_state], ieee80211_state_name[state]));
15183847Seh146360 
15193847Seh146360 	switch (state) {
15203847Seh146360 	case IEEE80211_S_RUN:
15213847Seh146360 		/*
15223847Seh146360 		 * we only need to use BSSID as to find the node
15233847Seh146360 		 */
15243847Seh146360 		drv_usecwait(200); /* firmware needs a short delay here */
15253847Seh146360 		len = IEEE80211_ADDR_LEN;
15263847Seh146360 		(void) ipw2100_table2_getbuf(sc, IPW2100_INFO_CURRENT_BSSID,
15273847Seh146360 		    macaddr, &len);
15283847Seh146360 
15293847Seh146360 		in = ieee80211_find_node(&ic->ic_scan, macaddr);
15303847Seh146360 		if (in == NULL)
15313847Seh146360 			break;
15323847Seh146360 
15333847Seh146360 		(void) ieee80211_sta_join(ic, in);
15343847Seh146360 		ieee80211_node_authorize(in);
15353847Seh146360 
15363847Seh146360 		/*
15373847Seh146360 		 * We can send data now; update the fastpath with our
15383847Seh146360 		 * current associated BSSID.
15393847Seh146360 		 */
15403847Seh146360 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
15413847Seh146360 			wd.wd_secalloc = WIFI_SEC_WEP;
15423847Seh146360 		else
15433847Seh146360 			wd.wd_secalloc = WIFI_SEC_NONE;
15443847Seh146360 		wd.wd_opmode = ic->ic_opmode;
15453847Seh146360 		IEEE80211_ADDR_COPY(wd.wd_bssid, ic->ic_bss->in_bssid);
15463847Seh146360 		(void) mac_pdata_update(ic->ic_mach, &wd, sizeof (wd));
15473847Seh146360 
15483847Seh146360 		break;
15493847Seh146360 
15503847Seh146360 	case IEEE80211_S_INIT:
15513847Seh146360 	case IEEE80211_S_SCAN:
15523847Seh146360 	case IEEE80211_S_AUTH:
15533847Seh146360 	case IEEE80211_S_ASSOC:
15543847Seh146360 		break;
15553847Seh146360 	}
15563847Seh146360 
15573847Seh146360 	/*
15583847Seh146360 	 * notify to update the link
15593847Seh146360 	 */
15603847Seh146360 	if ((ic->ic_state != IEEE80211_S_RUN) && (state == IEEE80211_S_RUN)) {
15613847Seh146360 		/*
15623847Seh146360 		 * previously disconnected and now connected
15633847Seh146360 		 */
15643847Seh146360 		sc->sc_linkstate = LINK_STATE_UP;
15653847Seh146360 		sc->sc_flags |= IPW2100_FLAG_LINK_CHANGE;
15663847Seh146360 	} else if ((ic->ic_state == IEEE80211_S_RUN) &&
15673847Seh146360 	    (state != IEEE80211_S_RUN)) {
15683847Seh146360 		/*
15693847Seh146360 		 * previously connected andd now disconnected
15703847Seh146360 		 */
15713847Seh146360 		sc->sc_linkstate = LINK_STATE_DOWN;
15723847Seh146360 		sc->sc_flags |= IPW2100_FLAG_LINK_CHANGE;
15733847Seh146360 	}
15743847Seh146360 
15753847Seh146360 	ic->ic_state = state;
15763847Seh146360 	return (DDI_SUCCESS);
15773847Seh146360 }
15783847Seh146360 
15793847Seh146360 /*
15803847Seh146360  * GLD operations
15813847Seh146360  */
15823847Seh146360 /* ARGSUSED */
15833847Seh146360 static int
15843847Seh146360 ipw2100_m_stat(void *arg, uint_t stat, uint64_t *val)
15853847Seh146360 {
15863847Seh146360 	ieee80211com_t	*ic = (ieee80211com_t *)arg;
15873847Seh146360 	IPW2100_DBG(IPW2100_DBG_GLD, (((struct ipw2100_softc *)arg)->sc_dip,
1588*4784Seh146360 	    CE_CONT,
1589*4784Seh146360 	    "ipw2100_m_stat(): enter\n"));
15903847Seh146360 	/*
15913847Seh146360 	 * some of below statistic data are from hardware, some from net80211
15923847Seh146360 	 */
15933847Seh146360 	switch (stat) {
15943847Seh146360 	case MAC_STAT_RBYTES:
15953847Seh146360 		*val = ic->ic_stats.is_rx_bytes;
15963847Seh146360 		break;
15973847Seh146360 	case MAC_STAT_IPACKETS:
15983847Seh146360 		*val = ic->ic_stats.is_rx_frags;
15993847Seh146360 		break;
16003847Seh146360 	case MAC_STAT_OBYTES:
16013847Seh146360 		*val = ic->ic_stats.is_tx_bytes;
16023847Seh146360 		break;
16033847Seh146360 	case MAC_STAT_OPACKETS:
16043847Seh146360 		*val = ic->ic_stats.is_tx_frags;
16053847Seh146360 		break;
16063847Seh146360 	/*
16073847Seh146360 	 * Get below from hardware statistic, retrieve net80211 value once 1s
16083847Seh146360 	 */
16093847Seh146360 	case WIFI_STAT_TX_FRAGS:
16103847Seh146360 	case WIFI_STAT_MCAST_TX:
16113847Seh146360 	case WIFI_STAT_TX_FAILED:
16123847Seh146360 	case WIFI_STAT_TX_RETRANS:
16133847Seh146360 	case WIFI_STAT_RTS_SUCCESS:
16143847Seh146360 	case WIFI_STAT_ACK_FAILURE:
16153847Seh146360 	case WIFI_STAT_RX_FRAGS:
16163847Seh146360 	case WIFI_STAT_MCAST_RX:
16173847Seh146360 	/*
16183847Seh146360 	 * Get blow information from net80211
16193847Seh146360 	 */
16203847Seh146360 	case WIFI_STAT_RTS_FAILURE:
16213847Seh146360 	case WIFI_STAT_RX_DUPS:
16223847Seh146360 	case WIFI_STAT_FCS_ERRORS:
16233847Seh146360 	case WIFI_STAT_WEP_ERRORS:
16243847Seh146360 		return (ieee80211_stat(ic, stat, val));
16253847Seh146360 	/*
16263847Seh146360 	 * need be supported in the future
16273847Seh146360 	 */
16283847Seh146360 	case MAC_STAT_IFSPEED:
16293847Seh146360 	case MAC_STAT_NOXMTBUF:
16303847Seh146360 	case MAC_STAT_IERRORS:
16313847Seh146360 	case MAC_STAT_OERRORS:
16323847Seh146360 	default:
16333847Seh146360 		return (ENOTSUP);
16343847Seh146360 	}
16353847Seh146360 	return (0);
16363847Seh146360 }
16373847Seh146360 
16383847Seh146360 /* ARGSUSED */
16393847Seh146360 static int
16403847Seh146360 ipw2100_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
16413847Seh146360 {
16423847Seh146360 	/* not supported */
16433847Seh146360 	IPW2100_DBG(IPW2100_DBG_GLD, (((struct ipw2100_softc *)arg)->sc_dip,
1644*4784Seh146360 	    CE_CONT,
1645*4784Seh146360 	    "ipw2100_m_multicst(): enter\n"));
16463847Seh146360 
16473847Seh146360 	return (DDI_SUCCESS);
16483847Seh146360 }
16493847Seh146360 
16503847Seh146360 /*
16513847Seh146360  * This thread function is used to handle the fatal error.
16523847Seh146360  */
16533847Seh146360 static void
16543847Seh146360 ipw2100_thread(struct ipw2100_softc *sc)
16553847Seh146360 {
16563847Seh146360 	struct ieee80211com	*ic = &sc->sc_ic;
16573847Seh146360 	int32_t			nlstate;
16583847Seh146360 	int			stat_cnt = 0;
16593847Seh146360 
16603847Seh146360 	IPW2100_DBG(IPW2100_DBG_SOFTINT, (sc->sc_dip, CE_CONT,
16613847Seh146360 	    "ipw2100_thread(): into ipw2100 thread--> %d\n",
16623847Seh146360 	    sc->sc_linkstate));
16633847Seh146360 
16643847Seh146360 	mutex_enter(&sc->sc_mflock);
16653847Seh146360 
16663847Seh146360 	while (sc->sc_mfthread_switch) {
16673847Seh146360 		/*
16683847Seh146360 		 * notify the link state
16693847Seh146360 		 */
16703847Seh146360 		if (ic->ic_mach && (sc->sc_flags & IPW2100_FLAG_LINK_CHANGE)) {
16713847Seh146360 			IPW2100_DBG(IPW2100_DBG_SOFTINT, (sc->sc_dip, CE_CONT,
16723847Seh146360 			    "ipw2100_thread(): link status --> %d\n",
16733847Seh146360 			    sc->sc_linkstate));
16743847Seh146360 
16753847Seh146360 			sc->sc_flags &= ~IPW2100_FLAG_LINK_CHANGE;
16763847Seh146360 			nlstate = sc->sc_linkstate;
16773847Seh146360 
16783847Seh146360 			mutex_exit(&sc->sc_mflock);
16793847Seh146360 			mac_link_update(ic->ic_mach, nlstate);
16803847Seh146360 			mutex_enter(&sc->sc_mflock);
16813847Seh146360 		}
16823847Seh146360 
16833847Seh146360 		/*
16843847Seh146360 		 * recovery interrupt fatal error
16853847Seh146360 		 */
16863847Seh146360 		if (ic->ic_mach &&
16873847Seh146360 		    (sc->sc_flags & IPW2100_FLAG_HW_ERR_RECOVER)) {
16883847Seh146360 
16893847Seh146360 			IPW2100_DBG(IPW2100_DBG_FATAL, (sc->sc_dip, CE_CONT,
16903847Seh146360 			    "try to recover fatal hw error\n"));
16913847Seh146360 			sc->sc_flags &= ~IPW2100_FLAG_HW_ERR_RECOVER;
16923847Seh146360 
16933847Seh146360 			mutex_exit(&sc->sc_mflock);
16943847Seh146360 			(void) ipw2100_init(sc); /* Force stat machine */
16953847Seh146360 			delay(drv_usectohz(delay_fatal_recover));
16963847Seh146360 			mutex_enter(&sc->sc_mflock);
16973847Seh146360 		}
16983847Seh146360 
16993847Seh146360 		/*
17003847Seh146360 		 * get statistic, the value will be retrieved by m_stat
17013847Seh146360 		 */
17023847Seh146360 		if (stat_cnt == 10) {
17033847Seh146360 			stat_cnt = 0; /* re-start */
17043847Seh146360 
17053847Seh146360 			mutex_exit(&sc->sc_mflock);
17063847Seh146360 			ipw2100_get_statistics(sc);
17073847Seh146360 			mutex_enter(&sc->sc_mflock);
17083847Seh146360 		} else
17093847Seh146360 			stat_cnt++; /* until 1s */
17103847Seh146360 
17113847Seh146360 		mutex_exit(&sc->sc_mflock);
17123847Seh146360 		delay(drv_usectohz(delay_aux_thread));
17133847Seh146360 		mutex_enter(&sc->sc_mflock);
17143847Seh146360 	}
17153847Seh146360 	sc->sc_mf_thread = NULL;
17163847Seh146360 	cv_broadcast(&sc->sc_mfthread_cv);
17173847Seh146360 	mutex_exit(&sc->sc_mflock);
17183847Seh146360 }
17193847Seh146360 
17203847Seh146360 static int
17213847Seh146360 ipw2100_m_start(void *arg)
17223847Seh146360 {
17233847Seh146360 	struct ipw2100_softc	*sc = (struct ipw2100_softc *)arg;
17243847Seh146360 
17253847Seh146360 	IPW2100_DBG(IPW2100_DBG_GLD, (sc->sc_dip, CE_CONT,
17263847Seh146360 	    "ipw2100_m_start(): enter\n"));
17273847Seh146360 
17283847Seh146360 	/*
17293847Seh146360 	 * initialize ipw2100 hardware
17303847Seh146360 	 */
17313847Seh146360 	(void) ipw2100_init(sc);
17323847Seh146360 
17333847Seh146360 	sc->sc_flags |= IPW2100_FLAG_RUNNING;
17343847Seh146360 
17353847Seh146360 	/*
17363847Seh146360 	 * fix KCF bug. - workaround, need to fix it in net80211
17373847Seh146360 	 */
17383847Seh146360 	(void) crypto_mech2id(SUN_CKM_RC4);
17393847Seh146360 
17403847Seh146360 	return (DDI_SUCCESS);
17413847Seh146360 }
17423847Seh146360 
17433847Seh146360 static void
17443847Seh146360 ipw2100_m_stop(void *arg)
17453847Seh146360 {
17463847Seh146360 	struct ipw2100_softc	*sc = (struct ipw2100_softc *)arg;
17473847Seh146360 
17483847Seh146360 	IPW2100_DBG(IPW2100_DBG_GLD, (sc->sc_dip, CE_CONT,
17493847Seh146360 	    "ipw2100_m_stop(): enter\n"));
17503847Seh146360 
17513847Seh146360 	ipw2100_stop(sc);
17523847Seh146360 
17533847Seh146360 	sc->sc_flags &= ~IPW2100_FLAG_RUNNING;
17543847Seh146360 }
17553847Seh146360 
17563847Seh146360 static int
17573847Seh146360 ipw2100_m_unicst(void *arg, const uint8_t *macaddr)
17583847Seh146360 {
17593847Seh146360 	struct ipw2100_softc	*sc = (struct ipw2100_softc *)arg;
17603847Seh146360 	struct ieee80211com	*ic = &sc->sc_ic;
17613847Seh146360 	int			err;
17623847Seh146360 
17633847Seh146360 	IPW2100_DBG(IPW2100_DBG_GLD, (sc->sc_dip, CE_CONT,
17643847Seh146360 	    "ipw2100_m_unicst(): enter\n"));
17653847Seh146360 
17663847Seh146360 	IPW2100_DBG(IPW2100_DBG_GLD, (sc->sc_dip, CE_CONT,
17673847Seh146360 	    "ipw2100_m_unicst(): GLD setting MAC address to "
17683847Seh146360 	    "%02x:%02x:%02x:%02x:%02x:%02x\n",
17693847Seh146360 	    macaddr[0], macaddr[1], macaddr[2],
17703847Seh146360 	    macaddr[3], macaddr[4], macaddr[5]));
17713847Seh146360 
17723847Seh146360 	if (!IEEE80211_ADDR_EQ(ic->ic_macaddr, macaddr)) {
17733847Seh146360 		IEEE80211_ADDR_COPY(ic->ic_macaddr, macaddr);
17743847Seh146360 
17753847Seh146360 		if (sc->sc_flags & IPW2100_FLAG_RUNNING) {
17763847Seh146360 			err = ipw2100_config(sc);
17773847Seh146360 			if (err != DDI_SUCCESS) {
17783847Seh146360 				IPW2100_WARN((sc->sc_dip, CE_WARN,
17793847Seh146360 				    "ipw2100_m_unicst(): "
17803847Seh146360 				    "device configuration failed\n"));
17813847Seh146360 				goto fail;
17823847Seh146360 			}
17833847Seh146360 		}
17843847Seh146360 	}
17853847Seh146360 
17863847Seh146360 	return (DDI_SUCCESS);
17873847Seh146360 fail:
17883847Seh146360 	return (err);
17893847Seh146360 }
17903847Seh146360 
17913847Seh146360 static int
17923847Seh146360 ipw2100_m_promisc(void *arg, boolean_t on)
17933847Seh146360 {
17943847Seh146360 	struct ipw2100_softc	*sc = (struct ipw2100_softc *)arg;
17953847Seh146360 	int recfg, err;
17963847Seh146360 
17973847Seh146360 	IPW2100_DBG(IPW2100_DBG_GLD, (sc->sc_dip, CE_CONT,
17983847Seh146360 	    "ipw2100_m_promisc(): enter. "
17993847Seh146360 	    "GLD setting promiscuous mode - %d\n", on));
18003847Seh146360 
18013847Seh146360 	recfg = 0;
18023847Seh146360 	if (on)
18033847Seh146360 		if (!(sc->if_flags & IFF_PROMISC)) {
18043847Seh146360 			sc->if_flags |= IFF_PROMISC;
18053847Seh146360 			recfg = 1;
18063847Seh146360 		}
18073847Seh146360 	else
18083847Seh146360 		if (sc->if_flags & IFF_PROMISC) {
18093847Seh146360 			sc->if_flags &= ~IFF_PROMISC;
18103847Seh146360 			recfg = 1;
18113847Seh146360 		}
18123847Seh146360 
18133847Seh146360 	if (recfg && (sc->sc_flags & IPW2100_FLAG_RUNNING)) {
18143847Seh146360 		err = ipw2100_config(sc);
18153847Seh146360 		if (err != DDI_SUCCESS) {
18163847Seh146360 			IPW2100_WARN((sc->sc_dip, CE_WARN,
18173847Seh146360 			    "ipw2100_m_promisc(): "
18183847Seh146360 			    "device configuration failed\n"));
18193847Seh146360 			goto fail;
18203847Seh146360 		}
18213847Seh146360 	}
18223847Seh146360 
18233847Seh146360 	return (DDI_SUCCESS);
18243847Seh146360 fail:
18253847Seh146360 	return (err);
18263847Seh146360 }
18273847Seh146360 
18283847Seh146360 static mblk_t *
18293847Seh146360 ipw2100_m_tx(void *arg, mblk_t *mp)
18303847Seh146360 {
18313847Seh146360 	struct ipw2100_softc	*sc = (struct ipw2100_softc *)arg;
18323847Seh146360 	struct ieee80211com	*ic = &sc->sc_ic;
18333847Seh146360 	mblk_t			*next;
18343847Seh146360 
18353847Seh146360 	/*
18363847Seh146360 	 * No data frames go out unless we're associated; this
18373847Seh146360 	 * should not happen as the 802.11 layer does not enable
18383847Seh146360 	 * the xmit queue until we enter the RUN state.
18393847Seh146360 	 */
18403847Seh146360 	if (ic->ic_state != IEEE80211_S_RUN) {
18413847Seh146360 		IPW2100_DBG(IPW2100_DBG_GLD, (sc->sc_dip, CE_CONT,
18423847Seh146360 		    "ipw2100_m_tx(): discard msg, ic_state = %u\n",
18433847Seh146360 		    ic->ic_state));
18443847Seh146360 		freemsgchain(mp);
18453847Seh146360 		return (NULL);
18463847Seh146360 	}
18473847Seh146360 
18483847Seh146360 	while (mp != NULL) {
18493847Seh146360 		next = mp->b_next;
18503847Seh146360 		mp->b_next = NULL;
18513847Seh146360 		if (ipw2100_send(ic, mp, IEEE80211_FC0_TYPE_DATA) !=
18523847Seh146360 		    DDI_SUCCESS) {
18533847Seh146360 			mp->b_next = next;
18543847Seh146360 			break;
18553847Seh146360 		}
18563847Seh146360 		mp = next;
18573847Seh146360 	}
18583847Seh146360 	return (mp);
18593847Seh146360 }
18603847Seh146360 
18613847Seh146360 /* ARGSUSED */
18623847Seh146360 static int
18633847Seh146360 ipw2100_send(ieee80211com_t *ic, mblk_t *mp, uint8_t type)
18643847Seh146360 {
18653847Seh146360 	struct ipw2100_softc	*sc = (struct ipw2100_softc *)ic;
18663847Seh146360 	struct ieee80211_node	*in;
18673847Seh146360 	struct ieee80211_frame	wh, *wh_tmp;
18683847Seh146360 	struct ieee80211_key	*k;
18693847Seh146360 	uint8_t			*hdat;
18703847Seh146360 	mblk_t			*m0, *m;
18713847Seh146360 	size_t			cnt, off;
18723847Seh146360 	struct ipw2100_bd	*txbd[2];
18733847Seh146360 	struct ipw2100_txb	*txbuf;
18743847Seh146360 	struct dma_region	*dr;
18753847Seh146360 	struct ipw2100_hdr	*h;
18763847Seh146360 	uint32_t		idx, bidx;
18773847Seh146360 	int			err;
18783847Seh146360 
18793847Seh146360 	ASSERT(mp->b_next == NULL);
18803847Seh146360 
18813847Seh146360 	m0 = NULL;
18823847Seh146360 	m = NULL;
18833847Seh146360 	err = DDI_SUCCESS;
18843847Seh146360 
18853847Seh146360 	IPW2100_DBG(IPW2100_DBG_GLD, (sc->sc_dip, CE_CONT,
18863847Seh146360 	    "ipw2100_send(): enter\n"));
18873847Seh146360 
18883847Seh146360 	if ((type & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_DATA) {
18893847Seh146360 		/*
18903847Seh146360 		 * it is impossible to send non-data 802.11 frame in current
18913847Seh146360 		 * ipw driver. Therefore, drop the package
18923847Seh146360 		 */
18933847Seh146360 		freemsg(mp);
18943847Seh146360 		err = DDI_SUCCESS;
18953847Seh146360 		goto fail0;
18963847Seh146360 	}
18973847Seh146360 
18983847Seh146360 	mutex_enter(&sc->sc_tx_lock);
18993847Seh146360 
19003847Seh146360 	/*
19013847Seh146360 	 * need 2 descriptors: 1 for SEND cmd parameter header,
19023847Seh146360 	 * and the other for payload, i.e., 802.11 frame including 802.11
19033847Seh146360 	 * frame header
19043847Seh146360 	 */
19053847Seh146360 	if (sc->sc_tx_free < 2) {
19063847Seh146360 		mutex_enter(&sc->sc_resched_lock);
19073847Seh146360 		IPW2100_DBG(IPW2100_DBG_RING, (sc->sc_dip, CE_WARN,
19083847Seh146360 		    "ipw2100_send(): no enough descriptors(%d)\n",
19093847Seh146360 		    sc->sc_tx_free));
19103847Seh146360 		ic->ic_stats.is_tx_nobuf++; /* no enough buffer */
19113847Seh146360 		sc->sc_flags |= IPW2100_FLAG_TX_SCHED;
19123847Seh146360 		err = DDI_FAILURE;
19133847Seh146360 		mutex_exit(&sc->sc_resched_lock);
19143847Seh146360 		goto fail1;
19153847Seh146360 	}
19163847Seh146360 	IPW2100_DBG(IPW2100_DBG_RING, (sc->sc_dip, CE_CONT,
19173847Seh146360 	    "ipw2100_send(): tx-free=%d,tx-curr=%d\n",
19183847Seh146360 	    sc->sc_tx_free, sc->sc_tx_cur));
19193847Seh146360 
19203847Seh146360 	wh_tmp = (struct ieee80211_frame *)mp->b_rptr;
19213847Seh146360 	in = ieee80211_find_txnode(ic, wh_tmp->i_addr1);
19223847Seh146360 	if (in == NULL) { /* can not find tx node, drop the package */
19233847Seh146360 		freemsg(mp);
19243847Seh146360 		err = DDI_SUCCESS;
19253847Seh146360 		goto fail1;
19263847Seh146360 	}
19273847Seh146360 	in->in_inact = 0;
19283847Seh146360 	(void) ieee80211_encap(ic, mp, in);
19293847Seh146360 	ieee80211_free_node(in);
19303847Seh146360 
19313847Seh146360 	if (wh_tmp->i_fc[1] & IEEE80211_FC1_WEP) {
19323847Seh146360 		/*
19333847Seh146360 		 * it is very bad that ieee80211_crypto_encap can only accept a
19343847Seh146360 		 * single continuous buffer.
19353847Seh146360 		 */
19363847Seh146360 		/*
19373847Seh146360 		 * allocate 32 more bytes is to be compatible with further
19383847Seh146360 		 * ieee802.11i standard.
19393847Seh146360 		 */
19403847Seh146360 		m = allocb(msgdsize(mp) + 32, BPRI_MED);
19413847Seh146360 		if (m == NULL) { /* can not alloc buf, drop this package */
19423847Seh146360 			IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
19433847Seh146360 			    "ipw2100_send(): msg allocation failed\n"));
19443847Seh146360 
19453847Seh146360 			freemsg(mp);
19463847Seh146360 
19473847Seh146360 			err = DDI_SUCCESS;
19483847Seh146360 			goto fail1;
19493847Seh146360 		}
19503847Seh146360 		off = 0;
19513847Seh146360 		m0 = mp;
19523847Seh146360 		while (m0) {
19533847Seh146360 			cnt = MBLKL(m0);
19543847Seh146360 			if (cnt) {
19553847Seh146360 				(void) memcpy(m->b_rptr + off, m0->b_rptr, cnt);
19563847Seh146360 				off += cnt;
19573847Seh146360 			}
19583847Seh146360 			m0 = m0->b_cont;
19593847Seh146360 		}
19603847Seh146360 		m->b_wptr += off;
19613847Seh146360 		IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
19623847Seh146360 		    "ipw2100_send(): "
19633847Seh146360 		    "Encrypting 802.11 frame started, %d, %d\n",
19643847Seh146360 		    msgdsize(mp), MBLKL(mp)));
19653847Seh146360 		k = ieee80211_crypto_encap(ic, m);
19663847Seh146360 		if (k == NULL) { /* can not get the key, drop packages */
19673847Seh146360 			IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
19683847Seh146360 			    "ipw2100_send(): "
19693847Seh146360 			    "Encrypting 802.11 frame failed\n"));
19703847Seh146360 
19713847Seh146360 			freemsg(mp);
19723847Seh146360 			err = DDI_SUCCESS;
19733847Seh146360 			goto fail2;
19743847Seh146360 		}
19753847Seh146360 		IPW2100_DBG(IPW2100_DBG_WIFI, (sc->sc_dip, CE_CONT,
19763847Seh146360 		    "ipw2100_send(): "
19773847Seh146360 		    "Encrypting 802.11 frame finished, %d, %d, k=0x%08x\n",
19783847Seh146360 		    msgdsize(mp), MBLKL(mp), k->wk_flags));
19793847Seh146360 	}
19803847Seh146360 
19813847Seh146360 	/*
19823847Seh146360 	 * header descriptor
19833847Seh146360 	 */
19843847Seh146360 	idx = sc->sc_tx_cur;
19853847Seh146360 	txbd[0]  = &sc->sc_txbd[idx];
19863847Seh146360 	if ((idx & 1) == 0)
19873847Seh146360 		bidx = idx / 2;
19883847Seh146360 	sc->sc_tx_cur = RING_FORWARD(sc->sc_tx_cur, 1, IPW2100_NUM_TXBD);
19893847Seh146360 	sc->sc_tx_free--;
19903847Seh146360 
19913847Seh146360 	/*
19923847Seh146360 	 * payload descriptor
19933847Seh146360 	 */
19943847Seh146360 	idx = sc->sc_tx_cur;
19953847Seh146360 	txbd[1]  = &sc->sc_txbd[idx];
19963847Seh146360 	if ((idx & 1) == 0)
19973847Seh146360 		bidx = idx / 2;
19983847Seh146360 	sc->sc_tx_cur = RING_FORWARD(sc->sc_tx_cur, 1, IPW2100_NUM_TXBD);
19993847Seh146360 	sc->sc_tx_free--;
20003847Seh146360 
20013847Seh146360 	/*
20023847Seh146360 	 * one buffer, SEND cmd header and payload buffer
20033847Seh146360 	 */
20043847Seh146360 	txbuf = sc->sc_txbufs[bidx];
20053847Seh146360 	dr = &sc->sc_dma_txbufs[bidx];
20063847Seh146360 
20073847Seh146360 	/*
20083847Seh146360 	 * extract 802.11 header from message, fill wh from m0
20093847Seh146360 	 */
20103847Seh146360 	hdat = (uint8_t *)&wh;
20113847Seh146360 	off = 0;
20123847Seh146360 	if (m)
20133847Seh146360 		m0 = m;
20143847Seh146360 	else
20153847Seh146360 		m0 = mp;
20163847Seh146360 	while (off < sizeof (wh)) {
20173847Seh146360 		cnt = MBLKL(m0);
20183847Seh146360 		if (cnt > (sizeof (wh) - off))
20193847Seh146360 			cnt = sizeof (wh) - off;
20203847Seh146360 		if (cnt) {
20213847Seh146360 			(void) memcpy(hdat + off, m0->b_rptr, cnt);
20223847Seh146360 			off += cnt;
20233847Seh146360 			m0->b_rptr += cnt;
20243847Seh146360 		}
20253847Seh146360 		else
20263847Seh146360 			m0 = m0->b_cont;
20273847Seh146360 	}
20283847Seh146360 
20293847Seh146360 	/*
20303847Seh146360 	 * prepare SEND cmd header
20313847Seh146360 	 */
20323847Seh146360 	h		= &txbuf->txb_hdr;
20333847Seh146360 	h->type		= LE_32(IPW2100_CMD_SEND);
20343847Seh146360 	h->subtype	= LE_32(0);
20353847Seh146360 	h->encrypted    = ic->ic_flags & IEEE80211_F_PRIVACY ? 1 : 0;
20363847Seh146360 	h->encrypt	= 0;
20373847Seh146360 	h->keyidx	= 0;
20383847Seh146360 	h->keysz	= 0;
20393847Seh146360 	h->fragsz	= LE_16(0);
20403847Seh146360 	IEEE80211_ADDR_COPY(h->saddr, wh.i_addr2);
20413847Seh146360 	if (ic->ic_opmode == IEEE80211_M_STA)
20423847Seh146360 		IEEE80211_ADDR_COPY(h->daddr, wh.i_addr3);
20433847Seh146360 	else
20443847Seh146360 		IEEE80211_ADDR_COPY(h->daddr, wh.i_addr1);
20453847Seh146360 
20463847Seh146360 	/*
20473847Seh146360 	 * extract payload from message into tx data buffer
20483847Seh146360 	 */
20493847Seh146360 	off = 0;
20503847Seh146360 	while (m0) {
20513847Seh146360 		cnt = MBLKL(m0);
20523847Seh146360 		if (cnt) {
20533847Seh146360 			(void) memcpy(&txbuf->txb_dat[off], m0->b_rptr, cnt);
20543847Seh146360 			off += cnt;
20553847Seh146360 		}
20563847Seh146360 		m0 = m0->b_cont;
20573847Seh146360 	}
20583847Seh146360 
20593847Seh146360 	/*
20603847Seh146360 	 * fill SEND cmd header descriptor
20613847Seh146360 	 */
20623847Seh146360 	txbd[0]->phyaddr = LE_32(dr->dr_pbase +
20633847Seh146360 	    OFFSETOF(struct ipw2100_txb, txb_hdr));
20643847Seh146360 	txbd[0]->len	= LE_32(sizeof (struct ipw2100_hdr));
20653847Seh146360 	txbd[0]->flags	= IPW2100_BD_FLAG_TX_FRAME_802_3 |
20663847Seh146360 	    IPW2100_BD_FLAG_TX_NOT_LAST_FRAGMENT;
20673847Seh146360 	txbd[0]->nfrag	= 2;
20683847Seh146360 	/*
20693847Seh146360 	 * fill payload descriptor
20703847Seh146360 	 */
20713847Seh146360 	txbd[1]->phyaddr = LE_32(dr->dr_pbase +
20723847Seh146360 	    OFFSETOF(struct ipw2100_txb, txb_dat[0]));
20733847Seh146360 	txbd[1]->len	= LE_32(off);
20743847Seh146360 	txbd[1]->flags	= IPW2100_BD_FLAG_TX_FRAME_802_3 |
20753847Seh146360 	    IPW2100_BD_FLAG_TX_LAST_FRAGMENT;
20763847Seh146360 	txbd[1]->nfrag	= 0;
20773847Seh146360 
20783847Seh146360 	/*
20793847Seh146360 	 * dma sync
20803847Seh146360 	 */
20813847Seh146360 	(void) ddi_dma_sync(dr->dr_hnd, 0, sizeof (struct ipw2100_txb),
20823847Seh146360 	    DDI_DMA_SYNC_FORDEV);
20833847Seh146360 	(void) ddi_dma_sync(sc->sc_dma_txbd.dr_hnd,
20843847Seh146360 	    (txbd[0] - sc->sc_txbd) * sizeof (struct ipw2100_bd),
20853847Seh146360 	    sizeof (struct ipw2100_bd), DDI_DMA_SYNC_FORDEV);
20863847Seh146360 	/*
20873847Seh146360 	 * since txbd[1] may not be successive to txbd[0] due to the ring
20883847Seh146360 	 * organization, another dma_sync is needed to simplify the logic
20893847Seh146360 	 */
20903847Seh146360 	(void) ddi_dma_sync(sc->sc_dma_txbd.dr_hnd,
20913847Seh146360 	    (txbd[1] - sc->sc_txbd) * sizeof (struct ipw2100_bd),
20923847Seh146360 	    sizeof (struct ipw2100_bd), DDI_DMA_SYNC_FORDEV);
20933847Seh146360 	/*
20943847Seh146360 	 * update txcur
20953847Seh146360 	 */
20963847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_TX_WRITE_INDEX, sc->sc_tx_cur);
20973847Seh146360 
20983847Seh146360 	if (mp) /* success, free the original message */
20993847Seh146360 		freemsg(mp);
21003847Seh146360 fail2:
21013847Seh146360 	if (m)
21023847Seh146360 		freemsg(m);
21033847Seh146360 fail1:
21043847Seh146360 	mutex_exit(&sc->sc_tx_lock);
21053847Seh146360 fail0:
21063847Seh146360 	IPW2100_DBG(IPW2100_DBG_GLD, (sc->sc_dip, CE_CONT,
21073847Seh146360 	    "ipw2100_send(): exit - err=%d\n", err));
21083847Seh146360 
21093847Seh146360 	return (err);
21103847Seh146360 }
21113847Seh146360 
21123847Seh146360 /*
21133847Seh146360  * IOCTL Handler
21143847Seh146360  */
21153847Seh146360 #define	IEEE80211_IOCTL_REQUIRED	(1)
21163847Seh146360 #define	IEEE80211_IOCTL_NOT_REQUIRED	(0)
21173847Seh146360 static void
21183847Seh146360 ipw2100_m_ioctl(void *arg, queue_t *q, mblk_t *m)
21193847Seh146360 {
21203847Seh146360 	struct ipw2100_softc	*sc  = (struct ipw2100_softc *)arg;
21213847Seh146360 	struct ieee80211com	*ic = &sc->sc_ic;
21223847Seh146360 	int			err;
21233847Seh146360 
21243847Seh146360 	IPW2100_DBG(IPW2100_DBG_GLD, (sc->sc_dip, CE_CONT,
21253847Seh146360 	    "ipw2100_m_ioctl(): enter\n"));
21263847Seh146360 
21273847Seh146360 	/*
21283847Seh146360 	 * check whether or not need to handle this in net80211
21293847Seh146360 	 */
21303847Seh146360 	if (ipw2100_ioctl(sc, q, m) == IEEE80211_IOCTL_NOT_REQUIRED)
21313847Seh146360 		return; /* succes or fail */
21323847Seh146360 
21333847Seh146360 	err = ieee80211_ioctl(ic, q, m);
21343847Seh146360 	if (err == ENETRESET) {
21353847Seh146360 		if (sc->sc_flags & IPW2100_FLAG_RUNNING) {
21363847Seh146360 			(void) ipw2100_m_start(sc);
21373847Seh146360 			(void) ieee80211_new_state(ic,
21383847Seh146360 			    IEEE80211_S_SCAN, -1);
21393847Seh146360 		}
21403847Seh146360 	}
21413847Seh146360 	if (err == ERESTART) {
21423847Seh146360 		if (sc->sc_flags & IPW2100_FLAG_RUNNING)
21433847Seh146360 			(void) ipw2100_chip_reset(sc);
21443847Seh146360 	}
21453847Seh146360 }
21463847Seh146360 
21473847Seh146360 static int
21483847Seh146360 ipw2100_ioctl(struct ipw2100_softc *sc, queue_t *q, mblk_t *m)
21493847Seh146360 {
21503847Seh146360 	struct iocblk	*iocp;
21513847Seh146360 	uint32_t	len, ret, cmd;
21523847Seh146360 	mblk_t		*m0;
21533847Seh146360 	boolean_t	need_privilege;
21543847Seh146360 	boolean_t	need_net80211;
21553847Seh146360 
21563847Seh146360 	if (MBLKL(m) < sizeof (struct iocblk)) {
21573847Seh146360 		IPW2100_DBG(IPW2100_DBG_IOCTL, (sc->sc_dip, CE_CONT,
21583847Seh146360 		    "ipw2100_ioctl(): ioctl buffer too short, %u\n",
21593847Seh146360 		    MBLKL(m)));
21603847Seh146360 		miocnak(q, m, 0, EINVAL);
21613847Seh146360 		return (IEEE80211_IOCTL_NOT_REQUIRED);
21623847Seh146360 	}
21633847Seh146360 
21643847Seh146360 	/*
21653847Seh146360 	 * Validate the command
21663847Seh146360 	 */
21673847Seh146360 	iocp = (struct iocblk *)m->b_rptr;
21683847Seh146360 	iocp->ioc_error = 0;
21693847Seh146360 	cmd = iocp->ioc_cmd;
21703847Seh146360 	need_privilege = B_TRUE;
21713847Seh146360 	switch (cmd) {
21723847Seh146360 	case WLAN_SET_PARAM:
21733847Seh146360 	case WLAN_COMMAND:
21743847Seh146360 		break;
21753847Seh146360 	case WLAN_GET_PARAM:
21763847Seh146360 		need_privilege = B_FALSE;
21773847Seh146360 		break;
21783847Seh146360 	default:
21793847Seh146360 		IPW2100_DBG(IPW2100_DBG_IOCTL, (sc->sc_dip, CE_CONT,
21803847Seh146360 		    "ieee80211_ioctl(): unknown cmd 0x%x", cmd));
21813847Seh146360 		miocnak(q, m, 0, EINVAL);
21823847Seh146360 		return (IEEE80211_IOCTL_NOT_REQUIRED);
21833847Seh146360 	}
21843847Seh146360 
21853847Seh146360 	if (need_privilege) {
21863847Seh146360 		/*
21873847Seh146360 		 * Check for specific net_config privilege on Solaris 10+.
21883847Seh146360 		 * Otherwise just check for root access ...
21893847Seh146360 		 */
21903847Seh146360 		if (secpolicy_net_config != NULL)
21913847Seh146360 			ret = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
21923847Seh146360 		else
21933847Seh146360 			ret = drv_priv(iocp->ioc_cr);
21943847Seh146360 		if (ret != 0) {
21953847Seh146360 			miocnak(q, m, 0, ret);
21963847Seh146360 			return (IEEE80211_IOCTL_NOT_REQUIRED);
21973847Seh146360 		}
21983847Seh146360 	}
21993847Seh146360 	/*
22003847Seh146360 	 * sanity check
22013847Seh146360 	 */
22023847Seh146360 	m0 = m->b_cont;
22033847Seh146360 	if (iocp->ioc_count == 0 || iocp->ioc_count < sizeof (wldp_t) ||
22043847Seh146360 	    m0 == NULL) {
22053847Seh146360 		miocnak(q, m, 0, EINVAL);
22063847Seh146360 		return (IEEE80211_IOCTL_NOT_REQUIRED);
22073847Seh146360 	}
22083847Seh146360 	/*
22093847Seh146360 	 * assuming single data block
22103847Seh146360 	 */
22113847Seh146360 	if (m0->b_cont) {
22123847Seh146360 		freemsg(m0->b_cont);
22133847Seh146360 		m0->b_cont = NULL;
22143847Seh146360 	}
22153847Seh146360 
22163847Seh146360 	need_net80211 = B_FALSE;
22173847Seh146360 	ret = ipw2100_getset(sc, m0, cmd, &need_net80211);
22183847Seh146360 	if (!need_net80211) {
22193847Seh146360 		len = msgdsize(m0);
22203847Seh146360 
22213847Seh146360 		IPW2100_DBG(IPW2100_DBG_IOCTL, (sc->sc_dip, CE_CONT,
22223847Seh146360 		    "ipw2100_ioctl(): go to call miocack with "
22233847Seh146360 		    "ret = %d, len = %d\n", ret, len));
22243847Seh146360 		miocack(q, m, len, ret);
22253847Seh146360 		return (IEEE80211_IOCTL_NOT_REQUIRED);
22263847Seh146360 	}
22273847Seh146360 
22283847Seh146360 	/*
22293847Seh146360 	 * IEEE80211_IOCTL_REQUIRED - need net80211 handle
22303847Seh146360 	 */
22313847Seh146360 	return (IEEE80211_IOCTL_REQUIRED);
22323847Seh146360 }
22333847Seh146360 
22343847Seh146360 static int
22353847Seh146360 ipw2100_getset(struct ipw2100_softc *sc, mblk_t *m, uint32_t cmd,
22363847Seh146360 	boolean_t *need_net80211)
22373847Seh146360 {
22383847Seh146360 	wldp_t		*infp, *outfp;
22393847Seh146360 	uint32_t	id;
22403847Seh146360 	int		ret; /* IEEE80211_IOCTL - handled by net80211 */
22413847Seh146360 
22423847Seh146360 	infp = (wldp_t *)m->b_rptr;
22433847Seh146360 	outfp = (wldp_t *)m->b_rptr;
22443847Seh146360 	outfp->wldp_result = WL_NOTSUPPORTED;
22453847Seh146360 
22463847Seh146360 	id = infp->wldp_id;
22473847Seh146360 	IPW2100_DBG(IPW2100_DBG_IOCTL, (sc->sc_dip, CE_CONT,
22483847Seh146360 	    "ipw2100_getset(): id = 0x%x\n", id));
22493847Seh146360 	switch (id) {
22503847Seh146360 	/*
22513847Seh146360 	 * which is not supported by net80211, so it
22523847Seh146360 	 * has to be handled from driver side
22533847Seh146360 	 */
22543847Seh146360 	case WL_RADIO:
22553847Seh146360 		ret = ipw_wificfg_radio(sc, cmd, outfp);
22563847Seh146360 		break;
22573847Seh146360 	/*
22583847Seh146360 	 * so far, drier doesn't support fix-rates
22593847Seh146360 	 */
22603847Seh146360 	case WL_DESIRED_RATES:
22613847Seh146360 		ret = ipw_wificfg_desrates(outfp);
22623847Seh146360 		break;
22633847Seh146360 	/*
22643847Seh146360 	 * current net80211 implementation clears the bssid while
22653847Seh146360 	 * this command received, which will result in the all zero
22663847Seh146360 	 * mac address for scan'ed AP which is just disconnected.
22673847Seh146360 	 * This is a workaround solution until net80211 find a
22683847Seh146360 	 * better method.
22693847Seh146360 	 */
22703847Seh146360 	case WL_DISASSOCIATE:
22713847Seh146360 		ret = ipw_wificfg_disassoc(sc, outfp);
22723847Seh146360 		break;
22733847Seh146360 	default:
22743847Seh146360 		/*
22753847Seh146360 		 * The wifi IOCTL net80211 supported:
22763847Seh146360 		 *	case WL_ESSID:
22773847Seh146360 		 *	case WL_BSSID:
22783847Seh146360 		 *	case WL_WEP_KEY_TAB:
22793847Seh146360 		 *	case WL_WEP_KEY_ID:
22803847Seh146360 		 *	case WL_AUTH_MODE:
22813847Seh146360 		 *	case WL_ENCRYPTION:
22823847Seh146360 		 *	case WL_BSS_TYPE:
22833847Seh146360 		 *	case WL_ESS_LIST:
22843847Seh146360 		 *	case WL_LINKSTATUS:
22853847Seh146360 		 *	case WL_RSSI:
22863847Seh146360 		 *	case WL_SCAN:
22873847Seh146360 		 *	case WL_LOAD_DEFAULTS:
22883847Seh146360 		 */
22893847Seh146360 		*need_net80211 = B_TRUE; /* let net80211 do the rest */
22903847Seh146360 		return (0);
22913847Seh146360 	}
22923847Seh146360 	/*
22933847Seh146360 	 * we will overwrite everything
22943847Seh146360 	 */
22953847Seh146360 	m->b_wptr = m->b_rptr + outfp->wldp_length;
22963847Seh146360 
22973847Seh146360 	return (ret);
22983847Seh146360 }
22993847Seh146360 
23003847Seh146360 static int
23013847Seh146360 ipw_wificfg_radio(struct ipw2100_softc *sc, uint32_t cmd, wldp_t *outfp)
23023847Seh146360 {
23033847Seh146360 	uint32_t	ret = ENOTSUP;
23043847Seh146360 
23053847Seh146360 	switch (cmd) {
23063847Seh146360 	case WLAN_GET_PARAM:
23073847Seh146360 		*(wl_linkstatus_t *)(outfp->wldp_buf) = ipw2100_get_radio(sc);
23083847Seh146360 		outfp->wldp_length = WIFI_BUF_OFFSET + sizeof (wl_linkstatus_t);
23093847Seh146360 		outfp->wldp_result = WL_SUCCESS;
23103847Seh146360 		ret = 0; /* command sucess */
23113847Seh146360 		break;
23123847Seh146360 	case WLAN_SET_PARAM:
23133847Seh146360 	default:
23143847Seh146360 		break;
23153847Seh146360 	}
23163847Seh146360 	return (ret);
23173847Seh146360 }
23183847Seh146360 
23193847Seh146360 static int
23203847Seh146360 ipw_wificfg_desrates(wldp_t *outfp)
23213847Seh146360 {
23223847Seh146360 	/*
23233847Seh146360 	 * return success, but with result NOTSUPPORTED
23243847Seh146360 	 */
23253847Seh146360 	outfp->wldp_length = WIFI_BUF_OFFSET;
23263847Seh146360 	outfp->wldp_result = WL_NOTSUPPORTED;
23273847Seh146360 	return (0);
23283847Seh146360 }
23293847Seh146360 
23303847Seh146360 static int
23313847Seh146360 ipw_wificfg_disassoc(struct ipw2100_softc *sc, wldp_t *outfp)
23323847Seh146360 {
23333847Seh146360 	struct ieee80211com	*ic = &sc->sc_ic;
23343847Seh146360 
23353847Seh146360 	/*
23363847Seh146360 	 * init the state
23373847Seh146360 	 */
23383847Seh146360 	if (ic->ic_state != IEEE80211_S_INIT) {
23393847Seh146360 		(void) ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
23403847Seh146360 	}
23413847Seh146360 
23423847Seh146360 	/*
23433847Seh146360 	 * return success always
23443847Seh146360 	 */
23453847Seh146360 	outfp->wldp_length = WIFI_BUF_OFFSET;
23463847Seh146360 	outfp->wldp_result = WL_SUCCESS;
23473847Seh146360 	return (0);
23483847Seh146360 }
23493847Seh146360 /* End of IOCTL Handler */
23503847Seh146360 
23513847Seh146360 static void
23523847Seh146360 ipw2100_fix_channel(struct ieee80211com *ic, mblk_t *m)
23533847Seh146360 {
23543847Seh146360 	struct ieee80211_frame	*wh;
23553847Seh146360 	uint8_t			subtype;
23563847Seh146360 	uint8_t			*frm, *efrm;
23573847Seh146360 
23583847Seh146360 	wh = (struct ieee80211_frame *)m->b_rptr;
23593847Seh146360 
23603847Seh146360 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
23613847Seh146360 		return;
23623847Seh146360 
23633847Seh146360 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
23643847Seh146360 
23653847Seh146360 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
23663847Seh146360 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
23673847Seh146360 		return;
23683847Seh146360 
23693847Seh146360 	/*
23703847Seh146360 	 * assume the message contains only 1 block
23713847Seh146360 	 */
23723847Seh146360 	frm   = (uint8_t *)(wh + 1);
23733847Seh146360 	efrm  = (uint8_t *)m->b_wptr;
23743847Seh146360 	frm  += 12;  /* skip tstamp, bintval and capinfo fields */
23753847Seh146360 	while (frm < efrm) {
23763847Seh146360 		if (*frm == IEEE80211_ELEMID_DSPARMS) {
23773847Seh146360 #if IEEE80211_CHAN_MAX < 255
2378*4784Seh146360 			if (frm[2] <= IEEE80211_CHAN_MAX)
23793847Seh146360 #endif
2380*4784Seh146360 			{
2381*4784Seh146360 				ic->ic_curchan = &ic->ic_sup_channels[frm[2]];
2382*4784Seh146360 			}
23833847Seh146360 		}
23843847Seh146360 		frm += frm[1] + 2;
23853847Seh146360 	}
23863847Seh146360 }
23873847Seh146360 
23883847Seh146360 static void
23893847Seh146360 ipw2100_rcvpkt(struct ipw2100_softc *sc, struct ipw2100_status *status,
23903847Seh146360     uint8_t *rxbuf)
23913847Seh146360 {
23923847Seh146360 	struct ieee80211com	*ic = &sc->sc_ic;
23933847Seh146360 	mblk_t			*m;
23943847Seh146360 	struct ieee80211_frame	*wh = (struct ieee80211_frame *)rxbuf;
23953847Seh146360 	struct ieee80211_node	*in;
23963847Seh146360 	uint32_t		rlen;
23973847Seh146360 
23983847Seh146360 	in = ieee80211_find_rxnode(ic, wh);
23993847Seh146360 	rlen = LE_32(status->len);
24003847Seh146360 	m = allocb(rlen, BPRI_MED);
24013847Seh146360 	if (m) {
24023847Seh146360 		(void) memcpy(m->b_wptr, rxbuf, rlen);
24033847Seh146360 		m->b_wptr += rlen;
24043847Seh146360 		if (ic->ic_state == IEEE80211_S_SCAN)
24053847Seh146360 			ipw2100_fix_channel(ic, m);
24063847Seh146360 		(void) ieee80211_input(ic, m, in, status->rssi, 0);
24073847Seh146360 	} else
24083847Seh146360 		IPW2100_WARN((sc->sc_dip, CE_WARN,
24093847Seh146360 		    "ipw2100_rcvpkg(): cannot allocate receive message(%u)\n",
24103847Seh146360 		    LE_32(status->len)));
24113847Seh146360 	ieee80211_free_node(in);
24123847Seh146360 }
24133847Seh146360 
24143847Seh146360 static uint_t
24153847Seh146360 ipw2100_intr(caddr_t arg)
24163847Seh146360 {
24173847Seh146360 	struct ipw2100_softc	*sc = (struct ipw2100_softc *)arg;
24183847Seh146360 	uint32_t		ireg, ridx, len, i;
24193847Seh146360 	struct ieee80211com	*ic = &sc->sc_ic;
24203847Seh146360 	struct ipw2100_status	*status;
24213847Seh146360 	uint8_t			*rxbuf;
24223847Seh146360 	struct dma_region	*dr;
24233847Seh146360 	uint32_t		state;
24243847Seh146360 #if DEBUG
24253847Seh146360 	struct ipw2100_bd *rxbd;
24263847Seh146360 #endif
24273847Seh146360 
24283847Seh146360 	ireg = ipw2100_csr_get32(sc, IPW2100_CSR_INTR);
24293847Seh146360 
24303847Seh146360 	if (!(ireg & IPW2100_INTR_MASK_ALL))
24313847Seh146360 		return (DDI_INTR_UNCLAIMED);
24323847Seh146360 
24333847Seh146360 	/*
24343847Seh146360 	 * mask all interrupts
24353847Seh146360 	 */
24363847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_INTR_MASK, 0);
24373847Seh146360 
24383847Seh146360 	/*
24393847Seh146360 	 * acknowledge all fired interrupts
24403847Seh146360 	 */
24413847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_INTR, ireg);
24423847Seh146360 
24433847Seh146360 	IPW2100_DBG(IPW2100_DBG_INT, (sc->sc_dip, CE_CONT,
24443847Seh146360 	    "ipw2100_intr(): interrupt is fired. int=0x%08x\n", ireg));
24453847Seh146360 
24463847Seh146360 	if (ireg & IPW2100_INTR_MASK_ERR) {
24473847Seh146360 
24483847Seh146360 		IPW2100_DBG(IPW2100_DBG_FATAL, (sc->sc_dip, CE_CONT,
24493847Seh146360 		    "ipw2100_intr(): interrupt is fired, MASK = 0x%08x\n",
24503847Seh146360 		    ireg));
24513847Seh146360 
24523847Seh146360 		/*
24533847Seh146360 		 * inform mfthread to recover hw error
24543847Seh146360 		 */
24553847Seh146360 		mutex_enter(&sc->sc_mflock);
24563847Seh146360 		sc->sc_flags |= IPW2100_FLAG_HW_ERR_RECOVER;
24573847Seh146360 		mutex_exit(&sc->sc_mflock);
24583847Seh146360 
24593847Seh146360 	} else {
24603847Seh146360 
24613847Seh146360 		/*
24623847Seh146360 		 * FW intr
24633847Seh146360 		 */
24643847Seh146360 		if (ireg & IPW2100_INTR_FW_INIT_DONE) {
24653847Seh146360 			mutex_enter(&sc->sc_ilock);
24663847Seh146360 			sc->sc_flags |= IPW2100_FLAG_FW_INITED;
24673847Seh146360 			cv_signal(&sc->sc_fw_cond);
24683847Seh146360 			mutex_exit(&sc->sc_ilock);
24693847Seh146360 		}
24703847Seh146360 
24713847Seh146360 		/*
24723847Seh146360 		 * RX intr
24733847Seh146360 		 */
24743847Seh146360 		if (ireg & IPW2100_INTR_RX_TRANSFER) {
24753847Seh146360 			ridx = ipw2100_csr_get32(sc,
24763847Seh146360 			    IPW2100_CSR_RX_READ_INDEX);
24773847Seh146360 
24783847Seh146360 			for (; sc->sc_rx_cur != ridx;
24793847Seh146360 			    sc->sc_rx_cur = RING_FORWARD(
2480*4784Seh146360 			    sc->sc_rx_cur, 1, IPW2100_NUM_RXBD)) {
24813847Seh146360 
24823847Seh146360 				i	= sc->sc_rx_cur;
24833847Seh146360 				status	= &sc->sc_status[i];
24843847Seh146360 				rxbuf	= &sc->sc_rxbufs[i]->rxb_dat[0];
24853847Seh146360 				dr	= &sc->sc_dma_rxbufs[i];
24863847Seh146360 
24873847Seh146360 				/*
24883847Seh146360 				 * sync
24893847Seh146360 				 */
24903847Seh146360 				(void) ddi_dma_sync(sc->sc_dma_status.dr_hnd,
24913847Seh146360 				    i * sizeof (struct ipw2100_status),
24923847Seh146360 				    sizeof (struct ipw2100_status),
24933847Seh146360 				    DDI_DMA_SYNC_FORKERNEL);
24943847Seh146360 				(void) ddi_dma_sync(sc->sc_dma_rxbd.dr_hnd,
24953847Seh146360 				    i * sizeof (struct ipw2100_bd),
24963847Seh146360 				    sizeof (struct ipw2100_bd),
24973847Seh146360 				    DDI_DMA_SYNC_FORKERNEL);
24983847Seh146360 				(void) ddi_dma_sync(dr->dr_hnd, 0,
24993847Seh146360 				    sizeof (struct ipw2100_rxb),
25003847Seh146360 				    DDI_DMA_SYNC_FORKERNEL);
25013847Seh146360 				IPW2100_DBG(IPW2100_DBG_INT,
25023847Seh146360 				    (sc->sc_dip, CE_CONT,
25033847Seh146360 				    "ipw2100_intr(): "
25043847Seh146360 				    "status code=0x%04x, len=0x%08x, "
25053847Seh146360 				    "flags=0x%02x, rssi=%02x\n",
25063847Seh146360 				    LE_16(status->code), LE_32(status->len),
25073847Seh146360 				    status->flags, status->rssi));
25083847Seh146360 #if DEBUG
25093847Seh146360 				rxbd	= &sc->sc_rxbd[i];
25103847Seh146360 				IPW2100_DBG(IPW2100_DBG_INT,
25113847Seh146360 				    (sc->sc_dip, CE_CONT,
25123847Seh146360 				    "ipw2100_intr(): "
25133847Seh146360 				    "rxbd,phyaddr=0x%08x, len=0x%08x, "
25143847Seh146360 				    "flags=0x%02x,nfrag=%02x\n",
25153847Seh146360 				    LE_32(rxbd->phyaddr), LE_32(rxbd->len),
25163847Seh146360 				    rxbd->flags, rxbd->nfrag));
25173847Seh146360 #endif
25183847Seh146360 				switch (LE_16(status->code) & 0x0f) {
25193847Seh146360 				/*
25203847Seh146360 				 * command complete response
25213847Seh146360 				 */
25223847Seh146360 				case IPW2100_STATUS_CODE_COMMAND:
25233847Seh146360 					mutex_enter(&sc->sc_ilock);
25243847Seh146360 					sc->sc_done = 1;
25253847Seh146360 					cv_signal(&sc->sc_cmd_cond);
25263847Seh146360 					mutex_exit(&sc->sc_ilock);
25273847Seh146360 					break;
25283847Seh146360 				/*
25293847Seh146360 				 * change state
25303847Seh146360 				 */
25313847Seh146360 				case IPW2100_STATUS_CODE_NEWSTATE:
25323847Seh146360 					state = LE_32(*((uint32_t *)rxbuf));
25333847Seh146360 					IPW2100_DBG(IPW2100_DBG_INT,
25343847Seh146360 					    (sc->sc_dip, CE_CONT,
25353847Seh146360 					    "ipw2100_intr(): "
25363847Seh146360 					    "newstate,state=0x%x\n", state));
25373847Seh146360 
25383847Seh146360 					switch (state) {
25393847Seh146360 					case IPW2100_STATE_ASSOCIATED:
25403847Seh146360 						ieee80211_new_state(ic,
25413847Seh146360 						    IEEE80211_S_RUN, -1);
25423847Seh146360 						break;
25433847Seh146360 					case IPW2100_STATE_ASSOCIATION_LOST:
25443847Seh146360 					case IPW2100_STATE_DISABLED:
25453847Seh146360 						ieee80211_new_state(ic,
25463847Seh146360 						    IEEE80211_S_INIT, -1);
25473847Seh146360 						break;
25483847Seh146360 					case IPW2100_STATE_RADIO_DISABLED:
25493847Seh146360 						IPW2100_REPORT((sc->sc_dip,
25503847Seh146360 						    CE_WARN,
25513847Seh146360 						    "ipw2100_intr(): "
25523847Seh146360 						    "RADIO is OFF\n"));
25533847Seh146360 						ipw2100_stop(sc);
25543847Seh146360 						break;
25553847Seh146360 					case IPW2100_STATE_SCAN_COMPLETE:
25563847Seh146360 						ieee80211_cancel_scan(ic);
25573847Seh146360 						break;
25583847Seh146360 					case IPW2100_STATE_SCANNING:
25593847Seh146360 						if (ic->ic_state !=
25603847Seh146360 						    IEEE80211_S_RUN)
25613847Seh146360 							ieee80211_new_state(ic,
25623847Seh146360 							    IEEE80211_S_SCAN,
25633847Seh146360 							    -1);
25643847Seh146360 						ic->ic_flags |=
2565*4784Seh146360 						    IEEE80211_F_SCAN;
25663847Seh146360 
25673847Seh146360 						break;
25683847Seh146360 					default:
25693847Seh146360 						break;
25703847Seh146360 					}
25713847Seh146360 					break;
25723847Seh146360 				case IPW2100_STATUS_CODE_DATA_802_11:
25733847Seh146360 				case IPW2100_STATUS_CODE_DATA_802_3:
25743847Seh146360 					ipw2100_rcvpkt(sc, status, rxbuf);
25753847Seh146360 					break;
25763847Seh146360 				case IPW2100_STATUS_CODE_NOTIFICATION:
25773847Seh146360 					break;
25783847Seh146360 				default:
25793847Seh146360 					IPW2100_WARN((sc->sc_dip, CE_WARN,
25803847Seh146360 					    "ipw2100_intr(): "
25813847Seh146360 					    "unknown status code 0x%04x\n",
25823847Seh146360 					    LE_16(status->code)));
25833847Seh146360 					break;
25843847Seh146360 				}
25853847Seh146360 			}
25863847Seh146360 			/*
25873847Seh146360 			 * write sc_rx_cur backward 1 step to RX_WRITE_INDEX
25883847Seh146360 			 */
25893847Seh146360 			ipw2100_csr_put32(sc, IPW2100_CSR_RX_WRITE_INDEX,
25903847Seh146360 			    RING_BACKWARD(sc->sc_rx_cur, 1, IPW2100_NUM_RXBD));
25913847Seh146360 		}
25923847Seh146360 
25933847Seh146360 		/*
25943847Seh146360 		 * TX intr
25953847Seh146360 		 */
25963847Seh146360 		if (ireg & IPW2100_INTR_TX_TRANSFER) {
25973847Seh146360 			mutex_enter(&sc->sc_tx_lock);
25983847Seh146360 			ridx = ipw2100_csr_get32(sc, IPW2100_CSR_TX_READ_INDEX);
25993847Seh146360 			len = RING_FLEN(RING_FORWARD(sc->sc_tx_cur,
26003847Seh146360 			    sc->sc_tx_free, IPW2100_NUM_TXBD),
26013847Seh146360 			    ridx, IPW2100_NUM_TXBD);
26023847Seh146360 			sc->sc_tx_free += len;
26033847Seh146360 			IPW2100_DBG(IPW2100_DBG_INT, (sc->sc_dip, CE_CONT,
26043847Seh146360 			    "ipw2100_intr(): len=%d\n", len));
26053847Seh146360 			mutex_exit(&sc->sc_tx_lock);
26063847Seh146360 
26073847Seh146360 			mutex_enter(&sc->sc_resched_lock);
26083847Seh146360 			if (len > 1 && (sc->sc_flags & IPW2100_FLAG_TX_SCHED)) {
26093847Seh146360 				sc->sc_flags &= ~IPW2100_FLAG_TX_SCHED;
26103847Seh146360 				mac_tx_update(ic->ic_mach);
26113847Seh146360 			}
26123847Seh146360 			mutex_exit(&sc->sc_resched_lock);
26133847Seh146360 		}
26143847Seh146360 	}
26153847Seh146360 
26163847Seh146360 	/*
26173847Seh146360 	 * enable all interrupts
26183847Seh146360 	 */
26193847Seh146360 	ipw2100_csr_put32(sc, IPW2100_CSR_INTR_MASK, IPW2100_INTR_MASK_ALL);
26203847Seh146360 
26213847Seh146360 	return (DDI_INTR_CLAIMED);
26223847Seh146360 }
26233847Seh146360 
26243847Seh146360 
26253847Seh146360 /*
26263847Seh146360  * Module Loading Data & Entry Points
26273847Seh146360  */
26283847Seh146360 DDI_DEFINE_STREAM_OPS(ipw2100_devops, nulldev, nulldev, ipw2100_attach,
2629*4784Seh146360     ipw2100_detach, ipw2100_reset, NULL, D_MP, NULL);
26303847Seh146360 
26313847Seh146360 static struct modldrv ipw2100_modldrv = {
26323847Seh146360 	&mod_driverops,
26333847Seh146360 	ipw2100_ident,
26343847Seh146360 	&ipw2100_devops
26353847Seh146360 };
26363847Seh146360 
26373847Seh146360 static struct modlinkage ipw2100_modlinkage = {
26383847Seh146360 	MODREV_1,
26393847Seh146360 	&ipw2100_modldrv,
26403847Seh146360 	NULL
26413847Seh146360 };
26423847Seh146360 
26433847Seh146360 int
26443847Seh146360 _init(void)
26453847Seh146360 {
26463847Seh146360 	int	status;
26473847Seh146360 
26483847Seh146360 	status = ddi_soft_state_init(&ipw2100_ssp,
26493847Seh146360 	    sizeof (struct ipw2100_softc), 1);
26503847Seh146360 	if (status != DDI_SUCCESS)
26513847Seh146360 		return (status);
26523847Seh146360 
26533847Seh146360 	mac_init_ops(&ipw2100_devops, IPW2100_DRV_NAME);
26543847Seh146360 	status = mod_install(&ipw2100_modlinkage);
26553847Seh146360 	if (status != DDI_SUCCESS) {
26563847Seh146360 		mac_fini_ops(&ipw2100_devops);
26573847Seh146360 		ddi_soft_state_fini(&ipw2100_ssp);
26583847Seh146360 	}
26593847Seh146360 
26603847Seh146360 	return (status);
26613847Seh146360 }
26623847Seh146360 
26633847Seh146360 int
26643847Seh146360 _fini(void)
26653847Seh146360 {
26663847Seh146360 	int status;
26673847Seh146360 
26683847Seh146360 	status = mod_remove(&ipw2100_modlinkage);
26693847Seh146360 	if (status == DDI_SUCCESS) {
26703847Seh146360 		mac_fini_ops(&ipw2100_devops);
26713847Seh146360 		ddi_soft_state_fini(&ipw2100_ssp);
26723847Seh146360 	}
26733847Seh146360 
26743847Seh146360 	return (status);
26753847Seh146360 }
26763847Seh146360 
26773847Seh146360 int
26783847Seh146360 _info(struct modinfo *mip)
26793847Seh146360 {
26803847Seh146360 	return (mod_info(&ipw2100_modlinkage, mip));
26813847Seh146360 }
2682