if_wpi.c revision 206358
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 206358 2010-04-07 15:29:13Z rpaulo $");
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>
96206358Srpaulo#include <net80211/ieee80211_ratectl.h>
97173362Sbenjsc
98173362Sbenjsc#include <netinet/in.h>
99173362Sbenjsc#include <netinet/in_systm.h>
100173362Sbenjsc#include <netinet/in_var.h>
101173362Sbenjsc#include <netinet/ip.h>
102173362Sbenjsc#include <netinet/if_ether.h>
103173362Sbenjsc
104173362Sbenjsc#include <dev/wpi/if_wpireg.h>
105173362Sbenjsc#include <dev/wpi/if_wpivar.h>
106173362Sbenjsc
107199037Sdougb#define WPI_DEBUG
108199037Sdougb
109173362Sbenjsc#ifdef WPI_DEBUG
110173362Sbenjsc#define DPRINTF(x)	do { if (wpi_debug != 0) printf x; } while (0)
111173362Sbenjsc#define DPRINTFN(n, x)	do { if (wpi_debug & n) printf x; } while (0)
112179957Sthompsa#define	WPI_DEBUG_SET	(wpi_debug != 0)
113173362Sbenjsc
114173362Sbenjscenum {
115173362Sbenjsc	WPI_DEBUG_UNUSED	= 0x00000001,   /* Unused */
116173362Sbenjsc	WPI_DEBUG_HW		= 0x00000002,   /* Stage 1 (eeprom) debugging */
117173362Sbenjsc	WPI_DEBUG_TX		= 0x00000004,   /* Stage 2 TX intrp debugging*/
118173362Sbenjsc	WPI_DEBUG_RX		= 0x00000008,   /* Stage 2 RX intrp debugging */
119173362Sbenjsc	WPI_DEBUG_CMD		= 0x00000010,   /* Stage 2 CMD intrp debugging*/
120173362Sbenjsc	WPI_DEBUG_FIRMWARE	= 0x00000020,   /* firmware(9) loading debug  */
121173362Sbenjsc	WPI_DEBUG_DMA		= 0x00000040,   /* DMA (de)allocations/syncs  */
122173362Sbenjsc	WPI_DEBUG_SCANNING	= 0x00000080,   /* Stage 2 Scanning debugging */
123173362Sbenjsc	WPI_DEBUG_NOTIFY	= 0x00000100,   /* State 2 Noftif intr debug */
124173362Sbenjsc	WPI_DEBUG_TEMP		= 0x00000200,   /* TXPower/Temp Calibration */
125173362Sbenjsc	WPI_DEBUG_OPS		= 0x00000400,   /* wpi_ops taskq debug */
126173362Sbenjsc	WPI_DEBUG_WATCHDOG	= 0x00000800,   /* Watch dog debug */
127173362Sbenjsc	WPI_DEBUG_ANY		= 0xffffffff
128173362Sbenjsc};
129173362Sbenjsc
130199037Sdougbstatic int wpi_debug = 0;
131173362SbenjscSYSCTL_INT(_debug, OID_AUTO, wpi, CTLFLAG_RW, &wpi_debug, 0, "wpi debug level");
132179957SthompsaTUNABLE_INT("debug.wpi", &wpi_debug);
133173362Sbenjsc
134173362Sbenjsc#else
135173362Sbenjsc#define DPRINTF(x)
136173362Sbenjsc#define DPRINTFN(n, x)
137179957Sthompsa#define WPI_DEBUG_SET	0
138173362Sbenjsc#endif
139173362Sbenjsc
140173362Sbenjscstruct wpi_ident {
141173362Sbenjsc	uint16_t	vendor;
142173362Sbenjsc	uint16_t	device;
143173362Sbenjsc	uint16_t	subdevice;
144173362Sbenjsc	const char	*name;
145173362Sbenjsc};
146173362Sbenjsc
147173362Sbenjscstatic const struct wpi_ident wpi_ident_table[] = {
148173362Sbenjsc	/* The below entries support ABG regardless of the subid */
149173362Sbenjsc	{ 0x8086, 0x4222,    0x0, "Intel(R) PRO/Wireless 3945ABG" },
150173362Sbenjsc	{ 0x8086, 0x4227,    0x0, "Intel(R) PRO/Wireless 3945ABG" },
151173362Sbenjsc	/* The below entries only support BG */
152182127Sbenjsc	{ 0x8086, 0x4222, 0x1005, "Intel(R) PRO/Wireless 3945BG"  },
153182127Sbenjsc	{ 0x8086, 0x4222, 0x1034, "Intel(R) PRO/Wireless 3945BG"  },
154182127Sbenjsc	{ 0x8086, 0x4227, 0x1014, "Intel(R) PRO/Wireless 3945BG"  },
155182127Sbenjsc	{ 0x8086, 0x4222, 0x1044, "Intel(R) PRO/Wireless 3945BG"  },
156173362Sbenjsc	{ 0, 0, 0, NULL }
157173362Sbenjsc};
158173362Sbenjsc
159178354Ssamstatic struct ieee80211vap *wpi_vap_create(struct ieee80211com *,
160178354Ssam		    const char name[IFNAMSIZ], int unit, int opmode,
161178354Ssam		    int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
162178354Ssam		    const uint8_t mac[IEEE80211_ADDR_LEN]);
163178354Ssamstatic void	wpi_vap_delete(struct ieee80211vap *);
164173362Sbenjscstatic int	wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *,
165173362Sbenjsc		    void **, bus_size_t, bus_size_t, int);
166173362Sbenjscstatic void	wpi_dma_contig_free(struct wpi_dma_info *);
167173362Sbenjscstatic void	wpi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
168173362Sbenjscstatic int	wpi_alloc_shared(struct wpi_softc *);
169173362Sbenjscstatic void	wpi_free_shared(struct wpi_softc *);
170173362Sbenjscstatic int	wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
171173362Sbenjscstatic void	wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
172173362Sbenjscstatic void	wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
173173362Sbenjscstatic int	wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
174173362Sbenjsc		    int, int);
175173362Sbenjscstatic void	wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
176173362Sbenjscstatic void	wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
177178354Ssamstatic int	wpi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
178173362Sbenjscstatic void	wpi_mem_lock(struct wpi_softc *);
179173362Sbenjscstatic void	wpi_mem_unlock(struct wpi_softc *);
180173362Sbenjscstatic uint32_t	wpi_mem_read(struct wpi_softc *, uint16_t);
181173362Sbenjscstatic void	wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t);
182173362Sbenjscstatic void	wpi_mem_write_region_4(struct wpi_softc *, uint16_t,
183173362Sbenjsc		    const uint32_t *, int);
184173362Sbenjscstatic uint16_t	wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
185173362Sbenjscstatic int	wpi_alloc_fwmem(struct wpi_softc *);
186173362Sbenjscstatic void	wpi_free_fwmem(struct wpi_softc *);
187173362Sbenjscstatic int	wpi_load_firmware(struct wpi_softc *);
188173362Sbenjscstatic void	wpi_unload_firmware(struct wpi_softc *);
189173362Sbenjscstatic int	wpi_load_microcode(struct wpi_softc *, const uint8_t *, int);
190173362Sbenjscstatic void	wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *,
191173362Sbenjsc		    struct wpi_rx_data *);
192173362Sbenjscstatic void	wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *);
193173362Sbenjscstatic void	wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *);
194173362Sbenjscstatic void	wpi_notif_intr(struct wpi_softc *);
195173362Sbenjscstatic void	wpi_intr(void *);
196173362Sbenjscstatic uint8_t	wpi_plcp_signal(int);
197177043Sthompsastatic void	wpi_watchdog(void *);
198173362Sbenjscstatic int	wpi_tx_data(struct wpi_softc *, struct mbuf *,
199173362Sbenjsc		    struct ieee80211_node *, int);
200173362Sbenjscstatic void	wpi_start(struct ifnet *);
201178354Ssamstatic void	wpi_start_locked(struct ifnet *);
202178354Ssamstatic int	wpi_raw_xmit(struct ieee80211_node *, struct mbuf *,
203178354Ssam		    const struct ieee80211_bpf_params *);
204173362Sbenjscstatic void	wpi_scan_start(struct ieee80211com *);
205173362Sbenjscstatic void	wpi_scan_end(struct ieee80211com *);
206173362Sbenjscstatic void	wpi_set_channel(struct ieee80211com *);
207178354Ssamstatic void	wpi_scan_curchan(struct ieee80211_scan_state *, unsigned long);
208178354Ssamstatic void	wpi_scan_mindwell(struct ieee80211_scan_state *);
209173362Sbenjscstatic int	wpi_ioctl(struct ifnet *, u_long, caddr_t);
210190526Ssamstatic void	wpi_read_eeprom(struct wpi_softc *,
211190526Ssam		    uint8_t macaddr[IEEE80211_ADDR_LEN]);
212173362Sbenjscstatic void	wpi_read_eeprom_channels(struct wpi_softc *, int);
213173362Sbenjscstatic void	wpi_read_eeprom_group(struct wpi_softc *, int);
214173362Sbenjscstatic int	wpi_cmd(struct wpi_softc *, int, const void *, int, int);
215173362Sbenjscstatic int	wpi_wme_update(struct ieee80211com *);
216173362Sbenjscstatic int	wpi_mrr_setup(struct wpi_softc *);
217173362Sbenjscstatic void	wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
218173362Sbenjscstatic void	wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *);
219173362Sbenjsc#if 0
220173362Sbenjscstatic int	wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
221173362Sbenjsc#endif
222178354Ssamstatic int	wpi_auth(struct wpi_softc *, struct ieee80211vap *);
223178354Ssamstatic int	wpi_run(struct wpi_softc *, struct ieee80211vap *);
224173362Sbenjscstatic int	wpi_scan(struct wpi_softc *);
225173362Sbenjscstatic int	wpi_config(struct wpi_softc *);
226173362Sbenjscstatic void	wpi_stop_master(struct wpi_softc *);
227173362Sbenjscstatic int	wpi_power_up(struct wpi_softc *);
228173362Sbenjscstatic int	wpi_reset(struct wpi_softc *);
229191746Sthompsastatic void	wpi_hwreset(void *, int);
230191746Sthompsastatic void	wpi_rfreset(void *, int);
231173362Sbenjscstatic void	wpi_hw_config(struct wpi_softc *);
232173362Sbenjscstatic void	wpi_init(void *);
233177043Sthompsastatic void	wpi_init_locked(struct wpi_softc *, int);
234173362Sbenjscstatic void	wpi_stop(struct wpi_softc *);
235173362Sbenjscstatic void	wpi_stop_locked(struct wpi_softc *);
236173362Sbenjsc
237173362Sbenjscstatic int	wpi_set_txpower(struct wpi_softc *, struct ieee80211_channel *,
238173362Sbenjsc		    int);
239173362Sbenjscstatic void	wpi_calib_timeout(void *);
240173362Sbenjscstatic void	wpi_power_calibration(struct wpi_softc *, int);
241173362Sbenjscstatic int	wpi_get_power_index(struct wpi_softc *,
242173362Sbenjsc		    struct wpi_power_group *, struct ieee80211_channel *, int);
243179957Sthompsa#ifdef WPI_DEBUG
244173362Sbenjscstatic const char *wpi_cmd_str(int);
245179957Sthompsa#endif
246173362Sbenjscstatic int wpi_probe(device_t);
247173362Sbenjscstatic int wpi_attach(device_t);
248173362Sbenjscstatic int wpi_detach(device_t);
249173362Sbenjscstatic int wpi_shutdown(device_t);
250173362Sbenjscstatic int wpi_suspend(device_t);
251173362Sbenjscstatic int wpi_resume(device_t);
252173362Sbenjsc
253173362Sbenjsc
254173362Sbenjscstatic device_method_t wpi_methods[] = {
255173362Sbenjsc	/* Device interface */
256173362Sbenjsc	DEVMETHOD(device_probe,		wpi_probe),
257173362Sbenjsc	DEVMETHOD(device_attach,	wpi_attach),
258173362Sbenjsc	DEVMETHOD(device_detach,	wpi_detach),
259173362Sbenjsc	DEVMETHOD(device_shutdown,	wpi_shutdown),
260173362Sbenjsc	DEVMETHOD(device_suspend,	wpi_suspend),
261173362Sbenjsc	DEVMETHOD(device_resume,	wpi_resume),
262173362Sbenjsc
263173362Sbenjsc	{ 0, 0 }
264173362Sbenjsc};
265173362Sbenjsc
266173362Sbenjscstatic driver_t wpi_driver = {
267173362Sbenjsc	"wpi",
268173362Sbenjsc	wpi_methods,
269173362Sbenjsc	sizeof (struct wpi_softc)
270173362Sbenjsc};
271173362Sbenjsc
272173362Sbenjscstatic devclass_t wpi_devclass;
273173362Sbenjsc
274173362SbenjscDRIVER_MODULE(wpi, pci, wpi_driver, wpi_devclass, 0, 0);
275173362Sbenjsc
276173362Sbenjscstatic const uint8_t wpi_ridx_to_plcp[] = {
277173362Sbenjsc	/* OFDM: IEEE Std 802.11a-1999, pp. 14 Table 80 */
278173362Sbenjsc	/* R1-R4 (ral/ural is R4-R1) */
279173362Sbenjsc	0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3,
280173362Sbenjsc	/* CCK: device-dependent */
281173362Sbenjsc	10, 20, 55, 110
282173362Sbenjsc};
283173362Sbenjscstatic const uint8_t wpi_ridx_to_rate[] = {
284173362Sbenjsc	12, 18, 24, 36, 48, 72, 96, 108, /* OFDM */
285173362Sbenjsc	2, 4, 11, 22 /*CCK */
286173362Sbenjsc};
287173362Sbenjsc
288173362Sbenjsc
289173362Sbenjscstatic int
290173362Sbenjscwpi_probe(device_t dev)
291173362Sbenjsc{
292173362Sbenjsc	const struct wpi_ident *ident;
293173362Sbenjsc
294173362Sbenjsc	for (ident = wpi_ident_table; ident->name != NULL; ident++) {
295173362Sbenjsc		if (pci_get_vendor(dev) == ident->vendor &&
296173362Sbenjsc		    pci_get_device(dev) == ident->device) {
297173362Sbenjsc			device_set_desc(dev, ident->name);
298173362Sbenjsc			return 0;
299173362Sbenjsc		}
300173362Sbenjsc	}
301173362Sbenjsc	return ENXIO;
302173362Sbenjsc}
303173362Sbenjsc
304173362Sbenjsc/**
305173362Sbenjsc * Load the firmare image from disk to the allocated dma buffer.
306173362Sbenjsc * we also maintain the reference to the firmware pointer as there
307173362Sbenjsc * is times where we may need to reload the firmware but we are not
308173362Sbenjsc * in a context that can access the filesystem (ie taskq cause by restart)
309173362Sbenjsc *
310173362Sbenjsc * @return 0 on success, an errno on failure
311173362Sbenjsc */
312173362Sbenjscstatic int
313173362Sbenjscwpi_load_firmware(struct wpi_softc *sc)
314173362Sbenjsc{
315178354Ssam	const struct firmware *fp;
316173362Sbenjsc	struct wpi_dma_info *dma = &sc->fw_dma;
317173362Sbenjsc	const struct wpi_firmware_hdr *hdr;
318173362Sbenjsc	const uint8_t *itext, *idata, *rtext, *rdata, *btext;
319173362Sbenjsc	uint32_t itextsz, idatasz, rtextsz, rdatasz, btextsz;
320173362Sbenjsc	int error;
321173362Sbenjsc
322173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
323173362Sbenjsc	    ("Attempting Loading Firmware from wpi_fw module\n"));
324173362Sbenjsc
325173362Sbenjsc	WPI_UNLOCK(sc);
326173362Sbenjsc
327173362Sbenjsc	if (sc->fw_fp == NULL && (sc->fw_fp = firmware_get("wpifw")) == NULL) {
328173362Sbenjsc		device_printf(sc->sc_dev,
329173362Sbenjsc		    "could not load firmware image 'wpifw'\n");
330173362Sbenjsc		error = ENOENT;
331173362Sbenjsc		WPI_LOCK(sc);
332173362Sbenjsc		goto fail;
333173362Sbenjsc	}
334173362Sbenjsc
335173362Sbenjsc	fp = sc->fw_fp;
336173362Sbenjsc
337173362Sbenjsc	WPI_LOCK(sc);
338173362Sbenjsc
339173362Sbenjsc	/* Validate the firmware is minimum a particular version */
340173362Sbenjsc	if (fp->version < WPI_FW_MINVERSION) {
341173362Sbenjsc	    device_printf(sc->sc_dev,
342173362Sbenjsc			   "firmware version is too old. Need %d, got %d\n",
343173362Sbenjsc			   WPI_FW_MINVERSION,
344173362Sbenjsc			   fp->version);
345173362Sbenjsc	    error = ENXIO;
346173362Sbenjsc	    goto fail;
347173362Sbenjsc	}
348173362Sbenjsc
349173362Sbenjsc	if (fp->datasize < sizeof (struct wpi_firmware_hdr)) {
350173362Sbenjsc		device_printf(sc->sc_dev,
351173362Sbenjsc		    "firmware file too short: %zu bytes\n", fp->datasize);
352173362Sbenjsc		error = ENXIO;
353173362Sbenjsc		goto fail;
354173362Sbenjsc	}
355173362Sbenjsc
356173362Sbenjsc	hdr = (const struct wpi_firmware_hdr *)fp->data;
357173362Sbenjsc
358173362Sbenjsc	/*     |  RUNTIME FIRMWARE   |    INIT FIRMWARE    | BOOT FW  |
359173362Sbenjsc	   |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */
360173362Sbenjsc
361173362Sbenjsc	rtextsz = le32toh(hdr->rtextsz);
362173362Sbenjsc	rdatasz = le32toh(hdr->rdatasz);
363173362Sbenjsc	itextsz = le32toh(hdr->itextsz);
364173362Sbenjsc	idatasz = le32toh(hdr->idatasz);
365173362Sbenjsc	btextsz = le32toh(hdr->btextsz);
366173362Sbenjsc
367173362Sbenjsc	/* check that all firmware segments are present */
368173362Sbenjsc	if (fp->datasize < sizeof (struct wpi_firmware_hdr) +
369173362Sbenjsc		rtextsz + rdatasz + itextsz + idatasz + btextsz) {
370173362Sbenjsc		device_printf(sc->sc_dev,
371173362Sbenjsc		    "firmware file too short: %zu bytes\n", fp->datasize);
372173362Sbenjsc		error = ENXIO; /* XXX appropriate error code? */
373173362Sbenjsc		goto fail;
374173362Sbenjsc	}
375173362Sbenjsc
376173362Sbenjsc	/* get pointers to firmware segments */
377173362Sbenjsc	rtext = (const uint8_t *)(hdr + 1);
378173362Sbenjsc	rdata = rtext + rtextsz;
379173362Sbenjsc	itext = rdata + rdatasz;
380173362Sbenjsc	idata = itext + itextsz;
381173362Sbenjsc	btext = idata + idatasz;
382173362Sbenjsc
383173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
384173362Sbenjsc	    ("Firmware Version: Major %d, Minor %d, Driver %d, \n"
385173362Sbenjsc	     "runtime (text: %u, data: %u) init (text: %u, data %u) boot (text %u)\n",
386173362Sbenjsc	     (le32toh(hdr->version) & 0xff000000) >> 24,
387173362Sbenjsc	     (le32toh(hdr->version) & 0x00ff0000) >> 16,
388173362Sbenjsc	     (le32toh(hdr->version) & 0x0000ffff),
389173362Sbenjsc	     rtextsz, rdatasz,
390173362Sbenjsc	     itextsz, idatasz, btextsz));
391173362Sbenjsc
392173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("rtext 0x%x\n", *(const uint32_t *)rtext));
393173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("rdata 0x%x\n", *(const uint32_t *)rdata));
394173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("itext 0x%x\n", *(const uint32_t *)itext));
395173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("idata 0x%x\n", *(const uint32_t *)idata));
396173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,("btext 0x%x\n", *(const uint32_t *)btext));
397173362Sbenjsc
398173362Sbenjsc	/* sanity checks */
399173362Sbenjsc	if (rtextsz > WPI_FW_MAIN_TEXT_MAXSZ ||
400173362Sbenjsc	    rdatasz > WPI_FW_MAIN_DATA_MAXSZ ||
401173362Sbenjsc	    itextsz > WPI_FW_INIT_TEXT_MAXSZ ||
402173362Sbenjsc	    idatasz > WPI_FW_INIT_DATA_MAXSZ ||
403173362Sbenjsc	    btextsz > WPI_FW_BOOT_TEXT_MAXSZ ||
404173362Sbenjsc	    (btextsz & 3) != 0) {
405173362Sbenjsc		device_printf(sc->sc_dev, "firmware invalid\n");
406173362Sbenjsc		error = EINVAL;
407173362Sbenjsc		goto fail;
408173362Sbenjsc	}
409173362Sbenjsc
410173362Sbenjsc	/* copy initialization images into pre-allocated DMA-safe memory */
411173362Sbenjsc	memcpy(dma->vaddr, idata, idatasz);
412173362Sbenjsc	memcpy(dma->vaddr + WPI_FW_INIT_DATA_MAXSZ, itext, itextsz);
413173362Sbenjsc
414173362Sbenjsc	bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
415173362Sbenjsc
416173362Sbenjsc	/* tell adapter where to find initialization images */
417173362Sbenjsc	wpi_mem_lock(sc);
418173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
419173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, idatasz);
420173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
421173362Sbenjsc	    dma->paddr + WPI_FW_INIT_DATA_MAXSZ);
422173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, itextsz);
423173362Sbenjsc	wpi_mem_unlock(sc);
424173362Sbenjsc
425173362Sbenjsc	/* load firmware boot code */
426173362Sbenjsc	if ((error = wpi_load_microcode(sc, btext, btextsz)) != 0) {
427173362Sbenjsc	    device_printf(sc->sc_dev, "Failed to load microcode\n");
428173362Sbenjsc	    goto fail;
429173362Sbenjsc	}
430173362Sbenjsc
431173362Sbenjsc	/* now press "execute" */
432173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, 0);
433173362Sbenjsc
434173362Sbenjsc	/* wait at most one second for the first alive notification */
435173362Sbenjsc	if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
436173362Sbenjsc		device_printf(sc->sc_dev,
437173362Sbenjsc		    "timeout waiting for adapter to initialize\n");
438173362Sbenjsc		goto fail;
439173362Sbenjsc	}
440173362Sbenjsc
441173362Sbenjsc	/* copy runtime images into pre-allocated DMA-sage memory */
442173362Sbenjsc	memcpy(dma->vaddr, rdata, rdatasz);
443173362Sbenjsc	memcpy(dma->vaddr + WPI_FW_MAIN_DATA_MAXSZ, rtext, rtextsz);
444173362Sbenjsc	bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
445173362Sbenjsc
446173362Sbenjsc	/* tell adapter where to find runtime images */
447173362Sbenjsc	wpi_mem_lock(sc);
448173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr);
449173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_DATA_SIZE, rdatasz);
450173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_BASE,
451173362Sbenjsc	    dma->paddr + WPI_FW_MAIN_DATA_MAXSZ);
452173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, WPI_FW_UPDATED | rtextsz);
453173362Sbenjsc	wpi_mem_unlock(sc);
454173362Sbenjsc
455173362Sbenjsc	/* wait at most one second for the first alive notification */
456173362Sbenjsc	if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
457173362Sbenjsc		device_printf(sc->sc_dev,
458173362Sbenjsc		    "timeout waiting for adapter to initialize2\n");
459173362Sbenjsc		goto fail;
460173362Sbenjsc	}
461173362Sbenjsc
462173362Sbenjsc	DPRINTFN(WPI_DEBUG_FIRMWARE,
463173362Sbenjsc	    ("Firmware loaded to driver successfully\n"));
464173362Sbenjsc	return error;
465173362Sbenjscfail:
466173362Sbenjsc	wpi_unload_firmware(sc);
467173362Sbenjsc	return error;
468173362Sbenjsc}
469173362Sbenjsc
470173362Sbenjsc/**
471173362Sbenjsc * Free the referenced firmware image
472173362Sbenjsc */
473173362Sbenjscstatic void
474173362Sbenjscwpi_unload_firmware(struct wpi_softc *sc)
475173362Sbenjsc{
476173362Sbenjsc
477173362Sbenjsc	if (sc->fw_fp) {
478173362Sbenjsc		WPI_UNLOCK(sc);
479173362Sbenjsc		firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
480173362Sbenjsc		WPI_LOCK(sc);
481173362Sbenjsc		sc->fw_fp = NULL;
482173362Sbenjsc	}
483173362Sbenjsc}
484173362Sbenjsc
485173362Sbenjscstatic int
486173362Sbenjscwpi_attach(device_t dev)
487173362Sbenjsc{
488173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
489173362Sbenjsc	struct ifnet *ifp;
490178354Ssam	struct ieee80211com *ic;
491173362Sbenjsc	int ac, error, supportsa = 1;
492173362Sbenjsc	uint32_t tmp;
493173362Sbenjsc	const struct wpi_ident *ident;
494190526Ssam	uint8_t macaddr[IEEE80211_ADDR_LEN];
495173362Sbenjsc
496173362Sbenjsc	sc->sc_dev = dev;
497173362Sbenjsc
498179957Sthompsa	if (bootverbose || WPI_DEBUG_SET)
499173362Sbenjsc	    device_printf(sc->sc_dev,"Driver Revision %s\n", VERSION);
500173362Sbenjsc
501173362Sbenjsc	/*
502173362Sbenjsc	 * Some card's only support 802.11b/g not a, check to see if
503173362Sbenjsc	 * this is one such card. A 0x0 in the subdevice table indicates
504173362Sbenjsc	 * the entire subdevice range is to be ignored.
505173362Sbenjsc	 */
506173362Sbenjsc	for (ident = wpi_ident_table; ident->name != NULL; ident++) {
507173362Sbenjsc		if (ident->subdevice &&
508173362Sbenjsc		    pci_get_subdevice(dev) == ident->subdevice) {
509173362Sbenjsc		    supportsa = 0;
510173362Sbenjsc		    break;
511173362Sbenjsc		}
512173362Sbenjsc	}
513173362Sbenjsc
514173362Sbenjsc	/* Create the tasks that can be queued */
515191746Sthompsa	TASK_INIT(&sc->sc_restarttask, 0, wpi_hwreset, sc);
516191746Sthompsa	TASK_INIT(&sc->sc_radiotask, 0, wpi_rfreset, sc);
517173362Sbenjsc
518173362Sbenjsc	WPI_LOCK_INIT(sc);
519173362Sbenjsc
520173362Sbenjsc	callout_init_mtx(&sc->calib_to, &sc->sc_mtx, 0);
521173362Sbenjsc	callout_init_mtx(&sc->watchdog_to, &sc->sc_mtx, 0);
522173362Sbenjsc
523173362Sbenjsc	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
524173362Sbenjsc		device_printf(dev, "chip is in D%d power mode "
525173362Sbenjsc		    "-- setting to D0\n", pci_get_powerstate(dev));
526173362Sbenjsc		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
527173362Sbenjsc	}
528173362Sbenjsc
529173362Sbenjsc	/* disable the retry timeout register */
530173362Sbenjsc	pci_write_config(dev, 0x41, 0, 1);
531173362Sbenjsc
532173362Sbenjsc	/* enable bus-mastering */
533173362Sbenjsc	pci_enable_busmaster(dev);
534173362Sbenjsc
535173362Sbenjsc	sc->mem_rid = PCIR_BAR(0);
536173362Sbenjsc	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
537173362Sbenjsc	    RF_ACTIVE);
538173362Sbenjsc	if (sc->mem == NULL) {
539173362Sbenjsc		device_printf(dev, "could not allocate memory resource\n");
540173362Sbenjsc		error = ENOMEM;
541173362Sbenjsc		goto fail;
542173362Sbenjsc	}
543173362Sbenjsc
544173362Sbenjsc	sc->sc_st = rman_get_bustag(sc->mem);
545173362Sbenjsc	sc->sc_sh = rman_get_bushandle(sc->mem);
546173362Sbenjsc
547173362Sbenjsc	sc->irq_rid = 0;
548173362Sbenjsc	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
549173362Sbenjsc	    RF_ACTIVE | RF_SHAREABLE);
550173362Sbenjsc	if (sc->irq == NULL) {
551173362Sbenjsc		device_printf(dev, "could not allocate interrupt resource\n");
552173362Sbenjsc		error = ENOMEM;
553173362Sbenjsc		goto fail;
554173362Sbenjsc	}
555173362Sbenjsc
556173362Sbenjsc	/*
557173362Sbenjsc	 * Allocate DMA memory for firmware transfers.
558173362Sbenjsc	 */
559173362Sbenjsc	if ((error = wpi_alloc_fwmem(sc)) != 0) {
560173362Sbenjsc		printf(": could not allocate firmware memory\n");
561173362Sbenjsc		error = ENOMEM;
562173362Sbenjsc		goto fail;
563173362Sbenjsc	}
564173362Sbenjsc
565173362Sbenjsc	/*
566173362Sbenjsc	 * Put adapter into a known state.
567173362Sbenjsc	 */
568173362Sbenjsc	if ((error = wpi_reset(sc)) != 0) {
569173362Sbenjsc		device_printf(dev, "could not reset adapter\n");
570173362Sbenjsc		goto fail;
571173362Sbenjsc	}
572173362Sbenjsc
573173362Sbenjsc	wpi_mem_lock(sc);
574173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
575179957Sthompsa	if (bootverbose || WPI_DEBUG_SET)
576173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Revision (0x%X)\n", tmp);
577173362Sbenjsc
578173362Sbenjsc	wpi_mem_unlock(sc);
579173362Sbenjsc
580173362Sbenjsc	/* Allocate shared page */
581173362Sbenjsc	if ((error = wpi_alloc_shared(sc)) != 0) {
582173362Sbenjsc		device_printf(dev, "could not allocate shared page\n");
583173362Sbenjsc		goto fail;
584173362Sbenjsc	}
585173362Sbenjsc
586173362Sbenjsc	/* tx data queues  - 4 for QoS purposes */
587173362Sbenjsc	for (ac = 0; ac < WME_NUM_AC; ac++) {
588173362Sbenjsc		error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, ac);
589173362Sbenjsc		if (error != 0) {
590173362Sbenjsc		    device_printf(dev, "could not allocate Tx ring %d\n",ac);
591173362Sbenjsc		    goto fail;
592173362Sbenjsc		}
593173362Sbenjsc	}
594173362Sbenjsc
595173362Sbenjsc	/* command queue to talk to the card's firmware */
596173362Sbenjsc	error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4);
597173362Sbenjsc	if (error != 0) {
598173362Sbenjsc		device_printf(dev, "could not allocate command ring\n");
599173362Sbenjsc		goto fail;
600173362Sbenjsc	}
601173362Sbenjsc
602173362Sbenjsc	/* receive data queue */
603173362Sbenjsc	error = wpi_alloc_rx_ring(sc, &sc->rxq);
604173362Sbenjsc	if (error != 0) {
605173362Sbenjsc		device_printf(dev, "could not allocate Rx ring\n");
606173362Sbenjsc		goto fail;
607173362Sbenjsc	}
608173362Sbenjsc
609178354Ssam	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
610173362Sbenjsc	if (ifp == NULL) {
611173362Sbenjsc		device_printf(dev, "can not if_alloc()\n");
612173362Sbenjsc		error = ENOMEM;
613173362Sbenjsc		goto fail;
614173362Sbenjsc	}
615178354Ssam	ic = ifp->if_l2com;
616173362Sbenjsc
617173362Sbenjsc	ic->ic_ifp = ifp;
618178354Ssam	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
619178354Ssam	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
620173362Sbenjsc
621173362Sbenjsc	/* set device capabilities */
622173362Sbenjsc	ic->ic_caps =
623178957Ssam		  IEEE80211_C_STA		/* station mode supported */
624178957Ssam		| IEEE80211_C_MONITOR		/* monitor mode supported */
625173362Sbenjsc		| IEEE80211_C_TXPMGT		/* tx power management */
626173362Sbenjsc		| IEEE80211_C_SHSLOT		/* short slot time supported */
627173362Sbenjsc		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
628173362Sbenjsc		| IEEE80211_C_WPA		/* 802.11i */
629173362Sbenjsc/* XXX looks like WME is partly supported? */
630173362Sbenjsc#if 0
631173362Sbenjsc		| IEEE80211_C_IBSS		/* IBSS mode support */
632173362Sbenjsc		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
633173362Sbenjsc		| IEEE80211_C_WME		/* 802.11e */
634173362Sbenjsc		| IEEE80211_C_HOSTAP		/* Host access point mode */
635173362Sbenjsc#endif
636173362Sbenjsc		;
637173362Sbenjsc
638173362Sbenjsc	/*
639173362Sbenjsc	 * Read in the eeprom and also setup the channels for
640173362Sbenjsc	 * net80211. We don't set the rates as net80211 does this for us
641173362Sbenjsc	 */
642190526Ssam	wpi_read_eeprom(sc, macaddr);
643173362Sbenjsc
644179957Sthompsa	if (bootverbose || WPI_DEBUG_SET) {
645173362Sbenjsc	    device_printf(sc->sc_dev, "Regulatory Domain: %.4s\n", sc->domain);
646173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Type: %c\n",
647173362Sbenjsc			  sc->type > 1 ? 'B': '?');
648173362Sbenjsc	    device_printf(sc->sc_dev, "Hardware Revision: %c\n",
649173362Sbenjsc			  ((le16toh(sc->rev) & 0xf0) == 0xd0) ? 'D': '?');
650173362Sbenjsc	    device_printf(sc->sc_dev, "SKU %s support 802.11a\n",
651173362Sbenjsc			  supportsa ? "does" : "does not");
652173362Sbenjsc
653173362Sbenjsc	    /* XXX hw_config uses the PCIDEV for the Hardware rev. Must check
654173362Sbenjsc	       what sc->rev really represents - benjsc 20070615 */
655173362Sbenjsc	}
656173362Sbenjsc
657173362Sbenjsc	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
658173362Sbenjsc	ifp->if_softc = sc;
659173362Sbenjsc	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
660173362Sbenjsc	ifp->if_init = wpi_init;
661173362Sbenjsc	ifp->if_ioctl = wpi_ioctl;
662173362Sbenjsc	ifp->if_start = wpi_start;
663173362Sbenjsc	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
664173362Sbenjsc	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
665173362Sbenjsc	IFQ_SET_READY(&ifp->if_snd);
666178354Ssam
667190526Ssam	ieee80211_ifattach(ic, macaddr);
668173362Sbenjsc	/* override default methods */
669178354Ssam	ic->ic_raw_xmit = wpi_raw_xmit;
670173362Sbenjsc	ic->ic_wme.wme_update = wpi_wme_update;
671173362Sbenjsc	ic->ic_scan_start = wpi_scan_start;
672173362Sbenjsc	ic->ic_scan_end = wpi_scan_end;
673173362Sbenjsc	ic->ic_set_channel = wpi_set_channel;
674173362Sbenjsc	ic->ic_scan_curchan = wpi_scan_curchan;
675173362Sbenjsc	ic->ic_scan_mindwell = wpi_scan_mindwell;
676173362Sbenjsc
677178354Ssam	ic->ic_vap_create = wpi_vap_create;
678178354Ssam	ic->ic_vap_delete = wpi_vap_delete;
679173362Sbenjsc
680192468Ssam	ieee80211_radiotap_attach(ic,
681192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
682192468Ssam		WPI_TX_RADIOTAP_PRESENT,
683192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
684192468Ssam		WPI_RX_RADIOTAP_PRESENT);
685173362Sbenjsc
686173362Sbenjsc	/*
687173362Sbenjsc	 * Hook our interrupt after all initialization is complete.
688173362Sbenjsc	 */
689177043Sthompsa	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET |INTR_MPSAFE,
690177043Sthompsa	    NULL, wpi_intr, sc, &sc->sc_ih);
691173362Sbenjsc	if (error != 0) {
692173362Sbenjsc		device_printf(dev, "could not set up interrupt\n");
693173362Sbenjsc		goto fail;
694173362Sbenjsc	}
695173362Sbenjsc
696177043Sthompsa	if (bootverbose)
697177043Sthompsa		ieee80211_announce(ic);
698173362Sbenjsc#ifdef XXX_DEBUG
699173362Sbenjsc	ieee80211_announce_channels(ic);
700173362Sbenjsc#endif
701173362Sbenjsc	return 0;
702173362Sbenjsc
703173362Sbenjscfail:	wpi_detach(dev);
704173362Sbenjsc	return ENXIO;
705173362Sbenjsc}
706173362Sbenjsc
707173362Sbenjscstatic int
708173362Sbenjscwpi_detach(device_t dev)
709173362Sbenjsc{
710173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
711178354Ssam	struct ifnet *ifp = sc->sc_ifp;
712200530Sgavin	struct ieee80211com *ic;
713173362Sbenjsc	int ac;
714173362Sbenjsc
715200530Sgavin	if (ifp != NULL) {
716200530Sgavin		ic = ifp->if_l2com;
717191746Sthompsa
718200530Sgavin		ieee80211_draintask(ic, &sc->sc_restarttask);
719200530Sgavin		ieee80211_draintask(ic, &sc->sc_radiotask);
720173362Sbenjsc		wpi_stop(sc);
721173362Sbenjsc		callout_drain(&sc->watchdog_to);
722173362Sbenjsc		callout_drain(&sc->calib_to);
723173362Sbenjsc		ieee80211_ifdetach(ic);
724173362Sbenjsc	}
725173362Sbenjsc
726173362Sbenjsc	WPI_LOCK(sc);
727173362Sbenjsc	if (sc->txq[0].data_dmat) {
728173362Sbenjsc		for (ac = 0; ac < WME_NUM_AC; ac++)
729173362Sbenjsc			wpi_free_tx_ring(sc, &sc->txq[ac]);
730173362Sbenjsc
731173362Sbenjsc		wpi_free_tx_ring(sc, &sc->cmdq);
732173362Sbenjsc		wpi_free_rx_ring(sc, &sc->rxq);
733173362Sbenjsc		wpi_free_shared(sc);
734173362Sbenjsc	}
735173362Sbenjsc
736173362Sbenjsc	if (sc->fw_fp != NULL) {
737173362Sbenjsc		wpi_unload_firmware(sc);
738173362Sbenjsc	}
739173362Sbenjsc
740173362Sbenjsc	if (sc->fw_dma.tag)
741173362Sbenjsc		wpi_free_fwmem(sc);
742173362Sbenjsc	WPI_UNLOCK(sc);
743173362Sbenjsc
744173362Sbenjsc	if (sc->irq != NULL) {
745173362Sbenjsc		bus_teardown_intr(dev, sc->irq, sc->sc_ih);
746173362Sbenjsc		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
747173362Sbenjsc	}
748173362Sbenjsc
749173362Sbenjsc	if (sc->mem != NULL)
750173362Sbenjsc		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
751173362Sbenjsc
752173362Sbenjsc	if (ifp != NULL)
753173362Sbenjsc		if_free(ifp);
754173362Sbenjsc
755173362Sbenjsc	WPI_LOCK_DESTROY(sc);
756173362Sbenjsc
757173362Sbenjsc	return 0;
758173362Sbenjsc}
759173362Sbenjsc
760178354Ssamstatic struct ieee80211vap *
761178354Ssamwpi_vap_create(struct ieee80211com *ic,
762178354Ssam	const char name[IFNAMSIZ], int unit, int opmode, int flags,
763178354Ssam	const uint8_t bssid[IEEE80211_ADDR_LEN],
764178354Ssam	const uint8_t mac[IEEE80211_ADDR_LEN])
765178354Ssam{
766178354Ssam	struct wpi_vap *wvp;
767178354Ssam	struct ieee80211vap *vap;
768178354Ssam
769178354Ssam	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
770178354Ssam		return NULL;
771178354Ssam	wvp = (struct wpi_vap *) malloc(sizeof(struct wpi_vap),
772178354Ssam	    M_80211_VAP, M_NOWAIT | M_ZERO);
773178354Ssam	if (wvp == NULL)
774178354Ssam		return NULL;
775178354Ssam	vap = &wvp->vap;
776178354Ssam	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
777178354Ssam	/* override with driver methods */
778178354Ssam	wvp->newstate = vap->iv_newstate;
779178354Ssam	vap->iv_newstate = wpi_newstate;
780178354Ssam
781206358Srpaulo	ieee80211_ratectl_init(vap);
782178354Ssam	/* complete setup */
783178354Ssam	ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
784178354Ssam	ic->ic_opmode = opmode;
785178354Ssam	return vap;
786178354Ssam}
787178354Ssam
788173362Sbenjscstatic void
789178354Ssamwpi_vap_delete(struct ieee80211vap *vap)
790178354Ssam{
791178354Ssam	struct wpi_vap *wvp = WPI_VAP(vap);
792178354Ssam
793206358Srpaulo	ieee80211_ratectl_deinit(vap);
794178354Ssam	ieee80211_vap_detach(vap);
795178354Ssam	free(wvp, M_80211_VAP);
796178354Ssam}
797178354Ssam
798178354Ssamstatic void
799173362Sbenjscwpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
800173362Sbenjsc{
801173362Sbenjsc	if (error != 0)
802173362Sbenjsc		return;
803173362Sbenjsc
804173362Sbenjsc	KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
805173362Sbenjsc
806173362Sbenjsc	*(bus_addr_t *)arg = segs[0].ds_addr;
807173362Sbenjsc}
808173362Sbenjsc
809177043Sthompsa/*
810177043Sthompsa * Allocates a contiguous block of dma memory of the requested size and
811177043Sthompsa * alignment. Due to limitations of the FreeBSD dma subsystem as of 20071217,
812177043Sthompsa * allocations greater than 4096 may fail. Hence if the requested alignment is
813177043Sthompsa * greater we allocate 'alignment' size extra memory and shift the vaddr and
814177043Sthompsa * paddr after the dma load. This bypasses the problem at the cost of a little
815177043Sthompsa * more memory.
816177043Sthompsa */
817173362Sbenjscstatic int
818173362Sbenjscwpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
819173362Sbenjsc    void **kvap, bus_size_t size, bus_size_t alignment, int flags)
820173362Sbenjsc{
821173362Sbenjsc	int error;
822177043Sthompsa	bus_size_t align;
823177043Sthompsa	bus_size_t reqsize;
824173362Sbenjsc
825173362Sbenjsc	DPRINTFN(WPI_DEBUG_DMA,
826177043Sthompsa	    ("Size: %zd - alignment %zd\n", size, alignment));
827173362Sbenjsc
828173362Sbenjsc	dma->size = size;
829173362Sbenjsc	dma->tag = NULL;
830173362Sbenjsc
831177043Sthompsa	if (alignment > 4096) {
832177043Sthompsa		align = PAGE_SIZE;
833177043Sthompsa		reqsize = size + alignment;
834177043Sthompsa	} else {
835177043Sthompsa		align = alignment;
836177043Sthompsa		reqsize = size;
837177043Sthompsa	}
838177043Sthompsa	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), align,
839173362Sbenjsc	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
840177043Sthompsa	    NULL, NULL, reqsize,
841177043Sthompsa	    1, reqsize, flags,
842173362Sbenjsc	    NULL, NULL, &dma->tag);
843173362Sbenjsc	if (error != 0) {
844173362Sbenjsc		device_printf(sc->sc_dev,
845173362Sbenjsc		    "could not create shared page DMA tag\n");
846173362Sbenjsc		goto fail;
847173362Sbenjsc	}
848177043Sthompsa	error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr_start,
849173362Sbenjsc	    flags | BUS_DMA_ZERO, &dma->map);
850173362Sbenjsc	if (error != 0) {
851173362Sbenjsc		device_printf(sc->sc_dev,
852173362Sbenjsc		    "could not allocate shared page DMA memory\n");
853173362Sbenjsc		goto fail;
854173362Sbenjsc	}
855173362Sbenjsc
856177043Sthompsa	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr_start,
857177043Sthompsa	    reqsize,  wpi_dma_map_addr, &dma->paddr_start, flags);
858177043Sthompsa
859177043Sthompsa	/* Save the original pointers so we can free all the memory */
860177043Sthompsa	dma->paddr = dma->paddr_start;
861177043Sthompsa	dma->vaddr = dma->vaddr_start;
862177043Sthompsa
863177043Sthompsa	/*
864177043Sthompsa	 * Check the alignment and increment by 4096 until we get the
865177043Sthompsa	 * requested alignment. Fail if can't obtain the alignment
866177043Sthompsa	 * we requested.
867173362Sbenjsc	 */
868177043Sthompsa	if ((dma->paddr & (alignment -1 )) != 0) {
869177043Sthompsa		int i;
870173362Sbenjsc
871177043Sthompsa		for (i = 0; i < alignment / 4096; i++) {
872177043Sthompsa			if ((dma->paddr & (alignment - 1 )) == 0)
873177043Sthompsa				break;
874177043Sthompsa			dma->paddr += 4096;
875177043Sthompsa			dma->vaddr += 4096;
876177043Sthompsa		}
877177043Sthompsa		if (i == alignment / 4096) {
878177043Sthompsa			device_printf(sc->sc_dev,
879177043Sthompsa			    "alignment requirement was not satisfied\n");
880177043Sthompsa			goto fail;
881177043Sthompsa		}
882173362Sbenjsc	}
883173362Sbenjsc
884173362Sbenjsc	if (error != 0) {
885173362Sbenjsc		device_printf(sc->sc_dev,
886173362Sbenjsc		    "could not load shared page DMA map\n");
887173362Sbenjsc		goto fail;
888173362Sbenjsc	}
889173362Sbenjsc
890173362Sbenjsc	if (kvap != NULL)
891173362Sbenjsc		*kvap = dma->vaddr;
892173362Sbenjsc
893173362Sbenjsc	return 0;
894173362Sbenjsc
895173362Sbenjscfail:
896173362Sbenjsc	wpi_dma_contig_free(dma);
897173362Sbenjsc	return error;
898173362Sbenjsc}
899173362Sbenjsc
900173362Sbenjscstatic void
901173362Sbenjscwpi_dma_contig_free(struct wpi_dma_info *dma)
902173362Sbenjsc{
903173362Sbenjsc	if (dma->tag) {
904173362Sbenjsc		if (dma->map != NULL) {
905177043Sthompsa			if (dma->paddr_start != 0) {
906173362Sbenjsc				bus_dmamap_sync(dma->tag, dma->map,
907173362Sbenjsc				    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
908173362Sbenjsc				bus_dmamap_unload(dma->tag, dma->map);
909173362Sbenjsc			}
910177043Sthompsa			bus_dmamem_free(dma->tag, &dma->vaddr_start, dma->map);
911173362Sbenjsc		}
912173362Sbenjsc		bus_dma_tag_destroy(dma->tag);
913173362Sbenjsc	}
914173362Sbenjsc}
915173362Sbenjsc
916173362Sbenjsc/*
917173362Sbenjsc * Allocate a shared page between host and NIC.
918173362Sbenjsc */
919173362Sbenjscstatic int
920173362Sbenjscwpi_alloc_shared(struct wpi_softc *sc)
921173362Sbenjsc{
922173362Sbenjsc	int error;
923173362Sbenjsc
924173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &sc->shared_dma,
925173362Sbenjsc	    (void **)&sc->shared, sizeof (struct wpi_shared),
926173362Sbenjsc	    PAGE_SIZE,
927173362Sbenjsc	    BUS_DMA_NOWAIT);
928173362Sbenjsc
929173362Sbenjsc	if (error != 0) {
930173362Sbenjsc		device_printf(sc->sc_dev,
931173362Sbenjsc		    "could not allocate shared area DMA memory\n");
932173362Sbenjsc	}
933173362Sbenjsc
934173362Sbenjsc	return error;
935173362Sbenjsc}
936173362Sbenjsc
937173362Sbenjscstatic void
938173362Sbenjscwpi_free_shared(struct wpi_softc *sc)
939173362Sbenjsc{
940173362Sbenjsc	wpi_dma_contig_free(&sc->shared_dma);
941173362Sbenjsc}
942173362Sbenjsc
943173362Sbenjscstatic int
944173362Sbenjscwpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
945173362Sbenjsc{
946173362Sbenjsc
947173362Sbenjsc	int i, error;
948173362Sbenjsc
949173362Sbenjsc	ring->cur = 0;
950173362Sbenjsc
951173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
952177043Sthompsa	    (void **)&ring->desc, WPI_RX_RING_COUNT * sizeof (uint32_t),
953177043Sthompsa	    WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
954173362Sbenjsc
955173362Sbenjsc	if (error != 0) {
956177043Sthompsa		device_printf(sc->sc_dev,
957177043Sthompsa		    "%s: could not allocate rx ring DMA memory, error %d\n",
958177043Sthompsa		    __func__, error);
959177043Sthompsa		goto fail;
960173362Sbenjsc	}
961173362Sbenjsc
962177043Sthompsa        error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
963177043Sthompsa	    BUS_SPACE_MAXADDR_32BIT,
964177043Sthompsa            BUS_SPACE_MAXADDR, NULL, NULL, MJUMPAGESIZE, 1,
965177043Sthompsa            MJUMPAGESIZE, BUS_DMA_NOWAIT, NULL, NULL, &ring->data_dmat);
966177043Sthompsa        if (error != 0) {
967177043Sthompsa                device_printf(sc->sc_dev,
968177043Sthompsa		    "%s: bus_dma_tag_create_failed, error %d\n",
969177043Sthompsa		    __func__, error);
970177043Sthompsa                goto fail;
971177043Sthompsa        }
972177043Sthompsa
973173362Sbenjsc	/*
974177043Sthompsa	 * Setup Rx buffers.
975173362Sbenjsc	 */
976173362Sbenjsc	for (i = 0; i < WPI_RX_RING_COUNT; i++) {
977177043Sthompsa		struct wpi_rx_data *data = &ring->data[i];
978177043Sthompsa		struct mbuf *m;
979177043Sthompsa		bus_addr_t paddr;
980173362Sbenjsc
981177043Sthompsa		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
982177043Sthompsa		if (error != 0) {
983173362Sbenjsc			device_printf(sc->sc_dev,
984177043Sthompsa			    "%s: bus_dmamap_create failed, error %d\n",
985177043Sthompsa			    __func__, error);
986173362Sbenjsc			goto fail;
987173362Sbenjsc		}
988177043Sthompsa		m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
989177043Sthompsa		if (m == NULL) {
990173362Sbenjsc			device_printf(sc->sc_dev,
991177043Sthompsa			   "%s: could not allocate rx mbuf\n", __func__);
992177043Sthompsa			error = ENOMEM;
993173362Sbenjsc			goto fail;
994173362Sbenjsc		}
995177043Sthompsa		/* map page */
996177043Sthompsa		error = bus_dmamap_load(ring->data_dmat, data->map,
997177043Sthompsa		    mtod(m, caddr_t), MJUMPAGESIZE,
998177043Sthompsa		    wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
999177043Sthompsa		if (error != 0 && error != EFBIG) {
1000177043Sthompsa			device_printf(sc->sc_dev,
1001177043Sthompsa			    "%s: bus_dmamap_load failed, error %d\n",
1002177043Sthompsa			    __func__, error);
1003177043Sthompsa			m_freem(m);
1004177043Sthompsa			error = ENOMEM;	/* XXX unique code */
1005173362Sbenjsc			goto fail;
1006173362Sbenjsc		}
1007177043Sthompsa		bus_dmamap_sync(ring->data_dmat, data->map,
1008177043Sthompsa		    BUS_DMASYNC_PREWRITE);
1009177043Sthompsa
1010177043Sthompsa		data->m = m;
1011177043Sthompsa		ring->desc[i] = htole32(paddr);
1012173362Sbenjsc	}
1013173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1014173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
1015173362Sbenjsc	return 0;
1016173362Sbenjscfail:
1017173362Sbenjsc	wpi_free_rx_ring(sc, ring);
1018173362Sbenjsc	return error;
1019173362Sbenjsc}
1020173362Sbenjsc
1021173362Sbenjscstatic void
1022173362Sbenjscwpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
1023173362Sbenjsc{
1024173362Sbenjsc	int ntries;
1025173362Sbenjsc
1026173362Sbenjsc	wpi_mem_lock(sc);
1027173362Sbenjsc
1028173362Sbenjsc	WPI_WRITE(sc, WPI_RX_CONFIG, 0);
1029173362Sbenjsc
1030173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1031173362Sbenjsc		if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE)
1032173362Sbenjsc			break;
1033173362Sbenjsc		DELAY(10);
1034173362Sbenjsc	}
1035173362Sbenjsc
1036173362Sbenjsc	wpi_mem_unlock(sc);
1037173362Sbenjsc
1038173362Sbenjsc#ifdef WPI_DEBUG
1039179957Sthompsa	if (ntries == 100 && wpi_debug > 0)
1040173362Sbenjsc		device_printf(sc->sc_dev, "timeout resetting Rx ring\n");
1041173362Sbenjsc#endif
1042173362Sbenjsc
1043173362Sbenjsc	ring->cur = 0;
1044173362Sbenjsc}
1045173362Sbenjsc
1046173362Sbenjscstatic void
1047173362Sbenjscwpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
1048173362Sbenjsc{
1049173362Sbenjsc	int i;
1050173362Sbenjsc
1051173362Sbenjsc	wpi_dma_contig_free(&ring->desc_dma);
1052173362Sbenjsc
1053177043Sthompsa	for (i = 0; i < WPI_RX_RING_COUNT; i++)
1054177043Sthompsa		if (ring->data[i].m != NULL)
1055173362Sbenjsc			m_freem(ring->data[i].m);
1056173362Sbenjsc}
1057173362Sbenjsc
1058173362Sbenjscstatic int
1059173362Sbenjscwpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count,
1060173362Sbenjsc	int qid)
1061173362Sbenjsc{
1062173362Sbenjsc	struct wpi_tx_data *data;
1063173362Sbenjsc	int i, error;
1064173362Sbenjsc
1065173362Sbenjsc	ring->qid = qid;
1066173362Sbenjsc	ring->count = count;
1067173362Sbenjsc	ring->queued = 0;
1068173362Sbenjsc	ring->cur = 0;
1069173362Sbenjsc	ring->data = NULL;
1070173362Sbenjsc
1071173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
1072173362Sbenjsc		(void **)&ring->desc, count * sizeof (struct wpi_tx_desc),
1073173362Sbenjsc		WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
1074173362Sbenjsc
1075173362Sbenjsc	if (error != 0) {
1076173362Sbenjsc	    device_printf(sc->sc_dev, "could not allocate tx dma memory\n");
1077173362Sbenjsc	    goto fail;
1078173362Sbenjsc	}
1079173362Sbenjsc
1080173362Sbenjsc	/* update shared page with ring's base address */
1081173362Sbenjsc	sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
1082173362Sbenjsc
1083173362Sbenjsc	error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
1084173362Sbenjsc		count * sizeof (struct wpi_tx_cmd), WPI_RING_DMA_ALIGN,
1085173362Sbenjsc		BUS_DMA_NOWAIT);
1086173362Sbenjsc
1087173362Sbenjsc	if (error != 0) {
1088173362Sbenjsc		device_printf(sc->sc_dev,
1089173362Sbenjsc		    "could not allocate tx command DMA memory\n");
1090173362Sbenjsc		goto fail;
1091173362Sbenjsc	}
1092173362Sbenjsc
1093173362Sbenjsc	ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF,
1094173362Sbenjsc	    M_NOWAIT | M_ZERO);
1095173362Sbenjsc	if (ring->data == NULL) {
1096173362Sbenjsc		device_printf(sc->sc_dev,
1097173362Sbenjsc		    "could not allocate tx data slots\n");
1098173362Sbenjsc		goto fail;
1099173362Sbenjsc	}
1100173362Sbenjsc
1101173362Sbenjsc	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1102173362Sbenjsc	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
1103173362Sbenjsc	    WPI_MAX_SCATTER - 1, MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL,
1104173362Sbenjsc	    &ring->data_dmat);
1105173362Sbenjsc	if (error != 0) {
1106173362Sbenjsc		device_printf(sc->sc_dev, "could not create data DMA tag\n");
1107173362Sbenjsc		goto fail;
1108173362Sbenjsc	}
1109173362Sbenjsc
1110173362Sbenjsc	for (i = 0; i < count; i++) {
1111173362Sbenjsc		data = &ring->data[i];
1112173362Sbenjsc
1113173362Sbenjsc		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1114173362Sbenjsc		if (error != 0) {
1115173362Sbenjsc			device_printf(sc->sc_dev,
1116173362Sbenjsc			    "could not create tx buf DMA map\n");
1117173362Sbenjsc			goto fail;
1118173362Sbenjsc		}
1119173362Sbenjsc		bus_dmamap_sync(ring->data_dmat, data->map,
1120173362Sbenjsc		    BUS_DMASYNC_PREWRITE);
1121173362Sbenjsc	}
1122173362Sbenjsc
1123173362Sbenjsc	return 0;
1124173362Sbenjsc
1125177043Sthompsafail:
1126177043Sthompsa	wpi_free_tx_ring(sc, ring);
1127173362Sbenjsc	return error;
1128173362Sbenjsc}
1129173362Sbenjsc
1130173362Sbenjscstatic void
1131173362Sbenjscwpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1132173362Sbenjsc{
1133173362Sbenjsc	struct wpi_tx_data *data;
1134173362Sbenjsc	int i, ntries;
1135173362Sbenjsc
1136173362Sbenjsc	wpi_mem_lock(sc);
1137173362Sbenjsc
1138173362Sbenjsc	WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0);
1139173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1140173362Sbenjsc		if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid))
1141173362Sbenjsc			break;
1142173362Sbenjsc		DELAY(10);
1143173362Sbenjsc	}
1144173362Sbenjsc#ifdef WPI_DEBUG
1145179957Sthompsa	if (ntries == 100 && wpi_debug > 0)
1146173362Sbenjsc		device_printf(sc->sc_dev, "timeout resetting Tx ring %d\n",
1147173362Sbenjsc		    ring->qid);
1148173362Sbenjsc#endif
1149173362Sbenjsc	wpi_mem_unlock(sc);
1150173362Sbenjsc
1151173362Sbenjsc	for (i = 0; i < ring->count; i++) {
1152173362Sbenjsc		data = &ring->data[i];
1153173362Sbenjsc
1154173362Sbenjsc		if (data->m != NULL) {
1155173362Sbenjsc			bus_dmamap_unload(ring->data_dmat, data->map);
1156173362Sbenjsc			m_freem(data->m);
1157173362Sbenjsc			data->m = NULL;
1158173362Sbenjsc		}
1159173362Sbenjsc	}
1160173362Sbenjsc
1161173362Sbenjsc	ring->queued = 0;
1162173362Sbenjsc	ring->cur = 0;
1163173362Sbenjsc}
1164173362Sbenjsc
1165173362Sbenjscstatic void
1166173362Sbenjscwpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1167173362Sbenjsc{
1168173362Sbenjsc	struct wpi_tx_data *data;
1169173362Sbenjsc	int i;
1170173362Sbenjsc
1171173362Sbenjsc	wpi_dma_contig_free(&ring->desc_dma);
1172173362Sbenjsc	wpi_dma_contig_free(&ring->cmd_dma);
1173173362Sbenjsc
1174173362Sbenjsc	if (ring->data != NULL) {
1175173362Sbenjsc		for (i = 0; i < ring->count; i++) {
1176173362Sbenjsc			data = &ring->data[i];
1177173362Sbenjsc
1178173362Sbenjsc			if (data->m != NULL) {
1179173362Sbenjsc				bus_dmamap_sync(ring->data_dmat, data->map,
1180173362Sbenjsc				    BUS_DMASYNC_POSTWRITE);
1181173362Sbenjsc				bus_dmamap_unload(ring->data_dmat, data->map);
1182173362Sbenjsc				m_freem(data->m);
1183173362Sbenjsc				data->m = NULL;
1184173362Sbenjsc			}
1185173362Sbenjsc		}
1186173362Sbenjsc		free(ring->data, M_DEVBUF);
1187173362Sbenjsc	}
1188173362Sbenjsc
1189173362Sbenjsc	if (ring->data_dmat != NULL)
1190173362Sbenjsc		bus_dma_tag_destroy(ring->data_dmat);
1191173362Sbenjsc}
1192173362Sbenjsc
1193173362Sbenjscstatic int
1194173362Sbenjscwpi_shutdown(device_t dev)
1195173362Sbenjsc{
1196173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1197173362Sbenjsc
1198173362Sbenjsc	WPI_LOCK(sc);
1199173362Sbenjsc	wpi_stop_locked(sc);
1200173362Sbenjsc	wpi_unload_firmware(sc);
1201173362Sbenjsc	WPI_UNLOCK(sc);
1202173362Sbenjsc
1203173362Sbenjsc	return 0;
1204173362Sbenjsc}
1205173362Sbenjsc
1206173362Sbenjscstatic int
1207173362Sbenjscwpi_suspend(device_t dev)
1208173362Sbenjsc{
1209173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1210173362Sbenjsc
1211173362Sbenjsc	wpi_stop(sc);
1212173362Sbenjsc	return 0;
1213173362Sbenjsc}
1214173362Sbenjsc
1215173362Sbenjscstatic int
1216173362Sbenjscwpi_resume(device_t dev)
1217173362Sbenjsc{
1218173362Sbenjsc	struct wpi_softc *sc = device_get_softc(dev);
1219178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1220173362Sbenjsc
1221173362Sbenjsc	pci_write_config(dev, 0x41, 0, 1);
1222173362Sbenjsc
1223173362Sbenjsc	if (ifp->if_flags & IFF_UP) {
1224173362Sbenjsc		wpi_init(ifp->if_softc);
1225173362Sbenjsc		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1226173362Sbenjsc			wpi_start(ifp);
1227173362Sbenjsc	}
1228173362Sbenjsc	return 0;
1229173362Sbenjsc}
1230173362Sbenjsc
1231173362Sbenjsc/**
1232173362Sbenjsc * Called by net80211 when ever there is a change to 80211 state machine
1233173362Sbenjsc */
1234173362Sbenjscstatic int
1235178354Ssamwpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1236173362Sbenjsc{
1237178354Ssam	struct wpi_vap *wvp = WPI_VAP(vap);
1238178354Ssam	struct ieee80211com *ic = vap->iv_ic;
1239173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
1240173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
1241178354Ssam	int error;
1242173362Sbenjsc
1243178354Ssam	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
1244178354Ssam		ieee80211_state_name[vap->iv_state],
1245178354Ssam		ieee80211_state_name[nstate], sc->flags));
1246173362Sbenjsc
1247191746Sthompsa	IEEE80211_UNLOCK(ic);
1248191746Sthompsa	WPI_LOCK(sc);
1249178354Ssam	if (nstate == IEEE80211_S_AUTH) {
1250191746Sthompsa		/* The node must be registered in the firmware before auth */
1251191746Sthompsa		error = wpi_auth(sc, vap);
1252191746Sthompsa		if (error != 0) {
1253191746Sthompsa			device_printf(sc->sc_dev,
1254191746Sthompsa			    "%s: could not move to auth state, error %d\n",
1255191746Sthompsa			    __func__, error);
1256191746Sthompsa		}
1257173362Sbenjsc	}
1258178354Ssam	if (nstate == IEEE80211_S_RUN && vap->iv_state != IEEE80211_S_RUN) {
1259191746Sthompsa		error = wpi_run(sc, vap);
1260191746Sthompsa		if (error != 0) {
1261191746Sthompsa			device_printf(sc->sc_dev,
1262191746Sthompsa			    "%s: could not move to run state, error %d\n",
1263191746Sthompsa			    __func__, error);
1264191746Sthompsa		}
1265178354Ssam	}
1266178354Ssam	if (nstate == IEEE80211_S_RUN) {
1267178354Ssam		/* RUN -> RUN transition; just restart the timers */
1268178354Ssam		wpi_calib_timeout(sc);
1269178354Ssam		/* XXX split out rate control timer */
1270178354Ssam	}
1271191746Sthompsa	WPI_UNLOCK(sc);
1272191746Sthompsa	IEEE80211_LOCK(ic);
1273178354Ssam	return wvp->newstate(vap, nstate, arg);
1274173362Sbenjsc}
1275173362Sbenjsc
1276173362Sbenjsc/*
1277173362Sbenjsc * Grab exclusive access to NIC memory.
1278173362Sbenjsc */
1279173362Sbenjscstatic void
1280173362Sbenjscwpi_mem_lock(struct wpi_softc *sc)
1281173362Sbenjsc{
1282173362Sbenjsc	int ntries;
1283173362Sbenjsc	uint32_t tmp;
1284173362Sbenjsc
1285173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
1286173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC);
1287173362Sbenjsc
1288173362Sbenjsc	/* spin until we actually get the lock */
1289173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
1290173362Sbenjsc		if ((WPI_READ(sc, WPI_GPIO_CTL) &
1291173362Sbenjsc			(WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK)
1292173362Sbenjsc			break;
1293173362Sbenjsc		DELAY(10);
1294173362Sbenjsc	}
1295173362Sbenjsc	if (ntries == 100)
1296173362Sbenjsc		device_printf(sc->sc_dev, "could not lock memory\n");
1297173362Sbenjsc}
1298173362Sbenjsc
1299173362Sbenjsc/*
1300173362Sbenjsc * Release lock on NIC memory.
1301173362Sbenjsc */
1302173362Sbenjscstatic void
1303173362Sbenjscwpi_mem_unlock(struct wpi_softc *sc)
1304173362Sbenjsc{
1305173362Sbenjsc	uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL);
1306173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC);
1307173362Sbenjsc}
1308173362Sbenjsc
1309173362Sbenjscstatic uint32_t
1310173362Sbenjscwpi_mem_read(struct wpi_softc *sc, uint16_t addr)
1311173362Sbenjsc{
1312173362Sbenjsc	WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr);
1313173362Sbenjsc	return WPI_READ(sc, WPI_READ_MEM_DATA);
1314173362Sbenjsc}
1315173362Sbenjsc
1316173362Sbenjscstatic void
1317173362Sbenjscwpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data)
1318173362Sbenjsc{
1319173362Sbenjsc	WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr);
1320173362Sbenjsc	WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data);
1321173362Sbenjsc}
1322173362Sbenjsc
1323173362Sbenjscstatic void
1324173362Sbenjscwpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr,
1325173362Sbenjsc    const uint32_t *data, int wlen)
1326173362Sbenjsc{
1327173362Sbenjsc	for (; wlen > 0; wlen--, data++, addr+=4)
1328173362Sbenjsc		wpi_mem_write(sc, addr, *data);
1329173362Sbenjsc}
1330173362Sbenjsc
1331173362Sbenjsc/*
1332173362Sbenjsc * Read data from the EEPROM.  We access EEPROM through the MAC instead of
1333173362Sbenjsc * using the traditional bit-bang method. Data is read up until len bytes have
1334173362Sbenjsc * been obtained.
1335173362Sbenjsc */
1336173362Sbenjscstatic uint16_t
1337173362Sbenjscwpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int len)
1338173362Sbenjsc{
1339173362Sbenjsc	int ntries;
1340173362Sbenjsc	uint32_t val;
1341173362Sbenjsc	uint8_t *out = data;
1342173362Sbenjsc
1343173362Sbenjsc	wpi_mem_lock(sc);
1344173362Sbenjsc
1345173362Sbenjsc	for (; len > 0; len -= 2, addr++) {
1346173362Sbenjsc		WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2);
1347173362Sbenjsc
1348173362Sbenjsc		for (ntries = 0; ntries < 10; ntries++) {
1349173362Sbenjsc			if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) & WPI_EEPROM_READY)
1350173362Sbenjsc				break;
1351173362Sbenjsc			DELAY(5);
1352173362Sbenjsc		}
1353173362Sbenjsc
1354173362Sbenjsc		if (ntries == 10) {
1355173362Sbenjsc			device_printf(sc->sc_dev, "could not read EEPROM\n");
1356173362Sbenjsc			return ETIMEDOUT;
1357173362Sbenjsc		}
1358173362Sbenjsc
1359173362Sbenjsc		*out++= val >> 16;
1360173362Sbenjsc		if (len > 1)
1361173362Sbenjsc			*out ++= val >> 24;
1362173362Sbenjsc	}
1363173362Sbenjsc
1364173362Sbenjsc	wpi_mem_unlock(sc);
1365173362Sbenjsc
1366173362Sbenjsc	return 0;
1367173362Sbenjsc}
1368173362Sbenjsc
1369173362Sbenjsc/*
1370173362Sbenjsc * The firmware text and data segments are transferred to the NIC using DMA.
1371173362Sbenjsc * The driver just copies the firmware into DMA-safe memory and tells the NIC
1372173362Sbenjsc * where to find it.  Once the NIC has copied the firmware into its internal
1373173362Sbenjsc * memory, we can free our local copy in the driver.
1374173362Sbenjsc */
1375173362Sbenjscstatic int
1376173362Sbenjscwpi_load_microcode(struct wpi_softc *sc, const uint8_t *fw, int size)
1377173362Sbenjsc{
1378173362Sbenjsc	int error, ntries;
1379173362Sbenjsc
1380173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,("Loading microcode  size 0x%x\n", size));
1381173362Sbenjsc
1382173362Sbenjsc	size /= sizeof(uint32_t);
1383173362Sbenjsc
1384173362Sbenjsc	wpi_mem_lock(sc);
1385173362Sbenjsc
1386173362Sbenjsc	wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE,
1387173362Sbenjsc	    (const uint32_t *)fw, size);
1388173362Sbenjsc
1389173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0);
1390173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT);
1391173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size);
1392173362Sbenjsc
1393173362Sbenjsc	/* run microcode */
1394173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN);
1395173362Sbenjsc
1396173362Sbenjsc	/* wait while the adapter is busy copying the firmware */
1397173362Sbenjsc	for (error = 0, ntries = 0; ntries < 1000; ntries++) {
1398173362Sbenjsc		uint32_t status = WPI_READ(sc, WPI_TX_STATUS);
1399173362Sbenjsc		DPRINTFN(WPI_DEBUG_HW,
1400173362Sbenjsc		    ("firmware status=0x%x, val=0x%x, result=0x%x\n", status,
1401173362Sbenjsc		     WPI_TX_IDLE(6), status & WPI_TX_IDLE(6)));
1402173362Sbenjsc		if (status & WPI_TX_IDLE(6)) {
1403173362Sbenjsc			DPRINTFN(WPI_DEBUG_HW,
1404173362Sbenjsc			    ("Status Match! - ntries = %d\n", ntries));
1405173362Sbenjsc			break;
1406173362Sbenjsc		}
1407173362Sbenjsc		DELAY(10);
1408173362Sbenjsc	}
1409173362Sbenjsc	if (ntries == 1000) {
1410173362Sbenjsc		device_printf(sc->sc_dev, "timeout transferring firmware\n");
1411173362Sbenjsc		error = ETIMEDOUT;
1412173362Sbenjsc	}
1413173362Sbenjsc
1414173362Sbenjsc	/* start the microcode executing */
1415173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_ENABLE);
1416173362Sbenjsc
1417173362Sbenjsc	wpi_mem_unlock(sc);
1418173362Sbenjsc
1419173362Sbenjsc	return (error);
1420173362Sbenjsc}
1421173362Sbenjsc
1422173362Sbenjscstatic void
1423173362Sbenjscwpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1424173362Sbenjsc	struct wpi_rx_data *data)
1425173362Sbenjsc{
1426178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1427178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1428173362Sbenjsc	struct wpi_rx_ring *ring = &sc->rxq;
1429173362Sbenjsc	struct wpi_rx_stat *stat;
1430173362Sbenjsc	struct wpi_rx_head *head;
1431173362Sbenjsc	struct wpi_rx_tail *tail;
1432173362Sbenjsc	struct ieee80211_node *ni;
1433173362Sbenjsc	struct mbuf *m, *mnew;
1434177043Sthompsa	bus_addr_t paddr;
1435177043Sthompsa	int error;
1436173362Sbenjsc
1437173362Sbenjsc	stat = (struct wpi_rx_stat *)(desc + 1);
1438173362Sbenjsc
1439173362Sbenjsc	if (stat->len > WPI_STAT_MAXLEN) {
1440173362Sbenjsc		device_printf(sc->sc_dev, "invalid rx statistic header\n");
1441173362Sbenjsc		ifp->if_ierrors++;
1442173362Sbenjsc		return;
1443173362Sbenjsc	}
1444173362Sbenjsc
1445173362Sbenjsc	head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1446173362Sbenjsc	tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + le16toh(head->len));
1447173362Sbenjsc
1448173362Sbenjsc	DPRINTFN(WPI_DEBUG_RX, ("rx intr: idx=%d len=%d stat len=%d rssi=%d "
1449173362Sbenjsc	    "rate=%x chan=%d tstamp=%ju\n", ring->cur, le32toh(desc->len),
1450173362Sbenjsc	    le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan,
1451173362Sbenjsc	    (uintmax_t)le64toh(tail->tstamp)));
1452173362Sbenjsc
1453190458Sjmallett	/* discard Rx frames with bad CRC early */
1454190458Sjmallett	if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1455190458Sjmallett		DPRINTFN(WPI_DEBUG_RX, ("%s: rx flags error %x\n", __func__,
1456190458Sjmallett		    le32toh(tail->flags)));
1457190458Sjmallett		ifp->if_ierrors++;
1458190458Sjmallett		return;
1459190458Sjmallett	}
1460190458Sjmallett	if (le16toh(head->len) < sizeof (struct ieee80211_frame)) {
1461190458Sjmallett		DPRINTFN(WPI_DEBUG_RX, ("%s: frame too short: %d\n", __func__,
1462190458Sjmallett		    le16toh(head->len)));
1463190458Sjmallett		ifp->if_ierrors++;
1464190458Sjmallett		return;
1465190458Sjmallett	}
1466190458Sjmallett
1467177043Sthompsa	/* XXX don't need mbuf, just dma buffer */
1468177043Sthompsa	mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1469177043Sthompsa	if (mnew == NULL) {
1470177043Sthompsa		DPRINTFN(WPI_DEBUG_RX, ("%s: no mbuf to restock ring\n",
1471177043Sthompsa		    __func__));
1472177043Sthompsa		ifp->if_ierrors++;
1473177043Sthompsa		return;
1474177043Sthompsa	}
1475177043Sthompsa	error = bus_dmamap_load(ring->data_dmat, data->map,
1476177043Sthompsa	    mtod(mnew, caddr_t), MJUMPAGESIZE,
1477177043Sthompsa	    wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1478177043Sthompsa	if (error != 0 && error != EFBIG) {
1479177043Sthompsa		device_printf(sc->sc_dev,
1480177043Sthompsa		    "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1481177043Sthompsa		m_freem(mnew);
1482177043Sthompsa		ifp->if_ierrors++;
1483177043Sthompsa		return;
1484177043Sthompsa	}
1485177043Sthompsa	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1486177043Sthompsa
1487177043Sthompsa	/* finalize mbuf and swap in new one */
1488173362Sbenjsc	m = data->m;
1489173362Sbenjsc	m->m_pkthdr.rcvif = ifp;
1490173362Sbenjsc	m->m_data = (caddr_t)(head + 1);
1491173362Sbenjsc	m->m_pkthdr.len = m->m_len = le16toh(head->len);
1492173362Sbenjsc
1493177043Sthompsa	data->m = mnew;
1494177043Sthompsa	/* update Rx descriptor */
1495177043Sthompsa	ring->desc[ring->cur] = htole32(paddr);
1496173362Sbenjsc
1497192468Ssam	if (ieee80211_radiotap_active(ic)) {
1498173362Sbenjsc		struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
1499173362Sbenjsc
1500173362Sbenjsc		tap->wr_flags = 0;
1501173362Sbenjsc		tap->wr_chan_freq =
1502173362Sbenjsc			htole16(ic->ic_channels[head->chan].ic_freq);
1503173362Sbenjsc		tap->wr_chan_flags =
1504173362Sbenjsc			htole16(ic->ic_channels[head->chan].ic_flags);
1505173362Sbenjsc		tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
1506173362Sbenjsc		tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise);
1507173362Sbenjsc		tap->wr_tsft = tail->tstamp;
1508173362Sbenjsc		tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
1509173362Sbenjsc		switch (head->rate) {
1510173362Sbenjsc		/* CCK rates */
1511173362Sbenjsc		case  10: tap->wr_rate =   2; break;
1512173362Sbenjsc		case  20: tap->wr_rate =   4; break;
1513173362Sbenjsc		case  55: tap->wr_rate =  11; break;
1514173362Sbenjsc		case 110: tap->wr_rate =  22; break;
1515173362Sbenjsc		/* OFDM rates */
1516173362Sbenjsc		case 0xd: tap->wr_rate =  12; break;
1517173362Sbenjsc		case 0xf: tap->wr_rate =  18; break;
1518173362Sbenjsc		case 0x5: tap->wr_rate =  24; break;
1519173362Sbenjsc		case 0x7: tap->wr_rate =  36; break;
1520173362Sbenjsc		case 0x9: tap->wr_rate =  48; break;
1521173362Sbenjsc		case 0xb: tap->wr_rate =  72; break;
1522173362Sbenjsc		case 0x1: tap->wr_rate =  96; break;
1523173362Sbenjsc		case 0x3: tap->wr_rate = 108; break;
1524173362Sbenjsc		/* unknown rate: should not happen */
1525173362Sbenjsc		default:  tap->wr_rate =   0;
1526173362Sbenjsc		}
1527173362Sbenjsc		if (le16toh(head->flags) & 0x4)
1528173362Sbenjsc			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1529173362Sbenjsc	}
1530173362Sbenjsc
1531173362Sbenjsc	WPI_UNLOCK(sc);
1532173362Sbenjsc
1533177043Sthompsa	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1534178354Ssam	if (ni != NULL) {
1535192468Ssam		(void) ieee80211_input(ni, m, stat->rssi, 0);
1536178354Ssam		ieee80211_free_node(ni);
1537178354Ssam	} else
1538192468Ssam		(void) ieee80211_input_all(ic, m, stat->rssi, 0);
1539173362Sbenjsc
1540173362Sbenjsc	WPI_LOCK(sc);
1541173362Sbenjsc}
1542173362Sbenjsc
1543173362Sbenjscstatic void
1544173362Sbenjscwpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1545173362Sbenjsc{
1546178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1547173362Sbenjsc	struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
1548173362Sbenjsc	struct wpi_tx_data *txdata = &ring->data[desc->idx];
1549173362Sbenjsc	struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
1550206358Srpaulo	struct ieee80211_node *ni = txdata->ni;
1551206358Srpaulo	struct ieee80211vap *vap = ni->ni_vap;
1552206358Srpaulo	int retrycnt = 0;
1553173362Sbenjsc
1554173362Sbenjsc	DPRINTFN(WPI_DEBUG_TX, ("tx done: qid=%d idx=%d retries=%d nkill=%d "
1555173362Sbenjsc	    "rate=%x duration=%d status=%x\n", desc->qid, desc->idx,
1556173362Sbenjsc	    stat->ntries, stat->nkill, stat->rate, le32toh(stat->duration),
1557173362Sbenjsc	    le32toh(stat->status)));
1558173362Sbenjsc
1559173362Sbenjsc	/*
1560173362Sbenjsc	 * Update rate control statistics for the node.
1561173362Sbenjsc	 * XXX we should not count mgmt frames since they're always sent at
1562173362Sbenjsc	 * the lowest available bit-rate.
1563173362Sbenjsc	 * XXX frames w/o ACK shouldn't be used either
1564173362Sbenjsc	 */
1565173362Sbenjsc	if (stat->ntries > 0) {
1566190462Sjmallett		DPRINTFN(WPI_DEBUG_TX, ("%d retries\n", stat->ntries));
1567206358Srpaulo		retrycnt = 1;
1568173362Sbenjsc	}
1569206358Srpaulo	ieee80211_ratectl_tx_complete(vap, ni, IEEE80211_RATECTL_TX_SUCCESS,
1570206358Srpaulo	    &retrycnt, NULL);
1571173362Sbenjsc
1572173362Sbenjsc	/* XXX oerrors should only count errors !maxtries */
1573173362Sbenjsc	if ((le32toh(stat->status) & 0xff) != 1)
1574173362Sbenjsc		ifp->if_oerrors++;
1575173362Sbenjsc	else
1576173362Sbenjsc		ifp->if_opackets++;
1577173362Sbenjsc
1578173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, txdata->map, BUS_DMASYNC_POSTWRITE);
1579173362Sbenjsc	bus_dmamap_unload(ring->data_dmat, txdata->map);
1580173362Sbenjsc	/* XXX handle M_TXCB? */
1581173362Sbenjsc	m_freem(txdata->m);
1582173362Sbenjsc	txdata->m = NULL;
1583173362Sbenjsc	ieee80211_free_node(txdata->ni);
1584173362Sbenjsc	txdata->ni = NULL;
1585173362Sbenjsc
1586173362Sbenjsc	ring->queued--;
1587173362Sbenjsc
1588173362Sbenjsc	sc->sc_tx_timer = 0;
1589173362Sbenjsc	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1590178354Ssam	wpi_start_locked(ifp);
1591173362Sbenjsc}
1592173362Sbenjsc
1593173362Sbenjscstatic void
1594173362Sbenjscwpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1595173362Sbenjsc{
1596173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
1597173362Sbenjsc	struct wpi_tx_data *data;
1598173362Sbenjsc
1599173362Sbenjsc	DPRINTFN(WPI_DEBUG_CMD, ("cmd notification qid=%x idx=%d flags=%x "
1600173362Sbenjsc				 "type=%s len=%d\n", desc->qid, desc->idx,
1601173362Sbenjsc				 desc->flags, wpi_cmd_str(desc->type),
1602173362Sbenjsc				 le32toh(desc->len)));
1603173362Sbenjsc
1604173362Sbenjsc	if ((desc->qid & 7) != 4)
1605173362Sbenjsc		return;	/* not a command ack */
1606173362Sbenjsc
1607173362Sbenjsc	data = &ring->data[desc->idx];
1608173362Sbenjsc
1609173362Sbenjsc	/* if the command was mapped in a mbuf, free it */
1610173362Sbenjsc	if (data->m != NULL) {
1611173362Sbenjsc		bus_dmamap_unload(ring->data_dmat, data->map);
1612173362Sbenjsc		m_freem(data->m);
1613173362Sbenjsc		data->m = NULL;
1614173362Sbenjsc	}
1615173362Sbenjsc
1616173362Sbenjsc	sc->flags &= ~WPI_FLAG_BUSY;
1617173362Sbenjsc	wakeup(&ring->cmd[desc->idx]);
1618173362Sbenjsc}
1619173362Sbenjsc
1620173362Sbenjscstatic void
1621173362Sbenjscwpi_notif_intr(struct wpi_softc *sc)
1622173362Sbenjsc{
1623178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1624178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1625173362Sbenjsc	struct wpi_rx_desc *desc;
1626173362Sbenjsc	struct wpi_rx_data *data;
1627173362Sbenjsc	uint32_t hw;
1628173362Sbenjsc
1629173362Sbenjsc	hw = le32toh(sc->shared->next);
1630173362Sbenjsc	while (sc->rxq.cur != hw) {
1631173362Sbenjsc		data = &sc->rxq.data[sc->rxq.cur];
1632173362Sbenjsc		desc = (void *)data->m->m_ext.ext_buf;
1633173362Sbenjsc
1634173362Sbenjsc		DPRINTFN(WPI_DEBUG_NOTIFY,
1635173362Sbenjsc			 ("notify qid=%x idx=%d flags=%x type=%d len=%d\n",
1636173362Sbenjsc			  desc->qid,
1637173362Sbenjsc			  desc->idx,
1638173362Sbenjsc			  desc->flags,
1639173362Sbenjsc			  desc->type,
1640173362Sbenjsc			  le32toh(desc->len)));
1641173362Sbenjsc
1642173362Sbenjsc		if (!(desc->qid & 0x80))	/* reply to a command */
1643173362Sbenjsc			wpi_cmd_intr(sc, desc);
1644173362Sbenjsc
1645173362Sbenjsc		switch (desc->type) {
1646173362Sbenjsc		case WPI_RX_DONE:
1647173362Sbenjsc			/* a 802.11 frame was received */
1648173362Sbenjsc			wpi_rx_intr(sc, desc, data);
1649173362Sbenjsc			break;
1650173362Sbenjsc
1651173362Sbenjsc		case WPI_TX_DONE:
1652173362Sbenjsc			/* a 802.11 frame has been transmitted */
1653173362Sbenjsc			wpi_tx_intr(sc, desc);
1654173362Sbenjsc			break;
1655173362Sbenjsc
1656173362Sbenjsc		case WPI_UC_READY:
1657173362Sbenjsc		{
1658173362Sbenjsc			struct wpi_ucode_info *uc =
1659173362Sbenjsc				(struct wpi_ucode_info *)(desc + 1);
1660173362Sbenjsc
1661173362Sbenjsc			/* the microcontroller is ready */
1662173362Sbenjsc			DPRINTF(("microcode alive notification version %x "
1663173362Sbenjsc				"alive %x\n", le32toh(uc->version),
1664173362Sbenjsc				le32toh(uc->valid)));
1665173362Sbenjsc
1666173362Sbenjsc			if (le32toh(uc->valid) != 1) {
1667173362Sbenjsc				device_printf(sc->sc_dev,
1668173362Sbenjsc				    "microcontroller initialization failed\n");
1669173362Sbenjsc				wpi_stop_locked(sc);
1670173362Sbenjsc			}
1671173362Sbenjsc			break;
1672173362Sbenjsc		}
1673173362Sbenjsc		case WPI_STATE_CHANGED:
1674173362Sbenjsc		{
1675173362Sbenjsc			uint32_t *status = (uint32_t *)(desc + 1);
1676173362Sbenjsc
1677173362Sbenjsc			/* enabled/disabled notification */
1678173362Sbenjsc			DPRINTF(("state changed to %x\n", le32toh(*status)));
1679173362Sbenjsc
1680173362Sbenjsc			if (le32toh(*status) & 1) {
1681173362Sbenjsc				device_printf(sc->sc_dev,
1682173362Sbenjsc				    "Radio transmitter is switched off\n");
1683173362Sbenjsc				sc->flags |= WPI_FLAG_HW_RADIO_OFF;
1684177043Sthompsa				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1685177043Sthompsa				/* Disable firmware commands */
1686177043Sthompsa				WPI_WRITE(sc, WPI_UCODE_SET, WPI_DISABLE_CMD);
1687173362Sbenjsc			}
1688173362Sbenjsc			break;
1689173362Sbenjsc		}
1690173362Sbenjsc		case WPI_START_SCAN:
1691173362Sbenjsc		{
1692179957Sthompsa#ifdef WPI_DEBUG
1693173362Sbenjsc			struct wpi_start_scan *scan =
1694173362Sbenjsc				(struct wpi_start_scan *)(desc + 1);
1695179957Sthompsa#endif
1696173362Sbenjsc
1697173362Sbenjsc			DPRINTFN(WPI_DEBUG_SCANNING,
1698173362Sbenjsc				 ("scanning channel %d status %x\n",
1699173362Sbenjsc			    scan->chan, le32toh(scan->status)));
1700173362Sbenjsc			break;
1701173362Sbenjsc		}
1702173362Sbenjsc		case WPI_STOP_SCAN:
1703173362Sbenjsc		{
1704179957Sthompsa#ifdef WPI_DEBUG
1705173362Sbenjsc			struct wpi_stop_scan *scan =
1706173362Sbenjsc				(struct wpi_stop_scan *)(desc + 1);
1707179957Sthompsa#endif
1708178354Ssam			struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1709173362Sbenjsc
1710173362Sbenjsc			DPRINTFN(WPI_DEBUG_SCANNING,
1711173362Sbenjsc			    ("scan finished nchan=%d status=%d chan=%d\n",
1712173362Sbenjsc			     scan->nchan, scan->status, scan->chan));
1713173362Sbenjsc
1714177043Sthompsa			sc->sc_scan_timer = 0;
1715178354Ssam			ieee80211_scan_next(vap);
1716173362Sbenjsc			break;
1717173362Sbenjsc		}
1718173976Sbenjsc		case WPI_MISSED_BEACON:
1719173976Sbenjsc		{
1720178354Ssam			struct wpi_missed_beacon *beacon =
1721173976Sbenjsc				(struct wpi_missed_beacon *)(desc + 1);
1722178354Ssam			struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1723173976Sbenjsc
1724178354Ssam			if (le32toh(beacon->consecutive) >=
1725178354Ssam			    vap->iv_bmissthreshold) {
1726178354Ssam				DPRINTF(("Beacon miss: %u >= %u\n",
1727178354Ssam					 le32toh(beacon->consecutive),
1728178354Ssam					 vap->iv_bmissthreshold));
1729191746Sthompsa				ieee80211_beacon_miss(ic);
1730178354Ssam			}
1731178354Ssam			break;
1732173362Sbenjsc		}
1733173976Sbenjsc		}
1734173362Sbenjsc
1735173362Sbenjsc		sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
1736173362Sbenjsc	}
1737173362Sbenjsc
1738173362Sbenjsc	/* tell the firmware what we have processed */
1739173362Sbenjsc	hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
1740173362Sbenjsc	WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7);
1741173362Sbenjsc}
1742173362Sbenjsc
1743173362Sbenjscstatic void
1744173362Sbenjscwpi_intr(void *arg)
1745173362Sbenjsc{
1746173362Sbenjsc	struct wpi_softc *sc = arg;
1747173362Sbenjsc	uint32_t r;
1748173362Sbenjsc
1749173362Sbenjsc	WPI_LOCK(sc);
1750173362Sbenjsc
1751173362Sbenjsc	r = WPI_READ(sc, WPI_INTR);
1752173362Sbenjsc	if (r == 0 || r == 0xffffffff) {
1753173362Sbenjsc		WPI_UNLOCK(sc);
1754173362Sbenjsc		return;
1755173362Sbenjsc	}
1756173362Sbenjsc
1757173362Sbenjsc	/* disable interrupts */
1758173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, 0);
1759173362Sbenjsc	/* ack interrupts */
1760173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, r);
1761173362Sbenjsc
1762173362Sbenjsc	if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) {
1763191746Sthompsa		struct ifnet *ifp = sc->sc_ifp;
1764191746Sthompsa		struct ieee80211com *ic = ifp->if_l2com;
1765191956Sthompsa		struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1766191746Sthompsa
1767173362Sbenjsc		device_printf(sc->sc_dev, "fatal firmware error\n");
1768173362Sbenjsc		DPRINTFN(6,("(%s)\n", (r & WPI_SW_ERROR) ? "(Software Error)" :
1769173362Sbenjsc				"(Hardware Error)"));
1770191956Sthompsa		if (vap != NULL)
1771191956Sthompsa			ieee80211_cancel_scan(vap);
1772191746Sthompsa		ieee80211_runtask(ic, &sc->sc_restarttask);
1773173362Sbenjsc		sc->flags &= ~WPI_FLAG_BUSY;
1774173362Sbenjsc		WPI_UNLOCK(sc);
1775173362Sbenjsc		return;
1776173362Sbenjsc	}
1777173362Sbenjsc
1778173362Sbenjsc	if (r & WPI_RX_INTR)
1779173362Sbenjsc		wpi_notif_intr(sc);
1780173362Sbenjsc
1781173362Sbenjsc	if (r & WPI_ALIVE_INTR)	/* firmware initialized */
1782173362Sbenjsc		wakeup(sc);
1783173362Sbenjsc
1784173362Sbenjsc	/* re-enable interrupts */
1785173362Sbenjsc	if (sc->sc_ifp->if_flags & IFF_UP)
1786173362Sbenjsc		WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
1787173362Sbenjsc
1788173362Sbenjsc	WPI_UNLOCK(sc);
1789173362Sbenjsc}
1790173362Sbenjsc
1791173362Sbenjscstatic uint8_t
1792173362Sbenjscwpi_plcp_signal(int rate)
1793173362Sbenjsc{
1794173362Sbenjsc	switch (rate) {
1795173362Sbenjsc	/* CCK rates (returned values are device-dependent) */
1796173362Sbenjsc	case 2:		return 10;
1797173362Sbenjsc	case 4:		return 20;
1798173362Sbenjsc	case 11:	return 55;
1799173362Sbenjsc	case 22:	return 110;
1800173362Sbenjsc
1801173362Sbenjsc	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1802173362Sbenjsc	/* R1-R4 (ral/ural is R4-R1) */
1803173362Sbenjsc	case 12:	return 0xd;
1804173362Sbenjsc	case 18:	return 0xf;
1805173362Sbenjsc	case 24:	return 0x5;
1806173362Sbenjsc	case 36:	return 0x7;
1807173362Sbenjsc	case 48:	return 0x9;
1808173362Sbenjsc	case 72:	return 0xb;
1809173362Sbenjsc	case 96:	return 0x1;
1810173362Sbenjsc	case 108:	return 0x3;
1811173362Sbenjsc
1812173362Sbenjsc	/* unsupported rates (should not get there) */
1813173362Sbenjsc	default:	return 0;
1814173362Sbenjsc	}
1815173362Sbenjsc}
1816173362Sbenjsc
1817173362Sbenjsc/* quickly determine if a given rate is CCK or OFDM */
1818173362Sbenjsc#define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1819173362Sbenjsc
1820173362Sbenjsc/*
1821173362Sbenjsc * Construct the data packet for a transmit buffer and acutally put
1822173362Sbenjsc * the buffer onto the transmit ring, kicking the card to process the
1823173362Sbenjsc * the buffer.
1824173362Sbenjsc */
1825173362Sbenjscstatic int
1826173362Sbenjscwpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1827173362Sbenjsc	int ac)
1828173362Sbenjsc{
1829178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
1830178354Ssam	struct ifnet *ifp = sc->sc_ifp;
1831178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
1832177043Sthompsa	const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
1833173362Sbenjsc	struct wpi_tx_ring *ring = &sc->txq[ac];
1834173362Sbenjsc	struct wpi_tx_desc *desc;
1835173362Sbenjsc	struct wpi_tx_data *data;
1836173362Sbenjsc	struct wpi_tx_cmd *cmd;
1837173362Sbenjsc	struct wpi_cmd_data *tx;
1838173362Sbenjsc	struct ieee80211_frame *wh;
1839178354Ssam	const struct ieee80211_txparam *tp;
1840173362Sbenjsc	struct ieee80211_key *k;
1841173362Sbenjsc	struct mbuf *mnew;
1842177043Sthompsa	int i, error, nsegs, rate, hdrlen, ismcast;
1843173362Sbenjsc	bus_dma_segment_t segs[WPI_MAX_SCATTER];
1844173362Sbenjsc
1845173362Sbenjsc	desc = &ring->desc[ring->cur];
1846173362Sbenjsc	data = &ring->data[ring->cur];
1847173362Sbenjsc
1848173362Sbenjsc	wh = mtod(m0, struct ieee80211_frame *);
1849173362Sbenjsc
1850177043Sthompsa	hdrlen = ieee80211_hdrsize(wh);
1851177043Sthompsa	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1852173362Sbenjsc
1853173362Sbenjsc	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1854178354Ssam		k = ieee80211_crypto_encap(ni, m0);
1855177043Sthompsa		if (k == NULL) {
1856173362Sbenjsc			m_freem(m0);
1857173362Sbenjsc			return ENOBUFS;
1858173362Sbenjsc		}
1859173362Sbenjsc		/* packet header may have moved, reset our local pointer */
1860173362Sbenjsc		wh = mtod(m0, struct ieee80211_frame *);
1861173362Sbenjsc	}
1862173362Sbenjsc
1863173362Sbenjsc	cmd = &ring->cmd[ring->cur];
1864173362Sbenjsc	cmd->code = WPI_CMD_TX_DATA;
1865173362Sbenjsc	cmd->flags = 0;
1866173362Sbenjsc	cmd->qid = ring->qid;
1867173362Sbenjsc	cmd->idx = ring->cur;
1868173362Sbenjsc
1869173362Sbenjsc	tx = (struct wpi_cmd_data *)cmd->data;
1870177043Sthompsa	tx->flags = htole32(WPI_TX_AUTO_SEQ);
1871178354Ssam	tx->timeout = htole16(0);
1872177043Sthompsa	tx->ofdm_mask = 0xff;
1873177043Sthompsa	tx->cck_mask = 0x0f;
1874177043Sthompsa	tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
1875177043Sthompsa	tx->id = ismcast ? WPI_ID_BROADCAST : WPI_ID_BSS;
1876177043Sthompsa	tx->len = htole16(m0->m_pkthdr.len);
1877173362Sbenjsc
1878177119Ssam	if (!ismcast) {
1879177043Sthompsa		if ((ni->ni_flags & IEEE80211_NODE_QOS) == 0 ||
1880177043Sthompsa		    !cap->cap_wmeParams[ac].wmep_noackPolicy)
1881177043Sthompsa			tx->flags |= htole32(WPI_TX_NEED_ACK);
1882178354Ssam		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
1883177043Sthompsa			tx->flags |= htole32(WPI_TX_NEED_RTS|WPI_TX_FULL_TXOP);
1884177043Sthompsa			tx->rts_ntries = 7;
1885177043Sthompsa		}
1886173362Sbenjsc	}
1887177043Sthompsa	/* pick a rate */
1888178354Ssam	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1889178354Ssam	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == IEEE80211_FC0_TYPE_MGT) {
1890173362Sbenjsc		uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1891173362Sbenjsc		/* tell h/w to set timestamp in probe responses */
1892173362Sbenjsc		if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1893177043Sthompsa			tx->flags |= htole32(WPI_TX_INSERT_TSTAMP);
1894173362Sbenjsc		if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
1895177043Sthompsa		    subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
1896173362Sbenjsc			tx->timeout = htole16(3);
1897173362Sbenjsc		else
1898173362Sbenjsc			tx->timeout = htole16(2);
1899178354Ssam		rate = tp->mgmtrate;
1900177043Sthompsa	} else if (ismcast) {
1901178354Ssam		rate = tp->mcastrate;
1902178354Ssam	} else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1903178354Ssam		rate = tp->ucastrate;
1904177043Sthompsa	} else {
1905206358Srpaulo		(void) ieee80211_ratectl_rate(ni, NULL, 0);
1906178354Ssam		rate = ni->ni_txrate;
1907177043Sthompsa	}
1908173362Sbenjsc	tx->rate = wpi_plcp_signal(rate);
1909173362Sbenjsc
1910173362Sbenjsc	/* be very persistant at sending frames out */
1911178354Ssam#if 0
1912178354Ssam	tx->data_ntries = tp->maxretry;
1913178354Ssam#else
1914178354Ssam	tx->data_ntries = 15;		/* XXX way too high */
1915178354Ssam#endif
1916173362Sbenjsc
1917192468Ssam	if (ieee80211_radiotap_active_vap(vap)) {
1918177043Sthompsa		struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
1919177043Sthompsa		tap->wt_flags = 0;
1920177043Sthompsa		tap->wt_rate = rate;
1921177043Sthompsa		tap->wt_hwqueue = ac;
1922177043Sthompsa		if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1923177043Sthompsa			tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1924178354Ssam
1925192468Ssam		ieee80211_radiotap_tx(vap, m0);
1926177043Sthompsa	}
1927173362Sbenjsc
1928173362Sbenjsc	/* save and trim IEEE802.11 header */
1929173362Sbenjsc	m_copydata(m0, 0, hdrlen, (caddr_t)&tx->wh);
1930173362Sbenjsc	m_adj(m0, hdrlen);
1931173362Sbenjsc
1932173362Sbenjsc	error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m0, segs,
1933173362Sbenjsc	    &nsegs, BUS_DMA_NOWAIT);
1934173362Sbenjsc	if (error != 0 && error != EFBIG) {
1935173362Sbenjsc		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1936173362Sbenjsc		    error);
1937173362Sbenjsc		m_freem(m0);
1938173362Sbenjsc		return error;
1939173362Sbenjsc	}
1940173362Sbenjsc	if (error != 0) {
1941175418Sjhb		/* XXX use m_collapse */
1942173362Sbenjsc		mnew = m_defrag(m0, M_DONTWAIT);
1943173362Sbenjsc		if (mnew == NULL) {
1944173362Sbenjsc			device_printf(sc->sc_dev,
1945173362Sbenjsc			    "could not defragment mbuf\n");
1946173362Sbenjsc			m_freem(m0);
1947173362Sbenjsc			return ENOBUFS;
1948173362Sbenjsc		}
1949173362Sbenjsc		m0 = mnew;
1950173362Sbenjsc
1951173362Sbenjsc		error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
1952173362Sbenjsc		    m0, segs, &nsegs, BUS_DMA_NOWAIT);
1953173362Sbenjsc		if (error != 0) {
1954173362Sbenjsc			device_printf(sc->sc_dev,
1955173362Sbenjsc			    "could not map mbuf (error %d)\n", error);
1956173362Sbenjsc			m_freem(m0);
1957173362Sbenjsc			return error;
1958173362Sbenjsc		}
1959173362Sbenjsc	}
1960173362Sbenjsc
1961173362Sbenjsc	data->m = m0;
1962173362Sbenjsc	data->ni = ni;
1963173362Sbenjsc
1964173362Sbenjsc	DPRINTFN(WPI_DEBUG_TX, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
1965173362Sbenjsc	    ring->qid, ring->cur, m0->m_pkthdr.len, nsegs));
1966173362Sbenjsc
1967173362Sbenjsc	/* first scatter/gather segment is used by the tx data command */
1968173362Sbenjsc	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 |
1969173362Sbenjsc	    (1 + nsegs) << 24);
1970173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
1971173362Sbenjsc	    ring->cur * sizeof (struct wpi_tx_cmd));
1972173362Sbenjsc	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_data));
1973173362Sbenjsc	for (i = 1; i <= nsegs; i++) {
1974173362Sbenjsc		desc->segs[i].addr = htole32(segs[i - 1].ds_addr);
1975173362Sbenjsc		desc->segs[i].len  = htole32(segs[i - 1].ds_len);
1976173362Sbenjsc	}
1977173362Sbenjsc
1978173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1979173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1980173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
1981173362Sbenjsc
1982173362Sbenjsc	ring->queued++;
1983173362Sbenjsc
1984173362Sbenjsc	/* kick ring */
1985173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
1986173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
1987173362Sbenjsc
1988173362Sbenjsc	return 0;
1989173362Sbenjsc}
1990173362Sbenjsc
1991173362Sbenjsc/**
1992173362Sbenjsc * Process data waiting to be sent on the IFNET output queue
1993173362Sbenjsc */
1994173362Sbenjscstatic void
1995173362Sbenjscwpi_start(struct ifnet *ifp)
1996173362Sbenjsc{
1997173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
1998178354Ssam
1999178354Ssam	WPI_LOCK(sc);
2000178354Ssam	wpi_start_locked(ifp);
2001178354Ssam	WPI_UNLOCK(sc);
2002178354Ssam}
2003178354Ssam
2004178354Ssamstatic void
2005178354Ssamwpi_start_locked(struct ifnet *ifp)
2006178354Ssam{
2007178354Ssam	struct wpi_softc *sc = ifp->if_softc;
2008173362Sbenjsc	struct ieee80211_node *ni;
2009178354Ssam	struct mbuf *m;
2010178354Ssam	int ac;
2011173362Sbenjsc
2012178354Ssam	WPI_LOCK_ASSERT(sc);
2013178354Ssam
2014177043Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2015177043Sthompsa		return;
2016173362Sbenjsc
2017173362Sbenjsc	for (;;) {
2018190458Sjmallett		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2019178354Ssam		if (m == NULL)
2020178354Ssam			break;
2021178354Ssam		ac = M_WME_GETAC(m);
2022178354Ssam		if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
2023178354Ssam			/* there is no place left in this ring */
2024178354Ssam			IFQ_DRV_PREPEND(&ifp->if_snd, m);
2025178354Ssam			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2026178354Ssam			break;
2027178354Ssam		}
2028178354Ssam		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
2029178354Ssam		if (wpi_tx_data(sc, m, ni, ac) != 0) {
2030178354Ssam			ieee80211_free_node(ni);
2031178354Ssam			ifp->if_oerrors++;
2032178354Ssam			break;
2033178354Ssam		}
2034178354Ssam		sc->sc_tx_timer = 5;
2035178354Ssam	}
2036178354Ssam}
2037173362Sbenjsc
2038178354Ssamstatic int
2039178354Ssamwpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2040178354Ssam	const struct ieee80211_bpf_params *params)
2041178354Ssam{
2042178354Ssam	struct ieee80211com *ic = ni->ni_ic;
2043178354Ssam	struct ifnet *ifp = ic->ic_ifp;
2044178354Ssam	struct wpi_softc *sc = ifp->if_softc;
2045173362Sbenjsc
2046178354Ssam	/* prevent management frames from being sent if we're not ready */
2047178354Ssam	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2048178354Ssam		m_freem(m);
2049178354Ssam		ieee80211_free_node(ni);
2050178354Ssam		return ENETDOWN;
2051178354Ssam	}
2052178354Ssam	WPI_LOCK(sc);
2053173362Sbenjsc
2054178354Ssam	/* management frames go into ring 0 */
2055178354Ssam	if (sc->txq[0].queued > sc->txq[0].count - 8) {
2056178354Ssam		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2057178354Ssam		m_freem(m);
2058178354Ssam		WPI_UNLOCK(sc);
2059178354Ssam		ieee80211_free_node(ni);
2060178354Ssam		return ENOBUFS;		/* XXX */
2061178354Ssam	}
2062173362Sbenjsc
2063178354Ssam	ifp->if_opackets++;
2064178354Ssam	if (wpi_tx_data(sc, m, ni, 0) != 0)
2065178354Ssam		goto bad;
2066178354Ssam	sc->sc_tx_timer = 5;
2067178354Ssam	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
2068173362Sbenjsc
2069178354Ssam	WPI_UNLOCK(sc);
2070178354Ssam	return 0;
2071178354Ssambad:
2072178354Ssam	ifp->if_oerrors++;
2073178354Ssam	WPI_UNLOCK(sc);
2074178354Ssam	ieee80211_free_node(ni);
2075178354Ssam	return EIO;		/* XXX */
2076173362Sbenjsc}
2077173362Sbenjsc
2078173362Sbenjscstatic int
2079173362Sbenjscwpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2080173362Sbenjsc{
2081173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
2082178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2083178354Ssam	struct ifreq *ifr = (struct ifreq *) data;
2084178354Ssam	int error = 0, startall = 0;
2085173362Sbenjsc
2086173362Sbenjsc	switch (cmd) {
2087173362Sbenjsc	case SIOCSIFFLAGS:
2088178704Sthompsa		WPI_LOCK(sc);
2089173362Sbenjsc		if ((ifp->if_flags & IFF_UP)) {
2090178354Ssam			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2091177043Sthompsa				wpi_init_locked(sc, 0);
2092178354Ssam				startall = 1;
2093178354Ssam			}
2094177043Sthompsa		} else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) ||
2095177043Sthompsa			   (sc->flags & WPI_FLAG_HW_RADIO_OFF))
2096173362Sbenjsc			wpi_stop_locked(sc);
2097178704Sthompsa		WPI_UNLOCK(sc);
2098178704Sthompsa		if (startall)
2099178704Sthompsa			ieee80211_start_all(ic);
2100173362Sbenjsc		break;
2101178354Ssam	case SIOCGIFMEDIA:
2102178354Ssam		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2103178354Ssam		break;
2104178704Sthompsa	case SIOCGIFADDR:
2105178354Ssam		error = ether_ioctl(ifp, cmd, data);
2106178354Ssam		break;
2107178704Sthompsa	default:
2108178704Sthompsa		error = EINVAL;
2109178704Sthompsa		break;
2110173362Sbenjsc	}
2111173362Sbenjsc	return error;
2112173362Sbenjsc}
2113173362Sbenjsc
2114173362Sbenjsc/*
2115173362Sbenjsc * Extract various information from EEPROM.
2116173362Sbenjsc */
2117173362Sbenjscstatic void
2118190526Ssamwpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
2119173362Sbenjsc{
2120173362Sbenjsc	int i;
2121173362Sbenjsc
2122173362Sbenjsc	/* read the hardware capabilities, revision and SKU type */
2123173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap,1);
2124173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,2);
2125173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1);
2126173362Sbenjsc
2127173362Sbenjsc	/* read the regulatory domain */
2128173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain, 4);
2129173362Sbenjsc
2130173362Sbenjsc	/* read in the hw MAC address */
2131190526Ssam	wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr, 6);
2132173362Sbenjsc
2133173362Sbenjsc	/* read the list of authorized channels */
2134173362Sbenjsc	for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
2135173362Sbenjsc		wpi_read_eeprom_channels(sc,i);
2136173362Sbenjsc
2137173362Sbenjsc	/* read the power level calibration info for each group */
2138173362Sbenjsc	for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
2139173362Sbenjsc		wpi_read_eeprom_group(sc,i);
2140173362Sbenjsc}
2141173362Sbenjsc
2142173362Sbenjsc/*
2143173362Sbenjsc * Send a command to the firmware.
2144173362Sbenjsc */
2145173362Sbenjscstatic int
2146173362Sbenjscwpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
2147173362Sbenjsc{
2148173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2149173362Sbenjsc	struct wpi_tx_desc *desc;
2150173362Sbenjsc	struct wpi_tx_cmd *cmd;
2151173362Sbenjsc
2152173362Sbenjsc#ifdef WPI_DEBUG
2153173362Sbenjsc	if (!async) {
2154173362Sbenjsc		WPI_LOCK_ASSERT(sc);
2155173362Sbenjsc	}
2156173362Sbenjsc#endif
2157173362Sbenjsc
2158173362Sbenjsc	DPRINTFN(WPI_DEBUG_CMD,("wpi_cmd %d size %d async %d\n", code, size,
2159173362Sbenjsc		    async));
2160173362Sbenjsc
2161173362Sbenjsc	if (sc->flags & WPI_FLAG_BUSY) {
2162173362Sbenjsc		device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n",
2163173362Sbenjsc		    __func__, code);
2164173362Sbenjsc		return EAGAIN;
2165173362Sbenjsc	}
2166173362Sbenjsc	sc->flags|= WPI_FLAG_BUSY;
2167173362Sbenjsc
2168173362Sbenjsc	KASSERT(size <= sizeof cmd->data, ("command %d too large: %d bytes",
2169173362Sbenjsc	    code, size));
2170173362Sbenjsc
2171173362Sbenjsc	desc = &ring->desc[ring->cur];
2172173362Sbenjsc	cmd = &ring->cmd[ring->cur];
2173173362Sbenjsc
2174173362Sbenjsc	cmd->code = code;
2175173362Sbenjsc	cmd->flags = 0;
2176173362Sbenjsc	cmd->qid = ring->qid;
2177173362Sbenjsc	cmd->idx = ring->cur;
2178173362Sbenjsc	memcpy(cmd->data, buf, size);
2179173362Sbenjsc
2180173362Sbenjsc	desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
2181173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2182173362Sbenjsc		ring->cur * sizeof (struct wpi_tx_cmd));
2183173362Sbenjsc	desc->segs[0].len  = htole32(4 + size);
2184173362Sbenjsc
2185173362Sbenjsc	/* kick cmd ring */
2186173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2187173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2188173362Sbenjsc
2189173362Sbenjsc	if (async) {
2190173362Sbenjsc		sc->flags &= ~ WPI_FLAG_BUSY;
2191173362Sbenjsc		return 0;
2192173362Sbenjsc	}
2193173362Sbenjsc
2194173362Sbenjsc	return msleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz);
2195173362Sbenjsc}
2196173362Sbenjsc
2197173362Sbenjscstatic int
2198173362Sbenjscwpi_wme_update(struct ieee80211com *ic)
2199173362Sbenjsc{
2200173362Sbenjsc#define WPI_EXP2(v)	htole16((1 << (v)) - 1)
2201173362Sbenjsc#define WPI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
2202173362Sbenjsc	struct wpi_softc *sc = ic->ic_ifp->if_softc;
2203173362Sbenjsc	const struct wmeParams *wmep;
2204173362Sbenjsc	struct wpi_wme_setup wme;
2205173362Sbenjsc	int ac;
2206173362Sbenjsc
2207173362Sbenjsc	/* don't override default WME values if WME is not actually enabled */
2208173362Sbenjsc	if (!(ic->ic_flags & IEEE80211_F_WME))
2209173362Sbenjsc		return 0;
2210173362Sbenjsc
2211173362Sbenjsc	wme.flags = 0;
2212173362Sbenjsc	for (ac = 0; ac < WME_NUM_AC; ac++) {
2213173362Sbenjsc		wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
2214173362Sbenjsc		wme.ac[ac].aifsn = wmep->wmep_aifsn;
2215173362Sbenjsc		wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin);
2216173362Sbenjsc		wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax);
2217173362Sbenjsc		wme.ac[ac].txop  = WPI_USEC(wmep->wmep_txopLimit);
2218173362Sbenjsc
2219173362Sbenjsc		DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
2220173362Sbenjsc		    "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
2221173362Sbenjsc		    wme.ac[ac].cwmax, wme.ac[ac].txop));
2222173362Sbenjsc	}
2223173362Sbenjsc	return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1);
2224173362Sbenjsc#undef WPI_USEC
2225173362Sbenjsc#undef WPI_EXP2
2226173362Sbenjsc}
2227173362Sbenjsc
2228173362Sbenjsc/*
2229173362Sbenjsc * Configure h/w multi-rate retries.
2230173362Sbenjsc */
2231173362Sbenjscstatic int
2232173362Sbenjscwpi_mrr_setup(struct wpi_softc *sc)
2233173362Sbenjsc{
2234178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2235178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2236173362Sbenjsc	struct wpi_mrr_setup mrr;
2237173362Sbenjsc	int i, error;
2238173362Sbenjsc
2239173362Sbenjsc	memset(&mrr, 0, sizeof (struct wpi_mrr_setup));
2240173362Sbenjsc
2241173362Sbenjsc	/* CCK rates (not used with 802.11a) */
2242173362Sbenjsc	for (i = WPI_CCK1; i <= WPI_CCK11; i++) {
2243173362Sbenjsc		mrr.rates[i].flags = 0;
2244173362Sbenjsc		mrr.rates[i].signal = wpi_ridx_to_plcp[i];
2245173362Sbenjsc		/* fallback to the immediate lower CCK rate (if any) */
2246173362Sbenjsc		mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1;
2247173362Sbenjsc		/* try one time at this rate before falling back to "next" */
2248173362Sbenjsc		mrr.rates[i].ntries = 1;
2249173362Sbenjsc	}
2250173362Sbenjsc
2251173362Sbenjsc	/* OFDM rates (not used with 802.11b) */
2252173362Sbenjsc	for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) {
2253173362Sbenjsc		mrr.rates[i].flags = 0;
2254173362Sbenjsc		mrr.rates[i].signal = wpi_ridx_to_plcp[i];
2255173362Sbenjsc		/* fallback to the immediate lower OFDM rate (if any) */
2256173362Sbenjsc		/* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */
2257173362Sbenjsc		mrr.rates[i].next = (i == WPI_OFDM6) ?
2258173362Sbenjsc		    ((ic->ic_curmode == IEEE80211_MODE_11A) ?
2259173362Sbenjsc			WPI_OFDM6 : WPI_CCK2) :
2260173362Sbenjsc		    i - 1;
2261173362Sbenjsc		/* try one time at this rate before falling back to "next" */
2262173362Sbenjsc		mrr.rates[i].ntries = 1;
2263173362Sbenjsc	}
2264173362Sbenjsc
2265173362Sbenjsc	/* setup MRR for control frames */
2266173362Sbenjsc	mrr.which = htole32(WPI_MRR_CTL);
2267173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2268173362Sbenjsc	if (error != 0) {
2269173362Sbenjsc		device_printf(sc->sc_dev,
2270173362Sbenjsc		    "could not setup MRR for control frames\n");
2271173362Sbenjsc		return error;
2272173362Sbenjsc	}
2273173362Sbenjsc
2274173362Sbenjsc	/* setup MRR for data frames */
2275173362Sbenjsc	mrr.which = htole32(WPI_MRR_DATA);
2276173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
2277173362Sbenjsc	if (error != 0) {
2278173362Sbenjsc		device_printf(sc->sc_dev,
2279173362Sbenjsc		    "could not setup MRR for data frames\n");
2280173362Sbenjsc		return error;
2281173362Sbenjsc	}
2282173362Sbenjsc
2283173362Sbenjsc	return 0;
2284173362Sbenjsc}
2285173362Sbenjsc
2286173362Sbenjscstatic void
2287173362Sbenjscwpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
2288173362Sbenjsc{
2289173362Sbenjsc	struct wpi_cmd_led led;
2290173362Sbenjsc
2291173362Sbenjsc	led.which = which;
2292173362Sbenjsc	led.unit = htole32(100000);	/* on/off in unit of 100ms */
2293173362Sbenjsc	led.off = off;
2294173362Sbenjsc	led.on = on;
2295173362Sbenjsc
2296173362Sbenjsc	(void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
2297173362Sbenjsc}
2298173362Sbenjsc
2299173362Sbenjscstatic void
2300173362Sbenjscwpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni)
2301173362Sbenjsc{
2302173362Sbenjsc	struct wpi_cmd_tsf tsf;
2303173362Sbenjsc	uint64_t val, mod;
2304173362Sbenjsc
2305173362Sbenjsc	memset(&tsf, 0, sizeof tsf);
2306173362Sbenjsc	memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8);
2307173362Sbenjsc	tsf.bintval = htole16(ni->ni_intval);
2308173362Sbenjsc	tsf.lintval = htole16(10);
2309173362Sbenjsc
2310173362Sbenjsc	/* compute remaining time until next beacon */
2311173362Sbenjsc	val = (uint64_t)ni->ni_intval  * 1024;	/* msec -> usec */
2312173362Sbenjsc	mod = le64toh(tsf.tstamp) % val;
2313173362Sbenjsc	tsf.binitval = htole32((uint32_t)(val - mod));
2314173362Sbenjsc
2315173362Sbenjsc	if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
2316173362Sbenjsc		device_printf(sc->sc_dev, "could not enable TSF\n");
2317173362Sbenjsc}
2318173362Sbenjsc
2319173362Sbenjsc#if 0
2320173362Sbenjsc/*
2321173362Sbenjsc * Build a beacon frame that the firmware will broadcast periodically in
2322173362Sbenjsc * IBSS or HostAP modes.
2323173362Sbenjsc */
2324173362Sbenjscstatic int
2325173362Sbenjscwpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
2326173362Sbenjsc{
2327178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2328178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2329173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2330173362Sbenjsc	struct wpi_tx_desc *desc;
2331173362Sbenjsc	struct wpi_tx_data *data;
2332173362Sbenjsc	struct wpi_tx_cmd *cmd;
2333173362Sbenjsc	struct wpi_cmd_beacon *bcn;
2334173362Sbenjsc	struct ieee80211_beacon_offsets bo;
2335173362Sbenjsc	struct mbuf *m0;
2336173362Sbenjsc	bus_addr_t physaddr;
2337173362Sbenjsc	int error;
2338173362Sbenjsc
2339173362Sbenjsc	desc = &ring->desc[ring->cur];
2340173362Sbenjsc	data = &ring->data[ring->cur];
2341173362Sbenjsc
2342173362Sbenjsc	m0 = ieee80211_beacon_alloc(ic, ni, &bo);
2343173362Sbenjsc	if (m0 == NULL) {
2344173362Sbenjsc		device_printf(sc->sc_dev, "could not allocate beacon frame\n");
2345173362Sbenjsc		return ENOMEM;
2346173362Sbenjsc	}
2347173362Sbenjsc
2348173362Sbenjsc	cmd = &ring->cmd[ring->cur];
2349173362Sbenjsc	cmd->code = WPI_CMD_SET_BEACON;
2350173362Sbenjsc	cmd->flags = 0;
2351173362Sbenjsc	cmd->qid = ring->qid;
2352173362Sbenjsc	cmd->idx = ring->cur;
2353173362Sbenjsc
2354173362Sbenjsc	bcn = (struct wpi_cmd_beacon *)cmd->data;
2355173362Sbenjsc	memset(bcn, 0, sizeof (struct wpi_cmd_beacon));
2356173362Sbenjsc	bcn->id = WPI_ID_BROADCAST;
2357173362Sbenjsc	bcn->ofdm_mask = 0xff;
2358173362Sbenjsc	bcn->cck_mask = 0x0f;
2359173362Sbenjsc	bcn->lifetime = htole32(WPI_LIFETIME_INFINITE);
2360173362Sbenjsc	bcn->len = htole16(m0->m_pkthdr.len);
2361173362Sbenjsc	bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2362173362Sbenjsc		wpi_plcp_signal(12) : wpi_plcp_signal(2);
2363173362Sbenjsc	bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
2364173362Sbenjsc
2365173362Sbenjsc	/* save and trim IEEE802.11 header */
2366173362Sbenjsc	m_copydata(m0, 0, sizeof (struct ieee80211_frame), (caddr_t)&bcn->wh);
2367173362Sbenjsc	m_adj(m0, sizeof (struct ieee80211_frame));
2368173362Sbenjsc
2369173362Sbenjsc	/* assume beacon frame is contiguous */
2370173362Sbenjsc	error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m0, void *),
2371173362Sbenjsc	    m0->m_pkthdr.len, wpi_dma_map_addr, &physaddr, 0);
2372173362Sbenjsc	if (error != 0) {
2373173362Sbenjsc		device_printf(sc->sc_dev, "could not map beacon\n");
2374173362Sbenjsc		m_freem(m0);
2375173362Sbenjsc		return error;
2376173362Sbenjsc	}
2377173362Sbenjsc
2378173362Sbenjsc	data->m = m0;
2379173362Sbenjsc
2380173362Sbenjsc	/* first scatter/gather segment is used by the beacon command */
2381173362Sbenjsc	desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24);
2382173362Sbenjsc	desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2383173362Sbenjsc		ring->cur * sizeof (struct wpi_tx_cmd));
2384173362Sbenjsc	desc->segs[0].len  = htole32(4 + sizeof (struct wpi_cmd_beacon));
2385173362Sbenjsc	desc->segs[1].addr = htole32(physaddr);
2386173362Sbenjsc	desc->segs[1].len  = htole32(m0->m_pkthdr.len);
2387173362Sbenjsc
2388173362Sbenjsc	/* kick cmd ring */
2389173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2390173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2391173362Sbenjsc
2392173362Sbenjsc	return 0;
2393173362Sbenjsc}
2394173362Sbenjsc#endif
2395173362Sbenjsc
2396173362Sbenjscstatic int
2397178354Ssamwpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap)
2398173362Sbenjsc{
2399178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2400178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2401173362Sbenjsc	struct wpi_node_info node;
2402173362Sbenjsc	int error;
2403173362Sbenjsc
2404177043Sthompsa
2405173362Sbenjsc	/* update adapter's configuration */
2406177043Sthompsa	sc->config.associd = 0;
2407177043Sthompsa	sc->config.filter &= ~htole32(WPI_FILTER_BSS);
2408173362Sbenjsc	IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
2409173362Sbenjsc	sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2410173362Sbenjsc	if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2411173362Sbenjsc		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2412173362Sbenjsc		    WPI_CONFIG_24GHZ);
2413173362Sbenjsc	}
2414178354Ssam	if (IEEE80211_IS_CHAN_A(ni->ni_chan)) {
2415173362Sbenjsc		sc->config.cck_mask  = 0;
2416173362Sbenjsc		sc->config.ofdm_mask = 0x15;
2417178354Ssam	} else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) {
2418173362Sbenjsc		sc->config.cck_mask  = 0x03;
2419173362Sbenjsc		sc->config.ofdm_mask = 0;
2420178354Ssam	} else {
2421178354Ssam		/* XXX assume 802.11b/g */
2422173362Sbenjsc		sc->config.cck_mask  = 0x0f;
2423173362Sbenjsc		sc->config.ofdm_mask = 0x15;
2424173362Sbenjsc	}
2425173362Sbenjsc
2426173362Sbenjsc	DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
2427173362Sbenjsc		sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
2428173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2429173362Sbenjsc		sizeof (struct wpi_config), 1);
2430173362Sbenjsc	if (error != 0) {
2431173362Sbenjsc		device_printf(sc->sc_dev, "could not configure\n");
2432173362Sbenjsc		return error;
2433173362Sbenjsc	}
2434173362Sbenjsc
2435173362Sbenjsc	/* configuration has changed, set Tx power accordingly */
2436173362Sbenjsc	if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) {
2437173362Sbenjsc		device_printf(sc->sc_dev, "could not set Tx power\n");
2438173362Sbenjsc		return error;
2439173362Sbenjsc	}
2440173362Sbenjsc
2441173362Sbenjsc	/* add default node */
2442173362Sbenjsc	memset(&node, 0, sizeof node);
2443173362Sbenjsc	IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid);
2444173362Sbenjsc	node.id = WPI_ID_BSS;
2445173362Sbenjsc	node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2446173362Sbenjsc	    wpi_plcp_signal(12) : wpi_plcp_signal(2);
2447173362Sbenjsc	node.action = htole32(WPI_ACTION_SET_RATE);
2448173362Sbenjsc	node.antenna = WPI_ANTENNA_BOTH;
2449173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2450177043Sthompsa	if (error != 0)
2451177043Sthompsa		device_printf(sc->sc_dev, "could not add BSS node\n");
2452177043Sthompsa
2453177043Sthompsa	return (error);
2454177043Sthompsa}
2455177043Sthompsa
2456177043Sthompsastatic int
2457178354Ssamwpi_run(struct wpi_softc *sc, struct ieee80211vap *vap)
2458177043Sthompsa{
2459178354Ssam	struct ieee80211com *ic = vap->iv_ic;
2460178354Ssam	struct ieee80211_node *ni = vap->iv_bss;
2461177043Sthompsa	int error;
2462177043Sthompsa
2463178354Ssam	if (vap->iv_opmode == IEEE80211_M_MONITOR) {
2464178354Ssam		/* link LED blinks while monitoring */
2465178354Ssam		wpi_set_led(sc, WPI_LED_LINK, 5, 5);
2466178354Ssam		return 0;
2467178354Ssam	}
2468178354Ssam
2469177043Sthompsa	wpi_enable_tsf(sc, ni);
2470177043Sthompsa
2471177043Sthompsa	/* update adapter's configuration */
2472177043Sthompsa	sc->config.associd = htole16(ni->ni_associd & ~0xc000);
2473177043Sthompsa	/* short preamble/slot time are negotiated when associating */
2474177043Sthompsa	sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE |
2475177043Sthompsa	    WPI_CONFIG_SHSLOT);
2476177043Sthompsa	if (ic->ic_flags & IEEE80211_F_SHSLOT)
2477177043Sthompsa		sc->config.flags |= htole32(WPI_CONFIG_SHSLOT);
2478177043Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2479177043Sthompsa		sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE);
2480177043Sthompsa	sc->config.filter |= htole32(WPI_FILTER_BSS);
2481177043Sthompsa
2482177043Sthompsa	/* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */
2483177043Sthompsa
2484177043Sthompsa	DPRINTF(("config chan %d flags %x\n", sc->config.chan,
2485177043Sthompsa		    sc->config.flags));
2486177043Sthompsa	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, sizeof (struct
2487177043Sthompsa		    wpi_config), 1);
2488173362Sbenjsc	if (error != 0) {
2489177043Sthompsa		device_printf(sc->sc_dev, "could not update configuration\n");
2490173362Sbenjsc		return error;
2491173362Sbenjsc	}
2492173362Sbenjsc
2493178354Ssam	error = wpi_set_txpower(sc, ni->ni_chan, 1);
2494177043Sthompsa	if (error != 0) {
2495177043Sthompsa		device_printf(sc->sc_dev, "could set txpower\n");
2496177043Sthompsa		return error;
2497177043Sthompsa	}
2498173362Sbenjsc
2499177043Sthompsa	/* link LED always on while associated */
2500177043Sthompsa	wpi_set_led(sc, WPI_LED_LINK, 0, 1);
2501177043Sthompsa
2502177043Sthompsa	/* start automatic rate control timer */
2503178354Ssam	callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
2504177043Sthompsa
2505177043Sthompsa	return (error);
2506173362Sbenjsc}
2507173362Sbenjsc
2508173362Sbenjsc/*
2509173362Sbenjsc * Send a scan request to the firmware.  Since this command is huge, we map it
2510173362Sbenjsc * into a mbufcluster instead of using the pre-allocated set of commands. Note,
2511173362Sbenjsc * much of this code is similar to that in wpi_cmd but because we must manually
2512173362Sbenjsc * construct the probe & channels, we duplicate what's needed here. XXX In the
2513173362Sbenjsc * future, this function should be modified to use wpi_cmd to help cleanup the
2514173362Sbenjsc * code base.
2515173362Sbenjsc */
2516173362Sbenjscstatic int
2517173362Sbenjscwpi_scan(struct wpi_softc *sc)
2518173362Sbenjsc{
2519178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2520178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2521177043Sthompsa	struct ieee80211_scan_state *ss = ic->ic_scan;
2522173362Sbenjsc	struct wpi_tx_ring *ring = &sc->cmdq;
2523173362Sbenjsc	struct wpi_tx_desc *desc;
2524173362Sbenjsc	struct wpi_tx_data *data;
2525173362Sbenjsc	struct wpi_tx_cmd *cmd;
2526173362Sbenjsc	struct wpi_scan_hdr *hdr;
2527173362Sbenjsc	struct wpi_scan_chan *chan;
2528173362Sbenjsc	struct ieee80211_frame *wh;
2529173362Sbenjsc	struct ieee80211_rateset *rs;
2530173362Sbenjsc	struct ieee80211_channel *c;
2531173362Sbenjsc	enum ieee80211_phymode mode;
2532173362Sbenjsc	uint8_t *frm;
2533177043Sthompsa	int nrates, pktlen, error, i, nssid;
2534173362Sbenjsc	bus_addr_t physaddr;
2535173362Sbenjsc
2536173362Sbenjsc	desc = &ring->desc[ring->cur];
2537173362Sbenjsc	data = &ring->data[ring->cur];
2538173362Sbenjsc
2539173362Sbenjsc	data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2540173362Sbenjsc	if (data->m == NULL) {
2541173362Sbenjsc		device_printf(sc->sc_dev,
2542173362Sbenjsc		    "could not allocate mbuf for scan command\n");
2543173362Sbenjsc		return ENOMEM;
2544173362Sbenjsc	}
2545173362Sbenjsc
2546173362Sbenjsc	cmd = mtod(data->m, struct wpi_tx_cmd *);
2547173362Sbenjsc	cmd->code = WPI_CMD_SCAN;
2548173362Sbenjsc	cmd->flags = 0;
2549173362Sbenjsc	cmd->qid = ring->qid;
2550173362Sbenjsc	cmd->idx = ring->cur;
2551173362Sbenjsc
2552173362Sbenjsc	hdr = (struct wpi_scan_hdr *)cmd->data;
2553173362Sbenjsc	memset(hdr, 0, sizeof(struct wpi_scan_hdr));
2554173362Sbenjsc
2555173362Sbenjsc	/*
2556173362Sbenjsc	 * Move to the next channel if no packets are received within 5 msecs
2557173362Sbenjsc	 * after sending the probe request (this helps to reduce the duration
2558173362Sbenjsc	 * of active scans).
2559173362Sbenjsc	 */
2560173362Sbenjsc	hdr->quiet = htole16(5);
2561173362Sbenjsc	hdr->threshold = htole16(1);
2562173362Sbenjsc
2563173362Sbenjsc	if (IEEE80211_IS_CHAN_A(ic->ic_curchan)) {
2564173362Sbenjsc		/* send probe requests at 6Mbps */
2565173362Sbenjsc		hdr->tx.rate = wpi_ridx_to_plcp[WPI_OFDM6];
2566173362Sbenjsc
2567173362Sbenjsc		/* Enable crc checking */
2568173362Sbenjsc		hdr->promotion = htole16(1);
2569173362Sbenjsc	} else {
2570173362Sbenjsc		hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO);
2571173362Sbenjsc		/* send probe requests at 1Mbps */
2572173362Sbenjsc		hdr->tx.rate = wpi_ridx_to_plcp[WPI_CCK1];
2573173362Sbenjsc	}
2574173362Sbenjsc	hdr->tx.id = WPI_ID_BROADCAST;
2575173362Sbenjsc	hdr->tx.lifetime = htole32(WPI_LIFETIME_INFINITE);
2576173362Sbenjsc	hdr->tx.flags = htole32(WPI_TX_AUTO_SEQ);
2577173362Sbenjsc
2578178354Ssam	memset(hdr->scan_essids, 0, sizeof(hdr->scan_essids));
2579177043Sthompsa	nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS);
2580178354Ssam	for (i = 0; i < nssid; i++) {
2581177043Sthompsa		hdr->scan_essids[i].id = IEEE80211_ELEMID_SSID;
2582177043Sthompsa		hdr->scan_essids[i].esslen = MIN(ss->ss_ssid[i].len, 32);
2583177043Sthompsa		memcpy(hdr->scan_essids[i].essid, ss->ss_ssid[i].ssid,
2584177043Sthompsa		    hdr->scan_essids[i].esslen);
2585179957Sthompsa#ifdef WPI_DEBUG
2586177043Sthompsa		if (wpi_debug & WPI_DEBUG_SCANNING) {
2587177043Sthompsa			printf("Scanning Essid: ");
2588178354Ssam			ieee80211_print_essid(hdr->scan_essids[i].essid,
2589178354Ssam			    hdr->scan_essids[i].esslen);
2590177043Sthompsa			printf("\n");
2591177043Sthompsa		}
2592179957Sthompsa#endif
2593173362Sbenjsc	}
2594173362Sbenjsc
2595173362Sbenjsc	/*
2596173362Sbenjsc	 * Build a probe request frame.  Most of the following code is a
2597173362Sbenjsc	 * copy & paste of what is done in net80211.
2598173362Sbenjsc	 */
2599173362Sbenjsc	wh = (struct ieee80211_frame *)&hdr->scan_essids[4];
2600173362Sbenjsc	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2601173362Sbenjsc		IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2602173362Sbenjsc	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2603173362Sbenjsc	IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
2604190526Ssam	IEEE80211_ADDR_COPY(wh->i_addr2, IF_LLADDR(ifp));
2605173362Sbenjsc	IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
2606173362Sbenjsc	*(u_int16_t *)&wh->i_dur[0] = 0;	/* filled by h/w */
2607173362Sbenjsc	*(u_int16_t *)&wh->i_seq[0] = 0;	/* filled by h/w */
2608173362Sbenjsc
2609173362Sbenjsc	frm = (uint8_t *)(wh + 1);
2610173362Sbenjsc
2611173362Sbenjsc	/* add essid IE, the hardware will fill this in for us */
2612173362Sbenjsc	*frm++ = IEEE80211_ELEMID_SSID;
2613173362Sbenjsc	*frm++ = 0;
2614173362Sbenjsc
2615173362Sbenjsc	mode = ieee80211_chan2mode(ic->ic_curchan);
2616173362Sbenjsc	rs = &ic->ic_sup_rates[mode];
2617173362Sbenjsc
2618173362Sbenjsc	/* add supported rates IE */
2619173362Sbenjsc	*frm++ = IEEE80211_ELEMID_RATES;
2620173362Sbenjsc	nrates = rs->rs_nrates;
2621173362Sbenjsc	if (nrates > IEEE80211_RATE_SIZE)
2622173362Sbenjsc		nrates = IEEE80211_RATE_SIZE;
2623173362Sbenjsc	*frm++ = nrates;
2624173362Sbenjsc	memcpy(frm, rs->rs_rates, nrates);
2625173362Sbenjsc	frm += nrates;
2626173362Sbenjsc
2627173362Sbenjsc	/* add supported xrates IE */
2628173362Sbenjsc	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2629173362Sbenjsc		nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2630173362Sbenjsc		*frm++ = IEEE80211_ELEMID_XRATES;
2631173362Sbenjsc		*frm++ = nrates;
2632173362Sbenjsc		memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2633173362Sbenjsc		frm += nrates;
2634173362Sbenjsc	}
2635173362Sbenjsc
2636173362Sbenjsc	/* setup length of probe request */
2637173362Sbenjsc	hdr->tx.len = htole16(frm - (uint8_t *)wh);
2638173362Sbenjsc
2639173362Sbenjsc	/*
2640173362Sbenjsc	 * Construct information about the channel that we
2641173362Sbenjsc	 * want to scan. The firmware expects this to be directly
2642173362Sbenjsc	 * after the scan probe request
2643173362Sbenjsc	 */
2644173362Sbenjsc	c = ic->ic_curchan;
2645173362Sbenjsc	chan = (struct wpi_scan_chan *)frm;
2646173362Sbenjsc	chan->chan = ieee80211_chan2ieee(ic, c);
2647173362Sbenjsc	chan->flags = 0;
2648173362Sbenjsc	if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
2649173362Sbenjsc		chan->flags |= WPI_CHAN_ACTIVE;
2650178354Ssam		if (nssid != 0)
2651173362Sbenjsc			chan->flags |= WPI_CHAN_DIRECT;
2652173362Sbenjsc	}
2653173362Sbenjsc	chan->gain_dsp = 0x6e; /* Default level */
2654173362Sbenjsc	if (IEEE80211_IS_CHAN_5GHZ(c)) {
2655173362Sbenjsc		chan->active = htole16(10);
2656178354Ssam		chan->passive = htole16(ss->ss_maxdwell);
2657173362Sbenjsc		chan->gain_radio = 0x3b;
2658173362Sbenjsc	} else {
2659173362Sbenjsc		chan->active = htole16(20);
2660178354Ssam		chan->passive = htole16(ss->ss_maxdwell);
2661173362Sbenjsc		chan->gain_radio = 0x28;
2662173362Sbenjsc	}
2663173362Sbenjsc
2664173362Sbenjsc	DPRINTFN(WPI_DEBUG_SCANNING,
2665173362Sbenjsc	    ("Scanning %u Passive: %d\n",
2666173362Sbenjsc	     chan->chan,
2667173362Sbenjsc	     c->ic_flags & IEEE80211_CHAN_PASSIVE));
2668173362Sbenjsc
2669173362Sbenjsc	hdr->nchan++;
2670173362Sbenjsc	chan++;
2671173362Sbenjsc
2672173362Sbenjsc	frm += sizeof (struct wpi_scan_chan);
2673173362Sbenjsc#if 0
2674173362Sbenjsc	// XXX All Channels....
2675173362Sbenjsc	for (c  = &ic->ic_channels[1];
2676173362Sbenjsc	     c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
2677173362Sbenjsc		if ((c->ic_flags & ic->ic_curchan->ic_flags) != ic->ic_curchan->ic_flags)
2678173362Sbenjsc			continue;
2679173362Sbenjsc
2680173362Sbenjsc		chan->chan = ieee80211_chan2ieee(ic, c);
2681173362Sbenjsc		chan->flags = 0;
2682173362Sbenjsc		if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
2683173362Sbenjsc		    chan->flags |= WPI_CHAN_ACTIVE;
2684173362Sbenjsc		    if (ic->ic_des_ssid[0].len != 0)
2685173362Sbenjsc			chan->flags |= WPI_CHAN_DIRECT;
2686173362Sbenjsc		}
2687173362Sbenjsc		chan->gain_dsp = 0x6e; /* Default level */
2688173362Sbenjsc		if (IEEE80211_IS_CHAN_5GHZ(c)) {
2689173362Sbenjsc			chan->active = htole16(10);
2690173362Sbenjsc			chan->passive = htole16(110);
2691173362Sbenjsc			chan->gain_radio = 0x3b;
2692173362Sbenjsc		} else {
2693173362Sbenjsc			chan->active = htole16(20);
2694173362Sbenjsc			chan->passive = htole16(120);
2695173362Sbenjsc			chan->gain_radio = 0x28;
2696173362Sbenjsc		}
2697173362Sbenjsc
2698173362Sbenjsc		DPRINTFN(WPI_DEBUG_SCANNING,
2699173362Sbenjsc			 ("Scanning %u Passive: %d\n",
2700173362Sbenjsc			  chan->chan,
2701173362Sbenjsc			  c->ic_flags & IEEE80211_CHAN_PASSIVE));
2702173362Sbenjsc
2703173362Sbenjsc		hdr->nchan++;
2704173362Sbenjsc		chan++;
2705173362Sbenjsc
2706173362Sbenjsc		frm += sizeof (struct wpi_scan_chan);
2707173362Sbenjsc	}
2708173362Sbenjsc#endif
2709173362Sbenjsc
2710173362Sbenjsc	hdr->len = htole16(frm - (uint8_t *)hdr);
2711173362Sbenjsc	pktlen = frm - (uint8_t *)cmd;
2712173362Sbenjsc
2713173362Sbenjsc	error = bus_dmamap_load(ring->data_dmat, data->map, cmd, pktlen,
2714173362Sbenjsc	    wpi_dma_map_addr, &physaddr, BUS_DMA_NOWAIT);
2715173362Sbenjsc	if (error != 0) {
2716173362Sbenjsc		device_printf(sc->sc_dev, "could not map scan command\n");
2717173362Sbenjsc		m_freem(data->m);
2718173362Sbenjsc		data->m = NULL;
2719173362Sbenjsc		return error;
2720173362Sbenjsc	}
2721173362Sbenjsc
2722173362Sbenjsc	desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24);
2723173362Sbenjsc	desc->segs[0].addr = htole32(physaddr);
2724173362Sbenjsc	desc->segs[0].len  = htole32(pktlen);
2725173362Sbenjsc
2726173362Sbenjsc	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2727173362Sbenjsc	    BUS_DMASYNC_PREWRITE);
2728173362Sbenjsc	bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2729173362Sbenjsc
2730173362Sbenjsc	/* kick cmd ring */
2731173362Sbenjsc	ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2732173362Sbenjsc	WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2733173362Sbenjsc
2734177043Sthompsa	sc->sc_scan_timer = 5;
2735173362Sbenjsc	return 0;	/* will be notified async. of failure/success */
2736173362Sbenjsc}
2737173362Sbenjsc
2738173362Sbenjsc/**
2739173362Sbenjsc * Configure the card to listen to a particular channel, this transisions the
2740173362Sbenjsc * card in to being able to receive frames from remote devices.
2741173362Sbenjsc */
2742173362Sbenjscstatic int
2743173362Sbenjscwpi_config(struct wpi_softc *sc)
2744173362Sbenjsc{
2745178354Ssam	struct ifnet *ifp = sc->sc_ifp;
2746178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2747173362Sbenjsc	struct wpi_power power;
2748173362Sbenjsc	struct wpi_bluetooth bluetooth;
2749173362Sbenjsc	struct wpi_node_info node;
2750173362Sbenjsc	int error;
2751173362Sbenjsc
2752173362Sbenjsc	/* set power mode */
2753173362Sbenjsc	memset(&power, 0, sizeof power);
2754173362Sbenjsc	power.flags = htole32(WPI_POWER_CAM|0x8);
2755173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0);
2756173362Sbenjsc	if (error != 0) {
2757173362Sbenjsc		device_printf(sc->sc_dev, "could not set power mode\n");
2758173362Sbenjsc		return error;
2759173362Sbenjsc	}
2760173362Sbenjsc
2761173362Sbenjsc	/* configure bluetooth coexistence */
2762173362Sbenjsc	memset(&bluetooth, 0, sizeof bluetooth);
2763173362Sbenjsc	bluetooth.flags = 3;
2764173362Sbenjsc	bluetooth.lead = 0xaa;
2765173362Sbenjsc	bluetooth.kill = 1;
2766173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
2767173362Sbenjsc	    0);
2768173362Sbenjsc	if (error != 0) {
2769173362Sbenjsc		device_printf(sc->sc_dev,
2770173362Sbenjsc		    "could not configure bluetooth coexistence\n");
2771173362Sbenjsc		return error;
2772173362Sbenjsc	}
2773173362Sbenjsc
2774173362Sbenjsc	/* configure adapter */
2775173362Sbenjsc	memset(&sc->config, 0, sizeof (struct wpi_config));
2776190526Ssam	IEEE80211_ADDR_COPY(sc->config.myaddr, IF_LLADDR(ifp));
2777173362Sbenjsc	/*set default channel*/
2778173362Sbenjsc	sc->config.chan = htole16(ieee80211_chan2ieee(ic, ic->ic_curchan));
2779173362Sbenjsc	sc->config.flags = htole32(WPI_CONFIG_TSF);
2780173362Sbenjsc	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2781173362Sbenjsc		sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2782173362Sbenjsc		    WPI_CONFIG_24GHZ);
2783173362Sbenjsc	}
2784173362Sbenjsc	sc->config.filter = 0;
2785173362Sbenjsc	switch (ic->ic_opmode) {
2786173362Sbenjsc	case IEEE80211_M_STA:
2787173362Sbenjsc	case IEEE80211_M_WDS:	/* No know setup, use STA for now */
2788173362Sbenjsc		sc->config.mode = WPI_MODE_STA;
2789173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_MULTICAST);
2790173362Sbenjsc		break;
2791173362Sbenjsc	case IEEE80211_M_IBSS:
2792173362Sbenjsc	case IEEE80211_M_AHDEMO:
2793173362Sbenjsc		sc->config.mode = WPI_MODE_IBSS;
2794173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_BEACON |
2795173362Sbenjsc					     WPI_FILTER_MULTICAST);
2796173362Sbenjsc		break;
2797173362Sbenjsc	case IEEE80211_M_HOSTAP:
2798173362Sbenjsc		sc->config.mode = WPI_MODE_HOSTAP;
2799173362Sbenjsc		break;
2800173362Sbenjsc	case IEEE80211_M_MONITOR:
2801173362Sbenjsc		sc->config.mode = WPI_MODE_MONITOR;
2802173362Sbenjsc		sc->config.filter |= htole32(WPI_FILTER_MULTICAST |
2803173362Sbenjsc			WPI_FILTER_CTL | WPI_FILTER_PROMISC);
2804173362Sbenjsc		break;
2805195562Srpaulo	default:
2806195562Srpaulo		device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2807195562Srpaulo		return EINVAL;
2808173362Sbenjsc	}
2809173362Sbenjsc	sc->config.cck_mask  = 0x0f;	/* not yet negotiated */
2810173362Sbenjsc	sc->config.ofdm_mask = 0xff;	/* not yet negotiated */
2811173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2812173362Sbenjsc		sizeof (struct wpi_config), 0);
2813173362Sbenjsc	if (error != 0) {
2814173362Sbenjsc		device_printf(sc->sc_dev, "configure command failed\n");
2815173362Sbenjsc		return error;
2816173362Sbenjsc	}
2817173362Sbenjsc
2818173362Sbenjsc	/* configuration has changed, set Tx power accordingly */
2819177043Sthompsa	if ((error = wpi_set_txpower(sc, ic->ic_curchan, 0)) != 0) {
2820173362Sbenjsc	    device_printf(sc->sc_dev, "could not set Tx power\n");
2821173362Sbenjsc	    return error;
2822173362Sbenjsc	}
2823173362Sbenjsc
2824173362Sbenjsc	/* add broadcast node */
2825173362Sbenjsc	memset(&node, 0, sizeof node);
2826173362Sbenjsc	IEEE80211_ADDR_COPY(node.bssid, ifp->if_broadcastaddr);
2827173362Sbenjsc	node.id = WPI_ID_BROADCAST;
2828173362Sbenjsc	node.rate = wpi_plcp_signal(2);
2829173362Sbenjsc	error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
2830173362Sbenjsc	if (error != 0) {
2831173362Sbenjsc		device_printf(sc->sc_dev, "could not add broadcast node\n");
2832173362Sbenjsc		return error;
2833173362Sbenjsc	}
2834173362Sbenjsc
2835173362Sbenjsc	/* Setup rate scalling */
2836173362Sbenjsc	error = wpi_mrr_setup(sc);
2837173362Sbenjsc	if (error != 0) {
2838173362Sbenjsc		device_printf(sc->sc_dev, "could not setup MRR\n");
2839173362Sbenjsc		return error;
2840173362Sbenjsc	}
2841173362Sbenjsc
2842173362Sbenjsc	return 0;
2843173362Sbenjsc}
2844173362Sbenjsc
2845173362Sbenjscstatic void
2846173362Sbenjscwpi_stop_master(struct wpi_softc *sc)
2847173362Sbenjsc{
2848173362Sbenjsc	uint32_t tmp;
2849173362Sbenjsc	int ntries;
2850173362Sbenjsc
2851173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,("Disabling Firmware execution\n"));
2852173362Sbenjsc
2853173362Sbenjsc	tmp = WPI_READ(sc, WPI_RESET);
2854173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER | WPI_NEVO_RESET);
2855173362Sbenjsc
2856173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
2857173362Sbenjsc	if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP)
2858173362Sbenjsc		return;	/* already asleep */
2859173362Sbenjsc
2860173362Sbenjsc	for (ntries = 0; ntries < 100; ntries++) {
2861173362Sbenjsc		if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED)
2862173362Sbenjsc			break;
2863173362Sbenjsc		DELAY(10);
2864173362Sbenjsc	}
2865173362Sbenjsc	if (ntries == 100) {
2866173362Sbenjsc		device_printf(sc->sc_dev, "timeout waiting for master\n");
2867173362Sbenjsc	}
2868173362Sbenjsc}
2869173362Sbenjsc
2870173362Sbenjscstatic int
2871173362Sbenjscwpi_power_up(struct wpi_softc *sc)
2872173362Sbenjsc{
2873173362Sbenjsc	uint32_t tmp;
2874173362Sbenjsc	int ntries;
2875173362Sbenjsc
2876173362Sbenjsc	wpi_mem_lock(sc);
2877173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_POWER);
2878173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000);
2879173362Sbenjsc	wpi_mem_unlock(sc);
2880173362Sbenjsc
2881173362Sbenjsc	for (ntries = 0; ntries < 5000; ntries++) {
2882173362Sbenjsc		if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED)
2883173362Sbenjsc			break;
2884173362Sbenjsc		DELAY(10);
2885173362Sbenjsc	}
2886173362Sbenjsc	if (ntries == 5000) {
2887173362Sbenjsc		device_printf(sc->sc_dev,
2888173362Sbenjsc		    "timeout waiting for NIC to power up\n");
2889173362Sbenjsc		return ETIMEDOUT;
2890173362Sbenjsc	}
2891173362Sbenjsc	return 0;
2892173362Sbenjsc}
2893173362Sbenjsc
2894173362Sbenjscstatic int
2895173362Sbenjscwpi_reset(struct wpi_softc *sc)
2896173362Sbenjsc{
2897173362Sbenjsc	uint32_t tmp;
2898173362Sbenjsc	int ntries;
2899173362Sbenjsc
2900173362Sbenjsc	DPRINTFN(WPI_DEBUG_HW,
2901173362Sbenjsc	    ("Resetting the card - clearing any uploaded firmware\n"));
2902173362Sbenjsc
2903173362Sbenjsc	/* clear any pending interrupts */
2904173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
2905173362Sbenjsc
2906173362Sbenjsc	tmp = WPI_READ(sc, WPI_PLL_CTL);
2907173362Sbenjsc	WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT);
2908173362Sbenjsc
2909173362Sbenjsc	tmp = WPI_READ(sc, WPI_CHICKEN);
2910173362Sbenjsc	WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS);
2911173362Sbenjsc
2912173362Sbenjsc	tmp = WPI_READ(sc, WPI_GPIO_CTL);
2913173362Sbenjsc	WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT);
2914173362Sbenjsc
2915173362Sbenjsc	/* wait for clock stabilization */
2916173362Sbenjsc	for (ntries = 0; ntries < 25000; ntries++) {
2917173362Sbenjsc		if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK)
2918173362Sbenjsc			break;
2919173362Sbenjsc		DELAY(10);
2920173362Sbenjsc	}
2921173362Sbenjsc	if (ntries == 25000) {
2922173362Sbenjsc		device_printf(sc->sc_dev,
2923173362Sbenjsc		    "timeout waiting for clock stabilization\n");
2924173362Sbenjsc		return ETIMEDOUT;
2925173362Sbenjsc	}
2926173362Sbenjsc
2927173362Sbenjsc	/* initialize EEPROM */
2928173362Sbenjsc	tmp = WPI_READ(sc, WPI_EEPROM_STATUS);
2929173362Sbenjsc
2930173362Sbenjsc	if ((tmp & WPI_EEPROM_VERSION) == 0) {
2931173362Sbenjsc		device_printf(sc->sc_dev, "EEPROM not found\n");
2932173362Sbenjsc		return EIO;
2933173362Sbenjsc	}
2934173362Sbenjsc	WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED);
2935173362Sbenjsc
2936173362Sbenjsc	return 0;
2937173362Sbenjsc}
2938173362Sbenjsc
2939173362Sbenjscstatic void
2940173362Sbenjscwpi_hw_config(struct wpi_softc *sc)
2941173362Sbenjsc{
2942173362Sbenjsc	uint32_t rev, hw;
2943173362Sbenjsc
2944173362Sbenjsc	/* voodoo from the Linux "driver".. */
2945173362Sbenjsc	hw = WPI_READ(sc, WPI_HWCONFIG);
2946173362Sbenjsc
2947173362Sbenjsc	rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1);
2948173362Sbenjsc	if ((rev & 0xc0) == 0x40)
2949173362Sbenjsc		hw |= WPI_HW_ALM_MB;
2950173362Sbenjsc	else if (!(rev & 0x80))
2951173362Sbenjsc		hw |= WPI_HW_ALM_MM;
2952173362Sbenjsc
2953173362Sbenjsc	if (sc->cap == 0x80)
2954173362Sbenjsc		hw |= WPI_HW_SKU_MRC;
2955173362Sbenjsc
2956173362Sbenjsc	hw &= ~WPI_HW_REV_D;
2957173362Sbenjsc	if ((le16toh(sc->rev) & 0xf0) == 0xd0)
2958173362Sbenjsc		hw |= WPI_HW_REV_D;
2959173362Sbenjsc
2960173362Sbenjsc	if (sc->type > 1)
2961173362Sbenjsc		hw |= WPI_HW_TYPE_B;
2962173362Sbenjsc
2963173362Sbenjsc	WPI_WRITE(sc, WPI_HWCONFIG, hw);
2964173362Sbenjsc}
2965173362Sbenjsc
2966173362Sbenjscstatic void
2967177043Sthompsawpi_rfkill_resume(struct wpi_softc *sc)
2968177043Sthompsa{
2969177043Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2970178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
2971178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2972177043Sthompsa	int ntries;
2973177043Sthompsa
2974177043Sthompsa	/* enable firmware again */
2975177043Sthompsa	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
2976177043Sthompsa	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
2977177043Sthompsa
2978177043Sthompsa	/* wait for thermal sensors to calibrate */
2979177043Sthompsa	for (ntries = 0; ntries < 1000; ntries++) {
2980177043Sthompsa		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
2981177043Sthompsa			break;
2982177043Sthompsa		DELAY(10);
2983177043Sthompsa	}
2984177043Sthompsa
2985177043Sthompsa	if (ntries == 1000) {
2986177043Sthompsa		device_printf(sc->sc_dev,
2987177043Sthompsa		    "timeout waiting for thermal calibration\n");
2988177043Sthompsa		WPI_UNLOCK(sc);
2989177043Sthompsa		return;
2990177043Sthompsa	}
2991177043Sthompsa	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d\n", sc->temp));
2992177043Sthompsa
2993177043Sthompsa	if (wpi_config(sc) != 0) {
2994177043Sthompsa		device_printf(sc->sc_dev, "device config failed\n");
2995177043Sthompsa		WPI_UNLOCK(sc);
2996177043Sthompsa		return;
2997177043Sthompsa	}
2998177043Sthompsa
2999177043Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3000177043Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3001177043Sthompsa	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3002177043Sthompsa
3003178354Ssam	if (vap != NULL) {
3004178354Ssam		if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
3005178354Ssam			if (vap->iv_opmode != IEEE80211_M_MONITOR) {
3006191746Sthompsa				ieee80211_beacon_miss(ic);
3007178354Ssam				wpi_set_led(sc, WPI_LED_LINK, 0, 1);
3008178354Ssam			} else
3009178354Ssam				wpi_set_led(sc, WPI_LED_LINK, 5, 5);
3010178354Ssam		} else {
3011178354Ssam			ieee80211_scan_next(vap);
3012177043Sthompsa			wpi_set_led(sc, WPI_LED_LINK, 20, 2);
3013178354Ssam		}
3014177043Sthompsa	}
3015177043Sthompsa
3016177043Sthompsa	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3017177043Sthompsa}
3018177043Sthompsa
3019177043Sthompsastatic void
3020177043Sthompsawpi_init_locked(struct wpi_softc *sc, int force)
3021177043Sthompsa{
3022178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3023173362Sbenjsc	uint32_t tmp;
3024177043Sthompsa	int ntries, qid;
3025173362Sbenjsc
3026173362Sbenjsc	wpi_stop_locked(sc);
3027173362Sbenjsc	(void)wpi_reset(sc);
3028173362Sbenjsc
3029173362Sbenjsc	wpi_mem_lock(sc);
3030173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00);
3031173362Sbenjsc	DELAY(20);
3032173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
3033173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800);
3034173362Sbenjsc	wpi_mem_unlock(sc);
3035173362Sbenjsc
3036173362Sbenjsc	(void)wpi_power_up(sc);
3037173362Sbenjsc	wpi_hw_config(sc);
3038173362Sbenjsc
3039173362Sbenjsc	/* init Rx ring */
3040173362Sbenjsc	wpi_mem_lock(sc);
3041173362Sbenjsc	WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr);
3042173362Sbenjsc	WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr +
3043173362Sbenjsc	    offsetof(struct wpi_shared, next));
3044173362Sbenjsc	WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
3045173362Sbenjsc	WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010);
3046173362Sbenjsc	wpi_mem_unlock(sc);
3047173362Sbenjsc
3048173362Sbenjsc	/* init Tx rings */
3049173362Sbenjsc	wpi_mem_lock(sc);
3050173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */
3051173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_RA, 1);   /* enable RA0 */
3052173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */
3053173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000);
3054173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002);
3055173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MAGIC4, 4);
3056173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MAGIC5, 5);
3057173362Sbenjsc
3058173362Sbenjsc	WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr);
3059173362Sbenjsc	WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5);
3060173362Sbenjsc
3061173362Sbenjsc	for (qid = 0; qid < 6; qid++) {
3062173362Sbenjsc		WPI_WRITE(sc, WPI_TX_CTL(qid), 0);
3063173362Sbenjsc		WPI_WRITE(sc, WPI_TX_BASE(qid), 0);
3064173362Sbenjsc		WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008);
3065173362Sbenjsc	}
3066173362Sbenjsc	wpi_mem_unlock(sc);
3067173362Sbenjsc
3068173362Sbenjsc	/* clear "radio off" and "disable command" bits (reversed logic) */
3069173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3070173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
3071173362Sbenjsc	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3072173362Sbenjsc
3073173362Sbenjsc	/* clear any pending interrupts */
3074173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, 0xffffffff);
3075173362Sbenjsc
3076173362Sbenjsc	/* enable interrupts */
3077173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
3078173362Sbenjsc
3079173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3080173362Sbenjsc	WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
3081173362Sbenjsc
3082177043Sthompsa	if ((wpi_load_firmware(sc)) != 0) {
3083173362Sbenjsc	    device_printf(sc->sc_dev,
3084173362Sbenjsc		"A problem occurred loading the firmware to the driver\n");
3085173362Sbenjsc	    return;
3086173362Sbenjsc	}
3087173362Sbenjsc
3088173362Sbenjsc	/* At this point the firmware is up and running. If the hardware
3089173362Sbenjsc	 * RF switch is turned off thermal calibration will fail, though
3090173362Sbenjsc	 * the card is still happy to continue to accept commands, catch
3091177043Sthompsa	 * this case and schedule a task to watch for it to be turned on.
3092173362Sbenjsc	 */
3093173362Sbenjsc	wpi_mem_lock(sc);
3094173362Sbenjsc	tmp = wpi_mem_read(sc, WPI_MEM_HW_RADIO_OFF);
3095173362Sbenjsc	wpi_mem_unlock(sc);
3096173362Sbenjsc
3097173362Sbenjsc	if (!(tmp & 0x1)) {
3098173362Sbenjsc		sc->flags |= WPI_FLAG_HW_RADIO_OFF;
3099173362Sbenjsc		device_printf(sc->sc_dev,"Radio Transmitter is switched off\n");
3100177043Sthompsa		goto out;
3101173362Sbenjsc	}
3102173362Sbenjsc
3103173362Sbenjsc	/* wait for thermal sensors to calibrate */
3104173362Sbenjsc	for (ntries = 0; ntries < 1000; ntries++) {
3105173362Sbenjsc		if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0)
3106173362Sbenjsc			break;
3107173362Sbenjsc		DELAY(10);
3108173362Sbenjsc	}
3109173362Sbenjsc
3110173362Sbenjsc	if (ntries == 1000) {
3111173362Sbenjsc		device_printf(sc->sc_dev,
3112173362Sbenjsc		    "timeout waiting for thermal sensors calibration\n");
3113173362Sbenjsc		return;
3114173362Sbenjsc	}
3115173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d\n", sc->temp));
3116173362Sbenjsc
3117177043Sthompsa	if (wpi_config(sc) != 0) {
3118177043Sthompsa		device_printf(sc->sc_dev, "device config failed\n");
3119177043Sthompsa		return;
3120177043Sthompsa	}
3121177043Sthompsa
3122173362Sbenjsc	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3123173362Sbenjsc	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3124177043Sthompsaout:
3125177043Sthompsa	callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3126173362Sbenjsc}
3127173362Sbenjsc
3128173362Sbenjscstatic void
3129178354Ssamwpi_init(void *arg)
3130173362Sbenjsc{
3131178354Ssam	struct wpi_softc *sc = arg;
3132178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3133178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3134173362Sbenjsc
3135173362Sbenjsc	WPI_LOCK(sc);
3136178354Ssam	wpi_init_locked(sc, 0);
3137173362Sbenjsc	WPI_UNLOCK(sc);
3138173362Sbenjsc
3139178354Ssam	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3140178354Ssam		ieee80211_start_all(ic);		/* start all vaps */
3141173362Sbenjsc}
3142178354Ssam
3143173362Sbenjscstatic void
3144173362Sbenjscwpi_stop_locked(struct wpi_softc *sc)
3145173362Sbenjsc{
3146178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3147173362Sbenjsc	uint32_t tmp;
3148173362Sbenjsc	int ac;
3149173362Sbenjsc
3150177043Sthompsa	sc->sc_tx_timer = 0;
3151177043Sthompsa	sc->sc_scan_timer = 0;
3152173362Sbenjsc	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3153177043Sthompsa	sc->flags &= ~WPI_FLAG_HW_RADIO_OFF;
3154177043Sthompsa	callout_stop(&sc->watchdog_to);
3155177043Sthompsa	callout_stop(&sc->calib_to);
3156173362Sbenjsc
3157177043Sthompsa
3158173362Sbenjsc	/* disable interrupts */
3159173362Sbenjsc	WPI_WRITE(sc, WPI_MASK, 0);
3160173362Sbenjsc	WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK);
3161173362Sbenjsc	WPI_WRITE(sc, WPI_INTR_STATUS, 0xff);
3162173362Sbenjsc	WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000);
3163173362Sbenjsc
3164173362Sbenjsc	wpi_mem_lock(sc);
3165173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_MODE, 0);
3166173362Sbenjsc	wpi_mem_unlock(sc);
3167173362Sbenjsc
3168173362Sbenjsc	/* reset all Tx rings */
3169173362Sbenjsc	for (ac = 0; ac < 4; ac++)
3170173362Sbenjsc		wpi_reset_tx_ring(sc, &sc->txq[ac]);
3171173362Sbenjsc	wpi_reset_tx_ring(sc, &sc->cmdq);
3172173362Sbenjsc
3173173362Sbenjsc	/* reset Rx ring */
3174173362Sbenjsc	wpi_reset_rx_ring(sc, &sc->rxq);
3175173362Sbenjsc
3176173362Sbenjsc	wpi_mem_lock(sc);
3177173362Sbenjsc	wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200);
3178173362Sbenjsc	wpi_mem_unlock(sc);
3179173362Sbenjsc
3180173362Sbenjsc	DELAY(5);
3181173362Sbenjsc
3182173362Sbenjsc	wpi_stop_master(sc);
3183173362Sbenjsc
3184173362Sbenjsc	tmp = WPI_READ(sc, WPI_RESET);
3185173362Sbenjsc	WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET);
3186173362Sbenjsc	sc->flags &= ~WPI_FLAG_BUSY;
3187173362Sbenjsc}
3188173362Sbenjsc
3189173362Sbenjscstatic void
3190178354Ssamwpi_stop(struct wpi_softc *sc)
3191173362Sbenjsc{
3192178354Ssam	WPI_LOCK(sc);
3193178354Ssam	wpi_stop_locked(sc);
3194178354Ssam	WPI_UNLOCK(sc);
3195173362Sbenjsc}
3196173362Sbenjsc
3197173362Sbenjscstatic void
3198173362Sbenjscwpi_calib_timeout(void *arg)
3199173362Sbenjsc{
3200173362Sbenjsc	struct wpi_softc *sc = arg;
3201178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3202178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3203178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3204173362Sbenjsc	int temp;
3205173362Sbenjsc
3206178354Ssam	if (vap->iv_state != IEEE80211_S_RUN)
3207177043Sthompsa		return;
3208177043Sthompsa
3209173362Sbenjsc	/* update sensor data */
3210173362Sbenjsc	temp = (int)WPI_READ(sc, WPI_TEMPERATURE);
3211173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("Temp in calibration is: %d\n", temp));
3212173362Sbenjsc
3213178354Ssam	wpi_power_calibration(sc, temp);
3214173362Sbenjsc
3215178354Ssam	callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
3216173362Sbenjsc}
3217173362Sbenjsc
3218173362Sbenjsc/*
3219173362Sbenjsc * This function is called periodically (every 60 seconds) to adjust output
3220173362Sbenjsc * power to temperature changes.
3221173362Sbenjsc */
3222173362Sbenjscstatic void
3223173362Sbenjscwpi_power_calibration(struct wpi_softc *sc, int temp)
3224173362Sbenjsc{
3225178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3226178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3227178354Ssam	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3228178354Ssam
3229173362Sbenjsc	/* sanity-check read value */
3230173362Sbenjsc	if (temp < -260 || temp > 25) {
3231173362Sbenjsc		/* this can't be correct, ignore */
3232173362Sbenjsc		DPRINTFN(WPI_DEBUG_TEMP,
3233173362Sbenjsc		    ("out-of-range temperature reported: %d\n", temp));
3234173362Sbenjsc		return;
3235173362Sbenjsc	}
3236173362Sbenjsc
3237173362Sbenjsc	DPRINTFN(WPI_DEBUG_TEMP,("temperature %d->%d\n", sc->temp, temp));
3238173362Sbenjsc
3239173362Sbenjsc	/* adjust Tx power if need be */
3240173362Sbenjsc	if (abs(temp - sc->temp) <= 6)
3241173362Sbenjsc		return;
3242173362Sbenjsc
3243173362Sbenjsc	sc->temp = temp;
3244173362Sbenjsc
3245178354Ssam	if (wpi_set_txpower(sc, vap->iv_bss->ni_chan, 1) != 0) {
3246173362Sbenjsc		/* just warn, too bad for the automatic calibration... */
3247173362Sbenjsc		device_printf(sc->sc_dev,"could not adjust Tx power\n");
3248173362Sbenjsc	}
3249173362Sbenjsc}
3250173362Sbenjsc
3251173362Sbenjsc/**
3252173362Sbenjsc * Read the eeprom to find out what channels are valid for the given
3253173362Sbenjsc * band and update net80211 with what we find.
3254173362Sbenjsc */
3255173362Sbenjscstatic void
3256173362Sbenjscwpi_read_eeprom_channels(struct wpi_softc *sc, int n)
3257173362Sbenjsc{
3258178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3259178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3260173362Sbenjsc	const struct wpi_chan_band *band = &wpi_bands[n];
3261173362Sbenjsc	struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND];
3262179957Sthompsa	struct ieee80211_channel *c;
3263179957Sthompsa	int chan, i, passive;
3264173362Sbenjsc
3265173362Sbenjsc	wpi_read_prom_data(sc, band->addr, channels,
3266173362Sbenjsc	    band->nchan * sizeof (struct wpi_eeprom_chan));
3267173362Sbenjsc
3268173362Sbenjsc	for (i = 0; i < band->nchan; i++) {
3269173362Sbenjsc		if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
3270173362Sbenjsc			DPRINTFN(WPI_DEBUG_HW,
3271173362Sbenjsc			    ("Channel Not Valid: %d, band %d\n",
3272173362Sbenjsc			     band->chan[i],n));
3273173362Sbenjsc			continue;
3274173362Sbenjsc		}
3275173362Sbenjsc
3276173362Sbenjsc		passive = 0;
3277173362Sbenjsc		chan = band->chan[i];
3278179957Sthompsa		c = &ic->ic_channels[ic->ic_nchans++];
3279173362Sbenjsc
3280173362Sbenjsc		/* is active scan allowed on this channel? */
3281173362Sbenjsc		if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) {
3282173362Sbenjsc			passive = IEEE80211_CHAN_PASSIVE;
3283173362Sbenjsc		}
3284173362Sbenjsc
3285173362Sbenjsc		if (n == 0) {	/* 2GHz band */
3286179957Sthompsa			c->ic_ieee = chan;
3287179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3288179957Sthompsa			    IEEE80211_CHAN_2GHZ);
3289179957Sthompsa			c->ic_flags = IEEE80211_CHAN_B | passive;
3290173362Sbenjsc
3291179957Sthompsa			c = &ic->ic_channels[ic->ic_nchans++];
3292179957Sthompsa			c->ic_ieee = chan;
3293179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3294179957Sthompsa			    IEEE80211_CHAN_2GHZ);
3295179957Sthompsa			c->ic_flags = IEEE80211_CHAN_G | passive;
3296179957Sthompsa
3297173362Sbenjsc		} else {	/* 5GHz band */
3298173362Sbenjsc			/*
3299173362Sbenjsc			 * Some 3945ABG adapters support channels 7, 8, 11
3300173362Sbenjsc			 * and 12 in the 2GHz *and* 5GHz bands.
3301173362Sbenjsc			 * Because of limitations in our net80211(9) stack,
3302173362Sbenjsc			 * we can't support these channels in 5GHz band.
3303173362Sbenjsc			 * XXX not true; just need to map to proper frequency
3304173362Sbenjsc			 */
3305173362Sbenjsc			if (chan <= 14)
3306173362Sbenjsc				continue;
3307173362Sbenjsc
3308179957Sthompsa			c->ic_ieee = chan;
3309179957Sthompsa			c->ic_freq = ieee80211_ieee2mhz(chan,
3310179957Sthompsa			    IEEE80211_CHAN_5GHZ);
3311179957Sthompsa			c->ic_flags = IEEE80211_CHAN_A | passive;
3312173362Sbenjsc		}
3313173362Sbenjsc
3314173362Sbenjsc		/* save maximum allowed power for this channel */
3315173362Sbenjsc		sc->maxpwr[chan] = channels[i].maxpwr;
3316173362Sbenjsc
3317173362Sbenjsc#if 0
3318173362Sbenjsc		// XXX We can probably use this an get rid of maxpwr - ben 20070617
3319173362Sbenjsc		ic->ic_channels[chan].ic_maxpower = channels[i].maxpwr;
3320173362Sbenjsc		//ic->ic_channels[chan].ic_minpower...
3321173362Sbenjsc		//ic->ic_channels[chan].ic_maxregtxpower...
3322173362Sbenjsc#endif
3323173362Sbenjsc
3324179957Sthompsa		DPRINTF(("adding chan %d (%dMHz) flags=0x%x maxpwr=%d"
3325179957Sthompsa		    " passive=%d, offset %d\n", chan, c->ic_freq,
3326179957Sthompsa		    channels[i].flags, sc->maxpwr[chan],
3327179957Sthompsa		    (c->ic_flags & IEEE80211_CHAN_PASSIVE) != 0,
3328179957Sthompsa		    ic->ic_nchans));
3329173362Sbenjsc	}
3330173362Sbenjsc}
3331173362Sbenjsc
3332173362Sbenjscstatic void
3333173362Sbenjscwpi_read_eeprom_group(struct wpi_softc *sc, int n)
3334173362Sbenjsc{
3335173362Sbenjsc	struct wpi_power_group *group = &sc->groups[n];
3336173362Sbenjsc	struct wpi_eeprom_group rgroup;
3337173362Sbenjsc	int i;
3338173362Sbenjsc
3339173362Sbenjsc	wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup,
3340173362Sbenjsc	    sizeof rgroup);
3341173362Sbenjsc
3342173362Sbenjsc	/* save power group information */
3343173362Sbenjsc	group->chan   = rgroup.chan;
3344173362Sbenjsc	group->maxpwr = rgroup.maxpwr;
3345173362Sbenjsc	/* temperature at which the samples were taken */
3346173362Sbenjsc	group->temp   = (int16_t)le16toh(rgroup.temp);
3347173362Sbenjsc
3348173362Sbenjsc	DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n,
3349173362Sbenjsc		    group->chan, group->maxpwr, group->temp));
3350173362Sbenjsc
3351173362Sbenjsc	for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
3352173362Sbenjsc		group->samples[i].index = rgroup.samples[i].index;
3353173362Sbenjsc		group->samples[i].power = rgroup.samples[i].power;
3354173362Sbenjsc
3355173362Sbenjsc		DPRINTF(("\tsample %d: index=%d power=%d\n", i,
3356173362Sbenjsc			    group->samples[i].index, group->samples[i].power));
3357173362Sbenjsc	}
3358173362Sbenjsc}
3359173362Sbenjsc
3360173362Sbenjsc/*
3361173362Sbenjsc * Update Tx power to match what is defined for channel `c'.
3362173362Sbenjsc */
3363173362Sbenjscstatic int
3364173362Sbenjscwpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async)
3365173362Sbenjsc{
3366178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3367178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3368173362Sbenjsc	struct wpi_power_group *group;
3369173362Sbenjsc	struct wpi_cmd_txpower txpower;
3370173362Sbenjsc	u_int chan;
3371173362Sbenjsc	int i;
3372173362Sbenjsc
3373173362Sbenjsc	/* get channel number */
3374173362Sbenjsc	chan = ieee80211_chan2ieee(ic, c);
3375173362Sbenjsc
3376173362Sbenjsc	/* find the power group to which this channel belongs */
3377173362Sbenjsc	if (IEEE80211_IS_CHAN_5GHZ(c)) {
3378173362Sbenjsc		for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
3379173362Sbenjsc			if (chan <= group->chan)
3380173362Sbenjsc				break;
3381173362Sbenjsc	} else
3382173362Sbenjsc		group = &sc->groups[0];
3383173362Sbenjsc
3384173362Sbenjsc	memset(&txpower, 0, sizeof txpower);
3385173362Sbenjsc	txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1;
3386173362Sbenjsc	txpower.channel = htole16(chan);
3387173362Sbenjsc
3388173362Sbenjsc	/* set Tx power for all OFDM and CCK rates */
3389173362Sbenjsc	for (i = 0; i <= 11 ; i++) {
3390173362Sbenjsc		/* retrieve Tx power for this channel/rate combination */
3391173362Sbenjsc		int idx = wpi_get_power_index(sc, group, c,
3392173362Sbenjsc		    wpi_ridx_to_rate[i]);
3393173362Sbenjsc
3394173362Sbenjsc		txpower.rates[i].rate = wpi_ridx_to_plcp[i];
3395173362Sbenjsc
3396173362Sbenjsc		if (IEEE80211_IS_CHAN_5GHZ(c)) {
3397173362Sbenjsc			txpower.rates[i].gain_radio = wpi_rf_gain_5ghz[idx];
3398173362Sbenjsc			txpower.rates[i].gain_dsp = wpi_dsp_gain_5ghz[idx];
3399173362Sbenjsc		} else {
3400173362Sbenjsc			txpower.rates[i].gain_radio = wpi_rf_gain_2ghz[idx];
3401173362Sbenjsc			txpower.rates[i].gain_dsp = wpi_dsp_gain_2ghz[idx];
3402173362Sbenjsc		}
3403173362Sbenjsc		DPRINTFN(WPI_DEBUG_TEMP,("chan %d/rate %d: power index %d\n",
3404173362Sbenjsc			    chan, wpi_ridx_to_rate[i], idx));
3405173362Sbenjsc	}
3406173362Sbenjsc
3407173362Sbenjsc	return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async);
3408173362Sbenjsc}
3409173362Sbenjsc
3410173362Sbenjsc/*
3411173362Sbenjsc * Determine Tx power index for a given channel/rate combination.
3412173362Sbenjsc * This takes into account the regulatory information from EEPROM and the
3413173362Sbenjsc * current temperature.
3414173362Sbenjsc */
3415173362Sbenjscstatic int
3416173362Sbenjscwpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
3417173362Sbenjsc    struct ieee80211_channel *c, int rate)
3418173362Sbenjsc{
3419173362Sbenjsc/* fixed-point arithmetic division using a n-bit fractional part */
3420173362Sbenjsc#define fdivround(a, b, n)      \
3421173362Sbenjsc	((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3422173362Sbenjsc
3423173362Sbenjsc/* linear interpolation */
3424173362Sbenjsc#define interpolate(x, x1, y1, x2, y2, n)       \
3425173362Sbenjsc	((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3426173362Sbenjsc
3427178354Ssam	struct ifnet *ifp = sc->sc_ifp;
3428178354Ssam	struct ieee80211com *ic = ifp->if_l2com;
3429173362Sbenjsc	struct wpi_power_sample *sample;
3430173362Sbenjsc	int pwr, idx;
3431173362Sbenjsc	u_int chan;
3432173362Sbenjsc
3433173362Sbenjsc	/* get channel number */
3434173362Sbenjsc	chan = ieee80211_chan2ieee(ic, c);
3435173362Sbenjsc
3436173362Sbenjsc	/* default power is group's maximum power - 3dB */
3437173362Sbenjsc	pwr = group->maxpwr / 2;
3438173362Sbenjsc
3439173362Sbenjsc	/* decrease power for highest OFDM rates to reduce distortion */
3440173362Sbenjsc	switch (rate) {
3441173362Sbenjsc		case 72:	/* 36Mb/s */
3442173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 :  5;
3443173362Sbenjsc			break;
3444173362Sbenjsc		case 96:	/* 48Mb/s */
3445173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10;
3446173362Sbenjsc			break;
3447173362Sbenjsc		case 108:	/* 54Mb/s */
3448173362Sbenjsc			pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12;
3449173362Sbenjsc			break;
3450173362Sbenjsc	}
3451173362Sbenjsc
3452173362Sbenjsc	/* never exceed channel's maximum allowed Tx power */
3453173362Sbenjsc	pwr = min(pwr, sc->maxpwr[chan]);
3454173362Sbenjsc
3455173362Sbenjsc	/* retrieve power index into gain tables from samples */
3456173362Sbenjsc	for (sample = group->samples; sample < &group->samples[3]; sample++)
3457173362Sbenjsc		if (pwr > sample[1].power)
3458173362Sbenjsc			break;
3459173362Sbenjsc	/* fixed-point linear interpolation using a 19-bit fractional part */
3460173362Sbenjsc	idx = interpolate(pwr, sample[0].power, sample[0].index,
3461173362Sbenjsc	    sample[1].power, sample[1].index, 19);
3462173362Sbenjsc
3463173362Sbenjsc	/*
3464173362Sbenjsc	 *  Adjust power index based on current temperature
3465173362Sbenjsc	 *	- if colder than factory-calibrated: decreate output power
3466173362Sbenjsc	 *	- if warmer than factory-calibrated: increase output power
3467173362Sbenjsc	 */
3468173362Sbenjsc	idx -= (sc->temp - group->temp) * 11 / 100;
3469173362Sbenjsc
3470173362Sbenjsc	/* decrease power for CCK rates (-5dB) */
3471173362Sbenjsc	if (!WPI_RATE_IS_OFDM(rate))
3472173362Sbenjsc		idx += 10;
3473173362Sbenjsc
3474173362Sbenjsc	/* keep power index in a valid range */
3475173362Sbenjsc	if (idx < 0)
3476173362Sbenjsc		return 0;
3477173362Sbenjsc	if (idx > WPI_MAX_PWR_INDEX)
3478173362Sbenjsc		return WPI_MAX_PWR_INDEX;
3479173362Sbenjsc	return idx;
3480173362Sbenjsc
3481173362Sbenjsc#undef interpolate
3482173362Sbenjsc#undef fdivround
3483173362Sbenjsc}
3484173362Sbenjsc
3485173362Sbenjsc/**
3486173362Sbenjsc * Called by net80211 framework to indicate that a scan
3487173362Sbenjsc * is starting. This function doesn't actually do the scan,
3488173362Sbenjsc * wpi_scan_curchan starts things off. This function is more
3489173362Sbenjsc * of an early warning from the framework we should get ready
3490173362Sbenjsc * for the scan.
3491173362Sbenjsc */
3492173362Sbenjscstatic void
3493173362Sbenjscwpi_scan_start(struct ieee80211com *ic)
3494173362Sbenjsc{
3495173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
3496173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3497173362Sbenjsc
3498191746Sthompsa	WPI_LOCK(sc);
3499191746Sthompsa	wpi_set_led(sc, WPI_LED_LINK, 20, 2);
3500191746Sthompsa	WPI_UNLOCK(sc);
3501173362Sbenjsc}
3502173362Sbenjsc
3503173362Sbenjsc/**
3504173362Sbenjsc * Called by the net80211 framework, indicates that the
3505173362Sbenjsc * scan has ended. If there is a scan in progress on the card
3506173362Sbenjsc * then it should be aborted.
3507173362Sbenjsc */
3508173362Sbenjscstatic void
3509173362Sbenjscwpi_scan_end(struct ieee80211com *ic)
3510173362Sbenjsc{
3511191746Sthompsa	/* XXX ignore */
3512173362Sbenjsc}
3513173362Sbenjsc
3514173362Sbenjsc/**
3515173362Sbenjsc * Called by the net80211 framework to indicate to the driver
3516173362Sbenjsc * that the channel should be changed
3517173362Sbenjsc */
3518173362Sbenjscstatic void
3519173362Sbenjscwpi_set_channel(struct ieee80211com *ic)
3520173362Sbenjsc{
3521173362Sbenjsc	struct ifnet *ifp = ic->ic_ifp;
3522173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3523191746Sthompsa	int error;
3524173362Sbenjsc
3525177043Sthompsa	/*
3526177043Sthompsa	 * Only need to set the channel in Monitor mode. AP scanning and auth
3527177043Sthompsa	 * are already taken care of by their respective firmware commands.
3528177043Sthompsa	 */
3529191746Sthompsa	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
3530191746Sthompsa		error = wpi_config(sc);
3531191746Sthompsa		if (error != 0)
3532191746Sthompsa			device_printf(sc->sc_dev,
3533191746Sthompsa			    "error %d settting channel\n", error);
3534191746Sthompsa	}
3535173362Sbenjsc}
3536173362Sbenjsc
3537173362Sbenjsc/**
3538173362Sbenjsc * Called by net80211 to indicate that we need to scan the current
3539173362Sbenjsc * channel. The channel is previously be set via the wpi_set_channel
3540173362Sbenjsc * callback.
3541173362Sbenjsc */
3542173362Sbenjscstatic void
3543178354Ssamwpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
3544173362Sbenjsc{
3545178354Ssam	struct ieee80211vap *vap = ss->ss_vap;
3546178354Ssam	struct ifnet *ifp = vap->iv_ic->ic_ifp;
3547173362Sbenjsc	struct wpi_softc *sc = ifp->if_softc;
3548173362Sbenjsc
3549191746Sthompsa	WPI_LOCK(sc);
3550191746Sthompsa	if (wpi_scan(sc))
3551191746Sthompsa		ieee80211_cancel_scan(vap);
3552191746Sthompsa	WPI_UNLOCK(sc);
3553173362Sbenjsc}
3554173362Sbenjsc
3555173362Sbenjsc/**
3556173362Sbenjsc * Called by the net80211 framework to indicate
3557173362Sbenjsc * the minimum dwell time has been met, terminate the scan.
3558173362Sbenjsc * We don't actually terminate the scan as the firmware will notify
3559173362Sbenjsc * us when it's finished and we have no way to interrupt it.
3560173362Sbenjsc */
3561173362Sbenjscstatic void
3562178354Ssamwpi_scan_mindwell(struct ieee80211_scan_state *ss)
3563173362Sbenjsc{
3564173362Sbenjsc	/* NB: don't try to abort scan; wait for firmware to finish */
3565173362Sbenjsc}
3566173362Sbenjsc
3567173362Sbenjscstatic void
3568191746Sthompsawpi_hwreset(void *arg, int pending)
3569173362Sbenjsc{
3570191746Sthompsa	struct wpi_softc *sc = arg;
3571173362Sbenjsc
3572173362Sbenjsc	WPI_LOCK(sc);
3573191746Sthompsa	wpi_init_locked(sc, 0);
3574173362Sbenjsc	WPI_UNLOCK(sc);
3575173362Sbenjsc}
3576173362Sbenjsc
3577191746Sthompsastatic void
3578191746Sthompsawpi_rfreset(void *arg, int pending)
3579173362Sbenjsc{
3580191746Sthompsa	struct wpi_softc *sc = arg;
3581173362Sbenjsc
3582191746Sthompsa	WPI_LOCK(sc);
3583191746Sthompsa	wpi_rfkill_resume(sc);
3584191746Sthompsa	WPI_UNLOCK(sc);
3585173362Sbenjsc}
3586173362Sbenjsc
3587173362Sbenjsc/*
3588173362Sbenjsc * Allocate DMA-safe memory for firmware transfer.
3589173362Sbenjsc */
3590173362Sbenjscstatic int
3591173362Sbenjscwpi_alloc_fwmem(struct wpi_softc *sc)
3592173362Sbenjsc{
3593173362Sbenjsc	/* allocate enough contiguous space to store text and data */
3594173362Sbenjsc	return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL,
3595173362Sbenjsc	    WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 1,
3596173362Sbenjsc	    BUS_DMA_NOWAIT);
3597173362Sbenjsc}
3598173362Sbenjsc
3599173362Sbenjscstatic void
3600173362Sbenjscwpi_free_fwmem(struct wpi_softc *sc)
3601173362Sbenjsc{
3602173362Sbenjsc	wpi_dma_contig_free(&sc->fw_dma);
3603173362Sbenjsc}
3604173362Sbenjsc
3605173362Sbenjsc/**
3606177043Sthompsa * Called every second, wpi_watchdog used by the watch dog timer
3607173362Sbenjsc * to check that the card is still alive
3608173362Sbenjsc */
3609173362Sbenjscstatic void
3610177043Sthompsawpi_watchdog(void *arg)
3611173362Sbenjsc{
3612173362Sbenjsc	struct wpi_softc *sc = arg;
3613177043Sthompsa	struct ifnet *ifp = sc->sc_ifp;
3614191746Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
3615177043Sthompsa	uint32_t tmp;
3616173362Sbenjsc
3617173362Sbenjsc	DPRINTFN(WPI_DEBUG_WATCHDOG,("Watchdog: tick\n"));
3618173362Sbenjsc
3619177043Sthompsa	if (sc->flags & WPI_FLAG_HW_RADIO_OFF) {
3620177043Sthompsa		/* No need to lock firmware memory */
3621177043Sthompsa		tmp = wpi_mem_read(sc, WPI_MEM_HW_RADIO_OFF);
3622177043Sthompsa
3623177043Sthompsa		if ((tmp & 0x1) == 0) {
3624177043Sthompsa			/* Radio kill switch is still off */
3625177043Sthompsa			callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3626177043Sthompsa			return;
3627177043Sthompsa		}
3628177043Sthompsa
3629177043Sthompsa		device_printf(sc->sc_dev, "Hardware Switch Enabled\n");
3630191746Sthompsa		ieee80211_runtask(ic, &sc->sc_radiotask);
3631177043Sthompsa		return;
3632177043Sthompsa	}
3633177043Sthompsa
3634177043Sthompsa	if (sc->sc_tx_timer > 0) {
3635177043Sthompsa		if (--sc->sc_tx_timer == 0) {
3636177043Sthompsa			device_printf(sc->sc_dev,"device timeout\n");
3637177043Sthompsa			ifp->if_oerrors++;
3638191746Sthompsa			ieee80211_runtask(ic, &sc->sc_restarttask);
3639177043Sthompsa		}
3640177043Sthompsa	}
3641177043Sthompsa	if (sc->sc_scan_timer > 0) {
3642178354Ssam		struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3643178354Ssam		if (--sc->sc_scan_timer == 0 && vap != NULL) {
3644177043Sthompsa			device_printf(sc->sc_dev,"scan timeout\n");
3645178354Ssam			ieee80211_cancel_scan(vap);
3646191746Sthompsa			ieee80211_runtask(ic, &sc->sc_restarttask);
3647177043Sthompsa		}
3648177043Sthompsa	}
3649177043Sthompsa
3650177043Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3651177043Sthompsa		callout_reset(&sc->watchdog_to, hz, wpi_watchdog, sc);
3652173362Sbenjsc}
3653173362Sbenjsc
3654173362Sbenjsc#ifdef WPI_DEBUG
3655173362Sbenjscstatic const char *wpi_cmd_str(int cmd)
3656173362Sbenjsc{
3657178354Ssam	switch (cmd) {
3658178354Ssam	case WPI_DISABLE_CMD:	return "WPI_DISABLE_CMD";
3659178354Ssam	case WPI_CMD_CONFIGURE:	return "WPI_CMD_CONFIGURE";
3660178354Ssam	case WPI_CMD_ASSOCIATE:	return "WPI_CMD_ASSOCIATE";
3661178354Ssam	case WPI_CMD_SET_WME:	return "WPI_CMD_SET_WME";
3662178354Ssam	case WPI_CMD_TSF:	return "WPI_CMD_TSF";
3663178354Ssam	case WPI_CMD_ADD_NODE:	return "WPI_CMD_ADD_NODE";
3664178354Ssam	case WPI_CMD_TX_DATA:	return "WPI_CMD_TX_DATA";
3665178354Ssam	case WPI_CMD_MRR_SETUP:	return "WPI_CMD_MRR_SETUP";
3666178354Ssam	case WPI_CMD_SET_LED:	return "WPI_CMD_SET_LED";
3667178354Ssam	case WPI_CMD_SET_POWER_MODE: return "WPI_CMD_SET_POWER_MODE";
3668178354Ssam	case WPI_CMD_SCAN:	return "WPI_CMD_SCAN";
3669178354Ssam	case WPI_CMD_SET_BEACON:return "WPI_CMD_SET_BEACON";
3670178354Ssam	case WPI_CMD_TXPOWER:	return "WPI_CMD_TXPOWER";
3671178354Ssam	case WPI_CMD_BLUETOOTH:	return "WPI_CMD_BLUETOOTH";
3672173362Sbenjsc
3673178354Ssam	default:
3674173362Sbenjsc		KASSERT(1, ("Unknown Command: %d\n", cmd));
3675178354Ssam		return "UNKNOWN CMD";	/* Make the compiler happy */
3676173362Sbenjsc	}
3677173362Sbenjsc}
3678173362Sbenjsc#endif
3679173362Sbenjsc
3680173362SbenjscMODULE_DEPEND(wpi, pci,  1, 1, 1);
3681173362SbenjscMODULE_DEPEND(wpi, wlan, 1, 1, 1);
3682173362SbenjscMODULE_DEPEND(wpi, firmware, 1, 1, 1);
3683