if_wpi.c revision 190526
1173362Sbenjsc/*-
2173362Sbenjsc * Copyright (c) 2006,2007
3173362Sbenjsc *	Damien Bergamini <damien.bergamini@free.fr>
4173362Sbenjsc *	Benjamin Close <Benjamin.Close@clearchain.com>
5173362Sbenjsc *
6173362Sbenjsc * Permission to use, copy, modify, and distribute this software for any
7173362Sbenjsc * purpose with or without fee is hereby granted, provided that the above
8173362Sbenjsc * copyright notice and this permission notice appear in all copies.
9173362Sbenjsc *
10173362Sbenjsc * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11173362Sbenjsc * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12173362Sbenjsc * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13173362Sbenjsc * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14173362Sbenjsc * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15173362Sbenjsc * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16173362Sbenjsc * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17173362Sbenjsc */
18173362Sbenjsc
19173977Sbenjsc#define VERSION "20071127"
20173362Sbenjsc
21173362Sbenjsc#include <sys/cdefs.h>
22173362Sbenjsc__FBSDID("$FreeBSD: head/sys/dev/wpi/if_wpi.c 190526 2009-03-29 17:59:14Z sam $");
23173362Sbenjsc
24173362Sbenjsc/*
25173362Sbenjsc * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
26173362Sbenjsc *
27173362Sbenjsc * The 3945ABG network adapter doesn't use traditional hardware as
28173362Sbenjsc * many other adaptors do. Instead at run time the eeprom is set into a known
29173362Sbenjsc * state and told to load boot firmware. The boot firmware loads an init and a
30173362Sbenjsc * main  binary firmware image into SRAM on the card via DMA.
31173362Sbenjsc * Once the firmware is loaded, the driver/hw then
32173362Sbenjsc * communicate by way of circular dma rings via the the SRAM to the firmware.
33173362Sbenjsc *
34173362Sbenjsc * There is 6 memory rings. 1 command ring, 1 rx data ring & 4 tx data rings.
35173362Sbenjsc * The 4 tx data rings allow for prioritization QoS.
36173362Sbenjsc *
37173362Sbenjsc * The rx data ring consists of 32 dma buffers. Two registers are used to
38173362Sbenjsc * indicate where in the ring the driver and the firmware are up to. The
39173362Sbenjsc * driver sets the initial read index (reg1) and the initial write index (reg2),
40173362Sbenjsc * the firmware updates the read index (reg1) on rx of a packet and fires an
41173362Sbenjsc * interrupt. The driver then processes the buffers starting at reg1 indicating
42173362Sbenjsc * to the firmware which buffers have been accessed by updating reg2. At the
43173362Sbenjsc * same time allocating new memory for the processed buffer.
44173362Sbenjsc *
45173362Sbenjsc * A similar thing happens with the tx rings. The difference is the firmware
46173362Sbenjsc * stop processing buffers once the queue is full and until confirmation
47173362Sbenjsc * of a successful transmition (tx_intr) has occurred.
48173362Sbenjsc *
49173362Sbenjsc * The command ring operates in the same manner as the tx queues.
50173362Sbenjsc *
51173362Sbenjsc * All communication direct to the card (ie eeprom) is classed as Stage1
52173362Sbenjsc * communication
53173362Sbenjsc *
54173362Sbenjsc * All communication via the firmware to the card is classed as State2.
55173362Sbenjsc * The firmware consists of 2 parts. A bootstrap firmware and a runtime
56173362Sbenjsc * firmware. The bootstrap firmware and runtime firmware are loaded
57173362Sbenjsc * from host memory via dma to the card then told to execute. From this point
58173362Sbenjsc * on the majority of communications between the driver and the card goes
59173362Sbenjsc * via the firmware.
60173362Sbenjsc */
61173362Sbenjsc
62173362Sbenjsc#include <sys/param.h>
63173362Sbenjsc#include <sys/sysctl.h>
64173362Sbenjsc#include <sys/sockio.h>
65173362Sbenjsc#include <sys/mbuf.h>
66173362Sbenjsc#include <sys/kernel.h>
67173362Sbenjsc#include <sys/socket.h>
68173362Sbenjsc#include <sys/systm.h>
69173362Sbenjsc#include <sys/malloc.h>
70173362Sbenjsc#include <sys/queue.h>
71173362Sbenjsc#include <sys/taskqueue.h>
72173362Sbenjsc#include <sys/module.h>
73173362Sbenjsc#include <sys/bus.h>
74173362Sbenjsc#include <sys/endian.h>
75173362Sbenjsc#include <sys/linker.h>
76173362Sbenjsc#include <sys/firmware.h>
77173362Sbenjsc
78173362Sbenjsc#include <machine/bus.h>
79173362Sbenjsc#include <machine/resource.h>
80173362Sbenjsc#include <sys/rman.h>
81173362Sbenjsc
82173362Sbenjsc#include <dev/pci/pcireg.h>
83173362Sbenjsc#include <dev/pci/pcivar.h>
84173362Sbenjsc
85173362Sbenjsc#include <net/bpf.h>
86173362Sbenjsc#include <net/if.h>
87173362Sbenjsc#include <net/if_arp.h>
88173362Sbenjsc#include <net/ethernet.h>
89173362Sbenjsc#include <net/if_dl.h>
90173362Sbenjsc#include <net/if_media.h>
91173362Sbenjsc#include <net/if_types.h>
92173362Sbenjsc
93173362Sbenjsc#include <net80211/ieee80211_var.h>
94173362Sbenjsc#include <net80211/ieee80211_radiotap.h>
95173362Sbenjsc#include <net80211/ieee80211_regdomain.h>
96173362Sbenjsc
97173362Sbenjsc#include <netinet/in.h>
98173362Sbenjsc#include <netinet/in_systm.h>
99173362Sbenjsc#include <netinet/in_var.h>
100173362Sbenjsc#include <netinet/ip.h>
101173362Sbenjsc#include <netinet/if_ether.h>
102173362Sbenjsc
103173362Sbenjsc#include <dev/wpi/if_wpireg.h>
104173362Sbenjsc#include <dev/wpi/if_wpivar.h>
105173362Sbenjsc
106173362Sbenjsc#define WPI_DEBUG
107173362Sbenjsc
108173362Sbenjsc#ifdef WPI_DEBUG
109173362Sbenjsc#define DPRINTF(x)	do { if (wpi_debug != 0) printf x; } while (0)
110173362Sbenjsc#define DPRINTFN(n, x)	do { if (wpi_debug & n) printf x; } while (0)
111179957Sthompsa#define	WPI_DEBUG_SET	(wpi_debug != 0)
112173362Sbenjsc
113173362Sbenjscenum {
114173362Sbenjsc	WPI_DEBUG_UNUSED	= 0x00000001,   /* Unused */
115173362Sbenjsc	WPI_DEBUG_HW		= 0x00000002,   /* Stage 1 (eeprom) debugging */
116173362Sbenjsc	WPI_DEBUG_TX		= 0x00000004,   /* Stage 2 TX intrp debugging*/
117173362Sbenjsc	WPI_DEBUG_RX		= 0x00000008,   /* Stage 2 RX intrp debugging */
118173362Sbenjsc	WPI_DEBUG_CMD		= 0x00000010,   /* Stage 2 CMD intrp debugging*/
119173362Sbenjsc	WPI_DEBUG_FIRMWARE	= 0x00000020,   /* firmware(9) loading debug  */
120173362Sbenjsc	WPI_DEBUG_DMA		= 0x00000040,   /* DMA (de)allocations/syncs  */
121173362Sbenjsc	WPI_DEBUG_SCANNING	= 0x00000080,   /* Stage 2 Scanning debugging */
122173362Sbenjsc	WPI_DEBUG_NOTIFY	= 0x00000100,   /* State 2 Noftif intr debug */
123173362Sbenjsc	WPI_DEBUG_TEMP		= 0x00000200,   /* TXPower/Temp Calibration */
124173362Sbenjsc	WPI_DEBUG_OPS		= 0x00000400,   /* wpi_ops taskq debug */
125173362Sbenjsc	WPI_DEBUG_WATCHDOG	= 0x00000800,   /* Watch dog debug */
126173362Sbenjsc	WPI_DEBUG_ANY		= 0xffffffff
127173362Sbenjsc};
128173362Sbenjsc
129179957Sthompsastatic int wpi_debug = 1;
130173362SbenjscSYSCTL_INT(_debug, OID_AUTO, wpi, CTLFLAG_RW, &wpi_debug, 0, "wpi debug level");
131179957SthompsaTUNABLE_INT("debug.wpi", &wpi_debug);
132173362Sbenjsc
133173362Sbenjsc#else
134173362Sbenjsc#define DPRINTF(x)
135173362Sbenjsc#define DPRINTFN(n, x)
136179957Sthompsa#define WPI_DEBUG_SET	0
137173362Sbenjsc#endif
138173362Sbenjsc
139173362Sbenjscstruct wpi_ident {
140173362Sbenjsc	uint16_t	vendor;
141173362Sbenjsc	uint16_t	device;
142173362Sbenjsc	uint16_t	subdevice;
143173362Sbenjsc	const char	*name;
144173362Sbenjsc};
145173362Sbenjsc
146173362Sbenjscstatic const struct wpi_ident wpi_ident_table[] = {
147173362Sbenjsc	/* The below entries support ABG regardless of the subid */
148173362Sbenjsc	{ 0x8086, 0x4222,    0x0, "Intel(R) PRO/Wireless 3945ABG" },
149173362Sbenjsc	{ 0x8086, 0x4227,    0x0, "Intel(R) PRO/Wireless 3945ABG" },
150173362Sbenjsc	/* The below entries only support BG */
151182127Sbenjsc	{ 0x8086, 0x4222, 0x1005, "Intel(R) PRO/Wireless 3945BG"  },
152182127Sbenjsc	{ 0x8086, 0x4222, 0x1034, "Intel(R) PRO/Wireless 3945BG"  },
153182127Sbenjsc	{ 0x8086, 0x4227, 0x1014, "Intel(R) PRO/Wireless 3945BG"  },
154182127Sbenjsc	{ 0x8086, 0x4222, 0x1044, "Intel(R) PRO/Wireless 3945BG"  },
155173362Sbenjsc	{ 0, 0, 0, NULL }
156173362Sbenjsc};
157173362Sbenjsc
158178354Ssamstatic struct ieee80211vap *wpi_vap_create(struct ieee80211com *,
159178354Ssam		    const char name[IFNAMSIZ], int unit, int opmode,
160178354Ssam		    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
161178354Ssam		    const uint8_t mac[IEEE80211_ADDR_LEN]);
162178354Ssamstatic void	wpi_vap_delete(struct ieee80211vap *);
163173362Sbenjscstatic int	wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *,
164173362Sbenjsc		    void **, bus_size_t, bus_size_t, int);
165173362Sbenjscstatic void	wpi_dma_contig_free(struct wpi_dma_info *);
166173362Sbenjscstatic void	wpi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
167173362Sbenjscstatic int	wpi_alloc_shared(struct wpi_softc *);
168173362Sbenjscstatic void	wpi_free_shared(struct wpi_softc *);
169173362Sbenjscstatic int	wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
170173362Sbenjscstatic void	wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
171173362Sbenjscstatic void	wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
172173362Sbenjscstatic int	wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
173173362Sbenjsc		    int, int);
174173362Sbenjscstatic void	wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
175173362Sbenjscstatic void	wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
176179643Ssamstatic struct ieee80211_node *wpi_node_alloc(struct ieee80211vap *,
177179643Ssam			    const uint8_t mac[IEEE80211_ADDR_LEN]);
178178354Ssamstatic int	wpi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
179173362Sbenjscstatic void	wpi_mem_lock(struct wpi_softc *);
180173362Sbenjscstatic void	wpi_mem_unlock(struct wpi_softc *);
181173362Sbenjscstatic uint32_t	wpi_mem_read(struct wpi_softc *, uint16_t);
182173362Sbenjscstatic void	wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t);
183173362Sbenjscstatic void	wpi_mem_write_region_4(struct wpi_softc *, uint16_t,
184173362Sbenjsc		    const uint32_t *, int);
185173362Sbenjscstatic uint16_t	wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
186173362Sbenjscstatic int	wpi_alloc_fwmem(struct wpi_softc *);
187173362Sbenjscstatic void	wpi_free_fwmem(struct wpi_softc *);
188173362Sbenjscstatic int	wpi_load_firmware(struct wpi_softc *);
189173362Sbenjscstatic void	wpi_unload_firmware(struct wpi_softc *);
190173362Sbenjscstatic int	wpi_load_microcode(struct wpi_softc *, const uint8_t *, int);
191173362Sbenjscstatic void	wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *,
192173362Sbenjsc		    struct wpi_rx_data *);
193173362Sbenjscstatic void	wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *);
194173362Sbenjscstatic void	wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *);
195179037Sthompsastatic void	wpi_bmiss(void *, int);
196173362Sbenjscstatic void	wpi_notif_intr(struct wpi_softc *);
197173362Sbenjscstatic void	wpi_intr(void *);
198173362Sbenjscstatic void	wpi_ops(void *, int);
199173362Sbenjscstatic uint8_t	wpi_plcp_signal(int);
200177043Sthompsastatic int	wpi_queue_cmd(struct wpi_softc *, int, int, int);
201177043Sthompsastatic void	wpi_watchdog(void *);
202173362Sbenjscstatic int	wpi_tx_data(struct wpi_softc *, struct mbuf *,
203173362Sbenjsc		    struct ieee80211_node *, int);
204173362Sbenjscstatic void	wpi_start(struct ifnet *);
205178354Ssamstatic void	wpi_start_locked(struct ifnet *);
206178354Ssamstatic int	wpi_raw_xmit(struct ieee80211_node *, struct mbuf *,
207178354Ssam		    const struct ieee80211_bpf_params *);
208173362Sbenjscstatic void	wpi_scan_start(struct ieee80211com *);
209173362Sbenjscstatic void	wpi_scan_end(struct ieee80211com *);
210173362Sbenjscstatic void	wpi_set_channel(struct ieee80211com *);
211178354Ssamstatic void	wpi_scan_curchan(struct ieee80211_scan_state *, unsigned long);
212178354Ssamstatic void	wpi_scan_mindwell(struct ieee80211_scan_state *);
213173362Sbenjscstatic int	wpi_ioctl(struct ifnet *, u_long, caddr_t);
214190526Ssamstatic void	wpi_read_eeprom(struct wpi_softc *,
215190526Ssam		    uint8_t macaddr[IEEE80211_ADDR_LEN]);
216173362Sbenjscstatic void	wpi_read_eeprom_channels(struct wpi_softc *, int);
217173362Sbenjscstatic void	wpi_read_eeprom_group(struct wpi_softc *, int);
218173362Sbenjscstatic int	wpi_cmd(struct wpi_softc *, int, const void *, int, int);
219173362Sbenjscstatic int	wpi_wme_update(struct ieee80211com *);
220173362Sbenjscstatic int	wpi_mrr_setup(struct wpi_softc *);
221173362Sbenjscstatic void	wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
222173362Sbenjscstatic void	wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *);
223173362Sbenjsc#if 0
224173362Sbenjscstatic int	wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
225173362Sbenjsc#endif
226178354Ssamstatic int	wpi_auth(struct wpi_softc *, struct ieee80211vap *);
227178354Ssamstatic int	wpi_run(struct wpi_softc *, struct ieee80211vap *);
228173362Sbenjscstatic int	wpi_scan(struct wpi_softc *);
229173362Sbenjscstatic int	wpi_config(struct wpi_softc *);
230173362Sbenjscstatic void	wpi_stop_master(struct wpi_softc *);
231173362Sbenjscstatic int	wpi_power_up(struct wpi_softc *);
232173362Sbenjscstatic int	wpi_reset(struct wpi_softc *);
233173362Sbenjscstatic void	wpi_hw_config(struct wpi_softc *);
234173362Sbenjscstatic void	wpi_init(void *);
235177043Sthompsastatic void	wpi_init_locked(struct wpi_softc *, int);
236173362Sbenjscstatic void	wpi_stop(struct wpi_softc *);
237173362Sbenjscstatic void	wpi_stop_locked(struct wpi_softc *);
238173362Sbenjsc
239173362Sbenjscstatic void	wpi_newassoc(struct ieee80211_node *, int);
240173362Sbenjscstatic int	wpi_set_txpower(struct wpi_softc *, struct ieee80211_channel *,
241173362Sbenjsc		    int);
242173362Sbenjscstatic void	wpi_calib_timeout(void *);
243173362Sbenjscstatic void	wpi_power_calibration(struct wpi_softc *, int);
244173362Sbenjscstatic int	wpi_get_power_index(struct wpi_softc *,
245173362Sbenjsc		    struct wpi_power_group *, struct ieee80211_channel *, int);
246179957Sthompsa#ifdef WPI_DEBUG
247173362Sbenjscstatic const char *wpi_cmd_str(int);
248179957Sthompsa#endif
249173362Sbenjscstatic int wpi_probe(device_t);
250173362Sbenjscstatic int wpi_attach(device_t);
251173362Sbenjscstatic int wpi_detach(device_t);
252173362Sbenjscstatic int wpi_shutdown(device_t);
253173362Sbenjscstatic int wpi_suspend(device_t);
254173362Sbenjscstatic int wpi_resume(device_t);
255173362Sbenjsc
256173362Sbenjsc
257173362Sbenjscstatic device_method_t wpi_methods[] = {
258173362Sbenjsc	/* Device interface */
259173362Sbenjsc	DEVMETHOD(device_probe,		wpi_probe),
260173362Sbenjsc	DEVMETHOD(device_attach,	wpi_attach),
261173362Sbenjsc	DEVMETHOD(device_detach,	wpi_detach),
262173362Sbenjsc	DEVMETHOD(device_shutdown,	wpi_shutdown),
263173362Sbenjsc	DEVMETHOD(device_suspend,	wpi_suspend),
264173362Sbenjsc	DEVMETHOD(device_resume,	wpi_resume),
265173362Sbenjsc
266173362Sbenjsc	{ 0, 0 }
267173362Sbenjsc};
268173362Sbenjsc
269173362Sbenjscstatic driver_t wpi_driver = {
270173362Sbenjsc	"wpi",
271173362Sbenjsc	wpi_methods,
272173362Sbenjsc	sizeof (struct wpi_softc)
273173362Sbenjsc};
274173362Sbenjsc
275173362Sbenjscstatic devclass_t wpi_devclass;
276173362Sbenjsc
277173362SbenjscDRIVER_MODULE(wpi, pci, wpi_driver, wpi_devclass, 0, 0);
278173362Sbenjsc
279173362Sbenjscstatic const uint8_t wpi_ridx_to_plcp[] = {
280173362Sbenjsc	/* OFDM: IEEE Std 802.11a-1999, pp. 14 Table 80 */
281173362Sbenjsc	/* R1-R4 (ral/ural is R4-R1) */
282173362Sbenjsc	0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3,
283173362Sbenjsc	/* CCK: device-dependent */
284173362Sbenjsc	10, 20, 55, 110
285173362Sbenjsc};
286173362Sbenjscstatic const uint8_t wpi_ridx_to_rate[] = {
287173362Sbenjsc	12, 18, 24, 36, 48, 72, 96, 108, /* OFDM */
288173362Sbenjsc	2, 4, 11, 22 /*CCK */
289173362Sbenjsc};
290173362Sbenjsc
291173362Sbenjsc
292173362Sbenjscstatic int
293173362Sbenjscwpi_probe(device_t dev)
294173362Sbenjsc{
295173362Sbenjsc	const struct wpi_ident *ident;
296173362Sbenjsc
297173362Sbenjsc	for (ident = wpi_ident_table; ident->name != NULL; ident++) {
298173362Sbenjsc		if (pci_get_vendor(dev) == ident->vendor &&
299173362Sbenjsc		    pci_get_device(dev) == ident->device) {
300173362Sbenjsc			device_set_desc(dev, ident->name);
301173362Sbenjsc			return 0;
302173362Sbenjsc		}
303173362Sbenjsc	}
304173362Sbenjsc	return ENXIO;
305173362Sbenjsc}
306173362Sbenjsc
307173362Sbenjsc/**
308173362Sbenjsc * Load the firmare image from disk to the allocated dma buffer.
309173362Sbenjsc * we also maintain the reference to the firmware pointer as there
310173362Sbenjsc * is times where we may need to reload the firmware but we are not
311173362Sbenjsc * in a context that can access the filesystem (ie taskq cause by restart)
312173362Sbenjsc *
313173362Sbenjsc * @return 0 on success, an errno on failure
314173362Sbenjsc */
315173362Sbenjscstatic int
316173362Sbenjscwpi_load_firmware(struct wpi_softc *sc)
317173362Sbenjsc{
318178354Ssam	const struct firmware *fp;
319173362Sbenjsc	struct wpi_dma_info *dma = &sc->fw_dma;
320173362Sbenjsc	const struct wpi_firmware_hdr *hdr;
321173362Sbenjsc	const uint8_t *itext, *idata, *rtext, *rdata, *btext;
322173362Sbenjsc	uint32_t itextsz, idatasz, rtextsz, rdatasz, btextsz;
323173362Sbenjsc	int error;
324173362Sbenjsc
325173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
326173362Sbenjsc	    ("Attempting Loading Firmware from wpi_fw module\n"));
327173362Sbenjsc
328173362Sbenjsc	WPI_UNLOCK(sc);
329173362Sbenjsc
330173362Sbenjsc	if (sc->fw_fp == NULL && (sc->fw_fp = firmware_get("wpifw")) == NULL) {
331173362Sbenjsc		device_printf(sc->sc_dev,
332173362Sbenjsc		    "could not load firmware image 'wpifw'\n");
333173362Sbenjsc		error = ENOENT;
334173362Sbenjsc		WPI_LOCK(sc);
335173362Sbenjsc		goto fail;
336173362Sbenjsc	}
337173362Sbenjsc
338173362Sbenjsc	fp = sc->fw_fp;
339173362Sbenjsc
340173362Sbenjsc	WPI_LOCK(sc);
341173362Sbenjsc
342173362Sbenjsc	/* Validate the firmware is minimum a particular version */
343173362Sbenjsc	if (fp->version < WPI_FW_MINVERSION) {
344173362Sbenjsc	    device_printf(sc->sc_dev,
345173362Sbenjsc			   "firmware version is too old. Need %d, got %d\n",
346173362Sbenjsc			   WPI_FW_MINVERSION,
347173362Sbenjsc			   fp->version);
348173362Sbenjsc	    error = ENXIO;
349173362Sbenjsc	    goto fail;
350173362Sbenjsc	}
351173362Sbenjsc
352173362Sbenjsc	if (fp->datasize < sizeof (struct wpi_firmware_hdr)) {
353173362Sbenjsc		device_printf(sc->sc_dev,
354173362Sbenjsc		    "firmware file too short: %zu bytes\n", fp->datasize);
355173362Sbenjsc		error = ENXIO;
356173362Sbenjsc		goto fail;
357173362Sbenjsc	}
358173362Sbenjsc
359173362Sbenjsc	hdr = (const struct wpi_firmware_hdr *)fp->data;
360173362Sbenjsc
361173362Sbenjsc	/*     |  RUNTIME FIRMWARE   |    INIT FIRMWARE    | BOOT FW  |
362173362Sbenjsc	   |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */
363173362Sbenjsc
364173362Sbenjsc	rtextsz = le32toh(hdr->rtextsz);
365173362Sbenjsc	rdatasz = le32toh(hdr->rdatasz);
366173362Sbenjsc	itextsz = le32toh(hdr->itextsz);
367173362Sbenjsc	idatasz = le32toh(hdr->idatasz);
368173362Sbenjsc	btextsz = le32toh(hdr->btextsz);
369173362Sbenjsc
370173362Sbenjsc	/* check that all firmware segments are present */
371173362Sbenjsc	if (fp->datasize < sizeof (struct wpi_firmware_hdr) +
372173362Sbenjsc		rtextsz + rdatasz + itextsz + idatasz + btextsz) {
373173362Sbenjsc		device_printf(sc->sc_dev,
374173362Sbenjsc		    "firmware file too short: %zu bytes\n", fp->datasize);
375173362Sbenjsc		error = ENXIO; /* XXX appropriate error code? */
376173362Sbenjsc		goto fail;
377173362Sbenjsc	}
378173362Sbenjsc
379173362Sbenjsc	/* get pointers to firmware segments */
380173362Sbenjsc	rtext = (const uint8_t *)(hdr + 1);
381173362Sbenjsc	rdata = rtext + rtextsz;
382173362Sbenjsc	itext = rdata + rdatasz;
383173362Sbenjsc	idata = itext + itextsz;
384173362Sbenjsc	btext = idata + idatasz;
385173362Sbenjsc
386173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
387173362Sbenjsc	    ("Firmware Version: Major %d, Minor %d, Driver %d, \n"
388173362Sbenjsc	     "runtime (text: %u, data: %u) init (text: %u, data %u) boot (text %u)\n",
389173362Sbenjsc	     (le32toh(hdr->version) & 0xff000000) >> 24,
390173362Sbenjsc	     (le32toh(hdr->version) & 0x00ff0000) >> 16,
391173362Sbenjsc	     (le32toh(hdr->version) & 0x0000ffff),
392173362Sbenjsc	     rtextsz, rdatasz,
393173362Sbenjsc	     itextsz, idatasz, btextsz));
394173362Sbenjsc
395173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("rtext 0x%x\n", *(const uint32_t *)rtext));
396173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("rdata 0x%x\n", *(const uint32_t *)rdata));
397173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("itext 0x%x\n", *(const uint32_t *)itext));
398173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("idata 0x%x\n", *(const uint32_t *)idata));
399173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("btext 0x%x\n", *(const uint32_t *)btext));
400173362Sbenjsc
401173362Sbenjsc	/* sanity checks */
402173362Sbenjsc	if (rtextsz > WPI_FW_MAIN_TEXT_MAXSZ ||
403173362Sbenjsc	    rdatasz > WPI_FW_MAIN_DATA_MAXSZ ||
404173362Sbenjsc	    itextsz > WPI_FW_INIT_TEXT_MAXSZ ||
405173362Sbenjsc	    idatasz > WPI_FW_INIT_DATA_MAXSZ ||
406173362Sbenjsc	    btextsz > WPI_FW_BOOT_TEXT_MAXSZ ||
407173362Sbenjsc	    (btextsz & 3) != 0) {
408173362Sbenjsc		device_printf(sc->sc_dev, "firmware invalid\n");
409173362Sbenjsc		error = EINVAL;
410173362Sbenjsc		goto fail;
411173362Sbenjsc	}
412173362Sbenjsc
413173362Sbenjsc	/* copy initialization images into pre-allocated DMA-safe memory */
414173362Sbenjsc	memcpy(dma->vaddr, idata, idatasz);
415173362Sbenjsc	memcpy(dma->vaddr + WPI_FW_INIT_DATA_MAXSZ, itext, itextsz);
416173362Sbenjsc
417173362Sbenjsc	bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
418173362Sbenjsc
419173362Sbenjsc	/* tell adapter where to find initialization images */
420173362Sbenjsc	wpi_mem_lock(sc);
421173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
422173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, idatasz);
423173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
424173362Sbenjsc	    dma->paddr + WPI_FW_INIT_DATA_MAXSZ);
425173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, itextsz);
426173362Sbenjsc	wpi_mem_unlock(sc);
427173362Sbenjsc
428173362Sbenjsc	/* load firmware boot code */
429173362Sbenjsc	if ((error = wpi_load_microcode(sc, btext, btextsz)) != 0) {
430173362Sbenjsc	    device_printf(sc->sc_dev, "Failed to load microcode\n");
431173362Sbenjsc	    goto fail;
432173362Sbenjsc	}
433173362Sbenjsc
434173362Sbenjsc	/* now press "execute" */
435173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, 0);
436173362Sbenjsc
437173362Sbenjsc	/* wait at most one second for the first alive notification */
438173362Sbenjsc	if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
439173362Sbenjsc		device_printf(sc->sc_dev,
440173362Sbenjsc		    "timeout waiting for adapter to initialize\n");
441173362Sbenjsc		goto fail;
442173362Sbenjsc	}
443173362Sbenjsc
444173362Sbenjsc	/* copy runtime images into pre-allocated DMA-sage memory */
445173362Sbenjsc	memcpy(dma->vaddr, rdata, rdatasz);
446173362Sbenjsc	memcpy(dma->vaddr + WPI_FW_MAIN_DATA_MAXSZ, rtext, rtextsz);
447173362Sbenjsc	bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
448173362Sbenjsc
449173362Sbenjsc	/* tell adapter where to find runtime images */
450173362Sbenjsc	wpi_mem_lock(sc);
451173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
452173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, rdatasz);
453173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
454173362Sbenjsc	    dma->paddr + WPI_FW_MAIN_DATA_MAXSZ);
455173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, WPI_FW_UPDATED | rtextsz);
456173362Sbenjsc	wpi_mem_unlock(sc);
457173362Sbenjsc
458173362Sbenjsc	/* wait at most one second for the first alive notification */
459173362Sbenjsc	if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
460173362Sbenjsc		device_printf(sc->sc_dev,
461173362Sbenjsc		    "timeout waiting for adapter to initialize2\n");
462173362Sbenjsc		goto fail;
463173362Sbenjsc	}
464173362Sbenjsc
465173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
466173362Sbenjsc	    ("Firmware loaded to driver successfully\n"));
467173362Sbenjsc	return error;
468173362Sbenjscfail:
469173362Sbenjsc	wpi_unload_firmware(sc);
470173362Sbenjsc	return error;
471173362Sbenjsc}
472173362Sbenjsc
473173362Sbenjsc/**
474173362Sbenjsc * Free the referenced firmware image
475173362Sbenjsc */
476173362Sbenjscstatic void
477173362Sbenjscwpi_unload_firmware(struct wpi_softc *sc)
478173362Sbenjsc{
479173362Sbenjsc
480173362Sbenjsc	if (sc->fw_fp) {
481173362Sbenjsc		WPI_UNLOCK(sc);
482173362Sbenjsc		firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
483173362Sbenjsc		WPI_LOCK(sc);
484173362Sbenjsc		sc->fw_fp = NULL;
485173362Sbenjsc	}
486173362Sbenjsc}
487173362Sbenjsc
488173362Sbenjscstatic int
489173362Sbenjscwpi_attach(device_t dev)
490173362Sbenjsc{
491173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
492173362Sbenjsc	struct ifnet *ifp;
493178354Ssam	struct ieee80211com *ic;
494173362Sbenjsc	int ac, error, supportsa = 1;
495173362Sbenjsc	uint32_t tmp;
496173362Sbenjsc	const struct wpi_ident *ident;
497190526Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
498173362Sbenjsc
499173362Sbenjsc	sc->sc_dev = dev;
500173362Sbenjsc
501179957Sthompsa	if (bootverbose || WPI_DEBUG_SET)
502173362Sbenjsc	    device_printf(sc->sc_dev,"Driver Revision %s\n", VERSION);
503173362Sbenjsc
504173362Sbenjsc	/*
505173362Sbenjsc	 * Some card's only support 802.11b/g not a, check to see if
506173362Sbenjsc	 * this is one such card. A 0x0 in the subdevice table indicates
507173362Sbenjsc	 * the entire subdevice range is to be ignored.
508173362Sbenjsc	 */
509173362Sbenjsc	for (ident = wpi_ident_table; ident->name != NULL; ident++) {
510173362Sbenjsc		if (ident->subdevice &&
511173362Sbenjsc		    pci_get_subdevice(dev) == ident->subdevice) {
512173362Sbenjsc		    supportsa = 0;
513173362Sbenjsc		    break;
514173362Sbenjsc		}
515173362Sbenjsc	}
516173362Sbenjsc
517173362Sbenjsc	/*
518173362Sbenjsc	 * Create the taskqueues used by the driver. Primarily
519173362Sbenjsc	 * sc_tq handles most the task
520173362Sbenjsc	 */
521173362Sbenjsc	sc->sc_tq = taskqueue_create("wpi_taskq", M_NOWAIT | M_ZERO,
522173362Sbenjsc	    taskqueue_thread_enqueue, &sc->sc_tq);
523173362Sbenjsc	taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
524173362Sbenjsc	    device_get_nameunit(dev));
525173362Sbenjsc
526173362Sbenjsc	/* Create the tasks that can be queued */
527173362Sbenjsc	TASK_INIT(&sc->sc_opstask, 0, wpi_ops, sc);
528179037Sthompsa	TASK_INIT(&sc->sc_bmiss_task, 0, wpi_bmiss, sc);
529173362Sbenjsc
530173362Sbenjsc	WPI_LOCK_INIT(sc);
531173362Sbenjsc	WPI_CMD_LOCK_INIT(sc);
532173362Sbenjsc
533173362Sbenjsc	callout_init_mtx(&sc->calib_to, &sc->sc_mtx, 0);
534173362Sbenjsc	callout_init_mtx(&sc->watchdog_to, &sc->sc_mtx, 0);
535173362Sbenjsc
536173362Sbenjsc	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
537173362Sbenjsc		device_printf(dev, "chip is in D%d power mode "
538173362Sbenjsc		    "-- setting to D0\n", pci_get_powerstate(dev));
539173362Sbenjsc		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
540173362Sbenjsc	}
541173362Sbenjsc
542173362Sbenjsc	/* disable the retry timeout register */
543173362Sbenjsc	pci_write_config(dev, 0x41, 0, 1);
544173362Sbenjsc
545173362Sbenjsc	/* enable bus-mastering */
546173362Sbenjsc	pci_enable_busmaster(dev);
547173362Sbenjsc
548173362Sbenjsc	sc->mem_rid = PCIR_BAR(0);
549173362Sbenjsc	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
550173362Sbenjsc	    RF_ACTIVE);
551173362Sbenjsc	if (sc->mem == NULL) {
552173362Sbenjsc		device_printf(dev, "could not allocate memory resource\n");
553173362Sbenjsc		error = ENOMEM;
554173362Sbenjsc		goto fail;
555173362Sbenjsc	}
556173362Sbenjsc
557173362Sbenjsc	sc->sc_st = rman_get_bustag(sc->mem);
558173362Sbenjsc	sc->sc_sh = rman_get_bushandle(sc->mem);
559173362Sbenjsc
560173362Sbenjsc	sc->irq_rid = 0;
561173362Sbenjsc	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
562173362Sbenjsc	    RF_ACTIVE | RF_SHAREABLE);
563173362Sbenjsc	if (sc->irq == NULL) {
564173362Sbenjsc		device_printf(dev, "could not allocate interrupt resource\n");
565173362Sbenjsc		error = ENOMEM;
566173362Sbenjsc		goto fail;
567173362Sbenjsc	}
568173362Sbenjsc
569173362Sbenjsc	/*
570173362Sbenjsc	 * Allocate DMA memory for firmware transfers.
571173362Sbenjsc	 */
572173362Sbenjsc	if ((error = wpi_alloc_fwmem(sc)) != 0) {
573173362Sbenjsc		printf(": could not allocate firmware memory\n");
574173362Sbenjsc		error = ENOMEM;
575173362Sbenjsc		goto fail;
576173362Sbenjsc	}
577173362Sbenjsc
578173362Sbenjsc	/*
579173362Sbenjsc	 * Put adapter into a known state.
580173362Sbenjsc	 */
581173362Sbenjsc	if ((error = wpi_reset(sc)) != 0) {
582173362Sbenjsc		device_printf(dev, "could not reset adapter\n");
583173362Sbenjsc		goto fail;
584173362Sbenjsc	}
585173362Sbenjsc
586173362Sbenjsc	wpi_mem_lock(sc);
587173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
588179957Sthompsa	if (bootverbose || WPI_DEBUG_SET)
589173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Revision (0x%X)\n", tmp);
590173362Sbenjsc
591173362Sbenjsc	wpi_mem_unlock(sc);
592173362Sbenjsc
593173362Sbenjsc	/* Allocate shared page */
594173362Sbenjsc	if ((error = wpi_alloc_shared(sc)) != 0) {
595173362Sbenjsc		device_printf(dev, "could not allocate shared page\n");
596173362Sbenjsc		goto fail;
597173362Sbenjsc	}
598173362Sbenjsc
599173362Sbenjsc	/* tx data queues  - 4 for QoS purposes */
600173362Sbenjsc	for (ac = 0; ac < WME_NUM_AC; ac++) {
601173362Sbenjsc		error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, ac);
602173362Sbenjsc		if (error != 0) {
603173362Sbenjsc		    device_printf(dev, "could not allocate Tx ring %d\n",ac);
604173362Sbenjsc		    goto fail;
605173362Sbenjsc		}
606173362Sbenjsc	}
607173362Sbenjsc
608173362Sbenjsc	/* command queue to talk to the card's firmware */
609173362Sbenjsc	error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4);
610173362Sbenjsc	if (error != 0) {
611173362Sbenjsc		device_printf(dev, "could not allocate command ring\n");
612173362Sbenjsc		goto fail;
613173362Sbenjsc	}
614173362Sbenjsc
615173362Sbenjsc	/* receive data queue */
616173362Sbenjsc	error = wpi_alloc_rx_ring(sc, &sc->rxq);
617173362Sbenjsc	if (error != 0) {
618173362Sbenjsc		device_printf(dev, "could not allocate Rx ring\n");
619173362Sbenjsc		goto fail;
620173362Sbenjsc	}
621173362Sbenjsc
622178354Ssam	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
623173362Sbenjsc	if (ifp == NULL) {
624173362Sbenjsc		device_printf(dev, "can not if_alloc()\n");
625173362Sbenjsc		error = ENOMEM;
626173362Sbenjsc		goto fail;
627173362Sbenjsc	}
628178354Ssam	ic = ifp->if_l2com;
629173362Sbenjsc
630173362Sbenjsc	ic->ic_ifp = ifp;
631178354Ssam	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
632178354Ssam	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
633173362Sbenjsc
634173362Sbenjsc	/* set device capabilities */
635173362Sbenjsc	ic->ic_caps =
636178957Ssam		  IEEE80211_C_STA		/* station mode supported */
637178957Ssam		| IEEE80211_C_MONITOR		/* monitor mode supported */
638173362Sbenjsc		| IEEE80211_C_TXPMGT		/* tx power management */
639173362Sbenjsc		| IEEE80211_C_SHSLOT		/* short slot time supported */
640173362Sbenjsc		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
641173362Sbenjsc		| IEEE80211_C_WPA		/* 802.11i */
642173362Sbenjsc/* XXX looks like WME is partly supported? */
643173362Sbenjsc#if 0
644173362Sbenjsc		| IEEE80211_C_IBSS		/* IBSS mode support */
645173362Sbenjsc		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
646173362Sbenjsc		| IEEE80211_C_WME		/* 802.11e */
647173362Sbenjsc		| IEEE80211_C_HOSTAP		/* Host access point mode */
648173362Sbenjsc#endif
649173362Sbenjsc		;
650173362Sbenjsc
651173362Sbenjsc	/*
652173362Sbenjsc	 * Read in the eeprom and also setup the channels for
653173362Sbenjsc	 * net80211. We don't set the rates as net80211 does this for us
654173362Sbenjsc	 */
655190526Ssam	wpi_read_eeprom(sc, macaddr);
656173362Sbenjsc
657179957Sthompsa	if (bootverbose || WPI_DEBUG_SET) {
658173362Sbenjsc	    device_printf(sc->sc_dev, "Regulatory Domain: %.4s\n", sc->domain);
659173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Type: %c\n",
660173362Sbenjsc			  sc->type > 1 ? 'B': '?');
661173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Revision: %c\n",
662173362Sbenjsc			  ((le16toh(sc->rev) & 0xf0) == 0xd0) ? 'D': '?');
663173362Sbenjsc	    device_printf(sc->sc_dev, "SKU %s support 802.11a\n",
664173362Sbenjsc			  supportsa ? "does" : "does not");
665173362Sbenjsc
666173362Sbenjsc	    /* XXX hw_config uses the PCIDEV for the Hardware rev. Must check
667173362Sbenjsc	       what sc->rev really represents - benjsc 20070615 */
668173362Sbenjsc	}
669173362Sbenjsc
670173362Sbenjsc	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
671173362Sbenjsc	ifp->if_softc = sc;
672173362Sbenjsc	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
673173362Sbenjsc	ifp->if_init = wpi_init;
674173362Sbenjsc	ifp->if_ioctl = wpi_ioctl;
675173362Sbenjsc	ifp->if_start = wpi_start;
676173362Sbenjsc	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
677173362Sbenjsc	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
678173362Sbenjsc	IFQ_SET_READY(&ifp->if_snd);
679178354Ssam
680190526Ssam	ieee80211_ifattach(ic, macaddr);
681173362Sbenjsc	/* override default methods */
682173362Sbenjsc	ic->ic_node_alloc = wpi_node_alloc;
683173362Sbenjsc	ic->ic_newassoc = wpi_newassoc;
684178354Ssam	ic->ic_raw_xmit = wpi_raw_xmit;
685173362Sbenjsc	ic->ic_wme.wme_update = wpi_wme_update;
686173362Sbenjsc	ic->ic_scan_start = wpi_scan_start;
687173362Sbenjsc	ic->ic_scan_end = wpi_scan_end;
688173362Sbenjsc	ic->ic_set_channel = wpi_set_channel;
689173362Sbenjsc	ic->ic_scan_curchan = wpi_scan_curchan;
690173362Sbenjsc	ic->ic_scan_mindwell = wpi_scan_mindwell;
691173362Sbenjsc
692178354Ssam	ic->ic_vap_create = wpi_vap_create;
693178354Ssam	ic->ic_vap_delete = wpi_vap_delete;
694173362Sbenjsc
695178354Ssam	bpfattach(ifp, DLT_IEEE802_11_RADIO,
696178354Ssam	    sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap));
697173362Sbenjsc
698173362Sbenjsc	sc->sc_rxtap_len = sizeof sc->sc_rxtap;
699173362Sbenjsc	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
700173362Sbenjsc	sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT);
701173362Sbenjsc
702173362Sbenjsc	sc->sc_txtap_len = sizeof sc->sc_txtap;
703173362Sbenjsc	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
704173362Sbenjsc	sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT);
705173362Sbenjsc
706173362Sbenjsc	/*
707173362Sbenjsc	 * Hook our interrupt after all initialization is complete.
708173362Sbenjsc	 */
709177043Sthompsa	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET |INTR_MPSAFE,
710177043Sthompsa	    NULL, wpi_intr, sc, &sc->sc_ih);
711173362Sbenjsc	if (error != 0) {
712173362Sbenjsc		device_printf(dev, "could not set up interrupt\n");
713173362Sbenjsc		goto fail;
714173362Sbenjsc	}
715173362Sbenjsc
716177043Sthompsa	if (bootverbose)
717177043Sthompsa		ieee80211_announce(ic);
718173362Sbenjsc#ifdef XXX_DEBUG
719173362Sbenjsc	ieee80211_announce_channels(ic);
720173362Sbenjsc#endif
721173362Sbenjsc	return 0;
722173362Sbenjsc
723173362Sbenjscfail:	wpi_detach(dev);
724173362Sbenjsc	return ENXIO;
725173362Sbenjsc}
726173362Sbenjsc
727173362Sbenjscstatic int
728173362Sbenjscwpi_detach(device_t dev)
729173362Sbenjsc{
730173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
731178354Ssam	struct ifnet *ifp = sc->sc_ifp;
732178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
733173362Sbenjsc	int ac;
734173362Sbenjsc
735173362Sbenjsc	if (ifp != NULL) {
736173362Sbenjsc		wpi_stop(sc);
737173362Sbenjsc		callout_drain(&sc->watchdog_to);
738173362Sbenjsc		callout_drain(&sc->calib_to);
739173362Sbenjsc		bpfdetach(ifp);
740173362Sbenjsc		ieee80211_ifdetach(ic);
741173362Sbenjsc	}
742173362Sbenjsc
743173362Sbenjsc	WPI_LOCK(sc);
744173362Sbenjsc	if (sc->txq[0].data_dmat) {
745173362Sbenjsc		for (ac = 0; ac < WME_NUM_AC; ac++)
746173362Sbenjsc			wpi_free_tx_ring(sc, &sc->txq[ac]);
747173362Sbenjsc
748173362Sbenjsc		wpi_free_tx_ring(sc, &sc->cmdq);
749173362Sbenjsc		wpi_free_rx_ring(sc, &sc->rxq);
750173362Sbenjsc		wpi_free_shared(sc);
751173362Sbenjsc	}
752173362Sbenjsc
753173362Sbenjsc	if (sc->fw_fp != NULL) {
754173362Sbenjsc		wpi_unload_firmware(sc);
755173362Sbenjsc	}
756173362Sbenjsc
757173362Sbenjsc	if (sc->fw_dma.tag)
758173362Sbenjsc		wpi_free_fwmem(sc);
759173362Sbenjsc	WPI_UNLOCK(sc);
760173362Sbenjsc
761173362Sbenjsc	if (sc->irq != NULL) {
762173362Sbenjsc		bus_teardown_intr(dev, sc->irq, sc->sc_ih);
763173362Sbenjsc		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
764173362Sbenjsc	}
765173362Sbenjsc
766173362Sbenjsc	if (sc->mem != NULL)
767173362Sbenjsc		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
768173362Sbenjsc
769173362Sbenjsc	if (ifp != NULL)
770173362Sbenjsc		if_free(ifp);
771173362Sbenjsc
772173362Sbenjsc	taskqueue_free(sc->sc_tq);
773173362Sbenjsc
774173362Sbenjsc	WPI_LOCK_DESTROY(sc);
775173362Sbenjsc	WPI_CMD_LOCK_DESTROY(sc);
776173362Sbenjsc
777173362Sbenjsc	return 0;
778173362Sbenjsc}
779173362Sbenjsc
780178354Ssamstatic struct ieee80211vap *
781178354Ssamwpi_vap_create(struct ieee80211com *ic,
782178354Ssam	const char name[IFNAMSIZ], int unit, int opmode, int flags,
783178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN],
784178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN])
785178354Ssam{
786178354Ssam	struct wpi_vap *wvp;
787178354Ssam	struct ieee80211vap *vap;
788178354Ssam
789178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
790178354Ssam		return NULL;
791178354Ssam	wvp = (struct wpi_vap *) malloc(sizeof(struct wpi_vap),
792178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
793178354Ssam	if (wvp == NULL)
794178354Ssam		return NULL;
795178354Ssam	vap = &wvp->vap;
796178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
797178354Ssam	/* override with driver methods */
798178354Ssam	wvp->newstate = vap->iv_newstate;
799178354Ssam	vap->iv_newstate = wpi_newstate;
800178354Ssam
801178354Ssam	ieee80211_amrr_init(&wvp->amrr, vap,
802178354Ssam	    IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
803178354Ssam	    IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
804178354Ssam	    500 /*ms*/);
805178354Ssam
806178354Ssam	/* complete setup */
807178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
808178354Ssam	ic->ic_opmode = opmode;
809178354Ssam	return vap;
810178354Ssam}
811178354Ssam
812173362Sbenjscstatic void
813178354Ssamwpi_vap_delete(struct ieee80211vap *vap)
814178354Ssam{
815178354Ssam	struct wpi_vap *wvp = WPI_VAP(vap);
816178354Ssam
817178354Ssam	ieee80211_amrr_cleanup(&wvp->amrr);
818178354Ssam	ieee80211_vap_detach(vap);
819178354Ssam	free(wvp, M_80211_VAP);
820178354Ssam}
821178354Ssam
822178354Ssamstatic void
823173362Sbenjscwpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
824173362Sbenjsc{
825173362Sbenjsc	if (error != 0)
826173362Sbenjsc		return;
827173362Sbenjsc
828173362Sbenjsc	KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
829173362Sbenjsc
830173362Sbenjsc	*(bus_addr_t *)arg = segs[0].ds_addr;
831173362Sbenjsc}
832173362Sbenjsc
833177043Sthompsa/*
834177043Sthompsa * Allocates a contiguous block of dma memory of the requested size and
835177043Sthompsa * alignment. Due to limitations of the FreeBSD dma subsystem as of 20071217,
836177043Sthompsa * allocations greater than 4096 may fail. Hence if the requested alignment is
837177043Sthompsa * greater we allocate 'alignment' size extra memory and shift the vaddr and
838177043Sthompsa * paddr after the dma load. This bypasses the problem at the cost of a little
839177043Sthompsa * more memory.
840177043Sthompsa */
841173362Sbenjscstatic int
842173362Sbenjscwpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
843173362Sbenjsc    void **kvap, bus_size_t size, bus_size_t alignment, int flags)
844173362Sbenjsc{
845173362Sbenjsc	int error;
846177043Sthompsa	bus_size_t align;
847177043Sthompsa	bus_size_t reqsize;
848173362Sbenjsc
849173362Sbenjsc	DPRINTFN(WPI_DEBUG_DMA,
850177043Sthompsa	    ("Size: %zd - alignment %zd\n", size, alignment));
851173362Sbenjsc
852173362Sbenjsc	dma->size = size;
853173362Sbenjsc	dma->tag = NULL;
854173362Sbenjsc
855177043Sthompsa	if (alignment > 4096) {
856177043Sthompsa		align = PAGE_SIZE;
857177043Sthompsa		reqsize = size + alignment;
858177043Sthompsa	} else {
859177043Sthompsa		align = alignment;
860177043Sthompsa		reqsize = size;
861177043Sthompsa	}
862177043Sthompsa	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), align,
863173362Sbenjsc	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
864177043Sthompsa	    NULL, NULL, reqsize,
865177043Sthompsa	    1, reqsize, flags,
866173362Sbenjsc	    NULL, NULL, &dma->tag);
867173362Sbenjsc	if (error != 0) {
868173362Sbenjsc		device_printf(sc->sc_dev,
869173362Sbenjsc		    "could not create shared page DMA tag\n");
870173362Sbenjsc		goto fail;
871173362Sbenjsc	}
872177043Sthompsa	error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr_start,
873173362Sbenjsc	    flags | BUS_DMA_ZERO, &dma->map);
874173362Sbenjsc	if (error != 0) {
875173362Sbenjsc		device_printf(sc->sc_dev,
876173362Sbenjsc		    "could not allocate shared page DMA memory\n");
877173362Sbenjsc		goto fail;
878173362Sbenjsc	}
879173362Sbenjsc
880177043Sthompsa	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr_start,
881177043Sthompsa	    reqsize,  wpi_dma_map_addr, &dma->paddr_start, flags);
882177043Sthompsa
883177043Sthompsa	/* Save the original pointers so we can free all the memory */
884177043Sthompsa	dma->paddr = dma->paddr_start;
885177043Sthompsa	dma->vaddr = dma->vaddr_start;
886177043Sthompsa
887177043Sthompsa	/*
888177043Sthompsa	 * Check the alignment and increment by 4096 until we get the
889177043Sthompsa	 * requested alignment. Fail if can't obtain the alignment
890177043Sthompsa	 * we requested.
891173362Sbenjsc	 */
892177043Sthompsa	if ((dma->paddr & (alignment -1 )) != 0) {
893177043Sthompsa		int i;
894173362Sbenjsc
895177043Sthompsa		for (i = 0; i < alignment / 4096; i++) {
896177043Sthompsa			if ((dma->paddr & (alignment - 1 )) == 0)
897177043Sthompsa				break;
898177043Sthompsa			dma->paddr += 4096;
899177043Sthompsa			dma->vaddr += 4096;
900177043Sthompsa		}
901177043Sthompsa		if (i == alignment / 4096) {
902177043Sthompsa			device_printf(sc->sc_dev,
903177043Sthompsa			    "alignment requirement was not satisfied\n");
904177043Sthompsa			goto fail;
905177043Sthompsa		}
906173362Sbenjsc	}
907173362Sbenjsc
908173362Sbenjsc	if (error != 0) {
909173362Sbenjsc		device_printf(sc->sc_dev,
910173362Sbenjsc		    "could not load shared page DMA map\n");
911173362Sbenjsc		goto fail;
912173362Sbenjsc	}
913173362Sbenjsc
914173362Sbenjsc	if (kvap != NULL)
915173362Sbenjsc		*kvap = dma->vaddr;
916173362Sbenjsc
917173362Sbenjsc	return 0;
918173362Sbenjsc
919173362Sbenjscfail:
920173362Sbenjsc	wpi_dma_contig_free(dma);
921173362Sbenjsc	return error;
922173362Sbenjsc}
923173362Sbenjsc
924173362Sbenjscstatic void
925173362Sbenjscwpi_dma_contig_free(struct wpi_dma_info *dma)
926173362Sbenjsc{
927173362Sbenjsc	if (dma->tag) {
928173362Sbenjsc		if (dma->map != NULL) {
929177043Sthompsa			if (dma->paddr_start != 0) {
930173362Sbenjsc				bus_dmamap_sync(dma->tag, dma->map,
931173362Sbenjsc				    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
932173362Sbenjsc				bus_dmamap_unload(dma->tag, dma->map);
933173362Sbenjsc			}
934177043Sthompsa			bus_dmamem_free(dma->tag, &dma->vaddr_start, dma->map);
935173362Sbenjsc		}
936173362Sbenjsc		bus_dma_tag_destroy(dma->tag);
937173362Sbenjsc	}
938173362Sbenjsc}
939173362Sbenjsc
940173362Sbenjsc/*
941173362Sbenjsc * Allocate a shared page between host and NIC.
942173362Sbenjsc */
943173362Sbenjscstatic int
944173362Sbenjscwpi_alloc_shared(struct wpi_softc *sc)
945173362Sbenjsc{
946173362Sbenjsc	int error;
947173362Sbenjsc
948173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &sc->shared_dma,
949173362Sbenjsc	    (void **)&sc->shared, sizeof (struct wpi_shared),
950173362Sbenjsc	    PAGE_SIZE,
951173362Sbenjsc	    BUS_DMA_NOWAIT);
952173362Sbenjsc
953173362Sbenjsc	if (error != 0) {
954173362Sbenjsc		device_printf(sc->sc_dev,
955173362Sbenjsc		    "could not allocate shared area DMA memory\n");
956173362Sbenjsc	}
957173362Sbenjsc
958173362Sbenjsc	return error;
959173362Sbenjsc}
960173362Sbenjsc
961173362Sbenjscstatic void
962173362Sbenjscwpi_free_shared(struct wpi_softc *sc)
963173362Sbenjsc{
964173362Sbenjsc	wpi_dma_contig_free(&sc->shared_dma);
965173362Sbenjsc}
966173362Sbenjsc
967173362Sbenjscstatic int
968173362Sbenjscwpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
969173362Sbenjsc{
970173362Sbenjsc
971173362Sbenjsc	int i, error;
972173362Sbenjsc
973173362Sbenjsc	ring->cur = 0;
974173362Sbenjsc
975173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
976177043Sthompsa	    (void **)&ring->desc, WPI_RX_RING_COUNT * sizeof (uint32_t),
977177043Sthompsa	    WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
978173362Sbenjsc
979173362Sbenjsc	if (error != 0) {
980177043Sthompsa		device_printf(sc->sc_dev,
981177043Sthompsa		    "%s: could not allocate rx ring DMA memory, error %d\n",
982177043Sthompsa		    __func__, error);
983177043Sthompsa		goto fail;
984173362Sbenjsc	}
985173362Sbenjsc
986177043Sthompsa        error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
987177043Sthompsa	    BUS_SPACE_MAXADDR_32BIT,
988177043Sthompsa            BUS_SPACE_MAXADDR, NULL, NULL, MJUMPAGESIZE, 1,
989177043Sthompsa            MJUMPAGESIZE, BUS_DMA_NOWAIT, NULL, NULL, &ring->data_dmat);
990177043Sthompsa        if (error != 0) {
991177043Sthompsa                device_printf(sc->sc_dev,
992177043Sthompsa		    "%s: bus_dma_tag_create_failed, error %d\n",
993177043Sthompsa		    __func__, error);
994177043Sthompsa                goto fail;
995177043Sthompsa        }
996177043Sthompsa
997173362Sbenjsc	/*
998177043Sthompsa	 * Setup Rx buffers.
999173362Sbenjsc	 */
1000173362Sbenjsc	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1001177043Sthompsa		struct wpi_rx_data *data = &ring->data[i];
1002177043Sthompsa		struct mbuf *m;
1003177043Sthompsa		bus_addr_t paddr;
1004173362Sbenjsc
1005177043Sthompsa		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1006177043Sthompsa		if (error != 0) {
1007173362Sbenjsc			device_printf(sc->sc_dev,
1008177043Sthompsa			    "%s: bus_dmamap_create failed, error %d\n",
1009177043Sthompsa			    __func__, error);
1010173362Sbenjsc			goto fail;
1011173362Sbenjsc		}
1012177043Sthompsa		m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1013177043Sthompsa		if (m == NULL) {
1014173362Sbenjsc			device_printf(sc->sc_dev,
1015177043Sthompsa			   "%s: could not allocate rx mbuf\n", __func__);
1016177043Sthompsa			error = ENOMEM;
1017173362Sbenjsc			goto fail;
1018173362Sbenjsc		}
1019177043Sthompsa		/* map page */
1020177043Sthompsa		error = bus_dmamap_load(ring->data_dmat, data->map,
1021177043Sthompsa		    mtod(m, caddr_t), MJUMPAGESIZE,
1022177043Sthompsa		    wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1023177043Sthompsa		if (error != 0 && error != EFBIG) {
1024177043Sthompsa			device_printf(sc->sc_dev,
1025177043Sthompsa			    "%s: bus_dmamap_load failed, error %d\n",
1026177043Sthompsa			    __func__, error);
1027177043Sthompsa			m_freem(m);
1028177043Sthompsa			error = ENOMEM;	/* XXX unique code */
1029173362Sbenjsc			goto fail;
1030173362Sbenjsc		}
1031177043Sthompsa		bus_dmamap_sync(ring->data_dmat, data->map,
1032177043Sthompsa		    BUS_DMASYNC_PREWRITE);
1033177043Sthompsa
1034177043Sthompsa		data->m = m;
1035177043Sthompsa		ring->desc[i] = htole32(paddr);
1036173362Sbenjsc	}
1037173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1038173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
1039173362Sbenjsc	return 0;
1040173362Sbenjscfail:
1041173362Sbenjsc	wpi_free_rx_ring(sc, ring);
1042173362Sbenjsc	return error;
1043173362Sbenjsc}
1044173362Sbenjsc
1045173362Sbenjscstatic void
1046173362Sbenjscwpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
1047173362Sbenjsc{
1048173362Sbenjsc	int ntries;
1049173362Sbenjsc
1050173362Sbenjsc	wpi_mem_lock(sc);
1051173362Sbenjsc
1052173362Sbenjsc	WPI_WRITE(sc, WPI_RX_CONFIG, 0);
1053173362Sbenjsc
1054173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1055173362Sbenjsc		if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE)
1056173362Sbenjsc			break;
1057173362Sbenjsc		DELAY(10);
1058173362Sbenjsc	}
1059173362Sbenjsc
1060173362Sbenjsc	wpi_mem_unlock(sc);
1061173362Sbenjsc
1062173362Sbenjsc#ifdef WPI_DEBUG
1063179957Sthompsa	if (ntries == 100 && wpi_debug > 0)
1064173362Sbenjsc		device_printf(sc->sc_dev, "timeout resetting Rx ring\n");
1065173362Sbenjsc#endif
1066173362Sbenjsc
1067173362Sbenjsc	ring->cur = 0;
1068173362Sbenjsc}
1069173362Sbenjsc
1070173362Sbenjscstatic void
1071173362Sbenjscwpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
1072173362Sbenjsc{
1073173362Sbenjsc	int i;
1074173362Sbenjsc
1075173362Sbenjsc	wpi_dma_contig_free(&ring->desc_dma);
1076173362Sbenjsc
1077177043Sthompsa	for (i = 0; i < WPI_RX_RING_COUNT; i++)
1078177043Sthompsa		if (ring->data[i].m != NULL)
1079173362Sbenjsc			m_freem(ring->data[i].m);
1080173362Sbenjsc}
1081173362Sbenjsc
1082173362Sbenjscstatic int
1083173362Sbenjscwpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count,
1084173362Sbenjsc	int qid)
1085173362Sbenjsc{
1086173362Sbenjsc	struct wpi_tx_data *data;
1087173362Sbenjsc	int i, error;
1088173362Sbenjsc
1089173362Sbenjsc	ring->qid = qid;
1090173362Sbenjsc	ring->count = count;
1091173362Sbenjsc	ring->queued = 0;
1092173362Sbenjsc	ring->cur = 0;
1093173362Sbenjsc	ring->data = NULL;
1094173362Sbenjsc
1095173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
1096173362Sbenjsc		(void **)&ring->desc, count * sizeof (struct wpi_tx_desc),
1097173362Sbenjsc		WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
1098173362Sbenjsc
1099173362Sbenjsc	if (error != 0) {
1100173362Sbenjsc	    device_printf(sc->sc_dev, "could not allocate tx dma memory\n");
1101173362Sbenjsc	    goto fail;
1102173362Sbenjsc	}
1103173362Sbenjsc
1104173362Sbenjsc	/* update shared page with ring's base address */
1105173362Sbenjsc	sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
1106173362Sbenjsc
1107173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
1108173362Sbenjsc		count * sizeof (struct wpi_tx_cmd), WPI_RING_DMA_ALIGN,
1109173362Sbenjsc		BUS_DMA_NOWAIT);
1110173362Sbenjsc
1111173362Sbenjsc	if (error != 0) {
1112173362Sbenjsc		device_printf(sc->sc_dev,
1113173362Sbenjsc		    "could not allocate tx command DMA memory\n");
1114173362Sbenjsc		goto fail;
1115173362Sbenjsc	}
1116173362Sbenjsc
1117173362Sbenjsc	ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF,
1118173362Sbenjsc	    M_NOWAIT | M_ZERO);
1119173362Sbenjsc	if (ring->data == NULL) {
1120173362Sbenjsc		device_printf(sc->sc_dev,
1121173362Sbenjsc		    "could not allocate tx data slots\n");
1122173362Sbenjsc		goto fail;
1123173362Sbenjsc	}
1124173362Sbenjsc
1125173362Sbenjsc	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1126173362Sbenjsc	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
1127173362Sbenjsc	    WPI_MAX_SCATTER - 1, MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL,
1128173362Sbenjsc	    &ring->data_dmat);
1129173362Sbenjsc	if (error != 0) {
1130173362Sbenjsc		device_printf(sc->sc_dev, "could not create data DMA tag\n");
1131173362Sbenjsc		goto fail;
1132173362Sbenjsc	}
1133173362Sbenjsc
1134173362Sbenjsc	for (i = 0; i < count; i++) {
1135173362Sbenjsc		data = &ring->data[i];
1136173362Sbenjsc
1137173362Sbenjsc		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1138173362Sbenjsc		if (error != 0) {
1139173362Sbenjsc			device_printf(sc->sc_dev,
1140173362Sbenjsc			    "could not create tx buf DMA map\n");
1141173362Sbenjsc			goto fail;
1142173362Sbenjsc		}
1143173362Sbenjsc		bus_dmamap_sync(ring->data_dmat, data->map,
1144173362Sbenjsc		    BUS_DMASYNC_PREWRITE);
1145173362Sbenjsc	}
1146173362Sbenjsc
1147173362Sbenjsc	return 0;
1148173362Sbenjsc
1149177043Sthompsafail:
1150177043Sthompsa	wpi_free_tx_ring(sc, ring);
1151173362Sbenjsc	return error;
1152173362Sbenjsc}
1153173362Sbenjsc
1154173362Sbenjscstatic void
1155173362Sbenjscwpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1156173362Sbenjsc{
1157173362Sbenjsc	struct wpi_tx_data *data;
1158173362Sbenjsc	int i, ntries;
1159173362Sbenjsc
1160173362Sbenjsc	wpi_mem_lock(sc);
1161173362Sbenjsc
1162173362Sbenjsc	WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0);
1163173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1164173362Sbenjsc		if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid))
1165173362Sbenjsc			break;
1166173362Sbenjsc		DELAY(10);
1167173362Sbenjsc	}
1168173362Sbenjsc#ifdef WPI_DEBUG
1169179957Sthompsa	if (ntries == 100 && wpi_debug > 0)
1170173362Sbenjsc		device_printf(sc->sc_dev, "timeout resetting Tx ring %d\n",
1171173362Sbenjsc		    ring->qid);
1172173362Sbenjsc#endif
1173173362Sbenjsc	wpi_mem_unlock(sc);
1174173362Sbenjsc
1175173362Sbenjsc	for (i = 0; i < ring->count; i++) {
1176173362Sbenjsc		data = &ring->data[i];
1177173362Sbenjsc
1178173362Sbenjsc		if (data->m != NULL) {
1179173362Sbenjsc			bus_dmamap_unload(ring->data_dmat, data->map);
1180173362Sbenjsc			m_freem(data->m);
1181173362Sbenjsc			data->m = NULL;
1182173362Sbenjsc		}
1183173362Sbenjsc	}
1184173362Sbenjsc
1185173362Sbenjsc	ring->queued = 0;
1186173362Sbenjsc	ring->cur = 0;
1187173362Sbenjsc}
1188173362Sbenjsc
1189173362Sbenjscstatic void
1190173362Sbenjscwpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1191173362Sbenjsc{
1192173362Sbenjsc	struct wpi_tx_data *data;
1193173362Sbenjsc	int i;
1194173362Sbenjsc
1195173362Sbenjsc	wpi_dma_contig_free(&ring->desc_dma);
1196173362Sbenjsc	wpi_dma_contig_free(&ring->cmd_dma);
1197173362Sbenjsc
1198173362Sbenjsc	if (ring->data != NULL) {
1199173362Sbenjsc		for (i = 0; i < ring->count; i++) {
1200173362Sbenjsc			data = &ring->data[i];
1201173362Sbenjsc
1202173362Sbenjsc			if (data->m != NULL) {
1203173362Sbenjsc				bus_dmamap_sync(ring->data_dmat, data->map,
1204173362Sbenjsc				    BUS_DMASYNC_POSTWRITE);
1205173362Sbenjsc				bus_dmamap_unload(ring->data_dmat, data->map);
1206173362Sbenjsc				m_freem(data->m);
1207173362Sbenjsc				data->m = NULL;
1208173362Sbenjsc			}
1209173362Sbenjsc		}
1210173362Sbenjsc		free(ring->data, M_DEVBUF);
1211173362Sbenjsc	}
1212173362Sbenjsc
1213173362Sbenjsc	if (ring->data_dmat != NULL)
1214173362Sbenjsc		bus_dma_tag_destroy(ring->data_dmat);
1215173362Sbenjsc}
1216173362Sbenjsc
1217173362Sbenjscstatic int
1218173362Sbenjscwpi_shutdown(device_t dev)
1219173362Sbenjsc{
1220173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1221173362Sbenjsc
1222173362Sbenjsc	WPI_LOCK(sc);
1223173362Sbenjsc	wpi_stop_locked(sc);
1224173362Sbenjsc	wpi_unload_firmware(sc);
1225173362Sbenjsc	WPI_UNLOCK(sc);
1226173362Sbenjsc
1227173362Sbenjsc	return 0;
1228173362Sbenjsc}
1229173362Sbenjsc
1230173362Sbenjscstatic int
1231173362Sbenjscwpi_suspend(device_t dev)
1232173362Sbenjsc{
1233173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1234173362Sbenjsc
1235173362Sbenjsc	wpi_stop(sc);
1236173362Sbenjsc	return 0;
1237173362Sbenjsc}
1238173362Sbenjsc
1239173362Sbenjscstatic int
1240173362Sbenjscwpi_resume(device_t dev)
1241173362Sbenjsc{
1242173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1243178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1244173362Sbenjsc
1245173362Sbenjsc	pci_write_config(dev, 0x41, 0, 1);
1246173362Sbenjsc
1247173362Sbenjsc	if (ifp->if_flags & IFF_UP) {
1248173362Sbenjsc		wpi_init(ifp->if_softc);
1249173362Sbenjsc		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1250173362Sbenjsc			wpi_start(ifp);
1251173362Sbenjsc	}
1252173362Sbenjsc	return 0;
1253173362Sbenjsc}
1254173362Sbenjsc
1255173362Sbenjsc/* ARGSUSED */
1256173362Sbenjscstatic struct ieee80211_node *
1257179643Ssamwpi_node_alloc(struct ieee80211vap *vap __unused,
1258179643Ssam	const uint8_t mac[IEEE80211_ADDR_LEN] __unused)
1259173362Sbenjsc{
1260173362Sbenjsc	struct wpi_node *wn;
1261173362Sbenjsc
1262178354Ssam	wn = malloc(sizeof (struct wpi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
1263173362Sbenjsc
1264173362Sbenjsc	return &wn->ni;
1265173362Sbenjsc}
1266173362Sbenjsc
1267173362Sbenjsc/**
1268173362Sbenjsc * Called by net80211 when ever there is a change to 80211 state machine
1269173362Sbenjsc */
1270173362Sbenjscstatic int
1271178354Ssamwpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1272173362Sbenjsc{
1273178354Ssam	struct wpi_vap *wvp = WPI_VAP(vap);
1274178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1275173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
1276173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
1277178354Ssam	int error;
1278173362Sbenjsc
1279178354Ssam	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
1280178354Ssam		ieee80211_state_name[vap->iv_state],
1281178354Ssam		ieee80211_state_name[nstate], sc->flags));
1282173362Sbenjsc
1283178354Ssam	if (nstate == IEEE80211_S_AUTH) {
1284177043Sthompsa		/* Delay the auth transition until we can update the firmware */
1285178354Ssam		error = wpi_queue_cmd(sc, WPI_AUTH, arg, WPI_QUEUE_NORMAL);
1286178354Ssam		return (error != 0 ? error : EINPROGRESS);
1287173362Sbenjsc	}
1288178354Ssam	if (nstate == IEEE80211_S_RUN && vap->iv_state != IEEE80211_S_RUN) {
1289178354Ssam		/* set the association id first */
1290178354Ssam		error = wpi_queue_cmd(sc, WPI_RUN, arg, WPI_QUEUE_NORMAL);
1291178354Ssam		return (error != 0 ? error : EINPROGRESS);
1292178354Ssam	}
1293178354Ssam	if (nstate == IEEE80211_S_RUN) {
1294178354Ssam		/* RUN -> RUN transition; just restart the timers */
1295178354Ssam		wpi_calib_timeout(sc);
1296178354Ssam		/* XXX split out rate control timer */
1297178354Ssam	}
1298178354Ssam	return wvp->newstate(vap, nstate, arg);
1299173362Sbenjsc}
1300173362Sbenjsc
1301173362Sbenjsc/*
1302173362Sbenjsc * Grab exclusive access to NIC memory.
1303173362Sbenjsc */
1304173362Sbenjscstatic void
1305173362Sbenjscwpi_mem_lock(struct wpi_softc *sc)
1306173362Sbenjsc{
1307173362Sbenjsc	int ntries;
1308173362Sbenjsc	uint32_t tmp;
1309173362Sbenjsc
1310173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
1311173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC);
1312173362Sbenjsc
1313173362Sbenjsc	/* spin until we actually get the lock */
1314173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1315173362Sbenjsc		if ((WPI_READ(sc, WPI_GPIO_CTL) &
1316173362Sbenjsc			(WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK)
1317173362Sbenjsc			break;
1318173362Sbenjsc		DELAY(10);
1319173362Sbenjsc	}
1320173362Sbenjsc	if (ntries == 100)
1321173362Sbenjsc		device_printf(sc->sc_dev, "could not lock memory\n");
1322173362Sbenjsc}
1323173362Sbenjsc
1324173362Sbenjsc/*
1325173362Sbenjsc * Release lock on NIC memory.
1326173362Sbenjsc */
1327173362Sbenjscstatic void
1328173362Sbenjscwpi_mem_unlock(struct wpi_softc *sc)
1329173362Sbenjsc{
1330173362Sbenjsc	uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL);
1331173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC);
1332173362Sbenjsc}
1333173362Sbenjsc
1334173362Sbenjscstatic uint32_t
1335173362Sbenjscwpi_mem_read(struct wpi_softc *sc, uint16_t addr)
1336173362Sbenjsc{
1337173362Sbenjsc	WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr);
1338173362Sbenjsc	return WPI_READ(sc, WPI_READ_MEM_DATA);
1339173362Sbenjsc}
1340173362Sbenjsc
1341173362Sbenjscstatic void
1342173362Sbenjscwpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data)
1343173362Sbenjsc{
1344173362Sbenjsc	WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr);
1345173362Sbenjsc	WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data);
1346173362Sbenjsc}
1347173362Sbenjsc
1348173362Sbenjscstatic void
1349173362Sbenjscwpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr,
1350173362Sbenjsc    const uint32_t *data, int wlen)
1351173362Sbenjsc{
1352173362Sbenjsc	for (; wlen > 0; wlen--, data++, addr+=4)
1353173362Sbenjsc		wpi_mem_write(sc, addr, *data);
1354173362Sbenjsc}
1355173362Sbenjsc
1356173362Sbenjsc/*
1357173362Sbenjsc * Read data from the EEPROM.  We access EEPROM through the MAC instead of
1358173362Sbenjsc * using the traditional bit-bang method. Data is read up until len bytes have
1359173362Sbenjsc * been obtained.
1360173362Sbenjsc */
1361173362Sbenjscstatic uint16_t
1362173362Sbenjscwpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int len)
1363173362Sbenjsc{
1364173362Sbenjsc	int ntries;
1365173362Sbenjsc	uint32_t val;
1366173362Sbenjsc	uint8_t *out = data;
1367173362Sbenjsc
1368173362Sbenjsc	wpi_mem_lock(sc);
1369173362Sbenjsc
1370173362Sbenjsc	for (; len > 0; len -= 2, addr++) {
1371173362Sbenjsc		WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2);
1372173362Sbenjsc
1373173362Sbenjsc		for (ntries = 0; ntries < 10; ntries++) {
1374173362Sbenjsc			if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) & WPI_EEPROM_READY)
1375173362Sbenjsc				break;
1376173362Sbenjsc			DELAY(5);
1377173362Sbenjsc		}
1378173362Sbenjsc
1379173362Sbenjsc		if (ntries == 10) {
1380173362Sbenjsc			device_printf(sc->sc_dev, "could not read EEPROM\n");
1381173362Sbenjsc			return ETIMEDOUT;
1382173362Sbenjsc		}
1383173362Sbenjsc
1384173362Sbenjsc		*out++= val >> 16;
1385173362Sbenjsc		if (len > 1)
1386173362Sbenjsc			*out ++= val >> 24;
1387173362Sbenjsc	}
1388173362Sbenjsc
1389173362Sbenjsc	wpi_mem_unlock(sc);
1390173362Sbenjsc
1391173362Sbenjsc	return 0;
1392173362Sbenjsc}
1393173362Sbenjsc
1394173362Sbenjsc/*
1395173362Sbenjsc * The firmware text and data segments are transferred to the NIC using DMA.
1396173362Sbenjsc * The driver just copies the firmware into DMA-safe memory and tells the NIC
1397173362Sbenjsc * where to find it.  Once the NIC has copied the firmware into its internal
1398173362Sbenjsc * memory, we can free our local copy in the driver.
1399173362Sbenjsc */
1400173362Sbenjscstatic int
1401173362Sbenjscwpi_load_microcode(struct wpi_softc *sc, const uint8_t *fw, int size)
1402173362Sbenjsc{
1403173362Sbenjsc	int error, ntries;
1404173362Sbenjsc
1405173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,("Loading microcode  size 0x%x\n", size));
1406173362Sbenjsc
1407173362Sbenjsc	size /= sizeof(uint32_t);
1408173362Sbenjsc
1409173362Sbenjsc	wpi_mem_lock(sc);
1410173362Sbenjsc
1411173362Sbenjsc	wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE,
1412173362Sbenjsc	    (const uint32_t *)fw, size);
1413173362Sbenjsc
1414173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0);
1415173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT);
1416173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size);
1417173362Sbenjsc
1418173362Sbenjsc	/* run microcode */
1419173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN);
1420173362Sbenjsc
1421173362Sbenjsc	/* wait while the adapter is busy copying the firmware */
1422173362Sbenjsc	for (error = 0, ntries = 0; ntries < 1000; ntries++) {
1423173362Sbenjsc		uint32_t status = WPI_READ(sc, WPI_TX_STATUS);
1424173362Sbenjsc		DPRINTFN(WPI_DEBUG_HW,
1425173362Sbenjsc		    ("firmware status=0x%x, val=0x%x, result=0x%x\n", status,
1426173362Sbenjsc		     WPI_TX_IDLE(6), status & WPI_TX_IDLE(6)));
1427173362Sbenjsc		if (status & WPI_TX_IDLE(6)) {
1428173362Sbenjsc			DPRINTFN(WPI_DEBUG_HW,
1429173362Sbenjsc			    ("Status Match! - ntries = %d\n", ntries));
1430173362Sbenjsc			break;
1431173362Sbenjsc		}
1432173362Sbenjsc		DELAY(10);
1433173362Sbenjsc	}
1434173362Sbenjsc	if (ntries == 1000) {
1435173362Sbenjsc		device_printf(sc->sc_dev, "timeout transferring firmware\n");
1436173362Sbenjsc		error = ETIMEDOUT;
1437173362Sbenjsc	}
1438173362Sbenjsc
1439173362Sbenjsc	/* start the microcode executing */
1440173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_ENABLE);
1441173362Sbenjsc
1442173362Sbenjsc	wpi_mem_unlock(sc);
1443173362Sbenjsc
1444173362Sbenjsc	return (error);
1445173362Sbenjsc}
1446173362Sbenjsc
1447173362Sbenjscstatic void
1448173362Sbenjscwpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1449173362Sbenjsc	struct wpi_rx_data *data)
1450173362Sbenjsc{
1451178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1452178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1453173362Sbenjsc	struct wpi_rx_ring *ring = &sc->rxq;
1454173362Sbenjsc	struct wpi_rx_stat *stat;
1455173362Sbenjsc	struct wpi_rx_head *head;
1456173362Sbenjsc	struct wpi_rx_tail *tail;
1457173362Sbenjsc	struct ieee80211_node *ni;
1458173362Sbenjsc	struct mbuf *m, *mnew;
1459177043Sthompsa	bus_addr_t paddr;
1460177043Sthompsa	int error;
1461173362Sbenjsc
1462173362Sbenjsc	stat = (struct wpi_rx_stat *)(desc + 1);
1463173362Sbenjsc
1464173362Sbenjsc	if (stat->len > WPI_STAT_MAXLEN) {
1465173362Sbenjsc		device_printf(sc->sc_dev, "invalid rx statistic header\n");
1466173362Sbenjsc		ifp->if_ierrors++;
1467173362Sbenjsc		return;
1468173362Sbenjsc	}
1469173362Sbenjsc
1470173362Sbenjsc	head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1471173362Sbenjsc	tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + le16toh(head->len));
1472173362Sbenjsc
1473173362Sbenjsc	DPRINTFN(WPI_DEBUG_RX, ("rx intr: idx=%d len=%d stat len=%d rssi=%d "
1474173362Sbenjsc	    "rate=%x chan=%d tstamp=%ju\n", ring->cur, le32toh(desc->len),
1475173362Sbenjsc	    le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan,
1476173362Sbenjsc	    (uintmax_t)le64toh(tail->tstamp)));
1477173362Sbenjsc
1478190458Sjmallett	/* discard Rx frames with bad CRC early */
1479190458Sjmallett	if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1480190458Sjmallett		DPRINTFN(WPI_DEBUG_RX, ("%s: rx flags error %x\n", __func__,
1481190458Sjmallett		    le32toh(tail->flags)));
1482190458Sjmallett		ifp->if_ierrors++;
1483190458Sjmallett		return;
1484190458Sjmallett	}
1485190458Sjmallett	if (le16toh(head->len) < sizeof (struct ieee80211_frame)) {
1486190458Sjmallett		DPRINTFN(WPI_DEBUG_RX, ("%s: frame too short: %d\n", __func__,
1487190458Sjmallett		    le16toh(head->len)));
1488190458Sjmallett		ifp->if_ierrors++;
1489190458Sjmallett		return;
1490190458Sjmallett	}
1491190458Sjmallett
1492177043Sthompsa	/* XXX don't need mbuf, just dma buffer */
1493177043Sthompsa	mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1494177043Sthompsa	if (mnew == NULL) {
1495177043Sthompsa		DPRINTFN(WPI_DEBUG_RX, ("%s: no mbuf to restock ring\n",
1496177043Sthompsa		    __func__));
1497177043Sthompsa		ifp->if_ierrors++;
1498177043Sthompsa		return;
1499177043Sthompsa	}
1500177043Sthompsa	error = bus_dmamap_load(ring->data_dmat, data->map,
1501177043Sthompsa	    mtod(mnew, caddr_t), MJUMPAGESIZE,
1502177043Sthompsa	    wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1503177043Sthompsa	if (error != 0 && error != EFBIG) {
1504177043Sthompsa		device_printf(sc->sc_dev,
1505177043Sthompsa		    "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1506177043Sthompsa		m_freem(mnew);
1507177043Sthompsa		ifp->if_ierrors++;
1508177043Sthompsa		return;
1509177043Sthompsa	}
1510177043Sthompsa	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1511177043Sthompsa
1512177043Sthompsa	/* finalize mbuf and swap in new one */
1513173362Sbenjsc	m = data->m;
1514173362Sbenjsc	m->m_pkthdr.rcvif = ifp;
1515173362Sbenjsc	m->m_data = (caddr_t)(head + 1);
1516173362Sbenjsc	m->m_pkthdr.len = m->m_len = le16toh(head->len);
1517173362Sbenjsc
1518177043Sthompsa	data->m = mnew;
1519177043Sthompsa	/* update Rx descriptor */
1520177043Sthompsa	ring->desc[ring->cur] = htole32(paddr);
1521173362Sbenjsc
1522178354Ssam	if (bpf_peers_present(ifp->if_bpf)) {
1523173362Sbenjsc		struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
1524173362Sbenjsc
1525173362Sbenjsc		tap->wr_flags = 0;
1526173362Sbenjsc		tap->wr_chan_freq =
1527173362Sbenjsc			htole16(ic->ic_channels[head->chan].ic_freq);
1528173362Sbenjsc		tap->wr_chan_flags =
1529173362Sbenjsc			htole16(ic->ic_channels[head->chan].ic_flags);
1530173362Sbenjsc		tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
1531173362Sbenjsc		tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise);
1532173362Sbenjsc		tap->wr_tsft = tail->tstamp;
1533173362Sbenjsc		tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
1534173362Sbenjsc		switch (head->rate) {
1535173362Sbenjsc		/* CCK rates */
1536173362Sbenjsc		case  10: tap->wr_rate =   2; break;
1537173362Sbenjsc		case  20: tap->wr_rate =   4; break;
1538173362Sbenjsc		case  55: tap->wr_rate =  11; break;
1539173362Sbenjsc		case 110: tap->wr_rate =  22; break;
1540173362Sbenjsc		/* OFDM rates */
1541173362Sbenjsc		case 0xd: tap->wr_rate =  12; break;
1542173362Sbenjsc		case 0xf: tap->wr_rate =  18; break;
1543173362Sbenjsc		case 0x5: tap->wr_rate =  24; break;
1544173362Sbenjsc		case 0x7: tap->wr_rate =  36; break;
1545173362Sbenjsc		case 0x9: tap->wr_rate =  48; break;
1546173362Sbenjsc		case 0xb: tap->wr_rate =  72; break;
1547173362Sbenjsc		case 0x1: tap->wr_rate =  96; break;
1548173362Sbenjsc		case 0x3: tap->wr_rate = 108; break;
1549173362Sbenjsc		/* unknown rate: should not happen */
1550173362Sbenjsc		default:  tap->wr_rate =   0;
1551173362Sbenjsc		}
1552173362Sbenjsc		if (le16toh(head->flags) & 0x4)
1553173362Sbenjsc			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1554173362Sbenjsc
1555178354Ssam		bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m);
1556173362Sbenjsc	}
1557173362Sbenjsc
1558173362Sbenjsc	WPI_UNLOCK(sc);
1559173362Sbenjsc
1560177043Sthompsa	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1561178354Ssam	if (ni != NULL) {
1562178354Ssam		(void) ieee80211_input(ni, m, stat->rssi, 0, 0);
1563178354Ssam		ieee80211_free_node(ni);
1564178354Ssam	} else
1565178354Ssam		(void) ieee80211_input_all(ic, m, stat->rssi, 0, 0);
1566173362Sbenjsc
1567173362Sbenjsc	WPI_LOCK(sc);
1568173362Sbenjsc}
1569173362Sbenjsc
1570173362Sbenjscstatic void
1571173362Sbenjscwpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1572173362Sbenjsc{
1573178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1574173362Sbenjsc	struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
1575173362Sbenjsc	struct wpi_tx_data *txdata = &ring->data[desc->idx];
1576173362Sbenjsc	struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
1577173362Sbenjsc	struct wpi_node *wn = (struct wpi_node *)txdata->ni;
1578173362Sbenjsc
1579173362Sbenjsc	DPRINTFN(WPI_DEBUG_TX, ("tx done: qid=%d idx=%d retries=%d nkill=%d "
1580173362Sbenjsc	    "rate=%x duration=%d status=%x\n", desc->qid, desc->idx,
1581173362Sbenjsc	    stat->ntries, stat->nkill, stat->rate, le32toh(stat->duration),
1582173362Sbenjsc	    le32toh(stat->status)));
1583173362Sbenjsc
1584173362Sbenjsc	/*
1585173362Sbenjsc	 * Update rate control statistics for the node.
1586173362Sbenjsc	 * XXX we should not count mgmt frames since they're always sent at
1587173362Sbenjsc	 * the lowest available bit-rate.
1588173362Sbenjsc	 * XXX frames w/o ACK shouldn't be used either
1589173362Sbenjsc	 */
1590173362Sbenjsc	wn->amn.amn_txcnt++;
1591173362Sbenjsc	if (stat->ntries > 0) {
1592190462Sjmallett		DPRINTFN(WPI_DEBUG_TX, ("%d retries\n", stat->ntries));
1593173362Sbenjsc		wn->amn.amn_retrycnt++;
1594173362Sbenjsc	}
1595173362Sbenjsc
1596173362Sbenjsc	/* XXX oerrors should only count errors !maxtries */
1597173362Sbenjsc	if ((le32toh(stat->status) & 0xff) != 1)
1598173362Sbenjsc		ifp->if_oerrors++;
1599173362Sbenjsc	else
1600173362Sbenjsc		ifp->if_opackets++;
1601173362Sbenjsc
1602173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, txdata->map, BUS_DMASYNC_POSTWRITE);
1603173362Sbenjsc	bus_dmamap_unload(ring->data_dmat, txdata->map);
1604173362Sbenjsc	/* XXX handle M_TXCB? */
1605173362Sbenjsc	m_freem(txdata->m);
1606173362Sbenjsc	txdata->m = NULL;
1607173362Sbenjsc	ieee80211_free_node(txdata->ni);
1608173362Sbenjsc	txdata->ni = NULL;
1609173362Sbenjsc
1610173362Sbenjsc	ring->queued--;
1611173362Sbenjsc
1612173362Sbenjsc	sc->sc_tx_timer = 0;
1613173362Sbenjsc	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1614178354Ssam	wpi_start_locked(ifp);
1615173362Sbenjsc}
1616173362Sbenjsc
1617173362Sbenjscstatic void
1618173362Sbenjscwpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1619173362Sbenjsc{
1620173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
1621173362Sbenjsc	struct wpi_tx_data *data;
1622173362Sbenjsc
1623173362Sbenjsc	DPRINTFN(WPI_DEBUG_CMD, ("cmd notification qid=%x idx=%d flags=%x "
1624173362Sbenjsc				 "type=%s len=%d\n", desc->qid, desc->idx,
1625173362Sbenjsc				 desc->flags, wpi_cmd_str(desc->type),
1626173362Sbenjsc				 le32toh(desc->len)));
1627173362Sbenjsc
1628173362Sbenjsc	if ((desc->qid & 7) != 4)
1629173362Sbenjsc		return;	/* not a command ack */
1630173362Sbenjsc
1631173362Sbenjsc	data = &ring->data[desc->idx];
1632173362Sbenjsc
1633173362Sbenjsc	/* if the command was mapped in a mbuf, free it */
1634173362Sbenjsc	if (data->m != NULL) {
1635173362Sbenjsc		bus_dmamap_unload(ring->data_dmat, data->map);
1636173362Sbenjsc		m_freem(data->m);
1637173362Sbenjsc		data->m = NULL;
1638173362Sbenjsc	}
1639173362Sbenjsc
1640173362Sbenjsc	sc->flags &= ~WPI_FLAG_BUSY;
1641173362Sbenjsc	wakeup(&ring->cmd[desc->idx]);
1642173362Sbenjsc}
1643173362Sbenjsc
1644173362Sbenjscstatic void
1645179037Sthompsawpi_bmiss(void *arg, int npending)
1646179037Sthompsa{
1647179037Sthompsa	struct wpi_softc *sc = arg;
1648179037Sthompsa	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
1649179037Sthompsa
1650179037Sthompsa	ieee80211_beacon_miss(ic);
1651179037Sthompsa}
1652179037Sthompsa
1653179037Sthompsastatic void
1654173362Sbenjscwpi_notif_intr(struct wpi_softc *sc)
1655173362Sbenjsc{
1656178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1657178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1658173362Sbenjsc	struct wpi_rx_desc *desc;
1659173362Sbenjsc	struct wpi_rx_data *data;
1660173362Sbenjsc	uint32_t hw;
1661173362Sbenjsc
1662173362Sbenjsc	hw = le32toh(sc->shared->next);
1663173362Sbenjsc	while (sc->rxq.cur != hw) {
1664173362Sbenjsc		data = &sc->rxq.data[sc->rxq.cur];
1665173362Sbenjsc		desc = (void *)data->m->m_ext.ext_buf;
1666173362Sbenjsc
1667173362Sbenjsc		DPRINTFN(WPI_DEBUG_NOTIFY,
1668173362Sbenjsc			 ("notify qid=%x idx=%d flags=%x type=%d len=%d\n",
1669173362Sbenjsc			  desc->qid,
1670173362Sbenjsc			  desc->idx,
1671173362Sbenjsc			  desc->flags,
1672173362Sbenjsc			  desc->type,
1673173362Sbenjsc			  le32toh(desc->len)));
1674173362Sbenjsc
1675173362Sbenjsc		if (!(desc->qid & 0x80))	/* reply to a command */
1676173362Sbenjsc			wpi_cmd_intr(sc, desc);
1677173362Sbenjsc
1678173362Sbenjsc		switch (desc->type) {
1679173362Sbenjsc		case WPI_RX_DONE:
1680173362Sbenjsc			/* a 802.11 frame was received */
1681173362Sbenjsc			wpi_rx_intr(sc, desc, data);
1682173362Sbenjsc			break;
1683173362Sbenjsc
1684173362Sbenjsc		case WPI_TX_DONE:
1685173362Sbenjsc			/* a 802.11 frame has been transmitted */
1686173362Sbenjsc			wpi_tx_intr(sc, desc);
1687173362Sbenjsc			break;
1688173362Sbenjsc
1689173362Sbenjsc		case WPI_UC_READY:
1690173362Sbenjsc		{
1691173362Sbenjsc			struct wpi_ucode_info *uc =
1692173362Sbenjsc				(struct wpi_ucode_info *)(desc + 1);
1693173362Sbenjsc
1694173362Sbenjsc			/* the microcontroller is ready */
1695173362Sbenjsc			DPRINTF(("microcode alive notification version %x "
1696173362Sbenjsc				"alive %x\n", le32toh(uc->version),
1697173362Sbenjsc				le32toh(uc->valid)));
1698173362Sbenjsc
1699173362Sbenjsc			if (le32toh(uc->valid) != 1) {
1700173362Sbenjsc				device_printf(sc->sc_dev,
1701173362Sbenjsc				    "microcontroller initialization failed\n");
1702173362Sbenjsc				wpi_stop_locked(sc);
1703173362Sbenjsc			}
1704173362Sbenjsc			break;
1705173362Sbenjsc		}
1706173362Sbenjsc		case WPI_STATE_CHANGED:
1707173362Sbenjsc		{
1708173362Sbenjsc			uint32_t *status = (uint32_t *)(desc + 1);
1709173362Sbenjsc
1710173362Sbenjsc			/* enabled/disabled notification */
1711173362Sbenjsc			DPRINTF(("state changed to %x\n", le32toh(*status)));
1712173362Sbenjsc
1713173362Sbenjsc			if (le32toh(*status) & 1) {
1714173362Sbenjsc				device_printf(sc->sc_dev,
1715173362Sbenjsc				    "Radio transmitter is switched off\n");
1716173362Sbenjsc				sc->flags |= WPI_FLAG_HW_RADIO_OFF;
1717177043Sthompsa				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1718177043Sthompsa				/* Disable firmware commands */
1719177043Sthompsa				WPI_WRITE(sc, WPI_UCODE_SET, WPI_DISABLE_CMD);
1720173362Sbenjsc			}
1721173362Sbenjsc			break;
1722173362Sbenjsc		}
1723173362Sbenjsc		case WPI_START_SCAN:
1724173362Sbenjsc		{
1725179957Sthompsa#ifdef WPI_DEBUG
1726173362Sbenjsc			struct wpi_start_scan *scan =
1727173362Sbenjsc				(struct wpi_start_scan *)(desc + 1);
1728179957Sthompsa#endif
1729173362Sbenjsc
1730173362Sbenjsc			DPRINTFN(WPI_DEBUG_SCANNING,
1731173362Sbenjsc				 ("scanning channel %d status %x\n",
1732173362Sbenjsc			    scan->chan, le32toh(scan->status)));
1733173362Sbenjsc			break;
1734173362Sbenjsc		}
1735173362Sbenjsc		case WPI_STOP_SCAN:
1736173362Sbenjsc		{
1737179957Sthompsa#ifdef WPI_DEBUG
1738173362Sbenjsc			struct wpi_stop_scan *scan =
1739173362Sbenjsc				(struct wpi_stop_scan *)(desc + 1);
1740179957Sthompsa#endif
1741178354Ssam			struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1742173362Sbenjsc
1743173362Sbenjsc			DPRINTFN(WPI_DEBUG_SCANNING,
1744173362Sbenjsc			    ("scan finished nchan=%d status=%d chan=%d\n",
1745173362Sbenjsc			     scan->nchan, scan->status, scan->chan));
1746173362Sbenjsc
1747177043Sthompsa			sc->sc_scan_timer = 0;
1748178354Ssam			ieee80211_scan_next(vap);
1749173362Sbenjsc			break;
1750173362Sbenjsc		}
1751173976Sbenjsc		case WPI_MISSED_BEACON:
1752173976Sbenjsc		{
1753178354Ssam			struct wpi_missed_beacon *beacon =
1754173976Sbenjsc				(struct wpi_missed_beacon *)(desc + 1);
1755178354Ssam			struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1756173976Sbenjsc
1757178354Ssam			if (le32toh(beacon->consecutive) >=
1758178354Ssam			    vap->iv_bmissthreshold) {
1759178354Ssam				DPRINTF(("Beacon miss: %u >= %u\n",
1760178354Ssam					 le32toh(beacon->consecutive),
1761178354Ssam					 vap->iv_bmissthreshold));
1762179037Sthompsa				taskqueue_enqueue(taskqueue_swi,
1763179037Sthompsa				    &sc->sc_bmiss_task);
1764178354Ssam			}
1765178354Ssam			break;
1766173362Sbenjsc		}
1767173976Sbenjsc		}
1768173362Sbenjsc
1769173362Sbenjsc		sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
1770173362Sbenjsc	}
1771173362Sbenjsc
1772173362Sbenjsc	/* tell the firmware what we have processed */
1773173362Sbenjsc	hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
1774173362Sbenjsc	WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7);
1775173362Sbenjsc}
1776173362Sbenjsc
1777173362Sbenjscstatic void
1778173362Sbenjscwpi_intr(void *arg)
1779173362Sbenjsc{
1780173362Sbenjsc	struct wpi_softc *sc = arg;
1781173362Sbenjsc	uint32_t r;
1782173362Sbenjsc
1783173362Sbenjsc	WPI_LOCK(sc);
1784173362Sbenjsc
1785173362Sbenjsc	r = WPI_READ(sc, WPI_INTR);
1786173362Sbenjsc	if (r == 0 || r == 0xffffffff) {
1787173362Sbenjsc		WPI_UNLOCK(sc);
1788173362Sbenjsc		return;
1789173362Sbenjsc	}
1790173362Sbenjsc
1791173362Sbenjsc	/* disable interrupts */
1792173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, 0);
1793173362Sbenjsc	/* ack interrupts */
1794173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, r);
1795173362Sbenjsc
1796173362Sbenjsc	if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) {
1797173362Sbenjsc		device_printf(sc->sc_dev, "fatal firmware error\n");
1798173362Sbenjsc		DPRINTFN(6,("(%s)\n", (r & WPI_SW_ERROR) ? "(Software Error)" :
1799173362Sbenjsc				"(Hardware Error)"));
1800177043Sthompsa		wpi_queue_cmd(sc, WPI_RESTART, 0, WPI_QUEUE_CLEAR);
1801173362Sbenjsc		sc->flags &= ~WPI_FLAG_BUSY;
1802173362Sbenjsc		WPI_UNLOCK(sc);
1803173362Sbenjsc		return;
1804173362Sbenjsc	}
1805173362Sbenjsc
1806173362Sbenjsc	if (r & WPI_RX_INTR)
1807173362Sbenjsc		wpi_notif_intr(sc);
1808173362Sbenjsc
1809173362Sbenjsc	if (r & WPI_ALIVE_INTR)	/* firmware initialized */
1810173362Sbenjsc		wakeup(sc);
1811173362Sbenjsc
1812173362Sbenjsc	/* re-enable interrupts */
1813173362Sbenjsc	if (sc->sc_ifp->if_flags & IFF_UP)
1814173362Sbenjsc		WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
1815173362Sbenjsc
1816173362Sbenjsc	WPI_UNLOCK(sc);
1817173362Sbenjsc}
1818173362Sbenjsc
1819173362Sbenjscstatic uint8_t
1820173362Sbenjscwpi_plcp_signal(int rate)
1821173362Sbenjsc{
1822173362Sbenjsc	switch (rate) {
1823173362Sbenjsc	/* CCK rates (returned values are device-dependent) */
1824173362Sbenjsc	case 2:		return 10;
1825173362Sbenjsc	case 4:		return 20;
1826173362Sbenjsc	case 11:	return 55;
1827173362Sbenjsc	case 22:	return 110;
1828173362Sbenjsc
1829173362Sbenjsc	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1830173362Sbenjsc	/* R1-R4 (ral/ural is R4-R1) */
1831173362Sbenjsc	case 12:	return 0xd;
1832173362Sbenjsc	case 18:	return 0xf;
1833173362Sbenjsc	case 24:	return 0x5;
1834173362Sbenjsc	case 36:	return 0x7;
1835173362Sbenjsc	case 48:	return 0x9;
1836173362Sbenjsc	case 72:	return 0xb;
1837173362Sbenjsc	case 96:	return 0x1;
1838173362Sbenjsc	case 108:	return 0x3;
1839173362Sbenjsc
1840173362Sbenjsc	/* unsupported rates (should not get there) */
1841173362Sbenjsc	default:	return 0;
1842173362Sbenjsc	}
1843173362Sbenjsc}
1844173362Sbenjsc
1845173362Sbenjsc/* quickly determine if a given rate is CCK or OFDM */
1846173362Sbenjsc#define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1847173362Sbenjsc
1848173362Sbenjsc/*
1849173362Sbenjsc * Construct the data packet for a transmit buffer and acutally put
1850173362Sbenjsc * the buffer onto the transmit ring, kicking the card to process the
1851173362Sbenjsc * the buffer.
1852173362Sbenjsc */
1853173362Sbenjscstatic int
1854173362Sbenjscwpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1855173362Sbenjsc	int ac)
1856173362Sbenjsc{
1857178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1858178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1859178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1860177043Sthompsa	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1861173362Sbenjsc	struct wpi_tx_ring *ring = &sc->txq[ac];
1862173362Sbenjsc	struct wpi_tx_desc *desc;
1863173362Sbenjsc	struct wpi_tx_data *data;
1864173362Sbenjsc	struct wpi_tx_cmd *cmd;
1865173362Sbenjsc	struct wpi_cmd_data *tx;
1866173362Sbenjsc	struct ieee80211_frame *wh;
1867178354Ssam	const struct ieee80211_txparam *tp;
1868173362Sbenjsc	struct ieee80211_key *k;
1869173362Sbenjsc	struct mbuf *mnew;
1870177043Sthompsa	int i, error, nsegs, rate, hdrlen, ismcast;
1871173362Sbenjsc	bus_dma_segment_t segs[WPI_MAX_SCATTER];
1872173362Sbenjsc
1873173362Sbenjsc	desc = &ring->desc[ring->cur];
1874173362Sbenjsc	data = &ring->data[ring->cur];
1875173362Sbenjsc
1876173362Sbenjsc	wh = mtod(m0, struct ieee80211_frame *);
1877173362Sbenjsc
1878177043Sthompsa	hdrlen = ieee80211_hdrsize(wh);
1879177043Sthompsa	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1880173362Sbenjsc
1881173362Sbenjsc	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1882178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1883177043Sthompsa		if (k == NULL) {
1884173362Sbenjsc			m_freem(m0);
1885173362Sbenjsc			return ENOBUFS;
1886173362Sbenjsc		}
1887173362Sbenjsc		/* packet header may have moved, reset our local pointer */
1888173362Sbenjsc		wh = mtod(m0, struct ieee80211_frame *);
1889173362Sbenjsc	}
1890173362Sbenjsc
1891173362Sbenjsc	cmd = &ring->cmd[ring->cur];
1892173362Sbenjsc	cmd->code = WPI_CMD_TX_DATA;
1893173362Sbenjsc	cmd->flags = 0;
1894173362Sbenjsc	cmd->qid = ring->qid;
1895173362Sbenjsc	cmd->idx = ring->cur;
1896173362Sbenjsc
1897173362Sbenjsc	tx = (struct wpi_cmd_data *)cmd->data;
1898177043Sthompsa	tx->flags = htole32(WPI_TX_AUTO_SEQ);
1899178354Ssam	tx->timeout = htole16(0);
1900177043Sthompsa	tx->ofdm_mask = 0xff;
1901177043Sthompsa	tx->cck_mask = 0x0f;
1902177043Sthompsa	tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
1903177043Sthompsa	tx->id = ismcast ? WPI_ID_BROADCAST : WPI_ID_BSS;
1904177043Sthompsa	tx->len = htole16(m0->m_pkthdr.len);
1905173362Sbenjsc
1906177119Ssam	if (!ismcast) {
1907177043Sthompsa		if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0 ||
1908177043Sthompsa		    !cap->cap_wmeParams[ac].wmep_noackPolicy)
1909177043Sthompsa			tx->flags |= htole32(WPI_TX_NEED_ACK);
1910178354Ssam		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
1911177043Sthompsa			tx->flags |= htole32(WPI_TX_NEED_RTS|WPI_TX_FULL_TXOP);
1912177043Sthompsa			tx->rts_ntries = 7;
1913177043Sthompsa		}
1914173362Sbenjsc	}
1915177043Sthompsa	/* pick a rate */
1916178354Ssam	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1917178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT) {
1918173362Sbenjsc		uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1919173362Sbenjsc		/* tell h/w to set timestamp in probe responses */
1920173362Sbenjsc		if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1921177043Sthompsa			tx->flags |= htole32(WPI_TX_INSERT_TSTAMP);
1922173362Sbenjsc		if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
1923177043Sthompsa		    subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
1924173362Sbenjsc			tx->timeout = htole16(3);
1925173362Sbenjsc		else
1926173362Sbenjsc			tx->timeout = htole16(2);
1927178354Ssam		rate = tp->mgmtrate;
1928177043Sthompsa	} else if (ismcast) {
1929178354Ssam		rate = tp->mcastrate;
1930178354Ssam	} else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1931178354Ssam		rate = tp->ucastrate;
1932177043Sthompsa	} else {
1933178354Ssam		(void) ieee80211_amrr_choose(ni, &WPI_NODE(ni)->amn);
1934178354Ssam		rate = ni->ni_txrate;
1935177043Sthompsa	}
1936173362Sbenjsc	tx->rate = wpi_plcp_signal(rate);
1937173362Sbenjsc
1938173362Sbenjsc	/* be very persistant at sending frames out */
1939178354Ssam#if 0
1940178354Ssam	tx->data_ntries = tp->maxretry;
1941178354Ssam#else
1942178354Ssam	tx->data_ntries = 15;		/* XXX way too high */
1943178354Ssam#endif
1944173362Sbenjsc
1945178354Ssam	if (bpf_peers_present(ifp->if_bpf)) {
1946177043Sthompsa		struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
1947177043Sthompsa		tap->wt_flags = 0;
1948177043Sthompsa		tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
1949177043Sthompsa		tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
1950177043Sthompsa		tap->wt_rate = rate;
1951177043Sthompsa		tap->wt_hwqueue = ac;
1952177043Sthompsa		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1953177043Sthompsa			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1954178354Ssam
1955178354Ssam		bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1956177043Sthompsa	}
1957173362Sbenjsc
1958173362Sbenjsc	/* save and trim IEEE802.11 header */
1959173362Sbenjsc	m_copydata(m0, 0, hdrlen, (caddr_t)&tx->wh);
1960173362Sbenjsc	m_adj(m0, hdrlen);
1961173362Sbenjsc
1962173362Sbenjsc	error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m0, segs,
1963173362Sbenjsc	    &nsegs, BUS_DMA_NOWAIT);
1964173362Sbenjsc	if (error != 0 && error != EFBIG) {
1965173362Sbenjsc		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1966173362Sbenjsc		    error);
1967173362Sbenjsc		m_freem(m0);
1968173362Sbenjsc		return error;
1969173362Sbenjsc	}
1970173362Sbenjsc	if (error != 0) {
1971175418Sjhb		/* XXX use m_collapse */
1972173362Sbenjsc		mnew = m_defrag(m0, M_DONTWAIT);
1973173362Sbenjsc		if (mnew == NULL) {
1974173362Sbenjsc			device_printf(sc->sc_dev,
1975173362Sbenjsc			    "could not defragment mbuf\n");
1976173362Sbenjsc			m_freem(m0);
1977173362Sbenjsc			return ENOBUFS;
1978173362Sbenjsc		}
1979173362Sbenjsc		m0 = mnew;
1980173362Sbenjsc
1981173362Sbenjsc		error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
1982173362Sbenjsc		    m0, segs, &nsegs, BUS_DMA_NOWAIT);
1983173362Sbenjsc		if (error != 0) {
1984173362Sbenjsc			device_printf(sc->sc_dev,
1985173362Sbenjsc			    "could not map mbuf (error %d)\n", error);
1986173362Sbenjsc			m_freem(m0);
1987173362Sbenjsc			return error;
1988173362Sbenjsc		}
1989173362Sbenjsc	}
1990173362Sbenjsc
1991173362Sbenjsc	data->m = m0;
1992173362Sbenjsc	data->ni = ni;
1993173362Sbenjsc
1994173362Sbenjsc	DPRINTFN(WPI_DEBUG_TX, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
1995173362Sbenjsc	    ring->qid, ring->cur, m0->m_pkthdr.len, nsegs));
1996173362Sbenjsc
1997173362Sbenjsc	/* first scatter/gather segment is used by the tx data command */
1998173362Sbenjsc	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 |
1999173362Sbenjsc	    (1 + nsegs) << 24);
2000173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2001173362Sbenjsc	    ring->cur * sizeof (struct wpi_tx_cmd));
2002173362Sbenjsc	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_data));
2003173362Sbenjsc	for (i = 1; i <= nsegs; i++) {
2004173362Sbenjsc		desc->segs[i].addr = htole32(segs[i - 1].ds_addr);
2005173362Sbenjsc		desc->segs[i].len  = htole32(segs[i - 1].ds_len);
2006173362Sbenjsc	}
2007173362Sbenjsc
2008173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2009173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2010173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
2011173362Sbenjsc
2012173362Sbenjsc	ring->queued++;
2013173362Sbenjsc
2014173362Sbenjsc	/* kick ring */
2015173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
2016173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2017173362Sbenjsc
2018173362Sbenjsc	return 0;
2019173362Sbenjsc}
2020173362Sbenjsc
2021173362Sbenjsc/**
2022173362Sbenjsc * Process data waiting to be sent on the IFNET output queue
2023173362Sbenjsc */
2024173362Sbenjscstatic void
2025173362Sbenjscwpi_start(struct ifnet *ifp)
2026173362Sbenjsc{
2027173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
2028178354Ssam
2029178354Ssam	WPI_LOCK(sc);
2030178354Ssam	wpi_start_locked(ifp);
2031178354Ssam	WPI_UNLOCK(sc);
2032178354Ssam}
2033178354Ssam
2034178354Ssamstatic void
2035178354Ssamwpi_start_locked(struct ifnet *ifp)
2036178354Ssam{
2037178354Ssam	struct wpi_softc *sc = ifp->if_softc;
2038173362Sbenjsc	struct ieee80211_node *ni;
2039178354Ssam	struct mbuf *m;
2040178354Ssam	int ac;
2041173362Sbenjsc
2042178354Ssam	WPI_LOCK_ASSERT(sc);
2043178354Ssam
2044177043Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2045177043Sthompsa		return;
2046173362Sbenjsc
2047173362Sbenjsc	for (;;) {
2048190458Sjmallett		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2049178354Ssam		if (m == NULL)
2050178354Ssam			break;
2051178354Ssam		/* no QoS encapsulation for EAPOL frames */
2052178354Ssam		ac = M_WME_GETAC(m);
2053178354Ssam		if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
2054178354Ssam			/* there is no place left in this ring */
2055178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m);
2056178354Ssam			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2057178354Ssam			break;
2058178354Ssam		}
2059178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
2060178354Ssam		m = ieee80211_encap(ni, m);
2061178354Ssam		if (m == NULL) {
2062178354Ssam			ieee80211_free_node(ni);
2063178354Ssam			ifp->if_oerrors++;
2064178354Ssam			continue;
2065178354Ssam		}
2066178354Ssam		if (wpi_tx_data(sc, m, ni, ac) != 0) {
2067178354Ssam			ieee80211_free_node(ni);
2068178354Ssam			ifp->if_oerrors++;
2069178354Ssam			break;
2070178354Ssam		}
2071178354Ssam		sc->sc_tx_timer = 5;
2072178354Ssam	}
2073178354Ssam}
2074173362Sbenjsc
2075178354Ssamstatic int
2076178354Ssamwpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2077178354Ssam	const struct ieee80211_bpf_params *params)
2078178354Ssam{
2079178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2080178354Ssam	struct ifnet *ifp = ic->ic_ifp;
2081178354Ssam	struct wpi_softc *sc = ifp->if_softc;
2082173362Sbenjsc
2083178354Ssam	/* prevent management frames from being sent if we're not ready */
2084178354Ssam	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2085178354Ssam		m_freem(m);
2086178354Ssam		ieee80211_free_node(ni);
2087178354Ssam		return ENETDOWN;
2088178354Ssam	}
2089178354Ssam	WPI_LOCK(sc);
2090173362Sbenjsc
2091178354Ssam	/* management frames go into ring 0 */
2092178354Ssam	if (sc->txq[0].queued > sc->txq[0].count - 8) {
2093178354Ssam		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2094178354Ssam		m_freem(m);
2095178354Ssam		WPI_UNLOCK(sc);
2096178354Ssam		ieee80211_free_node(ni);
2097178354Ssam		return ENOBUFS;		/* XXX */
2098178354Ssam	}
2099173362Sbenjsc
2100178354Ssam	ifp->if_opackets++;
2101178354Ssam	if (wpi_tx_data(sc, m, ni, 0) != 0)
2102178354Ssam		goto bad;
2103178354Ssam	sc->sc_tx_timer = 5;
2104178354Ssam	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
2105173362Sbenjsc
2106178354Ssam	WPI_UNLOCK(sc);
2107178354Ssam	return 0;
2108178354Ssambad:
2109178354Ssam	ifp->if_oerrors++;
2110178354Ssam	WPI_UNLOCK(sc);
2111178354Ssam	ieee80211_free_node(ni);
2112178354Ssam	return EIO;		/* XXX */
2113173362Sbenjsc}
2114173362Sbenjsc
2115173362Sbenjscstatic int
2116173362Sbenjscwpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2117173362Sbenjsc{
2118173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
2119178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2120178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
2121178354Ssam	int error = 0, startall = 0;
2122173362Sbenjsc
2123173362Sbenjsc	switch (cmd) {
2124173362Sbenjsc	case SIOCSIFFLAGS:
2125178704Sthompsa		WPI_LOCK(sc);
2126173362Sbenjsc		if ((ifp->if_flags & IFF_UP)) {
2127178354Ssam			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2128177043Sthompsa				wpi_init_locked(sc, 0);
2129178354Ssam				startall = 1;
2130178354Ssam			}
2131177043Sthompsa		} else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) ||
2132177043Sthompsa			   (sc->flags & WPI_FLAG_HW_RADIO_OFF))
2133173362Sbenjsc			wpi_stop_locked(sc);
2134178704Sthompsa		WPI_UNLOCK(sc);
2135178704Sthompsa		if (startall)
2136178704Sthompsa			ieee80211_start_all(ic);
2137173362Sbenjsc		break;
2138178354Ssam	case SIOCGIFMEDIA:
2139178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2140178354Ssam		break;
2141178704Sthompsa	case SIOCGIFADDR:
2142178354Ssam		error = ether_ioctl(ifp, cmd, data);
2143178354Ssam		break;
2144178704Sthompsa	default:
2145178704Sthompsa		error = EINVAL;
2146178704Sthompsa		break;
2147173362Sbenjsc	}
2148173362Sbenjsc	return error;
2149173362Sbenjsc}
2150173362Sbenjsc
2151173362Sbenjsc/*
2152173362Sbenjsc * Extract various information from EEPROM.
2153173362Sbenjsc */
2154173362Sbenjscstatic void
2155190526Ssamwpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
2156173362Sbenjsc{
2157173362Sbenjsc	int i;
2158173362Sbenjsc
2159173362Sbenjsc	/* read the hardware capabilities, revision and SKU type */
2160173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap,1);
2161173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,2);
2162173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1);
2163173362Sbenjsc
2164173362Sbenjsc	/* read the regulatory domain */
2165173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain, 4);
2166173362Sbenjsc
2167173362Sbenjsc	/* read in the hw MAC address */
2168190526Ssam	wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr, 6);
2169173362Sbenjsc
2170173362Sbenjsc	/* read the list of authorized channels */
2171173362Sbenjsc	for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
2172173362Sbenjsc		wpi_read_eeprom_channels(sc,i);
2173173362Sbenjsc
2174173362Sbenjsc	/* read the power level calibration info for each group */
2175173362Sbenjsc	for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
2176173362Sbenjsc		wpi_read_eeprom_group(sc,i);
2177173362Sbenjsc}
2178173362Sbenjsc
2179173362Sbenjsc/*
2180173362Sbenjsc * Send a command to the firmware.
2181173362Sbenjsc */
2182173362Sbenjscstatic int
2183173362Sbenjscwpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
2184173362Sbenjsc{
2185173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2186173362Sbenjsc	struct wpi_tx_desc *desc;
2187173362Sbenjsc	struct wpi_tx_cmd *cmd;
2188173362Sbenjsc
2189173362Sbenjsc#ifdef WPI_DEBUG
2190173362Sbenjsc	if (!async) {
2191173362Sbenjsc		WPI_LOCK_ASSERT(sc);
2192173362Sbenjsc	}
2193173362Sbenjsc#endif
2194173362Sbenjsc
2195173362Sbenjsc	DPRINTFN(WPI_DEBUG_CMD,("wpi_cmd %d size %d async %d\n", code, size,
2196173362Sbenjsc		    async));
2197173362Sbenjsc
2198173362Sbenjsc	if (sc->flags & WPI_FLAG_BUSY) {
2199173362Sbenjsc		device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n",
2200173362Sbenjsc		    __func__, code);
2201173362Sbenjsc		return EAGAIN;
2202173362Sbenjsc	}
2203173362Sbenjsc	sc->flags|= WPI_FLAG_BUSY;
2204173362Sbenjsc
2205173362Sbenjsc	KASSERT(size <= sizeof cmd->data, ("command %d too large: %d bytes",
2206173362Sbenjsc	    code, size));
2207173362Sbenjsc
2208173362Sbenjsc	desc = &ring->desc[ring->cur];
2209173362Sbenjsc	cmd = &ring->cmd[ring->cur];
2210173362Sbenjsc
2211173362Sbenjsc	cmd->code = code;
2212173362Sbenjsc	cmd->flags = 0;
2213173362Sbenjsc	cmd->qid = ring->qid;
2214173362Sbenjsc	cmd->idx = ring->cur;
2215173362Sbenjsc	memcpy(cmd->data, buf, size);
2216173362Sbenjsc
2217173362Sbenjsc	desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
2218173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2219173362Sbenjsc		ring->cur * sizeof (struct wpi_tx_cmd));
2220173362Sbenjsc	desc->segs[0].len  = htole32(4 + size);
2221173362Sbenjsc
2222173362Sbenjsc	/* kick cmd ring */
2223173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2224173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2225173362Sbenjsc
2226173362Sbenjsc	if (async) {
2227173362Sbenjsc		sc->flags &= ~ WPI_FLAG_BUSY;
2228173362Sbenjsc		return 0;
2229173362Sbenjsc	}
2230173362Sbenjsc
2231173362Sbenjsc	return msleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz);
2232173362Sbenjsc}
2233173362Sbenjsc
2234173362Sbenjscstatic int
2235173362Sbenjscwpi_wme_update(struct ieee80211com *ic)
2236173362Sbenjsc{
2237173362Sbenjsc#define WPI_EXP2(v)	htole16((1 << (v)) - 1)
2238173362Sbenjsc#define WPI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
2239173362Sbenjsc	struct wpi_softc *sc = ic->ic_ifp->if_softc;
2240173362Sbenjsc	const struct wmeParams *wmep;
2241173362Sbenjsc	struct wpi_wme_setup wme;
2242173362Sbenjsc	int ac;
2243173362Sbenjsc
2244173362Sbenjsc	/* don't override default WME values if WME is not actually enabled */
2245173362Sbenjsc	if (!(ic->ic_flags & IEEE80211_F_WME))
2246173362Sbenjsc		return 0;
2247173362Sbenjsc
2248173362Sbenjsc	wme.flags = 0;
2249173362Sbenjsc	for (ac = 0; ac < WME_NUM_AC; ac++) {
2250173362Sbenjsc		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
2251173362Sbenjsc		wme.ac[ac].aifsn = wmep->wmep_aifsn;
2252173362Sbenjsc		wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin);
2253173362Sbenjsc		wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax);
2254173362Sbenjsc		wme.ac[ac].txop  = WPI_USEC(wmep->wmep_txopLimit);
2255173362Sbenjsc
2256173362Sbenjsc		DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
2257173362Sbenjsc		    "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
2258173362Sbenjsc		    wme.ac[ac].cwmax, wme.ac[ac].txop));
2259173362Sbenjsc	}
2260173362Sbenjsc	return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1);
2261173362Sbenjsc#undef WPI_USEC
2262173362Sbenjsc#undef WPI_EXP2
2263173362Sbenjsc}
2264173362Sbenjsc
2265173362Sbenjsc/*
2266173362Sbenjsc * Configure h/w multi-rate retries.
2267173362Sbenjsc */
2268173362Sbenjscstatic int
2269173362Sbenjscwpi_mrr_setup(struct wpi_softc *sc)
2270173362Sbenjsc{
2271178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2272178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2273173362Sbenjsc	struct wpi_mrr_setup mrr;
2274173362Sbenjsc	int i, error;
2275173362Sbenjsc
2276173362Sbenjsc	memset(&mrr, 0, sizeof (struct wpi_mrr_setup));
2277173362Sbenjsc
2278173362Sbenjsc	/* CCK rates (not used with 802.11a) */
2279173362Sbenjsc	for (i = WPI_CCK1; i <= WPI_CCK11; i++) {
2280173362Sbenjsc		mrr.rates[i].flags = 0;
2281173362Sbenjsc		mrr.rates[i].signal = wpi_ridx_to_plcp[i];
2282173362Sbenjsc		/* fallback to the immediate lower CCK rate (if any) */
2283173362Sbenjsc		mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1;
2284173362Sbenjsc		/* try one time at this rate before falling back to "next" */
2285173362Sbenjsc		mrr.rates[i].ntries = 1;
2286173362Sbenjsc	}
2287173362Sbenjsc
2288173362Sbenjsc	/* OFDM rates (not used with 802.11b) */
2289173362Sbenjsc	for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) {
2290173362Sbenjsc		mrr.rates[i].flags = 0;
2291173362Sbenjsc		mrr.rates[i].signal = wpi_ridx_to_plcp[i];
2292173362Sbenjsc		/* fallback to the immediate lower OFDM rate (if any) */
2293173362Sbenjsc		/* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */
2294173362Sbenjsc		mrr.rates[i].next = (i == WPI_OFDM6) ?
2295173362Sbenjsc		    ((ic->ic_curmode == IEEE80211_MODE_11A) ?
2296173362Sbenjsc			WPI_OFDM6 : WPI_CCK2) :
2297173362Sbenjsc		    i - 1;
2298173362Sbenjsc		/* try one time at this rate before falling back to "next" */
2299173362Sbenjsc		mrr.rates[i].ntries = 1;
2300173362Sbenjsc	}
2301173362Sbenjsc
2302173362Sbenjsc	/* setup MRR for control frames */
2303173362Sbenjsc	mrr.which = htole32(WPI_MRR_CTL);
2304173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2305173362Sbenjsc	if (error != 0) {
2306173362Sbenjsc		device_printf(sc->sc_dev,
2307173362Sbenjsc		    "could not setup MRR for control frames\n");
2308173362Sbenjsc		return error;
2309173362Sbenjsc	}
2310173362Sbenjsc
2311173362Sbenjsc	/* setup MRR for data frames */
2312173362Sbenjsc	mrr.which = htole32(WPI_MRR_DATA);
2313173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2314173362Sbenjsc	if (error != 0) {
2315173362Sbenjsc		device_printf(sc->sc_dev,
2316173362Sbenjsc		    "could not setup MRR for data frames\n");
2317173362Sbenjsc		return error;
2318173362Sbenjsc	}
2319173362Sbenjsc
2320173362Sbenjsc	return 0;
2321173362Sbenjsc}
2322173362Sbenjsc
2323173362Sbenjscstatic void
2324173362Sbenjscwpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
2325173362Sbenjsc{
2326173362Sbenjsc	struct wpi_cmd_led led;
2327173362Sbenjsc
2328173362Sbenjsc	led.which = which;
2329173362Sbenjsc	led.unit = htole32(100000);	/* on/off in unit of 100ms */
2330173362Sbenjsc	led.off = off;
2331173362Sbenjsc	led.on = on;
2332173362Sbenjsc
2333173362Sbenjsc	(void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
2334173362Sbenjsc}
2335173362Sbenjsc
2336173362Sbenjscstatic void
2337173362Sbenjscwpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni)
2338173362Sbenjsc{
2339173362Sbenjsc	struct wpi_cmd_tsf tsf;
2340173362Sbenjsc	uint64_t val, mod;
2341173362Sbenjsc
2342173362Sbenjsc	memset(&tsf, 0, sizeof tsf);
2343173362Sbenjsc	memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8);
2344173362Sbenjsc	tsf.bintval = htole16(ni->ni_intval);
2345173362Sbenjsc	tsf.lintval = htole16(10);
2346173362Sbenjsc
2347173362Sbenjsc	/* compute remaining time until next beacon */
2348173362Sbenjsc	val = (uint64_t)ni->ni_intval  * 1024;	/* msec -> usec */
2349173362Sbenjsc	mod = le64toh(tsf.tstamp) % val;
2350173362Sbenjsc	tsf.binitval = htole32((uint32_t)(val - mod));
2351173362Sbenjsc
2352173362Sbenjsc	if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
2353173362Sbenjsc		device_printf(sc->sc_dev, "could not enable TSF\n");
2354173362Sbenjsc}
2355173362Sbenjsc
2356173362Sbenjsc#if 0
2357173362Sbenjsc/*
2358173362Sbenjsc * Build a beacon frame that the firmware will broadcast periodically in
2359173362Sbenjsc * IBSS or HostAP modes.
2360173362Sbenjsc */
2361173362Sbenjscstatic int
2362173362Sbenjscwpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
2363173362Sbenjsc{
2364178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2365178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2366173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2367173362Sbenjsc	struct wpi_tx_desc *desc;
2368173362Sbenjsc	struct wpi_tx_data *data;
2369173362Sbenjsc	struct wpi_tx_cmd *cmd;
2370173362Sbenjsc	struct wpi_cmd_beacon *bcn;
2371173362Sbenjsc	struct ieee80211_beacon_offsets bo;
2372173362Sbenjsc	struct mbuf *m0;
2373173362Sbenjsc	bus_addr_t physaddr;
2374173362Sbenjsc	int error;
2375173362Sbenjsc
2376173362Sbenjsc	desc = &ring->desc[ring->cur];
2377173362Sbenjsc	data = &ring->data[ring->cur];
2378173362Sbenjsc
2379173362Sbenjsc	m0 = ieee80211_beacon_alloc(ic, ni, &bo);
2380173362Sbenjsc	if (m0 == NULL) {
2381173362Sbenjsc		device_printf(sc->sc_dev, "could not allocate beacon frame\n");
2382173362Sbenjsc		return ENOMEM;
2383173362Sbenjsc	}
2384173362Sbenjsc
2385173362Sbenjsc	cmd = &ring->cmd[ring->cur];
2386173362Sbenjsc	cmd->code = WPI_CMD_SET_BEACON;
2387173362Sbenjsc	cmd->flags = 0;
2388173362Sbenjsc	cmd->qid = ring->qid;
2389173362Sbenjsc	cmd->idx = ring->cur;
2390173362Sbenjsc
2391173362Sbenjsc	bcn = (struct wpi_cmd_beacon *)cmd->data;
2392173362Sbenjsc	memset(bcn, 0, sizeof (struct wpi_cmd_beacon));
2393173362Sbenjsc	bcn->id = WPI_ID_BROADCAST;
2394173362Sbenjsc	bcn->ofdm_mask = 0xff;
2395173362Sbenjsc	bcn->cck_mask = 0x0f;
2396173362Sbenjsc	bcn->lifetime = htole32(WPI_LIFETIME_INFINITE);
2397173362Sbenjsc	bcn->len = htole16(m0->m_pkthdr.len);
2398173362Sbenjsc	bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2399173362Sbenjsc		wpi_plcp_signal(12) : wpi_plcp_signal(2);
2400173362Sbenjsc	bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
2401173362Sbenjsc
2402173362Sbenjsc	/* save and trim IEEE802.11 header */
2403173362Sbenjsc	m_copydata(m0, 0, sizeof (struct ieee80211_frame), (caddr_t)&bcn->wh);
2404173362Sbenjsc	m_adj(m0, sizeof (struct ieee80211_frame));
2405173362Sbenjsc
2406173362Sbenjsc	/* assume beacon frame is contiguous */
2407173362Sbenjsc	error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m0, void *),
2408173362Sbenjsc	    m0->m_pkthdr.len, wpi_dma_map_addr, &physaddr, 0);
2409173362Sbenjsc	if (error != 0) {
2410173362Sbenjsc		device_printf(sc->sc_dev, "could not map beacon\n");
2411173362Sbenjsc		m_freem(m0);
2412173362Sbenjsc		return error;
2413173362Sbenjsc	}
2414173362Sbenjsc
2415173362Sbenjsc	data->m = m0;
2416173362Sbenjsc
2417173362Sbenjsc	/* first scatter/gather segment is used by the beacon command */
2418173362Sbenjsc	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24);
2419173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2420173362Sbenjsc		ring->cur * sizeof (struct wpi_tx_cmd));
2421173362Sbenjsc	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_beacon));
2422173362Sbenjsc	desc->segs[1].addr = htole32(physaddr);
2423173362Sbenjsc	desc->segs[1].len  = htole32(m0->m_pkthdr.len);
2424173362Sbenjsc
2425173362Sbenjsc	/* kick cmd ring */
2426173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2427173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2428173362Sbenjsc
2429173362Sbenjsc	return 0;
2430173362Sbenjsc}
2431173362Sbenjsc#endif
2432173362Sbenjsc
2433173362Sbenjscstatic int
2434178354Ssamwpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap)
2435173362Sbenjsc{
2436178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2437178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2438173362Sbenjsc	struct wpi_node_info node;
2439173362Sbenjsc	int error;
2440173362Sbenjsc
2441177043Sthompsa
2442173362Sbenjsc	/* update adapter's configuration */
2443177043Sthompsa	sc->config.associd = 0;
2444177043Sthompsa	sc->config.filter &= ~htole32(WPI_FILTER_BSS);
2445173362Sbenjsc	IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
2446173362Sbenjsc	sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2447173362Sbenjsc	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2448173362Sbenjsc		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2449173362Sbenjsc		    WPI_CONFIG_24GHZ);
2450173362Sbenjsc	}
2451178354Ssam	if (IEEE80211_IS_CHAN_A(ni->ni_chan)) {
2452173362Sbenjsc		sc->config.cck_mask  = 0;
2453173362Sbenjsc		sc->config.ofdm_mask = 0x15;
2454178354Ssam	} else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) {
2455173362Sbenjsc		sc->config.cck_mask  = 0x03;
2456173362Sbenjsc		sc->config.ofdm_mask = 0;
2457178354Ssam	} else {
2458178354Ssam		/* XXX assume 802.11b/g */
2459173362Sbenjsc		sc->config.cck_mask  = 0x0f;
2460173362Sbenjsc		sc->config.ofdm_mask = 0x15;
2461173362Sbenjsc	}
2462173362Sbenjsc
2463173362Sbenjsc	DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
2464173362Sbenjsc		sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
2465173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2466173362Sbenjsc		sizeof (struct wpi_config), 1);
2467173362Sbenjsc	if (error != 0) {
2468173362Sbenjsc		device_printf(sc->sc_dev, "could not configure\n");
2469173362Sbenjsc		return error;
2470173362Sbenjsc	}
2471173362Sbenjsc
2472173362Sbenjsc	/* configuration has changed, set Tx power accordingly */
2473173362Sbenjsc	if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
2474173362Sbenjsc		device_printf(sc->sc_dev, "could not set Tx power\n");
2475173362Sbenjsc		return error;
2476173362Sbenjsc	}
2477173362Sbenjsc
2478173362Sbenjsc	/* add default node */
2479173362Sbenjsc	memset(&node, 0, sizeof node);
2480173362Sbenjsc	IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid);
2481173362Sbenjsc	node.id = WPI_ID_BSS;
2482173362Sbenjsc	node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2483173362Sbenjsc	    wpi_plcp_signal(12) : wpi_plcp_signal(2);
2484173362Sbenjsc	node.action = htole32(WPI_ACTION_SET_RATE);
2485173362Sbenjsc	node.antenna = WPI_ANTENNA_BOTH;
2486173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2487177043Sthompsa	if (error != 0)
2488177043Sthompsa		device_printf(sc->sc_dev, "could not add BSS node\n");
2489177043Sthompsa
2490177043Sthompsa	return (error);
2491177043Sthompsa}
2492177043Sthompsa
2493177043Sthompsastatic int
2494178354Ssamwpi_run(struct wpi_softc *sc, struct ieee80211vap *vap)
2495177043Sthompsa{
2496178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2497178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2498177043Sthompsa	int error;
2499177043Sthompsa
2500178354Ssam	if (vap->iv_opmode == IEEE80211_M_MONITOR) {
2501178354Ssam		/* link LED blinks while monitoring */
2502178354Ssam		wpi_set_led(sc, WPI_LED_LINK, 5, 5);
2503178354Ssam		return 0;
2504178354Ssam	}
2505178354Ssam
2506177043Sthompsa	wpi_enable_tsf(sc, ni);
2507177043Sthompsa
2508177043Sthompsa	/* update adapter's configuration */
2509177043Sthompsa	sc->config.associd = htole16(ni->ni_associd & ~0xc000);
2510177043Sthompsa	/* short preamble/slot time are negotiated when associating */
2511177043Sthompsa	sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE |
2512177043Sthompsa	    WPI_CONFIG_SHSLOT);
2513177043Sthompsa	if (ic->ic_flags & IEEE80211_F_SHSLOT)
2514177043Sthompsa		sc->config.flags |= htole32(WPI_CONFIG_SHSLOT);
2515177043Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2516177043Sthompsa		sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE);
2517177043Sthompsa	sc->config.filter |= htole32(WPI_FILTER_BSS);
2518177043Sthompsa
2519177043Sthompsa	/* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */
2520177043Sthompsa
2521177043Sthompsa	DPRINTF(("config chan %d flags %x\n", sc->config.chan,
2522177043Sthompsa		    sc->config.flags));
2523177043Sthompsa	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, sizeof (struct
2524177043Sthompsa		    wpi_config), 1);
2525173362Sbenjsc	if (error != 0) {
2526177043Sthompsa		device_printf(sc->sc_dev, "could not update configuration\n");
2527173362Sbenjsc		return error;
2528173362Sbenjsc	}
2529173362Sbenjsc
2530178354Ssam	error = wpi_set_txpower(sc, ni->ni_chan, 1);
2531177043Sthompsa	if (error != 0) {
2532177043Sthompsa		device_printf(sc->sc_dev, "could set txpower\n");
2533177043Sthompsa		return error;
2534177043Sthompsa	}
2535173362Sbenjsc
2536177043Sthompsa	/* link LED always on while associated */
2537177043Sthompsa	wpi_set_led(sc, WPI_LED_LINK, 0, 1);
2538177043Sthompsa
2539177043Sthompsa	/* start automatic rate control timer */
2540178354Ssam	callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
2541177043Sthompsa
2542177043Sthompsa	return (error);
2543173362Sbenjsc}
2544173362Sbenjsc
2545173362Sbenjsc/*
2546173362Sbenjsc * Send a scan request to the firmware.  Since this command is huge, we map it
2547173362Sbenjsc * into a mbufcluster instead of using the pre-allocated set of commands. Note,
2548173362Sbenjsc * much of this code is similar to that in wpi_cmd but because we must manually
2549173362Sbenjsc * construct the probe & channels, we duplicate what's needed here. XXX In the
2550173362Sbenjsc * future, this function should be modified to use wpi_cmd to help cleanup the
2551173362Sbenjsc * code base.
2552173362Sbenjsc */
2553173362Sbenjscstatic int
2554173362Sbenjscwpi_scan(struct wpi_softc *sc)
2555173362Sbenjsc{
2556178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2557178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2558177043Sthompsa	struct ieee80211_scan_state *ss = ic->ic_scan;
2559173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2560173362Sbenjsc	struct wpi_tx_desc *desc;
2561173362Sbenjsc	struct wpi_tx_data *data;
2562173362Sbenjsc	struct wpi_tx_cmd *cmd;
2563173362Sbenjsc	struct wpi_scan_hdr *hdr;
2564173362Sbenjsc	struct wpi_scan_chan *chan;
2565173362Sbenjsc	struct ieee80211_frame *wh;
2566173362Sbenjsc	struct ieee80211_rateset *rs;
2567173362Sbenjsc	struct ieee80211_channel *c;
2568173362Sbenjsc	enum ieee80211_phymode mode;
2569173362Sbenjsc	uint8_t *frm;
2570177043Sthompsa	int nrates, pktlen, error, i, nssid;
2571173362Sbenjsc	bus_addr_t physaddr;
2572173362Sbenjsc
2573173362Sbenjsc	desc = &ring->desc[ring->cur];
2574173362Sbenjsc	data = &ring->data[ring->cur];
2575173362Sbenjsc
2576173362Sbenjsc	data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2577173362Sbenjsc	if (data->m == NULL) {
2578173362Sbenjsc		device_printf(sc->sc_dev,
2579173362Sbenjsc		    "could not allocate mbuf for scan command\n");
2580173362Sbenjsc		return ENOMEM;
2581173362Sbenjsc	}
2582173362Sbenjsc
2583173362Sbenjsc	cmd = mtod(data->m, struct wpi_tx_cmd *);
2584173362Sbenjsc	cmd->code = WPI_CMD_SCAN;
2585173362Sbenjsc	cmd->flags = 0;
2586173362Sbenjsc	cmd->qid = ring->qid;
2587173362Sbenjsc	cmd->idx = ring->cur;
2588173362Sbenjsc
2589173362Sbenjsc	hdr = (struct wpi_scan_hdr *)cmd->data;
2590173362Sbenjsc	memset(hdr, 0, sizeof(struct wpi_scan_hdr));
2591173362Sbenjsc
2592173362Sbenjsc	/*
2593173362Sbenjsc	 * Move to the next channel if no packets are received within 5 msecs
2594173362Sbenjsc	 * after sending the probe request (this helps to reduce the duration
2595173362Sbenjsc	 * of active scans).
2596173362Sbenjsc	 */
2597173362Sbenjsc	hdr->quiet = htole16(5);
2598173362Sbenjsc	hdr->threshold = htole16(1);
2599173362Sbenjsc
2600173362Sbenjsc	if (IEEE80211_IS_CHAN_A(ic->ic_curchan)) {
2601173362Sbenjsc		/* send probe requests at 6Mbps */
2602173362Sbenjsc		hdr->tx.rate = wpi_ridx_to_plcp[WPI_OFDM6];
2603173362Sbenjsc
2604173362Sbenjsc		/* Enable crc checking */
2605173362Sbenjsc		hdr->promotion = htole16(1);
2606173362Sbenjsc	} else {
2607173362Sbenjsc		hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO);
2608173362Sbenjsc		/* send probe requests at 1Mbps */
2609173362Sbenjsc		hdr->tx.rate = wpi_ridx_to_plcp[WPI_CCK1];
2610173362Sbenjsc	}
2611173362Sbenjsc	hdr->tx.id = WPI_ID_BROADCAST;
2612173362Sbenjsc	hdr->tx.lifetime = htole32(WPI_LIFETIME_INFINITE);
2613173362Sbenjsc	hdr->tx.flags = htole32(WPI_TX_AUTO_SEQ);
2614173362Sbenjsc
2615178354Ssam	memset(hdr->scan_essids, 0, sizeof(hdr->scan_essids));
2616177043Sthompsa	nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS);
2617178354Ssam	for (i = 0; i < nssid; i++) {
2618177043Sthompsa		hdr->scan_essids[i].id = IEEE80211_ELEMID_SSID;
2619177043Sthompsa		hdr->scan_essids[i].esslen = MIN(ss->ss_ssid[i].len, 32);
2620177043Sthompsa		memcpy(hdr->scan_essids[i].essid, ss->ss_ssid[i].ssid,
2621177043Sthompsa		    hdr->scan_essids[i].esslen);
2622179957Sthompsa#ifdef WPI_DEBUG
2623177043Sthompsa		if (wpi_debug & WPI_DEBUG_SCANNING) {
2624177043Sthompsa			printf("Scanning Essid: ");
2625178354Ssam			ieee80211_print_essid(hdr->scan_essids[i].essid,
2626178354Ssam			    hdr->scan_essids[i].esslen);
2627177043Sthompsa			printf("\n");
2628177043Sthompsa		}
2629179957Sthompsa#endif
2630173362Sbenjsc	}
2631173362Sbenjsc
2632173362Sbenjsc	/*
2633173362Sbenjsc	 * Build a probe request frame.  Most of the following code is a
2634173362Sbenjsc	 * copy & paste of what is done in net80211.
2635173362Sbenjsc	 */
2636173362Sbenjsc	wh = (struct ieee80211_frame *)&hdr->scan_essids[4];
2637173362Sbenjsc	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2638173362Sbenjsc		IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2639173362Sbenjsc	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2640173362Sbenjsc	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2641190526Ssam	IEEE80211_ADDR_COPY(wh->i_addr2, IF_LLADDR(ifp));
2642173362Sbenjsc	IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
2643173362Sbenjsc	*(u_int16_t *)&wh->i_dur[0] = 0;	/* filled by h/w */
2644173362Sbenjsc	*(u_int16_t *)&wh->i_seq[0] = 0;	/* filled by h/w */
2645173362Sbenjsc
2646173362Sbenjsc	frm = (uint8_t *)(wh + 1);
2647173362Sbenjsc
2648173362Sbenjsc	/* add essid IE, the hardware will fill this in for us */
2649173362Sbenjsc	*frm++ = IEEE80211_ELEMID_SSID;
2650173362Sbenjsc	*frm++ = 0;
2651173362Sbenjsc
2652173362Sbenjsc	mode = ieee80211_chan2mode(ic->ic_curchan);
2653173362Sbenjsc	rs = &ic->ic_sup_rates[mode];
2654173362Sbenjsc
2655173362Sbenjsc	/* add supported rates IE */
2656173362Sbenjsc	*frm++ = IEEE80211_ELEMID_RATES;
2657173362Sbenjsc	nrates = rs->rs_nrates;
2658173362Sbenjsc	if (nrates > IEEE80211_RATE_SIZE)
2659173362Sbenjsc		nrates = IEEE80211_RATE_SIZE;
2660173362Sbenjsc	*frm++ = nrates;
2661173362Sbenjsc	memcpy(frm, rs->rs_rates, nrates);
2662173362Sbenjsc	frm += nrates;
2663173362Sbenjsc
2664173362Sbenjsc	/* add supported xrates IE */
2665173362Sbenjsc	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2666173362Sbenjsc		nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2667173362Sbenjsc		*frm++ = IEEE80211_ELEMID_XRATES;
2668173362Sbenjsc		*frm++ = nrates;
2669173362Sbenjsc		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2670173362Sbenjsc		frm += nrates;
2671173362Sbenjsc	}
2672173362Sbenjsc
2673173362Sbenjsc	/* setup length of probe request */
2674173362Sbenjsc	hdr->tx.len = htole16(frm - (uint8_t *)wh);
2675173362Sbenjsc
2676173362Sbenjsc	/*
2677173362Sbenjsc	 * Construct information about the channel that we
2678173362Sbenjsc	 * want to scan. The firmware expects this to be directly
2679173362Sbenjsc	 * after the scan probe request
2680173362Sbenjsc	 */
2681173362Sbenjsc	c = ic->ic_curchan;
2682173362Sbenjsc	chan = (struct wpi_scan_chan *)frm;
2683173362Sbenjsc	chan->chan = ieee80211_chan2ieee(ic, c);
2684173362Sbenjsc	chan->flags = 0;
2685173362Sbenjsc	if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
2686173362Sbenjsc		chan->flags |= WPI_CHAN_ACTIVE;
2687178354Ssam		if (nssid != 0)
2688173362Sbenjsc			chan->flags |= WPI_CHAN_DIRECT;
2689173362Sbenjsc	}
2690173362Sbenjsc	chan->gain_dsp = 0x6e; /* Default level */
2691173362Sbenjsc	if (IEEE80211_IS_CHAN_5GHZ(c)) {
2692173362Sbenjsc		chan->active = htole16(10);
2693178354Ssam		chan->passive = htole16(ss->ss_maxdwell);
2694173362Sbenjsc		chan->gain_radio = 0x3b;
2695173362Sbenjsc	} else {
2696173362Sbenjsc		chan->active = htole16(20);
2697178354Ssam		chan->passive = htole16(ss->ss_maxdwell);
2698173362Sbenjsc		chan->gain_radio = 0x28;
2699173362Sbenjsc	}
2700173362Sbenjsc
2701173362Sbenjsc	DPRINTFN(WPI_DEBUG_SCANNING,
2702173362Sbenjsc	    ("Scanning %u Passive: %d\n",
2703173362Sbenjsc	     chan->chan,
2704173362Sbenjsc	     c->ic_flags & IEEE80211_CHAN_PASSIVE));
2705173362Sbenjsc
2706173362Sbenjsc	hdr->nchan++;
2707173362Sbenjsc	chan++;
2708173362Sbenjsc
2709173362Sbenjsc	frm += sizeof (struct wpi_scan_chan);
2710173362Sbenjsc#if 0
2711173362Sbenjsc	// XXX All Channels....
2712173362Sbenjsc	for (c  = &ic->ic_channels[1];
2713173362Sbenjsc	     c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
2714173362Sbenjsc		if ((c->ic_flags & ic->ic_curchan->ic_flags) != ic->ic_curchan->ic_flags)
2715173362Sbenjsc			continue;
2716173362Sbenjsc
2717173362Sbenjsc		chan->chan = ieee80211_chan2ieee(ic, c);
2718173362Sbenjsc		chan->flags = 0;
2719173362Sbenjsc		if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
2720173362Sbenjsc		    chan->flags |= WPI_CHAN_ACTIVE;
2721173362Sbenjsc		    if (ic->ic_des_ssid[0].len != 0)
2722173362Sbenjsc			chan->flags |= WPI_CHAN_DIRECT;
2723173362Sbenjsc		}
2724173362Sbenjsc		chan->gain_dsp = 0x6e; /* Default level */
2725173362Sbenjsc		if (IEEE80211_IS_CHAN_5GHZ(c)) {
2726173362Sbenjsc			chan->active = htole16(10);
2727173362Sbenjsc			chan->passive = htole16(110);
2728173362Sbenjsc			chan->gain_radio = 0x3b;
2729173362Sbenjsc		} else {
2730173362Sbenjsc			chan->active = htole16(20);
2731173362Sbenjsc			chan->passive = htole16(120);
2732173362Sbenjsc			chan->gain_radio = 0x28;
2733173362Sbenjsc		}
2734173362Sbenjsc
2735173362Sbenjsc		DPRINTFN(WPI_DEBUG_SCANNING,
2736173362Sbenjsc			 ("Scanning %u Passive: %d\n",
2737173362Sbenjsc			  chan->chan,
2738173362Sbenjsc			  c->ic_flags & IEEE80211_CHAN_PASSIVE));
2739173362Sbenjsc
2740173362Sbenjsc		hdr->nchan++;
2741173362Sbenjsc		chan++;
2742173362Sbenjsc
2743173362Sbenjsc		frm += sizeof (struct wpi_scan_chan);
2744173362Sbenjsc	}
2745173362Sbenjsc#endif
2746173362Sbenjsc
2747173362Sbenjsc	hdr->len = htole16(frm - (uint8_t *)hdr);
2748173362Sbenjsc	pktlen = frm - (uint8_t *)cmd;
2749173362Sbenjsc
2750173362Sbenjsc	error = bus_dmamap_load(ring->data_dmat, data->map, cmd, pktlen,
2751173362Sbenjsc	    wpi_dma_map_addr, &physaddr, BUS_DMA_NOWAIT);
2752173362Sbenjsc	if (error != 0) {
2753173362Sbenjsc		device_printf(sc->sc_dev, "could not map scan command\n");
2754173362Sbenjsc		m_freem(data->m);
2755173362Sbenjsc		data->m = NULL;
2756173362Sbenjsc		return error;
2757173362Sbenjsc	}
2758173362Sbenjsc
2759173362Sbenjsc	desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24);
2760173362Sbenjsc	desc->segs[0].addr = htole32(physaddr);
2761173362Sbenjsc	desc->segs[0].len  = htole32(pktlen);
2762173362Sbenjsc
2763173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2764173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
2765173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2766173362Sbenjsc
2767173362Sbenjsc	/* kick cmd ring */
2768173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2769173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2770173362Sbenjsc
2771177043Sthompsa	sc->sc_scan_timer = 5;
2772173362Sbenjsc	return 0;	/* will be notified async. of failure/success */
2773173362Sbenjsc}
2774173362Sbenjsc
2775173362Sbenjsc/**
2776173362Sbenjsc * Configure the card to listen to a particular channel, this transisions the
2777173362Sbenjsc * card in to being able to receive frames from remote devices.
2778173362Sbenjsc */
2779173362Sbenjscstatic int
2780173362Sbenjscwpi_config(struct wpi_softc *sc)
2781173362Sbenjsc{
2782178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2783178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2784173362Sbenjsc	struct wpi_power power;
2785173362Sbenjsc	struct wpi_bluetooth bluetooth;
2786173362Sbenjsc	struct wpi_node_info node;
2787173362Sbenjsc	int error;
2788173362Sbenjsc
2789173362Sbenjsc	/* set power mode */
2790173362Sbenjsc	memset(&power, 0, sizeof power);
2791173362Sbenjsc	power.flags = htole32(WPI_POWER_CAM|0x8);
2792173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0);
2793173362Sbenjsc	if (error != 0) {
2794173362Sbenjsc		device_printf(sc->sc_dev, "could not set power mode\n");
2795173362Sbenjsc		return error;
2796173362Sbenjsc	}
2797173362Sbenjsc
2798173362Sbenjsc	/* configure bluetooth coexistence */
2799173362Sbenjsc	memset(&bluetooth, 0, sizeof bluetooth);
2800173362Sbenjsc	bluetooth.flags = 3;
2801173362Sbenjsc	bluetooth.lead = 0xaa;
2802173362Sbenjsc	bluetooth.kill = 1;
2803173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
2804173362Sbenjsc	    0);
2805173362Sbenjsc	if (error != 0) {
2806173362Sbenjsc		device_printf(sc->sc_dev,
2807173362Sbenjsc		    "could not configure bluetooth coexistence\n");
2808173362Sbenjsc		return error;
2809173362Sbenjsc	}
2810173362Sbenjsc
2811173362Sbenjsc	/* configure adapter */
2812173362Sbenjsc	memset(&sc->config, 0, sizeof (struct wpi_config));
2813190526Ssam	IEEE80211_ADDR_COPY(sc->config.myaddr, IF_LLADDR(ifp));
2814173362Sbenjsc	/*set default channel*/
2815173362Sbenjsc	sc->config.chan = htole16(ieee80211_chan2ieee(ic, ic->ic_curchan));
2816173362Sbenjsc	sc->config.flags = htole32(WPI_CONFIG_TSF);
2817173362Sbenjsc	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2818173362Sbenjsc		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2819173362Sbenjsc		    WPI_CONFIG_24GHZ);
2820173362Sbenjsc	}
2821173362Sbenjsc	sc->config.filter = 0;
2822173362Sbenjsc	switch (ic->ic_opmode) {
2823173362Sbenjsc	case IEEE80211_M_STA:
2824173362Sbenjsc	case IEEE80211_M_WDS:	/* No know setup, use STA for now */
2825173362Sbenjsc		sc->config.mode = WPI_MODE_STA;
2826173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_MULTICAST);
2827173362Sbenjsc		break;
2828173362Sbenjsc	case IEEE80211_M_IBSS:
2829173362Sbenjsc	case IEEE80211_M_AHDEMO:
2830173362Sbenjsc		sc->config.mode = WPI_MODE_IBSS;
2831173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_BEACON |
2832173362Sbenjsc					     WPI_FILTER_MULTICAST);
2833173362Sbenjsc		break;
2834173362Sbenjsc	case IEEE80211_M_HOSTAP:
2835173362Sbenjsc		sc->config.mode = WPI_MODE_HOSTAP;
2836173362Sbenjsc		break;
2837173362Sbenjsc	case IEEE80211_M_MONITOR:
2838173362Sbenjsc		sc->config.mode = WPI_MODE_MONITOR;
2839173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_MULTICAST |
2840173362Sbenjsc			WPI_FILTER_CTL | WPI_FILTER_PROMISC);
2841173362Sbenjsc		break;
2842173362Sbenjsc	}
2843173362Sbenjsc	sc->config.cck_mask  = 0x0f;	/* not yet negotiated */
2844173362Sbenjsc	sc->config.ofdm_mask = 0xff;	/* not yet negotiated */
2845173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2846173362Sbenjsc		sizeof (struct wpi_config), 0);
2847173362Sbenjsc	if (error != 0) {
2848173362Sbenjsc		device_printf(sc->sc_dev, "configure command failed\n");
2849173362Sbenjsc		return error;
2850173362Sbenjsc	}
2851173362Sbenjsc
2852173362Sbenjsc	/* configuration has changed, set Tx power accordingly */
2853177043Sthompsa	if ((error = wpi_set_txpower(sc, ic->ic_curchan, 0)) != 0) {
2854173362Sbenjsc	    device_printf(sc->sc_dev, "could not set Tx power\n");
2855173362Sbenjsc	    return error;
2856173362Sbenjsc	}
2857173362Sbenjsc
2858173362Sbenjsc	/* add broadcast node */
2859173362Sbenjsc	memset(&node, 0, sizeof node);
2860173362Sbenjsc	IEEE80211_ADDR_COPY(node.bssid, ifp->if_broadcastaddr);
2861173362Sbenjsc	node.id = WPI_ID_BROADCAST;
2862173362Sbenjsc	node.rate = wpi_plcp_signal(2);
2863173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
2864173362Sbenjsc	if (error != 0) {
2865173362Sbenjsc		device_printf(sc->sc_dev, "could not add broadcast node\n");
2866173362Sbenjsc		return error;
2867173362Sbenjsc	}
2868173362Sbenjsc
2869173362Sbenjsc	/* Setup rate scalling */
2870173362Sbenjsc	error = wpi_mrr_setup(sc);
2871173362Sbenjsc	if (error != 0) {
2872173362Sbenjsc		device_printf(sc->sc_dev, "could not setup MRR\n");
2873173362Sbenjsc		return error;
2874173362Sbenjsc	}
2875173362Sbenjsc
2876173362Sbenjsc	return 0;
2877173362Sbenjsc}
2878173362Sbenjsc
2879173362Sbenjscstatic void
2880173362Sbenjscwpi_stop_master(struct wpi_softc *sc)
2881173362Sbenjsc{
2882173362Sbenjsc	uint32_t tmp;
2883173362Sbenjsc	int ntries;
2884173362Sbenjsc
2885173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,("Disabling Firmware execution\n"));
2886173362Sbenjsc
2887173362Sbenjsc	tmp = WPI_READ(sc, WPI_RESET);
2888173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER | WPI_NEVO_RESET);
2889173362Sbenjsc
2890173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
2891173362Sbenjsc	if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP)
2892173362Sbenjsc		return;	/* already asleep */
2893173362Sbenjsc
2894173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
2895173362Sbenjsc		if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED)
2896173362Sbenjsc			break;
2897173362Sbenjsc		DELAY(10);
2898173362Sbenjsc	}
2899173362Sbenjsc	if (ntries == 100) {
2900173362Sbenjsc		device_printf(sc->sc_dev, "timeout waiting for master\n");
2901173362Sbenjsc	}
2902173362Sbenjsc}
2903173362Sbenjsc
2904173362Sbenjscstatic int
2905173362Sbenjscwpi_power_up(struct wpi_softc *sc)
2906173362Sbenjsc{
2907173362Sbenjsc	uint32_t tmp;
2908173362Sbenjsc	int ntries;
2909173362Sbenjsc
2910173362Sbenjsc	wpi_mem_lock(sc);
2911173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_POWER);
2912173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000);
2913173362Sbenjsc	wpi_mem_unlock(sc);
2914173362Sbenjsc
2915173362Sbenjsc	for (ntries = 0; ntries < 5000; ntries++) {
2916173362Sbenjsc		if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED)
2917173362Sbenjsc			break;
2918173362Sbenjsc		DELAY(10);
2919173362Sbenjsc	}
2920173362Sbenjsc	if (ntries == 5000) {
2921173362Sbenjsc		device_printf(sc->sc_dev,
2922173362Sbenjsc		    "timeout waiting for NIC to power up\n");
2923173362Sbenjsc		return ETIMEDOUT;
2924173362Sbenjsc	}
2925173362Sbenjsc	return 0;
2926173362Sbenjsc}
2927173362Sbenjsc
2928173362Sbenjscstatic int
2929173362Sbenjscwpi_reset(struct wpi_softc *sc)
2930173362Sbenjsc{
2931173362Sbenjsc	uint32_t tmp;
2932173362Sbenjsc	int ntries;
2933173362Sbenjsc
2934173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,
2935173362Sbenjsc	    ("Resetting the card - clearing any uploaded firmware\n"));
2936173362Sbenjsc
2937173362Sbenjsc	/* clear any pending interrupts */
2938173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
2939173362Sbenjsc
2940173362Sbenjsc	tmp = WPI_READ(sc, WPI_PLL_CTL);
2941173362Sbenjsc	WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT);
2942173362Sbenjsc
2943173362Sbenjsc	tmp = WPI_READ(sc, WPI_CHICKEN);
2944173362Sbenjsc	WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS);
2945173362Sbenjsc
2946173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
2947173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT);
2948173362Sbenjsc
2949173362Sbenjsc	/* wait for clock stabilization */
2950173362Sbenjsc	for (ntries = 0; ntries < 25000; ntries++) {
2951173362Sbenjsc		if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK)
2952173362Sbenjsc			break;
2953173362Sbenjsc		DELAY(10);
2954173362Sbenjsc	}
2955173362Sbenjsc	if (ntries == 25000) {
2956173362Sbenjsc		device_printf(sc->sc_dev,
2957173362Sbenjsc		    "timeout waiting for clock stabilization\n");
2958173362Sbenjsc		return ETIMEDOUT;
2959173362Sbenjsc	}
2960173362Sbenjsc
2961173362Sbenjsc	/* initialize EEPROM */
2962173362Sbenjsc	tmp = WPI_READ(sc, WPI_EEPROM_STATUS);
2963173362Sbenjsc
2964173362Sbenjsc	if ((tmp & WPI_EEPROM_VERSION) == 0) {
2965173362Sbenjsc		device_printf(sc->sc_dev, "EEPROM not found\n");
2966173362Sbenjsc		return EIO;
2967173362Sbenjsc	}
2968173362Sbenjsc	WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED);
2969173362Sbenjsc
2970173362Sbenjsc	return 0;
2971173362Sbenjsc}
2972173362Sbenjsc
2973173362Sbenjscstatic void
2974173362Sbenjscwpi_hw_config(struct wpi_softc *sc)
2975173362Sbenjsc{
2976173362Sbenjsc	uint32_t rev, hw;
2977173362Sbenjsc
2978173362Sbenjsc	/* voodoo from the Linux "driver".. */
2979173362Sbenjsc	hw = WPI_READ(sc, WPI_HWCONFIG);
2980173362Sbenjsc
2981173362Sbenjsc	rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1);
2982173362Sbenjsc	if ((rev & 0xc0) == 0x40)
2983173362Sbenjsc		hw |= WPI_HW_ALM_MB;
2984173362Sbenjsc	else if (!(rev & 0x80))
2985173362Sbenjsc		hw |= WPI_HW_ALM_MM;
2986173362Sbenjsc
2987173362Sbenjsc	if (sc->cap == 0x80)
2988173362Sbenjsc		hw |= WPI_HW_SKU_MRC;
2989173362Sbenjsc
2990173362Sbenjsc	hw &= ~WPI_HW_REV_D;
2991173362Sbenjsc	if ((le16toh(sc->rev) & 0xf0) == 0xd0)
2992173362Sbenjsc		hw |= WPI_HW_REV_D;
2993173362Sbenjsc
2994173362Sbenjsc	if (sc->type > 1)
2995173362Sbenjsc		hw |= WPI_HW_TYPE_B;
2996173362Sbenjsc
2997173362Sbenjsc	WPI_WRITE(sc, WPI_HWCONFIG, hw);
2998173362Sbenjsc}
2999173362Sbenjsc
3000173362Sbenjscstatic void
3001177043Sthompsawpi_rfkill_resume(struct wpi_softc *sc)
3002177043Sthompsa{
3003177043Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3004178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3005178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3006177043Sthompsa	int ntries;
3007177043Sthompsa
3008177043Sthompsa	/* enable firmware again */
3009177043Sthompsa	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3010177043Sthompsa	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
3011177043Sthompsa
3012177043Sthompsa	/* wait for thermal sensors to calibrate */
3013177043Sthompsa	for (ntries = 0; ntries < 1000; ntries++) {
3014177043Sthompsa		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
3015177043Sthompsa			break;
3016177043Sthompsa		DELAY(10);
3017177043Sthompsa	}
3018177043Sthompsa
3019177043Sthompsa	if (ntries == 1000) {
3020177043Sthompsa		device_printf(sc->sc_dev,
3021177043Sthompsa		    "timeout waiting for thermal calibration\n");
3022177043Sthompsa		WPI_UNLOCK(sc);
3023177043Sthompsa		return;
3024177043Sthompsa	}
3025177043Sthompsa	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d\n", sc->temp));
3026177043Sthompsa
3027177043Sthompsa	if (wpi_config(sc) != 0) {
3028177043Sthompsa		device_printf(sc->sc_dev, "device config failed\n");
3029177043Sthompsa		WPI_UNLOCK(sc);
3030177043Sthompsa		return;
3031177043Sthompsa	}
3032177043Sthompsa
3033177043Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3034177043Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3035177043Sthompsa	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3036177043Sthompsa
3037178354Ssam	if (vap != NULL) {
3038178354Ssam		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
3039178354Ssam			if (vap->iv_opmode != IEEE80211_M_MONITOR) {
3040179037Sthompsa				taskqueue_enqueue(taskqueue_swi,
3041179037Sthompsa				    &sc->sc_bmiss_task);
3042178354Ssam				wpi_set_led(sc, WPI_LED_LINK, 0, 1);
3043178354Ssam			} else
3044178354Ssam				wpi_set_led(sc, WPI_LED_LINK, 5, 5);
3045178354Ssam		} else {
3046178354Ssam			ieee80211_scan_next(vap);
3047177043Sthompsa			wpi_set_led(sc, WPI_LED_LINK, 20, 2);
3048178354Ssam		}
3049177043Sthompsa	}
3050177043Sthompsa
3051177043Sthompsa	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3052177043Sthompsa}
3053177043Sthompsa
3054177043Sthompsastatic void
3055177043Sthompsawpi_init_locked(struct wpi_softc *sc, int force)
3056177043Sthompsa{
3057178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3058173362Sbenjsc	uint32_t tmp;
3059177043Sthompsa	int ntries, qid;
3060173362Sbenjsc
3061173362Sbenjsc	wpi_stop_locked(sc);
3062173362Sbenjsc	(void)wpi_reset(sc);
3063173362Sbenjsc
3064173362Sbenjsc	wpi_mem_lock(sc);
3065173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00);
3066173362Sbenjsc	DELAY(20);
3067173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
3068173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800);
3069173362Sbenjsc	wpi_mem_unlock(sc);
3070173362Sbenjsc
3071173362Sbenjsc	(void)wpi_power_up(sc);
3072173362Sbenjsc	wpi_hw_config(sc);
3073173362Sbenjsc
3074173362Sbenjsc	/* init Rx ring */
3075173362Sbenjsc	wpi_mem_lock(sc);
3076173362Sbenjsc	WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr);
3077173362Sbenjsc	WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr +
3078173362Sbenjsc	    offsetof(struct wpi_shared, next));
3079173362Sbenjsc	WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
3080173362Sbenjsc	WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010);
3081173362Sbenjsc	wpi_mem_unlock(sc);
3082173362Sbenjsc
3083173362Sbenjsc	/* init Tx rings */
3084173362Sbenjsc	wpi_mem_lock(sc);
3085173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */
3086173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_RA, 1);   /* enable RA0 */
3087173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */
3088173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000);
3089173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002);
3090173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MAGIC4, 4);
3091173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MAGIC5, 5);
3092173362Sbenjsc
3093173362Sbenjsc	WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr);
3094173362Sbenjsc	WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5);
3095173362Sbenjsc
3096173362Sbenjsc	for (qid = 0; qid < 6; qid++) {
3097173362Sbenjsc		WPI_WRITE(sc, WPI_TX_CTL(qid), 0);
3098173362Sbenjsc		WPI_WRITE(sc, WPI_TX_BASE(qid), 0);
3099173362Sbenjsc		WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008);
3100173362Sbenjsc	}
3101173362Sbenjsc	wpi_mem_unlock(sc);
3102173362Sbenjsc
3103173362Sbenjsc	/* clear "radio off" and "disable command" bits (reversed logic) */
3104173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3105173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
3106173362Sbenjsc	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3107173362Sbenjsc
3108173362Sbenjsc	/* clear any pending interrupts */
3109173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
3110173362Sbenjsc
3111173362Sbenjsc	/* enable interrupts */
3112173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
3113173362Sbenjsc
3114173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3115173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3116173362Sbenjsc
3117177043Sthompsa	if ((wpi_load_firmware(sc)) != 0) {
3118173362Sbenjsc	    device_printf(sc->sc_dev,
3119173362Sbenjsc		"A problem occurred loading the firmware to the driver\n");
3120173362Sbenjsc	    return;
3121173362Sbenjsc	}
3122173362Sbenjsc
3123173362Sbenjsc	/* At this point the firmware is up and running. If the hardware
3124173362Sbenjsc	 * RF switch is turned off thermal calibration will fail, though
3125173362Sbenjsc	 * the card is still happy to continue to accept commands, catch
3126177043Sthompsa	 * this case and schedule a task to watch for it to be turned on.
3127173362Sbenjsc	 */
3128173362Sbenjsc	wpi_mem_lock(sc);
3129173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_HW_RADIO_OFF);
3130173362Sbenjsc	wpi_mem_unlock(sc);
3131173362Sbenjsc
3132173362Sbenjsc	if (!(tmp & 0x1)) {
3133173362Sbenjsc		sc->flags |= WPI_FLAG_HW_RADIO_OFF;
3134173362Sbenjsc		device_printf(sc->sc_dev,"Radio Transmitter is switched off\n");
3135177043Sthompsa		goto out;
3136173362Sbenjsc	}
3137173362Sbenjsc
3138173362Sbenjsc	/* wait for thermal sensors to calibrate */
3139173362Sbenjsc	for (ntries = 0; ntries < 1000; ntries++) {
3140173362Sbenjsc		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
3141173362Sbenjsc			break;
3142173362Sbenjsc		DELAY(10);
3143173362Sbenjsc	}
3144173362Sbenjsc
3145173362Sbenjsc	if (ntries == 1000) {
3146173362Sbenjsc		device_printf(sc->sc_dev,
3147173362Sbenjsc		    "timeout waiting for thermal sensors calibration\n");
3148173362Sbenjsc		return;
3149173362Sbenjsc	}
3150173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d\n", sc->temp));
3151173362Sbenjsc
3152177043Sthompsa	if (wpi_config(sc) != 0) {
3153177043Sthompsa		device_printf(sc->sc_dev, "device config failed\n");
3154177043Sthompsa		return;
3155177043Sthompsa	}
3156177043Sthompsa
3157173362Sbenjsc	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3158173362Sbenjsc	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3159177043Sthompsaout:
3160177043Sthompsa	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3161173362Sbenjsc}
3162173362Sbenjsc
3163173362Sbenjscstatic void
3164178354Ssamwpi_init(void *arg)
3165173362Sbenjsc{
3166178354Ssam	struct wpi_softc *sc = arg;
3167178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3168178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3169173362Sbenjsc
3170173362Sbenjsc	WPI_LOCK(sc);
3171178354Ssam	wpi_init_locked(sc, 0);
3172173362Sbenjsc	WPI_UNLOCK(sc);
3173173362Sbenjsc
3174178354Ssam	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3175178354Ssam		ieee80211_start_all(ic);		/* start all vaps */
3176173362Sbenjsc}
3177178354Ssam
3178173362Sbenjscstatic void
3179173362Sbenjscwpi_stop_locked(struct wpi_softc *sc)
3180173362Sbenjsc{
3181178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3182173362Sbenjsc	uint32_t tmp;
3183173362Sbenjsc	int ac;
3184173362Sbenjsc
3185177043Sthompsa	sc->sc_tx_timer = 0;
3186177043Sthompsa	sc->sc_scan_timer = 0;
3187173362Sbenjsc	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3188177043Sthompsa	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3189177043Sthompsa	callout_stop(&sc->watchdog_to);
3190177043Sthompsa	callout_stop(&sc->calib_to);
3191173362Sbenjsc
3192177043Sthompsa
3193173362Sbenjsc	/* disable interrupts */
3194173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, 0);
3195173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK);
3196173362Sbenjsc	WPI_WRITE(sc, WPI_INTR_STATUS, 0xff);
3197173362Sbenjsc	WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000);
3198173362Sbenjsc
3199173362Sbenjsc	/* Clear any commands left in the command buffer */
3200173362Sbenjsc	memset(sc->sc_cmd, 0, sizeof(sc->sc_cmd));
3201177043Sthompsa	memset(sc->sc_cmd_arg, 0, sizeof(sc->sc_cmd_arg));
3202177043Sthompsa	sc->sc_cmd_cur = 0;
3203177043Sthompsa	sc->sc_cmd_next = 0;
3204173362Sbenjsc
3205173362Sbenjsc	wpi_mem_lock(sc);
3206173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MODE, 0);
3207173362Sbenjsc	wpi_mem_unlock(sc);
3208173362Sbenjsc
3209173362Sbenjsc	/* reset all Tx rings */
3210173362Sbenjsc	for (ac = 0; ac < 4; ac++)
3211173362Sbenjsc		wpi_reset_tx_ring(sc, &sc->txq[ac]);
3212173362Sbenjsc	wpi_reset_tx_ring(sc, &sc->cmdq);
3213173362Sbenjsc
3214173362Sbenjsc	/* reset Rx ring */
3215173362Sbenjsc	wpi_reset_rx_ring(sc, &sc->rxq);
3216173362Sbenjsc
3217173362Sbenjsc	wpi_mem_lock(sc);
3218173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200);
3219173362Sbenjsc	wpi_mem_unlock(sc);
3220173362Sbenjsc
3221173362Sbenjsc	DELAY(5);
3222173362Sbenjsc
3223173362Sbenjsc	wpi_stop_master(sc);
3224173362Sbenjsc
3225173362Sbenjsc	tmp = WPI_READ(sc, WPI_RESET);
3226173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET);
3227173362Sbenjsc	sc->flags &= ~WPI_FLAG_BUSY;
3228173362Sbenjsc}
3229173362Sbenjsc
3230173362Sbenjscstatic void
3231178354Ssamwpi_stop(struct wpi_softc *sc)
3232173362Sbenjsc{
3233178354Ssam	WPI_LOCK(sc);
3234178354Ssam	wpi_stop_locked(sc);
3235178354Ssam	WPI_UNLOCK(sc);
3236173362Sbenjsc}
3237173362Sbenjsc
3238173362Sbenjscstatic void
3239173362Sbenjscwpi_newassoc(struct ieee80211_node *ni, int isnew)
3240173362Sbenjsc{
3241178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
3242178354Ssam	struct wpi_vap *wvp = WPI_VAP(vap);
3243173362Sbenjsc
3244178354Ssam	ieee80211_amrr_node_init(&wvp->amrr, &WPI_NODE(ni)->amn, ni);
3245173362Sbenjsc}
3246173362Sbenjsc
3247173362Sbenjscstatic void
3248173362Sbenjscwpi_calib_timeout(void *arg)
3249173362Sbenjsc{
3250173362Sbenjsc	struct wpi_softc *sc = arg;
3251178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3252178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3253178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3254173362Sbenjsc	int temp;
3255173362Sbenjsc
3256178354Ssam	if (vap->iv_state != IEEE80211_S_RUN)
3257177043Sthompsa		return;
3258177043Sthompsa
3259173362Sbenjsc	/* update sensor data */
3260173362Sbenjsc	temp = (int)WPI_READ(sc, WPI_TEMPERATURE);
3261173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("Temp in calibration is: %d\n", temp));
3262173362Sbenjsc
3263178354Ssam	wpi_power_calibration(sc, temp);
3264173362Sbenjsc
3265178354Ssam	callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
3266173362Sbenjsc}
3267173362Sbenjsc
3268173362Sbenjsc/*
3269173362Sbenjsc * This function is called periodically (every 60 seconds) to adjust output
3270173362Sbenjsc * power to temperature changes.
3271173362Sbenjsc */
3272173362Sbenjscstatic void
3273173362Sbenjscwpi_power_calibration(struct wpi_softc *sc, int temp)
3274173362Sbenjsc{
3275178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3276178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3277178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3278178354Ssam
3279173362Sbenjsc	/* sanity-check read value */
3280173362Sbenjsc	if (temp < -260 || temp > 25) {
3281173362Sbenjsc		/* this can't be correct, ignore */
3282173362Sbenjsc		DPRINTFN(WPI_DEBUG_TEMP,
3283173362Sbenjsc		    ("out-of-range temperature reported: %d\n", temp));
3284173362Sbenjsc		return;
3285173362Sbenjsc	}
3286173362Sbenjsc
3287173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d->%d\n", sc->temp, temp));
3288173362Sbenjsc
3289173362Sbenjsc	/* adjust Tx power if need be */
3290173362Sbenjsc	if (abs(temp - sc->temp) <= 6)
3291173362Sbenjsc		return;
3292173362Sbenjsc
3293173362Sbenjsc	sc->temp = temp;
3294173362Sbenjsc
3295178354Ssam	if (wpi_set_txpower(sc, vap->iv_bss->ni_chan, 1) != 0) {
3296173362Sbenjsc		/* just warn, too bad for the automatic calibration... */
3297173362Sbenjsc		device_printf(sc->sc_dev,"could not adjust Tx power\n");
3298173362Sbenjsc	}
3299173362Sbenjsc}
3300173362Sbenjsc
3301173362Sbenjsc/**
3302173362Sbenjsc * Read the eeprom to find out what channels are valid for the given
3303173362Sbenjsc * band and update net80211 with what we find.
3304173362Sbenjsc */
3305173362Sbenjscstatic void
3306173362Sbenjscwpi_read_eeprom_channels(struct wpi_softc *sc, int n)
3307173362Sbenjsc{
3308178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3309178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3310173362Sbenjsc	const struct wpi_chan_band *band = &wpi_bands[n];
3311173362Sbenjsc	struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND];
3312179957Sthompsa	struct ieee80211_channel *c;
3313179957Sthompsa	int chan, i, passive;
3314173362Sbenjsc
3315173362Sbenjsc	wpi_read_prom_data(sc, band->addr, channels,
3316173362Sbenjsc	    band->nchan * sizeof (struct wpi_eeprom_chan));
3317173362Sbenjsc
3318173362Sbenjsc	for (i = 0; i < band->nchan; i++) {
3319173362Sbenjsc		if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
3320173362Sbenjsc			DPRINTFN(WPI_DEBUG_HW,
3321173362Sbenjsc			    ("Channel Not Valid: %d, band %d\n",
3322173362Sbenjsc			     band->chan[i],n));
3323173362Sbenjsc			continue;
3324173362Sbenjsc		}
3325173362Sbenjsc
3326173362Sbenjsc		passive = 0;
3327173362Sbenjsc		chan = band->chan[i];
3328179957Sthompsa		c = &ic->ic_channels[ic->ic_nchans++];
3329173362Sbenjsc
3330173362Sbenjsc		/* is active scan allowed on this channel? */
3331173362Sbenjsc		if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) {
3332173362Sbenjsc			passive = IEEE80211_CHAN_PASSIVE;
3333173362Sbenjsc		}
3334173362Sbenjsc
3335173362Sbenjsc		if (n == 0) {	/* 2GHz band */
3336179957Sthompsa			c->ic_ieee = chan;
3337179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3338179957Sthompsa			    IEEE80211_CHAN_2GHZ);
3339179957Sthompsa			c->ic_flags = IEEE80211_CHAN_B | passive;
3340173362Sbenjsc
3341179957Sthompsa			c = &ic->ic_channels[ic->ic_nchans++];
3342179957Sthompsa			c->ic_ieee = chan;
3343179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3344179957Sthompsa			    IEEE80211_CHAN_2GHZ);
3345179957Sthompsa			c->ic_flags = IEEE80211_CHAN_G | passive;
3346179957Sthompsa
3347173362Sbenjsc		} else {	/* 5GHz band */
3348173362Sbenjsc			/*
3349173362Sbenjsc			 * Some 3945ABG adapters support channels 7, 8, 11
3350173362Sbenjsc			 * and 12 in the 2GHz *and* 5GHz bands.
3351173362Sbenjsc			 * Because of limitations in our net80211(9) stack,
3352173362Sbenjsc			 * we can't support these channels in 5GHz band.
3353173362Sbenjsc			 * XXX not true; just need to map to proper frequency
3354173362Sbenjsc			 */
3355173362Sbenjsc			if (chan <= 14)
3356173362Sbenjsc				continue;
3357173362Sbenjsc
3358179957Sthompsa			c->ic_ieee = chan;
3359179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3360179957Sthompsa			    IEEE80211_CHAN_5GHZ);
3361179957Sthompsa			c->ic_flags = IEEE80211_CHAN_A | passive;
3362173362Sbenjsc		}
3363173362Sbenjsc
3364173362Sbenjsc		/* save maximum allowed power for this channel */
3365173362Sbenjsc		sc->maxpwr[chan] = channels[i].maxpwr;
3366173362Sbenjsc
3367173362Sbenjsc#if 0
3368173362Sbenjsc		// XXX We can probably use this an get rid of maxpwr - ben 20070617
3369173362Sbenjsc		ic->ic_channels[chan].ic_maxpower = channels[i].maxpwr;
3370173362Sbenjsc		//ic->ic_channels[chan].ic_minpower...
3371173362Sbenjsc		//ic->ic_channels[chan].ic_maxregtxpower...
3372173362Sbenjsc#endif
3373173362Sbenjsc
3374179957Sthompsa		DPRINTF(("adding chan %d (%dMHz) flags=0x%x maxpwr=%d"
3375179957Sthompsa		    " passive=%d, offset %d\n", chan, c->ic_freq,
3376179957Sthompsa		    channels[i].flags, sc->maxpwr[chan],
3377179957Sthompsa		    (c->ic_flags & IEEE80211_CHAN_PASSIVE) != 0,
3378179957Sthompsa		    ic->ic_nchans));
3379173362Sbenjsc	}
3380173362Sbenjsc}
3381173362Sbenjsc
3382173362Sbenjscstatic void
3383173362Sbenjscwpi_read_eeprom_group(struct wpi_softc *sc, int n)
3384173362Sbenjsc{
3385173362Sbenjsc	struct wpi_power_group *group = &sc->groups[n];
3386173362Sbenjsc	struct wpi_eeprom_group rgroup;
3387173362Sbenjsc	int i;
3388173362Sbenjsc
3389173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup,
3390173362Sbenjsc	    sizeof rgroup);
3391173362Sbenjsc
3392173362Sbenjsc	/* save power group information */
3393173362Sbenjsc	group->chan   = rgroup.chan;
3394173362Sbenjsc	group->maxpwr = rgroup.maxpwr;
3395173362Sbenjsc	/* temperature at which the samples were taken */
3396173362Sbenjsc	group->temp   = (int16_t)le16toh(rgroup.temp);
3397173362Sbenjsc
3398173362Sbenjsc	DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n,
3399173362Sbenjsc		    group->chan, group->maxpwr, group->temp));
3400173362Sbenjsc
3401173362Sbenjsc	for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
3402173362Sbenjsc		group->samples[i].index = rgroup.samples[i].index;
3403173362Sbenjsc		group->samples[i].power = rgroup.samples[i].power;
3404173362Sbenjsc
3405173362Sbenjsc		DPRINTF(("\tsample %d: index=%d power=%d\n", i,
3406173362Sbenjsc			    group->samples[i].index, group->samples[i].power));
3407173362Sbenjsc	}
3408173362Sbenjsc}
3409173362Sbenjsc
3410173362Sbenjsc/*
3411173362Sbenjsc * Update Tx power to match what is defined for channel `c'.
3412173362Sbenjsc */
3413173362Sbenjscstatic int
3414173362Sbenjscwpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async)
3415173362Sbenjsc{
3416178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3417178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3418173362Sbenjsc	struct wpi_power_group *group;
3419173362Sbenjsc	struct wpi_cmd_txpower txpower;
3420173362Sbenjsc	u_int chan;
3421173362Sbenjsc	int i;
3422173362Sbenjsc
3423173362Sbenjsc	/* get channel number */
3424173362Sbenjsc	chan = ieee80211_chan2ieee(ic, c);
3425173362Sbenjsc
3426173362Sbenjsc	/* find the power group to which this channel belongs */
3427173362Sbenjsc	if (IEEE80211_IS_CHAN_5GHZ(c)) {
3428173362Sbenjsc		for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
3429173362Sbenjsc			if (chan <= group->chan)
3430173362Sbenjsc				break;
3431173362Sbenjsc	} else
3432173362Sbenjsc		group = &sc->groups[0];
3433173362Sbenjsc
3434173362Sbenjsc	memset(&txpower, 0, sizeof txpower);
3435173362Sbenjsc	txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1;
3436173362Sbenjsc	txpower.channel = htole16(chan);
3437173362Sbenjsc
3438173362Sbenjsc	/* set Tx power for all OFDM and CCK rates */
3439173362Sbenjsc	for (i = 0; i <= 11 ; i++) {
3440173362Sbenjsc		/* retrieve Tx power for this channel/rate combination */
3441173362Sbenjsc		int idx = wpi_get_power_index(sc, group, c,
3442173362Sbenjsc		    wpi_ridx_to_rate[i]);
3443173362Sbenjsc
3444173362Sbenjsc		txpower.rates[i].rate = wpi_ridx_to_plcp[i];
3445173362Sbenjsc
3446173362Sbenjsc		if (IEEE80211_IS_CHAN_5GHZ(c)) {
3447173362Sbenjsc			txpower.rates[i].gain_radio = wpi_rf_gain_5ghz[idx];
3448173362Sbenjsc			txpower.rates[i].gain_dsp = wpi_dsp_gain_5ghz[idx];
3449173362Sbenjsc		} else {
3450173362Sbenjsc			txpower.rates[i].gain_radio = wpi_rf_gain_2ghz[idx];
3451173362Sbenjsc			txpower.rates[i].gain_dsp = wpi_dsp_gain_2ghz[idx];
3452173362Sbenjsc		}
3453173362Sbenjsc		DPRINTFN(WPI_DEBUG_TEMP,("chan %d/rate %d: power index %d\n",
3454173362Sbenjsc			    chan, wpi_ridx_to_rate[i], idx));
3455173362Sbenjsc	}
3456173362Sbenjsc
3457173362Sbenjsc	return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async);
3458173362Sbenjsc}
3459173362Sbenjsc
3460173362Sbenjsc/*
3461173362Sbenjsc * Determine Tx power index for a given channel/rate combination.
3462173362Sbenjsc * This takes into account the regulatory information from EEPROM and the
3463173362Sbenjsc * current temperature.
3464173362Sbenjsc */
3465173362Sbenjscstatic int
3466173362Sbenjscwpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
3467173362Sbenjsc    struct ieee80211_channel *c, int rate)
3468173362Sbenjsc{
3469173362Sbenjsc/* fixed-point arithmetic division using a n-bit fractional part */
3470173362Sbenjsc#define fdivround(a, b, n)      \
3471173362Sbenjsc	((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3472173362Sbenjsc
3473173362Sbenjsc/* linear interpolation */
3474173362Sbenjsc#define interpolate(x, x1, y1, x2, y2, n)       \
3475173362Sbenjsc	((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3476173362Sbenjsc
3477178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3478178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3479173362Sbenjsc	struct wpi_power_sample *sample;
3480173362Sbenjsc	int pwr, idx;
3481173362Sbenjsc	u_int chan;
3482173362Sbenjsc
3483173362Sbenjsc	/* get channel number */
3484173362Sbenjsc	chan = ieee80211_chan2ieee(ic, c);
3485173362Sbenjsc
3486173362Sbenjsc	/* default power is group's maximum power - 3dB */
3487173362Sbenjsc	pwr = group->maxpwr / 2;
3488173362Sbenjsc
3489173362Sbenjsc	/* decrease power for highest OFDM rates to reduce distortion */
3490173362Sbenjsc	switch (rate) {
3491173362Sbenjsc		case 72:	/* 36Mb/s */
3492173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 :  5;
3493173362Sbenjsc			break;
3494173362Sbenjsc		case 96:	/* 48Mb/s */
3495173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10;
3496173362Sbenjsc			break;
3497173362Sbenjsc		case 108:	/* 54Mb/s */
3498173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12;
3499173362Sbenjsc			break;
3500173362Sbenjsc	}
3501173362Sbenjsc
3502173362Sbenjsc	/* never exceed channel's maximum allowed Tx power */
3503173362Sbenjsc	pwr = min(pwr, sc->maxpwr[chan]);
3504173362Sbenjsc
3505173362Sbenjsc	/* retrieve power index into gain tables from samples */
3506173362Sbenjsc	for (sample = group->samples; sample < &group->samples[3]; sample++)
3507173362Sbenjsc		if (pwr > sample[1].power)
3508173362Sbenjsc			break;
3509173362Sbenjsc	/* fixed-point linear interpolation using a 19-bit fractional part */
3510173362Sbenjsc	idx = interpolate(pwr, sample[0].power, sample[0].index,
3511173362Sbenjsc	    sample[1].power, sample[1].index, 19);
3512173362Sbenjsc
3513173362Sbenjsc	/*
3514173362Sbenjsc	 *  Adjust power index based on current temperature
3515173362Sbenjsc	 *	- if colder than factory-calibrated: decreate output power
3516173362Sbenjsc	 *	- if warmer than factory-calibrated: increase output power
3517173362Sbenjsc	 */
3518173362Sbenjsc	idx -= (sc->temp - group->temp) * 11 / 100;
3519173362Sbenjsc
3520173362Sbenjsc	/* decrease power for CCK rates (-5dB) */
3521173362Sbenjsc	if (!WPI_RATE_IS_OFDM(rate))
3522173362Sbenjsc		idx += 10;
3523173362Sbenjsc
3524173362Sbenjsc	/* keep power index in a valid range */
3525173362Sbenjsc	if (idx < 0)
3526173362Sbenjsc		return 0;
3527173362Sbenjsc	if (idx > WPI_MAX_PWR_INDEX)
3528173362Sbenjsc		return WPI_MAX_PWR_INDEX;
3529173362Sbenjsc	return idx;
3530173362Sbenjsc
3531173362Sbenjsc#undef interpolate
3532173362Sbenjsc#undef fdivround
3533173362Sbenjsc}
3534173362Sbenjsc
3535173362Sbenjsc/**
3536173362Sbenjsc * Called by net80211 framework to indicate that a scan
3537173362Sbenjsc * is starting. This function doesn't actually do the scan,
3538173362Sbenjsc * wpi_scan_curchan starts things off. This function is more
3539173362Sbenjsc * of an early warning from the framework we should get ready
3540173362Sbenjsc * for the scan.
3541173362Sbenjsc */
3542173362Sbenjscstatic void
3543173362Sbenjscwpi_scan_start(struct ieee80211com *ic)
3544173362Sbenjsc{
3545173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
3546173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3547173362Sbenjsc
3548177043Sthompsa	wpi_queue_cmd(sc, WPI_SCAN_START, 0, WPI_QUEUE_NORMAL);
3549173362Sbenjsc}
3550173362Sbenjsc
3551173362Sbenjsc/**
3552173362Sbenjsc * Called by the net80211 framework, indicates that the
3553173362Sbenjsc * scan has ended. If there is a scan in progress on the card
3554173362Sbenjsc * then it should be aborted.
3555173362Sbenjsc */
3556173362Sbenjscstatic void
3557173362Sbenjscwpi_scan_end(struct ieee80211com *ic)
3558173362Sbenjsc{
3559173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
3560173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3561173362Sbenjsc
3562177043Sthompsa	wpi_queue_cmd(sc, WPI_SCAN_STOP, 0, WPI_QUEUE_NORMAL);
3563173362Sbenjsc}
3564173362Sbenjsc
3565173362Sbenjsc/**
3566173362Sbenjsc * Called by the net80211 framework to indicate to the driver
3567173362Sbenjsc * that the channel should be changed
3568173362Sbenjsc */
3569173362Sbenjscstatic void
3570173362Sbenjscwpi_set_channel(struct ieee80211com *ic)
3571173362Sbenjsc{
3572173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
3573173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3574173362Sbenjsc
3575177043Sthompsa	/*
3576177043Sthompsa	 * Only need to set the channel in Monitor mode. AP scanning and auth
3577177043Sthompsa	 * are already taken care of by their respective firmware commands.
3578177043Sthompsa	 */
3579177043Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR)
3580177043Sthompsa		wpi_queue_cmd(sc, WPI_SET_CHAN, 0, WPI_QUEUE_NORMAL);
3581173362Sbenjsc}
3582173362Sbenjsc
3583173362Sbenjsc/**
3584173362Sbenjsc * Called by net80211 to indicate that we need to scan the current
3585173362Sbenjsc * channel. The channel is previously be set via the wpi_set_channel
3586173362Sbenjsc * callback.
3587173362Sbenjsc */
3588173362Sbenjscstatic void
3589178354Ssamwpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
3590173362Sbenjsc{
3591178354Ssam	struct ieee80211vap *vap = ss->ss_vap;
3592178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
3593173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3594173362Sbenjsc
3595177043Sthompsa	wpi_queue_cmd(sc, WPI_SCAN_CURCHAN, 0, WPI_QUEUE_NORMAL);
3596173362Sbenjsc}
3597173362Sbenjsc
3598173362Sbenjsc/**
3599173362Sbenjsc * Called by the net80211 framework to indicate
3600173362Sbenjsc * the minimum dwell time has been met, terminate the scan.
3601173362Sbenjsc * We don't actually terminate the scan as the firmware will notify
3602173362Sbenjsc * us when it's finished and we have no way to interrupt it.
3603173362Sbenjsc */
3604173362Sbenjscstatic void
3605178354Ssamwpi_scan_mindwell(struct ieee80211_scan_state *ss)
3606173362Sbenjsc{
3607173362Sbenjsc	/* NB: don't try to abort scan; wait for firmware to finish */
3608173362Sbenjsc}
3609173362Sbenjsc
3610173362Sbenjsc/**
3611173362Sbenjsc * The ops function is called to perform some actual work.
3612173362Sbenjsc * because we can't sleep from any of the ic callbacks, we queue an
3613173362Sbenjsc * op task with wpi_queue_cmd and have the taskqueue process that task.
3614173362Sbenjsc * The task that gets cued is a op task, which ends up calling this function.
3615173362Sbenjsc */
3616173362Sbenjscstatic void
3617177043Sthompsawpi_ops(void *arg0, int pending)
3618173362Sbenjsc{
3619177043Sthompsa	struct wpi_softc *sc = arg0;
3620178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3621178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3622177043Sthompsa	int cmd, arg, error;
3623178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3624173362Sbenjsc
3625173362Sbenjscagain:
3626173362Sbenjsc	WPI_CMD_LOCK(sc);
3627173362Sbenjsc	cmd = sc->sc_cmd[sc->sc_cmd_cur];
3628177043Sthompsa	arg = sc->sc_cmd_arg[sc->sc_cmd_cur];
3629173362Sbenjsc
3630173362Sbenjsc	if (cmd == 0) {
3631173362Sbenjsc		/* No more commands to process */
3632173362Sbenjsc		WPI_CMD_UNLOCK(sc);
3633173362Sbenjsc		return;
3634173362Sbenjsc	}
3635173362Sbenjsc	sc->sc_cmd[sc->sc_cmd_cur] = 0; /* free the slot */
3636177043Sthompsa	sc->sc_cmd_arg[sc->sc_cmd_cur] = 0; /* free the slot */
3637173362Sbenjsc	sc->sc_cmd_cur = (sc->sc_cmd_cur + 1) % WPI_CMD_MAXOPS;
3638173362Sbenjsc	WPI_CMD_UNLOCK(sc);
3639173362Sbenjsc	WPI_LOCK(sc);
3640173362Sbenjsc
3641177043Sthompsa	DPRINTFN(WPI_DEBUG_OPS,("wpi_ops: command: %d\n", cmd));
3642177043Sthompsa
3643177043Sthompsa	switch (cmd) {
3644177043Sthompsa	case WPI_RESTART:
3645177043Sthompsa		wpi_init_locked(sc, 0);
3646173362Sbenjsc		WPI_UNLOCK(sc);
3647173362Sbenjsc		return;
3648177043Sthompsa
3649177043Sthompsa	case WPI_RF_RESTART:
3650177043Sthompsa		wpi_rfkill_resume(sc);
3651177043Sthompsa		WPI_UNLOCK(sc);
3652177043Sthompsa		return;
3653173362Sbenjsc	}
3654173362Sbenjsc
3655177043Sthompsa	if (!(sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3656177043Sthompsa		WPI_UNLOCK(sc);
3657177043Sthompsa		return;
3658173362Sbenjsc	}
3659173362Sbenjsc
3660173362Sbenjsc	switch (cmd) {
3661173362Sbenjsc	case WPI_SCAN_START:
3662178354Ssam		/* make the link LED blink while we're scanning */
3663178354Ssam		wpi_set_led(sc, WPI_LED_LINK, 20, 2);
3664177043Sthompsa		sc->flags |= WPI_FLAG_SCANNING;
3665173362Sbenjsc		break;
3666173362Sbenjsc
3667173362Sbenjsc	case WPI_SCAN_STOP:
3668173362Sbenjsc		sc->flags &= ~WPI_FLAG_SCANNING;
3669173362Sbenjsc		break;
3670173362Sbenjsc
3671173362Sbenjsc	case WPI_SCAN_CURCHAN:
3672173362Sbenjsc		if (wpi_scan(sc))
3673178354Ssam			ieee80211_cancel_scan(vap);
3674173362Sbenjsc		break;
3675173362Sbenjsc
3676173362Sbenjsc	case WPI_SET_CHAN:
3677177043Sthompsa		error = wpi_config(sc);
3678177043Sthompsa		if (error != 0)
3679177043Sthompsa			device_printf(sc->sc_dev,
3680177043Sthompsa			    "error %d settting channel\n", error);
3681177043Sthompsa		break;
3682177043Sthompsa
3683177043Sthompsa	case WPI_AUTH:
3684177043Sthompsa		/* The node must be registered in the firmware before auth */
3685178354Ssam		error = wpi_auth(sc, vap);
3686178354Ssam		WPI_UNLOCK(sc);
3687177043Sthompsa		if (error != 0) {
3688177043Sthompsa			device_printf(sc->sc_dev,
3689177043Sthompsa			    "%s: could not move to auth state, error %d\n",
3690177043Sthompsa			    __func__, error);
3691173362Sbenjsc			return;
3692173362Sbenjsc		}
3693178354Ssam		IEEE80211_LOCK(ic);
3694178354Ssam		WPI_VAP(vap)->newstate(vap, IEEE80211_S_AUTH, arg);
3695178354Ssam		if (vap->iv_newstate_cb != NULL)
3696178354Ssam			vap->iv_newstate_cb(vap, IEEE80211_S_AUTH, arg);
3697178354Ssam		IEEE80211_UNLOCK(ic);
3698178354Ssam		goto again;
3699173362Sbenjsc
3700177043Sthompsa	case WPI_RUN:
3701178354Ssam		error = wpi_run(sc, vap);
3702178354Ssam		WPI_UNLOCK(sc);
3703177043Sthompsa		if (error != 0) {
3704173362Sbenjsc			device_printf(sc->sc_dev,
3705177043Sthompsa			    "%s: could not move to run state, error %d\n",
3706177043Sthompsa			    __func__, error);
3707173362Sbenjsc			return;
3708173362Sbenjsc		}
3709178354Ssam		IEEE80211_LOCK(ic);
3710178354Ssam		WPI_VAP(vap)->newstate(vap, IEEE80211_S_RUN, arg);
3711178354Ssam		if (vap->iv_newstate_cb != NULL)
3712178354Ssam			vap->iv_newstate_cb(vap, IEEE80211_S_RUN, arg);
3713178354Ssam		IEEE80211_UNLOCK(ic);
3714178354Ssam		goto again;
3715173362Sbenjsc	}
3716173362Sbenjsc	WPI_UNLOCK(sc);
3717173362Sbenjsc
3718173362Sbenjsc	/* Take another pass */
3719173362Sbenjsc	goto again;
3720173362Sbenjsc}
3721173362Sbenjsc
3722173362Sbenjsc/**
3723173362Sbenjsc * queue a command for later execution in a different thread.
3724173362Sbenjsc * This is needed as the net80211 callbacks do not allow
3725173362Sbenjsc * sleeping, since we need to sleep to confirm commands have
3726173362Sbenjsc * been processed by the firmware, we must defer execution to
3727173362Sbenjsc * a sleep enabled thread.
3728173362Sbenjsc */
3729173362Sbenjscstatic int
3730177043Sthompsawpi_queue_cmd(struct wpi_softc *sc, int cmd, int arg, int flush)
3731173362Sbenjsc{
3732173362Sbenjsc	WPI_CMD_LOCK(sc);
3733173362Sbenjsc
3734177043Sthompsa	if (flush) {
3735177043Sthompsa		memset(sc->sc_cmd, 0, sizeof (sc->sc_cmd));
3736177043Sthompsa		memset(sc->sc_cmd_arg, 0, sizeof (sc->sc_cmd_arg));
3737177043Sthompsa		sc->sc_cmd_cur = 0;
3738177043Sthompsa		sc->sc_cmd_next = 0;
3739177043Sthompsa	}
3740177043Sthompsa
3741173362Sbenjsc	if (sc->sc_cmd[sc->sc_cmd_next] != 0) {
3742173362Sbenjsc		WPI_CMD_UNLOCK(sc);
3743173362Sbenjsc		DPRINTF(("%s: command %d dropped\n", __func__, cmd));
3744173362Sbenjsc		return (EBUSY);
3745173362Sbenjsc	}
3746173362Sbenjsc
3747173362Sbenjsc	sc->sc_cmd[sc->sc_cmd_next] = cmd;
3748177043Sthompsa	sc->sc_cmd_arg[sc->sc_cmd_next] = arg;
3749173362Sbenjsc	sc->sc_cmd_next = (sc->sc_cmd_next + 1) % WPI_CMD_MAXOPS;
3750173362Sbenjsc
3751173362Sbenjsc	taskqueue_enqueue(sc->sc_tq, &sc->sc_opstask);
3752173362Sbenjsc
3753173362Sbenjsc	WPI_CMD_UNLOCK(sc);
3754173362Sbenjsc
3755173362Sbenjsc	return 0;
3756173362Sbenjsc}
3757173362Sbenjsc
3758173362Sbenjsc/*
3759173362Sbenjsc * Allocate DMA-safe memory for firmware transfer.
3760173362Sbenjsc */
3761173362Sbenjscstatic int
3762173362Sbenjscwpi_alloc_fwmem(struct wpi_softc *sc)
3763173362Sbenjsc{
3764173362Sbenjsc	/* allocate enough contiguous space to store text and data */
3765173362Sbenjsc	return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL,
3766173362Sbenjsc	    WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 1,
3767173362Sbenjsc	    BUS_DMA_NOWAIT);
3768173362Sbenjsc}
3769173362Sbenjsc
3770173362Sbenjscstatic void
3771173362Sbenjscwpi_free_fwmem(struct wpi_softc *sc)
3772173362Sbenjsc{
3773173362Sbenjsc	wpi_dma_contig_free(&sc->fw_dma);
3774173362Sbenjsc}
3775173362Sbenjsc
3776173362Sbenjsc/**
3777177043Sthompsa * Called every second, wpi_watchdog used by the watch dog timer
3778173362Sbenjsc * to check that the card is still alive
3779173362Sbenjsc */
3780173362Sbenjscstatic void
3781177043Sthompsawpi_watchdog(void *arg)
3782173362Sbenjsc{
3783173362Sbenjsc	struct wpi_softc *sc = arg;
3784177043Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3785177043Sthompsa	uint32_t tmp;
3786173362Sbenjsc
3787173362Sbenjsc	DPRINTFN(WPI_DEBUG_WATCHDOG,("Watchdog: tick\n"));
3788173362Sbenjsc
3789177043Sthompsa	if (sc->flags & WPI_FLAG_HW_RADIO_OFF) {
3790177043Sthompsa		/* No need to lock firmware memory */
3791177043Sthompsa		tmp = wpi_mem_read(sc, WPI_MEM_HW_RADIO_OFF);
3792177043Sthompsa
3793177043Sthompsa		if ((tmp & 0x1) == 0) {
3794177043Sthompsa			/* Radio kill switch is still off */
3795177043Sthompsa			callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3796177043Sthompsa			return;
3797177043Sthompsa		}
3798177043Sthompsa
3799177043Sthompsa		device_printf(sc->sc_dev, "Hardware Switch Enabled\n");
3800177043Sthompsa		wpi_queue_cmd(sc, WPI_RF_RESTART, 0, WPI_QUEUE_CLEAR);
3801177043Sthompsa		return;
3802177043Sthompsa	}
3803177043Sthompsa
3804177043Sthompsa	if (sc->sc_tx_timer > 0) {
3805177043Sthompsa		if (--sc->sc_tx_timer == 0) {
3806177043Sthompsa			device_printf(sc->sc_dev,"device timeout\n");
3807177043Sthompsa			ifp->if_oerrors++;
3808177043Sthompsa			wpi_queue_cmd(sc, WPI_RESTART, 0, WPI_QUEUE_CLEAR);
3809177043Sthompsa		}
3810177043Sthompsa	}
3811177043Sthompsa	if (sc->sc_scan_timer > 0) {
3812178354Ssam		struct ifnet *ifp = sc->sc_ifp;
3813178354Ssam		struct ieee80211com *ic = ifp->if_l2com;
3814178354Ssam		struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3815178354Ssam		if (--sc->sc_scan_timer == 0 && vap != NULL) {
3816177043Sthompsa			device_printf(sc->sc_dev,"scan timeout\n");
3817178354Ssam			ieee80211_cancel_scan(vap);
3818177043Sthompsa			wpi_queue_cmd(sc, WPI_RESTART, 0, WPI_QUEUE_CLEAR);
3819177043Sthompsa		}
3820177043Sthompsa	}
3821177043Sthompsa
3822177043Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3823177043Sthompsa		callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3824173362Sbenjsc}
3825173362Sbenjsc
3826173362Sbenjsc#ifdef WPI_DEBUG
3827173362Sbenjscstatic const char *wpi_cmd_str(int cmd)
3828173362Sbenjsc{
3829178354Ssam	switch (cmd) {
3830178354Ssam	case WPI_DISABLE_CMD:	return "WPI_DISABLE_CMD";
3831178354Ssam	case WPI_CMD_CONFIGURE:	return "WPI_CMD_CONFIGURE";
3832178354Ssam	case WPI_CMD_ASSOCIATE:	return "WPI_CMD_ASSOCIATE";
3833178354Ssam	case WPI_CMD_SET_WME:	return "WPI_CMD_SET_WME";
3834178354Ssam	case WPI_CMD_TSF:	return "WPI_CMD_TSF";
3835178354Ssam	case WPI_CMD_ADD_NODE:	return "WPI_CMD_ADD_NODE";
3836178354Ssam	case WPI_CMD_TX_DATA:	return "WPI_CMD_TX_DATA";
3837178354Ssam	case WPI_CMD_MRR_SETUP:	return "WPI_CMD_MRR_SETUP";
3838178354Ssam	case WPI_CMD_SET_LED:	return "WPI_CMD_SET_LED";
3839178354Ssam	case WPI_CMD_SET_POWER_MODE: return "WPI_CMD_SET_POWER_MODE";
3840178354Ssam	case WPI_CMD_SCAN:	return "WPI_CMD_SCAN";
3841178354Ssam	case WPI_CMD_SET_BEACON:return "WPI_CMD_SET_BEACON";
3842178354Ssam	case WPI_CMD_TXPOWER:	return "WPI_CMD_TXPOWER";
3843178354Ssam	case WPI_CMD_BLUETOOTH:	return "WPI_CMD_BLUETOOTH";
3844173362Sbenjsc
3845178354Ssam	default:
3846173362Sbenjsc		KASSERT(1, ("Unknown Command: %d\n", cmd));
3847178354Ssam		return "UNKNOWN CMD";	/* Make the compiler happy */
3848173362Sbenjsc	}
3849173362Sbenjsc}
3850173362Sbenjsc#endif
3851173362Sbenjsc
3852173362SbenjscMODULE_DEPEND(wpi, pci,  1, 1, 1);
3853173362SbenjscMODULE_DEPEND(wpi, wlan, 1, 1, 1);
3854173362SbenjscMODULE_DEPEND(wpi, firmware, 1, 1, 1);
3855173362SbenjscMODULE_DEPEND(wpi, wlan_amrr, 1, 1, 1);
3856