xref: /freebsd/sys/dev/iwi/if_iwi.c (revision 7067450010931479f8dd97e51e4c5bf6a4d34c7e)
1ceaec73dSDamien Bergamini /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4c10b1400SMax Laier  * Copyright (c) 2004, 2005
5ceaec73dSDamien Bergamini  *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6c10b1400SMax Laier  * Copyright (c) 2005-2006 Sam Leffler, Errno Consulting
78d399898SAndrew Thompson  * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
8ceaec73dSDamien Bergamini  *
9ceaec73dSDamien Bergamini  * Redistribution and use in source and binary forms, with or without
10ceaec73dSDamien Bergamini  * modification, are permitted provided that the following conditions
11ceaec73dSDamien Bergamini  * are met:
12ceaec73dSDamien Bergamini  * 1. Redistributions of source code must retain the above copyright
13ceaec73dSDamien Bergamini  *    notice unmodified, this list of conditions, and the following
14ceaec73dSDamien Bergamini  *    disclaimer.
15ceaec73dSDamien Bergamini  * 2. Redistributions in binary form must reproduce the above copyright
16ceaec73dSDamien Bergamini  *    notice, this list of conditions and the following disclaimer in the
17ceaec73dSDamien Bergamini  *    documentation and/or other materials provided with the distribution.
18ceaec73dSDamien Bergamini  *
19ceaec73dSDamien Bergamini  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20ceaec73dSDamien Bergamini  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ceaec73dSDamien Bergamini  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ceaec73dSDamien Bergamini  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23ceaec73dSDamien Bergamini  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ceaec73dSDamien Bergamini  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ceaec73dSDamien Bergamini  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ceaec73dSDamien Bergamini  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ceaec73dSDamien Bergamini  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ceaec73dSDamien Bergamini  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ceaec73dSDamien Bergamini  * SUCH DAMAGE.
30ceaec73dSDamien Bergamini  */
31ceaec73dSDamien Bergamini 
32ceaec73dSDamien Bergamini #include <sys/cdefs.h>
33ceaec73dSDamien Bergamini /*-
34ceaec73dSDamien Bergamini  * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
35ceaec73dSDamien Bergamini  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
36ceaec73dSDamien Bergamini  */
37ceaec73dSDamien Bergamini 
38ceaec73dSDamien Bergamini #include <sys/param.h>
39ceaec73dSDamien Bergamini #include <sys/sysctl.h>
40ceaec73dSDamien Bergamini #include <sys/sockio.h>
41ceaec73dSDamien Bergamini #include <sys/mbuf.h>
42ceaec73dSDamien Bergamini #include <sys/kernel.h>
43ceaec73dSDamien Bergamini #include <sys/socket.h>
44ceaec73dSDamien Bergamini #include <sys/systm.h>
45ceaec73dSDamien Bergamini #include <sys/malloc.h>
46f910c56cSKevin Lo #include <sys/lock.h>
47f910c56cSKevin Lo #include <sys/mutex.h>
48ceaec73dSDamien Bergamini #include <sys/module.h>
49ceaec73dSDamien Bergamini #include <sys/bus.h>
50ceaec73dSDamien Bergamini #include <sys/endian.h>
51c10b1400SMax Laier #include <sys/proc.h>
52c10b1400SMax Laier #include <sys/mount.h>
53c10b1400SMax Laier #include <sys/namei.h>
54bef9529dSDamien Bergamini #include <sys/linker.h>
55bef9529dSDamien Bergamini #include <sys/firmware.h>
56c10b1400SMax Laier #include <sys/taskqueue.h>
57ceaec73dSDamien Bergamini 
58ceaec73dSDamien Bergamini #include <machine/bus.h>
59ceaec73dSDamien Bergamini #include <machine/resource.h>
60ceaec73dSDamien Bergamini #include <sys/rman.h>
61ceaec73dSDamien Bergamini 
62ceaec73dSDamien Bergamini #include <dev/pci/pcireg.h>
63ceaec73dSDamien Bergamini #include <dev/pci/pcivar.h>
64ceaec73dSDamien Bergamini 
65ceaec73dSDamien Bergamini #include <net/bpf.h>
66ceaec73dSDamien Bergamini #include <net/if.h>
6776039bc8SGleb Smirnoff #include <net/if_var.h>
68ceaec73dSDamien Bergamini #include <net/if_arp.h>
69ceaec73dSDamien Bergamini #include <net/ethernet.h>
70ceaec73dSDamien Bergamini #include <net/if_dl.h>
71ceaec73dSDamien Bergamini #include <net/if_media.h>
72ceaec73dSDamien Bergamini #include <net/if_types.h>
73ceaec73dSDamien Bergamini 
74ceaec73dSDamien Bergamini #include <net80211/ieee80211_var.h>
75ceaec73dSDamien Bergamini #include <net80211/ieee80211_radiotap.h>
76b032f27cSSam Leffler #include <net80211/ieee80211_input.h>
7768e8e04eSSam Leffler #include <net80211/ieee80211_regdomain.h>
78ceaec73dSDamien Bergamini 
79ceaec73dSDamien Bergamini #include <netinet/in.h>
80ceaec73dSDamien Bergamini #include <netinet/in_systm.h>
81ceaec73dSDamien Bergamini #include <netinet/in_var.h>
82ceaec73dSDamien Bergamini #include <netinet/ip.h>
83ceaec73dSDamien Bergamini #include <netinet/if_ether.h>
84ceaec73dSDamien Bergamini 
8584f55b29SDamien Bergamini #include <dev/iwi/if_iwireg.h>
86c10b1400SMax Laier #include <dev/iwi/if_iwivar.h>
874982d693SAdrian Chadd #include <dev/iwi/if_iwi_ioctl.h>
88ceaec73dSDamien Bergamini 
89c10b1400SMax Laier #define IWI_DEBUG
90ceaec73dSDamien Bergamini #ifdef IWI_DEBUG
91ceaec73dSDamien Bergamini #define DPRINTF(x)	do { if (iwi_debug > 0) printf x; } while (0)
92ceaec73dSDamien Bergamini #define DPRINTFN(n, x)	do { if (iwi_debug >= (n)) printf x; } while (0)
93ceaec73dSDamien Bergamini int iwi_debug = 0;
94ceaec73dSDamien Bergamini SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level");
95b032f27cSSam Leffler 
96b032f27cSSam Leffler static const char *iwi_fw_states[] = {
97b032f27cSSam Leffler 	"IDLE", 		/* IWI_FW_IDLE */
98b032f27cSSam Leffler 	"LOADING",		/* IWI_FW_LOADING */
99b032f27cSSam Leffler 	"ASSOCIATING",		/* IWI_FW_ASSOCIATING */
100b032f27cSSam Leffler 	"DISASSOCIATING",	/* IWI_FW_DISASSOCIATING */
101b032f27cSSam Leffler 	"SCANNING",		/* IWI_FW_SCANNING */
102b032f27cSSam Leffler };
103ceaec73dSDamien Bergamini #else
104ceaec73dSDamien Bergamini #define DPRINTF(x)
105ceaec73dSDamien Bergamini #define DPRINTFN(n, x)
106ceaec73dSDamien Bergamini #endif
107ceaec73dSDamien Bergamini 
108ceaec73dSDamien Bergamini MODULE_DEPEND(iwi, pci,  1, 1, 1);
109ceaec73dSDamien Bergamini MODULE_DEPEND(iwi, wlan, 1, 1, 1);
1104f5d0153SDamien Bergamini MODULE_DEPEND(iwi, firmware, 1, 1, 1);
111ceaec73dSDamien Bergamini 
112c10b1400SMax Laier enum {
113c10b1400SMax Laier 	IWI_LED_TX,
114c10b1400SMax Laier 	IWI_LED_RX,
115c10b1400SMax Laier 	IWI_LED_POLL,
116c10b1400SMax Laier };
117c10b1400SMax Laier 
118ceaec73dSDamien Bergamini struct iwi_ident {
119ceaec73dSDamien Bergamini 	uint16_t	vendor;
120ceaec73dSDamien Bergamini 	uint16_t	device;
121ceaec73dSDamien Bergamini 	const char	*name;
122ceaec73dSDamien Bergamini };
123ceaec73dSDamien Bergamini 
124ceaec73dSDamien Bergamini static const struct iwi_ident iwi_ident_table[] = {
125ceaec73dSDamien Bergamini 	{ 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" },
126ceaec73dSDamien Bergamini 	{ 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" },
127ceaec73dSDamien Bergamini 	{ 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" },
128ceaec73dSDamien Bergamini 	{ 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" },
129ceaec73dSDamien Bergamini 
130ceaec73dSDamien Bergamini 	{ 0, 0, NULL }
131ceaec73dSDamien Bergamini };
132ceaec73dSDamien Bergamini 
1335dbc2ac9SAndriy Voskoboinyk static const uint8_t def_chan_5ghz_band1[] =
1345dbc2ac9SAndriy Voskoboinyk 	{ 36, 40, 44, 48, 52, 56, 60, 64 };
1355dbc2ac9SAndriy Voskoboinyk static const uint8_t def_chan_5ghz_band2[] =
1365dbc2ac9SAndriy Voskoboinyk 	{ 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140 };
1375dbc2ac9SAndriy Voskoboinyk static const uint8_t def_chan_5ghz_band3[] =
1385dbc2ac9SAndriy Voskoboinyk 	{ 149, 153, 157, 161, 165 };
1395dbc2ac9SAndriy Voskoboinyk 
140b032f27cSSam Leffler static struct ieee80211vap *iwi_vap_create(struct ieee80211com *,
141fcd9500fSBernhard Schmidt 		    const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
142fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN],
143fcd9500fSBernhard Schmidt 		    const uint8_t [IEEE80211_ADDR_LEN]);
144b032f27cSSam Leffler static void	iwi_vap_delete(struct ieee80211vap *);
145ceaec73dSDamien Bergamini static void	iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
146ceaec73dSDamien Bergamini static int	iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
147ceaec73dSDamien Bergamini 		    int);
148ceaec73dSDamien Bergamini static void	iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
149ceaec73dSDamien Bergamini static void	iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
150ceaec73dSDamien Bergamini static int	iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
151ec01dc2bSDamien Bergamini 		    int, bus_addr_t, bus_addr_t);
152ceaec73dSDamien Bergamini static void	iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
153ceaec73dSDamien Bergamini static void	iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
154ceaec73dSDamien Bergamini static int	iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
155ceaec73dSDamien Bergamini 		    int);
156ceaec73dSDamien Bergamini static void	iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
157ceaec73dSDamien Bergamini static void	iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
15838c208f8SSam Leffler static struct ieee80211_node *iwi_node_alloc(struct ieee80211vap *,
15938c208f8SSam Leffler 		    const uint8_t [IEEE80211_ADDR_LEN]);
16028a38073SDamien Bergamini static void	iwi_node_free(struct ieee80211_node *);
16176614fdcSJustin Hibbits static void	iwi_media_status(if_t, struct ifmediareq *);
162b032f27cSSam Leffler static int	iwi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
163c10b1400SMax Laier static void	iwi_wme_init(struct iwi_softc *);
1646468c413SGleb Smirnoff static int	iwi_wme_setparams(struct iwi_softc *);
165ec01dc2bSDamien Bergamini static int	iwi_wme_update(struct ieee80211com *);
166ceaec73dSDamien Bergamini static uint16_t	iwi_read_prom_word(struct iwi_softc *, uint8_t);
167ceaec73dSDamien Bergamini static void	iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
168ceaec73dSDamien Bergamini 		    struct iwi_frame *);
169ceaec73dSDamien Bergamini static void	iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
170ceaec73dSDamien Bergamini static void	iwi_rx_intr(struct iwi_softc *);
171ec01dc2bSDamien Bergamini static void	iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
172ceaec73dSDamien Bergamini static void	iwi_intr(void *);
173c10b1400SMax Laier static int	iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t);
174f322a6ceSJohn Baldwin static void	iwi_write_ibssnode(struct iwi_softc *, const u_int8_t [IEEE80211_ADDR_LEN], int);
1757a79cebfSGleb Smirnoff static int	iwi_tx_start(struct iwi_softc *, struct mbuf *,
1760b74a285SDamien Bergamini 		    struct ieee80211_node *, int);
177b032f27cSSam Leffler static int	iwi_raw_xmit(struct ieee80211_node *, struct mbuf *,
178b032f27cSSam Leffler 		    const struct ieee80211_bpf_params *);
1797a79cebfSGleb Smirnoff static void	iwi_start(struct iwi_softc *);
1807a79cebfSGleb Smirnoff static int	iwi_transmit(struct ieee80211com *, struct mbuf *);
18168e8e04eSSam Leffler static void	iwi_watchdog(void *);
1827a79cebfSGleb Smirnoff static int	iwi_ioctl(struct ieee80211com *, u_long, void *);
1837a79cebfSGleb Smirnoff static void	iwi_parent(struct ieee80211com *);
184ceaec73dSDamien Bergamini static void	iwi_stop_master(struct iwi_softc *);
185ceaec73dSDamien Bergamini static int	iwi_reset(struct iwi_softc *);
186c10b1400SMax Laier static int	iwi_load_ucode(struct iwi_softc *, const struct iwi_fw *);
187c10b1400SMax Laier static int	iwi_load_firmware(struct iwi_softc *, const struct iwi_fw *);
188484f6530SLuigi Rizzo static void	iwi_release_fw_dma(struct iwi_softc *sc);
189ceaec73dSDamien Bergamini static int	iwi_config(struct iwi_softc *);
190b032f27cSSam Leffler static int	iwi_get_firmware(struct iwi_softc *, enum ieee80211_opmode);
191c10b1400SMax Laier static void	iwi_put_firmware(struct iwi_softc *);
19240668dc5SBernhard Schmidt static void	iwi_monitor_scan(void *, int);
19368e8e04eSSam Leffler static int	iwi_scanchan(struct iwi_softc *, unsigned long, int);
19468e8e04eSSam Leffler static void	iwi_scan_start(struct ieee80211com *);
19568e8e04eSSam Leffler static void	iwi_scan_end(struct ieee80211com *);
19668e8e04eSSam Leffler static void	iwi_set_channel(struct ieee80211com *);
197b032f27cSSam Leffler static void	iwi_scan_curchan(struct ieee80211_scan_state *, unsigned long maxdwell);
198b032f27cSSam Leffler static void	iwi_scan_mindwell(struct ieee80211_scan_state *);
199b032f27cSSam Leffler static int	iwi_auth_and_assoc(struct iwi_softc *, struct ieee80211vap *);
2005efea30fSAndrew Thompson static void	iwi_disassoc(void *, int);
201c10b1400SMax Laier static int	iwi_disassociate(struct iwi_softc *, int quiet);
202b032f27cSSam Leffler static void	iwi_init_locked(struct iwi_softc *);
203ceaec73dSDamien Bergamini static void	iwi_init(void *);
204b032f27cSSam Leffler static int	iwi_init_fw_dma(struct iwi_softc *, int);
205b032f27cSSam Leffler static void	iwi_stop_locked(void *);
206b032f27cSSam Leffler static void	iwi_stop(struct iwi_softc *);
207c10b1400SMax Laier static void	iwi_restart(void *, int);
208c10b1400SMax Laier static int	iwi_getrfkill(struct iwi_softc *);
209c10b1400SMax Laier static void	iwi_radio_on(void *, int);
210c10b1400SMax Laier static void	iwi_radio_off(void *, int);
211c10b1400SMax Laier static void	iwi_sysctlattach(struct iwi_softc *);
212c10b1400SMax Laier static void	iwi_led_event(struct iwi_softc *, int);
213c10b1400SMax Laier static void	iwi_ledattach(struct iwi_softc *);
2145dbc2ac9SAndriy Voskoboinyk static void	iwi_collect_bands(struct ieee80211com *, uint8_t [], size_t);
2155dbc2ac9SAndriy Voskoboinyk static void	iwi_getradiocaps(struct ieee80211com *, int, int *,
2165dbc2ac9SAndriy Voskoboinyk 		    struct ieee80211_channel []);
217ceaec73dSDamien Bergamini 
218ceaec73dSDamien Bergamini static int iwi_probe(device_t);
219ceaec73dSDamien Bergamini static int iwi_attach(device_t);
220ceaec73dSDamien Bergamini static int iwi_detach(device_t);
221ceaec73dSDamien Bergamini static int iwi_shutdown(device_t);
222ceaec73dSDamien Bergamini static int iwi_suspend(device_t);
223ceaec73dSDamien Bergamini static int iwi_resume(device_t);
224ceaec73dSDamien Bergamini 
225ceaec73dSDamien Bergamini static device_method_t iwi_methods[] = {
226ceaec73dSDamien Bergamini 	/* Device interface */
227ceaec73dSDamien Bergamini 	DEVMETHOD(device_probe,		iwi_probe),
228ceaec73dSDamien Bergamini 	DEVMETHOD(device_attach,	iwi_attach),
229ceaec73dSDamien Bergamini 	DEVMETHOD(device_detach,	iwi_detach),
230ceaec73dSDamien Bergamini 	DEVMETHOD(device_shutdown,	iwi_shutdown),
231ceaec73dSDamien Bergamini 	DEVMETHOD(device_suspend,	iwi_suspend),
232ceaec73dSDamien Bergamini 	DEVMETHOD(device_resume,	iwi_resume),
233ceaec73dSDamien Bergamini 
2340a9d8839SMarius Strobl 	DEVMETHOD_END
235ceaec73dSDamien Bergamini };
236ceaec73dSDamien Bergamini 
237ceaec73dSDamien Bergamini static driver_t iwi_driver = {
238ceaec73dSDamien Bergamini 	"iwi",
239ceaec73dSDamien Bergamini 	iwi_methods,
240ceaec73dSDamien Bergamini 	sizeof (struct iwi_softc)
241ceaec73dSDamien Bergamini };
242ceaec73dSDamien Bergamini 
243a571fed7SJohn Baldwin DRIVER_MODULE(iwi, pci, iwi_driver, NULL, NULL);
244ceaec73dSDamien Bergamini 
245129145a4SBernhard Schmidt MODULE_VERSION(iwi, 1);
246129145a4SBernhard Schmidt 
247c10b1400SMax Laier static __inline uint8_t
MEM_READ_1(struct iwi_softc * sc,uint32_t addr)248c10b1400SMax Laier MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
249c10b1400SMax Laier {
250c10b1400SMax Laier 	CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
251c10b1400SMax Laier 	return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
252c10b1400SMax Laier }
253c10b1400SMax Laier 
254c10b1400SMax Laier static __inline uint32_t
MEM_READ_4(struct iwi_softc * sc,uint32_t addr)255c10b1400SMax Laier MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
256c10b1400SMax Laier {
257c10b1400SMax Laier 	CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
258c10b1400SMax Laier 	return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
259c10b1400SMax Laier }
260c10b1400SMax Laier 
261ceaec73dSDamien Bergamini static int
iwi_probe(device_t dev)262ceaec73dSDamien Bergamini iwi_probe(device_t dev)
263ceaec73dSDamien Bergamini {
264ceaec73dSDamien Bergamini 	const struct iwi_ident *ident;
265ceaec73dSDamien Bergamini 
266ceaec73dSDamien Bergamini 	for (ident = iwi_ident_table; ident->name != NULL; ident++) {
267ceaec73dSDamien Bergamini 		if (pci_get_vendor(dev) == ident->vendor &&
268ceaec73dSDamien Bergamini 		    pci_get_device(dev) == ident->device) {
269ceaec73dSDamien Bergamini 			device_set_desc(dev, ident->name);
2700a9d8839SMarius Strobl 			return (BUS_PROBE_DEFAULT);
271ceaec73dSDamien Bergamini 		}
272ceaec73dSDamien Bergamini 	}
273ceaec73dSDamien Bergamini 	return ENXIO;
274ceaec73dSDamien Bergamini }
275ceaec73dSDamien Bergamini 
276ceaec73dSDamien Bergamini static int
iwi_attach(device_t dev)277ceaec73dSDamien Bergamini iwi_attach(device_t dev)
278ceaec73dSDamien Bergamini {
279ceaec73dSDamien Bergamini 	struct iwi_softc *sc = device_get_softc(dev);
2807a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
281ceaec73dSDamien Bergamini 	uint16_t val;
282b032f27cSSam Leffler 	int i, error;
283ceaec73dSDamien Bergamini 
284ceaec73dSDamien Bergamini 	sc->sc_dev = dev;
285a14af830SIan Lepore 	sc->sc_ledevent = ticks;
286ceaec73dSDamien Bergamini 
28768e8e04eSSam Leffler 	IWI_LOCK_INIT(sc);
2887a79cebfSGleb Smirnoff 	mbufq_init(&sc->sc_snd, ifqmaxlen);
289ceaec73dSDamien Bergamini 
290c10b1400SMax Laier 	sc->sc_unr = new_unrhdr(1, IWI_MAX_IBSSNODE-1, &sc->sc_mtx);
29128a38073SDamien Bergamini 
292c10b1400SMax Laier 	TASK_INIT(&sc->sc_radiontask, 0, iwi_radio_on, sc);
293c10b1400SMax Laier 	TASK_INIT(&sc->sc_radiofftask, 0, iwi_radio_off, sc);
294c10b1400SMax Laier 	TASK_INIT(&sc->sc_restarttask, 0, iwi_restart, sc);
2955efea30fSAndrew Thompson 	TASK_INIT(&sc->sc_disassoctask, 0, iwi_disassoc, sc);
29640668dc5SBernhard Schmidt 	TASK_INIT(&sc->sc_monitortask, 0, iwi_monitor_scan, sc);
2975efea30fSAndrew Thompson 
29868e8e04eSSam Leffler 	callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
299b032f27cSSam Leffler 	callout_init_mtx(&sc->sc_rftimer, &sc->sc_mtx, 0);
3001b996b2aSDamien Bergamini 
301acea4241SDamien Bergamini 	pci_write_config(dev, 0x41, 0, 1);
302acea4241SDamien Bergamini 
303ceaec73dSDamien Bergamini 	/* enable bus-mastering */
304ceaec73dSDamien Bergamini 	pci_enable_busmaster(dev);
305ceaec73dSDamien Bergamini 
3060a9d8839SMarius Strobl 	i = PCIR_BAR(0);
3070a9d8839SMarius Strobl 	sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE);
308ceaec73dSDamien Bergamini 	if (sc->mem == NULL) {
309ceaec73dSDamien Bergamini 		device_printf(dev, "could not allocate memory resource\n");
310ceaec73dSDamien Bergamini 		goto fail;
311ceaec73dSDamien Bergamini 	}
312ceaec73dSDamien Bergamini 
313ceaec73dSDamien Bergamini 	sc->sc_st = rman_get_bustag(sc->mem);
314ceaec73dSDamien Bergamini 	sc->sc_sh = rman_get_bushandle(sc->mem);
315ceaec73dSDamien Bergamini 
3160a9d8839SMarius Strobl 	i = 0;
3170a9d8839SMarius Strobl 	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
318ceaec73dSDamien Bergamini 	    RF_ACTIVE | RF_SHAREABLE);
319ceaec73dSDamien Bergamini 	if (sc->irq == NULL) {
320ceaec73dSDamien Bergamini 		device_printf(dev, "could not allocate interrupt resource\n");
321ceaec73dSDamien Bergamini 		goto fail;
322ceaec73dSDamien Bergamini 	}
323ceaec73dSDamien Bergamini 
324ceaec73dSDamien Bergamini 	if (iwi_reset(sc) != 0) {
325ceaec73dSDamien Bergamini 		device_printf(dev, "could not reset adapter\n");
326ceaec73dSDamien Bergamini 		goto fail;
327ceaec73dSDamien Bergamini 	}
328ceaec73dSDamien Bergamini 
329ceaec73dSDamien Bergamini 	/*
330ceaec73dSDamien Bergamini 	 * Allocate rings.
331ceaec73dSDamien Bergamini 	 */
332ceaec73dSDamien Bergamini 	if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
333ceaec73dSDamien Bergamini 		device_printf(dev, "could not allocate Cmd ring\n");
334ceaec73dSDamien Bergamini 		goto fail;
335ceaec73dSDamien Bergamini 	}
336ceaec73dSDamien Bergamini 
337484f6530SLuigi Rizzo 	for (i = 0; i < 4; i++) {
338484f6530SLuigi Rizzo 		error = iwi_alloc_tx_ring(sc, &sc->txq[i], IWI_TX_RING_COUNT,
339484f6530SLuigi Rizzo 		    IWI_CSR_TX1_RIDX + i * 4,
340484f6530SLuigi Rizzo 		    IWI_CSR_TX1_WIDX + i * 4);
341ec01dc2bSDamien Bergamini 		if (error != 0) {
342484f6530SLuigi Rizzo 			device_printf(dev, "could not allocate Tx ring %d\n",
343484f6530SLuigi Rizzo 				i+i);
344ec01dc2bSDamien Bergamini 			goto fail;
345ec01dc2bSDamien Bergamini 		}
346ceaec73dSDamien Bergamini 	}
347ceaec73dSDamien Bergamini 
348ceaec73dSDamien Bergamini 	if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
349ceaec73dSDamien Bergamini 		device_printf(dev, "could not allocate Rx ring\n");
350ceaec73dSDamien Bergamini 		goto fail;
351ceaec73dSDamien Bergamini 	}
352ceaec73dSDamien Bergamini 
353c10b1400SMax Laier 	iwi_wme_init(sc);
354c10b1400SMax Laier 
35559686fe9SGleb Smirnoff 	ic->ic_softc = sc;
356c8550c02SGleb Smirnoff 	ic->ic_name = device_get_nameunit(dev);
357b032f27cSSam Leffler 	ic->ic_opmode = IEEE80211_M_STA;
358ceaec73dSDamien Bergamini 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
359ceaec73dSDamien Bergamini 
360ceaec73dSDamien Bergamini 	/* set device capabilities */
361ec01dc2bSDamien Bergamini 	ic->ic_caps =
362c43feedeSSam Leffler 	      IEEE80211_C_STA		/* station mode supported */
363c43feedeSSam Leffler 	    | IEEE80211_C_IBSS		/* IBSS mode supported */
36468e8e04eSSam Leffler 	    | IEEE80211_C_MONITOR	/* monitor mode supported */
36568e8e04eSSam Leffler 	    | IEEE80211_C_PMGT		/* power save supported */
36668e8e04eSSam Leffler 	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
36768e8e04eSSam Leffler 	    | IEEE80211_C_WPA		/* 802.11i */
36868e8e04eSSam Leffler 	    | IEEE80211_C_WME		/* 802.11e */
369b032f27cSSam Leffler #if 0
37068e8e04eSSam Leffler 	    | IEEE80211_C_BGSCAN	/* capable of bg scanning */
371b032f27cSSam Leffler #endif
37268e8e04eSSam Leffler 	    ;
373ceaec73dSDamien Bergamini 
374ceaec73dSDamien Bergamini 	/* read MAC address from EEPROM */
375ceaec73dSDamien Bergamini 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
3767a79cebfSGleb Smirnoff 	ic->ic_macaddr[0] = val & 0xff;
3777a79cebfSGleb Smirnoff 	ic->ic_macaddr[1] = val >> 8;
378ceaec73dSDamien Bergamini 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
3797a79cebfSGleb Smirnoff 	ic->ic_macaddr[2] = val & 0xff;
3807a79cebfSGleb Smirnoff 	ic->ic_macaddr[3] = val >> 8;
381ceaec73dSDamien Bergamini 	val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
3827a79cebfSGleb Smirnoff 	ic->ic_macaddr[4] = val & 0xff;
3837a79cebfSGleb Smirnoff 	ic->ic_macaddr[5] = val >> 8;
384ceaec73dSDamien Bergamini 
3855dbc2ac9SAndriy Voskoboinyk 	iwi_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
3865dbc2ac9SAndriy Voskoboinyk 	    ic->ic_channels);
387ceaec73dSDamien Bergamini 
3887a79cebfSGleb Smirnoff 	ieee80211_ifattach(ic);
38928a38073SDamien Bergamini 	/* override default methods */
39028a38073SDamien Bergamini 	ic->ic_node_alloc = iwi_node_alloc;
39128a38073SDamien Bergamini 	sc->sc_node_free = ic->ic_node_free;
39228a38073SDamien Bergamini 	ic->ic_node_free = iwi_node_free;
393b032f27cSSam Leffler 	ic->ic_raw_xmit = iwi_raw_xmit;
39468e8e04eSSam Leffler 	ic->ic_scan_start = iwi_scan_start;
39568e8e04eSSam Leffler 	ic->ic_scan_end = iwi_scan_end;
39668e8e04eSSam Leffler 	ic->ic_set_channel = iwi_set_channel;
39768e8e04eSSam Leffler 	ic->ic_scan_curchan = iwi_scan_curchan;
39868e8e04eSSam Leffler 	ic->ic_scan_mindwell = iwi_scan_mindwell;
399b032f27cSSam Leffler 	ic->ic_wme.wme_update = iwi_wme_update;
40068e8e04eSSam Leffler 
401b032f27cSSam Leffler 	ic->ic_vap_create = iwi_vap_create;
402b032f27cSSam Leffler 	ic->ic_vap_delete = iwi_vap_delete;
4037a79cebfSGleb Smirnoff 	ic->ic_ioctl = iwi_ioctl;
4047a79cebfSGleb Smirnoff 	ic->ic_transmit = iwi_transmit;
4057a79cebfSGleb Smirnoff 	ic->ic_parent = iwi_parent;
4065dbc2ac9SAndriy Voskoboinyk 	ic->ic_getradiocaps = iwi_getradiocaps;
407ceaec73dSDamien Bergamini 
4085463c4a4SSam Leffler 	ieee80211_radiotap_attach(ic,
4095463c4a4SSam Leffler 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
4105463c4a4SSam Leffler 		IWI_TX_RADIOTAP_PRESENT,
4115463c4a4SSam Leffler 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
4125463c4a4SSam Leffler 		IWI_RX_RADIOTAP_PRESENT);
413ceaec73dSDamien Bergamini 
414c10b1400SMax Laier 	iwi_sysctlattach(sc);
415c10b1400SMax Laier 	iwi_ledattach(sc);
416acea4241SDamien Bergamini 
417ceaec73dSDamien Bergamini 	/*
418ceaec73dSDamien Bergamini 	 * Hook our interrupt after all initialization is complete.
419ceaec73dSDamien Bergamini 	 */
420ceaec73dSDamien Bergamini 	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
421ef544f63SPaolo Pisati 	    NULL, iwi_intr, sc, &sc->sc_ih);
422ceaec73dSDamien Bergamini 	if (error != 0) {
423ceaec73dSDamien Bergamini 		device_printf(dev, "could not set up interrupt\n");
424ceaec73dSDamien Bergamini 		goto fail;
425ceaec73dSDamien Bergamini 	}
426ceaec73dSDamien Bergamini 
427ceaec73dSDamien Bergamini 	if (bootverbose)
428ceaec73dSDamien Bergamini 		ieee80211_announce(ic);
429ceaec73dSDamien Bergamini 
430ceaec73dSDamien Bergamini 	return 0;
431b032f27cSSam Leffler fail:
432b032f27cSSam Leffler 	/* XXX fix */
433b032f27cSSam Leffler 	iwi_detach(dev);
434ceaec73dSDamien Bergamini 	return ENXIO;
435ceaec73dSDamien Bergamini }
436ceaec73dSDamien Bergamini 
437ceaec73dSDamien Bergamini static int
iwi_detach(device_t dev)438ceaec73dSDamien Bergamini iwi_detach(device_t dev)
439ceaec73dSDamien Bergamini {
440ceaec73dSDamien Bergamini 	struct iwi_softc *sc = device_get_softc(dev);
4417a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
442ceaec73dSDamien Bergamini 
4430a9d8839SMarius Strobl 	bus_teardown_intr(dev, sc->irq, sc->sc_ih);
4440a9d8839SMarius Strobl 
445b032f27cSSam Leffler 	/* NB: do early to drain any pending tasks */
4465efea30fSAndrew Thompson 	ieee80211_draintask(ic, &sc->sc_radiontask);
4475efea30fSAndrew Thompson 	ieee80211_draintask(ic, &sc->sc_radiofftask);
4485efea30fSAndrew Thompson 	ieee80211_draintask(ic, &sc->sc_restarttask);
4495efea30fSAndrew Thompson 	ieee80211_draintask(ic, &sc->sc_disassoctask);
45040668dc5SBernhard Schmidt 	ieee80211_draintask(ic, &sc->sc_monitortask);
451b032f27cSSam Leffler 
452b569d213SAndrew Thompson 	iwi_stop(sc);
453b569d213SAndrew Thompson 
454b569d213SAndrew Thompson 	ieee80211_ifdetach(ic);
455b569d213SAndrew Thompson 
456fa6f748fSJohn Baldwin 	iwi_put_firmware(sc);
457484f6530SLuigi Rizzo 	iwi_release_fw_dma(sc);
458ceaec73dSDamien Bergamini 
459ceaec73dSDamien Bergamini 	iwi_free_cmd_ring(sc, &sc->cmdq);
460ec01dc2bSDamien Bergamini 	iwi_free_tx_ring(sc, &sc->txq[0]);
461ec01dc2bSDamien Bergamini 	iwi_free_tx_ring(sc, &sc->txq[1]);
462ec01dc2bSDamien Bergamini 	iwi_free_tx_ring(sc, &sc->txq[2]);
463ec01dc2bSDamien Bergamini 	iwi_free_tx_ring(sc, &sc->txq[3]);
464ceaec73dSDamien Bergamini 	iwi_free_rx_ring(sc, &sc->rxq);
465ceaec73dSDamien Bergamini 
4660a9d8839SMarius Strobl 	bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
467ceaec73dSDamien Bergamini 
4680a9d8839SMarius Strobl 	bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
4690a9d8839SMarius Strobl 	    sc->mem);
470ceaec73dSDamien Bergamini 
47128a38073SDamien Bergamini 	delete_unrhdr(sc->sc_unr);
4727a79cebfSGleb Smirnoff 	mbufq_drain(&sc->sc_snd);
47328a38073SDamien Bergamini 
47468e8e04eSSam Leffler 	IWI_LOCK_DESTROY(sc);
475ceaec73dSDamien Bergamini 
476ceaec73dSDamien Bergamini 	return 0;
477ceaec73dSDamien Bergamini }
478ceaec73dSDamien Bergamini 
479b032f27cSSam Leffler static struct ieee80211vap *
iwi_vap_create(struct ieee80211com * ic,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t mac[IEEE80211_ADDR_LEN])480fcd9500fSBernhard Schmidt iwi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
481fcd9500fSBernhard Schmidt     enum ieee80211_opmode opmode, int flags,
482b032f27cSSam Leffler     const uint8_t bssid[IEEE80211_ADDR_LEN],
483b032f27cSSam Leffler     const uint8_t mac[IEEE80211_ADDR_LEN])
484b032f27cSSam Leffler {
4857a79cebfSGleb Smirnoff 	struct iwi_softc *sc = ic->ic_softc;
486b032f27cSSam Leffler 	struct iwi_vap *ivp;
487b032f27cSSam Leffler 	struct ieee80211vap *vap;
488b032f27cSSam Leffler 	int i;
489b032f27cSSam Leffler 
490b032f27cSSam Leffler 	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
491b032f27cSSam Leffler 		return NULL;
492b032f27cSSam Leffler 	/*
493b032f27cSSam Leffler 	 * Get firmware image (and possibly dma memory) on mode change.
494b032f27cSSam Leffler 	 */
495b032f27cSSam Leffler 	if (iwi_get_firmware(sc, opmode))
496b032f27cSSam Leffler 		return NULL;
497b032f27cSSam Leffler 	/* allocate DMA memory for mapping firmware image */
498b032f27cSSam Leffler 	i = sc->fw_fw.size;
499b032f27cSSam Leffler 	if (sc->fw_boot.size > i)
500b032f27cSSam Leffler 		i = sc->fw_boot.size;
501b032f27cSSam Leffler 	/* XXX do we dma the ucode as well ? */
502b032f27cSSam Leffler 	if (sc->fw_uc.size > i)
503b032f27cSSam Leffler 		i = sc->fw_uc.size;
504b032f27cSSam Leffler 	if (iwi_init_fw_dma(sc, i))
505b032f27cSSam Leffler 		return NULL;
506b032f27cSSam Leffler 
5077a79cebfSGleb Smirnoff 	ivp = malloc(sizeof(struct iwi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
508b032f27cSSam Leffler 	vap = &ivp->iwi_vap;
5097a79cebfSGleb Smirnoff 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
510b032f27cSSam Leffler 	/* override the default, the setting comes from the linux driver */
511b032f27cSSam Leffler 	vap->iv_bmissthreshold = 24;
512b032f27cSSam Leffler 	/* override with driver methods */
513b032f27cSSam Leffler 	ivp->iwi_newstate = vap->iv_newstate;
514b032f27cSSam Leffler 	vap->iv_newstate = iwi_newstate;
515b032f27cSSam Leffler 
516b032f27cSSam Leffler 	/* complete setup */
5177a79cebfSGleb Smirnoff 	ieee80211_vap_attach(vap, ieee80211_media_change, iwi_media_status,
5187a79cebfSGleb Smirnoff 	    mac);
519b032f27cSSam Leffler 	ic->ic_opmode = opmode;
520b032f27cSSam Leffler 	return vap;
521b032f27cSSam Leffler }
522b032f27cSSam Leffler 
523b032f27cSSam Leffler static void
iwi_vap_delete(struct ieee80211vap * vap)524b032f27cSSam Leffler iwi_vap_delete(struct ieee80211vap *vap)
525b032f27cSSam Leffler {
526b032f27cSSam Leffler 	struct iwi_vap *ivp = IWI_VAP(vap);
527b032f27cSSam Leffler 
528b032f27cSSam Leffler 	ieee80211_vap_detach(vap);
529b032f27cSSam Leffler 	free(ivp, M_80211_VAP);
530b032f27cSSam Leffler }
531b032f27cSSam Leffler 
532ceaec73dSDamien Bergamini static void
iwi_dma_map_addr(void * arg,bus_dma_segment_t * segs,int nseg,int error)533ceaec73dSDamien Bergamini iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
534ceaec73dSDamien Bergamini {
535ceaec73dSDamien Bergamini 	if (error != 0)
536ceaec73dSDamien Bergamini 		return;
537ceaec73dSDamien Bergamini 
538ceaec73dSDamien Bergamini 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
539ceaec73dSDamien Bergamini 
540ceaec73dSDamien Bergamini 	*(bus_addr_t *)arg = segs[0].ds_addr;
541ceaec73dSDamien Bergamini }
542ceaec73dSDamien Bergamini 
543ceaec73dSDamien Bergamini static int
iwi_alloc_cmd_ring(struct iwi_softc * sc,struct iwi_cmd_ring * ring,int count)544ceaec73dSDamien Bergamini iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count)
545ceaec73dSDamien Bergamini {
546ceaec73dSDamien Bergamini 	int error;
547ceaec73dSDamien Bergamini 
548ceaec73dSDamien Bergamini 	ring->count = count;
549ceaec73dSDamien Bergamini 	ring->queued = 0;
550ceaec73dSDamien Bergamini 	ring->cur = ring->next = 0;
551ceaec73dSDamien Bergamini 
552b0ea96dfSKevin Lo 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
553b0ea96dfSKevin Lo 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
554b0ea96dfSKevin Lo 	    count * IWI_CMD_DESC_SIZE, 1, count * IWI_CMD_DESC_SIZE, 0,
555b0ea96dfSKevin Lo 	    NULL, NULL, &ring->desc_dmat);
556ceaec73dSDamien Bergamini 	if (error != 0) {
557ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not create desc DMA tag\n");
558ceaec73dSDamien Bergamini 		goto fail;
559ceaec73dSDamien Bergamini 	}
560ceaec73dSDamien Bergamini 
561ceaec73dSDamien Bergamini 	error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
562ceaec73dSDamien Bergamini 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
563ceaec73dSDamien Bergamini 	if (error != 0) {
564ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not allocate DMA memory\n");
565ceaec73dSDamien Bergamini 		goto fail;
566ceaec73dSDamien Bergamini 	}
567ceaec73dSDamien Bergamini 
568ceaec73dSDamien Bergamini 	error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
569ceaec73dSDamien Bergamini 	    count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
570ceaec73dSDamien Bergamini 	if (error != 0) {
571ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not load desc DMA map\n");
572ceaec73dSDamien Bergamini 		goto fail;
573ceaec73dSDamien Bergamini 	}
574ceaec73dSDamien Bergamini 
575ceaec73dSDamien Bergamini 	return 0;
576ceaec73dSDamien Bergamini 
577ceaec73dSDamien Bergamini fail:	iwi_free_cmd_ring(sc, ring);
578ceaec73dSDamien Bergamini 	return error;
579ceaec73dSDamien Bergamini }
580ceaec73dSDamien Bergamini 
581ceaec73dSDamien Bergamini static void
iwi_reset_cmd_ring(struct iwi_softc * sc,struct iwi_cmd_ring * ring)582ceaec73dSDamien Bergamini iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
583ceaec73dSDamien Bergamini {
584ceaec73dSDamien Bergamini 	ring->queued = 0;
585ceaec73dSDamien Bergamini 	ring->cur = ring->next = 0;
586ceaec73dSDamien Bergamini }
587ceaec73dSDamien Bergamini 
588ceaec73dSDamien Bergamini static void
iwi_free_cmd_ring(struct iwi_softc * sc,struct iwi_cmd_ring * ring)589ceaec73dSDamien Bergamini iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
590ceaec73dSDamien Bergamini {
591ceaec73dSDamien Bergamini 	if (ring->desc != NULL) {
592ceaec73dSDamien Bergamini 		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
593ceaec73dSDamien Bergamini 		    BUS_DMASYNC_POSTWRITE);
594ceaec73dSDamien Bergamini 		bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
595ceaec73dSDamien Bergamini 		bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
596ceaec73dSDamien Bergamini 	}
597ceaec73dSDamien Bergamini 
598ceaec73dSDamien Bergamini 	if (ring->desc_dmat != NULL)
599ceaec73dSDamien Bergamini 		bus_dma_tag_destroy(ring->desc_dmat);
600ceaec73dSDamien Bergamini }
601ceaec73dSDamien Bergamini 
602ceaec73dSDamien Bergamini static int
iwi_alloc_tx_ring(struct iwi_softc * sc,struct iwi_tx_ring * ring,int count,bus_addr_t csr_ridx,bus_addr_t csr_widx)603ec01dc2bSDamien Bergamini iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count,
604ec01dc2bSDamien Bergamini     bus_addr_t csr_ridx, bus_addr_t csr_widx)
605ceaec73dSDamien Bergamini {
606ceaec73dSDamien Bergamini 	int i, error;
607ceaec73dSDamien Bergamini 
608ceaec73dSDamien Bergamini 	ring->count = count;
609ceaec73dSDamien Bergamini 	ring->queued = 0;
610ceaec73dSDamien Bergamini 	ring->cur = ring->next = 0;
611ec01dc2bSDamien Bergamini 	ring->csr_ridx = csr_ridx;
612ec01dc2bSDamien Bergamini 	ring->csr_widx = csr_widx;
613ceaec73dSDamien Bergamini 
614b0ea96dfSKevin Lo 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
615b0ea96dfSKevin Lo 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
616b0ea96dfSKevin Lo 	    count * IWI_TX_DESC_SIZE, 1, count * IWI_TX_DESC_SIZE, 0, NULL,
617b0ea96dfSKevin Lo 	    NULL, &ring->desc_dmat);
618ceaec73dSDamien Bergamini 	if (error != 0) {
619ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not create desc DMA tag\n");
620ceaec73dSDamien Bergamini 		goto fail;
621ceaec73dSDamien Bergamini 	}
622ceaec73dSDamien Bergamini 
623ceaec73dSDamien Bergamini 	error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
624ceaec73dSDamien Bergamini 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
625ceaec73dSDamien Bergamini 	if (error != 0) {
626ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not allocate DMA memory\n");
627ceaec73dSDamien Bergamini 		goto fail;
628ceaec73dSDamien Bergamini 	}
629ceaec73dSDamien Bergamini 
630ceaec73dSDamien Bergamini 	error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
631ceaec73dSDamien Bergamini 	    count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
632ceaec73dSDamien Bergamini 	if (error != 0) {
633ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not load desc DMA map\n");
634ceaec73dSDamien Bergamini 		goto fail;
635ceaec73dSDamien Bergamini 	}
636ceaec73dSDamien Bergamini 
637ac2fffa4SPedro F. Giffuni 	ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
638ceaec73dSDamien Bergamini 	    M_NOWAIT | M_ZERO);
639ceaec73dSDamien Bergamini 	if (ring->data == NULL) {
640ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not allocate soft data\n");
641ceaec73dSDamien Bergamini 		error = ENOMEM;
642ceaec73dSDamien Bergamini 		goto fail;
643ceaec73dSDamien Bergamini 	}
644ceaec73dSDamien Bergamini 
645b0ea96dfSKevin Lo 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
646b0ea96dfSKevin Lo 	BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
647b0ea96dfSKevin Lo 	IWI_MAX_NSEG, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
648ceaec73dSDamien Bergamini 	if (error != 0) {
649ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not create data DMA tag\n");
650ceaec73dSDamien Bergamini 		goto fail;
651ceaec73dSDamien Bergamini 	}
652ceaec73dSDamien Bergamini 
653ceaec73dSDamien Bergamini 	for (i = 0; i < count; i++) {
654ceaec73dSDamien Bergamini 		error = bus_dmamap_create(ring->data_dmat, 0,
655ceaec73dSDamien Bergamini 		    &ring->data[i].map);
656ceaec73dSDamien Bergamini 		if (error != 0) {
657ceaec73dSDamien Bergamini 			device_printf(sc->sc_dev, "could not create DMA map\n");
658ceaec73dSDamien Bergamini 			goto fail;
659ceaec73dSDamien Bergamini 		}
660ceaec73dSDamien Bergamini 	}
661ceaec73dSDamien Bergamini 
662ceaec73dSDamien Bergamini 	return 0;
663ceaec73dSDamien Bergamini 
664ceaec73dSDamien Bergamini fail:	iwi_free_tx_ring(sc, ring);
665ceaec73dSDamien Bergamini 	return error;
666ceaec73dSDamien Bergamini }
667ceaec73dSDamien Bergamini 
668ceaec73dSDamien Bergamini static void
iwi_reset_tx_ring(struct iwi_softc * sc,struct iwi_tx_ring * ring)669ceaec73dSDamien Bergamini iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
670ceaec73dSDamien Bergamini {
671ceaec73dSDamien Bergamini 	struct iwi_tx_data *data;
672ceaec73dSDamien Bergamini 	int i;
673ceaec73dSDamien Bergamini 
674ceaec73dSDamien Bergamini 	for (i = 0; i < ring->count; i++) {
675ceaec73dSDamien Bergamini 		data = &ring->data[i];
676ceaec73dSDamien Bergamini 
677ceaec73dSDamien Bergamini 		if (data->m != NULL) {
678ceaec73dSDamien Bergamini 			bus_dmamap_sync(ring->data_dmat, data->map,
679ceaec73dSDamien Bergamini 			    BUS_DMASYNC_POSTWRITE);
680ceaec73dSDamien Bergamini 			bus_dmamap_unload(ring->data_dmat, data->map);
681ceaec73dSDamien Bergamini 			m_freem(data->m);
682ceaec73dSDamien Bergamini 			data->m = NULL;
683ceaec73dSDamien Bergamini 		}
684ceaec73dSDamien Bergamini 
685ceaec73dSDamien Bergamini 		if (data->ni != NULL) {
686ceaec73dSDamien Bergamini 			ieee80211_free_node(data->ni);
687ceaec73dSDamien Bergamini 			data->ni = NULL;
688ceaec73dSDamien Bergamini 		}
689ceaec73dSDamien Bergamini 	}
690ceaec73dSDamien Bergamini 
691ceaec73dSDamien Bergamini 	ring->queued = 0;
692ceaec73dSDamien Bergamini 	ring->cur = ring->next = 0;
693ceaec73dSDamien Bergamini }
694ceaec73dSDamien Bergamini 
695ceaec73dSDamien Bergamini static void
iwi_free_tx_ring(struct iwi_softc * sc,struct iwi_tx_ring * ring)696ceaec73dSDamien Bergamini iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
697ceaec73dSDamien Bergamini {
698ceaec73dSDamien Bergamini 	struct iwi_tx_data *data;
699ceaec73dSDamien Bergamini 	int i;
700ceaec73dSDamien Bergamini 
701ceaec73dSDamien Bergamini 	if (ring->desc != NULL) {
702ceaec73dSDamien Bergamini 		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
703ceaec73dSDamien Bergamini 		    BUS_DMASYNC_POSTWRITE);
704ceaec73dSDamien Bergamini 		bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
705ceaec73dSDamien Bergamini 		bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
706ceaec73dSDamien Bergamini 	}
707ceaec73dSDamien Bergamini 
708ceaec73dSDamien Bergamini 	if (ring->desc_dmat != NULL)
709ceaec73dSDamien Bergamini 		bus_dma_tag_destroy(ring->desc_dmat);
710ceaec73dSDamien Bergamini 
711ceaec73dSDamien Bergamini 	if (ring->data != NULL) {
712ceaec73dSDamien Bergamini 		for (i = 0; i < ring->count; i++) {
713ceaec73dSDamien Bergamini 			data = &ring->data[i];
714ceaec73dSDamien Bergamini 
715ceaec73dSDamien Bergamini 			if (data->m != NULL) {
716ceaec73dSDamien Bergamini 				bus_dmamap_sync(ring->data_dmat, data->map,
717ceaec73dSDamien Bergamini 				    BUS_DMASYNC_POSTWRITE);
718ceaec73dSDamien Bergamini 				bus_dmamap_unload(ring->data_dmat, data->map);
719ceaec73dSDamien Bergamini 				m_freem(data->m);
720ceaec73dSDamien Bergamini 			}
721ceaec73dSDamien Bergamini 
722ceaec73dSDamien Bergamini 			if (data->ni != NULL)
723ceaec73dSDamien Bergamini 				ieee80211_free_node(data->ni);
724ceaec73dSDamien Bergamini 
725ceaec73dSDamien Bergamini 			if (data->map != NULL)
726ceaec73dSDamien Bergamini 				bus_dmamap_destroy(ring->data_dmat, data->map);
727ceaec73dSDamien Bergamini 		}
728ceaec73dSDamien Bergamini 
729ceaec73dSDamien Bergamini 		free(ring->data, M_DEVBUF);
730ceaec73dSDamien Bergamini 	}
731ceaec73dSDamien Bergamini 
732ceaec73dSDamien Bergamini 	if (ring->data_dmat != NULL)
733ceaec73dSDamien Bergamini 		bus_dma_tag_destroy(ring->data_dmat);
734ceaec73dSDamien Bergamini }
735ceaec73dSDamien Bergamini 
736ceaec73dSDamien Bergamini static int
iwi_alloc_rx_ring(struct iwi_softc * sc,struct iwi_rx_ring * ring,int count)737ceaec73dSDamien Bergamini iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
738ceaec73dSDamien Bergamini {
739ceaec73dSDamien Bergamini 	struct iwi_rx_data *data;
740ceaec73dSDamien Bergamini 	int i, error;
741ceaec73dSDamien Bergamini 
742ceaec73dSDamien Bergamini 	ring->count = count;
743ceaec73dSDamien Bergamini 	ring->cur = 0;
744ceaec73dSDamien Bergamini 
745ac2fffa4SPedro F. Giffuni 	ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
746ceaec73dSDamien Bergamini 	    M_NOWAIT | M_ZERO);
747ceaec73dSDamien Bergamini 	if (ring->data == NULL) {
748ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not allocate soft data\n");
749ceaec73dSDamien Bergamini 		error = ENOMEM;
750ceaec73dSDamien Bergamini 		goto fail;
751ceaec73dSDamien Bergamini 	}
752ceaec73dSDamien Bergamini 
753b0ea96dfSKevin Lo 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
754b0ea96dfSKevin Lo 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
755b0ea96dfSKevin Lo 	    1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
756ceaec73dSDamien Bergamini 	if (error != 0) {
757ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not create data DMA tag\n");
758ceaec73dSDamien Bergamini 		goto fail;
759ceaec73dSDamien Bergamini 	}
760ceaec73dSDamien Bergamini 
761ceaec73dSDamien Bergamini 	for (i = 0; i < count; i++) {
762ceaec73dSDamien Bergamini 		data = &ring->data[i];
763ceaec73dSDamien Bergamini 
764ceaec73dSDamien Bergamini 		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
765ceaec73dSDamien Bergamini 		if (error != 0) {
766ceaec73dSDamien Bergamini 			device_printf(sc->sc_dev, "could not create DMA map\n");
767ceaec73dSDamien Bergamini 			goto fail;
768ceaec73dSDamien Bergamini 		}
769ceaec73dSDamien Bergamini 
770c6499eccSGleb Smirnoff 		data->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
771ceaec73dSDamien Bergamini 		if (data->m == NULL) {
772ceaec73dSDamien Bergamini 			device_printf(sc->sc_dev,
773ceaec73dSDamien Bergamini 			    "could not allocate rx mbuf\n");
774ceaec73dSDamien Bergamini 			error = ENOMEM;
775ceaec73dSDamien Bergamini 			goto fail;
776ceaec73dSDamien Bergamini 		}
777ceaec73dSDamien Bergamini 
778ceaec73dSDamien Bergamini 		error = bus_dmamap_load(ring->data_dmat, data->map,
779ceaec73dSDamien Bergamini 		    mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
780ceaec73dSDamien Bergamini 		    &data->physaddr, 0);
781ceaec73dSDamien Bergamini 		if (error != 0) {
782ceaec73dSDamien Bergamini 			device_printf(sc->sc_dev,
783ceaec73dSDamien Bergamini 			    "could not load rx buf DMA map");
784ceaec73dSDamien Bergamini 			goto fail;
785ceaec73dSDamien Bergamini 		}
786ceaec73dSDamien Bergamini 
787ceaec73dSDamien Bergamini 		data->reg = IWI_CSR_RX_BASE + i * 4;
788ceaec73dSDamien Bergamini 	}
789ceaec73dSDamien Bergamini 
790ceaec73dSDamien Bergamini 	return 0;
791ceaec73dSDamien Bergamini 
792ceaec73dSDamien Bergamini fail:	iwi_free_rx_ring(sc, ring);
793ceaec73dSDamien Bergamini 	return error;
794ceaec73dSDamien Bergamini }
795ceaec73dSDamien Bergamini 
796ceaec73dSDamien Bergamini static void
iwi_reset_rx_ring(struct iwi_softc * sc,struct iwi_rx_ring * ring)797ceaec73dSDamien Bergamini iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
798ceaec73dSDamien Bergamini {
799ceaec73dSDamien Bergamini 	ring->cur = 0;
800ceaec73dSDamien Bergamini }
801ceaec73dSDamien Bergamini 
802ceaec73dSDamien Bergamini static void
iwi_free_rx_ring(struct iwi_softc * sc,struct iwi_rx_ring * ring)803ceaec73dSDamien Bergamini iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
804ceaec73dSDamien Bergamini {
805ceaec73dSDamien Bergamini 	struct iwi_rx_data *data;
806ceaec73dSDamien Bergamini 	int i;
807ceaec73dSDamien Bergamini 
808ceaec73dSDamien Bergamini 	if (ring->data != NULL) {
809ceaec73dSDamien Bergamini 		for (i = 0; i < ring->count; i++) {
810ceaec73dSDamien Bergamini 			data = &ring->data[i];
811ceaec73dSDamien Bergamini 
812ceaec73dSDamien Bergamini 			if (data->m != NULL) {
813ceaec73dSDamien Bergamini 				bus_dmamap_sync(ring->data_dmat, data->map,
814ceaec73dSDamien Bergamini 				    BUS_DMASYNC_POSTREAD);
815ceaec73dSDamien Bergamini 				bus_dmamap_unload(ring->data_dmat, data->map);
816ceaec73dSDamien Bergamini 				m_freem(data->m);
817ceaec73dSDamien Bergamini 			}
818ceaec73dSDamien Bergamini 
819ceaec73dSDamien Bergamini 			if (data->map != NULL)
820ceaec73dSDamien Bergamini 				bus_dmamap_destroy(ring->data_dmat, data->map);
821ceaec73dSDamien Bergamini 		}
822ceaec73dSDamien Bergamini 
823ceaec73dSDamien Bergamini 		free(ring->data, M_DEVBUF);
824ceaec73dSDamien Bergamini 	}
825ceaec73dSDamien Bergamini 
826ceaec73dSDamien Bergamini 	if (ring->data_dmat != NULL)
827ceaec73dSDamien Bergamini 		bus_dma_tag_destroy(ring->data_dmat);
828ceaec73dSDamien Bergamini }
829ceaec73dSDamien Bergamini 
830ceaec73dSDamien Bergamini static int
iwi_shutdown(device_t dev)831ceaec73dSDamien Bergamini iwi_shutdown(device_t dev)
832ceaec73dSDamien Bergamini {
833ceaec73dSDamien Bergamini 	struct iwi_softc *sc = device_get_softc(dev);
834ceaec73dSDamien Bergamini 
835ceaec73dSDamien Bergamini 	iwi_stop(sc);
836c10b1400SMax Laier 	iwi_put_firmware(sc);		/* ??? XXX */
837ceaec73dSDamien Bergamini 
838ceaec73dSDamien Bergamini 	return 0;
839ceaec73dSDamien Bergamini }
840ceaec73dSDamien Bergamini 
841ceaec73dSDamien Bergamini static int
iwi_suspend(device_t dev)842ceaec73dSDamien Bergamini iwi_suspend(device_t dev)
843ceaec73dSDamien Bergamini {
844ceaec73dSDamien Bergamini 	struct iwi_softc *sc = device_get_softc(dev);
8457a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
846ceaec73dSDamien Bergamini 
847ada977b1SBernhard Schmidt 	ieee80211_suspend_all(ic);
848ceaec73dSDamien Bergamini 	return 0;
849ceaec73dSDamien Bergamini }
850ceaec73dSDamien Bergamini 
851ceaec73dSDamien Bergamini static int
iwi_resume(device_t dev)852ceaec73dSDamien Bergamini iwi_resume(device_t dev)
853ceaec73dSDamien Bergamini {
854ceaec73dSDamien Bergamini 	struct iwi_softc *sc = device_get_softc(dev);
8557a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
856ceaec73dSDamien Bergamini 
857acea4241SDamien Bergamini 	pci_write_config(dev, 0x41, 0, 1);
858acea4241SDamien Bergamini 
859ada977b1SBernhard Schmidt 	ieee80211_resume_all(ic);
860ceaec73dSDamien Bergamini 	return 0;
861ceaec73dSDamien Bergamini }
862ceaec73dSDamien Bergamini 
86328a38073SDamien Bergamini static struct ieee80211_node *
iwi_node_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN])86438c208f8SSam Leffler iwi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
86528a38073SDamien Bergamini {
86628a38073SDamien Bergamini 	struct iwi_node *in;
86728a38073SDamien Bergamini 
86828a38073SDamien Bergamini 	in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
86928a38073SDamien Bergamini 	if (in == NULL)
87028a38073SDamien Bergamini 		return NULL;
87138c208f8SSam Leffler 	/* XXX assign sta table entry for adhoc */
87228a38073SDamien Bergamini 	in->in_station = -1;
87328a38073SDamien Bergamini 
87428a38073SDamien Bergamini 	return &in->in_node;
87528a38073SDamien Bergamini }
87628a38073SDamien Bergamini 
87728a38073SDamien Bergamini static void
iwi_node_free(struct ieee80211_node * ni)87828a38073SDamien Bergamini iwi_node_free(struct ieee80211_node *ni)
87928a38073SDamien Bergamini {
88028a38073SDamien Bergamini 	struct ieee80211com *ic = ni->ni_ic;
8817a79cebfSGleb Smirnoff 	struct iwi_softc *sc = ic->ic_softc;
88228a38073SDamien Bergamini 	struct iwi_node *in = (struct iwi_node *)ni;
88328a38073SDamien Bergamini 
884c10b1400SMax Laier 	if (in->in_station != -1) {
885c10b1400SMax Laier 		DPRINTF(("%s mac %6D station %u\n", __func__,
886c10b1400SMax Laier 		    ni->ni_macaddr, ":", in->in_station));
88728a38073SDamien Bergamini 		free_unr(sc->sc_unr, in->in_station);
888c10b1400SMax Laier 	}
88928a38073SDamien Bergamini 
89028a38073SDamien Bergamini 	sc->sc_node_free(ni);
89128a38073SDamien Bergamini }
89228a38073SDamien Bergamini 
893c10b1400SMax Laier /*
894c10b1400SMax Laier  * Convert h/w rate code to IEEE rate code.
895c10b1400SMax Laier  */
896c10b1400SMax Laier static int
iwi_cvtrate(int iwirate)897c10b1400SMax Laier iwi_cvtrate(int iwirate)
898c10b1400SMax Laier {
899c10b1400SMax Laier 	switch (iwirate) {
900c10b1400SMax Laier 	case IWI_RATE_DS1:	return 2;
901c10b1400SMax Laier 	case IWI_RATE_DS2:	return 4;
902c10b1400SMax Laier 	case IWI_RATE_DS5:	return 11;
903c10b1400SMax Laier 	case IWI_RATE_DS11:	return 22;
904c10b1400SMax Laier 	case IWI_RATE_OFDM6:	return 12;
905c10b1400SMax Laier 	case IWI_RATE_OFDM9:	return 18;
906c10b1400SMax Laier 	case IWI_RATE_OFDM12:	return 24;
907c10b1400SMax Laier 	case IWI_RATE_OFDM18:	return 36;
908c10b1400SMax Laier 	case IWI_RATE_OFDM24:	return 48;
909c10b1400SMax Laier 	case IWI_RATE_OFDM36:	return 72;
910c10b1400SMax Laier 	case IWI_RATE_OFDM48:	return 96;
911c10b1400SMax Laier 	case IWI_RATE_OFDM54:	return 108;
912c10b1400SMax Laier 	}
913ceaec73dSDamien Bergamini 	return 0;
914ceaec73dSDamien Bergamini }
915ceaec73dSDamien Bergamini 
916ceaec73dSDamien Bergamini /*
91728a38073SDamien Bergamini  * The firmware automatically adapts the transmit speed.  We report its current
91828a38073SDamien Bergamini  * value here.
919ceaec73dSDamien Bergamini  */
920ceaec73dSDamien Bergamini static void
iwi_media_status(if_t ifp,struct ifmediareq * imr)92176614fdcSJustin Hibbits iwi_media_status(if_t ifp, struct ifmediareq *imr)
922ceaec73dSDamien Bergamini {
92376614fdcSJustin Hibbits 	struct ieee80211vap *vap = if_getsoftc(ifp);
924b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
9257a79cebfSGleb Smirnoff 	struct iwi_softc *sc = ic->ic_softc;
92639bb444cSAdrian Chadd 	struct ieee80211_node *ni;
927ceaec73dSDamien Bergamini 
928ceaec73dSDamien Bergamini 	/* read current transmission rate from adapter */
92939bb444cSAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
930*70674500SAdrian Chadd 	ieee80211_node_set_txrate_dot11rate(ni,
931*70674500SAdrian Chadd 	    iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE)));
93239bb444cSAdrian Chadd 	ieee80211_free_node(ni);
933b032f27cSSam Leffler 	ieee80211_media_status(ifp, imr);
934ceaec73dSDamien Bergamini }
935ceaec73dSDamien Bergamini 
936ceaec73dSDamien Bergamini static int
iwi_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)937b032f27cSSam Leffler iwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
938ceaec73dSDamien Bergamini {
939b032f27cSSam Leffler 	struct iwi_vap *ivp = IWI_VAP(vap);
940b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
9417a79cebfSGleb Smirnoff 	struct iwi_softc *sc = ic->ic_softc;
942b032f27cSSam Leffler 	IWI_LOCK_DECL;
94384f55b29SDamien Bergamini 
944c10b1400SMax Laier 	DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
945b032f27cSSam Leffler 		ieee80211_state_name[vap->iv_state],
946c10b1400SMax Laier 		ieee80211_state_name[nstate], sc->flags));
947ceaec73dSDamien Bergamini 
9485efea30fSAndrew Thompson 	IEEE80211_UNLOCK(ic);
9495efea30fSAndrew Thompson 	IWI_LOCK(sc);
950ceaec73dSDamien Bergamini 	switch (nstate) {
951b032f27cSSam Leffler 	case IEEE80211_S_INIT:
952b032f27cSSam Leffler 		/*
953b032f27cSSam Leffler 		 * NB: don't try to do this if iwi_stop_master has
954b032f27cSSam Leffler 		 *     shutdown the firmware and disabled interrupts.
955b032f27cSSam Leffler 		 */
956b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_RUN &&
957b032f27cSSam Leffler 		    (sc->flags & IWI_FLAG_FW_INITED))
9585efea30fSAndrew Thompson 			iwi_disassociate(sc, 0);
959ceaec73dSDamien Bergamini 		break;
960b032f27cSSam Leffler 	case IEEE80211_S_AUTH:
9615efea30fSAndrew Thompson 		iwi_auth_and_assoc(sc, vap);
9625efea30fSAndrew Thompson 		break;
963ceaec73dSDamien Bergamini 	case IEEE80211_S_RUN:
964b032f27cSSam Leffler 		if (vap->iv_opmode == IEEE80211_M_IBSS &&
965b032f27cSSam Leffler 		    vap->iv_state == IEEE80211_S_SCAN) {
966c10b1400SMax Laier 			/*
967c10b1400SMax Laier 			 * XXX when joining an ibss network we are called
968c10b1400SMax Laier 			 * with a SCAN -> RUN transition on scan complete.
969c10b1400SMax Laier 			 * Use that to call iwi_auth_and_assoc.  On completing
970c10b1400SMax Laier 			 * the join we are then called again with an
971c10b1400SMax Laier 			 * AUTH -> RUN transition and we want to do nothing.
972c10b1400SMax Laier 			 * This is all totally bogus and needs to be redone.
973c10b1400SMax Laier 			 */
9745efea30fSAndrew Thompson 			iwi_auth_and_assoc(sc, vap);
97540668dc5SBernhard Schmidt 		} else if (vap->iv_opmode == IEEE80211_M_MONITOR)
97640668dc5SBernhard Schmidt 			ieee80211_runtask(ic, &sc->sc_monitortask);
977acea4241SDamien Bergamini 		break;
97868e8e04eSSam Leffler 	case IEEE80211_S_ASSOC:
97968e8e04eSSam Leffler 		/*
980b032f27cSSam Leffler 		 * If we are transitioning from AUTH then just wait
981b032f27cSSam Leffler 		 * for the ASSOC status to come back from the firmware.
982b032f27cSSam Leffler 		 * Otherwise we need to issue the association request.
98368e8e04eSSam Leffler 		 */
984b032f27cSSam Leffler 		if (vap->iv_state == IEEE80211_S_AUTH)
98568e8e04eSSam Leffler 			break;
9865efea30fSAndrew Thompson 		iwi_auth_and_assoc(sc, vap);
9875efea30fSAndrew Thompson 		break;
98868e8e04eSSam Leffler 	default:
989ceaec73dSDamien Bergamini 		break;
990ceaec73dSDamien Bergamini 	}
9915efea30fSAndrew Thompson 	IWI_UNLOCK(sc);
9925efea30fSAndrew Thompson 	IEEE80211_LOCK(ic);
993b032f27cSSam Leffler 	return ivp->iwi_newstate(vap, nstate, arg);
994ceaec73dSDamien Bergamini }
995ceaec73dSDamien Bergamini 
996fb533d55SDamien Bergamini /*
997fb533d55SDamien Bergamini  * WME parameters coming from IEEE 802.11e specification.  These values are
998fb533d55SDamien Bergamini  * already declared in ieee80211_proto.c, but they are static so they can't
999fb533d55SDamien Bergamini  * be reused here.
1000fb533d55SDamien Bergamini  */
1001fb533d55SDamien Bergamini static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
1002fb533d55SDamien Bergamini 	{ 0, 3, 5,  7,   0 },	/* WME_AC_BE */
1003fb533d55SDamien Bergamini 	{ 0, 3, 5, 10,   0 },	/* WME_AC_BK */
1004fb533d55SDamien Bergamini 	{ 0, 2, 4,  5, 188 },	/* WME_AC_VI */
1005fb533d55SDamien Bergamini 	{ 0, 2, 3,  4, 102 }	/* WME_AC_VO */
1006fb533d55SDamien Bergamini };
1007fb533d55SDamien Bergamini 
1008fb533d55SDamien Bergamini static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
1009fb533d55SDamien Bergamini 	{ 0, 3, 4,  6,   0 },	/* WME_AC_BE */
1010fb533d55SDamien Bergamini 	{ 0, 3, 4, 10,   0 },	/* WME_AC_BK */
1011fb533d55SDamien Bergamini 	{ 0, 2, 3,  4,  94 },	/* WME_AC_VI */
1012fb533d55SDamien Bergamini 	{ 0, 2, 2,  3,  47 }	/* WME_AC_VO */
1013fb533d55SDamien Bergamini };
1014fb533d55SDamien Bergamini #define IWI_EXP2(v)	htole16((1 << (v)) - 1)
1015fb533d55SDamien Bergamini #define IWI_USEC(v)	htole16(IEEE80211_TXOP_TO_US(v))
1016c10b1400SMax Laier 
1017c10b1400SMax Laier static void
iwi_wme_init(struct iwi_softc * sc)1018c10b1400SMax Laier iwi_wme_init(struct iwi_softc *sc)
1019c10b1400SMax Laier {
1020fb533d55SDamien Bergamini 	const struct wmeParams *wmep;
1021fb533d55SDamien Bergamini 	int ac;
1022ec01dc2bSDamien Bergamini 
1023c10b1400SMax Laier 	memset(sc->wme, 0, sizeof sc->wme);
1024c10b1400SMax Laier 	for (ac = 0; ac < WME_NUM_AC; ac++) {
1025c10b1400SMax Laier 		/* set WME values for CCK modulation */
1026c10b1400SMax Laier 		wmep = &iwi_wme_cck_params[ac];
1027c10b1400SMax Laier 		sc->wme[1].aifsn[ac] = wmep->wmep_aifsn;
1028c10b1400SMax Laier 		sc->wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1029c10b1400SMax Laier 		sc->wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1030c10b1400SMax Laier 		sc->wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1031c10b1400SMax Laier 		sc->wme[1].acm[ac]   = wmep->wmep_acm;
1032c10b1400SMax Laier 
1033c10b1400SMax Laier 		/* set WME values for OFDM modulation */
1034c10b1400SMax Laier 		wmep = &iwi_wme_ofdm_params[ac];
1035c10b1400SMax Laier 		sc->wme[2].aifsn[ac] = wmep->wmep_aifsn;
1036c10b1400SMax Laier 		sc->wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1037c10b1400SMax Laier 		sc->wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1038c10b1400SMax Laier 		sc->wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1039c10b1400SMax Laier 		sc->wme[2].acm[ac]   = wmep->wmep_acm;
1040c10b1400SMax Laier 	}
1041c10b1400SMax Laier }
1042c10b1400SMax Laier 
1043c10b1400SMax Laier static int
iwi_wme_setparams(struct iwi_softc * sc)10446468c413SGleb Smirnoff iwi_wme_setparams(struct iwi_softc *sc)
1045c10b1400SMax Laier {
10467a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
10479fbe631aSAdrian Chadd 	struct chanAccParams chp;
1048c10b1400SMax Laier 	const struct wmeParams *wmep;
1049c10b1400SMax Laier 	int ac;
1050fb533d55SDamien Bergamini 
10519fbe631aSAdrian Chadd 	ieee80211_wme_ic_getparams(ic, &chp);
10529fbe631aSAdrian Chadd 
1053fb533d55SDamien Bergamini 	for (ac = 0; ac < WME_NUM_AC; ac++) {
1054fb533d55SDamien Bergamini 		/* set WME values for current operating mode */
10559fbe631aSAdrian Chadd 		wmep = &chp.cap_wmeParams[ac];
1056c10b1400SMax Laier 		sc->wme[0].aifsn[ac] = wmep->wmep_aifsn;
1057c10b1400SMax Laier 		sc->wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1058c10b1400SMax Laier 		sc->wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1059c10b1400SMax Laier 		sc->wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1060c10b1400SMax Laier 		sc->wme[0].acm[ac]   = wmep->wmep_acm;
1061fb533d55SDamien Bergamini 	}
1062fb533d55SDamien Bergamini 
1063fb533d55SDamien Bergamini 	DPRINTF(("Setting WME parameters\n"));
1064c10b1400SMax Laier 	return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, sc->wme, sizeof sc->wme);
1065c10b1400SMax Laier }
1066fb533d55SDamien Bergamini #undef IWI_USEC
1067fb533d55SDamien Bergamini #undef IWI_EXP2
1068c10b1400SMax Laier 
1069c10b1400SMax Laier static int
iwi_wme_update(struct ieee80211com * ic)1070c10b1400SMax Laier iwi_wme_update(struct ieee80211com *ic)
1071c10b1400SMax Laier {
10727a79cebfSGleb Smirnoff 	struct iwi_softc *sc = ic->ic_softc;
10735efea30fSAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1074a14954c5SAndriy Voskoboinyk 	IWI_LOCK_DECL;
1075c10b1400SMax Laier 
1076c10b1400SMax Laier 	/*
1077c10b1400SMax Laier 	 * We may be called to update the WME parameters in
1078c10b1400SMax Laier 	 * the adapter at various places.  If we're already
1079853569c6SSam Leffler 	 * associated then initiate the request immediately;
1080853569c6SSam Leffler 	 * otherwise we assume the params will get sent down
1081853569c6SSam Leffler 	 * to the adapter as part of the work iwi_auth_and_assoc
1082853569c6SSam Leffler 	 * does.
1083c10b1400SMax Laier 	 */
1084a14954c5SAndriy Voskoboinyk 	if (vap->iv_state == IEEE80211_S_RUN) {
1085a14954c5SAndriy Voskoboinyk 		IWI_LOCK(sc);
1086a14954c5SAndriy Voskoboinyk 		iwi_wme_setparams(sc);
1087a14954c5SAndriy Voskoboinyk 		IWI_UNLOCK(sc);
1088a14954c5SAndriy Voskoboinyk 	}
10895efea30fSAndrew Thompson 	return (0);
1090c10b1400SMax Laier }
1091c10b1400SMax Laier 
1092c10b1400SMax Laier static int
iwi_wme_setie(struct iwi_softc * sc)1093c10b1400SMax Laier iwi_wme_setie(struct iwi_softc *sc)
1094c10b1400SMax Laier {
1095c10b1400SMax Laier 	struct ieee80211_wme_info wme;
1096c10b1400SMax Laier 
1097c10b1400SMax Laier 	memset(&wme, 0, sizeof wme);
1098c10b1400SMax Laier 	wme.wme_id = IEEE80211_ELEMID_VENDOR;
1099c10b1400SMax Laier 	wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
1100c10b1400SMax Laier 	wme.wme_oui[0] = 0x00;
1101c10b1400SMax Laier 	wme.wme_oui[1] = 0x50;
1102c10b1400SMax Laier 	wme.wme_oui[2] = 0xf2;
1103c10b1400SMax Laier 	wme.wme_type = WME_OUI_TYPE;
1104c10b1400SMax Laier 	wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
1105c10b1400SMax Laier 	wme.wme_version = WME_VERSION;
1106c10b1400SMax Laier 	wme.wme_info = 0;
1107c10b1400SMax Laier 
1108c10b1400SMax Laier 	DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
1109c10b1400SMax Laier 	return iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme);
1110ec01dc2bSDamien Bergamini }
1111ec01dc2bSDamien Bergamini 
1112ceaec73dSDamien Bergamini /*
1113ceaec73dSDamien Bergamini  * Read 16 bits at address 'addr' from the serial EEPROM.
1114ceaec73dSDamien Bergamini  */
1115ceaec73dSDamien Bergamini static uint16_t
iwi_read_prom_word(struct iwi_softc * sc,uint8_t addr)1116ceaec73dSDamien Bergamini iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1117ceaec73dSDamien Bergamini {
1118ceaec73dSDamien Bergamini 	uint32_t tmp;
1119ceaec73dSDamien Bergamini 	uint16_t val;
1120ceaec73dSDamien Bergamini 	int n;
1121ceaec73dSDamien Bergamini 
1122ceaec73dSDamien Bergamini 	/* clock C once before the first command */
1123ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, 0);
1124ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1125ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1126ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1127ceaec73dSDamien Bergamini 
1128ceaec73dSDamien Bergamini 	/* write start bit (1) */
1129ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1130ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1131ceaec73dSDamien Bergamini 
1132ceaec73dSDamien Bergamini 	/* write READ opcode (10) */
1133ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1134ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1135ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1136ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1137ceaec73dSDamien Bergamini 
1138ceaec73dSDamien Bergamini 	/* write address A7-A0 */
1139ceaec73dSDamien Bergamini 	for (n = 7; n >= 0; n--) {
1140ceaec73dSDamien Bergamini 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1141ceaec73dSDamien Bergamini 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1142ceaec73dSDamien Bergamini 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1143ceaec73dSDamien Bergamini 		    (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1144ceaec73dSDamien Bergamini 	}
1145ceaec73dSDamien Bergamini 
1146ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1147ceaec73dSDamien Bergamini 
1148ceaec73dSDamien Bergamini 	/* read data Q15-Q0 */
1149ceaec73dSDamien Bergamini 	val = 0;
1150ceaec73dSDamien Bergamini 	for (n = 15; n >= 0; n--) {
1151ceaec73dSDamien Bergamini 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1152ceaec73dSDamien Bergamini 		IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1153ceaec73dSDamien Bergamini 		tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1154ceaec73dSDamien Bergamini 		val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1155ceaec73dSDamien Bergamini 	}
1156ceaec73dSDamien Bergamini 
1157ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, 0);
1158ceaec73dSDamien Bergamini 
1159ceaec73dSDamien Bergamini 	/* clear Chip Select and clock C */
1160ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1161ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, 0);
1162ceaec73dSDamien Bergamini 	IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1163ceaec73dSDamien Bergamini 
116481e6343cSDamien Bergamini 	return val;
1165ceaec73dSDamien Bergamini }
1166ceaec73dSDamien Bergamini 
1167ceaec73dSDamien Bergamini static void
iwi_setcurchan(struct iwi_softc * sc,int chan)1168c10b1400SMax Laier iwi_setcurchan(struct iwi_softc *sc, int chan)
1169ceaec73dSDamien Bergamini {
11707a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1171ceaec73dSDamien Bergamini 
1172c10b1400SMax Laier 	sc->curchan = chan;
11735463c4a4SSam Leffler 	ieee80211_radiotap_chan_change(ic);
1174ceaec73dSDamien Bergamini }
1175ceaec73dSDamien Bergamini 
1176ceaec73dSDamien Bergamini static void
iwi_frame_intr(struct iwi_softc * sc,struct iwi_rx_data * data,int i,struct iwi_frame * frame)1177ceaec73dSDamien Bergamini iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1178ceaec73dSDamien Bergamini     struct iwi_frame *frame)
1179ceaec73dSDamien Bergamini {
11807a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
118146c34436SDamien Bergamini 	struct mbuf *mnew, *m;
1182ceaec73dSDamien Bergamini 	struct ieee80211_node *ni;
1183c10b1400SMax Laier 	int type, error, framelen;
11845463c4a4SSam Leffler 	int8_t rssi, nf;
1185294410acSMax Laier 	IWI_LOCK_DECL;
1186ceaec73dSDamien Bergamini 
1187c10b1400SMax Laier 	framelen = le16toh(frame->len);
1188c10b1400SMax Laier 	if (framelen < IEEE80211_MIN_LEN || framelen > MCLBYTES) {
1189c10b1400SMax Laier 		/*
1190c10b1400SMax Laier 		 * XXX >MCLBYTES is bogus as it means the h/w dma'd
1191c10b1400SMax Laier 		 *     out of bounds; need to figure out how to limit
1192c10b1400SMax Laier 		 *     frame size in the firmware
1193c10b1400SMax Laier 		 */
1194c10b1400SMax Laier 		/* XXX stat */
1195c10b1400SMax Laier 		DPRINTFN(1,
1196c10b1400SMax Laier 		    ("drop rx frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1197c10b1400SMax Laier 		    le16toh(frame->len), frame->chan, frame->rssi,
1198c10b1400SMax Laier 		    frame->rssi_dbm));
1199acea4241SDamien Bergamini 		return;
1200c10b1400SMax Laier 	}
1201c10b1400SMax Laier 
1202c10b1400SMax Laier 	DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1203c10b1400SMax Laier 	    le16toh(frame->len), frame->chan, frame->rssi, frame->rssi_dbm));
1204c10b1400SMax Laier 
1205c10b1400SMax Laier 	if (frame->chan != sc->curchan)
1206c10b1400SMax Laier 		iwi_setcurchan(sc, frame->chan);
1207acea4241SDamien Bergamini 
120846c34436SDamien Bergamini 	/*
120946c34436SDamien Bergamini 	 * Try to allocate a new mbuf for this ring element and load it before
121046c34436SDamien Bergamini 	 * processing the current mbuf. If the ring element cannot be loaded,
121146c34436SDamien Bergamini 	 * drop the received packet and reuse the old mbuf. In the unlikely
121246c34436SDamien Bergamini 	 * case that the old mbuf can't be reloaded either, explicitly panic.
121346c34436SDamien Bergamini 	 */
1214c6499eccSGleb Smirnoff 	mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
121546c34436SDamien Bergamini 	if (mnew == NULL) {
12167a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_ierrors, 1);
121746c34436SDamien Bergamini 		return;
121846c34436SDamien Bergamini 	}
121946c34436SDamien Bergamini 
1220ceaec73dSDamien Bergamini 	bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1221ceaec73dSDamien Bergamini 
122246c34436SDamien Bergamini 	error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
122346c34436SDamien Bergamini 	    mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr,
122446c34436SDamien Bergamini 	    0);
122546c34436SDamien Bergamini 	if (error != 0) {
122646c34436SDamien Bergamini 		m_freem(mnew);
122746c34436SDamien Bergamini 
122846c34436SDamien Bergamini 		/* try to reload the old mbuf */
122946c34436SDamien Bergamini 		error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
123046c34436SDamien Bergamini 		    mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
123146c34436SDamien Bergamini 		    &data->physaddr, 0);
123246c34436SDamien Bergamini 		if (error != 0) {
123346c34436SDamien Bergamini 			/* very unlikely that it will fail... */
123446c34436SDamien Bergamini 			panic("%s: could not load old rx mbuf",
123546c34436SDamien Bergamini 			    device_get_name(sc->sc_dev));
123646c34436SDamien Bergamini 		}
12377a79cebfSGleb Smirnoff 		counter_u64_add(ic->ic_ierrors, 1);
123846c34436SDamien Bergamini 		return;
123946c34436SDamien Bergamini 	}
124046c34436SDamien Bergamini 
124146c34436SDamien Bergamini 	/*
124246c34436SDamien Bergamini 	 * New mbuf successfully loaded, update Rx ring and continue
124346c34436SDamien Bergamini 	 * processing.
124446c34436SDamien Bergamini 	 */
1245ceaec73dSDamien Bergamini 	m = data->m;
124646c34436SDamien Bergamini 	data->m = mnew;
124746c34436SDamien Bergamini 	CSR_WRITE_4(sc, data->reg, data->physaddr);
124846c34436SDamien Bergamini 
124946c34436SDamien Bergamini 	/* finalize mbuf */
1250ceaec73dSDamien Bergamini 	m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1251c10b1400SMax Laier 	    sizeof (struct iwi_frame) + framelen;
1252ceaec73dSDamien Bergamini 
1253ceaec73dSDamien Bergamini 	m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1254ceaec73dSDamien Bergamini 
1255fedda423SSam Leffler 	rssi = frame->rssi_dbm;
12565463c4a4SSam Leffler 	nf = -95;
12575463c4a4SSam Leffler 	if (ieee80211_radiotap_active(ic)) {
1258ceaec73dSDamien Bergamini 		struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1259ceaec73dSDamien Bergamini 
1260ceaec73dSDamien Bergamini 		tap->wr_flags = 0;
12615463c4a4SSam Leffler 		tap->wr_antsignal = rssi;
12625463c4a4SSam Leffler 		tap->wr_antnoise = nf;
1263c10b1400SMax Laier 		tap->wr_rate = iwi_cvtrate(frame->rate);
1264ceaec73dSDamien Bergamini 		tap->wr_antenna = frame->antenna;
1265ceaec73dSDamien Bergamini 	}
1266294410acSMax Laier 	IWI_UNLOCK(sc);
1267ceaec73dSDamien Bergamini 
1268c10b1400SMax Laier 	ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1269b032f27cSSam Leffler 	if (ni != NULL) {
12705463c4a4SSam Leffler 		type = ieee80211_input(ni, m, rssi, nf);
1271ceaec73dSDamien Bergamini 		ieee80211_free_node(ni);
1272b032f27cSSam Leffler 	} else
12735463c4a4SSam Leffler 		type = ieee80211_input_all(ic, m, rssi, nf);
1274c10b1400SMax Laier 
1275294410acSMax Laier 	IWI_LOCK(sc);
1276c10b1400SMax Laier 	if (sc->sc_softled) {
1277c10b1400SMax Laier 		/*
1278c10b1400SMax Laier 		 * Blink for any data frame.  Otherwise do a
1279c10b1400SMax Laier 		 * heartbeat-style blink when idle.  The latter
1280c10b1400SMax Laier 		 * is mainly for station mode where we depend on
1281c10b1400SMax Laier 		 * periodic beacon frames to trigger the poll event.
1282c10b1400SMax Laier 		 */
1283c10b1400SMax Laier 		if (type == IEEE80211_FC0_TYPE_DATA) {
1284c10b1400SMax Laier 			sc->sc_rxrate = frame->rate;
1285c10b1400SMax Laier 			iwi_led_event(sc, IWI_LED_RX);
1286c10b1400SMax Laier 		} else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
1287c10b1400SMax Laier 			iwi_led_event(sc, IWI_LED_POLL);
1288c10b1400SMax Laier 	}
1289c10b1400SMax Laier }
1290c10b1400SMax Laier 
1291c10b1400SMax Laier /*
1292c10b1400SMax Laier  * Check for an association response frame to see if QoS
1293c10b1400SMax Laier  * has been negotiated.  We parse just enough to figure
1294c10b1400SMax Laier  * out if we're supposed to use QoS.  The proper solution
1295c10b1400SMax Laier  * is to pass the frame up so ieee80211_input can do the
1296c10b1400SMax Laier  * work but that's made hard by how things currently are
1297c10b1400SMax Laier  * done in the driver.
1298c10b1400SMax Laier  */
1299c10b1400SMax Laier static void
iwi_checkforqos(struct ieee80211vap * vap,const struct ieee80211_frame * wh,int len)1300b032f27cSSam Leffler iwi_checkforqos(struct ieee80211vap *vap,
1301b032f27cSSam Leffler 	const struct ieee80211_frame *wh, int len)
1302c10b1400SMax Laier {
1303c10b1400SMax Laier #define	SUBTYPE(wh)	((wh)->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
1304c10b1400SMax Laier 	const uint8_t *frm, *efrm, *wme;
1305c10b1400SMax Laier 	struct ieee80211_node *ni;
130699f65e91SJohn Baldwin 	uint16_t capinfo, associd;
1307c10b1400SMax Laier 
1308c10b1400SMax Laier 	/* NB: +8 for capinfo, status, associd, and first ie */
1309c10b1400SMax Laier 	if (!(sizeof(*wh)+8 < len && len < IEEE80211_MAX_LEN) ||
1310c10b1400SMax Laier 	    SUBTYPE(wh) != IEEE80211_FC0_SUBTYPE_ASSOC_RESP)
1311c10b1400SMax Laier 		return;
1312c10b1400SMax Laier 	/*
1313c10b1400SMax Laier 	 * asresp frame format
1314c10b1400SMax Laier 	 *	[2] capability information
1315c10b1400SMax Laier 	 *	[2] status
1316c10b1400SMax Laier 	 *	[2] association ID
1317c10b1400SMax Laier 	 *	[tlv] supported rates
1318c10b1400SMax Laier 	 *	[tlv] extended supported rates
1319c10b1400SMax Laier 	 *	[tlv] WME
1320c10b1400SMax Laier 	 */
1321c10b1400SMax Laier 	frm = (const uint8_t *)&wh[1];
1322c10b1400SMax Laier 	efrm = ((const uint8_t *) wh) + len;
132368e8e04eSSam Leffler 
132468e8e04eSSam Leffler 	capinfo = le16toh(*(const uint16_t *)frm);
132568e8e04eSSam Leffler 	frm += 2;
132699f65e91SJohn Baldwin 	/* status */
132768e8e04eSSam Leffler 	frm += 2;
132868e8e04eSSam Leffler 	associd = le16toh(*(const uint16_t *)frm);
132968e8e04eSSam Leffler 	frm += 2;
1330c10b1400SMax Laier 
1331c10b1400SMax Laier 	wme = NULL;
13327199b6f7SMitsuru IWASAKI 	while (efrm - frm > 1) {
13337199b6f7SMitsuru IWASAKI 		IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1334c10b1400SMax Laier 		switch (*frm) {
1335c10b1400SMax Laier 		case IEEE80211_ELEMID_VENDOR:
1336c10b1400SMax Laier 			if (iswmeoui(frm))
1337c10b1400SMax Laier 				wme = frm;
1338c10b1400SMax Laier 			break;
1339c10b1400SMax Laier 		}
1340c10b1400SMax Laier 		frm += frm[1] + 2;
1341c10b1400SMax Laier 	}
1342c10b1400SMax Laier 
134339bb444cSAdrian Chadd 	ni = ieee80211_ref_node(vap->iv_bss);
134468e8e04eSSam Leffler 	ni->ni_capinfo = capinfo;
13459f953145SBernhard Schmidt 	ni->ni_associd = associd & 0x3fff;
1346c10b1400SMax Laier 	if (wme != NULL)
1347c10b1400SMax Laier 		ni->ni_flags |= IEEE80211_NODE_QOS;
1348c10b1400SMax Laier 	else
1349c10b1400SMax Laier 		ni->ni_flags &= ~IEEE80211_NODE_QOS;
135039bb444cSAdrian Chadd 	ieee80211_free_node(ni);
1351c10b1400SMax Laier #undef SUBTYPE
1352ceaec73dSDamien Bergamini }
1353ceaec73dSDamien Bergamini 
13544982d693SAdrian Chadd static void
iwi_notif_link_quality(struct iwi_softc * sc,struct iwi_notif * notif)13554982d693SAdrian Chadd iwi_notif_link_quality(struct iwi_softc *sc, struct iwi_notif *notif)
13564982d693SAdrian Chadd {
13574982d693SAdrian Chadd 	struct iwi_notif_link_quality *lq;
13584982d693SAdrian Chadd 	int len;
13594982d693SAdrian Chadd 
13604982d693SAdrian Chadd 	len = le16toh(notif->len);
13614982d693SAdrian Chadd 
13621c90e87fSEitan Adler 	DPRINTFN(5, ("Notification (%u) - len=%d, sizeof=%zu\n",
13634982d693SAdrian Chadd 	    notif->type,
13644982d693SAdrian Chadd 	    len,
13654982d693SAdrian Chadd 	    sizeof(struct iwi_notif_link_quality)
13664982d693SAdrian Chadd 	    ));
13674982d693SAdrian Chadd 
13684982d693SAdrian Chadd 	/* enforce length */
13694982d693SAdrian Chadd 	if (len != sizeof(struct iwi_notif_link_quality)) {
13704982d693SAdrian Chadd 		DPRINTFN(5, ("Notification: (%u) too short (%d)\n",
13714982d693SAdrian Chadd 		    notif->type,
13724982d693SAdrian Chadd 		    len));
13734982d693SAdrian Chadd 		return;
13744982d693SAdrian Chadd 	}
13754982d693SAdrian Chadd 
13764982d693SAdrian Chadd 	lq = (struct iwi_notif_link_quality *)(notif + 1);
13774982d693SAdrian Chadd 	memcpy(&sc->sc_linkqual, lq, sizeof(sc->sc_linkqual));
13784982d693SAdrian Chadd 	sc->sc_linkqual_valid = 1;
13794982d693SAdrian Chadd }
13804982d693SAdrian Chadd 
1381b032f27cSSam Leffler /*
1382b032f27cSSam Leffler  * Task queue callbacks for iwi_notification_intr used to avoid LOR's.
1383b032f27cSSam Leffler  */
1384b032f27cSSam Leffler 
1385b032f27cSSam Leffler static void
iwi_notification_intr(struct iwi_softc * sc,struct iwi_notif * notif)1386ceaec73dSDamien Bergamini iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1387ceaec73dSDamien Bergamini {
13887a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1389b032f27cSSam Leffler 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1390ceaec73dSDamien Bergamini 	struct iwi_notif_scan_channel *chan;
1391ceaec73dSDamien Bergamini 	struct iwi_notif_scan_complete *scan;
1392ceaec73dSDamien Bergamini 	struct iwi_notif_authentication *auth;
1393ceaec73dSDamien Bergamini 	struct iwi_notif_association *assoc;
1394c10b1400SMax Laier 	struct iwi_notif_beacon_state *beacon;
1395ceaec73dSDamien Bergamini 
1396ceaec73dSDamien Bergamini 	switch (notif->type) {
1397ceaec73dSDamien Bergamini 	case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1398ceaec73dSDamien Bergamini 		chan = (struct iwi_notif_scan_channel *)(notif + 1);
1399ceaec73dSDamien Bergamini 
1400c10b1400SMax Laier 		DPRINTFN(3, ("Scan of channel %u complete (%u)\n",
140168e8e04eSSam Leffler 		    ieee80211_ieee2mhz(chan->nchan, 0), chan->nchan));
140268e8e04eSSam Leffler 
140368e8e04eSSam Leffler 		/* Reset the timer, the scan is still going */
140468e8e04eSSam Leffler 		sc->sc_state_timer = 3;
1405ceaec73dSDamien Bergamini 		break;
1406ceaec73dSDamien Bergamini 
1407ceaec73dSDamien Bergamini 	case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1408ceaec73dSDamien Bergamini 		scan = (struct iwi_notif_scan_complete *)(notif + 1);
1409ceaec73dSDamien Bergamini 
1410ceaec73dSDamien Bergamini 		DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1411ceaec73dSDamien Bergamini 		    scan->status));
1412ceaec73dSDamien Bergamini 
141368e8e04eSSam Leffler 		IWI_STATE_END(sc, IWI_FW_SCANNING);
1414c10b1400SMax Laier 
141540668dc5SBernhard Schmidt 		/*
141640668dc5SBernhard Schmidt 		 * Monitor mode works by doing a passive scan to set
141740668dc5SBernhard Schmidt 		 * the channel and enable rx.  Because we don't want
141840668dc5SBernhard Schmidt 		 * to abort a scan lest the firmware crash we scan
141940668dc5SBernhard Schmidt 		 * for a short period of time and automatically restart
142040668dc5SBernhard Schmidt 		 * the scan when notified the sweep has completed.
142140668dc5SBernhard Schmidt 		 */
142240668dc5SBernhard Schmidt 		if (vap->iv_opmode == IEEE80211_M_MONITOR) {
142340668dc5SBernhard Schmidt 			ieee80211_runtask(ic, &sc->sc_monitortask);
142440668dc5SBernhard Schmidt 			break;
142540668dc5SBernhard Schmidt 		}
142640668dc5SBernhard Schmidt 
1427b032f27cSSam Leffler 		if (scan->status == IWI_SCAN_COMPLETED) {
1428b032f27cSSam Leffler 			/* NB: don't need to defer, net80211 does it for us */
1429b032f27cSSam Leffler 			ieee80211_scan_next(vap);
1430b032f27cSSam Leffler 		}
1431ceaec73dSDamien Bergamini 		break;
1432ceaec73dSDamien Bergamini 
1433ceaec73dSDamien Bergamini 	case IWI_NOTIF_TYPE_AUTHENTICATION:
1434ceaec73dSDamien Bergamini 		auth = (struct iwi_notif_authentication *)(notif + 1);
1435ceaec73dSDamien Bergamini 		switch (auth->state) {
1436c10b1400SMax Laier 		case IWI_AUTH_SUCCESS:
1437c10b1400SMax Laier 			DPRINTFN(2, ("Authentication succeeeded\n"));
14385efea30fSAndrew Thompson 			ieee80211_new_state(vap, IEEE80211_S_ASSOC, -1);
1439ceaec73dSDamien Bergamini 			break;
1440c10b1400SMax Laier 		case IWI_AUTH_FAIL:
1441b032f27cSSam Leffler 			/*
1442b032f27cSSam Leffler 			 * These are delivered as an unsolicited deauth
1443b032f27cSSam Leffler 			 * (e.g. due to inactivity) or in response to an
1444b032f27cSSam Leffler 			 * associate request.
1445b032f27cSSam Leffler 			 */
1446c10b1400SMax Laier 			sc->flags &= ~IWI_FLAG_ASSOCIATED;
1447b032f27cSSam Leffler 			if (vap->iv_state != IEEE80211_S_RUN) {
1448b032f27cSSam Leffler 				DPRINTFN(2, ("Authentication failed\n"));
1449b032f27cSSam Leffler 				vap->iv_stats.is_rx_auth_fail++;
145068e8e04eSSam Leffler 				IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1451b032f27cSSam Leffler 			} else {
1452b032f27cSSam Leffler 				DPRINTFN(2, ("Deauthenticated\n"));
1453b032f27cSSam Leffler 				vap->iv_stats.is_rx_deauth++;
1454b032f27cSSam Leffler 			}
14555efea30fSAndrew Thompson 			ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1456c10b1400SMax Laier 			break;
1457c10b1400SMax Laier 		case IWI_AUTH_SENT_1:
1458c10b1400SMax Laier 		case IWI_AUTH_RECV_2:
1459c10b1400SMax Laier 		case IWI_AUTH_SEQ1_PASS:
1460c10b1400SMax Laier 			break;
1461c10b1400SMax Laier 		case IWI_AUTH_SEQ1_FAIL:
1462c10b1400SMax Laier 			DPRINTFN(2, ("Initial authentication handshake failed; "
1463c10b1400SMax Laier 				"you probably need shared key\n"));
1464b032f27cSSam Leffler 			vap->iv_stats.is_rx_auth_fail++;
146568e8e04eSSam Leffler 			IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1466c10b1400SMax Laier 			/* XXX retry shared key when in auto */
1467ceaec73dSDamien Bergamini 			break;
1468ceaec73dSDamien Bergamini 		default:
1469ceaec73dSDamien Bergamini 			device_printf(sc->sc_dev,
1470ceaec73dSDamien Bergamini 			    "unknown authentication state %u\n", auth->state);
1471b032f27cSSam Leffler 			break;
1472ceaec73dSDamien Bergamini 		}
1473ceaec73dSDamien Bergamini 		break;
1474ceaec73dSDamien Bergamini 
1475ceaec73dSDamien Bergamini 	case IWI_NOTIF_TYPE_ASSOCIATION:
1476ceaec73dSDamien Bergamini 		assoc = (struct iwi_notif_association *)(notif + 1);
1477ceaec73dSDamien Bergamini 		switch (assoc->state) {
1478c10b1400SMax Laier 		case IWI_AUTH_SUCCESS:
1479ceaec73dSDamien Bergamini 			/* re-association, do nothing */
1480ceaec73dSDamien Bergamini 			break;
1481c10b1400SMax Laier 		case IWI_ASSOC_SUCCESS:
1482c10b1400SMax Laier 			DPRINTFN(2, ("Association succeeded\n"));
1483c10b1400SMax Laier 			sc->flags |= IWI_FLAG_ASSOCIATED;
148468e8e04eSSam Leffler 			IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1485b032f27cSSam Leffler 			iwi_checkforqos(vap,
1486c10b1400SMax Laier 			    (const struct ieee80211_frame *)(assoc+1),
148782510b7eSBernhard Schmidt 			    le16toh(notif->len) - sizeof(*assoc) - 1);
14885efea30fSAndrew Thompson 			ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
1489ceaec73dSDamien Bergamini 			break;
149068e8e04eSSam Leffler 		case IWI_ASSOC_INIT:
1491b032f27cSSam Leffler 			sc->flags &= ~IWI_FLAG_ASSOCIATED;
149268e8e04eSSam Leffler 			switch (sc->fw_state) {
149368e8e04eSSam Leffler 			case IWI_FW_ASSOCIATING:
1494c10b1400SMax Laier 				DPRINTFN(2, ("Association failed\n"));
149568e8e04eSSam Leffler 				IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
14965efea30fSAndrew Thompson 				ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
149768e8e04eSSam Leffler 				break;
149868e8e04eSSam Leffler 
149968e8e04eSSam Leffler 			case IWI_FW_DISASSOCIATING:
150068e8e04eSSam Leffler 				DPRINTFN(2, ("Dissassociated\n"));
1501b032f27cSSam Leffler 				IWI_STATE_END(sc, IWI_FW_DISASSOCIATING);
1502b032f27cSSam Leffler 				vap->iv_stats.is_rx_disassoc++;
15035efea30fSAndrew Thompson 				ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
150468e8e04eSSam Leffler 				break;
150568e8e04eSSam Leffler 			}
1506ceaec73dSDamien Bergamini 			break;
1507ceaec73dSDamien Bergamini 		default:
1508ceaec73dSDamien Bergamini 			device_printf(sc->sc_dev,
1509ceaec73dSDamien Bergamini 			    "unknown association state %u\n", assoc->state);
1510b032f27cSSam Leffler 			break;
1511ceaec73dSDamien Bergamini 		}
1512ceaec73dSDamien Bergamini 		break;
1513ceaec73dSDamien Bergamini 
1514c10b1400SMax Laier 	case IWI_NOTIF_TYPE_BEACON:
1515c10b1400SMax Laier 		/* XXX check struct length */
1516c10b1400SMax Laier 		beacon = (struct iwi_notif_beacon_state *)(notif + 1);
1517c10b1400SMax Laier 
1518c10b1400SMax Laier 		DPRINTFN(5, ("Beacon state (%u, %u)\n",
1519c10b1400SMax Laier 		    beacon->state, le32toh(beacon->number)));
1520c10b1400SMax Laier 
1521c10b1400SMax Laier 		if (beacon->state == IWI_BEACON_MISS) {
1522c10b1400SMax Laier 			/*
1523c10b1400SMax Laier 			 * The firmware notifies us of every beacon miss
1524c10b1400SMax Laier 			 * so we need to track the count against the
1525c10b1400SMax Laier 			 * configured threshold before notifying the
1526c10b1400SMax Laier 			 * 802.11 layer.
1527c10b1400SMax Laier 			 * XXX try to roam, drop assoc only on much higher count
1528c10b1400SMax Laier 			 */
1529b032f27cSSam Leffler 			if (le32toh(beacon->number) >= vap->iv_bmissthreshold) {
1530c10b1400SMax Laier 				DPRINTF(("Beacon miss: %u >= %u\n",
1531c10b1400SMax Laier 				    le32toh(beacon->number),
1532b032f27cSSam Leffler 				    vap->iv_bmissthreshold));
1533b032f27cSSam Leffler 				vap->iv_stats.is_beacon_miss++;
1534b032f27cSSam Leffler 				/*
1535b032f27cSSam Leffler 				 * It's pointless to notify the 802.11 layer
1536b032f27cSSam Leffler 				 * as it'll try to send a probe request (which
1537b032f27cSSam Leffler 				 * we'll discard) and then timeout and drop us
1538b032f27cSSam Leffler 				 * into scan state.  Instead tell the firmware
1539b032f27cSSam Leffler 				 * to disassociate and then on completion we'll
1540b032f27cSSam Leffler 				 * kick the state machine to scan.
1541b032f27cSSam Leffler 				 */
15425efea30fSAndrew Thompson 				ieee80211_runtask(ic, &sc->sc_disassoctask);
1543c10b1400SMax Laier 			}
1544c10b1400SMax Laier 		}
1545c10b1400SMax Laier 		break;
1546c10b1400SMax Laier 
1547c10b1400SMax Laier 	case IWI_NOTIF_TYPE_CALIBRATION:
1548c10b1400SMax Laier 	case IWI_NOTIF_TYPE_NOISE:
15494982d693SAdrian Chadd 		/* XXX handle? */
155084f55b29SDamien Bergamini 		DPRINTFN(5, ("Notification (%u)\n", notif->type));
1551c10b1400SMax Laier 		break;
15524982d693SAdrian Chadd 	case IWI_NOTIF_TYPE_LINK_QUALITY:
15534982d693SAdrian Chadd 		iwi_notif_link_quality(sc, notif);
15544982d693SAdrian Chadd 		break;
1555c10b1400SMax Laier 
1556c10b1400SMax Laier 	default:
1557c10b1400SMax Laier 		DPRINTF(("unknown notification type %u flags 0x%x len %u\n",
1558c10b1400SMax Laier 		    notif->type, notif->flags, le16toh(notif->len)));
1559b032f27cSSam Leffler 		break;
1560ceaec73dSDamien Bergamini 	}
1561ceaec73dSDamien Bergamini }
1562ceaec73dSDamien Bergamini 
1563ceaec73dSDamien Bergamini static void
iwi_rx_intr(struct iwi_softc * sc)1564ceaec73dSDamien Bergamini iwi_rx_intr(struct iwi_softc *sc)
1565ceaec73dSDamien Bergamini {
1566ceaec73dSDamien Bergamini 	struct iwi_rx_data *data;
1567ceaec73dSDamien Bergamini 	struct iwi_hdr *hdr;
1568ceaec73dSDamien Bergamini 	uint32_t hw;
1569ceaec73dSDamien Bergamini 
1570ceaec73dSDamien Bergamini 	hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1571ceaec73dSDamien Bergamini 
1572ceaec73dSDamien Bergamini 	for (; sc->rxq.cur != hw;) {
1573ceaec73dSDamien Bergamini 		data = &sc->rxq.data[sc->rxq.cur];
1574ceaec73dSDamien Bergamini 
1575ceaec73dSDamien Bergamini 		bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1576ceaec73dSDamien Bergamini 		    BUS_DMASYNC_POSTREAD);
1577ceaec73dSDamien Bergamini 
1578ceaec73dSDamien Bergamini 		hdr = mtod(data->m, struct iwi_hdr *);
1579ceaec73dSDamien Bergamini 
1580ceaec73dSDamien Bergamini 		switch (hdr->type) {
1581ceaec73dSDamien Bergamini 		case IWI_HDR_TYPE_FRAME:
1582ceaec73dSDamien Bergamini 			iwi_frame_intr(sc, data, sc->rxq.cur,
1583ceaec73dSDamien Bergamini 			    (struct iwi_frame *)(hdr + 1));
1584ceaec73dSDamien Bergamini 			break;
1585ceaec73dSDamien Bergamini 
1586ceaec73dSDamien Bergamini 		case IWI_HDR_TYPE_NOTIF:
1587ceaec73dSDamien Bergamini 			iwi_notification_intr(sc,
1588ceaec73dSDamien Bergamini 			    (struct iwi_notif *)(hdr + 1));
1589ceaec73dSDamien Bergamini 			break;
1590ceaec73dSDamien Bergamini 
1591ceaec73dSDamien Bergamini 		default:
1592ceaec73dSDamien Bergamini 			device_printf(sc->sc_dev, "unknown hdr type %u\n",
1593ceaec73dSDamien Bergamini 			    hdr->type);
1594ceaec73dSDamien Bergamini 		}
1595ceaec73dSDamien Bergamini 
1596ceaec73dSDamien Bergamini 		DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1597ceaec73dSDamien Bergamini 
1598ceaec73dSDamien Bergamini 		sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1599ceaec73dSDamien Bergamini 	}
1600ceaec73dSDamien Bergamini 
1601ceaec73dSDamien Bergamini 	/* tell the firmware what we have processed */
1602ceaec73dSDamien Bergamini 	hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1603ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1604ceaec73dSDamien Bergamini }
1605ceaec73dSDamien Bergamini 
1606ceaec73dSDamien Bergamini static void
iwi_tx_intr(struct iwi_softc * sc,struct iwi_tx_ring * txq)1607ec01dc2bSDamien Bergamini iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1608ceaec73dSDamien Bergamini {
1609ceaec73dSDamien Bergamini 	struct iwi_tx_data *data;
1610ceaec73dSDamien Bergamini 	uint32_t hw;
1611ceaec73dSDamien Bergamini 
1612ec01dc2bSDamien Bergamini 	hw = CSR_READ_4(sc, txq->csr_ridx);
1613ceaec73dSDamien Bergamini 
16147a79cebfSGleb Smirnoff 	while (txq->next != hw) {
1615ec01dc2bSDamien Bergamini 		data = &txq->data[txq->next];
16167a79cebfSGleb Smirnoff 		DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1617ec01dc2bSDamien Bergamini 		bus_dmamap_sync(txq->data_dmat, data->map,
1618ceaec73dSDamien Bergamini 		    BUS_DMASYNC_POSTWRITE);
1619ec01dc2bSDamien Bergamini 		bus_dmamap_unload(txq->data_dmat, data->map);
16207a79cebfSGleb Smirnoff 		ieee80211_tx_complete(data->ni, data->m, 0);
1621ba2c1fbcSAdrian Chadd 		data->ni = NULL;
16227a79cebfSGleb Smirnoff 		data->m = NULL;
1623ec01dc2bSDamien Bergamini 		txq->queued--;
1624ec01dc2bSDamien Bergamini 		txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1625ceaec73dSDamien Bergamini 	}
1626ceaec73dSDamien Bergamini 	sc->sc_tx_timer = 0;
1627c10b1400SMax Laier 	if (sc->sc_softled)
1628c10b1400SMax Laier 		iwi_led_event(sc, IWI_LED_TX);
16297a79cebfSGleb Smirnoff 	iwi_start(sc);
1630ceaec73dSDamien Bergamini }
1631ceaec73dSDamien Bergamini 
1632ceaec73dSDamien Bergamini static void
iwi_fatal_error_intr(struct iwi_softc * sc)16335efea30fSAndrew Thompson iwi_fatal_error_intr(struct iwi_softc *sc)
16345efea30fSAndrew Thompson {
16357a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1636e1d2045eSAndrew Thompson 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
16375efea30fSAndrew Thompson 
16385efea30fSAndrew Thompson 	device_printf(sc->sc_dev, "firmware error\n");
1639e1d2045eSAndrew Thompson 	if (vap != NULL)
1640e1d2045eSAndrew Thompson 		ieee80211_cancel_scan(vap);
16415efea30fSAndrew Thompson 	ieee80211_runtask(ic, &sc->sc_restarttask);
16425efea30fSAndrew Thompson 
16435efea30fSAndrew Thompson 	sc->flags &= ~IWI_FLAG_BUSY;
16445efea30fSAndrew Thompson 	sc->sc_busy_timer = 0;
16455efea30fSAndrew Thompson 	wakeup(sc);
16465efea30fSAndrew Thompson }
16475efea30fSAndrew Thompson 
16485efea30fSAndrew Thompson static void
iwi_radio_off_intr(struct iwi_softc * sc)16495efea30fSAndrew Thompson iwi_radio_off_intr(struct iwi_softc *sc)
16505efea30fSAndrew Thompson {
16515efea30fSAndrew Thompson 
16527a79cebfSGleb Smirnoff 	ieee80211_runtask(&sc->sc_ic, &sc->sc_radiofftask);
16535efea30fSAndrew Thompson }
16545efea30fSAndrew Thompson 
16555efea30fSAndrew Thompson static void
iwi_intr(void * arg)1656ceaec73dSDamien Bergamini iwi_intr(void *arg)
1657ceaec73dSDamien Bergamini {
1658ceaec73dSDamien Bergamini 	struct iwi_softc *sc = arg;
1659ceaec73dSDamien Bergamini 	uint32_t r;
1660c10b1400SMax Laier 	IWI_LOCK_DECL;
1661ceaec73dSDamien Bergamini 
1662c10b1400SMax Laier 	IWI_LOCK(sc);
1663ceaec73dSDamien Bergamini 
1664ceaec73dSDamien Bergamini 	if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) {
1665c10b1400SMax Laier 		IWI_UNLOCK(sc);
1666ceaec73dSDamien Bergamini 		return;
1667ceaec73dSDamien Bergamini 	}
1668ceaec73dSDamien Bergamini 
1669389fb0b4SDamien Bergamini 	/* acknowledge interrupts */
1670389fb0b4SDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1671ceaec73dSDamien Bergamini 
1672c10b1400SMax Laier 	if (r & IWI_INTR_FATAL_ERROR) {
16735efea30fSAndrew Thompson 		iwi_fatal_error_intr(sc);
16745efea30fSAndrew Thompson 		goto done;
1675ceaec73dSDamien Bergamini 	}
1676ceaec73dSDamien Bergamini 
1677c10b1400SMax Laier 	if (r & IWI_INTR_FW_INITED) {
1678c10b1400SMax Laier 		if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1679ceaec73dSDamien Bergamini 			wakeup(sc);
1680ceaec73dSDamien Bergamini 	}
1681ceaec73dSDamien Bergamini 
1682c10b1400SMax Laier 	if (r & IWI_INTR_RADIO_OFF)
16835efea30fSAndrew Thompson 		iwi_radio_off_intr(sc);
1684c10b1400SMax Laier 
1685c10b1400SMax Laier 	if (r & IWI_INTR_CMD_DONE) {
1686c10b1400SMax Laier 		sc->flags &= ~IWI_FLAG_BUSY;
168768e8e04eSSam Leffler 		sc->sc_busy_timer = 0;
1688ceaec73dSDamien Bergamini 		wakeup(sc);
1689c10b1400SMax Laier 	}
1690ceaec73dSDamien Bergamini 
1691ceaec73dSDamien Bergamini 	if (r & IWI_INTR_TX1_DONE)
1692ec01dc2bSDamien Bergamini 		iwi_tx_intr(sc, &sc->txq[0]);
1693ec01dc2bSDamien Bergamini 
1694ec01dc2bSDamien Bergamini 	if (r & IWI_INTR_TX2_DONE)
1695ec01dc2bSDamien Bergamini 		iwi_tx_intr(sc, &sc->txq[1]);
1696ec01dc2bSDamien Bergamini 
1697ec01dc2bSDamien Bergamini 	if (r & IWI_INTR_TX3_DONE)
1698ec01dc2bSDamien Bergamini 		iwi_tx_intr(sc, &sc->txq[2]);
1699ec01dc2bSDamien Bergamini 
1700ec01dc2bSDamien Bergamini 	if (r & IWI_INTR_TX4_DONE)
1701ec01dc2bSDamien Bergamini 		iwi_tx_intr(sc, &sc->txq[3]);
1702ec01dc2bSDamien Bergamini 
1703ec01dc2bSDamien Bergamini 	if (r & IWI_INTR_RX_DONE)
1704ec01dc2bSDamien Bergamini 		iwi_rx_intr(sc);
1705ceaec73dSDamien Bergamini 
1706c10b1400SMax Laier 	if (r & IWI_INTR_PARITY_ERROR) {
1707c10b1400SMax Laier 		/* XXX rate-limit */
1708c10b1400SMax Laier 		device_printf(sc->sc_dev, "parity error\n");
1709c10b1400SMax Laier 	}
17105efea30fSAndrew Thompson done:
1711c10b1400SMax Laier 	IWI_UNLOCK(sc);
1712ceaec73dSDamien Bergamini }
1713ceaec73dSDamien Bergamini 
1714ceaec73dSDamien Bergamini static int
iwi_cmd(struct iwi_softc * sc,uint8_t type,void * data,uint8_t len)1715c10b1400SMax Laier iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len)
1716ceaec73dSDamien Bergamini {
1717ceaec73dSDamien Bergamini 	struct iwi_cmd_desc *desc;
1718ceaec73dSDamien Bergamini 
171968e8e04eSSam Leffler 	IWI_LOCK_ASSERT(sc);
1720484f6530SLuigi Rizzo 
1721c10b1400SMax Laier 	if (sc->flags & IWI_FLAG_BUSY) {
1722c10b1400SMax Laier 		device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n",
1723c10b1400SMax Laier 			__func__, type);
1724c10b1400SMax Laier 		return EAGAIN;
1725c10b1400SMax Laier 	}
1726c10b1400SMax Laier 	sc->flags |= IWI_FLAG_BUSY;
172768e8e04eSSam Leffler 	sc->sc_busy_timer = 2;
1728c10b1400SMax Laier 
1729ceaec73dSDamien Bergamini 	desc = &sc->cmdq.desc[sc->cmdq.cur];
1730ceaec73dSDamien Bergamini 
1731ceaec73dSDamien Bergamini 	desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1732ceaec73dSDamien Bergamini 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1733ceaec73dSDamien Bergamini 	desc->type = type;
1734ceaec73dSDamien Bergamini 	desc->len = len;
1735ceaec73dSDamien Bergamini 	memcpy(desc->data, data, len);
1736ceaec73dSDamien Bergamini 
1737ceaec73dSDamien Bergamini 	bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map,
1738ceaec73dSDamien Bergamini 	    BUS_DMASYNC_PREWRITE);
1739ceaec73dSDamien Bergamini 
1740ceaec73dSDamien Bergamini 	DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1741ceaec73dSDamien Bergamini 	    type, len));
1742ceaec73dSDamien Bergamini 
1743ceaec73dSDamien Bergamini 	sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1744ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1745ceaec73dSDamien Bergamini 
1746c10b1400SMax Laier 	return msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz);
1747ceaec73dSDamien Bergamini }
1748ceaec73dSDamien Bergamini 
174928a38073SDamien Bergamini static void
iwi_write_ibssnode(struct iwi_softc * sc,const u_int8_t addr[IEEE80211_ADDR_LEN],int entry)1750c10b1400SMax Laier iwi_write_ibssnode(struct iwi_softc *sc,
1751c10b1400SMax Laier 	const u_int8_t addr[IEEE80211_ADDR_LEN], int entry)
175228a38073SDamien Bergamini {
175328a38073SDamien Bergamini 	struct iwi_ibssnode node;
175428a38073SDamien Bergamini 
175528a38073SDamien Bergamini 	/* write node information into NIC memory */
175628a38073SDamien Bergamini 	memset(&node, 0, sizeof node);
1757c10b1400SMax Laier 	IEEE80211_ADDR_COPY(node.bssid, addr);
1758c10b1400SMax Laier 
1759c10b1400SMax Laier 	DPRINTF(("%s mac %6D station %u\n", __func__, node.bssid, ":", entry));
176028a38073SDamien Bergamini 
176128a38073SDamien Bergamini 	CSR_WRITE_REGION_1(sc,
1762c10b1400SMax Laier 	    IWI_CSR_NODE_BASE + entry * sizeof node,
176328a38073SDamien Bergamini 	    (uint8_t *)&node, sizeof node);
176428a38073SDamien Bergamini }
176528a38073SDamien Bergamini 
1766ceaec73dSDamien Bergamini static int
iwi_tx_start(struct iwi_softc * sc,struct mbuf * m0,struct ieee80211_node * ni,int ac)17677a79cebfSGleb Smirnoff iwi_tx_start(struct iwi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
17680b74a285SDamien Bergamini     int ac)
1769ceaec73dSDamien Bergamini {
1770b032f27cSSam Leffler 	struct ieee80211vap *vap = ni->ni_vap;
177128a38073SDamien Bergamini 	struct iwi_node *in = (struct iwi_node *)ni;
1772c10b1400SMax Laier 	const struct ieee80211_frame *wh;
1773acea4241SDamien Bergamini 	struct ieee80211_key *k;
17740b74a285SDamien Bergamini 	struct iwi_tx_ring *txq = &sc->txq[ac];
1775ceaec73dSDamien Bergamini 	struct iwi_tx_data *data;
1776ceaec73dSDamien Bergamini 	struct iwi_tx_desc *desc;
1777ceaec73dSDamien Bergamini 	struct mbuf *mnew;
1778ceaec73dSDamien Bergamini 	bus_dma_segment_t segs[IWI_MAX_NSEG];
1779c10b1400SMax Laier 	int error, nsegs, hdrlen, i;
1780c10b1400SMax Laier 	int ismcast, flags, xflags, staid;
1781ceaec73dSDamien Bergamini 
178268e8e04eSSam Leffler 	IWI_LOCK_ASSERT(sc);
1783c10b1400SMax Laier 	wh = mtod(m0, const struct ieee80211_frame *);
1784c10b1400SMax Laier 	/* NB: only data frames use this path */
1785c10b1400SMax Laier 	hdrlen = ieee80211_hdrsize(wh);
1786c10b1400SMax Laier 	ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1787c10b1400SMax Laier 	flags = xflags = 0;
1788ec01dc2bSDamien Bergamini 
1789c10b1400SMax Laier 	if (!ismcast)
1790c10b1400SMax Laier 		flags |= IWI_DATA_FLAG_NEED_ACK;
1791b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_SHPREAMBLE)
1792c10b1400SMax Laier 		flags |= IWI_DATA_FLAG_SHPREAMBLE;
1793c10b1400SMax Laier 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
1794c10b1400SMax Laier 		xflags |= IWI_DATA_XFLAG_QOS;
17959fbe631aSAdrian Chadd 		if (ieee80211_wme_vap_ac_is_noack(vap, ac))
1796c10b1400SMax Laier 			flags &= ~IWI_DATA_FLAG_NEED_ACK;
1797c10b1400SMax Laier 	}
1798ec01dc2bSDamien Bergamini 
179928a38073SDamien Bergamini 	/*
180028a38073SDamien Bergamini 	 * This is only used in IBSS mode where the firmware expect an index
180128a38073SDamien Bergamini 	 * in a h/w table instead of a destination address.
180228a38073SDamien Bergamini 	 */
1803b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS) {
1804c10b1400SMax Laier 		if (!ismcast) {
1805c10b1400SMax Laier 			if (in->in_station == -1) {
180628a38073SDamien Bergamini 				in->in_station = alloc_unr(sc->sc_unr);
1807c10b1400SMax Laier 				if (in->in_station == -1) {
1808c10b1400SMax Laier 					/* h/w table is full */
18097a79cebfSGleb Smirnoff 					if_inc_counter(ni->ni_vap->iv_ifp,
18107a79cebfSGleb Smirnoff 					    IFCOUNTER_OERRORS, 1);
1811ec01dc2bSDamien Bergamini 					m_freem(m0);
18120b74a285SDamien Bergamini 					ieee80211_free_node(ni);
1813ec01dc2bSDamien Bergamini 					return 0;
1814ec01dc2bSDamien Bergamini 				}
1815c10b1400SMax Laier 				iwi_write_ibssnode(sc,
1816c10b1400SMax Laier 					ni->ni_macaddr, in->in_station);
18170b74a285SDamien Bergamini 			}
1818c10b1400SMax Laier 			staid = in->in_station;
1819c10b1400SMax Laier 		} else {
1820c10b1400SMax Laier 			/*
1821c10b1400SMax Laier 			 * Multicast addresses have no associated node
1822c10b1400SMax Laier 			 * so there will be no station entry.  We reserve
1823c10b1400SMax Laier 			 * entry 0 for one mcast address and use that.
1824c10b1400SMax Laier 			 * If there are many being used this will be
1825c10b1400SMax Laier 			 * expensive and we'll need to do a better job
1826c10b1400SMax Laier 			 * but for now this handles the broadcast case.
1827c10b1400SMax Laier 			 */
1828c10b1400SMax Laier 			if (!IEEE80211_ADDR_EQ(wh->i_addr1, sc->sc_mcast)) {
1829c10b1400SMax Laier 				IEEE80211_ADDR_COPY(sc->sc_mcast, wh->i_addr1);
1830c10b1400SMax Laier 				iwi_write_ibssnode(sc, sc->sc_mcast, 0);
1831c10b1400SMax Laier 			}
1832c10b1400SMax Laier 			staid = 0;
1833c10b1400SMax Laier 		}
1834c10b1400SMax Laier 	} else
1835c10b1400SMax Laier 		staid = 0;
1836ec01dc2bSDamien Bergamini 
18375945b5f5SKevin Lo 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1838b032f27cSSam Leffler 		k = ieee80211_crypto_encap(ni, m0);
183982145663SSam Leffler 		if (k == NULL) {
184082145663SSam Leffler 			m_freem(m0);
1841acea4241SDamien Bergamini 			return ENOBUFS;
1842acea4241SDamien Bergamini 		}
1843ec01dc2bSDamien Bergamini 
1844ec01dc2bSDamien Bergamini 		/* packet header may have moved, reset our local pointer */
1845ec01dc2bSDamien Bergamini 		wh = mtod(m0, struct ieee80211_frame *);
184682145663SSam Leffler 	}
1847acea4241SDamien Bergamini 
18485463c4a4SSam Leffler 	if (ieee80211_radiotap_active_vap(vap)) {
1849ceaec73dSDamien Bergamini 		struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1850ceaec73dSDamien Bergamini 
1851ceaec73dSDamien Bergamini 		tap->wt_flags = 0;
1852ceaec73dSDamien Bergamini 
18535463c4a4SSam Leffler 		ieee80211_radiotap_tx(vap, m0);
1854ceaec73dSDamien Bergamini 	}
1855ceaec73dSDamien Bergamini 
1856ec01dc2bSDamien Bergamini 	data = &txq->data[txq->cur];
1857ec01dc2bSDamien Bergamini 	desc = &txq->desc[txq->cur];
1858ceaec73dSDamien Bergamini 
1859ec01dc2bSDamien Bergamini 	/* save and trim IEEE802.11 header */
1860ec01dc2bSDamien Bergamini 	m_copydata(m0, 0, hdrlen, (caddr_t)&desc->wh);
1861ec01dc2bSDamien Bergamini 	m_adj(m0, hdrlen);
1862ceaec73dSDamien Bergamini 
1863ec01dc2bSDamien Bergamini 	error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0, segs,
1864ceaec73dSDamien Bergamini 	    &nsegs, 0);
1865ceaec73dSDamien Bergamini 	if (error != 0 && error != EFBIG) {
1866ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1867ceaec73dSDamien Bergamini 		    error);
1868ceaec73dSDamien Bergamini 		m_freem(m0);
1869ceaec73dSDamien Bergamini 		return error;
1870ceaec73dSDamien Bergamini 	}
1871ceaec73dSDamien Bergamini 	if (error != 0) {
1872c6499eccSGleb Smirnoff 		mnew = m_defrag(m0, M_NOWAIT);
1873ceaec73dSDamien Bergamini 		if (mnew == NULL) {
1874ceaec73dSDamien Bergamini 			device_printf(sc->sc_dev,
1875ceaec73dSDamien Bergamini 			    "could not defragment mbuf\n");
1876ceaec73dSDamien Bergamini 			m_freem(m0);
1877ceaec73dSDamien Bergamini 			return ENOBUFS;
1878ceaec73dSDamien Bergamini 		}
1879ceaec73dSDamien Bergamini 		m0 = mnew;
1880ceaec73dSDamien Bergamini 
1881ec01dc2bSDamien Bergamini 		error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map,
1882ceaec73dSDamien Bergamini 		    m0, segs, &nsegs, 0);
1883ceaec73dSDamien Bergamini 		if (error != 0) {
1884ceaec73dSDamien Bergamini 			device_printf(sc->sc_dev,
1885ceaec73dSDamien Bergamini 			    "could not map mbuf (error %d)\n", error);
1886ceaec73dSDamien Bergamini 			m_freem(m0);
1887ceaec73dSDamien Bergamini 			return error;
1888ceaec73dSDamien Bergamini 		}
1889ceaec73dSDamien Bergamini 	}
1890ceaec73dSDamien Bergamini 
1891ceaec73dSDamien Bergamini 	data->m = m0;
1892ceaec73dSDamien Bergamini 	data->ni = ni;
1893ceaec73dSDamien Bergamini 
1894ceaec73dSDamien Bergamini 	desc->hdr.type = IWI_HDR_TYPE_DATA;
1895ceaec73dSDamien Bergamini 	desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1896c10b1400SMax Laier 	desc->station = staid;
1897ceaec73dSDamien Bergamini 	desc->cmd = IWI_DATA_CMD_TX;
1898ceaec73dSDamien Bergamini 	desc->len = htole16(m0->m_pkthdr.len);
1899c10b1400SMax Laier 	desc->flags = flags;
1900c10b1400SMax Laier 	desc->xflags = xflags;
1901ceaec73dSDamien Bergamini 
1902acea4241SDamien Bergamini #if 0
1903b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
1904b032f27cSSam Leffler 		desc->wep_txkey = vap->iv_def_txkey;
1905c10b1400SMax Laier 	else
1906acea4241SDamien Bergamini #endif
1907ceaec73dSDamien Bergamini 		desc->flags |= IWI_DATA_FLAG_NO_WEP;
1908ceaec73dSDamien Bergamini 
1909ceaec73dSDamien Bergamini 	desc->nseg = htole32(nsegs);
1910ceaec73dSDamien Bergamini 	for (i = 0; i < nsegs; i++) {
1911ceaec73dSDamien Bergamini 		desc->seg_addr[i] = htole32(segs[i].ds_addr);
191281e6343cSDamien Bergamini 		desc->seg_len[i]  = htole16(segs[i].ds_len);
1913ceaec73dSDamien Bergamini 	}
1914ceaec73dSDamien Bergamini 
1915ec01dc2bSDamien Bergamini 	bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1916ec01dc2bSDamien Bergamini 	bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1917ceaec73dSDamien Bergamini 
1918ec01dc2bSDamien Bergamini 	DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
191981e6343cSDamien Bergamini 	    ac, txq->cur, le16toh(desc->len), nsegs));
1920ceaec73dSDamien Bergamini 
1921ec01dc2bSDamien Bergamini 	txq->queued++;
1922ec01dc2bSDamien Bergamini 	txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
1923ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1924ceaec73dSDamien Bergamini 
1925ceaec73dSDamien Bergamini 	return 0;
1926ceaec73dSDamien Bergamini }
1927ceaec73dSDamien Bergamini 
1928b032f27cSSam Leffler static int
iwi_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)1929b032f27cSSam Leffler iwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1930b032f27cSSam Leffler 	const struct ieee80211_bpf_params *params)
1931b032f27cSSam Leffler {
1932b032f27cSSam Leffler 	/* no support; just discard */
1933b032f27cSSam Leffler 	m_freem(m);
1934b032f27cSSam Leffler 	ieee80211_free_node(ni);
1935b032f27cSSam Leffler 	return 0;
1936b032f27cSSam Leffler }
1937b032f27cSSam Leffler 
19387a79cebfSGleb Smirnoff static int
iwi_transmit(struct ieee80211com * ic,struct mbuf * m)19397a79cebfSGleb Smirnoff iwi_transmit(struct ieee80211com *ic, struct mbuf *m)
194079d2c5e8SGleb Smirnoff {
19417a79cebfSGleb Smirnoff 	struct iwi_softc *sc = ic->ic_softc;
19427a79cebfSGleb Smirnoff 	int error;
19437a79cebfSGleb Smirnoff 	IWI_LOCK_DECL;
19447a79cebfSGleb Smirnoff 
19457a79cebfSGleb Smirnoff 	IWI_LOCK(sc);
19467a79cebfSGleb Smirnoff 	if (!sc->sc_running) {
19477a79cebfSGleb Smirnoff 		IWI_UNLOCK(sc);
19487a79cebfSGleb Smirnoff 		return (ENXIO);
19497a79cebfSGleb Smirnoff 	}
19507a79cebfSGleb Smirnoff 	error = mbufq_enqueue(&sc->sc_snd, m);
19517a79cebfSGleb Smirnoff 	if (error) {
19527a79cebfSGleb Smirnoff 		IWI_UNLOCK(sc);
19537a79cebfSGleb Smirnoff 		return (error);
19547a79cebfSGleb Smirnoff 	}
19557a79cebfSGleb Smirnoff 	iwi_start(sc);
19567a79cebfSGleb Smirnoff 	IWI_UNLOCK(sc);
19577a79cebfSGleb Smirnoff 	return (0);
19587a79cebfSGleb Smirnoff }
19597a79cebfSGleb Smirnoff 
19607a79cebfSGleb Smirnoff static void
iwi_start(struct iwi_softc * sc)19617a79cebfSGleb Smirnoff iwi_start(struct iwi_softc *sc)
19627a79cebfSGleb Smirnoff {
1963b032f27cSSam Leffler 	struct mbuf *m;
1964ceaec73dSDamien Bergamini 	struct ieee80211_node *ni;
19650b74a285SDamien Bergamini 	int ac;
1966ceaec73dSDamien Bergamini 
1967b032f27cSSam Leffler 	IWI_LOCK_ASSERT(sc);
1968ceaec73dSDamien Bergamini 
19697a79cebfSGleb Smirnoff 	while ((m =  mbufq_dequeue(&sc->sc_snd)) != NULL) {
1970b032f27cSSam Leffler 		ac = M_WME_GETAC(m);
19710b74a285SDamien Bergamini 		if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) {
1972b032f27cSSam Leffler 			/* there is no place left in this ring; tail drop */
1973b032f27cSSam Leffler 			/* XXX tail drop */
19747a79cebfSGleb Smirnoff 			mbufq_prepend(&sc->sc_snd, m);
19750b74a285SDamien Bergamini 			break;
19760b74a285SDamien Bergamini 		}
1977b032f27cSSam Leffler 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
19787a79cebfSGleb Smirnoff 		if (iwi_tx_start(sc, m, ni, ac) != 0) {
19797a79cebfSGleb Smirnoff 			if_inc_counter(ni->ni_vap->iv_ifp,
19807a79cebfSGleb Smirnoff 			    IFCOUNTER_OERRORS, 1);
1981ce017db1SAndriy Voskoboinyk 			ieee80211_free_node(ni);
1982ceaec73dSDamien Bergamini 			break;
1983ceaec73dSDamien Bergamini 		}
1984ceaec73dSDamien Bergamini 		sc->sc_tx_timer = 5;
1985b032f27cSSam Leffler 	}
1986ceaec73dSDamien Bergamini }
1987ceaec73dSDamien Bergamini 
1988b032f27cSSam Leffler static void
iwi_watchdog(void * arg)198968e8e04eSSam Leffler iwi_watchdog(void *arg)
1990ceaec73dSDamien Bergamini {
199168e8e04eSSam Leffler 	struct iwi_softc *sc = arg;
19927a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
1993ceaec73dSDamien Bergamini 
199468e8e04eSSam Leffler 	IWI_LOCK_ASSERT(sc);
1995ceaec73dSDamien Bergamini 
1996ceaec73dSDamien Bergamini 	if (sc->sc_tx_timer > 0) {
1997ceaec73dSDamien Bergamini 		if (--sc->sc_tx_timer == 0) {
19987a79cebfSGleb Smirnoff 			device_printf(sc->sc_dev, "device timeout\n");
19997a79cebfSGleb Smirnoff 			counter_u64_add(ic->ic_oerrors, 1);
20005efea30fSAndrew Thompson 			ieee80211_runtask(ic, &sc->sc_restarttask);
2001ceaec73dSDamien Bergamini 		}
2002c10b1400SMax Laier 	}
200368e8e04eSSam Leffler 	if (sc->sc_state_timer > 0) {
200468e8e04eSSam Leffler 		if (--sc->sc_state_timer == 0) {
20057a79cebfSGleb Smirnoff 			device_printf(sc->sc_dev,
20067a79cebfSGleb Smirnoff 			    "firmware stuck in state %d, resetting\n",
200768e8e04eSSam Leffler 			    sc->fw_state);
20087a79cebfSGleb Smirnoff 			if (sc->fw_state == IWI_FW_SCANNING)
2009b032f27cSSam Leffler 				ieee80211_cancel_scan(TAILQ_FIRST(&ic->ic_vaps));
20105efea30fSAndrew Thompson 			ieee80211_runtask(ic, &sc->sc_restarttask);
201168e8e04eSSam Leffler 			sc->sc_state_timer = 3;
201268e8e04eSSam Leffler 		}
201368e8e04eSSam Leffler 	}
201468e8e04eSSam Leffler 	if (sc->sc_busy_timer > 0) {
201568e8e04eSSam Leffler 		if (--sc->sc_busy_timer == 0) {
20167a79cebfSGleb Smirnoff 			device_printf(sc->sc_dev,
20177a79cebfSGleb Smirnoff 			    "firmware command timeout, resetting\n");
20185efea30fSAndrew Thompson 			ieee80211_runtask(ic, &sc->sc_restarttask);
2019c10b1400SMax Laier 		}
2020c10b1400SMax Laier 	}
202168e8e04eSSam Leffler 	callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
2022ceaec73dSDamien Bergamini }
2023ceaec73dSDamien Bergamini 
20247a79cebfSGleb Smirnoff static void
iwi_parent(struct ieee80211com * ic)20257a79cebfSGleb Smirnoff iwi_parent(struct ieee80211com *ic)
2026ceaec73dSDamien Bergamini {
20277a79cebfSGleb Smirnoff 	struct iwi_softc *sc = ic->ic_softc;
20287a79cebfSGleb Smirnoff 	int startall = 0;
2029c10b1400SMax Laier 	IWI_LOCK_DECL;
2030ceaec73dSDamien Bergamini 
203131a8c1edSAndrew Thompson 	IWI_LOCK(sc);
20327a79cebfSGleb Smirnoff 	if (ic->ic_nrunning > 0) {
20337a79cebfSGleb Smirnoff 		if (!sc->sc_running) {
2034b032f27cSSam Leffler 			iwi_init_locked(sc);
2035b032f27cSSam Leffler 			startall = 1;
2036b032f27cSSam Leffler 		}
20377a79cebfSGleb Smirnoff 	} else if (sc->sc_running)
2038b032f27cSSam Leffler 		iwi_stop_locked(sc);
2039c10b1400SMax Laier 	IWI_UNLOCK(sc);
2040b032f27cSSam Leffler 	if (startall)
2041b032f27cSSam Leffler 		ieee80211_start_all(ic);
20427a79cebfSGleb Smirnoff }
20437a79cebfSGleb Smirnoff 
20447a79cebfSGleb Smirnoff static int
iwi_ioctl(struct ieee80211com * ic,u_long cmd,void * data)20457a79cebfSGleb Smirnoff iwi_ioctl(struct ieee80211com *ic, u_long cmd, void *data)
20467a79cebfSGleb Smirnoff {
20477a79cebfSGleb Smirnoff 	struct ifreq *ifr = data;
20487a79cebfSGleb Smirnoff 	struct iwi_softc *sc = ic->ic_softc;
20497a79cebfSGleb Smirnoff 	int error;
20507a79cebfSGleb Smirnoff 	IWI_LOCK_DECL;
20517a79cebfSGleb Smirnoff 
2052ba2c1fbcSAdrian Chadd 	IWI_LOCK(sc);
20537a79cebfSGleb Smirnoff 	switch (cmd) {
20547a79cebfSGleb Smirnoff 	case SIOCGIWISTATS:
20554982d693SAdrian Chadd 		/* XXX validate permissions/memory/etc? */
2056541d96aaSBrooks Davis 		error = copyout(&sc->sc_linkqual, ifr_data_get_ptr(ifr),
20574982d693SAdrian Chadd 		    sizeof(struct iwi_notif_link_quality));
20584982d693SAdrian Chadd 		break;
20594982d693SAdrian Chadd 	case SIOCZIWISTATS:
20604982d693SAdrian Chadd 		memset(&sc->sc_linkqual, 0,
20614982d693SAdrian Chadd 		    sizeof(struct iwi_notif_link_quality));
20624982d693SAdrian Chadd 		error = 0;
20634982d693SAdrian Chadd 		break;
206431a8c1edSAndrew Thompson 	default:
20657a79cebfSGleb Smirnoff 		error = ENOTTY;
206631a8c1edSAndrew Thompson 		break;
206731a8c1edSAndrew Thompson 	}
20687a79cebfSGleb Smirnoff 	IWI_UNLOCK(sc);
20697a79cebfSGleb Smirnoff 
20707a79cebfSGleb Smirnoff 	return (error);
2071ceaec73dSDamien Bergamini }
2072ceaec73dSDamien Bergamini 
2073ceaec73dSDamien Bergamini static void
iwi_stop_master(struct iwi_softc * sc)2074ceaec73dSDamien Bergamini iwi_stop_master(struct iwi_softc *sc)
2075ceaec73dSDamien Bergamini {
2076ceaec73dSDamien Bergamini 	uint32_t tmp;
2077ceaec73dSDamien Bergamini 	int ntries;
2078ceaec73dSDamien Bergamini 
2079ceaec73dSDamien Bergamini 	/* disable interrupts */
2080ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
2081ceaec73dSDamien Bergamini 
2082ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
2083ceaec73dSDamien Bergamini 	for (ntries = 0; ntries < 5; ntries++) {
2084ceaec73dSDamien Bergamini 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2085ceaec73dSDamien Bergamini 			break;
2086ceaec73dSDamien Bergamini 		DELAY(10);
2087ceaec73dSDamien Bergamini 	}
2088ceaec73dSDamien Bergamini 	if (ntries == 5)
2089ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "timeout waiting for master\n");
2090ceaec73dSDamien Bergamini 
2091ceaec73dSDamien Bergamini 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
2092ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
2093ceaec73dSDamien Bergamini 
2094ceaec73dSDamien Bergamini 	sc->flags &= ~IWI_FLAG_FW_INITED;
2095ceaec73dSDamien Bergamini }
2096ceaec73dSDamien Bergamini 
2097ceaec73dSDamien Bergamini static int
iwi_reset(struct iwi_softc * sc)2098ceaec73dSDamien Bergamini iwi_reset(struct iwi_softc *sc)
2099ceaec73dSDamien Bergamini {
2100ceaec73dSDamien Bergamini 	uint32_t tmp;
2101ceaec73dSDamien Bergamini 	int i, ntries;
2102ceaec73dSDamien Bergamini 
2103ceaec73dSDamien Bergamini 	iwi_stop_master(sc);
2104ceaec73dSDamien Bergamini 
2105ceaec73dSDamien Bergamini 	tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2106ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2107ceaec73dSDamien Bergamini 
2108ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
2109ceaec73dSDamien Bergamini 
2110ceaec73dSDamien Bergamini 	/* wait for clock stabilization */
2111ceaec73dSDamien Bergamini 	for (ntries = 0; ntries < 1000; ntries++) {
2112ceaec73dSDamien Bergamini 		if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
2113ceaec73dSDamien Bergamini 			break;
2114ceaec73dSDamien Bergamini 		DELAY(200);
2115ceaec73dSDamien Bergamini 	}
2116ceaec73dSDamien Bergamini 	if (ntries == 1000) {
2117ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev,
2118ceaec73dSDamien Bergamini 		    "timeout waiting for clock stabilization\n");
2119ceaec73dSDamien Bergamini 		return EIO;
2120ceaec73dSDamien Bergamini 	}
2121ceaec73dSDamien Bergamini 
2122ceaec73dSDamien Bergamini 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
2123ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET);
2124ceaec73dSDamien Bergamini 
2125ceaec73dSDamien Bergamini 	DELAY(10);
2126ceaec73dSDamien Bergamini 
2127ceaec73dSDamien Bergamini 	tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2128ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2129ceaec73dSDamien Bergamini 
2130ceaec73dSDamien Bergamini 	/* clear NIC memory */
2131ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
2132ceaec73dSDamien Bergamini 	for (i = 0; i < 0xc000; i++)
2133ceaec73dSDamien Bergamini 		CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2134ceaec73dSDamien Bergamini 
2135ceaec73dSDamien Bergamini 	return 0;
2136ceaec73dSDamien Bergamini }
2137ceaec73dSDamien Bergamini 
2138c10b1400SMax Laier static const struct iwi_firmware_ohdr *
iwi_setup_ofw(struct iwi_softc * sc,struct iwi_fw * fw)2139c10b1400SMax Laier iwi_setup_ofw(struct iwi_softc *sc, struct iwi_fw *fw)
2140ceaec73dSDamien Bergamini {
214133d54970SLuigi Rizzo 	const struct firmware *fp = fw->fp;
2142c10b1400SMax Laier 	const struct iwi_firmware_ohdr *hdr;
2143ceaec73dSDamien Bergamini 
2144c10b1400SMax Laier 	if (fp->datasize < sizeof (struct iwi_firmware_ohdr)) {
2145c10b1400SMax Laier 		device_printf(sc->sc_dev, "image '%s' too small\n", fp->name);
2146c10b1400SMax Laier 		return NULL;
2147c10b1400SMax Laier 	}
2148c10b1400SMax Laier 	hdr = (const struct iwi_firmware_ohdr *)fp->data;
2149c10b1400SMax Laier 	if ((IWI_FW_GET_MAJOR(le32toh(hdr->version)) != IWI_FW_REQ_MAJOR) ||
2150c10b1400SMax Laier 	    (IWI_FW_GET_MINOR(le32toh(hdr->version)) != IWI_FW_REQ_MINOR)) {
2151c10b1400SMax Laier 		device_printf(sc->sc_dev, "version for '%s' %d.%d != %d.%d\n",
2152c10b1400SMax Laier 		    fp->name, IWI_FW_GET_MAJOR(le32toh(hdr->version)),
2153c10b1400SMax Laier 		    IWI_FW_GET_MINOR(le32toh(hdr->version)), IWI_FW_REQ_MAJOR,
2154c10b1400SMax Laier 		    IWI_FW_REQ_MINOR);
2155c10b1400SMax Laier 		return NULL;
2156c10b1400SMax Laier 	}
2157c10b1400SMax Laier 	fw->data = ((const char *) fp->data) + sizeof(struct iwi_firmware_ohdr);
2158c10b1400SMax Laier 	fw->size = fp->datasize - sizeof(struct iwi_firmware_ohdr);
2159c10b1400SMax Laier 	fw->name = fp->name;
2160c10b1400SMax Laier 	return hdr;
2161c10b1400SMax Laier }
2162c10b1400SMax Laier 
2163c10b1400SMax Laier static const struct iwi_firmware_ohdr *
iwi_setup_oucode(struct iwi_softc * sc,struct iwi_fw * fw)2164c10b1400SMax Laier iwi_setup_oucode(struct iwi_softc *sc, struct iwi_fw *fw)
2165c10b1400SMax Laier {
2166c10b1400SMax Laier 	const struct iwi_firmware_ohdr *hdr;
2167c10b1400SMax Laier 
2168c10b1400SMax Laier 	hdr = iwi_setup_ofw(sc, fw);
2169c10b1400SMax Laier 	if (hdr != NULL && le32toh(hdr->mode) != IWI_FW_MODE_UCODE) {
2170c10b1400SMax Laier 		device_printf(sc->sc_dev, "%s is not a ucode image\n",
2171c10b1400SMax Laier 		    fw->name);
2172c10b1400SMax Laier 		hdr = NULL;
2173c10b1400SMax Laier 	}
2174c10b1400SMax Laier 	return hdr;
2175c10b1400SMax Laier }
2176c10b1400SMax Laier 
2177c10b1400SMax Laier static void
iwi_getfw(struct iwi_fw * fw,const char * fwname,struct iwi_fw * uc,const char * ucname)2178c10b1400SMax Laier iwi_getfw(struct iwi_fw *fw, const char *fwname,
2179c10b1400SMax Laier 	  struct iwi_fw *uc, const char *ucname)
2180c10b1400SMax Laier {
2181c10b1400SMax Laier 	if (fw->fp == NULL)
2182c10b1400SMax Laier 		fw->fp = firmware_get(fwname);
2183c10b1400SMax Laier 	/* NB: pre-3.0 ucode is packaged separately */
2184c10b1400SMax Laier 	if (uc->fp == NULL && fw->fp != NULL && fw->fp->version < 300)
2185c10b1400SMax Laier 		uc->fp = firmware_get(ucname);
2186c10b1400SMax Laier }
2187c10b1400SMax Laier 
2188c10b1400SMax Laier /*
2189c10b1400SMax Laier  * Get the required firmware images if not already loaded.
2190c10b1400SMax Laier  * Note that we hold firmware images so long as the device
2191c10b1400SMax Laier  * is marked up in case we need to reload them on device init.
2192c10b1400SMax Laier  * This is necessary because we re-init the device sometimes
2193c10b1400SMax Laier  * from a context where we cannot read from the filesystem
2194c10b1400SMax Laier  * (e.g. from the taskqueue thread when rfkill is re-enabled).
2195484f6530SLuigi Rizzo  * XXX return 0 on success, 1 on error.
2196c10b1400SMax Laier  *
2197c10b1400SMax Laier  * NB: the order of get'ing and put'ing images here is
2198c10b1400SMax Laier  * intentional to support handling firmware images bundled
2199c10b1400SMax Laier  * by operating mode and/or all together in one file with
2200c10b1400SMax Laier  * the boot firmware as "master".
2201c10b1400SMax Laier  */
2202c10b1400SMax Laier static int
iwi_get_firmware(struct iwi_softc * sc,enum ieee80211_opmode opmode)2203b032f27cSSam Leffler iwi_get_firmware(struct iwi_softc *sc, enum ieee80211_opmode opmode)
2204c10b1400SMax Laier {
2205c10b1400SMax Laier 	const struct iwi_firmware_hdr *hdr;
220633d54970SLuigi Rizzo 	const struct firmware *fp;
2207c10b1400SMax Laier 
2208c10b1400SMax Laier 	/* invalidate cached firmware on mode change */
2209b032f27cSSam Leffler 	if (sc->fw_mode != opmode)
2210c10b1400SMax Laier 		iwi_put_firmware(sc);
2211c10b1400SMax Laier 
2212b032f27cSSam Leffler 	switch (opmode) {
2213c10b1400SMax Laier 	case IEEE80211_M_STA:
2214c10b1400SMax Laier 		iwi_getfw(&sc->fw_fw, "iwi_bss", &sc->fw_uc, "iwi_ucode_bss");
2215c10b1400SMax Laier 		break;
2216c10b1400SMax Laier 	case IEEE80211_M_IBSS:
2217c10b1400SMax Laier 		iwi_getfw(&sc->fw_fw, "iwi_ibss", &sc->fw_uc, "iwi_ucode_ibss");
2218c10b1400SMax Laier 		break;
2219c10b1400SMax Laier 	case IEEE80211_M_MONITOR:
2220c10b1400SMax Laier 		iwi_getfw(&sc->fw_fw, "iwi_monitor",
2221c10b1400SMax Laier 			  &sc->fw_uc, "iwi_ucode_monitor");
2222c10b1400SMax Laier 		break;
2223c10b1400SMax Laier 	default:
2224820e6a1fSRui Paulo 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
2225820e6a1fSRui Paulo 		return EINVAL;
2226c10b1400SMax Laier 	}
2227c10b1400SMax Laier 	fp = sc->fw_fw.fp;
2228c10b1400SMax Laier 	if (fp == NULL) {
2229c10b1400SMax Laier 		device_printf(sc->sc_dev, "could not load firmware\n");
2230c10b1400SMax Laier 		goto bad;
2231c10b1400SMax Laier 	}
2232c10b1400SMax Laier 	if (fp->version < 300) {
2233c10b1400SMax Laier 		/*
2234c10b1400SMax Laier 		 * Firmware prior to 3.0 was packaged as separate
2235c10b1400SMax Laier 		 * boot, firmware, and ucode images.  Verify the
2236c10b1400SMax Laier 		 * ucode image was read in, retrieve the boot image
2237c10b1400SMax Laier 		 * if needed, and check version stamps for consistency.
2238c10b1400SMax Laier 		 * The version stamps in the data are also checked
2239c10b1400SMax Laier 		 * above; this is a bit paranoid but is a cheap
2240c10b1400SMax Laier 		 * safeguard against mis-packaging.
2241c10b1400SMax Laier 		 */
2242c10b1400SMax Laier 		if (sc->fw_uc.fp == NULL) {
2243c10b1400SMax Laier 			device_printf(sc->sc_dev, "could not load ucode\n");
2244c10b1400SMax Laier 			goto bad;
2245c10b1400SMax Laier 		}
2246c10b1400SMax Laier 		if (sc->fw_boot.fp == NULL) {
2247c10b1400SMax Laier 			sc->fw_boot.fp = firmware_get("iwi_boot");
2248c10b1400SMax Laier 			if (sc->fw_boot.fp == NULL) {
2249c10b1400SMax Laier 				device_printf(sc->sc_dev,
2250c10b1400SMax Laier 					"could not load boot firmware\n");
2251c10b1400SMax Laier 				goto bad;
2252c10b1400SMax Laier 			}
2253c10b1400SMax Laier 		}
2254c10b1400SMax Laier 		if (sc->fw_boot.fp->version != sc->fw_fw.fp->version ||
2255c10b1400SMax Laier 		    sc->fw_boot.fp->version != sc->fw_uc.fp->version) {
2256c10b1400SMax Laier 			device_printf(sc->sc_dev,
2257c10b1400SMax Laier 			    "firmware version mismatch: "
2258c10b1400SMax Laier 			    "'%s' is %d, '%s' is %d, '%s' is %d\n",
2259c10b1400SMax Laier 			    sc->fw_boot.fp->name, sc->fw_boot.fp->version,
2260c10b1400SMax Laier 			    sc->fw_uc.fp->name, sc->fw_uc.fp->version,
2261c10b1400SMax Laier 			    sc->fw_fw.fp->name, sc->fw_fw.fp->version
2262c10b1400SMax Laier 			);
2263c10b1400SMax Laier 			goto bad;
2264c10b1400SMax Laier 		}
2265c10b1400SMax Laier 		/*
2266c10b1400SMax Laier 		 * Check and setup each image.
2267c10b1400SMax Laier 		 */
2268c10b1400SMax Laier 		if (iwi_setup_oucode(sc, &sc->fw_uc) == NULL ||
2269c10b1400SMax Laier 		    iwi_setup_ofw(sc, &sc->fw_boot) == NULL ||
2270c10b1400SMax Laier 		    iwi_setup_ofw(sc, &sc->fw_fw) == NULL)
2271c10b1400SMax Laier 			goto bad;
2272c10b1400SMax Laier 	} else {
2273c10b1400SMax Laier 		/*
2274c10b1400SMax Laier 		 * Check and setup combined image.
2275c10b1400SMax Laier 		 */
2276484f6530SLuigi Rizzo 		if (fp->datasize < sizeof(struct iwi_firmware_hdr)) {
2277c10b1400SMax Laier 			device_printf(sc->sc_dev, "image '%s' too small\n",
2278c10b1400SMax Laier 			    fp->name);
2279c10b1400SMax Laier 			goto bad;
2280c10b1400SMax Laier 		}
2281c10b1400SMax Laier 		hdr = (const struct iwi_firmware_hdr *)fp->data;
2282484f6530SLuigi Rizzo 		if (fp->datasize < sizeof(*hdr) + le32toh(hdr->bsize) + le32toh(hdr->usize)
2283484f6530SLuigi Rizzo 				+ le32toh(hdr->fsize)) {
2284c10b1400SMax Laier 			device_printf(sc->sc_dev, "image '%s' too small (2)\n",
2285c10b1400SMax Laier 			    fp->name);
2286c10b1400SMax Laier 			goto bad;
2287c10b1400SMax Laier 		}
2288c10b1400SMax Laier 		sc->fw_boot.data = ((const char *) fp->data) + sizeof(*hdr);
2289484f6530SLuigi Rizzo 		sc->fw_boot.size = le32toh(hdr->bsize);
2290c10b1400SMax Laier 		sc->fw_boot.name = fp->name;
2291c10b1400SMax Laier 		sc->fw_uc.data = sc->fw_boot.data + sc->fw_boot.size;
2292484f6530SLuigi Rizzo 		sc->fw_uc.size = le32toh(hdr->usize);
2293c10b1400SMax Laier 		sc->fw_uc.name = fp->name;
2294c10b1400SMax Laier 		sc->fw_fw.data = sc->fw_uc.data + sc->fw_uc.size;
2295484f6530SLuigi Rizzo 		sc->fw_fw.size = le32toh(hdr->fsize);
2296c10b1400SMax Laier 		sc->fw_fw.name = fp->name;
2297c10b1400SMax Laier 	}
2298484f6530SLuigi Rizzo #if 0
2299484f6530SLuigi Rizzo 	device_printf(sc->sc_dev, "boot %d ucode %d fw %d bytes\n",
2300484f6530SLuigi Rizzo 		sc->fw_boot.size, sc->fw_uc.size, sc->fw_fw.size);
2301484f6530SLuigi Rizzo #endif
2302c10b1400SMax Laier 
2303b032f27cSSam Leffler 	sc->fw_mode = opmode;
2304484f6530SLuigi Rizzo 	return 0;
2305c10b1400SMax Laier bad:
2306c10b1400SMax Laier 	iwi_put_firmware(sc);
2307484f6530SLuigi Rizzo 	return 1;
2308c10b1400SMax Laier }
2309c10b1400SMax Laier 
2310c10b1400SMax Laier static void
iwi_put_fw(struct iwi_fw * fw)2311c10b1400SMax Laier iwi_put_fw(struct iwi_fw *fw)
2312c10b1400SMax Laier {
2313c10b1400SMax Laier 	if (fw->fp != NULL) {
2314c10b1400SMax Laier 		firmware_put(fw->fp, FIRMWARE_UNLOAD);
2315c10b1400SMax Laier 		fw->fp = NULL;
2316c10b1400SMax Laier 	}
2317c10b1400SMax Laier 	fw->data = NULL;
2318c10b1400SMax Laier 	fw->size = 0;
2319c10b1400SMax Laier 	fw->name = NULL;
2320c10b1400SMax Laier }
2321c10b1400SMax Laier 
2322c10b1400SMax Laier /*
2323c10b1400SMax Laier  * Release any cached firmware images.
2324c10b1400SMax Laier  */
2325c10b1400SMax Laier static void
iwi_put_firmware(struct iwi_softc * sc)2326c10b1400SMax Laier iwi_put_firmware(struct iwi_softc *sc)
2327c10b1400SMax Laier {
2328c10b1400SMax Laier 	iwi_put_fw(&sc->fw_uc);
2329c10b1400SMax Laier 	iwi_put_fw(&sc->fw_fw);
2330c10b1400SMax Laier 	iwi_put_fw(&sc->fw_boot);
2331c10b1400SMax Laier }
2332c10b1400SMax Laier 
2333c10b1400SMax Laier static int
iwi_load_ucode(struct iwi_softc * sc,const struct iwi_fw * fw)2334c10b1400SMax Laier iwi_load_ucode(struct iwi_softc *sc, const struct iwi_fw *fw)
2335c10b1400SMax Laier {
2336c10b1400SMax Laier 	uint32_t tmp;
2337c10b1400SMax Laier 	const uint16_t *w;
2338c10b1400SMax Laier 	const char *uc = fw->data;
2339c10b1400SMax Laier 	size_t size = fw->size;
2340c10b1400SMax Laier 	int i, ntries, error;
2341c10b1400SMax Laier 
234268e8e04eSSam Leffler 	IWI_LOCK_ASSERT(sc);
2343c10b1400SMax Laier 	error = 0;
2344ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
2345ceaec73dSDamien Bergamini 	    IWI_RST_STOP_MASTER);
2346ceaec73dSDamien Bergamini 	for (ntries = 0; ntries < 5; ntries++) {
2347ceaec73dSDamien Bergamini 		if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2348ceaec73dSDamien Bergamini 			break;
2349ceaec73dSDamien Bergamini 		DELAY(10);
2350ceaec73dSDamien Bergamini 	}
2351ceaec73dSDamien Bergamini 	if (ntries == 5) {
2352ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "timeout waiting for master\n");
2353c10b1400SMax Laier 		error = EIO;
2354c10b1400SMax Laier 		goto fail;
2355ceaec73dSDamien Bergamini 	}
2356ceaec73dSDamien Bergamini 
2357ceaec73dSDamien Bergamini 	MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2358ceaec73dSDamien Bergamini 	DELAY(5000);
2359ceaec73dSDamien Bergamini 
2360ceaec73dSDamien Bergamini 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
2361ceaec73dSDamien Bergamini 	tmp &= ~IWI_RST_PRINCETON_RESET;
2362ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2363ceaec73dSDamien Bergamini 
2364ceaec73dSDamien Bergamini 	DELAY(5000);
2365ceaec73dSDamien Bergamini 	MEM_WRITE_4(sc, 0x3000e0, 0);
2366ceaec73dSDamien Bergamini 	DELAY(1000);
2367c10b1400SMax Laier 	MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 1);
2368ceaec73dSDamien Bergamini 	DELAY(1000);
2369c10b1400SMax Laier 	MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 0);
2370ceaec73dSDamien Bergamini 	DELAY(1000);
2371ceaec73dSDamien Bergamini 	MEM_WRITE_1(sc, 0x200000, 0x00);
2372ceaec73dSDamien Bergamini 	MEM_WRITE_1(sc, 0x200000, 0x40);
2373ceaec73dSDamien Bergamini 	DELAY(1000);
2374ceaec73dSDamien Bergamini 
2375ceaec73dSDamien Bergamini 	/* write microcode into adapter memory */
2376c10b1400SMax Laier 	for (w = (const uint16_t *)uc; size > 0; w++, size -= 2)
237781e6343cSDamien Bergamini 		MEM_WRITE_2(sc, 0x200010, htole16(*w));
2378ceaec73dSDamien Bergamini 
2379ceaec73dSDamien Bergamini 	MEM_WRITE_1(sc, 0x200000, 0x00);
2380ceaec73dSDamien Bergamini 	MEM_WRITE_1(sc, 0x200000, 0x80);
2381ceaec73dSDamien Bergamini 
2382ceaec73dSDamien Bergamini 	/* wait until we get an answer */
2383ceaec73dSDamien Bergamini 	for (ntries = 0; ntries < 100; ntries++) {
2384ceaec73dSDamien Bergamini 		if (MEM_READ_1(sc, 0x200000) & 1)
2385ceaec73dSDamien Bergamini 			break;
2386ceaec73dSDamien Bergamini 		DELAY(100);
2387ceaec73dSDamien Bergamini 	}
2388ceaec73dSDamien Bergamini 	if (ntries == 100) {
2389ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev,
2390ceaec73dSDamien Bergamini 		    "timeout waiting for ucode to initialize\n");
2391c10b1400SMax Laier 		error = EIO;
2392c10b1400SMax Laier 		goto fail;
2393ceaec73dSDamien Bergamini 	}
2394ceaec73dSDamien Bergamini 
2395ceaec73dSDamien Bergamini 	/* read the answer or the firmware will not initialize properly */
2396ceaec73dSDamien Bergamini 	for (i = 0; i < 7; i++)
2397ceaec73dSDamien Bergamini 		MEM_READ_4(sc, 0x200004);
2398ceaec73dSDamien Bergamini 
2399ceaec73dSDamien Bergamini 	MEM_WRITE_1(sc, 0x200000, 0x00);
2400ceaec73dSDamien Bergamini 
2401c10b1400SMax Laier fail:
2402c10b1400SMax Laier 	return error;
2403ceaec73dSDamien Bergamini }
2404ceaec73dSDamien Bergamini 
2405ceaec73dSDamien Bergamini /* macro to handle unaligned little endian data in firmware image */
2406ceaec73dSDamien Bergamini #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2407ceaec73dSDamien Bergamini 
2408ceaec73dSDamien Bergamini static int
iwi_load_firmware(struct iwi_softc * sc,const struct iwi_fw * fw)2409c10b1400SMax Laier iwi_load_firmware(struct iwi_softc *sc, const struct iwi_fw *fw)
2410ceaec73dSDamien Bergamini {
2411ceaec73dSDamien Bergamini 	u_char *p, *end;
2412ceaec73dSDamien Bergamini 	uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp;
2413e1000b36SScott Long 	int ntries, error;
2414408146edSDamien Bergamini 
241568e8e04eSSam Leffler 	IWI_LOCK_ASSERT(sc);
2416b032f27cSSam Leffler 
2417ceaec73dSDamien Bergamini 	/* copy firmware image to DMA memory */
2418c10b1400SMax Laier 	memcpy(sc->fw_virtaddr, fw->data, fw->size);
2419ceaec73dSDamien Bergamini 
2420ceaec73dSDamien Bergamini 	/* make sure the adapter will get up-to-date values */
2421c10b1400SMax Laier 	bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_PREWRITE);
2422c56adf0aSDamien Bergamini 
2423ceaec73dSDamien Bergamini 	/* tell the adapter where the command blocks are stored */
2424ceaec73dSDamien Bergamini 	MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2425ceaec73dSDamien Bergamini 
2426ceaec73dSDamien Bergamini 	/*
2427ceaec73dSDamien Bergamini 	 * Store command blocks into adapter's internal memory using register
2428ceaec73dSDamien Bergamini 	 * indirections. The adapter will read the firmware image through DMA
2429ceaec73dSDamien Bergamini 	 * using information stored in command blocks.
2430ceaec73dSDamien Bergamini 	 */
2431c10b1400SMax Laier 	src = sc->fw_physaddr;
2432c10b1400SMax Laier 	p = sc->fw_virtaddr;
2433c10b1400SMax Laier 	end = p + fw->size;
2434ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2435ceaec73dSDamien Bergamini 
2436ceaec73dSDamien Bergamini 	while (p < end) {
2437ceaec73dSDamien Bergamini 		dst = GETLE32(p); p += 4; src += 4;
2438ceaec73dSDamien Bergamini 		len = GETLE32(p); p += 4; src += 4;
2439ceaec73dSDamien Bergamini 		p += len;
2440ceaec73dSDamien Bergamini 
2441ceaec73dSDamien Bergamini 		while (len > 0) {
2442ceaec73dSDamien Bergamini 			mlen = min(len, IWI_CB_MAXDATALEN);
2443ceaec73dSDamien Bergamini 
2444ceaec73dSDamien Bergamini 			ctl = IWI_CB_DEFAULT_CTL | mlen;
2445ceaec73dSDamien Bergamini 			sum = ctl ^ src ^ dst;
2446ceaec73dSDamien Bergamini 
2447ceaec73dSDamien Bergamini 			/* write a command block */
2448ceaec73dSDamien Bergamini 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2449ceaec73dSDamien Bergamini 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2450ceaec73dSDamien Bergamini 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2451ceaec73dSDamien Bergamini 			CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2452ceaec73dSDamien Bergamini 
2453ceaec73dSDamien Bergamini 			src += mlen;
2454ceaec73dSDamien Bergamini 			dst += mlen;
2455ceaec73dSDamien Bergamini 			len -= mlen;
2456ceaec73dSDamien Bergamini 		}
2457ceaec73dSDamien Bergamini 	}
2458ceaec73dSDamien Bergamini 
2459ceaec73dSDamien Bergamini 	/* write a fictive final command block (sentinel) */
2460ceaec73dSDamien Bergamini 	sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2461ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2462ceaec73dSDamien Bergamini 
2463ceaec73dSDamien Bergamini 	tmp = CSR_READ_4(sc, IWI_CSR_RST);
2464ceaec73dSDamien Bergamini 	tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
2465ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2466ceaec73dSDamien Bergamini 
2467ceaec73dSDamien Bergamini 	/* tell the adapter to start processing command blocks */
2468ceaec73dSDamien Bergamini 	MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2469ceaec73dSDamien Bergamini 
2470408146edSDamien Bergamini 	/* wait until the adapter reaches the sentinel */
2471ceaec73dSDamien Bergamini 	for (ntries = 0; ntries < 400; ntries++) {
2472ceaec73dSDamien Bergamini 		if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2473ceaec73dSDamien Bergamini 			break;
2474ceaec73dSDamien Bergamini 		DELAY(100);
2475ceaec73dSDamien Bergamini 	}
2476484f6530SLuigi Rizzo 	/* sync dma, just in case */
2477484f6530SLuigi Rizzo 	bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_POSTWRITE);
2478ceaec73dSDamien Bergamini 	if (ntries == 400) {
2479ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev,
2480c10b1400SMax Laier 		    "timeout processing command blocks for %s firmware\n",
2481c10b1400SMax Laier 		    fw->name);
2482484f6530SLuigi Rizzo 		return EIO;
2483ceaec73dSDamien Bergamini 	}
2484ceaec73dSDamien Bergamini 
2485ceaec73dSDamien Bergamini 	/* we're done with command blocks processing */
2486ceaec73dSDamien Bergamini 	MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2487ceaec73dSDamien Bergamini 
2488408146edSDamien Bergamini 	/* allow interrupts so we know when the firmware is ready */
2489ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2490ceaec73dSDamien Bergamini 
2491ceaec73dSDamien Bergamini 	/* tell the adapter to initialize the firmware */
2492ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2493ceaec73dSDamien Bergamini 
2494ceaec73dSDamien Bergamini 	tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2495ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
2496ceaec73dSDamien Bergamini 
2497ceaec73dSDamien Bergamini 	/* wait at most one second for firmware initialization to complete */
2498ceaec73dSDamien Bergamini 	if ((error = msleep(sc, &sc->sc_mtx, 0, "iwiinit", hz)) != 0) {
2499c10b1400SMax Laier 		device_printf(sc->sc_dev, "timeout waiting for %s firmware "
2500c10b1400SMax Laier 		    "initialization to complete\n", fw->name);
2501ceaec73dSDamien Bergamini 	}
2502ceaec73dSDamien Bergamini 
2503ceaec73dSDamien Bergamini 	return error;
2504ceaec73dSDamien Bergamini }
2505ceaec73dSDamien Bergamini 
2506ceaec73dSDamien Bergamini static int
iwi_setpowermode(struct iwi_softc * sc,struct ieee80211vap * vap)2507b032f27cSSam Leffler iwi_setpowermode(struct iwi_softc *sc, struct ieee80211vap *vap)
2508c10b1400SMax Laier {
2509c10b1400SMax Laier 	uint32_t data;
2510c10b1400SMax Laier 
2511b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PMGTON) {
2512c10b1400SMax Laier 		/* XXX set more fine-grained operation */
2513c10b1400SMax Laier 		data = htole32(IWI_POWER_MODE_MAX);
2514c10b1400SMax Laier 	} else
2515c10b1400SMax Laier 		data = htole32(IWI_POWER_MODE_CAM);
2516c10b1400SMax Laier 
2517c10b1400SMax Laier 	DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2518c10b1400SMax Laier 	return iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data);
2519c10b1400SMax Laier }
2520c10b1400SMax Laier 
2521c10b1400SMax Laier static int
iwi_setwepkeys(struct iwi_softc * sc,struct ieee80211vap * vap)2522b032f27cSSam Leffler iwi_setwepkeys(struct iwi_softc *sc, struct ieee80211vap *vap)
2523c10b1400SMax Laier {
2524c10b1400SMax Laier 	struct iwi_wep_key wepkey;
2525c10b1400SMax Laier 	struct ieee80211_key *wk;
2526c10b1400SMax Laier 	int error, i;
2527c10b1400SMax Laier 
2528c10b1400SMax Laier 	for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2529b032f27cSSam Leffler 		wk = &vap->iv_nw_keys[i];
2530c10b1400SMax Laier 
2531c10b1400SMax Laier 		wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2532c10b1400SMax Laier 		wepkey.idx = i;
2533c10b1400SMax Laier 		wepkey.len = wk->wk_keylen;
2534c10b1400SMax Laier 		memset(wepkey.key, 0, sizeof wepkey.key);
2535c10b1400SMax Laier 		memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2536c10b1400SMax Laier 		DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2537c10b1400SMax Laier 		    wepkey.len));
2538c10b1400SMax Laier 		error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2539c10b1400SMax Laier 		    sizeof wepkey);
2540c10b1400SMax Laier 		if (error != 0)
2541c10b1400SMax Laier 			return error;
2542c10b1400SMax Laier 	}
2543c10b1400SMax Laier 	return 0;
2544c10b1400SMax Laier }
2545c10b1400SMax Laier 
2546c10b1400SMax Laier static int
iwi_set_rateset(struct iwi_softc * sc,const struct ieee80211_rateset * net_rs,int mode,int type)2547222a2f1dSAndriy Voskoboinyk iwi_set_rateset(struct iwi_softc *sc, const struct ieee80211_rateset *net_rs,
2548222a2f1dSAndriy Voskoboinyk     int mode, int type)
2549222a2f1dSAndriy Voskoboinyk {
2550222a2f1dSAndriy Voskoboinyk 	struct iwi_rateset rs;
2551222a2f1dSAndriy Voskoboinyk 
2552222a2f1dSAndriy Voskoboinyk 	memset(&rs, 0, sizeof(rs));
2553222a2f1dSAndriy Voskoboinyk 	rs.mode = mode;
2554222a2f1dSAndriy Voskoboinyk 	rs.type = type;
2555222a2f1dSAndriy Voskoboinyk 	rs.nrates = net_rs->rs_nrates;
2556222a2f1dSAndriy Voskoboinyk 	if (rs.nrates > nitems(rs.rates)) {
2557222a2f1dSAndriy Voskoboinyk 		DPRINTF(("Truncating negotiated rate set from %u\n",
2558222a2f1dSAndriy Voskoboinyk 		    rs.nrates));
2559222a2f1dSAndriy Voskoboinyk 		rs.nrates = nitems(rs.rates);
2560222a2f1dSAndriy Voskoboinyk 	}
2561222a2f1dSAndriy Voskoboinyk 	memcpy(rs.rates, net_rs->rs_rates, rs.nrates);
2562222a2f1dSAndriy Voskoboinyk 	DPRINTF(("Setting .11%c%s %s rates (%u)\n",
2563222a2f1dSAndriy Voskoboinyk 	    mode == IWI_MODE_11A ? 'a' : 'b',
2564222a2f1dSAndriy Voskoboinyk 	    mode == IWI_MODE_11G ? "g" : "",
2565222a2f1dSAndriy Voskoboinyk 	    type == IWI_RATESET_TYPE_SUPPORTED ? "supported" : "negotiated",
2566222a2f1dSAndriy Voskoboinyk 	    rs.nrates));
2567222a2f1dSAndriy Voskoboinyk 
2568222a2f1dSAndriy Voskoboinyk 	return (iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof(rs)));
2569222a2f1dSAndriy Voskoboinyk }
2570222a2f1dSAndriy Voskoboinyk 
2571222a2f1dSAndriy Voskoboinyk static int
iwi_config(struct iwi_softc * sc)2572ceaec73dSDamien Bergamini iwi_config(struct iwi_softc *sc)
2573ceaec73dSDamien Bergamini {
25747a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
257589427621SAndriy Voskoboinyk 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2576ceaec73dSDamien Bergamini 	struct iwi_configuration config;
2577ceaec73dSDamien Bergamini 	struct iwi_txpower power;
257889427621SAndriy Voskoboinyk 	uint8_t *macaddr;
2579ceaec73dSDamien Bergamini 	uint32_t data;
2580ceaec73dSDamien Bergamini 	int error, i;
2581b032f27cSSam Leffler 
258268e8e04eSSam Leffler 	IWI_LOCK_ASSERT(sc);
2583ceaec73dSDamien Bergamini 
258489427621SAndriy Voskoboinyk 	macaddr = vap ? vap->iv_myaddr : ic->ic_macaddr;
258589427621SAndriy Voskoboinyk 	DPRINTF(("Setting MAC address to %6D\n", macaddr, ":"));
258689427621SAndriy Voskoboinyk 	error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, macaddr,
2587c10b1400SMax Laier 	    IEEE80211_ADDR_LEN);
2588ceaec73dSDamien Bergamini 	if (error != 0)
2589ceaec73dSDamien Bergamini 		return error;
2590ceaec73dSDamien Bergamini 
2591ceaec73dSDamien Bergamini 	memset(&config, 0, sizeof config);
2592ceaec73dSDamien Bergamini 	config.bluetooth_coexistence = sc->bluetooth;
2593c10b1400SMax Laier 	config.silence_threshold = 0x1e;
2594acea4241SDamien Bergamini 	config.antenna = sc->antenna;
2595ceaec73dSDamien Bergamini 	config.multicast_enabled = 1;
2596acea4241SDamien Bergamini 	config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2597acea4241SDamien Bergamini 	config.disable_unicast_decryption = 1;
2598acea4241SDamien Bergamini 	config.disable_multicast_decryption = 1;
259940668dc5SBernhard Schmidt 	if (ic->ic_opmode == IEEE80211_M_MONITOR) {
260040668dc5SBernhard Schmidt 		config.allow_invalid_frames = 1;
260140668dc5SBernhard Schmidt 		config.allow_beacon_and_probe_resp = 1;
260240668dc5SBernhard Schmidt 		config.allow_mgt = 1;
260340668dc5SBernhard Schmidt 	}
2604ceaec73dSDamien Bergamini 	DPRINTF(("Configuring adapter\n"));
2605c10b1400SMax Laier 	error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2606ceaec73dSDamien Bergamini 	if (error != 0)
2607ceaec73dSDamien Bergamini 		return error;
2608ceaec73dSDamien Bergamini 	if (ic->ic_opmode == IEEE80211_M_IBSS) {
2609ceaec73dSDamien Bergamini 		power.mode = IWI_MODE_11B;
2610ceaec73dSDamien Bergamini 		power.nchan = 11;
2611ceaec73dSDamien Bergamini 		for (i = 0; i < 11; i++) {
2612ceaec73dSDamien Bergamini 			power.chan[i].chan = i + 1;
2613ceaec73dSDamien Bergamini 			power.chan[i].power = IWI_TXPOWER_MAX;
2614ceaec73dSDamien Bergamini 		}
2615ceaec73dSDamien Bergamini 		DPRINTF(("Setting .11b channels tx power\n"));
2616c10b1400SMax Laier 		error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2617ceaec73dSDamien Bergamini 		if (error != 0)
2618ceaec73dSDamien Bergamini 			return error;
2619ceaec73dSDamien Bergamini 
2620ceaec73dSDamien Bergamini 		power.mode = IWI_MODE_11G;
2621ceaec73dSDamien Bergamini 		DPRINTF(("Setting .11g channels tx power\n"));
2622c10b1400SMax Laier 		error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2623ceaec73dSDamien Bergamini 		if (error != 0)
2624ceaec73dSDamien Bergamini 			return error;
2625ceaec73dSDamien Bergamini 	}
2626ceaec73dSDamien Bergamini 
2627222a2f1dSAndriy Voskoboinyk 	error = iwi_set_rateset(sc, &ic->ic_sup_rates[IEEE80211_MODE_11G],
2628222a2f1dSAndriy Voskoboinyk 	    IWI_MODE_11G, IWI_RATESET_TYPE_SUPPORTED);
2629ceaec73dSDamien Bergamini 	if (error != 0)
2630ceaec73dSDamien Bergamini 		return error;
2631ceaec73dSDamien Bergamini 
2632222a2f1dSAndriy Voskoboinyk 	error = iwi_set_rateset(sc, &ic->ic_sup_rates[IEEE80211_MODE_11A],
2633222a2f1dSAndriy Voskoboinyk 	    IWI_MODE_11A, IWI_RATESET_TYPE_SUPPORTED);
2634ceaec73dSDamien Bergamini 	if (error != 0)
2635ceaec73dSDamien Bergamini 		return error;
2636ceaec73dSDamien Bergamini 
2637ceaec73dSDamien Bergamini 	data = htole32(arc4random());
2638ceaec73dSDamien Bergamini 	DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2639c10b1400SMax Laier 	error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data);
2640ceaec73dSDamien Bergamini 	if (error != 0)
2641ceaec73dSDamien Bergamini 		return error;
2642ceaec73dSDamien Bergamini 
2643ceaec73dSDamien Bergamini 	/* enable adapter */
2644ceaec73dSDamien Bergamini 	DPRINTF(("Enabling adapter\n"));
2645c10b1400SMax Laier 	return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0);
2646ceaec73dSDamien Bergamini }
2647ceaec73dSDamien Bergamini 
2648c10b1400SMax Laier static __inline void
set_scan_type(struct iwi_scan_ext * scan,int ix,int scan_type)2649c10b1400SMax Laier set_scan_type(struct iwi_scan_ext *scan, int ix, int scan_type)
2650acea4241SDamien Bergamini {
2651c10b1400SMax Laier 	uint8_t *st = &scan->scan_type[ix / 2];
2652c10b1400SMax Laier 	if (ix % 2)
2653c10b1400SMax Laier 		*st = (*st & 0xf0) | ((scan_type & 0xf) << 0);
2654c10b1400SMax Laier 	else
2655c10b1400SMax Laier 		*st = (*st & 0x0f) | ((scan_type & 0xf) << 4);
2656acea4241SDamien Bergamini }
2657acea4241SDamien Bergamini 
2658acea4241SDamien Bergamini static int
scan_type(const struct ieee80211_scan_state * ss,const struct ieee80211_channel * chan)265968e8e04eSSam Leffler scan_type(const struct ieee80211_scan_state *ss,
266068e8e04eSSam Leffler 	const struct ieee80211_channel *chan)
2661ceaec73dSDamien Bergamini {
266268e8e04eSSam Leffler 	/* We can only set one essid for a directed scan */
266368e8e04eSSam Leffler 	if (ss->ss_nssid != 0)
266468e8e04eSSam Leffler 		return IWI_SCAN_TYPE_BDIRECTED;
266568e8e04eSSam Leffler 	if ((ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
266668e8e04eSSam Leffler 	    (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
266768e8e04eSSam Leffler 		return IWI_SCAN_TYPE_BROADCAST;
266868e8e04eSSam Leffler 	return IWI_SCAN_TYPE_PASSIVE;
266968e8e04eSSam Leffler }
2670ceaec73dSDamien Bergamini 
267168e8e04eSSam Leffler static __inline int
scan_band(const struct ieee80211_channel * c)267268e8e04eSSam Leffler scan_band(const struct ieee80211_channel *c)
267368e8e04eSSam Leffler {
267468e8e04eSSam Leffler 	return IEEE80211_IS_CHAN_5GHZ(c) ?  IWI_CHAN_5GHZ : IWI_CHAN_2GHZ;
267568e8e04eSSam Leffler }
267668e8e04eSSam Leffler 
267740668dc5SBernhard Schmidt static void
iwi_monitor_scan(void * arg,int npending)267840668dc5SBernhard Schmidt iwi_monitor_scan(void *arg, int npending)
267940668dc5SBernhard Schmidt {
268040668dc5SBernhard Schmidt 	struct iwi_softc *sc = arg;
268140668dc5SBernhard Schmidt 	IWI_LOCK_DECL;
268240668dc5SBernhard Schmidt 
268340668dc5SBernhard Schmidt 	IWI_LOCK(sc);
268440668dc5SBernhard Schmidt 	(void) iwi_scanchan(sc, 2000, 0);
268540668dc5SBernhard Schmidt 	IWI_UNLOCK(sc);
268640668dc5SBernhard Schmidt }
268740668dc5SBernhard Schmidt 
268868e8e04eSSam Leffler /*
268968e8e04eSSam Leffler  * Start a scan on the current channel or all channels.
269068e8e04eSSam Leffler  */
269168e8e04eSSam Leffler static int
iwi_scanchan(struct iwi_softc * sc,unsigned long maxdwell,int allchan)26925efea30fSAndrew Thompson iwi_scanchan(struct iwi_softc *sc, unsigned long maxdwell, int allchan)
269368e8e04eSSam Leffler {
26947a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
269568e8e04eSSam Leffler 	struct ieee80211_channel *chan;
269668e8e04eSSam Leffler 	struct ieee80211_scan_state *ss;
269768e8e04eSSam Leffler 	struct iwi_scan_ext scan;
269868e8e04eSSam Leffler 	int error = 0;
269968e8e04eSSam Leffler 
270068e8e04eSSam Leffler 	IWI_LOCK_ASSERT(sc);
270168e8e04eSSam Leffler 	if (sc->fw_state == IWI_FW_SCANNING) {
270268e8e04eSSam Leffler 		/*
270368e8e04eSSam Leffler 		 * This should not happen as we only trigger scan_next after
270468e8e04eSSam Leffler 		 * completion
270568e8e04eSSam Leffler 		 */
270668e8e04eSSam Leffler 		DPRINTF(("%s: called too early - still scanning\n", __func__));
270768e8e04eSSam Leffler 		return (EBUSY);
270868e8e04eSSam Leffler 	}
270968e8e04eSSam Leffler 	IWI_STATE_BEGIN(sc, IWI_FW_SCANNING);
271068e8e04eSSam Leffler 
271168e8e04eSSam Leffler 	ss = ic->ic_scan;
2712484f6530SLuigi Rizzo 
2713ceaec73dSDamien Bergamini 	memset(&scan, 0, sizeof scan);
271468e8e04eSSam Leffler 	scan.full_scan_index = htole32(++sc->sc_scangen);
271568e8e04eSSam Leffler 	scan.dwell_time[IWI_SCAN_TYPE_PASSIVE] = htole16(maxdwell);
271668e8e04eSSam Leffler 	if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
271768e8e04eSSam Leffler 		/*
271868e8e04eSSam Leffler 		 * Use very short dwell times for when we send probe request
271968e8e04eSSam Leffler 		 * frames.  Without this bg scans hang.  Ideally this should
272068e8e04eSSam Leffler 		 * be handled with early-termination as done by net80211 but
272168e8e04eSSam Leffler 		 * that's not feasible (aborting a scan is problematic).
272268e8e04eSSam Leffler 		 */
272368e8e04eSSam Leffler 		scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(30);
272468e8e04eSSam Leffler 		scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(30);
272568e8e04eSSam Leffler 	} else {
272668e8e04eSSam Leffler 		scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(maxdwell);
272768e8e04eSSam Leffler 		scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(maxdwell);
272868e8e04eSSam Leffler 	}
272980e1a712SDamien Bergamini 
273068e8e04eSSam Leffler 	/* We can only set one essid for a directed scan */
273168e8e04eSSam Leffler 	if (ss->ss_nssid != 0) {
273268e8e04eSSam Leffler 		error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ss->ss_ssid[0].ssid,
273368e8e04eSSam Leffler 		    ss->ss_ssid[0].len);
273468e8e04eSSam Leffler 		if (error)
273568e8e04eSSam Leffler 			return (error);
273668e8e04eSSam Leffler 	}
2737ceaec73dSDamien Bergamini 
27385efea30fSAndrew Thompson 	if (allchan) {
273968e8e04eSSam Leffler 		int i, next, band, b, bstart;
274068e8e04eSSam Leffler 		/*
274168e8e04eSSam Leffler 		 * Convert scan list to run-length encoded channel list
274268e8e04eSSam Leffler 		 * the firmware requires (preserving the order setup by
274368e8e04eSSam Leffler 		 * net80211).  The first entry in each run specifies the
274468e8e04eSSam Leffler 		 * band and the count of items in the run.
274568e8e04eSSam Leffler 		 */
274668e8e04eSSam Leffler 		next = 0;		/* next open slot */
274768e8e04eSSam Leffler 		bstart = 0;		/* NB: not needed, silence compiler */
274868e8e04eSSam Leffler 		band = -1;		/* NB: impossible value */
274968e8e04eSSam Leffler 		KASSERT(ss->ss_last > 0, ("no channels"));
275068e8e04eSSam Leffler 		for (i = 0; i < ss->ss_last; i++) {
275168e8e04eSSam Leffler 			chan = ss->ss_chans[i];
275268e8e04eSSam Leffler 			b = scan_band(chan);
275368e8e04eSSam Leffler 			if (b != band) {
275468e8e04eSSam Leffler 				if (band != -1)
275568e8e04eSSam Leffler 					scan.channels[bstart] =
275668e8e04eSSam Leffler 					    (next - bstart) | band;
275768e8e04eSSam Leffler 				/* NB: this allocates a slot for the run-len */
275868e8e04eSSam Leffler 				band = b, bstart = next++;
275968e8e04eSSam Leffler 			}
276068e8e04eSSam Leffler 			if (next >= IWI_SCAN_CHANNELS) {
276168e8e04eSSam Leffler 				DPRINTF(("truncating scan list\n"));
276268e8e04eSSam Leffler 				break;
276368e8e04eSSam Leffler 			}
276468e8e04eSSam Leffler 			scan.channels[next] = ieee80211_chan2ieee(ic, chan);
276568e8e04eSSam Leffler 			set_scan_type(&scan, next, scan_type(ss, chan));
276668e8e04eSSam Leffler 			next++;
276768e8e04eSSam Leffler 		}
276868e8e04eSSam Leffler 		scan.channels[bstart] = (next - bstart) | band;
276968e8e04eSSam Leffler 	} else {
277068e8e04eSSam Leffler 		/* Scan the current channel only */
277168e8e04eSSam Leffler 		chan = ic->ic_curchan;
277268e8e04eSSam Leffler 		scan.channels[0] = 1 | scan_band(chan);
277368e8e04eSSam Leffler 		scan.channels[1] = ieee80211_chan2ieee(ic, chan);
277468e8e04eSSam Leffler 		set_scan_type(&scan, 1, scan_type(ss, chan));
277568e8e04eSSam Leffler 	}
27760c341cb2SMax Laier #ifdef IWI_DEBUG
27770c341cb2SMax Laier 	if (iwi_debug > 0) {
277868e8e04eSSam Leffler 		static const char *scantype[8] =
277968e8e04eSSam Leffler 		   { "PSTOP", "PASV", "DIR", "BCAST", "BDIR", "5", "6", "7" };
278068e8e04eSSam Leffler 		int i;
278168e8e04eSSam Leffler 		printf("Scan request: index %u dwell %d/%d/%d\n"
278268e8e04eSSam Leffler 		    , le32toh(scan.full_scan_index)
278368e8e04eSSam Leffler 		    , le16toh(scan.dwell_time[IWI_SCAN_TYPE_PASSIVE])
278468e8e04eSSam Leffler 		    , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BROADCAST])
278568e8e04eSSam Leffler 		    , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED])
278668e8e04eSSam Leffler 		);
278768e8e04eSSam Leffler 		i = 0;
278868e8e04eSSam Leffler 		do {
278968e8e04eSSam Leffler 			int run = scan.channels[i];
279068e8e04eSSam Leffler 			if (run == 0)
279168e8e04eSSam Leffler 				break;
279268e8e04eSSam Leffler 			printf("Scan %d %s channels:", run & 0x3f,
279368e8e04eSSam Leffler 			    run & IWI_CHAN_2GHZ ? "2.4GHz" : "5GHz");
279468e8e04eSSam Leffler 			for (run &= 0x3f, i++; run > 0; run--, i++) {
279568e8e04eSSam Leffler 				uint8_t type = scan.scan_type[i/2];
279668e8e04eSSam Leffler 				printf(" %u/%s", scan.channels[i],
279768e8e04eSSam Leffler 				    scantype[(i & 1 ? type : type>>4) & 7]);
279868e8e04eSSam Leffler 			}
27990c341cb2SMax Laier 			printf("\n");
280068e8e04eSSam Leffler 		} while (i < IWI_SCAN_CHANNELS);
28010c341cb2SMax Laier 	}
28020c341cb2SMax Laier #endif
2803c10b1400SMax Laier 
280468e8e04eSSam Leffler 	return (iwi_cmd(sc, IWI_CMD_SCAN_EXT, &scan, sizeof scan));
2805c10b1400SMax Laier }
2806c10b1400SMax Laier 
2807c10b1400SMax Laier static int
iwi_set_sensitivity(struct iwi_softc * sc,int8_t rssi_dbm)2808c10b1400SMax Laier iwi_set_sensitivity(struct iwi_softc *sc, int8_t rssi_dbm)
2809c10b1400SMax Laier {
2810c10b1400SMax Laier 	struct iwi_sensitivity sens;
2811c10b1400SMax Laier 
2812c10b1400SMax Laier 	DPRINTF(("Setting sensitivity to %d\n", rssi_dbm));
2813c10b1400SMax Laier 
2814c10b1400SMax Laier 	memset(&sens, 0, sizeof sens);
2815c10b1400SMax Laier 	sens.rssi = htole16(rssi_dbm);
2816c10b1400SMax Laier 	return iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &sens, sizeof sens);
2817ceaec73dSDamien Bergamini }
2818ceaec73dSDamien Bergamini 
2819ceaec73dSDamien Bergamini static int
iwi_auth_and_assoc(struct iwi_softc * sc,struct ieee80211vap * vap)2820b032f27cSSam Leffler iwi_auth_and_assoc(struct iwi_softc *sc, struct ieee80211vap *vap)
2821ceaec73dSDamien Bergamini {
2822b032f27cSSam Leffler 	struct ieee80211com *ic = vap->iv_ic;
282376614fdcSJustin Hibbits 	if_t ifp = vap->iv_ifp;
282439bb444cSAdrian Chadd 	struct ieee80211_node *ni;
2825ceaec73dSDamien Bergamini 	struct iwi_configuration config;
2826c10b1400SMax Laier 	struct iwi_associate *assoc = &sc->assoc;
2827acea4241SDamien Bergamini 	uint16_t capinfo;
2828b032f27cSSam Leffler 	uint32_t data;
2829163ee276SAndrew Thompson 	int error, mode;
2830ceaec73dSDamien Bergamini 
283168e8e04eSSam Leffler 	IWI_LOCK_ASSERT(sc);
283268e8e04eSSam Leffler 
283368e8e04eSSam Leffler 	if (sc->flags & IWI_FLAG_ASSOCIATED) {
283468e8e04eSSam Leffler 		DPRINTF(("Already associated\n"));
283568e8e04eSSam Leffler 		return (-1);
283668e8e04eSSam Leffler 	}
283768e8e04eSSam Leffler 
283812df38adSAndriy Voskoboinyk 	ni = ieee80211_ref_node(vap->iv_bss);
283912df38adSAndriy Voskoboinyk 
284068e8e04eSSam Leffler 	IWI_STATE_BEGIN(sc, IWI_FW_ASSOCIATING);
284168e8e04eSSam Leffler 	error = 0;
2842163ee276SAndrew Thompson 	mode = 0;
2843163ee276SAndrew Thompson 
2844163ee276SAndrew Thompson 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan))
2845163ee276SAndrew Thompson 		mode = IWI_MODE_11A;
2846163ee276SAndrew Thompson 	else if (IEEE80211_IS_CHAN_G(ic->ic_curchan))
2847163ee276SAndrew Thompson 		mode = IWI_MODE_11G;
2848163ee276SAndrew Thompson 	if (IEEE80211_IS_CHAN_B(ic->ic_curchan))
2849163ee276SAndrew Thompson 		mode = IWI_MODE_11B;
2850163ee276SAndrew Thompson 
285168e8e04eSSam Leffler 	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2852ceaec73dSDamien Bergamini 		memset(&config, 0, sizeof config);
2853ceaec73dSDamien Bergamini 		config.bluetooth_coexistence = sc->bluetooth;
2854acea4241SDamien Bergamini 		config.antenna = sc->antenna;
2855ceaec73dSDamien Bergamini 		config.multicast_enabled = 1;
2856163ee276SAndrew Thompson 		if (mode == IWI_MODE_11G)
2857ceaec73dSDamien Bergamini 			config.use_protection = 1;
2858acea4241SDamien Bergamini 		config.answer_pbreq =
2859b032f27cSSam Leffler 		    (vap->iv_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2860acea4241SDamien Bergamini 		config.disable_unicast_decryption = 1;
2861acea4241SDamien Bergamini 		config.disable_multicast_decryption = 1;
2862ceaec73dSDamien Bergamini 		DPRINTF(("Configuring adapter\n"));
2863c10b1400SMax Laier 		error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2864ceaec73dSDamien Bergamini 		if (error != 0)
286568e8e04eSSam Leffler 			goto done;
2866ceaec73dSDamien Bergamini 	}
2867ceaec73dSDamien Bergamini 
2868ceaec73dSDamien Bergamini #ifdef IWI_DEBUG
2869ceaec73dSDamien Bergamini 	if (iwi_debug > 0) {
2870ceaec73dSDamien Bergamini 		printf("Setting ESSID to ");
2871ceaec73dSDamien Bergamini 		ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2872ceaec73dSDamien Bergamini 		printf("\n");
2873ceaec73dSDamien Bergamini 	}
2874ceaec73dSDamien Bergamini #endif
2875c10b1400SMax Laier 	error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen);
2876ceaec73dSDamien Bergamini 	if (error != 0)
287768e8e04eSSam Leffler 		goto done;
2878ceaec73dSDamien Bergamini 
2879b032f27cSSam Leffler 	error = iwi_setpowermode(sc, vap);
2880b032f27cSSam Leffler 	if (error != 0)
2881b032f27cSSam Leffler 		goto done;
2882b032f27cSSam Leffler 
2883b032f27cSSam Leffler 	data = htole32(vap->iv_rtsthreshold);
2884b032f27cSSam Leffler 	DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2885b032f27cSSam Leffler 	error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2886b032f27cSSam Leffler 	if (error != 0)
2887b032f27cSSam Leffler 		goto done;
2888b032f27cSSam Leffler 
2889b032f27cSSam Leffler 	data = htole32(vap->iv_fragthreshold);
2890b032f27cSSam Leffler 	DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2891b032f27cSSam Leffler 	error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2892b032f27cSSam Leffler 	if (error != 0)
2893b032f27cSSam Leffler 		goto done;
2894b032f27cSSam Leffler 
289580e1a712SDamien Bergamini 	/* the rate set has already been "negotiated" */
2896222a2f1dSAndriy Voskoboinyk 	error = iwi_set_rateset(sc, &ni->ni_rates, mode,
2897222a2f1dSAndriy Voskoboinyk 	    IWI_RATESET_TYPE_NEGOTIATED);
2898ceaec73dSDamien Bergamini 	if (error != 0)
289968e8e04eSSam Leffler 		goto done;
2900ceaec73dSDamien Bergamini 
2901c10b1400SMax Laier 	memset(assoc, 0, sizeof *assoc);
2902c10b1400SMax Laier 
2903b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_WME) && ni->ni_ies.wme_ie != NULL) {
2904c10b1400SMax Laier 		/* NB: don't treat WME setup as failure */
29056468c413SGleb Smirnoff 		if (iwi_wme_setparams(sc) == 0 && iwi_wme_setie(sc) == 0)
2906c10b1400SMax Laier 			assoc->policy |= htole16(IWI_POLICY_WME);
2907c10b1400SMax Laier 		/* XXX complain on failure? */
2908ec01dc2bSDamien Bergamini 	}
2909ec01dc2bSDamien Bergamini 
2910b032f27cSSam Leffler 	if (vap->iv_appie_wpa != NULL) {
2911b032f27cSSam Leffler 		struct ieee80211_appie *ie = vap->iv_appie_wpa;
2912b032f27cSSam Leffler 
2913b032f27cSSam Leffler 		DPRINTF(("Setting optional IE (len=%u)\n", ie->ie_len));
2914b032f27cSSam Leffler 		error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ie->ie_data, ie->ie_len);
2915acea4241SDamien Bergamini 		if (error != 0)
291668e8e04eSSam Leffler 			goto done;
2917acea4241SDamien Bergamini 	}
2918acea4241SDamien Bergamini 
2919b032f27cSSam Leffler 	error = iwi_set_sensitivity(sc, ic->ic_node_getrssi(ni));
2920ceaec73dSDamien Bergamini 	if (error != 0)
292168e8e04eSSam Leffler 		goto done;
2922ceaec73dSDamien Bergamini 
2923163ee276SAndrew Thompson 	assoc->mode = mode;
292468e8e04eSSam Leffler 	assoc->chan = ic->ic_curchan->ic_ieee;
2925c10b1400SMax Laier 	/*
2926c10b1400SMax Laier 	 * NB: do not arrange for shared key auth w/o privacy
2927c10b1400SMax Laier 	 *     (i.e. a wep key); it causes a firmware error.
2928c10b1400SMax Laier 	 */
2929b032f27cSSam Leffler 	if ((vap->iv_flags & IEEE80211_F_PRIVACY) &&
2930c10b1400SMax Laier 	    ni->ni_authmode == IEEE80211_AUTH_SHARED) {
2931c10b1400SMax Laier 		assoc->auth = IWI_AUTH_SHARED;
2932c10b1400SMax Laier 		/*
2933c10b1400SMax Laier 		 * It's possible to have privacy marked but no default
2934c10b1400SMax Laier 		 * key setup.  This typically is due to a user app bug
2935c10b1400SMax Laier 		 * but if we blindly grab the key the firmware will
2936c10b1400SMax Laier 		 * barf so avoid it for now.
2937c10b1400SMax Laier 		 */
2938b032f27cSSam Leffler 		if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE)
2939b032f27cSSam Leffler 			assoc->auth |= vap->iv_def_txkey << 4;
2940c10b1400SMax Laier 
2941b032f27cSSam Leffler 		error = iwi_setwepkeys(sc, vap);
2942c10b1400SMax Laier 		if (error != 0)
294368e8e04eSSam Leffler 			goto done;
2944c10b1400SMax Laier 	}
2945b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_WPA)
2946c10b1400SMax Laier 		assoc->policy |= htole16(IWI_POLICY_WPA);
2947b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0)
2948c10b1400SMax Laier 		assoc->type = IWI_HC_IBSS_START;
2949c10b1400SMax Laier 	else
2950c10b1400SMax Laier 		assoc->type = IWI_HC_ASSOC;
2951c10b1400SMax Laier 	memcpy(assoc->tstamp, ni->ni_tstamp.data, 8);
2952acea4241SDamien Bergamini 
2953b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS)
2954acea4241SDamien Bergamini 		capinfo = IEEE80211_CAPINFO_IBSS;
2955acea4241SDamien Bergamini 	else
2956acea4241SDamien Bergamini 		capinfo = IEEE80211_CAPINFO_ESS;
2957b032f27cSSam Leffler 	if (vap->iv_flags & IEEE80211_F_PRIVACY)
2958acea4241SDamien Bergamini 		capinfo |= IEEE80211_CAPINFO_PRIVACY;
2959acea4241SDamien Bergamini 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
296068e8e04eSSam Leffler 	    IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2961acea4241SDamien Bergamini 		capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2962c10b1400SMax Laier 	if (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2963acea4241SDamien Bergamini 		capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2964c10b1400SMax Laier 	assoc->capinfo = htole16(capinfo);
2965acea4241SDamien Bergamini 
2966c10b1400SMax Laier 	assoc->lintval = htole16(ic->ic_lintval);
2967c10b1400SMax Laier 	assoc->intval = htole16(ni->ni_intval);
2968c10b1400SMax Laier 	IEEE80211_ADDR_COPY(assoc->bssid, ni->ni_bssid);
2969b032f27cSSam Leffler 	if (vap->iv_opmode == IEEE80211_M_IBSS)
297076614fdcSJustin Hibbits 		IEEE80211_ADDR_COPY(assoc->dst, if_getbroadcastaddr(ifp));
2971ceaec73dSDamien Bergamini 	else
2972c10b1400SMax Laier 		IEEE80211_ADDR_COPY(assoc->dst, ni->ni_bssid);
2973ceaec73dSDamien Bergamini 
2974c10b1400SMax Laier 	DPRINTF(("%s bssid %6D dst %6D channel %u policy 0x%x "
2975c10b1400SMax Laier 	    "auth %u capinfo 0x%x lintval %u bintval %u\n",
2976c10b1400SMax Laier 	    assoc->type == IWI_HC_IBSS_START ? "Start" : "Join",
2977c10b1400SMax Laier 	    assoc->bssid, ":", assoc->dst, ":",
2978c10b1400SMax Laier 	    assoc->chan, le16toh(assoc->policy), assoc->auth,
2979c10b1400SMax Laier 	    le16toh(assoc->capinfo), le16toh(assoc->lintval),
2980c10b1400SMax Laier 	    le16toh(assoc->intval)));
298168e8e04eSSam Leffler 	error = iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
298268e8e04eSSam Leffler done:
298339bb444cSAdrian Chadd 	ieee80211_free_node(ni);
298468e8e04eSSam Leffler 	if (error)
298568e8e04eSSam Leffler 		IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
298668e8e04eSSam Leffler 
298768e8e04eSSam Leffler 	return (error);
2988ceaec73dSDamien Bergamini }
2989ceaec73dSDamien Bergamini 
29905efea30fSAndrew Thompson static void
iwi_disassoc(void * arg,int pending)29915efea30fSAndrew Thompson iwi_disassoc(void *arg, int pending)
29925efea30fSAndrew Thompson {
29935efea30fSAndrew Thompson 	struct iwi_softc *sc = arg;
29945efea30fSAndrew Thompson 	IWI_LOCK_DECL;
29955efea30fSAndrew Thompson 
29965efea30fSAndrew Thompson 	IWI_LOCK(sc);
29975efea30fSAndrew Thompson 	iwi_disassociate(sc, 0);
29985efea30fSAndrew Thompson 	IWI_UNLOCK(sc);
29995efea30fSAndrew Thompson }
30005efea30fSAndrew Thompson 
3001c10b1400SMax Laier static int
iwi_disassociate(struct iwi_softc * sc,int quiet)3002c10b1400SMax Laier iwi_disassociate(struct iwi_softc *sc, int quiet)
30031b996b2aSDamien Bergamini {
3004c10b1400SMax Laier 	struct iwi_associate *assoc = &sc->assoc;
3005c10b1400SMax Laier 
300668e8e04eSSam Leffler 	if ((sc->flags & IWI_FLAG_ASSOCIATED) == 0) {
300768e8e04eSSam Leffler 		DPRINTF(("Not associated\n"));
300868e8e04eSSam Leffler 		return (-1);
300968e8e04eSSam Leffler 	}
301068e8e04eSSam Leffler 
301168e8e04eSSam Leffler 	IWI_STATE_BEGIN(sc, IWI_FW_DISASSOCIATING);
301268e8e04eSSam Leffler 
3013c10b1400SMax Laier 	if (quiet)
3014c10b1400SMax Laier 		assoc->type = IWI_HC_DISASSOC_QUIET;
3015c10b1400SMax Laier 	else
3016c10b1400SMax Laier 		assoc->type = IWI_HC_DISASSOC;
3017c10b1400SMax Laier 
3018c10b1400SMax Laier 	DPRINTF(("Trying to disassociate from %6D channel %u\n",
3019c10b1400SMax Laier 	    assoc->bssid, ":", assoc->chan));
3020c10b1400SMax Laier 	return iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
3021c10b1400SMax Laier }
3022c10b1400SMax Laier 
3023484f6530SLuigi Rizzo /*
3024484f6530SLuigi Rizzo  * release dma resources for the firmware
3025484f6530SLuigi Rizzo  */
3026484f6530SLuigi Rizzo static void
iwi_release_fw_dma(struct iwi_softc * sc)3027484f6530SLuigi Rizzo iwi_release_fw_dma(struct iwi_softc *sc)
3028484f6530SLuigi Rizzo {
3029484f6530SLuigi Rizzo 	if (sc->fw_flags & IWI_FW_HAVE_PHY)
3030484f6530SLuigi Rizzo 		bus_dmamap_unload(sc->fw_dmat, sc->fw_map);
3031484f6530SLuigi Rizzo 	if (sc->fw_flags & IWI_FW_HAVE_MAP)
3032484f6530SLuigi Rizzo 		bus_dmamem_free(sc->fw_dmat, sc->fw_virtaddr, sc->fw_map);
3033484f6530SLuigi Rizzo 	if (sc->fw_flags & IWI_FW_HAVE_DMAT)
3034484f6530SLuigi Rizzo 		bus_dma_tag_destroy(sc->fw_dmat);
3035484f6530SLuigi Rizzo 
3036484f6530SLuigi Rizzo 	sc->fw_flags = 0;
3037484f6530SLuigi Rizzo 	sc->fw_dma_size = 0;
3038484f6530SLuigi Rizzo 	sc->fw_dmat = NULL;
3039484f6530SLuigi Rizzo 	sc->fw_map = NULL;
3040484f6530SLuigi Rizzo 	sc->fw_physaddr = 0;
3041484f6530SLuigi Rizzo 	sc->fw_virtaddr = NULL;
3042484f6530SLuigi Rizzo }
3043484f6530SLuigi Rizzo 
3044484f6530SLuigi Rizzo /*
3045484f6530SLuigi Rizzo  * allocate the dma descriptor for the firmware.
3046484f6530SLuigi Rizzo  * Return 0 on success, 1 on error.
3047484f6530SLuigi Rizzo  * Must be called unlocked, protected by IWI_FLAG_FW_LOADING.
3048484f6530SLuigi Rizzo  */
3049484f6530SLuigi Rizzo static int
iwi_init_fw_dma(struct iwi_softc * sc,int size)3050484f6530SLuigi Rizzo iwi_init_fw_dma(struct iwi_softc *sc, int size)
3051484f6530SLuigi Rizzo {
3052899ff586SJohn Baldwin 	if (sc->fw_dma_size >= size)
3053484f6530SLuigi Rizzo 		return 0;
3054484f6530SLuigi Rizzo 	if (bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
3055484f6530SLuigi Rizzo 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
3056484f6530SLuigi Rizzo 	    size, 1, size, 0, NULL, NULL, &sc->fw_dmat) != 0) {
3057484f6530SLuigi Rizzo 		device_printf(sc->sc_dev,
3058484f6530SLuigi Rizzo 		    "could not create firmware DMA tag\n");
3059484f6530SLuigi Rizzo 		goto error;
3060484f6530SLuigi Rizzo 	}
3061484f6530SLuigi Rizzo 	sc->fw_flags |= IWI_FW_HAVE_DMAT;
3062484f6530SLuigi Rizzo 	if (bus_dmamem_alloc(sc->fw_dmat, &sc->fw_virtaddr, 0,
3063484f6530SLuigi Rizzo 	    &sc->fw_map) != 0) {
3064484f6530SLuigi Rizzo 		device_printf(sc->sc_dev,
3065484f6530SLuigi Rizzo 		    "could not allocate firmware DMA memory\n");
3066484f6530SLuigi Rizzo 		goto error;
3067484f6530SLuigi Rizzo 	}
3068484f6530SLuigi Rizzo 	sc->fw_flags |= IWI_FW_HAVE_MAP;
3069484f6530SLuigi Rizzo 	if (bus_dmamap_load(sc->fw_dmat, sc->fw_map, sc->fw_virtaddr,
3070484f6530SLuigi Rizzo 	    size, iwi_dma_map_addr, &sc->fw_physaddr, 0) != 0) {
3071484f6530SLuigi Rizzo 		device_printf(sc->sc_dev, "could not load firmware DMA map\n");
3072484f6530SLuigi Rizzo 		goto error;
3073484f6530SLuigi Rizzo 	}
3074484f6530SLuigi Rizzo 	sc->fw_flags |= IWI_FW_HAVE_PHY;
3075484f6530SLuigi Rizzo 	sc->fw_dma_size = size;
3076484f6530SLuigi Rizzo 	return 0;
3077484f6530SLuigi Rizzo 
3078484f6530SLuigi Rizzo error:
3079484f6530SLuigi Rizzo 	iwi_release_fw_dma(sc);
3080484f6530SLuigi Rizzo 	return 1;
3081484f6530SLuigi Rizzo }
3082484f6530SLuigi Rizzo 
3083c10b1400SMax Laier static void
iwi_init_locked(struct iwi_softc * sc)3084b032f27cSSam Leffler iwi_init_locked(struct iwi_softc *sc)
3085c10b1400SMax Laier {
3086ceaec73dSDamien Bergamini 	struct iwi_rx_data *data;
3087c10b1400SMax Laier 	int i;
3088c56adf0aSDamien Bergamini 
308968e8e04eSSam Leffler 	IWI_LOCK_ASSERT(sc);
3090b032f27cSSam Leffler 
309168e8e04eSSam Leffler 	if (sc->fw_state == IWI_FW_LOADING) {
3092484f6530SLuigi Rizzo 		device_printf(sc->sc_dev, "%s: already loading\n", __func__);
3093c10b1400SMax Laier 		return;		/* XXX: condvar? */
3094484f6530SLuigi Rizzo 	}
3095ceaec73dSDamien Bergamini 
3096b032f27cSSam Leffler 	iwi_stop_locked(sc);
3097b032f27cSSam Leffler 
309868e8e04eSSam Leffler 	IWI_STATE_BEGIN(sc, IWI_FW_LOADING);
3099ceaec73dSDamien Bergamini 
3100ceaec73dSDamien Bergamini 	if (iwi_reset(sc) != 0) {
3101ceaec73dSDamien Bergamini 		device_printf(sc->sc_dev, "could not reset adapter\n");
3102c10b1400SMax Laier 		goto fail;
3103ceaec73dSDamien Bergamini 	}
3104c10b1400SMax Laier 	if (iwi_load_firmware(sc, &sc->fw_boot) != 0) {
3105bef9529dSDamien Bergamini 		device_printf(sc->sc_dev,
3106c10b1400SMax Laier 		    "could not load boot firmware %s\n", sc->fw_boot.name);
3107484f6530SLuigi Rizzo 		goto fail;
3108bef9529dSDamien Bergamini 	}
3109c10b1400SMax Laier 	if (iwi_load_ucode(sc, &sc->fw_uc) != 0) {
3110c10b1400SMax Laier 		device_printf(sc->sc_dev,
3111c10b1400SMax Laier 		    "could not load microcode %s\n", sc->fw_uc.name);
3112484f6530SLuigi Rizzo 		goto fail;
3113ceaec73dSDamien Bergamini 	}
3114ceaec73dSDamien Bergamini 
3115ceaec73dSDamien Bergamini 	iwi_stop_master(sc);
3116ceaec73dSDamien Bergamini 
3117ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr);
3118ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
3119ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
3120ceaec73dSDamien Bergamini 
3121ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr);
3122ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
3123ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
3124ceaec73dSDamien Bergamini 
3125ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr);
3126ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
3127ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
3128ceaec73dSDamien Bergamini 
3129ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr);
3130ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
3131ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
3132ceaec73dSDamien Bergamini 
3133ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr);
3134ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
3135ec01dc2bSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
3136ceaec73dSDamien Bergamini 
3137ceaec73dSDamien Bergamini 	for (i = 0; i < sc->rxq.count; i++) {
3138ceaec73dSDamien Bergamini 		data = &sc->rxq.data[i];
3139ceaec73dSDamien Bergamini 		CSR_WRITE_4(sc, data->reg, data->physaddr);
3140ceaec73dSDamien Bergamini 	}
3141ceaec73dSDamien Bergamini 
3142ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1);
3143ceaec73dSDamien Bergamini 
3144c10b1400SMax Laier 	if (iwi_load_firmware(sc, &sc->fw_fw) != 0) {
3145c10b1400SMax Laier 		device_printf(sc->sc_dev,
3146c10b1400SMax Laier 		    "could not load main firmware %s\n", sc->fw_fw.name);
3147484f6530SLuigi Rizzo 		goto fail;
3148ceaec73dSDamien Bergamini 	}
3149ceaec73dSDamien Bergamini 	sc->flags |= IWI_FLAG_FW_INITED;
3150ceaec73dSDamien Bergamini 
3151b032f27cSSam Leffler 	IWI_STATE_END(sc, IWI_FW_LOADING);
3152ceaec73dSDamien Bergamini 
3153b032f27cSSam Leffler 	if (iwi_config(sc) != 0) {
3154b032f27cSSam Leffler 		device_printf(sc->sc_dev, "unable to enable adapter\n");
3155b032f27cSSam Leffler 		goto fail2;
3156b032f27cSSam Leffler 	}
3157ceaec73dSDamien Bergamini 
315868e8e04eSSam Leffler 	callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
31597a79cebfSGleb Smirnoff 	sc->sc_running = 1;
3160ceaec73dSDamien Bergamini 	return;
3161b032f27cSSam Leffler fail:
316268e8e04eSSam Leffler 	IWI_STATE_END(sc, IWI_FW_LOADING);
3163b032f27cSSam Leffler fail2:
3164b032f27cSSam Leffler 	iwi_stop_locked(sc);
3165ceaec73dSDamien Bergamini }
3166ceaec73dSDamien Bergamini 
3167ceaec73dSDamien Bergamini static void
iwi_init(void * priv)3168b032f27cSSam Leffler iwi_init(void *priv)
3169ceaec73dSDamien Bergamini {
3170ceaec73dSDamien Bergamini 	struct iwi_softc *sc = priv;
31717a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3172b032f27cSSam Leffler 	IWI_LOCK_DECL;
3173b032f27cSSam Leffler 
3174b032f27cSSam Leffler 	IWI_LOCK(sc);
3175b032f27cSSam Leffler 	iwi_init_locked(sc);
3176b032f27cSSam Leffler 	IWI_UNLOCK(sc);
3177b032f27cSSam Leffler 
31787a79cebfSGleb Smirnoff 	if (sc->sc_running)
3179b032f27cSSam Leffler 		ieee80211_start_all(ic);
3180b032f27cSSam Leffler }
3181b032f27cSSam Leffler 
3182b032f27cSSam Leffler static void
iwi_stop_locked(void * priv)3183b032f27cSSam Leffler iwi_stop_locked(void *priv)
3184b032f27cSSam Leffler {
3185b032f27cSSam Leffler 	struct iwi_softc *sc = priv;
3186ceaec73dSDamien Bergamini 
318768e8e04eSSam Leffler 	IWI_LOCK_ASSERT(sc);
3188b032f27cSSam Leffler 
31897a79cebfSGleb Smirnoff 	sc->sc_running = 0;
3190b032f27cSSam Leffler 
3191c10b1400SMax Laier 	if (sc->sc_softled) {
3192c10b1400SMax Laier 		callout_stop(&sc->sc_ledtimer);
3193c10b1400SMax Laier 		sc->sc_blinking = 0;
3194c10b1400SMax Laier 	}
319568e8e04eSSam Leffler 	callout_stop(&sc->sc_wdtimer);
3196b032f27cSSam Leffler 	callout_stop(&sc->sc_rftimer);
3197b032f27cSSam Leffler 
3198ceaec73dSDamien Bergamini 	iwi_stop_master(sc);
3199ceaec73dSDamien Bergamini 
3200ceaec73dSDamien Bergamini 	CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET);
3201ceaec73dSDamien Bergamini 
3202ceaec73dSDamien Bergamini 	/* reset rings */
3203ceaec73dSDamien Bergamini 	iwi_reset_cmd_ring(sc, &sc->cmdq);
3204ec01dc2bSDamien Bergamini 	iwi_reset_tx_ring(sc, &sc->txq[0]);
3205ec01dc2bSDamien Bergamini 	iwi_reset_tx_ring(sc, &sc->txq[1]);
3206ec01dc2bSDamien Bergamini 	iwi_reset_tx_ring(sc, &sc->txq[2]);
3207ec01dc2bSDamien Bergamini 	iwi_reset_tx_ring(sc, &sc->txq[3]);
3208ceaec73dSDamien Bergamini 	iwi_reset_rx_ring(sc, &sc->rxq);
3209ceaec73dSDamien Bergamini 
3210c10b1400SMax Laier 	sc->sc_tx_timer = 0;
321168e8e04eSSam Leffler 	sc->sc_state_timer = 0;
321268e8e04eSSam Leffler 	sc->sc_busy_timer = 0;
321368e8e04eSSam Leffler 	sc->flags &= ~(IWI_FLAG_BUSY | IWI_FLAG_ASSOCIATED);
321468e8e04eSSam Leffler 	sc->fw_state = IWI_FW_IDLE;
321568e8e04eSSam Leffler 	wakeup(sc);
3216b032f27cSSam Leffler }
3217c10b1400SMax Laier 
3218b032f27cSSam Leffler static void
iwi_stop(struct iwi_softc * sc)3219b032f27cSSam Leffler iwi_stop(struct iwi_softc *sc)
3220b032f27cSSam Leffler {
3221b032f27cSSam Leffler 	IWI_LOCK_DECL;
3222b032f27cSSam Leffler 
3223b032f27cSSam Leffler 	IWI_LOCK(sc);
3224b032f27cSSam Leffler 	iwi_stop_locked(sc);
3225b032f27cSSam Leffler 	IWI_UNLOCK(sc);
3226c10b1400SMax Laier }
3227c10b1400SMax Laier 
3228c10b1400SMax Laier static void
iwi_restart(void * arg,int npending)3229c10b1400SMax Laier iwi_restart(void *arg, int npending)
3230c10b1400SMax Laier {
3231c10b1400SMax Laier 	struct iwi_softc *sc = arg;
3232c10b1400SMax Laier 
3233b032f27cSSam Leffler 	iwi_init(sc);
3234c10b1400SMax Laier }
3235c10b1400SMax Laier 
3236c10b1400SMax Laier /*
3237c10b1400SMax Laier  * Return whether or not the radio is enabled in hardware
3238c10b1400SMax Laier  * (i.e. the rfkill switch is "off").
3239c10b1400SMax Laier  */
3240c10b1400SMax Laier static int
iwi_getrfkill(struct iwi_softc * sc)3241c10b1400SMax Laier iwi_getrfkill(struct iwi_softc *sc)
3242c10b1400SMax Laier {
3243c10b1400SMax Laier 	return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0;
3244c10b1400SMax Laier }
3245c10b1400SMax Laier 
3246c10b1400SMax Laier static void
iwi_radio_on(void * arg,int pending)3247c10b1400SMax Laier iwi_radio_on(void *arg, int pending)
3248c10b1400SMax Laier {
3249c10b1400SMax Laier 	struct iwi_softc *sc = arg;
32507a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3251c10b1400SMax Laier 
3252c10b1400SMax Laier 	device_printf(sc->sc_dev, "radio turned on\n");
3253b032f27cSSam Leffler 
3254c10b1400SMax Laier 	iwi_init(sc);
3255b032f27cSSam Leffler 	ieee80211_notify_radio(ic, 1);
3256b032f27cSSam Leffler }
3257b032f27cSSam Leffler 
3258b032f27cSSam Leffler static void
iwi_rfkill_poll(void * arg)3259b032f27cSSam Leffler iwi_rfkill_poll(void *arg)
3260b032f27cSSam Leffler {
3261b032f27cSSam Leffler 	struct iwi_softc *sc = arg;
3262b032f27cSSam Leffler 
3263b032f27cSSam Leffler 	IWI_LOCK_ASSERT(sc);
3264b032f27cSSam Leffler 
3265b032f27cSSam Leffler 	/*
3266b032f27cSSam Leffler 	 * Check for a change in rfkill state.  We get an
3267b032f27cSSam Leffler 	 * interrupt when a radio is disabled but not when
3268b032f27cSSam Leffler 	 * it is enabled so we must poll for the latter.
3269b032f27cSSam Leffler 	 */
3270b032f27cSSam Leffler 	if (!iwi_getrfkill(sc)) {
32717a79cebfSGleb Smirnoff 		ieee80211_runtask(&sc->sc_ic, &sc->sc_radiontask);
3272b032f27cSSam Leffler 		return;
3273b032f27cSSam Leffler 	}
3274b032f27cSSam Leffler 	callout_reset(&sc->sc_rftimer, 2*hz, iwi_rfkill_poll, sc);
3275c10b1400SMax Laier }
3276c10b1400SMax Laier 
3277c10b1400SMax Laier static void
iwi_radio_off(void * arg,int pending)3278c10b1400SMax Laier iwi_radio_off(void *arg, int pending)
3279c10b1400SMax Laier {
3280c10b1400SMax Laier 	struct iwi_softc *sc = arg;
32817a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
328268e8e04eSSam Leffler 	IWI_LOCK_DECL;
3283c10b1400SMax Laier 
3284c10b1400SMax Laier 	device_printf(sc->sc_dev, "radio turned off\n");
3285b032f27cSSam Leffler 
3286b032f27cSSam Leffler 	ieee80211_notify_radio(ic, 0);
3287b032f27cSSam Leffler 
328868e8e04eSSam Leffler 	IWI_LOCK(sc);
3289b032f27cSSam Leffler 	iwi_stop_locked(sc);
3290b032f27cSSam Leffler 	iwi_rfkill_poll(sc);
329168e8e04eSSam Leffler 	IWI_UNLOCK(sc);
3292ceaec73dSDamien Bergamini }
3293ceaec73dSDamien Bergamini 
3294ceaec73dSDamien Bergamini static int
iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)3295ceaec73dSDamien Bergamini iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
3296ceaec73dSDamien Bergamini {
3297ceaec73dSDamien Bergamini 	struct iwi_softc *sc = arg1;
3298ceaec73dSDamien Bergamini 	uint32_t size, buf[128];
3299ceaec73dSDamien Bergamini 
3300ceaec73dSDamien Bergamini 	memset(buf, 0, sizeof buf);
33016c3c35e4SPhilip Paeps 
33026c3c35e4SPhilip Paeps 	if (!(sc->flags & IWI_FLAG_FW_INITED))
3303ceaec73dSDamien Bergamini 		return SYSCTL_OUT(req, buf, sizeof buf);
3304ceaec73dSDamien Bergamini 
3305ceaec73dSDamien Bergamini 	size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
3306ceaec73dSDamien Bergamini 	CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
3307ceaec73dSDamien Bergamini 
33086c3c35e4SPhilip Paeps 	return SYSCTL_OUT(req, buf, size);
3309ceaec73dSDamien Bergamini }
3310ceaec73dSDamien Bergamini 
3311ceaec73dSDamien Bergamini static int
iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)3312ceaec73dSDamien Bergamini iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
3313ceaec73dSDamien Bergamini {
3314ceaec73dSDamien Bergamini 	struct iwi_softc *sc = arg1;
3315c10b1400SMax Laier 	int val = !iwi_getrfkill(sc);
3316ceaec73dSDamien Bergamini 
3317ceaec73dSDamien Bergamini 	return SYSCTL_OUT(req, &val, sizeof val);
3318ceaec73dSDamien Bergamini }
3319c10b1400SMax Laier 
3320c10b1400SMax Laier /*
3321c10b1400SMax Laier  * Add sysctl knobs.
3322c10b1400SMax Laier  */
3323c10b1400SMax Laier static void
iwi_sysctlattach(struct iwi_softc * sc)3324c10b1400SMax Laier iwi_sysctlattach(struct iwi_softc *sc)
3325c10b1400SMax Laier {
3326c10b1400SMax Laier 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3327c10b1400SMax Laier 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3328c10b1400SMax Laier 
3329c10b1400SMax Laier 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "radio",
33307029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
33317029da5cSPawel Biernacki 	    iwi_sysctl_radio, "I",
3332c10b1400SMax Laier 	    "radio transmitter switch state (0=off, 1=on)");
3333c10b1400SMax Laier 
3334c10b1400SMax Laier 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "stats",
33357029da5cSPawel Biernacki 	    CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
33367029da5cSPawel Biernacki 	    iwi_sysctl_stats, "S", "statistics");
3337c10b1400SMax Laier 
3338c10b1400SMax Laier 	sc->bluetooth = 0;
3339c10b1400SMax Laier 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "bluetooth",
3340c10b1400SMax Laier 	    CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence");
3341c10b1400SMax Laier 
3342c10b1400SMax Laier 	sc->antenna = IWI_ANTENNA_AUTO;
3343c10b1400SMax Laier 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "antenna",
3344c10b1400SMax Laier 	    CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)");
3345c10b1400SMax Laier }
3346c10b1400SMax Laier 
3347c10b1400SMax Laier /*
3348c10b1400SMax Laier  * LED support.
3349c10b1400SMax Laier  *
3350c10b1400SMax Laier  * Different cards have different capabilities.  Some have three
3351c10b1400SMax Laier  * led's while others have only one.  The linux ipw driver defines
3352c10b1400SMax Laier  * led's for link state (associated or not), band (11a, 11g, 11b),
3353c10b1400SMax Laier  * and for link activity.  We use one led and vary the blink rate
3354c10b1400SMax Laier  * according to the tx/rx traffic a la the ath driver.
3355c10b1400SMax Laier  */
3356c10b1400SMax Laier 
3357c10b1400SMax Laier static __inline uint32_t
iwi_toggle_event(uint32_t r)3358c10b1400SMax Laier iwi_toggle_event(uint32_t r)
3359c10b1400SMax Laier {
3360c10b1400SMax Laier 	return r &~ (IWI_RST_STANDBY | IWI_RST_GATE_ODMA |
3361c10b1400SMax Laier 		     IWI_RST_GATE_IDMA | IWI_RST_GATE_ADMA);
3362c10b1400SMax Laier }
3363c10b1400SMax Laier 
3364c10b1400SMax Laier static uint32_t
iwi_read_event(struct iwi_softc * sc)3365c10b1400SMax Laier iwi_read_event(struct iwi_softc *sc)
3366c10b1400SMax Laier {
3367c10b1400SMax Laier 	return MEM_READ_4(sc, IWI_MEM_EEPROM_EVENT);
3368c10b1400SMax Laier }
3369c10b1400SMax Laier 
3370c10b1400SMax Laier static void
iwi_write_event(struct iwi_softc * sc,uint32_t v)3371c10b1400SMax Laier iwi_write_event(struct iwi_softc *sc, uint32_t v)
3372c10b1400SMax Laier {
3373c10b1400SMax Laier 	MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, v);
3374c10b1400SMax Laier }
3375c10b1400SMax Laier 
3376c10b1400SMax Laier static void
iwi_led_done(void * arg)3377c10b1400SMax Laier iwi_led_done(void *arg)
3378c10b1400SMax Laier {
3379c10b1400SMax Laier 	struct iwi_softc *sc = arg;
3380c10b1400SMax Laier 
3381c10b1400SMax Laier 	sc->sc_blinking = 0;
3382c10b1400SMax Laier }
3383c10b1400SMax Laier 
3384c10b1400SMax Laier /*
3385c10b1400SMax Laier  * Turn the activity LED off: flip the pin and then set a timer so no
3386c10b1400SMax Laier  * update will happen for the specified duration.
3387c10b1400SMax Laier  */
3388c10b1400SMax Laier static void
iwi_led_off(void * arg)3389c10b1400SMax Laier iwi_led_off(void *arg)
3390c10b1400SMax Laier {
3391c10b1400SMax Laier 	struct iwi_softc *sc = arg;
3392c10b1400SMax Laier 	uint32_t v;
3393c10b1400SMax Laier 
3394c10b1400SMax Laier 	v = iwi_read_event(sc);
3395c10b1400SMax Laier 	v &= ~sc->sc_ledpin;
3396c10b1400SMax Laier 	iwi_write_event(sc, iwi_toggle_event(v));
3397c10b1400SMax Laier 	callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, iwi_led_done, sc);
3398c10b1400SMax Laier }
3399c10b1400SMax Laier 
3400c10b1400SMax Laier /*
3401c10b1400SMax Laier  * Blink the LED according to the specified on/off times.
3402c10b1400SMax Laier  */
3403c10b1400SMax Laier static void
iwi_led_blink(struct iwi_softc * sc,int on,int off)3404c10b1400SMax Laier iwi_led_blink(struct iwi_softc *sc, int on, int off)
3405c10b1400SMax Laier {
3406c10b1400SMax Laier 	uint32_t v;
3407c10b1400SMax Laier 
3408c10b1400SMax Laier 	v = iwi_read_event(sc);
3409c10b1400SMax Laier 	v |= sc->sc_ledpin;
3410c10b1400SMax Laier 	iwi_write_event(sc, iwi_toggle_event(v));
3411c10b1400SMax Laier 	sc->sc_blinking = 1;
3412c10b1400SMax Laier 	sc->sc_ledoff = off;
3413c10b1400SMax Laier 	callout_reset(&sc->sc_ledtimer, on, iwi_led_off, sc);
3414c10b1400SMax Laier }
3415c10b1400SMax Laier 
3416c10b1400SMax Laier static void
iwi_led_event(struct iwi_softc * sc,int event)3417c10b1400SMax Laier iwi_led_event(struct iwi_softc *sc, int event)
3418c10b1400SMax Laier {
3419c10b1400SMax Laier 	/* NB: on/off times from the Atheros NDIS driver, w/ permission */
3420c10b1400SMax Laier 	static const struct {
3421c10b1400SMax Laier 		u_int		rate;		/* tx/rx iwi rate */
3422c10b1400SMax Laier 		u_int16_t	timeOn;		/* LED on time (ms) */
3423c10b1400SMax Laier 		u_int16_t	timeOff;	/* LED off time (ms) */
3424c10b1400SMax Laier 	} blinkrates[] = {
3425c10b1400SMax Laier 		{ IWI_RATE_OFDM54, 40,  10 },
3426c10b1400SMax Laier 		{ IWI_RATE_OFDM48, 44,  11 },
3427c10b1400SMax Laier 		{ IWI_RATE_OFDM36, 50,  13 },
3428c10b1400SMax Laier 		{ IWI_RATE_OFDM24, 57,  14 },
3429c10b1400SMax Laier 		{ IWI_RATE_OFDM18, 67,  16 },
3430c10b1400SMax Laier 		{ IWI_RATE_OFDM12, 80,  20 },
3431c10b1400SMax Laier 		{ IWI_RATE_DS11,  100,  25 },
3432c10b1400SMax Laier 		{ IWI_RATE_OFDM9, 133,  34 },
3433c10b1400SMax Laier 		{ IWI_RATE_OFDM6, 160,  40 },
3434c10b1400SMax Laier 		{ IWI_RATE_DS5,   200,  50 },
3435c10b1400SMax Laier 		{            6,   240,  58 },	/* XXX 3Mb/s if it existed */
3436c10b1400SMax Laier 		{ IWI_RATE_DS2,   267,  66 },
3437c10b1400SMax Laier 		{ IWI_RATE_DS1,   400, 100 },
3438c10b1400SMax Laier 		{            0,   500, 130 },	/* unknown rate/polling */
3439c10b1400SMax Laier 	};
3440c10b1400SMax Laier 	uint32_t txrate;
3441c10b1400SMax Laier 	int j = 0;			/* XXX silence compiler */
3442c10b1400SMax Laier 
3443c10b1400SMax Laier 	sc->sc_ledevent = ticks;	/* time of last event */
3444c10b1400SMax Laier 	if (sc->sc_blinking)		/* don't interrupt active blink */
3445c10b1400SMax Laier 		return;
3446c10b1400SMax Laier 	switch (event) {
3447c10b1400SMax Laier 	case IWI_LED_POLL:
3448d6166defSAdrian Chadd 		j = nitems(blinkrates)-1;
3449c10b1400SMax Laier 		break;
3450c10b1400SMax Laier 	case IWI_LED_TX:
3451c10b1400SMax Laier 		/* read current transmission rate from adapter */
3452c10b1400SMax Laier 		txrate = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
3453c10b1400SMax Laier 		if (blinkrates[sc->sc_txrix].rate != txrate) {
3454d6166defSAdrian Chadd 			for (j = 0; j < nitems(blinkrates)-1; j++)
3455c10b1400SMax Laier 				if (blinkrates[j].rate == txrate)
3456c10b1400SMax Laier 					break;
3457c10b1400SMax Laier 			sc->sc_txrix = j;
3458c10b1400SMax Laier 		} else
3459c10b1400SMax Laier 			j = sc->sc_txrix;
3460c10b1400SMax Laier 		break;
3461c10b1400SMax Laier 	case IWI_LED_RX:
3462c10b1400SMax Laier 		if (blinkrates[sc->sc_rxrix].rate != sc->sc_rxrate) {
3463d6166defSAdrian Chadd 			for (j = 0; j < nitems(blinkrates)-1; j++)
3464c10b1400SMax Laier 				if (blinkrates[j].rate == sc->sc_rxrate)
3465c10b1400SMax Laier 					break;
3466c10b1400SMax Laier 			sc->sc_rxrix = j;
3467c10b1400SMax Laier 		} else
3468c10b1400SMax Laier 			j = sc->sc_rxrix;
3469c10b1400SMax Laier 		break;
3470c10b1400SMax Laier 	}
3471c10b1400SMax Laier 	/* XXX beware of overflow */
3472c10b1400SMax Laier 	iwi_led_blink(sc, (blinkrates[j].timeOn * hz) / 1000,
3473c10b1400SMax Laier 		(blinkrates[j].timeOff * hz) / 1000);
3474c10b1400SMax Laier }
3475c10b1400SMax Laier 
3476c10b1400SMax Laier static int
iwi_sysctl_softled(SYSCTL_HANDLER_ARGS)3477c10b1400SMax Laier iwi_sysctl_softled(SYSCTL_HANDLER_ARGS)
3478c10b1400SMax Laier {
3479c10b1400SMax Laier 	struct iwi_softc *sc = arg1;
3480c10b1400SMax Laier 	int softled = sc->sc_softled;
3481c10b1400SMax Laier 	int error;
3482c10b1400SMax Laier 
3483c10b1400SMax Laier 	error = sysctl_handle_int(oidp, &softled, 0, req);
3484c10b1400SMax Laier 	if (error || !req->newptr)
3485c10b1400SMax Laier 		return error;
3486c10b1400SMax Laier 	softled = (softled != 0);
3487c10b1400SMax Laier 	if (softled != sc->sc_softled) {
3488c10b1400SMax Laier 		if (softled) {
3489c10b1400SMax Laier 			uint32_t v = iwi_read_event(sc);
3490c10b1400SMax Laier 			v &= ~sc->sc_ledpin;
3491c10b1400SMax Laier 			iwi_write_event(sc, iwi_toggle_event(v));
3492c10b1400SMax Laier 		}
3493c10b1400SMax Laier 		sc->sc_softled = softled;
3494c10b1400SMax Laier 	}
3495c10b1400SMax Laier 	return 0;
3496c10b1400SMax Laier }
3497c10b1400SMax Laier 
3498c10b1400SMax Laier static void
iwi_ledattach(struct iwi_softc * sc)3499c10b1400SMax Laier iwi_ledattach(struct iwi_softc *sc)
3500c10b1400SMax Laier {
3501c10b1400SMax Laier 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3502c10b1400SMax Laier 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3503c10b1400SMax Laier 
3504c10b1400SMax Laier 	sc->sc_blinking = 0;
3505c10b1400SMax Laier 	sc->sc_ledstate = 1;
3506c10b1400SMax Laier 	sc->sc_ledidle = (2700*hz)/1000;	/* 2.7sec */
3507c10b1400SMax Laier 	callout_init_mtx(&sc->sc_ledtimer, &sc->sc_mtx, 0);
3508c10b1400SMax Laier 
3509c10b1400SMax Laier 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
35107029da5cSPawel Biernacki 	    "softled", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
3511c10b1400SMax Laier 	    iwi_sysctl_softled, "I", "enable/disable software LED support");
35126dc7dc9aSMatthew D Fleming 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3513c10b1400SMax Laier 		"ledpin", CTLFLAG_RW, &sc->sc_ledpin, 0,
3514c10b1400SMax Laier 		"pin setting to turn activity LED on");
35156dc7dc9aSMatthew D Fleming 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3516c10b1400SMax Laier 		"ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0,
3517c10b1400SMax Laier 		"idle time for inactivity LED (ticks)");
3518c10b1400SMax Laier 	/* XXX for debugging */
35196dc7dc9aSMatthew D Fleming 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3520c10b1400SMax Laier 		"nictype", CTLFLAG_RD, &sc->sc_nictype, 0,
3521c10b1400SMax Laier 		"NIC type from EEPROM");
3522c10b1400SMax Laier 
3523c10b1400SMax Laier 	sc->sc_ledpin = IWI_RST_LED_ACTIVITY;
3524c10b1400SMax Laier 	sc->sc_softled = 1;
3525c10b1400SMax Laier 
3526c10b1400SMax Laier 	sc->sc_nictype = (iwi_read_prom_word(sc, IWI_EEPROM_NIC) >> 8) & 0xff;
3527c10b1400SMax Laier 	if (sc->sc_nictype == 1) {
3528c10b1400SMax Laier 		/*
3529c10b1400SMax Laier 		 * NB: led's are reversed.
3530c10b1400SMax Laier 		 */
3531c10b1400SMax Laier 		sc->sc_ledpin = IWI_RST_LED_ASSOCIATED;
3532c10b1400SMax Laier 	}
3533c10b1400SMax Laier }
353468e8e04eSSam Leffler 
353568e8e04eSSam Leffler static void
iwi_scan_start(struct ieee80211com * ic)353668e8e04eSSam Leffler iwi_scan_start(struct ieee80211com *ic)
353768e8e04eSSam Leffler {
35385efea30fSAndrew Thompson 	/* ignore */
353968e8e04eSSam Leffler }
354068e8e04eSSam Leffler 
354168e8e04eSSam Leffler static void
iwi_set_channel(struct ieee80211com * ic)354268e8e04eSSam Leffler iwi_set_channel(struct ieee80211com *ic)
354368e8e04eSSam Leffler {
35447a79cebfSGleb Smirnoff 	struct iwi_softc *sc = ic->ic_softc;
35457a79cebfSGleb Smirnoff 
354668e8e04eSSam Leffler 	if (sc->fw_state == IWI_FW_IDLE)
354768e8e04eSSam Leffler 		iwi_setcurchan(sc, ic->ic_curchan->ic_ieee);
354868e8e04eSSam Leffler }
354968e8e04eSSam Leffler 
355068e8e04eSSam Leffler static void
iwi_scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)3551b032f27cSSam Leffler iwi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
355268e8e04eSSam Leffler {
3553b032f27cSSam Leffler 	struct ieee80211vap *vap = ss->ss_vap;
35547a79cebfSGleb Smirnoff 	struct iwi_softc *sc = vap->iv_ic->ic_softc;
35555efea30fSAndrew Thompson 	IWI_LOCK_DECL;
355668e8e04eSSam Leffler 
35575efea30fSAndrew Thompson 	IWI_LOCK(sc);
35585efea30fSAndrew Thompson 	if (iwi_scanchan(sc, maxdwell, 0))
35595efea30fSAndrew Thompson 		ieee80211_cancel_scan(vap);
35605efea30fSAndrew Thompson 	IWI_UNLOCK(sc);
356168e8e04eSSam Leffler }
356268e8e04eSSam Leffler 
356368e8e04eSSam Leffler static void
iwi_scan_mindwell(struct ieee80211_scan_state * ss)3564b032f27cSSam Leffler iwi_scan_mindwell(struct ieee80211_scan_state *ss)
356568e8e04eSSam Leffler {
356668e8e04eSSam Leffler 	/* NB: don't try to abort scan; wait for firmware to finish */
356768e8e04eSSam Leffler }
356868e8e04eSSam Leffler 
356968e8e04eSSam Leffler static void
iwi_scan_end(struct ieee80211com * ic)357068e8e04eSSam Leffler iwi_scan_end(struct ieee80211com *ic)
357168e8e04eSSam Leffler {
35727a79cebfSGleb Smirnoff 	struct iwi_softc *sc = ic->ic_softc;
35735efea30fSAndrew Thompson 	IWI_LOCK_DECL;
357468e8e04eSSam Leffler 
35755efea30fSAndrew Thompson 	IWI_LOCK(sc);
35765efea30fSAndrew Thompson 	sc->flags &= ~IWI_FLAG_CHANNEL_SCAN;
35775efea30fSAndrew Thompson 	/* NB: make sure we're still scanning */
35785efea30fSAndrew Thompson 	if (sc->fw_state == IWI_FW_SCANNING)
35795efea30fSAndrew Thompson 		iwi_cmd(sc, IWI_CMD_ABORT_SCAN, NULL, 0);
35805efea30fSAndrew Thompson 	IWI_UNLOCK(sc);
358168e8e04eSSam Leffler }
35825dbc2ac9SAndriy Voskoboinyk 
35835dbc2ac9SAndriy Voskoboinyk static void
iwi_collect_bands(struct ieee80211com * ic,uint8_t bands[],size_t bands_sz)35845dbc2ac9SAndriy Voskoboinyk iwi_collect_bands(struct ieee80211com *ic, uint8_t bands[], size_t bands_sz)
35855dbc2ac9SAndriy Voskoboinyk {
35865dbc2ac9SAndriy Voskoboinyk 	struct iwi_softc *sc = ic->ic_softc;
35875dbc2ac9SAndriy Voskoboinyk 	device_t dev = sc->sc_dev;
35885dbc2ac9SAndriy Voskoboinyk 
35895dbc2ac9SAndriy Voskoboinyk 	memset(bands, 0, bands_sz);
35905dbc2ac9SAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11B);
35915dbc2ac9SAndriy Voskoboinyk 	setbit(bands, IEEE80211_MODE_11G);
35925dbc2ac9SAndriy Voskoboinyk 	if (pci_get_device(dev) >= 0x4223)
35935dbc2ac9SAndriy Voskoboinyk 		setbit(bands, IEEE80211_MODE_11A);
35945dbc2ac9SAndriy Voskoboinyk }
35955dbc2ac9SAndriy Voskoboinyk 
35965dbc2ac9SAndriy Voskoboinyk static void
iwi_getradiocaps(struct ieee80211com * ic,int maxchans,int * nchans,struct ieee80211_channel chans[])35975dbc2ac9SAndriy Voskoboinyk iwi_getradiocaps(struct ieee80211com *ic,
35985dbc2ac9SAndriy Voskoboinyk     int maxchans, int *nchans, struct ieee80211_channel chans[])
35995dbc2ac9SAndriy Voskoboinyk {
36005dbc2ac9SAndriy Voskoboinyk 	uint8_t bands[IEEE80211_MODE_BYTES];
36015dbc2ac9SAndriy Voskoboinyk 
36025dbc2ac9SAndriy Voskoboinyk 	iwi_collect_bands(ic, bands, sizeof(bands));
36035dbc2ac9SAndriy Voskoboinyk 	*nchans = 0;
36045dbc2ac9SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11B) || isset(bands, IEEE80211_MODE_11G))
3605b84b3638SAndriy Voskoboinyk 		ieee80211_add_channels_default_2ghz(chans, maxchans, nchans,
3606b84b3638SAndriy Voskoboinyk 		    bands, 0);
36075dbc2ac9SAndriy Voskoboinyk 	if (isset(bands, IEEE80211_MODE_11A)) {
36085dbc2ac9SAndriy Voskoboinyk 		ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
36095dbc2ac9SAndriy Voskoboinyk 		    def_chan_5ghz_band1, nitems(def_chan_5ghz_band1),
36105dbc2ac9SAndriy Voskoboinyk 		    bands, 0);
36115dbc2ac9SAndriy Voskoboinyk 		ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
36125dbc2ac9SAndriy Voskoboinyk 		    def_chan_5ghz_band2, nitems(def_chan_5ghz_band2),
36135dbc2ac9SAndriy Voskoboinyk 		    bands, 0);
36145dbc2ac9SAndriy Voskoboinyk 		ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
36155dbc2ac9SAndriy Voskoboinyk 		    def_chan_5ghz_band3, nitems(def_chan_5ghz_band3),
36165dbc2ac9SAndriy Voskoboinyk 		    bands, 0);
36175dbc2ac9SAndriy Voskoboinyk 	}
36185dbc2ac9SAndriy Voskoboinyk }
3619