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