xref: /titanic_41/usr/src/uts/common/io/iwi/ipw2200.c (revision 186f7fbf5e07d046b50e4e15c32b21f109b76c80)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 2004, 2005
8  *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice unmodified, this list of conditions, and the following
15  *    disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/byteorder.h>
35 #include <sys/conf.h>
36 #include <sys/cmn_err.h>
37 #include <sys/stat.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/strsubr.h>
41 #include <sys/ethernet.h>
42 #include <inet/common.h>
43 #include <inet/nd.h>
44 #include <inet/mi.h>
45 #include <sys/note.h>
46 #include <sys/stream.h>
47 #include <sys/strsun.h>
48 #include <sys/modctl.h>
49 #include <sys/devops.h>
50 #include <sys/dlpi.h>
51 #include <sys/mac.h>
52 #include <sys/mac_wifi.h>
53 #include <sys/varargs.h>
54 #include <sys/pci.h>
55 #include <sys/policy.h>
56 #include <sys/random.h>
57 #include <sys/crypto/common.h>
58 #include <sys/crypto/api.h>
59 
60 #include "ipw2200.h"
61 #include "ipw2200_impl.h"
62 #include <inet/wifi_ioctl.h>
63 
64 /*
65  * for net80211 kernel usage
66  */
67 #include <sys/net80211.h>
68 #include <sys/net80211_proto.h>
69 
70 /*
71  * minimal size reserved in tx-ring
72  */
73 #define	IPW2200_TX_RING_MIN	(8)
74 #define	IPW2200_TXBUF_SIZE	(IEEE80211_MAX_LEN)
75 #define	IPW2200_RXBUF_SIZE	(4096)
76 
77 static void  *ipw2200_ssp = NULL;
78 static char ipw2200_ident[] = IPW2200_DRV_DESC;
79 
80 /*
81  * PIO access attributor for registers
82  */
83 static ddi_device_acc_attr_t ipw2200_csr_accattr = {
84 	DDI_DEVICE_ATTR_V0,
85 	DDI_STRUCTURE_LE_ACC,
86 	DDI_STRICTORDER_ACC
87 };
88 
89 /*
90  * DMA access attributor for descriptors
91  */
92 static ddi_device_acc_attr_t ipw2200_dma_accattr = {
93 	DDI_DEVICE_ATTR_V0,
94 	DDI_NEVERSWAP_ACC,
95 	DDI_STRICTORDER_ACC
96 };
97 
98 /*
99  * Describes the chip's DMA engine
100  */
101 static ddi_dma_attr_t ipw2200_dma_attr = {
102 	DMA_ATTR_V0,		/* version */
103 	0x0000000000000000ULL,  /* addr_lo */
104 	0x00000000ffffffffULL,  /* addr_hi */
105 	0x00000000ffffffffULL,  /* counter */
106 	0x0000000000000004ULL,  /* alignment */
107 	0xfff,			/* burst */
108 	1,			/* min xfer */
109 	0x00000000ffffffffULL,  /* max xfer */
110 	0x00000000ffffffffULL,  /* seg boud */
111 	1,			/* s/g list */
112 	1,			/* granularity */
113 	0			/* flags */
114 };
115 
116 static uint8_t ipw2200_broadcast_addr[] = {
117 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
118 };
119 static const struct ieee80211_rateset ipw2200_rateset_11a = { 8,
120 	{12, 18, 24, 36, 48, 72, 96, 108}
121 };
122 static const struct ieee80211_rateset ipw2200_rateset_11b = { 4,
123 	{2, 4, 11, 22}
124 };
125 static const struct ieee80211_rateset ipw2200_rateset_11g = { 12,
126 	{2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108}
127 };
128 
129 /*
130  * Used by multi function thread
131  */
132 extern pri_t minclsyspri;
133 
134 /*
135  * ipw2200 specific hardware operations
136  */
137 static void	ipw2200_hwconf_get(struct ipw2200_softc *sc);
138 static int	ipw2200_chip_reset(struct ipw2200_softc *sc);
139 static void	ipw2200_master_stop(struct ipw2200_softc *sc);
140 static void	ipw2200_stop(struct ipw2200_softc *sc);
141 static int	ipw2200_config(struct ipw2200_softc *sc);
142 static int	ipw2200_cmd(struct ipw2200_softc *sc,
143     uint32_t type, void *buf, size_t len, int async);
144 static void	ipw2200_ring_hwsetup(struct ipw2200_softc *sc);
145 static int	ipw2200_ring_alloc(struct ipw2200_softc *sc);
146 static void	ipw2200_ring_free(struct ipw2200_softc *sc);
147 static void	ipw2200_ring_reset(struct ipw2200_softc *sc);
148 static int	ipw2200_ring_init(struct ipw2200_softc *sc);
149 
150 /*
151  * GLD specific operations
152  */
153 static int	ipw2200_m_stat(void *arg, uint_t stat, uint64_t *val);
154 static int	ipw2200_m_start(void *arg);
155 static void	ipw2200_m_stop(void *arg);
156 static int	ipw2200_m_unicst(void *arg, const uint8_t *macaddr);
157 static int	ipw2200_m_multicst(void *arg, boolean_t add, const uint8_t *m);
158 static int	ipw2200_m_promisc(void *arg, boolean_t on);
159 static void	ipw2200_m_ioctl(void *arg, queue_t *wq, mblk_t *mp);
160 static mblk_t  *ipw2200_m_tx(void *arg, mblk_t *mp);
161 
162 /*
163  * Interrupt and Data transferring operations
164  */
165 static uint_t	ipw2200_intr(caddr_t arg);
166 static int	ipw2200_send(struct ieee80211com *ic, mblk_t *mp, uint8_t type);
167 static void	ipw2200_rcv_frame(struct ipw2200_softc *sc,
168     struct ipw2200_frame *frame);
169 static void	ipw2200_rcv_notif(struct ipw2200_softc *sc,
170     struct ipw2200_notif *notif);
171 
172 /*
173  * WiFi specific operations
174  */
175 static int	ipw2200_newstate(struct ieee80211com *ic,
176     enum ieee80211_state state, int arg);
177 static void	ipw2200_thread(struct ipw2200_softc *sc);
178 
179 /*
180  * IOCTL Handler
181  */
182 static int	ipw2200_ioctl(struct ipw2200_softc *sc, queue_t *q, mblk_t *m);
183 static int	ipw2200_getset(struct ipw2200_softc *sc,
184     mblk_t *m, uint32_t cmd, boolean_t *need_net80211);
185 static int	iwi_wificfg_radio(struct ipw2200_softc *sc,
186     uint32_t cmd,  wldp_t *outfp);
187 static int	iwi_wificfg_desrates(wldp_t *outfp);
188 
189 /*
190  * net80211 functions
191  */
192 extern uint8_t	ieee80211_crypto_getciphertype(ieee80211com_t *ic);
193 extern void	ieee80211_notify_node_join(ieee80211com_t *ic,
194     ieee80211_node_t *in);
195 extern void	ieee80211_notify_node_leave(ieee80211com_t *ic,
196     ieee80211_node_t *in);
197 
198 /*
199  * Mac Call Back entries
200  */
201 mac_callbacks_t	ipw2200_m_callbacks = {
202 	MC_IOCTL,
203 	ipw2200_m_stat,
204 	ipw2200_m_start,
205 	ipw2200_m_stop,
206 	ipw2200_m_promisc,
207 	ipw2200_m_multicst,
208 	ipw2200_m_unicst,
209 	ipw2200_m_tx,
210 	NULL,
211 	ipw2200_m_ioctl
212 };
213 
214 /*
215  * DEBUG Facility
216  */
217 #define		MAX_MSG		(128)
218 uint32_t	ipw2200_debug = 0;
219 /*
220  * supported debug marks are:
221  *	| IPW2200_DBG_CSR
222  *	| IPW2200_DBG_TABLE
223  *	| IPW2200_DBG_HWCAP
224  *	| IPW2200_DBG_TX
225  *	| IPW2200_DBG_INIT
226  *	| IPW2200_DBG_FW
227  *	| IPW2200_DBG_NOTIF
228  *	| IPW2200_DBG_SCAN
229  *	| IPW2200_DBG_IOCTL
230  *	| IPW2200_DBG_RING
231  *	| IPW2200_DBG_INT
232  *	| IPW2200_DBG_RX
233  *	| IPW2200_DBG_DMA
234  *	| IPW2200_DBG_GLD
235  *	| IPW2200_DBG_WIFI
236  *	| IPW2200_DBG_SOFTINT
237  *	| IPW2200_DBG_SUSPEND
238  */
239 
240 /*
241  * Global tunning parameter to work around unknown hardware issues
242  */
243 static uint32_t delay_config_stable	= 100000;	/* 100ms */
244 static uint32_t delay_fatal_recover	= 100000 * 20;	/* 2s */
245 static uint32_t delay_aux_thread	= 100000;	/* 100ms */
246 
247 #define	IEEE80211_IS_CHAN_2GHZ(_c) \
248 	(((_c)->ich_flags & IEEE80211_CHAN_2GHZ) != 0)
249 #define	IEEE80211_IS_CHAN_5GHZ(_c) \
250 	(((_c)->ich_flags & IEEE80211_CHAN_5GHZ) != 0)
251 #define	isset(a, i)	((a)[(i)/NBBY] & (1 << ((i)%NBBY)))
252 
253 void
254 ipw2200_dbg(dev_info_t *dip, int level, const char *fmt, ...)
255 {
256 	va_list	ap;
257 	char    buf[MAX_MSG];
258 	int	instance;
259 
260 	va_start(ap, fmt);
261 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
262 	va_end(ap);
263 
264 	if (dip) {
265 		instance = ddi_get_instance(dip);
266 		cmn_err(level, "%s%d: %s", IPW2200_DRV_NAME, instance, buf);
267 	} else
268 		cmn_err(level, "%s: %s", IPW2200_DRV_NAME, buf);
269 
270 }
271 
272 /*
273  * Set up pci
274  */
275 int
276 ipw2200_setup_pci(dev_info_t *dip, struct ipw2200_softc *sc)
277 {
278 	ddi_acc_handle_t	cfgh;
279 	caddr_t			regs;
280 	int			err;
281 
282 	/*
283 	 * Map config spaces register to read the vendor id, device id, sub
284 	 * vendor id, and sub device id.
285 	 */
286 	err = ddi_regs_map_setup(dip, IPW2200_PCI_CFG_RNUM, &regs,
287 	    0, 0, &ipw2200_csr_accattr, &cfgh);
288 	if (err != DDI_SUCCESS) {
289 		IPW2200_WARN((dip, CE_WARN,
290 		    "ipw2200_attach(): unable to map spaces regs\n"));
291 		return (DDI_FAILURE);
292 	}
293 
294 	ddi_put8(cfgh, (uint8_t *)(regs + 0x41), 0);
295 	sc->sc_vendor = ddi_get16(cfgh,
296 	    (uint16_t *)((uintptr_t)regs + PCI_CONF_VENID));
297 	sc->sc_device = ddi_get16(cfgh,
298 	    (uint16_t *)((uintptr_t)regs + PCI_CONF_DEVID));
299 	sc->sc_subven = ddi_get16(cfgh,
300 	    (uint16_t *)((uintptr_t)regs + PCI_CONF_SUBVENID));
301 	sc->sc_subdev = ddi_get16(cfgh,
302 	    (uint16_t *)((uintptr_t)regs + PCI_CONF_SUBSYSID));
303 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
304 	    "ipw2200_setup_pci(): vendor = 0x%04x, devic = 0x%04x,"
305 	    "subversion = 0x%04x, subdev = 0x%04x",
306 	    sc->sc_vendor, sc->sc_device, sc->sc_subven, sc->sc_subdev));
307 
308 	ddi_regs_map_free(&cfgh);
309 
310 	return (DDI_SUCCESS);
311 
312 }
313 
314 /*
315  * Device operations
316  */
317 int
318 ipw2200_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
319 {
320 	struct ipw2200_softc	*sc;
321 	struct ieee80211com	*ic;
322 	int			instance, err, i;
323 	char			strbuf[32];
324 	wifi_data_t		wd = { 0 };
325 	mac_register_t		*macp;
326 
327 	switch (cmd) {
328 	case DDI_ATTACH:
329 		break;
330 	case DDI_RESUME:
331 		sc = ddi_get_soft_state(ipw2200_ssp, ddi_get_instance(dip));
332 		ASSERT(sc != NULL);
333 
334 		/*
335 		 * set up pci
336 		 */
337 		err = ipw2200_setup_pci(dip, sc);
338 		if (err != DDI_SUCCESS) {
339 			IPW2200_DBG(IPW2200_DBG_SUSPEND, (sc->sc_dip, CE_CONT,
340 			    "ipw2200_attach(): resume failure\n"));
341 			return (DDI_FAILURE);
342 		}
343 
344 		/*
345 		 * resume hardware.
346 		 * If it was on runnning status, reset to INIT state
347 		 */
348 		sc->sc_flags &= ~IPW2200_FLAG_SUSPEND;
349 		if (sc->sc_flags & IPW2200_FLAG_RUNNING)
350 			(void) ipw2200_init(sc);
351 
352 		IPW2200_DBG(IPW2200_DBG_SUSPEND, (sc->sc_dip, CE_CONT,
353 		    "ipw2200_attach(): resume successful\n"));
354 		return (DDI_SUCCESS);
355 	default:
356 		return (DDI_FAILURE);
357 	}
358 
359 	instance = ddi_get_instance(dip);
360 	err = ddi_soft_state_zalloc(ipw2200_ssp, instance);
361 	if (err != DDI_SUCCESS) {
362 		IPW2200_WARN((dip, CE_WARN,
363 		    "ipw2200_attach(): unable to allocate soft state\n"));
364 		goto fail1;
365 	}
366 	sc = ddi_get_soft_state(ipw2200_ssp, instance);
367 	sc->sc_dip = dip;
368 
369 	/* set up pci, put reg+0x41 0 */
370 	err = ipw2200_setup_pci(dip, sc);
371 	if (err != DDI_SUCCESS) {
372 		IPW2200_WARN((dip, CE_WARN,
373 		    "ipw2200_attach(): unable to setup pci\n"));
374 		goto fail2;
375 	}
376 
377 	/*
378 	 * Map operating registers
379 	 */
380 	err = ddi_regs_map_setup(dip, IPW2200_PCI_CSR_RNUM, &sc->sc_regs,
381 	    0, 0, &ipw2200_csr_accattr, &sc->sc_ioh);
382 	if (err != DDI_SUCCESS) {
383 		IPW2200_WARN((dip, CE_WARN,
384 		    "ipw2200_attach(): ddi_regs_map_setup() failed\n"));
385 		goto fail2;
386 	}
387 
388 	/*
389 	 * Reset the chip
390 	 */
391 	err = ipw2200_chip_reset(sc);
392 	if (err != DDI_SUCCESS) {
393 		IPW2200_WARN((dip, CE_WARN,
394 		    "ipw2200_attach(): ipw2200_chip_reset() failed\n"));
395 		goto fail3;
396 	}
397 
398 	/*
399 	 * Get the hardware configuration, including the MAC address
400 	 * Then, init all the rings needed.
401 	 */
402 	ipw2200_hwconf_get(sc);
403 	err = ipw2200_ring_init(sc);
404 	if (err != DDI_SUCCESS) {
405 		IPW2200_WARN((dip, CE_WARN,
406 		    "ipw2200_attach(): ipw2200_ring_init() failed\n"));
407 		goto fail3;
408 	}
409 
410 	/*
411 	 * Initialize mutexs and condvars
412 	 */
413 	err = ddi_get_iblock_cookie(dip, 0, &sc->sc_iblk);
414 	if (err != DDI_SUCCESS) {
415 		IPW2200_WARN((dip, CE_WARN,
416 		    "ipw2200_attach(): ddi_get_iblock_cookie() failed\n"));
417 		goto fail4;
418 	}
419 
420 	/*
421 	 * interrupt lock
422 	 */
423 	mutex_init(&sc->sc_ilock, "intr-lock", MUTEX_DRIVER,
424 	    (void *) sc->sc_iblk);
425 	cv_init(&sc->sc_fw_cond, "firmware-ok", CV_DRIVER, NULL);
426 	cv_init(&sc->sc_cmd_status_cond, "cmd-status-ring", CV_DRIVER, NULL);
427 
428 	/*
429 	 * command ring lock
430 	 */
431 	mutex_init(&sc->sc_cmd_lock, "cmd-ring", MUTEX_DRIVER,
432 	    (void *) sc->sc_iblk);
433 	cv_init(&sc->sc_cmd_cond, "cmd-ring", CV_DRIVER, NULL);
434 
435 	/*
436 	 * tx ring lock
437 	 */
438 	mutex_init(&sc->sc_tx_lock, "tx-ring", MUTEX_DRIVER,
439 	    (void *) sc->sc_iblk);
440 
441 	/*
442 	 * rescheduled lock
443 	 */
444 	mutex_init(&sc->sc_resched_lock, "reschedule-lock", MUTEX_DRIVER,
445 	    (void *) sc->sc_iblk);
446 
447 	/*
448 	 * multi-function lock, may acquire this during interrupt
449 	 */
450 	mutex_init(&sc->sc_mflock, "function-lock", MUTEX_DRIVER,
451 	    (void *) sc->sc_iblk);
452 	cv_init(&sc->sc_mfthread_cv, NULL, CV_DRIVER, NULL);
453 	sc->sc_mf_thread = NULL;
454 	sc->sc_mfthread_switch = 0;
455 
456 	/*
457 	 * Initialize the WiFi part
458 	 */
459 	ic = &sc->sc_ic;
460 	ic->ic_phytype  = IEEE80211_T_OFDM;
461 	ic->ic_opmode   = IEEE80211_M_STA;
462 	ic->ic_state    = IEEE80211_S_INIT;
463 	ic->ic_maxrssi  = 100; /* experimental number */
464 	ic->ic_caps =
465 	    IEEE80211_C_SHPREAMBLE |
466 	    IEEE80211_C_TXPMGT |
467 	    IEEE80211_C_PMGT |
468 	    IEEE80211_C_WPA;
469 
470 	/*
471 	 * set mac addr
472 	 */
473 	IEEE80211_ADDR_COPY(ic->ic_macaddr, sc->sc_macaddr);
474 
475 	/*
476 	 * set supported .11a rates and channel - (2915ABG only)
477 	 */
478 	if (sc->sc_device >= 0x4223) {
479 		/* .11a rates */
480 		ic->ic_sup_rates[IEEE80211_MODE_11A] = ipw2200_rateset_11a;
481 		/* .11a channels */
482 		for (i = 36; i <= 64; i += 4) {
483 			ic->ic_sup_channels[i].ich_freq =
484 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
485 			ic->ic_sup_channels[i].ich_flags = /* CHAN_A */
486 			    IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_OFDM;
487 		}
488 		for (i = 149; i <= 165; i += 4) {
489 			ic->ic_sup_channels[i].ich_freq =
490 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
491 			ic->ic_sup_channels[i].ich_flags = /* CHAN_A */
492 			    IEEE80211_CHAN_5GHZ | IEEE80211_CHAN_OFDM;
493 		}
494 	}
495 
496 	/*
497 	 * set supported .11b and .11g rates
498 	 */
499 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ipw2200_rateset_11b;
500 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ipw2200_rateset_11g;
501 
502 	/*
503 	 * set supported .11b and .11g channels(1 through 14)
504 	 */
505 	for (i = 1; i < 14; i++) {
506 		ic->ic_sup_channels[i].ich_freq  =
507 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
508 		ic->ic_sup_channels[i].ich_flags =
509 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
510 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
511 	}
512 
513 	/*
514 	 * IBSS channal undefined for now
515 	 */
516 	ic->ic_ibss_chan = &ic->ic_sup_channels[0];
517 	ic->ic_xmit = ipw2200_send;
518 
519 	/*
520 	 * init generic layer, then override state transition machine
521 	 */
522 	ieee80211_attach(ic);
523 
524 	/*
525 	 * different instance has different WPA door
526 	 */
527 	ieee80211_register_door(ic, ddi_driver_name(dip), instance);
528 
529 	/*
530 	 * Override 80211 default routines
531 	 */
532 	ieee80211_media_init(ic); /* initial the node table and bss */
533 	sc->sc_newstate = ic->ic_newstate;
534 	ic->ic_newstate = ipw2200_newstate;
535 	ic->ic_def_txkey = 0;
536 	sc->sc_authmode = IEEE80211_AUTH_OPEN;
537 
538 	/*
539 	 * Add the interrupt handler
540 	 */
541 	err = ddi_add_intr(dip, 0, &sc->sc_iblk, NULL,
542 	    ipw2200_intr, (caddr_t)sc);
543 	if (err != DDI_SUCCESS) {
544 		IPW2200_WARN((dip, CE_WARN,
545 		    "ipw2200_attach(): ddi_add_intr() failed\n"));
546 		goto fail5;
547 	}
548 
549 	/*
550 	 * Initialize pointer to device specific functions
551 	 */
552 	wd.wd_secalloc = WIFI_SEC_NONE;
553 	wd.wd_opmode = ic->ic_opmode;
554 	IEEE80211_ADDR_COPY(wd.wd_bssid, ic->ic_bss->in_bssid);
555 
556 	macp = mac_alloc(MAC_VERSION);
557 	if (err != 0) {
558 		IPW2200_WARN((dip, CE_WARN,
559 		    "ipw2200_attach(): mac_alloc() failed\n"));
560 		goto fail6;
561 	}
562 
563 	macp->m_type_ident	= MAC_PLUGIN_IDENT_WIFI;
564 	macp->m_driver		= sc;
565 	macp->m_dip		= dip;
566 	macp->m_src_addr	= ic->ic_macaddr;
567 	macp->m_callbacks	= &ipw2200_m_callbacks;
568 	macp->m_min_sdu		= 0;
569 	macp->m_max_sdu		= IEEE80211_MTU;
570 	macp->m_pdata		= &wd;
571 	macp->m_pdata_size	= sizeof (wd);
572 
573 	/*
574 	 * Register the macp to mac
575 	 */
576 	err = mac_register(macp, &ic->ic_mach);
577 	mac_free(macp);
578 	if (err != DDI_SUCCESS) {
579 		IPW2200_WARN((dip, CE_WARN,
580 		    "ipw2200_attach(): mac_register() failed\n"));
581 		goto fail6;
582 	}
583 
584 	/*
585 	 * Create minor node of type DDI_NT_NET_WIFI
586 	 */
587 	(void) snprintf(strbuf, sizeof (strbuf), "%s%d",
588 	    IPW2200_DRV_NAME, instance);
589 	err = ddi_create_minor_node(dip, strbuf, S_IFCHR,
590 	    instance + 1, DDI_NT_NET_WIFI, 0);
591 	if (err != DDI_SUCCESS)
592 		IPW2200_WARN((dip, CE_WARN,
593 		    "ipw2200_attach(): ddi_create_minor_node() failed\n"));
594 
595 	/*
596 	 * Cache firmware will always be true
597 	 */
598 	(void) ipw2200_cache_firmware(sc);
599 
600 	/*
601 	 * Notify link is down now
602 	 */
603 	mac_link_update(ic->ic_mach, LINK_STATE_DOWN);
604 
605 	/*
606 	 * Create the mf thread to handle the link status,
607 	 * recovery fatal error, etc.
608 	 */
609 	sc->sc_mfthread_switch = 1;
610 	if (sc->sc_mf_thread == NULL)
611 		sc->sc_mf_thread = thread_create((caddr_t)NULL, 0,
612 		    ipw2200_thread, sc, 0, &p0, TS_RUN, minclsyspri);
613 
614 	return (DDI_SUCCESS);
615 
616 fail6:
617 	ddi_remove_intr(dip, 0, sc->sc_iblk);
618 fail5:
619 	ieee80211_detach(ic);
620 
621 	mutex_destroy(&sc->sc_ilock);
622 	mutex_destroy(&sc->sc_cmd_lock);
623 	mutex_destroy(&sc->sc_tx_lock);
624 	mutex_destroy(&sc->sc_mflock);
625 	mutex_destroy(&sc->sc_resched_lock);
626 	cv_destroy(&sc->sc_fw_cond);
627 	cv_destroy(&sc->sc_cmd_status_cond);
628 	cv_destroy(&sc->sc_cmd_cond);
629 	cv_destroy(&sc->sc_mfthread_cv);
630 fail4:
631 	ipw2200_ring_free(sc);
632 fail3:
633 	ddi_regs_map_free(&sc->sc_ioh);
634 fail2:
635 	ddi_soft_state_free(ipw2200_ssp, instance);
636 fail1:
637 	return (err);
638 }
639 
640 
641 int
642 ipw2200_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
643 {
644 	struct ipw2200_softc	*sc;
645 	int			err;
646 
647 	sc = ddi_get_soft_state(ipw2200_ssp, ddi_get_instance(dip));
648 	ASSERT(sc != NULL);
649 
650 	switch (cmd) {
651 	case DDI_DETACH:
652 		break;
653 	case DDI_SUSPEND:
654 		if (sc->sc_flags & IPW2200_FLAG_RUNNING) {
655 			ipw2200_stop(sc);
656 		}
657 		sc->sc_flags |= IPW2200_FLAG_SUSPEND;
658 
659 		IPW2200_DBG(IPW2200_DBG_SUSPEND, (sc->sc_dip, CE_CONT,
660 		    "ipw2200_detach(): suspend\n"));
661 		return (DDI_SUCCESS);
662 	default:
663 		return (DDI_FAILURE);
664 	}
665 
666 	err = mac_disable(sc->sc_ic.ic_mach);
667 	if (err != DDI_SUCCESS)
668 		return (err);
669 
670 	ipw2200_stop(sc);
671 
672 	/*
673 	 * Destroy the mf_thread
674 	 */
675 	mutex_enter(&sc->sc_mflock);
676 	sc->sc_mfthread_switch = 0;
677 	while (sc->sc_mf_thread != NULL) {
678 		if (cv_wait_sig(&sc->sc_mfthread_cv, &sc->sc_mflock) == 0)
679 			break;
680 	}
681 	mutex_exit(&sc->sc_mflock);
682 
683 	/*
684 	 * Unregister from the MAC layer subsystem
685 	 */
686 	(void) mac_unregister(sc->sc_ic.ic_mach);
687 
688 	ddi_remove_intr(dip, IPW2200_PCI_INTR_NUM, sc->sc_iblk);
689 
690 	mutex_destroy(&sc->sc_ilock);
691 	mutex_destroy(&sc->sc_cmd_lock);
692 	mutex_destroy(&sc->sc_tx_lock);
693 	mutex_destroy(&sc->sc_mflock);
694 	mutex_destroy(&sc->sc_resched_lock);
695 	cv_destroy(&sc->sc_fw_cond);
696 	cv_destroy(&sc->sc_cmd_status_cond);
697 	cv_destroy(&sc->sc_cmd_cond);
698 	cv_destroy(&sc->sc_mfthread_cv);
699 
700 	/*
701 	 * Detach ieee80211
702 	 */
703 	ieee80211_detach(&sc->sc_ic);
704 
705 	(void) ipw2200_free_firmware(sc);
706 	ipw2200_ring_free(sc);
707 
708 	ddi_regs_map_free(&sc->sc_ioh);
709 	ddi_remove_minor_node(dip, NULL);
710 	ddi_soft_state_free(ipw2200_ssp, ddi_get_instance(dip));
711 
712 	return (DDI_SUCCESS);
713 }
714 
715 /* ARGSUSED */
716 int
717 ipw2200_reset(dev_info_t *dip, ddi_reset_cmd_t cmd)
718 {
719 	struct ipw2200_softc	*sc =
720 	    ddi_get_soft_state(ipw2200_ssp, ddi_get_instance(dip));
721 	ASSERT(sc != NULL);
722 
723 	ipw2200_stop(sc);
724 
725 	return (DDI_SUCCESS);
726 }
727 
728 static void
729 ipw2200_stop(struct ipw2200_softc *sc)
730 {
731 	struct ieee80211com	*ic = &sc->sc_ic;
732 
733 	IPW2200_DBG(IPW2200_DBG_HWCAP, (sc->sc_dip, CE_CONT,
734 	    "ipw2200_stop(): enter\n"));
735 
736 	ipw2200_master_stop(sc);
737 	ipw2200_csr_put32(sc, IPW2200_CSR_RST, IPW2200_RST_SW_RESET);
738 
739 	/*
740 	 * Reset ring
741 	 */
742 	ipw2200_ring_reset(sc);
743 
744 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
745 	sc->sc_flags &= ~IPW2200_FLAG_SCANNING;
746 	sc->sc_flags &= ~IPW2200_FLAG_ASSOCIATED;
747 
748 	IPW2200_DBG(IPW2200_DBG_HWCAP, (sc->sc_dip, CE_CONT,
749 	    "ipw2200_stop(): exit\n"));
750 }
751 
752 static int
753 ipw2200_config(struct ipw2200_softc *sc)
754 {
755 	struct ieee80211com		*ic = &sc->sc_ic;
756 	struct ipw2200_configuration	cfg;
757 	uint32_t			data;
758 	struct ipw2200_txpower		pwr;
759 	struct ipw2200_rateset		rs;
760 	struct ipw2200_wep_key		wkey;
761 	int				err, i;
762 
763 	/*
764 	 * Set the IBSS mode channel: Tx power
765 	 */
766 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
767 		pwr.mode  = IPW2200_MODE_11B;
768 		pwr.nchan = 11;
769 		for (i = 0; i < pwr.nchan; i++) {
770 			pwr.chan[i].chan  = i + 1;
771 			pwr.chan[i].power = IPW2200_TXPOWER_MAX;
772 		}
773 		IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
774 		    "ipw2200_config(): Setting .11b channels Tx power\n"));
775 		err = ipw2200_cmd(sc, IPW2200_CMD_SET_TX_POWER,
776 		    &pwr, sizeof (pwr), 0);
777 		if (err != DDI_SUCCESS)
778 			return (err);
779 
780 		pwr.mode  = IPW2200_MODE_11G;
781 		IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
782 		    "ipw2200_config(): Setting .11g channels Tx power\n"));
783 		err = ipw2200_cmd(sc, IPW2200_CMD_SET_TX_POWER,
784 		    &pwr, sizeof (pwr), 0);
785 		if (err != DDI_SUCCESS)
786 			return (err);
787 	}
788 
789 	/*
790 	 * Set MAC address
791 	 */
792 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
793 	    "ipw2200_config(): Setting MAC address to "
794 	    "%02x:%02x:%02x:%02x:%02x:%02x\n",
795 	    ic->ic_macaddr[0], ic->ic_macaddr[1], ic->ic_macaddr[2],
796 	    ic->ic_macaddr[3], ic->ic_macaddr[4], ic->ic_macaddr[5]));
797 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_MAC_ADDRESS, ic->ic_macaddr,
798 	    IEEE80211_ADDR_LEN, 0);
799 	if (err != DDI_SUCCESS)
800 		return (err);
801 
802 	/*
803 	 * Set basic system config settings: configuration(capabilities)
804 	 */
805 	(void) memset(&cfg, 0, sizeof (cfg));
806 	cfg.bluetooth_coexistence	 = 1;
807 	cfg.multicast_enabled		 = 1;
808 	cfg.answer_pbreq		 = 1;
809 	cfg.noise_reported		 = 1;
810 	cfg.disable_multicast_decryption = 1; /* WPA */
811 	cfg.disable_unicast_decryption   = 1; /* WPA */
812 
813 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
814 	    "ipw2200_config(): Configuring adapter\n"));
815 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_CONFIG,
816 	    &cfg, sizeof (cfg), 0);
817 	if (err != DDI_SUCCESS)
818 		return (err);
819 
820 	/*
821 	 * Set power mode
822 	 */
823 	data = LE_32(IPW2200_POWER_MODE_CAM);
824 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
825 	    "ipw2200_config(): Setting power mode to %u\n", LE_32(data)));
826 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_POWER_MODE,
827 	    &data, sizeof (data), 0);
828 	if (err != DDI_SUCCESS)
829 		return (err);
830 
831 	/*
832 	 * Set supported rates
833 	 */
834 	rs.mode = IPW2200_MODE_11G;
835 	rs.type = IPW2200_RATESET_TYPE_SUPPORTED;
836 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].ir_nrates;
837 	(void) memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].ir_rates,
838 	    rs.nrates);
839 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
840 	    "ipw2200_config(): Setting .11g supported rates(%u)\n", rs.nrates));
841 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_RATES, &rs, sizeof (rs), 0);
842 	if (err != DDI_SUCCESS)
843 		return (err);
844 
845 	rs.mode = IPW2200_MODE_11A;
846 	rs.type = IPW2200_RATESET_TYPE_SUPPORTED;
847 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].ir_nrates;
848 	(void) memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].ir_rates,
849 	    rs.nrates);
850 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
851 	    "ipw2200_config(): Setting .11a supported rates(%u)\n", rs.nrates));
852 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_RATES, &rs, sizeof (rs), 0);
853 	if (err != DDI_SUCCESS)
854 		return (err);
855 
856 	/*
857 	 * Set RTS(request-to-send) threshold
858 	 */
859 	data = LE_32(ic->ic_rtsthreshold);
860 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
861 	    "ipw2200_config(): Setting RTS threshold to %u\n", LE_32(data)));
862 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_RTS_THRESHOLD, &data,
863 	    sizeof (data), 0);
864 	if (err != DDI_SUCCESS)
865 		return (err);
866 
867 	/*
868 	 * Set fragmentation threshold
869 	 */
870 	data = LE_32(ic->ic_fragthreshold);
871 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
872 	    "ipw2200_config(): Setting fragmentation threshold to %u\n",
873 	    LE_32(data)));
874 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_FRAG_THRESHOLD, &data,
875 	    sizeof (data), 0);
876 	if (err != DDI_SUCCESS)
877 		return (err);
878 
879 	/*
880 	 * Set desired ESSID if we have
881 	 */
882 	if (ic->ic_des_esslen != 0) {
883 		IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
884 		    "ipw2200_config(): Setting desired ESSID to "
885 		    "(%u),%c%c%c%c%c%c%c%c\n",
886 		    ic->ic_des_esslen,
887 		    ic->ic_des_essid[0], ic->ic_des_essid[1],
888 		    ic->ic_des_essid[2], ic->ic_des_essid[3],
889 		    ic->ic_des_essid[4], ic->ic_des_essid[5],
890 		    ic->ic_des_essid[6], ic->ic_des_essid[7]));
891 		err = ipw2200_cmd(sc, IPW2200_CMD_SET_ESSID, ic->ic_des_essid,
892 		    ic->ic_des_esslen, 0);
893 		if (err != DDI_SUCCESS)
894 			return (err);
895 	}
896 
897 	/*
898 	 * Set WEP initial vector(random seed)
899 	 */
900 	(void) random_get_pseudo_bytes((uint8_t *)&data, sizeof (data));
901 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
902 	    "ipw2200_config(): Setting initialization vector to %u\n",
903 	    LE_32(data)));
904 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_IV, &data, sizeof (data), 0);
905 	if (err != DDI_SUCCESS)
906 		return (err);
907 
908 	/*
909 	 * Set WEP if any
910 	 */
911 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
912 		IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
913 		    "ipw2200_config(): Setting Wep Key\n", LE_32(data)));
914 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
915 			wkey.cmd = IPW2200_WEP_KEY_CMD_SETKEY;
916 			wkey.idx = (uint8_t)i;
917 			wkey.len = ic->ic_nw_keys[i].wk_keylen;
918 			(void) memset(wkey.key, 0, sizeof (wkey.key));
919 			if (ic->ic_nw_keys[i].wk_keylen)
920 				(void) memcpy(wkey.key,
921 				    ic->ic_nw_keys[i].wk_key,
922 				    ic->ic_nw_keys[i].wk_keylen);
923 			err = ipw2200_cmd(sc, IPW2200_CMD_SET_WEP_KEY,
924 			    &wkey, sizeof (wkey), 0);
925 			if (err != DDI_SUCCESS)
926 				return (err);
927 		}
928 	}
929 
930 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
931 	    "ipw2200_config(): Enabling adapter\n"));
932 
933 	return (ipw2200_cmd(sc, IPW2200_CMD_ENABLE, NULL, 0, 0));
934 }
935 
936 static int
937 ipw2200_cmd(struct ipw2200_softc *sc,
938 	uint32_t type, void *buf, size_t len, int async)
939 {
940 	struct		ipw2200_cmd_desc *cmd;
941 	clock_t		clk;
942 	uint32_t	idx;
943 
944 	mutex_enter(&sc->sc_cmd_lock);
945 	while (sc->sc_cmd_free < 1)
946 		cv_wait(&sc->sc_cmd_cond, &sc->sc_cmd_lock);
947 
948 	idx = sc->sc_cmd_cur;
949 	cmd = &sc->sc_cmdsc[idx];
950 	(void) memset(cmd, 0, sizeof (*cmd));
951 
952 	IPW2200_DBG(IPW2200_DBG_RING, (sc->sc_dip, CE_CONT,
953 	    "ipw2200_cmd(): cmd-cur=%d\n", idx));
954 
955 	cmd->hdr.type   = IPW2200_HDR_TYPE_COMMAND;
956 	cmd->hdr.flags  = IPW2200_HDR_FLAG_IRQ;
957 	cmd->type	= (uint8_t)type;
958 	if (len == 0 || buf == NULL)
959 		cmd->len  = 0;
960 	else {
961 		cmd->len  = (uint8_t)len;
962 		(void) memcpy(cmd->data, buf, len);
963 	}
964 	sc->sc_done[idx] = 0;
965 
966 	/*
967 	 * DMA sync
968 	 */
969 	(void) ddi_dma_sync(sc->sc_dma_cmdsc.dr_hnd,
970 	    idx * sizeof (struct ipw2200_cmd_desc),
971 	    sizeof (struct ipw2200_cmd_desc), DDI_DMA_SYNC_FORDEV);
972 
973 	sc->sc_cmd_cur = RING_FORWARD(sc->sc_cmd_cur, 1, IPW2200_CMD_RING_SIZE);
974 	sc->sc_cmd_free--;
975 
976 	ipw2200_csr_put32(sc, IPW2200_CSR_CMD_WRITE_INDEX, sc->sc_cmd_cur);
977 
978 	mutex_exit(&sc->sc_cmd_lock);
979 
980 	if (async)
981 		goto out;
982 
983 	/*
984 	 * Wait for command done
985 	 */
986 	mutex_enter(&sc->sc_ilock);
987 	while (sc->sc_done[idx] == 0) {
988 		/* pending */
989 		clk = ddi_get_lbolt() + drv_usectohz(5000000);  /* 5 second */
990 		if (cv_timedwait(&sc->sc_cmd_status_cond, &sc->sc_ilock, clk)
991 		    < 0)
992 			break;
993 	}
994 	mutex_exit(&sc->sc_ilock);
995 
996 	IPW2200_DBG(IPW2200_DBG_RING, (sc->sc_dip, CE_CONT,
997 	    "ipw2200_cmd(): cmd-done=%s\n", sc->sc_done[idx] ? "yes" : "no"));
998 
999 	if (sc->sc_done[idx] == 0)
1000 		return (DDI_FAILURE);
1001 
1002 out:
1003 	return (DDI_SUCCESS);
1004 }
1005 
1006 /*
1007  * If init failed, it will call stop internally. Therefore, it's unnecessary
1008  * to call ipw2200_stop() when this subroutine is failed. Otherwise, it may
1009  * be called twice.
1010  */
1011 int
1012 ipw2200_init(struct ipw2200_softc *sc)
1013 {
1014 	int	err;
1015 
1016 	/*
1017 	 * No firmware is available, failed
1018 	 */
1019 	if (!(sc->sc_flags & IPW2200_FLAG_FW_CACHED)) {
1020 		IPW2200_WARN((sc->sc_dip, CE_WARN,
1021 		    "ipw2200_init(): no firmware is available\n"));
1022 		return (DDI_FAILURE); /* return directly */
1023 	}
1024 
1025 	ipw2200_stop(sc);
1026 
1027 	err = ipw2200_chip_reset(sc);
1028 	if (err != DDI_SUCCESS) {
1029 		IPW2200_WARN((sc->sc_dip, CE_WARN,
1030 		    "ipw2200_init(): could not reset adapter\n"));
1031 		goto fail;
1032 	}
1033 
1034 	/*
1035 	 * Load boot code
1036 	 */
1037 	err = ipw2200_load_fw(sc, sc->sc_fw.boot_base, sc->sc_fw.boot_size);
1038 	if (err != DDI_SUCCESS) {
1039 		IPW2200_WARN((sc->sc_dip, CE_WARN,
1040 		    "ipw2200_init(): could not load boot code\n"));
1041 		goto fail;
1042 	}
1043 
1044 	/*
1045 	 * Load boot microcode
1046 	 */
1047 	err = ipw2200_load_uc(sc, sc->sc_fw.uc_base, sc->sc_fw.uc_size);
1048 	if (err != DDI_SUCCESS) {
1049 		IPW2200_WARN((sc->sc_dip, CE_WARN,
1050 		    "ipw2200_init(): could not load microcode\n"));
1051 		goto fail;
1052 	}
1053 
1054 	ipw2200_master_stop(sc);
1055 	ipw2200_ring_hwsetup(sc);
1056 
1057 	/*
1058 	 * Load firmware
1059 	 */
1060 	err = ipw2200_load_fw(sc, sc->sc_fw.fw_base, sc->sc_fw.fw_size);
1061 	if (err != DDI_SUCCESS) {
1062 		IPW2200_WARN((sc->sc_dip, CE_WARN,
1063 		    "ipw2200_init(): could not load firmware\n"));
1064 		goto fail;
1065 	}
1066 
1067 	sc->sc_flags |= IPW2200_FLAG_FW_INITED;
1068 
1069 	/*
1070 	 * Hardware will be enabled after configuration
1071 	 */
1072 	err = ipw2200_config(sc);
1073 	if (err != DDI_SUCCESS) {
1074 		IPW2200_WARN((sc->sc_dip, CE_WARN,
1075 		    "ipw2200_init(): device configuration failed\n"));
1076 		goto fail;
1077 	}
1078 
1079 	/*
1080 	 * workround to prevent too many h/w error.
1081 	 * delay for a while till h/w is stable.
1082 	 */
1083 	delay(drv_usectohz(delay_config_stable));
1084 
1085 	return (DDI_SUCCESS); /* return successfully */
1086 fail:
1087 	ipw2200_stop(sc);
1088 	return (err);
1089 }
1090 
1091 /*
1092  * get hardware configurations from EEPROM embedded within PRO/2200
1093  */
1094 static void
1095 ipw2200_hwconf_get(struct ipw2200_softc *sc)
1096 {
1097 	int		i;
1098 	uint16_t	val;
1099 
1100 	/*
1101 	 * Get mac address
1102 	 */
1103 	i = 0;
1104 	val = ipw2200_rom_get16(sc, IPW2200_EEPROM_MAC + 0);
1105 	sc->sc_macaddr[i++] = val >> 8;
1106 	sc->sc_macaddr[i++] = val & 0xff;
1107 	val = ipw2200_rom_get16(sc, IPW2200_EEPROM_MAC + 1);
1108 	sc->sc_macaddr[i++] = val >> 8;
1109 	sc->sc_macaddr[i++] = val & 0xff;
1110 	val = ipw2200_rom_get16(sc, IPW2200_EEPROM_MAC + 2);
1111 	sc->sc_macaddr[i++] = val >> 8;
1112 	sc->sc_macaddr[i++] = val & 0xff;
1113 
1114 	/*
1115 	 * formatted MAC address string
1116 	 */
1117 	(void) snprintf(sc->sc_macstr, sizeof (sc->sc_macstr),
1118 	    "%02x:%02x:%02x:%02x:%02x:%02x",
1119 	    sc->sc_macaddr[0], sc->sc_macaddr[1],
1120 	    sc->sc_macaddr[2], sc->sc_macaddr[3],
1121 	    sc->sc_macaddr[4], sc->sc_macaddr[5]);
1122 
1123 }
1124 
1125 /*
1126  * all ipw2200 interrupts will be masked by this routine
1127  */
1128 static void
1129 ipw2200_master_stop(struct ipw2200_softc *sc)
1130 {
1131 	int	ntries;
1132 
1133 	/*
1134 	 * disable interrupts
1135 	 */
1136 	ipw2200_csr_put32(sc, IPW2200_CSR_INTR_MASK, 0);
1137 	ipw2200_csr_put32(sc, IPW2200_CSR_RST, IPW2200_RST_STOP_MASTER);
1138 
1139 	/*
1140 	 * wait long enough to ensure hardware stop successfully.
1141 	 */
1142 	for (ntries = 0; ntries < 500; ntries++) {
1143 		if (ipw2200_csr_get32(sc, IPW2200_CSR_RST) &
1144 		    IPW2200_RST_MASTER_DISABLED)
1145 			break;
1146 		/* wait for a while */
1147 		drv_usecwait(100);
1148 	}
1149 	if (ntries == 500)
1150 		IPW2200_WARN((sc->sc_dip, CE_WARN,
1151 		    "ipw2200_master_stop(): timeout\n"));
1152 
1153 	ipw2200_csr_put32(sc, IPW2200_CSR_RST,
1154 	    IPW2200_RST_PRINCETON_RESET |
1155 	    ipw2200_csr_get32(sc, IPW2200_CSR_RST));
1156 
1157 	sc->sc_flags &= ~IPW2200_FLAG_FW_INITED;
1158 }
1159 
1160 /*
1161  * all ipw2200 interrupts will be masked by this routine
1162  */
1163 static int
1164 ipw2200_chip_reset(struct ipw2200_softc *sc)
1165 {
1166 	uint32_t	tmp;
1167 	int		ntries, i;
1168 
1169 	ipw2200_master_stop(sc);
1170 
1171 	/*
1172 	 * Move adapter to DO state
1173 	 */
1174 	tmp = ipw2200_csr_get32(sc, IPW2200_CSR_CTL);
1175 	ipw2200_csr_put32(sc, IPW2200_CSR_CTL, tmp | IPW2200_CTL_INIT);
1176 
1177 	/*
1178 	 * Initialize Phase-Locked Level (PLL)
1179 	 */
1180 	ipw2200_csr_put32(sc, IPW2200_CSR_READ_INT, IPW2200_READ_INT_INIT_HOST);
1181 
1182 	/*
1183 	 * Wait for clock stabilization
1184 	 */
1185 	for (ntries = 0; ntries < 1000; ntries++) {
1186 		if (ipw2200_csr_get32(sc, IPW2200_CSR_CTL) &
1187 		    IPW2200_CTL_CLOCK_READY)
1188 			break;
1189 		drv_usecwait(200);
1190 	}
1191 	if (ntries == 1000) {
1192 		IPW2200_WARN((sc->sc_dip, CE_WARN,
1193 		    "ipw2200_chip_reset(): timeout\n"));
1194 		return (DDI_FAILURE);
1195 	}
1196 
1197 	tmp = ipw2200_csr_get32(sc, IPW2200_CSR_RST);
1198 	ipw2200_csr_put32(sc, IPW2200_CSR_RST, tmp | IPW2200_RST_SW_RESET);
1199 
1200 	drv_usecwait(10);
1201 
1202 	tmp = ipw2200_csr_get32(sc, IPW2200_CSR_CTL);
1203 	ipw2200_csr_put32(sc, IPW2200_CSR_CTL, tmp | IPW2200_CTL_INIT);
1204 
1205 	/*
1206 	 * clear NIC memory
1207 	 */
1208 	ipw2200_csr_put32(sc, IPW2200_CSR_AUTOINC_ADDR, 0);
1209 	for (i = 0; i < 0xc000; i++)
1210 		ipw2200_csr_put32(sc, IPW2200_CSR_AUTOINC_DATA, 0);
1211 
1212 	return (DDI_SUCCESS);
1213 }
1214 
1215 /*
1216  * This function is used by wificonfig/dladm to get the current
1217  * radio status, it is off/on
1218  */
1219 int
1220 ipw2200_radio_status(struct ipw2200_softc *sc)
1221 {
1222 	int	val;
1223 
1224 	val = (ipw2200_csr_get32(sc, IPW2200_CSR_IO) &
1225 	    IPW2200_IO_RADIO_ENABLED) ? 1 : 0;
1226 
1227 	return (val);
1228 }
1229 /*
1230  * This function is used to get the statistic
1231  */
1232 void
1233 ipw2200_get_statistics(struct ipw2200_softc *sc)
1234 {
1235 	struct ieee80211com	*ic = &sc->sc_ic;
1236 
1237 	uint32_t size, buf[128];
1238 
1239 	if (!(sc->sc_flags & IPW2200_FLAG_FW_INITED)) {
1240 		IPW2200_DBG(IPW2200_DBG_IOCTL, (sc->sc_dip, CE_CONT,
1241 		    "ipw2200_get_statistic(): fw doesn't download yet."));
1242 		return;
1243 	}
1244 
1245 	size = min(ipw2200_csr_get32(sc, IPW2200_CSR_TABLE0_SIZE), 128 - 1);
1246 	ipw2200_csr_getbuf32(sc, IPW2200_CSR_TABLE0_BASE, &buf[1], size);
1247 
1248 	/*
1249 	 * To retrieve the statistic information into proper places. There are
1250 	 * lot of information. These table will be read once a second.
1251 	 * Hopefully, it will not effect the performance.
1252 	 */
1253 
1254 	/*
1255 	 * For the tx/crc information, we can get them from chip directly;
1256 	 * For the rx/wep error/(rts) related information, leave them net80211.
1257 	 */
1258 	/* WIFI_STAT_TX_FRAGS */
1259 	ic->ic_stats.is_tx_frags = (uint32_t)buf[5];
1260 	/* WIFI_STAT_MCAST_TX */
1261 	ic->ic_stats.is_tx_mcast = (uint32_t)buf[31];
1262 	/* WIFI_STAT_TX_RETRANS */
1263 	ic->ic_stats.is_tx_retries = (uint32_t)buf[56];
1264 	/* WIFI_STAT_TX_FAILED */
1265 	ic->ic_stats.is_tx_failed = (uint32_t)buf[57];
1266 	/* MAC_STAT_OBYTES */
1267 	ic->ic_stats.is_tx_bytes = (uint32_t)buf[64];
1268 }
1269 
1270 /*
1271  * DMA region alloc subroutine
1272  */
1273 int
1274 ipw2200_dma_region_alloc(struct ipw2200_softc *sc, struct dma_region *dr,
1275 	size_t size, uint_t dir, uint_t flags)
1276 {
1277 	dev_info_t	*dip = sc->sc_dip;
1278 	int		err;
1279 
1280 	IPW2200_DBG(IPW2200_DBG_DMA, (sc->sc_dip, CE_CONT,
1281 	    "ipw2200_dma_region_alloc(): size =%u\n", size));
1282 
1283 	err = ddi_dma_alloc_handle(dip, &ipw2200_dma_attr, DDI_DMA_SLEEP, NULL,
1284 	    &dr->dr_hnd);
1285 	if (err != DDI_SUCCESS) {
1286 		IPW2200_DBG(IPW2200_DBG_DMA, (sc->sc_dip, CE_CONT,
1287 		    "ipw2200_dma_region_alloc(): "
1288 		    "ddi_dma_alloc_handle() failed\n"));
1289 		goto fail0;
1290 	}
1291 
1292 	err = ddi_dma_mem_alloc(dr->dr_hnd, size, &ipw2200_dma_accattr,
1293 	    flags, DDI_DMA_SLEEP, NULL,
1294 	    &dr->dr_base, &dr->dr_size, &dr->dr_acc);
1295 	if (err != DDI_SUCCESS) {
1296 		IPW2200_DBG(IPW2200_DBG_DMA, (sc->sc_dip, CE_CONT,
1297 		    "ipw2200_dma_region_alloc(): "
1298 		    "ddi_dma_mem_alloc() failed\n"));
1299 		goto fail1;
1300 	}
1301 
1302 	err = ddi_dma_addr_bind_handle(dr->dr_hnd, NULL,
1303 	    dr->dr_base, dr->dr_size,
1304 	    dir | flags, DDI_DMA_SLEEP, NULL,
1305 	    &dr->dr_cookie, &dr->dr_ccnt);
1306 	if (err != DDI_DMA_MAPPED) {
1307 		IPW2200_DBG(IPW2200_DBG_DMA, (sc->sc_dip, CE_CONT,
1308 		    "ipw2200_dma_region_alloc(): "
1309 		    "ddi_dma_addr_bind_handle() failed\n"));
1310 		goto fail2;
1311 	}
1312 
1313 	IPW2200_DBG(IPW2200_DBG_DMA, (sc->sc_dip, CE_CONT,
1314 	    "ipw2200_dma_region_alloc(): ccnt=%u\n", dr->dr_ccnt));
1315 
1316 	if (dr->dr_ccnt != 1) {
1317 		err = DDI_FAILURE;
1318 		goto fail3;
1319 	}
1320 
1321 	dr->dr_pbase = dr->dr_cookie.dmac_address;
1322 
1323 	IPW2200_DBG(IPW2200_DBG_DMA, (sc->sc_dip, CE_CONT,
1324 	    "ipw2200_dma_region_alloc(): get physical-base=0x%08x\n",
1325 	    dr->dr_pbase));
1326 
1327 	return (DDI_SUCCESS);
1328 
1329 fail3:
1330 	(void) ddi_dma_unbind_handle(dr->dr_hnd);
1331 fail2:
1332 	ddi_dma_mem_free(&dr->dr_acc);
1333 fail1:
1334 	ddi_dma_free_handle(&dr->dr_hnd);
1335 fail0:
1336 	return (err);
1337 }
1338 
1339 void
1340 ipw2200_dma_region_free(struct dma_region *dr)
1341 {
1342 	(void) ddi_dma_unbind_handle(dr->dr_hnd);
1343 	ddi_dma_mem_free(&dr->dr_acc);
1344 	ddi_dma_free_handle(&dr->dr_hnd);
1345 }
1346 
1347 static int
1348 ipw2200_ring_alloc(struct ipw2200_softc *sc)
1349 {
1350 	int	err, i;
1351 
1352 	/*
1353 	 * tx desc ring
1354 	 */
1355 	sc->sc_dma_txdsc.dr_name = "ipw2200-tx-desc-ring";
1356 	err = ipw2200_dma_region_alloc(sc, &sc->sc_dma_txdsc,
1357 	    IPW2200_TX_RING_SIZE * sizeof (struct ipw2200_tx_desc),
1358 	    DDI_DMA_WRITE, DDI_DMA_CONSISTENT);
1359 	if (err != DDI_SUCCESS)
1360 		goto fail0;
1361 	/*
1362 	 * tx buffer array
1363 	 */
1364 	for (i = 0; i < IPW2200_TX_RING_SIZE; i++) {
1365 		sc->sc_dma_txbufs[i].dr_name = "ipw2200-tx-buf";
1366 		err = ipw2200_dma_region_alloc(sc, &sc->sc_dma_txbufs[i],
1367 		    IPW2200_TXBUF_SIZE, DDI_DMA_WRITE, DDI_DMA_STREAMING);
1368 		if (err != DDI_SUCCESS) {
1369 			while (i >= 0) {
1370 				ipw2200_dma_region_free(&sc->sc_dma_txbufs[i]);
1371 				i--;
1372 			}
1373 			goto fail1;
1374 		}
1375 	}
1376 	/*
1377 	 * rx buffer array
1378 	 */
1379 	for (i = 0; i < IPW2200_RX_RING_SIZE; i++) {
1380 		sc->sc_dma_rxbufs[i].dr_name = "ipw2200-rx-buf";
1381 		err = ipw2200_dma_region_alloc(sc, &sc->sc_dma_rxbufs[i],
1382 		    IPW2200_RXBUF_SIZE, DDI_DMA_READ, DDI_DMA_STREAMING);
1383 		if (err != DDI_SUCCESS) {
1384 			while (i >= 0) {
1385 				ipw2200_dma_region_free(&sc->sc_dma_rxbufs[i]);
1386 				i--;
1387 			}
1388 			goto fail2;
1389 		}
1390 	}
1391 	/*
1392 	 * cmd desc ring
1393 	 */
1394 	sc->sc_dma_cmdsc.dr_name = "ipw2200-cmd-desc-ring";
1395 	err = ipw2200_dma_region_alloc(sc, &sc->sc_dma_cmdsc,
1396 	    IPW2200_CMD_RING_SIZE * sizeof (struct ipw2200_cmd_desc),
1397 	    DDI_DMA_WRITE, DDI_DMA_CONSISTENT);
1398 	if (err != DDI_SUCCESS)
1399 		goto fail3;
1400 
1401 	return (DDI_SUCCESS);
1402 
1403 fail3:
1404 	for (i = 0; i < IPW2200_RX_RING_SIZE; i++)
1405 		ipw2200_dma_region_free(&sc->sc_dma_rxbufs[i]);
1406 fail2:
1407 	for (i = 0; i < IPW2200_TX_RING_SIZE; i++)
1408 		ipw2200_dma_region_free(&sc->sc_dma_txbufs[i]);
1409 fail1:
1410 	ipw2200_dma_region_free(&sc->sc_dma_txdsc);
1411 fail0:
1412 	return (err);
1413 }
1414 
1415 static void
1416 ipw2200_ring_free(struct ipw2200_softc *sc)
1417 {
1418 	int	i;
1419 
1420 	/*
1421 	 * tx ring desc
1422 	 */
1423 	ipw2200_dma_region_free(&sc->sc_dma_txdsc);
1424 	/*
1425 	 * tx buf
1426 	 */
1427 	for (i = 0; i < IPW2200_TX_RING_SIZE; i++)
1428 		ipw2200_dma_region_free(&sc->sc_dma_txbufs[i]);
1429 	/*
1430 	 * rx buf
1431 	 */
1432 	for (i = 0; i < IPW2200_RX_RING_SIZE; i++)
1433 		ipw2200_dma_region_free(&sc->sc_dma_rxbufs[i]);
1434 	/*
1435 	 * command ring desc
1436 	 */
1437 	ipw2200_dma_region_free(&sc->sc_dma_cmdsc);
1438 }
1439 
1440 static void
1441 ipw2200_ring_reset(struct ipw2200_softc *sc)
1442 {
1443 	int i;
1444 
1445 	/*
1446 	 * tx desc ring & buffer array
1447 	 */
1448 	sc->sc_tx_cur   = 0;
1449 	sc->sc_tx_free  = IPW2200_TX_RING_SIZE;
1450 	sc->sc_txdsc    = (struct ipw2200_tx_desc *)sc->sc_dma_txdsc.dr_base;
1451 	for (i = 0; i < IPW2200_TX_RING_SIZE; i++)
1452 		sc->sc_txbufs[i] = (uint8_t *)sc->sc_dma_txbufs[i].dr_base;
1453 	/*
1454 	 * rx buffer array
1455 	 */
1456 	sc->sc_rx_cur   = 0;
1457 	sc->sc_rx_free  = IPW2200_RX_RING_SIZE;
1458 	for (i = 0; i < IPW2200_RX_RING_SIZE; i++)
1459 		sc->sc_rxbufs[i] = (uint8_t *)sc->sc_dma_rxbufs[i].dr_base;
1460 
1461 	/*
1462 	 * command desc ring
1463 	 */
1464 	sc->sc_cmd_cur  = 0;
1465 	sc->sc_cmd_free = IPW2200_CMD_RING_SIZE;
1466 	sc->sc_cmdsc    = (struct ipw2200_cmd_desc *)sc->sc_dma_cmdsc.dr_base;
1467 }
1468 
1469 /*
1470  * tx, rx rings and command initialization
1471  */
1472 static int
1473 ipw2200_ring_init(struct ipw2200_softc *sc)
1474 {
1475 	int	err;
1476 
1477 	err = ipw2200_ring_alloc(sc);
1478 	if (err != DDI_SUCCESS)
1479 		return (err);
1480 
1481 	ipw2200_ring_reset(sc);
1482 
1483 	return (DDI_SUCCESS);
1484 }
1485 
1486 static void
1487 ipw2200_ring_hwsetup(struct ipw2200_softc *sc)
1488 {
1489 	int	i;
1490 
1491 	/*
1492 	 * command desc ring
1493 	 */
1494 	ipw2200_csr_put32(sc, IPW2200_CSR_CMD_BASE, sc->sc_dma_cmdsc.dr_pbase);
1495 	ipw2200_csr_put32(sc, IPW2200_CSR_CMD_SIZE, IPW2200_CMD_RING_SIZE);
1496 	ipw2200_csr_put32(sc, IPW2200_CSR_CMD_WRITE_INDEX, sc->sc_cmd_cur);
1497 
1498 	/*
1499 	 * tx desc ring.  only tx1 is used, tx2, tx3, and tx4 are unused
1500 	 */
1501 	ipw2200_csr_put32(sc, IPW2200_CSR_TX1_BASE, sc->sc_dma_txdsc.dr_pbase);
1502 	ipw2200_csr_put32(sc, IPW2200_CSR_TX1_SIZE, IPW2200_TX_RING_SIZE);
1503 	ipw2200_csr_put32(sc, IPW2200_CSR_TX1_WRITE_INDEX, sc->sc_tx_cur);
1504 
1505 	/*
1506 	 * tx2, tx3, tx4 is not used
1507 	 */
1508 	ipw2200_csr_put32(sc, IPW2200_CSR_TX2_BASE, sc->sc_dma_txdsc.dr_pbase);
1509 	ipw2200_csr_put32(sc, IPW2200_CSR_TX2_SIZE, IPW2200_TX_RING_SIZE);
1510 	ipw2200_csr_put32(sc, IPW2200_CSR_TX2_READ_INDEX, 0);
1511 	ipw2200_csr_put32(sc, IPW2200_CSR_TX2_WRITE_INDEX, 0);
1512 	ipw2200_csr_put32(sc, IPW2200_CSR_TX3_BASE, sc->sc_dma_txdsc.dr_pbase);
1513 	ipw2200_csr_put32(sc, IPW2200_CSR_TX3_SIZE, IPW2200_TX_RING_SIZE);
1514 	ipw2200_csr_put32(sc, IPW2200_CSR_TX3_READ_INDEX, 0);
1515 	ipw2200_csr_put32(sc, IPW2200_CSR_TX3_WRITE_INDEX, 0);
1516 	ipw2200_csr_put32(sc, IPW2200_CSR_TX4_BASE, sc->sc_dma_txdsc.dr_pbase);
1517 	ipw2200_csr_put32(sc, IPW2200_CSR_TX4_SIZE, IPW2200_TX_RING_SIZE);
1518 	ipw2200_csr_put32(sc, IPW2200_CSR_TX4_READ_INDEX, 0);
1519 	ipw2200_csr_put32(sc, IPW2200_CSR_TX4_WRITE_INDEX, 0);
1520 
1521 	/*
1522 	 * rx buffer ring
1523 	 */
1524 	for (i = 0; i < IPW2200_RX_RING_SIZE; i++)
1525 		ipw2200_csr_put32(sc, IPW2200_CSR_RX_BASE + i * 4,
1526 		    sc->sc_dma_rxbufs[i].dr_pbase);
1527 	/*
1528 	 * all rx buffer are empty, rx-rd-index == 0 && rx-wr-index == N-1
1529 	 */
1530 	ipw2200_csr_put32(sc, IPW2200_CSR_RX_WRITE_INDEX,
1531 	    RING_BACKWARD(sc->sc_rx_cur, 1, IPW2200_RX_RING_SIZE));
1532 }
1533 
1534 int
1535 ipw2200_start_scan(struct ipw2200_softc *sc)
1536 {
1537 	struct ieee80211com	*ic = &sc->sc_ic;
1538 	struct ipw2200_scan	scan;
1539 	uint8_t			*ch;
1540 	int			cnt, i;
1541 
1542 	IPW2200_DBG(IPW2200_DBG_SCAN, (sc->sc_dip, CE_CONT,
1543 	    "ipw2200_start_scan(): start scanning \n"));
1544 
1545 	/*
1546 	 * start scanning
1547 	 */
1548 	sc->sc_flags |= IPW2200_FLAG_SCANNING;
1549 
1550 	(void) memset(&scan, 0, sizeof (scan));
1551 	scan.type = (ic->ic_des_esslen != 0) ? IPW2200_SCAN_TYPE_BDIRECTED :
1552 	    IPW2200_SCAN_TYPE_BROADCAST;
1553 	scan.dwelltime = LE_16(40); /* The interval is set up to 40 */
1554 
1555 	/*
1556 	 * Compact supported channel number(5G) into a single buffer
1557 	 */
1558 	ch = scan.channels;
1559 	cnt = 0;
1560 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1561 		if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_sup_channels[i]) &&
1562 		    isset(ic->ic_chan_active, i)) {
1563 			*++ch = (uint8_t)i;
1564 			cnt++;
1565 		}
1566 	}
1567 	*(ch - cnt) = IPW2200_CHAN_5GHZ | (uint8_t)cnt;
1568 	ch = (cnt > 0) ? (ch + 1) : (scan.channels);
1569 
1570 	/*
1571 	 * Compact supported channel number(2G) into a single buffer
1572 	 */
1573 	cnt = 0;
1574 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1575 		if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_sup_channels[i]) &&
1576 		    isset(ic->ic_chan_active, i)) {
1577 			*++ch = (uint8_t)i;
1578 			cnt++;
1579 		}
1580 	}
1581 	*(ch - cnt) = IPW2200_CHAN_2GHZ | cnt;
1582 
1583 	return (ipw2200_cmd(sc, IPW2200_CMD_SCAN, &scan, sizeof (scan), 1));
1584 }
1585 
1586 int
1587 ipw2200_auth_and_assoc(struct ipw2200_softc *sc)
1588 {
1589 	struct ieee80211com		*ic = &sc->sc_ic;
1590 	struct ieee80211_node		*in = ic->ic_bss;
1591 	struct ipw2200_configuration	cfg;
1592 	struct ipw2200_rateset		rs;
1593 	struct ipw2200_associate	assoc;
1594 	uint32_t			data;
1595 	int				err;
1596 	uint8_t				*wpa_level;
1597 
1598 	if (sc->sc_flags & IPW2200_FLAG_ASSOCIATED) {
1599 		/* already associated */
1600 		return (-1);
1601 	}
1602 
1603 	/*
1604 	 * set the confiuration
1605 	 */
1606 	if (IEEE80211_IS_CHAN_2GHZ(in->in_chan)) {
1607 		/* enable b/g auto-detection */
1608 		(void) memset(&cfg, 0, sizeof (cfg));
1609 		cfg.bluetooth_coexistence = 1;
1610 		cfg.multicast_enabled	  = 1;
1611 		cfg.use_protection	  = 1;
1612 		cfg.answer_pbreq	  = 1;
1613 		cfg.noise_reported	  = 1;
1614 		cfg.disable_multicast_decryption = 1; /* WPA */
1615 		cfg.disable_unicast_decryption   = 1; /* WPA */
1616 		err = ipw2200_cmd(sc, IPW2200_CMD_SET_CONFIG,
1617 		    &cfg, sizeof (cfg), 1);
1618 		if (err != DDI_SUCCESS)
1619 			return (err);
1620 	}
1621 
1622 	/*
1623 	 * set the essid, may be null/hidden AP
1624 	 */
1625 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
1626 	    "ipw2200_auth_and_assoc(): "
1627 	    "setting ESSID to(%u),%c%c%c%c%c%c%c%c\n",
1628 	    in->in_esslen,
1629 	    in->in_essid[0], in->in_essid[1],
1630 	    in->in_essid[2], in->in_essid[3],
1631 	    in->in_essid[4], in->in_essid[5],
1632 	    in->in_essid[6], in->in_essid[7]));
1633 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_ESSID, in->in_essid,
1634 	    in->in_esslen, 1);
1635 	if (err != DDI_SUCCESS)
1636 		return (err);
1637 
1638 	/*
1639 	 * set the rate: the rate set has already been ''negocitated''
1640 	 */
1641 	rs.mode = IEEE80211_IS_CHAN_5GHZ(in->in_chan) ?
1642 	    IPW2200_MODE_11A : IPW2200_MODE_11G;
1643 	rs.type = IPW2200_RATESET_TYPE_NEGOCIATED;
1644 	rs.nrates = in->in_rates.ir_nrates;
1645 	(void) memcpy(rs.rates, in->in_rates.ir_rates, in->in_rates.ir_nrates);
1646 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
1647 	    "ipw2200_auth_and_assoc(): "
1648 	    "setting negotiated rates to(nrates = %u)\n", rs.nrates));
1649 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_RATES, &rs, sizeof (rs), 1);
1650 	if (err != DDI_SUCCESS)
1651 		return (err);
1652 
1653 	/*
1654 	 * invoke command associate
1655 	 */
1656 	(void) memset(&assoc, 0, sizeof (assoc));
1657 
1658 	/*
1659 	 * set opt_ie to h/w if associated is WPA, opt_ie has been verified
1660 	 * by net80211 kernel module.
1661 	 */
1662 	if (ic->ic_opt_ie != NULL) {
1663 
1664 		wpa_level = (uint8_t *)ic->ic_opt_ie;
1665 
1666 		IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
1667 		    "ipw2200_auth_and_assoc(): "
1668 		    "set wpa_ie and wpa_ie_len to h/w. "
1669 		    "length is %d\n"
1670 		    "opt_ie[0] = %02X - element vendor\n"
1671 		    "opt_ie[1] = %02X - length\n"
1672 		    "opt_ie[2,3,4] = %02X %02X %02X - oui\n"
1673 		    "opt_ie[5] = %02X - oui type\n"
1674 		    "opt_ie[6,7] = %02X %02X - spec version \n"
1675 		    "opt_ie[8,9,10,11] = %02X %02X %02X %02X - gk cipher\n"
1676 		    "opt_ie[12,13] = %02X %02X - pairwise key cipher(1)\n"
1677 		    "opt_ie[14,15,16,17] = %02X %02X %02X %02X - ciphers\n"
1678 		    "opt_ie[18,19] = %02X %02X - authselcont(1) \n"
1679 		    "opt_ie[20,21,22,23] = %02X %02X %02X %02X - authsels\n",
1680 		    wpa_level[1], wpa_level[0], wpa_level[1],
1681 		    wpa_level[2], wpa_level[3], wpa_level[4],
1682 		    wpa_level[5], wpa_level[6], wpa_level[7],
1683 		    wpa_level[8], wpa_level[9], wpa_level[10],
1684 		    wpa_level[11], wpa_level[12], wpa_level[13],
1685 		    wpa_level[14], wpa_level[15], wpa_level[16],
1686 		    wpa_level[17], wpa_level[18], wpa_level[19],
1687 		    wpa_level[20], wpa_level[21], wpa_level[22],
1688 		    wpa_level[23]));
1689 
1690 		err = ipw2200_cmd(sc, IPW2200_CMD_SET_OPTIE,
1691 		    ic->ic_opt_ie, ic->ic_opt_ie_len, 1);
1692 		if (err != DDI_SUCCESS)
1693 			return (err);
1694 	}
1695 
1696 	/*
1697 	 * set the sensitive
1698 	 */
1699 	data = LE_32(in->in_rssi);
1700 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
1701 	    "ipw2200_auth_and_assoc(): "
1702 	    "setting sensitivity to rssi:(%u)\n", (uint8_t)in->in_rssi));
1703 	err = ipw2200_cmd(sc, IPW2200_CMD_SET_SENSITIVITY,
1704 	    &data, sizeof (data), 1);
1705 	if (err != DDI_SUCCESS)
1706 		return (err);
1707 
1708 	/*
1709 	 * set mode and channel for assocation command
1710 	 */
1711 	assoc.mode = IEEE80211_IS_CHAN_5GHZ(in->in_chan) ?
1712 	    IPW2200_MODE_11A : IPW2200_MODE_11G;
1713 	assoc.chan = ieee80211_chan2ieee(ic, in->in_chan);
1714 
1715 	/*
1716 	 * use the value set to ic_bss to retraive current sharedmode
1717 	 */
1718 	if (ic->ic_bss->in_authmode == WL_SHAREDKEY) {
1719 		assoc.auth = (ic->ic_def_txkey << 4) | IPW2200_AUTH_SHARED;
1720 		IPW2200_DBG(IPW2200_DBG_IOCTL, (sc->sc_dip, CE_CONT,
1721 		    "ipw2200_auth_and_assoc(): "
1722 		    "associate to shared key mode, set thru. ioctl"));
1723 	}
1724 
1725 	if (ic->ic_flags & IEEE80211_F_WPA)
1726 		assoc.policy = LE_16(IPW2200_POLICY_WPA); /* RSN/WPA active */
1727 	(void) memcpy(assoc.tstamp, in->in_tstamp.data, 8);
1728 	assoc.capinfo = LE_16(in->in_capinfo);
1729 	assoc.lintval = LE_16(ic->ic_lintval);
1730 	assoc.intval  = LE_16(in->in_intval);
1731 	IEEE80211_ADDR_COPY(assoc.bssid, in->in_bssid);
1732 	if (ic->ic_opmode == IEEE80211_M_IBSS)
1733 		IEEE80211_ADDR_COPY(assoc.dst, ipw2200_broadcast_addr);
1734 	else
1735 		IEEE80211_ADDR_COPY(assoc.dst, in->in_bssid);
1736 
1737 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
1738 	    "ipw2200_auth_and_assoc(): "
1739 	    "associate to bssid(%2x:%2x:%2x:%2x:%2x:%2x:), "
1740 	    "chan(%u), auth(%u)\n",
1741 	    assoc.bssid[0], assoc.bssid[1], assoc.bssid[2],
1742 	    assoc.bssid[3], assoc.bssid[4], assoc.bssid[5],
1743 	    assoc.chan, assoc.auth));
1744 	return (ipw2200_cmd(sc, IPW2200_CMD_ASSOCIATE,
1745 	    &assoc, sizeof (assoc), 1));
1746 }
1747 
1748 /*
1749  * Send the dis-association command to h/w, will receive notification to claim
1750  * the connection is dis-associated. So, it's not marked as disassociated this
1751  * moment.
1752  */
1753 static int
1754 ipw2200_disassoc(struct ipw2200_softc *sc)
1755 {
1756 	struct ipw2200_associate assoc;
1757 	assoc.type = 2;
1758 	return (ipw2200_cmd(sc, IPW2200_CMD_ASSOCIATE, &assoc,
1759 	    sizeof (assoc), 1));
1760 }
1761 
1762 /* ARGSUSED */
1763 static int
1764 ipw2200_newstate(struct ieee80211com *ic, enum ieee80211_state state, int arg)
1765 {
1766 	struct ipw2200_softc	*sc = (struct ipw2200_softc *)ic;
1767 	wifi_data_t		wd = { 0 };
1768 
1769 	switch (state) {
1770 	case IEEE80211_S_SCAN:
1771 		if (!(sc->sc_flags & IPW2200_FLAG_SCANNING)) {
1772 			ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
1773 			(void) ipw2200_start_scan(sc);
1774 		}
1775 		break;
1776 	case IEEE80211_S_AUTH:
1777 		/*
1778 		 * The firmware will fail if we are already associated
1779 		 */
1780 		if (sc->sc_flags & IPW2200_FLAG_ASSOCIATED)
1781 			(void) ipw2200_disassoc(sc);
1782 		(void) ipw2200_auth_and_assoc(sc);
1783 		break;
1784 	case IEEE80211_S_RUN:
1785 		/*
1786 		 * We can send data now; update the fastpath with our
1787 		 * current associated BSSID and other relevant settings.
1788 		 */
1789 		wd.wd_secalloc = ieee80211_crypto_getciphertype(ic);
1790 		wd.wd_opmode = ic->ic_opmode;
1791 		IEEE80211_ADDR_COPY(wd.wd_bssid, ic->ic_bss->in_bssid);
1792 		(void) mac_pdata_update(ic->ic_mach, &wd, sizeof (wd));
1793 		break;
1794 	case IEEE80211_S_ASSOC:
1795 	case IEEE80211_S_INIT:
1796 		break;
1797 	}
1798 
1799 	/*
1800 	 * notify to update the link, and WPA
1801 	 */
1802 	if ((ic->ic_state != IEEE80211_S_RUN) && (state == IEEE80211_S_RUN)) {
1803 		ieee80211_notify_node_join(ic, ic->ic_bss);
1804 	} else if ((ic->ic_state == IEEE80211_S_RUN) &&
1805 	    (state != IEEE80211_S_RUN)) {
1806 		ieee80211_notify_node_leave(ic, ic->ic_bss);
1807 	}
1808 
1809 	IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
1810 	    "ipw2200_newstat(): %s -> %s\n",
1811 	    ieee80211_state_name[ic->ic_state],
1812 	    ieee80211_state_name[state]));
1813 
1814 	ic->ic_state = state;
1815 	return (DDI_SUCCESS);
1816 }
1817 /*
1818  * GLD operations
1819  */
1820 /* ARGSUSED */
1821 static int
1822 ipw2200_m_stat(void *arg, uint_t stat, uint64_t *val)
1823 {
1824 	ieee80211com_t		*ic = (ieee80211com_t *)arg;
1825 	struct ipw2200_softc	*sc = (struct ipw2200_softc *)ic;
1826 
1827 	IPW2200_DBG(IPW2200_DBG_GLD, (((struct ipw2200_softc *)arg)->sc_dip,
1828 	    CE_CONT,
1829 	    "ipw2200_m_stat(): enter\n"));
1830 	/*
1831 	 * Some of below statistic data are from hardware, some from net80211
1832 	 */
1833 	switch (stat) {
1834 	case MAC_STAT_NOXMTBUF:
1835 		*val = ic->ic_stats.is_tx_nobuf;
1836 		break;
1837 	case MAC_STAT_IERRORS:
1838 		*val = sc->sc_stats.sc_rx_len_err;
1839 		break;
1840 	case MAC_STAT_OERRORS:
1841 		*val = sc->sc_stats.sc_tx_discard +
1842 		    sc->sc_stats.sc_tx_alloc_fail +
1843 		    sc->sc_stats.sc_tx_encap_fail +
1844 		    sc->sc_stats.sc_tx_crypto_fail;
1845 		break;
1846 	case MAC_STAT_RBYTES:
1847 		*val = ic->ic_stats.is_rx_bytes;
1848 		break;
1849 	case MAC_STAT_IPACKETS:
1850 		*val = ic->ic_stats.is_rx_frags;
1851 		break;
1852 	case MAC_STAT_OBYTES:
1853 		*val = ic->ic_stats.is_tx_bytes;
1854 		break;
1855 	case MAC_STAT_OPACKETS:
1856 		*val = ic->ic_stats.is_tx_frags;
1857 		break;
1858 	/*
1859 	 * Get below from hardware statistic, retraive net80211 value once 1s
1860 	 */
1861 	case WIFI_STAT_TX_FRAGS:
1862 	case WIFI_STAT_MCAST_TX:
1863 	case WIFI_STAT_TX_FAILED:
1864 	case WIFI_STAT_TX_RETRANS:
1865 	/*
1866 	 * Get blow information from net80211
1867 	 */
1868 	case WIFI_STAT_RTS_SUCCESS:
1869 	case WIFI_STAT_RTS_FAILURE:
1870 	case WIFI_STAT_ACK_FAILURE:
1871 	case WIFI_STAT_RX_FRAGS:
1872 	case WIFI_STAT_MCAST_RX:
1873 	case WIFI_STAT_RX_DUPS:
1874 	case WIFI_STAT_FCS_ERRORS:
1875 	case WIFI_STAT_WEP_ERRORS:
1876 		return (ieee80211_stat(ic, stat, val));
1877 	/*
1878 	 * Need be supported later
1879 	 */
1880 	case MAC_STAT_IFSPEED:
1881 	default:
1882 		return (ENOTSUP);
1883 	}
1884 	return (0);
1885 }
1886 
1887 /* ARGSUSED */
1888 static int
1889 ipw2200_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
1890 {
1891 	/* not supported */
1892 	IPW2200_DBG(IPW2200_DBG_GLD, (((struct ipw2200_softc *)arg)->sc_dip,
1893 	    CE_CONT,
1894 	    "ipw2200_m_multicst(): enter\n"));
1895 
1896 	return (0);
1897 }
1898 
1899 /*
1900  * Multithread handler for linkstatus, fatal error recovery, get statistic
1901  */
1902 static void
1903 ipw2200_thread(struct ipw2200_softc *sc)
1904 {
1905 	struct ieee80211com	*ic = &sc->sc_ic;
1906 	enum ieee80211_state	ostate;
1907 	int32_t			nlstate;
1908 	int			stat_cnt = 0;
1909 
1910 	IPW2200_DBG(IPW2200_DBG_SOFTINT, (sc->sc_dip, CE_CONT,
1911 	    "ipw2200_thread(): enter, linkstate %d\n", sc->sc_linkstate));
1912 
1913 	mutex_enter(&sc->sc_mflock);
1914 
1915 	while (sc->sc_mfthread_switch) {
1916 		/*
1917 		 * when radio is off or SUSPEND status, nothing to do
1918 		 */
1919 		if ((ipw2200_radio_status(sc) == 0) ||
1920 		    sc->sc_flags & IPW2200_FLAG_SUSPEND) {
1921 			goto wait_loop;
1922 		}
1923 
1924 		/*
1925 		 * notify the link state
1926 		 */
1927 		if (ic->ic_mach && (sc->sc_flags & IPW2200_FLAG_LINK_CHANGE)) {
1928 
1929 			IPW2200_DBG(IPW2200_DBG_SOFTINT, (sc->sc_dip, CE_CONT,
1930 			    "ipw2200_thread(): link status --> %d\n",
1931 			    sc->sc_linkstate));
1932 
1933 			sc->sc_flags &= ~IPW2200_FLAG_LINK_CHANGE;
1934 			nlstate = sc->sc_linkstate;
1935 
1936 			mutex_exit(&sc->sc_mflock);
1937 			mac_link_update(ic->ic_mach, nlstate);
1938 			mutex_enter(&sc->sc_mflock);
1939 		}
1940 
1941 		/*
1942 		 * recovery fatal error
1943 		 */
1944 		if (ic->ic_mach &&
1945 		    (sc->sc_flags & IPW2200_FLAG_HW_ERR_RECOVER)) {
1946 
1947 			IPW2200_DBG(IPW2200_DBG_FATAL, (sc->sc_dip, CE_CONT,
1948 			    "ipw2200_thread(): "
1949 			    "try to recover fatal hw error\n"));
1950 
1951 			sc->sc_flags &= ~IPW2200_FLAG_HW_ERR_RECOVER;
1952 			mutex_exit(&sc->sc_mflock);
1953 
1954 			/* stop again */
1955 			ostate = ic->ic_state;
1956 			(void) ipw2200_init(sc); /* Force state machine */
1957 
1958 			/*
1959 			 * workround. Delay for a while after init especially
1960 			 * when something wrong happened already.
1961 			 */
1962 			delay(drv_usectohz(delay_fatal_recover));
1963 
1964 			/*
1965 			 * Init scan will recovery the original connection if
1966 			 * the original state is run
1967 			 */
1968 			if (ostate != IEEE80211_S_INIT)
1969 				ieee80211_begin_scan(ic, 0);
1970 
1971 			mutex_enter(&sc->sc_mflock);
1972 		}
1973 
1974 		/*
1975 		 * get statistic, the value will be retrieved by m_stat
1976 		 */
1977 		if (stat_cnt == 10) {
1978 
1979 			stat_cnt = 0; /* re-start */
1980 			mutex_exit(&sc->sc_mflock);
1981 			ipw2200_get_statistics(sc);
1982 			mutex_enter(&sc->sc_mflock);
1983 
1984 		} else
1985 			stat_cnt++; /* until 1s */
1986 
1987 wait_loop:
1988 		mutex_exit(&sc->sc_mflock);
1989 		delay(drv_usectohz(delay_aux_thread));
1990 		mutex_enter(&sc->sc_mflock);
1991 
1992 	}
1993 	sc->sc_mf_thread = NULL;
1994 	cv_signal(&sc->sc_mfthread_cv);
1995 	mutex_exit(&sc->sc_mflock);
1996 }
1997 
1998 static int
1999 ipw2200_m_start(void *arg)
2000 {
2001 	struct ipw2200_softc	*sc = (struct ipw2200_softc *)arg;
2002 	struct ieee80211com	*ic = &sc->sc_ic;
2003 
2004 	IPW2200_DBG(IPW2200_DBG_GLD, (sc->sc_dip, CE_CONT,
2005 	    "ipw2200_m_start(): enter\n"));
2006 	/*
2007 	 * initialize ipw2200 hardware, everything ok will start scan
2008 	 */
2009 	(void) ipw2200_init(sc);
2010 
2011 	/*
2012 	 * set the state machine to INIT
2013 	 */
2014 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2015 
2016 	sc->sc_flags |= IPW2200_FLAG_RUNNING;
2017 
2018 	/*
2019 	 * fix KCF bug. - workaround, need to fix it in net80211
2020 	 */
2021 	(void) crypto_mech2id(SUN_CKM_RC4);
2022 
2023 	return (0);
2024 }
2025 
2026 static void
2027 ipw2200_m_stop(void *arg)
2028 {
2029 	struct ipw2200_softc	*sc = (struct ipw2200_softc *)arg;
2030 	struct ieee80211com	*ic = &sc->sc_ic;
2031 
2032 	IPW2200_DBG(IPW2200_DBG_GLD, (sc->sc_dip, CE_CONT,
2033 	    "ipw2200_m_stop(): enter\n"));
2034 
2035 	ipw2200_stop(sc);
2036 	/*
2037 	 * set the state machine to INIT
2038 	 */
2039 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2040 
2041 	sc->sc_flags &= ~IPW2200_FLAG_RUNNING;
2042 }
2043 
2044 static int
2045 ipw2200_m_unicst(void *arg, const uint8_t *macaddr)
2046 {
2047 	struct ipw2200_softc	*sc = (struct ipw2200_softc *)arg;
2048 	struct ieee80211com	*ic = &sc->sc_ic;
2049 	int			err;
2050 
2051 	IPW2200_DBG(IPW2200_DBG_GLD, (sc->sc_dip, CE_CONT,
2052 	    "ipw2200_m_unicst(): enter\n"));
2053 
2054 	IPW2200_DBG(IPW2200_DBG_GLD, (sc->sc_dip, CE_CONT,
2055 	    "ipw2200_m_unicst(): GLD setting MAC address to "
2056 	    "%02x:%02x:%02x:%02x:%02x:%02x\n",
2057 	    macaddr[0], macaddr[1], macaddr[2],
2058 	    macaddr[3], macaddr[4], macaddr[5]));
2059 
2060 	if (!IEEE80211_ADDR_EQ(ic->ic_macaddr, macaddr)) {
2061 
2062 		IEEE80211_ADDR_COPY(ic->ic_macaddr, macaddr);
2063 
2064 		if (sc->sc_flags & IPW2200_FLAG_RUNNING) {
2065 			err = ipw2200_config(sc);
2066 			if (err != DDI_SUCCESS) {
2067 				IPW2200_WARN((sc->sc_dip, CE_WARN,
2068 				    "ipw2200_m_unicst(): "
2069 				    "device configuration failed\n"));
2070 				goto fail;
2071 			}
2072 		}
2073 	}
2074 	return (0);
2075 fail:
2076 	return (EIO);
2077 }
2078 
2079 static int
2080 ipw2200_m_promisc(void *arg, boolean_t on)
2081 {
2082 	/* not supported */
2083 	struct ipw2200_softc	*sc = (struct ipw2200_softc *)arg;
2084 
2085 	IPW2200_DBG(IPW2200_DBG_GLD, (sc->sc_dip, CE_CONT,
2086 	    "ipw2200_m_promisc(): enter. "
2087 	    "GLD setting promiscuous mode - %d\n", on));
2088 
2089 	return (0);
2090 }
2091 
2092 static mblk_t *
2093 ipw2200_m_tx(void *arg, mblk_t *mp)
2094 {
2095 	struct ipw2200_softc	*sc = (struct ipw2200_softc *)arg;
2096 	struct ieee80211com	*ic = &sc->sc_ic;
2097 	mblk_t			*next;
2098 
2099 	/*
2100 	 * when driver in on suspend state, freemsgchain directly
2101 	 */
2102 	if (sc->sc_flags & IPW2200_FLAG_SUSPEND) {
2103 		IPW2200_DBG(IPW2200_DBG_SUSPEND, (sc->sc_dip, CE_CONT,
2104 		    "ipw2200_m_tx(): suspend status, discard msg\n"));
2105 		sc->sc_stats.sc_tx_discard++; /* discard data */
2106 		freemsgchain(mp);
2107 		return (NULL);
2108 	}
2109 
2110 	/*
2111 	 * No data frames go out unless we're associated; this
2112 	 * should not happen as the 802.11 layer does not enable
2113 	 * the xmit queue until we enter the RUN state.
2114 	 */
2115 	if (ic->ic_state != IEEE80211_S_RUN) {
2116 		IPW2200_DBG(IPW2200_DBG_GLD, (sc->sc_dip, CE_CONT,
2117 		    "ipw2200_m_tx(): discard msg, ic_state = %u\n",
2118 		    ic->ic_state));
2119 		sc->sc_stats.sc_tx_discard++; /* discard data */
2120 		freemsgchain(mp);
2121 		return (NULL);
2122 	}
2123 
2124 	while (mp != NULL) {
2125 		next = mp->b_next;
2126 		mp->b_next = NULL;
2127 		if (ipw2200_send(ic, mp, IEEE80211_FC0_TYPE_DATA) ==
2128 		    ENOMEM) {
2129 			mp->b_next = next;
2130 			break;
2131 		}
2132 		mp = next;
2133 	}
2134 	return (mp);
2135 }
2136 
2137 /*
2138  * ipw2200_send(): send data. softway to handle crypto_encap.
2139  */
2140 static int
2141 ipw2200_send(ieee80211com_t *ic, mblk_t *mp, uint8_t type)
2142 {
2143 	struct ipw2200_softc	*sc = (struct ipw2200_softc *)ic;
2144 	struct ieee80211_node	*in;
2145 	struct ieee80211_frame	*wh;
2146 	struct ieee80211_key	*k;
2147 	mblk_t			*m0, *m;
2148 	size_t			cnt, off;
2149 	struct ipw2200_tx_desc	*txdsc;
2150 	struct dma_region	*dr;
2151 	uint32_t		idx;
2152 	int			err = DDI_SUCCESS;
2153 	/* tmp pointer, used to pack header and payload */
2154 	uint8_t			*p;
2155 
2156 	ASSERT(mp->b_next == NULL);
2157 	IPW2200_DBG(IPW2200_DBG_GLD, (sc->sc_dip, CE_CONT,
2158 	    "ipw2200_send(): enter\n"));
2159 
2160 	if ((type & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_DATA) {
2161 		/*
2162 		 * skip all management frames since ipw2200 won't generate any
2163 		 * management frames. Therefore, drop this package.
2164 		 */
2165 		freemsg(mp);
2166 		err = DDI_FAILURE;
2167 		goto fail0;
2168 	}
2169 
2170 	mutex_enter(&sc->sc_tx_lock);
2171 	if (sc->sc_flags & IPW2200_FLAG_SUSPEND) {
2172 		/*
2173 		 * when sending data, system runs into suspend status,
2174 		 * return fail directly
2175 		 */
2176 		err = ENXIO;
2177 		goto fail0;
2178 	}
2179 
2180 	/*
2181 	 * need 1 empty descriptor
2182 	 */
2183 	if (sc->sc_tx_free <= IPW2200_TX_RING_MIN) {
2184 		mutex_enter(&sc->sc_resched_lock);
2185 		IPW2200_DBG(IPW2200_DBG_RING, (sc->sc_dip, CE_WARN,
2186 		    "ipw2200_send(): no enough descriptors(%d)\n",
2187 		    sc->sc_tx_free));
2188 		ic->ic_stats.is_tx_nobuf++; /* no enough buffer */
2189 		sc->sc_flags |= IPW2200_FLAG_TX_SCHED;
2190 		err = ENOMEM;
2191 		mutex_exit(&sc->sc_resched_lock);
2192 		goto fail1;
2193 	}
2194 	IPW2200_DBG(IPW2200_DBG_RING, (sc->sc_dip, CE_CONT,
2195 	    "ipw2200_send():  tx-free=%d,tx-curr=%d\n",
2196 	    sc->sc_tx_free, sc->sc_tx_cur));
2197 
2198 	/*
2199 	 * put the mp into one blk, and use it to do the crypto_encap
2200 	 * if necessaary.
2201 	 */
2202 	m = allocb(msgdsize(mp) + 32, BPRI_MED);
2203 	if (m == NULL) { /* can not alloc buf, drop this package */
2204 		IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
2205 		    "ipw2200_send(): msg allocation failed\n"));
2206 		freemsg(mp);
2207 		sc->sc_stats.sc_tx_alloc_fail++; /* alloc fail */
2208 		ic->ic_stats.is_tx_failed++;  /* trans failed */
2209 		err = DDI_FAILURE;
2210 		goto fail1;
2211 	}
2212 	for (off = 0, m0 = mp; m0 != NULL; m0 = m0->b_cont) {
2213 		cnt = MBLKL(m0);
2214 		(void) memcpy(m->b_rptr + off, m0->b_rptr, cnt);
2215 		off += cnt;
2216 	}
2217 	m->b_wptr += off;
2218 
2219 	/*
2220 	 * find tx_node, and encapsulate the data
2221 	 */
2222 	wh = (struct ieee80211_frame *)m->b_rptr;
2223 	in = ieee80211_find_txnode(ic, wh->i_addr1);
2224 	if (in == NULL) { /* can not find the tx node, drop the package */
2225 		sc->sc_stats.sc_tx_encap_fail++; /* tx encap fail */
2226 		ic->ic_stats.is_tx_failed++; /* trans failed */
2227 		freemsg(mp);
2228 		err = DDI_FAILURE;
2229 		goto fail2;
2230 	}
2231 	in->in_inact = 0;
2232 
2233 	(void) ieee80211_encap(ic, m, in);
2234 	ieee80211_free_node(in);
2235 
2236 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2237 		k = ieee80211_crypto_encap(ic, m);
2238 		if (k == NULL) { /* can not get the key, drop packages */
2239 			IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
2240 			    "ipw2200_send(): "
2241 			    "Encrypting 802.11 frame failed\n"));
2242 			sc->sc_stats.sc_tx_crypto_fail++; /* tx encap fail */
2243 			ic->ic_stats.is_tx_failed++; /* trans failed */
2244 			freemsg(mp);
2245 			err = DDI_FAILURE;
2246 			goto fail2;
2247 		}
2248 		wh = (struct ieee80211_frame *)m->b_rptr;
2249 	}
2250 
2251 	/*
2252 	 * get txdsc
2253 	 */
2254 	idx	= sc->sc_tx_cur;
2255 	txdsc	= &sc->sc_txdsc[idx];
2256 	(void) memset(txdsc, 0, sizeof (*txdsc));
2257 	/*
2258 	 * extract header from message
2259 	 */
2260 	p	= (uint8_t *)&txdsc->wh;
2261 	off	= sizeof (struct ieee80211_frame);
2262 	(void) memcpy(p, m->b_rptr, off);
2263 	/*
2264 	 * extract payload from message
2265 	 */
2266 	dr	= &sc->sc_dma_txbufs[idx];
2267 	p	= sc->sc_txbufs[idx];
2268 	cnt	= MBLKL(m);
2269 	(void) memcpy(p, m->b_rptr + off, cnt - off);
2270 	cnt    -= off;
2271 
2272 	txdsc->hdr.type   = IPW2200_HDR_TYPE_DATA;
2273 	txdsc->hdr.flags  = IPW2200_HDR_FLAG_IRQ;
2274 	txdsc->cmd	  = IPW2200_DATA_CMD_TX;
2275 	txdsc->len	  = LE_16(cnt);
2276 	txdsc->flags	  = 0;
2277 
2278 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2279 		if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
2280 			txdsc->flags |= IPW2200_DATA_FLAG_NEED_ACK;
2281 	} else if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
2282 		txdsc->flags |= IPW2200_DATA_FLAG_NEED_ACK;
2283 
2284 	/* always set it to none wep, because it's handled by software */
2285 	txdsc->flags |= IPW2200_DATA_FLAG_NO_WEP;
2286 
2287 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2288 		txdsc->flags |= IPW2200_DATA_FLAG_SHPREAMBLE;
2289 
2290 	txdsc->nseg	    = LE_32(1);
2291 	txdsc->seg_addr[0]  = LE_32(dr->dr_pbase);
2292 	txdsc->seg_len[0]   = LE_32(cnt);
2293 
2294 	/*
2295 	 * DMA sync: buffer and desc
2296 	 */
2297 	(void) ddi_dma_sync(dr->dr_hnd, 0,
2298 	    IPW2200_TXBUF_SIZE, DDI_DMA_SYNC_FORDEV);
2299 	(void) ddi_dma_sync(sc->sc_dma_txdsc.dr_hnd,
2300 	    idx * sizeof (struct ipw2200_tx_desc),
2301 	    sizeof (struct ipw2200_tx_desc), DDI_DMA_SYNC_FORDEV);
2302 
2303 	sc->sc_tx_cur = RING_FORWARD(sc->sc_tx_cur, 1, IPW2200_TX_RING_SIZE);
2304 	sc->sc_tx_free--;
2305 
2306 	/*
2307 	 * update txcur
2308 	 */
2309 	ipw2200_csr_put32(sc, IPW2200_CSR_TX1_WRITE_INDEX, sc->sc_tx_cur);
2310 
2311 	/*
2312 	 * success, free the original message
2313 	 */
2314 	if (mp)
2315 		freemsg(mp);
2316 fail2:
2317 	if (m)
2318 		freemsg(m);
2319 fail1:
2320 	mutex_exit(&sc->sc_tx_lock);
2321 fail0:
2322 	IPW2200_DBG(IPW2200_DBG_GLD, (sc->sc_dip, CE_CONT,
2323 	    "ipw2200_send(): exit - err=%d\n", err));
2324 
2325 	return (err);
2326 }
2327 
2328 /*
2329  * IOCTL handlers
2330  */
2331 #define	IEEE80211_IOCTL_REQUIRED	(1)
2332 #define	IEEE80211_IOCTL_NOT_REQUIRED	(0)
2333 static void
2334 ipw2200_m_ioctl(void *arg, queue_t *q, mblk_t *m)
2335 {
2336 	struct ipw2200_softc	*sc = (struct ipw2200_softc *)arg;
2337 	struct ieee80211com	*ic = &sc->sc_ic;
2338 	uint32_t		err;
2339 
2340 	IPW2200_DBG(IPW2200_DBG_GLD, (sc->sc_dip, CE_CONT,
2341 	    "ipw2200_m_ioctl(): enter\n"));
2342 
2343 	/*
2344 	 * Check whether or not need to handle this in net80211
2345 	 *
2346 	 */
2347 	if (ipw2200_ioctl(sc, q, m) == IEEE80211_IOCTL_NOT_REQUIRED)
2348 		return;
2349 
2350 	err = ieee80211_ioctl(ic, q, m);
2351 	if (err == ENETRESET) {
2352 		if (sc->sc_flags & IPW2200_FLAG_RUNNING) {
2353 			(void) ipw2200_m_start(sc);
2354 			(void) ieee80211_new_state(ic,
2355 			    IEEE80211_S_SCAN, -1);
2356 		}
2357 	}
2358 	if (err == ERESTART) {
2359 		if (sc->sc_flags & IPW2200_FLAG_RUNNING)
2360 			(void) ipw2200_chip_reset(sc);
2361 	}
2362 }
2363 static int
2364 ipw2200_ioctl(struct ipw2200_softc *sc, queue_t *q, mblk_t *m)
2365 {
2366 	struct iocblk	*iocp;
2367 	uint32_t	len, ret, cmd, mblen;
2368 	mblk_t		*m0;
2369 	boolean_t	need_privilege;
2370 	boolean_t	need_net80211;
2371 
2372 	mblen = MBLKL(m);
2373 	if (mblen < sizeof (struct iocblk)) {
2374 		IPW2200_DBG(IPW2200_DBG_IOCTL, (sc->sc_dip, CE_CONT,
2375 		    "ipw2200_ioctl(): ioctl buffer too short, %u\n",
2376 		    mblen));
2377 		miocnak(q, m, 0, EINVAL);
2378 		/*
2379 		 * Buf not enough, do not need net80211 either
2380 		 */
2381 		return (IEEE80211_IOCTL_NOT_REQUIRED);
2382 	}
2383 
2384 	/*
2385 	 * Validate the command
2386 	 */
2387 	iocp = (struct iocblk *)(uintptr_t)m->b_rptr;
2388 	iocp->ioc_error = 0;
2389 	cmd = iocp->ioc_cmd;
2390 	need_privilege = B_TRUE;
2391 	switch (cmd) {
2392 	case WLAN_SET_PARAM:
2393 	case WLAN_COMMAND:
2394 		break;
2395 	case WLAN_GET_PARAM:
2396 		need_privilege = B_FALSE;
2397 		break;
2398 	default:
2399 		IPW2200_DBG(IPW2200_DBG_IOCTL, (sc->sc_dip, CE_CONT,
2400 		    "ipw2200_ioctl(): unknown cmd 0x%x", cmd));
2401 		miocnak(q, m, 0, EINVAL);
2402 		/*
2403 		 * Unknown cmd, do not need net80211 either
2404 		 */
2405 		return (IEEE80211_IOCTL_NOT_REQUIRED);
2406 	}
2407 
2408 	if (need_privilege && (ret = secpolicy_dl_config(iocp->ioc_cr)) != 0) {
2409 		miocnak(q, m, 0, ret);
2410 		/*
2411 		 * privilege check fail, do not need net80211 either
2412 		 */
2413 		return (IEEE80211_IOCTL_NOT_REQUIRED);
2414 	}
2415 
2416 	/*
2417 	 * sanity check
2418 	 */
2419 	m0 = m->b_cont;
2420 	if (iocp->ioc_count == 0 || iocp->ioc_count < sizeof (wldp_t) ||
2421 	    m0 == NULL) {
2422 		miocnak(q, m, 0, EINVAL);
2423 		/*
2424 		 * invalid format, do not need net80211 either
2425 		 */
2426 		return (IEEE80211_IOCTL_NOT_REQUIRED);
2427 	}
2428 	/*
2429 	 * assuming single data block
2430 	 */
2431 	if (m0->b_cont) {
2432 		freemsg(m0->b_cont);
2433 		m0->b_cont = NULL;
2434 	}
2435 
2436 	need_net80211 = B_FALSE;
2437 	ret = ipw2200_getset(sc, m0, cmd, &need_net80211);
2438 	if (!need_net80211) {
2439 		len = msgdsize(m0);
2440 
2441 		IPW2200_DBG(IPW2200_DBG_IOCTL, (sc->sc_dip, CE_CONT,
2442 		    "ipw2200_ioctl(): go to call miocack with "
2443 		    "ret = %d, len = %d\n", ret, len));
2444 		miocack(q, m, len, ret);
2445 		return (IEEE80211_IOCTL_NOT_REQUIRED);
2446 	}
2447 
2448 	/*
2449 	 * IEEE80211_IOCTL - need net80211 handle
2450 	 */
2451 	return (IEEE80211_IOCTL_REQUIRED);
2452 }
2453 
2454 static int
2455 ipw2200_getset(struct ipw2200_softc *sc, mblk_t *m, uint32_t cmd,
2456 	boolean_t *need_net80211)
2457 {
2458 	wldp_t		*infp, *outfp;
2459 	uint32_t	id;
2460 	int		ret;
2461 
2462 	infp  = (wldp_t *)(uintptr_t)m->b_rptr;
2463 	outfp = (wldp_t *)(uintptr_t)m->b_rptr;
2464 	outfp->wldp_result = WL_NOTSUPPORTED;
2465 
2466 	id = infp->wldp_id;
2467 	IPW2200_DBG(IPW2200_DBG_IOCTL, (sc->sc_dip, CE_CONT,
2468 	    "ipw2200_getset(): id = 0x%x\n", id));
2469 	switch (id) {
2470 	case WL_RADIO: /* which is not supported by net80211 */
2471 		ret = iwi_wificfg_radio(sc, cmd, outfp);
2472 		break;
2473 	case WL_DESIRED_RATES: /* hardware doesn't support fix-rates */
2474 		ret = iwi_wificfg_desrates(outfp);
2475 		break;
2476 	default:
2477 		/*
2478 		 * The wifi IOCTL net80211 supported:
2479 		 *	case WL_ESSID:
2480 		 *	case WL_BSSID:
2481 		 *	case WL_WEP_KEY_TAB:
2482 		 *	case WL_WEP_KEY_ID:
2483 		 *	case WL_AUTH_MODE:
2484 		 *	case WL_ENCRYPTION:
2485 		 *	case WL_BSS_TYPE:
2486 		 *	case WL_ESS_LIST:
2487 		 *	case WL_LINKSTATUS:
2488 		 *	case WL_RSSI:
2489 		 *	case WL_SCAN:
2490 		 *	case WL_LOAD_DEFAULTS:
2491 		 *	case WL_DISASSOCIATE:
2492 		 */
2493 
2494 		/*
2495 		 * When radio is off, need to ignore all ioctl.  What need to
2496 		 * do is to check radio status firstly.  If radio is ON, pass
2497 		 * it to net80211, otherwise, return to upper layer directly.
2498 		 *
2499 		 * Considering the WL_SUCCESS also means WL_CONNECTED for
2500 		 * checking linkstatus, one exception for WL_LINKSTATUS is to
2501 		 * let net80211 handle it.
2502 		 */
2503 		if ((ipw2200_radio_status(sc) == 0) &&
2504 		    (id != WL_LINKSTATUS)) {
2505 
2506 			IPW2200_REPORT((sc->sc_dip, CE_CONT,
2507 			    "iwi: radio is OFF\n"));
2508 
2509 			outfp->wldp_length = WIFI_BUF_OFFSET;
2510 			outfp->wldp_result = WL_SUCCESS;
2511 			ret = 0;
2512 			break;
2513 		}
2514 
2515 		*need_net80211 = B_TRUE; /* let net80211 do the rest */
2516 		return (0);
2517 	}
2518 	/*
2519 	 * we will overwrite everything
2520 	 */
2521 	m->b_wptr = m->b_rptr + outfp->wldp_length;
2522 	return (ret);
2523 }
2524 
2525 static int
2526 iwi_wificfg_radio(struct ipw2200_softc *sc, uint32_t cmd, wldp_t *outfp)
2527 {
2528 	uint32_t	ret = ENOTSUP;
2529 
2530 	switch (cmd) {
2531 	case WLAN_GET_PARAM:
2532 		*(wl_linkstatus_t *)(outfp->wldp_buf) =
2533 		    ipw2200_radio_status(sc);
2534 		outfp->wldp_length = WIFI_BUF_OFFSET + sizeof (wl_linkstatus_t);
2535 		outfp->wldp_result = WL_SUCCESS;
2536 		ret = 0; /* command success */
2537 		break;
2538 	case WLAN_SET_PARAM:
2539 	default:
2540 		break;
2541 	}
2542 	return (ret);
2543 }
2544 
2545 static int
2546 iwi_wificfg_desrates(wldp_t *outfp)
2547 {
2548 	/* return success, but with result NOTSUPPORTED */
2549 	outfp->wldp_length = WIFI_BUF_OFFSET;
2550 	outfp->wldp_result = WL_NOTSUPPORTED;
2551 	return (0);
2552 }
2553 /* End of IOCTL Handlers */
2554 
2555 void
2556 ipw2200_fix_channel(struct ieee80211com *ic, mblk_t *m)
2557 {
2558 	struct ieee80211_frame	*wh;
2559 	uint8_t			subtype;
2560 	uint8_t			*frm, *efrm;
2561 
2562 	wh = (struct ieee80211_frame *)m->b_rptr;
2563 
2564 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
2565 		return;
2566 
2567 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2568 
2569 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
2570 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2571 		return;
2572 
2573 	/*
2574 	 * assume the message contains only 1 block
2575 	 */
2576 	frm   = (uint8_t *)(wh + 1);
2577 	efrm  = (uint8_t *)m->b_wptr;
2578 	frm  += 12;  /* skip tstamp, bintval and capinfo fields */
2579 	while (frm < efrm) {
2580 		if (*frm == IEEE80211_ELEMID_DSPARMS)
2581 #if IEEE80211_CHAN_MAX < 255
2582 		if (frm[2] <= IEEE80211_CHAN_MAX)
2583 #endif
2584 			ic->ic_curchan = &ic->ic_sup_channels[frm[2]];
2585 		frm += frm[1] + 2;
2586 	}
2587 }
2588 
2589 static void
2590 ipw2200_rcv_frame(struct ipw2200_softc *sc, struct ipw2200_frame *frame)
2591 {
2592 	struct ieee80211com	*ic = &sc->sc_ic;
2593 	uint8_t			*data = (uint8_t *)frame;
2594 	uint32_t		len;
2595 	struct ieee80211_frame	*wh;
2596 	struct ieee80211_node	*in;
2597 	mblk_t			*m;
2598 
2599 	len = LE_16(frame->len);
2600 	if ((len < sizeof (struct ieee80211_frame_min)) ||
2601 	    (len > IPW2200_RXBUF_SIZE)) {
2602 		IPW2200_DBG(IPW2200_DBG_RX, (sc->sc_dip, CE_CONT,
2603 		    "ipw2200_rcv_frame(): bad frame length=%u\n",
2604 		    LE_16(frame->len)));
2605 		sc->sc_stats.sc_rx_len_err++; /* length doesn't work */
2606 		return;
2607 	}
2608 	IPW2200_DBG(IPW2200_DBG_RX, (sc->sc_dip, CE_CONT,
2609 	    "ipw2200_rcv_frame(): chan = %d, length = %d\n", frame->chan, len));
2610 
2611 	/*
2612 	 * Skip the frame header, get the real data from the input
2613 	 */
2614 	data += sizeof (struct ipw2200_frame);
2615 
2616 	m = allocb(len, BPRI_MED);
2617 	if (m) {
2618 		(void) memcpy(m->b_wptr, data, len);
2619 		m->b_wptr += len;
2620 
2621 		if (ic->ic_state == IEEE80211_S_SCAN) {
2622 			ic->ic_ibss_chan = &ic->ic_sup_channels[frame->chan];
2623 			ipw2200_fix_channel(ic, m);
2624 		}
2625 		wh = (struct ieee80211_frame *)m->b_rptr;
2626 
2627 		in = ieee80211_find_rxnode(ic, wh);
2628 
2629 		IPW2200_DBG(IPW2200_DBG_RX, (sc->sc_dip, CE_CONT,
2630 		    "ipw2200_rcv_frame(): "
2631 		    "type = %x, subtype = %x, i_fc[1] = %x, "
2632 		    "ni_esslen:%d, ni_essid[0-5]:%c%c%c%c%c%c\n",
2633 		    wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK,
2634 		    wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK,
2635 		    wh->i_fc[1] & IEEE80211_FC1_WEP,
2636 		    in->in_esslen,
2637 		    in->in_essid[0], in->in_essid[1], in->in_essid[2],
2638 		    in->in_essid[3], in->in_essid[4], in->in_essid[5]));
2639 
2640 		(void) ieee80211_input(ic, m, in, frame->rssi_dbm, 0);
2641 
2642 		ieee80211_free_node(in);
2643 	}
2644 	else
2645 		IPW2200_WARN((sc->sc_dip, CE_WARN,
2646 		    "ipw2200_rcv_frame(): "
2647 		    "cannot allocate receive message(%u)\n",
2648 		    LE_16(frame->len)));
2649 }
2650 
2651 static void
2652 ipw2200_rcv_notif(struct ipw2200_softc *sc, struct ipw2200_notif *notif)
2653 {
2654 	struct ieee80211com			*ic = &sc->sc_ic;
2655 	struct ipw2200_notif_association	*assoc;
2656 	struct ipw2200_notif_authentication	*auth;
2657 	uint8_t					*ndata = (uint8_t *)notif;
2658 
2659 	IPW2200_DBG(IPW2200_DBG_NOTIF, (sc->sc_dip, CE_CONT,
2660 	    "ipw2200_rcv_notif(): type=%u\n", notif->type));
2661 
2662 	ndata += sizeof (struct ipw2200_notif);
2663 	switch (notif->type) {
2664 	case IPW2200_NOTIF_TYPE_ASSOCIATION:
2665 		assoc = (struct ipw2200_notif_association *)ndata;
2666 
2667 		IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
2668 		    "ipw2200_rcv_notif(): association=%u,%u\n",
2669 		    assoc->state, assoc->status));
2670 
2671 		switch (assoc->state) {
2672 		case IPW2200_ASSOC_SUCCESS:
2673 			sc->sc_flags |= IPW2200_FLAG_ASSOCIATED;
2674 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2675 			break;
2676 		case IPW2200_ASSOC_FAIL:
2677 			sc->sc_flags &= ~IPW2200_FLAG_ASSOCIATED;
2678 			ieee80211_begin_scan(ic, 1);
2679 			break;
2680 		default:
2681 			break;
2682 		}
2683 		break;
2684 
2685 	case IPW2200_NOTIF_TYPE_AUTHENTICATION:
2686 		auth = (struct ipw2200_notif_authentication *)ndata;
2687 
2688 		IPW2200_DBG(IPW2200_DBG_WIFI, (sc->sc_dip, CE_CONT,
2689 		    "ipw2200_rcv_notif(): authentication=%u\n", auth->state));
2690 
2691 		switch (auth->state) {
2692 		case IPW2200_AUTH_SUCCESS:
2693 			ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
2694 			break;
2695 		case IPW2200_AUTH_FAIL:
2696 			sc->sc_flags &= ~IPW2200_FLAG_ASSOCIATED;
2697 			break;
2698 		default:
2699 			IPW2200_DBG(IPW2200_DBG_NOTIF, (sc->sc_dip, CE_CONT,
2700 			    "ipw2200_rcv_notif(): "
2701 			    "unknown authentication state(%u)\n", auth->state));
2702 			break;
2703 		}
2704 		break;
2705 
2706 	case IPW2200_NOTIF_TYPE_SCAN_CHANNEL:
2707 		IPW2200_DBG(IPW2200_DBG_SCAN, (sc->sc_dip, CE_CONT,
2708 		    "ipw2200_rcv_notif(): scan-channel=%u\n",
2709 		    ((struct ipw2200_notif_scan_channel *)ndata)->nchan));
2710 		break;
2711 
2712 	case IPW2200_NOTIF_TYPE_SCAN_COMPLETE:
2713 		IPW2200_DBG(IPW2200_DBG_SCAN, (sc->sc_dip, CE_CONT,
2714 		    "ipw2200_rcv_notif():scan-completed,(%u,%u)\n",
2715 		    ((struct ipw2200_notif_scan_complete *)ndata)->nchan,
2716 		    ((struct ipw2200_notif_scan_complete *)ndata)->status));
2717 
2718 		/*
2719 		 * scan complete
2720 		 */
2721 		sc->sc_flags &= ~IPW2200_FLAG_SCANNING;
2722 		ieee80211_end_scan(ic);
2723 		break;
2724 
2725 	case IPW2200_NOTIF_TYPE_BEACON:
2726 	case IPW2200_NOTIF_TYPE_CALIBRATION:
2727 	case IPW2200_NOTIF_TYPE_NOISE:
2728 		/*
2729 		 * just ignore
2730 		 */
2731 		break;
2732 	default:
2733 		IPW2200_DBG(IPW2200_DBG_NOTIF, (sc->sc_dip, CE_CONT,
2734 		    "ipw2200_rcv_notif(): unknown notification type(%u)\n",
2735 		    notif->type));
2736 		break;
2737 	}
2738 }
2739 
2740 static uint_t
2741 ipw2200_intr(caddr_t arg)
2742 {
2743 	struct ipw2200_softc	*sc = (struct ipw2200_softc *)(uintptr_t)arg;
2744 	struct ieee80211com	*ic = &sc->sc_ic;
2745 	uint32_t		ireg, ridx, len, i;
2746 	uint8_t			*p, *rxbuf;
2747 	struct dma_region	*dr;
2748 	struct ipw2200_hdr	*hdr;
2749 	uint32_t		widx;
2750 
2751 	/* when it is on suspend, unclaim all interrupt directly */
2752 	if (sc->sc_flags & IPW2200_FLAG_SUSPEND)
2753 		return (DDI_INTR_UNCLAIMED);
2754 
2755 	/* unclaim interrupt when it is not for iwi */
2756 	ireg = ipw2200_csr_get32(sc, IPW2200_CSR_INTR);
2757 	if (ireg == 0xffffffff ||
2758 	    !(ireg & IPW2200_INTR_MASK_ALL))
2759 		return (DDI_INTR_UNCLAIMED);
2760 
2761 	/*
2762 	 * mask all interrupts
2763 	 */
2764 	ipw2200_csr_put32(sc, IPW2200_CSR_INTR_MASK, 0);
2765 
2766 	/*
2767 	 * acknowledge all fired interrupts
2768 	 */
2769 	ipw2200_csr_put32(sc, IPW2200_CSR_INTR, ireg);
2770 
2771 	IPW2200_DBG(IPW2200_DBG_INT, (sc->sc_dip, CE_CONT,
2772 	    "ipw2200_intr(): enter. interrupt fired, int=0x%08x\n", ireg));
2773 
2774 	if (ireg & IPW2200_INTR_MASK_ERR) {
2775 
2776 		IPW2200_DBG(IPW2200_DBG_FATAL, (sc->sc_dip, CE_CONT,
2777 		    "ipw2200 interrupt(): int= 0x%08x\n", ireg));
2778 
2779 		/*
2780 		 * inform mfthread to recover hw error by stopping it
2781 		 */
2782 		mutex_enter(&sc->sc_mflock);
2783 		sc->sc_flags |= IPW2200_FLAG_HW_ERR_RECOVER;
2784 		mutex_exit(&sc->sc_mflock);
2785 
2786 		goto enable_interrupt;
2787 	}
2788 
2789 	/*
2790 	 * FW intr
2791 	 */
2792 	if (ireg & IPW2200_INTR_FW_INITED) {
2793 		mutex_enter(&sc->sc_ilock);
2794 		sc->sc_fw_ok = 1;
2795 		cv_signal(&sc->sc_fw_cond);
2796 		mutex_exit(&sc->sc_ilock);
2797 	}
2798 
2799 	/*
2800 	 * Radio OFF
2801 	 */
2802 	if (ireg & IPW2200_INTR_RADIO_OFF) {
2803 		IPW2200_REPORT((sc->sc_dip, CE_CONT,
2804 		    "ipw2200_intr(): radio is OFF\n"));
2805 
2806 		/*
2807 		 * Stop hardware, will notify LINK is down.
2808 		 * Need a better scan solution to ensure
2809 		 * table has right value.
2810 		 */
2811 		ipw2200_stop(sc);
2812 	}
2813 
2814 	/*
2815 	 * CMD intr
2816 	 */
2817 	if (ireg & IPW2200_INTR_CMD_TRANSFER) {
2818 		mutex_enter(&sc->sc_cmd_lock);
2819 		ridx = ipw2200_csr_get32(sc,
2820 		    IPW2200_CSR_CMD_READ_INDEX);
2821 		i = RING_FORWARD(sc->sc_cmd_cur,
2822 		    sc->sc_cmd_free, IPW2200_CMD_RING_SIZE);
2823 		len = RING_FLEN(i, ridx, IPW2200_CMD_RING_SIZE);
2824 
2825 		IPW2200_DBG(IPW2200_DBG_INT, (sc->sc_dip, CE_CONT,
2826 		    "ipw2200_intr(): cmd-ring,i=%u,ridx=%u,len=%u\n",
2827 		    i, ridx, len));
2828 
2829 		if (len > 0) {
2830 			sc->sc_cmd_free += len;
2831 			cv_signal(&sc->sc_cmd_cond);
2832 		}
2833 		for (; i != ridx;
2834 		    i = RING_FORWARD(i, 1, IPW2200_CMD_RING_SIZE))
2835 			sc->sc_done[i] = 1;
2836 		mutex_exit(&sc->sc_cmd_lock);
2837 
2838 		mutex_enter(&sc->sc_ilock);
2839 		cv_signal(&sc->sc_cmd_status_cond);
2840 		mutex_exit(&sc->sc_ilock);
2841 	}
2842 
2843 	/*
2844 	 * RX intr
2845 	 */
2846 	if (ireg & IPW2200_INTR_RX_TRANSFER) {
2847 		ridx = ipw2200_csr_get32(sc,
2848 		    IPW2200_CSR_RX_READ_INDEX);
2849 		widx = ipw2200_csr_get32(sc,
2850 		    IPW2200_CSR_RX_WRITE_INDEX);
2851 
2852 		IPW2200_DBG(IPW2200_DBG_INT, (sc->sc_dip, CE_CONT,
2853 		    "ipw2200_intr(): rx-ring,widx=%u,ridx=%u\n",
2854 		    ridx, widx));
2855 
2856 		for (; sc->sc_rx_cur != ridx;
2857 		    sc->sc_rx_cur = RING_FORWARD(sc->sc_rx_cur, 1,
2858 		    IPW2200_RX_RING_SIZE)) {
2859 			i	= sc->sc_rx_cur;
2860 			rxbuf	= sc->sc_rxbufs[i];
2861 			dr	= &sc->sc_dma_rxbufs[i];
2862 
2863 			/*
2864 			 * DMA sync
2865 			 */
2866 			(void) ddi_dma_sync(dr->dr_hnd, 0,
2867 			    IPW2200_RXBUF_SIZE, DDI_DMA_SYNC_FORKERNEL);
2868 			/*
2869 			 * Get rx header(hdr) and rx data(p) from rxbuf
2870 			 */
2871 			p	= rxbuf;
2872 			hdr	= (struct ipw2200_hdr *)p;
2873 			p	+= sizeof (struct ipw2200_hdr);
2874 
2875 			IPW2200_DBG(IPW2200_DBG_INT, (sc->sc_dip, CE_CONT,
2876 			    "ipw2200_intr(): Rx hdr type %u\n",
2877 			    hdr->type));
2878 
2879 			switch (hdr->type) {
2880 			case IPW2200_HDR_TYPE_FRAME:
2881 				ipw2200_rcv_frame(sc,
2882 				    (struct ipw2200_frame *)p);
2883 				break;
2884 
2885 			case IPW2200_HDR_TYPE_NOTIF:
2886 				ipw2200_rcv_notif(sc,
2887 				    (struct ipw2200_notif *)p);
2888 				break;
2889 
2890 			default:
2891 				IPW2200_DBG(IPW2200_DBG_INT, (sc->sc_dip,
2892 				    CE_CONT,
2893 				    "ipw2200_intr(): unknown Rx hdr type %u\n",
2894 				    hdr->type));
2895 				break;
2896 			}
2897 		}
2898 		/*
2899 		 * write sc_rx_cur backward 1 step into RX_WRITE_INDEX
2900 		 */
2901 		ipw2200_csr_put32(sc, IPW2200_CSR_RX_WRITE_INDEX,
2902 		    RING_BACKWARD(sc->sc_rx_cur, 1,
2903 		    IPW2200_RX_RING_SIZE));
2904 	}
2905 
2906 	/*
2907 	 * TX intr
2908 	 */
2909 	if (ireg & IPW2200_INTR_TX1_TRANSFER) {
2910 		mutex_enter(&sc->sc_tx_lock);
2911 		ridx = ipw2200_csr_get32(sc,
2912 		    IPW2200_CSR_TX1_READ_INDEX);
2913 		len  = RING_FLEN(RING_FORWARD(sc->sc_tx_cur,
2914 		    sc->sc_tx_free, IPW2200_TX_RING_SIZE),
2915 		    ridx, IPW2200_TX_RING_SIZE);
2916 		sc->sc_tx_free += len;
2917 		IPW2200_DBG(IPW2200_DBG_RING, (sc->sc_dip, CE_CONT,
2918 		    "ipw2200_intr(): tx-ring,ridx=%u,len=%u\n",
2919 		    ridx, len));
2920 		mutex_exit(&sc->sc_tx_lock);
2921 
2922 		mutex_enter(&sc->sc_resched_lock);
2923 		if ((sc->sc_tx_free > IPW2200_TX_RING_MIN) &&
2924 		    (sc->sc_flags & IPW2200_FLAG_TX_SCHED)) {
2925 			IPW2200_DBG(IPW2200_DBG_RING, (sc->sc_dip,
2926 			    CE_CONT,
2927 			    "ipw2200_intr(): Need Reschedule!"));
2928 			sc->sc_flags &= ~IPW2200_FLAG_TX_SCHED;
2929 			mac_tx_update(ic->ic_mach);
2930 		}
2931 		mutex_exit(&sc->sc_resched_lock);
2932 	}
2933 
2934 enable_interrupt:
2935 	/*
2936 	 * enable all interrupts
2937 	 */
2938 	ipw2200_csr_put32(sc, IPW2200_CSR_INTR_MASK, IPW2200_INTR_MASK_ALL);
2939 
2940 	return (DDI_INTR_CLAIMED);
2941 }
2942 
2943 
2944 /*
2945  *  Module Loading Data & Entry Points
2946  */
2947 DDI_DEFINE_STREAM_OPS(ipw2200_devops, nulldev, nulldev, ipw2200_attach,
2948     ipw2200_detach, ipw2200_reset, NULL, D_MP, NULL, ddi_quiesce_not_supported);
2949 
2950 static struct modldrv ipw2200_modldrv = {
2951 	&mod_driverops,
2952 	ipw2200_ident,
2953 	&ipw2200_devops
2954 };
2955 
2956 static struct modlinkage ipw2200_modlinkage = {
2957 	MODREV_1,
2958 	&ipw2200_modldrv,
2959 	NULL
2960 };
2961 
2962 int
2963 _init(void)
2964 {
2965 	int status;
2966 
2967 	status = ddi_soft_state_init(&ipw2200_ssp,
2968 	    sizeof (struct ipw2200_softc), 1);
2969 	if (status != DDI_SUCCESS)
2970 		return (status);
2971 
2972 	mac_init_ops(&ipw2200_devops, IPW2200_DRV_NAME);
2973 	status = mod_install(&ipw2200_modlinkage);
2974 	if (status != DDI_SUCCESS) {
2975 		mac_fini_ops(&ipw2200_devops);
2976 		ddi_soft_state_fini(&ipw2200_ssp);
2977 	}
2978 
2979 	return (status);
2980 }
2981 
2982 int
2983 _fini(void)
2984 {
2985 	int status;
2986 
2987 	status = mod_remove(&ipw2200_modlinkage);
2988 	if (status == DDI_SUCCESS) {
2989 		mac_fini_ops(&ipw2200_devops);
2990 		ddi_soft_state_fini(&ipw2200_ssp);
2991 	}
2992 
2993 	return (status);
2994 }
2995 
2996 int
2997 _info(struct modinfo *modinfop)
2998 {
2999 	return (mod_info(&ipw2200_modlinkage, modinfop));
3000 }
3001