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