if_ipw.c revision 243857
1145247Sdamien/*	$FreeBSD: head/sys/dev/ipw/if_ipw.c 243857 2012-12-04 09:32:43Z glebius $	*/
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 243857 2012-12-04 09:32:43Z glebius $");
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
110178354Ssamstatic struct ieee80211vap *ipw_vap_create(struct ieee80211com *,
111228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
112228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
113228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
114178354Ssamstatic void	ipw_vap_delete(struct ieee80211vap *);
115145247Sdamienstatic int	ipw_dma_alloc(struct ipw_softc *);
116145247Sdamienstatic void	ipw_release(struct ipw_softc *);
117145247Sdamienstatic void	ipw_media_status(struct ifnet *, struct ifmediareq *);
118178354Ssamstatic int	ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
119145247Sdamienstatic uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
120172567Sthompsastatic void	ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
121172567Sthompsastatic void	ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
122172567Sthompsastatic void	ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
123145247Sdamien		    struct ipw_soft_bd *, struct ipw_soft_buf *);
124145247Sdamienstatic void	ipw_rx_intr(struct ipw_softc *);
125145247Sdamienstatic void	ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
126145247Sdamienstatic void	ipw_tx_intr(struct ipw_softc *);
127145247Sdamienstatic void	ipw_intr(void *);
128145247Sdamienstatic void	ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
129172567Sthompsastatic const char * ipw_cmdname(int);
130145247Sdamienstatic int	ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
131145247Sdamienstatic int	ipw_tx_start(struct ifnet *, struct mbuf *,
132145247Sdamien		    struct ieee80211_node *);
133178354Ssamstatic int	ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
134178354Ssam		    const struct ieee80211_bpf_params *);
135145247Sdamienstatic void	ipw_start(struct ifnet *);
136172567Sthompsastatic void	ipw_start_locked(struct ifnet *);
137172567Sthompsastatic void	ipw_watchdog(void *);
138145247Sdamienstatic int	ipw_ioctl(struct ifnet *, u_long, caddr_t);
139145247Sdamienstatic void	ipw_stop_master(struct ipw_softc *);
140172567Sthompsastatic int	ipw_enable(struct ipw_softc *);
141172567Sthompsastatic int	ipw_disable(struct ipw_softc *);
142145247Sdamienstatic int	ipw_reset(struct ipw_softc *);
143156599Sdamienstatic int	ipw_load_ucode(struct ipw_softc *, const char *, int);
144156599Sdamienstatic int	ipw_load_firmware(struct ipw_softc *, const char *, int);
145145247Sdamienstatic int	ipw_config(struct ipw_softc *);
146191746Sthompsastatic void	ipw_assoc(struct ieee80211com *, struct ieee80211vap *);
147191746Sthompsastatic void	ipw_disassoc(struct ieee80211com *, struct ieee80211vap *);
148156599Sdamienstatic void	ipw_init_task(void *, int);
149145247Sdamienstatic void	ipw_init(void *);
150178354Ssamstatic void	ipw_init_locked(struct ipw_softc *);
151145247Sdamienstatic void	ipw_stop(void *);
152172567Sthompsastatic void	ipw_stop_locked(struct ipw_softc *);
153145247Sdamienstatic int	ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
154145247Sdamienstatic int	ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
155145247Sdamienstatic uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
156145247Sdamienstatic void	ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
157172567Sthompsa#if 0
158145247Sdamienstatic int	ipw_read_table2(struct ipw_softc *, uint32_t, void *,
159145247Sdamien		    uint32_t *);
160145247Sdamienstatic void	ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
161145247Sdamien		    bus_size_t);
162172567Sthompsa#endif
163156599Sdamienstatic void	ipw_write_mem_1(struct ipw_softc *, bus_size_t,
164156599Sdamien		    const uint8_t *, bus_size_t);
165172567Sthompsastatic int	ipw_scan(struct ipw_softc *);
166172567Sthompsastatic void	ipw_scan_start(struct ieee80211com *);
167172567Sthompsastatic void	ipw_scan_end(struct ieee80211com *);
168172567Sthompsastatic void	ipw_set_channel(struct ieee80211com *);
169178354Ssamstatic void	ipw_scan_curchan(struct ieee80211_scan_state *,
170178354Ssam		    unsigned long maxdwell);
171178354Ssamstatic void	ipw_scan_mindwell(struct ieee80211_scan_state *);
172145247Sdamien
173145247Sdamienstatic int ipw_probe(device_t);
174145247Sdamienstatic int ipw_attach(device_t);
175145247Sdamienstatic int ipw_detach(device_t);
176145247Sdamienstatic int ipw_shutdown(device_t);
177145247Sdamienstatic int ipw_suspend(device_t);
178145247Sdamienstatic int ipw_resume(device_t);
179145247Sdamien
180145247Sdamienstatic device_method_t ipw_methods[] = {
181145247Sdamien	/* Device interface */
182145247Sdamien	DEVMETHOD(device_probe,		ipw_probe),
183145247Sdamien	DEVMETHOD(device_attach,	ipw_attach),
184145247Sdamien	DEVMETHOD(device_detach,	ipw_detach),
185145247Sdamien	DEVMETHOD(device_shutdown,	ipw_shutdown),
186145247Sdamien	DEVMETHOD(device_suspend,	ipw_suspend),
187145247Sdamien	DEVMETHOD(device_resume,	ipw_resume),
188145247Sdamien
189145247Sdamien	{ 0, 0 }
190145247Sdamien};
191145247Sdamien
192145247Sdamienstatic driver_t ipw_driver = {
193145247Sdamien	"ipw",
194145247Sdamien	ipw_methods,
195145247Sdamien	sizeof (struct ipw_softc)
196145247Sdamien};
197145247Sdamien
198145247Sdamienstatic devclass_t ipw_devclass;
199145247Sdamien
200145247SdamienDRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0);
201145247Sdamien
202222543SbschmidtMODULE_VERSION(ipw, 1);
203222543Sbschmidt
204145247Sdamienstatic int
205145247Sdamienipw_probe(device_t dev)
206145247Sdamien{
207145247Sdamien	const struct ipw_ident *ident;
208145247Sdamien
209145247Sdamien	for (ident = ipw_ident_table; ident->name != NULL; ident++) {
210145247Sdamien		if (pci_get_vendor(dev) == ident->vendor &&
211145247Sdamien		    pci_get_device(dev) == ident->device) {
212145247Sdamien			device_set_desc(dev, ident->name);
213145247Sdamien			return 0;
214145247Sdamien		}
215145247Sdamien	}
216145247Sdamien	return ENXIO;
217145247Sdamien}
218145247Sdamien
219145247Sdamien/* Base Address Register */
220145247Sdamien#define IPW_PCI_BAR0	0x10
221145247Sdamien
222145247Sdamienstatic int
223145247Sdamienipw_attach(device_t dev)
224145247Sdamien{
225145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
226147256Sbrooks	struct ifnet *ifp;
227178354Ssam	struct ieee80211com *ic;
228170530Ssam	struct ieee80211_channel *c;
229145247Sdamien	uint16_t val;
230145247Sdamien	int error, i;
231190526Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
232145247Sdamien
233145247Sdamien	sc->sc_dev = dev;
234145247Sdamien
235145247Sdamien	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
236145247Sdamien	    MTX_DEF | MTX_RECURSE);
237145247Sdamien
238156599Sdamien	TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
239172567Sthompsa	callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
240156599Sdamien
241145247Sdamien	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
242145247Sdamien		device_printf(dev, "chip is in D%d power mode "
243145247Sdamien		    "-- setting to D0\n", pci_get_powerstate(dev));
244145247Sdamien		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
245145247Sdamien	}
246145247Sdamien
247146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
248146498Sdamien
249145247Sdamien	/* enable bus-mastering */
250145247Sdamien	pci_enable_busmaster(dev);
251145247Sdamien
252145247Sdamien	sc->mem_rid = IPW_PCI_BAR0;
253145247Sdamien	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
254145247Sdamien	    RF_ACTIVE);
255145247Sdamien	if (sc->mem == NULL) {
256145247Sdamien		device_printf(dev, "could not allocate memory resource\n");
257145247Sdamien		goto fail;
258145247Sdamien	}
259145247Sdamien
260145247Sdamien	sc->sc_st = rman_get_bustag(sc->mem);
261145247Sdamien	sc->sc_sh = rman_get_bushandle(sc->mem);
262145247Sdamien
263145247Sdamien	sc->irq_rid = 0;
264145247Sdamien	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
265145247Sdamien	    RF_ACTIVE | RF_SHAREABLE);
266145247Sdamien	if (sc->irq == NULL) {
267145247Sdamien		device_printf(dev, "could not allocate interrupt resource\n");
268178354Ssam		goto fail1;
269145247Sdamien	}
270145247Sdamien
271145247Sdamien	if (ipw_reset(sc) != 0) {
272145247Sdamien		device_printf(dev, "could not reset adapter\n");
273178354Ssam		goto fail2;
274145247Sdamien	}
275145247Sdamien
276145247Sdamien	if (ipw_dma_alloc(sc) != 0) {
277145247Sdamien		device_printf(dev, "could not allocate DMA resources\n");
278178354Ssam		goto fail2;
279145247Sdamien	}
280147757Sdamien
281178354Ssam	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
282147256Sbrooks	if (ifp == NULL) {
283147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
284178354Ssam		goto fail3;
285147256Sbrooks	}
286178354Ssam	ic = ifp->if_l2com;
287147757Sdamien
288145247Sdamien	ifp->if_softc = sc;
289145247Sdamien	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
290145247Sdamien	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
291145247Sdamien	ifp->if_init = ipw_init;
292145247Sdamien	ifp->if_ioctl = ipw_ioctl;
293145247Sdamien	ifp->if_start = ipw_start;
294207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
295207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
296145247Sdamien	IFQ_SET_READY(&ifp->if_snd);
297145247Sdamien
298145247Sdamien	ic->ic_ifp = ifp;
299178354Ssam	ic->ic_opmode = IEEE80211_M_STA;
300145247Sdamien	ic->ic_phytype = IEEE80211_T_DS;
301145247Sdamien
302145247Sdamien	/* set device capabilities */
303178957Ssam	ic->ic_caps =
304178957Ssam		  IEEE80211_C_STA		/* station mode supported */
305178957Ssam		| IEEE80211_C_IBSS		/* IBSS mode supported */
306172567Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode supported */
307172567Sthompsa		| IEEE80211_C_PMGT		/* power save supported */
308172567Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
309172567Sthompsa		| IEEE80211_C_WPA		/* 802.11i supported */
310172567Sthompsa		;
311145247Sdamien
312145247Sdamien	/* read MAC address from EEPROM */
313145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
314190526Ssam	macaddr[0] = val >> 8;
315190526Ssam	macaddr[1] = val & 0xff;
316145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
317190526Ssam	macaddr[2] = val >> 8;
318190526Ssam	macaddr[3] = val & 0xff;
319145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
320190526Ssam	macaddr[4] = val >> 8;
321190526Ssam	macaddr[5] = val & 0xff;
322145247Sdamien
323170530Ssam	/* set supported .11b channels (read from EEPROM) */
324170530Ssam	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
325170530Ssam		val = 0x7ff; /* default to channels 1-11 */
326170530Ssam	val <<= 1;
327170530Ssam	for (i = 1; i < 16; i++) {
328170530Ssam		if (val & (1 << i)) {
329170530Ssam			c = &ic->ic_channels[ic->ic_nchans++];
330170530Ssam			c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
331170530Ssam			c->ic_flags = IEEE80211_CHAN_B;
332170530Ssam			c->ic_ieee = i;
333170530Ssam		}
334145247Sdamien	}
335145247Sdamien
336145247Sdamien	/* check support for radio transmitter switch in EEPROM */
337145247Sdamien	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
338145247Sdamien		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
339145247Sdamien
340190526Ssam	ieee80211_ifattach(ic, macaddr);
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;
346178354Ssam	ic->ic_raw_xmit = ipw_raw_xmit;
347172567Sthompsa
348178354Ssam	ic->ic_vap_create = ipw_vap_create;
349178354Ssam	ic->ic_vap_delete = ipw_vap_delete;
350145247Sdamien
351192468Ssam	ieee80211_radiotap_attach(ic,
352192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
353192468Ssam		IPW_TX_RADIOTAP_PRESENT,
354192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
355192468Ssam		IPW_RX_RADIOTAP_PRESENT);
356178354Ssam
357145247Sdamien	/*
358145247Sdamien	 * Add a few sysctl knobs.
359145247Sdamien	 */
360145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
361145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
362145247Sdamien	    CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
363145247Sdamien	    "radio transmitter switch state (0=off, 1=on)");
364145247Sdamien
365145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
366145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
367145247Sdamien	    CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
368145247Sdamien	    "statistics");
369145247Sdamien
370145247Sdamien	/*
371145247Sdamien	 * Hook our interrupt after all initialization is complete.
372145247Sdamien	 */
373145247Sdamien	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
374166901Spiso	    NULL, ipw_intr, sc, &sc->sc_ih);
375145247Sdamien	if (error != 0) {
376145247Sdamien		device_printf(dev, "could not set up interrupt\n");
377178354Ssam		goto fail4;
378145247Sdamien	}
379145247Sdamien
380145247Sdamien	if (bootverbose)
381145247Sdamien		ieee80211_announce(ic);
382145247Sdamien
383145247Sdamien	return 0;
384178354Ssamfail4:
385178354Ssam	if_free(ifp);
386178354Ssamfail3:
387178354Ssam	ipw_release(sc);
388178354Ssamfail2:
389178354Ssam	bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
390178354Ssamfail1:
391178354Ssam	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
392178354Ssamfail:
393178354Ssam	mtx_destroy(&sc->sc_mtx);
394145247Sdamien	return ENXIO;
395145247Sdamien}
396145247Sdamien
397145247Sdamienstatic int
398145247Sdamienipw_detach(device_t dev)
399145247Sdamien{
400145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
401178354Ssam	struct ifnet *ifp = sc->sc_ifp;
402178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
403145247Sdamien
404191912Sthompsa	ieee80211_draintask(ic, &sc->sc_init_task);
405145247Sdamien	ipw_stop(sc);
406178354Ssam
407178354Ssam	ieee80211_ifdetach(ic);
408178354Ssam
409172567Sthompsa	callout_drain(&sc->sc_wdtimer);
410145247Sdamien
411145247Sdamien	ipw_release(sc);
412145247Sdamien
413178354Ssam	bus_teardown_intr(dev, sc->irq, sc->sc_ih);
414178354Ssam	bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
415145247Sdamien
416178354Ssam	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
417156599Sdamien
418178354Ssam	if_free(ifp);
419145247Sdamien
420159487Siedowse	if (sc->sc_firmware != NULL) {
421159487Siedowse		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
422159487Siedowse		sc->sc_firmware = NULL;
423159487Siedowse	}
424159487Siedowse
425145247Sdamien	mtx_destroy(&sc->sc_mtx);
426145247Sdamien
427145247Sdamien	return 0;
428145247Sdamien}
429145247Sdamien
430178354Ssamstatic struct ieee80211vap *
431228621Sbschmidtipw_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
432228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
433228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
434228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
435178354Ssam{
436178354Ssam	struct ifnet *ifp = ic->ic_ifp;
437178354Ssam	struct ipw_softc *sc = ifp->if_softc;
438178354Ssam	struct ipw_vap *ivp;
439178354Ssam	struct ieee80211vap *vap;
440178354Ssam	const struct firmware *fp;
441178354Ssam	const struct ipw_firmware_hdr *hdr;
442178354Ssam	const char *imagename;
443178354Ssam
444178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
445178354Ssam		return NULL;
446178354Ssam
447178354Ssam	switch (opmode) {
448178354Ssam	case IEEE80211_M_STA:
449178354Ssam		imagename = "ipw_bss";
450178354Ssam		break;
451178354Ssam	case IEEE80211_M_IBSS:
452178354Ssam		imagename = "ipw_ibss";
453178354Ssam		break;
454178354Ssam	case IEEE80211_M_MONITOR:
455178354Ssam		imagename = "ipw_monitor";
456178354Ssam		break;
457178354Ssam	default:
458178354Ssam		return NULL;
459178354Ssam	}
460178354Ssam
461178354Ssam	/*
462178354Ssam	 * Load firmware image using the firmware(9) subsystem.  Doing
463178354Ssam	 * this unlocked is ok since we're single-threaded by the
464178354Ssam	 * 802.11 layer.
465178354Ssam	 */
466178354Ssam	if (sc->sc_firmware == NULL ||
467178354Ssam	    strcmp(sc->sc_firmware->name, imagename) != 0) {
468178354Ssam		if (sc->sc_firmware != NULL)
469178354Ssam			firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
470178354Ssam		sc->sc_firmware = firmware_get(imagename);
471178354Ssam	}
472178354Ssam	if (sc->sc_firmware == NULL) {
473178354Ssam		device_printf(sc->sc_dev,
474178354Ssam		    "could not load firmware image '%s'\n", imagename);
475178354Ssam		return NULL;
476178354Ssam	}
477178354Ssam	fp = sc->sc_firmware;
478178354Ssam	if (fp->datasize < sizeof *hdr) {
479178354Ssam		device_printf(sc->sc_dev,
480178354Ssam		    "firmware image too short %zu\n", fp->datasize);
481178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
482178354Ssam		sc->sc_firmware = NULL;
483178354Ssam		return NULL;
484178354Ssam	}
485178354Ssam	hdr = (const struct ipw_firmware_hdr *)fp->data;
486178354Ssam	if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
487178354Ssam	    le32toh(hdr->ucodesz)) {
488178354Ssam		device_printf(sc->sc_dev,
489178354Ssam		    "firmware image too short %zu\n", fp->datasize);
490178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
491178354Ssam		sc->sc_firmware = NULL;
492178354Ssam		return NULL;
493178354Ssam	}
494178354Ssam
495178354Ssam	ivp = (struct ipw_vap *) malloc(sizeof(struct ipw_vap),
496178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
497178354Ssam	if (ivp == NULL)
498178354Ssam		return NULL;
499178354Ssam	vap = &ivp->vap;
500178354Ssam
501178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
502178354Ssam	/* override with driver methods */
503178354Ssam	ivp->newstate = vap->iv_newstate;
504178354Ssam	vap->iv_newstate = ipw_newstate;
505178354Ssam
506178354Ssam	/* complete setup */
507178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status);
508178354Ssam	ic->ic_opmode = opmode;
509178354Ssam	return vap;
510178354Ssam}
511178354Ssam
512178354Ssamstatic void
513178354Ssamipw_vap_delete(struct ieee80211vap *vap)
514178354Ssam{
515178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
516178354Ssam
517178354Ssam	ieee80211_vap_detach(vap);
518178354Ssam	free(ivp, M_80211_VAP);
519178354Ssam}
520178354Ssam
521145247Sdamienstatic int
522145247Sdamienipw_dma_alloc(struct ipw_softc *sc)
523145247Sdamien{
524145247Sdamien	struct ipw_soft_bd *sbd;
525145247Sdamien	struct ipw_soft_hdr *shdr;
526145247Sdamien	struct ipw_soft_buf *sbuf;
527145247Sdamien	bus_addr_t physaddr;
528145247Sdamien	int error, i;
529145247Sdamien
530145247Sdamien	/*
531232874Sscottl	 * Allocate parent DMA tag for subsequent allocations.
532232874Sscottl	 */
533232874Sscottl	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
534232874Sscottl	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
535232874Sscottl	    BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED,
536232874Sscottl	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &sc->parent_dmat);
537232874Sscottl	if (error != 0) {
538232874Sscottl		device_printf(sc->sc_dev, "could not create parent DMA tag\n");
539232874Sscottl		goto fail;
540232874Sscottl	}
541232874Sscottl
542232874Sscottl	/*
543145247Sdamien	 * Allocate and map tx ring.
544145247Sdamien	 */
545232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
546145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
547145247Sdamien	    NULL, &sc->tbd_dmat);
548145247Sdamien	if (error != 0) {
549145247Sdamien		device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
550145247Sdamien		goto fail;
551145247Sdamien	}
552145247Sdamien
553145247Sdamien	error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
554145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
555145247Sdamien	if (error != 0) {
556145247Sdamien		device_printf(sc->sc_dev,
557145247Sdamien		    "could not allocate tx ring DMA memory\n");
558145247Sdamien		goto fail;
559145247Sdamien	}
560145247Sdamien
561145247Sdamien	error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
562145247Sdamien	    IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
563145247Sdamien	if (error != 0) {
564145247Sdamien		device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
565145247Sdamien		goto fail;
566145247Sdamien	}
567145247Sdamien
568145247Sdamien	/*
569145247Sdamien	 * Allocate and map rx ring.
570145247Sdamien	 */
571232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
572145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
573145247Sdamien	    NULL, &sc->rbd_dmat);
574145247Sdamien	if (error != 0) {
575145247Sdamien		device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
576145247Sdamien		goto fail;
577145247Sdamien	}
578145247Sdamien
579145247Sdamien	error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
580145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
581145247Sdamien	if (error != 0) {
582145247Sdamien		device_printf(sc->sc_dev,
583145247Sdamien		    "could not allocate rx ring DMA memory\n");
584145247Sdamien		goto fail;
585145247Sdamien	}
586145247Sdamien
587145247Sdamien	error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
588145247Sdamien	    IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
589145247Sdamien	if (error != 0) {
590145247Sdamien		device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
591145247Sdamien		goto fail;
592145247Sdamien	}
593145247Sdamien
594145247Sdamien	/*
595145247Sdamien	 * Allocate and map status ring.
596145247Sdamien	 */
597232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
598145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
599145247Sdamien	    NULL, NULL, &sc->status_dmat);
600145247Sdamien	if (error != 0) {
601145247Sdamien		device_printf(sc->sc_dev,
602145247Sdamien		    "could not create status ring DMA tag\n");
603145247Sdamien		goto fail;
604145247Sdamien	}
605145247Sdamien
606145247Sdamien	error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
607145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
608145247Sdamien	if (error != 0) {
609145247Sdamien		device_printf(sc->sc_dev,
610145247Sdamien		    "could not allocate status ring DMA memory\n");
611145247Sdamien		goto fail;
612145247Sdamien	}
613145247Sdamien
614145247Sdamien	error = bus_dmamap_load(sc->status_dmat, sc->status_map,
615145247Sdamien	    sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
616145247Sdamien	    0);
617145247Sdamien	if (error != 0) {
618145247Sdamien		device_printf(sc->sc_dev,
619145247Sdamien		    "could not map status ring DMA memory\n");
620145247Sdamien		goto fail;
621145247Sdamien	}
622145247Sdamien
623145247Sdamien	/*
624145247Sdamien	 * Allocate command DMA map.
625145247Sdamien	 */
626232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
627145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
628145247Sdamien	    sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
629145247Sdamien	if (error != 0) {
630145247Sdamien		device_printf(sc->sc_dev, "could not create command DMA tag\n");
631145247Sdamien		goto fail;
632145247Sdamien	}
633145247Sdamien
634145247Sdamien	error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
635145247Sdamien	if (error != 0) {
636145247Sdamien		device_printf(sc->sc_dev,
637145247Sdamien		    "could not create command DMA map\n");
638145247Sdamien		goto fail;
639145247Sdamien	}
640145247Sdamien
641145247Sdamien	/*
642145247Sdamien	 * Allocate headers DMA maps.
643145247Sdamien	 */
644232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
645145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
646145247Sdamien	    sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
647145247Sdamien	if (error != 0) {
648145247Sdamien		device_printf(sc->sc_dev, "could not create header DMA tag\n");
649145247Sdamien		goto fail;
650145247Sdamien	}
651145247Sdamien
652145247Sdamien	SLIST_INIT(&sc->free_shdr);
653145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
654145247Sdamien		shdr = &sc->shdr_list[i];
655145247Sdamien		error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
656145247Sdamien		if (error != 0) {
657145247Sdamien			device_printf(sc->sc_dev,
658145247Sdamien			    "could not create header DMA map\n");
659145247Sdamien			goto fail;
660145247Sdamien		}
661145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
662145247Sdamien	}
663145247Sdamien
664145247Sdamien	/*
665145247Sdamien	 * Allocate tx buffers DMA maps.
666145247Sdamien	 */
667232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
668145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
669145247Sdamien	    NULL, NULL, &sc->txbuf_dmat);
670145247Sdamien	if (error != 0) {
671145247Sdamien		device_printf(sc->sc_dev, "could not create tx DMA tag\n");
672145247Sdamien		goto fail;
673145247Sdamien	}
674145247Sdamien
675145247Sdamien	SLIST_INIT(&sc->free_sbuf);
676145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
677145247Sdamien		sbuf = &sc->tx_sbuf_list[i];
678145247Sdamien		error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
679145247Sdamien		if (error != 0) {
680145247Sdamien			device_printf(sc->sc_dev,
681145247Sdamien			    "could not create tx DMA map\n");
682145247Sdamien			goto fail;
683145247Sdamien		}
684145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
685145247Sdamien	}
686145247Sdamien
687145247Sdamien	/*
688145247Sdamien	 * Initialize tx ring.
689145247Sdamien	 */
690145247Sdamien	for (i = 0; i < IPW_NTBD; i++) {
691145247Sdamien		sbd = &sc->stbd_list[i];
692145247Sdamien		sbd->bd = &sc->tbd_list[i];
693145247Sdamien		sbd->type = IPW_SBD_TYPE_NOASSOC;
694145247Sdamien	}
695145247Sdamien
696145247Sdamien	/*
697145247Sdamien	 * Pre-allocate rx buffers and DMA maps.
698145247Sdamien	 */
699232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
700147841Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
701147841Sdamien	    NULL, &sc->rxbuf_dmat);
702145247Sdamien	if (error != 0) {
703145247Sdamien		device_printf(sc->sc_dev, "could not create rx DMA tag\n");
704145247Sdamien		goto fail;
705145247Sdamien	}
706145247Sdamien
707145247Sdamien	for (i = 0; i < IPW_NRBD; i++) {
708145247Sdamien		sbd = &sc->srbd_list[i];
709145247Sdamien		sbuf = &sc->rx_sbuf_list[i];
710145247Sdamien		sbd->bd = &sc->rbd_list[i];
711145247Sdamien
712243857Sglebius		sbuf->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
713145247Sdamien		if (sbuf->m == NULL) {
714145247Sdamien			device_printf(sc->sc_dev,
715145247Sdamien			    "could not allocate rx mbuf\n");
716145247Sdamien			error = ENOMEM;
717145247Sdamien			goto fail;
718145247Sdamien		}
719145247Sdamien
720145247Sdamien		error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
721145247Sdamien		if (error != 0) {
722145247Sdamien			device_printf(sc->sc_dev,
723145247Sdamien			    "could not create rx DMA map\n");
724145247Sdamien			goto fail;
725145247Sdamien		}
726145247Sdamien
727145247Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
728145247Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
729145247Sdamien		    &physaddr, 0);
730145247Sdamien		if (error != 0) {
731145247Sdamien			device_printf(sc->sc_dev,
732145247Sdamien			    "could not map rx DMA memory\n");
733145247Sdamien			goto fail;
734145247Sdamien		}
735145247Sdamien
736145247Sdamien		sbd->type = IPW_SBD_TYPE_DATA;
737145247Sdamien		sbd->priv = sbuf;
738145247Sdamien		sbd->bd->physaddr = htole32(physaddr);
739145247Sdamien		sbd->bd->len = htole32(MCLBYTES);
740145247Sdamien	}
741145247Sdamien
742145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
743145247Sdamien
744145247Sdamien	return 0;
745145247Sdamien
746145247Sdamienfail:	ipw_release(sc);
747145247Sdamien	return error;
748145247Sdamien}
749145247Sdamien
750145247Sdamienstatic void
751145247Sdamienipw_release(struct ipw_softc *sc)
752145247Sdamien{
753145247Sdamien	struct ipw_soft_buf *sbuf;
754145247Sdamien	int i;
755145247Sdamien
756232874Sscottl	if (sc->parent_dmat != NULL) {
757232874Sscottl		bus_dma_tag_destroy(sc->parent_dmat);
758232874Sscottl	}
759232874Sscottl
760145247Sdamien	if (sc->tbd_dmat != NULL) {
761145247Sdamien		if (sc->stbd_list != NULL) {
762145247Sdamien			bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
763145247Sdamien			bus_dmamem_free(sc->tbd_dmat, sc->tbd_list,
764145247Sdamien			    sc->tbd_map);
765145247Sdamien		}
766145247Sdamien		bus_dma_tag_destroy(sc->tbd_dmat);
767145247Sdamien	}
768145247Sdamien
769145247Sdamien	if (sc->rbd_dmat != NULL) {
770145247Sdamien		if (sc->rbd_list != NULL) {
771145247Sdamien			bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
772145247Sdamien			bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
773145247Sdamien			    sc->rbd_map);
774145247Sdamien		}
775145247Sdamien		bus_dma_tag_destroy(sc->rbd_dmat);
776145247Sdamien	}
777145247Sdamien
778145247Sdamien	if (sc->status_dmat != NULL) {
779145247Sdamien		if (sc->status_list != NULL) {
780145247Sdamien			bus_dmamap_unload(sc->status_dmat, sc->status_map);
781145247Sdamien			bus_dmamem_free(sc->status_dmat, sc->status_list,
782145247Sdamien			    sc->status_map);
783145247Sdamien		}
784145247Sdamien		bus_dma_tag_destroy(sc->status_dmat);
785145247Sdamien	}
786145247Sdamien
787145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
788145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
789145247Sdamien
790145247Sdamien	if (sc->cmd_dmat != NULL) {
791145247Sdamien		bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
792145247Sdamien		bus_dma_tag_destroy(sc->cmd_dmat);
793145247Sdamien	}
794145247Sdamien
795145247Sdamien	if (sc->hdr_dmat != NULL) {
796145247Sdamien		for (i = 0; i < IPW_NDATA; i++)
797145247Sdamien			bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
798145247Sdamien		bus_dma_tag_destroy(sc->hdr_dmat);
799145247Sdamien	}
800145247Sdamien
801145247Sdamien	if (sc->txbuf_dmat != NULL) {
802145247Sdamien		for (i = 0; i < IPW_NDATA; i++) {
803145247Sdamien			bus_dmamap_destroy(sc->txbuf_dmat,
804145247Sdamien			    sc->tx_sbuf_list[i].map);
805145247Sdamien		}
806145247Sdamien		bus_dma_tag_destroy(sc->txbuf_dmat);
807145247Sdamien	}
808145247Sdamien
809145247Sdamien	if (sc->rxbuf_dmat != NULL) {
810145247Sdamien		for (i = 0; i < IPW_NRBD; i++) {
811145247Sdamien			sbuf = &sc->rx_sbuf_list[i];
812145247Sdamien			if (sbuf->m != NULL) {
813145247Sdamien				bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
814145247Sdamien				    BUS_DMASYNC_POSTREAD);
815145247Sdamien				bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
816145247Sdamien				m_freem(sbuf->m);
817145247Sdamien			}
818145247Sdamien			bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
819145247Sdamien		}
820145247Sdamien		bus_dma_tag_destroy(sc->rxbuf_dmat);
821145247Sdamien	}
822145247Sdamien}
823145247Sdamien
824145247Sdamienstatic int
825145247Sdamienipw_shutdown(device_t dev)
826145247Sdamien{
827145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
828145247Sdamien
829145247Sdamien	ipw_stop(sc);
830145247Sdamien
831145247Sdamien	return 0;
832145247Sdamien}
833145247Sdamien
834145247Sdamienstatic int
835145247Sdamienipw_suspend(device_t dev)
836145247Sdamien{
837145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
838233387Sbschmidt	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
839145247Sdamien
840233387Sbschmidt	ieee80211_suspend_all(ic);
841145247Sdamien	return 0;
842145247Sdamien}
843145247Sdamien
844145247Sdamienstatic int
845145247Sdamienipw_resume(device_t dev)
846145247Sdamien{
847145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
848233387Sbschmidt	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
849145247Sdamien
850146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
851146498Sdamien
852233387Sbschmidt	ieee80211_resume_all(ic);
853145247Sdamien	return 0;
854145247Sdamien}
855145247Sdamien
856145247Sdamienstatic int
857172567Sthompsaipw_cvtrate(int ipwrate)
858172567Sthompsa{
859172567Sthompsa	switch (ipwrate) {
860172567Sthompsa	case IPW_RATE_DS1:	return 2;
861172567Sthompsa	case IPW_RATE_DS2:	return 4;
862172567Sthompsa	case IPW_RATE_DS5:	return 11;
863172567Sthompsa	case IPW_RATE_DS11:	return 22;
864172567Sthompsa	}
865145247Sdamien	return 0;
866145247Sdamien}
867145247Sdamien
868145247Sdamien/*
869156599Sdamien * The firmware automatically adapts the transmit speed. We report its current
870156599Sdamien * value here.
871145247Sdamien */
872145247Sdamienstatic void
873145247Sdamienipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
874145247Sdamien{
875178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
876178354Ssam	struct ieee80211com *ic = vap->iv_ic;
877178354Ssam	struct ipw_softc *sc = ic->ic_ifp->if_softc;
878145247Sdamien
879145247Sdamien	/* read current transmission rate from adapter */
880178354Ssam	vap->iv_bss->ni_txrate = ipw_cvtrate(
881178354Ssam	    ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
882178354Ssam	ieee80211_media_status(ifp, imr);
883145247Sdamien}
884145247Sdamien
885145247Sdamienstatic int
886178354Ssamipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
887145247Sdamien{
888178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
889178354Ssam	struct ieee80211com *ic = vap->iv_ic;
890145247Sdamien	struct ifnet *ifp = ic->ic_ifp;
891145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
892191746Sthompsa	enum ieee80211_state ostate;
893145247Sdamien
894172567Sthompsa	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
895178354Ssam		ieee80211_state_name[vap->iv_state],
896172567Sthompsa		ieee80211_state_name[nstate], sc->flags));
897172567Sthompsa
898191746Sthompsa	ostate = vap->iv_state;
899191746Sthompsa	IEEE80211_UNLOCK(ic);
900191746Sthompsa
901145247Sdamien	switch (nstate) {
902145247Sdamien	case IEEE80211_S_RUN:
903172567Sthompsa		if (ic->ic_opmode == IEEE80211_M_IBSS) {
904172567Sthompsa			/*
905172567Sthompsa			 * XXX when joining an ibss network we are called
906172567Sthompsa			 * with a SCAN -> RUN transition on scan complete.
907206763Sbschmidt			 * Use that to call ipw_assoc.  On completing the
908206763Sbschmidt			 * join we are then called again with an AUTH -> RUN
909206763Sbschmidt			 * transition and we want to do nothing.  This is
910206763Sbschmidt			 * all totally bogus and needs to be redone.
911172567Sthompsa			 */
912191746Sthompsa			if (ostate == IEEE80211_S_SCAN)
913191746Sthompsa				ipw_assoc(ic, vap);
914172567Sthompsa		}
915172567Sthompsa		break;
916145247Sdamien
917172567Sthompsa	case IEEE80211_S_INIT:
918172567Sthompsa		if (sc->flags & IPW_FLAG_ASSOCIATED)
919191746Sthompsa			ipw_disassoc(ic, vap);
920172567Sthompsa		break;
921145247Sdamien
922172567Sthompsa	case IEEE80211_S_AUTH:
923206765Sbschmidt		/*
924206765Sbschmidt		 * Move to ASSOC state after the ipw_assoc() call.  Firmware
925206765Sbschmidt		 * takes care of authentication, after the call we'll receive
926206765Sbschmidt		 * only an assoc response which would otherwise be discared
927206765Sbschmidt		 * if we are still in AUTH state.
928206765Sbschmidt		 */
929206765Sbschmidt		nstate = IEEE80211_S_ASSOC;
930191746Sthompsa		ipw_assoc(ic, vap);
931191746Sthompsa		break;
932145247Sdamien
933172567Sthompsa	case IEEE80211_S_ASSOC:
934172567Sthompsa		/*
935206763Sbschmidt		 * If we are not transitioning from AUTH then resend the
936172567Sthompsa		 * association request.
937172567Sthompsa		 */
938191746Sthompsa		if (ostate != IEEE80211_S_AUTH)
939191746Sthompsa			ipw_assoc(ic, vap);
940145247Sdamien		break;
941145247Sdamien
942172058Ssam	default:
943145247Sdamien		break;
944145247Sdamien	}
945191746Sthompsa	IEEE80211_LOCK(ic);
946178354Ssam	return ivp->newstate(vap, nstate, arg);
947145247Sdamien}
948145247Sdamien
949145247Sdamien/*
950145247Sdamien * Read 16 bits at address 'addr' from the serial EEPROM.
951145247Sdamien */
952145247Sdamienstatic uint16_t
953145247Sdamienipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
954145247Sdamien{
955145247Sdamien	uint32_t tmp;
956145247Sdamien	uint16_t val;
957145247Sdamien	int n;
958145247Sdamien
959145247Sdamien	/* clock C once before the first command */
960145247Sdamien	IPW_EEPROM_CTL(sc, 0);
961145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
962145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
963145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
964145247Sdamien
965145247Sdamien	/* write start bit (1) */
966145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
967145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
968145247Sdamien
969145247Sdamien	/* write READ opcode (10) */
970145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
971145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
972145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
973145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
974145247Sdamien
975145247Sdamien	/* write address A7-A0 */
976145247Sdamien	for (n = 7; n >= 0; n--) {
977145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
978145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
979145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
980145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
981145247Sdamien	}
982145247Sdamien
983145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
984145247Sdamien
985145247Sdamien	/* read data Q15-Q0 */
986145247Sdamien	val = 0;
987145247Sdamien	for (n = 15; n >= 0; n--) {
988145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
989145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
990145247Sdamien		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
991145247Sdamien		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
992145247Sdamien	}
993145247Sdamien
994145247Sdamien	IPW_EEPROM_CTL(sc, 0);
995145247Sdamien
996145247Sdamien	/* clear Chip Select and clock C */
997145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
998145247Sdamien	IPW_EEPROM_CTL(sc, 0);
999145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
1000145247Sdamien
1001145247Sdamien	return le16toh(val);
1002145247Sdamien}
1003145247Sdamien
1004145247Sdamienstatic void
1005172567Sthompsaipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1006145247Sdamien{
1007145247Sdamien	struct ipw_cmd *cmd;
1008145247Sdamien
1009145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1010145247Sdamien
1011145247Sdamien	cmd = mtod(sbuf->m, struct ipw_cmd *);
1012145247Sdamien
1013172567Sthompsa	DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
1014172567Sthompsa	    ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
1015145247Sdamien	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
1016145247Sdamien	    le32toh(cmd->status)));
1017145247Sdamien
1018172567Sthompsa	sc->flags &= ~IPW_FLAG_BUSY;
1019145247Sdamien	wakeup(sc);
1020145247Sdamien}
1021145247Sdamien
1022145247Sdamienstatic void
1023172567Sthompsaipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1024145247Sdamien{
1025178354Ssam#define	IEEESTATE(vap)	ieee80211_state_name[vap->iv_state]
1026178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1027178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1028178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1029145247Sdamien	uint32_t state;
1030145247Sdamien
1031145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1032145247Sdamien
1033145247Sdamien	state = le32toh(*mtod(sbuf->m, uint32_t *));
1034145247Sdamien
1035145247Sdamien	switch (state) {
1036145247Sdamien	case IPW_STATE_ASSOCIATED:
1037172567Sthompsa		DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1038178354Ssam			IEEESTATE(vap), sc->flags));
1039178354Ssam		/* XXX suppress state change in case the fw auto-associates */
1040178354Ssam		if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1041178354Ssam			DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1042178354Ssam				IEEESTATE(vap), sc->flags));
1043178354Ssam			break;
1044178354Ssam		}
1045178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATING;
1046172567Sthompsa		sc->flags |= IPW_FLAG_ASSOCIATED;
1047145247Sdamien		break;
1048145247Sdamien
1049145247Sdamien	case IPW_STATE_SCANNING:
1050172567Sthompsa		DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1051178354Ssam			IEEESTATE(vap), sc->flags));
1052172567Sthompsa		/*
1053172567Sthompsa		 * NB: Check driver state for association on assoc
1054172567Sthompsa		 * loss as the firmware will immediately start to
1055172567Sthompsa		 * scan and we would treat it as a beacon miss if
1056172567Sthompsa		 * we checked the 802.11 layer state.
1057172567Sthompsa		 */
1058178354Ssam		if (sc->flags & IPW_FLAG_ASSOCIATED) {
1059206767Sbschmidt			IPW_UNLOCK(sc);
1060178354Ssam			/* XXX probably need to issue disassoc to fw */
1061191746Sthompsa			ieee80211_beacon_miss(ic);
1062206767Sbschmidt			IPW_LOCK(sc);
1063178354Ssam		}
1064145247Sdamien		break;
1065145247Sdamien
1066145247Sdamien	case IPW_STATE_SCAN_COMPLETE:
1067172567Sthompsa		/*
1068172567Sthompsa		 * XXX For some reason scan requests generate scan
1069172567Sthompsa		 * started + scan done events before any traffic is
1070172567Sthompsa		 * received (e.g. probe response frames).  We work
1071172567Sthompsa		 * around this by marking the HACK flag and skipping
1072172567Sthompsa		 * the first scan complete event.
1073172567Sthompsa		*/
1074178354Ssam		DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1075178354Ssam			    IEEESTATE(vap), sc->flags));
1076172567Sthompsa		if (sc->flags & IPW_FLAG_HACK) {
1077172567Sthompsa			sc->flags &= ~IPW_FLAG_HACK;
1078172567Sthompsa			break;
1079172567Sthompsa		}
1080172567Sthompsa		if (sc->flags & IPW_FLAG_SCANNING) {
1081206767Sbschmidt			IPW_UNLOCK(sc);
1082191746Sthompsa			ieee80211_scan_done(vap);
1083206767Sbschmidt			IPW_LOCK(sc);
1084172567Sthompsa			sc->flags &= ~IPW_FLAG_SCANNING;
1085172567Sthompsa			sc->sc_scan_timer = 0;
1086172567Sthompsa		}
1087145247Sdamien		break;
1088145247Sdamien
1089145247Sdamien	case IPW_STATE_ASSOCIATION_LOST:
1090172567Sthompsa		DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1091178354Ssam			IEEESTATE(vap), sc->flags));
1092178354Ssam		sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1093206767Sbschmidt		if (vap->iv_state == IEEE80211_S_RUN) {
1094206767Sbschmidt			IPW_UNLOCK(sc);
1095191746Sthompsa			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1096206767Sbschmidt			IPW_LOCK(sc);
1097206767Sbschmidt		}
1098145247Sdamien		break;
1099145247Sdamien
1100172567Sthompsa	case IPW_STATE_DISABLED:
1101178354Ssam		/* XXX? is this right? */
1102206763Sbschmidt		sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1103178354Ssam		    IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1104172567Sthompsa		DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1105178354Ssam			IEEESTATE(vap), sc->flags));
1106172567Sthompsa		break;
1107172567Sthompsa
1108145247Sdamien	case IPW_STATE_RADIO_DISABLED:
1109178354Ssam		device_printf(sc->sc_dev, "radio turned off\n");
1110178354Ssam		ieee80211_notify_radio(ic, 0);
1111172567Sthompsa		ipw_stop_locked(sc);
1112178354Ssam		/* XXX start polling thread to detect radio on */
1113145247Sdamien		break;
1114172567Sthompsa
1115172567Sthompsa	default:
1116172567Sthompsa		DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1117178354Ssam			__func__, state, IEEESTATE(vap), sc->flags));
1118172567Sthompsa		break;
1119145247Sdamien	}
1120172567Sthompsa#undef IEEESTATE
1121145247Sdamien}
1122145247Sdamien
1123145247Sdamien/*
1124172567Sthompsa * Set driver state for current channel.
1125172567Sthompsa */
1126172567Sthompsastatic void
1127172567Sthompsaipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1128172567Sthompsa{
1129178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1130178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1131172567Sthompsa
1132172567Sthompsa	ic->ic_curchan = chan;
1133192468Ssam	ieee80211_radiotap_chan_change(ic);
1134172567Sthompsa}
1135172567Sthompsa
1136172567Sthompsa/*
1137145247Sdamien * XXX: Hack to set the current channel to the value advertised in beacons or
1138145247Sdamien * probe responses. Only used during AP detection.
1139145247Sdamien */
1140145247Sdamienstatic void
1141172567Sthompsaipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1142145247Sdamien{
1143178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1144178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1145172567Sthompsa	struct ieee80211_channel *c;
1146145247Sdamien	struct ieee80211_frame *wh;
1147145247Sdamien	uint8_t subtype;
1148145247Sdamien	uint8_t *frm, *efrm;
1149145247Sdamien
1150145247Sdamien	wh = mtod(m, struct ieee80211_frame *);
1151145247Sdamien
1152145247Sdamien	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1153145247Sdamien		return;
1154145247Sdamien
1155145247Sdamien	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1156145247Sdamien
1157145247Sdamien	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1158145247Sdamien	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1159145247Sdamien		return;
1160145247Sdamien
1161178354Ssam	/* XXX use ieee80211_parse_beacon */
1162145247Sdamien	frm = (uint8_t *)(wh + 1);
1163145247Sdamien	efrm = mtod(m, uint8_t *) + m->m_len;
1164145247Sdamien
1165145247Sdamien	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1166145247Sdamien	while (frm < efrm) {
1167145247Sdamien		if (*frm == IEEE80211_ELEMID_DSPARMS)
1168145247Sdamien#if IEEE80211_CHAN_MAX < 255
1169145247Sdamien		if (frm[2] <= IEEE80211_CHAN_MAX)
1170145247Sdamien#endif
1171172567Sthompsa		{
1172172567Sthompsa			DPRINTF(("Fixing channel to %d\n", frm[2]));
1173172567Sthompsa			c = ieee80211_find_channel(ic,
1174170530Ssam				ieee80211_ieee2mhz(frm[2], 0),
1175172567Sthompsa				IEEE80211_CHAN_B);
1176172567Sthompsa			if (c == NULL)
1177172567Sthompsa				c = &ic->ic_channels[0];
1178172567Sthompsa			ipw_setcurchan(sc, c);
1179172567Sthompsa		}
1180145247Sdamien
1181145247Sdamien		frm += frm[1] + 2;
1182145247Sdamien	}
1183145247Sdamien}
1184145247Sdamien
1185145247Sdamienstatic void
1186172567Sthompsaipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1187145247Sdamien    struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1188145247Sdamien{
1189178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1190178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1191152385Sdamien	struct mbuf *mnew, *m;
1192145247Sdamien	struct ieee80211_node *ni;
1193145247Sdamien	bus_addr_t physaddr;
1194145247Sdamien	int error;
1195192468Ssam	int8_t rssi, nf;
1196145247Sdamien
1197152385Sdamien	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1198152385Sdamien	    status->rssi));
1199152385Sdamien
1200147757Sdamien	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1201147757Sdamien	    le32toh(status->len) > MCLBYTES)
1202147757Sdamien		return;
1203147757Sdamien
1204152385Sdamien	/*
1205152385Sdamien	 * Try to allocate a new mbuf for this ring element and load it before
1206152385Sdamien	 * processing the current mbuf. If the ring element cannot be loaded,
1207152385Sdamien	 * drop the received packet and reuse the old mbuf. In the unlikely
1208152385Sdamien	 * case that the old mbuf can't be reloaded either, explicitly panic.
1209152385Sdamien	 */
1210243857Sglebius	mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1211152385Sdamien	if (mnew == NULL) {
1212152385Sdamien		ifp->if_ierrors++;
1213152385Sdamien		return;
1214152385Sdamien	}
1215152385Sdamien
1216145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1217145247Sdamien	bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1218145247Sdamien
1219152385Sdamien	error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1220152385Sdamien	    MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1221152385Sdamien	if (error != 0) {
1222152385Sdamien		m_freem(mnew);
1223152385Sdamien
1224152385Sdamien		/* try to reload the old mbuf */
1225152385Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1226152385Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1227152385Sdamien		    &physaddr, 0);
1228152385Sdamien		if (error != 0) {
1229152385Sdamien			/* very unlikely that it will fail... */
1230152385Sdamien			panic("%s: could not load old rx mbuf",
1231152385Sdamien			    device_get_name(sc->sc_dev));
1232152385Sdamien		}
1233152385Sdamien		ifp->if_ierrors++;
1234152385Sdamien		return;
1235152385Sdamien	}
1236152385Sdamien
1237152385Sdamien	/*
1238152385Sdamien	 * New mbuf successfully loaded, update Rx ring and continue
1239152385Sdamien	 * processing.
1240152385Sdamien	 */
1241152385Sdamien	m = sbuf->m;
1242152385Sdamien	sbuf->m = mnew;
1243152385Sdamien	sbd->bd->physaddr = htole32(physaddr);
1244152385Sdamien
1245145247Sdamien	/* finalize mbuf */
1246145247Sdamien	m->m_pkthdr.rcvif = ifp;
1247145247Sdamien	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1248145247Sdamien
1249192468Ssam	rssi = status->rssi + IPW_RSSI_TO_DBM;
1250192468Ssam	nf = -95;
1251192468Ssam	if (ieee80211_radiotap_active(ic)) {
1252145247Sdamien		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1253145247Sdamien
1254145247Sdamien		tap->wr_flags = 0;
1255192468Ssam		tap->wr_antsignal = rssi;
1256192468Ssam		tap->wr_antnoise = nf;
1257145247Sdamien	}
1258145247Sdamien
1259172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
1260172567Sthompsa		ipw_fix_channel(sc, m);
1261145247Sdamien
1262172567Sthompsa	IPW_UNLOCK(sc);
1263178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1264178354Ssam	if (ni != NULL) {
1265206764Sbschmidt		(void) ieee80211_input(ni, m, rssi - nf, nf);
1266178354Ssam		ieee80211_free_node(ni);
1267178354Ssam	} else
1268206764Sbschmidt		(void) ieee80211_input_all(ic, m, rssi - nf, nf);
1269172567Sthompsa	IPW_LOCK(sc);
1270145247Sdamien
1271145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1272145247Sdamien}
1273145247Sdamien
1274145247Sdamienstatic void
1275145247Sdamienipw_rx_intr(struct ipw_softc *sc)
1276145247Sdamien{
1277145247Sdamien	struct ipw_status *status;
1278145247Sdamien	struct ipw_soft_bd *sbd;
1279145247Sdamien	struct ipw_soft_buf *sbuf;
1280145247Sdamien	uint32_t r, i;
1281145247Sdamien
1282145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1283145247Sdamien		return;
1284145247Sdamien
1285145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1286145247Sdamien
1287145247Sdamien	bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1288145247Sdamien
1289145247Sdamien	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1290145247Sdamien		status = &sc->status_list[i];
1291145247Sdamien		sbd = &sc->srbd_list[i];
1292145247Sdamien		sbuf = sbd->priv;
1293145247Sdamien
1294145247Sdamien		switch (le16toh(status->code) & 0xf) {
1295145247Sdamien		case IPW_STATUS_CODE_COMMAND:
1296172567Sthompsa			ipw_rx_cmd_intr(sc, sbuf);
1297145247Sdamien			break;
1298145247Sdamien
1299145247Sdamien		case IPW_STATUS_CODE_NEWSTATE:
1300172567Sthompsa			ipw_rx_newstate_intr(sc, sbuf);
1301145247Sdamien			break;
1302145247Sdamien
1303145247Sdamien		case IPW_STATUS_CODE_DATA_802_3:
1304145247Sdamien		case IPW_STATUS_CODE_DATA_802_11:
1305172567Sthompsa			ipw_rx_data_intr(sc, status, sbd, sbuf);
1306145247Sdamien			break;
1307145247Sdamien
1308145247Sdamien		case IPW_STATUS_CODE_NOTIFICATION:
1309172567Sthompsa			DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1310172567Sthompsa			    le32toh(status->len), status->flags));
1311178354Ssam			/* XXX maybe drive state machine AUTH->ASSOC? */
1312145247Sdamien			break;
1313145247Sdamien
1314145247Sdamien		default:
1315172567Sthompsa			device_printf(sc->sc_dev, "unexpected status code %u\n",
1316145247Sdamien			    le16toh(status->code));
1317145247Sdamien		}
1318145247Sdamien
1319145247Sdamien		/* firmware was killed, stop processing received frames */
1320145247Sdamien		if (!(sc->flags & IPW_FLAG_FW_INITED))
1321145247Sdamien			return;
1322145247Sdamien
1323145247Sdamien		sbd->bd->flags = 0;
1324145247Sdamien	}
1325145247Sdamien
1326145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1327145247Sdamien
1328145247Sdamien	/* kick the firmware */
1329145247Sdamien	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1330145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1331145247Sdamien}
1332145247Sdamien
1333145247Sdamienstatic void
1334145247Sdamienipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1335145247Sdamien{
1336145247Sdamien	struct ipw_soft_hdr *shdr;
1337145247Sdamien	struct ipw_soft_buf *sbuf;
1338145247Sdamien
1339145247Sdamien	switch (sbd->type) {
1340145247Sdamien	case IPW_SBD_TYPE_COMMAND:
1341145247Sdamien		bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1342145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1343145247Sdamien		bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1344145247Sdamien		break;
1345145247Sdamien
1346145247Sdamien	case IPW_SBD_TYPE_HEADER:
1347145247Sdamien		shdr = sbd->priv;
1348145247Sdamien		bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1349145247Sdamien		bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1350145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1351145247Sdamien		break;
1352145247Sdamien
1353145247Sdamien	case IPW_SBD_TYPE_DATA:
1354145247Sdamien		sbuf = sbd->priv;
1355145247Sdamien		bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1356145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1357145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1358145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1359145247Sdamien
1360170530Ssam		if (sbuf->m->m_flags & M_TXCB)
1361170530Ssam			ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1362145247Sdamien		m_freem(sbuf->m);
1363145247Sdamien		ieee80211_free_node(sbuf->ni);
1364145247Sdamien
1365145247Sdamien		sc->sc_tx_timer = 0;
1366145247Sdamien		break;
1367145247Sdamien	}
1368145247Sdamien
1369145247Sdamien	sbd->type = IPW_SBD_TYPE_NOASSOC;
1370145247Sdamien}
1371145247Sdamien
1372145247Sdamienstatic void
1373145247Sdamienipw_tx_intr(struct ipw_softc *sc)
1374145247Sdamien{
1375178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1376145247Sdamien	struct ipw_soft_bd *sbd;
1377145247Sdamien	uint32_t r, i;
1378145247Sdamien
1379145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1380145247Sdamien		return;
1381145247Sdamien
1382145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1383145247Sdamien
1384145247Sdamien	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1385145247Sdamien		sbd = &sc->stbd_list[i];
1386145247Sdamien
1387145247Sdamien		if (sbd->type == IPW_SBD_TYPE_DATA)
1388145247Sdamien			ifp->if_opackets++;
1389145247Sdamien
1390145247Sdamien		ipw_release_sbd(sc, sbd);
1391145247Sdamien		sc->txfree++;
1392145247Sdamien	}
1393145247Sdamien
1394145247Sdamien	/* remember what the firmware has processed */
1395145247Sdamien	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1396145247Sdamien
1397148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1398172567Sthompsa	ipw_start_locked(ifp);
1399145247Sdamien}
1400145247Sdamien
1401145247Sdamienstatic void
1402191746Sthompsaipw_fatal_error_intr(struct ipw_softc *sc)
1403191746Sthompsa{
1404191746Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1405191746Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1406191956Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1407191746Sthompsa
1408191746Sthompsa	device_printf(sc->sc_dev, "firmware error\n");
1409206767Sbschmidt	if (vap != NULL) {
1410206767Sbschmidt		IPW_UNLOCK(sc);
1411191956Sthompsa		ieee80211_cancel_scan(vap);
1412206767Sbschmidt		IPW_LOCK(sc);
1413206767Sbschmidt	}
1414191746Sthompsa	ieee80211_runtask(ic, &sc->sc_init_task);
1415191746Sthompsa}
1416191746Sthompsa
1417191746Sthompsastatic void
1418145247Sdamienipw_intr(void *arg)
1419145247Sdamien{
1420145247Sdamien	struct ipw_softc *sc = arg;
1421145247Sdamien	uint32_t r;
1422145247Sdamien
1423172567Sthompsa	IPW_LOCK(sc);
1424145247Sdamien
1425191746Sthompsa	r = CSR_READ_4(sc, IPW_CSR_INTR);
1426191746Sthompsa	if (r == 0 || r == 0xffffffff)
1427191746Sthompsa		goto done;
1428145247Sdamien
1429145247Sdamien	/* disable interrupts */
1430145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1431145247Sdamien
1432156599Sdamien	/* acknowledge all interrupts */
1433156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1434156599Sdamien
1435145247Sdamien	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1436191746Sthompsa		ipw_fatal_error_intr(sc);
1437191746Sthompsa		goto done;
1438145247Sdamien	}
1439145247Sdamien
1440156599Sdamien	if (r & IPW_INTR_FW_INIT_DONE)
1441156599Sdamien		wakeup(sc);
1442145247Sdamien
1443145247Sdamien	if (r & IPW_INTR_RX_TRANSFER)
1444145247Sdamien		ipw_rx_intr(sc);
1445145247Sdamien
1446145247Sdamien	if (r & IPW_INTR_TX_TRANSFER)
1447145247Sdamien		ipw_tx_intr(sc);
1448145247Sdamien
1449145247Sdamien	/* re-enable interrupts */
1450145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1451191746Sthompsadone:
1452172567Sthompsa	IPW_UNLOCK(sc);
1453145247Sdamien}
1454145247Sdamien
1455145247Sdamienstatic void
1456145247Sdamienipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1457145247Sdamien{
1458145247Sdamien	if (error != 0)
1459145247Sdamien		return;
1460145247Sdamien
1461145247Sdamien	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1462145247Sdamien
1463145247Sdamien	*(bus_addr_t *)arg = segs[0].ds_addr;
1464145247Sdamien}
1465145247Sdamien
1466172567Sthompsastatic const char *
1467172567Sthompsaipw_cmdname(int cmd)
1468172567Sthompsa{
1469172567Sthompsa#define	N(a)	(sizeof(a) / sizeof(a[0]))
1470172567Sthompsa	static const struct {
1471172567Sthompsa		int	cmd;
1472172567Sthompsa		const char *name;
1473172567Sthompsa	} cmds[] = {
1474172567Sthompsa		{ IPW_CMD_ADD_MULTICAST,	"ADD_MULTICAST" },
1475172567Sthompsa		{ IPW_CMD_BROADCAST_SCAN,	"BROADCAST_SCAN" },
1476172567Sthompsa		{ IPW_CMD_DISABLE,		"DISABLE" },
1477172567Sthompsa		{ IPW_CMD_DISABLE_PHY,		"DISABLE_PHY" },
1478172567Sthompsa		{ IPW_CMD_ENABLE,		"ENABLE" },
1479172567Sthompsa		{ IPW_CMD_PREPARE_POWER_DOWN,	"PREPARE_POWER_DOWN" },
1480172567Sthompsa		{ IPW_CMD_SET_BASIC_TX_RATES,	"SET_BASIC_TX_RATES" },
1481172567Sthompsa		{ IPW_CMD_SET_BEACON_INTERVAL,	"SET_BEACON_INTERVAL" },
1482172567Sthompsa		{ IPW_CMD_SET_CHANNEL,		"SET_CHANNEL" },
1483172567Sthompsa		{ IPW_CMD_SET_CONFIGURATION,	"SET_CONFIGURATION" },
1484172567Sthompsa		{ IPW_CMD_SET_DESIRED_BSSID,	"SET_DESIRED_BSSID" },
1485172567Sthompsa		{ IPW_CMD_SET_ESSID,		"SET_ESSID" },
1486172567Sthompsa		{ IPW_CMD_SET_FRAG_THRESHOLD,	"SET_FRAG_THRESHOLD" },
1487172567Sthompsa		{ IPW_CMD_SET_MAC_ADDRESS,	"SET_MAC_ADDRESS" },
1488172567Sthompsa		{ IPW_CMD_SET_MANDATORY_BSSID,	"SET_MANDATORY_BSSID" },
1489172567Sthompsa		{ IPW_CMD_SET_MODE,		"SET_MODE" },
1490172567Sthompsa		{ IPW_CMD_SET_MSDU_TX_RATES,	"SET_MSDU_TX_RATES" },
1491172567Sthompsa		{ IPW_CMD_SET_POWER_MODE,	"SET_POWER_MODE" },
1492172567Sthompsa		{ IPW_CMD_SET_RTS_THRESHOLD,	"SET_RTS_THRESHOLD" },
1493172567Sthompsa		{ IPW_CMD_SET_SCAN_OPTIONS,	"SET_SCAN_OPTIONS" },
1494172567Sthompsa		{ IPW_CMD_SET_SECURITY_INFO,	"SET_SECURITY_INFO" },
1495172567Sthompsa		{ IPW_CMD_SET_TX_POWER_INDEX,	"SET_TX_POWER_INDEX" },
1496172567Sthompsa		{ IPW_CMD_SET_TX_RATES,		"SET_TX_RATES" },
1497172567Sthompsa		{ IPW_CMD_SET_WEP_FLAGS,	"SET_WEP_FLAGS" },
1498172567Sthompsa		{ IPW_CMD_SET_WEP_KEY,		"SET_WEP_KEY" },
1499172567Sthompsa		{ IPW_CMD_SET_WEP_KEY_INDEX,	"SET_WEP_KEY_INDEX" },
1500172567Sthompsa		{ IPW_CMD_SET_WPA_IE,		"SET_WPA_IE" },
1501172567Sthompsa
1502172567Sthompsa	};
1503172567Sthompsa	static char buf[12];
1504172567Sthompsa	int i;
1505172567Sthompsa
1506172567Sthompsa	for (i = 0; i < N(cmds); i++)
1507172567Sthompsa		if (cmds[i].cmd == cmd)
1508172567Sthompsa			return cmds[i].name;
1509172567Sthompsa	snprintf(buf, sizeof(buf), "%u", cmd);
1510172567Sthompsa	return buf;
1511172567Sthompsa#undef N
1512172567Sthompsa}
1513172567Sthompsa
1514145247Sdamien/*
1515145247Sdamien * Send a command to the firmware and wait for the acknowledgement.
1516145247Sdamien */
1517145247Sdamienstatic int
1518145247Sdamienipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1519145247Sdamien{
1520145247Sdamien	struct ipw_soft_bd *sbd;
1521145247Sdamien	bus_addr_t physaddr;
1522145247Sdamien	int error;
1523145247Sdamien
1524178354Ssam	IPW_LOCK_ASSERT(sc);
1525178354Ssam
1526172567Sthompsa	if (sc->flags & IPW_FLAG_BUSY) {
1527172567Sthompsa		device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1528172567Sthompsa			__func__, ipw_cmdname(type));
1529172567Sthompsa		return EAGAIN;
1530172567Sthompsa	}
1531172567Sthompsa	sc->flags |= IPW_FLAG_BUSY;
1532172567Sthompsa
1533145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1534145247Sdamien
1535145247Sdamien	error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1536145247Sdamien	    sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1537145247Sdamien	if (error != 0) {
1538145247Sdamien		device_printf(sc->sc_dev, "could not map command DMA memory\n");
1539172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1540145247Sdamien		return error;
1541145247Sdamien	}
1542145247Sdamien
1543145247Sdamien	sc->cmd.type = htole32(type);
1544145247Sdamien	sc->cmd.subtype = 0;
1545145247Sdamien	sc->cmd.len = htole32(len);
1546145247Sdamien	sc->cmd.seq = 0;
1547152637Sdamien	memcpy(sc->cmd.data, data, len);
1548145247Sdamien
1549145247Sdamien	sbd->type = IPW_SBD_TYPE_COMMAND;
1550145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1551145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1552145247Sdamien	sbd->bd->nfrag = 1;
1553145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1554145247Sdamien	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1555145247Sdamien
1556145247Sdamien	bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1557145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1558145247Sdamien
1559172567Sthompsa#ifdef IPW_DEBUG
1560172567Sthompsa	if (ipw_debug >= 4) {
1561172567Sthompsa		printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1562172567Sthompsa		    0, 0, len);
1563172567Sthompsa		/* Print the data buffer in the higher debug level */
1564172567Sthompsa		if (ipw_debug >= 9 && len > 0) {
1565172567Sthompsa			printf(" data: 0x");
1566172567Sthompsa			for (int i = 1; i <= len; i++)
1567174269Swkoszek				printf("%1D", (u_char *)data + len - i, "");
1568172567Sthompsa		}
1569172567Sthompsa		printf("\n");
1570172567Sthompsa	}
1571172567Sthompsa#endif
1572145247Sdamien
1573145247Sdamien	/* kick firmware */
1574145247Sdamien	sc->txfree--;
1575145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1576145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1577145247Sdamien
1578145247Sdamien	/* wait at most one second for command to complete */
1579172567Sthompsa	error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1580172567Sthompsa	if (error != 0) {
1581172567Sthompsa		device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1582172567Sthompsa		    __func__, ipw_cmdname(type), error);
1583172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1584172567Sthompsa		return (error);
1585172567Sthompsa	}
1586172567Sthompsa	return (0);
1587145247Sdamien}
1588145247Sdamien
1589145247Sdamienstatic int
1590145247Sdamienipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1591145247Sdamien{
1592145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1593178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1594192468Ssam	struct ieee80211vap *vap = ni->ni_vap;
1595145247Sdamien	struct ieee80211_frame *wh;
1596145247Sdamien	struct ipw_soft_bd *sbd;
1597145247Sdamien	struct ipw_soft_hdr *shdr;
1598145247Sdamien	struct ipw_soft_buf *sbuf;
1599145247Sdamien	struct ieee80211_key *k;
1600145247Sdamien	struct mbuf *mnew;
1601145247Sdamien	bus_dma_segment_t segs[IPW_MAX_NSEG];
1602145247Sdamien	bus_addr_t physaddr;
1603145247Sdamien	int nsegs, error, i;
1604145247Sdamien
1605145247Sdamien	wh = mtod(m0, struct ieee80211_frame *);
1606145247Sdamien
1607145247Sdamien	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1608178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1609147806Ssam		if (k == NULL) {
1610147806Ssam			m_freem(m0);
1611145247Sdamien			return ENOBUFS;
1612147806Ssam		}
1613145247Sdamien		/* packet header may have moved, reset our local pointer */
1614145247Sdamien		wh = mtod(m0, struct ieee80211_frame *);
1615145247Sdamien	}
1616145247Sdamien
1617192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1618145247Sdamien		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1619145247Sdamien
1620145247Sdamien		tap->wt_flags = 0;
1621145247Sdamien
1622192468Ssam		ieee80211_radiotap_tx(vap, m0);
1623145247Sdamien	}
1624145247Sdamien
1625145247Sdamien	shdr = SLIST_FIRST(&sc->free_shdr);
1626145247Sdamien	sbuf = SLIST_FIRST(&sc->free_sbuf);
1627145247Sdamien	KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1628145247Sdamien
1629145247Sdamien	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1630145247Sdamien	shdr->hdr.subtype = 0;
1631145247Sdamien	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1632145247Sdamien	shdr->hdr.encrypt = 0;
1633145247Sdamien	shdr->hdr.keyidx = 0;
1634145247Sdamien	shdr->hdr.keysz = 0;
1635145247Sdamien	shdr->hdr.fragmentsz = 0;
1636145247Sdamien	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1637145247Sdamien	if (ic->ic_opmode == IEEE80211_M_STA)
1638145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1639145247Sdamien	else
1640145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1641145247Sdamien
1642145247Sdamien	/* trim IEEE802.11 header */
1643145247Sdamien	m_adj(m0, sizeof (struct ieee80211_frame));
1644145247Sdamien
1645145247Sdamien	error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1646145247Sdamien	    &nsegs, 0);
1647145247Sdamien	if (error != 0 && error != EFBIG) {
1648145247Sdamien		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1649145247Sdamien		    error);
1650145247Sdamien		m_freem(m0);
1651145247Sdamien		return error;
1652145247Sdamien	}
1653145247Sdamien	if (error != 0) {
1654243857Sglebius		mnew = m_defrag(m0, M_NOWAIT);
1655145247Sdamien		if (mnew == NULL) {
1656145247Sdamien			device_printf(sc->sc_dev,
1657145247Sdamien			    "could not defragment mbuf\n");
1658145247Sdamien			m_freem(m0);
1659145247Sdamien			return ENOBUFS;
1660145247Sdamien		}
1661145247Sdamien		m0 = mnew;
1662145247Sdamien
1663145247Sdamien		error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1664145247Sdamien		    segs, &nsegs, 0);
1665145247Sdamien		if (error != 0) {
1666145247Sdamien			device_printf(sc->sc_dev,
1667145247Sdamien			    "could not map mbuf (error %d)\n", error);
1668145247Sdamien			m_freem(m0);
1669145247Sdamien			return error;
1670145247Sdamien		}
1671145247Sdamien	}
1672145247Sdamien
1673145247Sdamien	error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1674145247Sdamien	    sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1675145247Sdamien	if (error != 0) {
1676145247Sdamien		device_printf(sc->sc_dev, "could not map header DMA memory\n");
1677145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1678145247Sdamien		m_freem(m0);
1679145247Sdamien		return error;
1680145247Sdamien	}
1681145247Sdamien
1682145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1683145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1684145247Sdamien
1685145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1686145247Sdamien	sbd->type = IPW_SBD_TYPE_HEADER;
1687145247Sdamien	sbd->priv = shdr;
1688145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1689145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1690145247Sdamien	sbd->bd->nfrag = 1 + nsegs;
1691145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1692145247Sdamien	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1693145247Sdamien
1694145247Sdamien	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1695145247Sdamien	    shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1696145247Sdamien	    shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1697145247Sdamien	    ":"));
1698145247Sdamien
1699145247Sdamien	sc->txfree--;
1700145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1701145247Sdamien
1702145247Sdamien	sbuf->m = m0;
1703145247Sdamien	sbuf->ni = ni;
1704145247Sdamien
1705145247Sdamien	for (i = 0; i < nsegs; i++) {
1706145247Sdamien		sbd = &sc->stbd_list[sc->txcur];
1707145247Sdamien
1708145247Sdamien		sbd->bd->physaddr = htole32(segs[i].ds_addr);
1709145247Sdamien		sbd->bd->len = htole32(segs[i].ds_len);
1710145247Sdamien		sbd->bd->nfrag = 0;
1711145247Sdamien		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1712145247Sdamien		if (i == nsegs - 1) {
1713145247Sdamien			sbd->type = IPW_SBD_TYPE_DATA;
1714145247Sdamien			sbd->priv = sbuf;
1715145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1716145247Sdamien		} else {
1717145247Sdamien			sbd->type = IPW_SBD_TYPE_NOASSOC;
1718145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1719145247Sdamien		}
1720145247Sdamien
1721172574Sthompsa		DPRINTFN(5, ("sending fragment (%d)\n", i));
1722145247Sdamien
1723145247Sdamien		sc->txfree--;
1724145247Sdamien		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1725145247Sdamien	}
1726145247Sdamien
1727145247Sdamien	bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1728145247Sdamien	bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1729145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1730145247Sdamien
1731145247Sdamien	/* kick firmware */
1732145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1733145247Sdamien
1734145247Sdamien	return 0;
1735145247Sdamien}
1736145247Sdamien
1737178354Ssamstatic int
1738178354Ssamipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1739178354Ssam	const struct ieee80211_bpf_params *params)
1740178354Ssam{
1741178354Ssam	/* no support; just discard */
1742178354Ssam	m_freem(m);
1743178354Ssam	ieee80211_free_node(ni);
1744178354Ssam	return 0;
1745178354Ssam}
1746178354Ssam
1747145247Sdamienstatic void
1748145247Sdamienipw_start(struct ifnet *ifp)
1749145247Sdamien{
1750145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1751172567Sthompsa
1752172567Sthompsa	IPW_LOCK(sc);
1753172567Sthompsa	ipw_start_locked(ifp);
1754172567Sthompsa	IPW_UNLOCK(sc);
1755172567Sthompsa}
1756172567Sthompsa
1757172567Sthompsastatic void
1758172567Sthompsaipw_start_locked(struct ifnet *ifp)
1759172567Sthompsa{
1760172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
1761145247Sdamien	struct ieee80211_node *ni;
1762178354Ssam	struct mbuf *m;
1763145247Sdamien
1764172567Sthompsa	IPW_LOCK_ASSERT(sc);
1765145247Sdamien
1766145247Sdamien	for (;;) {
1767178354Ssam		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1768178354Ssam		if (m == NULL)
1769145247Sdamien			break;
1770145247Sdamien		if (sc->txfree < 1 + IPW_MAX_NSEG) {
1771178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1772148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1773145247Sdamien			break;
1774145247Sdamien		}
1775178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1776178354Ssam		if (ipw_tx_start(ifp, m, ni) != 0) {
1777145247Sdamien			ieee80211_free_node(ni);
1778145247Sdamien			ifp->if_oerrors++;
1779145247Sdamien			break;
1780145247Sdamien		}
1781145247Sdamien		/* start watchdog timer */
1782145247Sdamien		sc->sc_tx_timer = 5;
1783145247Sdamien	}
1784145247Sdamien}
1785145247Sdamien
1786145247Sdamienstatic void
1787172567Sthompsaipw_watchdog(void *arg)
1788145247Sdamien{
1789172567Sthompsa	struct ipw_softc *sc = arg;
1790172567Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1791178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1792145247Sdamien
1793172567Sthompsa	IPW_LOCK_ASSERT(sc);
1794156599Sdamien
1795145247Sdamien	if (sc->sc_tx_timer > 0) {
1796145247Sdamien		if (--sc->sc_tx_timer == 0) {
1797145247Sdamien			if_printf(ifp, "device timeout\n");
1798145247Sdamien			ifp->if_oerrors++;
1799178354Ssam			taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1800145247Sdamien		}
1801145247Sdamien	}
1802172567Sthompsa	if (sc->sc_scan_timer > 0) {
1803172567Sthompsa		if (--sc->sc_scan_timer == 0) {
1804172567Sthompsa			DPRINTFN(3, ("Scan timeout\n"));
1805172567Sthompsa			/* End the scan */
1806172567Sthompsa			if (sc->flags & IPW_FLAG_SCANNING) {
1807206767Sbschmidt				IPW_UNLOCK(sc);
1808178354Ssam				ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1809206767Sbschmidt				IPW_LOCK(sc);
1810172567Sthompsa				sc->flags &= ~IPW_FLAG_SCANNING;
1811172567Sthompsa			}
1812172567Sthompsa		}
1813172567Sthompsa	}
1814172567Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1815172567Sthompsa		callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1816145247Sdamien}
1817145247Sdamien
1818145247Sdamienstatic int
1819145247Sdamienipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1820145247Sdamien{
1821145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1822178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1823178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
1824178354Ssam	int error = 0, startall = 0;
1825145247Sdamien
1826145247Sdamien	switch (cmd) {
1827145247Sdamien	case SIOCSIFFLAGS:
1828178704Sthompsa		IPW_LOCK(sc);
1829145247Sdamien		if (ifp->if_flags & IFF_UP) {
1830178354Ssam			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1831178354Ssam				ipw_init_locked(sc);
1832178354Ssam				startall = 1;
1833178354Ssam			}
1834145247Sdamien		} else {
1835148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1836172567Sthompsa				ipw_stop_locked(sc);
1837145247Sdamien		}
1838178704Sthompsa		IPW_UNLOCK(sc);
1839178704Sthompsa		if (startall)
1840178704Sthompsa			ieee80211_start_all(ic);
1841145247Sdamien		break;
1842178354Ssam	case SIOCGIFMEDIA:
1843178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1844178354Ssam		break;
1845178704Sthompsa	case SIOCGIFADDR:
1846178704Sthompsa		error = ether_ioctl(ifp, cmd, data);
1847178704Sthompsa		break;
1848145247Sdamien	default:
1849178704Sthompsa		error = EINVAL;
1850178704Sthompsa		break;
1851145247Sdamien	}
1852145247Sdamien	return error;
1853145247Sdamien}
1854145247Sdamien
1855145247Sdamienstatic void
1856145247Sdamienipw_stop_master(struct ipw_softc *sc)
1857145247Sdamien{
1858156599Sdamien	uint32_t tmp;
1859145247Sdamien	int ntries;
1860145247Sdamien
1861145247Sdamien	/* disable interrupts */
1862145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1863145247Sdamien
1864145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1865145247Sdamien	for (ntries = 0; ntries < 50; ntries++) {
1866145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1867145247Sdamien			break;
1868145247Sdamien		DELAY(10);
1869145247Sdamien	}
1870145247Sdamien	if (ntries == 50)
1871145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for master\n");
1872145247Sdamien
1873156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1874156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1875145247Sdamien
1876172567Sthompsa	/* Clear all flags except the following */
1877172567Sthompsa	sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1878145247Sdamien}
1879145247Sdamien
1880145247Sdamienstatic int
1881145247Sdamienipw_reset(struct ipw_softc *sc)
1882145247Sdamien{
1883156599Sdamien	uint32_t tmp;
1884145247Sdamien	int ntries;
1885145247Sdamien
1886145247Sdamien	ipw_stop_master(sc);
1887145247Sdamien
1888145247Sdamien	/* move adapter to D0 state */
1889156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1890156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1891145247Sdamien
1892145247Sdamien	/* wait for clock stabilization */
1893145247Sdamien	for (ntries = 0; ntries < 1000; ntries++) {
1894145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1895145247Sdamien			break;
1896145247Sdamien		DELAY(200);
1897145247Sdamien	}
1898145247Sdamien	if (ntries == 1000)
1899145247Sdamien		return EIO;
1900145247Sdamien
1901156599Sdamien	tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1902156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1903145247Sdamien
1904145247Sdamien	DELAY(10);
1905145247Sdamien
1906156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1907156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1908145247Sdamien
1909145247Sdamien	return 0;
1910145247Sdamien}
1911145247Sdamien
1912172567Sthompsastatic int
1913172567Sthompsaipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1914172567Sthompsa{
1915172567Sthompsa	int ms = hz < 1000 ? 1 : hz/10;
1916172567Sthompsa	int i, error;
1917172567Sthompsa
1918172567Sthompsa	for (i = 0; i < 100; i++) {
1919172567Sthompsa		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1920172567Sthompsa			return 0;
1921172567Sthompsa		error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1922172567Sthompsa		if (error == 0 || error != EWOULDBLOCK)
1923172567Sthompsa			return 0;
1924172567Sthompsa	}
1925172567Sthompsa	DPRINTF(("%s: timeout waiting for %s\n",
1926172567Sthompsa		__func__, waitfor ? "disable" : "enable"));
1927172567Sthompsa	return ETIMEDOUT;
1928172567Sthompsa}
1929172567Sthompsa
1930172567Sthompsastatic int
1931172567Sthompsaipw_enable(struct ipw_softc *sc)
1932172567Sthompsa{
1933172567Sthompsa	int error;
1934172567Sthompsa
1935172567Sthompsa	if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1936172567Sthompsa		DPRINTF(("Enable adapter\n"));
1937172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1938172567Sthompsa		if (error != 0)
1939172567Sthompsa			return error;
1940172567Sthompsa		error = ipw_waitfordisable(sc, 0);
1941172567Sthompsa		if (error != 0)
1942172567Sthompsa			return error;
1943172567Sthompsa		sc->flags |= IPW_FLAG_ENABLED;
1944172567Sthompsa	}
1945172567Sthompsa	return 0;
1946172567Sthompsa}
1947172567Sthompsa
1948172567Sthompsastatic int
1949172567Sthompsaipw_disable(struct ipw_softc *sc)
1950172567Sthompsa{
1951172567Sthompsa	int error;
1952172567Sthompsa
1953172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
1954172567Sthompsa		DPRINTF(("Disable adapter\n"));
1955172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1956172567Sthompsa		if (error != 0)
1957172567Sthompsa			return error;
1958172567Sthompsa		error = ipw_waitfordisable(sc, 1);
1959172567Sthompsa		if (error != 0)
1960172567Sthompsa			return error;
1961172567Sthompsa		sc->flags &= ~IPW_FLAG_ENABLED;
1962172567Sthompsa	}
1963172567Sthompsa	return 0;
1964172567Sthompsa}
1965172567Sthompsa
1966145247Sdamien/*
1967145247Sdamien * Upload the microcode to the device.
1968145247Sdamien */
1969145247Sdamienstatic int
1970156599Sdamienipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1971145247Sdamien{
1972145247Sdamien	int ntries;
1973145247Sdamien
1974145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1975145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1976145247Sdamien
1977145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1978145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1979145247Sdamien
1980145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1981145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1982145247Sdamien
1983145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1984145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1985145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1986145247Sdamien
1987145247Sdamien	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1988145247Sdamien
1989145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1990145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1991145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1992145247Sdamien
1993145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1994145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1995145247Sdamien
1996145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1997145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1998145247Sdamien
1999145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
2000145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
2001145247Sdamien
2002145247Sdamien	for (ntries = 0; ntries < 10; ntries++) {
2003145247Sdamien		if (MEM_READ_1(sc, 0x210000) & 1)
2004145247Sdamien			break;
2005145247Sdamien		DELAY(10);
2006145247Sdamien	}
2007145247Sdamien	if (ntries == 10) {
2008145247Sdamien		device_printf(sc->sc_dev,
2009145247Sdamien		    "timeout waiting for ucode to initialize\n");
2010145247Sdamien		return EIO;
2011145247Sdamien	}
2012145247Sdamien
2013145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0);
2014145247Sdamien
2015145247Sdamien	return 0;
2016145247Sdamien}
2017145247Sdamien
2018145247Sdamien/* set of macros to handle unaligned little endian data in firmware image */
2019145247Sdamien#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2020145247Sdamien#define GETLE16(p) ((p)[0] | (p)[1] << 8)
2021145247Sdamienstatic int
2022156599Sdamienipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
2023145247Sdamien{
2024156599Sdamien	const uint8_t *p, *end;
2025156599Sdamien	uint32_t tmp, dst;
2026145247Sdamien	uint16_t len;
2027145247Sdamien	int error;
2028145247Sdamien
2029145247Sdamien	p = fw;
2030145247Sdamien	end = fw + size;
2031145247Sdamien	while (p < end) {
2032145247Sdamien		dst = GETLE32(p); p += 4;
2033145247Sdamien		len = GETLE16(p); p += 2;
2034145247Sdamien
2035145247Sdamien		ipw_write_mem_1(sc, dst, p, len);
2036145247Sdamien		p += len;
2037145247Sdamien	}
2038145247Sdamien
2039145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
2040145247Sdamien	    IPW_IO_LED_OFF);
2041145247Sdamien
2042145247Sdamien	/* enable interrupts */
2043145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
2044145247Sdamien
2045145247Sdamien	/* kick the firmware */
2046145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
2047145247Sdamien
2048156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
2049156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
2050145247Sdamien
2051145247Sdamien	/* wait at most one second for firmware initialization to complete */
2052145247Sdamien	if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
2053145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for firmware "
2054145247Sdamien		    "initialization to complete\n");
2055145247Sdamien		return error;
2056145247Sdamien	}
2057145247Sdamien
2058156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_IO);
2059156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2060156599Sdamien	    IPW_IO_GPIO3_MASK);
2061145247Sdamien
2062145247Sdamien	return 0;
2063145247Sdamien}
2064145247Sdamien
2065145247Sdamienstatic int
2066172567Sthompsaipw_setwepkeys(struct ipw_softc *sc)
2067172567Sthompsa{
2068178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2069178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2070178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2071172567Sthompsa	struct ipw_wep_key wepkey;
2072172567Sthompsa	struct ieee80211_key *wk;
2073172567Sthompsa	int error, i;
2074172567Sthompsa
2075172567Sthompsa	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2076178354Ssam		wk = &vap->iv_nw_keys[i];
2077172567Sthompsa
2078172567Sthompsa		if (wk->wk_cipher == NULL ||
2079172567Sthompsa		    wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2080172567Sthompsa			continue;
2081172567Sthompsa
2082172567Sthompsa		wepkey.idx = i;
2083172567Sthompsa		wepkey.len = wk->wk_keylen;
2084172567Sthompsa		memset(wepkey.key, 0, sizeof wepkey.key);
2085172567Sthompsa		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2086172567Sthompsa		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2087172567Sthompsa		    wepkey.len));
2088172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2089172567Sthompsa		    sizeof wepkey);
2090172567Sthompsa		if (error != 0)
2091172567Sthompsa			return error;
2092172567Sthompsa	}
2093172567Sthompsa	return 0;
2094172567Sthompsa}
2095172567Sthompsa
2096172567Sthompsastatic int
2097172567Sthompsaipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2098172567Sthompsa{
2099172567Sthompsa	struct ipw_wpa_ie wpaie;
2100172567Sthompsa
2101172567Sthompsa	memset(&wpaie, 0, sizeof(wpaie));
2102172567Sthompsa	wpaie.len = htole32(ielen);
2103172567Sthompsa	/* XXX verify length */
2104172567Sthompsa	memcpy(&wpaie.ie, ie, ielen);
2105172567Sthompsa	DPRINTF(("Setting WPA IE\n"));
2106172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2107172567Sthompsa}
2108172567Sthompsa
2109172567Sthompsastatic int
2110172567Sthompsaipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2111172567Sthompsa{
2112172567Sthompsa	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2113172567Sthompsa
2114172567Sthompsa	if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2115172567Sthompsa		DPRINTF(("Setting mandatory BSSID to null\n"));
2116172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2117172567Sthompsa	} else {
2118172567Sthompsa		DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2119172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2120172567Sthompsa			bssid, IEEE80211_ADDR_LEN);
2121172567Sthompsa	}
2122172567Sthompsa}
2123172567Sthompsa
2124172567Sthompsastatic int
2125172567Sthompsaipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2126172567Sthompsa{
2127172567Sthompsa	if (ssidlen == 0) {
2128172567Sthompsa		/*
2129172567Sthompsa		 * A bug in the firmware breaks the ``don't associate''
2130172567Sthompsa		 * bit in the scan options command.  To compensate for
2131172567Sthompsa		 * this install a bogus ssid when no ssid is specified
2132172567Sthompsa		 * so the firmware won't try to associate.
2133172567Sthompsa		 */
2134172567Sthompsa		DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2135172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2136172567Sthompsa			"\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2137172567Sthompsa			"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2138172567Sthompsa			"\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2139172567Sthompsa			"\x3c\x3d", IEEE80211_NWID_LEN);
2140172567Sthompsa	} else {
2141172567Sthompsa#ifdef IPW_DEBUG
2142172567Sthompsa		if (ipw_debug > 0) {
2143172567Sthompsa			printf("Setting ESSID to ");
2144172567Sthompsa			ieee80211_print_essid(ssid, ssidlen);
2145172567Sthompsa			printf("\n");
2146172567Sthompsa		}
2147172567Sthompsa#endif
2148172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2149172567Sthompsa	}
2150172567Sthompsa}
2151172567Sthompsa
2152172567Sthompsastatic int
2153172567Sthompsaipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2154172567Sthompsa{
2155172567Sthompsa	struct ipw_scan_options opts;
2156172567Sthompsa
2157172567Sthompsa	DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2158172567Sthompsa	opts.channels = htole32(chanmask);
2159172567Sthompsa	opts.flags = htole32(flags);
2160172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2161172567Sthompsa}
2162172567Sthompsa
2163172567Sthompsastatic int
2164172567Sthompsaipw_scan(struct ipw_softc *sc)
2165172567Sthompsa{
2166172567Sthompsa	uint32_t params;
2167172567Sthompsa	int error;
2168172567Sthompsa
2169172567Sthompsa	DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2170172567Sthompsa
2171172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
2172172567Sthompsa		return (EBUSY);
2173172567Sthompsa	sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2174172567Sthompsa
2175172567Sthompsa	/* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2176172567Sthompsa	error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2177172567Sthompsa	if (error != 0)
2178172567Sthompsa		goto done;
2179172567Sthompsa
2180172567Sthompsa	/*
2181172567Sthompsa	 * Setup null/bogus ssid so firmware doesn't use any previous
2182172567Sthompsa	 * ssid to try and associate.  This is because the ``don't
2183172567Sthompsa	 * associate'' option bit is broken (sigh).
2184172567Sthompsa	 */
2185172567Sthompsa	error = ipw_setssid(sc, NULL, 0);
2186172567Sthompsa	if (error != 0)
2187172567Sthompsa		goto done;
2188172567Sthompsa
2189172567Sthompsa	/*
2190172567Sthompsa	 * NB: the adapter may be disabled on association lost;
2191172567Sthompsa	 *     if so just re-enable it to kick off scanning.
2192172567Sthompsa	 */
2193172567Sthompsa	DPRINTF(("Starting scan\n"));
2194172567Sthompsa	sc->sc_scan_timer = 3;
2195172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
2196172567Sthompsa		params = 0;				/* XXX? */
2197172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2198172567Sthompsa				&params, sizeof(params));
2199172567Sthompsa	} else
2200172567Sthompsa		error = ipw_enable(sc);
2201172567Sthompsadone:
2202172567Sthompsa	if (error != 0) {
2203172567Sthompsa		DPRINTF(("Scan failed\n"));
2204172567Sthompsa		sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2205172567Sthompsa	}
2206172567Sthompsa	return (error);
2207172567Sthompsa}
2208172567Sthompsa
2209172567Sthompsastatic int
2210172567Sthompsaipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2211172567Sthompsa{
2212178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2213178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2214172567Sthompsa	uint32_t data;
2215172567Sthompsa	int error;
2216172567Sthompsa
2217172567Sthompsa	data = htole32(ieee80211_chan2ieee(ic, chan));
2218172567Sthompsa	DPRINTF(("Setting channel to %u\n", le32toh(data)));
2219172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2220172567Sthompsa	if (error == 0)
2221172567Sthompsa		ipw_setcurchan(sc, chan);
2222172567Sthompsa	return error;
2223172567Sthompsa}
2224172567Sthompsa
2225178354Ssamstatic void
2226191746Sthompsaipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2227145247Sdamien{
2228178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2229178354Ssam	struct ipw_softc *sc = ifp->if_softc;
2230178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2231145247Sdamien	struct ipw_security security;
2232145247Sdamien	uint32_t data;
2233172567Sthompsa	int error;
2234145247Sdamien
2235178354Ssam	IPW_LOCK(sc);
2236172567Sthompsa	error = ipw_disable(sc);
2237172567Sthompsa	if (error != 0)
2238178354Ssam		goto done;
2239172567Sthompsa
2240152637Sdamien	memset(&security, 0, sizeof security);
2241178354Ssam	security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2242145247Sdamien	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2243145247Sdamien	security.ciphers = htole32(IPW_CIPHER_NONE);
2244145247Sdamien	DPRINTF(("Setting authmode to %u\n", security.authmode));
2245172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2246145247Sdamien	    sizeof security);
2247145247Sdamien	if (error != 0)
2248178354Ssam		goto done;
2249145247Sdamien
2250178354Ssam	data = htole32(vap->iv_rtsthreshold);
2251178354Ssam	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2252178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2253145247Sdamien	if (error != 0)
2254178354Ssam		goto done;
2255145247Sdamien
2256178354Ssam	data = htole32(vap->iv_fragthreshold);
2257178354Ssam	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2258178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2259145247Sdamien	if (error != 0)
2260178354Ssam		goto done;
2261145247Sdamien
2262178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2263172567Sthompsa		error = ipw_setwepkeys(sc);
2264172567Sthompsa		if (error != 0)
2265178354Ssam			goto done;
2266172567Sthompsa
2267178354Ssam		if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2268178354Ssam			data = htole32(vap->iv_def_txkey);
2269172567Sthompsa			DPRINTF(("Setting wep tx key index to %u\n",
2270172567Sthompsa				le32toh(data)));
2271172567Sthompsa			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2272172567Sthompsa			    sizeof data);
2273172567Sthompsa			if (error != 0)
2274178354Ssam				goto done;
2275172567Sthompsa		}
2276172567Sthompsa	}
2277172567Sthompsa
2278178354Ssam	data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2279172567Sthompsa	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2280172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2281172567Sthompsa	if (error != 0)
2282178354Ssam		goto done;
2283172567Sthompsa
2284172567Sthompsa	error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2285172567Sthompsa	if (error != 0)
2286178354Ssam		goto done;
2287172567Sthompsa
2288172567Sthompsa	error = ipw_setbssid(sc, ni->ni_bssid);
2289172567Sthompsa	if (error != 0)
2290178354Ssam		goto done;
2291172567Sthompsa
2292206766Sbschmidt	if (vap->iv_appie_wpa != NULL) {
2293206766Sbschmidt		struct ieee80211_appie *ie = vap->iv_appie_wpa;
2294178354Ssam		error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2295172567Sthompsa		if (error != 0)
2296178354Ssam			goto done;
2297172567Sthompsa	}
2298172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2299172567Sthompsa		error = ipw_setchannel(sc, ni->ni_chan);
2300172567Sthompsa		if (error != 0)
2301178354Ssam			goto done;
2302172567Sthompsa	}
2303172567Sthompsa
2304172567Sthompsa	/* lock scan to ap's channel and enable associate */
2305172567Sthompsa	error = ipw_setscanopts(sc,
2306178354Ssam	    1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2307178354Ssam	if (error != 0)
2308178354Ssam		goto done;
2309172567Sthompsa
2310178354Ssam	error = ipw_enable(sc);		/* finally, enable adapter */
2311178354Ssam	if (error == 0)
2312178354Ssam		sc->flags |= IPW_FLAG_ASSOCIATING;
2313178354Ssamdone:
2314178354Ssam	IPW_UNLOCK(sc);
2315172567Sthompsa}
2316172567Sthompsa
2317172567Sthompsastatic void
2318191746Sthompsaipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2319172567Sthompsa{
2320178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2321178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2322178354Ssam	struct ipw_softc *sc = ifp->if_softc;
2323172567Sthompsa
2324172567Sthompsa	IPW_LOCK(sc);
2325172567Sthompsa	DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2326172567Sthompsa	/*
2327172567Sthompsa	 * NB: don't try to do this if ipw_stop_master has
2328172567Sthompsa	 *     shutdown the firmware and disabled interrupts.
2329172567Sthompsa	 */
2330178354Ssam	if (sc->flags & IPW_FLAG_FW_INITED) {
2331178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATED;
2332178354Ssam		/*
2333178354Ssam		 * NB: firmware currently ignores bssid parameter, but
2334178354Ssam		 *     supply it in case this changes (follow linux driver).
2335178354Ssam		 */
2336178354Ssam		(void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2337178354Ssam			ni->ni_bssid, IEEE80211_ADDR_LEN);
2338178354Ssam	}
2339178354Ssam	IPW_UNLOCK(sc);
2340172567Sthompsa}
2341172567Sthompsa
2342172567Sthompsa/*
2343156599Sdamien * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2344156599Sdamien * It is called on firmware panics or on watchdog timeouts.
2345156599Sdamien */
2346145247Sdamienstatic void
2347156599Sdamienipw_init_task(void *context, int pending)
2348156599Sdamien{
2349156599Sdamien	ipw_init(context);
2350156599Sdamien}
2351156599Sdamien
2352156599Sdamienstatic void
2353145247Sdamienipw_init(void *priv)
2354145247Sdamien{
2355145247Sdamien	struct ipw_softc *sc = priv;
2356178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2357178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2358172567Sthompsa
2359172567Sthompsa	IPW_LOCK(sc);
2360178354Ssam	ipw_init_locked(sc);
2361172567Sthompsa	IPW_UNLOCK(sc);
2362178354Ssam
2363178931Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2364178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2365172567Sthompsa}
2366172567Sthompsa
2367172567Sthompsastatic void
2368178354Ssamipw_init_locked(struct ipw_softc *sc)
2369172567Sthompsa{
2370178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2371178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2372178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2373166756Sluigi	const struct firmware *fp;
2374156599Sdamien	const struct ipw_firmware_hdr *hdr;
2375178354Ssam	const char *fw;
2376145247Sdamien
2377172567Sthompsa	IPW_LOCK_ASSERT(sc);
2378156599Sdamien
2379172567Sthompsa	DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2380178354Ssam		ieee80211_state_name[vap->iv_state], sc->flags));
2381172567Sthompsa
2382156599Sdamien	/*
2383156599Sdamien	 * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2384156599Sdamien	 * when loading the firmware and we don't want to be called during this
2385156599Sdamien	 * operation.
2386156599Sdamien	 */
2387172567Sthompsa	if (sc->flags & IPW_FLAG_INIT_LOCKED)
2388145247Sdamien		return;
2389156599Sdamien	sc->flags |= IPW_FLAG_INIT_LOCKED;
2390145247Sdamien
2391172567Sthompsa	ipw_stop_locked(sc);
2392145247Sdamien
2393145247Sdamien	if (ipw_reset(sc) != 0) {
2394145247Sdamien		device_printf(sc->sc_dev, "could not reset adapter\n");
2395178354Ssam		goto fail;
2396145247Sdamien	}
2397145247Sdamien
2398159487Siedowse	if (sc->sc_firmware == NULL) {
2399178354Ssam		device_printf(sc->sc_dev, "no firmware\n");
2400178354Ssam		goto fail;
2401156599Sdamien	}
2402178354Ssam	/* NB: consistency already checked on load */
2403159487Siedowse	fp = sc->sc_firmware;
2404156599Sdamien	hdr = (const struct ipw_firmware_hdr *)fp->data;
2405156599Sdamien
2406178354Ssam	DPRINTF(("Loading firmware image '%s'\n", fp->name));
2407156599Sdamien	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2408156599Sdamien	if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2409145247Sdamien		device_printf(sc->sc_dev, "could not load microcode\n");
2410178354Ssam		goto fail;
2411145247Sdamien	}
2412145247Sdamien
2413145247Sdamien	ipw_stop_master(sc);
2414145247Sdamien
2415145247Sdamien	/*
2416145247Sdamien	 * Setup tx, rx and status rings.
2417145247Sdamien	 */
2418145247Sdamien	sc->txold = IPW_NTBD - 1;
2419145247Sdamien	sc->txcur = 0;
2420145247Sdamien	sc->txfree = IPW_NTBD - 2;
2421145247Sdamien	sc->rxcur = IPW_NRBD - 1;
2422145247Sdamien
2423145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2424145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2425145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2426145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2427145247Sdamien
2428145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2429145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2430145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2431145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2432145247Sdamien
2433145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2434145247Sdamien
2435156599Sdamien	fw = (const char *)fp->data + sizeof *hdr;
2436156599Sdamien	if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2437145247Sdamien		device_printf(sc->sc_dev, "could not load firmware\n");
2438178354Ssam		goto fail;
2439145247Sdamien	}
2440145247Sdamien
2441145247Sdamien	sc->flags |= IPW_FLAG_FW_INITED;
2442145247Sdamien
2443145247Sdamien	/* retrieve information tables base addresses */
2444145247Sdamien	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2445145247Sdamien	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2446145247Sdamien
2447145247Sdamien	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2448145247Sdamien
2449145247Sdamien	if (ipw_config(sc) != 0) {
2450145247Sdamien		device_printf(sc->sc_dev, "device configuration failed\n");
2451178354Ssam		goto fail;
2452145247Sdamien	}
2453145247Sdamien
2454172567Sthompsa	callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2455148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2456148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2457145247Sdamien
2458156599Sdamien	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2459145247Sdamien	return;
2460145247Sdamien
2461178354Ssamfail:
2462172567Sthompsa	ipw_stop_locked(sc);
2463156599Sdamien	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2464145247Sdamien}
2465145247Sdamien
2466178354Ssamstatic int
2467178354Ssamipw_config(struct ipw_softc *sc)
2468178354Ssam{
2469178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2470178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2471178354Ssam	struct ipw_configuration config;
2472178354Ssam	uint32_t data;
2473178354Ssam	int error;
2474178354Ssam
2475178354Ssam	error = ipw_disable(sc);
2476178354Ssam	if (error != 0)
2477178354Ssam		return error;
2478178354Ssam
2479178354Ssam	switch (ic->ic_opmode) {
2480178354Ssam	case IEEE80211_M_STA:
2481178354Ssam	case IEEE80211_M_HOSTAP:
2482178354Ssam	case IEEE80211_M_WDS:		/* XXX */
2483178354Ssam		data = htole32(IPW_MODE_BSS);
2484178354Ssam		break;
2485178354Ssam	case IEEE80211_M_IBSS:
2486178354Ssam	case IEEE80211_M_AHDEMO:
2487178354Ssam		data = htole32(IPW_MODE_IBSS);
2488178354Ssam		break;
2489178354Ssam	case IEEE80211_M_MONITOR:
2490178354Ssam		data = htole32(IPW_MODE_MONITOR);
2491178354Ssam		break;
2492195562Srpaulo	default:
2493195562Srpaulo		device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2494195562Srpaulo		return EINVAL;
2495178354Ssam	}
2496178354Ssam	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2497178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2498178354Ssam	if (error != 0)
2499178354Ssam		return error;
2500178354Ssam
2501178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2502178354Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2503178354Ssam		error = ipw_setchannel(sc, ic->ic_curchan);
2504178354Ssam		if (error != 0)
2505178354Ssam			return error;
2506178354Ssam	}
2507178354Ssam
2508178354Ssam	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2509178354Ssam		return ipw_enable(sc);
2510178354Ssam
2511178354Ssam	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2512178354Ssam	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2513178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
2514178354Ssam		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2515178354Ssam	if (ifp->if_flags & IFF_PROMISC)
2516178354Ssam		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2517178354Ssam	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2518178354Ssam	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2519178354Ssam	DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2520178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2521178354Ssam	if (error != 0)
2522178354Ssam		return error;
2523178354Ssam
2524207926Sbschmidt	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2525178354Ssam	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2526178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2527178354Ssam	if (error != 0)
2528178354Ssam		return error;
2529178354Ssam
2530207926Sbschmidt	/* Use the same rate set */
2531178354Ssam	DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2532178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2533178354Ssam	if (error != 0)
2534178354Ssam		return error;
2535178354Ssam
2536207926Sbschmidt	/* Use the same rate set */
2537178354Ssam	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2538178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2539178354Ssam	if (error != 0)
2540178354Ssam		return error;
2541178354Ssam
2542178354Ssam	data = htole32(IPW_POWER_MODE_CAM);
2543178354Ssam	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2544178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2545178354Ssam	if (error != 0)
2546178354Ssam		return error;
2547178354Ssam
2548178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2549178354Ssam		data = htole32(32); /* default value */
2550178354Ssam		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2551178354Ssam		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2552178354Ssam		    sizeof data);
2553178354Ssam		if (error != 0)
2554178354Ssam			return error;
2555178354Ssam	}
2556178354Ssam
2557178354Ssam	return 0;
2558178354Ssam}
2559178354Ssam
2560145247Sdamienstatic void
2561145247Sdamienipw_stop(void *priv)
2562145247Sdamien{
2563145247Sdamien	struct ipw_softc *sc = priv;
2564172567Sthompsa
2565172567Sthompsa	IPW_LOCK(sc);
2566172567Sthompsa	ipw_stop_locked(sc);
2567172567Sthompsa	IPW_UNLOCK(sc);
2568172567Sthompsa}
2569172567Sthompsa
2570172567Sthompsastatic void
2571172567Sthompsaipw_stop_locked(struct ipw_softc *sc)
2572172567Sthompsa{
2573178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2574145247Sdamien	int i;
2575145247Sdamien
2576172567Sthompsa	IPW_LOCK_ASSERT(sc);
2577156599Sdamien
2578172567Sthompsa	callout_stop(&sc->sc_wdtimer);
2579145247Sdamien	ipw_stop_master(sc);
2580145247Sdamien
2581145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2582145247Sdamien
2583145247Sdamien	/*
2584145247Sdamien	 * Release tx buffers.
2585145247Sdamien	 */
2586145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
2587145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
2588145247Sdamien
2589145247Sdamien	sc->sc_tx_timer = 0;
2590148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2591145247Sdamien}
2592145247Sdamien
2593145247Sdamienstatic int
2594145247Sdamienipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2595145247Sdamien{
2596145247Sdamien	struct ipw_softc *sc = arg1;
2597145247Sdamien	uint32_t i, size, buf[256];
2598145247Sdamien
2599174317Sphilip	memset(buf, 0, sizeof buf);
2600174317Sphilip
2601174317Sphilip	if (!(sc->flags & IPW_FLAG_FW_INITED))
2602145247Sdamien		return SYSCTL_OUT(req, buf, sizeof buf);
2603145247Sdamien
2604145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2605145247Sdamien
2606145247Sdamien	size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2607145247Sdamien	for (i = 1; i < size; i++)
2608145247Sdamien		buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2609145247Sdamien
2610174317Sphilip	return SYSCTL_OUT(req, buf, size);
2611145247Sdamien}
2612145247Sdamien
2613145247Sdamienstatic int
2614145247Sdamienipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2615145247Sdamien{
2616145247Sdamien	struct ipw_softc *sc = arg1;
2617145247Sdamien	int val;
2618145247Sdamien
2619145247Sdamien	val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2620145247Sdamien	        (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2621145247Sdamien
2622145247Sdamien	return SYSCTL_OUT(req, &val, sizeof val);
2623145247Sdamien}
2624145247Sdamien
2625145247Sdamienstatic uint32_t
2626145247Sdamienipw_read_table1(struct ipw_softc *sc, uint32_t off)
2627145247Sdamien{
2628145247Sdamien	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2629145247Sdamien}
2630145247Sdamien
2631145247Sdamienstatic void
2632145247Sdamienipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2633145247Sdamien{
2634145247Sdamien	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2635145247Sdamien}
2636145247Sdamien
2637172567Sthompsa#if 0
2638145247Sdamienstatic int
2639145247Sdamienipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2640145247Sdamien{
2641145247Sdamien	uint32_t addr, info;
2642145247Sdamien	uint16_t count, size;
2643145247Sdamien	uint32_t total;
2644145247Sdamien
2645145247Sdamien	/* addr[4] + count[2] + size[2] */
2646145247Sdamien	addr = MEM_READ_4(sc, sc->table2_base + off);
2647145247Sdamien	info = MEM_READ_4(sc, sc->table2_base + off + 4);
2648145247Sdamien
2649145247Sdamien	count = info >> 16;
2650145247Sdamien	size = info & 0xffff;
2651145247Sdamien	total = count * size;
2652145247Sdamien
2653145247Sdamien	if (total > *len) {
2654145247Sdamien		*len = total;
2655145247Sdamien		return EINVAL;
2656145247Sdamien	}
2657145247Sdamien
2658145247Sdamien	*len = total;
2659145247Sdamien	ipw_read_mem_1(sc, addr, buf, total);
2660145247Sdamien
2661145247Sdamien	return 0;
2662145247Sdamien}
2663145247Sdamien
2664145247Sdamienstatic void
2665145247Sdamienipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2666145247Sdamien    bus_size_t count)
2667145247Sdamien{
2668145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2669145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2670145247Sdamien		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2671145247Sdamien	}
2672145247Sdamien}
2673172567Sthompsa#endif
2674145247Sdamien
2675145247Sdamienstatic void
2676156599Sdamienipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2677145247Sdamien    bus_size_t count)
2678145247Sdamien{
2679145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2680145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2681145247Sdamien		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2682145247Sdamien	}
2683145247Sdamien}
2684172567Sthompsa
2685172567Sthompsastatic void
2686172567Sthompsaipw_scan_start(struct ieee80211com *ic)
2687172567Sthompsa{
2688172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2689172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2690172567Sthompsa
2691172567Sthompsa	IPW_LOCK(sc);
2692191746Sthompsa	ipw_scan(sc);
2693172567Sthompsa	IPW_UNLOCK(sc);
2694172567Sthompsa}
2695172567Sthompsa
2696172567Sthompsastatic void
2697172567Sthompsaipw_set_channel(struct ieee80211com *ic)
2698172567Sthompsa{
2699172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2700172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2701172567Sthompsa
2702172567Sthompsa	IPW_LOCK(sc);
2703172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2704172567Sthompsa		ipw_disable(sc);
2705172567Sthompsa		ipw_setchannel(sc, ic->ic_curchan);
2706172567Sthompsa		ipw_enable(sc);
2707172567Sthompsa	}
2708172567Sthompsa	IPW_UNLOCK(sc);
2709172567Sthompsa}
2710172567Sthompsa
2711172567Sthompsastatic void
2712178354Ssamipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2713172567Sthompsa{
2714172567Sthompsa	/* NB: all channels are scanned at once */
2715172567Sthompsa}
2716172567Sthompsa
2717172567Sthompsastatic void
2718178354Ssamipw_scan_mindwell(struct ieee80211_scan_state *ss)
2719172567Sthompsa{
2720172567Sthompsa	/* NB: don't try to abort scan; wait for firmware to finish */
2721172567Sthompsa}
2722172567Sthompsa
2723172567Sthompsastatic void
2724172567Sthompsaipw_scan_end(struct ieee80211com *ic)
2725172567Sthompsa{
2726172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2727172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2728172567Sthompsa
2729172567Sthompsa	IPW_LOCK(sc);
2730172567Sthompsa	sc->flags &= ~IPW_FLAG_SCANNING;
2731172567Sthompsa	IPW_UNLOCK(sc);
2732172567Sthompsa}
2733