xref: /netbsd-src/sys/dev/pci/if_wpi.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*  $NetBSD: if_wpi.c,v 1.27 2007/11/07 00:23:19 ad Exp $    */
2 
3 /*-
4  * Copyright (c) 2006, 2007
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 #include <sys/cdefs.h>
21 __KERNEL_RCSID(0, "$NetBSD: if_wpi.c,v 1.27 2007/11/07 00:23:19 ad Exp $");
22 
23 /*
24  * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
25  */
26 
27 #include "bpfilter.h"
28 
29 #include <sys/param.h>
30 #include <sys/sockio.h>
31 #include <sys/sysctl.h>
32 #include <sys/mbuf.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/conf.h>
38 #include <sys/kauth.h>
39 #include <sys/callout.h>
40 
41 #include <sys/bus.h>
42 #include <machine/endian.h>
43 #include <sys/intr.h>
44 
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcidevs.h>
48 
49 #if NBPFILTER > 0
50 #include <net/bpf.h>
51 #endif
52 #include <net/if.h>
53 #include <net/if_arp.h>
54 #include <net/if_dl.h>
55 #include <net/if_ether.h>
56 #include <net/if_media.h>
57 #include <net/if_types.h>
58 
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_amrr.h>
61 #include <net80211/ieee80211_radiotap.h>
62 
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 
68 #include <dev/firmload.h>
69 
70 #include <dev/pci/if_wpireg.h>
71 #include <dev/pci/if_wpivar.h>
72 
73 #ifdef WPI_DEBUG
74 #define DPRINTF(x)	if (wpi_debug > 0) printf x
75 #define DPRINTFN(n, x)	if (wpi_debug >= (n)) printf x
76 int wpi_debug = 1;
77 #else
78 #define DPRINTF(x)
79 #define DPRINTFN(n, x)
80 #endif
81 
82 /*
83  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
84  */
85 static const struct ieee80211_rateset wpi_rateset_11a =
86 	{ 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
87 
88 static const struct ieee80211_rateset wpi_rateset_11b =
89 	{ 4, { 2, 4, 11, 22 } };
90 
91 static const struct ieee80211_rateset wpi_rateset_11g =
92 	{ 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
93 
94 static int  wpi_match(struct device *, struct cfdata *, void *);
95 static void wpi_attach(struct device *, struct device *, void *);
96 static int  wpi_detach(struct device*, int);
97 static void wpi_power(int, void *);
98 static int  wpi_dma_contig_alloc(bus_dma_tag_t, struct wpi_dma_info *,
99 	void **, bus_size_t, bus_size_t, int);
100 static void wpi_dma_contig_free(struct wpi_dma_info *);
101 static int  wpi_alloc_shared(struct wpi_softc *);
102 static void wpi_free_shared(struct wpi_softc *);
103 static int  wpi_alloc_fwmem(struct wpi_softc *);
104 static void wpi_free_fwmem(struct wpi_softc *);
105 static struct wpi_rbuf *wpi_alloc_rbuf(struct wpi_softc *);
106 static void wpi_free_rbuf(struct mbuf *, void *, size_t, void *);
107 static int  wpi_alloc_rpool(struct wpi_softc *);
108 static void wpi_free_rpool(struct wpi_softc *);
109 static int  wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
110 static void wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
111 static void wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
112 static int  wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *, int,
113 	int);
114 static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
115 static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
116 static struct ieee80211_node * wpi_node_alloc(struct ieee80211_node_table *);
117 static void wpi_newassoc(struct ieee80211_node *, int);
118 static int  wpi_media_change(struct ifnet *);
119 static int  wpi_newstate(struct ieee80211com *, enum ieee80211_state, int);
120 static void	wpi_fix_channel(struct ieee80211com *, struct mbuf *);
121 static void wpi_mem_lock(struct wpi_softc *);
122 static void wpi_mem_unlock(struct wpi_softc *);
123 static uint32_t wpi_mem_read(struct wpi_softc *, uint16_t);
124 static void wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t);
125 static void wpi_mem_write_region_4(struct wpi_softc *, uint16_t,
126 								   const uint32_t *, int);
127 static int  wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
128 static int  wpi_load_microcode(struct wpi_softc *,  const uint8_t *, int);
129 static int  wpi_load_firmware(struct wpi_softc *);
130 static void wpi_calib_timeout(void *);
131 static void wpi_iter_func(void *, struct ieee80211_node *);
132 static void wpi_power_calibration(struct wpi_softc *, int);
133 static void wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *,
134 	struct wpi_rx_data *);
135 static void wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *);
136 static void wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *);
137 static void wpi_notif_intr(struct wpi_softc *);
138 static int  wpi_intr(void *);
139 static void wpi_read_eeprom(struct wpi_softc *);
140 static void wpi_read_eeprom_channels(struct wpi_softc *, int);
141 static void wpi_read_eeprom_group(struct wpi_softc *, int);
142 static uint8_t wpi_plcp_signal(int);
143 static int  wpi_tx_data(struct wpi_softc *, struct mbuf *,
144 	struct ieee80211_node *, int);
145 static void wpi_start(struct ifnet *);
146 static void wpi_watchdog(struct ifnet *);
147 static int  wpi_ioctl(struct ifnet *, u_long, void *);
148 static int  wpi_cmd(struct wpi_softc *, int, const void *, int, int);
149 static int  wpi_wme_update(struct ieee80211com *);
150 static int  wpi_mrr_setup(struct wpi_softc *);
151 static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
152 static void wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *);
153 static int  wpi_set_txpower(struct wpi_softc *,
154 			    struct ieee80211_channel *, int);
155 static int  wpi_get_power_index(struct wpi_softc *,
156 		struct wpi_power_group *, struct ieee80211_channel *, int);
157 static int  wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
158 static int  wpi_auth(struct wpi_softc *);
159 static int  wpi_scan(struct wpi_softc *, uint16_t);
160 static int  wpi_config(struct wpi_softc *);
161 static void wpi_stop_master(struct wpi_softc *);
162 static int  wpi_power_up(struct wpi_softc *);
163 static int  wpi_reset(struct wpi_softc *);
164 static void wpi_hw_config(struct wpi_softc *);
165 static int  wpi_init(struct ifnet *);
166 static void wpi_stop(struct ifnet *, int);
167 
168 CFATTACH_DECL(wpi, sizeof (struct wpi_softc), wpi_match, wpi_attach,
169 	wpi_detach, NULL);
170 
171 static int
172 wpi_match(struct device *parent, struct cfdata *match __unused, void *aux)
173 {
174 	struct pci_attach_args *pa = aux;
175 
176 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
177 		return 0;
178 
179 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_1 ||
180 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_2)
181 		return 1;
182 
183 	return 0;
184 }
185 
186 /* Base Address Register */
187 #define WPI_PCI_BAR0	0x10
188 
189 static void
190 wpi_attach(struct device *parent __unused, struct device *self, void *aux)
191 {
192 	struct wpi_softc *sc = (struct wpi_softc *)self;
193 	struct ieee80211com *ic = &sc->sc_ic;
194 	struct ifnet *ifp = &sc->sc_ec.ec_if;
195 	struct pci_attach_args *pa = aux;
196 	const char *intrstr;
197 	char devinfo[256];
198 	bus_space_tag_t memt;
199 	bus_space_handle_t memh;
200 	pci_intr_handle_t ih;
201 	pcireg_t data;
202 	int error, ac, revision;
203 
204 	sc->sc_pct = pa->pa_pc;
205 	sc->sc_pcitag = pa->pa_tag;
206 
207 	callout_init(&sc->calib_to, 0);
208 
209 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
210 	revision = PCI_REVISION(pa->pa_class);
211 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
212 
213 	/* clear device specific PCI configuration register 0x41 */
214 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
215 	data &= ~0x0000ff00;
216 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
217 
218 	/* enable bus-mastering */
219 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
220 	data |= PCI_COMMAND_MASTER_ENABLE;
221 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
222 
223 	/* map the register window */
224 	error = pci_mapreg_map(pa, WPI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
225 		PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz);
226 	if (error != 0) {
227 		aprint_error("%s: could not map memory space\n",
228 			sc->sc_dev.dv_xname);
229 		return;
230 	}
231 
232 	sc->sc_st = memt;
233 	sc->sc_sh = memh;
234 	sc->sc_dmat = pa->pa_dmat;
235 
236 	if (pci_intr_map(pa, &ih) != 0) {
237 		aprint_error("%s: could not map interrupt\n",
238 			sc->sc_dev.dv_xname);
239 		return;
240 	}
241 
242 	intrstr = pci_intr_string(sc->sc_pct, ih);
243 	sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, wpi_intr, sc);
244 	if (sc->sc_ih == NULL) {
245 		aprint_error("%s: could not establish interrupt",
246 			sc->sc_dev.dv_xname);
247 		if (intrstr != NULL)
248 			aprint_error(" at %s", intrstr);
249 		aprint_error("\n");
250 		return;
251 	}
252 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
253 
254 	if (wpi_reset(sc) != 0) {
255 		aprint_error("%s: could not reset adapter\n",
256 			sc->sc_dev.dv_xname);
257 		return;
258 	}
259 
260  	/*
261 	 * Allocate DMA memory for firmware transfers.
262 	 */
263 	if ((error = wpi_alloc_fwmem(sc)) != 0) {
264 		aprint_error(": could not allocate firmware memory\n");
265 		return;
266 	}
267 
268 	/*
269 	 * Allocate shared page and Tx/Rx rings.
270 	 */
271 	if ((error = wpi_alloc_shared(sc)) != 0) {
272 		aprint_error("%s: could not allocate shared area\n",
273 			sc->sc_dev.dv_xname);
274 		goto fail1;
275 	}
276 
277 	if ((error = wpi_alloc_rpool(sc)) != 0) {
278 		aprint_error("%s: could not allocate Rx buffers\n",
279 			sc->sc_dev.dv_xname);
280 		goto fail2;
281 	}
282 
283 	for (ac = 0; ac < 4; ac++) {
284 		error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, ac);
285 		if (error != 0) {
286 			aprint_error("%s: could not allocate Tx ring %d\n",
287 					sc->sc_dev.dv_xname, ac);
288 			goto fail3;
289 		}
290 	}
291 
292 	error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4);
293 	if (error != 0) {
294 		aprint_error("%s: could not allocate command ring\n",
295 			sc->sc_dev.dv_xname);
296 		goto fail3;
297 	}
298 
299 	if (wpi_alloc_rx_ring(sc, &sc->rxq) != 0) {
300 		aprint_error("%s: could not allocate Rx ring\n",
301 			sc->sc_dev.dv_xname);
302 		goto fail4;
303 	}
304 
305 	ic->ic_ifp = ifp;
306 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
307 	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
308 	ic->ic_state = IEEE80211_S_INIT;
309 
310 	/* set device capabilities */
311 	ic->ic_caps =
312 		IEEE80211_C_IBSS |       /* IBSS mode support */
313 		IEEE80211_C_WPA |        /* 802.11i */
314 		IEEE80211_C_MONITOR |    /* monitor mode supported */
315 		IEEE80211_C_TXPMGT |     /* tx power management */
316 		IEEE80211_C_SHSLOT |     /* short slot time supported */
317 		IEEE80211_C_SHPREAMBLE | /* short preamble supported */
318 		IEEE80211_C_WME;         /* 802.11e */
319 
320 	/* read supported channels and MAC address from EEPROM */
321 	wpi_read_eeprom(sc);
322 
323 	/* set supported .11a, .11b, .11g rates */
324 	ic->ic_sup_rates[IEEE80211_MODE_11A] = wpi_rateset_11a;
325 	ic->ic_sup_rates[IEEE80211_MODE_11B] = wpi_rateset_11b;
326 	ic->ic_sup_rates[IEEE80211_MODE_11G] = wpi_rateset_11g;
327 
328 	ic->ic_ibss_chan = &ic->ic_channels[0];
329 
330 	ifp->if_softc = sc;
331 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
332 	ifp->if_init = wpi_init;
333 	ifp->if_stop = wpi_stop;
334 	ifp->if_ioctl = wpi_ioctl;
335 	ifp->if_start = wpi_start;
336 	ifp->if_watchdog = wpi_watchdog;
337 	IFQ_SET_READY(&ifp->if_snd);
338 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
339 
340 	if_attach(ifp);
341 	ieee80211_ifattach(ic);
342 	/* override default methods */
343 	ic->ic_node_alloc = wpi_node_alloc;
344 	ic->ic_newassoc = wpi_newassoc;
345 	ic->ic_wme.wme_update = wpi_wme_update;
346 
347 	/* override state transition machine */
348 	sc->sc_newstate = ic->ic_newstate;
349 	ic->ic_newstate = wpi_newstate;
350 	ieee80211_media_init(ic, wpi_media_change, ieee80211_media_status);
351 
352 	sc->amrr.amrr_min_success_threshold = 1;
353 	sc->amrr.amrr_max_success_threshold = 15;
354 
355 	/* set powerhook */
356 	sc->powerhook = powerhook_establish(sc->sc_dev.dv_xname, wpi_power, sc);
357 
358 #if NBPFILTER > 0
359 	bpfattach2(ifp, DLT_IEEE802_11_RADIO,
360 		sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
361 		&sc->sc_drvbpf);
362 
363 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
364 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
365 	sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT);
366 
367 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
368 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
369 	sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT);
370 #endif
371 
372 	ieee80211_announce(ic);
373 
374 	return;
375 
376 fail4:  wpi_free_tx_ring(sc, &sc->cmdq);
377 fail3:  while (--ac >= 0)
378 			wpi_free_tx_ring(sc, &sc->txq[ac]);
379 	wpi_free_rpool(sc);
380 fail2:	wpi_free_shared(sc);
381 fail1:	wpi_free_fwmem(sc);
382 }
383 
384 static int
385 wpi_detach(struct device* self, int flags __unused)
386 {
387 	struct wpi_softc *sc = (struct wpi_softc *)self;
388 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
389 	int ac;
390 
391 	wpi_stop(ifp, 1);
392 
393 #if NBPFILTER > 0
394 	if (ifp != NULL)
395 		bpfdetach(ifp);
396 #endif
397 	ieee80211_ifdetach(&sc->sc_ic);
398 	if (ifp != NULL)
399 		if_detach(ifp);
400 
401 	for (ac = 0; ac < 4; ac++)
402 		wpi_free_tx_ring(sc, &sc->txq[ac]);
403 	wpi_free_tx_ring(sc, &sc->cmdq);
404 	wpi_free_rx_ring(sc, &sc->rxq);
405 	wpi_free_rpool(sc);
406 	wpi_free_shared(sc);
407 
408 	if (sc->sc_ih != NULL) {
409 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
410 		sc->sc_ih = NULL;
411 	}
412 
413 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
414 
415 	return 0;
416 }
417 
418 static void
419 wpi_power(int why, void *arg)
420 {
421 	struct wpi_softc *sc = arg;
422 	struct ifnet *ifp;
423 	pcireg_t data;
424 	int s;
425 
426 	if (why != PWR_RESUME)
427 		return;
428 
429 	/* clear device specific PCI configuration register 0x41 */
430 	data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
431 	data &= ~0x0000ff00;
432 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
433 
434 	s = splnet();
435 	ifp = sc->sc_ic.ic_ifp;
436 	if (ifp->if_flags & IFF_UP) {
437 		ifp->if_init(ifp);
438 		if (ifp->if_flags & IFF_RUNNING)
439 			ifp->if_start(ifp);
440 	}
441 	splx(s);
442 }
443 
444 static int
445 wpi_dma_contig_alloc(bus_dma_tag_t tag, struct wpi_dma_info *dma,
446 	void **kvap, bus_size_t size, bus_size_t alignment, int flags)
447 {
448 	int nsegs, error;
449 
450 	dma->tag = tag;
451 	dma->size = size;
452 
453 	error = bus_dmamap_create(tag, size, 1, size, 0, flags, &dma->map);
454 	if (error != 0)
455 		goto fail;
456 
457 	error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs,
458 	    flags);
459 	if (error != 0)
460 		goto fail;
461 
462 	error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, flags);
463 	if (error != 0)
464 		goto fail;
465 
466 	error = bus_dmamap_load(tag, dma->map, dma->vaddr, size, NULL, flags);
467 	if (error != 0)
468 		goto fail;
469 
470 	memset(dma->vaddr, 0, size);
471 
472 	dma->paddr = dma->map->dm_segs[0].ds_addr;
473 	if (kvap != NULL)
474 		*kvap = dma->vaddr;
475 
476 	return 0;
477 
478 fail:   wpi_dma_contig_free(dma);
479 	return error;
480 }
481 
482 static void
483 wpi_dma_contig_free(struct wpi_dma_info *dma)
484 {
485 	if (dma->map != NULL) {
486 		if (dma->vaddr != NULL) {
487 			bus_dmamap_unload(dma->tag, dma->map);
488 			bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size);
489 			bus_dmamem_free(dma->tag, &dma->seg, 1);
490 			dma->vaddr = NULL;
491 		}
492 		bus_dmamap_destroy(dma->tag, dma->map);
493 		dma->map = NULL;
494 	}
495 }
496 
497 /*
498  * Allocate a shared page between host and NIC.
499  */
500 static int
501 wpi_alloc_shared(struct wpi_softc *sc)
502 {
503 	int error;
504 	/* must be aligned on a 4K-page boundary */
505 	error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->shared_dma,
506 			(void **)&sc->shared, sizeof (struct wpi_shared),
507 			WPI_BUF_ALIGN,BUS_DMA_NOWAIT);
508 	if (error != 0)
509 		aprint_error(
510 			"%s: could not allocate shared area DMA memory\n",
511 			sc->sc_dev.dv_xname);
512 
513 	return error;
514 }
515 
516 static void
517 wpi_free_shared(struct wpi_softc *sc)
518 {
519 	wpi_dma_contig_free(&sc->shared_dma);
520 }
521 
522 /*
523  * Allocate DMA-safe memory for firmware transfer.
524  */
525 static int
526 wpi_alloc_fwmem(struct wpi_softc *sc)
527 {
528 	int error;
529 	/* allocate enough contiguous space to store text and data */
530 	error = wpi_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL,
531 	    WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 0,
532 	    BUS_DMA_NOWAIT);
533 
534 	if (error != 0)
535 		aprint_error(
536 			"%s: could not allocate firmware transfer area"
537 			"DMA memory\n", sc->sc_dev.dv_xname);
538 	return error;
539 }
540 
541 static void
542 wpi_free_fwmem(struct wpi_softc *sc)
543 {
544 	wpi_dma_contig_free(&sc->fw_dma);
545 }
546 
547 
548 static struct wpi_rbuf *
549 wpi_alloc_rbuf(struct wpi_softc *sc)
550 {
551 	struct wpi_rbuf *rbuf;
552 
553 	rbuf = SLIST_FIRST(&sc->rxq.freelist);
554 	if (rbuf == NULL)
555 		return NULL;
556 	SLIST_REMOVE_HEAD(&sc->rxq.freelist, next);
557 	sc->rxq.nb_free_entries --;
558 
559 	return rbuf;
560 }
561 
562 /*
563  * This is called automatically by the network stack when the mbuf to which our
564  * Rx buffer is attached is freed.
565  */
566 static void
567 wpi_free_rbuf(struct mbuf* m, void *buf, size_t size, void *arg)
568 {
569 	struct wpi_rbuf *rbuf = arg;
570 	struct wpi_softc *sc = rbuf->sc;
571 
572 	/* put the buffer back in the free list */
573 
574 	SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next);
575 	sc->rxq.nb_free_entries ++;
576 
577 	if (__predict_true(m != NULL))
578 		pool_cache_put(mb_cache, m);
579 }
580 
581 static int
582 wpi_alloc_rpool(struct wpi_softc *sc)
583 {
584 	struct wpi_rx_ring *ring = &sc->rxq;
585 	struct wpi_rbuf *rbuf;
586 	int i, error;
587 
588 	/* allocate a big chunk of DMA'able memory.. */
589 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->buf_dma, NULL,
590 	    WPI_RBUF_COUNT * WPI_RBUF_SIZE, WPI_BUF_ALIGN, BUS_DMA_NOWAIT);
591 	if (error != 0) {
592 		aprint_normal("%s: could not allocate Rx buffers DMA memory\n",
593 		    sc->sc_dev.dv_xname);
594 	return error;
595 	}
596 
597 	/* ..and split it into 3KB chunks */
598 	SLIST_INIT(&ring->freelist);
599 	for (i = 0; i < WPI_RBUF_COUNT; i++) {
600 		rbuf = &ring->rbuf[i];
601 		rbuf->sc = sc;	/* backpointer for callbacks */
602 		rbuf->vaddr = (char *)ring->buf_dma.vaddr + i * WPI_RBUF_SIZE;
603 		rbuf->paddr = ring->buf_dma.paddr + i * WPI_RBUF_SIZE;
604 
605 		SLIST_INSERT_HEAD(&ring->freelist, rbuf, next);
606 	}
607 
608 	ring->nb_free_entries = WPI_RBUF_COUNT;
609 	return 0;
610 }
611 
612 static void
613 wpi_free_rpool(struct wpi_softc *sc)
614 {
615 	wpi_dma_contig_free(&sc->rxq.buf_dma);
616 }
617 
618 static int
619 wpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
620 {
621 	struct wpi_rx_data *data;
622 	struct wpi_rbuf *rbuf;
623 	int i, error;
624 
625 	ring->cur = 0;
626 
627 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
628 		(void **)&ring->desc,
629 		WPI_RX_RING_COUNT * sizeof (struct wpi_rx_desc),
630 		WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
631 	if (error != 0) {
632 		aprint_error("%s: could not allocate rx ring DMA memory\n",
633 			sc->sc_dev.dv_xname);
634 		goto fail;
635 	}
636 
637 	/*
638 	 * Setup Rx buffers.
639 	 */
640 	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
641 		data = &ring->data[i];
642 
643 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
644 		if (data->m == NULL) {
645 			aprint_error("%s: could not allocate rx mbuf\n",
646 				sc->sc_dev.dv_xname);
647 			error = ENOMEM;
648 			goto fail;
649 		}
650 		if ((rbuf = wpi_alloc_rbuf(sc)) == NULL) {
651 			m_freem(data->m);
652 			data->m = NULL;
653 			aprint_error("%s: could not allocate rx cluster\n",
654 				sc->sc_dev.dv_xname);
655 			error = ENOMEM;
656 			goto fail;
657 		}
658 		/* attach Rx buffer to mbuf */
659 		MEXTADD(data->m, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf,
660 		    rbuf);
661 		data->m->m_flags |= M_EXT_RW;
662 
663 		ring->desc[i] = htole32(rbuf->paddr);
664 	}
665 
666 	return 0;
667 
668 fail:	wpi_free_rx_ring(sc, ring);
669 	return error;
670 }
671 
672 static void
673 wpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
674 {
675 	int ntries;
676 
677 	wpi_mem_lock(sc);
678 
679 	WPI_WRITE(sc, WPI_RX_CONFIG, 0);
680 	for (ntries = 0; ntries < 100; ntries++) {
681 		if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE)
682 			break;
683 		DELAY(10);
684 	}
685 #ifdef WPI_DEBUG
686 	if (ntries == 100 && wpi_debug > 0)
687 		aprint_error("%s: timeout resetting Rx ring\n",
688 			sc->sc_dev.dv_xname);
689 #endif
690 	wpi_mem_unlock(sc);
691 
692 	ring->cur = 0;
693 }
694 
695 static void
696 wpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
697 {
698 	int i;
699 
700 	wpi_dma_contig_free(&ring->desc_dma);
701 
702 	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
703 		if (ring->data[i].m != NULL)
704 			m_freem(ring->data[i].m);
705 	}
706 }
707 
708 static int
709 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count,
710 	int qid)
711 {
712 	struct wpi_tx_data *data;
713 	int i, error;
714 
715 	ring->qid = qid;
716 	ring->count = count;
717 	ring->queued = 0;
718 	ring->cur = 0;
719 
720 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma,
721 		(void **)&ring->desc, count * sizeof (struct wpi_tx_desc),
722 		WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
723 	if (error != 0) {
724 		aprint_error("%s: could not allocate tx ring DMA memory\n",
725 			sc->sc_dev.dv_xname);
726 		goto fail;
727 	}
728 
729 	/* update shared page with ring's base address */
730 	sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
731 
732 	error = wpi_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma,
733 		(void **)&ring->cmd,
734 		count * sizeof (struct wpi_tx_cmd), 4, BUS_DMA_NOWAIT);
735 	if (error != 0) {
736 		aprint_error("%s: could not allocate tx cmd DMA memory\n",
737 			sc->sc_dev.dv_xname);
738 		goto fail;
739 	}
740 
741 	ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF,
742 		M_NOWAIT);
743 	if (ring->data == NULL) {
744 		aprint_error("%s: could not allocate tx data slots\n",
745 			sc->sc_dev.dv_xname);
746 		goto fail;
747 	}
748 
749 	memset(ring->data, 0, count * sizeof (struct wpi_tx_data));
750 
751 	for (i = 0; i < count; i++) {
752 		data = &ring->data[i];
753 
754 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
755 			WPI_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
756 			&data->map);
757 		if (error != 0) {
758 			aprint_error("%s: could not create tx buf DMA map\n",
759 				sc->sc_dev.dv_xname);
760 			goto fail;
761 		}
762 	}
763 
764 	return 0;
765 
766 fail:	wpi_free_tx_ring(sc, ring);
767 	return error;
768 }
769 
770 static void
771 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
772 {
773 	struct wpi_tx_data *data;
774 	int i, ntries;
775 
776 	wpi_mem_lock(sc);
777 
778 	WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0);
779 	for (ntries = 0; ntries < 100; ntries++) {
780 		if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid))
781 			break;
782 		DELAY(10);
783 	}
784 #ifdef WPI_DEBUG
785 	if (ntries == 100 && wpi_debug > 0) {
786 		aprint_error("%s: timeout resetting Tx ring %d\n",
787 			sc->sc_dev.dv_xname, ring->qid);
788 	}
789 #endif
790 	wpi_mem_unlock(sc);
791 
792 	for (i = 0; i < ring->count; i++) {
793 		data = &ring->data[i];
794 
795 		if (data->m != NULL) {
796 			bus_dmamap_unload(sc->sc_dmat, data->map);
797 			m_freem(data->m);
798 			data->m = NULL;
799 		}
800 	}
801 
802 	ring->queued = 0;
803 	ring->cur = 0;
804 }
805 
806 static void
807 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
808 {
809 	struct wpi_tx_data *data;
810 	int i;
811 
812 	wpi_dma_contig_free(&ring->desc_dma);
813 	wpi_dma_contig_free(&ring->cmd_dma);
814 
815 	if (ring->data != NULL) {
816 		for (i = 0; i < ring->count; i++) {
817 			data = &ring->data[i];
818 
819 			if (data->m != NULL) {
820 				bus_dmamap_unload(sc->sc_dmat, data->map);
821 				m_freem(data->m);
822 			}
823 		}
824 		free(ring->data, M_DEVBUF);
825 	}
826 }
827 
828 /*ARGUSED*/
829 static struct ieee80211_node *
830 wpi_node_alloc(struct ieee80211_node_table *nt __unused)
831 {
832 	struct wpi_node *wn;
833 
834 	wn = malloc(sizeof (struct wpi_node), M_DEVBUF, M_NOWAIT);
835 
836 	if (wn != NULL)
837 		memset(wn, 0, sizeof (struct wpi_node));
838 	return (struct ieee80211_node *)wn;
839 }
840 
841 static void
842 wpi_newassoc(struct ieee80211_node *ni, int isnew)
843 {
844 	struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
845 	int i;
846 
847 	ieee80211_amrr_node_init(&sc->amrr, &((struct wpi_node *)ni)->amn);
848 
849 	/* set rate to some reasonable initial value */
850 	for (i = ni->ni_rates.rs_nrates - 1;
851 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
852 	     i--);
853 	ni->ni_txrate = i;
854 }
855 
856 static int
857 wpi_media_change(struct ifnet *ifp)
858 {
859 	int error;
860 
861 	error = ieee80211_media_change(ifp);
862 	if (error != ENETRESET)
863 		return error;
864 
865 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
866 		wpi_init(ifp);
867 
868 	return 0;
869 }
870 
871 static int
872 wpi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
873 {
874 	struct ifnet *ifp = ic->ic_ifp;
875 	struct wpi_softc *sc = ifp->if_softc;
876 	struct ieee80211_node *ni;
877 	int error;
878 
879 	callout_stop(&sc->calib_to);
880 
881 	switch (nstate) {
882 	case IEEE80211_S_SCAN:
883 
884 		if (sc->is_scanning)
885 			break;
886 
887 		sc->is_scanning = true;
888 		ieee80211_node_table_reset(&ic->ic_scan);
889 		ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
890 
891 		/* make the link LED blink while we're scanning */
892 		wpi_set_led(sc, WPI_LED_LINK, 20, 2);
893 
894 		if ((error = wpi_scan(sc, IEEE80211_CHAN_G)) != 0) {
895 			aprint_error("%s: could not initiate scan\n",
896 				sc->sc_dev.dv_xname);
897 			ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
898 			return error;
899 		}
900 
901 		ic->ic_state = nstate;
902 		return 0;
903 
904 	case IEEE80211_S_ASSOC:
905 		if (ic->ic_state != IEEE80211_S_RUN)
906 			break;
907 		/* FALLTHROUGH */
908 	case IEEE80211_S_AUTH:
909 		sc->config.associd = 0;
910 		sc->config.filter &= ~htole32(WPI_FILTER_BSS);
911 		if ((error = wpi_auth(sc)) != 0) {
912 			aprint_error("%s: could not send authentication request\n",
913 				sc->sc_dev.dv_xname);
914 			return error;
915 		}
916 		break;
917 
918 	case IEEE80211_S_RUN:
919 		if (ic->ic_opmode == IEEE80211_M_MONITOR) {
920 			/* link LED blinks while monitoring */
921 			wpi_set_led(sc, WPI_LED_LINK, 5, 5);
922 			break;
923 		}
924 
925 		ni = ic->ic_bss;
926 
927 		if (ic->ic_opmode != IEEE80211_M_STA) {
928 			(void) wpi_auth(sc);    /* XXX */
929 			wpi_setup_beacon(sc, ni);
930 		}
931 
932 		wpi_enable_tsf(sc, ni);
933 
934 		/* update adapter's configuration */
935 		sc->config.associd = htole16(ni->ni_associd & ~0xc000);
936 		/* short preamble/slot time are negotiated when associating */
937 		sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE |
938 			WPI_CONFIG_SHSLOT);
939 		if (ic->ic_flags & IEEE80211_F_SHSLOT)
940 			sc->config.flags |= htole32(WPI_CONFIG_SHSLOT);
941 		if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
942 			sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE);
943 		sc->config.filter |= htole32(WPI_FILTER_BSS);
944 		if (ic->ic_opmode != IEEE80211_M_STA)
945 			sc->config.filter |= htole32(WPI_FILTER_BEACON);
946 
947 /* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */
948 
949 		DPRINTF(("config chan %d flags %x\n", sc->config.chan,
950 			sc->config.flags));
951 		error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
952 			sizeof (struct wpi_config), 1);
953 		if (error != 0) {
954 			aprint_error("%s: could not update configuration\n",
955 				sc->sc_dev.dv_xname);
956 			return error;
957 		}
958 
959 		/* configuration has changed, set Tx power accordingly */
960 		if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
961 			aprint_error("%s: could not set Tx power\n",
962 			    sc->sc_dev.dv_xname);
963 			return error;
964 		}
965 
966 		if (ic->ic_opmode == IEEE80211_M_STA) {
967 			/* fake a join to init the tx rate */
968 			wpi_newassoc(ni, 1);
969 		}
970 
971 		/* start periodic calibration timer */
972 		sc->calib_cnt = 0;
973 		callout_reset(&sc->calib_to, hz/2, wpi_calib_timeout, sc);
974 
975 		/* link LED always on while associated */
976 		wpi_set_led(sc, WPI_LED_LINK, 0, 1);
977 		break;
978 
979 	case IEEE80211_S_INIT:
980 		sc->is_scanning = false;
981 		break;
982 	}
983 
984 	return sc->sc_newstate(ic, nstate, arg);
985 }
986 
987 /*
988  * XXX: Hack to set the current channel to the value advertised in beacons or
989  * probe responses. Only used during AP detection.
990  * XXX: Duplicated from if_iwi.c
991  */
992 static void
993 wpi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
994 {
995 	struct ieee80211_frame *wh;
996 	uint8_t subtype;
997 	uint8_t *frm, *efrm;
998 
999 	wh = mtod(m, struct ieee80211_frame *);
1000 
1001 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1002 		return;
1003 
1004 	subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1005 
1006 	if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1007 	    subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1008 		return;
1009 
1010 	frm = (uint8_t *)(wh + 1);
1011 	efrm = mtod(m, uint8_t *) + m->m_len;
1012 
1013 	frm += 12;	/* skip tstamp, bintval and capinfo fields */
1014 	while (frm < efrm) {
1015 		if (*frm == IEEE80211_ELEMID_DSPARMS)
1016 #if IEEE80211_CHAN_MAX < 255
1017 		if (frm[2] <= IEEE80211_CHAN_MAX)
1018 #endif
1019 			ic->ic_curchan = &ic->ic_channels[frm[2]];
1020 
1021 		frm += frm[1] + 2;
1022 	}
1023 }
1024 
1025 /*
1026  * Grab exclusive access to NIC memory.
1027  */
1028 static void
1029 wpi_mem_lock(struct wpi_softc *sc)
1030 {
1031 	uint32_t tmp;
1032 	int ntries;
1033 
1034 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
1035 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC);
1036 
1037 	/* spin until we actually get the lock */
1038 	for (ntries = 0; ntries < 1000; ntries++) {
1039 		if ((WPI_READ(sc, WPI_GPIO_CTL) &
1040 			(WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK)
1041 			break;
1042 		DELAY(10);
1043 	}
1044 	if (ntries == 1000)
1045 		aprint_error("%s: could not lock memory\n", sc->sc_dev.dv_xname);
1046 }
1047 
1048 /*
1049  * Release lock on NIC memory.
1050  */
1051 static void
1052 wpi_mem_unlock(struct wpi_softc *sc)
1053 {
1054 	uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL);
1055 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC);
1056 }
1057 
1058 static uint32_t
1059 wpi_mem_read(struct wpi_softc *sc, uint16_t addr)
1060 {
1061 	WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr);
1062 	return WPI_READ(sc, WPI_READ_MEM_DATA);
1063 }
1064 
1065 static void
1066 wpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data)
1067 {
1068 	WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr);
1069 	WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data);
1070 }
1071 
1072 static void
1073 wpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr,
1074 						const uint32_t *data, int wlen)
1075 {
1076 	for (; wlen > 0; wlen--, data++, addr += 4)
1077 		wpi_mem_write(sc, addr, *data);
1078 }
1079 
1080 
1081 /*
1082  * Read `len' bytes from the EEPROM.  We access the EEPROM through the MAC
1083  * instead of using the traditional bit-bang method.
1084  */
1085 static int
1086 wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int len)
1087 {
1088 	uint8_t *out = data;
1089 	uint32_t val;
1090 	int ntries;
1091 
1092 	wpi_mem_lock(sc);
1093 	for (; len > 0; len -= 2, addr++) {
1094 		WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2);
1095 
1096 		for (ntries = 0; ntries < 10; ntries++) {
1097 			if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) &
1098 			    WPI_EEPROM_READY)
1099 				break;
1100 			DELAY(5);
1101 		}
1102 		if (ntries == 10) {
1103 			aprint_error("%s: could not read EEPROM\n",
1104 			    sc->sc_dev.dv_xname);
1105 			return ETIMEDOUT;
1106 		}
1107 		*out++ = val >> 16;
1108 		if (len > 1)
1109 			*out++ = val >> 24;
1110 	}
1111 	wpi_mem_unlock(sc);
1112 
1113 	return 0;
1114 }
1115 
1116 /*
1117  * The firmware boot code is small and is intended to be copied directly into
1118  * the NIC internal memory.
1119  */
1120 int
1121 wpi_load_microcode(struct wpi_softc *sc, const uint8_t *ucode, int size)
1122 {
1123 	int ntries;
1124 
1125 	size /= sizeof (uint32_t);
1126 
1127 	wpi_mem_lock(sc);
1128 
1129 	/* copy microcode image into NIC memory */
1130 	wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE,
1131 	    (const uint32_t *)ucode, size);
1132 
1133 	wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0);
1134 	wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT);
1135 	wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size);
1136 
1137 	/* run microcode */
1138 	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN);
1139 
1140 	/* wait for transfer to complete */
1141 	for (ntries = 0; ntries < 1000; ntries++) {
1142 		if (!(wpi_mem_read(sc, WPI_MEM_UCODE_CTL) & WPI_UC_RUN))
1143 			break;
1144 		DELAY(10);
1145 	}
1146 	if (ntries == 1000) {
1147 		wpi_mem_unlock(sc);
1148 		printf("%s: could not load boot firmware\n",
1149 		    sc->sc_dev.dv_xname);
1150 		return ETIMEDOUT;
1151 	}
1152 	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_ENABLE);
1153 
1154 	wpi_mem_unlock(sc);
1155 
1156 	return 0;
1157 }
1158 
1159 static int
1160 wpi_load_firmware(struct wpi_softc *sc)
1161 {
1162 	struct wpi_dma_info *dma = &sc->fw_dma;
1163 	struct wpi_firmware_hdr hdr;
1164 	const uint8_t *init_text, *init_data, *main_text, *main_data;
1165 	const uint8_t *boot_text;
1166 	uint32_t init_textsz, init_datasz, main_textsz, main_datasz;
1167 	uint32_t boot_textsz;
1168 	firmware_handle_t fw;
1169 	u_char *dfw;
1170 	size_t size;
1171 	int error;
1172 
1173 	/* load firmware image from disk */
1174 	if ((error = firmware_open("if_wpi","iwlwifi-3945.ucode", &fw) != 0)) {
1175 		aprint_error("%s: could not read firmware file\n",
1176 		    sc->sc_dev.dv_xname);
1177 		goto fail1;
1178 	}
1179 
1180 	size = firmware_get_size(fw);
1181 
1182 	/* extract firmware header information */
1183 	if (size < sizeof (struct wpi_firmware_hdr)) {
1184 		aprint_error("%s: truncated firmware header: %zu bytes\n",
1185 		    sc->sc_dev.dv_xname, size);
1186 		error = EINVAL;
1187 		goto fail2;
1188 	}
1189 
1190 	if ((error = firmware_read(fw, 0, &hdr,
1191 		sizeof (struct wpi_firmware_hdr))) != 0) {
1192 		aprint_error("%s: can't get firmware header\n",
1193 			sc->sc_dev.dv_xname);
1194 		goto fail2;
1195 	}
1196 
1197 	main_textsz = le32toh(hdr.main_textsz);
1198 	main_datasz = le32toh(hdr.main_datasz);
1199 	init_textsz = le32toh(hdr.init_textsz);
1200 	init_datasz = le32toh(hdr.init_datasz);
1201 	boot_textsz = le32toh(hdr.boot_textsz);
1202 
1203 	/* sanity-check firmware segments sizes */
1204 	if (main_textsz > WPI_FW_MAIN_TEXT_MAXSZ ||
1205 	    main_datasz > WPI_FW_MAIN_DATA_MAXSZ ||
1206 	    init_textsz > WPI_FW_INIT_TEXT_MAXSZ ||
1207 	    init_datasz > WPI_FW_INIT_DATA_MAXSZ ||
1208 	    boot_textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
1209 	    (boot_textsz & 3) != 0) {
1210 		printf("%s: invalid firmware header\n", sc->sc_dev.dv_xname);
1211 		error = EINVAL;
1212 		goto fail2;
1213 	}
1214 
1215 	/* check that all firmware segments are present */
1216 	if (size < sizeof (struct wpi_firmware_hdr) + main_textsz +
1217 		main_datasz + init_textsz + init_datasz + boot_textsz) {
1218 		aprint_error("%s: firmware file too short: %zu bytes\n",
1219 		    sc->sc_dev.dv_xname, size);
1220 		error = EINVAL;
1221 		goto fail2;
1222 	}
1223 
1224 	dfw = firmware_malloc(size);
1225 	if (dfw == NULL) {
1226 		aprint_error("%s: not enough memory to stock firmware\n",
1227 			sc->sc_dev.dv_xname);
1228 		error = ENOMEM;
1229 		goto fail2;
1230 	}
1231 
1232 	if ((error = firmware_read(fw, 0, dfw, size)) != 0) {
1233 		aprint_error("%s: can't get firmware\n",
1234 			sc->sc_dev.dv_xname);
1235 		goto fail2;
1236 	}
1237 
1238 	/* get pointers to firmware segments */
1239 	main_text = dfw + sizeof (struct wpi_firmware_hdr);
1240 	main_data = main_text + main_textsz;
1241 	init_text = main_data + main_datasz;
1242 	init_data = init_text + init_textsz;
1243 	boot_text = init_data + init_datasz;
1244 
1245 	/* copy initialization images into pre-allocated DMA-safe memory */
1246 	memcpy(dma->vaddr, init_data, init_datasz);
1247 	memcpy((char*)dma->vaddr + WPI_FW_INIT_DATA_MAXSZ, init_text, init_textsz);
1248 
1249 	/* tell adapter where to find initialization images */
1250 	wpi_mem_lock(sc);
1251 	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
1252 	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, init_datasz);
1253 	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
1254 	    dma->paddr + WPI_FW_INIT_DATA_MAXSZ);
1255 	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, init_textsz);
1256 	wpi_mem_unlock(sc);
1257 
1258 	/* load firmware boot code */
1259 	if ((error = wpi_load_microcode(sc, boot_text, boot_textsz)) != 0) {
1260 		printf("%s: could not load boot firmware\n",
1261 		    sc->sc_dev.dv_xname);
1262 		goto fail3;
1263 	}
1264 
1265 	/* now press "execute" ;-) */
1266 	WPI_WRITE(sc, WPI_RESET, 0);
1267 
1268 	/* ..and wait at most one second for adapter to initialize */
1269 	if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
1270 		/* this isn't what was supposed to happen.. */
1271 		aprint_error("%s: timeout waiting for adapter to initialize\n",
1272 		    sc->sc_dev.dv_xname);
1273 	}
1274 
1275 	/* copy runtime images into pre-allocated DMA-safe memory */
1276 	memcpy(dma->vaddr, main_data, main_datasz);
1277 	memcpy((char*)dma->vaddr + WPI_FW_MAIN_DATA_MAXSZ, main_text, main_textsz);
1278 
1279 	/* tell adapter where to find runtime images */
1280 	wpi_mem_lock(sc);
1281 	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
1282 	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, main_datasz);
1283 	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
1284 	    dma->paddr + WPI_FW_MAIN_DATA_MAXSZ);
1285 	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, WPI_FW_UPDATED | main_textsz);
1286 	wpi_mem_unlock(sc);
1287 
1288 	/* wait at most one second for second alive notification */
1289 	if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
1290 		/* this isn't what was supposed to happen.. */
1291 		printf("%s: timeout waiting for adapter to initialize\n",
1292 		    sc->sc_dev.dv_xname);
1293 	}
1294 
1295 
1296 fail3: 	firmware_free(dfw,size);
1297 fail2:	firmware_close(fw);
1298 fail1:	return error;
1299 }
1300 
1301 static void
1302 wpi_calib_timeout(void *arg)
1303 {
1304 	struct wpi_softc *sc = arg;
1305 	struct ieee80211com *ic = &sc->sc_ic;
1306 	int temp, s;
1307 
1308 	/* automatic rate control triggered every 500ms */
1309 	if (ic->ic_fixed_rate == -1) {
1310 		s = splnet();
1311 		if (ic->ic_opmode == IEEE80211_M_STA)
1312 			wpi_iter_func(sc, ic->ic_bss);
1313 		else
1314                 	ieee80211_iterate_nodes(&ic->ic_sta, wpi_iter_func, sc);
1315 		splx(s);
1316 	}
1317 
1318 	/* update sensor data */
1319 	temp = (int)WPI_READ(sc, WPI_TEMPERATURE);
1320 
1321 	/* automatic power calibration every 60s */
1322 	if (++sc->calib_cnt >= 120) {
1323 		wpi_power_calibration(sc, temp);
1324 		sc->calib_cnt = 0;
1325 	}
1326 
1327 	callout_reset(&sc->calib_to, hz/2, wpi_calib_timeout, sc);
1328 }
1329 
1330 static void
1331 wpi_iter_func(void *arg, struct ieee80211_node *ni)
1332 {
1333 	struct wpi_softc *sc = arg;
1334 	struct wpi_node *wn = (struct wpi_node *)ni;
1335 
1336 	ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn);
1337 }
1338 
1339 /*
1340  * This function is called periodically (every 60 seconds) to adjust output
1341  * power to temperature changes.
1342  */
1343 void
1344 wpi_power_calibration(struct wpi_softc *sc, int temp)
1345 {
1346 	/* sanity-check read value */
1347 	if (temp < -260 || temp > 25) {
1348 		/* this can't be correct, ignore */
1349 		DPRINTF(("out-of-range temperature reported: %d\n", temp));
1350 		return;
1351 	}
1352 
1353 	DPRINTF(("temperature %d->%d\n", sc->temp, temp));
1354 
1355 	/* adjust Tx power if need be */
1356 	if (abs(temp - sc->temp) <= 6)
1357 		return;
1358 
1359 	sc->temp = temp;
1360 
1361 	if (wpi_set_txpower(sc, sc->sc_ic.ic_bss->ni_chan, 1) != 0) {
1362 		/* just warn, too bad for the automatic calibration... */
1363 		aprint_error("%s: could not adjust Tx power\n",
1364 		    sc->sc_dev.dv_xname);
1365 	}
1366 }
1367 
1368 static void
1369 wpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1370 	struct wpi_rx_data *data)
1371 {
1372 	struct ieee80211com *ic = &sc->sc_ic;
1373 	struct ifnet *ifp = ic->ic_ifp;
1374 	struct wpi_rx_ring *ring = &sc->rxq;
1375 	struct wpi_rx_stat *stat;
1376 	struct wpi_rx_head *head;
1377 	struct wpi_rx_tail *tail;
1378 	struct wpi_rbuf *rbuf;
1379 	struct ieee80211_frame *wh;
1380 	struct ieee80211_node *ni;
1381 	struct mbuf *m, *mnew;
1382 	int data_off ;
1383 
1384 	stat = (struct wpi_rx_stat *)(desc + 1);
1385 
1386 	if (stat->len > WPI_STAT_MAXLEN) {
1387 		aprint_error("%s: invalid rx statistic header\n",
1388 			sc->sc_dev.dv_xname);
1389 		ifp->if_ierrors++;
1390 		return;
1391 	}
1392 
1393 	head = (struct wpi_rx_head *)((char *)(stat + 1) + stat->len);
1394 	tail = (struct wpi_rx_tail *)((char *)(head + 1) + le16toh(head->len));
1395 
1396 	DPRINTFN(4, ("rx intr: idx=%d len=%d stat len=%d rssi=%d rate=%x "
1397 		"chan=%d tstamp=%" PRId64 "\n", ring->cur, le32toh(desc->len),
1398 		le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan,
1399 		le64toh(tail->tstamp)));
1400 
1401 	/*
1402 	 * Discard Rx frames with bad CRC early (XXX we may want to pass them
1403 	 * to radiotap in monitor mode).
1404 	 */
1405 	if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1406 		DPRINTF(("rx tail flags error %x\n", le32toh(tail->flags)));
1407 		ifp->if_ierrors++;
1408 		return;
1409 	}
1410 
1411 	/* Compute where are the useful datas */
1412 	data_off = (char*)(head + 1) - mtod(data->m, char*);
1413 
1414 	/*
1415 	 * If the number of free entry is too low
1416 	 * just dup the data->m socket and reuse the same rbuf entry
1417 	 */
1418 	if (sc->rxq.nb_free_entries <= WPI_RBUF_LOW_LIMIT) {
1419 
1420 		/* Prepare the mbuf for the m_dup */
1421 		data->m->m_pkthdr.len = data->m->m_len = le16toh(head->len);
1422 		data->m->m_data = (char*) data->m->m_data + data_off;
1423 
1424 		m = m_dup(data->m,0,M_COPYALL,M_DONTWAIT);
1425 
1426 		/* Restore the m_data pointer for future use */
1427 		data->m->m_data = (char*) data->m->m_data - data_off;
1428 
1429 		if (m == NULL) {
1430 			ifp->if_ierrors++;
1431 			return;
1432 		}
1433 	} else {
1434 
1435 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1436 		if (mnew == NULL) {
1437 			ifp->if_ierrors++;
1438 			return;
1439 		}
1440 
1441 		rbuf = wpi_alloc_rbuf(sc);
1442 		KASSERT(rbuf != NULL);
1443 
1444  		/* attach Rx buffer to mbuf */
1445 		MEXTADD(mnew, rbuf->vaddr, WPI_RBUF_SIZE, 0, wpi_free_rbuf,
1446 		 	rbuf);
1447 		mnew->m_flags |= M_EXT_RW;
1448 
1449 		m = data->m;
1450 		data->m = mnew;
1451 
1452 		/* update Rx descriptor */
1453 		ring->desc[ring->cur] = htole32(rbuf->paddr);
1454 
1455 		m->m_data = (char*)m->m_data + data_off;
1456 		m->m_pkthdr.len = m->m_len = le16toh(head->len);
1457 	}
1458 
1459 	/* finalize mbuf */
1460 	m->m_pkthdr.rcvif = ifp;
1461 
1462 	if (ic->ic_state == IEEE80211_S_SCAN)
1463 		wpi_fix_channel(ic, m);
1464 
1465 #if NBPFILTER > 0
1466 	if (sc->sc_drvbpf != NULL) {
1467 		struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
1468 
1469 		tap->wr_flags = 0;
1470 		tap->wr_chan_freq =
1471 			htole16(ic->ic_channels[head->chan].ic_freq);
1472 		tap->wr_chan_flags =
1473 			htole16(ic->ic_channels[head->chan].ic_flags);
1474 		tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
1475 		tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise);
1476 		tap->wr_tsft = tail->tstamp;
1477 		tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
1478 		switch (head->rate) {
1479 		/* CCK rates */
1480 		case  10: tap->wr_rate =   2; break;
1481 		case  20: tap->wr_rate =   4; break;
1482 		case  55: tap->wr_rate =  11; break;
1483 		case 110: tap->wr_rate =  22; break;
1484 		/* OFDM rates */
1485 		case 0xd: tap->wr_rate =  12; break;
1486 		case 0xf: tap->wr_rate =  18; break;
1487 		case 0x5: tap->wr_rate =  24; break;
1488 		case 0x7: tap->wr_rate =  36; break;
1489 		case 0x9: tap->wr_rate =  48; break;
1490 		case 0xb: tap->wr_rate =  72; break;
1491 		case 0x1: tap->wr_rate =  96; break;
1492 		case 0x3: tap->wr_rate = 108; break;
1493 		/* unknown rate: should not happen */
1494 		default:  tap->wr_rate =   0;
1495 		}
1496 		if (le16toh(head->flags) & 0x4)
1497 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1498 
1499 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1500 	}
1501 #endif
1502 
1503 	/* grab a reference to the source node */
1504 	wh = mtod(m, struct ieee80211_frame *);
1505 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1506 
1507 	/* send the frame to the 802.11 layer */
1508 	ieee80211_input(ic, m, ni, stat->rssi, 0);
1509 
1510 	/* release node reference */
1511 	ieee80211_free_node(ni);
1512 }
1513 
1514 static void
1515 wpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1516 {
1517 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
1518 	struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
1519 	struct wpi_tx_data *txdata = &ring->data[desc->idx];
1520 	struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
1521 	struct wpi_node *wn = (struct wpi_node *)txdata->ni;
1522 
1523 	DPRINTFN(4, ("tx done: qid=%d idx=%d retries=%d nkill=%d rate=%x "
1524 		"duration=%d status=%x\n", desc->qid, desc->idx, stat->ntries,
1525 		stat->nkill, stat->rate, le32toh(stat->duration),
1526 		le32toh(stat->status)));
1527 
1528 	/*
1529 	 * Update rate control statistics for the node.
1530 	 * XXX we should not count mgmt frames since they're always sent at
1531 	 * the lowest available bit-rate.
1532 	 */
1533 	wn->amn.amn_txcnt++;
1534 	if (stat->ntries > 0) {
1535 		DPRINTFN(3, ("tx intr ntries %d\n", stat->ntries));
1536 		wn->amn.amn_retrycnt++;
1537 	}
1538 
1539 	if ((le32toh(stat->status) & 0xff) != 1)
1540 		ifp->if_oerrors++;
1541 	else
1542 		ifp->if_opackets++;
1543 
1544 	bus_dmamap_unload(sc->sc_dmat, txdata->map);
1545 	m_freem(txdata->m);
1546 	txdata->m = NULL;
1547 	ieee80211_free_node(txdata->ni);
1548 	txdata->ni = NULL;
1549 
1550 	ring->queued--;
1551 
1552 	sc->sc_tx_timer = 0;
1553 	ifp->if_flags &= ~IFF_OACTIVE;
1554 	wpi_start(ifp);
1555 }
1556 
1557 static void
1558 wpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1559 {
1560 	struct wpi_tx_ring *ring = &sc->cmdq;
1561 	struct wpi_tx_data *data;
1562 
1563 	if ((desc->qid & 7) != 4)
1564 		return;	/* not a command ack */
1565 
1566 	data = &ring->data[desc->idx];
1567 
1568 	/* if the command was mapped in a mbuf, free it */
1569 	if (data->m != NULL) {
1570 		bus_dmamap_unload(sc->sc_dmat, data->map);
1571 		m_freem(data->m);
1572 		data->m = NULL;
1573 	}
1574 
1575 	wakeup(&ring->cmd[desc->idx]);
1576 }
1577 
1578 static void
1579 wpi_notif_intr(struct wpi_softc *sc)
1580 {
1581 	struct ieee80211com *ic = &sc->sc_ic;
1582 	struct ifnet *ifp =  ic->ic_ifp;
1583 	struct wpi_rx_desc *desc;
1584 	struct wpi_rx_data *data;
1585 	uint32_t hw;
1586 
1587 	hw = le32toh(sc->shared->next);
1588 	while (sc->rxq.cur != hw) {
1589 		data = &sc->rxq.data[sc->rxq.cur];
1590 
1591 		desc = mtod(data->m, struct wpi_rx_desc *);
1592 
1593 		DPRINTFN(4, ("rx notification qid=%x idx=%d flags=%x type=%d "
1594 			"len=%d\n", desc->qid, desc->idx, desc->flags,
1595 			desc->type, le32toh(desc->len)));
1596 
1597 		if (!(desc->qid & 0x80))	/* reply to a command */
1598 			wpi_cmd_intr(sc, desc);
1599 
1600 		switch (desc->type) {
1601 		case WPI_RX_DONE:
1602 			/* a 802.11 frame was received */
1603 			wpi_rx_intr(sc, desc, data);
1604 			break;
1605 
1606 		case WPI_TX_DONE:
1607 			/* a 802.11 frame has been transmitted */
1608 			wpi_tx_intr(sc, desc);
1609 			break;
1610 
1611 		case WPI_UC_READY:
1612 		{
1613 			struct wpi_ucode_info *uc =
1614 				(struct wpi_ucode_info *)(desc + 1);
1615 
1616 			/* the microcontroller is ready */
1617 			DPRINTF(("microcode alive notification version %x "
1618 				"alive %x\n", le32toh(uc->version),
1619 				le32toh(uc->valid)));
1620 
1621 			if (le32toh(uc->valid) != 1) {
1622 				aprint_error("%s: microcontroller "
1623 					"initialization failed\n",
1624 					sc->sc_dev.dv_xname);
1625 			}
1626 			break;
1627 		}
1628 		case WPI_STATE_CHANGED:
1629 		{
1630 			uint32_t *status = (uint32_t *)(desc + 1);
1631 
1632 			/* enabled/disabled notification */
1633 			DPRINTF(("state changed to %x\n", le32toh(*status)));
1634 
1635 			if (le32toh(*status) & 1) {
1636 				/* the radio button has to be pushed */
1637 				aprint_error("%s: Radio transmitter is off\n",
1638 					sc->sc_dev.dv_xname);
1639 				/* turn the interface down */
1640 				ifp->if_flags &= ~IFF_UP;
1641 				wpi_stop(ifp, 1);
1642 				return;	/* no further processing */
1643 			}
1644 			break;
1645 		}
1646 		case WPI_START_SCAN:
1647 		{
1648 			struct wpi_start_scan *scan =
1649 				(struct wpi_start_scan *)(desc + 1);
1650 
1651 			DPRINTFN(2, ("scanning channel %d status %x\n",
1652 				scan->chan, le32toh(scan->status)));
1653 
1654 			/* fix current channel */
1655 			ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan];
1656 			break;
1657 		}
1658 		case WPI_STOP_SCAN:
1659 		{
1660 			struct wpi_stop_scan *scan =
1661 				(struct wpi_stop_scan *)(desc + 1);
1662 
1663 			DPRINTF(("scan finished nchan=%d status=%d chan=%d\n",
1664 				scan->nchan, scan->status, scan->chan));
1665 
1666 			if (scan->status == 1 && scan->chan <= 14) {
1667 				/*
1668 				 * We just finished scanning 802.11g channels,
1669 				 * start scanning 802.11a ones.
1670 				 */
1671 				if (wpi_scan(sc, IEEE80211_CHAN_A) == 0)
1672 					break;
1673 			}
1674 			sc->is_scanning = false;
1675 			ieee80211_end_scan(ic);
1676 			break;
1677 		}
1678 		}
1679 
1680 		sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
1681 	}
1682 
1683 	/* tell the firmware what we have processed */
1684 	hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
1685 	WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7);
1686 }
1687 
1688 static int
1689 wpi_intr(void *arg)
1690 {
1691 	struct wpi_softc *sc = arg;
1692 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
1693 	uint32_t r;
1694 
1695 	r = WPI_READ(sc, WPI_INTR);
1696 	if (r == 0 || r == 0xffffffff)
1697 		return 0;	/* not for us */
1698 
1699 	DPRINTFN(5, ("interrupt reg %x\n", r));
1700 
1701 	/* disable interrupts */
1702 	WPI_WRITE(sc, WPI_MASK, 0);
1703 	/* ack interrupts */
1704 	WPI_WRITE(sc, WPI_INTR, r);
1705 
1706 	if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) {
1707 		aprint_error("%s: fatal firmware error\n", sc->sc_dev.dv_xname);
1708 		sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1709 		wpi_stop(sc->sc_ic.ic_ifp, 1);
1710 		return 1;
1711 	}
1712 
1713 	if (r & WPI_RX_INTR)
1714 		wpi_notif_intr(sc);
1715 
1716 	if (r & WPI_ALIVE_INTR)	/* firmware initialized */
1717 		wakeup(sc);
1718 
1719 	/* re-enable interrupts */
1720 	if (ifp->if_flags & IFF_UP)
1721 		WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
1722 
1723 	return 1;
1724 }
1725 
1726 static uint8_t
1727 wpi_plcp_signal(int rate)
1728 {
1729 	switch (rate) {
1730 	/* CCK rates (returned values are device-dependent) */
1731 	case 2:		return 10;
1732 	case 4:		return 20;
1733 	case 11:	return 55;
1734 	case 22:	return 110;
1735 
1736 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1737 	/* R1-R4, (u)ral is R4-R1 */
1738 	case 12:	return 0xd;
1739 	case 18:	return 0xf;
1740 	case 24:	return 0x5;
1741 	case 36:	return 0x7;
1742 	case 48:	return 0x9;
1743 	case 72:	return 0xb;
1744 	case 96:	return 0x1;
1745 	case 108:	return 0x3;
1746 
1747 	/* unsupported rates (should not get there) */
1748 	default:	return 0;
1749 	}
1750 }
1751 
1752 /* quickly determine if a given rate is CCK or OFDM */
1753 #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1754 
1755 static int
1756 wpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1757 	int ac)
1758 {
1759 	struct ieee80211com *ic = &sc->sc_ic;
1760 	struct wpi_tx_ring *ring = &sc->txq[ac];
1761 	struct wpi_tx_desc *desc;
1762 	struct wpi_tx_data *data;
1763 	struct wpi_tx_cmd *cmd;
1764 	struct wpi_cmd_data *tx;
1765 	struct ieee80211_frame *wh;
1766 	struct ieee80211_key *k;
1767 	const struct chanAccParams *cap;
1768 	struct mbuf *mnew;
1769 	int i, error, rate, hdrlen, noack = 0;
1770 
1771 	desc = &ring->desc[ring->cur];
1772 	data = &ring->data[ring->cur];
1773 
1774 	wh = mtod(m0, struct ieee80211_frame *);
1775 
1776 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
1777 		cap = &ic->ic_wme.wme_chanParams;
1778 		noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1779 	}
1780 
1781 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1782 		k = ieee80211_crypto_encap(ic, ni, m0);
1783 		if (k == NULL) {
1784 			m_freem(m0);
1785 			return ENOBUFS;
1786 		}
1787 
1788 		/* packet header may have moved, reset our local pointer */
1789 		wh = mtod(m0, struct ieee80211_frame *);
1790 	}
1791 
1792 	hdrlen = ieee80211_anyhdrsize(wh);
1793 
1794 	/* pickup a rate */
1795 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1796 		IEEE80211_FC0_TYPE_MGT) {
1797 		/* mgmt frames are sent at the lowest available bit-rate */
1798 		rate = ni->ni_rates.rs_rates[0];
1799 	} else {
1800 		if (ic->ic_fixed_rate != -1) {
1801 			rate = ic->ic_sup_rates[ic->ic_curmode].
1802 				rs_rates[ic->ic_fixed_rate];
1803 		} else
1804 			rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1805 	}
1806 	rate &= IEEE80211_RATE_VAL;
1807 
1808 
1809 #if NBPFILTER > 0
1810 	if (sc->sc_drvbpf != NULL) {
1811 		struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
1812 
1813 		tap->wt_flags = 0;
1814 		tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
1815 		tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
1816 		tap->wt_rate = rate;
1817 		tap->wt_hwqueue = ac;
1818 		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1819 			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1820 
1821 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1822 	}
1823 #endif
1824 
1825 	cmd = &ring->cmd[ring->cur];
1826 	cmd->code = WPI_CMD_TX_DATA;
1827 	cmd->flags = 0;
1828 	cmd->qid = ring->qid;
1829 	cmd->idx = ring->cur;
1830 
1831 	tx = (struct wpi_cmd_data *)cmd->data;
1832 	tx->flags = 0;
1833 
1834 	if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1835 		tx->flags |= htole32(WPI_TX_NEED_ACK);
1836 	} else if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
1837 		tx->flags |= htole32(WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP);
1838 
1839 	tx->flags |= htole32(WPI_TX_AUTO_SEQ);
1840 
1841 	/* retrieve destination node's id */
1842 	tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? WPI_ID_BROADCAST :
1843 		WPI_ID_BSS;
1844 
1845 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1846 		IEEE80211_FC0_TYPE_MGT) {
1847 		/* tell h/w to set timestamp in probe responses */
1848 		if ((wh->i_fc[0] &
1849 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1850 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1851 			tx->flags |= htole32(WPI_TX_INSERT_TSTAMP);
1852 
1853 		if (((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1854 			 IEEE80211_FC0_SUBTYPE_ASSOC_REQ) ||
1855 			((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1856 			 IEEE80211_FC0_SUBTYPE_REASSOC_REQ))
1857 			tx->timeout = htole16(3);
1858 		else
1859 			tx->timeout = htole16(2);
1860 	} else
1861 		tx->timeout = htole16(0);
1862 
1863 	tx->rate = wpi_plcp_signal(rate);
1864 
1865 	/* be very persistant at sending frames out */
1866 	tx->rts_ntries = 7;
1867 	tx->data_ntries = 15;
1868 
1869 	tx->ofdm_mask = 0xff;
1870 	tx->cck_mask = 0xf;
1871 	tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
1872 
1873 	tx->len = htole16(m0->m_pkthdr.len);
1874 
1875 	/* save and trim IEEE802.11 header */
1876 	memcpy((uint8_t *)(tx + 1), wh, hdrlen);
1877 	m_adj(m0, hdrlen);
1878 
1879 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1880 		BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1881 	if (error != 0 && error != EFBIG) {
1882 		aprint_error("%s: could not map mbuf (error %d)\n",
1883 			sc->sc_dev.dv_xname, error);
1884 		m_freem(m0);
1885 		return error;
1886 	}
1887 	if (error != 0) {
1888 		/* too many fragments, linearize */
1889 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1890 		if (mnew == NULL) {
1891 			m_freem(m0);
1892 			return ENOMEM;
1893 		}
1894 
1895 		M_COPY_PKTHDR(mnew, m0);
1896 		if (m0->m_pkthdr.len > MHLEN) {
1897 			MCLGET(mnew, M_DONTWAIT);
1898 			if (!(mnew->m_flags & M_EXT)) {
1899 				m_freem(m0);
1900 				m_freem(mnew);
1901 				return ENOMEM;
1902 			}
1903 		}
1904 
1905 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
1906 		m_freem(m0);
1907 		mnew->m_len = mnew->m_pkthdr.len;
1908 		m0 = mnew;
1909 
1910 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1911 			BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1912 		if (error != 0) {
1913 			aprint_error("%s: could not map mbuf (error %d)\n",
1914 				sc->sc_dev.dv_xname, error);
1915 			m_freem(m0);
1916 			return error;
1917 		}
1918 	}
1919 
1920 	data->m = m0;
1921 	data->ni = ni;
1922 
1923 	DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
1924 		ring->qid, ring->cur, m0->m_pkthdr.len, data->map->dm_nsegs));
1925 
1926 	/* first scatter/gather segment is used by the tx data command */
1927 	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 |
1928 		(1 + data->map->dm_nsegs) << 24);
1929 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
1930 		ring->cur * sizeof (struct wpi_tx_cmd));
1931 	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_data) +
1932 						 ((hdrlen + 3) & ~3));
1933 
1934 	for (i = 1; i <= data->map->dm_nsegs; i++) {
1935 		desc->segs[i].addr =
1936 			htole32(data->map->dm_segs[i - 1].ds_addr);
1937 		desc->segs[i].len  =
1938 			htole32(data->map->dm_segs[i - 1].ds_len);
1939 	}
1940 
1941 	ring->queued++;
1942 
1943 	/* kick ring */
1944 	ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
1945 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
1946 
1947 	return 0;
1948 }
1949 
1950 static void
1951 wpi_start(struct ifnet *ifp)
1952 {
1953 	struct wpi_softc *sc = ifp->if_softc;
1954 	struct ieee80211com *ic = &sc->sc_ic;
1955 	struct ieee80211_node *ni;
1956 	struct ether_header *eh;
1957 	struct mbuf *m0;
1958 	int ac;
1959 
1960 	/*
1961 	 * net80211 may still try to send management frames even if the
1962 	 * IFF_RUNNING flag is not set...
1963 	 */
1964 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1965 		return;
1966 
1967 	for (;;) {
1968 		IF_DEQUEUE(&ic->ic_mgtq, m0);
1969 		if (m0 != NULL) {
1970 
1971 			ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1972 			m0->m_pkthdr.rcvif = NULL;
1973 
1974 			/* management frames go into ring 0 */
1975 			if (sc->txq[0].queued > sc->txq[0].count - 8) {
1976 				ifp->if_oerrors++;
1977 				continue;
1978 			}
1979 #if NBPFILTER > 0
1980 			if (ic->ic_rawbpf != NULL)
1981 				bpf_mtap(ic->ic_rawbpf, m0);
1982 #endif
1983 			if (wpi_tx_data(sc, m0, ni, 0) != 0) {
1984 				ifp->if_oerrors++;
1985 				break;
1986 			}
1987 		} else {
1988 			if (ic->ic_state != IEEE80211_S_RUN)
1989 				break;
1990 			IFQ_POLL(&ifp->if_snd, m0);
1991 			if (m0 == NULL)
1992 				break;
1993 
1994 			if (m0->m_len < sizeof (*eh) &&
1995 			    (m0 = m_pullup(m0, sizeof (*eh))) != NULL) {
1996 				ifp->if_oerrors++;
1997 				continue;
1998 			}
1999 			eh = mtod(m0, struct ether_header *);
2000 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2001 			if (ni == NULL) {
2002 				m_freem(m0);
2003 				ifp->if_oerrors++;
2004 				continue;
2005 			}
2006 
2007 			/* classify mbuf so we can find which tx ring to use */
2008 			if (ieee80211_classify(ic, m0, ni) != 0) {
2009 				m_freem(m0);
2010 				ieee80211_free_node(ni);
2011 				ifp->if_oerrors++;
2012 				continue;
2013 			}
2014 
2015 			/* no QoS encapsulation for EAPOL frames */
2016 			ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
2017 			    M_WME_GETAC(m0) : WME_AC_BE;
2018 
2019 			if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
2020 				/* there is no place left in this ring */
2021 				ifp->if_flags |= IFF_OACTIVE;
2022 				break;
2023 			}
2024 			IFQ_DEQUEUE(&ifp->if_snd, m0);
2025 #if NBPFILTER > 0
2026 			if (ifp->if_bpf != NULL)
2027 				bpf_mtap(ifp->if_bpf, m0);
2028 #endif
2029 			m0 = ieee80211_encap(ic, m0, ni);
2030 			if (m0 == NULL) {
2031 				ieee80211_free_node(ni);
2032 				ifp->if_oerrors++;
2033 				continue;
2034 			}
2035 #if NBPFILTER > 0
2036 			if (ic->ic_rawbpf != NULL)
2037 				bpf_mtap(ic->ic_rawbpf, m0);
2038 #endif
2039 			if (wpi_tx_data(sc, m0, ni, ac) != 0) {
2040 				ieee80211_free_node(ni);
2041 				ifp->if_oerrors++;
2042 				break;
2043 			}
2044 		}
2045 
2046 		sc->sc_tx_timer = 5;
2047 		ifp->if_timer = 1;
2048 	}
2049 }
2050 
2051 static void
2052 wpi_watchdog(struct ifnet *ifp)
2053 {
2054 	struct wpi_softc *sc = ifp->if_softc;
2055 
2056 	ifp->if_timer = 0;
2057 
2058 	if (sc->sc_tx_timer > 0) {
2059 		if (--sc->sc_tx_timer == 0) {
2060 			aprint_error("%s: device timeout\n",
2061 				sc->sc_dev.dv_xname);
2062 			ifp->if_oerrors++;
2063 			ifp->if_flags &= ~IFF_UP;
2064 			wpi_stop(ifp, 1);
2065 			return;
2066 		}
2067 		ifp->if_timer = 1;
2068 	}
2069 
2070 	ieee80211_watchdog(&sc->sc_ic);
2071 }
2072 
2073 static int
2074 wpi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2075 {
2076 #define IS_RUNNING(ifp) \
2077 	((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
2078 
2079 	struct wpi_softc *sc = ifp->if_softc;
2080 	struct ieee80211com *ic = &sc->sc_ic;
2081 	int s, error = 0;
2082 
2083 	s = splnet();
2084 
2085 	switch (cmd) {
2086 	case SIOCSIFFLAGS:
2087 		if (ifp->if_flags & IFF_UP) {
2088 			if (!(ifp->if_flags & IFF_RUNNING))
2089 				wpi_init(ifp);
2090 		} else {
2091 			if (ifp->if_flags & IFF_RUNNING)
2092 				wpi_stop(ifp, 1);
2093 		}
2094 		break;
2095 
2096 	case SIOCADDMULTI:
2097 	case SIOCDELMULTI:
2098 		/* XXX no h/w multicast filter? --dyoung */
2099 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
2100 			/* setup multicast filter, etc */
2101 			error = 0;
2102 		}
2103 		break;
2104 
2105 	default:
2106 		error = ieee80211_ioctl(ic, cmd, data);
2107 	}
2108 
2109 	if (error == ENETRESET) {
2110 		if (IS_RUNNING(ifp) &&
2111 			(ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2112 			wpi_init(ifp);
2113 		error = 0;
2114 	}
2115 
2116 	splx(s);
2117 	return error;
2118 
2119 #undef IS_RUNNING
2120 }
2121 
2122 /*
2123  * Extract various information from EEPROM.
2124  */
2125 static void
2126 wpi_read_eeprom(struct wpi_softc *sc)
2127 {
2128 	struct ieee80211com *ic = &sc->sc_ic;
2129 	char domain[4];
2130 	int i;
2131 
2132 	wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap, 1);
2133 	wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev, 2);
2134 	wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1);
2135 
2136 	DPRINTF(("cap=%x rev=%x type=%x\n", sc->cap, le16toh(sc->rev),
2137 	    sc->type));
2138 
2139 	/* read and print regulatory domain */
2140 	wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, domain, 4);
2141 	aprint_normal(", %.4s", domain);
2142 
2143 	/* read and print MAC address */
2144 	wpi_read_prom_data(sc, WPI_EEPROM_MAC, ic->ic_myaddr, 6);
2145 	aprint_normal(", address %s\n", ether_sprintf(ic->ic_myaddr));
2146 
2147 	/* read the list of authorized channels */
2148 	for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
2149 		wpi_read_eeprom_channels(sc, i);
2150 
2151 	/* read the list of power groups */
2152 	for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
2153 		wpi_read_eeprom_group(sc, i);
2154 }
2155 
2156 static void
2157 wpi_read_eeprom_channels(struct wpi_softc *sc, int n)
2158 {
2159 	struct ieee80211com *ic = &sc->sc_ic;
2160 	const struct wpi_chan_band *band = &wpi_bands[n];
2161 	struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND];
2162 	int chan, i;
2163 
2164 	wpi_read_prom_data(sc, band->addr, channels,
2165 	    band->nchan * sizeof (struct wpi_eeprom_chan));
2166 
2167 	for (i = 0; i < band->nchan; i++) {
2168 		if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID))
2169 			continue;
2170 
2171 		chan = band->chan[i];
2172 
2173 		if (n == 0) {	/* 2GHz band */
2174 			ic->ic_channels[chan].ic_freq =
2175 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
2176 			ic->ic_channels[chan].ic_flags =
2177 			    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
2178 			    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
2179 
2180 		} else {	/* 5GHz band */
2181 			/*
2182 			 * Some 3945abg adapters support channels 7, 8, 11
2183 			 * and 12 in the 2GHz *and* 5GHz bands.
2184 			 * Because of limitations in our net80211(9) stack,
2185 			 * we can't support these channels in 5GHz band.
2186 			 */
2187 			if (chan <= 14)
2188 				continue;
2189 
2190 			ic->ic_channels[chan].ic_freq =
2191 			    ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ);
2192 			ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A;
2193 		}
2194 
2195 		/* is active scan allowed on this channel? */
2196 		if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) {
2197 			ic->ic_channels[chan].ic_flags |=
2198 			    IEEE80211_CHAN_PASSIVE;
2199 		}
2200 
2201 		/* save maximum allowed power for this channel */
2202 		sc->maxpwr[chan] = channels[i].maxpwr;
2203 
2204 		DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n",
2205 		    chan, channels[i].flags, sc->maxpwr[chan]));
2206 	}
2207 }
2208 
2209 static void
2210 wpi_read_eeprom_group(struct wpi_softc *sc, int n)
2211 {
2212 	struct wpi_power_group *group = &sc->groups[n];
2213 	struct wpi_eeprom_group rgroup;
2214 	int i;
2215 
2216 	wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup,
2217 	    sizeof rgroup);
2218 
2219 	/* save power group information */
2220 	group->chan   = rgroup.chan;
2221 	group->maxpwr = rgroup.maxpwr;
2222 	/* temperature at which the samples were taken */
2223 	group->temp   = (int16_t)le16toh(rgroup.temp);
2224 
2225 	DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n,
2226 	    group->chan, group->maxpwr, group->temp));
2227 
2228 	for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
2229 		group->samples[i].index = rgroup.samples[i].index;
2230 		group->samples[i].power = rgroup.samples[i].power;
2231 
2232 		DPRINTF(("\tsample %d: index=%d power=%d\n", i,
2233 		    group->samples[i].index, group->samples[i].power));
2234 	}
2235 }
2236 
2237 /*
2238  * Send a command to the firmware.
2239  */
2240 static int
2241 wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
2242 {
2243 	struct wpi_tx_ring *ring = &sc->cmdq;
2244 	struct wpi_tx_desc *desc;
2245 	struct wpi_tx_cmd *cmd;
2246 
2247 	KASSERT(size <= sizeof cmd->data);
2248 
2249 	desc = &ring->desc[ring->cur];
2250 	cmd = &ring->cmd[ring->cur];
2251 
2252 	cmd->code = code;
2253 	cmd->flags = 0;
2254 	cmd->qid = ring->qid;
2255 	cmd->idx = ring->cur;
2256 	memcpy(cmd->data, buf, size);
2257 
2258 	desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
2259 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2260 		ring->cur * sizeof (struct wpi_tx_cmd));
2261 	desc->segs[0].len  = htole32(4 + size);
2262 
2263 	/* kick cmd ring */
2264 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2265 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2266 
2267 	return async ? 0 : tsleep(cmd, PCATCH, "wpicmd", hz);
2268 }
2269 
2270 static int
2271 wpi_wme_update(struct ieee80211com *ic)
2272 {
2273 #define WPI_EXP2(v)	htole16((1 << (v)) - 1)
2274 #define WPI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
2275 	struct wpi_softc *sc = ic->ic_ifp->if_softc;
2276 	const struct wmeParams *wmep;
2277 	struct wpi_wme_setup wme;
2278 	int ac;
2279 
2280 	/* don't override default WME values if WME is not actually enabled */
2281 	if (!(ic->ic_flags & IEEE80211_F_WME))
2282 		return 0;
2283 
2284 	wme.flags = 0;
2285 	for (ac = 0; ac < WME_NUM_AC; ac++) {
2286 		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
2287 		wme.ac[ac].aifsn = wmep->wmep_aifsn;
2288 		wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin);
2289 		wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax);
2290 		wme.ac[ac].txop  = WPI_USEC(wmep->wmep_txopLimit);
2291 
2292 		DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
2293 		    "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
2294 		    wme.ac[ac].cwmax, wme.ac[ac].txop));
2295 	}
2296 
2297 	return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1);
2298 #undef WPI_USEC
2299 #undef WPI_EXP2
2300 }
2301 
2302 /*
2303  * Configure h/w multi-rate retries.
2304  */
2305 static int
2306 wpi_mrr_setup(struct wpi_softc *sc)
2307 {
2308 	struct ieee80211com *ic = &sc->sc_ic;
2309 	struct wpi_mrr_setup mrr;
2310 	int i, error;
2311 
2312 	/* CCK rates (not used with 802.11a) */
2313 	for (i = WPI_CCK1; i <= WPI_CCK11; i++) {
2314 		mrr.rates[i].flags = 0;
2315 		mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
2316 		/* fallback to the immediate lower CCK rate (if any) */
2317 		mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1;
2318 		/* try one time at this rate before falling back to "next" */
2319 		mrr.rates[i].ntries = 1;
2320 	}
2321 
2322 	/* OFDM rates (not used with 802.11b) */
2323 	for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) {
2324 		mrr.rates[i].flags = 0;
2325 		mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
2326 		/* fallback to the immediate lower rate (if any) */
2327 		/* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */
2328 		mrr.rates[i].next = (i == WPI_OFDM6) ?
2329 		    ((ic->ic_curmode == IEEE80211_MODE_11A) ?
2330 			WPI_OFDM6 : WPI_CCK2) :
2331 		    i - 1;
2332 		/* try one time at this rate before falling back to "next" */
2333 		mrr.rates[i].ntries = 1;
2334 	}
2335 
2336 	/* setup MRR for control frames */
2337 	mrr.which = htole32(WPI_MRR_CTL);
2338 	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2339 	if (error != 0) {
2340 		aprint_error("%s: could not setup MRR for control frames\n",
2341 			sc->sc_dev.dv_xname);
2342 		return error;
2343 	}
2344 
2345 	/* setup MRR for data frames */
2346 	mrr.which = htole32(WPI_MRR_DATA);
2347 	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2348 	if (error != 0) {
2349 		aprint_error("%s: could not setup MRR for data frames\n",
2350 			sc->sc_dev.dv_xname);
2351 		return error;
2352 	}
2353 
2354 	return 0;
2355 }
2356 
2357 static void
2358 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
2359 {
2360 	struct wpi_cmd_led led;
2361 
2362 	led.which = which;
2363 	led.unit = htole32(100000);	/* on/off in unit of 100ms */
2364 	led.off = off;
2365 	led.on = on;
2366 
2367 	(void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
2368 }
2369 
2370 static void
2371 wpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni)
2372 {
2373 	struct wpi_cmd_tsf tsf;
2374 	uint64_t val, mod;
2375 
2376 	memset(&tsf, 0, sizeof tsf);
2377 	memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8);
2378 	tsf.bintval = htole16(ni->ni_intval);
2379 	tsf.lintval = htole16(10);
2380 
2381 	/* compute remaining time until next beacon */
2382 	val = (uint64_t)ni->ni_intval  * 1024;	/* msecs -> usecs */
2383 	mod = le64toh(tsf.tstamp) % val;
2384 	tsf.binitval = htole32((uint32_t)(val - mod));
2385 
2386 	DPRINTF(("TSF bintval=%u tstamp=%" PRId64 ", init=%u\n",
2387 	    ni->ni_intval, le64toh(tsf.tstamp), (uint32_t)(val - mod)));
2388 
2389 	if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
2390 		aprint_error("%s: could not enable TSF\n", sc->sc_dev.dv_xname);
2391 }
2392 
2393 /*
2394  * Update Tx power to match what is defined for channel `c'.
2395  */
2396 static int
2397 wpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async)
2398 {
2399 	struct ieee80211com *ic = &sc->sc_ic;
2400 	struct wpi_power_group *group;
2401 	struct wpi_cmd_txpower txpower;
2402 	u_int chan;
2403 	int i;
2404 
2405 	/* get channel number */
2406 	chan = ieee80211_chan2ieee(ic, c);
2407 
2408 	/* find the power group to which this channel belongs */
2409 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
2410 		for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
2411 			if (chan <= group->chan)
2412 				break;
2413 	} else
2414 		group = &sc->groups[0];
2415 
2416 	memset(&txpower, 0, sizeof txpower);
2417 	txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1;
2418 	txpower.chan = htole16(chan);
2419 
2420 	/* set Tx power for all OFDM and CCK rates */
2421 	for (i = 0; i <= 11 ; i++) {
2422 		/* retrieve Tx power for this channel/rate combination */
2423 		int idx = wpi_get_power_index(sc, group, c,
2424 		    wpi_ridx_to_rate[i]);
2425 
2426 		txpower.rates[i].plcp = wpi_ridx_to_plcp[i];
2427 
2428 		if (IEEE80211_IS_CHAN_5GHZ(c)) {
2429 			txpower.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
2430 			txpower.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
2431 		} else {
2432 			txpower.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
2433 			txpower.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
2434 		}
2435 		DPRINTF(("chan %d/rate %d: power index %d\n", chan,
2436 		    wpi_ridx_to_rate[i], idx));
2437 	}
2438 
2439 	return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async);
2440 }
2441 
2442 /*
2443  * Determine Tx power index for a given channel/rate combination.
2444  * This takes into account the regulatory information from EEPROM and the
2445  * current temperature.
2446  */
2447 static int
2448 wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
2449     struct ieee80211_channel *c, int rate)
2450 {
2451 /* fixed-point arithmetic division using a n-bit fractional part */
2452 #define fdivround(a, b, n)	\
2453 	((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
2454 
2455 /* linear interpolation */
2456 #define interpolate(x, x1, y1, x2, y2, n)	\
2457 	((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
2458 
2459 	struct ieee80211com *ic = &sc->sc_ic;
2460 	struct wpi_power_sample *sample;
2461 	int pwr, idx;
2462 	u_int chan;
2463 
2464 	/* get channel number */
2465 	chan = ieee80211_chan2ieee(ic, c);
2466 
2467 	/* default power is group's maximum power - 3dB */
2468 	pwr = group->maxpwr / 2;
2469 
2470 	/* decrease power for highest OFDM rates to reduce distortion */
2471 	switch (rate) {
2472 	case 72:	/* 36Mb/s */
2473 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 :  5;
2474 		break;
2475 	case 96:	/* 48Mb/s */
2476 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10;
2477 		break;
2478 	case 108:	/* 54Mb/s */
2479 		pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12;
2480 		break;
2481 	}
2482 
2483 	/* never exceed channel's maximum allowed Tx power */
2484 	pwr = min(pwr, sc->maxpwr[chan]);
2485 
2486 	/* retrieve power index into gain tables from samples */
2487 	for (sample = group->samples; sample < &group->samples[3]; sample++)
2488 		if (pwr > sample[1].power)
2489 			break;
2490 	/* fixed-point linear interpolation using a 19-bit fractional part */
2491 	idx = interpolate(pwr, sample[0].power, sample[0].index,
2492 	    sample[1].power, sample[1].index, 19);
2493 
2494 	/*
2495 	 * Adjust power index based on current temperature:
2496 	 * - if cooler than factory-calibrated: decrease output power
2497 	 * - if warmer than factory-calibrated: increase output power
2498 	 */
2499 	idx -= (sc->temp - group->temp) * 11 / 100;
2500 
2501 	/* decrease power for CCK rates (-5dB) */
2502 	if (!WPI_RATE_IS_OFDM(rate))
2503 		idx += 10;
2504 
2505 	/* keep power index in a valid range */
2506 	if (idx < 0)
2507 		return 0;
2508 	if (idx > WPI_MAX_PWR_INDEX)
2509 		return WPI_MAX_PWR_INDEX;
2510 	return idx;
2511 
2512 #undef interpolate
2513 #undef fdivround
2514 }
2515 
2516 /*
2517  * Build a beacon frame that the firmware will broadcast periodically in
2518  * IBSS or HostAP modes.
2519  */
2520 static int
2521 wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
2522 {
2523 	struct ieee80211com *ic = &sc->sc_ic;
2524 	struct wpi_tx_ring *ring = &sc->cmdq;
2525 	struct wpi_tx_desc *desc;
2526 	struct wpi_tx_data *data;
2527 	struct wpi_tx_cmd *cmd;
2528 	struct wpi_cmd_beacon *bcn;
2529 	struct ieee80211_beacon_offsets bo;
2530 	struct mbuf *m0;
2531 	int error;
2532 
2533 	desc = &ring->desc[ring->cur];
2534 	data = &ring->data[ring->cur];
2535 
2536 	m0 = ieee80211_beacon_alloc(ic, ni, &bo);
2537 	if (m0 == NULL) {
2538 		aprint_error("%s: could not allocate beacon frame\n",
2539 			sc->sc_dev.dv_xname);
2540 		return ENOMEM;
2541 	}
2542 
2543 	cmd = &ring->cmd[ring->cur];
2544 	cmd->code = WPI_CMD_SET_BEACON;
2545 	cmd->flags = 0;
2546 	cmd->qid = ring->qid;
2547 	cmd->idx = ring->cur;
2548 
2549 	bcn = (struct wpi_cmd_beacon *)cmd->data;
2550 	memset(bcn, 0, sizeof (struct wpi_cmd_beacon));
2551 	bcn->id = WPI_ID_BROADCAST;
2552 	bcn->ofdm_mask = 0xff;
2553 	bcn->cck_mask = 0x0f;
2554 	bcn->lifetime = htole32(WPI_LIFETIME_INFINITE);
2555 	bcn->len = htole16(m0->m_pkthdr.len);
2556 	bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2557 		wpi_plcp_signal(12) : wpi_plcp_signal(2);
2558 	bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
2559 
2560 	/* save and trim IEEE802.11 header */
2561 	m_copydata(m0, 0, sizeof (struct ieee80211_frame), (void *)&bcn->wh);
2562 	m_adj(m0, sizeof (struct ieee80211_frame));
2563 
2564 	/* assume beacon frame is contiguous */
2565 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
2566 		BUS_DMA_READ | BUS_DMA_NOWAIT);
2567 	if (error) {
2568 		aprint_error("%s: could not map beacon\n", sc->sc_dev.dv_xname);
2569 		m_freem(m0);
2570 		return error;
2571 	}
2572 
2573 	data->m = m0;
2574 
2575 	/* first scatter/gather segment is used by the beacon command */
2576 	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24);
2577 	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2578 		ring->cur * sizeof (struct wpi_tx_cmd));
2579 	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_beacon));
2580 	desc->segs[1].addr = htole32(data->map->dm_segs[0].ds_addr);
2581 	desc->segs[1].len  = htole32(data->map->dm_segs[0].ds_len);
2582 
2583 	/* kick cmd ring */
2584 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2585 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2586 
2587 	return 0;
2588 }
2589 
2590 static int
2591 wpi_auth(struct wpi_softc *sc)
2592 {
2593 	struct ieee80211com *ic = &sc->sc_ic;
2594 	struct ieee80211_node *ni = ic->ic_bss;
2595 	struct wpi_node_info node;
2596 	int error;
2597 
2598 	/* update adapter's configuration */
2599 	IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
2600 	sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2601 	sc->config.flags = htole32(WPI_CONFIG_TSF);
2602 	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2603 		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2604 		    WPI_CONFIG_24GHZ);
2605 	}
2606 	switch (ic->ic_curmode) {
2607 	case IEEE80211_MODE_11A:
2608 		sc->config.cck_mask  = 0;
2609 		sc->config.ofdm_mask = 0x15;
2610 		break;
2611 	case IEEE80211_MODE_11B:
2612 		sc->config.cck_mask  = 0x03;
2613 		sc->config.ofdm_mask = 0;
2614 		break;
2615 	default:	/* assume 802.11b/g */
2616 		sc->config.cck_mask  = 0x0f;
2617 		sc->config.ofdm_mask = 0x15;
2618 	}
2619 
2620 	DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
2621 		sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
2622 	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2623 		sizeof (struct wpi_config), 1);
2624 	if (error != 0) {
2625 		aprint_error("%s: could not configure\n", sc->sc_dev.dv_xname);
2626 		return error;
2627 	}
2628 
2629 	/* configuration has changed, set Tx power accordingly */
2630 	if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
2631 		aprint_error("%s: could not set Tx power\n", sc->sc_dev.dv_xname);
2632 		return error;
2633 	}
2634 
2635 	/* add default node */
2636 	memset(&node, 0, sizeof node);
2637 	IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid);
2638 	node.id = WPI_ID_BSS;
2639 	node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2640 	    wpi_plcp_signal(12) : wpi_plcp_signal(2);
2641 	node.action = htole32(WPI_ACTION_SET_RATE);
2642 	node.antenna = WPI_ANTENNA_BOTH;
2643 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2644 	if (error != 0) {
2645 		aprint_error("%s: could not add BSS node\n", sc->sc_dev.dv_xname);
2646 		return error;
2647 	}
2648 
2649 	return 0;
2650 }
2651 
2652 /*
2653  * Send a scan request to the firmware.  Since this command is huge, we map it
2654  * into a mbuf instead of using the pre-allocated set of commands.
2655  */
2656 static int
2657 wpi_scan(struct wpi_softc *sc, uint16_t flags)
2658 {
2659 	struct ieee80211com *ic = &sc->sc_ic;
2660 	struct wpi_tx_ring *ring = &sc->cmdq;
2661 	struct wpi_tx_desc *desc;
2662 	struct wpi_tx_data *data;
2663 	struct wpi_tx_cmd *cmd;
2664 	struct wpi_scan_hdr *hdr;
2665 	struct wpi_scan_chan *chan;
2666 	struct ieee80211_frame *wh;
2667 	struct ieee80211_rateset *rs;
2668 	struct ieee80211_channel *c;
2669 	enum ieee80211_phymode mode;
2670 	uint8_t *frm;
2671 	int nrates, pktlen, error;
2672 
2673 	desc = &ring->desc[ring->cur];
2674 	data = &ring->data[ring->cur];
2675 
2676 	MGETHDR(data->m, M_DONTWAIT, MT_DATA);
2677 	if (data->m == NULL) {
2678 		aprint_error("%s: could not allocate mbuf for scan command\n",
2679 			sc->sc_dev.dv_xname);
2680 		return ENOMEM;
2681 	}
2682 
2683 	MCLGET(data->m, M_DONTWAIT);
2684 	if (!(data->m->m_flags & M_EXT)) {
2685 		m_freem(data->m);
2686 		data->m = NULL;
2687 		aprint_error("%s: could not allocate mbuf for scan command\n",
2688 			sc->sc_dev.dv_xname);
2689 		return ENOMEM;
2690 	}
2691 
2692 	cmd = mtod(data->m, struct wpi_tx_cmd *);
2693 	cmd->code = WPI_CMD_SCAN;
2694 	cmd->flags = 0;
2695 	cmd->qid = ring->qid;
2696 	cmd->idx = ring->cur;
2697 
2698 	hdr = (struct wpi_scan_hdr *)cmd->data;
2699 	memset(hdr, 0, sizeof (struct wpi_scan_hdr));
2700 	hdr->txflags = htole32(WPI_TX_AUTO_SEQ);
2701 	hdr->id = WPI_ID_BROADCAST;
2702 	hdr->lifetime = htole32(WPI_LIFETIME_INFINITE);
2703 
2704 	/*
2705 	 * Move to the next channel if no packets are received within 5 msecs
2706 	 * after sending the probe request (this helps to reduce the duration
2707 	 * of active scans).
2708 	 */
2709 	hdr->quiet = htole16(5);        /* timeout in milliseconds */
2710 	hdr->plcp_threshold = htole16(1);	/* min # of packets */
2711 
2712 	if (flags & IEEE80211_CHAN_A) {
2713 		hdr->crc_threshold = htole16(1);
2714 		/* send probe requests at 6Mbps */
2715 		hdr->rate = wpi_plcp_signal(12);
2716 	} else {
2717 		hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO);
2718 		/* send probe requests at 1Mbps */
2719 		hdr->rate = wpi_plcp_signal(2);
2720 	}
2721 
2722 	/* for directed scans, firmware inserts the essid IE itself */
2723 	hdr->essid[0].id  = IEEE80211_ELEMID_SSID;
2724 	hdr->essid[0].len = ic->ic_des_esslen;
2725 	memcpy(hdr->essid[0].data, ic->ic_des_essid, ic->ic_des_esslen);
2726 
2727 	/*
2728 	 * Build a probe request frame.  Most of the following code is a
2729 	 * copy & paste of what is done in net80211.
2730 	 */
2731 	wh = (struct ieee80211_frame *)(hdr + 1);
2732 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2733 		IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2734 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2735 	IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
2736 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
2737 	IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr);
2738 	*(u_int16_t *)&wh->i_dur[0] = 0;	/* filled by h/w */
2739 	*(u_int16_t *)&wh->i_seq[0] = 0;	/* filled by h/w */
2740 
2741 	frm = (uint8_t *)(wh + 1);
2742 
2743 	/* add empty essid IE (firmware generates it for directed scans) */
2744 	*frm++ = IEEE80211_ELEMID_SSID;
2745 	*frm++ = 0;
2746 
2747 	mode = ieee80211_chan2mode(ic, ic->ic_ibss_chan);
2748 	rs = &ic->ic_sup_rates[mode];
2749 
2750 	/* add supported rates IE */
2751 	*frm++ = IEEE80211_ELEMID_RATES;
2752 	nrates = rs->rs_nrates;
2753 	if (nrates > IEEE80211_RATE_SIZE)
2754 		nrates = IEEE80211_RATE_SIZE;
2755 	*frm++ = nrates;
2756 	memcpy(frm, rs->rs_rates, nrates);
2757 	frm += nrates;
2758 
2759 	/* add supported xrates IE */
2760 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2761 		nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2762 		*frm++ = IEEE80211_ELEMID_XRATES;
2763 		*frm++ = nrates;
2764 		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2765 		frm += nrates;
2766 	}
2767 
2768 	/* setup length of probe request */
2769 	hdr->paylen = htole16(frm - (uint8_t *)wh);
2770 
2771 	chan = (struct wpi_scan_chan *)frm;
2772 	for (c  = &ic->ic_channels[1];
2773 	     c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
2774 		if ((c->ic_flags & flags) != flags)
2775 			continue;
2776 
2777 		chan->chan = ieee80211_chan2ieee(ic, c);
2778 		chan->flags = 0;
2779 		if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
2780 			chan->flags |= WPI_CHAN_ACTIVE;
2781 			if (ic->ic_des_esslen != 0)
2782 				chan->flags |= WPI_CHAN_DIRECT;
2783 		}
2784 		chan->dsp_gain = 0x6e;
2785 		if (IEEE80211_IS_CHAN_5GHZ(c)) {
2786 			chan->rf_gain = 0x3b;
2787 			chan->active = htole16(10);
2788 			chan->passive = htole16(110);
2789 		} else {
2790 			chan->rf_gain = 0x28;
2791 			chan->active = htole16(20);
2792 			chan->passive = htole16(120);
2793 		}
2794 		hdr->nchan++;
2795 		chan++;
2796 
2797 		frm += sizeof (struct wpi_scan_chan);
2798 	}
2799 	hdr->len = htole16(frm - (uint8_t *)hdr);
2800 	pktlen = frm - (uint8_t *)cmd;
2801 
2802 	error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, pktlen,
2803 		NULL, BUS_DMA_NOWAIT);
2804 	if (error) {
2805 		aprint_error("%s: could not map scan command\n",
2806 			sc->sc_dev.dv_xname);
2807 		m_freem(data->m);
2808 		data->m = NULL;
2809 		return error;
2810 	}
2811 
2812 	desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24);
2813 	desc->segs[0].addr = htole32(data->map->dm_segs[0].ds_addr);
2814 	desc->segs[0].len  = htole32(data->map->dm_segs[0].ds_len);
2815 
2816 	/* kick cmd ring */
2817 	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2818 	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2819 
2820 	return 0;	/* will be notified async. of failure/success */
2821 }
2822 
2823 static int
2824 wpi_config(struct wpi_softc *sc)
2825 {
2826 	struct ieee80211com *ic = &sc->sc_ic;
2827 	struct ifnet *ifp = ic->ic_ifp;
2828 	struct wpi_power power;
2829 	struct wpi_bluetooth bluetooth;
2830 	struct wpi_node_info node;
2831 	int error;
2832 
2833 	memset(&power, 0, sizeof power);
2834 	power.flags = htole32(WPI_POWER_CAM | 0x8);
2835 	error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0);
2836 	if (error != 0) {
2837 		aprint_error("%s: could not set power mode\n",
2838 			sc->sc_dev.dv_xname);
2839 		return error;
2840 	}
2841 
2842 	/* configure bluetooth coexistence */
2843 	memset(&bluetooth, 0, sizeof bluetooth);
2844 	bluetooth.flags = 3;
2845 	bluetooth.lead = 0xaa;
2846 	bluetooth.kill = 1;
2847 	error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
2848 		0);
2849 	if (error != 0) {
2850 		aprint_error(
2851 			"%s: could not configure bluetooth coexistence\n",
2852 			sc->sc_dev.dv_xname);
2853 		return error;
2854 	}
2855 
2856 	/* configure adapter */
2857 	memset(&sc->config, 0, sizeof (struct wpi_config));
2858 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2859 	IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr);
2860 	/*set default channel*/
2861 	sc->config.chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
2862 	sc->config.flags = htole32(WPI_CONFIG_TSF);
2863 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan)) {
2864 		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2865 		    WPI_CONFIG_24GHZ);
2866 	}
2867 	sc->config.filter = 0;
2868 	switch (ic->ic_opmode) {
2869 	case IEEE80211_M_STA:
2870 		sc->config.mode = WPI_MODE_STA;
2871 		sc->config.filter |= htole32(WPI_FILTER_MULTICAST);
2872 		break;
2873 	case IEEE80211_M_IBSS:
2874 	case IEEE80211_M_AHDEMO:
2875 		sc->config.mode = WPI_MODE_IBSS;
2876 		break;
2877 	case IEEE80211_M_HOSTAP:
2878 		sc->config.mode = WPI_MODE_HOSTAP;
2879 		break;
2880 	case IEEE80211_M_MONITOR:
2881 		sc->config.mode = WPI_MODE_MONITOR;
2882 		sc->config.filter |= htole32(WPI_FILTER_MULTICAST |
2883 			WPI_FILTER_CTL | WPI_FILTER_PROMISC);
2884 		break;
2885 	}
2886 	sc->config.cck_mask  = 0x0f;	/* not yet negotiated */
2887 	sc->config.ofdm_mask = 0xff;	/* not yet negotiated */
2888 	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2889 		sizeof (struct wpi_config), 0);
2890 	if (error != 0) {
2891 		aprint_error("%s: configure command failed\n",
2892 			sc->sc_dev.dv_xname);
2893 		return error;
2894 	}
2895 
2896 	/* configuration has changed, set Tx power accordingly */
2897 	if ((error = wpi_set_txpower(sc, ic->ic_ibss_chan, 0)) != 0) {
2898 		aprint_error("%s: could not set Tx power\n", sc->sc_dev.dv_xname);
2899 		return error;
2900 	}
2901 
2902 	/* add broadcast node */
2903 	memset(&node, 0, sizeof node);
2904 	IEEE80211_ADDR_COPY(node.bssid, etherbroadcastaddr);
2905 	node.id = WPI_ID_BROADCAST;
2906 	node.rate = wpi_plcp_signal(2);
2907 	node.action = htole32(WPI_ACTION_SET_RATE);
2908 	node.antenna = WPI_ANTENNA_BOTH;
2909 	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
2910 	if (error != 0) {
2911 		aprint_error("%s: could not add broadcast node\n",
2912 			sc->sc_dev.dv_xname);
2913 		return error;
2914 	}
2915 
2916 	if ((error = wpi_mrr_setup(sc)) != 0) {
2917 		aprint_error("%s: could not setup MRR\n", sc->sc_dev.dv_xname);
2918 		return error;
2919 	}
2920 
2921 	return 0;
2922 }
2923 
2924 static void
2925 wpi_stop_master(struct wpi_softc *sc)
2926 {
2927 	uint32_t tmp;
2928 	int ntries;
2929 
2930 	tmp = WPI_READ(sc, WPI_RESET);
2931 	WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER);
2932 
2933 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
2934 	if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP)
2935 		return;	/* already asleep */
2936 
2937 	for (ntries = 0; ntries < 100; ntries++) {
2938 		if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED)
2939 			break;
2940 		DELAY(10);
2941 	}
2942 	if (ntries == 100) {
2943 		aprint_error("%s: timeout waiting for master\n",
2944 			sc->sc_dev.dv_xname);
2945 	}
2946 }
2947 
2948 static int
2949 wpi_power_up(struct wpi_softc *sc)
2950 {
2951 	uint32_t tmp;
2952 	int ntries;
2953 
2954 	wpi_mem_lock(sc);
2955 	tmp = wpi_mem_read(sc, WPI_MEM_POWER);
2956 	wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000);
2957 	wpi_mem_unlock(sc);
2958 
2959 	for (ntries = 0; ntries < 5000; ntries++) {
2960 		if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED)
2961 			break;
2962 		DELAY(10);
2963 	}
2964 	if (ntries == 5000) {
2965 		aprint_error("%s: timeout waiting for NIC to power up\n",
2966 			sc->sc_dev.dv_xname);
2967 		return ETIMEDOUT;
2968 	}
2969 	return 0;
2970 }
2971 
2972 static int
2973 wpi_reset(struct wpi_softc *sc)
2974 {
2975 	uint32_t tmp;
2976 	int ntries;
2977 
2978 	/* clear any pending interrupts */
2979 	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
2980 
2981 	tmp = WPI_READ(sc, WPI_PLL_CTL);
2982 	WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT);
2983 
2984 	tmp = WPI_READ(sc, WPI_CHICKEN);
2985 	WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS);
2986 
2987 	tmp = WPI_READ(sc, WPI_GPIO_CTL);
2988 	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT);
2989 
2990 	/* wait for clock stabilization */
2991 	for (ntries = 0; ntries < 1000; ntries++) {
2992 		if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK)
2993 			break;
2994 		DELAY(10);
2995 	}
2996 	if (ntries == 1000) {
2997 		aprint_error("%s: timeout waiting for clock stabilization\n",
2998 			sc->sc_dev.dv_xname);
2999 		return ETIMEDOUT;
3000 	}
3001 
3002 	/* initialize EEPROM */
3003 	tmp = WPI_READ(sc, WPI_EEPROM_STATUS);
3004 	if ((tmp & WPI_EEPROM_VERSION) == 0) {
3005 		aprint_error("%s: EEPROM not found\n", sc->sc_dev.dv_xname);
3006 		return EIO;
3007 	}
3008 	WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED);
3009 
3010 	return 0;
3011 }
3012 
3013 static void
3014 wpi_hw_config(struct wpi_softc *sc)
3015 {
3016 	uint32_t rev, hw;
3017 
3018 	/* voodoo from the reference driver */
3019 	hw = WPI_READ(sc, WPI_HWCONFIG);
3020 
3021 	rev = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG);
3022 	rev = PCI_REVISION(rev);
3023 	if ((rev & 0xc0) == 0x40)
3024 		hw |= WPI_HW_ALM_MB;
3025 	else if (!(rev & 0x80))
3026 		hw |= WPI_HW_ALM_MM;
3027 
3028 	if (sc->cap == 0x80)
3029 		hw |= WPI_HW_SKU_MRC;
3030 
3031 	hw &= ~WPI_HW_REV_D;
3032 	if ((le16toh(sc->rev) & 0xf0) == 0xd0)
3033 		hw |= WPI_HW_REV_D;
3034 
3035 	if (sc->type > 1)
3036 		hw |= WPI_HW_TYPE_B;
3037 
3038 	DPRINTF(("setting h/w config %x\n", hw));
3039 	WPI_WRITE(sc, WPI_HWCONFIG, hw);
3040 }
3041 
3042 static int
3043 wpi_init(struct ifnet *ifp)
3044 {
3045 	struct wpi_softc *sc = ifp->if_softc;
3046 	struct ieee80211com *ic = &sc->sc_ic;
3047 	uint32_t tmp;
3048 	int qid, ntries, error;
3049 
3050 	wpi_stop(ifp,1);
3051 	(void)wpi_reset(sc);
3052 
3053 	wpi_mem_lock(sc);
3054 	wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00);
3055 	DELAY(20);
3056 	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
3057 	wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800);
3058 	wpi_mem_unlock(sc);
3059 
3060 	(void)wpi_power_up(sc);
3061 	wpi_hw_config(sc);
3062 
3063 	/* init Rx ring */
3064 	wpi_mem_lock(sc);
3065 	WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr);
3066 	WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr +
3067 	    offsetof(struct wpi_shared, next));
3068 	WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
3069 	WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010);
3070 	wpi_mem_unlock(sc);
3071 
3072 	/* init Tx rings */
3073 	wpi_mem_lock(sc);
3074 	wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */
3075 	wpi_mem_write(sc, WPI_MEM_RA, 1);   /* enable RA0 */
3076 	wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */
3077 	wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000);
3078 	wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002);
3079 	wpi_mem_write(sc, WPI_MEM_MAGIC4, 4);
3080 	wpi_mem_write(sc, WPI_MEM_MAGIC5, 5);
3081 
3082 	WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr);
3083 	WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5);
3084 
3085 	for (qid = 0; qid < 6; qid++) {
3086 		WPI_WRITE(sc, WPI_TX_CTL(qid), 0);
3087 		WPI_WRITE(sc, WPI_TX_BASE(qid), 0);
3088 		WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008);
3089 	}
3090 	wpi_mem_unlock(sc);
3091 
3092 	/* clear "radio off" and "disable command" bits (reversed logic) */
3093 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3094 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
3095 
3096 	/* clear any pending interrupts */
3097 	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
3098 	/* enable interrupts */
3099 	WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
3100 
3101 	/* not sure why/if this is necessary... */
3102 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3103 	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3104 
3105 	if ((error = wpi_load_firmware(sc)) != 0) {
3106 		aprint_error("%s: could not load firmware\n", sc->sc_dev.dv_xname);
3107 		goto fail1;
3108 	}
3109 
3110 	/* wait for thermal sensors to calibrate */
3111 	for (ntries = 0; ntries < 1000; ntries++) {
3112 		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
3113 			break;
3114 		DELAY(10);
3115 	}
3116 	if (ntries == 1000) {
3117 		aprint_error("%s: timeout waiting for thermal sensors calibration\n",
3118 			sc->sc_dev.dv_xname);
3119 		error = ETIMEDOUT;
3120 		goto fail1;
3121 	}
3122 
3123 	DPRINTF(("temperature %d\n", sc->temp));
3124 
3125 	if ((error = wpi_config(sc)) != 0) {
3126 		aprint_error("%s: could not configure device\n",
3127 			sc->sc_dev.dv_xname);
3128 		goto fail1;
3129 	}
3130 
3131 	ifp->if_flags &= ~IFF_OACTIVE;
3132 	ifp->if_flags |= IFF_RUNNING;
3133 
3134 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
3135 		if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
3136 			ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
3137 	}
3138 	else
3139 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
3140 
3141 	return 0;
3142 
3143 fail1:	wpi_stop(ifp, 1);
3144 	return error;
3145 }
3146 
3147 
3148 static void
3149 wpi_stop(struct ifnet *ifp, int disable)
3150 {
3151 	struct wpi_softc *sc = ifp->if_softc;
3152 	struct ieee80211com *ic = &sc->sc_ic;
3153 	uint32_t tmp;
3154 	int ac;
3155 
3156 	ifp->if_timer = sc->sc_tx_timer = 0;
3157 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3158 
3159 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
3160 
3161 	/* disable interrupts */
3162 	WPI_WRITE(sc, WPI_MASK, 0);
3163 	WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK);
3164 	WPI_WRITE(sc, WPI_INTR_STATUS, 0xff);
3165 	WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000);
3166 
3167 	wpi_mem_lock(sc);
3168 	wpi_mem_write(sc, WPI_MEM_MODE, 0);
3169 	wpi_mem_unlock(sc);
3170 
3171 	/* reset all Tx rings */
3172 	for (ac = 0; ac < 4; ac++)
3173 		wpi_reset_tx_ring(sc, &sc->txq[ac]);
3174 	wpi_reset_tx_ring(sc, &sc->cmdq);
3175 
3176 	/* reset Rx ring */
3177 	wpi_reset_rx_ring(sc, &sc->rxq);
3178 
3179 	wpi_mem_lock(sc);
3180 	wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200);
3181 	wpi_mem_unlock(sc);
3182 
3183 	DELAY(5);
3184 
3185 	wpi_stop_master(sc);
3186 
3187 	tmp = WPI_READ(sc, WPI_RESET);
3188 	WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET);
3189 }
3190