if_ipw.c revision 172574
1145247Sdamien/*	$FreeBSD: head/sys/dev/ipw/if_ipw.c 172574 2007-10-12 10:00:26Z thompsa $	*/
2145247Sdamien
3145247Sdamien/*-
4156599Sdamien * Copyright (c) 2004-2006
5145247Sdamien *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6172567Sthompsa * Copyright (c) 2006 Sam Leffler, Errno Consulting
7172567Sthompsa * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
8145247Sdamien *
9145247Sdamien * Redistribution and use in source and binary forms, with or without
10145247Sdamien * modification, are permitted provided that the following conditions
11145247Sdamien * are met:
12145247Sdamien * 1. Redistributions of source code must retain the above copyright
13145247Sdamien *    notice unmodified, this list of conditions, and the following
14145247Sdamien *    disclaimer.
15145247Sdamien * 2. Redistributions in binary form must reproduce the above copyright
16145247Sdamien *    notice, this list of conditions and the following disclaimer in the
17145247Sdamien *    documentation and/or other materials provided with the distribution.
18145247Sdamien *
19145247Sdamien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20145247Sdamien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21145247Sdamien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22145247Sdamien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23145247Sdamien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24145247Sdamien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25145247Sdamien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26145247Sdamien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27145247Sdamien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28145247Sdamien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29145247Sdamien * SUCH DAMAGE.
30145247Sdamien */
31145247Sdamien
32145247Sdamien#include <sys/cdefs.h>
33145247Sdamien__FBSDID("$FreeBSD: head/sys/dev/ipw/if_ipw.c 172574 2007-10-12 10:00:26Z thompsa $");
34145247Sdamien
35145247Sdamien/*-
36145247Sdamien * Intel(R) PRO/Wireless 2100 MiniPCI driver
37145247Sdamien * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
38145247Sdamien */
39145247Sdamien
40145247Sdamien#include <sys/param.h>
41145247Sdamien#include <sys/sysctl.h>
42145247Sdamien#include <sys/sockio.h>
43145247Sdamien#include <sys/mbuf.h>
44145247Sdamien#include <sys/kernel.h>
45145247Sdamien#include <sys/socket.h>
46145247Sdamien#include <sys/systm.h>
47145247Sdamien#include <sys/malloc.h>
48156599Sdamien#include <sys/queue.h>
49156599Sdamien#include <sys/taskqueue.h>
50145247Sdamien#include <sys/module.h>
51145247Sdamien#include <sys/bus.h>
52145247Sdamien#include <sys/endian.h>
53156599Sdamien#include <sys/linker.h>
54156599Sdamien#include <sys/firmware.h>
55145247Sdamien
56145247Sdamien#include <machine/bus.h>
57145247Sdamien#include <machine/resource.h>
58145247Sdamien#include <sys/rman.h>
59145247Sdamien
60145247Sdamien#include <dev/pci/pcireg.h>
61145247Sdamien#include <dev/pci/pcivar.h>
62145247Sdamien
63145247Sdamien#include <net/bpf.h>
64145247Sdamien#include <net/if.h>
65145247Sdamien#include <net/if_arp.h>
66145247Sdamien#include <net/ethernet.h>
67145247Sdamien#include <net/if_dl.h>
68145247Sdamien#include <net/if_media.h>
69145247Sdamien#include <net/if_types.h>
70145247Sdamien
71156599Sdamien#include <net80211/ieee80211_var.h>
72156599Sdamien#include <net80211/ieee80211_radiotap.h>
73156599Sdamien
74145247Sdamien#include <netinet/in.h>
75145247Sdamien#include <netinet/in_systm.h>
76145247Sdamien#include <netinet/in_var.h>
77145247Sdamien#include <netinet/ip.h>
78145247Sdamien#include <netinet/if_ether.h>
79145247Sdamien
80145247Sdamien#include <dev/ipw/if_ipwreg.h>
81145247Sdamien#include <dev/ipw/if_ipwvar.h>
82145247Sdamien
83172567Sthompsa#define IPW_DEBUG
84145247Sdamien#ifdef IPW_DEBUG
85145247Sdamien#define DPRINTF(x)	do { if (ipw_debug > 0) printf x; } while (0)
86145247Sdamien#define DPRINTFN(n, x)	do { if (ipw_debug >= (n)) printf x; } while (0)
87145247Sdamienint ipw_debug = 0;
88145247SdamienSYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
89145247Sdamien#else
90145247Sdamien#define DPRINTF(x)
91145247Sdamien#define DPRINTFN(n, x)
92145247Sdamien#endif
93145247Sdamien
94145247SdamienMODULE_DEPEND(ipw, pci,  1, 1, 1);
95145247SdamienMODULE_DEPEND(ipw, wlan, 1, 1, 1);
96156599SdamienMODULE_DEPEND(ipw, firmware, 1, 1, 1);
97145247Sdamien
98145247Sdamienstruct ipw_ident {
99145247Sdamien	uint16_t	vendor;
100145247Sdamien	uint16_t	device;
101145247Sdamien	const char	*name;
102145247Sdamien};
103145247Sdamien
104145247Sdamienstatic const struct ipw_ident ipw_ident_table[] = {
105145247Sdamien	{ 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
106145247Sdamien
107145247Sdamien	{ 0, 0, NULL }
108145247Sdamien};
109145247Sdamien
110145247Sdamienstatic int	ipw_dma_alloc(struct ipw_softc *);
111145247Sdamienstatic void	ipw_release(struct ipw_softc *);
112145247Sdamienstatic int	ipw_media_change(struct ifnet *);
113145247Sdamienstatic void	ipw_media_status(struct ifnet *, struct ifmediareq *);
114145247Sdamienstatic int	ipw_newstate(struct ieee80211com *, enum ieee80211_state, int);
115145247Sdamienstatic uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
116172567Sthompsastatic void	ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
117172567Sthompsastatic void	ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
118172567Sthompsastatic void	ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
119145247Sdamien		    struct ipw_soft_bd *, struct ipw_soft_buf *);
120145247Sdamienstatic void	ipw_rx_intr(struct ipw_softc *);
121145247Sdamienstatic void	ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
122145247Sdamienstatic void	ipw_tx_intr(struct ipw_softc *);
123145247Sdamienstatic void	ipw_intr(void *);
124145247Sdamienstatic void	ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
125172567Sthompsastatic const char * ipw_cmdname(int);
126145247Sdamienstatic int	ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
127145247Sdamienstatic int	ipw_tx_start(struct ifnet *, struct mbuf *,
128145247Sdamien		    struct ieee80211_node *);
129145247Sdamienstatic void	ipw_start(struct ifnet *);
130172567Sthompsastatic void	ipw_start_locked(struct ifnet *);
131172567Sthompsastatic void	ipw_watchdog(void *);
132145247Sdamienstatic int	ipw_ioctl(struct ifnet *, u_long, caddr_t);
133145247Sdamienstatic void	ipw_stop_master(struct ipw_softc *);
134172567Sthompsastatic int	ipw_enable(struct ipw_softc *);
135172567Sthompsastatic int	ipw_disable(struct ipw_softc *);
136145247Sdamienstatic int	ipw_reset(struct ipw_softc *);
137156599Sdamienstatic int	ipw_load_ucode(struct ipw_softc *, const char *, int);
138156599Sdamienstatic int	ipw_load_firmware(struct ipw_softc *, const char *, int);
139145247Sdamienstatic int	ipw_config(struct ipw_softc *);
140172567Sthompsastatic void	ipw_assoc_task(void *, int);
141172567Sthompsastatic int	ipw_auth_and_assoc(struct ipw_softc *);
142172567Sthompsastatic void	ipw_disassoc_task(void *, int);
143172567Sthompsastatic int	ipw_disassociate(struct ipw_softc *);
144156599Sdamienstatic void	ipw_init_task(void *, int);
145145247Sdamienstatic void	ipw_init(void *);
146172567Sthompsastatic void	ipw_init_locked(struct ipw_softc *, int);
147145247Sdamienstatic void	ipw_stop(void *);
148172567Sthompsastatic void	ipw_stop_locked(struct ipw_softc *);
149145247Sdamienstatic int	ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
150145247Sdamienstatic int	ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
151145247Sdamienstatic uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
152145247Sdamienstatic void	ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
153172567Sthompsa#if 0
154145247Sdamienstatic int	ipw_read_table2(struct ipw_softc *, uint32_t, void *,
155145247Sdamien		    uint32_t *);
156145247Sdamienstatic void	ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
157145247Sdamien		    bus_size_t);
158172567Sthompsa#endif
159156599Sdamienstatic void	ipw_write_mem_1(struct ipw_softc *, bus_size_t,
160156599Sdamien		    const uint8_t *, bus_size_t);
161172567Sthompsastatic void	ipw_scan_task(void *, int);
162172567Sthompsastatic int	ipw_scan(struct ipw_softc *);
163172567Sthompsastatic void	ipw_scan_start(struct ieee80211com *);
164172567Sthompsastatic void	ipw_scan_end(struct ieee80211com *);
165172567Sthompsastatic void	ipw_set_channel(struct ieee80211com *);
166172567Sthompsastatic void	ipw_scan_curchan(struct ieee80211com *, unsigned long maxdwell);
167172567Sthompsastatic void	ipw_scan_mindwell(struct ieee80211com *);
168145247Sdamien
169145247Sdamienstatic int ipw_probe(device_t);
170145247Sdamienstatic int ipw_attach(device_t);
171145247Sdamienstatic int ipw_detach(device_t);
172145247Sdamienstatic int ipw_shutdown(device_t);
173145247Sdamienstatic int ipw_suspend(device_t);
174145247Sdamienstatic int ipw_resume(device_t);
175145247Sdamien
176145247Sdamienstatic device_method_t ipw_methods[] = {
177145247Sdamien	/* Device interface */
178145247Sdamien	DEVMETHOD(device_probe,		ipw_probe),
179145247Sdamien	DEVMETHOD(device_attach,	ipw_attach),
180145247Sdamien	DEVMETHOD(device_detach,	ipw_detach),
181145247Sdamien	DEVMETHOD(device_shutdown,	ipw_shutdown),
182145247Sdamien	DEVMETHOD(device_suspend,	ipw_suspend),
183145247Sdamien	DEVMETHOD(device_resume,	ipw_resume),
184145247Sdamien
185145247Sdamien	{ 0, 0 }
186145247Sdamien};
187145247Sdamien
188145247Sdamienstatic driver_t ipw_driver = {
189145247Sdamien	"ipw",
190145247Sdamien	ipw_methods,
191145247Sdamien	sizeof (struct ipw_softc)
192145247Sdamien};
193145247Sdamien
194145247Sdamienstatic devclass_t ipw_devclass;
195145247Sdamien
196145247SdamienDRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0);
197172567SthompsaDRIVER_MODULE(ipw, cardbus, ipw_driver, ipw_devclass, 0, 0);
198145247Sdamien
199145247Sdamienstatic int
200145247Sdamienipw_probe(device_t dev)
201145247Sdamien{
202145247Sdamien	const struct ipw_ident *ident;
203145247Sdamien
204145247Sdamien	for (ident = ipw_ident_table; ident->name != NULL; ident++) {
205145247Sdamien		if (pci_get_vendor(dev) == ident->vendor &&
206145247Sdamien		    pci_get_device(dev) == ident->device) {
207145247Sdamien			device_set_desc(dev, ident->name);
208145247Sdamien			return 0;
209145247Sdamien		}
210145247Sdamien	}
211145247Sdamien	return ENXIO;
212145247Sdamien}
213145247Sdamien
214145247Sdamien/* Base Address Register */
215145247Sdamien#define IPW_PCI_BAR0	0x10
216145247Sdamien
217145247Sdamienstatic int
218145247Sdamienipw_attach(device_t dev)
219145247Sdamien{
220145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
221147256Sbrooks	struct ifnet *ifp;
222145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
223170530Ssam	struct ieee80211_channel *c;
224145247Sdamien	uint16_t val;
225145247Sdamien	int error, i;
226145247Sdamien
227145247Sdamien	sc->sc_dev = dev;
228145247Sdamien
229145247Sdamien	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
230145247Sdamien	    MTX_DEF | MTX_RECURSE);
231145247Sdamien
232156599Sdamien	TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
233172567Sthompsa	TASK_INIT(&sc->sc_scan_task, 0, ipw_scan_task, sc);
234172567Sthompsa	TASK_INIT(&sc->sc_assoc_task, 0, ipw_assoc_task, sc);
235172567Sthompsa	TASK_INIT(&sc->sc_disassoc_task, 0, ipw_disassoc_task, sc);
236172567Sthompsa	callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
237156599Sdamien
238145247Sdamien	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
239145247Sdamien		device_printf(dev, "chip is in D%d power mode "
240145247Sdamien		    "-- setting to D0\n", pci_get_powerstate(dev));
241145247Sdamien		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
242145247Sdamien	}
243145247Sdamien
244146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
245146498Sdamien
246145247Sdamien	/* enable bus-mastering */
247145247Sdamien	pci_enable_busmaster(dev);
248145247Sdamien
249145247Sdamien	sc->mem_rid = IPW_PCI_BAR0;
250145247Sdamien	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
251145247Sdamien	    RF_ACTIVE);
252145247Sdamien	if (sc->mem == NULL) {
253145247Sdamien		device_printf(dev, "could not allocate memory resource\n");
254145247Sdamien		goto fail;
255145247Sdamien	}
256145247Sdamien
257145247Sdamien	sc->sc_st = rman_get_bustag(sc->mem);
258145247Sdamien	sc->sc_sh = rman_get_bushandle(sc->mem);
259145247Sdamien
260145247Sdamien	sc->irq_rid = 0;
261145247Sdamien	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
262145247Sdamien	    RF_ACTIVE | RF_SHAREABLE);
263145247Sdamien	if (sc->irq == NULL) {
264145247Sdamien		device_printf(dev, "could not allocate interrupt resource\n");
265145247Sdamien		goto fail;
266145247Sdamien	}
267145247Sdamien
268145247Sdamien	if (ipw_reset(sc) != 0) {
269145247Sdamien		device_printf(dev, "could not reset adapter\n");
270145247Sdamien		goto fail;
271145247Sdamien	}
272145247Sdamien
273145247Sdamien	if (ipw_dma_alloc(sc) != 0) {
274145247Sdamien		device_printf(dev, "could not allocate DMA resources\n");
275145247Sdamien		goto fail;
276145247Sdamien	}
277147757Sdamien
278147256Sbrooks	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
279147256Sbrooks	if (ifp == NULL) {
280147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
281147256Sbrooks		goto fail;
282147256Sbrooks	}
283147757Sdamien
284145247Sdamien	ifp->if_softc = sc;
285145247Sdamien	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
286145247Sdamien	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
287145247Sdamien	ifp->if_init = ipw_init;
288145247Sdamien	ifp->if_ioctl = ipw_ioctl;
289145247Sdamien	ifp->if_start = ipw_start;
290145247Sdamien	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
291145247Sdamien	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
292145247Sdamien	IFQ_SET_READY(&ifp->if_snd);
293145247Sdamien
294145247Sdamien	ic->ic_ifp = ifp;
295145247Sdamien	ic->ic_phytype = IEEE80211_T_DS;
296145247Sdamien	ic->ic_opmode = IEEE80211_M_STA;
297145247Sdamien	ic->ic_state = IEEE80211_S_INIT;
298145247Sdamien
299145247Sdamien	/* set device capabilities */
300172567Sthompsa	ic->ic_caps = IEEE80211_C_IBSS		/* IBSS mode supported */
301172567Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode supported */
302172567Sthompsa		| IEEE80211_C_PMGT		/* power save supported */
303172567Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
304172567Sthompsa		| IEEE80211_C_WPA		/* 802.11i supported */
305172567Sthompsa		;
306145247Sdamien
307145247Sdamien	/* read MAC address from EEPROM */
308145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
309145247Sdamien	ic->ic_myaddr[0] = val >> 8;
310145247Sdamien	ic->ic_myaddr[1] = val & 0xff;
311145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
312145247Sdamien	ic->ic_myaddr[2] = val >> 8;
313145247Sdamien	ic->ic_myaddr[3] = val & 0xff;
314145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
315145247Sdamien	ic->ic_myaddr[4] = val >> 8;
316145247Sdamien	ic->ic_myaddr[5] = val & 0xff;
317145247Sdamien
318170530Ssam	/* set supported .11b channels (read from EEPROM) */
319170530Ssam	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
320170530Ssam		val = 0x7ff; /* default to channels 1-11 */
321170530Ssam	val <<= 1;
322170530Ssam	for (i = 1; i < 16; i++) {
323170530Ssam		if (val & (1 << i)) {
324170530Ssam			c = &ic->ic_channels[ic->ic_nchans++];
325170530Ssam			c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
326170530Ssam			c->ic_flags = IEEE80211_CHAN_B;
327170530Ssam			c->ic_ieee = i;
328170530Ssam		}
329145247Sdamien	}
330145247Sdamien
331145247Sdamien	/* check support for radio transmitter switch in EEPROM */
332145247Sdamien	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
333145247Sdamien		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
334145247Sdamien
335145247Sdamien	ieee80211_ifattach(ic);
336145247Sdamien	/* override state transition machine */
337145247Sdamien	sc->sc_newstate = ic->ic_newstate;
338145247Sdamien	ic->ic_newstate = ipw_newstate;
339145247Sdamien	ieee80211_media_init(ic, ipw_media_change, ipw_media_status);
340145247Sdamien
341172567Sthompsa	ic->ic_scan_start = ipw_scan_start;
342172567Sthompsa	ic->ic_scan_end = ipw_scan_end;
343172567Sthompsa	ic->ic_set_channel = ipw_set_channel;
344172567Sthompsa	ic->ic_scan_curchan = ipw_scan_curchan;
345172567Sthompsa	ic->ic_scan_mindwell = ipw_scan_mindwell;
346172567Sthompsa
347145247Sdamien	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
348172567Sthompsa	    sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap),
349171086Skevlo	    &sc->sc_drvbpf);
350145247Sdamien
351171086Skevlo	sc->sc_rxtap_len = sizeof sc->sc_rxtap;
352145247Sdamien	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
353145247Sdamien	sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
354145247Sdamien
355171086Skevlo	sc->sc_txtap_len = sizeof sc->sc_txtap;
356145247Sdamien	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
357145247Sdamien	sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
358145247Sdamien
359145247Sdamien	/*
360145247Sdamien	 * Add a few sysctl knobs.
361145247Sdamien	 */
362145247Sdamien	sc->dwelltime = 100;
363145247Sdamien
364145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
365145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
366145247Sdamien	    CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
367145247Sdamien	    "radio transmitter switch state (0=off, 1=on)");
368145247Sdamien
369145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
370145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
371145247Sdamien	    CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
372145247Sdamien	    "statistics");
373145247Sdamien
374145247Sdamien	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
375145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "dwell",
376145247Sdamien	    CTLFLAG_RW, &sc->dwelltime, 0,
377145247Sdamien	    "channel dwell time (ms) for AP/station scanning");
378145247Sdamien
379145247Sdamien	/*
380145247Sdamien	 * Hook our interrupt after all initialization is complete.
381145247Sdamien	 */
382145247Sdamien	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
383166901Spiso	    NULL, ipw_intr, sc, &sc->sc_ih);
384145247Sdamien	if (error != 0) {
385145247Sdamien		device_printf(dev, "could not set up interrupt\n");
386145247Sdamien		goto fail;
387145247Sdamien	}
388145247Sdamien
389145247Sdamien	if (bootverbose)
390145247Sdamien		ieee80211_announce(ic);
391145247Sdamien
392145247Sdamien	return 0;
393145247Sdamien
394145247Sdamienfail:	ipw_detach(dev);
395145247Sdamien	return ENXIO;
396145247Sdamien}
397145247Sdamien
398145247Sdamienstatic int
399145247Sdamienipw_detach(device_t dev)
400145247Sdamien{
401145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
402145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
403145247Sdamien	struct ifnet *ifp = ic->ic_ifp;
404145247Sdamien
405145247Sdamien	ipw_stop(sc);
406172567Sthompsa	callout_drain(&sc->sc_wdtimer);
407172567Sthompsa	taskqueue_drain(taskqueue_fast, &sc->sc_init_task);
408172567Sthompsa	taskqueue_drain(taskqueue_fast, &sc->sc_scan_task);
409172567Sthompsa	taskqueue_drain(taskqueue_fast, &sc->sc_assoc_task);
410172567Sthompsa	taskqueue_drain(taskqueue_fast, &sc->sc_disassoc_task);
411145247Sdamien
412147757Sdamien	if (ifp != NULL) {
413147256Sbrooks		bpfdetach(ifp);
414147757Sdamien		ieee80211_ifdetach(ic);
415147757Sdamien	}
416145247Sdamien
417145247Sdamien	ipw_release(sc);
418145247Sdamien
419145247Sdamien	if (sc->irq != NULL) {
420145247Sdamien		bus_teardown_intr(dev, sc->irq, sc->sc_ih);
421145247Sdamien		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
422145247Sdamien	}
423145247Sdamien
424145247Sdamien	if (sc->mem != NULL)
425145247Sdamien		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
426156599Sdamien
427150306Simp	if (ifp != NULL)
428150306Simp		if_free(ifp);
429145247Sdamien
430159487Siedowse	if (sc->sc_firmware != NULL) {
431159487Siedowse		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
432159487Siedowse		sc->sc_firmware = NULL;
433159487Siedowse	}
434159487Siedowse
435145247Sdamien	mtx_destroy(&sc->sc_mtx);
436145247Sdamien
437145247Sdamien	return 0;
438145247Sdamien}
439145247Sdamien
440145247Sdamienstatic int
441145247Sdamienipw_dma_alloc(struct ipw_softc *sc)
442145247Sdamien{
443145247Sdamien	struct ipw_soft_bd *sbd;
444145247Sdamien	struct ipw_soft_hdr *shdr;
445145247Sdamien	struct ipw_soft_buf *sbuf;
446145247Sdamien	bus_addr_t physaddr;
447145247Sdamien	int error, i;
448145247Sdamien
449145247Sdamien	/*
450145247Sdamien	 * Allocate and map tx ring.
451145247Sdamien	 */
452145247Sdamien	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
453145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
454145247Sdamien	    NULL, &sc->tbd_dmat);
455145247Sdamien	if (error != 0) {
456145247Sdamien		device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
457145247Sdamien		goto fail;
458145247Sdamien	}
459145247Sdamien
460145247Sdamien	error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
461145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
462145247Sdamien	if (error != 0) {
463145247Sdamien		device_printf(sc->sc_dev,
464145247Sdamien		    "could not allocate tx ring DMA memory\n");
465145247Sdamien		goto fail;
466145247Sdamien	}
467145247Sdamien
468145247Sdamien	error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
469145247Sdamien	    IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
470145247Sdamien	if (error != 0) {
471145247Sdamien		device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
472145247Sdamien		goto fail;
473145247Sdamien	}
474145247Sdamien
475145247Sdamien	/*
476145247Sdamien	 * Allocate and map rx ring.
477145247Sdamien	 */
478145247Sdamien	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
479145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
480145247Sdamien	    NULL, &sc->rbd_dmat);
481145247Sdamien	if (error != 0) {
482145247Sdamien		device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
483145247Sdamien		goto fail;
484145247Sdamien	}
485145247Sdamien
486145247Sdamien	error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
487145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
488145247Sdamien	if (error != 0) {
489145247Sdamien		device_printf(sc->sc_dev,
490145247Sdamien		    "could not allocate rx ring DMA memory\n");
491145247Sdamien		goto fail;
492145247Sdamien	}
493145247Sdamien
494145247Sdamien	error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
495145247Sdamien	    IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
496145247Sdamien	if (error != 0) {
497145247Sdamien		device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
498145247Sdamien		goto fail;
499145247Sdamien	}
500145247Sdamien
501145247Sdamien	/*
502145247Sdamien	 * Allocate and map status ring.
503145247Sdamien	 */
504145247Sdamien	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
505145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
506145247Sdamien	    NULL, NULL, &sc->status_dmat);
507145247Sdamien	if (error != 0) {
508145247Sdamien		device_printf(sc->sc_dev,
509145247Sdamien		    "could not create status ring DMA tag\n");
510145247Sdamien		goto fail;
511145247Sdamien	}
512145247Sdamien
513145247Sdamien	error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
514145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
515145247Sdamien	if (error != 0) {
516145247Sdamien		device_printf(sc->sc_dev,
517145247Sdamien		    "could not allocate status ring DMA memory\n");
518145247Sdamien		goto fail;
519145247Sdamien	}
520145247Sdamien
521145247Sdamien	error = bus_dmamap_load(sc->status_dmat, sc->status_map,
522145247Sdamien	    sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
523145247Sdamien	    0);
524145247Sdamien	if (error != 0) {
525145247Sdamien		device_printf(sc->sc_dev,
526145247Sdamien		    "could not map status ring DMA memory\n");
527145247Sdamien		goto fail;
528145247Sdamien	}
529145247Sdamien
530145247Sdamien	/*
531145247Sdamien	 * Allocate command DMA map.
532145247Sdamien	 */
533145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
534145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
535145247Sdamien	    sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
536145247Sdamien	if (error != 0) {
537145247Sdamien		device_printf(sc->sc_dev, "could not create command DMA tag\n");
538145247Sdamien		goto fail;
539145247Sdamien	}
540145247Sdamien
541145247Sdamien	error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
542145247Sdamien	if (error != 0) {
543145247Sdamien		device_printf(sc->sc_dev,
544145247Sdamien		    "could not create command DMA map\n");
545145247Sdamien		goto fail;
546145247Sdamien	}
547145247Sdamien
548145247Sdamien	/*
549145247Sdamien	 * Allocate headers DMA maps.
550145247Sdamien	 */
551145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
552145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
553145247Sdamien	    sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
554145247Sdamien	if (error != 0) {
555145247Sdamien		device_printf(sc->sc_dev, "could not create header DMA tag\n");
556145247Sdamien		goto fail;
557145247Sdamien	}
558145247Sdamien
559145247Sdamien	SLIST_INIT(&sc->free_shdr);
560145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
561145247Sdamien		shdr = &sc->shdr_list[i];
562145247Sdamien		error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
563145247Sdamien		if (error != 0) {
564145247Sdamien			device_printf(sc->sc_dev,
565145247Sdamien			    "could not create header DMA map\n");
566145247Sdamien			goto fail;
567145247Sdamien		}
568145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
569145247Sdamien	}
570145247Sdamien
571145247Sdamien	/*
572145247Sdamien	 * Allocate tx buffers DMA maps.
573145247Sdamien	 */
574145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
575145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
576145247Sdamien	    NULL, NULL, &sc->txbuf_dmat);
577145247Sdamien	if (error != 0) {
578145247Sdamien		device_printf(sc->sc_dev, "could not create tx DMA tag\n");
579145247Sdamien		goto fail;
580145247Sdamien	}
581145247Sdamien
582145247Sdamien	SLIST_INIT(&sc->free_sbuf);
583145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
584145247Sdamien		sbuf = &sc->tx_sbuf_list[i];
585145247Sdamien		error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
586145247Sdamien		if (error != 0) {
587145247Sdamien			device_printf(sc->sc_dev,
588145247Sdamien			    "could not create tx DMA map\n");
589145247Sdamien			goto fail;
590145247Sdamien		}
591145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
592145247Sdamien	}
593145247Sdamien
594145247Sdamien	/*
595145247Sdamien	 * Initialize tx ring.
596145247Sdamien	 */
597145247Sdamien	for (i = 0; i < IPW_NTBD; i++) {
598145247Sdamien		sbd = &sc->stbd_list[i];
599145247Sdamien		sbd->bd = &sc->tbd_list[i];
600145247Sdamien		sbd->type = IPW_SBD_TYPE_NOASSOC;
601145247Sdamien	}
602145247Sdamien
603145247Sdamien	/*
604145247Sdamien	 * Pre-allocate rx buffers and DMA maps.
605145247Sdamien	 */
606145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
607147841Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
608147841Sdamien	    NULL, &sc->rxbuf_dmat);
609145247Sdamien	if (error != 0) {
610145247Sdamien		device_printf(sc->sc_dev, "could not create rx DMA tag\n");
611145247Sdamien		goto fail;
612145247Sdamien	}
613145247Sdamien
614145247Sdamien	for (i = 0; i < IPW_NRBD; i++) {
615145247Sdamien		sbd = &sc->srbd_list[i];
616145247Sdamien		sbuf = &sc->rx_sbuf_list[i];
617145247Sdamien		sbd->bd = &sc->rbd_list[i];
618145247Sdamien
619145247Sdamien		sbuf->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
620145247Sdamien		if (sbuf->m == NULL) {
621145247Sdamien			device_printf(sc->sc_dev,
622145247Sdamien			    "could not allocate rx mbuf\n");
623145247Sdamien			error = ENOMEM;
624145247Sdamien			goto fail;
625145247Sdamien		}
626145247Sdamien
627145247Sdamien		error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
628145247Sdamien		if (error != 0) {
629145247Sdamien			device_printf(sc->sc_dev,
630145247Sdamien			    "could not create rx DMA map\n");
631145247Sdamien			goto fail;
632145247Sdamien		}
633145247Sdamien
634145247Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
635145247Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
636145247Sdamien		    &physaddr, 0);
637145247Sdamien		if (error != 0) {
638145247Sdamien			device_printf(sc->sc_dev,
639145247Sdamien			    "could not map rx DMA memory\n");
640145247Sdamien			goto fail;
641145247Sdamien		}
642145247Sdamien
643145247Sdamien		sbd->type = IPW_SBD_TYPE_DATA;
644145247Sdamien		sbd->priv = sbuf;
645145247Sdamien		sbd->bd->physaddr = htole32(physaddr);
646145247Sdamien		sbd->bd->len = htole32(MCLBYTES);
647145247Sdamien	}
648145247Sdamien
649145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
650145247Sdamien
651145247Sdamien	return 0;
652145247Sdamien
653145247Sdamienfail:	ipw_release(sc);
654145247Sdamien	return error;
655145247Sdamien}
656145247Sdamien
657145247Sdamienstatic void
658145247Sdamienipw_release(struct ipw_softc *sc)
659145247Sdamien{
660145247Sdamien	struct ipw_soft_buf *sbuf;
661145247Sdamien	int i;
662145247Sdamien
663145247Sdamien	if (sc->tbd_dmat != NULL) {
664145247Sdamien		if (sc->stbd_list != NULL) {
665145247Sdamien			bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
666145247Sdamien			bus_dmamem_free(sc->tbd_dmat, sc->tbd_list,
667145247Sdamien			    sc->tbd_map);
668145247Sdamien		}
669145247Sdamien		bus_dma_tag_destroy(sc->tbd_dmat);
670145247Sdamien	}
671145247Sdamien
672145247Sdamien	if (sc->rbd_dmat != NULL) {
673145247Sdamien		if (sc->rbd_list != NULL) {
674145247Sdamien			bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
675145247Sdamien			bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
676145247Sdamien			    sc->rbd_map);
677145247Sdamien		}
678145247Sdamien		bus_dma_tag_destroy(sc->rbd_dmat);
679145247Sdamien	}
680145247Sdamien
681145247Sdamien	if (sc->status_dmat != NULL) {
682145247Sdamien		if (sc->status_list != NULL) {
683145247Sdamien			bus_dmamap_unload(sc->status_dmat, sc->status_map);
684145247Sdamien			bus_dmamem_free(sc->status_dmat, sc->status_list,
685145247Sdamien			    sc->status_map);
686145247Sdamien		}
687145247Sdamien		bus_dma_tag_destroy(sc->status_dmat);
688145247Sdamien	}
689145247Sdamien
690145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
691145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
692145247Sdamien
693145247Sdamien	if (sc->cmd_dmat != NULL) {
694145247Sdamien		bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
695145247Sdamien		bus_dma_tag_destroy(sc->cmd_dmat);
696145247Sdamien	}
697145247Sdamien
698145247Sdamien	if (sc->hdr_dmat != NULL) {
699145247Sdamien		for (i = 0; i < IPW_NDATA; i++)
700145247Sdamien			bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
701145247Sdamien		bus_dma_tag_destroy(sc->hdr_dmat);
702145247Sdamien	}
703145247Sdamien
704145247Sdamien	if (sc->txbuf_dmat != NULL) {
705145247Sdamien		for (i = 0; i < IPW_NDATA; i++) {
706145247Sdamien			bus_dmamap_destroy(sc->txbuf_dmat,
707145247Sdamien			    sc->tx_sbuf_list[i].map);
708145247Sdamien		}
709145247Sdamien		bus_dma_tag_destroy(sc->txbuf_dmat);
710145247Sdamien	}
711145247Sdamien
712145247Sdamien	if (sc->rxbuf_dmat != NULL) {
713145247Sdamien		for (i = 0; i < IPW_NRBD; i++) {
714145247Sdamien			sbuf = &sc->rx_sbuf_list[i];
715145247Sdamien			if (sbuf->m != NULL) {
716145247Sdamien				bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
717145247Sdamien				    BUS_DMASYNC_POSTREAD);
718145247Sdamien				bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
719145247Sdamien				m_freem(sbuf->m);
720145247Sdamien			}
721145247Sdamien			bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
722145247Sdamien		}
723145247Sdamien		bus_dma_tag_destroy(sc->rxbuf_dmat);
724145247Sdamien	}
725145247Sdamien}
726145247Sdamien
727145247Sdamienstatic int
728145247Sdamienipw_shutdown(device_t dev)
729145247Sdamien{
730145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
731145247Sdamien
732145247Sdamien	ipw_stop(sc);
733145247Sdamien
734145247Sdamien	return 0;
735145247Sdamien}
736145247Sdamien
737145247Sdamienstatic int
738145247Sdamienipw_suspend(device_t dev)
739145247Sdamien{
740145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
741145247Sdamien
742145247Sdamien	ipw_stop(sc);
743145247Sdamien
744145247Sdamien	return 0;
745145247Sdamien}
746145247Sdamien
747145247Sdamienstatic int
748145247Sdamienipw_resume(device_t dev)
749145247Sdamien{
750145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
751145247Sdamien	struct ifnet *ifp = sc->sc_ic.ic_ifp;
752172567Sthompsa	IPW_LOCK_DECL;
753145247Sdamien
754172567Sthompsa	IPW_LOCK(sc);
755145247Sdamien
756146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
757146498Sdamien
758145247Sdamien	if (ifp->if_flags & IFF_UP) {
759172567Sthompsa		ipw_init_locked(sc, 0);
760148887Srwatson		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
761172567Sthompsa			ipw_start_locked(ifp);
762145247Sdamien	}
763145247Sdamien
764172567Sthompsa	IPW_UNLOCK(sc);
765145247Sdamien
766145247Sdamien	return 0;
767145247Sdamien}
768145247Sdamien
769145247Sdamienstatic int
770145247Sdamienipw_media_change(struct ifnet *ifp)
771145247Sdamien{
772145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
773145247Sdamien	int error;
774172567Sthompsa	IPW_LOCK_DECL;
775145247Sdamien
776172567Sthompsa	IPW_LOCK(sc);
777145247Sdamien	error = ieee80211_media_change(ifp);
778172567Sthompsa	if (error == ENETRESET) {
779172567Sthompsa		if ((ifp->if_flags & IFF_UP) &&
780172567Sthompsa		    (ifp->if_drv_flags & IFF_DRV_RUNNING))
781172567Sthompsa			ipw_init_locked(sc, 0);
782172567Sthompsa		error = 0;
783145247Sdamien	}
784172567Sthompsa	IPW_UNLOCK(sc);
785145247Sdamien
786172567Sthompsa	return (error);
787172567Sthompsa}
788145247Sdamien
789172567Sthompsastatic int
790172567Sthompsaipw_cvtrate(int ipwrate)
791172567Sthompsa{
792172567Sthompsa	switch (ipwrate) {
793172567Sthompsa	case IPW_RATE_DS1:	return 2;
794172567Sthompsa	case IPW_RATE_DS2:	return 4;
795172567Sthompsa	case IPW_RATE_DS5:	return 11;
796172567Sthompsa	case IPW_RATE_DS11:	return 22;
797172567Sthompsa	}
798145247Sdamien	return 0;
799145247Sdamien}
800145247Sdamien
801145247Sdamien/*
802156599Sdamien * The firmware automatically adapts the transmit speed. We report its current
803156599Sdamien * value here.
804145247Sdamien */
805145247Sdamienstatic void
806145247Sdamienipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
807145247Sdamien{
808145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
809145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
810172567Sthompsa	int rate;
811145247Sdamien
812145247Sdamien	imr->ifm_status = IFM_AVALID;
813145247Sdamien	imr->ifm_active = IFM_IEEE80211;
814145247Sdamien	if (ic->ic_state == IEEE80211_S_RUN)
815145247Sdamien		imr->ifm_status |= IFM_ACTIVE;
816145247Sdamien
817145247Sdamien	/* read current transmission rate from adapter */
818172567Sthompsa	rate = ipw_cvtrate(ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
819172567Sthompsa	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
820145247Sdamien
821145247Sdamien	switch (ic->ic_opmode) {
822145247Sdamien	case IEEE80211_M_STA:
823145247Sdamien		break;
824145247Sdamien
825145247Sdamien	case IEEE80211_M_IBSS:
826145247Sdamien		imr->ifm_active |= IFM_IEEE80211_IBSS;
827145247Sdamien		break;
828145247Sdamien
829145247Sdamien	case IEEE80211_M_MONITOR:
830145247Sdamien		imr->ifm_active |= IFM_IEEE80211_MONITOR;
831145247Sdamien		break;
832145247Sdamien
833145247Sdamien	case IEEE80211_M_AHDEMO:
834145247Sdamien	case IEEE80211_M_HOSTAP:
835170530Ssam	case IEEE80211_M_WDS:
836145247Sdamien		/* should not get there */
837145247Sdamien		break;
838145247Sdamien	}
839145247Sdamien}
840145247Sdamien
841145247Sdamienstatic int
842145247Sdamienipw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
843145247Sdamien{
844145247Sdamien	struct ifnet *ifp = ic->ic_ifp;
845145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
846145247Sdamien
847172567Sthompsa	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
848172567Sthompsa		ieee80211_state_name[ic->ic_state],
849172567Sthompsa		ieee80211_state_name[nstate], sc->flags));
850172567Sthompsa
851145247Sdamien	switch (nstate) {
852145247Sdamien	case IEEE80211_S_RUN:
853172567Sthompsa		if (ic->ic_opmode == IEEE80211_M_IBSS) {
854172567Sthompsa			/*
855172567Sthompsa			 * XXX when joining an ibss network we are called
856172567Sthompsa			 * with a SCAN -> RUN transition on scan complete.
857172567Sthompsa			 * Use that to call ipw_auth_and_assoc.  On completing
858172567Sthompsa			 * the join we are then called again with an
859172567Sthompsa			 * AUTH -> RUN transition and we want to do nothing.
860172567Sthompsa			 * This is all totally bogus and needs to be redone.
861172567Sthompsa			 */
862172567Sthompsa			if (ic->ic_state == IEEE80211_S_SCAN)
863172567Sthompsa				taskqueue_enqueue_fast(taskqueue_fast,
864172567Sthompsa				    &sc->sc_assoc_task);
865172567Sthompsa		}
866172567Sthompsa		break;
867145247Sdamien
868172567Sthompsa	case IEEE80211_S_INIT:
869172567Sthompsa		if (sc->flags & IPW_FLAG_ASSOCIATED)
870172567Sthompsa			taskqueue_enqueue_fast(taskqueue_fast,
871172567Sthompsa			    &sc->sc_disassoc_task);
872172567Sthompsa		break;
873145247Sdamien
874172567Sthompsa	case IEEE80211_S_AUTH:
875172567Sthompsa		taskqueue_enqueue_fast(taskqueue_fast, &sc->sc_assoc_task);
876172567Sthompsa		break;
877145247Sdamien
878172567Sthompsa	case IEEE80211_S_ASSOC:
879172567Sthompsa		/*
880172567Sthompsa		 * If we are not transitioning from AUTH the resend the
881172567Sthompsa		 * association request.
882172567Sthompsa		 */
883172567Sthompsa		if (ic->ic_state != IEEE80211_S_AUTH)
884172567Sthompsa			taskqueue_enqueue_fast(taskqueue_fast,
885172567Sthompsa			    &sc->sc_assoc_task);
886145247Sdamien		break;
887145247Sdamien
888172058Ssam	default:
889145247Sdamien		break;
890145247Sdamien	}
891172567Sthompsa	return (*sc->sc_newstate)(ic, nstate, arg);
892145247Sdamien}
893145247Sdamien
894145247Sdamien/*
895145247Sdamien * Read 16 bits at address 'addr' from the serial EEPROM.
896145247Sdamien */
897145247Sdamienstatic uint16_t
898145247Sdamienipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
899145247Sdamien{
900145247Sdamien	uint32_t tmp;
901145247Sdamien	uint16_t val;
902145247Sdamien	int n;
903145247Sdamien
904145247Sdamien	/* clock C once before the first command */
905145247Sdamien	IPW_EEPROM_CTL(sc, 0);
906145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
907145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
908145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
909145247Sdamien
910145247Sdamien	/* write start bit (1) */
911145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
912145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
913145247Sdamien
914145247Sdamien	/* write READ opcode (10) */
915145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
916145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
917145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
918145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
919145247Sdamien
920145247Sdamien	/* write address A7-A0 */
921145247Sdamien	for (n = 7; n >= 0; n--) {
922145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
923145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
924145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
925145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
926145247Sdamien	}
927145247Sdamien
928145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
929145247Sdamien
930145247Sdamien	/* read data Q15-Q0 */
931145247Sdamien	val = 0;
932145247Sdamien	for (n = 15; n >= 0; n--) {
933145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
934145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
935145247Sdamien		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
936145247Sdamien		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
937145247Sdamien	}
938145247Sdamien
939145247Sdamien	IPW_EEPROM_CTL(sc, 0);
940145247Sdamien
941145247Sdamien	/* clear Chip Select and clock C */
942145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
943145247Sdamien	IPW_EEPROM_CTL(sc, 0);
944145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
945145247Sdamien
946145247Sdamien	return le16toh(val);
947145247Sdamien}
948145247Sdamien
949145247Sdamienstatic void
950172567Sthompsaipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
951145247Sdamien{
952145247Sdamien	struct ipw_cmd *cmd;
953145247Sdamien
954145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
955145247Sdamien
956145247Sdamien	cmd = mtod(sbuf->m, struct ipw_cmd *);
957145247Sdamien
958172567Sthompsa	DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
959172567Sthompsa	    ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
960145247Sdamien	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
961145247Sdamien	    le32toh(cmd->status)));
962145247Sdamien
963172567Sthompsa	sc->flags &= ~IPW_FLAG_BUSY;
964145247Sdamien	wakeup(sc);
965145247Sdamien}
966145247Sdamien
967145247Sdamienstatic void
968172567Sthompsaipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
969145247Sdamien{
970172567Sthompsa#define	IEEESTATE(ic)	ieee80211_state_name[ic->ic_state]
971145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
972145247Sdamien	uint32_t state;
973145247Sdamien
974145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
975145247Sdamien
976145247Sdamien	state = le32toh(*mtod(sbuf->m, uint32_t *));
977145247Sdamien
978145247Sdamien	switch (state) {
979145247Sdamien	case IPW_STATE_ASSOCIATED:
980172567Sthompsa		DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
981172567Sthompsa			IEEESTATE(ic), sc->flags));
982172567Sthompsa		sc->flags |= IPW_FLAG_ASSOCIATED;
983172567Sthompsa		/* XXX suppress state change in case the fw auto-associates */
984172567Sthompsa		if (ic->ic_state != IEEE80211_S_ASSOC) {
985172567Sthompsa			DPRINTF(("Unexpected association (state %u)\n",
986172567Sthompsa				ic->ic_state));
987172567Sthompsa		} else
988172567Sthompsa			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
989145247Sdamien		break;
990145247Sdamien
991145247Sdamien	case IPW_STATE_SCANNING:
992172567Sthompsa		DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
993172567Sthompsa			IEEESTATE(ic), sc->flags));
994172567Sthompsa		/*
995172567Sthompsa		 * NB: Check driver state for association on assoc
996172567Sthompsa		 * loss as the firmware will immediately start to
997172567Sthompsa		 * scan and we would treat it as a beacon miss if
998172567Sthompsa		 * we checked the 802.11 layer state.
999172567Sthompsa		 */
1000172567Sthompsa		if (sc->flags & IPW_FLAG_ASSOCIATED)
1001172567Sthompsa			ieee80211_beacon_miss(ic);
1002145247Sdamien		break;
1003145247Sdamien
1004145247Sdamien	case IPW_STATE_SCAN_COMPLETE:
1005172567Sthompsa		/*
1006172567Sthompsa		 * XXX For some reason scan requests generate scan
1007172567Sthompsa		 * started + scan done events before any traffic is
1008172567Sthompsa		 * received (e.g. probe response frames).  We work
1009172567Sthompsa		 * around this by marking the HACK flag and skipping
1010172567Sthompsa		 * the first scan complete event.
1011172567Sthompsa		*/
1012172567Sthompsa		if (sc->flags & IPW_FLAG_HACK) {
1013172567Sthompsa			sc->flags &= ~IPW_FLAG_HACK;
1014172567Sthompsa			break;
1015172567Sthompsa		}
1016172567Sthompsa		DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1017172567Sthompsa			    IEEESTATE(ic), sc->flags));
1018172567Sthompsa		if (sc->flags & IPW_FLAG_SCANNING) {
1019172567Sthompsa			ieee80211_scan_done(ic);
1020172567Sthompsa			sc->flags &= ~IPW_FLAG_SCANNING;
1021172567Sthompsa			sc->sc_scan_timer = 0;
1022172567Sthompsa		}
1023145247Sdamien		break;
1024145247Sdamien
1025145247Sdamien	case IPW_STATE_ASSOCIATION_LOST:
1026172567Sthompsa		DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1027172567Sthompsa			IEEESTATE(ic), sc->flags));
1028172567Sthompsa		sc->flags &= ~IPW_FLAG_ASSOCIATED;
1029172567Sthompsa		if (ic->ic_state == IEEE80211_S_RUN)
1030172567Sthompsa			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1031145247Sdamien		break;
1032145247Sdamien
1033172567Sthompsa	case IPW_STATE_DISABLED:
1034172567Sthompsa		DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1035172567Sthompsa			IEEESTATE(ic), sc->flags));
1036172567Sthompsa		break;
1037172567Sthompsa
1038145247Sdamien	case IPW_STATE_RADIO_DISABLED:
1039172567Sthompsa		DPRINTFN(2, ("Radio off (%s flags 0x%x)\n",
1040172567Sthompsa			IEEESTATE(ic), sc->flags));
1041145247Sdamien		ic->ic_ifp->if_flags &= ~IFF_UP;
1042172567Sthompsa		ipw_stop_locked(sc);
1043145247Sdamien		break;
1044172567Sthompsa
1045172567Sthompsa	default:
1046172567Sthompsa		DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1047172567Sthompsa			__func__, state, IEEESTATE(ic), sc->flags));
1048172567Sthompsa		break;
1049145247Sdamien	}
1050172567Sthompsa#undef IEEESTATE
1051145247Sdamien}
1052145247Sdamien
1053145247Sdamien/*
1054172567Sthompsa * Set driver state for current channel.
1055172567Sthompsa */
1056172567Sthompsastatic void
1057172567Sthompsaipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1058172567Sthompsa{
1059172567Sthompsa	struct ieee80211com *ic = &sc->sc_ic;
1060172567Sthompsa
1061172567Sthompsa	ic->ic_curchan = chan;
1062172567Sthompsa	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
1063172567Sthompsa		htole16(ic->ic_curchan->ic_freq);
1064172567Sthompsa	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
1065172567Sthompsa		htole16(ic->ic_curchan->ic_flags);
1066172567Sthompsa}
1067172567Sthompsa
1068172567Sthompsa/*
1069145247Sdamien * XXX: Hack to set the current channel to the value advertised in beacons or
1070145247Sdamien * probe responses. Only used during AP detection.
1071145247Sdamien */
1072145247Sdamienstatic void
1073172567Sthompsaipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1074145247Sdamien{
1075172567Sthompsa	struct ieee80211com *ic = &sc->sc_ic;
1076172567Sthompsa	struct ieee80211_channel *c;
1077145247Sdamien	struct ieee80211_frame *wh;
1078145247Sdamien	uint8_t subtype;
1079145247Sdamien	uint8_t *frm, *efrm;
1080145247Sdamien
1081145247Sdamien	wh = mtod(m, struct ieee80211_frame *);
1082145247Sdamien
1083145247Sdamien	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1084145247Sdamien		return;
1085145247Sdamien
1086145247Sdamien	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1087145247Sdamien
1088145247Sdamien	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1089145247Sdamien	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1090145247Sdamien		return;
1091145247Sdamien
1092145247Sdamien	frm = (uint8_t *)(wh + 1);
1093145247Sdamien	efrm = mtod(m, uint8_t *) + m->m_len;
1094145247Sdamien
1095145247Sdamien	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1096145247Sdamien	while (frm < efrm) {
1097145247Sdamien		if (*frm == IEEE80211_ELEMID_DSPARMS)
1098145247Sdamien#if IEEE80211_CHAN_MAX < 255
1099145247Sdamien		if (frm[2] <= IEEE80211_CHAN_MAX)
1100145247Sdamien#endif
1101172567Sthompsa		{
1102172567Sthompsa			DPRINTF(("Fixing channel to %d\n", frm[2]));
1103172567Sthompsa			c = ieee80211_find_channel(ic,
1104170530Ssam				ieee80211_ieee2mhz(frm[2], 0),
1105172567Sthompsa				IEEE80211_CHAN_B);
1106172567Sthompsa			if (c == NULL)
1107172567Sthompsa				c = &ic->ic_channels[0];
1108172567Sthompsa			ipw_setcurchan(sc, c);
1109172567Sthompsa		}
1110145247Sdamien
1111145247Sdamien		frm += frm[1] + 2;
1112145247Sdamien	}
1113145247Sdamien}
1114145247Sdamien
1115145247Sdamienstatic void
1116172567Sthompsaipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1117145247Sdamien    struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1118145247Sdamien{
1119145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
1120145247Sdamien	struct ifnet *ifp = ic->ic_ifp;
1121152385Sdamien	struct mbuf *mnew, *m;
1122145247Sdamien	struct ieee80211_frame *wh;
1123145247Sdamien	struct ieee80211_node *ni;
1124145247Sdamien	bus_addr_t physaddr;
1125145247Sdamien	int error;
1126172567Sthompsa	IPW_LOCK_DECL;
1127145247Sdamien
1128152385Sdamien	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1129152385Sdamien	    status->rssi));
1130152385Sdamien
1131147757Sdamien	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1132147757Sdamien	    le32toh(status->len) > MCLBYTES)
1133147757Sdamien		return;
1134147757Sdamien
1135152385Sdamien	/*
1136152385Sdamien	 * Try to allocate a new mbuf for this ring element and load it before
1137152385Sdamien	 * processing the current mbuf. If the ring element cannot be loaded,
1138152385Sdamien	 * drop the received packet and reuse the old mbuf. In the unlikely
1139152385Sdamien	 * case that the old mbuf can't be reloaded either, explicitly panic.
1140152385Sdamien	 */
1141152385Sdamien	mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1142152385Sdamien	if (mnew == NULL) {
1143152385Sdamien		ifp->if_ierrors++;
1144152385Sdamien		return;
1145152385Sdamien	}
1146152385Sdamien
1147145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1148145247Sdamien	bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1149145247Sdamien
1150152385Sdamien	error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1151152385Sdamien	    MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1152152385Sdamien	if (error != 0) {
1153152385Sdamien		m_freem(mnew);
1154152385Sdamien
1155152385Sdamien		/* try to reload the old mbuf */
1156152385Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1157152385Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1158152385Sdamien		    &physaddr, 0);
1159152385Sdamien		if (error != 0) {
1160152385Sdamien			/* very unlikely that it will fail... */
1161152385Sdamien			panic("%s: could not load old rx mbuf",
1162152385Sdamien			    device_get_name(sc->sc_dev));
1163152385Sdamien		}
1164152385Sdamien		ifp->if_ierrors++;
1165152385Sdamien		return;
1166152385Sdamien	}
1167152385Sdamien
1168152385Sdamien	/*
1169152385Sdamien	 * New mbuf successfully loaded, update Rx ring and continue
1170152385Sdamien	 * processing.
1171152385Sdamien	 */
1172152385Sdamien	m = sbuf->m;
1173152385Sdamien	sbuf->m = mnew;
1174152385Sdamien	sbd->bd->physaddr = htole32(physaddr);
1175152385Sdamien
1176145247Sdamien	/* finalize mbuf */
1177145247Sdamien	m->m_pkthdr.rcvif = ifp;
1178145247Sdamien	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1179145247Sdamien
1180159180Scsjp	if (bpf_peers_present(sc->sc_drvbpf)) {
1181145247Sdamien		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1182145247Sdamien
1183145247Sdamien		tap->wr_flags = 0;
1184172567Sthompsa		tap->wr_antsignal = status->rssi + IPW_RSSI_TO_DBM;
1185156599Sdamien		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1186156599Sdamien		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1187145247Sdamien
1188145247Sdamien		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1189145247Sdamien	}
1190145247Sdamien
1191172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
1192172567Sthompsa		ipw_fix_channel(sc, m);
1193145247Sdamien
1194145247Sdamien	wh = mtod(m, struct ieee80211_frame *);
1195172567Sthompsa	IPW_UNLOCK(sc);
1196145247Sdamien	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1197145247Sdamien
1198145247Sdamien	/* send the frame to the 802.11 layer */
1199170530Ssam	ieee80211_input(ic, m, ni, status->rssi, -95/*XXX*/, 0);
1200145247Sdamien
1201145247Sdamien	/* node is no longer needed */
1202145247Sdamien	ieee80211_free_node(ni);
1203172567Sthompsa	IPW_LOCK(sc);
1204145247Sdamien
1205145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1206145247Sdamien}
1207145247Sdamien
1208145247Sdamienstatic void
1209145247Sdamienipw_rx_intr(struct ipw_softc *sc)
1210145247Sdamien{
1211172567Sthompsa	struct ieee80211com *ic = &sc->sc_ic;
1212145247Sdamien	struct ipw_status *status;
1213145247Sdamien	struct ipw_soft_bd *sbd;
1214145247Sdamien	struct ipw_soft_buf *sbuf;
1215145247Sdamien	uint32_t r, i;
1216145247Sdamien
1217145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1218145247Sdamien		return;
1219145247Sdamien
1220145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1221145247Sdamien
1222145247Sdamien	bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1223145247Sdamien
1224145247Sdamien	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1225145247Sdamien		status = &sc->status_list[i];
1226145247Sdamien		sbd = &sc->srbd_list[i];
1227145247Sdamien		sbuf = sbd->priv;
1228145247Sdamien
1229145247Sdamien		switch (le16toh(status->code) & 0xf) {
1230145247Sdamien		case IPW_STATUS_CODE_COMMAND:
1231172567Sthompsa			ipw_rx_cmd_intr(sc, sbuf);
1232145247Sdamien			break;
1233145247Sdamien
1234145247Sdamien		case IPW_STATUS_CODE_NEWSTATE:
1235172567Sthompsa			ipw_rx_newstate_intr(sc, sbuf);
1236145247Sdamien			break;
1237145247Sdamien
1238145247Sdamien		case IPW_STATUS_CODE_DATA_802_3:
1239145247Sdamien		case IPW_STATUS_CODE_DATA_802_11:
1240172567Sthompsa			ipw_rx_data_intr(sc, status, sbd, sbuf);
1241145247Sdamien			break;
1242145247Sdamien
1243145247Sdamien		case IPW_STATUS_CODE_NOTIFICATION:
1244172567Sthompsa			DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1245172567Sthompsa			    le32toh(status->len), status->flags));
1246172567Sthompsa			if (ic->ic_state == IEEE80211_S_AUTH) {
1247172567Sthompsa				/* XXX assume auth notification */
1248172567Sthompsa				ieee80211_node_authorize(ic->ic_bss);
1249172567Sthompsa				ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1250172567Sthompsa			}
1251145247Sdamien			break;
1252145247Sdamien
1253145247Sdamien		default:
1254172567Sthompsa			device_printf(sc->sc_dev, "unexpected status code %u\n",
1255145247Sdamien			    le16toh(status->code));
1256145247Sdamien		}
1257145247Sdamien
1258145247Sdamien		/* firmware was killed, stop processing received frames */
1259145247Sdamien		if (!(sc->flags & IPW_FLAG_FW_INITED))
1260145247Sdamien			return;
1261145247Sdamien
1262145247Sdamien		sbd->bd->flags = 0;
1263145247Sdamien	}
1264145247Sdamien
1265145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1266145247Sdamien
1267145247Sdamien	/* kick the firmware */
1268145247Sdamien	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1269145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1270145247Sdamien}
1271145247Sdamien
1272145247Sdamienstatic void
1273145247Sdamienipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1274145247Sdamien{
1275145247Sdamien	struct ipw_soft_hdr *shdr;
1276145247Sdamien	struct ipw_soft_buf *sbuf;
1277145247Sdamien
1278145247Sdamien	switch (sbd->type) {
1279145247Sdamien	case IPW_SBD_TYPE_COMMAND:
1280145247Sdamien		bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1281145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1282145247Sdamien		bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1283145247Sdamien		break;
1284145247Sdamien
1285145247Sdamien	case IPW_SBD_TYPE_HEADER:
1286145247Sdamien		shdr = sbd->priv;
1287145247Sdamien		bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1288145247Sdamien		bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1289145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1290145247Sdamien		break;
1291145247Sdamien
1292145247Sdamien	case IPW_SBD_TYPE_DATA:
1293145247Sdamien		sbuf = sbd->priv;
1294145247Sdamien		bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1295145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1296145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1297145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1298145247Sdamien
1299170530Ssam		if (sbuf->m->m_flags & M_TXCB)
1300170530Ssam			ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1301145247Sdamien		m_freem(sbuf->m);
1302145247Sdamien		ieee80211_free_node(sbuf->ni);
1303145247Sdamien
1304145247Sdamien		sc->sc_tx_timer = 0;
1305145247Sdamien		break;
1306145247Sdamien	}
1307145247Sdamien
1308145247Sdamien	sbd->type = IPW_SBD_TYPE_NOASSOC;
1309145247Sdamien}
1310145247Sdamien
1311145247Sdamienstatic void
1312145247Sdamienipw_tx_intr(struct ipw_softc *sc)
1313145247Sdamien{
1314145247Sdamien	struct ifnet *ifp = sc->sc_ic.ic_ifp;
1315145247Sdamien	struct ipw_soft_bd *sbd;
1316145247Sdamien	uint32_t r, i;
1317145247Sdamien
1318145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1319145247Sdamien		return;
1320145247Sdamien
1321145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1322145247Sdamien
1323145247Sdamien	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1324145247Sdamien		sbd = &sc->stbd_list[i];
1325145247Sdamien
1326145247Sdamien		if (sbd->type == IPW_SBD_TYPE_DATA)
1327145247Sdamien			ifp->if_opackets++;
1328145247Sdamien
1329145247Sdamien		ipw_release_sbd(sc, sbd);
1330145247Sdamien		sc->txfree++;
1331145247Sdamien	}
1332145247Sdamien
1333145247Sdamien	/* remember what the firmware has processed */
1334145247Sdamien	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1335145247Sdamien
1336148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1337172567Sthompsa	ipw_start_locked(ifp);
1338145247Sdamien}
1339145247Sdamien
1340145247Sdamienstatic void
1341145247Sdamienipw_intr(void *arg)
1342145247Sdamien{
1343145247Sdamien	struct ipw_softc *sc = arg;
1344145247Sdamien	uint32_t r;
1345172567Sthompsa	IPW_LOCK_DECL;
1346145247Sdamien
1347172567Sthompsa	IPW_LOCK(sc);
1348145247Sdamien
1349145247Sdamien	if ((r = CSR_READ_4(sc, IPW_CSR_INTR)) == 0 || r == 0xffffffff) {
1350172567Sthompsa		IPW_UNLOCK(sc);
1351145247Sdamien		return;
1352145247Sdamien	}
1353145247Sdamien
1354145247Sdamien	/* disable interrupts */
1355145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1356145247Sdamien
1357156599Sdamien	/* acknowledge all interrupts */
1358156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1359156599Sdamien
1360145247Sdamien	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1361156599Sdamien		device_printf(sc->sc_dev, "firmware error\n");
1362156599Sdamien		taskqueue_enqueue_fast(taskqueue_fast, &sc->sc_init_task);
1363156599Sdamien		r = 0;	/* don't process more interrupts */
1364145247Sdamien	}
1365145247Sdamien
1366156599Sdamien	if (r & IPW_INTR_FW_INIT_DONE)
1367156599Sdamien		wakeup(sc);
1368145247Sdamien
1369145247Sdamien	if (r & IPW_INTR_RX_TRANSFER)
1370145247Sdamien		ipw_rx_intr(sc);
1371145247Sdamien
1372145247Sdamien	if (r & IPW_INTR_TX_TRANSFER)
1373145247Sdamien		ipw_tx_intr(sc);
1374145247Sdamien
1375145247Sdamien	/* re-enable interrupts */
1376145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1377145247Sdamien
1378172567Sthompsa	IPW_UNLOCK(sc);
1379145247Sdamien}
1380145247Sdamien
1381145247Sdamienstatic void
1382145247Sdamienipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1383145247Sdamien{
1384145247Sdamien	if (error != 0)
1385145247Sdamien		return;
1386145247Sdamien
1387145247Sdamien	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1388145247Sdamien
1389145247Sdamien	*(bus_addr_t *)arg = segs[0].ds_addr;
1390145247Sdamien}
1391145247Sdamien
1392172567Sthompsastatic const char *
1393172567Sthompsaipw_cmdname(int cmd)
1394172567Sthompsa{
1395172567Sthompsa#define	N(a)	(sizeof(a) / sizeof(a[0]))
1396172567Sthompsa	static const struct {
1397172567Sthompsa		int	cmd;
1398172567Sthompsa		const char *name;
1399172567Sthompsa	} cmds[] = {
1400172567Sthompsa		{ IPW_CMD_ADD_MULTICAST,	"ADD_MULTICAST" },
1401172567Sthompsa		{ IPW_CMD_BROADCAST_SCAN,	"BROADCAST_SCAN" },
1402172567Sthompsa		{ IPW_CMD_DISABLE,		"DISABLE" },
1403172567Sthompsa		{ IPW_CMD_DISABLE_PHY,		"DISABLE_PHY" },
1404172567Sthompsa		{ IPW_CMD_ENABLE,		"ENABLE" },
1405172567Sthompsa		{ IPW_CMD_PREPARE_POWER_DOWN,	"PREPARE_POWER_DOWN" },
1406172567Sthompsa		{ IPW_CMD_SET_BASIC_TX_RATES,	"SET_BASIC_TX_RATES" },
1407172567Sthompsa		{ IPW_CMD_SET_BEACON_INTERVAL,	"SET_BEACON_INTERVAL" },
1408172567Sthompsa		{ IPW_CMD_SET_CHANNEL,		"SET_CHANNEL" },
1409172567Sthompsa		{ IPW_CMD_SET_CONFIGURATION,	"SET_CONFIGURATION" },
1410172567Sthompsa		{ IPW_CMD_SET_DESIRED_BSSID,	"SET_DESIRED_BSSID" },
1411172567Sthompsa		{ IPW_CMD_SET_ESSID,		"SET_ESSID" },
1412172567Sthompsa		{ IPW_CMD_SET_FRAG_THRESHOLD,	"SET_FRAG_THRESHOLD" },
1413172567Sthompsa		{ IPW_CMD_SET_MAC_ADDRESS,	"SET_MAC_ADDRESS" },
1414172567Sthompsa		{ IPW_CMD_SET_MANDATORY_BSSID,	"SET_MANDATORY_BSSID" },
1415172567Sthompsa		{ IPW_CMD_SET_MODE,		"SET_MODE" },
1416172567Sthompsa		{ IPW_CMD_SET_MSDU_TX_RATES,	"SET_MSDU_TX_RATES" },
1417172567Sthompsa		{ IPW_CMD_SET_POWER_MODE,	"SET_POWER_MODE" },
1418172567Sthompsa		{ IPW_CMD_SET_RTS_THRESHOLD,	"SET_RTS_THRESHOLD" },
1419172567Sthompsa		{ IPW_CMD_SET_SCAN_OPTIONS,	"SET_SCAN_OPTIONS" },
1420172567Sthompsa		{ IPW_CMD_SET_SECURITY_INFO,	"SET_SECURITY_INFO" },
1421172567Sthompsa		{ IPW_CMD_SET_TX_POWER_INDEX,	"SET_TX_POWER_INDEX" },
1422172567Sthompsa		{ IPW_CMD_SET_TX_RATES,		"SET_TX_RATES" },
1423172567Sthompsa		{ IPW_CMD_SET_WEP_FLAGS,	"SET_WEP_FLAGS" },
1424172567Sthompsa		{ IPW_CMD_SET_WEP_KEY,		"SET_WEP_KEY" },
1425172567Sthompsa		{ IPW_CMD_SET_WEP_KEY_INDEX,	"SET_WEP_KEY_INDEX" },
1426172567Sthompsa		{ IPW_CMD_SET_WPA_IE,		"SET_WPA_IE" },
1427172567Sthompsa
1428172567Sthompsa	};
1429172567Sthompsa	static char buf[12];
1430172567Sthompsa	int i;
1431172567Sthompsa
1432172567Sthompsa	for (i = 0; i < N(cmds); i++)
1433172567Sthompsa		if (cmds[i].cmd == cmd)
1434172567Sthompsa			return cmds[i].name;
1435172567Sthompsa	snprintf(buf, sizeof(buf), "%u", cmd);
1436172567Sthompsa	return buf;
1437172567Sthompsa#undef N
1438172567Sthompsa}
1439172567Sthompsa
1440145247Sdamien/*
1441145247Sdamien * Send a command to the firmware and wait for the acknowledgement.
1442145247Sdamien */
1443145247Sdamienstatic int
1444145247Sdamienipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1445145247Sdamien{
1446145247Sdamien	struct ipw_soft_bd *sbd;
1447145247Sdamien	bus_addr_t physaddr;
1448145247Sdamien	int error;
1449145247Sdamien
1450172567Sthompsa	if (sc->flags & IPW_FLAG_BUSY) {
1451172567Sthompsa		device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1452172567Sthompsa			__func__, ipw_cmdname(type));
1453172567Sthompsa		return EAGAIN;
1454172567Sthompsa	}
1455172567Sthompsa	sc->flags |= IPW_FLAG_BUSY;
1456172567Sthompsa
1457145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1458145247Sdamien
1459145247Sdamien	error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1460145247Sdamien	    sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1461145247Sdamien	if (error != 0) {
1462145247Sdamien		device_printf(sc->sc_dev, "could not map command DMA memory\n");
1463172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1464145247Sdamien		return error;
1465145247Sdamien	}
1466145247Sdamien
1467145247Sdamien	sc->cmd.type = htole32(type);
1468145247Sdamien	sc->cmd.subtype = 0;
1469145247Sdamien	sc->cmd.len = htole32(len);
1470145247Sdamien	sc->cmd.seq = 0;
1471152637Sdamien	memcpy(sc->cmd.data, data, len);
1472145247Sdamien
1473145247Sdamien	sbd->type = IPW_SBD_TYPE_COMMAND;
1474145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1475145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1476145247Sdamien	sbd->bd->nfrag = 1;
1477145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1478145247Sdamien	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1479145247Sdamien
1480145247Sdamien	bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1481145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1482145247Sdamien
1483172567Sthompsa#ifdef IPW_DEBUG
1484172567Sthompsa	if (ipw_debug >= 4) {
1485172567Sthompsa		printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1486172567Sthompsa		    0, 0, len);
1487172567Sthompsa		/* Print the data buffer in the higher debug level */
1488172567Sthompsa		if (ipw_debug >= 9 && len > 0) {
1489172567Sthompsa			printf(" data: 0x");
1490172567Sthompsa			for (int i = 1; i <= len; i++)
1491172567Sthompsa				printf("%1D", (char *)data + len - i, "");
1492172567Sthompsa		}
1493172567Sthompsa		printf("\n");
1494172567Sthompsa	}
1495172567Sthompsa#endif
1496145247Sdamien
1497145247Sdamien	/* kick firmware */
1498145247Sdamien	sc->txfree--;
1499145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1500145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1501145247Sdamien
1502145247Sdamien	/* wait at most one second for command to complete */
1503172567Sthompsa	error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1504172567Sthompsa	if (error != 0) {
1505172567Sthompsa		device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1506172567Sthompsa		    __func__, ipw_cmdname(type), error);
1507172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1508172567Sthompsa		return (error);
1509172567Sthompsa	}
1510172567Sthompsa	return (0);
1511145247Sdamien}
1512145247Sdamien
1513145247Sdamienstatic int
1514145247Sdamienipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1515145247Sdamien{
1516145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1517145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
1518145247Sdamien	struct ieee80211_frame *wh;
1519145247Sdamien	struct ipw_soft_bd *sbd;
1520145247Sdamien	struct ipw_soft_hdr *shdr;
1521145247Sdamien	struct ipw_soft_buf *sbuf;
1522145247Sdamien	struct ieee80211_key *k;
1523145247Sdamien	struct mbuf *mnew;
1524145247Sdamien	bus_dma_segment_t segs[IPW_MAX_NSEG];
1525145247Sdamien	bus_addr_t physaddr;
1526145247Sdamien	int nsegs, error, i;
1527145247Sdamien
1528145247Sdamien	wh = mtod(m0, struct ieee80211_frame *);
1529145247Sdamien
1530145247Sdamien	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1531145247Sdamien		k = ieee80211_crypto_encap(ic, ni, m0);
1532147806Ssam		if (k == NULL) {
1533147806Ssam			m_freem(m0);
1534145247Sdamien			return ENOBUFS;
1535147806Ssam		}
1536145247Sdamien
1537145247Sdamien		/* packet header may have moved, reset our local pointer */
1538145247Sdamien		wh = mtod(m0, struct ieee80211_frame *);
1539145247Sdamien	}
1540145247Sdamien
1541159180Scsjp	if (bpf_peers_present(sc->sc_drvbpf)) {
1542145247Sdamien		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1543145247Sdamien
1544145247Sdamien		tap->wt_flags = 0;
1545156599Sdamien		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1546156599Sdamien		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1547145247Sdamien
1548145247Sdamien		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1549145247Sdamien	}
1550145247Sdamien
1551145247Sdamien	shdr = SLIST_FIRST(&sc->free_shdr);
1552145247Sdamien	sbuf = SLIST_FIRST(&sc->free_sbuf);
1553145247Sdamien	KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1554145247Sdamien
1555145247Sdamien	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1556145247Sdamien	shdr->hdr.subtype = 0;
1557145247Sdamien	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1558145247Sdamien	shdr->hdr.encrypt = 0;
1559145247Sdamien	shdr->hdr.keyidx = 0;
1560145247Sdamien	shdr->hdr.keysz = 0;
1561145247Sdamien	shdr->hdr.fragmentsz = 0;
1562145247Sdamien	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1563145247Sdamien	if (ic->ic_opmode == IEEE80211_M_STA)
1564145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1565145247Sdamien	else
1566145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1567145247Sdamien
1568145247Sdamien	/* trim IEEE802.11 header */
1569145247Sdamien	m_adj(m0, sizeof (struct ieee80211_frame));
1570145247Sdamien
1571145247Sdamien	error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1572145247Sdamien	    &nsegs, 0);
1573145247Sdamien	if (error != 0 && error != EFBIG) {
1574145247Sdamien		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1575145247Sdamien		    error);
1576145247Sdamien		m_freem(m0);
1577145247Sdamien		return error;
1578145247Sdamien	}
1579145247Sdamien	if (error != 0) {
1580145247Sdamien		mnew = m_defrag(m0, M_DONTWAIT);
1581145247Sdamien		if (mnew == NULL) {
1582145247Sdamien			device_printf(sc->sc_dev,
1583145247Sdamien			    "could not defragment mbuf\n");
1584145247Sdamien			m_freem(m0);
1585145247Sdamien			return ENOBUFS;
1586145247Sdamien		}
1587145247Sdamien		m0 = mnew;
1588145247Sdamien
1589145247Sdamien		error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1590145247Sdamien		    segs, &nsegs, 0);
1591145247Sdamien		if (error != 0) {
1592145247Sdamien			device_printf(sc->sc_dev,
1593145247Sdamien			    "could not map mbuf (error %d)\n", error);
1594145247Sdamien			m_freem(m0);
1595145247Sdamien			return error;
1596145247Sdamien		}
1597145247Sdamien	}
1598145247Sdamien
1599145247Sdamien	error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1600145247Sdamien	    sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1601145247Sdamien	if (error != 0) {
1602145247Sdamien		device_printf(sc->sc_dev, "could not map header DMA memory\n");
1603145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1604145247Sdamien		m_freem(m0);
1605145247Sdamien		return error;
1606145247Sdamien	}
1607145247Sdamien
1608145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1609145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1610145247Sdamien
1611145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1612145247Sdamien	sbd->type = IPW_SBD_TYPE_HEADER;
1613145247Sdamien	sbd->priv = shdr;
1614145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1615145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1616145247Sdamien	sbd->bd->nfrag = 1 + nsegs;
1617145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1618145247Sdamien	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1619145247Sdamien
1620145247Sdamien	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1621145247Sdamien	    shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1622145247Sdamien	    shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1623145247Sdamien	    ":"));
1624145247Sdamien
1625145247Sdamien	sc->txfree--;
1626145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1627145247Sdamien
1628145247Sdamien	sbuf->m = m0;
1629145247Sdamien	sbuf->ni = ni;
1630145247Sdamien
1631145247Sdamien	for (i = 0; i < nsegs; i++) {
1632145247Sdamien		sbd = &sc->stbd_list[sc->txcur];
1633145247Sdamien
1634145247Sdamien		sbd->bd->physaddr = htole32(segs[i].ds_addr);
1635145247Sdamien		sbd->bd->len = htole32(segs[i].ds_len);
1636145247Sdamien		sbd->bd->nfrag = 0;
1637145247Sdamien		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1638145247Sdamien		if (i == nsegs - 1) {
1639145247Sdamien			sbd->type = IPW_SBD_TYPE_DATA;
1640145247Sdamien			sbd->priv = sbuf;
1641145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1642145247Sdamien		} else {
1643145247Sdamien			sbd->type = IPW_SBD_TYPE_NOASSOC;
1644145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1645145247Sdamien		}
1646145247Sdamien
1647172574Sthompsa		DPRINTFN(5, ("sending fragment (%d)\n", i));
1648145247Sdamien
1649145247Sdamien		sc->txfree--;
1650145247Sdamien		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1651145247Sdamien	}
1652145247Sdamien
1653145247Sdamien	bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1654145247Sdamien	bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1655145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1656145247Sdamien
1657145247Sdamien	/* kick firmware */
1658145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1659145247Sdamien
1660145247Sdamien	return 0;
1661145247Sdamien}
1662145247Sdamien
1663145247Sdamienstatic void
1664145247Sdamienipw_start(struct ifnet *ifp)
1665145247Sdamien{
1666145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1667172567Sthompsa	IPW_LOCK_DECL;
1668172567Sthompsa
1669172567Sthompsa	IPW_LOCK(sc);
1670172567Sthompsa	ipw_start_locked(ifp);
1671172567Sthompsa	IPW_UNLOCK(sc);
1672172567Sthompsa}
1673172567Sthompsa
1674172567Sthompsastatic void
1675172567Sthompsaipw_start_locked(struct ifnet *ifp)
1676172567Sthompsa{
1677172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
1678145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
1679145247Sdamien	struct mbuf *m0;
1680145247Sdamien	struct ether_header *eh;
1681145247Sdamien	struct ieee80211_node *ni;
1682145247Sdamien
1683172567Sthompsa	IPW_LOCK_ASSERT(sc);
1684145247Sdamien
1685172567Sthompsa	if (ic->ic_state != IEEE80211_S_RUN)
1686145247Sdamien		return;
1687145247Sdamien
1688145247Sdamien	for (;;) {
1689145247Sdamien		IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
1690145247Sdamien		if (m0 == NULL)
1691145247Sdamien			break;
1692145247Sdamien
1693145247Sdamien		if (sc->txfree < 1 + IPW_MAX_NSEG) {
1694145247Sdamien			IFQ_DRV_PREPEND(&ifp->if_snd, m0);
1695148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1696145247Sdamien			break;
1697145247Sdamien		}
1698145247Sdamien
1699145247Sdamien		if (m0->m_len < sizeof (struct ether_header) &&
1700145247Sdamien		    (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL)
1701145247Sdamien			continue;
1702145247Sdamien
1703145247Sdamien		eh = mtod(m0, struct ether_header *);
1704145247Sdamien		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1705145247Sdamien		if (ni == NULL) {
1706145247Sdamien			m_freem(m0);
1707145247Sdamien			continue;
1708145247Sdamien		}
1709145247Sdamien		BPF_MTAP(ifp, m0);
1710145247Sdamien
1711145247Sdamien		m0 = ieee80211_encap(ic, m0, ni);
1712145247Sdamien		if (m0 == NULL) {
1713145247Sdamien			ieee80211_free_node(ni);
1714145247Sdamien			continue;
1715145247Sdamien		}
1716145247Sdamien
1717159183Ssam		if (bpf_peers_present(ic->ic_rawbpf))
1718145247Sdamien			bpf_mtap(ic->ic_rawbpf, m0);
1719145247Sdamien
1720145247Sdamien		if (ipw_tx_start(ifp, m0, ni) != 0) {
1721145247Sdamien			ieee80211_free_node(ni);
1722145247Sdamien			ifp->if_oerrors++;
1723145247Sdamien			break;
1724145247Sdamien		}
1725145247Sdamien
1726145247Sdamien		/* start watchdog timer */
1727145247Sdamien		sc->sc_tx_timer = 5;
1728145247Sdamien	}
1729145247Sdamien}
1730145247Sdamien
1731145247Sdamienstatic void
1732172567Sthompsaipw_watchdog(void *arg)
1733145247Sdamien{
1734172567Sthompsa	struct ipw_softc *sc = arg;
1735172567Sthompsa	struct ieee80211com *ic = &sc->sc_ic;
1736172567Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1737145247Sdamien
1738172567Sthompsa	IPW_LOCK_ASSERT(sc);
1739156599Sdamien
1740145247Sdamien	if (sc->sc_tx_timer > 0) {
1741145247Sdamien		if (--sc->sc_tx_timer == 0) {
1742145247Sdamien			if_printf(ifp, "device timeout\n");
1743145247Sdamien			ifp->if_oerrors++;
1744156599Sdamien			taskqueue_enqueue_fast(taskqueue_fast,
1745156599Sdamien			    &sc->sc_init_task);
1746145247Sdamien		}
1747145247Sdamien	}
1748172567Sthompsa	if (sc->sc_scan_timer > 0) {
1749172567Sthompsa		if (--sc->sc_scan_timer == 0) {
1750172567Sthompsa			DPRINTFN(3, ("Scan timeout\n"));
1751172567Sthompsa			/* End the scan */
1752172567Sthompsa			if (sc->flags & IPW_FLAG_SCANNING) {
1753172567Sthompsa				ieee80211_scan_done(ic);
1754172567Sthompsa				sc->flags &= ~IPW_FLAG_SCANNING;
1755172567Sthompsa			}
1756172567Sthompsa		}
1757172567Sthompsa	}
1758172567Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1759172567Sthompsa		callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1760145247Sdamien}
1761145247Sdamien
1762145247Sdamienstatic int
1763145247Sdamienipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1764145247Sdamien{
1765145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1766145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
1767145247Sdamien	int error = 0;
1768172567Sthompsa	IPW_LOCK_DECL;
1769145247Sdamien
1770172567Sthompsa	IPW_LOCK(sc);
1771145247Sdamien
1772145247Sdamien	switch (cmd) {
1773145247Sdamien	case SIOCSIFFLAGS:
1774145247Sdamien		if (ifp->if_flags & IFF_UP) {
1775148887Srwatson			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1776172567Sthompsa				ipw_init_locked(sc, 0);
1777145247Sdamien		} else {
1778148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1779172567Sthompsa				ipw_stop_locked(sc);
1780145247Sdamien		}
1781145247Sdamien		break;
1782145247Sdamien
1783145247Sdamien	default:
1784145247Sdamien		error = ieee80211_ioctl(ic, cmd, data);
1785145247Sdamien	}
1786145247Sdamien
1787145247Sdamien	if (error == ENETRESET) {
1788148887Srwatson		if ((ifp->if_flags & IFF_UP) &&
1789172567Sthompsa		    (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1790172567Sthompsa		    (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1791172567Sthompsa			ipw_init_locked(sc, 0);
1792145247Sdamien		error = 0;
1793145247Sdamien	}
1794145247Sdamien
1795172567Sthompsa	IPW_UNLOCK(sc);
1796145247Sdamien
1797145247Sdamien	return error;
1798145247Sdamien}
1799145247Sdamien
1800145247Sdamienstatic void
1801145247Sdamienipw_stop_master(struct ipw_softc *sc)
1802145247Sdamien{
1803156599Sdamien	uint32_t tmp;
1804145247Sdamien	int ntries;
1805145247Sdamien
1806145247Sdamien	/* disable interrupts */
1807145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1808145247Sdamien
1809145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1810145247Sdamien	for (ntries = 0; ntries < 50; ntries++) {
1811145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1812145247Sdamien			break;
1813145247Sdamien		DELAY(10);
1814145247Sdamien	}
1815145247Sdamien	if (ntries == 50)
1816145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for master\n");
1817145247Sdamien
1818156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1819156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1820145247Sdamien
1821172567Sthompsa	/* Clear all flags except the following */
1822172567Sthompsa	sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1823145247Sdamien}
1824145247Sdamien
1825145247Sdamienstatic int
1826145247Sdamienipw_reset(struct ipw_softc *sc)
1827145247Sdamien{
1828156599Sdamien	uint32_t tmp;
1829145247Sdamien	int ntries;
1830145247Sdamien
1831145247Sdamien	ipw_stop_master(sc);
1832145247Sdamien
1833145247Sdamien	/* move adapter to D0 state */
1834156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1835156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1836145247Sdamien
1837145247Sdamien	/* wait for clock stabilization */
1838145247Sdamien	for (ntries = 0; ntries < 1000; ntries++) {
1839145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1840145247Sdamien			break;
1841145247Sdamien		DELAY(200);
1842145247Sdamien	}
1843145247Sdamien	if (ntries == 1000)
1844145247Sdamien		return EIO;
1845145247Sdamien
1846156599Sdamien	tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1847156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1848145247Sdamien
1849145247Sdamien	DELAY(10);
1850145247Sdamien
1851156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1852156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1853145247Sdamien
1854145247Sdamien	return 0;
1855145247Sdamien}
1856145247Sdamien
1857172567Sthompsastatic int
1858172567Sthompsaipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1859172567Sthompsa{
1860172567Sthompsa	int ms = hz < 1000 ? 1 : hz/10;
1861172567Sthompsa	int i, error;
1862172567Sthompsa
1863172567Sthompsa	for (i = 0; i < 100; i++) {
1864172567Sthompsa		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1865172567Sthompsa			return 0;
1866172567Sthompsa		error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1867172567Sthompsa		if (error == 0 || error != EWOULDBLOCK)
1868172567Sthompsa			return 0;
1869172567Sthompsa	}
1870172567Sthompsa	DPRINTF(("%s: timeout waiting for %s\n",
1871172567Sthompsa		__func__, waitfor ? "disable" : "enable"));
1872172567Sthompsa	return ETIMEDOUT;
1873172567Sthompsa}
1874172567Sthompsa
1875172567Sthompsastatic int
1876172567Sthompsaipw_enable(struct ipw_softc *sc)
1877172567Sthompsa{
1878172567Sthompsa	int error;
1879172567Sthompsa
1880172567Sthompsa	if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1881172567Sthompsa		DPRINTF(("Enable adapter\n"));
1882172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1883172567Sthompsa		if (error != 0)
1884172567Sthompsa			return error;
1885172567Sthompsa		error = ipw_waitfordisable(sc, 0);
1886172567Sthompsa		if (error != 0)
1887172567Sthompsa			return error;
1888172567Sthompsa		sc->flags |= IPW_FLAG_ENABLED;
1889172567Sthompsa	}
1890172567Sthompsa	return 0;
1891172567Sthompsa}
1892172567Sthompsa
1893172567Sthompsastatic int
1894172567Sthompsaipw_disable(struct ipw_softc *sc)
1895172567Sthompsa{
1896172567Sthompsa	int error;
1897172567Sthompsa
1898172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
1899172567Sthompsa		DPRINTF(("Disable adapter\n"));
1900172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1901172567Sthompsa		if (error != 0)
1902172567Sthompsa			return error;
1903172567Sthompsa		error = ipw_waitfordisable(sc, 1);
1904172567Sthompsa		if (error != 0)
1905172567Sthompsa			return error;
1906172567Sthompsa		sc->flags &= ~IPW_FLAG_ENABLED;
1907172567Sthompsa	}
1908172567Sthompsa	return 0;
1909172567Sthompsa}
1910172567Sthompsa
1911145247Sdamien/*
1912145247Sdamien * Upload the microcode to the device.
1913145247Sdamien */
1914145247Sdamienstatic int
1915156599Sdamienipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1916145247Sdamien{
1917145247Sdamien	int ntries;
1918145247Sdamien
1919145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1920145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1921145247Sdamien
1922145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1923145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1924145247Sdamien
1925145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1926145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1927145247Sdamien
1928145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1929145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1930145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1931145247Sdamien
1932145247Sdamien	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1933145247Sdamien
1934145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1935145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1936145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1937145247Sdamien
1938145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1939145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1940145247Sdamien
1941145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1942145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1943145247Sdamien
1944145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1945145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1946145247Sdamien
1947145247Sdamien	for (ntries = 0; ntries < 10; ntries++) {
1948145247Sdamien		if (MEM_READ_1(sc, 0x210000) & 1)
1949145247Sdamien			break;
1950145247Sdamien		DELAY(10);
1951145247Sdamien	}
1952145247Sdamien	if (ntries == 10) {
1953145247Sdamien		device_printf(sc->sc_dev,
1954145247Sdamien		    "timeout waiting for ucode to initialize\n");
1955145247Sdamien		return EIO;
1956145247Sdamien	}
1957145247Sdamien
1958145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0);
1959145247Sdamien
1960145247Sdamien	return 0;
1961145247Sdamien}
1962145247Sdamien
1963145247Sdamien/* set of macros to handle unaligned little endian data in firmware image */
1964145247Sdamien#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1965145247Sdamien#define GETLE16(p) ((p)[0] | (p)[1] << 8)
1966145247Sdamienstatic int
1967156599Sdamienipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
1968145247Sdamien{
1969156599Sdamien	const uint8_t *p, *end;
1970156599Sdamien	uint32_t tmp, dst;
1971145247Sdamien	uint16_t len;
1972145247Sdamien	int error;
1973145247Sdamien
1974145247Sdamien	p = fw;
1975145247Sdamien	end = fw + size;
1976145247Sdamien	while (p < end) {
1977145247Sdamien		dst = GETLE32(p); p += 4;
1978145247Sdamien		len = GETLE16(p); p += 2;
1979145247Sdamien
1980145247Sdamien		ipw_write_mem_1(sc, dst, p, len);
1981145247Sdamien		p += len;
1982145247Sdamien	}
1983145247Sdamien
1984145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
1985145247Sdamien	    IPW_IO_LED_OFF);
1986145247Sdamien
1987145247Sdamien	/* enable interrupts */
1988145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1989145247Sdamien
1990145247Sdamien	/* kick the firmware */
1991145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1992145247Sdamien
1993156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1994156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
1995145247Sdamien
1996145247Sdamien	/* wait at most one second for firmware initialization to complete */
1997145247Sdamien	if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
1998145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for firmware "
1999145247Sdamien		    "initialization to complete\n");
2000145247Sdamien		return error;
2001145247Sdamien	}
2002145247Sdamien
2003156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_IO);
2004156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2005156599Sdamien	    IPW_IO_GPIO3_MASK);
2006145247Sdamien
2007145247Sdamien	return 0;
2008145247Sdamien}
2009145247Sdamien
2010145247Sdamienstatic int
2011172567Sthompsaipw_setwepkeys(struct ipw_softc *sc)
2012172567Sthompsa{
2013172567Sthompsa	struct ieee80211com *ic = &sc->sc_ic;
2014172567Sthompsa	struct ipw_wep_key wepkey;
2015172567Sthompsa	struct ieee80211_key *wk;
2016172567Sthompsa	int error, i;
2017172567Sthompsa
2018172567Sthompsa	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2019172567Sthompsa		wk = &ic->ic_crypto.cs_nw_keys[i];
2020172567Sthompsa
2021172567Sthompsa		if (wk->wk_cipher == NULL ||
2022172567Sthompsa		    wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2023172567Sthompsa			continue;
2024172567Sthompsa
2025172567Sthompsa		wepkey.idx = i;
2026172567Sthompsa		wepkey.len = wk->wk_keylen;
2027172567Sthompsa		memset(wepkey.key, 0, sizeof wepkey.key);
2028172567Sthompsa		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2029172567Sthompsa		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2030172567Sthompsa		    wepkey.len));
2031172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2032172567Sthompsa		    sizeof wepkey);
2033172567Sthompsa		if (error != 0)
2034172567Sthompsa			return error;
2035172567Sthompsa	}
2036172567Sthompsa	return 0;
2037172567Sthompsa}
2038172567Sthompsa
2039172567Sthompsastatic int
2040172567Sthompsaipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2041172567Sthompsa{
2042172567Sthompsa	struct ipw_wpa_ie wpaie;
2043172567Sthompsa
2044172567Sthompsa	memset(&wpaie, 0, sizeof(wpaie));
2045172567Sthompsa	wpaie.len = htole32(ielen);
2046172567Sthompsa	/* XXX verify length */
2047172567Sthompsa	memcpy(&wpaie.ie, ie, ielen);
2048172567Sthompsa	DPRINTF(("Setting WPA IE\n"));
2049172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2050172567Sthompsa}
2051172567Sthompsa
2052172567Sthompsastatic int
2053172567Sthompsaipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2054172567Sthompsa{
2055172567Sthompsa	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2056172567Sthompsa
2057172567Sthompsa	if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2058172567Sthompsa		DPRINTF(("Setting mandatory BSSID to null\n"));
2059172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2060172567Sthompsa	} else {
2061172567Sthompsa		DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2062172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2063172567Sthompsa			bssid, IEEE80211_ADDR_LEN);
2064172567Sthompsa	}
2065172567Sthompsa}
2066172567Sthompsa
2067172567Sthompsastatic int
2068172567Sthompsaipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2069172567Sthompsa{
2070172567Sthompsa	if (ssidlen == 0) {
2071172567Sthompsa		/*
2072172567Sthompsa		 * A bug in the firmware breaks the ``don't associate''
2073172567Sthompsa		 * bit in the scan options command.  To compensate for
2074172567Sthompsa		 * this install a bogus ssid when no ssid is specified
2075172567Sthompsa		 * so the firmware won't try to associate.
2076172567Sthompsa		 */
2077172567Sthompsa		DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2078172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2079172567Sthompsa			"\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2080172567Sthompsa			"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2081172567Sthompsa			"\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2082172567Sthompsa			"\x3c\x3d", IEEE80211_NWID_LEN);
2083172567Sthompsa	} else {
2084172567Sthompsa#ifdef IPW_DEBUG
2085172567Sthompsa		if (ipw_debug > 0) {
2086172567Sthompsa			printf("Setting ESSID to ");
2087172567Sthompsa			ieee80211_print_essid(ssid, ssidlen);
2088172567Sthompsa			printf("\n");
2089172567Sthompsa		}
2090172567Sthompsa#endif
2091172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2092172567Sthompsa	}
2093172567Sthompsa}
2094172567Sthompsa
2095172567Sthompsastatic int
2096172567Sthompsaipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2097172567Sthompsa{
2098172567Sthompsa	struct ipw_scan_options opts;
2099172567Sthompsa
2100172567Sthompsa	DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2101172567Sthompsa	opts.channels = htole32(chanmask);
2102172567Sthompsa	opts.flags = htole32(flags);
2103172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2104172567Sthompsa}
2105172567Sthompsa
2106172567Sthompsa/*
2107172567Sthompsa * Handler for sc_scan_task.  This is a simple wrapper around ipw_scan().
2108172567Sthompsa */
2109172567Sthompsastatic void
2110172567Sthompsaipw_scan_task(void *context, int pending)
2111172567Sthompsa{
2112172567Sthompsa	struct ipw_softc *sc = context;
2113172567Sthompsa	IPW_LOCK_DECL;
2114172567Sthompsa
2115172567Sthompsa	IPW_LOCK(sc);
2116172567Sthompsa	ipw_scan(sc);
2117172567Sthompsa	IPW_UNLOCK(sc);
2118172567Sthompsa}
2119172567Sthompsa
2120172567Sthompsastatic int
2121172567Sthompsaipw_scan(struct ipw_softc *sc)
2122172567Sthompsa{
2123172567Sthompsa	uint32_t params;
2124172567Sthompsa	int error;
2125172567Sthompsa
2126172567Sthompsa	DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2127172567Sthompsa
2128172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
2129172567Sthompsa		return (EBUSY);
2130172567Sthompsa	sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2131172567Sthompsa
2132172567Sthompsa	/* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2133172567Sthompsa	error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2134172567Sthompsa	if (error != 0)
2135172567Sthompsa		goto done;
2136172567Sthompsa
2137172567Sthompsa	/*
2138172567Sthompsa	 * Setup null/bogus ssid so firmware doesn't use any previous
2139172567Sthompsa	 * ssid to try and associate.  This is because the ``don't
2140172567Sthompsa	 * associate'' option bit is broken (sigh).
2141172567Sthompsa	 */
2142172567Sthompsa	error = ipw_setssid(sc, NULL, 0);
2143172567Sthompsa	if (error != 0)
2144172567Sthompsa		goto done;
2145172567Sthompsa
2146172567Sthompsa	/*
2147172567Sthompsa	 * NB: the adapter may be disabled on association lost;
2148172567Sthompsa	 *     if so just re-enable it to kick off scanning.
2149172567Sthompsa	 */
2150172567Sthompsa	DPRINTF(("Starting scan\n"));
2151172567Sthompsa	sc->sc_scan_timer = 3;
2152172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
2153172567Sthompsa		params = 0;				/* XXX? */
2154172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2155172567Sthompsa				&params, sizeof(params));
2156172567Sthompsa	} else
2157172567Sthompsa		error = ipw_enable(sc);
2158172567Sthompsadone:
2159172567Sthompsa	if (error != 0) {
2160172567Sthompsa		DPRINTF(("Scan failed\n"));
2161172567Sthompsa		sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2162172567Sthompsa	}
2163172567Sthompsa	return (error);
2164172567Sthompsa}
2165172567Sthompsa
2166172567Sthompsastatic int
2167172567Sthompsaipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2168172567Sthompsa{
2169172567Sthompsa	struct ieee80211com *ic = &sc->sc_ic;
2170172567Sthompsa	uint32_t data;
2171172567Sthompsa	int error;
2172172567Sthompsa
2173172567Sthompsa	data = htole32(ieee80211_chan2ieee(ic, chan));
2174172567Sthompsa	DPRINTF(("Setting channel to %u\n", le32toh(data)));
2175172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2176172567Sthompsa	if (error == 0)
2177172567Sthompsa		ipw_setcurchan(sc, chan);
2178172567Sthompsa	return error;
2179172567Sthompsa}
2180172567Sthompsa
2181172567Sthompsastatic int
2182145247Sdamienipw_config(struct ipw_softc *sc)
2183145247Sdamien{
2184145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
2185145247Sdamien	struct ifnet *ifp = ic->ic_ifp;
2186145247Sdamien	struct ipw_security security;
2187145247Sdamien	struct ipw_configuration config;
2188145247Sdamien	uint32_t data;
2189172567Sthompsa	int error;
2190145247Sdamien
2191172567Sthompsa	error = ipw_disable(sc);
2192172567Sthompsa	if (error != 0)
2193172567Sthompsa		return error;
2194172567Sthompsa
2195145247Sdamien	switch (ic->ic_opmode) {
2196145247Sdamien	case IEEE80211_M_STA:
2197145247Sdamien	case IEEE80211_M_HOSTAP:
2198170530Ssam	case IEEE80211_M_WDS:		/* XXX */
2199145247Sdamien		data = htole32(IPW_MODE_BSS);
2200145247Sdamien		break;
2201145247Sdamien	case IEEE80211_M_IBSS:
2202145247Sdamien	case IEEE80211_M_AHDEMO:
2203145247Sdamien		data = htole32(IPW_MODE_IBSS);
2204145247Sdamien		break;
2205145247Sdamien	case IEEE80211_M_MONITOR:
2206145247Sdamien		data = htole32(IPW_MODE_MONITOR);
2207145247Sdamien		break;
2208145247Sdamien	}
2209145247Sdamien	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2210145247Sdamien	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2211145247Sdamien	if (error != 0)
2212145247Sdamien		return error;
2213145247Sdamien
2214145247Sdamien	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2215145247Sdamien	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2216172567Sthompsa		error = ipw_setchannel(sc, ic->ic_curchan);
2217145247Sdamien		if (error != 0)
2218145247Sdamien			return error;
2219145247Sdamien	}
2220145247Sdamien
2221172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2222172567Sthompsa		return ipw_enable(sc);
2223145247Sdamien
2224145247Sdamien	IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2225145247Sdamien	DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":"));
2226145247Sdamien	error = ipw_cmd(sc, IPW_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2227145247Sdamien	    IEEE80211_ADDR_LEN);
2228145247Sdamien	if (error != 0)
2229145247Sdamien		return error;
2230145247Sdamien
2231145247Sdamien	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2232145247Sdamien	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2233145247Sdamien	if (ic->ic_opmode == IEEE80211_M_IBSS)
2234145247Sdamien		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2235145247Sdamien	if (ifp->if_flags & IFF_PROMISC)
2236145247Sdamien		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2237145247Sdamien	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2238145247Sdamien	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2239145247Sdamien	DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2240145247Sdamien	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2241145247Sdamien	if (error != 0)
2242145247Sdamien		return error;
2243145247Sdamien
2244145247Sdamien	data = htole32(0x3); /* 1, 2 */
2245145247Sdamien	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2246145247Sdamien	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2247145247Sdamien	if (error != 0)
2248145247Sdamien		return error;
2249145247Sdamien
2250172567Sthompsa	/* NB: use the same rate set */
2251172567Sthompsa	DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2252172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2253172567Sthompsa	if (error != 0)
2254172567Sthompsa		return error;
2255172567Sthompsa
2256145247Sdamien	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2257145247Sdamien	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2258145247Sdamien	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2259145247Sdamien	if (error != 0)
2260145247Sdamien		return error;
2261145247Sdamien
2262145247Sdamien	data = htole32(IPW_POWER_MODE_CAM);
2263145247Sdamien	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2264145247Sdamien	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2265145247Sdamien	if (error != 0)
2266145247Sdamien		return error;
2267145247Sdamien
2268145247Sdamien	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2269145247Sdamien		data = htole32(32); /* default value */
2270145247Sdamien		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2271145247Sdamien		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2272145247Sdamien		    sizeof data);
2273145247Sdamien		if (error != 0)
2274145247Sdamien			return error;
2275145247Sdamien	}
2276145247Sdamien
2277145247Sdamien	data = htole32(ic->ic_rtsthreshold);
2278145247Sdamien	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2279145247Sdamien	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2280145247Sdamien	if (error != 0)
2281145247Sdamien		return error;
2282145247Sdamien
2283145247Sdamien	data = htole32(ic->ic_fragthreshold);
2284145247Sdamien	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2285145247Sdamien	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2286145247Sdamien	if (error != 0)
2287145247Sdamien		return error;
2288145247Sdamien
2289172567Sthompsa	error = ipw_setssid(sc, ic->ic_des_ssid[0].ssid, ic->ic_des_ssid[0].len);
2290145247Sdamien	if (error != 0)
2291145247Sdamien		return error;
2292145247Sdamien
2293172567Sthompsa	error = ipw_setbssid(sc, NULL);
2294145247Sdamien	if (error != 0)
2295145247Sdamien		return error;
2296145247Sdamien
2297145247Sdamien	if (ic->ic_flags & IEEE80211_F_DESBSSID) {
2298145247Sdamien		DPRINTF(("Setting desired BSSID to %6D\n", ic->ic_des_bssid,
2299145247Sdamien		    ":"));
2300145247Sdamien		error = ipw_cmd(sc, IPW_CMD_SET_DESIRED_BSSID,
2301145247Sdamien		    ic->ic_des_bssid, IEEE80211_ADDR_LEN);
2302145247Sdamien		if (error != 0)
2303145247Sdamien			return error;
2304145247Sdamien	}
2305145247Sdamien
2306152637Sdamien	memset(&security, 0, sizeof security);
2307145247Sdamien	security.authmode = (ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED) ?
2308145247Sdamien	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2309145247Sdamien	security.ciphers = htole32(IPW_CIPHER_NONE);
2310145247Sdamien	DPRINTF(("Setting authmode to %u\n", security.authmode));
2311172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2312145247Sdamien	    sizeof security);
2313145247Sdamien	if (error != 0)
2314145247Sdamien		return error;
2315145247Sdamien
2316145247Sdamien	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2317172567Sthompsa		error = ipw_setwepkeys(sc);
2318172567Sthompsa		if (error != 0)
2319172567Sthompsa			return error;
2320145247Sdamien
2321172567Sthompsa		if (ic->ic_crypto.cs_def_txkey != IEEE80211_KEYIX_NONE) {
2322172567Sthompsa			data = htole32(ic->ic_crypto.cs_def_txkey);
2323172567Sthompsa			DPRINTF(("Setting wep tx key index to %u\n",
2324172567Sthompsa				le32toh(data)));
2325172567Sthompsa			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2326172567Sthompsa			    sizeof data);
2327145247Sdamien			if (error != 0)
2328145247Sdamien				return error;
2329145247Sdamien		}
2330145247Sdamien	}
2331145247Sdamien
2332145247Sdamien	data = htole32((ic->ic_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2333145247Sdamien	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2334145247Sdamien	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2335145247Sdamien	if (error != 0)
2336145247Sdamien		return error;
2337145247Sdamien
2338172567Sthompsa	if (ic->ic_opt_ie != NULL) {
2339172567Sthompsa		error = ipw_setwpaie(sc, ic->ic_opt_ie, ic->ic_opt_ie_len);
2340172567Sthompsa		if (error != 0)
2341172567Sthompsa			return error;
2342172567Sthompsa	}
2343145247Sdamien
2344145247Sdamien	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2345148843Ssam		data = htole32(ic->ic_bintval);
2346145247Sdamien		DPRINTF(("Setting beacon interval to %u\n", le32toh(data)));
2347145247Sdamien		error = ipw_cmd(sc, IPW_CMD_SET_BEACON_INTERVAL, &data,
2348145247Sdamien		    sizeof data);
2349145247Sdamien		if (error != 0)
2350145247Sdamien			return error;
2351145247Sdamien	}
2352145247Sdamien
2353172567Sthompsa	error = ipw_setscanopts(sc, 0x3fff, 0);
2354145247Sdamien	if (error != 0)
2355145247Sdamien		return error;
2356145247Sdamien
2357172567Sthompsa	return (ipw_enable(sc));
2358145247Sdamien}
2359145247Sdamien
2360156599Sdamien/*
2361172567Sthompsa * Handler for sc_assoc_task.  This is a simple wrapper around
2362172567Sthompsa * ipw_auth_and_assoc().
2363172567Sthompsa */
2364172567Sthompsastatic void
2365172567Sthompsaipw_assoc_task(void *context, int pending)
2366172567Sthompsa{
2367172567Sthompsa	struct ipw_softc *sc = context;
2368172567Sthompsa	IPW_LOCK_DECL;
2369172567Sthompsa
2370172567Sthompsa	IPW_LOCK(sc);
2371172567Sthompsa	ipw_auth_and_assoc(sc);
2372172567Sthompsa	IPW_UNLOCK(sc);
2373172567Sthompsa}
2374172567Sthompsa
2375172567Sthompsastatic int
2376172567Sthompsaipw_auth_and_assoc(struct ipw_softc *sc)
2377172567Sthompsa{
2378172567Sthompsa	struct ieee80211com *ic = &sc->sc_ic;
2379172567Sthompsa	struct ieee80211_node *ni = ic->ic_bss;
2380172567Sthompsa	struct ipw_security security;
2381172567Sthompsa	uint32_t data;
2382172567Sthompsa	int error;
2383172567Sthompsa
2384172567Sthompsa	error = ipw_disable(sc);
2385172567Sthompsa	if (error != 0)
2386172567Sthompsa		return (error);
2387172567Sthompsa
2388172567Sthompsa	memset(&security, 0, sizeof security);
2389172567Sthompsa	security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2390172567Sthompsa	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2391172567Sthompsa	security.ciphers = htole32(IPW_CIPHER_NONE);
2392172567Sthompsa	DPRINTF(("Setting authmode to %u\n", security.authmode));
2393172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2394172567Sthompsa	    sizeof security);
2395172567Sthompsa	if (error != 0)
2396172567Sthompsa		return (error);
2397172567Sthompsa
2398172567Sthompsa	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2399172567Sthompsa		error = ipw_setwepkeys(sc);
2400172567Sthompsa		if (error != 0)
2401172567Sthompsa			return error;
2402172567Sthompsa
2403172567Sthompsa		if (ic->ic_crypto.cs_def_txkey != IEEE80211_KEYIX_NONE) {
2404172567Sthompsa			data = htole32(ic->ic_crypto.cs_def_txkey);
2405172567Sthompsa			DPRINTF(("Setting wep tx key index to %u\n",
2406172567Sthompsa				le32toh(data)));
2407172567Sthompsa			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2408172567Sthompsa			    sizeof data);
2409172567Sthompsa			if (error != 0)
2410172567Sthompsa				return error;
2411172567Sthompsa		}
2412172567Sthompsa	}
2413172567Sthompsa
2414172567Sthompsa	data = htole32((ic->ic_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2415172567Sthompsa	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2416172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2417172567Sthompsa	if (error != 0)
2418172567Sthompsa		return error;
2419172567Sthompsa
2420172567Sthompsa	error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2421172567Sthompsa	if (error != 0)
2422172567Sthompsa		return (error);
2423172567Sthompsa
2424172567Sthompsa	error = ipw_setbssid(sc, ni->ni_bssid);
2425172567Sthompsa	if (error != 0)
2426172567Sthompsa		return (error);
2427172567Sthompsa
2428172567Sthompsa	if (ic->ic_opt_ie != NULL) {
2429172567Sthompsa		error = ipw_setwpaie(sc, ic->ic_opt_ie, ic->ic_opt_ie_len);
2430172567Sthompsa		if (error != 0)
2431172567Sthompsa			return error;
2432172567Sthompsa	}
2433172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2434172567Sthompsa		error = ipw_setchannel(sc, ni->ni_chan);
2435172567Sthompsa		if (error != 0)
2436172567Sthompsa			return (error);
2437172567Sthompsa	}
2438172567Sthompsa
2439172567Sthompsa	/* lock scan to ap's channel and enable associate */
2440172567Sthompsa	error = ipw_setscanopts(sc,
2441172567Sthompsa			1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2442172567Sthompsa
2443172567Sthompsa	return ipw_enable(sc);		/* finally, enable adapter */
2444172567Sthompsa}
2445172567Sthompsa
2446172567Sthompsa/*
2447172567Sthompsa * Handler for sc_disassoc_task.  This is a simple wrapper around
2448172567Sthompsa * ipw_disassociate().
2449172567Sthompsa */
2450172567Sthompsastatic void
2451172567Sthompsaipw_disassoc_task(void *context, int pending)
2452172567Sthompsa{
2453172567Sthompsa	struct ipw_softc *sc = context;
2454172567Sthompsa	IPW_LOCK_DECL;
2455172567Sthompsa
2456172567Sthompsa	IPW_LOCK(sc);
2457172567Sthompsa	ipw_disassociate(sc);
2458172567Sthompsa	IPW_UNLOCK(sc);
2459172567Sthompsa}
2460172567Sthompsa
2461172567Sthompsastatic int
2462172567Sthompsaipw_disassociate(struct ipw_softc *sc)
2463172567Sthompsa{
2464172567Sthompsa	struct ieee80211com *ic = &sc->sc_ic;
2465172567Sthompsa	struct ieee80211_node *ni = ic->ic_bss;
2466172567Sthompsa
2467172567Sthompsa	DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2468172567Sthompsa
2469172567Sthompsa	/*
2470172567Sthompsa	 * NB: don't try to do this if ipw_stop_master has
2471172567Sthompsa	 *     shutdown the firmware and disabled interrupts.
2472172567Sthompsa	 */
2473172567Sthompsa	if (!(sc->flags & IPW_FLAG_FW_INITED))
2474172567Sthompsa		return (0);
2475172567Sthompsa
2476172567Sthompsa	sc->flags &= ~IPW_FLAG_ASSOCIATED;
2477172567Sthompsa	/*
2478172567Sthompsa	 * NB: firmware currently ignores bssid parameter, but
2479172567Sthompsa	 *     supply it in case this changes (follow linux driver).
2480172567Sthompsa	 */
2481172567Sthompsa	return ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2482172567Sthompsa		ni->ni_bssid, IEEE80211_ADDR_LEN);
2483172567Sthompsa}
2484172567Sthompsa
2485172567Sthompsa/*
2486156599Sdamien * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2487156599Sdamien * It is called on firmware panics or on watchdog timeouts.
2488156599Sdamien */
2489145247Sdamienstatic void
2490156599Sdamienipw_init_task(void *context, int pending)
2491156599Sdamien{
2492156599Sdamien	ipw_init(context);
2493156599Sdamien}
2494156599Sdamien
2495156599Sdamienstatic void
2496145247Sdamienipw_init(void *priv)
2497145247Sdamien{
2498145247Sdamien	struct ipw_softc *sc = priv;
2499172567Sthompsa	IPW_LOCK_DECL;
2500172567Sthompsa
2501172567Sthompsa	IPW_LOCK(sc);
2502172567Sthompsa	ipw_init_locked(sc, 0);
2503172567Sthompsa	IPW_UNLOCK(sc);
2504172567Sthompsa}
2505172567Sthompsa
2506172567Sthompsastatic void
2507172567Sthompsaipw_init_locked(struct ipw_softc *sc, int force)
2508172567Sthompsa{
2509145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
2510145247Sdamien	struct ifnet *ifp = ic->ic_ifp;
2511166756Sluigi	const struct firmware *fp;
2512156599Sdamien	const struct ipw_firmware_hdr *hdr;
2513156599Sdamien	const char *imagename, *fw;
2514172567Sthompsa	IPW_LOCK_DECL;
2515145247Sdamien
2516172567Sthompsa	IPW_LOCK_ASSERT(sc);
2517156599Sdamien
2518172567Sthompsa	DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2519172567Sthompsa		ieee80211_state_name[ic->ic_state], sc->flags));
2520172567Sthompsa
2521156599Sdamien	/*
2522156599Sdamien	 * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2523156599Sdamien	 * when loading the firmware and we don't want to be called during this
2524156599Sdamien	 * operation.
2525156599Sdamien	 */
2526172567Sthompsa	if (sc->flags & IPW_FLAG_INIT_LOCKED)
2527145247Sdamien		return;
2528156599Sdamien	sc->flags |= IPW_FLAG_INIT_LOCKED;
2529145247Sdamien
2530172567Sthompsa	ipw_stop_locked(sc);
2531145247Sdamien
2532145247Sdamien	if (ipw_reset(sc) != 0) {
2533145247Sdamien		device_printf(sc->sc_dev, "could not reset adapter\n");
2534156599Sdamien		goto fail1;
2535145247Sdamien	}
2536145247Sdamien
2537156599Sdamien	switch (ic->ic_opmode) {
2538156599Sdamien	case IEEE80211_M_STA:
2539156599Sdamien		imagename = "ipw_bss";
2540156599Sdamien		break;
2541156599Sdamien	case IEEE80211_M_IBSS:
2542156599Sdamien		imagename = "ipw_ibss";
2543156599Sdamien		break;
2544156599Sdamien	case IEEE80211_M_MONITOR:
2545156599Sdamien		imagename = "ipw_monitor";
2546156599Sdamien		break;
2547156599Sdamien	default:
2548156599Sdamien		imagename = NULL;	/* should not get there */
2549156599Sdamien	}
2550156599Sdamien
2551156599Sdamien	/*
2552156599Sdamien	 * Load firmware image using the firmware(9) subsystem.  We need to
2553156599Sdamien	 * release the driver's lock first.
2554156599Sdamien	 */
2555159487Siedowse	if (sc->sc_firmware == NULL || strcmp(sc->sc_firmware->name,
2556159487Siedowse	    imagename) != 0) {
2557172567Sthompsa		IPW_UNLOCK(sc);
2558159487Siedowse		if (sc->sc_firmware != NULL)
2559159487Siedowse			firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
2560159487Siedowse		sc->sc_firmware = firmware_get(imagename);
2561172567Sthompsa		IPW_LOCK(sc);
2562159487Siedowse	}
2563156599Sdamien
2564159487Siedowse	if (sc->sc_firmware == NULL) {
2565156599Sdamien		device_printf(sc->sc_dev,
2566156599Sdamien		    "could not load firmware image '%s'\n", imagename);
2567156599Sdamien		goto fail1;
2568156599Sdamien	}
2569156599Sdamien
2570159487Siedowse	fp = sc->sc_firmware;
2571156599Sdamien	if (fp->datasize < sizeof *hdr) {
2572156599Sdamien		device_printf(sc->sc_dev,
2573156599Sdamien		    "firmware image too short %zu\n", fp->datasize);
2574156599Sdamien		goto fail2;
2575156599Sdamien	}
2576156599Sdamien
2577156599Sdamien	hdr = (const struct ipw_firmware_hdr *)fp->data;
2578156599Sdamien
2579156599Sdamien	if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
2580156599Sdamien	    le32toh(hdr->ucodesz)) {
2581156599Sdamien		device_printf(sc->sc_dev,
2582156599Sdamien		    "firmware image too short %zu\n", fp->datasize);
2583156599Sdamien		goto fail2;
2584156599Sdamien	}
2585156599Sdamien
2586172567Sthompsa	DPRINTF(("Loading firmware image '%s'\n", imagename));
2587156599Sdamien	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2588156599Sdamien	if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2589145247Sdamien		device_printf(sc->sc_dev, "could not load microcode\n");
2590156599Sdamien		goto fail2;
2591145247Sdamien	}
2592145247Sdamien
2593145247Sdamien	ipw_stop_master(sc);
2594145247Sdamien
2595145247Sdamien	/*
2596145247Sdamien	 * Setup tx, rx and status rings.
2597145247Sdamien	 */
2598145247Sdamien	sc->txold = IPW_NTBD - 1;
2599145247Sdamien	sc->txcur = 0;
2600145247Sdamien	sc->txfree = IPW_NTBD - 2;
2601145247Sdamien	sc->rxcur = IPW_NRBD - 1;
2602145247Sdamien
2603145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2604145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2605145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2606145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2607145247Sdamien
2608145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2609145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2610145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2611145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2612145247Sdamien
2613145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2614145247Sdamien
2615156599Sdamien	fw = (const char *)fp->data + sizeof *hdr;
2616156599Sdamien	if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2617145247Sdamien		device_printf(sc->sc_dev, "could not load firmware\n");
2618156599Sdamien		goto fail2;
2619145247Sdamien	}
2620145247Sdamien
2621145247Sdamien	sc->flags |= IPW_FLAG_FW_INITED;
2622145247Sdamien
2623145247Sdamien	/* retrieve information tables base addresses */
2624145247Sdamien	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2625145247Sdamien	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2626145247Sdamien
2627145247Sdamien	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2628145247Sdamien
2629145247Sdamien	if (ipw_config(sc) != 0) {
2630145247Sdamien		device_printf(sc->sc_dev, "device configuration failed\n");
2631156599Sdamien		goto fail1;
2632145247Sdamien	}
2633145247Sdamien
2634172567Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2635172567Sthompsa		/*
2636172567Sthompsa		 * NB: When restarting the adapter clock the state
2637172567Sthompsa		 * machine regardless of the roaming mode; otherwise
2638172567Sthompsa		 * we need to notify user apps so they can manually
2639172567Sthompsa		 * get us going again.
2640172567Sthompsa		 */
2641172567Sthompsa		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL || force)
2642172567Sthompsa			ieee80211_new_state(ic, IEEE80211_S_SCAN, 0);
2643172567Sthompsa	} else
2644172567Sthompsa		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2645172567Sthompsa
2646172567Sthompsa	callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2647148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2648148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2649145247Sdamien
2650156599Sdamien	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2651145247Sdamien	return;
2652145247Sdamien
2653156599Sdamienfail2:	firmware_put(fp, FIRMWARE_UNLOAD);
2654159487Siedowse	sc->sc_firmware = NULL;
2655156599Sdamienfail1:	ifp->if_flags &= ~IFF_UP;
2656172567Sthompsa	ipw_stop_locked(sc);
2657156599Sdamien	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2658145247Sdamien}
2659145247Sdamien
2660145247Sdamienstatic void
2661145247Sdamienipw_stop(void *priv)
2662145247Sdamien{
2663145247Sdamien	struct ipw_softc *sc = priv;
2664172567Sthompsa	IPW_LOCK_DECL;
2665172567Sthompsa
2666172567Sthompsa	IPW_LOCK(sc);
2667172567Sthompsa	ipw_stop_locked(sc);
2668172567Sthompsa	IPW_UNLOCK(sc);
2669172567Sthompsa}
2670172567Sthompsa
2671172567Sthompsastatic void
2672172567Sthompsaipw_stop_locked(struct ipw_softc *sc)
2673172567Sthompsa{
2674145247Sdamien	struct ieee80211com *ic = &sc->sc_ic;
2675145247Sdamien	struct ifnet *ifp = ic->ic_ifp;
2676145247Sdamien	int i;
2677145247Sdamien
2678172567Sthompsa	IPW_LOCK_ASSERT(sc);
2679156599Sdamien
2680156599Sdamien	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2681156599Sdamien
2682172567Sthompsa	callout_stop(&sc->sc_wdtimer);
2683145247Sdamien	ipw_stop_master(sc);
2684145247Sdamien
2685145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2686145247Sdamien
2687145247Sdamien	/*
2688145247Sdamien	 * Release tx buffers.
2689145247Sdamien	 */
2690145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
2691145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
2692145247Sdamien
2693145247Sdamien	sc->sc_tx_timer = 0;
2694148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2695145247Sdamien}
2696145247Sdamien
2697145247Sdamienstatic int
2698145247Sdamienipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2699145247Sdamien{
2700145247Sdamien	struct ipw_softc *sc = arg1;
2701145247Sdamien	uint32_t i, size, buf[256];
2702145247Sdamien
2703145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED)) {
2704152637Sdamien		memset(buf, 0, sizeof buf);
2705145247Sdamien		return SYSCTL_OUT(req, buf, sizeof buf);
2706145247Sdamien	}
2707145247Sdamien
2708145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2709145247Sdamien
2710145247Sdamien	size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2711145247Sdamien	for (i = 1; i < size; i++)
2712145247Sdamien		buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2713145247Sdamien
2714145247Sdamien	return SYSCTL_OUT(req, buf, sizeof buf);
2715145247Sdamien}
2716145247Sdamien
2717145247Sdamienstatic int
2718145247Sdamienipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2719145247Sdamien{
2720145247Sdamien	struct ipw_softc *sc = arg1;
2721145247Sdamien	int val;
2722145247Sdamien
2723145247Sdamien	val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2724145247Sdamien	        (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2725145247Sdamien
2726145247Sdamien	return SYSCTL_OUT(req, &val, sizeof val);
2727145247Sdamien}
2728145247Sdamien
2729145247Sdamienstatic uint32_t
2730145247Sdamienipw_read_table1(struct ipw_softc *sc, uint32_t off)
2731145247Sdamien{
2732145247Sdamien	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2733145247Sdamien}
2734145247Sdamien
2735145247Sdamienstatic void
2736145247Sdamienipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2737145247Sdamien{
2738145247Sdamien	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2739145247Sdamien}
2740145247Sdamien
2741172567Sthompsa#if 0
2742145247Sdamienstatic int
2743145247Sdamienipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2744145247Sdamien{
2745145247Sdamien	uint32_t addr, info;
2746145247Sdamien	uint16_t count, size;
2747145247Sdamien	uint32_t total;
2748145247Sdamien
2749145247Sdamien	/* addr[4] + count[2] + size[2] */
2750145247Sdamien	addr = MEM_READ_4(sc, sc->table2_base + off);
2751145247Sdamien	info = MEM_READ_4(sc, sc->table2_base + off + 4);
2752145247Sdamien
2753145247Sdamien	count = info >> 16;
2754145247Sdamien	size = info & 0xffff;
2755145247Sdamien	total = count * size;
2756145247Sdamien
2757145247Sdamien	if (total > *len) {
2758145247Sdamien		*len = total;
2759145247Sdamien		return EINVAL;
2760145247Sdamien	}
2761145247Sdamien
2762145247Sdamien	*len = total;
2763145247Sdamien	ipw_read_mem_1(sc, addr, buf, total);
2764145247Sdamien
2765145247Sdamien	return 0;
2766145247Sdamien}
2767145247Sdamien
2768145247Sdamienstatic void
2769145247Sdamienipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2770145247Sdamien    bus_size_t count)
2771145247Sdamien{
2772145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2773145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2774145247Sdamien		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2775145247Sdamien	}
2776145247Sdamien}
2777172567Sthompsa#endif
2778145247Sdamien
2779145247Sdamienstatic void
2780156599Sdamienipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2781145247Sdamien    bus_size_t count)
2782145247Sdamien{
2783145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2784145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2785145247Sdamien		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2786145247Sdamien	}
2787145247Sdamien}
2788172567Sthompsa
2789172567Sthompsastatic void
2790172567Sthompsaipw_scan_start(struct ieee80211com *ic)
2791172567Sthompsa{
2792172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2793172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2794172567Sthompsa	IPW_LOCK_DECL;
2795172567Sthompsa
2796172567Sthompsa	IPW_LOCK(sc);
2797172567Sthompsa	if (!(sc->flags & IPW_FLAG_SCANNING))
2798172567Sthompsa		taskqueue_enqueue_fast(taskqueue_fast, &sc->sc_scan_task);
2799172567Sthompsa	IPW_UNLOCK(sc);
2800172567Sthompsa}
2801172567Sthompsa
2802172567Sthompsastatic void
2803172567Sthompsaipw_set_channel(struct ieee80211com *ic)
2804172567Sthompsa{
2805172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2806172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2807172567Sthompsa	IPW_LOCK_DECL;
2808172567Sthompsa
2809172567Sthompsa	IPW_LOCK(sc);
2810172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2811172567Sthompsa		ipw_disable(sc);
2812172567Sthompsa		ipw_setchannel(sc, ic->ic_curchan);
2813172567Sthompsa		ipw_enable(sc);
2814172567Sthompsa	}
2815172567Sthompsa	IPW_UNLOCK(sc);
2816172567Sthompsa}
2817172567Sthompsa
2818172567Sthompsastatic void
2819172567Sthompsaipw_scan_curchan(struct ieee80211com *ic, unsigned long maxdwell)
2820172567Sthompsa{
2821172567Sthompsa	/* NB: all channels are scanned at once */
2822172567Sthompsa}
2823172567Sthompsa
2824172567Sthompsastatic void
2825172567Sthompsaipw_scan_mindwell(struct ieee80211com *ic)
2826172567Sthompsa{
2827172567Sthompsa	/* NB: don't try to abort scan; wait for firmware to finish */
2828172567Sthompsa}
2829172567Sthompsa
2830172567Sthompsastatic void
2831172567Sthompsaipw_scan_end(struct ieee80211com *ic)
2832172567Sthompsa{
2833172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2834172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2835172567Sthompsa	IPW_LOCK_DECL;
2836172567Sthompsa
2837172567Sthompsa	IPW_LOCK(sc);
2838172567Sthompsa	sc->flags &= ~IPW_FLAG_SCANNING;
2839172567Sthompsa	IPW_UNLOCK(sc);
2840172567Sthompsa}
2841