if_ipw.c revision 192468
1/*	$FreeBSD: head/sys/dev/ipw/if_ipw.c 192468 2009-05-20 20:00:40Z sam $	*/
2
3/*-
4 * Copyright (c) 2004-2006
5 *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6 * Copyright (c) 2006 Sam Leffler, Errno Consulting
7 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice unmodified, this list of conditions, and the following
14 *    disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/dev/ipw/if_ipw.c 192468 2009-05-20 20:00:40Z sam $");
34
35/*-
36 * Intel(R) PRO/Wireless 2100 MiniPCI driver
37 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
38 */
39
40#include <sys/param.h>
41#include <sys/sysctl.h>
42#include <sys/sockio.h>
43#include <sys/mbuf.h>
44#include <sys/kernel.h>
45#include <sys/socket.h>
46#include <sys/systm.h>
47#include <sys/malloc.h>
48#include <sys/queue.h>
49#include <sys/taskqueue.h>
50#include <sys/module.h>
51#include <sys/bus.h>
52#include <sys/endian.h>
53#include <sys/linker.h>
54#include <sys/firmware.h>
55
56#include <machine/bus.h>
57#include <machine/resource.h>
58#include <sys/rman.h>
59
60#include <dev/pci/pcireg.h>
61#include <dev/pci/pcivar.h>
62
63#include <net/bpf.h>
64#include <net/if.h>
65#include <net/if_arp.h>
66#include <net/ethernet.h>
67#include <net/if_dl.h>
68#include <net/if_media.h>
69#include <net/if_types.h>
70
71#include <net80211/ieee80211_var.h>
72#include <net80211/ieee80211_radiotap.h>
73
74#include <netinet/in.h>
75#include <netinet/in_systm.h>
76#include <netinet/in_var.h>
77#include <netinet/ip.h>
78#include <netinet/if_ether.h>
79
80#include <dev/ipw/if_ipwreg.h>
81#include <dev/ipw/if_ipwvar.h>
82
83#define IPW_DEBUG
84#ifdef IPW_DEBUG
85#define DPRINTF(x)	do { if (ipw_debug > 0) printf x; } while (0)
86#define DPRINTFN(n, x)	do { if (ipw_debug >= (n)) printf x; } while (0)
87int ipw_debug = 0;
88SYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
89#else
90#define DPRINTF(x)
91#define DPRINTFN(n, x)
92#endif
93
94MODULE_DEPEND(ipw, pci,  1, 1, 1);
95MODULE_DEPEND(ipw, wlan, 1, 1, 1);
96MODULE_DEPEND(ipw, firmware, 1, 1, 1);
97
98struct ipw_ident {
99	uint16_t	vendor;
100	uint16_t	device;
101	const char	*name;
102};
103
104static const struct ipw_ident ipw_ident_table[] = {
105	{ 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
106
107	{ 0, 0, NULL }
108};
109
110static struct ieee80211vap *ipw_vap_create(struct ieee80211com *,
111		    const char name[IFNAMSIZ], int unit, int opmode, int flags,
112		    const uint8_t bssid[IEEE80211_ADDR_LEN],
113		    const uint8_t mac[IEEE80211_ADDR_LEN]);
114static void	ipw_vap_delete(struct ieee80211vap *);
115static int	ipw_dma_alloc(struct ipw_softc *);
116static void	ipw_release(struct ipw_softc *);
117static void	ipw_media_status(struct ifnet *, struct ifmediareq *);
118static int	ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
119static uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
120static void	ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
121static void	ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
122static void	ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
123		    struct ipw_soft_bd *, struct ipw_soft_buf *);
124static void	ipw_rx_intr(struct ipw_softc *);
125static void	ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
126static void	ipw_tx_intr(struct ipw_softc *);
127static void	ipw_intr(void *);
128static void	ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
129static const char * ipw_cmdname(int);
130static int	ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
131static int	ipw_tx_start(struct ifnet *, struct mbuf *,
132		    struct ieee80211_node *);
133static int	ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
134		    const struct ieee80211_bpf_params *);
135static void	ipw_start(struct ifnet *);
136static void	ipw_start_locked(struct ifnet *);
137static void	ipw_watchdog(void *);
138static int	ipw_ioctl(struct ifnet *, u_long, caddr_t);
139static void	ipw_stop_master(struct ipw_softc *);
140static int	ipw_enable(struct ipw_softc *);
141static int	ipw_disable(struct ipw_softc *);
142static int	ipw_reset(struct ipw_softc *);
143static int	ipw_load_ucode(struct ipw_softc *, const char *, int);
144static int	ipw_load_firmware(struct ipw_softc *, const char *, int);
145static int	ipw_config(struct ipw_softc *);
146static void	ipw_assoc(struct ieee80211com *, struct ieee80211vap *);
147static void	ipw_disassoc(struct ieee80211com *, struct ieee80211vap *);
148static void	ipw_init_task(void *, int);
149static void	ipw_init(void *);
150static void	ipw_init_locked(struct ipw_softc *);
151static void	ipw_stop(void *);
152static void	ipw_stop_locked(struct ipw_softc *);
153static int	ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
154static int	ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
155static uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
156static void	ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
157#if 0
158static int	ipw_read_table2(struct ipw_softc *, uint32_t, void *,
159		    uint32_t *);
160static void	ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
161		    bus_size_t);
162#endif
163static void	ipw_write_mem_1(struct ipw_softc *, bus_size_t,
164		    const uint8_t *, bus_size_t);
165static int	ipw_scan(struct ipw_softc *);
166static void	ipw_scan_start(struct ieee80211com *);
167static void	ipw_scan_end(struct ieee80211com *);
168static void	ipw_set_channel(struct ieee80211com *);
169static void	ipw_scan_curchan(struct ieee80211_scan_state *,
170		    unsigned long maxdwell);
171static void	ipw_scan_mindwell(struct ieee80211_scan_state *);
172
173static int ipw_probe(device_t);
174static int ipw_attach(device_t);
175static int ipw_detach(device_t);
176static int ipw_shutdown(device_t);
177static int ipw_suspend(device_t);
178static int ipw_resume(device_t);
179
180static device_method_t ipw_methods[] = {
181	/* Device interface */
182	DEVMETHOD(device_probe,		ipw_probe),
183	DEVMETHOD(device_attach,	ipw_attach),
184	DEVMETHOD(device_detach,	ipw_detach),
185	DEVMETHOD(device_shutdown,	ipw_shutdown),
186	DEVMETHOD(device_suspend,	ipw_suspend),
187	DEVMETHOD(device_resume,	ipw_resume),
188
189	{ 0, 0 }
190};
191
192static driver_t ipw_driver = {
193	"ipw",
194	ipw_methods,
195	sizeof (struct ipw_softc)
196};
197
198static devclass_t ipw_devclass;
199
200DRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0);
201
202static int
203ipw_probe(device_t dev)
204{
205	const struct ipw_ident *ident;
206
207	for (ident = ipw_ident_table; ident->name != NULL; ident++) {
208		if (pci_get_vendor(dev) == ident->vendor &&
209		    pci_get_device(dev) == ident->device) {
210			device_set_desc(dev, ident->name);
211			return 0;
212		}
213	}
214	return ENXIO;
215}
216
217/* Base Address Register */
218#define IPW_PCI_BAR0	0x10
219
220static int
221ipw_attach(device_t dev)
222{
223	struct ipw_softc *sc = device_get_softc(dev);
224	struct ifnet *ifp;
225	struct ieee80211com *ic;
226	struct ieee80211_channel *c;
227	uint16_t val;
228	int error, i;
229	uint8_t macaddr[IEEE80211_ADDR_LEN];
230
231	sc->sc_dev = dev;
232
233	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
234	    MTX_DEF | MTX_RECURSE);
235
236	TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
237	callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
238
239	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
240		device_printf(dev, "chip is in D%d power mode "
241		    "-- setting to D0\n", pci_get_powerstate(dev));
242		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
243	}
244
245	pci_write_config(dev, 0x41, 0, 1);
246
247	/* enable bus-mastering */
248	pci_enable_busmaster(dev);
249
250	sc->mem_rid = IPW_PCI_BAR0;
251	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
252	    RF_ACTIVE);
253	if (sc->mem == NULL) {
254		device_printf(dev, "could not allocate memory resource\n");
255		goto fail;
256	}
257
258	sc->sc_st = rman_get_bustag(sc->mem);
259	sc->sc_sh = rman_get_bushandle(sc->mem);
260
261	sc->irq_rid = 0;
262	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
263	    RF_ACTIVE | RF_SHAREABLE);
264	if (sc->irq == NULL) {
265		device_printf(dev, "could not allocate interrupt resource\n");
266		goto fail1;
267	}
268
269	if (ipw_reset(sc) != 0) {
270		device_printf(dev, "could not reset adapter\n");
271		goto fail2;
272	}
273
274	if (ipw_dma_alloc(sc) != 0) {
275		device_printf(dev, "could not allocate DMA resources\n");
276		goto fail2;
277	}
278
279	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
280	if (ifp == NULL) {
281		device_printf(dev, "can not if_alloc()\n");
282		goto fail3;
283	}
284	ic = ifp->if_l2com;
285
286	ifp->if_softc = sc;
287	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
288	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
289	ifp->if_init = ipw_init;
290	ifp->if_ioctl = ipw_ioctl;
291	ifp->if_start = ipw_start;
292	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
293	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
294	IFQ_SET_READY(&ifp->if_snd);
295
296	ic->ic_ifp = ifp;
297	ic->ic_opmode = IEEE80211_M_STA;
298	ic->ic_phytype = IEEE80211_T_DS;
299
300	/* set device capabilities */
301	ic->ic_caps =
302		  IEEE80211_C_STA		/* station mode supported */
303		| IEEE80211_C_IBSS		/* IBSS mode supported */
304		| IEEE80211_C_MONITOR		/* monitor mode supported */
305		| IEEE80211_C_PMGT		/* power save supported */
306		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
307		| IEEE80211_C_WPA		/* 802.11i supported */
308		;
309
310	/* read MAC address from EEPROM */
311	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
312	macaddr[0] = val >> 8;
313	macaddr[1] = val & 0xff;
314	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
315	macaddr[2] = val >> 8;
316	macaddr[3] = val & 0xff;
317	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
318	macaddr[4] = val >> 8;
319	macaddr[5] = val & 0xff;
320
321	/* set supported .11b channels (read from EEPROM) */
322	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
323		val = 0x7ff; /* default to channels 1-11 */
324	val <<= 1;
325	for (i = 1; i < 16; i++) {
326		if (val & (1 << i)) {
327			c = &ic->ic_channels[ic->ic_nchans++];
328			c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
329			c->ic_flags = IEEE80211_CHAN_B;
330			c->ic_ieee = i;
331		}
332	}
333
334	/* check support for radio transmitter switch in EEPROM */
335	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
336		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
337
338	ieee80211_ifattach(ic, macaddr);
339	ic->ic_scan_start = ipw_scan_start;
340	ic->ic_scan_end = ipw_scan_end;
341	ic->ic_set_channel = ipw_set_channel;
342	ic->ic_scan_curchan = ipw_scan_curchan;
343	ic->ic_scan_mindwell = ipw_scan_mindwell;
344	ic->ic_raw_xmit = ipw_raw_xmit;
345
346	ic->ic_vap_create = ipw_vap_create;
347	ic->ic_vap_delete = ipw_vap_delete;
348
349	ieee80211_radiotap_attach(ic,
350	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
351		IPW_TX_RADIOTAP_PRESENT,
352	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
353		IPW_RX_RADIOTAP_PRESENT);
354
355	/*
356	 * Add a few sysctl knobs.
357	 */
358	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
359	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
360	    CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
361	    "radio transmitter switch state (0=off, 1=on)");
362
363	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
364	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
365	    CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
366	    "statistics");
367
368	/*
369	 * Hook our interrupt after all initialization is complete.
370	 */
371	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
372	    NULL, ipw_intr, sc, &sc->sc_ih);
373	if (error != 0) {
374		device_printf(dev, "could not set up interrupt\n");
375		goto fail4;
376	}
377
378	if (bootverbose)
379		ieee80211_announce(ic);
380
381	return 0;
382fail4:
383	if_free(ifp);
384fail3:
385	ipw_release(sc);
386fail2:
387	bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
388fail1:
389	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
390fail:
391	mtx_destroy(&sc->sc_mtx);
392	return ENXIO;
393}
394
395static int
396ipw_detach(device_t dev)
397{
398	struct ipw_softc *sc = device_get_softc(dev);
399	struct ifnet *ifp = sc->sc_ifp;
400	struct ieee80211com *ic = ifp->if_l2com;
401
402	ieee80211_draintask(ic, &sc->sc_init_task);
403	ipw_stop(sc);
404
405	ieee80211_ifdetach(ic);
406
407	callout_drain(&sc->sc_wdtimer);
408
409	ipw_release(sc);
410
411	bus_teardown_intr(dev, sc->irq, sc->sc_ih);
412	bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
413
414	bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
415
416	if_free(ifp);
417
418	if (sc->sc_firmware != NULL) {
419		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
420		sc->sc_firmware = NULL;
421	}
422
423	mtx_destroy(&sc->sc_mtx);
424
425	return 0;
426}
427
428static struct ieee80211vap *
429ipw_vap_create(struct ieee80211com *ic,
430	const char name[IFNAMSIZ], int unit, int opmode, int flags,
431	const uint8_t bssid[IEEE80211_ADDR_LEN],
432	const uint8_t mac[IEEE80211_ADDR_LEN])
433{
434	struct ifnet *ifp = ic->ic_ifp;
435	struct ipw_softc *sc = ifp->if_softc;
436	struct ipw_vap *ivp;
437	struct ieee80211vap *vap;
438	const struct firmware *fp;
439	const struct ipw_firmware_hdr *hdr;
440	const char *imagename;
441
442	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
443		return NULL;
444
445	switch (opmode) {
446	case IEEE80211_M_STA:
447		imagename = "ipw_bss";
448		break;
449	case IEEE80211_M_IBSS:
450		imagename = "ipw_ibss";
451		break;
452	case IEEE80211_M_MONITOR:
453		imagename = "ipw_monitor";
454		break;
455	default:
456		return NULL;
457	}
458
459	/*
460	 * Load firmware image using the firmware(9) subsystem.  Doing
461	 * this unlocked is ok since we're single-threaded by the
462	 * 802.11 layer.
463	 */
464	if (sc->sc_firmware == NULL ||
465	    strcmp(sc->sc_firmware->name, imagename) != 0) {
466		if (sc->sc_firmware != NULL)
467			firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
468		sc->sc_firmware = firmware_get(imagename);
469	}
470	if (sc->sc_firmware == NULL) {
471		device_printf(sc->sc_dev,
472		    "could not load firmware image '%s'\n", imagename);
473		return NULL;
474	}
475	fp = sc->sc_firmware;
476	if (fp->datasize < sizeof *hdr) {
477		device_printf(sc->sc_dev,
478		    "firmware image too short %zu\n", fp->datasize);
479		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
480		sc->sc_firmware = NULL;
481		return NULL;
482	}
483	hdr = (const struct ipw_firmware_hdr *)fp->data;
484	if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
485	    le32toh(hdr->ucodesz)) {
486		device_printf(sc->sc_dev,
487		    "firmware image too short %zu\n", fp->datasize);
488		firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
489		sc->sc_firmware = NULL;
490		return NULL;
491	}
492
493	ivp = (struct ipw_vap *) malloc(sizeof(struct ipw_vap),
494	    M_80211_VAP, M_NOWAIT | M_ZERO);
495	if (ivp == NULL)
496		return NULL;
497	vap = &ivp->vap;
498
499	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
500	/* override with driver methods */
501	ivp->newstate = vap->iv_newstate;
502	vap->iv_newstate = ipw_newstate;
503
504	/* complete setup */
505	ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status);
506	ic->ic_opmode = opmode;
507	return vap;
508}
509
510static void
511ipw_vap_delete(struct ieee80211vap *vap)
512{
513	struct ipw_vap *ivp = IPW_VAP(vap);
514
515	ieee80211_vap_detach(vap);
516	free(ivp, M_80211_VAP);
517}
518
519static int
520ipw_dma_alloc(struct ipw_softc *sc)
521{
522	struct ipw_soft_bd *sbd;
523	struct ipw_soft_hdr *shdr;
524	struct ipw_soft_buf *sbuf;
525	bus_addr_t physaddr;
526	int error, i;
527
528	/*
529	 * Allocate and map tx ring.
530	 */
531	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
532	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
533	    NULL, &sc->tbd_dmat);
534	if (error != 0) {
535		device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
536		goto fail;
537	}
538
539	error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
540	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
541	if (error != 0) {
542		device_printf(sc->sc_dev,
543		    "could not allocate tx ring DMA memory\n");
544		goto fail;
545	}
546
547	error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
548	    IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
549	if (error != 0) {
550		device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
551		goto fail;
552	}
553
554	/*
555	 * Allocate and map rx ring.
556	 */
557	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
558	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
559	    NULL, &sc->rbd_dmat);
560	if (error != 0) {
561		device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
562		goto fail;
563	}
564
565	error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
566	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
567	if (error != 0) {
568		device_printf(sc->sc_dev,
569		    "could not allocate rx ring DMA memory\n");
570		goto fail;
571	}
572
573	error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
574	    IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
575	if (error != 0) {
576		device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
577		goto fail;
578	}
579
580	/*
581	 * Allocate and map status ring.
582	 */
583	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
584	    BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
585	    NULL, NULL, &sc->status_dmat);
586	if (error != 0) {
587		device_printf(sc->sc_dev,
588		    "could not create status ring DMA tag\n");
589		goto fail;
590	}
591
592	error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
593	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
594	if (error != 0) {
595		device_printf(sc->sc_dev,
596		    "could not allocate status ring DMA memory\n");
597		goto fail;
598	}
599
600	error = bus_dmamap_load(sc->status_dmat, sc->status_map,
601	    sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
602	    0);
603	if (error != 0) {
604		device_printf(sc->sc_dev,
605		    "could not map status ring DMA memory\n");
606		goto fail;
607	}
608
609	/*
610	 * Allocate command DMA map.
611	 */
612	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
613	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
614	    sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
615	if (error != 0) {
616		device_printf(sc->sc_dev, "could not create command DMA tag\n");
617		goto fail;
618	}
619
620	error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
621	if (error != 0) {
622		device_printf(sc->sc_dev,
623		    "could not create command DMA map\n");
624		goto fail;
625	}
626
627	/*
628	 * Allocate headers DMA maps.
629	 */
630	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
631	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
632	    sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
633	if (error != 0) {
634		device_printf(sc->sc_dev, "could not create header DMA tag\n");
635		goto fail;
636	}
637
638	SLIST_INIT(&sc->free_shdr);
639	for (i = 0; i < IPW_NDATA; i++) {
640		shdr = &sc->shdr_list[i];
641		error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
642		if (error != 0) {
643			device_printf(sc->sc_dev,
644			    "could not create header DMA map\n");
645			goto fail;
646		}
647		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
648	}
649
650	/*
651	 * Allocate tx buffers DMA maps.
652	 */
653	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
654	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
655	    NULL, NULL, &sc->txbuf_dmat);
656	if (error != 0) {
657		device_printf(sc->sc_dev, "could not create tx DMA tag\n");
658		goto fail;
659	}
660
661	SLIST_INIT(&sc->free_sbuf);
662	for (i = 0; i < IPW_NDATA; i++) {
663		sbuf = &sc->tx_sbuf_list[i];
664		error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
665		if (error != 0) {
666			device_printf(sc->sc_dev,
667			    "could not create tx DMA map\n");
668			goto fail;
669		}
670		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
671	}
672
673	/*
674	 * Initialize tx ring.
675	 */
676	for (i = 0; i < IPW_NTBD; i++) {
677		sbd = &sc->stbd_list[i];
678		sbd->bd = &sc->tbd_list[i];
679		sbd->type = IPW_SBD_TYPE_NOASSOC;
680	}
681
682	/*
683	 * Pre-allocate rx buffers and DMA maps.
684	 */
685	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
686	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
687	    NULL, &sc->rxbuf_dmat);
688	if (error != 0) {
689		device_printf(sc->sc_dev, "could not create rx DMA tag\n");
690		goto fail;
691	}
692
693	for (i = 0; i < IPW_NRBD; i++) {
694		sbd = &sc->srbd_list[i];
695		sbuf = &sc->rx_sbuf_list[i];
696		sbd->bd = &sc->rbd_list[i];
697
698		sbuf->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
699		if (sbuf->m == NULL) {
700			device_printf(sc->sc_dev,
701			    "could not allocate rx mbuf\n");
702			error = ENOMEM;
703			goto fail;
704		}
705
706		error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
707		if (error != 0) {
708			device_printf(sc->sc_dev,
709			    "could not create rx DMA map\n");
710			goto fail;
711		}
712
713		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
714		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
715		    &physaddr, 0);
716		if (error != 0) {
717			device_printf(sc->sc_dev,
718			    "could not map rx DMA memory\n");
719			goto fail;
720		}
721
722		sbd->type = IPW_SBD_TYPE_DATA;
723		sbd->priv = sbuf;
724		sbd->bd->physaddr = htole32(physaddr);
725		sbd->bd->len = htole32(MCLBYTES);
726	}
727
728	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
729
730	return 0;
731
732fail:	ipw_release(sc);
733	return error;
734}
735
736static void
737ipw_release(struct ipw_softc *sc)
738{
739	struct ipw_soft_buf *sbuf;
740	int i;
741
742	if (sc->tbd_dmat != NULL) {
743		if (sc->stbd_list != NULL) {
744			bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
745			bus_dmamem_free(sc->tbd_dmat, sc->tbd_list,
746			    sc->tbd_map);
747		}
748		bus_dma_tag_destroy(sc->tbd_dmat);
749	}
750
751	if (sc->rbd_dmat != NULL) {
752		if (sc->rbd_list != NULL) {
753			bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
754			bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
755			    sc->rbd_map);
756		}
757		bus_dma_tag_destroy(sc->rbd_dmat);
758	}
759
760	if (sc->status_dmat != NULL) {
761		if (sc->status_list != NULL) {
762			bus_dmamap_unload(sc->status_dmat, sc->status_map);
763			bus_dmamem_free(sc->status_dmat, sc->status_list,
764			    sc->status_map);
765		}
766		bus_dma_tag_destroy(sc->status_dmat);
767	}
768
769	for (i = 0; i < IPW_NTBD; i++)
770		ipw_release_sbd(sc, &sc->stbd_list[i]);
771
772	if (sc->cmd_dmat != NULL) {
773		bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
774		bus_dma_tag_destroy(sc->cmd_dmat);
775	}
776
777	if (sc->hdr_dmat != NULL) {
778		for (i = 0; i < IPW_NDATA; i++)
779			bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
780		bus_dma_tag_destroy(sc->hdr_dmat);
781	}
782
783	if (sc->txbuf_dmat != NULL) {
784		for (i = 0; i < IPW_NDATA; i++) {
785			bus_dmamap_destroy(sc->txbuf_dmat,
786			    sc->tx_sbuf_list[i].map);
787		}
788		bus_dma_tag_destroy(sc->txbuf_dmat);
789	}
790
791	if (sc->rxbuf_dmat != NULL) {
792		for (i = 0; i < IPW_NRBD; i++) {
793			sbuf = &sc->rx_sbuf_list[i];
794			if (sbuf->m != NULL) {
795				bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
796				    BUS_DMASYNC_POSTREAD);
797				bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
798				m_freem(sbuf->m);
799			}
800			bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
801		}
802		bus_dma_tag_destroy(sc->rxbuf_dmat);
803	}
804}
805
806static int
807ipw_shutdown(device_t dev)
808{
809	struct ipw_softc *sc = device_get_softc(dev);
810
811	ipw_stop(sc);
812
813	return 0;
814}
815
816static int
817ipw_suspend(device_t dev)
818{
819	struct ipw_softc *sc = device_get_softc(dev);
820
821	ipw_stop(sc);
822
823	return 0;
824}
825
826static int
827ipw_resume(device_t dev)
828{
829	struct ipw_softc *sc = device_get_softc(dev);
830	struct ifnet *ifp = sc->sc_ifp;
831
832	pci_write_config(dev, 0x41, 0, 1);
833
834	if (ifp->if_flags & IFF_UP)
835		ipw_init(sc);
836
837	return 0;
838}
839
840static int
841ipw_cvtrate(int ipwrate)
842{
843	switch (ipwrate) {
844	case IPW_RATE_DS1:	return 2;
845	case IPW_RATE_DS2:	return 4;
846	case IPW_RATE_DS5:	return 11;
847	case IPW_RATE_DS11:	return 22;
848	}
849	return 0;
850}
851
852/*
853 * The firmware automatically adapts the transmit speed. We report its current
854 * value here.
855 */
856static void
857ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
858{
859	struct ieee80211vap *vap = ifp->if_softc;
860	struct ieee80211com *ic = vap->iv_ic;
861	struct ipw_softc *sc = ic->ic_ifp->if_softc;
862
863	/* read current transmission rate from adapter */
864	vap->iv_bss->ni_txrate = ipw_cvtrate(
865	    ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
866	ieee80211_media_status(ifp, imr);
867}
868
869static int
870ipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
871{
872	struct ipw_vap *ivp = IPW_VAP(vap);
873	struct ieee80211com *ic = vap->iv_ic;
874	struct ifnet *ifp = ic->ic_ifp;
875	struct ipw_softc *sc = ifp->if_softc;
876	enum ieee80211_state ostate;
877
878	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
879		ieee80211_state_name[vap->iv_state],
880		ieee80211_state_name[nstate], sc->flags));
881
882	ostate = vap->iv_state;
883	IEEE80211_UNLOCK(ic);
884
885	switch (nstate) {
886	case IEEE80211_S_RUN:
887		if (ic->ic_opmode == IEEE80211_M_IBSS) {
888			/*
889			 * XXX when joining an ibss network we are called
890			 * with a SCAN -> RUN transition on scan complete.
891			 * Use that to call ipw_auth_and_assoc.  On completing
892			 * the join we are then called again with an
893			 * AUTH -> RUN transition and we want to do nothing.
894			 * This is all totally bogus and needs to be redone.
895			 */
896			if (ostate == IEEE80211_S_SCAN)
897				ipw_assoc(ic, vap);
898		}
899		break;
900
901	case IEEE80211_S_INIT:
902		if (sc->flags & IPW_FLAG_ASSOCIATED)
903			ipw_disassoc(ic, vap);
904		break;
905
906	case IEEE80211_S_AUTH:
907		ipw_assoc(ic, vap);
908		break;
909
910	case IEEE80211_S_ASSOC:
911		/*
912		 * If we are not transitioning from AUTH the resend the
913		 * association request.
914		 */
915		if (ostate != IEEE80211_S_AUTH)
916			ipw_assoc(ic, vap);
917		break;
918
919	default:
920		break;
921	}
922	IEEE80211_LOCK(ic);
923	return ivp->newstate(vap, nstate, arg);
924}
925
926/*
927 * Read 16 bits at address 'addr' from the serial EEPROM.
928 */
929static uint16_t
930ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
931{
932	uint32_t tmp;
933	uint16_t val;
934	int n;
935
936	/* clock C once before the first command */
937	IPW_EEPROM_CTL(sc, 0);
938	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
939	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
940	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
941
942	/* write start bit (1) */
943	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
944	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
945
946	/* write READ opcode (10) */
947	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
948	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
949	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
950	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
951
952	/* write address A7-A0 */
953	for (n = 7; n >= 0; n--) {
954		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
955		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
956		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
957		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
958	}
959
960	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
961
962	/* read data Q15-Q0 */
963	val = 0;
964	for (n = 15; n >= 0; n--) {
965		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
966		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
967		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
968		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
969	}
970
971	IPW_EEPROM_CTL(sc, 0);
972
973	/* clear Chip Select and clock C */
974	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
975	IPW_EEPROM_CTL(sc, 0);
976	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
977
978	return le16toh(val);
979}
980
981static void
982ipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
983{
984	struct ipw_cmd *cmd;
985
986	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
987
988	cmd = mtod(sbuf->m, struct ipw_cmd *);
989
990	DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
991	    ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
992	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
993	    le32toh(cmd->status)));
994
995	sc->flags &= ~IPW_FLAG_BUSY;
996	wakeup(sc);
997}
998
999static void
1000ipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1001{
1002#define	IEEESTATE(vap)	ieee80211_state_name[vap->iv_state]
1003	struct ifnet *ifp = sc->sc_ifp;
1004	struct ieee80211com *ic = ifp->if_l2com;
1005	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1006	uint32_t state;
1007
1008	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1009
1010	state = le32toh(*mtod(sbuf->m, uint32_t *));
1011
1012	switch (state) {
1013	case IPW_STATE_ASSOCIATED:
1014		DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1015			IEEESTATE(vap), sc->flags));
1016		/* XXX suppress state change in case the fw auto-associates */
1017		if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1018			DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1019				IEEESTATE(vap), sc->flags));
1020			break;
1021		}
1022		sc->flags &= ~IPW_FLAG_ASSOCIATING;
1023		sc->flags |= IPW_FLAG_ASSOCIATED;
1024		ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
1025		break;
1026
1027	case IPW_STATE_SCANNING:
1028		DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1029			IEEESTATE(vap), sc->flags));
1030		/*
1031		 * NB: Check driver state for association on assoc
1032		 * loss as the firmware will immediately start to
1033		 * scan and we would treat it as a beacon miss if
1034		 * we checked the 802.11 layer state.
1035		 */
1036		if (sc->flags & IPW_FLAG_ASSOCIATED) {
1037			/* XXX probably need to issue disassoc to fw */
1038			ieee80211_beacon_miss(ic);
1039		}
1040		break;
1041
1042	case IPW_STATE_SCAN_COMPLETE:
1043		/*
1044		 * XXX For some reason scan requests generate scan
1045		 * started + scan done events before any traffic is
1046		 * received (e.g. probe response frames).  We work
1047		 * around this by marking the HACK flag and skipping
1048		 * the first scan complete event.
1049		*/
1050		DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1051			    IEEESTATE(vap), sc->flags));
1052		if (sc->flags & IPW_FLAG_HACK) {
1053			sc->flags &= ~IPW_FLAG_HACK;
1054			break;
1055		}
1056		if (sc->flags & IPW_FLAG_SCANNING) {
1057			ieee80211_scan_done(vap);
1058			sc->flags &= ~IPW_FLAG_SCANNING;
1059			sc->sc_scan_timer = 0;
1060		}
1061		break;
1062
1063	case IPW_STATE_ASSOCIATION_LOST:
1064		DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1065			IEEESTATE(vap), sc->flags));
1066		sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1067		if (vap->iv_state == IEEE80211_S_RUN)
1068			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1069		break;
1070
1071	case IPW_STATE_DISABLED:
1072		/* XXX? is this right? */
1073		sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1074		    IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1075		DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1076			IEEESTATE(vap), sc->flags));
1077		break;
1078
1079	case IPW_STATE_RADIO_DISABLED:
1080		device_printf(sc->sc_dev, "radio turned off\n");
1081		ieee80211_notify_radio(ic, 0);
1082		ipw_stop_locked(sc);
1083		/* XXX start polling thread to detect radio on */
1084		break;
1085
1086	default:
1087		DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1088			__func__, state, IEEESTATE(vap), sc->flags));
1089		break;
1090	}
1091#undef IEEESTATE
1092}
1093
1094/*
1095 * Set driver state for current channel.
1096 */
1097static void
1098ipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1099{
1100	struct ifnet *ifp = sc->sc_ifp;
1101	struct ieee80211com *ic = ifp->if_l2com;
1102
1103	ic->ic_curchan = chan;
1104	ieee80211_radiotap_chan_change(ic);
1105}
1106
1107/*
1108 * XXX: Hack to set the current channel to the value advertised in beacons or
1109 * probe responses. Only used during AP detection.
1110 */
1111static void
1112ipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1113{
1114	struct ifnet *ifp = sc->sc_ifp;
1115	struct ieee80211com *ic = ifp->if_l2com;
1116	struct ieee80211_channel *c;
1117	struct ieee80211_frame *wh;
1118	uint8_t subtype;
1119	uint8_t *frm, *efrm;
1120
1121	wh = mtod(m, struct ieee80211_frame *);
1122
1123	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1124		return;
1125
1126	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1127
1128	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1129	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1130		return;
1131
1132	/* XXX use ieee80211_parse_beacon */
1133	frm = (uint8_t *)(wh + 1);
1134	efrm = mtod(m, uint8_t *) + m->m_len;
1135
1136	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1137	while (frm < efrm) {
1138		if (*frm == IEEE80211_ELEMID_DSPARMS)
1139#if IEEE80211_CHAN_MAX < 255
1140		if (frm[2] <= IEEE80211_CHAN_MAX)
1141#endif
1142		{
1143			DPRINTF(("Fixing channel to %d\n", frm[2]));
1144			c = ieee80211_find_channel(ic,
1145				ieee80211_ieee2mhz(frm[2], 0),
1146				IEEE80211_CHAN_B);
1147			if (c == NULL)
1148				c = &ic->ic_channels[0];
1149			ipw_setcurchan(sc, c);
1150		}
1151
1152		frm += frm[1] + 2;
1153	}
1154}
1155
1156static void
1157ipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1158    struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1159{
1160	struct ifnet *ifp = sc->sc_ifp;
1161	struct ieee80211com *ic = ifp->if_l2com;
1162	struct mbuf *mnew, *m;
1163	struct ieee80211_node *ni;
1164	bus_addr_t physaddr;
1165	int error;
1166	int8_t rssi, nf;
1167	IPW_LOCK_DECL;
1168
1169	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1170	    status->rssi));
1171
1172	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1173	    le32toh(status->len) > MCLBYTES)
1174		return;
1175
1176	/*
1177	 * Try to allocate a new mbuf for this ring element and load it before
1178	 * processing the current mbuf. If the ring element cannot be loaded,
1179	 * drop the received packet and reuse the old mbuf. In the unlikely
1180	 * case that the old mbuf can't be reloaded either, explicitly panic.
1181	 */
1182	mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1183	if (mnew == NULL) {
1184		ifp->if_ierrors++;
1185		return;
1186	}
1187
1188	bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1189	bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1190
1191	error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1192	    MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1193	if (error != 0) {
1194		m_freem(mnew);
1195
1196		/* try to reload the old mbuf */
1197		error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1198		    mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1199		    &physaddr, 0);
1200		if (error != 0) {
1201			/* very unlikely that it will fail... */
1202			panic("%s: could not load old rx mbuf",
1203			    device_get_name(sc->sc_dev));
1204		}
1205		ifp->if_ierrors++;
1206		return;
1207	}
1208
1209	/*
1210	 * New mbuf successfully loaded, update Rx ring and continue
1211	 * processing.
1212	 */
1213	m = sbuf->m;
1214	sbuf->m = mnew;
1215	sbd->bd->physaddr = htole32(physaddr);
1216
1217	/* finalize mbuf */
1218	m->m_pkthdr.rcvif = ifp;
1219	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1220
1221	rssi = status->rssi + IPW_RSSI_TO_DBM;
1222	nf = -95;
1223	if (ieee80211_radiotap_active(ic)) {
1224		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1225
1226		tap->wr_flags = 0;
1227		tap->wr_antsignal = rssi;
1228		tap->wr_antnoise = nf;
1229	}
1230
1231	if (sc->flags & IPW_FLAG_SCANNING)
1232		ipw_fix_channel(sc, m);
1233
1234	IPW_UNLOCK(sc);
1235	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1236	if (ni != NULL) {
1237		(void) ieee80211_input(ni, m, rssi, nf);
1238		ieee80211_free_node(ni);
1239	} else
1240		(void) ieee80211_input_all(ic, m, rssi, nf);
1241	IPW_LOCK(sc);
1242
1243	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1244}
1245
1246static void
1247ipw_rx_intr(struct ipw_softc *sc)
1248{
1249	struct ipw_status *status;
1250	struct ipw_soft_bd *sbd;
1251	struct ipw_soft_buf *sbuf;
1252	uint32_t r, i;
1253
1254	if (!(sc->flags & IPW_FLAG_FW_INITED))
1255		return;
1256
1257	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1258
1259	bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1260
1261	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1262		status = &sc->status_list[i];
1263		sbd = &sc->srbd_list[i];
1264		sbuf = sbd->priv;
1265
1266		switch (le16toh(status->code) & 0xf) {
1267		case IPW_STATUS_CODE_COMMAND:
1268			ipw_rx_cmd_intr(sc, sbuf);
1269			break;
1270
1271		case IPW_STATUS_CODE_NEWSTATE:
1272			ipw_rx_newstate_intr(sc, sbuf);
1273			break;
1274
1275		case IPW_STATUS_CODE_DATA_802_3:
1276		case IPW_STATUS_CODE_DATA_802_11:
1277			ipw_rx_data_intr(sc, status, sbd, sbuf);
1278			break;
1279
1280		case IPW_STATUS_CODE_NOTIFICATION:
1281			DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1282			    le32toh(status->len), status->flags));
1283			/* XXX maybe drive state machine AUTH->ASSOC? */
1284			break;
1285
1286		default:
1287			device_printf(sc->sc_dev, "unexpected status code %u\n",
1288			    le16toh(status->code));
1289		}
1290
1291		/* firmware was killed, stop processing received frames */
1292		if (!(sc->flags & IPW_FLAG_FW_INITED))
1293			return;
1294
1295		sbd->bd->flags = 0;
1296	}
1297
1298	bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1299
1300	/* kick the firmware */
1301	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1302	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1303}
1304
1305static void
1306ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1307{
1308	struct ipw_soft_hdr *shdr;
1309	struct ipw_soft_buf *sbuf;
1310
1311	switch (sbd->type) {
1312	case IPW_SBD_TYPE_COMMAND:
1313		bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1314		    BUS_DMASYNC_POSTWRITE);
1315		bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1316		break;
1317
1318	case IPW_SBD_TYPE_HEADER:
1319		shdr = sbd->priv;
1320		bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1321		bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1322		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1323		break;
1324
1325	case IPW_SBD_TYPE_DATA:
1326		sbuf = sbd->priv;
1327		bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1328		    BUS_DMASYNC_POSTWRITE);
1329		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1330		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1331
1332		if (sbuf->m->m_flags & M_TXCB)
1333			ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1334		m_freem(sbuf->m);
1335		ieee80211_free_node(sbuf->ni);
1336
1337		sc->sc_tx_timer = 0;
1338		break;
1339	}
1340
1341	sbd->type = IPW_SBD_TYPE_NOASSOC;
1342}
1343
1344static void
1345ipw_tx_intr(struct ipw_softc *sc)
1346{
1347	struct ifnet *ifp = sc->sc_ifp;
1348	struct ipw_soft_bd *sbd;
1349	uint32_t r, i;
1350
1351	if (!(sc->flags & IPW_FLAG_FW_INITED))
1352		return;
1353
1354	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1355
1356	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1357		sbd = &sc->stbd_list[i];
1358
1359		if (sbd->type == IPW_SBD_TYPE_DATA)
1360			ifp->if_opackets++;
1361
1362		ipw_release_sbd(sc, sbd);
1363		sc->txfree++;
1364	}
1365
1366	/* remember what the firmware has processed */
1367	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1368
1369	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1370	ipw_start_locked(ifp);
1371}
1372
1373static void
1374ipw_fatal_error_intr(struct ipw_softc *sc)
1375{
1376	struct ifnet *ifp = sc->sc_ifp;
1377	struct ieee80211com *ic = ifp->if_l2com;
1378	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1379
1380	device_printf(sc->sc_dev, "firmware error\n");
1381	if (vap != NULL)
1382		ieee80211_cancel_scan(vap);
1383	ieee80211_runtask(ic, &sc->sc_init_task);
1384}
1385
1386static void
1387ipw_intr(void *arg)
1388{
1389	struct ipw_softc *sc = arg;
1390	uint32_t r;
1391	IPW_LOCK_DECL;
1392
1393	IPW_LOCK(sc);
1394
1395	r = CSR_READ_4(sc, IPW_CSR_INTR);
1396	if (r == 0 || r == 0xffffffff)
1397		goto done;
1398
1399	/* disable interrupts */
1400	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1401
1402	/* acknowledge all interrupts */
1403	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1404
1405	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1406		ipw_fatal_error_intr(sc);
1407		goto done;
1408	}
1409
1410	if (r & IPW_INTR_FW_INIT_DONE)
1411		wakeup(sc);
1412
1413	if (r & IPW_INTR_RX_TRANSFER)
1414		ipw_rx_intr(sc);
1415
1416	if (r & IPW_INTR_TX_TRANSFER)
1417		ipw_tx_intr(sc);
1418
1419	/* re-enable interrupts */
1420	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1421done:
1422	IPW_UNLOCK(sc);
1423}
1424
1425static void
1426ipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1427{
1428	if (error != 0)
1429		return;
1430
1431	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1432
1433	*(bus_addr_t *)arg = segs[0].ds_addr;
1434}
1435
1436static const char *
1437ipw_cmdname(int cmd)
1438{
1439#define	N(a)	(sizeof(a) / sizeof(a[0]))
1440	static const struct {
1441		int	cmd;
1442		const char *name;
1443	} cmds[] = {
1444		{ IPW_CMD_ADD_MULTICAST,	"ADD_MULTICAST" },
1445		{ IPW_CMD_BROADCAST_SCAN,	"BROADCAST_SCAN" },
1446		{ IPW_CMD_DISABLE,		"DISABLE" },
1447		{ IPW_CMD_DISABLE_PHY,		"DISABLE_PHY" },
1448		{ IPW_CMD_ENABLE,		"ENABLE" },
1449		{ IPW_CMD_PREPARE_POWER_DOWN,	"PREPARE_POWER_DOWN" },
1450		{ IPW_CMD_SET_BASIC_TX_RATES,	"SET_BASIC_TX_RATES" },
1451		{ IPW_CMD_SET_BEACON_INTERVAL,	"SET_BEACON_INTERVAL" },
1452		{ IPW_CMD_SET_CHANNEL,		"SET_CHANNEL" },
1453		{ IPW_CMD_SET_CONFIGURATION,	"SET_CONFIGURATION" },
1454		{ IPW_CMD_SET_DESIRED_BSSID,	"SET_DESIRED_BSSID" },
1455		{ IPW_CMD_SET_ESSID,		"SET_ESSID" },
1456		{ IPW_CMD_SET_FRAG_THRESHOLD,	"SET_FRAG_THRESHOLD" },
1457		{ IPW_CMD_SET_MAC_ADDRESS,	"SET_MAC_ADDRESS" },
1458		{ IPW_CMD_SET_MANDATORY_BSSID,	"SET_MANDATORY_BSSID" },
1459		{ IPW_CMD_SET_MODE,		"SET_MODE" },
1460		{ IPW_CMD_SET_MSDU_TX_RATES,	"SET_MSDU_TX_RATES" },
1461		{ IPW_CMD_SET_POWER_MODE,	"SET_POWER_MODE" },
1462		{ IPW_CMD_SET_RTS_THRESHOLD,	"SET_RTS_THRESHOLD" },
1463		{ IPW_CMD_SET_SCAN_OPTIONS,	"SET_SCAN_OPTIONS" },
1464		{ IPW_CMD_SET_SECURITY_INFO,	"SET_SECURITY_INFO" },
1465		{ IPW_CMD_SET_TX_POWER_INDEX,	"SET_TX_POWER_INDEX" },
1466		{ IPW_CMD_SET_TX_RATES,		"SET_TX_RATES" },
1467		{ IPW_CMD_SET_WEP_FLAGS,	"SET_WEP_FLAGS" },
1468		{ IPW_CMD_SET_WEP_KEY,		"SET_WEP_KEY" },
1469		{ IPW_CMD_SET_WEP_KEY_INDEX,	"SET_WEP_KEY_INDEX" },
1470		{ IPW_CMD_SET_WPA_IE,		"SET_WPA_IE" },
1471
1472	};
1473	static char buf[12];
1474	int i;
1475
1476	for (i = 0; i < N(cmds); i++)
1477		if (cmds[i].cmd == cmd)
1478			return cmds[i].name;
1479	snprintf(buf, sizeof(buf), "%u", cmd);
1480	return buf;
1481#undef N
1482}
1483
1484/*
1485 * Send a command to the firmware and wait for the acknowledgement.
1486 */
1487static int
1488ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1489{
1490	struct ipw_soft_bd *sbd;
1491	bus_addr_t physaddr;
1492	int error;
1493
1494	IPW_LOCK_ASSERT(sc);
1495
1496	if (sc->flags & IPW_FLAG_BUSY) {
1497		device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1498			__func__, ipw_cmdname(type));
1499		return EAGAIN;
1500	}
1501	sc->flags |= IPW_FLAG_BUSY;
1502
1503	sbd = &sc->stbd_list[sc->txcur];
1504
1505	error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1506	    sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1507	if (error != 0) {
1508		device_printf(sc->sc_dev, "could not map command DMA memory\n");
1509		sc->flags &= ~IPW_FLAG_BUSY;
1510		return error;
1511	}
1512
1513	sc->cmd.type = htole32(type);
1514	sc->cmd.subtype = 0;
1515	sc->cmd.len = htole32(len);
1516	sc->cmd.seq = 0;
1517	memcpy(sc->cmd.data, data, len);
1518
1519	sbd->type = IPW_SBD_TYPE_COMMAND;
1520	sbd->bd->physaddr = htole32(physaddr);
1521	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1522	sbd->bd->nfrag = 1;
1523	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1524	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1525
1526	bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1527	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1528
1529#ifdef IPW_DEBUG
1530	if (ipw_debug >= 4) {
1531		printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1532		    0, 0, len);
1533		/* Print the data buffer in the higher debug level */
1534		if (ipw_debug >= 9 && len > 0) {
1535			printf(" data: 0x");
1536			for (int i = 1; i <= len; i++)
1537				printf("%1D", (u_char *)data + len - i, "");
1538		}
1539		printf("\n");
1540	}
1541#endif
1542
1543	/* kick firmware */
1544	sc->txfree--;
1545	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1546	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1547
1548	/* wait at most one second for command to complete */
1549	error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1550	if (error != 0) {
1551		device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1552		    __func__, ipw_cmdname(type), error);
1553		sc->flags &= ~IPW_FLAG_BUSY;
1554		return (error);
1555	}
1556	return (0);
1557}
1558
1559static int
1560ipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1561{
1562	struct ipw_softc *sc = ifp->if_softc;
1563	struct ieee80211com *ic = ifp->if_l2com;
1564	struct ieee80211vap *vap = ni->ni_vap;
1565	struct ieee80211_frame *wh;
1566	struct ipw_soft_bd *sbd;
1567	struct ipw_soft_hdr *shdr;
1568	struct ipw_soft_buf *sbuf;
1569	struct ieee80211_key *k;
1570	struct mbuf *mnew;
1571	bus_dma_segment_t segs[IPW_MAX_NSEG];
1572	bus_addr_t physaddr;
1573	int nsegs, error, i;
1574
1575	wh = mtod(m0, struct ieee80211_frame *);
1576
1577	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1578		k = ieee80211_crypto_encap(ni, m0);
1579		if (k == NULL) {
1580			m_freem(m0);
1581			return ENOBUFS;
1582		}
1583		/* packet header may have moved, reset our local pointer */
1584		wh = mtod(m0, struct ieee80211_frame *);
1585	}
1586
1587	if (ieee80211_radiotap_active_vap(vap)) {
1588		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1589
1590		tap->wt_flags = 0;
1591
1592		ieee80211_radiotap_tx(vap, m0);
1593	}
1594
1595	shdr = SLIST_FIRST(&sc->free_shdr);
1596	sbuf = SLIST_FIRST(&sc->free_sbuf);
1597	KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1598
1599	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1600	shdr->hdr.subtype = 0;
1601	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1602	shdr->hdr.encrypt = 0;
1603	shdr->hdr.keyidx = 0;
1604	shdr->hdr.keysz = 0;
1605	shdr->hdr.fragmentsz = 0;
1606	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1607	if (ic->ic_opmode == IEEE80211_M_STA)
1608		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1609	else
1610		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1611
1612	/* trim IEEE802.11 header */
1613	m_adj(m0, sizeof (struct ieee80211_frame));
1614
1615	error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1616	    &nsegs, 0);
1617	if (error != 0 && error != EFBIG) {
1618		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1619		    error);
1620		m_freem(m0);
1621		return error;
1622	}
1623	if (error != 0) {
1624		mnew = m_defrag(m0, M_DONTWAIT);
1625		if (mnew == NULL) {
1626			device_printf(sc->sc_dev,
1627			    "could not defragment mbuf\n");
1628			m_freem(m0);
1629			return ENOBUFS;
1630		}
1631		m0 = mnew;
1632
1633		error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1634		    segs, &nsegs, 0);
1635		if (error != 0) {
1636			device_printf(sc->sc_dev,
1637			    "could not map mbuf (error %d)\n", error);
1638			m_freem(m0);
1639			return error;
1640		}
1641	}
1642
1643	error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1644	    sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1645	if (error != 0) {
1646		device_printf(sc->sc_dev, "could not map header DMA memory\n");
1647		bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1648		m_freem(m0);
1649		return error;
1650	}
1651
1652	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1653	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1654
1655	sbd = &sc->stbd_list[sc->txcur];
1656	sbd->type = IPW_SBD_TYPE_HEADER;
1657	sbd->priv = shdr;
1658	sbd->bd->physaddr = htole32(physaddr);
1659	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1660	sbd->bd->nfrag = 1 + nsegs;
1661	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1662	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1663
1664	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1665	    shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1666	    shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1667	    ":"));
1668
1669	sc->txfree--;
1670	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1671
1672	sbuf->m = m0;
1673	sbuf->ni = ni;
1674
1675	for (i = 0; i < nsegs; i++) {
1676		sbd = &sc->stbd_list[sc->txcur];
1677
1678		sbd->bd->physaddr = htole32(segs[i].ds_addr);
1679		sbd->bd->len = htole32(segs[i].ds_len);
1680		sbd->bd->nfrag = 0;
1681		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1682		if (i == nsegs - 1) {
1683			sbd->type = IPW_SBD_TYPE_DATA;
1684			sbd->priv = sbuf;
1685			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1686		} else {
1687			sbd->type = IPW_SBD_TYPE_NOASSOC;
1688			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1689		}
1690
1691		DPRINTFN(5, ("sending fragment (%d)\n", i));
1692
1693		sc->txfree--;
1694		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1695	}
1696
1697	bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1698	bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1699	bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1700
1701	/* kick firmware */
1702	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1703
1704	return 0;
1705}
1706
1707static int
1708ipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1709	const struct ieee80211_bpf_params *params)
1710{
1711	/* no support; just discard */
1712	m_freem(m);
1713	ieee80211_free_node(ni);
1714	return 0;
1715}
1716
1717static void
1718ipw_start(struct ifnet *ifp)
1719{
1720	struct ipw_softc *sc = ifp->if_softc;
1721	IPW_LOCK_DECL;
1722
1723	IPW_LOCK(sc);
1724	ipw_start_locked(ifp);
1725	IPW_UNLOCK(sc);
1726}
1727
1728static void
1729ipw_start_locked(struct ifnet *ifp)
1730{
1731	struct ipw_softc *sc = ifp->if_softc;
1732	struct ieee80211_node *ni;
1733	struct mbuf *m;
1734
1735	IPW_LOCK_ASSERT(sc);
1736
1737	for (;;) {
1738		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1739		if (m == NULL)
1740			break;
1741		if (sc->txfree < 1 + IPW_MAX_NSEG) {
1742			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1743			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1744			break;
1745		}
1746		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1747		if (ipw_tx_start(ifp, m, ni) != 0) {
1748			ieee80211_free_node(ni);
1749			ifp->if_oerrors++;
1750			break;
1751		}
1752		/* start watchdog timer */
1753		sc->sc_tx_timer = 5;
1754	}
1755}
1756
1757static void
1758ipw_watchdog(void *arg)
1759{
1760	struct ipw_softc *sc = arg;
1761	struct ifnet *ifp = sc->sc_ifp;
1762	struct ieee80211com *ic = ifp->if_l2com;
1763
1764	IPW_LOCK_ASSERT(sc);
1765
1766	if (sc->sc_tx_timer > 0) {
1767		if (--sc->sc_tx_timer == 0) {
1768			if_printf(ifp, "device timeout\n");
1769			ifp->if_oerrors++;
1770			taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1771		}
1772	}
1773	if (sc->sc_scan_timer > 0) {
1774		if (--sc->sc_scan_timer == 0) {
1775			DPRINTFN(3, ("Scan timeout\n"));
1776			/* End the scan */
1777			if (sc->flags & IPW_FLAG_SCANNING) {
1778				ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1779				sc->flags &= ~IPW_FLAG_SCANNING;
1780			}
1781		}
1782	}
1783	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1784		callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1785}
1786
1787static int
1788ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1789{
1790	struct ipw_softc *sc = ifp->if_softc;
1791	struct ieee80211com *ic = ifp->if_l2com;
1792	struct ifreq *ifr = (struct ifreq *) data;
1793	int error = 0, startall = 0;
1794	IPW_LOCK_DECL;
1795
1796	switch (cmd) {
1797	case SIOCSIFFLAGS:
1798		IPW_LOCK(sc);
1799		if (ifp->if_flags & IFF_UP) {
1800			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1801				ipw_init_locked(sc);
1802				startall = 1;
1803			}
1804		} else {
1805			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1806				ipw_stop_locked(sc);
1807		}
1808		IPW_UNLOCK(sc);
1809		if (startall)
1810			ieee80211_start_all(ic);
1811		break;
1812	case SIOCGIFMEDIA:
1813		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1814		break;
1815	case SIOCGIFADDR:
1816		error = ether_ioctl(ifp, cmd, data);
1817		break;
1818	default:
1819		error = EINVAL;
1820		break;
1821	}
1822	return error;
1823}
1824
1825static void
1826ipw_stop_master(struct ipw_softc *sc)
1827{
1828	uint32_t tmp;
1829	int ntries;
1830
1831	/* disable interrupts */
1832	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1833
1834	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1835	for (ntries = 0; ntries < 50; ntries++) {
1836		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1837			break;
1838		DELAY(10);
1839	}
1840	if (ntries == 50)
1841		device_printf(sc->sc_dev, "timeout waiting for master\n");
1842
1843	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1844	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1845
1846	/* Clear all flags except the following */
1847	sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1848}
1849
1850static int
1851ipw_reset(struct ipw_softc *sc)
1852{
1853	uint32_t tmp;
1854	int ntries;
1855
1856	ipw_stop_master(sc);
1857
1858	/* move adapter to D0 state */
1859	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1860	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1861
1862	/* wait for clock stabilization */
1863	for (ntries = 0; ntries < 1000; ntries++) {
1864		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1865			break;
1866		DELAY(200);
1867	}
1868	if (ntries == 1000)
1869		return EIO;
1870
1871	tmp =  CSR_READ_4(sc, IPW_CSR_RST);
1872	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1873
1874	DELAY(10);
1875
1876	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1877	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1878
1879	return 0;
1880}
1881
1882static int
1883ipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1884{
1885	int ms = hz < 1000 ? 1 : hz/10;
1886	int i, error;
1887
1888	for (i = 0; i < 100; i++) {
1889		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1890			return 0;
1891		error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1892		if (error == 0 || error != EWOULDBLOCK)
1893			return 0;
1894	}
1895	DPRINTF(("%s: timeout waiting for %s\n",
1896		__func__, waitfor ? "disable" : "enable"));
1897	return ETIMEDOUT;
1898}
1899
1900static int
1901ipw_enable(struct ipw_softc *sc)
1902{
1903	int error;
1904
1905	if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1906		DPRINTF(("Enable adapter\n"));
1907		error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1908		if (error != 0)
1909			return error;
1910		error = ipw_waitfordisable(sc, 0);
1911		if (error != 0)
1912			return error;
1913		sc->flags |= IPW_FLAG_ENABLED;
1914	}
1915	return 0;
1916}
1917
1918static int
1919ipw_disable(struct ipw_softc *sc)
1920{
1921	int error;
1922
1923	if (sc->flags & IPW_FLAG_ENABLED) {
1924		DPRINTF(("Disable adapter\n"));
1925		error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1926		if (error != 0)
1927			return error;
1928		error = ipw_waitfordisable(sc, 1);
1929		if (error != 0)
1930			return error;
1931		sc->flags &= ~IPW_FLAG_ENABLED;
1932	}
1933	return 0;
1934}
1935
1936/*
1937 * Upload the microcode to the device.
1938 */
1939static int
1940ipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1941{
1942	int ntries;
1943
1944	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1945	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1946
1947	MEM_WRITE_2(sc, 0x220000, 0x0703);
1948	MEM_WRITE_2(sc, 0x220000, 0x0707);
1949
1950	MEM_WRITE_1(sc, 0x210014, 0x72);
1951	MEM_WRITE_1(sc, 0x210014, 0x72);
1952
1953	MEM_WRITE_1(sc, 0x210000, 0x40);
1954	MEM_WRITE_1(sc, 0x210000, 0x00);
1955	MEM_WRITE_1(sc, 0x210000, 0x40);
1956
1957	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1958
1959	MEM_WRITE_1(sc, 0x210000, 0x00);
1960	MEM_WRITE_1(sc, 0x210000, 0x00);
1961	MEM_WRITE_1(sc, 0x210000, 0x80);
1962
1963	MEM_WRITE_2(sc, 0x220000, 0x0703);
1964	MEM_WRITE_2(sc, 0x220000, 0x0707);
1965
1966	MEM_WRITE_1(sc, 0x210014, 0x72);
1967	MEM_WRITE_1(sc, 0x210014, 0x72);
1968
1969	MEM_WRITE_1(sc, 0x210000, 0x00);
1970	MEM_WRITE_1(sc, 0x210000, 0x80);
1971
1972	for (ntries = 0; ntries < 10; ntries++) {
1973		if (MEM_READ_1(sc, 0x210000) & 1)
1974			break;
1975		DELAY(10);
1976	}
1977	if (ntries == 10) {
1978		device_printf(sc->sc_dev,
1979		    "timeout waiting for ucode to initialize\n");
1980		return EIO;
1981	}
1982
1983	MEM_WRITE_4(sc, 0x3000e0, 0);
1984
1985	return 0;
1986}
1987
1988/* set of macros to handle unaligned little endian data in firmware image */
1989#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1990#define GETLE16(p) ((p)[0] | (p)[1] << 8)
1991static int
1992ipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
1993{
1994	const uint8_t *p, *end;
1995	uint32_t tmp, dst;
1996	uint16_t len;
1997	int error;
1998
1999	p = fw;
2000	end = fw + size;
2001	while (p < end) {
2002		dst = GETLE32(p); p += 4;
2003		len = GETLE16(p); p += 2;
2004
2005		ipw_write_mem_1(sc, dst, p, len);
2006		p += len;
2007	}
2008
2009	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
2010	    IPW_IO_LED_OFF);
2011
2012	/* enable interrupts */
2013	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
2014
2015	/* kick the firmware */
2016	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
2017
2018	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
2019	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
2020
2021	/* wait at most one second for firmware initialization to complete */
2022	if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
2023		device_printf(sc->sc_dev, "timeout waiting for firmware "
2024		    "initialization to complete\n");
2025		return error;
2026	}
2027
2028	tmp = CSR_READ_4(sc, IPW_CSR_IO);
2029	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2030	    IPW_IO_GPIO3_MASK);
2031
2032	return 0;
2033}
2034
2035static int
2036ipw_setwepkeys(struct ipw_softc *sc)
2037{
2038	struct ifnet *ifp = sc->sc_ifp;
2039	struct ieee80211com *ic = ifp->if_l2com;
2040	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2041	struct ipw_wep_key wepkey;
2042	struct ieee80211_key *wk;
2043	int error, i;
2044
2045	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2046		wk = &vap->iv_nw_keys[i];
2047
2048		if (wk->wk_cipher == NULL ||
2049		    wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2050			continue;
2051
2052		wepkey.idx = i;
2053		wepkey.len = wk->wk_keylen;
2054		memset(wepkey.key, 0, sizeof wepkey.key);
2055		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2056		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2057		    wepkey.len));
2058		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2059		    sizeof wepkey);
2060		if (error != 0)
2061			return error;
2062	}
2063	return 0;
2064}
2065
2066static int
2067ipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2068{
2069	struct ipw_wpa_ie wpaie;
2070
2071	memset(&wpaie, 0, sizeof(wpaie));
2072	wpaie.len = htole32(ielen);
2073	/* XXX verify length */
2074	memcpy(&wpaie.ie, ie, ielen);
2075	DPRINTF(("Setting WPA IE\n"));
2076	return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2077}
2078
2079static int
2080ipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2081{
2082	static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2083
2084	if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2085		DPRINTF(("Setting mandatory BSSID to null\n"));
2086		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2087	} else {
2088		DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2089		return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2090			bssid, IEEE80211_ADDR_LEN);
2091	}
2092}
2093
2094static int
2095ipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2096{
2097	if (ssidlen == 0) {
2098		/*
2099		 * A bug in the firmware breaks the ``don't associate''
2100		 * bit in the scan options command.  To compensate for
2101		 * this install a bogus ssid when no ssid is specified
2102		 * so the firmware won't try to associate.
2103		 */
2104		DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2105		return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2106			"\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2107			"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2108			"\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2109			"\x3c\x3d", IEEE80211_NWID_LEN);
2110	} else {
2111#ifdef IPW_DEBUG
2112		if (ipw_debug > 0) {
2113			printf("Setting ESSID to ");
2114			ieee80211_print_essid(ssid, ssidlen);
2115			printf("\n");
2116		}
2117#endif
2118		return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2119	}
2120}
2121
2122static int
2123ipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2124{
2125	struct ipw_scan_options opts;
2126
2127	DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2128	opts.channels = htole32(chanmask);
2129	opts.flags = htole32(flags);
2130	return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2131}
2132
2133static int
2134ipw_scan(struct ipw_softc *sc)
2135{
2136	uint32_t params;
2137	int error;
2138
2139	DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2140
2141	if (sc->flags & IPW_FLAG_SCANNING)
2142		return (EBUSY);
2143	sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2144
2145	/* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2146	error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2147	if (error != 0)
2148		goto done;
2149
2150	/*
2151	 * Setup null/bogus ssid so firmware doesn't use any previous
2152	 * ssid to try and associate.  This is because the ``don't
2153	 * associate'' option bit is broken (sigh).
2154	 */
2155	error = ipw_setssid(sc, NULL, 0);
2156	if (error != 0)
2157		goto done;
2158
2159	/*
2160	 * NB: the adapter may be disabled on association lost;
2161	 *     if so just re-enable it to kick off scanning.
2162	 */
2163	DPRINTF(("Starting scan\n"));
2164	sc->sc_scan_timer = 3;
2165	if (sc->flags & IPW_FLAG_ENABLED) {
2166		params = 0;				/* XXX? */
2167		error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2168				&params, sizeof(params));
2169	} else
2170		error = ipw_enable(sc);
2171done:
2172	if (error != 0) {
2173		DPRINTF(("Scan failed\n"));
2174		sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2175	}
2176	return (error);
2177}
2178
2179static int
2180ipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2181{
2182	struct ifnet *ifp = sc->sc_ifp;
2183	struct ieee80211com *ic = ifp->if_l2com;
2184	uint32_t data;
2185	int error;
2186
2187	data = htole32(ieee80211_chan2ieee(ic, chan));
2188	DPRINTF(("Setting channel to %u\n", le32toh(data)));
2189	error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2190	if (error == 0)
2191		ipw_setcurchan(sc, chan);
2192	return error;
2193}
2194
2195static void
2196ipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2197{
2198	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2199	struct ipw_softc *sc = ifp->if_softc;
2200	struct ieee80211_node *ni = vap->iv_bss;
2201	struct ipw_security security;
2202	uint32_t data;
2203	int error;
2204	IPW_LOCK_DECL;
2205
2206	IPW_LOCK(sc);
2207	error = ipw_disable(sc);
2208	if (error != 0)
2209		goto done;
2210
2211	memset(&security, 0, sizeof security);
2212	security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2213	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2214	security.ciphers = htole32(IPW_CIPHER_NONE);
2215	DPRINTF(("Setting authmode to %u\n", security.authmode));
2216	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2217	    sizeof security);
2218	if (error != 0)
2219		goto done;
2220
2221	data = htole32(vap->iv_rtsthreshold);
2222	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2223	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2224	if (error != 0)
2225		goto done;
2226
2227	data = htole32(vap->iv_fragthreshold);
2228	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2229	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2230	if (error != 0)
2231		goto done;
2232
2233	if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2234		error = ipw_setwepkeys(sc);
2235		if (error != 0)
2236			goto done;
2237
2238		if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2239			data = htole32(vap->iv_def_txkey);
2240			DPRINTF(("Setting wep tx key index to %u\n",
2241				le32toh(data)));
2242			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2243			    sizeof data);
2244			if (error != 0)
2245				goto done;
2246		}
2247	}
2248
2249	data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2250	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2251	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2252	if (error != 0)
2253		goto done;
2254
2255	error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2256	if (error != 0)
2257		goto done;
2258
2259	error = ipw_setbssid(sc, ni->ni_bssid);
2260	if (error != 0)
2261		goto done;
2262
2263	if (vap->iv_appie_assocreq != NULL) {
2264		struct ieee80211_appie *ie = vap->iv_appie_assocreq;
2265		error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2266		if (error != 0)
2267			goto done;
2268	}
2269	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2270		error = ipw_setchannel(sc, ni->ni_chan);
2271		if (error != 0)
2272			goto done;
2273	}
2274
2275	/* lock scan to ap's channel and enable associate */
2276	error = ipw_setscanopts(sc,
2277	    1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2278	if (error != 0)
2279		goto done;
2280
2281	error = ipw_enable(sc);		/* finally, enable adapter */
2282	if (error == 0)
2283		sc->flags |= IPW_FLAG_ASSOCIATING;
2284done:
2285	IPW_UNLOCK(sc);
2286}
2287
2288static void
2289ipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2290{
2291	struct ifnet *ifp = vap->iv_ic->ic_ifp;
2292	struct ieee80211_node *ni = vap->iv_bss;
2293	struct ipw_softc *sc = ifp->if_softc;
2294	IPW_LOCK_DECL;
2295
2296	IPW_LOCK(sc);
2297	DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2298	/*
2299	 * NB: don't try to do this if ipw_stop_master has
2300	 *     shutdown the firmware and disabled interrupts.
2301	 */
2302	if (sc->flags & IPW_FLAG_FW_INITED) {
2303		sc->flags &= ~IPW_FLAG_ASSOCIATED;
2304		/*
2305		 * NB: firmware currently ignores bssid parameter, but
2306		 *     supply it in case this changes (follow linux driver).
2307		 */
2308		(void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2309			ni->ni_bssid, IEEE80211_ADDR_LEN);
2310	}
2311	IPW_UNLOCK(sc);
2312}
2313
2314/*
2315 * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
2316 * It is called on firmware panics or on watchdog timeouts.
2317 */
2318static void
2319ipw_init_task(void *context, int pending)
2320{
2321	ipw_init(context);
2322}
2323
2324static void
2325ipw_init(void *priv)
2326{
2327	struct ipw_softc *sc = priv;
2328	struct ifnet *ifp = sc->sc_ifp;
2329	struct ieee80211com *ic = ifp->if_l2com;
2330	IPW_LOCK_DECL;
2331
2332	IPW_LOCK(sc);
2333	ipw_init_locked(sc);
2334	IPW_UNLOCK(sc);
2335
2336	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2337		ieee80211_start_all(ic);		/* start all vap's */
2338}
2339
2340static void
2341ipw_init_locked(struct ipw_softc *sc)
2342{
2343	struct ifnet *ifp = sc->sc_ifp;
2344	struct ieee80211com *ic = ifp->if_l2com;
2345	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2346	const struct firmware *fp;
2347	const struct ipw_firmware_hdr *hdr;
2348	const char *fw;
2349
2350	IPW_LOCK_ASSERT(sc);
2351
2352	DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2353		ieee80211_state_name[vap->iv_state], sc->flags));
2354
2355	/*
2356	 * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
2357	 * when loading the firmware and we don't want to be called during this
2358	 * operation.
2359	 */
2360	if (sc->flags & IPW_FLAG_INIT_LOCKED)
2361		return;
2362	sc->flags |= IPW_FLAG_INIT_LOCKED;
2363
2364	ipw_stop_locked(sc);
2365
2366	if (ipw_reset(sc) != 0) {
2367		device_printf(sc->sc_dev, "could not reset adapter\n");
2368		goto fail;
2369	}
2370
2371	if (sc->sc_firmware == NULL) {
2372		device_printf(sc->sc_dev, "no firmware\n");
2373		goto fail;
2374	}
2375	/* NB: consistency already checked on load */
2376	fp = sc->sc_firmware;
2377	hdr = (const struct ipw_firmware_hdr *)fp->data;
2378
2379	DPRINTF(("Loading firmware image '%s'\n", fp->name));
2380	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2381	if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2382		device_printf(sc->sc_dev, "could not load microcode\n");
2383		goto fail;
2384	}
2385
2386	ipw_stop_master(sc);
2387
2388	/*
2389	 * Setup tx, rx and status rings.
2390	 */
2391	sc->txold = IPW_NTBD - 1;
2392	sc->txcur = 0;
2393	sc->txfree = IPW_NTBD - 2;
2394	sc->rxcur = IPW_NRBD - 1;
2395
2396	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
2397	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2398	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2399	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2400
2401	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
2402	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2403	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2404	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2405
2406	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2407
2408	fw = (const char *)fp->data + sizeof *hdr;
2409	if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2410		device_printf(sc->sc_dev, "could not load firmware\n");
2411		goto fail;
2412	}
2413
2414	sc->flags |= IPW_FLAG_FW_INITED;
2415
2416	/* retrieve information tables base addresses */
2417	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2418	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2419
2420	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2421
2422	if (ipw_config(sc) != 0) {
2423		device_printf(sc->sc_dev, "device configuration failed\n");
2424		goto fail;
2425	}
2426
2427	callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2428	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2429	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2430
2431	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2432	return;
2433
2434fail:
2435	ipw_stop_locked(sc);
2436	sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2437}
2438
2439static int
2440ipw_config(struct ipw_softc *sc)
2441{
2442	struct ifnet *ifp = sc->sc_ifp;
2443	struct ieee80211com *ic = ifp->if_l2com;
2444	struct ipw_configuration config;
2445	uint32_t data;
2446	int error;
2447
2448	error = ipw_disable(sc);
2449	if (error != 0)
2450		return error;
2451
2452	switch (ic->ic_opmode) {
2453	case IEEE80211_M_STA:
2454	case IEEE80211_M_HOSTAP:
2455	case IEEE80211_M_WDS:		/* XXX */
2456		data = htole32(IPW_MODE_BSS);
2457		break;
2458	case IEEE80211_M_IBSS:
2459	case IEEE80211_M_AHDEMO:
2460		data = htole32(IPW_MODE_IBSS);
2461		break;
2462	case IEEE80211_M_MONITOR:
2463		data = htole32(IPW_MODE_MONITOR);
2464		break;
2465	}
2466	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2467	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2468	if (error != 0)
2469		return error;
2470
2471	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2472	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2473		error = ipw_setchannel(sc, ic->ic_curchan);
2474		if (error != 0)
2475			return error;
2476	}
2477
2478	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2479		return ipw_enable(sc);
2480
2481	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2482	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2483	if (ic->ic_opmode == IEEE80211_M_IBSS)
2484		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2485	if (ifp->if_flags & IFF_PROMISC)
2486		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2487	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2488	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2489	DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2490	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2491	if (error != 0)
2492		return error;
2493
2494	data = htole32(0x3); /* 1, 2 */
2495	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2496	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2497	if (error != 0)
2498		return error;
2499
2500	/* NB: use the same rate set */
2501	DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2502	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2503	if (error != 0)
2504		return error;
2505
2506	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2507	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2508	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2509	if (error != 0)
2510		return error;
2511
2512	data = htole32(IPW_POWER_MODE_CAM);
2513	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2514	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2515	if (error != 0)
2516		return error;
2517
2518	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2519		data = htole32(32); /* default value */
2520		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2521		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2522		    sizeof data);
2523		if (error != 0)
2524			return error;
2525	}
2526
2527	return 0;
2528}
2529
2530static void
2531ipw_stop(void *priv)
2532{
2533	struct ipw_softc *sc = priv;
2534	IPW_LOCK_DECL;
2535
2536	IPW_LOCK(sc);
2537	ipw_stop_locked(sc);
2538	IPW_UNLOCK(sc);
2539}
2540
2541static void
2542ipw_stop_locked(struct ipw_softc *sc)
2543{
2544	struct ifnet *ifp = sc->sc_ifp;
2545	int i;
2546
2547	IPW_LOCK_ASSERT(sc);
2548
2549	callout_stop(&sc->sc_wdtimer);
2550	ipw_stop_master(sc);
2551
2552	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2553
2554	/*
2555	 * Release tx buffers.
2556	 */
2557	for (i = 0; i < IPW_NTBD; i++)
2558		ipw_release_sbd(sc, &sc->stbd_list[i]);
2559
2560	sc->sc_tx_timer = 0;
2561	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2562}
2563
2564static int
2565ipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2566{
2567	struct ipw_softc *sc = arg1;
2568	uint32_t i, size, buf[256];
2569
2570	memset(buf, 0, sizeof buf);
2571
2572	if (!(sc->flags & IPW_FLAG_FW_INITED))
2573		return SYSCTL_OUT(req, buf, sizeof buf);
2574
2575	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2576
2577	size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2578	for (i = 1; i < size; i++)
2579		buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2580
2581	return SYSCTL_OUT(req, buf, size);
2582}
2583
2584static int
2585ipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2586{
2587	struct ipw_softc *sc = arg1;
2588	int val;
2589
2590	val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2591	        (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2592
2593	return SYSCTL_OUT(req, &val, sizeof val);
2594}
2595
2596static uint32_t
2597ipw_read_table1(struct ipw_softc *sc, uint32_t off)
2598{
2599	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2600}
2601
2602static void
2603ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2604{
2605	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2606}
2607
2608#if 0
2609static int
2610ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2611{
2612	uint32_t addr, info;
2613	uint16_t count, size;
2614	uint32_t total;
2615
2616	/* addr[4] + count[2] + size[2] */
2617	addr = MEM_READ_4(sc, sc->table2_base + off);
2618	info = MEM_READ_4(sc, sc->table2_base + off + 4);
2619
2620	count = info >> 16;
2621	size = info & 0xffff;
2622	total = count * size;
2623
2624	if (total > *len) {
2625		*len = total;
2626		return EINVAL;
2627	}
2628
2629	*len = total;
2630	ipw_read_mem_1(sc, addr, buf, total);
2631
2632	return 0;
2633}
2634
2635static void
2636ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2637    bus_size_t count)
2638{
2639	for (; count > 0; offset++, datap++, count--) {
2640		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2641		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2642	}
2643}
2644#endif
2645
2646static void
2647ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2648    bus_size_t count)
2649{
2650	for (; count > 0; offset++, datap++, count--) {
2651		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2652		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2653	}
2654}
2655
2656static void
2657ipw_scan_start(struct ieee80211com *ic)
2658{
2659	struct ifnet *ifp = ic->ic_ifp;
2660	struct ipw_softc *sc = ifp->if_softc;
2661	IPW_LOCK_DECL;
2662
2663	IPW_LOCK(sc);
2664	ipw_scan(sc);
2665	IPW_UNLOCK(sc);
2666}
2667
2668static void
2669ipw_set_channel(struct ieee80211com *ic)
2670{
2671	struct ifnet *ifp = ic->ic_ifp;
2672	struct ipw_softc *sc = ifp->if_softc;
2673	IPW_LOCK_DECL;
2674
2675	IPW_LOCK(sc);
2676	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2677		ipw_disable(sc);
2678		ipw_setchannel(sc, ic->ic_curchan);
2679		ipw_enable(sc);
2680	}
2681	IPW_UNLOCK(sc);
2682}
2683
2684static void
2685ipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2686{
2687	/* NB: all channels are scanned at once */
2688}
2689
2690static void
2691ipw_scan_mindwell(struct ieee80211_scan_state *ss)
2692{
2693	/* NB: don't try to abort scan; wait for firmware to finish */
2694}
2695
2696static void
2697ipw_scan_end(struct ieee80211com *ic)
2698{
2699	struct ifnet *ifp = ic->ic_ifp;
2700	struct ipw_softc *sc = ifp->if_softc;
2701	IPW_LOCK_DECL;
2702
2703	IPW_LOCK(sc);
2704	sc->flags &= ~IPW_FLAG_SCANNING;
2705	IPW_UNLOCK(sc);
2706}
2707