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