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