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