2 * Copyright (c) 2004-2006
3 * Damien Bergamini <damien.bergamini@free.fr>.
4 * Copyright (c) 2004, 2005
5 * Andrew Atrens <atrens@nortelnetworks.com>.
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
31 * $FreeBSD: src/sys/dev/iwi/if_iwi.c,v 1.8.2.6 2006/02/23 02:06:46 sam Exp $
32 * $DragonFly: src/sys/dev/netif/iwi/if_iwi.c,v 1.21 2008/05/14 11:59:20 sephe Exp $
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/kthread.h>
46 #include <sys/interrupt.h>
47 #include <sys/socket.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/endian.h>
54 #include <sys/ucred.h>
57 #include <sys/serialize.h>
59 #include <bus/pci/pcidevs.h>
60 #include <bus/pci/pcireg.h>
61 #include <bus/pci/pcivar.h>
65 #include <net/if_arp.h>
66 #include <net/ethernet.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69 #include <net/if_types.h>
70 #include <net/ifq_var.h>
72 #include <netproto/802_11/ieee80211_var.h>
73 #include <netproto/802_11/ieee80211_radiotap.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #include <netinet/if_ether.h>
81 #include <dev/netif/iwi/if_iwireg.h>
82 #include <dev/netif/iwi/if_iwivar.h>
85 #define DPRINTF(x) do { if (iwi_debug > 0) kprintf x; } while (0)
86 #define DPRINTFN(n, x) do { if (iwi_debug >= (n)) kprintf x; } while (0)
88 SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level");
91 #define DPRINTFN(n, x)
94 static struct iwi_ident {
98 } iwi_ident_table[] = {
99 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2200BG,
100 "Intel(R) PRO/Wireless 2200BG" },
101 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2225BG,
102 "Intel(R) PRO/Wireless 2225BG" },
103 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1,
104 "Intel(R) PRO/Wireless 2915ABG" },
105 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2,
106 "Intel(R) PRO/Wireless 2915ABG" },
110 static void iwi_fw_monitor(void *);
111 static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
112 static void iwi_dma_map_mbuf(void *, bus_dma_segment_t *, int, bus_size_t,
114 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
116 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
117 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
118 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
119 int, bus_addr_t, bus_addr_t);
120 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
121 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
122 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
124 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
125 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
126 static struct ieee80211_node *iwi_node_alloc(struct ieee80211_node_table *);
127 static void iwi_node_free(struct ieee80211_node *);
128 static int iwi_media_change(struct ifnet *);
129 static void iwi_media_status(struct ifnet *, struct ifmediareq *);
130 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
131 static int iwi_wme_update(struct ieee80211com *);
132 static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t);
133 static void iwi_fix_channel(struct ieee80211com *, struct mbuf *);
134 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
136 static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
137 static void iwi_rx_intr(struct iwi_softc *);
138 static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
139 static void iwi_intr(void *);
140 static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int);
141 static void iwi_write_ibssnode(struct iwi_softc *, const struct iwi_node *);
142 static int iwi_tx_start(struct ifnet *, struct mbuf *,
143 struct ieee80211_node *, int);
144 static void iwi_start(struct ifnet *);
145 static void iwi_watchdog(struct ifnet *);
146 static int iwi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
147 static void iwi_stop_master(struct iwi_softc *);
148 static int iwi_reset(struct iwi_softc *);
149 static int iwi_load_ucode(struct iwi_softc *, void *, int);
150 static int iwi_load_firmware(struct iwi_softc *, void *, int);
151 static int iwi_cache_firmware(struct iwi_softc *, void *);
152 static void iwi_free_firmware(struct iwi_softc *);
153 static int iwi_config(struct iwi_softc *);
154 static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
155 static int iwi_scan(struct iwi_softc *);
156 static int iwi_auth_and_assoc(struct iwi_softc *);
157 static void iwi_init(void *);
158 static void iwi_stop(void *);
159 static int iwi_sysctl_stats(SYSCTL_HANDLER_ARGS);
160 static int iwi_sysctl_radio(SYSCTL_HANDLER_ARGS);
162 static int iwi_probe(device_t);
163 static int iwi_attach(device_t);
164 static int iwi_detach(device_t);
165 static int iwi_shutdown(device_t);
166 static int iwi_suspend(device_t);
167 static int iwi_resume(device_t);
169 static int iwi_alloc_ibss_node(struct iwi_softc *);
170 static void iwi_free_ibss_node(struct iwi_softc *, int);
172 static device_method_t iwi_methods[] = {
173 /* Device interface */
174 DEVMETHOD(device_probe, iwi_probe),
175 DEVMETHOD(device_attach, iwi_attach),
176 DEVMETHOD(device_detach, iwi_detach),
177 DEVMETHOD(device_shutdown, iwi_shutdown),
178 DEVMETHOD(device_suspend, iwi_suspend),
179 DEVMETHOD(device_resume, iwi_resume),
184 static driver_t iwi_driver = {
187 sizeof (struct iwi_softc)
190 static devclass_t iwi_devclass;
192 DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, 0, 0);
194 MODULE_DEPEND(iwi, pci, 1, 1, 1);
195 MODULE_DEPEND(iwi, wlan, 1, 1, 1);
198 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
200 static const struct ieee80211_rateset iwi_rateset_11a =
201 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
203 static const struct ieee80211_rateset iwi_rateset_11b =
204 { 4, { 2, 4, 11, 22 } };
206 static const struct ieee80211_rateset iwi_rateset_11g =
207 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
210 iwi_fw_monitor(void *arg)
212 struct iwi_softc *sc = arg;
213 struct ifnet *ifp = &sc->sc_ic.ic_if;
215 lwkt_serialize_enter(ifp->if_serializer);
220 * Test to see whether we are detaching,
221 * this is used to avoid race condition
222 * especially when attaching fails.
224 if ((sc->flags & IWI_FLAG_EXIT) == 0) {
226 tsleep_interlock(IWI_FW_WAKE_MONITOR(sc));
227 lwkt_serialize_exit(ifp->if_serializer);
228 error = tsleep(IWI_FW_WAKE_MONITOR(sc),
231 lwkt_serialize_enter(ifp->if_serializer);
237 if (sc->flags & IWI_FLAG_EXIT)
239 else if ((sc->flags & IWI_FLAG_RESET) == 0)
242 if_printf(ifp, "reset firmware\n");
243 for (boff = 1; sc->flags & IWI_FLAG_RESET; boff++) {
245 if (sc->flags & IWI_FLAG_FW_INITED) {
246 sc->flags &= ~IWI_FLAG_RESET;
247 } else if (boff > 10) { /* XXX */
248 if_printf(ifp, "fw reset failed. "
251 /* XXX avoid to sleep to long */
256 * Since this would be infinite loop,
257 * if reseting firmware never succeeded,
258 * we test to see whether we are detaching.
260 if (sc->flags & IWI_FLAG_EXIT)
264 tsleep_interlock(IWI_FW_CMD_ACKED(sc));
265 lwkt_serialize_exit(ifp->if_serializer);
266 error = tsleep(IWI_FW_CMD_ACKED(sc), 0,
267 "iwirun", boff * hz);
269 lwkt_serialize_enter(ifp->if_serializer);
273 lwkt_serialize_exit(ifp->if_serializer);
275 if_printf(ifp, "fw monitor exiting\n");
276 wakeup(IWI_FW_EXIT_MONITOR(sc));
281 iwi_probe(device_t dev)
283 const struct iwi_ident *ident;
286 vid = pci_get_vendor(dev);
287 did = pci_get_device(dev);
288 for (ident = iwi_ident_table; ident->name != NULL; ident++) {
289 if (vid == ident->vendor && did == ident->device) {
290 device_set_desc(dev, ident->name);
297 /* Base Address Register */
298 #define IWI_PCI_BAR0 0x10
301 iwi_attach(device_t dev)
303 struct iwi_softc *sc = device_get_softc(dev);
304 struct ieee80211com *ic = &sc->sc_ic;
305 struct ifnet *ifp = &ic->ic_if;
310 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
312 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
313 device_printf(dev, "chip is in D%d power mode "
314 "-- setting to D0\n", pci_get_powerstate(dev));
315 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
318 pci_write_config(dev, 0x41, 0, 1);
320 /* enable bus-mastering */
321 pci_enable_busmaster(dev);
323 sc->mem_rid = IWI_PCI_BAR0;
324 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
326 if (sc->mem == NULL) {
327 device_printf(dev, "could not allocate memory resource\n");
330 sc->sc_st = rman_get_bustag(sc->mem);
331 sc->sc_sh = rman_get_bushandle(sc->mem);
334 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
335 RF_ACTIVE | RF_SHAREABLE);
336 if (sc->irq == NULL) {
337 device_printf(dev, "could not allocate interrupt resource\n");
341 if (iwi_reset(sc) != 0) {
342 device_printf(dev, "could not reset adapter\n");
349 error = iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT);
351 device_printf(dev, "could not allocate Cmd ring\n");
355 error = iwi_alloc_tx_ring(sc, &sc->txq[0], IWI_TX_RING_COUNT,
356 IWI_CSR_TX1_RIDX, IWI_CSR_TX1_WIDX);
358 device_printf(dev, "could not allocate Tx ring 1\n");
362 error = iwi_alloc_tx_ring(sc, &sc->txq[1], IWI_TX_RING_COUNT,
363 IWI_CSR_TX2_RIDX, IWI_CSR_TX2_WIDX);
365 device_printf(dev, "could not allocate Tx ring 2\n");
369 error = iwi_alloc_tx_ring(sc, &sc->txq[2], IWI_TX_RING_COUNT,
370 IWI_CSR_TX3_RIDX, IWI_CSR_TX3_WIDX);
372 device_printf(dev, "could not allocate Tx ring 3\n");
376 error = iwi_alloc_tx_ring(sc, &sc->txq[3], IWI_TX_RING_COUNT,
377 IWI_CSR_TX4_RIDX, IWI_CSR_TX4_WIDX);
379 device_printf(dev, "could not allocate Tx ring 4\n");
383 error = iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT);
385 device_printf(dev, "could not allocate Rx ring\n");
389 sysctl_ctx_init(&sc->sysctl_ctx);
390 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
391 SYSCTL_STATIC_CHILDREN(_hw),
393 device_get_nameunit(dev),
396 if (sc->sysctl_tree == NULL) {
397 device_printf(dev, "sysctl add node failed\n");
403 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
404 ifp->if_init = iwi_init;
405 ifp->if_ioctl = iwi_ioctl;
406 ifp->if_start = iwi_start;
407 ifp->if_watchdog = iwi_watchdog;
408 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
409 ifq_set_ready(&ifp->if_snd);
411 ic->ic_wme.wme_update = iwi_wme_update;
412 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
413 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
414 ic->ic_state = IEEE80211_S_INIT;
416 /* set device capabilities */
418 IEEE80211_C_IBSS | /* IBSS mode supported */
419 IEEE80211_C_MONITOR | /* monitor mode supported */
420 IEEE80211_C_TXPMGT | /* tx power management */
421 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
422 IEEE80211_C_WPA | /* 802.11i */
423 IEEE80211_C_WME; /* 802.11e */
425 /* read MAC address from EEPROM */
426 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
427 ic->ic_myaddr[0] = val & 0xff;
428 ic->ic_myaddr[1] = val >> 8;
429 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
430 ic->ic_myaddr[2] = val & 0xff;
431 ic->ic_myaddr[3] = val >> 8;
432 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
433 ic->ic_myaddr[4] = val & 0xff;
434 ic->ic_myaddr[5] = val >> 8;
436 if (pci_get_device(dev) >= 0x4223) {
437 /* set supported .11a rates (2915ABG only) */
438 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
440 /* set supported .11a channels */
441 for (i = 36; i <= 64; i += 4) {
442 ic->ic_channels[i].ic_freq =
443 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
444 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
446 for (i = 149; i <= 165; i += 4) {
447 ic->ic_channels[i].ic_freq =
448 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
449 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
453 /* set supported .11b and .11g rates */
454 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
455 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
457 /* set supported .11b and .11g channels (1 through 14) */
458 for (i = 1; i <= 14; i++) {
459 ic->ic_channels[i].ic_freq =
460 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
461 ic->ic_channels[i].ic_flags =
462 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
463 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
466 ieee80211_ifattach(ic);
467 /* override default methods */
468 ic->ic_node_alloc = iwi_node_alloc;
469 sc->sc_node_free = ic->ic_node_free;
470 ic->ic_node_free = iwi_node_free;
471 /* override state transition machine */
472 sc->sc_newstate = ic->ic_newstate;
473 ic->ic_newstate = iwi_newstate;
474 ieee80211_media_init(ic, iwi_media_change, iwi_media_status);
476 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
477 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
479 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
480 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
481 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
483 sc->sc_txtap_len = sizeof sc->sc_txtapu;
484 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
485 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
488 * Add a few sysctl knobs.
494 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
495 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "radio",
496 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I",
497 "radio transmitter switch state (0=off, 1=on)");
499 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
500 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "stats",
501 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S",
504 SYSCTL_ADD_INT(&sc->sysctl_ctx,
505 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "dwell",
506 CTLFLAG_RW, &sc->dwelltime, 0,
507 "channel dwell time (ms) for AP/station scanning");
509 SYSCTL_ADD_INT(&sc->sysctl_ctx,
510 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "bluetooth",
511 CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence");
513 SYSCTL_ADD_INT(&sc->sysctl_ctx,
514 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "antenna",
515 CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)");
518 * Start firmware monitoring thread
521 * This should be done only after serializer is initialized,
522 * i.e. after ieee80211_ifattach(), because serializer will be
523 * held once iwi_fw_monitor() is entered.
525 error = kthread_create(iwi_fw_monitor, sc, &sc->sc_fw_monitor,
526 "%s:fw-monitor", device_get_nameunit(dev));
528 device_printf(dev, "could not create fw monitor\n");
531 sc->flags |= IWI_FLAG_MONITOR;
534 * Hook our interrupt after all initialization is complete.
536 error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE, iwi_intr, sc,
537 &sc->sc_ih, ifp->if_serializer);
539 device_printf(dev, "could not set up interrupt\n");
543 ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->irq));
544 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
547 ieee80211_announce(ic);
553 ieee80211_ifdetach(ic);
560 iwi_detach(device_t dev)
562 struct iwi_softc *sc = device_get_softc(dev);
563 struct ieee80211com *ic = &sc->sc_ic;
564 struct ifnet *ifp = ic->ic_ifp;
566 if (sc->flags & IWI_FLAG_MONITOR) {
567 lwkt_serialize_enter(ifp->if_serializer);
568 sc->flags |= IWI_FLAG_EXIT;
569 wakeup(IWI_FW_WAKE_MONITOR(sc));
572 tsleep_interlock(IWI_FW_EXIT_MONITOR(sc));
573 lwkt_serialize_exit(ifp->if_serializer);
574 tsleep(IWI_FW_EXIT_MONITOR(sc), 0, "iwiexi", 0);
576 /* No need to hold serializer again */
578 if_printf(ifp, "fw monitor exited\n");
581 if (device_is_attached(dev)) {
582 lwkt_serialize_enter(ifp->if_serializer);
585 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
586 iwi_free_firmware(sc);
588 lwkt_serialize_exit(ifp->if_serializer);
591 ieee80211_ifdetach(ic);
594 iwi_free_cmd_ring(sc, &sc->cmdq);
595 iwi_free_tx_ring(sc, &sc->txq[0]);
596 iwi_free_tx_ring(sc, &sc->txq[1]);
597 iwi_free_tx_ring(sc, &sc->txq[2]);
598 iwi_free_tx_ring(sc, &sc->txq[3]);
599 iwi_free_rx_ring(sc, &sc->rxq);
602 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
605 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
607 if (sc->sysctl_tree != NULL)
608 sysctl_ctx_free(&sc->sysctl_ctx);
614 iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
619 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
621 *(bus_addr_t *)arg = segs[0].ds_addr;
625 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count)
631 ring->cur = ring->next = 0;
633 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
634 BUS_SPACE_MAXADDR, NULL, NULL, count * IWI_CMD_DESC_SIZE, 1,
635 count * IWI_CMD_DESC_SIZE, 0, &ring->desc_dmat);
637 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
641 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
642 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
644 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
648 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
649 count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
651 device_printf(sc->sc_dev, "could not load desc DMA map\n");
652 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
659 fail: iwi_free_cmd_ring(sc, ring);
664 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
667 ring->cur = ring->next = 0;
671 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
673 if (ring->desc != NULL) {
674 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
675 BUS_DMASYNC_POSTWRITE);
676 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
677 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
681 if (ring->desc_dmat != NULL) {
682 bus_dma_tag_destroy(ring->desc_dmat);
683 ring->desc_dmat = NULL;
688 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count,
689 bus_addr_t csr_ridx, bus_addr_t csr_widx)
695 ring->cur = ring->next = 0;
696 ring->csr_ridx = csr_ridx;
697 ring->csr_widx = csr_widx;
699 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
700 BUS_SPACE_MAXADDR, NULL, NULL, count * IWI_TX_DESC_SIZE, 1,
701 count * IWI_TX_DESC_SIZE, 0, &ring->desc_dmat);
703 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
707 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
708 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
710 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
714 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
715 count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0);
717 device_printf(sc->sc_dev, "could not load desc DMA map\n");
719 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
724 ring->data = kmalloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
727 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
728 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IWI_MAX_NSEG - 2,
729 MCLBYTES, 0, &ring->data_dmat);
731 device_printf(sc->sc_dev, "could not create data DMA tag\n");
735 for (i = 0; i < count; i++) {
736 error = bus_dmamap_create(ring->data_dmat, 0,
739 device_printf(sc->sc_dev, "could not create DMA map\n");
746 fail: iwi_free_tx_ring(sc, ring);
751 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
753 struct iwi_tx_data *data;
756 for (i = 0; i < ring->count; i++) {
757 data = &ring->data[i];
759 if (data->m != NULL) {
760 bus_dmamap_sync(ring->data_dmat, data->map,
761 BUS_DMASYNC_POSTWRITE);
762 bus_dmamap_unload(ring->data_dmat, data->map);
767 if (data->ni != NULL) {
768 ieee80211_free_node(data->ni);
774 ring->cur = ring->next = 0;
778 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
780 struct iwi_tx_data *data;
783 if (ring->desc != NULL) {
784 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
785 BUS_DMASYNC_POSTWRITE);
786 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
787 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
791 if (ring->desc_dmat != NULL) {
792 bus_dma_tag_destroy(ring->desc_dmat);
793 ring->desc_dmat = NULL;
796 if (ring->data != NULL) {
797 for (i = 0; i < ring->count; i++) {
798 data = &ring->data[i];
800 if (data->m != NULL) {
801 bus_dmamap_sync(ring->data_dmat, data->map,
802 BUS_DMASYNC_POSTWRITE);
803 bus_dmamap_unload(ring->data_dmat, data->map);
808 if (data->ni != NULL) {
809 ieee80211_free_node(data->ni);
813 if (data->map != NULL) {
814 bus_dmamap_destroy(ring->data_dmat, data->map);
819 kfree(ring->data, M_DEVBUF);
823 if (ring->data_dmat != NULL) {
824 bus_dma_tag_destroy(ring->data_dmat);
825 ring->data_dmat = NULL;
830 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
832 struct iwi_rx_data *data;
838 ring->data = kmalloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
841 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
842 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0,
845 device_printf(sc->sc_dev, "could not create data DMA tag\n");
849 for (i = 0; i < count; i++) {
850 data = &ring->data[i];
852 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
854 device_printf(sc->sc_dev, "could not create DMA map\n");
858 data->m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
859 if (data->m == NULL) {
860 device_printf(sc->sc_dev,
861 "could not allocate rx mbuf\n");
866 error = bus_dmamap_load(ring->data_dmat, data->map,
867 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
870 device_printf(sc->sc_dev,
871 "could not load rx buf DMA map");
878 data->reg = IWI_CSR_RX_BASE + i * 4;
883 fail: iwi_free_rx_ring(sc, ring);
888 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
894 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
896 struct iwi_rx_data *data;
899 if (ring->data != NULL) {
900 for (i = 0; i < ring->count; i++) {
901 data = &ring->data[i];
903 if (data->m != NULL) {
904 bus_dmamap_sync(ring->data_dmat, data->map,
905 BUS_DMASYNC_POSTREAD);
906 bus_dmamap_unload(ring->data_dmat, data->map);
911 if (data->map != NULL) {
912 bus_dmamap_destroy(ring->data_dmat, data->map);
917 kfree(ring->data, M_DEVBUF);
921 if (ring->data_dmat != NULL) {
922 bus_dma_tag_destroy(ring->data_dmat);
923 ring->data_dmat = NULL;
928 iwi_shutdown(device_t dev)
930 struct iwi_softc *sc = device_get_softc(dev);
931 struct ifnet *ifp = &sc->sc_ic.ic_if;
933 lwkt_serialize_enter(ifp->if_serializer);
935 lwkt_serialize_exit(ifp->if_serializer);
941 iwi_suspend(device_t dev)
943 struct iwi_softc *sc = device_get_softc(dev);
944 struct ifnet *ifp = &sc->sc_ic.ic_if;
946 lwkt_serialize_enter(ifp->if_serializer);
948 lwkt_serialize_exit(ifp->if_serializer);
954 iwi_resume(device_t dev)
956 struct iwi_softc *sc = device_get_softc(dev);
957 struct ifnet *ifp = sc->sc_ic.ic_ifp;
959 lwkt_serialize_enter(ifp->if_serializer);
961 pci_write_config(dev, 0x41, 0, 1);
963 if (ifp->if_flags & IFF_UP) {
964 ifp->if_init(ifp->if_softc);
965 if (ifp->if_flags & IFF_RUNNING)
969 lwkt_serialize_exit(ifp->if_serializer);
974 static struct ieee80211_node *
975 iwi_node_alloc(struct ieee80211_node_table *nt)
979 in = kmalloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
989 iwi_node_free(struct ieee80211_node *ni)
991 struct ieee80211com *ic = ni->ni_ic;
992 struct iwi_softc *sc = ic->ic_ifp->if_softc;
993 struct iwi_node *in = (struct iwi_node *)ni;
995 if (in->in_station != -1)
996 iwi_free_ibss_node(sc, in->in_station);
998 sc->sc_node_free(ni);
1002 iwi_media_change(struct ifnet *ifp)
1004 struct iwi_softc *sc = ifp->if_softc;
1007 ASSERT_SERIALIZED(ifp->if_serializer);
1009 error = ieee80211_media_change(ifp);
1010 if (error != ENETRESET)
1013 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
1019 * The firmware automatically adapts the transmit speed. We report its current
1023 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1025 struct iwi_softc *sc = ifp->if_softc;
1026 struct ieee80211com *ic = &sc->sc_ic;
1027 #define N(a) (sizeof (a) / sizeof (a[0]))
1028 static const struct {
1032 { IWI_RATE_DS1, 2 },
1033 { IWI_RATE_DS2, 4 },
1034 { IWI_RATE_DS5, 11 },
1035 { IWI_RATE_DS11, 22 },
1036 { IWI_RATE_OFDM6, 12 },
1037 { IWI_RATE_OFDM9, 18 },
1038 { IWI_RATE_OFDM12, 24 },
1039 { IWI_RATE_OFDM18, 36 },
1040 { IWI_RATE_OFDM24, 48 },
1041 { IWI_RATE_OFDM36, 72 },
1042 { IWI_RATE_OFDM48, 96 },
1043 { IWI_RATE_OFDM54, 108 },
1048 imr->ifm_status = IFM_AVALID;
1049 imr->ifm_active = IFM_IEEE80211;
1050 if (ic->ic_state == IEEE80211_S_RUN)
1051 imr->ifm_status |= IFM_ACTIVE;
1053 /* read current transmission rate from adapter */
1054 val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
1056 /* convert rate to 802.11 rate */
1057 for (i = 0; i < N(rates) && rates[i].val != val; i++);
1058 rate = (i < N(rates)) ? rates[i].rate : 0;
1060 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
1061 switch (ic->ic_opmode) {
1062 case IEEE80211_M_STA:
1065 case IEEE80211_M_IBSS:
1066 imr->ifm_active |= IFM_IEEE80211_ADHOC;
1069 case IEEE80211_M_MONITOR:
1070 imr->ifm_active |= IFM_IEEE80211_MONITOR;
1073 case IEEE80211_M_AHDEMO:
1074 case IEEE80211_M_HOSTAP:
1075 /* should not get there */
1082 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1084 struct ifnet *ifp = ic->ic_ifp;
1085 struct iwi_softc *sc = ifp->if_softc;
1086 enum ieee80211_state ostate;
1089 ostate = ic->ic_state;
1092 case IEEE80211_S_SCAN:
1093 if (sc->flags & IWI_FLAG_SCANNING)
1096 ieee80211_node_table_reset(&ic->ic_scan);
1097 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
1098 sc->flags |= IWI_FLAG_SCANNING;
1102 case IEEE80211_S_AUTH:
1103 iwi_auth_and_assoc(sc);
1106 case IEEE80211_S_RUN:
1107 if (ic->ic_opmode == IEEE80211_M_IBSS)
1108 iwi_auth_and_assoc(sc);
1109 else if (ic->ic_opmode == IEEE80211_M_MONITOR)
1110 iwi_set_chan(sc, ic->ic_ibss_chan);
1113 tmp = MEM_READ_4(sc, IWI_MEM_EVENT_CTL) & IWI_LED_MASK;
1114 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, tmp | IWI_LED_ASSOC);
1116 return sc->sc_newstate(ic, nstate,
1117 IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
1119 case IEEE80211_S_ASSOC:
1122 case IEEE80211_S_INIT:
1123 sc->flags &= ~IWI_FLAG_SCANNING;
1125 if (ostate != IEEE80211_S_RUN)
1129 tmp = MEM_READ_4(sc, IWI_MEM_EVENT_CTL) & IWI_LED_MASK;
1130 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, tmp & ~IWI_LED_ASSOC);
1134 ic->ic_state = nstate;
1140 * WME parameters coming from IEEE 802.11e specification. These values are
1141 * already declared in ieee80211_proto.c, but they are static so they can't
1144 static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
1145 { 0, 3, 5, 7, 0 }, /* WME_AC_BE */
1146 { 0, 3, 5, 10, 0 }, /* WME_AC_BK */
1147 { 0, 2, 4, 5, 188 }, /* WME_AC_VI */
1148 { 0, 2, 3, 4, 102 } /* WME_AC_VO */
1151 static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
1152 { 0, 3, 4, 6, 0 }, /* WME_AC_BE */
1153 { 0, 3, 4, 10, 0 }, /* WME_AC_BK */
1154 { 0, 2, 3, 4, 94 }, /* WME_AC_VI */
1155 { 0, 2, 2, 3, 47 } /* WME_AC_VO */
1159 iwi_wme_update(struct ieee80211com *ic)
1161 #define IWI_EXP2(v) htole16((1 << (v)) - 1)
1162 #define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
1163 struct iwi_softc *sc = ic->ic_ifp->if_softc;
1164 struct iwi_wme_params wme[3];
1165 const struct wmeParams *wmep;
1169 * We shall not override firmware default WME values if WME is not
1172 if (!(ic->ic_flags & IEEE80211_F_WME))
1175 for (ac = 0; ac < WME_NUM_AC; ac++) {
1176 /* set WME values for current operating mode */
1177 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
1178 wme[0].aifsn[ac] = wmep->wmep_aifsn;
1179 wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1180 wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1181 wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1182 wme[0].acm[ac] = wmep->wmep_acm;
1184 /* set WME values for CCK modulation */
1185 wmep = &iwi_wme_cck_params[ac];
1186 wme[1].aifsn[ac] = wmep->wmep_aifsn;
1187 wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1188 wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1189 wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1190 wme[1].acm[ac] = wmep->wmep_acm;
1192 /* set WME values for OFDM modulation */
1193 wmep = &iwi_wme_ofdm_params[ac];
1194 wme[2].aifsn[ac] = wmep->wmep_aifsn;
1195 wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1196 wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1197 wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1198 wme[2].acm[ac] = wmep->wmep_acm;
1201 DPRINTF(("Setting WME parameters\n"));
1202 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, wme, sizeof wme, 1);
1208 * Read 16 bits at address 'addr' from the serial EEPROM.
1211 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1217 /* Clock C once before the first command */
1218 IWI_EEPROM_CTL(sc, 0);
1219 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1220 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1221 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1223 /* Write start bit (1) */
1224 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1225 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1227 /* Write READ opcode (10) */
1228 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1229 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1230 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1231 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1233 /* Write address A7-A0 */
1234 for (n = 7; n >= 0; n--) {
1235 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1236 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1237 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1238 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1241 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1243 /* Read data Q15-Q0 */
1245 for (n = 15; n >= 0; n--) {
1246 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1247 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1248 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1249 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1252 IWI_EEPROM_CTL(sc, 0);
1254 /* Clear Chip Select and clock C */
1255 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1256 IWI_EEPROM_CTL(sc, 0);
1257 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1263 * XXX: Hack to set the current channel to the value advertised in beacons or
1264 * probe responses. Only used during AP detection.
1267 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
1269 struct ieee80211_frame *wh;
1271 uint8_t *frm, *efrm;
1273 wh = mtod(m, struct ieee80211_frame *);
1275 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1278 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1280 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1281 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1284 frm = (uint8_t *)(wh + 1);
1285 efrm = mtod(m, uint8_t *) + m->m_len;
1287 frm += 12; /* skip tstamp, bintval and capinfo fields */
1288 while (frm < efrm) {
1289 if (*frm == IEEE80211_ELEMID_DSPARMS)
1290 #if IEEE80211_CHAN_MAX < 255
1291 if (frm[2] <= IEEE80211_CHAN_MAX)
1293 ic->ic_curchan = &ic->ic_channels[frm[2]];
1300 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1301 struct iwi_frame *frame)
1303 struct ieee80211com *ic = &sc->sc_ic;
1304 struct ifnet *ifp = ic->ic_ifp;
1305 struct mbuf *mnew, *m;
1306 struct ieee80211_frame *wh;
1307 struct ieee80211_node *ni;
1310 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
1311 le16toh(frame->len), frame->chan, frame->rssi_dbm));
1313 if (le16toh(frame->len) < sizeof (struct ieee80211_frame))
1317 * Try to allocate a new mbuf for this ring element and load it before
1318 * processing the current mbuf. If the ring element cannot be loaded,
1319 * drop the received packet and reuse the old mbuf. In the unlikely
1320 * case that the old mbuf can't be reloaded either, explicitly panic.
1322 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1328 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1330 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1331 mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr,
1336 /* try to reload the old mbuf */
1337 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1338 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr,
1339 &data->physaddr, 0);
1341 /* very unlikely that it will fail... */
1342 panic("%s: could not load old rx mbuf",
1343 device_get_name(sc->sc_dev));
1350 * New mbuf successfully loaded, update Rx ring and continue
1355 CSR_WRITE_4(sc, data->reg, data->physaddr);
1358 m->m_pkthdr.rcvif = ifp;
1359 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1360 sizeof (struct iwi_frame) + le16toh(frame->len);
1362 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1364 if (ic->ic_state == IEEE80211_S_SCAN)
1365 iwi_fix_channel(ic, m);
1367 if (sc->sc_drvbpf != NULL) {
1368 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1371 tap->wr_rate = frame->rate;
1373 htole16(ic->ic_channels[frame->chan].ic_freq);
1374 tap->wr_chan_flags =
1375 htole16(ic->ic_channels[frame->chan].ic_flags);
1376 tap->wr_antsignal = frame->signal;
1377 tap->wr_antenna = frame->antenna;
1379 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1382 wh = mtod(m, struct ieee80211_frame *);
1383 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1385 /* send the frame to the 802.11 layer */
1386 ieee80211_input(ic, m, ni, frame->rssi_dbm, 0);
1388 /* node is no longer needed */
1389 ieee80211_free_node(ni);
1393 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1395 struct ieee80211com *ic = &sc->sc_ic;
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;
1401 switch (notif->type) {
1402 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1403 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1405 DPRINTFN(2, ("Scanning channel (%u)\n", chan->nchan));
1408 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1409 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1411 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1414 /* monitor mode uses scan to set the channel ... */
1415 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1416 sc->flags &= ~IWI_FLAG_SCANNING;
1417 ieee80211_end_scan(ic);
1419 iwi_set_chan(sc, ic->ic_ibss_chan);
1422 case IWI_NOTIF_TYPE_AUTHENTICATION:
1423 auth = (struct iwi_notif_authentication *)(notif + 1);
1425 DPRINTFN(2, ("Authentication (%u)\n", auth->state));
1427 switch (auth->state) {
1428 case IWI_AUTHENTICATED:
1429 ieee80211_node_authorize(ic->ic_bss);
1430 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1433 case IWI_DEAUTHENTICATED:
1437 device_printf(sc->sc_dev,
1438 "unknown authentication state %u\n", auth->state);
1442 case IWI_NOTIF_TYPE_ASSOCIATION:
1443 assoc = (struct iwi_notif_association *)(notif + 1);
1445 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
1448 switch (assoc->state) {
1449 case IWI_AUTHENTICATED:
1450 /* re-association, do nothing */
1453 case IWI_ASSOCIATED:
1454 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1457 case IWI_DEASSOCIATED:
1458 ieee80211_begin_scan(ic, 1);
1462 device_printf(sc->sc_dev,
1463 "unknown association state %u\n", assoc->state);
1468 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1473 iwi_rx_intr(struct iwi_softc *sc)
1475 struct iwi_rx_data *data;
1476 struct iwi_hdr *hdr;
1479 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1481 for (; sc->rxq.cur != hw;) {
1482 data = &sc->rxq.data[sc->rxq.cur];
1484 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1485 BUS_DMASYNC_POSTREAD);
1487 hdr = mtod(data->m, struct iwi_hdr *);
1489 switch (hdr->type) {
1490 case IWI_HDR_TYPE_FRAME:
1491 iwi_frame_intr(sc, data, sc->rxq.cur,
1492 (struct iwi_frame *)(hdr + 1));
1495 case IWI_HDR_TYPE_NOTIF:
1496 iwi_notification_intr(sc,
1497 (struct iwi_notif *)(hdr + 1));
1501 device_printf(sc->sc_dev, "unknown hdr type %u\n",
1505 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1507 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT;
1510 /* Tell the firmware what we have processed */
1511 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1;
1512 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1516 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1518 struct ieee80211com *ic = &sc->sc_ic;
1519 struct ifnet *ifp = ic->ic_ifp;
1520 struct iwi_tx_data *data;
1523 hw = CSR_READ_4(sc, txq->csr_ridx);
1525 for (; txq->next != hw;) {
1526 data = &txq->data[txq->next];
1528 bus_dmamap_sync(txq->data_dmat, data->map,
1529 BUS_DMASYNC_POSTWRITE);
1530 bus_dmamap_unload(txq->data_dmat, data->map);
1533 ieee80211_free_node(data->ni);
1536 DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1541 txq->next = (txq->next + 1) % IWI_TX_RING_COUNT;
1544 sc->sc_tx_timer = 0;
1545 ifp->if_flags &= ~IFF_OACTIVE;
1552 struct iwi_softc *sc = arg;
1555 r = CSR_READ_4(sc, IWI_CSR_INTR);
1556 if (r == 0 || r == 0xffffffff)
1559 /* disable interrupts */
1560 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1562 if (r & IWI_INTR_FATAL_ERROR) {
1563 device_printf(sc->sc_dev, "fatal error\n");
1565 if ((sc->flags & (IWI_FLAG_EXIT | IWI_FLAG_RESET)) == 0) {
1566 sc->flags |= IWI_FLAG_RESET;
1567 device_printf(sc->sc_dev, "wake firmware monitor\n");
1568 wakeup(IWI_FW_WAKE_MONITOR(sc));
1572 if (r & IWI_INTR_PARITY_ERROR) {
1573 device_printf(sc->sc_dev, "parity error\n");
1574 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1578 if (r & IWI_INTR_FW_INITED) {
1579 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1580 wakeup(IWI_FW_INITIALIZED(sc));
1583 if (r & IWI_INTR_RADIO_OFF) {
1584 DPRINTF(("radio transmitter turned off\n"));
1585 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1589 if (r & IWI_INTR_CMD_DONE)
1590 wakeup(IWI_FW_CMD_ACKED(sc));
1592 if (r & IWI_INTR_TX1_DONE)
1593 iwi_tx_intr(sc, &sc->txq[0]);
1595 if (r & IWI_INTR_TX2_DONE)
1596 iwi_tx_intr(sc, &sc->txq[1]);
1598 if (r & IWI_INTR_TX3_DONE)
1599 iwi_tx_intr(sc, &sc->txq[2]);
1601 if (r & IWI_INTR_TX4_DONE)
1602 iwi_tx_intr(sc, &sc->txq[3]);
1604 if (r & IWI_INTR_RX_DONE)
1607 /* acknowledge interrupts */
1608 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1610 /* re-enable interrupts */
1611 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1615 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len, int async)
1617 struct iwi_cmd_desc *desc;
1618 struct ifnet *ifp = &sc->sc_ic.ic_if;
1621 desc = &sc->cmdq.desc[sc->cmdq.cur];
1623 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1624 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1627 memcpy(desc->data, data, len);
1629 bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map,
1630 BUS_DMASYNC_PREWRITE);
1632 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur,
1635 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT;
1636 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1639 ASSERT_SERIALIZED(ifp->if_serializer);
1642 tsleep_interlock(IWI_FW_CMD_ACKED(sc));
1643 lwkt_serialize_exit(ifp->if_serializer);
1644 ret = tsleep(IWI_FW_CMD_ACKED(sc), 0, "iwicmd", hz);
1646 lwkt_serialize_enter(ifp->if_serializer);
1655 iwi_write_ibssnode(struct iwi_softc *sc, const struct iwi_node *in)
1657 struct iwi_ibssnode node;
1659 /* write node information into NIC memory */
1660 memset(&node, 0, sizeof node);
1661 IEEE80211_ADDR_COPY(node.bssid, in->in_node.ni_macaddr);
1663 CSR_WRITE_REGION_1(sc,
1664 IWI_CSR_NODE_BASE + in->in_station * sizeof node,
1665 (uint8_t *)&node, sizeof node);
1668 struct iwi_dma_mapping {
1669 bus_dma_segment_t segs[IWI_MAX_NSEG];
1675 iwi_dma_map_mbuf(void *arg, bus_dma_segment_t *segs, int nseg,
1676 bus_size_t mapsize, int error)
1678 struct iwi_dma_mapping *map = arg;
1683 KASSERT(nseg <= IWI_MAX_NSEG, ("too many DMA segments %d", nseg));
1685 bcopy(segs, map->segs, nseg * sizeof(bus_dma_segment_t));
1687 map->mapsize = mapsize;
1691 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni,
1694 struct iwi_softc *sc = ifp->if_softc;
1695 struct ieee80211com *ic = &sc->sc_ic;
1696 struct iwi_node *in = (struct iwi_node *)ni;
1697 struct ieee80211_frame *wh;
1698 struct ieee80211_key *k;
1699 const struct chanAccParams *cap;
1700 struct iwi_tx_ring *txq = &sc->txq[ac];
1701 struct iwi_tx_data *data;
1702 struct iwi_tx_desc *desc;
1704 struct iwi_dma_mapping map;
1705 int error, hdrlen, i, noack = 0;
1707 wh = mtod(m0, struct ieee80211_frame *);
1709 if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
1710 hdrlen = sizeof (struct ieee80211_qosframe);
1711 cap = &ic->ic_wme.wme_chanParams;
1712 noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1714 hdrlen = sizeof (struct ieee80211_frame);
1717 * This is only used in IBSS mode where the firmware expect an index
1718 * in a h/w table instead of a destination address.
1720 if (ic->ic_opmode == IEEE80211_M_IBSS && in->in_station == -1) {
1721 in->in_station = iwi_alloc_ibss_node(sc);
1722 if (in->in_station == -1) { /* h/w table is full */
1724 ieee80211_free_node(ni);
1726 if_printf(ifp, "ibss table is full\n");
1729 iwi_write_ibssnode(sc, in);
1732 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1733 k = ieee80211_crypto_encap(ic, ni, m0);
1739 /* packet header may have moved, reset our local pointer */
1740 wh = mtod(m0, struct ieee80211_frame *);
1743 if (sc->sc_drvbpf != NULL) {
1744 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1747 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1748 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1750 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1753 data = &txq->data[txq->cur];
1754 desc = &txq->desc[txq->cur];
1756 /* save and trim IEEE802.11 header */
1757 m_copydata(m0, 0, hdrlen, (caddr_t)&desc->wh);
1760 error = bus_dmamap_load_mbuf(txq->data_dmat, data->map, m0,
1761 iwi_dma_map_mbuf, &map, BUS_DMA_NOWAIT);
1762 if (error != 0 && error != EFBIG) {
1763 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1769 mnew = m_defrag(m0, MB_DONTWAIT);
1771 device_printf(sc->sc_dev,
1772 "could not defragment mbuf\n");
1778 error = bus_dmamap_load_mbuf(txq->data_dmat, data->map, m0,
1779 iwi_dma_map_mbuf, &map,
1782 device_printf(sc->sc_dev,
1783 "could not map mbuf (error %d)\n", error);
1792 desc->hdr.type = IWI_HDR_TYPE_DATA;
1793 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1795 (ic->ic_opmode == IEEE80211_M_IBSS) ? in->in_station : 0;
1796 desc->cmd = IWI_DATA_CMD_TX;
1797 desc->len = htole16(m0->m_pkthdr.len);
1801 if (!noack && !IEEE80211_IS_MULTICAST(desc->wh.i_addr1))
1802 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1805 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1806 desc->wh.i_fc[1] |= IEEE80211_FC1_WEP;
1807 desc->weptxkey = ic->ic_crypto.cs_def_txkey;
1810 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1812 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1813 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1815 if (desc->wh.i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS)
1816 desc->xflags |= IWI_DATA_XFLAG_QOS;
1818 desc->nseg = htole32(map.nseg);
1819 for (i = 0; i < map.nseg; i++) {
1820 desc->seg_addr[i] = htole32(map.segs[i].ds_addr);
1821 desc->seg_len[i] = htole16(map.segs[i].ds_len);
1824 bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1825 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1827 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
1828 ac, txq->cur, le16toh(desc->len), map.nseg));
1831 txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT;
1832 CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1838 iwi_start(struct ifnet *ifp)
1840 struct iwi_softc *sc = ifp->if_softc;
1841 struct ieee80211com *ic = &sc->sc_ic;
1843 struct ether_header *eh;
1844 struct ieee80211_node *ni;
1847 ieee80211_drain_mgtq(&ic->ic_mgtq);
1848 if (ic->ic_state != IEEE80211_S_RUN) {
1849 ifq_purge(&ifp->if_snd);
1854 m0 = ifq_dequeue(&ifp->if_snd, NULL);
1858 if (m0->m_len < sizeof (struct ether_header) &&
1859 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) {
1863 eh = mtod(m0, struct ether_header *);
1864 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1871 /* classify mbuf so we can find which tx ring to use */
1872 if (ieee80211_classify(ic, m0, ni) != 0) {
1874 ieee80211_free_node(ni);
1879 /* no QoS encapsulation for EAPOL frames */
1880 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1881 M_WME_GETAC(m0) : WME_AC_BE;
1883 if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) {
1884 /* there is no place left in this ring */
1886 ieee80211_free_node(ni);
1887 ifp->if_flags |= IFF_OACTIVE;
1893 m0 = ieee80211_encap(ic, m0, ni);
1895 ieee80211_free_node(ni);
1900 if (ic->ic_rawbpf != NULL)
1901 bpf_mtap(ic->ic_rawbpf, m0);
1903 if (iwi_tx_start(ifp, m0, ni, ac) != 0) {
1904 ieee80211_free_node(ni);
1909 sc->sc_tx_timer = 5;
1915 iwi_watchdog(struct ifnet *ifp)
1917 struct iwi_softc *sc = ifp->if_softc;
1918 struct ieee80211com *ic = &sc->sc_ic;
1922 if (sc->sc_tx_timer > 0) {
1923 if (--sc->sc_tx_timer == 0) {
1924 if_printf(ifp, "device timeout\n");
1926 sc->flags |= IWI_FLAG_RESET;
1927 wakeup(IWI_FW_WAKE_MONITOR(sc));
1933 ieee80211_watchdog(ic);
1937 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1939 struct iwi_softc *sc = ifp->if_softc;
1940 struct ieee80211com *ic = &sc->sc_ic;
1946 if (ifp->if_flags & IFF_UP) {
1947 if (!(ifp->if_flags & IFF_RUNNING))
1950 if (ifp->if_flags & IFF_RUNNING)
1956 /* only super-user can do that! */
1957 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1961 ifr = (struct ifreq *)data;
1962 error = iwi_cache_firmware(sc, ifr->ifr_data);
1966 /* only super-user can do that! */
1967 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1971 ifp->if_flags &= ~IFF_UP;
1973 iwi_free_firmware(sc);
1977 error = ieee80211_ioctl(ic, cmd, data, cr);
1980 if (error == ENETRESET) {
1981 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1982 (IFF_UP | IFF_RUNNING) &&
1983 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1992 iwi_stop_master(struct iwi_softc *sc)
1997 /* disable interrupts */
1998 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
2000 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
2001 for (ntries = 0; ntries < 5; ntries++) {
2002 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2007 if_printf(&sc->sc_ic.ic_if, "timeout waiting for master\n");
2009 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2010 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET);
2012 sc->flags &= ~IWI_FLAG_FW_INITED;
2016 iwi_reset(struct iwi_softc *sc)
2021 iwi_stop_master(sc);
2023 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2024 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2026 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
2028 /* Wait for clock stabilization */
2029 for (ntries = 0; ntries < 1000; ntries++) {
2030 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
2034 if (ntries == 1000) {
2035 if_printf(&sc->sc_ic.ic_if,
2036 "timeout waiting for clock stabilization\n");
2040 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2041 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET);
2045 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2046 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT);
2048 /* Clear NIC memory */
2049 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
2050 for (i = 0; i < 0xc000; i++)
2051 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2057 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
2063 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
2064 IWI_RST_STOP_MASTER);
2065 for (ntries = 0; ntries < 5; ntries++) {
2066 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2071 device_printf(sc->sc_dev, "timeout waiting for master\n");
2075 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2078 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2079 tmp &= ~IWI_RST_PRINCETON_RESET;
2080 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2083 MEM_WRITE_4(sc, 0x3000e0, 0);
2085 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, 1);
2087 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, 0);
2089 MEM_WRITE_1(sc, 0x200000, 0x00);
2090 MEM_WRITE_1(sc, 0x200000, 0x40);
2093 /* write microcode into adapter memory */
2094 for (w = uc; size > 0; w++, size -= 2)
2095 MEM_WRITE_2(sc, 0x200010, htole16(*w));
2097 MEM_WRITE_1(sc, 0x200000, 0x00);
2098 MEM_WRITE_1(sc, 0x200000, 0x80);
2100 /* wait until we get an answer */
2101 for (ntries = 0; ntries < 100; ntries++) {
2102 if (MEM_READ_1(sc, 0x200000) & 1)
2106 if (ntries == 100) {
2107 device_printf(sc->sc_dev,
2108 "timeout waiting for ucode to initialize\n");
2112 /* read the answer or the firmware will not initialize properly */
2113 for (i = 0; i < 7; i++)
2114 MEM_READ_4(sc, 0x200004);
2116 MEM_WRITE_1(sc, 0x200000, 0x00);
2121 /* macro to handle unaligned little endian data in firmware image */
2122 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2125 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
2129 bus_addr_t physaddr;
2132 uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp;
2133 int ntries, error = 0;
2134 struct ifnet *ifp = &sc->sc_ic.ic_if;
2136 ASSERT_SERIALIZED(ifp->if_serializer);
2138 /* Allocate DMA memory for mapping firmware image */
2139 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
2140 BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size, 0, &dmat);
2142 device_printf(sc->sc_dev,
2143 "could not create firmware DMA tag\n");
2147 error = bus_dmamem_alloc(dmat, &virtaddr, BUS_DMA_WAITOK, &map);
2149 device_printf(sc->sc_dev,
2150 "could not allocate firmware DMA memory\n");
2154 error = bus_dmamap_load(dmat, map, virtaddr, size, iwi_dma_map_addr,
2157 device_printf(sc->sc_dev, "could not load firmware DMA map\n");
2161 /* Copy firmware image to DMA memory */
2162 memcpy(virtaddr, fw, size);
2164 /* Make sure the adapter will get up-to-date values */
2165 bus_dmamap_sync(dmat, map, BUS_DMASYNC_PREWRITE);
2167 /* Tell the adapter where the command blocks are stored */
2168 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2171 * Store command blocks into adapter's internal memory using register
2172 * indirections. The adapter will read the firmware image through DMA
2173 * using information stored in command blocks.
2178 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2181 dst = GETLE32(p); p += 4; src += 4;
2182 len = GETLE32(p); p += 4; src += 4;
2186 mlen = min(len, IWI_CB_MAXDATALEN);
2188 ctl = IWI_CB_DEFAULT_CTL | mlen;
2189 sum = ctl ^ src ^ dst;
2191 /* Write a command block */
2192 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2193 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2194 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2195 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2203 /* Write a fictive final command block (sentinel) */
2204 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2205 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2207 tmp = CSR_READ_4(sc, IWI_CSR_RST);
2208 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER);
2209 CSR_WRITE_4(sc, IWI_CSR_RST, tmp);
2211 /* Tell the adapter to start processing command blocks */
2212 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2214 /* Wait until the adapter reaches the sentinel */
2215 for (ntries = 0; ntries < 400; ntries++) {
2216 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2220 if (ntries == 400) {
2221 device_printf(sc->sc_dev,
2222 "timeout processing command blocks\n");
2227 /* We're done with command blocks processing */
2228 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2230 /* Allow interrupts so we know when the firmware is ready */
2231 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2233 /* Tell the adapter to initialize the firmware */
2234 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2236 tmp = CSR_READ_4(sc, IWI_CSR_CTL);
2237 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY);
2239 /* wait at most one second for firmware initialization to complete */
2241 tsleep_interlock(IWI_FW_INITIALIZED(sc));
2242 lwkt_serialize_exit(ifp->if_serializer);
2243 error = tsleep(IWI_FW_INITIALIZED(sc), 0, "iwiinit", hz);
2245 lwkt_serialize_enter(ifp->if_serializer);
2247 device_printf(sc->sc_dev, "timeout waiting for firmware "
2248 "initialization to complete\n");
2252 fail4: bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTWRITE);
2253 bus_dmamap_unload(dmat, map);
2254 fail3: bus_dmamem_free(dmat, virtaddr, map);
2255 fail2: bus_dma_tag_destroy(dmat);
2261 * Store firmware into kernel memory so we can download it when we need to,
2262 * e.g when the adapter wakes up from suspend mode.
2265 iwi_cache_firmware(struct iwi_softc *sc, void *data)
2267 struct iwi_firmware *kfw = &sc->fw;
2268 struct iwi_firmware ufw;
2271 iwi_free_firmware(sc);
2273 if ((error = copyin(data, &ufw, sizeof ufw)) != 0)
2276 kfw->boot_size = ufw.boot_size;
2277 kfw->ucode_size = ufw.ucode_size;
2278 kfw->main_size = ufw.main_size;
2280 kfw->boot = kmalloc(kfw->boot_size, M_DEVBUF, M_WAITOK);
2281 kfw->ucode = kmalloc(kfw->ucode_size, M_DEVBUF, M_WAITOK);
2282 kfw->main = kmalloc(kfw->main_size, M_DEVBUF, M_WAITOK);
2284 if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0)
2287 if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0)
2290 if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0)
2293 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
2294 kfw->boot_size, kfw->ucode_size, kfw->main_size));
2296 sc->flags |= IWI_FLAG_FW_CACHED;
2301 kfree(kfw->boot, M_DEVBUF);
2302 kfree(kfw->ucode, M_DEVBUF);
2303 kfree(kfw->main, M_DEVBUF);
2309 iwi_free_firmware(struct iwi_softc *sc)
2311 if (!(sc->flags & IWI_FLAG_FW_CACHED))
2314 kfree(sc->fw.boot, M_DEVBUF);
2315 kfree(sc->fw.ucode, M_DEVBUF);
2316 kfree(sc->fw.main, M_DEVBUF);
2318 sc->flags &= ~IWI_FLAG_FW_CACHED;
2322 iwi_config(struct iwi_softc *sc)
2324 struct ieee80211com *ic = &sc->sc_ic;
2325 struct ifnet *ifp = ic->ic_ifp;
2326 struct iwi_configuration config;
2327 struct iwi_rateset rs;
2328 struct iwi_txpower power;
2329 struct ieee80211_key *wk;
2330 struct iwi_wep_key wepkey;
2334 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2335 DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":"));
2336 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2337 IEEE80211_ADDR_LEN, 0);
2341 memset(&config, 0, sizeof config);
2342 config.bluetooth_coexistence = sc->bluetooth;
2343 config.antenna = sc->antenna;
2344 config.multicast_enabled = 1;
2345 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2346 config.disable_unicast_decryption = 1;
2347 config.disable_multicast_decryption = 1;
2348 DPRINTF(("Configuring adapter\n"));
2349 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 0);
2353 data = htole32(IWI_POWER_MODE_CAM);
2354 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2355 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
2359 data = htole32(ic->ic_rtsthreshold);
2360 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2361 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
2365 data = htole32(ic->ic_fragthreshold);
2366 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2367 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
2371 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2372 power.mode = IWI_MODE_11B;
2374 for (i = 0; i < 11; i++) {
2375 power.chan[i].chan = i + 1;
2376 power.chan[i].power = IWI_TXPOWER_MAX;
2378 DPRINTF(("Setting .11b channels tx power\n"));
2379 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2384 power.mode = IWI_MODE_11G;
2385 DPRINTF(("Setting .11g channels tx power\n"));
2386 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2392 rs.mode = IWI_MODE_11G;
2393 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2394 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2395 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2397 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2398 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2402 rs.mode = IWI_MODE_11A;
2403 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2404 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2405 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2407 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2408 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2412 /* if we have a desired ESSID, set it now */
2413 if (ic->ic_des_esslen != 0) {
2415 if (iwi_debug > 0) {
2416 kprintf("Setting desired ESSID to ");
2417 ieee80211_print_essid(ic->ic_des_essid,
2422 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_essid,
2423 ic->ic_des_esslen, 0);
2428 data = htole32(karc4random());
2429 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2430 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
2434 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2435 wk = &ic->ic_crypto.cs_nw_keys[i];
2437 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2439 wepkey.len = wk->wk_keylen;
2440 memset(wepkey.key, 0, sizeof wepkey.key);
2441 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2442 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2444 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2450 /* Enable adapter */
2451 DPRINTF(("Enabling adapter\n"));
2452 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
2456 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
2458 struct ieee80211com *ic = &sc->sc_ic;
2459 struct iwi_scan scan;
2461 memset(&scan, 0, sizeof scan);
2462 memset(scan.type, IWI_SCAN_TYPE_PASSIVE, sizeof scan.type);
2463 scan.passive = htole16(2000);
2464 scan.channels[0] = 1 |
2465 (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ);
2466 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2468 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
2469 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2473 iwi_scan(struct iwi_softc *sc)
2475 struct ieee80211com *ic = &sc->sc_ic;
2476 struct iwi_scan scan;
2480 memset(&scan, 0, sizeof scan);
2482 if (ic->ic_des_esslen != 0) {
2483 scan.bdirected = htole16(sc->dwelltime);
2484 memset(scan.type, IWI_SCAN_TYPE_BDIRECTED, sizeof scan.type);
2486 scan.broadcast = htole16(sc->dwelltime);
2487 memset(scan.type, IWI_SCAN_TYPE_BROADCAST, sizeof scan.type);
2492 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2493 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
2494 isset(ic->ic_chan_active, i)) {
2499 *(p - count) = IWI_CHAN_5GHZ | count;
2501 p = (count > 0) ? p + 1 : scan.channels;
2503 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2504 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
2505 isset(ic->ic_chan_active, i)) {
2510 *(p - count) = IWI_CHAN_2GHZ | count;
2512 DPRINTF(("Start scanning\n"));
2513 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2517 iwi_auth_and_assoc(struct iwi_softc *sc)
2519 struct ieee80211com *ic = &sc->sc_ic;
2520 struct ifnet *ifp = ic->ic_ifp;
2521 struct ieee80211_node *ni = ic->ic_bss;
2522 struct ieee80211_wme_info wme;
2523 struct iwi_configuration config;
2524 struct iwi_associate assoc;
2525 struct iwi_rateset rs;
2530 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2531 memset(&config, 0, sizeof config);
2532 config.bluetooth_coexistence = sc->bluetooth;
2533 config.antenna = sc->antenna;
2534 config.multicast_enabled = 1;
2535 config.use_protection = 1;
2536 config.answer_pbreq =
2537 (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2538 config.disable_unicast_decryption = 1;
2539 config.disable_multicast_decryption = 1;
2540 DPRINTF(("Configuring adapter\n"));
2541 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config,
2548 if (iwi_debug > 0) {
2549 kprintf("Setting ESSID to ");
2550 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2554 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2558 /* the rate set has already been "negotiated" */
2559 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2561 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2562 rs.nrates = ni->ni_rates.rs_nrates;
2563 KKASSERT(rs.nrates <= IWI_RATESET_MAXSIZE);
2564 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2565 DPRINTF(("Setting negociated rates (%u)\n", rs.nrates));
2566 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2570 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) {
2571 wme.wme_id = IEEE80211_ELEMID_VENDOR;
2572 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
2573 wme.wme_oui[0] = 0x00;
2574 wme.wme_oui[1] = 0x50;
2575 wme.wme_oui[2] = 0xf2;
2576 wme.wme_type = WME_OUI_TYPE;
2577 wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
2578 wme.wme_version = WME_VERSION;
2581 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
2582 error = iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme, 1);
2587 if (ic->ic_opt_ie != NULL) {
2588 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
2589 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
2590 ic->ic_opt_ie_len, 1);
2595 data = htole32(ni->ni_rssi);
2596 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2597 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2601 memset(&assoc, 0, sizeof assoc);
2602 assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2604 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2605 if (ni->ni_authmode == IEEE80211_AUTH_SHARED)
2606 assoc.auth = ic->ic_crypto.cs_def_txkey << 4 | IWI_AUTH_SHARED;
2607 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
2608 assoc.policy |= htole16(IWI_POLICY_WME);
2609 if (ic->ic_flags & IEEE80211_F_WPA)
2610 assoc.policy |= htole16(IWI_POLICY_WPA);
2611 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
2613 if (ic->ic_opmode == IEEE80211_M_IBSS)
2614 capinfo = IEEE80211_CAPINFO_IBSS;
2616 capinfo = IEEE80211_CAPINFO_ESS;
2617 if (ic->ic_flags & IEEE80211_F_PRIVACY)
2618 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2619 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2620 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2621 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2622 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2623 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2624 assoc.capinfo = htole16(capinfo);
2626 assoc.lintval = htole16(ic->ic_lintval);
2627 assoc.intval = htole16(ni->ni_intval);
2628 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
2629 if (ic->ic_opmode == IEEE80211_M_IBSS)
2630 IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr);
2632 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
2634 DPRINTF(("Trying to associate to %6D channel %u auth %u\n",
2635 assoc.bssid, ":", assoc.chan, assoc.auth));
2636 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
2640 iwi_init(void *priv)
2642 struct iwi_softc *sc = priv;
2643 struct ieee80211com *ic = &sc->sc_ic;
2644 struct ifnet *ifp = ic->ic_ifp;
2645 struct iwi_firmware *fw = &sc->fw;
2646 struct iwi_rx_data *data;
2649 /* exit immediately if firmware has not been ioctl'd */
2650 if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
2651 if (!(sc->flags & IWI_FLAG_FW_WARNED))
2652 device_printf(sc->sc_dev, "Please load firmware\n");
2653 sc->flags |= IWI_FLAG_FW_WARNED;
2654 ifp->if_flags &= ~IFF_UP;
2660 if (iwi_reset(sc) != 0) {
2661 device_printf(sc->sc_dev, "could not reset adapter\n");
2665 if (iwi_load_firmware(sc, fw->boot, fw->boot_size) != 0) {
2666 device_printf(sc->sc_dev, "could not load boot firmware\n");
2670 if (iwi_load_ucode(sc, fw->ucode, fw->ucode_size) != 0) {
2671 device_printf(sc->sc_dev, "could not load microcode\n");
2675 iwi_stop_master(sc);
2677 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr);
2678 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
2679 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
2681 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr);
2682 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
2683 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
2685 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr);
2686 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
2687 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
2689 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr);
2690 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
2691 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
2693 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr);
2694 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
2695 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
2697 for (i = 0; i < sc->rxq.count; i++) {
2698 data = &sc->rxq.data[i];
2699 CSR_WRITE_4(sc, data->reg, data->physaddr);
2702 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1);
2704 if (iwi_load_firmware(sc, fw->main, fw->main_size) != 0) {
2705 device_printf(sc->sc_dev, "could not load main firmware\n");
2709 sc->flags |= IWI_FLAG_FW_INITED;
2711 if (iwi_config(sc) != 0) {
2712 device_printf(sc->sc_dev, "device configuration failed\n");
2716 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2717 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2718 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2720 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2722 ifp->if_flags &= ~IFF_OACTIVE;
2723 ifp->if_flags |= IFF_RUNNING;
2727 fail: ifp->if_flags &= ~IFF_UP;
2732 iwi_stop(void *priv)
2734 struct iwi_softc *sc = priv;
2735 struct ieee80211com *ic = &sc->sc_ic;
2736 struct ifnet *ifp = ic->ic_ifp;
2738 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2740 iwi_stop_master(sc);
2742 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET);
2745 iwi_reset_cmd_ring(sc, &sc->cmdq);
2746 iwi_reset_tx_ring(sc, &sc->txq[0]);
2747 iwi_reset_tx_ring(sc, &sc->txq[1]);
2748 iwi_reset_tx_ring(sc, &sc->txq[2]);
2749 iwi_reset_tx_ring(sc, &sc->txq[3]);
2750 iwi_reset_rx_ring(sc, &sc->rxq);
2752 sc->sc_tx_timer = 0;
2754 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2758 iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
2760 struct iwi_softc *sc = arg1;
2761 struct ifnet *ifp = &sc->sc_ic.ic_if;
2762 uint32_t size, buf[128];
2764 lwkt_serialize_enter(ifp->if_serializer);
2766 if (!(sc->flags & IWI_FLAG_FW_INITED)) {
2767 memset(buf, 0, sizeof buf);
2771 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
2772 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
2774 lwkt_serialize_exit(ifp->if_serializer);
2775 return SYSCTL_OUT(req, buf, sizeof buf);
2779 iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
2781 struct iwi_softc *sc = arg1;
2782 struct ifnet *ifp = &sc->sc_ic.ic_if;
2785 lwkt_serialize_enter(ifp->if_serializer);
2786 val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0;
2787 lwkt_serialize_exit(ifp->if_serializer);
2789 return SYSCTL_OUT(req, &val, sizeof val);
2792 static const int8_t iwi_bitmap[256] = {
2793 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2794 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
2795 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2796 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
2797 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2798 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
2799 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2800 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 7,
2801 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2802 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
2803 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2804 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6,
2805 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2806 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,
2807 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,
2808 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, -1
2812 iwi_alloc_ibss_node(struct iwi_softc *sc)
2816 ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
2818 for (i = 0; i < IWI_MAX_IBSSNODE_NBYTE; ++i) {
2821 ret = iwi_bitmap[sc->sc_ibss_node[i]];
2823 sc->sc_ibss_node[i] |= (1 << ret);
2832 iwi_free_ibss_node(struct iwi_softc *sc, int ibss_node)
2836 ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
2837 KASSERT(ibss_node < IWI_MAX_IBSSNODE, ("free ibss node, out of range"));
2839 i = ibss_node / NBBY;
2840 b = ibss_node % NBBY;
2842 sc->sc_ibss_node[i] &= ~(1 << b);