if_ipw.c revision 192468
1145247Sdamien/*	$FreeBSD: head/sys/dev/ipw/if_ipw.c 192468 2009-05-20 20:00:40Z sam $	*/
2145247Sdamien
3145247Sdamien/*-
4156599Sdamien * Copyright (c) 2004-2006
5145247Sdamien *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6172567Sthompsa * Copyright (c) 2006 Sam Leffler, Errno Consulting
7172567Sthompsa * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
8145247Sdamien *
9145247Sdamien * Redistribution and use in source and binary forms, with or without
10145247Sdamien * modification, are permitted provided that the following conditions
11145247Sdamien * are met:
12145247Sdamien * 1. Redistributions of source code must retain the above copyright
13145247Sdamien *    notice unmodified, this list of conditions, and the following
14145247Sdamien *    disclaimer.
15145247Sdamien * 2. Redistributions in binary form must reproduce the above copyright
16145247Sdamien *    notice, this list of conditions and the following disclaimer in the
17145247Sdamien *    documentation and/or other materials provided with the distribution.
18145247Sdamien *
19145247Sdamien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20145247Sdamien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21145247Sdamien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22145247Sdamien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23145247Sdamien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24145247Sdamien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25145247Sdamien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26145247Sdamien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27145247Sdamien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28145247Sdamien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29145247Sdamien * SUCH DAMAGE.
30145247Sdamien */
31145247Sdamien
32145247Sdamien#include <sys/cdefs.h>
33145247Sdamien__FBSDID("$FreeBSD: head/sys/dev/ipw/if_ipw.c 192468 2009-05-20 20:00:40Z sam $");
34145247Sdamien
35145247Sdamien/*-
36145247Sdamien * Intel(R) PRO/Wireless 2100 MiniPCI driver
37145247Sdamien * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
38145247Sdamien */
39145247Sdamien
40145247Sdamien#include <sys/param.h>
41145247Sdamien#include <sys/sysctl.h>
42145247Sdamien#include <sys/sockio.h>
43145247Sdamien#include <sys/mbuf.h>
44145247Sdamien#include <sys/kernel.h>
45145247Sdamien#include <sys/socket.h>
46145247Sdamien#include <sys/systm.h>
47145247Sdamien#include <sys/malloc.h>
48156599Sdamien#include <sys/queue.h>
49156599Sdamien#include <sys/taskqueue.h>
50145247Sdamien#include <sys/module.h>
51145247Sdamien#include <sys/bus.h>
52145247Sdamien#include <sys/endian.h>
53156599Sdamien#include <sys/linker.h>
54156599Sdamien#include <sys/firmware.h>
55145247Sdamien
56145247Sdamien#include <machine/bus.h>
57145247Sdamien#include <machine/resource.h>
58145247Sdamien#include <sys/rman.h>
59145247Sdamien
60145247Sdamien#include <dev/pci/pcireg.h>
61145247Sdamien#include <dev/pci/pcivar.h>
62145247Sdamien
63145247Sdamien#include <net/bpf.h>
64145247Sdamien#include <net/if.h>
65145247Sdamien#include <net/if_arp.h>
66145247Sdamien#include <net/ethernet.h>
67145247Sdamien#include <net/if_dl.h>
68145247Sdamien#include <net/if_media.h>
69145247Sdamien#include <net/if_types.h>
70145247Sdamien
71156599Sdamien#include <net80211/ieee80211_var.h>
72156599Sdamien#include <net80211/ieee80211_radiotap.h>
73156599Sdamien
74145247Sdamien#include <netinet/in.h>
75145247Sdamien#include <netinet/in_systm.h>
76145247Sdamien#include <netinet/in_var.h>
77145247Sdamien#include <netinet/ip.h>
78145247Sdamien#include <netinet/if_ether.h>
79145247Sdamien
80145247Sdamien#include <dev/ipw/if_ipwreg.h>
81145247Sdamien#include <dev/ipw/if_ipwvar.h>
82145247Sdamien
83172567Sthompsa#define IPW_DEBUG
84145247Sdamien#ifdef IPW_DEBUG
85145247Sdamien#define DPRINTF(x)	do { if (ipw_debug > 0) printf x; } while (0)
86145247Sdamien#define DPRINTFN(n, x)	do { if (ipw_debug >= (n)) printf x; } while (0)
87145247Sdamienint ipw_debug = 0;
88145247SdamienSYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
89145247Sdamien#else
90145247Sdamien#define DPRINTF(x)
91145247Sdamien#define DPRINTFN(n, x)
92145247Sdamien#endif
93145247Sdamien
94145247SdamienMODULE_DEPEND(ipw, pci,  1, 1, 1);
95145247SdamienMODULE_DEPEND(ipw, wlan, 1, 1, 1);
96156599SdamienMODULE_DEPEND(ipw, firmware, 1, 1, 1);
97145247Sdamien
98145247Sdamienstruct ipw_ident {
99145247Sdamien	uint16_t	vendor;
100145247Sdamien	uint16_t	device;
101145247Sdamien	const char	*name;
102145247Sdamien};
103145247Sdamien
104145247Sdamienstatic const struct ipw_ident ipw_ident_table[] = {
105145247Sdamien	{ 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
106145247Sdamien
107145247Sdamien	{ 0, 0, NULL }
108145247Sdamien};
109145247Sdamien
110178354Ssamstatic struct ieee80211vap *ipw_vap_create(struct ieee80211com *,
111178354Ssam		    const char name[IFNAMSIZ], int unit, int opmode, int flags,
112178354Ssam		    const uint8_t bssid[IEEE80211_ADDR_LEN],
113178354Ssam		    const uint8_t mac[IEEE80211_ADDR_LEN]);
114178354Ssamstatic void	ipw_vap_delete(struct ieee80211vap *);
115145247Sdamienstatic int	ipw_dma_alloc(struct ipw_softc *);
116145247Sdamienstatic void	ipw_release(struct ipw_softc *);
117145247Sdamienstatic void	ipw_media_status(struct ifnet *, struct ifmediareq *);
118178354Ssamstatic int	ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
119145247Sdamienstatic uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
120172567Sthompsastatic void	ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
121172567Sthompsastatic void	ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
122172567Sthompsastatic void	ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
123145247Sdamien		    struct ipw_soft_bd *, struct ipw_soft_buf *);
124145247Sdamienstatic void	ipw_rx_intr(struct ipw_softc *);
125145247Sdamienstatic void	ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
126145247Sdamienstatic void	ipw_tx_intr(struct ipw_softc *);
127145247Sdamienstatic void	ipw_intr(void *);
128145247Sdamienstatic void	ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
129172567Sthompsastatic const char * ipw_cmdname(int);
130145247Sdamienstatic int	ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
131145247Sdamienstatic int	ipw_tx_start(struct ifnet *, struct mbuf *,
132145247Sdamien		    struct ieee80211_node *);
133178354Ssamstatic int	ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
134178354Ssam		    const struct ieee80211_bpf_params *);
135145247Sdamienstatic void	ipw_start(struct ifnet *);
136172567Sthompsastatic void	ipw_start_locked(struct ifnet *);
137172567Sthompsastatic void	ipw_watchdog(void *);
138145247Sdamienstatic int	ipw_ioctl(struct ifnet *, u_long, caddr_t);
139145247Sdamienstatic void	ipw_stop_master(struct ipw_softc *);
140172567Sthompsastatic int	ipw_enable(struct ipw_softc *);
141172567Sthompsastatic int	ipw_disable(struct ipw_softc *);
142145247Sdamienstatic int	ipw_reset(struct ipw_softc *);
143156599Sdamienstatic int	ipw_load_ucode(struct ipw_softc *, const char *, int);
144156599Sdamienstatic int	ipw_load_firmware(struct ipw_softc *, const char *, int);
145145247Sdamienstatic int	ipw_config(struct ipw_softc *);
146191746Sthompsastatic void	ipw_assoc(struct ieee80211com *, struct ieee80211vap *);
147191746Sthompsastatic void	ipw_disassoc(struct ieee80211com *, struct ieee80211vap *);
148156599Sdamienstatic void	ipw_init_task(void *, int);
149145247Sdamienstatic void	ipw_init(void *);
150178354Ssamstatic void	ipw_init_locked(struct ipw_softc *);
151145247Sdamienstatic void	ipw_stop(void *);
152172567Sthompsastatic void	ipw_stop_locked(struct ipw_softc *);
153145247Sdamienstatic int	ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
154145247Sdamienstatic int	ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
155145247Sdamienstatic uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
156145247Sdamienstatic void	ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
157172567Sthompsa#if 0
158145247Sdamienstatic int	ipw_read_table2(struct ipw_softc *, uint32_t, void *,
159145247Sdamien		    uint32_t *);
160145247Sdamienstatic void	ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
161145247Sdamien		    bus_size_t);
162172567Sthompsa#endif
163156599Sdamienstatic void	ipw_write_mem_1(struct ipw_softc *, bus_size_t,
164156599Sdamien		    const uint8_t *, bus_size_t);
165172567Sthompsastatic int	ipw_scan(struct ipw_softc *);
166172567Sthompsastatic void	ipw_scan_start(struct ieee80211com *);
167172567Sthompsastatic void	ipw_scan_end(struct ieee80211com *);
168172567Sthompsastatic void	ipw_set_channel(struct ieee80211com *);
169178354Ssamstatic void	ipw_scan_curchan(struct ieee80211_scan_state *,
170178354Ssam		    unsigned long maxdwell);
171178354Ssamstatic void	ipw_scan_mindwell(struct ieee80211_scan_state *);
172145247Sdamien
173145247Sdamienstatic int ipw_probe(device_t);
174145247Sdamienstatic int ipw_attach(device_t);
175145247Sdamienstatic int ipw_detach(device_t);
176145247Sdamienstatic int ipw_shutdown(device_t);
177145247Sdamienstatic int ipw_suspend(device_t);
178145247Sdamienstatic int ipw_resume(device_t);
179145247Sdamien
180145247Sdamienstatic device_method_t ipw_methods[] = {
181145247Sdamien	/* Device interface */
182145247Sdamien	DEVMETHOD(device_probe,		ipw_probe),
183145247Sdamien	DEVMETHOD(device_attach,	ipw_attach),
184145247Sdamien	DEVMETHOD(device_detach,	ipw_detach),
185145247Sdamien	DEVMETHOD(device_shutdown,	ipw_shutdown),
186145247Sdamien	DEVMETHOD(device_suspend,	ipw_suspend),
187145247Sdamien	DEVMETHOD(device_resume,	ipw_resume),
188145247Sdamien
189145247Sdamien	{ 0, 0 }
190145247Sdamien};
191145247Sdamien
192145247Sdamienstatic driver_t ipw_driver = {
193145247Sdamien	"ipw",
194145247Sdamien	ipw_methods,
195145247Sdamien	sizeof (struct ipw_softc)
196145247Sdamien};
197145247Sdamien
198145247Sdamienstatic devclass_t ipw_devclass;
199145247Sdamien
200145247SdamienDRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0);
201145247Sdamien
202145247Sdamienstatic int
203145247Sdamienipw_probe(device_t dev)
204145247Sdamien{
205145247Sdamien	const struct ipw_ident *ident;
206145247Sdamien
207145247Sdamien	for (ident = ipw_ident_table; ident->name != NULL; ident++) {
208145247Sdamien		if (pci_get_vendor(dev) == ident->vendor &&
209145247Sdamien		    pci_get_device(dev) == ident->device) {
210145247Sdamien			device_set_desc(dev, ident->name);
211145247Sdamien			return 0;
212145247Sdamien		}
213145247Sdamien	}
214145247Sdamien	return ENXIO;
215145247Sdamien}
216145247Sdamien
217145247Sdamien/* Base Address Register */
218145247Sdamien#define IPW_PCI_BAR0	0x10
219145247Sdamien
220145247Sdamienstatic int
221145247Sdamienipw_attach(device_t dev)
222145247Sdamien{
223145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
224147256Sbrooks	struct ifnet *ifp;
225178354Ssam	struct ieee80211com *ic;
226170530Ssam	struct ieee80211_channel *c;
227145247Sdamien	uint16_t val;
228145247Sdamien	int error, i;
229190526Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
230145247Sdamien
231145247Sdamien	sc->sc_dev = dev;
232145247Sdamien
233145247Sdamien	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
234145247Sdamien	    MTX_DEF | MTX_RECURSE);
235145247Sdamien
236156599Sdamien	TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
237172567Sthompsa	callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
238156599Sdamien
239145247Sdamien	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
240145247Sdamien		device_printf(dev, "chip is in D%d power mode "
241145247Sdamien		    "-- setting to D0\n", pci_get_powerstate(dev));
242145247Sdamien		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
243145247Sdamien	}
244145247Sdamien
245146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
246146498Sdamien
247145247Sdamien	/* enable bus-mastering */
248145247Sdamien	pci_enable_busmaster(dev);
249145247Sdamien
250145247Sdamien	sc->mem_rid = IPW_PCI_BAR0;
251145247Sdamien	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
252145247Sdamien	    RF_ACTIVE);
253145247Sdamien	if (sc->mem == NULL) {
254145247Sdamien		device_printf(dev, "could not allocate memory resource\n");
255145247Sdamien		goto fail;
256145247Sdamien	}
257145247Sdamien
258145247Sdamien	sc->sc_st = rman_get_bustag(sc->mem);
259145247Sdamien	sc->sc_sh = rman_get_bushandle(sc->mem);
260145247Sdamien
261145247Sdamien	sc->irq_rid = 0;
262145247Sdamien	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
263145247Sdamien	    RF_ACTIVE | RF_SHAREABLE);
264145247Sdamien	if (sc->irq == NULL) {
265145247Sdamien		device_printf(dev, "could not allocate interrupt resource\n");
266178354Ssam		goto fail1;
267145247Sdamien	}
268145247Sdamien
269145247Sdamien	if (ipw_reset(sc) != 0) {
270145247Sdamien		device_printf(dev, "could not reset adapter\n");
271178354Ssam		goto fail2;
272145247Sdamien	}
273145247Sdamien
274145247Sdamien	if (ipw_dma_alloc(sc) != 0) {
275145247Sdamien		device_printf(dev, "could not allocate DMA resources\n");
276178354Ssam		goto fail2;
277145247Sdamien	}
278147757Sdamien
279178354Ssam	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
280147256Sbrooks	if (ifp == NULL) {
281147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
282178354Ssam		goto fail3;
283147256Sbrooks	}
284178354Ssam	ic = ifp->if_l2com;
285147757Sdamien
286145247Sdamien	ifp->if_softc = sc;
287145247Sdamien	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
288145247Sdamien	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
289145247Sdamien	ifp->if_init = ipw_init;
290145247Sdamien	ifp->if_ioctl = ipw_ioctl;
291145247Sdamien	ifp->if_start = ipw_start;
292145247Sdamien	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
293145247Sdamien	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
294145247Sdamien	IFQ_SET_READY(&ifp->if_snd);
295145247Sdamien
296145247Sdamien	ic->ic_ifp = ifp;
297178354Ssam	ic->ic_opmode = IEEE80211_M_STA;
298145247Sdamien	ic->ic_phytype = IEEE80211_T_DS;
299145247Sdamien
300145247Sdamien	/* set device capabilities */
301178957Ssam	ic->ic_caps =
302178957Ssam		  IEEE80211_C_STA		/* station mode supported */
303178957Ssam		| IEEE80211_C_IBSS		/* IBSS mode supported */
304172567Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode supported */
305172567Sthompsa		| IEEE80211_C_PMGT		/* power save supported */
306172567Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
307172567Sthompsa		| IEEE80211_C_WPA		/* 802.11i supported */
308172567Sthompsa		;
309145247Sdamien
310145247Sdamien	/* read MAC address from EEPROM */
311145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
312190526Ssam	macaddr[0] = val >> 8;
313190526Ssam	macaddr[1] = val & 0xff;
314145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
315190526Ssam	macaddr[2] = val >> 8;
316190526Ssam	macaddr[3] = val & 0xff;
317145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
318190526Ssam	macaddr[4] = val >> 8;
319190526Ssam	macaddr[5] = val & 0xff;
320145247Sdamien
321170530Ssam	/* set supported .11b channels (read from EEPROM) */
322170530Ssam	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
323170530Ssam		val = 0x7ff; /* default to channels 1-11 */
324170530Ssam	val <<= 1;
325170530Ssam	for (i = 1; i < 16; i++) {
326170530Ssam		if (val & (1 << i)) {
327170530Ssam			c = &ic->ic_channels[ic->ic_nchans++];
328170530Ssam			c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
329170530Ssam			c->ic_flags = IEEE80211_CHAN_B;
330170530Ssam			c->ic_ieee = i;
331170530Ssam		}
332145247Sdamien	}
333145247Sdamien
334145247Sdamien	/* check support for radio transmitter switch in EEPROM */
335145247Sdamien	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
336145247Sdamien		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
337145247Sdamien
338190526Ssam	ieee80211_ifattach(ic, macaddr);
339172567Sthompsa	ic->ic_scan_start = ipw_scan_start;
340172567Sthompsa	ic->ic_scan_end = ipw_scan_end;
341172567Sthompsa	ic->ic_set_channel = ipw_set_channel;
342172567Sthompsa	ic->ic_scan_curchan = ipw_scan_curchan;
343172567Sthompsa	ic->ic_scan_mindwell = ipw_scan_mindwell;
344178354Ssam	ic->ic_raw_xmit = ipw_raw_xmit;
345172567Sthompsa
346178354Ssam	ic->ic_vap_create = ipw_vap_create;
347178354Ssam	ic->ic_vap_delete = ipw_vap_delete;
348145247Sdamien
349192468Ssam	ieee80211_radiotap_attach(ic,
350192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
351192468Ssam		IPW_TX_RADIOTAP_PRESENT,
352192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
353192468Ssam		IPW_RX_RADIOTAP_PRESENT);
354178354Ssam
355145247Sdamien	/*
356145247Sdamien	 * Add a few sysctl knobs.
357145247Sdamien	 */
358145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
359145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
360145247Sdamien	    CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
361145247Sdamien	    "radio transmitter switch state (0=off, 1=on)");
362145247Sdamien
363145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
364145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
365145247Sdamien	    CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
366145247Sdamien	    "statistics");
367145247Sdamien
368145247Sdamien	/*
369145247Sdamien	 * Hook our interrupt after all initialization is complete.
370145247Sdamien	 */
371145247Sdamien	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
372166901Spiso	    NULL, ipw_intr, sc, &sc->sc_ih);
373145247Sdamien	if (error != 0) {
374145247Sdamien		device_printf(dev, "could not set up interrupt\n");
375178354Ssam		goto fail4;
376145247Sdamien	}
377145247Sdamien
378145247Sdamien	if (bootverbose)
379145247Sdamien		ieee80211_announce(ic);
380145247Sdamien
381145247Sdamien	return 0;
382178354Ssamfail4:
383178354Ssam	if_free(ifp);
384178354Ssamfail3:
385178354Ssam	ipw_release(sc);
386178354Ssamfail2:
387178354Ssam	bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
388178354Ssamfail1:
389178354Ssam	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
390178354Ssamfail:
391178354Ssam	mtx_destroy(&sc->sc_mtx);
392145247Sdamien	return ENXIO;
393145247Sdamien}
394145247Sdamien
395145247Sdamienstatic int
396145247Sdamienipw_detach(device_t dev)
397145247Sdamien{
398145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
399178354Ssam	struct ifnet *ifp = sc->sc_ifp;
400178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
401145247Sdamien
402191912Sthompsa	ieee80211_draintask(ic, &sc->sc_init_task);
403145247Sdamien	ipw_stop(sc);
404178354Ssam
405178354Ssam	ieee80211_ifdetach(ic);
406178354Ssam
407172567Sthompsa	callout_drain(&sc->sc_wdtimer);
408145247Sdamien
409145247Sdamien	ipw_release(sc);
410145247Sdamien
411178354Ssam	bus_teardown_intr(dev, sc->irq, sc->sc_ih);
412178354Ssam	bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
413145247Sdamien
414178354Ssam	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
415156599Sdamien
416178354Ssam	if_free(ifp);
417145247Sdamien
418159487Siedowse	if (sc->sc_firmware != NULL) {
419159487Siedowse		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
420159487Siedowse		sc->sc_firmware = NULL;
421159487Siedowse	}
422159487Siedowse
423145247Sdamien	mtx_destroy(&sc->sc_mtx);
424145247Sdamien
425145247Sdamien	return 0;
426145247Sdamien}
427145247Sdamien
428178354Ssamstatic struct ieee80211vap *
429178354Ssamipw_vap_create(struct ieee80211com *ic,
430178354Ssam	const char name[IFNAMSIZ], int unit, int opmode, int flags,
431178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN],
432178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN])
433178354Ssam{
434178354Ssam	struct ifnet *ifp = ic->ic_ifp;
435178354Ssam	struct ipw_softc *sc = ifp->if_softc;
436178354Ssam	struct ipw_vap *ivp;
437178354Ssam	struct ieee80211vap *vap;
438178354Ssam	const struct firmware *fp;
439178354Ssam	const struct ipw_firmware_hdr *hdr;
440178354Ssam	const char *imagename;
441178354Ssam
442178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
443178354Ssam		return NULL;
444178354Ssam
445178354Ssam	switch (opmode) {
446178354Ssam	case IEEE80211_M_STA:
447178354Ssam		imagename = "ipw_bss";
448178354Ssam		break;
449178354Ssam	case IEEE80211_M_IBSS:
450178354Ssam		imagename = "ipw_ibss";
451178354Ssam		break;
452178354Ssam	case IEEE80211_M_MONITOR:
453178354Ssam		imagename = "ipw_monitor";
454178354Ssam		break;
455178354Ssam	default:
456178354Ssam		return NULL;
457178354Ssam	}
458178354Ssam
459178354Ssam	/*
460178354Ssam	 * Load firmware image using the firmware(9) subsystem.  Doing
461178354Ssam	 * this unlocked is ok since we're single-threaded by the
462178354Ssam	 * 802.11 layer.
463178354Ssam	 */
464178354Ssam	if (sc->sc_firmware == NULL ||
465178354Ssam	    strcmp(sc->sc_firmware->name, imagename) != 0) {
466178354Ssam		if (sc->sc_firmware != NULL)
467178354Ssam			firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
468178354Ssam		sc->sc_firmware = firmware_get(imagename);
469178354Ssam	}
470178354Ssam	if (sc->sc_firmware == NULL) {
471178354Ssam		device_printf(sc->sc_dev,
472178354Ssam		    "could not load firmware image '%s'\n", imagename);
473178354Ssam		return NULL;
474178354Ssam	}
475178354Ssam	fp = sc->sc_firmware;
476178354Ssam	if (fp->datasize < sizeof *hdr) {
477178354Ssam		device_printf(sc->sc_dev,
478178354Ssam		    "firmware image too short %zu\n", fp->datasize);
479178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
480178354Ssam		sc->sc_firmware = NULL;
481178354Ssam		return NULL;
482178354Ssam	}
483178354Ssam	hdr = (const struct ipw_firmware_hdr *)fp->data;
484178354Ssam	if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
485178354Ssam	    le32toh(hdr->ucodesz)) {
486178354Ssam		device_printf(sc->sc_dev,
487178354Ssam		    "firmware image too short %zu\n", fp->datasize);
488178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
489178354Ssam		sc->sc_firmware = NULL;
490178354Ssam		return NULL;
491178354Ssam	}
492178354Ssam
493178354Ssam	ivp = (struct ipw_vap *) malloc(sizeof(struct ipw_vap),
494178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
495178354Ssam	if (ivp == NULL)
496178354Ssam		return NULL;
497178354Ssam	vap = &ivp->vap;
498178354Ssam
499178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
500178354Ssam	/* override with driver methods */
501178354Ssam	ivp->newstate = vap->iv_newstate;
502178354Ssam	vap->iv_newstate = ipw_newstate;
503178354Ssam
504178354Ssam	/* complete setup */
505178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status);
506178354Ssam	ic->ic_opmode = opmode;
507178354Ssam	return vap;
508178354Ssam}
509178354Ssam
510178354Ssamstatic void
511178354Ssamipw_vap_delete(struct ieee80211vap *vap)
512178354Ssam{
513178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
514178354Ssam
515178354Ssam	ieee80211_vap_detach(vap);
516178354Ssam	free(ivp, M_80211_VAP);
517178354Ssam}
518178354Ssam
519145247Sdamienstatic int
520145247Sdamienipw_dma_alloc(struct ipw_softc *sc)
521145247Sdamien{
522145247Sdamien	struct ipw_soft_bd *sbd;
523145247Sdamien	struct ipw_soft_hdr *shdr;
524145247Sdamien	struct ipw_soft_buf *sbuf;
525145247Sdamien	bus_addr_t physaddr;
526145247Sdamien	int error, i;
527145247Sdamien
528145247Sdamien	/*
529145247Sdamien	 * Allocate and map tx ring.
530145247Sdamien	 */
531145247Sdamien	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
532145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
533145247Sdamien	    NULL, &sc->tbd_dmat);
534145247Sdamien	if (error != 0) {
535145247Sdamien		device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
536145247Sdamien		goto fail;
537145247Sdamien	}
538145247Sdamien
539145247Sdamien	error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
540145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
541145247Sdamien	if (error != 0) {
542145247Sdamien		device_printf(sc->sc_dev,
543145247Sdamien		    "could not allocate tx ring DMA memory\n");
544145247Sdamien		goto fail;
545145247Sdamien	}
546145247Sdamien
547145247Sdamien	error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
548145247Sdamien	    IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
549145247Sdamien	if (error != 0) {
550145247Sdamien		device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
551145247Sdamien		goto fail;
552145247Sdamien	}
553145247Sdamien
554145247Sdamien	/*
555145247Sdamien	 * Allocate and map rx ring.
556145247Sdamien	 */
557145247Sdamien	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
558145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
559145247Sdamien	    NULL, &sc->rbd_dmat);
560145247Sdamien	if (error != 0) {
561145247Sdamien		device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
562145247Sdamien		goto fail;
563145247Sdamien	}
564145247Sdamien
565145247Sdamien	error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
566145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
567145247Sdamien	if (error != 0) {
568145247Sdamien		device_printf(sc->sc_dev,
569145247Sdamien		    "could not allocate rx ring DMA memory\n");
570145247Sdamien		goto fail;
571145247Sdamien	}
572145247Sdamien
573145247Sdamien	error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
574145247Sdamien	    IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
575145247Sdamien	if (error != 0) {
576145247Sdamien		device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
577145247Sdamien		goto fail;
578145247Sdamien	}
579145247Sdamien
580145247Sdamien	/*
581145247Sdamien	 * Allocate and map status ring.
582145247Sdamien	 */
583145247Sdamien	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
584145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
585145247Sdamien	    NULL, NULL, &sc->status_dmat);
586145247Sdamien	if (error != 0) {
587145247Sdamien		device_printf(sc->sc_dev,
588145247Sdamien		    "could not create status ring DMA tag\n");
589145247Sdamien		goto fail;
590145247Sdamien	}
591145247Sdamien
592145247Sdamien	error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
593145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
594145247Sdamien	if (error != 0) {
595145247Sdamien		device_printf(sc->sc_dev,
596145247Sdamien		    "could not allocate status ring DMA memory\n");
597145247Sdamien		goto fail;
598145247Sdamien	}
599145247Sdamien
600145247Sdamien	error = bus_dmamap_load(sc->status_dmat, sc->status_map,
601145247Sdamien	    sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
602145247Sdamien	    0);
603145247Sdamien	if (error != 0) {
604145247Sdamien		device_printf(sc->sc_dev,
605145247Sdamien		    "could not map status ring DMA memory\n");
606145247Sdamien		goto fail;
607145247Sdamien	}
608145247Sdamien
609145247Sdamien	/*
610145247Sdamien	 * Allocate command DMA map.
611145247Sdamien	 */
612145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
613145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
614145247Sdamien	    sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
615145247Sdamien	if (error != 0) {
616145247Sdamien		device_printf(sc->sc_dev, "could not create command DMA tag\n");
617145247Sdamien		goto fail;
618145247Sdamien	}
619145247Sdamien
620145247Sdamien	error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
621145247Sdamien	if (error != 0) {
622145247Sdamien		device_printf(sc->sc_dev,
623145247Sdamien		    "could not create command DMA map\n");
624145247Sdamien		goto fail;
625145247Sdamien	}
626145247Sdamien
627145247Sdamien	/*
628145247Sdamien	 * Allocate headers DMA maps.
629145247Sdamien	 */
630145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
631145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
632145247Sdamien	    sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
633145247Sdamien	if (error != 0) {
634145247Sdamien		device_printf(sc->sc_dev, "could not create header DMA tag\n");
635145247Sdamien		goto fail;
636145247Sdamien	}
637145247Sdamien
638145247Sdamien	SLIST_INIT(&sc->free_shdr);
639145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
640145247Sdamien		shdr = &sc->shdr_list[i];
641145247Sdamien		error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
642145247Sdamien		if (error != 0) {
643145247Sdamien			device_printf(sc->sc_dev,
644145247Sdamien			    "could not create header DMA map\n");
645145247Sdamien			goto fail;
646145247Sdamien		}
647145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
648145247Sdamien	}
649145247Sdamien
650145247Sdamien	/*
651145247Sdamien	 * Allocate tx buffers DMA maps.
652145247Sdamien	 */
653145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
654145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
655145247Sdamien	    NULL, NULL, &sc->txbuf_dmat);
656145247Sdamien	if (error != 0) {
657145247Sdamien		device_printf(sc->sc_dev, "could not create tx DMA tag\n");
658145247Sdamien		goto fail;
659145247Sdamien	}
660145247Sdamien
661145247Sdamien	SLIST_INIT(&sc->free_sbuf);
662145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
663145247Sdamien		sbuf = &sc->tx_sbuf_list[i];
664145247Sdamien		error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
665145247Sdamien		if (error != 0) {
666145247Sdamien			device_printf(sc->sc_dev,
667145247Sdamien			    "could not create tx DMA map\n");
668145247Sdamien			goto fail;
669145247Sdamien		}
670145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
671145247Sdamien	}
672145247Sdamien
673145247Sdamien	/*
674145247Sdamien	 * Initialize tx ring.
675145247Sdamien	 */
676145247Sdamien	for (i = 0; i < IPW_NTBD; i++) {
677145247Sdamien		sbd = &sc->stbd_list[i];
678145247Sdamien		sbd->bd = &sc->tbd_list[i];
679145247Sdamien		sbd->type = IPW_SBD_TYPE_NOASSOC;
680145247Sdamien	}
681145247Sdamien
682145247Sdamien	/*
683145247Sdamien	 * Pre-allocate rx buffers and DMA maps.
684145247Sdamien	 */
685145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
686147841Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
687147841Sdamien	    NULL, &sc->rxbuf_dmat);
688145247Sdamien	if (error != 0) {
689145247Sdamien		device_printf(sc->sc_dev, "could not create rx DMA tag\n");
690145247Sdamien		goto fail;
691145247Sdamien	}
692145247Sdamien
693145247Sdamien	for (i = 0; i < IPW_NRBD; i++) {
694145247Sdamien		sbd = &sc->srbd_list[i];
695145247Sdamien		sbuf = &sc->rx_sbuf_list[i];
696145247Sdamien		sbd->bd = &sc->rbd_list[i];
697145247Sdamien
698145247Sdamien		sbuf->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
699145247Sdamien		if (sbuf->m == NULL) {
700145247Sdamien			device_printf(sc->sc_dev,
701145247Sdamien			    "could not allocate rx mbuf\n");
702145247Sdamien			error = ENOMEM;
703145247Sdamien			goto fail;
704145247Sdamien		}
705145247Sdamien
706145247Sdamien		error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
707145247Sdamien		if (error != 0) {
708145247Sdamien			device_printf(sc->sc_dev,
709145247Sdamien			    "could not create rx DMA map\n");
710145247Sdamien			goto fail;
711145247Sdamien		}
712145247Sdamien
713145247Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
714145247Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
715145247Sdamien		    &physaddr, 0);
716145247Sdamien		if (error != 0) {
717145247Sdamien			device_printf(sc->sc_dev,
718145247Sdamien			    "could not map rx DMA memory\n");
719145247Sdamien			goto fail;
720145247Sdamien		}
721145247Sdamien
722145247Sdamien		sbd->type = IPW_SBD_TYPE_DATA;
723145247Sdamien		sbd->priv = sbuf;
724145247Sdamien		sbd->bd->physaddr = htole32(physaddr);
725145247Sdamien		sbd->bd->len = htole32(MCLBYTES);
726145247Sdamien	}
727145247Sdamien
728145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
729145247Sdamien
730145247Sdamien	return 0;
731145247Sdamien
732145247Sdamienfail:	ipw_release(sc);
733145247Sdamien	return error;
734145247Sdamien}
735145247Sdamien
736145247Sdamienstatic void
737145247Sdamienipw_release(struct ipw_softc *sc)
738145247Sdamien{
739145247Sdamien	struct ipw_soft_buf *sbuf;
740145247Sdamien	int i;
741145247Sdamien
742145247Sdamien	if (sc->tbd_dmat != NULL) {
743145247Sdamien		if (sc->stbd_list != NULL) {
744145247Sdamien			bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
745145247Sdamien			bus_dmamem_free(sc->tbd_dmat, sc->tbd_list,
746145247Sdamien			    sc->tbd_map);
747145247Sdamien		}
748145247Sdamien		bus_dma_tag_destroy(sc->tbd_dmat);
749145247Sdamien	}
750145247Sdamien
751145247Sdamien	if (sc->rbd_dmat != NULL) {
752145247Sdamien		if (sc->rbd_list != NULL) {
753145247Sdamien			bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
754145247Sdamien			bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
755145247Sdamien			    sc->rbd_map);
756145247Sdamien		}
757145247Sdamien		bus_dma_tag_destroy(sc->rbd_dmat);
758145247Sdamien	}
759145247Sdamien
760145247Sdamien	if (sc->status_dmat != NULL) {
761145247Sdamien		if (sc->status_list != NULL) {
762145247Sdamien			bus_dmamap_unload(sc->status_dmat, sc->status_map);
763145247Sdamien			bus_dmamem_free(sc->status_dmat, sc->status_list,
764145247Sdamien			    sc->status_map);
765145247Sdamien		}
766145247Sdamien		bus_dma_tag_destroy(sc->status_dmat);
767145247Sdamien	}
768145247Sdamien
769145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
770145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
771145247Sdamien
772145247Sdamien	if (sc->cmd_dmat != NULL) {
773145247Sdamien		bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
774145247Sdamien		bus_dma_tag_destroy(sc->cmd_dmat);
775145247Sdamien	}
776145247Sdamien
777145247Sdamien	if (sc->hdr_dmat != NULL) {
778145247Sdamien		for (i = 0; i < IPW_NDATA; i++)
779145247Sdamien			bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
780145247Sdamien		bus_dma_tag_destroy(sc->hdr_dmat);
781145247Sdamien	}
782145247Sdamien
783145247Sdamien	if (sc->txbuf_dmat != NULL) {
784145247Sdamien		for (i = 0; i < IPW_NDATA; i++) {
785145247Sdamien			bus_dmamap_destroy(sc->txbuf_dmat,
786145247Sdamien			    sc->tx_sbuf_list[i].map);
787145247Sdamien		}
788145247Sdamien		bus_dma_tag_destroy(sc->txbuf_dmat);
789145247Sdamien	}
790145247Sdamien
791145247Sdamien	if (sc->rxbuf_dmat != NULL) {
792145247Sdamien		for (i = 0; i < IPW_NRBD; i++) {
793145247Sdamien			sbuf = &sc->rx_sbuf_list[i];
794145247Sdamien			if (sbuf->m != NULL) {
795145247Sdamien				bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
796145247Sdamien				    BUS_DMASYNC_POSTREAD);
797145247Sdamien				bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
798145247Sdamien				m_freem(sbuf->m);
799145247Sdamien			}
800145247Sdamien			bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
801145247Sdamien		}
802145247Sdamien		bus_dma_tag_destroy(sc->rxbuf_dmat);
803145247Sdamien	}
804145247Sdamien}
805145247Sdamien
806145247Sdamienstatic int
807145247Sdamienipw_shutdown(device_t dev)
808145247Sdamien{
809145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
810145247Sdamien
811145247Sdamien	ipw_stop(sc);
812145247Sdamien
813145247Sdamien	return 0;
814145247Sdamien}
815145247Sdamien
816145247Sdamienstatic int
817145247Sdamienipw_suspend(device_t dev)
818145247Sdamien{
819145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
820145247Sdamien
821145247Sdamien	ipw_stop(sc);
822145247Sdamien
823145247Sdamien	return 0;
824145247Sdamien}
825145247Sdamien
826145247Sdamienstatic int
827145247Sdamienipw_resume(device_t dev)
828145247Sdamien{
829145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
830178354Ssam	struct ifnet *ifp = sc->sc_ifp;
831145247Sdamien
832146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
833146498Sdamien
834178354Ssam	if (ifp->if_flags & IFF_UP)
835178354Ssam		ipw_init(sc);
836145247Sdamien
837145247Sdamien	return 0;
838145247Sdamien}
839145247Sdamien
840145247Sdamienstatic int
841172567Sthompsaipw_cvtrate(int ipwrate)
842172567Sthompsa{
843172567Sthompsa	switch (ipwrate) {
844172567Sthompsa	case IPW_RATE_DS1:	return 2;
845172567Sthompsa	case IPW_RATE_DS2:	return 4;
846172567Sthompsa	case IPW_RATE_DS5:	return 11;
847172567Sthompsa	case IPW_RATE_DS11:	return 22;
848172567Sthompsa	}
849145247Sdamien	return 0;
850145247Sdamien}
851145247Sdamien
852145247Sdamien/*
853156599Sdamien * The firmware automatically adapts the transmit speed. We report its current
854156599Sdamien * value here.
855145247Sdamien */
856145247Sdamienstatic void
857145247Sdamienipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
858145247Sdamien{
859178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
860178354Ssam	struct ieee80211com *ic = vap->iv_ic;
861178354Ssam	struct ipw_softc *sc = ic->ic_ifp->if_softc;
862145247Sdamien
863145247Sdamien	/* read current transmission rate from adapter */
864178354Ssam	vap->iv_bss->ni_txrate = ipw_cvtrate(
865178354Ssam	    ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
866178354Ssam	ieee80211_media_status(ifp, imr);
867145247Sdamien}
868145247Sdamien
869145247Sdamienstatic int
870178354Ssamipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
871145247Sdamien{
872178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
873178354Ssam	struct ieee80211com *ic = vap->iv_ic;
874145247Sdamien	struct ifnet *ifp = ic->ic_ifp;
875145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
876191746Sthompsa	enum ieee80211_state ostate;
877145247Sdamien
878172567Sthompsa	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
879178354Ssam		ieee80211_state_name[vap->iv_state],
880172567Sthompsa		ieee80211_state_name[nstate], sc->flags));
881172567Sthompsa
882191746Sthompsa	ostate = vap->iv_state;
883191746Sthompsa	IEEE80211_UNLOCK(ic);
884191746Sthompsa
885145247Sdamien	switch (nstate) {
886145247Sdamien	case IEEE80211_S_RUN:
887172567Sthompsa		if (ic->ic_opmode == IEEE80211_M_IBSS) {
888172567Sthompsa			/*
889172567Sthompsa			 * XXX when joining an ibss network we are called
890172567Sthompsa			 * with a SCAN -> RUN transition on scan complete.
891172567Sthompsa			 * Use that to call ipw_auth_and_assoc.  On completing
892172567Sthompsa			 * the join we are then called again with an
893172567Sthompsa			 * AUTH -> RUN transition and we want to do nothing.
894172567Sthompsa			 * This is all totally bogus and needs to be redone.
895172567Sthompsa			 */
896191746Sthompsa			if (ostate == IEEE80211_S_SCAN)
897191746Sthompsa				ipw_assoc(ic, vap);
898172567Sthompsa		}
899172567Sthompsa		break;
900145247Sdamien
901172567Sthompsa	case IEEE80211_S_INIT:
902172567Sthompsa		if (sc->flags & IPW_FLAG_ASSOCIATED)
903191746Sthompsa			ipw_disassoc(ic, vap);
904172567Sthompsa		break;
905145247Sdamien
906172567Sthompsa	case IEEE80211_S_AUTH:
907191746Sthompsa		ipw_assoc(ic, vap);
908191746Sthompsa		break;
909145247Sdamien
910172567Sthompsa	case IEEE80211_S_ASSOC:
911172567Sthompsa		/*
912172567Sthompsa		 * If we are not transitioning from AUTH the resend the
913172567Sthompsa		 * association request.
914172567Sthompsa		 */
915191746Sthompsa		if (ostate != IEEE80211_S_AUTH)
916191746Sthompsa			ipw_assoc(ic, vap);
917145247Sdamien		break;
918145247Sdamien
919172058Ssam	default:
920145247Sdamien		break;
921145247Sdamien	}
922191746Sthompsa	IEEE80211_LOCK(ic);
923178354Ssam	return ivp->newstate(vap, nstate, arg);
924145247Sdamien}
925145247Sdamien
926145247Sdamien/*
927145247Sdamien * Read 16 bits at address 'addr' from the serial EEPROM.
928145247Sdamien */
929145247Sdamienstatic uint16_t
930145247Sdamienipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
931145247Sdamien{
932145247Sdamien	uint32_t tmp;
933145247Sdamien	uint16_t val;
934145247Sdamien	int n;
935145247Sdamien
936145247Sdamien	/* clock C once before the first command */
937145247Sdamien	IPW_EEPROM_CTL(sc, 0);
938145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
939145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
940145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
941145247Sdamien
942145247Sdamien	/* write start bit (1) */
943145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
944145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
945145247Sdamien
946145247Sdamien	/* write READ opcode (10) */
947145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
948145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
949145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
950145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
951145247Sdamien
952145247Sdamien	/* write address A7-A0 */
953145247Sdamien	for (n = 7; n >= 0; n--) {
954145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
955145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
956145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
957145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
958145247Sdamien	}
959145247Sdamien
960145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
961145247Sdamien
962145247Sdamien	/* read data Q15-Q0 */
963145247Sdamien	val = 0;
964145247Sdamien	for (n = 15; n >= 0; n--) {
965145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
966145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
967145247Sdamien		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
968145247Sdamien		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
969145247Sdamien	}
970145247Sdamien
971145247Sdamien	IPW_EEPROM_CTL(sc, 0);
972145247Sdamien
973145247Sdamien	/* clear Chip Select and clock C */
974145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
975145247Sdamien	IPW_EEPROM_CTL(sc, 0);
976145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
977145247Sdamien
978145247Sdamien	return le16toh(val);
979145247Sdamien}
980145247Sdamien
981145247Sdamienstatic void
982172567Sthompsaipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
983145247Sdamien{
984145247Sdamien	struct ipw_cmd *cmd;
985145247Sdamien
986145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
987145247Sdamien
988145247Sdamien	cmd = mtod(sbuf->m, struct ipw_cmd *);
989145247Sdamien
990172567Sthompsa	DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
991172567Sthompsa	    ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
992145247Sdamien	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
993145247Sdamien	    le32toh(cmd->status)));
994145247Sdamien
995172567Sthompsa	sc->flags &= ~IPW_FLAG_BUSY;
996145247Sdamien	wakeup(sc);
997145247Sdamien}
998145247Sdamien
999145247Sdamienstatic void
1000172567Sthompsaipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1001145247Sdamien{
1002178354Ssam#define	IEEESTATE(vap)	ieee80211_state_name[vap->iv_state]
1003178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1004178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1005178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1006145247Sdamien	uint32_t state;
1007145247Sdamien
1008145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1009145247Sdamien
1010145247Sdamien	state = le32toh(*mtod(sbuf->m, uint32_t *));
1011145247Sdamien
1012145247Sdamien	switch (state) {
1013145247Sdamien	case IPW_STATE_ASSOCIATED:
1014172567Sthompsa		DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1015178354Ssam			IEEESTATE(vap), sc->flags));
1016178354Ssam		/* XXX suppress state change in case the fw auto-associates */
1017178354Ssam		if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1018178354Ssam			DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1019178354Ssam				IEEESTATE(vap), sc->flags));
1020178354Ssam			break;
1021178354Ssam		}
1022178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATING;
1023172567Sthompsa		sc->flags |= IPW_FLAG_ASSOCIATED;
1024191746Sthompsa		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
1025145247Sdamien		break;
1026145247Sdamien
1027145247Sdamien	case IPW_STATE_SCANNING:
1028172567Sthompsa		DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1029178354Ssam			IEEESTATE(vap), sc->flags));
1030172567Sthompsa		/*
1031172567Sthompsa		 * NB: Check driver state for association on assoc
1032172567Sthompsa		 * loss as the firmware will immediately start to
1033172567Sthompsa		 * scan and we would treat it as a beacon miss if
1034172567Sthompsa		 * we checked the 802.11 layer state.
1035172567Sthompsa		 */
1036178354Ssam		if (sc->flags & IPW_FLAG_ASSOCIATED) {
1037178354Ssam			/* XXX probably need to issue disassoc to fw */
1038191746Sthompsa			ieee80211_beacon_miss(ic);
1039178354Ssam		}
1040145247Sdamien		break;
1041145247Sdamien
1042145247Sdamien	case IPW_STATE_SCAN_COMPLETE:
1043172567Sthompsa		/*
1044172567Sthompsa		 * XXX For some reason scan requests generate scan
1045172567Sthompsa		 * started + scan done events before any traffic is
1046172567Sthompsa		 * received (e.g. probe response frames).  We work
1047172567Sthompsa		 * around this by marking the HACK flag and skipping
1048172567Sthompsa		 * the first scan complete event.
1049172567Sthompsa		*/
1050178354Ssam		DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1051178354Ssam			    IEEESTATE(vap), sc->flags));
1052172567Sthompsa		if (sc->flags & IPW_FLAG_HACK) {
1053172567Sthompsa			sc->flags &= ~IPW_FLAG_HACK;
1054172567Sthompsa			break;
1055172567Sthompsa		}
1056172567Sthompsa		if (sc->flags & IPW_FLAG_SCANNING) {
1057191746Sthompsa			ieee80211_scan_done(vap);
1058172567Sthompsa			sc->flags &= ~IPW_FLAG_SCANNING;
1059172567Sthompsa			sc->sc_scan_timer = 0;
1060172567Sthompsa		}
1061145247Sdamien		break;
1062145247Sdamien
1063145247Sdamien	case IPW_STATE_ASSOCIATION_LOST:
1064172567Sthompsa		DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1065178354Ssam			IEEESTATE(vap), sc->flags));
1066178354Ssam		sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1067178354Ssam		if (vap->iv_state == IEEE80211_S_RUN)
1068191746Sthompsa			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1069145247Sdamien		break;
1070145247Sdamien
1071172567Sthompsa	case IPW_STATE_DISABLED:
1072178354Ssam		/* XXX? is this right? */
1073178354Ssam		sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1074178354Ssam		    IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1075172567Sthompsa		DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1076178354Ssam			IEEESTATE(vap), sc->flags));
1077172567Sthompsa		break;
1078172567Sthompsa
1079145247Sdamien	case IPW_STATE_RADIO_DISABLED:
1080178354Ssam		device_printf(sc->sc_dev, "radio turned off\n");
1081178354Ssam		ieee80211_notify_radio(ic, 0);
1082172567Sthompsa		ipw_stop_locked(sc);
1083178354Ssam		/* XXX start polling thread to detect radio on */
1084145247Sdamien		break;
1085172567Sthompsa
1086172567Sthompsa	default:
1087172567Sthompsa		DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1088178354Ssam			__func__, state, IEEESTATE(vap), sc->flags));
1089172567Sthompsa		break;
1090145247Sdamien	}
1091172567Sthompsa#undef IEEESTATE
1092145247Sdamien}
1093145247Sdamien
1094145247Sdamien/*
1095172567Sthompsa * Set driver state for current channel.
1096172567Sthompsa */
1097172567Sthompsastatic void
1098172567Sthompsaipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1099172567Sthompsa{
1100178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1101178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1102172567Sthompsa
1103172567Sthompsa	ic->ic_curchan = chan;
1104192468Ssam	ieee80211_radiotap_chan_change(ic);
1105172567Sthompsa}
1106172567Sthompsa
1107172567Sthompsa/*
1108145247Sdamien * XXX: Hack to set the current channel to the value advertised in beacons or
1109145247Sdamien * probe responses. Only used during AP detection.
1110145247Sdamien */
1111145247Sdamienstatic void
1112172567Sthompsaipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1113145247Sdamien{
1114178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1115178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1116172567Sthompsa	struct ieee80211_channel *c;
1117145247Sdamien	struct ieee80211_frame *wh;
1118145247Sdamien	uint8_t subtype;
1119145247Sdamien	uint8_t *frm, *efrm;
1120145247Sdamien
1121145247Sdamien	wh = mtod(m, struct ieee80211_frame *);
1122145247Sdamien
1123145247Sdamien	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1124145247Sdamien		return;
1125145247Sdamien
1126145247Sdamien	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1127145247Sdamien
1128145247Sdamien	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1129145247Sdamien	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1130145247Sdamien		return;
1131145247Sdamien
1132178354Ssam	/* XXX use ieee80211_parse_beacon */
1133145247Sdamien	frm = (uint8_t *)(wh + 1);
1134145247Sdamien	efrm = mtod(m, uint8_t *) + m->m_len;
1135145247Sdamien
1136145247Sdamien	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1137145247Sdamien	while (frm < efrm) {
1138145247Sdamien		if (*frm == IEEE80211_ELEMID_DSPARMS)
1139145247Sdamien#if IEEE80211_CHAN_MAX < 255
1140145247Sdamien		if (frm[2] <= IEEE80211_CHAN_MAX)
1141145247Sdamien#endif
1142172567Sthompsa		{
1143172567Sthompsa			DPRINTF(("Fixing channel to %d\n", frm[2]));
1144172567Sthompsa			c = ieee80211_find_channel(ic,
1145170530Ssam				ieee80211_ieee2mhz(frm[2], 0),
1146172567Sthompsa				IEEE80211_CHAN_B);
1147172567Sthompsa			if (c == NULL)
1148172567Sthompsa				c = &ic->ic_channels[0];
1149172567Sthompsa			ipw_setcurchan(sc, c);
1150172567Sthompsa		}
1151145247Sdamien
1152145247Sdamien		frm += frm[1] + 2;
1153145247Sdamien	}
1154145247Sdamien}
1155145247Sdamien
1156145247Sdamienstatic void
1157172567Sthompsaipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1158145247Sdamien    struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1159145247Sdamien{
1160178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1161178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1162152385Sdamien	struct mbuf *mnew, *m;
1163145247Sdamien	struct ieee80211_node *ni;
1164145247Sdamien	bus_addr_t physaddr;
1165145247Sdamien	int error;
1166192468Ssam	int8_t rssi, nf;
1167172567Sthompsa	IPW_LOCK_DECL;
1168145247Sdamien
1169152385Sdamien	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1170152385Sdamien	    status->rssi));
1171152385Sdamien
1172147757Sdamien	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1173147757Sdamien	    le32toh(status->len) > MCLBYTES)
1174147757Sdamien		return;
1175147757Sdamien
1176152385Sdamien	/*
1177152385Sdamien	 * Try to allocate a new mbuf for this ring element and load it before
1178152385Sdamien	 * processing the current mbuf. If the ring element cannot be loaded,
1179152385Sdamien	 * drop the received packet and reuse the old mbuf. In the unlikely
1180152385Sdamien	 * case that the old mbuf can't be reloaded either, explicitly panic.
1181152385Sdamien	 */
1182152385Sdamien	mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1183152385Sdamien	if (mnew == NULL) {
1184152385Sdamien		ifp->if_ierrors++;
1185152385Sdamien		return;
1186152385Sdamien	}
1187152385Sdamien
1188145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1189145247Sdamien	bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1190145247Sdamien
1191152385Sdamien	error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1192152385Sdamien	    MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1193152385Sdamien	if (error != 0) {
1194152385Sdamien		m_freem(mnew);
1195152385Sdamien
1196152385Sdamien		/* try to reload the old mbuf */
1197152385Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1198152385Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1199152385Sdamien		    &physaddr, 0);
1200152385Sdamien		if (error != 0) {
1201152385Sdamien			/* very unlikely that it will fail... */
1202152385Sdamien			panic("%s: could not load old rx mbuf",
1203152385Sdamien			    device_get_name(sc->sc_dev));
1204152385Sdamien		}
1205152385Sdamien		ifp->if_ierrors++;
1206152385Sdamien		return;
1207152385Sdamien	}
1208152385Sdamien
1209152385Sdamien	/*
1210152385Sdamien	 * New mbuf successfully loaded, update Rx ring and continue
1211152385Sdamien	 * processing.
1212152385Sdamien	 */
1213152385Sdamien	m = sbuf->m;
1214152385Sdamien	sbuf->m = mnew;
1215152385Sdamien	sbd->bd->physaddr = htole32(physaddr);
1216152385Sdamien
1217145247Sdamien	/* finalize mbuf */
1218145247Sdamien	m->m_pkthdr.rcvif = ifp;
1219145247Sdamien	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1220145247Sdamien
1221192468Ssam	rssi = status->rssi + IPW_RSSI_TO_DBM;
1222192468Ssam	nf = -95;
1223192468Ssam	if (ieee80211_radiotap_active(ic)) {
1224145247Sdamien		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1225145247Sdamien
1226145247Sdamien		tap->wr_flags = 0;
1227192468Ssam		tap->wr_antsignal = rssi;
1228192468Ssam		tap->wr_antnoise = nf;
1229145247Sdamien	}
1230145247Sdamien
1231172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
1232172567Sthompsa		ipw_fix_channel(sc, m);
1233145247Sdamien
1234172567Sthompsa	IPW_UNLOCK(sc);
1235178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1236178354Ssam	if (ni != NULL) {
1237192468Ssam		(void) ieee80211_input(ni, m, rssi, nf);
1238178354Ssam		ieee80211_free_node(ni);
1239178354Ssam	} else
1240192468Ssam		(void) ieee80211_input_all(ic, m, rssi, nf);
1241172567Sthompsa	IPW_LOCK(sc);
1242145247Sdamien
1243145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1244145247Sdamien}
1245145247Sdamien
1246145247Sdamienstatic void
1247145247Sdamienipw_rx_intr(struct ipw_softc *sc)
1248145247Sdamien{
1249145247Sdamien	struct ipw_status *status;
1250145247Sdamien	struct ipw_soft_bd *sbd;
1251145247Sdamien	struct ipw_soft_buf *sbuf;
1252145247Sdamien	uint32_t r, i;
1253145247Sdamien
1254145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1255145247Sdamien		return;
1256145247Sdamien
1257145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1258145247Sdamien
1259145247Sdamien	bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1260145247Sdamien
1261145247Sdamien	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1262145247Sdamien		status = &sc->status_list[i];
1263145247Sdamien		sbd = &sc->srbd_list[i];
1264145247Sdamien		sbuf = sbd->priv;
1265145247Sdamien
1266145247Sdamien		switch (le16toh(status->code) & 0xf) {
1267145247Sdamien		case IPW_STATUS_CODE_COMMAND:
1268172567Sthompsa			ipw_rx_cmd_intr(sc, sbuf);
1269145247Sdamien			break;
1270145247Sdamien
1271145247Sdamien		case IPW_STATUS_CODE_NEWSTATE:
1272172567Sthompsa			ipw_rx_newstate_intr(sc, sbuf);
1273145247Sdamien			break;
1274145247Sdamien
1275145247Sdamien		case IPW_STATUS_CODE_DATA_802_3:
1276145247Sdamien		case IPW_STATUS_CODE_DATA_802_11:
1277172567Sthompsa			ipw_rx_data_intr(sc, status, sbd, sbuf);
1278145247Sdamien			break;
1279145247Sdamien
1280145247Sdamien		case IPW_STATUS_CODE_NOTIFICATION:
1281172567Sthompsa			DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1282172567Sthompsa			    le32toh(status->len), status->flags));
1283178354Ssam			/* XXX maybe drive state machine AUTH->ASSOC? */
1284145247Sdamien			break;
1285145247Sdamien
1286145247Sdamien		default:
1287172567Sthompsa			device_printf(sc->sc_dev, "unexpected status code %u\n",
1288145247Sdamien			    le16toh(status->code));
1289145247Sdamien		}
1290145247Sdamien
1291145247Sdamien		/* firmware was killed, stop processing received frames */
1292145247Sdamien		if (!(sc->flags & IPW_FLAG_FW_INITED))
1293145247Sdamien			return;
1294145247Sdamien
1295145247Sdamien		sbd->bd->flags = 0;
1296145247Sdamien	}
1297145247Sdamien
1298145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1299145247Sdamien
1300145247Sdamien	/* kick the firmware */
1301145247Sdamien	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1302145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1303145247Sdamien}
1304145247Sdamien
1305145247Sdamienstatic void
1306145247Sdamienipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1307145247Sdamien{
1308145247Sdamien	struct ipw_soft_hdr *shdr;
1309145247Sdamien	struct ipw_soft_buf *sbuf;
1310145247Sdamien
1311145247Sdamien	switch (sbd->type) {
1312145247Sdamien	case IPW_SBD_TYPE_COMMAND:
1313145247Sdamien		bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1314145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1315145247Sdamien		bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1316145247Sdamien		break;
1317145247Sdamien
1318145247Sdamien	case IPW_SBD_TYPE_HEADER:
1319145247Sdamien		shdr = sbd->priv;
1320145247Sdamien		bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1321145247Sdamien		bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1322145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1323145247Sdamien		break;
1324145247Sdamien
1325145247Sdamien	case IPW_SBD_TYPE_DATA:
1326145247Sdamien		sbuf = sbd->priv;
1327145247Sdamien		bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1328145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1329145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1330145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1331145247Sdamien
1332170530Ssam		if (sbuf->m->m_flags & M_TXCB)
1333170530Ssam			ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1334145247Sdamien		m_freem(sbuf->m);
1335145247Sdamien		ieee80211_free_node(sbuf->ni);
1336145247Sdamien
1337145247Sdamien		sc->sc_tx_timer = 0;
1338145247Sdamien		break;
1339145247Sdamien	}
1340145247Sdamien
1341145247Sdamien	sbd->type = IPW_SBD_TYPE_NOASSOC;
1342145247Sdamien}
1343145247Sdamien
1344145247Sdamienstatic void
1345145247Sdamienipw_tx_intr(struct ipw_softc *sc)
1346145247Sdamien{
1347178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1348145247Sdamien	struct ipw_soft_bd *sbd;
1349145247Sdamien	uint32_t r, i;
1350145247Sdamien
1351145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1352145247Sdamien		return;
1353145247Sdamien
1354145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1355145247Sdamien
1356145247Sdamien	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1357145247Sdamien		sbd = &sc->stbd_list[i];
1358145247Sdamien
1359145247Sdamien		if (sbd->type == IPW_SBD_TYPE_DATA)
1360145247Sdamien			ifp->if_opackets++;
1361145247Sdamien
1362145247Sdamien		ipw_release_sbd(sc, sbd);
1363145247Sdamien		sc->txfree++;
1364145247Sdamien	}
1365145247Sdamien
1366145247Sdamien	/* remember what the firmware has processed */
1367145247Sdamien	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1368145247Sdamien
1369148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1370172567Sthompsa	ipw_start_locked(ifp);
1371145247Sdamien}
1372145247Sdamien
1373145247Sdamienstatic void
1374191746Sthompsaipw_fatal_error_intr(struct ipw_softc *sc)
1375191746Sthompsa{
1376191746Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1377191746Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1378191956Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1379191746Sthompsa
1380191746Sthompsa	device_printf(sc->sc_dev, "firmware error\n");
1381191956Sthompsa	if (vap != NULL)
1382191956Sthompsa		ieee80211_cancel_scan(vap);
1383191746Sthompsa	ieee80211_runtask(ic, &sc->sc_init_task);
1384191746Sthompsa}
1385191746Sthompsa
1386191746Sthompsastatic void
1387145247Sdamienipw_intr(void *arg)
1388145247Sdamien{
1389145247Sdamien	struct ipw_softc *sc = arg;
1390145247Sdamien	uint32_t r;
1391172567Sthompsa	IPW_LOCK_DECL;
1392145247Sdamien
1393172567Sthompsa	IPW_LOCK(sc);
1394145247Sdamien
1395191746Sthompsa	r = CSR_READ_4(sc, IPW_CSR_INTR);
1396191746Sthompsa	if (r == 0 || r == 0xffffffff)
1397191746Sthompsa		goto done;
1398145247Sdamien
1399145247Sdamien	/* disable interrupts */
1400145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1401145247Sdamien
1402156599Sdamien	/* acknowledge all interrupts */
1403156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1404156599Sdamien
1405145247Sdamien	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1406191746Sthompsa		ipw_fatal_error_intr(sc);
1407191746Sthompsa		goto done;
1408145247Sdamien	}
1409145247Sdamien
1410156599Sdamien	if (r & IPW_INTR_FW_INIT_DONE)
1411156599Sdamien		wakeup(sc);
1412145247Sdamien
1413145247Sdamien	if (r & IPW_INTR_RX_TRANSFER)
1414145247Sdamien		ipw_rx_intr(sc);
1415145247Sdamien
1416145247Sdamien	if (r & IPW_INTR_TX_TRANSFER)
1417145247Sdamien		ipw_tx_intr(sc);
1418145247Sdamien
1419145247Sdamien	/* re-enable interrupts */
1420145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1421191746Sthompsadone:
1422172567Sthompsa	IPW_UNLOCK(sc);
1423145247Sdamien}
1424145247Sdamien
1425145247Sdamienstatic void
1426145247Sdamienipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1427145247Sdamien{
1428145247Sdamien	if (error != 0)
1429145247Sdamien		return;
1430145247Sdamien
1431145247Sdamien	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1432145247Sdamien
1433145247Sdamien	*(bus_addr_t *)arg = segs[0].ds_addr;
1434145247Sdamien}
1435145247Sdamien
1436172567Sthompsastatic const char *
1437172567Sthompsaipw_cmdname(int cmd)
1438172567Sthompsa{
1439172567Sthompsa#define	N(a)	(sizeof(a) / sizeof(a[0]))
1440172567Sthompsa	static const struct {
1441172567Sthompsa		int	cmd;
1442172567Sthompsa		const char *name;
1443172567Sthompsa	} cmds[] = {
1444172567Sthompsa		{ IPW_CMD_ADD_MULTICAST,	"ADD_MULTICAST" },
1445172567Sthompsa		{ IPW_CMD_BROADCAST_SCAN,	"BROADCAST_SCAN" },
1446172567Sthompsa		{ IPW_CMD_DISABLE,		"DISABLE" },
1447172567Sthompsa		{ IPW_CMD_DISABLE_PHY,		"DISABLE_PHY" },
1448172567Sthompsa		{ IPW_CMD_ENABLE,		"ENABLE" },
1449172567Sthompsa		{ IPW_CMD_PREPARE_POWER_DOWN,	"PREPARE_POWER_DOWN" },
1450172567Sthompsa		{ IPW_CMD_SET_BASIC_TX_RATES,	"SET_BASIC_TX_RATES" },
1451172567Sthompsa		{ IPW_CMD_SET_BEACON_INTERVAL,	"SET_BEACON_INTERVAL" },
1452172567Sthompsa		{ IPW_CMD_SET_CHANNEL,		"SET_CHANNEL" },
1453172567Sthompsa		{ IPW_CMD_SET_CONFIGURATION,	"SET_CONFIGURATION" },
1454172567Sthompsa		{ IPW_CMD_SET_DESIRED_BSSID,	"SET_DESIRED_BSSID" },
1455172567Sthompsa		{ IPW_CMD_SET_ESSID,		"SET_ESSID" },
1456172567Sthompsa		{ IPW_CMD_SET_FRAG_THRESHOLD,	"SET_FRAG_THRESHOLD" },
1457172567Sthompsa		{ IPW_CMD_SET_MAC_ADDRESS,	"SET_MAC_ADDRESS" },
1458172567Sthompsa		{ IPW_CMD_SET_MANDATORY_BSSID,	"SET_MANDATORY_BSSID" },
1459172567Sthompsa		{ IPW_CMD_SET_MODE,		"SET_MODE" },
1460172567Sthompsa		{ IPW_CMD_SET_MSDU_TX_RATES,	"SET_MSDU_TX_RATES" },
1461172567Sthompsa		{ IPW_CMD_SET_POWER_MODE,	"SET_POWER_MODE" },
1462172567Sthompsa		{ IPW_CMD_SET_RTS_THRESHOLD,	"SET_RTS_THRESHOLD" },
1463172567Sthompsa		{ IPW_CMD_SET_SCAN_OPTIONS,	"SET_SCAN_OPTIONS" },
1464172567Sthompsa		{ IPW_CMD_SET_SECURITY_INFO,	"SET_SECURITY_INFO" },
1465172567Sthompsa		{ IPW_CMD_SET_TX_POWER_INDEX,	"SET_TX_POWER_INDEX" },
1466172567Sthompsa		{ IPW_CMD_SET_TX_RATES,		"SET_TX_RATES" },
1467172567Sthompsa		{ IPW_CMD_SET_WEP_FLAGS,	"SET_WEP_FLAGS" },
1468172567Sthompsa		{ IPW_CMD_SET_WEP_KEY,		"SET_WEP_KEY" },
1469172567Sthompsa		{ IPW_CMD_SET_WEP_KEY_INDEX,	"SET_WEP_KEY_INDEX" },
1470172567Sthompsa		{ IPW_CMD_SET_WPA_IE,		"SET_WPA_IE" },
1471172567Sthompsa
1472172567Sthompsa	};
1473172567Sthompsa	static char buf[12];
1474172567Sthompsa	int i;
1475172567Sthompsa
1476172567Sthompsa	for (i = 0; i < N(cmds); i++)
1477172567Sthompsa		if (cmds[i].cmd == cmd)
1478172567Sthompsa			return cmds[i].name;
1479172567Sthompsa	snprintf(buf, sizeof(buf), "%u", cmd);
1480172567Sthompsa	return buf;
1481172567Sthompsa#undef N
1482172567Sthompsa}
1483172567Sthompsa
1484145247Sdamien/*
1485145247Sdamien * Send a command to the firmware and wait for the acknowledgement.
1486145247Sdamien */
1487145247Sdamienstatic int
1488145247Sdamienipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1489145247Sdamien{
1490145247Sdamien	struct ipw_soft_bd *sbd;
1491145247Sdamien	bus_addr_t physaddr;
1492145247Sdamien	int error;
1493145247Sdamien
1494178354Ssam	IPW_LOCK_ASSERT(sc);
1495178354Ssam
1496172567Sthompsa	if (sc->flags & IPW_FLAG_BUSY) {
1497172567Sthompsa		device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1498172567Sthompsa			__func__, ipw_cmdname(type));
1499172567Sthompsa		return EAGAIN;
1500172567Sthompsa	}
1501172567Sthompsa	sc->flags |= IPW_FLAG_BUSY;
1502172567Sthompsa
1503145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1504145247Sdamien
1505145247Sdamien	error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1506145247Sdamien	    sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1507145247Sdamien	if (error != 0) {
1508145247Sdamien		device_printf(sc->sc_dev, "could not map command DMA memory\n");
1509172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1510145247Sdamien		return error;
1511145247Sdamien	}
1512145247Sdamien
1513145247Sdamien	sc->cmd.type = htole32(type);
1514145247Sdamien	sc->cmd.subtype = 0;
1515145247Sdamien	sc->cmd.len = htole32(len);
1516145247Sdamien	sc->cmd.seq = 0;
1517152637Sdamien	memcpy(sc->cmd.data, data, len);
1518145247Sdamien
1519145247Sdamien	sbd->type = IPW_SBD_TYPE_COMMAND;
1520145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1521145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1522145247Sdamien	sbd->bd->nfrag = 1;
1523145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1524145247Sdamien	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1525145247Sdamien
1526145247Sdamien	bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1527145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1528145247Sdamien
1529172567Sthompsa#ifdef IPW_DEBUG
1530172567Sthompsa	if (ipw_debug >= 4) {
1531172567Sthompsa		printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1532172567Sthompsa		    0, 0, len);
1533172567Sthompsa		/* Print the data buffer in the higher debug level */
1534172567Sthompsa		if (ipw_debug >= 9 && len > 0) {
1535172567Sthompsa			printf(" data: 0x");
1536172567Sthompsa			for (int i = 1; i <= len; i++)
1537174269Swkoszek				printf("%1D", (u_char *)data + len - i, "");
1538172567Sthompsa		}
1539172567Sthompsa		printf("\n");
1540172567Sthompsa	}
1541172567Sthompsa#endif
1542145247Sdamien
1543145247Sdamien	/* kick firmware */
1544145247Sdamien	sc->txfree--;
1545145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1546145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1547145247Sdamien
1548145247Sdamien	/* wait at most one second for command to complete */
1549172567Sthompsa	error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1550172567Sthompsa	if (error != 0) {
1551172567Sthompsa		device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1552172567Sthompsa		    __func__, ipw_cmdname(type), error);
1553172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1554172567Sthompsa		return (error);
1555172567Sthompsa	}
1556172567Sthompsa	return (0);
1557145247Sdamien}
1558145247Sdamien
1559145247Sdamienstatic int
1560145247Sdamienipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1561145247Sdamien{
1562145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1563178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1564192468Ssam	struct ieee80211vap *vap = ni->ni_vap;
1565145247Sdamien	struct ieee80211_frame *wh;
1566145247Sdamien	struct ipw_soft_bd *sbd;
1567145247Sdamien	struct ipw_soft_hdr *shdr;
1568145247Sdamien	struct ipw_soft_buf *sbuf;
1569145247Sdamien	struct ieee80211_key *k;
1570145247Sdamien	struct mbuf *mnew;
1571145247Sdamien	bus_dma_segment_t segs[IPW_MAX_NSEG];
1572145247Sdamien	bus_addr_t physaddr;
1573145247Sdamien	int nsegs, error, i;
1574145247Sdamien
1575145247Sdamien	wh = mtod(m0, struct ieee80211_frame *);
1576145247Sdamien
1577145247Sdamien	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1578178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1579147806Ssam		if (k == NULL) {
1580147806Ssam			m_freem(m0);
1581145247Sdamien			return ENOBUFS;
1582147806Ssam		}
1583145247Sdamien		/* packet header may have moved, reset our local pointer */
1584145247Sdamien		wh = mtod(m0, struct ieee80211_frame *);
1585145247Sdamien	}
1586145247Sdamien
1587192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1588145247Sdamien		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1589145247Sdamien
1590145247Sdamien		tap->wt_flags = 0;
1591145247Sdamien
1592192468Ssam		ieee80211_radiotap_tx(vap, m0);
1593145247Sdamien	}
1594145247Sdamien
1595145247Sdamien	shdr = SLIST_FIRST(&sc->free_shdr);
1596145247Sdamien	sbuf = SLIST_FIRST(&sc->free_sbuf);
1597145247Sdamien	KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1598145247Sdamien
1599145247Sdamien	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1600145247Sdamien	shdr->hdr.subtype = 0;
1601145247Sdamien	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1602145247Sdamien	shdr->hdr.encrypt = 0;
1603145247Sdamien	shdr->hdr.keyidx = 0;
1604145247Sdamien	shdr->hdr.keysz = 0;
1605145247Sdamien	shdr->hdr.fragmentsz = 0;
1606145247Sdamien	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1607145247Sdamien	if (ic->ic_opmode == IEEE80211_M_STA)
1608145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1609145247Sdamien	else
1610145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1611145247Sdamien
1612145247Sdamien	/* trim IEEE802.11 header */
1613145247Sdamien	m_adj(m0, sizeof (struct ieee80211_frame));
1614145247Sdamien
1615145247Sdamien	error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1616145247Sdamien	    &nsegs, 0);
1617145247Sdamien	if (error != 0 && error != EFBIG) {
1618145247Sdamien		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1619145247Sdamien		    error);
1620145247Sdamien		m_freem(m0);
1621145247Sdamien		return error;
1622145247Sdamien	}
1623145247Sdamien	if (error != 0) {
1624145247Sdamien		mnew = m_defrag(m0, M_DONTWAIT);
1625145247Sdamien		if (mnew == NULL) {
1626145247Sdamien			device_printf(sc->sc_dev,
1627145247Sdamien			    "could not defragment mbuf\n");
1628145247Sdamien			m_freem(m0);
1629145247Sdamien			return ENOBUFS;
1630145247Sdamien		}
1631145247Sdamien		m0 = mnew;
1632145247Sdamien
1633145247Sdamien		error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1634145247Sdamien		    segs, &nsegs, 0);
1635145247Sdamien		if (error != 0) {
1636145247Sdamien			device_printf(sc->sc_dev,
1637145247Sdamien			    "could not map mbuf (error %d)\n", error);
1638145247Sdamien			m_freem(m0);
1639145247Sdamien			return error;
1640145247Sdamien		}
1641145247Sdamien	}
1642145247Sdamien
1643145247Sdamien	error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1644145247Sdamien	    sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1645145247Sdamien	if (error != 0) {
1646145247Sdamien		device_printf(sc->sc_dev, "could not map header DMA memory\n");
1647145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1648145247Sdamien		m_freem(m0);
1649145247Sdamien		return error;
1650145247Sdamien	}
1651145247Sdamien
1652145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1653145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1654145247Sdamien
1655145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1656145247Sdamien	sbd->type = IPW_SBD_TYPE_HEADER;
1657145247Sdamien	sbd->priv = shdr;
1658145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1659145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1660145247Sdamien	sbd->bd->nfrag = 1 + nsegs;
1661145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1662145247Sdamien	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1663145247Sdamien
1664145247Sdamien	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1665145247Sdamien	    shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1666145247Sdamien	    shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1667145247Sdamien	    ":"));
1668145247Sdamien
1669145247Sdamien	sc->txfree--;
1670145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1671145247Sdamien
1672145247Sdamien	sbuf->m = m0;
1673145247Sdamien	sbuf->ni = ni;
1674145247Sdamien
1675145247Sdamien	for (i = 0; i < nsegs; i++) {
1676145247Sdamien		sbd = &sc->stbd_list[sc->txcur];
1677145247Sdamien
1678145247Sdamien		sbd->bd->physaddr = htole32(segs[i].ds_addr);
1679145247Sdamien		sbd->bd->len = htole32(segs[i].ds_len);
1680145247Sdamien		sbd->bd->nfrag = 0;
1681145247Sdamien		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1682145247Sdamien		if (i == nsegs - 1) {
1683145247Sdamien			sbd->type = IPW_SBD_TYPE_DATA;
1684145247Sdamien			sbd->priv = sbuf;
1685145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1686145247Sdamien		} else {
1687145247Sdamien			sbd->type = IPW_SBD_TYPE_NOASSOC;
1688145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1689145247Sdamien		}
1690145247Sdamien
1691172574Sthompsa		DPRINTFN(5, ("sending fragment (%d)\n", i));
1692145247Sdamien
1693145247Sdamien		sc->txfree--;
1694145247Sdamien		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1695145247Sdamien	}
1696145247Sdamien
1697145247Sdamien	bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1698145247Sdamien	bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1699145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1700145247Sdamien
1701145247Sdamien	/* kick firmware */
1702145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1703145247Sdamien
1704145247Sdamien	return 0;
1705145247Sdamien}
1706145247Sdamien
1707178354Ssamstatic int
1708178354Ssamipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1709178354Ssam	const struct ieee80211_bpf_params *params)
1710178354Ssam{
1711178354Ssam	/* no support; just discard */
1712178354Ssam	m_freem(m);
1713178354Ssam	ieee80211_free_node(ni);
1714178354Ssam	return 0;
1715178354Ssam}
1716178354Ssam
1717145247Sdamienstatic void
1718145247Sdamienipw_start(struct ifnet *ifp)
1719145247Sdamien{
1720145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1721172567Sthompsa	IPW_LOCK_DECL;
1722172567Sthompsa
1723172567Sthompsa	IPW_LOCK(sc);
1724172567Sthompsa	ipw_start_locked(ifp);
1725172567Sthompsa	IPW_UNLOCK(sc);
1726172567Sthompsa}
1727172567Sthompsa
1728172567Sthompsastatic void
1729172567Sthompsaipw_start_locked(struct ifnet *ifp)
1730172567Sthompsa{
1731172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
1732145247Sdamien	struct ieee80211_node *ni;
1733178354Ssam	struct mbuf *m;
1734145247Sdamien
1735172567Sthompsa	IPW_LOCK_ASSERT(sc);
1736145247Sdamien
1737145247Sdamien	for (;;) {
1738178354Ssam		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1739178354Ssam		if (m == NULL)
1740145247Sdamien			break;
1741145247Sdamien		if (sc->txfree < 1 + IPW_MAX_NSEG) {
1742178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1743148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1744145247Sdamien			break;
1745145247Sdamien		}
1746178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1747178354Ssam		if (ipw_tx_start(ifp, m, ni) != 0) {
1748145247Sdamien			ieee80211_free_node(ni);
1749145247Sdamien			ifp->if_oerrors++;
1750145247Sdamien			break;
1751145247Sdamien		}
1752145247Sdamien		/* start watchdog timer */
1753145247Sdamien		sc->sc_tx_timer = 5;
1754145247Sdamien	}
1755145247Sdamien}
1756145247Sdamien
1757145247Sdamienstatic void
1758172567Sthompsaipw_watchdog(void *arg)
1759145247Sdamien{
1760172567Sthompsa	struct ipw_softc *sc = arg;
1761172567Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1762178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1763145247Sdamien
1764172567Sthompsa	IPW_LOCK_ASSERT(sc);
1765156599Sdamien
1766145247Sdamien	if (sc->sc_tx_timer > 0) {
1767145247Sdamien		if (--sc->sc_tx_timer == 0) {
1768145247Sdamien			if_printf(ifp, "device timeout\n");
1769145247Sdamien			ifp->if_oerrors++;
1770178354Ssam			taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1771145247Sdamien		}
1772145247Sdamien	}
1773172567Sthompsa	if (sc->sc_scan_timer > 0) {
1774172567Sthompsa		if (--sc->sc_scan_timer == 0) {
1775172567Sthompsa			DPRINTFN(3, ("Scan timeout\n"));
1776172567Sthompsa			/* End the scan */
1777172567Sthompsa			if (sc->flags & IPW_FLAG_SCANNING) {
1778178354Ssam				ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1779172567Sthompsa				sc->flags &= ~IPW_FLAG_SCANNING;
1780172567Sthompsa			}
1781172567Sthompsa		}
1782172567Sthompsa	}
1783172567Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1784172567Sthompsa		callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1785145247Sdamien}
1786145247Sdamien
1787145247Sdamienstatic int
1788145247Sdamienipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1789145247Sdamien{
1790145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1791178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1792178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
1793178354Ssam	int error = 0, startall = 0;
1794172567Sthompsa	IPW_LOCK_DECL;
1795145247Sdamien
1796145247Sdamien	switch (cmd) {
1797145247Sdamien	case SIOCSIFFLAGS:
1798178704Sthompsa		IPW_LOCK(sc);
1799145247Sdamien		if (ifp->if_flags & IFF_UP) {
1800178354Ssam			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1801178354Ssam				ipw_init_locked(sc);
1802178354Ssam				startall = 1;
1803178354Ssam			}
1804145247Sdamien		} else {
1805148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1806172567Sthompsa				ipw_stop_locked(sc);
1807145247Sdamien		}
1808178704Sthompsa		IPW_UNLOCK(sc);
1809178704Sthompsa		if (startall)
1810178704Sthompsa			ieee80211_start_all(ic);
1811145247Sdamien		break;
1812178354Ssam	case SIOCGIFMEDIA:
1813178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1814178354Ssam		break;
1815178704Sthompsa	case SIOCGIFADDR:
1816178704Sthompsa		error = ether_ioctl(ifp, cmd, data);
1817178704Sthompsa		break;
1818145247Sdamien	default:
1819178704Sthompsa		error = EINVAL;
1820178704Sthompsa		break;
1821145247Sdamien	}
1822145247Sdamien	return error;
1823145247Sdamien}
1824145247Sdamien
1825145247Sdamienstatic void
1826145247Sdamienipw_stop_master(struct ipw_softc *sc)
1827145247Sdamien{
1828156599Sdamien	uint32_t tmp;
1829145247Sdamien	int ntries;
1830145247Sdamien
1831145247Sdamien	/* disable interrupts */
1832145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1833145247Sdamien
1834145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1835145247Sdamien	for (ntries = 0; ntries < 50; ntries++) {
1836145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1837145247Sdamien			break;
1838145247Sdamien		DELAY(10);
1839145247Sdamien	}
1840145247Sdamien	if (ntries == 50)
1841145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for master\n");
1842145247Sdamien
1843156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1844156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1845145247Sdamien
1846172567Sthompsa	/* Clear all flags except the following */
1847172567Sthompsa	sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1848145247Sdamien}
1849145247Sdamien
1850145247Sdamienstatic int
1851145247Sdamienipw_reset(struct ipw_softc *sc)
1852145247Sdamien{
1853156599Sdamien	uint32_t tmp;
1854145247Sdamien	int ntries;
1855145247Sdamien
1856145247Sdamien	ipw_stop_master(sc);
1857145247Sdamien
1858145247Sdamien	/* move adapter to D0 state */
1859156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1860156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1861145247Sdamien
1862145247Sdamien	/* wait for clock stabilization */
1863145247Sdamien	for (ntries = 0; ntries < 1000; ntries++) {
1864145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1865145247Sdamien			break;
1866145247Sdamien		DELAY(200);
1867145247Sdamien	}
1868145247Sdamien	if (ntries == 1000)
1869145247Sdamien		return EIO;
1870145247Sdamien
1871156599Sdamien	tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1872156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1873145247Sdamien
1874145247Sdamien	DELAY(10);
1875145247Sdamien
1876156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1877156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1878145247Sdamien
1879145247Sdamien	return 0;
1880145247Sdamien}
1881145247Sdamien
1882172567Sthompsastatic int
1883172567Sthompsaipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1884172567Sthompsa{
1885172567Sthompsa	int ms = hz < 1000 ? 1 : hz/10;
1886172567Sthompsa	int i, error;
1887172567Sthompsa
1888172567Sthompsa	for (i = 0; i < 100; i++) {
1889172567Sthompsa		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1890172567Sthompsa			return 0;
1891172567Sthompsa		error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1892172567Sthompsa		if (error == 0 || error != EWOULDBLOCK)
1893172567Sthompsa			return 0;
1894172567Sthompsa	}
1895172567Sthompsa	DPRINTF(("%s: timeout waiting for %s\n",
1896172567Sthompsa		__func__, waitfor ? "disable" : "enable"));
1897172567Sthompsa	return ETIMEDOUT;
1898172567Sthompsa}
1899172567Sthompsa
1900172567Sthompsastatic int
1901172567Sthompsaipw_enable(struct ipw_softc *sc)
1902172567Sthompsa{
1903172567Sthompsa	int error;
1904172567Sthompsa
1905172567Sthompsa	if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1906172567Sthompsa		DPRINTF(("Enable adapter\n"));
1907172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1908172567Sthompsa		if (error != 0)
1909172567Sthompsa			return error;
1910172567Sthompsa		error = ipw_waitfordisable(sc, 0);
1911172567Sthompsa		if (error != 0)
1912172567Sthompsa			return error;
1913172567Sthompsa		sc->flags |= IPW_FLAG_ENABLED;
1914172567Sthompsa	}
1915172567Sthompsa	return 0;
1916172567Sthompsa}
1917172567Sthompsa
1918172567Sthompsastatic int
1919172567Sthompsaipw_disable(struct ipw_softc *sc)
1920172567Sthompsa{
1921172567Sthompsa	int error;
1922172567Sthompsa
1923172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
1924172567Sthompsa		DPRINTF(("Disable adapter\n"));
1925172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1926172567Sthompsa		if (error != 0)
1927172567Sthompsa			return error;
1928172567Sthompsa		error = ipw_waitfordisable(sc, 1);
1929172567Sthompsa		if (error != 0)
1930172567Sthompsa			return error;
1931172567Sthompsa		sc->flags &= ~IPW_FLAG_ENABLED;
1932172567Sthompsa	}
1933172567Sthompsa	return 0;
1934172567Sthompsa}
1935172567Sthompsa
1936145247Sdamien/*
1937145247Sdamien * Upload the microcode to the device.
1938145247Sdamien */
1939145247Sdamienstatic int
1940156599Sdamienipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1941145247Sdamien{
1942145247Sdamien	int ntries;
1943145247Sdamien
1944145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1945145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1946145247Sdamien
1947145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1948145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1949145247Sdamien
1950145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1951145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1952145247Sdamien
1953145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1954145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1955145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1956145247Sdamien
1957145247Sdamien	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1958145247Sdamien
1959145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1960145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1961145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1962145247Sdamien
1963145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1964145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1965145247Sdamien
1966145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1967145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1968145247Sdamien
1969145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1970145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1971145247Sdamien
1972145247Sdamien	for (ntries = 0; ntries < 10; ntries++) {
1973145247Sdamien		if (MEM_READ_1(sc, 0x210000) & 1)
1974145247Sdamien			break;
1975145247Sdamien		DELAY(10);
1976145247Sdamien	}
1977145247Sdamien	if (ntries == 10) {
1978145247Sdamien		device_printf(sc->sc_dev,
1979145247Sdamien		    "timeout waiting for ucode to initialize\n");
1980145247Sdamien		return EIO;
1981145247Sdamien	}
1982145247Sdamien
1983145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0);
1984145247Sdamien
1985145247Sdamien	return 0;
1986145247Sdamien}
1987145247Sdamien
1988145247Sdamien/* set of macros to handle unaligned little endian data in firmware image */
1989145247Sdamien#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1990145247Sdamien#define GETLE16(p) ((p)[0] | (p)[1] << 8)
1991145247Sdamienstatic int
1992156599Sdamienipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
1993145247Sdamien{
1994156599Sdamien	const uint8_t *p, *end;
1995156599Sdamien	uint32_t tmp, dst;
1996145247Sdamien	uint16_t len;
1997145247Sdamien	int error;
1998145247Sdamien
1999145247Sdamien	p = fw;
2000145247Sdamien	end = fw + size;
2001145247Sdamien	while (p < end) {
2002145247Sdamien		dst = GETLE32(p); p += 4;
2003145247Sdamien		len = GETLE16(p); p += 2;
2004145247Sdamien
2005145247Sdamien		ipw_write_mem_1(sc, dst, p, len);
2006145247Sdamien		p += len;
2007145247Sdamien	}
2008145247Sdamien
2009145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
2010145247Sdamien	    IPW_IO_LED_OFF);
2011145247Sdamien
2012145247Sdamien	/* enable interrupts */
2013145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
2014145247Sdamien
2015145247Sdamien	/* kick the firmware */
2016145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
2017145247Sdamien
2018156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
2019156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
2020145247Sdamien
2021145247Sdamien	/* wait at most one second for firmware initialization to complete */
2022145247Sdamien	if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
2023145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for firmware "
2024145247Sdamien		    "initialization to complete\n");
2025145247Sdamien		return error;
2026145247Sdamien	}
2027145247Sdamien
2028156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_IO);
2029156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2030156599Sdamien	    IPW_IO_GPIO3_MASK);
2031145247Sdamien
2032145247Sdamien	return 0;
2033145247Sdamien}
2034145247Sdamien
2035145247Sdamienstatic int
2036172567Sthompsaipw_setwepkeys(struct ipw_softc *sc)
2037172567Sthompsa{
2038178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2039178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2040178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2041172567Sthompsa	struct ipw_wep_key wepkey;
2042172567Sthompsa	struct ieee80211_key *wk;
2043172567Sthompsa	int error, i;
2044172567Sthompsa
2045172567Sthompsa	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2046178354Ssam		wk = &vap->iv_nw_keys[i];
2047172567Sthompsa
2048172567Sthompsa		if (wk->wk_cipher == NULL ||
2049172567Sthompsa		    wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2050172567Sthompsa			continue;
2051172567Sthompsa
2052172567Sthompsa		wepkey.idx = i;
2053172567Sthompsa		wepkey.len = wk->wk_keylen;
2054172567Sthompsa		memset(wepkey.key, 0, sizeof wepkey.key);
2055172567Sthompsa		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2056172567Sthompsa		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2057172567Sthompsa		    wepkey.len));
2058172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2059172567Sthompsa		    sizeof wepkey);
2060172567Sthompsa		if (error != 0)
2061172567Sthompsa			return error;
2062172567Sthompsa	}
2063172567Sthompsa	return 0;
2064172567Sthompsa}
2065172567Sthompsa
2066172567Sthompsastatic int
2067172567Sthompsaipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2068172567Sthompsa{
2069172567Sthompsa	struct ipw_wpa_ie wpaie;
2070172567Sthompsa
2071172567Sthompsa	memset(&wpaie, 0, sizeof(wpaie));
2072172567Sthompsa	wpaie.len = htole32(ielen);
2073172567Sthompsa	/* XXX verify length */
2074172567Sthompsa	memcpy(&wpaie.ie, ie, ielen);
2075172567Sthompsa	DPRINTF(("Setting WPA IE\n"));
2076172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2077172567Sthompsa}
2078172567Sthompsa
2079172567Sthompsastatic int
2080172567Sthompsaipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2081172567Sthompsa{
2082172567Sthompsa	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2083172567Sthompsa
2084172567Sthompsa	if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2085172567Sthompsa		DPRINTF(("Setting mandatory BSSID to null\n"));
2086172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2087172567Sthompsa	} else {
2088172567Sthompsa		DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2089172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2090172567Sthompsa			bssid, IEEE80211_ADDR_LEN);
2091172567Sthompsa	}
2092172567Sthompsa}
2093172567Sthompsa
2094172567Sthompsastatic int
2095172567Sthompsaipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2096172567Sthompsa{
2097172567Sthompsa	if (ssidlen == 0) {
2098172567Sthompsa		/*
2099172567Sthompsa		 * A bug in the firmware breaks the ``don't associate''
2100172567Sthompsa		 * bit in the scan options command.  To compensate for
2101172567Sthompsa		 * this install a bogus ssid when no ssid is specified
2102172567Sthompsa		 * so the firmware won't try to associate.
2103172567Sthompsa		 */
2104172567Sthompsa		DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2105172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2106172567Sthompsa			"\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2107172567Sthompsa			"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2108172567Sthompsa			"\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2109172567Sthompsa			"\x3c\x3d", IEEE80211_NWID_LEN);
2110172567Sthompsa	} else {
2111172567Sthompsa#ifdef IPW_DEBUG
2112172567Sthompsa		if (ipw_debug > 0) {
2113172567Sthompsa			printf("Setting ESSID to ");
2114172567Sthompsa			ieee80211_print_essid(ssid, ssidlen);
2115172567Sthompsa			printf("\n");
2116172567Sthompsa		}
2117172567Sthompsa#endif
2118172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2119172567Sthompsa	}
2120172567Sthompsa}
2121172567Sthompsa
2122172567Sthompsastatic int
2123172567Sthompsaipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2124172567Sthompsa{
2125172567Sthompsa	struct ipw_scan_options opts;
2126172567Sthompsa
2127172567Sthompsa	DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2128172567Sthompsa	opts.channels = htole32(chanmask);
2129172567Sthompsa	opts.flags = htole32(flags);
2130172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2131172567Sthompsa}
2132172567Sthompsa
2133172567Sthompsastatic int
2134172567Sthompsaipw_scan(struct ipw_softc *sc)
2135172567Sthompsa{
2136172567Sthompsa	uint32_t params;
2137172567Sthompsa	int error;
2138172567Sthompsa
2139172567Sthompsa	DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2140172567Sthompsa
2141172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
2142172567Sthompsa		return (EBUSY);
2143172567Sthompsa	sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2144172567Sthompsa
2145172567Sthompsa	/* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2146172567Sthompsa	error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2147172567Sthompsa	if (error != 0)
2148172567Sthompsa		goto done;
2149172567Sthompsa
2150172567Sthompsa	/*
2151172567Sthompsa	 * Setup null/bogus ssid so firmware doesn't use any previous
2152172567Sthompsa	 * ssid to try and associate.  This is because the ``don't
2153172567Sthompsa	 * associate'' option bit is broken (sigh).
2154172567Sthompsa	 */
2155172567Sthompsa	error = ipw_setssid(sc, NULL, 0);
2156172567Sthompsa	if (error != 0)
2157172567Sthompsa		goto done;
2158172567Sthompsa
2159172567Sthompsa	/*
2160172567Sthompsa	 * NB: the adapter may be disabled on association lost;
2161172567Sthompsa	 *     if so just re-enable it to kick off scanning.
2162172567Sthompsa	 */
2163172567Sthompsa	DPRINTF(("Starting scan\n"));
2164172567Sthompsa	sc->sc_scan_timer = 3;
2165172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
2166172567Sthompsa		params = 0;				/* XXX? */
2167172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2168172567Sthompsa				&params, sizeof(params));
2169172567Sthompsa	} else
2170172567Sthompsa		error = ipw_enable(sc);
2171172567Sthompsadone:
2172172567Sthompsa	if (error != 0) {
2173172567Sthompsa		DPRINTF(("Scan failed\n"));
2174172567Sthompsa		sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2175172567Sthompsa	}
2176172567Sthompsa	return (error);
2177172567Sthompsa}
2178172567Sthompsa
2179172567Sthompsastatic int
2180172567Sthompsaipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2181172567Sthompsa{
2182178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2183178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2184172567Sthompsa	uint32_t data;
2185172567Sthompsa	int error;
2186172567Sthompsa
2187172567Sthompsa	data = htole32(ieee80211_chan2ieee(ic, chan));
2188172567Sthompsa	DPRINTF(("Setting channel to %u\n", le32toh(data)));
2189172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2190172567Sthompsa	if (error == 0)
2191172567Sthompsa		ipw_setcurchan(sc, chan);
2192172567Sthompsa	return error;
2193172567Sthompsa}
2194172567Sthompsa
2195178354Ssamstatic void
2196191746Sthompsaipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2197145247Sdamien{
2198178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2199178354Ssam	struct ipw_softc *sc = ifp->if_softc;
2200178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2201145247Sdamien	struct ipw_security security;
2202145247Sdamien	uint32_t data;
2203172567Sthompsa	int error;
2204178354Ssam	IPW_LOCK_DECL;
2205145247Sdamien
2206178354Ssam	IPW_LOCK(sc);
2207172567Sthompsa	error = ipw_disable(sc);
2208172567Sthompsa	if (error != 0)
2209178354Ssam		goto done;
2210172567Sthompsa
2211152637Sdamien	memset(&security, 0, sizeof security);
2212178354Ssam	security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2213145247Sdamien	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2214145247Sdamien	security.ciphers = htole32(IPW_CIPHER_NONE);
2215145247Sdamien	DPRINTF(("Setting authmode to %u\n", security.authmode));
2216172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2217145247Sdamien	    sizeof security);
2218145247Sdamien	if (error != 0)
2219178354Ssam		goto done;
2220145247Sdamien
2221178354Ssam	data = htole32(vap->iv_rtsthreshold);
2222178354Ssam	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2223178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2224145247Sdamien	if (error != 0)
2225178354Ssam		goto done;
2226145247Sdamien
2227178354Ssam	data = htole32(vap->iv_fragthreshold);
2228178354Ssam	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2229178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2230145247Sdamien	if (error != 0)
2231178354Ssam		goto done;
2232145247Sdamien
2233178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2234172567Sthompsa		error = ipw_setwepkeys(sc);
2235172567Sthompsa		if (error != 0)
2236178354Ssam			goto done;
2237172567Sthompsa
2238178354Ssam		if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2239178354Ssam			data = htole32(vap->iv_def_txkey);
2240172567Sthompsa			DPRINTF(("Setting wep tx key index to %u\n",
2241172567Sthompsa				le32toh(data)));
2242172567Sthompsa			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2243172567Sthompsa			    sizeof data);
2244172567Sthompsa			if (error != 0)
2245178354Ssam				goto done;
2246172567Sthompsa		}
2247172567Sthompsa	}
2248172567Sthompsa
2249178354Ssam	data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2250172567Sthompsa	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2251172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2252172567Sthompsa	if (error != 0)
2253178354Ssam		goto done;
2254172567Sthompsa
2255172567Sthompsa	error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2256172567Sthompsa	if (error != 0)
2257178354Ssam		goto done;
2258172567Sthompsa
2259172567Sthompsa	error = ipw_setbssid(sc, ni->ni_bssid);
2260172567Sthompsa	if (error != 0)
2261178354Ssam		goto done;
2262172567Sthompsa
2263178354Ssam	if (vap->iv_appie_assocreq != NULL) {
2264178354Ssam		struct ieee80211_appie *ie = vap->iv_appie_assocreq;
2265178354Ssam		error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2266172567Sthompsa		if (error != 0)
2267178354Ssam			goto done;
2268172567Sthompsa	}
2269172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2270172567Sthompsa		error = ipw_setchannel(sc, ni->ni_chan);
2271172567Sthompsa		if (error != 0)
2272178354Ssam			goto done;
2273172567Sthompsa	}
2274172567Sthompsa
2275172567Sthompsa	/* lock scan to ap's channel and enable associate */
2276172567Sthompsa	error = ipw_setscanopts(sc,
2277178354Ssam	    1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2278178354Ssam	if (error != 0)
2279178354Ssam		goto done;
2280172567Sthompsa
2281178354Ssam	error = ipw_enable(sc);		/* finally, enable adapter */
2282178354Ssam	if (error == 0)
2283178354Ssam		sc->flags |= IPW_FLAG_ASSOCIATING;
2284178354Ssamdone:
2285178354Ssam	IPW_UNLOCK(sc);
2286172567Sthompsa}
2287172567Sthompsa
2288172567Sthompsastatic void
2289191746Sthompsaipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2290172567Sthompsa{
2291178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2292178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2293178354Ssam	struct ipw_softc *sc = ifp->if_softc;
2294172567Sthompsa	IPW_LOCK_DECL;
2295172567Sthompsa
2296172567Sthompsa	IPW_LOCK(sc);
2297172567Sthompsa	DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2298172567Sthompsa	/*
2299172567Sthompsa	 * NB: don't try to do this if ipw_stop_master has
2300172567Sthompsa	 *     shutdown the firmware and disabled interrupts.
2301172567Sthompsa	 */
2302178354Ssam	if (sc->flags & IPW_FLAG_FW_INITED) {
2303178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATED;
2304178354Ssam		/*
2305178354Ssam		 * NB: firmware currently ignores bssid parameter, but
2306178354Ssam		 *     supply it in case this changes (follow linux driver).
2307178354Ssam		 */
2308178354Ssam		(void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2309178354Ssam			ni->ni_bssid, IEEE80211_ADDR_LEN);
2310178354Ssam	}
2311178354Ssam	IPW_UNLOCK(sc);
2312172567Sthompsa}
2313172567Sthompsa
2314172567Sthompsa/*
2315156599Sdamien * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2316156599Sdamien * It is called on firmware panics or on watchdog timeouts.
2317156599Sdamien */
2318145247Sdamienstatic void
2319156599Sdamienipw_init_task(void *context, int pending)
2320156599Sdamien{
2321156599Sdamien	ipw_init(context);
2322156599Sdamien}
2323156599Sdamien
2324156599Sdamienstatic void
2325145247Sdamienipw_init(void *priv)
2326145247Sdamien{
2327145247Sdamien	struct ipw_softc *sc = priv;
2328178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2329178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2330172567Sthompsa	IPW_LOCK_DECL;
2331172567Sthompsa
2332172567Sthompsa	IPW_LOCK(sc);
2333178354Ssam	ipw_init_locked(sc);
2334172567Sthompsa	IPW_UNLOCK(sc);
2335178354Ssam
2336178931Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2337178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2338172567Sthompsa}
2339172567Sthompsa
2340172567Sthompsastatic void
2341178354Ssamipw_init_locked(struct ipw_softc *sc)
2342172567Sthompsa{
2343178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2344178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2345178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2346166756Sluigi	const struct firmware *fp;
2347156599Sdamien	const struct ipw_firmware_hdr *hdr;
2348178354Ssam	const char *fw;
2349145247Sdamien
2350172567Sthompsa	IPW_LOCK_ASSERT(sc);
2351156599Sdamien
2352172567Sthompsa	DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2353178354Ssam		ieee80211_state_name[vap->iv_state], sc->flags));
2354172567Sthompsa
2355156599Sdamien	/*
2356156599Sdamien	 * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2357156599Sdamien	 * when loading the firmware and we don't want to be called during this
2358156599Sdamien	 * operation.
2359156599Sdamien	 */
2360172567Sthompsa	if (sc->flags & IPW_FLAG_INIT_LOCKED)
2361145247Sdamien		return;
2362156599Sdamien	sc->flags |= IPW_FLAG_INIT_LOCKED;
2363145247Sdamien
2364172567Sthompsa	ipw_stop_locked(sc);
2365145247Sdamien
2366145247Sdamien	if (ipw_reset(sc) != 0) {
2367145247Sdamien		device_printf(sc->sc_dev, "could not reset adapter\n");
2368178354Ssam		goto fail;
2369145247Sdamien	}
2370145247Sdamien
2371159487Siedowse	if (sc->sc_firmware == NULL) {
2372178354Ssam		device_printf(sc->sc_dev, "no firmware\n");
2373178354Ssam		goto fail;
2374156599Sdamien	}
2375178354Ssam	/* NB: consistency already checked on load */
2376159487Siedowse	fp = sc->sc_firmware;
2377156599Sdamien	hdr = (const struct ipw_firmware_hdr *)fp->data;
2378156599Sdamien
2379178354Ssam	DPRINTF(("Loading firmware image '%s'\n", fp->name));
2380156599Sdamien	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2381156599Sdamien	if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2382145247Sdamien		device_printf(sc->sc_dev, "could not load microcode\n");
2383178354Ssam		goto fail;
2384145247Sdamien	}
2385145247Sdamien
2386145247Sdamien	ipw_stop_master(sc);
2387145247Sdamien
2388145247Sdamien	/*
2389145247Sdamien	 * Setup tx, rx and status rings.
2390145247Sdamien	 */
2391145247Sdamien	sc->txold = IPW_NTBD - 1;
2392145247Sdamien	sc->txcur = 0;
2393145247Sdamien	sc->txfree = IPW_NTBD - 2;
2394145247Sdamien	sc->rxcur = IPW_NRBD - 1;
2395145247Sdamien
2396145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2397145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2398145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2399145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2400145247Sdamien
2401145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2402145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2403145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2404145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2405145247Sdamien
2406145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2407145247Sdamien
2408156599Sdamien	fw = (const char *)fp->data + sizeof *hdr;
2409156599Sdamien	if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2410145247Sdamien		device_printf(sc->sc_dev, "could not load firmware\n");
2411178354Ssam		goto fail;
2412145247Sdamien	}
2413145247Sdamien
2414145247Sdamien	sc->flags |= IPW_FLAG_FW_INITED;
2415145247Sdamien
2416145247Sdamien	/* retrieve information tables base addresses */
2417145247Sdamien	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2418145247Sdamien	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2419145247Sdamien
2420145247Sdamien	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2421145247Sdamien
2422145247Sdamien	if (ipw_config(sc) != 0) {
2423145247Sdamien		device_printf(sc->sc_dev, "device configuration failed\n");
2424178354Ssam		goto fail;
2425145247Sdamien	}
2426145247Sdamien
2427172567Sthompsa	callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2428148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2429148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2430145247Sdamien
2431156599Sdamien	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2432145247Sdamien	return;
2433145247Sdamien
2434178354Ssamfail:
2435172567Sthompsa	ipw_stop_locked(sc);
2436156599Sdamien	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2437145247Sdamien}
2438145247Sdamien
2439178354Ssamstatic int
2440178354Ssamipw_config(struct ipw_softc *sc)
2441178354Ssam{
2442178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2443178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2444178354Ssam	struct ipw_configuration config;
2445178354Ssam	uint32_t data;
2446178354Ssam	int error;
2447178354Ssam
2448178354Ssam	error = ipw_disable(sc);
2449178354Ssam	if (error != 0)
2450178354Ssam		return error;
2451178354Ssam
2452178354Ssam	switch (ic->ic_opmode) {
2453178354Ssam	case IEEE80211_M_STA:
2454178354Ssam	case IEEE80211_M_HOSTAP:
2455178354Ssam	case IEEE80211_M_WDS:		/* XXX */
2456178354Ssam		data = htole32(IPW_MODE_BSS);
2457178354Ssam		break;
2458178354Ssam	case IEEE80211_M_IBSS:
2459178354Ssam	case IEEE80211_M_AHDEMO:
2460178354Ssam		data = htole32(IPW_MODE_IBSS);
2461178354Ssam		break;
2462178354Ssam	case IEEE80211_M_MONITOR:
2463178354Ssam		data = htole32(IPW_MODE_MONITOR);
2464178354Ssam		break;
2465178354Ssam	}
2466178354Ssam	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2467178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2468178354Ssam	if (error != 0)
2469178354Ssam		return error;
2470178354Ssam
2471178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2472178354Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2473178354Ssam		error = ipw_setchannel(sc, ic->ic_curchan);
2474178354Ssam		if (error != 0)
2475178354Ssam			return error;
2476178354Ssam	}
2477178354Ssam
2478178354Ssam	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2479178354Ssam		return ipw_enable(sc);
2480178354Ssam
2481178354Ssam	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2482178354Ssam	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2483178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
2484178354Ssam		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2485178354Ssam	if (ifp->if_flags & IFF_PROMISC)
2486178354Ssam		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2487178354Ssam	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2488178354Ssam	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2489178354Ssam	DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2490178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2491178354Ssam	if (error != 0)
2492178354Ssam		return error;
2493178354Ssam
2494178354Ssam	data = htole32(0x3); /* 1, 2 */
2495178354Ssam	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2496178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2497178354Ssam	if (error != 0)
2498178354Ssam		return error;
2499178354Ssam
2500178354Ssam	/* NB: use the same rate set */
2501178354Ssam	DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2502178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2503178354Ssam	if (error != 0)
2504178354Ssam		return error;
2505178354Ssam
2506178354Ssam	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2507178354Ssam	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2508178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2509178354Ssam	if (error != 0)
2510178354Ssam		return error;
2511178354Ssam
2512178354Ssam	data = htole32(IPW_POWER_MODE_CAM);
2513178354Ssam	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2514178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2515178354Ssam	if (error != 0)
2516178354Ssam		return error;
2517178354Ssam
2518178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2519178354Ssam		data = htole32(32); /* default value */
2520178354Ssam		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2521178354Ssam		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2522178354Ssam		    sizeof data);
2523178354Ssam		if (error != 0)
2524178354Ssam			return error;
2525178354Ssam	}
2526178354Ssam
2527178354Ssam	return 0;
2528178354Ssam}
2529178354Ssam
2530145247Sdamienstatic void
2531145247Sdamienipw_stop(void *priv)
2532145247Sdamien{
2533145247Sdamien	struct ipw_softc *sc = priv;
2534172567Sthompsa	IPW_LOCK_DECL;
2535172567Sthompsa
2536172567Sthompsa	IPW_LOCK(sc);
2537172567Sthompsa	ipw_stop_locked(sc);
2538172567Sthompsa	IPW_UNLOCK(sc);
2539172567Sthompsa}
2540172567Sthompsa
2541172567Sthompsastatic void
2542172567Sthompsaipw_stop_locked(struct ipw_softc *sc)
2543172567Sthompsa{
2544178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2545145247Sdamien	int i;
2546145247Sdamien
2547172567Sthompsa	IPW_LOCK_ASSERT(sc);
2548156599Sdamien
2549172567Sthompsa	callout_stop(&sc->sc_wdtimer);
2550145247Sdamien	ipw_stop_master(sc);
2551145247Sdamien
2552145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2553145247Sdamien
2554145247Sdamien	/*
2555145247Sdamien	 * Release tx buffers.
2556145247Sdamien	 */
2557145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
2558145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
2559145247Sdamien
2560145247Sdamien	sc->sc_tx_timer = 0;
2561148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2562145247Sdamien}
2563145247Sdamien
2564145247Sdamienstatic int
2565145247Sdamienipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2566145247Sdamien{
2567145247Sdamien	struct ipw_softc *sc = arg1;
2568145247Sdamien	uint32_t i, size, buf[256];
2569145247Sdamien
2570174317Sphilip	memset(buf, 0, sizeof buf);
2571174317Sphilip
2572174317Sphilip	if (!(sc->flags & IPW_FLAG_FW_INITED))
2573145247Sdamien		return SYSCTL_OUT(req, buf, sizeof buf);
2574145247Sdamien
2575145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2576145247Sdamien
2577145247Sdamien	size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2578145247Sdamien	for (i = 1; i < size; i++)
2579145247Sdamien		buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2580145247Sdamien
2581174317Sphilip	return SYSCTL_OUT(req, buf, size);
2582145247Sdamien}
2583145247Sdamien
2584145247Sdamienstatic int
2585145247Sdamienipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2586145247Sdamien{
2587145247Sdamien	struct ipw_softc *sc = arg1;
2588145247Sdamien	int val;
2589145247Sdamien
2590145247Sdamien	val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2591145247Sdamien	        (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2592145247Sdamien
2593145247Sdamien	return SYSCTL_OUT(req, &val, sizeof val);
2594145247Sdamien}
2595145247Sdamien
2596145247Sdamienstatic uint32_t
2597145247Sdamienipw_read_table1(struct ipw_softc *sc, uint32_t off)
2598145247Sdamien{
2599145247Sdamien	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2600145247Sdamien}
2601145247Sdamien
2602145247Sdamienstatic void
2603145247Sdamienipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2604145247Sdamien{
2605145247Sdamien	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2606145247Sdamien}
2607145247Sdamien
2608172567Sthompsa#if 0
2609145247Sdamienstatic int
2610145247Sdamienipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2611145247Sdamien{
2612145247Sdamien	uint32_t addr, info;
2613145247Sdamien	uint16_t count, size;
2614145247Sdamien	uint32_t total;
2615145247Sdamien
2616145247Sdamien	/* addr[4] + count[2] + size[2] */
2617145247Sdamien	addr = MEM_READ_4(sc, sc->table2_base + off);
2618145247Sdamien	info = MEM_READ_4(sc, sc->table2_base + off + 4);
2619145247Sdamien
2620145247Sdamien	count = info >> 16;
2621145247Sdamien	size = info & 0xffff;
2622145247Sdamien	total = count * size;
2623145247Sdamien
2624145247Sdamien	if (total > *len) {
2625145247Sdamien		*len = total;
2626145247Sdamien		return EINVAL;
2627145247Sdamien	}
2628145247Sdamien
2629145247Sdamien	*len = total;
2630145247Sdamien	ipw_read_mem_1(sc, addr, buf, total);
2631145247Sdamien
2632145247Sdamien	return 0;
2633145247Sdamien}
2634145247Sdamien
2635145247Sdamienstatic void
2636145247Sdamienipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2637145247Sdamien    bus_size_t count)
2638145247Sdamien{
2639145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2640145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2641145247Sdamien		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2642145247Sdamien	}
2643145247Sdamien}
2644172567Sthompsa#endif
2645145247Sdamien
2646145247Sdamienstatic void
2647156599Sdamienipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2648145247Sdamien    bus_size_t count)
2649145247Sdamien{
2650145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2651145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2652145247Sdamien		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2653145247Sdamien	}
2654145247Sdamien}
2655172567Sthompsa
2656172567Sthompsastatic void
2657172567Sthompsaipw_scan_start(struct ieee80211com *ic)
2658172567Sthompsa{
2659172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2660172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2661172567Sthompsa	IPW_LOCK_DECL;
2662172567Sthompsa
2663172567Sthompsa	IPW_LOCK(sc);
2664191746Sthompsa	ipw_scan(sc);
2665172567Sthompsa	IPW_UNLOCK(sc);
2666172567Sthompsa}
2667172567Sthompsa
2668172567Sthompsastatic void
2669172567Sthompsaipw_set_channel(struct ieee80211com *ic)
2670172567Sthompsa{
2671172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2672172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2673172567Sthompsa	IPW_LOCK_DECL;
2674172567Sthompsa
2675172567Sthompsa	IPW_LOCK(sc);
2676172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2677172567Sthompsa		ipw_disable(sc);
2678172567Sthompsa		ipw_setchannel(sc, ic->ic_curchan);
2679172567Sthompsa		ipw_enable(sc);
2680172567Sthompsa	}
2681172567Sthompsa	IPW_UNLOCK(sc);
2682172567Sthompsa}
2683172567Sthompsa
2684172567Sthompsastatic void
2685178354Ssamipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2686172567Sthompsa{
2687172567Sthompsa	/* NB: all channels are scanned at once */
2688172567Sthompsa}
2689172567Sthompsa
2690172567Sthompsastatic void
2691178354Ssamipw_scan_mindwell(struct ieee80211_scan_state *ss)
2692172567Sthompsa{
2693172567Sthompsa	/* NB: don't try to abort scan; wait for firmware to finish */
2694172567Sthompsa}
2695172567Sthompsa
2696172567Sthompsastatic void
2697172567Sthompsaipw_scan_end(struct ieee80211com *ic)
2698172567Sthompsa{
2699172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2700172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2701172567Sthompsa	IPW_LOCK_DECL;
2702172567Sthompsa
2703172567Sthompsa	IPW_LOCK(sc);
2704172567Sthompsa	sc->flags &= ~IPW_FLAG_SCANNING;
2705172567Sthompsa	IPW_UNLOCK(sc);
2706172567Sthompsa}
2707