xref: /netbsd-src/sys/dev/pci/if_ipw.c (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1 /*	$NetBSD: if_ipw.c,v 1.76 2022/08/22 18:08:05 thorpej Exp $	*/
2 /*	FreeBSD: src/sys/dev/ipw/if_ipw.c,v 1.15 2005/11/13 17:17:40 damien Exp 	*/
3 
4 /*-
5  * Copyright (c) 2004, 2005
6  *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: if_ipw.c,v 1.76 2022/08/22 18:08:05 thorpej Exp $");
33 
34 /*-
35  * Intel(R) PRO/Wireless 2100 MiniPCI driver
36  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
37  */
38 
39 
40 #include <sys/param.h>
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43 #include <sys/mbuf.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/conf.h>
49 #include <sys/proc.h>
50 
51 #include <sys/bus.h>
52 #include <machine/endian.h>
53 #include <sys/intr.h>
54 
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
57 #include <dev/pci/pcidevs.h>
58 
59 #include <net/bpf.h>
60 #include <net/if.h>
61 #include <net/if_arp.h>
62 #include <net/if_dl.h>
63 #include <net/if_ether.h>
64 #include <net/if_media.h>
65 #include <net/if_types.h>
66 
67 #include <net80211/ieee80211_var.h>
68 #include <net80211/ieee80211_radiotap.h>
69 
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/in_var.h>
73 #include <netinet/ip.h>
74 
75 #include <dev/firmload.h>
76 
77 #include <dev/pci/if_ipwreg.h>
78 #include <dev/pci/if_ipwvar.h>
79 
80 #ifdef IPW_DEBUG
81 #define DPRINTF(x)	if (ipw_debug > 0) printf x
82 #define DPRINTFN(n, x)	if (ipw_debug >= (n)) printf x
83 int ipw_debug = 0;
84 #else
85 #define DPRINTF(x)
86 #define DPRINTFN(n, x)
87 #endif
88 
89 /* Permit loading the Intel firmware */
90 static int ipw_accept_eula;
91 
92 static int	ipw_dma_alloc(struct ipw_softc *);
93 static void	ipw_release(struct ipw_softc *);
94 static int	ipw_match(device_t, cfdata_t, void *);
95 static void	ipw_attach(device_t, device_t, void *);
96 static int	ipw_detach(device_t, int);
97 
98 static int	ipw_media_change(struct ifnet *);
99 static void	ipw_media_status(struct ifnet *, struct ifmediareq *);
100 static int	ipw_newstate(struct ieee80211com *, enum ieee80211_state, int);
101 static uint16_t	ipw_read_prom_word(struct ipw_softc *, uint8_t);
102 static void	ipw_command_intr(struct ipw_softc *, struct ipw_soft_buf *);
103 static void	ipw_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
104 static void	ipw_data_intr(struct ipw_softc *, struct ipw_status *,
105 		    struct ipw_soft_bd *, struct ipw_soft_buf *);
106 static void	ipw_rx_intr(struct ipw_softc *);
107 static void	ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
108 static void	ipw_tx_intr(struct ipw_softc *);
109 static int	ipw_intr(void *);
110 static void	ipw_softintr(void *);
111 static int	ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
112 static int	ipw_tx_start(struct ifnet *, struct mbuf *,
113 		    struct ieee80211_node *);
114 static void	ipw_start(struct ifnet *);
115 static void	ipw_watchdog(struct ifnet *);
116 static int	ipw_ioctl(struct ifnet *, u_long, void *);
117 static int	ipw_get_table1(struct ipw_softc *, uint32_t *);
118 static int	ipw_get_radio(struct ipw_softc *, int *);
119 static void	ipw_stop_master(struct ipw_softc *);
120 static int	ipw_reset(struct ipw_softc *);
121 static int	ipw_load_ucode(struct ipw_softc *, u_char *, int);
122 static int	ipw_load_firmware(struct ipw_softc *, u_char *, int);
123 static int	ipw_cache_firmware(struct ipw_softc *);
124 static void	ipw_free_firmware(struct ipw_softc *);
125 static int	ipw_config(struct ipw_softc *);
126 static int	ipw_init(struct ifnet *);
127 static void	ipw_stop(struct ifnet *, int);
128 static uint32_t	ipw_read_table1(struct ipw_softc *, uint32_t);
129 static void	ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
130 static int	ipw_read_table2(struct ipw_softc *, uint32_t, void *, uint32_t *);
131 static void	ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
132     bus_size_t);
133 static void	ipw_write_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
134     bus_size_t);
135 
136 static inline uint8_t
137 MEM_READ_1(struct ipw_softc *sc, uint32_t addr)
138 {
139 	CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
140 	return CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA);
141 }
142 
143 static inline uint32_t
144 MEM_READ_4(struct ipw_softc *sc, uint32_t addr)
145 {
146 	CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, addr);
147 	return CSR_READ_4(sc, IPW_CSR_INDIRECT_DATA);
148 }
149 
150 CFATTACH_DECL_NEW(ipw, sizeof (struct ipw_softc), ipw_match, ipw_attach,
151     ipw_detach, NULL);
152 
153 static int
154 ipw_match(device_t parent, cfdata_t match, void *aux)
155 {
156 	struct pci_attach_args *pa = aux;
157 
158 	if (PCI_VENDOR (pa->pa_id) == PCI_VENDOR_INTEL &&
159 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2100)
160 		return 1;
161 
162 	return 0;
163 }
164 
165 /* Base Address Register */
166 #define IPW_PCI_BAR0	0x10
167 
168 static void
169 ipw_attach(device_t parent, device_t self, void *aux)
170 {
171 	struct ipw_softc *sc = device_private(self);
172 	struct ieee80211com *ic = &sc->sc_ic;
173 	struct ifnet *ifp = &sc->sc_if;
174 	struct pci_attach_args *pa = aux;
175 	const char *intrstr;
176 	bus_space_tag_t memt;
177 	bus_space_handle_t memh;
178 	bus_addr_t base;
179 	pci_intr_handle_t ih;
180 	uint32_t data;
181 	uint16_t val;
182 	int i, error;
183 	char intrbuf[PCI_INTRSTR_LEN];
184 
185 	sc->sc_dev = self;
186 	sc->sc_pct = pa->pa_pc;
187 	sc->sc_pcitag = pa->pa_tag;
188 
189 	pci_aprint_devinfo(pa, NULL);
190 
191 	/* enable bus-mastering */
192 	data = pci_conf_read(sc->sc_pct, pa->pa_tag, PCI_COMMAND_STATUS_REG);
193 	data |= PCI_COMMAND_MASTER_ENABLE;
194 	pci_conf_write(sc->sc_pct, pa->pa_tag, PCI_COMMAND_STATUS_REG, data);
195 
196 	/* map the register window */
197 	error = pci_mapreg_map(pa, IPW_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
198 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz);
199 	if (error != 0) {
200 		aprint_error_dev(sc->sc_dev, "could not map memory space\n");
201 		return;
202 	}
203 
204 	sc->sc_st = memt;
205 	sc->sc_sh = memh;
206 	sc->sc_dmat = pa->pa_dmat;
207 	sc->sc_fwname = "ipw2100-1.2.fw";
208 
209 	/* disable interrupts */
210 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
211 
212 	if (pci_intr_map(pa, &ih) != 0) {
213 		aprint_error_dev(sc->sc_dev, "could not map interrupt\n");
214 		goto fail;
215 	}
216 
217 	sc->sc_soft_ih = softint_establish(SOFTINT_NET, ipw_softintr, sc);
218 	if (sc->sc_soft_ih == NULL) {
219 		aprint_error_dev(sc->sc_dev, "could not establish softint\n");
220 		goto fail;
221 	}
222 
223 	intrstr = pci_intr_string(sc->sc_pct, ih, intrbuf, sizeof(intrbuf));
224 	sc->sc_ih = pci_intr_establish_xname(sc->sc_pct, ih, IPL_NET, ipw_intr,
225 	    sc, device_xname(self));
226 	if (sc->sc_ih == NULL) {
227 		aprint_error_dev(sc->sc_dev, "could not establish interrupt");
228 		if (intrstr != NULL)
229 			aprint_error(" at %s", intrstr);
230 		aprint_error("\n");
231 		goto fail;
232 	}
233 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
234 
235 	if (ipw_reset(sc) != 0) {
236 		aprint_error_dev(sc->sc_dev, "could not reset adapter\n");
237 		goto fail;
238 	}
239 
240 	if (ipw_dma_alloc(sc) != 0) {
241 		aprint_error_dev(sc->sc_dev, "could not allocate DMA resources\n");
242 		goto fail;
243 	}
244 
245 	ifp->if_softc = sc;
246 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
247 	ifp->if_init = ipw_init;
248 	ifp->if_stop = ipw_stop;
249 	ifp->if_ioctl = ipw_ioctl;
250 	ifp->if_start = ipw_start;
251 	ifp->if_watchdog = ipw_watchdog;
252 	IFQ_SET_READY(&ifp->if_snd);
253 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
254 
255 	ic->ic_ifp = ifp;
256 	ic->ic_phytype = IEEE80211_T_DS;
257 	ic->ic_opmode = IEEE80211_M_STA;
258 	ic->ic_state = IEEE80211_S_INIT;
259 
260 	/* set device capabilities */
261 	ic->ic_caps =
262 	      IEEE80211_C_SHPREAMBLE	/* short preamble supported */
263 	    | IEEE80211_C_TXPMGT	/* tx power management */
264 	    | IEEE80211_C_IBSS		/* ibss mode */
265 	    | IEEE80211_C_MONITOR	/* monitor mode */
266 	    ;
267 
268 	/* read MAC address from EEPROM */
269 	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
270 	ic->ic_myaddr[0] = val >> 8;
271 	ic->ic_myaddr[1] = val & 0xff;
272 	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
273 	ic->ic_myaddr[2] = val >> 8;
274 	ic->ic_myaddr[3] = val & 0xff;
275 	val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
276 	ic->ic_myaddr[4] = val >> 8;
277 	ic->ic_myaddr[5] = val & 0xff;
278 
279 	/* set supported .11b rates */
280 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
281 
282 	/* set supported .11b channels (read from EEPROM) */
283 	if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
284 		val = 0x7ff; /* default to channels 1-11 */
285 	val <<= 1;
286 	for (i = 1; i < 16; i++) {
287 		if (val & (1 << i)) {
288 			ic->ic_channels[i].ic_freq =
289 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_B);
290 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B;
291 		}
292 	}
293 
294 	/* check support for radio transmitter switch in EEPROM */
295 	if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
296 		sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
297 
298 	aprint_normal_dev(sc->sc_dev, "802.11 address %s\n",
299 	    ether_sprintf(ic->ic_myaddr));
300 
301 	if_initialize(ifp);
302 	ieee80211_ifattach(ic);
303 	/* Use common softint-based if_input */
304 	ifp->if_percpuq = if_percpuq_create(ifp);
305 	if_register(ifp);
306 
307 	/* override state transition machine */
308 	sc->sc_newstate = ic->ic_newstate;
309 	ic->ic_newstate = ipw_newstate;
310 
311 	ieee80211_media_init(ic, ipw_media_change, ipw_media_status);
312 
313 	bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
314 	    sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
315 
316 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
317 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
318 	sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
319 
320 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
321 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
322 	sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
323 
324 	/*
325 	 * Add a few sysctl knobs.
326 	 * XXX: Not yet
327 	 */
328 	sc->dwelltime = 100;
329 
330 	if (pmf_device_register(self, NULL, NULL))
331 		pmf_class_network_register(self, ifp);
332 	else
333 		aprint_error_dev(self, "couldn't establish power handler\n");
334 
335 	ieee80211_announce(ic);
336 
337 	return;
338 
339 fail:	ipw_detach(self, 0);
340 }
341 
342 static int
343 ipw_detach(device_t self, int flags)
344 {
345 	struct ipw_softc *sc = device_private(self);
346 	struct ifnet *ifp = &sc->sc_if;
347 
348 	if (ifp->if_softc) {
349 		ipw_stop(ifp, 1);
350 		ipw_free_firmware(sc);
351 
352 		bpf_detach(ifp);
353 		ieee80211_ifdetach(&sc->sc_ic);
354 		if_detach(ifp);
355 
356 		ipw_release(sc);
357 	}
358 
359 	if (sc->sc_ih != NULL) {
360 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
361 		sc->sc_ih = NULL;
362 	}
363 
364 	if (sc->sc_soft_ih != NULL) {
365 		softint_disestablish(sc->sc_soft_ih);
366 		sc->sc_soft_ih = NULL;
367 	}
368 
369 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
370 
371 	return 0;
372 }
373 
374 static int
375 ipw_dma_alloc(struct ipw_softc *sc)
376 {
377 	struct ipw_soft_bd *sbd;
378 	struct ipw_soft_hdr *shdr;
379 	struct ipw_soft_buf *sbuf;
380 	int error, i, nsegs;
381 
382 	/*
383 	 * Allocate and map tx ring.
384 	 */
385 	error = bus_dmamap_create(sc->sc_dmat, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0,
386 	    BUS_DMA_NOWAIT, &sc->tbd_map);
387 	if (error != 0) {
388 		aprint_error_dev(sc->sc_dev, "could not create tbd dma map\n");
389 		goto fail;
390 	}
391 
392 	error = bus_dmamem_alloc(sc->sc_dmat, IPW_TBD_SZ, PAGE_SIZE, 0,
393 	    &sc->tbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
394 	if (error != 0) {
395 		aprint_error_dev(sc->sc_dev, "could not allocate tbd dma memory\n");
396 		goto fail;
397 	}
398 
399 	error = bus_dmamem_map(sc->sc_dmat, &sc->tbd_seg, nsegs, IPW_TBD_SZ,
400 	    (void **)&sc->tbd_list, BUS_DMA_NOWAIT);
401 	if (error != 0) {
402 		aprint_error_dev(sc->sc_dev, "could not map tbd dma memory\n");
403 		goto fail;
404 	}
405 
406 	error = bus_dmamap_load(sc->sc_dmat, sc->tbd_map, sc->tbd_list,
407 	    IPW_TBD_SZ, NULL, BUS_DMA_NOWAIT);
408 	if (error != 0) {
409 		aprint_error_dev(sc->sc_dev, "could not load tbd dma memory\n");
410 		goto fail;
411 	}
412 
413 	(void)memset(sc->tbd_list, 0, IPW_TBD_SZ);
414 
415 	/*
416 	 * Allocate and map rx ring.
417 	 */
418 	error = bus_dmamap_create(sc->sc_dmat, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0,
419 	    BUS_DMA_NOWAIT, &sc->rbd_map);
420 	if (error != 0) {
421 		aprint_error_dev(sc->sc_dev, "could not create rbd dma map\n");
422 		goto fail;
423 	}
424 
425 	error = bus_dmamem_alloc(sc->sc_dmat, IPW_RBD_SZ, PAGE_SIZE, 0,
426 	    &sc->rbd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
427 	if (error != 0) {
428 		aprint_error_dev(sc->sc_dev, "could not allocate rbd dma memory\n");
429 		goto fail;
430 	}
431 
432 	error = bus_dmamem_map(sc->sc_dmat, &sc->rbd_seg, nsegs, IPW_RBD_SZ,
433 	    (void **)&sc->rbd_list, BUS_DMA_NOWAIT);
434 	if (error != 0) {
435 		aprint_error_dev(sc->sc_dev, "could not map rbd dma memory\n");
436 		goto fail;
437 	}
438 
439 	error = bus_dmamap_load(sc->sc_dmat, sc->rbd_map, sc->rbd_list,
440 	    IPW_RBD_SZ, NULL, BUS_DMA_NOWAIT);
441 	if (error != 0) {
442 		aprint_error_dev(sc->sc_dev, "could not load rbd dma memory\n");
443 		goto fail;
444 	}
445 
446 	(void)memset(sc->rbd_list, 0, IPW_RBD_SZ);
447 
448 	/*
449 	 * Allocate and map status ring.
450 	 */
451 	error = bus_dmamap_create(sc->sc_dmat, IPW_STATUS_SZ, 1, IPW_STATUS_SZ,
452 	    0, BUS_DMA_NOWAIT, &sc->status_map);
453 	if (error != 0) {
454 		aprint_error_dev(sc->sc_dev, "could not create status dma map\n");
455 		goto fail;
456 	}
457 
458 	error = bus_dmamem_alloc(sc->sc_dmat, IPW_STATUS_SZ, PAGE_SIZE, 0,
459 	    &sc->status_seg, 1, &nsegs, BUS_DMA_NOWAIT);
460 	if (error != 0) {
461 		aprint_error_dev(sc->sc_dev, "could not allocate status dma memory\n");
462 		goto fail;
463 	}
464 
465 	error = bus_dmamem_map(sc->sc_dmat, &sc->status_seg, nsegs,
466 	    IPW_STATUS_SZ, (void **)&sc->status_list, BUS_DMA_NOWAIT);
467 	if (error != 0) {
468 		aprint_error_dev(sc->sc_dev, "could not map status dma memory\n");
469 		goto fail;
470 	}
471 
472 	error = bus_dmamap_load(sc->sc_dmat, sc->status_map, sc->status_list,
473 	    IPW_STATUS_SZ, NULL, BUS_DMA_NOWAIT);
474 	if (error != 0) {
475 		aprint_error_dev(sc->sc_dev, "could not load status dma memory\n");
476 		goto fail;
477 	}
478 
479 	(void)memset(sc->status_list, 0, IPW_STATUS_SZ);
480 
481 	/*
482 	 * Allocate command DMA map.
483 	 */
484 	error = bus_dmamap_create(sc->sc_dmat, sizeof (struct ipw_cmd),
485 	    1, sizeof (struct ipw_cmd), 0, BUS_DMA_NOWAIT, &sc->cmd_map);
486 	if (error != 0) {
487 		aprint_error_dev(sc->sc_dev, "could not create cmd dma map\n");
488 		goto fail;
489 	}
490 
491 	error = bus_dmamem_alloc(sc->sc_dmat, sizeof (struct ipw_cmd),
492 	    PAGE_SIZE, 0, &sc->cmd_seg, 1, &nsegs, BUS_DMA_NOWAIT);
493 	if (error != 0) {
494 		aprint_error_dev(sc->sc_dev, "could not allocate cmd dma memory\n");
495 		goto fail;
496 	}
497 
498 	error = bus_dmamem_map(sc->sc_dmat, &sc->cmd_seg, nsegs,
499 	    sizeof (struct ipw_cmd), (void **)&sc->cmd, BUS_DMA_NOWAIT);
500 	if (error != 0) {
501 		aprint_error_dev(sc->sc_dev, "could not map cmd dma memory\n");
502 		goto fail;
503 	}
504 
505 	error = bus_dmamap_load(sc->sc_dmat, sc->cmd_map, &sc->cmd,
506 	    sizeof (struct ipw_cmd), NULL, BUS_DMA_NOWAIT);
507 	if (error != 0) {
508 		aprint_error_dev(sc->sc_dev, "could not map cmd dma memory\n");
509 		return error;
510 	}
511 
512 	/*
513 	 * Allocate and map hdr list.
514 	 */
515 
516 	error = bus_dmamap_create(sc->sc_dmat,
517 	    IPW_NDATA * sizeof(struct ipw_hdr), 1,
518 	    sizeof(struct ipw_hdr), 0, BUS_DMA_NOWAIT,
519 	    &sc->hdr_map);
520 	if (error != 0) {
521 		aprint_error_dev(sc->sc_dev, "could not create hdr dma map\n");
522 		goto fail;
523 	}
524 
525 	error = bus_dmamem_alloc(sc->sc_dmat,
526 	    IPW_NDATA * sizeof(struct ipw_hdr), PAGE_SIZE, 0, &sc->hdr_seg,
527 	    1, &nsegs, BUS_DMA_NOWAIT);
528 	if (error != 0) {
529 		aprint_error_dev(sc->sc_dev, "could not allocate hdr memory\n");
530 		goto fail;
531 	}
532 
533 	error = bus_dmamem_map(sc->sc_dmat, &sc->hdr_seg, nsegs,
534 	    IPW_NDATA * sizeof(struct ipw_hdr), (void **)&sc->hdr_list,
535 	    BUS_DMA_NOWAIT);
536 	if (error != 0) {
537 		aprint_error_dev(sc->sc_dev, "could not map hdr memory\n");
538 		goto fail;
539 	}
540 
541 	error = bus_dmamap_load(sc->sc_dmat, sc->hdr_map, sc->hdr_list,
542 	    IPW_NDATA * sizeof(struct ipw_hdr), NULL, BUS_DMA_NOWAIT);
543 	if (error != 0) {
544 		aprint_error_dev(sc->sc_dev, "could not load hdr memory\n");
545 		goto fail;
546 	}
547 
548 	(void)memset(sc->hdr_list, 0, IPW_HDR_SZ);
549 
550 	/*
551 	 * Create DMA hdrs tailq.
552 	 */
553 	TAILQ_INIT(&sc->sc_free_shdr);
554 	for (i = 0; i < IPW_NDATA; i++) {
555 		shdr = &sc->shdr_list[i];
556 		shdr->hdr = sc->hdr_list + i;
557 		shdr->offset = sizeof(struct ipw_hdr) * i;
558 		shdr->addr = sc->hdr_map->dm_segs[0].ds_addr + shdr->offset;
559 		TAILQ_INSERT_TAIL(&sc->sc_free_shdr, shdr, next);
560 	}
561 
562 	/*
563 	 * Allocate tx buffers DMA maps.
564 	 */
565 	TAILQ_INIT(&sc->sc_free_sbuf);
566 	for (i = 0; i < IPW_NDATA; i++) {
567 		sbuf = &sc->tx_sbuf_list[i];
568 
569 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
570 		    IPW_MAX_NSEG, MCLBYTES, 0, BUS_DMA_NOWAIT, &sbuf->map);
571 		if (error != 0) {
572 			aprint_error_dev(sc->sc_dev, "could not create txbuf dma map\n");
573 			goto fail;
574 		}
575 		TAILQ_INSERT_TAIL(&sc->sc_free_sbuf, sbuf, next);
576 	}
577 
578 	/*
579 	 * Initialize tx ring.
580 	 */
581 	for (i = 0; i < IPW_NTBD; i++) {
582 		sbd = &sc->stbd_list[i];
583 		sbd->bd = &sc->tbd_list[i];
584 		sbd->type = IPW_SBD_TYPE_NOASSOC;
585 	}
586 
587 	/*
588 	 * Pre-allocate rx buffers and DMA maps
589 	 */
590 	for (i = 0; i < IPW_NRBD; i++) {
591 		sbd = &sc->srbd_list[i];
592 		sbuf = &sc->rx_sbuf_list[i];
593 		sbd->bd = &sc->rbd_list[i];
594 
595 		MGETHDR(sbuf->m, M_DONTWAIT, MT_DATA);
596 		if (sbuf->m == NULL) {
597 			aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n");
598 			error = ENOMEM;
599 			goto fail;
600 		}
601 
602 		MCLGET(sbuf->m, M_DONTWAIT);
603 		if (!(sbuf->m->m_flags & M_EXT)) {
604 			m_freem(sbuf->m);
605 			sbuf->m = NULL;
606 			aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf cluster\n");
607 			error = ENOMEM;
608 			goto fail;
609 		}
610 
611 		sbuf->m->m_pkthdr.len = sbuf->m->m_len = sbuf->m->m_ext.ext_size;
612 
613 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
614 		    0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sbuf->map);
615 		if (error != 0) {
616 			aprint_error_dev(sc->sc_dev, "could not create rxbuf dma map\n");
617 			m_freem(sbuf->m);
618 			sbuf->m = NULL;
619 			goto fail;
620 		}
621 
622 		error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map,
623 		    sbuf->m, BUS_DMA_READ | BUS_DMA_NOWAIT);
624 		if (error != 0) {
625 			bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
626 			sbuf->map = NULL;
627 			m_freem(sbuf->m);
628 			sbuf->m = NULL;
629 			aprint_error_dev(sc->sc_dev, "could not map rxbuf dma memory\n");
630 			goto fail;
631 		}
632 
633 		sbd->type = IPW_SBD_TYPE_DATA;
634 		sbd->priv = sbuf;
635 		sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
636 		sbd->bd->len = htole32(MCLBYTES);
637 
638 		bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0,
639 		    sbuf->map->dm_mapsize, BUS_DMASYNC_PREREAD);
640 
641 	}
642 
643 	bus_dmamap_sync(sc->sc_dmat, sc->rbd_map, 0, IPW_RBD_SZ,
644 	    BUS_DMASYNC_PREREAD);
645 
646 	return 0;
647 
648 fail:	ipw_release(sc);
649 	return error;
650 }
651 
652 static void
653 ipw_release(struct ipw_softc *sc)
654 {
655 	struct ipw_soft_buf *sbuf;
656 	int i;
657 
658 	if (sc->tbd_map != NULL) {
659 		if (sc->tbd_list != NULL) {
660 			bus_dmamap_unload(sc->sc_dmat, sc->tbd_map);
661 			bus_dmamem_unmap(sc->sc_dmat, (void *)sc->tbd_list,
662 			    IPW_TBD_SZ);
663 			bus_dmamem_free(sc->sc_dmat, &sc->tbd_seg, 1);
664 		}
665 		bus_dmamap_destroy(sc->sc_dmat, sc->tbd_map);
666 	}
667 
668 	if (sc->rbd_map != NULL) {
669 		if (sc->rbd_list != NULL) {
670 			bus_dmamap_unload(sc->sc_dmat, sc->rbd_map);
671 			bus_dmamem_unmap(sc->sc_dmat, (void *)sc->rbd_list,
672 			    IPW_RBD_SZ);
673 			bus_dmamem_free(sc->sc_dmat, &sc->rbd_seg, 1);
674 		}
675 		bus_dmamap_destroy(sc->sc_dmat, sc->rbd_map);
676 	}
677 
678 	if (sc->status_map != NULL) {
679 		if (sc->status_list != NULL) {
680 			bus_dmamap_unload(sc->sc_dmat, sc->status_map);
681 			bus_dmamem_unmap(sc->sc_dmat, (void *)sc->status_list,
682 			    IPW_RBD_SZ);
683 			bus_dmamem_free(sc->sc_dmat, &sc->status_seg, 1);
684 		}
685 		bus_dmamap_destroy(sc->sc_dmat, sc->status_map);
686 	}
687 
688 	for (i = 0; i < IPW_NTBD; i++)
689 		ipw_release_sbd(sc, &sc->stbd_list[i]);
690 
691 	if (sc->cmd_map != NULL)
692 		bus_dmamap_destroy(sc->sc_dmat, sc->cmd_map);
693 
694  	if (sc->hdr_list != NULL) {
695  		bus_dmamap_unload(sc->sc_dmat, sc->hdr_map);
696  		bus_dmamem_unmap(sc->sc_dmat, (void *)sc->hdr_list,
697  		    IPW_NDATA * sizeof(struct ipw_hdr));
698  	}
699  	if (sc->hdr_map != NULL) {
700  		bus_dmamem_free(sc->sc_dmat, &sc->hdr_seg, 1);
701  		bus_dmamap_destroy(sc->sc_dmat, sc->hdr_map);
702  	}
703 
704 	for (i = 0; i < IPW_NDATA; i++)
705 		bus_dmamap_destroy(sc->sc_dmat, sc->tx_sbuf_list[i].map);
706 
707 	for (i = 0; i < IPW_NRBD; i++) {
708 		sbuf = &sc->rx_sbuf_list[i];
709 		if (sbuf->map != NULL) {
710 			if (sbuf->m != NULL) {
711 				bus_dmamap_unload(sc->sc_dmat, sbuf->map);
712 				m_freem(sbuf->m);
713 			}
714 			bus_dmamap_destroy(sc->sc_dmat, sbuf->map);
715 		}
716 	}
717 
718 }
719 
720 static int
721 ipw_media_change(struct ifnet *ifp)
722 {
723 	int error;
724 
725 	error = ieee80211_media_change(ifp);
726 	if (error != ENETRESET)
727 		return error;
728 
729 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
730 		ipw_init(ifp);
731 
732 	return 0;
733 }
734 
735 /*
736  * The firmware automatically adapts the transmit speed. We report the current
737  * transmit speed here.
738  */
739 static void
740 ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
741 {
742 #define N(a)	(sizeof (a) / sizeof (a[0]))
743 	struct ipw_softc *sc = ifp->if_softc;
744 	struct ieee80211com *ic = &sc->sc_ic;
745 	static const struct {
746 		uint32_t	val;
747 		int		rate;
748 	} rates[] = {
749 		{ IPW_RATE_DS1,   2 },
750 		{ IPW_RATE_DS2,   4 },
751 		{ IPW_RATE_DS5,  11 },
752 		{ IPW_RATE_DS11, 22 },
753 	};
754 	uint32_t val;
755 	int rate, i;
756 
757 	imr->ifm_status = IFM_AVALID;
758 	imr->ifm_active = IFM_IEEE80211;
759 	if (ic->ic_state == IEEE80211_S_RUN)
760 		imr->ifm_status |= IFM_ACTIVE;
761 
762 	/* read current transmission rate from adapter */
763 	val = ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf;
764 
765 	/* convert ipw rate to 802.11 rate */
766 	for (i = 0; i < N(rates) && rates[i].val != val; i++);
767 	rate = (i < N(rates)) ? rates[i].rate : 0;
768 
769 	imr->ifm_active |= IFM_IEEE80211_11B;
770 	imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B);
771 	switch (ic->ic_opmode) {
772 	case IEEE80211_M_STA:
773 		break;
774 
775 	case IEEE80211_M_IBSS:
776 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
777 		break;
778 
779 	case IEEE80211_M_MONITOR:
780 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
781 		break;
782 
783 	case IEEE80211_M_AHDEMO:
784 	case IEEE80211_M_HOSTAP:
785 		/* should not get there */
786 		break;
787 	}
788 #undef N
789 }
790 
791 static int
792 ipw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate,
793     int arg)
794 {
795 	struct ifnet *ifp = ic->ic_ifp;
796 	struct ipw_softc *sc = ifp->if_softc;
797 	struct ieee80211_node *ni;
798 	uint8_t macaddr[IEEE80211_ADDR_LEN];
799 	uint32_t len;
800 	struct ipw_rx_radiotap_header *wr = &sc->sc_rxtap;
801 	struct ipw_tx_radiotap_header *wt = &sc->sc_txtap;
802 
803 	switch (nstate) {
804 	case IEEE80211_S_INIT:
805 		break;
806 	default:
807 		KASSERT(ic->ic_curchan != IEEE80211_CHAN_ANYC);
808 		KASSERT(ic->ic_curchan != NULL);
809 		wt->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
810 		wt->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
811 		wr->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
812 		wr->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
813 		break;
814 	}
815 
816 	switch (nstate) {
817 	case IEEE80211_S_RUN:
818 		DELAY(200); /* firmware needs a short delay here */
819 
820 		len = IEEE80211_ADDR_LEN;
821 		ipw_read_table2(sc, IPW_INFO_CURRENT_BSSID, macaddr, &len);
822 
823 		ni = ieee80211_find_node(&ic->ic_scan, macaddr);
824 		if (ni == NULL)
825 			break;
826 
827 		ieee80211_ref_node(ni);
828 		ieee80211_sta_join(ic, ni);
829 		ieee80211_node_authorize(ni);
830 
831 		if (ic->ic_opmode == IEEE80211_M_STA)
832 			ieee80211_notify_node_join(ic, ni, 1);
833 		break;
834 
835 	case IEEE80211_S_INIT:
836 	case IEEE80211_S_SCAN:
837 	case IEEE80211_S_AUTH:
838 	case IEEE80211_S_ASSOC:
839 		break;
840 	}
841 
842 	ic->ic_state = nstate;
843 	return 0;
844 }
845 
846 /*
847  * Read 16 bits at address 'addr' from the serial EEPROM.
848  */
849 static uint16_t
850 ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
851 {
852 	uint32_t tmp;
853 	uint16_t val;
854 	int n;
855 
856 	/* clock C once before the first command */
857 	IPW_EEPROM_CTL(sc, 0);
858 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
859 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
860 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
861 
862 	/* write start bit (1) */
863 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
864 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
865 
866 	/* write READ opcode (10) */
867 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
868 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
869 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
870 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
871 
872 	/* write address A7-A0 */
873 	for (n = 7; n >= 0; n--) {
874 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
875 		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
876 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
877 		    (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
878 	}
879 
880 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
881 
882 	/* read data Q15-Q0 */
883 	val = 0;
884 	for (n = 15; n >= 0; n--) {
885 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
886 		IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
887 		tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
888 		val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
889 	}
890 
891 	IPW_EEPROM_CTL(sc, 0);
892 
893 	/* clear Chip Select and clock C */
894 	IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
895 	IPW_EEPROM_CTL(sc, 0);
896 	IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
897 
898 	return le16toh(val);
899 }
900 
901 static void
902 ipw_command_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
903 {
904 
905 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof (struct ipw_cmd),
906 	    BUS_DMASYNC_POSTREAD);
907 
908 #ifdef IPW_DEBUG
909 	struct ipw_cmd *cmd = mtod(sbuf->m, struct ipw_cmd *);
910 
911 	DPRINTFN(2, ("cmd ack'ed (%u, %u, %u, %u, %u)\n", le32toh(cmd->type),
912 	    le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
913 	    le32toh(cmd->status)));
914 #endif
915 
916 	wakeup(&sc->cmd);
917 }
918 
919 static void
920 ipw_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
921 {
922 	struct ieee80211com *ic = &sc->sc_ic;
923 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
924 	uint32_t state;
925 	int s;
926 
927 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sizeof state,
928 	    BUS_DMASYNC_POSTREAD);
929 
930 	state = le32toh(*mtod(sbuf->m, uint32_t *));
931 
932 	DPRINTFN(2, ("entering state %u\n", state));
933 
934 	s = splnet();
935 
936 	switch (state) {
937 	case IPW_STATE_ASSOCIATED:
938 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
939 		break;
940 
941 	case IPW_STATE_SCANNING:
942 		/* don't leave run state on background scan */
943 		if (ic->ic_state != IEEE80211_S_RUN)
944 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
945 
946 		ic->ic_flags |= IEEE80211_F_SCAN;
947 		break;
948 
949 	case IPW_STATE_SCAN_COMPLETE:
950 		ieee80211_notify_scan_done(ic);
951 		ic->ic_flags &= ~IEEE80211_F_SCAN;
952 		break;
953 
954 	case IPW_STATE_ASSOCIATION_LOST:
955 		ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
956 		break;
957 
958 	case IPW_STATE_RADIO_DISABLED:
959 		ic->ic_ifp->if_flags &= ~IFF_UP;
960 		ipw_stop(ifp, 1);
961 		break;
962 	}
963 
964 	splx(s);
965 }
966 
967 /*
968  * XXX: Hack to set the current channel to the value advertised in beacons or
969  * probe responses. Only used during AP detection.
970  */
971 static void
972 ipw_fix_channel(struct ieee80211com *ic, struct mbuf *m)
973 {
974 	struct ieee80211_frame *wh;
975 	uint8_t subtype;
976 	uint8_t *frm, *efrm;
977 
978 	wh = mtod(m, struct ieee80211_frame *);
979 
980 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
981 		return;
982 
983 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
984 
985 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
986 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
987 		return;
988 
989 	frm = (uint8_t *)(wh + 1);
990 	efrm = mtod(m, uint8_t *) + m->m_len;
991 
992 	frm += 12;	/* skip tstamp, bintval and capinfo fields */
993 	while (frm + 2 < efrm) {
994 		if (*frm == IEEE80211_ELEMID_DSPARMS) {
995 #if IEEE80211_CHAN_MAX < 255
996 			if (frm[2] <= IEEE80211_CHAN_MAX)
997 #endif
998 				ic->ic_curchan = &ic->ic_channels[frm[2]];
999 		}
1000 
1001 		frm += frm[1] + 2;
1002 	}
1003 }
1004 
1005 static void
1006 ipw_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1007     struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1008 {
1009 	struct ieee80211com *ic = &sc->sc_ic;
1010 	struct ifnet *ifp = &sc->sc_if;
1011 	struct mbuf *mnew, *m;
1012 	struct ieee80211_frame *wh;
1013 	struct ieee80211_node *ni;
1014 	int error, s;
1015 
1016 	DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1017 	    status->rssi));
1018 
1019 	if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1020 	    le32toh(status->len) > MCLBYTES)
1021 		return;
1022 
1023 	/*
1024 	 * Try to allocate a new mbuf for this ring element and load it before
1025 	 * processing the current mbuf. If the ring element cannot be loaded,
1026 	 * drop the received packet and reuse the old mbuf. In the unlikely
1027 	 * case that the old mbuf can't be reloaded either, explicitly panic.
1028 	 */
1029 	MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1030 	if (mnew == NULL) {
1031 		aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n");
1032 		if_statinc(ifp, if_ierrors);
1033 		return;
1034 	}
1035 
1036 	MCLGET(mnew, M_DONTWAIT);
1037 	if (!(mnew->m_flags & M_EXT)) {
1038 		aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf cluster\n");
1039 		m_freem(mnew);
1040 		if_statinc(ifp, if_ierrors);
1041 		return;
1042 	}
1043 
1044 	mnew->m_pkthdr.len = mnew->m_len = mnew->m_ext.ext_size;
1045 
1046 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, le32toh(status->len),
1047 	    BUS_DMASYNC_POSTREAD);
1048 	bus_dmamap_unload(sc->sc_dmat, sbuf->map);
1049 
1050 	error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, mnew,
1051 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
1052 	if (error != 0) {
1053 		aprint_error_dev(sc->sc_dev, "could not load rx buf DMA map\n");
1054 		m_freem(mnew);
1055 
1056 		/* try to reload the old mbuf */
1057 		error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map,
1058 		    sbuf->m, BUS_DMA_READ | BUS_DMA_NOWAIT);
1059 		if (error != 0) {
1060 			/* very unlikely that it will fail... */
1061 			panic("%s: unable to remap rx buf",
1062 			    device_xname(sc->sc_dev));
1063 		}
1064 		if_statinc(ifp, if_ierrors);
1065 		return;
1066 	}
1067 
1068 	/*
1069 	 * New mbuf successfully loaded, update Rx ring and continue
1070 	 * processing.
1071 	 */
1072 	m = sbuf->m;
1073 	sbuf->m = mnew;
1074 	sbd->bd->physaddr = htole32(sbuf->map->dm_segs[0].ds_addr);
1075 
1076 	/* finalize mbuf */
1077 	m_set_rcvif(m, ifp);
1078 	m->m_pkthdr.len = m->m_len = le32toh(status->len);
1079 
1080 	s = splnet();
1081 
1082 	if (sc->sc_drvbpf != NULL) {
1083 		struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1084 
1085 		tap->wr_antsignal = status->rssi;
1086 
1087 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m, BPF_D_IN);
1088 	}
1089 
1090 	if (ic->ic_state == IEEE80211_S_SCAN)
1091 		ipw_fix_channel(ic, m);
1092 
1093 	wh = mtod(m, struct ieee80211_frame *);
1094 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1095 
1096 	/* send the frame to the 802.11 layer */
1097 	ieee80211_input(ic, m, ni, status->rssi, 0);
1098 
1099 	/* node is no longer needed */
1100 	ieee80211_free_node(ni);
1101 
1102 	splx(s);
1103 
1104 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0,
1105 	    sbuf->map->dm_mapsize, BUS_DMASYNC_PREREAD);
1106 }
1107 
1108 static void
1109 ipw_rx_intr(struct ipw_softc *sc)
1110 {
1111 	struct ipw_status *status;
1112 	struct ipw_soft_bd *sbd;
1113 	struct ipw_soft_buf *sbuf;
1114 	uint32_t r, i;
1115 
1116 	if (!(sc->flags & IPW_FLAG_FW_INITED))
1117 		return;
1118 
1119 	r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1120 
1121 	for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1122 
1123 		/* firmware was killed, stop processing received frames */
1124 		if (!(sc->flags & IPW_FLAG_FW_INITED))
1125 			return;
1126 
1127 		bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
1128 		    i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
1129 		    BUS_DMASYNC_POSTREAD);
1130 
1131 		bus_dmamap_sync(sc->sc_dmat, sc->status_map,
1132 		    i * sizeof (struct ipw_status), sizeof (struct ipw_status),
1133 		    BUS_DMASYNC_POSTREAD);
1134 
1135 		status = &sc->status_list[i];
1136 		sbd = &sc->srbd_list[i];
1137 		sbuf = sbd->priv;
1138 
1139 		switch (le16toh(status->code) & 0xf) {
1140 		case IPW_STATUS_CODE_COMMAND:
1141 			ipw_command_intr(sc, sbuf);
1142 			break;
1143 
1144 		case IPW_STATUS_CODE_NEWSTATE:
1145 			ipw_newstate_intr(sc, sbuf);
1146 			break;
1147 
1148 		case IPW_STATUS_CODE_DATA_802_3:
1149 		case IPW_STATUS_CODE_DATA_802_11:
1150 			ipw_data_intr(sc, status, sbd, sbuf);
1151 			break;
1152 
1153 		case IPW_STATUS_CODE_NOTIFICATION:
1154 			DPRINTFN(2, ("received notification\n"));
1155 			break;
1156 
1157 		default:
1158 			aprint_error_dev(sc->sc_dev, "unknown status code %u\n",
1159 			    le16toh(status->code));
1160 		}
1161 
1162 		sbd->bd->flags = 0;
1163 
1164 		bus_dmamap_sync(sc->sc_dmat, sc->rbd_map,
1165 		    i * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
1166 		    BUS_DMASYNC_PREREAD);
1167 
1168 		bus_dmamap_sync(sc->sc_dmat, sc->status_map,
1169 		    i * sizeof (struct ipw_status), sizeof (struct ipw_status),
1170 		    BUS_DMASYNC_PREREAD);
1171 	}
1172 
1173 	/* Tell the firmware what we have processed */
1174 	sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1175 	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1176 }
1177 
1178 static void
1179 ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1180 {
1181 	struct ipw_soft_hdr *shdr;
1182 	struct ipw_soft_buf *sbuf;
1183 
1184 	switch (sbd->type) {
1185 	case IPW_SBD_TYPE_COMMAND:
1186 		bus_dmamap_sync(sc->sc_dmat, sc->cmd_map,
1187 		    0, sizeof(struct ipw_cmd), BUS_DMASYNC_POSTWRITE);
1188 /*		bus_dmamap_unload(sc->sc_dmat, sc->cmd_map); */
1189 		break;
1190 
1191 	case IPW_SBD_TYPE_HEADER:
1192 		shdr = sbd->priv;
1193  		bus_dmamap_sync(sc->sc_dmat, sc->hdr_map,
1194  		    shdr->offset, sizeof(struct ipw_hdr), BUS_DMASYNC_POSTWRITE);
1195 		TAILQ_INSERT_TAIL(&sc->sc_free_shdr, shdr, next);
1196 		break;
1197 
1198 	case IPW_SBD_TYPE_DATA:
1199 		sbuf = sbd->priv;
1200 
1201 		bus_dmamap_sync(sc->sc_dmat, sbuf->map,
1202 		    0, sbuf->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1203 		bus_dmamap_unload(sc->sc_dmat, sbuf->map);
1204 		m_freem(sbuf->m);
1205 		if (sbuf->ni != NULL)
1206 			ieee80211_free_node(sbuf->ni);
1207 		/* kill watchdog timer */
1208 		sc->sc_tx_timer = 0;
1209 		TAILQ_INSERT_TAIL(&sc->sc_free_sbuf, sbuf, next);
1210 		break;
1211 	}
1212 	sbd->type = IPW_SBD_TYPE_NOASSOC;
1213 }
1214 
1215 static void
1216 ipw_tx_intr(struct ipw_softc *sc)
1217 {
1218 	struct ifnet *ifp = &sc->sc_if;
1219 	struct ipw_soft_bd *sbd;
1220 	uint32_t r, i;
1221 	int s;
1222 
1223 	if (!(sc->flags & IPW_FLAG_FW_INITED))
1224 		return;
1225 
1226 	s = splnet();
1227 
1228 	r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1229 
1230 	for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1231 		sbd = &sc->stbd_list[i];
1232 
1233 		if (sbd->type == IPW_SBD_TYPE_DATA)
1234 			if_statinc(ifp, if_opackets);
1235 
1236 		ipw_release_sbd(sc, sbd);
1237 		sc->txfree++;
1238 	}
1239 
1240 	/* remember what the firmware has processed */
1241 	sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1242 
1243 	/* Call start() since some buffer descriptors have been released */
1244 	ifp->if_flags &= ~IFF_OACTIVE;
1245 	ipw_start(ifp); /* in softint */
1246 
1247 	splx(s);
1248 }
1249 
1250 static int
1251 ipw_intr(void *arg)
1252 {
1253 	struct ipw_softc *sc = arg;
1254 	uint32_t r;
1255 
1256 	r = CSR_READ_4(sc, IPW_CSR_INTR);
1257 	if (r == 0 || r == 0xffffffff)
1258 		return 0;
1259 
1260 	/* Disable interrupts */
1261 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1262 
1263 	softint_schedule(sc->sc_soft_ih);
1264 	return 1;
1265 }
1266 
1267 static void
1268 ipw_softintr(void *arg)
1269 {
1270 	struct ipw_softc *sc = arg;
1271 	uint32_t r;
1272 	int s;
1273 
1274 	r = CSR_READ_4(sc, IPW_CSR_INTR);
1275 	if (r == 0 || r == 0xffffffff)
1276 		goto out;
1277 
1278 	if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1279 		aprint_error_dev(sc->sc_dev, "fatal error\n");
1280 		s = splnet();
1281 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1282 		ipw_stop(&sc->sc_if, 1);
1283 		splx(s);
1284 	}
1285 
1286 	if (r & IPW_INTR_FW_INIT_DONE) {
1287 		if (!(r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)))
1288 			wakeup(sc);
1289 	}
1290 
1291 	if (r & IPW_INTR_RX_TRANSFER)
1292 		ipw_rx_intr(sc);
1293 
1294 	if (r & IPW_INTR_TX_TRANSFER)
1295 		ipw_tx_intr(sc);
1296 
1297 	/* Acknowledge all interrupts */
1298 	CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1299 
1300  out:
1301 	/* Re-enable interrupts */
1302 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1303 }
1304 
1305 /*
1306  * Send a command to the firmware and wait for the acknowledgement.
1307  */
1308 static int
1309 ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1310 {
1311 	struct ipw_soft_bd *sbd;
1312 
1313 	sbd = &sc->stbd_list[sc->txcur];
1314 
1315 	sc->cmd.type = htole32(type);
1316 	sc->cmd.subtype = 0;
1317 	sc->cmd.len = htole32(len);
1318 	sc->cmd.seq = 0;
1319 
1320 	(void)memcpy(sc->cmd.data, data, len);
1321 
1322 	sbd->type = IPW_SBD_TYPE_COMMAND;
1323 	sbd->bd->physaddr = htole32(sc->cmd_map->dm_segs[0].ds_addr);
1324 	sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1325 	sbd->bd->nfrag = 1;
1326 	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1327 			 IPW_BD_FLAG_TX_LAST_FRAGMENT;
1328 
1329 	bus_dmamap_sync(sc->sc_dmat, sc->cmd_map, 0, sizeof (struct ipw_cmd),
1330 	    BUS_DMASYNC_PREWRITE);
1331 
1332 	bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
1333 	    sc->txcur * sizeof (struct ipw_bd), sizeof (struct ipw_bd),
1334 	    BUS_DMASYNC_PREWRITE);
1335 
1336 	DPRINTFN(2, ("sending command (%u, %u, %u, %u)\n", type, 0, 0, len));
1337 
1338 	/* kick firmware */
1339 	sc->txfree--;
1340 	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1341 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1342 
1343 	/* Wait at most one second for command to complete */
1344 	return tsleep(&sc->cmd, 0, "ipwcmd", hz);
1345 }
1346 
1347 static int
1348 ipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1349 {
1350 	struct ipw_softc *sc = ifp->if_softc;
1351 	struct ieee80211com *ic = &sc->sc_ic;
1352 	struct ieee80211_frame *wh;
1353 	struct ipw_soft_bd *sbd;
1354 	struct ipw_soft_hdr *shdr;
1355 	struct ipw_soft_buf *sbuf;
1356 	struct ieee80211_key *k;
1357 	struct mbuf *mnew;
1358 	int error, i;
1359 
1360 	wh = mtod(m0, struct ieee80211_frame *);
1361 
1362 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1363 		k = ieee80211_crypto_encap(ic, ni, m0);
1364 		if (k == NULL) {
1365 			m_freem(m0);
1366 			return ENOBUFS;
1367 		}
1368 
1369 		/* packet header may have moved, reset our local pointer */
1370 		wh = mtod(m0, struct ieee80211_frame *);
1371 	}
1372 
1373 	if (sc->sc_drvbpf != NULL) {
1374 		struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1375 
1376 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT);
1377 	}
1378 
1379 	shdr = TAILQ_FIRST(&sc->sc_free_shdr);
1380 	sbuf = TAILQ_FIRST(&sc->sc_free_sbuf);
1381 	KASSERT(shdr != NULL && sbuf != NULL);
1382 
1383 	shdr->hdr->type = htole32(IPW_HDR_TYPE_SEND);
1384 	shdr->hdr->subtype = 0;
1385 	shdr->hdr->encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1386 	shdr->hdr->encrypt = 0;
1387 	shdr->hdr->keyidx = 0;
1388 	shdr->hdr->keysz = 0;
1389 	shdr->hdr->fragmentsz = 0;
1390 	IEEE80211_ADDR_COPY(shdr->hdr->src_addr, wh->i_addr2);
1391 	if (ic->ic_opmode == IEEE80211_M_STA)
1392 		IEEE80211_ADDR_COPY(shdr->hdr->dst_addr, wh->i_addr3);
1393 	else
1394 		IEEE80211_ADDR_COPY(shdr->hdr->dst_addr, wh->i_addr1);
1395 
1396 	/* trim IEEE802.11 header */
1397 	m_adj(m0, sizeof (struct ieee80211_frame));
1398 
1399 	error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, m0,
1400 	    BUS_DMA_NOWAIT);
1401 	if (error != 0 && error != EFBIG) {
1402 		aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
1403 		    error);
1404 		m_freem(m0);
1405 		return error;
1406 	}
1407 
1408 	if (error != 0) {
1409 		/* too many fragments, linearize */
1410 
1411 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1412 		if (mnew == NULL) {
1413 			m_freem(m0);
1414 			return ENOMEM;
1415 		}
1416 
1417 		m_copy_pkthdr(mnew, m0);
1418 
1419 		/* If the data won't fit in the header, get a cluster */
1420 		if (m0->m_pkthdr.len > MHLEN) {
1421 			MCLGET(mnew, M_DONTWAIT);
1422 			if (!(mnew->m_flags & M_EXT)) {
1423 				m_freem(m0);
1424 				m_freem(mnew);
1425 				return ENOMEM;
1426 			}
1427 		}
1428 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
1429 		m_freem(m0);
1430 		mnew->m_len = mnew->m_pkthdr.len;
1431 		m0 = mnew;
1432 
1433 		error = bus_dmamap_load_mbuf(sc->sc_dmat, sbuf->map, m0,
1434 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1435 		if (error != 0) {
1436 			aprint_error_dev(sc->sc_dev,
1437 			    "could not map mbuf (error %d)\n", error);
1438 			m_freem(m0);
1439 			return error;
1440 		}
1441 	}
1442 
1443 	TAILQ_REMOVE(&sc->sc_free_sbuf, sbuf, next);
1444 	TAILQ_REMOVE(&sc->sc_free_shdr, shdr, next);
1445 
1446 	sbd = &sc->stbd_list[sc->txcur];
1447 	sbd->type = IPW_SBD_TYPE_HEADER;
1448 	sbd->priv = shdr;
1449  	sbd->bd->physaddr = htole32(shdr->addr);
1450 	sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1451 	sbd->bd->nfrag = 1 + sbuf->map->dm_nsegs;
1452 	sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1453 			 IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1454 
1455 	DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, )\n",
1456 	    shdr->hdr->type, shdr->hdr->subtype, shdr->hdr->encrypted,
1457 	    shdr->hdr->encrypt));
1458 	DPRINTFN(5, ("%s->", ether_sprintf(shdr->hdr->src_addr)));
1459 	DPRINTFN(5, ("%s\n", ether_sprintf(shdr->hdr->dst_addr)));
1460 
1461 	bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
1462 	    sc->txcur * sizeof (struct ipw_bd),
1463 	    sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
1464 
1465 	sc->txfree--;
1466 	sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1467 
1468 	sbuf->m = m0;
1469 	sbuf->ni = ni;
1470 
1471 	for (i = 0; i < sbuf->map->dm_nsegs; i++) {
1472 		sbd = &sc->stbd_list[sc->txcur];
1473 
1474 		sbd->bd->physaddr = htole32(sbuf->map->dm_segs[i].ds_addr);
1475 		sbd->bd->len = htole32(sbuf->map->dm_segs[i].ds_len);
1476 		sbd->bd->nfrag = 0;
1477 		sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1478 		if (i == sbuf->map->dm_nsegs - 1) {
1479 			sbd->type = IPW_SBD_TYPE_DATA;
1480 			sbd->priv = sbuf;
1481 			sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1482 		} else {
1483 			sbd->type = IPW_SBD_TYPE_NOASSOC;
1484 			sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1485 		}
1486 
1487 		DPRINTFN(5, ("sending fragment (%d, %d)\n", i,
1488 		    (int)sbuf->map->dm_segs[i].ds_len));
1489 
1490 		bus_dmamap_sync(sc->sc_dmat, sc->tbd_map,
1491 		    sc->txcur * sizeof (struct ipw_bd),
1492 		    sizeof (struct ipw_bd), BUS_DMASYNC_PREWRITE);
1493 
1494 		sc->txfree--;
1495 		sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1496 	}
1497 
1498 	bus_dmamap_sync(sc->sc_dmat, sc->hdr_map, shdr->offset,
1499 	    sizeof (struct ipw_hdr), BUS_DMASYNC_PREWRITE);
1500 
1501 	bus_dmamap_sync(sc->sc_dmat, sbuf->map, 0, sbuf->map->dm_mapsize,
1502 	    BUS_DMASYNC_PREWRITE);
1503 
1504 	/* Inform firmware about this new packet */
1505 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1506 
1507 	return 0;
1508 }
1509 
1510 static void
1511 ipw_start(struct ifnet *ifp)
1512 {
1513 	struct ipw_softc *sc = ifp->if_softc;
1514 	struct ieee80211com *ic = &sc->sc_ic;
1515 	struct mbuf *m0;
1516 	struct ether_header *eh;
1517 	struct ieee80211_node *ni;
1518 
1519 	if (ic->ic_state != IEEE80211_S_RUN)
1520 		return;
1521 
1522 	for (;;) {
1523 		IF_POLL(&ifp->if_snd, m0);
1524 		if (m0 == NULL)
1525 			break;
1526 
1527 		if (sc->txfree < 1 + IPW_MAX_NSEG) {
1528 			ifp->if_flags |= IFF_OACTIVE;
1529 			break;
1530 		}
1531 		IF_DEQUEUE(&ifp->if_snd, m0);
1532 
1533 		KASSERT(m0->m_len >= sizeof(struct ether_header));
1534 
1535 		eh = mtod(m0, struct ether_header *);
1536 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1537 		if (ni == NULL) {
1538 			m_freem(m0);
1539 			continue;
1540 		}
1541 
1542 		bpf_mtap(ifp, m0, BPF_D_OUT);
1543 
1544 		m0 = ieee80211_encap(ic, m0, ni);
1545 		if (m0 == NULL) {
1546 			ieee80211_free_node(ni);
1547 			continue;
1548 		}
1549 
1550 		bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
1551 
1552 		if (ipw_tx_start(ifp, m0, ni) != 0) {
1553 			ieee80211_free_node(ni);
1554 			if_statinc(ifp, if_oerrors);
1555 			break;
1556 		}
1557 
1558 		/* start watchdog timer */
1559 		sc->sc_tx_timer = 5;
1560 		ifp->if_timer = 1;
1561 	}
1562 }
1563 
1564 static void
1565 ipw_watchdog(struct ifnet *ifp)
1566 {
1567 	struct ipw_softc *sc = ifp->if_softc;
1568 
1569 	ifp->if_timer = 0;
1570 
1571 	if (sc->sc_tx_timer > 0) {
1572 		if (--sc->sc_tx_timer == 0) {
1573 			aprint_error_dev(sc->sc_dev, "device timeout\n");
1574 			if_statinc(ifp, if_oerrors);
1575 			ifp->if_flags &= ~IFF_UP;
1576 			ipw_stop(ifp, 1);
1577 			return;
1578 		}
1579 		ifp->if_timer = 1;
1580 	}
1581 
1582 	ieee80211_watchdog(&sc->sc_ic);
1583 }
1584 
1585 static int
1586 ipw_get_table1(struct ipw_softc *sc, uint32_t *tbl)
1587 {
1588 	uint32_t addr, size, data, i;
1589 	int error;
1590 
1591 	if (!(sc->flags & IPW_FLAG_FW_INITED))
1592 		return ENOTTY;
1593 
1594 	CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
1595 
1596 	size = CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA);
1597 	if ((error = copyout(&size, tbl, sizeof(size))) != 0)
1598 		return error;
1599 
1600 	for (i = 1, ++tbl; i < size; i++, tbl++) {
1601 		addr = CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA);
1602 		data = MEM_READ_4(sc, addr);
1603 		if ((error = copyout(&data, tbl, sizeof(data))) != 0)
1604 			return error;
1605 	}
1606 	return 0;
1607 }
1608 
1609 static int
1610 ipw_get_radio(struct ipw_softc *sc, int *ret)
1611 {
1612 	uint32_t addr, data;
1613 
1614 	if (!(sc->flags & IPW_FLAG_FW_INITED))
1615 		return ENOTTY;
1616 
1617 	addr = ipw_read_table1(sc, IPW_INFO_EEPROM_ADDRESS);
1618 	if ((MEM_READ_4(sc, addr + 32) >> 24) & 1)
1619 		data = -1;
1620 	else if (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED)
1621 		data = 0;
1622 	else
1623 		data = 1;
1624 
1625 	return copyout(&data, ret, sizeof(data));
1626 }
1627 
1628 static int
1629 ipw_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1630 {
1631 #define	IS_RUNNING(ifp) \
1632 	((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
1633 
1634 	struct ipw_softc *sc = ifp->if_softc;
1635 	struct ieee80211com *ic = &sc->sc_ic;
1636 	struct ifreq *ifr = (struct ifreq *)data;
1637 	int s, error = 0;
1638 
1639 	s = splnet();
1640 
1641 	switch (cmd) {
1642 	case SIOCSIFFLAGS:
1643 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1644 			break;
1645 		if (ifp->if_flags & IFF_UP) {
1646 			if (!(ifp->if_flags & IFF_RUNNING))
1647 				ipw_init(ifp);
1648 		} else {
1649 			if (ifp->if_flags & IFF_RUNNING)
1650 				ipw_stop(ifp, 1);
1651 		}
1652 		break;
1653 
1654 	case SIOCADDMULTI:
1655 	case SIOCDELMULTI:
1656 		/* XXX no h/w multicast filter? --dyoung */
1657 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1658 			/* setup multicast filter, etc */
1659 			error = 0;
1660 		}
1661 		break;
1662 
1663 	case SIOCGTABLE1:
1664 		error = ipw_get_table1(sc, (uint32_t *)ifr->ifr_data);
1665 		break;
1666 
1667 	case SIOCGRADIO:
1668 		error = ipw_get_radio(sc, (int *)ifr->ifr_data);
1669 		break;
1670 
1671 	case SIOCSIFMEDIA:
1672 		if (ifr->ifr_media & IFM_IEEE80211_ADHOC)
1673 			sc->sc_fwname = "ipw2100-1.2-i.fw";
1674 		else if (ifr->ifr_media & IFM_IEEE80211_MONITOR)
1675 			sc->sc_fwname = "ipw2100-1.2-p.fw";
1676 		else
1677 			sc->sc_fwname = "ipw2100-1.2.fw";
1678 
1679 		ipw_free_firmware(sc);
1680 		/* FALLTHROUGH */
1681 	default:
1682 		error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
1683 		if (error != ENETRESET)
1684 			break;
1685 
1686 		if (error == ENETRESET) {
1687 			if (IS_RUNNING(ifp) &&
1688 			    (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1689 				ipw_init(ifp);
1690 			error = 0;
1691 		}
1692 
1693 	}
1694 
1695 	splx(s);
1696 	return error;
1697 #undef IS_RUNNING
1698 }
1699 
1700 static uint32_t
1701 ipw_read_table1(struct ipw_softc *sc, uint32_t off)
1702 {
1703 	return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
1704 }
1705 
1706 static void
1707 ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
1708 {
1709 	MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
1710 }
1711 
1712 static int
1713 ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
1714 {
1715 	uint32_t addr, info;
1716 	uint16_t count, size;
1717 	uint32_t total;
1718 
1719 	/* addr[4] + count[2] + size[2] */
1720 	addr = MEM_READ_4(sc, sc->table2_base + off);
1721 	info = MEM_READ_4(sc, sc->table2_base + off + 4);
1722 
1723 	count = info >> 16;
1724 	size = info & 0xffff;
1725 	total = count * size;
1726 
1727 	if (total > *len) {
1728 		*len = total;
1729 		return EINVAL;
1730 	}
1731 
1732 	*len = total;
1733 	ipw_read_mem_1(sc, addr, buf, total);
1734 
1735 	return 0;
1736 }
1737 
1738 static void
1739 ipw_stop_master(struct ipw_softc *sc)
1740 {
1741 	int ntries;
1742 
1743 	/* disable interrupts */
1744 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1745 
1746 	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1747 	for (ntries = 0; ntries < 50; ntries++) {
1748 		if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1749 			break;
1750 		DELAY(10);
1751 	}
1752 	if (ntries == 50)
1753 		aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
1754 
1755 	CSR_WRITE_4(sc, IPW_CSR_RST, CSR_READ_4(sc, IPW_CSR_RST) |
1756 	    IPW_RST_PRINCETON_RESET);
1757 
1758 	sc->flags &= ~IPW_FLAG_FW_INITED;
1759 }
1760 
1761 static int
1762 ipw_reset(struct ipw_softc *sc)
1763 {
1764 	int ntries;
1765 
1766 	ipw_stop_master(sc);
1767 
1768 	/* move adapter to D0 state */
1769 	CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
1770 	    IPW_CTL_INIT);
1771 
1772 	/* wait for clock stabilization */
1773 	for (ntries = 0; ntries < 1000; ntries++) {
1774 		if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1775 			break;
1776 		DELAY(200);
1777 	}
1778 	if (ntries == 1000)
1779 		return EIO;
1780 
1781 	CSR_WRITE_4(sc, IPW_CSR_RST, CSR_READ_4(sc, IPW_CSR_RST) |
1782 	    IPW_RST_SW_RESET);
1783 
1784 	DELAY(10);
1785 
1786 	CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
1787 	    IPW_CTL_INIT);
1788 
1789 	return 0;
1790 }
1791 
1792 /*
1793  * Upload the microcode to the device.
1794  */
1795 static int
1796 ipw_load_ucode(struct ipw_softc *sc, u_char *uc, int size)
1797 {
1798 	int ntries;
1799 
1800 	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1801 	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1802 
1803 	MEM_WRITE_2(sc, 0x220000, 0x0703);
1804 	MEM_WRITE_2(sc, 0x220000, 0x0707);
1805 
1806 	MEM_WRITE_1(sc, 0x210014, 0x72);
1807 	MEM_WRITE_1(sc, 0x210014, 0x72);
1808 
1809 	MEM_WRITE_1(sc, 0x210000, 0x40);
1810 	MEM_WRITE_1(sc, 0x210000, 0x00);
1811 	MEM_WRITE_1(sc, 0x210000, 0x40);
1812 
1813 	MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1814 
1815 	MEM_WRITE_1(sc, 0x210000, 0x00);
1816 	MEM_WRITE_1(sc, 0x210000, 0x00);
1817 	MEM_WRITE_1(sc, 0x210000, 0x80);
1818 
1819 	MEM_WRITE_2(sc, 0x220000, 0x0703);
1820 	MEM_WRITE_2(sc, 0x220000, 0x0707);
1821 
1822 	MEM_WRITE_1(sc, 0x210014, 0x72);
1823 	MEM_WRITE_1(sc, 0x210014, 0x72);
1824 
1825 	MEM_WRITE_1(sc, 0x210000, 0x00);
1826 	MEM_WRITE_1(sc, 0x210000, 0x80);
1827 
1828 	for (ntries = 0; ntries < 10; ntries++) {
1829 		if (MEM_READ_1(sc, 0x210000) & 1)
1830 			break;
1831 		DELAY(10);
1832 	}
1833 	if (ntries == 10) {
1834 		aprint_error_dev(sc->sc_dev, "timeout waiting for ucode to initialize\n");
1835 		return EIO;
1836 	}
1837 
1838 	MEM_WRITE_4(sc, 0x3000e0, 0);
1839 
1840 	return 0;
1841 }
1842 
1843 /* set of macros to handle unaligned little endian data in firmware image */
1844 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1845 #define GETLE16(p) ((p)[0] | (p)[1] << 8)
1846 static int
1847 ipw_load_firmware(struct ipw_softc *sc, u_char *fw, int size)
1848 {
1849 	u_char *p, *end;
1850 	uint32_t dst;
1851 	uint16_t len;
1852 	int error;
1853 
1854 	p = fw;
1855 	end = fw + size;
1856 	while (p < end) {
1857 		dst = GETLE32(p); p += 4;
1858 		len = GETLE16(p); p += 2;
1859 
1860 		ipw_write_mem_1(sc, dst, p, len);
1861 		p += len;
1862 	}
1863 
1864 	CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
1865 	    IPW_IO_LED_OFF);
1866 
1867 	/* enable interrupts */
1868 	CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1869 
1870 	/* kick the firmware */
1871 	CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1872 
1873 	CSR_WRITE_4(sc, IPW_CSR_CTL, CSR_READ_4(sc, IPW_CSR_CTL) |
1874 	    IPW_CTL_ALLOW_STANDBY);
1875 
1876 	/* wait at most one second for firmware initialization to complete */
1877 	if ((error = tsleep(sc, 0, "ipwinit", hz)) != 0) {
1878 		aprint_error_dev(sc->sc_dev,
1879 		    "timeout waiting for firmware initialization "
1880 		    "to complete\n");
1881 		return error;
1882 	}
1883 
1884 	CSR_WRITE_4(sc, IPW_CSR_IO, CSR_READ_4(sc, IPW_CSR_IO) |
1885 	    IPW_IO_GPIO1_MASK | IPW_IO_GPIO3_MASK);
1886 
1887 	return 0;
1888 }
1889 
1890 /*
1891  * Store firmware into kernel memory so we can download it when we need to,
1892  * e.g when the adapter wakes up from suspend mode.
1893  */
1894 static int
1895 ipw_cache_firmware(struct ipw_softc *sc)
1896 {
1897 	struct ipw_firmware *fw = &sc->fw;
1898 	struct ipw_firmware_hdr hdr;
1899 	firmware_handle_t fwh;
1900 	off_t fwsz, p;
1901 	int error;
1902 
1903 	ipw_free_firmware(sc);
1904 
1905 	if (ipw_accept_eula == 0) {
1906 		aprint_error_dev(sc->sc_dev,
1907 		    "EULA not accepted; please see the ipw(4) man page.\n");
1908 		return EPERM;
1909 	}
1910 
1911 	if ((error = firmware_open("if_ipw", sc->sc_fwname, &fwh)) != 0)
1912 		goto fail0;
1913 
1914 	fwsz = firmware_get_size(fwh);
1915 
1916 	if (fwsz < sizeof(hdr))
1917 		goto fail2;
1918 
1919 	if ((error = firmware_read(fwh, 0, &hdr, sizeof(hdr))) != 0)
1920 		goto fail2;
1921 
1922 	fw->main_size  = le32toh(hdr.main_size);
1923 	fw->ucode_size = le32toh(hdr.ucode_size);
1924 
1925 	fw->main = firmware_malloc(fw->main_size);
1926 	if (fw->main == NULL) {
1927 		error = ENOMEM;
1928 		goto fail1;
1929 	}
1930 
1931 	fw->ucode = firmware_malloc(fw->ucode_size);
1932 	if (fw->ucode == NULL) {
1933 		error = ENOMEM;
1934 		goto fail2;
1935 	}
1936 
1937 	p = sizeof(hdr);
1938 	if ((error = firmware_read(fwh, p, fw->main, fw->main_size)) != 0)
1939 		goto fail3;
1940 
1941 	p += fw->main_size;
1942 	if ((error = firmware_read(fwh, p, fw->ucode, fw->ucode_size)) != 0)
1943 		goto fail3;
1944 
1945 	DPRINTF(("Firmware cached: main %u, ucode %u\n", fw->main_size,
1946 	    fw->ucode_size));
1947 
1948 	sc->flags |= IPW_FLAG_FW_CACHED;
1949 
1950 	firmware_close(fwh);
1951 
1952 	return 0;
1953 
1954 fail3:	firmware_free(fw->ucode, fw->ucode_size);
1955 fail2:	firmware_free(fw->main, fw->main_size);
1956 fail1:  firmware_close(fwh);
1957 fail0:
1958 	return error;
1959 }
1960 
1961 static void
1962 ipw_free_firmware(struct ipw_softc *sc)
1963 {
1964 	if (!(sc->flags & IPW_FLAG_FW_CACHED))
1965 		return;
1966 
1967 	firmware_free(sc->fw.main, sc->fw.main_size);
1968 	firmware_free(sc->fw.ucode, sc->fw.ucode_size);
1969 
1970 	sc->flags &= ~IPW_FLAG_FW_CACHED;
1971 }
1972 
1973 static int
1974 ipw_config(struct ipw_softc *sc)
1975 {
1976 	struct ieee80211com *ic = &sc->sc_ic;
1977 	struct ifnet *ifp = &sc->sc_if;
1978 	struct ipw_security security;
1979 	struct ieee80211_key *k;
1980 	struct ipw_wep_key wepkey;
1981 	struct ipw_scan_options options;
1982 	struct ipw_configuration config;
1983 	uint32_t data;
1984 	int error, i;
1985 
1986 	switch (ic->ic_opmode) {
1987 	case IEEE80211_M_STA:
1988 	case IEEE80211_M_HOSTAP:
1989 		data = htole32(IPW_MODE_BSS);
1990 		break;
1991 
1992 	case IEEE80211_M_IBSS:
1993 	case IEEE80211_M_AHDEMO:
1994 		data = htole32(IPW_MODE_IBSS);
1995 		break;
1996 
1997 	case IEEE80211_M_MONITOR:
1998 		data = htole32(IPW_MODE_MONITOR);
1999 		break;
2000 	}
2001 	DPRINTF(("Setting mode to %u\n", le32toh(data)));
2002 	error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2003 	if (error != 0)
2004 		return error;
2005 
2006 	if (ic->ic_opmode == IEEE80211_M_IBSS ||
2007 	    ic->ic_opmode == IEEE80211_M_MONITOR) {
2008 		data = htole32(ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
2009 		DPRINTF(("Setting channel to %u\n", le32toh(data)));
2010 		error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2011 		if (error != 0)
2012 			return error;
2013 	}
2014 
2015 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2016 		DPRINTF(("Enabling adapter\n"));
2017 		return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
2018 	}
2019 
2020 	DPRINTF(("Setting MAC to %s\n", ether_sprintf(ic->ic_myaddr)));
2021 	error = ipw_cmd(sc, IPW_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2022 	    IEEE80211_ADDR_LEN);
2023 	if (error != 0)
2024 		return error;
2025 
2026 	config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2027 	    IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2028 
2029 	if (ic->ic_opmode == IEEE80211_M_IBSS)
2030 		config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2031 	if (ifp->if_flags & IFF_PROMISC)
2032 		config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2033 	config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2034 	config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2035 	DPRINTF(("Setting adapter configuration 0x%08x\n", config.flags));
2036 	error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2037 	if (error != 0)
2038 		return error;
2039 
2040 	data = htole32(0x3); /* 1, 2 */
2041 	DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2042 	error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2043 	if (error != 0)
2044 		return error;
2045 
2046 	data = htole32(0xf); /* 1, 2, 5.5, 11 */
2047 	DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2048 	error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2049 	if (error != 0)
2050 		return error;
2051 
2052 	data = htole32(IPW_POWER_MODE_CAM);
2053 	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2054 	error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2055 	if (error != 0)
2056 		return error;
2057 
2058 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2059 		data = htole32(32); /* default value */
2060 		DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2061 		error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2062 		    sizeof data);
2063 		if (error != 0)
2064 			return error;
2065 	}
2066 
2067 	data = htole32(ic->ic_rtsthreshold);
2068 	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2069 	error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2070 	if (error != 0)
2071 		return error;
2072 
2073 	data = htole32(ic->ic_fragthreshold);
2074 	DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2075 	error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2076 	if (error != 0)
2077 		return error;
2078 
2079 #ifdef IPW_DEBUG
2080 	if (ipw_debug > 0) {
2081 		printf("Setting ESSID to ");
2082 		ieee80211_print_essid(ic->ic_des_essid, ic->ic_des_esslen);
2083 		printf("\n");
2084 	}
2085 #endif
2086 	error = ipw_cmd(sc, IPW_CMD_SET_ESSID, ic->ic_des_essid,
2087 	    ic->ic_des_esslen);
2088 	if (error != 0)
2089 		return error;
2090 
2091 	/* no mandatory BSSID */
2092 	DPRINTF(("Setting mandatory BSSID to null\n"));
2093 	error = ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2094 	if (error != 0)
2095 		return error;
2096 
2097 	if (ic->ic_flags & IEEE80211_F_DESBSSID) {
2098 		DPRINTF(("Setting desired BSSID to %s\n",
2099 		    ether_sprintf(ic->ic_des_bssid)));
2100 		error = ipw_cmd(sc, IPW_CMD_SET_DESIRED_BSSID,
2101 		    ic->ic_des_bssid, IEEE80211_ADDR_LEN);
2102 		if (error != 0)
2103 			return error;
2104 	}
2105 
2106 	(void)memset(&security, 0, sizeof(security));
2107 	security.authmode = (ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED) ?
2108 	    IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2109 	security.ciphers = htole32(IPW_CIPHER_NONE);
2110 	DPRINTF(("Setting authmode to %u\n", security.authmode));
2111 	error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFORMATION, &security,
2112 	    sizeof security);
2113 	if (error != 0)
2114 		return error;
2115 
2116 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2117 		k = ic->ic_crypto.cs_nw_keys;
2118 		for (i = 0; i < IEEE80211_WEP_NKID; i++, k++) {
2119 			if (k->wk_keylen == 0)
2120 				continue;
2121 
2122 			wepkey.idx = i;
2123 			wepkey.len = k->wk_keylen;
2124 			memset(wepkey.key, 0, sizeof(wepkey.key));
2125 			memcpy(wepkey.key, k->wk_key, k->wk_keylen);
2126 			DPRINTF(("Setting wep key index %u len %u\n",
2127 			    wepkey.idx, wepkey.len));
2128 			error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2129 			    sizeof wepkey);
2130 			if (error != 0)
2131 				return error;
2132 		}
2133 
2134 		data = htole32(ic->ic_crypto.cs_def_txkey);
2135 		DPRINTF(("Setting tx key index to %u\n", le32toh(data)));
2136 		error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2137 		    sizeof data);
2138 		if (error != 0)
2139 			return error;
2140 	}
2141 
2142 	data = htole32((sc->sc_ic.ic_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2143 	DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2144 	error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2145 	if (error != 0)
2146 		return error;
2147 
2148 #if 0
2149 	struct ipw_wpa_ie ie;
2150 
2151 	memset(&ie, 0 sizeof(ie));
2152 	ie.len = htole32(sizeof (struct ieee80211_ie_wpa));
2153 	DPRINTF(("Setting wpa ie\n"));
2154 	error = ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &ie, sizeof ie);
2155 	if (error != 0)
2156 		return error;
2157 #endif
2158 
2159 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2160 		data = htole32(ic->ic_bintval);
2161 		DPRINTF(("Setting beacon interval to %u\n", le32toh(data)));
2162 		error = ipw_cmd(sc, IPW_CMD_SET_BEACON_INTERVAL, &data,
2163 		    sizeof data);
2164 		if (error != 0)
2165 			return error;
2166 	}
2167 
2168 	options.flags = 0;
2169 	options.channels = htole32(0x3fff); /* scan channels 1-14 */
2170 	DPRINTF(("Setting scan options to 0x%x\n", le32toh(options.flags)));
2171 	error = ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &options, sizeof options);
2172 	if (error != 0)
2173 		return error;
2174 
2175 	/* finally, enable adapter (start scanning for an access point) */
2176 	DPRINTF(("Enabling adapter\n"));
2177 	return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
2178 }
2179 
2180 static int
2181 ipw_init(struct ifnet *ifp)
2182 {
2183 	struct ipw_softc *sc = ifp->if_softc;
2184 	struct ipw_firmware *fw = &sc->fw;
2185 
2186 	if (!(sc->flags & IPW_FLAG_FW_CACHED)) {
2187 		if (ipw_cache_firmware(sc) != 0) {
2188 			aprint_error_dev(sc->sc_dev,
2189 			    "could not cache the firmware (%s)\n",
2190 			    sc->sc_fwname);
2191 			goto fail;
2192 		}
2193 	}
2194 
2195 	ipw_stop(ifp, 0);
2196 
2197 	if (ipw_reset(sc) != 0) {
2198 		aprint_error_dev(sc->sc_dev, "could not reset adapter\n");
2199 		goto fail;
2200 	}
2201 
2202 	if (ipw_load_ucode(sc, fw->ucode, fw->ucode_size) != 0) {
2203 		aprint_error_dev(sc->sc_dev, "could not load microcode\n");
2204 		goto fail;
2205 	}
2206 
2207 	ipw_stop_master(sc);
2208 
2209 	/*
2210 	 * Setup tx, rx and status rings.
2211 	 */
2212 	sc->txold = IPW_NTBD - 1;
2213 	sc->txcur = 0;
2214 	sc->txfree = IPW_NTBD - 2;
2215 	sc->rxcur = IPW_NRBD - 1;
2216 
2217 	CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_map->dm_segs[0].ds_addr);
2218 	CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
2219 	CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
2220 	CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2221 
2222 	CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_map->dm_segs[0].ds_addr);
2223 	CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
2224 	CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
2225 	CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2226 
2227 	CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_map->dm_segs[0].ds_addr);
2228 
2229 	if (ipw_load_firmware(sc, fw->main, fw->main_size) != 0) {
2230 		aprint_error_dev(sc->sc_dev, "could not load firmware\n");
2231 		goto fail;
2232 	}
2233 
2234 	sc->flags |= IPW_FLAG_FW_INITED;
2235 
2236 	/* retrieve information tables base addresses */
2237 	sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2238 	sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2239 
2240 	ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2241 
2242 	if (ipw_config(sc) != 0) {
2243 		aprint_error_dev(sc->sc_dev, "device configuration failed\n");
2244 		goto fail;
2245 	}
2246 
2247 	ifp->if_flags &= ~IFF_OACTIVE;
2248 	ifp->if_flags |= IFF_RUNNING;
2249 
2250 	return 0;
2251 
2252 fail:	ifp->if_flags &= ~IFF_UP;
2253 	ipw_stop(ifp, 0);
2254 
2255 	return EIO;
2256 }
2257 
2258 static void
2259 ipw_stop(struct ifnet *ifp, int disable)
2260 {
2261 	struct ipw_softc *sc = ifp->if_softc;
2262 	struct ieee80211com *ic = &sc->sc_ic;
2263 	int i;
2264 
2265 	ipw_stop_master(sc);
2266 
2267 	CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2268 
2269 	/*
2270 	 * Release tx buffers.
2271 	 */
2272 	for (i = 0; i < IPW_NTBD; i++)
2273 		ipw_release_sbd(sc, &sc->stbd_list[i]);
2274 
2275 	sc->sc_tx_timer = 0;
2276 	ifp->if_timer = 0;
2277 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2278 
2279 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2280 }
2281 
2282 static void
2283 ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2284     bus_size_t count)
2285 {
2286 	for (; count > 0; offset++, datap++, count--) {
2287 		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2288 		*datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2289 	}
2290 }
2291 
2292 static void
2293 ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2294     bus_size_t count)
2295 {
2296 	for (; count > 0; offset++, datap++, count--) {
2297 		CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2298 		CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2299 	}
2300 }
2301 
2302 SYSCTL_SETUP(sysctl_hw_ipw_accept_eula_setup, "sysctl hw.ipw.accept_eula")
2303 {
2304 	const struct sysctlnode *rnode;
2305 	const struct sysctlnode *cnode;
2306 
2307 	sysctl_createv(NULL, 0, NULL, &rnode,
2308 		CTLFLAG_PERMANENT,
2309 		CTLTYPE_NODE, "ipw",
2310 		NULL,
2311 		NULL, 0,
2312 		NULL, 0,
2313 		CTL_HW, CTL_CREATE, CTL_EOL);
2314 
2315 	sysctl_createv(NULL, 0, &rnode, &cnode,
2316 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
2317 		CTLTYPE_INT, "accept_eula",
2318 		SYSCTL_DESCR("Accept Intel EULA and permit use of ipw(4) firmware"),
2319 		NULL, 0,
2320 		&ipw_accept_eula, sizeof(ipw_accept_eula),
2321 		CTL_CREATE, CTL_EOL);
2322 }
2323