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