Deleted Added
full compact
if_iwi.c (293339) if_iwi.c (298818)
1/*-
2 * Copyright (c) 2004, 2005
3 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4 * Copyright (c) 2005-2006 Sam Leffler, Errno Consulting
5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2004, 2005
3 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4 * Copyright (c) 2005-2006 Sam Leffler, Errno Consulting
5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/dev/iwi/if_iwi.c 293339 2016-01-07 18:41:03Z avos $");
31__FBSDID("$FreeBSD: head/sys/dev/iwi/if_iwi.c 298818 2016-04-29 22:14:11Z avos $");
32
33/*-
34 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
36 */
37
38#include <sys/param.h>
39#include <sys/sysctl.h>
40#include <sys/sockio.h>
41#include <sys/mbuf.h>
42#include <sys/kernel.h>
43#include <sys/socket.h>
44#include <sys/systm.h>
45#include <sys/malloc.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>
48#include <sys/module.h>
49#include <sys/bus.h>
50#include <sys/endian.h>
51#include <sys/proc.h>
52#include <sys/mount.h>
53#include <sys/namei.h>
54#include <sys/linker.h>
55#include <sys/firmware.h>
56#include <sys/taskqueue.h>
57
58#include <machine/bus.h>
59#include <machine/resource.h>
60#include <sys/rman.h>
61
62#include <dev/pci/pcireg.h>
63#include <dev/pci/pcivar.h>
64
65#include <net/bpf.h>
66#include <net/if.h>
67#include <net/if_var.h>
68#include <net/if_arp.h>
69#include <net/ethernet.h>
70#include <net/if_dl.h>
71#include <net/if_media.h>
72#include <net/if_types.h>
73
74#include <net80211/ieee80211_var.h>
75#include <net80211/ieee80211_radiotap.h>
76#include <net80211/ieee80211_input.h>
77#include <net80211/ieee80211_regdomain.h>
78
79#include <netinet/in.h>
80#include <netinet/in_systm.h>
81#include <netinet/in_var.h>
82#include <netinet/ip.h>
83#include <netinet/if_ether.h>
84
85#include <dev/iwi/if_iwireg.h>
86#include <dev/iwi/if_iwivar.h>
87#include <dev/iwi/if_iwi_ioctl.h>
88
89#define IWI_DEBUG
90#ifdef IWI_DEBUG
91#define DPRINTF(x) do { if (iwi_debug > 0) printf x; } while (0)
92#define DPRINTFN(n, x) do { if (iwi_debug >= (n)) printf x; } while (0)
93int iwi_debug = 0;
94SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level");
95
96static const char *iwi_fw_states[] = {
97 "IDLE", /* IWI_FW_IDLE */
98 "LOADING", /* IWI_FW_LOADING */
99 "ASSOCIATING", /* IWI_FW_ASSOCIATING */
100 "DISASSOCIATING", /* IWI_FW_DISASSOCIATING */
101 "SCANNING", /* IWI_FW_SCANNING */
102};
103#else
104#define DPRINTF(x)
105#define DPRINTFN(n, x)
106#endif
107
108MODULE_DEPEND(iwi, pci, 1, 1, 1);
109MODULE_DEPEND(iwi, wlan, 1, 1, 1);
110MODULE_DEPEND(iwi, firmware, 1, 1, 1);
111
112enum {
113 IWI_LED_TX,
114 IWI_LED_RX,
115 IWI_LED_POLL,
116};
117
118struct iwi_ident {
119 uint16_t vendor;
120 uint16_t device;
121 const char *name;
122};
123
124static const struct iwi_ident iwi_ident_table[] = {
125 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" },
126 { 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" },
127 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" },
128 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" },
129
130 { 0, 0, NULL }
131};
132
133static struct ieee80211vap *iwi_vap_create(struct ieee80211com *,
134 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
135 const uint8_t [IEEE80211_ADDR_LEN],
136 const uint8_t [IEEE80211_ADDR_LEN]);
137static void iwi_vap_delete(struct ieee80211vap *);
138static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
139static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
140 int);
141static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
142static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
143static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
144 int, bus_addr_t, bus_addr_t);
145static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
146static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
147static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
148 int);
149static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
150static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
151static struct ieee80211_node *iwi_node_alloc(struct ieee80211vap *,
152 const uint8_t [IEEE80211_ADDR_LEN]);
153static void iwi_node_free(struct ieee80211_node *);
154static void iwi_media_status(struct ifnet *, struct ifmediareq *);
155static int iwi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
156static void iwi_wme_init(struct iwi_softc *);
157static int iwi_wme_setparams(struct iwi_softc *);
158static int iwi_wme_update(struct ieee80211com *);
159static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t);
160static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
161 struct iwi_frame *);
162static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
163static void iwi_rx_intr(struct iwi_softc *);
164static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
165static void iwi_intr(void *);
166static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t);
167static void iwi_write_ibssnode(struct iwi_softc *, const u_int8_t [], int);
168static int iwi_tx_start(struct iwi_softc *, struct mbuf *,
169 struct ieee80211_node *, int);
170static int iwi_raw_xmit(struct ieee80211_node *, struct mbuf *,
171 const struct ieee80211_bpf_params *);
172static void iwi_start(struct iwi_softc *);
173static int iwi_transmit(struct ieee80211com *, struct mbuf *);
174static void iwi_watchdog(void *);
175static int iwi_ioctl(struct ieee80211com *, u_long, void *);
176static void iwi_parent(struct ieee80211com *);
177static void iwi_stop_master(struct iwi_softc *);
178static int iwi_reset(struct iwi_softc *);
179static int iwi_load_ucode(struct iwi_softc *, const struct iwi_fw *);
180static int iwi_load_firmware(struct iwi_softc *, const struct iwi_fw *);
181static void iwi_release_fw_dma(struct iwi_softc *sc);
182static int iwi_config(struct iwi_softc *);
183static int iwi_get_firmware(struct iwi_softc *, enum ieee80211_opmode);
184static void iwi_put_firmware(struct iwi_softc *);
185static void iwi_monitor_scan(void *, int);
186static int iwi_scanchan(struct iwi_softc *, unsigned long, int);
187static void iwi_scan_start(struct ieee80211com *);
188static void iwi_scan_end(struct ieee80211com *);
189static void iwi_set_channel(struct ieee80211com *);
190static void iwi_scan_curchan(struct ieee80211_scan_state *, unsigned long maxdwell);
191static void iwi_scan_mindwell(struct ieee80211_scan_state *);
192static int iwi_auth_and_assoc(struct iwi_softc *, struct ieee80211vap *);
193static void iwi_disassoc(void *, int);
194static int iwi_disassociate(struct iwi_softc *, int quiet);
195static void iwi_init_locked(struct iwi_softc *);
196static void iwi_init(void *);
197static int iwi_init_fw_dma(struct iwi_softc *, int);
198static void iwi_stop_locked(void *);
199static void iwi_stop(struct iwi_softc *);
200static void iwi_restart(void *, int);
201static int iwi_getrfkill(struct iwi_softc *);
202static void iwi_radio_on(void *, int);
203static void iwi_radio_off(void *, int);
204static void iwi_sysctlattach(struct iwi_softc *);
205static void iwi_led_event(struct iwi_softc *, int);
206static void iwi_ledattach(struct iwi_softc *);
207
208static int iwi_probe(device_t);
209static int iwi_attach(device_t);
210static int iwi_detach(device_t);
211static int iwi_shutdown(device_t);
212static int iwi_suspend(device_t);
213static int iwi_resume(device_t);
214
215static device_method_t iwi_methods[] = {
216 /* Device interface */
217 DEVMETHOD(device_probe, iwi_probe),
218 DEVMETHOD(device_attach, iwi_attach),
219 DEVMETHOD(device_detach, iwi_detach),
220 DEVMETHOD(device_shutdown, iwi_shutdown),
221 DEVMETHOD(device_suspend, iwi_suspend),
222 DEVMETHOD(device_resume, iwi_resume),
223
224 DEVMETHOD_END
225};
226
227static driver_t iwi_driver = {
228 "iwi",
229 iwi_methods,
230 sizeof (struct iwi_softc)
231};
232
233static devclass_t iwi_devclass;
234
235DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, NULL, NULL);
236
237MODULE_VERSION(iwi, 1);
238
239static __inline uint8_t
240MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
241{
242 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
243 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
244}
245
246static __inline uint32_t
247MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
248{
249 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
250 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
251}
252
253static int
254iwi_probe(device_t dev)
255{
256 const struct iwi_ident *ident;
257
258 for (ident = iwi_ident_table; ident->name != NULL; ident++) {
259 if (pci_get_vendor(dev) == ident->vendor &&
260 pci_get_device(dev) == ident->device) {
261 device_set_desc(dev, ident->name);
262 return (BUS_PROBE_DEFAULT);
263 }
264 }
265 return ENXIO;
266}
267
268static int
269iwi_attach(device_t dev)
270{
271 struct iwi_softc *sc = device_get_softc(dev);
272 struct ieee80211com *ic = &sc->sc_ic;
273 uint16_t val;
32
33/*-
34 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
36 */
37
38#include <sys/param.h>
39#include <sys/sysctl.h>
40#include <sys/sockio.h>
41#include <sys/mbuf.h>
42#include <sys/kernel.h>
43#include <sys/socket.h>
44#include <sys/systm.h>
45#include <sys/malloc.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>
48#include <sys/module.h>
49#include <sys/bus.h>
50#include <sys/endian.h>
51#include <sys/proc.h>
52#include <sys/mount.h>
53#include <sys/namei.h>
54#include <sys/linker.h>
55#include <sys/firmware.h>
56#include <sys/taskqueue.h>
57
58#include <machine/bus.h>
59#include <machine/resource.h>
60#include <sys/rman.h>
61
62#include <dev/pci/pcireg.h>
63#include <dev/pci/pcivar.h>
64
65#include <net/bpf.h>
66#include <net/if.h>
67#include <net/if_var.h>
68#include <net/if_arp.h>
69#include <net/ethernet.h>
70#include <net/if_dl.h>
71#include <net/if_media.h>
72#include <net/if_types.h>
73
74#include <net80211/ieee80211_var.h>
75#include <net80211/ieee80211_radiotap.h>
76#include <net80211/ieee80211_input.h>
77#include <net80211/ieee80211_regdomain.h>
78
79#include <netinet/in.h>
80#include <netinet/in_systm.h>
81#include <netinet/in_var.h>
82#include <netinet/ip.h>
83#include <netinet/if_ether.h>
84
85#include <dev/iwi/if_iwireg.h>
86#include <dev/iwi/if_iwivar.h>
87#include <dev/iwi/if_iwi_ioctl.h>
88
89#define IWI_DEBUG
90#ifdef IWI_DEBUG
91#define DPRINTF(x) do { if (iwi_debug > 0) printf x; } while (0)
92#define DPRINTFN(n, x) do { if (iwi_debug >= (n)) printf x; } while (0)
93int iwi_debug = 0;
94SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level");
95
96static const char *iwi_fw_states[] = {
97 "IDLE", /* IWI_FW_IDLE */
98 "LOADING", /* IWI_FW_LOADING */
99 "ASSOCIATING", /* IWI_FW_ASSOCIATING */
100 "DISASSOCIATING", /* IWI_FW_DISASSOCIATING */
101 "SCANNING", /* IWI_FW_SCANNING */
102};
103#else
104#define DPRINTF(x)
105#define DPRINTFN(n, x)
106#endif
107
108MODULE_DEPEND(iwi, pci, 1, 1, 1);
109MODULE_DEPEND(iwi, wlan, 1, 1, 1);
110MODULE_DEPEND(iwi, firmware, 1, 1, 1);
111
112enum {
113 IWI_LED_TX,
114 IWI_LED_RX,
115 IWI_LED_POLL,
116};
117
118struct iwi_ident {
119 uint16_t vendor;
120 uint16_t device;
121 const char *name;
122};
123
124static const struct iwi_ident iwi_ident_table[] = {
125 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" },
126 { 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" },
127 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" },
128 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" },
129
130 { 0, 0, NULL }
131};
132
133static struct ieee80211vap *iwi_vap_create(struct ieee80211com *,
134 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
135 const uint8_t [IEEE80211_ADDR_LEN],
136 const uint8_t [IEEE80211_ADDR_LEN]);
137static void iwi_vap_delete(struct ieee80211vap *);
138static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
139static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
140 int);
141static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
142static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
143static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
144 int, bus_addr_t, bus_addr_t);
145static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
146static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
147static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
148 int);
149static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
150static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
151static struct ieee80211_node *iwi_node_alloc(struct ieee80211vap *,
152 const uint8_t [IEEE80211_ADDR_LEN]);
153static void iwi_node_free(struct ieee80211_node *);
154static void iwi_media_status(struct ifnet *, struct ifmediareq *);
155static int iwi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
156static void iwi_wme_init(struct iwi_softc *);
157static int iwi_wme_setparams(struct iwi_softc *);
158static int iwi_wme_update(struct ieee80211com *);
159static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t);
160static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
161 struct iwi_frame *);
162static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
163static void iwi_rx_intr(struct iwi_softc *);
164static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
165static void iwi_intr(void *);
166static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t);
167static void iwi_write_ibssnode(struct iwi_softc *, const u_int8_t [], int);
168static int iwi_tx_start(struct iwi_softc *, struct mbuf *,
169 struct ieee80211_node *, int);
170static int iwi_raw_xmit(struct ieee80211_node *, struct mbuf *,
171 const struct ieee80211_bpf_params *);
172static void iwi_start(struct iwi_softc *);
173static int iwi_transmit(struct ieee80211com *, struct mbuf *);
174static void iwi_watchdog(void *);
175static int iwi_ioctl(struct ieee80211com *, u_long, void *);
176static void iwi_parent(struct ieee80211com *);
177static void iwi_stop_master(struct iwi_softc *);
178static int iwi_reset(struct iwi_softc *);
179static int iwi_load_ucode(struct iwi_softc *, const struct iwi_fw *);
180static int iwi_load_firmware(struct iwi_softc *, const struct iwi_fw *);
181static void iwi_release_fw_dma(struct iwi_softc *sc);
182static int iwi_config(struct iwi_softc *);
183static int iwi_get_firmware(struct iwi_softc *, enum ieee80211_opmode);
184static void iwi_put_firmware(struct iwi_softc *);
185static void iwi_monitor_scan(void *, int);
186static int iwi_scanchan(struct iwi_softc *, unsigned long, int);
187static void iwi_scan_start(struct ieee80211com *);
188static void iwi_scan_end(struct ieee80211com *);
189static void iwi_set_channel(struct ieee80211com *);
190static void iwi_scan_curchan(struct ieee80211_scan_state *, unsigned long maxdwell);
191static void iwi_scan_mindwell(struct ieee80211_scan_state *);
192static int iwi_auth_and_assoc(struct iwi_softc *, struct ieee80211vap *);
193static void iwi_disassoc(void *, int);
194static int iwi_disassociate(struct iwi_softc *, int quiet);
195static void iwi_init_locked(struct iwi_softc *);
196static void iwi_init(void *);
197static int iwi_init_fw_dma(struct iwi_softc *, int);
198static void iwi_stop_locked(void *);
199static void iwi_stop(struct iwi_softc *);
200static void iwi_restart(void *, int);
201static int iwi_getrfkill(struct iwi_softc *);
202static void iwi_radio_on(void *, int);
203static void iwi_radio_off(void *, int);
204static void iwi_sysctlattach(struct iwi_softc *);
205static void iwi_led_event(struct iwi_softc *, int);
206static void iwi_ledattach(struct iwi_softc *);
207
208static int iwi_probe(device_t);
209static int iwi_attach(device_t);
210static int iwi_detach(device_t);
211static int iwi_shutdown(device_t);
212static int iwi_suspend(device_t);
213static int iwi_resume(device_t);
214
215static device_method_t iwi_methods[] = {
216 /* Device interface */
217 DEVMETHOD(device_probe, iwi_probe),
218 DEVMETHOD(device_attach, iwi_attach),
219 DEVMETHOD(device_detach, iwi_detach),
220 DEVMETHOD(device_shutdown, iwi_shutdown),
221 DEVMETHOD(device_suspend, iwi_suspend),
222 DEVMETHOD(device_resume, iwi_resume),
223
224 DEVMETHOD_END
225};
226
227static driver_t iwi_driver = {
228 "iwi",
229 iwi_methods,
230 sizeof (struct iwi_softc)
231};
232
233static devclass_t iwi_devclass;
234
235DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, NULL, NULL);
236
237MODULE_VERSION(iwi, 1);
238
239static __inline uint8_t
240MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
241{
242 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
243 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
244}
245
246static __inline uint32_t
247MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
248{
249 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
250 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
251}
252
253static int
254iwi_probe(device_t dev)
255{
256 const struct iwi_ident *ident;
257
258 for (ident = iwi_ident_table; ident->name != NULL; ident++) {
259 if (pci_get_vendor(dev) == ident->vendor &&
260 pci_get_device(dev) == ident->device) {
261 device_set_desc(dev, ident->name);
262 return (BUS_PROBE_DEFAULT);
263 }
264 }
265 return ENXIO;
266}
267
268static int
269iwi_attach(device_t dev)
270{
271 struct iwi_softc *sc = device_get_softc(dev);
272 struct ieee80211com *ic = &sc->sc_ic;
273 uint16_t val;
274 uint8_t bands[howmany(IEEE80211_MODE_MAX, 8)];
274 uint8_t bands[IEEE80211_MODE_BYTES];
275 int i, error;
276
277 sc->sc_dev = dev;
278
279 IWI_LOCK_INIT(sc);
280 mbufq_init(&sc->sc_snd, ifqmaxlen);
281
282 sc->sc_unr = new_unrhdr(1, IWI_MAX_IBSSNODE-1, &sc->sc_mtx);
283
284 TASK_INIT(&sc->sc_radiontask, 0, iwi_radio_on, sc);
285 TASK_INIT(&sc->sc_radiofftask, 0, iwi_radio_off, sc);
286 TASK_INIT(&sc->sc_restarttask, 0, iwi_restart, sc);
287 TASK_INIT(&sc->sc_disassoctask, 0, iwi_disassoc, sc);
288 TASK_INIT(&sc->sc_monitortask, 0, iwi_monitor_scan, sc);
289
290 callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
291 callout_init_mtx(&sc->sc_rftimer, &sc->sc_mtx, 0);
292
293 pci_write_config(dev, 0x41, 0, 1);
294
295 /* enable bus-mastering */
296 pci_enable_busmaster(dev);
297
298 i = PCIR_BAR(0);
299 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE);
300 if (sc->mem == NULL) {
301 device_printf(dev, "could not allocate memory resource\n");
302 goto fail;
303 }
304
305 sc->sc_st = rman_get_bustag(sc->mem);
306 sc->sc_sh = rman_get_bushandle(sc->mem);
307
308 i = 0;
309 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
310 RF_ACTIVE | RF_SHAREABLE);
311 if (sc->irq == NULL) {
312 device_printf(dev, "could not allocate interrupt resource\n");
313 goto fail;
314 }
315
316 if (iwi_reset(sc) != 0) {
317 device_printf(dev, "could not reset adapter\n");
318 goto fail;
319 }
320
321 /*
322 * Allocate rings.
323 */
324 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
325 device_printf(dev, "could not allocate Cmd ring\n");
326 goto fail;
327 }
328
329 for (i = 0; i < 4; i++) {
330 error = iwi_alloc_tx_ring(sc, &sc->txq[i], IWI_TX_RING_COUNT,
331 IWI_CSR_TX1_RIDX + i * 4,
332 IWI_CSR_TX1_WIDX + i * 4);
333 if (error != 0) {
334 device_printf(dev, "could not allocate Tx ring %d\n",
335 i+i);
336 goto fail;
337 }
338 }
339
340 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
341 device_printf(dev, "could not allocate Rx ring\n");
342 goto fail;
343 }
344
345 iwi_wme_init(sc);
346
347 ic->ic_softc = sc;
348 ic->ic_name = device_get_nameunit(dev);
349 ic->ic_opmode = IEEE80211_M_STA;
350 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
351
352 /* set device capabilities */
353 ic->ic_caps =
354 IEEE80211_C_STA /* station mode supported */
355 | IEEE80211_C_IBSS /* IBSS mode supported */
356 | IEEE80211_C_MONITOR /* monitor mode supported */
357 | IEEE80211_C_PMGT /* power save supported */
358 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
359 | IEEE80211_C_WPA /* 802.11i */
360 | IEEE80211_C_WME /* 802.11e */
361#if 0
362 | IEEE80211_C_BGSCAN /* capable of bg scanning */
363#endif
364 ;
365
366 /* read MAC address from EEPROM */
367 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
368 ic->ic_macaddr[0] = val & 0xff;
369 ic->ic_macaddr[1] = val >> 8;
370 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
371 ic->ic_macaddr[2] = val & 0xff;
372 ic->ic_macaddr[3] = val >> 8;
373 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
374 ic->ic_macaddr[4] = val & 0xff;
375 ic->ic_macaddr[5] = val >> 8;
376
377 memset(bands, 0, sizeof(bands));
378 setbit(bands, IEEE80211_MODE_11B);
379 setbit(bands, IEEE80211_MODE_11G);
380 if (pci_get_device(dev) >= 0x4223)
381 setbit(bands, IEEE80211_MODE_11A);
382 ieee80211_init_channels(ic, NULL, bands);
383
384 ieee80211_ifattach(ic);
385 /* override default methods */
386 ic->ic_node_alloc = iwi_node_alloc;
387 sc->sc_node_free = ic->ic_node_free;
388 ic->ic_node_free = iwi_node_free;
389 ic->ic_raw_xmit = iwi_raw_xmit;
390 ic->ic_scan_start = iwi_scan_start;
391 ic->ic_scan_end = iwi_scan_end;
392 ic->ic_set_channel = iwi_set_channel;
393 ic->ic_scan_curchan = iwi_scan_curchan;
394 ic->ic_scan_mindwell = iwi_scan_mindwell;
395 ic->ic_wme.wme_update = iwi_wme_update;
396
397 ic->ic_vap_create = iwi_vap_create;
398 ic->ic_vap_delete = iwi_vap_delete;
399 ic->ic_ioctl = iwi_ioctl;
400 ic->ic_transmit = iwi_transmit;
401 ic->ic_parent = iwi_parent;
402
403 ieee80211_radiotap_attach(ic,
404 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
405 IWI_TX_RADIOTAP_PRESENT,
406 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
407 IWI_RX_RADIOTAP_PRESENT);
408
409 iwi_sysctlattach(sc);
410 iwi_ledattach(sc);
411
412 /*
413 * Hook our interrupt after all initialization is complete.
414 */
415 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
416 NULL, iwi_intr, sc, &sc->sc_ih);
417 if (error != 0) {
418 device_printf(dev, "could not set up interrupt\n");
419 goto fail;
420 }
421
422 if (bootverbose)
423 ieee80211_announce(ic);
424
425 return 0;
426fail:
427 /* XXX fix */
428 iwi_detach(dev);
429 return ENXIO;
430}
431
432static int
433iwi_detach(device_t dev)
434{
435 struct iwi_softc *sc = device_get_softc(dev);
436 struct ieee80211com *ic = &sc->sc_ic;
437
438 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
439
440 /* NB: do early to drain any pending tasks */
441 ieee80211_draintask(ic, &sc->sc_radiontask);
442 ieee80211_draintask(ic, &sc->sc_radiofftask);
443 ieee80211_draintask(ic, &sc->sc_restarttask);
444 ieee80211_draintask(ic, &sc->sc_disassoctask);
445 ieee80211_draintask(ic, &sc->sc_monitortask);
446
447 iwi_stop(sc);
448
449 ieee80211_ifdetach(ic);
450
451 iwi_put_firmware(sc);
452 iwi_release_fw_dma(sc);
453
454 iwi_free_cmd_ring(sc, &sc->cmdq);
455 iwi_free_tx_ring(sc, &sc->txq[0]);
456 iwi_free_tx_ring(sc, &sc->txq[1]);
457 iwi_free_tx_ring(sc, &sc->txq[2]);
458 iwi_free_tx_ring(sc, &sc->txq[3]);
459 iwi_free_rx_ring(sc, &sc->rxq);
460
461 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
462
463 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
464 sc->mem);
465
466 delete_unrhdr(sc->sc_unr);
467 mbufq_drain(&sc->sc_snd);
468
469 IWI_LOCK_DESTROY(sc);
470
471 return 0;
472}
473
474static struct ieee80211vap *
475iwi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
476 enum ieee80211_opmode opmode, int flags,
477 const uint8_t bssid[IEEE80211_ADDR_LEN],
478 const uint8_t mac[IEEE80211_ADDR_LEN])
479{
480 struct iwi_softc *sc = ic->ic_softc;
481 struct iwi_vap *ivp;
482 struct ieee80211vap *vap;
483 int i;
484
485 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
486 return NULL;
487 /*
488 * Get firmware image (and possibly dma memory) on mode change.
489 */
490 if (iwi_get_firmware(sc, opmode))
491 return NULL;
492 /* allocate DMA memory for mapping firmware image */
493 i = sc->fw_fw.size;
494 if (sc->fw_boot.size > i)
495 i = sc->fw_boot.size;
496 /* XXX do we dma the ucode as well ? */
497 if (sc->fw_uc.size > i)
498 i = sc->fw_uc.size;
499 if (iwi_init_fw_dma(sc, i))
500 return NULL;
501
502 ivp = malloc(sizeof(struct iwi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
503 vap = &ivp->iwi_vap;
504 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
505 /* override the default, the setting comes from the linux driver */
506 vap->iv_bmissthreshold = 24;
507 /* override with driver methods */
508 ivp->iwi_newstate = vap->iv_newstate;
509 vap->iv_newstate = iwi_newstate;
510
511 /* complete setup */
512 ieee80211_vap_attach(vap, ieee80211_media_change, iwi_media_status,
513 mac);
514 ic->ic_opmode = opmode;
515 return vap;
516}
517
518static void
519iwi_vap_delete(struct ieee80211vap *vap)
520{
521 struct iwi_vap *ivp = IWI_VAP(vap);
522
523 ieee80211_vap_detach(vap);
524 free(ivp, M_80211_VAP);
525}
526
527static void
528iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
529{
530 if (error != 0)
531 return;
532
533 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
534
535 *(bus_addr_t *)arg = segs[0].ds_addr;
536}
537
538static int
539iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count)
540{
541 int error;
542
543 ring->count = count;
544 ring->queued = 0;
545 ring->cur = ring->next = 0;
546
547 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
548 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
549 count * IWI_CMD_DESC_SIZE, 1, count * IWI_CMD_DESC_SIZE, 0,
550 NULL, NULL, &ring->desc_dmat);
551 if (error != 0) {
552 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
553 goto fail;
554 }
555
556 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
557 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
558 if (error != 0) {
559 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
560 goto fail;
561 }
562
563 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
564 count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
565 if (error != 0) {
566 device_printf(sc->sc_dev, "could not load desc DMA map\n");
567 goto fail;
568 }
569
570 return 0;
571
572fail: iwi_free_cmd_ring(sc, ring);
573 return error;
574}
575
576static void
577iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
578{
579 ring->queued = 0;
580 ring->cur = ring->next = 0;
581}
582
583static void
584iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
585{
586 if (ring->desc != NULL) {
587 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
588 BUS_DMASYNC_POSTWRITE);
589 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
590 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
591 }
592
593 if (ring->desc_dmat != NULL)
594 bus_dma_tag_destroy(ring->desc_dmat);
595}
596
597static int
598iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count,
599 bus_addr_t csr_ridx, bus_addr_t csr_widx)
600{
601 int i, error;
602
603 ring->count = count;
604 ring->queued = 0;
605 ring->cur = ring->next = 0;
606 ring->csr_ridx = csr_ridx;
607 ring->csr_widx = csr_widx;
608
609 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
610 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
611 count * IWI_TX_DESC_SIZE, 1, count * IWI_TX_DESC_SIZE, 0, NULL,
612 NULL, &ring->desc_dmat);
613 if (error != 0) {
614 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
615 goto fail;
616 }
617
618 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
619 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
620 if (error != 0) {
621 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
622 goto fail;
623 }
624
625 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
626 count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
627 if (error != 0) {
628 device_printf(sc->sc_dev, "could not load desc DMA map\n");
629 goto fail;
630 }
631
632 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
633 M_NOWAIT | M_ZERO);
634 if (ring->data == NULL) {
635 device_printf(sc->sc_dev, "could not allocate soft data\n");
636 error = ENOMEM;
637 goto fail;
638 }
639
640 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
641 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
642 IWI_MAX_NSEG, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
643 if (error != 0) {
644 device_printf(sc->sc_dev, "could not create data DMA tag\n");
645 goto fail;
646 }
647
648 for (i = 0; i < count; i++) {
649 error = bus_dmamap_create(ring->data_dmat, 0,
650 &ring->data[i].map);
651 if (error != 0) {
652 device_printf(sc->sc_dev, "could not create DMA map\n");
653 goto fail;
654 }
655 }
656
657 return 0;
658
659fail: iwi_free_tx_ring(sc, ring);
660 return error;
661}
662
663static void
664iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
665{
666 struct iwi_tx_data *data;
667 int i;
668
669 for (i = 0; i < ring->count; i++) {
670 data = &ring->data[i];
671
672 if (data->m != NULL) {
673 bus_dmamap_sync(ring->data_dmat, data->map,
674 BUS_DMASYNC_POSTWRITE);
675 bus_dmamap_unload(ring->data_dmat, data->map);
676 m_freem(data->m);
677 data->m = NULL;
678 }
679
680 if (data->ni != NULL) {
681 ieee80211_free_node(data->ni);
682 data->ni = NULL;
683 }
684 }
685
686 ring->queued = 0;
687 ring->cur = ring->next = 0;
688}
689
690static void
691iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
692{
693 struct iwi_tx_data *data;
694 int i;
695
696 if (ring->desc != NULL) {
697 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
698 BUS_DMASYNC_POSTWRITE);
699 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
700 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
701 }
702
703 if (ring->desc_dmat != NULL)
704 bus_dma_tag_destroy(ring->desc_dmat);
705
706 if (ring->data != NULL) {
707 for (i = 0; i < ring->count; i++) {
708 data = &ring->data[i];
709
710 if (data->m != NULL) {
711 bus_dmamap_sync(ring->data_dmat, data->map,
712 BUS_DMASYNC_POSTWRITE);
713 bus_dmamap_unload(ring->data_dmat, data->map);
714 m_freem(data->m);
715 }
716
717 if (data->ni != NULL)
718 ieee80211_free_node(data->ni);
719
720 if (data->map != NULL)
721 bus_dmamap_destroy(ring->data_dmat, data->map);
722 }
723
724 free(ring->data, M_DEVBUF);
725 }
726
727 if (ring->data_dmat != NULL)
728 bus_dma_tag_destroy(ring->data_dmat);
729}
730
731static int
732iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
733{
734 struct iwi_rx_data *data;
735 int i, error;
736
737 ring->count = count;
738 ring->cur = 0;
739
740 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
741 M_NOWAIT | M_ZERO);
742 if (ring->data == NULL) {
743 device_printf(sc->sc_dev, "could not allocate soft data\n");
744 error = ENOMEM;
745 goto fail;
746 }
747
748 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
749 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
750 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
751 if (error != 0) {
752 device_printf(sc->sc_dev, "could not create data DMA tag\n");
753 goto fail;
754 }
755
756 for (i = 0; i < count; i++) {
757 data = &ring->data[i];
758
759 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
760 if (error != 0) {
761 device_printf(sc->sc_dev, "could not create DMA map\n");
762 goto fail;
763 }
764
765 data->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
766 if (data->m == NULL) {
767 device_printf(sc->sc_dev,
768 "could not allocate rx mbuf\n");
769 error = ENOMEM;
770 goto fail;
771 }
772
773 error = bus_dmamap_load(ring->data_dmat, data->map,
774 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
775 &data->physaddr, 0);
776 if (error != 0) {
777 device_printf(sc->sc_dev,
778 "could not load rx buf DMA map");
779 goto fail;
780 }
781
782 data->reg = IWI_CSR_RX_BASE + i * 4;
783 }
784
785 return 0;
786
787fail: iwi_free_rx_ring(sc, ring);
788 return error;
789}
790
791static void
792iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
793{
794 ring->cur = 0;
795}
796
797static void
798iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
799{
800 struct iwi_rx_data *data;
801 int i;
802
803 if (ring->data != NULL) {
804 for (i = 0; i < ring->count; i++) {
805 data = &ring->data[i];
806
807 if (data->m != NULL) {
808 bus_dmamap_sync(ring->data_dmat, data->map,
809 BUS_DMASYNC_POSTREAD);
810 bus_dmamap_unload(ring->data_dmat, data->map);
811 m_freem(data->m);
812 }
813
814 if (data->map != NULL)
815 bus_dmamap_destroy(ring->data_dmat, data->map);
816 }
817
818 free(ring->data, M_DEVBUF);
819 }
820
821 if (ring->data_dmat != NULL)
822 bus_dma_tag_destroy(ring->data_dmat);
823}
824
825static int
826iwi_shutdown(device_t dev)
827{
828 struct iwi_softc *sc = device_get_softc(dev);
829
830 iwi_stop(sc);
831 iwi_put_firmware(sc); /* ??? XXX */
832
833 return 0;
834}
835
836static int
837iwi_suspend(device_t dev)
838{
839 struct iwi_softc *sc = device_get_softc(dev);
840 struct ieee80211com *ic = &sc->sc_ic;
841
842 ieee80211_suspend_all(ic);
843 return 0;
844}
845
846static int
847iwi_resume(device_t dev)
848{
849 struct iwi_softc *sc = device_get_softc(dev);
850 struct ieee80211com *ic = &sc->sc_ic;
851
852 pci_write_config(dev, 0x41, 0, 1);
853
854 ieee80211_resume_all(ic);
855 return 0;
856}
857
858static struct ieee80211_node *
859iwi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
860{
861 struct iwi_node *in;
862
863 in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
864 if (in == NULL)
865 return NULL;
866 /* XXX assign sta table entry for adhoc */
867 in->in_station = -1;
868
869 return &in->in_node;
870}
871
872static void
873iwi_node_free(struct ieee80211_node *ni)
874{
875 struct ieee80211com *ic = ni->ni_ic;
876 struct iwi_softc *sc = ic->ic_softc;
877 struct iwi_node *in = (struct iwi_node *)ni;
878
879 if (in->in_station != -1) {
880 DPRINTF(("%s mac %6D station %u\n", __func__,
881 ni->ni_macaddr, ":", in->in_station));
882 free_unr(sc->sc_unr, in->in_station);
883 }
884
885 sc->sc_node_free(ni);
886}
887
888/*
889 * Convert h/w rate code to IEEE rate code.
890 */
891static int
892iwi_cvtrate(int iwirate)
893{
894 switch (iwirate) {
895 case IWI_RATE_DS1: return 2;
896 case IWI_RATE_DS2: return 4;
897 case IWI_RATE_DS5: return 11;
898 case IWI_RATE_DS11: return 22;
899 case IWI_RATE_OFDM6: return 12;
900 case IWI_RATE_OFDM9: return 18;
901 case IWI_RATE_OFDM12: return 24;
902 case IWI_RATE_OFDM18: return 36;
903 case IWI_RATE_OFDM24: return 48;
904 case IWI_RATE_OFDM36: return 72;
905 case IWI_RATE_OFDM48: return 96;
906 case IWI_RATE_OFDM54: return 108;
907 }
908 return 0;
909}
910
911/*
912 * The firmware automatically adapts the transmit speed. We report its current
913 * value here.
914 */
915static void
916iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
917{
918 struct ieee80211vap *vap = ifp->if_softc;
919 struct ieee80211com *ic = vap->iv_ic;
920 struct iwi_softc *sc = ic->ic_softc;
921 struct ieee80211_node *ni;
922
923 /* read current transmission rate from adapter */
924 ni = ieee80211_ref_node(vap->iv_bss);
925 ni->ni_txrate =
926 iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE));
927 ieee80211_free_node(ni);
928 ieee80211_media_status(ifp, imr);
929}
930
931static int
932iwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
933{
934 struct iwi_vap *ivp = IWI_VAP(vap);
935 struct ieee80211com *ic = vap->iv_ic;
936 struct iwi_softc *sc = ic->ic_softc;
937 IWI_LOCK_DECL;
938
939 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
940 ieee80211_state_name[vap->iv_state],
941 ieee80211_state_name[nstate], sc->flags));
942
943 IEEE80211_UNLOCK(ic);
944 IWI_LOCK(sc);
945 switch (nstate) {
946 case IEEE80211_S_INIT:
947 /*
948 * NB: don't try to do this if iwi_stop_master has
949 * shutdown the firmware and disabled interrupts.
950 */
951 if (vap->iv_state == IEEE80211_S_RUN &&
952 (sc->flags & IWI_FLAG_FW_INITED))
953 iwi_disassociate(sc, 0);
954 break;
955 case IEEE80211_S_AUTH:
956 iwi_auth_and_assoc(sc, vap);
957 break;
958 case IEEE80211_S_RUN:
959 if (vap->iv_opmode == IEEE80211_M_IBSS &&
960 vap->iv_state == IEEE80211_S_SCAN) {
961 /*
962 * XXX when joining an ibss network we are called
963 * with a SCAN -> RUN transition on scan complete.
964 * Use that to call iwi_auth_and_assoc. On completing
965 * the join we are then called again with an
966 * AUTH -> RUN transition and we want to do nothing.
967 * This is all totally bogus and needs to be redone.
968 */
969 iwi_auth_and_assoc(sc, vap);
970 } else if (vap->iv_opmode == IEEE80211_M_MONITOR)
971 ieee80211_runtask(ic, &sc->sc_monitortask);
972 break;
973 case IEEE80211_S_ASSOC:
974 /*
975 * If we are transitioning from AUTH then just wait
976 * for the ASSOC status to come back from the firmware.
977 * Otherwise we need to issue the association request.
978 */
979 if (vap->iv_state == IEEE80211_S_AUTH)
980 break;
981 iwi_auth_and_assoc(sc, vap);
982 break;
983 default:
984 break;
985 }
986 IWI_UNLOCK(sc);
987 IEEE80211_LOCK(ic);
988 return ivp->iwi_newstate(vap, nstate, arg);
989}
990
991/*
992 * WME parameters coming from IEEE 802.11e specification. These values are
993 * already declared in ieee80211_proto.c, but they are static so they can't
994 * be reused here.
995 */
996static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
997 { 0, 3, 5, 7, 0 }, /* WME_AC_BE */
998 { 0, 3, 5, 10, 0 }, /* WME_AC_BK */
999 { 0, 2, 4, 5, 188 }, /* WME_AC_VI */
1000 { 0, 2, 3, 4, 102 } /* WME_AC_VO */
1001};
1002
1003static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
1004 { 0, 3, 4, 6, 0 }, /* WME_AC_BE */
1005 { 0, 3, 4, 10, 0 }, /* WME_AC_BK */
1006 { 0, 2, 3, 4, 94 }, /* WME_AC_VI */
1007 { 0, 2, 2, 3, 47 } /* WME_AC_VO */
1008};
1009#define IWI_EXP2(v) htole16((1 << (v)) - 1)
1010#define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
1011
1012static void
1013iwi_wme_init(struct iwi_softc *sc)
1014{
1015 const struct wmeParams *wmep;
1016 int ac;
1017
1018 memset(sc->wme, 0, sizeof sc->wme);
1019 for (ac = 0; ac < WME_NUM_AC; ac++) {
1020 /* set WME values for CCK modulation */
1021 wmep = &iwi_wme_cck_params[ac];
1022 sc->wme[1].aifsn[ac] = wmep->wmep_aifsn;
1023 sc->wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1024 sc->wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1025 sc->wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1026 sc->wme[1].acm[ac] = wmep->wmep_acm;
1027
1028 /* set WME values for OFDM modulation */
1029 wmep = &iwi_wme_ofdm_params[ac];
1030 sc->wme[2].aifsn[ac] = wmep->wmep_aifsn;
1031 sc->wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1032 sc->wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1033 sc->wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1034 sc->wme[2].acm[ac] = wmep->wmep_acm;
1035 }
1036}
1037
1038static int
1039iwi_wme_setparams(struct iwi_softc *sc)
1040{
1041 struct ieee80211com *ic = &sc->sc_ic;
1042 const struct wmeParams *wmep;
1043 int ac;
1044
1045 for (ac = 0; ac < WME_NUM_AC; ac++) {
1046 /* set WME values for current operating mode */
1047 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
1048 sc->wme[0].aifsn[ac] = wmep->wmep_aifsn;
1049 sc->wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1050 sc->wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1051 sc->wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1052 sc->wme[0].acm[ac] = wmep->wmep_acm;
1053 }
1054
1055 DPRINTF(("Setting WME parameters\n"));
1056 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, sc->wme, sizeof sc->wme);
1057}
1058#undef IWI_USEC
1059#undef IWI_EXP2
1060
1061static int
1062iwi_wme_update(struct ieee80211com *ic)
1063{
1064 struct iwi_softc *sc = ic->ic_softc;
1065 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1066 IWI_LOCK_DECL;
1067
1068 /*
1069 * We may be called to update the WME parameters in
1070 * the adapter at various places. If we're already
1071 * associated then initiate the request immediately;
1072 * otherwise we assume the params will get sent down
1073 * to the adapter as part of the work iwi_auth_and_assoc
1074 * does.
1075 */
1076 if (vap->iv_state == IEEE80211_S_RUN) {
1077 IWI_LOCK(sc);
1078 iwi_wme_setparams(sc);
1079 IWI_UNLOCK(sc);
1080 }
1081 return (0);
1082}
1083
1084static int
1085iwi_wme_setie(struct iwi_softc *sc)
1086{
1087 struct ieee80211_wme_info wme;
1088
1089 memset(&wme, 0, sizeof wme);
1090 wme.wme_id = IEEE80211_ELEMID_VENDOR;
1091 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
1092 wme.wme_oui[0] = 0x00;
1093 wme.wme_oui[1] = 0x50;
1094 wme.wme_oui[2] = 0xf2;
1095 wme.wme_type = WME_OUI_TYPE;
1096 wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
1097 wme.wme_version = WME_VERSION;
1098 wme.wme_info = 0;
1099
1100 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
1101 return iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme);
1102}
1103
1104/*
1105 * Read 16 bits at address 'addr' from the serial EEPROM.
1106 */
1107static uint16_t
1108iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1109{
1110 uint32_t tmp;
1111 uint16_t val;
1112 int n;
1113
1114 /* clock C once before the first command */
1115 IWI_EEPROM_CTL(sc, 0);
1116 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1117 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1118 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1119
1120 /* write start bit (1) */
1121 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1122 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1123
1124 /* write READ opcode (10) */
1125 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1126 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1127 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1128 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1129
1130 /* write address A7-A0 */
1131 for (n = 7; n >= 0; n--) {
1132 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1133 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1134 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1135 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1136 }
1137
1138 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1139
1140 /* read data Q15-Q0 */
1141 val = 0;
1142 for (n = 15; n >= 0; n--) {
1143 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1144 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1145 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1146 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1147 }
1148
1149 IWI_EEPROM_CTL(sc, 0);
1150
1151 /* clear Chip Select and clock C */
1152 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1153 IWI_EEPROM_CTL(sc, 0);
1154 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1155
1156 return val;
1157}
1158
1159static void
1160iwi_setcurchan(struct iwi_softc *sc, int chan)
1161{
1162 struct ieee80211com *ic = &sc->sc_ic;
1163
1164 sc->curchan = chan;
1165 ieee80211_radiotap_chan_change(ic);
1166}
1167
1168static void
1169iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1170 struct iwi_frame *frame)
1171{
1172 struct ieee80211com *ic = &sc->sc_ic;
1173 struct mbuf *mnew, *m;
1174 struct ieee80211_node *ni;
1175 int type, error, framelen;
1176 int8_t rssi, nf;
1177 IWI_LOCK_DECL;
1178
1179 framelen = le16toh(frame->len);
1180 if (framelen < IEEE80211_MIN_LEN || framelen > MCLBYTES) {
1181 /*
1182 * XXX >MCLBYTES is bogus as it means the h/w dma'd
1183 * out of bounds; need to figure out how to limit
1184 * frame size in the firmware
1185 */
1186 /* XXX stat */
1187 DPRINTFN(1,
1188 ("drop rx frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1189 le16toh(frame->len), frame->chan, frame->rssi,
1190 frame->rssi_dbm));
1191 return;
1192 }
1193
1194 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1195 le16toh(frame->len), frame->chan, frame->rssi, frame->rssi_dbm));
1196
1197 if (frame->chan != sc->curchan)
1198 iwi_setcurchan(sc, frame->chan);
1199
1200 /*
1201 * Try to allocate a new mbuf for this ring element and load it before
1202 * processing the current mbuf. If the ring element cannot be loaded,
1203 * drop the received packet and reuse the old mbuf. In the unlikely
1204 * case that the old mbuf can't be reloaded either, explicitly panic.
1205 */
1206 mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1207 if (mnew == NULL) {
1208 counter_u64_add(ic->ic_ierrors, 1);
1209 return;
1210 }
1211
1212 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1213
1214 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1215 mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr,
1216 0);
1217 if (error != 0) {
1218 m_freem(mnew);
1219
1220 /* try to reload the old mbuf */
1221 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1222 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
1223 &data->physaddr, 0);
1224 if (error != 0) {
1225 /* very unlikely that it will fail... */
1226 panic("%s: could not load old rx mbuf",
1227 device_get_name(sc->sc_dev));
1228 }
1229 counter_u64_add(ic->ic_ierrors, 1);
1230 return;
1231 }
1232
1233 /*
1234 * New mbuf successfully loaded, update Rx ring and continue
1235 * processing.
1236 */
1237 m = data->m;
1238 data->m = mnew;
1239 CSR_WRITE_4(sc, data->reg, data->physaddr);
1240
1241 /* finalize mbuf */
1242 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1243 sizeof (struct iwi_frame) + framelen;
1244
1245 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1246
1247 rssi = frame->rssi_dbm;
1248 nf = -95;
1249 if (ieee80211_radiotap_active(ic)) {
1250 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1251
1252 tap->wr_flags = 0;
1253 tap->wr_antsignal = rssi;
1254 tap->wr_antnoise = nf;
1255 tap->wr_rate = iwi_cvtrate(frame->rate);
1256 tap->wr_antenna = frame->antenna;
1257 }
1258 IWI_UNLOCK(sc);
1259
1260 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1261 if (ni != NULL) {
1262 type = ieee80211_input(ni, m, rssi, nf);
1263 ieee80211_free_node(ni);
1264 } else
1265 type = ieee80211_input_all(ic, m, rssi, nf);
1266
1267 IWI_LOCK(sc);
1268 if (sc->sc_softled) {
1269 /*
1270 * Blink for any data frame. Otherwise do a
1271 * heartbeat-style blink when idle. The latter
1272 * is mainly for station mode where we depend on
1273 * periodic beacon frames to trigger the poll event.
1274 */
1275 if (type == IEEE80211_FC0_TYPE_DATA) {
1276 sc->sc_rxrate = frame->rate;
1277 iwi_led_event(sc, IWI_LED_RX);
1278 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
1279 iwi_led_event(sc, IWI_LED_POLL);
1280 }
1281}
1282
1283/*
1284 * Check for an association response frame to see if QoS
1285 * has been negotiated. We parse just enough to figure
1286 * out if we're supposed to use QoS. The proper solution
1287 * is to pass the frame up so ieee80211_input can do the
1288 * work but that's made hard by how things currently are
1289 * done in the driver.
1290 */
1291static void
1292iwi_checkforqos(struct ieee80211vap *vap,
1293 const struct ieee80211_frame *wh, int len)
1294{
1295#define SUBTYPE(wh) ((wh)->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
1296 const uint8_t *frm, *efrm, *wme;
1297 struct ieee80211_node *ni;
1298 uint16_t capinfo, status, associd;
1299
1300 /* NB: +8 for capinfo, status, associd, and first ie */
1301 if (!(sizeof(*wh)+8 < len && len < IEEE80211_MAX_LEN) ||
1302 SUBTYPE(wh) != IEEE80211_FC0_SUBTYPE_ASSOC_RESP)
1303 return;
1304 /*
1305 * asresp frame format
1306 * [2] capability information
1307 * [2] status
1308 * [2] association ID
1309 * [tlv] supported rates
1310 * [tlv] extended supported rates
1311 * [tlv] WME
1312 */
1313 frm = (const uint8_t *)&wh[1];
1314 efrm = ((const uint8_t *) wh) + len;
1315
1316 capinfo = le16toh(*(const uint16_t *)frm);
1317 frm += 2;
1318 status = le16toh(*(const uint16_t *)frm);
1319 frm += 2;
1320 associd = le16toh(*(const uint16_t *)frm);
1321 frm += 2;
1322
1323 wme = NULL;
1324 while (efrm - frm > 1) {
1325 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1326 switch (*frm) {
1327 case IEEE80211_ELEMID_VENDOR:
1328 if (iswmeoui(frm))
1329 wme = frm;
1330 break;
1331 }
1332 frm += frm[1] + 2;
1333 }
1334
1335 ni = ieee80211_ref_node(vap->iv_bss);
1336 ni->ni_capinfo = capinfo;
1337 ni->ni_associd = associd & 0x3fff;
1338 if (wme != NULL)
1339 ni->ni_flags |= IEEE80211_NODE_QOS;
1340 else
1341 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1342 ieee80211_free_node(ni);
1343#undef SUBTYPE
1344}
1345
1346static void
1347iwi_notif_link_quality(struct iwi_softc *sc, struct iwi_notif *notif)
1348{
1349 struct iwi_notif_link_quality *lq;
1350 int len;
1351
1352 len = le16toh(notif->len);
1353
1354 DPRINTFN(5, ("Notification (%u) - len=%d, sizeof=%zu\n",
1355 notif->type,
1356 len,
1357 sizeof(struct iwi_notif_link_quality)
1358 ));
1359
1360 /* enforce length */
1361 if (len != sizeof(struct iwi_notif_link_quality)) {
1362 DPRINTFN(5, ("Notification: (%u) too short (%d)\n",
1363 notif->type,
1364 len));
1365 return;
1366 }
1367
1368 lq = (struct iwi_notif_link_quality *)(notif + 1);
1369 memcpy(&sc->sc_linkqual, lq, sizeof(sc->sc_linkqual));
1370 sc->sc_linkqual_valid = 1;
1371}
1372
1373/*
1374 * Task queue callbacks for iwi_notification_intr used to avoid LOR's.
1375 */
1376
1377static void
1378iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1379{
1380 struct ieee80211com *ic = &sc->sc_ic;
1381 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1382 struct iwi_notif_scan_channel *chan;
1383 struct iwi_notif_scan_complete *scan;
1384 struct iwi_notif_authentication *auth;
1385 struct iwi_notif_association *assoc;
1386 struct iwi_notif_beacon_state *beacon;
1387
1388 switch (notif->type) {
1389 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1390 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1391
1392 DPRINTFN(3, ("Scan of channel %u complete (%u)\n",
1393 ieee80211_ieee2mhz(chan->nchan, 0), chan->nchan));
1394
1395 /* Reset the timer, the scan is still going */
1396 sc->sc_state_timer = 3;
1397 break;
1398
1399 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1400 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1401
1402 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1403 scan->status));
1404
1405 IWI_STATE_END(sc, IWI_FW_SCANNING);
1406
1407 /*
1408 * Monitor mode works by doing a passive scan to set
1409 * the channel and enable rx. Because we don't want
1410 * to abort a scan lest the firmware crash we scan
1411 * for a short period of time and automatically restart
1412 * the scan when notified the sweep has completed.
1413 */
1414 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
1415 ieee80211_runtask(ic, &sc->sc_monitortask);
1416 break;
1417 }
1418
1419 if (scan->status == IWI_SCAN_COMPLETED) {
1420 /* NB: don't need to defer, net80211 does it for us */
1421 ieee80211_scan_next(vap);
1422 }
1423 break;
1424
1425 case IWI_NOTIF_TYPE_AUTHENTICATION:
1426 auth = (struct iwi_notif_authentication *)(notif + 1);
1427 switch (auth->state) {
1428 case IWI_AUTH_SUCCESS:
1429 DPRINTFN(2, ("Authentication succeeeded\n"));
1430 ieee80211_new_state(vap, IEEE80211_S_ASSOC, -1);
1431 break;
1432 case IWI_AUTH_FAIL:
1433 /*
1434 * These are delivered as an unsolicited deauth
1435 * (e.g. due to inactivity) or in response to an
1436 * associate request.
1437 */
1438 sc->flags &= ~IWI_FLAG_ASSOCIATED;
1439 if (vap->iv_state != IEEE80211_S_RUN) {
1440 DPRINTFN(2, ("Authentication failed\n"));
1441 vap->iv_stats.is_rx_auth_fail++;
1442 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1443 } else {
1444 DPRINTFN(2, ("Deauthenticated\n"));
1445 vap->iv_stats.is_rx_deauth++;
1446 }
1447 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1448 break;
1449 case IWI_AUTH_SENT_1:
1450 case IWI_AUTH_RECV_2:
1451 case IWI_AUTH_SEQ1_PASS:
1452 break;
1453 case IWI_AUTH_SEQ1_FAIL:
1454 DPRINTFN(2, ("Initial authentication handshake failed; "
1455 "you probably need shared key\n"));
1456 vap->iv_stats.is_rx_auth_fail++;
1457 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1458 /* XXX retry shared key when in auto */
1459 break;
1460 default:
1461 device_printf(sc->sc_dev,
1462 "unknown authentication state %u\n", auth->state);
1463 break;
1464 }
1465 break;
1466
1467 case IWI_NOTIF_TYPE_ASSOCIATION:
1468 assoc = (struct iwi_notif_association *)(notif + 1);
1469 switch (assoc->state) {
1470 case IWI_AUTH_SUCCESS:
1471 /* re-association, do nothing */
1472 break;
1473 case IWI_ASSOC_SUCCESS:
1474 DPRINTFN(2, ("Association succeeded\n"));
1475 sc->flags |= IWI_FLAG_ASSOCIATED;
1476 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1477 iwi_checkforqos(vap,
1478 (const struct ieee80211_frame *)(assoc+1),
1479 le16toh(notif->len) - sizeof(*assoc) - 1);
1480 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
1481 break;
1482 case IWI_ASSOC_INIT:
1483 sc->flags &= ~IWI_FLAG_ASSOCIATED;
1484 switch (sc->fw_state) {
1485 case IWI_FW_ASSOCIATING:
1486 DPRINTFN(2, ("Association failed\n"));
1487 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1488 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1489 break;
1490
1491 case IWI_FW_DISASSOCIATING:
1492 DPRINTFN(2, ("Dissassociated\n"));
1493 IWI_STATE_END(sc, IWI_FW_DISASSOCIATING);
1494 vap->iv_stats.is_rx_disassoc++;
1495 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1496 break;
1497 }
1498 break;
1499 default:
1500 device_printf(sc->sc_dev,
1501 "unknown association state %u\n", assoc->state);
1502 break;
1503 }
1504 break;
1505
1506 case IWI_NOTIF_TYPE_BEACON:
1507 /* XXX check struct length */
1508 beacon = (struct iwi_notif_beacon_state *)(notif + 1);
1509
1510 DPRINTFN(5, ("Beacon state (%u, %u)\n",
1511 beacon->state, le32toh(beacon->number)));
1512
1513 if (beacon->state == IWI_BEACON_MISS) {
1514 /*
1515 * The firmware notifies us of every beacon miss
1516 * so we need to track the count against the
1517 * configured threshold before notifying the
1518 * 802.11 layer.
1519 * XXX try to roam, drop assoc only on much higher count
1520 */
1521 if (le32toh(beacon->number) >= vap->iv_bmissthreshold) {
1522 DPRINTF(("Beacon miss: %u >= %u\n",
1523 le32toh(beacon->number),
1524 vap->iv_bmissthreshold));
1525 vap->iv_stats.is_beacon_miss++;
1526 /*
1527 * It's pointless to notify the 802.11 layer
1528 * as it'll try to send a probe request (which
1529 * we'll discard) and then timeout and drop us
1530 * into scan state. Instead tell the firmware
1531 * to disassociate and then on completion we'll
1532 * kick the state machine to scan.
1533 */
1534 ieee80211_runtask(ic, &sc->sc_disassoctask);
1535 }
1536 }
1537 break;
1538
1539 case IWI_NOTIF_TYPE_CALIBRATION:
1540 case IWI_NOTIF_TYPE_NOISE:
1541 /* XXX handle? */
1542 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1543 break;
1544 case IWI_NOTIF_TYPE_LINK_QUALITY:
1545 iwi_notif_link_quality(sc, notif);
1546 break;
1547
1548 default:
1549 DPRINTF(("unknown notification type %u flags 0x%x len %u\n",
1550 notif->type, notif->flags, le16toh(notif->len)));
1551 break;
1552 }
1553}
1554
1555static void
1556iwi_rx_intr(struct iwi_softc *sc)
1557{
1558 struct iwi_rx_data *data;
1559 struct iwi_hdr *hdr;
1560 uint32_t hw;
1561
1562 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1563
1564 for (; sc->rxq.cur != hw;) {
1565 data = &sc->rxq.data[sc->rxq.cur];
1566
1567 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1568 BUS_DMASYNC_POSTREAD);
1569
1570 hdr = mtod(data->m, struct iwi_hdr *);
1571
1572 switch (hdr->type) {
1573 case IWI_HDR_TYPE_FRAME:
1574 iwi_frame_intr(sc, data, sc->rxq.cur,
1575 (struct iwi_frame *)(hdr + 1));
1576 break;
1577
1578 case IWI_HDR_TYPE_NOTIF:
1579 iwi_notification_intr(sc,
1580 (struct iwi_notif *)(hdr + 1));
1581 break;
1582
1583 default:
1584 device_printf(sc->sc_dev, "unknown hdr type %u\n",
1585 hdr->type);
1586 }
1587
1588 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1589
1590 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1591 }
1592
1593 /* tell the firmware what we have processed */
1594 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1595 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1596}
1597
1598static void
1599iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1600{
1601 struct iwi_tx_data *data;
1602 uint32_t hw;
1603
1604 hw = CSR_READ_4(sc, txq->csr_ridx);
1605
1606 while (txq->next != hw) {
1607 data = &txq->data[txq->next];
1608 DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1609 bus_dmamap_sync(txq->data_dmat, data->map,
1610 BUS_DMASYNC_POSTWRITE);
1611 bus_dmamap_unload(txq->data_dmat, data->map);
1612 ieee80211_tx_complete(data->ni, data->m, 0);
1613 data->ni = NULL;
1614 data->m = NULL;
1615 txq->queued--;
1616 txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1617 }
1618 sc->sc_tx_timer = 0;
1619 if (sc->sc_softled)
1620 iwi_led_event(sc, IWI_LED_TX);
1621 iwi_start(sc);
1622}
1623
1624static void
1625iwi_fatal_error_intr(struct iwi_softc *sc)
1626{
1627 struct ieee80211com *ic = &sc->sc_ic;
1628 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1629
1630 device_printf(sc->sc_dev, "firmware error\n");
1631 if (vap != NULL)
1632 ieee80211_cancel_scan(vap);
1633 ieee80211_runtask(ic, &sc->sc_restarttask);
1634
1635 sc->flags &= ~IWI_FLAG_BUSY;
1636 sc->sc_busy_timer = 0;
1637 wakeup(sc);
1638}
1639
1640static void
1641iwi_radio_off_intr(struct iwi_softc *sc)
1642{
1643
1644 ieee80211_runtask(&sc->sc_ic, &sc->sc_radiofftask);
1645}
1646
1647static void
1648iwi_intr(void *arg)
1649{
1650 struct iwi_softc *sc = arg;
1651 uint32_t r;
1652 IWI_LOCK_DECL;
1653
1654 IWI_LOCK(sc);
1655
1656 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) {
1657 IWI_UNLOCK(sc);
1658 return;
1659 }
1660
1661 /* acknowledge interrupts */
1662 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1663
1664 if (r & IWI_INTR_FATAL_ERROR) {
1665 iwi_fatal_error_intr(sc);
1666 goto done;
1667 }
1668
1669 if (r & IWI_INTR_FW_INITED) {
1670 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1671 wakeup(sc);
1672 }
1673
1674 if (r & IWI_INTR_RADIO_OFF)
1675 iwi_radio_off_intr(sc);
1676
1677 if (r & IWI_INTR_CMD_DONE) {
1678 sc->flags &= ~IWI_FLAG_BUSY;
1679 sc->sc_busy_timer = 0;
1680 wakeup(sc);
1681 }
1682
1683 if (r & IWI_INTR_TX1_DONE)
1684 iwi_tx_intr(sc, &sc->txq[0]);
1685
1686 if (r & IWI_INTR_TX2_DONE)
1687 iwi_tx_intr(sc, &sc->txq[1]);
1688
1689 if (r & IWI_INTR_TX3_DONE)
1690 iwi_tx_intr(sc, &sc->txq[2]);
1691
1692 if (r & IWI_INTR_TX4_DONE)
1693 iwi_tx_intr(sc, &sc->txq[3]);
1694
1695 if (r & IWI_INTR_RX_DONE)
1696 iwi_rx_intr(sc);
1697
1698 if (r & IWI_INTR_PARITY_ERROR) {
1699 /* XXX rate-limit */
1700 device_printf(sc->sc_dev, "parity error\n");
1701 }
1702done:
1703 IWI_UNLOCK(sc);
1704}
1705
1706static int
1707iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len)
1708{
1709 struct iwi_cmd_desc *desc;
1710
1711 IWI_LOCK_ASSERT(sc);
1712
1713 if (sc->flags & IWI_FLAG_BUSY) {
1714 device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n",
1715 __func__, type);
1716 return EAGAIN;
1717 }
1718 sc->flags |= IWI_FLAG_BUSY;
1719 sc->sc_busy_timer = 2;
1720
1721 desc = &sc->cmdq.desc[sc->cmdq.cur];
1722
1723 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1724 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1725 desc->type = type;
1726 desc->len = len;
1727 memcpy(desc->data, data, len);
1728
1729 bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map,
1730 BUS_DMASYNC_PREWRITE);
1731
1732 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1733 type, len));
1734
1735 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1736 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1737
1738 return msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz);
1739}
1740
1741static void
1742iwi_write_ibssnode(struct iwi_softc *sc,
1743 const u_int8_t addr[IEEE80211_ADDR_LEN], int entry)
1744{
1745 struct iwi_ibssnode node;
1746
1747 /* write node information into NIC memory */
1748 memset(&node, 0, sizeof node);
1749 IEEE80211_ADDR_COPY(node.bssid, addr);
1750
1751 DPRINTF(("%s mac %6D station %u\n", __func__, node.bssid, ":", entry));
1752
1753 CSR_WRITE_REGION_1(sc,
1754 IWI_CSR_NODE_BASE + entry * sizeof node,
1755 (uint8_t *)&node, sizeof node);
1756}
1757
1758static int
1759iwi_tx_start(struct iwi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1760 int ac)
1761{
1762 struct ieee80211vap *vap = ni->ni_vap;
1763 struct ieee80211com *ic = ni->ni_ic;
1764 struct iwi_node *in = (struct iwi_node *)ni;
1765 const struct ieee80211_frame *wh;
1766 struct ieee80211_key *k;
1767 const struct chanAccParams *cap;
1768 struct iwi_tx_ring *txq = &sc->txq[ac];
1769 struct iwi_tx_data *data;
1770 struct iwi_tx_desc *desc;
1771 struct mbuf *mnew;
1772 bus_dma_segment_t segs[IWI_MAX_NSEG];
1773 int error, nsegs, hdrlen, i;
1774 int ismcast, flags, xflags, staid;
1775
1776 IWI_LOCK_ASSERT(sc);
1777 wh = mtod(m0, const struct ieee80211_frame *);
1778 /* NB: only data frames use this path */
1779 hdrlen = ieee80211_hdrsize(wh);
1780 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1781 flags = xflags = 0;
1782
1783 if (!ismcast)
1784 flags |= IWI_DATA_FLAG_NEED_ACK;
1785 if (vap->iv_flags & IEEE80211_F_SHPREAMBLE)
1786 flags |= IWI_DATA_FLAG_SHPREAMBLE;
1787 if (IEEE80211_QOS_HAS_SEQ(wh)) {
1788 xflags |= IWI_DATA_XFLAG_QOS;
1789 cap = &ic->ic_wme.wme_chanParams;
1790 if (!cap->cap_wmeParams[ac].wmep_noackPolicy)
1791 flags &= ~IWI_DATA_FLAG_NEED_ACK;
1792 }
1793
1794 /*
1795 * This is only used in IBSS mode where the firmware expect an index
1796 * in a h/w table instead of a destination address.
1797 */
1798 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1799 if (!ismcast) {
1800 if (in->in_station == -1) {
1801 in->in_station = alloc_unr(sc->sc_unr);
1802 if (in->in_station == -1) {
1803 /* h/w table is full */
1804 if_inc_counter(ni->ni_vap->iv_ifp,
1805 IFCOUNTER_OERRORS, 1);
1806 m_freem(m0);
1807 ieee80211_free_node(ni);
1808 return 0;
1809 }
1810 iwi_write_ibssnode(sc,
1811 ni->ni_macaddr, in->in_station);
1812 }
1813 staid = in->in_station;
1814 } else {
1815 /*
1816 * Multicast addresses have no associated node
1817 * so there will be no station entry. We reserve
1818 * entry 0 for one mcast address and use that.
1819 * If there are many being used this will be
1820 * expensive and we'll need to do a better job
1821 * but for now this handles the broadcast case.
1822 */
1823 if (!IEEE80211_ADDR_EQ(wh->i_addr1, sc->sc_mcast)) {
1824 IEEE80211_ADDR_COPY(sc->sc_mcast, wh->i_addr1);
1825 iwi_write_ibssnode(sc, sc->sc_mcast, 0);
1826 }
1827 staid = 0;
1828 }
1829 } else
1830 staid = 0;
1831
1832 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1833 k = ieee80211_crypto_encap(ni, m0);
1834 if (k == NULL) {
1835 m_freem(m0);
1836 return ENOBUFS;
1837 }
1838
1839 /* packet header may have moved, reset our local pointer */
1840 wh = mtod(m0, struct ieee80211_frame *);
1841 }
1842
1843 if (ieee80211_radiotap_active_vap(vap)) {
1844 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1845
1846 tap->wt_flags = 0;
1847
1848 ieee80211_radiotap_tx(vap, m0);
1849 }
1850
1851 data = &txq->data[txq->cur];
1852 desc = &txq->desc[txq->cur];
1853
1854 /* save and trim IEEE802.11 header */
1855 m_copydata(m0, 0, hdrlen, (caddr_t)&desc->wh);
1856 m_adj(m0, hdrlen);
1857
1858 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0, segs,
1859 &nsegs, 0);
1860 if (error != 0 && error != EFBIG) {
1861 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1862 error);
1863 m_freem(m0);
1864 return error;
1865 }
1866 if (error != 0) {
1867 mnew = m_defrag(m0, M_NOWAIT);
1868 if (mnew == NULL) {
1869 device_printf(sc->sc_dev,
1870 "could not defragment mbuf\n");
1871 m_freem(m0);
1872 return ENOBUFS;
1873 }
1874 m0 = mnew;
1875
1876 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map,
1877 m0, segs, &nsegs, 0);
1878 if (error != 0) {
1879 device_printf(sc->sc_dev,
1880 "could not map mbuf (error %d)\n", error);
1881 m_freem(m0);
1882 return error;
1883 }
1884 }
1885
1886 data->m = m0;
1887 data->ni = ni;
1888
1889 desc->hdr.type = IWI_HDR_TYPE_DATA;
1890 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1891 desc->station = staid;
1892 desc->cmd = IWI_DATA_CMD_TX;
1893 desc->len = htole16(m0->m_pkthdr.len);
1894 desc->flags = flags;
1895 desc->xflags = xflags;
1896
1897#if 0
1898 if (vap->iv_flags & IEEE80211_F_PRIVACY)
1899 desc->wep_txkey = vap->iv_def_txkey;
1900 else
1901#endif
1902 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1903
1904 desc->nseg = htole32(nsegs);
1905 for (i = 0; i < nsegs; i++) {
1906 desc->seg_addr[i] = htole32(segs[i].ds_addr);
1907 desc->seg_len[i] = htole16(segs[i].ds_len);
1908 }
1909
1910 bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1911 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1912
1913 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
1914 ac, txq->cur, le16toh(desc->len), nsegs));
1915
1916 txq->queued++;
1917 txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
1918 CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1919
1920 return 0;
1921}
1922
1923static int
1924iwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1925 const struct ieee80211_bpf_params *params)
1926{
1927 /* no support; just discard */
1928 m_freem(m);
1929 ieee80211_free_node(ni);
1930 return 0;
1931}
1932
1933static int
1934iwi_transmit(struct ieee80211com *ic, struct mbuf *m)
1935{
1936 struct iwi_softc *sc = ic->ic_softc;
1937 int error;
1938 IWI_LOCK_DECL;
1939
1940 IWI_LOCK(sc);
1941 if (!sc->sc_running) {
1942 IWI_UNLOCK(sc);
1943 return (ENXIO);
1944 }
1945 error = mbufq_enqueue(&sc->sc_snd, m);
1946 if (error) {
1947 IWI_UNLOCK(sc);
1948 return (error);
1949 }
1950 iwi_start(sc);
1951 IWI_UNLOCK(sc);
1952 return (0);
1953}
1954
1955static void
1956iwi_start(struct iwi_softc *sc)
1957{
1958 struct mbuf *m;
1959 struct ieee80211_node *ni;
1960 int ac;
1961
1962 IWI_LOCK_ASSERT(sc);
1963
1964 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1965 ac = M_WME_GETAC(m);
1966 if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) {
1967 /* there is no place left in this ring; tail drop */
1968 /* XXX tail drop */
1969 mbufq_prepend(&sc->sc_snd, m);
1970 break;
1971 }
1972 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1973 if (iwi_tx_start(sc, m, ni, ac) != 0) {
1974 ieee80211_free_node(ni);
1975 if_inc_counter(ni->ni_vap->iv_ifp,
1976 IFCOUNTER_OERRORS, 1);
1977 break;
1978 }
1979 sc->sc_tx_timer = 5;
1980 }
1981}
1982
1983static void
1984iwi_watchdog(void *arg)
1985{
1986 struct iwi_softc *sc = arg;
1987 struct ieee80211com *ic = &sc->sc_ic;
1988
1989 IWI_LOCK_ASSERT(sc);
1990
1991 if (sc->sc_tx_timer > 0) {
1992 if (--sc->sc_tx_timer == 0) {
1993 device_printf(sc->sc_dev, "device timeout\n");
1994 counter_u64_add(ic->ic_oerrors, 1);
1995 ieee80211_runtask(ic, &sc->sc_restarttask);
1996 }
1997 }
1998 if (sc->sc_state_timer > 0) {
1999 if (--sc->sc_state_timer == 0) {
2000 device_printf(sc->sc_dev,
2001 "firmware stuck in state %d, resetting\n",
2002 sc->fw_state);
2003 if (sc->fw_state == IWI_FW_SCANNING)
2004 ieee80211_cancel_scan(TAILQ_FIRST(&ic->ic_vaps));
2005 ieee80211_runtask(ic, &sc->sc_restarttask);
2006 sc->sc_state_timer = 3;
2007 }
2008 }
2009 if (sc->sc_busy_timer > 0) {
2010 if (--sc->sc_busy_timer == 0) {
2011 device_printf(sc->sc_dev,
2012 "firmware command timeout, resetting\n");
2013 ieee80211_runtask(ic, &sc->sc_restarttask);
2014 }
2015 }
2016 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
2017}
2018
2019static void
2020iwi_parent(struct ieee80211com *ic)
2021{
2022 struct iwi_softc *sc = ic->ic_softc;
2023 int startall = 0;
2024 IWI_LOCK_DECL;
2025
2026 IWI_LOCK(sc);
2027 if (ic->ic_nrunning > 0) {
2028 if (!sc->sc_running) {
2029 iwi_init_locked(sc);
2030 startall = 1;
2031 }
2032 } else if (sc->sc_running)
2033 iwi_stop_locked(sc);
2034 IWI_UNLOCK(sc);
2035 if (startall)
2036 ieee80211_start_all(ic);
2037}
2038
2039static int
2040iwi_ioctl(struct ieee80211com *ic, u_long cmd, void *data)
2041{
2042 struct ifreq *ifr = data;
2043 struct iwi_softc *sc = ic->ic_softc;
2044 int error;
2045 IWI_LOCK_DECL;
2046
2047 IWI_LOCK(sc);
2048 switch (cmd) {
2049 case SIOCGIWISTATS:
2050 /* XXX validate permissions/memory/etc? */
2051 error = copyout(&sc->sc_linkqual, ifr->ifr_data,
2052 sizeof(struct iwi_notif_link_quality));
2053 break;
2054 case SIOCZIWISTATS:
2055 memset(&sc->sc_linkqual, 0,
2056 sizeof(struct iwi_notif_link_quality));
2057 error = 0;
2058 break;
2059 default:
2060 error = ENOTTY;
2061 break;
2062 }
2063 IWI_UNLOCK(sc);
2064
2065 return (error);
2066}
2067
2068static void
2069iwi_stop_master(struct iwi_softc *sc)
2070{
2071 uint32_t tmp;
2072 int ntries;
2073
2074 /* disable interrupts */
2075 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
2076
2077 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
2078 for (ntries = 0; ntries < 5; ntries++) {
2079 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2080 break;
2081 DELAY(10);
2082 }
2083 if (ntries == 5)
2084 device_printf(sc->sc_dev, "timeout waiting for master\n");
2085
2086 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2087 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
2088
2089 sc->flags &= ~IWI_FLAG_FW_INITED;
2090}
2091
2092static int
2093iwi_reset(struct iwi_softc *sc)
2094{
2095 uint32_t tmp;
2096 int i, ntries;
2097
2098 iwi_stop_master(sc);
2099
2100 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2101 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2102
2103 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
2104
2105 /* wait for clock stabilization */
2106 for (ntries = 0; ntries < 1000; ntries++) {
2107 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
2108 break;
2109 DELAY(200);
2110 }
2111 if (ntries == 1000) {
2112 device_printf(sc->sc_dev,
2113 "timeout waiting for clock stabilization\n");
2114 return EIO;
2115 }
2116
2117 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2118 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET);
2119
2120 DELAY(10);
2121
2122 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2123 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2124
2125 /* clear NIC memory */
2126 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
2127 for (i = 0; i < 0xc000; i++)
2128 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2129
2130 return 0;
2131}
2132
2133static const struct iwi_firmware_ohdr *
2134iwi_setup_ofw(struct iwi_softc *sc, struct iwi_fw *fw)
2135{
2136 const struct firmware *fp = fw->fp;
2137 const struct iwi_firmware_ohdr *hdr;
2138
2139 if (fp->datasize < sizeof (struct iwi_firmware_ohdr)) {
2140 device_printf(sc->sc_dev, "image '%s' too small\n", fp->name);
2141 return NULL;
2142 }
2143 hdr = (const struct iwi_firmware_ohdr *)fp->data;
2144 if ((IWI_FW_GET_MAJOR(le32toh(hdr->version)) != IWI_FW_REQ_MAJOR) ||
2145 (IWI_FW_GET_MINOR(le32toh(hdr->version)) != IWI_FW_REQ_MINOR)) {
2146 device_printf(sc->sc_dev, "version for '%s' %d.%d != %d.%d\n",
2147 fp->name, IWI_FW_GET_MAJOR(le32toh(hdr->version)),
2148 IWI_FW_GET_MINOR(le32toh(hdr->version)), IWI_FW_REQ_MAJOR,
2149 IWI_FW_REQ_MINOR);
2150 return NULL;
2151 }
2152 fw->data = ((const char *) fp->data) + sizeof(struct iwi_firmware_ohdr);
2153 fw->size = fp->datasize - sizeof(struct iwi_firmware_ohdr);
2154 fw->name = fp->name;
2155 return hdr;
2156}
2157
2158static const struct iwi_firmware_ohdr *
2159iwi_setup_oucode(struct iwi_softc *sc, struct iwi_fw *fw)
2160{
2161 const struct iwi_firmware_ohdr *hdr;
2162
2163 hdr = iwi_setup_ofw(sc, fw);
2164 if (hdr != NULL && le32toh(hdr->mode) != IWI_FW_MODE_UCODE) {
2165 device_printf(sc->sc_dev, "%s is not a ucode image\n",
2166 fw->name);
2167 hdr = NULL;
2168 }
2169 return hdr;
2170}
2171
2172static void
2173iwi_getfw(struct iwi_fw *fw, const char *fwname,
2174 struct iwi_fw *uc, const char *ucname)
2175{
2176 if (fw->fp == NULL)
2177 fw->fp = firmware_get(fwname);
2178 /* NB: pre-3.0 ucode is packaged separately */
2179 if (uc->fp == NULL && fw->fp != NULL && fw->fp->version < 300)
2180 uc->fp = firmware_get(ucname);
2181}
2182
2183/*
2184 * Get the required firmware images if not already loaded.
2185 * Note that we hold firmware images so long as the device
2186 * is marked up in case we need to reload them on device init.
2187 * This is necessary because we re-init the device sometimes
2188 * from a context where we cannot read from the filesystem
2189 * (e.g. from the taskqueue thread when rfkill is re-enabled).
2190 * XXX return 0 on success, 1 on error.
2191 *
2192 * NB: the order of get'ing and put'ing images here is
2193 * intentional to support handling firmware images bundled
2194 * by operating mode and/or all together in one file with
2195 * the boot firmware as "master".
2196 */
2197static int
2198iwi_get_firmware(struct iwi_softc *sc, enum ieee80211_opmode opmode)
2199{
2200 const struct iwi_firmware_hdr *hdr;
2201 const struct firmware *fp;
2202
2203 /* invalidate cached firmware on mode change */
2204 if (sc->fw_mode != opmode)
2205 iwi_put_firmware(sc);
2206
2207 switch (opmode) {
2208 case IEEE80211_M_STA:
2209 iwi_getfw(&sc->fw_fw, "iwi_bss", &sc->fw_uc, "iwi_ucode_bss");
2210 break;
2211 case IEEE80211_M_IBSS:
2212 iwi_getfw(&sc->fw_fw, "iwi_ibss", &sc->fw_uc, "iwi_ucode_ibss");
2213 break;
2214 case IEEE80211_M_MONITOR:
2215 iwi_getfw(&sc->fw_fw, "iwi_monitor",
2216 &sc->fw_uc, "iwi_ucode_monitor");
2217 break;
2218 default:
2219 device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
2220 return EINVAL;
2221 }
2222 fp = sc->fw_fw.fp;
2223 if (fp == NULL) {
2224 device_printf(sc->sc_dev, "could not load firmware\n");
2225 goto bad;
2226 }
2227 if (fp->version < 300) {
2228 /*
2229 * Firmware prior to 3.0 was packaged as separate
2230 * boot, firmware, and ucode images. Verify the
2231 * ucode image was read in, retrieve the boot image
2232 * if needed, and check version stamps for consistency.
2233 * The version stamps in the data are also checked
2234 * above; this is a bit paranoid but is a cheap
2235 * safeguard against mis-packaging.
2236 */
2237 if (sc->fw_uc.fp == NULL) {
2238 device_printf(sc->sc_dev, "could not load ucode\n");
2239 goto bad;
2240 }
2241 if (sc->fw_boot.fp == NULL) {
2242 sc->fw_boot.fp = firmware_get("iwi_boot");
2243 if (sc->fw_boot.fp == NULL) {
2244 device_printf(sc->sc_dev,
2245 "could not load boot firmware\n");
2246 goto bad;
2247 }
2248 }
2249 if (sc->fw_boot.fp->version != sc->fw_fw.fp->version ||
2250 sc->fw_boot.fp->version != sc->fw_uc.fp->version) {
2251 device_printf(sc->sc_dev,
2252 "firmware version mismatch: "
2253 "'%s' is %d, '%s' is %d, '%s' is %d\n",
2254 sc->fw_boot.fp->name, sc->fw_boot.fp->version,
2255 sc->fw_uc.fp->name, sc->fw_uc.fp->version,
2256 sc->fw_fw.fp->name, sc->fw_fw.fp->version
2257 );
2258 goto bad;
2259 }
2260 /*
2261 * Check and setup each image.
2262 */
2263 if (iwi_setup_oucode(sc, &sc->fw_uc) == NULL ||
2264 iwi_setup_ofw(sc, &sc->fw_boot) == NULL ||
2265 iwi_setup_ofw(sc, &sc->fw_fw) == NULL)
2266 goto bad;
2267 } else {
2268 /*
2269 * Check and setup combined image.
2270 */
2271 if (fp->datasize < sizeof(struct iwi_firmware_hdr)) {
2272 device_printf(sc->sc_dev, "image '%s' too small\n",
2273 fp->name);
2274 goto bad;
2275 }
2276 hdr = (const struct iwi_firmware_hdr *)fp->data;
2277 if (fp->datasize < sizeof(*hdr) + le32toh(hdr->bsize) + le32toh(hdr->usize)
2278 + le32toh(hdr->fsize)) {
2279 device_printf(sc->sc_dev, "image '%s' too small (2)\n",
2280 fp->name);
2281 goto bad;
2282 }
2283 sc->fw_boot.data = ((const char *) fp->data) + sizeof(*hdr);
2284 sc->fw_boot.size = le32toh(hdr->bsize);
2285 sc->fw_boot.name = fp->name;
2286 sc->fw_uc.data = sc->fw_boot.data + sc->fw_boot.size;
2287 sc->fw_uc.size = le32toh(hdr->usize);
2288 sc->fw_uc.name = fp->name;
2289 sc->fw_fw.data = sc->fw_uc.data + sc->fw_uc.size;
2290 sc->fw_fw.size = le32toh(hdr->fsize);
2291 sc->fw_fw.name = fp->name;
2292 }
2293#if 0
2294 device_printf(sc->sc_dev, "boot %d ucode %d fw %d bytes\n",
2295 sc->fw_boot.size, sc->fw_uc.size, sc->fw_fw.size);
2296#endif
2297
2298 sc->fw_mode = opmode;
2299 return 0;
2300bad:
2301 iwi_put_firmware(sc);
2302 return 1;
2303}
2304
2305static void
2306iwi_put_fw(struct iwi_fw *fw)
2307{
2308 if (fw->fp != NULL) {
2309 firmware_put(fw->fp, FIRMWARE_UNLOAD);
2310 fw->fp = NULL;
2311 }
2312 fw->data = NULL;
2313 fw->size = 0;
2314 fw->name = NULL;
2315}
2316
2317/*
2318 * Release any cached firmware images.
2319 */
2320static void
2321iwi_put_firmware(struct iwi_softc *sc)
2322{
2323 iwi_put_fw(&sc->fw_uc);
2324 iwi_put_fw(&sc->fw_fw);
2325 iwi_put_fw(&sc->fw_boot);
2326}
2327
2328static int
2329iwi_load_ucode(struct iwi_softc *sc, const struct iwi_fw *fw)
2330{
2331 uint32_t tmp;
2332 const uint16_t *w;
2333 const char *uc = fw->data;
2334 size_t size = fw->size;
2335 int i, ntries, error;
2336
2337 IWI_LOCK_ASSERT(sc);
2338 error = 0;
2339 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
2340 IWI_RST_STOP_MASTER);
2341 for (ntries = 0; ntries < 5; ntries++) {
2342 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2343 break;
2344 DELAY(10);
2345 }
2346 if (ntries == 5) {
2347 device_printf(sc->sc_dev, "timeout waiting for master\n");
2348 error = EIO;
2349 goto fail;
2350 }
2351
2352 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2353 DELAY(5000);
2354
2355 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2356 tmp &= ~IWI_RST_PRINCETON_RESET;
2357 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2358
2359 DELAY(5000);
2360 MEM_WRITE_4(sc, 0x3000e0, 0);
2361 DELAY(1000);
2362 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 1);
2363 DELAY(1000);
2364 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 0);
2365 DELAY(1000);
2366 MEM_WRITE_1(sc, 0x200000, 0x00);
2367 MEM_WRITE_1(sc, 0x200000, 0x40);
2368 DELAY(1000);
2369
2370 /* write microcode into adapter memory */
2371 for (w = (const uint16_t *)uc; size > 0; w++, size -= 2)
2372 MEM_WRITE_2(sc, 0x200010, htole16(*w));
2373
2374 MEM_WRITE_1(sc, 0x200000, 0x00);
2375 MEM_WRITE_1(sc, 0x200000, 0x80);
2376
2377 /* wait until we get an answer */
2378 for (ntries = 0; ntries < 100; ntries++) {
2379 if (MEM_READ_1(sc, 0x200000) & 1)
2380 break;
2381 DELAY(100);
2382 }
2383 if (ntries == 100) {
2384 device_printf(sc->sc_dev,
2385 "timeout waiting for ucode to initialize\n");
2386 error = EIO;
2387 goto fail;
2388 }
2389
2390 /* read the answer or the firmware will not initialize properly */
2391 for (i = 0; i < 7; i++)
2392 MEM_READ_4(sc, 0x200004);
2393
2394 MEM_WRITE_1(sc, 0x200000, 0x00);
2395
2396fail:
2397 return error;
2398}
2399
2400/* macro to handle unaligned little endian data in firmware image */
2401#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2402
2403static int
2404iwi_load_firmware(struct iwi_softc *sc, const struct iwi_fw *fw)
2405{
2406 u_char *p, *end;
2407 uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp;
2408 int ntries, error;
2409
2410 IWI_LOCK_ASSERT(sc);
2411
2412 /* copy firmware image to DMA memory */
2413 memcpy(sc->fw_virtaddr, fw->data, fw->size);
2414
2415 /* make sure the adapter will get up-to-date values */
2416 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_PREWRITE);
2417
2418 /* tell the adapter where the command blocks are stored */
2419 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2420
2421 /*
2422 * Store command blocks into adapter's internal memory using register
2423 * indirections. The adapter will read the firmware image through DMA
2424 * using information stored in command blocks.
2425 */
2426 src = sc->fw_physaddr;
2427 p = sc->fw_virtaddr;
2428 end = p + fw->size;
2429 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2430
2431 while (p < end) {
2432 dst = GETLE32(p); p += 4; src += 4;
2433 len = GETLE32(p); p += 4; src += 4;
2434 p += len;
2435
2436 while (len > 0) {
2437 mlen = min(len, IWI_CB_MAXDATALEN);
2438
2439 ctl = IWI_CB_DEFAULT_CTL | mlen;
2440 sum = ctl ^ src ^ dst;
2441
2442 /* write a command block */
2443 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2444 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2445 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2446 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2447
2448 src += mlen;
2449 dst += mlen;
2450 len -= mlen;
2451 }
2452 }
2453
2454 /* write a fictive final command block (sentinel) */
2455 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2456 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2457
2458 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2459 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
2460 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2461
2462 /* tell the adapter to start processing command blocks */
2463 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2464
2465 /* wait until the adapter reaches the sentinel */
2466 for (ntries = 0; ntries < 400; ntries++) {
2467 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2468 break;
2469 DELAY(100);
2470 }
2471 /* sync dma, just in case */
2472 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_POSTWRITE);
2473 if (ntries == 400) {
2474 device_printf(sc->sc_dev,
2475 "timeout processing command blocks for %s firmware\n",
2476 fw->name);
2477 return EIO;
2478 }
2479
2480 /* we're done with command blocks processing */
2481 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2482
2483 /* allow interrupts so we know when the firmware is ready */
2484 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2485
2486 /* tell the adapter to initialize the firmware */
2487 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2488
2489 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2490 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
2491
2492 /* wait at most one second for firmware initialization to complete */
2493 if ((error = msleep(sc, &sc->sc_mtx, 0, "iwiinit", hz)) != 0) {
2494 device_printf(sc->sc_dev, "timeout waiting for %s firmware "
2495 "initialization to complete\n", fw->name);
2496 }
2497
2498 return error;
2499}
2500
2501static int
2502iwi_setpowermode(struct iwi_softc *sc, struct ieee80211vap *vap)
2503{
2504 uint32_t data;
2505
2506 if (vap->iv_flags & IEEE80211_F_PMGTON) {
2507 /* XXX set more fine-grained operation */
2508 data = htole32(IWI_POWER_MODE_MAX);
2509 } else
2510 data = htole32(IWI_POWER_MODE_CAM);
2511
2512 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2513 return iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data);
2514}
2515
2516static int
2517iwi_setwepkeys(struct iwi_softc *sc, struct ieee80211vap *vap)
2518{
2519 struct iwi_wep_key wepkey;
2520 struct ieee80211_key *wk;
2521 int error, i;
2522
2523 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2524 wk = &vap->iv_nw_keys[i];
2525
2526 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2527 wepkey.idx = i;
2528 wepkey.len = wk->wk_keylen;
2529 memset(wepkey.key, 0, sizeof wepkey.key);
2530 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2531 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2532 wepkey.len));
2533 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2534 sizeof wepkey);
2535 if (error != 0)
2536 return error;
2537 }
2538 return 0;
2539}
2540
2541static int
2542iwi_config(struct iwi_softc *sc)
2543{
2544 struct ieee80211com *ic = &sc->sc_ic;
2545 struct iwi_configuration config;
2546 struct iwi_rateset rs;
2547 struct iwi_txpower power;
2548 uint32_t data;
2549 int error, i;
2550
2551 IWI_LOCK_ASSERT(sc);
2552
2553 DPRINTF(("Setting MAC address to %6D\n", ic->ic_macaddr, ":"));
2554 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_macaddr,
2555 IEEE80211_ADDR_LEN);
2556 if (error != 0)
2557 return error;
2558
2559 memset(&config, 0, sizeof config);
2560 config.bluetooth_coexistence = sc->bluetooth;
2561 config.silence_threshold = 0x1e;
2562 config.antenna = sc->antenna;
2563 config.multicast_enabled = 1;
2564 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2565 config.disable_unicast_decryption = 1;
2566 config.disable_multicast_decryption = 1;
2567 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2568 config.allow_invalid_frames = 1;
2569 config.allow_beacon_and_probe_resp = 1;
2570 config.allow_mgt = 1;
2571 }
2572 DPRINTF(("Configuring adapter\n"));
2573 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2574 if (error != 0)
2575 return error;
2576 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2577 power.mode = IWI_MODE_11B;
2578 power.nchan = 11;
2579 for (i = 0; i < 11; i++) {
2580 power.chan[i].chan = i + 1;
2581 power.chan[i].power = IWI_TXPOWER_MAX;
2582 }
2583 DPRINTF(("Setting .11b channels tx power\n"));
2584 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2585 if (error != 0)
2586 return error;
2587
2588 power.mode = IWI_MODE_11G;
2589 DPRINTF(("Setting .11g channels tx power\n"));
2590 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2591 if (error != 0)
2592 return error;
2593 }
2594
2595 memset(&rs, 0, sizeof rs);
2596 rs.mode = IWI_MODE_11G;
2597 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2598 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2599 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2600 rs.nrates);
2601 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2602 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2603 if (error != 0)
2604 return error;
2605
2606 memset(&rs, 0, sizeof rs);
2607 rs.mode = IWI_MODE_11A;
2608 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2609 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2610 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2611 rs.nrates);
2612 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2613 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2614 if (error != 0)
2615 return error;
2616
2617 data = htole32(arc4random());
2618 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2619 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data);
2620 if (error != 0)
2621 return error;
2622
2623 /* enable adapter */
2624 DPRINTF(("Enabling adapter\n"));
2625 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0);
2626}
2627
2628static __inline void
2629set_scan_type(struct iwi_scan_ext *scan, int ix, int scan_type)
2630{
2631 uint8_t *st = &scan->scan_type[ix / 2];
2632 if (ix % 2)
2633 *st = (*st & 0xf0) | ((scan_type & 0xf) << 0);
2634 else
2635 *st = (*st & 0x0f) | ((scan_type & 0xf) << 4);
2636}
2637
2638static int
2639scan_type(const struct ieee80211_scan_state *ss,
2640 const struct ieee80211_channel *chan)
2641{
2642 /* We can only set one essid for a directed scan */
2643 if (ss->ss_nssid != 0)
2644 return IWI_SCAN_TYPE_BDIRECTED;
2645 if ((ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
2646 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
2647 return IWI_SCAN_TYPE_BROADCAST;
2648 return IWI_SCAN_TYPE_PASSIVE;
2649}
2650
2651static __inline int
2652scan_band(const struct ieee80211_channel *c)
2653{
2654 return IEEE80211_IS_CHAN_5GHZ(c) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ;
2655}
2656
2657static void
2658iwi_monitor_scan(void *arg, int npending)
2659{
2660 struct iwi_softc *sc = arg;
2661 IWI_LOCK_DECL;
2662
2663 IWI_LOCK(sc);
2664 (void) iwi_scanchan(sc, 2000, 0);
2665 IWI_UNLOCK(sc);
2666}
2667
2668/*
2669 * Start a scan on the current channel or all channels.
2670 */
2671static int
2672iwi_scanchan(struct iwi_softc *sc, unsigned long maxdwell, int allchan)
2673{
2674 struct ieee80211com *ic = &sc->sc_ic;
2675 struct ieee80211_channel *chan;
2676 struct ieee80211_scan_state *ss;
2677 struct iwi_scan_ext scan;
2678 int error = 0;
2679
2680 IWI_LOCK_ASSERT(sc);
2681 if (sc->fw_state == IWI_FW_SCANNING) {
2682 /*
2683 * This should not happen as we only trigger scan_next after
2684 * completion
2685 */
2686 DPRINTF(("%s: called too early - still scanning\n", __func__));
2687 return (EBUSY);
2688 }
2689 IWI_STATE_BEGIN(sc, IWI_FW_SCANNING);
2690
2691 ss = ic->ic_scan;
2692
2693 memset(&scan, 0, sizeof scan);
2694 scan.full_scan_index = htole32(++sc->sc_scangen);
2695 scan.dwell_time[IWI_SCAN_TYPE_PASSIVE] = htole16(maxdwell);
2696 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
2697 /*
2698 * Use very short dwell times for when we send probe request
2699 * frames. Without this bg scans hang. Ideally this should
2700 * be handled with early-termination as done by net80211 but
2701 * that's not feasible (aborting a scan is problematic).
2702 */
2703 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(30);
2704 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(30);
2705 } else {
2706 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(maxdwell);
2707 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(maxdwell);
2708 }
2709
2710 /* We can only set one essid for a directed scan */
2711 if (ss->ss_nssid != 0) {
2712 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ss->ss_ssid[0].ssid,
2713 ss->ss_ssid[0].len);
2714 if (error)
2715 return (error);
2716 }
2717
2718 if (allchan) {
2719 int i, next, band, b, bstart;
2720 /*
2721 * Convert scan list to run-length encoded channel list
2722 * the firmware requires (preserving the order setup by
2723 * net80211). The first entry in each run specifies the
2724 * band and the count of items in the run.
2725 */
2726 next = 0; /* next open slot */
2727 bstart = 0; /* NB: not needed, silence compiler */
2728 band = -1; /* NB: impossible value */
2729 KASSERT(ss->ss_last > 0, ("no channels"));
2730 for (i = 0; i < ss->ss_last; i++) {
2731 chan = ss->ss_chans[i];
2732 b = scan_band(chan);
2733 if (b != band) {
2734 if (band != -1)
2735 scan.channels[bstart] =
2736 (next - bstart) | band;
2737 /* NB: this allocates a slot for the run-len */
2738 band = b, bstart = next++;
2739 }
2740 if (next >= IWI_SCAN_CHANNELS) {
2741 DPRINTF(("truncating scan list\n"));
2742 break;
2743 }
2744 scan.channels[next] = ieee80211_chan2ieee(ic, chan);
2745 set_scan_type(&scan, next, scan_type(ss, chan));
2746 next++;
2747 }
2748 scan.channels[bstart] = (next - bstart) | band;
2749 } else {
2750 /* Scan the current channel only */
2751 chan = ic->ic_curchan;
2752 scan.channels[0] = 1 | scan_band(chan);
2753 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2754 set_scan_type(&scan, 1, scan_type(ss, chan));
2755 }
2756#ifdef IWI_DEBUG
2757 if (iwi_debug > 0) {
2758 static const char *scantype[8] =
2759 { "PSTOP", "PASV", "DIR", "BCAST", "BDIR", "5", "6", "7" };
2760 int i;
2761 printf("Scan request: index %u dwell %d/%d/%d\n"
2762 , le32toh(scan.full_scan_index)
2763 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_PASSIVE])
2764 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BROADCAST])
2765 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED])
2766 );
2767 i = 0;
2768 do {
2769 int run = scan.channels[i];
2770 if (run == 0)
2771 break;
2772 printf("Scan %d %s channels:", run & 0x3f,
2773 run & IWI_CHAN_2GHZ ? "2.4GHz" : "5GHz");
2774 for (run &= 0x3f, i++; run > 0; run--, i++) {
2775 uint8_t type = scan.scan_type[i/2];
2776 printf(" %u/%s", scan.channels[i],
2777 scantype[(i & 1 ? type : type>>4) & 7]);
2778 }
2779 printf("\n");
2780 } while (i < IWI_SCAN_CHANNELS);
2781 }
2782#endif
2783
2784 return (iwi_cmd(sc, IWI_CMD_SCAN_EXT, &scan, sizeof scan));
2785}
2786
2787static int
2788iwi_set_sensitivity(struct iwi_softc *sc, int8_t rssi_dbm)
2789{
2790 struct iwi_sensitivity sens;
2791
2792 DPRINTF(("Setting sensitivity to %d\n", rssi_dbm));
2793
2794 memset(&sens, 0, sizeof sens);
2795 sens.rssi = htole16(rssi_dbm);
2796 return iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &sens, sizeof sens);
2797}
2798
2799static int
2800iwi_auth_and_assoc(struct iwi_softc *sc, struct ieee80211vap *vap)
2801{
2802 struct ieee80211com *ic = vap->iv_ic;
2803 struct ifnet *ifp = vap->iv_ifp;
2804 struct ieee80211_node *ni;
2805 struct iwi_configuration config;
2806 struct iwi_associate *assoc = &sc->assoc;
2807 struct iwi_rateset rs;
2808 uint16_t capinfo;
2809 uint32_t data;
2810 int error, mode;
2811
2812 IWI_LOCK_ASSERT(sc);
2813
2814 ni = ieee80211_ref_node(vap->iv_bss);
2815
2816 if (sc->flags & IWI_FLAG_ASSOCIATED) {
2817 DPRINTF(("Already associated\n"));
2818 return (-1);
2819 }
2820
2821 IWI_STATE_BEGIN(sc, IWI_FW_ASSOCIATING);
2822 error = 0;
2823 mode = 0;
2824
2825 if (IEEE80211_IS_CHAN_A(ic->ic_curchan))
2826 mode = IWI_MODE_11A;
2827 else if (IEEE80211_IS_CHAN_G(ic->ic_curchan))
2828 mode = IWI_MODE_11G;
2829 if (IEEE80211_IS_CHAN_B(ic->ic_curchan))
2830 mode = IWI_MODE_11B;
2831
2832 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2833 memset(&config, 0, sizeof config);
2834 config.bluetooth_coexistence = sc->bluetooth;
2835 config.antenna = sc->antenna;
2836 config.multicast_enabled = 1;
2837 if (mode == IWI_MODE_11G)
2838 config.use_protection = 1;
2839 config.answer_pbreq =
2840 (vap->iv_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2841 config.disable_unicast_decryption = 1;
2842 config.disable_multicast_decryption = 1;
2843 DPRINTF(("Configuring adapter\n"));
2844 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2845 if (error != 0)
2846 goto done;
2847 }
2848
2849#ifdef IWI_DEBUG
2850 if (iwi_debug > 0) {
2851 printf("Setting ESSID to ");
2852 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2853 printf("\n");
2854 }
2855#endif
2856 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen);
2857 if (error != 0)
2858 goto done;
2859
2860 error = iwi_setpowermode(sc, vap);
2861 if (error != 0)
2862 goto done;
2863
2864 data = htole32(vap->iv_rtsthreshold);
2865 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2866 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2867 if (error != 0)
2868 goto done;
2869
2870 data = htole32(vap->iv_fragthreshold);
2871 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2872 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2873 if (error != 0)
2874 goto done;
2875
2876 /* the rate set has already been "negotiated" */
2877 memset(&rs, 0, sizeof rs);
2878 rs.mode = mode;
2879 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2880 rs.nrates = ni->ni_rates.rs_nrates;
2881 if (rs.nrates > IWI_RATESET_SIZE) {
2882 DPRINTF(("Truncating negotiated rate set from %u\n",
2883 rs.nrates));
2884 rs.nrates = IWI_RATESET_SIZE;
2885 }
2886 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2887 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
2888 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2889 if (error != 0)
2890 goto done;
2891
2892 memset(assoc, 0, sizeof *assoc);
2893
2894 if ((vap->iv_flags & IEEE80211_F_WME) && ni->ni_ies.wme_ie != NULL) {
2895 /* NB: don't treat WME setup as failure */
2896 if (iwi_wme_setparams(sc) == 0 && iwi_wme_setie(sc) == 0)
2897 assoc->policy |= htole16(IWI_POLICY_WME);
2898 /* XXX complain on failure? */
2899 }
2900
2901 if (vap->iv_appie_wpa != NULL) {
2902 struct ieee80211_appie *ie = vap->iv_appie_wpa;
2903
2904 DPRINTF(("Setting optional IE (len=%u)\n", ie->ie_len));
2905 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ie->ie_data, ie->ie_len);
2906 if (error != 0)
2907 goto done;
2908 }
2909
2910 error = iwi_set_sensitivity(sc, ic->ic_node_getrssi(ni));
2911 if (error != 0)
2912 goto done;
2913
2914 assoc->mode = mode;
2915 assoc->chan = ic->ic_curchan->ic_ieee;
2916 /*
2917 * NB: do not arrange for shared key auth w/o privacy
2918 * (i.e. a wep key); it causes a firmware error.
2919 */
2920 if ((vap->iv_flags & IEEE80211_F_PRIVACY) &&
2921 ni->ni_authmode == IEEE80211_AUTH_SHARED) {
2922 assoc->auth = IWI_AUTH_SHARED;
2923 /*
2924 * It's possible to have privacy marked but no default
2925 * key setup. This typically is due to a user app bug
2926 * but if we blindly grab the key the firmware will
2927 * barf so avoid it for now.
2928 */
2929 if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE)
2930 assoc->auth |= vap->iv_def_txkey << 4;
2931
2932 error = iwi_setwepkeys(sc, vap);
2933 if (error != 0)
2934 goto done;
2935 }
2936 if (vap->iv_flags & IEEE80211_F_WPA)
2937 assoc->policy |= htole16(IWI_POLICY_WPA);
2938 if (vap->iv_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0)
2939 assoc->type = IWI_HC_IBSS_START;
2940 else
2941 assoc->type = IWI_HC_ASSOC;
2942 memcpy(assoc->tstamp, ni->ni_tstamp.data, 8);
2943
2944 if (vap->iv_opmode == IEEE80211_M_IBSS)
2945 capinfo = IEEE80211_CAPINFO_IBSS;
2946 else
2947 capinfo = IEEE80211_CAPINFO_ESS;
2948 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2949 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2950 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2951 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2952 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2953 if (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2954 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2955 assoc->capinfo = htole16(capinfo);
2956
2957 assoc->lintval = htole16(ic->ic_lintval);
2958 assoc->intval = htole16(ni->ni_intval);
2959 IEEE80211_ADDR_COPY(assoc->bssid, ni->ni_bssid);
2960 if (vap->iv_opmode == IEEE80211_M_IBSS)
2961 IEEE80211_ADDR_COPY(assoc->dst, ifp->if_broadcastaddr);
2962 else
2963 IEEE80211_ADDR_COPY(assoc->dst, ni->ni_bssid);
2964
2965 DPRINTF(("%s bssid %6D dst %6D channel %u policy 0x%x "
2966 "auth %u capinfo 0x%x lintval %u bintval %u\n",
2967 assoc->type == IWI_HC_IBSS_START ? "Start" : "Join",
2968 assoc->bssid, ":", assoc->dst, ":",
2969 assoc->chan, le16toh(assoc->policy), assoc->auth,
2970 le16toh(assoc->capinfo), le16toh(assoc->lintval),
2971 le16toh(assoc->intval)));
2972 error = iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
2973done:
2974 ieee80211_free_node(ni);
2975 if (error)
2976 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
2977
2978 return (error);
2979}
2980
2981static void
2982iwi_disassoc(void *arg, int pending)
2983{
2984 struct iwi_softc *sc = arg;
2985 IWI_LOCK_DECL;
2986
2987 IWI_LOCK(sc);
2988 iwi_disassociate(sc, 0);
2989 IWI_UNLOCK(sc);
2990}
2991
2992static int
2993iwi_disassociate(struct iwi_softc *sc, int quiet)
2994{
2995 struct iwi_associate *assoc = &sc->assoc;
2996
2997 if ((sc->flags & IWI_FLAG_ASSOCIATED) == 0) {
2998 DPRINTF(("Not associated\n"));
2999 return (-1);
3000 }
3001
3002 IWI_STATE_BEGIN(sc, IWI_FW_DISASSOCIATING);
3003
3004 if (quiet)
3005 assoc->type = IWI_HC_DISASSOC_QUIET;
3006 else
3007 assoc->type = IWI_HC_DISASSOC;
3008
3009 DPRINTF(("Trying to disassociate from %6D channel %u\n",
3010 assoc->bssid, ":", assoc->chan));
3011 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
3012}
3013
3014/*
3015 * release dma resources for the firmware
3016 */
3017static void
3018iwi_release_fw_dma(struct iwi_softc *sc)
3019{
3020 if (sc->fw_flags & IWI_FW_HAVE_PHY)
3021 bus_dmamap_unload(sc->fw_dmat, sc->fw_map);
3022 if (sc->fw_flags & IWI_FW_HAVE_MAP)
3023 bus_dmamem_free(sc->fw_dmat, sc->fw_virtaddr, sc->fw_map);
3024 if (sc->fw_flags & IWI_FW_HAVE_DMAT)
3025 bus_dma_tag_destroy(sc->fw_dmat);
3026
3027 sc->fw_flags = 0;
3028 sc->fw_dma_size = 0;
3029 sc->fw_dmat = NULL;
3030 sc->fw_map = NULL;
3031 sc->fw_physaddr = 0;
3032 sc->fw_virtaddr = NULL;
3033}
3034
3035/*
3036 * allocate the dma descriptor for the firmware.
3037 * Return 0 on success, 1 on error.
3038 * Must be called unlocked, protected by IWI_FLAG_FW_LOADING.
3039 */
3040static int
3041iwi_init_fw_dma(struct iwi_softc *sc, int size)
3042{
3043 if (sc->fw_dma_size >= size)
3044 return 0;
3045 if (bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
3046 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
3047 size, 1, size, 0, NULL, NULL, &sc->fw_dmat) != 0) {
3048 device_printf(sc->sc_dev,
3049 "could not create firmware DMA tag\n");
3050 goto error;
3051 }
3052 sc->fw_flags |= IWI_FW_HAVE_DMAT;
3053 if (bus_dmamem_alloc(sc->fw_dmat, &sc->fw_virtaddr, 0,
3054 &sc->fw_map) != 0) {
3055 device_printf(sc->sc_dev,
3056 "could not allocate firmware DMA memory\n");
3057 goto error;
3058 }
3059 sc->fw_flags |= IWI_FW_HAVE_MAP;
3060 if (bus_dmamap_load(sc->fw_dmat, sc->fw_map, sc->fw_virtaddr,
3061 size, iwi_dma_map_addr, &sc->fw_physaddr, 0) != 0) {
3062 device_printf(sc->sc_dev, "could not load firmware DMA map\n");
3063 goto error;
3064 }
3065 sc->fw_flags |= IWI_FW_HAVE_PHY;
3066 sc->fw_dma_size = size;
3067 return 0;
3068
3069error:
3070 iwi_release_fw_dma(sc);
3071 return 1;
3072}
3073
3074static void
3075iwi_init_locked(struct iwi_softc *sc)
3076{
3077 struct iwi_rx_data *data;
3078 int i;
3079
3080 IWI_LOCK_ASSERT(sc);
3081
3082 if (sc->fw_state == IWI_FW_LOADING) {
3083 device_printf(sc->sc_dev, "%s: already loading\n", __func__);
3084 return; /* XXX: condvar? */
3085 }
3086
3087 iwi_stop_locked(sc);
3088
3089 IWI_STATE_BEGIN(sc, IWI_FW_LOADING);
3090
3091 if (iwi_reset(sc) != 0) {
3092 device_printf(sc->sc_dev, "could not reset adapter\n");
3093 goto fail;
3094 }
3095 if (iwi_load_firmware(sc, &sc->fw_boot) != 0) {
3096 device_printf(sc->sc_dev,
3097 "could not load boot firmware %s\n", sc->fw_boot.name);
3098 goto fail;
3099 }
3100 if (iwi_load_ucode(sc, &sc->fw_uc) != 0) {
3101 device_printf(sc->sc_dev,
3102 "could not load microcode %s\n", sc->fw_uc.name);
3103 goto fail;
3104 }
3105
3106 iwi_stop_master(sc);
3107
3108 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr);
3109 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
3110 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
3111
3112 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr);
3113 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
3114 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
3115
3116 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr);
3117 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
3118 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
3119
3120 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr);
3121 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
3122 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
3123
3124 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr);
3125 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
3126 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
3127
3128 for (i = 0; i < sc->rxq.count; i++) {
3129 data = &sc->rxq.data[i];
3130 CSR_WRITE_4(sc, data->reg, data->physaddr);
3131 }
3132
3133 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1);
3134
3135 if (iwi_load_firmware(sc, &sc->fw_fw) != 0) {
3136 device_printf(sc->sc_dev,
3137 "could not load main firmware %s\n", sc->fw_fw.name);
3138 goto fail;
3139 }
3140 sc->flags |= IWI_FLAG_FW_INITED;
3141
3142 IWI_STATE_END(sc, IWI_FW_LOADING);
3143
3144 if (iwi_config(sc) != 0) {
3145 device_printf(sc->sc_dev, "unable to enable adapter\n");
3146 goto fail2;
3147 }
3148
3149 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
3150 sc->sc_running = 1;
3151 return;
3152fail:
3153 IWI_STATE_END(sc, IWI_FW_LOADING);
3154fail2:
3155 iwi_stop_locked(sc);
3156}
3157
3158static void
3159iwi_init(void *priv)
3160{
3161 struct iwi_softc *sc = priv;
3162 struct ieee80211com *ic = &sc->sc_ic;
3163 IWI_LOCK_DECL;
3164
3165 IWI_LOCK(sc);
3166 iwi_init_locked(sc);
3167 IWI_UNLOCK(sc);
3168
3169 if (sc->sc_running)
3170 ieee80211_start_all(ic);
3171}
3172
3173static void
3174iwi_stop_locked(void *priv)
3175{
3176 struct iwi_softc *sc = priv;
3177
3178 IWI_LOCK_ASSERT(sc);
3179
3180 sc->sc_running = 0;
3181
3182 if (sc->sc_softled) {
3183 callout_stop(&sc->sc_ledtimer);
3184 sc->sc_blinking = 0;
3185 }
3186 callout_stop(&sc->sc_wdtimer);
3187 callout_stop(&sc->sc_rftimer);
3188
3189 iwi_stop_master(sc);
3190
3191 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET);
3192
3193 /* reset rings */
3194 iwi_reset_cmd_ring(sc, &sc->cmdq);
3195 iwi_reset_tx_ring(sc, &sc->txq[0]);
3196 iwi_reset_tx_ring(sc, &sc->txq[1]);
3197 iwi_reset_tx_ring(sc, &sc->txq[2]);
3198 iwi_reset_tx_ring(sc, &sc->txq[3]);
3199 iwi_reset_rx_ring(sc, &sc->rxq);
3200
3201 sc->sc_tx_timer = 0;
3202 sc->sc_state_timer = 0;
3203 sc->sc_busy_timer = 0;
3204 sc->flags &= ~(IWI_FLAG_BUSY | IWI_FLAG_ASSOCIATED);
3205 sc->fw_state = IWI_FW_IDLE;
3206 wakeup(sc);
3207}
3208
3209static void
3210iwi_stop(struct iwi_softc *sc)
3211{
3212 IWI_LOCK_DECL;
3213
3214 IWI_LOCK(sc);
3215 iwi_stop_locked(sc);
3216 IWI_UNLOCK(sc);
3217}
3218
3219static void
3220iwi_restart(void *arg, int npending)
3221{
3222 struct iwi_softc *sc = arg;
3223
3224 iwi_init(sc);
3225}
3226
3227/*
3228 * Return whether or not the radio is enabled in hardware
3229 * (i.e. the rfkill switch is "off").
3230 */
3231static int
3232iwi_getrfkill(struct iwi_softc *sc)
3233{
3234 return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0;
3235}
3236
3237static void
3238iwi_radio_on(void *arg, int pending)
3239{
3240 struct iwi_softc *sc = arg;
3241 struct ieee80211com *ic = &sc->sc_ic;
3242
3243 device_printf(sc->sc_dev, "radio turned on\n");
3244
3245 iwi_init(sc);
3246 ieee80211_notify_radio(ic, 1);
3247}
3248
3249static void
3250iwi_rfkill_poll(void *arg)
3251{
3252 struct iwi_softc *sc = arg;
3253
3254 IWI_LOCK_ASSERT(sc);
3255
3256 /*
3257 * Check for a change in rfkill state. We get an
3258 * interrupt when a radio is disabled but not when
3259 * it is enabled so we must poll for the latter.
3260 */
3261 if (!iwi_getrfkill(sc)) {
3262 ieee80211_runtask(&sc->sc_ic, &sc->sc_radiontask);
3263 return;
3264 }
3265 callout_reset(&sc->sc_rftimer, 2*hz, iwi_rfkill_poll, sc);
3266}
3267
3268static void
3269iwi_radio_off(void *arg, int pending)
3270{
3271 struct iwi_softc *sc = arg;
3272 struct ieee80211com *ic = &sc->sc_ic;
3273 IWI_LOCK_DECL;
3274
3275 device_printf(sc->sc_dev, "radio turned off\n");
3276
3277 ieee80211_notify_radio(ic, 0);
3278
3279 IWI_LOCK(sc);
3280 iwi_stop_locked(sc);
3281 iwi_rfkill_poll(sc);
3282 IWI_UNLOCK(sc);
3283}
3284
3285static int
3286iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
3287{
3288 struct iwi_softc *sc = arg1;
3289 uint32_t size, buf[128];
3290
3291 memset(buf, 0, sizeof buf);
3292
3293 if (!(sc->flags & IWI_FLAG_FW_INITED))
3294 return SYSCTL_OUT(req, buf, sizeof buf);
3295
3296 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
3297 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
3298
3299 return SYSCTL_OUT(req, buf, size);
3300}
3301
3302static int
3303iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
3304{
3305 struct iwi_softc *sc = arg1;
3306 int val = !iwi_getrfkill(sc);
3307
3308 return SYSCTL_OUT(req, &val, sizeof val);
3309}
3310
3311/*
3312 * Add sysctl knobs.
3313 */
3314static void
3315iwi_sysctlattach(struct iwi_softc *sc)
3316{
3317 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3318 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3319
3320 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "radio",
3321 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I",
3322 "radio transmitter switch state (0=off, 1=on)");
3323
3324 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "stats",
3325 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S",
3326 "statistics");
3327
3328 sc->bluetooth = 0;
3329 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "bluetooth",
3330 CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence");
3331
3332 sc->antenna = IWI_ANTENNA_AUTO;
3333 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "antenna",
3334 CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)");
3335}
3336
3337/*
3338 * LED support.
3339 *
3340 * Different cards have different capabilities. Some have three
3341 * led's while others have only one. The linux ipw driver defines
3342 * led's for link state (associated or not), band (11a, 11g, 11b),
3343 * and for link activity. We use one led and vary the blink rate
3344 * according to the tx/rx traffic a la the ath driver.
3345 */
3346
3347static __inline uint32_t
3348iwi_toggle_event(uint32_t r)
3349{
3350 return r &~ (IWI_RST_STANDBY | IWI_RST_GATE_ODMA |
3351 IWI_RST_GATE_IDMA | IWI_RST_GATE_ADMA);
3352}
3353
3354static uint32_t
3355iwi_read_event(struct iwi_softc *sc)
3356{
3357 return MEM_READ_4(sc, IWI_MEM_EEPROM_EVENT);
3358}
3359
3360static void
3361iwi_write_event(struct iwi_softc *sc, uint32_t v)
3362{
3363 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, v);
3364}
3365
3366static void
3367iwi_led_done(void *arg)
3368{
3369 struct iwi_softc *sc = arg;
3370
3371 sc->sc_blinking = 0;
3372}
3373
3374/*
3375 * Turn the activity LED off: flip the pin and then set a timer so no
3376 * update will happen for the specified duration.
3377 */
3378static void
3379iwi_led_off(void *arg)
3380{
3381 struct iwi_softc *sc = arg;
3382 uint32_t v;
3383
3384 v = iwi_read_event(sc);
3385 v &= ~sc->sc_ledpin;
3386 iwi_write_event(sc, iwi_toggle_event(v));
3387 callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, iwi_led_done, sc);
3388}
3389
3390/*
3391 * Blink the LED according to the specified on/off times.
3392 */
3393static void
3394iwi_led_blink(struct iwi_softc *sc, int on, int off)
3395{
3396 uint32_t v;
3397
3398 v = iwi_read_event(sc);
3399 v |= sc->sc_ledpin;
3400 iwi_write_event(sc, iwi_toggle_event(v));
3401 sc->sc_blinking = 1;
3402 sc->sc_ledoff = off;
3403 callout_reset(&sc->sc_ledtimer, on, iwi_led_off, sc);
3404}
3405
3406static void
3407iwi_led_event(struct iwi_softc *sc, int event)
3408{
3409 /* NB: on/off times from the Atheros NDIS driver, w/ permission */
3410 static const struct {
3411 u_int rate; /* tx/rx iwi rate */
3412 u_int16_t timeOn; /* LED on time (ms) */
3413 u_int16_t timeOff; /* LED off time (ms) */
3414 } blinkrates[] = {
3415 { IWI_RATE_OFDM54, 40, 10 },
3416 { IWI_RATE_OFDM48, 44, 11 },
3417 { IWI_RATE_OFDM36, 50, 13 },
3418 { IWI_RATE_OFDM24, 57, 14 },
3419 { IWI_RATE_OFDM18, 67, 16 },
3420 { IWI_RATE_OFDM12, 80, 20 },
3421 { IWI_RATE_DS11, 100, 25 },
3422 { IWI_RATE_OFDM9, 133, 34 },
3423 { IWI_RATE_OFDM6, 160, 40 },
3424 { IWI_RATE_DS5, 200, 50 },
3425 { 6, 240, 58 }, /* XXX 3Mb/s if it existed */
3426 { IWI_RATE_DS2, 267, 66 },
3427 { IWI_RATE_DS1, 400, 100 },
3428 { 0, 500, 130 }, /* unknown rate/polling */
3429 };
3430 uint32_t txrate;
3431 int j = 0; /* XXX silence compiler */
3432
3433 sc->sc_ledevent = ticks; /* time of last event */
3434 if (sc->sc_blinking) /* don't interrupt active blink */
3435 return;
3436 switch (event) {
3437 case IWI_LED_POLL:
3438 j = nitems(blinkrates)-1;
3439 break;
3440 case IWI_LED_TX:
3441 /* read current transmission rate from adapter */
3442 txrate = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
3443 if (blinkrates[sc->sc_txrix].rate != txrate) {
3444 for (j = 0; j < nitems(blinkrates)-1; j++)
3445 if (blinkrates[j].rate == txrate)
3446 break;
3447 sc->sc_txrix = j;
3448 } else
3449 j = sc->sc_txrix;
3450 break;
3451 case IWI_LED_RX:
3452 if (blinkrates[sc->sc_rxrix].rate != sc->sc_rxrate) {
3453 for (j = 0; j < nitems(blinkrates)-1; j++)
3454 if (blinkrates[j].rate == sc->sc_rxrate)
3455 break;
3456 sc->sc_rxrix = j;
3457 } else
3458 j = sc->sc_rxrix;
3459 break;
3460 }
3461 /* XXX beware of overflow */
3462 iwi_led_blink(sc, (blinkrates[j].timeOn * hz) / 1000,
3463 (blinkrates[j].timeOff * hz) / 1000);
3464}
3465
3466static int
3467iwi_sysctl_softled(SYSCTL_HANDLER_ARGS)
3468{
3469 struct iwi_softc *sc = arg1;
3470 int softled = sc->sc_softled;
3471 int error;
3472
3473 error = sysctl_handle_int(oidp, &softled, 0, req);
3474 if (error || !req->newptr)
3475 return error;
3476 softled = (softled != 0);
3477 if (softled != sc->sc_softled) {
3478 if (softled) {
3479 uint32_t v = iwi_read_event(sc);
3480 v &= ~sc->sc_ledpin;
3481 iwi_write_event(sc, iwi_toggle_event(v));
3482 }
3483 sc->sc_softled = softled;
3484 }
3485 return 0;
3486}
3487
3488static void
3489iwi_ledattach(struct iwi_softc *sc)
3490{
3491 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3492 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3493
3494 sc->sc_blinking = 0;
3495 sc->sc_ledstate = 1;
3496 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */
3497 callout_init_mtx(&sc->sc_ledtimer, &sc->sc_mtx, 0);
3498
3499 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3500 "softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
3501 iwi_sysctl_softled, "I", "enable/disable software LED support");
3502 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3503 "ledpin", CTLFLAG_RW, &sc->sc_ledpin, 0,
3504 "pin setting to turn activity LED on");
3505 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3506 "ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0,
3507 "idle time for inactivity LED (ticks)");
3508 /* XXX for debugging */
3509 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3510 "nictype", CTLFLAG_RD, &sc->sc_nictype, 0,
3511 "NIC type from EEPROM");
3512
3513 sc->sc_ledpin = IWI_RST_LED_ACTIVITY;
3514 sc->sc_softled = 1;
3515
3516 sc->sc_nictype = (iwi_read_prom_word(sc, IWI_EEPROM_NIC) >> 8) & 0xff;
3517 if (sc->sc_nictype == 1) {
3518 /*
3519 * NB: led's are reversed.
3520 */
3521 sc->sc_ledpin = IWI_RST_LED_ASSOCIATED;
3522 }
3523}
3524
3525static void
3526iwi_scan_start(struct ieee80211com *ic)
3527{
3528 /* ignore */
3529}
3530
3531static void
3532iwi_set_channel(struct ieee80211com *ic)
3533{
3534 struct iwi_softc *sc = ic->ic_softc;
3535
3536 if (sc->fw_state == IWI_FW_IDLE)
3537 iwi_setcurchan(sc, ic->ic_curchan->ic_ieee);
3538}
3539
3540static void
3541iwi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
3542{
3543 struct ieee80211vap *vap = ss->ss_vap;
3544 struct iwi_softc *sc = vap->iv_ic->ic_softc;
3545 IWI_LOCK_DECL;
3546
3547 IWI_LOCK(sc);
3548 if (iwi_scanchan(sc, maxdwell, 0))
3549 ieee80211_cancel_scan(vap);
3550 IWI_UNLOCK(sc);
3551}
3552
3553static void
3554iwi_scan_mindwell(struct ieee80211_scan_state *ss)
3555{
3556 /* NB: don't try to abort scan; wait for firmware to finish */
3557}
3558
3559static void
3560iwi_scan_end(struct ieee80211com *ic)
3561{
3562 struct iwi_softc *sc = ic->ic_softc;
3563 IWI_LOCK_DECL;
3564
3565 IWI_LOCK(sc);
3566 sc->flags &= ~IWI_FLAG_CHANNEL_SCAN;
3567 /* NB: make sure we're still scanning */
3568 if (sc->fw_state == IWI_FW_SCANNING)
3569 iwi_cmd(sc, IWI_CMD_ABORT_SCAN, NULL, 0);
3570 IWI_UNLOCK(sc);
3571}
275 int i, error;
276
277 sc->sc_dev = dev;
278
279 IWI_LOCK_INIT(sc);
280 mbufq_init(&sc->sc_snd, ifqmaxlen);
281
282 sc->sc_unr = new_unrhdr(1, IWI_MAX_IBSSNODE-1, &sc->sc_mtx);
283
284 TASK_INIT(&sc->sc_radiontask, 0, iwi_radio_on, sc);
285 TASK_INIT(&sc->sc_radiofftask, 0, iwi_radio_off, sc);
286 TASK_INIT(&sc->sc_restarttask, 0, iwi_restart, sc);
287 TASK_INIT(&sc->sc_disassoctask, 0, iwi_disassoc, sc);
288 TASK_INIT(&sc->sc_monitortask, 0, iwi_monitor_scan, sc);
289
290 callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
291 callout_init_mtx(&sc->sc_rftimer, &sc->sc_mtx, 0);
292
293 pci_write_config(dev, 0x41, 0, 1);
294
295 /* enable bus-mastering */
296 pci_enable_busmaster(dev);
297
298 i = PCIR_BAR(0);
299 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE);
300 if (sc->mem == NULL) {
301 device_printf(dev, "could not allocate memory resource\n");
302 goto fail;
303 }
304
305 sc->sc_st = rman_get_bustag(sc->mem);
306 sc->sc_sh = rman_get_bushandle(sc->mem);
307
308 i = 0;
309 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
310 RF_ACTIVE | RF_SHAREABLE);
311 if (sc->irq == NULL) {
312 device_printf(dev, "could not allocate interrupt resource\n");
313 goto fail;
314 }
315
316 if (iwi_reset(sc) != 0) {
317 device_printf(dev, "could not reset adapter\n");
318 goto fail;
319 }
320
321 /*
322 * Allocate rings.
323 */
324 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
325 device_printf(dev, "could not allocate Cmd ring\n");
326 goto fail;
327 }
328
329 for (i = 0; i < 4; i++) {
330 error = iwi_alloc_tx_ring(sc, &sc->txq[i], IWI_TX_RING_COUNT,
331 IWI_CSR_TX1_RIDX + i * 4,
332 IWI_CSR_TX1_WIDX + i * 4);
333 if (error != 0) {
334 device_printf(dev, "could not allocate Tx ring %d\n",
335 i+i);
336 goto fail;
337 }
338 }
339
340 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
341 device_printf(dev, "could not allocate Rx ring\n");
342 goto fail;
343 }
344
345 iwi_wme_init(sc);
346
347 ic->ic_softc = sc;
348 ic->ic_name = device_get_nameunit(dev);
349 ic->ic_opmode = IEEE80211_M_STA;
350 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
351
352 /* set device capabilities */
353 ic->ic_caps =
354 IEEE80211_C_STA /* station mode supported */
355 | IEEE80211_C_IBSS /* IBSS mode supported */
356 | IEEE80211_C_MONITOR /* monitor mode supported */
357 | IEEE80211_C_PMGT /* power save supported */
358 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
359 | IEEE80211_C_WPA /* 802.11i */
360 | IEEE80211_C_WME /* 802.11e */
361#if 0
362 | IEEE80211_C_BGSCAN /* capable of bg scanning */
363#endif
364 ;
365
366 /* read MAC address from EEPROM */
367 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
368 ic->ic_macaddr[0] = val & 0xff;
369 ic->ic_macaddr[1] = val >> 8;
370 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
371 ic->ic_macaddr[2] = val & 0xff;
372 ic->ic_macaddr[3] = val >> 8;
373 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
374 ic->ic_macaddr[4] = val & 0xff;
375 ic->ic_macaddr[5] = val >> 8;
376
377 memset(bands, 0, sizeof(bands));
378 setbit(bands, IEEE80211_MODE_11B);
379 setbit(bands, IEEE80211_MODE_11G);
380 if (pci_get_device(dev) >= 0x4223)
381 setbit(bands, IEEE80211_MODE_11A);
382 ieee80211_init_channels(ic, NULL, bands);
383
384 ieee80211_ifattach(ic);
385 /* override default methods */
386 ic->ic_node_alloc = iwi_node_alloc;
387 sc->sc_node_free = ic->ic_node_free;
388 ic->ic_node_free = iwi_node_free;
389 ic->ic_raw_xmit = iwi_raw_xmit;
390 ic->ic_scan_start = iwi_scan_start;
391 ic->ic_scan_end = iwi_scan_end;
392 ic->ic_set_channel = iwi_set_channel;
393 ic->ic_scan_curchan = iwi_scan_curchan;
394 ic->ic_scan_mindwell = iwi_scan_mindwell;
395 ic->ic_wme.wme_update = iwi_wme_update;
396
397 ic->ic_vap_create = iwi_vap_create;
398 ic->ic_vap_delete = iwi_vap_delete;
399 ic->ic_ioctl = iwi_ioctl;
400 ic->ic_transmit = iwi_transmit;
401 ic->ic_parent = iwi_parent;
402
403 ieee80211_radiotap_attach(ic,
404 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
405 IWI_TX_RADIOTAP_PRESENT,
406 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
407 IWI_RX_RADIOTAP_PRESENT);
408
409 iwi_sysctlattach(sc);
410 iwi_ledattach(sc);
411
412 /*
413 * Hook our interrupt after all initialization is complete.
414 */
415 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
416 NULL, iwi_intr, sc, &sc->sc_ih);
417 if (error != 0) {
418 device_printf(dev, "could not set up interrupt\n");
419 goto fail;
420 }
421
422 if (bootverbose)
423 ieee80211_announce(ic);
424
425 return 0;
426fail:
427 /* XXX fix */
428 iwi_detach(dev);
429 return ENXIO;
430}
431
432static int
433iwi_detach(device_t dev)
434{
435 struct iwi_softc *sc = device_get_softc(dev);
436 struct ieee80211com *ic = &sc->sc_ic;
437
438 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
439
440 /* NB: do early to drain any pending tasks */
441 ieee80211_draintask(ic, &sc->sc_radiontask);
442 ieee80211_draintask(ic, &sc->sc_radiofftask);
443 ieee80211_draintask(ic, &sc->sc_restarttask);
444 ieee80211_draintask(ic, &sc->sc_disassoctask);
445 ieee80211_draintask(ic, &sc->sc_monitortask);
446
447 iwi_stop(sc);
448
449 ieee80211_ifdetach(ic);
450
451 iwi_put_firmware(sc);
452 iwi_release_fw_dma(sc);
453
454 iwi_free_cmd_ring(sc, &sc->cmdq);
455 iwi_free_tx_ring(sc, &sc->txq[0]);
456 iwi_free_tx_ring(sc, &sc->txq[1]);
457 iwi_free_tx_ring(sc, &sc->txq[2]);
458 iwi_free_tx_ring(sc, &sc->txq[3]);
459 iwi_free_rx_ring(sc, &sc->rxq);
460
461 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
462
463 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
464 sc->mem);
465
466 delete_unrhdr(sc->sc_unr);
467 mbufq_drain(&sc->sc_snd);
468
469 IWI_LOCK_DESTROY(sc);
470
471 return 0;
472}
473
474static struct ieee80211vap *
475iwi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
476 enum ieee80211_opmode opmode, int flags,
477 const uint8_t bssid[IEEE80211_ADDR_LEN],
478 const uint8_t mac[IEEE80211_ADDR_LEN])
479{
480 struct iwi_softc *sc = ic->ic_softc;
481 struct iwi_vap *ivp;
482 struct ieee80211vap *vap;
483 int i;
484
485 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
486 return NULL;
487 /*
488 * Get firmware image (and possibly dma memory) on mode change.
489 */
490 if (iwi_get_firmware(sc, opmode))
491 return NULL;
492 /* allocate DMA memory for mapping firmware image */
493 i = sc->fw_fw.size;
494 if (sc->fw_boot.size > i)
495 i = sc->fw_boot.size;
496 /* XXX do we dma the ucode as well ? */
497 if (sc->fw_uc.size > i)
498 i = sc->fw_uc.size;
499 if (iwi_init_fw_dma(sc, i))
500 return NULL;
501
502 ivp = malloc(sizeof(struct iwi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
503 vap = &ivp->iwi_vap;
504 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
505 /* override the default, the setting comes from the linux driver */
506 vap->iv_bmissthreshold = 24;
507 /* override with driver methods */
508 ivp->iwi_newstate = vap->iv_newstate;
509 vap->iv_newstate = iwi_newstate;
510
511 /* complete setup */
512 ieee80211_vap_attach(vap, ieee80211_media_change, iwi_media_status,
513 mac);
514 ic->ic_opmode = opmode;
515 return vap;
516}
517
518static void
519iwi_vap_delete(struct ieee80211vap *vap)
520{
521 struct iwi_vap *ivp = IWI_VAP(vap);
522
523 ieee80211_vap_detach(vap);
524 free(ivp, M_80211_VAP);
525}
526
527static void
528iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
529{
530 if (error != 0)
531 return;
532
533 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
534
535 *(bus_addr_t *)arg = segs[0].ds_addr;
536}
537
538static int
539iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count)
540{
541 int error;
542
543 ring->count = count;
544 ring->queued = 0;
545 ring->cur = ring->next = 0;
546
547 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
548 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
549 count * IWI_CMD_DESC_SIZE, 1, count * IWI_CMD_DESC_SIZE, 0,
550 NULL, NULL, &ring->desc_dmat);
551 if (error != 0) {
552 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
553 goto fail;
554 }
555
556 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
557 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
558 if (error != 0) {
559 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
560 goto fail;
561 }
562
563 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
564 count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
565 if (error != 0) {
566 device_printf(sc->sc_dev, "could not load desc DMA map\n");
567 goto fail;
568 }
569
570 return 0;
571
572fail: iwi_free_cmd_ring(sc, ring);
573 return error;
574}
575
576static void
577iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
578{
579 ring->queued = 0;
580 ring->cur = ring->next = 0;
581}
582
583static void
584iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
585{
586 if (ring->desc != NULL) {
587 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
588 BUS_DMASYNC_POSTWRITE);
589 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
590 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
591 }
592
593 if (ring->desc_dmat != NULL)
594 bus_dma_tag_destroy(ring->desc_dmat);
595}
596
597static int
598iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count,
599 bus_addr_t csr_ridx, bus_addr_t csr_widx)
600{
601 int i, error;
602
603 ring->count = count;
604 ring->queued = 0;
605 ring->cur = ring->next = 0;
606 ring->csr_ridx = csr_ridx;
607 ring->csr_widx = csr_widx;
608
609 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
610 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
611 count * IWI_TX_DESC_SIZE, 1, count * IWI_TX_DESC_SIZE, 0, NULL,
612 NULL, &ring->desc_dmat);
613 if (error != 0) {
614 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
615 goto fail;
616 }
617
618 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
619 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
620 if (error != 0) {
621 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
622 goto fail;
623 }
624
625 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
626 count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
627 if (error != 0) {
628 device_printf(sc->sc_dev, "could not load desc DMA map\n");
629 goto fail;
630 }
631
632 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
633 M_NOWAIT | M_ZERO);
634 if (ring->data == NULL) {
635 device_printf(sc->sc_dev, "could not allocate soft data\n");
636 error = ENOMEM;
637 goto fail;
638 }
639
640 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
641 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
642 IWI_MAX_NSEG, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
643 if (error != 0) {
644 device_printf(sc->sc_dev, "could not create data DMA tag\n");
645 goto fail;
646 }
647
648 for (i = 0; i < count; i++) {
649 error = bus_dmamap_create(ring->data_dmat, 0,
650 &ring->data[i].map);
651 if (error != 0) {
652 device_printf(sc->sc_dev, "could not create DMA map\n");
653 goto fail;
654 }
655 }
656
657 return 0;
658
659fail: iwi_free_tx_ring(sc, ring);
660 return error;
661}
662
663static void
664iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
665{
666 struct iwi_tx_data *data;
667 int i;
668
669 for (i = 0; i < ring->count; i++) {
670 data = &ring->data[i];
671
672 if (data->m != NULL) {
673 bus_dmamap_sync(ring->data_dmat, data->map,
674 BUS_DMASYNC_POSTWRITE);
675 bus_dmamap_unload(ring->data_dmat, data->map);
676 m_freem(data->m);
677 data->m = NULL;
678 }
679
680 if (data->ni != NULL) {
681 ieee80211_free_node(data->ni);
682 data->ni = NULL;
683 }
684 }
685
686 ring->queued = 0;
687 ring->cur = ring->next = 0;
688}
689
690static void
691iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
692{
693 struct iwi_tx_data *data;
694 int i;
695
696 if (ring->desc != NULL) {
697 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
698 BUS_DMASYNC_POSTWRITE);
699 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
700 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
701 }
702
703 if (ring->desc_dmat != NULL)
704 bus_dma_tag_destroy(ring->desc_dmat);
705
706 if (ring->data != NULL) {
707 for (i = 0; i < ring->count; i++) {
708 data = &ring->data[i];
709
710 if (data->m != NULL) {
711 bus_dmamap_sync(ring->data_dmat, data->map,
712 BUS_DMASYNC_POSTWRITE);
713 bus_dmamap_unload(ring->data_dmat, data->map);
714 m_freem(data->m);
715 }
716
717 if (data->ni != NULL)
718 ieee80211_free_node(data->ni);
719
720 if (data->map != NULL)
721 bus_dmamap_destroy(ring->data_dmat, data->map);
722 }
723
724 free(ring->data, M_DEVBUF);
725 }
726
727 if (ring->data_dmat != NULL)
728 bus_dma_tag_destroy(ring->data_dmat);
729}
730
731static int
732iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
733{
734 struct iwi_rx_data *data;
735 int i, error;
736
737 ring->count = count;
738 ring->cur = 0;
739
740 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
741 M_NOWAIT | M_ZERO);
742 if (ring->data == NULL) {
743 device_printf(sc->sc_dev, "could not allocate soft data\n");
744 error = ENOMEM;
745 goto fail;
746 }
747
748 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
749 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
750 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
751 if (error != 0) {
752 device_printf(sc->sc_dev, "could not create data DMA tag\n");
753 goto fail;
754 }
755
756 for (i = 0; i < count; i++) {
757 data = &ring->data[i];
758
759 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
760 if (error != 0) {
761 device_printf(sc->sc_dev, "could not create DMA map\n");
762 goto fail;
763 }
764
765 data->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
766 if (data->m == NULL) {
767 device_printf(sc->sc_dev,
768 "could not allocate rx mbuf\n");
769 error = ENOMEM;
770 goto fail;
771 }
772
773 error = bus_dmamap_load(ring->data_dmat, data->map,
774 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
775 &data->physaddr, 0);
776 if (error != 0) {
777 device_printf(sc->sc_dev,
778 "could not load rx buf DMA map");
779 goto fail;
780 }
781
782 data->reg = IWI_CSR_RX_BASE + i * 4;
783 }
784
785 return 0;
786
787fail: iwi_free_rx_ring(sc, ring);
788 return error;
789}
790
791static void
792iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
793{
794 ring->cur = 0;
795}
796
797static void
798iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
799{
800 struct iwi_rx_data *data;
801 int i;
802
803 if (ring->data != NULL) {
804 for (i = 0; i < ring->count; i++) {
805 data = &ring->data[i];
806
807 if (data->m != NULL) {
808 bus_dmamap_sync(ring->data_dmat, data->map,
809 BUS_DMASYNC_POSTREAD);
810 bus_dmamap_unload(ring->data_dmat, data->map);
811 m_freem(data->m);
812 }
813
814 if (data->map != NULL)
815 bus_dmamap_destroy(ring->data_dmat, data->map);
816 }
817
818 free(ring->data, M_DEVBUF);
819 }
820
821 if (ring->data_dmat != NULL)
822 bus_dma_tag_destroy(ring->data_dmat);
823}
824
825static int
826iwi_shutdown(device_t dev)
827{
828 struct iwi_softc *sc = device_get_softc(dev);
829
830 iwi_stop(sc);
831 iwi_put_firmware(sc); /* ??? XXX */
832
833 return 0;
834}
835
836static int
837iwi_suspend(device_t dev)
838{
839 struct iwi_softc *sc = device_get_softc(dev);
840 struct ieee80211com *ic = &sc->sc_ic;
841
842 ieee80211_suspend_all(ic);
843 return 0;
844}
845
846static int
847iwi_resume(device_t dev)
848{
849 struct iwi_softc *sc = device_get_softc(dev);
850 struct ieee80211com *ic = &sc->sc_ic;
851
852 pci_write_config(dev, 0x41, 0, 1);
853
854 ieee80211_resume_all(ic);
855 return 0;
856}
857
858static struct ieee80211_node *
859iwi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
860{
861 struct iwi_node *in;
862
863 in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
864 if (in == NULL)
865 return NULL;
866 /* XXX assign sta table entry for adhoc */
867 in->in_station = -1;
868
869 return &in->in_node;
870}
871
872static void
873iwi_node_free(struct ieee80211_node *ni)
874{
875 struct ieee80211com *ic = ni->ni_ic;
876 struct iwi_softc *sc = ic->ic_softc;
877 struct iwi_node *in = (struct iwi_node *)ni;
878
879 if (in->in_station != -1) {
880 DPRINTF(("%s mac %6D station %u\n", __func__,
881 ni->ni_macaddr, ":", in->in_station));
882 free_unr(sc->sc_unr, in->in_station);
883 }
884
885 sc->sc_node_free(ni);
886}
887
888/*
889 * Convert h/w rate code to IEEE rate code.
890 */
891static int
892iwi_cvtrate(int iwirate)
893{
894 switch (iwirate) {
895 case IWI_RATE_DS1: return 2;
896 case IWI_RATE_DS2: return 4;
897 case IWI_RATE_DS5: return 11;
898 case IWI_RATE_DS11: return 22;
899 case IWI_RATE_OFDM6: return 12;
900 case IWI_RATE_OFDM9: return 18;
901 case IWI_RATE_OFDM12: return 24;
902 case IWI_RATE_OFDM18: return 36;
903 case IWI_RATE_OFDM24: return 48;
904 case IWI_RATE_OFDM36: return 72;
905 case IWI_RATE_OFDM48: return 96;
906 case IWI_RATE_OFDM54: return 108;
907 }
908 return 0;
909}
910
911/*
912 * The firmware automatically adapts the transmit speed. We report its current
913 * value here.
914 */
915static void
916iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
917{
918 struct ieee80211vap *vap = ifp->if_softc;
919 struct ieee80211com *ic = vap->iv_ic;
920 struct iwi_softc *sc = ic->ic_softc;
921 struct ieee80211_node *ni;
922
923 /* read current transmission rate from adapter */
924 ni = ieee80211_ref_node(vap->iv_bss);
925 ni->ni_txrate =
926 iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE));
927 ieee80211_free_node(ni);
928 ieee80211_media_status(ifp, imr);
929}
930
931static int
932iwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
933{
934 struct iwi_vap *ivp = IWI_VAP(vap);
935 struct ieee80211com *ic = vap->iv_ic;
936 struct iwi_softc *sc = ic->ic_softc;
937 IWI_LOCK_DECL;
938
939 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
940 ieee80211_state_name[vap->iv_state],
941 ieee80211_state_name[nstate], sc->flags));
942
943 IEEE80211_UNLOCK(ic);
944 IWI_LOCK(sc);
945 switch (nstate) {
946 case IEEE80211_S_INIT:
947 /*
948 * NB: don't try to do this if iwi_stop_master has
949 * shutdown the firmware and disabled interrupts.
950 */
951 if (vap->iv_state == IEEE80211_S_RUN &&
952 (sc->flags & IWI_FLAG_FW_INITED))
953 iwi_disassociate(sc, 0);
954 break;
955 case IEEE80211_S_AUTH:
956 iwi_auth_and_assoc(sc, vap);
957 break;
958 case IEEE80211_S_RUN:
959 if (vap->iv_opmode == IEEE80211_M_IBSS &&
960 vap->iv_state == IEEE80211_S_SCAN) {
961 /*
962 * XXX when joining an ibss network we are called
963 * with a SCAN -> RUN transition on scan complete.
964 * Use that to call iwi_auth_and_assoc. On completing
965 * the join we are then called again with an
966 * AUTH -> RUN transition and we want to do nothing.
967 * This is all totally bogus and needs to be redone.
968 */
969 iwi_auth_and_assoc(sc, vap);
970 } else if (vap->iv_opmode == IEEE80211_M_MONITOR)
971 ieee80211_runtask(ic, &sc->sc_monitortask);
972 break;
973 case IEEE80211_S_ASSOC:
974 /*
975 * If we are transitioning from AUTH then just wait
976 * for the ASSOC status to come back from the firmware.
977 * Otherwise we need to issue the association request.
978 */
979 if (vap->iv_state == IEEE80211_S_AUTH)
980 break;
981 iwi_auth_and_assoc(sc, vap);
982 break;
983 default:
984 break;
985 }
986 IWI_UNLOCK(sc);
987 IEEE80211_LOCK(ic);
988 return ivp->iwi_newstate(vap, nstate, arg);
989}
990
991/*
992 * WME parameters coming from IEEE 802.11e specification. These values are
993 * already declared in ieee80211_proto.c, but they are static so they can't
994 * be reused here.
995 */
996static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
997 { 0, 3, 5, 7, 0 }, /* WME_AC_BE */
998 { 0, 3, 5, 10, 0 }, /* WME_AC_BK */
999 { 0, 2, 4, 5, 188 }, /* WME_AC_VI */
1000 { 0, 2, 3, 4, 102 } /* WME_AC_VO */
1001};
1002
1003static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
1004 { 0, 3, 4, 6, 0 }, /* WME_AC_BE */
1005 { 0, 3, 4, 10, 0 }, /* WME_AC_BK */
1006 { 0, 2, 3, 4, 94 }, /* WME_AC_VI */
1007 { 0, 2, 2, 3, 47 } /* WME_AC_VO */
1008};
1009#define IWI_EXP2(v) htole16((1 << (v)) - 1)
1010#define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
1011
1012static void
1013iwi_wme_init(struct iwi_softc *sc)
1014{
1015 const struct wmeParams *wmep;
1016 int ac;
1017
1018 memset(sc->wme, 0, sizeof sc->wme);
1019 for (ac = 0; ac < WME_NUM_AC; ac++) {
1020 /* set WME values for CCK modulation */
1021 wmep = &iwi_wme_cck_params[ac];
1022 sc->wme[1].aifsn[ac] = wmep->wmep_aifsn;
1023 sc->wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1024 sc->wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1025 sc->wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1026 sc->wme[1].acm[ac] = wmep->wmep_acm;
1027
1028 /* set WME values for OFDM modulation */
1029 wmep = &iwi_wme_ofdm_params[ac];
1030 sc->wme[2].aifsn[ac] = wmep->wmep_aifsn;
1031 sc->wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1032 sc->wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1033 sc->wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1034 sc->wme[2].acm[ac] = wmep->wmep_acm;
1035 }
1036}
1037
1038static int
1039iwi_wme_setparams(struct iwi_softc *sc)
1040{
1041 struct ieee80211com *ic = &sc->sc_ic;
1042 const struct wmeParams *wmep;
1043 int ac;
1044
1045 for (ac = 0; ac < WME_NUM_AC; ac++) {
1046 /* set WME values for current operating mode */
1047 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
1048 sc->wme[0].aifsn[ac] = wmep->wmep_aifsn;
1049 sc->wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1050 sc->wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1051 sc->wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1052 sc->wme[0].acm[ac] = wmep->wmep_acm;
1053 }
1054
1055 DPRINTF(("Setting WME parameters\n"));
1056 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, sc->wme, sizeof sc->wme);
1057}
1058#undef IWI_USEC
1059#undef IWI_EXP2
1060
1061static int
1062iwi_wme_update(struct ieee80211com *ic)
1063{
1064 struct iwi_softc *sc = ic->ic_softc;
1065 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1066 IWI_LOCK_DECL;
1067
1068 /*
1069 * We may be called to update the WME parameters in
1070 * the adapter at various places. If we're already
1071 * associated then initiate the request immediately;
1072 * otherwise we assume the params will get sent down
1073 * to the adapter as part of the work iwi_auth_and_assoc
1074 * does.
1075 */
1076 if (vap->iv_state == IEEE80211_S_RUN) {
1077 IWI_LOCK(sc);
1078 iwi_wme_setparams(sc);
1079 IWI_UNLOCK(sc);
1080 }
1081 return (0);
1082}
1083
1084static int
1085iwi_wme_setie(struct iwi_softc *sc)
1086{
1087 struct ieee80211_wme_info wme;
1088
1089 memset(&wme, 0, sizeof wme);
1090 wme.wme_id = IEEE80211_ELEMID_VENDOR;
1091 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
1092 wme.wme_oui[0] = 0x00;
1093 wme.wme_oui[1] = 0x50;
1094 wme.wme_oui[2] = 0xf2;
1095 wme.wme_type = WME_OUI_TYPE;
1096 wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
1097 wme.wme_version = WME_VERSION;
1098 wme.wme_info = 0;
1099
1100 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
1101 return iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme);
1102}
1103
1104/*
1105 * Read 16 bits at address 'addr' from the serial EEPROM.
1106 */
1107static uint16_t
1108iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1109{
1110 uint32_t tmp;
1111 uint16_t val;
1112 int n;
1113
1114 /* clock C once before the first command */
1115 IWI_EEPROM_CTL(sc, 0);
1116 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1117 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1118 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1119
1120 /* write start bit (1) */
1121 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1122 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1123
1124 /* write READ opcode (10) */
1125 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1126 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1127 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1128 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1129
1130 /* write address A7-A0 */
1131 for (n = 7; n >= 0; n--) {
1132 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1133 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1134 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1135 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1136 }
1137
1138 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1139
1140 /* read data Q15-Q0 */
1141 val = 0;
1142 for (n = 15; n >= 0; n--) {
1143 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1144 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1145 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1146 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1147 }
1148
1149 IWI_EEPROM_CTL(sc, 0);
1150
1151 /* clear Chip Select and clock C */
1152 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1153 IWI_EEPROM_CTL(sc, 0);
1154 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1155
1156 return val;
1157}
1158
1159static void
1160iwi_setcurchan(struct iwi_softc *sc, int chan)
1161{
1162 struct ieee80211com *ic = &sc->sc_ic;
1163
1164 sc->curchan = chan;
1165 ieee80211_radiotap_chan_change(ic);
1166}
1167
1168static void
1169iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1170 struct iwi_frame *frame)
1171{
1172 struct ieee80211com *ic = &sc->sc_ic;
1173 struct mbuf *mnew, *m;
1174 struct ieee80211_node *ni;
1175 int type, error, framelen;
1176 int8_t rssi, nf;
1177 IWI_LOCK_DECL;
1178
1179 framelen = le16toh(frame->len);
1180 if (framelen < IEEE80211_MIN_LEN || framelen > MCLBYTES) {
1181 /*
1182 * XXX >MCLBYTES is bogus as it means the h/w dma'd
1183 * out of bounds; need to figure out how to limit
1184 * frame size in the firmware
1185 */
1186 /* XXX stat */
1187 DPRINTFN(1,
1188 ("drop rx frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1189 le16toh(frame->len), frame->chan, frame->rssi,
1190 frame->rssi_dbm));
1191 return;
1192 }
1193
1194 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1195 le16toh(frame->len), frame->chan, frame->rssi, frame->rssi_dbm));
1196
1197 if (frame->chan != sc->curchan)
1198 iwi_setcurchan(sc, frame->chan);
1199
1200 /*
1201 * Try to allocate a new mbuf for this ring element and load it before
1202 * processing the current mbuf. If the ring element cannot be loaded,
1203 * drop the received packet and reuse the old mbuf. In the unlikely
1204 * case that the old mbuf can't be reloaded either, explicitly panic.
1205 */
1206 mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1207 if (mnew == NULL) {
1208 counter_u64_add(ic->ic_ierrors, 1);
1209 return;
1210 }
1211
1212 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1213
1214 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1215 mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr,
1216 0);
1217 if (error != 0) {
1218 m_freem(mnew);
1219
1220 /* try to reload the old mbuf */
1221 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1222 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
1223 &data->physaddr, 0);
1224 if (error != 0) {
1225 /* very unlikely that it will fail... */
1226 panic("%s: could not load old rx mbuf",
1227 device_get_name(sc->sc_dev));
1228 }
1229 counter_u64_add(ic->ic_ierrors, 1);
1230 return;
1231 }
1232
1233 /*
1234 * New mbuf successfully loaded, update Rx ring and continue
1235 * processing.
1236 */
1237 m = data->m;
1238 data->m = mnew;
1239 CSR_WRITE_4(sc, data->reg, data->physaddr);
1240
1241 /* finalize mbuf */
1242 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1243 sizeof (struct iwi_frame) + framelen;
1244
1245 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1246
1247 rssi = frame->rssi_dbm;
1248 nf = -95;
1249 if (ieee80211_radiotap_active(ic)) {
1250 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1251
1252 tap->wr_flags = 0;
1253 tap->wr_antsignal = rssi;
1254 tap->wr_antnoise = nf;
1255 tap->wr_rate = iwi_cvtrate(frame->rate);
1256 tap->wr_antenna = frame->antenna;
1257 }
1258 IWI_UNLOCK(sc);
1259
1260 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1261 if (ni != NULL) {
1262 type = ieee80211_input(ni, m, rssi, nf);
1263 ieee80211_free_node(ni);
1264 } else
1265 type = ieee80211_input_all(ic, m, rssi, nf);
1266
1267 IWI_LOCK(sc);
1268 if (sc->sc_softled) {
1269 /*
1270 * Blink for any data frame. Otherwise do a
1271 * heartbeat-style blink when idle. The latter
1272 * is mainly for station mode where we depend on
1273 * periodic beacon frames to trigger the poll event.
1274 */
1275 if (type == IEEE80211_FC0_TYPE_DATA) {
1276 sc->sc_rxrate = frame->rate;
1277 iwi_led_event(sc, IWI_LED_RX);
1278 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
1279 iwi_led_event(sc, IWI_LED_POLL);
1280 }
1281}
1282
1283/*
1284 * Check for an association response frame to see if QoS
1285 * has been negotiated. We parse just enough to figure
1286 * out if we're supposed to use QoS. The proper solution
1287 * is to pass the frame up so ieee80211_input can do the
1288 * work but that's made hard by how things currently are
1289 * done in the driver.
1290 */
1291static void
1292iwi_checkforqos(struct ieee80211vap *vap,
1293 const struct ieee80211_frame *wh, int len)
1294{
1295#define SUBTYPE(wh) ((wh)->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
1296 const uint8_t *frm, *efrm, *wme;
1297 struct ieee80211_node *ni;
1298 uint16_t capinfo, status, associd;
1299
1300 /* NB: +8 for capinfo, status, associd, and first ie */
1301 if (!(sizeof(*wh)+8 < len && len < IEEE80211_MAX_LEN) ||
1302 SUBTYPE(wh) != IEEE80211_FC0_SUBTYPE_ASSOC_RESP)
1303 return;
1304 /*
1305 * asresp frame format
1306 * [2] capability information
1307 * [2] status
1308 * [2] association ID
1309 * [tlv] supported rates
1310 * [tlv] extended supported rates
1311 * [tlv] WME
1312 */
1313 frm = (const uint8_t *)&wh[1];
1314 efrm = ((const uint8_t *) wh) + len;
1315
1316 capinfo = le16toh(*(const uint16_t *)frm);
1317 frm += 2;
1318 status = le16toh(*(const uint16_t *)frm);
1319 frm += 2;
1320 associd = le16toh(*(const uint16_t *)frm);
1321 frm += 2;
1322
1323 wme = NULL;
1324 while (efrm - frm > 1) {
1325 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1326 switch (*frm) {
1327 case IEEE80211_ELEMID_VENDOR:
1328 if (iswmeoui(frm))
1329 wme = frm;
1330 break;
1331 }
1332 frm += frm[1] + 2;
1333 }
1334
1335 ni = ieee80211_ref_node(vap->iv_bss);
1336 ni->ni_capinfo = capinfo;
1337 ni->ni_associd = associd & 0x3fff;
1338 if (wme != NULL)
1339 ni->ni_flags |= IEEE80211_NODE_QOS;
1340 else
1341 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1342 ieee80211_free_node(ni);
1343#undef SUBTYPE
1344}
1345
1346static void
1347iwi_notif_link_quality(struct iwi_softc *sc, struct iwi_notif *notif)
1348{
1349 struct iwi_notif_link_quality *lq;
1350 int len;
1351
1352 len = le16toh(notif->len);
1353
1354 DPRINTFN(5, ("Notification (%u) - len=%d, sizeof=%zu\n",
1355 notif->type,
1356 len,
1357 sizeof(struct iwi_notif_link_quality)
1358 ));
1359
1360 /* enforce length */
1361 if (len != sizeof(struct iwi_notif_link_quality)) {
1362 DPRINTFN(5, ("Notification: (%u) too short (%d)\n",
1363 notif->type,
1364 len));
1365 return;
1366 }
1367
1368 lq = (struct iwi_notif_link_quality *)(notif + 1);
1369 memcpy(&sc->sc_linkqual, lq, sizeof(sc->sc_linkqual));
1370 sc->sc_linkqual_valid = 1;
1371}
1372
1373/*
1374 * Task queue callbacks for iwi_notification_intr used to avoid LOR's.
1375 */
1376
1377static void
1378iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1379{
1380 struct ieee80211com *ic = &sc->sc_ic;
1381 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1382 struct iwi_notif_scan_channel *chan;
1383 struct iwi_notif_scan_complete *scan;
1384 struct iwi_notif_authentication *auth;
1385 struct iwi_notif_association *assoc;
1386 struct iwi_notif_beacon_state *beacon;
1387
1388 switch (notif->type) {
1389 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1390 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1391
1392 DPRINTFN(3, ("Scan of channel %u complete (%u)\n",
1393 ieee80211_ieee2mhz(chan->nchan, 0), chan->nchan));
1394
1395 /* Reset the timer, the scan is still going */
1396 sc->sc_state_timer = 3;
1397 break;
1398
1399 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1400 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1401
1402 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1403 scan->status));
1404
1405 IWI_STATE_END(sc, IWI_FW_SCANNING);
1406
1407 /*
1408 * Monitor mode works by doing a passive scan to set
1409 * the channel and enable rx. Because we don't want
1410 * to abort a scan lest the firmware crash we scan
1411 * for a short period of time and automatically restart
1412 * the scan when notified the sweep has completed.
1413 */
1414 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
1415 ieee80211_runtask(ic, &sc->sc_monitortask);
1416 break;
1417 }
1418
1419 if (scan->status == IWI_SCAN_COMPLETED) {
1420 /* NB: don't need to defer, net80211 does it for us */
1421 ieee80211_scan_next(vap);
1422 }
1423 break;
1424
1425 case IWI_NOTIF_TYPE_AUTHENTICATION:
1426 auth = (struct iwi_notif_authentication *)(notif + 1);
1427 switch (auth->state) {
1428 case IWI_AUTH_SUCCESS:
1429 DPRINTFN(2, ("Authentication succeeeded\n"));
1430 ieee80211_new_state(vap, IEEE80211_S_ASSOC, -1);
1431 break;
1432 case IWI_AUTH_FAIL:
1433 /*
1434 * These are delivered as an unsolicited deauth
1435 * (e.g. due to inactivity) or in response to an
1436 * associate request.
1437 */
1438 sc->flags &= ~IWI_FLAG_ASSOCIATED;
1439 if (vap->iv_state != IEEE80211_S_RUN) {
1440 DPRINTFN(2, ("Authentication failed\n"));
1441 vap->iv_stats.is_rx_auth_fail++;
1442 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1443 } else {
1444 DPRINTFN(2, ("Deauthenticated\n"));
1445 vap->iv_stats.is_rx_deauth++;
1446 }
1447 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1448 break;
1449 case IWI_AUTH_SENT_1:
1450 case IWI_AUTH_RECV_2:
1451 case IWI_AUTH_SEQ1_PASS:
1452 break;
1453 case IWI_AUTH_SEQ1_FAIL:
1454 DPRINTFN(2, ("Initial authentication handshake failed; "
1455 "you probably need shared key\n"));
1456 vap->iv_stats.is_rx_auth_fail++;
1457 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1458 /* XXX retry shared key when in auto */
1459 break;
1460 default:
1461 device_printf(sc->sc_dev,
1462 "unknown authentication state %u\n", auth->state);
1463 break;
1464 }
1465 break;
1466
1467 case IWI_NOTIF_TYPE_ASSOCIATION:
1468 assoc = (struct iwi_notif_association *)(notif + 1);
1469 switch (assoc->state) {
1470 case IWI_AUTH_SUCCESS:
1471 /* re-association, do nothing */
1472 break;
1473 case IWI_ASSOC_SUCCESS:
1474 DPRINTFN(2, ("Association succeeded\n"));
1475 sc->flags |= IWI_FLAG_ASSOCIATED;
1476 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1477 iwi_checkforqos(vap,
1478 (const struct ieee80211_frame *)(assoc+1),
1479 le16toh(notif->len) - sizeof(*assoc) - 1);
1480 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
1481 break;
1482 case IWI_ASSOC_INIT:
1483 sc->flags &= ~IWI_FLAG_ASSOCIATED;
1484 switch (sc->fw_state) {
1485 case IWI_FW_ASSOCIATING:
1486 DPRINTFN(2, ("Association failed\n"));
1487 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1488 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1489 break;
1490
1491 case IWI_FW_DISASSOCIATING:
1492 DPRINTFN(2, ("Dissassociated\n"));
1493 IWI_STATE_END(sc, IWI_FW_DISASSOCIATING);
1494 vap->iv_stats.is_rx_disassoc++;
1495 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1496 break;
1497 }
1498 break;
1499 default:
1500 device_printf(sc->sc_dev,
1501 "unknown association state %u\n", assoc->state);
1502 break;
1503 }
1504 break;
1505
1506 case IWI_NOTIF_TYPE_BEACON:
1507 /* XXX check struct length */
1508 beacon = (struct iwi_notif_beacon_state *)(notif + 1);
1509
1510 DPRINTFN(5, ("Beacon state (%u, %u)\n",
1511 beacon->state, le32toh(beacon->number)));
1512
1513 if (beacon->state == IWI_BEACON_MISS) {
1514 /*
1515 * The firmware notifies us of every beacon miss
1516 * so we need to track the count against the
1517 * configured threshold before notifying the
1518 * 802.11 layer.
1519 * XXX try to roam, drop assoc only on much higher count
1520 */
1521 if (le32toh(beacon->number) >= vap->iv_bmissthreshold) {
1522 DPRINTF(("Beacon miss: %u >= %u\n",
1523 le32toh(beacon->number),
1524 vap->iv_bmissthreshold));
1525 vap->iv_stats.is_beacon_miss++;
1526 /*
1527 * It's pointless to notify the 802.11 layer
1528 * as it'll try to send a probe request (which
1529 * we'll discard) and then timeout and drop us
1530 * into scan state. Instead tell the firmware
1531 * to disassociate and then on completion we'll
1532 * kick the state machine to scan.
1533 */
1534 ieee80211_runtask(ic, &sc->sc_disassoctask);
1535 }
1536 }
1537 break;
1538
1539 case IWI_NOTIF_TYPE_CALIBRATION:
1540 case IWI_NOTIF_TYPE_NOISE:
1541 /* XXX handle? */
1542 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1543 break;
1544 case IWI_NOTIF_TYPE_LINK_QUALITY:
1545 iwi_notif_link_quality(sc, notif);
1546 break;
1547
1548 default:
1549 DPRINTF(("unknown notification type %u flags 0x%x len %u\n",
1550 notif->type, notif->flags, le16toh(notif->len)));
1551 break;
1552 }
1553}
1554
1555static void
1556iwi_rx_intr(struct iwi_softc *sc)
1557{
1558 struct iwi_rx_data *data;
1559 struct iwi_hdr *hdr;
1560 uint32_t hw;
1561
1562 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1563
1564 for (; sc->rxq.cur != hw;) {
1565 data = &sc->rxq.data[sc->rxq.cur];
1566
1567 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1568 BUS_DMASYNC_POSTREAD);
1569
1570 hdr = mtod(data->m, struct iwi_hdr *);
1571
1572 switch (hdr->type) {
1573 case IWI_HDR_TYPE_FRAME:
1574 iwi_frame_intr(sc, data, sc->rxq.cur,
1575 (struct iwi_frame *)(hdr + 1));
1576 break;
1577
1578 case IWI_HDR_TYPE_NOTIF:
1579 iwi_notification_intr(sc,
1580 (struct iwi_notif *)(hdr + 1));
1581 break;
1582
1583 default:
1584 device_printf(sc->sc_dev, "unknown hdr type %u\n",
1585 hdr->type);
1586 }
1587
1588 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1589
1590 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1591 }
1592
1593 /* tell the firmware what we have processed */
1594 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1595 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1596}
1597
1598static void
1599iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1600{
1601 struct iwi_tx_data *data;
1602 uint32_t hw;
1603
1604 hw = CSR_READ_4(sc, txq->csr_ridx);
1605
1606 while (txq->next != hw) {
1607 data = &txq->data[txq->next];
1608 DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1609 bus_dmamap_sync(txq->data_dmat, data->map,
1610 BUS_DMASYNC_POSTWRITE);
1611 bus_dmamap_unload(txq->data_dmat, data->map);
1612 ieee80211_tx_complete(data->ni, data->m, 0);
1613 data->ni = NULL;
1614 data->m = NULL;
1615 txq->queued--;
1616 txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1617 }
1618 sc->sc_tx_timer = 0;
1619 if (sc->sc_softled)
1620 iwi_led_event(sc, IWI_LED_TX);
1621 iwi_start(sc);
1622}
1623
1624static void
1625iwi_fatal_error_intr(struct iwi_softc *sc)
1626{
1627 struct ieee80211com *ic = &sc->sc_ic;
1628 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1629
1630 device_printf(sc->sc_dev, "firmware error\n");
1631 if (vap != NULL)
1632 ieee80211_cancel_scan(vap);
1633 ieee80211_runtask(ic, &sc->sc_restarttask);
1634
1635 sc->flags &= ~IWI_FLAG_BUSY;
1636 sc->sc_busy_timer = 0;
1637 wakeup(sc);
1638}
1639
1640static void
1641iwi_radio_off_intr(struct iwi_softc *sc)
1642{
1643
1644 ieee80211_runtask(&sc->sc_ic, &sc->sc_radiofftask);
1645}
1646
1647static void
1648iwi_intr(void *arg)
1649{
1650 struct iwi_softc *sc = arg;
1651 uint32_t r;
1652 IWI_LOCK_DECL;
1653
1654 IWI_LOCK(sc);
1655
1656 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) {
1657 IWI_UNLOCK(sc);
1658 return;
1659 }
1660
1661 /* acknowledge interrupts */
1662 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1663
1664 if (r & IWI_INTR_FATAL_ERROR) {
1665 iwi_fatal_error_intr(sc);
1666 goto done;
1667 }
1668
1669 if (r & IWI_INTR_FW_INITED) {
1670 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1671 wakeup(sc);
1672 }
1673
1674 if (r & IWI_INTR_RADIO_OFF)
1675 iwi_radio_off_intr(sc);
1676
1677 if (r & IWI_INTR_CMD_DONE) {
1678 sc->flags &= ~IWI_FLAG_BUSY;
1679 sc->sc_busy_timer = 0;
1680 wakeup(sc);
1681 }
1682
1683 if (r & IWI_INTR_TX1_DONE)
1684 iwi_tx_intr(sc, &sc->txq[0]);
1685
1686 if (r & IWI_INTR_TX2_DONE)
1687 iwi_tx_intr(sc, &sc->txq[1]);
1688
1689 if (r & IWI_INTR_TX3_DONE)
1690 iwi_tx_intr(sc, &sc->txq[2]);
1691
1692 if (r & IWI_INTR_TX4_DONE)
1693 iwi_tx_intr(sc, &sc->txq[3]);
1694
1695 if (r & IWI_INTR_RX_DONE)
1696 iwi_rx_intr(sc);
1697
1698 if (r & IWI_INTR_PARITY_ERROR) {
1699 /* XXX rate-limit */
1700 device_printf(sc->sc_dev, "parity error\n");
1701 }
1702done:
1703 IWI_UNLOCK(sc);
1704}
1705
1706static int
1707iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len)
1708{
1709 struct iwi_cmd_desc *desc;
1710
1711 IWI_LOCK_ASSERT(sc);
1712
1713 if (sc->flags & IWI_FLAG_BUSY) {
1714 device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n",
1715 __func__, type);
1716 return EAGAIN;
1717 }
1718 sc->flags |= IWI_FLAG_BUSY;
1719 sc->sc_busy_timer = 2;
1720
1721 desc = &sc->cmdq.desc[sc->cmdq.cur];
1722
1723 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1724 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1725 desc->type = type;
1726 desc->len = len;
1727 memcpy(desc->data, data, len);
1728
1729 bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map,
1730 BUS_DMASYNC_PREWRITE);
1731
1732 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1733 type, len));
1734
1735 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1736 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1737
1738 return msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz);
1739}
1740
1741static void
1742iwi_write_ibssnode(struct iwi_softc *sc,
1743 const u_int8_t addr[IEEE80211_ADDR_LEN], int entry)
1744{
1745 struct iwi_ibssnode node;
1746
1747 /* write node information into NIC memory */
1748 memset(&node, 0, sizeof node);
1749 IEEE80211_ADDR_COPY(node.bssid, addr);
1750
1751 DPRINTF(("%s mac %6D station %u\n", __func__, node.bssid, ":", entry));
1752
1753 CSR_WRITE_REGION_1(sc,
1754 IWI_CSR_NODE_BASE + entry * sizeof node,
1755 (uint8_t *)&node, sizeof node);
1756}
1757
1758static int
1759iwi_tx_start(struct iwi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1760 int ac)
1761{
1762 struct ieee80211vap *vap = ni->ni_vap;
1763 struct ieee80211com *ic = ni->ni_ic;
1764 struct iwi_node *in = (struct iwi_node *)ni;
1765 const struct ieee80211_frame *wh;
1766 struct ieee80211_key *k;
1767 const struct chanAccParams *cap;
1768 struct iwi_tx_ring *txq = &sc->txq[ac];
1769 struct iwi_tx_data *data;
1770 struct iwi_tx_desc *desc;
1771 struct mbuf *mnew;
1772 bus_dma_segment_t segs[IWI_MAX_NSEG];
1773 int error, nsegs, hdrlen, i;
1774 int ismcast, flags, xflags, staid;
1775
1776 IWI_LOCK_ASSERT(sc);
1777 wh = mtod(m0, const struct ieee80211_frame *);
1778 /* NB: only data frames use this path */
1779 hdrlen = ieee80211_hdrsize(wh);
1780 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1781 flags = xflags = 0;
1782
1783 if (!ismcast)
1784 flags |= IWI_DATA_FLAG_NEED_ACK;
1785 if (vap->iv_flags & IEEE80211_F_SHPREAMBLE)
1786 flags |= IWI_DATA_FLAG_SHPREAMBLE;
1787 if (IEEE80211_QOS_HAS_SEQ(wh)) {
1788 xflags |= IWI_DATA_XFLAG_QOS;
1789 cap = &ic->ic_wme.wme_chanParams;
1790 if (!cap->cap_wmeParams[ac].wmep_noackPolicy)
1791 flags &= ~IWI_DATA_FLAG_NEED_ACK;
1792 }
1793
1794 /*
1795 * This is only used in IBSS mode where the firmware expect an index
1796 * in a h/w table instead of a destination address.
1797 */
1798 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1799 if (!ismcast) {
1800 if (in->in_station == -1) {
1801 in->in_station = alloc_unr(sc->sc_unr);
1802 if (in->in_station == -1) {
1803 /* h/w table is full */
1804 if_inc_counter(ni->ni_vap->iv_ifp,
1805 IFCOUNTER_OERRORS, 1);
1806 m_freem(m0);
1807 ieee80211_free_node(ni);
1808 return 0;
1809 }
1810 iwi_write_ibssnode(sc,
1811 ni->ni_macaddr, in->in_station);
1812 }
1813 staid = in->in_station;
1814 } else {
1815 /*
1816 * Multicast addresses have no associated node
1817 * so there will be no station entry. We reserve
1818 * entry 0 for one mcast address and use that.
1819 * If there are many being used this will be
1820 * expensive and we'll need to do a better job
1821 * but for now this handles the broadcast case.
1822 */
1823 if (!IEEE80211_ADDR_EQ(wh->i_addr1, sc->sc_mcast)) {
1824 IEEE80211_ADDR_COPY(sc->sc_mcast, wh->i_addr1);
1825 iwi_write_ibssnode(sc, sc->sc_mcast, 0);
1826 }
1827 staid = 0;
1828 }
1829 } else
1830 staid = 0;
1831
1832 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1833 k = ieee80211_crypto_encap(ni, m0);
1834 if (k == NULL) {
1835 m_freem(m0);
1836 return ENOBUFS;
1837 }
1838
1839 /* packet header may have moved, reset our local pointer */
1840 wh = mtod(m0, struct ieee80211_frame *);
1841 }
1842
1843 if (ieee80211_radiotap_active_vap(vap)) {
1844 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1845
1846 tap->wt_flags = 0;
1847
1848 ieee80211_radiotap_tx(vap, m0);
1849 }
1850
1851 data = &txq->data[txq->cur];
1852 desc = &txq->desc[txq->cur];
1853
1854 /* save and trim IEEE802.11 header */
1855 m_copydata(m0, 0, hdrlen, (caddr_t)&desc->wh);
1856 m_adj(m0, hdrlen);
1857
1858 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0, segs,
1859 &nsegs, 0);
1860 if (error != 0 && error != EFBIG) {
1861 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1862 error);
1863 m_freem(m0);
1864 return error;
1865 }
1866 if (error != 0) {
1867 mnew = m_defrag(m0, M_NOWAIT);
1868 if (mnew == NULL) {
1869 device_printf(sc->sc_dev,
1870 "could not defragment mbuf\n");
1871 m_freem(m0);
1872 return ENOBUFS;
1873 }
1874 m0 = mnew;
1875
1876 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map,
1877 m0, segs, &nsegs, 0);
1878 if (error != 0) {
1879 device_printf(sc->sc_dev,
1880 "could not map mbuf (error %d)\n", error);
1881 m_freem(m0);
1882 return error;
1883 }
1884 }
1885
1886 data->m = m0;
1887 data->ni = ni;
1888
1889 desc->hdr.type = IWI_HDR_TYPE_DATA;
1890 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1891 desc->station = staid;
1892 desc->cmd = IWI_DATA_CMD_TX;
1893 desc->len = htole16(m0->m_pkthdr.len);
1894 desc->flags = flags;
1895 desc->xflags = xflags;
1896
1897#if 0
1898 if (vap->iv_flags & IEEE80211_F_PRIVACY)
1899 desc->wep_txkey = vap->iv_def_txkey;
1900 else
1901#endif
1902 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1903
1904 desc->nseg = htole32(nsegs);
1905 for (i = 0; i < nsegs; i++) {
1906 desc->seg_addr[i] = htole32(segs[i].ds_addr);
1907 desc->seg_len[i] = htole16(segs[i].ds_len);
1908 }
1909
1910 bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1911 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1912
1913 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
1914 ac, txq->cur, le16toh(desc->len), nsegs));
1915
1916 txq->queued++;
1917 txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
1918 CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1919
1920 return 0;
1921}
1922
1923static int
1924iwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1925 const struct ieee80211_bpf_params *params)
1926{
1927 /* no support; just discard */
1928 m_freem(m);
1929 ieee80211_free_node(ni);
1930 return 0;
1931}
1932
1933static int
1934iwi_transmit(struct ieee80211com *ic, struct mbuf *m)
1935{
1936 struct iwi_softc *sc = ic->ic_softc;
1937 int error;
1938 IWI_LOCK_DECL;
1939
1940 IWI_LOCK(sc);
1941 if (!sc->sc_running) {
1942 IWI_UNLOCK(sc);
1943 return (ENXIO);
1944 }
1945 error = mbufq_enqueue(&sc->sc_snd, m);
1946 if (error) {
1947 IWI_UNLOCK(sc);
1948 return (error);
1949 }
1950 iwi_start(sc);
1951 IWI_UNLOCK(sc);
1952 return (0);
1953}
1954
1955static void
1956iwi_start(struct iwi_softc *sc)
1957{
1958 struct mbuf *m;
1959 struct ieee80211_node *ni;
1960 int ac;
1961
1962 IWI_LOCK_ASSERT(sc);
1963
1964 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1965 ac = M_WME_GETAC(m);
1966 if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) {
1967 /* there is no place left in this ring; tail drop */
1968 /* XXX tail drop */
1969 mbufq_prepend(&sc->sc_snd, m);
1970 break;
1971 }
1972 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1973 if (iwi_tx_start(sc, m, ni, ac) != 0) {
1974 ieee80211_free_node(ni);
1975 if_inc_counter(ni->ni_vap->iv_ifp,
1976 IFCOUNTER_OERRORS, 1);
1977 break;
1978 }
1979 sc->sc_tx_timer = 5;
1980 }
1981}
1982
1983static void
1984iwi_watchdog(void *arg)
1985{
1986 struct iwi_softc *sc = arg;
1987 struct ieee80211com *ic = &sc->sc_ic;
1988
1989 IWI_LOCK_ASSERT(sc);
1990
1991 if (sc->sc_tx_timer > 0) {
1992 if (--sc->sc_tx_timer == 0) {
1993 device_printf(sc->sc_dev, "device timeout\n");
1994 counter_u64_add(ic->ic_oerrors, 1);
1995 ieee80211_runtask(ic, &sc->sc_restarttask);
1996 }
1997 }
1998 if (sc->sc_state_timer > 0) {
1999 if (--sc->sc_state_timer == 0) {
2000 device_printf(sc->sc_dev,
2001 "firmware stuck in state %d, resetting\n",
2002 sc->fw_state);
2003 if (sc->fw_state == IWI_FW_SCANNING)
2004 ieee80211_cancel_scan(TAILQ_FIRST(&ic->ic_vaps));
2005 ieee80211_runtask(ic, &sc->sc_restarttask);
2006 sc->sc_state_timer = 3;
2007 }
2008 }
2009 if (sc->sc_busy_timer > 0) {
2010 if (--sc->sc_busy_timer == 0) {
2011 device_printf(sc->sc_dev,
2012 "firmware command timeout, resetting\n");
2013 ieee80211_runtask(ic, &sc->sc_restarttask);
2014 }
2015 }
2016 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
2017}
2018
2019static void
2020iwi_parent(struct ieee80211com *ic)
2021{
2022 struct iwi_softc *sc = ic->ic_softc;
2023 int startall = 0;
2024 IWI_LOCK_DECL;
2025
2026 IWI_LOCK(sc);
2027 if (ic->ic_nrunning > 0) {
2028 if (!sc->sc_running) {
2029 iwi_init_locked(sc);
2030 startall = 1;
2031 }
2032 } else if (sc->sc_running)
2033 iwi_stop_locked(sc);
2034 IWI_UNLOCK(sc);
2035 if (startall)
2036 ieee80211_start_all(ic);
2037}
2038
2039static int
2040iwi_ioctl(struct ieee80211com *ic, u_long cmd, void *data)
2041{
2042 struct ifreq *ifr = data;
2043 struct iwi_softc *sc = ic->ic_softc;
2044 int error;
2045 IWI_LOCK_DECL;
2046
2047 IWI_LOCK(sc);
2048 switch (cmd) {
2049 case SIOCGIWISTATS:
2050 /* XXX validate permissions/memory/etc? */
2051 error = copyout(&sc->sc_linkqual, ifr->ifr_data,
2052 sizeof(struct iwi_notif_link_quality));
2053 break;
2054 case SIOCZIWISTATS:
2055 memset(&sc->sc_linkqual, 0,
2056 sizeof(struct iwi_notif_link_quality));
2057 error = 0;
2058 break;
2059 default:
2060 error = ENOTTY;
2061 break;
2062 }
2063 IWI_UNLOCK(sc);
2064
2065 return (error);
2066}
2067
2068static void
2069iwi_stop_master(struct iwi_softc *sc)
2070{
2071 uint32_t tmp;
2072 int ntries;
2073
2074 /* disable interrupts */
2075 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
2076
2077 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
2078 for (ntries = 0; ntries < 5; ntries++) {
2079 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2080 break;
2081 DELAY(10);
2082 }
2083 if (ntries == 5)
2084 device_printf(sc->sc_dev, "timeout waiting for master\n");
2085
2086 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2087 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
2088
2089 sc->flags &= ~IWI_FLAG_FW_INITED;
2090}
2091
2092static int
2093iwi_reset(struct iwi_softc *sc)
2094{
2095 uint32_t tmp;
2096 int i, ntries;
2097
2098 iwi_stop_master(sc);
2099
2100 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2101 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2102
2103 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
2104
2105 /* wait for clock stabilization */
2106 for (ntries = 0; ntries < 1000; ntries++) {
2107 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
2108 break;
2109 DELAY(200);
2110 }
2111 if (ntries == 1000) {
2112 device_printf(sc->sc_dev,
2113 "timeout waiting for clock stabilization\n");
2114 return EIO;
2115 }
2116
2117 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2118 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET);
2119
2120 DELAY(10);
2121
2122 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2123 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2124
2125 /* clear NIC memory */
2126 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
2127 for (i = 0; i < 0xc000; i++)
2128 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2129
2130 return 0;
2131}
2132
2133static const struct iwi_firmware_ohdr *
2134iwi_setup_ofw(struct iwi_softc *sc, struct iwi_fw *fw)
2135{
2136 const struct firmware *fp = fw->fp;
2137 const struct iwi_firmware_ohdr *hdr;
2138
2139 if (fp->datasize < sizeof (struct iwi_firmware_ohdr)) {
2140 device_printf(sc->sc_dev, "image '%s' too small\n", fp->name);
2141 return NULL;
2142 }
2143 hdr = (const struct iwi_firmware_ohdr *)fp->data;
2144 if ((IWI_FW_GET_MAJOR(le32toh(hdr->version)) != IWI_FW_REQ_MAJOR) ||
2145 (IWI_FW_GET_MINOR(le32toh(hdr->version)) != IWI_FW_REQ_MINOR)) {
2146 device_printf(sc->sc_dev, "version for '%s' %d.%d != %d.%d\n",
2147 fp->name, IWI_FW_GET_MAJOR(le32toh(hdr->version)),
2148 IWI_FW_GET_MINOR(le32toh(hdr->version)), IWI_FW_REQ_MAJOR,
2149 IWI_FW_REQ_MINOR);
2150 return NULL;
2151 }
2152 fw->data = ((const char *) fp->data) + sizeof(struct iwi_firmware_ohdr);
2153 fw->size = fp->datasize - sizeof(struct iwi_firmware_ohdr);
2154 fw->name = fp->name;
2155 return hdr;
2156}
2157
2158static const struct iwi_firmware_ohdr *
2159iwi_setup_oucode(struct iwi_softc *sc, struct iwi_fw *fw)
2160{
2161 const struct iwi_firmware_ohdr *hdr;
2162
2163 hdr = iwi_setup_ofw(sc, fw);
2164 if (hdr != NULL && le32toh(hdr->mode) != IWI_FW_MODE_UCODE) {
2165 device_printf(sc->sc_dev, "%s is not a ucode image\n",
2166 fw->name);
2167 hdr = NULL;
2168 }
2169 return hdr;
2170}
2171
2172static void
2173iwi_getfw(struct iwi_fw *fw, const char *fwname,
2174 struct iwi_fw *uc, const char *ucname)
2175{
2176 if (fw->fp == NULL)
2177 fw->fp = firmware_get(fwname);
2178 /* NB: pre-3.0 ucode is packaged separately */
2179 if (uc->fp == NULL && fw->fp != NULL && fw->fp->version < 300)
2180 uc->fp = firmware_get(ucname);
2181}
2182
2183/*
2184 * Get the required firmware images if not already loaded.
2185 * Note that we hold firmware images so long as the device
2186 * is marked up in case we need to reload them on device init.
2187 * This is necessary because we re-init the device sometimes
2188 * from a context where we cannot read from the filesystem
2189 * (e.g. from the taskqueue thread when rfkill is re-enabled).
2190 * XXX return 0 on success, 1 on error.
2191 *
2192 * NB: the order of get'ing and put'ing images here is
2193 * intentional to support handling firmware images bundled
2194 * by operating mode and/or all together in one file with
2195 * the boot firmware as "master".
2196 */
2197static int
2198iwi_get_firmware(struct iwi_softc *sc, enum ieee80211_opmode opmode)
2199{
2200 const struct iwi_firmware_hdr *hdr;
2201 const struct firmware *fp;
2202
2203 /* invalidate cached firmware on mode change */
2204 if (sc->fw_mode != opmode)
2205 iwi_put_firmware(sc);
2206
2207 switch (opmode) {
2208 case IEEE80211_M_STA:
2209 iwi_getfw(&sc->fw_fw, "iwi_bss", &sc->fw_uc, "iwi_ucode_bss");
2210 break;
2211 case IEEE80211_M_IBSS:
2212 iwi_getfw(&sc->fw_fw, "iwi_ibss", &sc->fw_uc, "iwi_ucode_ibss");
2213 break;
2214 case IEEE80211_M_MONITOR:
2215 iwi_getfw(&sc->fw_fw, "iwi_monitor",
2216 &sc->fw_uc, "iwi_ucode_monitor");
2217 break;
2218 default:
2219 device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
2220 return EINVAL;
2221 }
2222 fp = sc->fw_fw.fp;
2223 if (fp == NULL) {
2224 device_printf(sc->sc_dev, "could not load firmware\n");
2225 goto bad;
2226 }
2227 if (fp->version < 300) {
2228 /*
2229 * Firmware prior to 3.0 was packaged as separate
2230 * boot, firmware, and ucode images. Verify the
2231 * ucode image was read in, retrieve the boot image
2232 * if needed, and check version stamps for consistency.
2233 * The version stamps in the data are also checked
2234 * above; this is a bit paranoid but is a cheap
2235 * safeguard against mis-packaging.
2236 */
2237 if (sc->fw_uc.fp == NULL) {
2238 device_printf(sc->sc_dev, "could not load ucode\n");
2239 goto bad;
2240 }
2241 if (sc->fw_boot.fp == NULL) {
2242 sc->fw_boot.fp = firmware_get("iwi_boot");
2243 if (sc->fw_boot.fp == NULL) {
2244 device_printf(sc->sc_dev,
2245 "could not load boot firmware\n");
2246 goto bad;
2247 }
2248 }
2249 if (sc->fw_boot.fp->version != sc->fw_fw.fp->version ||
2250 sc->fw_boot.fp->version != sc->fw_uc.fp->version) {
2251 device_printf(sc->sc_dev,
2252 "firmware version mismatch: "
2253 "'%s' is %d, '%s' is %d, '%s' is %d\n",
2254 sc->fw_boot.fp->name, sc->fw_boot.fp->version,
2255 sc->fw_uc.fp->name, sc->fw_uc.fp->version,
2256 sc->fw_fw.fp->name, sc->fw_fw.fp->version
2257 );
2258 goto bad;
2259 }
2260 /*
2261 * Check and setup each image.
2262 */
2263 if (iwi_setup_oucode(sc, &sc->fw_uc) == NULL ||
2264 iwi_setup_ofw(sc, &sc->fw_boot) == NULL ||
2265 iwi_setup_ofw(sc, &sc->fw_fw) == NULL)
2266 goto bad;
2267 } else {
2268 /*
2269 * Check and setup combined image.
2270 */
2271 if (fp->datasize < sizeof(struct iwi_firmware_hdr)) {
2272 device_printf(sc->sc_dev, "image '%s' too small\n",
2273 fp->name);
2274 goto bad;
2275 }
2276 hdr = (const struct iwi_firmware_hdr *)fp->data;
2277 if (fp->datasize < sizeof(*hdr) + le32toh(hdr->bsize) + le32toh(hdr->usize)
2278 + le32toh(hdr->fsize)) {
2279 device_printf(sc->sc_dev, "image '%s' too small (2)\n",
2280 fp->name);
2281 goto bad;
2282 }
2283 sc->fw_boot.data = ((const char *) fp->data) + sizeof(*hdr);
2284 sc->fw_boot.size = le32toh(hdr->bsize);
2285 sc->fw_boot.name = fp->name;
2286 sc->fw_uc.data = sc->fw_boot.data + sc->fw_boot.size;
2287 sc->fw_uc.size = le32toh(hdr->usize);
2288 sc->fw_uc.name = fp->name;
2289 sc->fw_fw.data = sc->fw_uc.data + sc->fw_uc.size;
2290 sc->fw_fw.size = le32toh(hdr->fsize);
2291 sc->fw_fw.name = fp->name;
2292 }
2293#if 0
2294 device_printf(sc->sc_dev, "boot %d ucode %d fw %d bytes\n",
2295 sc->fw_boot.size, sc->fw_uc.size, sc->fw_fw.size);
2296#endif
2297
2298 sc->fw_mode = opmode;
2299 return 0;
2300bad:
2301 iwi_put_firmware(sc);
2302 return 1;
2303}
2304
2305static void
2306iwi_put_fw(struct iwi_fw *fw)
2307{
2308 if (fw->fp != NULL) {
2309 firmware_put(fw->fp, FIRMWARE_UNLOAD);
2310 fw->fp = NULL;
2311 }
2312 fw->data = NULL;
2313 fw->size = 0;
2314 fw->name = NULL;
2315}
2316
2317/*
2318 * Release any cached firmware images.
2319 */
2320static void
2321iwi_put_firmware(struct iwi_softc *sc)
2322{
2323 iwi_put_fw(&sc->fw_uc);
2324 iwi_put_fw(&sc->fw_fw);
2325 iwi_put_fw(&sc->fw_boot);
2326}
2327
2328static int
2329iwi_load_ucode(struct iwi_softc *sc, const struct iwi_fw *fw)
2330{
2331 uint32_t tmp;
2332 const uint16_t *w;
2333 const char *uc = fw->data;
2334 size_t size = fw->size;
2335 int i, ntries, error;
2336
2337 IWI_LOCK_ASSERT(sc);
2338 error = 0;
2339 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
2340 IWI_RST_STOP_MASTER);
2341 for (ntries = 0; ntries < 5; ntries++) {
2342 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2343 break;
2344 DELAY(10);
2345 }
2346 if (ntries == 5) {
2347 device_printf(sc->sc_dev, "timeout waiting for master\n");
2348 error = EIO;
2349 goto fail;
2350 }
2351
2352 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2353 DELAY(5000);
2354
2355 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2356 tmp &= ~IWI_RST_PRINCETON_RESET;
2357 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2358
2359 DELAY(5000);
2360 MEM_WRITE_4(sc, 0x3000e0, 0);
2361 DELAY(1000);
2362 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 1);
2363 DELAY(1000);
2364 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 0);
2365 DELAY(1000);
2366 MEM_WRITE_1(sc, 0x200000, 0x00);
2367 MEM_WRITE_1(sc, 0x200000, 0x40);
2368 DELAY(1000);
2369
2370 /* write microcode into adapter memory */
2371 for (w = (const uint16_t *)uc; size > 0; w++, size -= 2)
2372 MEM_WRITE_2(sc, 0x200010, htole16(*w));
2373
2374 MEM_WRITE_1(sc, 0x200000, 0x00);
2375 MEM_WRITE_1(sc, 0x200000, 0x80);
2376
2377 /* wait until we get an answer */
2378 for (ntries = 0; ntries < 100; ntries++) {
2379 if (MEM_READ_1(sc, 0x200000) & 1)
2380 break;
2381 DELAY(100);
2382 }
2383 if (ntries == 100) {
2384 device_printf(sc->sc_dev,
2385 "timeout waiting for ucode to initialize\n");
2386 error = EIO;
2387 goto fail;
2388 }
2389
2390 /* read the answer or the firmware will not initialize properly */
2391 for (i = 0; i < 7; i++)
2392 MEM_READ_4(sc, 0x200004);
2393
2394 MEM_WRITE_1(sc, 0x200000, 0x00);
2395
2396fail:
2397 return error;
2398}
2399
2400/* macro to handle unaligned little endian data in firmware image */
2401#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2402
2403static int
2404iwi_load_firmware(struct iwi_softc *sc, const struct iwi_fw *fw)
2405{
2406 u_char *p, *end;
2407 uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp;
2408 int ntries, error;
2409
2410 IWI_LOCK_ASSERT(sc);
2411
2412 /* copy firmware image to DMA memory */
2413 memcpy(sc->fw_virtaddr, fw->data, fw->size);
2414
2415 /* make sure the adapter will get up-to-date values */
2416 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_PREWRITE);
2417
2418 /* tell the adapter where the command blocks are stored */
2419 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2420
2421 /*
2422 * Store command blocks into adapter's internal memory using register
2423 * indirections. The adapter will read the firmware image through DMA
2424 * using information stored in command blocks.
2425 */
2426 src = sc->fw_physaddr;
2427 p = sc->fw_virtaddr;
2428 end = p + fw->size;
2429 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2430
2431 while (p < end) {
2432 dst = GETLE32(p); p += 4; src += 4;
2433 len = GETLE32(p); p += 4; src += 4;
2434 p += len;
2435
2436 while (len > 0) {
2437 mlen = min(len, IWI_CB_MAXDATALEN);
2438
2439 ctl = IWI_CB_DEFAULT_CTL | mlen;
2440 sum = ctl ^ src ^ dst;
2441
2442 /* write a command block */
2443 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2444 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2445 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2446 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2447
2448 src += mlen;
2449 dst += mlen;
2450 len -= mlen;
2451 }
2452 }
2453
2454 /* write a fictive final command block (sentinel) */
2455 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2456 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2457
2458 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2459 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
2460 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2461
2462 /* tell the adapter to start processing command blocks */
2463 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2464
2465 /* wait until the adapter reaches the sentinel */
2466 for (ntries = 0; ntries < 400; ntries++) {
2467 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2468 break;
2469 DELAY(100);
2470 }
2471 /* sync dma, just in case */
2472 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_POSTWRITE);
2473 if (ntries == 400) {
2474 device_printf(sc->sc_dev,
2475 "timeout processing command blocks for %s firmware\n",
2476 fw->name);
2477 return EIO;
2478 }
2479
2480 /* we're done with command blocks processing */
2481 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2482
2483 /* allow interrupts so we know when the firmware is ready */
2484 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2485
2486 /* tell the adapter to initialize the firmware */
2487 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2488
2489 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2490 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
2491
2492 /* wait at most one second for firmware initialization to complete */
2493 if ((error = msleep(sc, &sc->sc_mtx, 0, "iwiinit", hz)) != 0) {
2494 device_printf(sc->sc_dev, "timeout waiting for %s firmware "
2495 "initialization to complete\n", fw->name);
2496 }
2497
2498 return error;
2499}
2500
2501static int
2502iwi_setpowermode(struct iwi_softc *sc, struct ieee80211vap *vap)
2503{
2504 uint32_t data;
2505
2506 if (vap->iv_flags & IEEE80211_F_PMGTON) {
2507 /* XXX set more fine-grained operation */
2508 data = htole32(IWI_POWER_MODE_MAX);
2509 } else
2510 data = htole32(IWI_POWER_MODE_CAM);
2511
2512 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2513 return iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data);
2514}
2515
2516static int
2517iwi_setwepkeys(struct iwi_softc *sc, struct ieee80211vap *vap)
2518{
2519 struct iwi_wep_key wepkey;
2520 struct ieee80211_key *wk;
2521 int error, i;
2522
2523 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2524 wk = &vap->iv_nw_keys[i];
2525
2526 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2527 wepkey.idx = i;
2528 wepkey.len = wk->wk_keylen;
2529 memset(wepkey.key, 0, sizeof wepkey.key);
2530 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2531 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2532 wepkey.len));
2533 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2534 sizeof wepkey);
2535 if (error != 0)
2536 return error;
2537 }
2538 return 0;
2539}
2540
2541static int
2542iwi_config(struct iwi_softc *sc)
2543{
2544 struct ieee80211com *ic = &sc->sc_ic;
2545 struct iwi_configuration config;
2546 struct iwi_rateset rs;
2547 struct iwi_txpower power;
2548 uint32_t data;
2549 int error, i;
2550
2551 IWI_LOCK_ASSERT(sc);
2552
2553 DPRINTF(("Setting MAC address to %6D\n", ic->ic_macaddr, ":"));
2554 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_macaddr,
2555 IEEE80211_ADDR_LEN);
2556 if (error != 0)
2557 return error;
2558
2559 memset(&config, 0, sizeof config);
2560 config.bluetooth_coexistence = sc->bluetooth;
2561 config.silence_threshold = 0x1e;
2562 config.antenna = sc->antenna;
2563 config.multicast_enabled = 1;
2564 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2565 config.disable_unicast_decryption = 1;
2566 config.disable_multicast_decryption = 1;
2567 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2568 config.allow_invalid_frames = 1;
2569 config.allow_beacon_and_probe_resp = 1;
2570 config.allow_mgt = 1;
2571 }
2572 DPRINTF(("Configuring adapter\n"));
2573 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2574 if (error != 0)
2575 return error;
2576 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2577 power.mode = IWI_MODE_11B;
2578 power.nchan = 11;
2579 for (i = 0; i < 11; i++) {
2580 power.chan[i].chan = i + 1;
2581 power.chan[i].power = IWI_TXPOWER_MAX;
2582 }
2583 DPRINTF(("Setting .11b channels tx power\n"));
2584 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2585 if (error != 0)
2586 return error;
2587
2588 power.mode = IWI_MODE_11G;
2589 DPRINTF(("Setting .11g channels tx power\n"));
2590 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2591 if (error != 0)
2592 return error;
2593 }
2594
2595 memset(&rs, 0, sizeof rs);
2596 rs.mode = IWI_MODE_11G;
2597 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2598 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2599 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2600 rs.nrates);
2601 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2602 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2603 if (error != 0)
2604 return error;
2605
2606 memset(&rs, 0, sizeof rs);
2607 rs.mode = IWI_MODE_11A;
2608 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2609 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2610 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2611 rs.nrates);
2612 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2613 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2614 if (error != 0)
2615 return error;
2616
2617 data = htole32(arc4random());
2618 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2619 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data);
2620 if (error != 0)
2621 return error;
2622
2623 /* enable adapter */
2624 DPRINTF(("Enabling adapter\n"));
2625 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0);
2626}
2627
2628static __inline void
2629set_scan_type(struct iwi_scan_ext *scan, int ix, int scan_type)
2630{
2631 uint8_t *st = &scan->scan_type[ix / 2];
2632 if (ix % 2)
2633 *st = (*st & 0xf0) | ((scan_type & 0xf) << 0);
2634 else
2635 *st = (*st & 0x0f) | ((scan_type & 0xf) << 4);
2636}
2637
2638static int
2639scan_type(const struct ieee80211_scan_state *ss,
2640 const struct ieee80211_channel *chan)
2641{
2642 /* We can only set one essid for a directed scan */
2643 if (ss->ss_nssid != 0)
2644 return IWI_SCAN_TYPE_BDIRECTED;
2645 if ((ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
2646 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
2647 return IWI_SCAN_TYPE_BROADCAST;
2648 return IWI_SCAN_TYPE_PASSIVE;
2649}
2650
2651static __inline int
2652scan_band(const struct ieee80211_channel *c)
2653{
2654 return IEEE80211_IS_CHAN_5GHZ(c) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ;
2655}
2656
2657static void
2658iwi_monitor_scan(void *arg, int npending)
2659{
2660 struct iwi_softc *sc = arg;
2661 IWI_LOCK_DECL;
2662
2663 IWI_LOCK(sc);
2664 (void) iwi_scanchan(sc, 2000, 0);
2665 IWI_UNLOCK(sc);
2666}
2667
2668/*
2669 * Start a scan on the current channel or all channels.
2670 */
2671static int
2672iwi_scanchan(struct iwi_softc *sc, unsigned long maxdwell, int allchan)
2673{
2674 struct ieee80211com *ic = &sc->sc_ic;
2675 struct ieee80211_channel *chan;
2676 struct ieee80211_scan_state *ss;
2677 struct iwi_scan_ext scan;
2678 int error = 0;
2679
2680 IWI_LOCK_ASSERT(sc);
2681 if (sc->fw_state == IWI_FW_SCANNING) {
2682 /*
2683 * This should not happen as we only trigger scan_next after
2684 * completion
2685 */
2686 DPRINTF(("%s: called too early - still scanning\n", __func__));
2687 return (EBUSY);
2688 }
2689 IWI_STATE_BEGIN(sc, IWI_FW_SCANNING);
2690
2691 ss = ic->ic_scan;
2692
2693 memset(&scan, 0, sizeof scan);
2694 scan.full_scan_index = htole32(++sc->sc_scangen);
2695 scan.dwell_time[IWI_SCAN_TYPE_PASSIVE] = htole16(maxdwell);
2696 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
2697 /*
2698 * Use very short dwell times for when we send probe request
2699 * frames. Without this bg scans hang. Ideally this should
2700 * be handled with early-termination as done by net80211 but
2701 * that's not feasible (aborting a scan is problematic).
2702 */
2703 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(30);
2704 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(30);
2705 } else {
2706 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(maxdwell);
2707 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(maxdwell);
2708 }
2709
2710 /* We can only set one essid for a directed scan */
2711 if (ss->ss_nssid != 0) {
2712 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ss->ss_ssid[0].ssid,
2713 ss->ss_ssid[0].len);
2714 if (error)
2715 return (error);
2716 }
2717
2718 if (allchan) {
2719 int i, next, band, b, bstart;
2720 /*
2721 * Convert scan list to run-length encoded channel list
2722 * the firmware requires (preserving the order setup by
2723 * net80211). The first entry in each run specifies the
2724 * band and the count of items in the run.
2725 */
2726 next = 0; /* next open slot */
2727 bstart = 0; /* NB: not needed, silence compiler */
2728 band = -1; /* NB: impossible value */
2729 KASSERT(ss->ss_last > 0, ("no channels"));
2730 for (i = 0; i < ss->ss_last; i++) {
2731 chan = ss->ss_chans[i];
2732 b = scan_band(chan);
2733 if (b != band) {
2734 if (band != -1)
2735 scan.channels[bstart] =
2736 (next - bstart) | band;
2737 /* NB: this allocates a slot for the run-len */
2738 band = b, bstart = next++;
2739 }
2740 if (next >= IWI_SCAN_CHANNELS) {
2741 DPRINTF(("truncating scan list\n"));
2742 break;
2743 }
2744 scan.channels[next] = ieee80211_chan2ieee(ic, chan);
2745 set_scan_type(&scan, next, scan_type(ss, chan));
2746 next++;
2747 }
2748 scan.channels[bstart] = (next - bstart) | band;
2749 } else {
2750 /* Scan the current channel only */
2751 chan = ic->ic_curchan;
2752 scan.channels[0] = 1 | scan_band(chan);
2753 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2754 set_scan_type(&scan, 1, scan_type(ss, chan));
2755 }
2756#ifdef IWI_DEBUG
2757 if (iwi_debug > 0) {
2758 static const char *scantype[8] =
2759 { "PSTOP", "PASV", "DIR", "BCAST", "BDIR", "5", "6", "7" };
2760 int i;
2761 printf("Scan request: index %u dwell %d/%d/%d\n"
2762 , le32toh(scan.full_scan_index)
2763 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_PASSIVE])
2764 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BROADCAST])
2765 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED])
2766 );
2767 i = 0;
2768 do {
2769 int run = scan.channels[i];
2770 if (run == 0)
2771 break;
2772 printf("Scan %d %s channels:", run & 0x3f,
2773 run & IWI_CHAN_2GHZ ? "2.4GHz" : "5GHz");
2774 for (run &= 0x3f, i++; run > 0; run--, i++) {
2775 uint8_t type = scan.scan_type[i/2];
2776 printf(" %u/%s", scan.channels[i],
2777 scantype[(i & 1 ? type : type>>4) & 7]);
2778 }
2779 printf("\n");
2780 } while (i < IWI_SCAN_CHANNELS);
2781 }
2782#endif
2783
2784 return (iwi_cmd(sc, IWI_CMD_SCAN_EXT, &scan, sizeof scan));
2785}
2786
2787static int
2788iwi_set_sensitivity(struct iwi_softc *sc, int8_t rssi_dbm)
2789{
2790 struct iwi_sensitivity sens;
2791
2792 DPRINTF(("Setting sensitivity to %d\n", rssi_dbm));
2793
2794 memset(&sens, 0, sizeof sens);
2795 sens.rssi = htole16(rssi_dbm);
2796 return iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &sens, sizeof sens);
2797}
2798
2799static int
2800iwi_auth_and_assoc(struct iwi_softc *sc, struct ieee80211vap *vap)
2801{
2802 struct ieee80211com *ic = vap->iv_ic;
2803 struct ifnet *ifp = vap->iv_ifp;
2804 struct ieee80211_node *ni;
2805 struct iwi_configuration config;
2806 struct iwi_associate *assoc = &sc->assoc;
2807 struct iwi_rateset rs;
2808 uint16_t capinfo;
2809 uint32_t data;
2810 int error, mode;
2811
2812 IWI_LOCK_ASSERT(sc);
2813
2814 ni = ieee80211_ref_node(vap->iv_bss);
2815
2816 if (sc->flags & IWI_FLAG_ASSOCIATED) {
2817 DPRINTF(("Already associated\n"));
2818 return (-1);
2819 }
2820
2821 IWI_STATE_BEGIN(sc, IWI_FW_ASSOCIATING);
2822 error = 0;
2823 mode = 0;
2824
2825 if (IEEE80211_IS_CHAN_A(ic->ic_curchan))
2826 mode = IWI_MODE_11A;
2827 else if (IEEE80211_IS_CHAN_G(ic->ic_curchan))
2828 mode = IWI_MODE_11G;
2829 if (IEEE80211_IS_CHAN_B(ic->ic_curchan))
2830 mode = IWI_MODE_11B;
2831
2832 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2833 memset(&config, 0, sizeof config);
2834 config.bluetooth_coexistence = sc->bluetooth;
2835 config.antenna = sc->antenna;
2836 config.multicast_enabled = 1;
2837 if (mode == IWI_MODE_11G)
2838 config.use_protection = 1;
2839 config.answer_pbreq =
2840 (vap->iv_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2841 config.disable_unicast_decryption = 1;
2842 config.disable_multicast_decryption = 1;
2843 DPRINTF(("Configuring adapter\n"));
2844 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2845 if (error != 0)
2846 goto done;
2847 }
2848
2849#ifdef IWI_DEBUG
2850 if (iwi_debug > 0) {
2851 printf("Setting ESSID to ");
2852 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2853 printf("\n");
2854 }
2855#endif
2856 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen);
2857 if (error != 0)
2858 goto done;
2859
2860 error = iwi_setpowermode(sc, vap);
2861 if (error != 0)
2862 goto done;
2863
2864 data = htole32(vap->iv_rtsthreshold);
2865 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2866 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2867 if (error != 0)
2868 goto done;
2869
2870 data = htole32(vap->iv_fragthreshold);
2871 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2872 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2873 if (error != 0)
2874 goto done;
2875
2876 /* the rate set has already been "negotiated" */
2877 memset(&rs, 0, sizeof rs);
2878 rs.mode = mode;
2879 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2880 rs.nrates = ni->ni_rates.rs_nrates;
2881 if (rs.nrates > IWI_RATESET_SIZE) {
2882 DPRINTF(("Truncating negotiated rate set from %u\n",
2883 rs.nrates));
2884 rs.nrates = IWI_RATESET_SIZE;
2885 }
2886 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2887 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
2888 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2889 if (error != 0)
2890 goto done;
2891
2892 memset(assoc, 0, sizeof *assoc);
2893
2894 if ((vap->iv_flags & IEEE80211_F_WME) && ni->ni_ies.wme_ie != NULL) {
2895 /* NB: don't treat WME setup as failure */
2896 if (iwi_wme_setparams(sc) == 0 && iwi_wme_setie(sc) == 0)
2897 assoc->policy |= htole16(IWI_POLICY_WME);
2898 /* XXX complain on failure? */
2899 }
2900
2901 if (vap->iv_appie_wpa != NULL) {
2902 struct ieee80211_appie *ie = vap->iv_appie_wpa;
2903
2904 DPRINTF(("Setting optional IE (len=%u)\n", ie->ie_len));
2905 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ie->ie_data, ie->ie_len);
2906 if (error != 0)
2907 goto done;
2908 }
2909
2910 error = iwi_set_sensitivity(sc, ic->ic_node_getrssi(ni));
2911 if (error != 0)
2912 goto done;
2913
2914 assoc->mode = mode;
2915 assoc->chan = ic->ic_curchan->ic_ieee;
2916 /*
2917 * NB: do not arrange for shared key auth w/o privacy
2918 * (i.e. a wep key); it causes a firmware error.
2919 */
2920 if ((vap->iv_flags & IEEE80211_F_PRIVACY) &&
2921 ni->ni_authmode == IEEE80211_AUTH_SHARED) {
2922 assoc->auth = IWI_AUTH_SHARED;
2923 /*
2924 * It's possible to have privacy marked but no default
2925 * key setup. This typically is due to a user app bug
2926 * but if we blindly grab the key the firmware will
2927 * barf so avoid it for now.
2928 */
2929 if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE)
2930 assoc->auth |= vap->iv_def_txkey << 4;
2931
2932 error = iwi_setwepkeys(sc, vap);
2933 if (error != 0)
2934 goto done;
2935 }
2936 if (vap->iv_flags & IEEE80211_F_WPA)
2937 assoc->policy |= htole16(IWI_POLICY_WPA);
2938 if (vap->iv_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0)
2939 assoc->type = IWI_HC_IBSS_START;
2940 else
2941 assoc->type = IWI_HC_ASSOC;
2942 memcpy(assoc->tstamp, ni->ni_tstamp.data, 8);
2943
2944 if (vap->iv_opmode == IEEE80211_M_IBSS)
2945 capinfo = IEEE80211_CAPINFO_IBSS;
2946 else
2947 capinfo = IEEE80211_CAPINFO_ESS;
2948 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2949 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2950 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2951 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2952 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2953 if (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2954 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2955 assoc->capinfo = htole16(capinfo);
2956
2957 assoc->lintval = htole16(ic->ic_lintval);
2958 assoc->intval = htole16(ni->ni_intval);
2959 IEEE80211_ADDR_COPY(assoc->bssid, ni->ni_bssid);
2960 if (vap->iv_opmode == IEEE80211_M_IBSS)
2961 IEEE80211_ADDR_COPY(assoc->dst, ifp->if_broadcastaddr);
2962 else
2963 IEEE80211_ADDR_COPY(assoc->dst, ni->ni_bssid);
2964
2965 DPRINTF(("%s bssid %6D dst %6D channel %u policy 0x%x "
2966 "auth %u capinfo 0x%x lintval %u bintval %u\n",
2967 assoc->type == IWI_HC_IBSS_START ? "Start" : "Join",
2968 assoc->bssid, ":", assoc->dst, ":",
2969 assoc->chan, le16toh(assoc->policy), assoc->auth,
2970 le16toh(assoc->capinfo), le16toh(assoc->lintval),
2971 le16toh(assoc->intval)));
2972 error = iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
2973done:
2974 ieee80211_free_node(ni);
2975 if (error)
2976 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
2977
2978 return (error);
2979}
2980
2981static void
2982iwi_disassoc(void *arg, int pending)
2983{
2984 struct iwi_softc *sc = arg;
2985 IWI_LOCK_DECL;
2986
2987 IWI_LOCK(sc);
2988 iwi_disassociate(sc, 0);
2989 IWI_UNLOCK(sc);
2990}
2991
2992static int
2993iwi_disassociate(struct iwi_softc *sc, int quiet)
2994{
2995 struct iwi_associate *assoc = &sc->assoc;
2996
2997 if ((sc->flags & IWI_FLAG_ASSOCIATED) == 0) {
2998 DPRINTF(("Not associated\n"));
2999 return (-1);
3000 }
3001
3002 IWI_STATE_BEGIN(sc, IWI_FW_DISASSOCIATING);
3003
3004 if (quiet)
3005 assoc->type = IWI_HC_DISASSOC_QUIET;
3006 else
3007 assoc->type = IWI_HC_DISASSOC;
3008
3009 DPRINTF(("Trying to disassociate from %6D channel %u\n",
3010 assoc->bssid, ":", assoc->chan));
3011 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
3012}
3013
3014/*
3015 * release dma resources for the firmware
3016 */
3017static void
3018iwi_release_fw_dma(struct iwi_softc *sc)
3019{
3020 if (sc->fw_flags & IWI_FW_HAVE_PHY)
3021 bus_dmamap_unload(sc->fw_dmat, sc->fw_map);
3022 if (sc->fw_flags & IWI_FW_HAVE_MAP)
3023 bus_dmamem_free(sc->fw_dmat, sc->fw_virtaddr, sc->fw_map);
3024 if (sc->fw_flags & IWI_FW_HAVE_DMAT)
3025 bus_dma_tag_destroy(sc->fw_dmat);
3026
3027 sc->fw_flags = 0;
3028 sc->fw_dma_size = 0;
3029 sc->fw_dmat = NULL;
3030 sc->fw_map = NULL;
3031 sc->fw_physaddr = 0;
3032 sc->fw_virtaddr = NULL;
3033}
3034
3035/*
3036 * allocate the dma descriptor for the firmware.
3037 * Return 0 on success, 1 on error.
3038 * Must be called unlocked, protected by IWI_FLAG_FW_LOADING.
3039 */
3040static int
3041iwi_init_fw_dma(struct iwi_softc *sc, int size)
3042{
3043 if (sc->fw_dma_size >= size)
3044 return 0;
3045 if (bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
3046 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
3047 size, 1, size, 0, NULL, NULL, &sc->fw_dmat) != 0) {
3048 device_printf(sc->sc_dev,
3049 "could not create firmware DMA tag\n");
3050 goto error;
3051 }
3052 sc->fw_flags |= IWI_FW_HAVE_DMAT;
3053 if (bus_dmamem_alloc(sc->fw_dmat, &sc->fw_virtaddr, 0,
3054 &sc->fw_map) != 0) {
3055 device_printf(sc->sc_dev,
3056 "could not allocate firmware DMA memory\n");
3057 goto error;
3058 }
3059 sc->fw_flags |= IWI_FW_HAVE_MAP;
3060 if (bus_dmamap_load(sc->fw_dmat, sc->fw_map, sc->fw_virtaddr,
3061 size, iwi_dma_map_addr, &sc->fw_physaddr, 0) != 0) {
3062 device_printf(sc->sc_dev, "could not load firmware DMA map\n");
3063 goto error;
3064 }
3065 sc->fw_flags |= IWI_FW_HAVE_PHY;
3066 sc->fw_dma_size = size;
3067 return 0;
3068
3069error:
3070 iwi_release_fw_dma(sc);
3071 return 1;
3072}
3073
3074static void
3075iwi_init_locked(struct iwi_softc *sc)
3076{
3077 struct iwi_rx_data *data;
3078 int i;
3079
3080 IWI_LOCK_ASSERT(sc);
3081
3082 if (sc->fw_state == IWI_FW_LOADING) {
3083 device_printf(sc->sc_dev, "%s: already loading\n", __func__);
3084 return; /* XXX: condvar? */
3085 }
3086
3087 iwi_stop_locked(sc);
3088
3089 IWI_STATE_BEGIN(sc, IWI_FW_LOADING);
3090
3091 if (iwi_reset(sc) != 0) {
3092 device_printf(sc->sc_dev, "could not reset adapter\n");
3093 goto fail;
3094 }
3095 if (iwi_load_firmware(sc, &sc->fw_boot) != 0) {
3096 device_printf(sc->sc_dev,
3097 "could not load boot firmware %s\n", sc->fw_boot.name);
3098 goto fail;
3099 }
3100 if (iwi_load_ucode(sc, &sc->fw_uc) != 0) {
3101 device_printf(sc->sc_dev,
3102 "could not load microcode %s\n", sc->fw_uc.name);
3103 goto fail;
3104 }
3105
3106 iwi_stop_master(sc);
3107
3108 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr);
3109 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
3110 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
3111
3112 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr);
3113 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
3114 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
3115
3116 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr);
3117 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
3118 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
3119
3120 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr);
3121 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
3122 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
3123
3124 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr);
3125 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
3126 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
3127
3128 for (i = 0; i < sc->rxq.count; i++) {
3129 data = &sc->rxq.data[i];
3130 CSR_WRITE_4(sc, data->reg, data->physaddr);
3131 }
3132
3133 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1);
3134
3135 if (iwi_load_firmware(sc, &sc->fw_fw) != 0) {
3136 device_printf(sc->sc_dev,
3137 "could not load main firmware %s\n", sc->fw_fw.name);
3138 goto fail;
3139 }
3140 sc->flags |= IWI_FLAG_FW_INITED;
3141
3142 IWI_STATE_END(sc, IWI_FW_LOADING);
3143
3144 if (iwi_config(sc) != 0) {
3145 device_printf(sc->sc_dev, "unable to enable adapter\n");
3146 goto fail2;
3147 }
3148
3149 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
3150 sc->sc_running = 1;
3151 return;
3152fail:
3153 IWI_STATE_END(sc, IWI_FW_LOADING);
3154fail2:
3155 iwi_stop_locked(sc);
3156}
3157
3158static void
3159iwi_init(void *priv)
3160{
3161 struct iwi_softc *sc = priv;
3162 struct ieee80211com *ic = &sc->sc_ic;
3163 IWI_LOCK_DECL;
3164
3165 IWI_LOCK(sc);
3166 iwi_init_locked(sc);
3167 IWI_UNLOCK(sc);
3168
3169 if (sc->sc_running)
3170 ieee80211_start_all(ic);
3171}
3172
3173static void
3174iwi_stop_locked(void *priv)
3175{
3176 struct iwi_softc *sc = priv;
3177
3178 IWI_LOCK_ASSERT(sc);
3179
3180 sc->sc_running = 0;
3181
3182 if (sc->sc_softled) {
3183 callout_stop(&sc->sc_ledtimer);
3184 sc->sc_blinking = 0;
3185 }
3186 callout_stop(&sc->sc_wdtimer);
3187 callout_stop(&sc->sc_rftimer);
3188
3189 iwi_stop_master(sc);
3190
3191 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET);
3192
3193 /* reset rings */
3194 iwi_reset_cmd_ring(sc, &sc->cmdq);
3195 iwi_reset_tx_ring(sc, &sc->txq[0]);
3196 iwi_reset_tx_ring(sc, &sc->txq[1]);
3197 iwi_reset_tx_ring(sc, &sc->txq[2]);
3198 iwi_reset_tx_ring(sc, &sc->txq[3]);
3199 iwi_reset_rx_ring(sc, &sc->rxq);
3200
3201 sc->sc_tx_timer = 0;
3202 sc->sc_state_timer = 0;
3203 sc->sc_busy_timer = 0;
3204 sc->flags &= ~(IWI_FLAG_BUSY | IWI_FLAG_ASSOCIATED);
3205 sc->fw_state = IWI_FW_IDLE;
3206 wakeup(sc);
3207}
3208
3209static void
3210iwi_stop(struct iwi_softc *sc)
3211{
3212 IWI_LOCK_DECL;
3213
3214 IWI_LOCK(sc);
3215 iwi_stop_locked(sc);
3216 IWI_UNLOCK(sc);
3217}
3218
3219static void
3220iwi_restart(void *arg, int npending)
3221{
3222 struct iwi_softc *sc = arg;
3223
3224 iwi_init(sc);
3225}
3226
3227/*
3228 * Return whether or not the radio is enabled in hardware
3229 * (i.e. the rfkill switch is "off").
3230 */
3231static int
3232iwi_getrfkill(struct iwi_softc *sc)
3233{
3234 return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0;
3235}
3236
3237static void
3238iwi_radio_on(void *arg, int pending)
3239{
3240 struct iwi_softc *sc = arg;
3241 struct ieee80211com *ic = &sc->sc_ic;
3242
3243 device_printf(sc->sc_dev, "radio turned on\n");
3244
3245 iwi_init(sc);
3246 ieee80211_notify_radio(ic, 1);
3247}
3248
3249static void
3250iwi_rfkill_poll(void *arg)
3251{
3252 struct iwi_softc *sc = arg;
3253
3254 IWI_LOCK_ASSERT(sc);
3255
3256 /*
3257 * Check for a change in rfkill state. We get an
3258 * interrupt when a radio is disabled but not when
3259 * it is enabled so we must poll for the latter.
3260 */
3261 if (!iwi_getrfkill(sc)) {
3262 ieee80211_runtask(&sc->sc_ic, &sc->sc_radiontask);
3263 return;
3264 }
3265 callout_reset(&sc->sc_rftimer, 2*hz, iwi_rfkill_poll, sc);
3266}
3267
3268static void
3269iwi_radio_off(void *arg, int pending)
3270{
3271 struct iwi_softc *sc = arg;
3272 struct ieee80211com *ic = &sc->sc_ic;
3273 IWI_LOCK_DECL;
3274
3275 device_printf(sc->sc_dev, "radio turned off\n");
3276
3277 ieee80211_notify_radio(ic, 0);
3278
3279 IWI_LOCK(sc);
3280 iwi_stop_locked(sc);
3281 iwi_rfkill_poll(sc);
3282 IWI_UNLOCK(sc);
3283}
3284
3285static int
3286iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
3287{
3288 struct iwi_softc *sc = arg1;
3289 uint32_t size, buf[128];
3290
3291 memset(buf, 0, sizeof buf);
3292
3293 if (!(sc->flags & IWI_FLAG_FW_INITED))
3294 return SYSCTL_OUT(req, buf, sizeof buf);
3295
3296 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
3297 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
3298
3299 return SYSCTL_OUT(req, buf, size);
3300}
3301
3302static int
3303iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
3304{
3305 struct iwi_softc *sc = arg1;
3306 int val = !iwi_getrfkill(sc);
3307
3308 return SYSCTL_OUT(req, &val, sizeof val);
3309}
3310
3311/*
3312 * Add sysctl knobs.
3313 */
3314static void
3315iwi_sysctlattach(struct iwi_softc *sc)
3316{
3317 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3318 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3319
3320 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "radio",
3321 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I",
3322 "radio transmitter switch state (0=off, 1=on)");
3323
3324 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "stats",
3325 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S",
3326 "statistics");
3327
3328 sc->bluetooth = 0;
3329 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "bluetooth",
3330 CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence");
3331
3332 sc->antenna = IWI_ANTENNA_AUTO;
3333 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "antenna",
3334 CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)");
3335}
3336
3337/*
3338 * LED support.
3339 *
3340 * Different cards have different capabilities. Some have three
3341 * led's while others have only one. The linux ipw driver defines
3342 * led's for link state (associated or not), band (11a, 11g, 11b),
3343 * and for link activity. We use one led and vary the blink rate
3344 * according to the tx/rx traffic a la the ath driver.
3345 */
3346
3347static __inline uint32_t
3348iwi_toggle_event(uint32_t r)
3349{
3350 return r &~ (IWI_RST_STANDBY | IWI_RST_GATE_ODMA |
3351 IWI_RST_GATE_IDMA | IWI_RST_GATE_ADMA);
3352}
3353
3354static uint32_t
3355iwi_read_event(struct iwi_softc *sc)
3356{
3357 return MEM_READ_4(sc, IWI_MEM_EEPROM_EVENT);
3358}
3359
3360static void
3361iwi_write_event(struct iwi_softc *sc, uint32_t v)
3362{
3363 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, v);
3364}
3365
3366static void
3367iwi_led_done(void *arg)
3368{
3369 struct iwi_softc *sc = arg;
3370
3371 sc->sc_blinking = 0;
3372}
3373
3374/*
3375 * Turn the activity LED off: flip the pin and then set a timer so no
3376 * update will happen for the specified duration.
3377 */
3378static void
3379iwi_led_off(void *arg)
3380{
3381 struct iwi_softc *sc = arg;
3382 uint32_t v;
3383
3384 v = iwi_read_event(sc);
3385 v &= ~sc->sc_ledpin;
3386 iwi_write_event(sc, iwi_toggle_event(v));
3387 callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, iwi_led_done, sc);
3388}
3389
3390/*
3391 * Blink the LED according to the specified on/off times.
3392 */
3393static void
3394iwi_led_blink(struct iwi_softc *sc, int on, int off)
3395{
3396 uint32_t v;
3397
3398 v = iwi_read_event(sc);
3399 v |= sc->sc_ledpin;
3400 iwi_write_event(sc, iwi_toggle_event(v));
3401 sc->sc_blinking = 1;
3402 sc->sc_ledoff = off;
3403 callout_reset(&sc->sc_ledtimer, on, iwi_led_off, sc);
3404}
3405
3406static void
3407iwi_led_event(struct iwi_softc *sc, int event)
3408{
3409 /* NB: on/off times from the Atheros NDIS driver, w/ permission */
3410 static const struct {
3411 u_int rate; /* tx/rx iwi rate */
3412 u_int16_t timeOn; /* LED on time (ms) */
3413 u_int16_t timeOff; /* LED off time (ms) */
3414 } blinkrates[] = {
3415 { IWI_RATE_OFDM54, 40, 10 },
3416 { IWI_RATE_OFDM48, 44, 11 },
3417 { IWI_RATE_OFDM36, 50, 13 },
3418 { IWI_RATE_OFDM24, 57, 14 },
3419 { IWI_RATE_OFDM18, 67, 16 },
3420 { IWI_RATE_OFDM12, 80, 20 },
3421 { IWI_RATE_DS11, 100, 25 },
3422 { IWI_RATE_OFDM9, 133, 34 },
3423 { IWI_RATE_OFDM6, 160, 40 },
3424 { IWI_RATE_DS5, 200, 50 },
3425 { 6, 240, 58 }, /* XXX 3Mb/s if it existed */
3426 { IWI_RATE_DS2, 267, 66 },
3427 { IWI_RATE_DS1, 400, 100 },
3428 { 0, 500, 130 }, /* unknown rate/polling */
3429 };
3430 uint32_t txrate;
3431 int j = 0; /* XXX silence compiler */
3432
3433 sc->sc_ledevent = ticks; /* time of last event */
3434 if (sc->sc_blinking) /* don't interrupt active blink */
3435 return;
3436 switch (event) {
3437 case IWI_LED_POLL:
3438 j = nitems(blinkrates)-1;
3439 break;
3440 case IWI_LED_TX:
3441 /* read current transmission rate from adapter */
3442 txrate = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
3443 if (blinkrates[sc->sc_txrix].rate != txrate) {
3444 for (j = 0; j < nitems(blinkrates)-1; j++)
3445 if (blinkrates[j].rate == txrate)
3446 break;
3447 sc->sc_txrix = j;
3448 } else
3449 j = sc->sc_txrix;
3450 break;
3451 case IWI_LED_RX:
3452 if (blinkrates[sc->sc_rxrix].rate != sc->sc_rxrate) {
3453 for (j = 0; j < nitems(blinkrates)-1; j++)
3454 if (blinkrates[j].rate == sc->sc_rxrate)
3455 break;
3456 sc->sc_rxrix = j;
3457 } else
3458 j = sc->sc_rxrix;
3459 break;
3460 }
3461 /* XXX beware of overflow */
3462 iwi_led_blink(sc, (blinkrates[j].timeOn * hz) / 1000,
3463 (blinkrates[j].timeOff * hz) / 1000);
3464}
3465
3466static int
3467iwi_sysctl_softled(SYSCTL_HANDLER_ARGS)
3468{
3469 struct iwi_softc *sc = arg1;
3470 int softled = sc->sc_softled;
3471 int error;
3472
3473 error = sysctl_handle_int(oidp, &softled, 0, req);
3474 if (error || !req->newptr)
3475 return error;
3476 softled = (softled != 0);
3477 if (softled != sc->sc_softled) {
3478 if (softled) {
3479 uint32_t v = iwi_read_event(sc);
3480 v &= ~sc->sc_ledpin;
3481 iwi_write_event(sc, iwi_toggle_event(v));
3482 }
3483 sc->sc_softled = softled;
3484 }
3485 return 0;
3486}
3487
3488static void
3489iwi_ledattach(struct iwi_softc *sc)
3490{
3491 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3492 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3493
3494 sc->sc_blinking = 0;
3495 sc->sc_ledstate = 1;
3496 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */
3497 callout_init_mtx(&sc->sc_ledtimer, &sc->sc_mtx, 0);
3498
3499 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3500 "softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
3501 iwi_sysctl_softled, "I", "enable/disable software LED support");
3502 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3503 "ledpin", CTLFLAG_RW, &sc->sc_ledpin, 0,
3504 "pin setting to turn activity LED on");
3505 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3506 "ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0,
3507 "idle time for inactivity LED (ticks)");
3508 /* XXX for debugging */
3509 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3510 "nictype", CTLFLAG_RD, &sc->sc_nictype, 0,
3511 "NIC type from EEPROM");
3512
3513 sc->sc_ledpin = IWI_RST_LED_ACTIVITY;
3514 sc->sc_softled = 1;
3515
3516 sc->sc_nictype = (iwi_read_prom_word(sc, IWI_EEPROM_NIC) >> 8) & 0xff;
3517 if (sc->sc_nictype == 1) {
3518 /*
3519 * NB: led's are reversed.
3520 */
3521 sc->sc_ledpin = IWI_RST_LED_ASSOCIATED;
3522 }
3523}
3524
3525static void
3526iwi_scan_start(struct ieee80211com *ic)
3527{
3528 /* ignore */
3529}
3530
3531static void
3532iwi_set_channel(struct ieee80211com *ic)
3533{
3534 struct iwi_softc *sc = ic->ic_softc;
3535
3536 if (sc->fw_state == IWI_FW_IDLE)
3537 iwi_setcurchan(sc, ic->ic_curchan->ic_ieee);
3538}
3539
3540static void
3541iwi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
3542{
3543 struct ieee80211vap *vap = ss->ss_vap;
3544 struct iwi_softc *sc = vap->iv_ic->ic_softc;
3545 IWI_LOCK_DECL;
3546
3547 IWI_LOCK(sc);
3548 if (iwi_scanchan(sc, maxdwell, 0))
3549 ieee80211_cancel_scan(vap);
3550 IWI_UNLOCK(sc);
3551}
3552
3553static void
3554iwi_scan_mindwell(struct ieee80211_scan_state *ss)
3555{
3556 /* NB: don't try to abort scan; wait for firmware to finish */
3557}
3558
3559static void
3560iwi_scan_end(struct ieee80211com *ic)
3561{
3562 struct iwi_softc *sc = ic->ic_softc;
3563 IWI_LOCK_DECL;
3564
3565 IWI_LOCK(sc);
3566 sc->flags &= ~IWI_FLAG_CHANNEL_SCAN;
3567 /* NB: make sure we're still scanning */
3568 if (sc->fw_state == IWI_FW_SCANNING)
3569 iwi_cmd(sc, IWI_CMD_ABORT_SCAN, NULL, 0);
3570 IWI_UNLOCK(sc);
3571}