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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
38 #include <sys/param.h>
39 #include <sys/sysctl.h>
40 #include <sys/sockio.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/module.h>
50 #include <sys/endian.h>
52 #include <sys/mount.h>
53 #include <sys/namei.h>
54 #include <sys/linker.h>
55 #include <sys/firmware.h>
56 #include <sys/kthread.h>
57 #include <sys/taskqueue.h>
59 #include <machine/bus.h>
60 #include <machine/resource.h>
63 #include <dev/pci/pcireg.h>
64 #include <dev/pci/pcivar.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>
74 #include <net80211/ieee80211_var.h>
75 #include <net80211/ieee80211_radiotap.h>
76 #include <net80211/ieee80211_regdomain.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip.h>
82 #include <netinet/if_ether.h>
84 #include <dev/iwi/if_iwireg.h>
85 #include <dev/iwi/if_iwivar.h>
89 #define DPRINTF(x) do { if (iwi_debug > 0) printf x; } while (0)
90 #define DPRINTFN(n, x) do { if (iwi_debug >= (n)) printf x; } while (0)
92 SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level");
95 #define DPRINTFN(n, x)
98 MODULE_DEPEND(iwi, pci, 1, 1, 1);
99 MODULE_DEPEND(iwi, wlan, 1, 1, 1);
100 MODULE_DEPEND(iwi, firmware, 1, 1, 1);
114 static const struct iwi_ident iwi_ident_table[] = {
115 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" },
116 { 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" },
117 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" },
118 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" },
123 static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
124 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
126 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
127 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
128 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
129 int, bus_addr_t, bus_addr_t);
130 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
131 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
132 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
134 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
135 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
136 static struct ieee80211_node *iwi_node_alloc(struct ieee80211_node_table *);
137 static void iwi_node_free(struct ieee80211_node *);
138 static int iwi_media_change(struct ifnet *);
139 static void iwi_media_status(struct ifnet *, struct ifmediareq *);
140 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
141 static void iwi_wme_init(struct iwi_softc *);
142 static int iwi_wme_setparams(struct iwi_softc *);
143 static int iwi_wme_update(struct ieee80211com *);
144 static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t);
145 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
147 static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
148 static void iwi_rx_intr(struct iwi_softc *);
149 static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
150 static void iwi_intr(void *);
151 static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t);
152 static void iwi_write_ibssnode(struct iwi_softc *, const u_int8_t [], int);
153 static int iwi_tx_start(struct ifnet *, struct mbuf *,
154 struct ieee80211_node *, int);
155 static void iwi_start(struct ifnet *);
156 static void iwi_watchdog(void *);
157 static int iwi_ioctl(struct ifnet *, u_long, caddr_t);
158 static void iwi_stop_master(struct iwi_softc *);
159 static int iwi_reset(struct iwi_softc *);
160 static int iwi_load_ucode(struct iwi_softc *, const struct iwi_fw *);
161 static int iwi_load_firmware(struct iwi_softc *, const struct iwi_fw *);
162 static void iwi_release_fw_dma(struct iwi_softc *sc);
163 static int iwi_config(struct iwi_softc *);
164 static int iwi_get_firmware(struct iwi_softc *);
165 static void iwi_put_firmware(struct iwi_softc *);
166 static int iwi_scanchan(struct iwi_softc *, unsigned long, int);
167 static void iwi_scan_start(struct ieee80211com *);
168 static void iwi_scan_end(struct ieee80211com *);
169 static void iwi_scanabort(void *, int);
170 static void iwi_set_channel(struct ieee80211com *);
171 static void iwi_scan_curchan(struct ieee80211com *, unsigned long maxdwell);
173 static void iwi_scan_allchan(struct ieee80211com *, unsigned long maxdwell);
175 static void iwi_scan_mindwell(struct ieee80211com *);
176 static void iwi_assoc(struct ieee80211com *ic);
177 static void iwi_disassoc(struct ieee80211com *);
178 static void iwi_ops(void *, int);
179 static int iwi_queue_cmd(struct iwi_softc *, int);
180 static int iwi_auth_and_assoc(struct iwi_softc *);
181 static int iwi_disassociate(struct iwi_softc *, int quiet);
182 static void iwi_init(void *);
183 static void iwi_init_locked(void *, int);
184 static void iwi_stop(void *);
185 static void iwi_restart(void *, int);
186 static int iwi_getrfkill(struct iwi_softc *);
187 static void iwi_radio_on(void *, int);
188 static void iwi_radio_off(void *, int);
189 static void iwi_sysctlattach(struct iwi_softc *);
190 static void iwi_led_event(struct iwi_softc *, int);
191 static void iwi_ledattach(struct iwi_softc *);
193 static int iwi_probe(device_t);
194 static int iwi_attach(device_t);
195 static int iwi_detach(device_t);
196 static int iwi_shutdown(device_t);
197 static int iwi_suspend(device_t);
198 static int iwi_resume(device_t);
200 static device_method_t iwi_methods[] = {
201 /* Device interface */
202 DEVMETHOD(device_probe, iwi_probe),
203 DEVMETHOD(device_attach, iwi_attach),
204 DEVMETHOD(device_detach, iwi_detach),
205 DEVMETHOD(device_shutdown, iwi_shutdown),
206 DEVMETHOD(device_suspend, iwi_suspend),
207 DEVMETHOD(device_resume, iwi_resume),
212 static driver_t iwi_driver = {
215 sizeof (struct iwi_softc)
218 static devclass_t iwi_devclass;
220 DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, 0, 0);
222 static __inline uint8_t
223 MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
225 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
226 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
229 static __inline uint32_t
230 MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
232 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
233 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
237 iwi_probe(device_t dev)
239 const struct iwi_ident *ident;
241 for (ident = iwi_ident_table; ident->name != NULL; ident++) {
242 if (pci_get_vendor(dev) == ident->vendor &&
243 pci_get_device(dev) == ident->device) {
244 device_set_desc(dev, ident->name);
251 /* Base Address Register */
252 #define IWI_PCI_BAR0 0x10
255 iwi_attach(device_t dev)
257 struct iwi_softc *sc = device_get_softc(dev);
259 struct ieee80211com *ic = &sc->sc_ic;
266 IWI_CMD_LOCK_INIT(sc);
268 sc->sc_unr = new_unrhdr(1, IWI_MAX_IBSSNODE-1, &sc->sc_mtx);
270 #if __FreeBSD_version >= 700000
271 sc->sc_tq = taskqueue_create("iwi_taskq", M_NOWAIT | M_ZERO,
272 taskqueue_thread_enqueue, &sc->sc_tq);
273 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq",
274 device_get_nameunit(dev));
275 sc->sc_tq2 = taskqueue_create("iwi_taskq2", M_NOWAIT | M_ZERO,
276 taskqueue_thread_enqueue, &sc->sc_tq2);
277 taskqueue_start_threads(&sc->sc_tq2, 1, PI_NET, "%s taskq2",
278 device_get_nameunit(dev));
280 sc->sc_tq = taskqueue_create("iwi_taskq", M_NOWAIT | M_ZERO,
281 taskqueue_thread_enqueue, &sc->sc_tq, &sc->sc_tqproc);
282 kthread_create(taskqueue_thread_loop, &sc->sc_tq, &sc->sc_tqproc,
283 0, 0, "%s taskq", device_get_nameunit(dev));
284 sc->sc_tq2 = taskqueue_create("iwi_taskq2", M_NOWAIT | M_ZERO,
285 taskqueue_thread_enqueue, &sc->sc_tq2, &sc->sc_tqproc);
286 kthread_create(taskqueue_thread_loop, &sc->sc_tq2, &sc->sc_tqproc,
287 0, 0, "%s taskq2", device_get_nameunit(dev));
289 TASK_INIT(&sc->sc_radiontask, 0, iwi_radio_on, sc);
290 TASK_INIT(&sc->sc_radiofftask, 0, iwi_radio_off, sc);
291 TASK_INIT(&sc->sc_restarttask, 0, iwi_restart, sc);
292 TASK_INIT(&sc->sc_opstask, 0, iwi_ops, sc);
293 TASK_INIT(&sc->sc_scanaborttask, 0, iwi_scanabort, sc);
294 callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
296 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
297 device_printf(dev, "chip is in D%d power mode "
298 "-- setting to D0\n", pci_get_powerstate(dev));
299 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
302 pci_write_config(dev, 0x41, 0, 1);
304 /* enable bus-mastering */
305 pci_enable_busmaster(dev);
307 sc->mem_rid = IWI_PCI_BAR0;
308 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
310 if (sc->mem == NULL) {
311 device_printf(dev, "could not allocate memory resource\n");
315 sc->sc_st = rman_get_bustag(sc->mem);
316 sc->sc_sh = rman_get_bushandle(sc->mem);
319 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
320 RF_ACTIVE | RF_SHAREABLE);
321 if (sc->irq == NULL) {
322 device_printf(dev, "could not allocate interrupt resource\n");
326 if (iwi_reset(sc) != 0) {
327 device_printf(dev, "could not reset adapter\n");
334 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
335 device_printf(dev, "could not allocate Cmd ring\n");
339 for (i = 0; i < 4; i++) {
340 error = iwi_alloc_tx_ring(sc, &sc->txq[i], IWI_TX_RING_COUNT,
341 IWI_CSR_TX1_RIDX + i * 4,
342 IWI_CSR_TX1_WIDX + i * 4);
344 device_printf(dev, "could not allocate Tx ring %d\n",
350 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
351 device_printf(dev, "could not allocate Rx ring\n");
357 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
359 device_printf(dev, "can not if_alloc()\n");
364 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
365 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
366 ifp->if_init = iwi_init;
367 ifp->if_ioctl = iwi_ioctl;
368 ifp->if_start = iwi_start;
369 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
370 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
371 IFQ_SET_READY(&ifp->if_snd);
373 ic->ic_wme.wme_update = iwi_wme_update;
374 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
375 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
376 ic->ic_state = IEEE80211_S_INIT;
378 /* set device capabilities */
380 IEEE80211_C_IBSS /* IBSS mode supported */
381 | IEEE80211_C_MONITOR /* monitor mode supported */
382 | IEEE80211_C_PMGT /* power save supported */
383 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
384 | IEEE80211_C_WPA /* 802.11i */
385 | IEEE80211_C_WME /* 802.11e */
386 | IEEE80211_C_BGSCAN /* capable of bg scanning */
389 /* read MAC address from EEPROM */
390 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
391 ic->ic_myaddr[0] = val & 0xff;
392 ic->ic_myaddr[1] = val >> 8;
393 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
394 ic->ic_myaddr[2] = val & 0xff;
395 ic->ic_myaddr[3] = val >> 8;
396 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
397 ic->ic_myaddr[4] = val & 0xff;
398 ic->ic_myaddr[5] = val >> 8;
401 setbit(&bands, IEEE80211_MODE_11B);
402 setbit(&bands, IEEE80211_MODE_11G);
403 if (pci_get_device(dev) >= 0x4223)
404 setbit(&bands, IEEE80211_MODE_11A);
405 ieee80211_init_channels(ic, 0, CTRY_DEFAULT, bands, 0, 1);
407 ieee80211_ifattach(ic);
408 ic->ic_bmissthreshold = 10; /* override default */
409 /* override default methods */
410 ic->ic_node_alloc = iwi_node_alloc;
411 sc->sc_node_free = ic->ic_node_free;
412 ic->ic_node_free = iwi_node_free;
413 ic->ic_scan_start = iwi_scan_start;
414 ic->ic_scan_end = iwi_scan_end;
415 ic->ic_set_channel = iwi_set_channel;
416 ic->ic_scan_curchan = iwi_scan_curchan;
417 ic->ic_scan_mindwell = iwi_scan_mindwell;
419 /* override state transition machine */
420 sc->sc_newstate = ic->ic_newstate;
421 ic->ic_newstate = iwi_newstate;
422 ieee80211_media_init(ic, iwi_media_change, iwi_media_status);
424 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
425 sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap),
428 sc->sc_rxtap_len = sizeof sc->sc_rxtap;
429 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
430 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
432 sc->sc_txtap_len = sizeof sc->sc_txtap;
433 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
434 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
436 iwi_sysctlattach(sc);
440 * Hook our interrupt after all initialization is complete.
442 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
443 NULL, iwi_intr, sc, &sc->sc_ih);
445 device_printf(dev, "could not set up interrupt\n");
450 ieee80211_announce(ic);
454 fail: iwi_detach(dev);
459 iwi_detach(device_t dev)
461 struct iwi_softc *sc = device_get_softc(dev);
462 struct ieee80211com *ic = &sc->sc_ic;
463 struct ifnet *ifp = ic->ic_ifp;
471 ieee80211_ifdetach(ic);
474 callout_drain(&sc->sc_wdtimer);
475 iwi_put_firmware(sc);
476 iwi_release_fw_dma(sc);
478 iwi_free_cmd_ring(sc, &sc->cmdq);
479 iwi_free_tx_ring(sc, &sc->txq[0]);
480 iwi_free_tx_ring(sc, &sc->txq[1]);
481 iwi_free_tx_ring(sc, &sc->txq[2]);
482 iwi_free_tx_ring(sc, &sc->txq[3]);
483 iwi_free_rx_ring(sc, &sc->rxq);
485 if (sc->irq != NULL) {
486 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
487 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
491 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
496 taskqueue_free(sc->sc_tq);
497 taskqueue_free(sc->sc_tq2);
499 if (sc->sc_unr != NULL)
500 delete_unrhdr(sc->sc_unr);
502 IWI_LOCK_DESTROY(sc);
503 IWI_CMD_LOCK_DESTROY(sc);
509 iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
514 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
516 *(bus_addr_t *)arg = segs[0].ds_addr;
520 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count)
526 ring->cur = ring->next = 0;
528 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
529 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
530 count * IWI_CMD_DESC_SIZE, 1, count * IWI_CMD_DESC_SIZE, 0,
531 NULL, NULL, &ring->desc_dmat);
533 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
537 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
538 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
540 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
544 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
545 count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
547 device_printf(sc->sc_dev, "could not load desc DMA map\n");
553 fail: iwi_free_cmd_ring(sc, ring);
558 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
561 ring->cur = ring->next = 0;
565 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
567 if (ring->desc != NULL) {
568 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
569 BUS_DMASYNC_POSTWRITE);
570 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
571 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
574 if (ring->desc_dmat != NULL)
575 bus_dma_tag_destroy(ring->desc_dmat);
579 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count,
580 bus_addr_t csr_ridx, bus_addr_t csr_widx)
586 ring->cur = ring->next = 0;
587 ring->csr_ridx = csr_ridx;
588 ring->csr_widx = csr_widx;
590 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
591 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
592 count * IWI_TX_DESC_SIZE, 1, count * IWI_TX_DESC_SIZE, 0, NULL,
593 NULL, &ring->desc_dmat);
595 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
599 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
600 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
602 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
606 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
607 count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
609 device_printf(sc->sc_dev, "could not load desc DMA map\n");
613 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
615 if (ring->data == NULL) {
616 device_printf(sc->sc_dev, "could not allocate soft data\n");
621 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
622 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
623 IWI_MAX_NSEG, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
625 device_printf(sc->sc_dev, "could not create data DMA tag\n");
629 for (i = 0; i < count; i++) {
630 error = bus_dmamap_create(ring->data_dmat, 0,
633 device_printf(sc->sc_dev, "could not create DMA map\n");
640 fail: iwi_free_tx_ring(sc, ring);
645 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
647 struct iwi_tx_data *data;
650 for (i = 0; i < ring->count; i++) {
651 data = &ring->data[i];
653 if (data->m != NULL) {
654 bus_dmamap_sync(ring->data_dmat, data->map,
655 BUS_DMASYNC_POSTWRITE);
656 bus_dmamap_unload(ring->data_dmat, data->map);
661 if (data->ni != NULL) {
662 ieee80211_free_node(data->ni);
668 ring->cur = ring->next = 0;
672 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
674 struct iwi_tx_data *data;
677 if (ring->desc != NULL) {
678 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
679 BUS_DMASYNC_POSTWRITE);
680 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
681 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
684 if (ring->desc_dmat != NULL)
685 bus_dma_tag_destroy(ring->desc_dmat);
687 if (ring->data != NULL) {
688 for (i = 0; i < ring->count; i++) {
689 data = &ring->data[i];
691 if (data->m != NULL) {
692 bus_dmamap_sync(ring->data_dmat, data->map,
693 BUS_DMASYNC_POSTWRITE);
694 bus_dmamap_unload(ring->data_dmat, data->map);
698 if (data->ni != NULL)
699 ieee80211_free_node(data->ni);
701 if (data->map != NULL)
702 bus_dmamap_destroy(ring->data_dmat, data->map);
705 free(ring->data, M_DEVBUF);
708 if (ring->data_dmat != NULL)
709 bus_dma_tag_destroy(ring->data_dmat);
713 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
715 struct iwi_rx_data *data;
721 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
723 if (ring->data == NULL) {
724 device_printf(sc->sc_dev, "could not allocate soft data\n");
729 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
730 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
731 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
733 device_printf(sc->sc_dev, "could not create data DMA tag\n");
737 for (i = 0; i < count; i++) {
738 data = &ring->data[i];
740 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
742 device_printf(sc->sc_dev, "could not create DMA map\n");
746 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
747 if (data->m == NULL) {
748 device_printf(sc->sc_dev,
749 "could not allocate rx mbuf\n");
754 error = bus_dmamap_load(ring->data_dmat, data->map,
755 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
758 device_printf(sc->sc_dev,
759 "could not load rx buf DMA map");
763 data->reg = IWI_CSR_RX_BASE + i * 4;
768 fail: iwi_free_rx_ring(sc, ring);
773 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
779 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
781 struct iwi_rx_data *data;
784 if (ring->data != NULL) {
785 for (i = 0; i < ring->count; i++) {
786 data = &ring->data[i];
788 if (data->m != NULL) {
789 bus_dmamap_sync(ring->data_dmat, data->map,
790 BUS_DMASYNC_POSTREAD);
791 bus_dmamap_unload(ring->data_dmat, data->map);
795 if (data->map != NULL)
796 bus_dmamap_destroy(ring->data_dmat, data->map);
799 free(ring->data, M_DEVBUF);
802 if (ring->data_dmat != NULL)
803 bus_dma_tag_destroy(ring->data_dmat);
807 iwi_shutdown(device_t dev)
809 struct iwi_softc *sc = device_get_softc(dev);
815 iwi_put_firmware(sc); /* ??? XXX */
821 iwi_suspend(device_t dev)
823 struct iwi_softc *sc = device_get_softc(dev);
834 iwi_resume(device_t dev)
836 struct iwi_softc *sc = device_get_softc(dev);
837 struct ifnet *ifp = sc->sc_ic.ic_ifp;
842 pci_write_config(dev, 0x41, 0, 1);
844 if (ifp->if_flags & IFF_UP) {
845 ifp->if_init(ifp->if_softc);
846 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
855 static struct ieee80211_node *
856 iwi_node_alloc(struct ieee80211_node_table *nt)
860 in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
870 iwi_node_free(struct ieee80211_node *ni)
872 struct ieee80211com *ic = ni->ni_ic;
873 struct iwi_softc *sc = ic->ic_ifp->if_softc;
874 struct iwi_node *in = (struct iwi_node *)ni;
876 if (in->in_station != -1) {
877 DPRINTF(("%s mac %6D station %u\n", __func__,
878 ni->ni_macaddr, ":", in->in_station));
879 free_unr(sc->sc_unr, in->in_station);
882 sc->sc_node_free(ni);
886 iwi_media_change(struct ifnet *ifp)
888 struct iwi_softc *sc = ifp->if_softc;
894 error = ieee80211_media_change(ifp);
895 if (error == ENETRESET &&
896 (ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
897 iwi_init_locked(sc, 0);
905 * Convert h/w rate code to IEEE rate code.
908 iwi_cvtrate(int iwirate)
911 case IWI_RATE_DS1: return 2;
912 case IWI_RATE_DS2: return 4;
913 case IWI_RATE_DS5: return 11;
914 case IWI_RATE_DS11: return 22;
915 case IWI_RATE_OFDM6: return 12;
916 case IWI_RATE_OFDM9: return 18;
917 case IWI_RATE_OFDM12: return 24;
918 case IWI_RATE_OFDM18: return 36;
919 case IWI_RATE_OFDM24: return 48;
920 case IWI_RATE_OFDM36: return 72;
921 case IWI_RATE_OFDM48: return 96;
922 case IWI_RATE_OFDM54: return 108;
928 * The firmware automatically adapts the transmit speed. We report its current
932 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
934 struct iwi_softc *sc = ifp->if_softc;
935 struct ieee80211com *ic = &sc->sc_ic;
938 imr->ifm_status = IFM_AVALID;
939 imr->ifm_active = IFM_IEEE80211;
940 if (ic->ic_state == IEEE80211_S_RUN)
941 imr->ifm_status |= IFM_ACTIVE;
943 /* read current transmission rate from adapter */
944 rate = iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE));
945 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
947 if (ic->ic_opmode == IEEE80211_M_IBSS)
948 imr->ifm_active |= IFM_IEEE80211_ADHOC;
949 else if (ic->ic_opmode == IEEE80211_M_MONITOR)
950 imr->ifm_active |= IFM_IEEE80211_MONITOR;
954 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
956 struct ifnet *ifp = ic->ic_ifp;
957 struct iwi_softc *sc = ifp->if_softc;
960 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
961 ieee80211_state_name[ic->ic_state],
962 ieee80211_state_name[nstate], sc->flags));
964 /* XXX state change race with taskqueue */
966 case IEEE80211_S_AUTH:
969 case IEEE80211_S_RUN:
970 if (ic->ic_opmode == IEEE80211_M_IBSS) {
972 * XXX when joining an ibss network we are called
973 * with a SCAN -> RUN transition on scan complete.
974 * Use that to call iwi_auth_and_assoc. On completing
975 * the join we are then called again with an
976 * AUTH -> RUN transition and we want to do nothing.
977 * This is all totally bogus and needs to be redone.
979 if (ic->ic_state == IEEE80211_S_SCAN)
983 case IEEE80211_S_INIT:
985 * NB: don't try to do this if iwi_stop_master has
986 * shutdown the firmware and disabled interrupts.
988 if (ic->ic_state == IEEE80211_S_RUN &&
989 (sc->flags & IWI_FLAG_FW_INITED))
991 if (ic->ic_state == IEEE80211_S_SCAN &&
992 (sc->fw_state == IWI_FW_SCANNING))
993 ieee80211_cancel_scan(ic);
995 case IEEE80211_S_ASSOC:
997 * If we are not transitioning from AUTH the resend the
998 * association request.
1000 if (ic->ic_state != IEEE80211_S_AUTH)
1006 return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg);
1011 * WME parameters coming from IEEE 802.11e specification. These values are
1012 * already declared in ieee80211_proto.c, but they are static so they can't
1015 static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
1016 { 0, 3, 5, 7, 0 }, /* WME_AC_BE */
1017 { 0, 3, 5, 10, 0 }, /* WME_AC_BK */
1018 { 0, 2, 4, 5, 188 }, /* WME_AC_VI */
1019 { 0, 2, 3, 4, 102 } /* WME_AC_VO */
1022 static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
1023 { 0, 3, 4, 6, 0 }, /* WME_AC_BE */
1024 { 0, 3, 4, 10, 0 }, /* WME_AC_BK */
1025 { 0, 2, 3, 4, 94 }, /* WME_AC_VI */
1026 { 0, 2, 2, 3, 47 } /* WME_AC_VO */
1028 #define IWI_EXP2(v) htole16((1 << (v)) - 1)
1029 #define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
1032 iwi_wme_init(struct iwi_softc *sc)
1034 const struct wmeParams *wmep;
1037 memset(sc->wme, 0, sizeof sc->wme);
1038 for (ac = 0; ac < WME_NUM_AC; ac++) {
1039 /* set WME values for CCK modulation */
1040 wmep = &iwi_wme_cck_params[ac];
1041 sc->wme[1].aifsn[ac] = wmep->wmep_aifsn;
1042 sc->wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1043 sc->wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1044 sc->wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1045 sc->wme[1].acm[ac] = wmep->wmep_acm;
1047 /* set WME values for OFDM modulation */
1048 wmep = &iwi_wme_ofdm_params[ac];
1049 sc->wme[2].aifsn[ac] = wmep->wmep_aifsn;
1050 sc->wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1051 sc->wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1052 sc->wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1053 sc->wme[2].acm[ac] = wmep->wmep_acm;
1058 iwi_wme_setparams(struct iwi_softc *sc)
1060 struct ieee80211com *ic = &sc->sc_ic;
1061 const struct wmeParams *wmep;
1064 for (ac = 0; ac < WME_NUM_AC; ac++) {
1065 /* set WME values for current operating mode */
1066 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
1067 sc->wme[0].aifsn[ac] = wmep->wmep_aifsn;
1068 sc->wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1069 sc->wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1070 sc->wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1071 sc->wme[0].acm[ac] = wmep->wmep_acm;
1074 DPRINTF(("Setting WME parameters\n"));
1075 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, sc->wme, sizeof sc->wme);
1081 iwi_wme_update(struct ieee80211com *ic)
1083 struct iwi_softc *sc = ic->ic_ifp->if_softc;
1086 * We may be called to update the WME parameters in
1087 * the adapter at various places. If we're already
1088 * associated then initiate the request immediately
1089 * (via the taskqueue); otherwise we assume the params
1090 * will get sent down to the adapter as part of the
1091 * work iwi_auth_and_assoc does.
1093 return (iwi_queue_cmd(sc, IWI_SET_WME));
1097 iwi_wme_setie(struct iwi_softc *sc)
1099 struct ieee80211_wme_info wme;
1101 memset(&wme, 0, sizeof wme);
1102 wme.wme_id = IEEE80211_ELEMID_VENDOR;
1103 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
1104 wme.wme_oui[0] = 0x00;
1105 wme.wme_oui[1] = 0x50;
1106 wme.wme_oui[2] = 0xf2;
1107 wme.wme_type = WME_OUI_TYPE;
1108 wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
1109 wme.wme_version = WME_VERSION;
1112 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
1113 return iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme);
1117 * Read 16 bits at address 'addr' from the serial EEPROM.
1120 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1126 /* clock C once before the first command */
1127 IWI_EEPROM_CTL(sc, 0);
1128 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1129 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1130 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1132 /* write start bit (1) */
1133 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1134 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1136 /* write READ opcode (10) */
1137 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1138 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1139 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1140 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1142 /* write address A7-A0 */
1143 for (n = 7; n >= 0; n--) {
1144 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1145 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1146 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1147 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1150 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1152 /* read data Q15-Q0 */
1154 for (n = 15; n >= 0; n--) {
1155 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1156 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1157 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1158 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1161 IWI_EEPROM_CTL(sc, 0);
1163 /* clear Chip Select and clock C */
1164 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1165 IWI_EEPROM_CTL(sc, 0);
1166 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1172 iwi_setcurchan(struct iwi_softc *sc, int chan)
1174 struct ieee80211com *ic = &sc->sc_ic;
1178 sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
1179 htole16(ic->ic_curchan->ic_freq);
1180 sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
1181 htole16(ic->ic_curchan->ic_flags);
1185 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1186 struct iwi_frame *frame)
1188 struct ieee80211com *ic = &sc->sc_ic;
1189 struct ifnet *ifp = ic->ic_ifp;
1190 struct mbuf *mnew, *m;
1191 struct ieee80211_node *ni;
1192 int type, error, framelen;
1195 framelen = le16toh(frame->len);
1196 if (framelen < IEEE80211_MIN_LEN || framelen > MCLBYTES) {
1198 * XXX >MCLBYTES is bogus as it means the h/w dma'd
1199 * out of bounds; need to figure out how to limit
1200 * frame size in the firmware
1204 ("drop rx frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1205 le16toh(frame->len), frame->chan, frame->rssi,
1210 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1211 le16toh(frame->len), frame->chan, frame->rssi, frame->rssi_dbm));
1213 if (frame->chan != sc->curchan)
1214 iwi_setcurchan(sc, frame->chan);
1217 * Try to allocate a new mbuf for this ring element and load it before
1218 * processing the current mbuf. If the ring element cannot be loaded,
1219 * drop the received packet and reuse the old mbuf. In the unlikely
1220 * case that the old mbuf can't be reloaded either, explicitly panic.
1222 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1228 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1230 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1231 mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr,
1236 /* try to reload the old mbuf */
1237 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1238 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
1239 &data->physaddr, 0);
1241 /* very unlikely that it will fail... */
1242 panic("%s: could not load old rx mbuf",
1243 device_get_name(sc->sc_dev));
1250 * New mbuf successfully loaded, update Rx ring and continue
1255 CSR_WRITE_4(sc, data->reg, data->physaddr);
1258 m->m_pkthdr.rcvif = ifp;
1259 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1260 sizeof (struct iwi_frame) + framelen;
1262 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1264 if (bpf_peers_present(sc->sc_drvbpf)) {
1265 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1268 tap->wr_rate = iwi_cvtrate(frame->rate);
1269 tap->wr_antsignal = frame->signal;
1270 tap->wr_antenna = frame->antenna;
1272 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1276 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1278 /* send the frame to the 802.11 layer */
1279 type = ieee80211_input(ic, m, ni, frame->rssi_dbm, 0, 0);
1281 /* node is no longer needed */
1282 ieee80211_free_node(ni);
1285 if (sc->sc_softled) {
1287 * Blink for any data frame. Otherwise do a
1288 * heartbeat-style blink when idle. The latter
1289 * is mainly for station mode where we depend on
1290 * periodic beacon frames to trigger the poll event.
1292 if (type == IEEE80211_FC0_TYPE_DATA) {
1293 sc->sc_rxrate = frame->rate;
1294 iwi_led_event(sc, IWI_LED_RX);
1295 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
1296 iwi_led_event(sc, IWI_LED_POLL);
1300 /* unaligned little endian access */
1301 #define LE_READ_2(p) \
1303 ((((const u_int8_t *)(p))[0] ) | \
1304 (((const u_int8_t *)(p))[1] << 8)))
1305 #define LE_READ_4(p) \
1307 ((((const u_int8_t *)(p))[0] ) | \
1308 (((const u_int8_t *)(p))[1] << 8) | \
1309 (((const u_int8_t *)(p))[2] << 16) | \
1310 (((const u_int8_t *)(p))[3] << 24)))
1312 #define IEEE80211_VERIFY_LENGTH(_len, _minlen) do { \
1313 if ((_len) < (_minlen)) { \
1319 iswmeoui(const u_int8_t *frm)
1321 return frm[1] > 3 && LE_READ_4(frm+2) == ((WME_OUI_TYPE<<24)|WME_OUI);
1325 * Check for an association response frame to see if QoS
1326 * has been negotiated. We parse just enough to figure
1327 * out if we're supposed to use QoS. The proper solution
1328 * is to pass the frame up so ieee80211_input can do the
1329 * work but that's made hard by how things currently are
1330 * done in the driver.
1333 iwi_checkforqos(struct iwi_softc *sc, const struct ieee80211_frame *wh, int len)
1335 #define SUBTYPE(wh) ((wh)->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
1336 const uint8_t *frm, *efrm, *wme;
1337 struct ieee80211_node *ni;
1338 uint16_t capinfo, status, associd;
1340 /* NB: +8 for capinfo, status, associd, and first ie */
1341 if (!(sizeof(*wh)+8 < len && len < IEEE80211_MAX_LEN) ||
1342 SUBTYPE(wh) != IEEE80211_FC0_SUBTYPE_ASSOC_RESP)
1345 * asresp frame format
1346 * [2] capability information
1348 * [2] association ID
1349 * [tlv] supported rates
1350 * [tlv] extended supported rates
1353 frm = (const uint8_t *)&wh[1];
1354 efrm = ((const uint8_t *) wh) + len;
1356 capinfo = le16toh(*(const uint16_t *)frm);
1358 status = le16toh(*(const uint16_t *)frm);
1360 associd = le16toh(*(const uint16_t *)frm);
1364 while (frm < efrm) {
1365 IEEE80211_VERIFY_LENGTH(efrm - frm, frm[1]);
1367 case IEEE80211_ELEMID_VENDOR:
1375 ni = sc->sc_ic.ic_bss;
1376 ni->ni_capinfo = capinfo;
1377 ni->ni_associd = associd;
1379 ni->ni_flags |= IEEE80211_NODE_QOS;
1381 ni->ni_flags &= ~IEEE80211_NODE_QOS;
1386 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1388 struct ieee80211com *ic = &sc->sc_ic;
1389 struct iwi_notif_scan_channel *chan;
1390 struct iwi_notif_scan_complete *scan;
1391 struct iwi_notif_authentication *auth;
1392 struct iwi_notif_association *assoc;
1393 struct iwi_notif_beacon_state *beacon;
1395 switch (notif->type) {
1396 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1397 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1399 DPRINTFN(3, ("Scan of channel %u complete (%u)\n",
1400 ieee80211_ieee2mhz(chan->nchan, 0), chan->nchan));
1402 /* Reset the timer, the scan is still going */
1403 sc->sc_state_timer = 3;
1406 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1407 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1409 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1412 IWI_STATE_END(sc, IWI_FW_SCANNING);
1414 if (scan->status == IWI_SCAN_COMPLETED)
1415 ieee80211_scan_next(ic);
1419 case IWI_NOTIF_TYPE_AUTHENTICATION:
1420 auth = (struct iwi_notif_authentication *)(notif + 1);
1422 switch (auth->state) {
1423 case IWI_AUTH_SUCCESS:
1424 DPRINTFN(2, ("Authentication succeeeded\n"));
1425 ieee80211_node_authorize(ic->ic_bss);
1426 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1430 DPRINTFN(2, ("Authentication failed\n"));
1431 sc->flags &= ~IWI_FLAG_ASSOCIATED;
1432 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1436 case IWI_AUTH_SENT_1:
1437 case IWI_AUTH_RECV_2:
1438 case IWI_AUTH_SEQ1_PASS:
1441 case IWI_AUTH_SEQ1_FAIL:
1442 DPRINTFN(2, ("Initial authentication handshake failed; "
1443 "you probably need shared key\n"));
1444 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1445 /* XXX retry shared key when in auto */
1449 device_printf(sc->sc_dev,
1450 "unknown authentication state %u\n", auth->state);
1454 case IWI_NOTIF_TYPE_ASSOCIATION:
1455 assoc = (struct iwi_notif_association *)(notif + 1);
1457 switch (assoc->state) {
1458 case IWI_AUTH_SUCCESS:
1459 /* re-association, do nothing */
1462 case IWI_ASSOC_SUCCESS:
1463 DPRINTFN(2, ("Association succeeded\n"));
1464 sc->flags |= IWI_FLAG_ASSOCIATED;
1465 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1467 (const struct ieee80211_frame *)(assoc+1),
1468 le16toh(notif->len) - sizeof(*assoc));
1469 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1472 case IWI_ASSOC_INIT:
1473 switch (sc->fw_state) {
1474 case IWI_FW_ASSOCIATING:
1475 DPRINTFN(2, ("Association failed\n"));
1476 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
1477 ieee80211_new_state(ic,
1478 IEEE80211_S_SCAN, -1);
1481 case IWI_FW_DISASSOCIATING:
1482 DPRINTFN(2, ("Dissassociated\n"));
1484 IWI_FW_DISASSOCIATING);
1487 sc->flags &= ~IWI_FLAG_ASSOCIATED;
1491 device_printf(sc->sc_dev,
1492 "unknown association state %u\n", assoc->state);
1496 case IWI_NOTIF_TYPE_BEACON:
1497 /* XXX check struct length */
1498 beacon = (struct iwi_notif_beacon_state *)(notif + 1);
1500 DPRINTFN(5, ("Beacon state (%u, %u)\n",
1501 beacon->state, le32toh(beacon->number)));
1503 if (beacon->state == IWI_BEACON_MISS) {
1505 * The firmware notifies us of every beacon miss
1506 * so we need to track the count against the
1507 * configured threshold before notifying the
1509 * XXX try to roam, drop assoc only on much higher count
1511 if (le32toh(beacon->number) >= ic->ic_bmissthreshold) {
1512 DPRINTF(("Beacon miss: %u >= %u\n",
1513 le32toh(beacon->number),
1514 ic->ic_bmissthreshold));
1515 ieee80211_beacon_miss(ic);
1520 case IWI_NOTIF_TYPE_CALIBRATION:
1521 case IWI_NOTIF_TYPE_NOISE:
1522 case IWI_NOTIF_TYPE_LINK_QUALITY:
1523 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1527 DPRINTF(("unknown notification type %u flags 0x%x len %u\n",
1528 notif->type, notif->flags, le16toh(notif->len)));
1533 iwi_rx_intr(struct iwi_softc *sc)
1535 struct iwi_rx_data *data;
1536 struct iwi_hdr *hdr;
1539 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1541 for (; sc->rxq.cur != hw;) {
1542 data = &sc->rxq.data[sc->rxq.cur];
1544 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1545 BUS_DMASYNC_POSTREAD);
1547 hdr = mtod(data->m, struct iwi_hdr *);
1549 switch (hdr->type) {
1550 case IWI_HDR_TYPE_FRAME:
1551 iwi_frame_intr(sc, data, sc->rxq.cur,
1552 (struct iwi_frame *)(hdr + 1));
1555 case IWI_HDR_TYPE_NOTIF:
1556 iwi_notification_intr(sc,
1557 (struct iwi_notif *)(hdr + 1));
1561 device_printf(sc->sc_dev, "unknown hdr type %u\n",
1565 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1567 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1570 /* tell the firmware what we have processed */
1571 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1572 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1576 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1578 struct ieee80211com *ic = &sc->sc_ic;
1579 struct ifnet *ifp = ic->ic_ifp;
1580 struct iwi_tx_data *data;
1583 hw = CSR_READ_4(sc, txq->csr_ridx);
1585 for (; txq->next != hw;) {
1586 data = &txq->data[txq->next];
1588 bus_dmamap_sync(txq->data_dmat, data->map,
1589 BUS_DMASYNC_POSTWRITE);
1590 bus_dmamap_unload(txq->data_dmat, data->map);
1591 if (data->m->m_flags & M_TXCB)
1592 ieee80211_process_callback(data->ni, data->m, 0/*XXX*/);
1595 ieee80211_free_node(data->ni);
1598 DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1603 txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1606 sc->sc_tx_timer = 0;
1607 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1610 iwi_led_event(sc, IWI_LED_TX);
1618 struct iwi_softc *sc = arg;
1624 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) {
1629 /* acknowledge interrupts */
1630 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1632 if (r & IWI_INTR_FATAL_ERROR) {
1633 device_printf(sc->sc_dev, "firmware error\n");
1634 /* don't restart if the interface isn't up */
1635 if (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)
1636 taskqueue_enqueue(sc->sc_tq2, &sc->sc_restarttask);
1638 sc->flags &= ~IWI_FLAG_BUSY;
1639 sc->sc_busy_timer = 0;
1643 if (r & IWI_INTR_FW_INITED) {
1644 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1648 if (r & IWI_INTR_RADIO_OFF)
1649 taskqueue_enqueue(sc->sc_tq, &sc->sc_radiofftask);
1651 if (r & IWI_INTR_CMD_DONE) {
1652 sc->flags &= ~IWI_FLAG_BUSY;
1653 sc->sc_busy_timer = 0;
1657 if (r & IWI_INTR_TX1_DONE)
1658 iwi_tx_intr(sc, &sc->txq[0]);
1660 if (r & IWI_INTR_TX2_DONE)
1661 iwi_tx_intr(sc, &sc->txq[1]);
1663 if (r & IWI_INTR_TX3_DONE)
1664 iwi_tx_intr(sc, &sc->txq[2]);
1666 if (r & IWI_INTR_TX4_DONE)
1667 iwi_tx_intr(sc, &sc->txq[3]);
1669 if (r & IWI_INTR_RX_DONE)
1672 if (r & IWI_INTR_PARITY_ERROR) {
1673 /* XXX rate-limit */
1674 device_printf(sc->sc_dev, "parity error\n");
1681 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len)
1683 struct iwi_cmd_desc *desc;
1685 IWI_LOCK_ASSERT(sc);
1687 if (sc->flags & IWI_FLAG_BUSY) {
1688 device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n",
1692 sc->flags |= IWI_FLAG_BUSY;
1693 sc->sc_busy_timer = 2;
1695 desc = &sc->cmdq.desc[sc->cmdq.cur];
1697 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1698 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1701 memcpy(desc->data, data, len);
1703 bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map,
1704 BUS_DMASYNC_PREWRITE);
1706 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1709 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1710 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1712 return msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz);
1716 iwi_write_ibssnode(struct iwi_softc *sc,
1717 const u_int8_t addr[IEEE80211_ADDR_LEN], int entry)
1719 struct iwi_ibssnode node;
1721 /* write node information into NIC memory */
1722 memset(&node, 0, sizeof node);
1723 IEEE80211_ADDR_COPY(node.bssid, addr);
1725 DPRINTF(("%s mac %6D station %u\n", __func__, node.bssid, ":", entry));
1727 CSR_WRITE_REGION_1(sc,
1728 IWI_CSR_NODE_BASE + entry * sizeof node,
1729 (uint8_t *)&node, sizeof node);
1733 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni,
1736 struct iwi_softc *sc = ifp->if_softc;
1737 struct ieee80211com *ic = &sc->sc_ic;
1738 struct iwi_node *in = (struct iwi_node *)ni;
1739 const struct ieee80211_frame *wh;
1740 struct ieee80211_key *k;
1741 const struct chanAccParams *cap;
1742 struct iwi_tx_ring *txq = &sc->txq[ac];
1743 struct iwi_tx_data *data;
1744 struct iwi_tx_desc *desc;
1746 bus_dma_segment_t segs[IWI_MAX_NSEG];
1747 int error, nsegs, hdrlen, i;
1748 int ismcast, flags, xflags, staid;
1750 IWI_LOCK_ASSERT(sc);
1751 wh = mtod(m0, const struct ieee80211_frame *);
1752 /* NB: only data frames use this path */
1753 hdrlen = ieee80211_hdrsize(wh);
1754 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
1758 flags |= IWI_DATA_FLAG_NEED_ACK;
1759 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1760 flags |= IWI_DATA_FLAG_SHPREAMBLE;
1761 if (IEEE80211_QOS_HAS_SEQ(wh)) {
1762 xflags |= IWI_DATA_XFLAG_QOS;
1763 cap = &ic->ic_wme.wme_chanParams;
1764 if (!cap->cap_wmeParams[ac].wmep_noackPolicy)
1765 flags &= ~IWI_DATA_FLAG_NEED_ACK;
1769 * This is only used in IBSS mode where the firmware expect an index
1770 * in a h/w table instead of a destination address.
1772 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1774 if (in->in_station == -1) {
1775 in->in_station = alloc_unr(sc->sc_unr);
1776 if (in->in_station == -1) {
1777 /* h/w table is full */
1779 ieee80211_free_node(ni);
1783 iwi_write_ibssnode(sc,
1784 ni->ni_macaddr, in->in_station);
1786 staid = in->in_station;
1789 * Multicast addresses have no associated node
1790 * so there will be no station entry. We reserve
1791 * entry 0 for one mcast address and use that.
1792 * If there are many being used this will be
1793 * expensive and we'll need to do a better job
1794 * but for now this handles the broadcast case.
1796 if (!IEEE80211_ADDR_EQ(wh->i_addr1, sc->sc_mcast)) {
1797 IEEE80211_ADDR_COPY(sc->sc_mcast, wh->i_addr1);
1798 iwi_write_ibssnode(sc, sc->sc_mcast, 0);
1805 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1806 k = ieee80211_crypto_encap(ic, ni, m0);
1812 /* packet header may have moved, reset our local pointer */
1813 wh = mtod(m0, struct ieee80211_frame *);
1816 if (bpf_peers_present(sc->sc_drvbpf)) {
1817 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1821 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1824 data = &txq->data[txq->cur];
1825 desc = &txq->desc[txq->cur];
1827 /* save and trim IEEE802.11 header */
1828 m_copydata(m0, 0, hdrlen, (caddr_t)&desc->wh);
1831 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0, segs,
1833 if (error != 0 && error != EFBIG) {
1834 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1840 mnew = m_defrag(m0, M_DONTWAIT);
1842 device_printf(sc->sc_dev,
1843 "could not defragment mbuf\n");
1849 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map,
1850 m0, segs, &nsegs, 0);
1852 device_printf(sc->sc_dev,
1853 "could not map mbuf (error %d)\n", error);
1862 desc->hdr.type = IWI_HDR_TYPE_DATA;
1863 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1864 desc->station = staid;
1865 desc->cmd = IWI_DATA_CMD_TX;
1866 desc->len = htole16(m0->m_pkthdr.len);
1867 desc->flags = flags;
1868 desc->xflags = xflags;
1871 if (ic->ic_flags & IEEE80211_F_PRIVACY)
1872 desc->wep_txkey = ic->ic_crypto.cs_def_txkey;
1875 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1877 desc->nseg = htole32(nsegs);
1878 for (i = 0; i < nsegs; i++) {
1879 desc->seg_addr[i] = htole32(segs[i].ds_addr);
1880 desc->seg_len[i] = htole16(segs[i].ds_len);
1883 bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1884 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1886 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
1887 ac, txq->cur, le16toh(desc->len), nsegs));
1890 txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
1891 CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1897 iwi_start(struct ifnet *ifp)
1899 struct iwi_softc *sc = ifp->if_softc;
1900 struct ieee80211com *ic = &sc->sc_ic;
1902 struct ether_header *eh;
1903 struct ieee80211_node *ni;
1909 if (ic->ic_state != IEEE80211_S_RUN) {
1915 IF_DEQUEUE(&ic->ic_mgtq, m0);
1917 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
1921 * Cancel any background scan.
1923 if (ic->ic_flags & IEEE80211_F_SCAN)
1924 ieee80211_cancel_scan(ic);
1926 if (m0->m_len < sizeof (struct ether_header) &&
1927 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) {
1931 eh = mtod(m0, struct ether_header *);
1932 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1939 /* classify mbuf so we can find which tx ring to use */
1940 if (ieee80211_classify(ic, m0, ni) != 0) {
1942 ieee80211_free_node(ni);
1947 /* XXX does not belong here */
1948 /* no QoS encapsulation for EAPOL frames */
1949 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1950 M_WME_GETAC(m0) : WME_AC_BE;
1952 if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) {
1953 /* there is no place left in this ring */
1954 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
1955 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1961 m0 = ieee80211_encap(ic, m0, ni);
1963 ieee80211_free_node(ni);
1968 ni = (struct ieee80211_node *) m0->m_pkthdr.rcvif;
1969 m0->m_pkthdr.rcvif = NULL;
1970 /* XXX no way to send mgt frames (yet), discard */
1972 ieee80211_free_node(ni);
1976 if (bpf_peers_present(ic->ic_rawbpf))
1977 bpf_mtap(ic->ic_rawbpf, m0);
1979 if (iwi_tx_start(ifp, m0, ni, ac) != 0) {
1980 ieee80211_free_node(ni);
1985 sc->sc_tx_timer = 5;
1986 ic->ic_lastdata = ticks;
1993 iwi_watchdog(void *arg)
1995 struct iwi_softc *sc = arg;
1996 struct ifnet *ifp = sc->sc_ifp;
1998 IWI_LOCK_ASSERT(sc);
2000 if (sc->sc_tx_timer > 0) {
2001 if (--sc->sc_tx_timer == 0) {
2002 if_printf(ifp, "device timeout\n");
2004 taskqueue_enqueue(sc->sc_tq2, &sc->sc_restarttask);
2007 if (sc->sc_rfkill_timer > 0) {
2008 if (--sc->sc_rfkill_timer == 0) {
2010 * Check for a change in rfkill state. We get an
2011 * interrupt when a radio is disabled but not when
2012 * it is enabled so we must poll for the latter.
2014 if (!iwi_getrfkill(sc))
2015 taskqueue_enqueue(sc->sc_tq, &sc->sc_radiontask);
2017 sc->sc_rfkill_timer = 2;
2020 if (sc->sc_state_timer > 0) {
2021 if (--sc->sc_state_timer == 0) {
2022 if_printf(ifp, "firmware stuck in state %d, resetting\n",
2024 taskqueue_enqueue(sc->sc_tq2, &sc->sc_restarttask);
2025 if (sc->fw_state == IWI_FW_SCANNING)
2026 ieee80211_cancel_scan(&sc->sc_ic);
2027 sc->sc_state_timer = 3;
2030 if (sc->sc_busy_timer > 0) {
2031 if (--sc->sc_busy_timer == 0) {
2032 if_printf(ifp, "firmware command timeout, resetting\n");
2033 taskqueue_enqueue(sc->sc_tq2, &sc->sc_restarttask);
2037 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2038 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
2042 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2044 struct iwi_softc *sc = ifp->if_softc;
2045 struct ieee80211com *ic = &sc->sc_ic;
2052 * wait until pending iwi_cmd() are completed, to avoid races
2053 * that could cause problems.
2055 while (sc->flags & IWI_FLAG_BUSY)
2056 msleep(sc, &sc->sc_mtx, 0, "iwiioctl", hz);
2060 if (ifp->if_flags & IFF_UP) {
2061 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2062 iwi_init_locked(sc, 0);
2064 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2068 * If device was stopped due to rfkill then
2069 * marked down we'll have the polling thread
2070 * running; stop it explicitly.
2072 sc->sc_rfkill_timer = 0;
2078 error = ieee80211_ioctl(ic, cmd, data);
2081 if (error == ENETRESET) {
2082 if ((ifp->if_flags & IFF_UP) &&
2083 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
2084 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2085 iwi_init_locked(sc, 0);
2095 iwi_stop_master(struct iwi_softc *sc)
2100 /* disable interrupts */
2101 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
2103 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
2104 for (ntries = 0; ntries < 5; ntries++) {
2105 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2110 device_printf(sc->sc_dev, "timeout waiting for master\n");
2112 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2113 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
2115 sc->flags &= ~IWI_FLAG_FW_INITED;
2119 iwi_reset(struct iwi_softc *sc)
2124 iwi_stop_master(sc);
2126 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2127 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2129 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
2131 /* wait for clock stabilization */
2132 for (ntries = 0; ntries < 1000; ntries++) {
2133 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
2137 if (ntries == 1000) {
2138 device_printf(sc->sc_dev,
2139 "timeout waiting for clock stabilization\n");
2143 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2144 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET);
2148 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2149 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2151 /* clear NIC memory */
2152 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
2153 for (i = 0; i < 0xc000; i++)
2154 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2159 static const struct iwi_firmware_ohdr *
2160 iwi_setup_ofw(struct iwi_softc *sc, struct iwi_fw *fw)
2162 const struct firmware *fp = fw->fp;
2163 const struct iwi_firmware_ohdr *hdr;
2165 if (fp->datasize < sizeof (struct iwi_firmware_ohdr)) {
2166 device_printf(sc->sc_dev, "image '%s' too small\n", fp->name);
2169 hdr = (const struct iwi_firmware_ohdr *)fp->data;
2170 if ((IWI_FW_GET_MAJOR(le32toh(hdr->version)) != IWI_FW_REQ_MAJOR) ||
2171 (IWI_FW_GET_MINOR(le32toh(hdr->version)) != IWI_FW_REQ_MINOR)) {
2172 device_printf(sc->sc_dev, "version for '%s' %d.%d != %d.%d\n",
2173 fp->name, IWI_FW_GET_MAJOR(le32toh(hdr->version)),
2174 IWI_FW_GET_MINOR(le32toh(hdr->version)), IWI_FW_REQ_MAJOR,
2178 fw->data = ((const char *) fp->data) + sizeof(struct iwi_firmware_ohdr);
2179 fw->size = fp->datasize - sizeof(struct iwi_firmware_ohdr);
2180 fw->name = fp->name;
2184 static const struct iwi_firmware_ohdr *
2185 iwi_setup_oucode(struct iwi_softc *sc, struct iwi_fw *fw)
2187 const struct iwi_firmware_ohdr *hdr;
2189 hdr = iwi_setup_ofw(sc, fw);
2190 if (hdr != NULL && le32toh(hdr->mode) != IWI_FW_MODE_UCODE) {
2191 device_printf(sc->sc_dev, "%s is not a ucode image\n",
2199 iwi_getfw(struct iwi_fw *fw, const char *fwname,
2200 struct iwi_fw *uc, const char *ucname)
2203 fw->fp = firmware_get(fwname);
2204 /* NB: pre-3.0 ucode is packaged separately */
2205 if (uc->fp == NULL && fw->fp != NULL && fw->fp->version < 300)
2206 uc->fp = firmware_get(ucname);
2210 * Get the required firmware images if not already loaded.
2211 * Note that we hold firmware images so long as the device
2212 * is marked up in case we need to reload them on device init.
2213 * This is necessary because we re-init the device sometimes
2214 * from a context where we cannot read from the filesystem
2215 * (e.g. from the taskqueue thread when rfkill is re-enabled).
2216 * XXX return 0 on success, 1 on error.
2218 * NB: the order of get'ing and put'ing images here is
2219 * intentional to support handling firmware images bundled
2220 * by operating mode and/or all together in one file with
2221 * the boot firmware as "master".
2224 iwi_get_firmware(struct iwi_softc *sc)
2226 struct ieee80211com *ic = &sc->sc_ic;
2227 const struct iwi_firmware_hdr *hdr;
2228 const struct firmware *fp;
2230 /* invalidate cached firmware on mode change */
2231 if (sc->fw_mode != ic->ic_opmode)
2232 iwi_put_firmware(sc);
2234 switch (ic->ic_opmode) {
2235 case IEEE80211_M_STA:
2236 iwi_getfw(&sc->fw_fw, "iwi_bss", &sc->fw_uc, "iwi_ucode_bss");
2239 case IEEE80211_M_IBSS:
2240 iwi_getfw(&sc->fw_fw, "iwi_ibss", &sc->fw_uc, "iwi_ucode_ibss");
2243 case IEEE80211_M_MONITOR:
2244 iwi_getfw(&sc->fw_fw, "iwi_monitor",
2245 &sc->fw_uc, "iwi_ucode_monitor");
2253 device_printf(sc->sc_dev, "could not load firmware\n");
2256 if (fp->version < 300) {
2258 * Firmware prior to 3.0 was packaged as separate
2259 * boot, firmware, and ucode images. Verify the
2260 * ucode image was read in, retrieve the boot image
2261 * if needed, and check version stamps for consistency.
2262 * The version stamps in the data are also checked
2263 * above; this is a bit paranoid but is a cheap
2264 * safeguard against mis-packaging.
2266 if (sc->fw_uc.fp == NULL) {
2267 device_printf(sc->sc_dev, "could not load ucode\n");
2270 if (sc->fw_boot.fp == NULL) {
2271 sc->fw_boot.fp = firmware_get("iwi_boot");
2272 if (sc->fw_boot.fp == NULL) {
2273 device_printf(sc->sc_dev,
2274 "could not load boot firmware\n");
2278 if (sc->fw_boot.fp->version != sc->fw_fw.fp->version ||
2279 sc->fw_boot.fp->version != sc->fw_uc.fp->version) {
2280 device_printf(sc->sc_dev,
2281 "firmware version mismatch: "
2282 "'%s' is %d, '%s' is %d, '%s' is %d\n",
2283 sc->fw_boot.fp->name, sc->fw_boot.fp->version,
2284 sc->fw_uc.fp->name, sc->fw_uc.fp->version,
2285 sc->fw_fw.fp->name, sc->fw_fw.fp->version
2290 * Check and setup each image.
2292 if (iwi_setup_oucode(sc, &sc->fw_uc) == NULL ||
2293 iwi_setup_ofw(sc, &sc->fw_boot) == NULL ||
2294 iwi_setup_ofw(sc, &sc->fw_fw) == NULL)
2298 * Check and setup combined image.
2300 if (fp->datasize < sizeof(struct iwi_firmware_hdr)) {
2301 device_printf(sc->sc_dev, "image '%s' too small\n",
2305 hdr = (const struct iwi_firmware_hdr *)fp->data;
2306 if (fp->datasize < sizeof(*hdr) + le32toh(hdr->bsize) + le32toh(hdr->usize)
2307 + le32toh(hdr->fsize)) {
2308 device_printf(sc->sc_dev, "image '%s' too small (2)\n",
2312 sc->fw_boot.data = ((const char *) fp->data) + sizeof(*hdr);
2313 sc->fw_boot.size = le32toh(hdr->bsize);
2314 sc->fw_boot.name = fp->name;
2315 sc->fw_uc.data = sc->fw_boot.data + sc->fw_boot.size;
2316 sc->fw_uc.size = le32toh(hdr->usize);
2317 sc->fw_uc.name = fp->name;
2318 sc->fw_fw.data = sc->fw_uc.data + sc->fw_uc.size;
2319 sc->fw_fw.size = le32toh(hdr->fsize);
2320 sc->fw_fw.name = fp->name;
2323 device_printf(sc->sc_dev, "boot %d ucode %d fw %d bytes\n",
2324 sc->fw_boot.size, sc->fw_uc.size, sc->fw_fw.size);
2327 sc->fw_mode = ic->ic_opmode;
2330 iwi_put_firmware(sc);
2335 iwi_put_fw(struct iwi_fw *fw)
2337 if (fw->fp != NULL) {
2338 firmware_put(fw->fp, FIRMWARE_UNLOAD);
2347 * Release any cached firmware images.
2350 iwi_put_firmware(struct iwi_softc *sc)
2352 iwi_put_fw(&sc->fw_uc);
2353 iwi_put_fw(&sc->fw_fw);
2354 iwi_put_fw(&sc->fw_boot);
2358 iwi_load_ucode(struct iwi_softc *sc, const struct iwi_fw *fw)
2362 const char *uc = fw->data;
2363 size_t size = fw->size;
2364 int i, ntries, error;
2366 IWI_LOCK_ASSERT(sc);
2368 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
2369 IWI_RST_STOP_MASTER);
2370 for (ntries = 0; ntries < 5; ntries++) {
2371 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2376 device_printf(sc->sc_dev, "timeout waiting for master\n");
2381 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2384 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2385 tmp &= ~IWI_RST_PRINCETON_RESET;
2386 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2389 MEM_WRITE_4(sc, 0x3000e0, 0);
2391 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 1);
2393 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, 0);
2395 MEM_WRITE_1(sc, 0x200000, 0x00);
2396 MEM_WRITE_1(sc, 0x200000, 0x40);
2399 /* write microcode into adapter memory */
2400 for (w = (const uint16_t *)uc; size > 0; w++, size -= 2)
2401 MEM_WRITE_2(sc, 0x200010, htole16(*w));
2403 MEM_WRITE_1(sc, 0x200000, 0x00);
2404 MEM_WRITE_1(sc, 0x200000, 0x80);
2406 /* wait until we get an answer */
2407 for (ntries = 0; ntries < 100; ntries++) {
2408 if (MEM_READ_1(sc, 0x200000) & 1)
2412 if (ntries == 100) {
2413 device_printf(sc->sc_dev,
2414 "timeout waiting for ucode to initialize\n");
2419 /* read the answer or the firmware will not initialize properly */
2420 for (i = 0; i < 7; i++)
2421 MEM_READ_4(sc, 0x200004);
2423 MEM_WRITE_1(sc, 0x200000, 0x00);
2429 /* macro to handle unaligned little endian data in firmware image */
2430 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2433 iwi_load_firmware(struct iwi_softc *sc, const struct iwi_fw *fw)
2436 uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp;
2439 IWI_LOCK_ASSERT(sc);
2440 /* copy firmware image to DMA memory */
2441 memcpy(sc->fw_virtaddr, fw->data, fw->size);
2443 /* make sure the adapter will get up-to-date values */
2444 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_PREWRITE);
2446 /* tell the adapter where the command blocks are stored */
2447 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2450 * Store command blocks into adapter's internal memory using register
2451 * indirections. The adapter will read the firmware image through DMA
2452 * using information stored in command blocks.
2454 src = sc->fw_physaddr;
2455 p = sc->fw_virtaddr;
2457 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2460 dst = GETLE32(p); p += 4; src += 4;
2461 len = GETLE32(p); p += 4; src += 4;
2465 mlen = min(len, IWI_CB_MAXDATALEN);
2467 ctl = IWI_CB_DEFAULT_CTL | mlen;
2468 sum = ctl ^ src ^ dst;
2470 /* write a command block */
2471 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2472 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2473 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2474 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2482 /* write a fictive final command block (sentinel) */
2483 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2484 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2486 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2487 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
2488 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2490 /* tell the adapter to start processing command blocks */
2491 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2493 /* wait until the adapter reaches the sentinel */
2494 for (ntries = 0; ntries < 400; ntries++) {
2495 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2499 /* sync dma, just in case */
2500 bus_dmamap_sync(sc->fw_dmat, sc->fw_map, BUS_DMASYNC_POSTWRITE);
2501 if (ntries == 400) {
2502 device_printf(sc->sc_dev,
2503 "timeout processing command blocks for %s firmware\n",
2508 /* we're done with command blocks processing */
2509 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2511 /* allow interrupts so we know when the firmware is ready */
2512 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2514 /* tell the adapter to initialize the firmware */
2515 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2517 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2518 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
2520 /* wait at most one second for firmware initialization to complete */
2521 if ((error = msleep(sc, &sc->sc_mtx, 0, "iwiinit", hz)) != 0) {
2522 device_printf(sc->sc_dev, "timeout waiting for %s firmware "
2523 "initialization to complete\n", fw->name);
2530 iwi_setpowermode(struct iwi_softc *sc)
2532 struct ieee80211com *ic = &sc->sc_ic;
2535 if (ic->ic_flags & IEEE80211_F_PMGTON) {
2536 /* XXX set more fine-grained operation */
2537 data = htole32(IWI_POWER_MODE_MAX);
2539 data = htole32(IWI_POWER_MODE_CAM);
2541 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2542 return iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data);
2546 iwi_setwepkeys(struct iwi_softc *sc)
2548 struct ieee80211com *ic = &sc->sc_ic;
2549 struct iwi_wep_key wepkey;
2550 struct ieee80211_key *wk;
2553 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2554 wk = &ic->ic_crypto.cs_nw_keys[i];
2556 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2558 wepkey.len = wk->wk_keylen;
2559 memset(wepkey.key, 0, sizeof wepkey.key);
2560 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2561 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2563 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2572 iwi_config(struct iwi_softc *sc)
2574 struct ieee80211com *ic = &sc->sc_ic;
2575 struct ifnet *ifp = ic->ic_ifp;
2576 struct iwi_configuration config;
2577 struct iwi_rateset rs;
2578 struct iwi_txpower power;
2581 IWI_LOCK_ASSERT(sc);
2583 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2584 DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":"));
2585 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2586 IEEE80211_ADDR_LEN);
2590 memset(&config, 0, sizeof config);
2591 config.bluetooth_coexistence = sc->bluetooth;
2592 config.silence_threshold = 0x1e;
2593 config.antenna = sc->antenna;
2594 config.multicast_enabled = 1;
2595 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2596 config.disable_unicast_decryption = 1;
2597 config.disable_multicast_decryption = 1;
2598 DPRINTF(("Configuring adapter\n"));
2599 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2603 error = iwi_setpowermode(sc);
2607 data = htole32(ic->ic_rtsthreshold);
2608 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2609 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2613 data = htole32(ic->ic_fragthreshold);
2614 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2615 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2619 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2620 power.mode = IWI_MODE_11B;
2622 for (i = 0; i < 11; i++) {
2623 power.chan[i].chan = i + 1;
2624 power.chan[i].power = IWI_TXPOWER_MAX;
2626 DPRINTF(("Setting .11b channels tx power\n"));
2627 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2631 power.mode = IWI_MODE_11G;
2632 DPRINTF(("Setting .11g channels tx power\n"));
2633 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power);
2638 memset(&rs, 0, sizeof rs);
2639 rs.mode = IWI_MODE_11G;
2640 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2641 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2642 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2644 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2645 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2649 memset(&rs, 0, sizeof rs);
2650 rs.mode = IWI_MODE_11A;
2651 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2652 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2653 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2655 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2656 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2660 /* if we have a desired ESSID, set it now */
2661 if (ic->ic_des_ssid[0].len != 0) {
2663 if (iwi_debug > 0) {
2664 printf("Setting desired ESSID to ");
2665 ieee80211_print_essid(ic->ic_des_ssid[0].ssid,
2666 ic->ic_des_ssid[0].len);
2670 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_ssid[0].ssid,
2671 ic->ic_des_ssid[0].len);
2676 data = htole32(arc4random());
2677 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2678 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data);
2682 error = iwi_setwepkeys(sc);
2686 /* enable adapter */
2687 DPRINTF(("Enabling adapter\n"));
2688 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0);
2691 static __inline void
2692 set_scan_type(struct iwi_scan_ext *scan, int ix, int scan_type)
2694 uint8_t *st = &scan->scan_type[ix / 2];
2696 *st = (*st & 0xf0) | ((scan_type & 0xf) << 0);
2698 *st = (*st & 0x0f) | ((scan_type & 0xf) << 4);
2702 scan_type(const struct ieee80211_scan_state *ss,
2703 const struct ieee80211_channel *chan)
2705 /* We can only set one essid for a directed scan */
2706 if (ss->ss_nssid != 0)
2707 return IWI_SCAN_TYPE_BDIRECTED;
2708 if ((ss->ss_flags & IEEE80211_SCAN_ACTIVE) &&
2709 (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
2710 return IWI_SCAN_TYPE_BROADCAST;
2711 return IWI_SCAN_TYPE_PASSIVE;
2715 scan_band(const struct ieee80211_channel *c)
2717 return IEEE80211_IS_CHAN_5GHZ(c) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ;
2721 * Start a scan on the current channel or all channels.
2724 iwi_scanchan(struct iwi_softc *sc, unsigned long maxdwell, int mode)
2726 struct ieee80211com *ic;
2727 struct ieee80211_channel *chan;
2728 struct ieee80211_scan_state *ss;
2729 struct iwi_scan_ext scan;
2732 IWI_LOCK_ASSERT(sc);
2733 if (sc->fw_state == IWI_FW_SCANNING) {
2735 * This should not happen as we only trigger scan_next after
2738 DPRINTF(("%s: called too early - still scanning\n", __func__));
2741 IWI_STATE_BEGIN(sc, IWI_FW_SCANNING);
2746 memset(&scan, 0, sizeof scan);
2747 scan.full_scan_index = htole32(++sc->sc_scangen);
2748 scan.dwell_time[IWI_SCAN_TYPE_PASSIVE] = htole16(maxdwell);
2749 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
2751 * Use very short dwell times for when we send probe request
2752 * frames. Without this bg scans hang. Ideally this should
2753 * be handled with early-termination as done by net80211 but
2754 * that's not feasible (aborting a scan is problematic).
2756 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(30);
2757 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(30);
2759 scan.dwell_time[IWI_SCAN_TYPE_BROADCAST] = htole16(maxdwell);
2760 scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED] = htole16(maxdwell);
2763 /* We can only set one essid for a directed scan */
2764 if (ss->ss_nssid != 0) {
2765 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ss->ss_ssid[0].ssid,
2766 ss->ss_ssid[0].len);
2771 if (mode == IWI_SCAN_ALLCHAN) {
2772 int i, next, band, b, bstart;
2774 * Convert scan list to run-length encoded channel list
2775 * the firmware requires (preserving the order setup by
2776 * net80211). The first entry in each run specifies the
2777 * band and the count of items in the run.
2779 next = 0; /* next open slot */
2780 bstart = 0; /* NB: not needed, silence compiler */
2781 band = -1; /* NB: impossible value */
2782 KASSERT(ss->ss_last > 0, ("no channels"));
2783 for (i = 0; i < ss->ss_last; i++) {
2784 chan = ss->ss_chans[i];
2785 b = scan_band(chan);
2788 scan.channels[bstart] =
2789 (next - bstart) | band;
2790 /* NB: this allocates a slot for the run-len */
2791 band = b, bstart = next++;
2793 if (next >= IWI_SCAN_CHANNELS) {
2794 DPRINTF(("truncating scan list\n"));
2797 scan.channels[next] = ieee80211_chan2ieee(ic, chan);
2798 set_scan_type(&scan, next, scan_type(ss, chan));
2801 scan.channels[bstart] = (next - bstart) | band;
2803 /* Scan the current channel only */
2804 chan = ic->ic_curchan;
2805 scan.channels[0] = 1 | scan_band(chan);
2806 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2807 set_scan_type(&scan, 1, scan_type(ss, chan));
2810 if (iwi_debug > 0) {
2811 static const char *scantype[8] =
2812 { "PSTOP", "PASV", "DIR", "BCAST", "BDIR", "5", "6", "7" };
2814 printf("Scan request: index %u dwell %d/%d/%d\n"
2815 , le32toh(scan.full_scan_index)
2816 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_PASSIVE])
2817 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BROADCAST])
2818 , le16toh(scan.dwell_time[IWI_SCAN_TYPE_BDIRECTED])
2822 int run = scan.channels[i];
2825 printf("Scan %d %s channels:", run & 0x3f,
2826 run & IWI_CHAN_2GHZ ? "2.4GHz" : "5GHz");
2827 for (run &= 0x3f, i++; run > 0; run--, i++) {
2828 uint8_t type = scan.scan_type[i/2];
2829 printf(" %u/%s", scan.channels[i],
2830 scantype[(i & 1 ? type : type>>4) & 7]);
2833 } while (i < IWI_SCAN_CHANNELS);
2837 return (iwi_cmd(sc, IWI_CMD_SCAN_EXT, &scan, sizeof scan));
2841 iwi_scanabort(void *arg, int npending)
2843 struct iwi_softc *sc = arg;
2847 sc->flags &= ~IWI_FLAG_CHANNEL_SCAN;
2848 /* NB: make sure we're still scanning */
2849 if (sc->fw_state == IWI_FW_SCANNING)
2850 iwi_cmd(sc, IWI_CMD_ABORT_SCAN, NULL, 0);
2855 iwi_set_sensitivity(struct iwi_softc *sc, int8_t rssi_dbm)
2857 struct iwi_sensitivity sens;
2859 DPRINTF(("Setting sensitivity to %d\n", rssi_dbm));
2861 memset(&sens, 0, sizeof sens);
2862 sens.rssi = htole16(rssi_dbm);
2863 return iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &sens, sizeof sens);
2867 iwi_auth_and_assoc(struct iwi_softc *sc)
2869 struct ieee80211com *ic = &sc->sc_ic;
2870 struct ifnet *ifp = ic->ic_ifp;
2871 struct ieee80211_node *ni = ic->ic_bss;
2872 struct iwi_configuration config;
2873 struct iwi_associate *assoc = &sc->assoc;
2874 struct iwi_rateset rs;
2878 IWI_LOCK_ASSERT(sc);
2880 if (sc->flags & IWI_FLAG_ASSOCIATED) {
2881 DPRINTF(("Already associated\n"));
2885 IWI_STATE_BEGIN(sc, IWI_FW_ASSOCIATING);
2889 if (IEEE80211_IS_CHAN_A(ic->ic_curchan))
2890 mode = IWI_MODE_11A;
2891 else if (IEEE80211_IS_CHAN_G(ic->ic_curchan))
2892 mode = IWI_MODE_11G;
2893 if (IEEE80211_IS_CHAN_B(ic->ic_curchan))
2894 mode = IWI_MODE_11B;
2896 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2897 memset(&config, 0, sizeof config);
2898 config.bluetooth_coexistence = sc->bluetooth;
2899 config.antenna = sc->antenna;
2900 config.multicast_enabled = 1;
2901 if (mode == IWI_MODE_11G)
2902 config.use_protection = 1;
2903 config.answer_pbreq =
2904 (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2905 config.disable_unicast_decryption = 1;
2906 config.disable_multicast_decryption = 1;
2907 DPRINTF(("Configuring adapter\n"));
2908 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config);
2914 if (iwi_debug > 0) {
2915 printf("Setting ESSID to ");
2916 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2920 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen);
2924 /* the rate set has already been "negotiated" */
2925 memset(&rs, 0, sizeof rs);
2927 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2928 rs.nrates = ni->ni_rates.rs_nrates;
2929 if (rs.nrates > IWI_RATESET_SIZE) {
2930 DPRINTF(("Truncating negotiated rate set from %u\n",
2932 rs.nrates = IWI_RATESET_SIZE;
2934 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2935 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
2936 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs);
2940 memset(assoc, 0, sizeof *assoc);
2942 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_ies.wme_ie != NULL) {
2943 /* NB: don't treat WME setup as failure */
2944 if (iwi_wme_setparams(sc) == 0 && iwi_wme_setie(sc) == 0)
2945 assoc->policy |= htole16(IWI_POLICY_WME);
2946 /* XXX complain on failure? */
2949 if (ic->ic_opt_ie != NULL) {
2950 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
2951 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
2957 error = iwi_set_sensitivity(sc, ni->ni_rssi);
2962 assoc->chan = ic->ic_curchan->ic_ieee;
2964 * NB: do not arrange for shared key auth w/o privacy
2965 * (i.e. a wep key); it causes a firmware error.
2967 if ((ic->ic_flags & IEEE80211_F_PRIVACY) &&
2968 ni->ni_authmode == IEEE80211_AUTH_SHARED) {
2969 assoc->auth = IWI_AUTH_SHARED;
2971 * It's possible to have privacy marked but no default
2972 * key setup. This typically is due to a user app bug
2973 * but if we blindly grab the key the firmware will
2974 * barf so avoid it for now.
2976 if (ic->ic_crypto.cs_def_txkey != IEEE80211_KEYIX_NONE)
2977 assoc->auth |= ic->ic_crypto.cs_def_txkey << 4;
2979 error = iwi_setwepkeys(sc);
2983 if (ic->ic_flags & IEEE80211_F_WPA)
2984 assoc->policy |= htole16(IWI_POLICY_WPA);
2985 if (ic->ic_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0)
2986 assoc->type = IWI_HC_IBSS_START;
2988 assoc->type = IWI_HC_ASSOC;
2989 memcpy(assoc->tstamp, ni->ni_tstamp.data, 8);
2991 if (ic->ic_opmode == IEEE80211_M_IBSS)
2992 capinfo = IEEE80211_CAPINFO_IBSS;
2994 capinfo = IEEE80211_CAPINFO_ESS;
2995 if (ic->ic_flags & IEEE80211_F_PRIVACY)
2996 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2997 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2998 IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
2999 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
3000 if (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)
3001 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
3002 assoc->capinfo = htole16(capinfo);
3004 assoc->lintval = htole16(ic->ic_lintval);
3005 assoc->intval = htole16(ni->ni_intval);
3006 IEEE80211_ADDR_COPY(assoc->bssid, ni->ni_bssid);
3007 if (ic->ic_opmode == IEEE80211_M_IBSS)
3008 IEEE80211_ADDR_COPY(assoc->dst, ifp->if_broadcastaddr);
3010 IEEE80211_ADDR_COPY(assoc->dst, ni->ni_bssid);
3012 DPRINTF(("%s bssid %6D dst %6D channel %u policy 0x%x "
3013 "auth %u capinfo 0x%x lintval %u bintval %u\n",
3014 assoc->type == IWI_HC_IBSS_START ? "Start" : "Join",
3015 assoc->bssid, ":", assoc->dst, ":",
3016 assoc->chan, le16toh(assoc->policy), assoc->auth,
3017 le16toh(assoc->capinfo), le16toh(assoc->lintval),
3018 le16toh(assoc->intval)));
3019 error = iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
3022 IWI_STATE_END(sc, IWI_FW_ASSOCIATING);
3028 iwi_disassociate(struct iwi_softc *sc, int quiet)
3030 struct iwi_associate *assoc = &sc->assoc;
3032 if ((sc->flags & IWI_FLAG_ASSOCIATED) == 0) {
3033 DPRINTF(("Not associated\n"));
3037 IWI_STATE_BEGIN(sc, IWI_FW_DISASSOCIATING);
3040 assoc->type = IWI_HC_DISASSOC_QUIET;
3042 assoc->type = IWI_HC_DISASSOC;
3044 DPRINTF(("Trying to disassociate from %6D channel %u\n",
3045 assoc->bssid, ":", assoc->chan));
3046 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, assoc, sizeof *assoc);
3050 iwi_init(void *priv)
3052 struct iwi_softc *sc = priv;
3056 iwi_init_locked(sc, 0);
3061 * release dma resources for the firmware
3064 iwi_release_fw_dma(struct iwi_softc *sc)
3066 if (sc->fw_flags & IWI_FW_HAVE_PHY)
3067 bus_dmamap_unload(sc->fw_dmat, sc->fw_map);
3068 if (sc->fw_flags & IWI_FW_HAVE_MAP)
3069 bus_dmamem_free(sc->fw_dmat, sc->fw_virtaddr, sc->fw_map);
3070 if (sc->fw_flags & IWI_FW_HAVE_DMAT)
3071 bus_dma_tag_destroy(sc->fw_dmat);
3074 sc->fw_dma_size = 0;
3077 sc->fw_physaddr = 0;
3078 sc->fw_virtaddr = NULL;
3082 * allocate the dma descriptor for the firmware.
3083 * Return 0 on success, 1 on error.
3084 * Must be called unlocked, protected by IWI_FLAG_FW_LOADING.
3087 iwi_init_fw_dma(struct iwi_softc *sc, int size)
3089 if (sc->fw_dma_size >= size)
3091 if (bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
3092 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
3093 size, 1, size, 0, NULL, NULL, &sc->fw_dmat) != 0) {
3094 device_printf(sc->sc_dev,
3095 "could not create firmware DMA tag\n");
3098 sc->fw_flags |= IWI_FW_HAVE_DMAT;
3099 if (bus_dmamem_alloc(sc->fw_dmat, &sc->fw_virtaddr, 0,
3100 &sc->fw_map) != 0) {
3101 device_printf(sc->sc_dev,
3102 "could not allocate firmware DMA memory\n");
3105 sc->fw_flags |= IWI_FW_HAVE_MAP;
3106 if (bus_dmamap_load(sc->fw_dmat, sc->fw_map, sc->fw_virtaddr,
3107 size, iwi_dma_map_addr, &sc->fw_physaddr, 0) != 0) {
3108 device_printf(sc->sc_dev, "could not load firmware DMA map\n");
3111 sc->fw_flags |= IWI_FW_HAVE_PHY;
3112 sc->fw_dma_size = size;
3116 iwi_release_fw_dma(sc);
3121 iwi_init_locked(void *priv, int force)
3123 struct iwi_softc *sc = priv;
3124 struct ieee80211com *ic = &sc->sc_ic;
3125 struct ifnet *ifp = ic->ic_ifp;
3126 struct iwi_rx_data *data;
3130 IWI_LOCK_ASSERT(sc);
3131 if (sc->fw_state == IWI_FW_LOADING) {
3132 device_printf(sc->sc_dev, "%s: already loading\n", __func__);
3133 return; /* XXX: condvar? */
3137 IWI_STATE_BEGIN(sc, IWI_FW_LOADING);
3139 if (iwi_reset(sc) != 0) {
3140 device_printf(sc->sc_dev, "could not reset adapter\n");
3145 if (iwi_get_firmware(sc)) {
3150 /* allocate DMA memory for mapping firmware image */
3152 if (sc->fw_boot.size > i)
3153 i = sc->fw_boot.size;
3154 /* XXX do we dma the ucode as well ? */
3155 if (sc->fw_uc.size > i)
3157 if (iwi_init_fw_dma(sc, i)) {
3163 if (iwi_load_firmware(sc, &sc->fw_boot) != 0) {
3164 device_printf(sc->sc_dev,
3165 "could not load boot firmware %s\n", sc->fw_boot.name);
3169 if (iwi_load_ucode(sc, &sc->fw_uc) != 0) {
3170 device_printf(sc->sc_dev,
3171 "could not load microcode %s\n", sc->fw_uc.name);
3175 iwi_stop_master(sc);
3177 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr);
3178 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
3179 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
3181 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr);
3182 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
3183 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
3185 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr);
3186 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
3187 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
3189 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr);
3190 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
3191 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
3193 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr);
3194 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
3195 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
3197 for (i = 0; i < sc->rxq.count; i++) {
3198 data = &sc->rxq.data[i];
3199 CSR_WRITE_4(sc, data->reg, data->physaddr);
3202 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1);
3204 if (iwi_load_firmware(sc, &sc->fw_fw) != 0) {
3205 device_printf(sc->sc_dev,
3206 "could not load main firmware %s\n", sc->fw_fw.name);
3209 sc->flags |= IWI_FLAG_FW_INITED;
3211 if (iwi_config(sc) != 0) {
3212 device_printf(sc->sc_dev, "device configuration failed\n");
3216 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
3218 * NB: When restarting the adapter clock the state
3219 * machine regardless of the roaming mode; otherwise
3220 * we need to notify user apps so they can manually
3221 * get us going again.
3223 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL || force)
3224 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
3226 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
3228 callout_reset(&sc->sc_wdtimer, hz, iwi_watchdog, sc);
3229 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3230 ifp->if_drv_flags |= IFF_DRV_RUNNING;
3232 IWI_STATE_END(sc, IWI_FW_LOADING);
3235 fail: ifp->if_flags &= ~IFF_UP;
3236 IWI_STATE_END(sc, IWI_FW_LOADING);
3238 iwi_put_firmware(sc);
3242 iwi_stop(void *priv)
3244 struct iwi_softc *sc = priv;
3245 struct ieee80211com *ic = &sc->sc_ic;
3246 struct ifnet *ifp = ic->ic_ifp;
3248 IWI_LOCK_ASSERT(sc);
3249 if (sc->sc_softled) {
3250 callout_stop(&sc->sc_ledtimer);
3251 sc->sc_blinking = 0;
3254 callout_stop(&sc->sc_wdtimer);
3255 iwi_stop_master(sc);
3257 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET);
3260 iwi_reset_cmd_ring(sc, &sc->cmdq);
3261 iwi_reset_tx_ring(sc, &sc->txq[0]);
3262 iwi_reset_tx_ring(sc, &sc->txq[1]);
3263 iwi_reset_tx_ring(sc, &sc->txq[2]);
3264 iwi_reset_tx_ring(sc, &sc->txq[3]);
3265 iwi_reset_rx_ring(sc, &sc->rxq);
3267 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3269 memset(sc->sc_cmd, 0, sizeof(sc->sc_cmd));
3270 sc->sc_tx_timer = 0;
3271 sc->sc_rfkill_timer = 0;
3272 sc->sc_state_timer = 0;
3273 sc->sc_busy_timer = 0;
3274 sc->flags &= ~(IWI_FLAG_BUSY | IWI_FLAG_ASSOCIATED);
3275 sc->fw_state = IWI_FW_IDLE;
3278 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
3282 iwi_restart(void *arg, int npending)
3284 struct iwi_softc *sc = arg;
3288 iwi_init_locked(sc, 1); /* NB: force state machine */
3293 * Return whether or not the radio is enabled in hardware
3294 * (i.e. the rfkill switch is "off").
3297 iwi_getrfkill(struct iwi_softc *sc)
3299 return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0;
3303 iwi_radio_on(void *arg, int pending)
3305 struct iwi_softc *sc = arg;
3307 device_printf(sc->sc_dev, "radio turned on\n");
3312 iwi_radio_off(void *arg, int pending)
3314 struct iwi_softc *sc = arg;
3317 device_printf(sc->sc_dev, "radio turned off\n");
3320 sc->sc_rfkill_timer = 2;
3325 iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
3327 struct iwi_softc *sc = arg1;
3328 uint32_t size, buf[128];
3330 memset(buf, 0, sizeof buf);
3332 if (!(sc->flags & IWI_FLAG_FW_INITED))
3333 return SYSCTL_OUT(req, buf, sizeof buf);
3335 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
3336 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
3338 return SYSCTL_OUT(req, buf, size);
3342 iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
3344 struct iwi_softc *sc = arg1;
3345 int val = !iwi_getrfkill(sc);
3347 return SYSCTL_OUT(req, &val, sizeof val);
3354 iwi_sysctlattach(struct iwi_softc *sc)
3356 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3357 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3359 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "radio",
3360 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I",
3361 "radio transmitter switch state (0=off, 1=on)");
3363 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "stats",
3364 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S",
3368 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "bluetooth",
3369 CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence");
3371 sc->antenna = IWI_ANTENNA_AUTO;
3372 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "antenna",
3373 CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)");
3379 * Different cards have different capabilities. Some have three
3380 * led's while others have only one. The linux ipw driver defines
3381 * led's for link state (associated or not), band (11a, 11g, 11b),
3382 * and for link activity. We use one led and vary the blink rate
3383 * according to the tx/rx traffic a la the ath driver.
3386 static __inline uint32_t
3387 iwi_toggle_event(uint32_t r)
3389 return r &~ (IWI_RST_STANDBY | IWI_RST_GATE_ODMA |
3390 IWI_RST_GATE_IDMA | IWI_RST_GATE_ADMA);
3394 iwi_read_event(struct iwi_softc *sc)
3396 return MEM_READ_4(sc, IWI_MEM_EEPROM_EVENT);
3400 iwi_write_event(struct iwi_softc *sc, uint32_t v)
3402 MEM_WRITE_4(sc, IWI_MEM_EEPROM_EVENT, v);
3406 iwi_led_done(void *arg)
3408 struct iwi_softc *sc = arg;
3410 sc->sc_blinking = 0;
3414 * Turn the activity LED off: flip the pin and then set a timer so no
3415 * update will happen for the specified duration.
3418 iwi_led_off(void *arg)
3420 struct iwi_softc *sc = arg;
3423 v = iwi_read_event(sc);
3424 v &= ~sc->sc_ledpin;
3425 iwi_write_event(sc, iwi_toggle_event(v));
3426 callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, iwi_led_done, sc);
3430 * Blink the LED according to the specified on/off times.
3433 iwi_led_blink(struct iwi_softc *sc, int on, int off)
3437 v = iwi_read_event(sc);
3439 iwi_write_event(sc, iwi_toggle_event(v));
3440 sc->sc_blinking = 1;
3441 sc->sc_ledoff = off;
3442 callout_reset(&sc->sc_ledtimer, on, iwi_led_off, sc);
3446 iwi_led_event(struct iwi_softc *sc, int event)
3448 #define N(a) (sizeof(a)/sizeof(a[0]))
3449 /* NB: on/off times from the Atheros NDIS driver, w/ permission */
3450 static const struct {
3451 u_int rate; /* tx/rx iwi rate */
3452 u_int16_t timeOn; /* LED on time (ms) */
3453 u_int16_t timeOff; /* LED off time (ms) */
3455 { IWI_RATE_OFDM54, 40, 10 },
3456 { IWI_RATE_OFDM48, 44, 11 },
3457 { IWI_RATE_OFDM36, 50, 13 },
3458 { IWI_RATE_OFDM24, 57, 14 },
3459 { IWI_RATE_OFDM18, 67, 16 },
3460 { IWI_RATE_OFDM12, 80, 20 },
3461 { IWI_RATE_DS11, 100, 25 },
3462 { IWI_RATE_OFDM9, 133, 34 },
3463 { IWI_RATE_OFDM6, 160, 40 },
3464 { IWI_RATE_DS5, 200, 50 },
3465 { 6, 240, 58 }, /* XXX 3Mb/s if it existed */
3466 { IWI_RATE_DS2, 267, 66 },
3467 { IWI_RATE_DS1, 400, 100 },
3468 { 0, 500, 130 }, /* unknown rate/polling */
3471 int j = 0; /* XXX silence compiler */
3473 sc->sc_ledevent = ticks; /* time of last event */
3474 if (sc->sc_blinking) /* don't interrupt active blink */
3478 j = N(blinkrates)-1;
3481 /* read current transmission rate from adapter */
3482 txrate = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
3483 if (blinkrates[sc->sc_txrix].rate != txrate) {
3484 for (j = 0; j < N(blinkrates)-1; j++)
3485 if (blinkrates[j].rate == txrate)
3492 if (blinkrates[sc->sc_rxrix].rate != sc->sc_rxrate) {
3493 for (j = 0; j < N(blinkrates)-1; j++)
3494 if (blinkrates[j].rate == sc->sc_rxrate)
3501 /* XXX beware of overflow */
3502 iwi_led_blink(sc, (blinkrates[j].timeOn * hz) / 1000,
3503 (blinkrates[j].timeOff * hz) / 1000);
3508 iwi_sysctl_softled(SYSCTL_HANDLER_ARGS)
3510 struct iwi_softc *sc = arg1;
3511 int softled = sc->sc_softled;
3514 error = sysctl_handle_int(oidp, &softled, 0, req);
3515 if (error || !req->newptr)
3517 softled = (softled != 0);
3518 if (softled != sc->sc_softled) {
3520 uint32_t v = iwi_read_event(sc);
3521 v &= ~sc->sc_ledpin;
3522 iwi_write_event(sc, iwi_toggle_event(v));
3524 sc->sc_softled = softled;
3530 iwi_ledattach(struct iwi_softc *sc)
3532 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
3533 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
3535 sc->sc_blinking = 0;
3536 sc->sc_ledstate = 1;
3537 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */
3538 callout_init_mtx(&sc->sc_ledtimer, &sc->sc_mtx, 0);
3540 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3541 "softled", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
3542 iwi_sysctl_softled, "I", "enable/disable software LED support");
3543 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3544 "ledpin", CTLFLAG_RW, &sc->sc_ledpin, 0,
3545 "pin setting to turn activity LED on");
3546 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3547 "ledidle", CTLFLAG_RW, &sc->sc_ledidle, 0,
3548 "idle time for inactivity LED (ticks)");
3549 /* XXX for debugging */
3550 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
3551 "nictype", CTLFLAG_RD, &sc->sc_nictype, 0,
3552 "NIC type from EEPROM");
3554 sc->sc_ledpin = IWI_RST_LED_ACTIVITY;
3557 sc->sc_nictype = (iwi_read_prom_word(sc, IWI_EEPROM_NIC) >> 8) & 0xff;
3558 if (sc->sc_nictype == 1) {
3560 * NB: led's are reversed.
3562 sc->sc_ledpin = IWI_RST_LED_ASSOCIATED;
3567 iwi_ops(void *arg, int npending)
3569 struct iwi_softc *sc = arg;
3570 struct ieee80211com *ic = &sc->sc_ic;
3576 cmd = sc->sc_cmd[sc->sc_cmd_cur];
3578 /* No more commands to process */
3582 sc->sc_cmd[sc->sc_cmd_cur] = 0; /* free the slot */
3583 sc->sc_cmd_cur = (sc->sc_cmd_cur + 1) % IWI_CMD_MAXOPS;
3587 while (sc->fw_state != IWI_FW_IDLE || (sc->flags & IWI_FLAG_BUSY)) {
3588 msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz/10);
3591 if (!(sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3598 iwi_auth_and_assoc(sc);
3601 iwi_disassociate(sc, 0);
3604 if (ic->ic_state == IEEE80211_S_RUN)
3605 (void) iwi_wme_setparams(sc);
3607 case IWI_SCAN_START:
3608 sc->flags |= IWI_FLAG_CHANNEL_SCAN;
3610 case IWI_SCAN_CURCHAN:
3611 case IWI_SCAN_ALLCHAN:
3612 if (!(sc->flags & IWI_FLAG_CHANNEL_SCAN)) {
3613 DPRINTF(("%s: ic_scan_curchan while not scanning\n",
3617 if (iwi_scanchan(sc, sc->sc_maxdwell, cmd))
3618 ieee80211_cancel_scan(ic);
3625 /* Take another pass */
3630 iwi_queue_cmd(struct iwi_softc *sc, int cmd)
3633 if (sc->sc_cmd[sc->sc_cmd_next] != 0) {
3635 DPRINTF(("%s: command %d dropped\n", __func__, cmd));
3639 sc->sc_cmd[sc->sc_cmd_next] = cmd;
3640 sc->sc_cmd_next = (sc->sc_cmd_next + 1) % IWI_CMD_MAXOPS;
3641 taskqueue_enqueue(sc->sc_tq, &sc->sc_opstask);
3647 iwi_scan_start(struct ieee80211com *ic)
3649 struct ifnet *ifp = ic->ic_ifp;
3650 struct iwi_softc *sc = ifp->if_softc;
3652 iwi_queue_cmd(sc, IWI_SCAN_START);
3656 iwi_set_channel(struct ieee80211com *ic)
3658 struct ifnet *ifp = ic->ic_ifp;
3659 struct iwi_softc *sc = ifp->if_softc;
3660 if (sc->fw_state == IWI_FW_IDLE)
3661 iwi_setcurchan(sc, ic->ic_curchan->ic_ieee);
3665 iwi_scan_curchan(struct ieee80211com *ic, unsigned long maxdwell)
3667 struct ifnet *ifp = ic->ic_ifp;
3668 struct iwi_softc *sc = ifp->if_softc;
3670 sc->sc_maxdwell = maxdwell;
3671 iwi_queue_cmd(sc, IWI_SCAN_CURCHAN);
3676 iwi_scan_allchan(struct ieee80211com *ic, unsigned long maxdwell)
3678 struct ifnet *ifp = ic->ic_ifp;
3679 struct iwi_softc *sc = ifp->if_softc;
3681 sc->sc_maxdwell = maxdwell;
3682 iwi_queue_cmd(sc, IWI_SCAN_ALLCHAN);
3687 iwi_scan_mindwell(struct ieee80211com *ic)
3689 /* NB: don't try to abort scan; wait for firmware to finish */
3693 iwi_scan_end(struct ieee80211com *ic)
3695 struct ifnet *ifp = ic->ic_ifp;
3696 struct iwi_softc *sc = ifp->if_softc;
3698 taskqueue_enqueue(sc->sc_tq2, &sc->sc_scanaborttask);
3702 iwi_assoc(struct ieee80211com *ic)
3704 struct ifnet *ifp = ic->ic_ifp;
3705 struct iwi_softc *sc = ifp->if_softc;
3707 /* The firmware will fail if we are already associated */
3708 if (sc->flags & IWI_FLAG_ASSOCIATED)
3711 iwi_queue_cmd(sc, IWI_ASSOC);
3715 iwi_disassoc(struct ieee80211com *ic)
3717 struct ifnet *ifp = ic->ic_ifp;
3718 struct iwi_softc *sc = ifp->if_softc;
3720 iwi_queue_cmd(sc, IWI_DISASSOC);