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