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