if_ipw.c revision 178957
1145247Sdamien/*	$FreeBSD: head/sys/dev/ipw/if_ipw.c 178957 2008-05-12 00:15:30Z sam $	*/
2145247Sdamien
3145247Sdamien/*-
4156599Sdamien * Copyright (c) 2004-2006
5145247Sdamien *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6172567Sthompsa * Copyright (c) 2006 Sam Leffler, Errno Consulting
7172567Sthompsa * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
8145247Sdamien *
9145247Sdamien * Redistribution and use in source and binary forms, with or without
10145247Sdamien * modification, are permitted provided that the following conditions
11145247Sdamien * are met:
12145247Sdamien * 1. Redistributions of source code must retain the above copyright
13145247Sdamien *    notice unmodified, this list of conditions, and the following
14145247Sdamien *    disclaimer.
15145247Sdamien * 2. Redistributions in binary form must reproduce the above copyright
16145247Sdamien *    notice, this list of conditions and the following disclaimer in the
17145247Sdamien *    documentation and/or other materials provided with the distribution.
18145247Sdamien *
19145247Sdamien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20145247Sdamien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21145247Sdamien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22145247Sdamien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23145247Sdamien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24145247Sdamien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25145247Sdamien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26145247Sdamien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27145247Sdamien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28145247Sdamien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29145247Sdamien * SUCH DAMAGE.
30145247Sdamien */
31145247Sdamien
32145247Sdamien#include <sys/cdefs.h>
33145247Sdamien__FBSDID("$FreeBSD: head/sys/dev/ipw/if_ipw.c 178957 2008-05-12 00:15:30Z sam $");
34145247Sdamien
35145247Sdamien/*-
36145247Sdamien * Intel(R) PRO/Wireless 2100 MiniPCI driver
37145247Sdamien * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
38145247Sdamien */
39145247Sdamien
40145247Sdamien#include <sys/param.h>
41145247Sdamien#include <sys/sysctl.h>
42145247Sdamien#include <sys/sockio.h>
43145247Sdamien#include <sys/mbuf.h>
44145247Sdamien#include <sys/kernel.h>
45145247Sdamien#include <sys/socket.h>
46145247Sdamien#include <sys/systm.h>
47145247Sdamien#include <sys/malloc.h>
48156599Sdamien#include <sys/queue.h>
49156599Sdamien#include <sys/taskqueue.h>
50145247Sdamien#include <sys/module.h>
51145247Sdamien#include <sys/bus.h>
52145247Sdamien#include <sys/endian.h>
53156599Sdamien#include <sys/linker.h>
54156599Sdamien#include <sys/firmware.h>
55145247Sdamien
56145247Sdamien#include <machine/bus.h>
57145247Sdamien#include <machine/resource.h>
58145247Sdamien#include <sys/rman.h>
59145247Sdamien
60145247Sdamien#include <dev/pci/pcireg.h>
61145247Sdamien#include <dev/pci/pcivar.h>
62145247Sdamien
63145247Sdamien#include <net/bpf.h>
64145247Sdamien#include <net/if.h>
65145247Sdamien#include <net/if_arp.h>
66145247Sdamien#include <net/ethernet.h>
67145247Sdamien#include <net/if_dl.h>
68145247Sdamien#include <net/if_media.h>
69145247Sdamien#include <net/if_types.h>
70145247Sdamien
71156599Sdamien#include <net80211/ieee80211_var.h>
72156599Sdamien#include <net80211/ieee80211_radiotap.h>
73156599Sdamien
74145247Sdamien#include <netinet/in.h>
75145247Sdamien#include <netinet/in_systm.h>
76145247Sdamien#include <netinet/in_var.h>
77145247Sdamien#include <netinet/ip.h>
78145247Sdamien#include <netinet/if_ether.h>
79145247Sdamien
80145247Sdamien#include <dev/ipw/if_ipwreg.h>
81145247Sdamien#include <dev/ipw/if_ipwvar.h>
82145247Sdamien
83172567Sthompsa#define IPW_DEBUG
84145247Sdamien#ifdef IPW_DEBUG
85145247Sdamien#define DPRINTF(x)	do { if (ipw_debug > 0) printf x; } while (0)
86145247Sdamien#define DPRINTFN(n, x)	do { if (ipw_debug >= (n)) printf x; } while (0)
87145247Sdamienint ipw_debug = 0;
88145247SdamienSYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
89145247Sdamien#else
90145247Sdamien#define DPRINTF(x)
91145247Sdamien#define DPRINTFN(n, x)
92145247Sdamien#endif
93145247Sdamien
94145247SdamienMODULE_DEPEND(ipw, pci,  1, 1, 1);
95145247SdamienMODULE_DEPEND(ipw, wlan, 1, 1, 1);
96156599SdamienMODULE_DEPEND(ipw, firmware, 1, 1, 1);
97145247Sdamien
98145247Sdamienstruct ipw_ident {
99145247Sdamien	uint16_t	vendor;
100145247Sdamien	uint16_t	device;
101145247Sdamien	const char	*name;
102145247Sdamien};
103145247Sdamien
104145247Sdamienstatic const struct ipw_ident ipw_ident_table[] = {
105145247Sdamien	{ 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
106145247Sdamien
107145247Sdamien	{ 0, 0, NULL }
108145247Sdamien};
109145247Sdamien
110178354Ssamstatic struct ieee80211vap *ipw_vap_create(struct ieee80211com *,
111178354Ssam		    const char name[IFNAMSIZ], int unit, int opmode, int flags,
112178354Ssam		    const uint8_t bssid[IEEE80211_ADDR_LEN],
113178354Ssam		    const uint8_t mac[IEEE80211_ADDR_LEN]);
114178354Ssamstatic void	ipw_vap_delete(struct ieee80211vap *);
115145247Sdamienstatic int	ipw_dma_alloc(struct ipw_softc *);
116145247Sdamienstatic void	ipw_release(struct ipw_softc *);
117145247Sdamienstatic void	ipw_media_status(struct ifnet *, struct ifmediareq *);
118178354Ssamstatic int	ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
119145247Sdamienstatic uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
120172567Sthompsastatic void	ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
121178354Ssamstatic void	ipw_assocsuccess(void *, int);
122178354Ssamstatic void	ipw_assocfailed(void *, int);
123178354Ssamstatic void	ipw_scandone(void *, int);
124178354Ssamstatic void	ipw_bmiss(void *, int);
125172567Sthompsastatic void	ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
126172567Sthompsastatic void	ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
127145247Sdamien		    struct ipw_soft_bd *, struct ipw_soft_buf *);
128145247Sdamienstatic void	ipw_rx_intr(struct ipw_softc *);
129145247Sdamienstatic void	ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
130145247Sdamienstatic void	ipw_tx_intr(struct ipw_softc *);
131145247Sdamienstatic void	ipw_intr(void *);
132145247Sdamienstatic void	ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
133172567Sthompsastatic const char * ipw_cmdname(int);
134145247Sdamienstatic int	ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
135145247Sdamienstatic int	ipw_tx_start(struct ifnet *, struct mbuf *,
136145247Sdamien		    struct ieee80211_node *);
137178354Ssamstatic int	ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
138178354Ssam		    const struct ieee80211_bpf_params *);
139145247Sdamienstatic void	ipw_start(struct ifnet *);
140172567Sthompsastatic void	ipw_start_locked(struct ifnet *);
141172567Sthompsastatic void	ipw_watchdog(void *);
142145247Sdamienstatic int	ipw_ioctl(struct ifnet *, u_long, caddr_t);
143145247Sdamienstatic void	ipw_stop_master(struct ipw_softc *);
144172567Sthompsastatic int	ipw_enable(struct ipw_softc *);
145172567Sthompsastatic int	ipw_disable(struct ipw_softc *);
146145247Sdamienstatic int	ipw_reset(struct ipw_softc *);
147156599Sdamienstatic int	ipw_load_ucode(struct ipw_softc *, const char *, int);
148156599Sdamienstatic int	ipw_load_firmware(struct ipw_softc *, const char *, int);
149145247Sdamienstatic int	ipw_config(struct ipw_softc *);
150172567Sthompsastatic void	ipw_assoc_task(void *, int);
151172567Sthompsastatic void	ipw_disassoc_task(void *, int);
152156599Sdamienstatic void	ipw_init_task(void *, int);
153145247Sdamienstatic void	ipw_init(void *);
154178354Ssamstatic void	ipw_init_locked(struct ipw_softc *);
155145247Sdamienstatic void	ipw_stop(void *);
156172567Sthompsastatic void	ipw_stop_locked(struct ipw_softc *);
157145247Sdamienstatic int	ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
158145247Sdamienstatic int	ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
159145247Sdamienstatic uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
160145247Sdamienstatic void	ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
161172567Sthompsa#if 0
162145247Sdamienstatic int	ipw_read_table2(struct ipw_softc *, uint32_t, void *,
163145247Sdamien		    uint32_t *);
164145247Sdamienstatic void	ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
165145247Sdamien		    bus_size_t);
166172567Sthompsa#endif
167156599Sdamienstatic void	ipw_write_mem_1(struct ipw_softc *, bus_size_t,
168156599Sdamien		    const uint8_t *, bus_size_t);
169172567Sthompsastatic void	ipw_scan_task(void *, int);
170172567Sthompsastatic int	ipw_scan(struct ipw_softc *);
171172567Sthompsastatic void	ipw_scan_start(struct ieee80211com *);
172172567Sthompsastatic void	ipw_scan_end(struct ieee80211com *);
173172567Sthompsastatic void	ipw_set_channel(struct ieee80211com *);
174178354Ssamstatic void	ipw_scan_curchan(struct ieee80211_scan_state *,
175178354Ssam		    unsigned long maxdwell);
176178354Ssamstatic void	ipw_scan_mindwell(struct ieee80211_scan_state *);
177145247Sdamien
178145247Sdamienstatic int ipw_probe(device_t);
179145247Sdamienstatic int ipw_attach(device_t);
180145247Sdamienstatic int ipw_detach(device_t);
181145247Sdamienstatic int ipw_shutdown(device_t);
182145247Sdamienstatic int ipw_suspend(device_t);
183145247Sdamienstatic int ipw_resume(device_t);
184145247Sdamien
185145247Sdamienstatic device_method_t ipw_methods[] = {
186145247Sdamien	/* Device interface */
187145247Sdamien	DEVMETHOD(device_probe,		ipw_probe),
188145247Sdamien	DEVMETHOD(device_attach,	ipw_attach),
189145247Sdamien	DEVMETHOD(device_detach,	ipw_detach),
190145247Sdamien	DEVMETHOD(device_shutdown,	ipw_shutdown),
191145247Sdamien	DEVMETHOD(device_suspend,	ipw_suspend),
192145247Sdamien	DEVMETHOD(device_resume,	ipw_resume),
193145247Sdamien
194145247Sdamien	{ 0, 0 }
195145247Sdamien};
196145247Sdamien
197145247Sdamienstatic driver_t ipw_driver = {
198145247Sdamien	"ipw",
199145247Sdamien	ipw_methods,
200145247Sdamien	sizeof (struct ipw_softc)
201145247Sdamien};
202145247Sdamien
203145247Sdamienstatic devclass_t ipw_devclass;
204145247Sdamien
205145247SdamienDRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0);
206172567SthompsaDRIVER_MODULE(ipw, cardbus, ipw_driver, ipw_devclass, 0, 0);
207145247Sdamien
208145247Sdamienstatic int
209145247Sdamienipw_probe(device_t dev)
210145247Sdamien{
211145247Sdamien	const struct ipw_ident *ident;
212145247Sdamien
213145247Sdamien	for (ident = ipw_ident_table; ident->name != NULL; ident++) {
214145247Sdamien		if (pci_get_vendor(dev) == ident->vendor &&
215145247Sdamien		    pci_get_device(dev) == ident->device) {
216145247Sdamien			device_set_desc(dev, ident->name);
217145247Sdamien			return 0;
218145247Sdamien		}
219145247Sdamien	}
220145247Sdamien	return ENXIO;
221145247Sdamien}
222145247Sdamien
223145247Sdamien/* Base Address Register */
224145247Sdamien#define IPW_PCI_BAR0	0x10
225145247Sdamien
226145247Sdamienstatic int
227145247Sdamienipw_attach(device_t dev)
228145247Sdamien{
229145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
230147256Sbrooks	struct ifnet *ifp;
231178354Ssam	struct ieee80211com *ic;
232170530Ssam	struct ieee80211_channel *c;
233145247Sdamien	uint16_t val;
234145247Sdamien	int error, i;
235145247Sdamien
236145247Sdamien	sc->sc_dev = dev;
237145247Sdamien
238145247Sdamien	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
239145247Sdamien	    MTX_DEF | MTX_RECURSE);
240145247Sdamien
241156599Sdamien	TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
242172567Sthompsa	TASK_INIT(&sc->sc_scan_task, 0, ipw_scan_task, sc);
243178354Ssam	TASK_INIT(&sc->sc_bmiss_task, 0, ipw_bmiss, sc);
244172567Sthompsa	callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
245156599Sdamien
246145247Sdamien	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
247145247Sdamien		device_printf(dev, "chip is in D%d power mode "
248145247Sdamien		    "-- setting to D0\n", pci_get_powerstate(dev));
249145247Sdamien		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
250145247Sdamien	}
251145247Sdamien
252146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
253146498Sdamien
254145247Sdamien	/* enable bus-mastering */
255145247Sdamien	pci_enable_busmaster(dev);
256145247Sdamien
257145247Sdamien	sc->mem_rid = IPW_PCI_BAR0;
258145247Sdamien	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
259145247Sdamien	    RF_ACTIVE);
260145247Sdamien	if (sc->mem == NULL) {
261145247Sdamien		device_printf(dev, "could not allocate memory resource\n");
262145247Sdamien		goto fail;
263145247Sdamien	}
264145247Sdamien
265145247Sdamien	sc->sc_st = rman_get_bustag(sc->mem);
266145247Sdamien	sc->sc_sh = rman_get_bushandle(sc->mem);
267145247Sdamien
268145247Sdamien	sc->irq_rid = 0;
269145247Sdamien	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
270145247Sdamien	    RF_ACTIVE | RF_SHAREABLE);
271145247Sdamien	if (sc->irq == NULL) {
272145247Sdamien		device_printf(dev, "could not allocate interrupt resource\n");
273178354Ssam		goto fail1;
274145247Sdamien	}
275145247Sdamien
276145247Sdamien	if (ipw_reset(sc) != 0) {
277145247Sdamien		device_printf(dev, "could not reset adapter\n");
278178354Ssam		goto fail2;
279145247Sdamien	}
280145247Sdamien
281145247Sdamien	if (ipw_dma_alloc(sc) != 0) {
282145247Sdamien		device_printf(dev, "could not allocate DMA resources\n");
283178354Ssam		goto fail2;
284145247Sdamien	}
285147757Sdamien
286178354Ssam	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
287147256Sbrooks	if (ifp == NULL) {
288147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
289178354Ssam		goto fail3;
290147256Sbrooks	}
291178354Ssam	ic = ifp->if_l2com;
292147757Sdamien
293145247Sdamien	ifp->if_softc = sc;
294145247Sdamien	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
295145247Sdamien	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
296145247Sdamien	ifp->if_init = ipw_init;
297145247Sdamien	ifp->if_ioctl = ipw_ioctl;
298145247Sdamien	ifp->if_start = ipw_start;
299145247Sdamien	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
300145247Sdamien	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
301145247Sdamien	IFQ_SET_READY(&ifp->if_snd);
302145247Sdamien
303145247Sdamien	ic->ic_ifp = ifp;
304178354Ssam	ic->ic_opmode = IEEE80211_M_STA;
305145247Sdamien	ic->ic_phytype = IEEE80211_T_DS;
306145247Sdamien
307145247Sdamien	/* set device capabilities */
308178957Ssam	ic->ic_caps =
309178957Ssam		  IEEE80211_C_STA		/* station mode supported */
310178957Ssam		| IEEE80211_C_IBSS		/* IBSS mode supported */
311172567Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode supported */
312172567Sthompsa		| IEEE80211_C_PMGT		/* power save supported */
313172567Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
314172567Sthompsa		| IEEE80211_C_WPA		/* 802.11i supported */
315172567Sthompsa		;
316145247Sdamien
317145247Sdamien	/* read MAC address from EEPROM */
318145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
319145247Sdamien	ic->ic_myaddr[0] = val >> 8;
320145247Sdamien	ic->ic_myaddr[1] = val & 0xff;
321145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
322145247Sdamien	ic->ic_myaddr[2] = val >> 8;
323145247Sdamien	ic->ic_myaddr[3] = val & 0xff;
324145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
325145247Sdamien	ic->ic_myaddr[4] = val >> 8;
326145247Sdamien	ic->ic_myaddr[5] = val & 0xff;
327145247Sdamien
328170530Ssam	/* set supported .11b channels (read from EEPROM) */
329170530Ssam	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
330170530Ssam		val = 0x7ff; /* default to channels 1-11 */
331170530Ssam	val <<= 1;
332170530Ssam	for (i = 1; i < 16; i++) {
333170530Ssam		if (val & (1 << i)) {
334170530Ssam			c = &ic->ic_channels[ic->ic_nchans++];
335170530Ssam			c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
336170530Ssam			c->ic_flags = IEEE80211_CHAN_B;
337170530Ssam			c->ic_ieee = i;
338170530Ssam		}
339145247Sdamien	}
340145247Sdamien
341145247Sdamien	/* check support for radio transmitter switch in EEPROM */
342145247Sdamien	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
343145247Sdamien		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
344145247Sdamien
345145247Sdamien	ieee80211_ifattach(ic);
346172567Sthompsa	ic->ic_scan_start = ipw_scan_start;
347172567Sthompsa	ic->ic_scan_end = ipw_scan_end;
348172567Sthompsa	ic->ic_set_channel = ipw_set_channel;
349172567Sthompsa	ic->ic_scan_curchan = ipw_scan_curchan;
350172567Sthompsa	ic->ic_scan_mindwell = ipw_scan_mindwell;
351178354Ssam	ic->ic_raw_xmit = ipw_raw_xmit;
352172567Sthompsa
353178354Ssam	ic->ic_vap_create = ipw_vap_create;
354178354Ssam	ic->ic_vap_delete = ipw_vap_delete;
355145247Sdamien
356178354Ssam	bpfattach(ifp, DLT_IEEE802_11_RADIO,
357178354Ssam	    sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap));
358178354Ssam
359171086Skevlo	sc->sc_rxtap_len = sizeof sc->sc_rxtap;
360145247Sdamien	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
361145247Sdamien	sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
362145247Sdamien
363171086Skevlo	sc->sc_txtap_len = sizeof sc->sc_txtap;
364145247Sdamien	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
365145247Sdamien	sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
366145247Sdamien
367145247Sdamien	/*
368145247Sdamien	 * Add a few sysctl knobs.
369145247Sdamien	 */
370145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
371145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
372145247Sdamien	    CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
373145247Sdamien	    "radio transmitter switch state (0=off, 1=on)");
374145247Sdamien
375145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
376145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
377145247Sdamien	    CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
378145247Sdamien	    "statistics");
379145247Sdamien
380145247Sdamien	/*
381145247Sdamien	 * Hook our interrupt after all initialization is complete.
382145247Sdamien	 */
383145247Sdamien	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
384166901Spiso	    NULL, ipw_intr, sc, &sc->sc_ih);
385145247Sdamien	if (error != 0) {
386145247Sdamien		device_printf(dev, "could not set up interrupt\n");
387178354Ssam		goto fail4;
388145247Sdamien	}
389145247Sdamien
390145247Sdamien	if (bootverbose)
391145247Sdamien		ieee80211_announce(ic);
392145247Sdamien
393145247Sdamien	return 0;
394178354Ssamfail4:
395178354Ssam	if_free(ifp);
396178354Ssamfail3:
397178354Ssam	ipw_release(sc);
398178354Ssamfail2:
399178354Ssam	bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
400178354Ssamfail1:
401178354Ssam	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
402178354Ssamfail:
403178354Ssam	mtx_destroy(&sc->sc_mtx);
404145247Sdamien	return ENXIO;
405145247Sdamien}
406145247Sdamien
407145247Sdamienstatic int
408145247Sdamienipw_detach(device_t dev)
409145247Sdamien{
410145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
411178354Ssam	struct ifnet *ifp = sc->sc_ifp;
412178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
413145247Sdamien
414145247Sdamien	ipw_stop(sc);
415178354Ssam
416178354Ssam	bpfdetach(ifp);
417178354Ssam	ieee80211_ifdetach(ic);
418178354Ssam
419172567Sthompsa	callout_drain(&sc->sc_wdtimer);
420172567Sthompsa	taskqueue_drain(taskqueue_fast, &sc->sc_init_task);
421172567Sthompsa	taskqueue_drain(taskqueue_fast, &sc->sc_scan_task);
422178354Ssam	taskqueue_drain(taskqueue_fast, &sc->sc_bmiss_task);
423145247Sdamien
424145247Sdamien	ipw_release(sc);
425145247Sdamien
426178354Ssam	bus_teardown_intr(dev, sc->irq, sc->sc_ih);
427178354Ssam	bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
428145247Sdamien
429178354Ssam	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
430156599Sdamien
431178354Ssam	if_free(ifp);
432145247Sdamien
433159487Siedowse	if (sc->sc_firmware != NULL) {
434159487Siedowse		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
435159487Siedowse		sc->sc_firmware = NULL;
436159487Siedowse	}
437159487Siedowse
438145247Sdamien	mtx_destroy(&sc->sc_mtx);
439145247Sdamien
440145247Sdamien	return 0;
441145247Sdamien}
442145247Sdamien
443178354Ssamstatic struct ieee80211vap *
444178354Ssamipw_vap_create(struct ieee80211com *ic,
445178354Ssam	const char name[IFNAMSIZ], int unit, int opmode, int flags,
446178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN],
447178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN])
448178354Ssam{
449178354Ssam	struct ifnet *ifp = ic->ic_ifp;
450178354Ssam	struct ipw_softc *sc = ifp->if_softc;
451178354Ssam	struct ipw_vap *ivp;
452178354Ssam	struct ieee80211vap *vap;
453178354Ssam	const struct firmware *fp;
454178354Ssam	const struct ipw_firmware_hdr *hdr;
455178354Ssam	const char *imagename;
456178354Ssam
457178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
458178354Ssam		return NULL;
459178354Ssam
460178354Ssam	switch (opmode) {
461178354Ssam	case IEEE80211_M_STA:
462178354Ssam		imagename = "ipw_bss";
463178354Ssam		break;
464178354Ssam	case IEEE80211_M_IBSS:
465178354Ssam		imagename = "ipw_ibss";
466178354Ssam		break;
467178354Ssam	case IEEE80211_M_MONITOR:
468178354Ssam		imagename = "ipw_monitor";
469178354Ssam		break;
470178354Ssam	default:
471178354Ssam		return NULL;
472178354Ssam	}
473178354Ssam
474178354Ssam	/*
475178354Ssam	 * Load firmware image using the firmware(9) subsystem.  Doing
476178354Ssam	 * this unlocked is ok since we're single-threaded by the
477178354Ssam	 * 802.11 layer.
478178354Ssam	 */
479178354Ssam	if (sc->sc_firmware == NULL ||
480178354Ssam	    strcmp(sc->sc_firmware->name, imagename) != 0) {
481178354Ssam		if (sc->sc_firmware != NULL)
482178354Ssam			firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
483178354Ssam		sc->sc_firmware = firmware_get(imagename);
484178354Ssam	}
485178354Ssam	if (sc->sc_firmware == NULL) {
486178354Ssam		device_printf(sc->sc_dev,
487178354Ssam		    "could not load firmware image '%s'\n", imagename);
488178354Ssam		return NULL;
489178354Ssam	}
490178354Ssam	fp = sc->sc_firmware;
491178354Ssam	if (fp->datasize < sizeof *hdr) {
492178354Ssam		device_printf(sc->sc_dev,
493178354Ssam		    "firmware image too short %zu\n", fp->datasize);
494178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
495178354Ssam		sc->sc_firmware = NULL;
496178354Ssam		return NULL;
497178354Ssam	}
498178354Ssam	hdr = (const struct ipw_firmware_hdr *)fp->data;
499178354Ssam	if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
500178354Ssam	    le32toh(hdr->ucodesz)) {
501178354Ssam		device_printf(sc->sc_dev,
502178354Ssam		    "firmware image too short %zu\n", fp->datasize);
503178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
504178354Ssam		sc->sc_firmware = NULL;
505178354Ssam		return NULL;
506178354Ssam	}
507178354Ssam
508178354Ssam	ivp = (struct ipw_vap *) malloc(sizeof(struct ipw_vap),
509178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
510178354Ssam	if (ivp == NULL)
511178354Ssam		return NULL;
512178354Ssam	vap = &ivp->vap;
513178354Ssam
514178354Ssam	TASK_INIT(&ivp->assoc_task, 0, ipw_assoc_task, vap);
515178354Ssam	TASK_INIT(&ivp->disassoc_task, 0, ipw_disassoc_task, vap);
516178354Ssam	TASK_INIT(&ivp->assoc_success_task, 0, ipw_assocsuccess, vap);
517178354Ssam	TASK_INIT(&ivp->assoc_failed_task, 0, ipw_assocfailed, vap);
518178354Ssam	TASK_INIT(&ivp->scandone_task, 0, ipw_scandone, vap);
519178354Ssam
520178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
521178354Ssam	/* override with driver methods */
522178354Ssam	ivp->newstate = vap->iv_newstate;
523178354Ssam	vap->iv_newstate = ipw_newstate;
524178354Ssam
525178354Ssam	/* complete setup */
526178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status);
527178354Ssam	ic->ic_opmode = opmode;
528178354Ssam	return vap;
529178354Ssam}
530178354Ssam
531178354Ssamstatic void
532178354Ssamipw_vap_delete(struct ieee80211vap *vap)
533178354Ssam{
534178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
535178354Ssam
536178354Ssam	ieee80211_vap_detach(vap);
537178354Ssam	free(ivp, M_80211_VAP);
538178354Ssam}
539178354Ssam
540145247Sdamienstatic int
541145247Sdamienipw_dma_alloc(struct ipw_softc *sc)
542145247Sdamien{
543145247Sdamien	struct ipw_soft_bd *sbd;
544145247Sdamien	struct ipw_soft_hdr *shdr;
545145247Sdamien	struct ipw_soft_buf *sbuf;
546145247Sdamien	bus_addr_t physaddr;
547145247Sdamien	int error, i;
548145247Sdamien
549145247Sdamien	/*
550145247Sdamien	 * Allocate and map tx ring.
551145247Sdamien	 */
552145247Sdamien	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
553145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
554145247Sdamien	    NULL, &sc->tbd_dmat);
555145247Sdamien	if (error != 0) {
556145247Sdamien		device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
557145247Sdamien		goto fail;
558145247Sdamien	}
559145247Sdamien
560145247Sdamien	error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
561145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
562145247Sdamien	if (error != 0) {
563145247Sdamien		device_printf(sc->sc_dev,
564145247Sdamien		    "could not allocate tx ring DMA memory\n");
565145247Sdamien		goto fail;
566145247Sdamien	}
567145247Sdamien
568145247Sdamien	error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
569145247Sdamien	    IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
570145247Sdamien	if (error != 0) {
571145247Sdamien		device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
572145247Sdamien		goto fail;
573145247Sdamien	}
574145247Sdamien
575145247Sdamien	/*
576145247Sdamien	 * Allocate and map rx ring.
577145247Sdamien	 */
578145247Sdamien	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
579145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
580145247Sdamien	    NULL, &sc->rbd_dmat);
581145247Sdamien	if (error != 0) {
582145247Sdamien		device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
583145247Sdamien		goto fail;
584145247Sdamien	}
585145247Sdamien
586145247Sdamien	error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
587145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
588145247Sdamien	if (error != 0) {
589145247Sdamien		device_printf(sc->sc_dev,
590145247Sdamien		    "could not allocate rx ring DMA memory\n");
591145247Sdamien		goto fail;
592145247Sdamien	}
593145247Sdamien
594145247Sdamien	error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
595145247Sdamien	    IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
596145247Sdamien	if (error != 0) {
597145247Sdamien		device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
598145247Sdamien		goto fail;
599145247Sdamien	}
600145247Sdamien
601145247Sdamien	/*
602145247Sdamien	 * Allocate and map status ring.
603145247Sdamien	 */
604145247Sdamien	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
605145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
606145247Sdamien	    NULL, NULL, &sc->status_dmat);
607145247Sdamien	if (error != 0) {
608145247Sdamien		device_printf(sc->sc_dev,
609145247Sdamien		    "could not create status ring DMA tag\n");
610145247Sdamien		goto fail;
611145247Sdamien	}
612145247Sdamien
613145247Sdamien	error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
614145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
615145247Sdamien	if (error != 0) {
616145247Sdamien		device_printf(sc->sc_dev,
617145247Sdamien		    "could not allocate status ring DMA memory\n");
618145247Sdamien		goto fail;
619145247Sdamien	}
620145247Sdamien
621145247Sdamien	error = bus_dmamap_load(sc->status_dmat, sc->status_map,
622145247Sdamien	    sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
623145247Sdamien	    0);
624145247Sdamien	if (error != 0) {
625145247Sdamien		device_printf(sc->sc_dev,
626145247Sdamien		    "could not map status ring DMA memory\n");
627145247Sdamien		goto fail;
628145247Sdamien	}
629145247Sdamien
630145247Sdamien	/*
631145247Sdamien	 * Allocate command DMA map.
632145247Sdamien	 */
633145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
634145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
635145247Sdamien	    sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
636145247Sdamien	if (error != 0) {
637145247Sdamien		device_printf(sc->sc_dev, "could not create command DMA tag\n");
638145247Sdamien		goto fail;
639145247Sdamien	}
640145247Sdamien
641145247Sdamien	error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
642145247Sdamien	if (error != 0) {
643145247Sdamien		device_printf(sc->sc_dev,
644145247Sdamien		    "could not create command DMA map\n");
645145247Sdamien		goto fail;
646145247Sdamien	}
647145247Sdamien
648145247Sdamien	/*
649145247Sdamien	 * Allocate headers DMA maps.
650145247Sdamien	 */
651145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
652145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
653145247Sdamien	    sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
654145247Sdamien	if (error != 0) {
655145247Sdamien		device_printf(sc->sc_dev, "could not create header DMA tag\n");
656145247Sdamien		goto fail;
657145247Sdamien	}
658145247Sdamien
659145247Sdamien	SLIST_INIT(&sc->free_shdr);
660145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
661145247Sdamien		shdr = &sc->shdr_list[i];
662145247Sdamien		error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
663145247Sdamien		if (error != 0) {
664145247Sdamien			device_printf(sc->sc_dev,
665145247Sdamien			    "could not create header DMA map\n");
666145247Sdamien			goto fail;
667145247Sdamien		}
668145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
669145247Sdamien	}
670145247Sdamien
671145247Sdamien	/*
672145247Sdamien	 * Allocate tx buffers DMA maps.
673145247Sdamien	 */
674145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
675145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
676145247Sdamien	    NULL, NULL, &sc->txbuf_dmat);
677145247Sdamien	if (error != 0) {
678145247Sdamien		device_printf(sc->sc_dev, "could not create tx DMA tag\n");
679145247Sdamien		goto fail;
680145247Sdamien	}
681145247Sdamien
682145247Sdamien	SLIST_INIT(&sc->free_sbuf);
683145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
684145247Sdamien		sbuf = &sc->tx_sbuf_list[i];
685145247Sdamien		error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
686145247Sdamien		if (error != 0) {
687145247Sdamien			device_printf(sc->sc_dev,
688145247Sdamien			    "could not create tx DMA map\n");
689145247Sdamien			goto fail;
690145247Sdamien		}
691145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
692145247Sdamien	}
693145247Sdamien
694145247Sdamien	/*
695145247Sdamien	 * Initialize tx ring.
696145247Sdamien	 */
697145247Sdamien	for (i = 0; i < IPW_NTBD; i++) {
698145247Sdamien		sbd = &sc->stbd_list[i];
699145247Sdamien		sbd->bd = &sc->tbd_list[i];
700145247Sdamien		sbd->type = IPW_SBD_TYPE_NOASSOC;
701145247Sdamien	}
702145247Sdamien
703145247Sdamien	/*
704145247Sdamien	 * Pre-allocate rx buffers and DMA maps.
705145247Sdamien	 */
706145247Sdamien	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
707147841Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
708147841Sdamien	    NULL, &sc->rxbuf_dmat);
709145247Sdamien	if (error != 0) {
710145247Sdamien		device_printf(sc->sc_dev, "could not create rx DMA tag\n");
711145247Sdamien		goto fail;
712145247Sdamien	}
713145247Sdamien
714145247Sdamien	for (i = 0; i < IPW_NRBD; i++) {
715145247Sdamien		sbd = &sc->srbd_list[i];
716145247Sdamien		sbuf = &sc->rx_sbuf_list[i];
717145247Sdamien		sbd->bd = &sc->rbd_list[i];
718145247Sdamien
719145247Sdamien		sbuf->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
720145247Sdamien		if (sbuf->m == NULL) {
721145247Sdamien			device_printf(sc->sc_dev,
722145247Sdamien			    "could not allocate rx mbuf\n");
723145247Sdamien			error = ENOMEM;
724145247Sdamien			goto fail;
725145247Sdamien		}
726145247Sdamien
727145247Sdamien		error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
728145247Sdamien		if (error != 0) {
729145247Sdamien			device_printf(sc->sc_dev,
730145247Sdamien			    "could not create rx DMA map\n");
731145247Sdamien			goto fail;
732145247Sdamien		}
733145247Sdamien
734145247Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
735145247Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
736145247Sdamien		    &physaddr, 0);
737145247Sdamien		if (error != 0) {
738145247Sdamien			device_printf(sc->sc_dev,
739145247Sdamien			    "could not map rx DMA memory\n");
740145247Sdamien			goto fail;
741145247Sdamien		}
742145247Sdamien
743145247Sdamien		sbd->type = IPW_SBD_TYPE_DATA;
744145247Sdamien		sbd->priv = sbuf;
745145247Sdamien		sbd->bd->physaddr = htole32(physaddr);
746145247Sdamien		sbd->bd->len = htole32(MCLBYTES);
747145247Sdamien	}
748145247Sdamien
749145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
750145247Sdamien
751145247Sdamien	return 0;
752145247Sdamien
753145247Sdamienfail:	ipw_release(sc);
754145247Sdamien	return error;
755145247Sdamien}
756145247Sdamien
757145247Sdamienstatic void
758145247Sdamienipw_release(struct ipw_softc *sc)
759145247Sdamien{
760145247Sdamien	struct ipw_soft_buf *sbuf;
761145247Sdamien	int i;
762145247Sdamien
763145247Sdamien	if (sc->tbd_dmat != NULL) {
764145247Sdamien		if (sc->stbd_list != NULL) {
765145247Sdamien			bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
766145247Sdamien			bus_dmamem_free(sc->tbd_dmat, sc->tbd_list,
767145247Sdamien			    sc->tbd_map);
768145247Sdamien		}
769145247Sdamien		bus_dma_tag_destroy(sc->tbd_dmat);
770145247Sdamien	}
771145247Sdamien
772145247Sdamien	if (sc->rbd_dmat != NULL) {
773145247Sdamien		if (sc->rbd_list != NULL) {
774145247Sdamien			bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
775145247Sdamien			bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
776145247Sdamien			    sc->rbd_map);
777145247Sdamien		}
778145247Sdamien		bus_dma_tag_destroy(sc->rbd_dmat);
779145247Sdamien	}
780145247Sdamien
781145247Sdamien	if (sc->status_dmat != NULL) {
782145247Sdamien		if (sc->status_list != NULL) {
783145247Sdamien			bus_dmamap_unload(sc->status_dmat, sc->status_map);
784145247Sdamien			bus_dmamem_free(sc->status_dmat, sc->status_list,
785145247Sdamien			    sc->status_map);
786145247Sdamien		}
787145247Sdamien		bus_dma_tag_destroy(sc->status_dmat);
788145247Sdamien	}
789145247Sdamien
790145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
791145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
792145247Sdamien
793145247Sdamien	if (sc->cmd_dmat != NULL) {
794145247Sdamien		bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
795145247Sdamien		bus_dma_tag_destroy(sc->cmd_dmat);
796145247Sdamien	}
797145247Sdamien
798145247Sdamien	if (sc->hdr_dmat != NULL) {
799145247Sdamien		for (i = 0; i < IPW_NDATA; i++)
800145247Sdamien			bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
801145247Sdamien		bus_dma_tag_destroy(sc->hdr_dmat);
802145247Sdamien	}
803145247Sdamien
804145247Sdamien	if (sc->txbuf_dmat != NULL) {
805145247Sdamien		for (i = 0; i < IPW_NDATA; i++) {
806145247Sdamien			bus_dmamap_destroy(sc->txbuf_dmat,
807145247Sdamien			    sc->tx_sbuf_list[i].map);
808145247Sdamien		}
809145247Sdamien		bus_dma_tag_destroy(sc->txbuf_dmat);
810145247Sdamien	}
811145247Sdamien
812145247Sdamien	if (sc->rxbuf_dmat != NULL) {
813145247Sdamien		for (i = 0; i < IPW_NRBD; i++) {
814145247Sdamien			sbuf = &sc->rx_sbuf_list[i];
815145247Sdamien			if (sbuf->m != NULL) {
816145247Sdamien				bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
817145247Sdamien				    BUS_DMASYNC_POSTREAD);
818145247Sdamien				bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
819145247Sdamien				m_freem(sbuf->m);
820145247Sdamien			}
821145247Sdamien			bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
822145247Sdamien		}
823145247Sdamien		bus_dma_tag_destroy(sc->rxbuf_dmat);
824145247Sdamien	}
825145247Sdamien}
826145247Sdamien
827145247Sdamienstatic int
828145247Sdamienipw_shutdown(device_t dev)
829145247Sdamien{
830145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
831145247Sdamien
832145247Sdamien	ipw_stop(sc);
833145247Sdamien
834145247Sdamien	return 0;
835145247Sdamien}
836145247Sdamien
837145247Sdamienstatic int
838145247Sdamienipw_suspend(device_t dev)
839145247Sdamien{
840145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
841145247Sdamien
842145247Sdamien	ipw_stop(sc);
843145247Sdamien
844145247Sdamien	return 0;
845145247Sdamien}
846145247Sdamien
847145247Sdamienstatic int
848145247Sdamienipw_resume(device_t dev)
849145247Sdamien{
850145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
851178354Ssam	struct ifnet *ifp = sc->sc_ifp;
852145247Sdamien
853146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
854146498Sdamien
855178354Ssam	if (ifp->if_flags & IFF_UP)
856178354Ssam		ipw_init(sc);
857145247Sdamien
858145247Sdamien	return 0;
859145247Sdamien}
860145247Sdamien
861145247Sdamienstatic int
862172567Sthompsaipw_cvtrate(int ipwrate)
863172567Sthompsa{
864172567Sthompsa	switch (ipwrate) {
865172567Sthompsa	case IPW_RATE_DS1:	return 2;
866172567Sthompsa	case IPW_RATE_DS2:	return 4;
867172567Sthompsa	case IPW_RATE_DS5:	return 11;
868172567Sthompsa	case IPW_RATE_DS11:	return 22;
869172567Sthompsa	}
870145247Sdamien	return 0;
871145247Sdamien}
872145247Sdamien
873145247Sdamien/*
874156599Sdamien * The firmware automatically adapts the transmit speed. We report its current
875156599Sdamien * value here.
876145247Sdamien */
877145247Sdamienstatic void
878145247Sdamienipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
879145247Sdamien{
880178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
881178354Ssam	struct ieee80211com *ic = vap->iv_ic;
882178354Ssam	struct ipw_softc *sc = ic->ic_ifp->if_softc;
883145247Sdamien
884145247Sdamien	/* read current transmission rate from adapter */
885178354Ssam	vap->iv_bss->ni_txrate = ipw_cvtrate(
886178354Ssam	    ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
887178354Ssam	ieee80211_media_status(ifp, imr);
888145247Sdamien}
889145247Sdamien
890145247Sdamienstatic int
891178354Ssamipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
892145247Sdamien{
893178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
894178354Ssam	struct ieee80211com *ic = vap->iv_ic;
895145247Sdamien	struct ifnet *ifp = ic->ic_ifp;
896145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
897145247Sdamien
898172567Sthompsa	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
899178354Ssam		ieee80211_state_name[vap->iv_state],
900172567Sthompsa		ieee80211_state_name[nstate], sc->flags));
901172567Sthompsa
902145247Sdamien	switch (nstate) {
903145247Sdamien	case IEEE80211_S_RUN:
904172567Sthompsa		if (ic->ic_opmode == IEEE80211_M_IBSS) {
905172567Sthompsa			/*
906172567Sthompsa			 * XXX when joining an ibss network we are called
907172567Sthompsa			 * with a SCAN -> RUN transition on scan complete.
908172567Sthompsa			 * Use that to call ipw_auth_and_assoc.  On completing
909172567Sthompsa			 * the join we are then called again with an
910172567Sthompsa			 * AUTH -> RUN transition and we want to do nothing.
911172567Sthompsa			 * This is all totally bogus and needs to be redone.
912172567Sthompsa			 */
913178354Ssam			if (vap->iv_state == IEEE80211_S_SCAN) {
914178354Ssam				taskqueue_enqueue(taskqueue_swi,
915178354Ssam				    &IPW_VAP(vap)->assoc_task);
916178354Ssam				return EINPROGRESS;
917178354Ssam			}
918172567Sthompsa		}
919172567Sthompsa		break;
920145247Sdamien
921172567Sthompsa	case IEEE80211_S_INIT:
922172567Sthompsa		if (sc->flags & IPW_FLAG_ASSOCIATED)
923178354Ssam			taskqueue_enqueue(taskqueue_swi,
924178354Ssam			    &IPW_VAP(vap)->disassoc_task);
925172567Sthompsa		break;
926145247Sdamien
927172567Sthompsa	case IEEE80211_S_AUTH:
928178354Ssam		taskqueue_enqueue(taskqueue_swi, &IPW_VAP(vap)->assoc_task);
929178354Ssam		return EINPROGRESS;
930145247Sdamien
931172567Sthompsa	case IEEE80211_S_ASSOC:
932172567Sthompsa		/*
933172567Sthompsa		 * If we are not transitioning from AUTH the resend the
934172567Sthompsa		 * association request.
935172567Sthompsa		 */
936178354Ssam		if (vap->iv_state != IEEE80211_S_AUTH) {
937178354Ssam			taskqueue_enqueue(taskqueue_swi,
938178354Ssam			    &IPW_VAP(vap)->assoc_task);
939178354Ssam			return EINPROGRESS;
940178354Ssam		}
941145247Sdamien		break;
942145247Sdamien
943172058Ssam	default:
944145247Sdamien		break;
945145247Sdamien	}
946178354Ssam	return ivp->newstate(vap, nstate, arg);
947145247Sdamien}
948145247Sdamien
949145247Sdamien/*
950145247Sdamien * Read 16 bits at address 'addr' from the serial EEPROM.
951145247Sdamien */
952145247Sdamienstatic uint16_t
953145247Sdamienipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
954145247Sdamien{
955145247Sdamien	uint32_t tmp;
956145247Sdamien	uint16_t val;
957145247Sdamien	int n;
958145247Sdamien
959145247Sdamien	/* clock C once before the first command */
960145247Sdamien	IPW_EEPROM_CTL(sc, 0);
961145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
962145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
963145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
964145247Sdamien
965145247Sdamien	/* write start bit (1) */
966145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
967145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
968145247Sdamien
969145247Sdamien	/* write READ opcode (10) */
970145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
971145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
972145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
973145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
974145247Sdamien
975145247Sdamien	/* write address A7-A0 */
976145247Sdamien	for (n = 7; n >= 0; n--) {
977145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
978145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
979145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
980145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
981145247Sdamien	}
982145247Sdamien
983145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
984145247Sdamien
985145247Sdamien	/* read data Q15-Q0 */
986145247Sdamien	val = 0;
987145247Sdamien	for (n = 15; n >= 0; n--) {
988145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
989145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
990145247Sdamien		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
991145247Sdamien		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
992145247Sdamien	}
993145247Sdamien
994145247Sdamien	IPW_EEPROM_CTL(sc, 0);
995145247Sdamien
996145247Sdamien	/* clear Chip Select and clock C */
997145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
998145247Sdamien	IPW_EEPROM_CTL(sc, 0);
999145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
1000145247Sdamien
1001145247Sdamien	return le16toh(val);
1002145247Sdamien}
1003145247Sdamien
1004145247Sdamienstatic void
1005172567Sthompsaipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1006145247Sdamien{
1007145247Sdamien	struct ipw_cmd *cmd;
1008145247Sdamien
1009145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1010145247Sdamien
1011145247Sdamien	cmd = mtod(sbuf->m, struct ipw_cmd *);
1012145247Sdamien
1013172567Sthompsa	DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
1014172567Sthompsa	    ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
1015145247Sdamien	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
1016145247Sdamien	    le32toh(cmd->status)));
1017145247Sdamien
1018172567Sthompsa	sc->flags &= ~IPW_FLAG_BUSY;
1019145247Sdamien	wakeup(sc);
1020145247Sdamien}
1021145247Sdamien
1022145247Sdamienstatic void
1023178354Ssamipw_assocsuccess(void *arg, int npending)
1024178354Ssam{
1025178354Ssam	struct ieee80211vap *vap = arg;
1026178354Ssam
1027178354Ssam	ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
1028178354Ssam}
1029178354Ssam
1030178354Ssamstatic void
1031178354Ssamipw_assocfailed(void *arg, int npending)
1032178354Ssam{
1033178354Ssam	struct ieee80211vap *vap = arg;
1034178354Ssam
1035178354Ssam	ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1036178354Ssam}
1037178354Ssam
1038178354Ssamstatic void
1039178354Ssamipw_scandone(void *arg, int npending)
1040178354Ssam{
1041178354Ssam	struct ieee80211vap *vap = arg;
1042178354Ssam
1043178354Ssam	ieee80211_scan_done(vap);
1044178354Ssam}
1045178354Ssam
1046178354Ssamstatic void
1047178354Ssamipw_bmiss(void *arg, int npending)
1048178354Ssam{
1049178354Ssam	struct ieee80211com *ic = arg;
1050178354Ssam
1051178354Ssam	ieee80211_beacon_miss(ic);
1052178354Ssam}
1053178354Ssam
1054178354Ssamstatic void
1055172567Sthompsaipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1056145247Sdamien{
1057178354Ssam#define	IEEESTATE(vap)	ieee80211_state_name[vap->iv_state]
1058178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1059178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1060178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1061145247Sdamien	uint32_t state;
1062145247Sdamien
1063145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1064145247Sdamien
1065145247Sdamien	state = le32toh(*mtod(sbuf->m, uint32_t *));
1066145247Sdamien
1067145247Sdamien	switch (state) {
1068145247Sdamien	case IPW_STATE_ASSOCIATED:
1069172567Sthompsa		DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1070178354Ssam			IEEESTATE(vap), sc->flags));
1071178354Ssam		/* XXX suppress state change in case the fw auto-associates */
1072178354Ssam		if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1073178354Ssam			DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1074178354Ssam				IEEESTATE(vap), sc->flags));
1075178354Ssam			break;
1076178354Ssam		}
1077178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATING;
1078172567Sthompsa		sc->flags |= IPW_FLAG_ASSOCIATED;
1079178354Ssam		taskqueue_enqueue(taskqueue_swi,
1080178354Ssam		    &IPW_VAP(vap)->assoc_success_task);
1081145247Sdamien		break;
1082145247Sdamien
1083145247Sdamien	case IPW_STATE_SCANNING:
1084172567Sthompsa		DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1085178354Ssam			IEEESTATE(vap), sc->flags));
1086172567Sthompsa		/*
1087172567Sthompsa		 * NB: Check driver state for association on assoc
1088172567Sthompsa		 * loss as the firmware will immediately start to
1089172567Sthompsa		 * scan and we would treat it as a beacon miss if
1090172567Sthompsa		 * we checked the 802.11 layer state.
1091172567Sthompsa		 */
1092178354Ssam		if (sc->flags & IPW_FLAG_ASSOCIATED) {
1093178354Ssam			/* XXX probably need to issue disassoc to fw */
1094178354Ssam			taskqueue_enqueue(taskqueue_swi, &sc->sc_bmiss_task);
1095178354Ssam		}
1096145247Sdamien		break;
1097145247Sdamien
1098145247Sdamien	case IPW_STATE_SCAN_COMPLETE:
1099172567Sthompsa		/*
1100172567Sthompsa		 * XXX For some reason scan requests generate scan
1101172567Sthompsa		 * started + scan done events before any traffic is
1102172567Sthompsa		 * received (e.g. probe response frames).  We work
1103172567Sthompsa		 * around this by marking the HACK flag and skipping
1104172567Sthompsa		 * the first scan complete event.
1105172567Sthompsa		*/
1106178354Ssam		DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1107178354Ssam			    IEEESTATE(vap), sc->flags));
1108172567Sthompsa		if (sc->flags & IPW_FLAG_HACK) {
1109172567Sthompsa			sc->flags &= ~IPW_FLAG_HACK;
1110172567Sthompsa			break;
1111172567Sthompsa		}
1112172567Sthompsa		if (sc->flags & IPW_FLAG_SCANNING) {
1113178354Ssam			taskqueue_enqueue(taskqueue_swi,
1114178354Ssam			    &IPW_VAP(vap)->scandone_task);
1115172567Sthompsa			sc->flags &= ~IPW_FLAG_SCANNING;
1116172567Sthompsa			sc->sc_scan_timer = 0;
1117172567Sthompsa		}
1118145247Sdamien		break;
1119145247Sdamien
1120145247Sdamien	case IPW_STATE_ASSOCIATION_LOST:
1121172567Sthompsa		DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1122178354Ssam			IEEESTATE(vap), sc->flags));
1123178354Ssam		sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1124178354Ssam		if (vap->iv_state == IEEE80211_S_RUN)
1125178354Ssam			taskqueue_enqueue(taskqueue_swi,
1126178354Ssam			    &IPW_VAP(vap)->assoc_failed_task);
1127145247Sdamien		break;
1128145247Sdamien
1129172567Sthompsa	case IPW_STATE_DISABLED:
1130178354Ssam		/* XXX? is this right? */
1131178354Ssam		sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1132178354Ssam		    IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1133172567Sthompsa		DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1134178354Ssam			IEEESTATE(vap), sc->flags));
1135172567Sthompsa		break;
1136172567Sthompsa
1137145247Sdamien	case IPW_STATE_RADIO_DISABLED:
1138178354Ssam		device_printf(sc->sc_dev, "radio turned off\n");
1139178354Ssam		ieee80211_notify_radio(ic, 0);
1140172567Sthompsa		ipw_stop_locked(sc);
1141178354Ssam		/* XXX start polling thread to detect radio on */
1142145247Sdamien		break;
1143172567Sthompsa
1144172567Sthompsa	default:
1145172567Sthompsa		DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1146178354Ssam			__func__, state, IEEESTATE(vap), sc->flags));
1147172567Sthompsa		break;
1148145247Sdamien	}
1149172567Sthompsa#undef IEEESTATE
1150145247Sdamien}
1151145247Sdamien
1152145247Sdamien/*
1153172567Sthompsa * Set driver state for current channel.
1154172567Sthompsa */
1155172567Sthompsastatic void
1156172567Sthompsaipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1157172567Sthompsa{
1158178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1159178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1160172567Sthompsa
1161172567Sthompsa	ic->ic_curchan = chan;
1162172567Sthompsa	sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
1163172567Sthompsa		htole16(ic->ic_curchan->ic_freq);
1164172567Sthompsa	sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
1165172567Sthompsa		htole16(ic->ic_curchan->ic_flags);
1166172567Sthompsa}
1167172567Sthompsa
1168172567Sthompsa/*
1169145247Sdamien * XXX: Hack to set the current channel to the value advertised in beacons or
1170145247Sdamien * probe responses. Only used during AP detection.
1171145247Sdamien */
1172145247Sdamienstatic void
1173172567Sthompsaipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1174145247Sdamien{
1175178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1176178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1177172567Sthompsa	struct ieee80211_channel *c;
1178145247Sdamien	struct ieee80211_frame *wh;
1179145247Sdamien	uint8_t subtype;
1180145247Sdamien	uint8_t *frm, *efrm;
1181145247Sdamien
1182145247Sdamien	wh = mtod(m, struct ieee80211_frame *);
1183145247Sdamien
1184145247Sdamien	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1185145247Sdamien		return;
1186145247Sdamien
1187145247Sdamien	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1188145247Sdamien
1189145247Sdamien	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1190145247Sdamien	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1191145247Sdamien		return;
1192145247Sdamien
1193178354Ssam	/* XXX use ieee80211_parse_beacon */
1194145247Sdamien	frm = (uint8_t *)(wh + 1);
1195145247Sdamien	efrm = mtod(m, uint8_t *) + m->m_len;
1196145247Sdamien
1197145247Sdamien	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1198145247Sdamien	while (frm < efrm) {
1199145247Sdamien		if (*frm == IEEE80211_ELEMID_DSPARMS)
1200145247Sdamien#if IEEE80211_CHAN_MAX < 255
1201145247Sdamien		if (frm[2] <= IEEE80211_CHAN_MAX)
1202145247Sdamien#endif
1203172567Sthompsa		{
1204172567Sthompsa			DPRINTF(("Fixing channel to %d\n", frm[2]));
1205172567Sthompsa			c = ieee80211_find_channel(ic,
1206170530Ssam				ieee80211_ieee2mhz(frm[2], 0),
1207172567Sthompsa				IEEE80211_CHAN_B);
1208172567Sthompsa			if (c == NULL)
1209172567Sthompsa				c = &ic->ic_channels[0];
1210172567Sthompsa			ipw_setcurchan(sc, c);
1211172567Sthompsa		}
1212145247Sdamien
1213145247Sdamien		frm += frm[1] + 2;
1214145247Sdamien	}
1215145247Sdamien}
1216145247Sdamien
1217145247Sdamienstatic void
1218172567Sthompsaipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1219145247Sdamien    struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1220145247Sdamien{
1221178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1222178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1223152385Sdamien	struct mbuf *mnew, *m;
1224145247Sdamien	struct ieee80211_node *ni;
1225145247Sdamien	bus_addr_t physaddr;
1226145247Sdamien	int error;
1227172567Sthompsa	IPW_LOCK_DECL;
1228145247Sdamien
1229152385Sdamien	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1230152385Sdamien	    status->rssi));
1231152385Sdamien
1232147757Sdamien	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1233147757Sdamien	    le32toh(status->len) > MCLBYTES)
1234147757Sdamien		return;
1235147757Sdamien
1236152385Sdamien	/*
1237152385Sdamien	 * Try to allocate a new mbuf for this ring element and load it before
1238152385Sdamien	 * processing the current mbuf. If the ring element cannot be loaded,
1239152385Sdamien	 * drop the received packet and reuse the old mbuf. In the unlikely
1240152385Sdamien	 * case that the old mbuf can't be reloaded either, explicitly panic.
1241152385Sdamien	 */
1242152385Sdamien	mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1243152385Sdamien	if (mnew == NULL) {
1244152385Sdamien		ifp->if_ierrors++;
1245152385Sdamien		return;
1246152385Sdamien	}
1247152385Sdamien
1248145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1249145247Sdamien	bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1250145247Sdamien
1251152385Sdamien	error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1252152385Sdamien	    MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1253152385Sdamien	if (error != 0) {
1254152385Sdamien		m_freem(mnew);
1255152385Sdamien
1256152385Sdamien		/* try to reload the old mbuf */
1257152385Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1258152385Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1259152385Sdamien		    &physaddr, 0);
1260152385Sdamien		if (error != 0) {
1261152385Sdamien			/* very unlikely that it will fail... */
1262152385Sdamien			panic("%s: could not load old rx mbuf",
1263152385Sdamien			    device_get_name(sc->sc_dev));
1264152385Sdamien		}
1265152385Sdamien		ifp->if_ierrors++;
1266152385Sdamien		return;
1267152385Sdamien	}
1268152385Sdamien
1269152385Sdamien	/*
1270152385Sdamien	 * New mbuf successfully loaded, update Rx ring and continue
1271152385Sdamien	 * processing.
1272152385Sdamien	 */
1273152385Sdamien	m = sbuf->m;
1274152385Sdamien	sbuf->m = mnew;
1275152385Sdamien	sbd->bd->physaddr = htole32(physaddr);
1276152385Sdamien
1277145247Sdamien	/* finalize mbuf */
1278145247Sdamien	m->m_pkthdr.rcvif = ifp;
1279145247Sdamien	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1280145247Sdamien
1281178354Ssam	if (bpf_peers_present(ifp->if_bpf)) {
1282145247Sdamien		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1283145247Sdamien
1284145247Sdamien		tap->wr_flags = 0;
1285172567Sthompsa		tap->wr_antsignal = status->rssi + IPW_RSSI_TO_DBM;
1286156599Sdamien		tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1287156599Sdamien		tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1288145247Sdamien
1289178354Ssam		bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m);
1290145247Sdamien	}
1291145247Sdamien
1292172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
1293172567Sthompsa		ipw_fix_channel(sc, m);
1294145247Sdamien
1295172567Sthompsa	IPW_UNLOCK(sc);
1296178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1297178354Ssam	if (ni != NULL) {
1298178354Ssam		(void) ieee80211_input(ni, m, status->rssi, -95, 0);
1299178354Ssam		ieee80211_free_node(ni);
1300178354Ssam	} else
1301178354Ssam		(void) ieee80211_input_all(ic, m, status->rssi, -95, 0);
1302172567Sthompsa	IPW_LOCK(sc);
1303145247Sdamien
1304145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1305145247Sdamien}
1306145247Sdamien
1307145247Sdamienstatic void
1308145247Sdamienipw_rx_intr(struct ipw_softc *sc)
1309145247Sdamien{
1310145247Sdamien	struct ipw_status *status;
1311145247Sdamien	struct ipw_soft_bd *sbd;
1312145247Sdamien	struct ipw_soft_buf *sbuf;
1313145247Sdamien	uint32_t r, i;
1314145247Sdamien
1315145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1316145247Sdamien		return;
1317145247Sdamien
1318145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1319145247Sdamien
1320145247Sdamien	bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1321145247Sdamien
1322145247Sdamien	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1323145247Sdamien		status = &sc->status_list[i];
1324145247Sdamien		sbd = &sc->srbd_list[i];
1325145247Sdamien		sbuf = sbd->priv;
1326145247Sdamien
1327145247Sdamien		switch (le16toh(status->code) & 0xf) {
1328145247Sdamien		case IPW_STATUS_CODE_COMMAND:
1329172567Sthompsa			ipw_rx_cmd_intr(sc, sbuf);
1330145247Sdamien			break;
1331145247Sdamien
1332145247Sdamien		case IPW_STATUS_CODE_NEWSTATE:
1333172567Sthompsa			ipw_rx_newstate_intr(sc, sbuf);
1334145247Sdamien			break;
1335145247Sdamien
1336145247Sdamien		case IPW_STATUS_CODE_DATA_802_3:
1337145247Sdamien		case IPW_STATUS_CODE_DATA_802_11:
1338172567Sthompsa			ipw_rx_data_intr(sc, status, sbd, sbuf);
1339145247Sdamien			break;
1340145247Sdamien
1341145247Sdamien		case IPW_STATUS_CODE_NOTIFICATION:
1342172567Sthompsa			DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1343172567Sthompsa			    le32toh(status->len), status->flags));
1344178354Ssam			/* XXX maybe drive state machine AUTH->ASSOC? */
1345145247Sdamien			break;
1346145247Sdamien
1347145247Sdamien		default:
1348172567Sthompsa			device_printf(sc->sc_dev, "unexpected status code %u\n",
1349145247Sdamien			    le16toh(status->code));
1350145247Sdamien		}
1351145247Sdamien
1352145247Sdamien		/* firmware was killed, stop processing received frames */
1353145247Sdamien		if (!(sc->flags & IPW_FLAG_FW_INITED))
1354145247Sdamien			return;
1355145247Sdamien
1356145247Sdamien		sbd->bd->flags = 0;
1357145247Sdamien	}
1358145247Sdamien
1359145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1360145247Sdamien
1361145247Sdamien	/* kick the firmware */
1362145247Sdamien	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1363145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1364145247Sdamien}
1365145247Sdamien
1366145247Sdamienstatic void
1367145247Sdamienipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1368145247Sdamien{
1369145247Sdamien	struct ipw_soft_hdr *shdr;
1370145247Sdamien	struct ipw_soft_buf *sbuf;
1371145247Sdamien
1372145247Sdamien	switch (sbd->type) {
1373145247Sdamien	case IPW_SBD_TYPE_COMMAND:
1374145247Sdamien		bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1375145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1376145247Sdamien		bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1377145247Sdamien		break;
1378145247Sdamien
1379145247Sdamien	case IPW_SBD_TYPE_HEADER:
1380145247Sdamien		shdr = sbd->priv;
1381145247Sdamien		bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1382145247Sdamien		bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1383145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1384145247Sdamien		break;
1385145247Sdamien
1386145247Sdamien	case IPW_SBD_TYPE_DATA:
1387145247Sdamien		sbuf = sbd->priv;
1388145247Sdamien		bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1389145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1390145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1391145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1392145247Sdamien
1393170530Ssam		if (sbuf->m->m_flags & M_TXCB)
1394170530Ssam			ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1395145247Sdamien		m_freem(sbuf->m);
1396145247Sdamien		ieee80211_free_node(sbuf->ni);
1397145247Sdamien
1398145247Sdamien		sc->sc_tx_timer = 0;
1399145247Sdamien		break;
1400145247Sdamien	}
1401145247Sdamien
1402145247Sdamien	sbd->type = IPW_SBD_TYPE_NOASSOC;
1403145247Sdamien}
1404145247Sdamien
1405145247Sdamienstatic void
1406145247Sdamienipw_tx_intr(struct ipw_softc *sc)
1407145247Sdamien{
1408178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1409145247Sdamien	struct ipw_soft_bd *sbd;
1410145247Sdamien	uint32_t r, i;
1411145247Sdamien
1412145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1413145247Sdamien		return;
1414145247Sdamien
1415145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1416145247Sdamien
1417145247Sdamien	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1418145247Sdamien		sbd = &sc->stbd_list[i];
1419145247Sdamien
1420145247Sdamien		if (sbd->type == IPW_SBD_TYPE_DATA)
1421145247Sdamien			ifp->if_opackets++;
1422145247Sdamien
1423145247Sdamien		ipw_release_sbd(sc, sbd);
1424145247Sdamien		sc->txfree++;
1425145247Sdamien	}
1426145247Sdamien
1427145247Sdamien	/* remember what the firmware has processed */
1428145247Sdamien	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1429145247Sdamien
1430148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1431172567Sthompsa	ipw_start_locked(ifp);
1432145247Sdamien}
1433145247Sdamien
1434145247Sdamienstatic void
1435145247Sdamienipw_intr(void *arg)
1436145247Sdamien{
1437145247Sdamien	struct ipw_softc *sc = arg;
1438145247Sdamien	uint32_t r;
1439172567Sthompsa	IPW_LOCK_DECL;
1440145247Sdamien
1441172567Sthompsa	IPW_LOCK(sc);
1442145247Sdamien
1443145247Sdamien	if ((r = CSR_READ_4(sc, IPW_CSR_INTR)) == 0 || r == 0xffffffff) {
1444172567Sthompsa		IPW_UNLOCK(sc);
1445145247Sdamien		return;
1446145247Sdamien	}
1447145247Sdamien
1448145247Sdamien	/* disable interrupts */
1449145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1450145247Sdamien
1451156599Sdamien	/* acknowledge all interrupts */
1452156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1453156599Sdamien
1454145247Sdamien	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1455156599Sdamien		device_printf(sc->sc_dev, "firmware error\n");
1456178354Ssam		taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1457156599Sdamien		r = 0;	/* don't process more interrupts */
1458145247Sdamien	}
1459145247Sdamien
1460156599Sdamien	if (r & IPW_INTR_FW_INIT_DONE)
1461156599Sdamien		wakeup(sc);
1462145247Sdamien
1463145247Sdamien	if (r & IPW_INTR_RX_TRANSFER)
1464145247Sdamien		ipw_rx_intr(sc);
1465145247Sdamien
1466145247Sdamien	if (r & IPW_INTR_TX_TRANSFER)
1467145247Sdamien		ipw_tx_intr(sc);
1468145247Sdamien
1469145247Sdamien	/* re-enable interrupts */
1470145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1471145247Sdamien
1472172567Sthompsa	IPW_UNLOCK(sc);
1473145247Sdamien}
1474145247Sdamien
1475145247Sdamienstatic void
1476145247Sdamienipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1477145247Sdamien{
1478145247Sdamien	if (error != 0)
1479145247Sdamien		return;
1480145247Sdamien
1481145247Sdamien	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1482145247Sdamien
1483145247Sdamien	*(bus_addr_t *)arg = segs[0].ds_addr;
1484145247Sdamien}
1485145247Sdamien
1486172567Sthompsastatic const char *
1487172567Sthompsaipw_cmdname(int cmd)
1488172567Sthompsa{
1489172567Sthompsa#define	N(a)	(sizeof(a) / sizeof(a[0]))
1490172567Sthompsa	static const struct {
1491172567Sthompsa		int	cmd;
1492172567Sthompsa		const char *name;
1493172567Sthompsa	} cmds[] = {
1494172567Sthompsa		{ IPW_CMD_ADD_MULTICAST,	"ADD_MULTICAST" },
1495172567Sthompsa		{ IPW_CMD_BROADCAST_SCAN,	"BROADCAST_SCAN" },
1496172567Sthompsa		{ IPW_CMD_DISABLE,		"DISABLE" },
1497172567Sthompsa		{ IPW_CMD_DISABLE_PHY,		"DISABLE_PHY" },
1498172567Sthompsa		{ IPW_CMD_ENABLE,		"ENABLE" },
1499172567Sthompsa		{ IPW_CMD_PREPARE_POWER_DOWN,	"PREPARE_POWER_DOWN" },
1500172567Sthompsa		{ IPW_CMD_SET_BASIC_TX_RATES,	"SET_BASIC_TX_RATES" },
1501172567Sthompsa		{ IPW_CMD_SET_BEACON_INTERVAL,	"SET_BEACON_INTERVAL" },
1502172567Sthompsa		{ IPW_CMD_SET_CHANNEL,		"SET_CHANNEL" },
1503172567Sthompsa		{ IPW_CMD_SET_CONFIGURATION,	"SET_CONFIGURATION" },
1504172567Sthompsa		{ IPW_CMD_SET_DESIRED_BSSID,	"SET_DESIRED_BSSID" },
1505172567Sthompsa		{ IPW_CMD_SET_ESSID,		"SET_ESSID" },
1506172567Sthompsa		{ IPW_CMD_SET_FRAG_THRESHOLD,	"SET_FRAG_THRESHOLD" },
1507172567Sthompsa		{ IPW_CMD_SET_MAC_ADDRESS,	"SET_MAC_ADDRESS" },
1508172567Sthompsa		{ IPW_CMD_SET_MANDATORY_BSSID,	"SET_MANDATORY_BSSID" },
1509172567Sthompsa		{ IPW_CMD_SET_MODE,		"SET_MODE" },
1510172567Sthompsa		{ IPW_CMD_SET_MSDU_TX_RATES,	"SET_MSDU_TX_RATES" },
1511172567Sthompsa		{ IPW_CMD_SET_POWER_MODE,	"SET_POWER_MODE" },
1512172567Sthompsa		{ IPW_CMD_SET_RTS_THRESHOLD,	"SET_RTS_THRESHOLD" },
1513172567Sthompsa		{ IPW_CMD_SET_SCAN_OPTIONS,	"SET_SCAN_OPTIONS" },
1514172567Sthompsa		{ IPW_CMD_SET_SECURITY_INFO,	"SET_SECURITY_INFO" },
1515172567Sthompsa		{ IPW_CMD_SET_TX_POWER_INDEX,	"SET_TX_POWER_INDEX" },
1516172567Sthompsa		{ IPW_CMD_SET_TX_RATES,		"SET_TX_RATES" },
1517172567Sthompsa		{ IPW_CMD_SET_WEP_FLAGS,	"SET_WEP_FLAGS" },
1518172567Sthompsa		{ IPW_CMD_SET_WEP_KEY,		"SET_WEP_KEY" },
1519172567Sthompsa		{ IPW_CMD_SET_WEP_KEY_INDEX,	"SET_WEP_KEY_INDEX" },
1520172567Sthompsa		{ IPW_CMD_SET_WPA_IE,		"SET_WPA_IE" },
1521172567Sthompsa
1522172567Sthompsa	};
1523172567Sthompsa	static char buf[12];
1524172567Sthompsa	int i;
1525172567Sthompsa
1526172567Sthompsa	for (i = 0; i < N(cmds); i++)
1527172567Sthompsa		if (cmds[i].cmd == cmd)
1528172567Sthompsa			return cmds[i].name;
1529172567Sthompsa	snprintf(buf, sizeof(buf), "%u", cmd);
1530172567Sthompsa	return buf;
1531172567Sthompsa#undef N
1532172567Sthompsa}
1533172567Sthompsa
1534145247Sdamien/*
1535145247Sdamien * Send a command to the firmware and wait for the acknowledgement.
1536145247Sdamien */
1537145247Sdamienstatic int
1538145247Sdamienipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1539145247Sdamien{
1540145247Sdamien	struct ipw_soft_bd *sbd;
1541145247Sdamien	bus_addr_t physaddr;
1542145247Sdamien	int error;
1543145247Sdamien
1544178354Ssam	IPW_LOCK_ASSERT(sc);
1545178354Ssam
1546172567Sthompsa	if (sc->flags & IPW_FLAG_BUSY) {
1547172567Sthompsa		device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1548172567Sthompsa			__func__, ipw_cmdname(type));
1549172567Sthompsa		return EAGAIN;
1550172567Sthompsa	}
1551172567Sthompsa	sc->flags |= IPW_FLAG_BUSY;
1552172567Sthompsa
1553145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1554145247Sdamien
1555145247Sdamien	error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1556145247Sdamien	    sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1557145247Sdamien	if (error != 0) {
1558145247Sdamien		device_printf(sc->sc_dev, "could not map command DMA memory\n");
1559172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1560145247Sdamien		return error;
1561145247Sdamien	}
1562145247Sdamien
1563145247Sdamien	sc->cmd.type = htole32(type);
1564145247Sdamien	sc->cmd.subtype = 0;
1565145247Sdamien	sc->cmd.len = htole32(len);
1566145247Sdamien	sc->cmd.seq = 0;
1567152637Sdamien	memcpy(sc->cmd.data, data, len);
1568145247Sdamien
1569145247Sdamien	sbd->type = IPW_SBD_TYPE_COMMAND;
1570145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1571145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1572145247Sdamien	sbd->bd->nfrag = 1;
1573145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1574145247Sdamien	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1575145247Sdamien
1576145247Sdamien	bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1577145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1578145247Sdamien
1579172567Sthompsa#ifdef IPW_DEBUG
1580172567Sthompsa	if (ipw_debug >= 4) {
1581172567Sthompsa		printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1582172567Sthompsa		    0, 0, len);
1583172567Sthompsa		/* Print the data buffer in the higher debug level */
1584172567Sthompsa		if (ipw_debug >= 9 && len > 0) {
1585172567Sthompsa			printf(" data: 0x");
1586172567Sthompsa			for (int i = 1; i <= len; i++)
1587174269Swkoszek				printf("%1D", (u_char *)data + len - i, "");
1588172567Sthompsa		}
1589172567Sthompsa		printf("\n");
1590172567Sthompsa	}
1591172567Sthompsa#endif
1592145247Sdamien
1593145247Sdamien	/* kick firmware */
1594145247Sdamien	sc->txfree--;
1595145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1596145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1597145247Sdamien
1598145247Sdamien	/* wait at most one second for command to complete */
1599172567Sthompsa	error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1600172567Sthompsa	if (error != 0) {
1601172567Sthompsa		device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1602172567Sthompsa		    __func__, ipw_cmdname(type), error);
1603172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1604172567Sthompsa		return (error);
1605172567Sthompsa	}
1606172567Sthompsa	return (0);
1607145247Sdamien}
1608145247Sdamien
1609145247Sdamienstatic int
1610145247Sdamienipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1611145247Sdamien{
1612145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1613178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1614145247Sdamien	struct ieee80211_frame *wh;
1615145247Sdamien	struct ipw_soft_bd *sbd;
1616145247Sdamien	struct ipw_soft_hdr *shdr;
1617145247Sdamien	struct ipw_soft_buf *sbuf;
1618145247Sdamien	struct ieee80211_key *k;
1619145247Sdamien	struct mbuf *mnew;
1620145247Sdamien	bus_dma_segment_t segs[IPW_MAX_NSEG];
1621145247Sdamien	bus_addr_t physaddr;
1622145247Sdamien	int nsegs, error, i;
1623145247Sdamien
1624145247Sdamien	wh = mtod(m0, struct ieee80211_frame *);
1625145247Sdamien
1626145247Sdamien	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1627178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1628147806Ssam		if (k == NULL) {
1629147806Ssam			m_freem(m0);
1630145247Sdamien			return ENOBUFS;
1631147806Ssam		}
1632145247Sdamien		/* packet header may have moved, reset our local pointer */
1633145247Sdamien		wh = mtod(m0, struct ieee80211_frame *);
1634145247Sdamien	}
1635145247Sdamien
1636178354Ssam	if (bpf_peers_present(ifp->if_bpf)) {
1637145247Sdamien		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1638145247Sdamien
1639145247Sdamien		tap->wt_flags = 0;
1640156599Sdamien		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1641156599Sdamien		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1642145247Sdamien
1643178354Ssam		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1644145247Sdamien	}
1645145247Sdamien
1646145247Sdamien	shdr = SLIST_FIRST(&sc->free_shdr);
1647145247Sdamien	sbuf = SLIST_FIRST(&sc->free_sbuf);
1648145247Sdamien	KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1649145247Sdamien
1650145247Sdamien	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1651145247Sdamien	shdr->hdr.subtype = 0;
1652145247Sdamien	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1653145247Sdamien	shdr->hdr.encrypt = 0;
1654145247Sdamien	shdr->hdr.keyidx = 0;
1655145247Sdamien	shdr->hdr.keysz = 0;
1656145247Sdamien	shdr->hdr.fragmentsz = 0;
1657145247Sdamien	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1658145247Sdamien	if (ic->ic_opmode == IEEE80211_M_STA)
1659145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1660145247Sdamien	else
1661145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1662145247Sdamien
1663145247Sdamien	/* trim IEEE802.11 header */
1664145247Sdamien	m_adj(m0, sizeof (struct ieee80211_frame));
1665145247Sdamien
1666145247Sdamien	error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1667145247Sdamien	    &nsegs, 0);
1668145247Sdamien	if (error != 0 && error != EFBIG) {
1669145247Sdamien		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1670145247Sdamien		    error);
1671145247Sdamien		m_freem(m0);
1672145247Sdamien		return error;
1673145247Sdamien	}
1674145247Sdamien	if (error != 0) {
1675145247Sdamien		mnew = m_defrag(m0, M_DONTWAIT);
1676145247Sdamien		if (mnew == NULL) {
1677145247Sdamien			device_printf(sc->sc_dev,
1678145247Sdamien			    "could not defragment mbuf\n");
1679145247Sdamien			m_freem(m0);
1680145247Sdamien			return ENOBUFS;
1681145247Sdamien		}
1682145247Sdamien		m0 = mnew;
1683145247Sdamien
1684145247Sdamien		error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1685145247Sdamien		    segs, &nsegs, 0);
1686145247Sdamien		if (error != 0) {
1687145247Sdamien			device_printf(sc->sc_dev,
1688145247Sdamien			    "could not map mbuf (error %d)\n", error);
1689145247Sdamien			m_freem(m0);
1690145247Sdamien			return error;
1691145247Sdamien		}
1692145247Sdamien	}
1693145247Sdamien
1694145247Sdamien	error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1695145247Sdamien	    sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1696145247Sdamien	if (error != 0) {
1697145247Sdamien		device_printf(sc->sc_dev, "could not map header DMA memory\n");
1698145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1699145247Sdamien		m_freem(m0);
1700145247Sdamien		return error;
1701145247Sdamien	}
1702145247Sdamien
1703145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1704145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1705145247Sdamien
1706145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1707145247Sdamien	sbd->type = IPW_SBD_TYPE_HEADER;
1708145247Sdamien	sbd->priv = shdr;
1709145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1710145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1711145247Sdamien	sbd->bd->nfrag = 1 + nsegs;
1712145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1713145247Sdamien	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1714145247Sdamien
1715145247Sdamien	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1716145247Sdamien	    shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1717145247Sdamien	    shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1718145247Sdamien	    ":"));
1719145247Sdamien
1720145247Sdamien	sc->txfree--;
1721145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1722145247Sdamien
1723145247Sdamien	sbuf->m = m0;
1724145247Sdamien	sbuf->ni = ni;
1725145247Sdamien
1726145247Sdamien	for (i = 0; i < nsegs; i++) {
1727145247Sdamien		sbd = &sc->stbd_list[sc->txcur];
1728145247Sdamien
1729145247Sdamien		sbd->bd->physaddr = htole32(segs[i].ds_addr);
1730145247Sdamien		sbd->bd->len = htole32(segs[i].ds_len);
1731145247Sdamien		sbd->bd->nfrag = 0;
1732145247Sdamien		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1733145247Sdamien		if (i == nsegs - 1) {
1734145247Sdamien			sbd->type = IPW_SBD_TYPE_DATA;
1735145247Sdamien			sbd->priv = sbuf;
1736145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1737145247Sdamien		} else {
1738145247Sdamien			sbd->type = IPW_SBD_TYPE_NOASSOC;
1739145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1740145247Sdamien		}
1741145247Sdamien
1742172574Sthompsa		DPRINTFN(5, ("sending fragment (%d)\n", i));
1743145247Sdamien
1744145247Sdamien		sc->txfree--;
1745145247Sdamien		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1746145247Sdamien	}
1747145247Sdamien
1748145247Sdamien	bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1749145247Sdamien	bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1750145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1751145247Sdamien
1752145247Sdamien	/* kick firmware */
1753145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1754145247Sdamien
1755145247Sdamien	return 0;
1756145247Sdamien}
1757145247Sdamien
1758178354Ssamstatic int
1759178354Ssamipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1760178354Ssam	const struct ieee80211_bpf_params *params)
1761178354Ssam{
1762178354Ssam	/* no support; just discard */
1763178354Ssam	m_freem(m);
1764178354Ssam	ieee80211_free_node(ni);
1765178354Ssam	return 0;
1766178354Ssam}
1767178354Ssam
1768145247Sdamienstatic void
1769145247Sdamienipw_start(struct ifnet *ifp)
1770145247Sdamien{
1771145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1772172567Sthompsa	IPW_LOCK_DECL;
1773172567Sthompsa
1774172567Sthompsa	IPW_LOCK(sc);
1775172567Sthompsa	ipw_start_locked(ifp);
1776172567Sthompsa	IPW_UNLOCK(sc);
1777172567Sthompsa}
1778172567Sthompsa
1779172567Sthompsastatic void
1780172567Sthompsaipw_start_locked(struct ifnet *ifp)
1781172567Sthompsa{
1782172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
1783145247Sdamien	struct ieee80211_node *ni;
1784178354Ssam	struct mbuf *m;
1785145247Sdamien
1786172567Sthompsa	IPW_LOCK_ASSERT(sc);
1787145247Sdamien
1788145247Sdamien	for (;;) {
1789178354Ssam		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1790178354Ssam		if (m == NULL)
1791145247Sdamien			break;
1792145247Sdamien		if (sc->txfree < 1 + IPW_MAX_NSEG) {
1793178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1794148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1795145247Sdamien			break;
1796145247Sdamien		}
1797178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1798178354Ssam		m = ieee80211_encap(ni, m);
1799178354Ssam		if (m == NULL) {
1800145247Sdamien			ieee80211_free_node(ni);
1801145247Sdamien			continue;
1802145247Sdamien		}
1803178354Ssam		if (ipw_tx_start(ifp, m, ni) != 0) {
1804145247Sdamien			ieee80211_free_node(ni);
1805145247Sdamien			ifp->if_oerrors++;
1806145247Sdamien			break;
1807145247Sdamien		}
1808145247Sdamien		/* start watchdog timer */
1809145247Sdamien		sc->sc_tx_timer = 5;
1810145247Sdamien	}
1811145247Sdamien}
1812145247Sdamien
1813145247Sdamienstatic void
1814172567Sthompsaipw_watchdog(void *arg)
1815145247Sdamien{
1816172567Sthompsa	struct ipw_softc *sc = arg;
1817172567Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1818178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1819145247Sdamien
1820172567Sthompsa	IPW_LOCK_ASSERT(sc);
1821156599Sdamien
1822145247Sdamien	if (sc->sc_tx_timer > 0) {
1823145247Sdamien		if (--sc->sc_tx_timer == 0) {
1824145247Sdamien			if_printf(ifp, "device timeout\n");
1825145247Sdamien			ifp->if_oerrors++;
1826178354Ssam			taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1827145247Sdamien		}
1828145247Sdamien	}
1829172567Sthompsa	if (sc->sc_scan_timer > 0) {
1830172567Sthompsa		if (--sc->sc_scan_timer == 0) {
1831172567Sthompsa			DPRINTFN(3, ("Scan timeout\n"));
1832172567Sthompsa			/* End the scan */
1833172567Sthompsa			if (sc->flags & IPW_FLAG_SCANNING) {
1834178354Ssam				ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1835172567Sthompsa				sc->flags &= ~IPW_FLAG_SCANNING;
1836172567Sthompsa			}
1837172567Sthompsa		}
1838172567Sthompsa	}
1839172567Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1840172567Sthompsa		callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1841145247Sdamien}
1842145247Sdamien
1843145247Sdamienstatic int
1844145247Sdamienipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1845145247Sdamien{
1846145247Sdamien	struct ipw_softc *sc = ifp->if_softc;
1847178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1848178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
1849178354Ssam	int error = 0, startall = 0;
1850172567Sthompsa	IPW_LOCK_DECL;
1851145247Sdamien
1852145247Sdamien	switch (cmd) {
1853145247Sdamien	case SIOCSIFFLAGS:
1854178704Sthompsa		IPW_LOCK(sc);
1855145247Sdamien		if (ifp->if_flags & IFF_UP) {
1856178354Ssam			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1857178354Ssam				ipw_init_locked(sc);
1858178354Ssam				startall = 1;
1859178354Ssam			}
1860145247Sdamien		} else {
1861148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1862172567Sthompsa				ipw_stop_locked(sc);
1863145247Sdamien		}
1864178704Sthompsa		IPW_UNLOCK(sc);
1865178704Sthompsa		if (startall)
1866178704Sthompsa			ieee80211_start_all(ic);
1867145247Sdamien		break;
1868178354Ssam	case SIOCGIFMEDIA:
1869178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1870178354Ssam		break;
1871178704Sthompsa	case SIOCGIFADDR:
1872178704Sthompsa		error = ether_ioctl(ifp, cmd, data);
1873178704Sthompsa		break;
1874145247Sdamien	default:
1875178704Sthompsa		error = EINVAL;
1876178704Sthompsa		break;
1877145247Sdamien	}
1878145247Sdamien	return error;
1879145247Sdamien}
1880145247Sdamien
1881145247Sdamienstatic void
1882145247Sdamienipw_stop_master(struct ipw_softc *sc)
1883145247Sdamien{
1884156599Sdamien	uint32_t tmp;
1885145247Sdamien	int ntries;
1886145247Sdamien
1887145247Sdamien	/* disable interrupts */
1888145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1889145247Sdamien
1890145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1891145247Sdamien	for (ntries = 0; ntries < 50; ntries++) {
1892145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1893145247Sdamien			break;
1894145247Sdamien		DELAY(10);
1895145247Sdamien	}
1896145247Sdamien	if (ntries == 50)
1897145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for master\n");
1898145247Sdamien
1899156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1900156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1901145247Sdamien
1902172567Sthompsa	/* Clear all flags except the following */
1903172567Sthompsa	sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1904145247Sdamien}
1905145247Sdamien
1906145247Sdamienstatic int
1907145247Sdamienipw_reset(struct ipw_softc *sc)
1908145247Sdamien{
1909156599Sdamien	uint32_t tmp;
1910145247Sdamien	int ntries;
1911145247Sdamien
1912145247Sdamien	ipw_stop_master(sc);
1913145247Sdamien
1914145247Sdamien	/* move adapter to D0 state */
1915156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1916156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1917145247Sdamien
1918145247Sdamien	/* wait for clock stabilization */
1919145247Sdamien	for (ntries = 0; ntries < 1000; ntries++) {
1920145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1921145247Sdamien			break;
1922145247Sdamien		DELAY(200);
1923145247Sdamien	}
1924145247Sdamien	if (ntries == 1000)
1925145247Sdamien		return EIO;
1926145247Sdamien
1927156599Sdamien	tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1928156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1929145247Sdamien
1930145247Sdamien	DELAY(10);
1931145247Sdamien
1932156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1933156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1934145247Sdamien
1935145247Sdamien	return 0;
1936145247Sdamien}
1937145247Sdamien
1938172567Sthompsastatic int
1939172567Sthompsaipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1940172567Sthompsa{
1941172567Sthompsa	int ms = hz < 1000 ? 1 : hz/10;
1942172567Sthompsa	int i, error;
1943172567Sthompsa
1944172567Sthompsa	for (i = 0; i < 100; i++) {
1945172567Sthompsa		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1946172567Sthompsa			return 0;
1947172567Sthompsa		error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1948172567Sthompsa		if (error == 0 || error != EWOULDBLOCK)
1949172567Sthompsa			return 0;
1950172567Sthompsa	}
1951172567Sthompsa	DPRINTF(("%s: timeout waiting for %s\n",
1952172567Sthompsa		__func__, waitfor ? "disable" : "enable"));
1953172567Sthompsa	return ETIMEDOUT;
1954172567Sthompsa}
1955172567Sthompsa
1956172567Sthompsastatic int
1957172567Sthompsaipw_enable(struct ipw_softc *sc)
1958172567Sthompsa{
1959172567Sthompsa	int error;
1960172567Sthompsa
1961172567Sthompsa	if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1962172567Sthompsa		DPRINTF(("Enable adapter\n"));
1963172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1964172567Sthompsa		if (error != 0)
1965172567Sthompsa			return error;
1966172567Sthompsa		error = ipw_waitfordisable(sc, 0);
1967172567Sthompsa		if (error != 0)
1968172567Sthompsa			return error;
1969172567Sthompsa		sc->flags |= IPW_FLAG_ENABLED;
1970172567Sthompsa	}
1971172567Sthompsa	return 0;
1972172567Sthompsa}
1973172567Sthompsa
1974172567Sthompsastatic int
1975172567Sthompsaipw_disable(struct ipw_softc *sc)
1976172567Sthompsa{
1977172567Sthompsa	int error;
1978172567Sthompsa
1979172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
1980172567Sthompsa		DPRINTF(("Disable adapter\n"));
1981172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1982172567Sthompsa		if (error != 0)
1983172567Sthompsa			return error;
1984172567Sthompsa		error = ipw_waitfordisable(sc, 1);
1985172567Sthompsa		if (error != 0)
1986172567Sthompsa			return error;
1987172567Sthompsa		sc->flags &= ~IPW_FLAG_ENABLED;
1988172567Sthompsa	}
1989172567Sthompsa	return 0;
1990172567Sthompsa}
1991172567Sthompsa
1992145247Sdamien/*
1993145247Sdamien * Upload the microcode to the device.
1994145247Sdamien */
1995145247Sdamienstatic int
1996156599Sdamienipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1997145247Sdamien{
1998145247Sdamien	int ntries;
1999145247Sdamien
2000145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2001145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
2002145247Sdamien
2003145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
2004145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
2005145247Sdamien
2006145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
2007145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
2008145247Sdamien
2009145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
2010145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
2011145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
2012145247Sdamien
2013145247Sdamien	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
2014145247Sdamien
2015145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
2016145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
2017145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
2018145247Sdamien
2019145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
2020145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
2021145247Sdamien
2022145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
2023145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
2024145247Sdamien
2025145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
2026145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
2027145247Sdamien
2028145247Sdamien	for (ntries = 0; ntries < 10; ntries++) {
2029145247Sdamien		if (MEM_READ_1(sc, 0x210000) & 1)
2030145247Sdamien			break;
2031145247Sdamien		DELAY(10);
2032145247Sdamien	}
2033145247Sdamien	if (ntries == 10) {
2034145247Sdamien		device_printf(sc->sc_dev,
2035145247Sdamien		    "timeout waiting for ucode to initialize\n");
2036145247Sdamien		return EIO;
2037145247Sdamien	}
2038145247Sdamien
2039145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0);
2040145247Sdamien
2041145247Sdamien	return 0;
2042145247Sdamien}
2043145247Sdamien
2044145247Sdamien/* set of macros to handle unaligned little endian data in firmware image */
2045145247Sdamien#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2046145247Sdamien#define GETLE16(p) ((p)[0] | (p)[1] << 8)
2047145247Sdamienstatic int
2048156599Sdamienipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
2049145247Sdamien{
2050156599Sdamien	const uint8_t *p, *end;
2051156599Sdamien	uint32_t tmp, dst;
2052145247Sdamien	uint16_t len;
2053145247Sdamien	int error;
2054145247Sdamien
2055145247Sdamien	p = fw;
2056145247Sdamien	end = fw + size;
2057145247Sdamien	while (p < end) {
2058145247Sdamien		dst = GETLE32(p); p += 4;
2059145247Sdamien		len = GETLE16(p); p += 2;
2060145247Sdamien
2061145247Sdamien		ipw_write_mem_1(sc, dst, p, len);
2062145247Sdamien		p += len;
2063145247Sdamien	}
2064145247Sdamien
2065145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
2066145247Sdamien	    IPW_IO_LED_OFF);
2067145247Sdamien
2068145247Sdamien	/* enable interrupts */
2069145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
2070145247Sdamien
2071145247Sdamien	/* kick the firmware */
2072145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
2073145247Sdamien
2074156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
2075156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
2076145247Sdamien
2077145247Sdamien	/* wait at most one second for firmware initialization to complete */
2078145247Sdamien	if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
2079145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for firmware "
2080145247Sdamien		    "initialization to complete\n");
2081145247Sdamien		return error;
2082145247Sdamien	}
2083145247Sdamien
2084156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_IO);
2085156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2086156599Sdamien	    IPW_IO_GPIO3_MASK);
2087145247Sdamien
2088145247Sdamien	return 0;
2089145247Sdamien}
2090145247Sdamien
2091145247Sdamienstatic int
2092172567Sthompsaipw_setwepkeys(struct ipw_softc *sc)
2093172567Sthompsa{
2094178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2095178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2096178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2097172567Sthompsa	struct ipw_wep_key wepkey;
2098172567Sthompsa	struct ieee80211_key *wk;
2099172567Sthompsa	int error, i;
2100172567Sthompsa
2101172567Sthompsa	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2102178354Ssam		wk = &vap->iv_nw_keys[i];
2103172567Sthompsa
2104172567Sthompsa		if (wk->wk_cipher == NULL ||
2105172567Sthompsa		    wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2106172567Sthompsa			continue;
2107172567Sthompsa
2108172567Sthompsa		wepkey.idx = i;
2109172567Sthompsa		wepkey.len = wk->wk_keylen;
2110172567Sthompsa		memset(wepkey.key, 0, sizeof wepkey.key);
2111172567Sthompsa		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2112172567Sthompsa		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2113172567Sthompsa		    wepkey.len));
2114172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2115172567Sthompsa		    sizeof wepkey);
2116172567Sthompsa		if (error != 0)
2117172567Sthompsa			return error;
2118172567Sthompsa	}
2119172567Sthompsa	return 0;
2120172567Sthompsa}
2121172567Sthompsa
2122172567Sthompsastatic int
2123172567Sthompsaipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2124172567Sthompsa{
2125172567Sthompsa	struct ipw_wpa_ie wpaie;
2126172567Sthompsa
2127172567Sthompsa	memset(&wpaie, 0, sizeof(wpaie));
2128172567Sthompsa	wpaie.len = htole32(ielen);
2129172567Sthompsa	/* XXX verify length */
2130172567Sthompsa	memcpy(&wpaie.ie, ie, ielen);
2131172567Sthompsa	DPRINTF(("Setting WPA IE\n"));
2132172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2133172567Sthompsa}
2134172567Sthompsa
2135172567Sthompsastatic int
2136172567Sthompsaipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2137172567Sthompsa{
2138172567Sthompsa	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2139172567Sthompsa
2140172567Sthompsa	if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2141172567Sthompsa		DPRINTF(("Setting mandatory BSSID to null\n"));
2142172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2143172567Sthompsa	} else {
2144172567Sthompsa		DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2145172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2146172567Sthompsa			bssid, IEEE80211_ADDR_LEN);
2147172567Sthompsa	}
2148172567Sthompsa}
2149172567Sthompsa
2150172567Sthompsastatic int
2151172567Sthompsaipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2152172567Sthompsa{
2153172567Sthompsa	if (ssidlen == 0) {
2154172567Sthompsa		/*
2155172567Sthompsa		 * A bug in the firmware breaks the ``don't associate''
2156172567Sthompsa		 * bit in the scan options command.  To compensate for
2157172567Sthompsa		 * this install a bogus ssid when no ssid is specified
2158172567Sthompsa		 * so the firmware won't try to associate.
2159172567Sthompsa		 */
2160172567Sthompsa		DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2161172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2162172567Sthompsa			"\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2163172567Sthompsa			"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2164172567Sthompsa			"\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2165172567Sthompsa			"\x3c\x3d", IEEE80211_NWID_LEN);
2166172567Sthompsa	} else {
2167172567Sthompsa#ifdef IPW_DEBUG
2168172567Sthompsa		if (ipw_debug > 0) {
2169172567Sthompsa			printf("Setting ESSID to ");
2170172567Sthompsa			ieee80211_print_essid(ssid, ssidlen);
2171172567Sthompsa			printf("\n");
2172172567Sthompsa		}
2173172567Sthompsa#endif
2174172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2175172567Sthompsa	}
2176172567Sthompsa}
2177172567Sthompsa
2178172567Sthompsastatic int
2179172567Sthompsaipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2180172567Sthompsa{
2181172567Sthompsa	struct ipw_scan_options opts;
2182172567Sthompsa
2183172567Sthompsa	DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2184172567Sthompsa	opts.channels = htole32(chanmask);
2185172567Sthompsa	opts.flags = htole32(flags);
2186172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2187172567Sthompsa}
2188172567Sthompsa
2189172567Sthompsa/*
2190172567Sthompsa * Handler for sc_scan_task.  This is a simple wrapper around ipw_scan().
2191172567Sthompsa */
2192172567Sthompsastatic void
2193172567Sthompsaipw_scan_task(void *context, int pending)
2194172567Sthompsa{
2195172567Sthompsa	struct ipw_softc *sc = context;
2196172567Sthompsa	IPW_LOCK_DECL;
2197172567Sthompsa
2198172567Sthompsa	IPW_LOCK(sc);
2199172567Sthompsa	ipw_scan(sc);
2200172567Sthompsa	IPW_UNLOCK(sc);
2201172567Sthompsa}
2202172567Sthompsa
2203172567Sthompsastatic int
2204172567Sthompsaipw_scan(struct ipw_softc *sc)
2205172567Sthompsa{
2206172567Sthompsa	uint32_t params;
2207172567Sthompsa	int error;
2208172567Sthompsa
2209172567Sthompsa	DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2210172567Sthompsa
2211172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
2212172567Sthompsa		return (EBUSY);
2213172567Sthompsa	sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2214172567Sthompsa
2215172567Sthompsa	/* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2216172567Sthompsa	error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2217172567Sthompsa	if (error != 0)
2218172567Sthompsa		goto done;
2219172567Sthompsa
2220172567Sthompsa	/*
2221172567Sthompsa	 * Setup null/bogus ssid so firmware doesn't use any previous
2222172567Sthompsa	 * ssid to try and associate.  This is because the ``don't
2223172567Sthompsa	 * associate'' option bit is broken (sigh).
2224172567Sthompsa	 */
2225172567Sthompsa	error = ipw_setssid(sc, NULL, 0);
2226172567Sthompsa	if (error != 0)
2227172567Sthompsa		goto done;
2228172567Sthompsa
2229172567Sthompsa	/*
2230172567Sthompsa	 * NB: the adapter may be disabled on association lost;
2231172567Sthompsa	 *     if so just re-enable it to kick off scanning.
2232172567Sthompsa	 */
2233172567Sthompsa	DPRINTF(("Starting scan\n"));
2234172567Sthompsa	sc->sc_scan_timer = 3;
2235172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
2236172567Sthompsa		params = 0;				/* XXX? */
2237172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2238172567Sthompsa				&params, sizeof(params));
2239172567Sthompsa	} else
2240172567Sthompsa		error = ipw_enable(sc);
2241172567Sthompsadone:
2242172567Sthompsa	if (error != 0) {
2243172567Sthompsa		DPRINTF(("Scan failed\n"));
2244172567Sthompsa		sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2245172567Sthompsa	}
2246172567Sthompsa	return (error);
2247172567Sthompsa}
2248172567Sthompsa
2249172567Sthompsastatic int
2250172567Sthompsaipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2251172567Sthompsa{
2252178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2253178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2254172567Sthompsa	uint32_t data;
2255172567Sthompsa	int error;
2256172567Sthompsa
2257172567Sthompsa	data = htole32(ieee80211_chan2ieee(ic, chan));
2258172567Sthompsa	DPRINTF(("Setting channel to %u\n", le32toh(data)));
2259172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2260172567Sthompsa	if (error == 0)
2261172567Sthompsa		ipw_setcurchan(sc, chan);
2262172567Sthompsa	return error;
2263172567Sthompsa}
2264172567Sthompsa
2265178354Ssamstatic void
2266178354Ssamipw_assoc_task(void *context, int pending)
2267145247Sdamien{
2268178354Ssam	struct ieee80211vap *vap = context;
2269178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2270178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2271178354Ssam	struct ipw_softc *sc = ifp->if_softc;
2272178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2273145247Sdamien	struct ipw_security security;
2274145247Sdamien	uint32_t data;
2275172567Sthompsa	int error;
2276178354Ssam	IPW_LOCK_DECL;
2277145247Sdamien
2278178354Ssam	IPW_LOCK(sc);
2279172567Sthompsa	error = ipw_disable(sc);
2280172567Sthompsa	if (error != 0)
2281178354Ssam		goto done;
2282172567Sthompsa
2283152637Sdamien	memset(&security, 0, sizeof security);
2284178354Ssam	security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2285145247Sdamien	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2286145247Sdamien	security.ciphers = htole32(IPW_CIPHER_NONE);
2287145247Sdamien	DPRINTF(("Setting authmode to %u\n", security.authmode));
2288172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2289145247Sdamien	    sizeof security);
2290145247Sdamien	if (error != 0)
2291178354Ssam		goto done;
2292145247Sdamien
2293178354Ssam	data = htole32(vap->iv_rtsthreshold);
2294178354Ssam	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2295178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2296145247Sdamien	if (error != 0)
2297178354Ssam		goto done;
2298145247Sdamien
2299178354Ssam	data = htole32(vap->iv_fragthreshold);
2300178354Ssam	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2301178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2302145247Sdamien	if (error != 0)
2303178354Ssam		goto done;
2304145247Sdamien
2305178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2306172567Sthompsa		error = ipw_setwepkeys(sc);
2307172567Sthompsa		if (error != 0)
2308178354Ssam			goto done;
2309172567Sthompsa
2310178354Ssam		if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2311178354Ssam			data = htole32(vap->iv_def_txkey);
2312172567Sthompsa			DPRINTF(("Setting wep tx key index to %u\n",
2313172567Sthompsa				le32toh(data)));
2314172567Sthompsa			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2315172567Sthompsa			    sizeof data);
2316172567Sthompsa			if (error != 0)
2317178354Ssam				goto done;
2318172567Sthompsa		}
2319172567Sthompsa	}
2320172567Sthompsa
2321178354Ssam	data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2322172567Sthompsa	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2323172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2324172567Sthompsa	if (error != 0)
2325178354Ssam		goto done;
2326172567Sthompsa
2327172567Sthompsa	error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2328172567Sthompsa	if (error != 0)
2329178354Ssam		goto done;
2330172567Sthompsa
2331172567Sthompsa	error = ipw_setbssid(sc, ni->ni_bssid);
2332172567Sthompsa	if (error != 0)
2333178354Ssam		goto done;
2334172567Sthompsa
2335178354Ssam	if (vap->iv_appie_assocreq != NULL) {
2336178354Ssam		struct ieee80211_appie *ie = vap->iv_appie_assocreq;
2337178354Ssam		error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2338172567Sthompsa		if (error != 0)
2339178354Ssam			goto done;
2340172567Sthompsa	}
2341172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2342172567Sthompsa		error = ipw_setchannel(sc, ni->ni_chan);
2343172567Sthompsa		if (error != 0)
2344178354Ssam			goto done;
2345172567Sthompsa	}
2346172567Sthompsa
2347172567Sthompsa	/* lock scan to ap's channel and enable associate */
2348172567Sthompsa	error = ipw_setscanopts(sc,
2349178354Ssam	    1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2350178354Ssam	if (error != 0)
2351178354Ssam		goto done;
2352172567Sthompsa
2353178354Ssam	error = ipw_enable(sc);		/* finally, enable adapter */
2354178354Ssam	if (error == 0)
2355178354Ssam		sc->flags |= IPW_FLAG_ASSOCIATING;
2356178354Ssamdone:
2357178354Ssam	IPW_UNLOCK(sc);
2358172567Sthompsa}
2359172567Sthompsa
2360172567Sthompsastatic void
2361172567Sthompsaipw_disassoc_task(void *context, int pending)
2362172567Sthompsa{
2363178354Ssam	struct ieee80211vap *vap = context;
2364178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2365178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2366178354Ssam	struct ipw_softc *sc = ifp->if_softc;
2367172567Sthompsa	IPW_LOCK_DECL;
2368172567Sthompsa
2369172567Sthompsa	IPW_LOCK(sc);
2370172567Sthompsa	DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2371172567Sthompsa	/*
2372172567Sthompsa	 * NB: don't try to do this if ipw_stop_master has
2373172567Sthompsa	 *     shutdown the firmware and disabled interrupts.
2374172567Sthompsa	 */
2375178354Ssam	if (sc->flags & IPW_FLAG_FW_INITED) {
2376178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATED;
2377178354Ssam		/*
2378178354Ssam		 * NB: firmware currently ignores bssid parameter, but
2379178354Ssam		 *     supply it in case this changes (follow linux driver).
2380178354Ssam		 */
2381178354Ssam		(void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2382178354Ssam			ni->ni_bssid, IEEE80211_ADDR_LEN);
2383178354Ssam	}
2384178354Ssam	IPW_UNLOCK(sc);
2385172567Sthompsa}
2386172567Sthompsa
2387172567Sthompsa/*
2388156599Sdamien * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2389156599Sdamien * It is called on firmware panics or on watchdog timeouts.
2390156599Sdamien */
2391145247Sdamienstatic void
2392156599Sdamienipw_init_task(void *context, int pending)
2393156599Sdamien{
2394156599Sdamien	ipw_init(context);
2395156599Sdamien}
2396156599Sdamien
2397156599Sdamienstatic void
2398145247Sdamienipw_init(void *priv)
2399145247Sdamien{
2400145247Sdamien	struct ipw_softc *sc = priv;
2401178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2402178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2403172567Sthompsa	IPW_LOCK_DECL;
2404172567Sthompsa
2405172567Sthompsa	IPW_LOCK(sc);
2406178354Ssam	ipw_init_locked(sc);
2407172567Sthompsa	IPW_UNLOCK(sc);
2408178354Ssam
2409178931Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2410178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2411172567Sthompsa}
2412172567Sthompsa
2413172567Sthompsastatic void
2414178354Ssamipw_init_locked(struct ipw_softc *sc)
2415172567Sthompsa{
2416178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2417178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2418178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2419166756Sluigi	const struct firmware *fp;
2420156599Sdamien	const struct ipw_firmware_hdr *hdr;
2421178354Ssam	const char *fw;
2422145247Sdamien
2423172567Sthompsa	IPW_LOCK_ASSERT(sc);
2424156599Sdamien
2425172567Sthompsa	DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2426178354Ssam		ieee80211_state_name[vap->iv_state], sc->flags));
2427172567Sthompsa
2428156599Sdamien	/*
2429156599Sdamien	 * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2430156599Sdamien	 * when loading the firmware and we don't want to be called during this
2431156599Sdamien	 * operation.
2432156599Sdamien	 */
2433172567Sthompsa	if (sc->flags & IPW_FLAG_INIT_LOCKED)
2434145247Sdamien		return;
2435156599Sdamien	sc->flags |= IPW_FLAG_INIT_LOCKED;
2436145247Sdamien
2437172567Sthompsa	ipw_stop_locked(sc);
2438145247Sdamien
2439145247Sdamien	if (ipw_reset(sc) != 0) {
2440145247Sdamien		device_printf(sc->sc_dev, "could not reset adapter\n");
2441178354Ssam		goto fail;
2442145247Sdamien	}
2443145247Sdamien
2444159487Siedowse	if (sc->sc_firmware == NULL) {
2445178354Ssam		device_printf(sc->sc_dev, "no firmware\n");
2446178354Ssam		goto fail;
2447156599Sdamien	}
2448178354Ssam	/* NB: consistency already checked on load */
2449159487Siedowse	fp = sc->sc_firmware;
2450156599Sdamien	hdr = (const struct ipw_firmware_hdr *)fp->data;
2451156599Sdamien
2452178354Ssam	DPRINTF(("Loading firmware image '%s'\n", fp->name));
2453156599Sdamien	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2454156599Sdamien	if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2455145247Sdamien		device_printf(sc->sc_dev, "could not load microcode\n");
2456178354Ssam		goto fail;
2457145247Sdamien	}
2458145247Sdamien
2459145247Sdamien	ipw_stop_master(sc);
2460145247Sdamien
2461145247Sdamien	/*
2462145247Sdamien	 * Setup tx, rx and status rings.
2463145247Sdamien	 */
2464145247Sdamien	sc->txold = IPW_NTBD - 1;
2465145247Sdamien	sc->txcur = 0;
2466145247Sdamien	sc->txfree = IPW_NTBD - 2;
2467145247Sdamien	sc->rxcur = IPW_NRBD - 1;
2468145247Sdamien
2469145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2470145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2471145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2472145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2473145247Sdamien
2474145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2475145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2476145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2477145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2478145247Sdamien
2479145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2480145247Sdamien
2481156599Sdamien	fw = (const char *)fp->data + sizeof *hdr;
2482156599Sdamien	if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2483145247Sdamien		device_printf(sc->sc_dev, "could not load firmware\n");
2484178354Ssam		goto fail;
2485145247Sdamien	}
2486145247Sdamien
2487145247Sdamien	sc->flags |= IPW_FLAG_FW_INITED;
2488145247Sdamien
2489145247Sdamien	/* retrieve information tables base addresses */
2490145247Sdamien	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2491145247Sdamien	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2492145247Sdamien
2493145247Sdamien	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2494145247Sdamien
2495145247Sdamien	if (ipw_config(sc) != 0) {
2496145247Sdamien		device_printf(sc->sc_dev, "device configuration failed\n");
2497178354Ssam		goto fail;
2498145247Sdamien	}
2499145247Sdamien
2500172567Sthompsa	callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2501148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2502148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2503145247Sdamien
2504156599Sdamien	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2505145247Sdamien	return;
2506145247Sdamien
2507178354Ssamfail:
2508172567Sthompsa	ipw_stop_locked(sc);
2509156599Sdamien	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2510145247Sdamien}
2511145247Sdamien
2512178354Ssamstatic int
2513178354Ssamipw_config(struct ipw_softc *sc)
2514178354Ssam{
2515178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2516178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2517178354Ssam	struct ipw_configuration config;
2518178354Ssam	uint32_t data;
2519178354Ssam	int error;
2520178354Ssam
2521178354Ssam	error = ipw_disable(sc);
2522178354Ssam	if (error != 0)
2523178354Ssam		return error;
2524178354Ssam
2525178354Ssam	switch (ic->ic_opmode) {
2526178354Ssam	case IEEE80211_M_STA:
2527178354Ssam	case IEEE80211_M_HOSTAP:
2528178354Ssam	case IEEE80211_M_WDS:		/* XXX */
2529178354Ssam		data = htole32(IPW_MODE_BSS);
2530178354Ssam		break;
2531178354Ssam	case IEEE80211_M_IBSS:
2532178354Ssam	case IEEE80211_M_AHDEMO:
2533178354Ssam		data = htole32(IPW_MODE_IBSS);
2534178354Ssam		break;
2535178354Ssam	case IEEE80211_M_MONITOR:
2536178354Ssam		data = htole32(IPW_MODE_MONITOR);
2537178354Ssam		break;
2538178354Ssam	}
2539178354Ssam	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2540178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2541178354Ssam	if (error != 0)
2542178354Ssam		return error;
2543178354Ssam
2544178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2545178354Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2546178354Ssam		error = ipw_setchannel(sc, ic->ic_curchan);
2547178354Ssam		if (error != 0)
2548178354Ssam			return error;
2549178354Ssam	}
2550178354Ssam
2551178354Ssam	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2552178354Ssam		return ipw_enable(sc);
2553178354Ssam
2554178354Ssam	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2555178354Ssam	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2556178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
2557178354Ssam		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2558178354Ssam	if (ifp->if_flags & IFF_PROMISC)
2559178354Ssam		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2560178354Ssam	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2561178354Ssam	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2562178354Ssam	DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2563178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2564178354Ssam	if (error != 0)
2565178354Ssam		return error;
2566178354Ssam
2567178354Ssam	data = htole32(0x3); /* 1, 2 */
2568178354Ssam	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2569178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2570178354Ssam	if (error != 0)
2571178354Ssam		return error;
2572178354Ssam
2573178354Ssam	/* NB: use the same rate set */
2574178354Ssam	DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2575178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2576178354Ssam	if (error != 0)
2577178354Ssam		return error;
2578178354Ssam
2579178354Ssam	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2580178354Ssam	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2581178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2582178354Ssam	if (error != 0)
2583178354Ssam		return error;
2584178354Ssam
2585178354Ssam	data = htole32(IPW_POWER_MODE_CAM);
2586178354Ssam	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2587178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2588178354Ssam	if (error != 0)
2589178354Ssam		return error;
2590178354Ssam
2591178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2592178354Ssam		data = htole32(32); /* default value */
2593178354Ssam		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2594178354Ssam		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2595178354Ssam		    sizeof data);
2596178354Ssam		if (error != 0)
2597178354Ssam			return error;
2598178354Ssam	}
2599178354Ssam
2600178354Ssam	return 0;
2601178354Ssam}
2602178354Ssam
2603145247Sdamienstatic void
2604145247Sdamienipw_stop(void *priv)
2605145247Sdamien{
2606145247Sdamien	struct ipw_softc *sc = priv;
2607172567Sthompsa	IPW_LOCK_DECL;
2608172567Sthompsa
2609172567Sthompsa	IPW_LOCK(sc);
2610172567Sthompsa	ipw_stop_locked(sc);
2611172567Sthompsa	IPW_UNLOCK(sc);
2612172567Sthompsa}
2613172567Sthompsa
2614172567Sthompsastatic void
2615172567Sthompsaipw_stop_locked(struct ipw_softc *sc)
2616172567Sthompsa{
2617178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2618145247Sdamien	int i;
2619145247Sdamien
2620172567Sthompsa	IPW_LOCK_ASSERT(sc);
2621156599Sdamien
2622172567Sthompsa	callout_stop(&sc->sc_wdtimer);
2623145247Sdamien	ipw_stop_master(sc);
2624145247Sdamien
2625145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2626145247Sdamien
2627145247Sdamien	/*
2628145247Sdamien	 * Release tx buffers.
2629145247Sdamien	 */
2630145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
2631145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
2632145247Sdamien
2633145247Sdamien	sc->sc_tx_timer = 0;
2634148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2635145247Sdamien}
2636145247Sdamien
2637145247Sdamienstatic int
2638145247Sdamienipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2639145247Sdamien{
2640145247Sdamien	struct ipw_softc *sc = arg1;
2641145247Sdamien	uint32_t i, size, buf[256];
2642145247Sdamien
2643174317Sphilip	memset(buf, 0, sizeof buf);
2644174317Sphilip
2645174317Sphilip	if (!(sc->flags & IPW_FLAG_FW_INITED))
2646145247Sdamien		return SYSCTL_OUT(req, buf, sizeof buf);
2647145247Sdamien
2648145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2649145247Sdamien
2650145247Sdamien	size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2651145247Sdamien	for (i = 1; i < size; i++)
2652145247Sdamien		buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2653145247Sdamien
2654174317Sphilip	return SYSCTL_OUT(req, buf, size);
2655145247Sdamien}
2656145247Sdamien
2657145247Sdamienstatic int
2658145247Sdamienipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2659145247Sdamien{
2660145247Sdamien	struct ipw_softc *sc = arg1;
2661145247Sdamien	int val;
2662145247Sdamien
2663145247Sdamien	val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2664145247Sdamien	        (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2665145247Sdamien
2666145247Sdamien	return SYSCTL_OUT(req, &val, sizeof val);
2667145247Sdamien}
2668145247Sdamien
2669145247Sdamienstatic uint32_t
2670145247Sdamienipw_read_table1(struct ipw_softc *sc, uint32_t off)
2671145247Sdamien{
2672145247Sdamien	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2673145247Sdamien}
2674145247Sdamien
2675145247Sdamienstatic void
2676145247Sdamienipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2677145247Sdamien{
2678145247Sdamien	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2679145247Sdamien}
2680145247Sdamien
2681172567Sthompsa#if 0
2682145247Sdamienstatic int
2683145247Sdamienipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2684145247Sdamien{
2685145247Sdamien	uint32_t addr, info;
2686145247Sdamien	uint16_t count, size;
2687145247Sdamien	uint32_t total;
2688145247Sdamien
2689145247Sdamien	/* addr[4] + count[2] + size[2] */
2690145247Sdamien	addr = MEM_READ_4(sc, sc->table2_base + off);
2691145247Sdamien	info = MEM_READ_4(sc, sc->table2_base + off + 4);
2692145247Sdamien
2693145247Sdamien	count = info >> 16;
2694145247Sdamien	size = info & 0xffff;
2695145247Sdamien	total = count * size;
2696145247Sdamien
2697145247Sdamien	if (total > *len) {
2698145247Sdamien		*len = total;
2699145247Sdamien		return EINVAL;
2700145247Sdamien	}
2701145247Sdamien
2702145247Sdamien	*len = total;
2703145247Sdamien	ipw_read_mem_1(sc, addr, buf, total);
2704145247Sdamien
2705145247Sdamien	return 0;
2706145247Sdamien}
2707145247Sdamien
2708145247Sdamienstatic void
2709145247Sdamienipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2710145247Sdamien    bus_size_t count)
2711145247Sdamien{
2712145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2713145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2714145247Sdamien		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2715145247Sdamien	}
2716145247Sdamien}
2717172567Sthompsa#endif
2718145247Sdamien
2719145247Sdamienstatic void
2720156599Sdamienipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2721145247Sdamien    bus_size_t count)
2722145247Sdamien{
2723145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2724145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2725145247Sdamien		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2726145247Sdamien	}
2727145247Sdamien}
2728172567Sthompsa
2729172567Sthompsastatic void
2730172567Sthompsaipw_scan_start(struct ieee80211com *ic)
2731172567Sthompsa{
2732172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2733172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2734172567Sthompsa	IPW_LOCK_DECL;
2735172567Sthompsa
2736172567Sthompsa	IPW_LOCK(sc);
2737172567Sthompsa	if (!(sc->flags & IPW_FLAG_SCANNING))
2738178354Ssam		taskqueue_enqueue(taskqueue_swi, &sc->sc_scan_task);
2739172567Sthompsa	IPW_UNLOCK(sc);
2740172567Sthompsa}
2741172567Sthompsa
2742172567Sthompsastatic void
2743172567Sthompsaipw_set_channel(struct ieee80211com *ic)
2744172567Sthompsa{
2745172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2746172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2747172567Sthompsa	IPW_LOCK_DECL;
2748172567Sthompsa
2749172567Sthompsa	IPW_LOCK(sc);
2750172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2751172567Sthompsa		ipw_disable(sc);
2752172567Sthompsa		ipw_setchannel(sc, ic->ic_curchan);
2753172567Sthompsa		ipw_enable(sc);
2754172567Sthompsa	}
2755172567Sthompsa	IPW_UNLOCK(sc);
2756172567Sthompsa}
2757172567Sthompsa
2758172567Sthompsastatic void
2759178354Ssamipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2760172567Sthompsa{
2761172567Sthompsa	/* NB: all channels are scanned at once */
2762172567Sthompsa}
2763172567Sthompsa
2764172567Sthompsastatic void
2765178354Ssamipw_scan_mindwell(struct ieee80211_scan_state *ss)
2766172567Sthompsa{
2767172567Sthompsa	/* NB: don't try to abort scan; wait for firmware to finish */
2768172567Sthompsa}
2769172567Sthompsa
2770172567Sthompsastatic void
2771172567Sthompsaipw_scan_end(struct ieee80211com *ic)
2772172567Sthompsa{
2773172567Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2774172567Sthompsa	struct ipw_softc *sc = ifp->if_softc;
2775172567Sthompsa	IPW_LOCK_DECL;
2776172567Sthompsa
2777172567Sthompsa	IPW_LOCK(sc);
2778172567Sthompsa	sc->flags &= ~IPW_FLAG_SCANNING;
2779172567Sthompsa	IPW_UNLOCK(sc);
2780172567Sthompsa}
2781