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