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