if_ipw.c revision 1.119
1/*	$OpenBSD: if_ipw.c,v 1.119 2017/01/22 10:17:38 dlg Exp $	*/
2
3/*-
4 * Copyright (c) 2004-2008
5 *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/*
21 * Driver for Intel PRO/Wireless 2100 802.11 network adapters.
22 */
23
24#include "bpfilter.h"
25
26#include <sys/param.h>
27#include <sys/sockio.h>
28#include <sys/task.h>
29#include <sys/mbuf.h>
30#include <sys/kernel.h>
31#include <sys/rwlock.h>
32#include <sys/socket.h>
33#include <sys/systm.h>
34#include <sys/conf.h>
35#include <sys/device.h>
36#include <sys/endian.h>
37
38#include <machine/bus.h>
39#include <machine/intr.h>
40
41#include <dev/pci/pcireg.h>
42#include <dev/pci/pcivar.h>
43#include <dev/pci/pcidevs.h>
44
45#if NBPFILTER > 0
46#include <net/bpf.h>
47#endif
48#include <net/if.h>
49#include <net/if_dl.h>
50#include <net/if_media.h>
51
52#include <netinet/in.h>
53#include <netinet/if_ether.h>
54
55#include <net80211/ieee80211_var.h>
56#include <net80211/ieee80211_radiotap.h>
57
58#include <dev/pci/if_ipwreg.h>
59#include <dev/pci/if_ipwvar.h>
60
61int		ipw_match(struct device *, void *, void *);
62void		ipw_attach(struct device *, struct device *, void *);
63int		ipw_activate(struct device *, int);
64void		ipw_wakeup(struct ipw_softc *);
65int		ipw_dma_alloc(struct ipw_softc *);
66void		ipw_release(struct ipw_softc *);
67int		ipw_media_change(struct ifnet *);
68void		ipw_media_status(struct ifnet *, struct ifmediareq *);
69int		ipw_newstate(struct ieee80211com *, enum ieee80211_state, int);
70uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
71void		ipw_command_intr(struct ipw_softc *, struct ipw_soft_buf *);
72void		ipw_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
73void		ipw_data_intr(struct ipw_softc *, struct ipw_status *,
74		    struct ipw_soft_bd *, struct ipw_soft_buf *);
75void		ipw_notification_intr(struct ipw_softc *,
76		    struct ipw_soft_buf *);
77void		ipw_rx_intr(struct ipw_softc *);
78void		ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
79void		ipw_tx_intr(struct ipw_softc *);
80int		ipw_intr(void *);
81int		ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
82int		ipw_send_mgmt(struct ieee80211com *, struct ieee80211_node *,
83		    int, int, int);
84int		ipw_tx_start(struct ifnet *, struct mbuf *,
85		    struct ieee80211_node *);
86void		ipw_start(struct ifnet *);
87void		ipw_watchdog(struct ifnet *);
88int		ipw_ioctl(struct ifnet *, u_long, caddr_t);
89uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
90void		ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
91int		ipw_read_table2(struct ipw_softc *, uint32_t, void *,
92		    uint32_t *);
93void		ipw_stop_master(struct ipw_softc *);
94int		ipw_reset(struct ipw_softc *);
95int		ipw_load_ucode(struct ipw_softc *, u_char *, int);
96int		ipw_load_firmware(struct ipw_softc *, u_char *, int);
97int		ipw_read_firmware(struct ipw_softc *, struct ipw_firmware *);
98void		ipw_scan(void *);
99void		ipw_auth_and_assoc(void *);
100int		ipw_config(struct ipw_softc *);
101int		ipw_init(struct ifnet *);
102void		ipw_stop(struct ifnet *, int);
103void		ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
104		    bus_size_t);
105void		ipw_write_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
106		    bus_size_t);
107
108static __inline uint8_t
109MEM_READ_1(struct ipw_softc *sc, uint32_t addr)
110{
111	CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
112	return CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA);
113}
114
115static __inline uint32_t
116MEM_READ_4(struct ipw_softc *sc, uint32_t addr)
117{
118	CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
119	return CSR_READ_4(sc, IPW_CSR_INDIRECT_DATA);
120}
121
122#ifdef IPW_DEBUG
123#define DPRINTF(x)	do { if (ipw_debug > 0) printf x; } while (0)
124#define DPRINTFN(n, x)	do { if (ipw_debug >= (n)) printf x; } while (0)
125int ipw_debug = 0;
126#else
127#define DPRINTF(x)
128#define DPRINTFN(n, x)
129#endif
130
131struct cfattach ipw_ca = {
132	sizeof (struct ipw_softc), ipw_match, ipw_attach, NULL,
133	ipw_activate
134};
135
136int
137ipw_match(struct device *parent, void *match, void *aux)
138{
139	struct pci_attach_args *pa = aux;
140
141	if (PCI_VENDOR (pa->pa_id) == PCI_VENDOR_INTEL &&
142	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2100)
143		return 1;
144
145	return 0;
146}
147
148/* Base Address Register */
149#define IPW_PCI_BAR0	0x10
150
151void
152ipw_attach(struct device *parent, struct device *self, void *aux)
153{
154	struct ipw_softc *sc = (struct ipw_softc *)self;
155	struct ieee80211com *ic = &sc->sc_ic;
156	struct ifnet *ifp = &ic->ic_if;
157	struct pci_attach_args *pa = aux;
158	const char *intrstr;
159	bus_space_tag_t memt;
160	bus_space_handle_t memh;
161	bus_addr_t base;
162	pci_intr_handle_t ih;
163	pcireg_t data;
164	uint16_t val;
165	int error, i;
166
167	sc->sc_pct = pa->pa_pc;
168	sc->sc_pcitag = pa->pa_tag,
169
170	/* clear device specific PCI configuration register 0x41 */
171	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
172	data &= ~0x0000ff00;
173	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
174
175	/* map the register window */
176	error = pci_mapreg_map(pa, IPW_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
177	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz, 0);
178	if (error != 0) {
179		printf(": can't map mem space\n");
180		return;
181	}
182
183	sc->sc_st = memt;
184	sc->sc_sh = memh;
185	sc->sc_dmat = pa->pa_dmat;
186
187	/* disable interrupts */
188	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
189
190	if (pci_intr_map(pa, &ih) != 0) {
191		printf(": can't map interrupt\n");
192		return;
193	}
194
195	intrstr = pci_intr_string(sc->sc_pct, ih);
196	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, ipw_intr, sc,
197	    sc->sc_dev.dv_xname);
198	if (sc->sc_ih == NULL) {
199		printf(": can't establish interrupt");
200		if (intrstr != NULL)
201			printf(" at %s", intrstr);
202		printf("\n");
203		return;
204	}
205	printf(": %s", intrstr);
206
207	rw_init(&sc->sc_rwlock, "ipwlock");
208	task_set(&sc->sc_scantask, ipw_scan, sc);
209	task_set(&sc->sc_authandassoctask, ipw_auth_and_assoc, sc);
210
211	if (ipw_reset(sc) != 0) {
212		printf(": could not reset adapter\n");
213		return;
214	}
215
216	if (ipw_dma_alloc(sc) != 0) {
217		printf(": failed to allocate DMA resources\n");
218		return;
219	}
220
221	ic->ic_phytype = IEEE80211_T_DS;
222	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
223	ic->ic_state = IEEE80211_S_INIT;
224
225	/* set device capabilities */
226	ic->ic_caps =
227#ifndef IEEE80211_STA_ONLY
228	    IEEE80211_C_IBSS |		/* IBSS mode supported */
229#endif
230	    IEEE80211_C_MONITOR |	/* monitor mode supported */
231	    IEEE80211_C_TXPMGT |	/* tx power management */
232	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
233	    IEEE80211_C_WEP |		/* s/w WEP */
234	    IEEE80211_C_RSN |		/* WPA/RSN */
235	    IEEE80211_C_SCANALL;	/* h/w scanning */
236
237	/* read MAC address from EEPROM */
238	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
239	ic->ic_myaddr[0] = val >> 8;
240	ic->ic_myaddr[1] = val & 0xff;
241	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
242	ic->ic_myaddr[2] = val >> 8;
243	ic->ic_myaddr[3] = val & 0xff;
244	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
245	ic->ic_myaddr[4] = val >> 8;
246	ic->ic_myaddr[5] = val & 0xff;
247
248	printf(", address %s\n", ether_sprintf(ic->ic_myaddr));
249
250	/* set supported .11b rates */
251	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
252
253	/* set supported .11b channels (1 through 14) */
254	for (i = 1; i <= 14; i++) {
255		ic->ic_channels[i].ic_freq =
256		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
257		ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
258	}
259
260	/* IBSS channel undefined for now */
261	ic->ic_ibss_chan = &ic->ic_channels[0];
262
263	ifp->if_softc = sc;
264	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
265	ifp->if_ioctl = ipw_ioctl;
266	ifp->if_start = ipw_start;
267	ifp->if_watchdog = ipw_watchdog;
268	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
269
270	if_attach(ifp);
271	ieee80211_ifattach(ifp);
272	/* override state transition machine */
273	sc->sc_newstate = ic->ic_newstate;
274	ic->ic_newstate = ipw_newstate;
275	ic->ic_send_mgmt = ipw_send_mgmt;
276	ieee80211_media_init(ifp, ipw_media_change, ipw_media_status);
277
278#if NBPFILTER > 0
279	bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
280	    sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
281
282	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
283	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
284	sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
285
286	sc->sc_txtap_len = sizeof sc->sc_txtapu;
287	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
288	sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
289#endif
290}
291
292int
293ipw_activate(struct device *self, int act)
294{
295	struct ipw_softc *sc = (struct ipw_softc *)self;
296	struct ifnet *ifp = &sc->sc_ic.ic_if;
297
298	switch (act) {
299	case DVACT_SUSPEND:
300		if (ifp->if_flags & IFF_RUNNING)
301			ipw_stop(ifp, 0);
302		break;
303	case DVACT_WAKEUP:
304		ipw_wakeup(sc);
305		break;
306	}
307
308	return 0;
309}
310
311void
312ipw_wakeup(struct ipw_softc *sc)
313{
314	struct ifnet *ifp = &sc->sc_ic.ic_if;
315	pcireg_t data;
316	int s;
317
318	/* clear device specific PCI configuration register 0x41 */
319	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
320	data &= ~0x0000ff00;
321	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
322
323	rw_enter_write(&sc->sc_rwlock);
324	s = splnet();
325
326	if (ifp->if_flags & IFF_UP)
327		ipw_init(ifp);
328
329	splx(s);
330	rw_exit_write(&sc->sc_rwlock);
331}
332
333int
334ipw_dma_alloc(struct ipw_softc *sc)
335{
336	struct ipw_soft_bd *sbd;
337	struct ipw_soft_hdr *shdr;
338	struct ipw_soft_buf *sbuf;
339	int i, nsegs, error;
340
341	/*
342	 * Allocate and map tx ring.
343	 */
344	error = bus_dmamap_create(sc->sc_dmat, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0,
345	    BUS_DMA_NOWAIT, &sc->tbd_map);
346	if (error != 0) {
347		printf("%s: could not create tx ring DMA map\n",
348		    sc->sc_dev.dv_xname);
349		goto fail;
350	}
351
352	error = bus_dmamem_alloc(sc->sc_dmat, IPW_TBD_SZ, PAGE_SIZE, 0,
353	    &sc->tbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
354	if (error != 0) {
355		printf("%s: could not allocate tx ring DMA memory\n",
356		    sc->sc_dev.dv_xname);
357		goto fail;
358	}
359
360	error = bus_dmamem_map(sc->sc_dmat, &sc->tbd_seg, nsegs, IPW_TBD_SZ,
361	    (caddr_t *)&sc->tbd_list, BUS_DMA_NOWAIT);
362	if (error != 0) {
363		printf("%s: can't map tx ring DMA memory\n",
364		    sc->sc_dev.dv_xname);
365		goto fail;
366	}
367
368	error = bus_dmamap_load(sc->sc_dmat, sc->tbd_map, sc->tbd_list,
369	    IPW_TBD_SZ, NULL, BUS_DMA_NOWAIT);
370	if (error != 0) {
371		printf("%s: could not load tx ring DMA map\n",
372		    sc->sc_dev.dv_xname);
373		goto fail;
374	}
375
376	/*
377	 * Allocate and map rx ring.
378	 */
379	error = bus_dmamap_create(sc->sc_dmat, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0,
380	    BUS_DMA_NOWAIT, &sc->rbd_map);
381	if (error != 0) {
382		printf("%s: could not create rx ring DMA map\n",
383		    sc->sc_dev.dv_xname);
384		goto fail;
385	}
386
387	error = bus_dmamem_alloc(sc->sc_dmat, IPW_RBD_SZ, PAGE_SIZE, 0,
388	    &sc->rbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
389	if (error != 0) {
390		printf("%s: could not allocate rx ring DMA memory\n",
391		    sc->sc_dev.dv_xname);
392		goto fail;
393	}
394
395	error = bus_dmamem_map(sc->sc_dmat, &sc->rbd_seg, nsegs, IPW_RBD_SZ,
396	    (caddr_t *)&sc->rbd_list, BUS_DMA_NOWAIT);
397	if (error != 0) {
398		printf("%s: can't map rx ring DMA memory\n",
399		    sc->sc_dev.dv_xname);
400		goto fail;
401	}
402
403	error = bus_dmamap_load(sc->sc_dmat, sc->rbd_map, sc->rbd_list,
404	    IPW_RBD_SZ, NULL, BUS_DMA_NOWAIT);
405	if (error != 0) {
406		printf("%s: could not load tx ring DMA map\n",
407		    sc->sc_dev.dv_xname);
408		goto fail;
409	}
410
411	/*
412	 * Allocate and map status ring.
413	 */
414	error = bus_dmamap_create(sc->sc_dmat, IPW_STATUS_SZ, 1, IPW_STATUS_SZ,
415	    0, BUS_DMA_NOWAIT, &sc->status_map);
416	if (error != 0) {
417		printf("%s: could not create status ring DMA map\n",
418		    sc->sc_dev.dv_xname);
419		goto fail;
420	}
421
422	error = bus_dmamem_alloc(sc->sc_dmat, IPW_STATUS_SZ, PAGE_SIZE, 0,
423	    &sc->status_seg, 1, &nsegs, BUS_DMA_NOWAIT);
424	if (error != 0) {
425		printf("%s: could not allocate status ring DMA memory\n",
426		    sc->sc_dev.dv_xname);
427		goto fail;
428	}
429
430	error = bus_dmamem_map(sc->sc_dmat, &sc->status_seg, nsegs,
431	    IPW_STATUS_SZ, (caddr_t *)&sc->status_list, BUS_DMA_NOWAIT);
432	if (error != 0) {
433		printf("%s: can't map status ring DMA memory\n",
434		    sc->sc_dev.dv_xname);
435		goto fail;
436	}
437
438	error = bus_dmamap_load(sc->sc_dmat, sc->status_map, sc->status_list,
439	    IPW_STATUS_SZ, NULL, BUS_DMA_NOWAIT);
440	if (error != 0) {
441		printf("%s: could not load status ring DMA map\n",
442		    sc->sc_dev.dv_xname);
443		goto fail;
444	}
445
446	/*
447	 * Allocate command DMA map.
448	 */
449	error = bus_dmamap_create(sc->sc_dmat, sizeof (struct ipw_cmd), 1,
450	    sizeof (struct ipw_cmd), 0, BUS_DMA_NOWAIT, &sc->cmd_map);
451	if (error != 0) {
452		printf("%s: could not create command DMA map\n",
453		    sc->sc_dev.dv_xname);
454		goto fail;
455	}
456
457	/*
458	 * Allocate headers DMA maps.
459	 */
460	SLIST_INIT(&sc->free_shdr);
461	for (i = 0; i < IPW_NDATA; i++) {
462		shdr = &sc->shdr_list[i];
463		error = bus_dmamap_create(sc->sc_dmat, sizeof (struct ipw_hdr),
464		    1, sizeof (struct ipw_hdr), 0, BUS_DMA_NOWAIT, &shdr->map);
465		if (error != 0) {
466			printf("%s: could not create header DMA map\n",
467			    sc->sc_dev.dv_xname);
468			goto fail;
469		}
470		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
471	}
472
473	/*
474	 * Allocate tx buffers DMA maps.
475	 */
476	SLIST_INIT(&sc->free_sbuf);
477	for (i = 0; i < IPW_NDATA; i++) {
478		sbuf = &sc->tx_sbuf_list[i];
479		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, IPW_MAX_NSEG,
480		    MCLBYTES, 0, BUS_DMA_NOWAIT, &sbuf->map);
481		if (error != 0) {
482			printf("%s: could not create tx DMA map\n",
483			    sc->sc_dev.dv_xname);
484			goto fail;
485		}
486		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
487	}
488
489	/*
490	 * Initialize tx ring.
491	 */
492	for (i = 0; i < IPW_NTBD; i++) {
493		sbd = &sc->stbd_list[i];
494		sbd->bd = &sc->tbd_list[i];
495		sbd->type = IPW_SBD_TYPE_NOASSOC;
496	}
497
498	/*
499	 * Pre-allocate rx buffers and DMA maps.
500	 */
501	for (i = 0; i < IPW_NRBD; i++) {
502		sbd = &sc->srbd_list[i];
503		sbuf = &sc->rx_sbuf_list[i];
504		sbd->bd = &sc->rbd_list[i];
505
506		MGETHDR(sbuf->m, M_DONTWAIT, MT_DATA);
507		if (sbuf->m == NULL) {
508			printf("%s: could not allocate rx mbuf\n",
509			    sc->sc_dev.dv_xname);
510			error = ENOMEM;
511			goto fail;
512		}
513		MCLGET(sbuf->m, M_DONTWAIT);
514		if (!(sbuf->m->m_flags & M_EXT)) {
515			m_freem(sbuf->m);
516			printf("%s: could not allocate rx mbuf cluster\n",
517			    sc->sc_dev.dv_xname);
518			error = ENOMEM;
519			goto fail;
520		}
521
522		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
523		    0, BUS_DMA_NOWAIT, &sbuf->map);
524		if (error != 0) {
525			printf("%s: could not create rx DMA map\n",
526			    sc->sc_dev.dv_xname);
527			goto fail;
528		}
529
530		error = bus_dmamap_load(sc->sc_dmat, sbuf->map,
531		    mtod(sbuf->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
532		if (error != 0) {
533			printf("%s: can't map rx DMA memory\n",
534			    sc->sc_dev.dv_xname);
535			goto fail;
536		}
537
538		sbd->type = IPW_SBD_TYPE_DATA;
539		sbd->priv = sbuf;
540		sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
541		sbd->bd->len = htole32(MCLBYTES);
542	}
543
544	bus_dmamap_sync(sc->sc_dmat, sc->rbd_map, 0, IPW_RBD_SZ,
545	    BUS_DMASYNC_PREWRITE);
546
547	return 0;
548
549fail:	ipw_release(sc);
550	return error;
551}
552
553void
554ipw_release(struct ipw_softc *sc)
555{
556	struct ipw_soft_buf *sbuf;
557	int i;
558
559	if (sc->tbd_map != NULL) {
560		if (sc->tbd_list != NULL) {
561			bus_dmamap_unload(sc->sc_dmat, sc->tbd_map);
562			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->tbd_list,
563			    IPW_TBD_SZ);
564			bus_dmamem_free(sc->sc_dmat, &sc->tbd_seg, 1);
565		}
566		bus_dmamap_destroy(sc->sc_dmat, sc->tbd_map);
567	}
568
569	if (sc->rbd_map != NULL) {
570		if (sc->rbd_list != NULL) {
571			bus_dmamap_unload(sc->sc_dmat, sc->rbd_map);
572			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rbd_list,
573			    IPW_RBD_SZ);
574			bus_dmamem_free(sc->sc_dmat, &sc->rbd_seg, 1);
575		}
576		bus_dmamap_destroy(sc->sc_dmat, sc->rbd_map);
577	}
578
579	if (sc->status_map != NULL) {
580		if (sc->status_list != NULL) {
581			bus_dmamap_unload(sc->sc_dmat, sc->status_map);
582			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->status_list,
583			    IPW_RBD_SZ);
584			bus_dmamem_free(sc->sc_dmat, &sc->status_seg, 1);
585		}
586		bus_dmamap_destroy(sc->sc_dmat, sc->status_map);
587	}
588
589	if (sc->cmd_map != NULL)
590		bus_dmamap_destroy(sc->sc_dmat, sc->cmd_map);
591
592	for (i = 0; i < IPW_NDATA; i++)
593		bus_dmamap_destroy(sc->sc_dmat, sc->shdr_list[i].map);
594
595	for (i = 0; i < IPW_NDATA; i++)
596		bus_dmamap_destroy(sc->sc_dmat, sc->tx_sbuf_list[i].map);
597
598	for (i = 0; i < IPW_NRBD; i++) {
599		sbuf = &sc->rx_sbuf_list[i];
600		if (sbuf->map != NULL) {
601			if (sbuf->m != NULL) {
602				bus_dmamap_unload(sc->sc_dmat, sbuf->map);
603				m_freem(sbuf->m);
604			}
605			bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
606		}
607	}
608
609	task_del(systq, &sc->sc_scantask);
610	task_del(systq, &sc->sc_authandassoctask);
611}
612
613int
614ipw_media_change(struct ifnet *ifp)
615{
616	int error;
617
618	error = ieee80211_media_change(ifp);
619	if (error != ENETRESET)
620		return error;
621
622	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
623		ipw_init(ifp);
624
625	return 0;
626}
627
628void
629ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
630{
631	struct ipw_softc *sc = ifp->if_softc;
632	struct ieee80211com *ic = &sc->sc_ic;
633	static const struct {
634		uint32_t	val;
635		int		rate;
636	} rates[] = {
637		{ IPW_RATE_DS1,   2 },
638		{ IPW_RATE_DS2,   4 },
639		{ IPW_RATE_DS5,  11 },
640		{ IPW_RATE_DS11, 22 },
641	};
642	uint32_t val;
643	int rate, i;
644
645	imr->ifm_status = IFM_AVALID;
646	imr->ifm_active = IFM_IEEE80211;
647	if (ic->ic_state == IEEE80211_S_RUN)
648		imr->ifm_status |= IFM_ACTIVE;
649
650	/* read current transmission rate from adapter */
651	val = ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE);
652	val &= 0xf;
653
654	/* convert rate to 802.11 rate */
655	for (i = 0; i < nitems(rates) && rates[i].val != val; i++);
656	rate = (i < nitems(rates)) ? rates[i].rate : 0;
657
658	imr->ifm_active |= IFM_IEEE80211_11B;
659	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
660	switch (ic->ic_opmode) {
661	case IEEE80211_M_STA:
662		break;
663#ifndef IEEE80211_STA_ONLY
664	case IEEE80211_M_IBSS:
665		imr->ifm_active |= IFM_IEEE80211_IBSS;
666		break;
667#endif
668	case IEEE80211_M_MONITOR:
669		imr->ifm_active |= IFM_IEEE80211_MONITOR;
670		break;
671	default:
672		/* should not get there */
673		break;
674	}
675}
676
677int
678ipw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
679{
680	struct ipw_softc *sc = ic->ic_softc;
681
682	switch (nstate) {
683	case IEEE80211_S_SCAN:
684		task_add(systq, &sc->sc_scantask);
685		break;
686
687	case IEEE80211_S_AUTH:
688		task_add(systq, &sc->sc_authandassoctask);
689		break;
690
691	case IEEE80211_S_RUN:
692	case IEEE80211_S_INIT:
693	case IEEE80211_S_ASSOC:
694		/* nothing to do */
695		break;
696	}
697
698	ic->ic_state = nstate;
699	return 0;
700}
701
702/*
703 * Read 16 bits at address 'addr' from the Microwire EEPROM.
704 * DON'T PLAY WITH THIS CODE UNLESS YOU KNOW *EXACTLY* WHAT YOU'RE DOING!
705 */
706uint16_t
707ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
708{
709	uint32_t tmp;
710	uint16_t val;
711	int n;
712
713	/* clock C once before the first command */
714	IPW_EEPROM_CTL(sc, 0);
715	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
716	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
717	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
718
719	/* write start bit (1) */
720	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
721	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
722
723	/* write READ opcode (10) */
724	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
725	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
726	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
727	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
728
729	/* write address A7-A0 */
730	for (n = 7; n >= 0; n--) {
731		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
732		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
733		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
734		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
735	}
736
737	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
738
739	/* read data Q15-Q0 */
740	val = 0;
741	for (n = 15; n >= 0; n--) {
742		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
743		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
744		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
745		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
746	}
747
748	IPW_EEPROM_CTL(sc, 0);
749
750	/* clear Chip Select and clock C */
751	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
752	IPW_EEPROM_CTL(sc, 0);
753	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
754
755	return val;
756}
757
758void
759ipw_command_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
760{
761	struct ipw_cmd *cmd;
762
763	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof (struct ipw_cmd),
764	    BUS_DMASYNC_POSTREAD);
765
766	cmd = mtod(sbuf->m, struct ipw_cmd *);
767
768	DPRINTFN(2, ("received command ack type=%u,status=%u\n",
769	    letoh32(cmd->type), letoh32(cmd->status)));
770
771	wakeup(&sc->cmd);
772}
773
774void
775ipw_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
776{
777	struct ieee80211com *ic = &sc->sc_ic;
778	struct ifnet *ifp = &ic->ic_if;
779	uint32_t state;
780
781	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof state,
782	    BUS_DMASYNC_POSTREAD);
783
784	state = letoh32(*mtod(sbuf->m, uint32_t *));
785
786	DPRINTFN(2, ("firmware state changed to 0x%x\n", state));
787
788	switch (state) {
789	case IPW_STATE_ASSOCIATED:
790		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
791		break;
792
793	case IPW_STATE_SCANNING:
794		if (ic->ic_state == IEEE80211_S_RUN)
795			ieee80211_begin_scan(ifp);
796		break;
797
798	case IPW_STATE_SCAN_COMPLETE:
799		if (ic->ic_state == IEEE80211_S_SCAN)
800			ieee80211_end_scan(ifp);
801		break;
802
803	case IPW_STATE_ASSOCIATION_LOST:
804		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
805		break;
806
807	case IPW_STATE_DISABLED:
808		wakeup(sc);
809		break;
810
811	case IPW_STATE_RADIO_DISABLED:
812		ifp->if_flags &= ~IFF_UP;
813		ipw_stop(&ic->ic_if, 1);
814		break;
815	}
816}
817
818void
819ipw_data_intr(struct ipw_softc *sc, struct ipw_status *status,
820    struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
821{
822	struct ieee80211com *ic = &sc->sc_ic;
823	struct ifnet *ifp = &ic->ic_if;
824	struct mbuf *mnew, *m;
825	struct ieee80211_frame *wh;
826	struct ieee80211_rxinfo rxi;
827	struct ieee80211_node *ni;
828	int error;
829
830	DPRINTFN(5, ("received data frame len=%u,rssi=%u\n",
831	    letoh32(status->len), status->rssi));
832
833	/*
834	 * Try to allocate a new mbuf for this ring element and load it before
835	 * processing the current mbuf.  If the ring element cannot be loaded,
836	 * drop the received packet and reuse the old mbuf.  In the unlikely
837	 * case that the old mbuf can't be reloaded either, explicitly panic.
838	 */
839	MGETHDR(mnew, M_DONTWAIT, MT_DATA);
840	if (mnew == NULL) {
841		ifp->if_ierrors++;
842		return;
843	}
844	MCLGET(mnew, M_DONTWAIT);
845	if (!(mnew->m_flags & M_EXT)) {
846		m_freem(mnew);
847		ifp->if_ierrors++;
848		return;
849	}
850
851	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, letoh32(status->len),
852	    BUS_DMASYNC_POSTREAD);
853	bus_dmamap_unload(sc->sc_dmat, sbuf->map);
854
855	error = bus_dmamap_load(sc->sc_dmat, sbuf->map, mtod(mnew, void *),
856	    MCLBYTES, NULL, BUS_DMA_NOWAIT);
857	if (error != 0) {
858		m_freem(mnew);
859
860		/* try to reload the old mbuf */
861		error = bus_dmamap_load(sc->sc_dmat, sbuf->map,
862		    mtod(sbuf->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
863		if (error != 0) {
864			/* very unlikely that it will fail... */
865			panic("%s: could not load old rx mbuf",
866			    sc->sc_dev.dv_xname);
867		}
868		sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
869		ifp->if_ierrors++;
870		return;
871	}
872
873	m = sbuf->m;
874	sbuf->m = mnew;
875	sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
876
877	/* finalize mbuf */
878	m->m_pkthdr.len = m->m_len = letoh32(status->len);
879
880#if NBPFILTER > 0
881	if (sc->sc_drvbpf != NULL) {
882		struct mbuf mb;
883		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
884
885		tap->wr_flags = 0;
886		tap->wr_antsignal = status->rssi;
887		tap->wr_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
888		tap->wr_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
889
890		mb.m_data = (caddr_t)tap;
891		mb.m_len = sc->sc_rxtap_len;
892		mb.m_next = m;
893		mb.m_nextpkt = NULL;
894		mb.m_type = 0;
895		mb.m_flags = 0;
896		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
897	}
898#endif
899
900	wh = mtod(m, struct ieee80211_frame *);
901	ni = ieee80211_find_rxnode(ic, wh);
902
903	/* send the frame to the upper layer */
904	rxi.rxi_flags = 0;
905	rxi.rxi_rssi = status->rssi;
906	rxi.rxi_tstamp = 0;	/* unused */
907	ieee80211_input(ifp, m, ni, &rxi);
908
909	ieee80211_release_node(ic, ni);
910}
911
912void
913ipw_notification_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
914{
915	DPRINTFN(2, ("received notification\n"));
916}
917
918void
919ipw_rx_intr(struct ipw_softc *sc)
920{
921	struct ipw_status *status;
922	struct ipw_soft_bd *sbd;
923	struct ipw_soft_buf *sbuf;
924	uint32_t r, i;
925
926	r = CSR_READ_4(sc, IPW_CSR_RX_READ_INDEX);
927
928	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
929
930		bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
931		    i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
932		    BUS_DMASYNC_POSTREAD);
933
934		bus_dmamap_sync(sc->sc_dmat, sc->status_map,
935		    i * sizeof (struct ipw_status), sizeof (struct ipw_status),
936		    BUS_DMASYNC_POSTREAD);
937
938		status = &sc->status_list[i];
939		sbd = &sc->srbd_list[i];
940		sbuf = sbd->priv;
941
942		switch (letoh16(status->code) & 0xf) {
943		case IPW_STATUS_CODE_COMMAND:
944			ipw_command_intr(sc, sbuf);
945			break;
946
947		case IPW_STATUS_CODE_NEWSTATE:
948			ipw_newstate_intr(sc, sbuf);
949			break;
950
951		case IPW_STATUS_CODE_DATA_802_3:
952		case IPW_STATUS_CODE_DATA_802_11:
953			ipw_data_intr(sc, status, sbd, sbuf);
954			break;
955
956		case IPW_STATUS_CODE_NOTIFICATION:
957			ipw_notification_intr(sc, sbuf);
958			break;
959
960		default:
961			printf("%s: unknown status code %u\n",
962			    sc->sc_dev.dv_xname, letoh16(status->code));
963		}
964		sbd->bd->flags = 0;
965
966		bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
967		    i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
968		    BUS_DMASYNC_PREWRITE);
969	}
970
971	/* tell the firmware what we have processed */
972	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
973	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE_INDEX, sc->rxcur);
974}
975
976void
977ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
978{
979	struct ieee80211com *ic = &sc->sc_ic;
980	struct ipw_soft_hdr *shdr;
981	struct ipw_soft_buf *sbuf;
982
983	switch (sbd->type) {
984	case IPW_SBD_TYPE_COMMAND:
985		bus_dmamap_unload(sc->sc_dmat, sc->cmd_map);
986		break;
987
988	case IPW_SBD_TYPE_HEADER:
989		shdr = sbd->priv;
990		bus_dmamap_unload(sc->sc_dmat, shdr->map);
991		SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
992		break;
993
994	case IPW_SBD_TYPE_DATA:
995		sbuf = sbd->priv;
996		bus_dmamap_unload(sc->sc_dmat, sbuf->map);
997		SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
998
999		m_freem(sbuf->m);
1000
1001		if (sbuf->ni != NULL)
1002			ieee80211_release_node(ic, sbuf->ni);
1003
1004		/* kill watchdog timer */
1005		sc->sc_tx_timer = 0;
1006		break;
1007	}
1008	sbd->type = IPW_SBD_TYPE_NOASSOC;
1009}
1010
1011void
1012ipw_tx_intr(struct ipw_softc *sc)
1013{
1014	struct ifnet *ifp = &sc->sc_ic.ic_if;
1015	struct ipw_soft_bd *sbd;
1016	uint32_t r, i;
1017
1018	r = CSR_READ_4(sc, IPW_CSR_TX_READ_INDEX);
1019
1020	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1021		sbd = &sc->stbd_list[i];
1022
1023		ipw_release_sbd(sc, sbd);
1024		sc->txfree++;
1025	}
1026
1027	/* remember what the firmware has processed */
1028	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1029
1030	/* call start() since some buffer descriptors have been released */
1031	ifq_clr_oactive(&ifp->if_snd);
1032	(*ifp->if_start)(ifp);
1033}
1034
1035int
1036ipw_intr(void *arg)
1037{
1038	struct ipw_softc *sc = arg;
1039	struct ifnet *ifp = &sc->sc_ic.ic_if;
1040	uint32_t r;
1041
1042	if ((r = CSR_READ_4(sc, IPW_CSR_INTR)) == 0 || r == 0xffffffff)
1043		return 0;
1044
1045	/* disable interrupts */
1046	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1047
1048	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1049		printf("%s: fatal firmware error\n", sc->sc_dev.dv_xname);
1050		ifp->if_flags &= ~IFF_UP;
1051		ipw_stop(ifp, 1);
1052		return 1;
1053	}
1054
1055	if (r & IPW_INTR_FW_INIT_DONE)
1056		wakeup(sc);
1057
1058	if (r & IPW_INTR_RX_TRANSFER)
1059		ipw_rx_intr(sc);
1060
1061	if (r & IPW_INTR_TX_TRANSFER)
1062		ipw_tx_intr(sc);
1063
1064	/* acknowledge interrupts */
1065	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1066
1067	/* re-enable interrupts */
1068	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1069
1070	return 1;
1071}
1072
1073int
1074ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1075{
1076	struct ipw_soft_bd *sbd;
1077	int s, error;
1078
1079	s = splnet();
1080
1081	sc->cmd.type = htole32(type);
1082	sc->cmd.subtype = htole32(0);
1083	sc->cmd.len = htole32(len);
1084	sc->cmd.seq = htole32(0);
1085	if (data != NULL)
1086		bcopy(data, sc->cmd.data, len);
1087
1088	error = bus_dmamap_load(sc->sc_dmat, sc->cmd_map, &sc->cmd,
1089	    sizeof (struct ipw_cmd), NULL, BUS_DMA_NOWAIT);
1090	if (error != 0) {
1091		printf("%s: can't map command DMA memory\n",
1092		    sc->sc_dev.dv_xname);
1093		splx(s);
1094		return error;
1095	}
1096
1097	sbd = &sc->stbd_list[sc->txcur];
1098	sbd->type = IPW_SBD_TYPE_COMMAND;
1099	sbd->bd->physaddr = htole32(sc->cmd_map->dm_segs[0].ds_addr);
1100	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1101	sbd->bd->nfrag = 1;
1102	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1103	    IPW_BD_FLAG_TX_LAST_FRAGMENT;
1104
1105	bus_dmamap_sync(sc->sc_dmat, sc->cmd_map, 0, sizeof (struct ipw_cmd),
1106	    BUS_DMASYNC_PREWRITE);
1107	bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
1108	    sc->txcur * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
1109	    BUS_DMASYNC_PREWRITE);
1110
1111	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1112	sc->txfree--;
1113	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
1114
1115	DPRINTFN(2, ("sending command type=%u,len=%u\n", type, len));
1116
1117	/* wait at most one second for command to complete */
1118	error = tsleep(&sc->cmd, 0, "ipwcmd", hz);
1119	splx(s);
1120
1121	return error;
1122}
1123
1124/* ARGSUSED */
1125int
1126ipw_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni, int type,
1127    int arg1, int arg2)
1128{
1129	return EOPNOTSUPP;
1130}
1131
1132int
1133ipw_tx_start(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node *ni)
1134{
1135	struct ipw_softc *sc = ifp->if_softc;
1136	struct ieee80211com *ic = &sc->sc_ic;
1137	struct ieee80211_frame *wh;
1138	struct ieee80211_key *k;
1139	struct ipw_soft_bd *sbd;
1140	struct ipw_soft_hdr *shdr;
1141	struct ipw_soft_buf *sbuf;
1142	int error, i;
1143
1144	wh = mtod(m, struct ieee80211_frame *);
1145
1146	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1147		k = ieee80211_get_txkey(ic, wh, ni);
1148
1149		if ((m = ieee80211_encrypt(ic, m, k)) == NULL)
1150			return ENOBUFS;
1151
1152		/* packet header may have moved, reset our local pointer */
1153		wh = mtod(m, struct ieee80211_frame *);
1154	}
1155
1156#if NBPFILTER > 0
1157	if (sc->sc_drvbpf != NULL) {
1158		struct mbuf mb;
1159		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1160
1161		tap->wt_flags = 0;
1162		tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1163		tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1164
1165		mb.m_data = (caddr_t)tap;
1166		mb.m_len = sc->sc_txtap_len;
1167		mb.m_next = m;
1168		mb.m_nextpkt = NULL;
1169		mb.m_type = 0;
1170		mb.m_flags = 0;
1171		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
1172	}
1173#endif
1174
1175	shdr = SLIST_FIRST(&sc->free_shdr);
1176	sbuf = SLIST_FIRST(&sc->free_sbuf);
1177
1178	shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1179	shdr->hdr.subtype = htole32(0);
1180	shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) ? 1 : 0;
1181	shdr->hdr.encrypt = 0;
1182	shdr->hdr.keyidx = 0;
1183	shdr->hdr.keysz = 0;
1184	shdr->hdr.fragmentsz = htole16(0);
1185	IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1186	if (ic->ic_opmode == IEEE80211_M_STA)
1187		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1188	else
1189		IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1190
1191	/* trim IEEE802.11 header */
1192	m_adj(m, sizeof (struct ieee80211_frame));
1193
1194	error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, m, BUS_DMA_NOWAIT);
1195	if (error != 0 && error != EFBIG) {
1196		printf("%s: can't map mbuf (error %d)\n",
1197		    sc->sc_dev.dv_xname, error);
1198		m_freem(m);
1199		return error;
1200	}
1201	if (error != 0) {
1202		/* too many fragments, linearize */
1203		if (m_defrag(m, M_DONTWAIT)) {
1204			m_freem(m);
1205			return ENOBUFS;
1206		}
1207		error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, m,
1208		    BUS_DMA_NOWAIT);
1209		if (error != 0) {
1210			printf("%s: can't map mbuf (error %d)\n",
1211			    sc->sc_dev.dv_xname, error);
1212			m_freem(m);
1213			return error;
1214		}
1215	}
1216
1217	error = bus_dmamap_load(sc->sc_dmat, shdr->map, &shdr->hdr,
1218	    sizeof (struct ipw_hdr), NULL, BUS_DMA_NOWAIT);
1219	if (error != 0) {
1220		printf("%s: can't map header DMA memory (error %d)\n",
1221		    sc->sc_dev.dv_xname, error);
1222		bus_dmamap_unload(sc->sc_dmat, sbuf->map);
1223		m_freem(m);
1224		return error;
1225	}
1226
1227	SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1228	SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1229
1230	sbd = &sc->stbd_list[sc->txcur];
1231	sbd->type = IPW_SBD_TYPE_HEADER;
1232	sbd->priv = shdr;
1233	sbd->bd->physaddr = htole32(shdr->map->dm_segs[0].ds_addr);
1234	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1235	sbd->bd->nfrag = 1 + sbuf->map->dm_nsegs;
1236	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1237	    IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1238
1239	bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
1240	    sc->txcur * sizeof (struct ipw_bd),
1241	    sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
1242
1243	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1244	sc->txfree--;
1245
1246	sbuf->m = m;
1247	sbuf->ni = ni;
1248
1249	for (i = 0; i < sbuf->map->dm_nsegs; i++) {
1250		sbd = &sc->stbd_list[sc->txcur];
1251		sbd->bd->physaddr = htole32(sbuf->map->dm_segs[i].ds_addr);
1252		sbd->bd->len = htole32(sbuf->map->dm_segs[i].ds_len);
1253		sbd->bd->nfrag = 0;	/* used only in first bd */
1254		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1255		if (i == sbuf->map->dm_nsegs - 1) {
1256			sbd->type = IPW_SBD_TYPE_DATA;
1257			sbd->priv = sbuf;
1258			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1259		} else {
1260			sbd->type = IPW_SBD_TYPE_NOASSOC;
1261			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1262		}
1263
1264		bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
1265		    sc->txcur * sizeof (struct ipw_bd),
1266		    sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
1267
1268		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1269		sc->txfree--;
1270	}
1271
1272	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sbuf->map->dm_mapsize,
1273	    BUS_DMASYNC_PREWRITE);
1274	bus_dmamap_sync(sc->sc_dmat, shdr->map, 0, sizeof (struct ipw_hdr),
1275	    BUS_DMASYNC_PREWRITE);
1276
1277	/* inform firmware about this new packet */
1278	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, sc->txcur);
1279
1280	return 0;
1281}
1282
1283void
1284ipw_start(struct ifnet *ifp)
1285{
1286	struct ipw_softc *sc = ifp->if_softc;
1287	struct ieee80211com *ic = &sc->sc_ic;
1288	struct ieee80211_node *ni;
1289	struct mbuf *m;
1290
1291	if (ic->ic_state != IEEE80211_S_RUN)
1292		return;
1293
1294	for (;;) {
1295		if (sc->txfree < 1 + IPW_MAX_NSEG) {
1296			ifq_set_oactive(&ifp->if_snd);
1297			break;
1298		}
1299
1300		IFQ_DEQUEUE(&ifp->if_snd, m);
1301		if (m == NULL)
1302			break;
1303
1304#if NBPFILTER > 0
1305		if (ifp->if_bpf != NULL)
1306			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
1307#endif
1308
1309		m = ieee80211_encap(ifp, m, &ni);
1310		if (m == NULL)
1311			continue;
1312#if NBPFILTER > 0
1313		if (ic->ic_rawbpf != NULL)
1314			bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT);
1315#endif
1316		if (ipw_tx_start(ifp, m, ni) != 0) {
1317			if (ni != NULL)
1318				ieee80211_release_node(ic, ni);
1319			ifp->if_oerrors++;
1320			break;
1321		}
1322
1323		/* start watchdog timer */
1324		sc->sc_tx_timer = 5;
1325		ifp->if_timer = 1;
1326	}
1327}
1328
1329void
1330ipw_watchdog(struct ifnet *ifp)
1331{
1332	struct ipw_softc *sc = ifp->if_softc;
1333
1334	ifp->if_timer = 0;
1335
1336	if (sc->sc_tx_timer > 0) {
1337		if (--sc->sc_tx_timer == 0) {
1338			printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1339			ifp->if_flags &= ~IFF_UP;
1340			ipw_stop(ifp, 1);
1341			ifp->if_oerrors++;
1342			return;
1343		}
1344		ifp->if_timer = 1;
1345	}
1346
1347	ieee80211_watchdog(ifp);
1348}
1349
1350int
1351ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1352{
1353	struct ipw_softc *sc = ifp->if_softc;
1354	struct ieee80211com *ic = &sc->sc_ic;
1355	struct ifreq *ifr;
1356	int s, error = 0;
1357
1358	error = rw_enter(&sc->sc_rwlock, RW_WRITE | RW_INTR);
1359	if (error)
1360		return error;
1361	s = splnet();
1362
1363	switch (cmd) {
1364	case SIOCSIFADDR:
1365		ifp->if_flags |= IFF_UP;
1366		/* FALLTHROUGH */
1367	case SIOCSIFFLAGS:
1368		if (ifp->if_flags & IFF_UP) {
1369			if (!(ifp->if_flags & IFF_RUNNING))
1370				ipw_init(ifp);
1371		} else {
1372			if (ifp->if_flags & IFF_RUNNING)
1373				ipw_stop(ifp, 1);
1374		}
1375		break;
1376
1377	case SIOCADDMULTI:
1378	case SIOCDELMULTI:
1379		ifr = (struct ifreq *)data;
1380		error = (cmd == SIOCADDMULTI) ?
1381		    ether_addmulti(ifr, &ic->ic_ac) :
1382		    ether_delmulti(ifr, &ic->ic_ac);
1383
1384		if (error == ENETRESET)
1385			error = 0;
1386		break;
1387
1388	case SIOCG80211TXPOWER:
1389		/*
1390		 * If the hardware radio transmitter switch is off, report a
1391		 * tx power of IEEE80211_TXPOWER_MIN to indicate that radio
1392		 * transmitter is killed.
1393		 */
1394		((struct ieee80211_txpower *)data)->i_val =
1395		    (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED) ?
1396		    IEEE80211_TXPOWER_MIN : sc->sc_ic.ic_txpower;
1397		break;
1398
1399	default:
1400		error = ieee80211_ioctl(ifp, cmd, data);
1401	}
1402
1403	if (error == ENETRESET) {
1404		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1405		    (IFF_UP | IFF_RUNNING))
1406			ipw_init(ifp);
1407		error = 0;
1408	}
1409
1410	splx(s);
1411	rw_exit_write(&sc->sc_rwlock);
1412	return error;
1413}
1414
1415uint32_t
1416ipw_read_table1(struct ipw_softc *sc, uint32_t off)
1417{
1418	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
1419}
1420
1421void
1422ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
1423{
1424	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
1425}
1426
1427int
1428ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
1429{
1430	uint32_t addr, info;
1431	uint16_t count, size;
1432	uint32_t total;
1433
1434	/* addr[4] + count[2] + size[2] */
1435	addr = MEM_READ_4(sc, sc->table2_base + off);
1436	info = MEM_READ_4(sc, sc->table2_base + off + 4);
1437
1438	count = info >> 16;
1439	size  = info & 0xffff;
1440	total = count * size;
1441
1442	if (total > *len) {
1443		*len = total;
1444		return EINVAL;
1445	}
1446	*len = total;
1447	ipw_read_mem_1(sc, addr, buf, total);
1448
1449	return 0;
1450}
1451
1452void
1453ipw_stop_master(struct ipw_softc *sc)
1454{
1455	uint32_t tmp;
1456	int ntries;
1457
1458	/* disable interrupts */
1459	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1460
1461	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1462	for (ntries = 0; ntries < 50; ntries++) {
1463		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1464			break;
1465		DELAY(10);
1466	}
1467	if (ntries == 50)
1468		printf("%s: timeout waiting for master\n",
1469		    sc->sc_dev.dv_xname);
1470
1471	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1472	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1473}
1474
1475int
1476ipw_reset(struct ipw_softc *sc)
1477{
1478	uint32_t tmp;
1479	int ntries;
1480
1481	ipw_stop_master(sc);
1482
1483	/* move adapter to D0 state */
1484	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1485	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1486
1487	/* wait for clock stabilization */
1488	for (ntries = 0; ntries < 1000; ntries++) {
1489		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1490			break;
1491		DELAY(200);
1492	}
1493	if (ntries == 1000)
1494		return EIO;
1495
1496	tmp = CSR_READ_4(sc, IPW_CSR_RST);
1497	CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1498
1499	DELAY(10);
1500
1501	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1502	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1503
1504	return 0;
1505}
1506
1507int
1508ipw_load_ucode(struct ipw_softc *sc, u_char *uc, int size)
1509{
1510	int ntries;
1511
1512	/* voodoo from the Intel Linux driver */
1513	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1514	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1515
1516	MEM_WRITE_2(sc, 0x220000, 0x0703);
1517	MEM_WRITE_2(sc, 0x220000, 0x0707);
1518
1519	MEM_WRITE_1(sc, 0x210014, 0x72);
1520	MEM_WRITE_1(sc, 0x210014, 0x72);
1521
1522	MEM_WRITE_1(sc, 0x210000, 0x40);
1523	MEM_WRITE_1(sc, 0x210000, 0x00);
1524	MEM_WRITE_1(sc, 0x210000, 0x40);
1525
1526	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1527
1528	MEM_WRITE_1(sc, 0x210000, 0x00);
1529	MEM_WRITE_1(sc, 0x210000, 0x00);
1530	MEM_WRITE_1(sc, 0x210000, 0x80);
1531
1532	MEM_WRITE_2(sc, 0x220000, 0x0703);
1533	MEM_WRITE_2(sc, 0x220000, 0x0707);
1534
1535	MEM_WRITE_1(sc, 0x210014, 0x72);
1536	MEM_WRITE_1(sc, 0x210014, 0x72);
1537
1538	MEM_WRITE_1(sc, 0x210000, 0x00);
1539	MEM_WRITE_1(sc, 0x210000, 0x80);
1540
1541	for (ntries = 0; ntries < 100; ntries++) {
1542		if (MEM_READ_1(sc, 0x210000) & 1)
1543			break;
1544		DELAY(1000);
1545	}
1546	if (ntries == 100) {
1547		printf("%s: timeout waiting for ucode to initialize\n",
1548		    sc->sc_dev.dv_xname);
1549		return EIO;
1550	}
1551
1552	MEM_WRITE_4(sc, 0x3000e0, 0);
1553
1554	return 0;
1555}
1556
1557/* set of macros to handle unaligned little endian data in firmware image */
1558#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1559#define GETLE16(p) ((p)[0] | (p)[1] << 8)
1560int
1561ipw_load_firmware(struct ipw_softc *sc, u_char *fw, int size)
1562{
1563	u_char *p, *end;
1564	uint32_t tmp, dst;
1565	uint16_t len;
1566	int error;
1567
1568	p = fw;
1569	end = fw + size;
1570	while (p < end) {
1571		if (p + 6 > end)
1572			return EINVAL;
1573
1574		dst = GETLE32(p); p += 4;
1575		len = GETLE16(p); p += 2;
1576
1577		if (p + len > end)
1578			return EINVAL;
1579
1580		ipw_write_mem_1(sc, dst, p, len);
1581		p += len;
1582	}
1583
1584	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
1585	    IPW_IO_LED_OFF);
1586
1587	/* allow interrupts so we know when the firmware is inited */
1588	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1589
1590	/* tell the adapter to initialize the firmware */
1591	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1592	tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1593	CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
1594
1595	/* wait at most one second for firmware initialization to complete */
1596	if ((error = tsleep(sc, 0, "ipwinit", hz)) != 0) {
1597		printf("%s: timeout waiting for firmware initialization to "
1598		    "complete\n", sc->sc_dev.dv_xname);
1599		return error;
1600	}
1601
1602	tmp = CSR_READ_4(sc, IPW_CSR_IO);
1603	CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
1604	    IPW_IO_GPIO3_MASK);
1605
1606	return 0;
1607}
1608
1609int
1610ipw_read_firmware(struct ipw_softc *sc, struct ipw_firmware *fw)
1611{
1612	const struct ipw_firmware_hdr *hdr;
1613	const char *name;
1614	int error;
1615
1616	switch (sc->sc_ic.ic_opmode) {
1617	case IEEE80211_M_STA:
1618		name = "ipw-bss";
1619		break;
1620#ifndef IEEE80211_STA_ONLY
1621	case IEEE80211_M_IBSS:
1622		name = "ipw-ibss";
1623		break;
1624#endif
1625	case IEEE80211_M_MONITOR:
1626		name = "ipw-monitor";
1627		break;
1628	default:
1629		/* should not get there */
1630		return ENODEV;
1631	}
1632	if ((error = loadfirmware(name, &fw->data, &fw->size)) != 0)
1633		return error;
1634
1635	if (fw->size < sizeof (*hdr)) {
1636		error = EINVAL;
1637		goto fail;
1638	}
1639	hdr = (const struct ipw_firmware_hdr *)fw->data;
1640	fw->main_size  = letoh32(hdr->main_size);
1641	fw->ucode_size = letoh32(hdr->ucode_size);
1642
1643	if (fw->size < sizeof (*hdr) + fw->main_size + fw->ucode_size) {
1644		error = EINVAL;
1645		goto fail;
1646	}
1647	fw->main  = fw->data + sizeof (*hdr);
1648	fw->ucode = fw->main + fw->main_size;
1649
1650	return 0;
1651
1652fail:	free(fw->data, M_DEVBUF, fw->size);
1653	return error;
1654}
1655
1656void
1657ipw_scan(void *arg1)
1658{
1659	struct ipw_softc *sc = arg1;
1660	struct ifnet *ifp = &sc->sc_ic.ic_if;
1661	struct ipw_scan_options scan;
1662	uint8_t ssid[IEEE80211_NWID_LEN];
1663	int error;
1664
1665	/*
1666	 * Firmware has a bug and does not honour the ``do not associate
1667	 * after scan'' bit in the scan command.  To prevent the firmware
1668	 * from associating after the scan, we set the ESSID to something
1669	 * unlikely to be used by a real AP.
1670	 * XXX would setting the desired BSSID to a multicast address work?
1671	 */
1672	memset(ssid, '\r', sizeof ssid);
1673	error = ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, sizeof ssid);
1674	if (error != 0)
1675		goto fail;
1676
1677	/* no mandatory BSSID */
1678	DPRINTF(("Setting mandatory BSSID to null\n"));
1679	error = ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
1680	if (error != 0)
1681		goto fail;
1682
1683	scan.flags = htole32(IPW_SCAN_DO_NOT_ASSOCIATE | IPW_SCAN_MIXED_CELL);
1684	scan.channels = htole32(0x3fff);	/* scan channels 1-14 */
1685	DPRINTF(("Setting scan options to 0x%x\n", letoh32(scan.flags)));
1686	error = ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &scan, sizeof scan);
1687	if (error != 0)
1688		goto fail;
1689
1690	/* start scanning */
1691	DPRINTF(("Enabling adapter\n"));
1692	error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1693	if (error != 0)
1694		goto fail;
1695
1696	return;
1697fail:
1698	printf("%s: scan request failed (error=%d)\n", sc->sc_dev.dv_xname,
1699	    error);
1700	ieee80211_end_scan(ifp);
1701}
1702
1703void
1704ipw_auth_and_assoc(void *arg1)
1705{
1706	struct ipw_softc *sc = arg1;
1707	struct ieee80211com *ic = &sc->sc_ic;
1708	struct ieee80211_node *ni = ic->ic_bss;
1709	struct ipw_scan_options scan;
1710	struct ipw_security security;
1711	struct ipw_assoc_req assoc;
1712	uint32_t data;
1713	uint8_t chan;
1714	int s, error;
1715
1716	DPRINTF(("Disabling adapter\n"));
1717	error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1718	if (error != 0)
1719		goto fail;
1720#if 1
1721	/* wait at most one second for card to be disabled */
1722	s = splnet();
1723	error = tsleep(sc, 0, "ipwdis", hz);
1724	splx(s);
1725	if (error != 0) {
1726		printf("%s: timeout waiting for disabled state\n",
1727		    sc->sc_dev.dv_xname);
1728		goto fail;
1729	}
1730#else
1731	/* Intel's Linux driver polls for the DISABLED state instead.. */
1732	for (ntries = 0; ntries < 1000; ntries++) {
1733		if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == 1)
1734			break;
1735		DELAY(10);
1736	}
1737	if (ntries == 1000) {
1738		printf("%s: timeout waiting for disabled state\n",
1739		    sc->sc_dev.dv_xname);
1740		goto fail;
1741	}
1742#endif
1743
1744	bzero(&security, sizeof security);
1745	security.authmode = IPW_AUTH_OPEN;
1746	security.ciphers = htole32(IPW_CIPHER_NONE);
1747	DPRINTF(("Setting authmode to %u\n", security.authmode));
1748	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFORMATION, &security,
1749	    sizeof security);
1750	if (error != 0)
1751		goto fail;
1752
1753#ifdef IPW_DEBUG
1754	if (ipw_debug > 0) {
1755		printf("Setting ESSID to ");
1756		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
1757		printf("\n");
1758	}
1759#endif
1760	error = ipw_cmd(sc, IPW_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen);
1761	if (error != 0)
1762		goto fail;
1763
1764	DPRINTF(("Setting BSSID to %s\n", ether_sprintf(ni->ni_bssid)));
1765	error = ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, ni->ni_bssid,
1766	    IEEE80211_ADDR_LEN);
1767	if (error != 0)
1768		goto fail;
1769
1770	data = htole32((ic->ic_flags & (IEEE80211_F_WEPON |
1771	    IEEE80211_F_RSNON)) ? IPW_PRIVACYON : 0);
1772	DPRINTF(("Setting privacy flags to 0x%x\n", letoh32(data)));
1773	error = ipw_cmd(sc, IPW_CMD_SET_PRIVACY_FLAGS, &data, sizeof data);
1774	if (error != 0)
1775		goto fail;
1776
1777	/* let firmware set the capinfo, lintval, and bssid fixed fields */
1778	bzero(&assoc, sizeof assoc);
1779	if (ic->ic_flags & IEEE80211_F_RSNON) {
1780		uint8_t *frm = assoc.optie;
1781
1782		/* tell firmware to add a WPA or RSN IE in (Re)Assoc req */
1783		if (ni->ni_rsnprotos & IEEE80211_PROTO_RSN)
1784			frm = ieee80211_add_rsn(frm, ic, ni);
1785		else if (ni->ni_rsnprotos & IEEE80211_PROTO_WPA)
1786			frm = ieee80211_add_wpa(frm, ic, ni);
1787		assoc.optie_len = htole32(frm - assoc.optie);
1788	}
1789	DPRINTF(("Preparing assocation request (optional IE length=%d)\n",
1790	    letoh32(assoc.optie_len)));
1791	error = ipw_cmd(sc, IPW_CMD_SET_ASSOC_REQ, &assoc, sizeof assoc);
1792	if (error != 0)
1793		goto fail;
1794
1795	scan.flags = htole32(IPW_SCAN_MIXED_CELL);
1796	chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1797	scan.channels = htole32(1 << (chan - 1));
1798	DPRINTF(("Setting scan options to 0x%x\n", letoh32(scan.flags)));
1799	error = ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &scan, sizeof scan);
1800	if (error != 0)
1801		goto fail;
1802
1803	/* trigger scan+association */
1804	DPRINTF(("Enabling adapter\n"));
1805	error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1806	if (error != 0)
1807		goto fail;
1808
1809	return;
1810fail:
1811	printf("%s: association failed (error=%d)\n", sc->sc_dev.dv_xname,
1812	    error);
1813	ieee80211_begin_scan(&ic->ic_if);
1814}
1815
1816int
1817ipw_config(struct ipw_softc *sc)
1818{
1819	struct ieee80211com *ic = &sc->sc_ic;
1820	struct ifnet *ifp = &ic->ic_if;
1821	struct ipw_configuration config;
1822	uint32_t data;
1823	int error;
1824
1825	switch (ic->ic_opmode) {
1826	case IEEE80211_M_STA:
1827		data = htole32(IPW_MODE_BSS);
1828		break;
1829#ifndef IEEE80211_STA_ONLY
1830	case IEEE80211_M_IBSS:
1831		data = htole32(IPW_MODE_IBSS);
1832		break;
1833#endif
1834	case IEEE80211_M_MONITOR:
1835		data = htole32(IPW_MODE_MONITOR);
1836		break;
1837	default:
1838		/* should not get there */
1839		return ENODEV;
1840	}
1841	DPRINTF(("Setting mode to %u\n", letoh32(data)));
1842	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
1843	if (error != 0)
1844		return error;
1845
1846	if (
1847#ifndef IEEE80211_STA_ONLY
1848	    ic->ic_opmode == IEEE80211_M_IBSS ||
1849#endif
1850	    ic->ic_opmode == IEEE80211_M_MONITOR) {
1851		data = htole32(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
1852		DPRINTF(("Setting channel to %u\n", letoh32(data)));
1853		error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
1854		if (error != 0)
1855			return error;
1856	}
1857
1858	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
1859		DPRINTF(("Enabling adapter\n"));
1860		return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1861	}
1862
1863	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1864	DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
1865	error = ipw_cmd(sc, IPW_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
1866	    IEEE80211_ADDR_LEN);
1867	if (error != 0)
1868		return error;
1869
1870	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
1871	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1X_ENABLE);
1872#ifndef IEEE80211_STA_ONLY
1873	if (ic->ic_opmode == IEEE80211_M_IBSS)
1874		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
1875#endif
1876	if (ifp->if_flags & IFF_PROMISC)
1877		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
1878	config.bss_chan = htole32(0x3fff);	/* channels 1-14 */
1879	config.ibss_chan = htole32(0x7ff);	/* channels 1-11 */
1880	DPRINTF(("Setting configuration 0x%x\n", config.flags));
1881	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
1882	if (error != 0)
1883		return error;
1884
1885	data = htole32(ic->ic_rtsthreshold);
1886	DPRINTF(("Setting RTS threshold to %u\n", letoh32(data)));
1887	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
1888	if (error != 0)
1889		return error;
1890
1891	data = htole32(ic->ic_fragthreshold);
1892	DPRINTF(("Setting frag threshold to %u\n", letoh32(data)));
1893	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
1894	if (error != 0)
1895		return error;
1896
1897	data = htole32(0x3);	/* 1, 2 */
1898	DPRINTF(("Setting basic tx rates to 0x%x\n", letoh32(data)));
1899	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
1900	if (error != 0)
1901		return error;
1902
1903	data = htole32(0xf);	/* 1, 2, 5.5, 11 */
1904	DPRINTF(("Setting tx rates to 0x%x\n", letoh32(data)));
1905	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
1906	if (error != 0)
1907		return error;
1908
1909	data = htole32(0xf);	/* 1, 2, 5.5, 11 */
1910	DPRINTF(("Setting MSDU tx rates to 0x%x\n", letoh32(data)));
1911	error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
1912	if (error != 0)
1913		return error;
1914
1915	data = htole32(IPW_POWER_MODE_CAM);
1916	DPRINTF(("Setting power mode to %u\n", letoh32(data)));
1917	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
1918	if (error != 0)
1919		return error;
1920
1921#ifndef IEEE80211_STA_ONLY
1922	if (ic->ic_opmode == IEEE80211_M_IBSS) {
1923		data = htole32(32);	/* default value */
1924		DPRINTF(("Setting tx power index to %u\n", letoh32(data)));
1925		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
1926		    sizeof data);
1927		if (error != 0)
1928			return error;
1929
1930		data = htole32(ic->ic_lintval);
1931		DPRINTF(("Setting beacon interval to %u\n", letoh32(data)));
1932		error = ipw_cmd(sc, IPW_CMD_SET_BEACON_INTERVAL, &data,
1933		    sizeof data);
1934		if (error != 0)
1935			return error;
1936	}
1937#endif
1938	return 0;
1939}
1940
1941int
1942ipw_init(struct ifnet *ifp)
1943{
1944	struct ipw_softc *sc = ifp->if_softc;
1945	struct ieee80211com *ic = &sc->sc_ic;
1946	struct ipw_firmware fw;
1947	int error;
1948
1949	ipw_stop(ifp, 0);
1950
1951	if ((error = ipw_reset(sc)) != 0) {
1952		printf("%s: could not reset adapter\n", sc->sc_dev.dv_xname);
1953		goto fail1;
1954	}
1955
1956	if ((error = ipw_read_firmware(sc, &fw)) != 0) {
1957		printf("%s: error %d, could not read firmware\n",
1958		    sc->sc_dev.dv_xname, error);
1959		goto fail1;
1960	}
1961	if ((error = ipw_load_ucode(sc, fw.ucode, fw.ucode_size)) != 0) {
1962		printf("%s: could not load microcode\n", sc->sc_dev.dv_xname);
1963		goto fail2;
1964	}
1965
1966	ipw_stop_master(sc);
1967
1968	/*
1969	 * Setup tx, rx and status rings.
1970	 */
1971	CSR_WRITE_4(sc, IPW_CSR_TX_BD_BASE, sc->tbd_map->dm_segs[0].ds_addr);
1972	CSR_WRITE_4(sc, IPW_CSR_TX_BD_SIZE, IPW_NTBD);
1973	CSR_WRITE_4(sc, IPW_CSR_TX_READ_INDEX, 0);
1974	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE_INDEX, 0);
1975	sc->txold = IPW_NTBD - 1;	/* latest bd index ack by firmware */
1976	sc->txcur = 0; /* bd index to write to */
1977	sc->txfree = IPW_NTBD - 2;
1978
1979	CSR_WRITE_4(sc, IPW_CSR_RX_BD_BASE, sc->rbd_map->dm_segs[0].ds_addr);
1980	CSR_WRITE_4(sc, IPW_CSR_RX_BD_SIZE, IPW_NRBD);
1981	CSR_WRITE_4(sc, IPW_CSR_RX_READ_INDEX, 0);
1982	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE_INDEX, IPW_NRBD - 1);
1983	sc->rxcur = IPW_NRBD - 1;	/* latest bd index I've read */
1984
1985	CSR_WRITE_4(sc, IPW_CSR_RX_STATUS_BASE,
1986	    sc->status_map->dm_segs[0].ds_addr);
1987
1988	if ((error = ipw_load_firmware(sc, fw.main, fw.main_size)) != 0) {
1989		printf("%s: could not load firmware\n", sc->sc_dev.dv_xname);
1990		goto fail2;
1991	}
1992	free(fw.data, M_DEVBUF, fw.size);
1993	fw.data = NULL;
1994
1995	/* retrieve information tables base addresses */
1996	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
1997	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
1998
1999	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2000
2001	if ((error = ipw_config(sc)) != 0) {
2002		printf("%s: device configuration failed\n",
2003		    sc->sc_dev.dv_xname);
2004		goto fail1;
2005	}
2006
2007	ifq_clr_oactive(&ifp->if_snd);
2008	ifp->if_flags |= IFF_RUNNING;
2009
2010	if (ic->ic_opmode != IEEE80211_M_MONITOR)
2011		ieee80211_begin_scan(ifp);
2012	else
2013		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2014
2015	return 0;
2016
2017fail2:	free(fw.data, M_DEVBUF, fw.size);
2018	fw.data = NULL;
2019fail1:	ipw_stop(ifp, 0);
2020	return error;
2021}
2022
2023void
2024ipw_stop(struct ifnet *ifp, int disable)
2025{
2026	struct ipw_softc *sc = ifp->if_softc;
2027	struct ieee80211com *ic = &sc->sc_ic;
2028	int i;
2029
2030	ipw_stop_master(sc);
2031	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2032
2033	ifp->if_timer = 0;
2034	ifp->if_flags &= ~IFF_RUNNING;
2035	ifq_clr_oactive(&ifp->if_snd);
2036
2037	/*
2038	 * Release tx buffers.
2039	 */
2040	for (i = 0; i < IPW_NTBD; i++)
2041		ipw_release_sbd(sc, &sc->stbd_list[i]);
2042
2043	/* in case we were scanning, release the scan "lock" */
2044	ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED;
2045
2046	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2047}
2048
2049void
2050ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2051    bus_size_t count)
2052{
2053	for (; count > 0; offset++, datap++, count--) {
2054		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2055		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2056	}
2057}
2058
2059void
2060ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2061    bus_size_t count)
2062{
2063	for (; count > 0; offset++, datap++, count--) {
2064		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2065		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2066	}
2067}
2068
2069struct cfdriver ipw_cd = {
2070	NULL, "ipw", DV_IFNET
2071};
2072