if_ipw.c revision 206766
1145247Sdamien/*	$FreeBSD: head/sys/dev/ipw/if_ipw.c 206766 2010-04-17 18:17:25Z bschmidt $	*/
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 206766 2010-04-17 18:17:25Z bschmidt $");
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.
891206763Sbschmidt			 * Use that to call ipw_assoc.  On completing the
892206763Sbschmidt			 * join we are then called again with an AUTH -> RUN
893206763Sbschmidt			 * transition and we want to do nothing.  This is
894206763Sbschmidt			 * 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:
907206765Sbschmidt		/*
908206765Sbschmidt		 * Move to ASSOC state after the ipw_assoc() call.  Firmware
909206765Sbschmidt		 * takes care of authentication, after the call we'll receive
910206765Sbschmidt		 * only an assoc response which would otherwise be discared
911206765Sbschmidt		 * if we are still in AUTH state.
912206765Sbschmidt		 */
913206765Sbschmidt		nstate = IEEE80211_S_ASSOC;
914191746Sthompsa		ipw_assoc(ic, vap);
915191746Sthompsa		break;
916145247Sdamien
917172567Sthompsa	case IEEE80211_S_ASSOC:
918172567Sthompsa		/*
919206763Sbschmidt		 * If we are not transitioning from AUTH then resend the
920172567Sthompsa		 * association request.
921172567Sthompsa		 */
922191746Sthompsa		if (ostate != IEEE80211_S_AUTH)
923191746Sthompsa			ipw_assoc(ic, vap);
924145247Sdamien		break;
925145247Sdamien
926172058Ssam	default:
927145247Sdamien		break;
928145247Sdamien	}
929191746Sthompsa	IEEE80211_LOCK(ic);
930178354Ssam	return ivp->newstate(vap, nstate, arg);
931145247Sdamien}
932145247Sdamien
933145247Sdamien/*
934145247Sdamien * Read 16 bits at address 'addr' from the serial EEPROM.
935145247Sdamien */
936145247Sdamienstatic uint16_t
937145247Sdamienipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
938145247Sdamien{
939145247Sdamien	uint32_t tmp;
940145247Sdamien	uint16_t val;
941145247Sdamien	int n;
942145247Sdamien
943145247Sdamien	/* clock C once before the first command */
944145247Sdamien	IPW_EEPROM_CTL(sc, 0);
945145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
946145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
947145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
948145247Sdamien
949145247Sdamien	/* write start bit (1) */
950145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
951145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
952145247Sdamien
953145247Sdamien	/* write READ opcode (10) */
954145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
955145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
956145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
957145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
958145247Sdamien
959145247Sdamien	/* write address A7-A0 */
960145247Sdamien	for (n = 7; n >= 0; n--) {
961145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
962145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
963145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
964145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
965145247Sdamien	}
966145247Sdamien
967145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
968145247Sdamien
969145247Sdamien	/* read data Q15-Q0 */
970145247Sdamien	val = 0;
971145247Sdamien	for (n = 15; n >= 0; n--) {
972145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
973145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
974145247Sdamien		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
975145247Sdamien		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
976145247Sdamien	}
977145247Sdamien
978145247Sdamien	IPW_EEPROM_CTL(sc, 0);
979145247Sdamien
980145247Sdamien	/* clear Chip Select and clock C */
981145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
982145247Sdamien	IPW_EEPROM_CTL(sc, 0);
983145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
984145247Sdamien
985145247Sdamien	return le16toh(val);
986145247Sdamien}
987145247Sdamien
988145247Sdamienstatic void
989172567Sthompsaipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
990145247Sdamien{
991145247Sdamien	struct ipw_cmd *cmd;
992145247Sdamien
993145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
994145247Sdamien
995145247Sdamien	cmd = mtod(sbuf->m, struct ipw_cmd *);
996145247Sdamien
997172567Sthompsa	DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
998172567Sthompsa	    ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
999145247Sdamien	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
1000145247Sdamien	    le32toh(cmd->status)));
1001145247Sdamien
1002172567Sthompsa	sc->flags &= ~IPW_FLAG_BUSY;
1003145247Sdamien	wakeup(sc);
1004145247Sdamien}
1005145247Sdamien
1006145247Sdamienstatic void
1007172567Sthompsaipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1008145247Sdamien{
1009178354Ssam#define	IEEESTATE(vap)	ieee80211_state_name[vap->iv_state]
1010178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1011178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1012178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1013145247Sdamien	uint32_t state;
1014145247Sdamien
1015145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1016145247Sdamien
1017145247Sdamien	state = le32toh(*mtod(sbuf->m, uint32_t *));
1018145247Sdamien
1019145247Sdamien	switch (state) {
1020145247Sdamien	case IPW_STATE_ASSOCIATED:
1021172567Sthompsa		DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1022178354Ssam			IEEESTATE(vap), sc->flags));
1023178354Ssam		/* XXX suppress state change in case the fw auto-associates */
1024178354Ssam		if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1025178354Ssam			DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1026178354Ssam				IEEESTATE(vap), sc->flags));
1027178354Ssam			break;
1028178354Ssam		}
1029178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATING;
1030172567Sthompsa		sc->flags |= IPW_FLAG_ASSOCIATED;
1031145247Sdamien		break;
1032145247Sdamien
1033145247Sdamien	case IPW_STATE_SCANNING:
1034172567Sthompsa		DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1035178354Ssam			IEEESTATE(vap), sc->flags));
1036172567Sthompsa		/*
1037172567Sthompsa		 * NB: Check driver state for association on assoc
1038172567Sthompsa		 * loss as the firmware will immediately start to
1039172567Sthompsa		 * scan and we would treat it as a beacon miss if
1040172567Sthompsa		 * we checked the 802.11 layer state.
1041172567Sthompsa		 */
1042178354Ssam		if (sc->flags & IPW_FLAG_ASSOCIATED) {
1043178354Ssam			/* XXX probably need to issue disassoc to fw */
1044191746Sthompsa			ieee80211_beacon_miss(ic);
1045178354Ssam		}
1046145247Sdamien		break;
1047145247Sdamien
1048145247Sdamien	case IPW_STATE_SCAN_COMPLETE:
1049172567Sthompsa		/*
1050172567Sthompsa		 * XXX For some reason scan requests generate scan
1051172567Sthompsa		 * started + scan done events before any traffic is
1052172567Sthompsa		 * received (e.g. probe response frames).  We work
1053172567Sthompsa		 * around this by marking the HACK flag and skipping
1054172567Sthompsa		 * the first scan complete event.
1055172567Sthompsa		*/
1056178354Ssam		DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1057178354Ssam			    IEEESTATE(vap), sc->flags));
1058172567Sthompsa		if (sc->flags & IPW_FLAG_HACK) {
1059172567Sthompsa			sc->flags &= ~IPW_FLAG_HACK;
1060172567Sthompsa			break;
1061172567Sthompsa		}
1062172567Sthompsa		if (sc->flags & IPW_FLAG_SCANNING) {
1063191746Sthompsa			ieee80211_scan_done(vap);
1064172567Sthompsa			sc->flags &= ~IPW_FLAG_SCANNING;
1065172567Sthompsa			sc->sc_scan_timer = 0;
1066172567Sthompsa		}
1067145247Sdamien		break;
1068145247Sdamien
1069145247Sdamien	case IPW_STATE_ASSOCIATION_LOST:
1070172567Sthompsa		DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1071178354Ssam			IEEESTATE(vap), sc->flags));
1072178354Ssam		sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1073178354Ssam		if (vap->iv_state == IEEE80211_S_RUN)
1074191746Sthompsa			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1075145247Sdamien		break;
1076145247Sdamien
1077172567Sthompsa	case IPW_STATE_DISABLED:
1078178354Ssam		/* XXX? is this right? */
1079206763Sbschmidt		sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1080178354Ssam		    IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1081172567Sthompsa		DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1082178354Ssam			IEEESTATE(vap), sc->flags));
1083172567Sthompsa		break;
1084172567Sthompsa
1085145247Sdamien	case IPW_STATE_RADIO_DISABLED:
1086178354Ssam		device_printf(sc->sc_dev, "radio turned off\n");
1087178354Ssam		ieee80211_notify_radio(ic, 0);
1088172567Sthompsa		ipw_stop_locked(sc);
1089178354Ssam		/* XXX start polling thread to detect radio on */
1090145247Sdamien		break;
1091172567Sthompsa
1092172567Sthompsa	default:
1093172567Sthompsa		DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1094178354Ssam			__func__, state, IEEESTATE(vap), sc->flags));
1095172567Sthompsa		break;
1096145247Sdamien	}
1097172567Sthompsa#undef IEEESTATE
1098145247Sdamien}
1099145247Sdamien
1100145247Sdamien/*
1101172567Sthompsa * Set driver state for current channel.
1102172567Sthompsa */
1103172567Sthompsastatic void
1104172567Sthompsaipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1105172567Sthompsa{
1106178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1107178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1108172567Sthompsa
1109172567Sthompsa	ic->ic_curchan = chan;
1110192468Ssam	ieee80211_radiotap_chan_change(ic);
1111172567Sthompsa}
1112172567Sthompsa
1113172567Sthompsa/*
1114145247Sdamien * XXX: Hack to set the current channel to the value advertised in beacons or
1115145247Sdamien * probe responses. Only used during AP detection.
1116145247Sdamien */
1117145247Sdamienstatic void
1118172567Sthompsaipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1119145247Sdamien{
1120178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1121178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1122172567Sthompsa	struct ieee80211_channel *c;
1123145247Sdamien	struct ieee80211_frame *wh;
1124145247Sdamien	uint8_t subtype;
1125145247Sdamien	uint8_t *frm, *efrm;
1126145247Sdamien
1127145247Sdamien	wh = mtod(m, struct ieee80211_frame *);
1128145247Sdamien
1129145247Sdamien	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1130145247Sdamien		return;
1131145247Sdamien
1132145247Sdamien	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1133145247Sdamien
1134145247Sdamien	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1135145247Sdamien	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1136145247Sdamien		return;
1137145247Sdamien
1138178354Ssam	/* XXX use ieee80211_parse_beacon */
1139145247Sdamien	frm = (uint8_t *)(wh + 1);
1140145247Sdamien	efrm = mtod(m, uint8_t *) + m->m_len;
1141145247Sdamien
1142145247Sdamien	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1143145247Sdamien	while (frm < efrm) {
1144145247Sdamien		if (*frm == IEEE80211_ELEMID_DSPARMS)
1145145247Sdamien#if IEEE80211_CHAN_MAX < 255
1146145247Sdamien		if (frm[2] <= IEEE80211_CHAN_MAX)
1147145247Sdamien#endif
1148172567Sthompsa		{
1149172567Sthompsa			DPRINTF(("Fixing channel to %d\n", frm[2]));
1150172567Sthompsa			c = ieee80211_find_channel(ic,
1151170530Ssam				ieee80211_ieee2mhz(frm[2], 0),
1152172567Sthompsa				IEEE80211_CHAN_B);
1153172567Sthompsa			if (c == NULL)
1154172567Sthompsa				c = &ic->ic_channels[0];
1155172567Sthompsa			ipw_setcurchan(sc, c);
1156172567Sthompsa		}
1157145247Sdamien
1158145247Sdamien		frm += frm[1] + 2;
1159145247Sdamien	}
1160145247Sdamien}
1161145247Sdamien
1162145247Sdamienstatic void
1163172567Sthompsaipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1164145247Sdamien    struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1165145247Sdamien{
1166178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1167178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1168152385Sdamien	struct mbuf *mnew, *m;
1169145247Sdamien	struct ieee80211_node *ni;
1170145247Sdamien	bus_addr_t physaddr;
1171145247Sdamien	int error;
1172192468Ssam	int8_t rssi, nf;
1173172567Sthompsa	IPW_LOCK_DECL;
1174145247Sdamien
1175152385Sdamien	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1176152385Sdamien	    status->rssi));
1177152385Sdamien
1178147757Sdamien	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1179147757Sdamien	    le32toh(status->len) > MCLBYTES)
1180147757Sdamien		return;
1181147757Sdamien
1182152385Sdamien	/*
1183152385Sdamien	 * Try to allocate a new mbuf for this ring element and load it before
1184152385Sdamien	 * processing the current mbuf. If the ring element cannot be loaded,
1185152385Sdamien	 * drop the received packet and reuse the old mbuf. In the unlikely
1186152385Sdamien	 * case that the old mbuf can't be reloaded either, explicitly panic.
1187152385Sdamien	 */
1188152385Sdamien	mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1189152385Sdamien	if (mnew == NULL) {
1190152385Sdamien		ifp->if_ierrors++;
1191152385Sdamien		return;
1192152385Sdamien	}
1193152385Sdamien
1194145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1195145247Sdamien	bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1196145247Sdamien
1197152385Sdamien	error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1198152385Sdamien	    MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1199152385Sdamien	if (error != 0) {
1200152385Sdamien		m_freem(mnew);
1201152385Sdamien
1202152385Sdamien		/* try to reload the old mbuf */
1203152385Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1204152385Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1205152385Sdamien		    &physaddr, 0);
1206152385Sdamien		if (error != 0) {
1207152385Sdamien			/* very unlikely that it will fail... */
1208152385Sdamien			panic("%s: could not load old rx mbuf",
1209152385Sdamien			    device_get_name(sc->sc_dev));
1210152385Sdamien		}
1211152385Sdamien		ifp->if_ierrors++;
1212152385Sdamien		return;
1213152385Sdamien	}
1214152385Sdamien
1215152385Sdamien	/*
1216152385Sdamien	 * New mbuf successfully loaded, update Rx ring and continue
1217152385Sdamien	 * processing.
1218152385Sdamien	 */
1219152385Sdamien	m = sbuf->m;
1220152385Sdamien	sbuf->m = mnew;
1221152385Sdamien	sbd->bd->physaddr = htole32(physaddr);
1222152385Sdamien
1223145247Sdamien	/* finalize mbuf */
1224145247Sdamien	m->m_pkthdr.rcvif = ifp;
1225145247Sdamien	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1226145247Sdamien
1227192468Ssam	rssi = status->rssi + IPW_RSSI_TO_DBM;
1228192468Ssam	nf = -95;
1229192468Ssam	if (ieee80211_radiotap_active(ic)) {
1230145247Sdamien		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1231145247Sdamien
1232145247Sdamien		tap->wr_flags = 0;
1233192468Ssam		tap->wr_antsignal = rssi;
1234192468Ssam		tap->wr_antnoise = nf;
1235145247Sdamien	}
1236145247Sdamien
1237172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
1238172567Sthompsa		ipw_fix_channel(sc, m);
1239145247Sdamien
1240172567Sthompsa	IPW_UNLOCK(sc);
1241178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1242178354Ssam	if (ni != NULL) {
1243206764Sbschmidt		(void) ieee80211_input(ni, m, rssi - nf, nf);
1244178354Ssam		ieee80211_free_node(ni);
1245178354Ssam	} else
1246206764Sbschmidt		(void) ieee80211_input_all(ic, m, rssi - nf, nf);
1247172567Sthompsa	IPW_LOCK(sc);
1248145247Sdamien
1249145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1250145247Sdamien}
1251145247Sdamien
1252145247Sdamienstatic void
1253145247Sdamienipw_rx_intr(struct ipw_softc *sc)
1254145247Sdamien{
1255145247Sdamien	struct ipw_status *status;
1256145247Sdamien	struct ipw_soft_bd *sbd;
1257145247Sdamien	struct ipw_soft_buf *sbuf;
1258145247Sdamien	uint32_t r, i;
1259145247Sdamien
1260145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1261145247Sdamien		return;
1262145247Sdamien
1263145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1264145247Sdamien
1265145247Sdamien	bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1266145247Sdamien
1267145247Sdamien	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1268145247Sdamien		status = &sc->status_list[i];
1269145247Sdamien		sbd = &sc->srbd_list[i];
1270145247Sdamien		sbuf = sbd->priv;
1271145247Sdamien
1272145247Sdamien		switch (le16toh(status->code) & 0xf) {
1273145247Sdamien		case IPW_STATUS_CODE_COMMAND:
1274172567Sthompsa			ipw_rx_cmd_intr(sc, sbuf);
1275145247Sdamien			break;
1276145247Sdamien
1277145247Sdamien		case IPW_STATUS_CODE_NEWSTATE:
1278172567Sthompsa			ipw_rx_newstate_intr(sc, sbuf);
1279145247Sdamien			break;
1280145247Sdamien
1281145247Sdamien		case IPW_STATUS_CODE_DATA_802_3:
1282145247Sdamien		case IPW_STATUS_CODE_DATA_802_11:
1283172567Sthompsa			ipw_rx_data_intr(sc, status, sbd, sbuf);
1284145247Sdamien			break;
1285145247Sdamien
1286145247Sdamien		case IPW_STATUS_CODE_NOTIFICATION:
1287172567Sthompsa			DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1288172567Sthompsa			    le32toh(status->len), status->flags));
1289178354Ssam			/* XXX maybe drive state machine AUTH->ASSOC? */
1290145247Sdamien			break;
1291145247Sdamien
1292145247Sdamien		default:
1293172567Sthompsa			device_printf(sc->sc_dev, "unexpected status code %u\n",
1294145247Sdamien			    le16toh(status->code));
1295145247Sdamien		}
1296145247Sdamien
1297145247Sdamien		/* firmware was killed, stop processing received frames */
1298145247Sdamien		if (!(sc->flags & IPW_FLAG_FW_INITED))
1299145247Sdamien			return;
1300145247Sdamien
1301145247Sdamien		sbd->bd->flags = 0;
1302145247Sdamien	}
1303145247Sdamien
1304145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1305145247Sdamien
1306145247Sdamien	/* kick the firmware */
1307145247Sdamien	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1308145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1309145247Sdamien}
1310145247Sdamien
1311145247Sdamienstatic void
1312145247Sdamienipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1313145247Sdamien{
1314145247Sdamien	struct ipw_soft_hdr *shdr;
1315145247Sdamien	struct ipw_soft_buf *sbuf;
1316145247Sdamien
1317145247Sdamien	switch (sbd->type) {
1318145247Sdamien	case IPW_SBD_TYPE_COMMAND:
1319145247Sdamien		bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1320145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1321145247Sdamien		bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1322145247Sdamien		break;
1323145247Sdamien
1324145247Sdamien	case IPW_SBD_TYPE_HEADER:
1325145247Sdamien		shdr = sbd->priv;
1326145247Sdamien		bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1327145247Sdamien		bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1328145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1329145247Sdamien		break;
1330145247Sdamien
1331145247Sdamien	case IPW_SBD_TYPE_DATA:
1332145247Sdamien		sbuf = sbd->priv;
1333145247Sdamien		bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1334145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1335145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1336145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1337145247Sdamien
1338170530Ssam		if (sbuf->m->m_flags & M_TXCB)
1339170530Ssam			ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1340145247Sdamien		m_freem(sbuf->m);
1341145247Sdamien		ieee80211_free_node(sbuf->ni);
1342145247Sdamien
1343145247Sdamien		sc->sc_tx_timer = 0;
1344145247Sdamien		break;
1345145247Sdamien	}
1346145247Sdamien
1347145247Sdamien	sbd->type = IPW_SBD_TYPE_NOASSOC;
1348145247Sdamien}
1349145247Sdamien
1350145247Sdamienstatic void
1351145247Sdamienipw_tx_intr(struct ipw_softc *sc)
1352145247Sdamien{
1353178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1354145247Sdamien	struct ipw_soft_bd *sbd;
1355145247Sdamien	uint32_t r, i;
1356145247Sdamien
1357145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1358145247Sdamien		return;
1359145247Sdamien
1360145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1361145247Sdamien
1362145247Sdamien	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1363145247Sdamien		sbd = &sc->stbd_list[i];
1364145247Sdamien
1365145247Sdamien		if (sbd->type == IPW_SBD_TYPE_DATA)
1366145247Sdamien			ifp->if_opackets++;
1367145247Sdamien
1368145247Sdamien		ipw_release_sbd(sc, sbd);
1369145247Sdamien		sc->txfree++;
1370145247Sdamien	}
1371145247Sdamien
1372145247Sdamien	/* remember what the firmware has processed */
1373145247Sdamien	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1374145247Sdamien
1375148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1376172567Sthompsa	ipw_start_locked(ifp);
1377145247Sdamien}
1378145247Sdamien
1379145247Sdamienstatic void
1380191746Sthompsaipw_fatal_error_intr(struct ipw_softc *sc)
1381191746Sthompsa{
1382191746Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1383191746Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1384191956Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1385191746Sthompsa
1386191746Sthompsa	device_printf(sc->sc_dev, "firmware error\n");
1387191956Sthompsa	if (vap != NULL)
1388191956Sthompsa		ieee80211_cancel_scan(vap);
1389191746Sthompsa	ieee80211_runtask(ic, &sc->sc_init_task);
1390191746Sthompsa}
1391191746Sthompsa
1392191746Sthompsastatic void
1393145247Sdamienipw_intr(void *arg)
1394145247Sdamien{
1395145247Sdamien	struct ipw_softc *sc = arg;
1396145247Sdamien	uint32_t r;
1397172567Sthompsa	IPW_LOCK_DECL;
1398145247Sdamien
1399172567Sthompsa	IPW_LOCK(sc);
1400145247Sdamien
1401191746Sthompsa	r = CSR_READ_4(sc, IPW_CSR_INTR);
1402191746Sthompsa	if (r == 0 || r == 0xffffffff)
1403191746Sthompsa		goto done;
1404145247Sdamien
1405145247Sdamien	/* disable interrupts */
1406145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1407145247Sdamien
1408156599Sdamien	/* acknowledge all interrupts */
1409156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1410156599Sdamien
1411145247Sdamien	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1412191746Sthompsa		ipw_fatal_error_intr(sc);
1413191746Sthompsa		goto done;
1414145247Sdamien	}
1415145247Sdamien
1416156599Sdamien	if (r & IPW_INTR_FW_INIT_DONE)
1417156599Sdamien		wakeup(sc);
1418145247Sdamien
1419145247Sdamien	if (r & IPW_INTR_RX_TRANSFER)
1420145247Sdamien		ipw_rx_intr(sc);
1421145247Sdamien
1422145247Sdamien	if (r & IPW_INTR_TX_TRANSFER)
1423145247Sdamien		ipw_tx_intr(sc);
1424145247Sdamien
1425145247Sdamien	/* re-enable interrupts */
1426145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1427191746Sthompsadone:
1428172567Sthompsa	IPW_UNLOCK(sc);
1429145247Sdamien}
1430145247Sdamien
1431145247Sdamienstatic void
1432145247Sdamienipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1433145247Sdamien{
1434145247Sdamien	if (error != 0)
1435145247Sdamien		return;
1436145247Sdamien
1437145247Sdamien	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1438145247Sdamien
1439145247Sdamien	*(bus_addr_t *)arg = segs[0].ds_addr;
1440145247Sdamien}
1441145247Sdamien
1442172567Sthompsastatic const char *
1443172567Sthompsaipw_cmdname(int cmd)
1444172567Sthompsa{
1445172567Sthompsa#define	N(a)	(sizeof(a) / sizeof(a[0]))
1446172567Sthompsa	static const struct {
1447172567Sthompsa		int	cmd;
1448172567Sthompsa		const char *name;
1449172567Sthompsa	} cmds[] = {
1450172567Sthompsa		{ IPW_CMD_ADD_MULTICAST,	"ADD_MULTICAST" },
1451172567Sthompsa		{ IPW_CMD_BROADCAST_SCAN,	"BROADCAST_SCAN" },
1452172567Sthompsa		{ IPW_CMD_DISABLE,		"DISABLE" },
1453172567Sthompsa		{ IPW_CMD_DISABLE_PHY,		"DISABLE_PHY" },
1454172567Sthompsa		{ IPW_CMD_ENABLE,		"ENABLE" },
1455172567Sthompsa		{ IPW_CMD_PREPARE_POWER_DOWN,	"PREPARE_POWER_DOWN" },
1456172567Sthompsa		{ IPW_CMD_SET_BASIC_TX_RATES,	"SET_BASIC_TX_RATES" },
1457172567Sthompsa		{ IPW_CMD_SET_BEACON_INTERVAL,	"SET_BEACON_INTERVAL" },
1458172567Sthompsa		{ IPW_CMD_SET_CHANNEL,		"SET_CHANNEL" },
1459172567Sthompsa		{ IPW_CMD_SET_CONFIGURATION,	"SET_CONFIGURATION" },
1460172567Sthompsa		{ IPW_CMD_SET_DESIRED_BSSID,	"SET_DESIRED_BSSID" },
1461172567Sthompsa		{ IPW_CMD_SET_ESSID,		"SET_ESSID" },
1462172567Sthompsa		{ IPW_CMD_SET_FRAG_THRESHOLD,	"SET_FRAG_THRESHOLD" },
1463172567Sthompsa		{ IPW_CMD_SET_MAC_ADDRESS,	"SET_MAC_ADDRESS" },
1464172567Sthompsa		{ IPW_CMD_SET_MANDATORY_BSSID,	"SET_MANDATORY_BSSID" },
1465172567Sthompsa		{ IPW_CMD_SET_MODE,		"SET_MODE" },
1466172567Sthompsa		{ IPW_CMD_SET_MSDU_TX_RATES,	"SET_MSDU_TX_RATES" },
1467172567Sthompsa		{ IPW_CMD_SET_POWER_MODE,	"SET_POWER_MODE" },
1468172567Sthompsa		{ IPW_CMD_SET_RTS_THRESHOLD,	"SET_RTS_THRESHOLD" },
1469172567Sthompsa		{ IPW_CMD_SET_SCAN_OPTIONS,	"SET_SCAN_OPTIONS" },
1470172567Sthompsa		{ IPW_CMD_SET_SECURITY_INFO,	"SET_SECURITY_INFO" },
1471172567Sthompsa		{ IPW_CMD_SET_TX_POWER_INDEX,	"SET_TX_POWER_INDEX" },
1472172567Sthompsa		{ IPW_CMD_SET_TX_RATES,		"SET_TX_RATES" },
1473172567Sthompsa		{ IPW_CMD_SET_WEP_FLAGS,	"SET_WEP_FLAGS" },
1474172567Sthompsa		{ IPW_CMD_SET_WEP_KEY,		"SET_WEP_KEY" },
1475172567Sthompsa		{ IPW_CMD_SET_WEP_KEY_INDEX,	"SET_WEP_KEY_INDEX" },
1476172567Sthompsa		{ IPW_CMD_SET_WPA_IE,		"SET_WPA_IE" },
1477172567Sthompsa
1478172567Sthompsa	};
1479172567Sthompsa	static char buf[12];
1480172567Sthompsa	int i;
1481172567Sthompsa
1482172567Sthompsa	for (i = 0; i < N(cmds); i++)
1483172567Sthompsa		if (cmds[i].cmd == cmd)
1484172567Sthompsa			return cmds[i].name;
1485172567Sthompsa	snprintf(buf, sizeof(buf), "%u", cmd);
1486172567Sthompsa	return buf;
1487172567Sthompsa#undef N
1488172567Sthompsa}
1489172567Sthompsa
1490145247Sdamien/*
1491145247Sdamien * Send a command to the firmware and wait for the acknowledgement.
1492145247Sdamien */
1493145247Sdamienstatic int
1494145247Sdamienipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1495145247Sdamien{
1496145247Sdamien	struct ipw_soft_bd *sbd;
1497145247Sdamien	bus_addr_t physaddr;
1498145247Sdamien	int error;
1499145247Sdamien
1500178354Ssam	IPW_LOCK_ASSERT(sc);
1501178354Ssam
1502172567Sthompsa	if (sc->flags & IPW_FLAG_BUSY) {
1503172567Sthompsa		device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1504172567Sthompsa			__func__, ipw_cmdname(type));
1505172567Sthompsa		return EAGAIN;
1506172567Sthompsa	}
1507172567Sthompsa	sc->flags |= IPW_FLAG_BUSY;
1508172567Sthompsa
1509145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1510145247Sdamien
1511145247Sdamien	error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1512145247Sdamien	    sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1513145247Sdamien	if (error != 0) {
1514145247Sdamien		device_printf(sc->sc_dev, "could not map command DMA memory\n");
1515172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1516145247Sdamien		return error;
1517145247Sdamien	}
1518145247Sdamien
1519145247Sdamien	sc->cmd.type = htole32(type);
1520145247Sdamien	sc->cmd.subtype = 0;
1521145247Sdamien	sc->cmd.len = htole32(len);
1522145247Sdamien	sc->cmd.seq = 0;
1523152637Sdamien	memcpy(sc->cmd.data, data, len);
1524145247Sdamien
1525145247Sdamien	sbd->type = IPW_SBD_TYPE_COMMAND;
1526145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1527145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1528145247Sdamien	sbd->bd->nfrag = 1;
1529145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1530145247Sdamien	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1531145247Sdamien
1532145247Sdamien	bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1533145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1534145247Sdamien
1535172567Sthompsa#ifdef IPW_DEBUG
1536172567Sthompsa	if (ipw_debug >= 4) {
1537172567Sthompsa		printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1538172567Sthompsa		    0, 0, len);
1539172567Sthompsa		/* Print the data buffer in the higher debug level */
1540172567Sthompsa		if (ipw_debug >= 9 && len > 0) {
1541172567Sthompsa			printf(" data: 0x");
1542172567Sthompsa			for (int i = 1; i <= len; i++)
1543174269Swkoszek				printf("%1D", (u_char *)data + len - i, "");
1544172567Sthompsa		}
1545172567Sthompsa		printf("\n");
1546172567Sthompsa	}
1547172567Sthompsa#endif
1548145247Sdamien
1549145247Sdamien	/* kick firmware */
1550145247Sdamien	sc->txfree--;
1551145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1552145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1553145247Sdamien
1554145247Sdamien	/* wait at most one second for command to complete */
1555172567Sthompsa	error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1556172567Sthompsa	if (error != 0) {
1557172567Sthompsa		device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1558172567Sthompsa		    __func__, ipw_cmdname(type), error);
1559172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1560172567Sthompsa		return (error);
1561172567Sthompsa	}
1562172567Sthompsa	return (0);
1563145247Sdamien}
1564145247Sdamien
1565145247Sdamienstatic int
1566145247Sdamienipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1567145247Sdamien{
1568145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1569178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1570192468Ssam	struct ieee80211vap *vap = ni->ni_vap;
1571145247Sdamien	struct ieee80211_frame *wh;
1572145247Sdamien	struct ipw_soft_bd *sbd;
1573145247Sdamien	struct ipw_soft_hdr *shdr;
1574145247Sdamien	struct ipw_soft_buf *sbuf;
1575145247Sdamien	struct ieee80211_key *k;
1576145247Sdamien	struct mbuf *mnew;
1577145247Sdamien	bus_dma_segment_t segs[IPW_MAX_NSEG];
1578145247Sdamien	bus_addr_t physaddr;
1579145247Sdamien	int nsegs, error, i;
1580145247Sdamien
1581145247Sdamien	wh = mtod(m0, struct ieee80211_frame *);
1582145247Sdamien
1583145247Sdamien	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1584178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1585147806Ssam		if (k == NULL) {
1586147806Ssam			m_freem(m0);
1587145247Sdamien			return ENOBUFS;
1588147806Ssam		}
1589145247Sdamien		/* packet header may have moved, reset our local pointer */
1590145247Sdamien		wh = mtod(m0, struct ieee80211_frame *);
1591145247Sdamien	}
1592145247Sdamien
1593192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1594145247Sdamien		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1595145247Sdamien
1596145247Sdamien		tap->wt_flags = 0;
1597145247Sdamien
1598192468Ssam		ieee80211_radiotap_tx(vap, m0);
1599145247Sdamien	}
1600145247Sdamien
1601145247Sdamien	shdr = SLIST_FIRST(&sc->free_shdr);
1602145247Sdamien	sbuf = SLIST_FIRST(&sc->free_sbuf);
1603145247Sdamien	KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1604145247Sdamien
1605145247Sdamien	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1606145247Sdamien	shdr->hdr.subtype = 0;
1607145247Sdamien	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1608145247Sdamien	shdr->hdr.encrypt = 0;
1609145247Sdamien	shdr->hdr.keyidx = 0;
1610145247Sdamien	shdr->hdr.keysz = 0;
1611145247Sdamien	shdr->hdr.fragmentsz = 0;
1612145247Sdamien	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1613145247Sdamien	if (ic->ic_opmode == IEEE80211_M_STA)
1614145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1615145247Sdamien	else
1616145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1617145247Sdamien
1618145247Sdamien	/* trim IEEE802.11 header */
1619145247Sdamien	m_adj(m0, sizeof (struct ieee80211_frame));
1620145247Sdamien
1621145247Sdamien	error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1622145247Sdamien	    &nsegs, 0);
1623145247Sdamien	if (error != 0 && error != EFBIG) {
1624145247Sdamien		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1625145247Sdamien		    error);
1626145247Sdamien		m_freem(m0);
1627145247Sdamien		return error;
1628145247Sdamien	}
1629145247Sdamien	if (error != 0) {
1630145247Sdamien		mnew = m_defrag(m0, M_DONTWAIT);
1631145247Sdamien		if (mnew == NULL) {
1632145247Sdamien			device_printf(sc->sc_dev,
1633145247Sdamien			    "could not defragment mbuf\n");
1634145247Sdamien			m_freem(m0);
1635145247Sdamien			return ENOBUFS;
1636145247Sdamien		}
1637145247Sdamien		m0 = mnew;
1638145247Sdamien
1639145247Sdamien		error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1640145247Sdamien		    segs, &nsegs, 0);
1641145247Sdamien		if (error != 0) {
1642145247Sdamien			device_printf(sc->sc_dev,
1643145247Sdamien			    "could not map mbuf (error %d)\n", error);
1644145247Sdamien			m_freem(m0);
1645145247Sdamien			return error;
1646145247Sdamien		}
1647145247Sdamien	}
1648145247Sdamien
1649145247Sdamien	error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1650145247Sdamien	    sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1651145247Sdamien	if (error != 0) {
1652145247Sdamien		device_printf(sc->sc_dev, "could not map header DMA memory\n");
1653145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1654145247Sdamien		m_freem(m0);
1655145247Sdamien		return error;
1656145247Sdamien	}
1657145247Sdamien
1658145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1659145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1660145247Sdamien
1661145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1662145247Sdamien	sbd->type = IPW_SBD_TYPE_HEADER;
1663145247Sdamien	sbd->priv = shdr;
1664145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1665145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1666145247Sdamien	sbd->bd->nfrag = 1 + nsegs;
1667145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1668145247Sdamien	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1669145247Sdamien
1670145247Sdamien	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1671145247Sdamien	    shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1672145247Sdamien	    shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1673145247Sdamien	    ":"));
1674145247Sdamien
1675145247Sdamien	sc->txfree--;
1676145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1677145247Sdamien
1678145247Sdamien	sbuf->m = m0;
1679145247Sdamien	sbuf->ni = ni;
1680145247Sdamien
1681145247Sdamien	for (i = 0; i < nsegs; i++) {
1682145247Sdamien		sbd = &sc->stbd_list[sc->txcur];
1683145247Sdamien
1684145247Sdamien		sbd->bd->physaddr = htole32(segs[i].ds_addr);
1685145247Sdamien		sbd->bd->len = htole32(segs[i].ds_len);
1686145247Sdamien		sbd->bd->nfrag = 0;
1687145247Sdamien		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1688145247Sdamien		if (i == nsegs - 1) {
1689145247Sdamien			sbd->type = IPW_SBD_TYPE_DATA;
1690145247Sdamien			sbd->priv = sbuf;
1691145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1692145247Sdamien		} else {
1693145247Sdamien			sbd->type = IPW_SBD_TYPE_NOASSOC;
1694145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1695145247Sdamien		}
1696145247Sdamien
1697172574Sthompsa		DPRINTFN(5, ("sending fragment (%d)\n", i));
1698145247Sdamien
1699145247Sdamien		sc->txfree--;
1700145247Sdamien		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1701145247Sdamien	}
1702145247Sdamien
1703145247Sdamien	bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1704145247Sdamien	bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1705145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1706145247Sdamien
1707145247Sdamien	/* kick firmware */
1708145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1709145247Sdamien
1710145247Sdamien	return 0;
1711145247Sdamien}
1712145247Sdamien
1713178354Ssamstatic int
1714178354Ssamipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1715178354Ssam	const struct ieee80211_bpf_params *params)
1716178354Ssam{
1717178354Ssam	/* no support; just discard */
1718178354Ssam	m_freem(m);
1719178354Ssam	ieee80211_free_node(ni);
1720178354Ssam	return 0;
1721178354Ssam}
1722178354Ssam
1723145247Sdamienstatic void
1724145247Sdamienipw_start(struct ifnet *ifp)
1725145247Sdamien{
1726145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1727172567Sthompsa	IPW_LOCK_DECL;
1728172567Sthompsa
1729172567Sthompsa	IPW_LOCK(sc);
1730172567Sthompsa	ipw_start_locked(ifp);
1731172567Sthompsa	IPW_UNLOCK(sc);
1732172567Sthompsa}
1733172567Sthompsa
1734172567Sthompsastatic void
1735172567Sthompsaipw_start_locked(struct ifnet *ifp)
1736172567Sthompsa{
1737172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
1738145247Sdamien	struct ieee80211_node *ni;
1739178354Ssam	struct mbuf *m;
1740145247Sdamien
1741172567Sthompsa	IPW_LOCK_ASSERT(sc);
1742145247Sdamien
1743145247Sdamien	for (;;) {
1744178354Ssam		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1745178354Ssam		if (m == NULL)
1746145247Sdamien			break;
1747145247Sdamien		if (sc->txfree < 1 + IPW_MAX_NSEG) {
1748178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1749148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1750145247Sdamien			break;
1751145247Sdamien		}
1752178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1753178354Ssam		if (ipw_tx_start(ifp, m, ni) != 0) {
1754145247Sdamien			ieee80211_free_node(ni);
1755145247Sdamien			ifp->if_oerrors++;
1756145247Sdamien			break;
1757145247Sdamien		}
1758145247Sdamien		/* start watchdog timer */
1759145247Sdamien		sc->sc_tx_timer = 5;
1760145247Sdamien	}
1761145247Sdamien}
1762145247Sdamien
1763145247Sdamienstatic void
1764172567Sthompsaipw_watchdog(void *arg)
1765145247Sdamien{
1766172567Sthompsa	struct ipw_softc *sc = arg;
1767172567Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1768178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1769145247Sdamien
1770172567Sthompsa	IPW_LOCK_ASSERT(sc);
1771156599Sdamien
1772145247Sdamien	if (sc->sc_tx_timer > 0) {
1773145247Sdamien		if (--sc->sc_tx_timer == 0) {
1774145247Sdamien			if_printf(ifp, "device timeout\n");
1775145247Sdamien			ifp->if_oerrors++;
1776178354Ssam			taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1777145247Sdamien		}
1778145247Sdamien	}
1779172567Sthompsa	if (sc->sc_scan_timer > 0) {
1780172567Sthompsa		if (--sc->sc_scan_timer == 0) {
1781172567Sthompsa			DPRINTFN(3, ("Scan timeout\n"));
1782172567Sthompsa			/* End the scan */
1783172567Sthompsa			if (sc->flags & IPW_FLAG_SCANNING) {
1784178354Ssam				ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1785172567Sthompsa				sc->flags &= ~IPW_FLAG_SCANNING;
1786172567Sthompsa			}
1787172567Sthompsa		}
1788172567Sthompsa	}
1789172567Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1790172567Sthompsa		callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1791145247Sdamien}
1792145247Sdamien
1793145247Sdamienstatic int
1794145247Sdamienipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1795145247Sdamien{
1796145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1797178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1798178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
1799178354Ssam	int error = 0, startall = 0;
1800172567Sthompsa	IPW_LOCK_DECL;
1801145247Sdamien
1802145247Sdamien	switch (cmd) {
1803145247Sdamien	case SIOCSIFFLAGS:
1804178704Sthompsa		IPW_LOCK(sc);
1805145247Sdamien		if (ifp->if_flags & IFF_UP) {
1806178354Ssam			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1807178354Ssam				ipw_init_locked(sc);
1808178354Ssam				startall = 1;
1809178354Ssam			}
1810145247Sdamien		} else {
1811148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1812172567Sthompsa				ipw_stop_locked(sc);
1813145247Sdamien		}
1814178704Sthompsa		IPW_UNLOCK(sc);
1815178704Sthompsa		if (startall)
1816178704Sthompsa			ieee80211_start_all(ic);
1817145247Sdamien		break;
1818178354Ssam	case SIOCGIFMEDIA:
1819178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1820178354Ssam		break;
1821178704Sthompsa	case SIOCGIFADDR:
1822178704Sthompsa		error = ether_ioctl(ifp, cmd, data);
1823178704Sthompsa		break;
1824145247Sdamien	default:
1825178704Sthompsa		error = EINVAL;
1826178704Sthompsa		break;
1827145247Sdamien	}
1828145247Sdamien	return error;
1829145247Sdamien}
1830145247Sdamien
1831145247Sdamienstatic void
1832145247Sdamienipw_stop_master(struct ipw_softc *sc)
1833145247Sdamien{
1834156599Sdamien	uint32_t tmp;
1835145247Sdamien	int ntries;
1836145247Sdamien
1837145247Sdamien	/* disable interrupts */
1838145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1839145247Sdamien
1840145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1841145247Sdamien	for (ntries = 0; ntries < 50; ntries++) {
1842145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1843145247Sdamien			break;
1844145247Sdamien		DELAY(10);
1845145247Sdamien	}
1846145247Sdamien	if (ntries == 50)
1847145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for master\n");
1848145247Sdamien
1849156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1850156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1851145247Sdamien
1852172567Sthompsa	/* Clear all flags except the following */
1853172567Sthompsa	sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1854145247Sdamien}
1855145247Sdamien
1856145247Sdamienstatic int
1857145247Sdamienipw_reset(struct ipw_softc *sc)
1858145247Sdamien{
1859156599Sdamien	uint32_t tmp;
1860145247Sdamien	int ntries;
1861145247Sdamien
1862145247Sdamien	ipw_stop_master(sc);
1863145247Sdamien
1864145247Sdamien	/* move adapter to D0 state */
1865156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1866156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1867145247Sdamien
1868145247Sdamien	/* wait for clock stabilization */
1869145247Sdamien	for (ntries = 0; ntries < 1000; ntries++) {
1870145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1871145247Sdamien			break;
1872145247Sdamien		DELAY(200);
1873145247Sdamien	}
1874145247Sdamien	if (ntries == 1000)
1875145247Sdamien		return EIO;
1876145247Sdamien
1877156599Sdamien	tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1878156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1879145247Sdamien
1880145247Sdamien	DELAY(10);
1881145247Sdamien
1882156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1883156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1884145247Sdamien
1885145247Sdamien	return 0;
1886145247Sdamien}
1887145247Sdamien
1888172567Sthompsastatic int
1889172567Sthompsaipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1890172567Sthompsa{
1891172567Sthompsa	int ms = hz < 1000 ? 1 : hz/10;
1892172567Sthompsa	int i, error;
1893172567Sthompsa
1894172567Sthompsa	for (i = 0; i < 100; i++) {
1895172567Sthompsa		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1896172567Sthompsa			return 0;
1897172567Sthompsa		error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1898172567Sthompsa		if (error == 0 || error != EWOULDBLOCK)
1899172567Sthompsa			return 0;
1900172567Sthompsa	}
1901172567Sthompsa	DPRINTF(("%s: timeout waiting for %s\n",
1902172567Sthompsa		__func__, waitfor ? "disable" : "enable"));
1903172567Sthompsa	return ETIMEDOUT;
1904172567Sthompsa}
1905172567Sthompsa
1906172567Sthompsastatic int
1907172567Sthompsaipw_enable(struct ipw_softc *sc)
1908172567Sthompsa{
1909172567Sthompsa	int error;
1910172567Sthompsa
1911172567Sthompsa	if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1912172567Sthompsa		DPRINTF(("Enable adapter\n"));
1913172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1914172567Sthompsa		if (error != 0)
1915172567Sthompsa			return error;
1916172567Sthompsa		error = ipw_waitfordisable(sc, 0);
1917172567Sthompsa		if (error != 0)
1918172567Sthompsa			return error;
1919172567Sthompsa		sc->flags |= IPW_FLAG_ENABLED;
1920172567Sthompsa	}
1921172567Sthompsa	return 0;
1922172567Sthompsa}
1923172567Sthompsa
1924172567Sthompsastatic int
1925172567Sthompsaipw_disable(struct ipw_softc *sc)
1926172567Sthompsa{
1927172567Sthompsa	int error;
1928172567Sthompsa
1929172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
1930172567Sthompsa		DPRINTF(("Disable adapter\n"));
1931172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1932172567Sthompsa		if (error != 0)
1933172567Sthompsa			return error;
1934172567Sthompsa		error = ipw_waitfordisable(sc, 1);
1935172567Sthompsa		if (error != 0)
1936172567Sthompsa			return error;
1937172567Sthompsa		sc->flags &= ~IPW_FLAG_ENABLED;
1938172567Sthompsa	}
1939172567Sthompsa	return 0;
1940172567Sthompsa}
1941172567Sthompsa
1942145247Sdamien/*
1943145247Sdamien * Upload the microcode to the device.
1944145247Sdamien */
1945145247Sdamienstatic int
1946156599Sdamienipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1947145247Sdamien{
1948145247Sdamien	int ntries;
1949145247Sdamien
1950145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1951145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1952145247Sdamien
1953145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1954145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1955145247Sdamien
1956145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1957145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1958145247Sdamien
1959145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1960145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1961145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1962145247Sdamien
1963145247Sdamien	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1964145247Sdamien
1965145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1966145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1967145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1968145247Sdamien
1969145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1970145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1971145247Sdamien
1972145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1973145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1974145247Sdamien
1975145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1976145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1977145247Sdamien
1978145247Sdamien	for (ntries = 0; ntries < 10; ntries++) {
1979145247Sdamien		if (MEM_READ_1(sc, 0x210000) & 1)
1980145247Sdamien			break;
1981145247Sdamien		DELAY(10);
1982145247Sdamien	}
1983145247Sdamien	if (ntries == 10) {
1984145247Sdamien		device_printf(sc->sc_dev,
1985145247Sdamien		    "timeout waiting for ucode to initialize\n");
1986145247Sdamien		return EIO;
1987145247Sdamien	}
1988145247Sdamien
1989145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0);
1990145247Sdamien
1991145247Sdamien	return 0;
1992145247Sdamien}
1993145247Sdamien
1994145247Sdamien/* set of macros to handle unaligned little endian data in firmware image */
1995145247Sdamien#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1996145247Sdamien#define GETLE16(p) ((p)[0] | (p)[1] << 8)
1997145247Sdamienstatic int
1998156599Sdamienipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
1999145247Sdamien{
2000156599Sdamien	const uint8_t *p, *end;
2001156599Sdamien	uint32_t tmp, dst;
2002145247Sdamien	uint16_t len;
2003145247Sdamien	int error;
2004145247Sdamien
2005145247Sdamien	p = fw;
2006145247Sdamien	end = fw + size;
2007145247Sdamien	while (p < end) {
2008145247Sdamien		dst = GETLE32(p); p += 4;
2009145247Sdamien		len = GETLE16(p); p += 2;
2010145247Sdamien
2011145247Sdamien		ipw_write_mem_1(sc, dst, p, len);
2012145247Sdamien		p += len;
2013145247Sdamien	}
2014145247Sdamien
2015145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
2016145247Sdamien	    IPW_IO_LED_OFF);
2017145247Sdamien
2018145247Sdamien	/* enable interrupts */
2019145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
2020145247Sdamien
2021145247Sdamien	/* kick the firmware */
2022145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
2023145247Sdamien
2024156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
2025156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
2026145247Sdamien
2027145247Sdamien	/* wait at most one second for firmware initialization to complete */
2028145247Sdamien	if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
2029145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for firmware "
2030145247Sdamien		    "initialization to complete\n");
2031145247Sdamien		return error;
2032145247Sdamien	}
2033145247Sdamien
2034156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_IO);
2035156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2036156599Sdamien	    IPW_IO_GPIO3_MASK);
2037145247Sdamien
2038145247Sdamien	return 0;
2039145247Sdamien}
2040145247Sdamien
2041145247Sdamienstatic int
2042172567Sthompsaipw_setwepkeys(struct ipw_softc *sc)
2043172567Sthompsa{
2044178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2045178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2046178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2047172567Sthompsa	struct ipw_wep_key wepkey;
2048172567Sthompsa	struct ieee80211_key *wk;
2049172567Sthompsa	int error, i;
2050172567Sthompsa
2051172567Sthompsa	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2052178354Ssam		wk = &vap->iv_nw_keys[i];
2053172567Sthompsa
2054172567Sthompsa		if (wk->wk_cipher == NULL ||
2055172567Sthompsa		    wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2056172567Sthompsa			continue;
2057172567Sthompsa
2058172567Sthompsa		wepkey.idx = i;
2059172567Sthompsa		wepkey.len = wk->wk_keylen;
2060172567Sthompsa		memset(wepkey.key, 0, sizeof wepkey.key);
2061172567Sthompsa		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2062172567Sthompsa		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2063172567Sthompsa		    wepkey.len));
2064172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2065172567Sthompsa		    sizeof wepkey);
2066172567Sthompsa		if (error != 0)
2067172567Sthompsa			return error;
2068172567Sthompsa	}
2069172567Sthompsa	return 0;
2070172567Sthompsa}
2071172567Sthompsa
2072172567Sthompsastatic int
2073172567Sthompsaipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2074172567Sthompsa{
2075172567Sthompsa	struct ipw_wpa_ie wpaie;
2076172567Sthompsa
2077172567Sthompsa	memset(&wpaie, 0, sizeof(wpaie));
2078172567Sthompsa	wpaie.len = htole32(ielen);
2079172567Sthompsa	/* XXX verify length */
2080172567Sthompsa	memcpy(&wpaie.ie, ie, ielen);
2081172567Sthompsa	DPRINTF(("Setting WPA IE\n"));
2082172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2083172567Sthompsa}
2084172567Sthompsa
2085172567Sthompsastatic int
2086172567Sthompsaipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2087172567Sthompsa{
2088172567Sthompsa	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2089172567Sthompsa
2090172567Sthompsa	if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2091172567Sthompsa		DPRINTF(("Setting mandatory BSSID to null\n"));
2092172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2093172567Sthompsa	} else {
2094172567Sthompsa		DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2095172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2096172567Sthompsa			bssid, IEEE80211_ADDR_LEN);
2097172567Sthompsa	}
2098172567Sthompsa}
2099172567Sthompsa
2100172567Sthompsastatic int
2101172567Sthompsaipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2102172567Sthompsa{
2103172567Sthompsa	if (ssidlen == 0) {
2104172567Sthompsa		/*
2105172567Sthompsa		 * A bug in the firmware breaks the ``don't associate''
2106172567Sthompsa		 * bit in the scan options command.  To compensate for
2107172567Sthompsa		 * this install a bogus ssid when no ssid is specified
2108172567Sthompsa		 * so the firmware won't try to associate.
2109172567Sthompsa		 */
2110172567Sthompsa		DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2111172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2112172567Sthompsa			"\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2113172567Sthompsa			"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2114172567Sthompsa			"\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2115172567Sthompsa			"\x3c\x3d", IEEE80211_NWID_LEN);
2116172567Sthompsa	} else {
2117172567Sthompsa#ifdef IPW_DEBUG
2118172567Sthompsa		if (ipw_debug > 0) {
2119172567Sthompsa			printf("Setting ESSID to ");
2120172567Sthompsa			ieee80211_print_essid(ssid, ssidlen);
2121172567Sthompsa			printf("\n");
2122172567Sthompsa		}
2123172567Sthompsa#endif
2124172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2125172567Sthompsa	}
2126172567Sthompsa}
2127172567Sthompsa
2128172567Sthompsastatic int
2129172567Sthompsaipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2130172567Sthompsa{
2131172567Sthompsa	struct ipw_scan_options opts;
2132172567Sthompsa
2133172567Sthompsa	DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2134172567Sthompsa	opts.channels = htole32(chanmask);
2135172567Sthompsa	opts.flags = htole32(flags);
2136172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2137172567Sthompsa}
2138172567Sthompsa
2139172567Sthompsastatic int
2140172567Sthompsaipw_scan(struct ipw_softc *sc)
2141172567Sthompsa{
2142172567Sthompsa	uint32_t params;
2143172567Sthompsa	int error;
2144172567Sthompsa
2145172567Sthompsa	DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2146172567Sthompsa
2147172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
2148172567Sthompsa		return (EBUSY);
2149172567Sthompsa	sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2150172567Sthompsa
2151172567Sthompsa	/* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2152172567Sthompsa	error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2153172567Sthompsa	if (error != 0)
2154172567Sthompsa		goto done;
2155172567Sthompsa
2156172567Sthompsa	/*
2157172567Sthompsa	 * Setup null/bogus ssid so firmware doesn't use any previous
2158172567Sthompsa	 * ssid to try and associate.  This is because the ``don't
2159172567Sthompsa	 * associate'' option bit is broken (sigh).
2160172567Sthompsa	 */
2161172567Sthompsa	error = ipw_setssid(sc, NULL, 0);
2162172567Sthompsa	if (error != 0)
2163172567Sthompsa		goto done;
2164172567Sthompsa
2165172567Sthompsa	/*
2166172567Sthompsa	 * NB: the adapter may be disabled on association lost;
2167172567Sthompsa	 *     if so just re-enable it to kick off scanning.
2168172567Sthompsa	 */
2169172567Sthompsa	DPRINTF(("Starting scan\n"));
2170172567Sthompsa	sc->sc_scan_timer = 3;
2171172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
2172172567Sthompsa		params = 0;				/* XXX? */
2173172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2174172567Sthompsa				&params, sizeof(params));
2175172567Sthompsa	} else
2176172567Sthompsa		error = ipw_enable(sc);
2177172567Sthompsadone:
2178172567Sthompsa	if (error != 0) {
2179172567Sthompsa		DPRINTF(("Scan failed\n"));
2180172567Sthompsa		sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2181172567Sthompsa	}
2182172567Sthompsa	return (error);
2183172567Sthompsa}
2184172567Sthompsa
2185172567Sthompsastatic int
2186172567Sthompsaipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2187172567Sthompsa{
2188178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2189178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2190172567Sthompsa	uint32_t data;
2191172567Sthompsa	int error;
2192172567Sthompsa
2193172567Sthompsa	data = htole32(ieee80211_chan2ieee(ic, chan));
2194172567Sthompsa	DPRINTF(("Setting channel to %u\n", le32toh(data)));
2195172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2196172567Sthompsa	if (error == 0)
2197172567Sthompsa		ipw_setcurchan(sc, chan);
2198172567Sthompsa	return error;
2199172567Sthompsa}
2200172567Sthompsa
2201178354Ssamstatic void
2202191746Sthompsaipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2203145247Sdamien{
2204178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2205178354Ssam	struct ipw_softc *sc = ifp->if_softc;
2206178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2207145247Sdamien	struct ipw_security security;
2208145247Sdamien	uint32_t data;
2209172567Sthompsa	int error;
2210178354Ssam	IPW_LOCK_DECL;
2211145247Sdamien
2212178354Ssam	IPW_LOCK(sc);
2213172567Sthompsa	error = ipw_disable(sc);
2214172567Sthompsa	if (error != 0)
2215178354Ssam		goto done;
2216172567Sthompsa
2217152637Sdamien	memset(&security, 0, sizeof security);
2218178354Ssam	security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2219145247Sdamien	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2220145247Sdamien	security.ciphers = htole32(IPW_CIPHER_NONE);
2221145247Sdamien	DPRINTF(("Setting authmode to %u\n", security.authmode));
2222172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2223145247Sdamien	    sizeof security);
2224145247Sdamien	if (error != 0)
2225178354Ssam		goto done;
2226145247Sdamien
2227178354Ssam	data = htole32(vap->iv_rtsthreshold);
2228178354Ssam	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2229178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2230145247Sdamien	if (error != 0)
2231178354Ssam		goto done;
2232145247Sdamien
2233178354Ssam	data = htole32(vap->iv_fragthreshold);
2234178354Ssam	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2235178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2236145247Sdamien	if (error != 0)
2237178354Ssam		goto done;
2238145247Sdamien
2239178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2240172567Sthompsa		error = ipw_setwepkeys(sc);
2241172567Sthompsa		if (error != 0)
2242178354Ssam			goto done;
2243172567Sthompsa
2244178354Ssam		if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2245178354Ssam			data = htole32(vap->iv_def_txkey);
2246172567Sthompsa			DPRINTF(("Setting wep tx key index to %u\n",
2247172567Sthompsa				le32toh(data)));
2248172567Sthompsa			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2249172567Sthompsa			    sizeof data);
2250172567Sthompsa			if (error != 0)
2251178354Ssam				goto done;
2252172567Sthompsa		}
2253172567Sthompsa	}
2254172567Sthompsa
2255178354Ssam	data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2256172567Sthompsa	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2257172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2258172567Sthompsa	if (error != 0)
2259178354Ssam		goto done;
2260172567Sthompsa
2261172567Sthompsa	error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2262172567Sthompsa	if (error != 0)
2263178354Ssam		goto done;
2264172567Sthompsa
2265172567Sthompsa	error = ipw_setbssid(sc, ni->ni_bssid);
2266172567Sthompsa	if (error != 0)
2267178354Ssam		goto done;
2268172567Sthompsa
2269206766Sbschmidt	if (vap->iv_appie_wpa != NULL) {
2270206766Sbschmidt		struct ieee80211_appie *ie = vap->iv_appie_wpa;
2271178354Ssam		error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2272172567Sthompsa		if (error != 0)
2273178354Ssam			goto done;
2274172567Sthompsa	}
2275172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2276172567Sthompsa		error = ipw_setchannel(sc, ni->ni_chan);
2277172567Sthompsa		if (error != 0)
2278178354Ssam			goto done;
2279172567Sthompsa	}
2280172567Sthompsa
2281172567Sthompsa	/* lock scan to ap's channel and enable associate */
2282172567Sthompsa	error = ipw_setscanopts(sc,
2283178354Ssam	    1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2284178354Ssam	if (error != 0)
2285178354Ssam		goto done;
2286172567Sthompsa
2287178354Ssam	error = ipw_enable(sc);		/* finally, enable adapter */
2288178354Ssam	if (error == 0)
2289178354Ssam		sc->flags |= IPW_FLAG_ASSOCIATING;
2290178354Ssamdone:
2291178354Ssam	IPW_UNLOCK(sc);
2292172567Sthompsa}
2293172567Sthompsa
2294172567Sthompsastatic void
2295191746Sthompsaipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2296172567Sthompsa{
2297178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2298178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2299178354Ssam	struct ipw_softc *sc = ifp->if_softc;
2300172567Sthompsa	IPW_LOCK_DECL;
2301172567Sthompsa
2302172567Sthompsa	IPW_LOCK(sc);
2303172567Sthompsa	DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2304172567Sthompsa	/*
2305172567Sthompsa	 * NB: don't try to do this if ipw_stop_master has
2306172567Sthompsa	 *     shutdown the firmware and disabled interrupts.
2307172567Sthompsa	 */
2308178354Ssam	if (sc->flags & IPW_FLAG_FW_INITED) {
2309178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATED;
2310178354Ssam		/*
2311178354Ssam		 * NB: firmware currently ignores bssid parameter, but
2312178354Ssam		 *     supply it in case this changes (follow linux driver).
2313178354Ssam		 */
2314178354Ssam		(void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2315178354Ssam			ni->ni_bssid, IEEE80211_ADDR_LEN);
2316178354Ssam	}
2317178354Ssam	IPW_UNLOCK(sc);
2318172567Sthompsa}
2319172567Sthompsa
2320172567Sthompsa/*
2321156599Sdamien * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2322156599Sdamien * It is called on firmware panics or on watchdog timeouts.
2323156599Sdamien */
2324145247Sdamienstatic void
2325156599Sdamienipw_init_task(void *context, int pending)
2326156599Sdamien{
2327156599Sdamien	ipw_init(context);
2328156599Sdamien}
2329156599Sdamien
2330156599Sdamienstatic void
2331145247Sdamienipw_init(void *priv)
2332145247Sdamien{
2333145247Sdamien	struct ipw_softc *sc = priv;
2334178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2335178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2336172567Sthompsa	IPW_LOCK_DECL;
2337172567Sthompsa
2338172567Sthompsa	IPW_LOCK(sc);
2339178354Ssam	ipw_init_locked(sc);
2340172567Sthompsa	IPW_UNLOCK(sc);
2341178354Ssam
2342178931Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2343178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2344172567Sthompsa}
2345172567Sthompsa
2346172567Sthompsastatic void
2347178354Ssamipw_init_locked(struct ipw_softc *sc)
2348172567Sthompsa{
2349178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2350178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2351178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2352166756Sluigi	const struct firmware *fp;
2353156599Sdamien	const struct ipw_firmware_hdr *hdr;
2354178354Ssam	const char *fw;
2355145247Sdamien
2356172567Sthompsa	IPW_LOCK_ASSERT(sc);
2357156599Sdamien
2358172567Sthompsa	DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2359178354Ssam		ieee80211_state_name[vap->iv_state], sc->flags));
2360172567Sthompsa
2361156599Sdamien	/*
2362156599Sdamien	 * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2363156599Sdamien	 * when loading the firmware and we don't want to be called during this
2364156599Sdamien	 * operation.
2365156599Sdamien	 */
2366172567Sthompsa	if (sc->flags & IPW_FLAG_INIT_LOCKED)
2367145247Sdamien		return;
2368156599Sdamien	sc->flags |= IPW_FLAG_INIT_LOCKED;
2369145247Sdamien
2370172567Sthompsa	ipw_stop_locked(sc);
2371145247Sdamien
2372145247Sdamien	if (ipw_reset(sc) != 0) {
2373145247Sdamien		device_printf(sc->sc_dev, "could not reset adapter\n");
2374178354Ssam		goto fail;
2375145247Sdamien	}
2376145247Sdamien
2377159487Siedowse	if (sc->sc_firmware == NULL) {
2378178354Ssam		device_printf(sc->sc_dev, "no firmware\n");
2379178354Ssam		goto fail;
2380156599Sdamien	}
2381178354Ssam	/* NB: consistency already checked on load */
2382159487Siedowse	fp = sc->sc_firmware;
2383156599Sdamien	hdr = (const struct ipw_firmware_hdr *)fp->data;
2384156599Sdamien
2385178354Ssam	DPRINTF(("Loading firmware image '%s'\n", fp->name));
2386156599Sdamien	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2387156599Sdamien	if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2388145247Sdamien		device_printf(sc->sc_dev, "could not load microcode\n");
2389178354Ssam		goto fail;
2390145247Sdamien	}
2391145247Sdamien
2392145247Sdamien	ipw_stop_master(sc);
2393145247Sdamien
2394145247Sdamien	/*
2395145247Sdamien	 * Setup tx, rx and status rings.
2396145247Sdamien	 */
2397145247Sdamien	sc->txold = IPW_NTBD - 1;
2398145247Sdamien	sc->txcur = 0;
2399145247Sdamien	sc->txfree = IPW_NTBD - 2;
2400145247Sdamien	sc->rxcur = IPW_NRBD - 1;
2401145247Sdamien
2402145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2403145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2404145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2405145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2406145247Sdamien
2407145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2408145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2409145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2410145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2411145247Sdamien
2412145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2413145247Sdamien
2414156599Sdamien	fw = (const char *)fp->data + sizeof *hdr;
2415156599Sdamien	if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2416145247Sdamien		device_printf(sc->sc_dev, "could not load firmware\n");
2417178354Ssam		goto fail;
2418145247Sdamien	}
2419145247Sdamien
2420145247Sdamien	sc->flags |= IPW_FLAG_FW_INITED;
2421145247Sdamien
2422145247Sdamien	/* retrieve information tables base addresses */
2423145247Sdamien	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2424145247Sdamien	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2425145247Sdamien
2426145247Sdamien	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2427145247Sdamien
2428145247Sdamien	if (ipw_config(sc) != 0) {
2429145247Sdamien		device_printf(sc->sc_dev, "device configuration failed\n");
2430178354Ssam		goto fail;
2431145247Sdamien	}
2432145247Sdamien
2433172567Sthompsa	callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2434148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2435148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2436145247Sdamien
2437156599Sdamien	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2438145247Sdamien	return;
2439145247Sdamien
2440178354Ssamfail:
2441172567Sthompsa	ipw_stop_locked(sc);
2442156599Sdamien	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2443145247Sdamien}
2444145247Sdamien
2445178354Ssamstatic int
2446178354Ssamipw_config(struct ipw_softc *sc)
2447178354Ssam{
2448178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2449178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2450178354Ssam	struct ipw_configuration config;
2451178354Ssam	uint32_t data;
2452178354Ssam	int error;
2453178354Ssam
2454178354Ssam	error = ipw_disable(sc);
2455178354Ssam	if (error != 0)
2456178354Ssam		return error;
2457178354Ssam
2458178354Ssam	switch (ic->ic_opmode) {
2459178354Ssam	case IEEE80211_M_STA:
2460178354Ssam	case IEEE80211_M_HOSTAP:
2461178354Ssam	case IEEE80211_M_WDS:		/* XXX */
2462178354Ssam		data = htole32(IPW_MODE_BSS);
2463178354Ssam		break;
2464178354Ssam	case IEEE80211_M_IBSS:
2465178354Ssam	case IEEE80211_M_AHDEMO:
2466178354Ssam		data = htole32(IPW_MODE_IBSS);
2467178354Ssam		break;
2468178354Ssam	case IEEE80211_M_MONITOR:
2469178354Ssam		data = htole32(IPW_MODE_MONITOR);
2470178354Ssam		break;
2471195562Srpaulo	default:
2472195562Srpaulo		device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2473195562Srpaulo		return EINVAL;
2474178354Ssam	}
2475178354Ssam	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2476178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2477178354Ssam	if (error != 0)
2478178354Ssam		return error;
2479178354Ssam
2480178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2481178354Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2482178354Ssam		error = ipw_setchannel(sc, ic->ic_curchan);
2483178354Ssam		if (error != 0)
2484178354Ssam			return error;
2485178354Ssam	}
2486178354Ssam
2487178354Ssam	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2488178354Ssam		return ipw_enable(sc);
2489178354Ssam
2490178354Ssam	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2491178354Ssam	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2492178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
2493178354Ssam		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2494178354Ssam	if (ifp->if_flags & IFF_PROMISC)
2495178354Ssam		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2496178354Ssam	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2497178354Ssam	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2498178354Ssam	DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2499178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2500178354Ssam	if (error != 0)
2501178354Ssam		return error;
2502178354Ssam
2503178354Ssam	data = htole32(0x3); /* 1, 2 */
2504178354Ssam	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2505178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2506178354Ssam	if (error != 0)
2507178354Ssam		return error;
2508178354Ssam
2509178354Ssam	/* NB: use the same rate set */
2510178354Ssam	DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2511178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2512178354Ssam	if (error != 0)
2513178354Ssam		return error;
2514178354Ssam
2515178354Ssam	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2516178354Ssam	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2517178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2518178354Ssam	if (error != 0)
2519178354Ssam		return error;
2520178354Ssam
2521178354Ssam	data = htole32(IPW_POWER_MODE_CAM);
2522178354Ssam	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2523178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2524178354Ssam	if (error != 0)
2525178354Ssam		return error;
2526178354Ssam
2527178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2528178354Ssam		data = htole32(32); /* default value */
2529178354Ssam		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2530178354Ssam		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2531178354Ssam		    sizeof data);
2532178354Ssam		if (error != 0)
2533178354Ssam			return error;
2534178354Ssam	}
2535178354Ssam
2536178354Ssam	return 0;
2537178354Ssam}
2538178354Ssam
2539145247Sdamienstatic void
2540145247Sdamienipw_stop(void *priv)
2541145247Sdamien{
2542145247Sdamien	struct ipw_softc *sc = priv;
2543172567Sthompsa	IPW_LOCK_DECL;
2544172567Sthompsa
2545172567Sthompsa	IPW_LOCK(sc);
2546172567Sthompsa	ipw_stop_locked(sc);
2547172567Sthompsa	IPW_UNLOCK(sc);
2548172567Sthompsa}
2549172567Sthompsa
2550172567Sthompsastatic void
2551172567Sthompsaipw_stop_locked(struct ipw_softc *sc)
2552172567Sthompsa{
2553178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2554145247Sdamien	int i;
2555145247Sdamien
2556172567Sthompsa	IPW_LOCK_ASSERT(sc);
2557156599Sdamien
2558172567Sthompsa	callout_stop(&sc->sc_wdtimer);
2559145247Sdamien	ipw_stop_master(sc);
2560145247Sdamien
2561145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2562145247Sdamien
2563145247Sdamien	/*
2564145247Sdamien	 * Release tx buffers.
2565145247Sdamien	 */
2566145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
2567145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
2568145247Sdamien
2569145247Sdamien	sc->sc_tx_timer = 0;
2570148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2571145247Sdamien}
2572145247Sdamien
2573145247Sdamienstatic int
2574145247Sdamienipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2575145247Sdamien{
2576145247Sdamien	struct ipw_softc *sc = arg1;
2577145247Sdamien	uint32_t i, size, buf[256];
2578145247Sdamien
2579174317Sphilip	memset(buf, 0, sizeof buf);
2580174317Sphilip
2581174317Sphilip	if (!(sc->flags & IPW_FLAG_FW_INITED))
2582145247Sdamien		return SYSCTL_OUT(req, buf, sizeof buf);
2583145247Sdamien
2584145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2585145247Sdamien
2586145247Sdamien	size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2587145247Sdamien	for (i = 1; i < size; i++)
2588145247Sdamien		buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2589145247Sdamien
2590174317Sphilip	return SYSCTL_OUT(req, buf, size);
2591145247Sdamien}
2592145247Sdamien
2593145247Sdamienstatic int
2594145247Sdamienipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2595145247Sdamien{
2596145247Sdamien	struct ipw_softc *sc = arg1;
2597145247Sdamien	int val;
2598145247Sdamien
2599145247Sdamien	val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2600145247Sdamien	        (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2601145247Sdamien
2602145247Sdamien	return SYSCTL_OUT(req, &val, sizeof val);
2603145247Sdamien}
2604145247Sdamien
2605145247Sdamienstatic uint32_t
2606145247Sdamienipw_read_table1(struct ipw_softc *sc, uint32_t off)
2607145247Sdamien{
2608145247Sdamien	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2609145247Sdamien}
2610145247Sdamien
2611145247Sdamienstatic void
2612145247Sdamienipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2613145247Sdamien{
2614145247Sdamien	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2615145247Sdamien}
2616145247Sdamien
2617172567Sthompsa#if 0
2618145247Sdamienstatic int
2619145247Sdamienipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2620145247Sdamien{
2621145247Sdamien	uint32_t addr, info;
2622145247Sdamien	uint16_t count, size;
2623145247Sdamien	uint32_t total;
2624145247Sdamien
2625145247Sdamien	/* addr[4] + count[2] + size[2] */
2626145247Sdamien	addr = MEM_READ_4(sc, sc->table2_base + off);
2627145247Sdamien	info = MEM_READ_4(sc, sc->table2_base + off + 4);
2628145247Sdamien
2629145247Sdamien	count = info >> 16;
2630145247Sdamien	size = info & 0xffff;
2631145247Sdamien	total = count * size;
2632145247Sdamien
2633145247Sdamien	if (total > *len) {
2634145247Sdamien		*len = total;
2635145247Sdamien		return EINVAL;
2636145247Sdamien	}
2637145247Sdamien
2638145247Sdamien	*len = total;
2639145247Sdamien	ipw_read_mem_1(sc, addr, buf, total);
2640145247Sdamien
2641145247Sdamien	return 0;
2642145247Sdamien}
2643145247Sdamien
2644145247Sdamienstatic void
2645145247Sdamienipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2646145247Sdamien    bus_size_t count)
2647145247Sdamien{
2648145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2649145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2650145247Sdamien		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2651145247Sdamien	}
2652145247Sdamien}
2653172567Sthompsa#endif
2654145247Sdamien
2655145247Sdamienstatic void
2656156599Sdamienipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2657145247Sdamien    bus_size_t count)
2658145247Sdamien{
2659145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2660145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2661145247Sdamien		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2662145247Sdamien	}
2663145247Sdamien}
2664172567Sthompsa
2665172567Sthompsastatic void
2666172567Sthompsaipw_scan_start(struct ieee80211com *ic)
2667172567Sthompsa{
2668172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2669172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2670172567Sthompsa	IPW_LOCK_DECL;
2671172567Sthompsa
2672172567Sthompsa	IPW_LOCK(sc);
2673191746Sthompsa	ipw_scan(sc);
2674172567Sthompsa	IPW_UNLOCK(sc);
2675172567Sthompsa}
2676172567Sthompsa
2677172567Sthompsastatic void
2678172567Sthompsaipw_set_channel(struct ieee80211com *ic)
2679172567Sthompsa{
2680172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2681172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2682172567Sthompsa	IPW_LOCK_DECL;
2683172567Sthompsa
2684172567Sthompsa	IPW_LOCK(sc);
2685172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2686172567Sthompsa		ipw_disable(sc);
2687172567Sthompsa		ipw_setchannel(sc, ic->ic_curchan);
2688172567Sthompsa		ipw_enable(sc);
2689172567Sthompsa	}
2690172567Sthompsa	IPW_UNLOCK(sc);
2691172567Sthompsa}
2692172567Sthompsa
2693172567Sthompsastatic void
2694178354Ssamipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2695172567Sthompsa{
2696172567Sthompsa	/* NB: all channels are scanned at once */
2697172567Sthompsa}
2698172567Sthompsa
2699172567Sthompsastatic void
2700178354Ssamipw_scan_mindwell(struct ieee80211_scan_state *ss)
2701172567Sthompsa{
2702172567Sthompsa	/* NB: don't try to abort scan; wait for firmware to finish */
2703172567Sthompsa}
2704172567Sthompsa
2705172567Sthompsastatic void
2706172567Sthompsaipw_scan_end(struct ieee80211com *ic)
2707172567Sthompsa{
2708172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2709172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2710172567Sthompsa	IPW_LOCK_DECL;
2711172567Sthompsa
2712172567Sthompsa	IPW_LOCK(sc);
2713172567Sthompsa	sc->flags &= ~IPW_FLAG_SCANNING;
2714172567Sthompsa	IPW_UNLOCK(sc);
2715172567Sthompsa}
2716