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