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