xref: /freebsd/sys/dev/iwi/if_iwi.c (revision 87569f75a91f298c52a71823c04d41cf53c88889)
1 /*	$FreeBSD$	*/
2 
3 /*-
4  * Copyright (c) 2004-2006
5  *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*-
34  * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
35  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
36  */
37 
38 #include <sys/param.h>
39 #include <sys/sysctl.h>
40 #include <sys/sockio.h>
41 #include <sys/mbuf.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/taskqueue.h>
48 #include <sys/module.h>
49 #include <sys/bus.h>
50 #include <sys/endian.h>
51 #include <sys/linker.h>
52 #include <sys/firmware.h>
53 
54 #include <machine/bus.h>
55 #include <machine/resource.h>
56 #include <machine/clock.h>
57 #include <sys/rman.h>
58 
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
61 
62 #include <net/bpf.h>
63 #include <net/if.h>
64 #include <net/if_arp.h>
65 #include <net/ethernet.h>
66 #include <net/if_dl.h>
67 #include <net/if_media.h>
68 #include <net/if_types.h>
69 
70 #include <net80211/ieee80211_var.h>
71 #include <net80211/ieee80211_radiotap.h>
72 
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/if_ether.h>
78 
79 #include <dev/iwi/if_iwivar.h>
80 #include <dev/iwi/if_iwireg.h>
81 
82 #ifdef IWI_DEBUG
83 #define DPRINTF(x)	do { if (iwi_debug > 0) printf x; } while (0)
84 #define DPRINTFN(n, x)	do { if (iwi_debug >= (n)) printf x; } while (0)
85 int iwi_debug = 0;
86 SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level");
87 #else
88 #define DPRINTF(x)
89 #define DPRINTFN(n, x)
90 #endif
91 
92 MODULE_DEPEND(iwi, pci,  1, 1, 1);
93 MODULE_DEPEND(iwi, wlan, 1, 1, 1);
94 MODULE_DEPEND(iwi, firmware, 1, 1, 1);
95 
96 struct iwi_ident {
97 	uint16_t	vendor;
98 	uint16_t	device;
99 	const char	*name;
100 };
101 
102 static const struct iwi_ident iwi_ident_table[] = {
103 	{ 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" },
104 	{ 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" },
105 	{ 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" },
106 	{ 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" },
107 
108 	{ 0, 0, NULL }
109 };
110 
111 static void	iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
112 static int	iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
113 		    int);
114 static void	iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
115 static void	iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
116 static int	iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
117 		    int, bus_addr_t, bus_addr_t);
118 static void	iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
119 static void	iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
120 static int	iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
121 		    int);
122 static void	iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
123 static void	iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
124 static struct	ieee80211_node *iwi_node_alloc(struct ieee80211_node_table *);
125 static void	iwi_node_free(struct ieee80211_node *);
126 static int	iwi_media_change(struct ifnet *);
127 static void	iwi_media_status(struct ifnet *, struct ifmediareq *);
128 static int	iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
129 static int	iwi_wme_update(struct ieee80211com *);
130 static uint16_t	iwi_read_prom_word(struct iwi_softc *, uint8_t);
131 static void	iwi_fix_channel(struct ieee80211com *, struct mbuf *);
132 static void	iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
133 		    struct iwi_frame *);
134 static void	iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
135 static void	iwi_rx_intr(struct iwi_softc *);
136 static void	iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
137 static void	iwi_intr(void *);
138 static int	iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int);
139 static void	iwi_write_ibssnode(struct iwi_softc *, const struct iwi_node *);
140 static int	iwi_tx_start(struct ifnet *, struct mbuf *,
141 		    struct ieee80211_node *, int);
142 static void	iwi_start(struct ifnet *);
143 static void	iwi_watchdog(struct ifnet *);
144 static int	iwi_ioctl(struct ifnet *, u_long, caddr_t);
145 static void	iwi_stop_master(struct iwi_softc *);
146 static int	iwi_reset(struct iwi_softc *);
147 static int	iwi_load_ucode(struct iwi_softc *, const char *, int);
148 static int	iwi_load_firmware(struct iwi_softc *, const char *, int);
149 static int	iwi_config(struct iwi_softc *);
150 static int	iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
151 static int	iwi_scan(struct iwi_softc *);
152 static int	iwi_auth_and_assoc(struct iwi_softc *);
153 static void	iwi_init_task(void *, int);
154 static void	iwi_init(void *);
155 static void	iwi_stop(void *);
156 static int	iwi_sysctl_stats(SYSCTL_HANDLER_ARGS);
157 static int	iwi_sysctl_radio(SYSCTL_HANDLER_ARGS);
158 
159 static int iwi_probe(device_t);
160 static int iwi_attach(device_t);
161 static int iwi_detach(device_t);
162 static int iwi_shutdown(device_t);
163 static int iwi_suspend(device_t);
164 static int iwi_resume(device_t);
165 
166 static device_method_t iwi_methods[] = {
167 	/* Device interface */
168 	DEVMETHOD(device_probe,		iwi_probe),
169 	DEVMETHOD(device_attach,	iwi_attach),
170 	DEVMETHOD(device_detach,	iwi_detach),
171 	DEVMETHOD(device_shutdown,	iwi_shutdown),
172 	DEVMETHOD(device_suspend,	iwi_suspend),
173 	DEVMETHOD(device_resume,	iwi_resume),
174 
175 	{ 0, 0 }
176 };
177 
178 static driver_t iwi_driver = {
179 	"iwi",
180 	iwi_methods,
181 	sizeof (struct iwi_softc)
182 };
183 
184 static devclass_t iwi_devclass;
185 
186 DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, 0, 0);
187 
188 /*
189  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
190  */
191 static const struct ieee80211_rateset iwi_rateset_11a =
192 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
193 
194 static const struct ieee80211_rateset iwi_rateset_11b =
195 	{ 4, { 2, 4, 11, 22 } };
196 
197 static const struct ieee80211_rateset iwi_rateset_11g =
198 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
199 
200 static int
201 iwi_probe(device_t dev)
202 {
203 	const struct iwi_ident *ident;
204 
205 	for (ident = iwi_ident_table; ident->name != NULL; ident++) {
206 		if (pci_get_vendor(dev) == ident->vendor &&
207 		    pci_get_device(dev) == ident->device) {
208 			device_set_desc(dev, ident->name);
209 			return 0;
210 		}
211 	}
212 	return ENXIO;
213 }
214 
215 /* Base Address Register */
216 #define IWI_PCI_BAR0	0x10
217 
218 static int
219 iwi_attach(device_t dev)
220 {
221 	struct iwi_softc *sc = device_get_softc(dev);
222 	struct ifnet *ifp;
223 	struct ieee80211com *ic = &sc->sc_ic;
224 	uint16_t val;
225 	int error, i;
226 
227 	sc->sc_dev = dev;
228 
229 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
230 	    MTX_DEF | MTX_RECURSE);
231 
232 	sc->sc_unr = new_unrhdr(0, IWI_MAX_IBSSNODE, &sc->sc_mtx);
233 
234 	TASK_INIT(&sc->sc_init_task, 0, iwi_init_task, sc);
235 
236 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
237 		device_printf(dev, "chip is in D%d power mode "
238 		    "-- setting to D0\n", pci_get_powerstate(dev));
239 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
240 	}
241 
242 	pci_write_config(dev, 0x41, 0, 1);
243 
244 	/* enable bus-mastering */
245 	pci_enable_busmaster(dev);
246 
247 	sc->mem_rid = IWI_PCI_BAR0;
248 	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
249 	    RF_ACTIVE);
250 	if (sc->mem == NULL) {
251 		device_printf(dev, "could not allocate memory resource\n");
252 		goto fail;
253 	}
254 
255 	sc->sc_st = rman_get_bustag(sc->mem);
256 	sc->sc_sh = rman_get_bushandle(sc->mem);
257 
258 	sc->irq_rid = 0;
259 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
260 	    RF_ACTIVE | RF_SHAREABLE);
261 	if (sc->irq == NULL) {
262 		device_printf(dev, "could not allocate interrupt resource\n");
263 		goto fail;
264 	}
265 
266 	if (iwi_reset(sc) != 0) {
267 		device_printf(dev, "could not reset adapter\n");
268 		goto fail;
269 	}
270 
271 	/*
272 	 * Allocate rings.
273 	 */
274 	if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
275 		device_printf(dev, "could not allocate Cmd ring\n");
276 		goto fail;
277 	}
278 
279 	error = iwi_alloc_tx_ring(sc, &sc->txq[0], IWI_TX_RING_COUNT,
280 	    IWI_CSR_TX1_RIDX, IWI_CSR_TX1_WIDX);
281 	if (error != 0) {
282 		device_printf(dev, "could not allocate Tx ring 1\n");
283 		goto fail;
284 	}
285 
286 	error = iwi_alloc_tx_ring(sc, &sc->txq[1], IWI_TX_RING_COUNT,
287 	    IWI_CSR_TX2_RIDX, IWI_CSR_TX2_WIDX);
288 	if (error != 0) {
289 		device_printf(dev, "could not allocate Tx ring 2\n");
290 		goto fail;
291 	}
292 
293 	error = iwi_alloc_tx_ring(sc, &sc->txq[2], IWI_TX_RING_COUNT,
294 	    IWI_CSR_TX3_RIDX, IWI_CSR_TX3_WIDX);
295 	if (error != 0) {
296 		device_printf(dev, "could not allocate Tx ring 3\n");
297 		goto fail;
298 	}
299 
300 	error = iwi_alloc_tx_ring(sc, &sc->txq[3], IWI_TX_RING_COUNT,
301 	    IWI_CSR_TX4_RIDX, IWI_CSR_TX4_WIDX);
302 	if (error != 0) {
303 		device_printf(dev, "could not allocate Tx ring 4\n");
304 		goto fail;
305 	}
306 
307 	if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
308 		device_printf(dev, "could not allocate Rx ring\n");
309 		goto fail;
310 	}
311 
312 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
313 	if (ifp == NULL) {
314 		device_printf(dev, "can not if_alloc()\n");
315 		goto fail;
316 	}
317 	ifp->if_softc = sc;
318 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
319 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
320 	ifp->if_init = iwi_init;
321 	ifp->if_ioctl = iwi_ioctl;
322 	ifp->if_start = iwi_start;
323 	ifp->if_watchdog = iwi_watchdog;
324 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
325 	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
326 	IFQ_SET_READY(&ifp->if_snd);
327 
328 	ic->ic_ifp = ifp;
329 	ic->ic_wme.wme_update = iwi_wme_update;
330 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
331 	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
332 	ic->ic_state = IEEE80211_S_INIT;
333 
334 	/* set device capabilities */
335 	ic->ic_caps =
336 	    IEEE80211_C_IBSS |		/* IBSS mode supported */
337 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
338 	    IEEE80211_C_TXPMGT |	/* tx power management */
339 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
340 	    IEEE80211_C_WPA |		/* 802.11i */
341 	    IEEE80211_C_WME;		/* 802.11e */
342 
343 	/* read MAC address from EEPROM */
344 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
345 	ic->ic_myaddr[0] = val & 0xff;
346 	ic->ic_myaddr[1] = val >> 8;
347 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
348 	ic->ic_myaddr[2] = val & 0xff;
349 	ic->ic_myaddr[3] = val >> 8;
350 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
351 	ic->ic_myaddr[4] = val & 0xff;
352 	ic->ic_myaddr[5] = val >> 8;
353 
354 	if (pci_get_device(dev) >= 0x4223) {
355 		/* set supported .11a rates (2915ABG only) */
356 		ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
357 
358 		/* set supported .11a channels */
359 		for (i = 36; i <= 64; i += 4) {
360 			ic->ic_channels[i].ic_freq =
361 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
362 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
363 		}
364 		for (i = 149; i <= 165; i += 4) {
365 			ic->ic_channels[i].ic_freq =
366 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
367 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
368 		}
369 	}
370 
371 	/* set supported .11b and .11g rates */
372 	ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
373 	ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
374 
375 	/* set supported .11b and .11g channels (1 through 14) */
376 	for (i = 1; i <= 14; i++) {
377 		ic->ic_channels[i].ic_freq =
378 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
379 		ic->ic_channels[i].ic_flags =
380 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
381 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
382 	}
383 
384 	ieee80211_ifattach(ic);
385 	/* override default methods */
386 	ic->ic_node_alloc = iwi_node_alloc;
387 	sc->sc_node_free = ic->ic_node_free;
388 	ic->ic_node_free = iwi_node_free;
389 	/* override state transition machine */
390 	sc->sc_newstate = ic->ic_newstate;
391 	ic->ic_newstate = iwi_newstate;
392 	ieee80211_media_init(ic, iwi_media_change, iwi_media_status);
393 
394 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
395 	    sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
396 
397 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
398 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
399 	sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
400 
401 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
402 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
403 	sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
404 
405 	/*
406 	 * Add a few sysctl knobs.
407 	 */
408 	sc->dwelltime = 100;
409 	sc->bluetooth = 1;
410 	sc->antenna = 2;
411 
412 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
413 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
414 	    CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I",
415 	    "radio transmitter switch state (0=off, 1=on)");
416 
417 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
418 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
419 	    CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S",
420 	    "statistics");
421 
422 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
423 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "dwell",
424 	    CTLFLAG_RW, &sc->dwelltime, 0,
425 	    "channel dwell time (ms) for AP/station scanning");
426 
427 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
428 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
429 	    "bluetooth", CTLFLAG_RW, &sc->bluetooth, 0,
430 	    "bluetooth coexistence");
431 
432 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
433 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "antenna",
434 	    CTLFLAG_RW, &sc->antenna, 0,
435 	    "antenna (0=auto,1=A,3=B,2=diversity)");
436 
437 	/*
438 	 * Hook our interrupt after all initialization is complete.
439 	 */
440 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
441 	    iwi_intr, sc, &sc->sc_ih);
442 	if (error != 0) {
443 		device_printf(dev, "could not set up interrupt\n");
444 		goto fail;
445 	}
446 
447 	if (bootverbose)
448 		ieee80211_announce(ic);
449 
450 	return 0;
451 
452 fail:	iwi_detach(dev);
453 	return ENXIO;
454 }
455 
456 static int
457 iwi_detach(device_t dev)
458 {
459 	struct iwi_softc *sc = device_get_softc(dev);
460 	struct ieee80211com *ic = &sc->sc_ic;
461 	struct ifnet *ifp = ic->ic_ifp;
462 
463 	iwi_stop(sc);
464 
465 	if (ifp != NULL) {
466 		bpfdetach(ifp);
467 		ieee80211_ifdetach(ic);
468 	}
469 
470 	iwi_free_cmd_ring(sc, &sc->cmdq);
471 	iwi_free_tx_ring(sc, &sc->txq[0]);
472 	iwi_free_tx_ring(sc, &sc->txq[1]);
473 	iwi_free_tx_ring(sc, &sc->txq[2]);
474 	iwi_free_tx_ring(sc, &sc->txq[3]);
475 	iwi_free_rx_ring(sc, &sc->rxq);
476 
477 	if (sc->irq != NULL) {
478 		bus_teardown_intr(dev, sc->irq, sc->sc_ih);
479 		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
480 	}
481 
482 	if (sc->mem != NULL)
483 		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
484 
485 	if (ifp != NULL)
486 		if_free(ifp);
487 
488 	if (sc->sc_unr != NULL)
489 		delete_unrhdr(sc->sc_unr);
490 
491 	mtx_destroy(&sc->sc_mtx);
492 
493 	return 0;
494 }
495 
496 static void
497 iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
498 {
499 	if (error != 0)
500 		return;
501 
502 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
503 
504 	*(bus_addr_t *)arg = segs[0].ds_addr;
505 }
506 
507 static int
508 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count)
509 {
510 	int error;
511 
512 	ring->count = count;
513 	ring->queued = 0;
514 	ring->cur = ring->next = 0;
515 
516 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
517 	    BUS_SPACE_MAXADDR, NULL, NULL, count * IWI_CMD_DESC_SIZE, 1,
518 	    count * IWI_CMD_DESC_SIZE, 0, NULL, NULL, &ring->desc_dmat);
519 	if (error != 0) {
520 		device_printf(sc->sc_dev, "could not create desc DMA tag\n");
521 		goto fail;
522 	}
523 
524 	error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
525 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
526 	if (error != 0) {
527 		device_printf(sc->sc_dev, "could not allocate DMA memory\n");
528 		goto fail;
529 	}
530 
531 	error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
532 	    count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
533 	if (error != 0) {
534 		device_printf(sc->sc_dev, "could not load desc DMA map\n");
535 		goto fail;
536 	}
537 
538 	return 0;
539 
540 fail:	iwi_free_cmd_ring(sc, ring);
541 	return error;
542 }
543 
544 static void
545 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
546 {
547 	ring->queued = 0;
548 	ring->cur = ring->next = 0;
549 }
550 
551 static void
552 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
553 {
554 	if (ring->desc != NULL) {
555 		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
556 		    BUS_DMASYNC_POSTWRITE);
557 		bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
558 		bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
559 	}
560 
561 	if (ring->desc_dmat != NULL)
562 		bus_dma_tag_destroy(ring->desc_dmat);
563 }
564 
565 static int
566 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count,
567     bus_addr_t csr_ridx, bus_addr_t csr_widx)
568 {
569 	int i, error;
570 
571 	ring->count = count;
572 	ring->queued = 0;
573 	ring->cur = ring->next = 0;
574 	ring->csr_ridx = csr_ridx;
575 	ring->csr_widx = csr_widx;
576 
577 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
578 	    BUS_SPACE_MAXADDR, NULL, NULL, count * IWI_TX_DESC_SIZE, 1,
579 	    count * IWI_TX_DESC_SIZE, 0, NULL, NULL, &ring->desc_dmat);
580 	if (error != 0) {
581 		device_printf(sc->sc_dev, "could not create desc DMA tag\n");
582 		goto fail;
583 	}
584 
585 	error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
586 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
587 	if (error != 0) {
588 		device_printf(sc->sc_dev, "could not allocate DMA memory\n");
589 		goto fail;
590 	}
591 
592 	error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
593 	    count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
594 	if (error != 0) {
595 		device_printf(sc->sc_dev, "could not load desc DMA map\n");
596 		goto fail;
597 	}
598 
599 	ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
600 	    M_NOWAIT | M_ZERO);
601 	if (ring->data == NULL) {
602 		device_printf(sc->sc_dev, "could not allocate soft data\n");
603 		error = ENOMEM;
604 		goto fail;
605 	}
606 
607 	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
608 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IWI_MAX_NSEG - 2,
609 	    MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
610 	if (error != 0) {
611 		device_printf(sc->sc_dev, "could not create data DMA tag\n");
612 		goto fail;
613 	}
614 
615 	for (i = 0; i < count; i++) {
616 		error = bus_dmamap_create(ring->data_dmat, 0,
617 		    &ring->data[i].map);
618 		if (error != 0) {
619 			device_printf(sc->sc_dev, "could not create DMA map\n");
620 			goto fail;
621 		}
622 	}
623 
624 	return 0;
625 
626 fail:	iwi_free_tx_ring(sc, ring);
627 	return error;
628 }
629 
630 static void
631 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
632 {
633 	struct iwi_tx_data *data;
634 	int i;
635 
636 	for (i = 0; i < ring->count; i++) {
637 		data = &ring->data[i];
638 
639 		if (data->m != NULL) {
640 			bus_dmamap_sync(ring->data_dmat, data->map,
641 			    BUS_DMASYNC_POSTWRITE);
642 			bus_dmamap_unload(ring->data_dmat, data->map);
643 			m_freem(data->m);
644 			data->m = NULL;
645 		}
646 
647 		if (data->ni != NULL) {
648 			ieee80211_free_node(data->ni);
649 			data->ni = NULL;
650 		}
651 	}
652 
653 	ring->queued = 0;
654 	ring->cur = ring->next = 0;
655 }
656 
657 static void
658 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
659 {
660 	struct iwi_tx_data *data;
661 	int i;
662 
663 	if (ring->desc != NULL) {
664 		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
665 		    BUS_DMASYNC_POSTWRITE);
666 		bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
667 		bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
668 	}
669 
670 	if (ring->desc_dmat != NULL)
671 		bus_dma_tag_destroy(ring->desc_dmat);
672 
673 	if (ring->data != NULL) {
674 		for (i = 0; i < ring->count; i++) {
675 			data = &ring->data[i];
676 
677 			if (data->m != NULL) {
678 				bus_dmamap_sync(ring->data_dmat, data->map,
679 				    BUS_DMASYNC_POSTWRITE);
680 				bus_dmamap_unload(ring->data_dmat, data->map);
681 				m_freem(data->m);
682 			}
683 
684 			if (data->ni != NULL)
685 				ieee80211_free_node(data->ni);
686 
687 			if (data->map != NULL)
688 				bus_dmamap_destroy(ring->data_dmat, data->map);
689 		}
690 
691 		free(ring->data, M_DEVBUF);
692 	}
693 
694 	if (ring->data_dmat != NULL)
695 		bus_dma_tag_destroy(ring->data_dmat);
696 }
697 
698 static int
699 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
700 {
701 	struct iwi_rx_data *data;
702 	int i, error;
703 
704 	ring->count = count;
705 	ring->cur = 0;
706 
707 	ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
708 	    M_NOWAIT | M_ZERO);
709 	if (ring->data == NULL) {
710 		device_printf(sc->sc_dev, "could not allocate soft data\n");
711 		error = ENOMEM;
712 		goto fail;
713 	}
714 
715 	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
716 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
717 	    NULL, &ring->data_dmat);
718 	if (error != 0) {
719 		device_printf(sc->sc_dev, "could not create data DMA tag\n");
720 		goto fail;
721 	}
722 
723 	for (i = 0; i < count; i++) {
724 		data = &ring->data[i];
725 
726 		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
727 		if (error != 0) {
728 			device_printf(sc->sc_dev, "could not create DMA map\n");
729 			goto fail;
730 		}
731 
732 		data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
733 		if (data->m == NULL) {
734 			device_printf(sc->sc_dev,
735 			    "could not allocate rx mbuf\n");
736 			error = ENOMEM;
737 			goto fail;
738 		}
739 
740 		error = bus_dmamap_load(ring->data_dmat, data->map,
741 		    mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
742 		    &data->physaddr, 0);
743 		if (error != 0) {
744 			device_printf(sc->sc_dev,
745 			    "could not load rx buf DMA map");
746 			goto fail;
747 		}
748 
749 		data->reg = IWI_CSR_RX_BASE + i * 4;
750 	}
751 
752 	return 0;
753 
754 fail:	iwi_free_rx_ring(sc, ring);
755 	return error;
756 }
757 
758 static void
759 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
760 {
761 	ring->cur = 0;
762 }
763 
764 static void
765 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
766 {
767 	struct iwi_rx_data *data;
768 	int i;
769 
770 	if (ring->data != NULL) {
771 		for (i = 0; i < ring->count; i++) {
772 			data = &ring->data[i];
773 
774 			if (data->m != NULL) {
775 				bus_dmamap_sync(ring->data_dmat, data->map,
776 				    BUS_DMASYNC_POSTREAD);
777 				bus_dmamap_unload(ring->data_dmat, data->map);
778 				m_freem(data->m);
779 			}
780 
781 			if (data->map != NULL)
782 				bus_dmamap_destroy(ring->data_dmat, data->map);
783 		}
784 
785 		free(ring->data, M_DEVBUF);
786 	}
787 
788 	if (ring->data_dmat != NULL)
789 		bus_dma_tag_destroy(ring->data_dmat);
790 }
791 
792 static int
793 iwi_shutdown(device_t dev)
794 {
795 	struct iwi_softc *sc = device_get_softc(dev);
796 
797 	iwi_stop(sc);
798 
799 	return 0;
800 }
801 
802 static int
803 iwi_suspend(device_t dev)
804 {
805 	struct iwi_softc *sc = device_get_softc(dev);
806 
807 	iwi_stop(sc);
808 
809 	return 0;
810 }
811 
812 static int
813 iwi_resume(device_t dev)
814 {
815 	struct iwi_softc *sc = device_get_softc(dev);
816 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
817 
818 	mtx_lock(&sc->sc_mtx);
819 
820 	pci_write_config(dev, 0x41, 0, 1);
821 
822 	if (ifp->if_flags & IFF_UP) {
823 		ifp->if_init(ifp->if_softc);
824 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
825 			ifp->if_start(ifp);
826 	}
827 
828 	mtx_unlock(&sc->sc_mtx);
829 
830 	return 0;
831 }
832 
833 static struct ieee80211_node *
834 iwi_node_alloc(struct ieee80211_node_table *nt)
835 {
836 	struct iwi_node *in;
837 
838 	in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
839 	if (in == NULL)
840 		return NULL;
841 
842 	in->in_station = -1;
843 
844 	return &in->in_node;
845 }
846 
847 static void
848 iwi_node_free(struct ieee80211_node *ni)
849 {
850 	struct ieee80211com *ic = ni->ni_ic;
851 	struct iwi_softc *sc = ic->ic_ifp->if_softc;
852 	struct iwi_node *in = (struct iwi_node *)ni;
853 
854 	if (in->in_station != -1)
855 		free_unr(sc->sc_unr, in->in_station);
856 
857 	sc->sc_node_free(ni);
858 }
859 
860 static int
861 iwi_media_change(struct ifnet *ifp)
862 {
863 	struct iwi_softc *sc = ifp->if_softc;
864 	int error;
865 
866 	mtx_lock(&sc->sc_mtx);
867 
868 	error = ieee80211_media_change(ifp);
869 	if (error != ENETRESET) {
870 		mtx_unlock(&sc->sc_mtx);
871 		return error;
872 	}
873 
874 	if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
875 		iwi_init(sc);
876 
877 	mtx_unlock(&sc->sc_mtx);
878 
879 	return 0;
880 }
881 
882 /*
883  * The firmware automatically adapts the transmit speed.  We report its current
884  * value here.
885  */
886 static void
887 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
888 {
889 	struct iwi_softc *sc = ifp->if_softc;
890 	struct ieee80211com *ic = &sc->sc_ic;
891 #define N(a)	(sizeof (a) / sizeof (a[0]))
892 	static const struct {
893 		uint32_t	val;
894 		int		rate;
895 	} rates[] = {
896 		{ IWI_RATE_DS1,      2 },
897 		{ IWI_RATE_DS2,      4 },
898 		{ IWI_RATE_DS5,     11 },
899 		{ IWI_RATE_DS11,    22 },
900 		{ IWI_RATE_OFDM6,   12 },
901 		{ IWI_RATE_OFDM9,   18 },
902 		{ IWI_RATE_OFDM12,  24 },
903 		{ IWI_RATE_OFDM18,  36 },
904 		{ IWI_RATE_OFDM24,  48 },
905 		{ IWI_RATE_OFDM36,  72 },
906 		{ IWI_RATE_OFDM48,  96 },
907 		{ IWI_RATE_OFDM54, 108 },
908 	};
909 	uint32_t val;
910 	int rate, i;
911 
912 	imr->ifm_status = IFM_AVALID;
913 	imr->ifm_active = IFM_IEEE80211;
914 	if (ic->ic_state == IEEE80211_S_RUN)
915 		imr->ifm_status |= IFM_ACTIVE;
916 
917 	/* read current transmission rate from adapter */
918 	val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
919 
920 	/* convert rate to 802.11 rate */
921 	for (i = 0; i < N(rates) && rates[i].val != val; i++);
922 	rate = (i < N(rates)) ? rates[i].rate : 0;
923 
924 	imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
925 	switch (ic->ic_opmode) {
926 	case IEEE80211_M_STA:
927 		break;
928 
929 	case IEEE80211_M_IBSS:
930 		imr->ifm_active |= IFM_IEEE80211_ADHOC;
931 		break;
932 
933 	case IEEE80211_M_MONITOR:
934 		imr->ifm_active |= IFM_IEEE80211_MONITOR;
935 		break;
936 
937 	case IEEE80211_M_AHDEMO:
938 	case IEEE80211_M_HOSTAP:
939 		/* should not get there */
940 		break;
941 	}
942 #undef N
943 }
944 
945 static int
946 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
947 {
948 	struct ifnet *ifp = ic->ic_ifp;
949 	struct iwi_softc *sc = ifp->if_softc;
950 	enum ieee80211_state ostate;
951 	uint32_t tmp;
952 
953 	ostate = ic->ic_state;
954 
955 	switch (nstate) {
956 	case IEEE80211_S_SCAN:
957 		if (sc->flags & IWI_FLAG_SCANNING)
958 			break;
959 
960 		ieee80211_node_table_reset(&ic->ic_scan);
961 		ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
962 		sc->flags |= IWI_FLAG_SCANNING;
963 		iwi_scan(sc);
964 		break;
965 
966 	case IEEE80211_S_AUTH:
967 		iwi_auth_and_assoc(sc);
968 		break;
969 
970 	case IEEE80211_S_RUN:
971 		if (ic->ic_opmode == IEEE80211_M_IBSS)
972 			iwi_auth_and_assoc(sc);
973 		else if (ic->ic_opmode == IEEE80211_M_MONITOR)
974 			iwi_set_chan(sc, ic->ic_ibss_chan);
975 
976 		/* assoc led on */
977 		tmp = MEM_READ_4(sc, IWI_MEM_EVENT_CTL) & IWI_LED_MASK;
978 		MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, tmp | IWI_LED_ASSOC);
979 
980 		return sc->sc_newstate(ic, nstate,
981 		    IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
982 
983 	case IEEE80211_S_ASSOC:
984 		break;
985 
986 	case IEEE80211_S_INIT:
987 		sc->flags &= ~IWI_FLAG_SCANNING;
988 
989 		if (ostate != IEEE80211_S_RUN)
990 			break;
991 
992 		/* assoc led off */
993 		tmp = MEM_READ_4(sc, IWI_MEM_EVENT_CTL) & IWI_LED_MASK;
994 		MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, tmp & ~IWI_LED_ASSOC);
995 		break;
996 	}
997 
998 	ic->ic_state = nstate;
999 
1000 	return 0;
1001 }
1002 
1003 /*
1004  * WME parameters coming from IEEE 802.11e specification.  These values are
1005  * already declared in ieee80211_proto.c, but they are static so they can't
1006  * be reused here.
1007  */
1008 static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
1009 	{ 0, 3, 5,  7,   0 },	/* WME_AC_BE */
1010 	{ 0, 3, 5, 10,   0 },	/* WME_AC_BK */
1011 	{ 0, 2, 4,  5, 188 },	/* WME_AC_VI */
1012 	{ 0, 2, 3,  4, 102 }	/* WME_AC_VO */
1013 };
1014 
1015 static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
1016 	{ 0, 3, 4,  6,   0 },	/* WME_AC_BE */
1017 	{ 0, 3, 4, 10,   0 },	/* WME_AC_BK */
1018 	{ 0, 2, 3,  4,  94 },	/* WME_AC_VI */
1019 	{ 0, 2, 2,  3,  47 }	/* WME_AC_VO */
1020 };
1021 
1022 static int
1023 iwi_wme_update(struct ieee80211com *ic)
1024 {
1025 #define IWI_EXP2(v)	htole16((1 << (v)) - 1)
1026 #define IWI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
1027 	struct iwi_softc *sc = ic->ic_ifp->if_softc;
1028 	struct iwi_wme_params wme[3];
1029 	const struct wmeParams *wmep;
1030 	int ac;
1031 
1032 	/*
1033 	 * We shall not override firmware default WME values if WME is not
1034 	 * actually enabled.
1035 	 */
1036 	if (!(ic->ic_flags & IEEE80211_F_WME))
1037 		return 0;
1038 
1039 	for (ac = 0; ac < WME_NUM_AC; ac++) {
1040 		/* set WME values for current operating mode */
1041 		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
1042 		wme[0].aifsn[ac] = wmep->wmep_aifsn;
1043 		wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1044 		wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1045 		wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1046 		wme[0].acm[ac]   = wmep->wmep_acm;
1047 
1048 		/* set WME values for CCK modulation */
1049 		wmep = &iwi_wme_cck_params[ac];
1050 		wme[1].aifsn[ac] = wmep->wmep_aifsn;
1051 		wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1052 		wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1053 		wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1054 		wme[1].acm[ac]   = wmep->wmep_acm;
1055 
1056 		/* set WME values for OFDM modulation */
1057 		wmep = &iwi_wme_ofdm_params[ac];
1058 		wme[2].aifsn[ac] = wmep->wmep_aifsn;
1059 		wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1060 		wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1061 		wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1062 		wme[2].acm[ac]   = wmep->wmep_acm;
1063 	}
1064 
1065 	DPRINTF(("Setting WME parameters\n"));
1066 	return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, wme, sizeof wme, 1);
1067 #undef IWI_USEC
1068 #undef IWI_EXP2
1069 }
1070 
1071 /*
1072  * Read 16 bits at address 'addr' from the serial EEPROM.
1073  */
1074 static uint16_t
1075 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1076 {
1077 	uint32_t tmp;
1078 	uint16_t val;
1079 	int n;
1080 
1081 	/* clock C once before the first command */
1082 	IWI_EEPROM_CTL(sc, 0);
1083 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1084 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1085 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1086 
1087 	/* write start bit (1) */
1088 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1089 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1090 
1091 	/* write READ opcode (10) */
1092 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1093 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1094 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1095 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1096 
1097 	/* write address A7-A0 */
1098 	for (n = 7; n >= 0; n--) {
1099 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1100 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1101 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1102 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1103 	}
1104 
1105 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1106 
1107 	/* read data Q15-Q0 */
1108 	val = 0;
1109 	for (n = 15; n >= 0; n--) {
1110 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1111 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1112 		tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1113 		val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1114 	}
1115 
1116 	IWI_EEPROM_CTL(sc, 0);
1117 
1118 	/* clear Chip Select and clock C */
1119 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1120 	IWI_EEPROM_CTL(sc, 0);
1121 	IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1122 
1123 	return val;
1124 }
1125 
1126 /*
1127  * XXX: Hack to set the current channel to the value advertised in beacons or
1128  * probe responses. Only used during AP detection.
1129  */
1130 static void
1131 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
1132 {
1133 	struct ieee80211_frame *wh;
1134 	uint8_t subtype;
1135 	uint8_t *frm, *efrm;
1136 
1137 	wh = mtod(m, struct ieee80211_frame *);
1138 
1139 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1140 		return;
1141 
1142 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1143 
1144 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1145 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1146 		return;
1147 
1148 	frm = (uint8_t *)(wh + 1);
1149 	efrm = mtod(m, uint8_t *) + m->m_len;
1150 
1151 	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1152 	while (frm < efrm) {
1153 		if (*frm == IEEE80211_ELEMID_DSPARMS)
1154 #if IEEE80211_CHAN_MAX < 255
1155 		if (frm[2] <= IEEE80211_CHAN_MAX)
1156 #endif
1157 			ic->ic_curchan = &ic->ic_channels[frm[2]];
1158 
1159 		frm += frm[1] + 2;
1160 	}
1161 }
1162 
1163 static void
1164 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1165     struct iwi_frame *frame)
1166 {
1167 	struct ieee80211com *ic = &sc->sc_ic;
1168 	struct ifnet *ifp = ic->ic_ifp;
1169 	struct mbuf *mnew, *m;
1170 	struct ieee80211_frame *wh;
1171 	struct ieee80211_node *ni;
1172 	int error;
1173 
1174 	DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
1175 	    le16toh(frame->len), frame->chan, frame->rssi_dbm));
1176 
1177 	if (le16toh(frame->len) < sizeof (struct ieee80211_frame) ||
1178 	    le16toh(frame->len) > MCLBYTES)
1179 		return;
1180 
1181 	/*
1182 	 * Try to allocate a new mbuf for this ring element and load it before
1183 	 * processing the current mbuf. If the ring element cannot be loaded,
1184 	 * drop the received packet and reuse the old mbuf. In the unlikely
1185 	 * case that the old mbuf can't be reloaded either, explicitly panic.
1186 	 */
1187 	mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1188 	if (mnew == NULL) {
1189 		ifp->if_ierrors++;
1190 		return;
1191 	}
1192 
1193 	bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1194 
1195 	error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1196 	    mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr,
1197 	    0);
1198 	if (error != 0) {
1199 		m_freem(mnew);
1200 
1201 		/* try to reload the old mbuf */
1202 		error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1203 		    mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
1204 		    &data->physaddr, 0);
1205 		if (error != 0) {
1206 			/* very unlikely that it will fail... */
1207 			panic("%s: could not load old rx mbuf",
1208 			    device_get_name(sc->sc_dev));
1209 		}
1210 		ifp->if_ierrors++;
1211 		return;
1212 	}
1213 
1214 	/*
1215 	 * New mbuf successfully loaded, update Rx ring and continue
1216 	 * processing.
1217 	 */
1218 	m = data->m;
1219 	data->m = mnew;
1220 	CSR_WRITE_4(sc, data->reg, data->physaddr);
1221 
1222 	/* finalize mbuf */
1223 	m->m_pkthdr.rcvif = ifp;
1224 	m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1225 	    sizeof (struct iwi_frame) + le16toh(frame->len);
1226 
1227 	m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1228 
1229 	if (ic->ic_state == IEEE80211_S_SCAN)
1230 		iwi_fix_channel(ic, m);
1231 
1232 	if (sc->sc_drvbpf != NULL) {
1233 		struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1234 
1235 		tap->wr_flags = 0;
1236 		tap->wr_rate = frame->rate;
1237 		tap->wr_chan_freq =
1238 		    htole16(ic->ic_channels[frame->chan].ic_freq);
1239 		tap->wr_chan_flags =
1240 		    htole16(ic->ic_channels[frame->chan].ic_flags);
1241 		tap->wr_antsignal = frame->signal;
1242 		tap->wr_antenna = frame->antenna;
1243 
1244 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1245 	}
1246 
1247 	wh = mtod(m, struct ieee80211_frame *);
1248 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1249 
1250 	/* send the frame to the 802.11 layer */
1251 	ieee80211_input(ic, m, ni, frame->rssi_dbm, 0);
1252 
1253 	/* node is no longer needed */
1254 	ieee80211_free_node(ni);
1255 }
1256 
1257 static void
1258 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1259 {
1260 	struct ieee80211com *ic = &sc->sc_ic;
1261 	struct iwi_notif_scan_channel *chan;
1262 	struct iwi_notif_scan_complete *scan;
1263 	struct iwi_notif_authentication *auth;
1264 	struct iwi_notif_association *assoc;
1265 
1266 	switch (notif->type) {
1267 	case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1268 		chan = (struct iwi_notif_scan_channel *)(notif + 1);
1269 
1270 		DPRINTFN(2, ("Scanning channel (%u)\n", chan->nchan));
1271 		break;
1272 
1273 	case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1274 		scan = (struct iwi_notif_scan_complete *)(notif + 1);
1275 
1276 		DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1277 		    scan->status));
1278 
1279 		/* monitor mode uses scan to set the channel ... */
1280 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1281 			sc->flags &= ~IWI_FLAG_SCANNING;
1282 			ieee80211_end_scan(ic);
1283 		} else
1284 			iwi_set_chan(sc, ic->ic_ibss_chan);
1285 		break;
1286 
1287 	case IWI_NOTIF_TYPE_AUTHENTICATION:
1288 		auth = (struct iwi_notif_authentication *)(notif + 1);
1289 
1290 		DPRINTFN(2, ("Authentication (%u)\n", auth->state));
1291 
1292 		switch (auth->state) {
1293 		case IWI_AUTHENTICATED:
1294 			ieee80211_node_authorize(ic->ic_bss);
1295 			ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1296 			break;
1297 
1298 		case IWI_DEAUTHENTICATED:
1299 			break;
1300 
1301 		default:
1302 			device_printf(sc->sc_dev,
1303 			    "unknown authentication state %u\n", auth->state);
1304 		}
1305 		break;
1306 
1307 	case IWI_NOTIF_TYPE_ASSOCIATION:
1308 		assoc = (struct iwi_notif_association *)(notif + 1);
1309 
1310 		DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
1311 		    assoc->status));
1312 
1313 		switch (assoc->state) {
1314 		case IWI_AUTHENTICATED:
1315 			/* re-association, do nothing */
1316 			break;
1317 
1318 		case IWI_ASSOCIATED:
1319 			ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1320 			break;
1321 
1322 		case IWI_DEASSOCIATED:
1323 			ieee80211_begin_scan(ic, 1);
1324 			break;
1325 
1326 		default:
1327 			device_printf(sc->sc_dev,
1328 			    "unknown association state %u\n", assoc->state);
1329 		}
1330 		break;
1331 
1332 	default:
1333 		DPRINTFN(5, ("Notification (%u)\n", notif->type));
1334 	}
1335 }
1336 
1337 static void
1338 iwi_rx_intr(struct iwi_softc *sc)
1339 {
1340 	struct iwi_rx_data *data;
1341 	struct iwi_hdr *hdr;
1342 	uint32_t hw;
1343 
1344 	hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1345 
1346 	for (; sc->rxq.cur != hw;) {
1347 		data = &sc->rxq.data[sc->rxq.cur];
1348 
1349 		bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1350 		    BUS_DMASYNC_POSTREAD);
1351 
1352 		hdr = mtod(data->m, struct iwi_hdr *);
1353 
1354 		switch (hdr->type) {
1355 		case IWI_HDR_TYPE_FRAME:
1356 			iwi_frame_intr(sc, data, sc->rxq.cur,
1357 			    (struct iwi_frame *)(hdr + 1));
1358 			break;
1359 
1360 		case IWI_HDR_TYPE_NOTIF:
1361 			iwi_notification_intr(sc,
1362 			    (struct iwi_notif *)(hdr + 1));
1363 			break;
1364 
1365 		default:
1366 			device_printf(sc->sc_dev, "unknown hdr type %u\n",
1367 			    hdr->type);
1368 		}
1369 
1370 		DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1371 
1372 		sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1373 	}
1374 
1375 	/* tell the firmware what we have processed */
1376 	hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1377 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1378 }
1379 
1380 static void
1381 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1382 {
1383 	struct ieee80211com *ic = &sc->sc_ic;
1384 	struct ifnet *ifp = ic->ic_ifp;
1385 	struct iwi_tx_data *data;
1386 	uint32_t hw;
1387 
1388 	hw = CSR_READ_4(sc, txq->csr_ridx);
1389 
1390 	for (; txq->next != hw;) {
1391 		data = &txq->data[txq->next];
1392 
1393 		bus_dmamap_sync(txq->data_dmat, data->map,
1394 		    BUS_DMASYNC_POSTWRITE);
1395 		bus_dmamap_unload(txq->data_dmat, data->map);
1396 		m_freem(data->m);
1397 		data->m = NULL;
1398 		ieee80211_free_node(data->ni);
1399 		data->ni = NULL;
1400 
1401 		DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1402 
1403 		ifp->if_opackets++;
1404 
1405 		txq->queued--;
1406 		txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1407 	}
1408 
1409 	sc->sc_tx_timer = 0;
1410 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1411 	iwi_start(ifp);
1412 }
1413 
1414 static void
1415 iwi_intr(void *arg)
1416 {
1417 	struct iwi_softc *sc = arg;
1418 	uint32_t r;
1419 
1420 	mtx_lock(&sc->sc_mtx);
1421 
1422 	if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) {
1423 		mtx_unlock(&sc->sc_mtx);
1424 		return;
1425 	}
1426 
1427 	/* disable interrupts */
1428 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1429 
1430 	/* acknowledge interrupts */
1431 	CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1432 
1433 	if (r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)) {
1434 		device_printf(sc->sc_dev, "firmware error\n");
1435 		taskqueue_enqueue_fast(taskqueue_fast, &sc->sc_init_task);
1436 		r = 0;	/* don't process more interrupts */
1437 	}
1438 
1439 	if (r & IWI_INTR_FW_INITED)
1440 		wakeup(sc);
1441 
1442 	if (r & IWI_INTR_RADIO_OFF) {
1443 		DPRINTF(("radio transmitter turned off\n"));
1444 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1445 		iwi_stop(sc);
1446 		r = 0;	/* don't process more interrupts */
1447 	}
1448 
1449 	if (r & IWI_INTR_CMD_DONE)
1450 		wakeup(sc);
1451 
1452 	if (r & IWI_INTR_TX1_DONE)
1453 		iwi_tx_intr(sc, &sc->txq[0]);
1454 
1455 	if (r & IWI_INTR_TX2_DONE)
1456 		iwi_tx_intr(sc, &sc->txq[1]);
1457 
1458 	if (r & IWI_INTR_TX3_DONE)
1459 		iwi_tx_intr(sc, &sc->txq[2]);
1460 
1461 	if (r & IWI_INTR_TX4_DONE)
1462 		iwi_tx_intr(sc, &sc->txq[3]);
1463 
1464 	if (r & IWI_INTR_RX_DONE)
1465 		iwi_rx_intr(sc);
1466 
1467 	/* re-enable interrupts */
1468 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1469 
1470 	mtx_unlock(&sc->sc_mtx);
1471 }
1472 
1473 static int
1474 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len, int async)
1475 {
1476 	struct iwi_cmd_desc *desc;
1477 
1478 	desc = &sc->cmdq.desc[sc->cmdq.cur];
1479 
1480 	desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1481 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1482 	desc->type = type;
1483 	desc->len = len;
1484 	memcpy(desc->data, data, len);
1485 
1486 	bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map,
1487 	    BUS_DMASYNC_PREWRITE);
1488 
1489 	DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1490 	    type, len));
1491 
1492 	sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1493 	CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1494 
1495 	return async ? 0 : msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz);
1496 }
1497 
1498 static void
1499 iwi_write_ibssnode(struct iwi_softc *sc, const struct iwi_node *in)
1500 {
1501 	struct iwi_ibssnode node;
1502 
1503 	/* write node information into NIC memory */
1504 	memset(&node, 0, sizeof node);
1505 	IEEE80211_ADDR_COPY(node.bssid, in->in_node.ni_macaddr);
1506 
1507 	CSR_WRITE_REGION_1(sc,
1508 	    IWI_CSR_NODE_BASE + in->in_station * sizeof node,
1509 	    (uint8_t *)&node, sizeof node);
1510 }
1511 
1512 static int
1513 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni,
1514     int ac)
1515 {
1516 	struct iwi_softc *sc = ifp->if_softc;
1517 	struct ieee80211com *ic = &sc->sc_ic;
1518 	struct iwi_node *in = (struct iwi_node *)ni;
1519 	struct ieee80211_frame *wh;
1520 	struct ieee80211_key *k;
1521 	const struct chanAccParams *cap;
1522 	struct iwi_tx_ring *txq = &sc->txq[ac];
1523 	struct iwi_tx_data *data;
1524 	struct iwi_tx_desc *desc;
1525 	struct mbuf *mnew;
1526 	bus_dma_segment_t segs[IWI_MAX_NSEG];
1527 	int error, nsegs, hdrlen, i, noack = 0;
1528 
1529 	wh = mtod(m0, struct ieee80211_frame *);
1530 
1531 	if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
1532 		hdrlen = sizeof (struct ieee80211_qosframe);
1533 		cap = &ic->ic_wme.wme_chanParams;
1534 		noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1535 	} else
1536 		hdrlen = sizeof (struct ieee80211_frame);
1537 
1538 	/*
1539 	 * This is only used in IBSS mode where the firmware expect an index
1540 	 * in a h/w table instead of a destination address.
1541 	 */
1542 	if (ic->ic_opmode == IEEE80211_M_IBSS && in->in_station == -1) {
1543 		in->in_station = alloc_unr(sc->sc_unr);
1544 		if (in->in_station == -1) {	/* h/w table is full */
1545 			m_freem(m0);
1546 			ieee80211_free_node(ni);
1547 			ifp->if_oerrors++;
1548 			return 0;
1549 		}
1550 		iwi_write_ibssnode(sc, in);
1551 	}
1552 
1553 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1554 		k = ieee80211_crypto_encap(ic, ni, m0);
1555 		if (k == NULL) {
1556 			m_freem(m0);
1557 			return ENOBUFS;
1558 		}
1559 
1560 		/* packet header may have moved, reset our local pointer */
1561 		wh = mtod(m0, struct ieee80211_frame *);
1562 	}
1563 
1564 	if (sc->sc_drvbpf != NULL) {
1565 		struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1566 
1567 		tap->wt_flags = 0;
1568 		tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1569 		tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1570 
1571 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1572 	}
1573 
1574 	data = &txq->data[txq->cur];
1575 	desc = &txq->desc[txq->cur];
1576 
1577 	/* save and trim IEEE802.11 header */
1578 	m_copydata(m0, 0, hdrlen, (caddr_t)&desc->wh);
1579 	m_adj(m0, hdrlen);
1580 
1581 	error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0, segs,
1582 	    &nsegs, 0);
1583 	if (error != 0 && error != EFBIG) {
1584 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1585 		    error);
1586 		m_freem(m0);
1587 		return error;
1588 	}
1589 	if (error != 0) {
1590 		mnew = m_defrag(m0, M_DONTWAIT);
1591 		if (mnew == NULL) {
1592 			device_printf(sc->sc_dev,
1593 			    "could not defragment mbuf\n");
1594 			m_freem(m0);
1595 			return ENOBUFS;
1596 		}
1597 		m0 = mnew;
1598 
1599 		error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map,
1600 		    m0, segs, &nsegs, 0);
1601 		if (error != 0) {
1602 			device_printf(sc->sc_dev,
1603 			    "could not map mbuf (error %d)\n", error);
1604 			m_freem(m0);
1605 			return error;
1606 		}
1607 	}
1608 
1609 	data->m = m0;
1610 	data->ni = ni;
1611 
1612 	desc->hdr.type = IWI_HDR_TYPE_DATA;
1613 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1614 	desc->station =
1615 	    (ic->ic_opmode == IEEE80211_M_IBSS) ? in->in_station : 0;
1616 	desc->cmd = IWI_DATA_CMD_TX;
1617 	desc->len = htole16(m0->m_pkthdr.len);
1618 	desc->flags = 0;
1619 	desc->xflags = 0;
1620 
1621 	if (!noack && !IEEE80211_IS_MULTICAST(desc->wh.i_addr1))
1622 		desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1623 
1624 #if 0
1625 	if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1626 		desc->wh.i_fc[1] |= IEEE80211_FC1_WEP;
1627 		desc->weptxkey = ic->ic_crypto.cs_def_txkey;
1628 	} else
1629 #endif
1630 		desc->flags |= IWI_DATA_FLAG_NO_WEP;
1631 
1632 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1633 		desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1634 
1635 	if (desc->wh.i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS)
1636 		desc->xflags |= IWI_DATA_XFLAG_QOS;
1637 
1638 	desc->nseg = htole32(nsegs);
1639 	for (i = 0; i < nsegs; i++) {
1640 		desc->seg_addr[i] = htole32(segs[i].ds_addr);
1641 		desc->seg_len[i]  = htole16(segs[i].ds_len);
1642 	}
1643 
1644 	bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1645 	bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1646 
1647 	DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
1648 	    ac, txq->cur, le16toh(desc->len), nsegs));
1649 
1650 	txq->queued++;
1651 	txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
1652 	CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1653 
1654 	return 0;
1655 }
1656 
1657 static void
1658 iwi_start(struct ifnet *ifp)
1659 {
1660 	struct iwi_softc *sc = ifp->if_softc;
1661 	struct ieee80211com *ic = &sc->sc_ic;
1662 	struct mbuf *m0;
1663 	struct ether_header *eh;
1664 	struct ieee80211_node *ni;
1665 	int ac;
1666 
1667 	mtx_lock(&sc->sc_mtx);
1668 
1669 	if (ic->ic_state != IEEE80211_S_RUN) {
1670 		mtx_unlock(&sc->sc_mtx);
1671 		return;
1672 	}
1673 
1674 	for (;;) {
1675 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
1676 		if (m0 == NULL)
1677 			break;
1678 
1679 		if (m0->m_len < sizeof (struct ether_header) &&
1680 		    (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) {
1681 			ifp->if_oerrors++;
1682 			continue;
1683 		}
1684 		eh = mtod(m0, struct ether_header *);
1685 		ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1686 		if (ni == NULL) {
1687 			m_freem(m0);
1688 			ifp->if_oerrors++;
1689 			continue;
1690 		}
1691 
1692 		/* classify mbuf so we can find which tx ring to use */
1693 		if (ieee80211_classify(ic, m0, ni) != 0) {
1694 			m_freem(m0);
1695 			ieee80211_free_node(ni);
1696 			ifp->if_oerrors++;
1697 			continue;
1698 		}
1699 
1700 		/* no QoS encapsulation for EAPOL frames */
1701 		ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1702 		    M_WME_GETAC(m0) : WME_AC_BE;
1703 
1704 		if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) {
1705 			/* there is no place left in this ring */
1706 			IFQ_DRV_PREPEND(&ifp->if_snd, m0);
1707 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1708 			break;
1709 		}
1710 
1711 		BPF_MTAP(ifp, m0);
1712 
1713 		m0 = ieee80211_encap(ic, m0, ni);
1714 		if (m0 == NULL) {
1715 			ieee80211_free_node(ni);
1716 			ifp->if_oerrors++;
1717 			continue;
1718 		}
1719 
1720 		if (ic->ic_rawbpf != NULL)
1721 			bpf_mtap(ic->ic_rawbpf, m0);
1722 
1723 		if (iwi_tx_start(ifp, m0, ni, ac) != 0) {
1724 			ieee80211_free_node(ni);
1725 			ifp->if_oerrors++;
1726 			break;
1727 		}
1728 
1729 		sc->sc_tx_timer = 5;
1730 		ifp->if_timer = 1;
1731 	}
1732 
1733 	mtx_unlock(&sc->sc_mtx);
1734 }
1735 
1736 static void
1737 iwi_watchdog(struct ifnet *ifp)
1738 {
1739 	struct iwi_softc *sc = ifp->if_softc;
1740 	struct ieee80211com *ic = &sc->sc_ic;
1741 
1742 	mtx_lock(&sc->sc_mtx);
1743 
1744 	ifp->if_timer = 0;
1745 
1746 	if (sc->sc_tx_timer > 0) {
1747 		if (--sc->sc_tx_timer == 0) {
1748 			if_printf(ifp, "device timeout\n");
1749 			ifp->if_oerrors++;
1750 			taskqueue_enqueue_fast(taskqueue_fast,
1751 			    &sc->sc_init_task);
1752 			mtx_unlock(&sc->sc_mtx);
1753 			return;
1754 		}
1755 		ifp->if_timer = 1;
1756 	}
1757 
1758 	ieee80211_watchdog(ic);
1759 
1760 	mtx_unlock(&sc->sc_mtx);
1761 }
1762 
1763 static int
1764 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1765 {
1766 	struct iwi_softc *sc = ifp->if_softc;
1767 	struct ieee80211com *ic = &sc->sc_ic;
1768 	int error = 0;
1769 
1770 	mtx_lock(&sc->sc_mtx);
1771 
1772 	switch (cmd) {
1773 	case SIOCSIFFLAGS:
1774 		if (ifp->if_flags & IFF_UP) {
1775 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1776 				iwi_init(sc);
1777 		} else {
1778 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1779 				iwi_stop(sc);
1780 		}
1781 		break;
1782 
1783 	default:
1784 		error = ieee80211_ioctl(ic, cmd, data);
1785 	}
1786 
1787 	if (error == ENETRESET) {
1788 		if ((ifp->if_flags & IFF_UP) &&
1789 		    (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1790 		    (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1791 			iwi_init(sc);
1792 		error = 0;
1793 	}
1794 
1795 	mtx_unlock(&sc->sc_mtx);
1796 
1797 	return error;
1798 }
1799 
1800 static void
1801 iwi_stop_master(struct iwi_softc *sc)
1802 {
1803 	uint32_t tmp;
1804 	int ntries;
1805 
1806 	/* disable interrupts */
1807 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1808 
1809 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1810 	for (ntries = 0; ntries < 5; ntries++) {
1811 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1812 			break;
1813 		DELAY(10);
1814 	}
1815 	if (ntries == 5)
1816 		device_printf(sc->sc_dev, "timeout waiting for master\n");
1817 
1818 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
1819 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
1820 
1821 	sc->flags &= ~IWI_FLAG_FW_INITED;
1822 }
1823 
1824 static int
1825 iwi_reset(struct iwi_softc *sc)
1826 {
1827 	uint32_t tmp;
1828 	int i, ntries;
1829 
1830 	iwi_stop_master(sc);
1831 
1832 	tmp = CSR_READ_4(sc, IWI_CSR_CTL);
1833 	CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
1834 
1835 	CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
1836 
1837 	/* wait for clock stabilization */
1838 	for (ntries = 0; ntries < 1000; ntries++) {
1839 		if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
1840 			break;
1841 		DELAY(200);
1842 	}
1843 	if (ntries == 1000) {
1844 		device_printf(sc->sc_dev,
1845 		    "timeout waiting for clock stabilization\n");
1846 		return EIO;
1847 	}
1848 
1849 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
1850 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET);
1851 
1852 	DELAY(10);
1853 
1854 	tmp = CSR_READ_4(sc, IWI_CSR_CTL);
1855 	CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
1856 
1857 	/* clear NIC memory */
1858 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
1859 	for (i = 0; i < 0xc000; i++)
1860 		CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1861 
1862 	return 0;
1863 }
1864 
1865 static int
1866 iwi_load_ucode(struct iwi_softc *sc, const char *data, int size)
1867 {
1868 	const uint16_t *w;
1869 	uint32_t tmp;
1870 	int i, ntries;
1871 
1872 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1873 	    IWI_RST_STOP_MASTER);
1874 	for (ntries = 0; ntries < 5; ntries++) {
1875 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1876 			break;
1877 		DELAY(10);
1878 	}
1879 	if (ntries == 5) {
1880 		device_printf(sc->sc_dev, "timeout waiting for master\n");
1881 		return EIO;
1882 	}
1883 
1884 	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1885 	DELAY(5000);
1886 
1887 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
1888 	tmp &= ~IWI_RST_PRINCETON_RESET;
1889 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
1890 
1891 	DELAY(5000);
1892 	MEM_WRITE_4(sc, 0x3000e0, 0);
1893 	DELAY(1000);
1894 	MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, 1);
1895 	DELAY(1000);
1896 	MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, 0);
1897 	DELAY(1000);
1898 	MEM_WRITE_1(sc, 0x200000, 0x00);
1899 	MEM_WRITE_1(sc, 0x200000, 0x40);
1900 	DELAY(1000);
1901 
1902 	/* write microcode into adapter memory */
1903 	for (w = (const uint16_t *)data; size > 0; w++, size -= 2)
1904 		MEM_WRITE_2(sc, 0x200010, htole16(*w));
1905 
1906 	MEM_WRITE_1(sc, 0x200000, 0x00);
1907 	MEM_WRITE_1(sc, 0x200000, 0x80);
1908 
1909 	/* wait until we get an answer */
1910 	for (ntries = 0; ntries < 100; ntries++) {
1911 		if (MEM_READ_1(sc, 0x200000) & 1)
1912 			break;
1913 		DELAY(100);
1914 	}
1915 	if (ntries == 100) {
1916 		device_printf(sc->sc_dev,
1917 		    "timeout waiting for ucode to initialize\n");
1918 		return EIO;
1919 	}
1920 
1921 	/* read the answer or the firmware will not initialize properly */
1922 	for (i = 0; i < 7; i++)
1923 		MEM_READ_4(sc, 0x200004);
1924 
1925 	MEM_WRITE_1(sc, 0x200000, 0x00);
1926 
1927 	return 0;
1928 }
1929 
1930 /* macro to handle unaligned little endian data in firmware image */
1931 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1932 
1933 static int
1934 iwi_load_firmware(struct iwi_softc *sc, const char *data, int size)
1935 {
1936 	bus_dma_tag_t dmat;
1937 	bus_dmamap_t map;
1938 	bus_addr_t physaddr;
1939 	void *virtaddr;
1940 	u_char *p, *end;
1941 	uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp;
1942 	int ntries, error;
1943 
1944 	/* drop the driver's lock before doing any DMA operation */
1945 	mtx_unlock(&sc->sc_mtx);
1946 
1947 	/* allocate DMA memory for mapping firmware image */
1948 	error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
1949 	    BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size, 0, NULL, NULL, &dmat);
1950 	if (error != 0) {
1951 		device_printf(sc->sc_dev,
1952 		    "could not create firmware DMA tag\n");
1953 		mtx_lock(&sc->sc_mtx);
1954 		goto fail1;
1955 	}
1956 
1957 	error = bus_dmamem_alloc(dmat, &virtaddr, BUS_DMA_NOWAIT, &map);
1958 	if (error != 0) {
1959 		device_printf(sc->sc_dev,
1960 		    "could not allocate firmware DMA memory\n");
1961 		mtx_lock(&sc->sc_mtx);
1962 		goto fail2;
1963 	}
1964 
1965 	error = bus_dmamap_load(dmat, map, virtaddr, size, iwi_dma_map_addr,
1966 	    &physaddr, 0);
1967 	if (error != 0) {
1968 		device_printf(sc->sc_dev, "could not load firmware DMA map\n");
1969 		mtx_lock(&sc->sc_mtx);
1970 		goto fail3;
1971 	}
1972 
1973 	/* copy firmware image to DMA memory */
1974 	memcpy(virtaddr, data, size);
1975 
1976 	/* make sure the adapter will get up-to-date values */
1977 	bus_dmamap_sync(dmat, map, BUS_DMASYNC_PREWRITE);
1978 
1979 	/* reacquire the driver's lock before kicking the hardware */
1980 	mtx_lock(&sc->sc_mtx);
1981 
1982 	/* tell the adapter where the command blocks are stored */
1983 	MEM_WRITE_4(sc, 0x3000a0, 0x27000);
1984 
1985 	/*
1986 	 * Store command blocks into adapter's internal memory using register
1987 	 * indirections. The adapter will read the firmware image through DMA
1988 	 * using information stored in command blocks.
1989 	 */
1990 	src = physaddr;
1991 	p = virtaddr;
1992 	end = p + size;
1993 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
1994 
1995 	while (p < end) {
1996 		dst = GETLE32(p); p += 4; src += 4;
1997 		len = GETLE32(p); p += 4; src += 4;
1998 		p += len;
1999 
2000 		while (len > 0) {
2001 			mlen = min(len, IWI_CB_MAXDATALEN);
2002 
2003 			ctl = IWI_CB_DEFAULT_CTL | mlen;
2004 			sum = ctl ^ src ^ dst;
2005 
2006 			/* write a command block */
2007 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2008 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2009 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2010 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2011 
2012 			src += mlen;
2013 			dst += mlen;
2014 			len -= mlen;
2015 		}
2016 	}
2017 
2018 	/* write a fictive final command block (sentinel) */
2019 	sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2020 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2021 
2022 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
2023 	tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
2024 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2025 
2026 	/* tell the adapter to start processing command blocks */
2027 	MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2028 
2029 	/* wait until the adapter reaches the sentinel */
2030 	for (ntries = 0; ntries < 400; ntries++) {
2031 		if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2032 			break;
2033 		DELAY(100);
2034 	}
2035 	if (ntries == 400) {
2036 		device_printf(sc->sc_dev,
2037 		    "timeout processing command blocks\n");
2038 		error = EIO;
2039 		goto fail4;
2040 	}
2041 
2042 	/* we're done with command blocks processing */
2043 	MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2044 
2045 	/* allow interrupts so we know when the firmware is ready */
2046 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2047 
2048 	/* tell the adapter to initialize the firmware */
2049 	CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2050 
2051 	tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2052 	CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
2053 
2054 	/* wait at most one second for firmware initialization to complete */
2055 	if ((error = msleep(sc, &sc->sc_mtx, 0, "iwiinit", hz)) != 0) {
2056 		device_printf(sc->sc_dev, "timeout waiting for firmware "
2057 		    "initialization to complete\n");
2058 	}
2059 
2060 fail4:	bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTWRITE);
2061 	bus_dmamap_unload(dmat, map);
2062 fail3:	bus_dmamem_free(dmat, virtaddr, map);
2063 fail2:	bus_dma_tag_destroy(dmat);
2064 fail1:
2065 	return error;
2066 }
2067 
2068 static int
2069 iwi_config(struct iwi_softc *sc)
2070 {
2071 	struct ieee80211com *ic = &sc->sc_ic;
2072 	struct ifnet *ifp = ic->ic_ifp;
2073 	struct iwi_configuration config;
2074 	struct iwi_rateset rs;
2075 	struct iwi_txpower power;
2076 	struct ieee80211_key *wk;
2077 	struct iwi_wep_key wepkey;
2078 	uint32_t data;
2079 	int error, i;
2080 
2081 	IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2082 	DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":"));
2083 	error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2084 	    IEEE80211_ADDR_LEN, 0);
2085 	if (error != 0)
2086 		return error;
2087 
2088 	memset(&config, 0, sizeof config);
2089 	config.bluetooth_coexistence = sc->bluetooth;
2090 	config.antenna = sc->antenna;
2091 	config.multicast_enabled = 1;
2092 	config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2093 	config.disable_unicast_decryption = 1;
2094 	config.disable_multicast_decryption = 1;
2095 	DPRINTF(("Configuring adapter\n"));
2096 	error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 0);
2097 	if (error != 0)
2098 		return error;
2099 
2100 	data = htole32(IWI_POWER_MODE_CAM);
2101 	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2102 	error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
2103 	if (error != 0)
2104 		return error;
2105 
2106 	data = htole32(ic->ic_rtsthreshold);
2107 	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2108 	error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
2109 	if (error != 0)
2110 		return error;
2111 
2112 	data = htole32(ic->ic_fragthreshold);
2113 	DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2114 	error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
2115 	if (error != 0)
2116 		return error;
2117 
2118 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2119 		power.mode = IWI_MODE_11B;
2120 		power.nchan = 11;
2121 		for (i = 0; i < 11; i++) {
2122 			power.chan[i].chan = i + 1;
2123 			power.chan[i].power = IWI_TXPOWER_MAX;
2124 		}
2125 		DPRINTF(("Setting .11b channels tx power\n"));
2126 		error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2127 		    0);
2128 		if (error != 0)
2129 			return error;
2130 
2131 		power.mode = IWI_MODE_11G;
2132 		DPRINTF(("Setting .11g channels tx power\n"));
2133 		error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2134 		    0);
2135 		if (error != 0)
2136 			return error;
2137 	}
2138 
2139 	rs.mode = IWI_MODE_11G;
2140 	rs.type = IWI_RATESET_TYPE_SUPPORTED;
2141 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2142 	memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2143 	    rs.nrates);
2144 	DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2145 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2146 	if (error != 0)
2147 		return error;
2148 
2149 	rs.mode = IWI_MODE_11A;
2150 	rs.type = IWI_RATESET_TYPE_SUPPORTED;
2151 	rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2152 	memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2153 	    rs.nrates);
2154 	DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2155 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2156 	if (error != 0)
2157 		return error;
2158 
2159 	/* if we have a desired ESSID, set it now */
2160 	if (ic->ic_des_esslen != 0) {
2161 #ifdef IWI_DEBUG
2162 		if (iwi_debug > 0) {
2163 			printf("Setting desired ESSID to ");
2164 			ieee80211_print_essid(ic->ic_des_essid,
2165 			    ic->ic_des_esslen);
2166 			printf("\n");
2167 		}
2168 #endif
2169 		error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_essid,
2170 		    ic->ic_des_esslen, 0);
2171 		if (error != 0)
2172 			return error;
2173 	}
2174 
2175 	data = htole32(arc4random());
2176 	DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2177 	error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
2178 	if (error != 0)
2179 		return error;
2180 
2181 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2182 		wk = &ic->ic_crypto.cs_nw_keys[i];
2183 
2184 		wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2185 		wepkey.idx = i;
2186 		wepkey.len = wk->wk_keylen;
2187 		memset(wepkey.key, 0, sizeof wepkey.key);
2188 		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2189 		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2190 		    wepkey.len));
2191 		error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2192 		    sizeof wepkey, 0);
2193 		if (error != 0)
2194 			return error;
2195 	}
2196 
2197 	/* enable adapter */
2198 	DPRINTF(("Enabling adapter\n"));
2199 	return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
2200 }
2201 
2202 static int
2203 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
2204 {
2205 	struct ieee80211com *ic = &sc->sc_ic;
2206 	struct iwi_scan scan;
2207 
2208 	memset(&scan, 0, sizeof scan);
2209 	memset(scan.type, IWI_SCAN_TYPE_PASSIVE, sizeof scan.type);
2210 	scan.passive = htole16(2000);
2211 	scan.channels[0] = 1 |
2212 	    (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ);
2213 	scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2214 
2215 	DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
2216 	return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2217 }
2218 
2219 static int
2220 iwi_scan(struct iwi_softc *sc)
2221 {
2222 	struct ieee80211com *ic = &sc->sc_ic;
2223 	struct iwi_scan scan;
2224 	uint8_t *p;
2225 	int i, count;
2226 
2227 	memset(&scan, 0, sizeof scan);
2228 
2229 	if (ic->ic_des_esslen != 0) {
2230 		scan.bdirected = htole16(sc->dwelltime);
2231 		memset(scan.type, IWI_SCAN_TYPE_BDIRECTED, sizeof scan.type);
2232 	} else {
2233 		scan.broadcast = htole16(sc->dwelltime);
2234 		memset(scan.type, IWI_SCAN_TYPE_BROADCAST, sizeof scan.type);
2235 	}
2236 
2237 	p = scan.channels;
2238 	count = 0;
2239 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2240 		if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
2241 		    isset(ic->ic_chan_active, i)) {
2242 			*++p = i;
2243 			count++;
2244 		}
2245 	}
2246 	*(p - count) = IWI_CHAN_5GHZ | count;
2247 
2248 	p = (count > 0) ? p + 1 : scan.channels;
2249 	count = 0;
2250 	for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2251 		if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
2252 		    isset(ic->ic_chan_active, i)) {
2253 			*++p = i;
2254 			count++;
2255 		}
2256 	}
2257 	*(p - count) = IWI_CHAN_2GHZ | count;
2258 
2259 	DPRINTF(("Start scanning\n"));
2260 	return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2261 }
2262 
2263 static int
2264 iwi_auth_and_assoc(struct iwi_softc *sc)
2265 {
2266 	struct ieee80211com *ic = &sc->sc_ic;
2267 	struct ifnet *ifp = ic->ic_ifp;
2268 	struct ieee80211_node *ni = ic->ic_bss;
2269 	struct ieee80211_wme_info wme;
2270 	struct iwi_configuration config;
2271 	struct iwi_associate assoc;
2272 	struct iwi_rateset rs;
2273 	uint16_t capinfo;
2274 	uint32_t data;
2275 	int error;
2276 
2277 	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2278 		memset(&config, 0, sizeof config);
2279 		config.bluetooth_coexistence = sc->bluetooth;
2280 		config.antenna = sc->antenna;
2281 		config.multicast_enabled = 1;
2282 		config.use_protection = 1;
2283 		config.answer_pbreq =
2284 		    (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2285 		config.disable_unicast_decryption = 1;
2286 		config.disable_multicast_decryption = 1;
2287 		DPRINTF(("Configuring adapter\n"));
2288 		error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config,
2289 		    1);
2290 		if (error != 0)
2291 			return error;
2292 	}
2293 
2294 #ifdef IWI_DEBUG
2295 	if (iwi_debug > 0) {
2296 		printf("Setting ESSID to ");
2297 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2298 		printf("\n");
2299 	}
2300 #endif
2301 	error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2302 	if (error != 0)
2303 		return error;
2304 
2305 	/* the rate set has already been "negotiated" */
2306 	rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2307 	    IWI_MODE_11G;
2308 	rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2309 	rs.nrates = ni->ni_rates.rs_nrates;
2310 	memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2311 	DPRINTF(("Setting negociated rates (%u)\n", rs.nrates));
2312 	error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2313 	if (error != 0)
2314 		return error;
2315 
2316 	if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) {
2317 		wme.wme_id = IEEE80211_ELEMID_VENDOR;
2318 		wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
2319 		wme.wme_oui[0] = 0x00;
2320 		wme.wme_oui[1] = 0x50;
2321 		wme.wme_oui[2] = 0xf2;
2322 		wme.wme_type = WME_OUI_TYPE;
2323 		wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
2324 		wme.wme_version = WME_VERSION;
2325 		wme.wme_info = 0;
2326 
2327 		DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
2328 		error = iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme, 1);
2329 		if (error != 0)
2330 			return error;
2331 	}
2332 
2333 	if (ic->ic_opt_ie != NULL) {
2334 		DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
2335 		error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
2336 		    ic->ic_opt_ie_len, 1);
2337 		if (error != 0)
2338 			return error;
2339 	}
2340 
2341 	data = htole32(ni->ni_rssi);
2342 	DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2343 	error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2344 	if (error != 0)
2345 		return error;
2346 
2347 	memset(&assoc, 0, sizeof assoc);
2348 	assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2349 	    IWI_MODE_11G;
2350 	assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2351 	if (ni->ni_authmode == IEEE80211_AUTH_SHARED)
2352 		assoc.auth = ic->ic_crypto.cs_def_txkey << 4 | IWI_AUTH_SHARED;
2353 	if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
2354 		assoc.policy |= htole16(IWI_POLICY_WME);
2355 	if (ic->ic_flags & IEEE80211_F_WPA)
2356 		assoc.policy |= htole16(IWI_POLICY_WPA);
2357 	memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
2358 
2359 	if (ic->ic_opmode == IEEE80211_M_IBSS)
2360 		capinfo = IEEE80211_CAPINFO_IBSS;
2361 	else
2362 		capinfo = IEEE80211_CAPINFO_ESS;
2363 	if (ic->ic_flags & IEEE80211_F_PRIVACY)
2364 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
2365 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2366 	    IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2367 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2368 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
2369 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2370 	assoc.capinfo = htole16(capinfo);
2371 
2372 	assoc.lintval = htole16(ic->ic_lintval);
2373 	assoc.intval = htole16(ni->ni_intval);
2374 	IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
2375 	if (ic->ic_opmode == IEEE80211_M_IBSS)
2376 		IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr);
2377 	else
2378 		IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
2379 
2380 	DPRINTF(("Trying to associate to %6D channel %u auth %u\n",
2381 	    assoc.bssid, ":", assoc.chan, assoc.auth));
2382 	return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
2383 }
2384 
2385 /*
2386  * Handler for sc_init_task.  This is a simple wrapper around iwi_init().
2387  * It is called on firmware panics or on watchdog timeouts.
2388  */
2389 static void
2390 iwi_init_task(void *context, int pending)
2391 {
2392 	iwi_init(context);
2393 }
2394 
2395 static void
2396 iwi_init(void *priv)
2397 {
2398 	struct iwi_softc *sc = priv;
2399 	struct ieee80211com *ic = &sc->sc_ic;
2400 	struct ifnet *ifp = ic->ic_ifp;
2401 	struct iwi_rx_data *data;
2402 	struct firmware *fp;
2403 	const struct iwi_firmware_hdr *hdr;
2404 	const char *imagename, *fw;
2405 	int owned, i;
2406 
2407 	/*
2408 	 * iwi_init() is exposed through ifp->if_init so it might be called
2409 	 * without the driver's lock held.  Since msleep() doesn't like being
2410 	 * called on a recursed mutex, we acquire the driver's lock only if
2411 	 * we're not already holding it.
2412 	 */
2413 	if (!(owned = mtx_owned(&sc->sc_mtx)))
2414 		mtx_lock(&sc->sc_mtx);
2415 
2416 	/*
2417 	 * Avoid re-entrant calls.  We need to release the mutex in iwi_init()
2418 	 * when loading the firmware and allocating DMA memory and we don't
2419 	 * want to be called during these operations.
2420 	 */
2421 	if (sc->flags & IWI_FLAG_INIT_LOCKED) {
2422 		if (!owned)
2423 			mtx_unlock(&sc->sc_mtx);
2424 		return;
2425 	}
2426 	sc->flags |= IWI_FLAG_INIT_LOCKED;
2427 
2428 	iwi_stop(sc);
2429 
2430 	if (iwi_reset(sc) != 0) {
2431 		device_printf(sc->sc_dev, "could not reset adapter\n");
2432 		goto fail1;
2433 	}
2434 
2435 	switch (ic->ic_opmode) {
2436 	case IEEE80211_M_STA:
2437 		imagename = "iwi_bss";
2438 		break;
2439 	case IEEE80211_M_IBSS:
2440 		imagename = "iwi_ibss";
2441 		break;
2442 	case IEEE80211_M_MONITOR:
2443 		imagename = "iwi_monitor";
2444 		break;
2445 	default:
2446 		imagename = NULL;	/* should not get there */
2447 	}
2448 
2449 	/*
2450 	 * Load firmware image using the firmware(9) subsystem.  We need to
2451 	 * release the driver's lock first.
2452 	 */
2453 	mtx_unlock(&sc->sc_mtx);
2454 	fp = firmware_get(imagename);
2455 	mtx_lock(&sc->sc_mtx);
2456 
2457 	if (fp == NULL) {
2458 		device_printf(sc->sc_dev,
2459 		    "could not load firmware image '%s'\n", imagename);
2460 		goto fail1;
2461 	}
2462 
2463 	if (fp->datasize < sizeof *hdr) {
2464 		device_printf(sc->sc_dev,
2465 		    "firmware image too short: %zu bytes\n", fp->datasize);
2466 		goto fail2;
2467 	}
2468 
2469 	hdr = (const struct iwi_firmware_hdr *)fp->data;
2470 
2471 	if (fp->datasize < sizeof *hdr + le32toh(hdr->bootsz) +
2472 	    le32toh(hdr->ucodesz) + le32toh(hdr->mainsz)) {
2473 		device_printf(sc->sc_dev,
2474 		    "firmware image too short: %zu bytes\n", fp->datasize);
2475 		goto fail2;
2476 	}
2477 
2478 	fw = (const char *)fp->data + sizeof *hdr;
2479 	if (iwi_load_firmware(sc, fw, le32toh(hdr->bootsz)) != 0) {
2480 		device_printf(sc->sc_dev, "could not load boot firmware\n");
2481 		goto fail2;
2482 	}
2483 
2484 	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->bootsz);
2485 	if (iwi_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2486 		device_printf(sc->sc_dev, "could not load microcode\n");
2487 		goto fail2;
2488 	}
2489 
2490 	iwi_stop_master(sc);
2491 
2492 	CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr);
2493 	CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
2494 	CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
2495 
2496 	CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr);
2497 	CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
2498 	CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
2499 
2500 	CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr);
2501 	CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
2502 	CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
2503 
2504 	CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr);
2505 	CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
2506 	CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
2507 
2508 	CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr);
2509 	CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
2510 	CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
2511 
2512 	for (i = 0; i < sc->rxq.count; i++) {
2513 		data = &sc->rxq.data[i];
2514 		CSR_WRITE_4(sc, data->reg, data->physaddr);
2515 	}
2516 
2517 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1);
2518 
2519 	fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->bootsz) +
2520 	    le32toh(hdr->ucodesz);
2521 	if (iwi_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2522 		device_printf(sc->sc_dev, "could not load main firmware\n");
2523 		goto fail2;
2524 	}
2525 
2526 	firmware_put(fp, FIRMWARE_UNLOAD);
2527 	sc->flags |= IWI_FLAG_FW_INITED;
2528 
2529 	if (iwi_config(sc) != 0) {
2530 		device_printf(sc->sc_dev, "device configuration failed\n");
2531 		goto fail1;
2532 	}
2533 
2534 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2535 		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2536 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2537 	} else
2538 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2539 
2540 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2541 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2542 
2543 	sc->flags &=~ IWI_FLAG_INIT_LOCKED;
2544 
2545 	if (!owned)
2546 		mtx_unlock(&sc->sc_mtx);
2547 
2548 	return;
2549 
2550 fail2:	firmware_put(fp, FIRMWARE_UNLOAD);
2551 fail1:	ifp->if_flags &= ~IFF_UP;
2552 	iwi_stop(sc);
2553 	sc->flags &=~ IWI_FLAG_INIT_LOCKED;
2554 	if (!owned)
2555 		mtx_unlock(&sc->sc_mtx);
2556 }
2557 
2558 static void
2559 iwi_stop(void *priv)
2560 {
2561 	struct iwi_softc *sc = priv;
2562 	struct ieee80211com *ic = &sc->sc_ic;
2563 	struct ifnet *ifp = ic->ic_ifp;
2564 
2565 	mtx_lock(&sc->sc_mtx);
2566 
2567 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2568 
2569 	iwi_stop_master(sc);
2570 
2571 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET);
2572 
2573 	/* reset rings */
2574 	iwi_reset_cmd_ring(sc, &sc->cmdq);
2575 	iwi_reset_tx_ring(sc, &sc->txq[0]);
2576 	iwi_reset_tx_ring(sc, &sc->txq[1]);
2577 	iwi_reset_tx_ring(sc, &sc->txq[2]);
2578 	iwi_reset_tx_ring(sc, &sc->txq[3]);
2579 	iwi_reset_rx_ring(sc, &sc->rxq);
2580 
2581 	sc->sc_tx_timer = 0;
2582 	ifp->if_timer = 0;
2583 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2584 
2585 	mtx_unlock(&sc->sc_mtx);
2586 }
2587 
2588 static int
2589 iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
2590 {
2591 	struct iwi_softc *sc = arg1;
2592 	uint32_t size, buf[128];
2593 
2594 	if (!(sc->flags & IWI_FLAG_FW_INITED)) {
2595 		memset(buf, 0, sizeof buf);
2596 		return SYSCTL_OUT(req, buf, sizeof buf);
2597 	}
2598 
2599 	size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
2600 	CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
2601 
2602 	return SYSCTL_OUT(req, buf, sizeof buf);
2603 }
2604 
2605 static int
2606 iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
2607 {
2608 	struct iwi_softc *sc = arg1;
2609 	int val;
2610 
2611 	val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0;
2612 
2613 	return SYSCTL_OUT(req, &val, sizeof val);
2614 }
2615