if_wpi.c revision 200530
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 200530 2009-12-14 19:18:02Z gavin $");
23173362Sbenjsc
24173362Sbenjsc/*
25173362Sbenjsc * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
26173362Sbenjsc *
27173362Sbenjsc * The 3945ABG network adapter doesn't use traditional hardware as
28173362Sbenjsc * many other adaptors do. Instead at run time the eeprom is set into a known
29173362Sbenjsc * state and told to load boot firmware. The boot firmware loads an init and a
30173362Sbenjsc * main  binary firmware image into SRAM on the card via DMA.
31173362Sbenjsc * Once the firmware is loaded, the driver/hw then
32173362Sbenjsc * communicate by way of circular dma rings via the the SRAM to the firmware.
33173362Sbenjsc *
34173362Sbenjsc * There is 6 memory rings. 1 command ring, 1 rx data ring & 4 tx data rings.
35173362Sbenjsc * The 4 tx data rings allow for prioritization QoS.
36173362Sbenjsc *
37173362Sbenjsc * The rx data ring consists of 32 dma buffers. Two registers are used to
38173362Sbenjsc * indicate where in the ring the driver and the firmware are up to. The
39173362Sbenjsc * driver sets the initial read index (reg1) and the initial write index (reg2),
40173362Sbenjsc * the firmware updates the read index (reg1) on rx of a packet and fires an
41173362Sbenjsc * interrupt. The driver then processes the buffers starting at reg1 indicating
42173362Sbenjsc * to the firmware which buffers have been accessed by updating reg2. At the
43173362Sbenjsc * same time allocating new memory for the processed buffer.
44173362Sbenjsc *
45173362Sbenjsc * A similar thing happens with the tx rings. The difference is the firmware
46173362Sbenjsc * stop processing buffers once the queue is full and until confirmation
47173362Sbenjsc * of a successful transmition (tx_intr) has occurred.
48173362Sbenjsc *
49173362Sbenjsc * The command ring operates in the same manner as the tx queues.
50173362Sbenjsc *
51173362Sbenjsc * All communication direct to the card (ie eeprom) is classed as Stage1
52173362Sbenjsc * communication
53173362Sbenjsc *
54173362Sbenjsc * All communication via the firmware to the card is classed as State2.
55173362Sbenjsc * The firmware consists of 2 parts. A bootstrap firmware and a runtime
56173362Sbenjsc * firmware. The bootstrap firmware and runtime firmware are loaded
57173362Sbenjsc * from host memory via dma to the card then told to execute. From this point
58173362Sbenjsc * on the majority of communications between the driver and the card goes
59173362Sbenjsc * via the firmware.
60173362Sbenjsc */
61173362Sbenjsc
62173362Sbenjsc#include <sys/param.h>
63173362Sbenjsc#include <sys/sysctl.h>
64173362Sbenjsc#include <sys/sockio.h>
65173362Sbenjsc#include <sys/mbuf.h>
66173362Sbenjsc#include <sys/kernel.h>
67173362Sbenjsc#include <sys/socket.h>
68173362Sbenjsc#include <sys/systm.h>
69173362Sbenjsc#include <sys/malloc.h>
70173362Sbenjsc#include <sys/queue.h>
71173362Sbenjsc#include <sys/taskqueue.h>
72173362Sbenjsc#include <sys/module.h>
73173362Sbenjsc#include <sys/bus.h>
74173362Sbenjsc#include <sys/endian.h>
75173362Sbenjsc#include <sys/linker.h>
76173362Sbenjsc#include <sys/firmware.h>
77173362Sbenjsc
78173362Sbenjsc#include <machine/bus.h>
79173362Sbenjsc#include <machine/resource.h>
80173362Sbenjsc#include <sys/rman.h>
81173362Sbenjsc
82173362Sbenjsc#include <dev/pci/pcireg.h>
83173362Sbenjsc#include <dev/pci/pcivar.h>
84173362Sbenjsc
85173362Sbenjsc#include <net/bpf.h>
86173362Sbenjsc#include <net/if.h>
87173362Sbenjsc#include <net/if_arp.h>
88173362Sbenjsc#include <net/ethernet.h>
89173362Sbenjsc#include <net/if_dl.h>
90173362Sbenjsc#include <net/if_media.h>
91173362Sbenjsc#include <net/if_types.h>
92173362Sbenjsc
93173362Sbenjsc#include <net80211/ieee80211_var.h>
94173362Sbenjsc#include <net80211/ieee80211_radiotap.h>
95173362Sbenjsc#include <net80211/ieee80211_regdomain.h>
96173362Sbenjsc
97173362Sbenjsc#include <netinet/in.h>
98173362Sbenjsc#include <netinet/in_systm.h>
99173362Sbenjsc#include <netinet/in_var.h>
100173362Sbenjsc#include <netinet/ip.h>
101173362Sbenjsc#include <netinet/if_ether.h>
102173362Sbenjsc
103173362Sbenjsc#include <dev/wpi/if_wpireg.h>
104173362Sbenjsc#include <dev/wpi/if_wpivar.h>
105173362Sbenjsc
106199037Sdougb#define WPI_DEBUG
107199037Sdougb
108173362Sbenjsc#ifdef WPI_DEBUG
109173362Sbenjsc#define DPRINTF(x)	do { if (wpi_debug != 0) printf x; } while (0)
110173362Sbenjsc#define DPRINTFN(n, x)	do { if (wpi_debug & n) printf x; } while (0)
111179957Sthompsa#define	WPI_DEBUG_SET	(wpi_debug != 0)
112173362Sbenjsc
113173362Sbenjscenum {
114173362Sbenjsc	WPI_DEBUG_UNUSED	= 0x00000001,   /* Unused */
115173362Sbenjsc	WPI_DEBUG_HW		= 0x00000002,   /* Stage 1 (eeprom) debugging */
116173362Sbenjsc	WPI_DEBUG_TX		= 0x00000004,   /* Stage 2 TX intrp debugging*/
117173362Sbenjsc	WPI_DEBUG_RX		= 0x00000008,   /* Stage 2 RX intrp debugging */
118173362Sbenjsc	WPI_DEBUG_CMD		= 0x00000010,   /* Stage 2 CMD intrp debugging*/
119173362Sbenjsc	WPI_DEBUG_FIRMWARE	= 0x00000020,   /* firmware(9) loading debug  */
120173362Sbenjsc	WPI_DEBUG_DMA		= 0x00000040,   /* DMA (de)allocations/syncs  */
121173362Sbenjsc	WPI_DEBUG_SCANNING	= 0x00000080,   /* Stage 2 Scanning debugging */
122173362Sbenjsc	WPI_DEBUG_NOTIFY	= 0x00000100,   /* State 2 Noftif intr debug */
123173362Sbenjsc	WPI_DEBUG_TEMP		= 0x00000200,   /* TXPower/Temp Calibration */
124173362Sbenjsc	WPI_DEBUG_OPS		= 0x00000400,   /* wpi_ops taskq debug */
125173362Sbenjsc	WPI_DEBUG_WATCHDOG	= 0x00000800,   /* Watch dog debug */
126173362Sbenjsc	WPI_DEBUG_ANY		= 0xffffffff
127173362Sbenjsc};
128173362Sbenjsc
129199037Sdougbstatic int wpi_debug = 0;
130173362SbenjscSYSCTL_INT(_debug, OID_AUTO, wpi, CTLFLAG_RW, &wpi_debug, 0, "wpi debug level");
131179957SthompsaTUNABLE_INT("debug.wpi", &wpi_debug);
132173362Sbenjsc
133173362Sbenjsc#else
134173362Sbenjsc#define DPRINTF(x)
135173362Sbenjsc#define DPRINTFN(n, x)
136179957Sthompsa#define WPI_DEBUG_SET	0
137173362Sbenjsc#endif
138173362Sbenjsc
139173362Sbenjscstruct wpi_ident {
140173362Sbenjsc	uint16_t	vendor;
141173362Sbenjsc	uint16_t	device;
142173362Sbenjsc	uint16_t	subdevice;
143173362Sbenjsc	const char	*name;
144173362Sbenjsc};
145173362Sbenjsc
146173362Sbenjscstatic const struct wpi_ident wpi_ident_table[] = {
147173362Sbenjsc	/* The below entries support ABG regardless of the subid */
148173362Sbenjsc	{ 0x8086, 0x4222,    0x0, "Intel(R) PRO/Wireless 3945ABG" },
149173362Sbenjsc	{ 0x8086, 0x4227,    0x0, "Intel(R) PRO/Wireless 3945ABG" },
150173362Sbenjsc	/* The below entries only support BG */
151182127Sbenjsc	{ 0x8086, 0x4222, 0x1005, "Intel(R) PRO/Wireless 3945BG"  },
152182127Sbenjsc	{ 0x8086, 0x4222, 0x1034, "Intel(R) PRO/Wireless 3945BG"  },
153182127Sbenjsc	{ 0x8086, 0x4227, 0x1014, "Intel(R) PRO/Wireless 3945BG"  },
154182127Sbenjsc	{ 0x8086, 0x4222, 0x1044, "Intel(R) PRO/Wireless 3945BG"  },
155173362Sbenjsc	{ 0, 0, 0, NULL }
156173362Sbenjsc};
157173362Sbenjsc
158178354Ssamstatic struct ieee80211vap *wpi_vap_create(struct ieee80211com *,
159178354Ssam		    const char name[IFNAMSIZ], int unit, int opmode,
160178354Ssam		    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
161178354Ssam		    const uint8_t mac[IEEE80211_ADDR_LEN]);
162178354Ssamstatic void	wpi_vap_delete(struct ieee80211vap *);
163173362Sbenjscstatic int	wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *,
164173362Sbenjsc		    void **, bus_size_t, bus_size_t, int);
165173362Sbenjscstatic void	wpi_dma_contig_free(struct wpi_dma_info *);
166173362Sbenjscstatic void	wpi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
167173362Sbenjscstatic int	wpi_alloc_shared(struct wpi_softc *);
168173362Sbenjscstatic void	wpi_free_shared(struct wpi_softc *);
169173362Sbenjscstatic int	wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
170173362Sbenjscstatic void	wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
171173362Sbenjscstatic void	wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
172173362Sbenjscstatic int	wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
173173362Sbenjsc		    int, int);
174173362Sbenjscstatic void	wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
175173362Sbenjscstatic void	wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
176179643Ssamstatic struct ieee80211_node *wpi_node_alloc(struct ieee80211vap *,
177179643Ssam			    const uint8_t mac[IEEE80211_ADDR_LEN]);
178178354Ssamstatic int	wpi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
179173362Sbenjscstatic void	wpi_mem_lock(struct wpi_softc *);
180173362Sbenjscstatic void	wpi_mem_unlock(struct wpi_softc *);
181173362Sbenjscstatic uint32_t	wpi_mem_read(struct wpi_softc *, uint16_t);
182173362Sbenjscstatic void	wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t);
183173362Sbenjscstatic void	wpi_mem_write_region_4(struct wpi_softc *, uint16_t,
184173362Sbenjsc		    const uint32_t *, int);
185173362Sbenjscstatic uint16_t	wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
186173362Sbenjscstatic int	wpi_alloc_fwmem(struct wpi_softc *);
187173362Sbenjscstatic void	wpi_free_fwmem(struct wpi_softc *);
188173362Sbenjscstatic int	wpi_load_firmware(struct wpi_softc *);
189173362Sbenjscstatic void	wpi_unload_firmware(struct wpi_softc *);
190173362Sbenjscstatic int	wpi_load_microcode(struct wpi_softc *, const uint8_t *, int);
191173362Sbenjscstatic void	wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *,
192173362Sbenjsc		    struct wpi_rx_data *);
193173362Sbenjscstatic void	wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *);
194173362Sbenjscstatic void	wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *);
195173362Sbenjscstatic void	wpi_notif_intr(struct wpi_softc *);
196173362Sbenjscstatic void	wpi_intr(void *);
197173362Sbenjscstatic uint8_t	wpi_plcp_signal(int);
198177043Sthompsastatic void	wpi_watchdog(void *);
199173362Sbenjscstatic int	wpi_tx_data(struct wpi_softc *, struct mbuf *,
200173362Sbenjsc		    struct ieee80211_node *, int);
201173362Sbenjscstatic void	wpi_start(struct ifnet *);
202178354Ssamstatic void	wpi_start_locked(struct ifnet *);
203178354Ssamstatic int	wpi_raw_xmit(struct ieee80211_node *, struct mbuf *,
204178354Ssam		    const struct ieee80211_bpf_params *);
205173362Sbenjscstatic void	wpi_scan_start(struct ieee80211com *);
206173362Sbenjscstatic void	wpi_scan_end(struct ieee80211com *);
207173362Sbenjscstatic void	wpi_set_channel(struct ieee80211com *);
208178354Ssamstatic void	wpi_scan_curchan(struct ieee80211_scan_state *, unsigned long);
209178354Ssamstatic void	wpi_scan_mindwell(struct ieee80211_scan_state *);
210173362Sbenjscstatic int	wpi_ioctl(struct ifnet *, u_long, caddr_t);
211190526Ssamstatic void	wpi_read_eeprom(struct wpi_softc *,
212190526Ssam		    uint8_t macaddr[IEEE80211_ADDR_LEN]);
213173362Sbenjscstatic void	wpi_read_eeprom_channels(struct wpi_softc *, int);
214173362Sbenjscstatic void	wpi_read_eeprom_group(struct wpi_softc *, int);
215173362Sbenjscstatic int	wpi_cmd(struct wpi_softc *, int, const void *, int, int);
216173362Sbenjscstatic int	wpi_wme_update(struct ieee80211com *);
217173362Sbenjscstatic int	wpi_mrr_setup(struct wpi_softc *);
218173362Sbenjscstatic void	wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
219173362Sbenjscstatic void	wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *);
220173362Sbenjsc#if 0
221173362Sbenjscstatic int	wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
222173362Sbenjsc#endif
223178354Ssamstatic int	wpi_auth(struct wpi_softc *, struct ieee80211vap *);
224178354Ssamstatic int	wpi_run(struct wpi_softc *, struct ieee80211vap *);
225173362Sbenjscstatic int	wpi_scan(struct wpi_softc *);
226173362Sbenjscstatic int	wpi_config(struct wpi_softc *);
227173362Sbenjscstatic void	wpi_stop_master(struct wpi_softc *);
228173362Sbenjscstatic int	wpi_power_up(struct wpi_softc *);
229173362Sbenjscstatic int	wpi_reset(struct wpi_softc *);
230191746Sthompsastatic void	wpi_hwreset(void *, int);
231191746Sthompsastatic void	wpi_rfreset(void *, int);
232173362Sbenjscstatic void	wpi_hw_config(struct wpi_softc *);
233173362Sbenjscstatic void	wpi_init(void *);
234177043Sthompsastatic void	wpi_init_locked(struct wpi_softc *, int);
235173362Sbenjscstatic void	wpi_stop(struct wpi_softc *);
236173362Sbenjscstatic void	wpi_stop_locked(struct wpi_softc *);
237173362Sbenjsc
238173362Sbenjscstatic void	wpi_newassoc(struct ieee80211_node *, int);
239173362Sbenjscstatic int	wpi_set_txpower(struct wpi_softc *, struct ieee80211_channel *,
240173362Sbenjsc		    int);
241173362Sbenjscstatic void	wpi_calib_timeout(void *);
242173362Sbenjscstatic void	wpi_power_calibration(struct wpi_softc *, int);
243173362Sbenjscstatic int	wpi_get_power_index(struct wpi_softc *,
244173362Sbenjsc		    struct wpi_power_group *, struct ieee80211_channel *, int);
245179957Sthompsa#ifdef WPI_DEBUG
246173362Sbenjscstatic const char *wpi_cmd_str(int);
247179957Sthompsa#endif
248173362Sbenjscstatic int wpi_probe(device_t);
249173362Sbenjscstatic int wpi_attach(device_t);
250173362Sbenjscstatic int wpi_detach(device_t);
251173362Sbenjscstatic int wpi_shutdown(device_t);
252173362Sbenjscstatic int wpi_suspend(device_t);
253173362Sbenjscstatic int wpi_resume(device_t);
254173362Sbenjsc
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
265173362Sbenjsc	{ 0, 0 }
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
276173362SbenjscDRIVER_MODULE(wpi, pci, wpi_driver, wpi_devclass, 0, 0);
277173362Sbenjsc
278173362Sbenjscstatic const uint8_t wpi_ridx_to_plcp[] = {
279173362Sbenjsc	/* OFDM: IEEE Std 802.11a-1999, pp. 14 Table 80 */
280173362Sbenjsc	/* R1-R4 (ral/ural is R4-R1) */
281173362Sbenjsc	0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3,
282173362Sbenjsc	/* CCK: device-dependent */
283173362Sbenjsc	10, 20, 55, 110
284173362Sbenjsc};
285173362Sbenjscstatic const uint8_t wpi_ridx_to_rate[] = {
286173362Sbenjsc	12, 18, 24, 36, 48, 72, 96, 108, /* OFDM */
287173362Sbenjsc	2, 4, 11, 22 /*CCK */
288173362Sbenjsc};
289173362Sbenjsc
290173362Sbenjsc
291173362Sbenjscstatic int
292173362Sbenjscwpi_probe(device_t dev)
293173362Sbenjsc{
294173362Sbenjsc	const struct wpi_ident *ident;
295173362Sbenjsc
296173362Sbenjsc	for (ident = wpi_ident_table; ident->name != NULL; ident++) {
297173362Sbenjsc		if (pci_get_vendor(dev) == ident->vendor &&
298173362Sbenjsc		    pci_get_device(dev) == ident->device) {
299173362Sbenjsc			device_set_desc(dev, ident->name);
300173362Sbenjsc			return 0;
301173362Sbenjsc		}
302173362Sbenjsc	}
303173362Sbenjsc	return ENXIO;
304173362Sbenjsc}
305173362Sbenjsc
306173362Sbenjsc/**
307173362Sbenjsc * Load the firmare image from disk to the allocated dma buffer.
308173362Sbenjsc * we also maintain the reference to the firmware pointer as there
309173362Sbenjsc * is times where we may need to reload the firmware but we are not
310173362Sbenjsc * in a context that can access the filesystem (ie taskq cause by restart)
311173362Sbenjsc *
312173362Sbenjsc * @return 0 on success, an errno on failure
313173362Sbenjsc */
314173362Sbenjscstatic int
315173362Sbenjscwpi_load_firmware(struct wpi_softc *sc)
316173362Sbenjsc{
317178354Ssam	const struct firmware *fp;
318173362Sbenjsc	struct wpi_dma_info *dma = &sc->fw_dma;
319173362Sbenjsc	const struct wpi_firmware_hdr *hdr;
320173362Sbenjsc	const uint8_t *itext, *idata, *rtext, *rdata, *btext;
321173362Sbenjsc	uint32_t itextsz, idatasz, rtextsz, rdatasz, btextsz;
322173362Sbenjsc	int error;
323173362Sbenjsc
324173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
325173362Sbenjsc	    ("Attempting Loading Firmware from wpi_fw module\n"));
326173362Sbenjsc
327173362Sbenjsc	WPI_UNLOCK(sc);
328173362Sbenjsc
329173362Sbenjsc	if (sc->fw_fp == NULL && (sc->fw_fp = firmware_get("wpifw")) == NULL) {
330173362Sbenjsc		device_printf(sc->sc_dev,
331173362Sbenjsc		    "could not load firmware image 'wpifw'\n");
332173362Sbenjsc		error = ENOENT;
333173362Sbenjsc		WPI_LOCK(sc);
334173362Sbenjsc		goto fail;
335173362Sbenjsc	}
336173362Sbenjsc
337173362Sbenjsc	fp = sc->fw_fp;
338173362Sbenjsc
339173362Sbenjsc	WPI_LOCK(sc);
340173362Sbenjsc
341173362Sbenjsc	/* Validate the firmware is minimum a particular version */
342173362Sbenjsc	if (fp->version < WPI_FW_MINVERSION) {
343173362Sbenjsc	    device_printf(sc->sc_dev,
344173362Sbenjsc			   "firmware version is too old. Need %d, got %d\n",
345173362Sbenjsc			   WPI_FW_MINVERSION,
346173362Sbenjsc			   fp->version);
347173362Sbenjsc	    error = ENXIO;
348173362Sbenjsc	    goto fail;
349173362Sbenjsc	}
350173362Sbenjsc
351173362Sbenjsc	if (fp->datasize < sizeof (struct wpi_firmware_hdr)) {
352173362Sbenjsc		device_printf(sc->sc_dev,
353173362Sbenjsc		    "firmware file too short: %zu bytes\n", fp->datasize);
354173362Sbenjsc		error = ENXIO;
355173362Sbenjsc		goto fail;
356173362Sbenjsc	}
357173362Sbenjsc
358173362Sbenjsc	hdr = (const struct wpi_firmware_hdr *)fp->data;
359173362Sbenjsc
360173362Sbenjsc	/*     |  RUNTIME FIRMWARE   |    INIT FIRMWARE    | BOOT FW  |
361173362Sbenjsc	   |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */
362173362Sbenjsc
363173362Sbenjsc	rtextsz = le32toh(hdr->rtextsz);
364173362Sbenjsc	rdatasz = le32toh(hdr->rdatasz);
365173362Sbenjsc	itextsz = le32toh(hdr->itextsz);
366173362Sbenjsc	idatasz = le32toh(hdr->idatasz);
367173362Sbenjsc	btextsz = le32toh(hdr->btextsz);
368173362Sbenjsc
369173362Sbenjsc	/* check that all firmware segments are present */
370173362Sbenjsc	if (fp->datasize < sizeof (struct wpi_firmware_hdr) +
371173362Sbenjsc		rtextsz + rdatasz + itextsz + idatasz + btextsz) {
372173362Sbenjsc		device_printf(sc->sc_dev,
373173362Sbenjsc		    "firmware file too short: %zu bytes\n", fp->datasize);
374173362Sbenjsc		error = ENXIO; /* XXX appropriate error code? */
375173362Sbenjsc		goto fail;
376173362Sbenjsc	}
377173362Sbenjsc
378173362Sbenjsc	/* get pointers to firmware segments */
379173362Sbenjsc	rtext = (const uint8_t *)(hdr + 1);
380173362Sbenjsc	rdata = rtext + rtextsz;
381173362Sbenjsc	itext = rdata + rdatasz;
382173362Sbenjsc	idata = itext + itextsz;
383173362Sbenjsc	btext = idata + idatasz;
384173362Sbenjsc
385173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
386173362Sbenjsc	    ("Firmware Version: Major %d, Minor %d, Driver %d, \n"
387173362Sbenjsc	     "runtime (text: %u, data: %u) init (text: %u, data %u) boot (text %u)\n",
388173362Sbenjsc	     (le32toh(hdr->version) & 0xff000000) >> 24,
389173362Sbenjsc	     (le32toh(hdr->version) & 0x00ff0000) >> 16,
390173362Sbenjsc	     (le32toh(hdr->version) & 0x0000ffff),
391173362Sbenjsc	     rtextsz, rdatasz,
392173362Sbenjsc	     itextsz, idatasz, btextsz));
393173362Sbenjsc
394173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("rtext 0x%x\n", *(const uint32_t *)rtext));
395173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("rdata 0x%x\n", *(const uint32_t *)rdata));
396173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("itext 0x%x\n", *(const uint32_t *)itext));
397173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("idata 0x%x\n", *(const uint32_t *)idata));
398173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("btext 0x%x\n", *(const uint32_t *)btext));
399173362Sbenjsc
400173362Sbenjsc	/* sanity checks */
401173362Sbenjsc	if (rtextsz > WPI_FW_MAIN_TEXT_MAXSZ ||
402173362Sbenjsc	    rdatasz > WPI_FW_MAIN_DATA_MAXSZ ||
403173362Sbenjsc	    itextsz > WPI_FW_INIT_TEXT_MAXSZ ||
404173362Sbenjsc	    idatasz > WPI_FW_INIT_DATA_MAXSZ ||
405173362Sbenjsc	    btextsz > WPI_FW_BOOT_TEXT_MAXSZ ||
406173362Sbenjsc	    (btextsz & 3) != 0) {
407173362Sbenjsc		device_printf(sc->sc_dev, "firmware invalid\n");
408173362Sbenjsc		error = EINVAL;
409173362Sbenjsc		goto fail;
410173362Sbenjsc	}
411173362Sbenjsc
412173362Sbenjsc	/* copy initialization images into pre-allocated DMA-safe memory */
413173362Sbenjsc	memcpy(dma->vaddr, idata, idatasz);
414173362Sbenjsc	memcpy(dma->vaddr + WPI_FW_INIT_DATA_MAXSZ, itext, itextsz);
415173362Sbenjsc
416173362Sbenjsc	bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
417173362Sbenjsc
418173362Sbenjsc	/* tell adapter where to find initialization images */
419173362Sbenjsc	wpi_mem_lock(sc);
420173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
421173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, idatasz);
422173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
423173362Sbenjsc	    dma->paddr + WPI_FW_INIT_DATA_MAXSZ);
424173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, itextsz);
425173362Sbenjsc	wpi_mem_unlock(sc);
426173362Sbenjsc
427173362Sbenjsc	/* load firmware boot code */
428173362Sbenjsc	if ((error = wpi_load_microcode(sc, btext, btextsz)) != 0) {
429173362Sbenjsc	    device_printf(sc->sc_dev, "Failed to load microcode\n");
430173362Sbenjsc	    goto fail;
431173362Sbenjsc	}
432173362Sbenjsc
433173362Sbenjsc	/* now press "execute" */
434173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, 0);
435173362Sbenjsc
436173362Sbenjsc	/* wait at most one second for the first alive notification */
437173362Sbenjsc	if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
438173362Sbenjsc		device_printf(sc->sc_dev,
439173362Sbenjsc		    "timeout waiting for adapter to initialize\n");
440173362Sbenjsc		goto fail;
441173362Sbenjsc	}
442173362Sbenjsc
443173362Sbenjsc	/* copy runtime images into pre-allocated DMA-sage memory */
444173362Sbenjsc	memcpy(dma->vaddr, rdata, rdatasz);
445173362Sbenjsc	memcpy(dma->vaddr + WPI_FW_MAIN_DATA_MAXSZ, rtext, rtextsz);
446173362Sbenjsc	bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
447173362Sbenjsc
448173362Sbenjsc	/* tell adapter where to find runtime images */
449173362Sbenjsc	wpi_mem_lock(sc);
450173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
451173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, rdatasz);
452173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
453173362Sbenjsc	    dma->paddr + WPI_FW_MAIN_DATA_MAXSZ);
454173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, WPI_FW_UPDATED | rtextsz);
455173362Sbenjsc	wpi_mem_unlock(sc);
456173362Sbenjsc
457173362Sbenjsc	/* wait at most one second for the first alive notification */
458173362Sbenjsc	if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
459173362Sbenjsc		device_printf(sc->sc_dev,
460173362Sbenjsc		    "timeout waiting for adapter to initialize2\n");
461173362Sbenjsc		goto fail;
462173362Sbenjsc	}
463173362Sbenjsc
464173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
465173362Sbenjsc	    ("Firmware loaded to driver successfully\n"));
466173362Sbenjsc	return error;
467173362Sbenjscfail:
468173362Sbenjsc	wpi_unload_firmware(sc);
469173362Sbenjsc	return error;
470173362Sbenjsc}
471173362Sbenjsc
472173362Sbenjsc/**
473173362Sbenjsc * Free the referenced firmware image
474173362Sbenjsc */
475173362Sbenjscstatic void
476173362Sbenjscwpi_unload_firmware(struct wpi_softc *sc)
477173362Sbenjsc{
478173362Sbenjsc
479173362Sbenjsc	if (sc->fw_fp) {
480173362Sbenjsc		WPI_UNLOCK(sc);
481173362Sbenjsc		firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
482173362Sbenjsc		WPI_LOCK(sc);
483173362Sbenjsc		sc->fw_fp = NULL;
484173362Sbenjsc	}
485173362Sbenjsc}
486173362Sbenjsc
487173362Sbenjscstatic int
488173362Sbenjscwpi_attach(device_t dev)
489173362Sbenjsc{
490173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
491173362Sbenjsc	struct ifnet *ifp;
492178354Ssam	struct ieee80211com *ic;
493173362Sbenjsc	int ac, error, supportsa = 1;
494173362Sbenjsc	uint32_t tmp;
495173362Sbenjsc	const struct wpi_ident *ident;
496190526Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
497173362Sbenjsc
498173362Sbenjsc	sc->sc_dev = dev;
499173362Sbenjsc
500179957Sthompsa	if (bootverbose || WPI_DEBUG_SET)
501173362Sbenjsc	    device_printf(sc->sc_dev,"Driver Revision %s\n", VERSION);
502173362Sbenjsc
503173362Sbenjsc	/*
504173362Sbenjsc	 * Some card's only support 802.11b/g not a, check to see if
505173362Sbenjsc	 * this is one such card. A 0x0 in the subdevice table indicates
506173362Sbenjsc	 * the entire subdevice range is to be ignored.
507173362Sbenjsc	 */
508173362Sbenjsc	for (ident = wpi_ident_table; ident->name != NULL; ident++) {
509173362Sbenjsc		if (ident->subdevice &&
510173362Sbenjsc		    pci_get_subdevice(dev) == ident->subdevice) {
511173362Sbenjsc		    supportsa = 0;
512173362Sbenjsc		    break;
513173362Sbenjsc		}
514173362Sbenjsc	}
515173362Sbenjsc
516173362Sbenjsc	/* Create the tasks that can be queued */
517191746Sthompsa	TASK_INIT(&sc->sc_restarttask, 0, wpi_hwreset, sc);
518191746Sthompsa	TASK_INIT(&sc->sc_radiotask, 0, wpi_rfreset, sc);
519173362Sbenjsc
520173362Sbenjsc	WPI_LOCK_INIT(sc);
521173362Sbenjsc
522173362Sbenjsc	callout_init_mtx(&sc->calib_to, &sc->sc_mtx, 0);
523173362Sbenjsc	callout_init_mtx(&sc->watchdog_to, &sc->sc_mtx, 0);
524173362Sbenjsc
525173362Sbenjsc	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
526173362Sbenjsc		device_printf(dev, "chip is in D%d power mode "
527173362Sbenjsc		    "-- setting to D0\n", pci_get_powerstate(dev));
528173362Sbenjsc		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
529173362Sbenjsc	}
530173362Sbenjsc
531173362Sbenjsc	/* disable the retry timeout register */
532173362Sbenjsc	pci_write_config(dev, 0x41, 0, 1);
533173362Sbenjsc
534173362Sbenjsc	/* enable bus-mastering */
535173362Sbenjsc	pci_enable_busmaster(dev);
536173362Sbenjsc
537173362Sbenjsc	sc->mem_rid = PCIR_BAR(0);
538173362Sbenjsc	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
539173362Sbenjsc	    RF_ACTIVE);
540173362Sbenjsc	if (sc->mem == NULL) {
541173362Sbenjsc		device_printf(dev, "could not allocate memory resource\n");
542173362Sbenjsc		error = ENOMEM;
543173362Sbenjsc		goto fail;
544173362Sbenjsc	}
545173362Sbenjsc
546173362Sbenjsc	sc->sc_st = rman_get_bustag(sc->mem);
547173362Sbenjsc	sc->sc_sh = rman_get_bushandle(sc->mem);
548173362Sbenjsc
549173362Sbenjsc	sc->irq_rid = 0;
550173362Sbenjsc	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
551173362Sbenjsc	    RF_ACTIVE | RF_SHAREABLE);
552173362Sbenjsc	if (sc->irq == NULL) {
553173362Sbenjsc		device_printf(dev, "could not allocate interrupt resource\n");
554173362Sbenjsc		error = ENOMEM;
555173362Sbenjsc		goto fail;
556173362Sbenjsc	}
557173362Sbenjsc
558173362Sbenjsc	/*
559173362Sbenjsc	 * Allocate DMA memory for firmware transfers.
560173362Sbenjsc	 */
561173362Sbenjsc	if ((error = wpi_alloc_fwmem(sc)) != 0) {
562173362Sbenjsc		printf(": could not allocate firmware memory\n");
563173362Sbenjsc		error = ENOMEM;
564173362Sbenjsc		goto fail;
565173362Sbenjsc	}
566173362Sbenjsc
567173362Sbenjsc	/*
568173362Sbenjsc	 * Put adapter into a known state.
569173362Sbenjsc	 */
570173362Sbenjsc	if ((error = wpi_reset(sc)) != 0) {
571173362Sbenjsc		device_printf(dev, "could not reset adapter\n");
572173362Sbenjsc		goto fail;
573173362Sbenjsc	}
574173362Sbenjsc
575173362Sbenjsc	wpi_mem_lock(sc);
576173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
577179957Sthompsa	if (bootverbose || WPI_DEBUG_SET)
578173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Revision (0x%X)\n", tmp);
579173362Sbenjsc
580173362Sbenjsc	wpi_mem_unlock(sc);
581173362Sbenjsc
582173362Sbenjsc	/* Allocate shared page */
583173362Sbenjsc	if ((error = wpi_alloc_shared(sc)) != 0) {
584173362Sbenjsc		device_printf(dev, "could not allocate shared page\n");
585173362Sbenjsc		goto fail;
586173362Sbenjsc	}
587173362Sbenjsc
588173362Sbenjsc	/* tx data queues  - 4 for QoS purposes */
589173362Sbenjsc	for (ac = 0; ac < WME_NUM_AC; ac++) {
590173362Sbenjsc		error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, ac);
591173362Sbenjsc		if (error != 0) {
592173362Sbenjsc		    device_printf(dev, "could not allocate Tx ring %d\n",ac);
593173362Sbenjsc		    goto fail;
594173362Sbenjsc		}
595173362Sbenjsc	}
596173362Sbenjsc
597173362Sbenjsc	/* command queue to talk to the card's firmware */
598173362Sbenjsc	error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4);
599173362Sbenjsc	if (error != 0) {
600173362Sbenjsc		device_printf(dev, "could not allocate command ring\n");
601173362Sbenjsc		goto fail;
602173362Sbenjsc	}
603173362Sbenjsc
604173362Sbenjsc	/* receive data queue */
605173362Sbenjsc	error = wpi_alloc_rx_ring(sc, &sc->rxq);
606173362Sbenjsc	if (error != 0) {
607173362Sbenjsc		device_printf(dev, "could not allocate Rx ring\n");
608173362Sbenjsc		goto fail;
609173362Sbenjsc	}
610173362Sbenjsc
611178354Ssam	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
612173362Sbenjsc	if (ifp == NULL) {
613173362Sbenjsc		device_printf(dev, "can not if_alloc()\n");
614173362Sbenjsc		error = ENOMEM;
615173362Sbenjsc		goto fail;
616173362Sbenjsc	}
617178354Ssam	ic = ifp->if_l2com;
618173362Sbenjsc
619173362Sbenjsc	ic->ic_ifp = ifp;
620178354Ssam	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
621178354Ssam	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
622173362Sbenjsc
623173362Sbenjsc	/* set device capabilities */
624173362Sbenjsc	ic->ic_caps =
625178957Ssam		  IEEE80211_C_STA		/* station mode supported */
626178957Ssam		| IEEE80211_C_MONITOR		/* monitor mode supported */
627173362Sbenjsc		| IEEE80211_C_TXPMGT		/* tx power management */
628173362Sbenjsc		| IEEE80211_C_SHSLOT		/* short slot time supported */
629173362Sbenjsc		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
630173362Sbenjsc		| IEEE80211_C_WPA		/* 802.11i */
631173362Sbenjsc/* XXX looks like WME is partly supported? */
632173362Sbenjsc#if 0
633173362Sbenjsc		| IEEE80211_C_IBSS		/* IBSS mode support */
634173362Sbenjsc		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
635173362Sbenjsc		| IEEE80211_C_WME		/* 802.11e */
636173362Sbenjsc		| IEEE80211_C_HOSTAP		/* Host access point mode */
637173362Sbenjsc#endif
638173362Sbenjsc		;
639173362Sbenjsc
640173362Sbenjsc	/*
641173362Sbenjsc	 * Read in the eeprom and also setup the channels for
642173362Sbenjsc	 * net80211. We don't set the rates as net80211 does this for us
643173362Sbenjsc	 */
644190526Ssam	wpi_read_eeprom(sc, macaddr);
645173362Sbenjsc
646179957Sthompsa	if (bootverbose || WPI_DEBUG_SET) {
647173362Sbenjsc	    device_printf(sc->sc_dev, "Regulatory Domain: %.4s\n", sc->domain);
648173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Type: %c\n",
649173362Sbenjsc			  sc->type > 1 ? 'B': '?');
650173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Revision: %c\n",
651173362Sbenjsc			  ((le16toh(sc->rev) & 0xf0) == 0xd0) ? 'D': '?');
652173362Sbenjsc	    device_printf(sc->sc_dev, "SKU %s support 802.11a\n",
653173362Sbenjsc			  supportsa ? "does" : "does not");
654173362Sbenjsc
655173362Sbenjsc	    /* XXX hw_config uses the PCIDEV for the Hardware rev. Must check
656173362Sbenjsc	       what sc->rev really represents - benjsc 20070615 */
657173362Sbenjsc	}
658173362Sbenjsc
659173362Sbenjsc	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
660173362Sbenjsc	ifp->if_softc = sc;
661173362Sbenjsc	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
662173362Sbenjsc	ifp->if_init = wpi_init;
663173362Sbenjsc	ifp->if_ioctl = wpi_ioctl;
664173362Sbenjsc	ifp->if_start = wpi_start;
665173362Sbenjsc	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
666173362Sbenjsc	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
667173362Sbenjsc	IFQ_SET_READY(&ifp->if_snd);
668178354Ssam
669190526Ssam	ieee80211_ifattach(ic, macaddr);
670173362Sbenjsc	/* override default methods */
671173362Sbenjsc	ic->ic_node_alloc = wpi_node_alloc;
672173362Sbenjsc	ic->ic_newassoc = wpi_newassoc;
673178354Ssam	ic->ic_raw_xmit = wpi_raw_xmit;
674173362Sbenjsc	ic->ic_wme.wme_update = wpi_wme_update;
675173362Sbenjsc	ic->ic_scan_start = wpi_scan_start;
676173362Sbenjsc	ic->ic_scan_end = wpi_scan_end;
677173362Sbenjsc	ic->ic_set_channel = wpi_set_channel;
678173362Sbenjsc	ic->ic_scan_curchan = wpi_scan_curchan;
679173362Sbenjsc	ic->ic_scan_mindwell = wpi_scan_mindwell;
680173362Sbenjsc
681178354Ssam	ic->ic_vap_create = wpi_vap_create;
682178354Ssam	ic->ic_vap_delete = wpi_vap_delete;
683173362Sbenjsc
684192468Ssam	ieee80211_radiotap_attach(ic,
685192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
686192468Ssam		WPI_TX_RADIOTAP_PRESENT,
687192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
688192468Ssam		WPI_RX_RADIOTAP_PRESENT);
689173362Sbenjsc
690173362Sbenjsc	/*
691173362Sbenjsc	 * Hook our interrupt after all initialization is complete.
692173362Sbenjsc	 */
693177043Sthompsa	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET |INTR_MPSAFE,
694177043Sthompsa	    NULL, wpi_intr, sc, &sc->sc_ih);
695173362Sbenjsc	if (error != 0) {
696173362Sbenjsc		device_printf(dev, "could not set up interrupt\n");
697173362Sbenjsc		goto fail;
698173362Sbenjsc	}
699173362Sbenjsc
700177043Sthompsa	if (bootverbose)
701177043Sthompsa		ieee80211_announce(ic);
702173362Sbenjsc#ifdef XXX_DEBUG
703173362Sbenjsc	ieee80211_announce_channels(ic);
704173362Sbenjsc#endif
705173362Sbenjsc	return 0;
706173362Sbenjsc
707173362Sbenjscfail:	wpi_detach(dev);
708173362Sbenjsc	return ENXIO;
709173362Sbenjsc}
710173362Sbenjsc
711173362Sbenjscstatic int
712173362Sbenjscwpi_detach(device_t dev)
713173362Sbenjsc{
714173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
715178354Ssam	struct ifnet *ifp = sc->sc_ifp;
716200530Sgavin	struct ieee80211com *ic;
717173362Sbenjsc	int ac;
718173362Sbenjsc
719200530Sgavin	if (ifp != NULL) {
720200530Sgavin		ic = ifp->if_l2com;
721191746Sthompsa
722200530Sgavin		ieee80211_draintask(ic, &sc->sc_restarttask);
723200530Sgavin		ieee80211_draintask(ic, &sc->sc_radiotask);
724173362Sbenjsc		wpi_stop(sc);
725173362Sbenjsc		callout_drain(&sc->watchdog_to);
726173362Sbenjsc		callout_drain(&sc->calib_to);
727173362Sbenjsc		ieee80211_ifdetach(ic);
728173362Sbenjsc	}
729173362Sbenjsc
730173362Sbenjsc	WPI_LOCK(sc);
731173362Sbenjsc	if (sc->txq[0].data_dmat) {
732173362Sbenjsc		for (ac = 0; ac < WME_NUM_AC; ac++)
733173362Sbenjsc			wpi_free_tx_ring(sc, &sc->txq[ac]);
734173362Sbenjsc
735173362Sbenjsc		wpi_free_tx_ring(sc, &sc->cmdq);
736173362Sbenjsc		wpi_free_rx_ring(sc, &sc->rxq);
737173362Sbenjsc		wpi_free_shared(sc);
738173362Sbenjsc	}
739173362Sbenjsc
740173362Sbenjsc	if (sc->fw_fp != NULL) {
741173362Sbenjsc		wpi_unload_firmware(sc);
742173362Sbenjsc	}
743173362Sbenjsc
744173362Sbenjsc	if (sc->fw_dma.tag)
745173362Sbenjsc		wpi_free_fwmem(sc);
746173362Sbenjsc	WPI_UNLOCK(sc);
747173362Sbenjsc
748173362Sbenjsc	if (sc->irq != NULL) {
749173362Sbenjsc		bus_teardown_intr(dev, sc->irq, sc->sc_ih);
750173362Sbenjsc		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
751173362Sbenjsc	}
752173362Sbenjsc
753173362Sbenjsc	if (sc->mem != NULL)
754173362Sbenjsc		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
755173362Sbenjsc
756173362Sbenjsc	if (ifp != NULL)
757173362Sbenjsc		if_free(ifp);
758173362Sbenjsc
759173362Sbenjsc	WPI_LOCK_DESTROY(sc);
760173362Sbenjsc
761173362Sbenjsc	return 0;
762173362Sbenjsc}
763173362Sbenjsc
764178354Ssamstatic struct ieee80211vap *
765178354Ssamwpi_vap_create(struct ieee80211com *ic,
766178354Ssam	const char name[IFNAMSIZ], int unit, int opmode, int flags,
767178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN],
768178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN])
769178354Ssam{
770178354Ssam	struct wpi_vap *wvp;
771178354Ssam	struct ieee80211vap *vap;
772178354Ssam
773178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
774178354Ssam		return NULL;
775178354Ssam	wvp = (struct wpi_vap *) malloc(sizeof(struct wpi_vap),
776178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
777178354Ssam	if (wvp == NULL)
778178354Ssam		return NULL;
779178354Ssam	vap = &wvp->vap;
780178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
781178354Ssam	/* override with driver methods */
782178354Ssam	wvp->newstate = vap->iv_newstate;
783178354Ssam	vap->iv_newstate = wpi_newstate;
784178354Ssam
785178354Ssam	ieee80211_amrr_init(&wvp->amrr, vap,
786178354Ssam	    IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
787178354Ssam	    IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
788178354Ssam	    500 /*ms*/);
789178354Ssam
790178354Ssam	/* complete setup */
791178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
792178354Ssam	ic->ic_opmode = opmode;
793178354Ssam	return vap;
794178354Ssam}
795178354Ssam
796173362Sbenjscstatic void
797178354Ssamwpi_vap_delete(struct ieee80211vap *vap)
798178354Ssam{
799178354Ssam	struct wpi_vap *wvp = WPI_VAP(vap);
800178354Ssam
801178354Ssam	ieee80211_amrr_cleanup(&wvp->amrr);
802178354Ssam	ieee80211_vap_detach(vap);
803178354Ssam	free(wvp, M_80211_VAP);
804178354Ssam}
805178354Ssam
806178354Ssamstatic void
807173362Sbenjscwpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
808173362Sbenjsc{
809173362Sbenjsc	if (error != 0)
810173362Sbenjsc		return;
811173362Sbenjsc
812173362Sbenjsc	KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
813173362Sbenjsc
814173362Sbenjsc	*(bus_addr_t *)arg = segs[0].ds_addr;
815173362Sbenjsc}
816173362Sbenjsc
817177043Sthompsa/*
818177043Sthompsa * Allocates a contiguous block of dma memory of the requested size and
819177043Sthompsa * alignment. Due to limitations of the FreeBSD dma subsystem as of 20071217,
820177043Sthompsa * allocations greater than 4096 may fail. Hence if the requested alignment is
821177043Sthompsa * greater we allocate 'alignment' size extra memory and shift the vaddr and
822177043Sthompsa * paddr after the dma load. This bypasses the problem at the cost of a little
823177043Sthompsa * more memory.
824177043Sthompsa */
825173362Sbenjscstatic int
826173362Sbenjscwpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
827173362Sbenjsc    void **kvap, bus_size_t size, bus_size_t alignment, int flags)
828173362Sbenjsc{
829173362Sbenjsc	int error;
830177043Sthompsa	bus_size_t align;
831177043Sthompsa	bus_size_t reqsize;
832173362Sbenjsc
833173362Sbenjsc	DPRINTFN(WPI_DEBUG_DMA,
834177043Sthompsa	    ("Size: %zd - alignment %zd\n", size, alignment));
835173362Sbenjsc
836173362Sbenjsc	dma->size = size;
837173362Sbenjsc	dma->tag = NULL;
838173362Sbenjsc
839177043Sthompsa	if (alignment > 4096) {
840177043Sthompsa		align = PAGE_SIZE;
841177043Sthompsa		reqsize = size + alignment;
842177043Sthompsa	} else {
843177043Sthompsa		align = alignment;
844177043Sthompsa		reqsize = size;
845177043Sthompsa	}
846177043Sthompsa	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), align,
847173362Sbenjsc	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
848177043Sthompsa	    NULL, NULL, reqsize,
849177043Sthompsa	    1, reqsize, flags,
850173362Sbenjsc	    NULL, NULL, &dma->tag);
851173362Sbenjsc	if (error != 0) {
852173362Sbenjsc		device_printf(sc->sc_dev,
853173362Sbenjsc		    "could not create shared page DMA tag\n");
854173362Sbenjsc		goto fail;
855173362Sbenjsc	}
856177043Sthompsa	error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr_start,
857173362Sbenjsc	    flags | BUS_DMA_ZERO, &dma->map);
858173362Sbenjsc	if (error != 0) {
859173362Sbenjsc		device_printf(sc->sc_dev,
860173362Sbenjsc		    "could not allocate shared page DMA memory\n");
861173362Sbenjsc		goto fail;
862173362Sbenjsc	}
863173362Sbenjsc
864177043Sthompsa	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr_start,
865177043Sthompsa	    reqsize,  wpi_dma_map_addr, &dma->paddr_start, flags);
866177043Sthompsa
867177043Sthompsa	/* Save the original pointers so we can free all the memory */
868177043Sthompsa	dma->paddr = dma->paddr_start;
869177043Sthompsa	dma->vaddr = dma->vaddr_start;
870177043Sthompsa
871177043Sthompsa	/*
872177043Sthompsa	 * Check the alignment and increment by 4096 until we get the
873177043Sthompsa	 * requested alignment. Fail if can't obtain the alignment
874177043Sthompsa	 * we requested.
875173362Sbenjsc	 */
876177043Sthompsa	if ((dma->paddr & (alignment -1 )) != 0) {
877177043Sthompsa		int i;
878173362Sbenjsc
879177043Sthompsa		for (i = 0; i < alignment / 4096; i++) {
880177043Sthompsa			if ((dma->paddr & (alignment - 1 )) == 0)
881177043Sthompsa				break;
882177043Sthompsa			dma->paddr += 4096;
883177043Sthompsa			dma->vaddr += 4096;
884177043Sthompsa		}
885177043Sthompsa		if (i == alignment / 4096) {
886177043Sthompsa			device_printf(sc->sc_dev,
887177043Sthompsa			    "alignment requirement was not satisfied\n");
888177043Sthompsa			goto fail;
889177043Sthompsa		}
890173362Sbenjsc	}
891173362Sbenjsc
892173362Sbenjsc	if (error != 0) {
893173362Sbenjsc		device_printf(sc->sc_dev,
894173362Sbenjsc		    "could not load shared page DMA map\n");
895173362Sbenjsc		goto fail;
896173362Sbenjsc	}
897173362Sbenjsc
898173362Sbenjsc	if (kvap != NULL)
899173362Sbenjsc		*kvap = dma->vaddr;
900173362Sbenjsc
901173362Sbenjsc	return 0;
902173362Sbenjsc
903173362Sbenjscfail:
904173362Sbenjsc	wpi_dma_contig_free(dma);
905173362Sbenjsc	return error;
906173362Sbenjsc}
907173362Sbenjsc
908173362Sbenjscstatic void
909173362Sbenjscwpi_dma_contig_free(struct wpi_dma_info *dma)
910173362Sbenjsc{
911173362Sbenjsc	if (dma->tag) {
912173362Sbenjsc		if (dma->map != NULL) {
913177043Sthompsa			if (dma->paddr_start != 0) {
914173362Sbenjsc				bus_dmamap_sync(dma->tag, dma->map,
915173362Sbenjsc				    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
916173362Sbenjsc				bus_dmamap_unload(dma->tag, dma->map);
917173362Sbenjsc			}
918177043Sthompsa			bus_dmamem_free(dma->tag, &dma->vaddr_start, dma->map);
919173362Sbenjsc		}
920173362Sbenjsc		bus_dma_tag_destroy(dma->tag);
921173362Sbenjsc	}
922173362Sbenjsc}
923173362Sbenjsc
924173362Sbenjsc/*
925173362Sbenjsc * Allocate a shared page between host and NIC.
926173362Sbenjsc */
927173362Sbenjscstatic int
928173362Sbenjscwpi_alloc_shared(struct wpi_softc *sc)
929173362Sbenjsc{
930173362Sbenjsc	int error;
931173362Sbenjsc
932173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &sc->shared_dma,
933173362Sbenjsc	    (void **)&sc->shared, sizeof (struct wpi_shared),
934173362Sbenjsc	    PAGE_SIZE,
935173362Sbenjsc	    BUS_DMA_NOWAIT);
936173362Sbenjsc
937173362Sbenjsc	if (error != 0) {
938173362Sbenjsc		device_printf(sc->sc_dev,
939173362Sbenjsc		    "could not allocate shared area DMA memory\n");
940173362Sbenjsc	}
941173362Sbenjsc
942173362Sbenjsc	return error;
943173362Sbenjsc}
944173362Sbenjsc
945173362Sbenjscstatic void
946173362Sbenjscwpi_free_shared(struct wpi_softc *sc)
947173362Sbenjsc{
948173362Sbenjsc	wpi_dma_contig_free(&sc->shared_dma);
949173362Sbenjsc}
950173362Sbenjsc
951173362Sbenjscstatic int
952173362Sbenjscwpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
953173362Sbenjsc{
954173362Sbenjsc
955173362Sbenjsc	int i, error;
956173362Sbenjsc
957173362Sbenjsc	ring->cur = 0;
958173362Sbenjsc
959173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
960177043Sthompsa	    (void **)&ring->desc, WPI_RX_RING_COUNT * sizeof (uint32_t),
961177043Sthompsa	    WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
962173362Sbenjsc
963173362Sbenjsc	if (error != 0) {
964177043Sthompsa		device_printf(sc->sc_dev,
965177043Sthompsa		    "%s: could not allocate rx ring DMA memory, error %d\n",
966177043Sthompsa		    __func__, error);
967177043Sthompsa		goto fail;
968173362Sbenjsc	}
969173362Sbenjsc
970177043Sthompsa        error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
971177043Sthompsa	    BUS_SPACE_MAXADDR_32BIT,
972177043Sthompsa            BUS_SPACE_MAXADDR, NULL, NULL, MJUMPAGESIZE, 1,
973177043Sthompsa            MJUMPAGESIZE, BUS_DMA_NOWAIT, NULL, NULL, &ring->data_dmat);
974177043Sthompsa        if (error != 0) {
975177043Sthompsa                device_printf(sc->sc_dev,
976177043Sthompsa		    "%s: bus_dma_tag_create_failed, error %d\n",
977177043Sthompsa		    __func__, error);
978177043Sthompsa                goto fail;
979177043Sthompsa        }
980177043Sthompsa
981173362Sbenjsc	/*
982177043Sthompsa	 * Setup Rx buffers.
983173362Sbenjsc	 */
984173362Sbenjsc	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
985177043Sthompsa		struct wpi_rx_data *data = &ring->data[i];
986177043Sthompsa		struct mbuf *m;
987177043Sthompsa		bus_addr_t paddr;
988173362Sbenjsc
989177043Sthompsa		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
990177043Sthompsa		if (error != 0) {
991173362Sbenjsc			device_printf(sc->sc_dev,
992177043Sthompsa			    "%s: bus_dmamap_create failed, error %d\n",
993177043Sthompsa			    __func__, error);
994173362Sbenjsc			goto fail;
995173362Sbenjsc		}
996177043Sthompsa		m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
997177043Sthompsa		if (m == NULL) {
998173362Sbenjsc			device_printf(sc->sc_dev,
999177043Sthompsa			   "%s: could not allocate rx mbuf\n", __func__);
1000177043Sthompsa			error = ENOMEM;
1001173362Sbenjsc			goto fail;
1002173362Sbenjsc		}
1003177043Sthompsa		/* map page */
1004177043Sthompsa		error = bus_dmamap_load(ring->data_dmat, data->map,
1005177043Sthompsa		    mtod(m, caddr_t), MJUMPAGESIZE,
1006177043Sthompsa		    wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1007177043Sthompsa		if (error != 0 && error != EFBIG) {
1008177043Sthompsa			device_printf(sc->sc_dev,
1009177043Sthompsa			    "%s: bus_dmamap_load failed, error %d\n",
1010177043Sthompsa			    __func__, error);
1011177043Sthompsa			m_freem(m);
1012177043Sthompsa			error = ENOMEM;	/* XXX unique code */
1013173362Sbenjsc			goto fail;
1014173362Sbenjsc		}
1015177043Sthompsa		bus_dmamap_sync(ring->data_dmat, data->map,
1016177043Sthompsa		    BUS_DMASYNC_PREWRITE);
1017177043Sthompsa
1018177043Sthompsa		data->m = m;
1019177043Sthompsa		ring->desc[i] = htole32(paddr);
1020173362Sbenjsc	}
1021173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1022173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
1023173362Sbenjsc	return 0;
1024173362Sbenjscfail:
1025173362Sbenjsc	wpi_free_rx_ring(sc, ring);
1026173362Sbenjsc	return error;
1027173362Sbenjsc}
1028173362Sbenjsc
1029173362Sbenjscstatic void
1030173362Sbenjscwpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
1031173362Sbenjsc{
1032173362Sbenjsc	int ntries;
1033173362Sbenjsc
1034173362Sbenjsc	wpi_mem_lock(sc);
1035173362Sbenjsc
1036173362Sbenjsc	WPI_WRITE(sc, WPI_RX_CONFIG, 0);
1037173362Sbenjsc
1038173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1039173362Sbenjsc		if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE)
1040173362Sbenjsc			break;
1041173362Sbenjsc		DELAY(10);
1042173362Sbenjsc	}
1043173362Sbenjsc
1044173362Sbenjsc	wpi_mem_unlock(sc);
1045173362Sbenjsc
1046173362Sbenjsc#ifdef WPI_DEBUG
1047179957Sthompsa	if (ntries == 100 && wpi_debug > 0)
1048173362Sbenjsc		device_printf(sc->sc_dev, "timeout resetting Rx ring\n");
1049173362Sbenjsc#endif
1050173362Sbenjsc
1051173362Sbenjsc	ring->cur = 0;
1052173362Sbenjsc}
1053173362Sbenjsc
1054173362Sbenjscstatic void
1055173362Sbenjscwpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
1056173362Sbenjsc{
1057173362Sbenjsc	int i;
1058173362Sbenjsc
1059173362Sbenjsc	wpi_dma_contig_free(&ring->desc_dma);
1060173362Sbenjsc
1061177043Sthompsa	for (i = 0; i < WPI_RX_RING_COUNT; i++)
1062177043Sthompsa		if (ring->data[i].m != NULL)
1063173362Sbenjsc			m_freem(ring->data[i].m);
1064173362Sbenjsc}
1065173362Sbenjsc
1066173362Sbenjscstatic int
1067173362Sbenjscwpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count,
1068173362Sbenjsc	int qid)
1069173362Sbenjsc{
1070173362Sbenjsc	struct wpi_tx_data *data;
1071173362Sbenjsc	int i, error;
1072173362Sbenjsc
1073173362Sbenjsc	ring->qid = qid;
1074173362Sbenjsc	ring->count = count;
1075173362Sbenjsc	ring->queued = 0;
1076173362Sbenjsc	ring->cur = 0;
1077173362Sbenjsc	ring->data = NULL;
1078173362Sbenjsc
1079173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
1080173362Sbenjsc		(void **)&ring->desc, count * sizeof (struct wpi_tx_desc),
1081173362Sbenjsc		WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
1082173362Sbenjsc
1083173362Sbenjsc	if (error != 0) {
1084173362Sbenjsc	    device_printf(sc->sc_dev, "could not allocate tx dma memory\n");
1085173362Sbenjsc	    goto fail;
1086173362Sbenjsc	}
1087173362Sbenjsc
1088173362Sbenjsc	/* update shared page with ring's base address */
1089173362Sbenjsc	sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
1090173362Sbenjsc
1091173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
1092173362Sbenjsc		count * sizeof (struct wpi_tx_cmd), WPI_RING_DMA_ALIGN,
1093173362Sbenjsc		BUS_DMA_NOWAIT);
1094173362Sbenjsc
1095173362Sbenjsc	if (error != 0) {
1096173362Sbenjsc		device_printf(sc->sc_dev,
1097173362Sbenjsc		    "could not allocate tx command DMA memory\n");
1098173362Sbenjsc		goto fail;
1099173362Sbenjsc	}
1100173362Sbenjsc
1101173362Sbenjsc	ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF,
1102173362Sbenjsc	    M_NOWAIT | M_ZERO);
1103173362Sbenjsc	if (ring->data == NULL) {
1104173362Sbenjsc		device_printf(sc->sc_dev,
1105173362Sbenjsc		    "could not allocate tx data slots\n");
1106173362Sbenjsc		goto fail;
1107173362Sbenjsc	}
1108173362Sbenjsc
1109173362Sbenjsc	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1110173362Sbenjsc	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
1111173362Sbenjsc	    WPI_MAX_SCATTER - 1, MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL,
1112173362Sbenjsc	    &ring->data_dmat);
1113173362Sbenjsc	if (error != 0) {
1114173362Sbenjsc		device_printf(sc->sc_dev, "could not create data DMA tag\n");
1115173362Sbenjsc		goto fail;
1116173362Sbenjsc	}
1117173362Sbenjsc
1118173362Sbenjsc	for (i = 0; i < count; i++) {
1119173362Sbenjsc		data = &ring->data[i];
1120173362Sbenjsc
1121173362Sbenjsc		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1122173362Sbenjsc		if (error != 0) {
1123173362Sbenjsc			device_printf(sc->sc_dev,
1124173362Sbenjsc			    "could not create tx buf DMA map\n");
1125173362Sbenjsc			goto fail;
1126173362Sbenjsc		}
1127173362Sbenjsc		bus_dmamap_sync(ring->data_dmat, data->map,
1128173362Sbenjsc		    BUS_DMASYNC_PREWRITE);
1129173362Sbenjsc	}
1130173362Sbenjsc
1131173362Sbenjsc	return 0;
1132173362Sbenjsc
1133177043Sthompsafail:
1134177043Sthompsa	wpi_free_tx_ring(sc, ring);
1135173362Sbenjsc	return error;
1136173362Sbenjsc}
1137173362Sbenjsc
1138173362Sbenjscstatic void
1139173362Sbenjscwpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1140173362Sbenjsc{
1141173362Sbenjsc	struct wpi_tx_data *data;
1142173362Sbenjsc	int i, ntries;
1143173362Sbenjsc
1144173362Sbenjsc	wpi_mem_lock(sc);
1145173362Sbenjsc
1146173362Sbenjsc	WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0);
1147173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1148173362Sbenjsc		if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid))
1149173362Sbenjsc			break;
1150173362Sbenjsc		DELAY(10);
1151173362Sbenjsc	}
1152173362Sbenjsc#ifdef WPI_DEBUG
1153179957Sthompsa	if (ntries == 100 && wpi_debug > 0)
1154173362Sbenjsc		device_printf(sc->sc_dev, "timeout resetting Tx ring %d\n",
1155173362Sbenjsc		    ring->qid);
1156173362Sbenjsc#endif
1157173362Sbenjsc	wpi_mem_unlock(sc);
1158173362Sbenjsc
1159173362Sbenjsc	for (i = 0; i < ring->count; i++) {
1160173362Sbenjsc		data = &ring->data[i];
1161173362Sbenjsc
1162173362Sbenjsc		if (data->m != NULL) {
1163173362Sbenjsc			bus_dmamap_unload(ring->data_dmat, data->map);
1164173362Sbenjsc			m_freem(data->m);
1165173362Sbenjsc			data->m = NULL;
1166173362Sbenjsc		}
1167173362Sbenjsc	}
1168173362Sbenjsc
1169173362Sbenjsc	ring->queued = 0;
1170173362Sbenjsc	ring->cur = 0;
1171173362Sbenjsc}
1172173362Sbenjsc
1173173362Sbenjscstatic void
1174173362Sbenjscwpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1175173362Sbenjsc{
1176173362Sbenjsc	struct wpi_tx_data *data;
1177173362Sbenjsc	int i;
1178173362Sbenjsc
1179173362Sbenjsc	wpi_dma_contig_free(&ring->desc_dma);
1180173362Sbenjsc	wpi_dma_contig_free(&ring->cmd_dma);
1181173362Sbenjsc
1182173362Sbenjsc	if (ring->data != NULL) {
1183173362Sbenjsc		for (i = 0; i < ring->count; i++) {
1184173362Sbenjsc			data = &ring->data[i];
1185173362Sbenjsc
1186173362Sbenjsc			if (data->m != NULL) {
1187173362Sbenjsc				bus_dmamap_sync(ring->data_dmat, data->map,
1188173362Sbenjsc				    BUS_DMASYNC_POSTWRITE);
1189173362Sbenjsc				bus_dmamap_unload(ring->data_dmat, data->map);
1190173362Sbenjsc				m_freem(data->m);
1191173362Sbenjsc				data->m = NULL;
1192173362Sbenjsc			}
1193173362Sbenjsc		}
1194173362Sbenjsc		free(ring->data, M_DEVBUF);
1195173362Sbenjsc	}
1196173362Sbenjsc
1197173362Sbenjsc	if (ring->data_dmat != NULL)
1198173362Sbenjsc		bus_dma_tag_destroy(ring->data_dmat);
1199173362Sbenjsc}
1200173362Sbenjsc
1201173362Sbenjscstatic int
1202173362Sbenjscwpi_shutdown(device_t dev)
1203173362Sbenjsc{
1204173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1205173362Sbenjsc
1206173362Sbenjsc	WPI_LOCK(sc);
1207173362Sbenjsc	wpi_stop_locked(sc);
1208173362Sbenjsc	wpi_unload_firmware(sc);
1209173362Sbenjsc	WPI_UNLOCK(sc);
1210173362Sbenjsc
1211173362Sbenjsc	return 0;
1212173362Sbenjsc}
1213173362Sbenjsc
1214173362Sbenjscstatic int
1215173362Sbenjscwpi_suspend(device_t dev)
1216173362Sbenjsc{
1217173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1218173362Sbenjsc
1219173362Sbenjsc	wpi_stop(sc);
1220173362Sbenjsc	return 0;
1221173362Sbenjsc}
1222173362Sbenjsc
1223173362Sbenjscstatic int
1224173362Sbenjscwpi_resume(device_t dev)
1225173362Sbenjsc{
1226173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1227178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1228173362Sbenjsc
1229173362Sbenjsc	pci_write_config(dev, 0x41, 0, 1);
1230173362Sbenjsc
1231173362Sbenjsc	if (ifp->if_flags & IFF_UP) {
1232173362Sbenjsc		wpi_init(ifp->if_softc);
1233173362Sbenjsc		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1234173362Sbenjsc			wpi_start(ifp);
1235173362Sbenjsc	}
1236173362Sbenjsc	return 0;
1237173362Sbenjsc}
1238173362Sbenjsc
1239173362Sbenjsc/* ARGSUSED */
1240173362Sbenjscstatic struct ieee80211_node *
1241179643Ssamwpi_node_alloc(struct ieee80211vap *vap __unused,
1242179643Ssam	const uint8_t mac[IEEE80211_ADDR_LEN] __unused)
1243173362Sbenjsc{
1244173362Sbenjsc	struct wpi_node *wn;
1245173362Sbenjsc
1246178354Ssam	wn = malloc(sizeof (struct wpi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
1247173362Sbenjsc
1248173362Sbenjsc	return &wn->ni;
1249173362Sbenjsc}
1250173362Sbenjsc
1251173362Sbenjsc/**
1252173362Sbenjsc * Called by net80211 when ever there is a change to 80211 state machine
1253173362Sbenjsc */
1254173362Sbenjscstatic int
1255178354Ssamwpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1256173362Sbenjsc{
1257178354Ssam	struct wpi_vap *wvp = WPI_VAP(vap);
1258178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1259173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
1260173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
1261178354Ssam	int error;
1262173362Sbenjsc
1263178354Ssam	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
1264178354Ssam		ieee80211_state_name[vap->iv_state],
1265178354Ssam		ieee80211_state_name[nstate], sc->flags));
1266173362Sbenjsc
1267191746Sthompsa	IEEE80211_UNLOCK(ic);
1268191746Sthompsa	WPI_LOCK(sc);
1269178354Ssam	if (nstate == IEEE80211_S_AUTH) {
1270191746Sthompsa		/* The node must be registered in the firmware before auth */
1271191746Sthompsa		error = wpi_auth(sc, vap);
1272191746Sthompsa		if (error != 0) {
1273191746Sthompsa			device_printf(sc->sc_dev,
1274191746Sthompsa			    "%s: could not move to auth state, error %d\n",
1275191746Sthompsa			    __func__, error);
1276191746Sthompsa		}
1277173362Sbenjsc	}
1278178354Ssam	if (nstate == IEEE80211_S_RUN && vap->iv_state != IEEE80211_S_RUN) {
1279191746Sthompsa		error = wpi_run(sc, vap);
1280191746Sthompsa		if (error != 0) {
1281191746Sthompsa			device_printf(sc->sc_dev,
1282191746Sthompsa			    "%s: could not move to run state, error %d\n",
1283191746Sthompsa			    __func__, error);
1284191746Sthompsa		}
1285178354Ssam	}
1286178354Ssam	if (nstate == IEEE80211_S_RUN) {
1287178354Ssam		/* RUN -> RUN transition; just restart the timers */
1288178354Ssam		wpi_calib_timeout(sc);
1289178354Ssam		/* XXX split out rate control timer */
1290178354Ssam	}
1291191746Sthompsa	WPI_UNLOCK(sc);
1292191746Sthompsa	IEEE80211_LOCK(ic);
1293178354Ssam	return wvp->newstate(vap, nstate, arg);
1294173362Sbenjsc}
1295173362Sbenjsc
1296173362Sbenjsc/*
1297173362Sbenjsc * Grab exclusive access to NIC memory.
1298173362Sbenjsc */
1299173362Sbenjscstatic void
1300173362Sbenjscwpi_mem_lock(struct wpi_softc *sc)
1301173362Sbenjsc{
1302173362Sbenjsc	int ntries;
1303173362Sbenjsc	uint32_t tmp;
1304173362Sbenjsc
1305173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
1306173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC);
1307173362Sbenjsc
1308173362Sbenjsc	/* spin until we actually get the lock */
1309173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1310173362Sbenjsc		if ((WPI_READ(sc, WPI_GPIO_CTL) &
1311173362Sbenjsc			(WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK)
1312173362Sbenjsc			break;
1313173362Sbenjsc		DELAY(10);
1314173362Sbenjsc	}
1315173362Sbenjsc	if (ntries == 100)
1316173362Sbenjsc		device_printf(sc->sc_dev, "could not lock memory\n");
1317173362Sbenjsc}
1318173362Sbenjsc
1319173362Sbenjsc/*
1320173362Sbenjsc * Release lock on NIC memory.
1321173362Sbenjsc */
1322173362Sbenjscstatic void
1323173362Sbenjscwpi_mem_unlock(struct wpi_softc *sc)
1324173362Sbenjsc{
1325173362Sbenjsc	uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL);
1326173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC);
1327173362Sbenjsc}
1328173362Sbenjsc
1329173362Sbenjscstatic uint32_t
1330173362Sbenjscwpi_mem_read(struct wpi_softc *sc, uint16_t addr)
1331173362Sbenjsc{
1332173362Sbenjsc	WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr);
1333173362Sbenjsc	return WPI_READ(sc, WPI_READ_MEM_DATA);
1334173362Sbenjsc}
1335173362Sbenjsc
1336173362Sbenjscstatic void
1337173362Sbenjscwpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data)
1338173362Sbenjsc{
1339173362Sbenjsc	WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr);
1340173362Sbenjsc	WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data);
1341173362Sbenjsc}
1342173362Sbenjsc
1343173362Sbenjscstatic void
1344173362Sbenjscwpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr,
1345173362Sbenjsc    const uint32_t *data, int wlen)
1346173362Sbenjsc{
1347173362Sbenjsc	for (; wlen > 0; wlen--, data++, addr+=4)
1348173362Sbenjsc		wpi_mem_write(sc, addr, *data);
1349173362Sbenjsc}
1350173362Sbenjsc
1351173362Sbenjsc/*
1352173362Sbenjsc * Read data from the EEPROM.  We access EEPROM through the MAC instead of
1353173362Sbenjsc * using the traditional bit-bang method. Data is read up until len bytes have
1354173362Sbenjsc * been obtained.
1355173362Sbenjsc */
1356173362Sbenjscstatic uint16_t
1357173362Sbenjscwpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int len)
1358173362Sbenjsc{
1359173362Sbenjsc	int ntries;
1360173362Sbenjsc	uint32_t val;
1361173362Sbenjsc	uint8_t *out = data;
1362173362Sbenjsc
1363173362Sbenjsc	wpi_mem_lock(sc);
1364173362Sbenjsc
1365173362Sbenjsc	for (; len > 0; len -= 2, addr++) {
1366173362Sbenjsc		WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2);
1367173362Sbenjsc
1368173362Sbenjsc		for (ntries = 0; ntries < 10; ntries++) {
1369173362Sbenjsc			if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) & WPI_EEPROM_READY)
1370173362Sbenjsc				break;
1371173362Sbenjsc			DELAY(5);
1372173362Sbenjsc		}
1373173362Sbenjsc
1374173362Sbenjsc		if (ntries == 10) {
1375173362Sbenjsc			device_printf(sc->sc_dev, "could not read EEPROM\n");
1376173362Sbenjsc			return ETIMEDOUT;
1377173362Sbenjsc		}
1378173362Sbenjsc
1379173362Sbenjsc		*out++= val >> 16;
1380173362Sbenjsc		if (len > 1)
1381173362Sbenjsc			*out ++= val >> 24;
1382173362Sbenjsc	}
1383173362Sbenjsc
1384173362Sbenjsc	wpi_mem_unlock(sc);
1385173362Sbenjsc
1386173362Sbenjsc	return 0;
1387173362Sbenjsc}
1388173362Sbenjsc
1389173362Sbenjsc/*
1390173362Sbenjsc * The firmware text and data segments are transferred to the NIC using DMA.
1391173362Sbenjsc * The driver just copies the firmware into DMA-safe memory and tells the NIC
1392173362Sbenjsc * where to find it.  Once the NIC has copied the firmware into its internal
1393173362Sbenjsc * memory, we can free our local copy in the driver.
1394173362Sbenjsc */
1395173362Sbenjscstatic int
1396173362Sbenjscwpi_load_microcode(struct wpi_softc *sc, const uint8_t *fw, int size)
1397173362Sbenjsc{
1398173362Sbenjsc	int error, ntries;
1399173362Sbenjsc
1400173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,("Loading microcode  size 0x%x\n", size));
1401173362Sbenjsc
1402173362Sbenjsc	size /= sizeof(uint32_t);
1403173362Sbenjsc
1404173362Sbenjsc	wpi_mem_lock(sc);
1405173362Sbenjsc
1406173362Sbenjsc	wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE,
1407173362Sbenjsc	    (const uint32_t *)fw, size);
1408173362Sbenjsc
1409173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0);
1410173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT);
1411173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size);
1412173362Sbenjsc
1413173362Sbenjsc	/* run microcode */
1414173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN);
1415173362Sbenjsc
1416173362Sbenjsc	/* wait while the adapter is busy copying the firmware */
1417173362Sbenjsc	for (error = 0, ntries = 0; ntries < 1000; ntries++) {
1418173362Sbenjsc		uint32_t status = WPI_READ(sc, WPI_TX_STATUS);
1419173362Sbenjsc		DPRINTFN(WPI_DEBUG_HW,
1420173362Sbenjsc		    ("firmware status=0x%x, val=0x%x, result=0x%x\n", status,
1421173362Sbenjsc		     WPI_TX_IDLE(6), status & WPI_TX_IDLE(6)));
1422173362Sbenjsc		if (status & WPI_TX_IDLE(6)) {
1423173362Sbenjsc			DPRINTFN(WPI_DEBUG_HW,
1424173362Sbenjsc			    ("Status Match! - ntries = %d\n", ntries));
1425173362Sbenjsc			break;
1426173362Sbenjsc		}
1427173362Sbenjsc		DELAY(10);
1428173362Sbenjsc	}
1429173362Sbenjsc	if (ntries == 1000) {
1430173362Sbenjsc		device_printf(sc->sc_dev, "timeout transferring firmware\n");
1431173362Sbenjsc		error = ETIMEDOUT;
1432173362Sbenjsc	}
1433173362Sbenjsc
1434173362Sbenjsc	/* start the microcode executing */
1435173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_ENABLE);
1436173362Sbenjsc
1437173362Sbenjsc	wpi_mem_unlock(sc);
1438173362Sbenjsc
1439173362Sbenjsc	return (error);
1440173362Sbenjsc}
1441173362Sbenjsc
1442173362Sbenjscstatic void
1443173362Sbenjscwpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1444173362Sbenjsc	struct wpi_rx_data *data)
1445173362Sbenjsc{
1446178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1447178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1448173362Sbenjsc	struct wpi_rx_ring *ring = &sc->rxq;
1449173362Sbenjsc	struct wpi_rx_stat *stat;
1450173362Sbenjsc	struct wpi_rx_head *head;
1451173362Sbenjsc	struct wpi_rx_tail *tail;
1452173362Sbenjsc	struct ieee80211_node *ni;
1453173362Sbenjsc	struct mbuf *m, *mnew;
1454177043Sthompsa	bus_addr_t paddr;
1455177043Sthompsa	int error;
1456173362Sbenjsc
1457173362Sbenjsc	stat = (struct wpi_rx_stat *)(desc + 1);
1458173362Sbenjsc
1459173362Sbenjsc	if (stat->len > WPI_STAT_MAXLEN) {
1460173362Sbenjsc		device_printf(sc->sc_dev, "invalid rx statistic header\n");
1461173362Sbenjsc		ifp->if_ierrors++;
1462173362Sbenjsc		return;
1463173362Sbenjsc	}
1464173362Sbenjsc
1465173362Sbenjsc	head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1466173362Sbenjsc	tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + le16toh(head->len));
1467173362Sbenjsc
1468173362Sbenjsc	DPRINTFN(WPI_DEBUG_RX, ("rx intr: idx=%d len=%d stat len=%d rssi=%d "
1469173362Sbenjsc	    "rate=%x chan=%d tstamp=%ju\n", ring->cur, le32toh(desc->len),
1470173362Sbenjsc	    le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan,
1471173362Sbenjsc	    (uintmax_t)le64toh(tail->tstamp)));
1472173362Sbenjsc
1473190458Sjmallett	/* discard Rx frames with bad CRC early */
1474190458Sjmallett	if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1475190458Sjmallett		DPRINTFN(WPI_DEBUG_RX, ("%s: rx flags error %x\n", __func__,
1476190458Sjmallett		    le32toh(tail->flags)));
1477190458Sjmallett		ifp->if_ierrors++;
1478190458Sjmallett		return;
1479190458Sjmallett	}
1480190458Sjmallett	if (le16toh(head->len) < sizeof (struct ieee80211_frame)) {
1481190458Sjmallett		DPRINTFN(WPI_DEBUG_RX, ("%s: frame too short: %d\n", __func__,
1482190458Sjmallett		    le16toh(head->len)));
1483190458Sjmallett		ifp->if_ierrors++;
1484190458Sjmallett		return;
1485190458Sjmallett	}
1486190458Sjmallett
1487177043Sthompsa	/* XXX don't need mbuf, just dma buffer */
1488177043Sthompsa	mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1489177043Sthompsa	if (mnew == NULL) {
1490177043Sthompsa		DPRINTFN(WPI_DEBUG_RX, ("%s: no mbuf to restock ring\n",
1491177043Sthompsa		    __func__));
1492177043Sthompsa		ifp->if_ierrors++;
1493177043Sthompsa		return;
1494177043Sthompsa	}
1495177043Sthompsa	error = bus_dmamap_load(ring->data_dmat, data->map,
1496177043Sthompsa	    mtod(mnew, caddr_t), MJUMPAGESIZE,
1497177043Sthompsa	    wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1498177043Sthompsa	if (error != 0 && error != EFBIG) {
1499177043Sthompsa		device_printf(sc->sc_dev,
1500177043Sthompsa		    "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1501177043Sthompsa		m_freem(mnew);
1502177043Sthompsa		ifp->if_ierrors++;
1503177043Sthompsa		return;
1504177043Sthompsa	}
1505177043Sthompsa	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1506177043Sthompsa
1507177043Sthompsa	/* finalize mbuf and swap in new one */
1508173362Sbenjsc	m = data->m;
1509173362Sbenjsc	m->m_pkthdr.rcvif = ifp;
1510173362Sbenjsc	m->m_data = (caddr_t)(head + 1);
1511173362Sbenjsc	m->m_pkthdr.len = m->m_len = le16toh(head->len);
1512173362Sbenjsc
1513177043Sthompsa	data->m = mnew;
1514177043Sthompsa	/* update Rx descriptor */
1515177043Sthompsa	ring->desc[ring->cur] = htole32(paddr);
1516173362Sbenjsc
1517192468Ssam	if (ieee80211_radiotap_active(ic)) {
1518173362Sbenjsc		struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
1519173362Sbenjsc
1520173362Sbenjsc		tap->wr_flags = 0;
1521173362Sbenjsc		tap->wr_chan_freq =
1522173362Sbenjsc			htole16(ic->ic_channels[head->chan].ic_freq);
1523173362Sbenjsc		tap->wr_chan_flags =
1524173362Sbenjsc			htole16(ic->ic_channels[head->chan].ic_flags);
1525173362Sbenjsc		tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
1526173362Sbenjsc		tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise);
1527173362Sbenjsc		tap->wr_tsft = tail->tstamp;
1528173362Sbenjsc		tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
1529173362Sbenjsc		switch (head->rate) {
1530173362Sbenjsc		/* CCK rates */
1531173362Sbenjsc		case  10: tap->wr_rate =   2; break;
1532173362Sbenjsc		case  20: tap->wr_rate =   4; break;
1533173362Sbenjsc		case  55: tap->wr_rate =  11; break;
1534173362Sbenjsc		case 110: tap->wr_rate =  22; break;
1535173362Sbenjsc		/* OFDM rates */
1536173362Sbenjsc		case 0xd: tap->wr_rate =  12; break;
1537173362Sbenjsc		case 0xf: tap->wr_rate =  18; break;
1538173362Sbenjsc		case 0x5: tap->wr_rate =  24; break;
1539173362Sbenjsc		case 0x7: tap->wr_rate =  36; break;
1540173362Sbenjsc		case 0x9: tap->wr_rate =  48; break;
1541173362Sbenjsc		case 0xb: tap->wr_rate =  72; break;
1542173362Sbenjsc		case 0x1: tap->wr_rate =  96; break;
1543173362Sbenjsc		case 0x3: tap->wr_rate = 108; break;
1544173362Sbenjsc		/* unknown rate: should not happen */
1545173362Sbenjsc		default:  tap->wr_rate =   0;
1546173362Sbenjsc		}
1547173362Sbenjsc		if (le16toh(head->flags) & 0x4)
1548173362Sbenjsc			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1549173362Sbenjsc	}
1550173362Sbenjsc
1551173362Sbenjsc	WPI_UNLOCK(sc);
1552173362Sbenjsc
1553177043Sthompsa	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1554178354Ssam	if (ni != NULL) {
1555192468Ssam		(void) ieee80211_input(ni, m, stat->rssi, 0);
1556178354Ssam		ieee80211_free_node(ni);
1557178354Ssam	} else
1558192468Ssam		(void) ieee80211_input_all(ic, m, stat->rssi, 0);
1559173362Sbenjsc
1560173362Sbenjsc	WPI_LOCK(sc);
1561173362Sbenjsc}
1562173362Sbenjsc
1563173362Sbenjscstatic void
1564173362Sbenjscwpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1565173362Sbenjsc{
1566178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1567173362Sbenjsc	struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
1568173362Sbenjsc	struct wpi_tx_data *txdata = &ring->data[desc->idx];
1569173362Sbenjsc	struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
1570173362Sbenjsc	struct wpi_node *wn = (struct wpi_node *)txdata->ni;
1571173362Sbenjsc
1572173362Sbenjsc	DPRINTFN(WPI_DEBUG_TX, ("tx done: qid=%d idx=%d retries=%d nkill=%d "
1573173362Sbenjsc	    "rate=%x duration=%d status=%x\n", desc->qid, desc->idx,
1574173362Sbenjsc	    stat->ntries, stat->nkill, stat->rate, le32toh(stat->duration),
1575173362Sbenjsc	    le32toh(stat->status)));
1576173362Sbenjsc
1577173362Sbenjsc	/*
1578173362Sbenjsc	 * Update rate control statistics for the node.
1579173362Sbenjsc	 * XXX we should not count mgmt frames since they're always sent at
1580173362Sbenjsc	 * the lowest available bit-rate.
1581173362Sbenjsc	 * XXX frames w/o ACK shouldn't be used either
1582173362Sbenjsc	 */
1583173362Sbenjsc	wn->amn.amn_txcnt++;
1584173362Sbenjsc	if (stat->ntries > 0) {
1585190462Sjmallett		DPRINTFN(WPI_DEBUG_TX, ("%d retries\n", stat->ntries));
1586173362Sbenjsc		wn->amn.amn_retrycnt++;
1587173362Sbenjsc	}
1588173362Sbenjsc
1589173362Sbenjsc	/* XXX oerrors should only count errors !maxtries */
1590173362Sbenjsc	if ((le32toh(stat->status) & 0xff) != 1)
1591173362Sbenjsc		ifp->if_oerrors++;
1592173362Sbenjsc	else
1593173362Sbenjsc		ifp->if_opackets++;
1594173362Sbenjsc
1595173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, txdata->map, BUS_DMASYNC_POSTWRITE);
1596173362Sbenjsc	bus_dmamap_unload(ring->data_dmat, txdata->map);
1597173362Sbenjsc	/* XXX handle M_TXCB? */
1598173362Sbenjsc	m_freem(txdata->m);
1599173362Sbenjsc	txdata->m = NULL;
1600173362Sbenjsc	ieee80211_free_node(txdata->ni);
1601173362Sbenjsc	txdata->ni = NULL;
1602173362Sbenjsc
1603173362Sbenjsc	ring->queued--;
1604173362Sbenjsc
1605173362Sbenjsc	sc->sc_tx_timer = 0;
1606173362Sbenjsc	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1607178354Ssam	wpi_start_locked(ifp);
1608173362Sbenjsc}
1609173362Sbenjsc
1610173362Sbenjscstatic void
1611173362Sbenjscwpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1612173362Sbenjsc{
1613173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
1614173362Sbenjsc	struct wpi_tx_data *data;
1615173362Sbenjsc
1616173362Sbenjsc	DPRINTFN(WPI_DEBUG_CMD, ("cmd notification qid=%x idx=%d flags=%x "
1617173362Sbenjsc				 "type=%s len=%d\n", desc->qid, desc->idx,
1618173362Sbenjsc				 desc->flags, wpi_cmd_str(desc->type),
1619173362Sbenjsc				 le32toh(desc->len)));
1620173362Sbenjsc
1621173362Sbenjsc	if ((desc->qid & 7) != 4)
1622173362Sbenjsc		return;	/* not a command ack */
1623173362Sbenjsc
1624173362Sbenjsc	data = &ring->data[desc->idx];
1625173362Sbenjsc
1626173362Sbenjsc	/* if the command was mapped in a mbuf, free it */
1627173362Sbenjsc	if (data->m != NULL) {
1628173362Sbenjsc		bus_dmamap_unload(ring->data_dmat, data->map);
1629173362Sbenjsc		m_freem(data->m);
1630173362Sbenjsc		data->m = NULL;
1631173362Sbenjsc	}
1632173362Sbenjsc
1633173362Sbenjsc	sc->flags &= ~WPI_FLAG_BUSY;
1634173362Sbenjsc	wakeup(&ring->cmd[desc->idx]);
1635173362Sbenjsc}
1636173362Sbenjsc
1637173362Sbenjscstatic void
1638173362Sbenjscwpi_notif_intr(struct wpi_softc *sc)
1639173362Sbenjsc{
1640178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1641178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1642173362Sbenjsc	struct wpi_rx_desc *desc;
1643173362Sbenjsc	struct wpi_rx_data *data;
1644173362Sbenjsc	uint32_t hw;
1645173362Sbenjsc
1646173362Sbenjsc	hw = le32toh(sc->shared->next);
1647173362Sbenjsc	while (sc->rxq.cur != hw) {
1648173362Sbenjsc		data = &sc->rxq.data[sc->rxq.cur];
1649173362Sbenjsc		desc = (void *)data->m->m_ext.ext_buf;
1650173362Sbenjsc
1651173362Sbenjsc		DPRINTFN(WPI_DEBUG_NOTIFY,
1652173362Sbenjsc			 ("notify qid=%x idx=%d flags=%x type=%d len=%d\n",
1653173362Sbenjsc			  desc->qid,
1654173362Sbenjsc			  desc->idx,
1655173362Sbenjsc			  desc->flags,
1656173362Sbenjsc			  desc->type,
1657173362Sbenjsc			  le32toh(desc->len)));
1658173362Sbenjsc
1659173362Sbenjsc		if (!(desc->qid & 0x80))	/* reply to a command */
1660173362Sbenjsc			wpi_cmd_intr(sc, desc);
1661173362Sbenjsc
1662173362Sbenjsc		switch (desc->type) {
1663173362Sbenjsc		case WPI_RX_DONE:
1664173362Sbenjsc			/* a 802.11 frame was received */
1665173362Sbenjsc			wpi_rx_intr(sc, desc, data);
1666173362Sbenjsc			break;
1667173362Sbenjsc
1668173362Sbenjsc		case WPI_TX_DONE:
1669173362Sbenjsc			/* a 802.11 frame has been transmitted */
1670173362Sbenjsc			wpi_tx_intr(sc, desc);
1671173362Sbenjsc			break;
1672173362Sbenjsc
1673173362Sbenjsc		case WPI_UC_READY:
1674173362Sbenjsc		{
1675173362Sbenjsc			struct wpi_ucode_info *uc =
1676173362Sbenjsc				(struct wpi_ucode_info *)(desc + 1);
1677173362Sbenjsc
1678173362Sbenjsc			/* the microcontroller is ready */
1679173362Sbenjsc			DPRINTF(("microcode alive notification version %x "
1680173362Sbenjsc				"alive %x\n", le32toh(uc->version),
1681173362Sbenjsc				le32toh(uc->valid)));
1682173362Sbenjsc
1683173362Sbenjsc			if (le32toh(uc->valid) != 1) {
1684173362Sbenjsc				device_printf(sc->sc_dev,
1685173362Sbenjsc				    "microcontroller initialization failed\n");
1686173362Sbenjsc				wpi_stop_locked(sc);
1687173362Sbenjsc			}
1688173362Sbenjsc			break;
1689173362Sbenjsc		}
1690173362Sbenjsc		case WPI_STATE_CHANGED:
1691173362Sbenjsc		{
1692173362Sbenjsc			uint32_t *status = (uint32_t *)(desc + 1);
1693173362Sbenjsc
1694173362Sbenjsc			/* enabled/disabled notification */
1695173362Sbenjsc			DPRINTF(("state changed to %x\n", le32toh(*status)));
1696173362Sbenjsc
1697173362Sbenjsc			if (le32toh(*status) & 1) {
1698173362Sbenjsc				device_printf(sc->sc_dev,
1699173362Sbenjsc				    "Radio transmitter is switched off\n");
1700173362Sbenjsc				sc->flags |= WPI_FLAG_HW_RADIO_OFF;
1701177043Sthompsa				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1702177043Sthompsa				/* Disable firmware commands */
1703177043Sthompsa				WPI_WRITE(sc, WPI_UCODE_SET, WPI_DISABLE_CMD);
1704173362Sbenjsc			}
1705173362Sbenjsc			break;
1706173362Sbenjsc		}
1707173362Sbenjsc		case WPI_START_SCAN:
1708173362Sbenjsc		{
1709179957Sthompsa#ifdef WPI_DEBUG
1710173362Sbenjsc			struct wpi_start_scan *scan =
1711173362Sbenjsc				(struct wpi_start_scan *)(desc + 1);
1712179957Sthompsa#endif
1713173362Sbenjsc
1714173362Sbenjsc			DPRINTFN(WPI_DEBUG_SCANNING,
1715173362Sbenjsc				 ("scanning channel %d status %x\n",
1716173362Sbenjsc			    scan->chan, le32toh(scan->status)));
1717173362Sbenjsc			break;
1718173362Sbenjsc		}
1719173362Sbenjsc		case WPI_STOP_SCAN:
1720173362Sbenjsc		{
1721179957Sthompsa#ifdef WPI_DEBUG
1722173362Sbenjsc			struct wpi_stop_scan *scan =
1723173362Sbenjsc				(struct wpi_stop_scan *)(desc + 1);
1724179957Sthompsa#endif
1725178354Ssam			struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1726173362Sbenjsc
1727173362Sbenjsc			DPRINTFN(WPI_DEBUG_SCANNING,
1728173362Sbenjsc			    ("scan finished nchan=%d status=%d chan=%d\n",
1729173362Sbenjsc			     scan->nchan, scan->status, scan->chan));
1730173362Sbenjsc
1731177043Sthompsa			sc->sc_scan_timer = 0;
1732178354Ssam			ieee80211_scan_next(vap);
1733173362Sbenjsc			break;
1734173362Sbenjsc		}
1735173976Sbenjsc		case WPI_MISSED_BEACON:
1736173976Sbenjsc		{
1737178354Ssam			struct wpi_missed_beacon *beacon =
1738173976Sbenjsc				(struct wpi_missed_beacon *)(desc + 1);
1739178354Ssam			struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1740173976Sbenjsc
1741178354Ssam			if (le32toh(beacon->consecutive) >=
1742178354Ssam			    vap->iv_bmissthreshold) {
1743178354Ssam				DPRINTF(("Beacon miss: %u >= %u\n",
1744178354Ssam					 le32toh(beacon->consecutive),
1745178354Ssam					 vap->iv_bmissthreshold));
1746191746Sthompsa				ieee80211_beacon_miss(ic);
1747178354Ssam			}
1748178354Ssam			break;
1749173362Sbenjsc		}
1750173976Sbenjsc		}
1751173362Sbenjsc
1752173362Sbenjsc		sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
1753173362Sbenjsc	}
1754173362Sbenjsc
1755173362Sbenjsc	/* tell the firmware what we have processed */
1756173362Sbenjsc	hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
1757173362Sbenjsc	WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7);
1758173362Sbenjsc}
1759173362Sbenjsc
1760173362Sbenjscstatic void
1761173362Sbenjscwpi_intr(void *arg)
1762173362Sbenjsc{
1763173362Sbenjsc	struct wpi_softc *sc = arg;
1764173362Sbenjsc	uint32_t r;
1765173362Sbenjsc
1766173362Sbenjsc	WPI_LOCK(sc);
1767173362Sbenjsc
1768173362Sbenjsc	r = WPI_READ(sc, WPI_INTR);
1769173362Sbenjsc	if (r == 0 || r == 0xffffffff) {
1770173362Sbenjsc		WPI_UNLOCK(sc);
1771173362Sbenjsc		return;
1772173362Sbenjsc	}
1773173362Sbenjsc
1774173362Sbenjsc	/* disable interrupts */
1775173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, 0);
1776173362Sbenjsc	/* ack interrupts */
1777173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, r);
1778173362Sbenjsc
1779173362Sbenjsc	if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) {
1780191746Sthompsa		struct ifnet *ifp = sc->sc_ifp;
1781191746Sthompsa		struct ieee80211com *ic = ifp->if_l2com;
1782191956Sthompsa		struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1783191746Sthompsa
1784173362Sbenjsc		device_printf(sc->sc_dev, "fatal firmware error\n");
1785173362Sbenjsc		DPRINTFN(6,("(%s)\n", (r & WPI_SW_ERROR) ? "(Software Error)" :
1786173362Sbenjsc				"(Hardware Error)"));
1787191956Sthompsa		if (vap != NULL)
1788191956Sthompsa			ieee80211_cancel_scan(vap);
1789191746Sthompsa		ieee80211_runtask(ic, &sc->sc_restarttask);
1790173362Sbenjsc		sc->flags &= ~WPI_FLAG_BUSY;
1791173362Sbenjsc		WPI_UNLOCK(sc);
1792173362Sbenjsc		return;
1793173362Sbenjsc	}
1794173362Sbenjsc
1795173362Sbenjsc	if (r & WPI_RX_INTR)
1796173362Sbenjsc		wpi_notif_intr(sc);
1797173362Sbenjsc
1798173362Sbenjsc	if (r & WPI_ALIVE_INTR)	/* firmware initialized */
1799173362Sbenjsc		wakeup(sc);
1800173362Sbenjsc
1801173362Sbenjsc	/* re-enable interrupts */
1802173362Sbenjsc	if (sc->sc_ifp->if_flags & IFF_UP)
1803173362Sbenjsc		WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
1804173362Sbenjsc
1805173362Sbenjsc	WPI_UNLOCK(sc);
1806173362Sbenjsc}
1807173362Sbenjsc
1808173362Sbenjscstatic uint8_t
1809173362Sbenjscwpi_plcp_signal(int rate)
1810173362Sbenjsc{
1811173362Sbenjsc	switch (rate) {
1812173362Sbenjsc	/* CCK rates (returned values are device-dependent) */
1813173362Sbenjsc	case 2:		return 10;
1814173362Sbenjsc	case 4:		return 20;
1815173362Sbenjsc	case 11:	return 55;
1816173362Sbenjsc	case 22:	return 110;
1817173362Sbenjsc
1818173362Sbenjsc	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1819173362Sbenjsc	/* R1-R4 (ral/ural is R4-R1) */
1820173362Sbenjsc	case 12:	return 0xd;
1821173362Sbenjsc	case 18:	return 0xf;
1822173362Sbenjsc	case 24:	return 0x5;
1823173362Sbenjsc	case 36:	return 0x7;
1824173362Sbenjsc	case 48:	return 0x9;
1825173362Sbenjsc	case 72:	return 0xb;
1826173362Sbenjsc	case 96:	return 0x1;
1827173362Sbenjsc	case 108:	return 0x3;
1828173362Sbenjsc
1829173362Sbenjsc	/* unsupported rates (should not get there) */
1830173362Sbenjsc	default:	return 0;
1831173362Sbenjsc	}
1832173362Sbenjsc}
1833173362Sbenjsc
1834173362Sbenjsc/* quickly determine if a given rate is CCK or OFDM */
1835173362Sbenjsc#define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1836173362Sbenjsc
1837173362Sbenjsc/*
1838173362Sbenjsc * Construct the data packet for a transmit buffer and acutally put
1839173362Sbenjsc * the buffer onto the transmit ring, kicking the card to process the
1840173362Sbenjsc * the buffer.
1841173362Sbenjsc */
1842173362Sbenjscstatic int
1843173362Sbenjscwpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1844173362Sbenjsc	int ac)
1845173362Sbenjsc{
1846178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1847178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1848178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1849177043Sthompsa	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1850173362Sbenjsc	struct wpi_tx_ring *ring = &sc->txq[ac];
1851173362Sbenjsc	struct wpi_tx_desc *desc;
1852173362Sbenjsc	struct wpi_tx_data *data;
1853173362Sbenjsc	struct wpi_tx_cmd *cmd;
1854173362Sbenjsc	struct wpi_cmd_data *tx;
1855173362Sbenjsc	struct ieee80211_frame *wh;
1856178354Ssam	const struct ieee80211_txparam *tp;
1857173362Sbenjsc	struct ieee80211_key *k;
1858173362Sbenjsc	struct mbuf *mnew;
1859177043Sthompsa	int i, error, nsegs, rate, hdrlen, ismcast;
1860173362Sbenjsc	bus_dma_segment_t segs[WPI_MAX_SCATTER];
1861173362Sbenjsc
1862173362Sbenjsc	desc = &ring->desc[ring->cur];
1863173362Sbenjsc	data = &ring->data[ring->cur];
1864173362Sbenjsc
1865173362Sbenjsc	wh = mtod(m0, struct ieee80211_frame *);
1866173362Sbenjsc
1867177043Sthompsa	hdrlen = ieee80211_hdrsize(wh);
1868177043Sthompsa	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1869173362Sbenjsc
1870173362Sbenjsc	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1871178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1872177043Sthompsa		if (k == NULL) {
1873173362Sbenjsc			m_freem(m0);
1874173362Sbenjsc			return ENOBUFS;
1875173362Sbenjsc		}
1876173362Sbenjsc		/* packet header may have moved, reset our local pointer */
1877173362Sbenjsc		wh = mtod(m0, struct ieee80211_frame *);
1878173362Sbenjsc	}
1879173362Sbenjsc
1880173362Sbenjsc	cmd = &ring->cmd[ring->cur];
1881173362Sbenjsc	cmd->code = WPI_CMD_TX_DATA;
1882173362Sbenjsc	cmd->flags = 0;
1883173362Sbenjsc	cmd->qid = ring->qid;
1884173362Sbenjsc	cmd->idx = ring->cur;
1885173362Sbenjsc
1886173362Sbenjsc	tx = (struct wpi_cmd_data *)cmd->data;
1887177043Sthompsa	tx->flags = htole32(WPI_TX_AUTO_SEQ);
1888178354Ssam	tx->timeout = htole16(0);
1889177043Sthompsa	tx->ofdm_mask = 0xff;
1890177043Sthompsa	tx->cck_mask = 0x0f;
1891177043Sthompsa	tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
1892177043Sthompsa	tx->id = ismcast ? WPI_ID_BROADCAST : WPI_ID_BSS;
1893177043Sthompsa	tx->len = htole16(m0->m_pkthdr.len);
1894173362Sbenjsc
1895177119Ssam	if (!ismcast) {
1896177043Sthompsa		if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0 ||
1897177043Sthompsa		    !cap->cap_wmeParams[ac].wmep_noackPolicy)
1898177043Sthompsa			tx->flags |= htole32(WPI_TX_NEED_ACK);
1899178354Ssam		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
1900177043Sthompsa			tx->flags |= htole32(WPI_TX_NEED_RTS|WPI_TX_FULL_TXOP);
1901177043Sthompsa			tx->rts_ntries = 7;
1902177043Sthompsa		}
1903173362Sbenjsc	}
1904177043Sthompsa	/* pick a rate */
1905178354Ssam	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1906178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT) {
1907173362Sbenjsc		uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1908173362Sbenjsc		/* tell h/w to set timestamp in probe responses */
1909173362Sbenjsc		if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1910177043Sthompsa			tx->flags |= htole32(WPI_TX_INSERT_TSTAMP);
1911173362Sbenjsc		if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
1912177043Sthompsa		    subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
1913173362Sbenjsc			tx->timeout = htole16(3);
1914173362Sbenjsc		else
1915173362Sbenjsc			tx->timeout = htole16(2);
1916178354Ssam		rate = tp->mgmtrate;
1917177043Sthompsa	} else if (ismcast) {
1918178354Ssam		rate = tp->mcastrate;
1919178354Ssam	} else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1920178354Ssam		rate = tp->ucastrate;
1921177043Sthompsa	} else {
1922178354Ssam		(void) ieee80211_amrr_choose(ni, &WPI_NODE(ni)->amn);
1923178354Ssam		rate = ni->ni_txrate;
1924177043Sthompsa	}
1925173362Sbenjsc	tx->rate = wpi_plcp_signal(rate);
1926173362Sbenjsc
1927173362Sbenjsc	/* be very persistant at sending frames out */
1928178354Ssam#if 0
1929178354Ssam	tx->data_ntries = tp->maxretry;
1930178354Ssam#else
1931178354Ssam	tx->data_ntries = 15;		/* XXX way too high */
1932178354Ssam#endif
1933173362Sbenjsc
1934192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1935177043Sthompsa		struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
1936177043Sthompsa		tap->wt_flags = 0;
1937177043Sthompsa		tap->wt_rate = rate;
1938177043Sthompsa		tap->wt_hwqueue = ac;
1939177043Sthompsa		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1940177043Sthompsa			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1941178354Ssam
1942192468Ssam		ieee80211_radiotap_tx(vap, m0);
1943177043Sthompsa	}
1944173362Sbenjsc
1945173362Sbenjsc	/* save and trim IEEE802.11 header */
1946173362Sbenjsc	m_copydata(m0, 0, hdrlen, (caddr_t)&tx->wh);
1947173362Sbenjsc	m_adj(m0, hdrlen);
1948173362Sbenjsc
1949173362Sbenjsc	error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m0, segs,
1950173362Sbenjsc	    &nsegs, BUS_DMA_NOWAIT);
1951173362Sbenjsc	if (error != 0 && error != EFBIG) {
1952173362Sbenjsc		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1953173362Sbenjsc		    error);
1954173362Sbenjsc		m_freem(m0);
1955173362Sbenjsc		return error;
1956173362Sbenjsc	}
1957173362Sbenjsc	if (error != 0) {
1958175418Sjhb		/* XXX use m_collapse */
1959173362Sbenjsc		mnew = m_defrag(m0, M_DONTWAIT);
1960173362Sbenjsc		if (mnew == NULL) {
1961173362Sbenjsc			device_printf(sc->sc_dev,
1962173362Sbenjsc			    "could not defragment mbuf\n");
1963173362Sbenjsc			m_freem(m0);
1964173362Sbenjsc			return ENOBUFS;
1965173362Sbenjsc		}
1966173362Sbenjsc		m0 = mnew;
1967173362Sbenjsc
1968173362Sbenjsc		error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
1969173362Sbenjsc		    m0, segs, &nsegs, BUS_DMA_NOWAIT);
1970173362Sbenjsc		if (error != 0) {
1971173362Sbenjsc			device_printf(sc->sc_dev,
1972173362Sbenjsc			    "could not map mbuf (error %d)\n", error);
1973173362Sbenjsc			m_freem(m0);
1974173362Sbenjsc			return error;
1975173362Sbenjsc		}
1976173362Sbenjsc	}
1977173362Sbenjsc
1978173362Sbenjsc	data->m = m0;
1979173362Sbenjsc	data->ni = ni;
1980173362Sbenjsc
1981173362Sbenjsc	DPRINTFN(WPI_DEBUG_TX, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
1982173362Sbenjsc	    ring->qid, ring->cur, m0->m_pkthdr.len, nsegs));
1983173362Sbenjsc
1984173362Sbenjsc	/* first scatter/gather segment is used by the tx data command */
1985173362Sbenjsc	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 |
1986173362Sbenjsc	    (1 + nsegs) << 24);
1987173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
1988173362Sbenjsc	    ring->cur * sizeof (struct wpi_tx_cmd));
1989173362Sbenjsc	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_data));
1990173362Sbenjsc	for (i = 1; i <= nsegs; i++) {
1991173362Sbenjsc		desc->segs[i].addr = htole32(segs[i - 1].ds_addr);
1992173362Sbenjsc		desc->segs[i].len  = htole32(segs[i - 1].ds_len);
1993173362Sbenjsc	}
1994173362Sbenjsc
1995173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1996173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1997173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
1998173362Sbenjsc
1999173362Sbenjsc	ring->queued++;
2000173362Sbenjsc
2001173362Sbenjsc	/* kick ring */
2002173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
2003173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2004173362Sbenjsc
2005173362Sbenjsc	return 0;
2006173362Sbenjsc}
2007173362Sbenjsc
2008173362Sbenjsc/**
2009173362Sbenjsc * Process data waiting to be sent on the IFNET output queue
2010173362Sbenjsc */
2011173362Sbenjscstatic void
2012173362Sbenjscwpi_start(struct ifnet *ifp)
2013173362Sbenjsc{
2014173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
2015178354Ssam
2016178354Ssam	WPI_LOCK(sc);
2017178354Ssam	wpi_start_locked(ifp);
2018178354Ssam	WPI_UNLOCK(sc);
2019178354Ssam}
2020178354Ssam
2021178354Ssamstatic void
2022178354Ssamwpi_start_locked(struct ifnet *ifp)
2023178354Ssam{
2024178354Ssam	struct wpi_softc *sc = ifp->if_softc;
2025173362Sbenjsc	struct ieee80211_node *ni;
2026178354Ssam	struct mbuf *m;
2027178354Ssam	int ac;
2028173362Sbenjsc
2029178354Ssam	WPI_LOCK_ASSERT(sc);
2030178354Ssam
2031177043Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2032177043Sthompsa		return;
2033173362Sbenjsc
2034173362Sbenjsc	for (;;) {
2035190458Sjmallett		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2036178354Ssam		if (m == NULL)
2037178354Ssam			break;
2038178354Ssam		ac = M_WME_GETAC(m);
2039178354Ssam		if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
2040178354Ssam			/* there is no place left in this ring */
2041178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m);
2042178354Ssam			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2043178354Ssam			break;
2044178354Ssam		}
2045178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
2046178354Ssam		if (wpi_tx_data(sc, m, ni, ac) != 0) {
2047178354Ssam			ieee80211_free_node(ni);
2048178354Ssam			ifp->if_oerrors++;
2049178354Ssam			break;
2050178354Ssam		}
2051178354Ssam		sc->sc_tx_timer = 5;
2052178354Ssam	}
2053178354Ssam}
2054173362Sbenjsc
2055178354Ssamstatic int
2056178354Ssamwpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2057178354Ssam	const struct ieee80211_bpf_params *params)
2058178354Ssam{
2059178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2060178354Ssam	struct ifnet *ifp = ic->ic_ifp;
2061178354Ssam	struct wpi_softc *sc = ifp->if_softc;
2062173362Sbenjsc
2063178354Ssam	/* prevent management frames from being sent if we're not ready */
2064178354Ssam	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2065178354Ssam		m_freem(m);
2066178354Ssam		ieee80211_free_node(ni);
2067178354Ssam		return ENETDOWN;
2068178354Ssam	}
2069178354Ssam	WPI_LOCK(sc);
2070173362Sbenjsc
2071178354Ssam	/* management frames go into ring 0 */
2072178354Ssam	if (sc->txq[0].queued > sc->txq[0].count - 8) {
2073178354Ssam		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2074178354Ssam		m_freem(m);
2075178354Ssam		WPI_UNLOCK(sc);
2076178354Ssam		ieee80211_free_node(ni);
2077178354Ssam		return ENOBUFS;		/* XXX */
2078178354Ssam	}
2079173362Sbenjsc
2080178354Ssam	ifp->if_opackets++;
2081178354Ssam	if (wpi_tx_data(sc, m, ni, 0) != 0)
2082178354Ssam		goto bad;
2083178354Ssam	sc->sc_tx_timer = 5;
2084178354Ssam	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
2085173362Sbenjsc
2086178354Ssam	WPI_UNLOCK(sc);
2087178354Ssam	return 0;
2088178354Ssambad:
2089178354Ssam	ifp->if_oerrors++;
2090178354Ssam	WPI_UNLOCK(sc);
2091178354Ssam	ieee80211_free_node(ni);
2092178354Ssam	return EIO;		/* XXX */
2093173362Sbenjsc}
2094173362Sbenjsc
2095173362Sbenjscstatic int
2096173362Sbenjscwpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2097173362Sbenjsc{
2098173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
2099178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2100178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
2101178354Ssam	int error = 0, startall = 0;
2102173362Sbenjsc
2103173362Sbenjsc	switch (cmd) {
2104173362Sbenjsc	case SIOCSIFFLAGS:
2105178704Sthompsa		WPI_LOCK(sc);
2106173362Sbenjsc		if ((ifp->if_flags & IFF_UP)) {
2107178354Ssam			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2108177043Sthompsa				wpi_init_locked(sc, 0);
2109178354Ssam				startall = 1;
2110178354Ssam			}
2111177043Sthompsa		} else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) ||
2112177043Sthompsa			   (sc->flags & WPI_FLAG_HW_RADIO_OFF))
2113173362Sbenjsc			wpi_stop_locked(sc);
2114178704Sthompsa		WPI_UNLOCK(sc);
2115178704Sthompsa		if (startall)
2116178704Sthompsa			ieee80211_start_all(ic);
2117173362Sbenjsc		break;
2118178354Ssam	case SIOCGIFMEDIA:
2119178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2120178354Ssam		break;
2121178704Sthompsa	case SIOCGIFADDR:
2122178354Ssam		error = ether_ioctl(ifp, cmd, data);
2123178354Ssam		break;
2124178704Sthompsa	default:
2125178704Sthompsa		error = EINVAL;
2126178704Sthompsa		break;
2127173362Sbenjsc	}
2128173362Sbenjsc	return error;
2129173362Sbenjsc}
2130173362Sbenjsc
2131173362Sbenjsc/*
2132173362Sbenjsc * Extract various information from EEPROM.
2133173362Sbenjsc */
2134173362Sbenjscstatic void
2135190526Ssamwpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
2136173362Sbenjsc{
2137173362Sbenjsc	int i;
2138173362Sbenjsc
2139173362Sbenjsc	/* read the hardware capabilities, revision and SKU type */
2140173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap,1);
2141173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,2);
2142173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1);
2143173362Sbenjsc
2144173362Sbenjsc	/* read the regulatory domain */
2145173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain, 4);
2146173362Sbenjsc
2147173362Sbenjsc	/* read in the hw MAC address */
2148190526Ssam	wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr, 6);
2149173362Sbenjsc
2150173362Sbenjsc	/* read the list of authorized channels */
2151173362Sbenjsc	for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
2152173362Sbenjsc		wpi_read_eeprom_channels(sc,i);
2153173362Sbenjsc
2154173362Sbenjsc	/* read the power level calibration info for each group */
2155173362Sbenjsc	for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
2156173362Sbenjsc		wpi_read_eeprom_group(sc,i);
2157173362Sbenjsc}
2158173362Sbenjsc
2159173362Sbenjsc/*
2160173362Sbenjsc * Send a command to the firmware.
2161173362Sbenjsc */
2162173362Sbenjscstatic int
2163173362Sbenjscwpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
2164173362Sbenjsc{
2165173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2166173362Sbenjsc	struct wpi_tx_desc *desc;
2167173362Sbenjsc	struct wpi_tx_cmd *cmd;
2168173362Sbenjsc
2169173362Sbenjsc#ifdef WPI_DEBUG
2170173362Sbenjsc	if (!async) {
2171173362Sbenjsc		WPI_LOCK_ASSERT(sc);
2172173362Sbenjsc	}
2173173362Sbenjsc#endif
2174173362Sbenjsc
2175173362Sbenjsc	DPRINTFN(WPI_DEBUG_CMD,("wpi_cmd %d size %d async %d\n", code, size,
2176173362Sbenjsc		    async));
2177173362Sbenjsc
2178173362Sbenjsc	if (sc->flags & WPI_FLAG_BUSY) {
2179173362Sbenjsc		device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n",
2180173362Sbenjsc		    __func__, code);
2181173362Sbenjsc		return EAGAIN;
2182173362Sbenjsc	}
2183173362Sbenjsc	sc->flags|= WPI_FLAG_BUSY;
2184173362Sbenjsc
2185173362Sbenjsc	KASSERT(size <= sizeof cmd->data, ("command %d too large: %d bytes",
2186173362Sbenjsc	    code, size));
2187173362Sbenjsc
2188173362Sbenjsc	desc = &ring->desc[ring->cur];
2189173362Sbenjsc	cmd = &ring->cmd[ring->cur];
2190173362Sbenjsc
2191173362Sbenjsc	cmd->code = code;
2192173362Sbenjsc	cmd->flags = 0;
2193173362Sbenjsc	cmd->qid = ring->qid;
2194173362Sbenjsc	cmd->idx = ring->cur;
2195173362Sbenjsc	memcpy(cmd->data, buf, size);
2196173362Sbenjsc
2197173362Sbenjsc	desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
2198173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2199173362Sbenjsc		ring->cur * sizeof (struct wpi_tx_cmd));
2200173362Sbenjsc	desc->segs[0].len  = htole32(4 + size);
2201173362Sbenjsc
2202173362Sbenjsc	/* kick cmd ring */
2203173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2204173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2205173362Sbenjsc
2206173362Sbenjsc	if (async) {
2207173362Sbenjsc		sc->flags &= ~ WPI_FLAG_BUSY;
2208173362Sbenjsc		return 0;
2209173362Sbenjsc	}
2210173362Sbenjsc
2211173362Sbenjsc	return msleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz);
2212173362Sbenjsc}
2213173362Sbenjsc
2214173362Sbenjscstatic int
2215173362Sbenjscwpi_wme_update(struct ieee80211com *ic)
2216173362Sbenjsc{
2217173362Sbenjsc#define WPI_EXP2(v)	htole16((1 << (v)) - 1)
2218173362Sbenjsc#define WPI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
2219173362Sbenjsc	struct wpi_softc *sc = ic->ic_ifp->if_softc;
2220173362Sbenjsc	const struct wmeParams *wmep;
2221173362Sbenjsc	struct wpi_wme_setup wme;
2222173362Sbenjsc	int ac;
2223173362Sbenjsc
2224173362Sbenjsc	/* don't override default WME values if WME is not actually enabled */
2225173362Sbenjsc	if (!(ic->ic_flags & IEEE80211_F_WME))
2226173362Sbenjsc		return 0;
2227173362Sbenjsc
2228173362Sbenjsc	wme.flags = 0;
2229173362Sbenjsc	for (ac = 0; ac < WME_NUM_AC; ac++) {
2230173362Sbenjsc		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
2231173362Sbenjsc		wme.ac[ac].aifsn = wmep->wmep_aifsn;
2232173362Sbenjsc		wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin);
2233173362Sbenjsc		wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax);
2234173362Sbenjsc		wme.ac[ac].txop  = WPI_USEC(wmep->wmep_txopLimit);
2235173362Sbenjsc
2236173362Sbenjsc		DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
2237173362Sbenjsc		    "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
2238173362Sbenjsc		    wme.ac[ac].cwmax, wme.ac[ac].txop));
2239173362Sbenjsc	}
2240173362Sbenjsc	return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1);
2241173362Sbenjsc#undef WPI_USEC
2242173362Sbenjsc#undef WPI_EXP2
2243173362Sbenjsc}
2244173362Sbenjsc
2245173362Sbenjsc/*
2246173362Sbenjsc * Configure h/w multi-rate retries.
2247173362Sbenjsc */
2248173362Sbenjscstatic int
2249173362Sbenjscwpi_mrr_setup(struct wpi_softc *sc)
2250173362Sbenjsc{
2251178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2252178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2253173362Sbenjsc	struct wpi_mrr_setup mrr;
2254173362Sbenjsc	int i, error;
2255173362Sbenjsc
2256173362Sbenjsc	memset(&mrr, 0, sizeof (struct wpi_mrr_setup));
2257173362Sbenjsc
2258173362Sbenjsc	/* CCK rates (not used with 802.11a) */
2259173362Sbenjsc	for (i = WPI_CCK1; i <= WPI_CCK11; i++) {
2260173362Sbenjsc		mrr.rates[i].flags = 0;
2261173362Sbenjsc		mrr.rates[i].signal = wpi_ridx_to_plcp[i];
2262173362Sbenjsc		/* fallback to the immediate lower CCK rate (if any) */
2263173362Sbenjsc		mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1;
2264173362Sbenjsc		/* try one time at this rate before falling back to "next" */
2265173362Sbenjsc		mrr.rates[i].ntries = 1;
2266173362Sbenjsc	}
2267173362Sbenjsc
2268173362Sbenjsc	/* OFDM rates (not used with 802.11b) */
2269173362Sbenjsc	for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) {
2270173362Sbenjsc		mrr.rates[i].flags = 0;
2271173362Sbenjsc		mrr.rates[i].signal = wpi_ridx_to_plcp[i];
2272173362Sbenjsc		/* fallback to the immediate lower OFDM rate (if any) */
2273173362Sbenjsc		/* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */
2274173362Sbenjsc		mrr.rates[i].next = (i == WPI_OFDM6) ?
2275173362Sbenjsc		    ((ic->ic_curmode == IEEE80211_MODE_11A) ?
2276173362Sbenjsc			WPI_OFDM6 : WPI_CCK2) :
2277173362Sbenjsc		    i - 1;
2278173362Sbenjsc		/* try one time at this rate before falling back to "next" */
2279173362Sbenjsc		mrr.rates[i].ntries = 1;
2280173362Sbenjsc	}
2281173362Sbenjsc
2282173362Sbenjsc	/* setup MRR for control frames */
2283173362Sbenjsc	mrr.which = htole32(WPI_MRR_CTL);
2284173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2285173362Sbenjsc	if (error != 0) {
2286173362Sbenjsc		device_printf(sc->sc_dev,
2287173362Sbenjsc		    "could not setup MRR for control frames\n");
2288173362Sbenjsc		return error;
2289173362Sbenjsc	}
2290173362Sbenjsc
2291173362Sbenjsc	/* setup MRR for data frames */
2292173362Sbenjsc	mrr.which = htole32(WPI_MRR_DATA);
2293173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2294173362Sbenjsc	if (error != 0) {
2295173362Sbenjsc		device_printf(sc->sc_dev,
2296173362Sbenjsc		    "could not setup MRR for data frames\n");
2297173362Sbenjsc		return error;
2298173362Sbenjsc	}
2299173362Sbenjsc
2300173362Sbenjsc	return 0;
2301173362Sbenjsc}
2302173362Sbenjsc
2303173362Sbenjscstatic void
2304173362Sbenjscwpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
2305173362Sbenjsc{
2306173362Sbenjsc	struct wpi_cmd_led led;
2307173362Sbenjsc
2308173362Sbenjsc	led.which = which;
2309173362Sbenjsc	led.unit = htole32(100000);	/* on/off in unit of 100ms */
2310173362Sbenjsc	led.off = off;
2311173362Sbenjsc	led.on = on;
2312173362Sbenjsc
2313173362Sbenjsc	(void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
2314173362Sbenjsc}
2315173362Sbenjsc
2316173362Sbenjscstatic void
2317173362Sbenjscwpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni)
2318173362Sbenjsc{
2319173362Sbenjsc	struct wpi_cmd_tsf tsf;
2320173362Sbenjsc	uint64_t val, mod;
2321173362Sbenjsc
2322173362Sbenjsc	memset(&tsf, 0, sizeof tsf);
2323173362Sbenjsc	memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8);
2324173362Sbenjsc	tsf.bintval = htole16(ni->ni_intval);
2325173362Sbenjsc	tsf.lintval = htole16(10);
2326173362Sbenjsc
2327173362Sbenjsc	/* compute remaining time until next beacon */
2328173362Sbenjsc	val = (uint64_t)ni->ni_intval  * 1024;	/* msec -> usec */
2329173362Sbenjsc	mod = le64toh(tsf.tstamp) % val;
2330173362Sbenjsc	tsf.binitval = htole32((uint32_t)(val - mod));
2331173362Sbenjsc
2332173362Sbenjsc	if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
2333173362Sbenjsc		device_printf(sc->sc_dev, "could not enable TSF\n");
2334173362Sbenjsc}
2335173362Sbenjsc
2336173362Sbenjsc#if 0
2337173362Sbenjsc/*
2338173362Sbenjsc * Build a beacon frame that the firmware will broadcast periodically in
2339173362Sbenjsc * IBSS or HostAP modes.
2340173362Sbenjsc */
2341173362Sbenjscstatic int
2342173362Sbenjscwpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
2343173362Sbenjsc{
2344178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2345178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2346173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2347173362Sbenjsc	struct wpi_tx_desc *desc;
2348173362Sbenjsc	struct wpi_tx_data *data;
2349173362Sbenjsc	struct wpi_tx_cmd *cmd;
2350173362Sbenjsc	struct wpi_cmd_beacon *bcn;
2351173362Sbenjsc	struct ieee80211_beacon_offsets bo;
2352173362Sbenjsc	struct mbuf *m0;
2353173362Sbenjsc	bus_addr_t physaddr;
2354173362Sbenjsc	int error;
2355173362Sbenjsc
2356173362Sbenjsc	desc = &ring->desc[ring->cur];
2357173362Sbenjsc	data = &ring->data[ring->cur];
2358173362Sbenjsc
2359173362Sbenjsc	m0 = ieee80211_beacon_alloc(ic, ni, &bo);
2360173362Sbenjsc	if (m0 == NULL) {
2361173362Sbenjsc		device_printf(sc->sc_dev, "could not allocate beacon frame\n");
2362173362Sbenjsc		return ENOMEM;
2363173362Sbenjsc	}
2364173362Sbenjsc
2365173362Sbenjsc	cmd = &ring->cmd[ring->cur];
2366173362Sbenjsc	cmd->code = WPI_CMD_SET_BEACON;
2367173362Sbenjsc	cmd->flags = 0;
2368173362Sbenjsc	cmd->qid = ring->qid;
2369173362Sbenjsc	cmd->idx = ring->cur;
2370173362Sbenjsc
2371173362Sbenjsc	bcn = (struct wpi_cmd_beacon *)cmd->data;
2372173362Sbenjsc	memset(bcn, 0, sizeof (struct wpi_cmd_beacon));
2373173362Sbenjsc	bcn->id = WPI_ID_BROADCAST;
2374173362Sbenjsc	bcn->ofdm_mask = 0xff;
2375173362Sbenjsc	bcn->cck_mask = 0x0f;
2376173362Sbenjsc	bcn->lifetime = htole32(WPI_LIFETIME_INFINITE);
2377173362Sbenjsc	bcn->len = htole16(m0->m_pkthdr.len);
2378173362Sbenjsc	bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2379173362Sbenjsc		wpi_plcp_signal(12) : wpi_plcp_signal(2);
2380173362Sbenjsc	bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
2381173362Sbenjsc
2382173362Sbenjsc	/* save and trim IEEE802.11 header */
2383173362Sbenjsc	m_copydata(m0, 0, sizeof (struct ieee80211_frame), (caddr_t)&bcn->wh);
2384173362Sbenjsc	m_adj(m0, sizeof (struct ieee80211_frame));
2385173362Sbenjsc
2386173362Sbenjsc	/* assume beacon frame is contiguous */
2387173362Sbenjsc	error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m0, void *),
2388173362Sbenjsc	    m0->m_pkthdr.len, wpi_dma_map_addr, &physaddr, 0);
2389173362Sbenjsc	if (error != 0) {
2390173362Sbenjsc		device_printf(sc->sc_dev, "could not map beacon\n");
2391173362Sbenjsc		m_freem(m0);
2392173362Sbenjsc		return error;
2393173362Sbenjsc	}
2394173362Sbenjsc
2395173362Sbenjsc	data->m = m0;
2396173362Sbenjsc
2397173362Sbenjsc	/* first scatter/gather segment is used by the beacon command */
2398173362Sbenjsc	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24);
2399173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2400173362Sbenjsc		ring->cur * sizeof (struct wpi_tx_cmd));
2401173362Sbenjsc	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_beacon));
2402173362Sbenjsc	desc->segs[1].addr = htole32(physaddr);
2403173362Sbenjsc	desc->segs[1].len  = htole32(m0->m_pkthdr.len);
2404173362Sbenjsc
2405173362Sbenjsc	/* kick cmd ring */
2406173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2407173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2408173362Sbenjsc
2409173362Sbenjsc	return 0;
2410173362Sbenjsc}
2411173362Sbenjsc#endif
2412173362Sbenjsc
2413173362Sbenjscstatic int
2414178354Ssamwpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap)
2415173362Sbenjsc{
2416178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2417178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2418173362Sbenjsc	struct wpi_node_info node;
2419173362Sbenjsc	int error;
2420173362Sbenjsc
2421177043Sthompsa
2422173362Sbenjsc	/* update adapter's configuration */
2423177043Sthompsa	sc->config.associd = 0;
2424177043Sthompsa	sc->config.filter &= ~htole32(WPI_FILTER_BSS);
2425173362Sbenjsc	IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
2426173362Sbenjsc	sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2427173362Sbenjsc	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2428173362Sbenjsc		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2429173362Sbenjsc		    WPI_CONFIG_24GHZ);
2430173362Sbenjsc	}
2431178354Ssam	if (IEEE80211_IS_CHAN_A(ni->ni_chan)) {
2432173362Sbenjsc		sc->config.cck_mask  = 0;
2433173362Sbenjsc		sc->config.ofdm_mask = 0x15;
2434178354Ssam	} else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) {
2435173362Sbenjsc		sc->config.cck_mask  = 0x03;
2436173362Sbenjsc		sc->config.ofdm_mask = 0;
2437178354Ssam	} else {
2438178354Ssam		/* XXX assume 802.11b/g */
2439173362Sbenjsc		sc->config.cck_mask  = 0x0f;
2440173362Sbenjsc		sc->config.ofdm_mask = 0x15;
2441173362Sbenjsc	}
2442173362Sbenjsc
2443173362Sbenjsc	DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
2444173362Sbenjsc		sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
2445173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2446173362Sbenjsc		sizeof (struct wpi_config), 1);
2447173362Sbenjsc	if (error != 0) {
2448173362Sbenjsc		device_printf(sc->sc_dev, "could not configure\n");
2449173362Sbenjsc		return error;
2450173362Sbenjsc	}
2451173362Sbenjsc
2452173362Sbenjsc	/* configuration has changed, set Tx power accordingly */
2453173362Sbenjsc	if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
2454173362Sbenjsc		device_printf(sc->sc_dev, "could not set Tx power\n");
2455173362Sbenjsc		return error;
2456173362Sbenjsc	}
2457173362Sbenjsc
2458173362Sbenjsc	/* add default node */
2459173362Sbenjsc	memset(&node, 0, sizeof node);
2460173362Sbenjsc	IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid);
2461173362Sbenjsc	node.id = WPI_ID_BSS;
2462173362Sbenjsc	node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2463173362Sbenjsc	    wpi_plcp_signal(12) : wpi_plcp_signal(2);
2464173362Sbenjsc	node.action = htole32(WPI_ACTION_SET_RATE);
2465173362Sbenjsc	node.antenna = WPI_ANTENNA_BOTH;
2466173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2467177043Sthompsa	if (error != 0)
2468177043Sthompsa		device_printf(sc->sc_dev, "could not add BSS node\n");
2469177043Sthompsa
2470177043Sthompsa	return (error);
2471177043Sthompsa}
2472177043Sthompsa
2473177043Sthompsastatic int
2474178354Ssamwpi_run(struct wpi_softc *sc, struct ieee80211vap *vap)
2475177043Sthompsa{
2476178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2477178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2478177043Sthompsa	int error;
2479177043Sthompsa
2480178354Ssam	if (vap->iv_opmode == IEEE80211_M_MONITOR) {
2481178354Ssam		/* link LED blinks while monitoring */
2482178354Ssam		wpi_set_led(sc, WPI_LED_LINK, 5, 5);
2483178354Ssam		return 0;
2484178354Ssam	}
2485178354Ssam
2486177043Sthompsa	wpi_enable_tsf(sc, ni);
2487177043Sthompsa
2488177043Sthompsa	/* update adapter's configuration */
2489177043Sthompsa	sc->config.associd = htole16(ni->ni_associd & ~0xc000);
2490177043Sthompsa	/* short preamble/slot time are negotiated when associating */
2491177043Sthompsa	sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE |
2492177043Sthompsa	    WPI_CONFIG_SHSLOT);
2493177043Sthompsa	if (ic->ic_flags & IEEE80211_F_SHSLOT)
2494177043Sthompsa		sc->config.flags |= htole32(WPI_CONFIG_SHSLOT);
2495177043Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2496177043Sthompsa		sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE);
2497177043Sthompsa	sc->config.filter |= htole32(WPI_FILTER_BSS);
2498177043Sthompsa
2499177043Sthompsa	/* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */
2500177043Sthompsa
2501177043Sthompsa	DPRINTF(("config chan %d flags %x\n", sc->config.chan,
2502177043Sthompsa		    sc->config.flags));
2503177043Sthompsa	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, sizeof (struct
2504177043Sthompsa		    wpi_config), 1);
2505173362Sbenjsc	if (error != 0) {
2506177043Sthompsa		device_printf(sc->sc_dev, "could not update configuration\n");
2507173362Sbenjsc		return error;
2508173362Sbenjsc	}
2509173362Sbenjsc
2510178354Ssam	error = wpi_set_txpower(sc, ni->ni_chan, 1);
2511177043Sthompsa	if (error != 0) {
2512177043Sthompsa		device_printf(sc->sc_dev, "could set txpower\n");
2513177043Sthompsa		return error;
2514177043Sthompsa	}
2515173362Sbenjsc
2516177043Sthompsa	/* link LED always on while associated */
2517177043Sthompsa	wpi_set_led(sc, WPI_LED_LINK, 0, 1);
2518177043Sthompsa
2519177043Sthompsa	/* start automatic rate control timer */
2520178354Ssam	callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
2521177043Sthompsa
2522177043Sthompsa	return (error);
2523173362Sbenjsc}
2524173362Sbenjsc
2525173362Sbenjsc/*
2526173362Sbenjsc * Send a scan request to the firmware.  Since this command is huge, we map it
2527173362Sbenjsc * into a mbufcluster instead of using the pre-allocated set of commands. Note,
2528173362Sbenjsc * much of this code is similar to that in wpi_cmd but because we must manually
2529173362Sbenjsc * construct the probe & channels, we duplicate what's needed here. XXX In the
2530173362Sbenjsc * future, this function should be modified to use wpi_cmd to help cleanup the
2531173362Sbenjsc * code base.
2532173362Sbenjsc */
2533173362Sbenjscstatic int
2534173362Sbenjscwpi_scan(struct wpi_softc *sc)
2535173362Sbenjsc{
2536178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2537178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2538177043Sthompsa	struct ieee80211_scan_state *ss = ic->ic_scan;
2539173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2540173362Sbenjsc	struct wpi_tx_desc *desc;
2541173362Sbenjsc	struct wpi_tx_data *data;
2542173362Sbenjsc	struct wpi_tx_cmd *cmd;
2543173362Sbenjsc	struct wpi_scan_hdr *hdr;
2544173362Sbenjsc	struct wpi_scan_chan *chan;
2545173362Sbenjsc	struct ieee80211_frame *wh;
2546173362Sbenjsc	struct ieee80211_rateset *rs;
2547173362Sbenjsc	struct ieee80211_channel *c;
2548173362Sbenjsc	enum ieee80211_phymode mode;
2549173362Sbenjsc	uint8_t *frm;
2550177043Sthompsa	int nrates, pktlen, error, i, nssid;
2551173362Sbenjsc	bus_addr_t physaddr;
2552173362Sbenjsc
2553173362Sbenjsc	desc = &ring->desc[ring->cur];
2554173362Sbenjsc	data = &ring->data[ring->cur];
2555173362Sbenjsc
2556173362Sbenjsc	data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2557173362Sbenjsc	if (data->m == NULL) {
2558173362Sbenjsc		device_printf(sc->sc_dev,
2559173362Sbenjsc		    "could not allocate mbuf for scan command\n");
2560173362Sbenjsc		return ENOMEM;
2561173362Sbenjsc	}
2562173362Sbenjsc
2563173362Sbenjsc	cmd = mtod(data->m, struct wpi_tx_cmd *);
2564173362Sbenjsc	cmd->code = WPI_CMD_SCAN;
2565173362Sbenjsc	cmd->flags = 0;
2566173362Sbenjsc	cmd->qid = ring->qid;
2567173362Sbenjsc	cmd->idx = ring->cur;
2568173362Sbenjsc
2569173362Sbenjsc	hdr = (struct wpi_scan_hdr *)cmd->data;
2570173362Sbenjsc	memset(hdr, 0, sizeof(struct wpi_scan_hdr));
2571173362Sbenjsc
2572173362Sbenjsc	/*
2573173362Sbenjsc	 * Move to the next channel if no packets are received within 5 msecs
2574173362Sbenjsc	 * after sending the probe request (this helps to reduce the duration
2575173362Sbenjsc	 * of active scans).
2576173362Sbenjsc	 */
2577173362Sbenjsc	hdr->quiet = htole16(5);
2578173362Sbenjsc	hdr->threshold = htole16(1);
2579173362Sbenjsc
2580173362Sbenjsc	if (IEEE80211_IS_CHAN_A(ic->ic_curchan)) {
2581173362Sbenjsc		/* send probe requests at 6Mbps */
2582173362Sbenjsc		hdr->tx.rate = wpi_ridx_to_plcp[WPI_OFDM6];
2583173362Sbenjsc
2584173362Sbenjsc		/* Enable crc checking */
2585173362Sbenjsc		hdr->promotion = htole16(1);
2586173362Sbenjsc	} else {
2587173362Sbenjsc		hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO);
2588173362Sbenjsc		/* send probe requests at 1Mbps */
2589173362Sbenjsc		hdr->tx.rate = wpi_ridx_to_plcp[WPI_CCK1];
2590173362Sbenjsc	}
2591173362Sbenjsc	hdr->tx.id = WPI_ID_BROADCAST;
2592173362Sbenjsc	hdr->tx.lifetime = htole32(WPI_LIFETIME_INFINITE);
2593173362Sbenjsc	hdr->tx.flags = htole32(WPI_TX_AUTO_SEQ);
2594173362Sbenjsc
2595178354Ssam	memset(hdr->scan_essids, 0, sizeof(hdr->scan_essids));
2596177043Sthompsa	nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS);
2597178354Ssam	for (i = 0; i < nssid; i++) {
2598177043Sthompsa		hdr->scan_essids[i].id = IEEE80211_ELEMID_SSID;
2599177043Sthompsa		hdr->scan_essids[i].esslen = MIN(ss->ss_ssid[i].len, 32);
2600177043Sthompsa		memcpy(hdr->scan_essids[i].essid, ss->ss_ssid[i].ssid,
2601177043Sthompsa		    hdr->scan_essids[i].esslen);
2602179957Sthompsa#ifdef WPI_DEBUG
2603177043Sthompsa		if (wpi_debug & WPI_DEBUG_SCANNING) {
2604177043Sthompsa			printf("Scanning Essid: ");
2605178354Ssam			ieee80211_print_essid(hdr->scan_essids[i].essid,
2606178354Ssam			    hdr->scan_essids[i].esslen);
2607177043Sthompsa			printf("\n");
2608177043Sthompsa		}
2609179957Sthompsa#endif
2610173362Sbenjsc	}
2611173362Sbenjsc
2612173362Sbenjsc	/*
2613173362Sbenjsc	 * Build a probe request frame.  Most of the following code is a
2614173362Sbenjsc	 * copy & paste of what is done in net80211.
2615173362Sbenjsc	 */
2616173362Sbenjsc	wh = (struct ieee80211_frame *)&hdr->scan_essids[4];
2617173362Sbenjsc	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2618173362Sbenjsc		IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2619173362Sbenjsc	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2620173362Sbenjsc	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2621190526Ssam	IEEE80211_ADDR_COPY(wh->i_addr2, IF_LLADDR(ifp));
2622173362Sbenjsc	IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
2623173362Sbenjsc	*(u_int16_t *)&wh->i_dur[0] = 0;	/* filled by h/w */
2624173362Sbenjsc	*(u_int16_t *)&wh->i_seq[0] = 0;	/* filled by h/w */
2625173362Sbenjsc
2626173362Sbenjsc	frm = (uint8_t *)(wh + 1);
2627173362Sbenjsc
2628173362Sbenjsc	/* add essid IE, the hardware will fill this in for us */
2629173362Sbenjsc	*frm++ = IEEE80211_ELEMID_SSID;
2630173362Sbenjsc	*frm++ = 0;
2631173362Sbenjsc
2632173362Sbenjsc	mode = ieee80211_chan2mode(ic->ic_curchan);
2633173362Sbenjsc	rs = &ic->ic_sup_rates[mode];
2634173362Sbenjsc
2635173362Sbenjsc	/* add supported rates IE */
2636173362Sbenjsc	*frm++ = IEEE80211_ELEMID_RATES;
2637173362Sbenjsc	nrates = rs->rs_nrates;
2638173362Sbenjsc	if (nrates > IEEE80211_RATE_SIZE)
2639173362Sbenjsc		nrates = IEEE80211_RATE_SIZE;
2640173362Sbenjsc	*frm++ = nrates;
2641173362Sbenjsc	memcpy(frm, rs->rs_rates, nrates);
2642173362Sbenjsc	frm += nrates;
2643173362Sbenjsc
2644173362Sbenjsc	/* add supported xrates IE */
2645173362Sbenjsc	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2646173362Sbenjsc		nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2647173362Sbenjsc		*frm++ = IEEE80211_ELEMID_XRATES;
2648173362Sbenjsc		*frm++ = nrates;
2649173362Sbenjsc		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2650173362Sbenjsc		frm += nrates;
2651173362Sbenjsc	}
2652173362Sbenjsc
2653173362Sbenjsc	/* setup length of probe request */
2654173362Sbenjsc	hdr->tx.len = htole16(frm - (uint8_t *)wh);
2655173362Sbenjsc
2656173362Sbenjsc	/*
2657173362Sbenjsc	 * Construct information about the channel that we
2658173362Sbenjsc	 * want to scan. The firmware expects this to be directly
2659173362Sbenjsc	 * after the scan probe request
2660173362Sbenjsc	 */
2661173362Sbenjsc	c = ic->ic_curchan;
2662173362Sbenjsc	chan = (struct wpi_scan_chan *)frm;
2663173362Sbenjsc	chan->chan = ieee80211_chan2ieee(ic, c);
2664173362Sbenjsc	chan->flags = 0;
2665173362Sbenjsc	if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
2666173362Sbenjsc		chan->flags |= WPI_CHAN_ACTIVE;
2667178354Ssam		if (nssid != 0)
2668173362Sbenjsc			chan->flags |= WPI_CHAN_DIRECT;
2669173362Sbenjsc	}
2670173362Sbenjsc	chan->gain_dsp = 0x6e; /* Default level */
2671173362Sbenjsc	if (IEEE80211_IS_CHAN_5GHZ(c)) {
2672173362Sbenjsc		chan->active = htole16(10);
2673178354Ssam		chan->passive = htole16(ss->ss_maxdwell);
2674173362Sbenjsc		chan->gain_radio = 0x3b;
2675173362Sbenjsc	} else {
2676173362Sbenjsc		chan->active = htole16(20);
2677178354Ssam		chan->passive = htole16(ss->ss_maxdwell);
2678173362Sbenjsc		chan->gain_radio = 0x28;
2679173362Sbenjsc	}
2680173362Sbenjsc
2681173362Sbenjsc	DPRINTFN(WPI_DEBUG_SCANNING,
2682173362Sbenjsc	    ("Scanning %u Passive: %d\n",
2683173362Sbenjsc	     chan->chan,
2684173362Sbenjsc	     c->ic_flags & IEEE80211_CHAN_PASSIVE));
2685173362Sbenjsc
2686173362Sbenjsc	hdr->nchan++;
2687173362Sbenjsc	chan++;
2688173362Sbenjsc
2689173362Sbenjsc	frm += sizeof (struct wpi_scan_chan);
2690173362Sbenjsc#if 0
2691173362Sbenjsc	// XXX All Channels....
2692173362Sbenjsc	for (c  = &ic->ic_channels[1];
2693173362Sbenjsc	     c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
2694173362Sbenjsc		if ((c->ic_flags & ic->ic_curchan->ic_flags) != ic->ic_curchan->ic_flags)
2695173362Sbenjsc			continue;
2696173362Sbenjsc
2697173362Sbenjsc		chan->chan = ieee80211_chan2ieee(ic, c);
2698173362Sbenjsc		chan->flags = 0;
2699173362Sbenjsc		if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
2700173362Sbenjsc		    chan->flags |= WPI_CHAN_ACTIVE;
2701173362Sbenjsc		    if (ic->ic_des_ssid[0].len != 0)
2702173362Sbenjsc			chan->flags |= WPI_CHAN_DIRECT;
2703173362Sbenjsc		}
2704173362Sbenjsc		chan->gain_dsp = 0x6e; /* Default level */
2705173362Sbenjsc		if (IEEE80211_IS_CHAN_5GHZ(c)) {
2706173362Sbenjsc			chan->active = htole16(10);
2707173362Sbenjsc			chan->passive = htole16(110);
2708173362Sbenjsc			chan->gain_radio = 0x3b;
2709173362Sbenjsc		} else {
2710173362Sbenjsc			chan->active = htole16(20);
2711173362Sbenjsc			chan->passive = htole16(120);
2712173362Sbenjsc			chan->gain_radio = 0x28;
2713173362Sbenjsc		}
2714173362Sbenjsc
2715173362Sbenjsc		DPRINTFN(WPI_DEBUG_SCANNING,
2716173362Sbenjsc			 ("Scanning %u Passive: %d\n",
2717173362Sbenjsc			  chan->chan,
2718173362Sbenjsc			  c->ic_flags & IEEE80211_CHAN_PASSIVE));
2719173362Sbenjsc
2720173362Sbenjsc		hdr->nchan++;
2721173362Sbenjsc		chan++;
2722173362Sbenjsc
2723173362Sbenjsc		frm += sizeof (struct wpi_scan_chan);
2724173362Sbenjsc	}
2725173362Sbenjsc#endif
2726173362Sbenjsc
2727173362Sbenjsc	hdr->len = htole16(frm - (uint8_t *)hdr);
2728173362Sbenjsc	pktlen = frm - (uint8_t *)cmd;
2729173362Sbenjsc
2730173362Sbenjsc	error = bus_dmamap_load(ring->data_dmat, data->map, cmd, pktlen,
2731173362Sbenjsc	    wpi_dma_map_addr, &physaddr, BUS_DMA_NOWAIT);
2732173362Sbenjsc	if (error != 0) {
2733173362Sbenjsc		device_printf(sc->sc_dev, "could not map scan command\n");
2734173362Sbenjsc		m_freem(data->m);
2735173362Sbenjsc		data->m = NULL;
2736173362Sbenjsc		return error;
2737173362Sbenjsc	}
2738173362Sbenjsc
2739173362Sbenjsc	desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24);
2740173362Sbenjsc	desc->segs[0].addr = htole32(physaddr);
2741173362Sbenjsc	desc->segs[0].len  = htole32(pktlen);
2742173362Sbenjsc
2743173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2744173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
2745173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2746173362Sbenjsc
2747173362Sbenjsc	/* kick cmd ring */
2748173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2749173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2750173362Sbenjsc
2751177043Sthompsa	sc->sc_scan_timer = 5;
2752173362Sbenjsc	return 0;	/* will be notified async. of failure/success */
2753173362Sbenjsc}
2754173362Sbenjsc
2755173362Sbenjsc/**
2756173362Sbenjsc * Configure the card to listen to a particular channel, this transisions the
2757173362Sbenjsc * card in to being able to receive frames from remote devices.
2758173362Sbenjsc */
2759173362Sbenjscstatic int
2760173362Sbenjscwpi_config(struct wpi_softc *sc)
2761173362Sbenjsc{
2762178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2763178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2764173362Sbenjsc	struct wpi_power power;
2765173362Sbenjsc	struct wpi_bluetooth bluetooth;
2766173362Sbenjsc	struct wpi_node_info node;
2767173362Sbenjsc	int error;
2768173362Sbenjsc
2769173362Sbenjsc	/* set power mode */
2770173362Sbenjsc	memset(&power, 0, sizeof power);
2771173362Sbenjsc	power.flags = htole32(WPI_POWER_CAM|0x8);
2772173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0);
2773173362Sbenjsc	if (error != 0) {
2774173362Sbenjsc		device_printf(sc->sc_dev, "could not set power mode\n");
2775173362Sbenjsc		return error;
2776173362Sbenjsc	}
2777173362Sbenjsc
2778173362Sbenjsc	/* configure bluetooth coexistence */
2779173362Sbenjsc	memset(&bluetooth, 0, sizeof bluetooth);
2780173362Sbenjsc	bluetooth.flags = 3;
2781173362Sbenjsc	bluetooth.lead = 0xaa;
2782173362Sbenjsc	bluetooth.kill = 1;
2783173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
2784173362Sbenjsc	    0);
2785173362Sbenjsc	if (error != 0) {
2786173362Sbenjsc		device_printf(sc->sc_dev,
2787173362Sbenjsc		    "could not configure bluetooth coexistence\n");
2788173362Sbenjsc		return error;
2789173362Sbenjsc	}
2790173362Sbenjsc
2791173362Sbenjsc	/* configure adapter */
2792173362Sbenjsc	memset(&sc->config, 0, sizeof (struct wpi_config));
2793190526Ssam	IEEE80211_ADDR_COPY(sc->config.myaddr, IF_LLADDR(ifp));
2794173362Sbenjsc	/*set default channel*/
2795173362Sbenjsc	sc->config.chan = htole16(ieee80211_chan2ieee(ic, ic->ic_curchan));
2796173362Sbenjsc	sc->config.flags = htole32(WPI_CONFIG_TSF);
2797173362Sbenjsc	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2798173362Sbenjsc		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2799173362Sbenjsc		    WPI_CONFIG_24GHZ);
2800173362Sbenjsc	}
2801173362Sbenjsc	sc->config.filter = 0;
2802173362Sbenjsc	switch (ic->ic_opmode) {
2803173362Sbenjsc	case IEEE80211_M_STA:
2804173362Sbenjsc	case IEEE80211_M_WDS:	/* No know setup, use STA for now */
2805173362Sbenjsc		sc->config.mode = WPI_MODE_STA;
2806173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_MULTICAST);
2807173362Sbenjsc		break;
2808173362Sbenjsc	case IEEE80211_M_IBSS:
2809173362Sbenjsc	case IEEE80211_M_AHDEMO:
2810173362Sbenjsc		sc->config.mode = WPI_MODE_IBSS;
2811173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_BEACON |
2812173362Sbenjsc					     WPI_FILTER_MULTICAST);
2813173362Sbenjsc		break;
2814173362Sbenjsc	case IEEE80211_M_HOSTAP:
2815173362Sbenjsc		sc->config.mode = WPI_MODE_HOSTAP;
2816173362Sbenjsc		break;
2817173362Sbenjsc	case IEEE80211_M_MONITOR:
2818173362Sbenjsc		sc->config.mode = WPI_MODE_MONITOR;
2819173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_MULTICAST |
2820173362Sbenjsc			WPI_FILTER_CTL | WPI_FILTER_PROMISC);
2821173362Sbenjsc		break;
2822195562Srpaulo	default:
2823195562Srpaulo		device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2824195562Srpaulo		return EINVAL;
2825173362Sbenjsc	}
2826173362Sbenjsc	sc->config.cck_mask  = 0x0f;	/* not yet negotiated */
2827173362Sbenjsc	sc->config.ofdm_mask = 0xff;	/* not yet negotiated */
2828173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2829173362Sbenjsc		sizeof (struct wpi_config), 0);
2830173362Sbenjsc	if (error != 0) {
2831173362Sbenjsc		device_printf(sc->sc_dev, "configure command failed\n");
2832173362Sbenjsc		return error;
2833173362Sbenjsc	}
2834173362Sbenjsc
2835173362Sbenjsc	/* configuration has changed, set Tx power accordingly */
2836177043Sthompsa	if ((error = wpi_set_txpower(sc, ic->ic_curchan, 0)) != 0) {
2837173362Sbenjsc	    device_printf(sc->sc_dev, "could not set Tx power\n");
2838173362Sbenjsc	    return error;
2839173362Sbenjsc	}
2840173362Sbenjsc
2841173362Sbenjsc	/* add broadcast node */
2842173362Sbenjsc	memset(&node, 0, sizeof node);
2843173362Sbenjsc	IEEE80211_ADDR_COPY(node.bssid, ifp->if_broadcastaddr);
2844173362Sbenjsc	node.id = WPI_ID_BROADCAST;
2845173362Sbenjsc	node.rate = wpi_plcp_signal(2);
2846173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
2847173362Sbenjsc	if (error != 0) {
2848173362Sbenjsc		device_printf(sc->sc_dev, "could not add broadcast node\n");
2849173362Sbenjsc		return error;
2850173362Sbenjsc	}
2851173362Sbenjsc
2852173362Sbenjsc	/* Setup rate scalling */
2853173362Sbenjsc	error = wpi_mrr_setup(sc);
2854173362Sbenjsc	if (error != 0) {
2855173362Sbenjsc		device_printf(sc->sc_dev, "could not setup MRR\n");
2856173362Sbenjsc		return error;
2857173362Sbenjsc	}
2858173362Sbenjsc
2859173362Sbenjsc	return 0;
2860173362Sbenjsc}
2861173362Sbenjsc
2862173362Sbenjscstatic void
2863173362Sbenjscwpi_stop_master(struct wpi_softc *sc)
2864173362Sbenjsc{
2865173362Sbenjsc	uint32_t tmp;
2866173362Sbenjsc	int ntries;
2867173362Sbenjsc
2868173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,("Disabling Firmware execution\n"));
2869173362Sbenjsc
2870173362Sbenjsc	tmp = WPI_READ(sc, WPI_RESET);
2871173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER | WPI_NEVO_RESET);
2872173362Sbenjsc
2873173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
2874173362Sbenjsc	if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP)
2875173362Sbenjsc		return;	/* already asleep */
2876173362Sbenjsc
2877173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
2878173362Sbenjsc		if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED)
2879173362Sbenjsc			break;
2880173362Sbenjsc		DELAY(10);
2881173362Sbenjsc	}
2882173362Sbenjsc	if (ntries == 100) {
2883173362Sbenjsc		device_printf(sc->sc_dev, "timeout waiting for master\n");
2884173362Sbenjsc	}
2885173362Sbenjsc}
2886173362Sbenjsc
2887173362Sbenjscstatic int
2888173362Sbenjscwpi_power_up(struct wpi_softc *sc)
2889173362Sbenjsc{
2890173362Sbenjsc	uint32_t tmp;
2891173362Sbenjsc	int ntries;
2892173362Sbenjsc
2893173362Sbenjsc	wpi_mem_lock(sc);
2894173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_POWER);
2895173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000);
2896173362Sbenjsc	wpi_mem_unlock(sc);
2897173362Sbenjsc
2898173362Sbenjsc	for (ntries = 0; ntries < 5000; ntries++) {
2899173362Sbenjsc		if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED)
2900173362Sbenjsc			break;
2901173362Sbenjsc		DELAY(10);
2902173362Sbenjsc	}
2903173362Sbenjsc	if (ntries == 5000) {
2904173362Sbenjsc		device_printf(sc->sc_dev,
2905173362Sbenjsc		    "timeout waiting for NIC to power up\n");
2906173362Sbenjsc		return ETIMEDOUT;
2907173362Sbenjsc	}
2908173362Sbenjsc	return 0;
2909173362Sbenjsc}
2910173362Sbenjsc
2911173362Sbenjscstatic int
2912173362Sbenjscwpi_reset(struct wpi_softc *sc)
2913173362Sbenjsc{
2914173362Sbenjsc	uint32_t tmp;
2915173362Sbenjsc	int ntries;
2916173362Sbenjsc
2917173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,
2918173362Sbenjsc	    ("Resetting the card - clearing any uploaded firmware\n"));
2919173362Sbenjsc
2920173362Sbenjsc	/* clear any pending interrupts */
2921173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
2922173362Sbenjsc
2923173362Sbenjsc	tmp = WPI_READ(sc, WPI_PLL_CTL);
2924173362Sbenjsc	WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT);
2925173362Sbenjsc
2926173362Sbenjsc	tmp = WPI_READ(sc, WPI_CHICKEN);
2927173362Sbenjsc	WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS);
2928173362Sbenjsc
2929173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
2930173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT);
2931173362Sbenjsc
2932173362Sbenjsc	/* wait for clock stabilization */
2933173362Sbenjsc	for (ntries = 0; ntries < 25000; ntries++) {
2934173362Sbenjsc		if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK)
2935173362Sbenjsc			break;
2936173362Sbenjsc		DELAY(10);
2937173362Sbenjsc	}
2938173362Sbenjsc	if (ntries == 25000) {
2939173362Sbenjsc		device_printf(sc->sc_dev,
2940173362Sbenjsc		    "timeout waiting for clock stabilization\n");
2941173362Sbenjsc		return ETIMEDOUT;
2942173362Sbenjsc	}
2943173362Sbenjsc
2944173362Sbenjsc	/* initialize EEPROM */
2945173362Sbenjsc	tmp = WPI_READ(sc, WPI_EEPROM_STATUS);
2946173362Sbenjsc
2947173362Sbenjsc	if ((tmp & WPI_EEPROM_VERSION) == 0) {
2948173362Sbenjsc		device_printf(sc->sc_dev, "EEPROM not found\n");
2949173362Sbenjsc		return EIO;
2950173362Sbenjsc	}
2951173362Sbenjsc	WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED);
2952173362Sbenjsc
2953173362Sbenjsc	return 0;
2954173362Sbenjsc}
2955173362Sbenjsc
2956173362Sbenjscstatic void
2957173362Sbenjscwpi_hw_config(struct wpi_softc *sc)
2958173362Sbenjsc{
2959173362Sbenjsc	uint32_t rev, hw;
2960173362Sbenjsc
2961173362Sbenjsc	/* voodoo from the Linux "driver".. */
2962173362Sbenjsc	hw = WPI_READ(sc, WPI_HWCONFIG);
2963173362Sbenjsc
2964173362Sbenjsc	rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1);
2965173362Sbenjsc	if ((rev & 0xc0) == 0x40)
2966173362Sbenjsc		hw |= WPI_HW_ALM_MB;
2967173362Sbenjsc	else if (!(rev & 0x80))
2968173362Sbenjsc		hw |= WPI_HW_ALM_MM;
2969173362Sbenjsc
2970173362Sbenjsc	if (sc->cap == 0x80)
2971173362Sbenjsc		hw |= WPI_HW_SKU_MRC;
2972173362Sbenjsc
2973173362Sbenjsc	hw &= ~WPI_HW_REV_D;
2974173362Sbenjsc	if ((le16toh(sc->rev) & 0xf0) == 0xd0)
2975173362Sbenjsc		hw |= WPI_HW_REV_D;
2976173362Sbenjsc
2977173362Sbenjsc	if (sc->type > 1)
2978173362Sbenjsc		hw |= WPI_HW_TYPE_B;
2979173362Sbenjsc
2980173362Sbenjsc	WPI_WRITE(sc, WPI_HWCONFIG, hw);
2981173362Sbenjsc}
2982173362Sbenjsc
2983173362Sbenjscstatic void
2984177043Sthompsawpi_rfkill_resume(struct wpi_softc *sc)
2985177043Sthompsa{
2986177043Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2987178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2988178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2989177043Sthompsa	int ntries;
2990177043Sthompsa
2991177043Sthompsa	/* enable firmware again */
2992177043Sthompsa	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
2993177043Sthompsa	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
2994177043Sthompsa
2995177043Sthompsa	/* wait for thermal sensors to calibrate */
2996177043Sthompsa	for (ntries = 0; ntries < 1000; ntries++) {
2997177043Sthompsa		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
2998177043Sthompsa			break;
2999177043Sthompsa		DELAY(10);
3000177043Sthompsa	}
3001177043Sthompsa
3002177043Sthompsa	if (ntries == 1000) {
3003177043Sthompsa		device_printf(sc->sc_dev,
3004177043Sthompsa		    "timeout waiting for thermal calibration\n");
3005177043Sthompsa		WPI_UNLOCK(sc);
3006177043Sthompsa		return;
3007177043Sthompsa	}
3008177043Sthompsa	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d\n", sc->temp));
3009177043Sthompsa
3010177043Sthompsa	if (wpi_config(sc) != 0) {
3011177043Sthompsa		device_printf(sc->sc_dev, "device config failed\n");
3012177043Sthompsa		WPI_UNLOCK(sc);
3013177043Sthompsa		return;
3014177043Sthompsa	}
3015177043Sthompsa
3016177043Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3017177043Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3018177043Sthompsa	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3019177043Sthompsa
3020178354Ssam	if (vap != NULL) {
3021178354Ssam		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
3022178354Ssam			if (vap->iv_opmode != IEEE80211_M_MONITOR) {
3023191746Sthompsa				ieee80211_beacon_miss(ic);
3024178354Ssam				wpi_set_led(sc, WPI_LED_LINK, 0, 1);
3025178354Ssam			} else
3026178354Ssam				wpi_set_led(sc, WPI_LED_LINK, 5, 5);
3027178354Ssam		} else {
3028178354Ssam			ieee80211_scan_next(vap);
3029177043Sthompsa			wpi_set_led(sc, WPI_LED_LINK, 20, 2);
3030178354Ssam		}
3031177043Sthompsa	}
3032177043Sthompsa
3033177043Sthompsa	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3034177043Sthompsa}
3035177043Sthompsa
3036177043Sthompsastatic void
3037177043Sthompsawpi_init_locked(struct wpi_softc *sc, int force)
3038177043Sthompsa{
3039178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3040173362Sbenjsc	uint32_t tmp;
3041177043Sthompsa	int ntries, qid;
3042173362Sbenjsc
3043173362Sbenjsc	wpi_stop_locked(sc);
3044173362Sbenjsc	(void)wpi_reset(sc);
3045173362Sbenjsc
3046173362Sbenjsc	wpi_mem_lock(sc);
3047173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00);
3048173362Sbenjsc	DELAY(20);
3049173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
3050173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800);
3051173362Sbenjsc	wpi_mem_unlock(sc);
3052173362Sbenjsc
3053173362Sbenjsc	(void)wpi_power_up(sc);
3054173362Sbenjsc	wpi_hw_config(sc);
3055173362Sbenjsc
3056173362Sbenjsc	/* init Rx ring */
3057173362Sbenjsc	wpi_mem_lock(sc);
3058173362Sbenjsc	WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr);
3059173362Sbenjsc	WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr +
3060173362Sbenjsc	    offsetof(struct wpi_shared, next));
3061173362Sbenjsc	WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
3062173362Sbenjsc	WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010);
3063173362Sbenjsc	wpi_mem_unlock(sc);
3064173362Sbenjsc
3065173362Sbenjsc	/* init Tx rings */
3066173362Sbenjsc	wpi_mem_lock(sc);
3067173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */
3068173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_RA, 1);   /* enable RA0 */
3069173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */
3070173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000);
3071173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002);
3072173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MAGIC4, 4);
3073173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MAGIC5, 5);
3074173362Sbenjsc
3075173362Sbenjsc	WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr);
3076173362Sbenjsc	WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5);
3077173362Sbenjsc
3078173362Sbenjsc	for (qid = 0; qid < 6; qid++) {
3079173362Sbenjsc		WPI_WRITE(sc, WPI_TX_CTL(qid), 0);
3080173362Sbenjsc		WPI_WRITE(sc, WPI_TX_BASE(qid), 0);
3081173362Sbenjsc		WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008);
3082173362Sbenjsc	}
3083173362Sbenjsc	wpi_mem_unlock(sc);
3084173362Sbenjsc
3085173362Sbenjsc	/* clear "radio off" and "disable command" bits (reversed logic) */
3086173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3087173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
3088173362Sbenjsc	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3089173362Sbenjsc
3090173362Sbenjsc	/* clear any pending interrupts */
3091173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
3092173362Sbenjsc
3093173362Sbenjsc	/* enable interrupts */
3094173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
3095173362Sbenjsc
3096173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3097173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3098173362Sbenjsc
3099177043Sthompsa	if ((wpi_load_firmware(sc)) != 0) {
3100173362Sbenjsc	    device_printf(sc->sc_dev,
3101173362Sbenjsc		"A problem occurred loading the firmware to the driver\n");
3102173362Sbenjsc	    return;
3103173362Sbenjsc	}
3104173362Sbenjsc
3105173362Sbenjsc	/* At this point the firmware is up and running. If the hardware
3106173362Sbenjsc	 * RF switch is turned off thermal calibration will fail, though
3107173362Sbenjsc	 * the card is still happy to continue to accept commands, catch
3108177043Sthompsa	 * this case and schedule a task to watch for it to be turned on.
3109173362Sbenjsc	 */
3110173362Sbenjsc	wpi_mem_lock(sc);
3111173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_HW_RADIO_OFF);
3112173362Sbenjsc	wpi_mem_unlock(sc);
3113173362Sbenjsc
3114173362Sbenjsc	if (!(tmp & 0x1)) {
3115173362Sbenjsc		sc->flags |= WPI_FLAG_HW_RADIO_OFF;
3116173362Sbenjsc		device_printf(sc->sc_dev,"Radio Transmitter is switched off\n");
3117177043Sthompsa		goto out;
3118173362Sbenjsc	}
3119173362Sbenjsc
3120173362Sbenjsc	/* wait for thermal sensors to calibrate */
3121173362Sbenjsc	for (ntries = 0; ntries < 1000; ntries++) {
3122173362Sbenjsc		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
3123173362Sbenjsc			break;
3124173362Sbenjsc		DELAY(10);
3125173362Sbenjsc	}
3126173362Sbenjsc
3127173362Sbenjsc	if (ntries == 1000) {
3128173362Sbenjsc		device_printf(sc->sc_dev,
3129173362Sbenjsc		    "timeout waiting for thermal sensors calibration\n");
3130173362Sbenjsc		return;
3131173362Sbenjsc	}
3132173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d\n", sc->temp));
3133173362Sbenjsc
3134177043Sthompsa	if (wpi_config(sc) != 0) {
3135177043Sthompsa		device_printf(sc->sc_dev, "device config failed\n");
3136177043Sthompsa		return;
3137177043Sthompsa	}
3138177043Sthompsa
3139173362Sbenjsc	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3140173362Sbenjsc	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3141177043Sthompsaout:
3142177043Sthompsa	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3143173362Sbenjsc}
3144173362Sbenjsc
3145173362Sbenjscstatic void
3146178354Ssamwpi_init(void *arg)
3147173362Sbenjsc{
3148178354Ssam	struct wpi_softc *sc = arg;
3149178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3150178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3151173362Sbenjsc
3152173362Sbenjsc	WPI_LOCK(sc);
3153178354Ssam	wpi_init_locked(sc, 0);
3154173362Sbenjsc	WPI_UNLOCK(sc);
3155173362Sbenjsc
3156178354Ssam	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3157178354Ssam		ieee80211_start_all(ic);		/* start all vaps */
3158173362Sbenjsc}
3159178354Ssam
3160173362Sbenjscstatic void
3161173362Sbenjscwpi_stop_locked(struct wpi_softc *sc)
3162173362Sbenjsc{
3163178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3164173362Sbenjsc	uint32_t tmp;
3165173362Sbenjsc	int ac;
3166173362Sbenjsc
3167177043Sthompsa	sc->sc_tx_timer = 0;
3168177043Sthompsa	sc->sc_scan_timer = 0;
3169173362Sbenjsc	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3170177043Sthompsa	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3171177043Sthompsa	callout_stop(&sc->watchdog_to);
3172177043Sthompsa	callout_stop(&sc->calib_to);
3173173362Sbenjsc
3174177043Sthompsa
3175173362Sbenjsc	/* disable interrupts */
3176173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, 0);
3177173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK);
3178173362Sbenjsc	WPI_WRITE(sc, WPI_INTR_STATUS, 0xff);
3179173362Sbenjsc	WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000);
3180173362Sbenjsc
3181173362Sbenjsc	wpi_mem_lock(sc);
3182173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MODE, 0);
3183173362Sbenjsc	wpi_mem_unlock(sc);
3184173362Sbenjsc
3185173362Sbenjsc	/* reset all Tx rings */
3186173362Sbenjsc	for (ac = 0; ac < 4; ac++)
3187173362Sbenjsc		wpi_reset_tx_ring(sc, &sc->txq[ac]);
3188173362Sbenjsc	wpi_reset_tx_ring(sc, &sc->cmdq);
3189173362Sbenjsc
3190173362Sbenjsc	/* reset Rx ring */
3191173362Sbenjsc	wpi_reset_rx_ring(sc, &sc->rxq);
3192173362Sbenjsc
3193173362Sbenjsc	wpi_mem_lock(sc);
3194173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200);
3195173362Sbenjsc	wpi_mem_unlock(sc);
3196173362Sbenjsc
3197173362Sbenjsc	DELAY(5);
3198173362Sbenjsc
3199173362Sbenjsc	wpi_stop_master(sc);
3200173362Sbenjsc
3201173362Sbenjsc	tmp = WPI_READ(sc, WPI_RESET);
3202173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET);
3203173362Sbenjsc	sc->flags &= ~WPI_FLAG_BUSY;
3204173362Sbenjsc}
3205173362Sbenjsc
3206173362Sbenjscstatic void
3207178354Ssamwpi_stop(struct wpi_softc *sc)
3208173362Sbenjsc{
3209178354Ssam	WPI_LOCK(sc);
3210178354Ssam	wpi_stop_locked(sc);
3211178354Ssam	WPI_UNLOCK(sc);
3212173362Sbenjsc}
3213173362Sbenjsc
3214173362Sbenjscstatic void
3215173362Sbenjscwpi_newassoc(struct ieee80211_node *ni, int isnew)
3216173362Sbenjsc{
3217178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
3218178354Ssam	struct wpi_vap *wvp = WPI_VAP(vap);
3219173362Sbenjsc
3220178354Ssam	ieee80211_amrr_node_init(&wvp->amrr, &WPI_NODE(ni)->amn, ni);
3221173362Sbenjsc}
3222173362Sbenjsc
3223173362Sbenjscstatic void
3224173362Sbenjscwpi_calib_timeout(void *arg)
3225173362Sbenjsc{
3226173362Sbenjsc	struct wpi_softc *sc = arg;
3227178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3228178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3229178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3230173362Sbenjsc	int temp;
3231173362Sbenjsc
3232178354Ssam	if (vap->iv_state != IEEE80211_S_RUN)
3233177043Sthompsa		return;
3234177043Sthompsa
3235173362Sbenjsc	/* update sensor data */
3236173362Sbenjsc	temp = (int)WPI_READ(sc, WPI_TEMPERATURE);
3237173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("Temp in calibration is: %d\n", temp));
3238173362Sbenjsc
3239178354Ssam	wpi_power_calibration(sc, temp);
3240173362Sbenjsc
3241178354Ssam	callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
3242173362Sbenjsc}
3243173362Sbenjsc
3244173362Sbenjsc/*
3245173362Sbenjsc * This function is called periodically (every 60 seconds) to adjust output
3246173362Sbenjsc * power to temperature changes.
3247173362Sbenjsc */
3248173362Sbenjscstatic void
3249173362Sbenjscwpi_power_calibration(struct wpi_softc *sc, int temp)
3250173362Sbenjsc{
3251178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3252178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3253178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3254178354Ssam
3255173362Sbenjsc	/* sanity-check read value */
3256173362Sbenjsc	if (temp < -260 || temp > 25) {
3257173362Sbenjsc		/* this can't be correct, ignore */
3258173362Sbenjsc		DPRINTFN(WPI_DEBUG_TEMP,
3259173362Sbenjsc		    ("out-of-range temperature reported: %d\n", temp));
3260173362Sbenjsc		return;
3261173362Sbenjsc	}
3262173362Sbenjsc
3263173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d->%d\n", sc->temp, temp));
3264173362Sbenjsc
3265173362Sbenjsc	/* adjust Tx power if need be */
3266173362Sbenjsc	if (abs(temp - sc->temp) <= 6)
3267173362Sbenjsc		return;
3268173362Sbenjsc
3269173362Sbenjsc	sc->temp = temp;
3270173362Sbenjsc
3271178354Ssam	if (wpi_set_txpower(sc, vap->iv_bss->ni_chan, 1) != 0) {
3272173362Sbenjsc		/* just warn, too bad for the automatic calibration... */
3273173362Sbenjsc		device_printf(sc->sc_dev,"could not adjust Tx power\n");
3274173362Sbenjsc	}
3275173362Sbenjsc}
3276173362Sbenjsc
3277173362Sbenjsc/**
3278173362Sbenjsc * Read the eeprom to find out what channels are valid for the given
3279173362Sbenjsc * band and update net80211 with what we find.
3280173362Sbenjsc */
3281173362Sbenjscstatic void
3282173362Sbenjscwpi_read_eeprom_channels(struct wpi_softc *sc, int n)
3283173362Sbenjsc{
3284178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3285178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3286173362Sbenjsc	const struct wpi_chan_band *band = &wpi_bands[n];
3287173362Sbenjsc	struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND];
3288179957Sthompsa	struct ieee80211_channel *c;
3289179957Sthompsa	int chan, i, passive;
3290173362Sbenjsc
3291173362Sbenjsc	wpi_read_prom_data(sc, band->addr, channels,
3292173362Sbenjsc	    band->nchan * sizeof (struct wpi_eeprom_chan));
3293173362Sbenjsc
3294173362Sbenjsc	for (i = 0; i < band->nchan; i++) {
3295173362Sbenjsc		if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
3296173362Sbenjsc			DPRINTFN(WPI_DEBUG_HW,
3297173362Sbenjsc			    ("Channel Not Valid: %d, band %d\n",
3298173362Sbenjsc			     band->chan[i],n));
3299173362Sbenjsc			continue;
3300173362Sbenjsc		}
3301173362Sbenjsc
3302173362Sbenjsc		passive = 0;
3303173362Sbenjsc		chan = band->chan[i];
3304179957Sthompsa		c = &ic->ic_channels[ic->ic_nchans++];
3305173362Sbenjsc
3306173362Sbenjsc		/* is active scan allowed on this channel? */
3307173362Sbenjsc		if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) {
3308173362Sbenjsc			passive = IEEE80211_CHAN_PASSIVE;
3309173362Sbenjsc		}
3310173362Sbenjsc
3311173362Sbenjsc		if (n == 0) {	/* 2GHz band */
3312179957Sthompsa			c->ic_ieee = chan;
3313179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3314179957Sthompsa			    IEEE80211_CHAN_2GHZ);
3315179957Sthompsa			c->ic_flags = IEEE80211_CHAN_B | passive;
3316173362Sbenjsc
3317179957Sthompsa			c = &ic->ic_channels[ic->ic_nchans++];
3318179957Sthompsa			c->ic_ieee = chan;
3319179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3320179957Sthompsa			    IEEE80211_CHAN_2GHZ);
3321179957Sthompsa			c->ic_flags = IEEE80211_CHAN_G | passive;
3322179957Sthompsa
3323173362Sbenjsc		} else {	/* 5GHz band */
3324173362Sbenjsc			/*
3325173362Sbenjsc			 * Some 3945ABG adapters support channels 7, 8, 11
3326173362Sbenjsc			 * and 12 in the 2GHz *and* 5GHz bands.
3327173362Sbenjsc			 * Because of limitations in our net80211(9) stack,
3328173362Sbenjsc			 * we can't support these channels in 5GHz band.
3329173362Sbenjsc			 * XXX not true; just need to map to proper frequency
3330173362Sbenjsc			 */
3331173362Sbenjsc			if (chan <= 14)
3332173362Sbenjsc				continue;
3333173362Sbenjsc
3334179957Sthompsa			c->ic_ieee = chan;
3335179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3336179957Sthompsa			    IEEE80211_CHAN_5GHZ);
3337179957Sthompsa			c->ic_flags = IEEE80211_CHAN_A | passive;
3338173362Sbenjsc		}
3339173362Sbenjsc
3340173362Sbenjsc		/* save maximum allowed power for this channel */
3341173362Sbenjsc		sc->maxpwr[chan] = channels[i].maxpwr;
3342173362Sbenjsc
3343173362Sbenjsc#if 0
3344173362Sbenjsc		// XXX We can probably use this an get rid of maxpwr - ben 20070617
3345173362Sbenjsc		ic->ic_channels[chan].ic_maxpower = channels[i].maxpwr;
3346173362Sbenjsc		//ic->ic_channels[chan].ic_minpower...
3347173362Sbenjsc		//ic->ic_channels[chan].ic_maxregtxpower...
3348173362Sbenjsc#endif
3349173362Sbenjsc
3350179957Sthompsa		DPRINTF(("adding chan %d (%dMHz) flags=0x%x maxpwr=%d"
3351179957Sthompsa		    " passive=%d, offset %d\n", chan, c->ic_freq,
3352179957Sthompsa		    channels[i].flags, sc->maxpwr[chan],
3353179957Sthompsa		    (c->ic_flags & IEEE80211_CHAN_PASSIVE) != 0,
3354179957Sthompsa		    ic->ic_nchans));
3355173362Sbenjsc	}
3356173362Sbenjsc}
3357173362Sbenjsc
3358173362Sbenjscstatic void
3359173362Sbenjscwpi_read_eeprom_group(struct wpi_softc *sc, int n)
3360173362Sbenjsc{
3361173362Sbenjsc	struct wpi_power_group *group = &sc->groups[n];
3362173362Sbenjsc	struct wpi_eeprom_group rgroup;
3363173362Sbenjsc	int i;
3364173362Sbenjsc
3365173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup,
3366173362Sbenjsc	    sizeof rgroup);
3367173362Sbenjsc
3368173362Sbenjsc	/* save power group information */
3369173362Sbenjsc	group->chan   = rgroup.chan;
3370173362Sbenjsc	group->maxpwr = rgroup.maxpwr;
3371173362Sbenjsc	/* temperature at which the samples were taken */
3372173362Sbenjsc	group->temp   = (int16_t)le16toh(rgroup.temp);
3373173362Sbenjsc
3374173362Sbenjsc	DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n,
3375173362Sbenjsc		    group->chan, group->maxpwr, group->temp));
3376173362Sbenjsc
3377173362Sbenjsc	for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
3378173362Sbenjsc		group->samples[i].index = rgroup.samples[i].index;
3379173362Sbenjsc		group->samples[i].power = rgroup.samples[i].power;
3380173362Sbenjsc
3381173362Sbenjsc		DPRINTF(("\tsample %d: index=%d power=%d\n", i,
3382173362Sbenjsc			    group->samples[i].index, group->samples[i].power));
3383173362Sbenjsc	}
3384173362Sbenjsc}
3385173362Sbenjsc
3386173362Sbenjsc/*
3387173362Sbenjsc * Update Tx power to match what is defined for channel `c'.
3388173362Sbenjsc */
3389173362Sbenjscstatic int
3390173362Sbenjscwpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async)
3391173362Sbenjsc{
3392178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3393178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3394173362Sbenjsc	struct wpi_power_group *group;
3395173362Sbenjsc	struct wpi_cmd_txpower txpower;
3396173362Sbenjsc	u_int chan;
3397173362Sbenjsc	int i;
3398173362Sbenjsc
3399173362Sbenjsc	/* get channel number */
3400173362Sbenjsc	chan = ieee80211_chan2ieee(ic, c);
3401173362Sbenjsc
3402173362Sbenjsc	/* find the power group to which this channel belongs */
3403173362Sbenjsc	if (IEEE80211_IS_CHAN_5GHZ(c)) {
3404173362Sbenjsc		for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
3405173362Sbenjsc			if (chan <= group->chan)
3406173362Sbenjsc				break;
3407173362Sbenjsc	} else
3408173362Sbenjsc		group = &sc->groups[0];
3409173362Sbenjsc
3410173362Sbenjsc	memset(&txpower, 0, sizeof txpower);
3411173362Sbenjsc	txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1;
3412173362Sbenjsc	txpower.channel = htole16(chan);
3413173362Sbenjsc
3414173362Sbenjsc	/* set Tx power for all OFDM and CCK rates */
3415173362Sbenjsc	for (i = 0; i <= 11 ; i++) {
3416173362Sbenjsc		/* retrieve Tx power for this channel/rate combination */
3417173362Sbenjsc		int idx = wpi_get_power_index(sc, group, c,
3418173362Sbenjsc		    wpi_ridx_to_rate[i]);
3419173362Sbenjsc
3420173362Sbenjsc		txpower.rates[i].rate = wpi_ridx_to_plcp[i];
3421173362Sbenjsc
3422173362Sbenjsc		if (IEEE80211_IS_CHAN_5GHZ(c)) {
3423173362Sbenjsc			txpower.rates[i].gain_radio = wpi_rf_gain_5ghz[idx];
3424173362Sbenjsc			txpower.rates[i].gain_dsp = wpi_dsp_gain_5ghz[idx];
3425173362Sbenjsc		} else {
3426173362Sbenjsc			txpower.rates[i].gain_radio = wpi_rf_gain_2ghz[idx];
3427173362Sbenjsc			txpower.rates[i].gain_dsp = wpi_dsp_gain_2ghz[idx];
3428173362Sbenjsc		}
3429173362Sbenjsc		DPRINTFN(WPI_DEBUG_TEMP,("chan %d/rate %d: power index %d\n",
3430173362Sbenjsc			    chan, wpi_ridx_to_rate[i], idx));
3431173362Sbenjsc	}
3432173362Sbenjsc
3433173362Sbenjsc	return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async);
3434173362Sbenjsc}
3435173362Sbenjsc
3436173362Sbenjsc/*
3437173362Sbenjsc * Determine Tx power index for a given channel/rate combination.
3438173362Sbenjsc * This takes into account the regulatory information from EEPROM and the
3439173362Sbenjsc * current temperature.
3440173362Sbenjsc */
3441173362Sbenjscstatic int
3442173362Sbenjscwpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
3443173362Sbenjsc    struct ieee80211_channel *c, int rate)
3444173362Sbenjsc{
3445173362Sbenjsc/* fixed-point arithmetic division using a n-bit fractional part */
3446173362Sbenjsc#define fdivround(a, b, n)      \
3447173362Sbenjsc	((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3448173362Sbenjsc
3449173362Sbenjsc/* linear interpolation */
3450173362Sbenjsc#define interpolate(x, x1, y1, x2, y2, n)       \
3451173362Sbenjsc	((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3452173362Sbenjsc
3453178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3454178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3455173362Sbenjsc	struct wpi_power_sample *sample;
3456173362Sbenjsc	int pwr, idx;
3457173362Sbenjsc	u_int chan;
3458173362Sbenjsc
3459173362Sbenjsc	/* get channel number */
3460173362Sbenjsc	chan = ieee80211_chan2ieee(ic, c);
3461173362Sbenjsc
3462173362Sbenjsc	/* default power is group's maximum power - 3dB */
3463173362Sbenjsc	pwr = group->maxpwr / 2;
3464173362Sbenjsc
3465173362Sbenjsc	/* decrease power for highest OFDM rates to reduce distortion */
3466173362Sbenjsc	switch (rate) {
3467173362Sbenjsc		case 72:	/* 36Mb/s */
3468173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 :  5;
3469173362Sbenjsc			break;
3470173362Sbenjsc		case 96:	/* 48Mb/s */
3471173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10;
3472173362Sbenjsc			break;
3473173362Sbenjsc		case 108:	/* 54Mb/s */
3474173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12;
3475173362Sbenjsc			break;
3476173362Sbenjsc	}
3477173362Sbenjsc
3478173362Sbenjsc	/* never exceed channel's maximum allowed Tx power */
3479173362Sbenjsc	pwr = min(pwr, sc->maxpwr[chan]);
3480173362Sbenjsc
3481173362Sbenjsc	/* retrieve power index into gain tables from samples */
3482173362Sbenjsc	for (sample = group->samples; sample < &group->samples[3]; sample++)
3483173362Sbenjsc		if (pwr > sample[1].power)
3484173362Sbenjsc			break;
3485173362Sbenjsc	/* fixed-point linear interpolation using a 19-bit fractional part */
3486173362Sbenjsc	idx = interpolate(pwr, sample[0].power, sample[0].index,
3487173362Sbenjsc	    sample[1].power, sample[1].index, 19);
3488173362Sbenjsc
3489173362Sbenjsc	/*
3490173362Sbenjsc	 *  Adjust power index based on current temperature
3491173362Sbenjsc	 *	- if colder than factory-calibrated: decreate output power
3492173362Sbenjsc	 *	- if warmer than factory-calibrated: increase output power
3493173362Sbenjsc	 */
3494173362Sbenjsc	idx -= (sc->temp - group->temp) * 11 / 100;
3495173362Sbenjsc
3496173362Sbenjsc	/* decrease power for CCK rates (-5dB) */
3497173362Sbenjsc	if (!WPI_RATE_IS_OFDM(rate))
3498173362Sbenjsc		idx += 10;
3499173362Sbenjsc
3500173362Sbenjsc	/* keep power index in a valid range */
3501173362Sbenjsc	if (idx < 0)
3502173362Sbenjsc		return 0;
3503173362Sbenjsc	if (idx > WPI_MAX_PWR_INDEX)
3504173362Sbenjsc		return WPI_MAX_PWR_INDEX;
3505173362Sbenjsc	return idx;
3506173362Sbenjsc
3507173362Sbenjsc#undef interpolate
3508173362Sbenjsc#undef fdivround
3509173362Sbenjsc}
3510173362Sbenjsc
3511173362Sbenjsc/**
3512173362Sbenjsc * Called by net80211 framework to indicate that a scan
3513173362Sbenjsc * is starting. This function doesn't actually do the scan,
3514173362Sbenjsc * wpi_scan_curchan starts things off. This function is more
3515173362Sbenjsc * of an early warning from the framework we should get ready
3516173362Sbenjsc * for the scan.
3517173362Sbenjsc */
3518173362Sbenjscstatic void
3519173362Sbenjscwpi_scan_start(struct ieee80211com *ic)
3520173362Sbenjsc{
3521173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
3522173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3523173362Sbenjsc
3524191746Sthompsa	WPI_LOCK(sc);
3525191746Sthompsa	wpi_set_led(sc, WPI_LED_LINK, 20, 2);
3526191746Sthompsa	WPI_UNLOCK(sc);
3527173362Sbenjsc}
3528173362Sbenjsc
3529173362Sbenjsc/**
3530173362Sbenjsc * Called by the net80211 framework, indicates that the
3531173362Sbenjsc * scan has ended. If there is a scan in progress on the card
3532173362Sbenjsc * then it should be aborted.
3533173362Sbenjsc */
3534173362Sbenjscstatic void
3535173362Sbenjscwpi_scan_end(struct ieee80211com *ic)
3536173362Sbenjsc{
3537191746Sthompsa	/* XXX ignore */
3538173362Sbenjsc}
3539173362Sbenjsc
3540173362Sbenjsc/**
3541173362Sbenjsc * Called by the net80211 framework to indicate to the driver
3542173362Sbenjsc * that the channel should be changed
3543173362Sbenjsc */
3544173362Sbenjscstatic void
3545173362Sbenjscwpi_set_channel(struct ieee80211com *ic)
3546173362Sbenjsc{
3547173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
3548173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3549191746Sthompsa	int error;
3550173362Sbenjsc
3551177043Sthompsa	/*
3552177043Sthompsa	 * Only need to set the channel in Monitor mode. AP scanning and auth
3553177043Sthompsa	 * are already taken care of by their respective firmware commands.
3554177043Sthompsa	 */
3555191746Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
3556191746Sthompsa		error = wpi_config(sc);
3557191746Sthompsa		if (error != 0)
3558191746Sthompsa			device_printf(sc->sc_dev,
3559191746Sthompsa			    "error %d settting channel\n", error);
3560191746Sthompsa	}
3561173362Sbenjsc}
3562173362Sbenjsc
3563173362Sbenjsc/**
3564173362Sbenjsc * Called by net80211 to indicate that we need to scan the current
3565173362Sbenjsc * channel. The channel is previously be set via the wpi_set_channel
3566173362Sbenjsc * callback.
3567173362Sbenjsc */
3568173362Sbenjscstatic void
3569178354Ssamwpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
3570173362Sbenjsc{
3571178354Ssam	struct ieee80211vap *vap = ss->ss_vap;
3572178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
3573173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3574173362Sbenjsc
3575191746Sthompsa	WPI_LOCK(sc);
3576191746Sthompsa	if (wpi_scan(sc))
3577191746Sthompsa		ieee80211_cancel_scan(vap);
3578191746Sthompsa	WPI_UNLOCK(sc);
3579173362Sbenjsc}
3580173362Sbenjsc
3581173362Sbenjsc/**
3582173362Sbenjsc * Called by the net80211 framework to indicate
3583173362Sbenjsc * the minimum dwell time has been met, terminate the scan.
3584173362Sbenjsc * We don't actually terminate the scan as the firmware will notify
3585173362Sbenjsc * us when it's finished and we have no way to interrupt it.
3586173362Sbenjsc */
3587173362Sbenjscstatic void
3588178354Ssamwpi_scan_mindwell(struct ieee80211_scan_state *ss)
3589173362Sbenjsc{
3590173362Sbenjsc	/* NB: don't try to abort scan; wait for firmware to finish */
3591173362Sbenjsc}
3592173362Sbenjsc
3593173362Sbenjscstatic void
3594191746Sthompsawpi_hwreset(void *arg, int pending)
3595173362Sbenjsc{
3596191746Sthompsa	struct wpi_softc *sc = arg;
3597173362Sbenjsc
3598173362Sbenjsc	WPI_LOCK(sc);
3599191746Sthompsa	wpi_init_locked(sc, 0);
3600173362Sbenjsc	WPI_UNLOCK(sc);
3601173362Sbenjsc}
3602173362Sbenjsc
3603191746Sthompsastatic void
3604191746Sthompsawpi_rfreset(void *arg, int pending)
3605173362Sbenjsc{
3606191746Sthompsa	struct wpi_softc *sc = arg;
3607173362Sbenjsc
3608191746Sthompsa	WPI_LOCK(sc);
3609191746Sthompsa	wpi_rfkill_resume(sc);
3610191746Sthompsa	WPI_UNLOCK(sc);
3611173362Sbenjsc}
3612173362Sbenjsc
3613173362Sbenjsc/*
3614173362Sbenjsc * Allocate DMA-safe memory for firmware transfer.
3615173362Sbenjsc */
3616173362Sbenjscstatic int
3617173362Sbenjscwpi_alloc_fwmem(struct wpi_softc *sc)
3618173362Sbenjsc{
3619173362Sbenjsc	/* allocate enough contiguous space to store text and data */
3620173362Sbenjsc	return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL,
3621173362Sbenjsc	    WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 1,
3622173362Sbenjsc	    BUS_DMA_NOWAIT);
3623173362Sbenjsc}
3624173362Sbenjsc
3625173362Sbenjscstatic void
3626173362Sbenjscwpi_free_fwmem(struct wpi_softc *sc)
3627173362Sbenjsc{
3628173362Sbenjsc	wpi_dma_contig_free(&sc->fw_dma);
3629173362Sbenjsc}
3630173362Sbenjsc
3631173362Sbenjsc/**
3632177043Sthompsa * Called every second, wpi_watchdog used by the watch dog timer
3633173362Sbenjsc * to check that the card is still alive
3634173362Sbenjsc */
3635173362Sbenjscstatic void
3636177043Sthompsawpi_watchdog(void *arg)
3637173362Sbenjsc{
3638173362Sbenjsc	struct wpi_softc *sc = arg;
3639177043Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3640191746Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
3641177043Sthompsa	uint32_t tmp;
3642173362Sbenjsc
3643173362Sbenjsc	DPRINTFN(WPI_DEBUG_WATCHDOG,("Watchdog: tick\n"));
3644173362Sbenjsc
3645177043Sthompsa	if (sc->flags & WPI_FLAG_HW_RADIO_OFF) {
3646177043Sthompsa		/* No need to lock firmware memory */
3647177043Sthompsa		tmp = wpi_mem_read(sc, WPI_MEM_HW_RADIO_OFF);
3648177043Sthompsa
3649177043Sthompsa		if ((tmp & 0x1) == 0) {
3650177043Sthompsa			/* Radio kill switch is still off */
3651177043Sthompsa			callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3652177043Sthompsa			return;
3653177043Sthompsa		}
3654177043Sthompsa
3655177043Sthompsa		device_printf(sc->sc_dev, "Hardware Switch Enabled\n");
3656191746Sthompsa		ieee80211_runtask(ic, &sc->sc_radiotask);
3657177043Sthompsa		return;
3658177043Sthompsa	}
3659177043Sthompsa
3660177043Sthompsa	if (sc->sc_tx_timer > 0) {
3661177043Sthompsa		if (--sc->sc_tx_timer == 0) {
3662177043Sthompsa			device_printf(sc->sc_dev,"device timeout\n");
3663177043Sthompsa			ifp->if_oerrors++;
3664191746Sthompsa			ieee80211_runtask(ic, &sc->sc_restarttask);
3665177043Sthompsa		}
3666177043Sthompsa	}
3667177043Sthompsa	if (sc->sc_scan_timer > 0) {
3668178354Ssam		struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3669178354Ssam		if (--sc->sc_scan_timer == 0 && vap != NULL) {
3670177043Sthompsa			device_printf(sc->sc_dev,"scan timeout\n");
3671178354Ssam			ieee80211_cancel_scan(vap);
3672191746Sthompsa			ieee80211_runtask(ic, &sc->sc_restarttask);
3673177043Sthompsa		}
3674177043Sthompsa	}
3675177043Sthompsa
3676177043Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3677177043Sthompsa		callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3678173362Sbenjsc}
3679173362Sbenjsc
3680173362Sbenjsc#ifdef WPI_DEBUG
3681173362Sbenjscstatic const char *wpi_cmd_str(int cmd)
3682173362Sbenjsc{
3683178354Ssam	switch (cmd) {
3684178354Ssam	case WPI_DISABLE_CMD:	return "WPI_DISABLE_CMD";
3685178354Ssam	case WPI_CMD_CONFIGURE:	return "WPI_CMD_CONFIGURE";
3686178354Ssam	case WPI_CMD_ASSOCIATE:	return "WPI_CMD_ASSOCIATE";
3687178354Ssam	case WPI_CMD_SET_WME:	return "WPI_CMD_SET_WME";
3688178354Ssam	case WPI_CMD_TSF:	return "WPI_CMD_TSF";
3689178354Ssam	case WPI_CMD_ADD_NODE:	return "WPI_CMD_ADD_NODE";
3690178354Ssam	case WPI_CMD_TX_DATA:	return "WPI_CMD_TX_DATA";
3691178354Ssam	case WPI_CMD_MRR_SETUP:	return "WPI_CMD_MRR_SETUP";
3692178354Ssam	case WPI_CMD_SET_LED:	return "WPI_CMD_SET_LED";
3693178354Ssam	case WPI_CMD_SET_POWER_MODE: return "WPI_CMD_SET_POWER_MODE";
3694178354Ssam	case WPI_CMD_SCAN:	return "WPI_CMD_SCAN";
3695178354Ssam	case WPI_CMD_SET_BEACON:return "WPI_CMD_SET_BEACON";
3696178354Ssam	case WPI_CMD_TXPOWER:	return "WPI_CMD_TXPOWER";
3697178354Ssam	case WPI_CMD_BLUETOOTH:	return "WPI_CMD_BLUETOOTH";
3698173362Sbenjsc
3699178354Ssam	default:
3700173362Sbenjsc		KASSERT(1, ("Unknown Command: %d\n", cmd));
3701178354Ssam		return "UNKNOWN CMD";	/* Make the compiler happy */
3702173362Sbenjsc	}
3703173362Sbenjsc}
3704173362Sbenjsc#endif
3705173362Sbenjsc
3706173362SbenjscMODULE_DEPEND(wpi, pci,  1, 1, 1);
3707173362SbenjscMODULE_DEPEND(wpi, wlan, 1, 1, 1);
3708173362SbenjscMODULE_DEPEND(wpi, firmware, 1, 1, 1);
3709173362SbenjscMODULE_DEPEND(wpi, wlan_amrr, 1, 1, 1);
3710