2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004, 2005
5 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
6 * Copyright (c) 2005-2006 Sam Leffler, Errno Consulting
7 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
37 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
40 #include <sys/param.h>
41 #include <sys/sysctl.h>
42 #include <sys/sockio.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/module.h>
52 #include <sys/endian.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/linker.h>
57 #include <sys/firmware.h>
58 #include <sys/taskqueue.h>
60 #include <machine/bus.h>
61 #include <machine/resource.h>
64 #include <dev/pci/pcireg.h>
65 #include <dev/pci/pcivar.h>
69 #include <net/if_var.h>
70 #include <net/if_arp.h>
71 #include <net/ethernet.h>
72 #include <net/if_dl.h>
73 #include <net/if_media.h>
74 #include <net/if_types.h>
76 #include <net80211/ieee80211_var.h>
77 #include <net80211/ieee80211_radiotap.h>
78 #include <net80211/ieee80211_input.h>
79 #include <net80211/ieee80211_regdomain.h>
81 #include <netinet/in.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip.h>
85 #include <netinet/if_ether.h>
87 #include <dev/iwi/if_iwireg.h>
88 #include <dev/iwi/if_iwivar.h>
89 #include <dev/iwi/if_iwi_ioctl.h>
93 #define DPRINTF(x) do { if (iwi_debug > 0) printf x; } while (0)
94 #define DPRINTFN(n, x) do { if (iwi_debug >= (n)) printf x; } while (0)
96 SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level");
98 static const char *iwi_fw_states[] = {
99 "IDLE", /* IWI_FW_IDLE */
100 "LOADING", /* IWI_FW_LOADING */
101 "ASSOCIATING", /* IWI_FW_ASSOCIATING */
102 "DISASSOCIATING", /* IWI_FW_DISASSOCIATING */
103 "SCANNING", /* IWI_FW_SCANNING */
107 #define DPRINTFN(n, x)
110 MODULE_DEPEND(iwi, pci, 1, 1, 1);
111 MODULE_DEPEND(iwi, wlan, 1, 1, 1);
112 MODULE_DEPEND(iwi, firmware, 1, 1, 1);
126 static const struct iwi_ident iwi_ident_table[] = {
127 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" },
128 { 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" },
129 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" },
130 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" },
135 static const uint8_t def_chan_2ghz[] =
136 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
137 static const uint8_t def_chan_5ghz_band1[] =
138 { 36, 40, 44, 48, 52, 56, 60, 64 };
139 static const uint8_t def_chan_5ghz_band2[] =
140 { 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140 };
141 static const uint8_t def_chan_5ghz_band3[] =
142 { 149, 153, 157, 161, 165 };
144 static struct ieee80211vap *iwi_vap_create(struct ieee80211com *,
145 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
146 const uint8_t [IEEE80211_ADDR_LEN],
147 const uint8_t [IEEE80211_ADDR_LEN]);
148 static void iwi_vap_delete(struct ieee80211vap *);
149 static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
150 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
152 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
153 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
154 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
155 int, bus_addr_t, bus_addr_t);
156 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
157 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
158 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
160 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
161 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
162 static struct ieee80211_node *iwi_node_alloc(struct ieee80211vap *,
163 const uint8_t [IEEE80211_ADDR_LEN]);
164 static void iwi_node_free(struct ieee80211_node *);
165 static void iwi_media_status(struct ifnet *, struct ifmediareq *);
166 static int iwi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
167 static void iwi_wme_init(struct iwi_softc *);
168 static int iwi_wme_setparams(struct iwi_softc *);
169 static int iwi_wme_update(struct ieee80211com *);
170 static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t);
171 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
173 static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
174 static void iwi_rx_intr(struct iwi_softc *);
175 static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
176 static void iwi_intr(void *);
177 static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t);
178 static void iwi_write_ibssnode(struct iwi_softc *, const u_int8_t [], int);
179 static int iwi_tx_start(struct iwi_softc *, struct mbuf *,
180 struct ieee80211_node *, int);
181 static int iwi_raw_xmit(struct ieee80211_node *, struct mbuf *,
182 const struct ieee80211_bpf_params *);
183 static void iwi_start(struct iwi_softc *);
184 static int iwi_transmit(struct ieee80211com *, struct mbuf *);
185 static void iwi_watchdog(void *);
186 static int iwi_ioctl(struct ieee80211com *, u_long, void *);
187 static void iwi_parent(struct ieee80211com *);
188 static void iwi_stop_master(struct iwi_softc *);
189 static int iwi_reset(struct iwi_softc *);
190 static int iwi_load_ucode(struct iwi_softc *, const struct iwi_fw *);
191 static int iwi_load_firmware(struct iwi_softc *, const struct iwi_fw *);
192 static void iwi_release_fw_dma(struct iwi_softc *sc);
193 static int iwi_config(struct iwi_softc *);
194 static int iwi_get_firmware(struct iwi_softc *, enum ieee80211_opmode);
195 static void iwi_put_firmware(struct iwi_softc *);
196 static void iwi_monitor_scan(void *, int);
197 static int iwi_scanchan(struct iwi_softc *, unsigned long, int);
198 static void iwi_scan_start(struct ieee80211com *);
199 static void iwi_scan_end(struct ieee80211com *);
200 static void iwi_set_channel(struct ieee80211com *);
201 static void iwi_scan_curchan(struct ieee80211_scan_state *, unsigned long maxdwell);
202 static void iwi_scan_mindwell(struct ieee80211_scan_state *);
203 static int iwi_auth_and_assoc(struct iwi_softc *, struct ieee80211vap *);
204 static void iwi_disassoc(void *, int);
205 static int iwi_disassociate(struct iwi_softc *, int quiet);
206 static void iwi_init_locked(struct iwi_softc *);
207 static void iwi_init(void *);
208 static int iwi_init_fw_dma(struct iwi_softc *, int);
209 static void iwi_stop_locked(void *);
210 static void iwi_stop(struct iwi_softc *);
211 static void iwi_restart(void *, int);
212 static int iwi_getrfkill(struct iwi_softc *);
213 static void iwi_radio_on(void *, int);
214 static void iwi_radio_off(void *, int);
215 static void iwi_sysctlattach(struct iwi_softc *);
216 static void iwi_led_event(struct iwi_softc *, int);
217 static void iwi_ledattach(struct iwi_softc *);
218 static void iwi_collect_bands(struct ieee80211com *, uint8_t [], size_t);
219 static void iwi_getradiocaps(struct ieee80211com *, int, int *,
220 struct ieee80211_channel []);
222 static int iwi_probe(device_t);
223 static int iwi_attach(device_t);
224 static int iwi_detach(device_t);
225 static int iwi_shutdown(device_t);
226 static int iwi_suspend(device_t);
227 static int iwi_resume(device_t);
229 static device_method_t iwi_methods[] = {
230 /* Device interface */
231 DEVMETHOD(device_probe, iwi_probe),
232 DEVMETHOD(device_attach, iwi_attach),
233 DEVMETHOD(device_detach, iwi_detach),
234 DEVMETHOD(device_shutdown, iwi_shutdown),
235 DEVMETHOD(device_suspend, iwi_suspend),
236 DEVMETHOD(device_resume, iwi_resume),
241 static driver_t iwi_driver = {
244 sizeof (struct iwi_softc)
247 static devclass_t iwi_devclass;
249 DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, NULL, NULL);
251 MODULE_VERSION(iwi, 1);
253 static __inline uint8_t
254 MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
256 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
257 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
260 static __inline uint32_t
261 MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
263 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
264 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
268 iwi_probe(device_t dev)
270 const struct iwi_ident *ident;
272 for (ident = iwi_ident_table; ident->name != NULL; ident++) {
273 if (pci_get_vendor(dev) == ident->vendor &&
274 pci_get_device(dev) == ident->device) {
275 device_set_desc(dev, ident->name);
276 return (BUS_PROBE_DEFAULT);
283 iwi_attach(device_t dev)
285 struct iwi_softc *sc = device_get_softc(dev);
286 struct ieee80211com *ic = &sc->sc_ic;
291 sc->sc_ledevent = ticks;
294 mbufq_init(&sc->sc_snd, ifqmaxlen);
296 sc->sc_unr = new_unrhdr(1, IWI_MAX_IBSSNODE-1, &sc->sc_mtx);
298 TASK_INIT(&sc->sc_radiontask, 0, iwi_radio_on, sc);
299 TASK_INIT(&sc->sc_radiofftask, 0, iwi_radio_off, sc);
300 TASK_INIT(&sc->sc_restarttask, 0, iwi_restart, sc);
301 TASK_INIT(&sc->sc_disassoctask, 0, iwi_disassoc, sc);
302 TASK_INIT(&sc->sc_monitortask, 0, iwi_monitor_scan, sc);
304 callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
305 callout_init_mtx(&sc->sc_rftimer, &sc->sc_mtx, 0);
307 pci_write_config(dev, 0x41, 0, 1);
309 /* enable bus-mastering */
310 pci_enable_busmaster(dev);
313 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE);
314 if (sc->mem == NULL) {
315 device_printf(dev, "could not allocate memory resource\n");
319 sc->sc_st = rman_get_bustag(sc->mem);
320 sc->sc_sh = rman_get_bushandle(sc->mem);
323 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
324 RF_ACTIVE | RF_SHAREABLE);
325 if (sc->irq == NULL) {
326 device_printf(dev, "could not allocate interrupt resource\n");
330 if (iwi_reset(sc) != 0) {
331 device_printf(dev, "could not reset adapter\n");
338 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
339 device_printf(dev, "could not allocate Cmd ring\n");
343 for (i = 0; i < 4; i++) {
344 error = iwi_alloc_tx_ring(sc, &sc->txq[i], IWI_TX_RING_COUNT,
345 IWI_CSR_TX1_RIDX + i * 4,
346 IWI_CSR_TX1_WIDX + i * 4);
348 device_printf(dev, "could not allocate Tx ring %d\n",
354 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
355 device_printf(dev, "could not allocate Rx ring\n");
362 ic->ic_name = device_get_nameunit(dev);
363 ic->ic_opmode = IEEE80211_M_STA;
364 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
366 /* set device capabilities */
368 IEEE80211_C_STA /* station mode supported */
369 | IEEE80211_C_IBSS /* IBSS mode supported */
370 | IEEE80211_C_MONITOR /* monitor mode supported */
371 | IEEE80211_C_PMGT /* power save supported */
372 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
373 | IEEE80211_C_WPA /* 802.11i */
374 | IEEE80211_C_WME /* 802.11e */
376 | IEEE80211_C_BGSCAN /* capable of bg scanning */
380 /* read MAC address from EEPROM */
381 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
382 ic->ic_macaddr[0] = val & 0xff;
383 ic->ic_macaddr[1] = val >> 8;
384 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
385 ic->ic_macaddr[2] = val & 0xff;
386 ic->ic_macaddr[3] = val >> 8;
387 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
388 ic->ic_macaddr[4] = val & 0xff;
389 ic->ic_macaddr[5] = val >> 8;
391 iwi_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
394 ieee80211_ifattach(ic);
395 /* override default methods */
396 ic->ic_node_alloc = iwi_node_alloc;
397 sc->sc_node_free = ic->ic_node_free;
398 ic->ic_node_free = iwi_node_free;
399 ic->ic_raw_xmit = iwi_raw_xmit;
400 ic->ic_scan_start = iwi_scan_start;
401 ic->ic_scan_end = iwi_scan_end;
402 ic->ic_set_channel = iwi_set_channel;
403 ic->ic_scan_curchan = iwi_scan_curchan;
404 ic->ic_scan_mindwell = iwi_scan_mindwell;
405 ic->ic_wme.wme_update = iwi_wme_update;
407 ic->ic_vap_create = iwi_vap_create;
408 ic->ic_vap_delete = iwi_vap_delete;
409 ic->ic_ioctl = iwi_ioctl;
410 ic->ic_transmit = iwi_transmit;
411 ic->ic_parent = iwi_parent;
412 ic->ic_getradiocaps = iwi_getradiocaps;
414 ieee80211_radiotap_attach(ic,
415 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
416 IWI_TX_RADIOTAP_PRESENT,
417 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
418 IWI_RX_RADIOTAP_PRESENT);
420 iwi_sysctlattach(sc);
424 * Hook our interrupt after all initialization is complete.
426 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
427 NULL, iwi_intr, sc, &sc->sc_ih);
429 device_printf(dev, "could not set up interrupt\n");
434 ieee80211_announce(ic);
444 iwi_detach(device_t dev)
446 struct iwi_softc *sc = device_get_softc(dev);
447 struct ieee80211com *ic = &sc->sc_ic;
449 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
451 /* NB: do early to drain any pending tasks */
452 ieee80211_draintask(ic, &sc->sc_radiontask);
453 ieee80211_draintask(ic, &sc->sc_radiofftask);
454 ieee80211_draintask(ic, &sc->sc_restarttask);
455 ieee80211_draintask(ic, &sc->sc_disassoctask);
456 ieee80211_draintask(ic, &sc->sc_monitortask);
460 ieee80211_ifdetach(ic);
462 iwi_put_firmware(sc);
463 iwi_release_fw_dma(sc);
465 iwi_free_cmd_ring(sc, &sc->cmdq);
466 iwi_free_tx_ring(sc, &sc->txq[0]);
467 iwi_free_tx_ring(sc, &sc->txq[1]);
468 iwi_free_tx_ring(sc, &sc->txq[2]);
469 iwi_free_tx_ring(sc, &sc->txq[3]);
470 iwi_free_rx_ring(sc, &sc->rxq);
472 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
474 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
477 delete_unrhdr(sc->sc_unr);
478 mbufq_drain(&sc->sc_snd);
480 IWI_LOCK_DESTROY(sc);
485 static struct ieee80211vap *
486 iwi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
487 enum ieee80211_opmode opmode, int flags,
488 const uint8_t bssid[IEEE80211_ADDR_LEN],
489 const uint8_t mac[IEEE80211_ADDR_LEN])
491 struct iwi_softc *sc = ic->ic_softc;
493 struct ieee80211vap *vap;
496 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
499 * Get firmware image (and possibly dma memory) on mode change.
501 if (iwi_get_firmware(sc, opmode))
503 /* allocate DMA memory for mapping firmware image */
505 if (sc->fw_boot.size > i)
506 i = sc->fw_boot.size;
507 /* XXX do we dma the ucode as well ? */
508 if (sc->fw_uc.size > i)
510 if (iwi_init_fw_dma(sc, i))
513 ivp = malloc(sizeof(struct iwi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
515 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
516 /* override the default, the setting comes from the linux driver */
517 vap->iv_bmissthreshold = 24;
518 /* override with driver methods */
519 ivp->iwi_newstate = vap->iv_newstate;
520 vap->iv_newstate = iwi_newstate;
523 ieee80211_vap_attach(vap, ieee80211_media_change, iwi_media_status,
525 ic->ic_opmode = opmode;
530 iwi_vap_delete(struct ieee80211vap *vap)
532 struct iwi_vap *ivp = IWI_VAP(vap);
534 ieee80211_vap_detach(vap);
535 free(ivp, M_80211_VAP);
539 iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
544 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
546 *(bus_addr_t *)arg = segs[0].ds_addr;
550 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count)
556 ring->cur = ring->next = 0;
558 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
559 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
560 count * IWI_CMD_DESC_SIZE, 1, count * IWI_CMD_DESC_SIZE, 0,
561 NULL, NULL, &ring->desc_dmat);
563 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
567 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
568 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
570 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
574 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
575 count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
577 device_printf(sc->sc_dev, "could not load desc DMA map\n");
583 fail: iwi_free_cmd_ring(sc, ring);
588 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
591 ring->cur = ring->next = 0;
595 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
597 if (ring->desc != NULL) {
598 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
599 BUS_DMASYNC_POSTWRITE);
600 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
601 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
604 if (ring->desc_dmat != NULL)
605 bus_dma_tag_destroy(ring->desc_dmat);
609 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count,
610 bus_addr_t csr_ridx, bus_addr_t csr_widx)
616 ring->cur = ring->next = 0;
617 ring->csr_ridx = csr_ridx;
618 ring->csr_widx = csr_widx;
620 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
621 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
622 count * IWI_TX_DESC_SIZE, 1, count * IWI_TX_DESC_SIZE, 0, NULL,
623 NULL, &ring->desc_dmat);
625 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
629 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
630 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
632 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
636 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
637 count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
639 device_printf(sc->sc_dev, "could not load desc DMA map\n");
643 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
645 if (ring->data == NULL) {
646 device_printf(sc->sc_dev, "could not allocate soft data\n");
651 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
652 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
653 IWI_MAX_NSEG, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
655 device_printf(sc->sc_dev, "could not create data DMA tag\n");
659 for (i = 0; i < count; i++) {
660 error = bus_dmamap_create(ring->data_dmat, 0,
663 device_printf(sc->sc_dev, "could not create DMA map\n");
670 fail: iwi_free_tx_ring(sc, ring);
675 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
677 struct iwi_tx_data *data;
680 for (i = 0; i < ring->count; i++) {
681 data = &ring->data[i];
683 if (data->m != NULL) {
684 bus_dmamap_sync(ring->data_dmat, data->map,
685 BUS_DMASYNC_POSTWRITE);
686 bus_dmamap_unload(ring->data_dmat, data->map);
691 if (data->ni != NULL) {
692 ieee80211_free_node(data->ni);
698 ring->cur = ring->next = 0;
702 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
704 struct iwi_tx_data *data;
707 if (ring->desc != NULL) {
708 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
709 BUS_DMASYNC_POSTWRITE);
710 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
711 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
714 if (ring->desc_dmat != NULL)
715 bus_dma_tag_destroy(ring->desc_dmat);
717 if (ring->data != NULL) {
718 for (i = 0; i < ring->count; i++) {
719 data = &ring->data[i];
721 if (data->m != NULL) {
722 bus_dmamap_sync(ring->data_dmat, data->map,
723 BUS_DMASYNC_POSTWRITE);
724 bus_dmamap_unload(ring->data_dmat, data->map);
728 if (data->ni != NULL)
729 ieee80211_free_node(data->ni);
731 if (data->map != NULL)
732 bus_dmamap_destroy(ring->data_dmat, data->map);
735 free(ring->data, M_DEVBUF);
738 if (ring->data_dmat != NULL)
739 bus_dma_tag_destroy(ring->data_dmat);
743 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
745 struct iwi_rx_data *data;
751 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
753 if (ring->data == NULL) {
754 device_printf(sc->sc_dev, "could not allocate soft data\n");
759 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
760 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
761 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
763 device_printf(sc->sc_dev, "could not create data DMA tag\n");
767 for (i = 0; i < count; i++) {
768 data = &ring->data[i];
770 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
772 device_printf(sc->sc_dev, "could not create DMA map\n");
776 data->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
777 if (data->m == NULL) {
778 device_printf(sc->sc_dev,
779 "could not allocate rx mbuf\n");
784 error = bus_dmamap_load(ring->data_dmat, data->map,
785 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
788 device_printf(sc->sc_dev,
789 "could not load rx buf DMA map");
793 data->reg = IWI_CSR_RX_BASE + i * 4;
798 fail: iwi_free_rx_ring(sc, ring);
803 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
809 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
811 struct iwi_rx_data *data;
814 if (ring->data != NULL) {
815 for (i = 0; i < ring->count; i++) {
816 data = &ring->data[i];
818 if (data->m != NULL) {
819 bus_dmamap_sync(ring->data_dmat, data->map,
820 BUS_DMASYNC_POSTREAD);
821 bus_dmamap_unload(ring->data_dmat, data->map);
825 if (data->map != NULL)
826 bus_dmamap_destroy(ring->data_dmat, data->map);
829 free(ring->data, M_DEVBUF);
832 if (ring->data_dmat != NULL)
833 bus_dma_tag_destroy(ring->data_dmat);
837 iwi_shutdown(device_t dev)
839 struct iwi_softc *sc = device_get_softc(dev);
842 iwi_put_firmware(sc); /* ??? XXX */
848 iwi_suspend(device_t dev)
850 struct iwi_softc *sc = device_get_softc(dev);
851 struct ieee80211com *ic = &sc->sc_ic;
853 ieee80211_suspend_all(ic);
858 iwi_resume(device_t dev)
860 struct iwi_softc *sc = device_get_softc(dev);
861 struct ieee80211com *ic = &sc->sc_ic;
863 pci_write_config(dev, 0x41, 0, 1);
865 ieee80211_resume_all(ic);
869 static struct ieee80211_node *
870 iwi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
874 in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
877 /* XXX assign sta table entry for adhoc */
884 iwi_node_free(struct ieee80211_node *ni)
886 struct ieee80211com *ic = ni->ni_ic;
887 struct iwi_softc *sc = ic->ic_softc;
888 struct iwi_node *in = (struct iwi_node *)ni;
890 if (in->in_station != -1) {
891 DPRINTF(("%s mac %6D station %u\n", __func__,
892 ni->ni_macaddr, ":", in->in_station));
893 free_unr(sc->sc_unr, in->in_station);
896 sc->sc_node_free(ni);
900 * Convert h/w rate code to IEEE rate code.
903 iwi_cvtrate(int iwirate)
906 case IWI_RATE_DS1: return 2;
907 case IWI_RATE_DS2: return 4;
908 case IWI_RATE_DS5: return 11;
909 case IWI_RATE_DS11: return 22;
910 case IWI_RATE_OFDM6: return 12;
911 case IWI_RATE_OFDM9: return 18;
912 case IWI_RATE_OFDM12: return 24;
913 case IWI_RATE_OFDM18: return 36;
914 case IWI_RATE_OFDM24: return 48;
915 case IWI_RATE_OFDM36: return 72;
916 case IWI_RATE_OFDM48: return 96;
917 case IWI_RATE_OFDM54: return 108;
923 * The firmware automatically adapts the transmit speed. We report its current
927 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
929 struct ieee80211vap *vap = ifp->if_softc;
930 struct ieee80211com *ic = vap->iv_ic;
931 struct iwi_softc *sc = ic->ic_softc;
932 struct ieee80211_node *ni;
934 /* read current transmission rate from adapter */
935 ni = ieee80211_ref_node(vap->iv_bss);
937 iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE));
938 ieee80211_free_node(ni);
939 ieee80211_media_status(ifp, imr);
943 iwi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
945 struct iwi_vap *ivp = IWI_VAP(vap);
946 struct ieee80211com *ic = vap->iv_ic;
947 struct iwi_softc *sc = ic->ic_softc;
950 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
951 ieee80211_state_name[vap->iv_state],
952 ieee80211_state_name[nstate], sc->flags));
954 IEEE80211_UNLOCK(ic);
957 case IEEE80211_S_INIT:
959 * NB: don't try to do this if iwi_stop_master has
960 * shutdown the firmware and disabled interrupts.
962 if (vap->iv_state == IEEE80211_S_RUN &&
963 (sc->flags & IWI_FLAG_FW_INITED))
964 iwi_disassociate(sc, 0);
966 case IEEE80211_S_AUTH:
967 iwi_auth_and_assoc(sc, vap);
969 case IEEE80211_S_RUN:
970 if (vap->iv_opmode == IEEE80211_M_IBSS &&
971 vap->iv_state == IEEE80211_S_SCAN) {
973 * XXX when joining an ibss network we are called
974 * with a SCAN -> RUN transition on scan complete.
975 * Use that to call iwi_auth_and_assoc. On completing
976 * the join we are then called again with an
977 * AUTH -> RUN transition and we want to do nothing.
978 * This is all totally bogus and needs to be redone.
980 iwi_auth_and_assoc(sc, vap);
981 } else if (vap->iv_opmode == IEEE80211_M_MONITOR)
982 ieee80211_runtask(ic, &sc->sc_monitortask);
984 case IEEE80211_S_ASSOC:
986 * If we are transitioning from AUTH then just wait
987 * for the ASSOC status to come back from the firmware.
988 * Otherwise we need to issue the association request.
990 if (vap->iv_state == IEEE80211_S_AUTH)
992 iwi_auth_and_assoc(sc, vap);
999 return ivp->iwi_newstate(vap, nstate, arg);
1003 * WME parameters coming from IEEE 802.11e specification. These values are
1004 * already declared in ieee80211_proto.c, but they are static so they can't
1007 static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
1008 { 0, 3, 5, 7, 0 }, /* WME_AC_BE */
1009 { 0, 3, 5, 10, 0 }, /* WME_AC_BK */
1010 { 0, 2, 4, 5, 188 }, /* WME_AC_VI */
1011 { 0, 2, 3, 4, 102 } /* WME_AC_VO */
1014 static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
1015 { 0, 3, 4, 6, 0 }, /* WME_AC_BE */
1016 { 0, 3, 4, 10, 0 }, /* WME_AC_BK */
1017 { 0, 2, 3, 4, 94 }, /* WME_AC_VI */
1018 { 0, 2, 2, 3, 47 } /* WME_AC_VO */
1020 #define IWI_EXP2(v) htole16((1 << (v)) - 1)
1021 #define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
1024 iwi_wme_init(struct iwi_softc *sc)
1026 const struct wmeParams *wmep;
1029 memset(sc->wme, 0, sizeof sc->wme);
1030 for (ac = 0; ac < WME_NUM_AC; ac++) {
1031 /* set WME values for CCK modulation */
1032 wmep = &iwi_wme_cck_params[ac];
1033 sc->wme[1].aifsn[ac] = wmep->wmep_aifsn;
1034 sc->wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1035 sc->wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1036 sc->wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1037 sc->wme[1].acm[ac] = wmep->wmep_acm;
1039 /* set WME values for OFDM modulation */
1040 wmep = &iwi_wme_ofdm_params[ac];
1041 sc->wme[2].aifsn[ac] = wmep->wmep_aifsn;
1042 sc->wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1043 sc->wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1044 sc->wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1045 sc->wme[2].acm[ac] = wmep->wmep_acm;
1050 iwi_wme_setparams(struct iwi_softc *sc)
1052 struct ieee80211com *ic = &sc->sc_ic;
1053 struct chanAccParams chp;
1054 const struct wmeParams *wmep;
1057 ieee80211_wme_ic_getparams(ic, &chp);
1059 for (ac = 0; ac < WME_NUM_AC; ac++) {
1060 /* set WME values for current operating mode */
1061 wmep = &chp.cap_wmeParams[ac];
1062 sc->wme[0].aifsn[ac] = wmep->wmep_aifsn;
1063 sc->wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1064 sc->wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1065 sc->wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1066 sc->wme[0].acm[ac] = wmep->wmep_acm;
1069 DPRINTF(("Setting WME parameters\n"));
1070 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, sc->wme, sizeof sc->wme);
1076 iwi_wme_update(struct ieee80211com *ic)
1078 struct iwi_softc *sc = ic->ic_softc;
1079 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1083 * We may be called to update the WME parameters in
1084 * the adapter at various places. If we're already
1085 * associated then initiate the request immediately;
1086 * otherwise we assume the params will get sent down
1087 * to the adapter as part of the work iwi_auth_and_assoc
1090 if (vap->iv_state == IEEE80211_S_RUN) {
1092 iwi_wme_setparams(sc);
1099 iwi_wme_setie(struct iwi_softc *sc)
1101 struct ieee80211_wme_info wme;
1103 memset(&wme, 0, sizeof wme);
1104 wme.wme_id = IEEE80211_ELEMID_VENDOR;
1105 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
1106 wme.wme_oui[0] = 0x00;
1107 wme.wme_oui[1] = 0x50;
1108 wme.wme_oui[2] = 0xf2;
1109 wme.wme_type = WME_OUI_TYPE;
1110 wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
1111 wme.wme_version = WME_VERSION;
1114 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
1115 return iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme);
1119 * Read 16 bits at address 'addr' from the serial EEPROM.
1122 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1128 /* clock C once before the first command */
1129 IWI_EEPROM_CTL(sc, 0);
1130 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1131 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1132 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1134 /* write start bit (1) */
1135 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1136 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1138 /* write READ opcode (10) */
1139 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1140 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1141 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1142 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1144 /* write address A7-A0 */
1145 for (n = 7; n >= 0; n--) {
1146 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1147 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1148 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1149 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1152 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1154 /* read data Q15-Q0 */
1156 for (n = 15; n >= 0; n--) {
1157 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1158 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1159 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1160 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1163 IWI_EEPROM_CTL(sc, 0);
1165 /* clear Chip Select and clock C */
1166 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1167 IWI_EEPROM_CTL(sc, 0);
1168 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1174 iwi_setcurchan(struct iwi_softc *sc, int chan)
1176 struct ieee80211com *ic = &sc->sc_ic;
1179 ieee80211_radiotap_chan_change(ic);
1183 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1184 struct iwi_frame *frame)
1186 struct ieee80211com *ic = &sc->sc_ic;
1187 struct mbuf *mnew, *m;
1188 struct ieee80211_node *ni;
1189 int type, error, framelen;
1193 framelen = le16toh(frame->len);
1194 if (framelen < IEEE80211_MIN_LEN || framelen > MCLBYTES) {
1196 * XXX >MCLBYTES is bogus as it means the h/w dma'd
1197 * out of bounds; need to figure out how to limit
1198 * frame size in the firmware
1202 ("drop rx frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1203 le16toh(frame->len), frame->chan, frame->rssi,
1208 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1209 le16toh(frame->len), frame->chan, frame->rssi, frame->rssi_dbm));
1211 if (frame->chan != sc->curchan)
1212 iwi_setcurchan(sc, frame->chan);
1215 * Try to allocate a new mbuf for this ring element and load it before
1216 * processing the current mbuf. If the ring element cannot be loaded,
1217 * drop the received packet and reuse the old mbuf. In the unlikely
1218 * case that the old mbuf can't be reloaded either, explicitly panic.
1220 mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1222 counter_u64_add(ic->ic_ierrors, 1);
1226 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1228 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1229 mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr,
1234 /* try to reload the old mbuf */
1235 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1236 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
1237 &data->physaddr, 0);
1239 /* very unlikely that it will fail... */
1240 panic("%s: could not load old rx mbuf",
1241 device_get_name(sc->sc_dev));
1243 counter_u64_add(ic->ic_ierrors, 1);
1248 * New mbuf successfully loaded, update Rx ring and continue
1253 CSR_WRITE_4(sc, data->reg, data->physaddr);
1256 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1257 sizeof (struct iwi_frame) + framelen;
1259 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1261 rssi = frame->rssi_dbm;
1263 if (ieee80211_radiotap_active(ic)) {
1264 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1267 tap->wr_antsignal = rssi;
1268 tap->wr_antnoise = nf;
1269 tap->wr_rate = iwi_cvtrate(frame->rate);
1270 tap->wr_antenna = frame->antenna;
1274 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1276 type = ieee80211_input(ni, m, rssi, nf);
1277 ieee80211_free_node(ni);
1279 type = ieee80211_input_all(ic, m, rssi, nf);
1282 if (sc->sc_softled) {
1284 * Blink for any data frame. Otherwise do a
1285 * heartbeat-style blink when idle. The latter
1286 * is mainly for station mode where we depend on
1287 * periodic beacon frames to trigger the poll event.
1289 if (type == IEEE80211_FC0_TYPE_DATA) {
1290 sc->sc_rxrate = frame->rate;
1291 iwi_led_event(sc, IWI_LED_RX);
1292 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
1293 iwi_led_event(sc, IWI_LED_POLL);
1298 * Check for an association response frame to see if QoS
1299 * has been negotiated. We parse just enough to figure
1300 * out if we're supposed to use QoS. The proper solution
1301 * is to pass the frame up so ieee80211_input can do the
1302 * work but that's made hard by how things currently are
1303 * done in the driver.
1306 iwi_checkforqos(struct ieee80211vap *vap,
1307 const struct ieee80211_frame *wh, int len)
1309 #define SUBTYPE(wh) ((wh)->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
1310 const uint8_t *frm, *efrm, *wme;
1311 struct ieee80211_node *ni;
1312 uint16_t capinfo, status, associd;
1314 /* NB: +8 for capinfo, status, associd, and first ie */
1315 if (!(sizeof(*wh)+8 < len && len < IEEE80211_MAX_LEN) ||
1316 SUBTYPE(wh) != IEEE80211_FC0_SUBTYPE_ASSOC_RESP)
1319 * asresp frame format
1320 * [2] capability information
1322 * [2] association ID
1323 * [tlv] supported rates
1324 * [tlv] extended supported rates
1327 frm = (const uint8_t *)&wh[1];
1328 efrm = ((const uint8_t *) wh) + len;
1330 capinfo = le16toh(*(const uint16_t *)frm);
1332 status = le16toh(*(const uint16_t *)frm);
1334 associd = le16toh(*(const uint16_t *)frm);
1338 while (efrm - frm > 1) {
1339 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1] + 2, return);
1341 case IEEE80211_ELEMID_VENDOR:
1349 ni = ieee80211_ref_node(vap->iv_bss);
1350 ni->ni_capinfo = capinfo;
1351 ni->ni_associd = associd & 0x3fff;
1353 ni->ni_flags |= IEEE80211_NODE_QOS;
1355 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1356 ieee80211_free_node(ni);
1361 iwi_notif_link_quality(struct iwi_softc *sc, struct iwi_notif *notif)
1363 struct iwi_notif_link_quality *lq;
1366 len = le16toh(notif->len);
1368 DPRINTFN(5, ("Notification (%u) - len=%d, sizeof=%zu\n",
1371 sizeof(struct iwi_notif_link_quality)
1374 /* enforce length */
1375 if (len != sizeof(struct iwi_notif_link_quality)) {
1376 DPRINTFN(5, ("Notification: (%u) too short (%d)\n",
1382 lq = (struct iwi_notif_link_quality *)(notif + 1);
1383 memcpy(&sc->sc_linkqual, lq, sizeof(sc->sc_linkqual));
1384 sc->sc_linkqual_valid = 1;
1388 * Task queue callbacks for iwi_notification_intr used to avoid LOR's.
1392 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1394 struct ieee80211com *ic = &sc->sc_ic;
1395 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1396 struct iwi_notif_scan_channel *chan;
1397 struct iwi_notif_scan_complete *scan;
1398 struct iwi_notif_authentication *auth;
1399 struct iwi_notif_association *assoc;
1400 struct iwi_notif_beacon_state *beacon;
1402 switch (notif->type) {
1403 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1404 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1406 DPRINTFN(3, ("Scan of channel %u complete (%u)\n",
1407 ieee80211_ieee2mhz(chan->nchan, 0), chan->nchan));
1409 /* Reset the timer, the scan is still going */
1410 sc->sc_state_timer = 3;
1413 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1414 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1416 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1419 IWI_STATE_END(sc, IWI_FW_SCANNING);
1422 * Monitor mode works by doing a passive scan to set
1423 * the channel and enable rx. Because we don't want
1424 * to abort a scan lest the firmware crash we scan
1425 * for a short period of time and automatically restart
1426 * the scan when notified the sweep has completed.
1428 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
1429 ieee80211_runtask(ic, &sc->sc_monitortask);
1433 if (scan->status == IWI_SCAN_COMPLETED) {
1434 /* NB: don't need to defer, net80211 does it for us */
1435 ieee80211_scan_next(vap);
1439 case IWI_NOTIF_TYPE_AUTHENTICATION:
1440 auth = (struct iwi_notif_authentication *)(notif + 1);
1441 switch (auth->state) {
1442 case IWI_AUTH_SUCCESS:
1443 DPRINTFN(2, ("Authentication succeeeded\n"));
1444 ieee80211_new_state(vap, IEEE80211_S_ASSOC, -1);
1448 * These are delivered as an unsolicited deauth
1449 * (e.g. due to inactivity) or in response to an
1450 * associate request.
1452 sc->flags &= ~IWI_FLAG_ASSOCIATED;
1453 if (vap->iv_state != IEEE80211_S_RUN) {
1454 DPRINTFN(2, ("Authentication failed\n"));
1455 vap->iv_stats.is_rx_auth_fail++;
1456 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1458 DPRINTFN(2, ("Deauthenticated\n"));
1459 vap->iv_stats.is_rx_deauth++;
1461 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1463 case IWI_AUTH_SENT_1:
1464 case IWI_AUTH_RECV_2:
1465 case IWI_AUTH_SEQ1_PASS:
1467 case IWI_AUTH_SEQ1_FAIL:
1468 DPRINTFN(2, ("Initial authentication handshake failed; "
1469 "you probably need shared key\n"));
1470 vap->iv_stats.is_rx_auth_fail++;
1471 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1472 /* XXX retry shared key when in auto */
1475 device_printf(sc->sc_dev,
1476 "unknown authentication state %u\n", auth->state);
1481 case IWI_NOTIF_TYPE_ASSOCIATION:
1482 assoc = (struct iwi_notif_association *)(notif + 1);
1483 switch (assoc->state) {
1484 case IWI_AUTH_SUCCESS:
1485 /* re-association, do nothing */
1487 case IWI_ASSOC_SUCCESS:
1488 DPRINTFN(2, ("Association succeeded\n"));
1489 sc->flags |= IWI_FLAG_ASSOCIATED;
1490 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1491 iwi_checkforqos(vap,
1492 (const struct ieee80211_frame *)(assoc+1),
1493 le16toh(notif->len) - sizeof(*assoc) - 1);
1494 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
1496 case IWI_ASSOC_INIT:
1497 sc->flags &= ~IWI_FLAG_ASSOCIATED;
1498 switch (sc->fw_state) {
1499 case IWI_FW_ASSOCIATING:
1500 DPRINTFN(2, ("Association failed\n"));
1501 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1502 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1505 case IWI_FW_DISASSOCIATING:
1506 DPRINTFN(2, ("Dissassociated\n"));
1507 IWI_STATE_END(sc, IWI_FW_DISASSOCIATING);
1508 vap->iv_stats.is_rx_disassoc++;
1509 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1514 device_printf(sc->sc_dev,
1515 "unknown association state %u\n", assoc->state);
1520 case IWI_NOTIF_TYPE_BEACON:
1521 /* XXX check struct length */
1522 beacon = (struct iwi_notif_beacon_state *)(notif + 1);
1524 DPRINTFN(5, ("Beacon state (%u, %u)\n",
1525 beacon->state, le32toh(beacon->number)));
1527 if (beacon->state == IWI_BEACON_MISS) {
1529 * The firmware notifies us of every beacon miss
1530 * so we need to track the count against the
1531 * configured threshold before notifying the
1533 * XXX try to roam, drop assoc only on much higher count
1535 if (le32toh(beacon->number) >= vap->iv_bmissthreshold) {
1536 DPRINTF(("Beacon miss: %u >= %u\n",
1537 le32toh(beacon->number),
1538 vap->iv_bmissthreshold));
1539 vap->iv_stats.is_beacon_miss++;
1541 * It's pointless to notify the 802.11 layer
1542 * as it'll try to send a probe request (which
1543 * we'll discard) and then timeout and drop us
1544 * into scan state. Instead tell the firmware
1545 * to disassociate and then on completion we'll
1546 * kick the state machine to scan.
1548 ieee80211_runtask(ic, &sc->sc_disassoctask);
1553 case IWI_NOTIF_TYPE_CALIBRATION:
1554 case IWI_NOTIF_TYPE_NOISE:
1556 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1558 case IWI_NOTIF_TYPE_LINK_QUALITY:
1559 iwi_notif_link_quality(sc, notif);
1563 DPRINTF(("unknown notification type %u flags 0x%x len %u\n",
1564 notif->type, notif->flags, le16toh(notif->len)));
1570 iwi_rx_intr(struct iwi_softc *sc)
1572 struct iwi_rx_data *data;
1573 struct iwi_hdr *hdr;
1576 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1578 for (; sc->rxq.cur != hw;) {
1579 data = &sc->rxq.data[sc->rxq.cur];
1581 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1582 BUS_DMASYNC_POSTREAD);
1584 hdr = mtod(data->m, struct iwi_hdr *);
1586 switch (hdr->type) {
1587 case IWI_HDR_TYPE_FRAME:
1588 iwi_frame_intr(sc, data, sc->rxq.cur,
1589 (struct iwi_frame *)(hdr + 1));
1592 case IWI_HDR_TYPE_NOTIF:
1593 iwi_notification_intr(sc,
1594 (struct iwi_notif *)(hdr + 1));
1598 device_printf(sc->sc_dev, "unknown hdr type %u\n",
1602 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1604 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1607 /* tell the firmware what we have processed */
1608 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1609 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1613 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1615 struct iwi_tx_data *data;
1618 hw = CSR_READ_4(sc, txq->csr_ridx);
1620 while (txq->next != hw) {
1621 data = &txq->data[txq->next];
1622 DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1623 bus_dmamap_sync(txq->data_dmat, data->map,
1624 BUS_DMASYNC_POSTWRITE);
1625 bus_dmamap_unload(txq->data_dmat, data->map);
1626 ieee80211_tx_complete(data->ni, data->m, 0);
1630 txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1632 sc->sc_tx_timer = 0;
1634 iwi_led_event(sc, IWI_LED_TX);
1639 iwi_fatal_error_intr(struct iwi_softc *sc)
1641 struct ieee80211com *ic = &sc->sc_ic;
1642 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1644 device_printf(sc->sc_dev, "firmware error\n");
1646 ieee80211_cancel_scan(vap);
1647 ieee80211_runtask(ic, &sc->sc_restarttask);
1649 sc->flags &= ~IWI_FLAG_BUSY;
1650 sc->sc_busy_timer = 0;
1655 iwi_radio_off_intr(struct iwi_softc *sc)
1658 ieee80211_runtask(&sc->sc_ic, &sc->sc_radiofftask);
1664 struct iwi_softc *sc = arg;
1670 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) {
1675 /* acknowledge interrupts */
1676 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1678 if (r & IWI_INTR_FATAL_ERROR) {
1679 iwi_fatal_error_intr(sc);
1683 if (r & IWI_INTR_FW_INITED) {
1684 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1688 if (r & IWI_INTR_RADIO_OFF)
1689 iwi_radio_off_intr(sc);
1691 if (r & IWI_INTR_CMD_DONE) {
1692 sc->flags &= ~IWI_FLAG_BUSY;
1693 sc->sc_busy_timer = 0;
1697 if (r & IWI_INTR_TX1_DONE)
1698 iwi_tx_intr(sc, &sc->txq[0]);
1700 if (r & IWI_INTR_TX2_DONE)
1701 iwi_tx_intr(sc, &sc->txq[1]);
1703 if (r & IWI_INTR_TX3_DONE)
1704 iwi_tx_intr(sc, &sc->txq[2]);
1706 if (r & IWI_INTR_TX4_DONE)
1707 iwi_tx_intr(sc, &sc->txq[3]);
1709 if (r & IWI_INTR_RX_DONE)
1712 if (r & IWI_INTR_PARITY_ERROR) {
1713 /* XXX rate-limit */
1714 device_printf(sc->sc_dev, "parity error\n");
1721 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len)
1723 struct iwi_cmd_desc *desc;
1725 IWI_LOCK_ASSERT(sc);
1727 if (sc->flags & IWI_FLAG_BUSY) {
1728 device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n",
1732 sc->flags |= IWI_FLAG_BUSY;
1733 sc->sc_busy_timer = 2;
1735 desc = &sc->cmdq.desc[sc->cmdq.cur];
1737 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1738 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1741 memcpy(desc->data, data, len);
1743 bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map,
1744 BUS_DMASYNC_PREWRITE);
1746 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1749 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1750 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1752 return msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz);
1756 iwi_write_ibssnode(struct iwi_softc *sc,
1757 const u_int8_t addr[IEEE80211_ADDR_LEN], int entry)
1759 struct iwi_ibssnode node;
1761 /* write node information into NIC memory */
1762 memset(&node, 0, sizeof node);
1763 IEEE80211_ADDR_COPY(node.bssid, addr);
1765 DPRINTF(("%s mac %6D station %u\n", __func__, node.bssid, ":", entry));
1767 CSR_WRITE_REGION_1(sc,
1768 IWI_CSR_NODE_BASE + entry * sizeof node,
1769 (uint8_t *)&node, sizeof node);
1773 iwi_tx_start(struct iwi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1776 struct ieee80211vap *vap = ni->ni_vap;
1777 struct iwi_node *in = (struct iwi_node *)ni;
1778 const struct ieee80211_frame *wh;
1779 struct ieee80211_key *k;
1780 struct iwi_tx_ring *txq = &sc->txq[ac];
1781 struct iwi_tx_data *data;
1782 struct iwi_tx_desc *desc;
1784 bus_dma_segment_t segs[IWI_MAX_NSEG];
1785 int error, nsegs, hdrlen, i;
1786 int ismcast, flags, xflags, staid;
1788 IWI_LOCK_ASSERT(sc);
1789 wh = mtod(m0, const struct ieee80211_frame *);
1790 /* NB: only data frames use this path */
1791 hdrlen = ieee80211_hdrsize(wh);
1792 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1796 flags |= IWI_DATA_FLAG_NEED_ACK;
1797 if (vap->iv_flags & IEEE80211_F_SHPREAMBLE)
1798 flags |= IWI_DATA_FLAG_SHPREAMBLE;
1799 if (IEEE80211_QOS_HAS_SEQ(wh)) {
1800 xflags |= IWI_DATA_XFLAG_QOS;
1801 if (ieee80211_wme_vap_ac_is_noack(vap, ac))
1802 flags &= ~IWI_DATA_FLAG_NEED_ACK;
1806 * This is only used in IBSS mode where the firmware expect an index
1807 * in a h/w table instead of a destination address.
1809 if (vap->iv_opmode == IEEE80211_M_IBSS) {
1811 if (in->in_station == -1) {
1812 in->in_station = alloc_unr(sc->sc_unr);
1813 if (in->in_station == -1) {
1814 /* h/w table is full */
1815 if_inc_counter(ni->ni_vap->iv_ifp,
1816 IFCOUNTER_OERRORS, 1);
1818 ieee80211_free_node(ni);
1821 iwi_write_ibssnode(sc,
1822 ni->ni_macaddr, in->in_station);
1824 staid = in->in_station;
1827 * Multicast addresses have no associated node
1828 * so there will be no station entry. We reserve
1829 * entry 0 for one mcast address and use that.
1830 * If there are many being used this will be
1831 * expensive and we'll need to do a better job
1832 * but for now this handles the broadcast case.
1834 if (!IEEE80211_ADDR_EQ(wh->i_addr1, sc->sc_mcast)) {
1835 IEEE80211_ADDR_COPY(sc->sc_mcast, wh->i_addr1);
1836 iwi_write_ibssnode(sc, sc->sc_mcast, 0);
1843 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1844 k = ieee80211_crypto_encap(ni, m0);
1850 /* packet header may have moved, reset our local pointer */
1851 wh = mtod(m0, struct ieee80211_frame *);
1854 if (ieee80211_radiotap_active_vap(vap)) {
1855 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1859 ieee80211_radiotap_tx(vap, m0);
1862 data = &txq->data[txq->cur];
1863 desc = &txq->desc[txq->cur];
1865 /* save and trim IEEE802.11 header */
1866 m_copydata(m0, 0, hdrlen, (caddr_t)&desc->wh);
1869 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0, segs,
1871 if (error != 0 && error != EFBIG) {
1872 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1878 mnew = m_defrag(m0, M_NOWAIT);
1880 device_printf(sc->sc_dev,
1881 "could not defragment mbuf\n");
1887 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map,
1888 m0, segs, &nsegs, 0);
1890 device_printf(sc->sc_dev,
1891 "could not map mbuf (error %d)\n", error);
1900 desc->hdr.type = IWI_HDR_TYPE_DATA;
1901 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1902 desc->station = staid;
1903 desc->cmd = IWI_DATA_CMD_TX;
1904 desc->len = htole16(m0->m_pkthdr.len);
1905 desc->flags = flags;
1906 desc->xflags = xflags;
1909 if (vap->iv_flags & IEEE80211_F_PRIVACY)
1910 desc->wep_txkey = vap->iv_def_txkey;
1913 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1915 desc->nseg = htole32(nsegs);
1916 for (i = 0; i < nsegs; i++) {
1917 desc->seg_addr[i] = htole32(segs[i].ds_addr);
1918 desc->seg_len[i] = htole16(segs[i].ds_len);
1921 bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1922 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1924 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
1925 ac, txq->cur, le16toh(desc->len), nsegs));
1928 txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
1929 CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1935 iwi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1936 const struct ieee80211_bpf_params *params)
1938 /* no support; just discard */
1940 ieee80211_free_node(ni);
1945 iwi_transmit(struct ieee80211com *ic, struct mbuf *m)
1947 struct iwi_softc *sc = ic->ic_softc;
1952 if (!sc->sc_running) {
1956 error = mbufq_enqueue(&sc->sc_snd, m);
1967 iwi_start(struct iwi_softc *sc)
1970 struct ieee80211_node *ni;
1973 IWI_LOCK_ASSERT(sc);
1975 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1976 ac = M_WME_GETAC(m);
1977 if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) {
1978 /* there is no place left in this ring; tail drop */
1980 mbufq_prepend(&sc->sc_snd, m);
1983 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1984 if (iwi_tx_start(sc, m, ni, ac) != 0) {
1985 if_inc_counter(ni->ni_vap->iv_ifp,
1986 IFCOUNTER_OERRORS, 1);
1987 ieee80211_free_node(ni);
1990 sc->sc_tx_timer = 5;
1995 iwi_watchdog(void *arg)
1997 struct iwi_softc *sc = arg;
1998 struct ieee80211com *ic = &sc->sc_ic;
2000 IWI_LOCK_ASSERT(sc);
2002 if (sc->sc_tx_timer > 0) {
2003 if (--sc->sc_tx_timer == 0) {
2004 device_printf(sc->sc_dev, "device timeout\n");
2005 counter_u64_add(ic->ic_oerrors, 1);
2006 ieee80211_runtask(ic, &sc->sc_restarttask);
2009 if (sc->sc_state_timer > 0) {
2010 if (--sc->sc_state_timer == 0) {
2011 device_printf(sc->sc_dev,
2012 "firmware stuck in state %d, resetting\n",
2014 if (sc->fw_state == IWI_FW_SCANNING)
2015 ieee80211_cancel_scan(TAILQ_FIRST(&ic->ic_vaps));
2016 ieee80211_runtask(ic, &sc->sc_restarttask);
2017 sc->sc_state_timer = 3;
2020 if (sc->sc_busy_timer > 0) {
2021 if (--sc->sc_busy_timer == 0) {
2022 device_printf(sc->sc_dev,
2023 "firmware command timeout, resetting\n");
2024 ieee80211_runtask(ic, &sc->sc_restarttask);
2027 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
2031 iwi_parent(struct ieee80211com *ic)
2033 struct iwi_softc *sc = ic->ic_softc;
2038 if (ic->ic_nrunning > 0) {
2039 if (!sc->sc_running) {
2040 iwi_init_locked(sc);
2043 } else if (sc->sc_running)
2044 iwi_stop_locked(sc);
2047 ieee80211_start_all(ic);
2051 iwi_ioctl(struct ieee80211com *ic, u_long cmd, void *data)
2053 struct ifreq *ifr = data;
2054 struct iwi_softc *sc = ic->ic_softc;
2061 /* XXX validate permissions/memory/etc? */
2062 error = copyout(&sc->sc_linkqual, ifr_data_get_ptr(ifr),
2063 sizeof(struct iwi_notif_link_quality));
2066 memset(&sc->sc_linkqual, 0,
2067 sizeof(struct iwi_notif_link_quality));
2080 iwi_stop_master(struct iwi_softc *sc)
2085 /* disable interrupts */
2086 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
2088 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
2089 for (ntries = 0; ntries < 5; ntries++) {
2090 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2095 device_printf(sc->sc_dev, "timeout waiting for master\n");
2097 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2098 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
2100 sc->flags &= ~IWI_FLAG_FW_INITED;
2104 iwi_reset(struct iwi_softc *sc)
2109 iwi_stop_master(sc);
2111 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2112 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2114 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
2116 /* wait for clock stabilization */
2117 for (ntries = 0; ntries < 1000; ntries++) {
2118 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
2122 if (ntries == 1000) {
2123 device_printf(sc->sc_dev,
2124 "timeout waiting for clock stabilization\n");
2128 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2129 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET);
2133 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2134 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2136 /* clear NIC memory */
2137 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
2138 for (i = 0; i < 0xc000; i++)
2139 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2144 static const struct iwi_firmware_ohdr *
2145 iwi_setup_ofw(struct iwi_softc *sc, struct iwi_fw *fw)
2147 const struct firmware *fp = fw->fp;
2148 const struct iwi_firmware_ohdr *hdr;
2150 if (fp->datasize < sizeof (struct iwi_firmware_ohdr)) {
2151 device_printf(sc->sc_dev, "image '%s' too small\n", fp->name);
2154 hdr = (const struct iwi_firmware_ohdr *)fp->data;
2155 if ((IWI_FW_GET_MAJOR(le32toh(hdr->version)) != IWI_FW_REQ_MAJOR) ||
2156 (IWI_FW_GET_MINOR(le32toh(hdr->version)) != IWI_FW_REQ_MINOR)) {
2157 device_printf(sc->sc_dev, "version for '%s' %d.%d != %d.%d\n",
2158 fp->name, IWI_FW_GET_MAJOR(le32toh(hdr->version)),
2159 IWI_FW_GET_MINOR(le32toh(hdr->version)), IWI_FW_REQ_MAJOR,
2163 fw->data = ((const char *) fp->data) + sizeof(struct iwi_firmware_ohdr);
2164 fw->size = fp->datasize - sizeof(struct iwi_firmware_ohdr);
2165 fw->name = fp->name;
2169 static const struct iwi_firmware_ohdr *
2170 iwi_setup_oucode(struct iwi_softc *sc, struct iwi_fw *fw)
2172 const struct iwi_firmware_ohdr *hdr;
2174 hdr = iwi_setup_ofw(sc, fw);
2175 if (hdr != NULL && le32toh(hdr->mode) != IWI_FW_MODE_UCODE) {
2176 device_printf(sc->sc_dev, "%s is not a ucode image\n",
2184 iwi_getfw(struct iwi_fw *fw, const char *fwname,
2185 struct iwi_fw *uc, const char *ucname)
2188 fw->fp = firmware_get(fwname);
2189 /* NB: pre-3.0 ucode is packaged separately */
2190 if (uc->fp == NULL && fw->fp != NULL && fw->fp->version < 300)
2191 uc->fp = firmware_get(ucname);
2195 * Get the required firmware images if not already loaded.
2196 * Note that we hold firmware images so long as the device
2197 * is marked up in case we need to reload them on device init.
2198 * This is necessary because we re-init the device sometimes
2199 * from a context where we cannot read from the filesystem
2200 * (e.g. from the taskqueue thread when rfkill is re-enabled).
2201 * XXX return 0 on success, 1 on error.
2203 * NB: the order of get'ing and put'ing images here is
2204 * intentional to support handling firmware images bundled
2205 * by operating mode and/or all together in one file with
2206 * the boot firmware as "master".
2209 iwi_get_firmware(struct iwi_softc *sc, enum ieee80211_opmode opmode)
2211 const struct iwi_firmware_hdr *hdr;
2212 const struct firmware *fp;
2214 /* invalidate cached firmware on mode change */
2215 if (sc->fw_mode != opmode)
2216 iwi_put_firmware(sc);
2219 case IEEE80211_M_STA:
2220 iwi_getfw(&sc->fw_fw, "iwi_bss", &sc->fw_uc, "iwi_ucode_bss");
2222 case IEEE80211_M_IBSS:
2223 iwi_getfw(&sc->fw_fw, "iwi_ibss", &sc->fw_uc, "iwi_ucode_ibss");
2225 case IEEE80211_M_MONITOR:
2226 iwi_getfw(&sc->fw_fw, "iwi_monitor",
2227 &sc->fw_uc, "iwi_ucode_monitor");
2230 device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
2235 device_printf(sc->sc_dev, "could not load firmware\n");
2238 if (fp->version < 300) {
2240 * Firmware prior to 3.0 was packaged as separate
2241 * boot, firmware, and ucode images. Verify the
2242 * ucode image was read in, retrieve the boot image
2243 * if needed, and check version stamps for consistency.
2244 * The version stamps in the data are also checked
2245 * above; this is a bit paranoid but is a cheap
2246 * safeguard against mis-packaging.
2248 if (sc->fw_uc.fp == NULL) {
2249 device_printf(sc->sc_dev, "could not load ucode\n");
2252 if (sc->fw_boot.fp == NULL) {
2253 sc->fw_boot.fp = firmware_get("iwi_boot");
2254 if (sc->fw_boot.fp == NULL) {
2255 device_printf(sc->sc_dev,
2256 "could not load boot firmware\n");
2260 if (sc->fw_boot.fp->version != sc->fw_fw.fp->version ||
2261 sc->fw_boot.fp->version != sc->fw_uc.fp->version) {
2262 device_printf(sc->sc_dev,
2263 "firmware version mismatch: "
2264 "'%s' is %d, '%s' is %d, '%s' is %d\n",
2265 sc->fw_boot.fp->name, sc->fw_boot.fp->version,
2266 sc->fw_uc.fp->name, sc->fw_uc.fp->version,
2267 sc->fw_fw.fp->name, sc->fw_fw.fp->version
2272 * Check and setup each image.
2274 if (iwi_setup_oucode(sc, &sc->fw_uc) == NULL ||
2275 iwi_setup_ofw(sc, &sc->fw_boot) == NULL ||
2276 iwi_setup_ofw(sc, &sc->fw_fw) == NULL)
2280 * Check and setup combined image.
2282 if (fp->datasize < sizeof(struct iwi_firmware_hdr)) {
2283 device_printf(sc->sc_dev, "image '%s' too small\n",
2287 hdr = (const struct iwi_firmware_hdr *)fp->data;
2288 if (fp->datasize < sizeof(*hdr) + le32toh(hdr->bsize) + le32toh(hdr->usize)
2289 + le32toh(hdr->fsize)) {
2290 device_printf(sc->sc_dev, "image '%s' too small (2)\n",
2294 sc->fw_boot.data = ((const char *) fp->data) + sizeof(*hdr);
2295 sc->fw_boot.size = le32toh(hdr->bsize);
2296 sc->fw_boot.name = fp->name;
2297 sc->fw_uc.data = sc->fw_boot.data + sc->fw_boot.size;
2298 sc->fw_uc.size = le32toh(hdr->usize);
2299 sc->fw_uc.name = fp->name;
2300 sc->fw_fw.data = sc->fw_uc.data + sc->fw_uc.size;
2301 sc->fw_fw.size = le32toh(hdr->fsize);
2302 sc->fw_fw.name = fp->name;
2305 device_printf(sc->sc_dev, "boot %d ucode %d fw %d bytes\n",
2306 sc->fw_boot.size, sc->fw_uc.size, sc->fw_fw.size);
2309 sc->fw_mode = opmode;
2312 iwi_put_firmware(sc);
2317 iwi_put_fw(struct iwi_fw *fw)
2319 if (fw->fp != NULL) {
2320 firmware_put(fw->fp, FIRMWARE_UNLOAD);
2329 * Release any cached firmware images.
2332 iwi_put_firmware(struct iwi_softc *sc)
2334 iwi_put_fw(&sc->fw_uc);
2335 iwi_put_fw(&sc->fw_fw);
2336 iwi_put_fw(&sc->fw_boot);
2340 iwi_load_ucode(struct iwi_softc *sc, const struct iwi_fw *fw)
2344 const char *uc = fw->data;
2345 size_t size = fw->size;
2346 int i, ntries, error;
2348 IWI_LOCK_ASSERT(sc);
2350 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
2351 IWI_RST_STOP_MASTER);
2352 for (ntries = 0; ntries < 5; ntries++) {
2353 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2358 device_printf(sc->sc_dev, "timeout waiting for master\n");
2363 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2366 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2367 tmp &= ~IWI_RST_PRINCETON_RESET;
2368 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2371 MEM_WRITE_4(sc, 0x3000e0, 0);
2373 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 1);
2375 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 0);
2377 MEM_WRITE_1(sc, 0x200000, 0x00);
2378 MEM_WRITE_1(sc, 0x200000, 0x40);
2381 /* write microcode into adapter memory */
2382 for (w = (const uint16_t *)uc; size > 0; w++, size -= 2)
2383 MEM_WRITE_2(sc, 0x200010, htole16(*w));
2385 MEM_WRITE_1(sc, 0x200000, 0x00);
2386 MEM_WRITE_1(sc, 0x200000, 0x80);
2388 /* wait until we get an answer */
2389 for (ntries = 0; ntries < 100; ntries++) {
2390 if (MEM_READ_1(sc, 0x200000) & 1)
2394 if (ntries == 100) {
2395 device_printf(sc->sc_dev,
2396 "timeout waiting for ucode to initialize\n");
2401 /* read the answer or the firmware will not initialize properly */
2402 for (i = 0; i < 7; i++)
2403 MEM_READ_4(sc, 0x200004);
2405 MEM_WRITE_1(sc, 0x200000, 0x00);
2411 /* macro to handle unaligned little endian data in firmware image */
2412 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2415 iwi_load_firmware(struct iwi_softc *sc, const struct iwi_fw *fw)
2418 uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp;
2421 IWI_LOCK_ASSERT(sc);
2423 /* copy firmware image to DMA memory */
2424 memcpy(sc->fw_virtaddr, fw->data, fw->size);
2426 /* make sure the adapter will get up-to-date values */
2427 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_PREWRITE);
2429 /* tell the adapter where the command blocks are stored */
2430 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2433 * Store command blocks into adapter's internal memory using register
2434 * indirections. The adapter will read the firmware image through DMA
2435 * using information stored in command blocks.
2437 src = sc->fw_physaddr;
2438 p = sc->fw_virtaddr;
2440 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2443 dst = GETLE32(p); p += 4; src += 4;
2444 len = GETLE32(p); p += 4; src += 4;
2448 mlen = min(len, IWI_CB_MAXDATALEN);
2450 ctl = IWI_CB_DEFAULT_CTL | mlen;
2451 sum = ctl ^ src ^ dst;
2453 /* write a command block */
2454 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2455 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2456 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2457 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2465 /* write a fictive final command block (sentinel) */
2466 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2467 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2469 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2470 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
2471 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2473 /* tell the adapter to start processing command blocks */
2474 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2476 /* wait until the adapter reaches the sentinel */
2477 for (ntries = 0; ntries < 400; ntries++) {
2478 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2482 /* sync dma, just in case */
2483 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_POSTWRITE);
2484 if (ntries == 400) {
2485 device_printf(sc->sc_dev,
2486 "timeout processing command blocks for %s firmware\n",
2491 /* we're done with command blocks processing */
2492 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2494 /* allow interrupts so we know when the firmware is ready */
2495 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2497 /* tell the adapter to initialize the firmware */
2498 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2500 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2501 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
2503 /* wait at most one second for firmware initialization to complete */
2504 if ((error = msleep(sc, &sc->sc_mtx, 0, "iwiinit", hz)) != 0) {
2505 device_printf(sc->sc_dev, "timeout waiting for %s firmware "
2506 "initialization to complete\n", fw->name);
2513 iwi_setpowermode(struct iwi_softc *sc, struct ieee80211vap *vap)
2517 if (vap->iv_flags & IEEE80211_F_PMGTON) {
2518 /* XXX set more fine-grained operation */
2519 data = htole32(IWI_POWER_MODE_MAX);
2521 data = htole32(IWI_POWER_MODE_CAM);
2523 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2524 return iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data);
2528 iwi_setwepkeys(struct iwi_softc *sc, struct ieee80211vap *vap)
2530 struct iwi_wep_key wepkey;
2531 struct ieee80211_key *wk;
2534 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2535 wk = &vap->iv_nw_keys[i];
2537 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2539 wepkey.len = wk->wk_keylen;
2540 memset(wepkey.key, 0, sizeof wepkey.key);
2541 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2542 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2544 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2553 iwi_set_rateset(struct iwi_softc *sc, const struct ieee80211_rateset *net_rs,
2556 struct iwi_rateset rs;
2558 memset(&rs, 0, sizeof(rs));
2561 rs.nrates = net_rs->rs_nrates;
2562 if (rs.nrates > nitems(rs.rates)) {
2563 DPRINTF(("Truncating negotiated rate set from %u\n",
2565 rs.nrates = nitems(rs.rates);
2567 memcpy(rs.rates, net_rs->rs_rates, rs.nrates);
2568 DPRINTF(("Setting .11%c%s %s rates (%u)\n",
2569 mode == IWI_MODE_11A ? 'a' : 'b',
2570 mode == IWI_MODE_11G ? "g" : "",
2571 type == IWI_RATESET_TYPE_SUPPORTED ? "supported" : "negotiated",
2574 return (iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof(rs)));
2578 iwi_config(struct iwi_softc *sc)
2580 struct ieee80211com *ic = &sc->sc_ic;
2581 struct iwi_configuration config;
2582 struct iwi_txpower power;
2586 IWI_LOCK_ASSERT(sc);
2588 DPRINTF(("Setting MAC address to %6D\n", ic->ic_macaddr, ":"));
2589 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_macaddr,
2590 IEEE80211_ADDR_LEN);
2594 memset(&config, 0, sizeof config);
2595 config.bluetooth_coexistence = sc->bluetooth;
2596 config.silence_threshold = 0x1e;
2597 config.antenna = sc->antenna;
2598 config.multicast_enabled = 1;
2599 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2600 config.disable_unicast_decryption = 1;
2601 config.disable_multicast_decryption = 1;
2602 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2603 config.allow_invalid_frames = 1;
2604 config.allow_beacon_and_probe_resp = 1;
2605 config.allow_mgt = 1;
2607 DPRINTF(("Configuring adapter\n"));
2608 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2611 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2612 power.mode = IWI_MODE_11B;
2614 for (i = 0; i < 11; i++) {
2615 power.chan[i].chan = i + 1;
2616 power.chan[i].power = IWI_TXPOWER_MAX;
2618 DPRINTF(("Setting .11b channels tx power\n"));
2619 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2623 power.mode = IWI_MODE_11G;
2624 DPRINTF(("Setting .11g channels tx power\n"));
2625 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2630 error = iwi_set_rateset(sc, &ic->ic_sup_rates[IEEE80211_MODE_11G],
2631 IWI_MODE_11G, IWI_RATESET_TYPE_SUPPORTED);
2635 error = iwi_set_rateset(sc, &ic->ic_sup_rates[IEEE80211_MODE_11A],
2636 IWI_MODE_11A, IWI_RATESET_TYPE_SUPPORTED);
2640 data = htole32(arc4random());
2641 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2642 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data);
2646 /* enable adapter */
2647 DPRINTF(("Enabling adapter\n"));
2648 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0);
2651 static __inline void
2652 set_scan_type(struct iwi_scan_ext *scan, int ix, int scan_type)
2654 uint8_t *st = &scan->scan_type[ix / 2];
2656 *st = (*st & 0xf0) | ((scan_type & 0xf) << 0);
2658 *st = (*st & 0x0f) | ((scan_type & 0xf) << 4);
2662 scan_type(const struct ieee80211_scan_state *ss,
2663 const struct ieee80211_channel *chan)
2665 /* We can only set one essid for a directed scan */
2666 if (ss->ss_nssid != 0)
2667 return IWI_SCAN_TYPE_BDIRECTED;
2668 if ((ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
2669 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
2670 return IWI_SCAN_TYPE_BROADCAST;
2671 return IWI_SCAN_TYPE_PASSIVE;
2675 scan_band(const struct ieee80211_channel *c)
2677 return IEEE80211_IS_CHAN_5GHZ(c) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ;
2681 iwi_monitor_scan(void *arg, int npending)
2683 struct iwi_softc *sc = arg;
2687 (void) iwi_scanchan(sc, 2000, 0);
2692 * Start a scan on the current channel or all channels.
2695 iwi_scanchan(struct iwi_softc *sc, unsigned long maxdwell, int allchan)
2697 struct ieee80211com *ic = &sc->sc_ic;
2698 struct ieee80211_channel *chan;
2699 struct ieee80211_scan_state *ss;
2700 struct iwi_scan_ext scan;
2703 IWI_LOCK_ASSERT(sc);
2704 if (sc->fw_state == IWI_FW_SCANNING) {
2706 * This should not happen as we only trigger scan_next after
2709 DPRINTF(("%s: called too early - still scanning\n", __func__));
2712 IWI_STATE_BEGIN(sc, IWI_FW_SCANNING);
2716 memset(&scan, 0, sizeof scan);
2717 scan.full_scan_index = htole32(++sc->sc_scangen);
2718 scan.dwell_time[IWI_SCAN_TYPE_PASSIVE] = htole16(maxdwell);
2719 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
2721 * Use very short dwell times for when we send probe request
2722 * frames. Without this bg scans hang. Ideally this should
2723 * be handled with early-termination as done by net80211 but
2724 * that's not feasible (aborting a scan is problematic).
2726 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(30);
2727 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(30);
2729 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(maxdwell);
2730 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(maxdwell);
2733 /* We can only set one essid for a directed scan */
2734 if (ss->ss_nssid != 0) {
2735 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ss->ss_ssid[0].ssid,
2736 ss->ss_ssid[0].len);
2742 int i, next, band, b, bstart;
2744 * Convert scan list to run-length encoded channel list
2745 * the firmware requires (preserving the order setup by
2746 * net80211). The first entry in each run specifies the
2747 * band and the count of items in the run.
2749 next = 0; /* next open slot */
2750 bstart = 0; /* NB: not needed, silence compiler */
2751 band = -1; /* NB: impossible value */
2752 KASSERT(ss->ss_last > 0, ("no channels"));
2753 for (i = 0; i < ss->ss_last; i++) {
2754 chan = ss->ss_chans[i];
2755 b = scan_band(chan);
2758 scan.channels[bstart] =
2759 (next - bstart) | band;
2760 /* NB: this allocates a slot for the run-len */
2761 band = b, bstart = next++;
2763 if (next >= IWI_SCAN_CHANNELS) {
2764 DPRINTF(("truncating scan list\n"));
2767 scan.channels[next] = ieee80211_chan2ieee(ic, chan);
2768 set_scan_type(&scan, next, scan_type(ss, chan));
2771 scan.channels[bstart] = (next - bstart) | band;
2773 /* Scan the current channel only */
2774 chan = ic->ic_curchan;
2775 scan.channels[0] = 1 | scan_band(chan);
2776 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2777 set_scan_type(&scan, 1, scan_type(ss, chan));
2780 if (iwi_debug > 0) {
2781 static const char *scantype[8] =
2782 { "PSTOP", "PASV", "DIR", "BCAST", "BDIR", "5", "6", "7" };
2784 printf("Scan request: index %u dwell %d/%d/%d\n"
2785 , le32toh(scan.full_scan_index)
2786 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_PASSIVE])
2787 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BROADCAST])
2788 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED])
2792 int run = scan.channels[i];
2795 printf("Scan %d %s channels:", run & 0x3f,
2796 run & IWI_CHAN_2GHZ ? "2.4GHz" : "5GHz");
2797 for (run &= 0x3f, i++; run > 0; run--, i++) {
2798 uint8_t type = scan.scan_type[i/2];
2799 printf(" %u/%s", scan.channels[i],
2800 scantype[(i & 1 ? type : type>>4) & 7]);
2803 } while (i < IWI_SCAN_CHANNELS);
2807 return (iwi_cmd(sc, IWI_CMD_SCAN_EXT, &scan, sizeof scan));
2811 iwi_set_sensitivity(struct iwi_softc *sc, int8_t rssi_dbm)
2813 struct iwi_sensitivity sens;
2815 DPRINTF(("Setting sensitivity to %d\n", rssi_dbm));
2817 memset(&sens, 0, sizeof sens);
2818 sens.rssi = htole16(rssi_dbm);
2819 return iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &sens, sizeof sens);
2823 iwi_auth_and_assoc(struct iwi_softc *sc, struct ieee80211vap *vap)
2825 struct ieee80211com *ic = vap->iv_ic;
2826 struct ifnet *ifp = vap->iv_ifp;
2827 struct ieee80211_node *ni;
2828 struct iwi_configuration config;
2829 struct iwi_associate *assoc = &sc->assoc;
2834 IWI_LOCK_ASSERT(sc);
2836 ni = ieee80211_ref_node(vap->iv_bss);
2838 if (sc->flags & IWI_FLAG_ASSOCIATED) {
2839 DPRINTF(("Already associated\n"));
2843 IWI_STATE_BEGIN(sc, IWI_FW_ASSOCIATING);
2847 if (IEEE80211_IS_CHAN_A(ic->ic_curchan))
2848 mode = IWI_MODE_11A;
2849 else if (IEEE80211_IS_CHAN_G(ic->ic_curchan))
2850 mode = IWI_MODE_11G;
2851 if (IEEE80211_IS_CHAN_B(ic->ic_curchan))
2852 mode = IWI_MODE_11B;
2854 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2855 memset(&config, 0, sizeof config);
2856 config.bluetooth_coexistence = sc->bluetooth;
2857 config.antenna = sc->antenna;
2858 config.multicast_enabled = 1;
2859 if (mode == IWI_MODE_11G)
2860 config.use_protection = 1;
2861 config.answer_pbreq =
2862 (vap->iv_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2863 config.disable_unicast_decryption = 1;
2864 config.disable_multicast_decryption = 1;
2865 DPRINTF(("Configuring adapter\n"));
2866 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2872 if (iwi_debug > 0) {
2873 printf("Setting ESSID to ");
2874 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2878 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen);
2882 error = iwi_setpowermode(sc, vap);
2886 data = htole32(vap->iv_rtsthreshold);
2887 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2888 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2892 data = htole32(vap->iv_fragthreshold);
2893 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2894 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2898 /* the rate set has already been "negotiated" */
2899 error = iwi_set_rateset(sc, &ni->ni_rates, mode,
2900 IWI_RATESET_TYPE_NEGOTIATED);
2904 memset(assoc, 0, sizeof *assoc);
2906 if ((vap->iv_flags & IEEE80211_F_WME) && ni->ni_ies.wme_ie != NULL) {
2907 /* NB: don't treat WME setup as failure */
2908 if (iwi_wme_setparams(sc) == 0 && iwi_wme_setie(sc) == 0)
2909 assoc->policy |= htole16(IWI_POLICY_WME);
2910 /* XXX complain on failure? */
2913 if (vap->iv_appie_wpa != NULL) {
2914 struct ieee80211_appie *ie = vap->iv_appie_wpa;
2916 DPRINTF(("Setting optional IE (len=%u)\n", ie->ie_len));
2917 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ie->ie_data, ie->ie_len);
2922 error = iwi_set_sensitivity(sc, ic->ic_node_getrssi(ni));
2927 assoc->chan = ic->ic_curchan->ic_ieee;
2929 * NB: do not arrange for shared key auth w/o privacy
2930 * (i.e. a wep key); it causes a firmware error.
2932 if ((vap->iv_flags & IEEE80211_F_PRIVACY) &&
2933 ni->ni_authmode == IEEE80211_AUTH_SHARED) {
2934 assoc->auth = IWI_AUTH_SHARED;
2936 * It's possible to have privacy marked but no default
2937 * key setup. This typically is due to a user app bug
2938 * but if we blindly grab the key the firmware will
2939 * barf so avoid it for now.
2941 if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE)
2942 assoc->auth |= vap->iv_def_txkey << 4;
2944 error = iwi_setwepkeys(sc, vap);
2948 if (vap->iv_flags & IEEE80211_F_WPA)
2949 assoc->policy |= htole16(IWI_POLICY_WPA);
2950 if (vap->iv_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0)
2951 assoc->type = IWI_HC_IBSS_START;
2953 assoc->type = IWI_HC_ASSOC;
2954 memcpy(assoc->tstamp, ni->ni_tstamp.data, 8);
2956 if (vap->iv_opmode == IEEE80211_M_IBSS)
2957 capinfo = IEEE80211_CAPINFO_IBSS;
2959 capinfo = IEEE80211_CAPINFO_ESS;
2960 if (vap->iv_flags & IEEE80211_F_PRIVACY)
2961 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2962 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2963 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2964 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2965 if (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
2966 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2967 assoc->capinfo = htole16(capinfo);
2969 assoc->lintval = htole16(ic->ic_lintval);
2970 assoc->intval = htole16(ni->ni_intval);
2971 IEEE80211_ADDR_COPY(assoc->bssid, ni->ni_bssid);
2972 if (vap->iv_opmode == IEEE80211_M_IBSS)
2973 IEEE80211_ADDR_COPY(assoc->dst, ifp->if_broadcastaddr);
2975 IEEE80211_ADDR_COPY(assoc->dst, ni->ni_bssid);
2977 DPRINTF(("%s bssid %6D dst %6D channel %u policy 0x%x "
2978 "auth %u capinfo 0x%x lintval %u bintval %u\n",
2979 assoc->type == IWI_HC_IBSS_START ? "Start" : "Join",
2980 assoc->bssid, ":", assoc->dst, ":",
2981 assoc->chan, le16toh(assoc->policy), assoc->auth,
2982 le16toh(assoc->capinfo), le16toh(assoc->lintval),
2983 le16toh(assoc->intval)));
2984 error = iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
2986 ieee80211_free_node(ni);
2988 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
2994 iwi_disassoc(void *arg, int pending)
2996 struct iwi_softc *sc = arg;
3000 iwi_disassociate(sc, 0);
3005 iwi_disassociate(struct iwi_softc *sc, int quiet)
3007 struct iwi_associate *assoc = &sc->assoc;
3009 if ((sc->flags & IWI_FLAG_ASSOCIATED) == 0) {
3010 DPRINTF(("Not associated\n"));
3014 IWI_STATE_BEGIN(sc, IWI_FW_DISASSOCIATING);
3017 assoc->type = IWI_HC_DISASSOC_QUIET;
3019 assoc->type = IWI_HC_DISASSOC;
3021 DPRINTF(("Trying to disassociate from %6D channel %u\n",
3022 assoc->bssid, ":", assoc->chan));
3023 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
3027 * release dma resources for the firmware
3030 iwi_release_fw_dma(struct iwi_softc *sc)
3032 if (sc->fw_flags & IWI_FW_HAVE_PHY)
3033 bus_dmamap_unload(sc->fw_dmat, sc->fw_map);
3034 if (sc->fw_flags & IWI_FW_HAVE_MAP)
3035 bus_dmamem_free(sc->fw_dmat, sc->fw_virtaddr, sc->fw_map);
3036 if (sc->fw_flags & IWI_FW_HAVE_DMAT)
3037 bus_dma_tag_destroy(sc->fw_dmat);
3040 sc->fw_dma_size = 0;
3043 sc->fw_physaddr = 0;
3044 sc->fw_virtaddr = NULL;
3048 * allocate the dma descriptor for the firmware.
3049 * Return 0 on success, 1 on error.
3050 * Must be called unlocked, protected by IWI_FLAG_FW_LOADING.
3053 iwi_init_fw_dma(struct iwi_softc *sc, int size)
3055 if (sc->fw_dma_size >= size)
3057 if (bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
3058 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
3059 size, 1, size, 0, NULL, NULL, &sc->fw_dmat) != 0) {
3060 device_printf(sc->sc_dev,
3061 "could not create firmware DMA tag\n");
3064 sc->fw_flags |= IWI_FW_HAVE_DMAT;
3065 if (bus_dmamem_alloc(sc->fw_dmat, &sc->fw_virtaddr, 0,
3066 &sc->fw_map) != 0) {
3067 device_printf(sc->sc_dev,
3068 "could not allocate firmware DMA memory\n");
3071 sc->fw_flags |= IWI_FW_HAVE_MAP;
3072 if (bus_dmamap_load(sc->fw_dmat, sc->fw_map, sc->fw_virtaddr,
3073 size, iwi_dma_map_addr, &sc->fw_physaddr, 0) != 0) {
3074 device_printf(sc->sc_dev, "could not load firmware DMA map\n");
3077 sc->fw_flags |= IWI_FW_HAVE_PHY;
3078 sc->fw_dma_size = size;
3082 iwi_release_fw_dma(sc);
3087 iwi_init_locked(struct iwi_softc *sc)
3089 struct iwi_rx_data *data;
3092 IWI_LOCK_ASSERT(sc);
3094 if (sc->fw_state == IWI_FW_LOADING) {
3095 device_printf(sc->sc_dev, "%s: already loading\n", __func__);
3096 return; /* XXX: condvar? */
3099 iwi_stop_locked(sc);
3101 IWI_STATE_BEGIN(sc, IWI_FW_LOADING);
3103 if (iwi_reset(sc) != 0) {
3104 device_printf(sc->sc_dev, "could not reset adapter\n");
3107 if (iwi_load_firmware(sc, &sc->fw_boot) != 0) {
3108 device_printf(sc->sc_dev,
3109 "could not load boot firmware %s\n", sc->fw_boot.name);
3112 if (iwi_load_ucode(sc, &sc->fw_uc) != 0) {
3113 device_printf(sc->sc_dev,
3114 "could not load microcode %s\n", sc->fw_uc.name);
3118 iwi_stop_master(sc);
3120 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr);
3121 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
3122 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
3124 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr);
3125 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
3126 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
3128 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr);
3129 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
3130 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
3132 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr);
3133 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
3134 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
3136 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr);
3137 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
3138 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
3140 for (i = 0; i < sc->rxq.count; i++) {
3141 data = &sc->rxq.data[i];
3142 CSR_WRITE_4(sc, data->reg, data->physaddr);
3145 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1);
3147 if (iwi_load_firmware(sc, &sc->fw_fw) != 0) {
3148 device_printf(sc->sc_dev,
3149 "could not load main firmware %s\n", sc->fw_fw.name);
3152 sc->flags |= IWI_FLAG_FW_INITED;
3154 IWI_STATE_END(sc, IWI_FW_LOADING);
3156 if (iwi_config(sc) != 0) {
3157 device_printf(sc->sc_dev, "unable to enable adapter\n");
3161 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
3165 IWI_STATE_END(sc, IWI_FW_LOADING);
3167 iwi_stop_locked(sc);
3171 iwi_init(void *priv)
3173 struct iwi_softc *sc = priv;
3174 struct ieee80211com *ic = &sc->sc_ic;
3178 iwi_init_locked(sc);
3182 ieee80211_start_all(ic);
3186 iwi_stop_locked(void *priv)
3188 struct iwi_softc *sc = priv;
3190 IWI_LOCK_ASSERT(sc);
3194 if (sc->sc_softled) {
3195 callout_stop(&sc->sc_ledtimer);
3196 sc->sc_blinking = 0;
3198 callout_stop(&sc->sc_wdtimer);
3199 callout_stop(&sc->sc_rftimer);
3201 iwi_stop_master(sc);
3203 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET);
3206 iwi_reset_cmd_ring(sc, &sc->cmdq);
3207 iwi_reset_tx_ring(sc, &sc->txq[0]);
3208 iwi_reset_tx_ring(sc, &sc->txq[1]);
3209 iwi_reset_tx_ring(sc, &sc->txq[2]);
3210 iwi_reset_tx_ring(sc, &sc->txq[3]);
3211 iwi_reset_rx_ring(sc, &sc->rxq);
3213 sc->sc_tx_timer = 0;
3214 sc->sc_state_timer = 0;
3215 sc->sc_busy_timer = 0;
3216 sc->flags &= ~(IWI_FLAG_BUSY | IWI_FLAG_ASSOCIATED);
3217 sc->fw_state = IWI_FW_IDLE;
3222 iwi_stop(struct iwi_softc *sc)
3227 iwi_stop_locked(sc);
3232 iwi_restart(void *arg, int npending)
3234 struct iwi_softc *sc = arg;
3240 * Return whether or not the radio is enabled in hardware
3241 * (i.e. the rfkill switch is "off").
3244 iwi_getrfkill(struct iwi_softc *sc)
3246 return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0;
3250 iwi_radio_on(void *arg, int pending)
3252 struct iwi_softc *sc = arg;
3253 struct ieee80211com *ic = &sc->sc_ic;
3255 device_printf(sc->sc_dev, "radio turned on\n");
3258 ieee80211_notify_radio(ic, 1);
3262 iwi_rfkill_poll(void *arg)
3264 struct iwi_softc *sc = arg;
3266 IWI_LOCK_ASSERT(sc);
3269 * Check for a change in rfkill state. We get an
3270 * interrupt when a radio is disabled but not when
3271 * it is enabled so we must poll for the latter.
3273 if (!iwi_getrfkill(sc)) {
3274 ieee80211_runtask(&sc->sc_ic, &sc->sc_radiontask);
3277 callout_reset(&sc->sc_rftimer, 2*hz, iwi_rfkill_poll, sc);
3281 iwi_radio_off(void *arg, int pending)
3283 struct iwi_softc *sc = arg;
3284 struct ieee80211com *ic = &sc->sc_ic;
3287 device_printf(sc->sc_dev, "radio turned off\n");
3289 ieee80211_notify_radio(ic, 0);
3292 iwi_stop_locked(sc);
3293 iwi_rfkill_poll(sc);
3298 iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
3300 struct iwi_softc *sc = arg1;
3301 uint32_t size, buf[128];
3303 memset(buf, 0, sizeof buf);
3305 if (!(sc->flags & IWI_FLAG_FW_INITED))
3306 return SYSCTL_OUT(req, buf, sizeof buf);
3308 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
3309 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
3311 return SYSCTL_OUT(req, buf, size);
3315 iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
3317 struct iwi_softc *sc = arg1;
3318 int val = !iwi_getrfkill(sc);
3320 return SYSCTL_OUT(req, &val, sizeof val);
3327 iwi_sysctlattach(struct iwi_softc *sc)
3329 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3330 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3332 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "radio",
3333 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I",
3334 "radio transmitter switch state (0=off, 1=on)");
3336 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "stats",
3337 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S",
3341 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "bluetooth",
3342 CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence");
3344 sc->antenna = IWI_ANTENNA_AUTO;
3345 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "antenna",
3346 CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)");
3352 * Different cards have different capabilities. Some have three
3353 * led's while others have only one. The linux ipw driver defines
3354 * led's for link state (associated or not), band (11a, 11g, 11b),
3355 * and for link activity. We use one led and vary the blink rate
3356 * according to the tx/rx traffic a la the ath driver.
3359 static __inline uint32_t
3360 iwi_toggle_event(uint32_t r)
3362 return r &~ (IWI_RST_STANDBY | IWI_RST_GATE_ODMA |
3363 IWI_RST_GATE_IDMA | IWI_RST_GATE_ADMA);
3367 iwi_read_event(struct iwi_softc *sc)
3369 return MEM_READ_4(sc, IWI_MEM_EEPROM_EVENT);
3373 iwi_write_event(struct iwi_softc *sc, uint32_t v)
3375 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, v);
3379 iwi_led_done(void *arg)
3381 struct iwi_softc *sc = arg;
3383 sc->sc_blinking = 0;
3387 * Turn the activity LED off: flip the pin and then set a timer so no
3388 * update will happen for the specified duration.
3391 iwi_led_off(void *arg)
3393 struct iwi_softc *sc = arg;
3396 v = iwi_read_event(sc);
3397 v &= ~sc->sc_ledpin;
3398 iwi_write_event(sc, iwi_toggle_event(v));
3399 callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, iwi_led_done, sc);
3403 * Blink the LED according to the specified on/off times.
3406 iwi_led_blink(struct iwi_softc *sc, int on, int off)
3410 v = iwi_read_event(sc);
3412 iwi_write_event(sc, iwi_toggle_event(v));
3413 sc->sc_blinking = 1;
3414 sc->sc_ledoff = off;
3415 callout_reset(&sc->sc_ledtimer, on, iwi_led_off, sc);
3419 iwi_led_event(struct iwi_softc *sc, int event)
3421 /* NB: on/off times from the Atheros NDIS driver, w/ permission */
3422 static const struct {
3423 u_int rate; /* tx/rx iwi rate */
3424 u_int16_t timeOn; /* LED on time (ms) */
3425 u_int16_t timeOff; /* LED off time (ms) */
3427 { IWI_RATE_OFDM54, 40, 10 },
3428 { IWI_RATE_OFDM48, 44, 11 },
3429 { IWI_RATE_OFDM36, 50, 13 },
3430 { IWI_RATE_OFDM24, 57, 14 },
3431 { IWI_RATE_OFDM18, 67, 16 },
3432 { IWI_RATE_OFDM12, 80, 20 },
3433 { IWI_RATE_DS11, 100, 25 },
3434 { IWI_RATE_OFDM9, 133, 34 },
3435 { IWI_RATE_OFDM6, 160, 40 },
3436 { IWI_RATE_DS5, 200, 50 },
3437 { 6, 240, 58 }, /* XXX 3Mb/s if it existed */
3438 { IWI_RATE_DS2, 267, 66 },
3439 { IWI_RATE_DS1, 400, 100 },
3440 { 0, 500, 130 }, /* unknown rate/polling */
3443 int j = 0; /* XXX silence compiler */
3445 sc->sc_ledevent = ticks; /* time of last event */
3446 if (sc->sc_blinking) /* don't interrupt active blink */
3450 j = nitems(blinkrates)-1;
3453 /* read current transmission rate from adapter */
3454 txrate = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
3455 if (blinkrates[sc->sc_txrix].rate != txrate) {
3456 for (j = 0; j < nitems(blinkrates)-1; j++)
3457 if (blinkrates[j].rate == txrate)
3464 if (blinkrates[sc->sc_rxrix].rate != sc->sc_rxrate) {
3465 for (j = 0; j < nitems(blinkrates)-1; j++)
3466 if (blinkrates[j].rate == sc->sc_rxrate)
3473 /* XXX beware of overflow */
3474 iwi_led_blink(sc, (blinkrates[j].timeOn * hz) / 1000,
3475 (blinkrates[j].timeOff * hz) / 1000);
3479 iwi_sysctl_softled(SYSCTL_HANDLER_ARGS)
3481 struct iwi_softc *sc = arg1;
3482 int softled = sc->sc_softled;
3485 error = sysctl_handle_int(oidp, &softled, 0, req);
3486 if (error || !req->newptr)
3488 softled = (softled != 0);
3489 if (softled != sc->sc_softled) {
3491 uint32_t v = iwi_read_event(sc);
3492 v &= ~sc->sc_ledpin;
3493 iwi_write_event(sc, iwi_toggle_event(v));
3495 sc->sc_softled = softled;
3501 iwi_ledattach(struct iwi_softc *sc)
3503 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3504 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3506 sc->sc_blinking = 0;
3507 sc->sc_ledstate = 1;
3508 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */
3509 callout_init_mtx(&sc->sc_ledtimer, &sc->sc_mtx, 0);
3511 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3512 "softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
3513 iwi_sysctl_softled, "I", "enable/disable software LED support");
3514 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3515 "ledpin", CTLFLAG_RW, &sc->sc_ledpin, 0,
3516 "pin setting to turn activity LED on");
3517 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3518 "ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0,
3519 "idle time for inactivity LED (ticks)");
3520 /* XXX for debugging */
3521 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3522 "nictype", CTLFLAG_RD, &sc->sc_nictype, 0,
3523 "NIC type from EEPROM");
3525 sc->sc_ledpin = IWI_RST_LED_ACTIVITY;
3528 sc->sc_nictype = (iwi_read_prom_word(sc, IWI_EEPROM_NIC) >> 8) & 0xff;
3529 if (sc->sc_nictype == 1) {
3531 * NB: led's are reversed.
3533 sc->sc_ledpin = IWI_RST_LED_ASSOCIATED;
3538 iwi_scan_start(struct ieee80211com *ic)
3544 iwi_set_channel(struct ieee80211com *ic)
3546 struct iwi_softc *sc = ic->ic_softc;
3548 if (sc->fw_state == IWI_FW_IDLE)
3549 iwi_setcurchan(sc, ic->ic_curchan->ic_ieee);
3553 iwi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
3555 struct ieee80211vap *vap = ss->ss_vap;
3556 struct iwi_softc *sc = vap->iv_ic->ic_softc;
3560 if (iwi_scanchan(sc, maxdwell, 0))
3561 ieee80211_cancel_scan(vap);
3566 iwi_scan_mindwell(struct ieee80211_scan_state *ss)
3568 /* NB: don't try to abort scan; wait for firmware to finish */
3572 iwi_scan_end(struct ieee80211com *ic)
3574 struct iwi_softc *sc = ic->ic_softc;
3578 sc->flags &= ~IWI_FLAG_CHANNEL_SCAN;
3579 /* NB: make sure we're still scanning */
3580 if (sc->fw_state == IWI_FW_SCANNING)
3581 iwi_cmd(sc, IWI_CMD_ABORT_SCAN, NULL, 0);
3586 iwi_collect_bands(struct ieee80211com *ic, uint8_t bands[], size_t bands_sz)
3588 struct iwi_softc *sc = ic->ic_softc;
3589 device_t dev = sc->sc_dev;
3591 memset(bands, 0, bands_sz);
3592 setbit(bands, IEEE80211_MODE_11B);
3593 setbit(bands, IEEE80211_MODE_11G);
3594 if (pci_get_device(dev) >= 0x4223)
3595 setbit(bands, IEEE80211_MODE_11A);
3599 iwi_getradiocaps(struct ieee80211com *ic,
3600 int maxchans, int *nchans, struct ieee80211_channel chans[])
3602 uint8_t bands[IEEE80211_MODE_BYTES];
3604 iwi_collect_bands(ic, bands, sizeof(bands));
3606 if (isset(bands, IEEE80211_MODE_11B) || isset(bands, IEEE80211_MODE_11G))
3607 ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
3608 def_chan_2ghz, nitems(def_chan_2ghz), bands, 0);
3609 if (isset(bands, IEEE80211_MODE_11A)) {
3610 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
3611 def_chan_5ghz_band1, nitems(def_chan_5ghz_band1),
3613 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
3614 def_chan_5ghz_band2, nitems(def_chan_5ghz_band2),
3616 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
3617 def_chan_5ghz_band3, nitems(def_chan_5ghz_band3),