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