if_wpi.c revision 267580
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 267580 2014-06-17 14:47:49Z jhb $");
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
32218909Sbrucec * communicate by way of circular dma rings via 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
62236381Sadrian#include "opt_wlan.h"
63236381Sadrian
64173362Sbenjsc#include <sys/param.h>
65173362Sbenjsc#include <sys/sysctl.h>
66173362Sbenjsc#include <sys/sockio.h>
67173362Sbenjsc#include <sys/mbuf.h>
68173362Sbenjsc#include <sys/kernel.h>
69173362Sbenjsc#include <sys/socket.h>
70173362Sbenjsc#include <sys/systm.h>
71173362Sbenjsc#include <sys/malloc.h>
72173362Sbenjsc#include <sys/queue.h>
73173362Sbenjsc#include <sys/taskqueue.h>
74173362Sbenjsc#include <sys/module.h>
75173362Sbenjsc#include <sys/bus.h>
76173362Sbenjsc#include <sys/endian.h>
77173362Sbenjsc#include <sys/linker.h>
78173362Sbenjsc#include <sys/firmware.h>
79173362Sbenjsc
80173362Sbenjsc#include <machine/bus.h>
81173362Sbenjsc#include <machine/resource.h>
82173362Sbenjsc#include <sys/rman.h>
83173362Sbenjsc
84173362Sbenjsc#include <dev/pci/pcireg.h>
85173362Sbenjsc#include <dev/pci/pcivar.h>
86173362Sbenjsc
87173362Sbenjsc#include <net/bpf.h>
88173362Sbenjsc#include <net/if.h>
89257176Sglebius#include <net/if_var.h>
90173362Sbenjsc#include <net/if_arp.h>
91173362Sbenjsc#include <net/ethernet.h>
92173362Sbenjsc#include <net/if_dl.h>
93173362Sbenjsc#include <net/if_media.h>
94173362Sbenjsc#include <net/if_types.h>
95173362Sbenjsc
96173362Sbenjsc#include <net80211/ieee80211_var.h>
97173362Sbenjsc#include <net80211/ieee80211_radiotap.h>
98173362Sbenjsc#include <net80211/ieee80211_regdomain.h>
99206358Srpaulo#include <net80211/ieee80211_ratectl.h>
100173362Sbenjsc
101173362Sbenjsc#include <netinet/in.h>
102173362Sbenjsc#include <netinet/in_systm.h>
103173362Sbenjsc#include <netinet/in_var.h>
104173362Sbenjsc#include <netinet/ip.h>
105173362Sbenjsc#include <netinet/if_ether.h>
106173362Sbenjsc
107173362Sbenjsc#include <dev/wpi/if_wpireg.h>
108173362Sbenjsc#include <dev/wpi/if_wpivar.h>
109173362Sbenjsc
110199037Sdougb#define WPI_DEBUG
111199037Sdougb
112173362Sbenjsc#ifdef WPI_DEBUG
113173362Sbenjsc#define DPRINTF(x)	do { if (wpi_debug != 0) printf x; } while (0)
114173362Sbenjsc#define DPRINTFN(n, x)	do { if (wpi_debug & n) printf x; } while (0)
115179957Sthompsa#define	WPI_DEBUG_SET	(wpi_debug != 0)
116173362Sbenjsc
117173362Sbenjscenum {
118173362Sbenjsc	WPI_DEBUG_UNUSED	= 0x00000001,   /* Unused */
119173362Sbenjsc	WPI_DEBUG_HW		= 0x00000002,   /* Stage 1 (eeprom) debugging */
120173362Sbenjsc	WPI_DEBUG_TX		= 0x00000004,   /* Stage 2 TX intrp debugging*/
121173362Sbenjsc	WPI_DEBUG_RX		= 0x00000008,   /* Stage 2 RX intrp debugging */
122173362Sbenjsc	WPI_DEBUG_CMD		= 0x00000010,   /* Stage 2 CMD intrp debugging*/
123173362Sbenjsc	WPI_DEBUG_FIRMWARE	= 0x00000020,   /* firmware(9) loading debug  */
124173362Sbenjsc	WPI_DEBUG_DMA		= 0x00000040,   /* DMA (de)allocations/syncs  */
125173362Sbenjsc	WPI_DEBUG_SCANNING	= 0x00000080,   /* Stage 2 Scanning debugging */
126173362Sbenjsc	WPI_DEBUG_NOTIFY	= 0x00000100,   /* State 2 Noftif intr debug */
127173362Sbenjsc	WPI_DEBUG_TEMP		= 0x00000200,   /* TXPower/Temp Calibration */
128173362Sbenjsc	WPI_DEBUG_OPS		= 0x00000400,   /* wpi_ops taskq debug */
129173362Sbenjsc	WPI_DEBUG_WATCHDOG	= 0x00000800,   /* Watch dog debug */
130173362Sbenjsc	WPI_DEBUG_ANY		= 0xffffffff
131173362Sbenjsc};
132173362Sbenjsc
133199037Sdougbstatic int wpi_debug = 0;
134173362SbenjscSYSCTL_INT(_debug, OID_AUTO, wpi, CTLFLAG_RW, &wpi_debug, 0, "wpi debug level");
135179957SthompsaTUNABLE_INT("debug.wpi", &wpi_debug);
136173362Sbenjsc
137173362Sbenjsc#else
138173362Sbenjsc#define DPRINTF(x)
139173362Sbenjsc#define DPRINTFN(n, x)
140179957Sthompsa#define WPI_DEBUG_SET	0
141173362Sbenjsc#endif
142173362Sbenjsc
143173362Sbenjscstruct wpi_ident {
144173362Sbenjsc	uint16_t	vendor;
145173362Sbenjsc	uint16_t	device;
146173362Sbenjsc	uint16_t	subdevice;
147173362Sbenjsc	const char	*name;
148173362Sbenjsc};
149173362Sbenjsc
150173362Sbenjscstatic const struct wpi_ident wpi_ident_table[] = {
151173362Sbenjsc	/* The below entries support ABG regardless of the subid */
152173362Sbenjsc	{ 0x8086, 0x4222,    0x0, "Intel(R) PRO/Wireless 3945ABG" },
153173362Sbenjsc	{ 0x8086, 0x4227,    0x0, "Intel(R) PRO/Wireless 3945ABG" },
154173362Sbenjsc	/* The below entries only support BG */
155182127Sbenjsc	{ 0x8086, 0x4222, 0x1005, "Intel(R) PRO/Wireless 3945BG"  },
156182127Sbenjsc	{ 0x8086, 0x4222, 0x1034, "Intel(R) PRO/Wireless 3945BG"  },
157182127Sbenjsc	{ 0x8086, 0x4227, 0x1014, "Intel(R) PRO/Wireless 3945BG"  },
158182127Sbenjsc	{ 0x8086, 0x4222, 0x1044, "Intel(R) PRO/Wireless 3945BG"  },
159173362Sbenjsc	{ 0, 0, 0, NULL }
160173362Sbenjsc};
161173362Sbenjsc
162178354Ssamstatic struct ieee80211vap *wpi_vap_create(struct ieee80211com *,
163228621Sbschmidt		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
164228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN],
165228621Sbschmidt		    const uint8_t [IEEE80211_ADDR_LEN]);
166178354Ssamstatic void	wpi_vap_delete(struct ieee80211vap *);
167173362Sbenjscstatic int	wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *,
168173362Sbenjsc		    void **, bus_size_t, bus_size_t, int);
169173362Sbenjscstatic void	wpi_dma_contig_free(struct wpi_dma_info *);
170173362Sbenjscstatic void	wpi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
171173362Sbenjscstatic int	wpi_alloc_shared(struct wpi_softc *);
172173362Sbenjscstatic void	wpi_free_shared(struct wpi_softc *);
173173362Sbenjscstatic int	wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
174173362Sbenjscstatic void	wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
175173362Sbenjscstatic void	wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
176173362Sbenjscstatic int	wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
177173362Sbenjsc		    int, int);
178173362Sbenjscstatic void	wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
179173362Sbenjscstatic void	wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
180178354Ssamstatic int	wpi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
181173362Sbenjscstatic void	wpi_mem_lock(struct wpi_softc *);
182173362Sbenjscstatic void	wpi_mem_unlock(struct wpi_softc *);
183173362Sbenjscstatic uint32_t	wpi_mem_read(struct wpi_softc *, uint16_t);
184173362Sbenjscstatic void	wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t);
185173362Sbenjscstatic void	wpi_mem_write_region_4(struct wpi_softc *, uint16_t,
186173362Sbenjsc		    const uint32_t *, int);
187173362Sbenjscstatic uint16_t	wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
188173362Sbenjscstatic int	wpi_alloc_fwmem(struct wpi_softc *);
189173362Sbenjscstatic void	wpi_free_fwmem(struct wpi_softc *);
190173362Sbenjscstatic int	wpi_load_firmware(struct wpi_softc *);
191173362Sbenjscstatic void	wpi_unload_firmware(struct wpi_softc *);
192173362Sbenjscstatic int	wpi_load_microcode(struct wpi_softc *, const uint8_t *, int);
193173362Sbenjscstatic void	wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *,
194173362Sbenjsc		    struct wpi_rx_data *);
195173362Sbenjscstatic void	wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *);
196173362Sbenjscstatic void	wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *);
197173362Sbenjscstatic void	wpi_notif_intr(struct wpi_softc *);
198173362Sbenjscstatic void	wpi_intr(void *);
199173362Sbenjscstatic uint8_t	wpi_plcp_signal(int);
200177043Sthompsastatic void	wpi_watchdog(void *);
201173362Sbenjscstatic int	wpi_tx_data(struct wpi_softc *, struct mbuf *,
202173362Sbenjsc		    struct ieee80211_node *, int);
203173362Sbenjscstatic void	wpi_start(struct ifnet *);
204178354Ssamstatic void	wpi_start_locked(struct ifnet *);
205178354Ssamstatic int	wpi_raw_xmit(struct ieee80211_node *, struct mbuf *,
206178354Ssam		    const struct ieee80211_bpf_params *);
207173362Sbenjscstatic void	wpi_scan_start(struct ieee80211com *);
208173362Sbenjscstatic void	wpi_scan_end(struct ieee80211com *);
209173362Sbenjscstatic void	wpi_set_channel(struct ieee80211com *);
210178354Ssamstatic void	wpi_scan_curchan(struct ieee80211_scan_state *, unsigned long);
211178354Ssamstatic void	wpi_scan_mindwell(struct ieee80211_scan_state *);
212173362Sbenjscstatic int	wpi_ioctl(struct ifnet *, u_long, caddr_t);
213190526Ssamstatic void	wpi_read_eeprom(struct wpi_softc *,
214190526Ssam		    uint8_t macaddr[IEEE80211_ADDR_LEN]);
215173362Sbenjscstatic void	wpi_read_eeprom_channels(struct wpi_softc *, int);
216173362Sbenjscstatic void	wpi_read_eeprom_group(struct wpi_softc *, int);
217173362Sbenjscstatic int	wpi_cmd(struct wpi_softc *, int, const void *, int, int);
218173362Sbenjscstatic int	wpi_wme_update(struct ieee80211com *);
219173362Sbenjscstatic int	wpi_mrr_setup(struct wpi_softc *);
220173362Sbenjscstatic void	wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
221173362Sbenjscstatic void	wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *);
222173362Sbenjsc#if 0
223173362Sbenjscstatic int	wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
224173362Sbenjsc#endif
225178354Ssamstatic int	wpi_auth(struct wpi_softc *, struct ieee80211vap *);
226178354Ssamstatic int	wpi_run(struct wpi_softc *, struct ieee80211vap *);
227173362Sbenjscstatic int	wpi_scan(struct wpi_softc *);
228173362Sbenjscstatic int	wpi_config(struct wpi_softc *);
229173362Sbenjscstatic void	wpi_stop_master(struct wpi_softc *);
230173362Sbenjscstatic int	wpi_power_up(struct wpi_softc *);
231173362Sbenjscstatic int	wpi_reset(struct wpi_softc *);
232191746Sthompsastatic void	wpi_hwreset(void *, int);
233191746Sthompsastatic void	wpi_rfreset(void *, int);
234173362Sbenjscstatic void	wpi_hw_config(struct wpi_softc *);
235173362Sbenjscstatic void	wpi_init(void *);
236177043Sthompsastatic void	wpi_init_locked(struct wpi_softc *, int);
237173362Sbenjscstatic void	wpi_stop(struct wpi_softc *);
238173362Sbenjscstatic void	wpi_stop_locked(struct wpi_softc *);
239173362Sbenjsc
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
256173362Sbenjscstatic device_method_t wpi_methods[] = {
257173362Sbenjsc	/* Device interface */
258173362Sbenjsc	DEVMETHOD(device_probe,		wpi_probe),
259173362Sbenjsc	DEVMETHOD(device_attach,	wpi_attach),
260173362Sbenjsc	DEVMETHOD(device_detach,	wpi_detach),
261173362Sbenjsc	DEVMETHOD(device_shutdown,	wpi_shutdown),
262173362Sbenjsc	DEVMETHOD(device_suspend,	wpi_suspend),
263173362Sbenjsc	DEVMETHOD(device_resume,	wpi_resume),
264173362Sbenjsc
265260064Smarius	DEVMETHOD_END
266173362Sbenjsc};
267173362Sbenjsc
268173362Sbenjscstatic driver_t wpi_driver = {
269173362Sbenjsc	"wpi",
270173362Sbenjsc	wpi_methods,
271173362Sbenjsc	sizeof (struct wpi_softc)
272173362Sbenjsc};
273173362Sbenjsc
274173362Sbenjscstatic devclass_t wpi_devclass;
275173362Sbenjsc
276260064SmariusDRIVER_MODULE(wpi, pci, wpi_driver, wpi_devclass, NULL, NULL);
277173362Sbenjsc
278222543SbschmidtMODULE_VERSION(wpi, 1);
279222543Sbschmidt
280173362Sbenjscstatic const uint8_t wpi_ridx_to_plcp[] = {
281173362Sbenjsc	/* OFDM: IEEE Std 802.11a-1999, pp. 14 Table 80 */
282173362Sbenjsc	/* R1-R4 (ral/ural is R4-R1) */
283173362Sbenjsc	0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3,
284173362Sbenjsc	/* CCK: device-dependent */
285173362Sbenjsc	10, 20, 55, 110
286173362Sbenjsc};
287260064Smarius
288173362Sbenjscstatic const uint8_t wpi_ridx_to_rate[] = {
289173362Sbenjsc	12, 18, 24, 36, 48, 72, 96, 108, /* OFDM */
290173362Sbenjsc	2, 4, 11, 22 /*CCK */
291173362Sbenjsc};
292173362Sbenjsc
293173362Sbenjscstatic int
294173362Sbenjscwpi_probe(device_t dev)
295173362Sbenjsc{
296173362Sbenjsc	const struct wpi_ident *ident;
297173362Sbenjsc
298173362Sbenjsc	for (ident = wpi_ident_table; ident->name != NULL; ident++) {
299173362Sbenjsc		if (pci_get_vendor(dev) == ident->vendor &&
300173362Sbenjsc		    pci_get_device(dev) == ident->device) {
301173362Sbenjsc			device_set_desc(dev, ident->name);
302260064Smarius			return (BUS_PROBE_DEFAULT);
303173362Sbenjsc		}
304173362Sbenjsc	}
305173362Sbenjsc	return ENXIO;
306173362Sbenjsc}
307173362Sbenjsc
308173362Sbenjsc/**
309173362Sbenjsc * Load the firmare image from disk to the allocated dma buffer.
310173362Sbenjsc * we also maintain the reference to the firmware pointer as there
311173362Sbenjsc * is times where we may need to reload the firmware but we are not
312173362Sbenjsc * in a context that can access the filesystem (ie taskq cause by restart)
313173362Sbenjsc *
314173362Sbenjsc * @return 0 on success, an errno on failure
315173362Sbenjsc */
316173362Sbenjscstatic int
317173362Sbenjscwpi_load_firmware(struct wpi_softc *sc)
318173362Sbenjsc{
319178354Ssam	const struct firmware *fp;
320173362Sbenjsc	struct wpi_dma_info *dma = &sc->fw_dma;
321173362Sbenjsc	const struct wpi_firmware_hdr *hdr;
322173362Sbenjsc	const uint8_t *itext, *idata, *rtext, *rdata, *btext;
323173362Sbenjsc	uint32_t itextsz, idatasz, rtextsz, rdatasz, btextsz;
324173362Sbenjsc	int error;
325173362Sbenjsc
326173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
327173362Sbenjsc	    ("Attempting Loading Firmware from wpi_fw module\n"));
328173362Sbenjsc
329173362Sbenjsc	WPI_UNLOCK(sc);
330173362Sbenjsc
331173362Sbenjsc	if (sc->fw_fp == NULL && (sc->fw_fp = firmware_get("wpifw")) == NULL) {
332173362Sbenjsc		device_printf(sc->sc_dev,
333173362Sbenjsc		    "could not load firmware image 'wpifw'\n");
334173362Sbenjsc		error = ENOENT;
335173362Sbenjsc		WPI_LOCK(sc);
336173362Sbenjsc		goto fail;
337173362Sbenjsc	}
338173362Sbenjsc
339173362Sbenjsc	fp = sc->fw_fp;
340173362Sbenjsc
341173362Sbenjsc	WPI_LOCK(sc);
342173362Sbenjsc
343173362Sbenjsc	/* Validate the firmware is minimum a particular version */
344173362Sbenjsc	if (fp->version < WPI_FW_MINVERSION) {
345173362Sbenjsc	    device_printf(sc->sc_dev,
346173362Sbenjsc			   "firmware version is too old. Need %d, got %d\n",
347173362Sbenjsc			   WPI_FW_MINVERSION,
348173362Sbenjsc			   fp->version);
349173362Sbenjsc	    error = ENXIO;
350173362Sbenjsc	    goto fail;
351173362Sbenjsc	}
352173362Sbenjsc
353173362Sbenjsc	if (fp->datasize < sizeof (struct wpi_firmware_hdr)) {
354173362Sbenjsc		device_printf(sc->sc_dev,
355173362Sbenjsc		    "firmware file too short: %zu bytes\n", fp->datasize);
356173362Sbenjsc		error = ENXIO;
357173362Sbenjsc		goto fail;
358173362Sbenjsc	}
359173362Sbenjsc
360173362Sbenjsc	hdr = (const struct wpi_firmware_hdr *)fp->data;
361173362Sbenjsc
362173362Sbenjsc	/*     |  RUNTIME FIRMWARE   |    INIT FIRMWARE    | BOOT FW  |
363173362Sbenjsc	   |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */
364173362Sbenjsc
365173362Sbenjsc	rtextsz = le32toh(hdr->rtextsz);
366173362Sbenjsc	rdatasz = le32toh(hdr->rdatasz);
367173362Sbenjsc	itextsz = le32toh(hdr->itextsz);
368173362Sbenjsc	idatasz = le32toh(hdr->idatasz);
369173362Sbenjsc	btextsz = le32toh(hdr->btextsz);
370173362Sbenjsc
371173362Sbenjsc	/* check that all firmware segments are present */
372173362Sbenjsc	if (fp->datasize < sizeof (struct wpi_firmware_hdr) +
373173362Sbenjsc		rtextsz + rdatasz + itextsz + idatasz + btextsz) {
374173362Sbenjsc		device_printf(sc->sc_dev,
375173362Sbenjsc		    "firmware file too short: %zu bytes\n", fp->datasize);
376173362Sbenjsc		error = ENXIO; /* XXX appropriate error code? */
377173362Sbenjsc		goto fail;
378173362Sbenjsc	}
379173362Sbenjsc
380173362Sbenjsc	/* get pointers to firmware segments */
381173362Sbenjsc	rtext = (const uint8_t *)(hdr + 1);
382173362Sbenjsc	rdata = rtext + rtextsz;
383173362Sbenjsc	itext = rdata + rdatasz;
384173362Sbenjsc	idata = itext + itextsz;
385173362Sbenjsc	btext = idata + idatasz;
386173362Sbenjsc
387173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
388173362Sbenjsc	    ("Firmware Version: Major %d, Minor %d, Driver %d, \n"
389173362Sbenjsc	     "runtime (text: %u, data: %u) init (text: %u, data %u) boot (text %u)\n",
390173362Sbenjsc	     (le32toh(hdr->version) & 0xff000000) >> 24,
391173362Sbenjsc	     (le32toh(hdr->version) & 0x00ff0000) >> 16,
392173362Sbenjsc	     (le32toh(hdr->version) & 0x0000ffff),
393173362Sbenjsc	     rtextsz, rdatasz,
394173362Sbenjsc	     itextsz, idatasz, btextsz));
395173362Sbenjsc
396173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("rtext 0x%x\n", *(const uint32_t *)rtext));
397173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("rdata 0x%x\n", *(const uint32_t *)rdata));
398173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("itext 0x%x\n", *(const uint32_t *)itext));
399173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("idata 0x%x\n", *(const uint32_t *)idata));
400173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("btext 0x%x\n", *(const uint32_t *)btext));
401173362Sbenjsc
402173362Sbenjsc	/* sanity checks */
403173362Sbenjsc	if (rtextsz > WPI_FW_MAIN_TEXT_MAXSZ ||
404173362Sbenjsc	    rdatasz > WPI_FW_MAIN_DATA_MAXSZ ||
405173362Sbenjsc	    itextsz > WPI_FW_INIT_TEXT_MAXSZ ||
406173362Sbenjsc	    idatasz > WPI_FW_INIT_DATA_MAXSZ ||
407173362Sbenjsc	    btextsz > WPI_FW_BOOT_TEXT_MAXSZ ||
408173362Sbenjsc	    (btextsz & 3) != 0) {
409173362Sbenjsc		device_printf(sc->sc_dev, "firmware invalid\n");
410173362Sbenjsc		error = EINVAL;
411173362Sbenjsc		goto fail;
412173362Sbenjsc	}
413173362Sbenjsc
414173362Sbenjsc	/* copy initialization images into pre-allocated DMA-safe memory */
415173362Sbenjsc	memcpy(dma->vaddr, idata, idatasz);
416173362Sbenjsc	memcpy(dma->vaddr + WPI_FW_INIT_DATA_MAXSZ, itext, itextsz);
417173362Sbenjsc
418173362Sbenjsc	bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
419173362Sbenjsc
420173362Sbenjsc	/* tell adapter where to find initialization images */
421173362Sbenjsc	wpi_mem_lock(sc);
422173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
423173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, idatasz);
424173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
425173362Sbenjsc	    dma->paddr + WPI_FW_INIT_DATA_MAXSZ);
426173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, itextsz);
427173362Sbenjsc	wpi_mem_unlock(sc);
428173362Sbenjsc
429173362Sbenjsc	/* load firmware boot code */
430173362Sbenjsc	if ((error = wpi_load_microcode(sc, btext, btextsz)) != 0) {
431173362Sbenjsc	    device_printf(sc->sc_dev, "Failed to load microcode\n");
432173362Sbenjsc	    goto fail;
433173362Sbenjsc	}
434173362Sbenjsc
435173362Sbenjsc	/* now press "execute" */
436173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, 0);
437173362Sbenjsc
438173362Sbenjsc	/* wait at most one second for the first alive notification */
439173362Sbenjsc	if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
440173362Sbenjsc		device_printf(sc->sc_dev,
441173362Sbenjsc		    "timeout waiting for adapter to initialize\n");
442173362Sbenjsc		goto fail;
443173362Sbenjsc	}
444173362Sbenjsc
445173362Sbenjsc	/* copy runtime images into pre-allocated DMA-sage memory */
446173362Sbenjsc	memcpy(dma->vaddr, rdata, rdatasz);
447173362Sbenjsc	memcpy(dma->vaddr + WPI_FW_MAIN_DATA_MAXSZ, rtext, rtextsz);
448173362Sbenjsc	bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
449173362Sbenjsc
450173362Sbenjsc	/* tell adapter where to find runtime images */
451173362Sbenjsc	wpi_mem_lock(sc);
452173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
453173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, rdatasz);
454173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
455173362Sbenjsc	    dma->paddr + WPI_FW_MAIN_DATA_MAXSZ);
456173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, WPI_FW_UPDATED | rtextsz);
457173362Sbenjsc	wpi_mem_unlock(sc);
458173362Sbenjsc
459173362Sbenjsc	/* wait at most one second for the first alive notification */
460173362Sbenjsc	if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
461173362Sbenjsc		device_printf(sc->sc_dev,
462173362Sbenjsc		    "timeout waiting for adapter to initialize2\n");
463173362Sbenjsc		goto fail;
464173362Sbenjsc	}
465173362Sbenjsc
466173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
467173362Sbenjsc	    ("Firmware loaded to driver successfully\n"));
468173362Sbenjsc	return error;
469173362Sbenjscfail:
470173362Sbenjsc	wpi_unload_firmware(sc);
471173362Sbenjsc	return error;
472173362Sbenjsc}
473173362Sbenjsc
474173362Sbenjsc/**
475173362Sbenjsc * Free the referenced firmware image
476173362Sbenjsc */
477173362Sbenjscstatic void
478173362Sbenjscwpi_unload_firmware(struct wpi_softc *sc)
479173362Sbenjsc{
480173362Sbenjsc
481173362Sbenjsc	if (sc->fw_fp) {
482173362Sbenjsc		WPI_UNLOCK(sc);
483173362Sbenjsc		firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
484173362Sbenjsc		WPI_LOCK(sc);
485173362Sbenjsc		sc->fw_fp = NULL;
486173362Sbenjsc	}
487173362Sbenjsc}
488173362Sbenjsc
489173362Sbenjscstatic int
490173362Sbenjscwpi_attach(device_t dev)
491173362Sbenjsc{
492173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
493173362Sbenjsc	struct ifnet *ifp;
494178354Ssam	struct ieee80211com *ic;
495260064Smarius	int ac, error, rid, supportsa = 1;
496173362Sbenjsc	uint32_t tmp;
497173362Sbenjsc	const struct wpi_ident *ident;
498190526Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
499173362Sbenjsc
500173362Sbenjsc	sc->sc_dev = dev;
501173362Sbenjsc
502179957Sthompsa	if (bootverbose || WPI_DEBUG_SET)
503173362Sbenjsc	    device_printf(sc->sc_dev,"Driver Revision %s\n", VERSION);
504173362Sbenjsc
505173362Sbenjsc	/*
506173362Sbenjsc	 * Some card's only support 802.11b/g not a, check to see if
507173362Sbenjsc	 * this is one such card. A 0x0 in the subdevice table indicates
508173362Sbenjsc	 * the entire subdevice range is to be ignored.
509173362Sbenjsc	 */
510173362Sbenjsc	for (ident = wpi_ident_table; ident->name != NULL; ident++) {
511173362Sbenjsc		if (ident->subdevice &&
512173362Sbenjsc		    pci_get_subdevice(dev) == ident->subdevice) {
513173362Sbenjsc		    supportsa = 0;
514173362Sbenjsc		    break;
515173362Sbenjsc		}
516173362Sbenjsc	}
517173362Sbenjsc
518173362Sbenjsc	/* Create the tasks that can be queued */
519191746Sthompsa	TASK_INIT(&sc->sc_restarttask, 0, wpi_hwreset, sc);
520191746Sthompsa	TASK_INIT(&sc->sc_radiotask, 0, wpi_rfreset, sc);
521173362Sbenjsc
522173362Sbenjsc	WPI_LOCK_INIT(sc);
523173362Sbenjsc
524173362Sbenjsc	callout_init_mtx(&sc->calib_to, &sc->sc_mtx, 0);
525173362Sbenjsc	callout_init_mtx(&sc->watchdog_to, &sc->sc_mtx, 0);
526173362Sbenjsc
527173362Sbenjsc	/* disable the retry timeout register */
528173362Sbenjsc	pci_write_config(dev, 0x41, 0, 1);
529173362Sbenjsc
530173362Sbenjsc	/* enable bus-mastering */
531173362Sbenjsc	pci_enable_busmaster(dev);
532173362Sbenjsc
533260064Smarius	rid = PCIR_BAR(0);
534260064Smarius	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
535173362Sbenjsc	    RF_ACTIVE);
536173362Sbenjsc	if (sc->mem == NULL) {
537173362Sbenjsc		device_printf(dev, "could not allocate memory resource\n");
538173362Sbenjsc		error = ENOMEM;
539173362Sbenjsc		goto fail;
540173362Sbenjsc	}
541173362Sbenjsc
542173362Sbenjsc	sc->sc_st = rman_get_bustag(sc->mem);
543173362Sbenjsc	sc->sc_sh = rman_get_bushandle(sc->mem);
544173362Sbenjsc
545260064Smarius	rid = 0;
546260064Smarius	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
547173362Sbenjsc	    RF_ACTIVE | RF_SHAREABLE);
548173362Sbenjsc	if (sc->irq == NULL) {
549173362Sbenjsc		device_printf(dev, "could not allocate interrupt resource\n");
550173362Sbenjsc		error = ENOMEM;
551173362Sbenjsc		goto fail;
552173362Sbenjsc	}
553173362Sbenjsc
554173362Sbenjsc	/*
555173362Sbenjsc	 * Allocate DMA memory for firmware transfers.
556173362Sbenjsc	 */
557173362Sbenjsc	if ((error = wpi_alloc_fwmem(sc)) != 0) {
558173362Sbenjsc		printf(": could not allocate firmware memory\n");
559173362Sbenjsc		error = ENOMEM;
560173362Sbenjsc		goto fail;
561173362Sbenjsc	}
562173362Sbenjsc
563173362Sbenjsc	/*
564173362Sbenjsc	 * Put adapter into a known state.
565173362Sbenjsc	 */
566173362Sbenjsc	if ((error = wpi_reset(sc)) != 0) {
567173362Sbenjsc		device_printf(dev, "could not reset adapter\n");
568173362Sbenjsc		goto fail;
569173362Sbenjsc	}
570173362Sbenjsc
571173362Sbenjsc	wpi_mem_lock(sc);
572173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
573179957Sthompsa	if (bootverbose || WPI_DEBUG_SET)
574173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Revision (0x%X)\n", tmp);
575173362Sbenjsc
576173362Sbenjsc	wpi_mem_unlock(sc);
577173362Sbenjsc
578173362Sbenjsc	/* Allocate shared page */
579173362Sbenjsc	if ((error = wpi_alloc_shared(sc)) != 0) {
580173362Sbenjsc		device_printf(dev, "could not allocate shared page\n");
581173362Sbenjsc		goto fail;
582173362Sbenjsc	}
583173362Sbenjsc
584173362Sbenjsc	/* tx data queues  - 4 for QoS purposes */
585173362Sbenjsc	for (ac = 0; ac < WME_NUM_AC; ac++) {
586173362Sbenjsc		error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, ac);
587173362Sbenjsc		if (error != 0) {
588173362Sbenjsc		    device_printf(dev, "could not allocate Tx ring %d\n",ac);
589173362Sbenjsc		    goto fail;
590173362Sbenjsc		}
591173362Sbenjsc	}
592173362Sbenjsc
593173362Sbenjsc	/* command queue to talk to the card's firmware */
594173362Sbenjsc	error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4);
595173362Sbenjsc	if (error != 0) {
596173362Sbenjsc		device_printf(dev, "could not allocate command ring\n");
597173362Sbenjsc		goto fail;
598173362Sbenjsc	}
599173362Sbenjsc
600173362Sbenjsc	/* receive data queue */
601173362Sbenjsc	error = wpi_alloc_rx_ring(sc, &sc->rxq);
602173362Sbenjsc	if (error != 0) {
603173362Sbenjsc		device_printf(dev, "could not allocate Rx ring\n");
604173362Sbenjsc		goto fail;
605173362Sbenjsc	}
606173362Sbenjsc
607178354Ssam	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
608173362Sbenjsc	if (ifp == NULL) {
609173362Sbenjsc		device_printf(dev, "can not if_alloc()\n");
610173362Sbenjsc		error = ENOMEM;
611173362Sbenjsc		goto fail;
612173362Sbenjsc	}
613178354Ssam	ic = ifp->if_l2com;
614173362Sbenjsc
615173362Sbenjsc	ic->ic_ifp = ifp;
616178354Ssam	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
617178354Ssam	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
618173362Sbenjsc
619173362Sbenjsc	/* set device capabilities */
620173362Sbenjsc	ic->ic_caps =
621178957Ssam		  IEEE80211_C_STA		/* station mode supported */
622178957Ssam		| IEEE80211_C_MONITOR		/* monitor mode supported */
623173362Sbenjsc		| IEEE80211_C_TXPMGT		/* tx power management */
624173362Sbenjsc		| IEEE80211_C_SHSLOT		/* short slot time supported */
625173362Sbenjsc		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
626173362Sbenjsc		| IEEE80211_C_WPA		/* 802.11i */
627173362Sbenjsc/* XXX looks like WME is partly supported? */
628173362Sbenjsc#if 0
629173362Sbenjsc		| IEEE80211_C_IBSS		/* IBSS mode support */
630173362Sbenjsc		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
631173362Sbenjsc		| IEEE80211_C_WME		/* 802.11e */
632173362Sbenjsc		| IEEE80211_C_HOSTAP		/* Host access point mode */
633173362Sbenjsc#endif
634173362Sbenjsc		;
635173362Sbenjsc
636173362Sbenjsc	/*
637173362Sbenjsc	 * Read in the eeprom and also setup the channels for
638173362Sbenjsc	 * net80211. We don't set the rates as net80211 does this for us
639173362Sbenjsc	 */
640190526Ssam	wpi_read_eeprom(sc, macaddr);
641173362Sbenjsc
642179957Sthompsa	if (bootverbose || WPI_DEBUG_SET) {
643173362Sbenjsc	    device_printf(sc->sc_dev, "Regulatory Domain: %.4s\n", sc->domain);
644173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Type: %c\n",
645173362Sbenjsc			  sc->type > 1 ? 'B': '?');
646173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Revision: %c\n",
647173362Sbenjsc			  ((le16toh(sc->rev) & 0xf0) == 0xd0) ? 'D': '?');
648173362Sbenjsc	    device_printf(sc->sc_dev, "SKU %s support 802.11a\n",
649173362Sbenjsc			  supportsa ? "does" : "does not");
650173362Sbenjsc
651173362Sbenjsc	    /* XXX hw_config uses the PCIDEV for the Hardware rev. Must check
652173362Sbenjsc	       what sc->rev really represents - benjsc 20070615 */
653173362Sbenjsc	}
654173362Sbenjsc
655173362Sbenjsc	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
656173362Sbenjsc	ifp->if_softc = sc;
657173362Sbenjsc	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
658173362Sbenjsc	ifp->if_init = wpi_init;
659173362Sbenjsc	ifp->if_ioctl = wpi_ioctl;
660173362Sbenjsc	ifp->if_start = wpi_start;
661207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
662207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
663173362Sbenjsc	IFQ_SET_READY(&ifp->if_snd);
664178354Ssam
665190526Ssam	ieee80211_ifattach(ic, macaddr);
666173362Sbenjsc	/* override default methods */
667178354Ssam	ic->ic_raw_xmit = wpi_raw_xmit;
668173362Sbenjsc	ic->ic_wme.wme_update = wpi_wme_update;
669173362Sbenjsc	ic->ic_scan_start = wpi_scan_start;
670173362Sbenjsc	ic->ic_scan_end = wpi_scan_end;
671173362Sbenjsc	ic->ic_set_channel = wpi_set_channel;
672173362Sbenjsc	ic->ic_scan_curchan = wpi_scan_curchan;
673173362Sbenjsc	ic->ic_scan_mindwell = wpi_scan_mindwell;
674173362Sbenjsc
675178354Ssam	ic->ic_vap_create = wpi_vap_create;
676178354Ssam	ic->ic_vap_delete = wpi_vap_delete;
677173362Sbenjsc
678192468Ssam	ieee80211_radiotap_attach(ic,
679192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
680192468Ssam		WPI_TX_RADIOTAP_PRESENT,
681192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
682192468Ssam		WPI_RX_RADIOTAP_PRESENT);
683173362Sbenjsc
684173362Sbenjsc	/*
685173362Sbenjsc	 * Hook our interrupt after all initialization is complete.
686173362Sbenjsc	 */
687177043Sthompsa	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET |INTR_MPSAFE,
688177043Sthompsa	    NULL, wpi_intr, sc, &sc->sc_ih);
689173362Sbenjsc	if (error != 0) {
690173362Sbenjsc		device_printf(dev, "could not set up interrupt\n");
691173362Sbenjsc		goto fail;
692173362Sbenjsc	}
693173362Sbenjsc
694177043Sthompsa	if (bootverbose)
695177043Sthompsa		ieee80211_announce(ic);
696173362Sbenjsc#ifdef XXX_DEBUG
697173362Sbenjsc	ieee80211_announce_channels(ic);
698173362Sbenjsc#endif
699173362Sbenjsc	return 0;
700173362Sbenjsc
701173362Sbenjscfail:	wpi_detach(dev);
702173362Sbenjsc	return ENXIO;
703173362Sbenjsc}
704173362Sbenjsc
705173362Sbenjscstatic int
706173362Sbenjscwpi_detach(device_t dev)
707173362Sbenjsc{
708173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
709178354Ssam	struct ifnet *ifp = sc->sc_ifp;
710200530Sgavin	struct ieee80211com *ic;
711173362Sbenjsc	int ac;
712173362Sbenjsc
713260064Smarius	if (sc->irq != NULL)
714260064Smarius		bus_teardown_intr(dev, sc->irq, sc->sc_ih);
715260064Smarius
716200530Sgavin	if (ifp != NULL) {
717200530Sgavin		ic = ifp->if_l2com;
718191746Sthompsa
719200530Sgavin		ieee80211_draintask(ic, &sc->sc_restarttask);
720200530Sgavin		ieee80211_draintask(ic, &sc->sc_radiotask);
721173362Sbenjsc		wpi_stop(sc);
722173362Sbenjsc		callout_drain(&sc->watchdog_to);
723173362Sbenjsc		callout_drain(&sc->calib_to);
724173362Sbenjsc		ieee80211_ifdetach(ic);
725173362Sbenjsc	}
726173362Sbenjsc
727173362Sbenjsc	WPI_LOCK(sc);
728173362Sbenjsc	if (sc->txq[0].data_dmat) {
729173362Sbenjsc		for (ac = 0; ac < WME_NUM_AC; ac++)
730173362Sbenjsc			wpi_free_tx_ring(sc, &sc->txq[ac]);
731173362Sbenjsc
732173362Sbenjsc		wpi_free_tx_ring(sc, &sc->cmdq);
733173362Sbenjsc		wpi_free_rx_ring(sc, &sc->rxq);
734173362Sbenjsc		wpi_free_shared(sc);
735173362Sbenjsc	}
736173362Sbenjsc
737173362Sbenjsc	if (sc->fw_fp != NULL) {
738173362Sbenjsc		wpi_unload_firmware(sc);
739173362Sbenjsc	}
740173362Sbenjsc
741173362Sbenjsc	if (sc->fw_dma.tag)
742173362Sbenjsc		wpi_free_fwmem(sc);
743173362Sbenjsc	WPI_UNLOCK(sc);
744173362Sbenjsc
745260064Smarius	if (sc->irq != NULL)
746260064Smarius		bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq),
747260064Smarius		    sc->irq);
748173362Sbenjsc	if (sc->mem != NULL)
749260064Smarius		bus_release_resource(dev, SYS_RES_MEMORY,
750260064Smarius		    rman_get_rid(sc->mem), sc->mem);
751173362Sbenjsc
752173362Sbenjsc	if (ifp != NULL)
753173362Sbenjsc		if_free(ifp);
754173362Sbenjsc
755173362Sbenjsc	WPI_LOCK_DESTROY(sc);
756173362Sbenjsc
757173362Sbenjsc	return 0;
758173362Sbenjsc}
759173362Sbenjsc
760178354Ssamstatic struct ieee80211vap *
761228621Sbschmidtwpi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
762228621Sbschmidt    enum ieee80211_opmode opmode, int flags,
763228621Sbschmidt    const uint8_t bssid[IEEE80211_ADDR_LEN],
764228621Sbschmidt    const uint8_t mac[IEEE80211_ADDR_LEN])
765178354Ssam{
766178354Ssam	struct wpi_vap *wvp;
767178354Ssam	struct ieee80211vap *vap;
768178354Ssam
769178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
770178354Ssam		return NULL;
771178354Ssam	wvp = (struct wpi_vap *) malloc(sizeof(struct wpi_vap),
772178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
773178354Ssam	if (wvp == NULL)
774178354Ssam		return NULL;
775178354Ssam	vap = &wvp->vap;
776178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
777178354Ssam	/* override with driver methods */
778178354Ssam	wvp->newstate = vap->iv_newstate;
779178354Ssam	vap->iv_newstate = wpi_newstate;
780178354Ssam
781206358Srpaulo	ieee80211_ratectl_init(vap);
782178354Ssam	/* complete setup */
783178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
784178354Ssam	ic->ic_opmode = opmode;
785178354Ssam	return vap;
786178354Ssam}
787178354Ssam
788173362Sbenjscstatic void
789178354Ssamwpi_vap_delete(struct ieee80211vap *vap)
790178354Ssam{
791178354Ssam	struct wpi_vap *wvp = WPI_VAP(vap);
792178354Ssam
793206358Srpaulo	ieee80211_ratectl_deinit(vap);
794178354Ssam	ieee80211_vap_detach(vap);
795178354Ssam	free(wvp, M_80211_VAP);
796178354Ssam}
797178354Ssam
798178354Ssamstatic void
799173362Sbenjscwpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
800173362Sbenjsc{
801173362Sbenjsc	if (error != 0)
802173362Sbenjsc		return;
803173362Sbenjsc
804173362Sbenjsc	KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
805173362Sbenjsc
806173362Sbenjsc	*(bus_addr_t *)arg = segs[0].ds_addr;
807173362Sbenjsc}
808173362Sbenjsc
809177043Sthompsa/*
810177043Sthompsa * Allocates a contiguous block of dma memory of the requested size and
811177043Sthompsa * alignment. Due to limitations of the FreeBSD dma subsystem as of 20071217,
812177043Sthompsa * allocations greater than 4096 may fail. Hence if the requested alignment is
813177043Sthompsa * greater we allocate 'alignment' size extra memory and shift the vaddr and
814177043Sthompsa * paddr after the dma load. This bypasses the problem at the cost of a little
815177043Sthompsa * more memory.
816177043Sthompsa */
817173362Sbenjscstatic int
818173362Sbenjscwpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
819173362Sbenjsc    void **kvap, bus_size_t size, bus_size_t alignment, int flags)
820173362Sbenjsc{
821173362Sbenjsc	int error;
822177043Sthompsa	bus_size_t align;
823177043Sthompsa	bus_size_t reqsize;
824173362Sbenjsc
825173362Sbenjsc	DPRINTFN(WPI_DEBUG_DMA,
826177043Sthompsa	    ("Size: %zd - alignment %zd\n", size, alignment));
827173362Sbenjsc
828173362Sbenjsc	dma->size = size;
829173362Sbenjsc	dma->tag = NULL;
830173362Sbenjsc
831177043Sthompsa	if (alignment > 4096) {
832177043Sthompsa		align = PAGE_SIZE;
833177043Sthompsa		reqsize = size + alignment;
834177043Sthompsa	} else {
835177043Sthompsa		align = alignment;
836177043Sthompsa		reqsize = size;
837177043Sthompsa	}
838177043Sthompsa	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), align,
839173362Sbenjsc	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
840177043Sthompsa	    NULL, NULL, reqsize,
841177043Sthompsa	    1, reqsize, flags,
842173362Sbenjsc	    NULL, NULL, &dma->tag);
843173362Sbenjsc	if (error != 0) {
844173362Sbenjsc		device_printf(sc->sc_dev,
845173362Sbenjsc		    "could not create shared page DMA tag\n");
846173362Sbenjsc		goto fail;
847173362Sbenjsc	}
848177043Sthompsa	error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr_start,
849173362Sbenjsc	    flags | BUS_DMA_ZERO, &dma->map);
850173362Sbenjsc	if (error != 0) {
851173362Sbenjsc		device_printf(sc->sc_dev,
852173362Sbenjsc		    "could not allocate shared page DMA memory\n");
853173362Sbenjsc		goto fail;
854173362Sbenjsc	}
855173362Sbenjsc
856177043Sthompsa	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr_start,
857177043Sthompsa	    reqsize,  wpi_dma_map_addr, &dma->paddr_start, flags);
858177043Sthompsa
859177043Sthompsa	/* Save the original pointers so we can free all the memory */
860177043Sthompsa	dma->paddr = dma->paddr_start;
861177043Sthompsa	dma->vaddr = dma->vaddr_start;
862177043Sthompsa
863177043Sthompsa	/*
864177043Sthompsa	 * Check the alignment and increment by 4096 until we get the
865177043Sthompsa	 * requested alignment. Fail if can't obtain the alignment
866177043Sthompsa	 * we requested.
867173362Sbenjsc	 */
868177043Sthompsa	if ((dma->paddr & (alignment -1 )) != 0) {
869177043Sthompsa		int i;
870173362Sbenjsc
871177043Sthompsa		for (i = 0; i < alignment / 4096; i++) {
872177043Sthompsa			if ((dma->paddr & (alignment - 1 )) == 0)
873177043Sthompsa				break;
874177043Sthompsa			dma->paddr += 4096;
875177043Sthompsa			dma->vaddr += 4096;
876177043Sthompsa		}
877177043Sthompsa		if (i == alignment / 4096) {
878177043Sthompsa			device_printf(sc->sc_dev,
879177043Sthompsa			    "alignment requirement was not satisfied\n");
880177043Sthompsa			goto fail;
881177043Sthompsa		}
882173362Sbenjsc	}
883173362Sbenjsc
884173362Sbenjsc	if (error != 0) {
885173362Sbenjsc		device_printf(sc->sc_dev,
886173362Sbenjsc		    "could not load shared page DMA map\n");
887173362Sbenjsc		goto fail;
888173362Sbenjsc	}
889173362Sbenjsc
890173362Sbenjsc	if (kvap != NULL)
891173362Sbenjsc		*kvap = dma->vaddr;
892173362Sbenjsc
893173362Sbenjsc	return 0;
894173362Sbenjsc
895173362Sbenjscfail:
896173362Sbenjsc	wpi_dma_contig_free(dma);
897173362Sbenjsc	return error;
898173362Sbenjsc}
899173362Sbenjsc
900173362Sbenjscstatic void
901173362Sbenjscwpi_dma_contig_free(struct wpi_dma_info *dma)
902173362Sbenjsc{
903173362Sbenjsc	if (dma->tag) {
904267580Sjhb		if (dma->vaddr_start != NULL) {
905177043Sthompsa			if (dma->paddr_start != 0) {
906173362Sbenjsc				bus_dmamap_sync(dma->tag, dma->map,
907173362Sbenjsc				    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
908173362Sbenjsc				bus_dmamap_unload(dma->tag, dma->map);
909173362Sbenjsc			}
910267580Sjhb			bus_dmamem_free(dma->tag, dma->vaddr_start, dma->map);
911173362Sbenjsc		}
912173362Sbenjsc		bus_dma_tag_destroy(dma->tag);
913173362Sbenjsc	}
914173362Sbenjsc}
915173362Sbenjsc
916173362Sbenjsc/*
917173362Sbenjsc * Allocate a shared page between host and NIC.
918173362Sbenjsc */
919173362Sbenjscstatic int
920173362Sbenjscwpi_alloc_shared(struct wpi_softc *sc)
921173362Sbenjsc{
922173362Sbenjsc	int error;
923173362Sbenjsc
924173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &sc->shared_dma,
925173362Sbenjsc	    (void **)&sc->shared, sizeof (struct wpi_shared),
926173362Sbenjsc	    PAGE_SIZE,
927173362Sbenjsc	    BUS_DMA_NOWAIT);
928173362Sbenjsc
929173362Sbenjsc	if (error != 0) {
930173362Sbenjsc		device_printf(sc->sc_dev,
931173362Sbenjsc		    "could not allocate shared area DMA memory\n");
932173362Sbenjsc	}
933173362Sbenjsc
934173362Sbenjsc	return error;
935173362Sbenjsc}
936173362Sbenjsc
937173362Sbenjscstatic void
938173362Sbenjscwpi_free_shared(struct wpi_softc *sc)
939173362Sbenjsc{
940173362Sbenjsc	wpi_dma_contig_free(&sc->shared_dma);
941173362Sbenjsc}
942173362Sbenjsc
943173362Sbenjscstatic int
944173362Sbenjscwpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
945173362Sbenjsc{
946173362Sbenjsc
947173362Sbenjsc	int i, error;
948173362Sbenjsc
949173362Sbenjsc	ring->cur = 0;
950173362Sbenjsc
951173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
952177043Sthompsa	    (void **)&ring->desc, WPI_RX_RING_COUNT * sizeof (uint32_t),
953177043Sthompsa	    WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
954173362Sbenjsc
955173362Sbenjsc	if (error != 0) {
956177043Sthompsa		device_printf(sc->sc_dev,
957177043Sthompsa		    "%s: could not allocate rx ring DMA memory, error %d\n",
958177043Sthompsa		    __func__, error);
959177043Sthompsa		goto fail;
960173362Sbenjsc	}
961173362Sbenjsc
962177043Sthompsa        error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
963177043Sthompsa	    BUS_SPACE_MAXADDR_32BIT,
964177043Sthompsa            BUS_SPACE_MAXADDR, NULL, NULL, MJUMPAGESIZE, 1,
965177043Sthompsa            MJUMPAGESIZE, BUS_DMA_NOWAIT, NULL, NULL, &ring->data_dmat);
966177043Sthompsa        if (error != 0) {
967177043Sthompsa                device_printf(sc->sc_dev,
968177043Sthompsa		    "%s: bus_dma_tag_create_failed, error %d\n",
969177043Sthompsa		    __func__, error);
970177043Sthompsa                goto fail;
971177043Sthompsa        }
972177043Sthompsa
973173362Sbenjsc	/*
974177043Sthompsa	 * Setup Rx buffers.
975173362Sbenjsc	 */
976173362Sbenjsc	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
977177043Sthompsa		struct wpi_rx_data *data = &ring->data[i];
978177043Sthompsa		struct mbuf *m;
979177043Sthompsa		bus_addr_t paddr;
980173362Sbenjsc
981177043Sthompsa		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
982177043Sthompsa		if (error != 0) {
983173362Sbenjsc			device_printf(sc->sc_dev,
984177043Sthompsa			    "%s: bus_dmamap_create failed, error %d\n",
985177043Sthompsa			    __func__, error);
986173362Sbenjsc			goto fail;
987173362Sbenjsc		}
988243857Sglebius		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
989177043Sthompsa		if (m == NULL) {
990173362Sbenjsc			device_printf(sc->sc_dev,
991177043Sthompsa			   "%s: could not allocate rx mbuf\n", __func__);
992177043Sthompsa			error = ENOMEM;
993173362Sbenjsc			goto fail;
994173362Sbenjsc		}
995177043Sthompsa		/* map page */
996177043Sthompsa		error = bus_dmamap_load(ring->data_dmat, data->map,
997177043Sthompsa		    mtod(m, caddr_t), MJUMPAGESIZE,
998177043Sthompsa		    wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
999177043Sthompsa		if (error != 0 && error != EFBIG) {
1000177043Sthompsa			device_printf(sc->sc_dev,
1001177043Sthompsa			    "%s: bus_dmamap_load failed, error %d\n",
1002177043Sthompsa			    __func__, error);
1003177043Sthompsa			m_freem(m);
1004177043Sthompsa			error = ENOMEM;	/* XXX unique code */
1005173362Sbenjsc			goto fail;
1006173362Sbenjsc		}
1007177043Sthompsa		bus_dmamap_sync(ring->data_dmat, data->map,
1008177043Sthompsa		    BUS_DMASYNC_PREWRITE);
1009177043Sthompsa
1010177043Sthompsa		data->m = m;
1011177043Sthompsa		ring->desc[i] = htole32(paddr);
1012173362Sbenjsc	}
1013173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1014173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
1015173362Sbenjsc	return 0;
1016173362Sbenjscfail:
1017173362Sbenjsc	wpi_free_rx_ring(sc, ring);
1018173362Sbenjsc	return error;
1019173362Sbenjsc}
1020173362Sbenjsc
1021173362Sbenjscstatic void
1022173362Sbenjscwpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
1023173362Sbenjsc{
1024173362Sbenjsc	int ntries;
1025173362Sbenjsc
1026173362Sbenjsc	wpi_mem_lock(sc);
1027173362Sbenjsc
1028173362Sbenjsc	WPI_WRITE(sc, WPI_RX_CONFIG, 0);
1029173362Sbenjsc
1030173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1031173362Sbenjsc		if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE)
1032173362Sbenjsc			break;
1033173362Sbenjsc		DELAY(10);
1034173362Sbenjsc	}
1035173362Sbenjsc
1036173362Sbenjsc	wpi_mem_unlock(sc);
1037173362Sbenjsc
1038173362Sbenjsc#ifdef WPI_DEBUG
1039179957Sthompsa	if (ntries == 100 && wpi_debug > 0)
1040173362Sbenjsc		device_printf(sc->sc_dev, "timeout resetting Rx ring\n");
1041173362Sbenjsc#endif
1042173362Sbenjsc
1043173362Sbenjsc	ring->cur = 0;
1044173362Sbenjsc}
1045173362Sbenjsc
1046173362Sbenjscstatic void
1047173362Sbenjscwpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
1048173362Sbenjsc{
1049173362Sbenjsc	int i;
1050173362Sbenjsc
1051173362Sbenjsc	wpi_dma_contig_free(&ring->desc_dma);
1052173362Sbenjsc
1053216824Sbschmidt	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1054216824Sbschmidt		struct wpi_rx_data *data = &ring->data[i];
1055216824Sbschmidt
1056216824Sbschmidt		if (data->m != NULL) {
1057216824Sbschmidt			bus_dmamap_sync(ring->data_dmat, data->map,
1058216824Sbschmidt			    BUS_DMASYNC_POSTREAD);
1059216824Sbschmidt			bus_dmamap_unload(ring->data_dmat, data->map);
1060216824Sbschmidt			m_freem(data->m);
1061216824Sbschmidt		}
1062216824Sbschmidt		if (data->map != NULL)
1063216824Sbschmidt			bus_dmamap_destroy(ring->data_dmat, data->map);
1064216824Sbschmidt	}
1065173362Sbenjsc}
1066173362Sbenjsc
1067173362Sbenjscstatic int
1068173362Sbenjscwpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count,
1069173362Sbenjsc	int qid)
1070173362Sbenjsc{
1071173362Sbenjsc	struct wpi_tx_data *data;
1072173362Sbenjsc	int i, error;
1073173362Sbenjsc
1074173362Sbenjsc	ring->qid = qid;
1075173362Sbenjsc	ring->count = count;
1076173362Sbenjsc	ring->queued = 0;
1077173362Sbenjsc	ring->cur = 0;
1078173362Sbenjsc	ring->data = NULL;
1079173362Sbenjsc
1080173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
1081173362Sbenjsc		(void **)&ring->desc, count * sizeof (struct wpi_tx_desc),
1082173362Sbenjsc		WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
1083173362Sbenjsc
1084173362Sbenjsc	if (error != 0) {
1085173362Sbenjsc	    device_printf(sc->sc_dev, "could not allocate tx dma memory\n");
1086173362Sbenjsc	    goto fail;
1087173362Sbenjsc	}
1088173362Sbenjsc
1089173362Sbenjsc	/* update shared page with ring's base address */
1090173362Sbenjsc	sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
1091173362Sbenjsc
1092173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
1093173362Sbenjsc		count * sizeof (struct wpi_tx_cmd), WPI_RING_DMA_ALIGN,
1094173362Sbenjsc		BUS_DMA_NOWAIT);
1095173362Sbenjsc
1096173362Sbenjsc	if (error != 0) {
1097173362Sbenjsc		device_printf(sc->sc_dev,
1098173362Sbenjsc		    "could not allocate tx command DMA memory\n");
1099173362Sbenjsc		goto fail;
1100173362Sbenjsc	}
1101173362Sbenjsc
1102173362Sbenjsc	ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF,
1103173362Sbenjsc	    M_NOWAIT | M_ZERO);
1104173362Sbenjsc	if (ring->data == NULL) {
1105173362Sbenjsc		device_printf(sc->sc_dev,
1106173362Sbenjsc		    "could not allocate tx data slots\n");
1107173362Sbenjsc		goto fail;
1108173362Sbenjsc	}
1109173362Sbenjsc
1110173362Sbenjsc	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1111173362Sbenjsc	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
1112173362Sbenjsc	    WPI_MAX_SCATTER - 1, MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL,
1113173362Sbenjsc	    &ring->data_dmat);
1114173362Sbenjsc	if (error != 0) {
1115173362Sbenjsc		device_printf(sc->sc_dev, "could not create data DMA tag\n");
1116173362Sbenjsc		goto fail;
1117173362Sbenjsc	}
1118173362Sbenjsc
1119173362Sbenjsc	for (i = 0; i < count; i++) {
1120173362Sbenjsc		data = &ring->data[i];
1121173362Sbenjsc
1122173362Sbenjsc		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1123173362Sbenjsc		if (error != 0) {
1124173362Sbenjsc			device_printf(sc->sc_dev,
1125173362Sbenjsc			    "could not create tx buf DMA map\n");
1126173362Sbenjsc			goto fail;
1127173362Sbenjsc		}
1128173362Sbenjsc		bus_dmamap_sync(ring->data_dmat, data->map,
1129173362Sbenjsc		    BUS_DMASYNC_PREWRITE);
1130173362Sbenjsc	}
1131173362Sbenjsc
1132173362Sbenjsc	return 0;
1133173362Sbenjsc
1134177043Sthompsafail:
1135177043Sthompsa	wpi_free_tx_ring(sc, ring);
1136173362Sbenjsc	return error;
1137173362Sbenjsc}
1138173362Sbenjsc
1139173362Sbenjscstatic void
1140173362Sbenjscwpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1141173362Sbenjsc{
1142173362Sbenjsc	struct wpi_tx_data *data;
1143173362Sbenjsc	int i, ntries;
1144173362Sbenjsc
1145173362Sbenjsc	wpi_mem_lock(sc);
1146173362Sbenjsc
1147173362Sbenjsc	WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0);
1148173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1149173362Sbenjsc		if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid))
1150173362Sbenjsc			break;
1151173362Sbenjsc		DELAY(10);
1152173362Sbenjsc	}
1153173362Sbenjsc#ifdef WPI_DEBUG
1154179957Sthompsa	if (ntries == 100 && wpi_debug > 0)
1155173362Sbenjsc		device_printf(sc->sc_dev, "timeout resetting Tx ring %d\n",
1156173362Sbenjsc		    ring->qid);
1157173362Sbenjsc#endif
1158173362Sbenjsc	wpi_mem_unlock(sc);
1159173362Sbenjsc
1160173362Sbenjsc	for (i = 0; i < ring->count; i++) {
1161173362Sbenjsc		data = &ring->data[i];
1162173362Sbenjsc
1163173362Sbenjsc		if (data->m != NULL) {
1164173362Sbenjsc			bus_dmamap_unload(ring->data_dmat, data->map);
1165173362Sbenjsc			m_freem(data->m);
1166173362Sbenjsc			data->m = NULL;
1167173362Sbenjsc		}
1168173362Sbenjsc	}
1169173362Sbenjsc
1170173362Sbenjsc	ring->queued = 0;
1171173362Sbenjsc	ring->cur = 0;
1172173362Sbenjsc}
1173173362Sbenjsc
1174173362Sbenjscstatic void
1175173362Sbenjscwpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1176173362Sbenjsc{
1177173362Sbenjsc	struct wpi_tx_data *data;
1178173362Sbenjsc	int i;
1179173362Sbenjsc
1180173362Sbenjsc	wpi_dma_contig_free(&ring->desc_dma);
1181173362Sbenjsc	wpi_dma_contig_free(&ring->cmd_dma);
1182173362Sbenjsc
1183173362Sbenjsc	if (ring->data != NULL) {
1184173362Sbenjsc		for (i = 0; i < ring->count; i++) {
1185173362Sbenjsc			data = &ring->data[i];
1186173362Sbenjsc
1187173362Sbenjsc			if (data->m != NULL) {
1188173362Sbenjsc				bus_dmamap_sync(ring->data_dmat, data->map,
1189173362Sbenjsc				    BUS_DMASYNC_POSTWRITE);
1190173362Sbenjsc				bus_dmamap_unload(ring->data_dmat, data->map);
1191173362Sbenjsc				m_freem(data->m);
1192173362Sbenjsc				data->m = NULL;
1193173362Sbenjsc			}
1194173362Sbenjsc		}
1195173362Sbenjsc		free(ring->data, M_DEVBUF);
1196173362Sbenjsc	}
1197173362Sbenjsc
1198173362Sbenjsc	if (ring->data_dmat != NULL)
1199173362Sbenjsc		bus_dma_tag_destroy(ring->data_dmat);
1200173362Sbenjsc}
1201173362Sbenjsc
1202173362Sbenjscstatic int
1203173362Sbenjscwpi_shutdown(device_t dev)
1204173362Sbenjsc{
1205173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1206173362Sbenjsc
1207173362Sbenjsc	WPI_LOCK(sc);
1208173362Sbenjsc	wpi_stop_locked(sc);
1209173362Sbenjsc	wpi_unload_firmware(sc);
1210173362Sbenjsc	WPI_UNLOCK(sc);
1211173362Sbenjsc
1212173362Sbenjsc	return 0;
1213173362Sbenjsc}
1214173362Sbenjsc
1215173362Sbenjscstatic int
1216173362Sbenjscwpi_suspend(device_t dev)
1217173362Sbenjsc{
1218173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1219233387Sbschmidt	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
1220173362Sbenjsc
1221233387Sbschmidt	ieee80211_suspend_all(ic);
1222173362Sbenjsc	return 0;
1223173362Sbenjsc}
1224173362Sbenjsc
1225173362Sbenjscstatic int
1226173362Sbenjscwpi_resume(device_t dev)
1227173362Sbenjsc{
1228173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1229233387Sbschmidt	struct ieee80211com *ic = sc->sc_ifp->if_l2com;
1230173362Sbenjsc
1231173362Sbenjsc	pci_write_config(dev, 0x41, 0, 1);
1232173362Sbenjsc
1233233387Sbschmidt	ieee80211_resume_all(ic);
1234173362Sbenjsc	return 0;
1235173362Sbenjsc}
1236173362Sbenjsc
1237173362Sbenjsc/**
1238173362Sbenjsc * Called by net80211 when ever there is a change to 80211 state machine
1239173362Sbenjsc */
1240173362Sbenjscstatic int
1241178354Ssamwpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1242173362Sbenjsc{
1243178354Ssam	struct wpi_vap *wvp = WPI_VAP(vap);
1244178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1245173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
1246173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
1247178354Ssam	int error;
1248173362Sbenjsc
1249178354Ssam	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
1250178354Ssam		ieee80211_state_name[vap->iv_state],
1251178354Ssam		ieee80211_state_name[nstate], sc->flags));
1252173362Sbenjsc
1253191746Sthompsa	IEEE80211_UNLOCK(ic);
1254191746Sthompsa	WPI_LOCK(sc);
1255216238Sbschmidt	if (nstate == IEEE80211_S_SCAN && vap->iv_state != IEEE80211_S_INIT) {
1256216238Sbschmidt		/*
1257216238Sbschmidt		 * On !INIT -> SCAN transitions, we need to clear any possible
1258216238Sbschmidt		 * knowledge about associations.
1259216238Sbschmidt		 */
1260216238Sbschmidt		error = wpi_config(sc);
1261216238Sbschmidt		if (error != 0) {
1262216238Sbschmidt			device_printf(sc->sc_dev,
1263216238Sbschmidt			    "%s: device config failed, error %d\n",
1264216238Sbschmidt			    __func__, error);
1265216238Sbschmidt		}
1266216238Sbschmidt	}
1267216238Sbschmidt	if (nstate == IEEE80211_S_AUTH ||
1268216238Sbschmidt	    (nstate == IEEE80211_S_ASSOC && vap->iv_state == IEEE80211_S_RUN)) {
1269216238Sbschmidt		/*
1270216238Sbschmidt		 * The node must be registered in the firmware before auth.
1271216238Sbschmidt		 * Also the associd must be cleared on RUN -> ASSOC
1272216238Sbschmidt		 * transitions.
1273216238Sbschmidt		 */
1274191746Sthompsa		error = wpi_auth(sc, vap);
1275191746Sthompsa		if (error != 0) {
1276191746Sthompsa			device_printf(sc->sc_dev,
1277191746Sthompsa			    "%s: could not move to auth state, error %d\n",
1278191746Sthompsa			    __func__, error);
1279191746Sthompsa		}
1280173362Sbenjsc	}
1281178354Ssam	if (nstate == IEEE80211_S_RUN && vap->iv_state != IEEE80211_S_RUN) {
1282191746Sthompsa		error = wpi_run(sc, vap);
1283191746Sthompsa		if (error != 0) {
1284191746Sthompsa			device_printf(sc->sc_dev,
1285191746Sthompsa			    "%s: could not move to run state, error %d\n",
1286191746Sthompsa			    __func__, error);
1287191746Sthompsa		}
1288178354Ssam	}
1289178354Ssam	if (nstate == IEEE80211_S_RUN) {
1290178354Ssam		/* RUN -> RUN transition; just restart the timers */
1291178354Ssam		wpi_calib_timeout(sc);
1292178354Ssam		/* XXX split out rate control timer */
1293178354Ssam	}
1294191746Sthompsa	WPI_UNLOCK(sc);
1295191746Sthompsa	IEEE80211_LOCK(ic);
1296178354Ssam	return wvp->newstate(vap, nstate, arg);
1297173362Sbenjsc}
1298173362Sbenjsc
1299173362Sbenjsc/*
1300173362Sbenjsc * Grab exclusive access to NIC memory.
1301173362Sbenjsc */
1302173362Sbenjscstatic void
1303173362Sbenjscwpi_mem_lock(struct wpi_softc *sc)
1304173362Sbenjsc{
1305173362Sbenjsc	int ntries;
1306173362Sbenjsc	uint32_t tmp;
1307173362Sbenjsc
1308173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
1309173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC);
1310173362Sbenjsc
1311173362Sbenjsc	/* spin until we actually get the lock */
1312173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1313173362Sbenjsc		if ((WPI_READ(sc, WPI_GPIO_CTL) &
1314173362Sbenjsc			(WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK)
1315173362Sbenjsc			break;
1316173362Sbenjsc		DELAY(10);
1317173362Sbenjsc	}
1318173362Sbenjsc	if (ntries == 100)
1319173362Sbenjsc		device_printf(sc->sc_dev, "could not lock memory\n");
1320173362Sbenjsc}
1321173362Sbenjsc
1322173362Sbenjsc/*
1323173362Sbenjsc * Release lock on NIC memory.
1324173362Sbenjsc */
1325173362Sbenjscstatic void
1326173362Sbenjscwpi_mem_unlock(struct wpi_softc *sc)
1327173362Sbenjsc{
1328173362Sbenjsc	uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL);
1329173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC);
1330173362Sbenjsc}
1331173362Sbenjsc
1332173362Sbenjscstatic uint32_t
1333173362Sbenjscwpi_mem_read(struct wpi_softc *sc, uint16_t addr)
1334173362Sbenjsc{
1335173362Sbenjsc	WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr);
1336173362Sbenjsc	return WPI_READ(sc, WPI_READ_MEM_DATA);
1337173362Sbenjsc}
1338173362Sbenjsc
1339173362Sbenjscstatic void
1340173362Sbenjscwpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data)
1341173362Sbenjsc{
1342173362Sbenjsc	WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr);
1343173362Sbenjsc	WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data);
1344173362Sbenjsc}
1345173362Sbenjsc
1346173362Sbenjscstatic void
1347173362Sbenjscwpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr,
1348173362Sbenjsc    const uint32_t *data, int wlen)
1349173362Sbenjsc{
1350173362Sbenjsc	for (; wlen > 0; wlen--, data++, addr+=4)
1351173362Sbenjsc		wpi_mem_write(sc, addr, *data);
1352173362Sbenjsc}
1353173362Sbenjsc
1354173362Sbenjsc/*
1355173362Sbenjsc * Read data from the EEPROM.  We access EEPROM through the MAC instead of
1356173362Sbenjsc * using the traditional bit-bang method. Data is read up until len bytes have
1357173362Sbenjsc * been obtained.
1358173362Sbenjsc */
1359173362Sbenjscstatic uint16_t
1360173362Sbenjscwpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int len)
1361173362Sbenjsc{
1362173362Sbenjsc	int ntries;
1363173362Sbenjsc	uint32_t val;
1364173362Sbenjsc	uint8_t *out = data;
1365173362Sbenjsc
1366173362Sbenjsc	wpi_mem_lock(sc);
1367173362Sbenjsc
1368173362Sbenjsc	for (; len > 0; len -= 2, addr++) {
1369173362Sbenjsc		WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2);
1370173362Sbenjsc
1371173362Sbenjsc		for (ntries = 0; ntries < 10; ntries++) {
1372173362Sbenjsc			if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) & WPI_EEPROM_READY)
1373173362Sbenjsc				break;
1374173362Sbenjsc			DELAY(5);
1375173362Sbenjsc		}
1376173362Sbenjsc
1377173362Sbenjsc		if (ntries == 10) {
1378173362Sbenjsc			device_printf(sc->sc_dev, "could not read EEPROM\n");
1379173362Sbenjsc			return ETIMEDOUT;
1380173362Sbenjsc		}
1381173362Sbenjsc
1382173362Sbenjsc		*out++= val >> 16;
1383173362Sbenjsc		if (len > 1)
1384173362Sbenjsc			*out ++= val >> 24;
1385173362Sbenjsc	}
1386173362Sbenjsc
1387173362Sbenjsc	wpi_mem_unlock(sc);
1388173362Sbenjsc
1389173362Sbenjsc	return 0;
1390173362Sbenjsc}
1391173362Sbenjsc
1392173362Sbenjsc/*
1393173362Sbenjsc * The firmware text and data segments are transferred to the NIC using DMA.
1394173362Sbenjsc * The driver just copies the firmware into DMA-safe memory and tells the NIC
1395173362Sbenjsc * where to find it.  Once the NIC has copied the firmware into its internal
1396173362Sbenjsc * memory, we can free our local copy in the driver.
1397173362Sbenjsc */
1398173362Sbenjscstatic int
1399173362Sbenjscwpi_load_microcode(struct wpi_softc *sc, const uint8_t *fw, int size)
1400173362Sbenjsc{
1401173362Sbenjsc	int error, ntries;
1402173362Sbenjsc
1403173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,("Loading microcode  size 0x%x\n", size));
1404173362Sbenjsc
1405173362Sbenjsc	size /= sizeof(uint32_t);
1406173362Sbenjsc
1407173362Sbenjsc	wpi_mem_lock(sc);
1408173362Sbenjsc
1409173362Sbenjsc	wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE,
1410173362Sbenjsc	    (const uint32_t *)fw, size);
1411173362Sbenjsc
1412173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0);
1413173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT);
1414173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size);
1415173362Sbenjsc
1416173362Sbenjsc	/* run microcode */
1417173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN);
1418173362Sbenjsc
1419173362Sbenjsc	/* wait while the adapter is busy copying the firmware */
1420173362Sbenjsc	for (error = 0, ntries = 0; ntries < 1000; ntries++) {
1421173362Sbenjsc		uint32_t status = WPI_READ(sc, WPI_TX_STATUS);
1422173362Sbenjsc		DPRINTFN(WPI_DEBUG_HW,
1423173362Sbenjsc		    ("firmware status=0x%x, val=0x%x, result=0x%x\n", status,
1424173362Sbenjsc		     WPI_TX_IDLE(6), status & WPI_TX_IDLE(6)));
1425173362Sbenjsc		if (status & WPI_TX_IDLE(6)) {
1426173362Sbenjsc			DPRINTFN(WPI_DEBUG_HW,
1427173362Sbenjsc			    ("Status Match! - ntries = %d\n", ntries));
1428173362Sbenjsc			break;
1429173362Sbenjsc		}
1430173362Sbenjsc		DELAY(10);
1431173362Sbenjsc	}
1432173362Sbenjsc	if (ntries == 1000) {
1433173362Sbenjsc		device_printf(sc->sc_dev, "timeout transferring firmware\n");
1434173362Sbenjsc		error = ETIMEDOUT;
1435173362Sbenjsc	}
1436173362Sbenjsc
1437173362Sbenjsc	/* start the microcode executing */
1438173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_ENABLE);
1439173362Sbenjsc
1440173362Sbenjsc	wpi_mem_unlock(sc);
1441173362Sbenjsc
1442173362Sbenjsc	return (error);
1443173362Sbenjsc}
1444173362Sbenjsc
1445173362Sbenjscstatic void
1446173362Sbenjscwpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1447173362Sbenjsc	struct wpi_rx_data *data)
1448173362Sbenjsc{
1449178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1450178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1451173362Sbenjsc	struct wpi_rx_ring *ring = &sc->rxq;
1452173362Sbenjsc	struct wpi_rx_stat *stat;
1453173362Sbenjsc	struct wpi_rx_head *head;
1454173362Sbenjsc	struct wpi_rx_tail *tail;
1455173362Sbenjsc	struct ieee80211_node *ni;
1456173362Sbenjsc	struct mbuf *m, *mnew;
1457177043Sthompsa	bus_addr_t paddr;
1458177043Sthompsa	int error;
1459173362Sbenjsc
1460173362Sbenjsc	stat = (struct wpi_rx_stat *)(desc + 1);
1461173362Sbenjsc
1462173362Sbenjsc	if (stat->len > WPI_STAT_MAXLEN) {
1463173362Sbenjsc		device_printf(sc->sc_dev, "invalid rx statistic header\n");
1464173362Sbenjsc		ifp->if_ierrors++;
1465173362Sbenjsc		return;
1466173362Sbenjsc	}
1467173362Sbenjsc
1468216824Sbschmidt	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD);
1469173362Sbenjsc	head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1470173362Sbenjsc	tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + le16toh(head->len));
1471173362Sbenjsc
1472173362Sbenjsc	DPRINTFN(WPI_DEBUG_RX, ("rx intr: idx=%d len=%d stat len=%d rssi=%d "
1473173362Sbenjsc	    "rate=%x chan=%d tstamp=%ju\n", ring->cur, le32toh(desc->len),
1474173362Sbenjsc	    le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan,
1475173362Sbenjsc	    (uintmax_t)le64toh(tail->tstamp)));
1476173362Sbenjsc
1477190458Sjmallett	/* discard Rx frames with bad CRC early */
1478190458Sjmallett	if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1479190458Sjmallett		DPRINTFN(WPI_DEBUG_RX, ("%s: rx flags error %x\n", __func__,
1480190458Sjmallett		    le32toh(tail->flags)));
1481190458Sjmallett		ifp->if_ierrors++;
1482190458Sjmallett		return;
1483190458Sjmallett	}
1484190458Sjmallett	if (le16toh(head->len) < sizeof (struct ieee80211_frame)) {
1485190458Sjmallett		DPRINTFN(WPI_DEBUG_RX, ("%s: frame too short: %d\n", __func__,
1486190458Sjmallett		    le16toh(head->len)));
1487190458Sjmallett		ifp->if_ierrors++;
1488190458Sjmallett		return;
1489190458Sjmallett	}
1490190458Sjmallett
1491177043Sthompsa	/* XXX don't need mbuf, just dma buffer */
1492243857Sglebius	mnew = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1493177043Sthompsa	if (mnew == NULL) {
1494177043Sthompsa		DPRINTFN(WPI_DEBUG_RX, ("%s: no mbuf to restock ring\n",
1495177043Sthompsa		    __func__));
1496177043Sthompsa		ifp->if_ierrors++;
1497177043Sthompsa		return;
1498177043Sthompsa	}
1499216824Sbschmidt	bus_dmamap_unload(ring->data_dmat, data->map);
1500216824Sbschmidt
1501177043Sthompsa	error = bus_dmamap_load(ring->data_dmat, data->map,
1502177043Sthompsa	    mtod(mnew, caddr_t), MJUMPAGESIZE,
1503177043Sthompsa	    wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1504177043Sthompsa	if (error != 0 && error != EFBIG) {
1505177043Sthompsa		device_printf(sc->sc_dev,
1506177043Sthompsa		    "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1507177043Sthompsa		m_freem(mnew);
1508177043Sthompsa		ifp->if_ierrors++;
1509177043Sthompsa		return;
1510177043Sthompsa	}
1511177043Sthompsa	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1512177043Sthompsa
1513177043Sthompsa	/* finalize mbuf and swap in new one */
1514173362Sbenjsc	m = data->m;
1515173362Sbenjsc	m->m_pkthdr.rcvif = ifp;
1516173362Sbenjsc	m->m_data = (caddr_t)(head + 1);
1517173362Sbenjsc	m->m_pkthdr.len = m->m_len = le16toh(head->len);
1518173362Sbenjsc
1519177043Sthompsa	data->m = mnew;
1520177043Sthompsa	/* update Rx descriptor */
1521177043Sthompsa	ring->desc[ring->cur] = htole32(paddr);
1522173362Sbenjsc
1523192468Ssam	if (ieee80211_radiotap_active(ic)) {
1524173362Sbenjsc		struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
1525173362Sbenjsc
1526173362Sbenjsc		tap->wr_flags = 0;
1527173362Sbenjsc		tap->wr_chan_freq =
1528173362Sbenjsc			htole16(ic->ic_channels[head->chan].ic_freq);
1529173362Sbenjsc		tap->wr_chan_flags =
1530173362Sbenjsc			htole16(ic->ic_channels[head->chan].ic_flags);
1531173362Sbenjsc		tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
1532173362Sbenjsc		tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise);
1533173362Sbenjsc		tap->wr_tsft = tail->tstamp;
1534173362Sbenjsc		tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
1535173362Sbenjsc		switch (head->rate) {
1536173362Sbenjsc		/* CCK rates */
1537173362Sbenjsc		case  10: tap->wr_rate =   2; break;
1538173362Sbenjsc		case  20: tap->wr_rate =   4; break;
1539173362Sbenjsc		case  55: tap->wr_rate =  11; break;
1540173362Sbenjsc		case 110: tap->wr_rate =  22; break;
1541173362Sbenjsc		/* OFDM rates */
1542173362Sbenjsc		case 0xd: tap->wr_rate =  12; break;
1543173362Sbenjsc		case 0xf: tap->wr_rate =  18; break;
1544173362Sbenjsc		case 0x5: tap->wr_rate =  24; break;
1545173362Sbenjsc		case 0x7: tap->wr_rate =  36; break;
1546173362Sbenjsc		case 0x9: tap->wr_rate =  48; break;
1547173362Sbenjsc		case 0xb: tap->wr_rate =  72; break;
1548173362Sbenjsc		case 0x1: tap->wr_rate =  96; break;
1549173362Sbenjsc		case 0x3: tap->wr_rate = 108; break;
1550173362Sbenjsc		/* unknown rate: should not happen */
1551173362Sbenjsc		default:  tap->wr_rate =   0;
1552173362Sbenjsc		}
1553173362Sbenjsc		if (le16toh(head->flags) & 0x4)
1554173362Sbenjsc			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1555173362Sbenjsc	}
1556173362Sbenjsc
1557173362Sbenjsc	WPI_UNLOCK(sc);
1558173362Sbenjsc
1559177043Sthompsa	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1560178354Ssam	if (ni != NULL) {
1561192468Ssam		(void) ieee80211_input(ni, m, stat->rssi, 0);
1562178354Ssam		ieee80211_free_node(ni);
1563178354Ssam	} else
1564192468Ssam		(void) ieee80211_input_all(ic, m, stat->rssi, 0);
1565173362Sbenjsc
1566173362Sbenjsc	WPI_LOCK(sc);
1567173362Sbenjsc}
1568173362Sbenjsc
1569173362Sbenjscstatic void
1570173362Sbenjscwpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1571173362Sbenjsc{
1572178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1573173362Sbenjsc	struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
1574173362Sbenjsc	struct wpi_tx_data *txdata = &ring->data[desc->idx];
1575173362Sbenjsc	struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
1576206358Srpaulo	struct ieee80211_node *ni = txdata->ni;
1577206358Srpaulo	struct ieee80211vap *vap = ni->ni_vap;
1578206358Srpaulo	int retrycnt = 0;
1579173362Sbenjsc
1580173362Sbenjsc	DPRINTFN(WPI_DEBUG_TX, ("tx done: qid=%d idx=%d retries=%d nkill=%d "
1581173362Sbenjsc	    "rate=%x duration=%d status=%x\n", desc->qid, desc->idx,
1582173362Sbenjsc	    stat->ntries, stat->nkill, stat->rate, le32toh(stat->duration),
1583173362Sbenjsc	    le32toh(stat->status)));
1584173362Sbenjsc
1585173362Sbenjsc	/*
1586173362Sbenjsc	 * Update rate control statistics for the node.
1587173362Sbenjsc	 * XXX we should not count mgmt frames since they're always sent at
1588173362Sbenjsc	 * the lowest available bit-rate.
1589173362Sbenjsc	 * XXX frames w/o ACK shouldn't be used either
1590173362Sbenjsc	 */
1591173362Sbenjsc	if (stat->ntries > 0) {
1592190462Sjmallett		DPRINTFN(WPI_DEBUG_TX, ("%d retries\n", stat->ntries));
1593206358Srpaulo		retrycnt = 1;
1594173362Sbenjsc	}
1595206358Srpaulo	ieee80211_ratectl_tx_complete(vap, ni, IEEE80211_RATECTL_TX_SUCCESS,
1596206358Srpaulo	    &retrycnt, NULL);
1597173362Sbenjsc
1598173362Sbenjsc	/* XXX oerrors should only count errors !maxtries */
1599173362Sbenjsc	if ((le32toh(stat->status) & 0xff) != 1)
1600173362Sbenjsc		ifp->if_oerrors++;
1601173362Sbenjsc	else
1602173362Sbenjsc		ifp->if_opackets++;
1603173362Sbenjsc
1604173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, txdata->map, BUS_DMASYNC_POSTWRITE);
1605173362Sbenjsc	bus_dmamap_unload(ring->data_dmat, txdata->map);
1606173362Sbenjsc	/* XXX handle M_TXCB? */
1607173362Sbenjsc	m_freem(txdata->m);
1608173362Sbenjsc	txdata->m = NULL;
1609173362Sbenjsc	ieee80211_free_node(txdata->ni);
1610173362Sbenjsc	txdata->ni = NULL;
1611173362Sbenjsc
1612173362Sbenjsc	ring->queued--;
1613173362Sbenjsc
1614173362Sbenjsc	sc->sc_tx_timer = 0;
1615173362Sbenjsc	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1616178354Ssam	wpi_start_locked(ifp);
1617173362Sbenjsc}
1618173362Sbenjsc
1619173362Sbenjscstatic void
1620173362Sbenjscwpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1621173362Sbenjsc{
1622173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
1623173362Sbenjsc	struct wpi_tx_data *data;
1624173362Sbenjsc
1625173362Sbenjsc	DPRINTFN(WPI_DEBUG_CMD, ("cmd notification qid=%x idx=%d flags=%x "
1626173362Sbenjsc				 "type=%s len=%d\n", desc->qid, desc->idx,
1627173362Sbenjsc				 desc->flags, wpi_cmd_str(desc->type),
1628173362Sbenjsc				 le32toh(desc->len)));
1629173362Sbenjsc
1630173362Sbenjsc	if ((desc->qid & 7) != 4)
1631173362Sbenjsc		return;	/* not a command ack */
1632173362Sbenjsc
1633173362Sbenjsc	data = &ring->data[desc->idx];
1634173362Sbenjsc
1635173362Sbenjsc	/* if the command was mapped in a mbuf, free it */
1636173362Sbenjsc	if (data->m != NULL) {
1637173362Sbenjsc		bus_dmamap_unload(ring->data_dmat, data->map);
1638173362Sbenjsc		m_freem(data->m);
1639173362Sbenjsc		data->m = NULL;
1640173362Sbenjsc	}
1641173362Sbenjsc
1642173362Sbenjsc	sc->flags &= ~WPI_FLAG_BUSY;
1643173362Sbenjsc	wakeup(&ring->cmd[desc->idx]);
1644173362Sbenjsc}
1645173362Sbenjsc
1646173362Sbenjscstatic void
1647173362Sbenjscwpi_notif_intr(struct wpi_softc *sc)
1648173362Sbenjsc{
1649178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1650178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1651173362Sbenjsc	struct wpi_rx_desc *desc;
1652173362Sbenjsc	struct wpi_rx_data *data;
1653173362Sbenjsc	uint32_t hw;
1654173362Sbenjsc
1655216523Sbschmidt	bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
1656216523Sbschmidt	    BUS_DMASYNC_POSTREAD);
1657216523Sbschmidt
1658173362Sbenjsc	hw = le32toh(sc->shared->next);
1659173362Sbenjsc	while (sc->rxq.cur != hw) {
1660173362Sbenjsc		data = &sc->rxq.data[sc->rxq.cur];
1661216523Sbschmidt
1662216523Sbschmidt		bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1663216523Sbschmidt		    BUS_DMASYNC_POSTREAD);
1664173362Sbenjsc		desc = (void *)data->m->m_ext.ext_buf;
1665173362Sbenjsc
1666173362Sbenjsc		DPRINTFN(WPI_DEBUG_NOTIFY,
1667173362Sbenjsc			 ("notify qid=%x idx=%d flags=%x type=%d len=%d\n",
1668173362Sbenjsc			  desc->qid,
1669173362Sbenjsc			  desc->idx,
1670173362Sbenjsc			  desc->flags,
1671173362Sbenjsc			  desc->type,
1672173362Sbenjsc			  le32toh(desc->len)));
1673173362Sbenjsc
1674173362Sbenjsc		if (!(desc->qid & 0x80))	/* reply to a command */
1675173362Sbenjsc			wpi_cmd_intr(sc, desc);
1676173362Sbenjsc
1677173362Sbenjsc		switch (desc->type) {
1678173362Sbenjsc		case WPI_RX_DONE:
1679173362Sbenjsc			/* a 802.11 frame was received */
1680173362Sbenjsc			wpi_rx_intr(sc, desc, data);
1681173362Sbenjsc			break;
1682173362Sbenjsc
1683173362Sbenjsc		case WPI_TX_DONE:
1684173362Sbenjsc			/* a 802.11 frame has been transmitted */
1685173362Sbenjsc			wpi_tx_intr(sc, desc);
1686173362Sbenjsc			break;
1687173362Sbenjsc
1688173362Sbenjsc		case WPI_UC_READY:
1689173362Sbenjsc		{
1690173362Sbenjsc			struct wpi_ucode_info *uc =
1691173362Sbenjsc				(struct wpi_ucode_info *)(desc + 1);
1692173362Sbenjsc
1693173362Sbenjsc			/* the microcontroller is ready */
1694173362Sbenjsc			DPRINTF(("microcode alive notification version %x "
1695173362Sbenjsc				"alive %x\n", le32toh(uc->version),
1696173362Sbenjsc				le32toh(uc->valid)));
1697173362Sbenjsc
1698173362Sbenjsc			if (le32toh(uc->valid) != 1) {
1699173362Sbenjsc				device_printf(sc->sc_dev,
1700173362Sbenjsc				    "microcontroller initialization failed\n");
1701173362Sbenjsc				wpi_stop_locked(sc);
1702173362Sbenjsc			}
1703173362Sbenjsc			break;
1704173362Sbenjsc		}
1705173362Sbenjsc		case WPI_STATE_CHANGED:
1706173362Sbenjsc		{
1707173362Sbenjsc			uint32_t *status = (uint32_t *)(desc + 1);
1708173362Sbenjsc
1709173362Sbenjsc			/* enabled/disabled notification */
1710173362Sbenjsc			DPRINTF(("state changed to %x\n", le32toh(*status)));
1711173362Sbenjsc
1712173362Sbenjsc			if (le32toh(*status) & 1) {
1713173362Sbenjsc				device_printf(sc->sc_dev,
1714173362Sbenjsc				    "Radio transmitter is switched off\n");
1715173362Sbenjsc				sc->flags |= WPI_FLAG_HW_RADIO_OFF;
1716177043Sthompsa				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1717177043Sthompsa				/* Disable firmware commands */
1718177043Sthompsa				WPI_WRITE(sc, WPI_UCODE_SET, WPI_DISABLE_CMD);
1719173362Sbenjsc			}
1720173362Sbenjsc			break;
1721173362Sbenjsc		}
1722173362Sbenjsc		case WPI_START_SCAN:
1723173362Sbenjsc		{
1724179957Sthompsa#ifdef WPI_DEBUG
1725173362Sbenjsc			struct wpi_start_scan *scan =
1726173362Sbenjsc				(struct wpi_start_scan *)(desc + 1);
1727179957Sthompsa#endif
1728173362Sbenjsc
1729173362Sbenjsc			DPRINTFN(WPI_DEBUG_SCANNING,
1730173362Sbenjsc				 ("scanning channel %d status %x\n",
1731173362Sbenjsc			    scan->chan, le32toh(scan->status)));
1732173362Sbenjsc			break;
1733173362Sbenjsc		}
1734173362Sbenjsc		case WPI_STOP_SCAN:
1735173362Sbenjsc		{
1736179957Sthompsa#ifdef WPI_DEBUG
1737173362Sbenjsc			struct wpi_stop_scan *scan =
1738173362Sbenjsc				(struct wpi_stop_scan *)(desc + 1);
1739179957Sthompsa#endif
1740178354Ssam			struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1741173362Sbenjsc
1742173362Sbenjsc			DPRINTFN(WPI_DEBUG_SCANNING,
1743173362Sbenjsc			    ("scan finished nchan=%d status=%d chan=%d\n",
1744173362Sbenjsc			     scan->nchan, scan->status, scan->chan));
1745173362Sbenjsc
1746177043Sthompsa			sc->sc_scan_timer = 0;
1747178354Ssam			ieee80211_scan_next(vap);
1748173362Sbenjsc			break;
1749173362Sbenjsc		}
1750173976Sbenjsc		case WPI_MISSED_BEACON:
1751173976Sbenjsc		{
1752178354Ssam			struct wpi_missed_beacon *beacon =
1753173976Sbenjsc				(struct wpi_missed_beacon *)(desc + 1);
1754178354Ssam			struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1755173976Sbenjsc
1756178354Ssam			if (le32toh(beacon->consecutive) >=
1757178354Ssam			    vap->iv_bmissthreshold) {
1758178354Ssam				DPRINTF(("Beacon miss: %u >= %u\n",
1759178354Ssam					 le32toh(beacon->consecutive),
1760178354Ssam					 vap->iv_bmissthreshold));
1761191746Sthompsa				ieee80211_beacon_miss(ic);
1762178354Ssam			}
1763178354Ssam			break;
1764173362Sbenjsc		}
1765173976Sbenjsc		}
1766173362Sbenjsc
1767173362Sbenjsc		sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
1768173362Sbenjsc	}
1769173362Sbenjsc
1770173362Sbenjsc	/* tell the firmware what we have processed */
1771173362Sbenjsc	hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
1772173362Sbenjsc	WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7);
1773173362Sbenjsc}
1774173362Sbenjsc
1775173362Sbenjscstatic void
1776173362Sbenjscwpi_intr(void *arg)
1777173362Sbenjsc{
1778173362Sbenjsc	struct wpi_softc *sc = arg;
1779173362Sbenjsc	uint32_t r;
1780173362Sbenjsc
1781173362Sbenjsc	WPI_LOCK(sc);
1782173362Sbenjsc
1783173362Sbenjsc	r = WPI_READ(sc, WPI_INTR);
1784173362Sbenjsc	if (r == 0 || r == 0xffffffff) {
1785173362Sbenjsc		WPI_UNLOCK(sc);
1786173362Sbenjsc		return;
1787173362Sbenjsc	}
1788173362Sbenjsc
1789173362Sbenjsc	/* disable interrupts */
1790173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, 0);
1791173362Sbenjsc	/* ack interrupts */
1792173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, r);
1793173362Sbenjsc
1794173362Sbenjsc	if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) {
1795191746Sthompsa		struct ifnet *ifp = sc->sc_ifp;
1796191746Sthompsa		struct ieee80211com *ic = ifp->if_l2com;
1797191956Sthompsa		struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1798191746Sthompsa
1799173362Sbenjsc		device_printf(sc->sc_dev, "fatal firmware error\n");
1800173362Sbenjsc		DPRINTFN(6,("(%s)\n", (r & WPI_SW_ERROR) ? "(Software Error)" :
1801173362Sbenjsc				"(Hardware Error)"));
1802191956Sthompsa		if (vap != NULL)
1803191956Sthompsa			ieee80211_cancel_scan(vap);
1804191746Sthompsa		ieee80211_runtask(ic, &sc->sc_restarttask);
1805173362Sbenjsc		sc->flags &= ~WPI_FLAG_BUSY;
1806173362Sbenjsc		WPI_UNLOCK(sc);
1807173362Sbenjsc		return;
1808173362Sbenjsc	}
1809173362Sbenjsc
1810173362Sbenjsc	if (r & WPI_RX_INTR)
1811173362Sbenjsc		wpi_notif_intr(sc);
1812173362Sbenjsc
1813173362Sbenjsc	if (r & WPI_ALIVE_INTR)	/* firmware initialized */
1814173362Sbenjsc		wakeup(sc);
1815173362Sbenjsc
1816173362Sbenjsc	/* re-enable interrupts */
1817173362Sbenjsc	if (sc->sc_ifp->if_flags & IFF_UP)
1818173362Sbenjsc		WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
1819173362Sbenjsc
1820173362Sbenjsc	WPI_UNLOCK(sc);
1821173362Sbenjsc}
1822173362Sbenjsc
1823173362Sbenjscstatic uint8_t
1824173362Sbenjscwpi_plcp_signal(int rate)
1825173362Sbenjsc{
1826173362Sbenjsc	switch (rate) {
1827173362Sbenjsc	/* CCK rates (returned values are device-dependent) */
1828173362Sbenjsc	case 2:		return 10;
1829173362Sbenjsc	case 4:		return 20;
1830173362Sbenjsc	case 11:	return 55;
1831173362Sbenjsc	case 22:	return 110;
1832173362Sbenjsc
1833173362Sbenjsc	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1834173362Sbenjsc	/* R1-R4 (ral/ural is R4-R1) */
1835173362Sbenjsc	case 12:	return 0xd;
1836173362Sbenjsc	case 18:	return 0xf;
1837173362Sbenjsc	case 24:	return 0x5;
1838173362Sbenjsc	case 36:	return 0x7;
1839173362Sbenjsc	case 48:	return 0x9;
1840173362Sbenjsc	case 72:	return 0xb;
1841173362Sbenjsc	case 96:	return 0x1;
1842173362Sbenjsc	case 108:	return 0x3;
1843173362Sbenjsc
1844173362Sbenjsc	/* unsupported rates (should not get there) */
1845173362Sbenjsc	default:	return 0;
1846173362Sbenjsc	}
1847173362Sbenjsc}
1848173362Sbenjsc
1849173362Sbenjsc/* quickly determine if a given rate is CCK or OFDM */
1850173362Sbenjsc#define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1851173362Sbenjsc
1852173362Sbenjsc/*
1853173362Sbenjsc * Construct the data packet for a transmit buffer and acutally put
1854173362Sbenjsc * the buffer onto the transmit ring, kicking the card to process the
1855173362Sbenjsc * the buffer.
1856173362Sbenjsc */
1857173362Sbenjscstatic int
1858173362Sbenjscwpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1859173362Sbenjsc	int ac)
1860173362Sbenjsc{
1861178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1862178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1863178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1864177043Sthompsa	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1865173362Sbenjsc	struct wpi_tx_ring *ring = &sc->txq[ac];
1866173362Sbenjsc	struct wpi_tx_desc *desc;
1867173362Sbenjsc	struct wpi_tx_data *data;
1868173362Sbenjsc	struct wpi_tx_cmd *cmd;
1869173362Sbenjsc	struct wpi_cmd_data *tx;
1870173362Sbenjsc	struct ieee80211_frame *wh;
1871178354Ssam	const struct ieee80211_txparam *tp;
1872173362Sbenjsc	struct ieee80211_key *k;
1873173362Sbenjsc	struct mbuf *mnew;
1874177043Sthompsa	int i, error, nsegs, rate, hdrlen, ismcast;
1875173362Sbenjsc	bus_dma_segment_t segs[WPI_MAX_SCATTER];
1876173362Sbenjsc
1877173362Sbenjsc	desc = &ring->desc[ring->cur];
1878173362Sbenjsc	data = &ring->data[ring->cur];
1879173362Sbenjsc
1880173362Sbenjsc	wh = mtod(m0, struct ieee80211_frame *);
1881173362Sbenjsc
1882177043Sthompsa	hdrlen = ieee80211_hdrsize(wh);
1883177043Sthompsa	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1884173362Sbenjsc
1885260444Skevlo	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1886178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1887177043Sthompsa		if (k == NULL) {
1888173362Sbenjsc			m_freem(m0);
1889173362Sbenjsc			return ENOBUFS;
1890173362Sbenjsc		}
1891173362Sbenjsc		/* packet header may have moved, reset our local pointer */
1892173362Sbenjsc		wh = mtod(m0, struct ieee80211_frame *);
1893173362Sbenjsc	}
1894173362Sbenjsc
1895173362Sbenjsc	cmd = &ring->cmd[ring->cur];
1896173362Sbenjsc	cmd->code = WPI_CMD_TX_DATA;
1897173362Sbenjsc	cmd->flags = 0;
1898173362Sbenjsc	cmd->qid = ring->qid;
1899173362Sbenjsc	cmd->idx = ring->cur;
1900173362Sbenjsc
1901173362Sbenjsc	tx = (struct wpi_cmd_data *)cmd->data;
1902177043Sthompsa	tx->flags = htole32(WPI_TX_AUTO_SEQ);
1903178354Ssam	tx->timeout = htole16(0);
1904177043Sthompsa	tx->ofdm_mask = 0xff;
1905177043Sthompsa	tx->cck_mask = 0x0f;
1906177043Sthompsa	tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
1907177043Sthompsa	tx->id = ismcast ? WPI_ID_BROADCAST : WPI_ID_BSS;
1908177043Sthompsa	tx->len = htole16(m0->m_pkthdr.len);
1909173362Sbenjsc
1910177119Ssam	if (!ismcast) {
1911177043Sthompsa		if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0 ||
1912177043Sthompsa		    !cap->cap_wmeParams[ac].wmep_noackPolicy)
1913177043Sthompsa			tx->flags |= htole32(WPI_TX_NEED_ACK);
1914178354Ssam		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
1915177043Sthompsa			tx->flags |= htole32(WPI_TX_NEED_RTS|WPI_TX_FULL_TXOP);
1916177043Sthompsa			tx->rts_ntries = 7;
1917177043Sthompsa		}
1918173362Sbenjsc	}
1919177043Sthompsa	/* pick a rate */
1920178354Ssam	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1921178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT) {
1922173362Sbenjsc		uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1923173362Sbenjsc		/* tell h/w to set timestamp in probe responses */
1924173362Sbenjsc		if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1925177043Sthompsa			tx->flags |= htole32(WPI_TX_INSERT_TSTAMP);
1926173362Sbenjsc		if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
1927177043Sthompsa		    subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
1928173362Sbenjsc			tx->timeout = htole16(3);
1929173362Sbenjsc		else
1930173362Sbenjsc			tx->timeout = htole16(2);
1931178354Ssam		rate = tp->mgmtrate;
1932177043Sthompsa	} else if (ismcast) {
1933178354Ssam		rate = tp->mcastrate;
1934178354Ssam	} else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1935178354Ssam		rate = tp->ucastrate;
1936177043Sthompsa	} else {
1937206358Srpaulo		(void) ieee80211_ratectl_rate(ni, NULL, 0);
1938178354Ssam		rate = ni->ni_txrate;
1939177043Sthompsa	}
1940173362Sbenjsc	tx->rate = wpi_plcp_signal(rate);
1941173362Sbenjsc
1942173362Sbenjsc	/* be very persistant at sending frames out */
1943178354Ssam#if 0
1944178354Ssam	tx->data_ntries = tp->maxretry;
1945178354Ssam#else
1946178354Ssam	tx->data_ntries = 15;		/* XXX way too high */
1947178354Ssam#endif
1948173362Sbenjsc
1949192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1950177043Sthompsa		struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
1951177043Sthompsa		tap->wt_flags = 0;
1952177043Sthompsa		tap->wt_rate = rate;
1953177043Sthompsa		tap->wt_hwqueue = ac;
1954260444Skevlo		if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
1955177043Sthompsa			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1956178354Ssam
1957192468Ssam		ieee80211_radiotap_tx(vap, m0);
1958177043Sthompsa	}
1959173362Sbenjsc
1960173362Sbenjsc	/* save and trim IEEE802.11 header */
1961173362Sbenjsc	m_copydata(m0, 0, hdrlen, (caddr_t)&tx->wh);
1962173362Sbenjsc	m_adj(m0, hdrlen);
1963173362Sbenjsc
1964173362Sbenjsc	error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m0, segs,
1965173362Sbenjsc	    &nsegs, BUS_DMA_NOWAIT);
1966173362Sbenjsc	if (error != 0 && error != EFBIG) {
1967173362Sbenjsc		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1968173362Sbenjsc		    error);
1969173362Sbenjsc		m_freem(m0);
1970173362Sbenjsc		return error;
1971173362Sbenjsc	}
1972173362Sbenjsc	if (error != 0) {
1973175418Sjhb		/* XXX use m_collapse */
1974243857Sglebius		mnew = m_defrag(m0, M_NOWAIT);
1975173362Sbenjsc		if (mnew == NULL) {
1976173362Sbenjsc			device_printf(sc->sc_dev,
1977173362Sbenjsc			    "could not defragment mbuf\n");
1978173362Sbenjsc			m_freem(m0);
1979173362Sbenjsc			return ENOBUFS;
1980173362Sbenjsc		}
1981173362Sbenjsc		m0 = mnew;
1982173362Sbenjsc
1983173362Sbenjsc		error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
1984173362Sbenjsc		    m0, segs, &nsegs, BUS_DMA_NOWAIT);
1985173362Sbenjsc		if (error != 0) {
1986173362Sbenjsc			device_printf(sc->sc_dev,
1987173362Sbenjsc			    "could not map mbuf (error %d)\n", error);
1988173362Sbenjsc			m_freem(m0);
1989173362Sbenjsc			return error;
1990173362Sbenjsc		}
1991173362Sbenjsc	}
1992173362Sbenjsc
1993173362Sbenjsc	data->m = m0;
1994173362Sbenjsc	data->ni = ni;
1995173362Sbenjsc
1996173362Sbenjsc	DPRINTFN(WPI_DEBUG_TX, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
1997173362Sbenjsc	    ring->qid, ring->cur, m0->m_pkthdr.len, nsegs));
1998173362Sbenjsc
1999173362Sbenjsc	/* first scatter/gather segment is used by the tx data command */
2000173362Sbenjsc	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 |
2001173362Sbenjsc	    (1 + nsegs) << 24);
2002173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2003173362Sbenjsc	    ring->cur * sizeof (struct wpi_tx_cmd));
2004173362Sbenjsc	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_data));
2005173362Sbenjsc	for (i = 1; i <= nsegs; i++) {
2006173362Sbenjsc		desc->segs[i].addr = htole32(segs[i - 1].ds_addr);
2007173362Sbenjsc		desc->segs[i].len  = htole32(segs[i - 1].ds_len);
2008173362Sbenjsc	}
2009173362Sbenjsc
2010173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2011173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2012173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
2013173362Sbenjsc
2014173362Sbenjsc	ring->queued++;
2015173362Sbenjsc
2016173362Sbenjsc	/* kick ring */
2017173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
2018173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2019173362Sbenjsc
2020173362Sbenjsc	return 0;
2021173362Sbenjsc}
2022173362Sbenjsc
2023173362Sbenjsc/**
2024173362Sbenjsc * Process data waiting to be sent on the IFNET output queue
2025173362Sbenjsc */
2026173362Sbenjscstatic void
2027173362Sbenjscwpi_start(struct ifnet *ifp)
2028173362Sbenjsc{
2029173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
2030178354Ssam
2031178354Ssam	WPI_LOCK(sc);
2032178354Ssam	wpi_start_locked(ifp);
2033178354Ssam	WPI_UNLOCK(sc);
2034178354Ssam}
2035178354Ssam
2036178354Ssamstatic void
2037178354Ssamwpi_start_locked(struct ifnet *ifp)
2038178354Ssam{
2039178354Ssam	struct wpi_softc *sc = ifp->if_softc;
2040173362Sbenjsc	struct ieee80211_node *ni;
2041178354Ssam	struct mbuf *m;
2042178354Ssam	int ac;
2043173362Sbenjsc
2044178354Ssam	WPI_LOCK_ASSERT(sc);
2045178354Ssam
2046177043Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2047177043Sthompsa		return;
2048173362Sbenjsc
2049173362Sbenjsc	for (;;) {
2050190458Sjmallett		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2051178354Ssam		if (m == NULL)
2052178354Ssam			break;
2053178354Ssam		ac = M_WME_GETAC(m);
2054178354Ssam		if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
2055178354Ssam			/* there is no place left in this ring */
2056178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m);
2057178354Ssam			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2058178354Ssam			break;
2059178354Ssam		}
2060178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
2061178354Ssam		if (wpi_tx_data(sc, m, ni, ac) != 0) {
2062178354Ssam			ieee80211_free_node(ni);
2063178354Ssam			ifp->if_oerrors++;
2064178354Ssam			break;
2065178354Ssam		}
2066178354Ssam		sc->sc_tx_timer = 5;
2067178354Ssam	}
2068178354Ssam}
2069173362Sbenjsc
2070178354Ssamstatic int
2071178354Ssamwpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2072178354Ssam	const struct ieee80211_bpf_params *params)
2073178354Ssam{
2074178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2075178354Ssam	struct ifnet *ifp = ic->ic_ifp;
2076178354Ssam	struct wpi_softc *sc = ifp->if_softc;
2077173362Sbenjsc
2078178354Ssam	/* prevent management frames from being sent if we're not ready */
2079178354Ssam	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2080178354Ssam		m_freem(m);
2081178354Ssam		ieee80211_free_node(ni);
2082178354Ssam		return ENETDOWN;
2083178354Ssam	}
2084178354Ssam	WPI_LOCK(sc);
2085173362Sbenjsc
2086178354Ssam	/* management frames go into ring 0 */
2087178354Ssam	if (sc->txq[0].queued > sc->txq[0].count - 8) {
2088178354Ssam		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2089178354Ssam		m_freem(m);
2090178354Ssam		WPI_UNLOCK(sc);
2091178354Ssam		ieee80211_free_node(ni);
2092178354Ssam		return ENOBUFS;		/* XXX */
2093178354Ssam	}
2094173362Sbenjsc
2095178354Ssam	ifp->if_opackets++;
2096178354Ssam	if (wpi_tx_data(sc, m, ni, 0) != 0)
2097178354Ssam		goto bad;
2098178354Ssam	sc->sc_tx_timer = 5;
2099178354Ssam	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
2100173362Sbenjsc
2101178354Ssam	WPI_UNLOCK(sc);
2102178354Ssam	return 0;
2103178354Ssambad:
2104178354Ssam	ifp->if_oerrors++;
2105178354Ssam	WPI_UNLOCK(sc);
2106178354Ssam	ieee80211_free_node(ni);
2107178354Ssam	return EIO;		/* XXX */
2108173362Sbenjsc}
2109173362Sbenjsc
2110173362Sbenjscstatic int
2111173362Sbenjscwpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2112173362Sbenjsc{
2113173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
2114178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2115178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
2116178354Ssam	int error = 0, startall = 0;
2117173362Sbenjsc
2118173362Sbenjsc	switch (cmd) {
2119173362Sbenjsc	case SIOCSIFFLAGS:
2120178704Sthompsa		WPI_LOCK(sc);
2121173362Sbenjsc		if ((ifp->if_flags & IFF_UP)) {
2122178354Ssam			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2123177043Sthompsa				wpi_init_locked(sc, 0);
2124178354Ssam				startall = 1;
2125178354Ssam			}
2126177043Sthompsa		} else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) ||
2127177043Sthompsa			   (sc->flags & WPI_FLAG_HW_RADIO_OFF))
2128173362Sbenjsc			wpi_stop_locked(sc);
2129178704Sthompsa		WPI_UNLOCK(sc);
2130178704Sthompsa		if (startall)
2131178704Sthompsa			ieee80211_start_all(ic);
2132173362Sbenjsc		break;
2133178354Ssam	case SIOCGIFMEDIA:
2134178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2135178354Ssam		break;
2136178704Sthompsa	case SIOCGIFADDR:
2137178354Ssam		error = ether_ioctl(ifp, cmd, data);
2138178354Ssam		break;
2139178704Sthompsa	default:
2140178704Sthompsa		error = EINVAL;
2141178704Sthompsa		break;
2142173362Sbenjsc	}
2143173362Sbenjsc	return error;
2144173362Sbenjsc}
2145173362Sbenjsc
2146173362Sbenjsc/*
2147173362Sbenjsc * Extract various information from EEPROM.
2148173362Sbenjsc */
2149173362Sbenjscstatic void
2150190526Ssamwpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
2151173362Sbenjsc{
2152173362Sbenjsc	int i;
2153173362Sbenjsc
2154173362Sbenjsc	/* read the hardware capabilities, revision and SKU type */
2155173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap,1);
2156173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,2);
2157173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1);
2158173362Sbenjsc
2159173362Sbenjsc	/* read the regulatory domain */
2160173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain, 4);
2161173362Sbenjsc
2162173362Sbenjsc	/* read in the hw MAC address */
2163190526Ssam	wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr, 6);
2164173362Sbenjsc
2165173362Sbenjsc	/* read the list of authorized channels */
2166173362Sbenjsc	for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
2167173362Sbenjsc		wpi_read_eeprom_channels(sc,i);
2168173362Sbenjsc
2169173362Sbenjsc	/* read the power level calibration info for each group */
2170173362Sbenjsc	for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
2171173362Sbenjsc		wpi_read_eeprom_group(sc,i);
2172173362Sbenjsc}
2173173362Sbenjsc
2174173362Sbenjsc/*
2175173362Sbenjsc * Send a command to the firmware.
2176173362Sbenjsc */
2177173362Sbenjscstatic int
2178173362Sbenjscwpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
2179173362Sbenjsc{
2180173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2181173362Sbenjsc	struct wpi_tx_desc *desc;
2182173362Sbenjsc	struct wpi_tx_cmd *cmd;
2183173362Sbenjsc
2184173362Sbenjsc#ifdef WPI_DEBUG
2185173362Sbenjsc	if (!async) {
2186173362Sbenjsc		WPI_LOCK_ASSERT(sc);
2187173362Sbenjsc	}
2188173362Sbenjsc#endif
2189173362Sbenjsc
2190173362Sbenjsc	DPRINTFN(WPI_DEBUG_CMD,("wpi_cmd %d size %d async %d\n", code, size,
2191173362Sbenjsc		    async));
2192173362Sbenjsc
2193173362Sbenjsc	if (sc->flags & WPI_FLAG_BUSY) {
2194173362Sbenjsc		device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n",
2195173362Sbenjsc		    __func__, code);
2196173362Sbenjsc		return EAGAIN;
2197173362Sbenjsc	}
2198173362Sbenjsc	sc->flags|= WPI_FLAG_BUSY;
2199173362Sbenjsc
2200173362Sbenjsc	KASSERT(size <= sizeof cmd->data, ("command %d too large: %d bytes",
2201173362Sbenjsc	    code, size));
2202173362Sbenjsc
2203173362Sbenjsc	desc = &ring->desc[ring->cur];
2204173362Sbenjsc	cmd = &ring->cmd[ring->cur];
2205173362Sbenjsc
2206173362Sbenjsc	cmd->code = code;
2207173362Sbenjsc	cmd->flags = 0;
2208173362Sbenjsc	cmd->qid = ring->qid;
2209173362Sbenjsc	cmd->idx = ring->cur;
2210173362Sbenjsc	memcpy(cmd->data, buf, size);
2211173362Sbenjsc
2212173362Sbenjsc	desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
2213173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2214173362Sbenjsc		ring->cur * sizeof (struct wpi_tx_cmd));
2215173362Sbenjsc	desc->segs[0].len  = htole32(4 + size);
2216173362Sbenjsc
2217173362Sbenjsc	/* kick cmd ring */
2218173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2219173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2220173362Sbenjsc
2221173362Sbenjsc	if (async) {
2222173362Sbenjsc		sc->flags &= ~ WPI_FLAG_BUSY;
2223173362Sbenjsc		return 0;
2224173362Sbenjsc	}
2225173362Sbenjsc
2226173362Sbenjsc	return msleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz);
2227173362Sbenjsc}
2228173362Sbenjsc
2229173362Sbenjscstatic int
2230173362Sbenjscwpi_wme_update(struct ieee80211com *ic)
2231173362Sbenjsc{
2232173362Sbenjsc#define WPI_EXP2(v)	htole16((1 << (v)) - 1)
2233173362Sbenjsc#define WPI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
2234173362Sbenjsc	struct wpi_softc *sc = ic->ic_ifp->if_softc;
2235173362Sbenjsc	const struct wmeParams *wmep;
2236173362Sbenjsc	struct wpi_wme_setup wme;
2237173362Sbenjsc	int ac;
2238173362Sbenjsc
2239173362Sbenjsc	/* don't override default WME values if WME is not actually enabled */
2240173362Sbenjsc	if (!(ic->ic_flags & IEEE80211_F_WME))
2241173362Sbenjsc		return 0;
2242173362Sbenjsc
2243173362Sbenjsc	wme.flags = 0;
2244173362Sbenjsc	for (ac = 0; ac < WME_NUM_AC; ac++) {
2245173362Sbenjsc		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
2246173362Sbenjsc		wme.ac[ac].aifsn = wmep->wmep_aifsn;
2247173362Sbenjsc		wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin);
2248173362Sbenjsc		wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax);
2249173362Sbenjsc		wme.ac[ac].txop  = WPI_USEC(wmep->wmep_txopLimit);
2250173362Sbenjsc
2251173362Sbenjsc		DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
2252173362Sbenjsc		    "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
2253173362Sbenjsc		    wme.ac[ac].cwmax, wme.ac[ac].txop));
2254173362Sbenjsc	}
2255173362Sbenjsc	return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1);
2256173362Sbenjsc#undef WPI_USEC
2257173362Sbenjsc#undef WPI_EXP2
2258173362Sbenjsc}
2259173362Sbenjsc
2260173362Sbenjsc/*
2261173362Sbenjsc * Configure h/w multi-rate retries.
2262173362Sbenjsc */
2263173362Sbenjscstatic int
2264173362Sbenjscwpi_mrr_setup(struct wpi_softc *sc)
2265173362Sbenjsc{
2266178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2267178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2268173362Sbenjsc	struct wpi_mrr_setup mrr;
2269173362Sbenjsc	int i, error;
2270173362Sbenjsc
2271173362Sbenjsc	memset(&mrr, 0, sizeof (struct wpi_mrr_setup));
2272173362Sbenjsc
2273173362Sbenjsc	/* CCK rates (not used with 802.11a) */
2274173362Sbenjsc	for (i = WPI_CCK1; i <= WPI_CCK11; i++) {
2275173362Sbenjsc		mrr.rates[i].flags = 0;
2276173362Sbenjsc		mrr.rates[i].signal = wpi_ridx_to_plcp[i];
2277173362Sbenjsc		/* fallback to the immediate lower CCK rate (if any) */
2278173362Sbenjsc		mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1;
2279173362Sbenjsc		/* try one time at this rate before falling back to "next" */
2280173362Sbenjsc		mrr.rates[i].ntries = 1;
2281173362Sbenjsc	}
2282173362Sbenjsc
2283173362Sbenjsc	/* OFDM rates (not used with 802.11b) */
2284173362Sbenjsc	for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) {
2285173362Sbenjsc		mrr.rates[i].flags = 0;
2286173362Sbenjsc		mrr.rates[i].signal = wpi_ridx_to_plcp[i];
2287173362Sbenjsc		/* fallback to the immediate lower OFDM rate (if any) */
2288173362Sbenjsc		/* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */
2289173362Sbenjsc		mrr.rates[i].next = (i == WPI_OFDM6) ?
2290173362Sbenjsc		    ((ic->ic_curmode == IEEE80211_MODE_11A) ?
2291173362Sbenjsc			WPI_OFDM6 : WPI_CCK2) :
2292173362Sbenjsc		    i - 1;
2293173362Sbenjsc		/* try one time at this rate before falling back to "next" */
2294173362Sbenjsc		mrr.rates[i].ntries = 1;
2295173362Sbenjsc	}
2296173362Sbenjsc
2297173362Sbenjsc	/* setup MRR for control frames */
2298221299Sbschmidt	mrr.which = WPI_MRR_CTL;
2299173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2300173362Sbenjsc	if (error != 0) {
2301173362Sbenjsc		device_printf(sc->sc_dev,
2302173362Sbenjsc		    "could not setup MRR for control frames\n");
2303173362Sbenjsc		return error;
2304173362Sbenjsc	}
2305173362Sbenjsc
2306173362Sbenjsc	/* setup MRR for data frames */
2307221299Sbschmidt	mrr.which = WPI_MRR_DATA;
2308173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2309173362Sbenjsc	if (error != 0) {
2310173362Sbenjsc		device_printf(sc->sc_dev,
2311173362Sbenjsc		    "could not setup MRR for data frames\n");
2312173362Sbenjsc		return error;
2313173362Sbenjsc	}
2314173362Sbenjsc
2315173362Sbenjsc	return 0;
2316173362Sbenjsc}
2317173362Sbenjsc
2318173362Sbenjscstatic void
2319173362Sbenjscwpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
2320173362Sbenjsc{
2321173362Sbenjsc	struct wpi_cmd_led led;
2322173362Sbenjsc
2323173362Sbenjsc	led.which = which;
2324173362Sbenjsc	led.unit = htole32(100000);	/* on/off in unit of 100ms */
2325173362Sbenjsc	led.off = off;
2326173362Sbenjsc	led.on = on;
2327173362Sbenjsc
2328173362Sbenjsc	(void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
2329173362Sbenjsc}
2330173362Sbenjsc
2331173362Sbenjscstatic void
2332173362Sbenjscwpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni)
2333173362Sbenjsc{
2334173362Sbenjsc	struct wpi_cmd_tsf tsf;
2335173362Sbenjsc	uint64_t val, mod;
2336173362Sbenjsc
2337173362Sbenjsc	memset(&tsf, 0, sizeof tsf);
2338173362Sbenjsc	memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8);
2339173362Sbenjsc	tsf.bintval = htole16(ni->ni_intval);
2340173362Sbenjsc	tsf.lintval = htole16(10);
2341173362Sbenjsc
2342173362Sbenjsc	/* compute remaining time until next beacon */
2343173362Sbenjsc	val = (uint64_t)ni->ni_intval  * 1024;	/* msec -> usec */
2344173362Sbenjsc	mod = le64toh(tsf.tstamp) % val;
2345173362Sbenjsc	tsf.binitval = htole32((uint32_t)(val - mod));
2346173362Sbenjsc
2347173362Sbenjsc	if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
2348173362Sbenjsc		device_printf(sc->sc_dev, "could not enable TSF\n");
2349173362Sbenjsc}
2350173362Sbenjsc
2351173362Sbenjsc#if 0
2352173362Sbenjsc/*
2353173362Sbenjsc * Build a beacon frame that the firmware will broadcast periodically in
2354173362Sbenjsc * IBSS or HostAP modes.
2355173362Sbenjsc */
2356173362Sbenjscstatic int
2357173362Sbenjscwpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
2358173362Sbenjsc{
2359178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2360178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2361173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2362173362Sbenjsc	struct wpi_tx_desc *desc;
2363173362Sbenjsc	struct wpi_tx_data *data;
2364173362Sbenjsc	struct wpi_tx_cmd *cmd;
2365173362Sbenjsc	struct wpi_cmd_beacon *bcn;
2366173362Sbenjsc	struct ieee80211_beacon_offsets bo;
2367173362Sbenjsc	struct mbuf *m0;
2368173362Sbenjsc	bus_addr_t physaddr;
2369173362Sbenjsc	int error;
2370173362Sbenjsc
2371173362Sbenjsc	desc = &ring->desc[ring->cur];
2372173362Sbenjsc	data = &ring->data[ring->cur];
2373173362Sbenjsc
2374173362Sbenjsc	m0 = ieee80211_beacon_alloc(ic, ni, &bo);
2375173362Sbenjsc	if (m0 == NULL) {
2376173362Sbenjsc		device_printf(sc->sc_dev, "could not allocate beacon frame\n");
2377173362Sbenjsc		return ENOMEM;
2378173362Sbenjsc	}
2379173362Sbenjsc
2380173362Sbenjsc	cmd = &ring->cmd[ring->cur];
2381173362Sbenjsc	cmd->code = WPI_CMD_SET_BEACON;
2382173362Sbenjsc	cmd->flags = 0;
2383173362Sbenjsc	cmd->qid = ring->qid;
2384173362Sbenjsc	cmd->idx = ring->cur;
2385173362Sbenjsc
2386173362Sbenjsc	bcn = (struct wpi_cmd_beacon *)cmd->data;
2387173362Sbenjsc	memset(bcn, 0, sizeof (struct wpi_cmd_beacon));
2388173362Sbenjsc	bcn->id = WPI_ID_BROADCAST;
2389173362Sbenjsc	bcn->ofdm_mask = 0xff;
2390173362Sbenjsc	bcn->cck_mask = 0x0f;
2391173362Sbenjsc	bcn->lifetime = htole32(WPI_LIFETIME_INFINITE);
2392173362Sbenjsc	bcn->len = htole16(m0->m_pkthdr.len);
2393173362Sbenjsc	bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2394173362Sbenjsc		wpi_plcp_signal(12) : wpi_plcp_signal(2);
2395173362Sbenjsc	bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
2396173362Sbenjsc
2397173362Sbenjsc	/* save and trim IEEE802.11 header */
2398173362Sbenjsc	m_copydata(m0, 0, sizeof (struct ieee80211_frame), (caddr_t)&bcn->wh);
2399173362Sbenjsc	m_adj(m0, sizeof (struct ieee80211_frame));
2400173362Sbenjsc
2401173362Sbenjsc	/* assume beacon frame is contiguous */
2402173362Sbenjsc	error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m0, void *),
2403173362Sbenjsc	    m0->m_pkthdr.len, wpi_dma_map_addr, &physaddr, 0);
2404173362Sbenjsc	if (error != 0) {
2405173362Sbenjsc		device_printf(sc->sc_dev, "could not map beacon\n");
2406173362Sbenjsc		m_freem(m0);
2407173362Sbenjsc		return error;
2408173362Sbenjsc	}
2409173362Sbenjsc
2410173362Sbenjsc	data->m = m0;
2411173362Sbenjsc
2412173362Sbenjsc	/* first scatter/gather segment is used by the beacon command */
2413173362Sbenjsc	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24);
2414173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2415173362Sbenjsc		ring->cur * sizeof (struct wpi_tx_cmd));
2416173362Sbenjsc	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_beacon));
2417173362Sbenjsc	desc->segs[1].addr = htole32(physaddr);
2418173362Sbenjsc	desc->segs[1].len  = htole32(m0->m_pkthdr.len);
2419173362Sbenjsc
2420173362Sbenjsc	/* kick cmd ring */
2421173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2422173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2423173362Sbenjsc
2424173362Sbenjsc	return 0;
2425173362Sbenjsc}
2426173362Sbenjsc#endif
2427173362Sbenjsc
2428173362Sbenjscstatic int
2429178354Ssamwpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap)
2430173362Sbenjsc{
2431178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2432178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2433173362Sbenjsc	struct wpi_node_info node;
2434173362Sbenjsc	int error;
2435173362Sbenjsc
2436177043Sthompsa
2437173362Sbenjsc	/* update adapter's configuration */
2438177043Sthompsa	sc->config.associd = 0;
2439177043Sthompsa	sc->config.filter &= ~htole32(WPI_FILTER_BSS);
2440173362Sbenjsc	IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
2441173362Sbenjsc	sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2442173362Sbenjsc	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2443173362Sbenjsc		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2444173362Sbenjsc		    WPI_CONFIG_24GHZ);
2445216522Sbschmidt	} else {
2446216522Sbschmidt		sc->config.flags &= ~htole32(WPI_CONFIG_AUTO |
2447216522Sbschmidt		    WPI_CONFIG_24GHZ);
2448173362Sbenjsc	}
2449178354Ssam	if (IEEE80211_IS_CHAN_A(ni->ni_chan)) {
2450173362Sbenjsc		sc->config.cck_mask  = 0;
2451173362Sbenjsc		sc->config.ofdm_mask = 0x15;
2452178354Ssam	} else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) {
2453173362Sbenjsc		sc->config.cck_mask  = 0x03;
2454173362Sbenjsc		sc->config.ofdm_mask = 0;
2455178354Ssam	} else {
2456178354Ssam		/* XXX assume 802.11b/g */
2457173362Sbenjsc		sc->config.cck_mask  = 0x0f;
2458173362Sbenjsc		sc->config.ofdm_mask = 0x15;
2459173362Sbenjsc	}
2460173362Sbenjsc
2461173362Sbenjsc	DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
2462173362Sbenjsc		sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
2463173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2464173362Sbenjsc		sizeof (struct wpi_config), 1);
2465173362Sbenjsc	if (error != 0) {
2466173362Sbenjsc		device_printf(sc->sc_dev, "could not configure\n");
2467173362Sbenjsc		return error;
2468173362Sbenjsc	}
2469173362Sbenjsc
2470173362Sbenjsc	/* configuration has changed, set Tx power accordingly */
2471173362Sbenjsc	if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
2472173362Sbenjsc		device_printf(sc->sc_dev, "could not set Tx power\n");
2473173362Sbenjsc		return error;
2474173362Sbenjsc	}
2475173362Sbenjsc
2476173362Sbenjsc	/* add default node */
2477173362Sbenjsc	memset(&node, 0, sizeof node);
2478173362Sbenjsc	IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid);
2479173362Sbenjsc	node.id = WPI_ID_BSS;
2480173362Sbenjsc	node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2481173362Sbenjsc	    wpi_plcp_signal(12) : wpi_plcp_signal(2);
2482173362Sbenjsc	node.action = htole32(WPI_ACTION_SET_RATE);
2483173362Sbenjsc	node.antenna = WPI_ANTENNA_BOTH;
2484173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2485177043Sthompsa	if (error != 0)
2486177043Sthompsa		device_printf(sc->sc_dev, "could not add BSS node\n");
2487177043Sthompsa
2488177043Sthompsa	return (error);
2489177043Sthompsa}
2490177043Sthompsa
2491177043Sthompsastatic int
2492178354Ssamwpi_run(struct wpi_softc *sc, struct ieee80211vap *vap)
2493177043Sthompsa{
2494178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2495178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2496177043Sthompsa	int error;
2497177043Sthompsa
2498178354Ssam	if (vap->iv_opmode == IEEE80211_M_MONITOR) {
2499178354Ssam		/* link LED blinks while monitoring */
2500178354Ssam		wpi_set_led(sc, WPI_LED_LINK, 5, 5);
2501178354Ssam		return 0;
2502178354Ssam	}
2503178354Ssam
2504177043Sthompsa	wpi_enable_tsf(sc, ni);
2505177043Sthompsa
2506177043Sthompsa	/* update adapter's configuration */
2507177043Sthompsa	sc->config.associd = htole16(ni->ni_associd & ~0xc000);
2508177043Sthompsa	/* short preamble/slot time are negotiated when associating */
2509177043Sthompsa	sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE |
2510177043Sthompsa	    WPI_CONFIG_SHSLOT);
2511177043Sthompsa	if (ic->ic_flags & IEEE80211_F_SHSLOT)
2512177043Sthompsa		sc->config.flags |= htole32(WPI_CONFIG_SHSLOT);
2513177043Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2514177043Sthompsa		sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE);
2515177043Sthompsa	sc->config.filter |= htole32(WPI_FILTER_BSS);
2516177043Sthompsa
2517177043Sthompsa	/* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */
2518177043Sthompsa
2519177043Sthompsa	DPRINTF(("config chan %d flags %x\n", sc->config.chan,
2520177043Sthompsa		    sc->config.flags));
2521177043Sthompsa	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, sizeof (struct
2522177043Sthompsa		    wpi_config), 1);
2523173362Sbenjsc	if (error != 0) {
2524177043Sthompsa		device_printf(sc->sc_dev, "could not update configuration\n");
2525173362Sbenjsc		return error;
2526173362Sbenjsc	}
2527173362Sbenjsc
2528178354Ssam	error = wpi_set_txpower(sc, ni->ni_chan, 1);
2529177043Sthompsa	if (error != 0) {
2530177043Sthompsa		device_printf(sc->sc_dev, "could set txpower\n");
2531177043Sthompsa		return error;
2532177043Sthompsa	}
2533173362Sbenjsc
2534177043Sthompsa	/* link LED always on while associated */
2535177043Sthompsa	wpi_set_led(sc, WPI_LED_LINK, 0, 1);
2536177043Sthompsa
2537177043Sthompsa	/* start automatic rate control timer */
2538178354Ssam	callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
2539177043Sthompsa
2540177043Sthompsa	return (error);
2541173362Sbenjsc}
2542173362Sbenjsc
2543173362Sbenjsc/*
2544173362Sbenjsc * Send a scan request to the firmware.  Since this command is huge, we map it
2545173362Sbenjsc * into a mbufcluster instead of using the pre-allocated set of commands. Note,
2546173362Sbenjsc * much of this code is similar to that in wpi_cmd but because we must manually
2547173362Sbenjsc * construct the probe & channels, we duplicate what's needed here. XXX In the
2548173362Sbenjsc * future, this function should be modified to use wpi_cmd to help cleanup the
2549173362Sbenjsc * code base.
2550173362Sbenjsc */
2551173362Sbenjscstatic int
2552173362Sbenjscwpi_scan(struct wpi_softc *sc)
2553173362Sbenjsc{
2554178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2555178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2556177043Sthompsa	struct ieee80211_scan_state *ss = ic->ic_scan;
2557173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2558173362Sbenjsc	struct wpi_tx_desc *desc;
2559173362Sbenjsc	struct wpi_tx_data *data;
2560173362Sbenjsc	struct wpi_tx_cmd *cmd;
2561173362Sbenjsc	struct wpi_scan_hdr *hdr;
2562173362Sbenjsc	struct wpi_scan_chan *chan;
2563173362Sbenjsc	struct ieee80211_frame *wh;
2564173362Sbenjsc	struct ieee80211_rateset *rs;
2565173362Sbenjsc	struct ieee80211_channel *c;
2566173362Sbenjsc	enum ieee80211_phymode mode;
2567173362Sbenjsc	uint8_t *frm;
2568177043Sthompsa	int nrates, pktlen, error, i, nssid;
2569173362Sbenjsc	bus_addr_t physaddr;
2570173362Sbenjsc
2571173362Sbenjsc	desc = &ring->desc[ring->cur];
2572173362Sbenjsc	data = &ring->data[ring->cur];
2573173362Sbenjsc
2574243857Sglebius	data->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2575173362Sbenjsc	if (data->m == NULL) {
2576173362Sbenjsc		device_printf(sc->sc_dev,
2577173362Sbenjsc		    "could not allocate mbuf for scan command\n");
2578173362Sbenjsc		return ENOMEM;
2579173362Sbenjsc	}
2580173362Sbenjsc
2581173362Sbenjsc	cmd = mtod(data->m, struct wpi_tx_cmd *);
2582173362Sbenjsc	cmd->code = WPI_CMD_SCAN;
2583173362Sbenjsc	cmd->flags = 0;
2584173362Sbenjsc	cmd->qid = ring->qid;
2585173362Sbenjsc	cmd->idx = ring->cur;
2586173362Sbenjsc
2587173362Sbenjsc	hdr = (struct wpi_scan_hdr *)cmd->data;
2588173362Sbenjsc	memset(hdr, 0, sizeof(struct wpi_scan_hdr));
2589173362Sbenjsc
2590173362Sbenjsc	/*
2591173362Sbenjsc	 * Move to the next channel if no packets are received within 5 msecs
2592173362Sbenjsc	 * after sending the probe request (this helps to reduce the duration
2593173362Sbenjsc	 * of active scans).
2594173362Sbenjsc	 */
2595173362Sbenjsc	hdr->quiet = htole16(5);
2596173362Sbenjsc	hdr->threshold = htole16(1);
2597173362Sbenjsc
2598173362Sbenjsc	if (IEEE80211_IS_CHAN_A(ic->ic_curchan)) {
2599173362Sbenjsc		/* send probe requests at 6Mbps */
2600173362Sbenjsc		hdr->tx.rate = wpi_ridx_to_plcp[WPI_OFDM6];
2601173362Sbenjsc
2602173362Sbenjsc		/* Enable crc checking */
2603173362Sbenjsc		hdr->promotion = htole16(1);
2604173362Sbenjsc	} else {
2605173362Sbenjsc		hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO);
2606173362Sbenjsc		/* send probe requests at 1Mbps */
2607173362Sbenjsc		hdr->tx.rate = wpi_ridx_to_plcp[WPI_CCK1];
2608173362Sbenjsc	}
2609173362Sbenjsc	hdr->tx.id = WPI_ID_BROADCAST;
2610173362Sbenjsc	hdr->tx.lifetime = htole32(WPI_LIFETIME_INFINITE);
2611173362Sbenjsc	hdr->tx.flags = htole32(WPI_TX_AUTO_SEQ);
2612173362Sbenjsc
2613178354Ssam	memset(hdr->scan_essids, 0, sizeof(hdr->scan_essids));
2614177043Sthompsa	nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS);
2615178354Ssam	for (i = 0; i < nssid; i++) {
2616177043Sthompsa		hdr->scan_essids[i].id = IEEE80211_ELEMID_SSID;
2617177043Sthompsa		hdr->scan_essids[i].esslen = MIN(ss->ss_ssid[i].len, 32);
2618177043Sthompsa		memcpy(hdr->scan_essids[i].essid, ss->ss_ssid[i].ssid,
2619177043Sthompsa		    hdr->scan_essids[i].esslen);
2620179957Sthompsa#ifdef WPI_DEBUG
2621177043Sthompsa		if (wpi_debug & WPI_DEBUG_SCANNING) {
2622177043Sthompsa			printf("Scanning Essid: ");
2623178354Ssam			ieee80211_print_essid(hdr->scan_essids[i].essid,
2624178354Ssam			    hdr->scan_essids[i].esslen);
2625177043Sthompsa			printf("\n");
2626177043Sthompsa		}
2627179957Sthompsa#endif
2628173362Sbenjsc	}
2629173362Sbenjsc
2630173362Sbenjsc	/*
2631173362Sbenjsc	 * Build a probe request frame.  Most of the following code is a
2632173362Sbenjsc	 * copy & paste of what is done in net80211.
2633173362Sbenjsc	 */
2634173362Sbenjsc	wh = (struct ieee80211_frame *)&hdr->scan_essids[4];
2635173362Sbenjsc	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2636173362Sbenjsc		IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2637173362Sbenjsc	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2638173362Sbenjsc	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2639190526Ssam	IEEE80211_ADDR_COPY(wh->i_addr2, IF_LLADDR(ifp));
2640173362Sbenjsc	IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
2641173362Sbenjsc	*(u_int16_t *)&wh->i_dur[0] = 0;	/* filled by h/w */
2642173362Sbenjsc	*(u_int16_t *)&wh->i_seq[0] = 0;	/* filled by h/w */
2643173362Sbenjsc
2644173362Sbenjsc	frm = (uint8_t *)(wh + 1);
2645173362Sbenjsc
2646173362Sbenjsc	/* add essid IE, the hardware will fill this in for us */
2647173362Sbenjsc	*frm++ = IEEE80211_ELEMID_SSID;
2648173362Sbenjsc	*frm++ = 0;
2649173362Sbenjsc
2650173362Sbenjsc	mode = ieee80211_chan2mode(ic->ic_curchan);
2651173362Sbenjsc	rs = &ic->ic_sup_rates[mode];
2652173362Sbenjsc
2653173362Sbenjsc	/* add supported rates IE */
2654173362Sbenjsc	*frm++ = IEEE80211_ELEMID_RATES;
2655173362Sbenjsc	nrates = rs->rs_nrates;
2656173362Sbenjsc	if (nrates > IEEE80211_RATE_SIZE)
2657173362Sbenjsc		nrates = IEEE80211_RATE_SIZE;
2658173362Sbenjsc	*frm++ = nrates;
2659173362Sbenjsc	memcpy(frm, rs->rs_rates, nrates);
2660173362Sbenjsc	frm += nrates;
2661173362Sbenjsc
2662173362Sbenjsc	/* add supported xrates IE */
2663173362Sbenjsc	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2664173362Sbenjsc		nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2665173362Sbenjsc		*frm++ = IEEE80211_ELEMID_XRATES;
2666173362Sbenjsc		*frm++ = nrates;
2667173362Sbenjsc		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2668173362Sbenjsc		frm += nrates;
2669173362Sbenjsc	}
2670173362Sbenjsc
2671173362Sbenjsc	/* setup length of probe request */
2672173362Sbenjsc	hdr->tx.len = htole16(frm - (uint8_t *)wh);
2673173362Sbenjsc
2674173362Sbenjsc	/*
2675173362Sbenjsc	 * Construct information about the channel that we
2676173362Sbenjsc	 * want to scan. The firmware expects this to be directly
2677173362Sbenjsc	 * after the scan probe request
2678173362Sbenjsc	 */
2679173362Sbenjsc	c = ic->ic_curchan;
2680173362Sbenjsc	chan = (struct wpi_scan_chan *)frm;
2681173362Sbenjsc	chan->chan = ieee80211_chan2ieee(ic, c);
2682173362Sbenjsc	chan->flags = 0;
2683173362Sbenjsc	if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
2684173362Sbenjsc		chan->flags |= WPI_CHAN_ACTIVE;
2685178354Ssam		if (nssid != 0)
2686173362Sbenjsc			chan->flags |= WPI_CHAN_DIRECT;
2687173362Sbenjsc	}
2688173362Sbenjsc	chan->gain_dsp = 0x6e; /* Default level */
2689173362Sbenjsc	if (IEEE80211_IS_CHAN_5GHZ(c)) {
2690173362Sbenjsc		chan->active = htole16(10);
2691178354Ssam		chan->passive = htole16(ss->ss_maxdwell);
2692173362Sbenjsc		chan->gain_radio = 0x3b;
2693173362Sbenjsc	} else {
2694173362Sbenjsc		chan->active = htole16(20);
2695178354Ssam		chan->passive = htole16(ss->ss_maxdwell);
2696173362Sbenjsc		chan->gain_radio = 0x28;
2697173362Sbenjsc	}
2698173362Sbenjsc
2699173362Sbenjsc	DPRINTFN(WPI_DEBUG_SCANNING,
2700173362Sbenjsc	    ("Scanning %u Passive: %d\n",
2701173362Sbenjsc	     chan->chan,
2702173362Sbenjsc	     c->ic_flags & IEEE80211_CHAN_PASSIVE));
2703173362Sbenjsc
2704173362Sbenjsc	hdr->nchan++;
2705173362Sbenjsc	chan++;
2706173362Sbenjsc
2707173362Sbenjsc	frm += sizeof (struct wpi_scan_chan);
2708173362Sbenjsc#if 0
2709173362Sbenjsc	// XXX All Channels....
2710173362Sbenjsc	for (c  = &ic->ic_channels[1];
2711173362Sbenjsc	     c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
2712173362Sbenjsc		if ((c->ic_flags & ic->ic_curchan->ic_flags) != ic->ic_curchan->ic_flags)
2713173362Sbenjsc			continue;
2714173362Sbenjsc
2715173362Sbenjsc		chan->chan = ieee80211_chan2ieee(ic, c);
2716173362Sbenjsc		chan->flags = 0;
2717173362Sbenjsc		if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
2718173362Sbenjsc		    chan->flags |= WPI_CHAN_ACTIVE;
2719173362Sbenjsc		    if (ic->ic_des_ssid[0].len != 0)
2720173362Sbenjsc			chan->flags |= WPI_CHAN_DIRECT;
2721173362Sbenjsc		}
2722173362Sbenjsc		chan->gain_dsp = 0x6e; /* Default level */
2723173362Sbenjsc		if (IEEE80211_IS_CHAN_5GHZ(c)) {
2724173362Sbenjsc			chan->active = htole16(10);
2725173362Sbenjsc			chan->passive = htole16(110);
2726173362Sbenjsc			chan->gain_radio = 0x3b;
2727173362Sbenjsc		} else {
2728173362Sbenjsc			chan->active = htole16(20);
2729173362Sbenjsc			chan->passive = htole16(120);
2730173362Sbenjsc			chan->gain_radio = 0x28;
2731173362Sbenjsc		}
2732173362Sbenjsc
2733173362Sbenjsc		DPRINTFN(WPI_DEBUG_SCANNING,
2734173362Sbenjsc			 ("Scanning %u Passive: %d\n",
2735173362Sbenjsc			  chan->chan,
2736173362Sbenjsc			  c->ic_flags & IEEE80211_CHAN_PASSIVE));
2737173362Sbenjsc
2738173362Sbenjsc		hdr->nchan++;
2739173362Sbenjsc		chan++;
2740173362Sbenjsc
2741173362Sbenjsc		frm += sizeof (struct wpi_scan_chan);
2742173362Sbenjsc	}
2743173362Sbenjsc#endif
2744173362Sbenjsc
2745173362Sbenjsc	hdr->len = htole16(frm - (uint8_t *)hdr);
2746173362Sbenjsc	pktlen = frm - (uint8_t *)cmd;
2747173362Sbenjsc
2748173362Sbenjsc	error = bus_dmamap_load(ring->data_dmat, data->map, cmd, pktlen,
2749173362Sbenjsc	    wpi_dma_map_addr, &physaddr, BUS_DMA_NOWAIT);
2750173362Sbenjsc	if (error != 0) {
2751173362Sbenjsc		device_printf(sc->sc_dev, "could not map scan command\n");
2752173362Sbenjsc		m_freem(data->m);
2753173362Sbenjsc		data->m = NULL;
2754173362Sbenjsc		return error;
2755173362Sbenjsc	}
2756173362Sbenjsc
2757173362Sbenjsc	desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24);
2758173362Sbenjsc	desc->segs[0].addr = htole32(physaddr);
2759173362Sbenjsc	desc->segs[0].len  = htole32(pktlen);
2760173362Sbenjsc
2761173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2762173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
2763173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2764173362Sbenjsc
2765173362Sbenjsc	/* kick cmd ring */
2766173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2767173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2768173362Sbenjsc
2769177043Sthompsa	sc->sc_scan_timer = 5;
2770173362Sbenjsc	return 0;	/* will be notified async. of failure/success */
2771173362Sbenjsc}
2772173362Sbenjsc
2773173362Sbenjsc/**
2774173362Sbenjsc * Configure the card to listen to a particular channel, this transisions the
2775173362Sbenjsc * card in to being able to receive frames from remote devices.
2776173362Sbenjsc */
2777173362Sbenjscstatic int
2778173362Sbenjscwpi_config(struct wpi_softc *sc)
2779173362Sbenjsc{
2780178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2781178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2782173362Sbenjsc	struct wpi_power power;
2783173362Sbenjsc	struct wpi_bluetooth bluetooth;
2784173362Sbenjsc	struct wpi_node_info node;
2785173362Sbenjsc	int error;
2786173362Sbenjsc
2787173362Sbenjsc	/* set power mode */
2788173362Sbenjsc	memset(&power, 0, sizeof power);
2789173362Sbenjsc	power.flags = htole32(WPI_POWER_CAM|0x8);
2790173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0);
2791173362Sbenjsc	if (error != 0) {
2792173362Sbenjsc		device_printf(sc->sc_dev, "could not set power mode\n");
2793173362Sbenjsc		return error;
2794173362Sbenjsc	}
2795173362Sbenjsc
2796173362Sbenjsc	/* configure bluetooth coexistence */
2797173362Sbenjsc	memset(&bluetooth, 0, sizeof bluetooth);
2798173362Sbenjsc	bluetooth.flags = 3;
2799173362Sbenjsc	bluetooth.lead = 0xaa;
2800173362Sbenjsc	bluetooth.kill = 1;
2801173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
2802173362Sbenjsc	    0);
2803173362Sbenjsc	if (error != 0) {
2804173362Sbenjsc		device_printf(sc->sc_dev,
2805173362Sbenjsc		    "could not configure bluetooth coexistence\n");
2806173362Sbenjsc		return error;
2807173362Sbenjsc	}
2808173362Sbenjsc
2809173362Sbenjsc	/* configure adapter */
2810173362Sbenjsc	memset(&sc->config, 0, sizeof (struct wpi_config));
2811190526Ssam	IEEE80211_ADDR_COPY(sc->config.myaddr, IF_LLADDR(ifp));
2812173362Sbenjsc	/*set default channel*/
2813173362Sbenjsc	sc->config.chan = htole16(ieee80211_chan2ieee(ic, ic->ic_curchan));
2814173362Sbenjsc	sc->config.flags = htole32(WPI_CONFIG_TSF);
2815173362Sbenjsc	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2816173362Sbenjsc		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2817173362Sbenjsc		    WPI_CONFIG_24GHZ);
2818173362Sbenjsc	}
2819173362Sbenjsc	sc->config.filter = 0;
2820173362Sbenjsc	switch (ic->ic_opmode) {
2821173362Sbenjsc	case IEEE80211_M_STA:
2822173362Sbenjsc	case IEEE80211_M_WDS:	/* No know setup, use STA for now */
2823173362Sbenjsc		sc->config.mode = WPI_MODE_STA;
2824173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_MULTICAST);
2825173362Sbenjsc		break;
2826173362Sbenjsc	case IEEE80211_M_IBSS:
2827173362Sbenjsc	case IEEE80211_M_AHDEMO:
2828173362Sbenjsc		sc->config.mode = WPI_MODE_IBSS;
2829173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_BEACON |
2830173362Sbenjsc					     WPI_FILTER_MULTICAST);
2831173362Sbenjsc		break;
2832173362Sbenjsc	case IEEE80211_M_HOSTAP:
2833173362Sbenjsc		sc->config.mode = WPI_MODE_HOSTAP;
2834173362Sbenjsc		break;
2835173362Sbenjsc	case IEEE80211_M_MONITOR:
2836173362Sbenjsc		sc->config.mode = WPI_MODE_MONITOR;
2837173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_MULTICAST |
2838173362Sbenjsc			WPI_FILTER_CTL | WPI_FILTER_PROMISC);
2839173362Sbenjsc		break;
2840195562Srpaulo	default:
2841195562Srpaulo		device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2842195562Srpaulo		return EINVAL;
2843173362Sbenjsc	}
2844173362Sbenjsc	sc->config.cck_mask  = 0x0f;	/* not yet negotiated */
2845173362Sbenjsc	sc->config.ofdm_mask = 0xff;	/* not yet negotiated */
2846173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2847173362Sbenjsc		sizeof (struct wpi_config), 0);
2848173362Sbenjsc	if (error != 0) {
2849173362Sbenjsc		device_printf(sc->sc_dev, "configure command failed\n");
2850173362Sbenjsc		return error;
2851173362Sbenjsc	}
2852173362Sbenjsc
2853173362Sbenjsc	/* configuration has changed, set Tx power accordingly */
2854177043Sthompsa	if ((error = wpi_set_txpower(sc, ic->ic_curchan, 0)) != 0) {
2855173362Sbenjsc	    device_printf(sc->sc_dev, "could not set Tx power\n");
2856173362Sbenjsc	    return error;
2857173362Sbenjsc	}
2858173362Sbenjsc
2859173362Sbenjsc	/* add broadcast node */
2860173362Sbenjsc	memset(&node, 0, sizeof node);
2861173362Sbenjsc	IEEE80211_ADDR_COPY(node.bssid, ifp->if_broadcastaddr);
2862173362Sbenjsc	node.id = WPI_ID_BROADCAST;
2863173362Sbenjsc	node.rate = wpi_plcp_signal(2);
2864173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
2865173362Sbenjsc	if (error != 0) {
2866173362Sbenjsc		device_printf(sc->sc_dev, "could not add broadcast node\n");
2867173362Sbenjsc		return error;
2868173362Sbenjsc	}
2869173362Sbenjsc
2870173362Sbenjsc	/* Setup rate scalling */
2871173362Sbenjsc	error = wpi_mrr_setup(sc);
2872173362Sbenjsc	if (error != 0) {
2873173362Sbenjsc		device_printf(sc->sc_dev, "could not setup MRR\n");
2874173362Sbenjsc		return error;
2875173362Sbenjsc	}
2876173362Sbenjsc
2877173362Sbenjsc	return 0;
2878173362Sbenjsc}
2879173362Sbenjsc
2880173362Sbenjscstatic void
2881173362Sbenjscwpi_stop_master(struct wpi_softc *sc)
2882173362Sbenjsc{
2883173362Sbenjsc	uint32_t tmp;
2884173362Sbenjsc	int ntries;
2885173362Sbenjsc
2886173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,("Disabling Firmware execution\n"));
2887173362Sbenjsc
2888173362Sbenjsc	tmp = WPI_READ(sc, WPI_RESET);
2889173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER | WPI_NEVO_RESET);
2890173362Sbenjsc
2891173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
2892173362Sbenjsc	if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP)
2893173362Sbenjsc		return;	/* already asleep */
2894173362Sbenjsc
2895173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
2896173362Sbenjsc		if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED)
2897173362Sbenjsc			break;
2898173362Sbenjsc		DELAY(10);
2899173362Sbenjsc	}
2900173362Sbenjsc	if (ntries == 100) {
2901173362Sbenjsc		device_printf(sc->sc_dev, "timeout waiting for master\n");
2902173362Sbenjsc	}
2903173362Sbenjsc}
2904173362Sbenjsc
2905173362Sbenjscstatic int
2906173362Sbenjscwpi_power_up(struct wpi_softc *sc)
2907173362Sbenjsc{
2908173362Sbenjsc	uint32_t tmp;
2909173362Sbenjsc	int ntries;
2910173362Sbenjsc
2911173362Sbenjsc	wpi_mem_lock(sc);
2912173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_POWER);
2913173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000);
2914173362Sbenjsc	wpi_mem_unlock(sc);
2915173362Sbenjsc
2916173362Sbenjsc	for (ntries = 0; ntries < 5000; ntries++) {
2917173362Sbenjsc		if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED)
2918173362Sbenjsc			break;
2919173362Sbenjsc		DELAY(10);
2920173362Sbenjsc	}
2921173362Sbenjsc	if (ntries == 5000) {
2922173362Sbenjsc		device_printf(sc->sc_dev,
2923173362Sbenjsc		    "timeout waiting for NIC to power up\n");
2924173362Sbenjsc		return ETIMEDOUT;
2925173362Sbenjsc	}
2926173362Sbenjsc	return 0;
2927173362Sbenjsc}
2928173362Sbenjsc
2929173362Sbenjscstatic int
2930173362Sbenjscwpi_reset(struct wpi_softc *sc)
2931173362Sbenjsc{
2932173362Sbenjsc	uint32_t tmp;
2933173362Sbenjsc	int ntries;
2934173362Sbenjsc
2935173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,
2936173362Sbenjsc	    ("Resetting the card - clearing any uploaded firmware\n"));
2937173362Sbenjsc
2938173362Sbenjsc	/* clear any pending interrupts */
2939173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
2940173362Sbenjsc
2941173362Sbenjsc	tmp = WPI_READ(sc, WPI_PLL_CTL);
2942173362Sbenjsc	WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT);
2943173362Sbenjsc
2944173362Sbenjsc	tmp = WPI_READ(sc, WPI_CHICKEN);
2945173362Sbenjsc	WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS);
2946173362Sbenjsc
2947173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
2948173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT);
2949173362Sbenjsc
2950173362Sbenjsc	/* wait for clock stabilization */
2951173362Sbenjsc	for (ntries = 0; ntries < 25000; ntries++) {
2952173362Sbenjsc		if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK)
2953173362Sbenjsc			break;
2954173362Sbenjsc		DELAY(10);
2955173362Sbenjsc	}
2956173362Sbenjsc	if (ntries == 25000) {
2957173362Sbenjsc		device_printf(sc->sc_dev,
2958173362Sbenjsc		    "timeout waiting for clock stabilization\n");
2959173362Sbenjsc		return ETIMEDOUT;
2960173362Sbenjsc	}
2961173362Sbenjsc
2962173362Sbenjsc	/* initialize EEPROM */
2963173362Sbenjsc	tmp = WPI_READ(sc, WPI_EEPROM_STATUS);
2964173362Sbenjsc
2965173362Sbenjsc	if ((tmp & WPI_EEPROM_VERSION) == 0) {
2966173362Sbenjsc		device_printf(sc->sc_dev, "EEPROM not found\n");
2967173362Sbenjsc		return EIO;
2968173362Sbenjsc	}
2969173362Sbenjsc	WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED);
2970173362Sbenjsc
2971173362Sbenjsc	return 0;
2972173362Sbenjsc}
2973173362Sbenjsc
2974173362Sbenjscstatic void
2975173362Sbenjscwpi_hw_config(struct wpi_softc *sc)
2976173362Sbenjsc{
2977173362Sbenjsc	uint32_t rev, hw;
2978173362Sbenjsc
2979173362Sbenjsc	/* voodoo from the Linux "driver".. */
2980173362Sbenjsc	hw = WPI_READ(sc, WPI_HWCONFIG);
2981173362Sbenjsc
2982173362Sbenjsc	rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1);
2983173362Sbenjsc	if ((rev & 0xc0) == 0x40)
2984173362Sbenjsc		hw |= WPI_HW_ALM_MB;
2985173362Sbenjsc	else if (!(rev & 0x80))
2986173362Sbenjsc		hw |= WPI_HW_ALM_MM;
2987173362Sbenjsc
2988173362Sbenjsc	if (sc->cap == 0x80)
2989173362Sbenjsc		hw |= WPI_HW_SKU_MRC;
2990173362Sbenjsc
2991173362Sbenjsc	hw &= ~WPI_HW_REV_D;
2992173362Sbenjsc	if ((le16toh(sc->rev) & 0xf0) == 0xd0)
2993173362Sbenjsc		hw |= WPI_HW_REV_D;
2994173362Sbenjsc
2995173362Sbenjsc	if (sc->type > 1)
2996173362Sbenjsc		hw |= WPI_HW_TYPE_B;
2997173362Sbenjsc
2998173362Sbenjsc	WPI_WRITE(sc, WPI_HWCONFIG, hw);
2999173362Sbenjsc}
3000173362Sbenjsc
3001173362Sbenjscstatic void
3002177043Sthompsawpi_rfkill_resume(struct wpi_softc *sc)
3003177043Sthompsa{
3004177043Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3005178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3006178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3007177043Sthompsa	int ntries;
3008177043Sthompsa
3009177043Sthompsa	/* enable firmware again */
3010177043Sthompsa	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3011177043Sthompsa	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
3012177043Sthompsa
3013177043Sthompsa	/* wait for thermal sensors to calibrate */
3014177043Sthompsa	for (ntries = 0; ntries < 1000; ntries++) {
3015177043Sthompsa		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
3016177043Sthompsa			break;
3017177043Sthompsa		DELAY(10);
3018177043Sthompsa	}
3019177043Sthompsa
3020177043Sthompsa	if (ntries == 1000) {
3021177043Sthompsa		device_printf(sc->sc_dev,
3022177043Sthompsa		    "timeout waiting for thermal calibration\n");
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		return;
3030177043Sthompsa	}
3031177043Sthompsa
3032177043Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3033177043Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3034177043Sthompsa	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3035177043Sthompsa
3036178354Ssam	if (vap != NULL) {
3037178354Ssam		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
3038178354Ssam			if (vap->iv_opmode != IEEE80211_M_MONITOR) {
3039191746Sthompsa				ieee80211_beacon_miss(ic);
3040178354Ssam				wpi_set_led(sc, WPI_LED_LINK, 0, 1);
3041178354Ssam			} else
3042178354Ssam				wpi_set_led(sc, WPI_LED_LINK, 5, 5);
3043178354Ssam		} else {
3044178354Ssam			ieee80211_scan_next(vap);
3045177043Sthompsa			wpi_set_led(sc, WPI_LED_LINK, 20, 2);
3046178354Ssam		}
3047177043Sthompsa	}
3048177043Sthompsa
3049177043Sthompsa	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3050177043Sthompsa}
3051177043Sthompsa
3052177043Sthompsastatic void
3053177043Sthompsawpi_init_locked(struct wpi_softc *sc, int force)
3054177043Sthompsa{
3055178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3056173362Sbenjsc	uint32_t tmp;
3057177043Sthompsa	int ntries, qid;
3058173362Sbenjsc
3059173362Sbenjsc	wpi_stop_locked(sc);
3060173362Sbenjsc	(void)wpi_reset(sc);
3061173362Sbenjsc
3062173362Sbenjsc	wpi_mem_lock(sc);
3063173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00);
3064173362Sbenjsc	DELAY(20);
3065173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
3066173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800);
3067173362Sbenjsc	wpi_mem_unlock(sc);
3068173362Sbenjsc
3069173362Sbenjsc	(void)wpi_power_up(sc);
3070173362Sbenjsc	wpi_hw_config(sc);
3071173362Sbenjsc
3072173362Sbenjsc	/* init Rx ring */
3073173362Sbenjsc	wpi_mem_lock(sc);
3074173362Sbenjsc	WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr);
3075173362Sbenjsc	WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr +
3076173362Sbenjsc	    offsetof(struct wpi_shared, next));
3077173362Sbenjsc	WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
3078173362Sbenjsc	WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010);
3079173362Sbenjsc	wpi_mem_unlock(sc);
3080173362Sbenjsc
3081173362Sbenjsc	/* init Tx rings */
3082173362Sbenjsc	wpi_mem_lock(sc);
3083173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */
3084173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_RA, 1);   /* enable RA0 */
3085173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */
3086173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000);
3087173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002);
3088173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MAGIC4, 4);
3089173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MAGIC5, 5);
3090173362Sbenjsc
3091173362Sbenjsc	WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr);
3092173362Sbenjsc	WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5);
3093173362Sbenjsc
3094173362Sbenjsc	for (qid = 0; qid < 6; qid++) {
3095173362Sbenjsc		WPI_WRITE(sc, WPI_TX_CTL(qid), 0);
3096173362Sbenjsc		WPI_WRITE(sc, WPI_TX_BASE(qid), 0);
3097173362Sbenjsc		WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008);
3098173362Sbenjsc	}
3099173362Sbenjsc	wpi_mem_unlock(sc);
3100173362Sbenjsc
3101173362Sbenjsc	/* clear "radio off" and "disable command" bits (reversed logic) */
3102173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3103173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
3104173362Sbenjsc	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3105173362Sbenjsc
3106173362Sbenjsc	/* clear any pending interrupts */
3107173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
3108173362Sbenjsc
3109173362Sbenjsc	/* enable interrupts */
3110173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
3111173362Sbenjsc
3112173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3113173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3114173362Sbenjsc
3115177043Sthompsa	if ((wpi_load_firmware(sc)) != 0) {
3116173362Sbenjsc	    device_printf(sc->sc_dev,
3117173362Sbenjsc		"A problem occurred loading the firmware to the driver\n");
3118173362Sbenjsc	    return;
3119173362Sbenjsc	}
3120173362Sbenjsc
3121173362Sbenjsc	/* At this point the firmware is up and running. If the hardware
3122173362Sbenjsc	 * RF switch is turned off thermal calibration will fail, though
3123173362Sbenjsc	 * the card is still happy to continue to accept commands, catch
3124177043Sthompsa	 * this case and schedule a task to watch for it to be turned on.
3125173362Sbenjsc	 */
3126173362Sbenjsc	wpi_mem_lock(sc);
3127173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_HW_RADIO_OFF);
3128173362Sbenjsc	wpi_mem_unlock(sc);
3129173362Sbenjsc
3130173362Sbenjsc	if (!(tmp & 0x1)) {
3131173362Sbenjsc		sc->flags |= WPI_FLAG_HW_RADIO_OFF;
3132173362Sbenjsc		device_printf(sc->sc_dev,"Radio Transmitter is switched off\n");
3133177043Sthompsa		goto out;
3134173362Sbenjsc	}
3135173362Sbenjsc
3136173362Sbenjsc	/* wait for thermal sensors to calibrate */
3137173362Sbenjsc	for (ntries = 0; ntries < 1000; ntries++) {
3138173362Sbenjsc		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
3139173362Sbenjsc			break;
3140173362Sbenjsc		DELAY(10);
3141173362Sbenjsc	}
3142173362Sbenjsc
3143173362Sbenjsc	if (ntries == 1000) {
3144173362Sbenjsc		device_printf(sc->sc_dev,
3145173362Sbenjsc		    "timeout waiting for thermal sensors calibration\n");
3146173362Sbenjsc		return;
3147173362Sbenjsc	}
3148173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d\n", sc->temp));
3149173362Sbenjsc
3150177043Sthompsa	if (wpi_config(sc) != 0) {
3151177043Sthompsa		device_printf(sc->sc_dev, "device config failed\n");
3152177043Sthompsa		return;
3153177043Sthompsa	}
3154177043Sthompsa
3155173362Sbenjsc	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3156173362Sbenjsc	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3157177043Sthompsaout:
3158177043Sthompsa	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3159173362Sbenjsc}
3160173362Sbenjsc
3161173362Sbenjscstatic void
3162178354Ssamwpi_init(void *arg)
3163173362Sbenjsc{
3164178354Ssam	struct wpi_softc *sc = arg;
3165178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3166178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3167173362Sbenjsc
3168173362Sbenjsc	WPI_LOCK(sc);
3169178354Ssam	wpi_init_locked(sc, 0);
3170173362Sbenjsc	WPI_UNLOCK(sc);
3171173362Sbenjsc
3172178354Ssam	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3173178354Ssam		ieee80211_start_all(ic);		/* start all vaps */
3174173362Sbenjsc}
3175178354Ssam
3176173362Sbenjscstatic void
3177173362Sbenjscwpi_stop_locked(struct wpi_softc *sc)
3178173362Sbenjsc{
3179178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3180173362Sbenjsc	uint32_t tmp;
3181173362Sbenjsc	int ac;
3182173362Sbenjsc
3183177043Sthompsa	sc->sc_tx_timer = 0;
3184177043Sthompsa	sc->sc_scan_timer = 0;
3185173362Sbenjsc	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3186177043Sthompsa	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3187177043Sthompsa	callout_stop(&sc->watchdog_to);
3188177043Sthompsa	callout_stop(&sc->calib_to);
3189173362Sbenjsc
3190173362Sbenjsc	/* disable interrupts */
3191173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, 0);
3192173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK);
3193173362Sbenjsc	WPI_WRITE(sc, WPI_INTR_STATUS, 0xff);
3194173362Sbenjsc	WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000);
3195173362Sbenjsc
3196173362Sbenjsc	wpi_mem_lock(sc);
3197173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MODE, 0);
3198173362Sbenjsc	wpi_mem_unlock(sc);
3199173362Sbenjsc
3200173362Sbenjsc	/* reset all Tx rings */
3201173362Sbenjsc	for (ac = 0; ac < 4; ac++)
3202173362Sbenjsc		wpi_reset_tx_ring(sc, &sc->txq[ac]);
3203173362Sbenjsc	wpi_reset_tx_ring(sc, &sc->cmdq);
3204173362Sbenjsc
3205173362Sbenjsc	/* reset Rx ring */
3206173362Sbenjsc	wpi_reset_rx_ring(sc, &sc->rxq);
3207173362Sbenjsc
3208173362Sbenjsc	wpi_mem_lock(sc);
3209173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200);
3210173362Sbenjsc	wpi_mem_unlock(sc);
3211173362Sbenjsc
3212173362Sbenjsc	DELAY(5);
3213173362Sbenjsc
3214173362Sbenjsc	wpi_stop_master(sc);
3215173362Sbenjsc
3216173362Sbenjsc	tmp = WPI_READ(sc, WPI_RESET);
3217173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET);
3218173362Sbenjsc	sc->flags &= ~WPI_FLAG_BUSY;
3219173362Sbenjsc}
3220173362Sbenjsc
3221173362Sbenjscstatic void
3222178354Ssamwpi_stop(struct wpi_softc *sc)
3223173362Sbenjsc{
3224178354Ssam	WPI_LOCK(sc);
3225178354Ssam	wpi_stop_locked(sc);
3226178354Ssam	WPI_UNLOCK(sc);
3227173362Sbenjsc}
3228173362Sbenjsc
3229173362Sbenjscstatic void
3230173362Sbenjscwpi_calib_timeout(void *arg)
3231173362Sbenjsc{
3232173362Sbenjsc	struct wpi_softc *sc = arg;
3233178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3234178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3235178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3236173362Sbenjsc	int temp;
3237173362Sbenjsc
3238178354Ssam	if (vap->iv_state != IEEE80211_S_RUN)
3239177043Sthompsa		return;
3240177043Sthompsa
3241173362Sbenjsc	/* update sensor data */
3242173362Sbenjsc	temp = (int)WPI_READ(sc, WPI_TEMPERATURE);
3243173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("Temp in calibration is: %d\n", temp));
3244173362Sbenjsc
3245178354Ssam	wpi_power_calibration(sc, temp);
3246173362Sbenjsc
3247178354Ssam	callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
3248173362Sbenjsc}
3249173362Sbenjsc
3250173362Sbenjsc/*
3251173362Sbenjsc * This function is called periodically (every 60 seconds) to adjust output
3252173362Sbenjsc * power to temperature changes.
3253173362Sbenjsc */
3254173362Sbenjscstatic void
3255173362Sbenjscwpi_power_calibration(struct wpi_softc *sc, int temp)
3256173362Sbenjsc{
3257178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3258178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3259178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3260178354Ssam
3261173362Sbenjsc	/* sanity-check read value */
3262173362Sbenjsc	if (temp < -260 || temp > 25) {
3263173362Sbenjsc		/* this can't be correct, ignore */
3264173362Sbenjsc		DPRINTFN(WPI_DEBUG_TEMP,
3265173362Sbenjsc		    ("out-of-range temperature reported: %d\n", temp));
3266173362Sbenjsc		return;
3267173362Sbenjsc	}
3268173362Sbenjsc
3269173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d->%d\n", sc->temp, temp));
3270173362Sbenjsc
3271173362Sbenjsc	/* adjust Tx power if need be */
3272173362Sbenjsc	if (abs(temp - sc->temp) <= 6)
3273173362Sbenjsc		return;
3274173362Sbenjsc
3275173362Sbenjsc	sc->temp = temp;
3276173362Sbenjsc
3277178354Ssam	if (wpi_set_txpower(sc, vap->iv_bss->ni_chan, 1) != 0) {
3278173362Sbenjsc		/* just warn, too bad for the automatic calibration... */
3279173362Sbenjsc		device_printf(sc->sc_dev,"could not adjust Tx power\n");
3280173362Sbenjsc	}
3281173362Sbenjsc}
3282173362Sbenjsc
3283173362Sbenjsc/**
3284173362Sbenjsc * Read the eeprom to find out what channels are valid for the given
3285173362Sbenjsc * band and update net80211 with what we find.
3286173362Sbenjsc */
3287173362Sbenjscstatic void
3288173362Sbenjscwpi_read_eeprom_channels(struct wpi_softc *sc, int n)
3289173362Sbenjsc{
3290178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3291178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3292173362Sbenjsc	const struct wpi_chan_band *band = &wpi_bands[n];
3293173362Sbenjsc	struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND];
3294179957Sthompsa	struct ieee80211_channel *c;
3295179957Sthompsa	int chan, i, passive;
3296173362Sbenjsc
3297173362Sbenjsc	wpi_read_prom_data(sc, band->addr, channels,
3298173362Sbenjsc	    band->nchan * sizeof (struct wpi_eeprom_chan));
3299173362Sbenjsc
3300173362Sbenjsc	for (i = 0; i < band->nchan; i++) {
3301173362Sbenjsc		if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
3302173362Sbenjsc			DPRINTFN(WPI_DEBUG_HW,
3303173362Sbenjsc			    ("Channel Not Valid: %d, band %d\n",
3304173362Sbenjsc			     band->chan[i],n));
3305173362Sbenjsc			continue;
3306173362Sbenjsc		}
3307173362Sbenjsc
3308173362Sbenjsc		passive = 0;
3309173362Sbenjsc		chan = band->chan[i];
3310179957Sthompsa		c = &ic->ic_channels[ic->ic_nchans++];
3311173362Sbenjsc
3312173362Sbenjsc		/* is active scan allowed on this channel? */
3313173362Sbenjsc		if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) {
3314173362Sbenjsc			passive = IEEE80211_CHAN_PASSIVE;
3315173362Sbenjsc		}
3316173362Sbenjsc
3317173362Sbenjsc		if (n == 0) {	/* 2GHz band */
3318179957Sthompsa			c->ic_ieee = chan;
3319179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3320179957Sthompsa			    IEEE80211_CHAN_2GHZ);
3321179957Sthompsa			c->ic_flags = IEEE80211_CHAN_B | passive;
3322173362Sbenjsc
3323179957Sthompsa			c = &ic->ic_channels[ic->ic_nchans++];
3324179957Sthompsa			c->ic_ieee = chan;
3325179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3326179957Sthompsa			    IEEE80211_CHAN_2GHZ);
3327179957Sthompsa			c->ic_flags = IEEE80211_CHAN_G | passive;
3328179957Sthompsa
3329173362Sbenjsc		} else {	/* 5GHz band */
3330173362Sbenjsc			/*
3331173362Sbenjsc			 * Some 3945ABG adapters support channels 7, 8, 11
3332173362Sbenjsc			 * and 12 in the 2GHz *and* 5GHz bands.
3333173362Sbenjsc			 * Because of limitations in our net80211(9) stack,
3334173362Sbenjsc			 * we can't support these channels in 5GHz band.
3335173362Sbenjsc			 * XXX not true; just need to map to proper frequency
3336173362Sbenjsc			 */
3337173362Sbenjsc			if (chan <= 14)
3338173362Sbenjsc				continue;
3339173362Sbenjsc
3340179957Sthompsa			c->ic_ieee = chan;
3341179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3342179957Sthompsa			    IEEE80211_CHAN_5GHZ);
3343179957Sthompsa			c->ic_flags = IEEE80211_CHAN_A | passive;
3344173362Sbenjsc		}
3345173362Sbenjsc
3346173362Sbenjsc		/* save maximum allowed power for this channel */
3347173362Sbenjsc		sc->maxpwr[chan] = channels[i].maxpwr;
3348173362Sbenjsc
3349173362Sbenjsc#if 0
3350173362Sbenjsc		// XXX We can probably use this an get rid of maxpwr - ben 20070617
3351173362Sbenjsc		ic->ic_channels[chan].ic_maxpower = channels[i].maxpwr;
3352173362Sbenjsc		//ic->ic_channels[chan].ic_minpower...
3353173362Sbenjsc		//ic->ic_channels[chan].ic_maxregtxpower...
3354173362Sbenjsc#endif
3355173362Sbenjsc
3356179957Sthompsa		DPRINTF(("adding chan %d (%dMHz) flags=0x%x maxpwr=%d"
3357179957Sthompsa		    " passive=%d, offset %d\n", chan, c->ic_freq,
3358179957Sthompsa		    channels[i].flags, sc->maxpwr[chan],
3359179957Sthompsa		    (c->ic_flags & IEEE80211_CHAN_PASSIVE) != 0,
3360179957Sthompsa		    ic->ic_nchans));
3361173362Sbenjsc	}
3362173362Sbenjsc}
3363173362Sbenjsc
3364173362Sbenjscstatic void
3365173362Sbenjscwpi_read_eeprom_group(struct wpi_softc *sc, int n)
3366173362Sbenjsc{
3367173362Sbenjsc	struct wpi_power_group *group = &sc->groups[n];
3368173362Sbenjsc	struct wpi_eeprom_group rgroup;
3369173362Sbenjsc	int i;
3370173362Sbenjsc
3371173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup,
3372173362Sbenjsc	    sizeof rgroup);
3373173362Sbenjsc
3374173362Sbenjsc	/* save power group information */
3375173362Sbenjsc	group->chan   = rgroup.chan;
3376173362Sbenjsc	group->maxpwr = rgroup.maxpwr;
3377173362Sbenjsc	/* temperature at which the samples were taken */
3378173362Sbenjsc	group->temp   = (int16_t)le16toh(rgroup.temp);
3379173362Sbenjsc
3380173362Sbenjsc	DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n,
3381173362Sbenjsc		    group->chan, group->maxpwr, group->temp));
3382173362Sbenjsc
3383173362Sbenjsc	for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
3384173362Sbenjsc		group->samples[i].index = rgroup.samples[i].index;
3385173362Sbenjsc		group->samples[i].power = rgroup.samples[i].power;
3386173362Sbenjsc
3387173362Sbenjsc		DPRINTF(("\tsample %d: index=%d power=%d\n", i,
3388173362Sbenjsc			    group->samples[i].index, group->samples[i].power));
3389173362Sbenjsc	}
3390173362Sbenjsc}
3391173362Sbenjsc
3392173362Sbenjsc/*
3393173362Sbenjsc * Update Tx power to match what is defined for channel `c'.
3394173362Sbenjsc */
3395173362Sbenjscstatic int
3396173362Sbenjscwpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async)
3397173362Sbenjsc{
3398178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3399178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3400173362Sbenjsc	struct wpi_power_group *group;
3401173362Sbenjsc	struct wpi_cmd_txpower txpower;
3402173362Sbenjsc	u_int chan;
3403173362Sbenjsc	int i;
3404173362Sbenjsc
3405173362Sbenjsc	/* get channel number */
3406173362Sbenjsc	chan = ieee80211_chan2ieee(ic, c);
3407173362Sbenjsc
3408173362Sbenjsc	/* find the power group to which this channel belongs */
3409173362Sbenjsc	if (IEEE80211_IS_CHAN_5GHZ(c)) {
3410173362Sbenjsc		for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
3411173362Sbenjsc			if (chan <= group->chan)
3412173362Sbenjsc				break;
3413173362Sbenjsc	} else
3414173362Sbenjsc		group = &sc->groups[0];
3415173362Sbenjsc
3416173362Sbenjsc	memset(&txpower, 0, sizeof txpower);
3417173362Sbenjsc	txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1;
3418173362Sbenjsc	txpower.channel = htole16(chan);
3419173362Sbenjsc
3420173362Sbenjsc	/* set Tx power for all OFDM and CCK rates */
3421173362Sbenjsc	for (i = 0; i <= 11 ; i++) {
3422173362Sbenjsc		/* retrieve Tx power for this channel/rate combination */
3423173362Sbenjsc		int idx = wpi_get_power_index(sc, group, c,
3424173362Sbenjsc		    wpi_ridx_to_rate[i]);
3425173362Sbenjsc
3426173362Sbenjsc		txpower.rates[i].rate = wpi_ridx_to_plcp[i];
3427173362Sbenjsc
3428173362Sbenjsc		if (IEEE80211_IS_CHAN_5GHZ(c)) {
3429173362Sbenjsc			txpower.rates[i].gain_radio = wpi_rf_gain_5ghz[idx];
3430173362Sbenjsc			txpower.rates[i].gain_dsp = wpi_dsp_gain_5ghz[idx];
3431173362Sbenjsc		} else {
3432173362Sbenjsc			txpower.rates[i].gain_radio = wpi_rf_gain_2ghz[idx];
3433173362Sbenjsc			txpower.rates[i].gain_dsp = wpi_dsp_gain_2ghz[idx];
3434173362Sbenjsc		}
3435173362Sbenjsc		DPRINTFN(WPI_DEBUG_TEMP,("chan %d/rate %d: power index %d\n",
3436173362Sbenjsc			    chan, wpi_ridx_to_rate[i], idx));
3437173362Sbenjsc	}
3438173362Sbenjsc
3439173362Sbenjsc	return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async);
3440173362Sbenjsc}
3441173362Sbenjsc
3442173362Sbenjsc/*
3443173362Sbenjsc * Determine Tx power index for a given channel/rate combination.
3444173362Sbenjsc * This takes into account the regulatory information from EEPROM and the
3445173362Sbenjsc * current temperature.
3446173362Sbenjsc */
3447173362Sbenjscstatic int
3448173362Sbenjscwpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
3449173362Sbenjsc    struct ieee80211_channel *c, int rate)
3450173362Sbenjsc{
3451173362Sbenjsc/* fixed-point arithmetic division using a n-bit fractional part */
3452173362Sbenjsc#define fdivround(a, b, n)      \
3453173362Sbenjsc	((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3454173362Sbenjsc
3455173362Sbenjsc/* linear interpolation */
3456173362Sbenjsc#define interpolate(x, x1, y1, x2, y2, n)       \
3457173362Sbenjsc	((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3458173362Sbenjsc
3459178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3460178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3461173362Sbenjsc	struct wpi_power_sample *sample;
3462173362Sbenjsc	int pwr, idx;
3463173362Sbenjsc	u_int chan;
3464173362Sbenjsc
3465173362Sbenjsc	/* get channel number */
3466173362Sbenjsc	chan = ieee80211_chan2ieee(ic, c);
3467173362Sbenjsc
3468173362Sbenjsc	/* default power is group's maximum power - 3dB */
3469173362Sbenjsc	pwr = group->maxpwr / 2;
3470173362Sbenjsc
3471173362Sbenjsc	/* decrease power for highest OFDM rates to reduce distortion */
3472173362Sbenjsc	switch (rate) {
3473173362Sbenjsc		case 72:	/* 36Mb/s */
3474173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 :  5;
3475173362Sbenjsc			break;
3476173362Sbenjsc		case 96:	/* 48Mb/s */
3477173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10;
3478173362Sbenjsc			break;
3479173362Sbenjsc		case 108:	/* 54Mb/s */
3480173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12;
3481173362Sbenjsc			break;
3482173362Sbenjsc	}
3483173362Sbenjsc
3484173362Sbenjsc	/* never exceed channel's maximum allowed Tx power */
3485173362Sbenjsc	pwr = min(pwr, sc->maxpwr[chan]);
3486173362Sbenjsc
3487173362Sbenjsc	/* retrieve power index into gain tables from samples */
3488173362Sbenjsc	for (sample = group->samples; sample < &group->samples[3]; sample++)
3489173362Sbenjsc		if (pwr > sample[1].power)
3490173362Sbenjsc			break;
3491173362Sbenjsc	/* fixed-point linear interpolation using a 19-bit fractional part */
3492173362Sbenjsc	idx = interpolate(pwr, sample[0].power, sample[0].index,
3493173362Sbenjsc	    sample[1].power, sample[1].index, 19);
3494173362Sbenjsc
3495173362Sbenjsc	/*
3496173362Sbenjsc	 *  Adjust power index based on current temperature
3497173362Sbenjsc	 *	- if colder than factory-calibrated: decreate output power
3498173362Sbenjsc	 *	- if warmer than factory-calibrated: increase output power
3499173362Sbenjsc	 */
3500173362Sbenjsc	idx -= (sc->temp - group->temp) * 11 / 100;
3501173362Sbenjsc
3502173362Sbenjsc	/* decrease power for CCK rates (-5dB) */
3503173362Sbenjsc	if (!WPI_RATE_IS_OFDM(rate))
3504173362Sbenjsc		idx += 10;
3505173362Sbenjsc
3506173362Sbenjsc	/* keep power index in a valid range */
3507173362Sbenjsc	if (idx < 0)
3508173362Sbenjsc		return 0;
3509173362Sbenjsc	if (idx > WPI_MAX_PWR_INDEX)
3510173362Sbenjsc		return WPI_MAX_PWR_INDEX;
3511173362Sbenjsc	return idx;
3512173362Sbenjsc
3513173362Sbenjsc#undef interpolate
3514173362Sbenjsc#undef fdivround
3515173362Sbenjsc}
3516173362Sbenjsc
3517173362Sbenjsc/**
3518173362Sbenjsc * Called by net80211 framework to indicate that a scan
3519173362Sbenjsc * is starting. This function doesn't actually do the scan,
3520173362Sbenjsc * wpi_scan_curchan starts things off. This function is more
3521173362Sbenjsc * of an early warning from the framework we should get ready
3522173362Sbenjsc * for the scan.
3523173362Sbenjsc */
3524173362Sbenjscstatic void
3525173362Sbenjscwpi_scan_start(struct ieee80211com *ic)
3526173362Sbenjsc{
3527173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
3528173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3529173362Sbenjsc
3530191746Sthompsa	WPI_LOCK(sc);
3531191746Sthompsa	wpi_set_led(sc, WPI_LED_LINK, 20, 2);
3532191746Sthompsa	WPI_UNLOCK(sc);
3533173362Sbenjsc}
3534173362Sbenjsc
3535173362Sbenjsc/**
3536173362Sbenjsc * Called by the net80211 framework, indicates that the
3537173362Sbenjsc * scan has ended. If there is a scan in progress on the card
3538173362Sbenjsc * then it should be aborted.
3539173362Sbenjsc */
3540173362Sbenjscstatic void
3541173362Sbenjscwpi_scan_end(struct ieee80211com *ic)
3542173362Sbenjsc{
3543191746Sthompsa	/* XXX ignore */
3544173362Sbenjsc}
3545173362Sbenjsc
3546173362Sbenjsc/**
3547173362Sbenjsc * Called by the net80211 framework to indicate to the driver
3548173362Sbenjsc * that the channel should be changed
3549173362Sbenjsc */
3550173362Sbenjscstatic void
3551173362Sbenjscwpi_set_channel(struct ieee80211com *ic)
3552173362Sbenjsc{
3553173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
3554173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3555191746Sthompsa	int error;
3556173362Sbenjsc
3557177043Sthompsa	/*
3558177043Sthompsa	 * Only need to set the channel in Monitor mode. AP scanning and auth
3559177043Sthompsa	 * are already taken care of by their respective firmware commands.
3560177043Sthompsa	 */
3561191746Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
3562216557Sbschmidt		WPI_LOCK(sc);
3563191746Sthompsa		error = wpi_config(sc);
3564216557Sbschmidt		WPI_UNLOCK(sc);
3565191746Sthompsa		if (error != 0)
3566191746Sthompsa			device_printf(sc->sc_dev,
3567191746Sthompsa			    "error %d settting channel\n", error);
3568191746Sthompsa	}
3569173362Sbenjsc}
3570173362Sbenjsc
3571173362Sbenjsc/**
3572173362Sbenjsc * Called by net80211 to indicate that we need to scan the current
3573173362Sbenjsc * channel. The channel is previously be set via the wpi_set_channel
3574173362Sbenjsc * callback.
3575173362Sbenjsc */
3576173362Sbenjscstatic void
3577178354Ssamwpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
3578173362Sbenjsc{
3579178354Ssam	struct ieee80211vap *vap = ss->ss_vap;
3580178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
3581173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3582173362Sbenjsc
3583191746Sthompsa	WPI_LOCK(sc);
3584191746Sthompsa	if (wpi_scan(sc))
3585191746Sthompsa		ieee80211_cancel_scan(vap);
3586191746Sthompsa	WPI_UNLOCK(sc);
3587173362Sbenjsc}
3588173362Sbenjsc
3589173362Sbenjsc/**
3590173362Sbenjsc * Called by the net80211 framework to indicate
3591173362Sbenjsc * the minimum dwell time has been met, terminate the scan.
3592173362Sbenjsc * We don't actually terminate the scan as the firmware will notify
3593173362Sbenjsc * us when it's finished and we have no way to interrupt it.
3594173362Sbenjsc */
3595173362Sbenjscstatic void
3596178354Ssamwpi_scan_mindwell(struct ieee80211_scan_state *ss)
3597173362Sbenjsc{
3598173362Sbenjsc	/* NB: don't try to abort scan; wait for firmware to finish */
3599173362Sbenjsc}
3600173362Sbenjsc
3601173362Sbenjscstatic void
3602191746Sthompsawpi_hwreset(void *arg, int pending)
3603173362Sbenjsc{
3604191746Sthompsa	struct wpi_softc *sc = arg;
3605173362Sbenjsc
3606173362Sbenjsc	WPI_LOCK(sc);
3607191746Sthompsa	wpi_init_locked(sc, 0);
3608173362Sbenjsc	WPI_UNLOCK(sc);
3609173362Sbenjsc}
3610173362Sbenjsc
3611191746Sthompsastatic void
3612191746Sthompsawpi_rfreset(void *arg, int pending)
3613173362Sbenjsc{
3614191746Sthompsa	struct wpi_softc *sc = arg;
3615173362Sbenjsc
3616191746Sthompsa	WPI_LOCK(sc);
3617191746Sthompsa	wpi_rfkill_resume(sc);
3618191746Sthompsa	WPI_UNLOCK(sc);
3619173362Sbenjsc}
3620173362Sbenjsc
3621173362Sbenjsc/*
3622173362Sbenjsc * Allocate DMA-safe memory for firmware transfer.
3623173362Sbenjsc */
3624173362Sbenjscstatic int
3625173362Sbenjscwpi_alloc_fwmem(struct wpi_softc *sc)
3626173362Sbenjsc{
3627173362Sbenjsc	/* allocate enough contiguous space to store text and data */
3628173362Sbenjsc	return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL,
3629173362Sbenjsc	    WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 1,
3630173362Sbenjsc	    BUS_DMA_NOWAIT);
3631173362Sbenjsc}
3632173362Sbenjsc
3633173362Sbenjscstatic void
3634173362Sbenjscwpi_free_fwmem(struct wpi_softc *sc)
3635173362Sbenjsc{
3636173362Sbenjsc	wpi_dma_contig_free(&sc->fw_dma);
3637173362Sbenjsc}
3638173362Sbenjsc
3639173362Sbenjsc/**
3640177043Sthompsa * Called every second, wpi_watchdog used by the watch dog timer
3641173362Sbenjsc * to check that the card is still alive
3642173362Sbenjsc */
3643173362Sbenjscstatic void
3644177043Sthompsawpi_watchdog(void *arg)
3645173362Sbenjsc{
3646173362Sbenjsc	struct wpi_softc *sc = arg;
3647177043Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3648191746Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
3649177043Sthompsa	uint32_t tmp;
3650173362Sbenjsc
3651173362Sbenjsc	DPRINTFN(WPI_DEBUG_WATCHDOG,("Watchdog: tick\n"));
3652173362Sbenjsc
3653177043Sthompsa	if (sc->flags & WPI_FLAG_HW_RADIO_OFF) {
3654177043Sthompsa		/* No need to lock firmware memory */
3655177043Sthompsa		tmp = wpi_mem_read(sc, WPI_MEM_HW_RADIO_OFF);
3656177043Sthompsa
3657177043Sthompsa		if ((tmp & 0x1) == 0) {
3658177043Sthompsa			/* Radio kill switch is still off */
3659177043Sthompsa			callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3660177043Sthompsa			return;
3661177043Sthompsa		}
3662177043Sthompsa
3663177043Sthompsa		device_printf(sc->sc_dev, "Hardware Switch Enabled\n");
3664191746Sthompsa		ieee80211_runtask(ic, &sc->sc_radiotask);
3665177043Sthompsa		return;
3666177043Sthompsa	}
3667177043Sthompsa
3668177043Sthompsa	if (sc->sc_tx_timer > 0) {
3669177043Sthompsa		if (--sc->sc_tx_timer == 0) {
3670177043Sthompsa			device_printf(sc->sc_dev,"device timeout\n");
3671177043Sthompsa			ifp->if_oerrors++;
3672191746Sthompsa			ieee80211_runtask(ic, &sc->sc_restarttask);
3673177043Sthompsa		}
3674177043Sthompsa	}
3675177043Sthompsa	if (sc->sc_scan_timer > 0) {
3676178354Ssam		struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3677178354Ssam		if (--sc->sc_scan_timer == 0 && vap != NULL) {
3678177043Sthompsa			device_printf(sc->sc_dev,"scan timeout\n");
3679178354Ssam			ieee80211_cancel_scan(vap);
3680191746Sthompsa			ieee80211_runtask(ic, &sc->sc_restarttask);
3681177043Sthompsa		}
3682177043Sthompsa	}
3683177043Sthompsa
3684177043Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3685177043Sthompsa		callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3686173362Sbenjsc}
3687173362Sbenjsc
3688173362Sbenjsc#ifdef WPI_DEBUG
3689173362Sbenjscstatic const char *wpi_cmd_str(int cmd)
3690173362Sbenjsc{
3691178354Ssam	switch (cmd) {
3692178354Ssam	case WPI_DISABLE_CMD:	return "WPI_DISABLE_CMD";
3693178354Ssam	case WPI_CMD_CONFIGURE:	return "WPI_CMD_CONFIGURE";
3694178354Ssam	case WPI_CMD_ASSOCIATE:	return "WPI_CMD_ASSOCIATE";
3695178354Ssam	case WPI_CMD_SET_WME:	return "WPI_CMD_SET_WME";
3696178354Ssam	case WPI_CMD_TSF:	return "WPI_CMD_TSF";
3697178354Ssam	case WPI_CMD_ADD_NODE:	return "WPI_CMD_ADD_NODE";
3698178354Ssam	case WPI_CMD_TX_DATA:	return "WPI_CMD_TX_DATA";
3699178354Ssam	case WPI_CMD_MRR_SETUP:	return "WPI_CMD_MRR_SETUP";
3700178354Ssam	case WPI_CMD_SET_LED:	return "WPI_CMD_SET_LED";
3701178354Ssam	case WPI_CMD_SET_POWER_MODE: return "WPI_CMD_SET_POWER_MODE";
3702178354Ssam	case WPI_CMD_SCAN:	return "WPI_CMD_SCAN";
3703178354Ssam	case WPI_CMD_SET_BEACON:return "WPI_CMD_SET_BEACON";
3704178354Ssam	case WPI_CMD_TXPOWER:	return "WPI_CMD_TXPOWER";
3705178354Ssam	case WPI_CMD_BLUETOOTH:	return "WPI_CMD_BLUETOOTH";
3706173362Sbenjsc
3707178354Ssam	default:
3708173362Sbenjsc		KASSERT(1, ("Unknown Command: %d\n", cmd));
3709178354Ssam		return "UNKNOWN CMD";	/* Make the compiler happy */
3710173362Sbenjsc	}
3711173362Sbenjsc}
3712173362Sbenjsc#endif
3713173362Sbenjsc
3714173362SbenjscMODULE_DEPEND(wpi, pci,  1, 1, 1);
3715173362SbenjscMODULE_DEPEND(wpi, wlan, 1, 1, 1);
3716173362SbenjscMODULE_DEPEND(wpi, firmware, 1, 1, 1);
3717