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