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