xref: /openbsd-src/sys/dev/pci/if_iwi.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /*	$OpenBSD: if_iwi.c,v 1.132 2016/04/13 10:34:32 mpi Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004-2008
5  *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * Driver for Intel PRO/Wireless 2200BG/2915ABG 802.11 network adapters.
22  */
23 
24 #include "bpfilter.h"
25 
26 #include <sys/param.h>
27 #include <sys/sockio.h>
28 #include <sys/mbuf.h>
29 #include <sys/kernel.h>
30 #include <sys/socket.h>
31 #include <sys/systm.h>
32 #include <sys/conf.h>
33 #include <sys/device.h>
34 #include <sys/task.h>
35 #include <sys/endian.h>
36 
37 #include <machine/bus.h>
38 #include <machine/intr.h>
39 
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcidevs.h>
43 
44 #if NBPFILTER > 0
45 #include <net/bpf.h>
46 #endif
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/if_ether.h>
53 
54 #include <net80211/ieee80211_var.h>
55 #include <net80211/ieee80211_radiotap.h>
56 
57 #include <dev/pci/if_iwireg.h>
58 #include <dev/pci/if_iwivar.h>
59 
60 const struct pci_matchid iwi_devices[] = {
61 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2200BG },
62 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2225BG },
63 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 },
64 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2 }
65 };
66 
67 int		iwi_match(struct device *, void *, void *);
68 void		iwi_attach(struct device *, struct device *, void *);
69 int		iwi_activate(struct device *, int);
70 void		iwi_wakeup(struct iwi_softc *);
71 void		iwi_init_task(void *);
72 int		iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
73 void		iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
74 void		iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
75 int		iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
76 		    int);
77 void		iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
78 void		iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
79 int		iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
80 void		iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
81 void		iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
82 int		iwi_media_change(struct ifnet *);
83 void		iwi_media_status(struct ifnet *, struct ifmediareq *);
84 uint16_t	iwi_read_prom_word(struct iwi_softc *, uint8_t);
85 int		iwi_find_txnode(struct iwi_softc *, const uint8_t *);
86 int		iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
87 uint8_t		iwi_rate(int);
88 void		iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *,
89 		    struct iwi_frame *);
90 void		iwi_notification_intr(struct iwi_softc *, struct iwi_rx_data *,
91 		    struct iwi_notif *);
92 void		iwi_rx_intr(struct iwi_softc *);
93 void		iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
94 int		iwi_intr(void *);
95 int		iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int);
96 int		iwi_send_mgmt(struct ieee80211com *, struct ieee80211_node *,
97 		    int, int, int);
98 int		iwi_tx_start(struct ifnet *, struct mbuf *,
99 		    struct ieee80211_node *);
100 void		iwi_start(struct ifnet *);
101 void		iwi_watchdog(struct ifnet *);
102 int		iwi_ioctl(struct ifnet *, u_long, caddr_t);
103 void		iwi_stop_master(struct iwi_softc *);
104 int		iwi_reset(struct iwi_softc *);
105 int		iwi_load_ucode(struct iwi_softc *, const char *, int);
106 int		iwi_load_firmware(struct iwi_softc *, const char *, int);
107 int		iwi_config(struct iwi_softc *);
108 void		iwi_update_edca(struct ieee80211com *);
109 int		iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
110 int		iwi_scan(struct iwi_softc *);
111 int		iwi_auth_and_assoc(struct iwi_softc *);
112 int		iwi_init(struct ifnet *);
113 void		iwi_stop(struct ifnet *, int);
114 
115 static __inline uint8_t
116 MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
117 {
118 	CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
119 	return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
120 }
121 
122 static __inline uint32_t
123 MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
124 {
125 	CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
126 	return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
127 }
128 
129 #ifdef IWI_DEBUG
130 #define DPRINTF(x)	do { if (iwi_debug > 0) printf x; } while (0)
131 #define DPRINTFN(n, x)	do { if (iwi_debug >= (n)) printf x; } while (0)
132 int iwi_debug = 0;
133 #else
134 #define DPRINTF(x)
135 #define DPRINTFN(n, x)
136 #endif
137 
138 struct cfattach iwi_ca = {
139 	sizeof (struct iwi_softc), iwi_match, iwi_attach, NULL,
140 	iwi_activate
141 };
142 
143 int
144 iwi_match(struct device *parent, void *match, void *aux)
145 {
146 	return pci_matchbyid((struct pci_attach_args *)aux, iwi_devices,
147 	    nitems(iwi_devices));
148 }
149 
150 /* Base Address Register */
151 #define IWI_PCI_BAR0	0x10
152 
153 void
154 iwi_attach(struct device *parent, struct device *self, void *aux)
155 {
156 	struct iwi_softc *sc = (struct iwi_softc *)self;
157 	struct ieee80211com *ic = &sc->sc_ic;
158 	struct ifnet *ifp = &ic->ic_if;
159 	struct pci_attach_args *pa = aux;
160 	const char *intrstr;
161 	bus_space_tag_t memt;
162 	bus_space_handle_t memh;
163 	pci_intr_handle_t ih;
164 	pcireg_t data;
165 	uint16_t val;
166 	int error, ac, i;
167 
168 	sc->sc_pct = pa->pa_pc;
169 	sc->sc_pcitag = pa->pa_tag;
170 
171 	/* clear device specific PCI configuration register 0x41 */
172 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
173 	data &= ~0x0000ff00;
174 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
175 
176 	/* map the register window */
177 	error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
178 	    PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz, 0);
179 	if (error != 0) {
180 		printf(": can't map mem space\n");
181 		return;
182 	}
183 
184 	sc->sc_st = memt;
185 	sc->sc_sh = memh;
186 	sc->sc_dmat = pa->pa_dmat;
187 
188 	if (pci_intr_map(pa, &ih) != 0) {
189 		printf(": can't map interrupt\n");
190 		return;
191 	}
192 
193 	intrstr = pci_intr_string(sc->sc_pct, ih);
194 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwi_intr, sc,
195 	    sc->sc_dev.dv_xname);
196 	if (sc->sc_ih == NULL) {
197 		printf(": can't establish interrupt");
198 		if (intrstr != NULL)
199 			printf(" at %s", intrstr);
200 		printf("\n");
201 		return;
202 	}
203 	printf(": %s", intrstr);
204 
205 	if (iwi_reset(sc) != 0) {
206 		printf(": could not reset adapter\n");
207 		return;
208 	}
209 
210 	/*
211 	 * Allocate rings.
212 	 */
213 	if (iwi_alloc_cmd_ring(sc, &sc->cmdq) != 0) {
214 		printf(": could not allocate Cmd ring\n");
215 		return;
216 	}
217 	for (ac = 0; ac < EDCA_NUM_AC; ac++) {
218 		if (iwi_alloc_tx_ring(sc, &sc->txq[ac], ac) != 0) {
219 			printf(": could not allocate Tx ring %d\n", ac);
220 			goto fail;
221 		}
222 	}
223 	if (iwi_alloc_rx_ring(sc, &sc->rxq) != 0) {
224 		printf(": could not allocate Rx ring\n");
225 		goto fail;
226 	}
227 
228 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
229 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
230 	ic->ic_state = IEEE80211_S_INIT;
231 
232 	/* set device capabilities */
233 	ic->ic_caps =
234 #ifndef IEEE80211_STA_ONLY
235 	    IEEE80211_C_IBSS |		/* IBSS mode supported */
236 #endif
237 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
238 	    IEEE80211_C_TXPMGT |	/* tx power management */
239 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
240 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
241 	    IEEE80211_C_WEP |		/* s/w WEP */
242 	    IEEE80211_C_RSN |		/* WPA/RSN supported */
243 	    IEEE80211_C_SCANALL;	/* h/w scanning */
244 
245 	/* read MAC address from EEPROM */
246 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
247 	ic->ic_myaddr[0] = val & 0xff;
248 	ic->ic_myaddr[1] = val >> 8;
249 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
250 	ic->ic_myaddr[2] = val & 0xff;
251 	ic->ic_myaddr[3] = val >> 8;
252 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
253 	ic->ic_myaddr[4] = val & 0xff;
254 	ic->ic_myaddr[5] = val >> 8;
255 
256 	printf(", address %s\n", ether_sprintf(ic->ic_myaddr));
257 
258 	if (PCI_PRODUCT(pa->pa_id) >= PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1) {
259 		/* set supported .11a rates */
260 		ic->ic_sup_rates[IEEE80211_MODE_11A] =
261 		    ieee80211_std_rateset_11a;
262 
263 		/* set supported .11a channels */
264 		for (i = 36; i <= 64; i += 4) {
265 			ic->ic_channels[i].ic_freq =
266 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
267 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
268 		}
269 		for (i = 149; i <= 165; i += 4) {
270 			ic->ic_channels[i].ic_freq =
271 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
272 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
273 		}
274 	}
275 
276 	/* set supported .11b and .11g rates */
277 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
278 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
279 
280 	/* set supported .11b and .11g channels (1 through 14) */
281 	for (i = 1; i <= 14; i++) {
282 		ic->ic_channels[i].ic_freq =
283 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
284 		ic->ic_channels[i].ic_flags =
285 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
286 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
287 	}
288 
289 	/* IBSS channel undefined for now */
290 	ic->ic_ibss_chan = &ic->ic_channels[0];
291 
292 	ifp->if_softc = sc;
293 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
294 	ifp->if_ioctl = iwi_ioctl;
295 	ifp->if_start = iwi_start;
296 	ifp->if_watchdog = iwi_watchdog;
297 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
298 
299 	if_attach(ifp);
300 	ieee80211_ifattach(ifp);
301 	/* override state transition machine */
302 	sc->sc_newstate = ic->ic_newstate;
303 	ic->ic_newstate = iwi_newstate;
304 	ic->ic_send_mgmt = iwi_send_mgmt;
305 	ieee80211_media_init(ifp, iwi_media_change, iwi_media_status);
306 
307 #if NBPFILTER > 0
308 	bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
309 	    sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
310 
311 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
312 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
313 	sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
314 
315 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
316 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
317 	sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
318 #endif
319 
320 	task_set(&sc->init_task, iwi_init_task, sc);
321 	return;
322 
323 fail:	while (--ac >= 0)
324 		iwi_free_tx_ring(sc, &sc->txq[ac]);
325 	iwi_free_cmd_ring(sc, &sc->cmdq);
326 }
327 
328 int
329 iwi_activate(struct device *self, int act)
330 {
331 	struct iwi_softc *sc = (struct iwi_softc *)self;
332 	struct ifnet *ifp = &sc->sc_ic.ic_if;
333 
334 	switch (act) {
335 	case DVACT_SUSPEND:
336 		if (ifp->if_flags & IFF_RUNNING)
337 			iwi_stop(ifp, 0);
338 		break;
339 	case DVACT_WAKEUP:
340 		iwi_wakeup(sc);
341 		break;
342 	}
343 
344 	return 0;
345 }
346 
347 void
348 iwi_wakeup(struct iwi_softc *sc)
349 {
350 	pcireg_t data;
351 
352 	/* clear device specific PCI configuration register 0x41 */
353 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
354 	data &= ~0x0000ff00;
355 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
356 
357 	iwi_init_task(sc);
358 }
359 
360 void
361 iwi_init_task(void *arg1)
362 {
363 	struct iwi_softc *sc = arg1;
364 	struct ifnet *ifp = &sc->sc_ic.ic_if;
365 	int s;
366 
367 	s = splnet();
368 	while (sc->sc_flags & IWI_FLAG_BUSY)
369 		tsleep(&sc->sc_flags, 0, "iwipwr", 0);
370 	sc->sc_flags |= IWI_FLAG_BUSY;
371 
372 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_UP)
373 		iwi_init(ifp);
374 
375 	sc->sc_flags &= ~IWI_FLAG_BUSY;
376 	wakeup(&sc->sc_flags);
377 	splx(s);
378 }
379 
380 int
381 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
382 {
383 	int nsegs, error;
384 
385 	ring->queued = 0;
386 	ring->cur = ring->next = 0;
387 
388 	error = bus_dmamap_create(sc->sc_dmat,
389 	    sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT, 1,
390 	    sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT, 0,
391 	    BUS_DMA_NOWAIT, &ring->map);
392 	if (error != 0) {
393 		printf("%s: could not create cmd ring DMA map\n",
394 		    sc->sc_dev.dv_xname);
395 		goto fail;
396 	}
397 
398 	error = bus_dmamem_alloc(sc->sc_dmat,
399 	    sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT, PAGE_SIZE, 0,
400 	    &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
401 	if (error != 0) {
402 		printf("%s: could not allocate cmd ring DMA memory\n",
403 		    sc->sc_dev.dv_xname);
404 		goto fail;
405 	}
406 
407 	error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
408 	    sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT,
409 	    (caddr_t *)&ring->desc, BUS_DMA_NOWAIT);
410 	if (error != 0) {
411 		printf("%s: can't map cmd ring DMA memory\n",
412 		    sc->sc_dev.dv_xname);
413 		goto fail;
414 	}
415 
416 	error = bus_dmamap_load(sc->sc_dmat, ring->map, ring->desc,
417 	    sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT, NULL,
418 	    BUS_DMA_NOWAIT);
419 	if (error != 0) {
420 		printf("%s: could not load cmd ring DMA map\n",
421 		    sc->sc_dev.dv_xname);
422 		goto fail;
423 	}
424 
425 	return 0;
426 
427 fail:	iwi_free_cmd_ring(sc, ring);
428 	return error;
429 }
430 
431 void
432 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
433 {
434 	ring->queued = 0;
435 	ring->cur = ring->next = 0;
436 }
437 
438 void
439 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
440 {
441 	if (ring->map != NULL) {
442 		if (ring->desc != NULL) {
443 			bus_dmamap_unload(sc->sc_dmat, ring->map);
444 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
445 			    sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_COUNT);
446 			bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
447 		}
448 		bus_dmamap_destroy(sc->sc_dmat, ring->map);
449 	}
450 }
451 
452 int
453 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int ac)
454 {
455 	struct iwi_tx_data *data;
456 	int i, nsegs, error;
457 
458 	ring->queued = 0;
459 	ring->cur = ring->next = 0;
460 	ring->csr_ridx = IWI_CSR_TX_RIDX(ac);
461 	ring->csr_widx = IWI_CSR_TX_WIDX(ac);
462 
463 	error = bus_dmamap_create(sc->sc_dmat,
464 	    sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT, 1,
465 	    sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT, 0, BUS_DMA_NOWAIT,
466 	    &ring->map);
467 	if (error != 0) {
468 		printf("%s: could not create tx ring DMA map\n",
469 		    sc->sc_dev.dv_xname);
470 		goto fail;
471 	}
472 
473 	error = bus_dmamem_alloc(sc->sc_dmat,
474 	    sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT, PAGE_SIZE, 0,
475 	    &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
476 	if (error != 0) {
477 		printf("%s: could not allocate tx ring DMA memory\n",
478 		    sc->sc_dev.dv_xname);
479 		goto fail;
480 	}
481 
482 	error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
483 	    sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT,
484 	    (caddr_t *)&ring->desc, BUS_DMA_NOWAIT);
485 	if (error != 0) {
486 		printf("%s: can't map tx ring DMA memory\n",
487 		    sc->sc_dev.dv_xname);
488 		goto fail;
489 	}
490 
491 	error = bus_dmamap_load(sc->sc_dmat, ring->map, ring->desc,
492 	    sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT, NULL,
493 	    BUS_DMA_NOWAIT);
494 	if (error != 0) {
495 		printf("%s: could not load tx ring DMA map\n",
496 		    sc->sc_dev.dv_xname);
497 		goto fail;
498 	}
499 
500 	for (i = 0; i < IWI_TX_RING_COUNT; i++) {
501 		data = &ring->data[i];
502 
503 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
504 		    IWI_MAX_SCATTER, MCLBYTES, 0, BUS_DMA_NOWAIT, &data->map);
505 		if (error != 0) {
506 			printf("%s: could not create tx buf DMA map\n",
507 			    sc->sc_dev.dv_xname);
508 			goto fail;
509 		}
510 	}
511 
512 	return 0;
513 
514 fail:	iwi_free_tx_ring(sc, ring);
515 	return error;
516 }
517 
518 void
519 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
520 {
521 	struct iwi_tx_data *data;
522 	int i;
523 
524 	for (i = 0; i < IWI_TX_RING_COUNT; i++) {
525 		data = &ring->data[i];
526 
527 		if (data->m != NULL) {
528 			bus_dmamap_unload(sc->sc_dmat, data->map);
529 			m_freem(data->m);
530 			data->m = NULL;
531 		}
532 	}
533 
534 	ring->queued = 0;
535 	ring->cur = ring->next = 0;
536 }
537 
538 void
539 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
540 {
541 	struct iwi_tx_data *data;
542 	int i;
543 
544 	if (ring->map != NULL) {
545 		if (ring->desc != NULL) {
546 			bus_dmamap_unload(sc->sc_dmat, ring->map);
547 			bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc,
548 			    sizeof (struct iwi_tx_desc) * IWI_TX_RING_COUNT);
549 			bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
550 		}
551 		bus_dmamap_destroy(sc->sc_dmat, ring->map);
552 	}
553 
554 	for (i = 0; i < IWI_TX_RING_COUNT; i++) {
555 		data = &ring->data[i];
556 
557 		if (data->m != NULL) {
558 			bus_dmamap_unload(sc->sc_dmat, data->map);
559 			m_freem(data->m);
560 		}
561 		bus_dmamap_destroy(sc->sc_dmat, data->map);
562 	}
563 }
564 
565 int
566 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
567 {
568 	struct iwi_rx_data *data;
569 	int i, error;
570 
571 	ring->cur = 0;
572 
573 	for (i = 0; i < IWI_RX_RING_COUNT; i++) {
574 		data = &sc->rxq.data[i];
575 
576 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
577 		    0, BUS_DMA_NOWAIT, &data->map);
578 		if (error != 0) {
579 			printf("%s: could not create rx buf DMA map\n",
580 			    sc->sc_dev.dv_xname);
581 			goto fail;
582 		}
583 
584 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
585 		if (data->m == NULL) {
586 			printf("%s: could not allocate rx mbuf\n",
587 			    sc->sc_dev.dv_xname);
588 			error = ENOMEM;
589 			goto fail;
590 		}
591 		MCLGET(data->m, M_DONTWAIT);
592 		if (!(data->m->m_flags & M_EXT)) {
593 			m_freem(data->m);
594 			data->m = NULL;
595 			printf("%s: could not allocate rx mbuf cluster\n",
596 			    sc->sc_dev.dv_xname);
597 			error = ENOMEM;
598 			goto fail;
599 		}
600 
601 		error = bus_dmamap_load(sc->sc_dmat, data->map,
602 		    mtod(data->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
603 		if (error != 0) {
604 			printf("%s: could not load rx buf DMA map\n",
605 			    sc->sc_dev.dv_xname);
606 			goto fail;
607 		}
608 
609 		data->reg = IWI_CSR_RX_BASE + i * 4;
610 	}
611 
612 	return 0;
613 
614 fail:	iwi_free_rx_ring(sc, ring);
615 	return error;
616 }
617 
618 void
619 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
620 {
621 	ring->cur = 0;
622 }
623 
624 void
625 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
626 {
627 	struct iwi_rx_data *data;
628 	int i;
629 
630 	for (i = 0; i < IWI_RX_RING_COUNT; i++) {
631 		data = &sc->rxq.data[i];
632 
633 		if (data->m != NULL) {
634 			bus_dmamap_unload(sc->sc_dmat, data->map);
635 			m_freem(data->m);
636 		}
637 		bus_dmamap_destroy(sc->sc_dmat, data->map);
638 	}
639 }
640 
641 int
642 iwi_media_change(struct ifnet *ifp)
643 {
644 	int error;
645 
646 	error = ieee80211_media_change(ifp);
647 	if (error != ENETRESET)
648 		return error;
649 
650 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
651 		iwi_init(ifp);
652 
653 	return 0;
654 }
655 
656 void
657 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
658 {
659 	struct iwi_softc *sc = ifp->if_softc;
660 	struct ieee80211com *ic = &sc->sc_ic;
661 	uint32_t val;
662 	int rate;
663 
664 	imr->ifm_status = IFM_AVALID;
665 	imr->ifm_active = IFM_IEEE80211;
666 	if (ic->ic_state == IEEE80211_S_RUN)
667 		imr->ifm_status |= IFM_ACTIVE;
668 
669 	/* read current transmission rate from adapter */
670 	val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
671 	/* convert PLCP signal to 802.11 rate */
672 	rate = iwi_rate(val);
673 
674 	imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
675 	switch (ic->ic_opmode) {
676 	case IEEE80211_M_STA:
677 		break;
678 #ifndef IEEE80211_STA_ONLY
679 	case IEEE80211_M_IBSS:
680 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
681 		break;
682 #endif
683 	case IEEE80211_M_MONITOR:
684 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
685 		break;
686 	default:
687 		/* should not get there */
688 		break;
689 	}
690 }
691 
692 #ifndef IEEE80211_STA_ONLY
693 /*
694  * This is only used for IBSS mode where the firmware expect an index to an
695  * internal node table instead of a destination address.
696  */
697 int
698 iwi_find_txnode(struct iwi_softc *sc, const uint8_t *macaddr)
699 {
700 	struct iwi_node node;
701 	int i;
702 
703 	for (i = 0; i < sc->nsta; i++)
704 		if (IEEE80211_ADDR_EQ(sc->sta[i], macaddr))
705 			return i;	/* already existing node */
706 
707 	if (i == IWI_MAX_NODE)
708 		return -1;	/* no place left in neighbor table */
709 
710 	/* save this new node in our softc table */
711 	IEEE80211_ADDR_COPY(sc->sta[i], macaddr);
712 	sc->nsta = i;
713 
714 	/* write node information into NIC memory */
715 	bzero(&node, sizeof node);
716 	IEEE80211_ADDR_COPY(node.bssid, macaddr);
717 
718 	CSR_WRITE_REGION_1(sc, IWI_CSR_NODE_BASE + i * sizeof node,
719 	    (uint8_t *)&node, sizeof node);
720 
721 	return i;
722 }
723 #endif
724 
725 int
726 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
727 {
728 	struct iwi_softc *sc = ic->ic_softc;
729 	enum ieee80211_state ostate;
730 	uint32_t tmp;
731 
732 	ostate = ic->ic_state;
733 
734 	switch (nstate) {
735 	case IEEE80211_S_SCAN:
736 		iwi_scan(sc);
737 		break;
738 
739 	case IEEE80211_S_AUTH:
740 		iwi_auth_and_assoc(sc);
741 		break;
742 
743 	case IEEE80211_S_RUN:
744 #ifndef IEEE80211_STA_ONLY
745 		if (ic->ic_opmode == IEEE80211_M_IBSS) {
746 			sc->nsta = 0;	/* flush IBSS nodes */
747 			ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
748 		} else
749 #endif
750 		if (ic->ic_opmode == IEEE80211_M_MONITOR)
751 			iwi_set_chan(sc, ic->ic_ibss_chan);
752 
753 		/* assoc led on */
754 		tmp = MEM_READ_4(sc, IWI_MEM_EVENT_CTL) & IWI_LED_MASK;
755 		MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, tmp | IWI_LED_ASSOC);
756 		break;
757 
758 	case IEEE80211_S_INIT:
759 		if (ostate != IEEE80211_S_RUN)
760 			break;
761 
762 		/* assoc led off */
763 		tmp = MEM_READ_4(sc, IWI_MEM_EVENT_CTL) & IWI_LED_MASK;
764 		MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, tmp & ~IWI_LED_ASSOC);
765 		break;
766 
767 	case IEEE80211_S_ASSOC:
768 		break;
769 	}
770 
771 	ic->ic_state = nstate;
772 	return 0;
773 }
774 
775 /*
776  * Read 16 bits at address 'addr' from the serial EEPROM.
777  * DON'T PLAY WITH THIS CODE UNLESS YOU KNOW *EXACTLY* WHAT YOU'RE DOING!
778  */
779 uint16_t
780 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
781 {
782 	uint32_t tmp;
783 	uint16_t val;
784 	int n;
785 
786 	/* clock C once before the first command */
787 	IWI_EEPROM_CTL(sc, 0);
788 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
789 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
790 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
791 
792 	/* write start bit (1) */
793 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
794 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
795 
796 	/* write READ opcode (10) */
797 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
798 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
799 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
800 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
801 
802 	/* write address A7-A0 */
803 	for (n = 7; n >= 0; n--) {
804 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
805 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
806 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
807 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
808 	}
809 
810 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
811 
812 	/* read data Q15-Q0 */
813 	val = 0;
814 	for (n = 15; n >= 0; n--) {
815 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
816 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
817 		tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
818 		val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
819 	}
820 
821 	IWI_EEPROM_CTL(sc, 0);
822 
823 	/* clear Chip Select and clock C */
824 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
825 	IWI_EEPROM_CTL(sc, 0);
826 	IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
827 
828 	return val;
829 }
830 
831 uint8_t
832 iwi_rate(int plcp)
833 {
834 	switch (plcp) {
835 	/* CCK rates (values are device-dependent) */
836 	case  10:	return 2;
837 	case  20:	return 4;
838 	case  55:	return 11;
839 	case 110:	return 22;
840 
841 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
842 	case 0xd:	return 12;
843 	case 0xf:	return 18;
844 	case 0x5:	return 24;
845 	case 0x7:	return 36;
846 	case 0x9:	return 48;
847 	case 0xb:	return 72;
848 	case 0x1:	return 96;
849 	case 0x3:	return 108;
850 
851 	/* unknown rate: should not happen */
852 	default:	return 0;
853 	}
854 }
855 
856 void
857 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data,
858     struct iwi_frame *frame)
859 {
860 	struct ieee80211com *ic = &sc->sc_ic;
861 	struct ifnet *ifp = &ic->ic_if;
862 	struct mbuf *mnew, *m;
863 	struct ieee80211_frame *wh;
864 	struct ieee80211_rxinfo rxi;
865 	struct ieee80211_node *ni;
866 	int error;
867 
868 	DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
869 	    letoh16(frame->len), frame->chan, frame->rssi_dbm));
870 
871 	if (letoh16(frame->len) < sizeof (struct ieee80211_frame_min) ||
872 	    letoh16(frame->len) > MCLBYTES) {
873 		DPRINTF(("%s: bad frame length\n", sc->sc_dev.dv_xname));
874 		ifp->if_ierrors++;
875 		return;
876 	}
877 
878 	/*
879 	 * Try to allocate a new mbuf for this ring element and load it before
880 	 * processing the current mbuf.  If the ring element cannot be loaded,
881 	 * drop the received packet and reuse the old mbuf.  In the unlikely
882 	 * case that the old mbuf can't be reloaded either, explicitly panic.
883 	 */
884 	MGETHDR(mnew, M_DONTWAIT, MT_DATA);
885 	if (mnew == NULL) {
886 		ifp->if_ierrors++;
887 		return;
888 	}
889 	MCLGET(mnew, M_DONTWAIT);
890 	if (!(mnew->m_flags & M_EXT)) {
891 		m_freem(mnew);
892 		ifp->if_ierrors++;
893 		return;
894 	}
895 
896 	bus_dmamap_unload(sc->sc_dmat, data->map);
897 
898 	error = bus_dmamap_load(sc->sc_dmat, data->map, mtod(mnew, void *),
899 	    MCLBYTES, NULL, BUS_DMA_NOWAIT);
900 	if (error != 0) {
901 		m_freem(mnew);
902 
903 		/* try to reload the old mbuf */
904 		error = bus_dmamap_load(sc->sc_dmat, data->map,
905 		    mtod(data->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
906 		if (error != 0) {
907 			/* very unlikely that it will fail... */
908 			panic("%s: could not load old rx mbuf",
909 			    sc->sc_dev.dv_xname);
910 		}
911 		CSR_WRITE_4(sc, data->reg, data->map->dm_segs[0].ds_addr);
912 		ifp->if_ierrors++;
913 		return;
914 	}
915 
916 	m = data->m;
917 	data->m = mnew;
918 	CSR_WRITE_4(sc, data->reg, data->map->dm_segs[0].ds_addr);
919 
920 	/* finalize mbuf */
921 	m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
922 	    sizeof (struct iwi_frame) + letoh16(frame->len);
923 	m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
924 
925 #if NBPFILTER > 0
926 	if (sc->sc_drvbpf != NULL) {
927 		struct mbuf mb;
928 		struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
929 
930 		tap->wr_flags = 0;
931 		tap->wr_rate = iwi_rate(frame->rate);
932 		tap->wr_chan_freq =
933 		    htole16(ic->ic_channels[frame->chan].ic_freq);
934 		tap->wr_chan_flags =
935 		    htole16(ic->ic_channels[frame->chan].ic_flags);
936 		tap->wr_antsignal = frame->signal;
937 		tap->wr_antenna = frame->antenna & 0x3;
938 		if (frame->antenna & 0x40)
939 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
940 
941 		mb.m_data = (caddr_t)tap;
942 		mb.m_len = sc->sc_rxtap_len;
943 		mb.m_next = m;
944 		mb.m_nextpkt = NULL;
945 		mb.m_type = 0;
946 		mb.m_flags = 0;
947 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
948 	}
949 #endif
950 
951 	wh = mtod(m, struct ieee80211_frame *);
952 	ni = ieee80211_find_rxnode(ic, wh);
953 
954 	/* send the frame to the upper layer */
955 	rxi.rxi_flags = 0;
956 	rxi.rxi_rssi = frame->rssi_dbm;
957 	rxi.rxi_tstamp = 0;	/* unused */
958 	ieee80211_input(ifp, m, ni, &rxi);
959 
960 	/* node is no longer needed */
961 	ieee80211_release_node(ic, ni);
962 }
963 
964 void
965 iwi_notification_intr(struct iwi_softc *sc, struct iwi_rx_data *data,
966     struct iwi_notif *notif)
967 {
968 	struct ieee80211com *ic = &sc->sc_ic;
969 	struct ifnet *ifp = &ic->ic_if;
970 
971 	switch (notif->type) {
972 	case IWI_NOTIF_TYPE_SCAN_CHANNEL:
973 	{
974 #ifdef IWI_DEBUG
975 		struct iwi_notif_scan_channel *chan =
976 		    (struct iwi_notif_scan_channel *)(notif + 1);
977 #endif
978 		DPRINTFN(2, ("Scanning channel (%u)\n", chan->nchan));
979 		break;
980 	}
981 	case IWI_NOTIF_TYPE_SCAN_COMPLETE:
982 	{
983 #ifdef IWI_DEBUG
984 		struct iwi_notif_scan_complete *scan =
985 		    (struct iwi_notif_scan_complete *)(notif + 1);
986 #endif
987 		DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
988 		    scan->status));
989 
990 		/* monitor mode uses scan to set the channel ... */
991 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
992 			ieee80211_end_scan(ifp);
993 		else
994 			iwi_set_chan(sc, ic->ic_ibss_chan);
995 		break;
996 	}
997 	case IWI_NOTIF_TYPE_AUTHENTICATION:
998 	{
999 		struct iwi_notif_authentication *auth =
1000 		    (struct iwi_notif_authentication *)(notif + 1);
1001 
1002 		DPRINTFN(2, ("Authentication (%u)\n", auth->state));
1003 
1004 		switch (auth->state) {
1005 		case IWI_AUTHENTICATED:
1006 			ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1007 			break;
1008 
1009 		case IWI_DEAUTHENTICATED:
1010 			break;
1011 
1012 		default:
1013 			printf("%s: unknown authentication state %u\n",
1014 			    sc->sc_dev.dv_xname, auth->state);
1015 		}
1016 		break;
1017 	}
1018 	case IWI_NOTIF_TYPE_ASSOCIATION:
1019 	{
1020 		struct iwi_notif_association *assoc =
1021 		    (struct iwi_notif_association *)(notif + 1);
1022 
1023 		DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
1024 		    assoc->status));
1025 
1026 		switch (assoc->state) {
1027 		case IWI_AUTHENTICATED:
1028 			/* re-association, do nothing */
1029 			break;
1030 
1031 		case IWI_ASSOCIATED:
1032 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1033 			break;
1034 
1035 		case IWI_DEASSOCIATED:
1036 			ieee80211_begin_scan(ifp);
1037 			break;
1038 
1039 		default:
1040 			printf("%s: unknown association state %u\n",
1041 			    sc->sc_dev.dv_xname, assoc->state);
1042 		}
1043 		break;
1044 	}
1045 	case IWI_NOTIF_TYPE_BEACON:
1046 	{
1047 		struct iwi_notif_beacon *beacon =
1048 		    (struct iwi_notif_beacon *)(notif + 1);
1049 
1050 		if (letoh32(beacon->status) == IWI_BEACON_MISSED) {
1051 			/* XXX should roam when too many beacons missed */
1052 			DPRINTFN(2, ("%s: %u beacon(s) missed\n",
1053 			    sc->sc_dev.dv_xname, letoh32(beacon->count)));
1054 		}
1055 		break;
1056 	}
1057 	case IWI_NOTIF_TYPE_BAD_LINK:
1058 		DPRINTFN(2, ("link deterioration detected\n"));
1059 		break;
1060 
1061 	case IWI_NOTIF_TYPE_NOISE:
1062 		DPRINTFN(5, ("Measured noise %u\n",
1063 		    letoh32(*(uint32_t *)(notif + 1)) & 0xff));
1064 		break;
1065 
1066 	default:
1067 		DPRINTFN(5, ("Notification (%u)\n", notif->type));
1068 	}
1069 }
1070 
1071 void
1072 iwi_rx_intr(struct iwi_softc *sc)
1073 {
1074 	struct iwi_rx_data *data;
1075 	struct iwi_hdr *hdr;
1076 	uint32_t hw;
1077 
1078 	hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1079 
1080 	for (; sc->rxq.cur != hw;) {
1081 		data = &sc->rxq.data[sc->rxq.cur];
1082 
1083 		bus_dmamap_sync(sc->sc_dmat, data->map, 0, MCLBYTES,
1084 		    BUS_DMASYNC_POSTREAD);
1085 
1086 		hdr = mtod(data->m, struct iwi_hdr *);
1087 
1088 		switch (hdr->type) {
1089 		case IWI_HDR_TYPE_FRAME:
1090 			iwi_frame_intr(sc, data,
1091 			    (struct iwi_frame *)(hdr + 1));
1092 			break;
1093 
1094 		case IWI_HDR_TYPE_NOTIF:
1095 			iwi_notification_intr(sc, data,
1096 			    (struct iwi_notif *)(hdr + 1));
1097 			break;
1098 
1099 		default:
1100 			printf("%s: unknown hdr type %u\n",
1101 			    sc->sc_dev.dv_xname, hdr->type);
1102 		}
1103 
1104 		sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1105 	}
1106 
1107 	/* tell the firmware what we have processed */
1108 	hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1109 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1110 }
1111 
1112 void
1113 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1114 {
1115 	struct ieee80211com *ic = &sc->sc_ic;
1116 	struct ifnet *ifp = &ic->ic_if;
1117 	struct iwi_tx_data *data;
1118 	uint32_t hw;
1119 
1120 	hw = CSR_READ_4(sc, txq->csr_ridx);
1121 
1122 	for (; txq->next != hw;) {
1123 		data = &txq->data[txq->next];
1124 
1125 		bus_dmamap_unload(sc->sc_dmat, data->map);
1126 		m_freem(data->m);
1127 		data->m = NULL;
1128 		ieee80211_release_node(ic, data->ni);
1129 		data->ni = NULL;
1130 
1131 		ifp->if_opackets++;
1132 
1133 		txq->queued--;
1134 		txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1135 	}
1136 
1137 	sc->sc_tx_timer = 0;
1138 	ifq_clr_oactive(&ifp->if_snd);
1139 	(*ifp->if_start)(ifp);
1140 }
1141 
1142 int
1143 iwi_intr(void *arg)
1144 {
1145 	struct iwi_softc *sc = arg;
1146 	struct ifnet *ifp = &sc->sc_ic.ic_if;
1147 	uint32_t r;
1148 
1149 	if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff)
1150 		return 0;
1151 
1152 	/* disable interrupts */
1153 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1154 
1155 	/* acknowledge interrupts */
1156 	CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1157 
1158 	if (r & IWI_INTR_FATAL_ERROR) {
1159 		printf("%s: fatal firmware error\n", sc->sc_dev.dv_xname);
1160 		iwi_stop(ifp, 1);
1161 		task_add(systq, &sc->init_task);
1162 		return 1;
1163 	}
1164 
1165 	if (r & IWI_INTR_FW_INITED)
1166 		wakeup(sc);
1167 
1168 	if (r & IWI_INTR_RADIO_OFF) {
1169 		DPRINTF(("radio transmitter off\n"));
1170 		ifp->if_flags &= ~IFF_UP;
1171 		iwi_stop(ifp, 1);
1172 		return 1;
1173 	}
1174 
1175 	if (r & IWI_INTR_CMD_DONE) {
1176 		/* kick next pending command if any */
1177 		sc->cmdq.next = (sc->cmdq.next + 1) % IWI_CMD_RING_COUNT;
1178 		if (--sc->cmdq.queued > 0)
1179 			CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.next);
1180 
1181 		wakeup(sc);
1182 	}
1183 
1184 	if (r & IWI_INTR_TX1_DONE)
1185 		iwi_tx_intr(sc, &sc->txq[0]);
1186 
1187 	if (r & IWI_INTR_TX2_DONE)
1188 		iwi_tx_intr(sc, &sc->txq[1]);
1189 
1190 	if (r & IWI_INTR_TX3_DONE)
1191 		iwi_tx_intr(sc, &sc->txq[2]);
1192 
1193 	if (r & IWI_INTR_TX4_DONE)
1194 		iwi_tx_intr(sc, &sc->txq[3]);
1195 
1196 	if (r & IWI_INTR_RX_DONE)
1197 		iwi_rx_intr(sc);
1198 
1199 	/* re-enable interrupts */
1200 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1201 
1202 	return 1;
1203 }
1204 
1205 int
1206 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len, int async)
1207 {
1208 	struct iwi_cmd_desc *desc;
1209 
1210 	desc = &sc->cmdq.desc[sc->cmdq.cur];
1211 	desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1212 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1213 	desc->type = type;
1214 	desc->len = len;
1215 	bcopy(data, desc->data, len);
1216 
1217 	bus_dmamap_sync(sc->sc_dmat, sc->cmdq.map,
1218 	    sc->cmdq.cur * sizeof (struct iwi_cmd_desc),
1219 	    sizeof (struct iwi_cmd_desc), BUS_DMASYNC_PREWRITE);
1220 
1221 	DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1222 	    type, len));
1223 
1224 	sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1225 
1226 	/* don't kick cmd immediately if another async command is pending */
1227 	if (++sc->cmdq.queued == 1) {
1228 		sc->cmdq.next = sc->cmdq.cur;
1229 		CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.next);
1230 	}
1231 
1232 	return async ? 0 : tsleep(sc, PCATCH, "iwicmd", hz);
1233 }
1234 
1235 /* ARGSUSED */
1236 int
1237 iwi_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni, int type,
1238     int arg1, int arg2)
1239 {
1240 	return EOPNOTSUPP;
1241 }
1242 
1243 int
1244 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1245 {
1246 	struct iwi_softc *sc = ifp->if_softc;
1247 	struct ieee80211com *ic = &sc->sc_ic;
1248 	struct ieee80211_frame *wh;
1249 	struct ieee80211_key *k;
1250 	struct iwi_tx_data *data;
1251 	struct iwi_tx_desc *desc;
1252 	struct iwi_tx_ring *txq = &sc->txq[0];
1253 	int hdrlen, error, i, station = 0;
1254 
1255 	wh = mtod(m0, struct ieee80211_frame *);
1256 
1257 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1258 		k = ieee80211_get_txkey(ic, wh, ni);
1259 
1260 		if ((m0 = ieee80211_encrypt(ic, m0, k)) == NULL)
1261 			return ENOBUFS;
1262 
1263 		/* packet header may have moved, reset our local pointer */
1264 		wh = mtod(m0, struct ieee80211_frame *);
1265 	}
1266 
1267 #if NBPFILTER > 0
1268 	if (sc->sc_drvbpf != NULL) {
1269 		struct mbuf mb;
1270 		struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1271 
1272 		tap->wt_flags = 0;
1273 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1274 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1275 
1276 		mb.m_data = (caddr_t)tap;
1277 		mb.m_len = sc->sc_txtap_len;
1278 		mb.m_next = m0;
1279 		mb.m_nextpkt = NULL;
1280 		mb.m_type = 0;
1281 		mb.m_flags = 0;
1282 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
1283 	}
1284 #endif
1285 
1286 	data = &txq->data[txq->cur];
1287 	desc = &txq->desc[txq->cur];
1288 
1289 	/* copy and trim IEEE802.11 header */
1290 	hdrlen = ieee80211_get_hdrlen(wh);
1291 	bcopy(wh, &desc->wh, hdrlen);
1292 	m_adj(m0, hdrlen);
1293 
1294 #ifndef IEEE80211_STA_ONLY
1295 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
1296 		station = iwi_find_txnode(sc, desc->wh.i_addr1);
1297 		if (station == -1) {
1298 			m_freem(m0);
1299 			ieee80211_release_node(ic, ni);
1300 			ifp->if_oerrors++;
1301 			return 0;
1302 		}
1303 	}
1304 #endif
1305 
1306 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1307 	    BUS_DMA_NOWAIT);
1308 	if (error != 0 && error != EFBIG) {
1309 		printf("%s: can't map mbuf (error %d)\n",
1310 		    sc->sc_dev.dv_xname, error);
1311 		m_freem(m0);
1312 		return error;
1313 	}
1314 	if (error != 0) {
1315 		/* too many fragments, linearize */
1316 		if (m_defrag(m0, M_DONTWAIT)) {
1317 			m_freem(m0);
1318 			return ENOBUFS;
1319 		}
1320 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1321 		    BUS_DMA_NOWAIT);
1322 		if (error != 0) {
1323 			printf("%s: can't map mbuf (error %d)\n",
1324 			    sc->sc_dev.dv_xname, error);
1325 			m_freem(m0);
1326 			return error;
1327 		}
1328 	}
1329 
1330 	data->m = m0;
1331 	data->ni = ni;
1332 
1333 	desc->hdr.type = IWI_HDR_TYPE_DATA;
1334 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1335 	desc->cmd = IWI_DATA_CMD_TX;
1336 	desc->len = htole16(m0->m_pkthdr.len);
1337 	desc->station = station;
1338 	desc->flags = IWI_DATA_FLAG_NO_WEP;
1339 	desc->xflags = 0;
1340 
1341 	if (!IEEE80211_IS_MULTICAST(desc->wh.i_addr1))
1342 		desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1343 
1344 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1345 		desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1346 
1347 	if ((desc->wh.i_fc[0] &
1348 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_QOS)) ==
1349 	    (IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS))
1350 		desc->xflags |= IWI_DATA_XFLAG_QOS;
1351 
1352 	if (ic->ic_curmode == IEEE80211_MODE_11B)
1353 		desc->xflags |= IWI_DATA_XFLAG_CCK;
1354 
1355 	desc->nseg = htole32(data->map->dm_nsegs);
1356 	for (i = 0; i < data->map->dm_nsegs; i++) {
1357 		desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr);
1358 		desc->seg_len[i]  = htole16(data->map->dm_segs[i].ds_len);
1359 	}
1360 
1361 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
1362 	    BUS_DMASYNC_PREWRITE);
1363 	bus_dmamap_sync(sc->sc_dmat, txq->map,
1364 	    txq->cur * sizeof (struct iwi_tx_desc),
1365 	    sizeof (struct iwi_tx_desc), BUS_DMASYNC_PREWRITE);
1366 
1367 	DPRINTFN(5, ("sending data frame idx=%u len=%u nseg=%u\n", txq->cur,
1368 	    letoh16(desc->len), data->map->dm_nsegs));
1369 
1370 	txq->queued++;
1371 	txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
1372 	CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1373 
1374 	return 0;
1375 }
1376 
1377 void
1378 iwi_start(struct ifnet *ifp)
1379 {
1380 	struct iwi_softc *sc = ifp->if_softc;
1381 	struct ieee80211com *ic = &sc->sc_ic;
1382 	struct mbuf *m0;
1383 	struct ieee80211_node *ni;
1384 
1385 	if (ic->ic_state != IEEE80211_S_RUN)
1386 		return;
1387 
1388 	for (;;) {
1389 		if (sc->txq[0].queued + IWI_MAX_NSEG + 2 >= IWI_TX_RING_COUNT) {
1390 			ifq_set_oactive(&ifp->if_snd);
1391 			break;
1392 		}
1393 
1394 		IFQ_DEQUEUE(&ifp->if_snd, m0);
1395 		if (m0 == NULL)
1396 			break;
1397 
1398 #if NBPFILTER > 0
1399 		if (ifp->if_bpf != NULL)
1400 			bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
1401 #endif
1402 
1403 		m0 = ieee80211_encap(ifp, m0, &ni);
1404 		if (m0 == NULL)
1405 			continue;
1406 
1407 #if NBPFILTER > 0
1408 		if (ic->ic_rawbpf != NULL)
1409 			bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
1410 #endif
1411 
1412 		if (iwi_tx_start(ifp, m0, ni) != 0) {
1413 			if (ni != NULL)
1414 				ieee80211_release_node(ic, ni);
1415 			ifp->if_oerrors++;
1416 			break;
1417 		}
1418 
1419 		/* start watchdog timer */
1420 		sc->sc_tx_timer = 5;
1421 		ifp->if_timer = 1;
1422 	}
1423 }
1424 
1425 void
1426 iwi_watchdog(struct ifnet *ifp)
1427 {
1428 	struct iwi_softc *sc = ifp->if_softc;
1429 
1430 	ifp->if_timer = 0;
1431 
1432 	if (sc->sc_tx_timer > 0) {
1433 		if (--sc->sc_tx_timer == 0) {
1434 			printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1435 			ifp->if_flags &= ~IFF_UP;
1436 			iwi_stop(ifp, 1);
1437 			ifp->if_oerrors++;
1438 			return;
1439 		}
1440 		ifp->if_timer = 1;
1441 	}
1442 
1443 	ieee80211_watchdog(ifp);
1444 }
1445 
1446 int
1447 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1448 {
1449 	struct iwi_softc *sc = ifp->if_softc;
1450 	struct ieee80211com *ic = &sc->sc_ic;
1451 	struct ifreq *ifr;
1452 	int s, error = 0;
1453 
1454 	s = splnet();
1455 	/*
1456 	 * Prevent processes from entering this function while another
1457 	 * process is tsleep'ing in it.
1458 	 */
1459 	while ((sc->sc_flags & IWI_FLAG_BUSY) && error == 0)
1460 		error = tsleep(&sc->sc_flags, PCATCH, "iwiioc", 0);
1461 	if (error != 0) {
1462 		splx(s);
1463 		return error;
1464 	}
1465 	sc->sc_flags |= IWI_FLAG_BUSY;
1466 
1467 	switch (cmd) {
1468 	case SIOCSIFADDR:
1469 		ifp->if_flags |= IFF_UP;
1470 		/* FALLTHROUGH */
1471 	case SIOCSIFFLAGS:
1472 		if (ifp->if_flags & IFF_UP) {
1473 			if (!(ifp->if_flags & IFF_RUNNING))
1474 				iwi_init(ifp);
1475 		} else {
1476 			if (ifp->if_flags & IFF_RUNNING)
1477 				iwi_stop(ifp, 1);
1478 		}
1479 		break;
1480 
1481 	case SIOCADDMULTI:
1482 	case SIOCDELMULTI:
1483 		ifr = (struct ifreq *)data;
1484 		error = (cmd == SIOCADDMULTI) ?
1485 		    ether_addmulti(ifr, &ic->ic_ac) :
1486 		    ether_delmulti(ifr, &ic->ic_ac);
1487 
1488 		if (error == ENETRESET)
1489 			error = 0;
1490 		break;
1491 
1492 	case SIOCG80211TXPOWER:
1493 		/*
1494 		 * If the hardware radio transmitter switch is off, report a
1495 		 * tx power of IEEE80211_TXPOWER_MIN to indicate that radio
1496 		 * transmitter is killed.
1497 		 */
1498 		((struct ieee80211_txpower *)data)->i_val =
1499 		    (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ?
1500 		    sc->sc_ic.ic_txpower : IEEE80211_TXPOWER_MIN;
1501 		break;
1502 
1503 	default:
1504 		error = ieee80211_ioctl(ifp, cmd, data);
1505 	}
1506 
1507 	if (error == ENETRESET) {
1508 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1509 		    (IFF_UP | IFF_RUNNING))
1510 			iwi_init(ifp);
1511 		error = 0;
1512 	}
1513 
1514 	sc->sc_flags &= ~IWI_FLAG_BUSY;
1515 	wakeup(&sc->sc_flags);
1516 	splx(s);
1517 	return error;
1518 }
1519 
1520 void
1521 iwi_stop_master(struct iwi_softc *sc)
1522 {
1523 	uint32_t tmp;
1524 	int ntries;
1525 
1526 	/* disable interrupts */
1527 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1528 
1529 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1530 	for (ntries = 0; ntries < 5; ntries++) {
1531 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1532 			break;
1533 		DELAY(10);
1534 	}
1535 	if (ntries == 5) {
1536 		printf("%s: timeout waiting for master\n",
1537 		    sc->sc_dev.dv_xname);
1538 	}
1539 
1540 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
1541 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
1542 
1543 	sc->sc_flags &= ~IWI_FLAG_FW_INITED;
1544 }
1545 
1546 int
1547 iwi_reset(struct iwi_softc *sc)
1548 {
1549 	uint32_t tmp;
1550 	int i, ntries;
1551 
1552 	iwi_stop_master(sc);
1553 
1554 	/* move adapter to D0 state */
1555 	tmp = CSR_READ_4(sc, IWI_CSR_CTL);
1556 	CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
1557 
1558 	CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
1559 
1560 	/* wait for clock stabilization */
1561 	for (ntries = 0; ntries < 1000; ntries++) {
1562 		if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
1563 			break;
1564 		DELAY(200);
1565 	}
1566 	if (ntries == 1000) {
1567 		printf("%s: timeout waiting for clock stabilization\n",
1568 		    sc->sc_dev.dv_xname);
1569 		return ETIMEDOUT;
1570 	}
1571 
1572 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
1573 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SW_RESET);
1574 
1575 	DELAY(10);
1576 
1577 	tmp = CSR_READ_4(sc, IWI_CSR_CTL);
1578 	CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
1579 
1580 	/* clear NIC memory */
1581 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
1582 	for (i = 0; i < 0xc000; i++)
1583 		CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1584 
1585 	return 0;
1586 }
1587 
1588 int
1589 iwi_load_ucode(struct iwi_softc *sc, const char *data, int size)
1590 {
1591 	const uint16_t *w;
1592 	uint32_t tmp;
1593 	int ntries, i;
1594 
1595 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
1596 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_STOP_MASTER);
1597 	for (ntries = 0; ntries < 5; ntries++) {
1598 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1599 			break;
1600 		DELAY(10);
1601 	}
1602 	if (ntries == 5) {
1603 		printf("%s: timeout waiting for master\n",
1604 		    sc->sc_dev.dv_xname);
1605 		return ETIMEDOUT;
1606 	}
1607 
1608 	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1609 	DELAY(5000);
1610 
1611 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
1612 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp & ~IWI_RST_PRINCETON_RESET);
1613 
1614 	DELAY(5000);
1615 	MEM_WRITE_4(sc, 0x3000e0, 0);
1616 	DELAY(1000);
1617 	MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, 1);
1618 	DELAY(1000);
1619 	MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, 0);
1620 	DELAY(1000);
1621 	MEM_WRITE_1(sc, 0x200000, 0x00);
1622 	MEM_WRITE_1(sc, 0x200000, 0x40);
1623 	DELAY(1000);
1624 
1625 	/* adapter is buggy, we must set the address for each word */
1626 	for (w = (const uint16_t *)data; size > 0; w++, size -= 2)
1627 		MEM_WRITE_2(sc, 0x200010, htole16(*w));
1628 
1629 	MEM_WRITE_1(sc, 0x200000, 0x00);
1630 	MEM_WRITE_1(sc, 0x200000, 0x80);
1631 
1632 	/* wait until we get an answer */
1633 	for (ntries = 0; ntries < 100; ntries++) {
1634 		if (MEM_READ_1(sc, 0x200000) & 1)
1635 			break;
1636 		DELAY(100);
1637 	}
1638 	if (ntries == 100) {
1639 		printf("%s: timeout waiting for ucode to initialize\n",
1640 		    sc->sc_dev.dv_xname);
1641 		return ETIMEDOUT;
1642 	}
1643 
1644 	/* read the answer or the firmware will not initialize properly */
1645 	for (i = 0; i < 7; i++)
1646 		MEM_READ_4(sc, 0x200004);
1647 
1648 	MEM_WRITE_1(sc, 0x200000, 0x00);
1649 
1650 	return 0;
1651 }
1652 
1653 /* macro to handle unaligned little endian data in firmware image */
1654 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1655 
1656 int
1657 iwi_load_firmware(struct iwi_softc *sc, const char *data, int size)
1658 {
1659 	bus_dmamap_t map;
1660 	bus_dma_segment_t seg;
1661 	caddr_t virtaddr;
1662 	u_char *p, *end;
1663 	uint32_t sentinel, tmp, ctl, src, dst, sum, len, mlen;
1664 	int ntries, nsegs, error;
1665 
1666 	/* allocate DMA memory to store firmware image */
1667 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1668 	    BUS_DMA_NOWAIT, &map);
1669 	if (error != 0) {
1670 		printf("%s: could not create firmware DMA map\n",
1671 		    sc->sc_dev.dv_xname);
1672 		goto fail1;
1673 	}
1674 
1675 	error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
1676 	    &nsegs, BUS_DMA_NOWAIT);
1677 	if (error != 0) {
1678 		printf("%s: could not allocate firmware DMA memory\n",
1679 		    sc->sc_dev.dv_xname);
1680 		goto fail2;
1681 	}
1682 
1683 	error = bus_dmamem_map(sc->sc_dmat, &seg, nsegs, size, &virtaddr,
1684 	    BUS_DMA_NOWAIT);
1685 	if (error != 0) {
1686 		printf("%s: can't map firmware DMA memory\n",
1687 		    sc->sc_dev.dv_xname);
1688 		goto fail3;
1689 	}
1690 
1691 	error = bus_dmamap_load(sc->sc_dmat, map, virtaddr, size, NULL,
1692 	    BUS_DMA_NOWAIT);
1693 	if (error != 0) {
1694 		printf("%s: could not load firmware DMA map\n",
1695 		    sc->sc_dev.dv_xname);
1696 		goto fail4;
1697 	}
1698 
1699 	/* copy firmware image to DMA memory */
1700 	bcopy(data, virtaddr, size);
1701 
1702 	/* make sure the adapter will get up-to-date values */
1703 	bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
1704 
1705 	/* tell the adapter where the command blocks are stored */
1706 	MEM_WRITE_4(sc, 0x3000a0, 0x27000);
1707 
1708 	/*
1709 	 * Store command blocks into adapter's internal memory using register
1710 	 * indirections. The adapter will read the firmware image through DMA
1711 	 * using information stored in command blocks.
1712 	 */
1713 	src = map->dm_segs[0].ds_addr;
1714 	p = virtaddr;
1715 	end = p + size;
1716 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
1717 
1718 	while (p < end) {
1719 		dst = GETLE32(p); p += 4; src += 4;
1720 		len = GETLE32(p); p += 4; src += 4;
1721 		p += len;
1722 
1723 		while (len > 0) {
1724 			mlen = min(len, IWI_CB_MAXDATALEN);
1725 
1726 			ctl = IWI_CB_DEFAULT_CTL | mlen;
1727 			sum = ctl ^ src ^ dst;
1728 
1729 			/* write a command block */
1730 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
1731 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
1732 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
1733 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
1734 
1735 			src += mlen;
1736 			dst += mlen;
1737 			len -= mlen;
1738 		}
1739 	}
1740 
1741 	/* write a fictive final command block (sentinel) */
1742 	sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
1743 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1744 
1745 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
1746 	tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
1747 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
1748 
1749 	/* tell the adapter to start processing command blocks */
1750 	MEM_WRITE_4(sc, 0x3000a4, 0x540100);
1751 
1752 	/* wait until the adapter has processed all command blocks */
1753 	for (ntries = 0; ntries < 400; ntries++) {
1754 		if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
1755 			break;
1756 		DELAY(100);
1757 	}
1758 	if (ntries == 400) {
1759 		printf("%s: timeout processing cb\n", sc->sc_dev.dv_xname);
1760 		error = ETIMEDOUT;
1761 		goto fail5;
1762 	}
1763 
1764 	/* we're done with command blocks processing */
1765 	MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
1766 
1767 	/* allow interrupts so we know when the firmware is inited */
1768 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1769 
1770 	/* tell the adapter to initialize the firmware */
1771 	CSR_WRITE_4(sc, IWI_CSR_RST, 0);
1772 
1773 	tmp = CSR_READ_4(sc, IWI_CSR_CTL);
1774 	CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
1775 
1776 	/* wait at most one second for firmware initialization to complete */
1777 	if ((error = tsleep(sc, PCATCH, "iwiinit", hz)) != 0) {
1778 		printf("%s: timeout waiting for firmware initialization to "
1779 		    "complete\n", sc->sc_dev.dv_xname);
1780 		goto fail5;
1781 	}
1782 
1783 fail5:	bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
1784 	bus_dmamap_unload(sc->sc_dmat, map);
1785 fail4:	bus_dmamem_unmap(sc->sc_dmat, virtaddr, size);
1786 fail3:	bus_dmamem_free(sc->sc_dmat, &seg, 1);
1787 fail2:	bus_dmamap_destroy(sc->sc_dmat, map);
1788 fail1:	return error;
1789 }
1790 
1791 int
1792 iwi_config(struct iwi_softc *sc)
1793 {
1794 	struct ieee80211com *ic = &sc->sc_ic;
1795 	struct ifnet *ifp = &ic->ic_if;
1796 	struct iwi_configuration config;
1797 	struct iwi_rateset rs;
1798 	struct iwi_txpower power;
1799 	uint32_t data;
1800 	int error, nchan, i;
1801 
1802 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1803 	DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
1804 	error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
1805 	    IEEE80211_ADDR_LEN, 0);
1806 	if (error != 0)
1807 		return error;
1808 
1809 	bzero(&config, sizeof config);
1810 	config.multicast_enabled = 1;
1811 	config.silence_threshold = 30;
1812 	config.report_noise = 1;
1813 	config.answer_pbreq =
1814 #ifndef IEEE80211_STA_ONLY
1815 	    (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 :
1816 #endif
1817 	    0;
1818 	DPRINTF(("Configuring adapter\n"));
1819 	error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 0);
1820 	if (error != 0)
1821 		return error;
1822 
1823 	data = htole32(IWI_POWER_MODE_CAM);
1824 	DPRINTF(("Setting power mode to %u\n", letoh32(data)));
1825 	error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
1826 	if (error != 0)
1827 		return error;
1828 
1829 	data = htole32(ic->ic_rtsthreshold);
1830 	DPRINTF(("Setting RTS threshold to %u\n", letoh32(data)));
1831 	error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
1832 	if (error != 0)
1833 		return error;
1834 
1835 	data = htole32(ic->ic_fragthreshold);
1836 	DPRINTF(("Setting fragmentation threshold to %u\n", letoh32(data)));
1837 	error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
1838 	if (error != 0)
1839 		return error;
1840 
1841 	/*
1842 	 * Set default Tx power for 802.11b/g and 802.11a channels.
1843 	 */
1844 	nchan = 0;
1845 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1846 		if (!IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]))
1847 			continue;
1848 		power.chan[nchan].chan = i;
1849 		power.chan[nchan].power = IWI_TXPOWER_MAX;
1850 		nchan++;
1851 	}
1852 	power.nchan = nchan;
1853 
1854 	power.mode = IWI_MODE_11G;
1855 	DPRINTF(("Setting .11g channels tx power\n"));
1856 	error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0);
1857 	if (error != 0)
1858 		return error;
1859 
1860 	power.mode = IWI_MODE_11B;
1861 	DPRINTF(("Setting .11b channels tx power\n"));
1862 	error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0);
1863 	if (error != 0)
1864 		return error;
1865 
1866 	nchan = 0;
1867 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
1868 		if (!IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]))
1869 			continue;
1870 		power.chan[nchan].chan = i;
1871 		power.chan[nchan].power = IWI_TXPOWER_MAX;
1872 		nchan++;
1873 	}
1874 	power.nchan = nchan;
1875 
1876 	if (nchan > 0) {	/* 2915ABG only */
1877 		power.mode = IWI_MODE_11A;
1878 		DPRINTF(("Setting .11a channels tx power\n"));
1879 		error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
1880 		    0);
1881 		if (error != 0)
1882 			return error;
1883 	}
1884 
1885 	rs.mode = IWI_MODE_11G;
1886 	rs.type = IWI_RATESET_TYPE_SUPPORTED;
1887 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
1888 	bcopy(ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates, rs.rates,
1889 	    rs.nrates);
1890 	DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
1891 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
1892 	if (error != 0)
1893 		return error;
1894 
1895 	rs.mode = IWI_MODE_11A;
1896 	rs.type = IWI_RATESET_TYPE_SUPPORTED;
1897 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
1898 	bcopy(ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates, rs.rates,
1899 	    rs.nrates);
1900 	DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
1901 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
1902 	if (error != 0)
1903 		return error;
1904 
1905 	/* if we have a desired ESSID, set it now */
1906 	if (ic->ic_des_esslen != 0) {
1907 #ifdef IWI_DEBUG
1908 		if (iwi_debug > 0) {
1909 			printf("Setting desired ESSID to ");
1910 			ieee80211_print_essid(ic->ic_des_essid,
1911 			    ic->ic_des_esslen);
1912 			printf("\n");
1913 		}
1914 #endif
1915 		error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_essid,
1916 		    ic->ic_des_esslen, 0);
1917 		if (error != 0)
1918 			return error;
1919 	}
1920 
1921 	arc4random_buf(&data, sizeof data);
1922 	DPRINTF(("Setting random seed to %u\n", data));
1923 	error = iwi_cmd(sc, IWI_CMD_SET_RANDOM_SEED, &data, sizeof data, 0);
1924 	if (error != 0)
1925 		return error;
1926 
1927 	/* enable adapter */
1928 	DPRINTF(("Enabling adapter\n"));
1929 	return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
1930 }
1931 
1932 void
1933 iwi_update_edca(struct ieee80211com *ic)
1934 {
1935 #define IWI_EXP2(v)	htole16((1 << (v)) - 1)
1936 #define IWI_TXOP(v)	IEEE80211_TXOP_TO_US(v)
1937 	struct iwi_softc *sc = ic->ic_softc;
1938 	struct iwi_qos_cmd cmd;
1939 	struct iwi_qos_params *qos;
1940 	struct ieee80211_edca_ac_params *edca = ic->ic_edca_ac;
1941 	int aci;
1942 
1943 	/* set default QoS parameters for CCK */
1944 	qos = &cmd.cck;
1945 	for (aci = 0; aci < EDCA_NUM_AC; aci++) {
1946 		qos->cwmin[aci] = IWI_EXP2(iwi_cck[aci].ac_ecwmin);
1947 		qos->cwmax[aci] = IWI_EXP2(iwi_cck[aci].ac_ecwmax);
1948 		qos->txop [aci] = IWI_TXOP(iwi_cck[aci].ac_txoplimit);
1949 		qos->aifsn[aci] = iwi_cck[aci].ac_aifsn;
1950 		qos->acm  [aci] = 0;
1951 	}
1952 	/* set default QoS parameters for OFDM */
1953 	qos = &cmd.ofdm;
1954 	for (aci = 0; aci < EDCA_NUM_AC; aci++) {
1955 		qos->cwmin[aci] = IWI_EXP2(iwi_ofdm[aci].ac_ecwmin);
1956 		qos->cwmax[aci] = IWI_EXP2(iwi_ofdm[aci].ac_ecwmax);
1957 		qos->txop [aci] = IWI_TXOP(iwi_ofdm[aci].ac_txoplimit);
1958 		qos->aifsn[aci] = iwi_ofdm[aci].ac_aifsn;
1959 		qos->acm  [aci] = 0;
1960 	}
1961 	/* set current QoS parameters */
1962 	qos = &cmd.current;
1963 	for (aci = 0; aci < EDCA_NUM_AC; aci++) {
1964 		qos->cwmin[aci] = IWI_EXP2(edca[aci].ac_ecwmin);
1965 		qos->cwmax[aci] = IWI_EXP2(edca[aci].ac_ecwmax);
1966 		qos->txop [aci] = IWI_TXOP(edca[aci].ac_txoplimit);
1967 		qos->aifsn[aci] = edca[aci].ac_aifsn;
1968 		qos->acm  [aci] = 0;
1969 	}
1970 
1971 	DPRINTF(("Setting QoS parameters\n"));
1972 	(void)iwi_cmd(sc, IWI_CMD_SET_QOS_PARAMS, &cmd, sizeof cmd, 1);
1973 #undef IWI_EXP2
1974 #undef IWI_TXOP
1975 }
1976 
1977 int
1978 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
1979 {
1980 	struct ieee80211com *ic = &sc->sc_ic;
1981 	struct iwi_scan scan;
1982 
1983 	bzero(&scan, sizeof scan);
1984 	memset(scan.type, IWI_SCAN_TYPE_PASSIVE, sizeof scan.type);
1985 	scan.passive = htole16(2000);
1986 	scan.channels[0] = 1 |
1987 	    (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ);
1988 	scan.channels[1] = ieee80211_chan2ieee(ic, chan);
1989 
1990 	DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
1991 	return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
1992 }
1993 
1994 int
1995 iwi_scan(struct iwi_softc *sc)
1996 {
1997 	struct ieee80211com *ic = &sc->sc_ic;
1998 	struct iwi_scan scan;
1999 	uint8_t *p;
2000 	int i, count;
2001 
2002 	bzero(&scan, sizeof scan);
2003 
2004 	if (ic->ic_des_esslen != 0) {
2005 		scan.bdirected = htole16(40);
2006 		memset(scan.type, IWI_SCAN_TYPE_BDIRECTED, sizeof scan.type);
2007 	} else {
2008 		scan.broadcast = htole16(40);
2009 		memset(scan.type, IWI_SCAN_TYPE_BROADCAST, sizeof scan.type);
2010 	}
2011 
2012 	p = scan.channels;
2013 	count = 0;
2014 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2015 		if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i])) {
2016 			*++p = i;
2017 			count++;
2018 		}
2019 	}
2020 	*(p - count) = IWI_CHAN_5GHZ | count;
2021 
2022 	p = (count > 0) ? p + 1 : scan.channels;
2023 	count = 0;
2024 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2025 		if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i])) {
2026 			*++p = i;
2027 			count++;
2028 		}
2029 	}
2030 	*(p - count) = IWI_CHAN_2GHZ | count;
2031 
2032 	DPRINTF(("Start scanning\n"));
2033 	return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2034 }
2035 
2036 int
2037 iwi_auth_and_assoc(struct iwi_softc *sc)
2038 {
2039 	struct ieee80211com *ic = &sc->sc_ic;
2040 	struct ieee80211_node *ni = ic->ic_bss;
2041 	struct iwi_configuration config;
2042 	struct iwi_associate assoc;
2043 	struct iwi_rateset rs;
2044 	uint8_t *frm;
2045 	uint32_t data;
2046 	uint16_t capinfo;
2047 	uint8_t buf[64];	/* XXX max WPA/RSN/WMM IE length */
2048 	int error;
2049 
2050 	/* update adapter configuration */
2051 	bzero(&config, sizeof config);
2052 	config.multicast_enabled = 1;
2053 	config.disable_unicast_decryption = 1;
2054 	config.disable_multicast_decryption = 1;
2055 	config.silence_threshold = 30;
2056 	config.report_noise = 1;
2057 	config.answer_pbreq =
2058 #ifndef IEEE80211_STA_ONLY
2059 	    (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 :
2060 #endif
2061 	    0;
2062 	if (ic->ic_curmode == IEEE80211_MODE_11G)
2063 		config.bg_autodetection = 1;
2064 	DPRINTF(("Configuring adapter\n"));
2065 	error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 1);
2066 	if (error != 0)
2067 		return error;
2068 
2069 #ifdef IWI_DEBUG
2070 	if (iwi_debug > 0) {
2071 		printf("Setting ESSID to ");
2072 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2073 		printf("\n");
2074 	}
2075 #endif
2076 	error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2077 	if (error != 0)
2078 		return error;
2079 
2080 	/* the rate set has already been "negotiated" */
2081 	rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2082 	    IWI_MODE_11G;
2083 	rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2084 	rs.nrates = ni->ni_rates.rs_nrates;
2085 	if (rs.nrates > sizeof rs.rates) {
2086 #ifdef DIAGNOSTIC
2087 		/* should not happen since the rates are negotiated */
2088 		printf("%s: XXX too many rates (count=%d, last=%d)\n",
2089 		    sc->sc_dev.dv_xname, ni->ni_rates.rs_nrates,
2090 		    ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] &
2091 		    IEEE80211_RATE_VAL);
2092 #endif
2093 		rs.nrates = sizeof rs.rates;
2094 	}
2095 	bcopy(ni->ni_rates.rs_rates, rs.rates, rs.nrates);
2096 	DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
2097 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2098 	if (error != 0)
2099 		return error;
2100 
2101 	data = htole32(ni->ni_rssi);
2102 	DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2103 	error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2104 	if (error != 0)
2105 		return error;
2106 
2107 	if (ic->ic_flags & IEEE80211_F_QOS) {
2108 		iwi_update_edca(ic);
2109 
2110 		frm = ieee80211_add_qos_capability(buf, ic);
2111 		DPRINTF(("Setting QoS Capability IE length %d\n", frm - buf));
2112 		error = iwi_cmd(sc, IWI_CMD_SET_QOS_CAP, buf, frm - buf, 1);
2113 		if (error != 0)
2114 			return error;
2115 	}
2116 	if (ic->ic_flags & IEEE80211_F_RSNON) {
2117 		/* tell firmware to add WPA/RSN IE to (re)assoc request */
2118 		if (ni->ni_rsnprotos == IEEE80211_PROTO_RSN)
2119 			frm = ieee80211_add_rsn(buf, ic, ni);
2120 		else
2121 			frm = ieee80211_add_wpa(buf, ic, ni);
2122 		DPRINTF(("Setting RSN IE length %d\n", frm - buf));
2123 		error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, buf, frm - buf, 1);
2124 		if (error != 0)
2125 			return error;
2126 	}
2127 
2128 	bzero(&assoc, sizeof assoc);
2129 #ifndef IEEE80211_STA_ONLY
2130 	if (ic->ic_flags & IEEE80211_F_SIBSS)
2131 		assoc.type = IWI_ASSOC_SIBSS;
2132 	else
2133 #endif
2134 		assoc.type = IWI_ASSOC_ASSOCIATE;
2135 	assoc.policy = 0;
2136 	if (ic->ic_flags & IEEE80211_F_RSNON)
2137 		assoc.policy |= htole16(IWI_ASSOC_POLICY_RSN);
2138 	if (ic->ic_flags & IEEE80211_F_QOS)
2139 		assoc.policy |= htole16(IWI_ASSOC_POLICY_QOS);
2140 	if (ic->ic_curmode == IEEE80211_MODE_11A)
2141 		assoc.mode = IWI_MODE_11A;
2142 	else if (ic->ic_curmode == IEEE80211_MODE_11B)
2143 		assoc.mode = IWI_MODE_11B;
2144 	else	/* assume 802.11b/g */
2145 		assoc.mode = IWI_MODE_11G;
2146 	assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2147 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2148 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2149 		assoc.plen = IWI_ASSOC_SHPREAMBLE;
2150 	bcopy(ni->ni_tstamp, assoc.tstamp, 8);
2151 	capinfo = IEEE80211_CAPINFO_ESS;
2152 	if (ic->ic_flags & IEEE80211_F_WEPON)
2153 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
2154 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2155 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2156 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2157 	if (ic->ic_caps & IEEE80211_C_SHSLOT)
2158 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2159 	assoc.capinfo = htole16(capinfo);
2160 
2161 	assoc.lintval = htole16(ic->ic_lintval);
2162 	assoc.intval = htole16(ni->ni_intval);
2163 	IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
2164 #ifndef IEEE80211_STA_ONLY
2165 	if (ic->ic_opmode == IEEE80211_M_IBSS)
2166 		IEEE80211_ADDR_COPY(assoc.dst, etherbroadcastaddr);
2167 	else
2168 #endif
2169 		IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
2170 
2171 	DPRINTF(("Trying to associate to %s channel %u auth %u\n",
2172 	    ether_sprintf(assoc.bssid), assoc.chan, assoc.auth));
2173 	return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
2174 }
2175 
2176 int
2177 iwi_init(struct ifnet *ifp)
2178 {
2179 	struct iwi_softc *sc = ifp->if_softc;
2180 	struct ieee80211com *ic = &sc->sc_ic;
2181 	struct iwi_firmware_hdr *hdr;
2182 	const char *name, *fw;
2183 	u_char *data;
2184 	size_t size;
2185 	int i, ac, error;
2186 
2187 	iwi_stop(ifp, 0);
2188 
2189 	if ((error = iwi_reset(sc)) != 0) {
2190 		printf("%s: could not reset adapter\n", sc->sc_dev.dv_xname);
2191 		goto fail1;
2192 	}
2193 
2194 	switch (ic->ic_opmode) {
2195 	case IEEE80211_M_STA:
2196 		name = "iwi-bss";
2197 		break;
2198 #ifndef IEEE80211_STA_ONLY
2199 	case IEEE80211_M_IBSS:
2200 	case IEEE80211_M_AHDEMO:
2201 		name = "iwi-ibss";
2202 		break;
2203 #endif
2204 	case IEEE80211_M_MONITOR:
2205 		name = "iwi-monitor";
2206 		break;
2207 	default:
2208 		/* should not get there */
2209 		error = EINVAL;
2210 		goto fail1;
2211 	}
2212 
2213 	if ((error = loadfirmware(name, &data, &size)) != 0) {
2214 		printf("%s: error %d, could not read firmware %s\n",
2215 		    sc->sc_dev.dv_xname, error, name);
2216 		goto fail1;
2217 	}
2218 	if (size < sizeof (struct iwi_firmware_hdr)) {
2219 		printf("%s: firmware image too short: %zu bytes\n",
2220 		    sc->sc_dev.dv_xname, size);
2221 		error = EINVAL;
2222 		goto fail2;
2223 	}
2224 	hdr = (struct iwi_firmware_hdr *)data;
2225 
2226 	if (hdr->vermaj < 3 || hdr->bootsz == 0 || hdr->ucodesz == 0 ||
2227 	    hdr->mainsz == 0) {
2228 		printf("%s: firmware image too old (need at least 3.0)\n",
2229 		    sc->sc_dev.dv_xname);
2230 		error = EINVAL;
2231 		goto fail2;
2232 	}
2233 
2234 	if (size < sizeof (struct iwi_firmware_hdr) + letoh32(hdr->bootsz) +
2235 	    letoh32(hdr->ucodesz) + letoh32(hdr->mainsz)) {
2236 		printf("%s: firmware image too short: %zu bytes\n",
2237 		    sc->sc_dev.dv_xname, size);
2238 		error = EINVAL;
2239 		goto fail2;
2240 	}
2241 
2242 	fw = (const char *)data + sizeof (struct iwi_firmware_hdr);
2243 	if ((error = iwi_load_firmware(sc, fw, letoh32(hdr->bootsz))) != 0) {
2244 		printf("%s: could not load boot firmware\n",
2245 		    sc->sc_dev.dv_xname);
2246 		goto fail2;
2247 	}
2248 
2249 	fw = (const char *)data + sizeof (struct iwi_firmware_hdr) +
2250 	    letoh32(hdr->bootsz);
2251 	if ((error = iwi_load_ucode(sc, fw, letoh32(hdr->ucodesz))) != 0) {
2252 		printf("%s: could not load microcode\n", sc->sc_dev.dv_xname);
2253 		goto fail2;
2254 	}
2255 
2256 	iwi_stop_master(sc);
2257 
2258 	CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.map->dm_segs[0].ds_addr);
2259 	CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, IWI_CMD_RING_COUNT);
2260 	CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
2261 
2262 	for (ac = 0; ac < EDCA_NUM_AC; ac++) {
2263 		CSR_WRITE_4(sc, IWI_CSR_TX_BASE(ac),
2264 		    sc->txq[ac].map->dm_segs[0].ds_addr);
2265 		CSR_WRITE_4(sc, IWI_CSR_TX_SIZE(ac), IWI_TX_RING_COUNT);
2266 		CSR_WRITE_4(sc, IWI_CSR_TX_WIDX(ac), sc->txq[ac].cur);
2267 	}
2268 
2269 	for (i = 0; i < IWI_RX_RING_COUNT; i++) {
2270 		struct iwi_rx_data *data = &sc->rxq.data[i];
2271 		CSR_WRITE_4(sc, data->reg, data->map->dm_segs[0].ds_addr);
2272 	}
2273 
2274 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, IWI_RX_RING_COUNT - 1);
2275 
2276 	fw = (const char *)data + sizeof (struct iwi_firmware_hdr) +
2277 	    letoh32(hdr->bootsz) + letoh32(hdr->ucodesz);
2278 	if ((error = iwi_load_firmware(sc, fw, letoh32(hdr->mainsz))) != 0) {
2279 		printf("%s: could not load main firmware\n",
2280 		    sc->sc_dev.dv_xname);
2281 		goto fail2;
2282 	}
2283 
2284 	free(data, M_DEVBUF, size);
2285 	sc->sc_flags |= IWI_FLAG_FW_INITED;
2286 
2287 	if ((error = iwi_config(sc)) != 0) {
2288 		printf("%s: device configuration failed\n",
2289 		    sc->sc_dev.dv_xname);
2290 		goto fail1;
2291 	}
2292 
2293 	ifq_clr_oactive(&ifp->if_snd);
2294 	ifp->if_flags |= IFF_RUNNING;
2295 
2296 	if (ic->ic_opmode != IEEE80211_M_MONITOR)
2297 		ieee80211_begin_scan(ifp);
2298 	else
2299 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2300 
2301 	return 0;
2302 
2303 fail2:	free(data, M_DEVBUF, size);
2304 fail1:	iwi_stop(ifp, 0);
2305 	return error;
2306 }
2307 
2308 void
2309 iwi_stop(struct ifnet *ifp, int disable)
2310 {
2311 	struct iwi_softc *sc = ifp->if_softc;
2312 	struct ieee80211com *ic = &sc->sc_ic;
2313 	int ac;
2314 
2315 	sc->sc_tx_timer = 0;
2316 	ifp->if_timer = 0;
2317 	ifp->if_flags &= ~IFF_RUNNING;
2318 	ifq_clr_oactive(&ifp->if_snd);
2319 
2320 	/* in case we were scanning, release the scan "lock" */
2321 	ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED;
2322 
2323 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2324 
2325 	iwi_stop_master(sc);
2326 
2327 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
2328 
2329 	/* reset rings */
2330 	iwi_reset_cmd_ring(sc, &sc->cmdq);
2331 	for (ac = 0; ac < EDCA_NUM_AC; ac++)
2332 		iwi_reset_tx_ring(sc, &sc->txq[ac]);
2333 	iwi_reset_rx_ring(sc, &sc->rxq);
2334 }
2335 
2336 struct cfdriver iwi_cd = {
2337 	NULL, "iwi", DV_IFNET
2338 };
2339