1145247Sdamien/*-
2156599Sdamien * Copyright (c) 2004-2006
3145247Sdamien *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4172567Sthompsa * Copyright (c) 2006 Sam Leffler, Errno Consulting
5172567Sthompsa * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6145247Sdamien *
7145247Sdamien * Redistribution and use in source and binary forms, with or without
8145247Sdamien * modification, are permitted provided that the following conditions
9145247Sdamien * are met:
10145247Sdamien * 1. Redistributions of source code must retain the above copyright
11145247Sdamien *    notice unmodified, this list of conditions, and the following
12145247Sdamien *    disclaimer.
13145247Sdamien * 2. Redistributions in binary form must reproduce the above copyright
14145247Sdamien *    notice, this list of conditions and the following disclaimer in the
15145247Sdamien *    documentation and/or other materials provided with the distribution.
16145247Sdamien *
17145247Sdamien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18145247Sdamien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19145247Sdamien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20145247Sdamien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21145247Sdamien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22145247Sdamien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23145247Sdamien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24145247Sdamien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25145247Sdamien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26145247Sdamien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27145247Sdamien * SUCH DAMAGE.
28145247Sdamien */
29145247Sdamien
30145247Sdamien#include <sys/cdefs.h>
31145247Sdamien__FBSDID("$FreeBSD: stable/11/sys/dev/ipw/if_ipw.c 343907 2019-02-08 13:57:28Z avos $");
32145247Sdamien
33145247Sdamien/*-
34145247Sdamien * Intel(R) PRO/Wireless 2100 MiniPCI driver
35145247Sdamien * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
36145247Sdamien */
37145247Sdamien
38145247Sdamien#include <sys/param.h>
39145247Sdamien#include <sys/sysctl.h>
40145247Sdamien#include <sys/sockio.h>
41145247Sdamien#include <sys/mbuf.h>
42145247Sdamien#include <sys/kernel.h>
43145247Sdamien#include <sys/socket.h>
44145247Sdamien#include <sys/systm.h>
45145247Sdamien#include <sys/malloc.h>
46156599Sdamien#include <sys/queue.h>
47156599Sdamien#include <sys/taskqueue.h>
48145247Sdamien#include <sys/module.h>
49145247Sdamien#include <sys/bus.h>
50145247Sdamien#include <sys/endian.h>
51156599Sdamien#include <sys/linker.h>
52156599Sdamien#include <sys/firmware.h>
53145247Sdamien
54145247Sdamien#include <machine/bus.h>
55145247Sdamien#include <machine/resource.h>
56145247Sdamien#include <sys/rman.h>
57145247Sdamien
58145247Sdamien#include <dev/pci/pcireg.h>
59145247Sdamien#include <dev/pci/pcivar.h>
60145247Sdamien
61145247Sdamien#include <net/bpf.h>
62145247Sdamien#include <net/if.h>
63257176Sglebius#include <net/if_var.h>
64145247Sdamien#include <net/if_arp.h>
65145247Sdamien#include <net/ethernet.h>
66145247Sdamien#include <net/if_dl.h>
67145247Sdamien#include <net/if_media.h>
68145247Sdamien#include <net/if_types.h>
69145247Sdamien
70156599Sdamien#include <net80211/ieee80211_var.h>
71156599Sdamien#include <net80211/ieee80211_radiotap.h>
72156599Sdamien
73145247Sdamien#include <netinet/in.h>
74145247Sdamien#include <netinet/in_systm.h>
75145247Sdamien#include <netinet/in_var.h>
76145247Sdamien#include <netinet/ip.h>
77145247Sdamien#include <netinet/if_ether.h>
78145247Sdamien
79145247Sdamien#include <dev/ipw/if_ipwreg.h>
80145247Sdamien#include <dev/ipw/if_ipwvar.h>
81145247Sdamien
82172567Sthompsa#define IPW_DEBUG
83145247Sdamien#ifdef IPW_DEBUG
84145247Sdamien#define DPRINTF(x)	do { if (ipw_debug > 0) printf x; } while (0)
85145247Sdamien#define DPRINTFN(n, x)	do { if (ipw_debug >= (n)) printf x; } while (0)
86145247Sdamienint ipw_debug = 0;
87145247SdamienSYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
88145247Sdamien#else
89145247Sdamien#define DPRINTF(x)
90145247Sdamien#define DPRINTFN(n, x)
91145247Sdamien#endif
92145247Sdamien
93145247SdamienMODULE_DEPEND(ipw, pci,  1, 1, 1);
94145247SdamienMODULE_DEPEND(ipw, wlan, 1, 1, 1);
95156599SdamienMODULE_DEPEND(ipw, firmware, 1, 1, 1);
96145247Sdamien
97145247Sdamienstruct ipw_ident {
98145247Sdamien	uint16_t	vendor;
99145247Sdamien	uint16_t	device;
100145247Sdamien	const char	*name;
101145247Sdamien};
102145247Sdamien
103145247Sdamienstatic const struct ipw_ident ipw_ident_table[] = {
104145247Sdamien	{ 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
105145247Sdamien
106145247Sdamien	{ 0, 0, NULL }
107145247Sdamien};
108145247Sdamien
109178354Ssamstatic struct ieee80211vap *ipw_vap_create(struct ieee80211com *,
110228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
111228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
112228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
113178354Ssamstatic void	ipw_vap_delete(struct ieee80211vap *);
114145247Sdamienstatic int	ipw_dma_alloc(struct ipw_softc *);
115145247Sdamienstatic void	ipw_release(struct ipw_softc *);
116145247Sdamienstatic void	ipw_media_status(struct ifnet *, struct ifmediareq *);
117178354Ssamstatic int	ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
118145247Sdamienstatic uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
119300239Savosstatic uint16_t	ipw_read_chanmask(struct ipw_softc *);
120172567Sthompsastatic void	ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
121172567Sthompsastatic void	ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
122172567Sthompsastatic void	ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
123145247Sdamien		    struct ipw_soft_bd *, struct ipw_soft_buf *);
124145247Sdamienstatic void	ipw_rx_intr(struct ipw_softc *);
125145247Sdamienstatic void	ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
126145247Sdamienstatic void	ipw_tx_intr(struct ipw_softc *);
127145247Sdamienstatic void	ipw_intr(void *);
128145247Sdamienstatic void	ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
129172567Sthompsastatic const char * ipw_cmdname(int);
130145247Sdamienstatic int	ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
131287197Sglebiusstatic int	ipw_tx_start(struct ipw_softc *, struct mbuf *,
132145247Sdamien		    struct ieee80211_node *);
133178354Ssamstatic int	ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
134178354Ssam		    const struct ieee80211_bpf_params *);
135287197Sglebiusstatic int	ipw_transmit(struct ieee80211com *, struct mbuf *);
136287197Sglebiusstatic void	ipw_start(struct ipw_softc *);
137172567Sthompsastatic void	ipw_watchdog(void *);
138287197Sglebiusstatic void	ipw_parent(struct ieee80211com *);
139145247Sdamienstatic void	ipw_stop_master(struct ipw_softc *);
140172567Sthompsastatic int	ipw_enable(struct ipw_softc *);
141172567Sthompsastatic int	ipw_disable(struct ipw_softc *);
142145247Sdamienstatic int	ipw_reset(struct ipw_softc *);
143156599Sdamienstatic int	ipw_load_ucode(struct ipw_softc *, const char *, int);
144156599Sdamienstatic int	ipw_load_firmware(struct ipw_softc *, const char *, int);
145145247Sdamienstatic int	ipw_config(struct ipw_softc *);
146191746Sthompsastatic void	ipw_assoc(struct ieee80211com *, struct ieee80211vap *);
147191746Sthompsastatic void	ipw_disassoc(struct ieee80211com *, struct ieee80211vap *);
148156599Sdamienstatic void	ipw_init_task(void *, int);
149145247Sdamienstatic void	ipw_init(void *);
150178354Ssamstatic void	ipw_init_locked(struct ipw_softc *);
151145247Sdamienstatic void	ipw_stop(void *);
152172567Sthompsastatic void	ipw_stop_locked(struct ipw_softc *);
153145247Sdamienstatic int	ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
154145247Sdamienstatic int	ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
155145247Sdamienstatic uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
156145247Sdamienstatic void	ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
157172567Sthompsa#if 0
158145247Sdamienstatic int	ipw_read_table2(struct ipw_softc *, uint32_t, void *,
159145247Sdamien		    uint32_t *);
160145247Sdamienstatic void	ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
161145247Sdamien		    bus_size_t);
162172567Sthompsa#endif
163156599Sdamienstatic void	ipw_write_mem_1(struct ipw_softc *, bus_size_t,
164156599Sdamien		    const uint8_t *, bus_size_t);
165172567Sthompsastatic int	ipw_scan(struct ipw_softc *);
166172567Sthompsastatic void	ipw_scan_start(struct ieee80211com *);
167172567Sthompsastatic void	ipw_scan_end(struct ieee80211com *);
168300239Savosstatic void	ipw_getradiocaps(struct ieee80211com *, int, int *,
169300239Savos		    struct ieee80211_channel[]);
170172567Sthompsastatic void	ipw_set_channel(struct ieee80211com *);
171178354Ssamstatic void	ipw_scan_curchan(struct ieee80211_scan_state *,
172178354Ssam		    unsigned long maxdwell);
173178354Ssamstatic void	ipw_scan_mindwell(struct ieee80211_scan_state *);
174145247Sdamien
175145247Sdamienstatic int ipw_probe(device_t);
176145247Sdamienstatic int ipw_attach(device_t);
177145247Sdamienstatic int ipw_detach(device_t);
178145247Sdamienstatic int ipw_shutdown(device_t);
179145247Sdamienstatic int ipw_suspend(device_t);
180145247Sdamienstatic int ipw_resume(device_t);
181145247Sdamien
182145247Sdamienstatic device_method_t ipw_methods[] = {
183145247Sdamien	/* Device interface */
184145247Sdamien	DEVMETHOD(device_probe,		ipw_probe),
185145247Sdamien	DEVMETHOD(device_attach,	ipw_attach),
186145247Sdamien	DEVMETHOD(device_detach,	ipw_detach),
187145247Sdamien	DEVMETHOD(device_shutdown,	ipw_shutdown),
188145247Sdamien	DEVMETHOD(device_suspend,	ipw_suspend),
189145247Sdamien	DEVMETHOD(device_resume,	ipw_resume),
190145247Sdamien
191260062Smarius	DEVMETHOD_END
192145247Sdamien};
193145247Sdamien
194145247Sdamienstatic driver_t ipw_driver = {
195145247Sdamien	"ipw",
196145247Sdamien	ipw_methods,
197145247Sdamien	sizeof (struct ipw_softc)
198145247Sdamien};
199145247Sdamien
200145247Sdamienstatic devclass_t ipw_devclass;
201145247Sdamien
202260062SmariusDRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, NULL, NULL);
203145247Sdamien
204222543SbschmidtMODULE_VERSION(ipw, 1);
205222543Sbschmidt
206145247Sdamienstatic int
207145247Sdamienipw_probe(device_t dev)
208145247Sdamien{
209145247Sdamien	const struct ipw_ident *ident;
210145247Sdamien
211145247Sdamien	for (ident = ipw_ident_table; ident->name != NULL; ident++) {
212145247Sdamien		if (pci_get_vendor(dev) == ident->vendor &&
213145247Sdamien		    pci_get_device(dev) == ident->device) {
214145247Sdamien			device_set_desc(dev, ident->name);
215260062Smarius			return (BUS_PROBE_DEFAULT);
216145247Sdamien		}
217145247Sdamien	}
218145247Sdamien	return ENXIO;
219145247Sdamien}
220145247Sdamien
221145247Sdamien/* Base Address Register */
222145247Sdamienstatic int
223145247Sdamienipw_attach(device_t dev)
224145247Sdamien{
225145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
226287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
227145247Sdamien	uint16_t val;
228145247Sdamien	int error, i;
229145247Sdamien
230145247Sdamien	sc->sc_dev = dev;
231145247Sdamien
232145247Sdamien	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
233145247Sdamien	    MTX_DEF | MTX_RECURSE);
234287197Sglebius	mbufq_init(&sc->sc_snd, ifqmaxlen);
235156599Sdamien	TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
236172567Sthompsa	callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
237156599Sdamien
238146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
239146498Sdamien
240145247Sdamien	/* enable bus-mastering */
241145247Sdamien	pci_enable_busmaster(dev);
242145247Sdamien
243260062Smarius	i = PCIR_BAR(0);
244260062Smarius	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE);
245145247Sdamien	if (sc->mem == NULL) {
246145247Sdamien		device_printf(dev, "could not allocate memory resource\n");
247145247Sdamien		goto fail;
248145247Sdamien	}
249145247Sdamien
250145247Sdamien	sc->sc_st = rman_get_bustag(sc->mem);
251145247Sdamien	sc->sc_sh = rman_get_bushandle(sc->mem);
252145247Sdamien
253260062Smarius	i = 0;
254260062Smarius	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
255145247Sdamien	    RF_ACTIVE | RF_SHAREABLE);
256145247Sdamien	if (sc->irq == NULL) {
257145247Sdamien		device_printf(dev, "could not allocate interrupt resource\n");
258178354Ssam		goto fail1;
259145247Sdamien	}
260145247Sdamien
261145247Sdamien	if (ipw_reset(sc) != 0) {
262145247Sdamien		device_printf(dev, "could not reset adapter\n");
263178354Ssam		goto fail2;
264145247Sdamien	}
265145247Sdamien
266145247Sdamien	if (ipw_dma_alloc(sc) != 0) {
267145247Sdamien		device_printf(dev, "could not allocate DMA resources\n");
268178354Ssam		goto fail2;
269145247Sdamien	}
270147757Sdamien
271283537Sglebius	ic->ic_softc = sc;
272283527Sglebius	ic->ic_name = device_get_nameunit(dev);
273178354Ssam	ic->ic_opmode = IEEE80211_M_STA;
274145247Sdamien	ic->ic_phytype = IEEE80211_T_DS;
275145247Sdamien
276145247Sdamien	/* set device capabilities */
277178957Ssam	ic->ic_caps =
278178957Ssam		  IEEE80211_C_STA		/* station mode supported */
279178957Ssam		| IEEE80211_C_IBSS		/* IBSS mode supported */
280172567Sthompsa		| IEEE80211_C_MONITOR		/* monitor mode supported */
281172567Sthompsa		| IEEE80211_C_PMGT		/* power save supported */
282172567Sthompsa		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
283172567Sthompsa		| IEEE80211_C_WPA		/* 802.11i supported */
284172567Sthompsa		;
285145247Sdamien
286145247Sdamien	/* read MAC address from EEPROM */
287145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
288287197Sglebius	ic->ic_macaddr[0] = val >> 8;
289287197Sglebius	ic->ic_macaddr[1] = val & 0xff;
290145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
291287197Sglebius	ic->ic_macaddr[2] = val >> 8;
292287197Sglebius	ic->ic_macaddr[3] = val & 0xff;
293145247Sdamien	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
294287197Sglebius	ic->ic_macaddr[4] = val >> 8;
295287197Sglebius	ic->ic_macaddr[5] = val & 0xff;
296145247Sdamien
297300239Savos	sc->chanmask = ipw_read_chanmask(sc);
298300239Savos	ipw_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
299300239Savos	    ic->ic_channels);
300145247Sdamien
301145247Sdamien	/* check support for radio transmitter switch in EEPROM */
302145247Sdamien	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
303145247Sdamien		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
304145247Sdamien
305287197Sglebius	ieee80211_ifattach(ic);
306172567Sthompsa	ic->ic_scan_start = ipw_scan_start;
307172567Sthompsa	ic->ic_scan_end = ipw_scan_end;
308300239Savos	ic->ic_getradiocaps = ipw_getradiocaps;
309172567Sthompsa	ic->ic_set_channel = ipw_set_channel;
310172567Sthompsa	ic->ic_scan_curchan = ipw_scan_curchan;
311172567Sthompsa	ic->ic_scan_mindwell = ipw_scan_mindwell;
312178354Ssam	ic->ic_raw_xmit = ipw_raw_xmit;
313178354Ssam	ic->ic_vap_create = ipw_vap_create;
314178354Ssam	ic->ic_vap_delete = ipw_vap_delete;
315287197Sglebius	ic->ic_transmit = ipw_transmit;
316287197Sglebius	ic->ic_parent = ipw_parent;
317145247Sdamien
318192468Ssam	ieee80211_radiotap_attach(ic,
319192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
320192468Ssam		IPW_TX_RADIOTAP_PRESENT,
321192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
322192468Ssam		IPW_RX_RADIOTAP_PRESENT);
323178354Ssam
324145247Sdamien	/*
325145247Sdamien	 * Add a few sysctl knobs.
326145247Sdamien	 */
327145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
328145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
329145247Sdamien	    CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
330145247Sdamien	    "radio transmitter switch state (0=off, 1=on)");
331145247Sdamien
332145247Sdamien	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
333145247Sdamien	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
334145247Sdamien	    CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
335145247Sdamien	    "statistics");
336145247Sdamien
337145247Sdamien	/*
338145247Sdamien	 * Hook our interrupt after all initialization is complete.
339145247Sdamien	 */
340145247Sdamien	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
341166901Spiso	    NULL, ipw_intr, sc, &sc->sc_ih);
342145247Sdamien	if (error != 0) {
343145247Sdamien		device_printf(dev, "could not set up interrupt\n");
344287197Sglebius		goto fail3;
345145247Sdamien	}
346145247Sdamien
347145247Sdamien	if (bootverbose)
348145247Sdamien		ieee80211_announce(ic);
349145247Sdamien
350145247Sdamien	return 0;
351178354Ssamfail3:
352178354Ssam	ipw_release(sc);
353178354Ssamfail2:
354260062Smarius	bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
355178354Ssamfail1:
356260062Smarius	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
357260062Smarius	    sc->mem);
358178354Ssamfail:
359178354Ssam	mtx_destroy(&sc->sc_mtx);
360145247Sdamien	return ENXIO;
361145247Sdamien}
362145247Sdamien
363145247Sdamienstatic int
364145247Sdamienipw_detach(device_t dev)
365145247Sdamien{
366145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
367287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
368145247Sdamien
369260062Smarius	bus_teardown_intr(dev, sc->irq, sc->sc_ih);
370260062Smarius
371191912Sthompsa	ieee80211_draintask(ic, &sc->sc_init_task);
372145247Sdamien	ipw_stop(sc);
373178354Ssam
374178354Ssam	ieee80211_ifdetach(ic);
375178354Ssam
376172567Sthompsa	callout_drain(&sc->sc_wdtimer);
377287197Sglebius	mbufq_drain(&sc->sc_snd);
378145247Sdamien
379145247Sdamien	ipw_release(sc);
380145247Sdamien
381260062Smarius	bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
382145247Sdamien
383260062Smarius	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
384260062Smarius	    sc->mem);
385156599Sdamien
386159487Siedowse	if (sc->sc_firmware != NULL) {
387159487Siedowse		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
388159487Siedowse		sc->sc_firmware = NULL;
389159487Siedowse	}
390159487Siedowse
391145247Sdamien	mtx_destroy(&sc->sc_mtx);
392145247Sdamien
393145247Sdamien	return 0;
394145247Sdamien}
395145247Sdamien
396178354Ssamstatic struct ieee80211vap *
397228621Sbschmidtipw_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
398228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
399228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
400228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
401178354Ssam{
402286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
403178354Ssam	struct ipw_vap *ivp;
404178354Ssam	struct ieee80211vap *vap;
405178354Ssam	const struct firmware *fp;
406178354Ssam	const struct ipw_firmware_hdr *hdr;
407178354Ssam	const char *imagename;
408178354Ssam
409178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
410178354Ssam		return NULL;
411178354Ssam
412178354Ssam	switch (opmode) {
413178354Ssam	case IEEE80211_M_STA:
414178354Ssam		imagename = "ipw_bss";
415178354Ssam		break;
416178354Ssam	case IEEE80211_M_IBSS:
417178354Ssam		imagename = "ipw_ibss";
418178354Ssam		break;
419178354Ssam	case IEEE80211_M_MONITOR:
420178354Ssam		imagename = "ipw_monitor";
421178354Ssam		break;
422178354Ssam	default:
423178354Ssam		return NULL;
424178354Ssam	}
425178354Ssam
426178354Ssam	/*
427178354Ssam	 * Load firmware image using the firmware(9) subsystem.  Doing
428178354Ssam	 * this unlocked is ok since we're single-threaded by the
429178354Ssam	 * 802.11 layer.
430178354Ssam	 */
431178354Ssam	if (sc->sc_firmware == NULL ||
432178354Ssam	    strcmp(sc->sc_firmware->name, imagename) != 0) {
433178354Ssam		if (sc->sc_firmware != NULL)
434178354Ssam			firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
435178354Ssam		sc->sc_firmware = firmware_get(imagename);
436178354Ssam	}
437178354Ssam	if (sc->sc_firmware == NULL) {
438178354Ssam		device_printf(sc->sc_dev,
439178354Ssam		    "could not load firmware image '%s'\n", imagename);
440178354Ssam		return NULL;
441178354Ssam	}
442178354Ssam	fp = sc->sc_firmware;
443178354Ssam	if (fp->datasize < sizeof *hdr) {
444178354Ssam		device_printf(sc->sc_dev,
445178354Ssam		    "firmware image too short %zu\n", fp->datasize);
446178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
447178354Ssam		sc->sc_firmware = NULL;
448178354Ssam		return NULL;
449178354Ssam	}
450178354Ssam	hdr = (const struct ipw_firmware_hdr *)fp->data;
451178354Ssam	if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
452178354Ssam	    le32toh(hdr->ucodesz)) {
453178354Ssam		device_printf(sc->sc_dev,
454178354Ssam		    "firmware image too short %zu\n", fp->datasize);
455178354Ssam		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
456178354Ssam		sc->sc_firmware = NULL;
457178354Ssam		return NULL;
458178354Ssam	}
459178354Ssam
460287197Sglebius	ivp = malloc(sizeof(struct ipw_vap), M_80211_VAP, M_WAITOK | M_ZERO);
461178354Ssam	vap = &ivp->vap;
462178354Ssam
463287197Sglebius	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
464178354Ssam	/* override with driver methods */
465178354Ssam	ivp->newstate = vap->iv_newstate;
466178354Ssam	vap->iv_newstate = ipw_newstate;
467178354Ssam
468178354Ssam	/* complete setup */
469287197Sglebius	ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status,
470287197Sglebius	    mac);
471178354Ssam	ic->ic_opmode = opmode;
472178354Ssam	return vap;
473178354Ssam}
474178354Ssam
475178354Ssamstatic void
476178354Ssamipw_vap_delete(struct ieee80211vap *vap)
477178354Ssam{
478178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
479178354Ssam
480178354Ssam	ieee80211_vap_detach(vap);
481178354Ssam	free(ivp, M_80211_VAP);
482178354Ssam}
483178354Ssam
484145247Sdamienstatic int
485145247Sdamienipw_dma_alloc(struct ipw_softc *sc)
486145247Sdamien{
487145247Sdamien	struct ipw_soft_bd *sbd;
488145247Sdamien	struct ipw_soft_hdr *shdr;
489145247Sdamien	struct ipw_soft_buf *sbuf;
490145247Sdamien	bus_addr_t physaddr;
491145247Sdamien	int error, i;
492145247Sdamien
493145247Sdamien	/*
494232874Sscottl	 * Allocate parent DMA tag for subsequent allocations.
495232874Sscottl	 */
496232874Sscottl	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
497232874Sscottl	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
498232874Sscottl	    BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED,
499232874Sscottl	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &sc->parent_dmat);
500232874Sscottl	if (error != 0) {
501232874Sscottl		device_printf(sc->sc_dev, "could not create parent DMA tag\n");
502232874Sscottl		goto fail;
503232874Sscottl	}
504232874Sscottl
505232874Sscottl	/*
506145247Sdamien	 * Allocate and map tx ring.
507145247Sdamien	 */
508232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
509145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
510145247Sdamien	    NULL, &sc->tbd_dmat);
511145247Sdamien	if (error != 0) {
512145247Sdamien		device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
513145247Sdamien		goto fail;
514145247Sdamien	}
515145247Sdamien
516145247Sdamien	error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
517145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
518145247Sdamien	if (error != 0) {
519145247Sdamien		device_printf(sc->sc_dev,
520145247Sdamien		    "could not allocate tx ring DMA memory\n");
521145247Sdamien		goto fail;
522145247Sdamien	}
523145247Sdamien
524145247Sdamien	error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
525145247Sdamien	    IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
526145247Sdamien	if (error != 0) {
527145247Sdamien		device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
528145247Sdamien		goto fail;
529145247Sdamien	}
530145247Sdamien
531145247Sdamien	/*
532145247Sdamien	 * Allocate and map rx ring.
533145247Sdamien	 */
534232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
535145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
536145247Sdamien	    NULL, &sc->rbd_dmat);
537145247Sdamien	if (error != 0) {
538145247Sdamien		device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
539145247Sdamien		goto fail;
540145247Sdamien	}
541145247Sdamien
542145247Sdamien	error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
543145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
544145247Sdamien	if (error != 0) {
545145247Sdamien		device_printf(sc->sc_dev,
546145247Sdamien		    "could not allocate rx ring DMA memory\n");
547145247Sdamien		goto fail;
548145247Sdamien	}
549145247Sdamien
550145247Sdamien	error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
551145247Sdamien	    IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
552145247Sdamien	if (error != 0) {
553145247Sdamien		device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
554145247Sdamien		goto fail;
555145247Sdamien	}
556145247Sdamien
557145247Sdamien	/*
558145247Sdamien	 * Allocate and map status ring.
559145247Sdamien	 */
560232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
561145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
562145247Sdamien	    NULL, NULL, &sc->status_dmat);
563145247Sdamien	if (error != 0) {
564145247Sdamien		device_printf(sc->sc_dev,
565145247Sdamien		    "could not create status ring DMA tag\n");
566145247Sdamien		goto fail;
567145247Sdamien	}
568145247Sdamien
569145247Sdamien	error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
570145247Sdamien	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
571145247Sdamien	if (error != 0) {
572145247Sdamien		device_printf(sc->sc_dev,
573145247Sdamien		    "could not allocate status ring DMA memory\n");
574145247Sdamien		goto fail;
575145247Sdamien	}
576145247Sdamien
577145247Sdamien	error = bus_dmamap_load(sc->status_dmat, sc->status_map,
578145247Sdamien	    sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
579145247Sdamien	    0);
580145247Sdamien	if (error != 0) {
581145247Sdamien		device_printf(sc->sc_dev,
582145247Sdamien		    "could not map status ring DMA memory\n");
583145247Sdamien		goto fail;
584145247Sdamien	}
585145247Sdamien
586145247Sdamien	/*
587145247Sdamien	 * Allocate command DMA map.
588145247Sdamien	 */
589232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
590145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
591145247Sdamien	    sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
592145247Sdamien	if (error != 0) {
593145247Sdamien		device_printf(sc->sc_dev, "could not create command DMA tag\n");
594145247Sdamien		goto fail;
595145247Sdamien	}
596145247Sdamien
597145247Sdamien	error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
598145247Sdamien	if (error != 0) {
599145247Sdamien		device_printf(sc->sc_dev,
600145247Sdamien		    "could not create command DMA map\n");
601145247Sdamien		goto fail;
602145247Sdamien	}
603145247Sdamien
604145247Sdamien	/*
605145247Sdamien	 * Allocate headers DMA maps.
606145247Sdamien	 */
607232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
608145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
609145247Sdamien	    sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
610145247Sdamien	if (error != 0) {
611145247Sdamien		device_printf(sc->sc_dev, "could not create header DMA tag\n");
612145247Sdamien		goto fail;
613145247Sdamien	}
614145247Sdamien
615145247Sdamien	SLIST_INIT(&sc->free_shdr);
616145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
617145247Sdamien		shdr = &sc->shdr_list[i];
618145247Sdamien		error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
619145247Sdamien		if (error != 0) {
620145247Sdamien			device_printf(sc->sc_dev,
621145247Sdamien			    "could not create header DMA map\n");
622145247Sdamien			goto fail;
623145247Sdamien		}
624145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
625145247Sdamien	}
626145247Sdamien
627145247Sdamien	/*
628145247Sdamien	 * Allocate tx buffers DMA maps.
629145247Sdamien	 */
630232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
631145247Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
632145247Sdamien	    NULL, NULL, &sc->txbuf_dmat);
633145247Sdamien	if (error != 0) {
634145247Sdamien		device_printf(sc->sc_dev, "could not create tx DMA tag\n");
635145247Sdamien		goto fail;
636145247Sdamien	}
637145247Sdamien
638145247Sdamien	SLIST_INIT(&sc->free_sbuf);
639145247Sdamien	for (i = 0; i < IPW_NDATA; i++) {
640145247Sdamien		sbuf = &sc->tx_sbuf_list[i];
641145247Sdamien		error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
642145247Sdamien		if (error != 0) {
643145247Sdamien			device_printf(sc->sc_dev,
644145247Sdamien			    "could not create tx DMA map\n");
645145247Sdamien			goto fail;
646145247Sdamien		}
647145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
648145247Sdamien	}
649145247Sdamien
650145247Sdamien	/*
651145247Sdamien	 * Initialize tx ring.
652145247Sdamien	 */
653145247Sdamien	for (i = 0; i < IPW_NTBD; i++) {
654145247Sdamien		sbd = &sc->stbd_list[i];
655145247Sdamien		sbd->bd = &sc->tbd_list[i];
656145247Sdamien		sbd->type = IPW_SBD_TYPE_NOASSOC;
657145247Sdamien	}
658145247Sdamien
659145247Sdamien	/*
660145247Sdamien	 * Pre-allocate rx buffers and DMA maps.
661145247Sdamien	 */
662232874Sscottl	error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
663147841Sdamien	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
664147841Sdamien	    NULL, &sc->rxbuf_dmat);
665145247Sdamien	if (error != 0) {
666145247Sdamien		device_printf(sc->sc_dev, "could not create rx DMA tag\n");
667145247Sdamien		goto fail;
668145247Sdamien	}
669145247Sdamien
670145247Sdamien	for (i = 0; i < IPW_NRBD; i++) {
671145247Sdamien		sbd = &sc->srbd_list[i];
672145247Sdamien		sbuf = &sc->rx_sbuf_list[i];
673145247Sdamien		sbd->bd = &sc->rbd_list[i];
674145247Sdamien
675243857Sglebius		sbuf->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
676145247Sdamien		if (sbuf->m == NULL) {
677145247Sdamien			device_printf(sc->sc_dev,
678145247Sdamien			    "could not allocate rx mbuf\n");
679145247Sdamien			error = ENOMEM;
680145247Sdamien			goto fail;
681145247Sdamien		}
682145247Sdamien
683145247Sdamien		error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
684145247Sdamien		if (error != 0) {
685145247Sdamien			device_printf(sc->sc_dev,
686145247Sdamien			    "could not create rx DMA map\n");
687145247Sdamien			goto fail;
688145247Sdamien		}
689145247Sdamien
690145247Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
691145247Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
692145247Sdamien		    &physaddr, 0);
693145247Sdamien		if (error != 0) {
694145247Sdamien			device_printf(sc->sc_dev,
695145247Sdamien			    "could not map rx DMA memory\n");
696145247Sdamien			goto fail;
697145247Sdamien		}
698145247Sdamien
699145247Sdamien		sbd->type = IPW_SBD_TYPE_DATA;
700145247Sdamien		sbd->priv = sbuf;
701145247Sdamien		sbd->bd->physaddr = htole32(physaddr);
702145247Sdamien		sbd->bd->len = htole32(MCLBYTES);
703145247Sdamien	}
704145247Sdamien
705145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
706145247Sdamien
707145247Sdamien	return 0;
708145247Sdamien
709145247Sdamienfail:	ipw_release(sc);
710145247Sdamien	return error;
711145247Sdamien}
712145247Sdamien
713145247Sdamienstatic void
714145247Sdamienipw_release(struct ipw_softc *sc)
715145247Sdamien{
716145247Sdamien	struct ipw_soft_buf *sbuf;
717145247Sdamien	int i;
718145247Sdamien
719232874Sscottl	if (sc->parent_dmat != NULL) {
720232874Sscottl		bus_dma_tag_destroy(sc->parent_dmat);
721232874Sscottl	}
722232874Sscottl
723145247Sdamien	if (sc->tbd_dmat != NULL) {
724283528Sglebius		bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
725283528Sglebius		bus_dmamem_free(sc->tbd_dmat, sc->tbd_list, sc->tbd_map);
726145247Sdamien		bus_dma_tag_destroy(sc->tbd_dmat);
727145247Sdamien	}
728145247Sdamien
729145247Sdamien	if (sc->rbd_dmat != NULL) {
730145247Sdamien		if (sc->rbd_list != NULL) {
731145247Sdamien			bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
732145247Sdamien			bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
733145247Sdamien			    sc->rbd_map);
734145247Sdamien		}
735145247Sdamien		bus_dma_tag_destroy(sc->rbd_dmat);
736145247Sdamien	}
737145247Sdamien
738145247Sdamien	if (sc->status_dmat != NULL) {
739145247Sdamien		if (sc->status_list != NULL) {
740145247Sdamien			bus_dmamap_unload(sc->status_dmat, sc->status_map);
741145247Sdamien			bus_dmamem_free(sc->status_dmat, sc->status_list,
742145247Sdamien			    sc->status_map);
743145247Sdamien		}
744145247Sdamien		bus_dma_tag_destroy(sc->status_dmat);
745145247Sdamien	}
746145247Sdamien
747145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
748145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
749145247Sdamien
750145247Sdamien	if (sc->cmd_dmat != NULL) {
751145247Sdamien		bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
752145247Sdamien		bus_dma_tag_destroy(sc->cmd_dmat);
753145247Sdamien	}
754145247Sdamien
755145247Sdamien	if (sc->hdr_dmat != NULL) {
756145247Sdamien		for (i = 0; i < IPW_NDATA; i++)
757145247Sdamien			bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
758145247Sdamien		bus_dma_tag_destroy(sc->hdr_dmat);
759145247Sdamien	}
760145247Sdamien
761145247Sdamien	if (sc->txbuf_dmat != NULL) {
762145247Sdamien		for (i = 0; i < IPW_NDATA; i++) {
763145247Sdamien			bus_dmamap_destroy(sc->txbuf_dmat,
764145247Sdamien			    sc->tx_sbuf_list[i].map);
765145247Sdamien		}
766145247Sdamien		bus_dma_tag_destroy(sc->txbuf_dmat);
767145247Sdamien	}
768145247Sdamien
769145247Sdamien	if (sc->rxbuf_dmat != NULL) {
770145247Sdamien		for (i = 0; i < IPW_NRBD; i++) {
771145247Sdamien			sbuf = &sc->rx_sbuf_list[i];
772145247Sdamien			if (sbuf->m != NULL) {
773145247Sdamien				bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
774145247Sdamien				    BUS_DMASYNC_POSTREAD);
775145247Sdamien				bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
776145247Sdamien				m_freem(sbuf->m);
777145247Sdamien			}
778145247Sdamien			bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
779145247Sdamien		}
780145247Sdamien		bus_dma_tag_destroy(sc->rxbuf_dmat);
781145247Sdamien	}
782145247Sdamien}
783145247Sdamien
784145247Sdamienstatic int
785145247Sdamienipw_shutdown(device_t dev)
786145247Sdamien{
787145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
788145247Sdamien
789145247Sdamien	ipw_stop(sc);
790145247Sdamien
791145247Sdamien	return 0;
792145247Sdamien}
793145247Sdamien
794145247Sdamienstatic int
795145247Sdamienipw_suspend(device_t dev)
796145247Sdamien{
797145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
798287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
799145247Sdamien
800233387Sbschmidt	ieee80211_suspend_all(ic);
801145247Sdamien	return 0;
802145247Sdamien}
803145247Sdamien
804145247Sdamienstatic int
805145247Sdamienipw_resume(device_t dev)
806145247Sdamien{
807145247Sdamien	struct ipw_softc *sc = device_get_softc(dev);
808287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
809145247Sdamien
810146498Sdamien	pci_write_config(dev, 0x41, 0, 1);
811146498Sdamien
812233387Sbschmidt	ieee80211_resume_all(ic);
813145247Sdamien	return 0;
814145247Sdamien}
815145247Sdamien
816145247Sdamienstatic int
817172567Sthompsaipw_cvtrate(int ipwrate)
818172567Sthompsa{
819172567Sthompsa	switch (ipwrate) {
820172567Sthompsa	case IPW_RATE_DS1:	return 2;
821172567Sthompsa	case IPW_RATE_DS2:	return 4;
822172567Sthompsa	case IPW_RATE_DS5:	return 11;
823172567Sthompsa	case IPW_RATE_DS11:	return 22;
824172567Sthompsa	}
825145247Sdamien	return 0;
826145247Sdamien}
827145247Sdamien
828145247Sdamien/*
829156599Sdamien * The firmware automatically adapts the transmit speed. We report its current
830156599Sdamien * value here.
831145247Sdamien */
832145247Sdamienstatic void
833145247Sdamienipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
834145247Sdamien{
835178354Ssam	struct ieee80211vap *vap = ifp->if_softc;
836178354Ssam	struct ieee80211com *ic = vap->iv_ic;
837286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
838145247Sdamien
839145247Sdamien	/* read current transmission rate from adapter */
840178354Ssam	vap->iv_bss->ni_txrate = ipw_cvtrate(
841178354Ssam	    ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
842178354Ssam	ieee80211_media_status(ifp, imr);
843145247Sdamien}
844145247Sdamien
845145247Sdamienstatic int
846178354Ssamipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
847145247Sdamien{
848178354Ssam	struct ipw_vap *ivp = IPW_VAP(vap);
849178354Ssam	struct ieee80211com *ic = vap->iv_ic;
850286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
851191746Sthompsa	enum ieee80211_state ostate;
852145247Sdamien
853172567Sthompsa	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
854178354Ssam		ieee80211_state_name[vap->iv_state],
855172567Sthompsa		ieee80211_state_name[nstate], sc->flags));
856172567Sthompsa
857191746Sthompsa	ostate = vap->iv_state;
858191746Sthompsa	IEEE80211_UNLOCK(ic);
859191746Sthompsa
860145247Sdamien	switch (nstate) {
861145247Sdamien	case IEEE80211_S_RUN:
862172567Sthompsa		if (ic->ic_opmode == IEEE80211_M_IBSS) {
863172567Sthompsa			/*
864172567Sthompsa			 * XXX when joining an ibss network we are called
865172567Sthompsa			 * with a SCAN -> RUN transition on scan complete.
866206763Sbschmidt			 * Use that to call ipw_assoc.  On completing the
867206763Sbschmidt			 * join we are then called again with an AUTH -> RUN
868206763Sbschmidt			 * transition and we want to do nothing.  This is
869206763Sbschmidt			 * all totally bogus and needs to be redone.
870172567Sthompsa			 */
871191746Sthompsa			if (ostate == IEEE80211_S_SCAN)
872191746Sthompsa				ipw_assoc(ic, vap);
873172567Sthompsa		}
874172567Sthompsa		break;
875145247Sdamien
876172567Sthompsa	case IEEE80211_S_INIT:
877172567Sthompsa		if (sc->flags & IPW_FLAG_ASSOCIATED)
878191746Sthompsa			ipw_disassoc(ic, vap);
879172567Sthompsa		break;
880145247Sdamien
881172567Sthompsa	case IEEE80211_S_AUTH:
882206765Sbschmidt		/*
883206765Sbschmidt		 * Move to ASSOC state after the ipw_assoc() call.  Firmware
884206765Sbschmidt		 * takes care of authentication, after the call we'll receive
885206765Sbschmidt		 * only an assoc response which would otherwise be discared
886206765Sbschmidt		 * if we are still in AUTH state.
887206765Sbschmidt		 */
888206765Sbschmidt		nstate = IEEE80211_S_ASSOC;
889191746Sthompsa		ipw_assoc(ic, vap);
890191746Sthompsa		break;
891145247Sdamien
892172567Sthompsa	case IEEE80211_S_ASSOC:
893172567Sthompsa		/*
894206763Sbschmidt		 * If we are not transitioning from AUTH then resend the
895172567Sthompsa		 * association request.
896172567Sthompsa		 */
897191746Sthompsa		if (ostate != IEEE80211_S_AUTH)
898191746Sthompsa			ipw_assoc(ic, vap);
899145247Sdamien		break;
900145247Sdamien
901172058Ssam	default:
902145247Sdamien		break;
903145247Sdamien	}
904191746Sthompsa	IEEE80211_LOCK(ic);
905178354Ssam	return ivp->newstate(vap, nstate, arg);
906145247Sdamien}
907145247Sdamien
908145247Sdamien/*
909145247Sdamien * Read 16 bits at address 'addr' from the serial EEPROM.
910145247Sdamien */
911145247Sdamienstatic uint16_t
912145247Sdamienipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
913145247Sdamien{
914145247Sdamien	uint32_t tmp;
915145247Sdamien	uint16_t val;
916145247Sdamien	int n;
917145247Sdamien
918145247Sdamien	/* clock C once before the first command */
919145247Sdamien	IPW_EEPROM_CTL(sc, 0);
920145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
921145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
922145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
923145247Sdamien
924145247Sdamien	/* write start bit (1) */
925145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
926145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
927145247Sdamien
928145247Sdamien	/* write READ opcode (10) */
929145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
930145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
931145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
932145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
933145247Sdamien
934145247Sdamien	/* write address A7-A0 */
935145247Sdamien	for (n = 7; n >= 0; n--) {
936145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
937145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
938145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
939145247Sdamien		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
940145247Sdamien	}
941145247Sdamien
942145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
943145247Sdamien
944145247Sdamien	/* read data Q15-Q0 */
945145247Sdamien	val = 0;
946145247Sdamien	for (n = 15; n >= 0; n--) {
947145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
948145247Sdamien		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
949145247Sdamien		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
950145247Sdamien		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
951145247Sdamien	}
952145247Sdamien
953145247Sdamien	IPW_EEPROM_CTL(sc, 0);
954145247Sdamien
955145247Sdamien	/* clear Chip Select and clock C */
956145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
957145247Sdamien	IPW_EEPROM_CTL(sc, 0);
958145247Sdamien	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
959145247Sdamien
960145247Sdamien	return le16toh(val);
961145247Sdamien}
962145247Sdamien
963300239Savosstatic uint16_t
964300239Savosipw_read_chanmask(struct ipw_softc *sc)
965300239Savos{
966300239Savos	uint16_t val;
967300239Savos
968300239Savos	/* set supported .11b channels (read from EEPROM) */
969300239Savos	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
970300239Savos		val = 0x7ff;	/* default to channels 1-11 */
971300239Savos	val <<= 1;
972300239Savos
973300239Savos	return (val);
974300239Savos}
975300239Savos
976145247Sdamienstatic void
977172567Sthompsaipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
978145247Sdamien{
979145247Sdamien	struct ipw_cmd *cmd;
980145247Sdamien
981145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
982145247Sdamien
983145247Sdamien	cmd = mtod(sbuf->m, struct ipw_cmd *);
984145247Sdamien
985172567Sthompsa	DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
986172567Sthompsa	    ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
987145247Sdamien	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
988145247Sdamien	    le32toh(cmd->status)));
989145247Sdamien
990172567Sthompsa	sc->flags &= ~IPW_FLAG_BUSY;
991145247Sdamien	wakeup(sc);
992145247Sdamien}
993145247Sdamien
994145247Sdamienstatic void
995172567Sthompsaipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
996145247Sdamien{
997178354Ssam#define	IEEESTATE(vap)	ieee80211_state_name[vap->iv_state]
998287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
999178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1000145247Sdamien	uint32_t state;
1001145247Sdamien
1002145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1003145247Sdamien
1004145247Sdamien	state = le32toh(*mtod(sbuf->m, uint32_t *));
1005145247Sdamien
1006145247Sdamien	switch (state) {
1007145247Sdamien	case IPW_STATE_ASSOCIATED:
1008172567Sthompsa		DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1009178354Ssam			IEEESTATE(vap), sc->flags));
1010178354Ssam		/* XXX suppress state change in case the fw auto-associates */
1011178354Ssam		if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1012178354Ssam			DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1013178354Ssam				IEEESTATE(vap), sc->flags));
1014178354Ssam			break;
1015178354Ssam		}
1016178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATING;
1017172567Sthompsa		sc->flags |= IPW_FLAG_ASSOCIATED;
1018145247Sdamien		break;
1019145247Sdamien
1020145247Sdamien	case IPW_STATE_SCANNING:
1021172567Sthompsa		DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1022178354Ssam			IEEESTATE(vap), sc->flags));
1023172567Sthompsa		/*
1024172567Sthompsa		 * NB: Check driver state for association on assoc
1025172567Sthompsa		 * loss as the firmware will immediately start to
1026172567Sthompsa		 * scan and we would treat it as a beacon miss if
1027172567Sthompsa		 * we checked the 802.11 layer state.
1028172567Sthompsa		 */
1029178354Ssam		if (sc->flags & IPW_FLAG_ASSOCIATED) {
1030206767Sbschmidt			IPW_UNLOCK(sc);
1031178354Ssam			/* XXX probably need to issue disassoc to fw */
1032191746Sthompsa			ieee80211_beacon_miss(ic);
1033206767Sbschmidt			IPW_LOCK(sc);
1034178354Ssam		}
1035145247Sdamien		break;
1036145247Sdamien
1037145247Sdamien	case IPW_STATE_SCAN_COMPLETE:
1038172567Sthompsa		/*
1039172567Sthompsa		 * XXX For some reason scan requests generate scan
1040172567Sthompsa		 * started + scan done events before any traffic is
1041172567Sthompsa		 * received (e.g. probe response frames).  We work
1042172567Sthompsa		 * around this by marking the HACK flag and skipping
1043172567Sthompsa		 * the first scan complete event.
1044172567Sthompsa		*/
1045178354Ssam		DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1046178354Ssam			    IEEESTATE(vap), sc->flags));
1047172567Sthompsa		if (sc->flags & IPW_FLAG_HACK) {
1048172567Sthompsa			sc->flags &= ~IPW_FLAG_HACK;
1049172567Sthompsa			break;
1050172567Sthompsa		}
1051172567Sthompsa		if (sc->flags & IPW_FLAG_SCANNING) {
1052206767Sbschmidt			IPW_UNLOCK(sc);
1053191746Sthompsa			ieee80211_scan_done(vap);
1054206767Sbschmidt			IPW_LOCK(sc);
1055172567Sthompsa			sc->flags &= ~IPW_FLAG_SCANNING;
1056172567Sthompsa			sc->sc_scan_timer = 0;
1057172567Sthompsa		}
1058145247Sdamien		break;
1059145247Sdamien
1060145247Sdamien	case IPW_STATE_ASSOCIATION_LOST:
1061172567Sthompsa		DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1062178354Ssam			IEEESTATE(vap), sc->flags));
1063178354Ssam		sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1064206767Sbschmidt		if (vap->iv_state == IEEE80211_S_RUN) {
1065206767Sbschmidt			IPW_UNLOCK(sc);
1066191746Sthompsa			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1067206767Sbschmidt			IPW_LOCK(sc);
1068206767Sbschmidt		}
1069145247Sdamien		break;
1070145247Sdamien
1071172567Sthompsa	case IPW_STATE_DISABLED:
1072178354Ssam		/* XXX? is this right? */
1073206763Sbschmidt		sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1074178354Ssam		    IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1075172567Sthompsa		DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1076178354Ssam			IEEESTATE(vap), sc->flags));
1077172567Sthompsa		break;
1078172567Sthompsa
1079145247Sdamien	case IPW_STATE_RADIO_DISABLED:
1080178354Ssam		device_printf(sc->sc_dev, "radio turned off\n");
1081178354Ssam		ieee80211_notify_radio(ic, 0);
1082172567Sthompsa		ipw_stop_locked(sc);
1083178354Ssam		/* XXX start polling thread to detect radio on */
1084145247Sdamien		break;
1085172567Sthompsa
1086172567Sthompsa	default:
1087172567Sthompsa		DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1088178354Ssam			__func__, state, IEEESTATE(vap), sc->flags));
1089172567Sthompsa		break;
1090145247Sdamien	}
1091172567Sthompsa#undef IEEESTATE
1092145247Sdamien}
1093145247Sdamien
1094145247Sdamien/*
1095172567Sthompsa * Set driver state for current channel.
1096172567Sthompsa */
1097172567Sthompsastatic void
1098172567Sthompsaipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1099172567Sthompsa{
1100287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1101172567Sthompsa
1102172567Sthompsa	ic->ic_curchan = chan;
1103192468Ssam	ieee80211_radiotap_chan_change(ic);
1104172567Sthompsa}
1105172567Sthompsa
1106172567Sthompsa/*
1107145247Sdamien * XXX: Hack to set the current channel to the value advertised in beacons or
1108145247Sdamien * probe responses. Only used during AP detection.
1109145247Sdamien */
1110145247Sdamienstatic void
1111172567Sthompsaipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1112145247Sdamien{
1113287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1114172567Sthompsa	struct ieee80211_channel *c;
1115145247Sdamien	struct ieee80211_frame *wh;
1116145247Sdamien	uint8_t subtype;
1117145247Sdamien	uint8_t *frm, *efrm;
1118145247Sdamien
1119145247Sdamien	wh = mtod(m, struct ieee80211_frame *);
1120145247Sdamien
1121145247Sdamien	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1122145247Sdamien		return;
1123145247Sdamien
1124145247Sdamien	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1125145247Sdamien
1126145247Sdamien	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1127145247Sdamien	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1128145247Sdamien		return;
1129145247Sdamien
1130178354Ssam	/* XXX use ieee80211_parse_beacon */
1131145247Sdamien	frm = (uint8_t *)(wh + 1);
1132145247Sdamien	efrm = mtod(m, uint8_t *) + m->m_len;
1133145247Sdamien
1134145247Sdamien	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1135145247Sdamien	while (frm < efrm) {
1136145247Sdamien		if (*frm == IEEE80211_ELEMID_DSPARMS)
1137145247Sdamien#if IEEE80211_CHAN_MAX < 255
1138145247Sdamien		if (frm[2] <= IEEE80211_CHAN_MAX)
1139145247Sdamien#endif
1140172567Sthompsa		{
1141172567Sthompsa			DPRINTF(("Fixing channel to %d\n", frm[2]));
1142172567Sthompsa			c = ieee80211_find_channel(ic,
1143170530Ssam				ieee80211_ieee2mhz(frm[2], 0),
1144172567Sthompsa				IEEE80211_CHAN_B);
1145172567Sthompsa			if (c == NULL)
1146172567Sthompsa				c = &ic->ic_channels[0];
1147172567Sthompsa			ipw_setcurchan(sc, c);
1148172567Sthompsa		}
1149145247Sdamien
1150145247Sdamien		frm += frm[1] + 2;
1151145247Sdamien	}
1152145247Sdamien}
1153145247Sdamien
1154145247Sdamienstatic void
1155172567Sthompsaipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1156145247Sdamien    struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1157145247Sdamien{
1158287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1159152385Sdamien	struct mbuf *mnew, *m;
1160145247Sdamien	struct ieee80211_node *ni;
1161145247Sdamien	bus_addr_t physaddr;
1162145247Sdamien	int error;
1163192468Ssam	int8_t rssi, nf;
1164145247Sdamien
1165152385Sdamien	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1166152385Sdamien	    status->rssi));
1167152385Sdamien
1168147757Sdamien	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1169147757Sdamien	    le32toh(status->len) > MCLBYTES)
1170147757Sdamien		return;
1171147757Sdamien
1172152385Sdamien	/*
1173152385Sdamien	 * Try to allocate a new mbuf for this ring element and load it before
1174152385Sdamien	 * processing the current mbuf. If the ring element cannot be loaded,
1175152385Sdamien	 * drop the received packet and reuse the old mbuf. In the unlikely
1176152385Sdamien	 * case that the old mbuf can't be reloaded either, explicitly panic.
1177152385Sdamien	 */
1178243857Sglebius	mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1179152385Sdamien	if (mnew == NULL) {
1180287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
1181152385Sdamien		return;
1182152385Sdamien	}
1183152385Sdamien
1184145247Sdamien	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1185145247Sdamien	bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1186145247Sdamien
1187152385Sdamien	error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1188152385Sdamien	    MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1189152385Sdamien	if (error != 0) {
1190152385Sdamien		m_freem(mnew);
1191152385Sdamien
1192152385Sdamien		/* try to reload the old mbuf */
1193152385Sdamien		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1194152385Sdamien		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1195152385Sdamien		    &physaddr, 0);
1196152385Sdamien		if (error != 0) {
1197152385Sdamien			/* very unlikely that it will fail... */
1198152385Sdamien			panic("%s: could not load old rx mbuf",
1199152385Sdamien			    device_get_name(sc->sc_dev));
1200152385Sdamien		}
1201287197Sglebius		counter_u64_add(ic->ic_ierrors, 1);
1202152385Sdamien		return;
1203152385Sdamien	}
1204152385Sdamien
1205152385Sdamien	/*
1206152385Sdamien	 * New mbuf successfully loaded, update Rx ring and continue
1207152385Sdamien	 * processing.
1208152385Sdamien	 */
1209152385Sdamien	m = sbuf->m;
1210152385Sdamien	sbuf->m = mnew;
1211152385Sdamien	sbd->bd->physaddr = htole32(physaddr);
1212145247Sdamien	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1213145247Sdamien
1214192468Ssam	rssi = status->rssi + IPW_RSSI_TO_DBM;
1215192468Ssam	nf = -95;
1216192468Ssam	if (ieee80211_radiotap_active(ic)) {
1217145247Sdamien		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1218145247Sdamien
1219145247Sdamien		tap->wr_flags = 0;
1220192468Ssam		tap->wr_antsignal = rssi;
1221192468Ssam		tap->wr_antnoise = nf;
1222145247Sdamien	}
1223145247Sdamien
1224172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
1225172567Sthompsa		ipw_fix_channel(sc, m);
1226145247Sdamien
1227172567Sthompsa	IPW_UNLOCK(sc);
1228178354Ssam	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1229178354Ssam	if (ni != NULL) {
1230206764Sbschmidt		(void) ieee80211_input(ni, m, rssi - nf, nf);
1231178354Ssam		ieee80211_free_node(ni);
1232178354Ssam	} else
1233206764Sbschmidt		(void) ieee80211_input_all(ic, m, rssi - nf, nf);
1234172567Sthompsa	IPW_LOCK(sc);
1235145247Sdamien
1236145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1237145247Sdamien}
1238145247Sdamien
1239145247Sdamienstatic void
1240145247Sdamienipw_rx_intr(struct ipw_softc *sc)
1241145247Sdamien{
1242145247Sdamien	struct ipw_status *status;
1243145247Sdamien	struct ipw_soft_bd *sbd;
1244145247Sdamien	struct ipw_soft_buf *sbuf;
1245145247Sdamien	uint32_t r, i;
1246145247Sdamien
1247145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1248145247Sdamien		return;
1249145247Sdamien
1250145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1251145247Sdamien
1252145247Sdamien	bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1253145247Sdamien
1254145247Sdamien	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1255145247Sdamien		status = &sc->status_list[i];
1256145247Sdamien		sbd = &sc->srbd_list[i];
1257145247Sdamien		sbuf = sbd->priv;
1258145247Sdamien
1259145247Sdamien		switch (le16toh(status->code) & 0xf) {
1260145247Sdamien		case IPW_STATUS_CODE_COMMAND:
1261172567Sthompsa			ipw_rx_cmd_intr(sc, sbuf);
1262145247Sdamien			break;
1263145247Sdamien
1264145247Sdamien		case IPW_STATUS_CODE_NEWSTATE:
1265172567Sthompsa			ipw_rx_newstate_intr(sc, sbuf);
1266145247Sdamien			break;
1267145247Sdamien
1268145247Sdamien		case IPW_STATUS_CODE_DATA_802_3:
1269145247Sdamien		case IPW_STATUS_CODE_DATA_802_11:
1270172567Sthompsa			ipw_rx_data_intr(sc, status, sbd, sbuf);
1271145247Sdamien			break;
1272145247Sdamien
1273145247Sdamien		case IPW_STATUS_CODE_NOTIFICATION:
1274172567Sthompsa			DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1275172567Sthompsa			    le32toh(status->len), status->flags));
1276178354Ssam			/* XXX maybe drive state machine AUTH->ASSOC? */
1277145247Sdamien			break;
1278145247Sdamien
1279145247Sdamien		default:
1280172567Sthompsa			device_printf(sc->sc_dev, "unexpected status code %u\n",
1281145247Sdamien			    le16toh(status->code));
1282145247Sdamien		}
1283145247Sdamien
1284145247Sdamien		/* firmware was killed, stop processing received frames */
1285145247Sdamien		if (!(sc->flags & IPW_FLAG_FW_INITED))
1286145247Sdamien			return;
1287145247Sdamien
1288145247Sdamien		sbd->bd->flags = 0;
1289145247Sdamien	}
1290145247Sdamien
1291145247Sdamien	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1292145247Sdamien
1293145247Sdamien	/* kick the firmware */
1294145247Sdamien	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1295145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1296145247Sdamien}
1297145247Sdamien
1298145247Sdamienstatic void
1299145247Sdamienipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1300145247Sdamien{
1301145247Sdamien	struct ipw_soft_hdr *shdr;
1302145247Sdamien	struct ipw_soft_buf *sbuf;
1303145247Sdamien
1304145247Sdamien	switch (sbd->type) {
1305145247Sdamien	case IPW_SBD_TYPE_COMMAND:
1306145247Sdamien		bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1307145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1308145247Sdamien		bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1309145247Sdamien		break;
1310145247Sdamien
1311145247Sdamien	case IPW_SBD_TYPE_HEADER:
1312145247Sdamien		shdr = sbd->priv;
1313145247Sdamien		bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1314145247Sdamien		bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1315145247Sdamien		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1316145247Sdamien		break;
1317145247Sdamien
1318145247Sdamien	case IPW_SBD_TYPE_DATA:
1319145247Sdamien		sbuf = sbd->priv;
1320145247Sdamien		bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1321145247Sdamien		    BUS_DMASYNC_POSTWRITE);
1322145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1323145247Sdamien		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1324145247Sdamien
1325343907Savos		ieee80211_tx_complete(sbuf->ni, sbuf->m, 0/*XXX*/);
1326145247Sdamien
1327145247Sdamien		sc->sc_tx_timer = 0;
1328145247Sdamien		break;
1329145247Sdamien	}
1330145247Sdamien
1331145247Sdamien	sbd->type = IPW_SBD_TYPE_NOASSOC;
1332145247Sdamien}
1333145247Sdamien
1334145247Sdamienstatic void
1335145247Sdamienipw_tx_intr(struct ipw_softc *sc)
1336145247Sdamien{
1337145247Sdamien	struct ipw_soft_bd *sbd;
1338145247Sdamien	uint32_t r, i;
1339145247Sdamien
1340145247Sdamien	if (!(sc->flags & IPW_FLAG_FW_INITED))
1341145247Sdamien		return;
1342145247Sdamien
1343145247Sdamien	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1344145247Sdamien
1345145247Sdamien	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1346145247Sdamien		sbd = &sc->stbd_list[i];
1347145247Sdamien		ipw_release_sbd(sc, sbd);
1348145247Sdamien		sc->txfree++;
1349145247Sdamien	}
1350145247Sdamien
1351145247Sdamien	/* remember what the firmware has processed */
1352145247Sdamien	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1353145247Sdamien
1354287197Sglebius	ipw_start(sc);
1355145247Sdamien}
1356145247Sdamien
1357145247Sdamienstatic void
1358191746Sthompsaipw_fatal_error_intr(struct ipw_softc *sc)
1359191746Sthompsa{
1360287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1361191956Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1362191746Sthompsa
1363191746Sthompsa	device_printf(sc->sc_dev, "firmware error\n");
1364206767Sbschmidt	if (vap != NULL) {
1365206767Sbschmidt		IPW_UNLOCK(sc);
1366191956Sthompsa		ieee80211_cancel_scan(vap);
1367206767Sbschmidt		IPW_LOCK(sc);
1368206767Sbschmidt	}
1369191746Sthompsa	ieee80211_runtask(ic, &sc->sc_init_task);
1370191746Sthompsa}
1371191746Sthompsa
1372191746Sthompsastatic void
1373145247Sdamienipw_intr(void *arg)
1374145247Sdamien{
1375145247Sdamien	struct ipw_softc *sc = arg;
1376145247Sdamien	uint32_t r;
1377145247Sdamien
1378172567Sthompsa	IPW_LOCK(sc);
1379145247Sdamien
1380191746Sthompsa	r = CSR_READ_4(sc, IPW_CSR_INTR);
1381191746Sthompsa	if (r == 0 || r == 0xffffffff)
1382191746Sthompsa		goto done;
1383145247Sdamien
1384145247Sdamien	/* disable interrupts */
1385145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1386145247Sdamien
1387156599Sdamien	/* acknowledge all interrupts */
1388156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1389156599Sdamien
1390145247Sdamien	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1391191746Sthompsa		ipw_fatal_error_intr(sc);
1392191746Sthompsa		goto done;
1393145247Sdamien	}
1394145247Sdamien
1395156599Sdamien	if (r & IPW_INTR_FW_INIT_DONE)
1396156599Sdamien		wakeup(sc);
1397145247Sdamien
1398145247Sdamien	if (r & IPW_INTR_RX_TRANSFER)
1399145247Sdamien		ipw_rx_intr(sc);
1400145247Sdamien
1401145247Sdamien	if (r & IPW_INTR_TX_TRANSFER)
1402145247Sdamien		ipw_tx_intr(sc);
1403145247Sdamien
1404145247Sdamien	/* re-enable interrupts */
1405145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1406191746Sthompsadone:
1407172567Sthompsa	IPW_UNLOCK(sc);
1408145247Sdamien}
1409145247Sdamien
1410145247Sdamienstatic void
1411145247Sdamienipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1412145247Sdamien{
1413145247Sdamien	if (error != 0)
1414145247Sdamien		return;
1415145247Sdamien
1416145247Sdamien	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1417145247Sdamien
1418145247Sdamien	*(bus_addr_t *)arg = segs[0].ds_addr;
1419145247Sdamien}
1420145247Sdamien
1421172567Sthompsastatic const char *
1422172567Sthompsaipw_cmdname(int cmd)
1423172567Sthompsa{
1424172567Sthompsa	static const struct {
1425172567Sthompsa		int	cmd;
1426172567Sthompsa		const char *name;
1427172567Sthompsa	} cmds[] = {
1428172567Sthompsa		{ IPW_CMD_ADD_MULTICAST,	"ADD_MULTICAST" },
1429172567Sthompsa		{ IPW_CMD_BROADCAST_SCAN,	"BROADCAST_SCAN" },
1430172567Sthompsa		{ IPW_CMD_DISABLE,		"DISABLE" },
1431172567Sthompsa		{ IPW_CMD_DISABLE_PHY,		"DISABLE_PHY" },
1432172567Sthompsa		{ IPW_CMD_ENABLE,		"ENABLE" },
1433172567Sthompsa		{ IPW_CMD_PREPARE_POWER_DOWN,	"PREPARE_POWER_DOWN" },
1434172567Sthompsa		{ IPW_CMD_SET_BASIC_TX_RATES,	"SET_BASIC_TX_RATES" },
1435172567Sthompsa		{ IPW_CMD_SET_BEACON_INTERVAL,	"SET_BEACON_INTERVAL" },
1436172567Sthompsa		{ IPW_CMD_SET_CHANNEL,		"SET_CHANNEL" },
1437172567Sthompsa		{ IPW_CMD_SET_CONFIGURATION,	"SET_CONFIGURATION" },
1438172567Sthompsa		{ IPW_CMD_SET_DESIRED_BSSID,	"SET_DESIRED_BSSID" },
1439172567Sthompsa		{ IPW_CMD_SET_ESSID,		"SET_ESSID" },
1440172567Sthompsa		{ IPW_CMD_SET_FRAG_THRESHOLD,	"SET_FRAG_THRESHOLD" },
1441172567Sthompsa		{ IPW_CMD_SET_MAC_ADDRESS,	"SET_MAC_ADDRESS" },
1442172567Sthompsa		{ IPW_CMD_SET_MANDATORY_BSSID,	"SET_MANDATORY_BSSID" },
1443172567Sthompsa		{ IPW_CMD_SET_MODE,		"SET_MODE" },
1444172567Sthompsa		{ IPW_CMD_SET_MSDU_TX_RATES,	"SET_MSDU_TX_RATES" },
1445172567Sthompsa		{ IPW_CMD_SET_POWER_MODE,	"SET_POWER_MODE" },
1446172567Sthompsa		{ IPW_CMD_SET_RTS_THRESHOLD,	"SET_RTS_THRESHOLD" },
1447172567Sthompsa		{ IPW_CMD_SET_SCAN_OPTIONS,	"SET_SCAN_OPTIONS" },
1448172567Sthompsa		{ IPW_CMD_SET_SECURITY_INFO,	"SET_SECURITY_INFO" },
1449172567Sthompsa		{ IPW_CMD_SET_TX_POWER_INDEX,	"SET_TX_POWER_INDEX" },
1450172567Sthompsa		{ IPW_CMD_SET_TX_RATES,		"SET_TX_RATES" },
1451172567Sthompsa		{ IPW_CMD_SET_WEP_FLAGS,	"SET_WEP_FLAGS" },
1452172567Sthompsa		{ IPW_CMD_SET_WEP_KEY,		"SET_WEP_KEY" },
1453172567Sthompsa		{ IPW_CMD_SET_WEP_KEY_INDEX,	"SET_WEP_KEY_INDEX" },
1454172567Sthompsa		{ IPW_CMD_SET_WPA_IE,		"SET_WPA_IE" },
1455172567Sthompsa
1456172567Sthompsa	};
1457172567Sthompsa	static char buf[12];
1458172567Sthompsa	int i;
1459172567Sthompsa
1460288087Sadrian	for (i = 0; i < nitems(cmds); i++)
1461172567Sthompsa		if (cmds[i].cmd == cmd)
1462172567Sthompsa			return cmds[i].name;
1463172567Sthompsa	snprintf(buf, sizeof(buf), "%u", cmd);
1464172567Sthompsa	return buf;
1465172567Sthompsa}
1466172567Sthompsa
1467145247Sdamien/*
1468145247Sdamien * Send a command to the firmware and wait for the acknowledgement.
1469145247Sdamien */
1470145247Sdamienstatic int
1471145247Sdamienipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1472145247Sdamien{
1473145247Sdamien	struct ipw_soft_bd *sbd;
1474145247Sdamien	bus_addr_t physaddr;
1475145247Sdamien	int error;
1476145247Sdamien
1477178354Ssam	IPW_LOCK_ASSERT(sc);
1478178354Ssam
1479172567Sthompsa	if (sc->flags & IPW_FLAG_BUSY) {
1480172567Sthompsa		device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1481172567Sthompsa			__func__, ipw_cmdname(type));
1482172567Sthompsa		return EAGAIN;
1483172567Sthompsa	}
1484172567Sthompsa	sc->flags |= IPW_FLAG_BUSY;
1485172567Sthompsa
1486145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1487145247Sdamien
1488145247Sdamien	error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1489145247Sdamien	    sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1490145247Sdamien	if (error != 0) {
1491145247Sdamien		device_printf(sc->sc_dev, "could not map command DMA memory\n");
1492172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1493145247Sdamien		return error;
1494145247Sdamien	}
1495145247Sdamien
1496145247Sdamien	sc->cmd.type = htole32(type);
1497145247Sdamien	sc->cmd.subtype = 0;
1498145247Sdamien	sc->cmd.len = htole32(len);
1499145247Sdamien	sc->cmd.seq = 0;
1500152637Sdamien	memcpy(sc->cmd.data, data, len);
1501145247Sdamien
1502145247Sdamien	sbd->type = IPW_SBD_TYPE_COMMAND;
1503145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1504145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1505145247Sdamien	sbd->bd->nfrag = 1;
1506145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1507145247Sdamien	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1508145247Sdamien
1509145247Sdamien	bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1510145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1511145247Sdamien
1512172567Sthompsa#ifdef IPW_DEBUG
1513172567Sthompsa	if (ipw_debug >= 4) {
1514172567Sthompsa		printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1515172567Sthompsa		    0, 0, len);
1516172567Sthompsa		/* Print the data buffer in the higher debug level */
1517172567Sthompsa		if (ipw_debug >= 9 && len > 0) {
1518172567Sthompsa			printf(" data: 0x");
1519172567Sthompsa			for (int i = 1; i <= len; i++)
1520174269Swkoszek				printf("%1D", (u_char *)data + len - i, "");
1521172567Sthompsa		}
1522172567Sthompsa		printf("\n");
1523172567Sthompsa	}
1524172567Sthompsa#endif
1525145247Sdamien
1526145247Sdamien	/* kick firmware */
1527145247Sdamien	sc->txfree--;
1528145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1529145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1530145247Sdamien
1531145247Sdamien	/* wait at most one second for command to complete */
1532172567Sthompsa	error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1533172567Sthompsa	if (error != 0) {
1534172567Sthompsa		device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1535172567Sthompsa		    __func__, ipw_cmdname(type), error);
1536172567Sthompsa		sc->flags &= ~IPW_FLAG_BUSY;
1537172567Sthompsa		return (error);
1538172567Sthompsa	}
1539172567Sthompsa	return (0);
1540145247Sdamien}
1541145247Sdamien
1542145247Sdamienstatic int
1543287197Sglebiusipw_tx_start(struct ipw_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1544145247Sdamien{
1545287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1546192468Ssam	struct ieee80211vap *vap = ni->ni_vap;
1547145247Sdamien	struct ieee80211_frame *wh;
1548145247Sdamien	struct ipw_soft_bd *sbd;
1549145247Sdamien	struct ipw_soft_hdr *shdr;
1550145247Sdamien	struct ipw_soft_buf *sbuf;
1551145247Sdamien	struct ieee80211_key *k;
1552145247Sdamien	struct mbuf *mnew;
1553145247Sdamien	bus_dma_segment_t segs[IPW_MAX_NSEG];
1554145247Sdamien	bus_addr_t physaddr;
1555145247Sdamien	int nsegs, error, i;
1556145247Sdamien
1557145247Sdamien	wh = mtod(m0, struct ieee80211_frame *);
1558145247Sdamien
1559260444Skevlo	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1560178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1561147806Ssam		if (k == NULL) {
1562147806Ssam			m_freem(m0);
1563145247Sdamien			return ENOBUFS;
1564147806Ssam		}
1565145247Sdamien		/* packet header may have moved, reset our local pointer */
1566145247Sdamien		wh = mtod(m0, struct ieee80211_frame *);
1567145247Sdamien	}
1568145247Sdamien
1569192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1570145247Sdamien		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1571145247Sdamien
1572145247Sdamien		tap->wt_flags = 0;
1573145247Sdamien
1574192468Ssam		ieee80211_radiotap_tx(vap, m0);
1575145247Sdamien	}
1576145247Sdamien
1577145247Sdamien	shdr = SLIST_FIRST(&sc->free_shdr);
1578145247Sdamien	sbuf = SLIST_FIRST(&sc->free_sbuf);
1579145247Sdamien	KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1580145247Sdamien
1581145247Sdamien	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1582145247Sdamien	shdr->hdr.subtype = 0;
1583260444Skevlo	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) ? 1 : 0;
1584145247Sdamien	shdr->hdr.encrypt = 0;
1585145247Sdamien	shdr->hdr.keyidx = 0;
1586145247Sdamien	shdr->hdr.keysz = 0;
1587145247Sdamien	shdr->hdr.fragmentsz = 0;
1588145247Sdamien	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1589145247Sdamien	if (ic->ic_opmode == IEEE80211_M_STA)
1590145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1591145247Sdamien	else
1592145247Sdamien		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1593145247Sdamien
1594145247Sdamien	/* trim IEEE802.11 header */
1595145247Sdamien	m_adj(m0, sizeof (struct ieee80211_frame));
1596145247Sdamien
1597145247Sdamien	error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1598145247Sdamien	    &nsegs, 0);
1599145247Sdamien	if (error != 0 && error != EFBIG) {
1600145247Sdamien		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1601145247Sdamien		    error);
1602145247Sdamien		m_freem(m0);
1603145247Sdamien		return error;
1604145247Sdamien	}
1605145247Sdamien	if (error != 0) {
1606243857Sglebius		mnew = m_defrag(m0, M_NOWAIT);
1607145247Sdamien		if (mnew == NULL) {
1608145247Sdamien			device_printf(sc->sc_dev,
1609145247Sdamien			    "could not defragment mbuf\n");
1610145247Sdamien			m_freem(m0);
1611145247Sdamien			return ENOBUFS;
1612145247Sdamien		}
1613145247Sdamien		m0 = mnew;
1614145247Sdamien
1615145247Sdamien		error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1616145247Sdamien		    segs, &nsegs, 0);
1617145247Sdamien		if (error != 0) {
1618145247Sdamien			device_printf(sc->sc_dev,
1619145247Sdamien			    "could not map mbuf (error %d)\n", error);
1620145247Sdamien			m_freem(m0);
1621145247Sdamien			return error;
1622145247Sdamien		}
1623145247Sdamien	}
1624145247Sdamien
1625145247Sdamien	error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1626145247Sdamien	    sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1627145247Sdamien	if (error != 0) {
1628145247Sdamien		device_printf(sc->sc_dev, "could not map header DMA memory\n");
1629145247Sdamien		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1630145247Sdamien		m_freem(m0);
1631145247Sdamien		return error;
1632145247Sdamien	}
1633145247Sdamien
1634145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1635145247Sdamien	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1636145247Sdamien
1637145247Sdamien	sbd = &sc->stbd_list[sc->txcur];
1638145247Sdamien	sbd->type = IPW_SBD_TYPE_HEADER;
1639145247Sdamien	sbd->priv = shdr;
1640145247Sdamien	sbd->bd->physaddr = htole32(physaddr);
1641145247Sdamien	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1642145247Sdamien	sbd->bd->nfrag = 1 + nsegs;
1643145247Sdamien	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1644145247Sdamien	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1645145247Sdamien
1646145247Sdamien	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1647145247Sdamien	    shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1648145247Sdamien	    shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1649145247Sdamien	    ":"));
1650145247Sdamien
1651145247Sdamien	sc->txfree--;
1652145247Sdamien	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1653145247Sdamien
1654145247Sdamien	sbuf->m = m0;
1655145247Sdamien	sbuf->ni = ni;
1656145247Sdamien
1657145247Sdamien	for (i = 0; i < nsegs; i++) {
1658145247Sdamien		sbd = &sc->stbd_list[sc->txcur];
1659145247Sdamien
1660145247Sdamien		sbd->bd->physaddr = htole32(segs[i].ds_addr);
1661145247Sdamien		sbd->bd->len = htole32(segs[i].ds_len);
1662145247Sdamien		sbd->bd->nfrag = 0;
1663145247Sdamien		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1664145247Sdamien		if (i == nsegs - 1) {
1665145247Sdamien			sbd->type = IPW_SBD_TYPE_DATA;
1666145247Sdamien			sbd->priv = sbuf;
1667145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1668145247Sdamien		} else {
1669145247Sdamien			sbd->type = IPW_SBD_TYPE_NOASSOC;
1670145247Sdamien			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1671145247Sdamien		}
1672145247Sdamien
1673172574Sthompsa		DPRINTFN(5, ("sending fragment (%d)\n", i));
1674145247Sdamien
1675145247Sdamien		sc->txfree--;
1676145247Sdamien		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1677145247Sdamien	}
1678145247Sdamien
1679145247Sdamien	bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1680145247Sdamien	bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1681145247Sdamien	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1682145247Sdamien
1683145247Sdamien	/* kick firmware */
1684145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1685145247Sdamien
1686145247Sdamien	return 0;
1687145247Sdamien}
1688145247Sdamien
1689178354Ssamstatic int
1690178354Ssamipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1691178354Ssam	const struct ieee80211_bpf_params *params)
1692178354Ssam{
1693178354Ssam	/* no support; just discard */
1694178354Ssam	m_freem(m);
1695178354Ssam	ieee80211_free_node(ni);
1696178354Ssam	return 0;
1697178354Ssam}
1698178354Ssam
1699287197Sglebiusstatic int
1700287197Sglebiusipw_transmit(struct ieee80211com *ic, struct mbuf *m)
1701145247Sdamien{
1702287197Sglebius	struct ipw_softc *sc = ic->ic_softc;
1703287197Sglebius	int error;
1704172567Sthompsa
1705172567Sthompsa	IPW_LOCK(sc);
1706287197Sglebius	if ((sc->flags & IPW_FLAG_RUNNING) == 0) {
1707287197Sglebius		IPW_UNLOCK(sc);
1708287197Sglebius		return (ENXIO);
1709287197Sglebius	}
1710287197Sglebius	error = mbufq_enqueue(&sc->sc_snd, m);
1711287197Sglebius	if (error) {
1712287197Sglebius		IPW_UNLOCK(sc);
1713287197Sglebius		return (error);
1714287197Sglebius	}
1715287197Sglebius	ipw_start(sc);
1716172567Sthompsa	IPW_UNLOCK(sc);
1717287197Sglebius	return (0);
1718172567Sthompsa}
1719172567Sthompsa
1720172567Sthompsastatic void
1721287197Sglebiusipw_start(struct ipw_softc *sc)
1722172567Sthompsa{
1723145247Sdamien	struct ieee80211_node *ni;
1724178354Ssam	struct mbuf *m;
1725145247Sdamien
1726172567Sthompsa	IPW_LOCK_ASSERT(sc);
1727145247Sdamien
1728339976Sglebius	while (sc->txfree >= 1 + IPW_MAX_NSEG &&
1729287197Sglebius	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1730178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1731287197Sglebius		if (ipw_tx_start(sc, m, ni) != 0) {
1732287197Sglebius			if_inc_counter(ni->ni_vap->iv_ifp,
1733287197Sglebius			    IFCOUNTER_OERRORS, 1);
1734145247Sdamien			ieee80211_free_node(ni);
1735145247Sdamien			break;
1736145247Sdamien		}
1737145247Sdamien		/* start watchdog timer */
1738145247Sdamien		sc->sc_tx_timer = 5;
1739145247Sdamien	}
1740145247Sdamien}
1741145247Sdamien
1742145247Sdamienstatic void
1743172567Sthompsaipw_watchdog(void *arg)
1744145247Sdamien{
1745172567Sthompsa	struct ipw_softc *sc = arg;
1746287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
1747145247Sdamien
1748172567Sthompsa	IPW_LOCK_ASSERT(sc);
1749156599Sdamien
1750145247Sdamien	if (sc->sc_tx_timer > 0) {
1751145247Sdamien		if (--sc->sc_tx_timer == 0) {
1752287197Sglebius			device_printf(sc->sc_dev, "device timeout\n");
1753287197Sglebius			counter_u64_add(ic->ic_oerrors, 1);
1754178354Ssam			taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1755145247Sdamien		}
1756145247Sdamien	}
1757172567Sthompsa	if (sc->sc_scan_timer > 0) {
1758172567Sthompsa		if (--sc->sc_scan_timer == 0) {
1759172567Sthompsa			DPRINTFN(3, ("Scan timeout\n"));
1760172567Sthompsa			/* End the scan */
1761172567Sthompsa			if (sc->flags & IPW_FLAG_SCANNING) {
1762206767Sbschmidt				IPW_UNLOCK(sc);
1763178354Ssam				ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1764206767Sbschmidt				IPW_LOCK(sc);
1765172567Sthompsa				sc->flags &= ~IPW_FLAG_SCANNING;
1766172567Sthompsa			}
1767172567Sthompsa		}
1768172567Sthompsa	}
1769287197Sglebius	if (sc->flags & IPW_FLAG_RUNNING)
1770172567Sthompsa		callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1771145247Sdamien}
1772145247Sdamien
1773287197Sglebiusstatic void
1774287197Sglebiusipw_parent(struct ieee80211com *ic)
1775145247Sdamien{
1776287197Sglebius	struct ipw_softc *sc = ic->ic_softc;
1777287197Sglebius	int startall = 0;
1778145247Sdamien
1779287197Sglebius	IPW_LOCK(sc);
1780287197Sglebius	if (ic->ic_nrunning > 0) {
1781287197Sglebius		if (!(sc->flags & IPW_FLAG_RUNNING)) {
1782287197Sglebius			ipw_init_locked(sc);
1783287197Sglebius			startall = 1;
1784145247Sdamien		}
1785287197Sglebius	} else if (sc->flags & IPW_FLAG_RUNNING)
1786287197Sglebius		ipw_stop_locked(sc);
1787287197Sglebius	IPW_UNLOCK(sc);
1788287197Sglebius	if (startall)
1789287197Sglebius		ieee80211_start_all(ic);
1790145247Sdamien}
1791145247Sdamien
1792145247Sdamienstatic void
1793145247Sdamienipw_stop_master(struct ipw_softc *sc)
1794145247Sdamien{
1795156599Sdamien	uint32_t tmp;
1796145247Sdamien	int ntries;
1797145247Sdamien
1798145247Sdamien	/* disable interrupts */
1799145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1800145247Sdamien
1801145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1802145247Sdamien	for (ntries = 0; ntries < 50; ntries++) {
1803145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1804145247Sdamien			break;
1805145247Sdamien		DELAY(10);
1806145247Sdamien	}
1807145247Sdamien	if (ntries == 50)
1808145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for master\n");
1809145247Sdamien
1810156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1811156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1812145247Sdamien
1813172567Sthompsa	/* Clear all flags except the following */
1814172567Sthompsa	sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1815145247Sdamien}
1816145247Sdamien
1817145247Sdamienstatic int
1818145247Sdamienipw_reset(struct ipw_softc *sc)
1819145247Sdamien{
1820156599Sdamien	uint32_t tmp;
1821145247Sdamien	int ntries;
1822145247Sdamien
1823145247Sdamien	ipw_stop_master(sc);
1824145247Sdamien
1825145247Sdamien	/* move adapter to D0 state */
1826156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1827156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1828145247Sdamien
1829145247Sdamien	/* wait for clock stabilization */
1830145247Sdamien	for (ntries = 0; ntries < 1000; ntries++) {
1831145247Sdamien		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1832145247Sdamien			break;
1833145247Sdamien		DELAY(200);
1834145247Sdamien	}
1835145247Sdamien	if (ntries == 1000)
1836145247Sdamien		return EIO;
1837145247Sdamien
1838156599Sdamien	tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1839156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1840145247Sdamien
1841145247Sdamien	DELAY(10);
1842145247Sdamien
1843156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1844156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1845145247Sdamien
1846145247Sdamien	return 0;
1847145247Sdamien}
1848145247Sdamien
1849172567Sthompsastatic int
1850172567Sthompsaipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1851172567Sthompsa{
1852172567Sthompsa	int ms = hz < 1000 ? 1 : hz/10;
1853172567Sthompsa	int i, error;
1854172567Sthompsa
1855172567Sthompsa	for (i = 0; i < 100; i++) {
1856172567Sthompsa		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1857172567Sthompsa			return 0;
1858172567Sthompsa		error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1859172567Sthompsa		if (error == 0 || error != EWOULDBLOCK)
1860172567Sthompsa			return 0;
1861172567Sthompsa	}
1862172567Sthompsa	DPRINTF(("%s: timeout waiting for %s\n",
1863172567Sthompsa		__func__, waitfor ? "disable" : "enable"));
1864172567Sthompsa	return ETIMEDOUT;
1865172567Sthompsa}
1866172567Sthompsa
1867172567Sthompsastatic int
1868172567Sthompsaipw_enable(struct ipw_softc *sc)
1869172567Sthompsa{
1870172567Sthompsa	int error;
1871172567Sthompsa
1872172567Sthompsa	if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1873172567Sthompsa		DPRINTF(("Enable adapter\n"));
1874172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1875172567Sthompsa		if (error != 0)
1876172567Sthompsa			return error;
1877172567Sthompsa		error = ipw_waitfordisable(sc, 0);
1878172567Sthompsa		if (error != 0)
1879172567Sthompsa			return error;
1880172567Sthompsa		sc->flags |= IPW_FLAG_ENABLED;
1881172567Sthompsa	}
1882172567Sthompsa	return 0;
1883172567Sthompsa}
1884172567Sthompsa
1885172567Sthompsastatic int
1886172567Sthompsaipw_disable(struct ipw_softc *sc)
1887172567Sthompsa{
1888172567Sthompsa	int error;
1889172567Sthompsa
1890172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
1891172567Sthompsa		DPRINTF(("Disable adapter\n"));
1892172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1893172567Sthompsa		if (error != 0)
1894172567Sthompsa			return error;
1895172567Sthompsa		error = ipw_waitfordisable(sc, 1);
1896172567Sthompsa		if (error != 0)
1897172567Sthompsa			return error;
1898172567Sthompsa		sc->flags &= ~IPW_FLAG_ENABLED;
1899172567Sthompsa	}
1900172567Sthompsa	return 0;
1901172567Sthompsa}
1902172567Sthompsa
1903145247Sdamien/*
1904145247Sdamien * Upload the microcode to the device.
1905145247Sdamien */
1906145247Sdamienstatic int
1907156599Sdamienipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1908145247Sdamien{
1909145247Sdamien	int ntries;
1910145247Sdamien
1911145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1912145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1913145247Sdamien
1914145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1915145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1916145247Sdamien
1917145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1918145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1919145247Sdamien
1920145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1921145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1922145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x40);
1923145247Sdamien
1924145247Sdamien	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1925145247Sdamien
1926145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1927145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1928145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1929145247Sdamien
1930145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0703);
1931145247Sdamien	MEM_WRITE_2(sc, 0x220000, 0x0707);
1932145247Sdamien
1933145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1934145247Sdamien	MEM_WRITE_1(sc, 0x210014, 0x72);
1935145247Sdamien
1936145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x00);
1937145247Sdamien	MEM_WRITE_1(sc, 0x210000, 0x80);
1938145247Sdamien
1939145247Sdamien	for (ntries = 0; ntries < 10; ntries++) {
1940145247Sdamien		if (MEM_READ_1(sc, 0x210000) & 1)
1941145247Sdamien			break;
1942145247Sdamien		DELAY(10);
1943145247Sdamien	}
1944145247Sdamien	if (ntries == 10) {
1945145247Sdamien		device_printf(sc->sc_dev,
1946145247Sdamien		    "timeout waiting for ucode to initialize\n");
1947145247Sdamien		return EIO;
1948145247Sdamien	}
1949145247Sdamien
1950145247Sdamien	MEM_WRITE_4(sc, 0x3000e0, 0);
1951145247Sdamien
1952145247Sdamien	return 0;
1953145247Sdamien}
1954145247Sdamien
1955145247Sdamien/* set of macros to handle unaligned little endian data in firmware image */
1956145247Sdamien#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1957145247Sdamien#define GETLE16(p) ((p)[0] | (p)[1] << 8)
1958145247Sdamienstatic int
1959156599Sdamienipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
1960145247Sdamien{
1961156599Sdamien	const uint8_t *p, *end;
1962156599Sdamien	uint32_t tmp, dst;
1963145247Sdamien	uint16_t len;
1964145247Sdamien	int error;
1965145247Sdamien
1966145247Sdamien	p = fw;
1967145247Sdamien	end = fw + size;
1968145247Sdamien	while (p < end) {
1969145247Sdamien		dst = GETLE32(p); p += 4;
1970145247Sdamien		len = GETLE16(p); p += 2;
1971145247Sdamien
1972145247Sdamien		ipw_write_mem_1(sc, dst, p, len);
1973145247Sdamien		p += len;
1974145247Sdamien	}
1975145247Sdamien
1976145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
1977145247Sdamien	    IPW_IO_LED_OFF);
1978145247Sdamien
1979145247Sdamien	/* enable interrupts */
1980145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1981145247Sdamien
1982145247Sdamien	/* kick the firmware */
1983145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1984145247Sdamien
1985156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1986156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
1987145247Sdamien
1988145247Sdamien	/* wait at most one second for firmware initialization to complete */
1989145247Sdamien	if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
1990145247Sdamien		device_printf(sc->sc_dev, "timeout waiting for firmware "
1991145247Sdamien		    "initialization to complete\n");
1992145247Sdamien		return error;
1993145247Sdamien	}
1994145247Sdamien
1995156599Sdamien	tmp = CSR_READ_4(sc, IPW_CSR_IO);
1996156599Sdamien	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
1997156599Sdamien	    IPW_IO_GPIO3_MASK);
1998145247Sdamien
1999145247Sdamien	return 0;
2000145247Sdamien}
2001145247Sdamien
2002145247Sdamienstatic int
2003172567Sthompsaipw_setwepkeys(struct ipw_softc *sc)
2004172567Sthompsa{
2005287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2006178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2007172567Sthompsa	struct ipw_wep_key wepkey;
2008172567Sthompsa	struct ieee80211_key *wk;
2009172567Sthompsa	int error, i;
2010172567Sthompsa
2011172567Sthompsa	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2012178354Ssam		wk = &vap->iv_nw_keys[i];
2013172567Sthompsa
2014172567Sthompsa		if (wk->wk_cipher == NULL ||
2015172567Sthompsa		    wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2016172567Sthompsa			continue;
2017172567Sthompsa
2018172567Sthompsa		wepkey.idx = i;
2019172567Sthompsa		wepkey.len = wk->wk_keylen;
2020172567Sthompsa		memset(wepkey.key, 0, sizeof wepkey.key);
2021172567Sthompsa		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2022172567Sthompsa		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2023172567Sthompsa		    wepkey.len));
2024172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2025172567Sthompsa		    sizeof wepkey);
2026172567Sthompsa		if (error != 0)
2027172567Sthompsa			return error;
2028172567Sthompsa	}
2029172567Sthompsa	return 0;
2030172567Sthompsa}
2031172567Sthompsa
2032172567Sthompsastatic int
2033172567Sthompsaipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2034172567Sthompsa{
2035172567Sthompsa	struct ipw_wpa_ie wpaie;
2036172567Sthompsa
2037172567Sthompsa	memset(&wpaie, 0, sizeof(wpaie));
2038172567Sthompsa	wpaie.len = htole32(ielen);
2039172567Sthompsa	/* XXX verify length */
2040172567Sthompsa	memcpy(&wpaie.ie, ie, ielen);
2041172567Sthompsa	DPRINTF(("Setting WPA IE\n"));
2042172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2043172567Sthompsa}
2044172567Sthompsa
2045172567Sthompsastatic int
2046172567Sthompsaipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2047172567Sthompsa{
2048172567Sthompsa	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2049172567Sthompsa
2050172567Sthompsa	if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2051172567Sthompsa		DPRINTF(("Setting mandatory BSSID to null\n"));
2052172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2053172567Sthompsa	} else {
2054172567Sthompsa		DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2055172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2056172567Sthompsa			bssid, IEEE80211_ADDR_LEN);
2057172567Sthompsa	}
2058172567Sthompsa}
2059172567Sthompsa
2060172567Sthompsastatic int
2061172567Sthompsaipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2062172567Sthompsa{
2063172567Sthompsa	if (ssidlen == 0) {
2064172567Sthompsa		/*
2065172567Sthompsa		 * A bug in the firmware breaks the ``don't associate''
2066172567Sthompsa		 * bit in the scan options command.  To compensate for
2067172567Sthompsa		 * this install a bogus ssid when no ssid is specified
2068172567Sthompsa		 * so the firmware won't try to associate.
2069172567Sthompsa		 */
2070172567Sthompsa		DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2071172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2072172567Sthompsa			"\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2073172567Sthompsa			"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2074172567Sthompsa			"\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2075172567Sthompsa			"\x3c\x3d", IEEE80211_NWID_LEN);
2076172567Sthompsa	} else {
2077172567Sthompsa#ifdef IPW_DEBUG
2078172567Sthompsa		if (ipw_debug > 0) {
2079172567Sthompsa			printf("Setting ESSID to ");
2080172567Sthompsa			ieee80211_print_essid(ssid, ssidlen);
2081172567Sthompsa			printf("\n");
2082172567Sthompsa		}
2083172567Sthompsa#endif
2084172567Sthompsa		return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2085172567Sthompsa	}
2086172567Sthompsa}
2087172567Sthompsa
2088172567Sthompsastatic int
2089172567Sthompsaipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2090172567Sthompsa{
2091172567Sthompsa	struct ipw_scan_options opts;
2092172567Sthompsa
2093172567Sthompsa	DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2094172567Sthompsa	opts.channels = htole32(chanmask);
2095172567Sthompsa	opts.flags = htole32(flags);
2096172567Sthompsa	return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2097172567Sthompsa}
2098172567Sthompsa
2099172567Sthompsastatic int
2100172567Sthompsaipw_scan(struct ipw_softc *sc)
2101172567Sthompsa{
2102172567Sthompsa	uint32_t params;
2103172567Sthompsa	int error;
2104172567Sthompsa
2105172567Sthompsa	DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2106172567Sthompsa
2107172567Sthompsa	if (sc->flags & IPW_FLAG_SCANNING)
2108172567Sthompsa		return (EBUSY);
2109172567Sthompsa	sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2110172567Sthompsa
2111172567Sthompsa	/* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2112172567Sthompsa	error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2113172567Sthompsa	if (error != 0)
2114172567Sthompsa		goto done;
2115172567Sthompsa
2116172567Sthompsa	/*
2117172567Sthompsa	 * Setup null/bogus ssid so firmware doesn't use any previous
2118172567Sthompsa	 * ssid to try and associate.  This is because the ``don't
2119172567Sthompsa	 * associate'' option bit is broken (sigh).
2120172567Sthompsa	 */
2121172567Sthompsa	error = ipw_setssid(sc, NULL, 0);
2122172567Sthompsa	if (error != 0)
2123172567Sthompsa		goto done;
2124172567Sthompsa
2125172567Sthompsa	/*
2126172567Sthompsa	 * NB: the adapter may be disabled on association lost;
2127172567Sthompsa	 *     if so just re-enable it to kick off scanning.
2128172567Sthompsa	 */
2129172567Sthompsa	DPRINTF(("Starting scan\n"));
2130172567Sthompsa	sc->sc_scan_timer = 3;
2131172567Sthompsa	if (sc->flags & IPW_FLAG_ENABLED) {
2132172567Sthompsa		params = 0;				/* XXX? */
2133172567Sthompsa		error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2134172567Sthompsa				&params, sizeof(params));
2135172567Sthompsa	} else
2136172567Sthompsa		error = ipw_enable(sc);
2137172567Sthompsadone:
2138172567Sthompsa	if (error != 0) {
2139172567Sthompsa		DPRINTF(("Scan failed\n"));
2140172567Sthompsa		sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2141172567Sthompsa	}
2142172567Sthompsa	return (error);
2143172567Sthompsa}
2144172567Sthompsa
2145172567Sthompsastatic int
2146172567Sthompsaipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2147172567Sthompsa{
2148287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2149172567Sthompsa	uint32_t data;
2150172567Sthompsa	int error;
2151172567Sthompsa
2152172567Sthompsa	data = htole32(ieee80211_chan2ieee(ic, chan));
2153172567Sthompsa	DPRINTF(("Setting channel to %u\n", le32toh(data)));
2154172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2155172567Sthompsa	if (error == 0)
2156172567Sthompsa		ipw_setcurchan(sc, chan);
2157172567Sthompsa	return error;
2158172567Sthompsa}
2159172567Sthompsa
2160178354Ssamstatic void
2161191746Sthompsaipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2162145247Sdamien{
2163286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2164178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2165145247Sdamien	struct ipw_security security;
2166145247Sdamien	uint32_t data;
2167172567Sthompsa	int error;
2168145247Sdamien
2169178354Ssam	IPW_LOCK(sc);
2170172567Sthompsa	error = ipw_disable(sc);
2171172567Sthompsa	if (error != 0)
2172178354Ssam		goto done;
2173172567Sthompsa
2174152637Sdamien	memset(&security, 0, sizeof security);
2175178354Ssam	security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2176145247Sdamien	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2177145247Sdamien	security.ciphers = htole32(IPW_CIPHER_NONE);
2178145247Sdamien	DPRINTF(("Setting authmode to %u\n", security.authmode));
2179172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2180145247Sdamien	    sizeof security);
2181145247Sdamien	if (error != 0)
2182178354Ssam		goto done;
2183145247Sdamien
2184178354Ssam	data = htole32(vap->iv_rtsthreshold);
2185178354Ssam	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2186178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2187145247Sdamien	if (error != 0)
2188178354Ssam		goto done;
2189145247Sdamien
2190178354Ssam	data = htole32(vap->iv_fragthreshold);
2191178354Ssam	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2192178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2193145247Sdamien	if (error != 0)
2194178354Ssam		goto done;
2195145247Sdamien
2196178354Ssam	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2197172567Sthompsa		error = ipw_setwepkeys(sc);
2198172567Sthompsa		if (error != 0)
2199178354Ssam			goto done;
2200172567Sthompsa
2201178354Ssam		if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2202178354Ssam			data = htole32(vap->iv_def_txkey);
2203172567Sthompsa			DPRINTF(("Setting wep tx key index to %u\n",
2204172567Sthompsa				le32toh(data)));
2205172567Sthompsa			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2206172567Sthompsa			    sizeof data);
2207172567Sthompsa			if (error != 0)
2208178354Ssam				goto done;
2209172567Sthompsa		}
2210172567Sthompsa	}
2211172567Sthompsa
2212178354Ssam	data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2213172567Sthompsa	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2214172567Sthompsa	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2215172567Sthompsa	if (error != 0)
2216178354Ssam		goto done;
2217172567Sthompsa
2218172567Sthompsa	error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2219172567Sthompsa	if (error != 0)
2220178354Ssam		goto done;
2221172567Sthompsa
2222172567Sthompsa	error = ipw_setbssid(sc, ni->ni_bssid);
2223172567Sthompsa	if (error != 0)
2224178354Ssam		goto done;
2225172567Sthompsa
2226206766Sbschmidt	if (vap->iv_appie_wpa != NULL) {
2227206766Sbschmidt		struct ieee80211_appie *ie = vap->iv_appie_wpa;
2228178354Ssam		error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2229172567Sthompsa		if (error != 0)
2230178354Ssam			goto done;
2231172567Sthompsa	}
2232172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2233172567Sthompsa		error = ipw_setchannel(sc, ni->ni_chan);
2234172567Sthompsa		if (error != 0)
2235178354Ssam			goto done;
2236172567Sthompsa	}
2237172567Sthompsa
2238172567Sthompsa	/* lock scan to ap's channel and enable associate */
2239172567Sthompsa	error = ipw_setscanopts(sc,
2240178354Ssam	    1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2241178354Ssam	if (error != 0)
2242178354Ssam		goto done;
2243172567Sthompsa
2244178354Ssam	error = ipw_enable(sc);		/* finally, enable adapter */
2245178354Ssam	if (error == 0)
2246178354Ssam		sc->flags |= IPW_FLAG_ASSOCIATING;
2247178354Ssamdone:
2248178354Ssam	IPW_UNLOCK(sc);
2249172567Sthompsa}
2250172567Sthompsa
2251172567Sthompsastatic void
2252191746Sthompsaipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2253172567Sthompsa{
2254178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2255286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2256172567Sthompsa
2257172567Sthompsa	IPW_LOCK(sc);
2258172567Sthompsa	DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2259172567Sthompsa	/*
2260172567Sthompsa	 * NB: don't try to do this if ipw_stop_master has
2261172567Sthompsa	 *     shutdown the firmware and disabled interrupts.
2262172567Sthompsa	 */
2263178354Ssam	if (sc->flags & IPW_FLAG_FW_INITED) {
2264178354Ssam		sc->flags &= ~IPW_FLAG_ASSOCIATED;
2265178354Ssam		/*
2266178354Ssam		 * NB: firmware currently ignores bssid parameter, but
2267178354Ssam		 *     supply it in case this changes (follow linux driver).
2268178354Ssam		 */
2269178354Ssam		(void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2270178354Ssam			ni->ni_bssid, IEEE80211_ADDR_LEN);
2271178354Ssam	}
2272178354Ssam	IPW_UNLOCK(sc);
2273172567Sthompsa}
2274172567Sthompsa
2275172567Sthompsa/*
2276156599Sdamien * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2277156599Sdamien * It is called on firmware panics or on watchdog timeouts.
2278156599Sdamien */
2279145247Sdamienstatic void
2280156599Sdamienipw_init_task(void *context, int pending)
2281156599Sdamien{
2282156599Sdamien	ipw_init(context);
2283156599Sdamien}
2284156599Sdamien
2285156599Sdamienstatic void
2286145247Sdamienipw_init(void *priv)
2287145247Sdamien{
2288145247Sdamien	struct ipw_softc *sc = priv;
2289287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2290172567Sthompsa
2291172567Sthompsa	IPW_LOCK(sc);
2292178354Ssam	ipw_init_locked(sc);
2293172567Sthompsa	IPW_UNLOCK(sc);
2294178354Ssam
2295287197Sglebius	if (sc->flags & IPW_FLAG_RUNNING)
2296178931Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2297172567Sthompsa}
2298172567Sthompsa
2299172567Sthompsastatic void
2300178354Ssamipw_init_locked(struct ipw_softc *sc)
2301172567Sthompsa{
2302287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2303178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2304166756Sluigi	const struct firmware *fp;
2305156599Sdamien	const struct ipw_firmware_hdr *hdr;
2306178354Ssam	const char *fw;
2307145247Sdamien
2308172567Sthompsa	IPW_LOCK_ASSERT(sc);
2309156599Sdamien
2310172567Sthompsa	DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2311178354Ssam		ieee80211_state_name[vap->iv_state], sc->flags));
2312172567Sthompsa
2313156599Sdamien	/*
2314156599Sdamien	 * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2315156599Sdamien	 * when loading the firmware and we don't want to be called during this
2316156599Sdamien	 * operation.
2317156599Sdamien	 */
2318172567Sthompsa	if (sc->flags & IPW_FLAG_INIT_LOCKED)
2319145247Sdamien		return;
2320156599Sdamien	sc->flags |= IPW_FLAG_INIT_LOCKED;
2321145247Sdamien
2322172567Sthompsa	ipw_stop_locked(sc);
2323145247Sdamien
2324145247Sdamien	if (ipw_reset(sc) != 0) {
2325145247Sdamien		device_printf(sc->sc_dev, "could not reset adapter\n");
2326178354Ssam		goto fail;
2327145247Sdamien	}
2328145247Sdamien
2329159487Siedowse	if (sc->sc_firmware == NULL) {
2330178354Ssam		device_printf(sc->sc_dev, "no firmware\n");
2331178354Ssam		goto fail;
2332156599Sdamien	}
2333178354Ssam	/* NB: consistency already checked on load */
2334159487Siedowse	fp = sc->sc_firmware;
2335156599Sdamien	hdr = (const struct ipw_firmware_hdr *)fp->data;
2336156599Sdamien
2337178354Ssam	DPRINTF(("Loading firmware image '%s'\n", fp->name));
2338156599Sdamien	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2339156599Sdamien	if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2340145247Sdamien		device_printf(sc->sc_dev, "could not load microcode\n");
2341178354Ssam		goto fail;
2342145247Sdamien	}
2343145247Sdamien
2344145247Sdamien	ipw_stop_master(sc);
2345145247Sdamien
2346145247Sdamien	/*
2347145247Sdamien	 * Setup tx, rx and status rings.
2348145247Sdamien	 */
2349145247Sdamien	sc->txold = IPW_NTBD - 1;
2350145247Sdamien	sc->txcur = 0;
2351145247Sdamien	sc->txfree = IPW_NTBD - 2;
2352145247Sdamien	sc->rxcur = IPW_NRBD - 1;
2353145247Sdamien
2354145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2355145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2356145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2357145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2358145247Sdamien
2359145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2360145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2361145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2362145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2363145247Sdamien
2364145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2365145247Sdamien
2366156599Sdamien	fw = (const char *)fp->data + sizeof *hdr;
2367156599Sdamien	if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2368145247Sdamien		device_printf(sc->sc_dev, "could not load firmware\n");
2369178354Ssam		goto fail;
2370145247Sdamien	}
2371145247Sdamien
2372145247Sdamien	sc->flags |= IPW_FLAG_FW_INITED;
2373145247Sdamien
2374145247Sdamien	/* retrieve information tables base addresses */
2375145247Sdamien	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2376145247Sdamien	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2377145247Sdamien
2378145247Sdamien	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2379145247Sdamien
2380145247Sdamien	if (ipw_config(sc) != 0) {
2381145247Sdamien		device_printf(sc->sc_dev, "device configuration failed\n");
2382178354Ssam		goto fail;
2383145247Sdamien	}
2384145247Sdamien
2385172567Sthompsa	callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2386287197Sglebius	sc->flags |= IPW_FLAG_RUNNING;
2387287197Sglebius	sc->flags &= ~IPW_FLAG_INIT_LOCKED;
2388145247Sdamien	return;
2389145247Sdamien
2390178354Ssamfail:
2391172567Sthompsa	ipw_stop_locked(sc);
2392287197Sglebius	sc->flags &= ~IPW_FLAG_INIT_LOCKED;
2393145247Sdamien}
2394145247Sdamien
2395178354Ssamstatic int
2396178354Ssamipw_config(struct ipw_softc *sc)
2397178354Ssam{
2398287197Sglebius	struct ieee80211com *ic = &sc->sc_ic;
2399178354Ssam	struct ipw_configuration config;
2400178354Ssam	uint32_t data;
2401178354Ssam	int error;
2402178354Ssam
2403178354Ssam	error = ipw_disable(sc);
2404178354Ssam	if (error != 0)
2405178354Ssam		return error;
2406178354Ssam
2407178354Ssam	switch (ic->ic_opmode) {
2408178354Ssam	case IEEE80211_M_STA:
2409178354Ssam	case IEEE80211_M_HOSTAP:
2410178354Ssam	case IEEE80211_M_WDS:		/* XXX */
2411178354Ssam		data = htole32(IPW_MODE_BSS);
2412178354Ssam		break;
2413178354Ssam	case IEEE80211_M_IBSS:
2414178354Ssam	case IEEE80211_M_AHDEMO:
2415178354Ssam		data = htole32(IPW_MODE_IBSS);
2416178354Ssam		break;
2417178354Ssam	case IEEE80211_M_MONITOR:
2418178354Ssam		data = htole32(IPW_MODE_MONITOR);
2419178354Ssam		break;
2420195562Srpaulo	default:
2421195562Srpaulo		device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2422195562Srpaulo		return EINVAL;
2423178354Ssam	}
2424178354Ssam	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2425178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2426178354Ssam	if (error != 0)
2427178354Ssam		return error;
2428178354Ssam
2429178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2430178354Ssam	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2431178354Ssam		error = ipw_setchannel(sc, ic->ic_curchan);
2432178354Ssam		if (error != 0)
2433178354Ssam			return error;
2434178354Ssam	}
2435178354Ssam
2436178354Ssam	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2437178354Ssam		return ipw_enable(sc);
2438178354Ssam
2439178354Ssam	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2440178354Ssam	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2441178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS)
2442178354Ssam		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2443287197Sglebius	if (ic->ic_promisc > 0)
2444178354Ssam		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2445178354Ssam	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2446178354Ssam	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2447178354Ssam	DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2448178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2449178354Ssam	if (error != 0)
2450178354Ssam		return error;
2451178354Ssam
2452207926Sbschmidt	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2453178354Ssam	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2454178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2455178354Ssam	if (error != 0)
2456178354Ssam		return error;
2457178354Ssam
2458207926Sbschmidt	/* Use the same rate set */
2459178354Ssam	DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2460178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2461178354Ssam	if (error != 0)
2462178354Ssam		return error;
2463178354Ssam
2464207926Sbschmidt	/* Use the same rate set */
2465178354Ssam	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2466178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2467178354Ssam	if (error != 0)
2468178354Ssam		return error;
2469178354Ssam
2470178354Ssam	data = htole32(IPW_POWER_MODE_CAM);
2471178354Ssam	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2472178354Ssam	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2473178354Ssam	if (error != 0)
2474178354Ssam		return error;
2475178354Ssam
2476178354Ssam	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2477178354Ssam		data = htole32(32); /* default value */
2478178354Ssam		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2479178354Ssam		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2480178354Ssam		    sizeof data);
2481178354Ssam		if (error != 0)
2482178354Ssam			return error;
2483178354Ssam	}
2484178354Ssam
2485178354Ssam	return 0;
2486178354Ssam}
2487178354Ssam
2488145247Sdamienstatic void
2489145247Sdamienipw_stop(void *priv)
2490145247Sdamien{
2491145247Sdamien	struct ipw_softc *sc = priv;
2492172567Sthompsa
2493172567Sthompsa	IPW_LOCK(sc);
2494172567Sthompsa	ipw_stop_locked(sc);
2495172567Sthompsa	IPW_UNLOCK(sc);
2496172567Sthompsa}
2497172567Sthompsa
2498172567Sthompsastatic void
2499172567Sthompsaipw_stop_locked(struct ipw_softc *sc)
2500172567Sthompsa{
2501145247Sdamien	int i;
2502145247Sdamien
2503172567Sthompsa	IPW_LOCK_ASSERT(sc);
2504156599Sdamien
2505172567Sthompsa	callout_stop(&sc->sc_wdtimer);
2506145247Sdamien	ipw_stop_master(sc);
2507145247Sdamien
2508145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2509145247Sdamien
2510145247Sdamien	/*
2511145247Sdamien	 * Release tx buffers.
2512145247Sdamien	 */
2513145247Sdamien	for (i = 0; i < IPW_NTBD; i++)
2514145247Sdamien		ipw_release_sbd(sc, &sc->stbd_list[i]);
2515145247Sdamien
2516145247Sdamien	sc->sc_tx_timer = 0;
2517287197Sglebius	sc->flags &= ~IPW_FLAG_RUNNING;
2518145247Sdamien}
2519145247Sdamien
2520145247Sdamienstatic int
2521145247Sdamienipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2522145247Sdamien{
2523145247Sdamien	struct ipw_softc *sc = arg1;
2524145247Sdamien	uint32_t i, size, buf[256];
2525145247Sdamien
2526174317Sphilip	memset(buf, 0, sizeof buf);
2527174317Sphilip
2528174317Sphilip	if (!(sc->flags & IPW_FLAG_FW_INITED))
2529145247Sdamien		return SYSCTL_OUT(req, buf, sizeof buf);
2530145247Sdamien
2531145247Sdamien	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2532145247Sdamien
2533145247Sdamien	size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2534145247Sdamien	for (i = 1; i < size; i++)
2535145247Sdamien		buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2536145247Sdamien
2537174317Sphilip	return SYSCTL_OUT(req, buf, size);
2538145247Sdamien}
2539145247Sdamien
2540145247Sdamienstatic int
2541145247Sdamienipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2542145247Sdamien{
2543145247Sdamien	struct ipw_softc *sc = arg1;
2544145247Sdamien	int val;
2545145247Sdamien
2546145247Sdamien	val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2547145247Sdamien	        (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2548145247Sdamien
2549145247Sdamien	return SYSCTL_OUT(req, &val, sizeof val);
2550145247Sdamien}
2551145247Sdamien
2552145247Sdamienstatic uint32_t
2553145247Sdamienipw_read_table1(struct ipw_softc *sc, uint32_t off)
2554145247Sdamien{
2555145247Sdamien	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2556145247Sdamien}
2557145247Sdamien
2558145247Sdamienstatic void
2559145247Sdamienipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2560145247Sdamien{
2561145247Sdamien	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2562145247Sdamien}
2563145247Sdamien
2564172567Sthompsa#if 0
2565145247Sdamienstatic int
2566145247Sdamienipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2567145247Sdamien{
2568145247Sdamien	uint32_t addr, info;
2569145247Sdamien	uint16_t count, size;
2570145247Sdamien	uint32_t total;
2571145247Sdamien
2572145247Sdamien	/* addr[4] + count[2] + size[2] */
2573145247Sdamien	addr = MEM_READ_4(sc, sc->table2_base + off);
2574145247Sdamien	info = MEM_READ_4(sc, sc->table2_base + off + 4);
2575145247Sdamien
2576145247Sdamien	count = info >> 16;
2577145247Sdamien	size = info & 0xffff;
2578145247Sdamien	total = count * size;
2579145247Sdamien
2580145247Sdamien	if (total > *len) {
2581145247Sdamien		*len = total;
2582145247Sdamien		return EINVAL;
2583145247Sdamien	}
2584145247Sdamien
2585145247Sdamien	*len = total;
2586145247Sdamien	ipw_read_mem_1(sc, addr, buf, total);
2587145247Sdamien
2588145247Sdamien	return 0;
2589145247Sdamien}
2590145247Sdamien
2591145247Sdamienstatic void
2592145247Sdamienipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2593145247Sdamien    bus_size_t count)
2594145247Sdamien{
2595145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2596145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2597145247Sdamien		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2598145247Sdamien	}
2599145247Sdamien}
2600172567Sthompsa#endif
2601145247Sdamien
2602145247Sdamienstatic void
2603156599Sdamienipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2604145247Sdamien    bus_size_t count)
2605145247Sdamien{
2606145247Sdamien	for (; count > 0; offset++, datap++, count--) {
2607145247Sdamien		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2608145247Sdamien		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2609145247Sdamien	}
2610145247Sdamien}
2611172567Sthompsa
2612172567Sthompsastatic void
2613172567Sthompsaipw_scan_start(struct ieee80211com *ic)
2614172567Sthompsa{
2615286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2616172567Sthompsa
2617172567Sthompsa	IPW_LOCK(sc);
2618191746Sthompsa	ipw_scan(sc);
2619172567Sthompsa	IPW_UNLOCK(sc);
2620172567Sthompsa}
2621172567Sthompsa
2622172567Sthompsastatic void
2623300239Savosipw_getradiocaps(struct ieee80211com *ic,
2624300239Savos    int maxchans, int *nchans, struct ieee80211_channel chans[])
2625300239Savos{
2626300239Savos	struct ipw_softc *sc = ic->ic_softc;
2627300239Savos	uint8_t bands[IEEE80211_MODE_BYTES];
2628300239Savos	int i;
2629300239Savos
2630300239Savos	memset(bands, 0, sizeof(bands));
2631300239Savos	setbit(bands, IEEE80211_MODE_11B);
2632300239Savos
2633300239Savos	for (i = 1; i < 16; i++) {
2634300239Savos		if (sc->chanmask & (1 << i)) {
2635300239Savos			ieee80211_add_channel(chans, maxchans, nchans,
2636300239Savos			    i, 0, 0, 0, bands);
2637300239Savos		}
2638300239Savos	}
2639300239Savos
2640300239Savos}
2641300239Savos
2642300239Savosstatic void
2643172567Sthompsaipw_set_channel(struct ieee80211com *ic)
2644172567Sthompsa{
2645286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2646172567Sthompsa
2647172567Sthompsa	IPW_LOCK(sc);
2648172567Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2649172567Sthompsa		ipw_disable(sc);
2650172567Sthompsa		ipw_setchannel(sc, ic->ic_curchan);
2651172567Sthompsa		ipw_enable(sc);
2652172567Sthompsa	}
2653172567Sthompsa	IPW_UNLOCK(sc);
2654172567Sthompsa}
2655172567Sthompsa
2656172567Sthompsastatic void
2657178354Ssamipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2658172567Sthompsa{
2659172567Sthompsa	/* NB: all channels are scanned at once */
2660172567Sthompsa}
2661172567Sthompsa
2662172567Sthompsastatic void
2663178354Ssamipw_scan_mindwell(struct ieee80211_scan_state *ss)
2664172567Sthompsa{
2665172567Sthompsa	/* NB: don't try to abort scan; wait for firmware to finish */
2666172567Sthompsa}
2667172567Sthompsa
2668172567Sthompsastatic void
2669172567Sthompsaipw_scan_end(struct ieee80211com *ic)
2670172567Sthompsa{
2671286865Sadrian	struct ipw_softc *sc = ic->ic_softc;
2672172567Sthompsa
2673172567Sthompsa	IPW_LOCK(sc);
2674172567Sthompsa	sc->flags &= ~IPW_FLAG_SCANNING;
2675172567Sthompsa	IPW_UNLOCK(sc);
2676172567Sthompsa}
2677