if_ipw.c revision 288087
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 288087 2015-09-22 02:44:59Z 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);
130287197Sglebiusstatic int	ipw_tx_start(struct ipw_softc *, struct mbuf *,
131145247Sdamien		    struct ieee80211_node *);
132178354Ssamstatic int	ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
133178354Ssam		    const struct ieee80211_bpf_params *);
134287197Sglebiusstatic int	ipw_transmit(struct ieee80211com *, struct mbuf *);
135287197Sglebiusstatic void	ipw_start(struct ipw_softc *);
136172567Sthompsastatic void	ipw_watchdog(void *);
137287197Sglebiusstatic void	ipw_parent(struct ieee80211com *);
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);
223287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
224170530Ssam	struct ieee80211_channel *c;
225145247Sdamien	uint16_t val;
226145247Sdamien	int error, i;
227145247Sdamien
228145247Sdamien	sc->sc_dev = dev;
229145247Sdamien
230145247Sdamien	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
231145247Sdamien	    MTX_DEF | MTX_RECURSE);
232287197Sglebius	mbufq_init(&sc->sc_snd, ifqmaxlen);
233156599Sdamien	TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
234172567Sthompsa	callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
235156599Sdamien
236146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
237146498Sdamien
238145247Sdamien	/* enable bus-mastering */
239145247Sdamien	pci_enable_busmaster(dev);
240145247Sdamien
241260062Smarius	i = PCIR_BAR(0);
242260062Smarius	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE);
243145247Sdamien	if (sc->mem == NULL) {
244145247Sdamien		device_printf(dev, "could not allocate memory resource\n");
245145247Sdamien		goto fail;
246145247Sdamien	}
247145247Sdamien
248145247Sdamien	sc->sc_st = rman_get_bustag(sc->mem);
249145247Sdamien	sc->sc_sh = rman_get_bushandle(sc->mem);
250145247Sdamien
251260062Smarius	i = 0;
252260062Smarius	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
253145247Sdamien	    RF_ACTIVE | RF_SHAREABLE);
254145247Sdamien	if (sc->irq == NULL) {
255145247Sdamien		device_printf(dev, "could not allocate interrupt resource\n");
256178354Ssam		goto fail1;
257145247Sdamien	}
258145247Sdamien
259145247Sdamien	if (ipw_reset(sc) != 0) {
260145247Sdamien		device_printf(dev, "could not reset adapter\n");
261178354Ssam		goto fail2;
262145247Sdamien	}
263145247Sdamien
264145247Sdamien	if (ipw_dma_alloc(sc) != 0) {
265145247Sdamien		device_printf(dev, "could not allocate DMA resources\n");
266178354Ssam		goto fail2;
267145247Sdamien	}
268147757Sdamien
269283537Sglebius	ic->ic_softc = sc;
270283527Sglebius	ic->ic_name = device_get_nameunit(dev);
271178354Ssam	ic->ic_opmode = IEEE80211_M_STA;
272145247Sdamien	ic->ic_phytype = IEEE80211_T_DS;
273145247Sdamien
274145247Sdamien	/* set device capabilities */
275178957Ssam	ic->ic_caps =
276178957Ssam		  IEEE80211_C_STA		/* station mode supported */
277178957Ssam		| IEEE80211_C_IBSS		/* IBSS mode supported */
278172567Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode supported */
279172567Sthompsa		| IEEE80211_C_PMGT		/* power save supported */
280172567Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
281172567Sthompsa		| IEEE80211_C_WPA		/* 802.11i supported */
282172567Sthompsa		;
283145247Sdamien
284145247Sdamien	/* read MAC address from EEPROM */
285145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
286287197Sglebius	ic->ic_macaddr[0] = val >> 8;
287287197Sglebius	ic->ic_macaddr[1] = val & 0xff;
288145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
289287197Sglebius	ic->ic_macaddr[2] = val >> 8;
290287197Sglebius	ic->ic_macaddr[3] = val & 0xff;
291145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
292287197Sglebius	ic->ic_macaddr[4] = val >> 8;
293287197Sglebius	ic->ic_macaddr[5] = val & 0xff;
294145247Sdamien
295170530Ssam	/* set supported .11b channels (read from EEPROM) */
296170530Ssam	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
297170530Ssam		val = 0x7ff; /* default to channels 1-11 */
298170530Ssam	val <<= 1;
299170530Ssam	for (i = 1; i < 16; i++) {
300170530Ssam		if (val & (1 << i)) {
301170530Ssam			c = &ic->ic_channels[ic->ic_nchans++];
302170530Ssam			c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
303170530Ssam			c->ic_flags = IEEE80211_CHAN_B;
304170530Ssam			c->ic_ieee = i;
305170530Ssam		}
306145247Sdamien	}
307145247Sdamien
308145247Sdamien	/* check support for radio transmitter switch in EEPROM */
309145247Sdamien	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
310145247Sdamien		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
311145247Sdamien
312287197Sglebius	ieee80211_ifattach(ic);
313172567Sthompsa	ic->ic_scan_start = ipw_scan_start;
314172567Sthompsa	ic->ic_scan_end = ipw_scan_end;
315172567Sthompsa	ic->ic_set_channel = ipw_set_channel;
316172567Sthompsa	ic->ic_scan_curchan = ipw_scan_curchan;
317172567Sthompsa	ic->ic_scan_mindwell = ipw_scan_mindwell;
318178354Ssam	ic->ic_raw_xmit = ipw_raw_xmit;
319178354Ssam	ic->ic_vap_create = ipw_vap_create;
320178354Ssam	ic->ic_vap_delete = ipw_vap_delete;
321287197Sglebius	ic->ic_transmit = ipw_transmit;
322287197Sglebius	ic->ic_parent = ipw_parent;
323145247Sdamien
324192468Ssam	ieee80211_radiotap_attach(ic,
325192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
326192468Ssam		IPW_TX_RADIOTAP_PRESENT,
327192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
328192468Ssam		IPW_RX_RADIOTAP_PRESENT);
329178354Ssam
330145247Sdamien	/*
331145247Sdamien	 * Add a few sysctl knobs.
332145247Sdamien	 */
333145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
334145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
335145247Sdamien	    CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
336145247Sdamien	    "radio transmitter switch state (0=off, 1=on)");
337145247Sdamien
338145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
339145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
340145247Sdamien	    CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
341145247Sdamien	    "statistics");
342145247Sdamien
343145247Sdamien	/*
344145247Sdamien	 * Hook our interrupt after all initialization is complete.
345145247Sdamien	 */
346145247Sdamien	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
347166901Spiso	    NULL, ipw_intr, sc, &sc->sc_ih);
348145247Sdamien	if (error != 0) {
349145247Sdamien		device_printf(dev, "could not set up interrupt\n");
350287197Sglebius		goto fail3;
351145247Sdamien	}
352145247Sdamien
353145247Sdamien	if (bootverbose)
354145247Sdamien		ieee80211_announce(ic);
355145247Sdamien
356145247Sdamien	return 0;
357178354Ssamfail3:
358178354Ssam	ipw_release(sc);
359178354Ssamfail2:
360260062Smarius	bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
361178354Ssamfail1:
362260062Smarius	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
363260062Smarius	    sc->mem);
364178354Ssamfail:
365178354Ssam	mtx_destroy(&sc->sc_mtx);
366145247Sdamien	return ENXIO;
367145247Sdamien}
368145247Sdamien
369145247Sdamienstatic int
370145247Sdamienipw_detach(device_t dev)
371145247Sdamien{
372145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
373287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
374145247Sdamien
375260062Smarius	bus_teardown_intr(dev, sc->irq, sc->sc_ih);
376260062Smarius
377191912Sthompsa	ieee80211_draintask(ic, &sc->sc_init_task);
378145247Sdamien	ipw_stop(sc);
379178354Ssam
380178354Ssam	ieee80211_ifdetach(ic);
381178354Ssam
382172567Sthompsa	callout_drain(&sc->sc_wdtimer);
383287197Sglebius	mbufq_drain(&sc->sc_snd);
384145247Sdamien
385145247Sdamien	ipw_release(sc);
386145247Sdamien
387260062Smarius	bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
388145247Sdamien
389260062Smarius	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
390260062Smarius	    sc->mem);
391156599Sdamien
392159487Siedowse	if (sc->sc_firmware != NULL) {
393159487Siedowse		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
394159487Siedowse		sc->sc_firmware = NULL;
395159487Siedowse	}
396159487Siedowse
397145247Sdamien	mtx_destroy(&sc->sc_mtx);
398145247Sdamien
399145247Sdamien	return 0;
400145247Sdamien}
401145247Sdamien
402178354Ssamstatic struct ieee80211vap *
403228621Sbschmidtipw_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
404228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
405228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
406228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
407178354Ssam{
408286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
409178354Ssam	struct ipw_vap *ivp;
410178354Ssam	struct ieee80211vap *vap;
411178354Ssam	const struct firmware *fp;
412178354Ssam	const struct ipw_firmware_hdr *hdr;
413178354Ssam	const char *imagename;
414178354Ssam
415178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
416178354Ssam		return NULL;
417178354Ssam
418178354Ssam	switch (opmode) {
419178354Ssam	case IEEE80211_M_STA:
420178354Ssam		imagename = "ipw_bss";
421178354Ssam		break;
422178354Ssam	case IEEE80211_M_IBSS:
423178354Ssam		imagename = "ipw_ibss";
424178354Ssam		break;
425178354Ssam	case IEEE80211_M_MONITOR:
426178354Ssam		imagename = "ipw_monitor";
427178354Ssam		break;
428178354Ssam	default:
429178354Ssam		return NULL;
430178354Ssam	}
431178354Ssam
432178354Ssam	/*
433178354Ssam	 * Load firmware image using the firmware(9) subsystem.  Doing
434178354Ssam	 * this unlocked is ok since we're single-threaded by the
435178354Ssam	 * 802.11 layer.
436178354Ssam	 */
437178354Ssam	if (sc->sc_firmware == NULL ||
438178354Ssam	    strcmp(sc->sc_firmware->name, imagename) != 0) {
439178354Ssam		if (sc->sc_firmware != NULL)
440178354Ssam			firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
441178354Ssam		sc->sc_firmware = firmware_get(imagename);
442178354Ssam	}
443178354Ssam	if (sc->sc_firmware == NULL) {
444178354Ssam		device_printf(sc->sc_dev,
445178354Ssam		    "could not load firmware image '%s'\n", imagename);
446178354Ssam		return NULL;
447178354Ssam	}
448178354Ssam	fp = sc->sc_firmware;
449178354Ssam	if (fp->datasize < sizeof *hdr) {
450178354Ssam		device_printf(sc->sc_dev,
451178354Ssam		    "firmware image too short %zu\n", fp->datasize);
452178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
453178354Ssam		sc->sc_firmware = NULL;
454178354Ssam		return NULL;
455178354Ssam	}
456178354Ssam	hdr = (const struct ipw_firmware_hdr *)fp->data;
457178354Ssam	if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
458178354Ssam	    le32toh(hdr->ucodesz)) {
459178354Ssam		device_printf(sc->sc_dev,
460178354Ssam		    "firmware image too short %zu\n", fp->datasize);
461178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
462178354Ssam		sc->sc_firmware = NULL;
463178354Ssam		return NULL;
464178354Ssam	}
465178354Ssam
466287197Sglebius	ivp = malloc(sizeof(struct ipw_vap), M_80211_VAP, M_WAITOK | M_ZERO);
467178354Ssam	vap = &ivp->vap;
468178354Ssam
469287197Sglebius	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
470178354Ssam	/* override with driver methods */
471178354Ssam	ivp->newstate = vap->iv_newstate;
472178354Ssam	vap->iv_newstate = ipw_newstate;
473178354Ssam
474178354Ssam	/* complete setup */
475287197Sglebius	ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status,
476287197Sglebius	    mac);
477178354Ssam	ic->ic_opmode = opmode;
478178354Ssam	return vap;
479178354Ssam}
480178354Ssam
481178354Ssamstatic void
482178354Ssamipw_vap_delete(struct ieee80211vap *vap)
483178354Ssam{
484178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
485178354Ssam
486178354Ssam	ieee80211_vap_detach(vap);
487178354Ssam	free(ivp, M_80211_VAP);
488178354Ssam}
489178354Ssam
490145247Sdamienstatic int
491145247Sdamienipw_dma_alloc(struct ipw_softc *sc)
492145247Sdamien{
493145247Sdamien	struct ipw_soft_bd *sbd;
494145247Sdamien	struct ipw_soft_hdr *shdr;
495145247Sdamien	struct ipw_soft_buf *sbuf;
496145247Sdamien	bus_addr_t physaddr;
497145247Sdamien	int error, i;
498145247Sdamien
499145247Sdamien	/*
500232874Sscottl	 * Allocate parent DMA tag for subsequent allocations.
501232874Sscottl	 */
502232874Sscottl	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
503232874Sscottl	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
504232874Sscottl	    BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED,
505232874Sscottl	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &sc->parent_dmat);
506232874Sscottl	if (error != 0) {
507232874Sscottl		device_printf(sc->sc_dev, "could not create parent DMA tag\n");
508232874Sscottl		goto fail;
509232874Sscottl	}
510232874Sscottl
511232874Sscottl	/*
512145247Sdamien	 * Allocate and map tx ring.
513145247Sdamien	 */
514232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
515145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
516145247Sdamien	    NULL, &sc->tbd_dmat);
517145247Sdamien	if (error != 0) {
518145247Sdamien		device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
519145247Sdamien		goto fail;
520145247Sdamien	}
521145247Sdamien
522145247Sdamien	error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
523145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
524145247Sdamien	if (error != 0) {
525145247Sdamien		device_printf(sc->sc_dev,
526145247Sdamien		    "could not allocate tx ring DMA memory\n");
527145247Sdamien		goto fail;
528145247Sdamien	}
529145247Sdamien
530145247Sdamien	error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
531145247Sdamien	    IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
532145247Sdamien	if (error != 0) {
533145247Sdamien		device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
534145247Sdamien		goto fail;
535145247Sdamien	}
536145247Sdamien
537145247Sdamien	/*
538145247Sdamien	 * Allocate and map rx ring.
539145247Sdamien	 */
540232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
541145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
542145247Sdamien	    NULL, &sc->rbd_dmat);
543145247Sdamien	if (error != 0) {
544145247Sdamien		device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
545145247Sdamien		goto fail;
546145247Sdamien	}
547145247Sdamien
548145247Sdamien	error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
549145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
550145247Sdamien	if (error != 0) {
551145247Sdamien		device_printf(sc->sc_dev,
552145247Sdamien		    "could not allocate rx ring DMA memory\n");
553145247Sdamien		goto fail;
554145247Sdamien	}
555145247Sdamien
556145247Sdamien	error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
557145247Sdamien	    IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
558145247Sdamien	if (error != 0) {
559145247Sdamien		device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
560145247Sdamien		goto fail;
561145247Sdamien	}
562145247Sdamien
563145247Sdamien	/*
564145247Sdamien	 * Allocate and map status ring.
565145247Sdamien	 */
566232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
567145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
568145247Sdamien	    NULL, NULL, &sc->status_dmat);
569145247Sdamien	if (error != 0) {
570145247Sdamien		device_printf(sc->sc_dev,
571145247Sdamien		    "could not create status ring DMA tag\n");
572145247Sdamien		goto fail;
573145247Sdamien	}
574145247Sdamien
575145247Sdamien	error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
576145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
577145247Sdamien	if (error != 0) {
578145247Sdamien		device_printf(sc->sc_dev,
579145247Sdamien		    "could not allocate status ring DMA memory\n");
580145247Sdamien		goto fail;
581145247Sdamien	}
582145247Sdamien
583145247Sdamien	error = bus_dmamap_load(sc->status_dmat, sc->status_map,
584145247Sdamien	    sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
585145247Sdamien	    0);
586145247Sdamien	if (error != 0) {
587145247Sdamien		device_printf(sc->sc_dev,
588145247Sdamien		    "could not map status ring DMA memory\n");
589145247Sdamien		goto fail;
590145247Sdamien	}
591145247Sdamien
592145247Sdamien	/*
593145247Sdamien	 * Allocate command DMA map.
594145247Sdamien	 */
595232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
596145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
597145247Sdamien	    sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
598145247Sdamien	if (error != 0) {
599145247Sdamien		device_printf(sc->sc_dev, "could not create command DMA tag\n");
600145247Sdamien		goto fail;
601145247Sdamien	}
602145247Sdamien
603145247Sdamien	error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
604145247Sdamien	if (error != 0) {
605145247Sdamien		device_printf(sc->sc_dev,
606145247Sdamien		    "could not create command DMA map\n");
607145247Sdamien		goto fail;
608145247Sdamien	}
609145247Sdamien
610145247Sdamien	/*
611145247Sdamien	 * Allocate headers DMA maps.
612145247Sdamien	 */
613232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
614145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
615145247Sdamien	    sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
616145247Sdamien	if (error != 0) {
617145247Sdamien		device_printf(sc->sc_dev, "could not create header DMA tag\n");
618145247Sdamien		goto fail;
619145247Sdamien	}
620145247Sdamien
621145247Sdamien	SLIST_INIT(&sc->free_shdr);
622145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
623145247Sdamien		shdr = &sc->shdr_list[i];
624145247Sdamien		error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
625145247Sdamien		if (error != 0) {
626145247Sdamien			device_printf(sc->sc_dev,
627145247Sdamien			    "could not create header DMA map\n");
628145247Sdamien			goto fail;
629145247Sdamien		}
630145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
631145247Sdamien	}
632145247Sdamien
633145247Sdamien	/*
634145247Sdamien	 * Allocate tx buffers DMA maps.
635145247Sdamien	 */
636232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
637145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
638145247Sdamien	    NULL, NULL, &sc->txbuf_dmat);
639145247Sdamien	if (error != 0) {
640145247Sdamien		device_printf(sc->sc_dev, "could not create tx DMA tag\n");
641145247Sdamien		goto fail;
642145247Sdamien	}
643145247Sdamien
644145247Sdamien	SLIST_INIT(&sc->free_sbuf);
645145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
646145247Sdamien		sbuf = &sc->tx_sbuf_list[i];
647145247Sdamien		error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
648145247Sdamien		if (error != 0) {
649145247Sdamien			device_printf(sc->sc_dev,
650145247Sdamien			    "could not create tx DMA map\n");
651145247Sdamien			goto fail;
652145247Sdamien		}
653145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
654145247Sdamien	}
655145247Sdamien
656145247Sdamien	/*
657145247Sdamien	 * Initialize tx ring.
658145247Sdamien	 */
659145247Sdamien	for (i = 0; i < IPW_NTBD; i++) {
660145247Sdamien		sbd = &sc->stbd_list[i];
661145247Sdamien		sbd->bd = &sc->tbd_list[i];
662145247Sdamien		sbd->type = IPW_SBD_TYPE_NOASSOC;
663145247Sdamien	}
664145247Sdamien
665145247Sdamien	/*
666145247Sdamien	 * Pre-allocate rx buffers and DMA maps.
667145247Sdamien	 */
668232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
669147841Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
670147841Sdamien	    NULL, &sc->rxbuf_dmat);
671145247Sdamien	if (error != 0) {
672145247Sdamien		device_printf(sc->sc_dev, "could not create rx DMA tag\n");
673145247Sdamien		goto fail;
674145247Sdamien	}
675145247Sdamien
676145247Sdamien	for (i = 0; i < IPW_NRBD; i++) {
677145247Sdamien		sbd = &sc->srbd_list[i];
678145247Sdamien		sbuf = &sc->rx_sbuf_list[i];
679145247Sdamien		sbd->bd = &sc->rbd_list[i];
680145247Sdamien
681243857Sglebius		sbuf->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
682145247Sdamien		if (sbuf->m == NULL) {
683145247Sdamien			device_printf(sc->sc_dev,
684145247Sdamien			    "could not allocate rx mbuf\n");
685145247Sdamien			error = ENOMEM;
686145247Sdamien			goto fail;
687145247Sdamien		}
688145247Sdamien
689145247Sdamien		error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
690145247Sdamien		if (error != 0) {
691145247Sdamien			device_printf(sc->sc_dev,
692145247Sdamien			    "could not create rx DMA map\n");
693145247Sdamien			goto fail;
694145247Sdamien		}
695145247Sdamien
696145247Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
697145247Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
698145247Sdamien		    &physaddr, 0);
699145247Sdamien		if (error != 0) {
700145247Sdamien			device_printf(sc->sc_dev,
701145247Sdamien			    "could not map rx DMA memory\n");
702145247Sdamien			goto fail;
703145247Sdamien		}
704145247Sdamien
705145247Sdamien		sbd->type = IPW_SBD_TYPE_DATA;
706145247Sdamien		sbd->priv = sbuf;
707145247Sdamien		sbd->bd->physaddr = htole32(physaddr);
708145247Sdamien		sbd->bd->len = htole32(MCLBYTES);
709145247Sdamien	}
710145247Sdamien
711145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
712145247Sdamien
713145247Sdamien	return 0;
714145247Sdamien
715145247Sdamienfail:	ipw_release(sc);
716145247Sdamien	return error;
717145247Sdamien}
718145247Sdamien
719145247Sdamienstatic void
720145247Sdamienipw_release(struct ipw_softc *sc)
721145247Sdamien{
722145247Sdamien	struct ipw_soft_buf *sbuf;
723145247Sdamien	int i;
724145247Sdamien
725232874Sscottl	if (sc->parent_dmat != NULL) {
726232874Sscottl		bus_dma_tag_destroy(sc->parent_dmat);
727232874Sscottl	}
728232874Sscottl
729145247Sdamien	if (sc->tbd_dmat != NULL) {
730283528Sglebius		bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
731283528Sglebius		bus_dmamem_free(sc->tbd_dmat, sc->tbd_list, sc->tbd_map);
732145247Sdamien		bus_dma_tag_destroy(sc->tbd_dmat);
733145247Sdamien	}
734145247Sdamien
735145247Sdamien	if (sc->rbd_dmat != NULL) {
736145247Sdamien		if (sc->rbd_list != NULL) {
737145247Sdamien			bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
738145247Sdamien			bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
739145247Sdamien			    sc->rbd_map);
740145247Sdamien		}
741145247Sdamien		bus_dma_tag_destroy(sc->rbd_dmat);
742145247Sdamien	}
743145247Sdamien
744145247Sdamien	if (sc->status_dmat != NULL) {
745145247Sdamien		if (sc->status_list != NULL) {
746145247Sdamien			bus_dmamap_unload(sc->status_dmat, sc->status_map);
747145247Sdamien			bus_dmamem_free(sc->status_dmat, sc->status_list,
748145247Sdamien			    sc->status_map);
749145247Sdamien		}
750145247Sdamien		bus_dma_tag_destroy(sc->status_dmat);
751145247Sdamien	}
752145247Sdamien
753145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
754145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
755145247Sdamien
756145247Sdamien	if (sc->cmd_dmat != NULL) {
757145247Sdamien		bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
758145247Sdamien		bus_dma_tag_destroy(sc->cmd_dmat);
759145247Sdamien	}
760145247Sdamien
761145247Sdamien	if (sc->hdr_dmat != NULL) {
762145247Sdamien		for (i = 0; i < IPW_NDATA; i++)
763145247Sdamien			bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
764145247Sdamien		bus_dma_tag_destroy(sc->hdr_dmat);
765145247Sdamien	}
766145247Sdamien
767145247Sdamien	if (sc->txbuf_dmat != NULL) {
768145247Sdamien		for (i = 0; i < IPW_NDATA; i++) {
769145247Sdamien			bus_dmamap_destroy(sc->txbuf_dmat,
770145247Sdamien			    sc->tx_sbuf_list[i].map);
771145247Sdamien		}
772145247Sdamien		bus_dma_tag_destroy(sc->txbuf_dmat);
773145247Sdamien	}
774145247Sdamien
775145247Sdamien	if (sc->rxbuf_dmat != NULL) {
776145247Sdamien		for (i = 0; i < IPW_NRBD; i++) {
777145247Sdamien			sbuf = &sc->rx_sbuf_list[i];
778145247Sdamien			if (sbuf->m != NULL) {
779145247Sdamien				bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
780145247Sdamien				    BUS_DMASYNC_POSTREAD);
781145247Sdamien				bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
782145247Sdamien				m_freem(sbuf->m);
783145247Sdamien			}
784145247Sdamien			bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
785145247Sdamien		}
786145247Sdamien		bus_dma_tag_destroy(sc->rxbuf_dmat);
787145247Sdamien	}
788145247Sdamien}
789145247Sdamien
790145247Sdamienstatic int
791145247Sdamienipw_shutdown(device_t dev)
792145247Sdamien{
793145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
794145247Sdamien
795145247Sdamien	ipw_stop(sc);
796145247Sdamien
797145247Sdamien	return 0;
798145247Sdamien}
799145247Sdamien
800145247Sdamienstatic int
801145247Sdamienipw_suspend(device_t dev)
802145247Sdamien{
803145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
804287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
805145247Sdamien
806233387Sbschmidt	ieee80211_suspend_all(ic);
807145247Sdamien	return 0;
808145247Sdamien}
809145247Sdamien
810145247Sdamienstatic int
811145247Sdamienipw_resume(device_t dev)
812145247Sdamien{
813145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
814287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
815145247Sdamien
816146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
817146498Sdamien
818233387Sbschmidt	ieee80211_resume_all(ic);
819145247Sdamien	return 0;
820145247Sdamien}
821145247Sdamien
822145247Sdamienstatic int
823172567Sthompsaipw_cvtrate(int ipwrate)
824172567Sthompsa{
825172567Sthompsa	switch (ipwrate) {
826172567Sthompsa	case IPW_RATE_DS1:	return 2;
827172567Sthompsa	case IPW_RATE_DS2:	return 4;
828172567Sthompsa	case IPW_RATE_DS5:	return 11;
829172567Sthompsa	case IPW_RATE_DS11:	return 22;
830172567Sthompsa	}
831145247Sdamien	return 0;
832145247Sdamien}
833145247Sdamien
834145247Sdamien/*
835156599Sdamien * The firmware automatically adapts the transmit speed. We report its current
836156599Sdamien * value here.
837145247Sdamien */
838145247Sdamienstatic void
839145247Sdamienipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
840145247Sdamien{
841178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
842178354Ssam	struct ieee80211com *ic = vap->iv_ic;
843286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
844145247Sdamien
845145247Sdamien	/* read current transmission rate from adapter */
846178354Ssam	vap->iv_bss->ni_txrate = ipw_cvtrate(
847178354Ssam	    ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
848178354Ssam	ieee80211_media_status(ifp, imr);
849145247Sdamien}
850145247Sdamien
851145247Sdamienstatic int
852178354Ssamipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
853145247Sdamien{
854178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
855178354Ssam	struct ieee80211com *ic = vap->iv_ic;
856286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
857191746Sthompsa	enum ieee80211_state ostate;
858145247Sdamien
859172567Sthompsa	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
860178354Ssam		ieee80211_state_name[vap->iv_state],
861172567Sthompsa		ieee80211_state_name[nstate], sc->flags));
862172567Sthompsa
863191746Sthompsa	ostate = vap->iv_state;
864191746Sthompsa	IEEE80211_UNLOCK(ic);
865191746Sthompsa
866145247Sdamien	switch (nstate) {
867145247Sdamien	case IEEE80211_S_RUN:
868172567Sthompsa		if (ic->ic_opmode == IEEE80211_M_IBSS) {
869172567Sthompsa			/*
870172567Sthompsa			 * XXX when joining an ibss network we are called
871172567Sthompsa			 * with a SCAN -> RUN transition on scan complete.
872206763Sbschmidt			 * Use that to call ipw_assoc.  On completing the
873206763Sbschmidt			 * join we are then called again with an AUTH -> RUN
874206763Sbschmidt			 * transition and we want to do nothing.  This is
875206763Sbschmidt			 * all totally bogus and needs to be redone.
876172567Sthompsa			 */
877191746Sthompsa			if (ostate == IEEE80211_S_SCAN)
878191746Sthompsa				ipw_assoc(ic, vap);
879172567Sthompsa		}
880172567Sthompsa		break;
881145247Sdamien
882172567Sthompsa	case IEEE80211_S_INIT:
883172567Sthompsa		if (sc->flags & IPW_FLAG_ASSOCIATED)
884191746Sthompsa			ipw_disassoc(ic, vap);
885172567Sthompsa		break;
886145247Sdamien
887172567Sthompsa	case IEEE80211_S_AUTH:
888206765Sbschmidt		/*
889206765Sbschmidt		 * Move to ASSOC state after the ipw_assoc() call.  Firmware
890206765Sbschmidt		 * takes care of authentication, after the call we'll receive
891206765Sbschmidt		 * only an assoc response which would otherwise be discared
892206765Sbschmidt		 * if we are still in AUTH state.
893206765Sbschmidt		 */
894206765Sbschmidt		nstate = IEEE80211_S_ASSOC;
895191746Sthompsa		ipw_assoc(ic, vap);
896191746Sthompsa		break;
897145247Sdamien
898172567Sthompsa	case IEEE80211_S_ASSOC:
899172567Sthompsa		/*
900206763Sbschmidt		 * If we are not transitioning from AUTH then resend the
901172567Sthompsa		 * association request.
902172567Sthompsa		 */
903191746Sthompsa		if (ostate != IEEE80211_S_AUTH)
904191746Sthompsa			ipw_assoc(ic, vap);
905145247Sdamien		break;
906145247Sdamien
907172058Ssam	default:
908145247Sdamien		break;
909145247Sdamien	}
910191746Sthompsa	IEEE80211_LOCK(ic);
911178354Ssam	return ivp->newstate(vap, nstate, arg);
912145247Sdamien}
913145247Sdamien
914145247Sdamien/*
915145247Sdamien * Read 16 bits at address 'addr' from the serial EEPROM.
916145247Sdamien */
917145247Sdamienstatic uint16_t
918145247Sdamienipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
919145247Sdamien{
920145247Sdamien	uint32_t tmp;
921145247Sdamien	uint16_t val;
922145247Sdamien	int n;
923145247Sdamien
924145247Sdamien	/* clock C once before the first command */
925145247Sdamien	IPW_EEPROM_CTL(sc, 0);
926145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
927145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
928145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
929145247Sdamien
930145247Sdamien	/* write start bit (1) */
931145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
932145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
933145247Sdamien
934145247Sdamien	/* write READ opcode (10) */
935145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
936145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
937145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
938145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
939145247Sdamien
940145247Sdamien	/* write address A7-A0 */
941145247Sdamien	for (n = 7; n >= 0; n--) {
942145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
943145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
944145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
945145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
946145247Sdamien	}
947145247Sdamien
948145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
949145247Sdamien
950145247Sdamien	/* read data Q15-Q0 */
951145247Sdamien	val = 0;
952145247Sdamien	for (n = 15; n >= 0; n--) {
953145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
954145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
955145247Sdamien		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
956145247Sdamien		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
957145247Sdamien	}
958145247Sdamien
959145247Sdamien	IPW_EEPROM_CTL(sc, 0);
960145247Sdamien
961145247Sdamien	/* clear Chip Select and clock C */
962145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
963145247Sdamien	IPW_EEPROM_CTL(sc, 0);
964145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
965145247Sdamien
966145247Sdamien	return le16toh(val);
967145247Sdamien}
968145247Sdamien
969145247Sdamienstatic void
970172567Sthompsaipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
971145247Sdamien{
972145247Sdamien	struct ipw_cmd *cmd;
973145247Sdamien
974145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
975145247Sdamien
976145247Sdamien	cmd = mtod(sbuf->m, struct ipw_cmd *);
977145247Sdamien
978172567Sthompsa	DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
979172567Sthompsa	    ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
980145247Sdamien	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
981145247Sdamien	    le32toh(cmd->status)));
982145247Sdamien
983172567Sthompsa	sc->flags &= ~IPW_FLAG_BUSY;
984145247Sdamien	wakeup(sc);
985145247Sdamien}
986145247Sdamien
987145247Sdamienstatic void
988172567Sthompsaipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
989145247Sdamien{
990178354Ssam#define	IEEESTATE(vap)	ieee80211_state_name[vap->iv_state]
991287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
992178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
993145247Sdamien	uint32_t state;
994145247Sdamien
995145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
996145247Sdamien
997145247Sdamien	state = le32toh(*mtod(sbuf->m, uint32_t *));
998145247Sdamien
999145247Sdamien	switch (state) {
1000145247Sdamien	case IPW_STATE_ASSOCIATED:
1001172567Sthompsa		DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1002178354Ssam			IEEESTATE(vap), sc->flags));
1003178354Ssam		/* XXX suppress state change in case the fw auto-associates */
1004178354Ssam		if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1005178354Ssam			DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1006178354Ssam				IEEESTATE(vap), sc->flags));
1007178354Ssam			break;
1008178354Ssam		}
1009178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATING;
1010172567Sthompsa		sc->flags |= IPW_FLAG_ASSOCIATED;
1011145247Sdamien		break;
1012145247Sdamien
1013145247Sdamien	case IPW_STATE_SCANNING:
1014172567Sthompsa		DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1015178354Ssam			IEEESTATE(vap), sc->flags));
1016172567Sthompsa		/*
1017172567Sthompsa		 * NB: Check driver state for association on assoc
1018172567Sthompsa		 * loss as the firmware will immediately start to
1019172567Sthompsa		 * scan and we would treat it as a beacon miss if
1020172567Sthompsa		 * we checked the 802.11 layer state.
1021172567Sthompsa		 */
1022178354Ssam		if (sc->flags & IPW_FLAG_ASSOCIATED) {
1023206767Sbschmidt			IPW_UNLOCK(sc);
1024178354Ssam			/* XXX probably need to issue disassoc to fw */
1025191746Sthompsa			ieee80211_beacon_miss(ic);
1026206767Sbschmidt			IPW_LOCK(sc);
1027178354Ssam		}
1028145247Sdamien		break;
1029145247Sdamien
1030145247Sdamien	case IPW_STATE_SCAN_COMPLETE:
1031172567Sthompsa		/*
1032172567Sthompsa		 * XXX For some reason scan requests generate scan
1033172567Sthompsa		 * started + scan done events before any traffic is
1034172567Sthompsa		 * received (e.g. probe response frames).  We work
1035172567Sthompsa		 * around this by marking the HACK flag and skipping
1036172567Sthompsa		 * the first scan complete event.
1037172567Sthompsa		*/
1038178354Ssam		DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1039178354Ssam			    IEEESTATE(vap), sc->flags));
1040172567Sthompsa		if (sc->flags & IPW_FLAG_HACK) {
1041172567Sthompsa			sc->flags &= ~IPW_FLAG_HACK;
1042172567Sthompsa			break;
1043172567Sthompsa		}
1044172567Sthompsa		if (sc->flags & IPW_FLAG_SCANNING) {
1045206767Sbschmidt			IPW_UNLOCK(sc);
1046191746Sthompsa			ieee80211_scan_done(vap);
1047206767Sbschmidt			IPW_LOCK(sc);
1048172567Sthompsa			sc->flags &= ~IPW_FLAG_SCANNING;
1049172567Sthompsa			sc->sc_scan_timer = 0;
1050172567Sthompsa		}
1051145247Sdamien		break;
1052145247Sdamien
1053145247Sdamien	case IPW_STATE_ASSOCIATION_LOST:
1054172567Sthompsa		DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1055178354Ssam			IEEESTATE(vap), sc->flags));
1056178354Ssam		sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1057206767Sbschmidt		if (vap->iv_state == IEEE80211_S_RUN) {
1058206767Sbschmidt			IPW_UNLOCK(sc);
1059191746Sthompsa			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1060206767Sbschmidt			IPW_LOCK(sc);
1061206767Sbschmidt		}
1062145247Sdamien		break;
1063145247Sdamien
1064172567Sthompsa	case IPW_STATE_DISABLED:
1065178354Ssam		/* XXX? is this right? */
1066206763Sbschmidt		sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1067178354Ssam		    IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1068172567Sthompsa		DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1069178354Ssam			IEEESTATE(vap), sc->flags));
1070172567Sthompsa		break;
1071172567Sthompsa
1072145247Sdamien	case IPW_STATE_RADIO_DISABLED:
1073178354Ssam		device_printf(sc->sc_dev, "radio turned off\n");
1074178354Ssam		ieee80211_notify_radio(ic, 0);
1075172567Sthompsa		ipw_stop_locked(sc);
1076178354Ssam		/* XXX start polling thread to detect radio on */
1077145247Sdamien		break;
1078172567Sthompsa
1079172567Sthompsa	default:
1080172567Sthompsa		DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1081178354Ssam			__func__, state, IEEESTATE(vap), sc->flags));
1082172567Sthompsa		break;
1083145247Sdamien	}
1084172567Sthompsa#undef IEEESTATE
1085145247Sdamien}
1086145247Sdamien
1087145247Sdamien/*
1088172567Sthompsa * Set driver state for current channel.
1089172567Sthompsa */
1090172567Sthompsastatic void
1091172567Sthompsaipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1092172567Sthompsa{
1093287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1094172567Sthompsa
1095172567Sthompsa	ic->ic_curchan = chan;
1096192468Ssam	ieee80211_radiotap_chan_change(ic);
1097172567Sthompsa}
1098172567Sthompsa
1099172567Sthompsa/*
1100145247Sdamien * XXX: Hack to set the current channel to the value advertised in beacons or
1101145247Sdamien * probe responses. Only used during AP detection.
1102145247Sdamien */
1103145247Sdamienstatic void
1104172567Sthompsaipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1105145247Sdamien{
1106287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1107172567Sthompsa	struct ieee80211_channel *c;
1108145247Sdamien	struct ieee80211_frame *wh;
1109145247Sdamien	uint8_t subtype;
1110145247Sdamien	uint8_t *frm, *efrm;
1111145247Sdamien
1112145247Sdamien	wh = mtod(m, struct ieee80211_frame *);
1113145247Sdamien
1114145247Sdamien	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1115145247Sdamien		return;
1116145247Sdamien
1117145247Sdamien	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1118145247Sdamien
1119145247Sdamien	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1120145247Sdamien	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1121145247Sdamien		return;
1122145247Sdamien
1123178354Ssam	/* XXX use ieee80211_parse_beacon */
1124145247Sdamien	frm = (uint8_t *)(wh + 1);
1125145247Sdamien	efrm = mtod(m, uint8_t *) + m->m_len;
1126145247Sdamien
1127145247Sdamien	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1128145247Sdamien	while (frm < efrm) {
1129145247Sdamien		if (*frm == IEEE80211_ELEMID_DSPARMS)
1130145247Sdamien#if IEEE80211_CHAN_MAX < 255
1131145247Sdamien		if (frm[2] <= IEEE80211_CHAN_MAX)
1132145247Sdamien#endif
1133172567Sthompsa		{
1134172567Sthompsa			DPRINTF(("Fixing channel to %d\n", frm[2]));
1135172567Sthompsa			c = ieee80211_find_channel(ic,
1136170530Ssam				ieee80211_ieee2mhz(frm[2], 0),
1137172567Sthompsa				IEEE80211_CHAN_B);
1138172567Sthompsa			if (c == NULL)
1139172567Sthompsa				c = &ic->ic_channels[0];
1140172567Sthompsa			ipw_setcurchan(sc, c);
1141172567Sthompsa		}
1142145247Sdamien
1143145247Sdamien		frm += frm[1] + 2;
1144145247Sdamien	}
1145145247Sdamien}
1146145247Sdamien
1147145247Sdamienstatic void
1148172567Sthompsaipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1149145247Sdamien    struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1150145247Sdamien{
1151287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1152152385Sdamien	struct mbuf *mnew, *m;
1153145247Sdamien	struct ieee80211_node *ni;
1154145247Sdamien	bus_addr_t physaddr;
1155145247Sdamien	int error;
1156192468Ssam	int8_t rssi, nf;
1157145247Sdamien
1158152385Sdamien	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1159152385Sdamien	    status->rssi));
1160152385Sdamien
1161147757Sdamien	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1162147757Sdamien	    le32toh(status->len) > MCLBYTES)
1163147757Sdamien		return;
1164147757Sdamien
1165152385Sdamien	/*
1166152385Sdamien	 * Try to allocate a new mbuf for this ring element and load it before
1167152385Sdamien	 * processing the current mbuf. If the ring element cannot be loaded,
1168152385Sdamien	 * drop the received packet and reuse the old mbuf. In the unlikely
1169152385Sdamien	 * case that the old mbuf can't be reloaded either, explicitly panic.
1170152385Sdamien	 */
1171243857Sglebius	mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1172152385Sdamien	if (mnew == NULL) {
1173287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
1174152385Sdamien		return;
1175152385Sdamien	}
1176152385Sdamien
1177145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1178145247Sdamien	bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1179145247Sdamien
1180152385Sdamien	error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1181152385Sdamien	    MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1182152385Sdamien	if (error != 0) {
1183152385Sdamien		m_freem(mnew);
1184152385Sdamien
1185152385Sdamien		/* try to reload the old mbuf */
1186152385Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1187152385Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1188152385Sdamien		    &physaddr, 0);
1189152385Sdamien		if (error != 0) {
1190152385Sdamien			/* very unlikely that it will fail... */
1191152385Sdamien			panic("%s: could not load old rx mbuf",
1192152385Sdamien			    device_get_name(sc->sc_dev));
1193152385Sdamien		}
1194287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
1195152385Sdamien		return;
1196152385Sdamien	}
1197152385Sdamien
1198152385Sdamien	/*
1199152385Sdamien	 * New mbuf successfully loaded, update Rx ring and continue
1200152385Sdamien	 * processing.
1201152385Sdamien	 */
1202152385Sdamien	m = sbuf->m;
1203152385Sdamien	sbuf->m = mnew;
1204152385Sdamien	sbd->bd->physaddr = htole32(physaddr);
1205145247Sdamien	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1206145247Sdamien
1207192468Ssam	rssi = status->rssi + IPW_RSSI_TO_DBM;
1208192468Ssam	nf = -95;
1209192468Ssam	if (ieee80211_radiotap_active(ic)) {
1210145247Sdamien		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1211145247Sdamien
1212145247Sdamien		tap->wr_flags = 0;
1213192468Ssam		tap->wr_antsignal = rssi;
1214192468Ssam		tap->wr_antnoise = nf;
1215145247Sdamien	}
1216145247Sdamien
1217172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
1218172567Sthompsa		ipw_fix_channel(sc, m);
1219145247Sdamien
1220172567Sthompsa	IPW_UNLOCK(sc);
1221178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1222178354Ssam	if (ni != NULL) {
1223206764Sbschmidt		(void) ieee80211_input(ni, m, rssi - nf, nf);
1224178354Ssam		ieee80211_free_node(ni);
1225178354Ssam	} else
1226206764Sbschmidt		(void) ieee80211_input_all(ic, m, rssi - nf, nf);
1227172567Sthompsa	IPW_LOCK(sc);
1228145247Sdamien
1229145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1230145247Sdamien}
1231145247Sdamien
1232145247Sdamienstatic void
1233145247Sdamienipw_rx_intr(struct ipw_softc *sc)
1234145247Sdamien{
1235145247Sdamien	struct ipw_status *status;
1236145247Sdamien	struct ipw_soft_bd *sbd;
1237145247Sdamien	struct ipw_soft_buf *sbuf;
1238145247Sdamien	uint32_t r, i;
1239145247Sdamien
1240145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1241145247Sdamien		return;
1242145247Sdamien
1243145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1244145247Sdamien
1245145247Sdamien	bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1246145247Sdamien
1247145247Sdamien	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1248145247Sdamien		status = &sc->status_list[i];
1249145247Sdamien		sbd = &sc->srbd_list[i];
1250145247Sdamien		sbuf = sbd->priv;
1251145247Sdamien
1252145247Sdamien		switch (le16toh(status->code) & 0xf) {
1253145247Sdamien		case IPW_STATUS_CODE_COMMAND:
1254172567Sthompsa			ipw_rx_cmd_intr(sc, sbuf);
1255145247Sdamien			break;
1256145247Sdamien
1257145247Sdamien		case IPW_STATUS_CODE_NEWSTATE:
1258172567Sthompsa			ipw_rx_newstate_intr(sc, sbuf);
1259145247Sdamien			break;
1260145247Sdamien
1261145247Sdamien		case IPW_STATUS_CODE_DATA_802_3:
1262145247Sdamien		case IPW_STATUS_CODE_DATA_802_11:
1263172567Sthompsa			ipw_rx_data_intr(sc, status, sbd, sbuf);
1264145247Sdamien			break;
1265145247Sdamien
1266145247Sdamien		case IPW_STATUS_CODE_NOTIFICATION:
1267172567Sthompsa			DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1268172567Sthompsa			    le32toh(status->len), status->flags));
1269178354Ssam			/* XXX maybe drive state machine AUTH->ASSOC? */
1270145247Sdamien			break;
1271145247Sdamien
1272145247Sdamien		default:
1273172567Sthompsa			device_printf(sc->sc_dev, "unexpected status code %u\n",
1274145247Sdamien			    le16toh(status->code));
1275145247Sdamien		}
1276145247Sdamien
1277145247Sdamien		/* firmware was killed, stop processing received frames */
1278145247Sdamien		if (!(sc->flags & IPW_FLAG_FW_INITED))
1279145247Sdamien			return;
1280145247Sdamien
1281145247Sdamien		sbd->bd->flags = 0;
1282145247Sdamien	}
1283145247Sdamien
1284145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1285145247Sdamien
1286145247Sdamien	/* kick the firmware */
1287145247Sdamien	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1288145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1289145247Sdamien}
1290145247Sdamien
1291145247Sdamienstatic void
1292145247Sdamienipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1293145247Sdamien{
1294145247Sdamien	struct ipw_soft_hdr *shdr;
1295145247Sdamien	struct ipw_soft_buf *sbuf;
1296145247Sdamien
1297145247Sdamien	switch (sbd->type) {
1298145247Sdamien	case IPW_SBD_TYPE_COMMAND:
1299145247Sdamien		bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1300145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1301145247Sdamien		bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1302145247Sdamien		break;
1303145247Sdamien
1304145247Sdamien	case IPW_SBD_TYPE_HEADER:
1305145247Sdamien		shdr = sbd->priv;
1306145247Sdamien		bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1307145247Sdamien		bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1308145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1309145247Sdamien		break;
1310145247Sdamien
1311145247Sdamien	case IPW_SBD_TYPE_DATA:
1312145247Sdamien		sbuf = sbd->priv;
1313145247Sdamien		bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1314145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1315145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1316145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1317145247Sdamien
1318170530Ssam		if (sbuf->m->m_flags & M_TXCB)
1319170530Ssam			ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1320145247Sdamien		m_freem(sbuf->m);
1321145247Sdamien		ieee80211_free_node(sbuf->ni);
1322145247Sdamien
1323145247Sdamien		sc->sc_tx_timer = 0;
1324145247Sdamien		break;
1325145247Sdamien	}
1326145247Sdamien
1327145247Sdamien	sbd->type = IPW_SBD_TYPE_NOASSOC;
1328145247Sdamien}
1329145247Sdamien
1330145247Sdamienstatic void
1331145247Sdamienipw_tx_intr(struct ipw_softc *sc)
1332145247Sdamien{
1333145247Sdamien	struct ipw_soft_bd *sbd;
1334145247Sdamien	uint32_t r, i;
1335145247Sdamien
1336145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1337145247Sdamien		return;
1338145247Sdamien
1339145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1340145247Sdamien
1341145247Sdamien	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1342145247Sdamien		sbd = &sc->stbd_list[i];
1343145247Sdamien		ipw_release_sbd(sc, sbd);
1344145247Sdamien		sc->txfree++;
1345145247Sdamien	}
1346145247Sdamien
1347145247Sdamien	/* remember what the firmware has processed */
1348145247Sdamien	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1349145247Sdamien
1350287197Sglebius	ipw_start(sc);
1351145247Sdamien}
1352145247Sdamien
1353145247Sdamienstatic void
1354191746Sthompsaipw_fatal_error_intr(struct ipw_softc *sc)
1355191746Sthompsa{
1356287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1357191956Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1358191746Sthompsa
1359191746Sthompsa	device_printf(sc->sc_dev, "firmware error\n");
1360206767Sbschmidt	if (vap != NULL) {
1361206767Sbschmidt		IPW_UNLOCK(sc);
1362191956Sthompsa		ieee80211_cancel_scan(vap);
1363206767Sbschmidt		IPW_LOCK(sc);
1364206767Sbschmidt	}
1365191746Sthompsa	ieee80211_runtask(ic, &sc->sc_init_task);
1366191746Sthompsa}
1367191746Sthompsa
1368191746Sthompsastatic void
1369145247Sdamienipw_intr(void *arg)
1370145247Sdamien{
1371145247Sdamien	struct ipw_softc *sc = arg;
1372145247Sdamien	uint32_t r;
1373145247Sdamien
1374172567Sthompsa	IPW_LOCK(sc);
1375145247Sdamien
1376191746Sthompsa	r = CSR_READ_4(sc, IPW_CSR_INTR);
1377191746Sthompsa	if (r == 0 || r == 0xffffffff)
1378191746Sthompsa		goto done;
1379145247Sdamien
1380145247Sdamien	/* disable interrupts */
1381145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1382145247Sdamien
1383156599Sdamien	/* acknowledge all interrupts */
1384156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1385156599Sdamien
1386145247Sdamien	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1387191746Sthompsa		ipw_fatal_error_intr(sc);
1388191746Sthompsa		goto done;
1389145247Sdamien	}
1390145247Sdamien
1391156599Sdamien	if (r & IPW_INTR_FW_INIT_DONE)
1392156599Sdamien		wakeup(sc);
1393145247Sdamien
1394145247Sdamien	if (r & IPW_INTR_RX_TRANSFER)
1395145247Sdamien		ipw_rx_intr(sc);
1396145247Sdamien
1397145247Sdamien	if (r & IPW_INTR_TX_TRANSFER)
1398145247Sdamien		ipw_tx_intr(sc);
1399145247Sdamien
1400145247Sdamien	/* re-enable interrupts */
1401145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1402191746Sthompsadone:
1403172567Sthompsa	IPW_UNLOCK(sc);
1404145247Sdamien}
1405145247Sdamien
1406145247Sdamienstatic void
1407145247Sdamienipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1408145247Sdamien{
1409145247Sdamien	if (error != 0)
1410145247Sdamien		return;
1411145247Sdamien
1412145247Sdamien	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1413145247Sdamien
1414145247Sdamien	*(bus_addr_t *)arg = segs[0].ds_addr;
1415145247Sdamien}
1416145247Sdamien
1417172567Sthompsastatic const char *
1418172567Sthompsaipw_cmdname(int cmd)
1419172567Sthompsa{
1420172567Sthompsa	static const struct {
1421172567Sthompsa		int	cmd;
1422172567Sthompsa		const char *name;
1423172567Sthompsa	} cmds[] = {
1424172567Sthompsa		{ IPW_CMD_ADD_MULTICAST,	"ADD_MULTICAST" },
1425172567Sthompsa		{ IPW_CMD_BROADCAST_SCAN,	"BROADCAST_SCAN" },
1426172567Sthompsa		{ IPW_CMD_DISABLE,		"DISABLE" },
1427172567Sthompsa		{ IPW_CMD_DISABLE_PHY,		"DISABLE_PHY" },
1428172567Sthompsa		{ IPW_CMD_ENABLE,		"ENABLE" },
1429172567Sthompsa		{ IPW_CMD_PREPARE_POWER_DOWN,	"PREPARE_POWER_DOWN" },
1430172567Sthompsa		{ IPW_CMD_SET_BASIC_TX_RATES,	"SET_BASIC_TX_RATES" },
1431172567Sthompsa		{ IPW_CMD_SET_BEACON_INTERVAL,	"SET_BEACON_INTERVAL" },
1432172567Sthompsa		{ IPW_CMD_SET_CHANNEL,		"SET_CHANNEL" },
1433172567Sthompsa		{ IPW_CMD_SET_CONFIGURATION,	"SET_CONFIGURATION" },
1434172567Sthompsa		{ IPW_CMD_SET_DESIRED_BSSID,	"SET_DESIRED_BSSID" },
1435172567Sthompsa		{ IPW_CMD_SET_ESSID,		"SET_ESSID" },
1436172567Sthompsa		{ IPW_CMD_SET_FRAG_THRESHOLD,	"SET_FRAG_THRESHOLD" },
1437172567Sthompsa		{ IPW_CMD_SET_MAC_ADDRESS,	"SET_MAC_ADDRESS" },
1438172567Sthompsa		{ IPW_CMD_SET_MANDATORY_BSSID,	"SET_MANDATORY_BSSID" },
1439172567Sthompsa		{ IPW_CMD_SET_MODE,		"SET_MODE" },
1440172567Sthompsa		{ IPW_CMD_SET_MSDU_TX_RATES,	"SET_MSDU_TX_RATES" },
1441172567Sthompsa		{ IPW_CMD_SET_POWER_MODE,	"SET_POWER_MODE" },
1442172567Sthompsa		{ IPW_CMD_SET_RTS_THRESHOLD,	"SET_RTS_THRESHOLD" },
1443172567Sthompsa		{ IPW_CMD_SET_SCAN_OPTIONS,	"SET_SCAN_OPTIONS" },
1444172567Sthompsa		{ IPW_CMD_SET_SECURITY_INFO,	"SET_SECURITY_INFO" },
1445172567Sthompsa		{ IPW_CMD_SET_TX_POWER_INDEX,	"SET_TX_POWER_INDEX" },
1446172567Sthompsa		{ IPW_CMD_SET_TX_RATES,		"SET_TX_RATES" },
1447172567Sthompsa		{ IPW_CMD_SET_WEP_FLAGS,	"SET_WEP_FLAGS" },
1448172567Sthompsa		{ IPW_CMD_SET_WEP_KEY,		"SET_WEP_KEY" },
1449172567Sthompsa		{ IPW_CMD_SET_WEP_KEY_INDEX,	"SET_WEP_KEY_INDEX" },
1450172567Sthompsa		{ IPW_CMD_SET_WPA_IE,		"SET_WPA_IE" },
1451172567Sthompsa
1452172567Sthompsa	};
1453172567Sthompsa	static char buf[12];
1454172567Sthompsa	int i;
1455172567Sthompsa
1456288087Sadrian	for (i = 0; i < nitems(cmds); i++)
1457172567Sthompsa		if (cmds[i].cmd == cmd)
1458172567Sthompsa			return cmds[i].name;
1459172567Sthompsa	snprintf(buf, sizeof(buf), "%u", cmd);
1460172567Sthompsa	return buf;
1461172567Sthompsa}
1462172567Sthompsa
1463145247Sdamien/*
1464145247Sdamien * Send a command to the firmware and wait for the acknowledgement.
1465145247Sdamien */
1466145247Sdamienstatic int
1467145247Sdamienipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1468145247Sdamien{
1469145247Sdamien	struct ipw_soft_bd *sbd;
1470145247Sdamien	bus_addr_t physaddr;
1471145247Sdamien	int error;
1472145247Sdamien
1473178354Ssam	IPW_LOCK_ASSERT(sc);
1474178354Ssam
1475172567Sthompsa	if (sc->flags & IPW_FLAG_BUSY) {
1476172567Sthompsa		device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1477172567Sthompsa			__func__, ipw_cmdname(type));
1478172567Sthompsa		return EAGAIN;
1479172567Sthompsa	}
1480172567Sthompsa	sc->flags |= IPW_FLAG_BUSY;
1481172567Sthompsa
1482145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1483145247Sdamien
1484145247Sdamien	error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1485145247Sdamien	    sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1486145247Sdamien	if (error != 0) {
1487145247Sdamien		device_printf(sc->sc_dev, "could not map command DMA memory\n");
1488172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1489145247Sdamien		return error;
1490145247Sdamien	}
1491145247Sdamien
1492145247Sdamien	sc->cmd.type = htole32(type);
1493145247Sdamien	sc->cmd.subtype = 0;
1494145247Sdamien	sc->cmd.len = htole32(len);
1495145247Sdamien	sc->cmd.seq = 0;
1496152637Sdamien	memcpy(sc->cmd.data, data, len);
1497145247Sdamien
1498145247Sdamien	sbd->type = IPW_SBD_TYPE_COMMAND;
1499145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1500145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1501145247Sdamien	sbd->bd->nfrag = 1;
1502145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1503145247Sdamien	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1504145247Sdamien
1505145247Sdamien	bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1506145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1507145247Sdamien
1508172567Sthompsa#ifdef IPW_DEBUG
1509172567Sthompsa	if (ipw_debug >= 4) {
1510172567Sthompsa		printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1511172567Sthompsa		    0, 0, len);
1512172567Sthompsa		/* Print the data buffer in the higher debug level */
1513172567Sthompsa		if (ipw_debug >= 9 && len > 0) {
1514172567Sthompsa			printf(" data: 0x");
1515172567Sthompsa			for (int i = 1; i <= len; i++)
1516174269Swkoszek				printf("%1D", (u_char *)data + len - i, "");
1517172567Sthompsa		}
1518172567Sthompsa		printf("\n");
1519172567Sthompsa	}
1520172567Sthompsa#endif
1521145247Sdamien
1522145247Sdamien	/* kick firmware */
1523145247Sdamien	sc->txfree--;
1524145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1525145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1526145247Sdamien
1527145247Sdamien	/* wait at most one second for command to complete */
1528172567Sthompsa	error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1529172567Sthompsa	if (error != 0) {
1530172567Sthompsa		device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1531172567Sthompsa		    __func__, ipw_cmdname(type), error);
1532172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1533172567Sthompsa		return (error);
1534172567Sthompsa	}
1535172567Sthompsa	return (0);
1536145247Sdamien}
1537145247Sdamien
1538145247Sdamienstatic int
1539287197Sglebiusipw_tx_start(struct ipw_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1540145247Sdamien{
1541287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1542192468Ssam	struct ieee80211vap *vap = ni->ni_vap;
1543145247Sdamien	struct ieee80211_frame *wh;
1544145247Sdamien	struct ipw_soft_bd *sbd;
1545145247Sdamien	struct ipw_soft_hdr *shdr;
1546145247Sdamien	struct ipw_soft_buf *sbuf;
1547145247Sdamien	struct ieee80211_key *k;
1548145247Sdamien	struct mbuf *mnew;
1549145247Sdamien	bus_dma_segment_t segs[IPW_MAX_NSEG];
1550145247Sdamien	bus_addr_t physaddr;
1551145247Sdamien	int nsegs, error, i;
1552145247Sdamien
1553145247Sdamien	wh = mtod(m0, struct ieee80211_frame *);
1554145247Sdamien
1555260444Skevlo	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1556178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1557147806Ssam		if (k == NULL) {
1558147806Ssam			m_freem(m0);
1559145247Sdamien			return ENOBUFS;
1560147806Ssam		}
1561145247Sdamien		/* packet header may have moved, reset our local pointer */
1562145247Sdamien		wh = mtod(m0, struct ieee80211_frame *);
1563145247Sdamien	}
1564145247Sdamien
1565192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1566145247Sdamien		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1567145247Sdamien
1568145247Sdamien		tap->wt_flags = 0;
1569145247Sdamien
1570192468Ssam		ieee80211_radiotap_tx(vap, m0);
1571145247Sdamien	}
1572145247Sdamien
1573145247Sdamien	shdr = SLIST_FIRST(&sc->free_shdr);
1574145247Sdamien	sbuf = SLIST_FIRST(&sc->free_sbuf);
1575145247Sdamien	KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1576145247Sdamien
1577145247Sdamien	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1578145247Sdamien	shdr->hdr.subtype = 0;
1579260444Skevlo	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) ? 1 : 0;
1580145247Sdamien	shdr->hdr.encrypt = 0;
1581145247Sdamien	shdr->hdr.keyidx = 0;
1582145247Sdamien	shdr->hdr.keysz = 0;
1583145247Sdamien	shdr->hdr.fragmentsz = 0;
1584145247Sdamien	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1585145247Sdamien	if (ic->ic_opmode == IEEE80211_M_STA)
1586145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1587145247Sdamien	else
1588145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1589145247Sdamien
1590145247Sdamien	/* trim IEEE802.11 header */
1591145247Sdamien	m_adj(m0, sizeof (struct ieee80211_frame));
1592145247Sdamien
1593145247Sdamien	error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1594145247Sdamien	    &nsegs, 0);
1595145247Sdamien	if (error != 0 && error != EFBIG) {
1596145247Sdamien		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1597145247Sdamien		    error);
1598145247Sdamien		m_freem(m0);
1599145247Sdamien		return error;
1600145247Sdamien	}
1601145247Sdamien	if (error != 0) {
1602243857Sglebius		mnew = m_defrag(m0, M_NOWAIT);
1603145247Sdamien		if (mnew == NULL) {
1604145247Sdamien			device_printf(sc->sc_dev,
1605145247Sdamien			    "could not defragment mbuf\n");
1606145247Sdamien			m_freem(m0);
1607145247Sdamien			return ENOBUFS;
1608145247Sdamien		}
1609145247Sdamien		m0 = mnew;
1610145247Sdamien
1611145247Sdamien		error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1612145247Sdamien		    segs, &nsegs, 0);
1613145247Sdamien		if (error != 0) {
1614145247Sdamien			device_printf(sc->sc_dev,
1615145247Sdamien			    "could not map mbuf (error %d)\n", error);
1616145247Sdamien			m_freem(m0);
1617145247Sdamien			return error;
1618145247Sdamien		}
1619145247Sdamien	}
1620145247Sdamien
1621145247Sdamien	error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1622145247Sdamien	    sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1623145247Sdamien	if (error != 0) {
1624145247Sdamien		device_printf(sc->sc_dev, "could not map header DMA memory\n");
1625145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1626145247Sdamien		m_freem(m0);
1627145247Sdamien		return error;
1628145247Sdamien	}
1629145247Sdamien
1630145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1631145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1632145247Sdamien
1633145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1634145247Sdamien	sbd->type = IPW_SBD_TYPE_HEADER;
1635145247Sdamien	sbd->priv = shdr;
1636145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1637145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1638145247Sdamien	sbd->bd->nfrag = 1 + nsegs;
1639145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1640145247Sdamien	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1641145247Sdamien
1642145247Sdamien	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1643145247Sdamien	    shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1644145247Sdamien	    shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1645145247Sdamien	    ":"));
1646145247Sdamien
1647145247Sdamien	sc->txfree--;
1648145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1649145247Sdamien
1650145247Sdamien	sbuf->m = m0;
1651145247Sdamien	sbuf->ni = ni;
1652145247Sdamien
1653145247Sdamien	for (i = 0; i < nsegs; i++) {
1654145247Sdamien		sbd = &sc->stbd_list[sc->txcur];
1655145247Sdamien
1656145247Sdamien		sbd->bd->physaddr = htole32(segs[i].ds_addr);
1657145247Sdamien		sbd->bd->len = htole32(segs[i].ds_len);
1658145247Sdamien		sbd->bd->nfrag = 0;
1659145247Sdamien		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1660145247Sdamien		if (i == nsegs - 1) {
1661145247Sdamien			sbd->type = IPW_SBD_TYPE_DATA;
1662145247Sdamien			sbd->priv = sbuf;
1663145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1664145247Sdamien		} else {
1665145247Sdamien			sbd->type = IPW_SBD_TYPE_NOASSOC;
1666145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1667145247Sdamien		}
1668145247Sdamien
1669172574Sthompsa		DPRINTFN(5, ("sending fragment (%d)\n", i));
1670145247Sdamien
1671145247Sdamien		sc->txfree--;
1672145247Sdamien		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1673145247Sdamien	}
1674145247Sdamien
1675145247Sdamien	bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1676145247Sdamien	bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1677145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1678145247Sdamien
1679145247Sdamien	/* kick firmware */
1680145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1681145247Sdamien
1682145247Sdamien	return 0;
1683145247Sdamien}
1684145247Sdamien
1685178354Ssamstatic int
1686178354Ssamipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1687178354Ssam	const struct ieee80211_bpf_params *params)
1688178354Ssam{
1689178354Ssam	/* no support; just discard */
1690178354Ssam	m_freem(m);
1691178354Ssam	ieee80211_free_node(ni);
1692178354Ssam	return 0;
1693178354Ssam}
1694178354Ssam
1695287197Sglebiusstatic int
1696287197Sglebiusipw_transmit(struct ieee80211com *ic, struct mbuf *m)
1697145247Sdamien{
1698287197Sglebius	struct ipw_softc *sc = ic->ic_softc;
1699287197Sglebius	int error;
1700172567Sthompsa
1701172567Sthompsa	IPW_LOCK(sc);
1702287197Sglebius	if ((sc->flags & IPW_FLAG_RUNNING) == 0) {
1703287197Sglebius		IPW_UNLOCK(sc);
1704287197Sglebius		return (ENXIO);
1705287197Sglebius	}
1706287197Sglebius	error = mbufq_enqueue(&sc->sc_snd, m);
1707287197Sglebius	if (error) {
1708287197Sglebius		IPW_UNLOCK(sc);
1709287197Sglebius		return (error);
1710287197Sglebius	}
1711287197Sglebius	ipw_start(sc);
1712172567Sthompsa	IPW_UNLOCK(sc);
1713287197Sglebius	return (0);
1714172567Sthompsa}
1715172567Sthompsa
1716172567Sthompsastatic void
1717287197Sglebiusipw_start(struct ipw_softc *sc)
1718172567Sthompsa{
1719145247Sdamien	struct ieee80211_node *ni;
1720178354Ssam	struct mbuf *m;
1721145247Sdamien
1722172567Sthompsa	IPW_LOCK_ASSERT(sc);
1723145247Sdamien
1724287197Sglebius	while (sc->txfree < 1 + IPW_MAX_NSEG &&
1725287197Sglebius	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1726178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1727287197Sglebius		if (ipw_tx_start(sc, m, ni) != 0) {
1728287197Sglebius			if_inc_counter(ni->ni_vap->iv_ifp,
1729287197Sglebius			    IFCOUNTER_OERRORS, 1);
1730145247Sdamien			ieee80211_free_node(ni);
1731145247Sdamien			break;
1732145247Sdamien		}
1733145247Sdamien		/* start watchdog timer */
1734145247Sdamien		sc->sc_tx_timer = 5;
1735145247Sdamien	}
1736145247Sdamien}
1737145247Sdamien
1738145247Sdamienstatic void
1739172567Sthompsaipw_watchdog(void *arg)
1740145247Sdamien{
1741172567Sthompsa	struct ipw_softc *sc = arg;
1742287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1743145247Sdamien
1744172567Sthompsa	IPW_LOCK_ASSERT(sc);
1745156599Sdamien
1746145247Sdamien	if (sc->sc_tx_timer > 0) {
1747145247Sdamien		if (--sc->sc_tx_timer == 0) {
1748287197Sglebius			device_printf(sc->sc_dev, "device timeout\n");
1749287197Sglebius			counter_u64_add(ic->ic_oerrors, 1);
1750178354Ssam			taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1751145247Sdamien		}
1752145247Sdamien	}
1753172567Sthompsa	if (sc->sc_scan_timer > 0) {
1754172567Sthompsa		if (--sc->sc_scan_timer == 0) {
1755172567Sthompsa			DPRINTFN(3, ("Scan timeout\n"));
1756172567Sthompsa			/* End the scan */
1757172567Sthompsa			if (sc->flags & IPW_FLAG_SCANNING) {
1758206767Sbschmidt				IPW_UNLOCK(sc);
1759178354Ssam				ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1760206767Sbschmidt				IPW_LOCK(sc);
1761172567Sthompsa				sc->flags &= ~IPW_FLAG_SCANNING;
1762172567Sthompsa			}
1763172567Sthompsa		}
1764172567Sthompsa	}
1765287197Sglebius	if (sc->flags & IPW_FLAG_RUNNING)
1766172567Sthompsa		callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1767145247Sdamien}
1768145247Sdamien
1769287197Sglebiusstatic void
1770287197Sglebiusipw_parent(struct ieee80211com *ic)
1771145247Sdamien{
1772287197Sglebius	struct ipw_softc *sc = ic->ic_softc;
1773287197Sglebius	int startall = 0;
1774145247Sdamien
1775287197Sglebius	IPW_LOCK(sc);
1776287197Sglebius	if (ic->ic_nrunning > 0) {
1777287197Sglebius		if (!(sc->flags & IPW_FLAG_RUNNING)) {
1778287197Sglebius			ipw_init_locked(sc);
1779287197Sglebius			startall = 1;
1780145247Sdamien		}
1781287197Sglebius	} else if (sc->flags & IPW_FLAG_RUNNING)
1782287197Sglebius		ipw_stop_locked(sc);
1783287197Sglebius	IPW_UNLOCK(sc);
1784287197Sglebius	if (startall)
1785287197Sglebius		ieee80211_start_all(ic);
1786145247Sdamien}
1787145247Sdamien
1788145247Sdamienstatic void
1789145247Sdamienipw_stop_master(struct ipw_softc *sc)
1790145247Sdamien{
1791156599Sdamien	uint32_t tmp;
1792145247Sdamien	int ntries;
1793145247Sdamien
1794145247Sdamien	/* disable interrupts */
1795145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1796145247Sdamien
1797145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1798145247Sdamien	for (ntries = 0; ntries < 50; ntries++) {
1799145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1800145247Sdamien			break;
1801145247Sdamien		DELAY(10);
1802145247Sdamien	}
1803145247Sdamien	if (ntries == 50)
1804145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for master\n");
1805145247Sdamien
1806156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1807156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1808145247Sdamien
1809172567Sthompsa	/* Clear all flags except the following */
1810172567Sthompsa	sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1811145247Sdamien}
1812145247Sdamien
1813145247Sdamienstatic int
1814145247Sdamienipw_reset(struct ipw_softc *sc)
1815145247Sdamien{
1816156599Sdamien	uint32_t tmp;
1817145247Sdamien	int ntries;
1818145247Sdamien
1819145247Sdamien	ipw_stop_master(sc);
1820145247Sdamien
1821145247Sdamien	/* move adapter to D0 state */
1822156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1823156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1824145247Sdamien
1825145247Sdamien	/* wait for clock stabilization */
1826145247Sdamien	for (ntries = 0; ntries < 1000; ntries++) {
1827145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1828145247Sdamien			break;
1829145247Sdamien		DELAY(200);
1830145247Sdamien	}
1831145247Sdamien	if (ntries == 1000)
1832145247Sdamien		return EIO;
1833145247Sdamien
1834156599Sdamien	tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1835156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1836145247Sdamien
1837145247Sdamien	DELAY(10);
1838145247Sdamien
1839156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1840156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1841145247Sdamien
1842145247Sdamien	return 0;
1843145247Sdamien}
1844145247Sdamien
1845172567Sthompsastatic int
1846172567Sthompsaipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1847172567Sthompsa{
1848172567Sthompsa	int ms = hz < 1000 ? 1 : hz/10;
1849172567Sthompsa	int i, error;
1850172567Sthompsa
1851172567Sthompsa	for (i = 0; i < 100; i++) {
1852172567Sthompsa		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1853172567Sthompsa			return 0;
1854172567Sthompsa		error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1855172567Sthompsa		if (error == 0 || error != EWOULDBLOCK)
1856172567Sthompsa			return 0;
1857172567Sthompsa	}
1858172567Sthompsa	DPRINTF(("%s: timeout waiting for %s\n",
1859172567Sthompsa		__func__, waitfor ? "disable" : "enable"));
1860172567Sthompsa	return ETIMEDOUT;
1861172567Sthompsa}
1862172567Sthompsa
1863172567Sthompsastatic int
1864172567Sthompsaipw_enable(struct ipw_softc *sc)
1865172567Sthompsa{
1866172567Sthompsa	int error;
1867172567Sthompsa
1868172567Sthompsa	if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1869172567Sthompsa		DPRINTF(("Enable adapter\n"));
1870172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1871172567Sthompsa		if (error != 0)
1872172567Sthompsa			return error;
1873172567Sthompsa		error = ipw_waitfordisable(sc, 0);
1874172567Sthompsa		if (error != 0)
1875172567Sthompsa			return error;
1876172567Sthompsa		sc->flags |= IPW_FLAG_ENABLED;
1877172567Sthompsa	}
1878172567Sthompsa	return 0;
1879172567Sthompsa}
1880172567Sthompsa
1881172567Sthompsastatic int
1882172567Sthompsaipw_disable(struct ipw_softc *sc)
1883172567Sthompsa{
1884172567Sthompsa	int error;
1885172567Sthompsa
1886172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
1887172567Sthompsa		DPRINTF(("Disable adapter\n"));
1888172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1889172567Sthompsa		if (error != 0)
1890172567Sthompsa			return error;
1891172567Sthompsa		error = ipw_waitfordisable(sc, 1);
1892172567Sthompsa		if (error != 0)
1893172567Sthompsa			return error;
1894172567Sthompsa		sc->flags &= ~IPW_FLAG_ENABLED;
1895172567Sthompsa	}
1896172567Sthompsa	return 0;
1897172567Sthompsa}
1898172567Sthompsa
1899145247Sdamien/*
1900145247Sdamien * Upload the microcode to the device.
1901145247Sdamien */
1902145247Sdamienstatic int
1903156599Sdamienipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1904145247Sdamien{
1905145247Sdamien	int ntries;
1906145247Sdamien
1907145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1908145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1909145247Sdamien
1910145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1911145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1912145247Sdamien
1913145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1914145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1915145247Sdamien
1916145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1917145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1918145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1919145247Sdamien
1920145247Sdamien	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1921145247Sdamien
1922145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1923145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1924145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1925145247Sdamien
1926145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1927145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1928145247Sdamien
1929145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1930145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1931145247Sdamien
1932145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1933145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1934145247Sdamien
1935145247Sdamien	for (ntries = 0; ntries < 10; ntries++) {
1936145247Sdamien		if (MEM_READ_1(sc, 0x210000) & 1)
1937145247Sdamien			break;
1938145247Sdamien		DELAY(10);
1939145247Sdamien	}
1940145247Sdamien	if (ntries == 10) {
1941145247Sdamien		device_printf(sc->sc_dev,
1942145247Sdamien		    "timeout waiting for ucode to initialize\n");
1943145247Sdamien		return EIO;
1944145247Sdamien	}
1945145247Sdamien
1946145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0);
1947145247Sdamien
1948145247Sdamien	return 0;
1949145247Sdamien}
1950145247Sdamien
1951145247Sdamien/* set of macros to handle unaligned little endian data in firmware image */
1952145247Sdamien#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1953145247Sdamien#define GETLE16(p) ((p)[0] | (p)[1] << 8)
1954145247Sdamienstatic int
1955156599Sdamienipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
1956145247Sdamien{
1957156599Sdamien	const uint8_t *p, *end;
1958156599Sdamien	uint32_t tmp, dst;
1959145247Sdamien	uint16_t len;
1960145247Sdamien	int error;
1961145247Sdamien
1962145247Sdamien	p = fw;
1963145247Sdamien	end = fw + size;
1964145247Sdamien	while (p < end) {
1965145247Sdamien		dst = GETLE32(p); p += 4;
1966145247Sdamien		len = GETLE16(p); p += 2;
1967145247Sdamien
1968145247Sdamien		ipw_write_mem_1(sc, dst, p, len);
1969145247Sdamien		p += len;
1970145247Sdamien	}
1971145247Sdamien
1972145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
1973145247Sdamien	    IPW_IO_LED_OFF);
1974145247Sdamien
1975145247Sdamien	/* enable interrupts */
1976145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1977145247Sdamien
1978145247Sdamien	/* kick the firmware */
1979145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1980145247Sdamien
1981156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1982156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
1983145247Sdamien
1984145247Sdamien	/* wait at most one second for firmware initialization to complete */
1985145247Sdamien	if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
1986145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for firmware "
1987145247Sdamien		    "initialization to complete\n");
1988145247Sdamien		return error;
1989145247Sdamien	}
1990145247Sdamien
1991156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_IO);
1992156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
1993156599Sdamien	    IPW_IO_GPIO3_MASK);
1994145247Sdamien
1995145247Sdamien	return 0;
1996145247Sdamien}
1997145247Sdamien
1998145247Sdamienstatic int
1999172567Sthompsaipw_setwepkeys(struct ipw_softc *sc)
2000172567Sthompsa{
2001287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2002178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2003172567Sthompsa	struct ipw_wep_key wepkey;
2004172567Sthompsa	struct ieee80211_key *wk;
2005172567Sthompsa	int error, i;
2006172567Sthompsa
2007172567Sthompsa	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2008178354Ssam		wk = &vap->iv_nw_keys[i];
2009172567Sthompsa
2010172567Sthompsa		if (wk->wk_cipher == NULL ||
2011172567Sthompsa		    wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2012172567Sthompsa			continue;
2013172567Sthompsa
2014172567Sthompsa		wepkey.idx = i;
2015172567Sthompsa		wepkey.len = wk->wk_keylen;
2016172567Sthompsa		memset(wepkey.key, 0, sizeof wepkey.key);
2017172567Sthompsa		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2018172567Sthompsa		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2019172567Sthompsa		    wepkey.len));
2020172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2021172567Sthompsa		    sizeof wepkey);
2022172567Sthompsa		if (error != 0)
2023172567Sthompsa			return error;
2024172567Sthompsa	}
2025172567Sthompsa	return 0;
2026172567Sthompsa}
2027172567Sthompsa
2028172567Sthompsastatic int
2029172567Sthompsaipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2030172567Sthompsa{
2031172567Sthompsa	struct ipw_wpa_ie wpaie;
2032172567Sthompsa
2033172567Sthompsa	memset(&wpaie, 0, sizeof(wpaie));
2034172567Sthompsa	wpaie.len = htole32(ielen);
2035172567Sthompsa	/* XXX verify length */
2036172567Sthompsa	memcpy(&wpaie.ie, ie, ielen);
2037172567Sthompsa	DPRINTF(("Setting WPA IE\n"));
2038172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2039172567Sthompsa}
2040172567Sthompsa
2041172567Sthompsastatic int
2042172567Sthompsaipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2043172567Sthompsa{
2044172567Sthompsa	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2045172567Sthompsa
2046172567Sthompsa	if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2047172567Sthompsa		DPRINTF(("Setting mandatory BSSID to null\n"));
2048172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2049172567Sthompsa	} else {
2050172567Sthompsa		DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2051172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2052172567Sthompsa			bssid, IEEE80211_ADDR_LEN);
2053172567Sthompsa	}
2054172567Sthompsa}
2055172567Sthompsa
2056172567Sthompsastatic int
2057172567Sthompsaipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2058172567Sthompsa{
2059172567Sthompsa	if (ssidlen == 0) {
2060172567Sthompsa		/*
2061172567Sthompsa		 * A bug in the firmware breaks the ``don't associate''
2062172567Sthompsa		 * bit in the scan options command.  To compensate for
2063172567Sthompsa		 * this install a bogus ssid when no ssid is specified
2064172567Sthompsa		 * so the firmware won't try to associate.
2065172567Sthompsa		 */
2066172567Sthompsa		DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2067172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2068172567Sthompsa			"\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2069172567Sthompsa			"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2070172567Sthompsa			"\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2071172567Sthompsa			"\x3c\x3d", IEEE80211_NWID_LEN);
2072172567Sthompsa	} else {
2073172567Sthompsa#ifdef IPW_DEBUG
2074172567Sthompsa		if (ipw_debug > 0) {
2075172567Sthompsa			printf("Setting ESSID to ");
2076172567Sthompsa			ieee80211_print_essid(ssid, ssidlen);
2077172567Sthompsa			printf("\n");
2078172567Sthompsa		}
2079172567Sthompsa#endif
2080172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2081172567Sthompsa	}
2082172567Sthompsa}
2083172567Sthompsa
2084172567Sthompsastatic int
2085172567Sthompsaipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2086172567Sthompsa{
2087172567Sthompsa	struct ipw_scan_options opts;
2088172567Sthompsa
2089172567Sthompsa	DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2090172567Sthompsa	opts.channels = htole32(chanmask);
2091172567Sthompsa	opts.flags = htole32(flags);
2092172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2093172567Sthompsa}
2094172567Sthompsa
2095172567Sthompsastatic int
2096172567Sthompsaipw_scan(struct ipw_softc *sc)
2097172567Sthompsa{
2098172567Sthompsa	uint32_t params;
2099172567Sthompsa	int error;
2100172567Sthompsa
2101172567Sthompsa	DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2102172567Sthompsa
2103172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
2104172567Sthompsa		return (EBUSY);
2105172567Sthompsa	sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2106172567Sthompsa
2107172567Sthompsa	/* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2108172567Sthompsa	error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2109172567Sthompsa	if (error != 0)
2110172567Sthompsa		goto done;
2111172567Sthompsa
2112172567Sthompsa	/*
2113172567Sthompsa	 * Setup null/bogus ssid so firmware doesn't use any previous
2114172567Sthompsa	 * ssid to try and associate.  This is because the ``don't
2115172567Sthompsa	 * associate'' option bit is broken (sigh).
2116172567Sthompsa	 */
2117172567Sthompsa	error = ipw_setssid(sc, NULL, 0);
2118172567Sthompsa	if (error != 0)
2119172567Sthompsa		goto done;
2120172567Sthompsa
2121172567Sthompsa	/*
2122172567Sthompsa	 * NB: the adapter may be disabled on association lost;
2123172567Sthompsa	 *     if so just re-enable it to kick off scanning.
2124172567Sthompsa	 */
2125172567Sthompsa	DPRINTF(("Starting scan\n"));
2126172567Sthompsa	sc->sc_scan_timer = 3;
2127172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
2128172567Sthompsa		params = 0;				/* XXX? */
2129172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2130172567Sthompsa				&params, sizeof(params));
2131172567Sthompsa	} else
2132172567Sthompsa		error = ipw_enable(sc);
2133172567Sthompsadone:
2134172567Sthompsa	if (error != 0) {
2135172567Sthompsa		DPRINTF(("Scan failed\n"));
2136172567Sthompsa		sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2137172567Sthompsa	}
2138172567Sthompsa	return (error);
2139172567Sthompsa}
2140172567Sthompsa
2141172567Sthompsastatic int
2142172567Sthompsaipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2143172567Sthompsa{
2144287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2145172567Sthompsa	uint32_t data;
2146172567Sthompsa	int error;
2147172567Sthompsa
2148172567Sthompsa	data = htole32(ieee80211_chan2ieee(ic, chan));
2149172567Sthompsa	DPRINTF(("Setting channel to %u\n", le32toh(data)));
2150172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2151172567Sthompsa	if (error == 0)
2152172567Sthompsa		ipw_setcurchan(sc, chan);
2153172567Sthompsa	return error;
2154172567Sthompsa}
2155172567Sthompsa
2156178354Ssamstatic void
2157191746Sthompsaipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2158145247Sdamien{
2159286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2160178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2161145247Sdamien	struct ipw_security security;
2162145247Sdamien	uint32_t data;
2163172567Sthompsa	int error;
2164145247Sdamien
2165178354Ssam	IPW_LOCK(sc);
2166172567Sthompsa	error = ipw_disable(sc);
2167172567Sthompsa	if (error != 0)
2168178354Ssam		goto done;
2169172567Sthompsa
2170152637Sdamien	memset(&security, 0, sizeof security);
2171178354Ssam	security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2172145247Sdamien	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2173145247Sdamien	security.ciphers = htole32(IPW_CIPHER_NONE);
2174145247Sdamien	DPRINTF(("Setting authmode to %u\n", security.authmode));
2175172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2176145247Sdamien	    sizeof security);
2177145247Sdamien	if (error != 0)
2178178354Ssam		goto done;
2179145247Sdamien
2180178354Ssam	data = htole32(vap->iv_rtsthreshold);
2181178354Ssam	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2182178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2183145247Sdamien	if (error != 0)
2184178354Ssam		goto done;
2185145247Sdamien
2186178354Ssam	data = htole32(vap->iv_fragthreshold);
2187178354Ssam	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2188178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2189145247Sdamien	if (error != 0)
2190178354Ssam		goto done;
2191145247Sdamien
2192178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2193172567Sthompsa		error = ipw_setwepkeys(sc);
2194172567Sthompsa		if (error != 0)
2195178354Ssam			goto done;
2196172567Sthompsa
2197178354Ssam		if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2198178354Ssam			data = htole32(vap->iv_def_txkey);
2199172567Sthompsa			DPRINTF(("Setting wep tx key index to %u\n",
2200172567Sthompsa				le32toh(data)));
2201172567Sthompsa			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2202172567Sthompsa			    sizeof data);
2203172567Sthompsa			if (error != 0)
2204178354Ssam				goto done;
2205172567Sthompsa		}
2206172567Sthompsa	}
2207172567Sthompsa
2208178354Ssam	data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2209172567Sthompsa	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2210172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2211172567Sthompsa	if (error != 0)
2212178354Ssam		goto done;
2213172567Sthompsa
2214172567Sthompsa	error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2215172567Sthompsa	if (error != 0)
2216178354Ssam		goto done;
2217172567Sthompsa
2218172567Sthompsa	error = ipw_setbssid(sc, ni->ni_bssid);
2219172567Sthompsa	if (error != 0)
2220178354Ssam		goto done;
2221172567Sthompsa
2222206766Sbschmidt	if (vap->iv_appie_wpa != NULL) {
2223206766Sbschmidt		struct ieee80211_appie *ie = vap->iv_appie_wpa;
2224178354Ssam		error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2225172567Sthompsa		if (error != 0)
2226178354Ssam			goto done;
2227172567Sthompsa	}
2228172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2229172567Sthompsa		error = ipw_setchannel(sc, ni->ni_chan);
2230172567Sthompsa		if (error != 0)
2231178354Ssam			goto done;
2232172567Sthompsa	}
2233172567Sthompsa
2234172567Sthompsa	/* lock scan to ap's channel and enable associate */
2235172567Sthompsa	error = ipw_setscanopts(sc,
2236178354Ssam	    1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2237178354Ssam	if (error != 0)
2238178354Ssam		goto done;
2239172567Sthompsa
2240178354Ssam	error = ipw_enable(sc);		/* finally, enable adapter */
2241178354Ssam	if (error == 0)
2242178354Ssam		sc->flags |= IPW_FLAG_ASSOCIATING;
2243178354Ssamdone:
2244178354Ssam	IPW_UNLOCK(sc);
2245172567Sthompsa}
2246172567Sthompsa
2247172567Sthompsastatic void
2248191746Sthompsaipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2249172567Sthompsa{
2250178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2251286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2252172567Sthompsa
2253172567Sthompsa	IPW_LOCK(sc);
2254172567Sthompsa	DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2255172567Sthompsa	/*
2256172567Sthompsa	 * NB: don't try to do this if ipw_stop_master has
2257172567Sthompsa	 *     shutdown the firmware and disabled interrupts.
2258172567Sthompsa	 */
2259178354Ssam	if (sc->flags & IPW_FLAG_FW_INITED) {
2260178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATED;
2261178354Ssam		/*
2262178354Ssam		 * NB: firmware currently ignores bssid parameter, but
2263178354Ssam		 *     supply it in case this changes (follow linux driver).
2264178354Ssam		 */
2265178354Ssam		(void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2266178354Ssam			ni->ni_bssid, IEEE80211_ADDR_LEN);
2267178354Ssam	}
2268178354Ssam	IPW_UNLOCK(sc);
2269172567Sthompsa}
2270172567Sthompsa
2271172567Sthompsa/*
2272156599Sdamien * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2273156599Sdamien * It is called on firmware panics or on watchdog timeouts.
2274156599Sdamien */
2275145247Sdamienstatic void
2276156599Sdamienipw_init_task(void *context, int pending)
2277156599Sdamien{
2278156599Sdamien	ipw_init(context);
2279156599Sdamien}
2280156599Sdamien
2281156599Sdamienstatic void
2282145247Sdamienipw_init(void *priv)
2283145247Sdamien{
2284145247Sdamien	struct ipw_softc *sc = priv;
2285287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2286172567Sthompsa
2287172567Sthompsa	IPW_LOCK(sc);
2288178354Ssam	ipw_init_locked(sc);
2289172567Sthompsa	IPW_UNLOCK(sc);
2290178354Ssam
2291287197Sglebius	if (sc->flags & IPW_FLAG_RUNNING)
2292178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2293172567Sthompsa}
2294172567Sthompsa
2295172567Sthompsastatic void
2296178354Ssamipw_init_locked(struct ipw_softc *sc)
2297172567Sthompsa{
2298287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2299178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2300166756Sluigi	const struct firmware *fp;
2301156599Sdamien	const struct ipw_firmware_hdr *hdr;
2302178354Ssam	const char *fw;
2303145247Sdamien
2304172567Sthompsa	IPW_LOCK_ASSERT(sc);
2305156599Sdamien
2306172567Sthompsa	DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2307178354Ssam		ieee80211_state_name[vap->iv_state], sc->flags));
2308172567Sthompsa
2309156599Sdamien	/*
2310156599Sdamien	 * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2311156599Sdamien	 * when loading the firmware and we don't want to be called during this
2312156599Sdamien	 * operation.
2313156599Sdamien	 */
2314172567Sthompsa	if (sc->flags & IPW_FLAG_INIT_LOCKED)
2315145247Sdamien		return;
2316156599Sdamien	sc->flags |= IPW_FLAG_INIT_LOCKED;
2317145247Sdamien
2318172567Sthompsa	ipw_stop_locked(sc);
2319145247Sdamien
2320145247Sdamien	if (ipw_reset(sc) != 0) {
2321145247Sdamien		device_printf(sc->sc_dev, "could not reset adapter\n");
2322178354Ssam		goto fail;
2323145247Sdamien	}
2324145247Sdamien
2325159487Siedowse	if (sc->sc_firmware == NULL) {
2326178354Ssam		device_printf(sc->sc_dev, "no firmware\n");
2327178354Ssam		goto fail;
2328156599Sdamien	}
2329178354Ssam	/* NB: consistency already checked on load */
2330159487Siedowse	fp = sc->sc_firmware;
2331156599Sdamien	hdr = (const struct ipw_firmware_hdr *)fp->data;
2332156599Sdamien
2333178354Ssam	DPRINTF(("Loading firmware image '%s'\n", fp->name));
2334156599Sdamien	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2335156599Sdamien	if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2336145247Sdamien		device_printf(sc->sc_dev, "could not load microcode\n");
2337178354Ssam		goto fail;
2338145247Sdamien	}
2339145247Sdamien
2340145247Sdamien	ipw_stop_master(sc);
2341145247Sdamien
2342145247Sdamien	/*
2343145247Sdamien	 * Setup tx, rx and status rings.
2344145247Sdamien	 */
2345145247Sdamien	sc->txold = IPW_NTBD - 1;
2346145247Sdamien	sc->txcur = 0;
2347145247Sdamien	sc->txfree = IPW_NTBD - 2;
2348145247Sdamien	sc->rxcur = IPW_NRBD - 1;
2349145247Sdamien
2350145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2351145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2352145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2353145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2354145247Sdamien
2355145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2356145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2357145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2358145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2359145247Sdamien
2360145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2361145247Sdamien
2362156599Sdamien	fw = (const char *)fp->data + sizeof *hdr;
2363156599Sdamien	if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2364145247Sdamien		device_printf(sc->sc_dev, "could not load firmware\n");
2365178354Ssam		goto fail;
2366145247Sdamien	}
2367145247Sdamien
2368145247Sdamien	sc->flags |= IPW_FLAG_FW_INITED;
2369145247Sdamien
2370145247Sdamien	/* retrieve information tables base addresses */
2371145247Sdamien	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2372145247Sdamien	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2373145247Sdamien
2374145247Sdamien	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2375145247Sdamien
2376145247Sdamien	if (ipw_config(sc) != 0) {
2377145247Sdamien		device_printf(sc->sc_dev, "device configuration failed\n");
2378178354Ssam		goto fail;
2379145247Sdamien	}
2380145247Sdamien
2381172567Sthompsa	callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2382287197Sglebius	sc->flags |= IPW_FLAG_RUNNING;
2383287197Sglebius	sc->flags &= ~IPW_FLAG_INIT_LOCKED;
2384145247Sdamien	return;
2385145247Sdamien
2386178354Ssamfail:
2387172567Sthompsa	ipw_stop_locked(sc);
2388287197Sglebius	sc->flags &= ~IPW_FLAG_INIT_LOCKED;
2389145247Sdamien}
2390145247Sdamien
2391178354Ssamstatic int
2392178354Ssamipw_config(struct ipw_softc *sc)
2393178354Ssam{
2394287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2395178354Ssam	struct ipw_configuration config;
2396178354Ssam	uint32_t data;
2397178354Ssam	int error;
2398178354Ssam
2399178354Ssam	error = ipw_disable(sc);
2400178354Ssam	if (error != 0)
2401178354Ssam		return error;
2402178354Ssam
2403178354Ssam	switch (ic->ic_opmode) {
2404178354Ssam	case IEEE80211_M_STA:
2405178354Ssam	case IEEE80211_M_HOSTAP:
2406178354Ssam	case IEEE80211_M_WDS:		/* XXX */
2407178354Ssam		data = htole32(IPW_MODE_BSS);
2408178354Ssam		break;
2409178354Ssam	case IEEE80211_M_IBSS:
2410178354Ssam	case IEEE80211_M_AHDEMO:
2411178354Ssam		data = htole32(IPW_MODE_IBSS);
2412178354Ssam		break;
2413178354Ssam	case IEEE80211_M_MONITOR:
2414178354Ssam		data = htole32(IPW_MODE_MONITOR);
2415178354Ssam		break;
2416195562Srpaulo	default:
2417195562Srpaulo		device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2418195562Srpaulo		return EINVAL;
2419178354Ssam	}
2420178354Ssam	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2421178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2422178354Ssam	if (error != 0)
2423178354Ssam		return error;
2424178354Ssam
2425178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2426178354Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2427178354Ssam		error = ipw_setchannel(sc, ic->ic_curchan);
2428178354Ssam		if (error != 0)
2429178354Ssam			return error;
2430178354Ssam	}
2431178354Ssam
2432178354Ssam	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2433178354Ssam		return ipw_enable(sc);
2434178354Ssam
2435178354Ssam	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2436178354Ssam	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2437178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
2438178354Ssam		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2439287197Sglebius	if (ic->ic_promisc > 0)
2440178354Ssam		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2441178354Ssam	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2442178354Ssam	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2443178354Ssam	DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2444178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2445178354Ssam	if (error != 0)
2446178354Ssam		return error;
2447178354Ssam
2448207926Sbschmidt	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2449178354Ssam	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2450178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2451178354Ssam	if (error != 0)
2452178354Ssam		return error;
2453178354Ssam
2454207926Sbschmidt	/* Use the same rate set */
2455178354Ssam	DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2456178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2457178354Ssam	if (error != 0)
2458178354Ssam		return error;
2459178354Ssam
2460207926Sbschmidt	/* Use the same rate set */
2461178354Ssam	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2462178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2463178354Ssam	if (error != 0)
2464178354Ssam		return error;
2465178354Ssam
2466178354Ssam	data = htole32(IPW_POWER_MODE_CAM);
2467178354Ssam	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2468178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2469178354Ssam	if (error != 0)
2470178354Ssam		return error;
2471178354Ssam
2472178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2473178354Ssam		data = htole32(32); /* default value */
2474178354Ssam		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2475178354Ssam		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2476178354Ssam		    sizeof data);
2477178354Ssam		if (error != 0)
2478178354Ssam			return error;
2479178354Ssam	}
2480178354Ssam
2481178354Ssam	return 0;
2482178354Ssam}
2483178354Ssam
2484145247Sdamienstatic void
2485145247Sdamienipw_stop(void *priv)
2486145247Sdamien{
2487145247Sdamien	struct ipw_softc *sc = priv;
2488172567Sthompsa
2489172567Sthompsa	IPW_LOCK(sc);
2490172567Sthompsa	ipw_stop_locked(sc);
2491172567Sthompsa	IPW_UNLOCK(sc);
2492172567Sthompsa}
2493172567Sthompsa
2494172567Sthompsastatic void
2495172567Sthompsaipw_stop_locked(struct ipw_softc *sc)
2496172567Sthompsa{
2497145247Sdamien	int i;
2498145247Sdamien
2499172567Sthompsa	IPW_LOCK_ASSERT(sc);
2500156599Sdamien
2501172567Sthompsa	callout_stop(&sc->sc_wdtimer);
2502145247Sdamien	ipw_stop_master(sc);
2503145247Sdamien
2504145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2505145247Sdamien
2506145247Sdamien	/*
2507145247Sdamien	 * Release tx buffers.
2508145247Sdamien	 */
2509145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
2510145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
2511145247Sdamien
2512145247Sdamien	sc->sc_tx_timer = 0;
2513287197Sglebius	sc->flags &= ~IPW_FLAG_RUNNING;
2514145247Sdamien}
2515145247Sdamien
2516145247Sdamienstatic int
2517145247Sdamienipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2518145247Sdamien{
2519145247Sdamien	struct ipw_softc *sc = arg1;
2520145247Sdamien	uint32_t i, size, buf[256];
2521145247Sdamien
2522174317Sphilip	memset(buf, 0, sizeof buf);
2523174317Sphilip
2524174317Sphilip	if (!(sc->flags & IPW_FLAG_FW_INITED))
2525145247Sdamien		return SYSCTL_OUT(req, buf, sizeof buf);
2526145247Sdamien
2527145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2528145247Sdamien
2529145247Sdamien	size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2530145247Sdamien	for (i = 1; i < size; i++)
2531145247Sdamien		buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2532145247Sdamien
2533174317Sphilip	return SYSCTL_OUT(req, buf, size);
2534145247Sdamien}
2535145247Sdamien
2536145247Sdamienstatic int
2537145247Sdamienipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2538145247Sdamien{
2539145247Sdamien	struct ipw_softc *sc = arg1;
2540145247Sdamien	int val;
2541145247Sdamien
2542145247Sdamien	val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2543145247Sdamien	        (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2544145247Sdamien
2545145247Sdamien	return SYSCTL_OUT(req, &val, sizeof val);
2546145247Sdamien}
2547145247Sdamien
2548145247Sdamienstatic uint32_t
2549145247Sdamienipw_read_table1(struct ipw_softc *sc, uint32_t off)
2550145247Sdamien{
2551145247Sdamien	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2552145247Sdamien}
2553145247Sdamien
2554145247Sdamienstatic void
2555145247Sdamienipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2556145247Sdamien{
2557145247Sdamien	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2558145247Sdamien}
2559145247Sdamien
2560172567Sthompsa#if 0
2561145247Sdamienstatic int
2562145247Sdamienipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2563145247Sdamien{
2564145247Sdamien	uint32_t addr, info;
2565145247Sdamien	uint16_t count, size;
2566145247Sdamien	uint32_t total;
2567145247Sdamien
2568145247Sdamien	/* addr[4] + count[2] + size[2] */
2569145247Sdamien	addr = MEM_READ_4(sc, sc->table2_base + off);
2570145247Sdamien	info = MEM_READ_4(sc, sc->table2_base + off + 4);
2571145247Sdamien
2572145247Sdamien	count = info >> 16;
2573145247Sdamien	size = info & 0xffff;
2574145247Sdamien	total = count * size;
2575145247Sdamien
2576145247Sdamien	if (total > *len) {
2577145247Sdamien		*len = total;
2578145247Sdamien		return EINVAL;
2579145247Sdamien	}
2580145247Sdamien
2581145247Sdamien	*len = total;
2582145247Sdamien	ipw_read_mem_1(sc, addr, buf, total);
2583145247Sdamien
2584145247Sdamien	return 0;
2585145247Sdamien}
2586145247Sdamien
2587145247Sdamienstatic void
2588145247Sdamienipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2589145247Sdamien    bus_size_t count)
2590145247Sdamien{
2591145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2592145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2593145247Sdamien		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2594145247Sdamien	}
2595145247Sdamien}
2596172567Sthompsa#endif
2597145247Sdamien
2598145247Sdamienstatic void
2599156599Sdamienipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2600145247Sdamien    bus_size_t count)
2601145247Sdamien{
2602145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2603145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2604145247Sdamien		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2605145247Sdamien	}
2606145247Sdamien}
2607172567Sthompsa
2608172567Sthompsastatic void
2609172567Sthompsaipw_scan_start(struct ieee80211com *ic)
2610172567Sthompsa{
2611286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2612172567Sthompsa
2613172567Sthompsa	IPW_LOCK(sc);
2614191746Sthompsa	ipw_scan(sc);
2615172567Sthompsa	IPW_UNLOCK(sc);
2616172567Sthompsa}
2617172567Sthompsa
2618172567Sthompsastatic void
2619172567Sthompsaipw_set_channel(struct ieee80211com *ic)
2620172567Sthompsa{
2621286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2622172567Sthompsa
2623172567Sthompsa	IPW_LOCK(sc);
2624172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2625172567Sthompsa		ipw_disable(sc);
2626172567Sthompsa		ipw_setchannel(sc, ic->ic_curchan);
2627172567Sthompsa		ipw_enable(sc);
2628172567Sthompsa	}
2629172567Sthompsa	IPW_UNLOCK(sc);
2630172567Sthompsa}
2631172567Sthompsa
2632172567Sthompsastatic void
2633178354Ssamipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2634172567Sthompsa{
2635172567Sthompsa	/* NB: all channels are scanned at once */
2636172567Sthompsa}
2637172567Sthompsa
2638172567Sthompsastatic void
2639178354Ssamipw_scan_mindwell(struct ieee80211_scan_state *ss)
2640172567Sthompsa{
2641172567Sthompsa	/* NB: don't try to abort scan; wait for firmware to finish */
2642172567Sthompsa}
2643172567Sthompsa
2644172567Sthompsastatic void
2645172567Sthompsaipw_scan_end(struct ieee80211com *ic)
2646172567Sthompsa{
2647286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2648172567Sthompsa
2649172567Sthompsa	IPW_LOCK(sc);
2650172567Sthompsa	sc->flags &= ~IPW_FLAG_SCANNING;
2651172567Sthompsa	IPW_UNLOCK(sc);
2652172567Sthompsa}
2653