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