xref: /freebsd/sys/dev/ipw/if_ipw.c (revision 19fa1aa698364d0b6962471809b80d7682fd5e2a)
1 /*	$FreeBSD$	*/
2 
3 /*-
4  * Copyright (c) 2004-2006
5  *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6  * Copyright (c) 2006 Sam Leffler, Errno Consulting
7  * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 /*-
36  * Intel(R) PRO/Wireless 2100 MiniPCI driver
37  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
38  */
39 
40 #include <sys/param.h>
41 #include <sys/sysctl.h>
42 #include <sys/sockio.h>
43 #include <sys/mbuf.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/queue.h>
49 #include <sys/taskqueue.h>
50 #include <sys/module.h>
51 #include <sys/bus.h>
52 #include <sys/endian.h>
53 #include <sys/linker.h>
54 #include <sys/firmware.h>
55 
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58 #include <sys/rman.h>
59 
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
62 
63 #include <net/bpf.h>
64 #include <net/if.h>
65 #include <net/if_var.h>
66 #include <net/if_arp.h>
67 #include <net/ethernet.h>
68 #include <net/if_dl.h>
69 #include <net/if_media.h>
70 #include <net/if_types.h>
71 
72 #include <net80211/ieee80211_var.h>
73 #include <net80211/ieee80211_radiotap.h>
74 
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #include <netinet/if_ether.h>
80 
81 #include <dev/ipw/if_ipwreg.h>
82 #include <dev/ipw/if_ipwvar.h>
83 
84 #define IPW_DEBUG
85 #ifdef IPW_DEBUG
86 #define DPRINTF(x)	do { if (ipw_debug > 0) printf x; } while (0)
87 #define DPRINTFN(n, x)	do { if (ipw_debug >= (n)) printf x; } while (0)
88 int ipw_debug = 0;
89 SYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
90 #else
91 #define DPRINTF(x)
92 #define DPRINTFN(n, x)
93 #endif
94 
95 MODULE_DEPEND(ipw, pci,  1, 1, 1);
96 MODULE_DEPEND(ipw, wlan, 1, 1, 1);
97 MODULE_DEPEND(ipw, firmware, 1, 1, 1);
98 
99 struct ipw_ident {
100 	uint16_t	vendor;
101 	uint16_t	device;
102 	const char	*name;
103 };
104 
105 static const struct ipw_ident ipw_ident_table[] = {
106 	{ 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
107 
108 	{ 0, 0, NULL }
109 };
110 
111 static struct ieee80211vap *ipw_vap_create(struct ieee80211com *,
112 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
113 		    const uint8_t [IEEE80211_ADDR_LEN],
114 		    const uint8_t [IEEE80211_ADDR_LEN]);
115 static void	ipw_vap_delete(struct ieee80211vap *);
116 static int	ipw_dma_alloc(struct ipw_softc *);
117 static void	ipw_release(struct ipw_softc *);
118 static void	ipw_media_status(struct ifnet *, struct ifmediareq *);
119 static int	ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
120 static uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
121 static void	ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
122 static void	ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
123 static void	ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
124 		    struct ipw_soft_bd *, struct ipw_soft_buf *);
125 static void	ipw_rx_intr(struct ipw_softc *);
126 static void	ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
127 static void	ipw_tx_intr(struct ipw_softc *);
128 static void	ipw_intr(void *);
129 static void	ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
130 static const char * ipw_cmdname(int);
131 static int	ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
132 static int	ipw_tx_start(struct ifnet *, struct mbuf *,
133 		    struct ieee80211_node *);
134 static int	ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
135 		    const struct ieee80211_bpf_params *);
136 static void	ipw_start(struct ifnet *);
137 static void	ipw_start_locked(struct ifnet *);
138 static void	ipw_watchdog(void *);
139 static int	ipw_ioctl(struct ifnet *, u_long, caddr_t);
140 static void	ipw_stop_master(struct ipw_softc *);
141 static int	ipw_enable(struct ipw_softc *);
142 static int	ipw_disable(struct ipw_softc *);
143 static int	ipw_reset(struct ipw_softc *);
144 static int	ipw_load_ucode(struct ipw_softc *, const char *, int);
145 static int	ipw_load_firmware(struct ipw_softc *, const char *, int);
146 static int	ipw_config(struct ipw_softc *);
147 static void	ipw_assoc(struct ieee80211com *, struct ieee80211vap *);
148 static void	ipw_disassoc(struct ieee80211com *, struct ieee80211vap *);
149 static void	ipw_init_task(void *, int);
150 static void	ipw_init(void *);
151 static void	ipw_init_locked(struct ipw_softc *);
152 static void	ipw_stop(void *);
153 static void	ipw_stop_locked(struct ipw_softc *);
154 static int	ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
155 static int	ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
156 static uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
157 static void	ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
158 #if 0
159 static int	ipw_read_table2(struct ipw_softc *, uint32_t, void *,
160 		    uint32_t *);
161 static void	ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
162 		    bus_size_t);
163 #endif
164 static void	ipw_write_mem_1(struct ipw_softc *, bus_size_t,
165 		    const uint8_t *, bus_size_t);
166 static int	ipw_scan(struct ipw_softc *);
167 static void	ipw_scan_start(struct ieee80211com *);
168 static void	ipw_scan_end(struct ieee80211com *);
169 static void	ipw_set_channel(struct ieee80211com *);
170 static void	ipw_scan_curchan(struct ieee80211_scan_state *,
171 		    unsigned long maxdwell);
172 static void	ipw_scan_mindwell(struct ieee80211_scan_state *);
173 
174 static int ipw_probe(device_t);
175 static int ipw_attach(device_t);
176 static int ipw_detach(device_t);
177 static int ipw_shutdown(device_t);
178 static int ipw_suspend(device_t);
179 static int ipw_resume(device_t);
180 
181 static device_method_t ipw_methods[] = {
182 	/* Device interface */
183 	DEVMETHOD(device_probe,		ipw_probe),
184 	DEVMETHOD(device_attach,	ipw_attach),
185 	DEVMETHOD(device_detach,	ipw_detach),
186 	DEVMETHOD(device_shutdown,	ipw_shutdown),
187 	DEVMETHOD(device_suspend,	ipw_suspend),
188 	DEVMETHOD(device_resume,	ipw_resume),
189 
190 	{ 0, 0 }
191 };
192 
193 static driver_t ipw_driver = {
194 	"ipw",
195 	ipw_methods,
196 	sizeof (struct ipw_softc)
197 };
198 
199 static devclass_t ipw_devclass;
200 
201 DRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0);
202 
203 MODULE_VERSION(ipw, 1);
204 
205 static int
206 ipw_probe(device_t dev)
207 {
208 	const struct ipw_ident *ident;
209 
210 	for (ident = ipw_ident_table; ident->name != NULL; ident++) {
211 		if (pci_get_vendor(dev) == ident->vendor &&
212 		    pci_get_device(dev) == ident->device) {
213 			device_set_desc(dev, ident->name);
214 			return 0;
215 		}
216 	}
217 	return ENXIO;
218 }
219 
220 /* Base Address Register */
221 #define IPW_PCI_BAR0	0x10
222 
223 static int
224 ipw_attach(device_t dev)
225 {
226 	struct ipw_softc *sc = device_get_softc(dev);
227 	struct ifnet *ifp;
228 	struct ieee80211com *ic;
229 	struct ieee80211_channel *c;
230 	uint16_t val;
231 	int error, i;
232 	uint8_t macaddr[IEEE80211_ADDR_LEN];
233 
234 	sc->sc_dev = dev;
235 
236 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
237 	    MTX_DEF | MTX_RECURSE);
238 
239 	TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
240 	callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
241 
242 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
243 		device_printf(dev, "chip is in D%d power mode "
244 		    "-- setting to D0\n", pci_get_powerstate(dev));
245 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
246 	}
247 
248 	pci_write_config(dev, 0x41, 0, 1);
249 
250 	/* enable bus-mastering */
251 	pci_enable_busmaster(dev);
252 
253 	sc->mem_rid = IPW_PCI_BAR0;
254 	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
255 	    RF_ACTIVE);
256 	if (sc->mem == NULL) {
257 		device_printf(dev, "could not allocate memory resource\n");
258 		goto fail;
259 	}
260 
261 	sc->sc_st = rman_get_bustag(sc->mem);
262 	sc->sc_sh = rman_get_bushandle(sc->mem);
263 
264 	sc->irq_rid = 0;
265 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
266 	    RF_ACTIVE | RF_SHAREABLE);
267 	if (sc->irq == NULL) {
268 		device_printf(dev, "could not allocate interrupt resource\n");
269 		goto fail1;
270 	}
271 
272 	if (ipw_reset(sc) != 0) {
273 		device_printf(dev, "could not reset adapter\n");
274 		goto fail2;
275 	}
276 
277 	if (ipw_dma_alloc(sc) != 0) {
278 		device_printf(dev, "could not allocate DMA resources\n");
279 		goto fail2;
280 	}
281 
282 	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
283 	if (ifp == NULL) {
284 		device_printf(dev, "can not if_alloc()\n");
285 		goto fail3;
286 	}
287 	ic = ifp->if_l2com;
288 
289 	ifp->if_softc = sc;
290 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
291 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
292 	ifp->if_init = ipw_init;
293 	ifp->if_ioctl = ipw_ioctl;
294 	ifp->if_start = ipw_start;
295 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
296 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
297 	IFQ_SET_READY(&ifp->if_snd);
298 
299 	ic->ic_ifp = ifp;
300 	ic->ic_opmode = IEEE80211_M_STA;
301 	ic->ic_phytype = IEEE80211_T_DS;
302 
303 	/* set device capabilities */
304 	ic->ic_caps =
305 		  IEEE80211_C_STA		/* station mode supported */
306 		| IEEE80211_C_IBSS		/* IBSS mode supported */
307 		| IEEE80211_C_MONITOR		/* monitor mode supported */
308 		| IEEE80211_C_PMGT		/* power save supported */
309 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
310 		| IEEE80211_C_WPA		/* 802.11i supported */
311 		;
312 
313 	/* read MAC address from EEPROM */
314 	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
315 	macaddr[0] = val >> 8;
316 	macaddr[1] = val & 0xff;
317 	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
318 	macaddr[2] = val >> 8;
319 	macaddr[3] = val & 0xff;
320 	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
321 	macaddr[4] = val >> 8;
322 	macaddr[5] = val & 0xff;
323 
324 	/* set supported .11b channels (read from EEPROM) */
325 	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
326 		val = 0x7ff; /* default to channels 1-11 */
327 	val <<= 1;
328 	for (i = 1; i < 16; i++) {
329 		if (val & (1 << i)) {
330 			c = &ic->ic_channels[ic->ic_nchans++];
331 			c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
332 			c->ic_flags = IEEE80211_CHAN_B;
333 			c->ic_ieee = i;
334 		}
335 	}
336 
337 	/* check support for radio transmitter switch in EEPROM */
338 	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
339 		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
340 
341 	ieee80211_ifattach(ic, macaddr);
342 	ic->ic_scan_start = ipw_scan_start;
343 	ic->ic_scan_end = ipw_scan_end;
344 	ic->ic_set_channel = ipw_set_channel;
345 	ic->ic_scan_curchan = ipw_scan_curchan;
346 	ic->ic_scan_mindwell = ipw_scan_mindwell;
347 	ic->ic_raw_xmit = ipw_raw_xmit;
348 
349 	ic->ic_vap_create = ipw_vap_create;
350 	ic->ic_vap_delete = ipw_vap_delete;
351 
352 	ieee80211_radiotap_attach(ic,
353 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
354 		IPW_TX_RADIOTAP_PRESENT,
355 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
356 		IPW_RX_RADIOTAP_PRESENT);
357 
358 	/*
359 	 * Add a few sysctl knobs.
360 	 */
361 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
362 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
363 	    CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
364 	    "radio transmitter switch state (0=off, 1=on)");
365 
366 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
367 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
368 	    CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
369 	    "statistics");
370 
371 	/*
372 	 * Hook our interrupt after all initialization is complete.
373 	 */
374 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
375 	    NULL, ipw_intr, sc, &sc->sc_ih);
376 	if (error != 0) {
377 		device_printf(dev, "could not set up interrupt\n");
378 		goto fail4;
379 	}
380 
381 	if (bootverbose)
382 		ieee80211_announce(ic);
383 
384 	return 0;
385 fail4:
386 	if_free(ifp);
387 fail3:
388 	ipw_release(sc);
389 fail2:
390 	bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
391 fail1:
392 	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
393 fail:
394 	mtx_destroy(&sc->sc_mtx);
395 	return ENXIO;
396 }
397 
398 static int
399 ipw_detach(device_t dev)
400 {
401 	struct ipw_softc *sc = device_get_softc(dev);
402 	struct ifnet *ifp = sc->sc_ifp;
403 	struct ieee80211com *ic = ifp->if_l2com;
404 
405 	ieee80211_draintask(ic, &sc->sc_init_task);
406 	ipw_stop(sc);
407 
408 	ieee80211_ifdetach(ic);
409 
410 	callout_drain(&sc->sc_wdtimer);
411 
412 	ipw_release(sc);
413 
414 	bus_teardown_intr(dev, sc->irq, sc->sc_ih);
415 	bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
416 
417 	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
418 
419 	if_free(ifp);
420 
421 	if (sc->sc_firmware != NULL) {
422 		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
423 		sc->sc_firmware = NULL;
424 	}
425 
426 	mtx_destroy(&sc->sc_mtx);
427 
428 	return 0;
429 }
430 
431 static struct ieee80211vap *
432 ipw_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
433     enum ieee80211_opmode opmode, int flags,
434     const uint8_t bssid[IEEE80211_ADDR_LEN],
435     const uint8_t mac[IEEE80211_ADDR_LEN])
436 {
437 	struct ifnet *ifp = ic->ic_ifp;
438 	struct ipw_softc *sc = ifp->if_softc;
439 	struct ipw_vap *ivp;
440 	struct ieee80211vap *vap;
441 	const struct firmware *fp;
442 	const struct ipw_firmware_hdr *hdr;
443 	const char *imagename;
444 
445 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
446 		return NULL;
447 
448 	switch (opmode) {
449 	case IEEE80211_M_STA:
450 		imagename = "ipw_bss";
451 		break;
452 	case IEEE80211_M_IBSS:
453 		imagename = "ipw_ibss";
454 		break;
455 	case IEEE80211_M_MONITOR:
456 		imagename = "ipw_monitor";
457 		break;
458 	default:
459 		return NULL;
460 	}
461 
462 	/*
463 	 * Load firmware image using the firmware(9) subsystem.  Doing
464 	 * this unlocked is ok since we're single-threaded by the
465 	 * 802.11 layer.
466 	 */
467 	if (sc->sc_firmware == NULL ||
468 	    strcmp(sc->sc_firmware->name, imagename) != 0) {
469 		if (sc->sc_firmware != NULL)
470 			firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
471 		sc->sc_firmware = firmware_get(imagename);
472 	}
473 	if (sc->sc_firmware == NULL) {
474 		device_printf(sc->sc_dev,
475 		    "could not load firmware image '%s'\n", imagename);
476 		return NULL;
477 	}
478 	fp = sc->sc_firmware;
479 	if (fp->datasize < sizeof *hdr) {
480 		device_printf(sc->sc_dev,
481 		    "firmware image too short %zu\n", fp->datasize);
482 		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
483 		sc->sc_firmware = NULL;
484 		return NULL;
485 	}
486 	hdr = (const struct ipw_firmware_hdr *)fp->data;
487 	if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
488 	    le32toh(hdr->ucodesz)) {
489 		device_printf(sc->sc_dev,
490 		    "firmware image too short %zu\n", fp->datasize);
491 		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
492 		sc->sc_firmware = NULL;
493 		return NULL;
494 	}
495 
496 	ivp = (struct ipw_vap *) malloc(sizeof(struct ipw_vap),
497 	    M_80211_VAP, M_NOWAIT | M_ZERO);
498 	if (ivp == NULL)
499 		return NULL;
500 	vap = &ivp->vap;
501 
502 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
503 	/* override with driver methods */
504 	ivp->newstate = vap->iv_newstate;
505 	vap->iv_newstate = ipw_newstate;
506 
507 	/* complete setup */
508 	ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status);
509 	ic->ic_opmode = opmode;
510 	return vap;
511 }
512 
513 static void
514 ipw_vap_delete(struct ieee80211vap *vap)
515 {
516 	struct ipw_vap *ivp = IPW_VAP(vap);
517 
518 	ieee80211_vap_detach(vap);
519 	free(ivp, M_80211_VAP);
520 }
521 
522 static int
523 ipw_dma_alloc(struct ipw_softc *sc)
524 {
525 	struct ipw_soft_bd *sbd;
526 	struct ipw_soft_hdr *shdr;
527 	struct ipw_soft_buf *sbuf;
528 	bus_addr_t physaddr;
529 	int error, i;
530 
531 	/*
532 	 * Allocate parent DMA tag for subsequent allocations.
533 	 */
534 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
535 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
536 	    BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED,
537 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &sc->parent_dmat);
538 	if (error != 0) {
539 		device_printf(sc->sc_dev, "could not create parent DMA tag\n");
540 		goto fail;
541 	}
542 
543 	/*
544 	 * Allocate and map tx ring.
545 	 */
546 	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
547 	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
548 	    NULL, &sc->tbd_dmat);
549 	if (error != 0) {
550 		device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
551 		goto fail;
552 	}
553 
554 	error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
555 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
556 	if (error != 0) {
557 		device_printf(sc->sc_dev,
558 		    "could not allocate tx ring DMA memory\n");
559 		goto fail;
560 	}
561 
562 	error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
563 	    IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
564 	if (error != 0) {
565 		device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
566 		goto fail;
567 	}
568 
569 	/*
570 	 * Allocate and map rx ring.
571 	 */
572 	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
573 	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
574 	    NULL, &sc->rbd_dmat);
575 	if (error != 0) {
576 		device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
577 		goto fail;
578 	}
579 
580 	error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
581 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
582 	if (error != 0) {
583 		device_printf(sc->sc_dev,
584 		    "could not allocate rx ring DMA memory\n");
585 		goto fail;
586 	}
587 
588 	error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
589 	    IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
590 	if (error != 0) {
591 		device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
592 		goto fail;
593 	}
594 
595 	/*
596 	 * Allocate and map status ring.
597 	 */
598 	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
599 	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
600 	    NULL, NULL, &sc->status_dmat);
601 	if (error != 0) {
602 		device_printf(sc->sc_dev,
603 		    "could not create status ring DMA tag\n");
604 		goto fail;
605 	}
606 
607 	error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
608 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
609 	if (error != 0) {
610 		device_printf(sc->sc_dev,
611 		    "could not allocate status ring DMA memory\n");
612 		goto fail;
613 	}
614 
615 	error = bus_dmamap_load(sc->status_dmat, sc->status_map,
616 	    sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
617 	    0);
618 	if (error != 0) {
619 		device_printf(sc->sc_dev,
620 		    "could not map status ring DMA memory\n");
621 		goto fail;
622 	}
623 
624 	/*
625 	 * Allocate command DMA map.
626 	 */
627 	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
628 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
629 	    sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
630 	if (error != 0) {
631 		device_printf(sc->sc_dev, "could not create command DMA tag\n");
632 		goto fail;
633 	}
634 
635 	error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
636 	if (error != 0) {
637 		device_printf(sc->sc_dev,
638 		    "could not create command DMA map\n");
639 		goto fail;
640 	}
641 
642 	/*
643 	 * Allocate headers DMA maps.
644 	 */
645 	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
646 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
647 	    sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
648 	if (error != 0) {
649 		device_printf(sc->sc_dev, "could not create header DMA tag\n");
650 		goto fail;
651 	}
652 
653 	SLIST_INIT(&sc->free_shdr);
654 	for (i = 0; i < IPW_NDATA; i++) {
655 		shdr = &sc->shdr_list[i];
656 		error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
657 		if (error != 0) {
658 			device_printf(sc->sc_dev,
659 			    "could not create header DMA map\n");
660 			goto fail;
661 		}
662 		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
663 	}
664 
665 	/*
666 	 * Allocate tx buffers DMA maps.
667 	 */
668 	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
669 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
670 	    NULL, NULL, &sc->txbuf_dmat);
671 	if (error != 0) {
672 		device_printf(sc->sc_dev, "could not create tx DMA tag\n");
673 		goto fail;
674 	}
675 
676 	SLIST_INIT(&sc->free_sbuf);
677 	for (i = 0; i < IPW_NDATA; i++) {
678 		sbuf = &sc->tx_sbuf_list[i];
679 		error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
680 		if (error != 0) {
681 			device_printf(sc->sc_dev,
682 			    "could not create tx DMA map\n");
683 			goto fail;
684 		}
685 		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
686 	}
687 
688 	/*
689 	 * Initialize tx ring.
690 	 */
691 	for (i = 0; i < IPW_NTBD; i++) {
692 		sbd = &sc->stbd_list[i];
693 		sbd->bd = &sc->tbd_list[i];
694 		sbd->type = IPW_SBD_TYPE_NOASSOC;
695 	}
696 
697 	/*
698 	 * Pre-allocate rx buffers and DMA maps.
699 	 */
700 	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
701 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
702 	    NULL, &sc->rxbuf_dmat);
703 	if (error != 0) {
704 		device_printf(sc->sc_dev, "could not create rx DMA tag\n");
705 		goto fail;
706 	}
707 
708 	for (i = 0; i < IPW_NRBD; i++) {
709 		sbd = &sc->srbd_list[i];
710 		sbuf = &sc->rx_sbuf_list[i];
711 		sbd->bd = &sc->rbd_list[i];
712 
713 		sbuf->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
714 		if (sbuf->m == NULL) {
715 			device_printf(sc->sc_dev,
716 			    "could not allocate rx mbuf\n");
717 			error = ENOMEM;
718 			goto fail;
719 		}
720 
721 		error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
722 		if (error != 0) {
723 			device_printf(sc->sc_dev,
724 			    "could not create rx DMA map\n");
725 			goto fail;
726 		}
727 
728 		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
729 		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
730 		    &physaddr, 0);
731 		if (error != 0) {
732 			device_printf(sc->sc_dev,
733 			    "could not map rx DMA memory\n");
734 			goto fail;
735 		}
736 
737 		sbd->type = IPW_SBD_TYPE_DATA;
738 		sbd->priv = sbuf;
739 		sbd->bd->physaddr = htole32(physaddr);
740 		sbd->bd->len = htole32(MCLBYTES);
741 	}
742 
743 	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
744 
745 	return 0;
746 
747 fail:	ipw_release(sc);
748 	return error;
749 }
750 
751 static void
752 ipw_release(struct ipw_softc *sc)
753 {
754 	struct ipw_soft_buf *sbuf;
755 	int i;
756 
757 	if (sc->parent_dmat != NULL) {
758 		bus_dma_tag_destroy(sc->parent_dmat);
759 	}
760 
761 	if (sc->tbd_dmat != NULL) {
762 		if (sc->stbd_list != NULL) {
763 			bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
764 			bus_dmamem_free(sc->tbd_dmat, sc->tbd_list,
765 			    sc->tbd_map);
766 		}
767 		bus_dma_tag_destroy(sc->tbd_dmat);
768 	}
769 
770 	if (sc->rbd_dmat != NULL) {
771 		if (sc->rbd_list != NULL) {
772 			bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
773 			bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
774 			    sc->rbd_map);
775 		}
776 		bus_dma_tag_destroy(sc->rbd_dmat);
777 	}
778 
779 	if (sc->status_dmat != NULL) {
780 		if (sc->status_list != NULL) {
781 			bus_dmamap_unload(sc->status_dmat, sc->status_map);
782 			bus_dmamem_free(sc->status_dmat, sc->status_list,
783 			    sc->status_map);
784 		}
785 		bus_dma_tag_destroy(sc->status_dmat);
786 	}
787 
788 	for (i = 0; i < IPW_NTBD; i++)
789 		ipw_release_sbd(sc, &sc->stbd_list[i]);
790 
791 	if (sc->cmd_dmat != NULL) {
792 		bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
793 		bus_dma_tag_destroy(sc->cmd_dmat);
794 	}
795 
796 	if (sc->hdr_dmat != NULL) {
797 		for (i = 0; i < IPW_NDATA; i++)
798 			bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
799 		bus_dma_tag_destroy(sc->hdr_dmat);
800 	}
801 
802 	if (sc->txbuf_dmat != NULL) {
803 		for (i = 0; i < IPW_NDATA; i++) {
804 			bus_dmamap_destroy(sc->txbuf_dmat,
805 			    sc->tx_sbuf_list[i].map);
806 		}
807 		bus_dma_tag_destroy(sc->txbuf_dmat);
808 	}
809 
810 	if (sc->rxbuf_dmat != NULL) {
811 		for (i = 0; i < IPW_NRBD; i++) {
812 			sbuf = &sc->rx_sbuf_list[i];
813 			if (sbuf->m != NULL) {
814 				bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
815 				    BUS_DMASYNC_POSTREAD);
816 				bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
817 				m_freem(sbuf->m);
818 			}
819 			bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
820 		}
821 		bus_dma_tag_destroy(sc->rxbuf_dmat);
822 	}
823 }
824 
825 static int
826 ipw_shutdown(device_t dev)
827 {
828 	struct ipw_softc *sc = device_get_softc(dev);
829 
830 	ipw_stop(sc);
831 
832 	return 0;
833 }
834 
835 static int
836 ipw_suspend(device_t dev)
837 {
838 	struct ipw_softc *sc = device_get_softc(dev);
839 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
840 
841 	ieee80211_suspend_all(ic);
842 	return 0;
843 }
844 
845 static int
846 ipw_resume(device_t dev)
847 {
848 	struct ipw_softc *sc = device_get_softc(dev);
849 	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
850 
851 	pci_write_config(dev, 0x41, 0, 1);
852 
853 	ieee80211_resume_all(ic);
854 	return 0;
855 }
856 
857 static int
858 ipw_cvtrate(int ipwrate)
859 {
860 	switch (ipwrate) {
861 	case IPW_RATE_DS1:	return 2;
862 	case IPW_RATE_DS2:	return 4;
863 	case IPW_RATE_DS5:	return 11;
864 	case IPW_RATE_DS11:	return 22;
865 	}
866 	return 0;
867 }
868 
869 /*
870  * The firmware automatically adapts the transmit speed. We report its current
871  * value here.
872  */
873 static void
874 ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
875 {
876 	struct ieee80211vap *vap = ifp->if_softc;
877 	struct ieee80211com *ic = vap->iv_ic;
878 	struct ipw_softc *sc = ic->ic_ifp->if_softc;
879 
880 	/* read current transmission rate from adapter */
881 	vap->iv_bss->ni_txrate = ipw_cvtrate(
882 	    ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
883 	ieee80211_media_status(ifp, imr);
884 }
885 
886 static int
887 ipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
888 {
889 	struct ipw_vap *ivp = IPW_VAP(vap);
890 	struct ieee80211com *ic = vap->iv_ic;
891 	struct ifnet *ifp = ic->ic_ifp;
892 	struct ipw_softc *sc = ifp->if_softc;
893 	enum ieee80211_state ostate;
894 
895 	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
896 		ieee80211_state_name[vap->iv_state],
897 		ieee80211_state_name[nstate], sc->flags));
898 
899 	ostate = vap->iv_state;
900 	IEEE80211_UNLOCK(ic);
901 
902 	switch (nstate) {
903 	case IEEE80211_S_RUN:
904 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
905 			/*
906 			 * XXX when joining an ibss network we are called
907 			 * with a SCAN -> RUN transition on scan complete.
908 			 * Use that to call ipw_assoc.  On completing the
909 			 * join we are then called again with an AUTH -> RUN
910 			 * transition and we want to do nothing.  This is
911 			 * all totally bogus and needs to be redone.
912 			 */
913 			if (ostate == IEEE80211_S_SCAN)
914 				ipw_assoc(ic, vap);
915 		}
916 		break;
917 
918 	case IEEE80211_S_INIT:
919 		if (sc->flags & IPW_FLAG_ASSOCIATED)
920 			ipw_disassoc(ic, vap);
921 		break;
922 
923 	case IEEE80211_S_AUTH:
924 		/*
925 		 * Move to ASSOC state after the ipw_assoc() call.  Firmware
926 		 * takes care of authentication, after the call we'll receive
927 		 * only an assoc response which would otherwise be discared
928 		 * if we are still in AUTH state.
929 		 */
930 		nstate = IEEE80211_S_ASSOC;
931 		ipw_assoc(ic, vap);
932 		break;
933 
934 	case IEEE80211_S_ASSOC:
935 		/*
936 		 * If we are not transitioning from AUTH then resend the
937 		 * association request.
938 		 */
939 		if (ostate != IEEE80211_S_AUTH)
940 			ipw_assoc(ic, vap);
941 		break;
942 
943 	default:
944 		break;
945 	}
946 	IEEE80211_LOCK(ic);
947 	return ivp->newstate(vap, nstate, arg);
948 }
949 
950 /*
951  * Read 16 bits at address 'addr' from the serial EEPROM.
952  */
953 static uint16_t
954 ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
955 {
956 	uint32_t tmp;
957 	uint16_t val;
958 	int n;
959 
960 	/* clock C once before the first command */
961 	IPW_EEPROM_CTL(sc, 0);
962 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
963 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
964 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
965 
966 	/* write start bit (1) */
967 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
968 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
969 
970 	/* write READ opcode (10) */
971 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
972 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
973 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
974 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
975 
976 	/* write address A7-A0 */
977 	for (n = 7; n >= 0; n--) {
978 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
979 		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
980 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
981 		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
982 	}
983 
984 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
985 
986 	/* read data Q15-Q0 */
987 	val = 0;
988 	for (n = 15; n >= 0; n--) {
989 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
990 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
991 		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
992 		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
993 	}
994 
995 	IPW_EEPROM_CTL(sc, 0);
996 
997 	/* clear Chip Select and clock C */
998 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
999 	IPW_EEPROM_CTL(sc, 0);
1000 	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
1001 
1002 	return le16toh(val);
1003 }
1004 
1005 static void
1006 ipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1007 {
1008 	struct ipw_cmd *cmd;
1009 
1010 	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1011 
1012 	cmd = mtod(sbuf->m, struct ipw_cmd *);
1013 
1014 	DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
1015 	    ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
1016 	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
1017 	    le32toh(cmd->status)));
1018 
1019 	sc->flags &= ~IPW_FLAG_BUSY;
1020 	wakeup(sc);
1021 }
1022 
1023 static void
1024 ipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1025 {
1026 #define	IEEESTATE(vap)	ieee80211_state_name[vap->iv_state]
1027 	struct ifnet *ifp = sc->sc_ifp;
1028 	struct ieee80211com *ic = ifp->if_l2com;
1029 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1030 	uint32_t state;
1031 
1032 	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1033 
1034 	state = le32toh(*mtod(sbuf->m, uint32_t *));
1035 
1036 	switch (state) {
1037 	case IPW_STATE_ASSOCIATED:
1038 		DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1039 			IEEESTATE(vap), sc->flags));
1040 		/* XXX suppress state change in case the fw auto-associates */
1041 		if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1042 			DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1043 				IEEESTATE(vap), sc->flags));
1044 			break;
1045 		}
1046 		sc->flags &= ~IPW_FLAG_ASSOCIATING;
1047 		sc->flags |= IPW_FLAG_ASSOCIATED;
1048 		break;
1049 
1050 	case IPW_STATE_SCANNING:
1051 		DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1052 			IEEESTATE(vap), sc->flags));
1053 		/*
1054 		 * NB: Check driver state for association on assoc
1055 		 * loss as the firmware will immediately start to
1056 		 * scan and we would treat it as a beacon miss if
1057 		 * we checked the 802.11 layer state.
1058 		 */
1059 		if (sc->flags & IPW_FLAG_ASSOCIATED) {
1060 			IPW_UNLOCK(sc);
1061 			/* XXX probably need to issue disassoc to fw */
1062 			ieee80211_beacon_miss(ic);
1063 			IPW_LOCK(sc);
1064 		}
1065 		break;
1066 
1067 	case IPW_STATE_SCAN_COMPLETE:
1068 		/*
1069 		 * XXX For some reason scan requests generate scan
1070 		 * started + scan done events before any traffic is
1071 		 * received (e.g. probe response frames).  We work
1072 		 * around this by marking the HACK flag and skipping
1073 		 * the first scan complete event.
1074 		*/
1075 		DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1076 			    IEEESTATE(vap), sc->flags));
1077 		if (sc->flags & IPW_FLAG_HACK) {
1078 			sc->flags &= ~IPW_FLAG_HACK;
1079 			break;
1080 		}
1081 		if (sc->flags & IPW_FLAG_SCANNING) {
1082 			IPW_UNLOCK(sc);
1083 			ieee80211_scan_done(vap);
1084 			IPW_LOCK(sc);
1085 			sc->flags &= ~IPW_FLAG_SCANNING;
1086 			sc->sc_scan_timer = 0;
1087 		}
1088 		break;
1089 
1090 	case IPW_STATE_ASSOCIATION_LOST:
1091 		DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1092 			IEEESTATE(vap), sc->flags));
1093 		sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1094 		if (vap->iv_state == IEEE80211_S_RUN) {
1095 			IPW_UNLOCK(sc);
1096 			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1097 			IPW_LOCK(sc);
1098 		}
1099 		break;
1100 
1101 	case IPW_STATE_DISABLED:
1102 		/* XXX? is this right? */
1103 		sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1104 		    IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1105 		DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1106 			IEEESTATE(vap), sc->flags));
1107 		break;
1108 
1109 	case IPW_STATE_RADIO_DISABLED:
1110 		device_printf(sc->sc_dev, "radio turned off\n");
1111 		ieee80211_notify_radio(ic, 0);
1112 		ipw_stop_locked(sc);
1113 		/* XXX start polling thread to detect radio on */
1114 		break;
1115 
1116 	default:
1117 		DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1118 			__func__, state, IEEESTATE(vap), sc->flags));
1119 		break;
1120 	}
1121 #undef IEEESTATE
1122 }
1123 
1124 /*
1125  * Set driver state for current channel.
1126  */
1127 static void
1128 ipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1129 {
1130 	struct ifnet *ifp = sc->sc_ifp;
1131 	struct ieee80211com *ic = ifp->if_l2com;
1132 
1133 	ic->ic_curchan = chan;
1134 	ieee80211_radiotap_chan_change(ic);
1135 }
1136 
1137 /*
1138  * XXX: Hack to set the current channel to the value advertised in beacons or
1139  * probe responses. Only used during AP detection.
1140  */
1141 static void
1142 ipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1143 {
1144 	struct ifnet *ifp = sc->sc_ifp;
1145 	struct ieee80211com *ic = ifp->if_l2com;
1146 	struct ieee80211_channel *c;
1147 	struct ieee80211_frame *wh;
1148 	uint8_t subtype;
1149 	uint8_t *frm, *efrm;
1150 
1151 	wh = mtod(m, struct ieee80211_frame *);
1152 
1153 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1154 		return;
1155 
1156 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1157 
1158 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1159 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1160 		return;
1161 
1162 	/* XXX use ieee80211_parse_beacon */
1163 	frm = (uint8_t *)(wh + 1);
1164 	efrm = mtod(m, uint8_t *) + m->m_len;
1165 
1166 	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1167 	while (frm < efrm) {
1168 		if (*frm == IEEE80211_ELEMID_DSPARMS)
1169 #if IEEE80211_CHAN_MAX < 255
1170 		if (frm[2] <= IEEE80211_CHAN_MAX)
1171 #endif
1172 		{
1173 			DPRINTF(("Fixing channel to %d\n", frm[2]));
1174 			c = ieee80211_find_channel(ic,
1175 				ieee80211_ieee2mhz(frm[2], 0),
1176 				IEEE80211_CHAN_B);
1177 			if (c == NULL)
1178 				c = &ic->ic_channels[0];
1179 			ipw_setcurchan(sc, c);
1180 		}
1181 
1182 		frm += frm[1] + 2;
1183 	}
1184 }
1185 
1186 static void
1187 ipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1188     struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1189 {
1190 	struct ifnet *ifp = sc->sc_ifp;
1191 	struct ieee80211com *ic = ifp->if_l2com;
1192 	struct mbuf *mnew, *m;
1193 	struct ieee80211_node *ni;
1194 	bus_addr_t physaddr;
1195 	int error;
1196 	int8_t rssi, nf;
1197 
1198 	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1199 	    status->rssi));
1200 
1201 	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1202 	    le32toh(status->len) > MCLBYTES)
1203 		return;
1204 
1205 	/*
1206 	 * Try to allocate a new mbuf for this ring element and load it before
1207 	 * processing the current mbuf. If the ring element cannot be loaded,
1208 	 * drop the received packet and reuse the old mbuf. In the unlikely
1209 	 * case that the old mbuf can't be reloaded either, explicitly panic.
1210 	 */
1211 	mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1212 	if (mnew == NULL) {
1213 		ifp->if_ierrors++;
1214 		return;
1215 	}
1216 
1217 	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1218 	bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1219 
1220 	error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1221 	    MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1222 	if (error != 0) {
1223 		m_freem(mnew);
1224 
1225 		/* try to reload the old mbuf */
1226 		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1227 		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1228 		    &physaddr, 0);
1229 		if (error != 0) {
1230 			/* very unlikely that it will fail... */
1231 			panic("%s: could not load old rx mbuf",
1232 			    device_get_name(sc->sc_dev));
1233 		}
1234 		ifp->if_ierrors++;
1235 		return;
1236 	}
1237 
1238 	/*
1239 	 * New mbuf successfully loaded, update Rx ring and continue
1240 	 * processing.
1241 	 */
1242 	m = sbuf->m;
1243 	sbuf->m = mnew;
1244 	sbd->bd->physaddr = htole32(physaddr);
1245 
1246 	/* finalize mbuf */
1247 	m->m_pkthdr.rcvif = ifp;
1248 	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1249 
1250 	rssi = status->rssi + IPW_RSSI_TO_DBM;
1251 	nf = -95;
1252 	if (ieee80211_radiotap_active(ic)) {
1253 		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1254 
1255 		tap->wr_flags = 0;
1256 		tap->wr_antsignal = rssi;
1257 		tap->wr_antnoise = nf;
1258 	}
1259 
1260 	if (sc->flags & IPW_FLAG_SCANNING)
1261 		ipw_fix_channel(sc, m);
1262 
1263 	IPW_UNLOCK(sc);
1264 	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1265 	if (ni != NULL) {
1266 		(void) ieee80211_input(ni, m, rssi - nf, nf);
1267 		ieee80211_free_node(ni);
1268 	} else
1269 		(void) ieee80211_input_all(ic, m, rssi - nf, nf);
1270 	IPW_LOCK(sc);
1271 
1272 	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1273 }
1274 
1275 static void
1276 ipw_rx_intr(struct ipw_softc *sc)
1277 {
1278 	struct ipw_status *status;
1279 	struct ipw_soft_bd *sbd;
1280 	struct ipw_soft_buf *sbuf;
1281 	uint32_t r, i;
1282 
1283 	if (!(sc->flags & IPW_FLAG_FW_INITED))
1284 		return;
1285 
1286 	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1287 
1288 	bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1289 
1290 	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1291 		status = &sc->status_list[i];
1292 		sbd = &sc->srbd_list[i];
1293 		sbuf = sbd->priv;
1294 
1295 		switch (le16toh(status->code) & 0xf) {
1296 		case IPW_STATUS_CODE_COMMAND:
1297 			ipw_rx_cmd_intr(sc, sbuf);
1298 			break;
1299 
1300 		case IPW_STATUS_CODE_NEWSTATE:
1301 			ipw_rx_newstate_intr(sc, sbuf);
1302 			break;
1303 
1304 		case IPW_STATUS_CODE_DATA_802_3:
1305 		case IPW_STATUS_CODE_DATA_802_11:
1306 			ipw_rx_data_intr(sc, status, sbd, sbuf);
1307 			break;
1308 
1309 		case IPW_STATUS_CODE_NOTIFICATION:
1310 			DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1311 			    le32toh(status->len), status->flags));
1312 			/* XXX maybe drive state machine AUTH->ASSOC? */
1313 			break;
1314 
1315 		default:
1316 			device_printf(sc->sc_dev, "unexpected status code %u\n",
1317 			    le16toh(status->code));
1318 		}
1319 
1320 		/* firmware was killed, stop processing received frames */
1321 		if (!(sc->flags & IPW_FLAG_FW_INITED))
1322 			return;
1323 
1324 		sbd->bd->flags = 0;
1325 	}
1326 
1327 	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1328 
1329 	/* kick the firmware */
1330 	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1331 	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1332 }
1333 
1334 static void
1335 ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1336 {
1337 	struct ipw_soft_hdr *shdr;
1338 	struct ipw_soft_buf *sbuf;
1339 
1340 	switch (sbd->type) {
1341 	case IPW_SBD_TYPE_COMMAND:
1342 		bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1343 		    BUS_DMASYNC_POSTWRITE);
1344 		bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1345 		break;
1346 
1347 	case IPW_SBD_TYPE_HEADER:
1348 		shdr = sbd->priv;
1349 		bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1350 		bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1351 		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1352 		break;
1353 
1354 	case IPW_SBD_TYPE_DATA:
1355 		sbuf = sbd->priv;
1356 		bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1357 		    BUS_DMASYNC_POSTWRITE);
1358 		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1359 		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1360 
1361 		if (sbuf->m->m_flags & M_TXCB)
1362 			ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1363 		m_freem(sbuf->m);
1364 		ieee80211_free_node(sbuf->ni);
1365 
1366 		sc->sc_tx_timer = 0;
1367 		break;
1368 	}
1369 
1370 	sbd->type = IPW_SBD_TYPE_NOASSOC;
1371 }
1372 
1373 static void
1374 ipw_tx_intr(struct ipw_softc *sc)
1375 {
1376 	struct ifnet *ifp = sc->sc_ifp;
1377 	struct ipw_soft_bd *sbd;
1378 	uint32_t r, i;
1379 
1380 	if (!(sc->flags & IPW_FLAG_FW_INITED))
1381 		return;
1382 
1383 	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1384 
1385 	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1386 		sbd = &sc->stbd_list[i];
1387 
1388 		if (sbd->type == IPW_SBD_TYPE_DATA)
1389 			ifp->if_opackets++;
1390 
1391 		ipw_release_sbd(sc, sbd);
1392 		sc->txfree++;
1393 	}
1394 
1395 	/* remember what the firmware has processed */
1396 	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1397 
1398 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1399 	ipw_start_locked(ifp);
1400 }
1401 
1402 static void
1403 ipw_fatal_error_intr(struct ipw_softc *sc)
1404 {
1405 	struct ifnet *ifp = sc->sc_ifp;
1406 	struct ieee80211com *ic = ifp->if_l2com;
1407 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1408 
1409 	device_printf(sc->sc_dev, "firmware error\n");
1410 	if (vap != NULL) {
1411 		IPW_UNLOCK(sc);
1412 		ieee80211_cancel_scan(vap);
1413 		IPW_LOCK(sc);
1414 	}
1415 	ieee80211_runtask(ic, &sc->sc_init_task);
1416 }
1417 
1418 static void
1419 ipw_intr(void *arg)
1420 {
1421 	struct ipw_softc *sc = arg;
1422 	uint32_t r;
1423 
1424 	IPW_LOCK(sc);
1425 
1426 	r = CSR_READ_4(sc, IPW_CSR_INTR);
1427 	if (r == 0 || r == 0xffffffff)
1428 		goto done;
1429 
1430 	/* disable interrupts */
1431 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1432 
1433 	/* acknowledge all interrupts */
1434 	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1435 
1436 	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1437 		ipw_fatal_error_intr(sc);
1438 		goto done;
1439 	}
1440 
1441 	if (r & IPW_INTR_FW_INIT_DONE)
1442 		wakeup(sc);
1443 
1444 	if (r & IPW_INTR_RX_TRANSFER)
1445 		ipw_rx_intr(sc);
1446 
1447 	if (r & IPW_INTR_TX_TRANSFER)
1448 		ipw_tx_intr(sc);
1449 
1450 	/* re-enable interrupts */
1451 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1452 done:
1453 	IPW_UNLOCK(sc);
1454 }
1455 
1456 static void
1457 ipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1458 {
1459 	if (error != 0)
1460 		return;
1461 
1462 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1463 
1464 	*(bus_addr_t *)arg = segs[0].ds_addr;
1465 }
1466 
1467 static const char *
1468 ipw_cmdname(int cmd)
1469 {
1470 #define	N(a)	(sizeof(a) / sizeof(a[0]))
1471 	static const struct {
1472 		int	cmd;
1473 		const char *name;
1474 	} cmds[] = {
1475 		{ IPW_CMD_ADD_MULTICAST,	"ADD_MULTICAST" },
1476 		{ IPW_CMD_BROADCAST_SCAN,	"BROADCAST_SCAN" },
1477 		{ IPW_CMD_DISABLE,		"DISABLE" },
1478 		{ IPW_CMD_DISABLE_PHY,		"DISABLE_PHY" },
1479 		{ IPW_CMD_ENABLE,		"ENABLE" },
1480 		{ IPW_CMD_PREPARE_POWER_DOWN,	"PREPARE_POWER_DOWN" },
1481 		{ IPW_CMD_SET_BASIC_TX_RATES,	"SET_BASIC_TX_RATES" },
1482 		{ IPW_CMD_SET_BEACON_INTERVAL,	"SET_BEACON_INTERVAL" },
1483 		{ IPW_CMD_SET_CHANNEL,		"SET_CHANNEL" },
1484 		{ IPW_CMD_SET_CONFIGURATION,	"SET_CONFIGURATION" },
1485 		{ IPW_CMD_SET_DESIRED_BSSID,	"SET_DESIRED_BSSID" },
1486 		{ IPW_CMD_SET_ESSID,		"SET_ESSID" },
1487 		{ IPW_CMD_SET_FRAG_THRESHOLD,	"SET_FRAG_THRESHOLD" },
1488 		{ IPW_CMD_SET_MAC_ADDRESS,	"SET_MAC_ADDRESS" },
1489 		{ IPW_CMD_SET_MANDATORY_BSSID,	"SET_MANDATORY_BSSID" },
1490 		{ IPW_CMD_SET_MODE,		"SET_MODE" },
1491 		{ IPW_CMD_SET_MSDU_TX_RATES,	"SET_MSDU_TX_RATES" },
1492 		{ IPW_CMD_SET_POWER_MODE,	"SET_POWER_MODE" },
1493 		{ IPW_CMD_SET_RTS_THRESHOLD,	"SET_RTS_THRESHOLD" },
1494 		{ IPW_CMD_SET_SCAN_OPTIONS,	"SET_SCAN_OPTIONS" },
1495 		{ IPW_CMD_SET_SECURITY_INFO,	"SET_SECURITY_INFO" },
1496 		{ IPW_CMD_SET_TX_POWER_INDEX,	"SET_TX_POWER_INDEX" },
1497 		{ IPW_CMD_SET_TX_RATES,		"SET_TX_RATES" },
1498 		{ IPW_CMD_SET_WEP_FLAGS,	"SET_WEP_FLAGS" },
1499 		{ IPW_CMD_SET_WEP_KEY,		"SET_WEP_KEY" },
1500 		{ IPW_CMD_SET_WEP_KEY_INDEX,	"SET_WEP_KEY_INDEX" },
1501 		{ IPW_CMD_SET_WPA_IE,		"SET_WPA_IE" },
1502 
1503 	};
1504 	static char buf[12];
1505 	int i;
1506 
1507 	for (i = 0; i < N(cmds); i++)
1508 		if (cmds[i].cmd == cmd)
1509 			return cmds[i].name;
1510 	snprintf(buf, sizeof(buf), "%u", cmd);
1511 	return buf;
1512 #undef N
1513 }
1514 
1515 /*
1516  * Send a command to the firmware and wait for the acknowledgement.
1517  */
1518 static int
1519 ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1520 {
1521 	struct ipw_soft_bd *sbd;
1522 	bus_addr_t physaddr;
1523 	int error;
1524 
1525 	IPW_LOCK_ASSERT(sc);
1526 
1527 	if (sc->flags & IPW_FLAG_BUSY) {
1528 		device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1529 			__func__, ipw_cmdname(type));
1530 		return EAGAIN;
1531 	}
1532 	sc->flags |= IPW_FLAG_BUSY;
1533 
1534 	sbd = &sc->stbd_list[sc->txcur];
1535 
1536 	error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1537 	    sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1538 	if (error != 0) {
1539 		device_printf(sc->sc_dev, "could not map command DMA memory\n");
1540 		sc->flags &= ~IPW_FLAG_BUSY;
1541 		return error;
1542 	}
1543 
1544 	sc->cmd.type = htole32(type);
1545 	sc->cmd.subtype = 0;
1546 	sc->cmd.len = htole32(len);
1547 	sc->cmd.seq = 0;
1548 	memcpy(sc->cmd.data, data, len);
1549 
1550 	sbd->type = IPW_SBD_TYPE_COMMAND;
1551 	sbd->bd->physaddr = htole32(physaddr);
1552 	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1553 	sbd->bd->nfrag = 1;
1554 	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1555 	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1556 
1557 	bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1558 	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1559 
1560 #ifdef IPW_DEBUG
1561 	if (ipw_debug >= 4) {
1562 		printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1563 		    0, 0, len);
1564 		/* Print the data buffer in the higher debug level */
1565 		if (ipw_debug >= 9 && len > 0) {
1566 			printf(" data: 0x");
1567 			for (int i = 1; i <= len; i++)
1568 				printf("%1D", (u_char *)data + len - i, "");
1569 		}
1570 		printf("\n");
1571 	}
1572 #endif
1573 
1574 	/* kick firmware */
1575 	sc->txfree--;
1576 	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1577 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1578 
1579 	/* wait at most one second for command to complete */
1580 	error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1581 	if (error != 0) {
1582 		device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1583 		    __func__, ipw_cmdname(type), error);
1584 		sc->flags &= ~IPW_FLAG_BUSY;
1585 		return (error);
1586 	}
1587 	return (0);
1588 }
1589 
1590 static int
1591 ipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1592 {
1593 	struct ipw_softc *sc = ifp->if_softc;
1594 	struct ieee80211com *ic = ifp->if_l2com;
1595 	struct ieee80211vap *vap = ni->ni_vap;
1596 	struct ieee80211_frame *wh;
1597 	struct ipw_soft_bd *sbd;
1598 	struct ipw_soft_hdr *shdr;
1599 	struct ipw_soft_buf *sbuf;
1600 	struct ieee80211_key *k;
1601 	struct mbuf *mnew;
1602 	bus_dma_segment_t segs[IPW_MAX_NSEG];
1603 	bus_addr_t physaddr;
1604 	int nsegs, error, i;
1605 
1606 	wh = mtod(m0, struct ieee80211_frame *);
1607 
1608 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1609 		k = ieee80211_crypto_encap(ni, m0);
1610 		if (k == NULL) {
1611 			m_freem(m0);
1612 			return ENOBUFS;
1613 		}
1614 		/* packet header may have moved, reset our local pointer */
1615 		wh = mtod(m0, struct ieee80211_frame *);
1616 	}
1617 
1618 	if (ieee80211_radiotap_active_vap(vap)) {
1619 		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1620 
1621 		tap->wt_flags = 0;
1622 
1623 		ieee80211_radiotap_tx(vap, m0);
1624 	}
1625 
1626 	shdr = SLIST_FIRST(&sc->free_shdr);
1627 	sbuf = SLIST_FIRST(&sc->free_sbuf);
1628 	KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1629 
1630 	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1631 	shdr->hdr.subtype = 0;
1632 	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1633 	shdr->hdr.encrypt = 0;
1634 	shdr->hdr.keyidx = 0;
1635 	shdr->hdr.keysz = 0;
1636 	shdr->hdr.fragmentsz = 0;
1637 	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1638 	if (ic->ic_opmode == IEEE80211_M_STA)
1639 		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1640 	else
1641 		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1642 
1643 	/* trim IEEE802.11 header */
1644 	m_adj(m0, sizeof (struct ieee80211_frame));
1645 
1646 	error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1647 	    &nsegs, 0);
1648 	if (error != 0 && error != EFBIG) {
1649 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1650 		    error);
1651 		m_freem(m0);
1652 		return error;
1653 	}
1654 	if (error != 0) {
1655 		mnew = m_defrag(m0, M_NOWAIT);
1656 		if (mnew == NULL) {
1657 			device_printf(sc->sc_dev,
1658 			    "could not defragment mbuf\n");
1659 			m_freem(m0);
1660 			return ENOBUFS;
1661 		}
1662 		m0 = mnew;
1663 
1664 		error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1665 		    segs, &nsegs, 0);
1666 		if (error != 0) {
1667 			device_printf(sc->sc_dev,
1668 			    "could not map mbuf (error %d)\n", error);
1669 			m_freem(m0);
1670 			return error;
1671 		}
1672 	}
1673 
1674 	error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1675 	    sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1676 	if (error != 0) {
1677 		device_printf(sc->sc_dev, "could not map header DMA memory\n");
1678 		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1679 		m_freem(m0);
1680 		return error;
1681 	}
1682 
1683 	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1684 	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1685 
1686 	sbd = &sc->stbd_list[sc->txcur];
1687 	sbd->type = IPW_SBD_TYPE_HEADER;
1688 	sbd->priv = shdr;
1689 	sbd->bd->physaddr = htole32(physaddr);
1690 	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1691 	sbd->bd->nfrag = 1 + nsegs;
1692 	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1693 	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1694 
1695 	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1696 	    shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1697 	    shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1698 	    ":"));
1699 
1700 	sc->txfree--;
1701 	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1702 
1703 	sbuf->m = m0;
1704 	sbuf->ni = ni;
1705 
1706 	for (i = 0; i < nsegs; i++) {
1707 		sbd = &sc->stbd_list[sc->txcur];
1708 
1709 		sbd->bd->physaddr = htole32(segs[i].ds_addr);
1710 		sbd->bd->len = htole32(segs[i].ds_len);
1711 		sbd->bd->nfrag = 0;
1712 		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1713 		if (i == nsegs - 1) {
1714 			sbd->type = IPW_SBD_TYPE_DATA;
1715 			sbd->priv = sbuf;
1716 			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1717 		} else {
1718 			sbd->type = IPW_SBD_TYPE_NOASSOC;
1719 			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1720 		}
1721 
1722 		DPRINTFN(5, ("sending fragment (%d)\n", i));
1723 
1724 		sc->txfree--;
1725 		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1726 	}
1727 
1728 	bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1729 	bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1730 	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1731 
1732 	/* kick firmware */
1733 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1734 
1735 	return 0;
1736 }
1737 
1738 static int
1739 ipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1740 	const struct ieee80211_bpf_params *params)
1741 {
1742 	/* no support; just discard */
1743 	m_freem(m);
1744 	ieee80211_free_node(ni);
1745 	return 0;
1746 }
1747 
1748 static void
1749 ipw_start(struct ifnet *ifp)
1750 {
1751 	struct ipw_softc *sc = ifp->if_softc;
1752 
1753 	IPW_LOCK(sc);
1754 	ipw_start_locked(ifp);
1755 	IPW_UNLOCK(sc);
1756 }
1757 
1758 static void
1759 ipw_start_locked(struct ifnet *ifp)
1760 {
1761 	struct ipw_softc *sc = ifp->if_softc;
1762 	struct ieee80211_node *ni;
1763 	struct mbuf *m;
1764 
1765 	IPW_LOCK_ASSERT(sc);
1766 
1767 	for (;;) {
1768 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1769 		if (m == NULL)
1770 			break;
1771 		if (sc->txfree < 1 + IPW_MAX_NSEG) {
1772 			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1773 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1774 			break;
1775 		}
1776 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1777 		if (ipw_tx_start(ifp, m, ni) != 0) {
1778 			ieee80211_free_node(ni);
1779 			ifp->if_oerrors++;
1780 			break;
1781 		}
1782 		/* start watchdog timer */
1783 		sc->sc_tx_timer = 5;
1784 	}
1785 }
1786 
1787 static void
1788 ipw_watchdog(void *arg)
1789 {
1790 	struct ipw_softc *sc = arg;
1791 	struct ifnet *ifp = sc->sc_ifp;
1792 	struct ieee80211com *ic = ifp->if_l2com;
1793 
1794 	IPW_LOCK_ASSERT(sc);
1795 
1796 	if (sc->sc_tx_timer > 0) {
1797 		if (--sc->sc_tx_timer == 0) {
1798 			if_printf(ifp, "device timeout\n");
1799 			ifp->if_oerrors++;
1800 			taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1801 		}
1802 	}
1803 	if (sc->sc_scan_timer > 0) {
1804 		if (--sc->sc_scan_timer == 0) {
1805 			DPRINTFN(3, ("Scan timeout\n"));
1806 			/* End the scan */
1807 			if (sc->flags & IPW_FLAG_SCANNING) {
1808 				IPW_UNLOCK(sc);
1809 				ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1810 				IPW_LOCK(sc);
1811 				sc->flags &= ~IPW_FLAG_SCANNING;
1812 			}
1813 		}
1814 	}
1815 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1816 		callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1817 }
1818 
1819 static int
1820 ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1821 {
1822 	struct ipw_softc *sc = ifp->if_softc;
1823 	struct ieee80211com *ic = ifp->if_l2com;
1824 	struct ifreq *ifr = (struct ifreq *) data;
1825 	int error = 0, startall = 0;
1826 
1827 	switch (cmd) {
1828 	case SIOCSIFFLAGS:
1829 		IPW_LOCK(sc);
1830 		if (ifp->if_flags & IFF_UP) {
1831 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1832 				ipw_init_locked(sc);
1833 				startall = 1;
1834 			}
1835 		} else {
1836 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1837 				ipw_stop_locked(sc);
1838 		}
1839 		IPW_UNLOCK(sc);
1840 		if (startall)
1841 			ieee80211_start_all(ic);
1842 		break;
1843 	case SIOCGIFMEDIA:
1844 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1845 		break;
1846 	case SIOCGIFADDR:
1847 		error = ether_ioctl(ifp, cmd, data);
1848 		break;
1849 	default:
1850 		error = EINVAL;
1851 		break;
1852 	}
1853 	return error;
1854 }
1855 
1856 static void
1857 ipw_stop_master(struct ipw_softc *sc)
1858 {
1859 	uint32_t tmp;
1860 	int ntries;
1861 
1862 	/* disable interrupts */
1863 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1864 
1865 	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1866 	for (ntries = 0; ntries < 50; ntries++) {
1867 		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1868 			break;
1869 		DELAY(10);
1870 	}
1871 	if (ntries == 50)
1872 		device_printf(sc->sc_dev, "timeout waiting for master\n");
1873 
1874 	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1875 	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1876 
1877 	/* Clear all flags except the following */
1878 	sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1879 }
1880 
1881 static int
1882 ipw_reset(struct ipw_softc *sc)
1883 {
1884 	uint32_t tmp;
1885 	int ntries;
1886 
1887 	ipw_stop_master(sc);
1888 
1889 	/* move adapter to D0 state */
1890 	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1891 	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1892 
1893 	/* wait for clock stabilization */
1894 	for (ntries = 0; ntries < 1000; ntries++) {
1895 		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1896 			break;
1897 		DELAY(200);
1898 	}
1899 	if (ntries == 1000)
1900 		return EIO;
1901 
1902 	tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1903 	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1904 
1905 	DELAY(10);
1906 
1907 	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1908 	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1909 
1910 	return 0;
1911 }
1912 
1913 static int
1914 ipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1915 {
1916 	int ms = hz < 1000 ? 1 : hz/10;
1917 	int i, error;
1918 
1919 	for (i = 0; i < 100; i++) {
1920 		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1921 			return 0;
1922 		error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1923 		if (error == 0 || error != EWOULDBLOCK)
1924 			return 0;
1925 	}
1926 	DPRINTF(("%s: timeout waiting for %s\n",
1927 		__func__, waitfor ? "disable" : "enable"));
1928 	return ETIMEDOUT;
1929 }
1930 
1931 static int
1932 ipw_enable(struct ipw_softc *sc)
1933 {
1934 	int error;
1935 
1936 	if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1937 		DPRINTF(("Enable adapter\n"));
1938 		error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1939 		if (error != 0)
1940 			return error;
1941 		error = ipw_waitfordisable(sc, 0);
1942 		if (error != 0)
1943 			return error;
1944 		sc->flags |= IPW_FLAG_ENABLED;
1945 	}
1946 	return 0;
1947 }
1948 
1949 static int
1950 ipw_disable(struct ipw_softc *sc)
1951 {
1952 	int error;
1953 
1954 	if (sc->flags & IPW_FLAG_ENABLED) {
1955 		DPRINTF(("Disable adapter\n"));
1956 		error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1957 		if (error != 0)
1958 			return error;
1959 		error = ipw_waitfordisable(sc, 1);
1960 		if (error != 0)
1961 			return error;
1962 		sc->flags &= ~IPW_FLAG_ENABLED;
1963 	}
1964 	return 0;
1965 }
1966 
1967 /*
1968  * Upload the microcode to the device.
1969  */
1970 static int
1971 ipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1972 {
1973 	int ntries;
1974 
1975 	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1976 	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1977 
1978 	MEM_WRITE_2(sc, 0x220000, 0x0703);
1979 	MEM_WRITE_2(sc, 0x220000, 0x0707);
1980 
1981 	MEM_WRITE_1(sc, 0x210014, 0x72);
1982 	MEM_WRITE_1(sc, 0x210014, 0x72);
1983 
1984 	MEM_WRITE_1(sc, 0x210000, 0x40);
1985 	MEM_WRITE_1(sc, 0x210000, 0x00);
1986 	MEM_WRITE_1(sc, 0x210000, 0x40);
1987 
1988 	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1989 
1990 	MEM_WRITE_1(sc, 0x210000, 0x00);
1991 	MEM_WRITE_1(sc, 0x210000, 0x00);
1992 	MEM_WRITE_1(sc, 0x210000, 0x80);
1993 
1994 	MEM_WRITE_2(sc, 0x220000, 0x0703);
1995 	MEM_WRITE_2(sc, 0x220000, 0x0707);
1996 
1997 	MEM_WRITE_1(sc, 0x210014, 0x72);
1998 	MEM_WRITE_1(sc, 0x210014, 0x72);
1999 
2000 	MEM_WRITE_1(sc, 0x210000, 0x00);
2001 	MEM_WRITE_1(sc, 0x210000, 0x80);
2002 
2003 	for (ntries = 0; ntries < 10; ntries++) {
2004 		if (MEM_READ_1(sc, 0x210000) & 1)
2005 			break;
2006 		DELAY(10);
2007 	}
2008 	if (ntries == 10) {
2009 		device_printf(sc->sc_dev,
2010 		    "timeout waiting for ucode to initialize\n");
2011 		return EIO;
2012 	}
2013 
2014 	MEM_WRITE_4(sc, 0x3000e0, 0);
2015 
2016 	return 0;
2017 }
2018 
2019 /* set of macros to handle unaligned little endian data in firmware image */
2020 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2021 #define GETLE16(p) ((p)[0] | (p)[1] << 8)
2022 static int
2023 ipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
2024 {
2025 	const uint8_t *p, *end;
2026 	uint32_t tmp, dst;
2027 	uint16_t len;
2028 	int error;
2029 
2030 	p = fw;
2031 	end = fw + size;
2032 	while (p < end) {
2033 		dst = GETLE32(p); p += 4;
2034 		len = GETLE16(p); p += 2;
2035 
2036 		ipw_write_mem_1(sc, dst, p, len);
2037 		p += len;
2038 	}
2039 
2040 	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
2041 	    IPW_IO_LED_OFF);
2042 
2043 	/* enable interrupts */
2044 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
2045 
2046 	/* kick the firmware */
2047 	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
2048 
2049 	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
2050 	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
2051 
2052 	/* wait at most one second for firmware initialization to complete */
2053 	if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
2054 		device_printf(sc->sc_dev, "timeout waiting for firmware "
2055 		    "initialization to complete\n");
2056 		return error;
2057 	}
2058 
2059 	tmp = CSR_READ_4(sc, IPW_CSR_IO);
2060 	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2061 	    IPW_IO_GPIO3_MASK);
2062 
2063 	return 0;
2064 }
2065 
2066 static int
2067 ipw_setwepkeys(struct ipw_softc *sc)
2068 {
2069 	struct ifnet *ifp = sc->sc_ifp;
2070 	struct ieee80211com *ic = ifp->if_l2com;
2071 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2072 	struct ipw_wep_key wepkey;
2073 	struct ieee80211_key *wk;
2074 	int error, i;
2075 
2076 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2077 		wk = &vap->iv_nw_keys[i];
2078 
2079 		if (wk->wk_cipher == NULL ||
2080 		    wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2081 			continue;
2082 
2083 		wepkey.idx = i;
2084 		wepkey.len = wk->wk_keylen;
2085 		memset(wepkey.key, 0, sizeof wepkey.key);
2086 		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2087 		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2088 		    wepkey.len));
2089 		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2090 		    sizeof wepkey);
2091 		if (error != 0)
2092 			return error;
2093 	}
2094 	return 0;
2095 }
2096 
2097 static int
2098 ipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2099 {
2100 	struct ipw_wpa_ie wpaie;
2101 
2102 	memset(&wpaie, 0, sizeof(wpaie));
2103 	wpaie.len = htole32(ielen);
2104 	/* XXX verify length */
2105 	memcpy(&wpaie.ie, ie, ielen);
2106 	DPRINTF(("Setting WPA IE\n"));
2107 	return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2108 }
2109 
2110 static int
2111 ipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2112 {
2113 	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2114 
2115 	if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2116 		DPRINTF(("Setting mandatory BSSID to null\n"));
2117 		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2118 	} else {
2119 		DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2120 		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2121 			bssid, IEEE80211_ADDR_LEN);
2122 	}
2123 }
2124 
2125 static int
2126 ipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2127 {
2128 	if (ssidlen == 0) {
2129 		/*
2130 		 * A bug in the firmware breaks the ``don't associate''
2131 		 * bit in the scan options command.  To compensate for
2132 		 * this install a bogus ssid when no ssid is specified
2133 		 * so the firmware won't try to associate.
2134 		 */
2135 		DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2136 		return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2137 			"\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2138 			"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2139 			"\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2140 			"\x3c\x3d", IEEE80211_NWID_LEN);
2141 	} else {
2142 #ifdef IPW_DEBUG
2143 		if (ipw_debug > 0) {
2144 			printf("Setting ESSID to ");
2145 			ieee80211_print_essid(ssid, ssidlen);
2146 			printf("\n");
2147 		}
2148 #endif
2149 		return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2150 	}
2151 }
2152 
2153 static int
2154 ipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2155 {
2156 	struct ipw_scan_options opts;
2157 
2158 	DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2159 	opts.channels = htole32(chanmask);
2160 	opts.flags = htole32(flags);
2161 	return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2162 }
2163 
2164 static int
2165 ipw_scan(struct ipw_softc *sc)
2166 {
2167 	uint32_t params;
2168 	int error;
2169 
2170 	DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2171 
2172 	if (sc->flags & IPW_FLAG_SCANNING)
2173 		return (EBUSY);
2174 	sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2175 
2176 	/* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2177 	error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2178 	if (error != 0)
2179 		goto done;
2180 
2181 	/*
2182 	 * Setup null/bogus ssid so firmware doesn't use any previous
2183 	 * ssid to try and associate.  This is because the ``don't
2184 	 * associate'' option bit is broken (sigh).
2185 	 */
2186 	error = ipw_setssid(sc, NULL, 0);
2187 	if (error != 0)
2188 		goto done;
2189 
2190 	/*
2191 	 * NB: the adapter may be disabled on association lost;
2192 	 *     if so just re-enable it to kick off scanning.
2193 	 */
2194 	DPRINTF(("Starting scan\n"));
2195 	sc->sc_scan_timer = 3;
2196 	if (sc->flags & IPW_FLAG_ENABLED) {
2197 		params = 0;				/* XXX? */
2198 		error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2199 				&params, sizeof(params));
2200 	} else
2201 		error = ipw_enable(sc);
2202 done:
2203 	if (error != 0) {
2204 		DPRINTF(("Scan failed\n"));
2205 		sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2206 	}
2207 	return (error);
2208 }
2209 
2210 static int
2211 ipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2212 {
2213 	struct ifnet *ifp = sc->sc_ifp;
2214 	struct ieee80211com *ic = ifp->if_l2com;
2215 	uint32_t data;
2216 	int error;
2217 
2218 	data = htole32(ieee80211_chan2ieee(ic, chan));
2219 	DPRINTF(("Setting channel to %u\n", le32toh(data)));
2220 	error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2221 	if (error == 0)
2222 		ipw_setcurchan(sc, chan);
2223 	return error;
2224 }
2225 
2226 static void
2227 ipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2228 {
2229 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2230 	struct ipw_softc *sc = ifp->if_softc;
2231 	struct ieee80211_node *ni = vap->iv_bss;
2232 	struct ipw_security security;
2233 	uint32_t data;
2234 	int error;
2235 
2236 	IPW_LOCK(sc);
2237 	error = ipw_disable(sc);
2238 	if (error != 0)
2239 		goto done;
2240 
2241 	memset(&security, 0, sizeof security);
2242 	security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2243 	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2244 	security.ciphers = htole32(IPW_CIPHER_NONE);
2245 	DPRINTF(("Setting authmode to %u\n", security.authmode));
2246 	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2247 	    sizeof security);
2248 	if (error != 0)
2249 		goto done;
2250 
2251 	data = htole32(vap->iv_rtsthreshold);
2252 	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2253 	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2254 	if (error != 0)
2255 		goto done;
2256 
2257 	data = htole32(vap->iv_fragthreshold);
2258 	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2259 	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2260 	if (error != 0)
2261 		goto done;
2262 
2263 	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2264 		error = ipw_setwepkeys(sc);
2265 		if (error != 0)
2266 			goto done;
2267 
2268 		if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2269 			data = htole32(vap->iv_def_txkey);
2270 			DPRINTF(("Setting wep tx key index to %u\n",
2271 				le32toh(data)));
2272 			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2273 			    sizeof data);
2274 			if (error != 0)
2275 				goto done;
2276 		}
2277 	}
2278 
2279 	data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2280 	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2281 	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2282 	if (error != 0)
2283 		goto done;
2284 
2285 	error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2286 	if (error != 0)
2287 		goto done;
2288 
2289 	error = ipw_setbssid(sc, ni->ni_bssid);
2290 	if (error != 0)
2291 		goto done;
2292 
2293 	if (vap->iv_appie_wpa != NULL) {
2294 		struct ieee80211_appie *ie = vap->iv_appie_wpa;
2295 		error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2296 		if (error != 0)
2297 			goto done;
2298 	}
2299 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2300 		error = ipw_setchannel(sc, ni->ni_chan);
2301 		if (error != 0)
2302 			goto done;
2303 	}
2304 
2305 	/* lock scan to ap's channel and enable associate */
2306 	error = ipw_setscanopts(sc,
2307 	    1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2308 	if (error != 0)
2309 		goto done;
2310 
2311 	error = ipw_enable(sc);		/* finally, enable adapter */
2312 	if (error == 0)
2313 		sc->flags |= IPW_FLAG_ASSOCIATING;
2314 done:
2315 	IPW_UNLOCK(sc);
2316 }
2317 
2318 static void
2319 ipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2320 {
2321 	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2322 	struct ieee80211_node *ni = vap->iv_bss;
2323 	struct ipw_softc *sc = ifp->if_softc;
2324 
2325 	IPW_LOCK(sc);
2326 	DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2327 	/*
2328 	 * NB: don't try to do this if ipw_stop_master has
2329 	 *     shutdown the firmware and disabled interrupts.
2330 	 */
2331 	if (sc->flags & IPW_FLAG_FW_INITED) {
2332 		sc->flags &= ~IPW_FLAG_ASSOCIATED;
2333 		/*
2334 		 * NB: firmware currently ignores bssid parameter, but
2335 		 *     supply it in case this changes (follow linux driver).
2336 		 */
2337 		(void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2338 			ni->ni_bssid, IEEE80211_ADDR_LEN);
2339 	}
2340 	IPW_UNLOCK(sc);
2341 }
2342 
2343 /*
2344  * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2345  * It is called on firmware panics or on watchdog timeouts.
2346  */
2347 static void
2348 ipw_init_task(void *context, int pending)
2349 {
2350 	ipw_init(context);
2351 }
2352 
2353 static void
2354 ipw_init(void *priv)
2355 {
2356 	struct ipw_softc *sc = priv;
2357 	struct ifnet *ifp = sc->sc_ifp;
2358 	struct ieee80211com *ic = ifp->if_l2com;
2359 
2360 	IPW_LOCK(sc);
2361 	ipw_init_locked(sc);
2362 	IPW_UNLOCK(sc);
2363 
2364 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2365 		ieee80211_start_all(ic);		/* start all vap's */
2366 }
2367 
2368 static void
2369 ipw_init_locked(struct ipw_softc *sc)
2370 {
2371 	struct ifnet *ifp = sc->sc_ifp;
2372 	struct ieee80211com *ic = ifp->if_l2com;
2373 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2374 	const struct firmware *fp;
2375 	const struct ipw_firmware_hdr *hdr;
2376 	const char *fw;
2377 
2378 	IPW_LOCK_ASSERT(sc);
2379 
2380 	DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2381 		ieee80211_state_name[vap->iv_state], sc->flags));
2382 
2383 	/*
2384 	 * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2385 	 * when loading the firmware and we don't want to be called during this
2386 	 * operation.
2387 	 */
2388 	if (sc->flags & IPW_FLAG_INIT_LOCKED)
2389 		return;
2390 	sc->flags |= IPW_FLAG_INIT_LOCKED;
2391 
2392 	ipw_stop_locked(sc);
2393 
2394 	if (ipw_reset(sc) != 0) {
2395 		device_printf(sc->sc_dev, "could not reset adapter\n");
2396 		goto fail;
2397 	}
2398 
2399 	if (sc->sc_firmware == NULL) {
2400 		device_printf(sc->sc_dev, "no firmware\n");
2401 		goto fail;
2402 	}
2403 	/* NB: consistency already checked on load */
2404 	fp = sc->sc_firmware;
2405 	hdr = (const struct ipw_firmware_hdr *)fp->data;
2406 
2407 	DPRINTF(("Loading firmware image '%s'\n", fp->name));
2408 	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2409 	if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2410 		device_printf(sc->sc_dev, "could not load microcode\n");
2411 		goto fail;
2412 	}
2413 
2414 	ipw_stop_master(sc);
2415 
2416 	/*
2417 	 * Setup tx, rx and status rings.
2418 	 */
2419 	sc->txold = IPW_NTBD - 1;
2420 	sc->txcur = 0;
2421 	sc->txfree = IPW_NTBD - 2;
2422 	sc->rxcur = IPW_NRBD - 1;
2423 
2424 	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2425 	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2426 	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2427 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2428 
2429 	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2430 	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2431 	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2432 	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2433 
2434 	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2435 
2436 	fw = (const char *)fp->data + sizeof *hdr;
2437 	if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2438 		device_printf(sc->sc_dev, "could not load firmware\n");
2439 		goto fail;
2440 	}
2441 
2442 	sc->flags |= IPW_FLAG_FW_INITED;
2443 
2444 	/* retrieve information tables base addresses */
2445 	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2446 	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2447 
2448 	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2449 
2450 	if (ipw_config(sc) != 0) {
2451 		device_printf(sc->sc_dev, "device configuration failed\n");
2452 		goto fail;
2453 	}
2454 
2455 	callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2456 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2457 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2458 
2459 	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2460 	return;
2461 
2462 fail:
2463 	ipw_stop_locked(sc);
2464 	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2465 }
2466 
2467 static int
2468 ipw_config(struct ipw_softc *sc)
2469 {
2470 	struct ifnet *ifp = sc->sc_ifp;
2471 	struct ieee80211com *ic = ifp->if_l2com;
2472 	struct ipw_configuration config;
2473 	uint32_t data;
2474 	int error;
2475 
2476 	error = ipw_disable(sc);
2477 	if (error != 0)
2478 		return error;
2479 
2480 	switch (ic->ic_opmode) {
2481 	case IEEE80211_M_STA:
2482 	case IEEE80211_M_HOSTAP:
2483 	case IEEE80211_M_WDS:		/* XXX */
2484 		data = htole32(IPW_MODE_BSS);
2485 		break;
2486 	case IEEE80211_M_IBSS:
2487 	case IEEE80211_M_AHDEMO:
2488 		data = htole32(IPW_MODE_IBSS);
2489 		break;
2490 	case IEEE80211_M_MONITOR:
2491 		data = htole32(IPW_MODE_MONITOR);
2492 		break;
2493 	default:
2494 		device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2495 		return EINVAL;
2496 	}
2497 	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2498 	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2499 	if (error != 0)
2500 		return error;
2501 
2502 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2503 	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2504 		error = ipw_setchannel(sc, ic->ic_curchan);
2505 		if (error != 0)
2506 			return error;
2507 	}
2508 
2509 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2510 		return ipw_enable(sc);
2511 
2512 	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2513 	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2514 	if (ic->ic_opmode == IEEE80211_M_IBSS)
2515 		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2516 	if (ifp->if_flags & IFF_PROMISC)
2517 		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2518 	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2519 	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2520 	DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2521 	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2522 	if (error != 0)
2523 		return error;
2524 
2525 	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2526 	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2527 	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2528 	if (error != 0)
2529 		return error;
2530 
2531 	/* Use the same rate set */
2532 	DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2533 	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2534 	if (error != 0)
2535 		return error;
2536 
2537 	/* Use the same rate set */
2538 	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2539 	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2540 	if (error != 0)
2541 		return error;
2542 
2543 	data = htole32(IPW_POWER_MODE_CAM);
2544 	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2545 	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2546 	if (error != 0)
2547 		return error;
2548 
2549 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2550 		data = htole32(32); /* default value */
2551 		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2552 		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2553 		    sizeof data);
2554 		if (error != 0)
2555 			return error;
2556 	}
2557 
2558 	return 0;
2559 }
2560 
2561 static void
2562 ipw_stop(void *priv)
2563 {
2564 	struct ipw_softc *sc = priv;
2565 
2566 	IPW_LOCK(sc);
2567 	ipw_stop_locked(sc);
2568 	IPW_UNLOCK(sc);
2569 }
2570 
2571 static void
2572 ipw_stop_locked(struct ipw_softc *sc)
2573 {
2574 	struct ifnet *ifp = sc->sc_ifp;
2575 	int i;
2576 
2577 	IPW_LOCK_ASSERT(sc);
2578 
2579 	callout_stop(&sc->sc_wdtimer);
2580 	ipw_stop_master(sc);
2581 
2582 	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2583 
2584 	/*
2585 	 * Release tx buffers.
2586 	 */
2587 	for (i = 0; i < IPW_NTBD; i++)
2588 		ipw_release_sbd(sc, &sc->stbd_list[i]);
2589 
2590 	sc->sc_tx_timer = 0;
2591 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2592 }
2593 
2594 static int
2595 ipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2596 {
2597 	struct ipw_softc *sc = arg1;
2598 	uint32_t i, size, buf[256];
2599 
2600 	memset(buf, 0, sizeof buf);
2601 
2602 	if (!(sc->flags & IPW_FLAG_FW_INITED))
2603 		return SYSCTL_OUT(req, buf, sizeof buf);
2604 
2605 	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2606 
2607 	size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2608 	for (i = 1; i < size; i++)
2609 		buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2610 
2611 	return SYSCTL_OUT(req, buf, size);
2612 }
2613 
2614 static int
2615 ipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2616 {
2617 	struct ipw_softc *sc = arg1;
2618 	int val;
2619 
2620 	val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2621 	        (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2622 
2623 	return SYSCTL_OUT(req, &val, sizeof val);
2624 }
2625 
2626 static uint32_t
2627 ipw_read_table1(struct ipw_softc *sc, uint32_t off)
2628 {
2629 	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2630 }
2631 
2632 static void
2633 ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2634 {
2635 	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2636 }
2637 
2638 #if 0
2639 static int
2640 ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2641 {
2642 	uint32_t addr, info;
2643 	uint16_t count, size;
2644 	uint32_t total;
2645 
2646 	/* addr[4] + count[2] + size[2] */
2647 	addr = MEM_READ_4(sc, sc->table2_base + off);
2648 	info = MEM_READ_4(sc, sc->table2_base + off + 4);
2649 
2650 	count = info >> 16;
2651 	size = info & 0xffff;
2652 	total = count * size;
2653 
2654 	if (total > *len) {
2655 		*len = total;
2656 		return EINVAL;
2657 	}
2658 
2659 	*len = total;
2660 	ipw_read_mem_1(sc, addr, buf, total);
2661 
2662 	return 0;
2663 }
2664 
2665 static void
2666 ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2667     bus_size_t count)
2668 {
2669 	for (; count > 0; offset++, datap++, count--) {
2670 		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2671 		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2672 	}
2673 }
2674 #endif
2675 
2676 static void
2677 ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2678     bus_size_t count)
2679 {
2680 	for (; count > 0; offset++, datap++, count--) {
2681 		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2682 		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2683 	}
2684 }
2685 
2686 static void
2687 ipw_scan_start(struct ieee80211com *ic)
2688 {
2689 	struct ifnet *ifp = ic->ic_ifp;
2690 	struct ipw_softc *sc = ifp->if_softc;
2691 
2692 	IPW_LOCK(sc);
2693 	ipw_scan(sc);
2694 	IPW_UNLOCK(sc);
2695 }
2696 
2697 static void
2698 ipw_set_channel(struct ieee80211com *ic)
2699 {
2700 	struct ifnet *ifp = ic->ic_ifp;
2701 	struct ipw_softc *sc = ifp->if_softc;
2702 
2703 	IPW_LOCK(sc);
2704 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2705 		ipw_disable(sc);
2706 		ipw_setchannel(sc, ic->ic_curchan);
2707 		ipw_enable(sc);
2708 	}
2709 	IPW_UNLOCK(sc);
2710 }
2711 
2712 static void
2713 ipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2714 {
2715 	/* NB: all channels are scanned at once */
2716 }
2717 
2718 static void
2719 ipw_scan_mindwell(struct ieee80211_scan_state *ss)
2720 {
2721 	/* NB: don't try to abort scan; wait for firmware to finish */
2722 }
2723 
2724 static void
2725 ipw_scan_end(struct ieee80211com *ic)
2726 {
2727 	struct ifnet *ifp = ic->ic_ifp;
2728 	struct ipw_softc *sc = ifp->if_softc;
2729 
2730 	IPW_LOCK(sc);
2731 	sc->flags &= ~IPW_FLAG_SCANNING;
2732 	IPW_UNLOCK(sc);
2733 }
2734