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