if_ipw.c revision 286865
1145247Sdamien/*-
2156599Sdamien * Copyright (c) 2004-2006
3145247Sdamien *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4172567Sthompsa * Copyright (c) 2006 Sam Leffler, Errno Consulting
5172567Sthompsa * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6145247Sdamien *
7145247Sdamien * Redistribution and use in source and binary forms, with or without
8145247Sdamien * modification, are permitted provided that the following conditions
9145247Sdamien * are met:
10145247Sdamien * 1. Redistributions of source code must retain the above copyright
11145247Sdamien *    notice unmodified, this list of conditions, and the following
12145247Sdamien *    disclaimer.
13145247Sdamien * 2. Redistributions in binary form must reproduce the above copyright
14145247Sdamien *    notice, this list of conditions and the following disclaimer in the
15145247Sdamien *    documentation and/or other materials provided with the distribution.
16145247Sdamien *
17145247Sdamien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18145247Sdamien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19145247Sdamien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20145247Sdamien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21145247Sdamien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22145247Sdamien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23145247Sdamien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24145247Sdamien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25145247Sdamien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26145247Sdamien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27145247Sdamien * SUCH DAMAGE.
28145247Sdamien */
29145247Sdamien
30145247Sdamien#include <sys/cdefs.h>
31145247Sdamien__FBSDID("$FreeBSD: head/sys/dev/ipw/if_ipw.c 286865 2015-08-17 23:44:38Z adrian $");
32145247Sdamien
33145247Sdamien/*-
34145247Sdamien * Intel(R) PRO/Wireless 2100 MiniPCI driver
35145247Sdamien * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
36145247Sdamien */
37145247Sdamien
38145247Sdamien#include <sys/param.h>
39145247Sdamien#include <sys/sysctl.h>
40145247Sdamien#include <sys/sockio.h>
41145247Sdamien#include <sys/mbuf.h>
42145247Sdamien#include <sys/kernel.h>
43145247Sdamien#include <sys/socket.h>
44145247Sdamien#include <sys/systm.h>
45145247Sdamien#include <sys/malloc.h>
46156599Sdamien#include <sys/queue.h>
47156599Sdamien#include <sys/taskqueue.h>
48145247Sdamien#include <sys/module.h>
49145247Sdamien#include <sys/bus.h>
50145247Sdamien#include <sys/endian.h>
51156599Sdamien#include <sys/linker.h>
52156599Sdamien#include <sys/firmware.h>
53145247Sdamien
54145247Sdamien#include <machine/bus.h>
55145247Sdamien#include <machine/resource.h>
56145247Sdamien#include <sys/rman.h>
57145247Sdamien
58145247Sdamien#include <dev/pci/pcireg.h>
59145247Sdamien#include <dev/pci/pcivar.h>
60145247Sdamien
61145247Sdamien#include <net/bpf.h>
62145247Sdamien#include <net/if.h>
63257176Sglebius#include <net/if_var.h>
64145247Sdamien#include <net/if_arp.h>
65145247Sdamien#include <net/ethernet.h>
66145247Sdamien#include <net/if_dl.h>
67145247Sdamien#include <net/if_media.h>
68145247Sdamien#include <net/if_types.h>
69145247Sdamien
70156599Sdamien#include <net80211/ieee80211_var.h>
71156599Sdamien#include <net80211/ieee80211_radiotap.h>
72156599Sdamien
73145247Sdamien#include <netinet/in.h>
74145247Sdamien#include <netinet/in_systm.h>
75145247Sdamien#include <netinet/in_var.h>
76145247Sdamien#include <netinet/ip.h>
77145247Sdamien#include <netinet/if_ether.h>
78145247Sdamien
79145247Sdamien#include <dev/ipw/if_ipwreg.h>
80145247Sdamien#include <dev/ipw/if_ipwvar.h>
81145247Sdamien
82172567Sthompsa#define IPW_DEBUG
83145247Sdamien#ifdef IPW_DEBUG
84145247Sdamien#define DPRINTF(x)	do { if (ipw_debug > 0) printf x; } while (0)
85145247Sdamien#define DPRINTFN(n, x)	do { if (ipw_debug >= (n)) printf x; } while (0)
86145247Sdamienint ipw_debug = 0;
87145247SdamienSYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
88145247Sdamien#else
89145247Sdamien#define DPRINTF(x)
90145247Sdamien#define DPRINTFN(n, x)
91145247Sdamien#endif
92145247Sdamien
93145247SdamienMODULE_DEPEND(ipw, pci,  1, 1, 1);
94145247SdamienMODULE_DEPEND(ipw, wlan, 1, 1, 1);
95156599SdamienMODULE_DEPEND(ipw, firmware, 1, 1, 1);
96145247Sdamien
97145247Sdamienstruct ipw_ident {
98145247Sdamien	uint16_t	vendor;
99145247Sdamien	uint16_t	device;
100145247Sdamien	const char	*name;
101145247Sdamien};
102145247Sdamien
103145247Sdamienstatic const struct ipw_ident ipw_ident_table[] = {
104145247Sdamien	{ 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
105145247Sdamien
106145247Sdamien	{ 0, 0, NULL }
107145247Sdamien};
108145247Sdamien
109178354Ssamstatic struct ieee80211vap *ipw_vap_create(struct ieee80211com *,
110228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
111228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
112228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
113178354Ssamstatic void	ipw_vap_delete(struct ieee80211vap *);
114145247Sdamienstatic int	ipw_dma_alloc(struct ipw_softc *);
115145247Sdamienstatic void	ipw_release(struct ipw_softc *);
116145247Sdamienstatic void	ipw_media_status(struct ifnet *, struct ifmediareq *);
117178354Ssamstatic int	ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
118145247Sdamienstatic uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
119172567Sthompsastatic void	ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
120172567Sthompsastatic void	ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
121172567Sthompsastatic void	ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
122145247Sdamien		    struct ipw_soft_bd *, struct ipw_soft_buf *);
123145247Sdamienstatic void	ipw_rx_intr(struct ipw_softc *);
124145247Sdamienstatic void	ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
125145247Sdamienstatic void	ipw_tx_intr(struct ipw_softc *);
126145247Sdamienstatic void	ipw_intr(void *);
127145247Sdamienstatic void	ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
128172567Sthompsastatic const char * ipw_cmdname(int);
129145247Sdamienstatic int	ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
130286437Sadrianstatic int	ipw_tx_start(struct ifnet *, struct mbuf *,
131145247Sdamien		    struct ieee80211_node *);
132178354Ssamstatic int	ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
133178354Ssam		    const struct ieee80211_bpf_params *);
134286437Sadrianstatic void	ipw_start(struct ifnet *);
135286437Sadrianstatic void	ipw_start_locked(struct ifnet *);
136172567Sthompsastatic void	ipw_watchdog(void *);
137286437Sadrianstatic int	ipw_ioctl(struct ifnet *, u_long, caddr_t);
138145247Sdamienstatic void	ipw_stop_master(struct ipw_softc *);
139172567Sthompsastatic int	ipw_enable(struct ipw_softc *);
140172567Sthompsastatic int	ipw_disable(struct ipw_softc *);
141145247Sdamienstatic int	ipw_reset(struct ipw_softc *);
142156599Sdamienstatic int	ipw_load_ucode(struct ipw_softc *, const char *, int);
143156599Sdamienstatic int	ipw_load_firmware(struct ipw_softc *, const char *, int);
144145247Sdamienstatic int	ipw_config(struct ipw_softc *);
145191746Sthompsastatic void	ipw_assoc(struct ieee80211com *, struct ieee80211vap *);
146191746Sthompsastatic void	ipw_disassoc(struct ieee80211com *, struct ieee80211vap *);
147156599Sdamienstatic void	ipw_init_task(void *, int);
148145247Sdamienstatic void	ipw_init(void *);
149178354Ssamstatic void	ipw_init_locked(struct ipw_softc *);
150145247Sdamienstatic void	ipw_stop(void *);
151172567Sthompsastatic void	ipw_stop_locked(struct ipw_softc *);
152145247Sdamienstatic int	ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
153145247Sdamienstatic int	ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
154145247Sdamienstatic uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
155145247Sdamienstatic void	ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
156172567Sthompsa#if 0
157145247Sdamienstatic int	ipw_read_table2(struct ipw_softc *, uint32_t, void *,
158145247Sdamien		    uint32_t *);
159145247Sdamienstatic void	ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
160145247Sdamien		    bus_size_t);
161172567Sthompsa#endif
162156599Sdamienstatic void	ipw_write_mem_1(struct ipw_softc *, bus_size_t,
163156599Sdamien		    const uint8_t *, bus_size_t);
164172567Sthompsastatic int	ipw_scan(struct ipw_softc *);
165172567Sthompsastatic void	ipw_scan_start(struct ieee80211com *);
166172567Sthompsastatic void	ipw_scan_end(struct ieee80211com *);
167172567Sthompsastatic void	ipw_set_channel(struct ieee80211com *);
168178354Ssamstatic void	ipw_scan_curchan(struct ieee80211_scan_state *,
169178354Ssam		    unsigned long maxdwell);
170178354Ssamstatic void	ipw_scan_mindwell(struct ieee80211_scan_state *);
171145247Sdamien
172145247Sdamienstatic int ipw_probe(device_t);
173145247Sdamienstatic int ipw_attach(device_t);
174145247Sdamienstatic int ipw_detach(device_t);
175145247Sdamienstatic int ipw_shutdown(device_t);
176145247Sdamienstatic int ipw_suspend(device_t);
177145247Sdamienstatic int ipw_resume(device_t);
178145247Sdamien
179145247Sdamienstatic device_method_t ipw_methods[] = {
180145247Sdamien	/* Device interface */
181145247Sdamien	DEVMETHOD(device_probe,		ipw_probe),
182145247Sdamien	DEVMETHOD(device_attach,	ipw_attach),
183145247Sdamien	DEVMETHOD(device_detach,	ipw_detach),
184145247Sdamien	DEVMETHOD(device_shutdown,	ipw_shutdown),
185145247Sdamien	DEVMETHOD(device_suspend,	ipw_suspend),
186145247Sdamien	DEVMETHOD(device_resume,	ipw_resume),
187145247Sdamien
188260062Smarius	DEVMETHOD_END
189145247Sdamien};
190145247Sdamien
191145247Sdamienstatic driver_t ipw_driver = {
192145247Sdamien	"ipw",
193145247Sdamien	ipw_methods,
194145247Sdamien	sizeof (struct ipw_softc)
195145247Sdamien};
196145247Sdamien
197145247Sdamienstatic devclass_t ipw_devclass;
198145247Sdamien
199260062SmariusDRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, NULL, NULL);
200145247Sdamien
201222543SbschmidtMODULE_VERSION(ipw, 1);
202222543Sbschmidt
203145247Sdamienstatic int
204145247Sdamienipw_probe(device_t dev)
205145247Sdamien{
206145247Sdamien	const struct ipw_ident *ident;
207145247Sdamien
208145247Sdamien	for (ident = ipw_ident_table; ident->name != NULL; ident++) {
209145247Sdamien		if (pci_get_vendor(dev) == ident->vendor &&
210145247Sdamien		    pci_get_device(dev) == ident->device) {
211145247Sdamien			device_set_desc(dev, ident->name);
212260062Smarius			return (BUS_PROBE_DEFAULT);
213145247Sdamien		}
214145247Sdamien	}
215145247Sdamien	return ENXIO;
216145247Sdamien}
217145247Sdamien
218145247Sdamien/* Base Address Register */
219145247Sdamienstatic int
220145247Sdamienipw_attach(device_t dev)
221145247Sdamien{
222145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
223286437Sadrian	struct ifnet *ifp;
224286437Sadrian	struct ieee80211com *ic;
225170530Ssam	struct ieee80211_channel *c;
226145247Sdamien	uint16_t val;
227145247Sdamien	int error, i;
228286437Sadrian	uint8_t macaddr[IEEE80211_ADDR_LEN];
229145247Sdamien
230145247Sdamien	sc->sc_dev = dev;
231145247Sdamien
232145247Sdamien	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
233145247Sdamien	    MTX_DEF | MTX_RECURSE);
234286437Sadrian
235156599Sdamien	TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
236172567Sthompsa	callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
237156599Sdamien
238146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
239146498Sdamien
240145247Sdamien	/* enable bus-mastering */
241145247Sdamien	pci_enable_busmaster(dev);
242145247Sdamien
243260062Smarius	i = PCIR_BAR(0);
244260062Smarius	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE);
245145247Sdamien	if (sc->mem == NULL) {
246145247Sdamien		device_printf(dev, "could not allocate memory resource\n");
247145247Sdamien		goto fail;
248145247Sdamien	}
249145247Sdamien
250145247Sdamien	sc->sc_st = rman_get_bustag(sc->mem);
251145247Sdamien	sc->sc_sh = rman_get_bushandle(sc->mem);
252145247Sdamien
253260062Smarius	i = 0;
254260062Smarius	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
255145247Sdamien	    RF_ACTIVE | RF_SHAREABLE);
256145247Sdamien	if (sc->irq == NULL) {
257145247Sdamien		device_printf(dev, "could not allocate interrupt resource\n");
258178354Ssam		goto fail1;
259145247Sdamien	}
260145247Sdamien
261145247Sdamien	if (ipw_reset(sc) != 0) {
262145247Sdamien		device_printf(dev, "could not reset adapter\n");
263178354Ssam		goto fail2;
264145247Sdamien	}
265145247Sdamien
266145247Sdamien	if (ipw_dma_alloc(sc) != 0) {
267145247Sdamien		device_printf(dev, "could not allocate DMA resources\n");
268178354Ssam		goto fail2;
269145247Sdamien	}
270147757Sdamien
271286437Sadrian	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
272286437Sadrian	if (ifp == NULL) {
273286437Sadrian		device_printf(dev, "can not if_alloc()\n");
274286437Sadrian		goto fail3;
275286437Sadrian	}
276286437Sadrian	ic = ifp->if_l2com;
277286437Sadrian
278286437Sadrian	ifp->if_softc = sc;
279286437Sadrian	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
280286437Sadrian	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
281286437Sadrian	ifp->if_init = ipw_init;
282286437Sadrian	ifp->if_ioctl = ipw_ioctl;
283286437Sadrian	ifp->if_start = ipw_start;
284286437Sadrian	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
285286437Sadrian	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
286286437Sadrian	IFQ_SET_READY(&ifp->if_snd);
287286437Sadrian
288286437Sadrian	ic->ic_ifp = ifp;
289283537Sglebius	ic->ic_softc = sc;
290283527Sglebius	ic->ic_name = device_get_nameunit(dev);
291178354Ssam	ic->ic_opmode = IEEE80211_M_STA;
292145247Sdamien	ic->ic_phytype = IEEE80211_T_DS;
293145247Sdamien
294145247Sdamien	/* set device capabilities */
295178957Ssam	ic->ic_caps =
296178957Ssam		  IEEE80211_C_STA		/* station mode supported */
297178957Ssam		| IEEE80211_C_IBSS		/* IBSS mode supported */
298172567Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode supported */
299172567Sthompsa		| IEEE80211_C_PMGT		/* power save supported */
300172567Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
301172567Sthompsa		| IEEE80211_C_WPA		/* 802.11i supported */
302172567Sthompsa		;
303145247Sdamien
304145247Sdamien	/* read MAC address from EEPROM */
305145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
306286437Sadrian	macaddr[0] = val >> 8;
307286437Sadrian	macaddr[1] = val & 0xff;
308145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
309286437Sadrian	macaddr[2] = val >> 8;
310286437Sadrian	macaddr[3] = val & 0xff;
311145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
312286437Sadrian	macaddr[4] = val >> 8;
313286437Sadrian	macaddr[5] = val & 0xff;
314145247Sdamien
315170530Ssam	/* set supported .11b channels (read from EEPROM) */
316170530Ssam	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
317170530Ssam		val = 0x7ff; /* default to channels 1-11 */
318170530Ssam	val <<= 1;
319170530Ssam	for (i = 1; i < 16; i++) {
320170530Ssam		if (val & (1 << i)) {
321170530Ssam			c = &ic->ic_channels[ic->ic_nchans++];
322170530Ssam			c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
323170530Ssam			c->ic_flags = IEEE80211_CHAN_B;
324170530Ssam			c->ic_ieee = i;
325170530Ssam		}
326145247Sdamien	}
327145247Sdamien
328145247Sdamien	/* check support for radio transmitter switch in EEPROM */
329145247Sdamien	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
330145247Sdamien		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
331145247Sdamien
332286437Sadrian	ieee80211_ifattach(ic, macaddr);
333172567Sthompsa	ic->ic_scan_start = ipw_scan_start;
334172567Sthompsa	ic->ic_scan_end = ipw_scan_end;
335172567Sthompsa	ic->ic_set_channel = ipw_set_channel;
336172567Sthompsa	ic->ic_scan_curchan = ipw_scan_curchan;
337172567Sthompsa	ic->ic_scan_mindwell = ipw_scan_mindwell;
338178354Ssam	ic->ic_raw_xmit = ipw_raw_xmit;
339286437Sadrian
340178354Ssam	ic->ic_vap_create = ipw_vap_create;
341178354Ssam	ic->ic_vap_delete = ipw_vap_delete;
342145247Sdamien
343192468Ssam	ieee80211_radiotap_attach(ic,
344192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
345192468Ssam		IPW_TX_RADIOTAP_PRESENT,
346192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
347192468Ssam		IPW_RX_RADIOTAP_PRESENT);
348178354Ssam
349145247Sdamien	/*
350145247Sdamien	 * Add a few sysctl knobs.
351145247Sdamien	 */
352145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
353145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
354145247Sdamien	    CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
355145247Sdamien	    "radio transmitter switch state (0=off, 1=on)");
356145247Sdamien
357145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
358145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
359145247Sdamien	    CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
360145247Sdamien	    "statistics");
361145247Sdamien
362145247Sdamien	/*
363145247Sdamien	 * Hook our interrupt after all initialization is complete.
364145247Sdamien	 */
365145247Sdamien	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
366166901Spiso	    NULL, ipw_intr, sc, &sc->sc_ih);
367145247Sdamien	if (error != 0) {
368145247Sdamien		device_printf(dev, "could not set up interrupt\n");
369286437Sadrian		goto fail4;
370145247Sdamien	}
371145247Sdamien
372145247Sdamien	if (bootverbose)
373145247Sdamien		ieee80211_announce(ic);
374145247Sdamien
375145247Sdamien	return 0;
376286437Sadrianfail4:
377286437Sadrian	if_free(ifp);
378178354Ssamfail3:
379178354Ssam	ipw_release(sc);
380178354Ssamfail2:
381260062Smarius	bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
382178354Ssamfail1:
383260062Smarius	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
384260062Smarius	    sc->mem);
385178354Ssamfail:
386178354Ssam	mtx_destroy(&sc->sc_mtx);
387145247Sdamien	return ENXIO;
388145247Sdamien}
389145247Sdamien
390145247Sdamienstatic int
391145247Sdamienipw_detach(device_t dev)
392145247Sdamien{
393145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
394286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
395286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
396145247Sdamien
397260062Smarius	bus_teardown_intr(dev, sc->irq, sc->sc_ih);
398260062Smarius
399191912Sthompsa	ieee80211_draintask(ic, &sc->sc_init_task);
400145247Sdamien	ipw_stop(sc);
401178354Ssam
402178354Ssam	ieee80211_ifdetach(ic);
403178354Ssam
404172567Sthompsa	callout_drain(&sc->sc_wdtimer);
405145247Sdamien
406145247Sdamien	ipw_release(sc);
407145247Sdamien
408260062Smarius	bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
409145247Sdamien
410260062Smarius	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
411260062Smarius	    sc->mem);
412156599Sdamien
413286437Sadrian	if_free(ifp);
414286437Sadrian
415159487Siedowse	if (sc->sc_firmware != NULL) {
416159487Siedowse		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
417159487Siedowse		sc->sc_firmware = NULL;
418159487Siedowse	}
419159487Siedowse
420145247Sdamien	mtx_destroy(&sc->sc_mtx);
421145247Sdamien
422145247Sdamien	return 0;
423145247Sdamien}
424145247Sdamien
425178354Ssamstatic struct ieee80211vap *
426228621Sbschmidtipw_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
427228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
428228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
429228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
430178354Ssam{
431286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
432178354Ssam	struct ipw_vap *ivp;
433178354Ssam	struct ieee80211vap *vap;
434178354Ssam	const struct firmware *fp;
435178354Ssam	const struct ipw_firmware_hdr *hdr;
436178354Ssam	const char *imagename;
437178354Ssam
438178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
439178354Ssam		return NULL;
440178354Ssam
441178354Ssam	switch (opmode) {
442178354Ssam	case IEEE80211_M_STA:
443178354Ssam		imagename = "ipw_bss";
444178354Ssam		break;
445178354Ssam	case IEEE80211_M_IBSS:
446178354Ssam		imagename = "ipw_ibss";
447178354Ssam		break;
448178354Ssam	case IEEE80211_M_MONITOR:
449178354Ssam		imagename = "ipw_monitor";
450178354Ssam		break;
451178354Ssam	default:
452178354Ssam		return NULL;
453178354Ssam	}
454178354Ssam
455178354Ssam	/*
456178354Ssam	 * Load firmware image using the firmware(9) subsystem.  Doing
457178354Ssam	 * this unlocked is ok since we're single-threaded by the
458178354Ssam	 * 802.11 layer.
459178354Ssam	 */
460178354Ssam	if (sc->sc_firmware == NULL ||
461178354Ssam	    strcmp(sc->sc_firmware->name, imagename) != 0) {
462178354Ssam		if (sc->sc_firmware != NULL)
463178354Ssam			firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
464178354Ssam		sc->sc_firmware = firmware_get(imagename);
465178354Ssam	}
466178354Ssam	if (sc->sc_firmware == NULL) {
467178354Ssam		device_printf(sc->sc_dev,
468178354Ssam		    "could not load firmware image '%s'\n", imagename);
469178354Ssam		return NULL;
470178354Ssam	}
471178354Ssam	fp = sc->sc_firmware;
472178354Ssam	if (fp->datasize < sizeof *hdr) {
473178354Ssam		device_printf(sc->sc_dev,
474178354Ssam		    "firmware image too short %zu\n", fp->datasize);
475178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
476178354Ssam		sc->sc_firmware = NULL;
477178354Ssam		return NULL;
478178354Ssam	}
479178354Ssam	hdr = (const struct ipw_firmware_hdr *)fp->data;
480178354Ssam	if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
481178354Ssam	    le32toh(hdr->ucodesz)) {
482178354Ssam		device_printf(sc->sc_dev,
483178354Ssam		    "firmware image too short %zu\n", fp->datasize);
484178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
485178354Ssam		sc->sc_firmware = NULL;
486178354Ssam		return NULL;
487178354Ssam	}
488178354Ssam
489286437Sadrian	ivp = (struct ipw_vap *) malloc(sizeof(struct ipw_vap),
490286437Sadrian	    M_80211_VAP, M_NOWAIT | M_ZERO);
491286437Sadrian	if (ivp == NULL)
492286437Sadrian		return NULL;
493178354Ssam	vap = &ivp->vap;
494178354Ssam
495286437Sadrian	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
496178354Ssam	/* override with driver methods */
497178354Ssam	ivp->newstate = vap->iv_newstate;
498178354Ssam	vap->iv_newstate = ipw_newstate;
499178354Ssam
500178354Ssam	/* complete setup */
501286437Sadrian	ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status);
502178354Ssam	ic->ic_opmode = opmode;
503178354Ssam	return vap;
504178354Ssam}
505178354Ssam
506178354Ssamstatic void
507178354Ssamipw_vap_delete(struct ieee80211vap *vap)
508178354Ssam{
509178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
510178354Ssam
511178354Ssam	ieee80211_vap_detach(vap);
512178354Ssam	free(ivp, M_80211_VAP);
513178354Ssam}
514178354Ssam
515145247Sdamienstatic int
516145247Sdamienipw_dma_alloc(struct ipw_softc *sc)
517145247Sdamien{
518145247Sdamien	struct ipw_soft_bd *sbd;
519145247Sdamien	struct ipw_soft_hdr *shdr;
520145247Sdamien	struct ipw_soft_buf *sbuf;
521145247Sdamien	bus_addr_t physaddr;
522145247Sdamien	int error, i;
523145247Sdamien
524145247Sdamien	/*
525232874Sscottl	 * Allocate parent DMA tag for subsequent allocations.
526232874Sscottl	 */
527232874Sscottl	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
528232874Sscottl	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
529232874Sscottl	    BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED,
530232874Sscottl	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &sc->parent_dmat);
531232874Sscottl	if (error != 0) {
532232874Sscottl		device_printf(sc->sc_dev, "could not create parent DMA tag\n");
533232874Sscottl		goto fail;
534232874Sscottl	}
535232874Sscottl
536232874Sscottl	/*
537145247Sdamien	 * Allocate and map tx ring.
538145247Sdamien	 */
539232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
540145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
541145247Sdamien	    NULL, &sc->tbd_dmat);
542145247Sdamien	if (error != 0) {
543145247Sdamien		device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
544145247Sdamien		goto fail;
545145247Sdamien	}
546145247Sdamien
547145247Sdamien	error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
548145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
549145247Sdamien	if (error != 0) {
550145247Sdamien		device_printf(sc->sc_dev,
551145247Sdamien		    "could not allocate tx ring DMA memory\n");
552145247Sdamien		goto fail;
553145247Sdamien	}
554145247Sdamien
555145247Sdamien	error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
556145247Sdamien	    IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
557145247Sdamien	if (error != 0) {
558145247Sdamien		device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
559145247Sdamien		goto fail;
560145247Sdamien	}
561145247Sdamien
562145247Sdamien	/*
563145247Sdamien	 * Allocate and map rx ring.
564145247Sdamien	 */
565232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
566145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
567145247Sdamien	    NULL, &sc->rbd_dmat);
568145247Sdamien	if (error != 0) {
569145247Sdamien		device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
570145247Sdamien		goto fail;
571145247Sdamien	}
572145247Sdamien
573145247Sdamien	error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
574145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
575145247Sdamien	if (error != 0) {
576145247Sdamien		device_printf(sc->sc_dev,
577145247Sdamien		    "could not allocate rx ring DMA memory\n");
578145247Sdamien		goto fail;
579145247Sdamien	}
580145247Sdamien
581145247Sdamien	error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
582145247Sdamien	    IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
583145247Sdamien	if (error != 0) {
584145247Sdamien		device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
585145247Sdamien		goto fail;
586145247Sdamien	}
587145247Sdamien
588145247Sdamien	/*
589145247Sdamien	 * Allocate and map status ring.
590145247Sdamien	 */
591232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
592145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
593145247Sdamien	    NULL, NULL, &sc->status_dmat);
594145247Sdamien	if (error != 0) {
595145247Sdamien		device_printf(sc->sc_dev,
596145247Sdamien		    "could not create status ring DMA tag\n");
597145247Sdamien		goto fail;
598145247Sdamien	}
599145247Sdamien
600145247Sdamien	error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
601145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
602145247Sdamien	if (error != 0) {
603145247Sdamien		device_printf(sc->sc_dev,
604145247Sdamien		    "could not allocate status ring DMA memory\n");
605145247Sdamien		goto fail;
606145247Sdamien	}
607145247Sdamien
608145247Sdamien	error = bus_dmamap_load(sc->status_dmat, sc->status_map,
609145247Sdamien	    sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
610145247Sdamien	    0);
611145247Sdamien	if (error != 0) {
612145247Sdamien		device_printf(sc->sc_dev,
613145247Sdamien		    "could not map status ring DMA memory\n");
614145247Sdamien		goto fail;
615145247Sdamien	}
616145247Sdamien
617145247Sdamien	/*
618145247Sdamien	 * Allocate command DMA map.
619145247Sdamien	 */
620232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
621145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
622145247Sdamien	    sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
623145247Sdamien	if (error != 0) {
624145247Sdamien		device_printf(sc->sc_dev, "could not create command DMA tag\n");
625145247Sdamien		goto fail;
626145247Sdamien	}
627145247Sdamien
628145247Sdamien	error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
629145247Sdamien	if (error != 0) {
630145247Sdamien		device_printf(sc->sc_dev,
631145247Sdamien		    "could not create command DMA map\n");
632145247Sdamien		goto fail;
633145247Sdamien	}
634145247Sdamien
635145247Sdamien	/*
636145247Sdamien	 * Allocate headers DMA maps.
637145247Sdamien	 */
638232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
639145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
640145247Sdamien	    sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
641145247Sdamien	if (error != 0) {
642145247Sdamien		device_printf(sc->sc_dev, "could not create header DMA tag\n");
643145247Sdamien		goto fail;
644145247Sdamien	}
645145247Sdamien
646145247Sdamien	SLIST_INIT(&sc->free_shdr);
647145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
648145247Sdamien		shdr = &sc->shdr_list[i];
649145247Sdamien		error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
650145247Sdamien		if (error != 0) {
651145247Sdamien			device_printf(sc->sc_dev,
652145247Sdamien			    "could not create header DMA map\n");
653145247Sdamien			goto fail;
654145247Sdamien		}
655145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
656145247Sdamien	}
657145247Sdamien
658145247Sdamien	/*
659145247Sdamien	 * Allocate tx buffers DMA maps.
660145247Sdamien	 */
661232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
662145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
663145247Sdamien	    NULL, NULL, &sc->txbuf_dmat);
664145247Sdamien	if (error != 0) {
665145247Sdamien		device_printf(sc->sc_dev, "could not create tx DMA tag\n");
666145247Sdamien		goto fail;
667145247Sdamien	}
668145247Sdamien
669145247Sdamien	SLIST_INIT(&sc->free_sbuf);
670145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
671145247Sdamien		sbuf = &sc->tx_sbuf_list[i];
672145247Sdamien		error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
673145247Sdamien		if (error != 0) {
674145247Sdamien			device_printf(sc->sc_dev,
675145247Sdamien			    "could not create tx DMA map\n");
676145247Sdamien			goto fail;
677145247Sdamien		}
678145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
679145247Sdamien	}
680145247Sdamien
681145247Sdamien	/*
682145247Sdamien	 * Initialize tx ring.
683145247Sdamien	 */
684145247Sdamien	for (i = 0; i < IPW_NTBD; i++) {
685145247Sdamien		sbd = &sc->stbd_list[i];
686145247Sdamien		sbd->bd = &sc->tbd_list[i];
687145247Sdamien		sbd->type = IPW_SBD_TYPE_NOASSOC;
688145247Sdamien	}
689145247Sdamien
690145247Sdamien	/*
691145247Sdamien	 * Pre-allocate rx buffers and DMA maps.
692145247Sdamien	 */
693232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
694147841Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
695147841Sdamien	    NULL, &sc->rxbuf_dmat);
696145247Sdamien	if (error != 0) {
697145247Sdamien		device_printf(sc->sc_dev, "could not create rx DMA tag\n");
698145247Sdamien		goto fail;
699145247Sdamien	}
700145247Sdamien
701145247Sdamien	for (i = 0; i < IPW_NRBD; i++) {
702145247Sdamien		sbd = &sc->srbd_list[i];
703145247Sdamien		sbuf = &sc->rx_sbuf_list[i];
704145247Sdamien		sbd->bd = &sc->rbd_list[i];
705145247Sdamien
706243857Sglebius		sbuf->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
707145247Sdamien		if (sbuf->m == NULL) {
708145247Sdamien			device_printf(sc->sc_dev,
709145247Sdamien			    "could not allocate rx mbuf\n");
710145247Sdamien			error = ENOMEM;
711145247Sdamien			goto fail;
712145247Sdamien		}
713145247Sdamien
714145247Sdamien		error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
715145247Sdamien		if (error != 0) {
716145247Sdamien			device_printf(sc->sc_dev,
717145247Sdamien			    "could not create rx DMA map\n");
718145247Sdamien			goto fail;
719145247Sdamien		}
720145247Sdamien
721145247Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
722145247Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
723145247Sdamien		    &physaddr, 0);
724145247Sdamien		if (error != 0) {
725145247Sdamien			device_printf(sc->sc_dev,
726145247Sdamien			    "could not map rx DMA memory\n");
727145247Sdamien			goto fail;
728145247Sdamien		}
729145247Sdamien
730145247Sdamien		sbd->type = IPW_SBD_TYPE_DATA;
731145247Sdamien		sbd->priv = sbuf;
732145247Sdamien		sbd->bd->physaddr = htole32(physaddr);
733145247Sdamien		sbd->bd->len = htole32(MCLBYTES);
734145247Sdamien	}
735145247Sdamien
736145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
737145247Sdamien
738145247Sdamien	return 0;
739145247Sdamien
740145247Sdamienfail:	ipw_release(sc);
741145247Sdamien	return error;
742145247Sdamien}
743145247Sdamien
744145247Sdamienstatic void
745145247Sdamienipw_release(struct ipw_softc *sc)
746145247Sdamien{
747145247Sdamien	struct ipw_soft_buf *sbuf;
748145247Sdamien	int i;
749145247Sdamien
750232874Sscottl	if (sc->parent_dmat != NULL) {
751232874Sscottl		bus_dma_tag_destroy(sc->parent_dmat);
752232874Sscottl	}
753232874Sscottl
754145247Sdamien	if (sc->tbd_dmat != NULL) {
755283528Sglebius		bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
756283528Sglebius		bus_dmamem_free(sc->tbd_dmat, sc->tbd_list, sc->tbd_map);
757145247Sdamien		bus_dma_tag_destroy(sc->tbd_dmat);
758145247Sdamien	}
759145247Sdamien
760145247Sdamien	if (sc->rbd_dmat != NULL) {
761145247Sdamien		if (sc->rbd_list != NULL) {
762145247Sdamien			bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
763145247Sdamien			bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
764145247Sdamien			    sc->rbd_map);
765145247Sdamien		}
766145247Sdamien		bus_dma_tag_destroy(sc->rbd_dmat);
767145247Sdamien	}
768145247Sdamien
769145247Sdamien	if (sc->status_dmat != NULL) {
770145247Sdamien		if (sc->status_list != NULL) {
771145247Sdamien			bus_dmamap_unload(sc->status_dmat, sc->status_map);
772145247Sdamien			bus_dmamem_free(sc->status_dmat, sc->status_list,
773145247Sdamien			    sc->status_map);
774145247Sdamien		}
775145247Sdamien		bus_dma_tag_destroy(sc->status_dmat);
776145247Sdamien	}
777145247Sdamien
778145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
779145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
780145247Sdamien
781145247Sdamien	if (sc->cmd_dmat != NULL) {
782145247Sdamien		bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
783145247Sdamien		bus_dma_tag_destroy(sc->cmd_dmat);
784145247Sdamien	}
785145247Sdamien
786145247Sdamien	if (sc->hdr_dmat != NULL) {
787145247Sdamien		for (i = 0; i < IPW_NDATA; i++)
788145247Sdamien			bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
789145247Sdamien		bus_dma_tag_destroy(sc->hdr_dmat);
790145247Sdamien	}
791145247Sdamien
792145247Sdamien	if (sc->txbuf_dmat != NULL) {
793145247Sdamien		for (i = 0; i < IPW_NDATA; i++) {
794145247Sdamien			bus_dmamap_destroy(sc->txbuf_dmat,
795145247Sdamien			    sc->tx_sbuf_list[i].map);
796145247Sdamien		}
797145247Sdamien		bus_dma_tag_destroy(sc->txbuf_dmat);
798145247Sdamien	}
799145247Sdamien
800145247Sdamien	if (sc->rxbuf_dmat != NULL) {
801145247Sdamien		for (i = 0; i < IPW_NRBD; i++) {
802145247Sdamien			sbuf = &sc->rx_sbuf_list[i];
803145247Sdamien			if (sbuf->m != NULL) {
804145247Sdamien				bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
805145247Sdamien				    BUS_DMASYNC_POSTREAD);
806145247Sdamien				bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
807145247Sdamien				m_freem(sbuf->m);
808145247Sdamien			}
809145247Sdamien			bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
810145247Sdamien		}
811145247Sdamien		bus_dma_tag_destroy(sc->rxbuf_dmat);
812145247Sdamien	}
813145247Sdamien}
814145247Sdamien
815145247Sdamienstatic int
816145247Sdamienipw_shutdown(device_t dev)
817145247Sdamien{
818145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
819145247Sdamien
820145247Sdamien	ipw_stop(sc);
821145247Sdamien
822145247Sdamien	return 0;
823145247Sdamien}
824145247Sdamien
825145247Sdamienstatic int
826145247Sdamienipw_suspend(device_t dev)
827145247Sdamien{
828145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
829286437Sadrian	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
830145247Sdamien
831233387Sbschmidt	ieee80211_suspend_all(ic);
832145247Sdamien	return 0;
833145247Sdamien}
834145247Sdamien
835145247Sdamienstatic int
836145247Sdamienipw_resume(device_t dev)
837145247Sdamien{
838145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
839286437Sadrian	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
840145247Sdamien
841146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
842146498Sdamien
843233387Sbschmidt	ieee80211_resume_all(ic);
844145247Sdamien	return 0;
845145247Sdamien}
846145247Sdamien
847145247Sdamienstatic int
848172567Sthompsaipw_cvtrate(int ipwrate)
849172567Sthompsa{
850172567Sthompsa	switch (ipwrate) {
851172567Sthompsa	case IPW_RATE_DS1:	return 2;
852172567Sthompsa	case IPW_RATE_DS2:	return 4;
853172567Sthompsa	case IPW_RATE_DS5:	return 11;
854172567Sthompsa	case IPW_RATE_DS11:	return 22;
855172567Sthompsa	}
856145247Sdamien	return 0;
857145247Sdamien}
858145247Sdamien
859145247Sdamien/*
860156599Sdamien * The firmware automatically adapts the transmit speed. We report its current
861156599Sdamien * value here.
862145247Sdamien */
863145247Sdamienstatic void
864145247Sdamienipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
865145247Sdamien{
866178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
867178354Ssam	struct ieee80211com *ic = vap->iv_ic;
868286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
869145247Sdamien
870145247Sdamien	/* read current transmission rate from adapter */
871178354Ssam	vap->iv_bss->ni_txrate = ipw_cvtrate(
872178354Ssam	    ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
873178354Ssam	ieee80211_media_status(ifp, imr);
874145247Sdamien}
875145247Sdamien
876145247Sdamienstatic int
877178354Ssamipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
878145247Sdamien{
879178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
880178354Ssam	struct ieee80211com *ic = vap->iv_ic;
881286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
882191746Sthompsa	enum ieee80211_state ostate;
883145247Sdamien
884172567Sthompsa	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
885178354Ssam		ieee80211_state_name[vap->iv_state],
886172567Sthompsa		ieee80211_state_name[nstate], sc->flags));
887172567Sthompsa
888191746Sthompsa	ostate = vap->iv_state;
889191746Sthompsa	IEEE80211_UNLOCK(ic);
890191746Sthompsa
891145247Sdamien	switch (nstate) {
892145247Sdamien	case IEEE80211_S_RUN:
893172567Sthompsa		if (ic->ic_opmode == IEEE80211_M_IBSS) {
894172567Sthompsa			/*
895172567Sthompsa			 * XXX when joining an ibss network we are called
896172567Sthompsa			 * with a SCAN -> RUN transition on scan complete.
897206763Sbschmidt			 * Use that to call ipw_assoc.  On completing the
898206763Sbschmidt			 * join we are then called again with an AUTH -> RUN
899206763Sbschmidt			 * transition and we want to do nothing.  This is
900206763Sbschmidt			 * all totally bogus and needs to be redone.
901172567Sthompsa			 */
902191746Sthompsa			if (ostate == IEEE80211_S_SCAN)
903191746Sthompsa				ipw_assoc(ic, vap);
904172567Sthompsa		}
905172567Sthompsa		break;
906145247Sdamien
907172567Sthompsa	case IEEE80211_S_INIT:
908172567Sthompsa		if (sc->flags & IPW_FLAG_ASSOCIATED)
909191746Sthompsa			ipw_disassoc(ic, vap);
910172567Sthompsa		break;
911145247Sdamien
912172567Sthompsa	case IEEE80211_S_AUTH:
913206765Sbschmidt		/*
914206765Sbschmidt		 * Move to ASSOC state after the ipw_assoc() call.  Firmware
915206765Sbschmidt		 * takes care of authentication, after the call we'll receive
916206765Sbschmidt		 * only an assoc response which would otherwise be discared
917206765Sbschmidt		 * if we are still in AUTH state.
918206765Sbschmidt		 */
919206765Sbschmidt		nstate = IEEE80211_S_ASSOC;
920191746Sthompsa		ipw_assoc(ic, vap);
921191746Sthompsa		break;
922145247Sdamien
923172567Sthompsa	case IEEE80211_S_ASSOC:
924172567Sthompsa		/*
925206763Sbschmidt		 * If we are not transitioning from AUTH then resend the
926172567Sthompsa		 * association request.
927172567Sthompsa		 */
928191746Sthompsa		if (ostate != IEEE80211_S_AUTH)
929191746Sthompsa			ipw_assoc(ic, vap);
930145247Sdamien		break;
931145247Sdamien
932172058Ssam	default:
933145247Sdamien		break;
934145247Sdamien	}
935191746Sthompsa	IEEE80211_LOCK(ic);
936178354Ssam	return ivp->newstate(vap, nstate, arg);
937145247Sdamien}
938145247Sdamien
939145247Sdamien/*
940145247Sdamien * Read 16 bits at address 'addr' from the serial EEPROM.
941145247Sdamien */
942145247Sdamienstatic uint16_t
943145247Sdamienipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
944145247Sdamien{
945145247Sdamien	uint32_t tmp;
946145247Sdamien	uint16_t val;
947145247Sdamien	int n;
948145247Sdamien
949145247Sdamien	/* clock C once before the first command */
950145247Sdamien	IPW_EEPROM_CTL(sc, 0);
951145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
952145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
953145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
954145247Sdamien
955145247Sdamien	/* write start bit (1) */
956145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
957145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
958145247Sdamien
959145247Sdamien	/* write READ opcode (10) */
960145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
961145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
962145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
963145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
964145247Sdamien
965145247Sdamien	/* write address A7-A0 */
966145247Sdamien	for (n = 7; n >= 0; n--) {
967145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
968145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
969145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
970145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
971145247Sdamien	}
972145247Sdamien
973145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
974145247Sdamien
975145247Sdamien	/* read data Q15-Q0 */
976145247Sdamien	val = 0;
977145247Sdamien	for (n = 15; n >= 0; n--) {
978145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
979145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
980145247Sdamien		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
981145247Sdamien		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
982145247Sdamien	}
983145247Sdamien
984145247Sdamien	IPW_EEPROM_CTL(sc, 0);
985145247Sdamien
986145247Sdamien	/* clear Chip Select and clock C */
987145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
988145247Sdamien	IPW_EEPROM_CTL(sc, 0);
989145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
990145247Sdamien
991145247Sdamien	return le16toh(val);
992145247Sdamien}
993145247Sdamien
994145247Sdamienstatic void
995172567Sthompsaipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
996145247Sdamien{
997145247Sdamien	struct ipw_cmd *cmd;
998145247Sdamien
999145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1000145247Sdamien
1001145247Sdamien	cmd = mtod(sbuf->m, struct ipw_cmd *);
1002145247Sdamien
1003172567Sthompsa	DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
1004172567Sthompsa	    ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
1005145247Sdamien	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
1006145247Sdamien	    le32toh(cmd->status)));
1007145247Sdamien
1008172567Sthompsa	sc->flags &= ~IPW_FLAG_BUSY;
1009145247Sdamien	wakeup(sc);
1010145247Sdamien}
1011145247Sdamien
1012145247Sdamienstatic void
1013172567Sthompsaipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1014145247Sdamien{
1015178354Ssam#define	IEEESTATE(vap)	ieee80211_state_name[vap->iv_state]
1016286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
1017286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1018178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1019145247Sdamien	uint32_t state;
1020145247Sdamien
1021145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1022145247Sdamien
1023145247Sdamien	state = le32toh(*mtod(sbuf->m, uint32_t *));
1024145247Sdamien
1025145247Sdamien	switch (state) {
1026145247Sdamien	case IPW_STATE_ASSOCIATED:
1027172567Sthompsa		DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1028178354Ssam			IEEESTATE(vap), sc->flags));
1029178354Ssam		/* XXX suppress state change in case the fw auto-associates */
1030178354Ssam		if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1031178354Ssam			DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1032178354Ssam				IEEESTATE(vap), sc->flags));
1033178354Ssam			break;
1034178354Ssam		}
1035178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATING;
1036172567Sthompsa		sc->flags |= IPW_FLAG_ASSOCIATED;
1037145247Sdamien		break;
1038145247Sdamien
1039145247Sdamien	case IPW_STATE_SCANNING:
1040172567Sthompsa		DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1041178354Ssam			IEEESTATE(vap), sc->flags));
1042172567Sthompsa		/*
1043172567Sthompsa		 * NB: Check driver state for association on assoc
1044172567Sthompsa		 * loss as the firmware will immediately start to
1045172567Sthompsa		 * scan and we would treat it as a beacon miss if
1046172567Sthompsa		 * we checked the 802.11 layer state.
1047172567Sthompsa		 */
1048178354Ssam		if (sc->flags & IPW_FLAG_ASSOCIATED) {
1049206767Sbschmidt			IPW_UNLOCK(sc);
1050178354Ssam			/* XXX probably need to issue disassoc to fw */
1051191746Sthompsa			ieee80211_beacon_miss(ic);
1052206767Sbschmidt			IPW_LOCK(sc);
1053178354Ssam		}
1054145247Sdamien		break;
1055145247Sdamien
1056145247Sdamien	case IPW_STATE_SCAN_COMPLETE:
1057172567Sthompsa		/*
1058172567Sthompsa		 * XXX For some reason scan requests generate scan
1059172567Sthompsa		 * started + scan done events before any traffic is
1060172567Sthompsa		 * received (e.g. probe response frames).  We work
1061172567Sthompsa		 * around this by marking the HACK flag and skipping
1062172567Sthompsa		 * the first scan complete event.
1063172567Sthompsa		*/
1064178354Ssam		DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1065178354Ssam			    IEEESTATE(vap), sc->flags));
1066172567Sthompsa		if (sc->flags & IPW_FLAG_HACK) {
1067172567Sthompsa			sc->flags &= ~IPW_FLAG_HACK;
1068172567Sthompsa			break;
1069172567Sthompsa		}
1070172567Sthompsa		if (sc->flags & IPW_FLAG_SCANNING) {
1071206767Sbschmidt			IPW_UNLOCK(sc);
1072191746Sthompsa			ieee80211_scan_done(vap);
1073206767Sbschmidt			IPW_LOCK(sc);
1074172567Sthompsa			sc->flags &= ~IPW_FLAG_SCANNING;
1075172567Sthompsa			sc->sc_scan_timer = 0;
1076172567Sthompsa		}
1077145247Sdamien		break;
1078145247Sdamien
1079145247Sdamien	case IPW_STATE_ASSOCIATION_LOST:
1080172567Sthompsa		DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1081178354Ssam			IEEESTATE(vap), sc->flags));
1082178354Ssam		sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1083206767Sbschmidt		if (vap->iv_state == IEEE80211_S_RUN) {
1084206767Sbschmidt			IPW_UNLOCK(sc);
1085191746Sthompsa			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1086206767Sbschmidt			IPW_LOCK(sc);
1087206767Sbschmidt		}
1088145247Sdamien		break;
1089145247Sdamien
1090172567Sthompsa	case IPW_STATE_DISABLED:
1091178354Ssam		/* XXX? is this right? */
1092206763Sbschmidt		sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1093178354Ssam		    IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1094172567Sthompsa		DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1095178354Ssam			IEEESTATE(vap), sc->flags));
1096172567Sthompsa		break;
1097172567Sthompsa
1098145247Sdamien	case IPW_STATE_RADIO_DISABLED:
1099178354Ssam		device_printf(sc->sc_dev, "radio turned off\n");
1100178354Ssam		ieee80211_notify_radio(ic, 0);
1101172567Sthompsa		ipw_stop_locked(sc);
1102178354Ssam		/* XXX start polling thread to detect radio on */
1103145247Sdamien		break;
1104172567Sthompsa
1105172567Sthompsa	default:
1106172567Sthompsa		DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1107178354Ssam			__func__, state, IEEESTATE(vap), sc->flags));
1108172567Sthompsa		break;
1109145247Sdamien	}
1110172567Sthompsa#undef IEEESTATE
1111145247Sdamien}
1112145247Sdamien
1113145247Sdamien/*
1114172567Sthompsa * Set driver state for current channel.
1115172567Sthompsa */
1116172567Sthompsastatic void
1117172567Sthompsaipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1118172567Sthompsa{
1119286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
1120286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1121172567Sthompsa
1122172567Sthompsa	ic->ic_curchan = chan;
1123192468Ssam	ieee80211_radiotap_chan_change(ic);
1124172567Sthompsa}
1125172567Sthompsa
1126172567Sthompsa/*
1127145247Sdamien * XXX: Hack to set the current channel to the value advertised in beacons or
1128145247Sdamien * probe responses. Only used during AP detection.
1129145247Sdamien */
1130145247Sdamienstatic void
1131172567Sthompsaipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1132145247Sdamien{
1133286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
1134286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1135172567Sthompsa	struct ieee80211_channel *c;
1136145247Sdamien	struct ieee80211_frame *wh;
1137145247Sdamien	uint8_t subtype;
1138145247Sdamien	uint8_t *frm, *efrm;
1139145247Sdamien
1140145247Sdamien	wh = mtod(m, struct ieee80211_frame *);
1141145247Sdamien
1142145247Sdamien	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1143145247Sdamien		return;
1144145247Sdamien
1145145247Sdamien	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1146145247Sdamien
1147145247Sdamien	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1148145247Sdamien	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1149145247Sdamien		return;
1150145247Sdamien
1151178354Ssam	/* XXX use ieee80211_parse_beacon */
1152145247Sdamien	frm = (uint8_t *)(wh + 1);
1153145247Sdamien	efrm = mtod(m, uint8_t *) + m->m_len;
1154145247Sdamien
1155145247Sdamien	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1156145247Sdamien	while (frm < efrm) {
1157145247Sdamien		if (*frm == IEEE80211_ELEMID_DSPARMS)
1158145247Sdamien#if IEEE80211_CHAN_MAX < 255
1159145247Sdamien		if (frm[2] <= IEEE80211_CHAN_MAX)
1160145247Sdamien#endif
1161172567Sthompsa		{
1162172567Sthompsa			DPRINTF(("Fixing channel to %d\n", frm[2]));
1163172567Sthompsa			c = ieee80211_find_channel(ic,
1164170530Ssam				ieee80211_ieee2mhz(frm[2], 0),
1165172567Sthompsa				IEEE80211_CHAN_B);
1166172567Sthompsa			if (c == NULL)
1167172567Sthompsa				c = &ic->ic_channels[0];
1168172567Sthompsa			ipw_setcurchan(sc, c);
1169172567Sthompsa		}
1170145247Sdamien
1171145247Sdamien		frm += frm[1] + 2;
1172145247Sdamien	}
1173145247Sdamien}
1174145247Sdamien
1175145247Sdamienstatic void
1176172567Sthompsaipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1177145247Sdamien    struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1178145247Sdamien{
1179286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
1180286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1181152385Sdamien	struct mbuf *mnew, *m;
1182145247Sdamien	struct ieee80211_node *ni;
1183145247Sdamien	bus_addr_t physaddr;
1184145247Sdamien	int error;
1185192468Ssam	int8_t rssi, nf;
1186145247Sdamien
1187152385Sdamien	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1188152385Sdamien	    status->rssi));
1189152385Sdamien
1190147757Sdamien	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1191147757Sdamien	    le32toh(status->len) > MCLBYTES)
1192147757Sdamien		return;
1193147757Sdamien
1194152385Sdamien	/*
1195152385Sdamien	 * Try to allocate a new mbuf for this ring element and load it before
1196152385Sdamien	 * processing the current mbuf. If the ring element cannot be loaded,
1197152385Sdamien	 * drop the received packet and reuse the old mbuf. In the unlikely
1198152385Sdamien	 * case that the old mbuf can't be reloaded either, explicitly panic.
1199152385Sdamien	 */
1200243857Sglebius	mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1201152385Sdamien	if (mnew == NULL) {
1202286437Sadrian		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1203152385Sdamien		return;
1204152385Sdamien	}
1205152385Sdamien
1206145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1207145247Sdamien	bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1208145247Sdamien
1209152385Sdamien	error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1210152385Sdamien	    MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1211152385Sdamien	if (error != 0) {
1212152385Sdamien		m_freem(mnew);
1213152385Sdamien
1214152385Sdamien		/* try to reload the old mbuf */
1215152385Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1216152385Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1217152385Sdamien		    &physaddr, 0);
1218152385Sdamien		if (error != 0) {
1219152385Sdamien			/* very unlikely that it will fail... */
1220152385Sdamien			panic("%s: could not load old rx mbuf",
1221152385Sdamien			    device_get_name(sc->sc_dev));
1222152385Sdamien		}
1223286437Sadrian		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1224152385Sdamien		return;
1225152385Sdamien	}
1226152385Sdamien
1227152385Sdamien	/*
1228152385Sdamien	 * New mbuf successfully loaded, update Rx ring and continue
1229152385Sdamien	 * processing.
1230152385Sdamien	 */
1231152385Sdamien	m = sbuf->m;
1232152385Sdamien	sbuf->m = mnew;
1233152385Sdamien	sbd->bd->physaddr = htole32(physaddr);
1234286437Sadrian
1235286437Sadrian	/* finalize mbuf */
1236286437Sadrian	m->m_pkthdr.rcvif = ifp;
1237145247Sdamien	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1238145247Sdamien
1239192468Ssam	rssi = status->rssi + IPW_RSSI_TO_DBM;
1240192468Ssam	nf = -95;
1241192468Ssam	if (ieee80211_radiotap_active(ic)) {
1242145247Sdamien		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1243145247Sdamien
1244145247Sdamien		tap->wr_flags = 0;
1245192468Ssam		tap->wr_antsignal = rssi;
1246192468Ssam		tap->wr_antnoise = nf;
1247145247Sdamien	}
1248145247Sdamien
1249172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
1250172567Sthompsa		ipw_fix_channel(sc, m);
1251145247Sdamien
1252172567Sthompsa	IPW_UNLOCK(sc);
1253178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1254178354Ssam	if (ni != NULL) {
1255206764Sbschmidt		(void) ieee80211_input(ni, m, rssi - nf, nf);
1256178354Ssam		ieee80211_free_node(ni);
1257178354Ssam	} else
1258206764Sbschmidt		(void) ieee80211_input_all(ic, m, rssi - nf, nf);
1259172567Sthompsa	IPW_LOCK(sc);
1260145247Sdamien
1261145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1262145247Sdamien}
1263145247Sdamien
1264145247Sdamienstatic void
1265145247Sdamienipw_rx_intr(struct ipw_softc *sc)
1266145247Sdamien{
1267145247Sdamien	struct ipw_status *status;
1268145247Sdamien	struct ipw_soft_bd *sbd;
1269145247Sdamien	struct ipw_soft_buf *sbuf;
1270145247Sdamien	uint32_t r, i;
1271145247Sdamien
1272145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1273145247Sdamien		return;
1274145247Sdamien
1275145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1276145247Sdamien
1277145247Sdamien	bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1278145247Sdamien
1279145247Sdamien	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1280145247Sdamien		status = &sc->status_list[i];
1281145247Sdamien		sbd = &sc->srbd_list[i];
1282145247Sdamien		sbuf = sbd->priv;
1283145247Sdamien
1284145247Sdamien		switch (le16toh(status->code) & 0xf) {
1285145247Sdamien		case IPW_STATUS_CODE_COMMAND:
1286172567Sthompsa			ipw_rx_cmd_intr(sc, sbuf);
1287145247Sdamien			break;
1288145247Sdamien
1289145247Sdamien		case IPW_STATUS_CODE_NEWSTATE:
1290172567Sthompsa			ipw_rx_newstate_intr(sc, sbuf);
1291145247Sdamien			break;
1292145247Sdamien
1293145247Sdamien		case IPW_STATUS_CODE_DATA_802_3:
1294145247Sdamien		case IPW_STATUS_CODE_DATA_802_11:
1295172567Sthompsa			ipw_rx_data_intr(sc, status, sbd, sbuf);
1296145247Sdamien			break;
1297145247Sdamien
1298145247Sdamien		case IPW_STATUS_CODE_NOTIFICATION:
1299172567Sthompsa			DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1300172567Sthompsa			    le32toh(status->len), status->flags));
1301178354Ssam			/* XXX maybe drive state machine AUTH->ASSOC? */
1302145247Sdamien			break;
1303145247Sdamien
1304145247Sdamien		default:
1305172567Sthompsa			device_printf(sc->sc_dev, "unexpected status code %u\n",
1306145247Sdamien			    le16toh(status->code));
1307145247Sdamien		}
1308145247Sdamien
1309145247Sdamien		/* firmware was killed, stop processing received frames */
1310145247Sdamien		if (!(sc->flags & IPW_FLAG_FW_INITED))
1311145247Sdamien			return;
1312145247Sdamien
1313145247Sdamien		sbd->bd->flags = 0;
1314145247Sdamien	}
1315145247Sdamien
1316145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1317145247Sdamien
1318145247Sdamien	/* kick the firmware */
1319145247Sdamien	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1320145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1321145247Sdamien}
1322145247Sdamien
1323145247Sdamienstatic void
1324145247Sdamienipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1325145247Sdamien{
1326145247Sdamien	struct ipw_soft_hdr *shdr;
1327145247Sdamien	struct ipw_soft_buf *sbuf;
1328145247Sdamien
1329145247Sdamien	switch (sbd->type) {
1330145247Sdamien	case IPW_SBD_TYPE_COMMAND:
1331145247Sdamien		bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1332145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1333145247Sdamien		bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1334145247Sdamien		break;
1335145247Sdamien
1336145247Sdamien	case IPW_SBD_TYPE_HEADER:
1337145247Sdamien		shdr = sbd->priv;
1338145247Sdamien		bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1339145247Sdamien		bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1340145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1341145247Sdamien		break;
1342145247Sdamien
1343145247Sdamien	case IPW_SBD_TYPE_DATA:
1344145247Sdamien		sbuf = sbd->priv;
1345145247Sdamien		bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1346145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1347145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1348145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1349145247Sdamien
1350170530Ssam		if (sbuf->m->m_flags & M_TXCB)
1351170530Ssam			ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1352145247Sdamien		m_freem(sbuf->m);
1353145247Sdamien		ieee80211_free_node(sbuf->ni);
1354145247Sdamien
1355145247Sdamien		sc->sc_tx_timer = 0;
1356145247Sdamien		break;
1357145247Sdamien	}
1358145247Sdamien
1359145247Sdamien	sbd->type = IPW_SBD_TYPE_NOASSOC;
1360145247Sdamien}
1361145247Sdamien
1362145247Sdamienstatic void
1363145247Sdamienipw_tx_intr(struct ipw_softc *sc)
1364145247Sdamien{
1365286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
1366145247Sdamien	struct ipw_soft_bd *sbd;
1367145247Sdamien	uint32_t r, i;
1368145247Sdamien
1369145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1370145247Sdamien		return;
1371145247Sdamien
1372145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1373145247Sdamien
1374145247Sdamien	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1375145247Sdamien		sbd = &sc->stbd_list[i];
1376286437Sadrian
1377286437Sadrian		if (sbd->type == IPW_SBD_TYPE_DATA)
1378286437Sadrian			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1379286437Sadrian
1380145247Sdamien		ipw_release_sbd(sc, sbd);
1381145247Sdamien		sc->txfree++;
1382145247Sdamien	}
1383145247Sdamien
1384145247Sdamien	/* remember what the firmware has processed */
1385145247Sdamien	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1386145247Sdamien
1387286437Sadrian	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1388286437Sadrian	ipw_start_locked(ifp);
1389145247Sdamien}
1390145247Sdamien
1391145247Sdamienstatic void
1392191746Sthompsaipw_fatal_error_intr(struct ipw_softc *sc)
1393191746Sthompsa{
1394286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
1395286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1396191956Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1397191746Sthompsa
1398191746Sthompsa	device_printf(sc->sc_dev, "firmware error\n");
1399206767Sbschmidt	if (vap != NULL) {
1400206767Sbschmidt		IPW_UNLOCK(sc);
1401191956Sthompsa		ieee80211_cancel_scan(vap);
1402206767Sbschmidt		IPW_LOCK(sc);
1403206767Sbschmidt	}
1404191746Sthompsa	ieee80211_runtask(ic, &sc->sc_init_task);
1405191746Sthompsa}
1406191746Sthompsa
1407191746Sthompsastatic void
1408145247Sdamienipw_intr(void *arg)
1409145247Sdamien{
1410145247Sdamien	struct ipw_softc *sc = arg;
1411145247Sdamien	uint32_t r;
1412145247Sdamien
1413172567Sthompsa	IPW_LOCK(sc);
1414145247Sdamien
1415191746Sthompsa	r = CSR_READ_4(sc, IPW_CSR_INTR);
1416191746Sthompsa	if (r == 0 || r == 0xffffffff)
1417191746Sthompsa		goto done;
1418145247Sdamien
1419145247Sdamien	/* disable interrupts */
1420145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1421145247Sdamien
1422156599Sdamien	/* acknowledge all interrupts */
1423156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1424156599Sdamien
1425145247Sdamien	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1426191746Sthompsa		ipw_fatal_error_intr(sc);
1427191746Sthompsa		goto done;
1428145247Sdamien	}
1429145247Sdamien
1430156599Sdamien	if (r & IPW_INTR_FW_INIT_DONE)
1431156599Sdamien		wakeup(sc);
1432145247Sdamien
1433145247Sdamien	if (r & IPW_INTR_RX_TRANSFER)
1434145247Sdamien		ipw_rx_intr(sc);
1435145247Sdamien
1436145247Sdamien	if (r & IPW_INTR_TX_TRANSFER)
1437145247Sdamien		ipw_tx_intr(sc);
1438145247Sdamien
1439145247Sdamien	/* re-enable interrupts */
1440145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1441191746Sthompsadone:
1442172567Sthompsa	IPW_UNLOCK(sc);
1443145247Sdamien}
1444145247Sdamien
1445145247Sdamienstatic void
1446145247Sdamienipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1447145247Sdamien{
1448145247Sdamien	if (error != 0)
1449145247Sdamien		return;
1450145247Sdamien
1451145247Sdamien	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1452145247Sdamien
1453145247Sdamien	*(bus_addr_t *)arg = segs[0].ds_addr;
1454145247Sdamien}
1455145247Sdamien
1456172567Sthompsastatic const char *
1457172567Sthompsaipw_cmdname(int cmd)
1458172567Sthompsa{
1459172567Sthompsa#define	N(a)	(sizeof(a) / sizeof(a[0]))
1460172567Sthompsa	static const struct {
1461172567Sthompsa		int	cmd;
1462172567Sthompsa		const char *name;
1463172567Sthompsa	} cmds[] = {
1464172567Sthompsa		{ IPW_CMD_ADD_MULTICAST,	"ADD_MULTICAST" },
1465172567Sthompsa		{ IPW_CMD_BROADCAST_SCAN,	"BROADCAST_SCAN" },
1466172567Sthompsa		{ IPW_CMD_DISABLE,		"DISABLE" },
1467172567Sthompsa		{ IPW_CMD_DISABLE_PHY,		"DISABLE_PHY" },
1468172567Sthompsa		{ IPW_CMD_ENABLE,		"ENABLE" },
1469172567Sthompsa		{ IPW_CMD_PREPARE_POWER_DOWN,	"PREPARE_POWER_DOWN" },
1470172567Sthompsa		{ IPW_CMD_SET_BASIC_TX_RATES,	"SET_BASIC_TX_RATES" },
1471172567Sthompsa		{ IPW_CMD_SET_BEACON_INTERVAL,	"SET_BEACON_INTERVAL" },
1472172567Sthompsa		{ IPW_CMD_SET_CHANNEL,		"SET_CHANNEL" },
1473172567Sthompsa		{ IPW_CMD_SET_CONFIGURATION,	"SET_CONFIGURATION" },
1474172567Sthompsa		{ IPW_CMD_SET_DESIRED_BSSID,	"SET_DESIRED_BSSID" },
1475172567Sthompsa		{ IPW_CMD_SET_ESSID,		"SET_ESSID" },
1476172567Sthompsa		{ IPW_CMD_SET_FRAG_THRESHOLD,	"SET_FRAG_THRESHOLD" },
1477172567Sthompsa		{ IPW_CMD_SET_MAC_ADDRESS,	"SET_MAC_ADDRESS" },
1478172567Sthompsa		{ IPW_CMD_SET_MANDATORY_BSSID,	"SET_MANDATORY_BSSID" },
1479172567Sthompsa		{ IPW_CMD_SET_MODE,		"SET_MODE" },
1480172567Sthompsa		{ IPW_CMD_SET_MSDU_TX_RATES,	"SET_MSDU_TX_RATES" },
1481172567Sthompsa		{ IPW_CMD_SET_POWER_MODE,	"SET_POWER_MODE" },
1482172567Sthompsa		{ IPW_CMD_SET_RTS_THRESHOLD,	"SET_RTS_THRESHOLD" },
1483172567Sthompsa		{ IPW_CMD_SET_SCAN_OPTIONS,	"SET_SCAN_OPTIONS" },
1484172567Sthompsa		{ IPW_CMD_SET_SECURITY_INFO,	"SET_SECURITY_INFO" },
1485172567Sthompsa		{ IPW_CMD_SET_TX_POWER_INDEX,	"SET_TX_POWER_INDEX" },
1486172567Sthompsa		{ IPW_CMD_SET_TX_RATES,		"SET_TX_RATES" },
1487172567Sthompsa		{ IPW_CMD_SET_WEP_FLAGS,	"SET_WEP_FLAGS" },
1488172567Sthompsa		{ IPW_CMD_SET_WEP_KEY,		"SET_WEP_KEY" },
1489172567Sthompsa		{ IPW_CMD_SET_WEP_KEY_INDEX,	"SET_WEP_KEY_INDEX" },
1490172567Sthompsa		{ IPW_CMD_SET_WPA_IE,		"SET_WPA_IE" },
1491172567Sthompsa
1492172567Sthompsa	};
1493172567Sthompsa	static char buf[12];
1494172567Sthompsa	int i;
1495172567Sthompsa
1496172567Sthompsa	for (i = 0; i < N(cmds); i++)
1497172567Sthompsa		if (cmds[i].cmd == cmd)
1498172567Sthompsa			return cmds[i].name;
1499172567Sthompsa	snprintf(buf, sizeof(buf), "%u", cmd);
1500172567Sthompsa	return buf;
1501172567Sthompsa#undef N
1502172567Sthompsa}
1503172567Sthompsa
1504145247Sdamien/*
1505145247Sdamien * Send a command to the firmware and wait for the acknowledgement.
1506145247Sdamien */
1507145247Sdamienstatic int
1508145247Sdamienipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1509145247Sdamien{
1510145247Sdamien	struct ipw_soft_bd *sbd;
1511145247Sdamien	bus_addr_t physaddr;
1512145247Sdamien	int error;
1513145247Sdamien
1514178354Ssam	IPW_LOCK_ASSERT(sc);
1515178354Ssam
1516172567Sthompsa	if (sc->flags & IPW_FLAG_BUSY) {
1517172567Sthompsa		device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1518172567Sthompsa			__func__, ipw_cmdname(type));
1519172567Sthompsa		return EAGAIN;
1520172567Sthompsa	}
1521172567Sthompsa	sc->flags |= IPW_FLAG_BUSY;
1522172567Sthompsa
1523145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1524145247Sdamien
1525145247Sdamien	error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1526145247Sdamien	    sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1527145247Sdamien	if (error != 0) {
1528145247Sdamien		device_printf(sc->sc_dev, "could not map command DMA memory\n");
1529172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1530145247Sdamien		return error;
1531145247Sdamien	}
1532145247Sdamien
1533145247Sdamien	sc->cmd.type = htole32(type);
1534145247Sdamien	sc->cmd.subtype = 0;
1535145247Sdamien	sc->cmd.len = htole32(len);
1536145247Sdamien	sc->cmd.seq = 0;
1537152637Sdamien	memcpy(sc->cmd.data, data, len);
1538145247Sdamien
1539145247Sdamien	sbd->type = IPW_SBD_TYPE_COMMAND;
1540145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1541145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1542145247Sdamien	sbd->bd->nfrag = 1;
1543145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1544145247Sdamien	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1545145247Sdamien
1546145247Sdamien	bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1547145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1548145247Sdamien
1549172567Sthompsa#ifdef IPW_DEBUG
1550172567Sthompsa	if (ipw_debug >= 4) {
1551172567Sthompsa		printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1552172567Sthompsa		    0, 0, len);
1553172567Sthompsa		/* Print the data buffer in the higher debug level */
1554172567Sthompsa		if (ipw_debug >= 9 && len > 0) {
1555172567Sthompsa			printf(" data: 0x");
1556172567Sthompsa			for (int i = 1; i <= len; i++)
1557174269Swkoszek				printf("%1D", (u_char *)data + len - i, "");
1558172567Sthompsa		}
1559172567Sthompsa		printf("\n");
1560172567Sthompsa	}
1561172567Sthompsa#endif
1562145247Sdamien
1563145247Sdamien	/* kick firmware */
1564145247Sdamien	sc->txfree--;
1565145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1566145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1567145247Sdamien
1568145247Sdamien	/* wait at most one second for command to complete */
1569172567Sthompsa	error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1570172567Sthompsa	if (error != 0) {
1571172567Sthompsa		device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1572172567Sthompsa		    __func__, ipw_cmdname(type), error);
1573172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1574172567Sthompsa		return (error);
1575172567Sthompsa	}
1576172567Sthompsa	return (0);
1577145247Sdamien}
1578145247Sdamien
1579145247Sdamienstatic int
1580286437Sadrianipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1581145247Sdamien{
1582286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1583286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
1584192468Ssam	struct ieee80211vap *vap = ni->ni_vap;
1585145247Sdamien	struct ieee80211_frame *wh;
1586145247Sdamien	struct ipw_soft_bd *sbd;
1587145247Sdamien	struct ipw_soft_hdr *shdr;
1588145247Sdamien	struct ipw_soft_buf *sbuf;
1589145247Sdamien	struct ieee80211_key *k;
1590145247Sdamien	struct mbuf *mnew;
1591145247Sdamien	bus_dma_segment_t segs[IPW_MAX_NSEG];
1592145247Sdamien	bus_addr_t physaddr;
1593145247Sdamien	int nsegs, error, i;
1594145247Sdamien
1595145247Sdamien	wh = mtod(m0, struct ieee80211_frame *);
1596145247Sdamien
1597260444Skevlo	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1598178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1599147806Ssam		if (k == NULL) {
1600147806Ssam			m_freem(m0);
1601145247Sdamien			return ENOBUFS;
1602147806Ssam		}
1603145247Sdamien		/* packet header may have moved, reset our local pointer */
1604145247Sdamien		wh = mtod(m0, struct ieee80211_frame *);
1605145247Sdamien	}
1606145247Sdamien
1607192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1608145247Sdamien		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1609145247Sdamien
1610145247Sdamien		tap->wt_flags = 0;
1611145247Sdamien
1612192468Ssam		ieee80211_radiotap_tx(vap, m0);
1613145247Sdamien	}
1614145247Sdamien
1615145247Sdamien	shdr = SLIST_FIRST(&sc->free_shdr);
1616145247Sdamien	sbuf = SLIST_FIRST(&sc->free_sbuf);
1617145247Sdamien	KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1618145247Sdamien
1619145247Sdamien	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1620145247Sdamien	shdr->hdr.subtype = 0;
1621260444Skevlo	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) ? 1 : 0;
1622145247Sdamien	shdr->hdr.encrypt = 0;
1623145247Sdamien	shdr->hdr.keyidx = 0;
1624145247Sdamien	shdr->hdr.keysz = 0;
1625145247Sdamien	shdr->hdr.fragmentsz = 0;
1626145247Sdamien	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1627145247Sdamien	if (ic->ic_opmode == IEEE80211_M_STA)
1628145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1629145247Sdamien	else
1630145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1631145247Sdamien
1632145247Sdamien	/* trim IEEE802.11 header */
1633145247Sdamien	m_adj(m0, sizeof (struct ieee80211_frame));
1634145247Sdamien
1635145247Sdamien	error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1636145247Sdamien	    &nsegs, 0);
1637145247Sdamien	if (error != 0 && error != EFBIG) {
1638145247Sdamien		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1639145247Sdamien		    error);
1640145247Sdamien		m_freem(m0);
1641145247Sdamien		return error;
1642145247Sdamien	}
1643145247Sdamien	if (error != 0) {
1644243857Sglebius		mnew = m_defrag(m0, M_NOWAIT);
1645145247Sdamien		if (mnew == NULL) {
1646145247Sdamien			device_printf(sc->sc_dev,
1647145247Sdamien			    "could not defragment mbuf\n");
1648145247Sdamien			m_freem(m0);
1649145247Sdamien			return ENOBUFS;
1650145247Sdamien		}
1651145247Sdamien		m0 = mnew;
1652145247Sdamien
1653145247Sdamien		error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1654145247Sdamien		    segs, &nsegs, 0);
1655145247Sdamien		if (error != 0) {
1656145247Sdamien			device_printf(sc->sc_dev,
1657145247Sdamien			    "could not map mbuf (error %d)\n", error);
1658145247Sdamien			m_freem(m0);
1659145247Sdamien			return error;
1660145247Sdamien		}
1661145247Sdamien	}
1662145247Sdamien
1663145247Sdamien	error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1664145247Sdamien	    sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1665145247Sdamien	if (error != 0) {
1666145247Sdamien		device_printf(sc->sc_dev, "could not map header DMA memory\n");
1667145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1668145247Sdamien		m_freem(m0);
1669145247Sdamien		return error;
1670145247Sdamien	}
1671145247Sdamien
1672145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1673145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1674145247Sdamien
1675145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1676145247Sdamien	sbd->type = IPW_SBD_TYPE_HEADER;
1677145247Sdamien	sbd->priv = shdr;
1678145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1679145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1680145247Sdamien	sbd->bd->nfrag = 1 + nsegs;
1681145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1682145247Sdamien	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1683145247Sdamien
1684145247Sdamien	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1685145247Sdamien	    shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1686145247Sdamien	    shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1687145247Sdamien	    ":"));
1688145247Sdamien
1689145247Sdamien	sc->txfree--;
1690145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1691145247Sdamien
1692145247Sdamien	sbuf->m = m0;
1693145247Sdamien	sbuf->ni = ni;
1694145247Sdamien
1695145247Sdamien	for (i = 0; i < nsegs; i++) {
1696145247Sdamien		sbd = &sc->stbd_list[sc->txcur];
1697145247Sdamien
1698145247Sdamien		sbd->bd->physaddr = htole32(segs[i].ds_addr);
1699145247Sdamien		sbd->bd->len = htole32(segs[i].ds_len);
1700145247Sdamien		sbd->bd->nfrag = 0;
1701145247Sdamien		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1702145247Sdamien		if (i == nsegs - 1) {
1703145247Sdamien			sbd->type = IPW_SBD_TYPE_DATA;
1704145247Sdamien			sbd->priv = sbuf;
1705145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1706145247Sdamien		} else {
1707145247Sdamien			sbd->type = IPW_SBD_TYPE_NOASSOC;
1708145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1709145247Sdamien		}
1710145247Sdamien
1711172574Sthompsa		DPRINTFN(5, ("sending fragment (%d)\n", i));
1712145247Sdamien
1713145247Sdamien		sc->txfree--;
1714145247Sdamien		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1715145247Sdamien	}
1716145247Sdamien
1717145247Sdamien	bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1718145247Sdamien	bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1719145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1720145247Sdamien
1721145247Sdamien	/* kick firmware */
1722145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1723145247Sdamien
1724145247Sdamien	return 0;
1725145247Sdamien}
1726145247Sdamien
1727178354Ssamstatic int
1728178354Ssamipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1729178354Ssam	const struct ieee80211_bpf_params *params)
1730178354Ssam{
1731178354Ssam	/* no support; just discard */
1732178354Ssam	m_freem(m);
1733178354Ssam	ieee80211_free_node(ni);
1734178354Ssam	return 0;
1735178354Ssam}
1736178354Ssam
1737286437Sadrianstatic void
1738286437Sadrianipw_start(struct ifnet *ifp)
1739145247Sdamien{
1740286437Sadrian	struct ipw_softc *sc = ifp->if_softc;
1741172567Sthompsa
1742172567Sthompsa	IPW_LOCK(sc);
1743286437Sadrian	ipw_start_locked(ifp);
1744172567Sthompsa	IPW_UNLOCK(sc);
1745172567Sthompsa}
1746172567Sthompsa
1747172567Sthompsastatic void
1748286437Sadrianipw_start_locked(struct ifnet *ifp)
1749172567Sthompsa{
1750286437Sadrian	struct ipw_softc *sc = ifp->if_softc;
1751145247Sdamien	struct ieee80211_node *ni;
1752178354Ssam	struct mbuf *m;
1753145247Sdamien
1754172567Sthompsa	IPW_LOCK_ASSERT(sc);
1755145247Sdamien
1756286437Sadrian	for (;;) {
1757286437Sadrian		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1758286437Sadrian		if (m == NULL)
1759286437Sadrian			break;
1760286437Sadrian		if (sc->txfree < 1 + IPW_MAX_NSEG) {
1761286437Sadrian			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1762286437Sadrian			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1763286437Sadrian			break;
1764286437Sadrian		}
1765178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1766286437Sadrian		if (ipw_tx_start(ifp, m, ni) != 0) {
1767145247Sdamien			ieee80211_free_node(ni);
1768286437Sadrian			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1769145247Sdamien			break;
1770145247Sdamien		}
1771145247Sdamien		/* start watchdog timer */
1772145247Sdamien		sc->sc_tx_timer = 5;
1773145247Sdamien	}
1774145247Sdamien}
1775145247Sdamien
1776145247Sdamienstatic void
1777172567Sthompsaipw_watchdog(void *arg)
1778145247Sdamien{
1779172567Sthompsa	struct ipw_softc *sc = arg;
1780286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
1781286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1782145247Sdamien
1783172567Sthompsa	IPW_LOCK_ASSERT(sc);
1784156599Sdamien
1785145247Sdamien	if (sc->sc_tx_timer > 0) {
1786145247Sdamien		if (--sc->sc_tx_timer == 0) {
1787286437Sadrian			if_printf(ifp, "device timeout\n");
1788286437Sadrian			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1789178354Ssam			taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1790145247Sdamien		}
1791145247Sdamien	}
1792172567Sthompsa	if (sc->sc_scan_timer > 0) {
1793172567Sthompsa		if (--sc->sc_scan_timer == 0) {
1794172567Sthompsa			DPRINTFN(3, ("Scan timeout\n"));
1795172567Sthompsa			/* End the scan */
1796172567Sthompsa			if (sc->flags & IPW_FLAG_SCANNING) {
1797206767Sbschmidt				IPW_UNLOCK(sc);
1798178354Ssam				ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1799206767Sbschmidt				IPW_LOCK(sc);
1800172567Sthompsa				sc->flags &= ~IPW_FLAG_SCANNING;
1801172567Sthompsa			}
1802172567Sthompsa		}
1803172567Sthompsa	}
1804286437Sadrian	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1805172567Sthompsa		callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1806145247Sdamien}
1807145247Sdamien
1808286437Sadrianstatic int
1809286437Sadrianipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1810145247Sdamien{
1811286437Sadrian	struct ipw_softc *sc = ifp->if_softc;
1812286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
1813286437Sadrian	struct ifreq *ifr = (struct ifreq *) data;
1814286437Sadrian	int error = 0, startall = 0;
1815145247Sdamien
1816286437Sadrian	switch (cmd) {
1817286437Sadrian	case SIOCSIFFLAGS:
1818286437Sadrian		IPW_LOCK(sc);
1819286437Sadrian		if (ifp->if_flags & IFF_UP) {
1820286437Sadrian			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1821286437Sadrian				ipw_init_locked(sc);
1822286437Sadrian				startall = 1;
1823286437Sadrian			}
1824286437Sadrian		} else {
1825286437Sadrian			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1826286437Sadrian				ipw_stop_locked(sc);
1827145247Sdamien		}
1828286437Sadrian		IPW_UNLOCK(sc);
1829286437Sadrian		if (startall)
1830286437Sadrian			ieee80211_start_all(ic);
1831286437Sadrian		break;
1832286437Sadrian	case SIOCGIFMEDIA:
1833286437Sadrian		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1834286437Sadrian		break;
1835286437Sadrian	case SIOCGIFADDR:
1836286437Sadrian		error = ether_ioctl(ifp, cmd, data);
1837286437Sadrian		break;
1838286437Sadrian	default:
1839286437Sadrian		error = EINVAL;
1840286437Sadrian		break;
1841286437Sadrian	}
1842286437Sadrian	return error;
1843145247Sdamien}
1844145247Sdamien
1845145247Sdamienstatic void
1846145247Sdamienipw_stop_master(struct ipw_softc *sc)
1847145247Sdamien{
1848156599Sdamien	uint32_t tmp;
1849145247Sdamien	int ntries;
1850145247Sdamien
1851145247Sdamien	/* disable interrupts */
1852145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1853145247Sdamien
1854145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1855145247Sdamien	for (ntries = 0; ntries < 50; ntries++) {
1856145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1857145247Sdamien			break;
1858145247Sdamien		DELAY(10);
1859145247Sdamien	}
1860145247Sdamien	if (ntries == 50)
1861145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for master\n");
1862145247Sdamien
1863156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1864156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1865145247Sdamien
1866172567Sthompsa	/* Clear all flags except the following */
1867172567Sthompsa	sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1868145247Sdamien}
1869145247Sdamien
1870145247Sdamienstatic int
1871145247Sdamienipw_reset(struct ipw_softc *sc)
1872145247Sdamien{
1873156599Sdamien	uint32_t tmp;
1874145247Sdamien	int ntries;
1875145247Sdamien
1876145247Sdamien	ipw_stop_master(sc);
1877145247Sdamien
1878145247Sdamien	/* move adapter to D0 state */
1879156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1880156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1881145247Sdamien
1882145247Sdamien	/* wait for clock stabilization */
1883145247Sdamien	for (ntries = 0; ntries < 1000; ntries++) {
1884145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1885145247Sdamien			break;
1886145247Sdamien		DELAY(200);
1887145247Sdamien	}
1888145247Sdamien	if (ntries == 1000)
1889145247Sdamien		return EIO;
1890145247Sdamien
1891156599Sdamien	tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1892156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1893145247Sdamien
1894145247Sdamien	DELAY(10);
1895145247Sdamien
1896156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1897156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1898145247Sdamien
1899145247Sdamien	return 0;
1900145247Sdamien}
1901145247Sdamien
1902172567Sthompsastatic int
1903172567Sthompsaipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1904172567Sthompsa{
1905172567Sthompsa	int ms = hz < 1000 ? 1 : hz/10;
1906172567Sthompsa	int i, error;
1907172567Sthompsa
1908172567Sthompsa	for (i = 0; i < 100; i++) {
1909172567Sthompsa		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1910172567Sthompsa			return 0;
1911172567Sthompsa		error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1912172567Sthompsa		if (error == 0 || error != EWOULDBLOCK)
1913172567Sthompsa			return 0;
1914172567Sthompsa	}
1915172567Sthompsa	DPRINTF(("%s: timeout waiting for %s\n",
1916172567Sthompsa		__func__, waitfor ? "disable" : "enable"));
1917172567Sthompsa	return ETIMEDOUT;
1918172567Sthompsa}
1919172567Sthompsa
1920172567Sthompsastatic int
1921172567Sthompsaipw_enable(struct ipw_softc *sc)
1922172567Sthompsa{
1923172567Sthompsa	int error;
1924172567Sthompsa
1925172567Sthompsa	if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1926172567Sthompsa		DPRINTF(("Enable adapter\n"));
1927172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1928172567Sthompsa		if (error != 0)
1929172567Sthompsa			return error;
1930172567Sthompsa		error = ipw_waitfordisable(sc, 0);
1931172567Sthompsa		if (error != 0)
1932172567Sthompsa			return error;
1933172567Sthompsa		sc->flags |= IPW_FLAG_ENABLED;
1934172567Sthompsa	}
1935172567Sthompsa	return 0;
1936172567Sthompsa}
1937172567Sthompsa
1938172567Sthompsastatic int
1939172567Sthompsaipw_disable(struct ipw_softc *sc)
1940172567Sthompsa{
1941172567Sthompsa	int error;
1942172567Sthompsa
1943172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
1944172567Sthompsa		DPRINTF(("Disable adapter\n"));
1945172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1946172567Sthompsa		if (error != 0)
1947172567Sthompsa			return error;
1948172567Sthompsa		error = ipw_waitfordisable(sc, 1);
1949172567Sthompsa		if (error != 0)
1950172567Sthompsa			return error;
1951172567Sthompsa		sc->flags &= ~IPW_FLAG_ENABLED;
1952172567Sthompsa	}
1953172567Sthompsa	return 0;
1954172567Sthompsa}
1955172567Sthompsa
1956145247Sdamien/*
1957145247Sdamien * Upload the microcode to the device.
1958145247Sdamien */
1959145247Sdamienstatic int
1960156599Sdamienipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1961145247Sdamien{
1962145247Sdamien	int ntries;
1963145247Sdamien
1964145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1965145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1966145247Sdamien
1967145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1968145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1969145247Sdamien
1970145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1971145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1972145247Sdamien
1973145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1974145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1975145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1976145247Sdamien
1977145247Sdamien	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1978145247Sdamien
1979145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1980145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1981145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1982145247Sdamien
1983145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1984145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1985145247Sdamien
1986145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1987145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1988145247Sdamien
1989145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1990145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1991145247Sdamien
1992145247Sdamien	for (ntries = 0; ntries < 10; ntries++) {
1993145247Sdamien		if (MEM_READ_1(sc, 0x210000) & 1)
1994145247Sdamien			break;
1995145247Sdamien		DELAY(10);
1996145247Sdamien	}
1997145247Sdamien	if (ntries == 10) {
1998145247Sdamien		device_printf(sc->sc_dev,
1999145247Sdamien		    "timeout waiting for ucode to initialize\n");
2000145247Sdamien		return EIO;
2001145247Sdamien	}
2002145247Sdamien
2003145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0);
2004145247Sdamien
2005145247Sdamien	return 0;
2006145247Sdamien}
2007145247Sdamien
2008145247Sdamien/* set of macros to handle unaligned little endian data in firmware image */
2009145247Sdamien#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2010145247Sdamien#define GETLE16(p) ((p)[0] | (p)[1] << 8)
2011145247Sdamienstatic int
2012156599Sdamienipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
2013145247Sdamien{
2014156599Sdamien	const uint8_t *p, *end;
2015156599Sdamien	uint32_t tmp, dst;
2016145247Sdamien	uint16_t len;
2017145247Sdamien	int error;
2018145247Sdamien
2019145247Sdamien	p = fw;
2020145247Sdamien	end = fw + size;
2021145247Sdamien	while (p < end) {
2022145247Sdamien		dst = GETLE32(p); p += 4;
2023145247Sdamien		len = GETLE16(p); p += 2;
2024145247Sdamien
2025145247Sdamien		ipw_write_mem_1(sc, dst, p, len);
2026145247Sdamien		p += len;
2027145247Sdamien	}
2028145247Sdamien
2029145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
2030145247Sdamien	    IPW_IO_LED_OFF);
2031145247Sdamien
2032145247Sdamien	/* enable interrupts */
2033145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
2034145247Sdamien
2035145247Sdamien	/* kick the firmware */
2036145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
2037145247Sdamien
2038156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
2039156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
2040145247Sdamien
2041145247Sdamien	/* wait at most one second for firmware initialization to complete */
2042145247Sdamien	if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
2043145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for firmware "
2044145247Sdamien		    "initialization to complete\n");
2045145247Sdamien		return error;
2046145247Sdamien	}
2047145247Sdamien
2048156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_IO);
2049156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2050156599Sdamien	    IPW_IO_GPIO3_MASK);
2051145247Sdamien
2052145247Sdamien	return 0;
2053145247Sdamien}
2054145247Sdamien
2055145247Sdamienstatic int
2056172567Sthompsaipw_setwepkeys(struct ipw_softc *sc)
2057172567Sthompsa{
2058286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
2059286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
2060178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2061172567Sthompsa	struct ipw_wep_key wepkey;
2062172567Sthompsa	struct ieee80211_key *wk;
2063172567Sthompsa	int error, i;
2064172567Sthompsa
2065172567Sthompsa	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2066178354Ssam		wk = &vap->iv_nw_keys[i];
2067172567Sthompsa
2068172567Sthompsa		if (wk->wk_cipher == NULL ||
2069172567Sthompsa		    wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2070172567Sthompsa			continue;
2071172567Sthompsa
2072172567Sthompsa		wepkey.idx = i;
2073172567Sthompsa		wepkey.len = wk->wk_keylen;
2074172567Sthompsa		memset(wepkey.key, 0, sizeof wepkey.key);
2075172567Sthompsa		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2076172567Sthompsa		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2077172567Sthompsa		    wepkey.len));
2078172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2079172567Sthompsa		    sizeof wepkey);
2080172567Sthompsa		if (error != 0)
2081172567Sthompsa			return error;
2082172567Sthompsa	}
2083172567Sthompsa	return 0;
2084172567Sthompsa}
2085172567Sthompsa
2086172567Sthompsastatic int
2087172567Sthompsaipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2088172567Sthompsa{
2089172567Sthompsa	struct ipw_wpa_ie wpaie;
2090172567Sthompsa
2091172567Sthompsa	memset(&wpaie, 0, sizeof(wpaie));
2092172567Sthompsa	wpaie.len = htole32(ielen);
2093172567Sthompsa	/* XXX verify length */
2094172567Sthompsa	memcpy(&wpaie.ie, ie, ielen);
2095172567Sthompsa	DPRINTF(("Setting WPA IE\n"));
2096172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2097172567Sthompsa}
2098172567Sthompsa
2099172567Sthompsastatic int
2100172567Sthompsaipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2101172567Sthompsa{
2102172567Sthompsa	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2103172567Sthompsa
2104172567Sthompsa	if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2105172567Sthompsa		DPRINTF(("Setting mandatory BSSID to null\n"));
2106172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2107172567Sthompsa	} else {
2108172567Sthompsa		DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2109172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2110172567Sthompsa			bssid, IEEE80211_ADDR_LEN);
2111172567Sthompsa	}
2112172567Sthompsa}
2113172567Sthompsa
2114172567Sthompsastatic int
2115172567Sthompsaipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2116172567Sthompsa{
2117172567Sthompsa	if (ssidlen == 0) {
2118172567Sthompsa		/*
2119172567Sthompsa		 * A bug in the firmware breaks the ``don't associate''
2120172567Sthompsa		 * bit in the scan options command.  To compensate for
2121172567Sthompsa		 * this install a bogus ssid when no ssid is specified
2122172567Sthompsa		 * so the firmware won't try to associate.
2123172567Sthompsa		 */
2124172567Sthompsa		DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2125172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2126172567Sthompsa			"\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2127172567Sthompsa			"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2128172567Sthompsa			"\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2129172567Sthompsa			"\x3c\x3d", IEEE80211_NWID_LEN);
2130172567Sthompsa	} else {
2131172567Sthompsa#ifdef IPW_DEBUG
2132172567Sthompsa		if (ipw_debug > 0) {
2133172567Sthompsa			printf("Setting ESSID to ");
2134172567Sthompsa			ieee80211_print_essid(ssid, ssidlen);
2135172567Sthompsa			printf("\n");
2136172567Sthompsa		}
2137172567Sthompsa#endif
2138172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2139172567Sthompsa	}
2140172567Sthompsa}
2141172567Sthompsa
2142172567Sthompsastatic int
2143172567Sthompsaipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2144172567Sthompsa{
2145172567Sthompsa	struct ipw_scan_options opts;
2146172567Sthompsa
2147172567Sthompsa	DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2148172567Sthompsa	opts.channels = htole32(chanmask);
2149172567Sthompsa	opts.flags = htole32(flags);
2150172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2151172567Sthompsa}
2152172567Sthompsa
2153172567Sthompsastatic int
2154172567Sthompsaipw_scan(struct ipw_softc *sc)
2155172567Sthompsa{
2156172567Sthompsa	uint32_t params;
2157172567Sthompsa	int error;
2158172567Sthompsa
2159172567Sthompsa	DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2160172567Sthompsa
2161172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
2162172567Sthompsa		return (EBUSY);
2163172567Sthompsa	sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2164172567Sthompsa
2165172567Sthompsa	/* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2166172567Sthompsa	error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2167172567Sthompsa	if (error != 0)
2168172567Sthompsa		goto done;
2169172567Sthompsa
2170172567Sthompsa	/*
2171172567Sthompsa	 * Setup null/bogus ssid so firmware doesn't use any previous
2172172567Sthompsa	 * ssid to try and associate.  This is because the ``don't
2173172567Sthompsa	 * associate'' option bit is broken (sigh).
2174172567Sthompsa	 */
2175172567Sthompsa	error = ipw_setssid(sc, NULL, 0);
2176172567Sthompsa	if (error != 0)
2177172567Sthompsa		goto done;
2178172567Sthompsa
2179172567Sthompsa	/*
2180172567Sthompsa	 * NB: the adapter may be disabled on association lost;
2181172567Sthompsa	 *     if so just re-enable it to kick off scanning.
2182172567Sthompsa	 */
2183172567Sthompsa	DPRINTF(("Starting scan\n"));
2184172567Sthompsa	sc->sc_scan_timer = 3;
2185172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
2186172567Sthompsa		params = 0;				/* XXX? */
2187172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2188172567Sthompsa				&params, sizeof(params));
2189172567Sthompsa	} else
2190172567Sthompsa		error = ipw_enable(sc);
2191172567Sthompsadone:
2192172567Sthompsa	if (error != 0) {
2193172567Sthompsa		DPRINTF(("Scan failed\n"));
2194172567Sthompsa		sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2195172567Sthompsa	}
2196172567Sthompsa	return (error);
2197172567Sthompsa}
2198172567Sthompsa
2199172567Sthompsastatic int
2200172567Sthompsaipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2201172567Sthompsa{
2202286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
2203286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
2204172567Sthompsa	uint32_t data;
2205172567Sthompsa	int error;
2206172567Sthompsa
2207172567Sthompsa	data = htole32(ieee80211_chan2ieee(ic, chan));
2208172567Sthompsa	DPRINTF(("Setting channel to %u\n", le32toh(data)));
2209172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2210172567Sthompsa	if (error == 0)
2211172567Sthompsa		ipw_setcurchan(sc, chan);
2212172567Sthompsa	return error;
2213172567Sthompsa}
2214172567Sthompsa
2215178354Ssamstatic void
2216191746Sthompsaipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2217145247Sdamien{
2218286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2219178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2220145247Sdamien	struct ipw_security security;
2221145247Sdamien	uint32_t data;
2222172567Sthompsa	int error;
2223145247Sdamien
2224178354Ssam	IPW_LOCK(sc);
2225172567Sthompsa	error = ipw_disable(sc);
2226172567Sthompsa	if (error != 0)
2227178354Ssam		goto done;
2228172567Sthompsa
2229152637Sdamien	memset(&security, 0, sizeof security);
2230178354Ssam	security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2231145247Sdamien	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2232145247Sdamien	security.ciphers = htole32(IPW_CIPHER_NONE);
2233145247Sdamien	DPRINTF(("Setting authmode to %u\n", security.authmode));
2234172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2235145247Sdamien	    sizeof security);
2236145247Sdamien	if (error != 0)
2237178354Ssam		goto done;
2238145247Sdamien
2239178354Ssam	data = htole32(vap->iv_rtsthreshold);
2240178354Ssam	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2241178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2242145247Sdamien	if (error != 0)
2243178354Ssam		goto done;
2244145247Sdamien
2245178354Ssam	data = htole32(vap->iv_fragthreshold);
2246178354Ssam	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2247178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2248145247Sdamien	if (error != 0)
2249178354Ssam		goto done;
2250145247Sdamien
2251178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2252172567Sthompsa		error = ipw_setwepkeys(sc);
2253172567Sthompsa		if (error != 0)
2254178354Ssam			goto done;
2255172567Sthompsa
2256178354Ssam		if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2257178354Ssam			data = htole32(vap->iv_def_txkey);
2258172567Sthompsa			DPRINTF(("Setting wep tx key index to %u\n",
2259172567Sthompsa				le32toh(data)));
2260172567Sthompsa			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2261172567Sthompsa			    sizeof data);
2262172567Sthompsa			if (error != 0)
2263178354Ssam				goto done;
2264172567Sthompsa		}
2265172567Sthompsa	}
2266172567Sthompsa
2267178354Ssam	data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2268172567Sthompsa	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2269172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2270172567Sthompsa	if (error != 0)
2271178354Ssam		goto done;
2272172567Sthompsa
2273172567Sthompsa	error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2274172567Sthompsa	if (error != 0)
2275178354Ssam		goto done;
2276172567Sthompsa
2277172567Sthompsa	error = ipw_setbssid(sc, ni->ni_bssid);
2278172567Sthompsa	if (error != 0)
2279178354Ssam		goto done;
2280172567Sthompsa
2281206766Sbschmidt	if (vap->iv_appie_wpa != NULL) {
2282206766Sbschmidt		struct ieee80211_appie *ie = vap->iv_appie_wpa;
2283178354Ssam		error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2284172567Sthompsa		if (error != 0)
2285178354Ssam			goto done;
2286172567Sthompsa	}
2287172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2288172567Sthompsa		error = ipw_setchannel(sc, ni->ni_chan);
2289172567Sthompsa		if (error != 0)
2290178354Ssam			goto done;
2291172567Sthompsa	}
2292172567Sthompsa
2293172567Sthompsa	/* lock scan to ap's channel and enable associate */
2294172567Sthompsa	error = ipw_setscanopts(sc,
2295178354Ssam	    1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2296178354Ssam	if (error != 0)
2297178354Ssam		goto done;
2298172567Sthompsa
2299178354Ssam	error = ipw_enable(sc);		/* finally, enable adapter */
2300178354Ssam	if (error == 0)
2301178354Ssam		sc->flags |= IPW_FLAG_ASSOCIATING;
2302178354Ssamdone:
2303178354Ssam	IPW_UNLOCK(sc);
2304172567Sthompsa}
2305172567Sthompsa
2306172567Sthompsastatic void
2307191746Sthompsaipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2308172567Sthompsa{
2309178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2310286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2311172567Sthompsa
2312172567Sthompsa	IPW_LOCK(sc);
2313172567Sthompsa	DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2314172567Sthompsa	/*
2315172567Sthompsa	 * NB: don't try to do this if ipw_stop_master has
2316172567Sthompsa	 *     shutdown the firmware and disabled interrupts.
2317172567Sthompsa	 */
2318178354Ssam	if (sc->flags & IPW_FLAG_FW_INITED) {
2319178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATED;
2320178354Ssam		/*
2321178354Ssam		 * NB: firmware currently ignores bssid parameter, but
2322178354Ssam		 *     supply it in case this changes (follow linux driver).
2323178354Ssam		 */
2324178354Ssam		(void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2325178354Ssam			ni->ni_bssid, IEEE80211_ADDR_LEN);
2326178354Ssam	}
2327178354Ssam	IPW_UNLOCK(sc);
2328172567Sthompsa}
2329172567Sthompsa
2330172567Sthompsa/*
2331156599Sdamien * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2332156599Sdamien * It is called on firmware panics or on watchdog timeouts.
2333156599Sdamien */
2334145247Sdamienstatic void
2335156599Sdamienipw_init_task(void *context, int pending)
2336156599Sdamien{
2337156599Sdamien	ipw_init(context);
2338156599Sdamien}
2339156599Sdamien
2340156599Sdamienstatic void
2341145247Sdamienipw_init(void *priv)
2342145247Sdamien{
2343145247Sdamien	struct ipw_softc *sc = priv;
2344286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
2345286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
2346172567Sthompsa
2347172567Sthompsa	IPW_LOCK(sc);
2348178354Ssam	ipw_init_locked(sc);
2349172567Sthompsa	IPW_UNLOCK(sc);
2350178354Ssam
2351286437Sadrian	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2352178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2353172567Sthompsa}
2354172567Sthompsa
2355172567Sthompsastatic void
2356178354Ssamipw_init_locked(struct ipw_softc *sc)
2357172567Sthompsa{
2358286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
2359286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
2360178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2361166756Sluigi	const struct firmware *fp;
2362156599Sdamien	const struct ipw_firmware_hdr *hdr;
2363178354Ssam	const char *fw;
2364145247Sdamien
2365172567Sthompsa	IPW_LOCK_ASSERT(sc);
2366156599Sdamien
2367172567Sthompsa	DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2368178354Ssam		ieee80211_state_name[vap->iv_state], sc->flags));
2369172567Sthompsa
2370156599Sdamien	/*
2371156599Sdamien	 * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2372156599Sdamien	 * when loading the firmware and we don't want to be called during this
2373156599Sdamien	 * operation.
2374156599Sdamien	 */
2375172567Sthompsa	if (sc->flags & IPW_FLAG_INIT_LOCKED)
2376145247Sdamien		return;
2377156599Sdamien	sc->flags |= IPW_FLAG_INIT_LOCKED;
2378145247Sdamien
2379172567Sthompsa	ipw_stop_locked(sc);
2380145247Sdamien
2381145247Sdamien	if (ipw_reset(sc) != 0) {
2382145247Sdamien		device_printf(sc->sc_dev, "could not reset adapter\n");
2383178354Ssam		goto fail;
2384145247Sdamien	}
2385145247Sdamien
2386159487Siedowse	if (sc->sc_firmware == NULL) {
2387178354Ssam		device_printf(sc->sc_dev, "no firmware\n");
2388178354Ssam		goto fail;
2389156599Sdamien	}
2390178354Ssam	/* NB: consistency already checked on load */
2391159487Siedowse	fp = sc->sc_firmware;
2392156599Sdamien	hdr = (const struct ipw_firmware_hdr *)fp->data;
2393156599Sdamien
2394178354Ssam	DPRINTF(("Loading firmware image '%s'\n", fp->name));
2395156599Sdamien	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2396156599Sdamien	if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2397145247Sdamien		device_printf(sc->sc_dev, "could not load microcode\n");
2398178354Ssam		goto fail;
2399145247Sdamien	}
2400145247Sdamien
2401145247Sdamien	ipw_stop_master(sc);
2402145247Sdamien
2403145247Sdamien	/*
2404145247Sdamien	 * Setup tx, rx and status rings.
2405145247Sdamien	 */
2406145247Sdamien	sc->txold = IPW_NTBD - 1;
2407145247Sdamien	sc->txcur = 0;
2408145247Sdamien	sc->txfree = IPW_NTBD - 2;
2409145247Sdamien	sc->rxcur = IPW_NRBD - 1;
2410145247Sdamien
2411145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2412145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2413145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2414145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2415145247Sdamien
2416145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2417145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2418145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2419145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2420145247Sdamien
2421145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2422145247Sdamien
2423156599Sdamien	fw = (const char *)fp->data + sizeof *hdr;
2424156599Sdamien	if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2425145247Sdamien		device_printf(sc->sc_dev, "could not load firmware\n");
2426178354Ssam		goto fail;
2427145247Sdamien	}
2428145247Sdamien
2429145247Sdamien	sc->flags |= IPW_FLAG_FW_INITED;
2430145247Sdamien
2431145247Sdamien	/* retrieve information tables base addresses */
2432145247Sdamien	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2433145247Sdamien	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2434145247Sdamien
2435145247Sdamien	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2436145247Sdamien
2437145247Sdamien	if (ipw_config(sc) != 0) {
2438145247Sdamien		device_printf(sc->sc_dev, "device configuration failed\n");
2439178354Ssam		goto fail;
2440145247Sdamien	}
2441145247Sdamien
2442172567Sthompsa	callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2443286437Sadrian	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2444286437Sadrian	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2445286437Sadrian
2446286437Sadrian	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2447145247Sdamien	return;
2448145247Sdamien
2449178354Ssamfail:
2450172567Sthompsa	ipw_stop_locked(sc);
2451286437Sadrian	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2452145247Sdamien}
2453145247Sdamien
2454178354Ssamstatic int
2455178354Ssamipw_config(struct ipw_softc *sc)
2456178354Ssam{
2457286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
2458286437Sadrian	struct ieee80211com *ic = ifp->if_l2com;
2459178354Ssam	struct ipw_configuration config;
2460178354Ssam	uint32_t data;
2461178354Ssam	int error;
2462178354Ssam
2463178354Ssam	error = ipw_disable(sc);
2464178354Ssam	if (error != 0)
2465178354Ssam		return error;
2466178354Ssam
2467178354Ssam	switch (ic->ic_opmode) {
2468178354Ssam	case IEEE80211_M_STA:
2469178354Ssam	case IEEE80211_M_HOSTAP:
2470178354Ssam	case IEEE80211_M_WDS:		/* XXX */
2471178354Ssam		data = htole32(IPW_MODE_BSS);
2472178354Ssam		break;
2473178354Ssam	case IEEE80211_M_IBSS:
2474178354Ssam	case IEEE80211_M_AHDEMO:
2475178354Ssam		data = htole32(IPW_MODE_IBSS);
2476178354Ssam		break;
2477178354Ssam	case IEEE80211_M_MONITOR:
2478178354Ssam		data = htole32(IPW_MODE_MONITOR);
2479178354Ssam		break;
2480195562Srpaulo	default:
2481195562Srpaulo		device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2482195562Srpaulo		return EINVAL;
2483178354Ssam	}
2484178354Ssam	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2485178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2486178354Ssam	if (error != 0)
2487178354Ssam		return error;
2488178354Ssam
2489178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2490178354Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2491178354Ssam		error = ipw_setchannel(sc, ic->ic_curchan);
2492178354Ssam		if (error != 0)
2493178354Ssam			return error;
2494178354Ssam	}
2495178354Ssam
2496178354Ssam	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2497178354Ssam		return ipw_enable(sc);
2498178354Ssam
2499178354Ssam	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2500178354Ssam	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2501178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
2502178354Ssam		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2503286437Sadrian	if (ifp->if_flags & IFF_PROMISC)
2504178354Ssam		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2505178354Ssam	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2506178354Ssam	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2507178354Ssam	DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2508178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2509178354Ssam	if (error != 0)
2510178354Ssam		return error;
2511178354Ssam
2512207926Sbschmidt	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2513178354Ssam	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2514178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2515178354Ssam	if (error != 0)
2516178354Ssam		return error;
2517178354Ssam
2518207926Sbschmidt	/* Use the same rate set */
2519178354Ssam	DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2520178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2521178354Ssam	if (error != 0)
2522178354Ssam		return error;
2523178354Ssam
2524207926Sbschmidt	/* Use the same rate set */
2525178354Ssam	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2526178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2527178354Ssam	if (error != 0)
2528178354Ssam		return error;
2529178354Ssam
2530178354Ssam	data = htole32(IPW_POWER_MODE_CAM);
2531178354Ssam	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2532178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2533178354Ssam	if (error != 0)
2534178354Ssam		return error;
2535178354Ssam
2536178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2537178354Ssam		data = htole32(32); /* default value */
2538178354Ssam		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2539178354Ssam		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2540178354Ssam		    sizeof data);
2541178354Ssam		if (error != 0)
2542178354Ssam			return error;
2543178354Ssam	}
2544178354Ssam
2545178354Ssam	return 0;
2546178354Ssam}
2547178354Ssam
2548145247Sdamienstatic void
2549145247Sdamienipw_stop(void *priv)
2550145247Sdamien{
2551145247Sdamien	struct ipw_softc *sc = priv;
2552172567Sthompsa
2553172567Sthompsa	IPW_LOCK(sc);
2554172567Sthompsa	ipw_stop_locked(sc);
2555172567Sthompsa	IPW_UNLOCK(sc);
2556172567Sthompsa}
2557172567Sthompsa
2558172567Sthompsastatic void
2559172567Sthompsaipw_stop_locked(struct ipw_softc *sc)
2560172567Sthompsa{
2561286437Sadrian	struct ifnet *ifp = sc->sc_ifp;
2562145247Sdamien	int i;
2563145247Sdamien
2564172567Sthompsa	IPW_LOCK_ASSERT(sc);
2565156599Sdamien
2566172567Sthompsa	callout_stop(&sc->sc_wdtimer);
2567145247Sdamien	ipw_stop_master(sc);
2568145247Sdamien
2569145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2570145247Sdamien
2571145247Sdamien	/*
2572145247Sdamien	 * Release tx buffers.
2573145247Sdamien	 */
2574145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
2575145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
2576145247Sdamien
2577145247Sdamien	sc->sc_tx_timer = 0;
2578286437Sadrian	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2579145247Sdamien}
2580145247Sdamien
2581145247Sdamienstatic int
2582145247Sdamienipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2583145247Sdamien{
2584145247Sdamien	struct ipw_softc *sc = arg1;
2585145247Sdamien	uint32_t i, size, buf[256];
2586145247Sdamien
2587174317Sphilip	memset(buf, 0, sizeof buf);
2588174317Sphilip
2589174317Sphilip	if (!(sc->flags & IPW_FLAG_FW_INITED))
2590145247Sdamien		return SYSCTL_OUT(req, buf, sizeof buf);
2591145247Sdamien
2592145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2593145247Sdamien
2594145247Sdamien	size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2595145247Sdamien	for (i = 1; i < size; i++)
2596145247Sdamien		buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2597145247Sdamien
2598174317Sphilip	return SYSCTL_OUT(req, buf, size);
2599145247Sdamien}
2600145247Sdamien
2601145247Sdamienstatic int
2602145247Sdamienipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2603145247Sdamien{
2604145247Sdamien	struct ipw_softc *sc = arg1;
2605145247Sdamien	int val;
2606145247Sdamien
2607145247Sdamien	val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2608145247Sdamien	        (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2609145247Sdamien
2610145247Sdamien	return SYSCTL_OUT(req, &val, sizeof val);
2611145247Sdamien}
2612145247Sdamien
2613145247Sdamienstatic uint32_t
2614145247Sdamienipw_read_table1(struct ipw_softc *sc, uint32_t off)
2615145247Sdamien{
2616145247Sdamien	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2617145247Sdamien}
2618145247Sdamien
2619145247Sdamienstatic void
2620145247Sdamienipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2621145247Sdamien{
2622145247Sdamien	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2623145247Sdamien}
2624145247Sdamien
2625172567Sthompsa#if 0
2626145247Sdamienstatic int
2627145247Sdamienipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2628145247Sdamien{
2629145247Sdamien	uint32_t addr, info;
2630145247Sdamien	uint16_t count, size;
2631145247Sdamien	uint32_t total;
2632145247Sdamien
2633145247Sdamien	/* addr[4] + count[2] + size[2] */
2634145247Sdamien	addr = MEM_READ_4(sc, sc->table2_base + off);
2635145247Sdamien	info = MEM_READ_4(sc, sc->table2_base + off + 4);
2636145247Sdamien
2637145247Sdamien	count = info >> 16;
2638145247Sdamien	size = info & 0xffff;
2639145247Sdamien	total = count * size;
2640145247Sdamien
2641145247Sdamien	if (total > *len) {
2642145247Sdamien		*len = total;
2643145247Sdamien		return EINVAL;
2644145247Sdamien	}
2645145247Sdamien
2646145247Sdamien	*len = total;
2647145247Sdamien	ipw_read_mem_1(sc, addr, buf, total);
2648145247Sdamien
2649145247Sdamien	return 0;
2650145247Sdamien}
2651145247Sdamien
2652145247Sdamienstatic void
2653145247Sdamienipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2654145247Sdamien    bus_size_t count)
2655145247Sdamien{
2656145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2657145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2658145247Sdamien		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2659145247Sdamien	}
2660145247Sdamien}
2661172567Sthompsa#endif
2662145247Sdamien
2663145247Sdamienstatic void
2664156599Sdamienipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2665145247Sdamien    bus_size_t count)
2666145247Sdamien{
2667145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2668145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2669145247Sdamien		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2670145247Sdamien	}
2671145247Sdamien}
2672172567Sthompsa
2673172567Sthompsastatic void
2674172567Sthompsaipw_scan_start(struct ieee80211com *ic)
2675172567Sthompsa{
2676286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2677172567Sthompsa
2678172567Sthompsa	IPW_LOCK(sc);
2679191746Sthompsa	ipw_scan(sc);
2680172567Sthompsa	IPW_UNLOCK(sc);
2681172567Sthompsa}
2682172567Sthompsa
2683172567Sthompsastatic void
2684172567Sthompsaipw_set_channel(struct ieee80211com *ic)
2685172567Sthompsa{
2686286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2687172567Sthompsa
2688172567Sthompsa	IPW_LOCK(sc);
2689172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2690172567Sthompsa		ipw_disable(sc);
2691172567Sthompsa		ipw_setchannel(sc, ic->ic_curchan);
2692172567Sthompsa		ipw_enable(sc);
2693172567Sthompsa	}
2694172567Sthompsa	IPW_UNLOCK(sc);
2695172567Sthompsa}
2696172567Sthompsa
2697172567Sthompsastatic void
2698178354Ssamipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2699172567Sthompsa{
2700172567Sthompsa	/* NB: all channels are scanned at once */
2701172567Sthompsa}
2702172567Sthompsa
2703172567Sthompsastatic void
2704178354Ssamipw_scan_mindwell(struct ieee80211_scan_state *ss)
2705172567Sthompsa{
2706172567Sthompsa	/* NB: don't try to abort scan; wait for firmware to finish */
2707172567Sthompsa}
2708172567Sthompsa
2709172567Sthompsastatic void
2710172567Sthompsaipw_scan_end(struct ieee80211com *ic)
2711172567Sthompsa{
2712286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2713172567Sthompsa
2714172567Sthompsa	IPW_LOCK(sc);
2715172567Sthompsa	sc->flags &= ~IPW_FLAG_SCANNING;
2716172567Sthompsa	IPW_UNLOCK(sc);
2717172567Sthompsa}
2718