xref: /openbsd/sys/dev/pci/if_wpi.c (revision 998de4a5)
1 /*	$OpenBSD: if_wpi.c,v 1.134 2016/08/17 11:08:08 stsp Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006-2008
5  *	Damien Bergamini <damien.bergamini@free.fr>
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 3945ABG 802.11 network adapters.
22  */
23 
24 #include "bpfilter.h"
25 
26 #include <sys/param.h>
27 #include <sys/sockio.h>
28 #include <sys/mbuf.h>
29 #include <sys/kernel.h>
30 #include <sys/socket.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.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_amrr.h>
57 #include <net80211/ieee80211_radiotap.h>
58 
59 #include <dev/pci/if_wpireg.h>
60 #include <dev/pci/if_wpivar.h>
61 
62 static const struct pci_matchid wpi_devices[] = {
63 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_3945ABG_1 },
64 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_3945ABG_2 }
65 };
66 
67 int		wpi_match(struct device *, void *, void *);
68 void		wpi_attach(struct device *, struct device *, void *);
69 #if NBPFILTER > 0
70 void		wpi_radiotap_attach(struct wpi_softc *);
71 #endif
72 int		wpi_detach(struct device *, int);
73 int		wpi_activate(struct device *, int);
74 void		wpi_wakeup(struct wpi_softc *);
75 void		wpi_init_task(void *);
76 int		wpi_nic_lock(struct wpi_softc *);
77 int		wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
78 int		wpi_dma_contig_alloc(bus_dma_tag_t, struct wpi_dma_info *,
79 		    void **, bus_size_t, bus_size_t);
80 void		wpi_dma_contig_free(struct wpi_dma_info *);
81 int		wpi_alloc_shared(struct wpi_softc *);
82 void		wpi_free_shared(struct wpi_softc *);
83 int		wpi_alloc_fwmem(struct wpi_softc *);
84 void		wpi_free_fwmem(struct wpi_softc *);
85 int		wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
86 void		wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
87 void		wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
88 int		wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
89 		    int);
90 void		wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
91 void		wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
92 int		wpi_read_eeprom(struct wpi_softc *);
93 void		wpi_read_eeprom_channels(struct wpi_softc *, int);
94 void		wpi_read_eeprom_group(struct wpi_softc *, int);
95 struct		ieee80211_node *wpi_node_alloc(struct ieee80211com *);
96 void		wpi_newassoc(struct ieee80211com *, struct ieee80211_node *,
97 		    int);
98 int		wpi_media_change(struct ifnet *);
99 int		wpi_newstate(struct ieee80211com *, enum ieee80211_state, int);
100 void		wpi_iter_func(void *, struct ieee80211_node *);
101 void		wpi_calib_timeout(void *);
102 int		wpi_ccmp_decap(struct wpi_softc *, struct mbuf *,
103 		    struct ieee80211_key *);
104 void		wpi_rx_done(struct wpi_softc *, struct wpi_rx_desc *,
105 		    struct wpi_rx_data *);
106 void		wpi_tx_done(struct wpi_softc *, struct wpi_rx_desc *);
107 void		wpi_cmd_done(struct wpi_softc *, struct wpi_rx_desc *);
108 void		wpi_notif_intr(struct wpi_softc *);
109 void		wpi_fatal_intr(struct wpi_softc *);
110 int		wpi_intr(void *);
111 int		wpi_tx(struct wpi_softc *, struct mbuf *,
112 		    struct ieee80211_node *);
113 void		wpi_start(struct ifnet *);
114 void		wpi_watchdog(struct ifnet *);
115 int		wpi_ioctl(struct ifnet *, u_long, caddr_t);
116 int		wpi_cmd(struct wpi_softc *, int, const void *, int, int);
117 int		wpi_mrr_setup(struct wpi_softc *);
118 void		wpi_updateedca(struct ieee80211com *);
119 void		wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
120 int		wpi_set_timing(struct wpi_softc *, struct ieee80211_node *);
121 void		wpi_power_calibration(struct wpi_softc *);
122 int		wpi_set_txpower(struct wpi_softc *, int);
123 int		wpi_get_power_index(struct wpi_softc *,
124 		    struct wpi_power_group *, struct ieee80211_channel *, int);
125 int		wpi_set_pslevel(struct wpi_softc *, int, int, int);
126 int		wpi_config(struct wpi_softc *);
127 int		wpi_scan(struct wpi_softc *, uint16_t);
128 int		wpi_auth(struct wpi_softc *);
129 int		wpi_run(struct wpi_softc *);
130 int		wpi_set_key(struct ieee80211com *, struct ieee80211_node *,
131 		    struct ieee80211_key *);
132 void		wpi_delete_key(struct ieee80211com *, struct ieee80211_node *,
133 		    struct ieee80211_key *);
134 int		wpi_post_alive(struct wpi_softc *);
135 int		wpi_load_bootcode(struct wpi_softc *, const uint8_t *, int);
136 int		wpi_load_firmware(struct wpi_softc *);
137 int		wpi_read_firmware(struct wpi_softc *);
138 int		wpi_clock_wait(struct wpi_softc *);
139 int		wpi_apm_init(struct wpi_softc *);
140 void		wpi_apm_stop_master(struct wpi_softc *);
141 void		wpi_apm_stop(struct wpi_softc *);
142 void		wpi_nic_config(struct wpi_softc *);
143 int		wpi_hw_init(struct wpi_softc *);
144 void		wpi_hw_stop(struct wpi_softc *);
145 int		wpi_init(struct ifnet *);
146 void		wpi_stop(struct ifnet *, int);
147 
148 #ifdef WPI_DEBUG
149 #define DPRINTF(x)	do { if (wpi_debug > 0) printf x; } while (0)
150 #define DPRINTFN(n, x)	do { if (wpi_debug >= (n)) printf x; } while (0)
151 int wpi_debug = 0;
152 #else
153 #define DPRINTF(x)
154 #define DPRINTFN(n, x)
155 #endif
156 
157 struct cfdriver wpi_cd = {
158 	NULL, "wpi", DV_IFNET
159 };
160 
161 struct cfattach wpi_ca = {
162 	sizeof (struct wpi_softc), wpi_match, wpi_attach, wpi_detach,
163 	wpi_activate
164 };
165 
166 int
167 wpi_match(struct device *parent, void *match, void *aux)
168 {
169 	return pci_matchbyid((struct pci_attach_args *)aux, wpi_devices,
170 	    nitems(wpi_devices));
171 }
172 
173 void
174 wpi_attach(struct device *parent, struct device *self, void *aux)
175 {
176 	struct wpi_softc *sc = (struct wpi_softc *)self;
177 	struct ieee80211com *ic = &sc->sc_ic;
178 	struct ifnet *ifp = &ic->ic_if;
179 	struct pci_attach_args *pa = aux;
180 	const char *intrstr;
181 	pci_intr_handle_t ih;
182 	pcireg_t memtype, reg;
183 	int i, error;
184 
185 	sc->sc_pct = pa->pa_pc;
186 	sc->sc_pcitag = pa->pa_tag;
187 	sc->sc_dmat = pa->pa_dmat;
188 
189 	/*
190 	 * Get the offset of the PCI Express Capability Structure in PCI
191 	 * Configuration Space (the vendor driver hard-codes it as E0h.)
192 	 */
193 	error = pci_get_capability(sc->sc_pct, sc->sc_pcitag,
194 	    PCI_CAP_PCIEXPRESS, &sc->sc_cap_off, NULL);
195 	if (error == 0) {
196 		printf(": PCIe capability structure not found!\n");
197 		return;
198 	}
199 
200 	/* Clear device-specific "PCI retry timeout" register (41h). */
201 	reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
202 	reg &= ~0xff00;
203 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, reg);
204 
205 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, WPI_PCI_BAR0);
206 	error = pci_mapreg_map(pa, WPI_PCI_BAR0, memtype, 0, &sc->sc_st,
207 	    &sc->sc_sh, NULL, &sc->sc_sz, 0);
208 	if (error != 0) {
209 		printf(": can't map mem space\n");
210 		return;
211 	}
212 
213 	/* Install interrupt handler. */
214 	if (pci_intr_map_msi(pa, &ih) != 0 && pci_intr_map(pa, &ih) != 0) {
215 		printf(": can't map interrupt\n");
216 		return;
217 	}
218 	intrstr = pci_intr_string(sc->sc_pct, ih);
219 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, wpi_intr, sc,
220 	    sc->sc_dev.dv_xname);
221 	if (sc->sc_ih == NULL) {
222 		printf(": can't establish interrupt");
223 		if (intrstr != NULL)
224 			printf(" at %s", intrstr);
225 		printf("\n");
226 		return;
227 	}
228 	printf(": %s", intrstr);
229 
230 	/* Power ON adapter. */
231 	if ((error = wpi_apm_init(sc)) != 0) {
232 		printf(": could not power ON adapter\n");
233 		return;
234 	}
235 
236 	/* Read MAC address, channels, etc from EEPROM. */
237 	if ((error = wpi_read_eeprom(sc)) != 0) {
238 		printf(": could not read EEPROM\n");
239 		return;
240 	}
241 
242 	/* Allocate DMA memory for firmware transfers. */
243 	if ((error = wpi_alloc_fwmem(sc)) != 0) {
244 		printf(": could not allocate memory for firmware\n");
245 		return;
246 	}
247 
248 	/* Allocate shared area. */
249 	if ((error = wpi_alloc_shared(sc)) != 0) {
250 		printf(": could not allocate shared area\n");
251 		goto fail1;
252 	}
253 
254 	/* Allocate TX rings. */
255 	for (i = 0; i < WPI_NTXQUEUES; i++) {
256 		if ((error = wpi_alloc_tx_ring(sc, &sc->txq[i], i)) != 0) {
257 			printf(": could not allocate TX ring %d\n", i);
258 			goto fail2;
259 		}
260 	}
261 
262 	/* Allocate RX ring. */
263 	if ((error = wpi_alloc_rx_ring(sc, &sc->rxq)) != 0) {
264 		printf(": could not allocate Rx ring\n");
265 		goto fail2;
266 	}
267 
268 	/* Power OFF adapter. */
269 	wpi_apm_stop(sc);
270 	/* Clear pending interrupts. */
271 	WPI_WRITE(sc, WPI_INT, 0xffffffff);
272 
273 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
274 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
275 	ic->ic_state = IEEE80211_S_INIT;
276 
277 	/* Set device capabilities. */
278 	ic->ic_caps =
279 	    IEEE80211_C_WEP |		/* WEP */
280 	    IEEE80211_C_RSN |		/* WPA/RSN */
281 	    IEEE80211_C_SCANALL |	/* device scans all channels at once */
282 	    IEEE80211_C_SCANALLBAND |	/* driver scans all bands at once */
283 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
284 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
285 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
286 	    IEEE80211_C_PMGT;		/* power saving supported */
287 
288 	/* Set supported rates. */
289 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
290 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
291 	if (sc->sc_flags & WPI_FLAG_HAS_5GHZ) {
292 		ic->ic_sup_rates[IEEE80211_MODE_11A] =
293 		    ieee80211_std_rateset_11a;
294 	}
295 
296 	/* IBSS channel undefined for now. */
297 	ic->ic_ibss_chan = &ic->ic_channels[0];
298 
299 	ifp->if_softc = sc;
300 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
301 	ifp->if_ioctl = wpi_ioctl;
302 	ifp->if_start = wpi_start;
303 	ifp->if_watchdog = wpi_watchdog;
304 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
305 
306 	if_attach(ifp);
307 	ieee80211_ifattach(ifp);
308 	ic->ic_node_alloc = wpi_node_alloc;
309 	ic->ic_newassoc = wpi_newassoc;
310 	ic->ic_updateedca = wpi_updateedca;
311 	ic->ic_set_key = wpi_set_key;
312 	ic->ic_delete_key = wpi_delete_key;
313 
314 	/* Override 802.11 state transition machine. */
315 	sc->sc_newstate = ic->ic_newstate;
316 	ic->ic_newstate = wpi_newstate;
317 	ieee80211_media_init(ifp, wpi_media_change, ieee80211_media_status);
318 
319 	sc->amrr.amrr_min_success_threshold =  1;
320 	sc->amrr.amrr_max_success_threshold = 15;
321 
322 #if NBPFILTER > 0
323 	wpi_radiotap_attach(sc);
324 #endif
325 	timeout_set(&sc->calib_to, wpi_calib_timeout, sc);
326 	task_set(&sc->init_task, wpi_init_task, sc);
327 	return;
328 
329 	/* Free allocated memory if something failed during attachment. */
330 fail2:	while (--i >= 0)
331 		wpi_free_tx_ring(sc, &sc->txq[i]);
332 	wpi_free_shared(sc);
333 fail1:	wpi_free_fwmem(sc);
334 }
335 
336 #if NBPFILTER > 0
337 /*
338  * Attach the interface to 802.11 radiotap.
339  */
340 void
341 wpi_radiotap_attach(struct wpi_softc *sc)
342 {
343 	bpfattach(&sc->sc_drvbpf, &sc->sc_ic.ic_if, DLT_IEEE802_11_RADIO,
344 	    sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
345 
346 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
347 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
348 	sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT);
349 
350 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
351 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
352 	sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT);
353 }
354 #endif
355 
356 int
357 wpi_detach(struct device *self, int flags)
358 {
359 	struct wpi_softc *sc = (struct wpi_softc *)self;
360 	struct ifnet *ifp = &sc->sc_ic.ic_if;
361 	int qid;
362 
363 	timeout_del(&sc->calib_to);
364 	task_del(systq, &sc->init_task);
365 
366 	/* Uninstall interrupt handler. */
367 	if (sc->sc_ih != NULL)
368 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
369 
370 	/* Free DMA resources. */
371 	wpi_free_rx_ring(sc, &sc->rxq);
372 	for (qid = 0; qid < WPI_NTXQUEUES; qid++)
373 		wpi_free_tx_ring(sc, &sc->txq[qid]);
374 	wpi_free_shared(sc);
375 	wpi_free_fwmem(sc);
376 
377 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
378 
379 	ieee80211_ifdetach(ifp);
380 	if_detach(ifp);
381 
382 	return 0;
383 }
384 
385 int
386 wpi_activate(struct device *self, int act)
387 {
388 	struct wpi_softc *sc = (struct wpi_softc *)self;
389 	struct ifnet *ifp = &sc->sc_ic.ic_if;
390 
391 	switch (act) {
392 	case DVACT_SUSPEND:
393 		if (ifp->if_flags & IFF_RUNNING)
394 			wpi_stop(ifp, 0);
395 		break;
396 	case DVACT_WAKEUP:
397 		wpi_wakeup(sc);
398 		break;
399 	}
400 
401 	return 0;
402 }
403 
404 void
405 wpi_wakeup(struct wpi_softc *sc)
406 {
407 	pcireg_t reg;
408 
409 	/* Clear device-specific "PCI retry timeout" register (41h). */
410 	reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
411 	reg &= ~0xff00;
412 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, reg);
413 
414 	wpi_init_task(sc);
415 }
416 
417 void
418 wpi_init_task(void *arg1)
419 {
420 	struct wpi_softc *sc = arg1;
421 	struct ifnet *ifp = &sc->sc_ic.ic_if;
422 	int s;
423 
424 	s = splnet();
425 	while (sc->sc_flags & WPI_FLAG_BUSY)
426 		tsleep(&sc->sc_flags, 0, "wpipwr", 0);
427 	sc->sc_flags |= WPI_FLAG_BUSY;
428 
429 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_UP)
430 		wpi_init(ifp);
431 
432 	sc->sc_flags &= ~WPI_FLAG_BUSY;
433 	wakeup(&sc->sc_flags);
434 	splx(s);
435 }
436 
437 int
438 wpi_nic_lock(struct wpi_softc *sc)
439 {
440 	int ntries;
441 
442 	/* Request exclusive access to NIC. */
443 	WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
444 
445 	/* Spin until we actually get the lock. */
446 	for (ntries = 0; ntries < 1000; ntries++) {
447 		if ((WPI_READ(sc, WPI_GP_CNTRL) &
448 		     (WPI_GP_CNTRL_MAC_ACCESS_ENA | WPI_GP_CNTRL_SLEEP)) ==
449 		    WPI_GP_CNTRL_MAC_ACCESS_ENA)
450 			return 0;
451 		DELAY(10);
452 	}
453 	return ETIMEDOUT;
454 }
455 
456 static __inline void
457 wpi_nic_unlock(struct wpi_softc *sc)
458 {
459 	WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
460 }
461 
462 static __inline uint32_t
463 wpi_prph_read(struct wpi_softc *sc, uint32_t addr)
464 {
465 	WPI_WRITE(sc, WPI_PRPH_RADDR, WPI_PRPH_DWORD | addr);
466 	WPI_BARRIER_READ_WRITE(sc);
467 	return WPI_READ(sc, WPI_PRPH_RDATA);
468 }
469 
470 static __inline void
471 wpi_prph_write(struct wpi_softc *sc, uint32_t addr, uint32_t data)
472 {
473 	WPI_WRITE(sc, WPI_PRPH_WADDR, WPI_PRPH_DWORD | addr);
474 	WPI_BARRIER_WRITE(sc);
475 	WPI_WRITE(sc, WPI_PRPH_WDATA, data);
476 }
477 
478 static __inline void
479 wpi_prph_setbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
480 {
481 	wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) | mask);
482 }
483 
484 static __inline void
485 wpi_prph_clrbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
486 {
487 	wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) & ~mask);
488 }
489 
490 static __inline void
491 wpi_prph_write_region_4(struct wpi_softc *sc, uint32_t addr,
492     const uint32_t *data, int count)
493 {
494 	for (; count > 0; count--, data++, addr += 4)
495 		wpi_prph_write(sc, addr, *data);
496 }
497 
498 static __inline uint32_t
499 wpi_mem_read(struct wpi_softc *sc, uint32_t addr)
500 {
501 	WPI_WRITE(sc, WPI_MEM_RADDR, addr);
502 	WPI_BARRIER_READ_WRITE(sc);
503 	return WPI_READ(sc, WPI_MEM_RDATA);
504 }
505 
506 static __inline void
507 wpi_mem_write(struct wpi_softc *sc, uint32_t addr, uint32_t data)
508 {
509 	WPI_WRITE(sc, WPI_MEM_WADDR, addr);
510 	WPI_BARRIER_WRITE(sc);
511 	WPI_WRITE(sc, WPI_MEM_WDATA, data);
512 }
513 
514 static __inline void
515 wpi_mem_read_region_4(struct wpi_softc *sc, uint32_t addr, uint32_t *data,
516     int count)
517 {
518 	for (; count > 0; count--, addr += 4)
519 		*data++ = wpi_mem_read(sc, addr);
520 }
521 
522 int
523 wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int count)
524 {
525 	uint8_t *out = data;
526 	uint32_t val;
527 	int error, ntries;
528 
529 	if ((error = wpi_nic_lock(sc)) != 0)
530 		return error;
531 
532 	for (; count > 0; count -= 2, addr++) {
533 		WPI_WRITE(sc, WPI_EEPROM, addr << 2);
534 		WPI_CLRBITS(sc, WPI_EEPROM, WPI_EEPROM_CMD);
535 
536 		for (ntries = 0; ntries < 10; ntries++) {
537 			val = WPI_READ(sc, WPI_EEPROM);
538 			if (val & WPI_EEPROM_READ_VALID)
539 				break;
540 			DELAY(5);
541 		}
542 		if (ntries == 10) {
543 			printf("%s: could not read EEPROM\n",
544 			    sc->sc_dev.dv_xname);
545 			return ETIMEDOUT;
546 		}
547 		*out++ = val >> 16;
548 		if (count > 1)
549 			*out++ = val >> 24;
550 	}
551 
552 	wpi_nic_unlock(sc);
553 	return 0;
554 }
555 
556 int
557 wpi_dma_contig_alloc(bus_dma_tag_t tag, struct wpi_dma_info *dma, void **kvap,
558     bus_size_t size, bus_size_t alignment)
559 {
560 	int nsegs, error;
561 
562 	dma->tag = tag;
563 	dma->size = size;
564 
565 	error = bus_dmamap_create(tag, size, 1, size, 0, BUS_DMA_NOWAIT,
566 	    &dma->map);
567 	if (error != 0)
568 		goto fail;
569 
570 	error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs,
571 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO);
572 	if (error != 0)
573 		goto fail;
574 
575 	error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr,
576 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
577 	if (error != 0)
578 		goto fail;
579 
580 	error = bus_dmamap_load_raw(tag, dma->map, &dma->seg, 1, size,
581 	    BUS_DMA_NOWAIT);
582 	if (error != 0)
583 		goto fail;
584 
585 	bus_dmamap_sync(tag, dma->map, 0, size, BUS_DMASYNC_PREWRITE);
586 
587 	dma->paddr = dma->map->dm_segs[0].ds_addr;
588 	if (kvap != NULL)
589 		*kvap = dma->vaddr;
590 
591 	return 0;
592 
593 fail:	wpi_dma_contig_free(dma);
594 	return error;
595 }
596 
597 void
598 wpi_dma_contig_free(struct wpi_dma_info *dma)
599 {
600 	if (dma->map != NULL) {
601 		if (dma->vaddr != NULL) {
602 			bus_dmamap_sync(dma->tag, dma->map, 0, dma->size,
603 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
604 			bus_dmamap_unload(dma->tag, dma->map);
605 			bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size);
606 			bus_dmamem_free(dma->tag, &dma->seg, 1);
607 			dma->vaddr = NULL;
608 		}
609 		bus_dmamap_destroy(dma->tag, dma->map);
610 		dma->map = NULL;
611 	}
612 }
613 
614 int
615 wpi_alloc_shared(struct wpi_softc *sc)
616 {
617 	/* Shared buffer must be aligned on a 4KB boundary. */
618 	return wpi_dma_contig_alloc(sc->sc_dmat, &sc->shared_dma,
619 	    (void **)&sc->shared, sizeof (struct wpi_shared), 4096);
620 }
621 
622 void
623 wpi_free_shared(struct wpi_softc *sc)
624 {
625 	wpi_dma_contig_free(&sc->shared_dma);
626 }
627 
628 int
629 wpi_alloc_fwmem(struct wpi_softc *sc)
630 {
631 	/* Allocate enough contiguous space to store text and data. */
632 	return wpi_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL,
633 	    WPI_FW_TEXT_MAXSZ + WPI_FW_DATA_MAXSZ, 16);
634 }
635 
636 void
637 wpi_free_fwmem(struct wpi_softc *sc)
638 {
639 	wpi_dma_contig_free(&sc->fw_dma);
640 }
641 
642 int
643 wpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
644 {
645 	bus_size_t size;
646 	int i, error;
647 
648 	ring->cur = 0;
649 
650 	/* Allocate RX descriptors (16KB aligned.) */
651 	size = WPI_RX_RING_COUNT * sizeof (uint32_t);
652 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
653 	    (void **)&ring->desc, size, 16 * 1024);
654 	if (error != 0) {
655 		printf("%s: could not allocate RX ring DMA memory\n",
656 		    sc->sc_dev.dv_xname);
657 		goto fail;
658 	}
659 
660 	/*
661 	 * Allocate and map RX buffers.
662 	 */
663 	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
664 		struct wpi_rx_data *data = &ring->data[i];
665 
666 		error = bus_dmamap_create(sc->sc_dmat, WPI_RBUF_SIZE, 1,
667 		    WPI_RBUF_SIZE, 0, BUS_DMA_NOWAIT, &data->map);
668 		if (error != 0) {
669 			printf("%s: could not create RX buf DMA map\n",
670 			    sc->sc_dev.dv_xname);
671 			goto fail;
672 		}
673 
674 		data->m = MCLGETI(NULL, M_DONTWAIT, NULL, WPI_RBUF_SIZE);
675 		if (data->m == NULL) {
676 			printf("%s: could not allocate RX mbuf\n",
677 			    sc->sc_dev.dv_xname);
678 			error = ENOBUFS;
679 			goto fail;
680 		}
681 
682 		error = bus_dmamap_load(sc->sc_dmat, data->map,
683 		    mtod(data->m, void *), WPI_RBUF_SIZE, NULL,
684 		    BUS_DMA_NOWAIT | BUS_DMA_READ);
685 		if (error != 0) {
686 			printf("%s: can't map mbuf (error %d)\n",
687 			    sc->sc_dev.dv_xname, error);
688 			goto fail;
689 		}
690 
691 		/* Set physical address of RX buffer. */
692 		ring->desc[i] = htole32(data->map->dm_segs[0].ds_addr);
693 	}
694 
695 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map, 0, size,
696 	    BUS_DMASYNC_PREWRITE);
697 
698 	return 0;
699 
700 fail:	wpi_free_rx_ring(sc, ring);
701 	return error;
702 }
703 
704 void
705 wpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
706 {
707 	int ntries;
708 
709 	if (wpi_nic_lock(sc) == 0) {
710 		WPI_WRITE(sc, WPI_FH_RX_CONFIG, 0);
711 		for (ntries = 0; ntries < 100; ntries++) {
712 			if (WPI_READ(sc, WPI_FH_RX_STATUS) &
713 			    WPI_FH_RX_STATUS_IDLE)
714 				break;
715 			DELAY(10);
716 		}
717 		wpi_nic_unlock(sc);
718 	}
719 	ring->cur = 0;
720 }
721 
722 void
723 wpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
724 {
725 	int i;
726 
727 	wpi_dma_contig_free(&ring->desc_dma);
728 
729 	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
730 		struct wpi_rx_data *data = &ring->data[i];
731 
732 		if (data->m != NULL) {
733 			bus_dmamap_sync(sc->sc_dmat, data->map, 0,
734 			    data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
735 			bus_dmamap_unload(sc->sc_dmat, data->map);
736 			m_freem(data->m);
737 		}
738 		if (data->map != NULL)
739 			bus_dmamap_destroy(sc->sc_dmat, data->map);
740 	}
741 }
742 
743 int
744 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int qid)
745 {
746 	bus_addr_t paddr;
747 	bus_size_t size;
748 	int i, error;
749 
750 	ring->qid = qid;
751 	ring->queued = 0;
752 	ring->cur = 0;
753 
754 	/* Allocate TX descriptors (16KB aligned.) */
755 	size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_desc);
756 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
757 	    (void **)&ring->desc, size, 16 * 1024);
758 	if (error != 0) {
759 		printf("%s: could not allocate TX ring DMA memory\n",
760 		    sc->sc_dev.dv_xname);
761 		goto fail;
762 	}
763 
764 	/* Update shared area with ring physical address. */
765 	sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
766 	bus_dmamap_sync(sc->sc_dmat, sc->shared_dma.map, 0,
767 	    sizeof (struct wpi_shared), BUS_DMASYNC_PREWRITE);
768 
769 	/*
770 	 * We only use rings 0 through 4 (4 EDCA + cmd) so there is no need
771 	 * to allocate commands space for other rings.
772 	 * XXX Do we really need to allocate descriptors for other rings?
773 	 */
774 	if (qid > 4)
775 		return 0;
776 
777 	size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_cmd);
778 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma,
779 	    (void **)&ring->cmd, size, 4);
780 	if (error != 0) {
781 		printf("%s: could not allocate TX cmd DMA memory\n",
782 		    sc->sc_dev.dv_xname);
783 		goto fail;
784 	}
785 
786 	paddr = ring->cmd_dma.paddr;
787 	for (i = 0; i < WPI_TX_RING_COUNT; i++) {
788 		struct wpi_tx_data *data = &ring->data[i];
789 
790 		data->cmd_paddr = paddr;
791 		paddr += sizeof (struct wpi_tx_cmd);
792 
793 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
794 		    WPI_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
795 		    &data->map);
796 		if (error != 0) {
797 			printf("%s: could not create TX buf DMA map\n",
798 			    sc->sc_dev.dv_xname);
799 			goto fail;
800 		}
801 	}
802 	return 0;
803 
804 fail:	wpi_free_tx_ring(sc, ring);
805 	return error;
806 }
807 
808 void
809 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
810 {
811 	int i;
812 
813 	for (i = 0; i < WPI_TX_RING_COUNT; i++) {
814 		struct wpi_tx_data *data = &ring->data[i];
815 
816 		if (data->m != NULL) {
817 			bus_dmamap_sync(sc->sc_dmat, data->map, 0,
818 			    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
819 			bus_dmamap_unload(sc->sc_dmat, data->map);
820 			m_freem(data->m);
821 			data->m = NULL;
822 		}
823 	}
824 	/* Clear TX descriptors. */
825 	memset(ring->desc, 0, ring->desc_dma.size);
826 	sc->qfullmsk &= ~(1 << ring->qid);
827 	ring->queued = 0;
828 	ring->cur = 0;
829 }
830 
831 void
832 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
833 {
834 	int i;
835 
836 	wpi_dma_contig_free(&ring->desc_dma);
837 	wpi_dma_contig_free(&ring->cmd_dma);
838 
839 	for (i = 0; i < WPI_TX_RING_COUNT; i++) {
840 		struct wpi_tx_data *data = &ring->data[i];
841 
842 		if (data->m != NULL) {
843 			bus_dmamap_sync(sc->sc_dmat, data->map, 0,
844 			    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
845 			bus_dmamap_unload(sc->sc_dmat, data->map);
846 			m_freem(data->m);
847 		}
848 		if (data->map != NULL)
849 			bus_dmamap_destroy(sc->sc_dmat, data->map);
850 	}
851 }
852 
853 int
854 wpi_read_eeprom(struct wpi_softc *sc)
855 {
856 	struct ieee80211com *ic = &sc->sc_ic;
857 	char domain[4];
858 	int i;
859 
860 	if ((WPI_READ(sc, WPI_EEPROM_GP) & 0x6) == 0) {
861 		printf("%s: bad EEPROM signature\n", sc->sc_dev.dv_xname);
862 		return EIO;
863 	}
864 	/* Clear HW ownership of EEPROM. */
865 	WPI_CLRBITS(sc, WPI_EEPROM_GP, WPI_EEPROM_GP_IF_OWNER);
866 
867 	wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap, 1);
868 	wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev, 2);
869 	wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1);
870 
871 	DPRINTF(("cap=%x rev=%x type=%x\n", sc->cap, letoh16(sc->rev),
872 	    sc->type));
873 
874 	/* Read and print regulatory domain (4 ASCII characters.) */
875 	wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, domain, 4);
876 	printf(", %.4s", domain);
877 
878 	/* Read and print MAC address. */
879 	wpi_read_prom_data(sc, WPI_EEPROM_MAC, ic->ic_myaddr, 6);
880 	printf(", address %s\n", ether_sprintf(ic->ic_myaddr));
881 
882 	/* Read the list of authorized channels. */
883 	for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
884 		wpi_read_eeprom_channels(sc, i);
885 
886 	/* Read the list of TX power groups. */
887 	for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
888 		wpi_read_eeprom_group(sc, i);
889 
890 	return 0;
891 }
892 
893 void
894 wpi_read_eeprom_channels(struct wpi_softc *sc, int n)
895 {
896 	struct ieee80211com *ic = &sc->sc_ic;
897 	const struct wpi_chan_band *band = &wpi_bands[n];
898 	struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND];
899 	int chan, i;
900 
901 	wpi_read_prom_data(sc, band->addr, channels,
902 	    band->nchan * sizeof (struct wpi_eeprom_chan));
903 
904 	for (i = 0; i < band->nchan; i++) {
905 		if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID))
906 			continue;
907 
908 		chan = band->chan[i];
909 
910 		if (n == 0) {	/* 2GHz band */
911 			ic->ic_channels[chan].ic_freq =
912 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
913 			ic->ic_channels[chan].ic_flags =
914 			    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
915 			    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
916 
917 		} else {	/* 5GHz band */
918 			/*
919 			 * Some adapters support channels 7, 8, 11 and 12
920 			 * both in the 2GHz and 4.9GHz bands.
921 			 * Because of limitations in our net80211 layer,
922 			 * we don't support them in the 4.9GHz band.
923 			 */
924 			if (chan <= 14)
925 				continue;
926 
927 			ic->ic_channels[chan].ic_freq =
928 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ);
929 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A;
930 			/* We have at least one valid 5GHz channel. */
931 			sc->sc_flags |= WPI_FLAG_HAS_5GHZ;
932 		}
933 
934 		/* Is active scan allowed on this channel? */
935 		if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) {
936 			ic->ic_channels[chan].ic_flags |=
937 			    IEEE80211_CHAN_PASSIVE;
938 		}
939 
940 		/* Save maximum allowed TX power for this channel. */
941 		sc->maxpwr[chan] = channels[i].maxpwr;
942 
943 		DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n",
944 		    chan, channels[i].flags, sc->maxpwr[chan]));
945 	}
946 }
947 
948 void
949 wpi_read_eeprom_group(struct wpi_softc *sc, int n)
950 {
951 	struct wpi_power_group *group = &sc->groups[n];
952 	struct wpi_eeprom_group rgroup;
953 	int i;
954 
955 	wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup,
956 	    sizeof rgroup);
957 
958 	/* Save TX power group information. */
959 	group->chan   = rgroup.chan;
960 	group->maxpwr = rgroup.maxpwr;
961 	/* Retrieve temperature at which the samples were taken. */
962 	group->temp   = (int16_t)letoh16(rgroup.temp);
963 
964 	DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n,
965 	    group->chan, group->maxpwr, group->temp));
966 
967 	for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
968 		group->samples[i].index = rgroup.samples[i].index;
969 		group->samples[i].power = rgroup.samples[i].power;
970 
971 		DPRINTF(("\tsample %d: index=%d power=%d\n", i,
972 		    group->samples[i].index, group->samples[i].power));
973 	}
974 }
975 
976 struct ieee80211_node *
977 wpi_node_alloc(struct ieee80211com *ic)
978 {
979 	return malloc(sizeof (struct wpi_node), M_DEVBUF, M_NOWAIT | M_ZERO);
980 }
981 
982 void
983 wpi_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
984 {
985 	struct wpi_softc *sc = ic->ic_if.if_softc;
986 	struct wpi_node *wn = (void *)ni;
987 	uint8_t rate;
988 	int ridx, i;
989 
990 	ieee80211_amrr_node_init(&sc->amrr, &wn->amn);
991 	/* Start at lowest available bit-rate, AMRR will raise. */
992 	ni->ni_txrate = 0;
993 
994 	for (i = 0; i < ni->ni_rates.rs_nrates; i++) {
995 		rate = ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL;
996 		/* Map 802.11 rate to HW rate index. */
997 		for (ridx = 0; ridx <= WPI_RIDX_MAX; ridx++)
998 			if (wpi_rates[ridx].rate == rate)
999 				break;
1000 		wn->ridx[i] = ridx;
1001 	}
1002 }
1003 
1004 int
1005 wpi_media_change(struct ifnet *ifp)
1006 {
1007 	struct wpi_softc *sc = ifp->if_softc;
1008 	struct ieee80211com *ic = &sc->sc_ic;
1009 	uint8_t rate, ridx;
1010 	int error;
1011 
1012 	error = ieee80211_media_change(ifp);
1013 	if (error != ENETRESET)
1014 		return error;
1015 
1016 	if (ic->ic_fixed_rate != -1) {
1017 		rate = ic->ic_sup_rates[ic->ic_curmode].
1018 		    rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
1019 		/* Map 802.11 rate to HW rate index. */
1020 		for (ridx = 0; ridx <= WPI_RIDX_MAX; ridx++)
1021 			if (wpi_rates[ridx].rate == rate)
1022 				break;
1023 		sc->fixed_ridx = ridx;
1024 	}
1025 
1026 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1027 	    (IFF_UP | IFF_RUNNING)) {
1028 		wpi_stop(ifp, 0);
1029 		error = wpi_init(ifp);
1030 	}
1031 	return error;
1032 }
1033 
1034 int
1035 wpi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1036 {
1037 	struct ifnet *ifp = &ic->ic_if;
1038 	struct wpi_softc *sc = ifp->if_softc;
1039 	int error;
1040 
1041 	timeout_del(&sc->calib_to);
1042 
1043 	switch (nstate) {
1044 	case IEEE80211_S_SCAN:
1045 		/* Make the link LED blink while we're scanning. */
1046 		wpi_set_led(sc, WPI_LED_LINK, 20, 2);
1047 
1048 		if ((error = wpi_scan(sc, IEEE80211_CHAN_2GHZ)) != 0) {
1049 			printf("%s: could not initiate scan\n",
1050 			    sc->sc_dev.dv_xname);
1051 			return error;
1052 		}
1053 		ic->ic_state = nstate;
1054 		return 0;
1055 
1056 	case IEEE80211_S_ASSOC:
1057 		if (ic->ic_state != IEEE80211_S_RUN)
1058 			break;
1059 		/* FALLTHROUGH */
1060 	case IEEE80211_S_AUTH:
1061 		/* Reset state to handle reassociations correctly. */
1062 		sc->rxon.associd = 0;
1063 		sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1064 
1065 		if ((error = wpi_auth(sc)) != 0) {
1066 			printf("%s: could not move to auth state\n",
1067 			    sc->sc_dev.dv_xname);
1068 			return error;
1069 		}
1070 		break;
1071 
1072 	case IEEE80211_S_RUN:
1073 		if ((error = wpi_run(sc)) != 0) {
1074 			printf("%s: could not move to run state\n",
1075 			    sc->sc_dev.dv_xname);
1076 			return error;
1077 		}
1078 		break;
1079 
1080 	case IEEE80211_S_INIT:
1081 		break;
1082 	}
1083 
1084 	return sc->sc_newstate(ic, nstate, arg);
1085 }
1086 
1087 void
1088 wpi_iter_func(void *arg, struct ieee80211_node *ni)
1089 {
1090 	struct wpi_softc *sc = arg;
1091 	struct wpi_node *wn = (struct wpi_node *)ni;
1092 
1093 	ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn);
1094 }
1095 
1096 void
1097 wpi_calib_timeout(void *arg)
1098 {
1099 	struct wpi_softc *sc = arg;
1100 	struct ieee80211com *ic = &sc->sc_ic;
1101 	int s;
1102 
1103 	s = splnet();
1104 	/* Automatic rate control triggered every 500ms. */
1105 	if (ic->ic_fixed_rate == -1) {
1106 		if (ic->ic_opmode == IEEE80211_M_STA)
1107 			wpi_iter_func(sc, ic->ic_bss);
1108 		else
1109 			ieee80211_iterate_nodes(ic, wpi_iter_func, sc);
1110 	}
1111 
1112 	/* Force automatic TX power calibration every 60 secs. */
1113 	if (++sc->calib_cnt >= 120) {
1114 		wpi_power_calibration(sc);
1115 		sc->calib_cnt = 0;
1116 	}
1117 	splx(s);
1118 
1119 	/* Automatic rate control triggered every 500ms. */
1120 	timeout_add_msec(&sc->calib_to, 500);
1121 }
1122 
1123 int
1124 wpi_ccmp_decap(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_key *k)
1125 {
1126 	struct ieee80211_frame *wh;
1127 	uint64_t pn, *prsc;
1128 	uint8_t *ivp;
1129 	uint8_t tid;
1130 	int hdrlen;
1131 
1132 	wh = mtod(m, struct ieee80211_frame *);
1133 	hdrlen = ieee80211_get_hdrlen(wh);
1134 	ivp = (uint8_t *)wh + hdrlen;
1135 
1136 	/* Check that ExtIV bit is be set. */
1137 	if (!(ivp[3] & IEEE80211_WEP_EXTIV)) {
1138 		DPRINTF(("CCMP decap ExtIV not set\n"));
1139 		return 1;
1140 	}
1141 	tid = ieee80211_has_qos(wh) ?
1142 	    ieee80211_get_qos(wh) & IEEE80211_QOS_TID : 0;
1143 	prsc = &k->k_rsc[tid];
1144 
1145 	/* Extract the 48-bit PN from the CCMP header. */
1146 	pn = (uint64_t)ivp[0]       |
1147 	     (uint64_t)ivp[1] <<  8 |
1148 	     (uint64_t)ivp[4] << 16 |
1149 	     (uint64_t)ivp[5] << 24 |
1150 	     (uint64_t)ivp[6] << 32 |
1151 	     (uint64_t)ivp[7] << 40;
1152 	if (pn <= *prsc) {
1153 		/*
1154 		 * Not necessarily a replayed frame since we did not check
1155 		 * the sequence number of the 802.11 header yet.
1156 		 */
1157 		DPRINTF(("CCMP replayed\n"));
1158 		return 1;
1159 	}
1160 	/* Update last seen packet number. */
1161 	*prsc = pn;
1162 
1163 	/* Clear Protected bit and strip IV. */
1164 	wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1165 	memmove(mtod(m, caddr_t) + IEEE80211_CCMP_HDRLEN, wh, hdrlen);
1166 	m_adj(m, IEEE80211_CCMP_HDRLEN);
1167 	/* Strip MIC. */
1168 	m_adj(m, -IEEE80211_CCMP_MICLEN);
1169 	return 0;
1170 }
1171 
1172 void
1173 wpi_rx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1174     struct wpi_rx_data *data)
1175 {
1176 	struct ieee80211com *ic = &sc->sc_ic;
1177 	struct ifnet *ifp = &ic->ic_if;
1178 	struct wpi_rx_ring *ring = &sc->rxq;
1179 	struct wpi_rx_stat *stat;
1180 	struct wpi_rx_head *head;
1181 	struct wpi_rx_tail *tail;
1182 	struct ieee80211_frame *wh;
1183 	struct ieee80211_rxinfo rxi;
1184 	struct ieee80211_node *ni;
1185 	struct mbuf *m, *m1;
1186 	uint32_t flags;
1187 	int error;
1188 
1189 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, WPI_RBUF_SIZE,
1190 	    BUS_DMASYNC_POSTREAD);
1191 	stat = (struct wpi_rx_stat *)(desc + 1);
1192 
1193 	if (stat->len > WPI_STAT_MAXLEN) {
1194 		printf("%s: invalid RX statistic header\n",
1195 		    sc->sc_dev.dv_xname);
1196 		ifp->if_ierrors++;
1197 		return;
1198 	}
1199 	head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1200 	tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + letoh16(head->len));
1201 	flags = letoh32(tail->flags);
1202 
1203 	/* Discard frames with a bad FCS early. */
1204 	if ((flags & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1205 		DPRINTFN(2, ("rx tail flags error %x\n", flags));
1206 		ifp->if_ierrors++;
1207 		return;
1208 	}
1209 	/* Discard frames that are too short. */
1210 	if (letoh16(head->len) < sizeof (*wh)) {
1211 		DPRINTF(("frame too short: %d\n", letoh16(head->len)));
1212 		ic->ic_stats.is_rx_tooshort++;
1213 		ifp->if_ierrors++;
1214 		return;
1215 	}
1216 
1217 	m1 = MCLGETI(NULL, M_DONTWAIT, NULL, WPI_RBUF_SIZE);
1218 	if (m1 == NULL) {
1219 		ic->ic_stats.is_rx_nombuf++;
1220 		ifp->if_ierrors++;
1221 		return;
1222 	}
1223 	bus_dmamap_unload(sc->sc_dmat, data->map);
1224 
1225 	error = bus_dmamap_load(sc->sc_dmat, data->map, mtod(m1, void *),
1226 	    WPI_RBUF_SIZE, NULL, BUS_DMA_NOWAIT | BUS_DMA_READ);
1227 	if (error != 0) {
1228 		m_freem(m1);
1229 
1230 		/* Try to reload the old mbuf. */
1231 		error = bus_dmamap_load(sc->sc_dmat, data->map,
1232 		    mtod(data->m, void *), WPI_RBUF_SIZE, NULL,
1233 		    BUS_DMA_NOWAIT | BUS_DMA_READ);
1234 		if (error != 0) {
1235 			panic("%s: could not load old RX mbuf",
1236 			    sc->sc_dev.dv_xname);
1237 		}
1238 		/* Physical address may have changed. */
1239 		ring->desc[ring->cur] = htole32(data->map->dm_segs[0].ds_addr);
1240 		bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
1241 		    ring->cur * sizeof (uint32_t), sizeof (uint32_t),
1242 		    BUS_DMASYNC_PREWRITE);
1243 		ifp->if_ierrors++;
1244 		return;
1245 	}
1246 
1247 	m = data->m;
1248 	data->m = m1;
1249 	/* Update RX descriptor. */
1250 	ring->desc[ring->cur] = htole32(data->map->dm_segs[0].ds_addr);
1251 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
1252 	    ring->cur * sizeof (uint32_t), sizeof (uint32_t),
1253 	    BUS_DMASYNC_PREWRITE);
1254 
1255 	/* Finalize mbuf. */
1256 	m->m_data = (caddr_t)(head + 1);
1257 	m->m_pkthdr.len = m->m_len = letoh16(head->len);
1258 
1259 	/* Grab a reference to the source node. */
1260 	wh = mtod(m, struct ieee80211_frame *);
1261 	ni = ieee80211_find_rxnode(ic, wh);
1262 
1263 	rxi.rxi_flags = 0;
1264 	if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1265 	    !IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1266 	    (ni->ni_flags & IEEE80211_NODE_RXPROT) &&
1267 	    ni->ni_pairwise_key.k_cipher == IEEE80211_CIPHER_CCMP) {
1268 		if ((flags & WPI_RX_CIPHER_MASK) != WPI_RX_CIPHER_CCMP) {
1269 			ic->ic_stats.is_ccmp_dec_errs++;
1270 			ifp->if_ierrors++;
1271 			m_freem(m);
1272 			return;
1273 		}
1274 		/* Check whether decryption was successful or not. */
1275 		if ((flags & WPI_RX_DECRYPT_MASK) != WPI_RX_DECRYPT_OK) {
1276 			DPRINTF(("CCMP decryption failed 0x%x\n", flags));
1277 			ic->ic_stats.is_ccmp_dec_errs++;
1278 			ifp->if_ierrors++;
1279 			m_freem(m);
1280 			return;
1281 		}
1282 		if (wpi_ccmp_decap(sc, m, &ni->ni_pairwise_key) != 0) {
1283 			ifp->if_ierrors++;
1284 			m_freem(m);
1285 			return;
1286 		}
1287 		rxi.rxi_flags |= IEEE80211_RXI_HWDEC;
1288 	}
1289 
1290 #if NBPFILTER > 0
1291 	if (sc->sc_drvbpf != NULL) {
1292 		struct mbuf mb;
1293 		struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
1294 
1295 		tap->wr_flags = 0;
1296 		if (letoh16(head->flags) & 0x4)
1297 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1298 		tap->wr_chan_freq =
1299 		    htole16(ic->ic_channels[head->chan].ic_freq);
1300 		tap->wr_chan_flags =
1301 		    htole16(ic->ic_channels[head->chan].ic_flags);
1302 		tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
1303 		tap->wr_dbm_antnoise = (int8_t)letoh16(stat->noise);
1304 		tap->wr_tsft = tail->tstamp;
1305 		tap->wr_antenna = (letoh16(head->flags) >> 4) & 0xf;
1306 		switch (head->rate) {
1307 		/* CCK rates. */
1308 		case  10: tap->wr_rate =   2; break;
1309 		case  20: tap->wr_rate =   4; break;
1310 		case  55: tap->wr_rate =  11; break;
1311 		case 110: tap->wr_rate =  22; break;
1312 		/* OFDM rates. */
1313 		case 0xd: tap->wr_rate =  12; break;
1314 		case 0xf: tap->wr_rate =  18; break;
1315 		case 0x5: tap->wr_rate =  24; break;
1316 		case 0x7: tap->wr_rate =  36; break;
1317 		case 0x9: tap->wr_rate =  48; break;
1318 		case 0xb: tap->wr_rate =  72; break;
1319 		case 0x1: tap->wr_rate =  96; break;
1320 		case 0x3: tap->wr_rate = 108; break;
1321 		/* Unknown rate: should not happen. */
1322 		default:  tap->wr_rate =   0;
1323 		}
1324 
1325 		mb.m_data = (caddr_t)tap;
1326 		mb.m_len = sc->sc_rxtap_len;
1327 		mb.m_next = m;
1328 		mb.m_nextpkt = NULL;
1329 		mb.m_type = 0;
1330 		mb.m_flags = 0;
1331 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
1332 	}
1333 #endif
1334 
1335 	/* Send the frame to the 802.11 layer. */
1336 	rxi.rxi_rssi = stat->rssi;
1337 	rxi.rxi_tstamp = 0;	/* unused */
1338 	ieee80211_input(ifp, m, ni, &rxi);
1339 
1340 	/* Node is no longer needed. */
1341 	ieee80211_release_node(ic, ni);
1342 }
1343 
1344 void
1345 wpi_tx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1346 {
1347 	struct ieee80211com *ic = &sc->sc_ic;
1348 	struct ifnet *ifp = &ic->ic_if;
1349 	struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
1350 	struct wpi_tx_data *data = &ring->data[desc->idx];
1351 	struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
1352 	struct wpi_node *wn = (struct wpi_node *)data->ni;
1353 
1354 	/* Update rate control statistics. */
1355 	wn->amn.amn_txcnt++;
1356 	if (stat->retrycnt > 0)
1357 		wn->amn.amn_retrycnt++;
1358 
1359 	if ((letoh32(stat->status) & 0xff) != 1)
1360 		ifp->if_oerrors++;
1361 	else
1362 		ifp->if_opackets++;
1363 
1364 	/* Unmap and free mbuf. */
1365 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
1366 	    BUS_DMASYNC_POSTWRITE);
1367 	bus_dmamap_unload(sc->sc_dmat, data->map);
1368 	m_freem(data->m);
1369 	data->m = NULL;
1370 	ieee80211_release_node(ic, data->ni);
1371 	data->ni = NULL;
1372 
1373 	sc->sc_tx_timer = 0;
1374 	if (--ring->queued < WPI_TX_RING_LOMARK) {
1375 		sc->qfullmsk &= ~(1 << ring->qid);
1376 		if (sc->qfullmsk == 0 && ifq_is_oactive(&ifp->if_snd)) {
1377 			ifq_clr_oactive(&ifp->if_snd);
1378 			(*ifp->if_start)(ifp);
1379 		}
1380 	}
1381 }
1382 
1383 void
1384 wpi_cmd_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1385 {
1386 	struct wpi_tx_ring *ring = &sc->txq[4];
1387 	struct wpi_tx_data *data;
1388 
1389 	if ((desc->qid & 7) != 4)
1390 		return;	/* Not a command ack. */
1391 
1392 	data = &ring->data[desc->idx];
1393 
1394 	/* If the command was mapped in an mbuf, free it. */
1395 	if (data->m != NULL) {
1396 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1397 		    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1398 		bus_dmamap_unload(sc->sc_dmat, data->map);
1399 		m_freem(data->m);
1400 		data->m = NULL;
1401 	}
1402 	wakeup(&ring->cmd[desc->idx]);
1403 }
1404 
1405 void
1406 wpi_notif_intr(struct wpi_softc *sc)
1407 {
1408 	struct ieee80211com *ic = &sc->sc_ic;
1409 	struct ifnet *ifp = &ic->ic_if;
1410 	uint32_t hw;
1411 
1412 	bus_dmamap_sync(sc->sc_dmat, sc->shared_dma.map, 0,
1413 	    sizeof (struct wpi_shared), BUS_DMASYNC_POSTREAD);
1414 
1415 	hw = letoh32(sc->shared->next);
1416 	while (sc->rxq.cur != hw) {
1417 		struct wpi_rx_data *data = &sc->rxq.data[sc->rxq.cur];
1418 		struct wpi_rx_desc *desc;
1419 
1420 		bus_dmamap_sync(sc->sc_dmat, data->map, 0, sizeof (*desc),
1421 		    BUS_DMASYNC_POSTREAD);
1422 		desc = mtod(data->m, struct wpi_rx_desc *);
1423 
1424 		DPRINTFN(4, ("rx notification qid=%x idx=%d flags=%x type=%d "
1425 		    "len=%d\n", desc->qid, desc->idx, desc->flags, desc->type,
1426 		    letoh32(desc->len)));
1427 
1428 		if (!(desc->qid & 0x80))	/* Reply to a command. */
1429 			wpi_cmd_done(sc, desc);
1430 
1431 		switch (desc->type) {
1432 		case WPI_RX_DONE:
1433 			/* An 802.11 frame has been received. */
1434 			wpi_rx_done(sc, desc, data);
1435 			break;
1436 
1437 		case WPI_TX_DONE:
1438 			/* An 802.11 frame has been transmitted. */
1439 			wpi_tx_done(sc, desc);
1440 			break;
1441 
1442 		case WPI_UC_READY:
1443 		{
1444 			struct wpi_ucode_info *uc =
1445 			    (struct wpi_ucode_info *)(desc + 1);
1446 
1447 			/* The microcontroller is ready. */
1448 			bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
1449 			    sizeof (*uc), BUS_DMASYNC_POSTREAD);
1450 			DPRINTF(("microcode alive notification version %x "
1451 			    "alive %x\n", letoh32(uc->version),
1452 			    letoh32(uc->valid)));
1453 
1454 			if (letoh32(uc->valid) != 1) {
1455 				printf("%s: microcontroller initialization "
1456 				    "failed\n", sc->sc_dev.dv_xname);
1457 			}
1458 			if (uc->subtype != WPI_UCODE_INIT) {
1459 				/* Save the address of the error log. */
1460 				sc->errptr = letoh32(uc->errptr);
1461 			}
1462 			break;
1463 		}
1464 		case WPI_STATE_CHANGED:
1465 		{
1466 			uint32_t *status = (uint32_t *)(desc + 1);
1467 
1468 			/* Enabled/disabled notification. */
1469 			bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
1470 			    sizeof (*status), BUS_DMASYNC_POSTREAD);
1471 			DPRINTF(("state changed to %x\n", letoh32(*status)));
1472 
1473 			if (letoh32(*status) & 1) {
1474 				/* The radio button has to be pushed. */
1475 				printf("%s: Radio transmitter is off\n",
1476 				    sc->sc_dev.dv_xname);
1477 				/* Turn the interface down. */
1478 				ifp->if_flags &= ~IFF_UP;
1479 				wpi_stop(ifp, 1);
1480 				return;	/* No further processing. */
1481 			}
1482 			break;
1483 		}
1484 		case WPI_START_SCAN:
1485 		{
1486 			struct wpi_start_scan *scan =
1487 			    (struct wpi_start_scan *)(desc + 1);
1488 
1489 			bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
1490 			    sizeof (*scan), BUS_DMASYNC_POSTREAD);
1491 			DPRINTFN(2, ("scanning channel %d status %x\n",
1492 			    scan->chan, letoh32(scan->status)));
1493 
1494 			/* Fix current channel. */
1495 			ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan];
1496 			break;
1497 		}
1498 		case WPI_STOP_SCAN:
1499 		{
1500 			struct wpi_stop_scan *scan =
1501 			    (struct wpi_stop_scan *)(desc + 1);
1502 
1503 			bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (*desc),
1504 			    sizeof (*scan), BUS_DMASYNC_POSTREAD);
1505 			DPRINTF(("scan finished nchan=%d status=%d chan=%d\n",
1506 			    scan->nchan, scan->status, scan->chan));
1507 
1508 			if (scan->status == 1 && scan->chan <= 14 &&
1509 			    (sc->sc_flags & WPI_FLAG_HAS_5GHZ)) {
1510 				/*
1511 				 * We just finished scanning 2GHz channels,
1512 				 * start scanning 5GHz ones.
1513 				 */
1514 				if (wpi_scan(sc, IEEE80211_CHAN_5GHZ) == 0)
1515 					break;
1516 			}
1517 			ieee80211_end_scan(ifp);
1518 			break;
1519 		}
1520 		}
1521 
1522 		sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
1523 	}
1524 
1525 	/* Tell the firmware what we have processed. */
1526 	hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
1527 	WPI_WRITE(sc, WPI_FH_RX_WPTR, hw & ~7);
1528 }
1529 
1530 /*
1531  * Dump the error log of the firmware when a firmware panic occurs.  Although
1532  * we can't debug the firmware because it is neither open source nor free, it
1533  * can help us to identify certain classes of problems.
1534  */
1535 void
1536 wpi_fatal_intr(struct wpi_softc *sc)
1537 {
1538 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1539 	struct wpi_fwdump dump;
1540 	uint32_t i, offset, count;
1541 
1542 	/* Check that the error log address is valid. */
1543 	if (sc->errptr < WPI_FW_DATA_BASE ||
1544 	    sc->errptr + sizeof (dump) >
1545 	    WPI_FW_DATA_BASE + WPI_FW_DATA_MAXSZ) {
1546 		printf("%s: bad firmware error log address 0x%08x\n",
1547 		    sc->sc_dev.dv_xname, sc->errptr);
1548 		return;
1549 	}
1550 
1551 	if (wpi_nic_lock(sc) != 0) {
1552 		printf("%s: could not read firmware error log\n",
1553 		    sc->sc_dev.dv_xname);
1554 		return;
1555 	}
1556 	/* Read number of entries in the log. */
1557 	count = wpi_mem_read(sc, sc->errptr);
1558 	if (count == 0 || count * sizeof (dump) > WPI_FW_DATA_MAXSZ) {
1559 		printf("%s: invalid count field (count=%u)\n",
1560 		    sc->sc_dev.dv_xname, count);
1561 		wpi_nic_unlock(sc);
1562 		return;
1563 	}
1564 	/* Skip "count" field. */
1565 	offset = sc->errptr + sizeof (uint32_t);
1566 	printf("firmware error log (count=%u):\n", count);
1567 	for (i = 0; i < count; i++) {
1568 		wpi_mem_read_region_4(sc, offset, (uint32_t *)&dump,
1569 		    sizeof (dump) / sizeof (uint32_t));
1570 
1571 		printf("  error type = \"%s\" (0x%08X)\n",
1572 		    (dump.desc < N(wpi_fw_errmsg)) ?
1573 			wpi_fw_errmsg[dump.desc] : "UNKNOWN",
1574 		    dump.desc);
1575 		printf("  error data      = 0x%08X\n",
1576 		    dump.data);
1577 		printf("  branch link     = 0x%08X%08X\n",
1578 		    dump.blink[0], dump.blink[1]);
1579 		printf("  interrupt link  = 0x%08X%08X\n",
1580 		    dump.ilink[0], dump.ilink[1]);
1581 		printf("  time            = %u\n", dump.time);
1582 
1583 		offset += sizeof (dump);
1584 	}
1585 	wpi_nic_unlock(sc);
1586 	/* Dump driver status (TX and RX rings) while we're here. */
1587 	printf("driver status:\n");
1588 	for (i = 0; i < 6; i++) {
1589 		struct wpi_tx_ring *ring = &sc->txq[i];
1590 		printf("  tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n",
1591 		    i, ring->qid, ring->cur, ring->queued);
1592 	}
1593 	printf("  rx ring: cur=%d\n", sc->rxq.cur);
1594 	printf("  802.11 state %d\n", sc->sc_ic.ic_state);
1595 #undef N
1596 }
1597 
1598 int
1599 wpi_intr(void *arg)
1600 {
1601 	struct wpi_softc *sc = arg;
1602 	struct ifnet *ifp = &sc->sc_ic.ic_if;
1603 	uint32_t r1, r2;
1604 
1605 	/* Disable interrupts. */
1606 	WPI_WRITE(sc, WPI_MASK, 0);
1607 
1608 	r1 = WPI_READ(sc, WPI_INT);
1609 	r2 = WPI_READ(sc, WPI_FH_INT);
1610 
1611 	if (r1 == 0 && r2 == 0) {
1612 		if (ifp->if_flags & IFF_UP)
1613 			WPI_WRITE(sc, WPI_MASK, WPI_INT_MASK);
1614 		return 0;	/* Interrupt not for us. */
1615 	}
1616 	if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0)
1617 		return 0;	/* Hardware gone! */
1618 
1619 	/* Acknowledge interrupts. */
1620 	WPI_WRITE(sc, WPI_INT, r1);
1621 	WPI_WRITE(sc, WPI_FH_INT, r2);
1622 
1623 	if (r1 & (WPI_INT_SW_ERR | WPI_INT_HW_ERR)) {
1624 		printf("%s: fatal firmware error\n", sc->sc_dev.dv_xname);
1625 		/* Dump firmware error log and stop. */
1626 		wpi_fatal_intr(sc);
1627 		wpi_stop(ifp, 1);
1628 		task_add(systq, &sc->init_task);
1629 		return 1;
1630 	}
1631 	if ((r1 & (WPI_INT_FH_RX | WPI_INT_SW_RX)) ||
1632 	    (r2 & WPI_FH_INT_RX))
1633 		wpi_notif_intr(sc);
1634 
1635 	if (r1 & WPI_INT_ALIVE)
1636 		wakeup(sc);	/* Firmware is alive. */
1637 
1638 	/* Re-enable interrupts. */
1639 	if (ifp->if_flags & IFF_UP)
1640 		WPI_WRITE(sc, WPI_MASK, WPI_INT_MASK);
1641 
1642 	return 1;
1643 }
1644 
1645 int
1646 wpi_tx(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
1647 {
1648 	struct ieee80211com *ic = &sc->sc_ic;
1649 	struct wpi_node *wn = (void *)ni;
1650 	struct wpi_tx_ring *ring;
1651 	struct wpi_tx_desc *desc;
1652 	struct wpi_tx_data *data;
1653 	struct wpi_tx_cmd *cmd;
1654 	struct wpi_cmd_data *tx;
1655 	const struct wpi_rate *rinfo;
1656 	struct ieee80211_frame *wh;
1657 	struct ieee80211_key *k = NULL;
1658 	enum ieee80211_edca_ac ac;
1659 	uint32_t flags;
1660 	uint16_t qos;
1661 	u_int hdrlen;
1662 	uint8_t *ivp, tid, ridx, type;
1663 	int i, totlen, hasqos, error;
1664 
1665 	wh = mtod(m, struct ieee80211_frame *);
1666 	hdrlen = ieee80211_get_hdrlen(wh);
1667 	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1668 
1669 	/* Select EDCA Access Category and TX ring for this frame. */
1670 	if ((hasqos = ieee80211_has_qos(wh))) {
1671 		qos = ieee80211_get_qos(wh);
1672 		tid = qos & IEEE80211_QOS_TID;
1673 		ac = ieee80211_up_to_ac(ic, tid);
1674 	} else {
1675 		tid = 0;
1676 		ac = EDCA_AC_BE;
1677 	}
1678 
1679 	ring = &sc->txq[ac];
1680 	desc = &ring->desc[ring->cur];
1681 	data = &ring->data[ring->cur];
1682 
1683 	/* Choose a TX rate index. */
1684 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
1685 	    type != IEEE80211_FC0_TYPE_DATA) {
1686 		ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
1687 		    WPI_RIDX_OFDM6 : WPI_RIDX_CCK1;
1688 	} else if (ic->ic_fixed_rate != -1) {
1689 		ridx = sc->fixed_ridx;
1690 	} else
1691 		ridx = wn->ridx[ni->ni_txrate];
1692 	rinfo = &wpi_rates[ridx];
1693 
1694 #if NBPFILTER > 0
1695 	if (sc->sc_drvbpf != NULL) {
1696 		struct mbuf mb;
1697 		struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
1698 
1699 		tap->wt_flags = 0;
1700 		tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
1701 		tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
1702 		tap->wt_rate = rinfo->rate;
1703 		tap->wt_hwqueue = ac;
1704 		if ((ic->ic_flags & IEEE80211_F_WEPON) &&
1705 		    (wh->i_fc[1] & IEEE80211_FC1_PROTECTED))
1706 			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1707 
1708 		mb.m_data = (caddr_t)tap;
1709 		mb.m_len = sc->sc_txtap_len;
1710 		mb.m_next = m;
1711 		mb.m_nextpkt = NULL;
1712 		mb.m_type = 0;
1713 		mb.m_flags = 0;
1714 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
1715 	}
1716 #endif
1717 
1718 	totlen = m->m_pkthdr.len;
1719 
1720 	/* Encrypt the frame if need be. */
1721 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1722 		/* Retrieve key for TX. */
1723 		k = ieee80211_get_txkey(ic, wh, ni);
1724 		if (k->k_cipher != IEEE80211_CIPHER_CCMP) {
1725 			/* Do software encryption. */
1726 			if ((m = ieee80211_encrypt(ic, m, k)) == NULL)
1727 				return ENOBUFS;
1728 			/* 802.11 header may have moved. */
1729 			wh = mtod(m, struct ieee80211_frame *);
1730 			totlen = m->m_pkthdr.len;
1731 
1732 		} else	/* HW appends CCMP MIC. */
1733 			totlen += IEEE80211_CCMP_HDRLEN;
1734 	}
1735 
1736 	/* Prepare TX firmware command. */
1737 	cmd = &ring->cmd[ring->cur];
1738 	cmd->code = WPI_CMD_TX_DATA;
1739 	cmd->flags = 0;
1740 	cmd->qid = ring->qid;
1741 	cmd->idx = ring->cur;
1742 
1743 	tx = (struct wpi_cmd_data *)cmd->data;
1744 	/* NB: No need to clear tx, all fields are reinitialized here. */
1745 
1746 	flags = 0;
1747 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1748 		/* Unicast frame, check if an ACK is expected. */
1749 		if (!hasqos || (qos & IEEE80211_QOS_ACK_POLICY_MASK) !=
1750 		    IEEE80211_QOS_ACK_POLICY_NOACK)
1751 			flags |= WPI_TX_NEED_ACK;
1752 	}
1753 
1754 	/* Check if frame must be protected using RTS/CTS or CTS-to-self. */
1755 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1756 		/* NB: Group frames are sent using CCK in 802.11b/g. */
1757 		if (totlen + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) {
1758 			flags |= WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP;
1759 		} else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1760 		    ridx <= WPI_RIDX_OFDM54) {
1761 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
1762 				flags |= WPI_TX_NEED_CTS | WPI_TX_FULL_TXOP;
1763 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
1764 				flags |= WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP;
1765 		}
1766 	}
1767 
1768 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
1769 	    type != IEEE80211_FC0_TYPE_DATA)
1770 		tx->id = WPI_ID_BROADCAST;
1771 	else
1772 		tx->id = wn->id;
1773 
1774 	if (type == IEEE80211_FC0_TYPE_MGT) {
1775 		uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1776 
1777 #ifndef IEEE80211_STA_ONLY
1778 		/* Tell HW to set timestamp in probe responses. */
1779 		if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1780 			flags |= WPI_TX_INSERT_TSTAMP;
1781 #endif
1782 		if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
1783 		    subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
1784 			tx->timeout = htole16(3);
1785 		else
1786 			tx->timeout = htole16(2);
1787 	} else
1788 		tx->timeout = htole16(0);
1789 
1790 	tx->len = htole16(totlen);
1791 	tx->tid = tid;
1792 	tx->rts_ntries = 7;
1793 	tx->data_ntries = 15;
1794 	tx->ofdm_mask = 0xff;
1795 	tx->cck_mask = 0x0f;
1796 	tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
1797 	tx->plcp = rinfo->plcp;
1798 
1799 	/* Copy 802.11 header in TX command. */
1800 	memcpy((uint8_t *)(tx + 1), wh, hdrlen);
1801 
1802 	if (k != NULL && k->k_cipher == IEEE80211_CIPHER_CCMP) {
1803 		/* Trim 802.11 header and prepend CCMP IV. */
1804 		m_adj(m, hdrlen - IEEE80211_CCMP_HDRLEN);
1805 		ivp = mtod(m, uint8_t *);
1806 		k->k_tsc++;
1807 		ivp[0] = k->k_tsc;
1808 		ivp[1] = k->k_tsc >> 8;
1809 		ivp[2] = 0;
1810 		ivp[3] = k->k_id << 6 | IEEE80211_WEP_EXTIV;
1811 		ivp[4] = k->k_tsc >> 16;
1812 		ivp[5] = k->k_tsc >> 24;
1813 		ivp[6] = k->k_tsc >> 32;
1814 		ivp[7] = k->k_tsc >> 40;
1815 
1816 		tx->security = WPI_CIPHER_CCMP;
1817 		memcpy(tx->key, k->k_key, k->k_len);
1818 	} else {
1819 		/* Trim 802.11 header. */
1820 		m_adj(m, hdrlen);
1821 		tx->security = 0;
1822 	}
1823 	tx->flags = htole32(flags);
1824 
1825 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m,
1826 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
1827 	if (error != 0 && error != EFBIG) {
1828 		printf("%s: can't map mbuf (error %d)\n",
1829 		    sc->sc_dev.dv_xname, error);
1830 		m_freem(m);
1831 		return error;
1832 	}
1833 	if (error != 0) {
1834 		/* Too many DMA segments, linearize mbuf. */
1835 		if (m_defrag(m, M_DONTWAIT)) {
1836 			m_freem(m);
1837 			return ENOBUFS;
1838 		}
1839 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m,
1840 		    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
1841 		if (error != 0) {
1842 			printf("%s: can't map mbuf (error %d)\n",
1843 			    sc->sc_dev.dv_xname, error);
1844 			m_freem(m);
1845 			return error;
1846 		}
1847 	}
1848 
1849 	data->m = m;
1850 	data->ni = ni;
1851 
1852 	DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
1853 	    ring->qid, ring->cur, m->m_pkthdr.len, data->map->dm_nsegs));
1854 
1855 	/* Fill TX descriptor. */
1856 	desc->flags = htole32(WPI_PAD32(m->m_pkthdr.len) << 28 |
1857 	    (1 + data->map->dm_nsegs) << 24);
1858 	/* First DMA segment is used by the TX command. */
1859 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
1860 	    ring->cur * sizeof (struct wpi_tx_cmd));
1861 	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_data) +
1862 	    ((hdrlen + 3) & ~3));
1863 	/* Other DMA segments are for data payload. */
1864 	for (i = 1; i <= data->map->dm_nsegs; i++) {
1865 		desc->segs[i].addr =
1866 		    htole32(data->map->dm_segs[i - 1].ds_addr);
1867 		desc->segs[i].len  =
1868 		    htole32(data->map->dm_segs[i - 1].ds_len);
1869 	}
1870 
1871 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
1872 	    BUS_DMASYNC_PREWRITE);
1873 	bus_dmamap_sync(sc->sc_dmat, ring->cmd_dma.map,
1874 	    (caddr_t)cmd - ring->cmd_dma.vaddr, sizeof (*cmd),
1875 	    BUS_DMASYNC_PREWRITE);
1876 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
1877 	    (caddr_t)desc - ring->desc_dma.vaddr, sizeof (*desc),
1878 	    BUS_DMASYNC_PREWRITE);
1879 
1880 	/* Kick TX ring. */
1881 	ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
1882 	WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
1883 
1884 	/* Mark TX ring as full if we reach a certain threshold. */
1885 	if (++ring->queued > WPI_TX_RING_HIMARK)
1886 		sc->qfullmsk |= 1 << ring->qid;
1887 
1888 	return 0;
1889 }
1890 
1891 void
1892 wpi_start(struct ifnet *ifp)
1893 {
1894 	struct wpi_softc *sc = ifp->if_softc;
1895 	struct ieee80211com *ic = &sc->sc_ic;
1896 	struct ieee80211_node *ni;
1897 	struct mbuf *m;
1898 
1899 	if (!(ifp->if_flags & IFF_RUNNING) || ifq_is_oactive(&ifp->if_snd))
1900 		return;
1901 
1902 	for (;;) {
1903 		if (sc->qfullmsk != 0) {
1904 			ifq_set_oactive(&ifp->if_snd);
1905 			break;
1906 		}
1907 		/* Send pending management frames first. */
1908 		m = mq_dequeue(&ic->ic_mgtq);
1909 		if (m != NULL) {
1910 			ni = m->m_pkthdr.ph_cookie;
1911 			goto sendit;
1912 		}
1913 		if (ic->ic_state != IEEE80211_S_RUN)
1914 			break;
1915 
1916 		/* Encapsulate and send data frames. */
1917 		IFQ_DEQUEUE(&ifp->if_snd, m);
1918 		if (m == NULL)
1919 			break;
1920 #if NBPFILTER > 0
1921 		if (ifp->if_bpf != NULL)
1922 			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
1923 #endif
1924 		if ((m = ieee80211_encap(ifp, m, &ni)) == NULL)
1925 			continue;
1926 sendit:
1927 #if NBPFILTER > 0
1928 		if (ic->ic_rawbpf != NULL)
1929 			bpf_mtap(ic->ic_rawbpf, m, BPF_DIRECTION_OUT);
1930 #endif
1931 		if (wpi_tx(sc, m, ni) != 0) {
1932 			ieee80211_release_node(ic, ni);
1933 			ifp->if_oerrors++;
1934 			continue;
1935 		}
1936 
1937 		sc->sc_tx_timer = 5;
1938 		ifp->if_timer = 1;
1939 	}
1940 }
1941 
1942 void
1943 wpi_watchdog(struct ifnet *ifp)
1944 {
1945 	struct wpi_softc *sc = ifp->if_softc;
1946 
1947 	ifp->if_timer = 0;
1948 
1949 	if (sc->sc_tx_timer > 0) {
1950 		if (--sc->sc_tx_timer == 0) {
1951 			printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1952 			ifp->if_flags &= ~IFF_UP;
1953 			wpi_stop(ifp, 1);
1954 			ifp->if_oerrors++;
1955 			return;
1956 		}
1957 		ifp->if_timer = 1;
1958 	}
1959 
1960 	ieee80211_watchdog(ifp);
1961 }
1962 
1963 int
1964 wpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1965 {
1966 	struct wpi_softc *sc = ifp->if_softc;
1967 	struct ieee80211com *ic = &sc->sc_ic;
1968 	struct ifreq *ifr;
1969 	int s, error = 0;
1970 
1971 	s = splnet();
1972 	/*
1973 	 * Prevent processes from entering this function while another
1974 	 * process is tsleep'ing in it.
1975 	 */
1976 	while ((sc->sc_flags & WPI_FLAG_BUSY) && error == 0)
1977 		error = tsleep(&sc->sc_flags, PCATCH, "wpiioc", 0);
1978 	if (error != 0) {
1979 		splx(s);
1980 		return error;
1981 	}
1982 	sc->sc_flags |= WPI_FLAG_BUSY;
1983 
1984 	switch (cmd) {
1985 	case SIOCSIFADDR:
1986 		ifp->if_flags |= IFF_UP;
1987 		/* FALLTHROUGH */
1988 	case SIOCSIFFLAGS:
1989 		if (ifp->if_flags & IFF_UP) {
1990 			if (!(ifp->if_flags & IFF_RUNNING))
1991 				error = wpi_init(ifp);
1992 		} else {
1993 			if (ifp->if_flags & IFF_RUNNING)
1994 				wpi_stop(ifp, 1);
1995 		}
1996 		break;
1997 
1998 	case SIOCADDMULTI:
1999 	case SIOCDELMULTI:
2000 		ifr = (struct ifreq *)data;
2001 		error = (cmd == SIOCADDMULTI) ?
2002 		    ether_addmulti(ifr, &ic->ic_ac) :
2003 		    ether_delmulti(ifr, &ic->ic_ac);
2004 
2005 		if (error == ENETRESET)
2006 			error = 0;
2007 		break;
2008 
2009 	case SIOCS80211POWER:
2010 		error = ieee80211_ioctl(ifp, cmd, data);
2011 		if (error != ENETRESET)
2012 			break;
2013 		if (ic->ic_state == IEEE80211_S_RUN) {
2014 			if (ic->ic_flags & IEEE80211_F_PMGTON)
2015 				error = wpi_set_pslevel(sc, 0, 3, 0);
2016 			else	/* back to CAM */
2017 				error = wpi_set_pslevel(sc, 0, 0, 0);
2018 		} else {
2019 			/* Defer until transition to IEEE80211_S_RUN. */
2020 			error = 0;
2021 		}
2022 		break;
2023 
2024 	default:
2025 		error = ieee80211_ioctl(ifp, cmd, data);
2026 	}
2027 
2028 	if (error == ENETRESET) {
2029 		error = 0;
2030 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
2031 		    (IFF_UP | IFF_RUNNING)) {
2032 			wpi_stop(ifp, 0);
2033 			error = wpi_init(ifp);
2034 		}
2035 	}
2036 
2037 	sc->sc_flags &= ~WPI_FLAG_BUSY;
2038 	wakeup(&sc->sc_flags);
2039 	splx(s);
2040 	return error;
2041 }
2042 
2043 /*
2044  * Send a command to the firmware.
2045  */
2046 int
2047 wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
2048 {
2049 	struct wpi_tx_ring *ring = &sc->txq[4];
2050 	struct wpi_tx_desc *desc;
2051 	struct wpi_tx_data *data;
2052 	struct wpi_tx_cmd *cmd;
2053 	struct mbuf *m;
2054 	bus_addr_t paddr;
2055 	int totlen, error;
2056 
2057 	desc = &ring->desc[ring->cur];
2058 	data = &ring->data[ring->cur];
2059 	totlen = 4 + size;
2060 
2061 	if (size > sizeof cmd->data) {
2062 		/* Command is too large to fit in a descriptor. */
2063 		if (totlen > MCLBYTES)
2064 			return EINVAL;
2065 		MGETHDR(m, M_DONTWAIT, MT_DATA);
2066 		if (m == NULL)
2067 			return ENOMEM;
2068 		if (totlen > MHLEN) {
2069 			MCLGET(m, M_DONTWAIT);
2070 			if (!(m->m_flags & M_EXT)) {
2071 				m_freem(m);
2072 				return ENOMEM;
2073 			}
2074 		}
2075 		cmd = mtod(m, struct wpi_tx_cmd *);
2076 		error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, totlen,
2077 		    NULL, BUS_DMA_NOWAIT | BUS_DMA_WRITE);
2078 		if (error != 0) {
2079 			m_freem(m);
2080 			return error;
2081 		}
2082 		data->m = m;
2083 		paddr = data->map->dm_segs[0].ds_addr;
2084 	} else {
2085 		cmd = &ring->cmd[ring->cur];
2086 		paddr = data->cmd_paddr;
2087 	}
2088 
2089 	cmd->code = code;
2090 	cmd->flags = 0;
2091 	cmd->qid = ring->qid;
2092 	cmd->idx = ring->cur;
2093 	memcpy(cmd->data, buf, size);
2094 
2095 	desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
2096 	desc->segs[0].addr = htole32(paddr);
2097 	desc->segs[0].len  = htole32(totlen);
2098 
2099 	if (size > sizeof cmd->data) {
2100 		bus_dmamap_sync(sc->sc_dmat, data->map, 0, totlen,
2101 		    BUS_DMASYNC_PREWRITE);
2102 	} else {
2103 		bus_dmamap_sync(sc->sc_dmat, ring->cmd_dma.map,
2104 		    (caddr_t)cmd - ring->cmd_dma.vaddr, totlen,
2105 		    BUS_DMASYNC_PREWRITE);
2106 	}
2107 	bus_dmamap_sync(sc->sc_dmat, ring->desc_dma.map,
2108 	    (caddr_t)desc - ring->desc_dma.vaddr, sizeof (*desc),
2109 	    BUS_DMASYNC_PREWRITE);
2110 
2111 	/* Kick command ring. */
2112 	ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
2113 	WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
2114 
2115 	return async ? 0 : tsleep(cmd, PCATCH, "wpicmd", hz);
2116 }
2117 
2118 /*
2119  * Configure HW multi-rate retries.
2120  */
2121 int
2122 wpi_mrr_setup(struct wpi_softc *sc)
2123 {
2124 	struct ieee80211com *ic = &sc->sc_ic;
2125 	struct wpi_mrr_setup mrr;
2126 	int i, error;
2127 
2128 	/* CCK rates (not used with 802.11a). */
2129 	for (i = WPI_RIDX_CCK1; i <= WPI_RIDX_CCK11; i++) {
2130 		mrr.rates[i].flags = 0;
2131 		mrr.rates[i].plcp = wpi_rates[i].plcp;
2132 		/* Fallback to the immediate lower CCK rate (if any.) */
2133 		mrr.rates[i].next =
2134 		    (i == WPI_RIDX_CCK1) ? WPI_RIDX_CCK1 : i - 1;
2135 		/* Try one time at this rate before falling back to "next". */
2136 		mrr.rates[i].ntries = 1;
2137 	}
2138 	/* OFDM rates (not used with 802.11b). */
2139 	for (i = WPI_RIDX_OFDM6; i <= WPI_RIDX_OFDM54; i++) {
2140 		mrr.rates[i].flags = 0;
2141 		mrr.rates[i].plcp = wpi_rates[i].plcp;
2142 		/* Fallback to the immediate lower rate (if any.) */
2143 		/* We allow fallback from OFDM/6 to CCK/2 in 11b/g mode. */
2144 		mrr.rates[i].next = (i == WPI_RIDX_OFDM6) ?
2145 		    ((ic->ic_curmode == IEEE80211_MODE_11A) ?
2146 			WPI_RIDX_OFDM6 : WPI_RIDX_CCK2) :
2147 		    i - 1;
2148 		/* Try one time at this rate before falling back to "next". */
2149 		mrr.rates[i].ntries = 1;
2150 	}
2151 	/* Setup MRR for control frames. */
2152 	mrr.which = htole32(WPI_MRR_CTL);
2153 	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2154 	if (error != 0) {
2155 		printf("%s: could not setup MRR for control frames\n",
2156 		    sc->sc_dev.dv_xname);
2157 		return error;
2158 	}
2159 	/* Setup MRR for data frames. */
2160 	mrr.which = htole32(WPI_MRR_DATA);
2161 	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2162 	if (error != 0) {
2163 		printf("%s: could not setup MRR for data frames\n",
2164 		    sc->sc_dev.dv_xname);
2165 		return error;
2166 	}
2167 	return 0;
2168 }
2169 
2170 void
2171 wpi_updateedca(struct ieee80211com *ic)
2172 {
2173 #define WPI_EXP2(x)	((1 << (x)) - 1)	/* CWmin = 2^ECWmin - 1 */
2174 	struct wpi_softc *sc = ic->ic_softc;
2175 	struct wpi_edca_params cmd;
2176 	int aci;
2177 
2178 	memset(&cmd, 0, sizeof cmd);
2179 	cmd.flags = htole32(WPI_EDCA_UPDATE);
2180 	for (aci = 0; aci < EDCA_NUM_AC; aci++) {
2181 		const struct ieee80211_edca_ac_params *ac =
2182 		    &ic->ic_edca_ac[aci];
2183 		cmd.ac[aci].aifsn = ac->ac_aifsn;
2184 		cmd.ac[aci].cwmin = htole16(WPI_EXP2(ac->ac_ecwmin));
2185 		cmd.ac[aci].cwmax = htole16(WPI_EXP2(ac->ac_ecwmax));
2186 		cmd.ac[aci].txoplimit =
2187 		    htole16(IEEE80211_TXOP_TO_US(ac->ac_txoplimit));
2188 	}
2189 	(void)wpi_cmd(sc, WPI_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
2190 #undef WPI_EXP2
2191 }
2192 
2193 void
2194 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
2195 {
2196 	struct wpi_cmd_led led;
2197 
2198 	led.which = which;
2199 	led.unit = htole32(100000);	/* on/off in unit of 100ms */
2200 	led.off = off;
2201 	led.on = on;
2202 	(void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
2203 }
2204 
2205 int
2206 wpi_set_timing(struct wpi_softc *sc, struct ieee80211_node *ni)
2207 {
2208 	struct wpi_cmd_timing cmd;
2209 	uint64_t val, mod;
2210 
2211 	memset(&cmd, 0, sizeof cmd);
2212 	memcpy(&cmd.tstamp, ni->ni_tstamp, sizeof (uint64_t));
2213 	cmd.bintval = htole16(ni->ni_intval);
2214 	cmd.lintval = htole16(10);
2215 
2216 	/* Compute remaining time until next beacon. */
2217 	val = (uint64_t)ni->ni_intval * 1024;	/* msecs -> usecs */
2218 	mod = letoh64(cmd.tstamp) % val;
2219 	cmd.binitval = htole32((uint32_t)(val - mod));
2220 
2221 	DPRINTF(("timing bintval=%u, tstamp=%llu, init=%u\n",
2222 	    ni->ni_intval, letoh64(cmd.tstamp), (uint32_t)(val - mod)));
2223 
2224 	return wpi_cmd(sc, WPI_CMD_TIMING, &cmd, sizeof cmd, 1);
2225 }
2226 
2227 /*
2228  * This function is called periodically (every minute) to adjust TX power
2229  * based on temperature variation.
2230  */
2231 void
2232 wpi_power_calibration(struct wpi_softc *sc)
2233 {
2234 	int temp;
2235 
2236 	temp = (int)WPI_READ(sc, WPI_UCODE_GP2);
2237 	/* Sanity-check temperature. */
2238 	if (temp < -260 || temp > 25) {
2239 		/* This can't be correct, ignore. */
2240 		DPRINTF(("out-of-range temperature reported: %d\n", temp));
2241 		return;
2242 	}
2243 	DPRINTF(("temperature %d->%d\n", sc->temp, temp));
2244 	/* Adjust TX power if need be (delta > 6). */
2245 	if (abs(temp - sc->temp) > 6) {
2246 		/* Record temperature of last calibration. */
2247 		sc->temp = temp;
2248 		(void)wpi_set_txpower(sc, 1);
2249 	}
2250 }
2251 
2252 /*
2253  * Set TX power for current channel (each rate has its own power settings).
2254  */
2255 int
2256 wpi_set_txpower(struct wpi_softc *sc, int async)
2257 {
2258 	struct ieee80211com *ic = &sc->sc_ic;
2259 	struct ieee80211_channel *ch;
2260 	struct wpi_power_group *group;
2261 	struct wpi_cmd_txpower cmd;
2262 	u_int chan;
2263 	int idx, i;
2264 
2265 	/* Retrieve current channel from last RXON. */
2266 	chan = sc->rxon.chan;
2267 	DPRINTF(("setting TX power for channel %d\n", chan));
2268 	ch = &ic->ic_channels[chan];
2269 
2270 	/* Find the TX power group to which this channel belongs. */
2271 	if (IEEE80211_IS_CHAN_5GHZ(ch)) {
2272 		for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
2273 			if (chan <= group->chan)
2274 				break;
2275 	} else
2276 		group = &sc->groups[0];
2277 
2278 	memset(&cmd, 0, sizeof cmd);
2279 	cmd.band = IEEE80211_IS_CHAN_5GHZ(ch) ? 0 : 1;
2280 	cmd.chan = htole16(chan);
2281 
2282 	/* Set TX power for all OFDM and CCK rates. */
2283 	for (i = 0; i <= WPI_RIDX_MAX ; i++) {
2284 		/* Retrieve TX power for this channel/rate. */
2285 		idx = wpi_get_power_index(sc, group, ch, i);
2286 
2287 		cmd.rates[i].plcp = wpi_rates[i].plcp;
2288 
2289 		if (IEEE80211_IS_CHAN_5GHZ(ch)) {
2290 			cmd.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
2291 			cmd.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
2292 		} else {
2293 			cmd.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
2294 			cmd.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
2295 		}
2296 		DPRINTF(("chan %d/rate %d: power index %d\n", chan,
2297 		    wpi_rates[i].rate, idx));
2298 	}
2299 	return wpi_cmd(sc, WPI_CMD_TXPOWER, &cmd, sizeof cmd, async);
2300 }
2301 
2302 /*
2303  * Determine TX power index for a given channel/rate combination.
2304  * This takes into account the regulatory information from EEPROM and the
2305  * current temperature.
2306  */
2307 int
2308 wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
2309     struct ieee80211_channel *c, int ridx)
2310 {
2311 /* Fixed-point arithmetic division using a n-bit fractional part. */
2312 #define fdivround(a, b, n)	\
2313 	((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
2314 
2315 /* Linear interpolation. */
2316 #define interpolate(x, x1, y1, x2, y2, n)	\
2317 	((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
2318 
2319 	struct ieee80211com *ic = &sc->sc_ic;
2320 	struct wpi_power_sample *sample;
2321 	int pwr, idx;
2322 	u_int chan;
2323 
2324 	/* Get channel number. */
2325 	chan = ieee80211_chan2ieee(ic, c);
2326 
2327 	/* Default TX power is group maximum TX power minus 3dB. */
2328 	pwr = group->maxpwr / 2;
2329 
2330 	/* Decrease TX power for highest OFDM rates to reduce distortion. */
2331 	switch (ridx) {
2332 	case WPI_RIDX_OFDM36:
2333 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 :  5;
2334 		break;
2335 	case WPI_RIDX_OFDM48:
2336 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10;
2337 		break;
2338 	case WPI_RIDX_OFDM54:
2339 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12;
2340 		break;
2341 	}
2342 
2343 	/* Never exceed the channel maximum allowed TX power. */
2344 	pwr = MIN(pwr, sc->maxpwr[chan]);
2345 
2346 	/* Retrieve TX power index into gain tables from samples. */
2347 	for (sample = group->samples; sample < &group->samples[3]; sample++)
2348 		if (pwr > sample[1].power)
2349 			break;
2350 	/* Fixed-point linear interpolation using a 19-bit fractional part. */
2351 	idx = interpolate(pwr, sample[0].power, sample[0].index,
2352 	    sample[1].power, sample[1].index, 19);
2353 
2354 	/*-
2355 	 * Adjust power index based on current temperature:
2356 	 * - if cooler than factory-calibrated: decrease output power
2357 	 * - if warmer than factory-calibrated: increase output power
2358 	 */
2359 	idx -= (sc->temp - group->temp) * 11 / 100;
2360 
2361 	/* Decrease TX power for CCK rates (-5dB). */
2362 	if (ridx >= WPI_RIDX_CCK1)
2363 		idx += 10;
2364 
2365 	/* Make sure idx stays in a valid range. */
2366 	if (idx < 0)
2367 		idx = 0;
2368 	else if (idx > WPI_MAX_PWR_INDEX)
2369 		idx = WPI_MAX_PWR_INDEX;
2370 	return idx;
2371 
2372 #undef interpolate
2373 #undef fdivround
2374 }
2375 
2376 /*
2377  * Set STA mode power saving level (between 0 and 5).
2378  * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving.
2379  */
2380 int
2381 wpi_set_pslevel(struct wpi_softc *sc, int dtim, int level, int async)
2382 {
2383 	struct wpi_pmgt_cmd cmd;
2384 	const struct wpi_pmgt *pmgt;
2385 	uint32_t max, skip_dtim;
2386 	pcireg_t reg;
2387 	int i;
2388 
2389 	/* Select which PS parameters to use. */
2390 	if (dtim <= 10)
2391 		pmgt = &wpi_pmgt[0][level];
2392 	else
2393 		pmgt = &wpi_pmgt[1][level];
2394 
2395 	memset(&cmd, 0, sizeof cmd);
2396 	if (level != 0)	/* not CAM */
2397 		cmd.flags |= htole16(WPI_PS_ALLOW_SLEEP);
2398 	/* Retrieve PCIe Active State Power Management (ASPM). */
2399 	reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
2400 	    sc->sc_cap_off + PCI_PCIE_LCSR);
2401 	if (!(reg & PCI_PCIE_LCSR_ASPM_L0S))	/* L0s Entry disabled. */
2402 		cmd.flags |= htole16(WPI_PS_PCI_PMGT);
2403 	cmd.rxtimeout = htole32(pmgt->rxtimeout * 1024);
2404 	cmd.txtimeout = htole32(pmgt->txtimeout * 1024);
2405 
2406 	if (dtim == 0) {
2407 		dtim = 1;
2408 		skip_dtim = 0;
2409 	} else
2410 		skip_dtim = pmgt->skip_dtim;
2411 	if (skip_dtim != 0) {
2412 		cmd.flags |= htole16(WPI_PS_SLEEP_OVER_DTIM);
2413 		max = pmgt->intval[4];
2414 		if (max == (uint32_t)-1)
2415 			max = dtim * (skip_dtim + 1);
2416 		else if (max > dtim)
2417 			max = (max / dtim) * dtim;
2418 	} else
2419 		max = dtim;
2420 	for (i = 0; i < 5; i++)
2421 		cmd.intval[i] = htole32(MIN(max, pmgt->intval[i]));
2422 
2423 	DPRINTF(("setting power saving level to %d\n", level));
2424 	return wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async);
2425 }
2426 
2427 int
2428 wpi_config(struct wpi_softc *sc)
2429 {
2430 	struct ieee80211com *ic = &sc->sc_ic;
2431 	struct ifnet *ifp = &ic->ic_if;
2432 	struct wpi_bluetooth bluetooth;
2433 	struct wpi_node_info node;
2434 	int error;
2435 
2436 	/* Set power saving level to CAM during initialization. */
2437 	if ((error = wpi_set_pslevel(sc, 0, 0, 0)) != 0) {
2438 		printf("%s: could not set power saving level\n",
2439 		    sc->sc_dev.dv_xname);
2440 		return error;
2441 	}
2442 
2443 	/* Configure bluetooth coexistence. */
2444 	memset(&bluetooth, 0, sizeof bluetooth);
2445 	bluetooth.flags = WPI_BT_COEX_MODE_4WIRE;
2446 	bluetooth.lead_time = WPI_BT_LEAD_TIME_DEF;
2447 	bluetooth.max_kill = WPI_BT_MAX_KILL_DEF;
2448 	error = wpi_cmd(sc, WPI_CMD_BT_COEX, &bluetooth, sizeof bluetooth, 0);
2449 	if (error != 0) {
2450 		printf("%s: could not configure bluetooth coexistence\n",
2451 		    sc->sc_dev.dv_xname);
2452 		return error;
2453 	}
2454 
2455 	/* Configure adapter. */
2456 	memset(&sc->rxon, 0, sizeof (struct wpi_rxon));
2457 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
2458 	IEEE80211_ADDR_COPY(sc->rxon.myaddr, ic->ic_myaddr);
2459 	/* Set default channel. */
2460 	sc->rxon.chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
2461 	sc->rxon.flags = htole32(WPI_RXON_TSF);
2462 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan))
2463 		sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
2464 	switch (ic->ic_opmode) {
2465 	case IEEE80211_M_STA:
2466 		sc->rxon.mode = WPI_MODE_STA;
2467 		sc->rxon.filter = htole32(WPI_FILTER_MULTICAST);
2468 		break;
2469 	case IEEE80211_M_MONITOR:
2470 		sc->rxon.mode = WPI_MODE_MONITOR;
2471 		sc->rxon.filter = htole32(WPI_FILTER_MULTICAST |
2472 		    WPI_FILTER_CTL | WPI_FILTER_PROMISC);
2473 		break;
2474 	default:
2475 		/* Should not get there. */
2476 		break;
2477 	}
2478 	sc->rxon.cck_mask  = 0x0f;	/* not yet negotiated */
2479 	sc->rxon.ofdm_mask = 0xff;	/* not yet negotiated */
2480 	DPRINTF(("setting configuration\n"));
2481 	error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon, sizeof (struct wpi_rxon),
2482 	    0);
2483 	if (error != 0) {
2484 		printf("%s: RXON command failed\n", sc->sc_dev.dv_xname);
2485 		return error;
2486 	}
2487 
2488 	/* Configuration has changed, set TX power accordingly. */
2489 	if ((error = wpi_set_txpower(sc, 0)) != 0) {
2490 		printf("%s: could not set TX power\n", sc->sc_dev.dv_xname);
2491 		return error;
2492 	}
2493 
2494 	/* Add broadcast node. */
2495 	memset(&node, 0, sizeof node);
2496 	IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr);
2497 	node.id = WPI_ID_BROADCAST;
2498 	node.plcp = wpi_rates[WPI_RIDX_CCK1].plcp;
2499 	node.action = htole32(WPI_ACTION_SET_RATE);
2500 	node.antenna = WPI_ANTENNA_BOTH;
2501 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
2502 	if (error != 0) {
2503 		printf("%s: could not add broadcast node\n",
2504 		    sc->sc_dev.dv_xname);
2505 		return error;
2506 	}
2507 
2508 	if ((error = wpi_mrr_setup(sc)) != 0) {
2509 		printf("%s: could not setup MRR\n", sc->sc_dev.dv_xname);
2510 		return error;
2511 	}
2512 	return 0;
2513 }
2514 
2515 int
2516 wpi_scan(struct wpi_softc *sc, uint16_t flags)
2517 {
2518 	struct ieee80211com *ic = &sc->sc_ic;
2519 	struct wpi_scan_hdr *hdr;
2520 	struct wpi_cmd_data *tx;
2521 	struct wpi_scan_essid *essid;
2522 	struct wpi_scan_chan *chan;
2523 	struct ieee80211_frame *wh;
2524 	struct ieee80211_rateset *rs;
2525 	struct ieee80211_channel *c;
2526 	uint8_t *buf, *frm;
2527 	int buflen, error;
2528 
2529 	buf = malloc(WPI_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO);
2530 	if (buf == NULL) {
2531 		printf("%s: could not allocate buffer for scan command\n",
2532 		    sc->sc_dev.dv_xname);
2533 		return ENOMEM;
2534 	}
2535 	hdr = (struct wpi_scan_hdr *)buf;
2536 	/*
2537 	 * Move to the next channel if no frames are received within 10ms
2538 	 * after sending the probe request.
2539 	 */
2540 	hdr->quiet_time = htole16(10);		/* timeout in milliseconds */
2541 	hdr->quiet_threshold = htole16(1);	/* min # of packets */
2542 
2543 	tx = (struct wpi_cmd_data *)(hdr + 1);
2544 	tx->flags = htole32(WPI_TX_AUTO_SEQ);
2545 	tx->id = WPI_ID_BROADCAST;
2546 	tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2547 
2548 	if (flags & IEEE80211_CHAN_5GHZ) {
2549 		hdr->crc_threshold = htole16(1);
2550 		/* Send probe requests at 6Mbps. */
2551 		tx->plcp = wpi_rates[WPI_RIDX_OFDM6].plcp;
2552 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
2553 	} else {
2554 		hdr->flags = htole32(WPI_RXON_24GHZ | WPI_RXON_AUTO);
2555 		/* Send probe requests at 1Mbps. */
2556 		tx->plcp = wpi_rates[WPI_RIDX_CCK1].plcp;
2557 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
2558 	}
2559 
2560 	essid = (struct wpi_scan_essid *)(tx + 1);
2561 	if (ic->ic_des_esslen != 0) {
2562 		essid[0].id  = IEEE80211_ELEMID_SSID;
2563 		essid[0].len = ic->ic_des_esslen;
2564 		memcpy(essid[0].data, ic->ic_des_essid, ic->ic_des_esslen);
2565 	}
2566 	/*
2567 	 * Build a probe request frame.  Most of the following code is a
2568 	 * copy & paste of what is done in net80211.
2569 	 */
2570 	wh = (struct ieee80211_frame *)(essid + 4);
2571 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2572 	    IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2573 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2574 	IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
2575 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
2576 	IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr);
2577 	*(uint16_t *)&wh->i_dur[0] = 0;	/* filled by HW */
2578 	*(uint16_t *)&wh->i_seq[0] = 0;	/* filled by HW */
2579 
2580 	frm = (uint8_t *)(wh + 1);
2581 	frm = ieee80211_add_ssid(frm, NULL, 0);
2582 	frm = ieee80211_add_rates(frm, rs);
2583 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
2584 		frm = ieee80211_add_xrates(frm, rs);
2585 
2586 	/* Set length of probe request. */
2587 	tx->len = htole16(frm - (uint8_t *)wh);
2588 
2589 	chan = (struct wpi_scan_chan *)frm;
2590 	for (c  = &ic->ic_channels[1];
2591 	     c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
2592 		if ((c->ic_flags & flags) != flags)
2593 			continue;
2594 
2595 		chan->chan = ieee80211_chan2ieee(ic, c);
2596 		DPRINTFN(2, ("adding channel %d\n", chan->chan));
2597 		chan->flags = 0;
2598 		if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE))
2599 			chan->flags |= WPI_CHAN_ACTIVE;
2600 		if (ic->ic_des_esslen != 0)
2601 			chan->flags |= WPI_CHAN_NPBREQS(1);
2602 		chan->dsp_gain = 0x6e;
2603 		if (IEEE80211_IS_CHAN_5GHZ(c)) {
2604 			chan->rf_gain = 0x3b;
2605 			chan->active  = htole16(24);
2606 			chan->passive = htole16(110);
2607 		} else {
2608 			chan->rf_gain = 0x28;
2609 			chan->active  = htole16(36);
2610 			chan->passive = htole16(120);
2611 		}
2612 		hdr->nchan++;
2613 		chan++;
2614 	}
2615 
2616 	buflen = (uint8_t *)chan - buf;
2617 	hdr->len = htole16(buflen);
2618 
2619 	DPRINTF(("sending scan command nchan=%d\n", hdr->nchan));
2620 	error = wpi_cmd(sc, WPI_CMD_SCAN, buf, buflen, 1);
2621 	free(buf, M_DEVBUF, WPI_SCAN_MAXSZ);
2622 	return error;
2623 }
2624 
2625 int
2626 wpi_auth(struct wpi_softc *sc)
2627 {
2628 	struct ieee80211com *ic = &sc->sc_ic;
2629 	struct ieee80211_node *ni = ic->ic_bss;
2630 	struct wpi_node_info node;
2631 	int error;
2632 
2633 	/* Update adapter configuration. */
2634 	IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
2635 	sc->rxon.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2636 	sc->rxon.flags = htole32(WPI_RXON_TSF);
2637 	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2638 		sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
2639 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
2640 		sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
2641 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2642 		sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
2643 	switch (ic->ic_curmode) {
2644 	case IEEE80211_MODE_11A:
2645 		sc->rxon.cck_mask  = 0;
2646 		sc->rxon.ofdm_mask = 0x15;
2647 		break;
2648 	case IEEE80211_MODE_11B:
2649 		sc->rxon.cck_mask  = 0x03;
2650 		sc->rxon.ofdm_mask = 0;
2651 		break;
2652 	default:	/* Assume 802.11b/g. */
2653 		sc->rxon.cck_mask  = 0x0f;
2654 		sc->rxon.ofdm_mask = 0x15;
2655 	}
2656 	DPRINTF(("rxon chan %d flags %x cck %x ofdm %x\n", sc->rxon.chan,
2657 	    sc->rxon.flags, sc->rxon.cck_mask, sc->rxon.ofdm_mask));
2658 	error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon, sizeof (struct wpi_rxon),
2659 	    1);
2660 	if (error != 0) {
2661 		printf("%s: RXON command failed\n", sc->sc_dev.dv_xname);
2662 		return error;
2663 	}
2664 
2665 	/* Configuration has changed, set TX power accordingly. */
2666 	if ((error = wpi_set_txpower(sc, 1)) != 0) {
2667 		printf("%s: could not set TX power\n", sc->sc_dev.dv_xname);
2668 		return error;
2669 	}
2670 	/*
2671 	 * Reconfiguring RXON clears the firmware nodes table so we must
2672 	 * add the broadcast node again.
2673 	 */
2674 	memset(&node, 0, sizeof node);
2675 	IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr);
2676 	node.id = WPI_ID_BROADCAST;
2677 	node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2678 	    wpi_rates[WPI_RIDX_OFDM6].plcp : wpi_rates[WPI_RIDX_CCK1].plcp;
2679 	node.action = htole32(WPI_ACTION_SET_RATE);
2680 	node.antenna = WPI_ANTENNA_BOTH;
2681 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2682 	if (error != 0) {
2683 		printf("%s: could not add broadcast node\n",
2684 		    sc->sc_dev.dv_xname);
2685 		return error;
2686 	}
2687 	return 0;
2688 }
2689 
2690 int
2691 wpi_run(struct wpi_softc *sc)
2692 {
2693 	struct ieee80211com *ic = &sc->sc_ic;
2694 	struct ieee80211_node *ni = ic->ic_bss;
2695 	struct wpi_node_info node;
2696 	int error;
2697 
2698 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2699 		/* Link LED blinks while monitoring. */
2700 		wpi_set_led(sc, WPI_LED_LINK, 5, 5);
2701 		return 0;
2702 	}
2703 	if ((error = wpi_set_timing(sc, ni)) != 0) {
2704 		printf("%s: could not set timing\n", sc->sc_dev.dv_xname);
2705 		return error;
2706 	}
2707 
2708 	/* Update adapter configuration. */
2709 	sc->rxon.associd = htole16(IEEE80211_AID(ni->ni_associd));
2710 	/* Short preamble and slot time are negotiated when associating. */
2711 	sc->rxon.flags &= ~htole32(WPI_RXON_SHPREAMBLE | WPI_RXON_SHSLOT);
2712 	if (ic->ic_flags & IEEE80211_F_SHSLOT)
2713 		sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
2714 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2715 		sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
2716 	sc->rxon.filter |= htole32(WPI_FILTER_BSS);
2717 	DPRINTF(("rxon chan %d flags %x\n", sc->rxon.chan, sc->rxon.flags));
2718 	error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon, sizeof (struct wpi_rxon),
2719 	    1);
2720 	if (error != 0) {
2721 		printf("%s: RXON command failed\n", sc->sc_dev.dv_xname);
2722 		return error;
2723 	}
2724 
2725 	/* Configuration has changed, set TX power accordingly. */
2726 	if ((error = wpi_set_txpower(sc, 1)) != 0) {
2727 		printf("%s: could not set TX power\n", sc->sc_dev.dv_xname);
2728 		return error;
2729 	}
2730 
2731 	/* Fake a join to init the TX rate. */
2732 	((struct wpi_node *)ni)->id = WPI_ID_BSS;
2733 	wpi_newassoc(ic, ni, 1);
2734 
2735 	/* Add BSS node. */
2736 	memset(&node, 0, sizeof node);
2737 	IEEE80211_ADDR_COPY(node.macaddr, ni->ni_bssid);
2738 	node.id = WPI_ID_BSS;
2739 	node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2740 	    wpi_rates[WPI_RIDX_OFDM6].plcp : wpi_rates[WPI_RIDX_CCK1].plcp;
2741 	node.action = htole32(WPI_ACTION_SET_RATE);
2742 	node.antenna = WPI_ANTENNA_BOTH;
2743 	DPRINTF(("adding BSS node\n"));
2744 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2745 	if (error != 0) {
2746 		printf("%s: could not add BSS node\n", sc->sc_dev.dv_xname);
2747 		return error;
2748 	}
2749 
2750 	/* Start periodic calibration timer. */
2751 	sc->calib_cnt = 0;
2752 	timeout_add_msec(&sc->calib_to, 500);
2753 
2754 	/* Link LED always on while associated. */
2755 	wpi_set_led(sc, WPI_LED_LINK, 0, 1);
2756 
2757 	/* Enable power-saving mode if requested by user. */
2758 	if (sc->sc_ic.ic_flags & IEEE80211_F_PMGTON)
2759 		(void)wpi_set_pslevel(sc, 0, 3, 1);
2760 
2761 	return 0;
2762 }
2763 
2764 /*
2765  * We support CCMP hardware encryption/decryption of unicast frames only.
2766  * HW support for TKIP really sucks.  We should let TKIP die anyway.
2767  */
2768 int
2769 wpi_set_key(struct ieee80211com *ic, struct ieee80211_node *ni,
2770     struct ieee80211_key *k)
2771 {
2772 	struct wpi_softc *sc = ic->ic_softc;
2773 	struct wpi_node *wn = (void *)ni;
2774 	struct wpi_node_info node;
2775 	uint16_t kflags;
2776 
2777 	if ((k->k_flags & IEEE80211_KEY_GROUP) ||
2778 	    k->k_cipher != IEEE80211_CIPHER_CCMP)
2779 		return ieee80211_set_key(ic, ni, k);
2780 
2781 	kflags = WPI_KFLAG_CCMP | WPI_KFLAG_KID(k->k_id);
2782 	memset(&node, 0, sizeof node);
2783 	node.id = wn->id;
2784 	node.control = WPI_NODE_UPDATE;
2785 	node.flags = WPI_FLAG_SET_KEY;
2786 	node.kflags = htole16(kflags);
2787 	memcpy(node.key, k->k_key, k->k_len);
2788 	DPRINTF(("set key id=%d for node %d\n", k->k_id, node.id));
2789 	return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2790 }
2791 
2792 void
2793 wpi_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni,
2794     struct ieee80211_key *k)
2795 {
2796 	struct wpi_softc *sc = ic->ic_softc;
2797 	struct wpi_node *wn = (void *)ni;
2798 	struct wpi_node_info node;
2799 
2800 	if ((k->k_flags & IEEE80211_KEY_GROUP) ||
2801 	    k->k_cipher != IEEE80211_CIPHER_CCMP) {
2802 		/* See comment about other ciphers above. */
2803 		ieee80211_delete_key(ic, ni, k);
2804 		return;
2805 	}
2806 	if (ic->ic_state != IEEE80211_S_RUN)
2807 		return;	/* Nothing to do. */
2808 	memset(&node, 0, sizeof node);
2809 	node.id = wn->id;
2810 	node.control = WPI_NODE_UPDATE;
2811 	node.flags = WPI_FLAG_SET_KEY;
2812 	node.kflags = 0;
2813 	DPRINTF(("delete keys for node %d\n", node.id));
2814 	(void)wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2815 }
2816 
2817 int
2818 wpi_post_alive(struct wpi_softc *sc)
2819 {
2820 	int ntries, error;
2821 
2822 	/* Check (again) that the radio is not disabled. */
2823 	if ((error = wpi_nic_lock(sc)) != 0)
2824 		return error;
2825 	/* NB: Runtime firmware must be up and running. */
2826 	if (!(wpi_prph_read(sc, WPI_APMG_RFKILL) & 1)) {
2827 		printf("%s: radio is disabled by hardware switch\n",
2828 		    sc->sc_dev.dv_xname);
2829 		wpi_nic_unlock(sc);
2830 		return EPERM;	/* :-) */
2831 	}
2832 	wpi_nic_unlock(sc);
2833 
2834 	/* Wait for thermal sensor to calibrate. */
2835 	for (ntries = 0; ntries < 1000; ntries++) {
2836 		if ((sc->temp = (int)WPI_READ(sc, WPI_UCODE_GP2)) != 0)
2837 			break;
2838 		DELAY(10);
2839 	}
2840 	if (ntries == 1000) {
2841 		printf("%s: timeout waiting for thermal sensor calibration\n",
2842 		    sc->sc_dev.dv_xname);
2843 		return ETIMEDOUT;
2844 	}
2845 	DPRINTF(("temperature %d\n", sc->temp));
2846 	return 0;
2847 }
2848 
2849 /*
2850  * The firmware boot code is small and is intended to be copied directly into
2851  * the NIC internal memory (no DMA transfer.)
2852  */
2853 int
2854 wpi_load_bootcode(struct wpi_softc *sc, const uint8_t *ucode, int size)
2855 {
2856 	int error, ntries;
2857 
2858 	size /= sizeof (uint32_t);
2859 
2860 	if ((error = wpi_nic_lock(sc)) != 0)
2861 		return error;
2862 
2863 	/* Copy microcode image into NIC memory. */
2864 	wpi_prph_write_region_4(sc, WPI_BSM_SRAM_BASE,
2865 	    (const uint32_t *)ucode, size);
2866 
2867 	wpi_prph_write(sc, WPI_BSM_WR_MEM_SRC, 0);
2868 	wpi_prph_write(sc, WPI_BSM_WR_MEM_DST, WPI_FW_TEXT_BASE);
2869 	wpi_prph_write(sc, WPI_BSM_WR_DWCOUNT, size);
2870 
2871 	/* Start boot load now. */
2872 	wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START);
2873 
2874 	/* Wait for transfer to complete. */
2875 	for (ntries = 0; ntries < 1000; ntries++) {
2876 		if (!(wpi_prph_read(sc, WPI_BSM_WR_CTRL) &
2877 		    WPI_BSM_WR_CTRL_START))
2878 			break;
2879 		DELAY(10);
2880 	}
2881 	if (ntries == 1000) {
2882 		printf("%s: could not load boot firmware\n",
2883 		    sc->sc_dev.dv_xname);
2884 		wpi_nic_unlock(sc);
2885 		return ETIMEDOUT;
2886 	}
2887 
2888 	/* Enable boot after power up. */
2889 	wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START_EN);
2890 
2891 	wpi_nic_unlock(sc);
2892 	return 0;
2893 }
2894 
2895 int
2896 wpi_load_firmware(struct wpi_softc *sc)
2897 {
2898 	struct wpi_fw_info *fw = &sc->fw;
2899 	struct wpi_dma_info *dma = &sc->fw_dma;
2900 	int error;
2901 
2902 	/* Copy initialization sections into pre-allocated DMA-safe memory. */
2903 	memcpy(dma->vaddr, fw->init.data, fw->init.datasz);
2904 	bus_dmamap_sync(sc->sc_dmat, dma->map, 0, fw->init.datasz,
2905 	    BUS_DMASYNC_PREWRITE);
2906 	memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ,
2907 	    fw->init.text, fw->init.textsz);
2908 	bus_dmamap_sync(sc->sc_dmat, dma->map, WPI_FW_DATA_MAXSZ,
2909 	    fw->init.textsz, BUS_DMASYNC_PREWRITE);
2910 
2911 	/* Tell adapter where to find initialization sections. */
2912 	if ((error = wpi_nic_lock(sc)) != 0)
2913 		return error;
2914 	wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
2915 	wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->init.datasz);
2916 	wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
2917 	    dma->paddr + WPI_FW_DATA_MAXSZ);
2918 	wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, fw->init.textsz);
2919 	wpi_nic_unlock(sc);
2920 
2921 	/* Load firmware boot code. */
2922 	error = wpi_load_bootcode(sc, fw->boot.text, fw->boot.textsz);
2923 	if (error != 0) {
2924 		printf("%s: could not load boot firmware\n",
2925 		    sc->sc_dev.dv_xname);
2926 		return error;
2927 	}
2928 	/* Now press "execute". */
2929 	WPI_WRITE(sc, WPI_RESET, 0);
2930 
2931 	/* Wait at most one second for first alive notification. */
2932 	if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
2933 		printf("%s: timeout waiting for adapter to initialize\n",
2934 		    sc->sc_dev.dv_xname);
2935 		return error;
2936 	}
2937 
2938 	/* Copy runtime sections into pre-allocated DMA-safe memory. */
2939 	memcpy(dma->vaddr, fw->main.data, fw->main.datasz);
2940 	bus_dmamap_sync(sc->sc_dmat, dma->map, 0, fw->main.datasz,
2941 	    BUS_DMASYNC_PREWRITE);
2942 	memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ,
2943 	    fw->main.text, fw->main.textsz);
2944 	bus_dmamap_sync(sc->sc_dmat, dma->map, WPI_FW_DATA_MAXSZ,
2945 	    fw->main.textsz, BUS_DMASYNC_PREWRITE);
2946 
2947 	/* Tell adapter where to find runtime sections. */
2948 	if ((error = wpi_nic_lock(sc)) != 0)
2949 		return error;
2950 	wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
2951 	wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->main.datasz);
2952 	wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
2953 	    dma->paddr + WPI_FW_DATA_MAXSZ);
2954 	wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE,
2955 	    WPI_FW_UPDATED | fw->main.textsz);
2956 	wpi_nic_unlock(sc);
2957 
2958 	return 0;
2959 }
2960 
2961 int
2962 wpi_read_firmware(struct wpi_softc *sc)
2963 {
2964 	struct wpi_fw_info *fw = &sc->fw;
2965 	const struct wpi_firmware_hdr *hdr;
2966 	size_t size;
2967 	int error;
2968 
2969 	/* Read firmware image from filesystem. */
2970 	if ((error = loadfirmware("wpi-3945abg", &fw->data, &size)) != 0) {
2971 		printf("%s: error, %d, could not read firmware %s\n",
2972 		    sc->sc_dev.dv_xname, error, "wpi-3945abg");
2973 		return error;
2974 	}
2975 	if (size < sizeof (*hdr)) {
2976 		printf("%s: truncated firmware header: %zu bytes\n",
2977 		    sc->sc_dev.dv_xname, size);
2978 		free(fw->data, M_DEVBUF, size);
2979 		return EINVAL;
2980 	}
2981 	/* Extract firmware header information. */
2982 	hdr = (struct wpi_firmware_hdr *)fw->data;
2983 	fw->main.textsz = letoh32(hdr->main_textsz);
2984 	fw->main.datasz = letoh32(hdr->main_datasz);
2985 	fw->init.textsz = letoh32(hdr->init_textsz);
2986 	fw->init.datasz = letoh32(hdr->init_datasz);
2987 	fw->boot.textsz = letoh32(hdr->boot_textsz);
2988 	fw->boot.datasz = 0;
2989 
2990 	/* Sanity-check firmware header. */
2991 	if (fw->main.textsz > WPI_FW_TEXT_MAXSZ ||
2992 	    fw->main.datasz > WPI_FW_DATA_MAXSZ ||
2993 	    fw->init.textsz > WPI_FW_TEXT_MAXSZ ||
2994 	    fw->init.datasz > WPI_FW_DATA_MAXSZ ||
2995 	    fw->boot.textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
2996 	    (fw->boot.textsz & 3) != 0) {
2997 		printf("%s: invalid firmware header\n", sc->sc_dev.dv_xname);
2998 		free(fw->data, M_DEVBUF, size);
2999 		return EINVAL;
3000 	}
3001 
3002 	/* Check that all firmware sections fit. */
3003 	if (size < sizeof (*hdr) + fw->main.textsz + fw->main.datasz +
3004 	    fw->init.textsz + fw->init.datasz + fw->boot.textsz) {
3005 		printf("%s: firmware file too short: %zu bytes\n",
3006 		    sc->sc_dev.dv_xname, size);
3007 		free(fw->data, M_DEVBUF, size);
3008 		return EINVAL;
3009 	}
3010 
3011 	/* Get pointers to firmware sections. */
3012 	fw->main.text = (const uint8_t *)(hdr + 1);
3013 	fw->main.data = fw->main.text + fw->main.textsz;
3014 	fw->init.text = fw->main.data + fw->main.datasz;
3015 	fw->init.data = fw->init.text + fw->init.textsz;
3016 	fw->boot.text = fw->init.data + fw->init.datasz;
3017 
3018 	return 0;
3019 }
3020 
3021 int
3022 wpi_clock_wait(struct wpi_softc *sc)
3023 {
3024 	int ntries;
3025 
3026 	/* Set "initialization complete" bit. */
3027 	WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
3028 
3029 	/* Wait for clock stabilization. */
3030 	for (ntries = 0; ntries < 25000; ntries++) {
3031 		if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_MAC_CLOCK_READY)
3032 			return 0;
3033 		DELAY(100);
3034 	}
3035 	printf("%s: timeout waiting for clock stabilization\n",
3036 	    sc->sc_dev.dv_xname);
3037 	return ETIMEDOUT;
3038 }
3039 
3040 int
3041 wpi_apm_init(struct wpi_softc *sc)
3042 {
3043 	int error;
3044 
3045 	WPI_SETBITS(sc, WPI_ANA_PLL, WPI_ANA_PLL_INIT);
3046 	/* Disable L0s. */
3047 	WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_L1A_NO_L0S_RX);
3048 
3049 	if ((error = wpi_clock_wait(sc)) != 0)
3050 		return error;
3051 
3052 	if ((error = wpi_nic_lock(sc)) != 0)
3053 		return error;
3054 	/* Enable DMA. */
3055 	wpi_prph_write(sc, WPI_APMG_CLK_ENA,
3056 	    WPI_APMG_CLK_DMA_CLK_RQT | WPI_APMG_CLK_BSM_CLK_RQT);
3057 	DELAY(20);
3058 	/* Disable L1. */
3059 	wpi_prph_setbits(sc, WPI_APMG_PCI_STT, WPI_APMG_PCI_STT_L1A_DIS);
3060 	wpi_nic_unlock(sc);
3061 
3062 	return 0;
3063 }
3064 
3065 void
3066 wpi_apm_stop_master(struct wpi_softc *sc)
3067 {
3068 	int ntries;
3069 
3070 	WPI_SETBITS(sc, WPI_RESET, WPI_RESET_STOP_MASTER);
3071 
3072 	if ((WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_PS_MASK) ==
3073 	    WPI_GP_CNTRL_MAC_PS)
3074 		return;	/* Already asleep. */
3075 
3076 	for (ntries = 0; ntries < 100; ntries++) {
3077 		if (WPI_READ(sc, WPI_RESET) & WPI_RESET_MASTER_DISABLED)
3078 			return;
3079 		DELAY(10);
3080 	}
3081 	printf("%s: timeout waiting for master\n", sc->sc_dev.dv_xname);
3082 }
3083 
3084 void
3085 wpi_apm_stop(struct wpi_softc *sc)
3086 {
3087 	wpi_apm_stop_master(sc);
3088 	WPI_SETBITS(sc, WPI_RESET, WPI_RESET_SW);
3089 }
3090 
3091 void
3092 wpi_nic_config(struct wpi_softc *sc)
3093 {
3094 	pcireg_t reg;
3095 	uint8_t rev;
3096 
3097 	/* Voodoo from the reference driver. */
3098 	reg = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG);
3099 	rev = PCI_REVISION(reg);
3100 	if ((rev & 0xc0) == 0x40)
3101 		WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MB);
3102 	else if (!(rev & 0x80))
3103 		WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MM);
3104 
3105 	if (sc->cap == 0x80)
3106 		WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_SKU_MRC);
3107 
3108 	if ((letoh16(sc->rev) & 0xf0) == 0xd0)
3109 		WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
3110 	else
3111 		WPI_CLRBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
3112 
3113 	if (sc->type > 1)
3114 		WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_TYPE_B);
3115 }
3116 
3117 int
3118 wpi_hw_init(struct wpi_softc *sc)
3119 {
3120 	int chnl, ntries, error;
3121 
3122 	/* Clear pending interrupts. */
3123 	WPI_WRITE(sc, WPI_INT, 0xffffffff);
3124 
3125 	if ((error = wpi_apm_init(sc)) != 0) {
3126 		printf("%s: could not power ON adapter\n",
3127 		    sc->sc_dev.dv_xname);
3128 		return error;
3129 	}
3130 
3131 	/* Select VMAIN power source. */
3132 	if ((error = wpi_nic_lock(sc)) != 0)
3133 		return error;
3134 	wpi_prph_clrbits(sc, WPI_APMG_PS, WPI_APMG_PS_PWR_SRC_MASK);
3135 	wpi_nic_unlock(sc);
3136 	/* Spin until VMAIN gets selected. */
3137 	for (ntries = 0; ntries < 5000; ntries++) {
3138 		if (WPI_READ(sc, WPI_GPIO_IN) & WPI_GPIO_IN_VMAIN)
3139 			break;
3140 		DELAY(10);
3141 	}
3142 	if (ntries == 5000) {
3143 		printf("%s: timeout selecting power source\n",
3144 		    sc->sc_dev.dv_xname);
3145 		return ETIMEDOUT;
3146 	}
3147 
3148 	/* Perform adapter initialization. */
3149 	(void)wpi_nic_config(sc);
3150 
3151 	/* Initialize RX ring. */
3152 	if ((error = wpi_nic_lock(sc)) != 0)
3153 		return error;
3154 	/* Set physical address of RX ring. */
3155 	WPI_WRITE(sc, WPI_FH_RX_BASE, sc->rxq.desc_dma.paddr);
3156 	/* Set physical address of RX read pointer. */
3157 	WPI_WRITE(sc, WPI_FH_RX_RPTR_ADDR, sc->shared_dma.paddr +
3158 	    offsetof(struct wpi_shared, next));
3159 	WPI_WRITE(sc, WPI_FH_RX_WPTR, 0);
3160 	/* Enable RX. */
3161 	WPI_WRITE(sc, WPI_FH_RX_CONFIG,
3162 	    WPI_FH_RX_CONFIG_DMA_ENA |
3163 	    WPI_FH_RX_CONFIG_RDRBD_ENA |
3164 	    WPI_FH_RX_CONFIG_WRSTATUS_ENA |
3165 	    WPI_FH_RX_CONFIG_MAXFRAG |
3166 	    WPI_FH_RX_CONFIG_NRBD(WPI_RX_RING_COUNT_LOG) |
3167 	    WPI_FH_RX_CONFIG_IRQ_DST_HOST |
3168 	    WPI_FH_RX_CONFIG_IRQ_RBTH(1));
3169 	(void)WPI_READ(sc, WPI_FH_RSSR_TBL);	/* barrier */
3170 	WPI_WRITE(sc, WPI_FH_RX_WPTR, (WPI_RX_RING_COUNT - 1) & ~7);
3171 	wpi_nic_unlock(sc);
3172 
3173 	/* Initialize TX rings. */
3174 	if ((error = wpi_nic_lock(sc)) != 0)
3175 		return error;
3176 	wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 2);	/* bypass mode */
3177 	wpi_prph_write(sc, WPI_ALM_SCHED_ARASTAT, 1);	/* enable RA0 */
3178 	/* Enable all 6 TX rings. */
3179 	wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0x3f);
3180 	wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE1, 0x10000);
3181 	wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE2, 0x30002);
3182 	wpi_prph_write(sc, WPI_ALM_SCHED_TXF4MF, 4);
3183 	wpi_prph_write(sc, WPI_ALM_SCHED_TXF5MF, 5);
3184 	/* Set physical address of TX rings. */
3185 	WPI_WRITE(sc, WPI_FH_TX_BASE, sc->shared_dma.paddr);
3186 	WPI_WRITE(sc, WPI_FH_MSG_CONFIG, 0xffff05a5);
3187 
3188 	/* Enable all DMA channels. */
3189 	for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
3190 		WPI_WRITE(sc, WPI_FH_CBBC_CTRL(chnl), 0);
3191 		WPI_WRITE(sc, WPI_FH_CBBC_BASE(chnl), 0);
3192 		WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0x80200008);
3193 	}
3194 	wpi_nic_unlock(sc);
3195 	(void)WPI_READ(sc, WPI_FH_TX_BASE);	/* barrier */
3196 
3197 	/* Clear "radio off" and "commands blocked" bits. */
3198 	WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
3199 	WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_CMD_BLOCKED);
3200 
3201 	/* Clear pending interrupts. */
3202 	WPI_WRITE(sc, WPI_INT, 0xffffffff);
3203 	/* Enable interrupts. */
3204 	WPI_WRITE(sc, WPI_MASK, WPI_INT_MASK);
3205 
3206 	/* _Really_ make sure "radio off" bit is cleared! */
3207 	WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
3208 	WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
3209 
3210 	if ((error = wpi_load_firmware(sc)) != 0) {
3211 		printf("%s: could not load firmware\n", sc->sc_dev.dv_xname);
3212 		return error;
3213 	}
3214 	/* Wait at most one second for firmware alive notification. */
3215 	if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
3216 		printf("%s: timeout waiting for adapter to initialize\n",
3217 		    sc->sc_dev.dv_xname);
3218 		return error;
3219 	}
3220 	/* Do post-firmware initialization. */
3221 	return wpi_post_alive(sc);
3222 }
3223 
3224 void
3225 wpi_hw_stop(struct wpi_softc *sc)
3226 {
3227 	int chnl, qid, ntries;
3228 	uint32_t tmp;
3229 
3230 	WPI_WRITE(sc, WPI_RESET, WPI_RESET_NEVO);
3231 
3232 	/* Disable interrupts. */
3233 	WPI_WRITE(sc, WPI_MASK, 0);
3234 	WPI_WRITE(sc, WPI_INT, 0xffffffff);
3235 	WPI_WRITE(sc, WPI_FH_INT, 0xffffffff);
3236 
3237 	/* Make sure we no longer hold the NIC lock. */
3238 	wpi_nic_unlock(sc);
3239 
3240 	if (wpi_nic_lock(sc) == 0) {
3241 		/* Stop TX scheduler. */
3242 		wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 0);
3243 		wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0);
3244 
3245 		/* Stop all DMA channels. */
3246 		for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
3247 			WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0);
3248 			for (ntries = 0; ntries < 100; ntries++) {
3249 				tmp = WPI_READ(sc, WPI_FH_TX_STATUS);
3250 				if ((tmp & WPI_FH_TX_STATUS_IDLE(chnl)) ==
3251 				    WPI_FH_TX_STATUS_IDLE(chnl))
3252 					break;
3253 				DELAY(10);
3254 			}
3255 		}
3256 		wpi_nic_unlock(sc);
3257 	}
3258 
3259 	/* Stop RX ring. */
3260 	wpi_reset_rx_ring(sc, &sc->rxq);
3261 
3262 	/* Reset all TX rings. */
3263 	for (qid = 0; qid < WPI_NTXQUEUES; qid++)
3264 		wpi_reset_tx_ring(sc, &sc->txq[qid]);
3265 
3266 	if (wpi_nic_lock(sc) == 0) {
3267 		wpi_prph_write(sc, WPI_APMG_CLK_DIS, WPI_APMG_CLK_DMA_CLK_RQT);
3268 		wpi_nic_unlock(sc);
3269 	}
3270 	DELAY(5);
3271 	/* Power OFF adapter. */
3272 	wpi_apm_stop(sc);
3273 }
3274 
3275 int
3276 wpi_init(struct ifnet *ifp)
3277 {
3278 	struct wpi_softc *sc = ifp->if_softc;
3279 	struct ieee80211com *ic = &sc->sc_ic;
3280 	int error;
3281 
3282 #ifdef notyet
3283 	/* Check that the radio is not disabled by hardware switch. */
3284 	if (!(WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL)) {
3285 		printf("%s: radio is disabled by hardware switch\n",
3286 		    sc->sc_dev.dv_xname);
3287 		error = EPERM;	/* :-) */
3288 		goto fail;
3289 	}
3290 #endif
3291 	/* Read firmware images from the filesystem. */
3292 	if ((error = wpi_read_firmware(sc)) != 0) {
3293 		printf("%s: could not read firmware\n", sc->sc_dev.dv_xname);
3294 		goto fail;
3295 	}
3296 
3297 	/* Initialize hardware and upload firmware. */
3298 	error = wpi_hw_init(sc);
3299 	free(sc->fw.data, M_DEVBUF, 0);
3300 	if (error != 0) {
3301 		printf("%s: could not initialize hardware\n",
3302 		    sc->sc_dev.dv_xname);
3303 		goto fail;
3304 	}
3305 
3306 	/* Configure adapter now that it is ready. */
3307 	if ((error = wpi_config(sc)) != 0) {
3308 		printf("%s: could not configure device\n",
3309 		    sc->sc_dev.dv_xname);
3310 		goto fail;
3311 	}
3312 
3313 	ifq_clr_oactive(&ifp->if_snd);
3314 	ifp->if_flags |= IFF_RUNNING;
3315 
3316 	if (ic->ic_opmode != IEEE80211_M_MONITOR)
3317 		ieee80211_begin_scan(ifp);
3318 	else
3319 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
3320 
3321 	return 0;
3322 
3323 fail:	wpi_stop(ifp, 1);
3324 	return error;
3325 }
3326 
3327 void
3328 wpi_stop(struct ifnet *ifp, int disable)
3329 {
3330 	struct wpi_softc *sc = ifp->if_softc;
3331 	struct ieee80211com *ic = &sc->sc_ic;
3332 
3333 	ifp->if_timer = sc->sc_tx_timer = 0;
3334 	ifp->if_flags &= ~IFF_RUNNING;
3335 	ifq_clr_oactive(&ifp->if_snd);
3336 
3337 	/* In case we were scanning, release the scan "lock". */
3338 	ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED;
3339 
3340 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
3341 
3342 	/* Power OFF hardware. */
3343 	wpi_hw_stop(sc);
3344 }
3345